]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
* objcopy.c (setup_section): Set elf_group_id.
[thirdparty/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91
AC
2
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
b7c368d0 4 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
252b5132 5
5e8d7549 6 This file is part of BFD, the Binary File Descriptor library.
252b5132 7
5e8d7549
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
cd123cb7 10 the Free Software Foundation; either version 3 of the License, or
5e8d7549 11 (at your option) any later version.
252b5132 12
5e8d7549
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
252b5132 17
5e8d7549 18 You should have received a copy of the GNU General Public License
b34976b6 19 along with this program; if not, write to the Free Software
cd123cb7
NC
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
252b5132 23
1b74d094
BW
24/*
25SECTION
252b5132
RH
26 ELF backends
27
28 BFD support for ELF formats is being worked on.
29 Currently, the best supported back ends are for sparc and i386
30 (running svr4 or Solaris 2).
31
32 Documentation of the internals of the support code still needs
33 to be written. The code is changing quickly enough that we
661a3fd4 34 haven't bothered yet. */
252b5132 35
7ee38065
MS
36/* For sparc64-cross-sparc32. */
37#define _SYSCALL32
252b5132 38#include "sysdep.h"
3db64b00 39#include "bfd.h"
252b5132
RH
40#include "bfdlink.h"
41#include "libbfd.h"
42#define ARCH_SIZE 0
43#include "elf-bfd.h"
e0e8c97f 44#include "libiberty.h"
ff59fc36 45#include "safe-ctype.h"
252b5132 46
217aa764 47static int elf_sort_sections (const void *, const void *);
c84fca4d 48static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
217aa764
AM
49static bfd_boolean prep_headers (bfd *);
50static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
718175fa
JK
51static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
52static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
53 file_ptr offset);
50b2bdb7 54
252b5132
RH
55/* Swap version information in and out. The version information is
56 currently size independent. If that ever changes, this code will
57 need to move into elfcode.h. */
58
59/* Swap in a Verdef structure. */
60
61void
217aa764
AM
62_bfd_elf_swap_verdef_in (bfd *abfd,
63 const Elf_External_Verdef *src,
64 Elf_Internal_Verdef *dst)
252b5132 65{
dc810e39
AM
66 dst->vd_version = H_GET_16 (abfd, src->vd_version);
67 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
68 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
69 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
70 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
71 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
72 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
73}
74
75/* Swap out a Verdef structure. */
76
77void
217aa764
AM
78_bfd_elf_swap_verdef_out (bfd *abfd,
79 const Elf_Internal_Verdef *src,
80 Elf_External_Verdef *dst)
252b5132 81{
dc810e39
AM
82 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
83 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
84 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
85 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
86 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
87 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
88 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
89}
90
91/* Swap in a Verdaux structure. */
92
93void
217aa764
AM
94_bfd_elf_swap_verdaux_in (bfd *abfd,
95 const Elf_External_Verdaux *src,
96 Elf_Internal_Verdaux *dst)
252b5132 97{
dc810e39
AM
98 dst->vda_name = H_GET_32 (abfd, src->vda_name);
99 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
100}
101
102/* Swap out a Verdaux structure. */
103
104void
217aa764
AM
105_bfd_elf_swap_verdaux_out (bfd *abfd,
106 const Elf_Internal_Verdaux *src,
107 Elf_External_Verdaux *dst)
252b5132 108{
dc810e39
AM
109 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
110 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
111}
112
113/* Swap in a Verneed structure. */
114
115void
217aa764
AM
116_bfd_elf_swap_verneed_in (bfd *abfd,
117 const Elf_External_Verneed *src,
118 Elf_Internal_Verneed *dst)
252b5132 119{
dc810e39
AM
120 dst->vn_version = H_GET_16 (abfd, src->vn_version);
121 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
122 dst->vn_file = H_GET_32 (abfd, src->vn_file);
123 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
124 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
125}
126
127/* Swap out a Verneed structure. */
128
129void
217aa764
AM
130_bfd_elf_swap_verneed_out (bfd *abfd,
131 const Elf_Internal_Verneed *src,
132 Elf_External_Verneed *dst)
252b5132 133{
dc810e39
AM
134 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
135 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
136 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
137 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
138 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
139}
140
141/* Swap in a Vernaux structure. */
142
143void
217aa764
AM
144_bfd_elf_swap_vernaux_in (bfd *abfd,
145 const Elf_External_Vernaux *src,
146 Elf_Internal_Vernaux *dst)
252b5132 147{
dc810e39
AM
148 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
149 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
150 dst->vna_other = H_GET_16 (abfd, src->vna_other);
151 dst->vna_name = H_GET_32 (abfd, src->vna_name);
152 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
153}
154
155/* Swap out a Vernaux structure. */
156
157void
217aa764
AM
158_bfd_elf_swap_vernaux_out (bfd *abfd,
159 const Elf_Internal_Vernaux *src,
160 Elf_External_Vernaux *dst)
252b5132 161{
dc810e39
AM
162 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
163 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
164 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
165 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
166 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
167}
168
169/* Swap in a Versym structure. */
170
171void
217aa764
AM
172_bfd_elf_swap_versym_in (bfd *abfd,
173 const Elf_External_Versym *src,
174 Elf_Internal_Versym *dst)
252b5132 175{
dc810e39 176 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
177}
178
179/* Swap out a Versym structure. */
180
181void
217aa764
AM
182_bfd_elf_swap_versym_out (bfd *abfd,
183 const Elf_Internal_Versym *src,
184 Elf_External_Versym *dst)
252b5132 185{
dc810e39 186 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
187}
188
189/* Standard ELF hash function. Do not change this function; you will
190 cause invalid hash tables to be generated. */
3a99b017 191
252b5132 192unsigned long
217aa764 193bfd_elf_hash (const char *namearg)
252b5132 194{
3a99b017 195 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
196 unsigned long h = 0;
197 unsigned long g;
198 int ch;
199
200 while ((ch = *name++) != '\0')
201 {
202 h = (h << 4) + ch;
203 if ((g = (h & 0xf0000000)) != 0)
204 {
205 h ^= g >> 24;
206 /* The ELF ABI says `h &= ~g', but this is equivalent in
207 this case and on some machines one insn instead of two. */
208 h ^= g;
209 }
210 }
32dfa85d 211 return h & 0xffffffff;
252b5132
RH
212}
213
fdc90cb4
JJ
214/* DT_GNU_HASH hash function. Do not change this function; you will
215 cause invalid hash tables to be generated. */
216
217unsigned long
218bfd_elf_gnu_hash (const char *namearg)
219{
220 const unsigned char *name = (const unsigned char *) namearg;
221 unsigned long h = 5381;
222 unsigned char ch;
223
224 while ((ch = *name++) != '\0')
225 h = (h << 5) + h + ch;
226 return h & 0xffffffff;
227}
228
0c8d6e5c
AM
229/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
230 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
b34976b6 231bfd_boolean
0c8d6e5c 232bfd_elf_allocate_object (bfd *abfd,
0ffa91dd
NC
233 size_t object_size,
234 enum elf_object_id object_id)
252b5132 235{
0ffa91dd
NC
236 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
237 abfd->tdata.any = bfd_zalloc (abfd, object_size);
238 if (abfd->tdata.any == NULL)
239 return FALSE;
252b5132 240
0ffa91dd
NC
241 elf_object_id (abfd) = object_id;
242 elf_program_header_size (abfd) = (bfd_size_type) -1;
b34976b6 243 return TRUE;
252b5132
RH
244}
245
0ffa91dd
NC
246
247bfd_boolean
248bfd_elf_make_generic_object (bfd *abfd)
249{
250 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
251 GENERIC_ELF_TDATA);
252}
253
b34976b6 254bfd_boolean
217aa764 255bfd_elf_mkcorefile (bfd *abfd)
252b5132 256{
c044fabd 257 /* I think this can be done just like an object file. */
0ffa91dd 258 return bfd_elf_make_generic_object (abfd);
252b5132
RH
259}
260
261char *
217aa764 262bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252b5132
RH
263{
264 Elf_Internal_Shdr **i_shdrp;
f075ee0c 265 bfd_byte *shstrtab = NULL;
dc810e39
AM
266 file_ptr offset;
267 bfd_size_type shstrtabsize;
252b5132
RH
268
269 i_shdrp = elf_elfsections (abfd);
74f2e02b
AM
270 if (i_shdrp == 0
271 || shindex >= elf_numsections (abfd)
272 || i_shdrp[shindex] == 0)
f075ee0c 273 return NULL;
252b5132 274
f075ee0c 275 shstrtab = i_shdrp[shindex]->contents;
252b5132
RH
276 if (shstrtab == NULL)
277 {
c044fabd 278 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
279 offset = i_shdrp[shindex]->sh_offset;
280 shstrtabsize = i_shdrp[shindex]->sh_size;
c6c60d09
JJ
281
282 /* Allocate and clear an extra byte at the end, to prevent crashes
283 in case the string table is not terminated. */
3471d59d 284 if (shstrtabsize + 1 <= 1
c6c60d09
JJ
285 || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL
286 || bfd_seek (abfd, offset, SEEK_SET) != 0)
287 shstrtab = NULL;
288 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
289 {
290 if (bfd_get_error () != bfd_error_system_call)
291 bfd_set_error (bfd_error_file_truncated);
292 shstrtab = NULL;
3471d59d
CC
293 /* Once we've failed to read it, make sure we don't keep
294 trying. Otherwise, we'll keep allocating space for
295 the string table over and over. */
296 i_shdrp[shindex]->sh_size = 0;
c6c60d09
JJ
297 }
298 else
299 shstrtab[shstrtabsize] = '\0';
217aa764 300 i_shdrp[shindex]->contents = shstrtab;
252b5132 301 }
f075ee0c 302 return (char *) shstrtab;
252b5132
RH
303}
304
305char *
217aa764
AM
306bfd_elf_string_from_elf_section (bfd *abfd,
307 unsigned int shindex,
308 unsigned int strindex)
252b5132
RH
309{
310 Elf_Internal_Shdr *hdr;
311
312 if (strindex == 0)
313 return "";
314
74f2e02b
AM
315 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
316 return NULL;
317
252b5132
RH
318 hdr = elf_elfsections (abfd)[shindex];
319
320 if (hdr->contents == NULL
321 && bfd_elf_get_str_section (abfd, shindex) == NULL)
322 return NULL;
323
324 if (strindex >= hdr->sh_size)
325 {
1b3a8575 326 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
252b5132 327 (*_bfd_error_handler)
d003868e
AM
328 (_("%B: invalid string offset %u >= %lu for section `%s'"),
329 abfd, strindex, (unsigned long) hdr->sh_size,
1b3a8575 330 (shindex == shstrndx && strindex == hdr->sh_name
252b5132 331 ? ".shstrtab"
1b3a8575 332 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
252b5132
RH
333 return "";
334 }
335
336 return ((char *) hdr->contents) + strindex;
337}
338
6cdc0ccc
AM
339/* Read and convert symbols to internal format.
340 SYMCOUNT specifies the number of symbols to read, starting from
341 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
342 are non-NULL, they are used to store the internal symbols, external
b7c368d0
NC
343 symbols, and symbol section index extensions, respectively.
344 Returns a pointer to the internal symbol buffer (malloced if necessary)
345 or NULL if there were no symbols or some kind of problem. */
6cdc0ccc
AM
346
347Elf_Internal_Sym *
217aa764
AM
348bfd_elf_get_elf_syms (bfd *ibfd,
349 Elf_Internal_Shdr *symtab_hdr,
350 size_t symcount,
351 size_t symoffset,
352 Elf_Internal_Sym *intsym_buf,
353 void *extsym_buf,
354 Elf_External_Sym_Shndx *extshndx_buf)
6cdc0ccc
AM
355{
356 Elf_Internal_Shdr *shndx_hdr;
217aa764 357 void *alloc_ext;
df622259 358 const bfd_byte *esym;
6cdc0ccc
AM
359 Elf_External_Sym_Shndx *alloc_extshndx;
360 Elf_External_Sym_Shndx *shndx;
4dd07732 361 Elf_Internal_Sym *alloc_intsym;
6cdc0ccc
AM
362 Elf_Internal_Sym *isym;
363 Elf_Internal_Sym *isymend;
9c5bfbb7 364 const struct elf_backend_data *bed;
6cdc0ccc
AM
365 size_t extsym_size;
366 bfd_size_type amt;
367 file_ptr pos;
368
e44a2c9c
AM
369 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
370 abort ();
371
6cdc0ccc
AM
372 if (symcount == 0)
373 return intsym_buf;
374
375 /* Normal syms might have section extension entries. */
376 shndx_hdr = NULL;
377 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
378 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
379
380 /* Read the symbols. */
381 alloc_ext = NULL;
382 alloc_extshndx = NULL;
4dd07732 383 alloc_intsym = NULL;
6cdc0ccc
AM
384 bed = get_elf_backend_data (ibfd);
385 extsym_size = bed->s->sizeof_sym;
386 amt = symcount * extsym_size;
387 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
388 if (extsym_buf == NULL)
389 {
d0fb9a8d 390 alloc_ext = bfd_malloc2 (symcount, extsym_size);
6cdc0ccc
AM
391 extsym_buf = alloc_ext;
392 }
393 if (extsym_buf == NULL
394 || bfd_seek (ibfd, pos, SEEK_SET) != 0
395 || bfd_bread (extsym_buf, amt, ibfd) != amt)
396 {
397 intsym_buf = NULL;
398 goto out;
399 }
400
401 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
402 extshndx_buf = NULL;
403 else
404 {
405 amt = symcount * sizeof (Elf_External_Sym_Shndx);
406 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
407 if (extshndx_buf == NULL)
408 {
d0fb9a8d
JJ
409 alloc_extshndx = bfd_malloc2 (symcount,
410 sizeof (Elf_External_Sym_Shndx));
6cdc0ccc
AM
411 extshndx_buf = alloc_extshndx;
412 }
413 if (extshndx_buf == NULL
414 || bfd_seek (ibfd, pos, SEEK_SET) != 0
415 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
416 {
417 intsym_buf = NULL;
418 goto out;
419 }
420 }
421
422 if (intsym_buf == NULL)
423 {
4dd07732
AM
424 alloc_intsym = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
425 intsym_buf = alloc_intsym;
6cdc0ccc
AM
426 if (intsym_buf == NULL)
427 goto out;
428 }
429
430 /* Convert the symbols to internal form. */
431 isymend = intsym_buf + symcount;
432 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
433 isym < isymend;
434 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
435 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
436 {
437 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
438 (*_bfd_error_handler) (_("%B symbol number %lu references "
439 "nonexistent SHT_SYMTAB_SHNDX section"),
440 ibfd, (unsigned long) symoffset);
4dd07732
AM
441 if (alloc_intsym != NULL)
442 free (alloc_intsym);
8384fb8f
AM
443 intsym_buf = NULL;
444 goto out;
445 }
6cdc0ccc
AM
446
447 out:
448 if (alloc_ext != NULL)
449 free (alloc_ext);
450 if (alloc_extshndx != NULL)
451 free (alloc_extshndx);
452
453 return intsym_buf;
454}
455
5cab59f6
AM
456/* Look up a symbol name. */
457const char *
be8dd2ca
AM
458bfd_elf_sym_name (bfd *abfd,
459 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
460 Elf_Internal_Sym *isym,
461 asection *sym_sec)
5cab59f6 462{
26c61ae5 463 const char *name;
5cab59f6 464 unsigned int iname = isym->st_name;
be8dd2ca 465 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 466
138f35cc
JJ
467 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
468 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 469 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
470 {
471 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
472 shindex = elf_elfheader (abfd)->e_shstrndx;
473 }
474
26c61ae5
L
475 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
476 if (name == NULL)
477 name = "(null)";
478 else if (sym_sec && *name == '\0')
479 name = bfd_section_name (abfd, sym_sec);
480
481 return name;
5cab59f6
AM
482}
483
dbb410c3
AM
484/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
485 sections. The first element is the flags, the rest are section
486 pointers. */
487
488typedef union elf_internal_group {
489 Elf_Internal_Shdr *shdr;
490 unsigned int flags;
491} Elf_Internal_Group;
492
b885599b
AM
493/* Return the name of the group signature symbol. Why isn't the
494 signature just a string? */
495
496static const char *
217aa764 497group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 498{
9dce4196 499 Elf_Internal_Shdr *hdr;
9dce4196
AM
500 unsigned char esym[sizeof (Elf64_External_Sym)];
501 Elf_External_Sym_Shndx eshndx;
502 Elf_Internal_Sym isym;
b885599b 503
13792e9d
L
504 /* First we need to ensure the symbol table is available. Make sure
505 that it is a symbol table section. */
4fbb74a6
AM
506 if (ghdr->sh_link >= elf_numsections (abfd))
507 return NULL;
13792e9d
L
508 hdr = elf_elfsections (abfd) [ghdr->sh_link];
509 if (hdr->sh_type != SHT_SYMTAB
510 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
511 return NULL;
512
9dce4196
AM
513 /* Go read the symbol. */
514 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
515 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
516 &isym, esym, &eshndx) == NULL)
b885599b 517 return NULL;
9dce4196 518
26c61ae5 519 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
520}
521
dbb410c3
AM
522/* Set next_in_group list pointer, and group name for NEWSECT. */
523
b34976b6 524static bfd_boolean
217aa764 525setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
526{
527 unsigned int num_group = elf_tdata (abfd)->num_group;
528
529 /* If num_group is zero, read in all SHT_GROUP sections. The count
530 is set to -1 if there are no SHT_GROUP sections. */
531 if (num_group == 0)
532 {
533 unsigned int i, shnum;
534
535 /* First count the number of groups. If we have a SHT_GROUP
536 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 537 shnum = elf_numsections (abfd);
dbb410c3 538 num_group = 0;
08a40648 539
1783205a
NC
540#define IS_VALID_GROUP_SECTION_HEADER(shdr) \
541 ( (shdr)->sh_type == SHT_GROUP \
542 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
543 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
544 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 545
dbb410c3
AM
546 for (i = 0; i < shnum; i++)
547 {
548 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a
NC
549
550 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
dbb410c3
AM
551 num_group += 1;
552 }
553
554 if (num_group == 0)
20dbb49d
L
555 {
556 num_group = (unsigned) -1;
557 elf_tdata (abfd)->num_group = num_group;
558 }
559 else
dbb410c3
AM
560 {
561 /* We keep a list of elf section headers for group sections,
562 so we can find them quickly. */
20dbb49d 563 bfd_size_type amt;
d0fb9a8d 564
20dbb49d 565 elf_tdata (abfd)->num_group = num_group;
d0fb9a8d
JJ
566 elf_tdata (abfd)->group_sect_ptr
567 = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
dbb410c3 568 if (elf_tdata (abfd)->group_sect_ptr == NULL)
b34976b6 569 return FALSE;
dbb410c3
AM
570
571 num_group = 0;
572 for (i = 0; i < shnum; i++)
573 {
574 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a
NC
575
576 if (IS_VALID_GROUP_SECTION_HEADER (shdr))
dbb410c3 577 {
973ffd63 578 unsigned char *src;
dbb410c3
AM
579 Elf_Internal_Group *dest;
580
581 /* Add to list of sections. */
582 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
583 num_group += 1;
584
585 /* Read the raw contents. */
586 BFD_ASSERT (sizeof (*dest) >= 4);
587 amt = shdr->sh_size * sizeof (*dest) / 4;
d0fb9a8d
JJ
588 shdr->contents = bfd_alloc2 (abfd, shdr->sh_size,
589 sizeof (*dest) / 4);
1783205a
NC
590 /* PR binutils/4110: Handle corrupt group headers. */
591 if (shdr->contents == NULL)
592 {
593 _bfd_error_handler
594 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
595 bfd_set_error (bfd_error_bad_value);
596 return FALSE;
597 }
598
599 memset (shdr->contents, 0, amt);
600
601 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
dbb410c3
AM
602 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
603 != shdr->sh_size))
b34976b6 604 return FALSE;
dbb410c3
AM
605
606 /* Translate raw contents, a flag word followed by an
607 array of elf section indices all in target byte order,
608 to the flag word followed by an array of elf section
609 pointers. */
610 src = shdr->contents + shdr->sh_size;
611 dest = (Elf_Internal_Group *) (shdr->contents + amt);
612 while (1)
613 {
614 unsigned int idx;
615
616 src -= 4;
617 --dest;
618 idx = H_GET_32 (abfd, src);
619 if (src == shdr->contents)
620 {
621 dest->flags = idx;
b885599b
AM
622 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
623 shdr->bfd_section->flags
624 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
625 break;
626 }
627 if (idx >= shnum)
628 {
629 ((*_bfd_error_handler)
d003868e 630 (_("%B: invalid SHT_GROUP entry"), abfd));
dbb410c3
AM
631 idx = 0;
632 }
633 dest->shdr = elf_elfsections (abfd)[idx];
634 }
635 }
636 }
637 }
638 }
639
640 if (num_group != (unsigned) -1)
641 {
642 unsigned int i;
643
644 for (i = 0; i < num_group; i++)
645 {
646 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
647 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
648 unsigned int n_elt = shdr->sh_size / 4;
649
650 /* Look through this group's sections to see if current
651 section is a member. */
652 while (--n_elt != 0)
653 if ((++idx)->shdr == hdr)
654 {
e0e8c97f 655 asection *s = NULL;
dbb410c3
AM
656
657 /* We are a member of this group. Go looking through
658 other members to see if any others are linked via
659 next_in_group. */
660 idx = (Elf_Internal_Group *) shdr->contents;
661 n_elt = shdr->sh_size / 4;
662 while (--n_elt != 0)
663 if ((s = (++idx)->shdr->bfd_section) != NULL
945906ff 664 && elf_next_in_group (s) != NULL)
dbb410c3
AM
665 break;
666 if (n_elt != 0)
667 {
dbb410c3
AM
668 /* Snarf the group name from other member, and
669 insert current section in circular list. */
945906ff
AM
670 elf_group_name (newsect) = elf_group_name (s);
671 elf_next_in_group (newsect) = elf_next_in_group (s);
672 elf_next_in_group (s) = newsect;
dbb410c3
AM
673 }
674 else
675 {
dbb410c3
AM
676 const char *gname;
677
b885599b
AM
678 gname = group_signature (abfd, shdr);
679 if (gname == NULL)
b34976b6 680 return FALSE;
945906ff 681 elf_group_name (newsect) = gname;
dbb410c3
AM
682
683 /* Start a circular list with one element. */
945906ff 684 elf_next_in_group (newsect) = newsect;
dbb410c3 685 }
b885599b 686
9dce4196
AM
687 /* If the group section has been created, point to the
688 new member. */
dbb410c3 689 if (shdr->bfd_section != NULL)
945906ff 690 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 691
dbb410c3
AM
692 i = num_group - 1;
693 break;
694 }
695 }
696 }
697
945906ff 698 if (elf_group_name (newsect) == NULL)
dbb410c3 699 {
d003868e
AM
700 (*_bfd_error_handler) (_("%B: no group info for section %A"),
701 abfd, newsect);
dbb410c3 702 }
b34976b6 703 return TRUE;
dbb410c3
AM
704}
705
3d7f7666 706bfd_boolean
dd863624 707_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
708{
709 unsigned int i;
710 unsigned int num_group = elf_tdata (abfd)->num_group;
711 bfd_boolean result = TRUE;
dd863624
L
712 asection *s;
713
714 /* Process SHF_LINK_ORDER. */
715 for (s = abfd->sections; s != NULL; s = s->next)
716 {
717 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
718 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
719 {
720 unsigned int elfsec = this_hdr->sh_link;
721 /* FIXME: The old Intel compiler and old strip/objcopy may
722 not set the sh_link or sh_info fields. Hence we could
723 get the situation where elfsec is 0. */
724 if (elfsec == 0)
725 {
4fbb74a6 726 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
dd863624
L
727 if (bed->link_order_error_handler)
728 bed->link_order_error_handler
729 (_("%B: warning: sh_link not set for section `%A'"),
730 abfd, s);
731 }
732 else
733 {
4fbb74a6 734 asection *link = NULL;
25bbc984 735
4fbb74a6
AM
736 if (elfsec < elf_numsections (abfd))
737 {
738 this_hdr = elf_elfsections (abfd)[elfsec];
739 link = this_hdr->bfd_section;
740 }
25bbc984
L
741
742 /* PR 1991, 2008:
743 Some strip/objcopy may leave an incorrect value in
744 sh_link. We don't want to proceed. */
25bbc984
L
745 if (link == NULL)
746 {
747 (*_bfd_error_handler)
748 (_("%B: sh_link [%d] in section `%A' is incorrect"),
749 s->owner, s, elfsec);
750 result = FALSE;
751 }
752
753 elf_linked_to_section (s) = link;
dd863624
L
754 }
755 }
756 }
3d7f7666 757
dd863624 758 /* Process section groups. */
3d7f7666
L
759 if (num_group == (unsigned) -1)
760 return result;
761
762 for (i = 0; i < num_group; i++)
763 {
764 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
765 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
766 unsigned int n_elt = shdr->sh_size / 4;
767
768 while (--n_elt != 0)
769 if ((++idx)->shdr->bfd_section)
770 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
771 else if (idx->shdr->sh_type == SHT_RELA
772 || idx->shdr->sh_type == SHT_REL)
773 /* We won't include relocation sections in section groups in
774 output object files. We adjust the group section size here
775 so that relocatable link will work correctly when
776 relocation sections are in section group in input object
777 files. */
778 shdr->bfd_section->size -= 4;
779 else
780 {
781 /* There are some unknown sections in the group. */
782 (*_bfd_error_handler)
d003868e
AM
783 (_("%B: unknown [%d] section `%s' in group [%s]"),
784 abfd,
3d7f7666 785 (unsigned int) idx->shdr->sh_type,
1b3a8575
AM
786 bfd_elf_string_from_elf_section (abfd,
787 (elf_elfheader (abfd)
788 ->e_shstrndx),
789 idx->shdr->sh_name),
3d7f7666
L
790 shdr->bfd_section->name);
791 result = FALSE;
792 }
793 }
794 return result;
795}
796
72adc230
AM
797bfd_boolean
798bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
799{
800 return elf_next_in_group (sec) != NULL;
801}
802
252b5132
RH
803/* Make a BFD section from an ELF section. We store a pointer to the
804 BFD section in the bfd_section field of the header. */
805
b34976b6 806bfd_boolean
217aa764
AM
807_bfd_elf_make_section_from_shdr (bfd *abfd,
808 Elf_Internal_Shdr *hdr,
6dc132d9
L
809 const char *name,
810 int shindex)
252b5132
RH
811{
812 asection *newsect;
813 flagword flags;
9c5bfbb7 814 const struct elf_backend_data *bed;
252b5132
RH
815
816 if (hdr->bfd_section != NULL)
817 {
818 BFD_ASSERT (strcmp (name,
819 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
b34976b6 820 return TRUE;
252b5132
RH
821 }
822
823 newsect = bfd_make_section_anyway (abfd, name);
824 if (newsect == NULL)
b34976b6 825 return FALSE;
252b5132 826
1829f4b2
AM
827 hdr->bfd_section = newsect;
828 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 829 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 830
2f89ff8d
L
831 /* Always use the real type/flags. */
832 elf_section_type (newsect) = hdr->sh_type;
833 elf_section_flags (newsect) = hdr->sh_flags;
834
252b5132
RH
835 newsect->filepos = hdr->sh_offset;
836
837 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
838 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
839 || ! bfd_set_section_alignment (abfd, newsect,
72de5009 840 bfd_log2 (hdr->sh_addralign)))
b34976b6 841 return FALSE;
252b5132
RH
842
843 flags = SEC_NO_FLAGS;
844 if (hdr->sh_type != SHT_NOBITS)
845 flags |= SEC_HAS_CONTENTS;
dbb410c3 846 if (hdr->sh_type == SHT_GROUP)
b3096250 847 flags |= SEC_GROUP | SEC_EXCLUDE;
252b5132
RH
848 if ((hdr->sh_flags & SHF_ALLOC) != 0)
849 {
850 flags |= SEC_ALLOC;
851 if (hdr->sh_type != SHT_NOBITS)
852 flags |= SEC_LOAD;
853 }
854 if ((hdr->sh_flags & SHF_WRITE) == 0)
855 flags |= SEC_READONLY;
856 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
857 flags |= SEC_CODE;
858 else if ((flags & SEC_LOAD) != 0)
859 flags |= SEC_DATA;
f5fa8ca2
JJ
860 if ((hdr->sh_flags & SHF_MERGE) != 0)
861 {
862 flags |= SEC_MERGE;
863 newsect->entsize = hdr->sh_entsize;
864 if ((hdr->sh_flags & SHF_STRINGS) != 0)
865 flags |= SEC_STRINGS;
866 }
dbb410c3
AM
867 if (hdr->sh_flags & SHF_GROUP)
868 if (!setup_group (abfd, hdr, newsect))
b34976b6 869 return FALSE;
13ae64f3
JJ
870 if ((hdr->sh_flags & SHF_TLS) != 0)
871 flags |= SEC_THREAD_LOCAL;
252b5132 872
3d2b39cf 873 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 874 {
3d2b39cf
L
875 /* The debugging sections appear to be recognized only by name,
876 not any sort of flag. Their SEC_ALLOC bits are cleared. */
877 static const struct
878 {
879 const char *name;
880 int len;
881 } debug_sections [] =
882 {
0112cd26 883 { STRING_COMMA_LEN ("debug") }, /* 'd' */
3d2b39cf
L
884 { NULL, 0 }, /* 'e' */
885 { NULL, 0 }, /* 'f' */
0112cd26 886 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
3d2b39cf
L
887 { NULL, 0 }, /* 'h' */
888 { NULL, 0 }, /* 'i' */
889 { NULL, 0 }, /* 'j' */
890 { NULL, 0 }, /* 'k' */
0112cd26 891 { STRING_COMMA_LEN ("line") }, /* 'l' */
3d2b39cf
L
892 { NULL, 0 }, /* 'm' */
893 { NULL, 0 }, /* 'n' */
894 { NULL, 0 }, /* 'o' */
895 { NULL, 0 }, /* 'p' */
896 { NULL, 0 }, /* 'q' */
897 { NULL, 0 }, /* 'r' */
1b315056
CS
898 { STRING_COMMA_LEN ("stab") }, /* 's' */
899 { NULL, 0 }, /* 't' */
900 { NULL, 0 }, /* 'u' */
901 { NULL, 0 }, /* 'v' */
902 { NULL, 0 }, /* 'w' */
903 { NULL, 0 }, /* 'x' */
904 { NULL, 0 }, /* 'y' */
905 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
3d2b39cf 906 };
08a40648 907
3d2b39cf
L
908 if (name [0] == '.')
909 {
910 int i = name [1] - 'd';
911 if (i >= 0
912 && i < (int) ARRAY_SIZE (debug_sections)
913 && debug_sections [i].name != NULL
914 && strncmp (&name [1], debug_sections [i].name,
915 debug_sections [i].len) == 0)
916 flags |= SEC_DEBUGGING;
917 }
918 }
252b5132
RH
919
920 /* As a GNU extension, if the name begins with .gnu.linkonce, we
921 only link a single copy of the section. This is used to support
922 g++. g++ will emit each template expansion in its own section.
923 The symbols will be defined as weak, so that multiple definitions
924 are permitted. The GNU linker extension is to actually discard
925 all but one of the sections. */
0112cd26 926 if (CONST_STRNEQ (name, ".gnu.linkonce")
b885599b 927 && elf_next_in_group (newsect) == NULL)
252b5132
RH
928 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
929
fa152c49
JW
930 bed = get_elf_backend_data (abfd);
931 if (bed->elf_backend_section_flags)
932 if (! bed->elf_backend_section_flags (&flags, hdr))
b34976b6 933 return FALSE;
fa152c49 934
252b5132 935 if (! bfd_set_section_flags (abfd, newsect, flags))
b34976b6 936 return FALSE;
252b5132 937
718175fa
JK
938 /* We do not parse the PT_NOTE segments as we are interested even in the
939 separate debug info files which may have the segments offsets corrupted.
940 PT_NOTEs from the core files are currently not parsed using BFD. */
941 if (hdr->sh_type == SHT_NOTE)
942 {
baea7ef1 943 bfd_byte *contents;
718175fa 944
baea7ef1 945 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
718175fa
JK
946 return FALSE;
947
baea7ef1 948 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
718175fa
JK
949 free (contents);
950 }
951
252b5132
RH
952 if ((flags & SEC_ALLOC) != 0)
953 {
954 Elf_Internal_Phdr *phdr;
6ffd7900
AM
955 unsigned int i, nload;
956
957 /* Some ELF linkers produce binaries with all the program header
958 p_paddr fields zero. If we have such a binary with more than
959 one PT_LOAD header, then leave the section lma equal to vma
960 so that we don't create sections with overlapping lma. */
961 phdr = elf_tdata (abfd)->phdr;
962 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
963 if (phdr->p_paddr != 0)
964 break;
965 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
966 ++nload;
967 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
968 return TRUE;
252b5132 969
252b5132
RH
970 phdr = elf_tdata (abfd)->phdr;
971 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
972 {
88967714
AM
973 /* This section is part of this segment if its file
974 offset plus size lies within the segment's memory
975 span and, if the section is loaded, the extent of the
976 loaded data lies within the extent of the segment.
977
978 Note - we used to check the p_paddr field as well, and
979 refuse to set the LMA if it was 0. This is wrong
980 though, as a perfectly valid initialised segment can
981 have a p_paddr of zero. Some architectures, eg ARM,
982 place special significance on the address 0 and
983 executables need to be able to have a segment which
984 covers this address. */
985 if (phdr->p_type == PT_LOAD
986 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
987 && (hdr->sh_offset + hdr->sh_size
988 <= phdr->p_offset + phdr->p_memsz)
989 && ((flags & SEC_LOAD) == 0
990 || (hdr->sh_offset + hdr->sh_size
991 <= phdr->p_offset + phdr->p_filesz)))
252b5132 992 {
88967714
AM
993 if ((flags & SEC_LOAD) == 0)
994 newsect->lma = (phdr->p_paddr
995 + hdr->sh_addr - phdr->p_vaddr);
996 else
997 /* We used to use the same adjustment for SEC_LOAD
998 sections, but that doesn't work if the segment
999 is packed with code from multiple VMAs.
1000 Instead we calculate the section LMA based on
1001 the segment LMA. It is assumed that the
1002 segment will contain sections with contiguous
1003 LMAs, even if the VMAs are not. */
1004 newsect->lma = (phdr->p_paddr
1005 + hdr->sh_offset - phdr->p_offset);
1006
1007 /* With contiguous segments, we can't tell from file
1008 offsets whether a section with zero size should
1009 be placed at the end of one segment or the
1010 beginning of the next. Decide based on vaddr. */
1011 if (hdr->sh_addr >= phdr->p_vaddr
1012 && (hdr->sh_addr + hdr->sh_size
1013 <= phdr->p_vaddr + phdr->p_memsz))
1014 break;
252b5132
RH
1015 }
1016 }
1017 }
1018
b34976b6 1019 return TRUE;
252b5132
RH
1020}
1021
1022/*
1023INTERNAL_FUNCTION
1024 bfd_elf_find_section
1025
1026SYNOPSIS
1027 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
1028
1029DESCRIPTION
1030 Helper functions for GDB to locate the string tables.
1031 Since BFD hides string tables from callers, GDB needs to use an
1032 internal hook to find them. Sun's .stabstr, in particular,
1033 isn't even pointed to by the .stab section, so ordinary
1034 mechanisms wouldn't work to find it, even if we had some.
1035*/
1036
1037struct elf_internal_shdr *
217aa764 1038bfd_elf_find_section (bfd *abfd, char *name)
252b5132
RH
1039{
1040 Elf_Internal_Shdr **i_shdrp;
1041 char *shstrtab;
1042 unsigned int max;
1043 unsigned int i;
1044
1045 i_shdrp = elf_elfsections (abfd);
1046 if (i_shdrp != NULL)
1047 {
9ad5cbcf
AM
1048 shstrtab = bfd_elf_get_str_section (abfd,
1049 elf_elfheader (abfd)->e_shstrndx);
252b5132
RH
1050 if (shstrtab != NULL)
1051 {
9ad5cbcf 1052 max = elf_numsections (abfd);
252b5132
RH
1053 for (i = 1; i < max; i++)
1054 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
1055 return i_shdrp[i];
1056 }
1057 }
1058 return 0;
1059}
1060
1061const char *const bfd_elf_section_type_names[] = {
1062 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1063 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1064 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1065};
1066
1049f94e 1067/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1068 output, and the reloc is against an external symbol, and nothing
1069 has given us any additional addend, the resulting reloc will also
1070 be against the same symbol. In such a case, we don't want to
1071 change anything about the way the reloc is handled, since it will
1072 all be done at final link time. Rather than put special case code
1073 into bfd_perform_relocation, all the reloc types use this howto
1074 function. It just short circuits the reloc if producing
1049f94e 1075 relocatable output against an external symbol. */
252b5132 1076
252b5132 1077bfd_reloc_status_type
217aa764
AM
1078bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1079 arelent *reloc_entry,
1080 asymbol *symbol,
1081 void *data ATTRIBUTE_UNUSED,
1082 asection *input_section,
1083 bfd *output_bfd,
1084 char **error_message ATTRIBUTE_UNUSED)
1085{
1086 if (output_bfd != NULL
252b5132
RH
1087 && (symbol->flags & BSF_SECTION_SYM) == 0
1088 && (! reloc_entry->howto->partial_inplace
1089 || reloc_entry->addend == 0))
1090 {
1091 reloc_entry->address += input_section->output_offset;
1092 return bfd_reloc_ok;
1093 }
1094
1095 return bfd_reloc_continue;
1096}
1097\f
0ac4564e
L
1098/* Copy the program header and other data from one object module to
1099 another. */
252b5132 1100
b34976b6 1101bfd_boolean
217aa764 1102_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050
L
1103{
1104 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1105 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 1106 return TRUE;
2d502050
L
1107
1108 BFD_ASSERT (!elf_flags_init (obfd)
1109 || (elf_elfheader (obfd)->e_flags
1110 == elf_elfheader (ibfd)->e_flags));
1111
0ac4564e 1112 elf_gp (obfd) = elf_gp (ibfd);
2d502050 1113 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
b34976b6 1114 elf_flags_init (obfd) = TRUE;
104d59d1
JM
1115
1116 /* Copy object attributes. */
1117 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1118
b34976b6 1119 return TRUE;
2d502050
L
1120}
1121
cedc298e
L
1122static const char *
1123get_segment_type (unsigned int p_type)
1124{
1125 const char *pt;
1126 switch (p_type)
1127 {
1128 case PT_NULL: pt = "NULL"; break;
1129 case PT_LOAD: pt = "LOAD"; break;
1130 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1131 case PT_INTERP: pt = "INTERP"; break;
1132 case PT_NOTE: pt = "NOTE"; break;
1133 case PT_SHLIB: pt = "SHLIB"; break;
1134 case PT_PHDR: pt = "PHDR"; break;
1135 case PT_TLS: pt = "TLS"; break;
1136 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1137 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1138 case PT_GNU_RELRO: pt = "RELRO"; break;
1139 default: pt = NULL; break;
1140 }
1141 return pt;
1142}
1143
f0b79d91
L
1144/* Print out the program headers. */
1145
b34976b6 1146bfd_boolean
217aa764 1147_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1148{
217aa764 1149 FILE *f = farg;
252b5132
RH
1150 Elf_Internal_Phdr *p;
1151 asection *s;
1152 bfd_byte *dynbuf = NULL;
1153
1154 p = elf_tdata (abfd)->phdr;
1155 if (p != NULL)
1156 {
1157 unsigned int i, c;
1158
1159 fprintf (f, _("\nProgram Header:\n"));
1160 c = elf_elfheader (abfd)->e_phnum;
1161 for (i = 0; i < c; i++, p++)
1162 {
cedc298e 1163 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1164 char buf[20];
1165
cedc298e 1166 if (pt == NULL)
252b5132 1167 {
cedc298e
L
1168 sprintf (buf, "0x%lx", p->p_type);
1169 pt = buf;
252b5132 1170 }
dc810e39 1171 fprintf (f, "%8s off 0x", pt);
60b89a18 1172 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1173 fprintf (f, " vaddr 0x");
60b89a18 1174 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1175 fprintf (f, " paddr 0x");
60b89a18 1176 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1177 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1178 fprintf (f, " filesz 0x");
60b89a18 1179 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1180 fprintf (f, " memsz 0x");
60b89a18 1181 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1182 fprintf (f, " flags %c%c%c",
1183 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1184 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1185 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1186 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1187 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1188 fprintf (f, "\n");
1189 }
1190 }
1191
1192 s = bfd_get_section_by_name (abfd, ".dynamic");
1193 if (s != NULL)
1194 {
cb33740c 1195 unsigned int elfsec;
dc810e39 1196 unsigned long shlink;
252b5132
RH
1197 bfd_byte *extdyn, *extdynend;
1198 size_t extdynsize;
217aa764 1199 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1200
1201 fprintf (f, _("\nDynamic Section:\n"));
1202
eea6121a 1203 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1204 goto error_return;
1205
1206 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1207 if (elfsec == SHN_BAD)
252b5132 1208 goto error_return;
dc810e39 1209 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1210
1211 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1212 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1213
1214 extdyn = dynbuf;
eea6121a 1215 extdynend = extdyn + s->size;
252b5132
RH
1216 for (; extdyn < extdynend; extdyn += extdynsize)
1217 {
1218 Elf_Internal_Dyn dyn;
ad9563d6 1219 const char *name = "";
252b5132 1220 char ab[20];
b34976b6 1221 bfd_boolean stringp;
ad9563d6 1222 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1223
217aa764 1224 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1225
1226 if (dyn.d_tag == DT_NULL)
1227 break;
1228
b34976b6 1229 stringp = FALSE;
252b5132
RH
1230 switch (dyn.d_tag)
1231 {
1232 default:
ad9563d6
CM
1233 if (bed->elf_backend_get_target_dtag)
1234 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1235
1236 if (!strcmp (name, ""))
1237 {
1238 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1239 name = ab;
1240 }
252b5132
RH
1241 break;
1242
b34976b6 1243 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
252b5132
RH
1244 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1245 case DT_PLTGOT: name = "PLTGOT"; break;
1246 case DT_HASH: name = "HASH"; break;
1247 case DT_STRTAB: name = "STRTAB"; break;
1248 case DT_SYMTAB: name = "SYMTAB"; break;
1249 case DT_RELA: name = "RELA"; break;
1250 case DT_RELASZ: name = "RELASZ"; break;
1251 case DT_RELAENT: name = "RELAENT"; break;
1252 case DT_STRSZ: name = "STRSZ"; break;
1253 case DT_SYMENT: name = "SYMENT"; break;
1254 case DT_INIT: name = "INIT"; break;
1255 case DT_FINI: name = "FINI"; break;
b34976b6
AM
1256 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1257 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
252b5132
RH
1258 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1259 case DT_REL: name = "REL"; break;
1260 case DT_RELSZ: name = "RELSZ"; break;
1261 case DT_RELENT: name = "RELENT"; break;
1262 case DT_PLTREL: name = "PLTREL"; break;
1263 case DT_DEBUG: name = "DEBUG"; break;
1264 case DT_TEXTREL: name = "TEXTREL"; break;
1265 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1266 case DT_BIND_NOW: name = "BIND_NOW"; break;
1267 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1268 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1269 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1270 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
b34976b6 1271 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
94558834
L
1272 case DT_FLAGS: name = "FLAGS"; break;
1273 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1274 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1275 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1276 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1277 case DT_MOVEENT: name = "MOVEENT"; break;
1278 case DT_MOVESZ: name = "MOVESZ"; break;
1279 case DT_FEATURE: name = "FEATURE"; break;
1280 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1281 case DT_SYMINSZ: name = "SYMINSZ"; break;
1282 case DT_SYMINENT: name = "SYMINENT"; break;
b34976b6
AM
1283 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1284 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1285 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
94558834
L
1286 case DT_PLTPAD: name = "PLTPAD"; break;
1287 case DT_MOVETAB: name = "MOVETAB"; break;
1288 case DT_SYMINFO: name = "SYMINFO"; break;
1289 case DT_RELACOUNT: name = "RELACOUNT"; break;
1290 case DT_RELCOUNT: name = "RELCOUNT"; break;
1291 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1292 case DT_VERSYM: name = "VERSYM"; break;
1293 case DT_VERDEF: name = "VERDEF"; break;
1294 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1295 case DT_VERNEED: name = "VERNEED"; break;
1296 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
b34976b6 1297 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
94558834 1298 case DT_USED: name = "USED"; break;
b34976b6 1299 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
fdc90cb4 1300 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1301 }
1302
ad9563d6 1303 fprintf (f, " %-20s ", name);
252b5132 1304 if (! stringp)
a1f3c56e
AN
1305 {
1306 fprintf (f, "0x");
1307 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1308 }
252b5132
RH
1309 else
1310 {
1311 const char *string;
dc810e39 1312 unsigned int tagv = dyn.d_un.d_val;
252b5132 1313
dc810e39 1314 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1315 if (string == NULL)
1316 goto error_return;
1317 fprintf (f, "%s", string);
1318 }
1319 fprintf (f, "\n");
1320 }
1321
1322 free (dynbuf);
1323 dynbuf = NULL;
1324 }
1325
1326 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1327 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1328 {
fc0e6df6 1329 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
b34976b6 1330 return FALSE;
252b5132
RH
1331 }
1332
1333 if (elf_dynverdef (abfd) != 0)
1334 {
1335 Elf_Internal_Verdef *t;
1336
1337 fprintf (f, _("\nVersion definitions:\n"));
1338 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1339 {
1340 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1341 t->vd_flags, t->vd_hash,
1342 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1343 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1344 {
1345 Elf_Internal_Verdaux *a;
1346
1347 fprintf (f, "\t");
1348 for (a = t->vd_auxptr->vda_nextptr;
1349 a != NULL;
1350 a = a->vda_nextptr)
d0fb9a8d
JJ
1351 fprintf (f, "%s ",
1352 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1353 fprintf (f, "\n");
1354 }
1355 }
1356 }
1357
1358 if (elf_dynverref (abfd) != 0)
1359 {
1360 Elf_Internal_Verneed *t;
1361
1362 fprintf (f, _("\nVersion References:\n"));
1363 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1364 {
1365 Elf_Internal_Vernaux *a;
1366
d0fb9a8d
JJ
1367 fprintf (f, _(" required from %s:\n"),
1368 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1369 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1370 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1371 a->vna_flags, a->vna_other,
1372 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1373 }
1374 }
1375
b34976b6 1376 return TRUE;
252b5132
RH
1377
1378 error_return:
1379 if (dynbuf != NULL)
1380 free (dynbuf);
b34976b6 1381 return FALSE;
252b5132
RH
1382}
1383
1384/* Display ELF-specific fields of a symbol. */
1385
1386void
217aa764
AM
1387bfd_elf_print_symbol (bfd *abfd,
1388 void *filep,
1389 asymbol *symbol,
1390 bfd_print_symbol_type how)
252b5132 1391{
217aa764 1392 FILE *file = filep;
252b5132
RH
1393 switch (how)
1394 {
1395 case bfd_print_symbol_name:
1396 fprintf (file, "%s", symbol->name);
1397 break;
1398 case bfd_print_symbol_more:
1399 fprintf (file, "elf ");
60b89a18 1400 bfd_fprintf_vma (abfd, file, symbol->value);
0af1713e 1401 fprintf (file, " %lx", (unsigned long) symbol->flags);
252b5132
RH
1402 break;
1403 case bfd_print_symbol_all:
1404 {
4e8a9624
AM
1405 const char *section_name;
1406 const char *name = NULL;
9c5bfbb7 1407 const struct elf_backend_data *bed;
7a13edea 1408 unsigned char st_other;
dbb410c3 1409 bfd_vma val;
c044fabd 1410
252b5132 1411 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1412
1413 bed = get_elf_backend_data (abfd);
1414 if (bed->elf_backend_print_symbol_all)
c044fabd 1415 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1416
1417 if (name == NULL)
1418 {
7ee38065 1419 name = symbol->name;
217aa764 1420 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
1421 }
1422
252b5132
RH
1423 fprintf (file, " %s\t", section_name);
1424 /* Print the "other" value for a symbol. For common symbols,
1425 we've already printed the size; now print the alignment.
1426 For other symbols, we have no specified alignment, and
1427 we've printed the address; now print the size. */
dcf6c779 1428 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
1429 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1430 else
1431 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1432 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
1433
1434 /* If we have version information, print it. */
1435 if (elf_tdata (abfd)->dynversym_section != 0
1436 && (elf_tdata (abfd)->dynverdef_section != 0
1437 || elf_tdata (abfd)->dynverref_section != 0))
1438 {
1439 unsigned int vernum;
1440 const char *version_string;
1441
1442 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1443
1444 if (vernum == 0)
1445 version_string = "";
1446 else if (vernum == 1)
1447 version_string = "Base";
1448 else if (vernum <= elf_tdata (abfd)->cverdefs)
1449 version_string =
1450 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1451 else
1452 {
1453 Elf_Internal_Verneed *t;
1454
1455 version_string = "";
1456 for (t = elf_tdata (abfd)->verref;
1457 t != NULL;
1458 t = t->vn_nextref)
1459 {
1460 Elf_Internal_Vernaux *a;
1461
1462 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1463 {
1464 if (a->vna_other == vernum)
1465 {
1466 version_string = a->vna_nodename;
1467 break;
1468 }
1469 }
1470 }
1471 }
1472
1473 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1474 fprintf (file, " %-11s", version_string);
1475 else
1476 {
1477 int i;
1478
1479 fprintf (file, " (%s)", version_string);
1480 for (i = 10 - strlen (version_string); i > 0; --i)
1481 putc (' ', file);
1482 }
1483 }
1484
1485 /* If the st_other field is not zero, print it. */
7a13edea 1486 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 1487
7a13edea
NC
1488 switch (st_other)
1489 {
1490 case 0: break;
1491 case STV_INTERNAL: fprintf (file, " .internal"); break;
1492 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1493 case STV_PROTECTED: fprintf (file, " .protected"); break;
1494 default:
1495 /* Some other non-defined flags are also present, so print
1496 everything hex. */
1497 fprintf (file, " 0x%02x", (unsigned int) st_other);
1498 }
252b5132 1499
587ff49e 1500 fprintf (file, " %s", name);
252b5132
RH
1501 }
1502 break;
1503 }
1504}
252b5132 1505
252b5132
RH
1506/* Allocate an ELF string table--force the first byte to be zero. */
1507
1508struct bfd_strtab_hash *
217aa764 1509_bfd_elf_stringtab_init (void)
252b5132
RH
1510{
1511 struct bfd_strtab_hash *ret;
1512
1513 ret = _bfd_stringtab_init ();
1514 if (ret != NULL)
1515 {
1516 bfd_size_type loc;
1517
b34976b6 1518 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
252b5132
RH
1519 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1520 if (loc == (bfd_size_type) -1)
1521 {
1522 _bfd_stringtab_free (ret);
1523 ret = NULL;
1524 }
1525 }
1526 return ret;
1527}
1528\f
1529/* ELF .o/exec file reading */
1530
c044fabd 1531/* Create a new bfd section from an ELF section header. */
252b5132 1532
b34976b6 1533bfd_boolean
217aa764 1534bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 1535{
4fbb74a6
AM
1536 Elf_Internal_Shdr *hdr;
1537 Elf_Internal_Ehdr *ehdr;
1538 const struct elf_backend_data *bed;
90937f86 1539 const char *name;
252b5132 1540
4fbb74a6
AM
1541 if (shindex >= elf_numsections (abfd))
1542 return FALSE;
1543
1544 hdr = elf_elfsections (abfd)[shindex];
1545 ehdr = elf_elfheader (abfd);
1546 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 1547 hdr->sh_name);
933d961a
JJ
1548 if (name == NULL)
1549 return FALSE;
252b5132 1550
4fbb74a6 1551 bed = get_elf_backend_data (abfd);
252b5132
RH
1552 switch (hdr->sh_type)
1553 {
1554 case SHT_NULL:
1555 /* Inactive section. Throw it away. */
b34976b6 1556 return TRUE;
252b5132
RH
1557
1558 case SHT_PROGBITS: /* Normal section with contents. */
252b5132
RH
1559 case SHT_NOBITS: /* .bss section. */
1560 case SHT_HASH: /* .hash section. */
1561 case SHT_NOTE: /* .note section. */
25e27870
L
1562 case SHT_INIT_ARRAY: /* .init_array section. */
1563 case SHT_FINI_ARRAY: /* .fini_array section. */
1564 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 1565 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 1566 case SHT_GNU_HASH: /* .gnu.hash section. */
6dc132d9 1567 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
252b5132 1568
797fc050 1569 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 1570 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 1571 return FALSE;
8e0ed13f
NC
1572 if (hdr->sh_link > elf_numsections (abfd)
1573 || elf_elfsections (abfd)[hdr->sh_link] == NULL)
1574 return FALSE;
797fc050
AM
1575 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1576 {
1577 Elf_Internal_Shdr *dynsymhdr;
1578
1579 /* The shared libraries distributed with hpux11 have a bogus
1580 sh_link field for the ".dynamic" section. Find the
1581 string table for the ".dynsym" section instead. */
1582 if (elf_dynsymtab (abfd) != 0)
1583 {
1584 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1585 hdr->sh_link = dynsymhdr->sh_link;
1586 }
1587 else
1588 {
1589 unsigned int i, num_sec;
1590
1591 num_sec = elf_numsections (abfd);
1592 for (i = 1; i < num_sec; i++)
1593 {
1594 dynsymhdr = elf_elfsections (abfd)[i];
1595 if (dynsymhdr->sh_type == SHT_DYNSYM)
1596 {
1597 hdr->sh_link = dynsymhdr->sh_link;
1598 break;
1599 }
1600 }
1601 }
1602 }
1603 break;
1604
252b5132
RH
1605 case SHT_SYMTAB: /* A symbol table */
1606 if (elf_onesymtab (abfd) == shindex)
b34976b6 1607 return TRUE;
252b5132 1608
a50b2160
JJ
1609 if (hdr->sh_entsize != bed->s->sizeof_sym)
1610 return FALSE;
252b5132
RH
1611 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1612 elf_onesymtab (abfd) = shindex;
1613 elf_tdata (abfd)->symtab_hdr = *hdr;
1614 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1615 abfd->flags |= HAS_SYMS;
1616
1617 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
1618 SHF_ALLOC is set, and this is a shared object, then we also
1619 treat this section as a BFD section. We can not base the
1620 decision purely on SHF_ALLOC, because that flag is sometimes
1621 set in a relocatable object file, which would confuse the
1622 linker. */
252b5132
RH
1623 if ((hdr->sh_flags & SHF_ALLOC) != 0
1624 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
1625 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1626 shindex))
b34976b6 1627 return FALSE;
252b5132 1628
1b3a8575
AM
1629 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1630 can't read symbols without that section loaded as well. It
1631 is most likely specified by the next section header. */
1632 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1633 {
1634 unsigned int i, num_sec;
1635
1636 num_sec = elf_numsections (abfd);
1637 for (i = shindex + 1; i < num_sec; i++)
1638 {
1639 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1640 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1641 && hdr2->sh_link == shindex)
1642 break;
1643 }
1644 if (i == num_sec)
1645 for (i = 1; i < shindex; i++)
1646 {
1647 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1648 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1649 && hdr2->sh_link == shindex)
1650 break;
1651 }
1652 if (i != shindex)
1653 return bfd_section_from_shdr (abfd, i);
1654 }
b34976b6 1655 return TRUE;
252b5132
RH
1656
1657 case SHT_DYNSYM: /* A dynamic symbol table */
1658 if (elf_dynsymtab (abfd) == shindex)
b34976b6 1659 return TRUE;
252b5132 1660
a50b2160
JJ
1661 if (hdr->sh_entsize != bed->s->sizeof_sym)
1662 return FALSE;
252b5132
RH
1663 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1664 elf_dynsymtab (abfd) = shindex;
1665 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1666 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1667 abfd->flags |= HAS_SYMS;
1668
1669 /* Besides being a symbol table, we also treat this as a regular
1670 section, so that objcopy can handle it. */
6dc132d9 1671 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
252b5132 1672
9ad5cbcf
AM
1673 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1674 if (elf_symtab_shndx (abfd) == shindex)
b34976b6 1675 return TRUE;
9ad5cbcf 1676
1b3a8575 1677 BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
9ad5cbcf
AM
1678 elf_symtab_shndx (abfd) = shindex;
1679 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1680 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
b34976b6 1681 return TRUE;
9ad5cbcf 1682
252b5132
RH
1683 case SHT_STRTAB: /* A string table */
1684 if (hdr->bfd_section != NULL)
b34976b6 1685 return TRUE;
252b5132
RH
1686 if (ehdr->e_shstrndx == shindex)
1687 {
1688 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1689 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
b34976b6 1690 return TRUE;
252b5132 1691 }
1b3a8575
AM
1692 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1693 {
1694 symtab_strtab:
1695 elf_tdata (abfd)->strtab_hdr = *hdr;
1696 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1697 return TRUE;
1698 }
1699 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1700 {
1701 dynsymtab_strtab:
1702 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1703 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1704 elf_elfsections (abfd)[shindex] = hdr;
1705 /* We also treat this as a regular section, so that objcopy
1706 can handle it. */
6dc132d9
L
1707 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1708 shindex);
1b3a8575 1709 }
252b5132 1710
1b3a8575
AM
1711 /* If the string table isn't one of the above, then treat it as a
1712 regular section. We need to scan all the headers to be sure,
1713 just in case this strtab section appeared before the above. */
1714 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1715 {
1716 unsigned int i, num_sec;
252b5132 1717
1b3a8575
AM
1718 num_sec = elf_numsections (abfd);
1719 for (i = 1; i < num_sec; i++)
1720 {
1721 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1722 if (hdr2->sh_link == shindex)
1723 {
933d961a
JJ
1724 /* Prevent endless recursion on broken objects. */
1725 if (i == shindex)
1726 return FALSE;
1b3a8575
AM
1727 if (! bfd_section_from_shdr (abfd, i))
1728 return FALSE;
1729 if (elf_onesymtab (abfd) == i)
1730 goto symtab_strtab;
1731 if (elf_dynsymtab (abfd) == i)
1732 goto dynsymtab_strtab;
1733 }
1734 }
1735 }
6dc132d9 1736 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
252b5132
RH
1737
1738 case SHT_REL:
1739 case SHT_RELA:
1740 /* *These* do a lot of work -- but build no sections! */
1741 {
1742 asection *target_sect;
1743 Elf_Internal_Shdr *hdr2;
9ad5cbcf 1744 unsigned int num_sec = elf_numsections (abfd);
252b5132 1745
aa2ca951
JJ
1746 if (hdr->sh_entsize
1747 != (bfd_size_type) (hdr->sh_type == SHT_REL
a50b2160
JJ
1748 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1749 return FALSE;
1750
03ae5f59 1751 /* Check for a bogus link to avoid crashing. */
4fbb74a6 1752 if (hdr->sh_link >= num_sec)
03ae5f59
ILT
1753 {
1754 ((*_bfd_error_handler)
d003868e
AM
1755 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1756 abfd, hdr->sh_link, name, shindex));
6dc132d9
L
1757 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1758 shindex);
03ae5f59
ILT
1759 }
1760
252b5132
RH
1761 /* For some incomprehensible reason Oracle distributes
1762 libraries for Solaris in which some of the objects have
1763 bogus sh_link fields. It would be nice if we could just
1764 reject them, but, unfortunately, some people need to use
1765 them. We scan through the section headers; if we find only
1766 one suitable symbol table, we clobber the sh_link to point
1767 to it. I hope this doesn't break anything. */
1768 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1769 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1770 {
9ad5cbcf 1771 unsigned int scan;
252b5132
RH
1772 int found;
1773
1774 found = 0;
9ad5cbcf 1775 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
1776 {
1777 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1778 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1779 {
1780 if (found != 0)
1781 {
1782 found = 0;
1783 break;
1784 }
1785 found = scan;
1786 }
1787 }
1788 if (found != 0)
1789 hdr->sh_link = found;
1790 }
1791
1792 /* Get the symbol table. */
1b3a8575
AM
1793 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1794 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 1795 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
b34976b6 1796 return FALSE;
252b5132
RH
1797
1798 /* If this reloc section does not use the main symbol table we
1799 don't treat it as a reloc section. BFD can't adequately
1800 represent such a section, so at least for now, we don't
c044fabd 1801 try. We just present it as a normal section. We also
60bcf0fa 1802 can't use it as a reloc section if it points to the null
185ef66d
AM
1803 section, an invalid section, or another reloc section. */
1804 if (hdr->sh_link != elf_onesymtab (abfd)
1805 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
1806 || hdr->sh_info >= num_sec
1807 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1808 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
6dc132d9
L
1809 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1810 shindex);
252b5132
RH
1811
1812 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
b34976b6 1813 return FALSE;
252b5132
RH
1814 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1815 if (target_sect == NULL)
b34976b6 1816 return FALSE;
252b5132
RH
1817
1818 if ((target_sect->flags & SEC_RELOC) == 0
1819 || target_sect->reloc_count == 0)
1820 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1821 else
1822 {
dc810e39 1823 bfd_size_type amt;
252b5132 1824 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
dc810e39 1825 amt = sizeof (*hdr2);
217aa764 1826 hdr2 = bfd_alloc (abfd, amt);
14b1c01e
AM
1827 if (hdr2 == NULL)
1828 return FALSE;
252b5132
RH
1829 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1830 }
1831 *hdr2 = *hdr;
1832 elf_elfsections (abfd)[shindex] = hdr2;
d9bc7a44 1833 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
252b5132
RH
1834 target_sect->flags |= SEC_RELOC;
1835 target_sect->relocation = NULL;
1836 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
1837 /* In the section to which the relocations apply, mark whether
1838 its relocations are of the REL or RELA variety. */
72730e0c 1839 if (hdr->sh_size != 0)
68bfbfcc 1840 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
252b5132 1841 abfd->flags |= HAS_RELOC;
b34976b6 1842 return TRUE;
252b5132 1843 }
252b5132
RH
1844
1845 case SHT_GNU_verdef:
1846 elf_dynverdef (abfd) = shindex;
1847 elf_tdata (abfd)->dynverdef_hdr = *hdr;
6dc132d9 1848 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
252b5132
RH
1849
1850 case SHT_GNU_versym:
a50b2160
JJ
1851 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1852 return FALSE;
252b5132
RH
1853 elf_dynversym (abfd) = shindex;
1854 elf_tdata (abfd)->dynversym_hdr = *hdr;
6dc132d9 1855 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
252b5132
RH
1856
1857 case SHT_GNU_verneed:
1858 elf_dynverref (abfd) = shindex;
1859 elf_tdata (abfd)->dynverref_hdr = *hdr;
6dc132d9 1860 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
252b5132
RH
1861
1862 case SHT_SHLIB:
b34976b6 1863 return TRUE;
252b5132 1864
dbb410c3 1865 case SHT_GROUP:
b885599b
AM
1866 /* We need a BFD section for objcopy and relocatable linking,
1867 and it's handy to have the signature available as the section
1868 name. */
1783205a 1869 if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
a50b2160 1870 return FALSE;
b885599b
AM
1871 name = group_signature (abfd, hdr);
1872 if (name == NULL)
b34976b6 1873 return FALSE;
6dc132d9 1874 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 1875 return FALSE;
dbb410c3
AM
1876 if (hdr->contents != NULL)
1877 {
1878 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1783205a 1879 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
dbb410c3
AM
1880 asection *s;
1881
b885599b
AM
1882 if (idx->flags & GRP_COMDAT)
1883 hdr->bfd_section->flags
1884 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1885
45c5e9ed
L
1886 /* We try to keep the same section order as it comes in. */
1887 idx += n_elt;
dbb410c3 1888 while (--n_elt != 0)
1783205a
NC
1889 {
1890 --idx;
1891
1892 if (idx->shdr != NULL
1893 && (s = idx->shdr->bfd_section) != NULL
1894 && elf_next_in_group (s) != NULL)
1895 {
1896 elf_next_in_group (hdr->bfd_section) = s;
1897 break;
1898 }
1899 }
dbb410c3
AM
1900 }
1901 break;
1902
252b5132 1903 default:
104d59d1
JM
1904 /* Possibly an attributes section. */
1905 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1906 || hdr->sh_type == bed->obj_attrs_section_type)
1907 {
1908 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1909 return FALSE;
1910 _bfd_elf_parse_attributes (abfd, hdr);
1911 return TRUE;
1912 }
1913
252b5132 1914 /* Check for any processor-specific section types. */
3eb70a79
L
1915 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1916 return TRUE;
1917
1918 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1919 {
1920 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1921 /* FIXME: How to properly handle allocated section reserved
1922 for applications? */
1923 (*_bfd_error_handler)
1924 (_("%B: don't know how to handle allocated, application "
1925 "specific section `%s' [0x%8x]"),
1926 abfd, name, hdr->sh_type);
1927 else
1928 /* Allow sections reserved for applications. */
1929 return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1930 shindex);
1931 }
1932 else if (hdr->sh_type >= SHT_LOPROC
1933 && hdr->sh_type <= SHT_HIPROC)
1934 /* FIXME: We should handle this section. */
1935 (*_bfd_error_handler)
1936 (_("%B: don't know how to handle processor specific section "
1937 "`%s' [0x%8x]"),
1938 abfd, name, hdr->sh_type);
1939 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
1940 {
1941 /* Unrecognised OS-specific sections. */
1942 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1943 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 1944 required to correctly process the section and the file should
ff15b240
NC
1945 be rejected with an error message. */
1946 (*_bfd_error_handler)
1947 (_("%B: don't know how to handle OS specific section "
1948 "`%s' [0x%8x]"),
1949 abfd, name, hdr->sh_type);
1950 else
1951 /* Otherwise it should be processed. */
1952 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1953 }
3eb70a79
L
1954 else
1955 /* FIXME: We should handle this section. */
1956 (*_bfd_error_handler)
1957 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1958 abfd, name, hdr->sh_type);
1959
1960 return FALSE;
252b5132
RH
1961 }
1962
b34976b6 1963 return TRUE;
252b5132
RH
1964}
1965
ec338859
AM
1966/* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1967 Return SEC for sections that have no elf section, and NULL on error. */
1968
1969asection *
217aa764
AM
1970bfd_section_from_r_symndx (bfd *abfd,
1971 struct sym_sec_cache *cache,
1972 asection *sec,
1973 unsigned long r_symndx)
ec338859 1974{
ec338859 1975 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
a5d1b3b5 1976 asection *s;
ec338859 1977
a5d1b3b5
AM
1978 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1979 {
1980 Elf_Internal_Shdr *symtab_hdr;
1981 unsigned char esym[sizeof (Elf64_External_Sym)];
1982 Elf_External_Sym_Shndx eshndx;
1983 Elf_Internal_Sym isym;
ec338859 1984
a5d1b3b5
AM
1985 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1986 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1987 &isym, esym, &eshndx) == NULL)
1988 return NULL;
9ad5cbcf 1989
a5d1b3b5
AM
1990 if (cache->abfd != abfd)
1991 {
1992 memset (cache->indx, -1, sizeof (cache->indx));
1993 cache->abfd = abfd;
1994 }
1995 cache->indx[ent] = r_symndx;
1996 cache->shndx[ent] = isym.st_shndx;
ec338859 1997 }
a5d1b3b5
AM
1998
1999 s = bfd_section_from_elf_index (abfd, cache->shndx[ent]);
2000 if (s != NULL)
2001 return s;
2002
2003 return sec;
ec338859
AM
2004}
2005
252b5132
RH
2006/* Given an ELF section number, retrieve the corresponding BFD
2007 section. */
2008
2009asection *
217aa764 2010bfd_section_from_elf_index (bfd *abfd, unsigned int index)
252b5132 2011{
9ad5cbcf 2012 if (index >= elf_numsections (abfd))
252b5132
RH
2013 return NULL;
2014 return elf_elfsections (abfd)[index]->bfd_section;
2015}
2016
b35d266b 2017static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2018{
0112cd26
NC
2019 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2020 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2021};
2022
b35d266b 2023static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2024{
0112cd26
NC
2025 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2026 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2027};
2028
b35d266b 2029static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2030{
0112cd26
NC
2031 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2032 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2033 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2034 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2035 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2036 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2037 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2038 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2039 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2040 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2041 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2042};
2043
b35d266b 2044static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2045{
0112cd26
NC
2046 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2047 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2048 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2049};
2050
b35d266b 2051static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2052{
0112cd26
NC
2053 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2054 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2055 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2056 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2057 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2058 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2059 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2060 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2061 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2062};
2063
b35d266b 2064static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2065{
0112cd26
NC
2066 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2067 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2068};
2069
b35d266b 2070static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2071{
0112cd26
NC
2072 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2073 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2074 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2075 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2076};
2077
b35d266b 2078static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2079{
0112cd26
NC
2080 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2081 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2082};
2083
b35d266b 2084static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2085{
0112cd26
NC
2086 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2087 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2088 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2089};
2090
b35d266b 2091static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2092{
0112cd26
NC
2093 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2094 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2095 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2096};
2097
b35d266b 2098static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2099{
0112cd26
NC
2100 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2101 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2102 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2103 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2104 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2105};
2106
b35d266b 2107static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2108{
0112cd26
NC
2109 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2110 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2111 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2112 /* See struct bfd_elf_special_section declaration for the semantics of
2113 this special case where .prefix_length != strlen (.prefix). */
2114 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
0112cd26 2115 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2116};
2117
b35d266b 2118static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2119{
0112cd26
NC
2120 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2121 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2122 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2123 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2124};
2125
1b315056
CS
2126static const struct bfd_elf_special_section special_sections_z[] =
2127{
2128 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2129 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2130 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2131 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2132 { NULL, 0, 0, 0, 0 }
2133};
2134
b35d266b 2135static const struct bfd_elf_special_section *special_sections[] =
7f4d3958 2136{
7f4d3958 2137 special_sections_b, /* 'b' */
98ece1b3 2138 special_sections_c, /* 'c' */
7f4d3958
L
2139 special_sections_d, /* 'd' */
2140 NULL, /* 'e' */
2141 special_sections_f, /* 'f' */
2142 special_sections_g, /* 'g' */
2143 special_sections_h, /* 'h' */
2144 special_sections_i, /* 'i' */
2145 NULL, /* 'j' */
2146 NULL, /* 'k' */
2147 special_sections_l, /* 'l' */
2148 NULL, /* 'm' */
2149 special_sections_n, /* 'n' */
2150 NULL, /* 'o' */
2151 special_sections_p, /* 'p' */
2152 NULL, /* 'q' */
2153 special_sections_r, /* 'r' */
2154 special_sections_s, /* 's' */
2155 special_sections_t, /* 't' */
1b315056
CS
2156 NULL, /* 'u' */
2157 NULL, /* 'v' */
2158 NULL, /* 'w' */
2159 NULL, /* 'x' */
2160 NULL, /* 'y' */
2161 special_sections_z /* 'z' */
7f4d3958
L
2162};
2163
551b43fd
AM
2164const struct bfd_elf_special_section *
2165_bfd_elf_get_special_section (const char *name,
2166 const struct bfd_elf_special_section *spec,
2167 unsigned int rela)
2f89ff8d
L
2168{
2169 int i;
7f4d3958 2170 int len;
7f4d3958 2171
551b43fd 2172 len = strlen (name);
7f4d3958 2173
551b43fd 2174 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2175 {
2176 int suffix_len;
551b43fd 2177 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2178
2179 if (len < prefix_len)
2180 continue;
551b43fd 2181 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2182 continue;
2183
551b43fd 2184 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2185 if (suffix_len <= 0)
2186 {
2187 if (name[prefix_len] != 0)
2188 {
2189 if (suffix_len == 0)
2190 continue;
2191 if (name[prefix_len] != '.'
2192 && (suffix_len == -2
551b43fd 2193 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2194 continue;
2195 }
2196 }
2197 else
2198 {
2199 if (len < prefix_len + suffix_len)
2200 continue;
2201 if (memcmp (name + len - suffix_len,
551b43fd 2202 spec[i].prefix + prefix_len,
7dcb9820
AM
2203 suffix_len) != 0)
2204 continue;
2205 }
551b43fd 2206 return &spec[i];
7dcb9820 2207 }
2f89ff8d
L
2208
2209 return NULL;
2210}
2211
7dcb9820 2212const struct bfd_elf_special_section *
29ef7005 2213_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2214{
551b43fd
AM
2215 int i;
2216 const struct bfd_elf_special_section *spec;
29ef7005 2217 const struct elf_backend_data *bed;
2f89ff8d
L
2218
2219 /* See if this is one of the special sections. */
551b43fd
AM
2220 if (sec->name == NULL)
2221 return NULL;
2f89ff8d 2222
29ef7005
L
2223 bed = get_elf_backend_data (abfd);
2224 spec = bed->special_sections;
2225 if (spec)
2226 {
2227 spec = _bfd_elf_get_special_section (sec->name,
2228 bed->special_sections,
2229 sec->use_rela_p);
2230 if (spec != NULL)
2231 return spec;
2232 }
2233
551b43fd
AM
2234 if (sec->name[0] != '.')
2235 return NULL;
2f89ff8d 2236
551b43fd 2237 i = sec->name[1] - 'b';
1b315056 2238 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2239 return NULL;
2240
2241 spec = special_sections[i];
2f89ff8d 2242
551b43fd
AM
2243 if (spec == NULL)
2244 return NULL;
2245
2246 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2247}
2248
b34976b6 2249bfd_boolean
217aa764 2250_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2251{
2252 struct bfd_elf_section_data *sdata;
551b43fd 2253 const struct elf_backend_data *bed;
7dcb9820 2254 const struct bfd_elf_special_section *ssect;
252b5132 2255
f0abc2a1
AM
2256 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2257 if (sdata == NULL)
2258 {
217aa764 2259 sdata = bfd_zalloc (abfd, sizeof (*sdata));
f0abc2a1
AM
2260 if (sdata == NULL)
2261 return FALSE;
217aa764 2262 sec->used_by_bfd = sdata;
f0abc2a1 2263 }
bf572ba0 2264
551b43fd
AM
2265 /* Indicate whether or not this section should use RELA relocations. */
2266 bed = get_elf_backend_data (abfd);
2267 sec->use_rela_p = bed->default_use_rela_p;
2268
e843e0f8
L
2269 /* When we read a file, we don't need to set ELF section type and
2270 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2271 anyway. We will set ELF section type and flags for all linker
2272 created sections. If user specifies BFD section flags, we will
2273 set ELF section type and flags based on BFD section flags in
2274 elf_fake_sections. */
2275 if ((!sec->flags && abfd->direction != read_direction)
3496cb2a 2276 || (sec->flags & SEC_LINKER_CREATED) != 0)
2f89ff8d 2277 {
551b43fd 2278 ssect = (*bed->get_sec_type_attr) (abfd, sec);
a31501e9
L
2279 if (ssect != NULL)
2280 {
2281 elf_section_type (sec) = ssect->type;
2282 elf_section_flags (sec) = ssect->attr;
2283 }
2f89ff8d
L
2284 }
2285
f592407e 2286 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2287}
2288
2289/* Create a new bfd section from an ELF program header.
2290
2291 Since program segments have no names, we generate a synthetic name
2292 of the form segment<NUM>, where NUM is generally the index in the
2293 program header table. For segments that are split (see below) we
2294 generate the names segment<NUM>a and segment<NUM>b.
2295
2296 Note that some program segments may have a file size that is different than
2297 (less than) the memory size. All this means is that at execution the
2298 system must allocate the amount of memory specified by the memory size,
2299 but only initialize it with the first "file size" bytes read from the
2300 file. This would occur for example, with program segments consisting
2301 of combined data+bss.
2302
2303 To handle the above situation, this routine generates TWO bfd sections
2304 for the single program segment. The first has the length specified by
2305 the file size of the segment, and the second has the length specified
2306 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2307 into its initialized and uninitialized parts.
252b5132
RH
2308
2309 */
2310
b34976b6 2311bfd_boolean
217aa764
AM
2312_bfd_elf_make_section_from_phdr (bfd *abfd,
2313 Elf_Internal_Phdr *hdr,
2314 int index,
2315 const char *typename)
252b5132
RH
2316{
2317 asection *newsect;
2318 char *name;
2319 char namebuf[64];
d4c88bbb 2320 size_t len;
252b5132
RH
2321 int split;
2322
2323 split = ((hdr->p_memsz > 0)
2324 && (hdr->p_filesz > 0)
2325 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2326
2327 if (hdr->p_filesz > 0)
252b5132 2328 {
d5191d0c
AM
2329 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2330 len = strlen (namebuf) + 1;
2331 name = bfd_alloc (abfd, len);
2332 if (!name)
2333 return FALSE;
2334 memcpy (name, namebuf, len);
2335 newsect = bfd_make_section (abfd, name);
2336 if (newsect == NULL)
2337 return FALSE;
2338 newsect->vma = hdr->p_vaddr;
2339 newsect->lma = hdr->p_paddr;
2340 newsect->size = hdr->p_filesz;
2341 newsect->filepos = hdr->p_offset;
2342 newsect->flags |= SEC_HAS_CONTENTS;
2343 newsect->alignment_power = bfd_log2 (hdr->p_align);
2344 if (hdr->p_type == PT_LOAD)
252b5132 2345 {
d5191d0c
AM
2346 newsect->flags |= SEC_ALLOC;
2347 newsect->flags |= SEC_LOAD;
2348 if (hdr->p_flags & PF_X)
2349 {
2350 /* FIXME: all we known is that it has execute PERMISSION,
2351 may be data. */
2352 newsect->flags |= SEC_CODE;
2353 }
2354 }
2355 if (!(hdr->p_flags & PF_W))
2356 {
2357 newsect->flags |= SEC_READONLY;
252b5132 2358 }
252b5132
RH
2359 }
2360
d5191d0c 2361 if (hdr->p_memsz > hdr->p_filesz)
252b5132 2362 {
d5191d0c
AM
2363 bfd_vma align;
2364
2365 sprintf (namebuf, "%s%d%s", typename, index, split ? "b" : "");
d4c88bbb 2366 len = strlen (namebuf) + 1;
217aa764 2367 name = bfd_alloc (abfd, len);
252b5132 2368 if (!name)
b34976b6 2369 return FALSE;
d4c88bbb 2370 memcpy (name, namebuf, len);
252b5132
RH
2371 newsect = bfd_make_section (abfd, name);
2372 if (newsect == NULL)
b34976b6 2373 return FALSE;
252b5132
RH
2374 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2375 newsect->lma = hdr->p_paddr + hdr->p_filesz;
eea6121a 2376 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
2377 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2378 align = newsect->vma & -newsect->vma;
2379 if (align == 0 || align > hdr->p_align)
2380 align = hdr->p_align;
2381 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
2382 if (hdr->p_type == PT_LOAD)
2383 {
d5191d0c
AM
2384 /* Hack for gdb. Segments that have not been modified do
2385 not have their contents written to a core file, on the
2386 assumption that a debugger can find the contents in the
2387 executable. We flag this case by setting the fake
2388 section size to zero. Note that "real" bss sections will
2389 always have their contents dumped to the core file. */
2390 if (bfd_get_format (abfd) == bfd_core)
2391 newsect->size = 0;
252b5132
RH
2392 newsect->flags |= SEC_ALLOC;
2393 if (hdr->p_flags & PF_X)
2394 newsect->flags |= SEC_CODE;
2395 }
2396 if (!(hdr->p_flags & PF_W))
2397 newsect->flags |= SEC_READONLY;
2398 }
2399
b34976b6 2400 return TRUE;
252b5132
RH
2401}
2402
b34976b6 2403bfd_boolean
217aa764 2404bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
20cfcaae 2405{
9c5bfbb7 2406 const struct elf_backend_data *bed;
20cfcaae
NC
2407
2408 switch (hdr->p_type)
2409 {
2410 case PT_NULL:
2411 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2412
2413 case PT_LOAD:
2414 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2415
2416 case PT_DYNAMIC:
2417 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2418
2419 case PT_INTERP:
2420 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2421
2422 case PT_NOTE:
2423 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
b34976b6 2424 return FALSE;
718175fa 2425 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
b34976b6
AM
2426 return FALSE;
2427 return TRUE;
20cfcaae
NC
2428
2429 case PT_SHLIB:
2430 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2431
2432 case PT_PHDR:
2433 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2434
811072d8
RM
2435 case PT_GNU_EH_FRAME:
2436 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2437 "eh_frame_hdr");
2438
2b05f1b7
L
2439 case PT_GNU_STACK:
2440 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
9ee5e499 2441
8c37241b
JJ
2442 case PT_GNU_RELRO:
2443 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2444
20cfcaae 2445 default:
8c1acd09 2446 /* Check for any processor-specific program segment types. */
20cfcaae 2447 bed = get_elf_backend_data (abfd);
d27f5fa1 2448 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
20cfcaae
NC
2449 }
2450}
2451
23bc299b 2452/* Initialize REL_HDR, the section-header for new section, containing
b34976b6 2453 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
23bc299b
MM
2454 relocations; otherwise, we use REL relocations. */
2455
b34976b6 2456bfd_boolean
217aa764
AM
2457_bfd_elf_init_reloc_shdr (bfd *abfd,
2458 Elf_Internal_Shdr *rel_hdr,
2459 asection *asect,
2460 bfd_boolean use_rela_p)
23bc299b
MM
2461{
2462 char *name;
9c5bfbb7 2463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
dc810e39 2464 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
23bc299b 2465
dc810e39 2466 name = bfd_alloc (abfd, amt);
23bc299b 2467 if (name == NULL)
b34976b6 2468 return FALSE;
23bc299b
MM
2469 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2470 rel_hdr->sh_name =
2b0f7ef9 2471 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
b34976b6 2472 FALSE);
23bc299b 2473 if (rel_hdr->sh_name == (unsigned int) -1)
b34976b6 2474 return FALSE;
23bc299b
MM
2475 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2476 rel_hdr->sh_entsize = (use_rela_p
2477 ? bed->s->sizeof_rela
2478 : bed->s->sizeof_rel);
72de5009 2479 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
23bc299b
MM
2480 rel_hdr->sh_flags = 0;
2481 rel_hdr->sh_addr = 0;
2482 rel_hdr->sh_size = 0;
2483 rel_hdr->sh_offset = 0;
2484
b34976b6 2485 return TRUE;
23bc299b
MM
2486}
2487
252b5132
RH
2488/* Set up an ELF internal section header for a section. */
2489
252b5132 2490static void
217aa764 2491elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
252b5132 2492{
9c5bfbb7 2493 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 2494 bfd_boolean *failedptr = failedptrarg;
252b5132 2495 Elf_Internal_Shdr *this_hdr;
0414f35b 2496 unsigned int sh_type;
252b5132
RH
2497
2498 if (*failedptr)
2499 {
2500 /* We already failed; just get out of the bfd_map_over_sections
08a40648 2501 loop. */
252b5132
RH
2502 return;
2503 }
2504
2505 this_hdr = &elf_section_data (asect)->this_hdr;
2506
e57b5356
AM
2507 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2508 asect->name, FALSE);
2509 if (this_hdr->sh_name == (unsigned int) -1)
252b5132 2510 {
b34976b6 2511 *failedptr = TRUE;
252b5132
RH
2512 return;
2513 }
2514
a4d8e49b 2515 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
2516
2517 if ((asect->flags & SEC_ALLOC) != 0
2518 || asect->user_set_vma)
2519 this_hdr->sh_addr = asect->vma;
2520 else
2521 this_hdr->sh_addr = 0;
2522
2523 this_hdr->sh_offset = 0;
eea6121a 2524 this_hdr->sh_size = asect->size;
252b5132 2525 this_hdr->sh_link = 0;
72de5009 2526 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
252b5132
RH
2527 /* The sh_entsize and sh_info fields may have been set already by
2528 copy_private_section_data. */
2529
2530 this_hdr->bfd_section = asect;
2531 this_hdr->contents = NULL;
2532
3cddba1e
L
2533 /* If the section type is unspecified, we set it based on
2534 asect->flags. */
98ece1b3
AM
2535 if ((asect->flags & SEC_GROUP) != 0)
2536 sh_type = SHT_GROUP;
2537 else if ((asect->flags & SEC_ALLOC) != 0
2538 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2539 || (asect->flags & SEC_NEVER_LOAD) != 0))
2540 sh_type = SHT_NOBITS;
2541 else
2542 sh_type = SHT_PROGBITS;
2543
3cddba1e 2544 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
2545 this_hdr->sh_type = sh_type;
2546 else if (this_hdr->sh_type == SHT_NOBITS
2547 && sh_type == SHT_PROGBITS
2548 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 2549 {
98ece1b3
AM
2550 /* Warn if we are changing a NOBITS section to PROGBITS, but
2551 allow the link to proceed. This can happen when users link
2552 non-bss input sections to bss output sections, or emit data
2553 to a bss output section via a linker script. */
2554 (*_bfd_error_handler)
58f0869b 2555 (_("warning: section `%A' type changed to PROGBITS"), asect);
98ece1b3 2556 this_hdr->sh_type = sh_type;
3cddba1e
L
2557 }
2558
2f89ff8d 2559 switch (this_hdr->sh_type)
252b5132 2560 {
2f89ff8d 2561 default:
2f89ff8d
L
2562 break;
2563
2564 case SHT_STRTAB:
2565 case SHT_INIT_ARRAY:
2566 case SHT_FINI_ARRAY:
2567 case SHT_PREINIT_ARRAY:
2568 case SHT_NOTE:
2569 case SHT_NOBITS:
2570 case SHT_PROGBITS:
2571 break;
2572
2573 case SHT_HASH:
c7ac6ff8 2574 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 2575 break;
5de3bf90 2576
2f89ff8d 2577 case SHT_DYNSYM:
252b5132 2578 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
2579 break;
2580
2581 case SHT_DYNAMIC:
252b5132 2582 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
2583 break;
2584
2585 case SHT_RELA:
2586 if (get_elf_backend_data (abfd)->may_use_rela_p)
2587 this_hdr->sh_entsize = bed->s->sizeof_rela;
2588 break;
2589
2590 case SHT_REL:
2591 if (get_elf_backend_data (abfd)->may_use_rel_p)
2592 this_hdr->sh_entsize = bed->s->sizeof_rel;
2593 break;
2594
2595 case SHT_GNU_versym:
252b5132 2596 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
2597 break;
2598
2599 case SHT_GNU_verdef:
252b5132
RH
2600 this_hdr->sh_entsize = 0;
2601 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
2602 cverdefs. The linker will set cverdefs, but sh_info will be
2603 zero. */
252b5132
RH
2604 if (this_hdr->sh_info == 0)
2605 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2606 else
2607 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2608 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
2609 break;
2610
2611 case SHT_GNU_verneed:
252b5132
RH
2612 this_hdr->sh_entsize = 0;
2613 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
2614 cverrefs. The linker will set cverrefs, but sh_info will be
2615 zero. */
252b5132
RH
2616 if (this_hdr->sh_info == 0)
2617 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2618 else
2619 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2620 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
2621 break;
2622
2623 case SHT_GROUP:
1783205a 2624 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 2625 break;
fdc90cb4
JJ
2626
2627 case SHT_GNU_HASH:
2628 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2629 break;
dbb410c3 2630 }
252b5132
RH
2631
2632 if ((asect->flags & SEC_ALLOC) != 0)
2633 this_hdr->sh_flags |= SHF_ALLOC;
2634 if ((asect->flags & SEC_READONLY) == 0)
2635 this_hdr->sh_flags |= SHF_WRITE;
2636 if ((asect->flags & SEC_CODE) != 0)
2637 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
2638 if ((asect->flags & SEC_MERGE) != 0)
2639 {
2640 this_hdr->sh_flags |= SHF_MERGE;
2641 this_hdr->sh_entsize = asect->entsize;
2642 if ((asect->flags & SEC_STRINGS) != 0)
2643 this_hdr->sh_flags |= SHF_STRINGS;
2644 }
1126897b 2645 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 2646 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 2647 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
2648 {
2649 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
2650 if (asect->size == 0
2651 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 2652 {
3a800eb9 2653 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 2654
704afa60 2655 this_hdr->sh_size = 0;
3a800eb9
AM
2656 if (o != NULL)
2657 {
704afa60 2658 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
2659 if (this_hdr->sh_size != 0)
2660 this_hdr->sh_type = SHT_NOBITS;
2661 }
704afa60
JJ
2662 }
2663 }
252b5132
RH
2664
2665 /* Check for processor-specific section types. */
0414f35b 2666 sh_type = this_hdr->sh_type;
e1fddb6b
AO
2667 if (bed->elf_backend_fake_sections
2668 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
b34976b6 2669 *failedptr = TRUE;
252b5132 2670
42bb2e33 2671 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
2672 {
2673 /* Don't change the header type from NOBITS if we are being
42bb2e33 2674 called for objcopy --only-keep-debug. */
0414f35b
AM
2675 this_hdr->sh_type = sh_type;
2676 }
2677
252b5132 2678 /* If the section has relocs, set up a section header for the
23bc299b
MM
2679 SHT_REL[A] section. If two relocation sections are required for
2680 this section, it is up to the processor-specific back-end to
c044fabd 2681 create the other. */
23bc299b 2682 if ((asect->flags & SEC_RELOC) != 0
c044fabd 2683 && !_bfd_elf_init_reloc_shdr (abfd,
23bc299b 2684 &elf_section_data (asect)->rel_hdr,
c044fabd 2685 asect,
68bfbfcc 2686 asect->use_rela_p))
b34976b6 2687 *failedptr = TRUE;
252b5132
RH
2688}
2689
dbb410c3
AM
2690/* Fill in the contents of a SHT_GROUP section. */
2691
1126897b 2692void
217aa764 2693bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 2694{
217aa764 2695 bfd_boolean *failedptr = failedptrarg;
dbb410c3 2696 unsigned long symindx;
9dce4196 2697 asection *elt, *first;
dbb410c3 2698 unsigned char *loc;
b34976b6 2699 bfd_boolean gas;
dbb410c3 2700
7e4111ad
L
2701 /* Ignore linker created group section. See elfNN_ia64_object_p in
2702 elfxx-ia64.c. */
2703 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
dbb410c3
AM
2704 || *failedptr)
2705 return;
2706
1126897b
AM
2707 symindx = 0;
2708 if (elf_group_id (sec) != NULL)
2709 symindx = elf_group_id (sec)->udata.i;
2710
2711 if (symindx == 0)
2712 {
2713 /* If called from the assembler, swap_out_syms will have set up
2714 elf_section_syms; If called for "ld -r", use target_index. */
2715 if (elf_section_syms (abfd) != NULL)
2716 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2717 else
2718 symindx = sec->target_index;
2719 }
dbb410c3
AM
2720 elf_section_data (sec)->this_hdr.sh_info = symindx;
2721
1126897b 2722 /* The contents won't be allocated for "ld -r" or objcopy. */
b34976b6 2723 gas = TRUE;
dbb410c3
AM
2724 if (sec->contents == NULL)
2725 {
b34976b6 2726 gas = FALSE;
eea6121a 2727 sec->contents = bfd_alloc (abfd, sec->size);
9dce4196
AM
2728
2729 /* Arrange for the section to be written out. */
2730 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
2731 if (sec->contents == NULL)
2732 {
b34976b6 2733 *failedptr = TRUE;
dbb410c3
AM
2734 return;
2735 }
2736 }
2737
eea6121a 2738 loc = sec->contents + sec->size;
dbb410c3 2739
9dce4196
AM
2740 /* Get the pointer to the first section in the group that gas
2741 squirreled away here. objcopy arranges for this to be set to the
2742 start of the input section group. */
2743 first = elt = elf_next_in_group (sec);
dbb410c3
AM
2744
2745 /* First element is a flag word. Rest of section is elf section
2746 indices for all the sections of the group. Write them backwards
2747 just to keep the group in the same order as given in .section
2748 directives, not that it matters. */
2749 while (elt != NULL)
2750 {
9dce4196
AM
2751 asection *s;
2752 unsigned int idx;
2753
dbb410c3 2754 loc -= 4;
9dce4196
AM
2755 s = elt;
2756 if (!gas)
2757 s = s->output_section;
2758 idx = 0;
2759 if (s != NULL)
2760 idx = elf_section_data (s)->this_idx;
2761 H_PUT_32 (abfd, idx, loc);
945906ff 2762 elt = elf_next_in_group (elt);
9dce4196
AM
2763 if (elt == first)
2764 break;
dbb410c3
AM
2765 }
2766
3d7f7666 2767 if ((loc -= 4) != sec->contents)
9dce4196 2768 abort ();
dbb410c3 2769
9dce4196 2770 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
2771}
2772
252b5132
RH
2773/* Assign all ELF section numbers. The dummy first section is handled here
2774 too. The link/info pointers for the standard section types are filled
2775 in here too, while we're at it. */
2776
b34976b6 2777static bfd_boolean
da9f89d4 2778assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
2779{
2780 struct elf_obj_tdata *t = elf_tdata (abfd);
2781 asection *sec;
2b0f7ef9 2782 unsigned int section_number, secn;
252b5132 2783 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 2784 struct bfd_elf_section_data *d;
252b5132
RH
2785
2786 section_number = 1;
2787
2b0f7ef9
JJ
2788 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2789
da9f89d4
L
2790 /* SHT_GROUP sections are in relocatable files only. */
2791 if (link_info == NULL || link_info->relocatable)
252b5132 2792 {
da9f89d4 2793 /* Put SHT_GROUP sections first. */
04dd1667 2794 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 2795 {
5daa8fe7 2796 d = elf_section_data (sec);
da9f89d4
L
2797
2798 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 2799 {
5daa8fe7 2800 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
2801 {
2802 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 2803 bfd_section_list_remove (abfd, sec);
da9f89d4 2804 abfd->section_count--;
da9f89d4 2805 }
08a40648 2806 else
4fbb74a6 2807 d->this_idx = section_number++;
da9f89d4 2808 }
47cc2cf5
PB
2809 }
2810 }
2811
2812 for (sec = abfd->sections; sec; sec = sec->next)
2813 {
2814 d = elf_section_data (sec);
2815
2816 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 2817 d->this_idx = section_number++;
2b0f7ef9 2818 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
252b5132
RH
2819 if ((sec->flags & SEC_RELOC) == 0)
2820 d->rel_idx = 0;
2821 else
2b0f7ef9
JJ
2822 {
2823 d->rel_idx = section_number++;
2824 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2825 }
23bc299b
MM
2826
2827 if (d->rel_hdr2)
2b0f7ef9
JJ
2828 {
2829 d->rel_idx2 = section_number++;
2830 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2831 }
23bc299b
MM
2832 else
2833 d->rel_idx2 = 0;
252b5132
RH
2834 }
2835
2836 t->shstrtab_section = section_number++;
2b0f7ef9 2837 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
252b5132 2838 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
252b5132
RH
2839
2840 if (bfd_get_symcount (abfd) > 0)
2841 {
2842 t->symtab_section = section_number++;
2b0f7ef9 2843 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 2844 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 2845 {
9ad5cbcf
AM
2846 t->symtab_shndx_section = section_number++;
2847 t->symtab_shndx_hdr.sh_name
2848 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
b34976b6 2849 ".symtab_shndx", FALSE);
9ad5cbcf 2850 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
b34976b6 2851 return FALSE;
9ad5cbcf 2852 }
252b5132 2853 t->strtab_section = section_number++;
2b0f7ef9 2854 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
2855 }
2856
2b0f7ef9
JJ
2857 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2858 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
9ad5cbcf
AM
2859
2860 elf_numsections (abfd) = section_number;
252b5132
RH
2861 elf_elfheader (abfd)->e_shnum = section_number;
2862
2863 /* Set up the list of section header pointers, in agreement with the
2864 indices. */
d0fb9a8d 2865 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
252b5132 2866 if (i_shdrp == NULL)
b34976b6 2867 return FALSE;
252b5132 2868
d0fb9a8d 2869 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
252b5132
RH
2870 if (i_shdrp[0] == NULL)
2871 {
2872 bfd_release (abfd, i_shdrp);
b34976b6 2873 return FALSE;
252b5132 2874 }
252b5132
RH
2875
2876 elf_elfsections (abfd) = i_shdrp;
2877
2878 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2879 if (bfd_get_symcount (abfd) > 0)
2880 {
2881 i_shdrp[t->symtab_section] = &t->symtab_hdr;
4fbb74a6 2882 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf
AM
2883 {
2884 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2885 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2886 }
252b5132
RH
2887 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2888 t->symtab_hdr.sh_link = t->strtab_section;
2889 }
38ce5b11 2890
252b5132
RH
2891 for (sec = abfd->sections; sec; sec = sec->next)
2892 {
2893 struct bfd_elf_section_data *d = elf_section_data (sec);
2894 asection *s;
2895 const char *name;
2896
2897 i_shdrp[d->this_idx] = &d->this_hdr;
2898 if (d->rel_idx != 0)
2899 i_shdrp[d->rel_idx] = &d->rel_hdr;
23bc299b
MM
2900 if (d->rel_idx2 != 0)
2901 i_shdrp[d->rel_idx2] = d->rel_hdr2;
252b5132
RH
2902
2903 /* Fill in the sh_link and sh_info fields while we're at it. */
2904
2905 /* sh_link of a reloc section is the section index of the symbol
2906 table. sh_info is the section index of the section to which
2907 the relocation entries apply. */
2908 if (d->rel_idx != 0)
2909 {
2910 d->rel_hdr.sh_link = t->symtab_section;
2911 d->rel_hdr.sh_info = d->this_idx;
2912 }
23bc299b
MM
2913 if (d->rel_idx2 != 0)
2914 {
2915 d->rel_hdr2->sh_link = t->symtab_section;
2916 d->rel_hdr2->sh_info = d->this_idx;
2917 }
252b5132 2918
38ce5b11
L
2919 /* We need to set up sh_link for SHF_LINK_ORDER. */
2920 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2921 {
2922 s = elf_linked_to_section (sec);
2923 if (s)
38ce5b11 2924 {
f2876037 2925 /* elf_linked_to_section points to the input section. */
ccd2ec6a 2926 if (link_info != NULL)
38ce5b11 2927 {
f2876037 2928 /* Check discarded linkonce section. */
ccd2ec6a 2929 if (elf_discarded_section (s))
38ce5b11 2930 {
ccd2ec6a
L
2931 asection *kept;
2932 (*_bfd_error_handler)
2933 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2934 abfd, d->this_hdr.bfd_section,
2935 s, s->owner);
2936 /* Point to the kept section if it has the same
2937 size as the discarded one. */
c0f00686 2938 kept = _bfd_elf_check_kept_section (s, link_info);
ccd2ec6a 2939 if (kept == NULL)
185d09ad 2940 {
ccd2ec6a
L
2941 bfd_set_error (bfd_error_bad_value);
2942 return FALSE;
185d09ad 2943 }
ccd2ec6a 2944 s = kept;
38ce5b11 2945 }
e424ecc8 2946
ccd2ec6a
L
2947 s = s->output_section;
2948 BFD_ASSERT (s != NULL);
38ce5b11 2949 }
f2876037
L
2950 else
2951 {
2952 /* Handle objcopy. */
2953 if (s->output_section == NULL)
2954 {
2955 (*_bfd_error_handler)
2956 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2957 abfd, d->this_hdr.bfd_section, s, s->owner);
2958 bfd_set_error (bfd_error_bad_value);
2959 return FALSE;
2960 }
2961 s = s->output_section;
2962 }
ccd2ec6a
L
2963 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2964 }
2965 else
2966 {
2967 /* PR 290:
2968 The Intel C compiler generates SHT_IA_64_UNWIND with
2969 SHF_LINK_ORDER. But it doesn't set the sh_link or
2970 sh_info fields. Hence we could get the situation
08a40648 2971 where s is NULL. */
ccd2ec6a
L
2972 const struct elf_backend_data *bed
2973 = get_elf_backend_data (abfd);
2974 if (bed->link_order_error_handler)
2975 bed->link_order_error_handler
2976 (_("%B: warning: sh_link not set for section `%A'"),
2977 abfd, sec);
38ce5b11
L
2978 }
2979 }
2980
252b5132
RH
2981 switch (d->this_hdr.sh_type)
2982 {
2983 case SHT_REL:
2984 case SHT_RELA:
2985 /* A reloc section which we are treating as a normal BFD
2986 section. sh_link is the section index of the symbol
2987 table. sh_info is the section index of the section to
2988 which the relocation entries apply. We assume that an
2989 allocated reloc section uses the dynamic symbol table.
2990 FIXME: How can we be sure? */
2991 s = bfd_get_section_by_name (abfd, ".dynsym");
2992 if (s != NULL)
2993 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2994
2995 /* We look up the section the relocs apply to by name. */
2996 name = sec->name;
2997 if (d->this_hdr.sh_type == SHT_REL)
2998 name += 4;
2999 else
3000 name += 5;
3001 s = bfd_get_section_by_name (abfd, name);
3002 if (s != NULL)
3003 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3004 break;
3005
3006 case SHT_STRTAB:
3007 /* We assume that a section named .stab*str is a stabs
3008 string section. We look for a section with the same name
3009 but without the trailing ``str'', and set its sh_link
3010 field to point to this section. */
0112cd26 3011 if (CONST_STRNEQ (sec->name, ".stab")
252b5132
RH
3012 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3013 {
3014 size_t len;
3015 char *alc;
3016
3017 len = strlen (sec->name);
217aa764 3018 alc = bfd_malloc (len - 2);
252b5132 3019 if (alc == NULL)
b34976b6 3020 return FALSE;
d4c88bbb 3021 memcpy (alc, sec->name, len - 3);
252b5132
RH
3022 alc[len - 3] = '\0';
3023 s = bfd_get_section_by_name (abfd, alc);
3024 free (alc);
3025 if (s != NULL)
3026 {
3027 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3028
3029 /* This is a .stab section. */
0594c12d
AM
3030 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3031 elf_section_data (s)->this_hdr.sh_entsize
3032 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
252b5132
RH
3033 }
3034 }
3035 break;
3036
3037 case SHT_DYNAMIC:
3038 case SHT_DYNSYM:
3039 case SHT_GNU_verneed:
3040 case SHT_GNU_verdef:
3041 /* sh_link is the section header index of the string table
3042 used for the dynamic entries, or the symbol table, or the
3043 version strings. */
3044 s = bfd_get_section_by_name (abfd, ".dynstr");
3045 if (s != NULL)
3046 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3047 break;
3048
7f1204bb
JJ
3049 case SHT_GNU_LIBLIST:
3050 /* sh_link is the section header index of the prelink library
08a40648
AM
3051 list used for the dynamic entries, or the symbol table, or
3052 the version strings. */
7f1204bb
JJ
3053 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3054 ? ".dynstr" : ".gnu.libstr");
3055 if (s != NULL)
3056 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3057 break;
3058
252b5132 3059 case SHT_HASH:
fdc90cb4 3060 case SHT_GNU_HASH:
252b5132
RH
3061 case SHT_GNU_versym:
3062 /* sh_link is the section header index of the symbol table
3063 this hash table or version table is for. */
3064 s = bfd_get_section_by_name (abfd, ".dynsym");
3065 if (s != NULL)
3066 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3067 break;
dbb410c3
AM
3068
3069 case SHT_GROUP:
3070 d->this_hdr.sh_link = t->symtab_section;
252b5132
RH
3071 }
3072 }
3073
2b0f7ef9 3074 for (secn = 1; secn < section_number; ++secn)
9ad5cbcf
AM
3075 if (i_shdrp[secn] == NULL)
3076 i_shdrp[secn] = i_shdrp[0];
3077 else
3078 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3079 i_shdrp[secn]->sh_name);
b34976b6 3080 return TRUE;
252b5132
RH
3081}
3082
3083/* Map symbol from it's internal number to the external number, moving
3084 all local symbols to be at the head of the list. */
3085
5372391b 3086static bfd_boolean
217aa764 3087sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
3088{
3089 /* If the backend has a special mapping, use it. */
9c5bfbb7 3090 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
3091 if (bed->elf_backend_sym_is_global)
3092 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132
RH
3093
3094 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3095 || bfd_is_und_section (bfd_get_section (sym))
3096 || bfd_is_com_section (bfd_get_section (sym)));
3097}
3098
5372391b 3099/* Don't output section symbols for sections that are not going to be
0f0a5e58 3100 output. */
5372391b
AM
3101
3102static bfd_boolean
3103ignore_section_sym (bfd *abfd, asymbol *sym)
3104{
3105 return ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58
AM
3106 && !(sym->section->owner == abfd
3107 || (sym->section->output_section->owner == abfd
3108 && sym->section->output_offset == 0)));
5372391b
AM
3109}
3110
b34976b6 3111static bfd_boolean
217aa764 3112elf_map_symbols (bfd *abfd)
252b5132 3113{
dc810e39 3114 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
3115 asymbol **syms = bfd_get_outsymbols (abfd);
3116 asymbol **sect_syms;
dc810e39
AM
3117 unsigned int num_locals = 0;
3118 unsigned int num_globals = 0;
3119 unsigned int num_locals2 = 0;
3120 unsigned int num_globals2 = 0;
252b5132 3121 int max_index = 0;
dc810e39 3122 unsigned int idx;
252b5132
RH
3123 asection *asect;
3124 asymbol **new_syms;
252b5132
RH
3125
3126#ifdef DEBUG
3127 fprintf (stderr, "elf_map_symbols\n");
3128 fflush (stderr);
3129#endif
3130
252b5132
RH
3131 for (asect = abfd->sections; asect; asect = asect->next)
3132 {
3133 if (max_index < asect->index)
3134 max_index = asect->index;
3135 }
3136
3137 max_index++;
d0fb9a8d 3138 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
252b5132 3139 if (sect_syms == NULL)
b34976b6 3140 return FALSE;
252b5132 3141 elf_section_syms (abfd) = sect_syms;
4e89ac30 3142 elf_num_section_syms (abfd) = max_index;
252b5132 3143
079e9a2f
AM
3144 /* Init sect_syms entries for any section symbols we have already
3145 decided to output. */
252b5132
RH
3146 for (idx = 0; idx < symcount; idx++)
3147 {
dc810e39 3148 asymbol *sym = syms[idx];
c044fabd 3149
252b5132 3150 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 3151 && sym->value == 0
5372391b 3152 && !ignore_section_sym (abfd, sym))
252b5132 3153 {
5372391b 3154 asection *sec = sym->section;
252b5132 3155
5372391b
AM
3156 if (sec->owner != abfd)
3157 sec = sec->output_section;
252b5132 3158
5372391b 3159 sect_syms[sec->index] = syms[idx];
252b5132
RH
3160 }
3161 }
3162
252b5132
RH
3163 /* Classify all of the symbols. */
3164 for (idx = 0; idx < symcount; idx++)
3165 {
5372391b
AM
3166 if (ignore_section_sym (abfd, syms[idx]))
3167 continue;
252b5132
RH
3168 if (!sym_is_global (abfd, syms[idx]))
3169 num_locals++;
3170 else
3171 num_globals++;
3172 }
079e9a2f 3173
5372391b 3174 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
3175 sections will already have a section symbol in outsymbols, but
3176 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3177 at least in that case. */
252b5132
RH
3178 for (asect = abfd->sections; asect; asect = asect->next)
3179 {
079e9a2f 3180 if (sect_syms[asect->index] == NULL)
252b5132 3181 {
079e9a2f 3182 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
3183 num_locals++;
3184 else
3185 num_globals++;
252b5132
RH
3186 }
3187 }
3188
3189 /* Now sort the symbols so the local symbols are first. */
d0fb9a8d 3190 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
dc810e39 3191
252b5132 3192 if (new_syms == NULL)
b34976b6 3193 return FALSE;
252b5132
RH
3194
3195 for (idx = 0; idx < symcount; idx++)
3196 {
3197 asymbol *sym = syms[idx];
dc810e39 3198 unsigned int i;
252b5132 3199
5372391b
AM
3200 if (ignore_section_sym (abfd, sym))
3201 continue;
252b5132
RH
3202 if (!sym_is_global (abfd, sym))
3203 i = num_locals2++;
3204 else
3205 i = num_locals + num_globals2++;
3206 new_syms[i] = sym;
3207 sym->udata.i = i + 1;
3208 }
3209 for (asect = abfd->sections; asect; asect = asect->next)
3210 {
079e9a2f 3211 if (sect_syms[asect->index] == NULL)
252b5132 3212 {
079e9a2f 3213 asymbol *sym = asect->symbol;
dc810e39 3214 unsigned int i;
252b5132 3215
079e9a2f 3216 sect_syms[asect->index] = sym;
252b5132
RH
3217 if (!sym_is_global (abfd, sym))
3218 i = num_locals2++;
3219 else
3220 i = num_locals + num_globals2++;
3221 new_syms[i] = sym;
3222 sym->udata.i = i + 1;
3223 }
3224 }
3225
3226 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3227
3228 elf_num_locals (abfd) = num_locals;
3229 elf_num_globals (abfd) = num_globals;
b34976b6 3230 return TRUE;
252b5132
RH
3231}
3232
3233/* Align to the maximum file alignment that could be required for any
3234 ELF data structure. */
3235
268b6b39 3236static inline file_ptr
217aa764 3237align_file_position (file_ptr off, int align)
252b5132
RH
3238{
3239 return (off + align - 1) & ~(align - 1);
3240}
3241
3242/* Assign a file position to a section, optionally aligning to the
3243 required section alignment. */
3244
217aa764
AM
3245file_ptr
3246_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3247 file_ptr offset,
3248 bfd_boolean align)
252b5132 3249{
72de5009
AM
3250 if (align && i_shdrp->sh_addralign > 1)
3251 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
252b5132
RH
3252 i_shdrp->sh_offset = offset;
3253 if (i_shdrp->bfd_section != NULL)
3254 i_shdrp->bfd_section->filepos = offset;
3255 if (i_shdrp->sh_type != SHT_NOBITS)
3256 offset += i_shdrp->sh_size;
3257 return offset;
3258}
3259
3260/* Compute the file positions we are going to put the sections at, and
3261 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3262 is not NULL, this is being called by the ELF backend linker. */
3263
b34976b6 3264bfd_boolean
217aa764
AM
3265_bfd_elf_compute_section_file_positions (bfd *abfd,
3266 struct bfd_link_info *link_info)
252b5132 3267{
9c5bfbb7 3268 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
b34976b6 3269 bfd_boolean failed;
4b6c0f2f 3270 struct bfd_strtab_hash *strtab = NULL;
252b5132
RH
3271 Elf_Internal_Shdr *shstrtab_hdr;
3272
3273 if (abfd->output_has_begun)
b34976b6 3274 return TRUE;
252b5132
RH
3275
3276 /* Do any elf backend specific processing first. */
3277 if (bed->elf_backend_begin_write_processing)
3278 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3279
3280 if (! prep_headers (abfd))
b34976b6 3281 return FALSE;
252b5132 3282
e6c51ed4
NC
3283 /* Post process the headers if necessary. */
3284 if (bed->elf_backend_post_process_headers)
3285 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3286
b34976b6 3287 failed = FALSE;
252b5132
RH
3288 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3289 if (failed)
b34976b6 3290 return FALSE;
252b5132 3291
da9f89d4 3292 if (!assign_section_numbers (abfd, link_info))
b34976b6 3293 return FALSE;
252b5132
RH
3294
3295 /* The backend linker builds symbol table information itself. */
3296 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3297 {
3298 /* Non-zero if doing a relocatable link. */
3299 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3300
3301 if (! swap_out_syms (abfd, &strtab, relocatable_p))
b34976b6 3302 return FALSE;
252b5132
RH
3303 }
3304
1126897b 3305 if (link_info == NULL)
dbb410c3 3306 {
1126897b 3307 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 3308 if (failed)
b34976b6 3309 return FALSE;
dbb410c3
AM
3310 }
3311
252b5132
RH
3312 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3313 /* sh_name was set in prep_headers. */
3314 shstrtab_hdr->sh_type = SHT_STRTAB;
3315 shstrtab_hdr->sh_flags = 0;
3316 shstrtab_hdr->sh_addr = 0;
2b0f7ef9 3317 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
252b5132
RH
3318 shstrtab_hdr->sh_entsize = 0;
3319 shstrtab_hdr->sh_link = 0;
3320 shstrtab_hdr->sh_info = 0;
3321 /* sh_offset is set in assign_file_positions_except_relocs. */
3322 shstrtab_hdr->sh_addralign = 1;
3323
c84fca4d 3324 if (!assign_file_positions_except_relocs (abfd, link_info))
b34976b6 3325 return FALSE;
252b5132
RH
3326
3327 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3328 {
3329 file_ptr off;
3330 Elf_Internal_Shdr *hdr;
3331
3332 off = elf_tdata (abfd)->next_file_pos;
3333
3334 hdr = &elf_tdata (abfd)->symtab_hdr;
b34976b6 3335 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 3336
9ad5cbcf
AM
3337 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3338 if (hdr->sh_size != 0)
b34976b6 3339 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
9ad5cbcf 3340
252b5132 3341 hdr = &elf_tdata (abfd)->strtab_hdr;
b34976b6 3342 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132
RH
3343
3344 elf_tdata (abfd)->next_file_pos = off;
3345
3346 /* Now that we know where the .strtab section goes, write it
08a40648 3347 out. */
252b5132
RH
3348 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3349 || ! _bfd_stringtab_emit (abfd, strtab))
b34976b6 3350 return FALSE;
252b5132
RH
3351 _bfd_stringtab_free (strtab);
3352 }
3353
b34976b6 3354 abfd->output_has_begun = TRUE;
252b5132 3355
b34976b6 3356 return TRUE;
252b5132
RH
3357}
3358
8ded5a0f
AM
3359/* Make an initial estimate of the size of the program header. If we
3360 get the number wrong here, we'll redo section placement. */
3361
3362static bfd_size_type
3363get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3364{
3365 size_t segs;
3366 asection *s;
2b05f1b7 3367 const struct elf_backend_data *bed;
8ded5a0f
AM
3368
3369 /* Assume we will need exactly two PT_LOAD segments: one for text
3370 and one for data. */
3371 segs = 2;
3372
3373 s = bfd_get_section_by_name (abfd, ".interp");
3374 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3375 {
3376 /* If we have a loadable interpreter section, we need a
3377 PT_INTERP segment. In this case, assume we also need a
3378 PT_PHDR segment, although that may not be true for all
3379 targets. */
3380 segs += 2;
3381 }
3382
3383 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3384 {
3385 /* We need a PT_DYNAMIC segment. */
3386 ++segs;
f210dcff 3387 }
08a40648 3388
f210dcff
L
3389 if (info->relro)
3390 {
3391 /* We need a PT_GNU_RELRO segment. */
3392 ++segs;
8ded5a0f
AM
3393 }
3394
3395 if (elf_tdata (abfd)->eh_frame_hdr)
3396 {
3397 /* We need a PT_GNU_EH_FRAME segment. */
3398 ++segs;
3399 }
3400
2b05f1b7 3401 if (elf_tdata (abfd)->stack_flags)
8ded5a0f 3402 {
2b05f1b7
L
3403 /* We need a PT_GNU_STACK segment. */
3404 ++segs;
3405 }
94b11780 3406
2b05f1b7
L
3407 for (s = abfd->sections; s != NULL; s = s->next)
3408 {
8ded5a0f 3409 if ((s->flags & SEC_LOAD) != 0
0112cd26 3410 && CONST_STRNEQ (s->name, ".note"))
8ded5a0f
AM
3411 {
3412 /* We need a PT_NOTE segment. */
3413 ++segs;
1c5265b5
JJ
3414 /* Try to create just one PT_NOTE segment
3415 for all adjacent loadable .note* sections.
3416 gABI requires that within a PT_NOTE segment
3417 (and also inside of each SHT_NOTE section)
3418 each note is padded to a multiple of 4 size,
3419 so we check whether the sections are correctly
3420 aligned. */
3421 if (s->alignment_power == 2)
3422 while (s->next != NULL
3423 && s->next->alignment_power == 2
3424 && (s->next->flags & SEC_LOAD) != 0
3425 && CONST_STRNEQ (s->next->name, ".note"))
3426 s = s->next;
8ded5a0f
AM
3427 }
3428 }
3429
3430 for (s = abfd->sections; s != NULL; s = s->next)
3431 {
3432 if (s->flags & SEC_THREAD_LOCAL)
3433 {
3434 /* We need a PT_TLS segment. */
3435 ++segs;
3436 break;
3437 }
3438 }
3439
3440 /* Let the backend count up any program headers it might need. */
2b05f1b7 3441 bed = get_elf_backend_data (abfd);
8ded5a0f
AM
3442 if (bed->elf_backend_additional_program_headers)
3443 {
3444 int a;
3445
3446 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3447 if (a == -1)
3448 abort ();
3449 segs += a;
3450 }
3451
3452 return segs * bed->s->sizeof_phdr;
3453}
3454
2ea37f1c
NC
3455/* Find the segment that contains the output_section of section. */
3456
3457Elf_Internal_Phdr *
3458_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3459{
3460 struct elf_segment_map *m;
3461 Elf_Internal_Phdr *p;
3462
3463 for (m = elf_tdata (abfd)->segment_map,
3464 p = elf_tdata (abfd)->phdr;
3465 m != NULL;
3466 m = m->next, p++)
3467 {
3468 int i;
3469
3470 for (i = m->count - 1; i >= 0; i--)
3471 if (m->sections[i] == section)
3472 return p;
3473 }
3474
3475 return NULL;
3476}
3477
252b5132
RH
3478/* Create a mapping from a set of sections to a program segment. */
3479
217aa764
AM
3480static struct elf_segment_map *
3481make_mapping (bfd *abfd,
3482 asection **sections,
3483 unsigned int from,
3484 unsigned int to,
3485 bfd_boolean phdr)
252b5132
RH
3486{
3487 struct elf_segment_map *m;
3488 unsigned int i;
3489 asection **hdrpp;
dc810e39 3490 bfd_size_type amt;
252b5132 3491
dc810e39
AM
3492 amt = sizeof (struct elf_segment_map);
3493 amt += (to - from - 1) * sizeof (asection *);
217aa764 3494 m = bfd_zalloc (abfd, amt);
252b5132
RH
3495 if (m == NULL)
3496 return NULL;
3497 m->next = NULL;
3498 m->p_type = PT_LOAD;
3499 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3500 m->sections[i - from] = *hdrpp;
3501 m->count = to - from;
3502
3503 if (from == 0 && phdr)
3504 {
3505 /* Include the headers in the first PT_LOAD segment. */
3506 m->includes_filehdr = 1;
3507 m->includes_phdrs = 1;
3508 }
3509
3510 return m;
3511}
3512
229fcec5
MM
3513/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3514 on failure. */
3515
3516struct elf_segment_map *
3517_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3518{
3519 struct elf_segment_map *m;
3520
3521 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3522 if (m == NULL)
3523 return NULL;
3524 m->next = NULL;
3525 m->p_type = PT_DYNAMIC;
3526 m->count = 1;
3527 m->sections[0] = dynsec;
08a40648 3528
229fcec5
MM
3529 return m;
3530}
3531
8ded5a0f 3532/* Possibly add or remove segments from the segment map. */
252b5132 3533
b34976b6 3534static bfd_boolean
3dea8fca
AM
3535elf_modify_segment_map (bfd *abfd,
3536 struct bfd_link_info *info,
3537 bfd_boolean remove_empty_load)
252b5132 3538{
252e386e 3539 struct elf_segment_map **m;
8ded5a0f 3540 const struct elf_backend_data *bed;
252b5132 3541
8ded5a0f
AM
3542 /* The placement algorithm assumes that non allocated sections are
3543 not in PT_LOAD segments. We ensure this here by removing such
3544 sections from the segment map. We also remove excluded
252e386e
AM
3545 sections. Finally, any PT_LOAD segment without sections is
3546 removed. */
3547 m = &elf_tdata (abfd)->segment_map;
3548 while (*m)
8ded5a0f
AM
3549 {
3550 unsigned int i, new_count;
252b5132 3551
252e386e 3552 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 3553 {
252e386e
AM
3554 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3555 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3556 || (*m)->p_type != PT_LOAD))
8ded5a0f 3557 {
252e386e
AM
3558 (*m)->sections[new_count] = (*m)->sections[i];
3559 new_count++;
8ded5a0f
AM
3560 }
3561 }
252e386e 3562 (*m)->count = new_count;
252b5132 3563
3dea8fca 3564 if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
252e386e
AM
3565 *m = (*m)->next;
3566 else
3567 m = &(*m)->next;
8ded5a0f 3568 }
252b5132 3569
8ded5a0f
AM
3570 bed = get_elf_backend_data (abfd);
3571 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 3572 {
252e386e 3573 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
8ded5a0f 3574 return FALSE;
252b5132 3575 }
252b5132 3576
8ded5a0f
AM
3577 return TRUE;
3578}
252b5132 3579
8ded5a0f 3580/* Set up a mapping from BFD sections to program segments. */
252b5132 3581
8ded5a0f
AM
3582bfd_boolean
3583_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3584{
3585 unsigned int count;
3586 struct elf_segment_map *m;
3587 asection **sections = NULL;
3588 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3dea8fca 3589 bfd_boolean no_user_phdrs;
252b5132 3590
3dea8fca
AM
3591 no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3592 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 3593 {
8ded5a0f
AM
3594 asection *s;
3595 unsigned int i;
3596 struct elf_segment_map *mfirst;
3597 struct elf_segment_map **pm;
3598 asection *last_hdr;
3599 bfd_vma last_size;
3600 unsigned int phdr_index;
3601 bfd_vma maxpagesize;
3602 asection **hdrpp;
3603 bfd_boolean phdr_in_segment = TRUE;
3604 bfd_boolean writable;
3605 int tls_count = 0;
3606 asection *first_tls = NULL;
3607 asection *dynsec, *eh_frame_hdr;
3608 bfd_size_type amt;
252b5132 3609
8ded5a0f 3610 /* Select the allocated sections, and sort them. */
252b5132 3611
8ded5a0f
AM
3612 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3613 if (sections == NULL)
252b5132 3614 goto error_return;
252b5132 3615
8ded5a0f
AM
3616 i = 0;
3617 for (s = abfd->sections; s != NULL; s = s->next)
3618 {
3619 if ((s->flags & SEC_ALLOC) != 0)
3620 {
3621 sections[i] = s;
3622 ++i;
3623 }
3624 }
3625 BFD_ASSERT (i <= bfd_count_sections (abfd));
3626 count = i;
252b5132 3627
8ded5a0f 3628 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 3629
8ded5a0f 3630 /* Build the mapping. */
252b5132 3631
8ded5a0f
AM
3632 mfirst = NULL;
3633 pm = &mfirst;
252b5132 3634
8ded5a0f
AM
3635 /* If we have a .interp section, then create a PT_PHDR segment for
3636 the program headers and a PT_INTERP segment for the .interp
3637 section. */
3638 s = bfd_get_section_by_name (abfd, ".interp");
3639 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3640 {
3641 amt = sizeof (struct elf_segment_map);
3642 m = bfd_zalloc (abfd, amt);
3643 if (m == NULL)
3644 goto error_return;
3645 m->next = NULL;
3646 m->p_type = PT_PHDR;
3647 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3648 m->p_flags = PF_R | PF_X;
3649 m->p_flags_valid = 1;
3650 m->includes_phdrs = 1;
252b5132 3651
8ded5a0f
AM
3652 *pm = m;
3653 pm = &m->next;
252b5132 3654
8ded5a0f
AM
3655 amt = sizeof (struct elf_segment_map);
3656 m = bfd_zalloc (abfd, amt);
3657 if (m == NULL)
3658 goto error_return;
3659 m->next = NULL;
3660 m->p_type = PT_INTERP;
3661 m->count = 1;
3662 m->sections[0] = s;
3663
3664 *pm = m;
3665 pm = &m->next;
252b5132 3666 }
8ded5a0f
AM
3667
3668 /* Look through the sections. We put sections in the same program
3669 segment when the start of the second section can be placed within
3670 a few bytes of the end of the first section. */
3671 last_hdr = NULL;
3672 last_size = 0;
3673 phdr_index = 0;
3674 maxpagesize = bed->maxpagesize;
3675 writable = FALSE;
3676 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3677 if (dynsec != NULL
3678 && (dynsec->flags & SEC_LOAD) == 0)
3679 dynsec = NULL;
3680
3681 /* Deal with -Ttext or something similar such that the first section
3682 is not adjacent to the program headers. This is an
3683 approximation, since at this point we don't know exactly how many
3684 program headers we will need. */
3685 if (count > 0)
252b5132 3686 {
8ded5a0f
AM
3687 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3688
62d7a5f6 3689 if (phdr_size == (bfd_size_type) -1)
8ded5a0f
AM
3690 phdr_size = get_program_header_size (abfd, info);
3691 if ((abfd->flags & D_PAGED) == 0
3692 || sections[0]->lma < phdr_size
3693 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3694 phdr_in_segment = FALSE;
252b5132
RH
3695 }
3696
8ded5a0f 3697 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 3698 {
8ded5a0f
AM
3699 asection *hdr;
3700 bfd_boolean new_segment;
3701
3702 hdr = *hdrpp;
3703
3704 /* See if this section and the last one will fit in the same
3705 segment. */
3706
3707 if (last_hdr == NULL)
3708 {
3709 /* If we don't have a segment yet, then we don't need a new
3710 one (we build the last one after this loop). */
3711 new_segment = FALSE;
3712 }
3713 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3714 {
3715 /* If this section has a different relation between the
3716 virtual address and the load address, then we need a new
3717 segment. */
3718 new_segment = TRUE;
3719 }
39948a60
NC
3720 /* In the next test we have to be careful when last_hdr->lma is close
3721 to the end of the address space. If the aligned address wraps
3722 around to the start of the address space, then there are no more
3723 pages left in memory and it is OK to assume that the current
3724 section can be included in the current segment. */
3725 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3726 > last_hdr->lma)
3727 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4ff73856 3728 <= hdr->lma))
8ded5a0f
AM
3729 {
3730 /* If putting this section in this segment would force us to
3731 skip a page in the segment, then we need a new segment. */
3732 new_segment = TRUE;
3733 }
3734 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3735 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3736 {
3737 /* We don't want to put a loadable section after a
3738 nonloadable section in the same segment.
3739 Consider .tbss sections as loadable for this purpose. */
3740 new_segment = TRUE;
3741 }
3742 else if ((abfd->flags & D_PAGED) == 0)
3743 {
3744 /* If the file is not demand paged, which means that we
3745 don't require the sections to be correctly aligned in the
3746 file, then there is no other reason for a new segment. */
3747 new_segment = FALSE;
3748 }
3749 else if (! writable
3750 && (hdr->flags & SEC_READONLY) == 0
3751 && (((last_hdr->lma + last_size - 1)
3752 & ~(maxpagesize - 1))
3753 != (hdr->lma & ~(maxpagesize - 1))))
3754 {
3755 /* We don't want to put a writable section in a read only
3756 segment, unless they are on the same page in memory
3757 anyhow. We already know that the last section does not
3758 bring us past the current section on the page, so the
3759 only case in which the new section is not on the same
3760 page as the previous section is when the previous section
3761 ends precisely on a page boundary. */
3762 new_segment = TRUE;
3763 }
3764 else
3765 {
3766 /* Otherwise, we can use the same segment. */
3767 new_segment = FALSE;
3768 }
3769
2889e75b
NC
3770 /* Allow interested parties a chance to override our decision. */
3771 if (last_hdr && info->callbacks->override_segment_assignment)
3772 new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment);
3773
8ded5a0f
AM
3774 if (! new_segment)
3775 {
3776 if ((hdr->flags & SEC_READONLY) == 0)
3777 writable = TRUE;
3778 last_hdr = hdr;
3779 /* .tbss sections effectively have zero size. */
3780 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3781 != SEC_THREAD_LOCAL)
3782 last_size = hdr->size;
3783 else
3784 last_size = 0;
3785 continue;
3786 }
3787
3788 /* We need a new program segment. We must create a new program
3789 header holding all the sections from phdr_index until hdr. */
3790
3791 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3792 if (m == NULL)
3793 goto error_return;
3794
3795 *pm = m;
3796 pm = &m->next;
3797
252b5132 3798 if ((hdr->flags & SEC_READONLY) == 0)
b34976b6 3799 writable = TRUE;
8ded5a0f
AM
3800 else
3801 writable = FALSE;
3802
baaff79e
JJ
3803 last_hdr = hdr;
3804 /* .tbss sections effectively have zero size. */
e5caec89 3805 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
eea6121a 3806 last_size = hdr->size;
baaff79e
JJ
3807 else
3808 last_size = 0;
8ded5a0f
AM
3809 phdr_index = i;
3810 phdr_in_segment = FALSE;
252b5132
RH
3811 }
3812
8ded5a0f
AM
3813 /* Create a final PT_LOAD program segment. */
3814 if (last_hdr != NULL)
3815 {
3816 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3817 if (m == NULL)
3818 goto error_return;
252b5132 3819
8ded5a0f
AM
3820 *pm = m;
3821 pm = &m->next;
3822 }
252b5132 3823
8ded5a0f
AM
3824 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3825 if (dynsec != NULL)
3826 {
3827 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3828 if (m == NULL)
3829 goto error_return;
3830 *pm = m;
3831 pm = &m->next;
3832 }
252b5132 3833
1c5265b5
JJ
3834 /* For each batch of consecutive loadable .note sections,
3835 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3836 because if we link together nonloadable .note sections and
3837 loadable .note sections, we will generate two .note sections
3838 in the output file. FIXME: Using names for section types is
3839 bogus anyhow. */
8ded5a0f
AM
3840 for (s = abfd->sections; s != NULL; s = s->next)
3841 {
3842 if ((s->flags & SEC_LOAD) != 0
0112cd26 3843 && CONST_STRNEQ (s->name, ".note"))
8ded5a0f 3844 {
1c5265b5
JJ
3845 asection *s2;
3846 unsigned count = 1;
8ded5a0f 3847 amt = sizeof (struct elf_segment_map);
1c5265b5
JJ
3848 if (s->alignment_power == 2)
3849 for (s2 = s; s2->next != NULL; s2 = s2->next)
55b581a6
JJ
3850 {
3851 if (s2->next->alignment_power == 2
3852 && (s2->next->flags & SEC_LOAD) != 0
3853 && CONST_STRNEQ (s2->next->name, ".note")
3854 && align_power (s2->vma + s2->size, 2)
3855 == s2->next->vma)
3856 count++;
3857 else
3858 break;
3859 }
1c5265b5 3860 amt += (count - 1) * sizeof (asection *);
8ded5a0f
AM
3861 m = bfd_zalloc (abfd, amt);
3862 if (m == NULL)
3863 goto error_return;
3864 m->next = NULL;
3865 m->p_type = PT_NOTE;
1c5265b5
JJ
3866 m->count = count;
3867 while (count > 1)
3868 {
3869 m->sections[m->count - count--] = s;
3870 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3871 s = s->next;
3872 }
3873 m->sections[m->count - 1] = s;
3874 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
3875 *pm = m;
3876 pm = &m->next;
3877 }
3878 if (s->flags & SEC_THREAD_LOCAL)
3879 {
3880 if (! tls_count)
3881 first_tls = s;
3882 tls_count++;
3883 }
3884 }
252b5132 3885
8ded5a0f
AM
3886 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3887 if (tls_count > 0)
3888 {
3889 int i;
252b5132 3890
8ded5a0f
AM
3891 amt = sizeof (struct elf_segment_map);
3892 amt += (tls_count - 1) * sizeof (asection *);
3893 m = bfd_zalloc (abfd, amt);
3894 if (m == NULL)
3895 goto error_return;
3896 m->next = NULL;
3897 m->p_type = PT_TLS;
3898 m->count = tls_count;
3899 /* Mandated PF_R. */
3900 m->p_flags = PF_R;
3901 m->p_flags_valid = 1;
3902 for (i = 0; i < tls_count; ++i)
3903 {
3904 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3905 m->sections[i] = first_tls;
3906 first_tls = first_tls->next;
3907 }
252b5132 3908
8ded5a0f
AM
3909 *pm = m;
3910 pm = &m->next;
3911 }
252b5132 3912
8ded5a0f
AM
3913 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3914 segment. */
3915 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3916 if (eh_frame_hdr != NULL
3917 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 3918 {
dc810e39 3919 amt = sizeof (struct elf_segment_map);
217aa764 3920 m = bfd_zalloc (abfd, amt);
252b5132
RH
3921 if (m == NULL)
3922 goto error_return;
3923 m->next = NULL;
8ded5a0f 3924 m->p_type = PT_GNU_EH_FRAME;
252b5132 3925 m->count = 1;
8ded5a0f 3926 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
3927
3928 *pm = m;
3929 pm = &m->next;
3930 }
13ae64f3 3931
8ded5a0f 3932 if (elf_tdata (abfd)->stack_flags)
13ae64f3 3933 {
8ded5a0f
AM
3934 amt = sizeof (struct elf_segment_map);
3935 m = bfd_zalloc (abfd, amt);
3936 if (m == NULL)
3937 goto error_return;
3938 m->next = NULL;
2b05f1b7 3939 m->p_type = PT_GNU_STACK;
8ded5a0f
AM
3940 m->p_flags = elf_tdata (abfd)->stack_flags;
3941 m->p_flags_valid = 1;
252b5132 3942
8ded5a0f
AM
3943 *pm = m;
3944 pm = &m->next;
3945 }
65765700 3946
f210dcff 3947 if (info->relro)
8ded5a0f 3948 {
f210dcff
L
3949 for (m = mfirst; m != NULL; m = m->next)
3950 {
3951 if (m->p_type == PT_LOAD)
3952 {
3953 asection *last = m->sections[m->count - 1];
3954 bfd_vma vaddr = m->sections[0]->vma;
3955 bfd_vma filesz = last->vma - vaddr + last->size;
65765700 3956
f210dcff
L
3957 if (vaddr < info->relro_end
3958 && vaddr >= info->relro_start
3959 && (vaddr + filesz) >= info->relro_end)
3960 break;
3961 }
3962 }
3963
3964 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3965 if (m != NULL)
3966 {
3967 amt = sizeof (struct elf_segment_map);
3968 m = bfd_zalloc (abfd, amt);
3969 if (m == NULL)
3970 goto error_return;
3971 m->next = NULL;
3972 m->p_type = PT_GNU_RELRO;
3973 m->p_flags = PF_R;
3974 m->p_flags_valid = 1;
3975
3976 *pm = m;
3977 pm = &m->next;
3978 }
8ded5a0f 3979 }
9ee5e499 3980
8ded5a0f
AM
3981 free (sections);
3982 elf_tdata (abfd)->segment_map = mfirst;
9ee5e499
JJ
3983 }
3984
3dea8fca 3985 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
8ded5a0f 3986 return FALSE;
8c37241b 3987
8ded5a0f
AM
3988 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3989 ++count;
3990 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
252b5132 3991
b34976b6 3992 return TRUE;
252b5132
RH
3993
3994 error_return:
3995 if (sections != NULL)
3996 free (sections);
b34976b6 3997 return FALSE;
252b5132
RH
3998}
3999
4000/* Sort sections by address. */
4001
4002static int
217aa764 4003elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
4004{
4005 const asection *sec1 = *(const asection **) arg1;
4006 const asection *sec2 = *(const asection **) arg2;
eecdbe52 4007 bfd_size_type size1, size2;
252b5132
RH
4008
4009 /* Sort by LMA first, since this is the address used to
4010 place the section into a segment. */
4011 if (sec1->lma < sec2->lma)
4012 return -1;
4013 else if (sec1->lma > sec2->lma)
4014 return 1;
4015
4016 /* Then sort by VMA. Normally the LMA and the VMA will be
4017 the same, and this will do nothing. */
4018 if (sec1->vma < sec2->vma)
4019 return -1;
4020 else if (sec1->vma > sec2->vma)
4021 return 1;
4022
4023 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4024
07c6e936 4025#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
252b5132
RH
4026
4027 if (TOEND (sec1))
4028 {
4029 if (TOEND (sec2))
00a7cdc5
NC
4030 {
4031 /* If the indicies are the same, do not return 0
4032 here, but continue to try the next comparison. */
4033 if (sec1->target_index - sec2->target_index != 0)
4034 return sec1->target_index - sec2->target_index;
4035 }
252b5132
RH
4036 else
4037 return 1;
4038 }
00a7cdc5 4039 else if (TOEND (sec2))
252b5132
RH
4040 return -1;
4041
4042#undef TOEND
4043
00a7cdc5
NC
4044 /* Sort by size, to put zero sized sections
4045 before others at the same address. */
252b5132 4046
eea6121a
AM
4047 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4048 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
4049
4050 if (size1 < size2)
252b5132 4051 return -1;
eecdbe52 4052 if (size1 > size2)
252b5132
RH
4053 return 1;
4054
4055 return sec1->target_index - sec2->target_index;
4056}
4057
340b6d91
AC
4058/* Ian Lance Taylor writes:
4059
4060 We shouldn't be using % with a negative signed number. That's just
4061 not good. We have to make sure either that the number is not
4062 negative, or that the number has an unsigned type. When the types
4063 are all the same size they wind up as unsigned. When file_ptr is a
4064 larger signed type, the arithmetic winds up as signed long long,
4065 which is wrong.
4066
4067 What we're trying to say here is something like ``increase OFF by
4068 the least amount that will cause it to be equal to the VMA modulo
4069 the page size.'' */
4070/* In other words, something like:
4071
4072 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4073 off_offset = off % bed->maxpagesize;
4074 if (vma_offset < off_offset)
4075 adjustment = vma_offset + bed->maxpagesize - off_offset;
4076 else
4077 adjustment = vma_offset - off_offset;
08a40648 4078
340b6d91
AC
4079 which can can be collapsed into the expression below. */
4080
4081static file_ptr
4082vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4083{
4084 return ((vma - off) % maxpagesize);
4085}
4086
6d33f217
L
4087static void
4088print_segment_map (const struct elf_segment_map *m)
4089{
4090 unsigned int j;
4091 const char *pt = get_segment_type (m->p_type);
4092 char buf[32];
4093
4094 if (pt == NULL)
4095 {
4096 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4097 sprintf (buf, "LOPROC+%7.7x",
4098 (unsigned int) (m->p_type - PT_LOPROC));
4099 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4100 sprintf (buf, "LOOS+%7.7x",
4101 (unsigned int) (m->p_type - PT_LOOS));
4102 else
4103 snprintf (buf, sizeof (buf), "%8.8x",
4104 (unsigned int) m->p_type);
4105 pt = buf;
4106 }
4107 fprintf (stderr, "%s:", pt);
4108 for (j = 0; j < m->count; j++)
4109 fprintf (stderr, " %s", m->sections [j]->name);
4110 putc ('\n',stderr);
4111}
4112
252b5132
RH
4113/* Assign file positions to the sections based on the mapping from
4114 sections to segments. This function also sets up some fields in
f3520d2f 4115 the file header. */
252b5132 4116
b34976b6 4117static bfd_boolean
f3520d2f
AM
4118assign_file_positions_for_load_sections (bfd *abfd,
4119 struct bfd_link_info *link_info)
252b5132
RH
4120{
4121 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 4122 struct elf_segment_map *m;
252b5132 4123 Elf_Internal_Phdr *phdrs;
252b5132 4124 Elf_Internal_Phdr *p;
02bf8d82 4125 file_ptr off;
3f570048 4126 bfd_size_type maxpagesize;
f3520d2f 4127 unsigned int alloc;
0920dee7 4128 unsigned int i, j;
252b5132 4129
e36284ab 4130 if (link_info == NULL
3dea8fca 4131 && !elf_modify_segment_map (abfd, link_info, FALSE))
8ded5a0f 4132 return FALSE;
252b5132 4133
8ded5a0f 4134 alloc = 0;
252b5132 4135 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
8ded5a0f 4136 ++alloc;
252b5132
RH
4137
4138 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4139 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
8ded5a0f 4140 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 4141
62d7a5f6 4142 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
8ded5a0f
AM
4143 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4144 else
4145 BFD_ASSERT (elf_tdata (abfd)->program_header_size
59e0647f 4146 >= alloc * bed->s->sizeof_phdr);
252b5132
RH
4147
4148 if (alloc == 0)
f3520d2f 4149 {
8ded5a0f
AM
4150 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4151 return TRUE;
f3520d2f 4152 }
252b5132 4153
d0fb9a8d 4154 phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr));
f3520d2f 4155 elf_tdata (abfd)->phdr = phdrs;
252b5132 4156 if (phdrs == NULL)
b34976b6 4157 return FALSE;
252b5132 4158
3f570048
AM
4159 maxpagesize = 1;
4160 if ((abfd->flags & D_PAGED) != 0)
4161 maxpagesize = bed->maxpagesize;
4162
252b5132
RH
4163 off = bed->s->sizeof_ehdr;
4164 off += alloc * bed->s->sizeof_phdr;
4165
0920dee7 4166 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
252b5132 4167 m != NULL;
0920dee7 4168 m = m->next, p++, j++)
252b5132 4169 {
252b5132 4170 asection **secpp;
bf988460
AM
4171 bfd_vma off_adjust;
4172 bfd_boolean no_contents;
252b5132
RH
4173
4174 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 4175 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
4176 not be done to the PT_NOTE section of a corefile, which may
4177 contain several pseudo-sections artificially created by bfd.
4178 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
4179 if (m->count > 1
4180 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 4181 && m->p_type == PT_NOTE))
252b5132
RH
4182 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4183 elf_sort_sections);
4184
b301b248
AM
4185 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4186 number of sections with contents contributing to both p_filesz
4187 and p_memsz, followed by a number of sections with no contents
4188 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 4189 available file offset for PT_LOAD and PT_NOTE segments. */
252b5132 4190 p->p_type = m->p_type;
28a7f3e7 4191 p->p_flags = m->p_flags;
252b5132 4192
3f570048
AM
4193 if (m->count == 0)
4194 p->p_vaddr = 0;
4195 else
3271a814 4196 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
3f570048
AM
4197
4198 if (m->p_paddr_valid)
4199 p->p_paddr = m->p_paddr;
4200 else if (m->count == 0)
4201 p->p_paddr = 0;
4202 else
08a40648 4203 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
3f570048
AM
4204
4205 if (p->p_type == PT_LOAD
4206 && (abfd->flags & D_PAGED) != 0)
4207 {
4208 /* p_align in demand paged PT_LOAD segments effectively stores
4209 the maximum page size. When copying an executable with
4210 objcopy, we set m->p_align from the input file. Use this
4211 value for maxpagesize rather than bed->maxpagesize, which
4212 may be different. Note that we use maxpagesize for PT_TLS
4213 segment alignment later in this function, so we are relying
4214 on at least one PT_LOAD segment appearing before a PT_TLS
4215 segment. */
4216 if (m->p_align_valid)
4217 maxpagesize = m->p_align;
4218
4219 p->p_align = maxpagesize;
4220 }
3271a814
NS
4221 else if (m->p_align_valid)
4222 p->p_align = m->p_align;
e970b90a
DJ
4223 else if (m->count == 0)
4224 p->p_align = 1 << bed->s->log_file_align;
3f570048
AM
4225 else
4226 p->p_align = 0;
4227
bf988460
AM
4228 no_contents = FALSE;
4229 off_adjust = 0;
252b5132 4230 if (p->p_type == PT_LOAD
b301b248 4231 && m->count > 0)
252b5132 4232 {
b301b248 4233 bfd_size_type align;
a49e53ed 4234 unsigned int align_power = 0;
b301b248 4235
3271a814
NS
4236 if (m->p_align_valid)
4237 align = p->p_align;
4238 else
252b5132 4239 {
3271a814
NS
4240 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4241 {
4242 unsigned int secalign;
08a40648 4243
3271a814
NS
4244 secalign = bfd_get_section_alignment (abfd, *secpp);
4245 if (secalign > align_power)
4246 align_power = secalign;
4247 }
4248 align = (bfd_size_type) 1 << align_power;
4249 if (align < maxpagesize)
4250 align = maxpagesize;
b301b248 4251 }
252b5132 4252
02bf8d82
AM
4253 for (i = 0; i < m->count; i++)
4254 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4255 /* If we aren't making room for this section, then
4256 it must be SHT_NOBITS regardless of what we've
4257 set via struct bfd_elf_special_section. */
4258 elf_section_type (m->sections[i]) = SHT_NOBITS;
4259
bf988460
AM
4260 /* Find out whether this segment contains any loadable
4261 sections. If the first section isn't loadable, the same
4262 holds for any other sections. */
4263 i = 0;
4264 while (elf_section_type (m->sections[i]) == SHT_NOBITS)
b301b248 4265 {
bf988460
AM
4266 /* If a segment starts with .tbss, we need to look
4267 at the next section to decide whether the segment
4268 has any loadable sections. */
4269 if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0
4270 || ++i >= m->count)
b301b248 4271 {
bf988460
AM
4272 no_contents = TRUE;
4273 break;
b301b248 4274 }
252b5132 4275 }
bf988460
AM
4276
4277 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4278 off += off_adjust;
4279 if (no_contents)
4280 {
4281 /* We shouldn't need to align the segment on disk since
4282 the segment doesn't need file space, but the gABI
4283 arguably requires the alignment and glibc ld.so
4284 checks it. So to comply with the alignment
4285 requirement but not waste file space, we adjust
4286 p_offset for just this segment. (OFF_ADJUST is
4287 subtracted from OFF later.) This may put p_offset
4288 past the end of file, but that shouldn't matter. */
4289 }
4290 else
4291 off_adjust = 0;
252b5132 4292 }
b1a6d0b1
NC
4293 /* Make sure the .dynamic section is the first section in the
4294 PT_DYNAMIC segment. */
4295 else if (p->p_type == PT_DYNAMIC
4296 && m->count > 1
4297 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4298 {
4299 _bfd_error_handler
b301b248
AM
4300 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4301 abfd);
b1a6d0b1
NC
4302 bfd_set_error (bfd_error_bad_value);
4303 return FALSE;
4304 }
3f001e84
JK
4305 /* Set the note section type to SHT_NOTE. */
4306 else if (p->p_type == PT_NOTE)
4307 for (i = 0; i < m->count; i++)
4308 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 4309
252b5132
RH
4310 p->p_offset = 0;
4311 p->p_filesz = 0;
4312 p->p_memsz = 0;
4313
4314 if (m->includes_filehdr)
4315 {
bf988460 4316 if (!m->p_flags_valid)
252b5132 4317 p->p_flags |= PF_R;
252b5132
RH
4318 p->p_filesz = bed->s->sizeof_ehdr;
4319 p->p_memsz = bed->s->sizeof_ehdr;
4320 if (m->count > 0)
4321 {
4322 BFD_ASSERT (p->p_type == PT_LOAD);
4323
4324 if (p->p_vaddr < (bfd_vma) off)
4325 {
caf47ea6 4326 (*_bfd_error_handler)
b301b248
AM
4327 (_("%B: Not enough room for program headers, try linking with -N"),
4328 abfd);
252b5132 4329 bfd_set_error (bfd_error_bad_value);
b34976b6 4330 return FALSE;
252b5132
RH
4331 }
4332
4333 p->p_vaddr -= off;
bf988460 4334 if (!m->p_paddr_valid)
252b5132
RH
4335 p->p_paddr -= off;
4336 }
252b5132
RH
4337 }
4338
4339 if (m->includes_phdrs)
4340 {
bf988460 4341 if (!m->p_flags_valid)
252b5132
RH
4342 p->p_flags |= PF_R;
4343
f3520d2f 4344 if (!m->includes_filehdr)
252b5132
RH
4345 {
4346 p->p_offset = bed->s->sizeof_ehdr;
4347
4348 if (m->count > 0)
4349 {
4350 BFD_ASSERT (p->p_type == PT_LOAD);
4351 p->p_vaddr -= off - p->p_offset;
bf988460 4352 if (!m->p_paddr_valid)
252b5132
RH
4353 p->p_paddr -= off - p->p_offset;
4354 }
252b5132
RH
4355 }
4356
4357 p->p_filesz += alloc * bed->s->sizeof_phdr;
4358 p->p_memsz += alloc * bed->s->sizeof_phdr;
4359 }
4360
4361 if (p->p_type == PT_LOAD
4362 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4363 {
bf988460 4364 if (!m->includes_filehdr && !m->includes_phdrs)
02bf8d82 4365 p->p_offset = off;
252b5132
RH
4366 else
4367 {
4368 file_ptr adjust;
4369
4370 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
4371 if (!no_contents)
4372 p->p_filesz += adjust;
252b5132
RH
4373 p->p_memsz += adjust;
4374 }
4375 }
4376
1ea63fd2
AM
4377 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4378 maps. Set filepos for sections in PT_LOAD segments, and in
4379 core files, for sections in PT_NOTE segments.
4380 assign_file_positions_for_non_load_sections will set filepos
4381 for other sections and update p_filesz for other segments. */
252b5132
RH
4382 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4383 {
4384 asection *sec;
252b5132 4385 bfd_size_type align;
627b32bc 4386 Elf_Internal_Shdr *this_hdr;
252b5132
RH
4387
4388 sec = *secpp;
02bf8d82 4389 this_hdr = &elf_section_data (sec)->this_hdr;
3f570048 4390 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
252b5132 4391
88967714
AM
4392 if ((p->p_type == PT_LOAD
4393 || p->p_type == PT_TLS)
4394 && (this_hdr->sh_type != SHT_NOBITS
4395 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4396 && ((this_hdr->sh_flags & SHF_TLS) == 0
4397 || p->p_type == PT_TLS))))
252b5132 4398 {
11701589 4399 bfd_signed_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
252b5132 4400
88967714 4401 if (adjust < 0)
252b5132 4402 {
88967714 4403 (*_bfd_error_handler)
11701589 4404 (_("%B: section %A vma 0x%lx overlaps previous sections"),
37c43c55 4405 abfd, sec, (unsigned long) sec->vma);
88967714
AM
4406 adjust = 0;
4407 }
4408 p->p_memsz += adjust;
0e922b77 4409
88967714
AM
4410 if (this_hdr->sh_type != SHT_NOBITS)
4411 {
4412 off += adjust;
4413 p->p_filesz += adjust;
252b5132 4414 }
252b5132
RH
4415 }
4416
4417 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4418 {
b301b248
AM
4419 /* The section at i == 0 is the one that actually contains
4420 everything. */
4a938328
MS
4421 if (i == 0)
4422 {
627b32bc 4423 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
4424 off += this_hdr->sh_size;
4425 p->p_filesz = this_hdr->sh_size;
b301b248
AM
4426 p->p_memsz = 0;
4427 p->p_align = 1;
252b5132 4428 }
4a938328 4429 else
252b5132 4430 {
b301b248 4431 /* The rest are fake sections that shouldn't be written. */
252b5132 4432 sec->filepos = 0;
eea6121a 4433 sec->size = 0;
b301b248
AM
4434 sec->flags = 0;
4435 continue;
252b5132 4436 }
252b5132
RH
4437 }
4438 else
4439 {
b301b248
AM
4440 if (p->p_type == PT_LOAD)
4441 {
02bf8d82
AM
4442 this_hdr->sh_offset = sec->filepos = off;
4443 if (this_hdr->sh_type != SHT_NOBITS)
6a3cd2b4 4444 off += this_hdr->sh_size;
b301b248 4445 }
252b5132 4446
02bf8d82 4447 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 4448 {
6a3cd2b4 4449 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
4450 /* A load section without SHF_ALLOC is something like
4451 a note section in a PT_NOTE segment. These take
4452 file space but are not loaded into memory. */
4453 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 4454 p->p_memsz += this_hdr->sh_size;
b301b248 4455 }
6a3cd2b4 4456 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 4457 {
6a3cd2b4
AM
4458 if (p->p_type == PT_TLS)
4459 p->p_memsz += this_hdr->sh_size;
4460
4461 /* .tbss is special. It doesn't contribute to p_memsz of
4462 normal segments. */
4463 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4464 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
4465 }
4466
b10a8ae0
L
4467 if (align > p->p_align
4468 && !m->p_align_valid
4469 && (p->p_type != PT_LOAD
4470 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
4471 p->p_align = align;
4472 }
4473
bf988460 4474 if (!m->p_flags_valid)
252b5132
RH
4475 {
4476 p->p_flags |= PF_R;
02bf8d82 4477 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 4478 p->p_flags |= PF_X;
02bf8d82 4479 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
4480 p->p_flags |= PF_W;
4481 }
4482 }
bf988460 4483 off -= off_adjust;
0920dee7 4484
7c928300
AM
4485 /* Check that all sections are in a PT_LOAD segment.
4486 Don't check funky gdb generated core files. */
4487 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
0920dee7
L
4488 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4489 {
4490 Elf_Internal_Shdr *this_hdr;
4491 asection *sec;
4492
4493 sec = *secpp;
4494 this_hdr = &(elf_section_data(sec)->this_hdr);
4495 if (this_hdr->sh_size != 0
4496 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4497 {
4498 (*_bfd_error_handler)
4499 (_("%B: section `%A' can't be allocated in segment %d"),
4500 abfd, sec, j);
6d33f217 4501 print_segment_map (m);
0920dee7
L
4502 bfd_set_error (bfd_error_bad_value);
4503 return FALSE;
4504 }
4505 }
252b5132
RH
4506 }
4507
f3520d2f
AM
4508 elf_tdata (abfd)->next_file_pos = off;
4509 return TRUE;
4510}
4511
4512/* Assign file positions for the other sections. */
4513
4514static bfd_boolean
4515assign_file_positions_for_non_load_sections (bfd *abfd,
4516 struct bfd_link_info *link_info)
4517{
4518 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4519 Elf_Internal_Shdr **i_shdrpp;
4520 Elf_Internal_Shdr **hdrpp;
4521 Elf_Internal_Phdr *phdrs;
4522 Elf_Internal_Phdr *p;
4523 struct elf_segment_map *m;
4524 bfd_vma filehdr_vaddr, filehdr_paddr;
4525 bfd_vma phdrs_vaddr, phdrs_paddr;
4526 file_ptr off;
4527 unsigned int num_sec;
4528 unsigned int i;
4529 unsigned int count;
4530
5c182d5f
AM
4531 i_shdrpp = elf_elfsections (abfd);
4532 num_sec = elf_numsections (abfd);
f3520d2f 4533 off = elf_tdata (abfd)->next_file_pos;
5c182d5f
AM
4534 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4535 {
4536 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4537 Elf_Internal_Shdr *hdr;
4538
4539 hdr = *hdrpp;
4540 if (hdr->bfd_section != NULL
252e386e
AM
4541 && (hdr->bfd_section->filepos != 0
4542 || (hdr->sh_type == SHT_NOBITS
4543 && hdr->contents == NULL)))
627b32bc 4544 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
4545 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4546 {
49c13adb
L
4547 if (hdr->sh_size != 0)
4548 ((*_bfd_error_handler)
4549 (_("%B: warning: allocated section `%s' not in segment"),
3ba71138
L
4550 abfd,
4551 (hdr->bfd_section == NULL
4552 ? "*unknown*"
4553 : hdr->bfd_section->name)));
4554 /* We don't need to page align empty sections. */
4555 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5c182d5f
AM
4556 off += vma_page_aligned_bias (hdr->sh_addr, off,
4557 bed->maxpagesize);
4558 else
4559 off += vma_page_aligned_bias (hdr->sh_addr, off,
4560 hdr->sh_addralign);
4561 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4562 FALSE);
4563 }
4564 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4565 && hdr->bfd_section == NULL)
4566 || hdr == i_shdrpp[tdata->symtab_section]
4567 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4568 || hdr == i_shdrpp[tdata->strtab_section])
4569 hdr->sh_offset = -1;
4570 else
4571 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5c182d5f
AM
4572 }
4573
252b5132
RH
4574 /* Now that we have set the section file positions, we can set up
4575 the file positions for the non PT_LOAD segments. */
f3520d2f
AM
4576 count = 0;
4577 filehdr_vaddr = 0;
4578 filehdr_paddr = 0;
4579 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4580 phdrs_paddr = 0;
4581 phdrs = elf_tdata (abfd)->phdr;
4582 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4583 m != NULL;
4584 m = m->next, p++)
4585 {
4586 ++count;
4587 if (p->p_type != PT_LOAD)
4588 continue;
4589
4590 if (m->includes_filehdr)
4591 {
4592 filehdr_vaddr = p->p_vaddr;
4593 filehdr_paddr = p->p_paddr;
4594 }
4595 if (m->includes_phdrs)
4596 {
4597 phdrs_vaddr = p->p_vaddr;
4598 phdrs_paddr = p->p_paddr;
4599 if (m->includes_filehdr)
4600 {
4601 phdrs_vaddr += bed->s->sizeof_ehdr;
4602 phdrs_paddr += bed->s->sizeof_ehdr;
4603 }
4604 }
4605 }
4606
252b5132
RH
4607 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4608 m != NULL;
4609 m = m->next, p++)
4610 {
1ea63fd2 4611 if (m->count != 0)
252b5132 4612 {
1ea63fd2 4613 if (p->p_type != PT_LOAD
b10a8ae0
L
4614 && (p->p_type != PT_NOTE
4615 || bfd_get_format (abfd) != bfd_core))
229fcec5 4616 {
1ea63fd2 4617 Elf_Internal_Shdr *hdr;
b10a8ae0
L
4618 asection *sect;
4619
1ea63fd2
AM
4620 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4621
b10a8ae0
L
4622 sect = m->sections[m->count - 1];
4623 hdr = &elf_section_data (sect)->this_hdr;
4624 p->p_filesz = sect->filepos - m->sections[0]->filepos;
1ea63fd2
AM
4625 if (hdr->sh_type != SHT_NOBITS)
4626 p->p_filesz += hdr->sh_size;
4627
b10a8ae0
L
4628 if (p->p_type == PT_GNU_RELRO)
4629 {
4630 /* When we get here, we are copying executable
4631 or shared library. But we need to use the same
4632 linker logic. */
4633 Elf_Internal_Phdr *lp;
4634
4635 for (lp = phdrs; lp < phdrs + count; ++lp)
4636 {
4637 if (lp->p_type == PT_LOAD
4638 && lp->p_paddr == p->p_paddr)
4639 break;
4640 }
4641
4642 if (lp < phdrs + count)
4643 {
4644 /* We should use p_size if it is valid since it
4645 may contain the first few bytes of the next
4646 SEC_ALLOC section. */
4647 if (m->p_size_valid)
4648 p->p_filesz = m->p_size;
4649 else
4650 abort ();
4651 p->p_vaddr = lp->p_vaddr;
4652 p->p_offset = lp->p_offset;
4653 p->p_memsz = p->p_filesz;
4654 p->p_align = 1;
4655 }
4656 else
4657 abort ();
4658 }
4659 else
4660 p->p_offset = m->sections[0]->filepos;
229fcec5 4661 }
252b5132 4662 }
1ea63fd2 4663 else
252b5132
RH
4664 {
4665 if (m->includes_filehdr)
4666 {
4667 p->p_vaddr = filehdr_vaddr;
4668 if (! m->p_paddr_valid)
4669 p->p_paddr = filehdr_paddr;
4670 }
4671 else if (m->includes_phdrs)
4672 {
4673 p->p_vaddr = phdrs_vaddr;
4674 if (! m->p_paddr_valid)
4675 p->p_paddr = phdrs_paddr;
4676 }
8c37241b
JJ
4677 else if (p->p_type == PT_GNU_RELRO)
4678 {
4679 Elf_Internal_Phdr *lp;
4680
4681 for (lp = phdrs; lp < phdrs + count; ++lp)
4682 {
4683 if (lp->p_type == PT_LOAD
4684 && lp->p_vaddr <= link_info->relro_end
4685 && lp->p_vaddr >= link_info->relro_start
e36284ab
AM
4686 && (lp->p_vaddr + lp->p_filesz
4687 >= link_info->relro_end))
8c37241b
JJ
4688 break;
4689 }
4690
4691 if (lp < phdrs + count
4692 && link_info->relro_end > lp->p_vaddr)
4693 {
4694 p->p_vaddr = lp->p_vaddr;
4695 p->p_paddr = lp->p_paddr;
4696 p->p_offset = lp->p_offset;
4697 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4698 p->p_memsz = p->p_filesz;
4699 p->p_align = 1;
4700 p->p_flags = (lp->p_flags & ~PF_W);
4701 }
4702 else
4703 {
4704 memset (p, 0, sizeof *p);
4705 p->p_type = PT_NULL;
4706 }
4707 }
252b5132
RH
4708 }
4709 }
4710
252b5132
RH
4711 elf_tdata (abfd)->next_file_pos = off;
4712
b34976b6 4713 return TRUE;
252b5132
RH
4714}
4715
252b5132
RH
4716/* Work out the file positions of all the sections. This is called by
4717 _bfd_elf_compute_section_file_positions. All the section sizes and
4718 VMAs must be known before this is called.
4719
e0638f70
AM
4720 Reloc sections come in two flavours: Those processed specially as
4721 "side-channel" data attached to a section to which they apply, and
4722 those that bfd doesn't process as relocations. The latter sort are
4723 stored in a normal bfd section by bfd_section_from_shdr. We don't
4724 consider the former sort here, unless they form part of the loadable
4725 image. Reloc sections not assigned here will be handled later by
4726 assign_file_positions_for_relocs.
252b5132
RH
4727
4728 We also don't set the positions of the .symtab and .strtab here. */
4729
b34976b6 4730static bfd_boolean
c84fca4d
AO
4731assign_file_positions_except_relocs (bfd *abfd,
4732 struct bfd_link_info *link_info)
252b5132 4733{
5c182d5f
AM
4734 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4735 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
252b5132 4736 file_ptr off;
9c5bfbb7 4737 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
4738
4739 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4740 && bfd_get_format (abfd) != bfd_core)
4741 {
5c182d5f
AM
4742 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4743 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
4744 Elf_Internal_Shdr **hdrpp;
4745 unsigned int i;
4746
4747 /* Start after the ELF header. */
4748 off = i_ehdrp->e_ehsize;
4749
4750 /* We are not creating an executable, which means that we are
4751 not creating a program header, and that the actual order of
4752 the sections in the file is unimportant. */
9ad5cbcf 4753 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
4754 {
4755 Elf_Internal_Shdr *hdr;
4756
4757 hdr = *hdrpp;
e0638f70
AM
4758 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4759 && hdr->bfd_section == NULL)
9ad5cbcf
AM
4760 || i == tdata->symtab_section
4761 || i == tdata->symtab_shndx_section
252b5132
RH
4762 || i == tdata->strtab_section)
4763 {
4764 hdr->sh_offset = -1;
252b5132 4765 }
9ad5cbcf 4766 else
b34976b6 4767 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132
RH
4768 }
4769 }
4770 else
4771 {
f3520d2f
AM
4772 unsigned int alloc;
4773
252b5132 4774 /* Assign file positions for the loaded sections based on the
08a40648 4775 assignment of sections to segments. */
f3520d2f
AM
4776 if (!assign_file_positions_for_load_sections (abfd, link_info))
4777 return FALSE;
4778
4779 /* And for non-load sections. */
4780 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4781 return FALSE;
4782
e36284ab
AM
4783 if (bed->elf_backend_modify_program_headers != NULL)
4784 {
4785 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4786 return FALSE;
4787 }
4788
f3520d2f
AM
4789 /* Write out the program headers. */
4790 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4791 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4792 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
b34976b6 4793 return FALSE;
252b5132 4794
5c182d5f 4795 off = tdata->next_file_pos;
252b5132
RH
4796 }
4797
4798 /* Place the section headers. */
45d6a902 4799 off = align_file_position (off, 1 << bed->s->log_file_align);
252b5132
RH
4800 i_ehdrp->e_shoff = off;
4801 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4802
5c182d5f 4803 tdata->next_file_pos = off;
252b5132 4804
b34976b6 4805 return TRUE;
252b5132
RH
4806}
4807
b34976b6 4808static bfd_boolean
217aa764 4809prep_headers (bfd *abfd)
252b5132
RH
4810{
4811 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4812 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2b0f7ef9 4813 struct elf_strtab_hash *shstrtab;
9c5bfbb7 4814 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
4815
4816 i_ehdrp = elf_elfheader (abfd);
252b5132 4817
2b0f7ef9 4818 shstrtab = _bfd_elf_strtab_init ();
252b5132 4819 if (shstrtab == NULL)
b34976b6 4820 return FALSE;
252b5132
RH
4821
4822 elf_shstrtab (abfd) = shstrtab;
4823
4824 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4825 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4826 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4827 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4828
4829 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4830 i_ehdrp->e_ident[EI_DATA] =
4831 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4832 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4833
252b5132
RH
4834 if ((abfd->flags & DYNAMIC) != 0)
4835 i_ehdrp->e_type = ET_DYN;
4836 else if ((abfd->flags & EXEC_P) != 0)
4837 i_ehdrp->e_type = ET_EXEC;
4838 else if (bfd_get_format (abfd) == bfd_core)
4839 i_ehdrp->e_type = ET_CORE;
4840 else
4841 i_ehdrp->e_type = ET_REL;
4842
4843 switch (bfd_get_arch (abfd))
4844 {
4845 case bfd_arch_unknown:
4846 i_ehdrp->e_machine = EM_NONE;
4847 break;
aa4f99bb
AO
4848
4849 /* There used to be a long list of cases here, each one setting
4850 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4851 in the corresponding bfd definition. To avoid duplication,
4852 the switch was removed. Machines that need special handling
4853 can generally do it in elf_backend_final_write_processing(),
4854 unless they need the information earlier than the final write.
4855 Such need can generally be supplied by replacing the tests for
4856 e_machine with the conditions used to determine it. */
252b5132 4857 default:
9c5bfbb7
AM
4858 i_ehdrp->e_machine = bed->elf_machine_code;
4859 }
aa4f99bb 4860
252b5132
RH
4861 i_ehdrp->e_version = bed->s->ev_current;
4862 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4863
c044fabd 4864 /* No program header, for now. */
252b5132
RH
4865 i_ehdrp->e_phoff = 0;
4866 i_ehdrp->e_phentsize = 0;
4867 i_ehdrp->e_phnum = 0;
4868
c044fabd 4869 /* Each bfd section is section header entry. */
252b5132
RH
4870 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4871 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4872
c044fabd 4873 /* If we're building an executable, we'll need a program header table. */
252b5132 4874 if (abfd->flags & EXEC_P)
0e71e495
BE
4875 /* It all happens later. */
4876 ;
252b5132
RH
4877 else
4878 {
4879 i_ehdrp->e_phentsize = 0;
4880 i_phdrp = 0;
4881 i_ehdrp->e_phoff = 0;
4882 }
4883
4884 elf_tdata (abfd)->symtab_hdr.sh_name =
b34976b6 4885 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
252b5132 4886 elf_tdata (abfd)->strtab_hdr.sh_name =
b34976b6 4887 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
252b5132 4888 elf_tdata (abfd)->shstrtab_hdr.sh_name =
b34976b6 4889 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
252b5132
RH
4890 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4891 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4892 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
b34976b6 4893 return FALSE;
252b5132 4894
b34976b6 4895 return TRUE;
252b5132
RH
4896}
4897
4898/* Assign file positions for all the reloc sections which are not part
4899 of the loadable file image. */
4900
4901void
217aa764 4902_bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
252b5132
RH
4903{
4904 file_ptr off;
9ad5cbcf 4905 unsigned int i, num_sec;
252b5132
RH
4906 Elf_Internal_Shdr **shdrpp;
4907
4908 off = elf_tdata (abfd)->next_file_pos;
4909
9ad5cbcf
AM
4910 num_sec = elf_numsections (abfd);
4911 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
252b5132
RH
4912 {
4913 Elf_Internal_Shdr *shdrp;
4914
4915 shdrp = *shdrpp;
4916 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4917 && shdrp->sh_offset == -1)
b34976b6 4918 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
252b5132
RH
4919 }
4920
4921 elf_tdata (abfd)->next_file_pos = off;
4922}
4923
b34976b6 4924bfd_boolean
217aa764 4925_bfd_elf_write_object_contents (bfd *abfd)
252b5132 4926{
9c5bfbb7 4927 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
4928 Elf_Internal_Ehdr *i_ehdrp;
4929 Elf_Internal_Shdr **i_shdrp;
b34976b6 4930 bfd_boolean failed;
9ad5cbcf 4931 unsigned int count, num_sec;
252b5132
RH
4932
4933 if (! abfd->output_has_begun
217aa764 4934 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 4935 return FALSE;
252b5132
RH
4936
4937 i_shdrp = elf_elfsections (abfd);
4938 i_ehdrp = elf_elfheader (abfd);
4939
b34976b6 4940 failed = FALSE;
252b5132
RH
4941 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4942 if (failed)
b34976b6 4943 return FALSE;
252b5132
RH
4944
4945 _bfd_elf_assign_file_positions_for_relocs (abfd);
4946
c044fabd 4947 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
4948 num_sec = elf_numsections (abfd);
4949 for (count = 1; count < num_sec; count++)
252b5132
RH
4950 {
4951 if (bed->elf_backend_section_processing)
4952 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4953 if (i_shdrp[count]->contents)
4954 {
dc810e39
AM
4955 bfd_size_type amt = i_shdrp[count]->sh_size;
4956
252b5132 4957 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 4958 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
b34976b6 4959 return FALSE;
252b5132
RH
4960 }
4961 }
4962
4963 /* Write out the section header names. */
26ae6d5e
DJ
4964 if (elf_shstrtab (abfd) != NULL
4965 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 4966 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
b34976b6 4967 return FALSE;
252b5132
RH
4968
4969 if (bed->elf_backend_final_write_processing)
4970 (*bed->elf_backend_final_write_processing) (abfd,
4971 elf_tdata (abfd)->linker);
4972
ff59fc36
RM
4973 if (!bed->s->write_shdrs_and_ehdr (abfd))
4974 return FALSE;
4975
4976 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
bfb53a4f
RM
4977 if (elf_tdata (abfd)->after_write_object_contents)
4978 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
ff59fc36
RM
4979
4980 return TRUE;
252b5132
RH
4981}
4982
b34976b6 4983bfd_boolean
217aa764 4984_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 4985{
c044fabd 4986 /* Hopefully this can be done just like an object file. */
252b5132
RH
4987 return _bfd_elf_write_object_contents (abfd);
4988}
c044fabd
KH
4989
4990/* Given a section, search the header to find them. */
4991
cb33740c 4992unsigned int
198beae2 4993_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 4994{
9c5bfbb7 4995 const struct elf_backend_data *bed;
cb33740c 4996 unsigned int index;
252b5132 4997
9ad5cbcf
AM
4998 if (elf_section_data (asect) != NULL
4999 && elf_section_data (asect)->this_idx != 0)
5000 return elf_section_data (asect)->this_idx;
5001
5002 if (bfd_is_abs_section (asect))
af746e92
AM
5003 index = SHN_ABS;
5004 else if (bfd_is_com_section (asect))
5005 index = SHN_COMMON;
5006 else if (bfd_is_und_section (asect))
5007 index = SHN_UNDEF;
5008 else
cb33740c 5009 index = SHN_BAD;
252b5132 5010
af746e92 5011 bed = get_elf_backend_data (abfd);
252b5132
RH
5012 if (bed->elf_backend_section_from_bfd_section)
5013 {
af746e92 5014 int retval = index;
9ad5cbcf 5015
af746e92
AM
5016 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5017 return retval;
252b5132
RH
5018 }
5019
cb33740c 5020 if (index == SHN_BAD)
af746e92 5021 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 5022
af746e92 5023 return index;
252b5132
RH
5024}
5025
5026/* Given a BFD symbol, return the index in the ELF symbol table, or -1
5027 on error. */
5028
5029int
217aa764 5030_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
5031{
5032 asymbol *asym_ptr = *asym_ptr_ptr;
5033 int idx;
5034 flagword flags = asym_ptr->flags;
5035
5036 /* When gas creates relocations against local labels, it creates its
5037 own symbol for the section, but does put the symbol into the
5038 symbol chain, so udata is 0. When the linker is generating
5039 relocatable output, this section symbol may be for one of the
5040 input sections rather than the output section. */
5041 if (asym_ptr->udata.i == 0
5042 && (flags & BSF_SECTION_SYM)
5043 && asym_ptr->section)
5044 {
5372391b 5045 asection *sec;
252b5132
RH
5046 int indx;
5047
5372391b
AM
5048 sec = asym_ptr->section;
5049 if (sec->owner != abfd && sec->output_section != NULL)
5050 sec = sec->output_section;
5051 if (sec->owner == abfd
5052 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 5053 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
5054 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5055 }
5056
5057 idx = asym_ptr->udata.i;
5058
5059 if (idx == 0)
5060 {
5061 /* This case can occur when using --strip-symbol on a symbol
08a40648 5062 which is used in a relocation entry. */
252b5132 5063 (*_bfd_error_handler)
d003868e
AM
5064 (_("%B: symbol `%s' required but not present"),
5065 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
5066 bfd_set_error (bfd_error_no_symbols);
5067 return -1;
5068 }
5069
5070#if DEBUG & 4
5071 {
5072 fprintf (stderr,
661a3fd4 5073 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
252b5132
RH
5074 (long) asym_ptr, asym_ptr->name, idx, flags,
5075 elf_symbol_flags (flags));
5076 fflush (stderr);
5077 }
5078#endif
5079
5080 return idx;
5081}
5082
84d1d650 5083/* Rewrite program header information. */
252b5132 5084
b34976b6 5085static bfd_boolean
84d1d650 5086rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
252b5132 5087{
b34976b6
AM
5088 Elf_Internal_Ehdr *iehdr;
5089 struct elf_segment_map *map;
5090 struct elf_segment_map *map_first;
5091 struct elf_segment_map **pointer_to_map;
5092 Elf_Internal_Phdr *segment;
5093 asection *section;
5094 unsigned int i;
5095 unsigned int num_segments;
5096 bfd_boolean phdr_included = FALSE;
5c44b38e 5097 bfd_boolean p_paddr_valid;
b34976b6
AM
5098 bfd_vma maxpagesize;
5099 struct elf_segment_map *phdr_adjust_seg = NULL;
5100 unsigned int phdr_adjust_num = 0;
9c5bfbb7 5101 const struct elf_backend_data *bed;
bc67d8a6 5102
caf47ea6 5103 bed = get_elf_backend_data (ibfd);
252b5132
RH
5104 iehdr = elf_elfheader (ibfd);
5105
bc67d8a6 5106 map_first = NULL;
c044fabd 5107 pointer_to_map = &map_first;
252b5132
RH
5108
5109 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
5110 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5111
5112 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
5113#define SEGMENT_END(segment, start) \
5114 (start + (segment->p_memsz > segment->p_filesz \
5115 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 5116
eecdbe52
JJ
5117#define SECTION_SIZE(section, segment) \
5118 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5119 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 5120 ? section->size : 0)
eecdbe52 5121
b34976b6 5122 /* Returns TRUE if the given section is contained within
bc67d8a6 5123 the given segment. VMA addresses are compared. */
aecc8f8a
AM
5124#define IS_CONTAINED_BY_VMA(section, segment) \
5125 (section->vma >= segment->p_vaddr \
eecdbe52 5126 && (section->vma + SECTION_SIZE (section, segment) \
aecc8f8a 5127 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 5128
b34976b6 5129 /* Returns TRUE if the given section is contained within
bc67d8a6 5130 the given segment. LMA addresses are compared. */
aecc8f8a
AM
5131#define IS_CONTAINED_BY_LMA(section, segment, base) \
5132 (section->lma >= base \
eecdbe52 5133 && (section->lma + SECTION_SIZE (section, segment) \
aecc8f8a 5134 <= SEGMENT_END (segment, base)))
252b5132 5135
0efc80c8
L
5136 /* Handle PT_NOTE segment. */
5137#define IS_NOTE(p, s) \
aecc8f8a 5138 (p->p_type == PT_NOTE \
0efc80c8 5139 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 5140 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 5141 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 5142 <= p->p_offset + p->p_filesz))
252b5132 5143
0efc80c8
L
5144 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5145 etc. */
5146#define IS_COREFILE_NOTE(p, s) \
5147 (IS_NOTE (p, s) \
5148 && bfd_get_format (ibfd) == bfd_core \
5149 && s->vma == 0 \
5150 && s->lma == 0)
5151
252b5132
RH
5152 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5153 linker, which generates a PT_INTERP section with p_vaddr and
5154 p_memsz set to 0. */
aecc8f8a
AM
5155#define IS_SOLARIS_PT_INTERP(p, s) \
5156 (p->p_vaddr == 0 \
5157 && p->p_paddr == 0 \
5158 && p->p_memsz == 0 \
5159 && p->p_filesz > 0 \
5160 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 5161 && s->size > 0 \
aecc8f8a 5162 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 5163 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 5164 <= p->p_offset + p->p_filesz))
5c440b1e 5165
bc67d8a6
NC
5166 /* Decide if the given section should be included in the given segment.
5167 A section will be included if:
f5ffc919 5168 1. It is within the address space of the segment -- we use the LMA
08a40648 5169 if that is set for the segment and the VMA otherwise,
0efc80c8
L
5170 2. It is an allocated section or a NOTE section in a PT_NOTE
5171 segment.
bc67d8a6 5172 3. There is an output section associated with it,
eecdbe52 5173 4. The section has not already been allocated to a previous segment.
2b05f1b7 5174 5. PT_GNU_STACK segments do not include any sections.
03394ac9 5175 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
5176 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5177 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 5178 (with the possible exception of .dynamic). */
9f17e2a6 5179#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
2b05f1b7
L
5180 ((((segment->p_paddr \
5181 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5182 : IS_CONTAINED_BY_VMA (section, segment)) \
5183 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 5184 || IS_NOTE (segment, section)) \
2b05f1b7
L
5185 && segment->p_type != PT_GNU_STACK \
5186 && (segment->p_type != PT_TLS \
5187 || (section->flags & SEC_THREAD_LOCAL)) \
5188 && (segment->p_type == PT_LOAD \
5189 || segment->p_type == PT_TLS \
5190 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5191 && (segment->p_type != PT_DYNAMIC \
5192 || SECTION_SIZE (section, segment) > 0 \
5193 || (segment->p_paddr \
5194 ? segment->p_paddr != section->lma \
5195 : segment->p_vaddr != section->vma) \
5196 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5197 == 0)) \
0067a569 5198 && !section->segment_mark)
bc67d8a6 5199
9f17e2a6
L
5200/* If the output section of a section in the input segment is NULL,
5201 it is removed from the corresponding output segment. */
5202#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5203 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5204 && section->output_section != NULL)
5205
b34976b6 5206 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
5207#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5208 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5209
5210 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5211 their VMA address ranges and their LMA address ranges overlap.
5212 It is possible to have overlapping VMA ranges without overlapping LMA
5213 ranges. RedBoot images for example can have both .data and .bss mapped
5214 to the same VMA range, but with the .data section mapped to a different
5215 LMA. */
aecc8f8a 5216#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 5217 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 5218 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 5219 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 5220 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
5221
5222 /* Initialise the segment mark field. */
5223 for (section = ibfd->sections; section != NULL; section = section->next)
b34976b6 5224 section->segment_mark = FALSE;
bc67d8a6 5225
5c44b38e
AM
5226 /* The Solaris linker creates program headers in which all the
5227 p_paddr fields are zero. When we try to objcopy or strip such a
5228 file, we get confused. Check for this case, and if we find it
5229 don't set the p_paddr_valid fields. */
5230 p_paddr_valid = FALSE;
5231 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5232 i < num_segments;
5233 i++, segment++)
5234 if (segment->p_paddr != 0)
5235 {
5236 p_paddr_valid = TRUE;
5237 break;
5238 }
5239
252b5132 5240 /* Scan through the segments specified in the program header
bc67d8a6 5241 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 5242 in the loadable segments. These can be created by weird
aecc8f8a 5243 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
5244 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5245 i < num_segments;
c044fabd 5246 i++, segment++)
252b5132 5247 {
252b5132 5248 unsigned int j;
c044fabd 5249 Elf_Internal_Phdr *segment2;
252b5132 5250
aecc8f8a
AM
5251 if (segment->p_type == PT_INTERP)
5252 for (section = ibfd->sections; section; section = section->next)
5253 if (IS_SOLARIS_PT_INTERP (segment, section))
5254 {
5255 /* Mininal change so that the normal section to segment
4cc11e76 5256 assignment code will work. */
aecc8f8a
AM
5257 segment->p_vaddr = section->vma;
5258 break;
5259 }
5260
bc67d8a6 5261 if (segment->p_type != PT_LOAD)
b10a8ae0
L
5262 {
5263 /* Remove PT_GNU_RELRO segment. */
5264 if (segment->p_type == PT_GNU_RELRO)
5265 segment->p_type = PT_NULL;
5266 continue;
5267 }
c044fabd 5268
bc67d8a6 5269 /* Determine if this segment overlaps any previous segments. */
0067a569 5270 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
5271 {
5272 bfd_signed_vma extra_length;
c044fabd 5273
bc67d8a6 5274 if (segment2->p_type != PT_LOAD
0067a569 5275 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 5276 continue;
c044fabd 5277
bc67d8a6
NC
5278 /* Merge the two segments together. */
5279 if (segment2->p_vaddr < segment->p_vaddr)
5280 {
c044fabd 5281 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 5282 SEGMENT. */
0067a569
AM
5283 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5284 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 5285
bc67d8a6
NC
5286 if (extra_length > 0)
5287 {
0067a569 5288 segment2->p_memsz += extra_length;
bc67d8a6
NC
5289 segment2->p_filesz += extra_length;
5290 }
c044fabd 5291
bc67d8a6 5292 segment->p_type = PT_NULL;
c044fabd 5293
bc67d8a6
NC
5294 /* Since we have deleted P we must restart the outer loop. */
5295 i = 0;
5296 segment = elf_tdata (ibfd)->phdr;
5297 break;
5298 }
5299 else
5300 {
c044fabd 5301 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 5302 SEGMENT2. */
0067a569
AM
5303 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5304 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 5305
bc67d8a6
NC
5306 if (extra_length > 0)
5307 {
0067a569 5308 segment->p_memsz += extra_length;
bc67d8a6
NC
5309 segment->p_filesz += extra_length;
5310 }
c044fabd 5311
bc67d8a6
NC
5312 segment2->p_type = PT_NULL;
5313 }
5314 }
5315 }
c044fabd 5316
bc67d8a6
NC
5317 /* The second scan attempts to assign sections to segments. */
5318 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5319 i < num_segments;
0067a569 5320 i++, segment++)
bc67d8a6 5321 {
0067a569
AM
5322 unsigned int section_count;
5323 asection **sections;
5324 asection *output_section;
5325 unsigned int isec;
5326 bfd_vma matching_lma;
5327 bfd_vma suggested_lma;
5328 unsigned int j;
dc810e39 5329 bfd_size_type amt;
0067a569
AM
5330 asection *first_section;
5331 bfd_boolean first_matching_lma;
5332 bfd_boolean first_suggested_lma;
bc67d8a6
NC
5333
5334 if (segment->p_type == PT_NULL)
5335 continue;
c044fabd 5336
9f17e2a6 5337 first_section = NULL;
bc67d8a6 5338 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
5339 for (section = ibfd->sections, section_count = 0;
5340 section != NULL;
5341 section = section->next)
9f17e2a6
L
5342 {
5343 /* Find the first section in the input segment, which may be
5344 removed from the corresponding output segment. */
5345 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5346 {
5347 if (first_section == NULL)
5348 first_section = section;
5349 if (section->output_section != NULL)
5350 ++section_count;
5351 }
5352 }
811072d8 5353
b5f852ea
NC
5354 /* Allocate a segment map big enough to contain
5355 all of the sections we have selected. */
dc810e39
AM
5356 amt = sizeof (struct elf_segment_map);
5357 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
41f8ce69 5358 map = bfd_zalloc (obfd, amt);
bc67d8a6 5359 if (map == NULL)
b34976b6 5360 return FALSE;
252b5132
RH
5361
5362 /* Initialise the fields of the segment map. Default to
5363 using the physical address of the segment in the input BFD. */
0067a569
AM
5364 map->next = NULL;
5365 map->p_type = segment->p_type;
5366 map->p_flags = segment->p_flags;
bc67d8a6 5367 map->p_flags_valid = 1;
55d55ac7 5368
9f17e2a6
L
5369 /* If the first section in the input segment is removed, there is
5370 no need to preserve segment physical address in the corresponding
5371 output segment. */
945c025a 5372 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
5373 {
5374 map->p_paddr = segment->p_paddr;
5c44b38e 5375 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 5376 }
252b5132
RH
5377
5378 /* Determine if this segment contains the ELF file header
5379 and if it contains the program headers themselves. */
bc67d8a6
NC
5380 map->includes_filehdr = (segment->p_offset == 0
5381 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 5382 map->includes_phdrs = 0;
252b5132 5383
0067a569 5384 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 5385 {
bc67d8a6
NC
5386 map->includes_phdrs =
5387 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5388 && (segment->p_offset + segment->p_filesz
252b5132
RH
5389 >= ((bfd_vma) iehdr->e_phoff
5390 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 5391
bc67d8a6 5392 if (segment->p_type == PT_LOAD && map->includes_phdrs)
b34976b6 5393 phdr_included = TRUE;
252b5132
RH
5394 }
5395
bc67d8a6 5396 if (section_count == 0)
252b5132
RH
5397 {
5398 /* Special segments, such as the PT_PHDR segment, may contain
5399 no sections, but ordinary, loadable segments should contain
1ed89aa9
NC
5400 something. They are allowed by the ELF spec however, so only
5401 a warning is produced. */
bc67d8a6 5402 if (segment->p_type == PT_LOAD)
0067a569
AM
5403 (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5404 " detected, is this intentional ?\n"),
5405 ibfd);
252b5132 5406
bc67d8a6 5407 map->count = 0;
c044fabd
KH
5408 *pointer_to_map = map;
5409 pointer_to_map = &map->next;
252b5132
RH
5410
5411 continue;
5412 }
5413
5414 /* Now scan the sections in the input BFD again and attempt
5415 to add their corresponding output sections to the segment map.
5416 The problem here is how to handle an output section which has
5417 been moved (ie had its LMA changed). There are four possibilities:
5418
5419 1. None of the sections have been moved.
5420 In this case we can continue to use the segment LMA from the
5421 input BFD.
5422
5423 2. All of the sections have been moved by the same amount.
5424 In this case we can change the segment's LMA to match the LMA
5425 of the first section.
5426
5427 3. Some of the sections have been moved, others have not.
5428 In this case those sections which have not been moved can be
5429 placed in the current segment which will have to have its size,
5430 and possibly its LMA changed, and a new segment or segments will
5431 have to be created to contain the other sections.
5432
b5f852ea 5433 4. The sections have been moved, but not by the same amount.
252b5132
RH
5434 In this case we can change the segment's LMA to match the LMA
5435 of the first section and we will have to create a new segment
5436 or segments to contain the other sections.
5437
5438 In order to save time, we allocate an array to hold the section
5439 pointers that we are interested in. As these sections get assigned
5440 to a segment, they are removed from this array. */
5441
d0fb9a8d 5442 sections = bfd_malloc2 (section_count, sizeof (asection *));
252b5132 5443 if (sections == NULL)
b34976b6 5444 return FALSE;
252b5132
RH
5445
5446 /* Step One: Scan for segment vs section LMA conflicts.
5447 Also add the sections to the section array allocated above.
5448 Also add the sections to the current segment. In the common
5449 case, where the sections have not been moved, this means that
5450 we have completely filled the segment, and there is nothing
5451 more to do. */
252b5132 5452 isec = 0;
72730e0c 5453 matching_lma = 0;
252b5132 5454 suggested_lma = 0;
0067a569
AM
5455 first_matching_lma = TRUE;
5456 first_suggested_lma = TRUE;
252b5132 5457
147d51c2 5458 for (section = ibfd->sections;
bc67d8a6
NC
5459 section != NULL;
5460 section = section->next)
147d51c2
L
5461 if (section == first_section)
5462 break;
5463
5464 for (j = 0; section != NULL; section = section->next)
252b5132 5465 {
caf47ea6 5466 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c0f7859b 5467 {
bc67d8a6
NC
5468 output_section = section->output_section;
5469
0067a569 5470 sections[j++] = section;
252b5132
RH
5471
5472 /* The Solaris native linker always sets p_paddr to 0.
5473 We try to catch that case here, and set it to the
5e8d7549
NC
5474 correct value. Note - some backends require that
5475 p_paddr be left as zero. */
5c44b38e 5476 if (!p_paddr_valid
4455705d 5477 && segment->p_vaddr != 0
0067a569 5478 && !bed->want_p_paddr_set_to_zero
252b5132 5479 && isec == 0
bc67d8a6 5480 && output_section->lma != 0
0067a569
AM
5481 && output_section->vma == (segment->p_vaddr
5482 + (map->includes_filehdr
5483 ? iehdr->e_ehsize
5484 : 0)
5485 + (map->includes_phdrs
5486 ? (iehdr->e_phnum
5487 * iehdr->e_phentsize)
5488 : 0)))
bc67d8a6 5489 map->p_paddr = segment->p_vaddr;
252b5132
RH
5490
5491 /* Match up the physical address of the segment with the
5492 LMA address of the output section. */
bc67d8a6 5493 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5e8d7549 5494 || IS_COREFILE_NOTE (segment, section)
0067a569
AM
5495 || (bed->want_p_paddr_set_to_zero
5496 && IS_CONTAINED_BY_VMA (output_section, segment)))
252b5132 5497 {
0067a569
AM
5498 if (first_matching_lma || output_section->lma < matching_lma)
5499 {
5500 matching_lma = output_section->lma;
5501 first_matching_lma = FALSE;
5502 }
252b5132
RH
5503
5504 /* We assume that if the section fits within the segment
bc67d8a6 5505 then it does not overlap any other section within that
252b5132 5506 segment. */
0067a569
AM
5507 map->sections[isec++] = output_section;
5508 }
5509 else if (first_suggested_lma)
5510 {
5511 suggested_lma = output_section->lma;
5512 first_suggested_lma = FALSE;
252b5132 5513 }
147d51c2
L
5514
5515 if (j == section_count)
5516 break;
252b5132
RH
5517 }
5518 }
5519
bc67d8a6 5520 BFD_ASSERT (j == section_count);
252b5132
RH
5521
5522 /* Step Two: Adjust the physical address of the current segment,
5523 if necessary. */
bc67d8a6 5524 if (isec == section_count)
252b5132
RH
5525 {
5526 /* All of the sections fitted within the segment as currently
5527 specified. This is the default case. Add the segment to
5528 the list of built segments and carry on to process the next
5529 program header in the input BFD. */
bc67d8a6 5530 map->count = section_count;
c044fabd
KH
5531 *pointer_to_map = map;
5532 pointer_to_map = &map->next;
08a40648 5533
5c44b38e
AM
5534 if (p_paddr_valid
5535 && !bed->want_p_paddr_set_to_zero
147d51c2 5536 && matching_lma != map->p_paddr
5c44b38e
AM
5537 && !map->includes_filehdr
5538 && !map->includes_phdrs)
3271a814
NS
5539 /* There is some padding before the first section in the
5540 segment. So, we must account for that in the output
5541 segment's vma. */
5542 map->p_vaddr_offset = matching_lma - map->p_paddr;
08a40648 5543
252b5132
RH
5544 free (sections);
5545 continue;
5546 }
252b5132
RH
5547 else
5548 {
0067a569 5549 if (!first_matching_lma)
72730e0c
AM
5550 {
5551 /* At least one section fits inside the current segment.
5552 Keep it, but modify its physical address to match the
5553 LMA of the first section that fitted. */
bc67d8a6 5554 map->p_paddr = matching_lma;
72730e0c
AM
5555 }
5556 else
5557 {
5558 /* None of the sections fitted inside the current segment.
5559 Change the current segment's physical address to match
5560 the LMA of the first section. */
bc67d8a6 5561 map->p_paddr = suggested_lma;
72730e0c
AM
5562 }
5563
bc67d8a6
NC
5564 /* Offset the segment physical address from the lma
5565 to allow for space taken up by elf headers. */
5566 if (map->includes_filehdr)
010c8431
AM
5567 {
5568 if (map->p_paddr >= iehdr->e_ehsize)
5569 map->p_paddr -= iehdr->e_ehsize;
5570 else
5571 {
5572 map->includes_filehdr = FALSE;
5573 map->includes_phdrs = FALSE;
5574 }
5575 }
252b5132 5576
bc67d8a6
NC
5577 if (map->includes_phdrs)
5578 {
010c8431
AM
5579 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5580 {
5581 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5582
5583 /* iehdr->e_phnum is just an estimate of the number
5584 of program headers that we will need. Make a note
5585 here of the number we used and the segment we chose
5586 to hold these headers, so that we can adjust the
5587 offset when we know the correct value. */
5588 phdr_adjust_num = iehdr->e_phnum;
5589 phdr_adjust_seg = map;
5590 }
5591 else
5592 map->includes_phdrs = FALSE;
bc67d8a6 5593 }
252b5132
RH
5594 }
5595
5596 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 5597 those that fit to the current segment and removing them from the
252b5132
RH
5598 sections array; but making sure not to leave large gaps. Once all
5599 possible sections have been assigned to the current segment it is
5600 added to the list of built segments and if sections still remain
5601 to be assigned, a new segment is constructed before repeating
5602 the loop. */
5603 isec = 0;
5604 do
5605 {
bc67d8a6 5606 map->count = 0;
252b5132 5607 suggested_lma = 0;
0067a569 5608 first_suggested_lma = TRUE;
252b5132
RH
5609
5610 /* Fill the current segment with sections that fit. */
bc67d8a6 5611 for (j = 0; j < section_count; j++)
252b5132 5612 {
bc67d8a6 5613 section = sections[j];
252b5132 5614
bc67d8a6 5615 if (section == NULL)
252b5132
RH
5616 continue;
5617
bc67d8a6 5618 output_section = section->output_section;
252b5132 5619
bc67d8a6 5620 BFD_ASSERT (output_section != NULL);
c044fabd 5621
bc67d8a6
NC
5622 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5623 || IS_COREFILE_NOTE (segment, section))
252b5132 5624 {
bc67d8a6 5625 if (map->count == 0)
252b5132
RH
5626 {
5627 /* If the first section in a segment does not start at
bc67d8a6
NC
5628 the beginning of the segment, then something is
5629 wrong. */
0067a569
AM
5630 if (output_section->lma
5631 != (map->p_paddr
5632 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5633 + (map->includes_phdrs
5634 ? iehdr->e_phnum * iehdr->e_phentsize
5635 : 0)))
252b5132
RH
5636 abort ();
5637 }
5638 else
5639 {
0067a569 5640 asection *prev_sec;
252b5132 5641
bc67d8a6 5642 prev_sec = map->sections[map->count - 1];
252b5132
RH
5643
5644 /* If the gap between the end of the previous section
bc67d8a6
NC
5645 and the start of this section is more than
5646 maxpagesize then we need to start a new segment. */
eea6121a 5647 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 5648 maxpagesize)
caf47ea6 5649 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 5650 || (prev_sec->lma + prev_sec->size
079e9a2f 5651 > output_section->lma))
252b5132 5652 {
0067a569
AM
5653 if (first_suggested_lma)
5654 {
5655 suggested_lma = output_section->lma;
5656 first_suggested_lma = FALSE;
5657 }
252b5132
RH
5658
5659 continue;
5660 }
5661 }
5662
bc67d8a6 5663 map->sections[map->count++] = output_section;
252b5132
RH
5664 ++isec;
5665 sections[j] = NULL;
b34976b6 5666 section->segment_mark = TRUE;
252b5132 5667 }
0067a569
AM
5668 else if (first_suggested_lma)
5669 {
5670 suggested_lma = output_section->lma;
5671 first_suggested_lma = FALSE;
5672 }
252b5132
RH
5673 }
5674
bc67d8a6 5675 BFD_ASSERT (map->count > 0);
252b5132
RH
5676
5677 /* Add the current segment to the list of built segments. */
c044fabd
KH
5678 *pointer_to_map = map;
5679 pointer_to_map = &map->next;
252b5132 5680
bc67d8a6 5681 if (isec < section_count)
252b5132
RH
5682 {
5683 /* We still have not allocated all of the sections to
5684 segments. Create a new segment here, initialise it
5685 and carry on looping. */
dc810e39
AM
5686 amt = sizeof (struct elf_segment_map);
5687 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
217aa764 5688 map = bfd_alloc (obfd, amt);
bc67d8a6 5689 if (map == NULL)
5ed6aba4
NC
5690 {
5691 free (sections);
5692 return FALSE;
5693 }
252b5132
RH
5694
5695 /* Initialise the fields of the segment map. Set the physical
5696 physical address to the LMA of the first section that has
5697 not yet been assigned. */
0067a569
AM
5698 map->next = NULL;
5699 map->p_type = segment->p_type;
5700 map->p_flags = segment->p_flags;
5701 map->p_flags_valid = 1;
5702 map->p_paddr = suggested_lma;
5c44b38e 5703 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 5704 map->includes_filehdr = 0;
0067a569 5705 map->includes_phdrs = 0;
252b5132
RH
5706 }
5707 }
bc67d8a6 5708 while (isec < section_count);
252b5132
RH
5709
5710 free (sections);
5711 }
5712
bc67d8a6
NC
5713 elf_tdata (obfd)->segment_map = map_first;
5714
5715 /* If we had to estimate the number of program headers that were
9ad5cbcf 5716 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
5717 the offset if necessary. */
5718 if (phdr_adjust_seg != NULL)
5719 {
5720 unsigned int count;
c044fabd 5721
bc67d8a6 5722 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 5723 count++;
252b5132 5724
bc67d8a6
NC
5725 if (count > phdr_adjust_num)
5726 phdr_adjust_seg->p_paddr
5727 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5728 }
c044fabd 5729
bc67d8a6 5730#undef SEGMENT_END
eecdbe52 5731#undef SECTION_SIZE
bc67d8a6
NC
5732#undef IS_CONTAINED_BY_VMA
5733#undef IS_CONTAINED_BY_LMA
0efc80c8 5734#undef IS_NOTE
252b5132 5735#undef IS_COREFILE_NOTE
bc67d8a6 5736#undef IS_SOLARIS_PT_INTERP
9f17e2a6 5737#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
5738#undef INCLUDE_SECTION_IN_SEGMENT
5739#undef SEGMENT_AFTER_SEGMENT
5740#undef SEGMENT_OVERLAPS
b34976b6 5741 return TRUE;
252b5132
RH
5742}
5743
84d1d650
L
5744/* Copy ELF program header information. */
5745
5746static bfd_boolean
5747copy_elf_program_header (bfd *ibfd, bfd *obfd)
5748{
5749 Elf_Internal_Ehdr *iehdr;
5750 struct elf_segment_map *map;
5751 struct elf_segment_map *map_first;
5752 struct elf_segment_map **pointer_to_map;
5753 Elf_Internal_Phdr *segment;
5754 unsigned int i;
5755 unsigned int num_segments;
5756 bfd_boolean phdr_included = FALSE;
88967714 5757 bfd_boolean p_paddr_valid;
84d1d650
L
5758
5759 iehdr = elf_elfheader (ibfd);
5760
5761 map_first = NULL;
5762 pointer_to_map = &map_first;
5763
88967714
AM
5764 /* If all the segment p_paddr fields are zero, don't set
5765 map->p_paddr_valid. */
5766 p_paddr_valid = FALSE;
84d1d650 5767 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
5768 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5769 i < num_segments;
5770 i++, segment++)
5771 if (segment->p_paddr != 0)
5772 {
5773 p_paddr_valid = TRUE;
5774 break;
5775 }
5776
84d1d650
L
5777 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5778 i < num_segments;
5779 i++, segment++)
5780 {
5781 asection *section;
5782 unsigned int section_count;
5783 bfd_size_type amt;
5784 Elf_Internal_Shdr *this_hdr;
53020534 5785 asection *first_section = NULL;
c981028a 5786 asection *lowest_section = NULL;
84d1d650 5787
84d1d650
L
5788 /* Compute how many sections are in this segment. */
5789 for (section = ibfd->sections, section_count = 0;
5790 section != NULL;
5791 section = section->next)
5792 {
5793 this_hdr = &(elf_section_data(section)->this_hdr);
5794 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
3271a814 5795 {
53020534 5796 if (!first_section)
c981028a
DJ
5797 first_section = lowest_section = section;
5798 if (section->lma < lowest_section->lma)
5799 lowest_section = section;
3271a814
NS
5800 section_count++;
5801 }
84d1d650
L
5802 }
5803
5804 /* Allocate a segment map big enough to contain
5805 all of the sections we have selected. */
5806 amt = sizeof (struct elf_segment_map);
5807 if (section_count != 0)
5808 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
41f8ce69 5809 map = bfd_zalloc (obfd, amt);
84d1d650
L
5810 if (map == NULL)
5811 return FALSE;
5812
5813 /* Initialize the fields of the output segment map with the
5814 input segment. */
5815 map->next = NULL;
5816 map->p_type = segment->p_type;
5817 map->p_flags = segment->p_flags;
5818 map->p_flags_valid = 1;
5819 map->p_paddr = segment->p_paddr;
88967714 5820 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
5821 map->p_align = segment->p_align;
5822 map->p_align_valid = 1;
3271a814 5823 map->p_vaddr_offset = 0;
84d1d650 5824
b10a8ae0
L
5825 if (map->p_type == PT_GNU_RELRO
5826 && segment->p_filesz == segment->p_memsz)
5827 {
5828 /* The PT_GNU_RELRO segment may contain the first a few
5829 bytes in the .got.plt section even if the whole .got.plt
5830 section isn't in the PT_GNU_RELRO segment. We won't
5831 change the size of the PT_GNU_RELRO segment. */
5832 map->p_size = segment->p_filesz;
5833 map->p_size_valid = 1;
5834 }
5835
84d1d650
L
5836 /* Determine if this segment contains the ELF file header
5837 and if it contains the program headers themselves. */
5838 map->includes_filehdr = (segment->p_offset == 0
5839 && segment->p_filesz >= iehdr->e_ehsize);
5840
5841 map->includes_phdrs = 0;
5842 if (! phdr_included || segment->p_type != PT_LOAD)
5843 {
5844 map->includes_phdrs =
5845 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5846 && (segment->p_offset + segment->p_filesz
5847 >= ((bfd_vma) iehdr->e_phoff
5848 + iehdr->e_phnum * iehdr->e_phentsize)));
5849
5850 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5851 phdr_included = TRUE;
5852 }
5853
88967714
AM
5854 if (!map->includes_phdrs
5855 && !map->includes_filehdr
5856 && map->p_paddr_valid)
3271a814 5857 /* There is some other padding before the first section. */
c981028a 5858 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
53020534 5859 - segment->p_paddr);
08a40648 5860
84d1d650
L
5861 if (section_count != 0)
5862 {
5863 unsigned int isec = 0;
5864
53020534 5865 for (section = first_section;
84d1d650
L
5866 section != NULL;
5867 section = section->next)
5868 {
5869 this_hdr = &(elf_section_data(section)->this_hdr);
5870 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
53020534
L
5871 {
5872 map->sections[isec++] = section->output_section;
5873 if (isec == section_count)
5874 break;
5875 }
84d1d650
L
5876 }
5877 }
5878
5879 map->count = section_count;
5880 *pointer_to_map = map;
5881 pointer_to_map = &map->next;
5882 }
5883
5884 elf_tdata (obfd)->segment_map = map_first;
5885 return TRUE;
5886}
5887
5888/* Copy private BFD data. This copies or rewrites ELF program header
5889 information. */
5890
5891static bfd_boolean
5892copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5893{
84d1d650
L
5894 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5895 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5896 return TRUE;
5897
5898 if (elf_tdata (ibfd)->phdr == NULL)
5899 return TRUE;
5900
5901 if (ibfd->xvec == obfd->xvec)
5902 {
cb3ff1e5
NC
5903 /* Check to see if any sections in the input BFD
5904 covered by ELF program header have changed. */
d55ce4e2 5905 Elf_Internal_Phdr *segment;
84d1d650
L
5906 asection *section, *osec;
5907 unsigned int i, num_segments;
5908 Elf_Internal_Shdr *this_hdr;
147d51c2
L
5909 const struct elf_backend_data *bed;
5910
5911 bed = get_elf_backend_data (ibfd);
5912
5913 /* Regenerate the segment map if p_paddr is set to 0. */
5914 if (bed->want_p_paddr_set_to_zero)
5915 goto rewrite;
84d1d650
L
5916
5917 /* Initialize the segment mark field. */
5918 for (section = obfd->sections; section != NULL;
5919 section = section->next)
5920 section->segment_mark = FALSE;
5921
5922 num_segments = elf_elfheader (ibfd)->e_phnum;
5923 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5924 i < num_segments;
5925 i++, segment++)
5926 {
5f6999aa
NC
5927 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5928 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5929 which severly confuses things, so always regenerate the segment
5930 map in this case. */
5931 if (segment->p_paddr == 0
5932 && segment->p_memsz == 0
5933 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 5934 goto rewrite;
5f6999aa 5935
84d1d650
L
5936 for (section = ibfd->sections;
5937 section != NULL; section = section->next)
5938 {
5939 /* We mark the output section so that we know it comes
5940 from the input BFD. */
5941 osec = section->output_section;
5942 if (osec)
5943 osec->segment_mark = TRUE;
5944
5945 /* Check if this section is covered by the segment. */
5946 this_hdr = &(elf_section_data(section)->this_hdr);
5947 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5948 {
5949 /* FIXME: Check if its output section is changed or
5950 removed. What else do we need to check? */
5951 if (osec == NULL
5952 || section->flags != osec->flags
5953 || section->lma != osec->lma
5954 || section->vma != osec->vma
5955 || section->size != osec->size
5956 || section->rawsize != osec->rawsize
5957 || section->alignment_power != osec->alignment_power)
5958 goto rewrite;
5959 }
5960 }
5961 }
5962
cb3ff1e5 5963 /* Check to see if any output section do not come from the
84d1d650
L
5964 input BFD. */
5965 for (section = obfd->sections; section != NULL;
5966 section = section->next)
5967 {
5968 if (section->segment_mark == FALSE)
5969 goto rewrite;
5970 else
5971 section->segment_mark = FALSE;
5972 }
5973
5974 return copy_elf_program_header (ibfd, obfd);
5975 }
5976
5977rewrite:
5978 return rewrite_elf_program_header (ibfd, obfd);
5979}
5980
ccd2ec6a
L
5981/* Initialize private output section information from input section. */
5982
5983bfd_boolean
5984_bfd_elf_init_private_section_data (bfd *ibfd,
5985 asection *isec,
5986 bfd *obfd,
5987 asection *osec,
5988 struct bfd_link_info *link_info)
5989
5990{
5991 Elf_Internal_Shdr *ihdr, *ohdr;
5992 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
5993
5994 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5995 || obfd->xvec->flavour != bfd_target_elf_flavour)
5996 return TRUE;
5997
e843e0f8 5998 /* Don't copy the output ELF section type from input if the
d3fd4074 5999 output BFD section flags have been set to something different.
e843e0f8
L
6000 elf_fake_sections will set ELF section type based on BFD
6001 section flags. */
42bb2e33
AM
6002 if (elf_section_type (osec) == SHT_NULL
6003 && (osec->flags == isec->flags || !osec->flags))
6004 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
6005
6006 /* FIXME: Is this correct for all OS/PROC specific flags? */
6007 elf_section_flags (osec) |= (elf_section_flags (isec)
6008 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a
L
6009
6010 /* Set things up for objcopy and relocatable link. The output
6011 SHT_GROUP section will have its elf_next_in_group pointing back
6012 to the input group members. Ignore linker created group section.
6013 See elfNN_ia64_object_p in elfxx-ia64.c. */
ccd2ec6a
L
6014 if (need_group)
6015 {
6016 if (elf_sec_group (isec) == NULL
6017 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6018 {
6019 if (elf_section_flags (isec) & SHF_GROUP)
6020 elf_section_flags (osec) |= SHF_GROUP;
6021 elf_next_in_group (osec) = elf_next_in_group (isec);
6022 elf_group_name (osec) = elf_group_name (isec);
6023 }
6024 }
6025
6026 ihdr = &elf_section_data (isec)->this_hdr;
6027
6028 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6029 don't use the output section of the linked-to section since it
6030 may be NULL at this point. */
6031 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6032 {
6033 ohdr = &elf_section_data (osec)->this_hdr;
6034 ohdr->sh_flags |= SHF_LINK_ORDER;
6035 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6036 }
6037
6038 osec->use_rela_p = isec->use_rela_p;
6039
6040 return TRUE;
6041}
6042
252b5132
RH
6043/* Copy private section information. This copies over the entsize
6044 field, and sometimes the info field. */
6045
b34976b6 6046bfd_boolean
217aa764
AM
6047_bfd_elf_copy_private_section_data (bfd *ibfd,
6048 asection *isec,
6049 bfd *obfd,
6050 asection *osec)
252b5132
RH
6051{
6052 Elf_Internal_Shdr *ihdr, *ohdr;
6053
6054 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6055 || obfd->xvec->flavour != bfd_target_elf_flavour)
b34976b6 6056 return TRUE;
252b5132 6057
252b5132
RH
6058 ihdr = &elf_section_data (isec)->this_hdr;
6059 ohdr = &elf_section_data (osec)->this_hdr;
6060
6061 ohdr->sh_entsize = ihdr->sh_entsize;
6062
6063 if (ihdr->sh_type == SHT_SYMTAB
6064 || ihdr->sh_type == SHT_DYNSYM
6065 || ihdr->sh_type == SHT_GNU_verneed
6066 || ihdr->sh_type == SHT_GNU_verdef)
6067 ohdr->sh_info = ihdr->sh_info;
6068
ccd2ec6a
L
6069 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6070 NULL);
252b5132
RH
6071}
6072
80fccad2
BW
6073/* Copy private header information. */
6074
6075bfd_boolean
6076_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6077{
30288845
AM
6078 asection *isec;
6079
80fccad2
BW
6080 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6081 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6082 return TRUE;
6083
6084 /* Copy over private BFD data if it has not already been copied.
6085 This must be done here, rather than in the copy_private_bfd_data
6086 entry point, because the latter is called after the section
6087 contents have been set, which means that the program headers have
6088 already been worked out. */
6089 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6090 {
6091 if (! copy_private_bfd_data (ibfd, obfd))
6092 return FALSE;
6093 }
6094
30288845
AM
6095 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6096 but this might be wrong if we deleted the group section. */
6097 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6098 if (elf_section_type (isec) == SHT_GROUP
6099 && isec->output_section == NULL)
6100 {
6101 asection *first = elf_next_in_group (isec);
6102 asection *s = first;
6103 while (s != NULL)
6104 {
6105 if (s->output_section != NULL)
6106 {
6107 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6108 elf_group_name (s->output_section) = NULL;
6109 }
6110 s = elf_next_in_group (s);
6111 if (s == first)
6112 break;
6113 }
6114 }
6115
80fccad2
BW
6116 return TRUE;
6117}
6118
252b5132
RH
6119/* Copy private symbol information. If this symbol is in a section
6120 which we did not map into a BFD section, try to map the section
6121 index correctly. We use special macro definitions for the mapped
6122 section indices; these definitions are interpreted by the
6123 swap_out_syms function. */
6124
9ad5cbcf
AM
6125#define MAP_ONESYMTAB (SHN_HIOS + 1)
6126#define MAP_DYNSYMTAB (SHN_HIOS + 2)
6127#define MAP_STRTAB (SHN_HIOS + 3)
6128#define MAP_SHSTRTAB (SHN_HIOS + 4)
6129#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 6130
b34976b6 6131bfd_boolean
217aa764
AM
6132_bfd_elf_copy_private_symbol_data (bfd *ibfd,
6133 asymbol *isymarg,
6134 bfd *obfd,
6135 asymbol *osymarg)
252b5132
RH
6136{
6137 elf_symbol_type *isym, *osym;
6138
6139 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6140 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 6141 return TRUE;
252b5132
RH
6142
6143 isym = elf_symbol_from (ibfd, isymarg);
6144 osym = elf_symbol_from (obfd, osymarg);
6145
6146 if (isym != NULL
8424d8f5 6147 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
6148 && osym != NULL
6149 && bfd_is_abs_section (isym->symbol.section))
6150 {
6151 unsigned int shndx;
6152
6153 shndx = isym->internal_elf_sym.st_shndx;
6154 if (shndx == elf_onesymtab (ibfd))
6155 shndx = MAP_ONESYMTAB;
6156 else if (shndx == elf_dynsymtab (ibfd))
6157 shndx = MAP_DYNSYMTAB;
6158 else if (shndx == elf_tdata (ibfd)->strtab_section)
6159 shndx = MAP_STRTAB;
6160 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6161 shndx = MAP_SHSTRTAB;
9ad5cbcf
AM
6162 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6163 shndx = MAP_SYM_SHNDX;
252b5132
RH
6164 osym->internal_elf_sym.st_shndx = shndx;
6165 }
6166
b34976b6 6167 return TRUE;
252b5132
RH
6168}
6169
6170/* Swap out the symbols. */
6171
b34976b6 6172static bfd_boolean
217aa764
AM
6173swap_out_syms (bfd *abfd,
6174 struct bfd_strtab_hash **sttp,
6175 int relocatable_p)
252b5132 6176{
9c5bfbb7 6177 const struct elf_backend_data *bed;
079e9a2f
AM
6178 int symcount;
6179 asymbol **syms;
6180 struct bfd_strtab_hash *stt;
6181 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 6182 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 6183 Elf_Internal_Shdr *symstrtab_hdr;
f075ee0c
AM
6184 bfd_byte *outbound_syms;
6185 bfd_byte *outbound_shndx;
079e9a2f
AM
6186 int idx;
6187 bfd_size_type amt;
174fd7f9 6188 bfd_boolean name_local_sections;
252b5132
RH
6189
6190 if (!elf_map_symbols (abfd))
b34976b6 6191 return FALSE;
252b5132 6192
c044fabd 6193 /* Dump out the symtabs. */
079e9a2f
AM
6194 stt = _bfd_elf_stringtab_init ();
6195 if (stt == NULL)
b34976b6 6196 return FALSE;
252b5132 6197
079e9a2f
AM
6198 bed = get_elf_backend_data (abfd);
6199 symcount = bfd_get_symcount (abfd);
6200 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6201 symtab_hdr->sh_type = SHT_SYMTAB;
6202 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6203 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6204 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
72de5009 6205 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
6206
6207 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6208 symstrtab_hdr->sh_type = SHT_STRTAB;
6209
d0fb9a8d 6210 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
079e9a2f 6211 if (outbound_syms == NULL)
5ed6aba4
NC
6212 {
6213 _bfd_stringtab_free (stt);
6214 return FALSE;
6215 }
217aa764 6216 symtab_hdr->contents = outbound_syms;
252b5132 6217
9ad5cbcf
AM
6218 outbound_shndx = NULL;
6219 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6220 if (symtab_shndx_hdr->sh_name != 0)
6221 {
6222 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
d0fb9a8d
JJ
6223 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6224 sizeof (Elf_External_Sym_Shndx));
9ad5cbcf 6225 if (outbound_shndx == NULL)
5ed6aba4
NC
6226 {
6227 _bfd_stringtab_free (stt);
6228 return FALSE;
6229 }
6230
9ad5cbcf
AM
6231 symtab_shndx_hdr->contents = outbound_shndx;
6232 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6233 symtab_shndx_hdr->sh_size = amt;
6234 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6235 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6236 }
6237
589e6347 6238 /* Now generate the data (for "contents"). */
079e9a2f
AM
6239 {
6240 /* Fill in zeroth symbol and swap it out. */
6241 Elf_Internal_Sym sym;
6242 sym.st_name = 0;
6243 sym.st_value = 0;
6244 sym.st_size = 0;
6245 sym.st_info = 0;
6246 sym.st_other = 0;
6247 sym.st_shndx = SHN_UNDEF;
9ad5cbcf 6248 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 6249 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
6250 if (outbound_shndx != NULL)
6251 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 6252 }
252b5132 6253
174fd7f9
RS
6254 name_local_sections
6255 = (bed->elf_backend_name_local_section_symbols
6256 && bed->elf_backend_name_local_section_symbols (abfd));
6257
079e9a2f
AM
6258 syms = bfd_get_outsymbols (abfd);
6259 for (idx = 0; idx < symcount; idx++)
252b5132 6260 {
252b5132 6261 Elf_Internal_Sym sym;
079e9a2f
AM
6262 bfd_vma value = syms[idx]->value;
6263 elf_symbol_type *type_ptr;
6264 flagword flags = syms[idx]->flags;
6265 int type;
252b5132 6266
174fd7f9
RS
6267 if (!name_local_sections
6268 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
6269 {
6270 /* Local section symbols have no name. */
6271 sym.st_name = 0;
6272 }
6273 else
6274 {
6275 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6276 syms[idx]->name,
b34976b6 6277 TRUE, FALSE);
079e9a2f 6278 if (sym.st_name == (unsigned long) -1)
5ed6aba4
NC
6279 {
6280 _bfd_stringtab_free (stt);
6281 return FALSE;
6282 }
079e9a2f 6283 }
252b5132 6284
079e9a2f 6285 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 6286
079e9a2f
AM
6287 if ((flags & BSF_SECTION_SYM) == 0
6288 && bfd_is_com_section (syms[idx]->section))
6289 {
6290 /* ELF common symbols put the alignment into the `value' field,
6291 and the size into the `size' field. This is backwards from
6292 how BFD handles it, so reverse it here. */
6293 sym.st_size = value;
6294 if (type_ptr == NULL
6295 || type_ptr->internal_elf_sym.st_value == 0)
6296 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6297 else
6298 sym.st_value = type_ptr->internal_elf_sym.st_value;
6299 sym.st_shndx = _bfd_elf_section_from_bfd_section
6300 (abfd, syms[idx]->section);
6301 }
6302 else
6303 {
6304 asection *sec = syms[idx]->section;
cb33740c 6305 unsigned int shndx;
252b5132 6306
079e9a2f
AM
6307 if (sec->output_section)
6308 {
6309 value += sec->output_offset;
6310 sec = sec->output_section;
6311 }
589e6347 6312
079e9a2f
AM
6313 /* Don't add in the section vma for relocatable output. */
6314 if (! relocatable_p)
6315 value += sec->vma;
6316 sym.st_value = value;
6317 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6318
6319 if (bfd_is_abs_section (sec)
6320 && type_ptr != NULL
6321 && type_ptr->internal_elf_sym.st_shndx != 0)
6322 {
6323 /* This symbol is in a real ELF section which we did
6324 not create as a BFD section. Undo the mapping done
6325 by copy_private_symbol_data. */
6326 shndx = type_ptr->internal_elf_sym.st_shndx;
6327 switch (shndx)
6328 {
6329 case MAP_ONESYMTAB:
6330 shndx = elf_onesymtab (abfd);
6331 break;
6332 case MAP_DYNSYMTAB:
6333 shndx = elf_dynsymtab (abfd);
6334 break;
6335 case MAP_STRTAB:
6336 shndx = elf_tdata (abfd)->strtab_section;
6337 break;
6338 case MAP_SHSTRTAB:
6339 shndx = elf_tdata (abfd)->shstrtab_section;
6340 break;
9ad5cbcf
AM
6341 case MAP_SYM_SHNDX:
6342 shndx = elf_tdata (abfd)->symtab_shndx_section;
6343 break;
079e9a2f
AM
6344 default:
6345 break;
6346 }
6347 }
6348 else
6349 {
6350 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 6351
cb33740c 6352 if (shndx == SHN_BAD)
079e9a2f
AM
6353 {
6354 asection *sec2;
6355
6356 /* Writing this would be a hell of a lot easier if
6357 we had some decent documentation on bfd, and
6358 knew what to expect of the library, and what to
6359 demand of applications. For example, it
6360 appears that `objcopy' might not set the
6361 section of a symbol to be a section that is
6362 actually in the output file. */
6363 sec2 = bfd_get_section_by_name (abfd, sec->name);
589e6347
NC
6364 if (sec2 == NULL)
6365 {
6366 _bfd_error_handler (_("\
6367Unable to find equivalent output section for symbol '%s' from section '%s'"),
6368 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6369 sec->name);
811072d8 6370 bfd_set_error (bfd_error_invalid_operation);
5ed6aba4 6371 _bfd_stringtab_free (stt);
589e6347
NC
6372 return FALSE;
6373 }
811072d8 6374
079e9a2f 6375 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
cb33740c 6376 BFD_ASSERT (shndx != SHN_BAD);
079e9a2f
AM
6377 }
6378 }
252b5132 6379
079e9a2f
AM
6380 sym.st_shndx = shndx;
6381 }
252b5132 6382
13ae64f3
JJ
6383 if ((flags & BSF_THREAD_LOCAL) != 0)
6384 type = STT_TLS;
6385 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
6386 type = STT_FUNC;
6387 else if ((flags & BSF_OBJECT) != 0)
6388 type = STT_OBJECT;
d9352518
DB
6389 else if ((flags & BSF_RELC) != 0)
6390 type = STT_RELC;
6391 else if ((flags & BSF_SRELC) != 0)
6392 type = STT_SRELC;
079e9a2f
AM
6393 else
6394 type = STT_NOTYPE;
252b5132 6395
13ae64f3
JJ
6396 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6397 type = STT_TLS;
6398
589e6347 6399 /* Processor-specific types. */
079e9a2f
AM
6400 if (type_ptr != NULL
6401 && bed->elf_backend_get_symbol_type)
6402 type = ((*bed->elf_backend_get_symbol_type)
6403 (&type_ptr->internal_elf_sym, type));
252b5132 6404
079e9a2f
AM
6405 if (flags & BSF_SECTION_SYM)
6406 {
6407 if (flags & BSF_GLOBAL)
6408 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6409 else
6410 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6411 }
6412 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 6413 {
504b7d20 6414#ifdef USE_STT_COMMON
0a40daed
MK
6415 if (type == STT_OBJECT)
6416 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6417 else
6418#else
6419 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
504b7d20 6420#endif
0a40daed 6421 }
079e9a2f
AM
6422 else if (bfd_is_und_section (syms[idx]->section))
6423 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6424 ? STB_WEAK
6425 : STB_GLOBAL),
6426 type);
6427 else if (flags & BSF_FILE)
6428 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6429 else
6430 {
6431 int bind = STB_LOCAL;
252b5132 6432
079e9a2f
AM
6433 if (flags & BSF_LOCAL)
6434 bind = STB_LOCAL;
6435 else if (flags & BSF_WEAK)
6436 bind = STB_WEAK;
6437 else if (flags & BSF_GLOBAL)
6438 bind = STB_GLOBAL;
252b5132 6439
079e9a2f
AM
6440 sym.st_info = ELF_ST_INFO (bind, type);
6441 }
252b5132 6442
079e9a2f
AM
6443 if (type_ptr != NULL)
6444 sym.st_other = type_ptr->internal_elf_sym.st_other;
6445 else
6446 sym.st_other = 0;
252b5132 6447
9ad5cbcf 6448 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 6449 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
6450 if (outbound_shndx != NULL)
6451 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 6452 }
252b5132 6453
079e9a2f
AM
6454 *sttp = stt;
6455 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6456 symstrtab_hdr->sh_type = SHT_STRTAB;
252b5132 6457
079e9a2f
AM
6458 symstrtab_hdr->sh_flags = 0;
6459 symstrtab_hdr->sh_addr = 0;
6460 symstrtab_hdr->sh_entsize = 0;
6461 symstrtab_hdr->sh_link = 0;
6462 symstrtab_hdr->sh_info = 0;
6463 symstrtab_hdr->sh_addralign = 1;
252b5132 6464
b34976b6 6465 return TRUE;
252b5132
RH
6466}
6467
6468/* Return the number of bytes required to hold the symtab vector.
6469
6470 Note that we base it on the count plus 1, since we will null terminate
6471 the vector allocated based on this size. However, the ELF symbol table
6472 always has a dummy entry as symbol #0, so it ends up even. */
6473
6474long
217aa764 6475_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132
RH
6476{
6477 long symcount;
6478 long symtab_size;
6479 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6480
6481 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
6482 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6483 if (symcount > 0)
6484 symtab_size -= sizeof (asymbol *);
252b5132
RH
6485
6486 return symtab_size;
6487}
6488
6489long
217aa764 6490_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132
RH
6491{
6492 long symcount;
6493 long symtab_size;
6494 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6495
6496 if (elf_dynsymtab (abfd) == 0)
6497 {
6498 bfd_set_error (bfd_error_invalid_operation);
6499 return -1;
6500 }
6501
6502 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
6503 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6504 if (symcount > 0)
6505 symtab_size -= sizeof (asymbol *);
252b5132
RH
6506
6507 return symtab_size;
6508}
6509
6510long
217aa764
AM
6511_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6512 sec_ptr asect)
252b5132
RH
6513{
6514 return (asect->reloc_count + 1) * sizeof (arelent *);
6515}
6516
6517/* Canonicalize the relocs. */
6518
6519long
217aa764
AM
6520_bfd_elf_canonicalize_reloc (bfd *abfd,
6521 sec_ptr section,
6522 arelent **relptr,
6523 asymbol **symbols)
252b5132
RH
6524{
6525 arelent *tblptr;
6526 unsigned int i;
9c5bfbb7 6527 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6528
b34976b6 6529 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
252b5132
RH
6530 return -1;
6531
6532 tblptr = section->relocation;
6533 for (i = 0; i < section->reloc_count; i++)
6534 *relptr++ = tblptr++;
6535
6536 *relptr = NULL;
6537
6538 return section->reloc_count;
6539}
6540
6541long
6cee3f79 6542_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 6543{
9c5bfbb7 6544 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 6545 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
252b5132
RH
6546
6547 if (symcount >= 0)
6548 bfd_get_symcount (abfd) = symcount;
6549 return symcount;
6550}
6551
6552long
217aa764
AM
6553_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6554 asymbol **allocation)
252b5132 6555{
9c5bfbb7 6556 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 6557 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
1f70368c
DJ
6558
6559 if (symcount >= 0)
6560 bfd_get_dynamic_symcount (abfd) = symcount;
6561 return symcount;
252b5132
RH
6562}
6563
8615f3f2
AM
6564/* Return the size required for the dynamic reloc entries. Any loadable
6565 section that was actually installed in the BFD, and has type SHT_REL
6566 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6567 dynamic reloc section. */
252b5132
RH
6568
6569long
217aa764 6570_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132
RH
6571{
6572 long ret;
6573 asection *s;
6574
6575 if (elf_dynsymtab (abfd) == 0)
6576 {
6577 bfd_set_error (bfd_error_invalid_operation);
6578 return -1;
6579 }
6580
6581 ret = sizeof (arelent *);
6582 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 6583 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
6584 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6585 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
eea6121a 6586 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
252b5132
RH
6587 * sizeof (arelent *));
6588
6589 return ret;
6590}
6591
8615f3f2
AM
6592/* Canonicalize the dynamic relocation entries. Note that we return the
6593 dynamic relocations as a single block, although they are actually
6594 associated with particular sections; the interface, which was
6595 designed for SunOS style shared libraries, expects that there is only
6596 one set of dynamic relocs. Any loadable section that was actually
6597 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6598 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
6599
6600long
217aa764
AM
6601_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6602 arelent **storage,
6603 asymbol **syms)
252b5132 6604{
217aa764 6605 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
252b5132
RH
6606 asection *s;
6607 long ret;
6608
6609 if (elf_dynsymtab (abfd) == 0)
6610 {
6611 bfd_set_error (bfd_error_invalid_operation);
6612 return -1;
6613 }
6614
6615 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6616 ret = 0;
6617 for (s = abfd->sections; s != NULL; s = s->next)
6618 {
266b05cf 6619 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
6620 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6621 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6622 {
6623 arelent *p;
6624 long count, i;
6625
b34976b6 6626 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
252b5132 6627 return -1;
eea6121a 6628 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
6629 p = s->relocation;
6630 for (i = 0; i < count; i++)
6631 *storage++ = p++;
6632 ret += count;
6633 }
6634 }
6635
6636 *storage = NULL;
6637
6638 return ret;
6639}
6640\f
6641/* Read in the version information. */
6642
b34976b6 6643bfd_boolean
fc0e6df6 6644_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
252b5132
RH
6645{
6646 bfd_byte *contents = NULL;
fc0e6df6
PB
6647 unsigned int freeidx = 0;
6648
6649 if (elf_dynverref (abfd) != 0)
6650 {
6651 Elf_Internal_Shdr *hdr;
6652 Elf_External_Verneed *everneed;
6653 Elf_Internal_Verneed *iverneed;
6654 unsigned int i;
d0fb9a8d 6655 bfd_byte *contents_end;
fc0e6df6
PB
6656
6657 hdr = &elf_tdata (abfd)->dynverref_hdr;
6658
d0fb9a8d
JJ
6659 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6660 sizeof (Elf_Internal_Verneed));
fc0e6df6
PB
6661 if (elf_tdata (abfd)->verref == NULL)
6662 goto error_return;
6663
6664 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6665
6666 contents = bfd_malloc (hdr->sh_size);
6667 if (contents == NULL)
d0fb9a8d
JJ
6668 {
6669error_return_verref:
6670 elf_tdata (abfd)->verref = NULL;
6671 elf_tdata (abfd)->cverrefs = 0;
6672 goto error_return;
6673 }
fc0e6df6
PB
6674 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6675 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
d0fb9a8d 6676 goto error_return_verref;
fc0e6df6 6677
d0fb9a8d
JJ
6678 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6679 goto error_return_verref;
6680
6681 BFD_ASSERT (sizeof (Elf_External_Verneed)
6682 == sizeof (Elf_External_Vernaux));
6683 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
6684 everneed = (Elf_External_Verneed *) contents;
6685 iverneed = elf_tdata (abfd)->verref;
6686 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6687 {
6688 Elf_External_Vernaux *evernaux;
6689 Elf_Internal_Vernaux *ivernaux;
6690 unsigned int j;
6691
6692 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6693
6694 iverneed->vn_bfd = abfd;
6695
6696 iverneed->vn_filename =
6697 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6698 iverneed->vn_file);
6699 if (iverneed->vn_filename == NULL)
d0fb9a8d 6700 goto error_return_verref;
fc0e6df6 6701
d0fb9a8d
JJ
6702 if (iverneed->vn_cnt == 0)
6703 iverneed->vn_auxptr = NULL;
6704 else
6705 {
6706 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6707 sizeof (Elf_Internal_Vernaux));
6708 if (iverneed->vn_auxptr == NULL)
6709 goto error_return_verref;
6710 }
6711
6712 if (iverneed->vn_aux
6713 > (size_t) (contents_end - (bfd_byte *) everneed))
6714 goto error_return_verref;
fc0e6df6
PB
6715
6716 evernaux = ((Elf_External_Vernaux *)
6717 ((bfd_byte *) everneed + iverneed->vn_aux));
6718 ivernaux = iverneed->vn_auxptr;
6719 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6720 {
6721 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6722
6723 ivernaux->vna_nodename =
6724 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6725 ivernaux->vna_name);
6726 if (ivernaux->vna_nodename == NULL)
d0fb9a8d 6727 goto error_return_verref;
fc0e6df6
PB
6728
6729 if (j + 1 < iverneed->vn_cnt)
6730 ivernaux->vna_nextptr = ivernaux + 1;
6731 else
6732 ivernaux->vna_nextptr = NULL;
6733
d0fb9a8d
JJ
6734 if (ivernaux->vna_next
6735 > (size_t) (contents_end - (bfd_byte *) evernaux))
6736 goto error_return_verref;
6737
fc0e6df6
PB
6738 evernaux = ((Elf_External_Vernaux *)
6739 ((bfd_byte *) evernaux + ivernaux->vna_next));
6740
6741 if (ivernaux->vna_other > freeidx)
6742 freeidx = ivernaux->vna_other;
6743 }
6744
6745 if (i + 1 < hdr->sh_info)
6746 iverneed->vn_nextref = iverneed + 1;
6747 else
6748 iverneed->vn_nextref = NULL;
6749
d0fb9a8d
JJ
6750 if (iverneed->vn_next
6751 > (size_t) (contents_end - (bfd_byte *) everneed))
6752 goto error_return_verref;
6753
fc0e6df6
PB
6754 everneed = ((Elf_External_Verneed *)
6755 ((bfd_byte *) everneed + iverneed->vn_next));
6756 }
6757
6758 free (contents);
6759 contents = NULL;
6760 }
252b5132
RH
6761
6762 if (elf_dynverdef (abfd) != 0)
6763 {
6764 Elf_Internal_Shdr *hdr;
6765 Elf_External_Verdef *everdef;
6766 Elf_Internal_Verdef *iverdef;
f631889e
UD
6767 Elf_Internal_Verdef *iverdefarr;
6768 Elf_Internal_Verdef iverdefmem;
252b5132 6769 unsigned int i;
062e2358 6770 unsigned int maxidx;
d0fb9a8d 6771 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
6772
6773 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6774
217aa764 6775 contents = bfd_malloc (hdr->sh_size);
252b5132
RH
6776 if (contents == NULL)
6777 goto error_return;
6778 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
217aa764 6779 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
6780 goto error_return;
6781
d0fb9a8d
JJ
6782 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6783 goto error_return;
6784
6785 BFD_ASSERT (sizeof (Elf_External_Verdef)
6786 >= sizeof (Elf_External_Verdaux));
6787 contents_end_def = contents + hdr->sh_size
6788 - sizeof (Elf_External_Verdef);
6789 contents_end_aux = contents + hdr->sh_size
6790 - sizeof (Elf_External_Verdaux);
6791
f631889e
UD
6792 /* We know the number of entries in the section but not the maximum
6793 index. Therefore we have to run through all entries and find
6794 the maximum. */
252b5132 6795 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
6796 maxidx = 0;
6797 for (i = 0; i < hdr->sh_info; ++i)
6798 {
6799 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6800
062e2358
AM
6801 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6802 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 6803
d0fb9a8d
JJ
6804 if (iverdefmem.vd_next
6805 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6806 goto error_return;
6807
f631889e
UD
6808 everdef = ((Elf_External_Verdef *)
6809 ((bfd_byte *) everdef + iverdefmem.vd_next));
6810 }
6811
fc0e6df6
PB
6812 if (default_imported_symver)
6813 {
6814 if (freeidx > maxidx)
6815 maxidx = ++freeidx;
6816 else
6817 freeidx = ++maxidx;
6818 }
d0fb9a8d
JJ
6819 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6820 sizeof (Elf_Internal_Verdef));
f631889e
UD
6821 if (elf_tdata (abfd)->verdef == NULL)
6822 goto error_return;
6823
6824 elf_tdata (abfd)->cverdefs = maxidx;
6825
6826 everdef = (Elf_External_Verdef *) contents;
6827 iverdefarr = elf_tdata (abfd)->verdef;
6828 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
6829 {
6830 Elf_External_Verdaux *everdaux;
6831 Elf_Internal_Verdaux *iverdaux;
6832 unsigned int j;
6833
f631889e
UD
6834 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6835
d0fb9a8d
JJ
6836 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6837 {
6838error_return_verdef:
6839 elf_tdata (abfd)->verdef = NULL;
6840 elf_tdata (abfd)->cverdefs = 0;
6841 goto error_return;
6842 }
6843
f631889e
UD
6844 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6845 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
252b5132
RH
6846
6847 iverdef->vd_bfd = abfd;
6848
d0fb9a8d
JJ
6849 if (iverdef->vd_cnt == 0)
6850 iverdef->vd_auxptr = NULL;
6851 else
6852 {
6853 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6854 sizeof (Elf_Internal_Verdaux));
6855 if (iverdef->vd_auxptr == NULL)
6856 goto error_return_verdef;
6857 }
6858
6859 if (iverdef->vd_aux
6860 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6861 goto error_return_verdef;
252b5132
RH
6862
6863 everdaux = ((Elf_External_Verdaux *)
6864 ((bfd_byte *) everdef + iverdef->vd_aux));
6865 iverdaux = iverdef->vd_auxptr;
6866 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6867 {
6868 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6869
6870 iverdaux->vda_nodename =
6871 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6872 iverdaux->vda_name);
6873 if (iverdaux->vda_nodename == NULL)
d0fb9a8d 6874 goto error_return_verdef;
252b5132
RH
6875
6876 if (j + 1 < iverdef->vd_cnt)
6877 iverdaux->vda_nextptr = iverdaux + 1;
6878 else
6879 iverdaux->vda_nextptr = NULL;
6880
d0fb9a8d
JJ
6881 if (iverdaux->vda_next
6882 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6883 goto error_return_verdef;
6884
252b5132
RH
6885 everdaux = ((Elf_External_Verdaux *)
6886 ((bfd_byte *) everdaux + iverdaux->vda_next));
6887 }
6888
d0fb9a8d
JJ
6889 if (iverdef->vd_cnt)
6890 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 6891
d0fb9a8d 6892 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132
RH
6893 iverdef->vd_nextdef = iverdef + 1;
6894 else
6895 iverdef->vd_nextdef = NULL;
6896
6897 everdef = ((Elf_External_Verdef *)
6898 ((bfd_byte *) everdef + iverdef->vd_next));
6899 }
6900
6901 free (contents);
6902 contents = NULL;
6903 }
fc0e6df6 6904 else if (default_imported_symver)
252b5132 6905 {
fc0e6df6
PB
6906 if (freeidx < 3)
6907 freeidx = 3;
6908 else
6909 freeidx++;
252b5132 6910
d0fb9a8d
JJ
6911 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6912 sizeof (Elf_Internal_Verdef));
fc0e6df6 6913 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
6914 goto error_return;
6915
fc0e6df6
PB
6916 elf_tdata (abfd)->cverdefs = freeidx;
6917 }
252b5132 6918
fc0e6df6
PB
6919 /* Create a default version based on the soname. */
6920 if (default_imported_symver)
6921 {
6922 Elf_Internal_Verdef *iverdef;
6923 Elf_Internal_Verdaux *iverdaux;
252b5132 6924
fc0e6df6 6925 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
252b5132 6926
fc0e6df6
PB
6927 iverdef->vd_version = VER_DEF_CURRENT;
6928 iverdef->vd_flags = 0;
6929 iverdef->vd_ndx = freeidx;
6930 iverdef->vd_cnt = 1;
252b5132 6931
fc0e6df6 6932 iverdef->vd_bfd = abfd;
252b5132 6933
fc0e6df6
PB
6934 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6935 if (iverdef->vd_nodename == NULL)
d0fb9a8d 6936 goto error_return_verdef;
fc0e6df6 6937 iverdef->vd_nextdef = NULL;
d0fb9a8d
JJ
6938 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6939 if (iverdef->vd_auxptr == NULL)
6940 goto error_return_verdef;
252b5132 6941
fc0e6df6
PB
6942 iverdaux = iverdef->vd_auxptr;
6943 iverdaux->vda_nodename = iverdef->vd_nodename;
6944 iverdaux->vda_nextptr = NULL;
252b5132
RH
6945 }
6946
b34976b6 6947 return TRUE;
252b5132
RH
6948
6949 error_return:
5ed6aba4 6950 if (contents != NULL)
252b5132 6951 free (contents);
b34976b6 6952 return FALSE;
252b5132
RH
6953}
6954\f
6955asymbol *
217aa764 6956_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
6957{
6958 elf_symbol_type *newsym;
dc810e39 6959 bfd_size_type amt = sizeof (elf_symbol_type);
252b5132 6960
217aa764 6961 newsym = bfd_zalloc (abfd, amt);
252b5132
RH
6962 if (!newsym)
6963 return NULL;
6964 else
6965 {
6966 newsym->symbol.the_bfd = abfd;
6967 return &newsym->symbol;
6968 }
6969}
6970
6971void
217aa764
AM
6972_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6973 asymbol *symbol,
6974 symbol_info *ret)
252b5132
RH
6975{
6976 bfd_symbol_info (symbol, ret);
6977}
6978
6979/* Return whether a symbol name implies a local symbol. Most targets
6980 use this function for the is_local_label_name entry point, but some
6981 override it. */
6982
b34976b6 6983bfd_boolean
217aa764
AM
6984_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6985 const char *name)
252b5132
RH
6986{
6987 /* Normal local symbols start with ``.L''. */
6988 if (name[0] == '.' && name[1] == 'L')
b34976b6 6989 return TRUE;
252b5132
RH
6990
6991 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6992 DWARF debugging symbols starting with ``..''. */
6993 if (name[0] == '.' && name[1] == '.')
b34976b6 6994 return TRUE;
252b5132
RH
6995
6996 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6997 emitting DWARF debugging output. I suspect this is actually a
6998 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6999 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7000 underscore to be emitted on some ELF targets). For ease of use,
7001 we treat such symbols as local. */
7002 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
b34976b6 7003 return TRUE;
252b5132 7004
b34976b6 7005 return FALSE;
252b5132
RH
7006}
7007
7008alent *
217aa764
AM
7009_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7010 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
7011{
7012 abort ();
7013 return NULL;
7014}
7015
b34976b6 7016bfd_boolean
217aa764
AM
7017_bfd_elf_set_arch_mach (bfd *abfd,
7018 enum bfd_architecture arch,
7019 unsigned long machine)
252b5132
RH
7020{
7021 /* If this isn't the right architecture for this backend, and this
7022 isn't the generic backend, fail. */
7023 if (arch != get_elf_backend_data (abfd)->arch
7024 && arch != bfd_arch_unknown
7025 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
b34976b6 7026 return FALSE;
252b5132
RH
7027
7028 return bfd_default_set_arch_mach (abfd, arch, machine);
7029}
7030
d1fad7c6
NC
7031/* Find the function to a particular section and offset,
7032 for error reporting. */
252b5132 7033
b34976b6 7034static bfd_boolean
217aa764
AM
7035elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
7036 asection *section,
7037 asymbol **symbols,
7038 bfd_vma offset,
7039 const char **filename_ptr,
7040 const char **functionname_ptr)
252b5132 7041{
252b5132 7042 const char *filename;
57426232 7043 asymbol *func, *file;
252b5132
RH
7044 bfd_vma low_func;
7045 asymbol **p;
57426232
JB
7046 /* ??? Given multiple file symbols, it is impossible to reliably
7047 choose the right file name for global symbols. File symbols are
7048 local symbols, and thus all file symbols must sort before any
7049 global symbols. The ELF spec may be interpreted to say that a
7050 file symbol must sort before other local symbols, but currently
7051 ld -r doesn't do this. So, for ld -r output, it is possible to
7052 make a better choice of file name for local symbols by ignoring
7053 file symbols appearing after a given local symbol. */
7054 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
252b5132 7055
252b5132
RH
7056 filename = NULL;
7057 func = NULL;
57426232 7058 file = NULL;
252b5132 7059 low_func = 0;
57426232 7060 state = nothing_seen;
252b5132
RH
7061
7062 for (p = symbols; *p != NULL; p++)
7063 {
7064 elf_symbol_type *q;
7065
7066 q = (elf_symbol_type *) *p;
7067
252b5132
RH
7068 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7069 {
7070 default:
7071 break;
7072 case STT_FILE:
57426232
JB
7073 file = &q->symbol;
7074 if (state == symbol_seen)
7075 state = file_after_symbol_seen;
7076 continue;
252b5132
RH
7077 case STT_NOTYPE:
7078 case STT_FUNC:
6b40fcba 7079 if (bfd_get_section (&q->symbol) == section
252b5132
RH
7080 && q->symbol.value >= low_func
7081 && q->symbol.value <= offset)
7082 {
7083 func = (asymbol *) q;
7084 low_func = q->symbol.value;
a1923858
AM
7085 filename = NULL;
7086 if (file != NULL
7087 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7088 || state != file_after_symbol_seen))
57426232 7089 filename = bfd_asymbol_name (file);
252b5132
RH
7090 }
7091 break;
7092 }
57426232
JB
7093 if (state == nothing_seen)
7094 state = symbol_seen;
252b5132
RH
7095 }
7096
7097 if (func == NULL)
b34976b6 7098 return FALSE;
252b5132 7099
d1fad7c6
NC
7100 if (filename_ptr)
7101 *filename_ptr = filename;
7102 if (functionname_ptr)
7103 *functionname_ptr = bfd_asymbol_name (func);
7104
b34976b6 7105 return TRUE;
d1fad7c6
NC
7106}
7107
7108/* Find the nearest line to a particular section and offset,
7109 for error reporting. */
7110
b34976b6 7111bfd_boolean
217aa764
AM
7112_bfd_elf_find_nearest_line (bfd *abfd,
7113 asection *section,
7114 asymbol **symbols,
7115 bfd_vma offset,
7116 const char **filename_ptr,
7117 const char **functionname_ptr,
7118 unsigned int *line_ptr)
d1fad7c6 7119{
b34976b6 7120 bfd_boolean found;
d1fad7c6
NC
7121
7122 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
7123 filename_ptr, functionname_ptr,
7124 line_ptr))
d1fad7c6
NC
7125 {
7126 if (!*functionname_ptr)
4e8a9624
AM
7127 elf_find_function (abfd, section, symbols, offset,
7128 *filename_ptr ? NULL : filename_ptr,
7129 functionname_ptr);
7130
b34976b6 7131 return TRUE;
d1fad7c6
NC
7132 }
7133
7134 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
7135 filename_ptr, functionname_ptr,
7136 line_ptr, 0,
7137 &elf_tdata (abfd)->dwarf2_find_line_info))
d1fad7c6
NC
7138 {
7139 if (!*functionname_ptr)
4e8a9624
AM
7140 elf_find_function (abfd, section, symbols, offset,
7141 *filename_ptr ? NULL : filename_ptr,
7142 functionname_ptr);
7143
b34976b6 7144 return TRUE;
d1fad7c6
NC
7145 }
7146
7147 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
7148 &found, filename_ptr,
7149 functionname_ptr, line_ptr,
7150 &elf_tdata (abfd)->line_info))
b34976b6 7151 return FALSE;
dc43ada5 7152 if (found && (*functionname_ptr || *line_ptr))
b34976b6 7153 return TRUE;
d1fad7c6
NC
7154
7155 if (symbols == NULL)
b34976b6 7156 return FALSE;
d1fad7c6
NC
7157
7158 if (! elf_find_function (abfd, section, symbols, offset,
4e8a9624 7159 filename_ptr, functionname_ptr))
b34976b6 7160 return FALSE;
d1fad7c6 7161
252b5132 7162 *line_ptr = 0;
b34976b6 7163 return TRUE;
252b5132
RH
7164}
7165
5420f73d
L
7166/* Find the line for a symbol. */
7167
7168bfd_boolean
7169_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7170 const char **filename_ptr, unsigned int *line_ptr)
7171{
7172 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7173 filename_ptr, line_ptr, 0,
7174 &elf_tdata (abfd)->dwarf2_find_line_info);
7175}
7176
4ab527b0
FF
7177/* After a call to bfd_find_nearest_line, successive calls to
7178 bfd_find_inliner_info can be used to get source information about
7179 each level of function inlining that terminated at the address
7180 passed to bfd_find_nearest_line. Currently this is only supported
7181 for DWARF2 with appropriate DWARF3 extensions. */
7182
7183bfd_boolean
7184_bfd_elf_find_inliner_info (bfd *abfd,
7185 const char **filename_ptr,
7186 const char **functionname_ptr,
7187 unsigned int *line_ptr)
7188{
7189 bfd_boolean found;
7190 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7191 functionname_ptr, line_ptr,
7192 & elf_tdata (abfd)->dwarf2_find_line_info);
7193 return found;
7194}
7195
252b5132 7196int
a6b96beb 7197_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 7198{
8ded5a0f
AM
7199 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7200 int ret = bed->s->sizeof_ehdr;
252b5132 7201
a6b96beb 7202 if (!info->relocatable)
8ded5a0f 7203 {
62d7a5f6 7204 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
8ded5a0f 7205
62d7a5f6
AM
7206 if (phdr_size == (bfd_size_type) -1)
7207 {
7208 struct elf_segment_map *m;
7209
7210 phdr_size = 0;
7211 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7212 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 7213
62d7a5f6
AM
7214 if (phdr_size == 0)
7215 phdr_size = get_program_header_size (abfd, info);
7216 }
8ded5a0f
AM
7217
7218 elf_tdata (abfd)->program_header_size = phdr_size;
7219 ret += phdr_size;
7220 }
7221
252b5132
RH
7222 return ret;
7223}
7224
b34976b6 7225bfd_boolean
217aa764
AM
7226_bfd_elf_set_section_contents (bfd *abfd,
7227 sec_ptr section,
0f867abe 7228 const void *location,
217aa764
AM
7229 file_ptr offset,
7230 bfd_size_type count)
252b5132
RH
7231{
7232 Elf_Internal_Shdr *hdr;
dc810e39 7233 bfd_signed_vma pos;
252b5132
RH
7234
7235 if (! abfd->output_has_begun
217aa764 7236 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 7237 return FALSE;
252b5132
RH
7238
7239 hdr = &elf_section_data (section)->this_hdr;
dc810e39
AM
7240 pos = hdr->sh_offset + offset;
7241 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7242 || bfd_bwrite (location, count, abfd) != count)
b34976b6 7243 return FALSE;
252b5132 7244
b34976b6 7245 return TRUE;
252b5132
RH
7246}
7247
7248void
217aa764
AM
7249_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7250 arelent *cache_ptr ATTRIBUTE_UNUSED,
7251 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
7252{
7253 abort ();
7254}
7255
252b5132
RH
7256/* Try to convert a non-ELF reloc into an ELF one. */
7257
b34976b6 7258bfd_boolean
217aa764 7259_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 7260{
c044fabd 7261 /* Check whether we really have an ELF howto. */
252b5132
RH
7262
7263 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7264 {
7265 bfd_reloc_code_real_type code;
7266 reloc_howto_type *howto;
7267
7268 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 7269 equivalent ELF reloc. */
252b5132
RH
7270
7271 if (areloc->howto->pc_relative)
7272 {
7273 switch (areloc->howto->bitsize)
7274 {
7275 case 8:
7276 code = BFD_RELOC_8_PCREL;
7277 break;
7278 case 12:
7279 code = BFD_RELOC_12_PCREL;
7280 break;
7281 case 16:
7282 code = BFD_RELOC_16_PCREL;
7283 break;
7284 case 24:
7285 code = BFD_RELOC_24_PCREL;
7286 break;
7287 case 32:
7288 code = BFD_RELOC_32_PCREL;
7289 break;
7290 case 64:
7291 code = BFD_RELOC_64_PCREL;
7292 break;
7293 default:
7294 goto fail;
7295 }
7296
7297 howto = bfd_reloc_type_lookup (abfd, code);
7298
7299 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7300 {
7301 if (howto->pcrel_offset)
7302 areloc->addend += areloc->address;
7303 else
7304 areloc->addend -= areloc->address; /* addend is unsigned!! */
7305 }
7306 }
7307 else
7308 {
7309 switch (areloc->howto->bitsize)
7310 {
7311 case 8:
7312 code = BFD_RELOC_8;
7313 break;
7314 case 14:
7315 code = BFD_RELOC_14;
7316 break;
7317 case 16:
7318 code = BFD_RELOC_16;
7319 break;
7320 case 26:
7321 code = BFD_RELOC_26;
7322 break;
7323 case 32:
7324 code = BFD_RELOC_32;
7325 break;
7326 case 64:
7327 code = BFD_RELOC_64;
7328 break;
7329 default:
7330 goto fail;
7331 }
7332
7333 howto = bfd_reloc_type_lookup (abfd, code);
7334 }
7335
7336 if (howto)
7337 areloc->howto = howto;
7338 else
7339 goto fail;
7340 }
7341
b34976b6 7342 return TRUE;
252b5132
RH
7343
7344 fail:
7345 (*_bfd_error_handler)
d003868e
AM
7346 (_("%B: unsupported relocation type %s"),
7347 abfd, areloc->howto->name);
252b5132 7348 bfd_set_error (bfd_error_bad_value);
b34976b6 7349 return FALSE;
252b5132
RH
7350}
7351
b34976b6 7352bfd_boolean
217aa764 7353_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132
RH
7354{
7355 if (bfd_get_format (abfd) == bfd_object)
7356 {
b25e3d87 7357 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 7358 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6f140a15 7359 _bfd_dwarf2_cleanup_debug_info (abfd);
252b5132
RH
7360 }
7361
7362 return _bfd_generic_close_and_cleanup (abfd);
7363}
7364
7365/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7366 in the relocation's offset. Thus we cannot allow any sort of sanity
7367 range-checking to interfere. There is nothing else to do in processing
7368 this reloc. */
7369
7370bfd_reloc_status_type
217aa764
AM
7371_bfd_elf_rel_vtable_reloc_fn
7372 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 7373 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
7374 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7375 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
7376{
7377 return bfd_reloc_ok;
7378}
252b5132
RH
7379\f
7380/* Elf core file support. Much of this only works on native
7381 toolchains, since we rely on knowing the
7382 machine-dependent procfs structure in order to pick
c044fabd 7383 out details about the corefile. */
252b5132
RH
7384
7385#ifdef HAVE_SYS_PROCFS_H
7386# include <sys/procfs.h>
7387#endif
7388
c044fabd 7389/* FIXME: this is kinda wrong, but it's what gdb wants. */
252b5132
RH
7390
7391static int
217aa764 7392elfcore_make_pid (bfd *abfd)
252b5132
RH
7393{
7394 return ((elf_tdata (abfd)->core_lwpid << 16)
7395 + (elf_tdata (abfd)->core_pid));
7396}
7397
252b5132
RH
7398/* If there isn't a section called NAME, make one, using
7399 data from SECT. Note, this function will generate a
7400 reference to NAME, so you shouldn't deallocate or
c044fabd 7401 overwrite it. */
252b5132 7402
b34976b6 7403static bfd_boolean
217aa764 7404elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 7405{
c044fabd 7406 asection *sect2;
252b5132
RH
7407
7408 if (bfd_get_section_by_name (abfd, name) != NULL)
b34976b6 7409 return TRUE;
252b5132 7410
117ed4f8 7411 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 7412 if (sect2 == NULL)
b34976b6 7413 return FALSE;
252b5132 7414
eea6121a 7415 sect2->size = sect->size;
252b5132 7416 sect2->filepos = sect->filepos;
252b5132 7417 sect2->alignment_power = sect->alignment_power;
b34976b6 7418 return TRUE;
252b5132
RH
7419}
7420
bb0082d6
AM
7421/* Create a pseudosection containing SIZE bytes at FILEPOS. This
7422 actually creates up to two pseudosections:
7423 - For the single-threaded case, a section named NAME, unless
7424 such a section already exists.
7425 - For the multi-threaded case, a section named "NAME/PID", where
7426 PID is elfcore_make_pid (abfd).
7427 Both pseudosections have identical contents. */
b34976b6 7428bfd_boolean
217aa764
AM
7429_bfd_elfcore_make_pseudosection (bfd *abfd,
7430 char *name,
7431 size_t size,
7432 ufile_ptr filepos)
bb0082d6
AM
7433{
7434 char buf[100];
7435 char *threaded_name;
d4c88bbb 7436 size_t len;
bb0082d6
AM
7437 asection *sect;
7438
7439 /* Build the section name. */
7440
7441 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 7442 len = strlen (buf) + 1;
217aa764 7443 threaded_name = bfd_alloc (abfd, len);
bb0082d6 7444 if (threaded_name == NULL)
b34976b6 7445 return FALSE;
d4c88bbb 7446 memcpy (threaded_name, buf, len);
bb0082d6 7447
117ed4f8
AM
7448 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7449 SEC_HAS_CONTENTS);
bb0082d6 7450 if (sect == NULL)
b34976b6 7451 return FALSE;
eea6121a 7452 sect->size = size;
bb0082d6 7453 sect->filepos = filepos;
bb0082d6
AM
7454 sect->alignment_power = 2;
7455
936e320b 7456 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
7457}
7458
252b5132 7459/* prstatus_t exists on:
4a938328 7460 solaris 2.5+
252b5132
RH
7461 linux 2.[01] + glibc
7462 unixware 4.2
7463*/
7464
7465#if defined (HAVE_PRSTATUS_T)
a7b97311 7466
b34976b6 7467static bfd_boolean
217aa764 7468elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 7469{
eea6121a 7470 size_t size;
7ee38065 7471 int offset;
252b5132 7472
4a938328
MS
7473 if (note->descsz == sizeof (prstatus_t))
7474 {
7475 prstatus_t prstat;
252b5132 7476
eea6121a 7477 size = sizeof (prstat.pr_reg);
7ee38065 7478 offset = offsetof (prstatus_t, pr_reg);
4a938328 7479 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 7480
fa49d224
NC
7481 /* Do not overwrite the core signal if it
7482 has already been set by another thread. */
7483 if (elf_tdata (abfd)->core_signal == 0)
7484 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328 7485 elf_tdata (abfd)->core_pid = prstat.pr_pid;
252b5132 7486
4a938328
MS
7487 /* pr_who exists on:
7488 solaris 2.5+
7489 unixware 4.2
7490 pr_who doesn't exist on:
7491 linux 2.[01]
7492 */
252b5132 7493#if defined (HAVE_PRSTATUS_T_PR_WHO)
4a938328 7494 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
252b5132 7495#endif
4a938328 7496 }
7ee38065 7497#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
7498 else if (note->descsz == sizeof (prstatus32_t))
7499 {
7500 /* 64-bit host, 32-bit corefile */
7501 prstatus32_t prstat;
7502
eea6121a 7503 size = sizeof (prstat.pr_reg);
7ee38065 7504 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
7505 memcpy (&prstat, note->descdata, sizeof (prstat));
7506
fa49d224
NC
7507 /* Do not overwrite the core signal if it
7508 has already been set by another thread. */
7509 if (elf_tdata (abfd)->core_signal == 0)
7510 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328
MS
7511 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7512
7513 /* pr_who exists on:
7514 solaris 2.5+
7515 unixware 4.2
7516 pr_who doesn't exist on:
7517 linux 2.[01]
7518 */
7ee38065 7519#if defined (HAVE_PRSTATUS32_T_PR_WHO)
4a938328
MS
7520 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7521#endif
7522 }
7ee38065 7523#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
7524 else
7525 {
7526 /* Fail - we don't know how to handle any other
7527 note size (ie. data object type). */
b34976b6 7528 return TRUE;
4a938328 7529 }
252b5132 7530
bb0082d6 7531 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 7532 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 7533 size, note->descpos + offset);
252b5132
RH
7534}
7535#endif /* defined (HAVE_PRSTATUS_T) */
7536
bb0082d6 7537/* Create a pseudosection containing the exact contents of NOTE. */
b34976b6 7538static bfd_boolean
217aa764
AM
7539elfcore_make_note_pseudosection (bfd *abfd,
7540 char *name,
7541 Elf_Internal_Note *note)
252b5132 7542{
936e320b
AM
7543 return _bfd_elfcore_make_pseudosection (abfd, name,
7544 note->descsz, note->descpos);
252b5132
RH
7545}
7546
ff08c6bb
JB
7547/* There isn't a consistent prfpregset_t across platforms,
7548 but it doesn't matter, because we don't have to pick this
c044fabd
KH
7549 data structure apart. */
7550
b34976b6 7551static bfd_boolean
217aa764 7552elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
7553{
7554 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7555}
7556
ff08c6bb 7557/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 7558 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 7559 literally. */
c044fabd 7560
b34976b6 7561static bfd_boolean
217aa764 7562elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
7563{
7564 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7565}
7566
97753bd5
AM
7567static bfd_boolean
7568elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7569{
7570 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7571}
7572
89eeb0bc
LM
7573static bfd_boolean
7574elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7575{
7576 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7577}
97753bd5 7578
252b5132 7579#if defined (HAVE_PRPSINFO_T)
4a938328 7580typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 7581#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
7582typedef prpsinfo32_t elfcore_psinfo32_t;
7583#endif
252b5132
RH
7584#endif
7585
7586#if defined (HAVE_PSINFO_T)
4a938328 7587typedef psinfo_t elfcore_psinfo_t;
7ee38065 7588#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
7589typedef psinfo32_t elfcore_psinfo32_t;
7590#endif
252b5132
RH
7591#endif
7592
252b5132
RH
7593/* return a malloc'ed copy of a string at START which is at
7594 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 7595 the copy will always have a terminating '\0'. */
252b5132 7596
936e320b 7597char *
217aa764 7598_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 7599{
dc810e39 7600 char *dups;
c044fabd 7601 char *end = memchr (start, '\0', max);
dc810e39 7602 size_t len;
252b5132
RH
7603
7604 if (end == NULL)
7605 len = max;
7606 else
7607 len = end - start;
7608
217aa764 7609 dups = bfd_alloc (abfd, len + 1);
dc810e39 7610 if (dups == NULL)
252b5132
RH
7611 return NULL;
7612
dc810e39
AM
7613 memcpy (dups, start, len);
7614 dups[len] = '\0';
252b5132 7615
dc810e39 7616 return dups;
252b5132
RH
7617}
7618
bb0082d6 7619#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
b34976b6 7620static bfd_boolean
217aa764 7621elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 7622{
4a938328
MS
7623 if (note->descsz == sizeof (elfcore_psinfo_t))
7624 {
7625 elfcore_psinfo_t psinfo;
252b5132 7626
7ee38065 7627 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 7628
4a938328 7629 elf_tdata (abfd)->core_program
936e320b
AM
7630 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7631 sizeof (psinfo.pr_fname));
252b5132 7632
4a938328 7633 elf_tdata (abfd)->core_command
936e320b
AM
7634 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7635 sizeof (psinfo.pr_psargs));
4a938328 7636 }
7ee38065 7637#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
7638 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7639 {
7640 /* 64-bit host, 32-bit corefile */
7641 elfcore_psinfo32_t psinfo;
7642
7ee38065 7643 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 7644
4a938328 7645 elf_tdata (abfd)->core_program
936e320b
AM
7646 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7647 sizeof (psinfo.pr_fname));
4a938328
MS
7648
7649 elf_tdata (abfd)->core_command
936e320b
AM
7650 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7651 sizeof (psinfo.pr_psargs));
4a938328
MS
7652 }
7653#endif
7654
7655 else
7656 {
7657 /* Fail - we don't know how to handle any other
7658 note size (ie. data object type). */
b34976b6 7659 return TRUE;
4a938328 7660 }
252b5132
RH
7661
7662 /* Note that for some reason, a spurious space is tacked
7663 onto the end of the args in some (at least one anyway)
c044fabd 7664 implementations, so strip it off if it exists. */
252b5132
RH
7665
7666 {
c044fabd 7667 char *command = elf_tdata (abfd)->core_command;
252b5132
RH
7668 int n = strlen (command);
7669
7670 if (0 < n && command[n - 1] == ' ')
7671 command[n - 1] = '\0';
7672 }
7673
b34976b6 7674 return TRUE;
252b5132
RH
7675}
7676#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7677
252b5132 7678#if defined (HAVE_PSTATUS_T)
b34976b6 7679static bfd_boolean
217aa764 7680elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 7681{
f572a39d
AM
7682 if (note->descsz == sizeof (pstatus_t)
7683#if defined (HAVE_PXSTATUS_T)
7684 || note->descsz == sizeof (pxstatus_t)
7685#endif
7686 )
4a938328
MS
7687 {
7688 pstatus_t pstat;
252b5132 7689
4a938328 7690 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 7691
4a938328
MS
7692 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7693 }
7ee38065 7694#if defined (HAVE_PSTATUS32_T)
4a938328
MS
7695 else if (note->descsz == sizeof (pstatus32_t))
7696 {
7697 /* 64-bit host, 32-bit corefile */
7698 pstatus32_t pstat;
252b5132 7699
4a938328 7700 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 7701
4a938328
MS
7702 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7703 }
7704#endif
252b5132
RH
7705 /* Could grab some more details from the "representative"
7706 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 7707 NT_LWPSTATUS note, presumably. */
252b5132 7708
b34976b6 7709 return TRUE;
252b5132
RH
7710}
7711#endif /* defined (HAVE_PSTATUS_T) */
7712
252b5132 7713#if defined (HAVE_LWPSTATUS_T)
b34976b6 7714static bfd_boolean
217aa764 7715elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
7716{
7717 lwpstatus_t lwpstat;
7718 char buf[100];
c044fabd 7719 char *name;
d4c88bbb 7720 size_t len;
c044fabd 7721 asection *sect;
252b5132 7722
f572a39d
AM
7723 if (note->descsz != sizeof (lwpstat)
7724#if defined (HAVE_LWPXSTATUS_T)
7725 && note->descsz != sizeof (lwpxstatus_t)
7726#endif
7727 )
b34976b6 7728 return TRUE;
252b5132
RH
7729
7730 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7731
7732 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7733 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7734
c044fabd 7735 /* Make a ".reg/999" section. */
252b5132
RH
7736
7737 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 7738 len = strlen (buf) + 1;
217aa764 7739 name = bfd_alloc (abfd, len);
252b5132 7740 if (name == NULL)
b34976b6 7741 return FALSE;
d4c88bbb 7742 memcpy (name, buf, len);
252b5132 7743
117ed4f8 7744 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 7745 if (sect == NULL)
b34976b6 7746 return FALSE;
252b5132
RH
7747
7748#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 7749 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
7750 sect->filepos = note->descpos
7751 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7752#endif
7753
7754#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 7755 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
7756 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7757#endif
7758
252b5132
RH
7759 sect->alignment_power = 2;
7760
7761 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 7762 return FALSE;
252b5132
RH
7763
7764 /* Make a ".reg2/999" section */
7765
7766 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 7767 len = strlen (buf) + 1;
217aa764 7768 name = bfd_alloc (abfd, len);
252b5132 7769 if (name == NULL)
b34976b6 7770 return FALSE;
d4c88bbb 7771 memcpy (name, buf, len);
252b5132 7772
117ed4f8 7773 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 7774 if (sect == NULL)
b34976b6 7775 return FALSE;
252b5132
RH
7776
7777#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 7778 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
7779 sect->filepos = note->descpos
7780 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7781#endif
7782
7783#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 7784 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
7785 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7786#endif
7787
252b5132
RH
7788 sect->alignment_power = 2;
7789
936e320b 7790 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
7791}
7792#endif /* defined (HAVE_LWPSTATUS_T) */
7793
b34976b6 7794static bfd_boolean
217aa764 7795elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
7796{
7797 char buf[30];
c044fabd 7798 char *name;
d4c88bbb 7799 size_t len;
c044fabd 7800 asection *sect;
4a6636fb
PA
7801 int type;
7802 int is_active_thread;
7803 bfd_vma base_addr;
16e9c715 7804
4a6636fb 7805 if (note->descsz < 728)
b34976b6 7806 return TRUE;
16e9c715 7807
4a6636fb
PA
7808 if (! CONST_STRNEQ (note->namedata, "win32"))
7809 return TRUE;
7810
7811 type = bfd_get_32 (abfd, note->descdata);
c044fabd 7812
4a6636fb 7813 switch (type)
16e9c715 7814 {
4a6636fb 7815 case 1 /* NOTE_INFO_PROCESS */:
16e9c715 7816 /* FIXME: need to add ->core_command. */
4a6636fb
PA
7817 /* process_info.pid */
7818 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7819 /* process_info.signal */
7820 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
c044fabd 7821 break;
16e9c715 7822
4a6636fb 7823 case 2 /* NOTE_INFO_THREAD */:
16e9c715 7824 /* Make a ".reg/999" section. */
4a6636fb
PA
7825 /* thread_info.tid */
7826 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
c044fabd 7827
d4c88bbb 7828 len = strlen (buf) + 1;
217aa764 7829 name = bfd_alloc (abfd, len);
16e9c715 7830 if (name == NULL)
b34976b6 7831 return FALSE;
c044fabd 7832
d4c88bbb 7833 memcpy (name, buf, len);
16e9c715 7834
117ed4f8 7835 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 7836 if (sect == NULL)
b34976b6 7837 return FALSE;
c044fabd 7838
4a6636fb
PA
7839 /* sizeof (thread_info.thread_context) */
7840 sect->size = 716;
7841 /* offsetof (thread_info.thread_context) */
7842 sect->filepos = note->descpos + 12;
16e9c715
NC
7843 sect->alignment_power = 2;
7844
4a6636fb
PA
7845 /* thread_info.is_active_thread */
7846 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7847
7848 if (is_active_thread)
16e9c715 7849 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 7850 return FALSE;
16e9c715
NC
7851 break;
7852
4a6636fb 7853 case 3 /* NOTE_INFO_MODULE */:
16e9c715 7854 /* Make a ".module/xxxxxxxx" section. */
4a6636fb
PA
7855 /* module_info.base_address */
7856 base_addr = bfd_get_32 (abfd, note->descdata + 4);
0af1713e 7857 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
c044fabd 7858
d4c88bbb 7859 len = strlen (buf) + 1;
217aa764 7860 name = bfd_alloc (abfd, len);
16e9c715 7861 if (name == NULL)
b34976b6 7862 return FALSE;
c044fabd 7863
d4c88bbb 7864 memcpy (name, buf, len);
252b5132 7865
117ed4f8 7866 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 7867
16e9c715 7868 if (sect == NULL)
b34976b6 7869 return FALSE;
c044fabd 7870
eea6121a 7871 sect->size = note->descsz;
16e9c715 7872 sect->filepos = note->descpos;
16e9c715
NC
7873 sect->alignment_power = 2;
7874 break;
7875
7876 default:
b34976b6 7877 return TRUE;
16e9c715
NC
7878 }
7879
b34976b6 7880 return TRUE;
16e9c715 7881}
252b5132 7882
b34976b6 7883static bfd_boolean
217aa764 7884elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 7885{
9c5bfbb7 7886 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 7887
252b5132
RH
7888 switch (note->type)
7889 {
7890 default:
b34976b6 7891 return TRUE;
252b5132 7892
252b5132 7893 case NT_PRSTATUS:
bb0082d6
AM
7894 if (bed->elf_backend_grok_prstatus)
7895 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
b34976b6 7896 return TRUE;
bb0082d6 7897#if defined (HAVE_PRSTATUS_T)
252b5132 7898 return elfcore_grok_prstatus (abfd, note);
bb0082d6 7899#else
b34976b6 7900 return TRUE;
252b5132
RH
7901#endif
7902
7903#if defined (HAVE_PSTATUS_T)
7904 case NT_PSTATUS:
7905 return elfcore_grok_pstatus (abfd, note);
7906#endif
7907
7908#if defined (HAVE_LWPSTATUS_T)
7909 case NT_LWPSTATUS:
7910 return elfcore_grok_lwpstatus (abfd, note);
7911#endif
7912
7913 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7914 return elfcore_grok_prfpreg (abfd, note);
7915
c044fabd 7916 case NT_WIN32PSTATUS:
16e9c715 7917 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 7918
c044fabd 7919 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
7920 if (note->namesz == 6
7921 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
7922 return elfcore_grok_prxfpreg (abfd, note);
7923 else
b34976b6 7924 return TRUE;
ff08c6bb 7925
97753bd5
AM
7926 case NT_PPC_VMX:
7927 if (note->namesz == 6
7928 && strcmp (note->namedata, "LINUX") == 0)
7929 return elfcore_grok_ppc_vmx (abfd, note);
7930 else
7931 return TRUE;
7932
89eeb0bc
LM
7933 case NT_PPC_VSX:
7934 if (note->namesz == 6
7935 && strcmp (note->namedata, "LINUX") == 0)
7936 return elfcore_grok_ppc_vsx (abfd, note);
7937 else
7938 return TRUE;
7939
252b5132
RH
7940 case NT_PRPSINFO:
7941 case NT_PSINFO:
bb0082d6
AM
7942 if (bed->elf_backend_grok_psinfo)
7943 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
b34976b6 7944 return TRUE;
bb0082d6 7945#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 7946 return elfcore_grok_psinfo (abfd, note);
bb0082d6 7947#else
b34976b6 7948 return TRUE;
252b5132 7949#endif
3333a7c3
RM
7950
7951 case NT_AUXV:
7952 {
117ed4f8
AM
7953 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7954 SEC_HAS_CONTENTS);
3333a7c3
RM
7955
7956 if (sect == NULL)
7957 return FALSE;
eea6121a 7958 sect->size = note->descsz;
3333a7c3 7959 sect->filepos = note->descpos;
3333a7c3
RM
7960 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7961
7962 return TRUE;
7963 }
252b5132
RH
7964 }
7965}
7966
718175fa
JK
7967static bfd_boolean
7968elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
7969{
7970 elf_tdata (abfd)->build_id_size = note->descsz;
7971 elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz);
7972 if (elf_tdata (abfd)->build_id == NULL)
7973 return FALSE;
7974
7975 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
7976
7977 return TRUE;
7978}
7979
7980static bfd_boolean
7981elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
7982{
7983 switch (note->type)
7984 {
7985 default:
7986 return TRUE;
7987
7988 case NT_GNU_BUILD_ID:
7989 return elfobj_grok_gnu_build_id (abfd, note);
7990 }
7991}
7992
b34976b6 7993static bfd_boolean
217aa764 7994elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
7995{
7996 char *cp;
7997
7998 cp = strchr (note->namedata, '@');
7999 if (cp != NULL)
8000 {
d2b64500 8001 *lwpidp = atoi(cp + 1);
b34976b6 8002 return TRUE;
50b2bdb7 8003 }
b34976b6 8004 return FALSE;
50b2bdb7
AM
8005}
8006
b34976b6 8007static bfd_boolean
217aa764 8008elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 8009{
50b2bdb7
AM
8010 /* Signal number at offset 0x08. */
8011 elf_tdata (abfd)->core_signal
8012 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8013
8014 /* Process ID at offset 0x50. */
8015 elf_tdata (abfd)->core_pid
8016 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8017
8018 /* Command name at 0x7c (max 32 bytes, including nul). */
8019 elf_tdata (abfd)->core_command
8020 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8021
7720ba9f
MK
8022 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8023 note);
50b2bdb7
AM
8024}
8025
b34976b6 8026static bfd_boolean
217aa764 8027elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
8028{
8029 int lwp;
8030
8031 if (elfcore_netbsd_get_lwpid (note, &lwp))
8032 elf_tdata (abfd)->core_lwpid = lwp;
8033
b4db1224 8034 if (note->type == NT_NETBSDCORE_PROCINFO)
50b2bdb7
AM
8035 {
8036 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
8037 find this note before any of the others, which is fine,
8038 since the kernel writes this note out first when it
8039 creates a core file. */
47d9a591 8040
50b2bdb7
AM
8041 return elfcore_grok_netbsd_procinfo (abfd, note);
8042 }
8043
b4db1224
JT
8044 /* As of Jan 2002 there are no other machine-independent notes
8045 defined for NetBSD core files. If the note type is less
8046 than the start of the machine-dependent note types, we don't
8047 understand it. */
47d9a591 8048
b4db1224 8049 if (note->type < NT_NETBSDCORE_FIRSTMACH)
b34976b6 8050 return TRUE;
50b2bdb7
AM
8051
8052
8053 switch (bfd_get_arch (abfd))
8054 {
08a40648
AM
8055 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8056 PT_GETFPREGS == mach+2. */
50b2bdb7
AM
8057
8058 case bfd_arch_alpha:
8059 case bfd_arch_sparc:
8060 switch (note->type)
08a40648
AM
8061 {
8062 case NT_NETBSDCORE_FIRSTMACH+0:
8063 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 8064
08a40648
AM
8065 case NT_NETBSDCORE_FIRSTMACH+2:
8066 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 8067
08a40648
AM
8068 default:
8069 return TRUE;
8070 }
50b2bdb7 8071
08a40648
AM
8072 /* On all other arch's, PT_GETREGS == mach+1 and
8073 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
8074
8075 default:
8076 switch (note->type)
08a40648
AM
8077 {
8078 case NT_NETBSDCORE_FIRSTMACH+1:
8079 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 8080
08a40648
AM
8081 case NT_NETBSDCORE_FIRSTMACH+3:
8082 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 8083
08a40648
AM
8084 default:
8085 return TRUE;
8086 }
50b2bdb7
AM
8087 }
8088 /* NOTREACHED */
8089}
8090
07c6e936 8091static bfd_boolean
d3fd4074 8092elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
8093{
8094 void *ddata = note->descdata;
8095 char buf[100];
8096 char *name;
8097 asection *sect;
f8843e87
AM
8098 short sig;
8099 unsigned flags;
07c6e936
NC
8100
8101 /* nto_procfs_status 'pid' field is at offset 0. */
8102 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8103
f8843e87
AM
8104 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8105 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8106
8107 /* nto_procfs_status 'flags' field is at offset 8. */
8108 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
8109
8110 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
8111 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8112 {
8113 elf_tdata (abfd)->core_signal = sig;
8114 elf_tdata (abfd)->core_lwpid = *tid;
8115 }
07c6e936 8116
f8843e87
AM
8117 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8118 do not come from signals so we make sure we set the current
8119 thread just in case. */
8120 if (flags & 0x00000080)
8121 elf_tdata (abfd)->core_lwpid = *tid;
07c6e936
NC
8122
8123 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 8124 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 8125
217aa764 8126 name = bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
8127 if (name == NULL)
8128 return FALSE;
8129 strcpy (name, buf);
8130
117ed4f8 8131 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
8132 if (sect == NULL)
8133 return FALSE;
8134
eea6121a 8135 sect->size = note->descsz;
07c6e936 8136 sect->filepos = note->descpos;
07c6e936
NC
8137 sect->alignment_power = 2;
8138
8139 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8140}
8141
8142static bfd_boolean
d69f560c
KW
8143elfcore_grok_nto_regs (bfd *abfd,
8144 Elf_Internal_Note *note,
d3fd4074 8145 long tid,
d69f560c 8146 char *base)
07c6e936
NC
8147{
8148 char buf[100];
8149 char *name;
8150 asection *sect;
8151
d69f560c 8152 /* Make a "(base)/%d" section. */
d3fd4074 8153 sprintf (buf, "%s/%ld", base, tid);
07c6e936 8154
217aa764 8155 name = bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
8156 if (name == NULL)
8157 return FALSE;
8158 strcpy (name, buf);
8159
117ed4f8 8160 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
8161 if (sect == NULL)
8162 return FALSE;
8163
eea6121a 8164 sect->size = note->descsz;
07c6e936 8165 sect->filepos = note->descpos;
07c6e936
NC
8166 sect->alignment_power = 2;
8167
f8843e87
AM
8168 /* This is the current thread. */
8169 if (elf_tdata (abfd)->core_lwpid == tid)
d69f560c 8170 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87
AM
8171
8172 return TRUE;
07c6e936
NC
8173}
8174
8175#define BFD_QNT_CORE_INFO 7
8176#define BFD_QNT_CORE_STATUS 8
8177#define BFD_QNT_CORE_GREG 9
8178#define BFD_QNT_CORE_FPREG 10
8179
8180static bfd_boolean
217aa764 8181elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
8182{
8183 /* Every GREG section has a STATUS section before it. Store the
811072d8 8184 tid from the previous call to pass down to the next gregs
07c6e936 8185 function. */
d3fd4074 8186 static long tid = 1;
07c6e936
NC
8187
8188 switch (note->type)
8189 {
d69f560c
KW
8190 case BFD_QNT_CORE_INFO:
8191 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8192 case BFD_QNT_CORE_STATUS:
8193 return elfcore_grok_nto_status (abfd, note, &tid);
8194 case BFD_QNT_CORE_GREG:
8195 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8196 case BFD_QNT_CORE_FPREG:
8197 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8198 default:
8199 return TRUE;
07c6e936
NC
8200 }
8201}
8202
b15fa79e
AM
8203static bfd_boolean
8204elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8205{
8206 char *name;
8207 asection *sect;
8208 size_t len;
8209
8210 /* Use note name as section name. */
8211 len = note->namesz;
8212 name = bfd_alloc (abfd, len);
8213 if (name == NULL)
8214 return FALSE;
8215 memcpy (name, note->namedata, len);
8216 name[len - 1] = '\0';
8217
8218 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8219 if (sect == NULL)
8220 return FALSE;
8221
8222 sect->size = note->descsz;
8223 sect->filepos = note->descpos;
8224 sect->alignment_power = 1;
8225
8226 return TRUE;
8227}
8228
7c76fa91
MS
8229/* Function: elfcore_write_note
8230
47d9a591 8231 Inputs:
a39f3346 8232 buffer to hold note, and current size of buffer
7c76fa91
MS
8233 name of note
8234 type of note
8235 data for note
8236 size of data for note
8237
a39f3346
AM
8238 Writes note to end of buffer. ELF64 notes are written exactly as
8239 for ELF32, despite the current (as of 2006) ELF gabi specifying
8240 that they ought to have 8-byte namesz and descsz field, and have
8241 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8242
7c76fa91 8243 Return:
a39f3346 8244 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
8245
8246char *
a39f3346 8247elfcore_write_note (bfd *abfd,
217aa764 8248 char *buf,
a39f3346 8249 int *bufsiz,
217aa764 8250 const char *name,
a39f3346 8251 int type,
217aa764 8252 const void *input,
a39f3346 8253 int size)
7c76fa91
MS
8254{
8255 Elf_External_Note *xnp;
d4c88bbb 8256 size_t namesz;
d4c88bbb 8257 size_t newspace;
a39f3346 8258 char *dest;
7c76fa91 8259
d4c88bbb 8260 namesz = 0;
d4c88bbb 8261 if (name != NULL)
a39f3346 8262 namesz = strlen (name) + 1;
d4c88bbb 8263
a39f3346 8264 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 8265
a39f3346 8266 buf = realloc (buf, *bufsiz + newspace);
14b1c01e
AM
8267 if (buf == NULL)
8268 return buf;
a39f3346 8269 dest = buf + *bufsiz;
7c76fa91
MS
8270 *bufsiz += newspace;
8271 xnp = (Elf_External_Note *) dest;
8272 H_PUT_32 (abfd, namesz, xnp->namesz);
8273 H_PUT_32 (abfd, size, xnp->descsz);
8274 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
8275 dest = xnp->name;
8276 if (name != NULL)
8277 {
8278 memcpy (dest, name, namesz);
8279 dest += namesz;
a39f3346 8280 while (namesz & 3)
d4c88bbb
AM
8281 {
8282 *dest++ = '\0';
a39f3346 8283 ++namesz;
d4c88bbb
AM
8284 }
8285 }
8286 memcpy (dest, input, size);
a39f3346
AM
8287 dest += size;
8288 while (size & 3)
8289 {
8290 *dest++ = '\0';
8291 ++size;
8292 }
8293 return buf;
7c76fa91
MS
8294}
8295
8296#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8297char *
217aa764
AM
8298elfcore_write_prpsinfo (bfd *abfd,
8299 char *buf,
8300 int *bufsiz,
8301 const char *fname,
8302 const char *psargs)
7c76fa91 8303{
183e98be
AM
8304 const char *note_name = "CORE";
8305 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8306
8307 if (bed->elf_backend_write_core_note != NULL)
8308 {
8309 char *ret;
8310 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8311 NT_PRPSINFO, fname, psargs);
8312 if (ret != NULL)
8313 return ret;
8314 }
7c76fa91 8315
183e98be
AM
8316#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8317 if (bed->s->elfclass == ELFCLASS32)
8318 {
8319#if defined (HAVE_PSINFO32_T)
8320 psinfo32_t data;
8321 int note_type = NT_PSINFO;
8322#else
8323 prpsinfo32_t data;
8324 int note_type = NT_PRPSINFO;
8325#endif
8326
8327 memset (&data, 0, sizeof (data));
8328 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8329 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8330 return elfcore_write_note (abfd, buf, bufsiz,
8331 note_name, note_type, &data, sizeof (data));
8332 }
8333 else
8334#endif
8335 {
7c76fa91 8336#if defined (HAVE_PSINFO_T)
183e98be
AM
8337 psinfo_t data;
8338 int note_type = NT_PSINFO;
7c76fa91 8339#else
183e98be
AM
8340 prpsinfo_t data;
8341 int note_type = NT_PRPSINFO;
7c76fa91
MS
8342#endif
8343
183e98be
AM
8344 memset (&data, 0, sizeof (data));
8345 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8346 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8347 return elfcore_write_note (abfd, buf, bufsiz,
8348 note_name, note_type, &data, sizeof (data));
8349 }
7c76fa91
MS
8350}
8351#endif /* PSINFO_T or PRPSINFO_T */
8352
8353#if defined (HAVE_PRSTATUS_T)
8354char *
217aa764
AM
8355elfcore_write_prstatus (bfd *abfd,
8356 char *buf,
8357 int *bufsiz,
8358 long pid,
8359 int cursig,
8360 const void *gregs)
7c76fa91 8361{
183e98be
AM
8362 const char *note_name = "CORE";
8363 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 8364
183e98be
AM
8365 if (bed->elf_backend_write_core_note != NULL)
8366 {
8367 char *ret;
8368 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8369 NT_PRSTATUS,
8370 pid, cursig, gregs);
8371 if (ret != NULL)
8372 return ret;
8373 }
8374
8375#if defined (HAVE_PRSTATUS32_T)
8376 if (bed->s->elfclass == ELFCLASS32)
8377 {
8378 prstatus32_t prstat;
8379
8380 memset (&prstat, 0, sizeof (prstat));
8381 prstat.pr_pid = pid;
8382 prstat.pr_cursig = cursig;
8383 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8384 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8385 NT_PRSTATUS, &prstat, sizeof (prstat));
8386 }
8387 else
8388#endif
8389 {
8390 prstatus_t prstat;
8391
8392 memset (&prstat, 0, sizeof (prstat));
8393 prstat.pr_pid = pid;
8394 prstat.pr_cursig = cursig;
8395 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8396 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8397 NT_PRSTATUS, &prstat, sizeof (prstat));
8398 }
7c76fa91
MS
8399}
8400#endif /* HAVE_PRSTATUS_T */
8401
51316059
MS
8402#if defined (HAVE_LWPSTATUS_T)
8403char *
217aa764
AM
8404elfcore_write_lwpstatus (bfd *abfd,
8405 char *buf,
8406 int *bufsiz,
8407 long pid,
8408 int cursig,
8409 const void *gregs)
51316059
MS
8410{
8411 lwpstatus_t lwpstat;
183e98be 8412 const char *note_name = "CORE";
51316059
MS
8413
8414 memset (&lwpstat, 0, sizeof (lwpstat));
8415 lwpstat.pr_lwpid = pid >> 16;
8416 lwpstat.pr_cursig = cursig;
8417#if defined (HAVE_LWPSTATUS_T_PR_REG)
8418 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8419#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8420#if !defined(gregs)
8421 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8422 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8423#else
8424 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8425 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8426#endif
8427#endif
47d9a591 8428 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
8429 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8430}
8431#endif /* HAVE_LWPSTATUS_T */
8432
7c76fa91
MS
8433#if defined (HAVE_PSTATUS_T)
8434char *
217aa764
AM
8435elfcore_write_pstatus (bfd *abfd,
8436 char *buf,
8437 int *bufsiz,
8438 long pid,
6c10990d
NC
8439 int cursig ATTRIBUTE_UNUSED,
8440 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 8441{
183e98be
AM
8442 const char *note_name = "CORE";
8443#if defined (HAVE_PSTATUS32_T)
8444 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 8445
183e98be
AM
8446 if (bed->s->elfclass == ELFCLASS32)
8447 {
8448 pstatus32_t pstat;
8449
8450 memset (&pstat, 0, sizeof (pstat));
8451 pstat.pr_pid = pid & 0xffff;
8452 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8453 NT_PSTATUS, &pstat, sizeof (pstat));
8454 return buf;
8455 }
8456 else
8457#endif
8458 {
8459 pstatus_t pstat;
8460
8461 memset (&pstat, 0, sizeof (pstat));
8462 pstat.pr_pid = pid & 0xffff;
8463 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8464 NT_PSTATUS, &pstat, sizeof (pstat));
8465 return buf;
8466 }
7c76fa91
MS
8467}
8468#endif /* HAVE_PSTATUS_T */
8469
8470char *
217aa764
AM
8471elfcore_write_prfpreg (bfd *abfd,
8472 char *buf,
8473 int *bufsiz,
8474 const void *fpregs,
8475 int size)
7c76fa91 8476{
183e98be 8477 const char *note_name = "CORE";
47d9a591 8478 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
8479 note_name, NT_FPREGSET, fpregs, size);
8480}
8481
8482char *
217aa764
AM
8483elfcore_write_prxfpreg (bfd *abfd,
8484 char *buf,
8485 int *bufsiz,
8486 const void *xfpregs,
8487 int size)
7c76fa91
MS
8488{
8489 char *note_name = "LINUX";
47d9a591 8490 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
8491 note_name, NT_PRXFPREG, xfpregs, size);
8492}
8493
97753bd5
AM
8494char *
8495elfcore_write_ppc_vmx (bfd *abfd,
8496 char *buf,
8497 int *bufsiz,
8498 const void *ppc_vmx,
8499 int size)
8500{
8501 char *note_name = "LINUX";
8502 return elfcore_write_note (abfd, buf, bufsiz,
8503 note_name, NT_PPC_VMX, ppc_vmx, size);
8504}
8505
89eeb0bc
LM
8506char *
8507elfcore_write_ppc_vsx (bfd *abfd,
8508 char *buf,
8509 int *bufsiz,
8510 const void *ppc_vsx,
8511 int size)
8512{
8513 char *note_name = "LINUX";
8514 return elfcore_write_note (abfd, buf, bufsiz,
8515 note_name, NT_PPC_VSX, ppc_vsx, size);
8516}
8517
bb864ac1
CES
8518char *
8519elfcore_write_register_note (bfd *abfd,
8520 char *buf,
8521 int *bufsiz,
8522 const char *section,
8523 const void *data,
8524 int size)
8525{
8526 if (strcmp (section, ".reg2") == 0)
8527 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8528 if (strcmp (section, ".reg-xfp") == 0)
8529 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8530 if (strcmp (section, ".reg-ppc-vmx") == 0)
8531 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
8532 if (strcmp (section, ".reg-ppc-vsx") == 0)
8533 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
bb864ac1
CES
8534 return NULL;
8535}
8536
b34976b6 8537static bfd_boolean
718175fa 8538elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
252b5132 8539{
c044fabd 8540 char *p;
252b5132 8541
252b5132
RH
8542 p = buf;
8543 while (p < buf + size)
8544 {
c044fabd
KH
8545 /* FIXME: bad alignment assumption. */
8546 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
8547 Elf_Internal_Note in;
8548
baea7ef1
AM
8549 if (offsetof (Elf_External_Note, name) > buf - p + size)
8550 return FALSE;
8551
dc810e39 8552 in.type = H_GET_32 (abfd, xnp->type);
252b5132 8553
dc810e39 8554 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 8555 in.namedata = xnp->name;
baea7ef1
AM
8556 if (in.namesz > buf - in.namedata + size)
8557 return FALSE;
252b5132 8558
dc810e39 8559 in.descsz = H_GET_32 (abfd, xnp->descsz);
252b5132
RH
8560 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8561 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
8562 if (in.descsz != 0
8563 && (in.descdata >= buf + size
8564 || in.descsz > buf - in.descdata + size))
8565 return FALSE;
252b5132 8566
718175fa
JK
8567 switch (bfd_get_format (abfd))
8568 {
8569 default:
8570 return TRUE;
8571
8572 case bfd_core:
8573 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8574 {
8575 if (! elfcore_grok_netbsd_note (abfd, &in))
8576 return FALSE;
8577 }
8578 else if (CONST_STRNEQ (in.namedata, "QNX"))
8579 {
8580 if (! elfcore_grok_nto_note (abfd, &in))
8581 return FALSE;
8582 }
b15fa79e
AM
8583 else if (CONST_STRNEQ (in.namedata, "SPU/"))
8584 {
8585 if (! elfcore_grok_spu_note (abfd, &in))
8586 return FALSE;
8587 }
718175fa
JK
8588 else
8589 {
8590 if (! elfcore_grok_note (abfd, &in))
8591 return FALSE;
8592 }
8593 break;
8594
8595 case bfd_object:
8596 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8597 {
8598 if (! elfobj_grok_gnu_note (abfd, &in))
8599 return FALSE;
8600 }
8601 break;
08a40648 8602 }
252b5132
RH
8603
8604 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8605 }
8606
718175fa
JK
8607 return TRUE;
8608}
8609
8610static bfd_boolean
8611elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8612{
8613 char *buf;
8614
8615 if (size <= 0)
8616 return TRUE;
8617
8618 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8619 return FALSE;
8620
8621 buf = bfd_malloc (size);
8622 if (buf == NULL)
8623 return FALSE;
8624
8625 if (bfd_bread (buf, size, abfd) != size
8626 || !elf_parse_notes (abfd, buf, size, offset))
8627 {
8628 free (buf);
8629 return FALSE;
8630 }
8631
252b5132 8632 free (buf);
b34976b6 8633 return TRUE;
252b5132 8634}
98d8431c
JB
8635\f
8636/* Providing external access to the ELF program header table. */
8637
8638/* Return an upper bound on the number of bytes required to store a
8639 copy of ABFD's program header table entries. Return -1 if an error
8640 occurs; bfd_get_error will return an appropriate code. */
c044fabd 8641
98d8431c 8642long
217aa764 8643bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
8644{
8645 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8646 {
8647 bfd_set_error (bfd_error_wrong_format);
8648 return -1;
8649 }
8650
936e320b 8651 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
8652}
8653
98d8431c
JB
8654/* Copy ABFD's program header table entries to *PHDRS. The entries
8655 will be stored as an array of Elf_Internal_Phdr structures, as
8656 defined in include/elf/internal.h. To find out how large the
8657 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8658
8659 Return the number of program header table entries read, or -1 if an
8660 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 8661
98d8431c 8662int
217aa764 8663bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
8664{
8665 int num_phdrs;
8666
8667 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8668 {
8669 bfd_set_error (bfd_error_wrong_format);
8670 return -1;
8671 }
8672
8673 num_phdrs = elf_elfheader (abfd)->e_phnum;
c044fabd 8674 memcpy (phdrs, elf_tdata (abfd)->phdr,
98d8431c
JB
8675 num_phdrs * sizeof (Elf_Internal_Phdr));
8676
8677 return num_phdrs;
8678}
ae4221d7 8679
db6751f2 8680enum elf_reloc_type_class
217aa764 8681_bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
8682{
8683 return reloc_class_normal;
8684}
f8df10f4 8685
47d9a591 8686/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
8687 relocation against a local symbol. */
8688
8689bfd_vma
217aa764
AM
8690_bfd_elf_rela_local_sym (bfd *abfd,
8691 Elf_Internal_Sym *sym,
8517fae7 8692 asection **psec,
217aa764 8693 Elf_Internal_Rela *rel)
f8df10f4 8694{
8517fae7 8695 asection *sec = *psec;
f8df10f4
JJ
8696 bfd_vma relocation;
8697
8698 relocation = (sec->output_section->vma
8699 + sec->output_offset
8700 + sym->st_value);
8701 if ((sec->flags & SEC_MERGE)
c629eae0 8702 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
68bfbfcc 8703 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
f8df10f4 8704 {
f8df10f4 8705 rel->r_addend =
8517fae7 8706 _bfd_merged_section_offset (abfd, psec,
65765700 8707 elf_section_data (sec)->sec_info,
753731ee
AM
8708 sym->st_value + rel->r_addend);
8709 if (sec != *psec)
8710 {
8711 /* If we have changed the section, and our original section is
8712 marked with SEC_EXCLUDE, it means that the original
8713 SEC_MERGE section has been completely subsumed in some
8714 other SEC_MERGE section. In this case, we need to leave
8715 some info around for --emit-relocs. */
8716 if ((sec->flags & SEC_EXCLUDE) != 0)
8717 sec->kept_section = *psec;
8718 sec = *psec;
8719 }
8517fae7
AM
8720 rel->r_addend -= relocation;
8721 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
8722 }
8723 return relocation;
8724}
c629eae0
JJ
8725
8726bfd_vma
217aa764
AM
8727_bfd_elf_rel_local_sym (bfd *abfd,
8728 Elf_Internal_Sym *sym,
8729 asection **psec,
8730 bfd_vma addend)
47d9a591 8731{
c629eae0
JJ
8732 asection *sec = *psec;
8733
68bfbfcc 8734 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
c629eae0
JJ
8735 return sym->st_value + addend;
8736
8737 return _bfd_merged_section_offset (abfd, psec,
65765700 8738 elf_section_data (sec)->sec_info,
753731ee 8739 sym->st_value + addend);
c629eae0
JJ
8740}
8741
8742bfd_vma
217aa764 8743_bfd_elf_section_offset (bfd *abfd,
92e4ec35 8744 struct bfd_link_info *info,
217aa764
AM
8745 asection *sec,
8746 bfd_vma offset)
c629eae0 8747{
68bfbfcc 8748 switch (sec->sec_info_type)
65765700
JJ
8749 {
8750 case ELF_INFO_TYPE_STABS:
eea6121a
AM
8751 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8752 offset);
65765700 8753 case ELF_INFO_TYPE_EH_FRAME:
92e4ec35 8754 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
65765700
JJ
8755 default:
8756 return offset;
8757 }
c629eae0 8758}
3333a7c3
RM
8759\f
8760/* Create a new BFD as if by bfd_openr. Rather than opening a file,
8761 reconstruct an ELF file by reading the segments out of remote memory
8762 based on the ELF file header at EHDR_VMA and the ELF program headers it
8763 points to. If not null, *LOADBASEP is filled in with the difference
8764 between the VMAs from which the segments were read, and the VMAs the
8765 file headers (and hence BFD's idea of each section's VMA) put them at.
8766
8767 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8768 remote memory at target address VMA into the local buffer at MYADDR; it
8769 should return zero on success or an `errno' code on failure. TEMPL must
8770 be a BFD for an ELF target with the word size and byte order found in
8771 the remote memory. */
8772
8773bfd *
217aa764
AM
8774bfd_elf_bfd_from_remote_memory
8775 (bfd *templ,
8776 bfd_vma ehdr_vma,
8777 bfd_vma *loadbasep,
f075ee0c 8778 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
3333a7c3
RM
8779{
8780 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8781 (templ, ehdr_vma, loadbasep, target_read_memory);
8782}
4c45e5c9
JJ
8783\f
8784long
c9727e01
AM
8785_bfd_elf_get_synthetic_symtab (bfd *abfd,
8786 long symcount ATTRIBUTE_UNUSED,
8787 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 8788 long dynsymcount,
c9727e01
AM
8789 asymbol **dynsyms,
8790 asymbol **ret)
4c45e5c9
JJ
8791{
8792 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8793 asection *relplt;
8794 asymbol *s;
8795 const char *relplt_name;
8796 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8797 arelent *p;
8798 long count, i, n;
8799 size_t size;
8800 Elf_Internal_Shdr *hdr;
8801 char *names;
8802 asection *plt;
8803
8615f3f2
AM
8804 *ret = NULL;
8805
90e3cdf2
JJ
8806 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8807 return 0;
8808
8615f3f2
AM
8809 if (dynsymcount <= 0)
8810 return 0;
8811
4c45e5c9
JJ
8812 if (!bed->plt_sym_val)
8813 return 0;
8814
8815 relplt_name = bed->relplt_name;
8816 if (relplt_name == NULL)
d35fd659 8817 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
8818 relplt = bfd_get_section_by_name (abfd, relplt_name);
8819 if (relplt == NULL)
8820 return 0;
8821
8822 hdr = &elf_section_data (relplt)->this_hdr;
8823 if (hdr->sh_link != elf_dynsymtab (abfd)
8824 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8825 return 0;
8826
8827 plt = bfd_get_section_by_name (abfd, ".plt");
8828 if (plt == NULL)
8829 return 0;
8830
8831 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
c9727e01 8832 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
4c45e5c9
JJ
8833 return -1;
8834
eea6121a 8835 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
8836 size = count * sizeof (asymbol);
8837 p = relplt->relocation;
cb53bf42 8838 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
8839 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8840
8841 s = *ret = bfd_malloc (size);
8842 if (s == NULL)
8843 return -1;
8844
8845 names = (char *) (s + count);
8846 p = relplt->relocation;
8847 n = 0;
cb53bf42 8848 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
8849 {
8850 size_t len;
8851 bfd_vma addr;
8852
8853 addr = bed->plt_sym_val (i, plt, p);
8854 if (addr == (bfd_vma) -1)
8855 continue;
8856
8857 *s = **p->sym_ptr_ptr;
65a7a66f
AM
8858 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8859 we are defining a symbol, ensure one of them is set. */
8860 if ((s->flags & BSF_LOCAL) == 0)
8861 s->flags |= BSF_GLOBAL;
6ba2a415 8862 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
8863 s->section = plt;
8864 s->value = addr - plt->vma;
8865 s->name = names;
8f39ba8e 8866 s->udata.p = NULL;
4c45e5c9
JJ
8867 len = strlen ((*p->sym_ptr_ptr)->name);
8868 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8869 names += len;
8870 memcpy (names, "@plt", sizeof ("@plt"));
8871 names += sizeof ("@plt");
8f39ba8e 8872 ++s, ++n;
4c45e5c9
JJ
8873 }
8874
8875 return n;
8876}
3d7f7666 8877
3b22753a
L
8878/* It is only used by x86-64 so far. */
8879asection _bfd_elf_large_com_section
8880 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
f592407e 8881 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
ecca9871 8882
d1036acb
L
8883void
8884_bfd_elf_set_osabi (bfd * abfd,
8885 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8886{
8887 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
8888
8889 i_ehdrp = elf_elfheader (abfd);
8890
8891 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
8892}
fcb93ecf
PB
8893
8894
8895/* Return TRUE for ELF symbol types that represent functions.
8896 This is the default version of this function, which is sufficient for
8897 most targets. It returns true if TYPE is STT_FUNC. */
8898
8899bfd_boolean
8900_bfd_elf_is_function_type (unsigned int type)
8901{
8902 return (type == STT_FUNC);
8903}