1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
29 BFD support for ELF formats is being worked on.
30 Currently, the best supported back ends are for sparc and i386
31 (running svr4 or Solaris 2).
33 Documentation of the internals of the support code still needs
34 to be written. The code is changing quickly enough that we
35 haven't bothered yet. */
37 /* For sparc64-cross-sparc32. */
45 #include "libiberty.h"
46 #include "safe-ctype.h"
48 static int elf_sort_sections (const void *, const void *);
49 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
50 static bfd_boolean
prep_headers (bfd
*);
51 static bfd_boolean
swap_out_syms (bfd
*, struct bfd_strtab_hash
**, int) ;
52 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
53 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
56 /* Swap version information in and out. The version information is
57 currently size independent. If that ever changes, this code will
58 need to move into elfcode.h. */
60 /* Swap in a Verdef structure. */
63 _bfd_elf_swap_verdef_in (bfd
*abfd
,
64 const Elf_External_Verdef
*src
,
65 Elf_Internal_Verdef
*dst
)
67 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
68 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
69 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
70 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
71 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
72 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
73 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
76 /* Swap out a Verdef structure. */
79 _bfd_elf_swap_verdef_out (bfd
*abfd
,
80 const Elf_Internal_Verdef
*src
,
81 Elf_External_Verdef
*dst
)
83 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
84 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
85 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
86 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
87 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
88 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
89 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
92 /* Swap in a Verdaux structure. */
95 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
96 const Elf_External_Verdaux
*src
,
97 Elf_Internal_Verdaux
*dst
)
99 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
100 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
103 /* Swap out a Verdaux structure. */
106 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
107 const Elf_Internal_Verdaux
*src
,
108 Elf_External_Verdaux
*dst
)
110 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
111 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
114 /* Swap in a Verneed structure. */
117 _bfd_elf_swap_verneed_in (bfd
*abfd
,
118 const Elf_External_Verneed
*src
,
119 Elf_Internal_Verneed
*dst
)
121 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
122 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
123 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
124 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
125 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
128 /* Swap out a Verneed structure. */
131 _bfd_elf_swap_verneed_out (bfd
*abfd
,
132 const Elf_Internal_Verneed
*src
,
133 Elf_External_Verneed
*dst
)
135 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
136 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
137 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
138 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
139 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
142 /* Swap in a Vernaux structure. */
145 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
146 const Elf_External_Vernaux
*src
,
147 Elf_Internal_Vernaux
*dst
)
149 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
150 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
151 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
152 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
153 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
156 /* Swap out a Vernaux structure. */
159 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
160 const Elf_Internal_Vernaux
*src
,
161 Elf_External_Vernaux
*dst
)
163 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
164 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
165 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
166 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
167 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
170 /* Swap in a Versym structure. */
173 _bfd_elf_swap_versym_in (bfd
*abfd
,
174 const Elf_External_Versym
*src
,
175 Elf_Internal_Versym
*dst
)
177 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
180 /* Swap out a Versym structure. */
183 _bfd_elf_swap_versym_out (bfd
*abfd
,
184 const Elf_Internal_Versym
*src
,
185 Elf_External_Versym
*dst
)
187 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
190 /* Standard ELF hash function. Do not change this function; you will
191 cause invalid hash tables to be generated. */
194 bfd_elf_hash (const char *namearg
)
196 const unsigned char *name
= (const unsigned char *) namearg
;
201 while ((ch
= *name
++) != '\0')
204 if ((g
= (h
& 0xf0000000)) != 0)
207 /* The ELF ABI says `h &= ~g', but this is equivalent in
208 this case and on some machines one insn instead of two. */
212 return h
& 0xffffffff;
215 /* DT_GNU_HASH hash function. Do not change this function; you will
216 cause invalid hash tables to be generated. */
219 bfd_elf_gnu_hash (const char *namearg
)
221 const unsigned char *name
= (const unsigned char *) namearg
;
222 unsigned long h
= 5381;
225 while ((ch
= *name
++) != '\0')
226 h
= (h
<< 5) + h
+ ch
;
227 return h
& 0xffffffff;
230 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
231 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
233 bfd_elf_allocate_object (bfd
*abfd
,
235 enum elf_target_id object_id
)
237 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
238 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
239 if (abfd
->tdata
.any
== NULL
)
242 elf_object_id (abfd
) = object_id
;
243 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
249 bfd_elf_make_generic_object (bfd
*abfd
)
251 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
256 bfd_elf_mkcorefile (bfd
*abfd
)
258 /* I think this can be done just like an object file. */
259 return bfd_elf_make_generic_object (abfd
);
263 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
265 Elf_Internal_Shdr
**i_shdrp
;
266 bfd_byte
*shstrtab
= NULL
;
268 bfd_size_type shstrtabsize
;
270 i_shdrp
= elf_elfsections (abfd
);
272 || shindex
>= elf_numsections (abfd
)
273 || i_shdrp
[shindex
] == 0)
276 shstrtab
= i_shdrp
[shindex
]->contents
;
277 if (shstrtab
== NULL
)
279 /* No cached one, attempt to read, and cache what we read. */
280 offset
= i_shdrp
[shindex
]->sh_offset
;
281 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
283 /* Allocate and clear an extra byte at the end, to prevent crashes
284 in case the string table is not terminated. */
285 if (shstrtabsize
+ 1 <= 1
286 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
287 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
289 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
291 if (bfd_get_error () != bfd_error_system_call
)
292 bfd_set_error (bfd_error_file_truncated
);
294 /* Once we've failed to read it, make sure we don't keep
295 trying. Otherwise, we'll keep allocating space for
296 the string table over and over. */
297 i_shdrp
[shindex
]->sh_size
= 0;
300 shstrtab
[shstrtabsize
] = '\0';
301 i_shdrp
[shindex
]->contents
= shstrtab
;
303 return (char *) shstrtab
;
307 bfd_elf_string_from_elf_section (bfd
*abfd
,
308 unsigned int shindex
,
309 unsigned int strindex
)
311 Elf_Internal_Shdr
*hdr
;
316 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
319 hdr
= elf_elfsections (abfd
)[shindex
];
321 if (hdr
->contents
== NULL
322 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
325 if (strindex
>= hdr
->sh_size
)
327 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
328 (*_bfd_error_handler
)
329 (_("%B: invalid string offset %u >= %lu for section `%s'"),
330 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
331 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
333 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
337 return ((char *) hdr
->contents
) + strindex
;
340 /* Read and convert symbols to internal format.
341 SYMCOUNT specifies the number of symbols to read, starting from
342 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
343 are non-NULL, they are used to store the internal symbols, external
344 symbols, and symbol section index extensions, respectively.
345 Returns a pointer to the internal symbol buffer (malloced if necessary)
346 or NULL if there were no symbols or some kind of problem. */
349 bfd_elf_get_elf_syms (bfd
*ibfd
,
350 Elf_Internal_Shdr
*symtab_hdr
,
353 Elf_Internal_Sym
*intsym_buf
,
355 Elf_External_Sym_Shndx
*extshndx_buf
)
357 Elf_Internal_Shdr
*shndx_hdr
;
359 const bfd_byte
*esym
;
360 Elf_External_Sym_Shndx
*alloc_extshndx
;
361 Elf_External_Sym_Shndx
*shndx
;
362 Elf_Internal_Sym
*alloc_intsym
;
363 Elf_Internal_Sym
*isym
;
364 Elf_Internal_Sym
*isymend
;
365 const struct elf_backend_data
*bed
;
370 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
376 /* Normal syms might have section extension entries. */
378 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
379 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
381 /* Read the symbols. */
383 alloc_extshndx
= NULL
;
385 bed
= get_elf_backend_data (ibfd
);
386 extsym_size
= bed
->s
->sizeof_sym
;
387 amt
= symcount
* extsym_size
;
388 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
389 if (extsym_buf
== NULL
)
391 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
392 extsym_buf
= alloc_ext
;
394 if (extsym_buf
== NULL
395 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
396 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
402 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
406 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
407 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
408 if (extshndx_buf
== NULL
)
410 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
411 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
412 extshndx_buf
= alloc_extshndx
;
414 if (extshndx_buf
== NULL
415 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
416 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
423 if (intsym_buf
== NULL
)
425 alloc_intsym
= (Elf_Internal_Sym
*)
426 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
427 intsym_buf
= alloc_intsym
;
428 if (intsym_buf
== NULL
)
432 /* Convert the symbols to internal form. */
433 isymend
= intsym_buf
+ symcount
;
434 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
435 shndx
= extshndx_buf
;
437 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
438 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
440 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
441 (*_bfd_error_handler
) (_("%B symbol number %lu references "
442 "nonexistent SHT_SYMTAB_SHNDX section"),
443 ibfd
, (unsigned long) symoffset
);
444 if (alloc_intsym
!= NULL
)
451 if (alloc_ext
!= NULL
)
453 if (alloc_extshndx
!= NULL
)
454 free (alloc_extshndx
);
459 /* Look up a symbol name. */
461 bfd_elf_sym_name (bfd
*abfd
,
462 Elf_Internal_Shdr
*symtab_hdr
,
463 Elf_Internal_Sym
*isym
,
467 unsigned int iname
= isym
->st_name
;
468 unsigned int shindex
= symtab_hdr
->sh_link
;
470 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
471 /* Check for a bogus st_shndx to avoid crashing. */
472 && isym
->st_shndx
< elf_numsections (abfd
))
474 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
475 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
478 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
481 else if (sym_sec
&& *name
== '\0')
482 name
= bfd_section_name (abfd
, sym_sec
);
487 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
488 sections. The first element is the flags, the rest are section
491 typedef union elf_internal_group
{
492 Elf_Internal_Shdr
*shdr
;
494 } Elf_Internal_Group
;
496 /* Return the name of the group signature symbol. Why isn't the
497 signature just a string? */
500 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
502 Elf_Internal_Shdr
*hdr
;
503 unsigned char esym
[sizeof (Elf64_External_Sym
)];
504 Elf_External_Sym_Shndx eshndx
;
505 Elf_Internal_Sym isym
;
507 /* First we need to ensure the symbol table is available. Make sure
508 that it is a symbol table section. */
509 if (ghdr
->sh_link
>= elf_numsections (abfd
))
511 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
512 if (hdr
->sh_type
!= SHT_SYMTAB
513 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
516 /* Go read the symbol. */
517 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
518 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
519 &isym
, esym
, &eshndx
) == NULL
)
522 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
525 /* Set next_in_group list pointer, and group name for NEWSECT. */
528 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
530 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
532 /* If num_group is zero, read in all SHT_GROUP sections. The count
533 is set to -1 if there are no SHT_GROUP sections. */
536 unsigned int i
, shnum
;
538 /* First count the number of groups. If we have a SHT_GROUP
539 section with just a flag word (ie. sh_size is 4), ignore it. */
540 shnum
= elf_numsections (abfd
);
543 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
544 ( (shdr)->sh_type == SHT_GROUP \
545 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
546 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
547 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
549 for (i
= 0; i
< shnum
; i
++)
551 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
553 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
559 num_group
= (unsigned) -1;
560 elf_tdata (abfd
)->num_group
= num_group
;
564 /* We keep a list of elf section headers for group sections,
565 so we can find them quickly. */
568 elf_tdata (abfd
)->num_group
= num_group
;
569 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
570 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
571 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
575 for (i
= 0; i
< shnum
; i
++)
577 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
579 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
582 Elf_Internal_Group
*dest
;
584 /* Add to list of sections. */
585 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
588 /* Read the raw contents. */
589 BFD_ASSERT (sizeof (*dest
) >= 4);
590 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
591 shdr
->contents
= (unsigned char *)
592 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
593 /* PR binutils/4110: Handle corrupt group headers. */
594 if (shdr
->contents
== NULL
)
597 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
598 bfd_set_error (bfd_error_bad_value
);
602 memset (shdr
->contents
, 0, amt
);
604 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
605 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
609 /* Translate raw contents, a flag word followed by an
610 array of elf section indices all in target byte order,
611 to the flag word followed by an array of elf section
613 src
= shdr
->contents
+ shdr
->sh_size
;
614 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
621 idx
= H_GET_32 (abfd
, src
);
622 if (src
== shdr
->contents
)
625 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
626 shdr
->bfd_section
->flags
627 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
632 ((*_bfd_error_handler
)
633 (_("%B: invalid SHT_GROUP entry"), abfd
));
636 dest
->shdr
= elf_elfsections (abfd
)[idx
];
643 if (num_group
!= (unsigned) -1)
647 for (i
= 0; i
< num_group
; i
++)
649 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
650 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
651 unsigned int n_elt
= shdr
->sh_size
/ 4;
653 /* Look through this group's sections to see if current
654 section is a member. */
656 if ((++idx
)->shdr
== hdr
)
660 /* We are a member of this group. Go looking through
661 other members to see if any others are linked via
663 idx
= (Elf_Internal_Group
*) shdr
->contents
;
664 n_elt
= shdr
->sh_size
/ 4;
666 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
667 && elf_next_in_group (s
) != NULL
)
671 /* Snarf the group name from other member, and
672 insert current section in circular list. */
673 elf_group_name (newsect
) = elf_group_name (s
);
674 elf_next_in_group (newsect
) = elf_next_in_group (s
);
675 elf_next_in_group (s
) = newsect
;
681 gname
= group_signature (abfd
, shdr
);
684 elf_group_name (newsect
) = gname
;
686 /* Start a circular list with one element. */
687 elf_next_in_group (newsect
) = newsect
;
690 /* If the group section has been created, point to the
692 if (shdr
->bfd_section
!= NULL
)
693 elf_next_in_group (shdr
->bfd_section
) = newsect
;
701 if (elf_group_name (newsect
) == NULL
)
703 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
710 _bfd_elf_setup_sections (bfd
*abfd
)
713 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
714 bfd_boolean result
= TRUE
;
717 /* Process SHF_LINK_ORDER. */
718 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
720 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
721 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
723 unsigned int elfsec
= this_hdr
->sh_link
;
724 /* FIXME: The old Intel compiler and old strip/objcopy may
725 not set the sh_link or sh_info fields. Hence we could
726 get the situation where elfsec is 0. */
729 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
730 if (bed
->link_order_error_handler
)
731 bed
->link_order_error_handler
732 (_("%B: warning: sh_link not set for section `%A'"),
737 asection
*linksec
= NULL
;
739 if (elfsec
< elf_numsections (abfd
))
741 this_hdr
= elf_elfsections (abfd
)[elfsec
];
742 linksec
= this_hdr
->bfd_section
;
746 Some strip/objcopy may leave an incorrect value in
747 sh_link. We don't want to proceed. */
750 (*_bfd_error_handler
)
751 (_("%B: sh_link [%d] in section `%A' is incorrect"),
752 s
->owner
, s
, elfsec
);
756 elf_linked_to_section (s
) = linksec
;
761 /* Process section groups. */
762 if (num_group
== (unsigned) -1)
765 for (i
= 0; i
< num_group
; i
++)
767 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
768 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
769 unsigned int n_elt
= shdr
->sh_size
/ 4;
772 if ((++idx
)->shdr
->bfd_section
)
773 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
774 else if (idx
->shdr
->sh_type
== SHT_RELA
775 || idx
->shdr
->sh_type
== SHT_REL
)
776 /* We won't include relocation sections in section groups in
777 output object files. We adjust the group section size here
778 so that relocatable link will work correctly when
779 relocation sections are in section group in input object
781 shdr
->bfd_section
->size
-= 4;
784 /* There are some unknown sections in the group. */
785 (*_bfd_error_handler
)
786 (_("%B: unknown [%d] section `%s' in group [%s]"),
788 (unsigned int) idx
->shdr
->sh_type
,
789 bfd_elf_string_from_elf_section (abfd
,
790 (elf_elfheader (abfd
)
793 shdr
->bfd_section
->name
);
801 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
803 return elf_next_in_group (sec
) != NULL
;
806 /* Make a BFD section from an ELF section. We store a pointer to the
807 BFD section in the bfd_section field of the header. */
810 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
811 Elf_Internal_Shdr
*hdr
,
817 const struct elf_backend_data
*bed
;
819 if (hdr
->bfd_section
!= NULL
)
821 BFD_ASSERT (strcmp (name
,
822 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
826 newsect
= bfd_make_section_anyway (abfd
, name
);
830 hdr
->bfd_section
= newsect
;
831 elf_section_data (newsect
)->this_hdr
= *hdr
;
832 elf_section_data (newsect
)->this_idx
= shindex
;
834 /* Always use the real type/flags. */
835 elf_section_type (newsect
) = hdr
->sh_type
;
836 elf_section_flags (newsect
) = hdr
->sh_flags
;
838 newsect
->filepos
= hdr
->sh_offset
;
840 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
841 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
842 || ! bfd_set_section_alignment (abfd
, newsect
,
843 bfd_log2 (hdr
->sh_addralign
)))
846 flags
= SEC_NO_FLAGS
;
847 if (hdr
->sh_type
!= SHT_NOBITS
)
848 flags
|= SEC_HAS_CONTENTS
;
849 if (hdr
->sh_type
== SHT_GROUP
)
850 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
851 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
854 if (hdr
->sh_type
!= SHT_NOBITS
)
857 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
858 flags
|= SEC_READONLY
;
859 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
861 else if ((flags
& SEC_LOAD
) != 0)
863 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
866 newsect
->entsize
= hdr
->sh_entsize
;
867 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
868 flags
|= SEC_STRINGS
;
870 if (hdr
->sh_flags
& SHF_GROUP
)
871 if (!setup_group (abfd
, hdr
, newsect
))
873 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
874 flags
|= SEC_THREAD_LOCAL
;
876 if ((flags
& SEC_ALLOC
) == 0)
878 /* The debugging sections appear to be recognized only by name,
879 not any sort of flag. Their SEC_ALLOC bits are cleared. */
884 } debug_sections
[] =
886 { STRING_COMMA_LEN ("debug") }, /* 'd' */
887 { NULL
, 0 }, /* 'e' */
888 { NULL
, 0 }, /* 'f' */
889 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
890 { NULL
, 0 }, /* 'h' */
891 { NULL
, 0 }, /* 'i' */
892 { NULL
, 0 }, /* 'j' */
893 { NULL
, 0 }, /* 'k' */
894 { STRING_COMMA_LEN ("line") }, /* 'l' */
895 { NULL
, 0 }, /* 'm' */
896 { NULL
, 0 }, /* 'n' */
897 { NULL
, 0 }, /* 'o' */
898 { NULL
, 0 }, /* 'p' */
899 { NULL
, 0 }, /* 'q' */
900 { NULL
, 0 }, /* 'r' */
901 { STRING_COMMA_LEN ("stab") }, /* 's' */
902 { NULL
, 0 }, /* 't' */
903 { NULL
, 0 }, /* 'u' */
904 { NULL
, 0 }, /* 'v' */
905 { NULL
, 0 }, /* 'w' */
906 { NULL
, 0 }, /* 'x' */
907 { NULL
, 0 }, /* 'y' */
908 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
913 int i
= name
[1] - 'd';
915 && i
< (int) ARRAY_SIZE (debug_sections
)
916 && debug_sections
[i
].name
!= NULL
917 && strncmp (&name
[1], debug_sections
[i
].name
,
918 debug_sections
[i
].len
) == 0)
919 flags
|= SEC_DEBUGGING
;
923 /* As a GNU extension, if the name begins with .gnu.linkonce, we
924 only link a single copy of the section. This is used to support
925 g++. g++ will emit each template expansion in its own section.
926 The symbols will be defined as weak, so that multiple definitions
927 are permitted. The GNU linker extension is to actually discard
928 all but one of the sections. */
929 if (CONST_STRNEQ (name
, ".gnu.linkonce")
930 && elf_next_in_group (newsect
) == NULL
)
931 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
933 bed
= get_elf_backend_data (abfd
);
934 if (bed
->elf_backend_section_flags
)
935 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
938 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
941 /* We do not parse the PT_NOTE segments as we are interested even in the
942 separate debug info files which may have the segments offsets corrupted.
943 PT_NOTEs from the core files are currently not parsed using BFD. */
944 if (hdr
->sh_type
== SHT_NOTE
)
948 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
951 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
955 if ((flags
& SEC_ALLOC
) != 0)
957 Elf_Internal_Phdr
*phdr
;
958 unsigned int i
, nload
;
960 /* Some ELF linkers produce binaries with all the program header
961 p_paddr fields zero. If we have such a binary with more than
962 one PT_LOAD header, then leave the section lma equal to vma
963 so that we don't create sections with overlapping lma. */
964 phdr
= elf_tdata (abfd
)->phdr
;
965 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
966 if (phdr
->p_paddr
!= 0)
968 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
970 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
973 phdr
= elf_tdata (abfd
)->phdr
;
974 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
976 if (phdr
->p_type
== PT_LOAD
977 && ELF_IS_SECTION_IN_SEGMENT (hdr
, phdr
))
979 if ((flags
& SEC_LOAD
) == 0)
980 newsect
->lma
= (phdr
->p_paddr
981 + hdr
->sh_addr
- phdr
->p_vaddr
);
983 /* We used to use the same adjustment for SEC_LOAD
984 sections, but that doesn't work if the segment
985 is packed with code from multiple VMAs.
986 Instead we calculate the section LMA based on
987 the segment LMA. It is assumed that the
988 segment will contain sections with contiguous
989 LMAs, even if the VMAs are not. */
990 newsect
->lma
= (phdr
->p_paddr
991 + hdr
->sh_offset
- phdr
->p_offset
);
993 /* With contiguous segments, we can't tell from file
994 offsets whether a section with zero size should
995 be placed at the end of one segment or the
996 beginning of the next. Decide based on vaddr. */
997 if (hdr
->sh_addr
>= phdr
->p_vaddr
998 && (hdr
->sh_addr
+ hdr
->sh_size
999 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1008 const char *const bfd_elf_section_type_names
[] = {
1009 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1010 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1011 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1014 /* ELF relocs are against symbols. If we are producing relocatable
1015 output, and the reloc is against an external symbol, and nothing
1016 has given us any additional addend, the resulting reloc will also
1017 be against the same symbol. In such a case, we don't want to
1018 change anything about the way the reloc is handled, since it will
1019 all be done at final link time. Rather than put special case code
1020 into bfd_perform_relocation, all the reloc types use this howto
1021 function. It just short circuits the reloc if producing
1022 relocatable output against an external symbol. */
1024 bfd_reloc_status_type
1025 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1026 arelent
*reloc_entry
,
1028 void *data ATTRIBUTE_UNUSED
,
1029 asection
*input_section
,
1031 char **error_message ATTRIBUTE_UNUSED
)
1033 if (output_bfd
!= NULL
1034 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1035 && (! reloc_entry
->howto
->partial_inplace
1036 || reloc_entry
->addend
== 0))
1038 reloc_entry
->address
+= input_section
->output_offset
;
1039 return bfd_reloc_ok
;
1042 return bfd_reloc_continue
;
1045 /* Copy the program header and other data from one object module to
1049 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1051 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1052 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1055 BFD_ASSERT (!elf_flags_init (obfd
)
1056 || (elf_elfheader (obfd
)->e_flags
1057 == elf_elfheader (ibfd
)->e_flags
));
1059 elf_gp (obfd
) = elf_gp (ibfd
);
1060 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1061 elf_flags_init (obfd
) = TRUE
;
1063 /* Copy object attributes. */
1064 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1070 get_segment_type (unsigned int p_type
)
1075 case PT_NULL
: pt
= "NULL"; break;
1076 case PT_LOAD
: pt
= "LOAD"; break;
1077 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1078 case PT_INTERP
: pt
= "INTERP"; break;
1079 case PT_NOTE
: pt
= "NOTE"; break;
1080 case PT_SHLIB
: pt
= "SHLIB"; break;
1081 case PT_PHDR
: pt
= "PHDR"; break;
1082 case PT_TLS
: pt
= "TLS"; break;
1083 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1084 case PT_GNU_STACK
: pt
= "STACK"; break;
1085 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1086 default: pt
= NULL
; break;
1091 /* Print out the program headers. */
1094 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1096 FILE *f
= (FILE *) farg
;
1097 Elf_Internal_Phdr
*p
;
1099 bfd_byte
*dynbuf
= NULL
;
1101 p
= elf_tdata (abfd
)->phdr
;
1106 fprintf (f
, _("\nProgram Header:\n"));
1107 c
= elf_elfheader (abfd
)->e_phnum
;
1108 for (i
= 0; i
< c
; i
++, p
++)
1110 const char *pt
= get_segment_type (p
->p_type
);
1115 sprintf (buf
, "0x%lx", p
->p_type
);
1118 fprintf (f
, "%8s off 0x", pt
);
1119 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1120 fprintf (f
, " vaddr 0x");
1121 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1122 fprintf (f
, " paddr 0x");
1123 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1124 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1125 fprintf (f
, " filesz 0x");
1126 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1127 fprintf (f
, " memsz 0x");
1128 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1129 fprintf (f
, " flags %c%c%c",
1130 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1131 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1132 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1133 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1134 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1139 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1142 unsigned int elfsec
;
1143 unsigned long shlink
;
1144 bfd_byte
*extdyn
, *extdynend
;
1146 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1148 fprintf (f
, _("\nDynamic Section:\n"));
1150 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1153 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1154 if (elfsec
== SHN_BAD
)
1156 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1158 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1159 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1162 extdynend
= extdyn
+ s
->size
;
1163 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1165 Elf_Internal_Dyn dyn
;
1166 const char *name
= "";
1168 bfd_boolean stringp
;
1169 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1171 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1173 if (dyn
.d_tag
== DT_NULL
)
1180 if (bed
->elf_backend_get_target_dtag
)
1181 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1183 if (!strcmp (name
, ""))
1185 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1190 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1191 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1192 case DT_PLTGOT
: name
= "PLTGOT"; break;
1193 case DT_HASH
: name
= "HASH"; break;
1194 case DT_STRTAB
: name
= "STRTAB"; break;
1195 case DT_SYMTAB
: name
= "SYMTAB"; break;
1196 case DT_RELA
: name
= "RELA"; break;
1197 case DT_RELASZ
: name
= "RELASZ"; break;
1198 case DT_RELAENT
: name
= "RELAENT"; break;
1199 case DT_STRSZ
: name
= "STRSZ"; break;
1200 case DT_SYMENT
: name
= "SYMENT"; break;
1201 case DT_INIT
: name
= "INIT"; break;
1202 case DT_FINI
: name
= "FINI"; break;
1203 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1204 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1205 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1206 case DT_REL
: name
= "REL"; break;
1207 case DT_RELSZ
: name
= "RELSZ"; break;
1208 case DT_RELENT
: name
= "RELENT"; break;
1209 case DT_PLTREL
: name
= "PLTREL"; break;
1210 case DT_DEBUG
: name
= "DEBUG"; break;
1211 case DT_TEXTREL
: name
= "TEXTREL"; break;
1212 case DT_JMPREL
: name
= "JMPREL"; break;
1213 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1214 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1215 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1216 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1217 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1218 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1219 case DT_FLAGS
: name
= "FLAGS"; break;
1220 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1221 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1222 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1223 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1224 case DT_MOVEENT
: name
= "MOVEENT"; break;
1225 case DT_MOVESZ
: name
= "MOVESZ"; break;
1226 case DT_FEATURE
: name
= "FEATURE"; break;
1227 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1228 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1229 case DT_SYMINENT
: name
= "SYMINENT"; break;
1230 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1231 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1232 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1233 case DT_PLTPAD
: name
= "PLTPAD"; break;
1234 case DT_MOVETAB
: name
= "MOVETAB"; break;
1235 case DT_SYMINFO
: name
= "SYMINFO"; break;
1236 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1237 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1238 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1239 case DT_VERSYM
: name
= "VERSYM"; break;
1240 case DT_VERDEF
: name
= "VERDEF"; break;
1241 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1242 case DT_VERNEED
: name
= "VERNEED"; break;
1243 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1244 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1245 case DT_USED
: name
= "USED"; break;
1246 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1247 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1250 fprintf (f
, " %-20s ", name
);
1254 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1259 unsigned int tagv
= dyn
.d_un
.d_val
;
1261 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1264 fprintf (f
, "%s", string
);
1273 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1274 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1276 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1280 if (elf_dynverdef (abfd
) != 0)
1282 Elf_Internal_Verdef
*t
;
1284 fprintf (f
, _("\nVersion definitions:\n"));
1285 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1287 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1288 t
->vd_flags
, t
->vd_hash
,
1289 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1290 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1292 Elf_Internal_Verdaux
*a
;
1295 for (a
= t
->vd_auxptr
->vda_nextptr
;
1299 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1305 if (elf_dynverref (abfd
) != 0)
1307 Elf_Internal_Verneed
*t
;
1309 fprintf (f
, _("\nVersion References:\n"));
1310 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1312 Elf_Internal_Vernaux
*a
;
1314 fprintf (f
, _(" required from %s:\n"),
1315 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1316 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1317 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1318 a
->vna_flags
, a
->vna_other
,
1319 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1331 /* Display ELF-specific fields of a symbol. */
1334 bfd_elf_print_symbol (bfd
*abfd
,
1337 bfd_print_symbol_type how
)
1339 FILE *file
= (FILE *) filep
;
1342 case bfd_print_symbol_name
:
1343 fprintf (file
, "%s", symbol
->name
);
1345 case bfd_print_symbol_more
:
1346 fprintf (file
, "elf ");
1347 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1348 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1350 case bfd_print_symbol_all
:
1352 const char *section_name
;
1353 const char *name
= NULL
;
1354 const struct elf_backend_data
*bed
;
1355 unsigned char st_other
;
1358 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1360 bed
= get_elf_backend_data (abfd
);
1361 if (bed
->elf_backend_print_symbol_all
)
1362 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1366 name
= symbol
->name
;
1367 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1370 fprintf (file
, " %s\t", section_name
);
1371 /* Print the "other" value for a symbol. For common symbols,
1372 we've already printed the size; now print the alignment.
1373 For other symbols, we have no specified alignment, and
1374 we've printed the address; now print the size. */
1375 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1376 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1378 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1379 bfd_fprintf_vma (abfd
, file
, val
);
1381 /* If we have version information, print it. */
1382 if (elf_tdata (abfd
)->dynversym_section
!= 0
1383 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1384 || elf_tdata (abfd
)->dynverref_section
!= 0))
1386 unsigned int vernum
;
1387 const char *version_string
;
1389 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1392 version_string
= "";
1393 else if (vernum
== 1)
1394 version_string
= "Base";
1395 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1397 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1400 Elf_Internal_Verneed
*t
;
1402 version_string
= "";
1403 for (t
= elf_tdata (abfd
)->verref
;
1407 Elf_Internal_Vernaux
*a
;
1409 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1411 if (a
->vna_other
== vernum
)
1413 version_string
= a
->vna_nodename
;
1420 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1421 fprintf (file
, " %-11s", version_string
);
1426 fprintf (file
, " (%s)", version_string
);
1427 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1432 /* If the st_other field is not zero, print it. */
1433 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1438 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1439 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1440 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1442 /* Some other non-defined flags are also present, so print
1444 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1447 fprintf (file
, " %s", name
);
1453 /* Allocate an ELF string table--force the first byte to be zero. */
1455 struct bfd_strtab_hash
*
1456 _bfd_elf_stringtab_init (void)
1458 struct bfd_strtab_hash
*ret
;
1460 ret
= _bfd_stringtab_init ();
1465 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1466 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1467 if (loc
== (bfd_size_type
) -1)
1469 _bfd_stringtab_free (ret
);
1476 /* ELF .o/exec file reading */
1478 /* Create a new bfd section from an ELF section header. */
1481 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1483 Elf_Internal_Shdr
*hdr
;
1484 Elf_Internal_Ehdr
*ehdr
;
1485 const struct elf_backend_data
*bed
;
1488 if (shindex
>= elf_numsections (abfd
))
1491 hdr
= elf_elfsections (abfd
)[shindex
];
1492 ehdr
= elf_elfheader (abfd
);
1493 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1498 bed
= get_elf_backend_data (abfd
);
1499 switch (hdr
->sh_type
)
1502 /* Inactive section. Throw it away. */
1505 case SHT_PROGBITS
: /* Normal section with contents. */
1506 case SHT_NOBITS
: /* .bss section. */
1507 case SHT_HASH
: /* .hash section. */
1508 case SHT_NOTE
: /* .note section. */
1509 case SHT_INIT_ARRAY
: /* .init_array section. */
1510 case SHT_FINI_ARRAY
: /* .fini_array section. */
1511 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1512 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1513 case SHT_GNU_HASH
: /* .gnu.hash section. */
1514 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1516 case SHT_DYNAMIC
: /* Dynamic linking information. */
1517 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1519 if (hdr
->sh_link
> elf_numsections (abfd
))
1521 /* PR 10478: Accept Solaris binaries with a sh_link
1522 field set to SHN_BEFORE or SHN_AFTER. */
1523 switch (bfd_get_arch (abfd
))
1526 case bfd_arch_sparc
:
1527 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1528 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1530 /* Otherwise fall through. */
1535 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1537 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1539 Elf_Internal_Shdr
*dynsymhdr
;
1541 /* The shared libraries distributed with hpux11 have a bogus
1542 sh_link field for the ".dynamic" section. Find the
1543 string table for the ".dynsym" section instead. */
1544 if (elf_dynsymtab (abfd
) != 0)
1546 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1547 hdr
->sh_link
= dynsymhdr
->sh_link
;
1551 unsigned int i
, num_sec
;
1553 num_sec
= elf_numsections (abfd
);
1554 for (i
= 1; i
< num_sec
; i
++)
1556 dynsymhdr
= elf_elfsections (abfd
)[i
];
1557 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1559 hdr
->sh_link
= dynsymhdr
->sh_link
;
1567 case SHT_SYMTAB
: /* A symbol table */
1568 if (elf_onesymtab (abfd
) == shindex
)
1571 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1573 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1575 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1576 elf_onesymtab (abfd
) = shindex
;
1577 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1578 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1579 abfd
->flags
|= HAS_SYMS
;
1581 /* Sometimes a shared object will map in the symbol table. If
1582 SHF_ALLOC is set, and this is a shared object, then we also
1583 treat this section as a BFD section. We can not base the
1584 decision purely on SHF_ALLOC, because that flag is sometimes
1585 set in a relocatable object file, which would confuse the
1587 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1588 && (abfd
->flags
& DYNAMIC
) != 0
1589 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1593 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1594 can't read symbols without that section loaded as well. It
1595 is most likely specified by the next section header. */
1596 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1598 unsigned int i
, num_sec
;
1600 num_sec
= elf_numsections (abfd
);
1601 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1603 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1604 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1605 && hdr2
->sh_link
== shindex
)
1609 for (i
= 1; i
< shindex
; i
++)
1611 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1612 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1613 && hdr2
->sh_link
== shindex
)
1617 return bfd_section_from_shdr (abfd
, i
);
1621 case SHT_DYNSYM
: /* A dynamic symbol table */
1622 if (elf_dynsymtab (abfd
) == shindex
)
1625 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1627 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1628 elf_dynsymtab (abfd
) = shindex
;
1629 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1630 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1631 abfd
->flags
|= HAS_SYMS
;
1633 /* Besides being a symbol table, we also treat this as a regular
1634 section, so that objcopy can handle it. */
1635 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1637 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1638 if (elf_symtab_shndx (abfd
) == shindex
)
1641 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1642 elf_symtab_shndx (abfd
) = shindex
;
1643 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1644 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1647 case SHT_STRTAB
: /* A string table */
1648 if (hdr
->bfd_section
!= NULL
)
1650 if (ehdr
->e_shstrndx
== shindex
)
1652 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1653 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1656 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1659 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1660 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1663 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1666 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1667 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1668 elf_elfsections (abfd
)[shindex
] = hdr
;
1669 /* We also treat this as a regular section, so that objcopy
1671 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1675 /* If the string table isn't one of the above, then treat it as a
1676 regular section. We need to scan all the headers to be sure,
1677 just in case this strtab section appeared before the above. */
1678 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1680 unsigned int i
, num_sec
;
1682 num_sec
= elf_numsections (abfd
);
1683 for (i
= 1; i
< num_sec
; i
++)
1685 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1686 if (hdr2
->sh_link
== shindex
)
1688 /* Prevent endless recursion on broken objects. */
1691 if (! bfd_section_from_shdr (abfd
, i
))
1693 if (elf_onesymtab (abfd
) == i
)
1695 if (elf_dynsymtab (abfd
) == i
)
1696 goto dynsymtab_strtab
;
1700 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1704 /* *These* do a lot of work -- but build no sections! */
1706 asection
*target_sect
;
1707 Elf_Internal_Shdr
*hdr2
;
1708 unsigned int num_sec
= elf_numsections (abfd
);
1711 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1712 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1715 /* Check for a bogus link to avoid crashing. */
1716 if (hdr
->sh_link
>= num_sec
)
1718 ((*_bfd_error_handler
)
1719 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1720 abfd
, hdr
->sh_link
, name
, shindex
));
1721 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1725 /* For some incomprehensible reason Oracle distributes
1726 libraries for Solaris in which some of the objects have
1727 bogus sh_link fields. It would be nice if we could just
1728 reject them, but, unfortunately, some people need to use
1729 them. We scan through the section headers; if we find only
1730 one suitable symbol table, we clobber the sh_link to point
1731 to it. I hope this doesn't break anything.
1733 Don't do it on executable nor shared library. */
1734 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
1735 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1736 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1742 for (scan
= 1; scan
< num_sec
; scan
++)
1744 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1745 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1756 hdr
->sh_link
= found
;
1759 /* Get the symbol table. */
1760 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1761 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1762 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1765 /* If this reloc section does not use the main symbol table we
1766 don't treat it as a reloc section. BFD can't adequately
1767 represent such a section, so at least for now, we don't
1768 try. We just present it as a normal section. We also
1769 can't use it as a reloc section if it points to the null
1770 section, an invalid section, another reloc section, or its
1771 sh_link points to the null section. */
1772 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1773 || hdr
->sh_link
== SHN_UNDEF
1774 || hdr
->sh_info
== SHN_UNDEF
1775 || hdr
->sh_info
>= num_sec
1776 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1777 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1778 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1781 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1783 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1784 if (target_sect
== NULL
)
1787 if ((target_sect
->flags
& SEC_RELOC
) == 0
1788 || target_sect
->reloc_count
== 0)
1789 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1793 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1794 amt
= sizeof (*hdr2
);
1795 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1798 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1801 elf_elfsections (abfd
)[shindex
] = hdr2
;
1802 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1803 target_sect
->flags
|= SEC_RELOC
;
1804 target_sect
->relocation
= NULL
;
1805 target_sect
->rel_filepos
= hdr
->sh_offset
;
1806 /* In the section to which the relocations apply, mark whether
1807 its relocations are of the REL or RELA variety. */
1808 if (hdr
->sh_size
!= 0)
1809 target_sect
->use_rela_p
= hdr
->sh_type
== SHT_RELA
;
1810 abfd
->flags
|= HAS_RELOC
;
1814 case SHT_GNU_verdef
:
1815 elf_dynverdef (abfd
) = shindex
;
1816 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1817 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1819 case SHT_GNU_versym
:
1820 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1822 elf_dynversym (abfd
) = shindex
;
1823 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1824 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1826 case SHT_GNU_verneed
:
1827 elf_dynverref (abfd
) = shindex
;
1828 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1829 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1835 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
))
1837 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1839 if (hdr
->contents
!= NULL
)
1841 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1842 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
1845 if (idx
->flags
& GRP_COMDAT
)
1846 hdr
->bfd_section
->flags
1847 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1849 /* We try to keep the same section order as it comes in. */
1851 while (--n_elt
!= 0)
1855 if (idx
->shdr
!= NULL
1856 && (s
= idx
->shdr
->bfd_section
) != NULL
1857 && elf_next_in_group (s
) != NULL
)
1859 elf_next_in_group (hdr
->bfd_section
) = s
;
1867 /* Possibly an attributes section. */
1868 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
1869 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
1871 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1873 _bfd_elf_parse_attributes (abfd
, hdr
);
1877 /* Check for any processor-specific section types. */
1878 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
1881 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
1883 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1884 /* FIXME: How to properly handle allocated section reserved
1885 for applications? */
1886 (*_bfd_error_handler
)
1887 (_("%B: don't know how to handle allocated, application "
1888 "specific section `%s' [0x%8x]"),
1889 abfd
, name
, hdr
->sh_type
);
1891 /* Allow sections reserved for applications. */
1892 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1895 else if (hdr
->sh_type
>= SHT_LOPROC
1896 && hdr
->sh_type
<= SHT_HIPROC
)
1897 /* FIXME: We should handle this section. */
1898 (*_bfd_error_handler
)
1899 (_("%B: don't know how to handle processor specific section "
1901 abfd
, name
, hdr
->sh_type
);
1902 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
1904 /* Unrecognised OS-specific sections. */
1905 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
1906 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1907 required to correctly process the section and the file should
1908 be rejected with an error message. */
1909 (*_bfd_error_handler
)
1910 (_("%B: don't know how to handle OS specific section "
1912 abfd
, name
, hdr
->sh_type
);
1914 /* Otherwise it should be processed. */
1915 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1918 /* FIXME: We should handle this section. */
1919 (*_bfd_error_handler
)
1920 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1921 abfd
, name
, hdr
->sh_type
);
1929 /* Return the local symbol specified by ABFD, R_SYMNDX. */
1932 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
1934 unsigned long r_symndx
)
1936 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1938 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
1940 Elf_Internal_Shdr
*symtab_hdr
;
1941 unsigned char esym
[sizeof (Elf64_External_Sym
)];
1942 Elf_External_Sym_Shndx eshndx
;
1944 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1945 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
1946 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
1949 if (cache
->abfd
!= abfd
)
1951 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1954 cache
->indx
[ent
] = r_symndx
;
1957 return &cache
->sym
[ent
];
1960 /* Given an ELF section number, retrieve the corresponding BFD
1964 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
1966 if (sec_index
>= elf_numsections (abfd
))
1968 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
1971 static const struct bfd_elf_special_section special_sections_b
[] =
1973 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
1974 { NULL
, 0, 0, 0, 0 }
1977 static const struct bfd_elf_special_section special_sections_c
[] =
1979 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
1980 { NULL
, 0, 0, 0, 0 }
1983 static const struct bfd_elf_special_section special_sections_d
[] =
1985 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
1986 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
1987 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
1988 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
1989 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
1990 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
1991 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
1992 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
1993 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
1994 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
1995 { NULL
, 0, 0, 0, 0 }
1998 static const struct bfd_elf_special_section special_sections_f
[] =
2000 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2001 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2002 { NULL
, 0, 0, 0, 0 }
2005 static const struct bfd_elf_special_section special_sections_g
[] =
2007 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2008 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2009 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2010 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2011 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2012 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2013 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2014 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2015 { NULL
, 0, 0, 0, 0 }
2018 static const struct bfd_elf_special_section special_sections_h
[] =
2020 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2021 { NULL
, 0, 0, 0, 0 }
2024 static const struct bfd_elf_special_section special_sections_i
[] =
2026 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2027 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2028 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2029 { NULL
, 0, 0, 0, 0 }
2032 static const struct bfd_elf_special_section special_sections_l
[] =
2034 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2035 { NULL
, 0, 0, 0, 0 }
2038 static const struct bfd_elf_special_section special_sections_n
[] =
2040 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2041 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2042 { NULL
, 0, 0, 0, 0 }
2045 static const struct bfd_elf_special_section special_sections_p
[] =
2047 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2048 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2049 { NULL
, 0, 0, 0, 0 }
2052 static const struct bfd_elf_special_section special_sections_r
[] =
2054 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2055 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2056 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2057 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2058 { NULL
, 0, 0, 0, 0 }
2061 static const struct bfd_elf_special_section special_sections_s
[] =
2063 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2064 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2065 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2066 /* See struct bfd_elf_special_section declaration for the semantics of
2067 this special case where .prefix_length != strlen (.prefix). */
2068 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2069 { NULL
, 0, 0, 0, 0 }
2072 static const struct bfd_elf_special_section special_sections_t
[] =
2074 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2075 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2076 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2077 { NULL
, 0, 0, 0, 0 }
2080 static const struct bfd_elf_special_section special_sections_z
[] =
2082 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2083 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2084 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2085 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2086 { NULL
, 0, 0, 0, 0 }
2089 static const struct bfd_elf_special_section
*special_sections
[] =
2091 special_sections_b
, /* 'b' */
2092 special_sections_c
, /* 'c' */
2093 special_sections_d
, /* 'd' */
2095 special_sections_f
, /* 'f' */
2096 special_sections_g
, /* 'g' */
2097 special_sections_h
, /* 'h' */
2098 special_sections_i
, /* 'i' */
2101 special_sections_l
, /* 'l' */
2103 special_sections_n
, /* 'n' */
2105 special_sections_p
, /* 'p' */
2107 special_sections_r
, /* 'r' */
2108 special_sections_s
, /* 's' */
2109 special_sections_t
, /* 't' */
2115 special_sections_z
/* 'z' */
2118 const struct bfd_elf_special_section
*
2119 _bfd_elf_get_special_section (const char *name
,
2120 const struct bfd_elf_special_section
*spec
,
2126 len
= strlen (name
);
2128 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2131 int prefix_len
= spec
[i
].prefix_length
;
2133 if (len
< prefix_len
)
2135 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2138 suffix_len
= spec
[i
].suffix_length
;
2139 if (suffix_len
<= 0)
2141 if (name
[prefix_len
] != 0)
2143 if (suffix_len
== 0)
2145 if (name
[prefix_len
] != '.'
2146 && (suffix_len
== -2
2147 || (rela
&& spec
[i
].type
== SHT_REL
)))
2153 if (len
< prefix_len
+ suffix_len
)
2155 if (memcmp (name
+ len
- suffix_len
,
2156 spec
[i
].prefix
+ prefix_len
,
2166 const struct bfd_elf_special_section
*
2167 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2170 const struct bfd_elf_special_section
*spec
;
2171 const struct elf_backend_data
*bed
;
2173 /* See if this is one of the special sections. */
2174 if (sec
->name
== NULL
)
2177 bed
= get_elf_backend_data (abfd
);
2178 spec
= bed
->special_sections
;
2181 spec
= _bfd_elf_get_special_section (sec
->name
,
2182 bed
->special_sections
,
2188 if (sec
->name
[0] != '.')
2191 i
= sec
->name
[1] - 'b';
2192 if (i
< 0 || i
> 'z' - 'b')
2195 spec
= special_sections
[i
];
2200 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2204 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2206 struct bfd_elf_section_data
*sdata
;
2207 const struct elf_backend_data
*bed
;
2208 const struct bfd_elf_special_section
*ssect
;
2210 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2213 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2217 sec
->used_by_bfd
= sdata
;
2220 /* Indicate whether or not this section should use RELA relocations. */
2221 bed
= get_elf_backend_data (abfd
);
2222 sec
->use_rela_p
= bed
->default_use_rela_p
;
2224 /* When we read a file, we don't need to set ELF section type and
2225 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2226 anyway. We will set ELF section type and flags for all linker
2227 created sections. If user specifies BFD section flags, we will
2228 set ELF section type and flags based on BFD section flags in
2229 elf_fake_sections. */
2230 if ((!sec
->flags
&& abfd
->direction
!= read_direction
)
2231 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2233 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2236 elf_section_type (sec
) = ssect
->type
;
2237 elf_section_flags (sec
) = ssect
->attr
;
2241 return _bfd_generic_new_section_hook (abfd
, sec
);
2244 /* Create a new bfd section from an ELF program header.
2246 Since program segments have no names, we generate a synthetic name
2247 of the form segment<NUM>, where NUM is generally the index in the
2248 program header table. For segments that are split (see below) we
2249 generate the names segment<NUM>a and segment<NUM>b.
2251 Note that some program segments may have a file size that is different than
2252 (less than) the memory size. All this means is that at execution the
2253 system must allocate the amount of memory specified by the memory size,
2254 but only initialize it with the first "file size" bytes read from the
2255 file. This would occur for example, with program segments consisting
2256 of combined data+bss.
2258 To handle the above situation, this routine generates TWO bfd sections
2259 for the single program segment. The first has the length specified by
2260 the file size of the segment, and the second has the length specified
2261 by the difference between the two sizes. In effect, the segment is split
2262 into its initialized and uninitialized parts.
2267 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2268 Elf_Internal_Phdr
*hdr
,
2270 const char *type_name
)
2278 split
= ((hdr
->p_memsz
> 0)
2279 && (hdr
->p_filesz
> 0)
2280 && (hdr
->p_memsz
> hdr
->p_filesz
));
2282 if (hdr
->p_filesz
> 0)
2284 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2285 len
= strlen (namebuf
) + 1;
2286 name
= (char *) bfd_alloc (abfd
, len
);
2289 memcpy (name
, namebuf
, len
);
2290 newsect
= bfd_make_section (abfd
, name
);
2291 if (newsect
== NULL
)
2293 newsect
->vma
= hdr
->p_vaddr
;
2294 newsect
->lma
= hdr
->p_paddr
;
2295 newsect
->size
= hdr
->p_filesz
;
2296 newsect
->filepos
= hdr
->p_offset
;
2297 newsect
->flags
|= SEC_HAS_CONTENTS
;
2298 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2299 if (hdr
->p_type
== PT_LOAD
)
2301 newsect
->flags
|= SEC_ALLOC
;
2302 newsect
->flags
|= SEC_LOAD
;
2303 if (hdr
->p_flags
& PF_X
)
2305 /* FIXME: all we known is that it has execute PERMISSION,
2307 newsect
->flags
|= SEC_CODE
;
2310 if (!(hdr
->p_flags
& PF_W
))
2312 newsect
->flags
|= SEC_READONLY
;
2316 if (hdr
->p_memsz
> hdr
->p_filesz
)
2320 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2321 len
= strlen (namebuf
) + 1;
2322 name
= (char *) bfd_alloc (abfd
, len
);
2325 memcpy (name
, namebuf
, len
);
2326 newsect
= bfd_make_section (abfd
, name
);
2327 if (newsect
== NULL
)
2329 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2330 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2331 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2332 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2333 align
= newsect
->vma
& -newsect
->vma
;
2334 if (align
== 0 || align
> hdr
->p_align
)
2335 align
= hdr
->p_align
;
2336 newsect
->alignment_power
= bfd_log2 (align
);
2337 if (hdr
->p_type
== PT_LOAD
)
2339 /* Hack for gdb. Segments that have not been modified do
2340 not have their contents written to a core file, on the
2341 assumption that a debugger can find the contents in the
2342 executable. We flag this case by setting the fake
2343 section size to zero. Note that "real" bss sections will
2344 always have their contents dumped to the core file. */
2345 if (bfd_get_format (abfd
) == bfd_core
)
2347 newsect
->flags
|= SEC_ALLOC
;
2348 if (hdr
->p_flags
& PF_X
)
2349 newsect
->flags
|= SEC_CODE
;
2351 if (!(hdr
->p_flags
& PF_W
))
2352 newsect
->flags
|= SEC_READONLY
;
2359 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2361 const struct elf_backend_data
*bed
;
2363 switch (hdr
->p_type
)
2366 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2369 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2372 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2375 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2378 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2380 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2385 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2388 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2390 case PT_GNU_EH_FRAME
:
2391 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2395 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2398 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2401 /* Check for any processor-specific program segment types. */
2402 bed
= get_elf_backend_data (abfd
);
2403 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2407 /* Initialize REL_HDR, the section-header for new section, containing
2408 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2409 relocations; otherwise, we use REL relocations. */
2412 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2413 Elf_Internal_Shdr
*rel_hdr
,
2415 bfd_boolean use_rela_p
)
2418 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2419 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2421 name
= (char *) bfd_alloc (abfd
, amt
);
2424 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2426 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2428 if (rel_hdr
->sh_name
== (unsigned int) -1)
2430 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2431 rel_hdr
->sh_entsize
= (use_rela_p
2432 ? bed
->s
->sizeof_rela
2433 : bed
->s
->sizeof_rel
);
2434 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2435 rel_hdr
->sh_flags
= 0;
2436 rel_hdr
->sh_addr
= 0;
2437 rel_hdr
->sh_size
= 0;
2438 rel_hdr
->sh_offset
= 0;
2443 /* Return the default section type based on the passed in section flags. */
2446 bfd_elf_get_default_section_type (flagword flags
)
2448 if ((flags
& SEC_ALLOC
) != 0
2449 && ((flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0
2450 || (flags
& SEC_NEVER_LOAD
) != 0))
2452 return SHT_PROGBITS
;
2455 /* Set up an ELF internal section header for a section. */
2458 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *failedptrarg
)
2460 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2461 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2462 Elf_Internal_Shdr
*this_hdr
;
2463 unsigned int sh_type
;
2467 /* We already failed; just get out of the bfd_map_over_sections
2472 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2474 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2475 asect
->name
, FALSE
);
2476 if (this_hdr
->sh_name
== (unsigned int) -1)
2482 /* Don't clear sh_flags. Assembler may set additional bits. */
2484 if ((asect
->flags
& SEC_ALLOC
) != 0
2485 || asect
->user_set_vma
)
2486 this_hdr
->sh_addr
= asect
->vma
;
2488 this_hdr
->sh_addr
= 0;
2490 this_hdr
->sh_offset
= 0;
2491 this_hdr
->sh_size
= asect
->size
;
2492 this_hdr
->sh_link
= 0;
2493 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2494 /* The sh_entsize and sh_info fields may have been set already by
2495 copy_private_section_data. */
2497 this_hdr
->bfd_section
= asect
;
2498 this_hdr
->contents
= NULL
;
2500 /* If the section type is unspecified, we set it based on
2502 if ((asect
->flags
& SEC_GROUP
) != 0)
2503 sh_type
= SHT_GROUP
;
2505 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2507 if (this_hdr
->sh_type
== SHT_NULL
)
2508 this_hdr
->sh_type
= sh_type
;
2509 else if (this_hdr
->sh_type
== SHT_NOBITS
2510 && sh_type
== SHT_PROGBITS
2511 && (asect
->flags
& SEC_ALLOC
) != 0)
2513 /* Warn if we are changing a NOBITS section to PROGBITS, but
2514 allow the link to proceed. This can happen when users link
2515 non-bss input sections to bss output sections, or emit data
2516 to a bss output section via a linker script. */
2517 (*_bfd_error_handler
)
2518 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2519 this_hdr
->sh_type
= sh_type
;
2522 switch (this_hdr
->sh_type
)
2528 case SHT_INIT_ARRAY
:
2529 case SHT_FINI_ARRAY
:
2530 case SHT_PREINIT_ARRAY
:
2537 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2541 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2545 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2549 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2550 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2554 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2555 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2558 case SHT_GNU_versym
:
2559 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2562 case SHT_GNU_verdef
:
2563 this_hdr
->sh_entsize
= 0;
2564 /* objcopy or strip will copy over sh_info, but may not set
2565 cverdefs. The linker will set cverdefs, but sh_info will be
2567 if (this_hdr
->sh_info
== 0)
2568 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2570 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2571 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2574 case SHT_GNU_verneed
:
2575 this_hdr
->sh_entsize
= 0;
2576 /* objcopy or strip will copy over sh_info, but may not set
2577 cverrefs. The linker will set cverrefs, but sh_info will be
2579 if (this_hdr
->sh_info
== 0)
2580 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2582 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2583 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2587 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2591 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2595 if ((asect
->flags
& SEC_ALLOC
) != 0)
2596 this_hdr
->sh_flags
|= SHF_ALLOC
;
2597 if ((asect
->flags
& SEC_READONLY
) == 0)
2598 this_hdr
->sh_flags
|= SHF_WRITE
;
2599 if ((asect
->flags
& SEC_CODE
) != 0)
2600 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2601 if ((asect
->flags
& SEC_MERGE
) != 0)
2603 this_hdr
->sh_flags
|= SHF_MERGE
;
2604 this_hdr
->sh_entsize
= asect
->entsize
;
2605 if ((asect
->flags
& SEC_STRINGS
) != 0)
2606 this_hdr
->sh_flags
|= SHF_STRINGS
;
2608 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2609 this_hdr
->sh_flags
|= SHF_GROUP
;
2610 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2612 this_hdr
->sh_flags
|= SHF_TLS
;
2613 if (asect
->size
== 0
2614 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2616 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2618 this_hdr
->sh_size
= 0;
2621 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2622 if (this_hdr
->sh_size
!= 0)
2623 this_hdr
->sh_type
= SHT_NOBITS
;
2628 /* Check for processor-specific section types. */
2629 sh_type
= this_hdr
->sh_type
;
2630 if (bed
->elf_backend_fake_sections
2631 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2634 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2636 /* Don't change the header type from NOBITS if we are being
2637 called for objcopy --only-keep-debug. */
2638 this_hdr
->sh_type
= sh_type
;
2641 /* If the section has relocs, set up a section header for the
2642 SHT_REL[A] section. If two relocation sections are required for
2643 this section, it is up to the processor-specific back-end to
2644 create the other. */
2645 if ((asect
->flags
& SEC_RELOC
) != 0
2646 && !_bfd_elf_init_reloc_shdr (abfd
,
2647 &elf_section_data (asect
)->rel_hdr
,
2653 /* Fill in the contents of a SHT_GROUP section. Called from
2654 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2655 when ELF targets use the generic linker, ld. Called for ld -r
2656 from bfd_elf_final_link. */
2659 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2661 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2662 asection
*elt
, *first
;
2666 /* Ignore linker created group section. See elfNN_ia64_object_p in
2668 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2672 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
2674 unsigned long symindx
= 0;
2676 /* elf_group_id will have been set up by objcopy and the
2678 if (elf_group_id (sec
) != NULL
)
2679 symindx
= elf_group_id (sec
)->udata
.i
;
2683 /* If called from the assembler, swap_out_syms will have set up
2684 elf_section_syms. */
2685 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
2686 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2688 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2690 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
2692 /* The ELF backend linker sets sh_info to -2 when the group
2693 signature symbol is global, and thus the index can't be
2694 set until all local symbols are output. */
2695 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
2696 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
2697 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
2698 unsigned long extsymoff
= 0;
2699 struct elf_link_hash_entry
*h
;
2701 if (!elf_bad_symtab (igroup
->owner
))
2703 Elf_Internal_Shdr
*symtab_hdr
;
2705 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
2706 extsymoff
= symtab_hdr
->sh_info
;
2708 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
2709 while (h
->root
.type
== bfd_link_hash_indirect
2710 || h
->root
.type
== bfd_link_hash_warning
)
2711 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2713 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
2716 /* The contents won't be allocated for "ld -r" or objcopy. */
2718 if (sec
->contents
== NULL
)
2721 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
2723 /* Arrange for the section to be written out. */
2724 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2725 if (sec
->contents
== NULL
)
2732 loc
= sec
->contents
+ sec
->size
;
2734 /* Get the pointer to the first section in the group that gas
2735 squirreled away here. objcopy arranges for this to be set to the
2736 start of the input section group. */
2737 first
= elt
= elf_next_in_group (sec
);
2739 /* First element is a flag word. Rest of section is elf section
2740 indices for all the sections of the group. Write them backwards
2741 just to keep the group in the same order as given in .section
2742 directives, not that it matters. */
2749 if (! elf_discarded_section (s
))
2753 s
= s
->output_section
;
2756 idx
= elf_section_data (s
)->this_idx
;
2757 H_PUT_32 (abfd
, idx
, loc
);
2759 elt
= elf_next_in_group (elt
);
2764 if ((loc
-= 4) != sec
->contents
)
2767 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2770 /* Assign all ELF section numbers. The dummy first section is handled here
2771 too. The link/info pointers for the standard section types are filled
2772 in here too, while we're at it. */
2775 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
2777 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2779 unsigned int section_number
, secn
;
2780 Elf_Internal_Shdr
**i_shdrp
;
2781 struct bfd_elf_section_data
*d
;
2782 bfd_boolean need_symtab
;
2786 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2788 /* SHT_GROUP sections are in relocatable files only. */
2789 if (link_info
== NULL
|| link_info
->relocatable
)
2791 /* Put SHT_GROUP sections first. */
2792 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2794 d
= elf_section_data (sec
);
2796 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2798 if (sec
->flags
& SEC_LINKER_CREATED
)
2800 /* Remove the linker created SHT_GROUP sections. */
2801 bfd_section_list_remove (abfd
, sec
);
2802 abfd
->section_count
--;
2805 d
->this_idx
= section_number
++;
2810 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2812 d
= elf_section_data (sec
);
2814 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2815 d
->this_idx
= section_number
++;
2816 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2817 if ((sec
->flags
& SEC_RELOC
) == 0)
2821 d
->rel_idx
= section_number
++;
2822 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2827 d
->rel_idx2
= section_number
++;
2828 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2834 t
->shstrtab_section
= section_number
++;
2835 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2836 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2838 need_symtab
= (bfd_get_symcount (abfd
) > 0
2839 || (link_info
== NULL
2840 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
2844 t
->symtab_section
= section_number
++;
2845 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2846 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
2848 t
->symtab_shndx_section
= section_number
++;
2849 t
->symtab_shndx_hdr
.sh_name
2850 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2851 ".symtab_shndx", FALSE
);
2852 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2855 t
->strtab_section
= section_number
++;
2856 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2859 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2860 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2862 elf_numsections (abfd
) = section_number
;
2863 elf_elfheader (abfd
)->e_shnum
= section_number
;
2865 /* Set up the list of section header pointers, in agreement with the
2867 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
2868 sizeof (Elf_Internal_Shdr
*));
2869 if (i_shdrp
== NULL
)
2872 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
2873 sizeof (Elf_Internal_Shdr
));
2874 if (i_shdrp
[0] == NULL
)
2876 bfd_release (abfd
, i_shdrp
);
2880 elf_elfsections (abfd
) = i_shdrp
;
2882 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2885 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2886 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
2888 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2889 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2891 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2892 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2895 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2900 d
= elf_section_data (sec
);
2902 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2903 if (d
->rel_idx
!= 0)
2904 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2905 if (d
->rel_idx2
!= 0)
2906 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2908 /* Fill in the sh_link and sh_info fields while we're at it. */
2910 /* sh_link of a reloc section is the section index of the symbol
2911 table. sh_info is the section index of the section to which
2912 the relocation entries apply. */
2913 if (d
->rel_idx
!= 0)
2915 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2916 d
->rel_hdr
.sh_info
= d
->this_idx
;
2918 if (d
->rel_idx2
!= 0)
2920 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2921 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2924 /* We need to set up sh_link for SHF_LINK_ORDER. */
2925 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
2927 s
= elf_linked_to_section (sec
);
2930 /* elf_linked_to_section points to the input section. */
2931 if (link_info
!= NULL
)
2933 /* Check discarded linkonce section. */
2934 if (elf_discarded_section (s
))
2937 (*_bfd_error_handler
)
2938 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2939 abfd
, d
->this_hdr
.bfd_section
,
2941 /* Point to the kept section if it has the same
2942 size as the discarded one. */
2943 kept
= _bfd_elf_check_kept_section (s
, link_info
);
2946 bfd_set_error (bfd_error_bad_value
);
2952 s
= s
->output_section
;
2953 BFD_ASSERT (s
!= NULL
);
2957 /* Handle objcopy. */
2958 if (s
->output_section
== NULL
)
2960 (*_bfd_error_handler
)
2961 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2962 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
2963 bfd_set_error (bfd_error_bad_value
);
2966 s
= s
->output_section
;
2968 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2973 The Intel C compiler generates SHT_IA_64_UNWIND with
2974 SHF_LINK_ORDER. But it doesn't set the sh_link or
2975 sh_info fields. Hence we could get the situation
2977 const struct elf_backend_data
*bed
2978 = get_elf_backend_data (abfd
);
2979 if (bed
->link_order_error_handler
)
2980 bed
->link_order_error_handler
2981 (_("%B: warning: sh_link not set for section `%A'"),
2986 switch (d
->this_hdr
.sh_type
)
2990 /* A reloc section which we are treating as a normal BFD
2991 section. sh_link is the section index of the symbol
2992 table. sh_info is the section index of the section to
2993 which the relocation entries apply. We assume that an
2994 allocated reloc section uses the dynamic symbol table.
2995 FIXME: How can we be sure? */
2996 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2998 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3000 /* We look up the section the relocs apply to by name. */
3002 if (d
->this_hdr
.sh_type
== SHT_REL
)
3006 s
= bfd_get_section_by_name (abfd
, name
);
3008 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3012 /* We assume that a section named .stab*str is a stabs
3013 string section. We look for a section with the same name
3014 but without the trailing ``str'', and set its sh_link
3015 field to point to this section. */
3016 if (CONST_STRNEQ (sec
->name
, ".stab")
3017 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3022 len
= strlen (sec
->name
);
3023 alc
= (char *) bfd_malloc (len
- 2);
3026 memcpy (alc
, sec
->name
, len
- 3);
3027 alc
[len
- 3] = '\0';
3028 s
= bfd_get_section_by_name (abfd
, alc
);
3032 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3034 /* This is a .stab section. */
3035 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3036 elf_section_data (s
)->this_hdr
.sh_entsize
3037 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3044 case SHT_GNU_verneed
:
3045 case SHT_GNU_verdef
:
3046 /* sh_link is the section header index of the string table
3047 used for the dynamic entries, or the symbol table, or the
3049 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3051 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3054 case SHT_GNU_LIBLIST
:
3055 /* sh_link is the section header index of the prelink library
3056 list used for the dynamic entries, or the symbol table, or
3057 the version strings. */
3058 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3059 ? ".dynstr" : ".gnu.libstr");
3061 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3066 case SHT_GNU_versym
:
3067 /* sh_link is the section header index of the symbol table
3068 this hash table or version table is for. */
3069 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3071 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3075 d
->this_hdr
.sh_link
= t
->symtab_section
;
3079 for (secn
= 1; secn
< section_number
; ++secn
)
3080 if (i_shdrp
[secn
] == NULL
)
3081 i_shdrp
[secn
] = i_shdrp
[0];
3083 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3084 i_shdrp
[secn
]->sh_name
);
3088 /* Map symbol from it's internal number to the external number, moving
3089 all local symbols to be at the head of the list. */
3092 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3094 /* If the backend has a special mapping, use it. */
3095 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3096 if (bed
->elf_backend_sym_is_global
)
3097 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3099 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3100 || bfd_is_und_section (bfd_get_section (sym
))
3101 || bfd_is_com_section (bfd_get_section (sym
)));
3104 /* Don't output section symbols for sections that are not going to be
3108 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3110 return ((sym
->flags
& BSF_SECTION_SYM
) != 0
3111 && !(sym
->section
->owner
== abfd
3112 || (sym
->section
->output_section
->owner
== abfd
3113 && sym
->section
->output_offset
== 0)));
3117 elf_map_symbols (bfd
*abfd
)
3119 unsigned int symcount
= bfd_get_symcount (abfd
);
3120 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3121 asymbol
**sect_syms
;
3122 unsigned int num_locals
= 0;
3123 unsigned int num_globals
= 0;
3124 unsigned int num_locals2
= 0;
3125 unsigned int num_globals2
= 0;
3132 fprintf (stderr
, "elf_map_symbols\n");
3136 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3138 if (max_index
< asect
->index
)
3139 max_index
= asect
->index
;
3143 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3144 if (sect_syms
== NULL
)
3146 elf_section_syms (abfd
) = sect_syms
;
3147 elf_num_section_syms (abfd
) = max_index
;
3149 /* Init sect_syms entries for any section symbols we have already
3150 decided to output. */
3151 for (idx
= 0; idx
< symcount
; idx
++)
3153 asymbol
*sym
= syms
[idx
];
3155 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3157 && !ignore_section_sym (abfd
, sym
))
3159 asection
*sec
= sym
->section
;
3161 if (sec
->owner
!= abfd
)
3162 sec
= sec
->output_section
;
3164 sect_syms
[sec
->index
] = syms
[idx
];
3168 /* Classify all of the symbols. */
3169 for (idx
= 0; idx
< symcount
; idx
++)
3171 if (ignore_section_sym (abfd
, syms
[idx
]))
3173 if (!sym_is_global (abfd
, syms
[idx
]))
3179 /* We will be adding a section symbol for each normal BFD section. Most
3180 sections will already have a section symbol in outsymbols, but
3181 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3182 at least in that case. */
3183 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3185 if (sect_syms
[asect
->index
] == NULL
)
3187 if (!sym_is_global (abfd
, asect
->symbol
))
3194 /* Now sort the symbols so the local symbols are first. */
3195 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3196 sizeof (asymbol
*));
3198 if (new_syms
== NULL
)
3201 for (idx
= 0; idx
< symcount
; idx
++)
3203 asymbol
*sym
= syms
[idx
];
3206 if (ignore_section_sym (abfd
, sym
))
3208 if (!sym_is_global (abfd
, sym
))
3211 i
= num_locals
+ num_globals2
++;
3213 sym
->udata
.i
= i
+ 1;
3215 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3217 if (sect_syms
[asect
->index
] == NULL
)
3219 asymbol
*sym
= asect
->symbol
;
3222 sect_syms
[asect
->index
] = sym
;
3223 if (!sym_is_global (abfd
, sym
))
3226 i
= num_locals
+ num_globals2
++;
3228 sym
->udata
.i
= i
+ 1;
3232 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3234 elf_num_locals (abfd
) = num_locals
;
3235 elf_num_globals (abfd
) = num_globals
;
3239 /* Align to the maximum file alignment that could be required for any
3240 ELF data structure. */
3242 static inline file_ptr
3243 align_file_position (file_ptr off
, int align
)
3245 return (off
+ align
- 1) & ~(align
- 1);
3248 /* Assign a file position to a section, optionally aligning to the
3249 required section alignment. */
3252 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3256 if (align
&& i_shdrp
->sh_addralign
> 1)
3257 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3258 i_shdrp
->sh_offset
= offset
;
3259 if (i_shdrp
->bfd_section
!= NULL
)
3260 i_shdrp
->bfd_section
->filepos
= offset
;
3261 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3262 offset
+= i_shdrp
->sh_size
;
3266 /* Compute the file positions we are going to put the sections at, and
3267 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3268 is not NULL, this is being called by the ELF backend linker. */
3271 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3272 struct bfd_link_info
*link_info
)
3274 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3276 struct bfd_strtab_hash
*strtab
= NULL
;
3277 Elf_Internal_Shdr
*shstrtab_hdr
;
3278 bfd_boolean need_symtab
;
3280 if (abfd
->output_has_begun
)
3283 /* Do any elf backend specific processing first. */
3284 if (bed
->elf_backend_begin_write_processing
)
3285 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3287 if (! prep_headers (abfd
))
3290 /* Post process the headers if necessary. */
3291 if (bed
->elf_backend_post_process_headers
)
3292 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3295 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3299 if (!assign_section_numbers (abfd
, link_info
))
3302 /* The backend linker builds symbol table information itself. */
3303 need_symtab
= (link_info
== NULL
3304 && (bfd_get_symcount (abfd
) > 0
3305 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3309 /* Non-zero if doing a relocatable link. */
3310 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3312 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3316 if (link_info
== NULL
)
3318 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3323 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3324 /* sh_name was set in prep_headers. */
3325 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3326 shstrtab_hdr
->sh_flags
= 0;
3327 shstrtab_hdr
->sh_addr
= 0;
3328 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3329 shstrtab_hdr
->sh_entsize
= 0;
3330 shstrtab_hdr
->sh_link
= 0;
3331 shstrtab_hdr
->sh_info
= 0;
3332 /* sh_offset is set in assign_file_positions_except_relocs. */
3333 shstrtab_hdr
->sh_addralign
= 1;
3335 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3341 Elf_Internal_Shdr
*hdr
;
3343 off
= elf_tdata (abfd
)->next_file_pos
;
3345 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3346 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3348 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3349 if (hdr
->sh_size
!= 0)
3350 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3352 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3353 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3355 elf_tdata (abfd
)->next_file_pos
= off
;
3357 /* Now that we know where the .strtab section goes, write it
3359 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3360 || ! _bfd_stringtab_emit (abfd
, strtab
))
3362 _bfd_stringtab_free (strtab
);
3365 abfd
->output_has_begun
= TRUE
;
3370 /* Make an initial estimate of the size of the program header. If we
3371 get the number wrong here, we'll redo section placement. */
3373 static bfd_size_type
3374 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3378 const struct elf_backend_data
*bed
;
3380 /* Assume we will need exactly two PT_LOAD segments: one for text
3381 and one for data. */
3384 s
= bfd_get_section_by_name (abfd
, ".interp");
3385 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3387 /* If we have a loadable interpreter section, we need a
3388 PT_INTERP segment. In this case, assume we also need a
3389 PT_PHDR segment, although that may not be true for all
3394 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3396 /* We need a PT_DYNAMIC segment. */
3400 if (info
!= NULL
&& info
->relro
)
3402 /* We need a PT_GNU_RELRO segment. */
3406 if (elf_tdata (abfd
)->eh_frame_hdr
)
3408 /* We need a PT_GNU_EH_FRAME segment. */
3412 if (elf_tdata (abfd
)->stack_flags
)
3414 /* We need a PT_GNU_STACK segment. */
3418 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3420 if ((s
->flags
& SEC_LOAD
) != 0
3421 && CONST_STRNEQ (s
->name
, ".note"))
3423 /* We need a PT_NOTE segment. */
3425 /* Try to create just one PT_NOTE segment
3426 for all adjacent loadable .note* sections.
3427 gABI requires that within a PT_NOTE segment
3428 (and also inside of each SHT_NOTE section)
3429 each note is padded to a multiple of 4 size,
3430 so we check whether the sections are correctly
3432 if (s
->alignment_power
== 2)
3433 while (s
->next
!= NULL
3434 && s
->next
->alignment_power
== 2
3435 && (s
->next
->flags
& SEC_LOAD
) != 0
3436 && CONST_STRNEQ (s
->next
->name
, ".note"))
3441 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3443 if (s
->flags
& SEC_THREAD_LOCAL
)
3445 /* We need a PT_TLS segment. */
3451 /* Let the backend count up any program headers it might need. */
3452 bed
= get_elf_backend_data (abfd
);
3453 if (bed
->elf_backend_additional_program_headers
)
3457 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3463 return segs
* bed
->s
->sizeof_phdr
;
3466 /* Find the segment that contains the output_section of section. */
3469 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3471 struct elf_segment_map
*m
;
3472 Elf_Internal_Phdr
*p
;
3474 for (m
= elf_tdata (abfd
)->segment_map
,
3475 p
= elf_tdata (abfd
)->phdr
;
3481 for (i
= m
->count
- 1; i
>= 0; i
--)
3482 if (m
->sections
[i
] == section
)
3489 /* Create a mapping from a set of sections to a program segment. */
3491 static struct elf_segment_map
*
3492 make_mapping (bfd
*abfd
,
3493 asection
**sections
,
3498 struct elf_segment_map
*m
;
3503 amt
= sizeof (struct elf_segment_map
);
3504 amt
+= (to
- from
- 1) * sizeof (asection
*);
3505 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3509 m
->p_type
= PT_LOAD
;
3510 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3511 m
->sections
[i
- from
] = *hdrpp
;
3512 m
->count
= to
- from
;
3514 if (from
== 0 && phdr
)
3516 /* Include the headers in the first PT_LOAD segment. */
3517 m
->includes_filehdr
= 1;
3518 m
->includes_phdrs
= 1;
3524 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3527 struct elf_segment_map
*
3528 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3530 struct elf_segment_map
*m
;
3532 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
3533 sizeof (struct elf_segment_map
));
3537 m
->p_type
= PT_DYNAMIC
;
3539 m
->sections
[0] = dynsec
;
3544 /* Possibly add or remove segments from the segment map. */
3547 elf_modify_segment_map (bfd
*abfd
,
3548 struct bfd_link_info
*info
,
3549 bfd_boolean remove_empty_load
)
3551 struct elf_segment_map
**m
;
3552 const struct elf_backend_data
*bed
;
3554 /* The placement algorithm assumes that non allocated sections are
3555 not in PT_LOAD segments. We ensure this here by removing such
3556 sections from the segment map. We also remove excluded
3557 sections. Finally, any PT_LOAD segment without sections is
3559 m
= &elf_tdata (abfd
)->segment_map
;
3562 unsigned int i
, new_count
;
3564 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3566 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3567 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3568 || (*m
)->p_type
!= PT_LOAD
))
3570 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3574 (*m
)->count
= new_count
;
3576 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3582 bed
= get_elf_backend_data (abfd
);
3583 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3585 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3592 /* Set up a mapping from BFD sections to program segments. */
3595 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3598 struct elf_segment_map
*m
;
3599 asection
**sections
= NULL
;
3600 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3601 bfd_boolean no_user_phdrs
;
3603 no_user_phdrs
= elf_tdata (abfd
)->segment_map
== NULL
;
3604 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3608 struct elf_segment_map
*mfirst
;
3609 struct elf_segment_map
**pm
;
3612 unsigned int phdr_index
;
3613 bfd_vma maxpagesize
;
3615 bfd_boolean phdr_in_segment
= TRUE
;
3616 bfd_boolean writable
;
3618 asection
*first_tls
= NULL
;
3619 asection
*dynsec
, *eh_frame_hdr
;
3622 /* Select the allocated sections, and sort them. */
3624 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
3625 sizeof (asection
*));
3626 if (sections
== NULL
)
3630 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3632 if ((s
->flags
& SEC_ALLOC
) != 0)
3638 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3641 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3643 /* Build the mapping. */
3648 /* If we have a .interp section, then create a PT_PHDR segment for
3649 the program headers and a PT_INTERP segment for the .interp
3651 s
= bfd_get_section_by_name (abfd
, ".interp");
3652 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3654 amt
= sizeof (struct elf_segment_map
);
3655 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3659 m
->p_type
= PT_PHDR
;
3660 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3661 m
->p_flags
= PF_R
| PF_X
;
3662 m
->p_flags_valid
= 1;
3663 m
->includes_phdrs
= 1;
3668 amt
= sizeof (struct elf_segment_map
);
3669 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3673 m
->p_type
= PT_INTERP
;
3681 /* Look through the sections. We put sections in the same program
3682 segment when the start of the second section can be placed within
3683 a few bytes of the end of the first section. */
3687 maxpagesize
= bed
->maxpagesize
;
3689 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3691 && (dynsec
->flags
& SEC_LOAD
) == 0)
3694 /* Deal with -Ttext or something similar such that the first section
3695 is not adjacent to the program headers. This is an
3696 approximation, since at this point we don't know exactly how many
3697 program headers we will need. */
3700 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
3702 if (phdr_size
== (bfd_size_type
) -1)
3703 phdr_size
= get_program_header_size (abfd
, info
);
3704 if ((abfd
->flags
& D_PAGED
) == 0
3705 || sections
[0]->lma
< phdr_size
3706 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3707 phdr_in_segment
= FALSE
;
3710 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3713 bfd_boolean new_segment
;
3717 /* See if this section and the last one will fit in the same
3720 if (last_hdr
== NULL
)
3722 /* If we don't have a segment yet, then we don't need a new
3723 one (we build the last one after this loop). */
3724 new_segment
= FALSE
;
3726 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3728 /* If this section has a different relation between the
3729 virtual address and the load address, then we need a new
3733 /* In the next test we have to be careful when last_hdr->lma is close
3734 to the end of the address space. If the aligned address wraps
3735 around to the start of the address space, then there are no more
3736 pages left in memory and it is OK to assume that the current
3737 section can be included in the current segment. */
3738 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3740 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3743 /* If putting this section in this segment would force us to
3744 skip a page in the segment, then we need a new segment. */
3747 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3748 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3750 /* We don't want to put a loadable section after a
3751 nonloadable section in the same segment.
3752 Consider .tbss sections as loadable for this purpose. */
3755 else if ((abfd
->flags
& D_PAGED
) == 0)
3757 /* If the file is not demand paged, which means that we
3758 don't require the sections to be correctly aligned in the
3759 file, then there is no other reason for a new segment. */
3760 new_segment
= FALSE
;
3763 && (hdr
->flags
& SEC_READONLY
) == 0
3764 && (((last_hdr
->lma
+ last_size
- 1)
3765 & ~(maxpagesize
- 1))
3766 != (hdr
->lma
& ~(maxpagesize
- 1))))
3768 /* We don't want to put a writable section in a read only
3769 segment, unless they are on the same page in memory
3770 anyhow. We already know that the last section does not
3771 bring us past the current section on the page, so the
3772 only case in which the new section is not on the same
3773 page as the previous section is when the previous section
3774 ends precisely on a page boundary. */
3779 /* Otherwise, we can use the same segment. */
3780 new_segment
= FALSE
;
3783 /* Allow interested parties a chance to override our decision. */
3784 if (last_hdr
!= NULL
3786 && info
->callbacks
->override_segment_assignment
!= NULL
)
3788 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
3794 if ((hdr
->flags
& SEC_READONLY
) == 0)
3797 /* .tbss sections effectively have zero size. */
3798 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
3799 != SEC_THREAD_LOCAL
)
3800 last_size
= hdr
->size
;
3806 /* We need a new program segment. We must create a new program
3807 header holding all the sections from phdr_index until hdr. */
3809 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3816 if ((hdr
->flags
& SEC_READONLY
) == 0)
3822 /* .tbss sections effectively have zero size. */
3823 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3824 last_size
= hdr
->size
;
3828 phdr_in_segment
= FALSE
;
3831 /* Create a final PT_LOAD program segment. */
3832 if (last_hdr
!= NULL
)
3834 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3842 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3845 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
3852 /* For each batch of consecutive loadable .note sections,
3853 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3854 because if we link together nonloadable .note sections and
3855 loadable .note sections, we will generate two .note sections
3856 in the output file. FIXME: Using names for section types is
3858 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3860 if ((s
->flags
& SEC_LOAD
) != 0
3861 && CONST_STRNEQ (s
->name
, ".note"))
3866 amt
= sizeof (struct elf_segment_map
);
3867 if (s
->alignment_power
== 2)
3868 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
3870 if (s2
->next
->alignment_power
== 2
3871 && (s2
->next
->flags
& SEC_LOAD
) != 0
3872 && CONST_STRNEQ (s2
->next
->name
, ".note")
3873 && align_power (s2
->vma
+ s2
->size
, 2)
3879 amt
+= (count
- 1) * sizeof (asection
*);
3880 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3884 m
->p_type
= PT_NOTE
;
3888 m
->sections
[m
->count
- count
--] = s
;
3889 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
3892 m
->sections
[m
->count
- 1] = s
;
3893 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
3897 if (s
->flags
& SEC_THREAD_LOCAL
)
3905 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3908 amt
= sizeof (struct elf_segment_map
);
3909 amt
+= (tls_count
- 1) * sizeof (asection
*);
3910 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3915 m
->count
= tls_count
;
3916 /* Mandated PF_R. */
3918 m
->p_flags_valid
= 1;
3919 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
3921 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3922 m
->sections
[i
] = first_tls
;
3923 first_tls
= first_tls
->next
;
3930 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3932 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
3933 if (eh_frame_hdr
!= NULL
3934 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
3936 amt
= sizeof (struct elf_segment_map
);
3937 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3941 m
->p_type
= PT_GNU_EH_FRAME
;
3943 m
->sections
[0] = eh_frame_hdr
->output_section
;
3949 if (elf_tdata (abfd
)->stack_flags
)
3951 amt
= sizeof (struct elf_segment_map
);
3952 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3956 m
->p_type
= PT_GNU_STACK
;
3957 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
3958 m
->p_flags_valid
= 1;
3964 if (info
!= NULL
&& info
->relro
)
3966 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3968 if (m
->p_type
== PT_LOAD
)
3970 asection
*last
= m
->sections
[m
->count
- 1];
3971 bfd_vma vaddr
= m
->sections
[0]->vma
;
3972 bfd_vma filesz
= last
->vma
- vaddr
+ last
->size
;
3974 if (vaddr
< info
->relro_end
3975 && vaddr
>= info
->relro_start
3976 && (vaddr
+ filesz
) >= info
->relro_end
)
3981 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3984 amt
= sizeof (struct elf_segment_map
);
3985 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3989 m
->p_type
= PT_GNU_RELRO
;
3991 m
->p_flags_valid
= 1;
3999 elf_tdata (abfd
)->segment_map
= mfirst
;
4002 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4005 for (count
= 0, m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4007 elf_tdata (abfd
)->program_header_size
= count
* bed
->s
->sizeof_phdr
;
4012 if (sections
!= NULL
)
4017 /* Sort sections by address. */
4020 elf_sort_sections (const void *arg1
, const void *arg2
)
4022 const asection
*sec1
= *(const asection
**) arg1
;
4023 const asection
*sec2
= *(const asection
**) arg2
;
4024 bfd_size_type size1
, size2
;
4026 /* Sort by LMA first, since this is the address used to
4027 place the section into a segment. */
4028 if (sec1
->lma
< sec2
->lma
)
4030 else if (sec1
->lma
> sec2
->lma
)
4033 /* Then sort by VMA. Normally the LMA and the VMA will be
4034 the same, and this will do nothing. */
4035 if (sec1
->vma
< sec2
->vma
)
4037 else if (sec1
->vma
> sec2
->vma
)
4040 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4042 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4048 /* If the indicies are the same, do not return 0
4049 here, but continue to try the next comparison. */
4050 if (sec1
->target_index
- sec2
->target_index
!= 0)
4051 return sec1
->target_index
- sec2
->target_index
;
4056 else if (TOEND (sec2
))
4061 /* Sort by size, to put zero sized sections
4062 before others at the same address. */
4064 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4065 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4072 return sec1
->target_index
- sec2
->target_index
;
4075 /* Ian Lance Taylor writes:
4077 We shouldn't be using % with a negative signed number. That's just
4078 not good. We have to make sure either that the number is not
4079 negative, or that the number has an unsigned type. When the types
4080 are all the same size they wind up as unsigned. When file_ptr is a
4081 larger signed type, the arithmetic winds up as signed long long,
4084 What we're trying to say here is something like ``increase OFF by
4085 the least amount that will cause it to be equal to the VMA modulo
4087 /* In other words, something like:
4089 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4090 off_offset = off % bed->maxpagesize;
4091 if (vma_offset < off_offset)
4092 adjustment = vma_offset + bed->maxpagesize - off_offset;
4094 adjustment = vma_offset - off_offset;
4096 which can can be collapsed into the expression below. */
4099 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4101 return ((vma
- off
) % maxpagesize
);
4105 print_segment_map (const struct elf_segment_map
*m
)
4108 const char *pt
= get_segment_type (m
->p_type
);
4113 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4114 sprintf (buf
, "LOPROC+%7.7x",
4115 (unsigned int) (m
->p_type
- PT_LOPROC
));
4116 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4117 sprintf (buf
, "LOOS+%7.7x",
4118 (unsigned int) (m
->p_type
- PT_LOOS
));
4120 snprintf (buf
, sizeof (buf
), "%8.8x",
4121 (unsigned int) m
->p_type
);
4124 fprintf (stderr
, "%s:", pt
);
4125 for (j
= 0; j
< m
->count
; j
++)
4126 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4131 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4136 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4138 buf
= bfd_zmalloc (len
);
4141 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4146 /* Assign file positions to the sections based on the mapping from
4147 sections to segments. This function also sets up some fields in
4151 assign_file_positions_for_load_sections (bfd
*abfd
,
4152 struct bfd_link_info
*link_info
)
4154 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4155 struct elf_segment_map
*m
;
4156 Elf_Internal_Phdr
*phdrs
;
4157 Elf_Internal_Phdr
*p
;
4159 bfd_size_type maxpagesize
;
4162 bfd_vma header_pad
= 0;
4164 if (link_info
== NULL
4165 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4169 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4173 header_pad
= m
->header_size
;
4176 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4177 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4178 elf_elfheader (abfd
)->e_phnum
= alloc
;
4180 if (elf_tdata (abfd
)->program_header_size
== (bfd_size_type
) -1)
4181 elf_tdata (abfd
)->program_header_size
= alloc
* bed
->s
->sizeof_phdr
;
4183 BFD_ASSERT (elf_tdata (abfd
)->program_header_size
4184 >= alloc
* bed
->s
->sizeof_phdr
);
4188 elf_tdata (abfd
)->next_file_pos
= bed
->s
->sizeof_ehdr
;
4192 /* We're writing the size in elf_tdata (abfd)->program_header_size,
4193 see assign_file_positions_except_relocs, so make sure we have
4194 that amount allocated, with trailing space cleared.
4195 The variable alloc contains the computed need, while elf_tdata
4196 (abfd)->program_header_size contains the size used for the
4198 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4199 where the layout is forced to according to a larger size in the
4200 last iterations for the testcase ld-elf/header. */
4201 BFD_ASSERT (elf_tdata (abfd
)->program_header_size
% bed
->s
->sizeof_phdr
4203 phdrs
= (Elf_Internal_Phdr
*)
4205 (elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
),
4206 sizeof (Elf_Internal_Phdr
));
4207 elf_tdata (abfd
)->phdr
= phdrs
;
4212 if ((abfd
->flags
& D_PAGED
) != 0)
4213 maxpagesize
= bed
->maxpagesize
;
4215 off
= bed
->s
->sizeof_ehdr
;
4216 off
+= alloc
* bed
->s
->sizeof_phdr
;
4217 if (header_pad
< (bfd_vma
) off
)
4223 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
, j
= 0;
4225 m
= m
->next
, p
++, j
++)
4229 bfd_boolean no_contents
;
4231 /* If elf_segment_map is not from map_sections_to_segments, the
4232 sections may not be correctly ordered. NOTE: sorting should
4233 not be done to the PT_NOTE section of a corefile, which may
4234 contain several pseudo-sections artificially created by bfd.
4235 Sorting these pseudo-sections breaks things badly. */
4237 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4238 && m
->p_type
== PT_NOTE
))
4239 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4242 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4243 number of sections with contents contributing to both p_filesz
4244 and p_memsz, followed by a number of sections with no contents
4245 that just contribute to p_memsz. In this loop, OFF tracks next
4246 available file offset for PT_LOAD and PT_NOTE segments. */
4247 p
->p_type
= m
->p_type
;
4248 p
->p_flags
= m
->p_flags
;
4253 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4255 if (m
->p_paddr_valid
)
4256 p
->p_paddr
= m
->p_paddr
;
4257 else if (m
->count
== 0)
4260 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4262 if (p
->p_type
== PT_LOAD
4263 && (abfd
->flags
& D_PAGED
) != 0)
4265 /* p_align in demand paged PT_LOAD segments effectively stores
4266 the maximum page size. When copying an executable with
4267 objcopy, we set m->p_align from the input file. Use this
4268 value for maxpagesize rather than bed->maxpagesize, which
4269 may be different. Note that we use maxpagesize for PT_TLS
4270 segment alignment later in this function, so we are relying
4271 on at least one PT_LOAD segment appearing before a PT_TLS
4273 if (m
->p_align_valid
)
4274 maxpagesize
= m
->p_align
;
4276 p
->p_align
= maxpagesize
;
4278 else if (m
->p_align_valid
)
4279 p
->p_align
= m
->p_align
;
4280 else if (m
->count
== 0)
4281 p
->p_align
= 1 << bed
->s
->log_file_align
;
4285 no_contents
= FALSE
;
4287 if (p
->p_type
== PT_LOAD
4290 bfd_size_type align
;
4291 unsigned int align_power
= 0;
4293 if (m
->p_align_valid
)
4297 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4299 unsigned int secalign
;
4301 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4302 if (secalign
> align_power
)
4303 align_power
= secalign
;
4305 align
= (bfd_size_type
) 1 << align_power
;
4306 if (align
< maxpagesize
)
4307 align
= maxpagesize
;
4310 for (i
= 0; i
< m
->count
; i
++)
4311 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4312 /* If we aren't making room for this section, then
4313 it must be SHT_NOBITS regardless of what we've
4314 set via struct bfd_elf_special_section. */
4315 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4317 /* Find out whether this segment contains any loadable
4320 for (i
= 0; i
< m
->count
; i
++)
4321 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4323 no_contents
= FALSE
;
4327 off_adjust
= vma_page_aligned_bias (m
->sections
[0]->vma
, off
, align
);
4331 /* We shouldn't need to align the segment on disk since
4332 the segment doesn't need file space, but the gABI
4333 arguably requires the alignment and glibc ld.so
4334 checks it. So to comply with the alignment
4335 requirement but not waste file space, we adjust
4336 p_offset for just this segment. (OFF_ADJUST is
4337 subtracted from OFF later.) This may put p_offset
4338 past the end of file, but that shouldn't matter. */
4343 /* Make sure the .dynamic section is the first section in the
4344 PT_DYNAMIC segment. */
4345 else if (p
->p_type
== PT_DYNAMIC
4347 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4350 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4352 bfd_set_error (bfd_error_bad_value
);
4355 /* Set the note section type to SHT_NOTE. */
4356 else if (p
->p_type
== PT_NOTE
)
4357 for (i
= 0; i
< m
->count
; i
++)
4358 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4364 if (m
->includes_filehdr
)
4366 if (!m
->p_flags_valid
)
4368 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4369 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4372 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4374 if (p
->p_vaddr
< (bfd_vma
) off
)
4376 (*_bfd_error_handler
)
4377 (_("%B: Not enough room for program headers, try linking with -N"),
4379 bfd_set_error (bfd_error_bad_value
);
4384 if (!m
->p_paddr_valid
)
4389 if (m
->includes_phdrs
)
4391 if (!m
->p_flags_valid
)
4394 if (!m
->includes_filehdr
)
4396 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4400 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4401 p
->p_vaddr
-= off
- p
->p_offset
;
4402 if (!m
->p_paddr_valid
)
4403 p
->p_paddr
-= off
- p
->p_offset
;
4407 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4408 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4411 p
->p_filesz
+= header_pad
;
4412 p
->p_memsz
+= header_pad
;
4416 if (p
->p_type
== PT_LOAD
4417 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4419 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4425 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4427 p
->p_filesz
+= adjust
;
4428 p
->p_memsz
+= adjust
;
4432 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4433 maps. Set filepos for sections in PT_LOAD segments, and in
4434 core files, for sections in PT_NOTE segments.
4435 assign_file_positions_for_non_load_sections will set filepos
4436 for other sections and update p_filesz for other segments. */
4437 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4440 bfd_size_type align
;
4441 Elf_Internal_Shdr
*this_hdr
;
4444 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4445 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4447 if ((p
->p_type
== PT_LOAD
4448 || p
->p_type
== PT_TLS
)
4449 && (this_hdr
->sh_type
!= SHT_NOBITS
4450 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4451 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4452 || p
->p_type
== PT_TLS
))))
4454 bfd_signed_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
4458 (*_bfd_error_handler
)
4459 (_("%B: section %A vma 0x%lx overlaps previous sections"),
4460 abfd
, sec
, (unsigned long) sec
->vma
);
4463 p
->p_memsz
+= adjust
;
4465 if (p
->p_paddr
+ p
->p_memsz
!= sec
->lma
)
4467 /* This behavior is a compromise--ld has long
4468 silently changed the lma of sections when
4469 lma - vma is not equal for every section in a
4470 pheader--but only in the internal elf structures.
4471 Silently changing the lma is probably a bug, but
4472 changing it would have subtle and unknown
4473 consequences for existing scripts.
4475 Instead modify the bfd data structure to reflect
4476 what happened. This at least fixes the values
4477 for the lma in the mapfile. */
4478 sec
->lma
= p
->p_paddr
+ p
->p_memsz
;
4481 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4483 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
4485 /* We have a PROGBITS section following NOBITS ones.
4486 Allocate file space for the NOBITS section(s) and
4488 adjust
= p
->p_memsz
- p
->p_filesz
;
4489 if (!write_zeros (abfd
, off
, adjust
))
4493 p
->p_filesz
+= adjust
;
4497 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4499 /* The section at i == 0 is the one that actually contains
4503 this_hdr
->sh_offset
= sec
->filepos
= off
;
4504 off
+= this_hdr
->sh_size
;
4505 p
->p_filesz
= this_hdr
->sh_size
;
4511 /* The rest are fake sections that shouldn't be written. */
4520 if (p
->p_type
== PT_LOAD
)
4522 this_hdr
->sh_offset
= sec
->filepos
= off
;
4523 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4524 off
+= this_hdr
->sh_size
;
4527 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4529 p
->p_filesz
+= this_hdr
->sh_size
;
4530 /* A load section without SHF_ALLOC is something like
4531 a note section in a PT_NOTE segment. These take
4532 file space but are not loaded into memory. */
4533 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4534 p
->p_memsz
+= this_hdr
->sh_size
;
4536 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4538 if (p
->p_type
== PT_TLS
)
4539 p
->p_memsz
+= this_hdr
->sh_size
;
4541 /* .tbss is special. It doesn't contribute to p_memsz of
4543 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4544 p
->p_memsz
+= this_hdr
->sh_size
;
4547 if (align
> p
->p_align
4548 && !m
->p_align_valid
4549 && (p
->p_type
!= PT_LOAD
4550 || (abfd
->flags
& D_PAGED
) == 0))
4554 if (!m
->p_flags_valid
)
4557 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4559 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4565 /* Check that all sections are in a PT_LOAD segment.
4566 Don't check funky gdb generated core files. */
4567 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4568 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4570 Elf_Internal_Shdr
*this_hdr
;
4574 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4575 if (this_hdr
->sh_size
!= 0
4576 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, p
))
4578 (*_bfd_error_handler
)
4579 (_("%B: section `%A' can't be allocated in segment %d"),
4581 print_segment_map (m
);
4582 bfd_set_error (bfd_error_bad_value
);
4588 elf_tdata (abfd
)->next_file_pos
= off
;
4592 /* Assign file positions for the other sections. */
4595 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4596 struct bfd_link_info
*link_info
)
4598 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4599 Elf_Internal_Shdr
**i_shdrpp
;
4600 Elf_Internal_Shdr
**hdrpp
;
4601 Elf_Internal_Phdr
*phdrs
;
4602 Elf_Internal_Phdr
*p
;
4603 struct elf_segment_map
*m
;
4604 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4605 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4607 unsigned int num_sec
;
4611 i_shdrpp
= elf_elfsections (abfd
);
4612 num_sec
= elf_numsections (abfd
);
4613 off
= elf_tdata (abfd
)->next_file_pos
;
4614 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4616 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4617 Elf_Internal_Shdr
*hdr
;
4620 if (hdr
->bfd_section
!= NULL
4621 && (hdr
->bfd_section
->filepos
!= 0
4622 || (hdr
->sh_type
== SHT_NOBITS
4623 && hdr
->contents
== NULL
)))
4624 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4625 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4627 if (hdr
->sh_size
!= 0)
4628 ((*_bfd_error_handler
)
4629 (_("%B: warning: allocated section `%s' not in segment"),
4631 (hdr
->bfd_section
== NULL
4633 : hdr
->bfd_section
->name
)));
4634 /* We don't need to page align empty sections. */
4635 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4636 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4639 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4641 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4644 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4645 && hdr
->bfd_section
== NULL
)
4646 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4647 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4648 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4649 hdr
->sh_offset
= -1;
4651 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4654 /* Now that we have set the section file positions, we can set up
4655 the file positions for the non PT_LOAD segments. */
4659 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4661 phdrs
= elf_tdata (abfd
)->phdr
;
4662 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4667 if (p
->p_type
!= PT_LOAD
)
4670 if (m
->includes_filehdr
)
4672 filehdr_vaddr
= p
->p_vaddr
;
4673 filehdr_paddr
= p
->p_paddr
;
4675 if (m
->includes_phdrs
)
4677 phdrs_vaddr
= p
->p_vaddr
;
4678 phdrs_paddr
= p
->p_paddr
;
4679 if (m
->includes_filehdr
)
4681 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
4682 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
4687 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4691 if (p
->p_type
== PT_GNU_RELRO
)
4693 const Elf_Internal_Phdr
*lp
;
4695 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
4697 if (link_info
!= NULL
)
4699 /* During linking the range of the RELRO segment is passed
4701 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4703 if (lp
->p_type
== PT_LOAD
4704 && lp
->p_vaddr
>= link_info
->relro_start
4705 && lp
->p_vaddr
< link_info
->relro_end
4706 && lp
->p_vaddr
+ lp
->p_filesz
>= link_info
->relro_end
)
4712 /* Otherwise we are copying an executable or shared
4713 library, but we need to use the same linker logic. */
4714 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4716 if (lp
->p_type
== PT_LOAD
4717 && lp
->p_paddr
== p
->p_paddr
)
4722 if (lp
< phdrs
+ count
)
4724 p
->p_vaddr
= lp
->p_vaddr
;
4725 p
->p_paddr
= lp
->p_paddr
;
4726 p
->p_offset
= lp
->p_offset
;
4727 if (link_info
!= NULL
)
4728 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
4729 else if (m
->p_size_valid
)
4730 p
->p_filesz
= m
->p_size
;
4733 p
->p_memsz
= p
->p_filesz
;
4735 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
4739 memset (p
, 0, sizeof *p
);
4740 p
->p_type
= PT_NULL
;
4743 else if (m
->count
!= 0)
4745 if (p
->p_type
!= PT_LOAD
4746 && (p
->p_type
!= PT_NOTE
4747 || bfd_get_format (abfd
) != bfd_core
))
4749 Elf_Internal_Shdr
*hdr
;
4752 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
4754 sect
= m
->sections
[m
->count
- 1];
4755 hdr
= &elf_section_data (sect
)->this_hdr
;
4756 p
->p_filesz
= sect
->filepos
- m
->sections
[0]->filepos
;
4757 if (hdr
->sh_type
!= SHT_NOBITS
)
4758 p
->p_filesz
+= hdr
->sh_size
;
4759 p
->p_offset
= m
->sections
[0]->filepos
;
4762 else if (m
->includes_filehdr
)
4764 p
->p_vaddr
= filehdr_vaddr
;
4765 if (! m
->p_paddr_valid
)
4766 p
->p_paddr
= filehdr_paddr
;
4768 else if (m
->includes_phdrs
)
4770 p
->p_vaddr
= phdrs_vaddr
;
4771 if (! m
->p_paddr_valid
)
4772 p
->p_paddr
= phdrs_paddr
;
4776 elf_tdata (abfd
)->next_file_pos
= off
;
4781 /* Work out the file positions of all the sections. This is called by
4782 _bfd_elf_compute_section_file_positions. All the section sizes and
4783 VMAs must be known before this is called.
4785 Reloc sections come in two flavours: Those processed specially as
4786 "side-channel" data attached to a section to which they apply, and
4787 those that bfd doesn't process as relocations. The latter sort are
4788 stored in a normal bfd section by bfd_section_from_shdr. We don't
4789 consider the former sort here, unless they form part of the loadable
4790 image. Reloc sections not assigned here will be handled later by
4791 assign_file_positions_for_relocs.
4793 We also don't set the positions of the .symtab and .strtab here. */
4796 assign_file_positions_except_relocs (bfd
*abfd
,
4797 struct bfd_link_info
*link_info
)
4799 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4800 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
4802 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4804 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4805 && bfd_get_format (abfd
) != bfd_core
)
4807 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4808 unsigned int num_sec
= elf_numsections (abfd
);
4809 Elf_Internal_Shdr
**hdrpp
;
4812 /* Start after the ELF header. */
4813 off
= i_ehdrp
->e_ehsize
;
4815 /* We are not creating an executable, which means that we are
4816 not creating a program header, and that the actual order of
4817 the sections in the file is unimportant. */
4818 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4820 Elf_Internal_Shdr
*hdr
;
4823 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4824 && hdr
->bfd_section
== NULL
)
4825 || i
== tdata
->symtab_section
4826 || i
== tdata
->symtab_shndx_section
4827 || i
== tdata
->strtab_section
)
4829 hdr
->sh_offset
= -1;
4832 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4839 /* Assign file positions for the loaded sections based on the
4840 assignment of sections to segments. */
4841 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
4844 /* And for non-load sections. */
4845 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
4848 if (bed
->elf_backend_modify_program_headers
!= NULL
)
4850 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
4854 /* Write out the program headers. */
4855 alloc
= tdata
->program_header_size
/ bed
->s
->sizeof_phdr
;
4856 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4857 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
4860 off
= tdata
->next_file_pos
;
4863 /* Place the section headers. */
4864 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
4865 i_ehdrp
->e_shoff
= off
;
4866 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4868 tdata
->next_file_pos
= off
;
4874 prep_headers (bfd
*abfd
)
4876 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4877 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4878 struct elf_strtab_hash
*shstrtab
;
4879 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4881 i_ehdrp
= elf_elfheader (abfd
);
4883 shstrtab
= _bfd_elf_strtab_init ();
4884 if (shstrtab
== NULL
)
4887 elf_shstrtab (abfd
) = shstrtab
;
4889 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4890 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4891 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4892 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4894 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4895 i_ehdrp
->e_ident
[EI_DATA
] =
4896 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4897 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4899 if ((abfd
->flags
& DYNAMIC
) != 0)
4900 i_ehdrp
->e_type
= ET_DYN
;
4901 else if ((abfd
->flags
& EXEC_P
) != 0)
4902 i_ehdrp
->e_type
= ET_EXEC
;
4903 else if (bfd_get_format (abfd
) == bfd_core
)
4904 i_ehdrp
->e_type
= ET_CORE
;
4906 i_ehdrp
->e_type
= ET_REL
;
4908 switch (bfd_get_arch (abfd
))
4910 case bfd_arch_unknown
:
4911 i_ehdrp
->e_machine
= EM_NONE
;
4914 /* There used to be a long list of cases here, each one setting
4915 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4916 in the corresponding bfd definition. To avoid duplication,
4917 the switch was removed. Machines that need special handling
4918 can generally do it in elf_backend_final_write_processing(),
4919 unless they need the information earlier than the final write.
4920 Such need can generally be supplied by replacing the tests for
4921 e_machine with the conditions used to determine it. */
4923 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
4926 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4927 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4929 /* No program header, for now. */
4930 i_ehdrp
->e_phoff
= 0;
4931 i_ehdrp
->e_phentsize
= 0;
4932 i_ehdrp
->e_phnum
= 0;
4934 /* Each bfd section is section header entry. */
4935 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4936 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4938 /* If we're building an executable, we'll need a program header table. */
4939 if (abfd
->flags
& EXEC_P
)
4940 /* It all happens later. */
4944 i_ehdrp
->e_phentsize
= 0;
4946 i_ehdrp
->e_phoff
= 0;
4949 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4950 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
4951 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4952 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
4953 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4954 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
4955 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4956 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4957 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4963 /* Assign file positions for all the reloc sections which are not part
4964 of the loadable file image. */
4967 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
4970 unsigned int i
, num_sec
;
4971 Elf_Internal_Shdr
**shdrpp
;
4973 off
= elf_tdata (abfd
)->next_file_pos
;
4975 num_sec
= elf_numsections (abfd
);
4976 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4978 Elf_Internal_Shdr
*shdrp
;
4981 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4982 && shdrp
->sh_offset
== -1)
4983 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
4986 elf_tdata (abfd
)->next_file_pos
= off
;
4990 _bfd_elf_write_object_contents (bfd
*abfd
)
4992 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4993 Elf_Internal_Ehdr
*i_ehdrp
;
4994 Elf_Internal_Shdr
**i_shdrp
;
4996 unsigned int count
, num_sec
;
4998 if (! abfd
->output_has_begun
4999 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5002 i_shdrp
= elf_elfsections (abfd
);
5003 i_ehdrp
= elf_elfheader (abfd
);
5006 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5010 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5012 /* After writing the headers, we need to write the sections too... */
5013 num_sec
= elf_numsections (abfd
);
5014 for (count
= 1; count
< num_sec
; count
++)
5016 if (bed
->elf_backend_section_processing
)
5017 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5018 if (i_shdrp
[count
]->contents
)
5020 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5022 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5023 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5028 /* Write out the section header names. */
5029 if (elf_shstrtab (abfd
) != NULL
5030 && (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5031 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5034 if (bed
->elf_backend_final_write_processing
)
5035 (*bed
->elf_backend_final_write_processing
) (abfd
,
5036 elf_tdata (abfd
)->linker
);
5038 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5041 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5042 if (elf_tdata (abfd
)->after_write_object_contents
)
5043 return (*elf_tdata (abfd
)->after_write_object_contents
) (abfd
);
5049 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5051 /* Hopefully this can be done just like an object file. */
5052 return _bfd_elf_write_object_contents (abfd
);
5055 /* Given a section, search the header to find them. */
5058 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5060 const struct elf_backend_data
*bed
;
5061 unsigned int sec_index
;
5063 if (elf_section_data (asect
) != NULL
5064 && elf_section_data (asect
)->this_idx
!= 0)
5065 return elf_section_data (asect
)->this_idx
;
5067 if (bfd_is_abs_section (asect
))
5068 sec_index
= SHN_ABS
;
5069 else if (bfd_is_com_section (asect
))
5070 sec_index
= SHN_COMMON
;
5071 else if (bfd_is_und_section (asect
))
5072 sec_index
= SHN_UNDEF
;
5074 sec_index
= SHN_BAD
;
5076 bed
= get_elf_backend_data (abfd
);
5077 if (bed
->elf_backend_section_from_bfd_section
)
5079 int retval
= sec_index
;
5081 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5085 if (sec_index
== SHN_BAD
)
5086 bfd_set_error (bfd_error_nonrepresentable_section
);
5091 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5095 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5097 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5099 flagword flags
= asym_ptr
->flags
;
5101 /* When gas creates relocations against local labels, it creates its
5102 own symbol for the section, but does put the symbol into the
5103 symbol chain, so udata is 0. When the linker is generating
5104 relocatable output, this section symbol may be for one of the
5105 input sections rather than the output section. */
5106 if (asym_ptr
->udata
.i
== 0
5107 && (flags
& BSF_SECTION_SYM
)
5108 && asym_ptr
->section
)
5113 sec
= asym_ptr
->section
;
5114 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5115 sec
= sec
->output_section
;
5116 if (sec
->owner
== abfd
5117 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5118 && elf_section_syms (abfd
)[indx
] != NULL
)
5119 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5122 idx
= asym_ptr
->udata
.i
;
5126 /* This case can occur when using --strip-symbol on a symbol
5127 which is used in a relocation entry. */
5128 (*_bfd_error_handler
)
5129 (_("%B: symbol `%s' required but not present"),
5130 abfd
, bfd_asymbol_name (asym_ptr
));
5131 bfd_set_error (bfd_error_no_symbols
);
5138 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5139 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
5140 elf_symbol_flags (flags
));
5148 /* Rewrite program header information. */
5151 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5153 Elf_Internal_Ehdr
*iehdr
;
5154 struct elf_segment_map
*map
;
5155 struct elf_segment_map
*map_first
;
5156 struct elf_segment_map
**pointer_to_map
;
5157 Elf_Internal_Phdr
*segment
;
5160 unsigned int num_segments
;
5161 bfd_boolean phdr_included
= FALSE
;
5162 bfd_boolean p_paddr_valid
;
5163 bfd_vma maxpagesize
;
5164 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5165 unsigned int phdr_adjust_num
= 0;
5166 const struct elf_backend_data
*bed
;
5168 bed
= get_elf_backend_data (ibfd
);
5169 iehdr
= elf_elfheader (ibfd
);
5172 pointer_to_map
= &map_first
;
5174 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5175 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5177 /* Returns the end address of the segment + 1. */
5178 #define SEGMENT_END(segment, start) \
5179 (start + (segment->p_memsz > segment->p_filesz \
5180 ? segment->p_memsz : segment->p_filesz))
5182 #define SECTION_SIZE(section, segment) \
5183 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5184 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5185 ? section->size : 0)
5187 /* Returns TRUE if the given section is contained within
5188 the given segment. VMA addresses are compared. */
5189 #define IS_CONTAINED_BY_VMA(section, segment) \
5190 (section->vma >= segment->p_vaddr \
5191 && (section->vma + SECTION_SIZE (section, segment) \
5192 <= (SEGMENT_END (segment, segment->p_vaddr))))
5194 /* Returns TRUE if the given section is contained within
5195 the given segment. LMA addresses are compared. */
5196 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5197 (section->lma >= base \
5198 && (section->lma + SECTION_SIZE (section, segment) \
5199 <= SEGMENT_END (segment, base)))
5201 /* Handle PT_NOTE segment. */
5202 #define IS_NOTE(p, s) \
5203 (p->p_type == PT_NOTE \
5204 && elf_section_type (s) == SHT_NOTE \
5205 && (bfd_vma) s->filepos >= p->p_offset \
5206 && ((bfd_vma) s->filepos + s->size \
5207 <= p->p_offset + p->p_filesz))
5209 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5211 #define IS_COREFILE_NOTE(p, s) \
5213 && bfd_get_format (ibfd) == bfd_core \
5217 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5218 linker, which generates a PT_INTERP section with p_vaddr and
5219 p_memsz set to 0. */
5220 #define IS_SOLARIS_PT_INTERP(p, s) \
5222 && p->p_paddr == 0 \
5223 && p->p_memsz == 0 \
5224 && p->p_filesz > 0 \
5225 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5227 && (bfd_vma) s->filepos >= p->p_offset \
5228 && ((bfd_vma) s->filepos + s->size \
5229 <= p->p_offset + p->p_filesz))
5231 /* Decide if the given section should be included in the given segment.
5232 A section will be included if:
5233 1. It is within the address space of the segment -- we use the LMA
5234 if that is set for the segment and the VMA otherwise,
5235 2. It is an allocated section or a NOTE section in a PT_NOTE
5237 3. There is an output section associated with it,
5238 4. The section has not already been allocated to a previous segment.
5239 5. PT_GNU_STACK segments do not include any sections.
5240 6. PT_TLS segment includes only SHF_TLS sections.
5241 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5242 8. PT_DYNAMIC should not contain empty sections at the beginning
5243 (with the possible exception of .dynamic). */
5244 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5245 ((((segment->p_paddr \
5246 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5247 : IS_CONTAINED_BY_VMA (section, segment)) \
5248 && (section->flags & SEC_ALLOC) != 0) \
5249 || IS_NOTE (segment, section)) \
5250 && segment->p_type != PT_GNU_STACK \
5251 && (segment->p_type != PT_TLS \
5252 || (section->flags & SEC_THREAD_LOCAL)) \
5253 && (segment->p_type == PT_LOAD \
5254 || segment->p_type == PT_TLS \
5255 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5256 && (segment->p_type != PT_DYNAMIC \
5257 || SECTION_SIZE (section, segment) > 0 \
5258 || (segment->p_paddr \
5259 ? segment->p_paddr != section->lma \
5260 : segment->p_vaddr != section->vma) \
5261 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5263 && !section->segment_mark)
5265 /* If the output section of a section in the input segment is NULL,
5266 it is removed from the corresponding output segment. */
5267 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5268 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5269 && section->output_section != NULL)
5271 /* Returns TRUE iff seg1 starts after the end of seg2. */
5272 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5273 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5275 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5276 their VMA address ranges and their LMA address ranges overlap.
5277 It is possible to have overlapping VMA ranges without overlapping LMA
5278 ranges. RedBoot images for example can have both .data and .bss mapped
5279 to the same VMA range, but with the .data section mapped to a different
5281 #define SEGMENT_OVERLAPS(seg1, seg2) \
5282 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5283 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5284 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5285 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5287 /* Initialise the segment mark field. */
5288 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5289 section
->segment_mark
= FALSE
;
5291 /* The Solaris linker creates program headers in which all the
5292 p_paddr fields are zero. When we try to objcopy or strip such a
5293 file, we get confused. Check for this case, and if we find it
5294 don't set the p_paddr_valid fields. */
5295 p_paddr_valid
= FALSE
;
5296 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5299 if (segment
->p_paddr
!= 0)
5301 p_paddr_valid
= TRUE
;
5305 /* Scan through the segments specified in the program header
5306 of the input BFD. For this first scan we look for overlaps
5307 in the loadable segments. These can be created by weird
5308 parameters to objcopy. Also, fix some solaris weirdness. */
5309 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5314 Elf_Internal_Phdr
*segment2
;
5316 if (segment
->p_type
== PT_INTERP
)
5317 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5318 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5320 /* Mininal change so that the normal section to segment
5321 assignment code will work. */
5322 segment
->p_vaddr
= section
->vma
;
5326 if (segment
->p_type
!= PT_LOAD
)
5328 /* Remove PT_GNU_RELRO segment. */
5329 if (segment
->p_type
== PT_GNU_RELRO
)
5330 segment
->p_type
= PT_NULL
;
5334 /* Determine if this segment overlaps any previous segments. */
5335 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5337 bfd_signed_vma extra_length
;
5339 if (segment2
->p_type
!= PT_LOAD
5340 || !SEGMENT_OVERLAPS (segment
, segment2
))
5343 /* Merge the two segments together. */
5344 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5346 /* Extend SEGMENT2 to include SEGMENT and then delete
5348 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5349 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5351 if (extra_length
> 0)
5353 segment2
->p_memsz
+= extra_length
;
5354 segment2
->p_filesz
+= extra_length
;
5357 segment
->p_type
= PT_NULL
;
5359 /* Since we have deleted P we must restart the outer loop. */
5361 segment
= elf_tdata (ibfd
)->phdr
;
5366 /* Extend SEGMENT to include SEGMENT2 and then delete
5368 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5369 - SEGMENT_END (segment
, segment
->p_vaddr
));
5371 if (extra_length
> 0)
5373 segment
->p_memsz
+= extra_length
;
5374 segment
->p_filesz
+= extra_length
;
5377 segment2
->p_type
= PT_NULL
;
5382 /* The second scan attempts to assign sections to segments. */
5383 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5387 unsigned int section_count
;
5388 asection
**sections
;
5389 asection
*output_section
;
5391 bfd_vma matching_lma
;
5392 bfd_vma suggested_lma
;
5395 asection
*first_section
;
5396 bfd_boolean first_matching_lma
;
5397 bfd_boolean first_suggested_lma
;
5399 if (segment
->p_type
== PT_NULL
)
5402 first_section
= NULL
;
5403 /* Compute how many sections might be placed into this segment. */
5404 for (section
= ibfd
->sections
, section_count
= 0;
5406 section
= section
->next
)
5408 /* Find the first section in the input segment, which may be
5409 removed from the corresponding output segment. */
5410 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5412 if (first_section
== NULL
)
5413 first_section
= section
;
5414 if (section
->output_section
!= NULL
)
5419 /* Allocate a segment map big enough to contain
5420 all of the sections we have selected. */
5421 amt
= sizeof (struct elf_segment_map
);
5422 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5423 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
5427 /* Initialise the fields of the segment map. Default to
5428 using the physical address of the segment in the input BFD. */
5430 map
->p_type
= segment
->p_type
;
5431 map
->p_flags
= segment
->p_flags
;
5432 map
->p_flags_valid
= 1;
5434 /* If the first section in the input segment is removed, there is
5435 no need to preserve segment physical address in the corresponding
5437 if (!first_section
|| first_section
->output_section
!= NULL
)
5439 map
->p_paddr
= segment
->p_paddr
;
5440 map
->p_paddr_valid
= p_paddr_valid
;
5443 /* Determine if this segment contains the ELF file header
5444 and if it contains the program headers themselves. */
5445 map
->includes_filehdr
= (segment
->p_offset
== 0
5446 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5447 map
->includes_phdrs
= 0;
5449 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5451 map
->includes_phdrs
=
5452 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5453 && (segment
->p_offset
+ segment
->p_filesz
5454 >= ((bfd_vma
) iehdr
->e_phoff
5455 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5457 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5458 phdr_included
= TRUE
;
5461 if (section_count
== 0)
5463 /* Special segments, such as the PT_PHDR segment, may contain
5464 no sections, but ordinary, loadable segments should contain
5465 something. They are allowed by the ELF spec however, so only
5466 a warning is produced. */
5467 if (segment
->p_type
== PT_LOAD
)
5468 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5469 " detected, is this intentional ?\n"),
5473 *pointer_to_map
= map
;
5474 pointer_to_map
= &map
->next
;
5479 /* Now scan the sections in the input BFD again and attempt
5480 to add their corresponding output sections to the segment map.
5481 The problem here is how to handle an output section which has
5482 been moved (ie had its LMA changed). There are four possibilities:
5484 1. None of the sections have been moved.
5485 In this case we can continue to use the segment LMA from the
5488 2. All of the sections have been moved by the same amount.
5489 In this case we can change the segment's LMA to match the LMA
5490 of the first section.
5492 3. Some of the sections have been moved, others have not.
5493 In this case those sections which have not been moved can be
5494 placed in the current segment which will have to have its size,
5495 and possibly its LMA changed, and a new segment or segments will
5496 have to be created to contain the other sections.
5498 4. The sections have been moved, but not by the same amount.
5499 In this case we can change the segment's LMA to match the LMA
5500 of the first section and we will have to create a new segment
5501 or segments to contain the other sections.
5503 In order to save time, we allocate an array to hold the section
5504 pointers that we are interested in. As these sections get assigned
5505 to a segment, they are removed from this array. */
5507 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
5508 if (sections
== NULL
)
5511 /* Step One: Scan for segment vs section LMA conflicts.
5512 Also add the sections to the section array allocated above.
5513 Also add the sections to the current segment. In the common
5514 case, where the sections have not been moved, this means that
5515 we have completely filled the segment, and there is nothing
5520 first_matching_lma
= TRUE
;
5521 first_suggested_lma
= TRUE
;
5523 for (section
= ibfd
->sections
;
5525 section
= section
->next
)
5526 if (section
== first_section
)
5529 for (j
= 0; section
!= NULL
; section
= section
->next
)
5531 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5533 output_section
= section
->output_section
;
5535 sections
[j
++] = section
;
5537 /* The Solaris native linker always sets p_paddr to 0.
5538 We try to catch that case here, and set it to the
5539 correct value. Note - some backends require that
5540 p_paddr be left as zero. */
5542 && segment
->p_vaddr
!= 0
5543 && !bed
->want_p_paddr_set_to_zero
5545 && output_section
->lma
!= 0
5546 && output_section
->vma
== (segment
->p_vaddr
5547 + (map
->includes_filehdr
5550 + (map
->includes_phdrs
5552 * iehdr
->e_phentsize
)
5554 map
->p_paddr
= segment
->p_vaddr
;
5556 /* Match up the physical address of the segment with the
5557 LMA address of the output section. */
5558 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5559 || IS_COREFILE_NOTE (segment
, section
)
5560 || (bed
->want_p_paddr_set_to_zero
5561 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
5563 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
5565 matching_lma
= output_section
->lma
;
5566 first_matching_lma
= FALSE
;
5569 /* We assume that if the section fits within the segment
5570 then it does not overlap any other section within that
5572 map
->sections
[isec
++] = output_section
;
5574 else if (first_suggested_lma
)
5576 suggested_lma
= output_section
->lma
;
5577 first_suggested_lma
= FALSE
;
5580 if (j
== section_count
)
5585 BFD_ASSERT (j
== section_count
);
5587 /* Step Two: Adjust the physical address of the current segment,
5589 if (isec
== section_count
)
5591 /* All of the sections fitted within the segment as currently
5592 specified. This is the default case. Add the segment to
5593 the list of built segments and carry on to process the next
5594 program header in the input BFD. */
5595 map
->count
= section_count
;
5596 *pointer_to_map
= map
;
5597 pointer_to_map
= &map
->next
;
5600 && !bed
->want_p_paddr_set_to_zero
5601 && matching_lma
!= map
->p_paddr
5602 && !map
->includes_filehdr
5603 && !map
->includes_phdrs
)
5604 /* There is some padding before the first section in the
5605 segment. So, we must account for that in the output
5607 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
5614 if (!first_matching_lma
)
5616 /* At least one section fits inside the current segment.
5617 Keep it, but modify its physical address to match the
5618 LMA of the first section that fitted. */
5619 map
->p_paddr
= matching_lma
;
5623 /* None of the sections fitted inside the current segment.
5624 Change the current segment's physical address to match
5625 the LMA of the first section. */
5626 map
->p_paddr
= suggested_lma
;
5629 /* Offset the segment physical address from the lma
5630 to allow for space taken up by elf headers. */
5631 if (map
->includes_filehdr
)
5633 if (map
->p_paddr
>= iehdr
->e_ehsize
)
5634 map
->p_paddr
-= iehdr
->e_ehsize
;
5637 map
->includes_filehdr
= FALSE
;
5638 map
->includes_phdrs
= FALSE
;
5642 if (map
->includes_phdrs
)
5644 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
5646 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5648 /* iehdr->e_phnum is just an estimate of the number
5649 of program headers that we will need. Make a note
5650 here of the number we used and the segment we chose
5651 to hold these headers, so that we can adjust the
5652 offset when we know the correct value. */
5653 phdr_adjust_num
= iehdr
->e_phnum
;
5654 phdr_adjust_seg
= map
;
5657 map
->includes_phdrs
= FALSE
;
5661 /* Step Three: Loop over the sections again, this time assigning
5662 those that fit to the current segment and removing them from the
5663 sections array; but making sure not to leave large gaps. Once all
5664 possible sections have been assigned to the current segment it is
5665 added to the list of built segments and if sections still remain
5666 to be assigned, a new segment is constructed before repeating
5673 first_suggested_lma
= TRUE
;
5675 /* Fill the current segment with sections that fit. */
5676 for (j
= 0; j
< section_count
; j
++)
5678 section
= sections
[j
];
5680 if (section
== NULL
)
5683 output_section
= section
->output_section
;
5685 BFD_ASSERT (output_section
!= NULL
);
5687 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5688 || IS_COREFILE_NOTE (segment
, section
))
5690 if (map
->count
== 0)
5692 /* If the first section in a segment does not start at
5693 the beginning of the segment, then something is
5695 if (output_section
->lma
5697 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5698 + (map
->includes_phdrs
5699 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5707 prev_sec
= map
->sections
[map
->count
- 1];
5709 /* If the gap between the end of the previous section
5710 and the start of this section is more than
5711 maxpagesize then we need to start a new segment. */
5712 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
5714 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5715 || (prev_sec
->lma
+ prev_sec
->size
5716 > output_section
->lma
))
5718 if (first_suggested_lma
)
5720 suggested_lma
= output_section
->lma
;
5721 first_suggested_lma
= FALSE
;
5728 map
->sections
[map
->count
++] = output_section
;
5731 section
->segment_mark
= TRUE
;
5733 else if (first_suggested_lma
)
5735 suggested_lma
= output_section
->lma
;
5736 first_suggested_lma
= FALSE
;
5740 BFD_ASSERT (map
->count
> 0);
5742 /* Add the current segment to the list of built segments. */
5743 *pointer_to_map
= map
;
5744 pointer_to_map
= &map
->next
;
5746 if (isec
< section_count
)
5748 /* We still have not allocated all of the sections to
5749 segments. Create a new segment here, initialise it
5750 and carry on looping. */
5751 amt
= sizeof (struct elf_segment_map
);
5752 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5753 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
5760 /* Initialise the fields of the segment map. Set the physical
5761 physical address to the LMA of the first section that has
5762 not yet been assigned. */
5764 map
->p_type
= segment
->p_type
;
5765 map
->p_flags
= segment
->p_flags
;
5766 map
->p_flags_valid
= 1;
5767 map
->p_paddr
= suggested_lma
;
5768 map
->p_paddr_valid
= p_paddr_valid
;
5769 map
->includes_filehdr
= 0;
5770 map
->includes_phdrs
= 0;
5773 while (isec
< section_count
);
5778 elf_tdata (obfd
)->segment_map
= map_first
;
5780 /* If we had to estimate the number of program headers that were
5781 going to be needed, then check our estimate now and adjust
5782 the offset if necessary. */
5783 if (phdr_adjust_seg
!= NULL
)
5787 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5790 if (count
> phdr_adjust_num
)
5791 phdr_adjust_seg
->p_paddr
5792 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5797 #undef IS_CONTAINED_BY_VMA
5798 #undef IS_CONTAINED_BY_LMA
5800 #undef IS_COREFILE_NOTE
5801 #undef IS_SOLARIS_PT_INTERP
5802 #undef IS_SECTION_IN_INPUT_SEGMENT
5803 #undef INCLUDE_SECTION_IN_SEGMENT
5804 #undef SEGMENT_AFTER_SEGMENT
5805 #undef SEGMENT_OVERLAPS
5809 /* Copy ELF program header information. */
5812 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5814 Elf_Internal_Ehdr
*iehdr
;
5815 struct elf_segment_map
*map
;
5816 struct elf_segment_map
*map_first
;
5817 struct elf_segment_map
**pointer_to_map
;
5818 Elf_Internal_Phdr
*segment
;
5820 unsigned int num_segments
;
5821 bfd_boolean phdr_included
= FALSE
;
5822 bfd_boolean p_paddr_valid
;
5824 iehdr
= elf_elfheader (ibfd
);
5827 pointer_to_map
= &map_first
;
5829 /* If all the segment p_paddr fields are zero, don't set
5830 map->p_paddr_valid. */
5831 p_paddr_valid
= FALSE
;
5832 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5833 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5836 if (segment
->p_paddr
!= 0)
5838 p_paddr_valid
= TRUE
;
5842 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5847 unsigned int section_count
;
5849 Elf_Internal_Shdr
*this_hdr
;
5850 asection
*first_section
= NULL
;
5851 asection
*lowest_section
= NULL
;
5853 /* Compute how many sections are in this segment. */
5854 for (section
= ibfd
->sections
, section_count
= 0;
5856 section
= section
->next
)
5858 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5859 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5862 first_section
= lowest_section
= section
;
5863 if (section
->lma
< lowest_section
->lma
)
5864 lowest_section
= section
;
5869 /* Allocate a segment map big enough to contain
5870 all of the sections we have selected. */
5871 amt
= sizeof (struct elf_segment_map
);
5872 if (section_count
!= 0)
5873 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5874 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
5878 /* Initialize the fields of the output segment map with the
5881 map
->p_type
= segment
->p_type
;
5882 map
->p_flags
= segment
->p_flags
;
5883 map
->p_flags_valid
= 1;
5884 map
->p_paddr
= segment
->p_paddr
;
5885 map
->p_paddr_valid
= p_paddr_valid
;
5886 map
->p_align
= segment
->p_align
;
5887 map
->p_align_valid
= 1;
5888 map
->p_vaddr_offset
= 0;
5890 if (map
->p_type
== PT_GNU_RELRO
)
5892 /* The PT_GNU_RELRO segment may contain the first a few
5893 bytes in the .got.plt section even if the whole .got.plt
5894 section isn't in the PT_GNU_RELRO segment. We won't
5895 change the size of the PT_GNU_RELRO segment. */
5896 map
->p_size
= segment
->p_memsz
;
5897 map
->p_size_valid
= 1;
5900 /* Determine if this segment contains the ELF file header
5901 and if it contains the program headers themselves. */
5902 map
->includes_filehdr
= (segment
->p_offset
== 0
5903 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5905 map
->includes_phdrs
= 0;
5906 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
5908 map
->includes_phdrs
=
5909 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5910 && (segment
->p_offset
+ segment
->p_filesz
5911 >= ((bfd_vma
) iehdr
->e_phoff
5912 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5914 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5915 phdr_included
= TRUE
;
5918 if (map
->includes_filehdr
&& first_section
)
5919 /* We need to keep the space used by the headers fixed. */
5920 map
->header_size
= first_section
->vma
- segment
->p_vaddr
;
5922 if (!map
->includes_phdrs
5923 && !map
->includes_filehdr
5924 && map
->p_paddr_valid
)
5925 /* There is some other padding before the first section. */
5926 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
5927 - segment
->p_paddr
);
5929 if (section_count
!= 0)
5931 unsigned int isec
= 0;
5933 for (section
= first_section
;
5935 section
= section
->next
)
5937 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5938 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5940 map
->sections
[isec
++] = section
->output_section
;
5941 if (isec
== section_count
)
5947 map
->count
= section_count
;
5948 *pointer_to_map
= map
;
5949 pointer_to_map
= &map
->next
;
5952 elf_tdata (obfd
)->segment_map
= map_first
;
5956 /* Copy private BFD data. This copies or rewrites ELF program header
5960 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
5962 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5963 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5966 if (elf_tdata (ibfd
)->phdr
== NULL
)
5969 if (ibfd
->xvec
== obfd
->xvec
)
5971 /* Check to see if any sections in the input BFD
5972 covered by ELF program header have changed. */
5973 Elf_Internal_Phdr
*segment
;
5974 asection
*section
, *osec
;
5975 unsigned int i
, num_segments
;
5976 Elf_Internal_Shdr
*this_hdr
;
5977 const struct elf_backend_data
*bed
;
5979 bed
= get_elf_backend_data (ibfd
);
5981 /* Regenerate the segment map if p_paddr is set to 0. */
5982 if (bed
->want_p_paddr_set_to_zero
)
5985 /* Initialize the segment mark field. */
5986 for (section
= obfd
->sections
; section
!= NULL
;
5987 section
= section
->next
)
5988 section
->segment_mark
= FALSE
;
5990 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5991 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5995 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5996 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5997 which severly confuses things, so always regenerate the segment
5998 map in this case. */
5999 if (segment
->p_paddr
== 0
6000 && segment
->p_memsz
== 0
6001 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6004 for (section
= ibfd
->sections
;
6005 section
!= NULL
; section
= section
->next
)
6007 /* We mark the output section so that we know it comes
6008 from the input BFD. */
6009 osec
= section
->output_section
;
6011 osec
->segment_mark
= TRUE
;
6013 /* Check if this section is covered by the segment. */
6014 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6015 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
6017 /* FIXME: Check if its output section is changed or
6018 removed. What else do we need to check? */
6020 || section
->flags
!= osec
->flags
6021 || section
->lma
!= osec
->lma
6022 || section
->vma
!= osec
->vma
6023 || section
->size
!= osec
->size
6024 || section
->rawsize
!= osec
->rawsize
6025 || section
->alignment_power
!= osec
->alignment_power
)
6031 /* Check to see if any output section do not come from the
6033 for (section
= obfd
->sections
; section
!= NULL
;
6034 section
= section
->next
)
6036 if (section
->segment_mark
== FALSE
)
6039 section
->segment_mark
= FALSE
;
6042 return copy_elf_program_header (ibfd
, obfd
);
6046 return rewrite_elf_program_header (ibfd
, obfd
);
6049 /* Initialize private output section information from input section. */
6052 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6056 struct bfd_link_info
*link_info
)
6059 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6060 bfd_boolean need_group
= link_info
== NULL
|| link_info
->relocatable
;
6062 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6063 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6066 /* Don't copy the output ELF section type from input if the
6067 output BFD section flags have been set to something different.
6068 elf_fake_sections will set ELF section type based on BFD
6070 if (elf_section_type (osec
) == SHT_NULL
6071 && (osec
->flags
== isec
->flags
|| !osec
->flags
))
6072 elf_section_type (osec
) = elf_section_type (isec
);
6074 /* FIXME: Is this correct for all OS/PROC specific flags? */
6075 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6076 & (SHF_MASKOS
| SHF_MASKPROC
));
6078 /* Set things up for objcopy and relocatable link. The output
6079 SHT_GROUP section will have its elf_next_in_group pointing back
6080 to the input group members. Ignore linker created group section.
6081 See elfNN_ia64_object_p in elfxx-ia64.c. */
6084 if (elf_sec_group (isec
) == NULL
6085 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6087 if (elf_section_flags (isec
) & SHF_GROUP
)
6088 elf_section_flags (osec
) |= SHF_GROUP
;
6089 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6090 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6094 ihdr
= &elf_section_data (isec
)->this_hdr
;
6096 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6097 don't use the output section of the linked-to section since it
6098 may be NULL at this point. */
6099 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6101 ohdr
= &elf_section_data (osec
)->this_hdr
;
6102 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6103 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6106 osec
->use_rela_p
= isec
->use_rela_p
;
6111 /* Copy private section information. This copies over the entsize
6112 field, and sometimes the info field. */
6115 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6120 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6122 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6123 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6126 ihdr
= &elf_section_data (isec
)->this_hdr
;
6127 ohdr
= &elf_section_data (osec
)->this_hdr
;
6129 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6131 if (ihdr
->sh_type
== SHT_SYMTAB
6132 || ihdr
->sh_type
== SHT_DYNSYM
6133 || ihdr
->sh_type
== SHT_GNU_verneed
6134 || ihdr
->sh_type
== SHT_GNU_verdef
)
6135 ohdr
->sh_info
= ihdr
->sh_info
;
6137 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6141 /* Copy private header information. */
6144 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6148 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6149 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6152 /* Copy over private BFD data if it has not already been copied.
6153 This must be done here, rather than in the copy_private_bfd_data
6154 entry point, because the latter is called after the section
6155 contents have been set, which means that the program headers have
6156 already been worked out. */
6157 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6159 if (! copy_private_bfd_data (ibfd
, obfd
))
6163 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6164 but this might be wrong if we deleted the group section. */
6165 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6166 if (elf_section_type (isec
) == SHT_GROUP
6167 && isec
->output_section
== NULL
)
6169 asection
*first
= elf_next_in_group (isec
);
6170 asection
*s
= first
;
6173 if (s
->output_section
!= NULL
)
6175 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6176 elf_group_name (s
->output_section
) = NULL
;
6178 s
= elf_next_in_group (s
);
6187 /* Copy private symbol information. If this symbol is in a section
6188 which we did not map into a BFD section, try to map the section
6189 index correctly. We use special macro definitions for the mapped
6190 section indices; these definitions are interpreted by the
6191 swap_out_syms function. */
6193 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6194 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6195 #define MAP_STRTAB (SHN_HIOS + 3)
6196 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6197 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6200 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6205 elf_symbol_type
*isym
, *osym
;
6207 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6208 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6211 isym
= elf_symbol_from (ibfd
, isymarg
);
6212 osym
= elf_symbol_from (obfd
, osymarg
);
6215 && isym
->internal_elf_sym
.st_shndx
!= 0
6217 && bfd_is_abs_section (isym
->symbol
.section
))
6221 shndx
= isym
->internal_elf_sym
.st_shndx
;
6222 if (shndx
== elf_onesymtab (ibfd
))
6223 shndx
= MAP_ONESYMTAB
;
6224 else if (shndx
== elf_dynsymtab (ibfd
))
6225 shndx
= MAP_DYNSYMTAB
;
6226 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
6228 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
6229 shndx
= MAP_SHSTRTAB
;
6230 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
6231 shndx
= MAP_SYM_SHNDX
;
6232 osym
->internal_elf_sym
.st_shndx
= shndx
;
6238 /* Swap out the symbols. */
6241 swap_out_syms (bfd
*abfd
,
6242 struct bfd_strtab_hash
**sttp
,
6245 const struct elf_backend_data
*bed
;
6248 struct bfd_strtab_hash
*stt
;
6249 Elf_Internal_Shdr
*symtab_hdr
;
6250 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6251 Elf_Internal_Shdr
*symstrtab_hdr
;
6252 bfd_byte
*outbound_syms
;
6253 bfd_byte
*outbound_shndx
;
6256 bfd_boolean name_local_sections
;
6258 if (!elf_map_symbols (abfd
))
6261 /* Dump out the symtabs. */
6262 stt
= _bfd_elf_stringtab_init ();
6266 bed
= get_elf_backend_data (abfd
);
6267 symcount
= bfd_get_symcount (abfd
);
6268 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6269 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6270 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6271 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6272 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
6273 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6275 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6276 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6278 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
6279 bed
->s
->sizeof_sym
);
6280 if (outbound_syms
== NULL
)
6282 _bfd_stringtab_free (stt
);
6285 symtab_hdr
->contents
= outbound_syms
;
6287 outbound_shndx
= NULL
;
6288 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6289 if (symtab_shndx_hdr
->sh_name
!= 0)
6291 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6292 outbound_shndx
= (bfd_byte
*)
6293 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
6294 if (outbound_shndx
== NULL
)
6296 _bfd_stringtab_free (stt
);
6300 symtab_shndx_hdr
->contents
= outbound_shndx
;
6301 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6302 symtab_shndx_hdr
->sh_size
= amt
;
6303 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6304 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6307 /* Now generate the data (for "contents"). */
6309 /* Fill in zeroth symbol and swap it out. */
6310 Elf_Internal_Sym sym
;
6316 sym
.st_shndx
= SHN_UNDEF
;
6317 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6318 outbound_syms
+= bed
->s
->sizeof_sym
;
6319 if (outbound_shndx
!= NULL
)
6320 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6324 = (bed
->elf_backend_name_local_section_symbols
6325 && bed
->elf_backend_name_local_section_symbols (abfd
));
6327 syms
= bfd_get_outsymbols (abfd
);
6328 for (idx
= 0; idx
< symcount
; idx
++)
6330 Elf_Internal_Sym sym
;
6331 bfd_vma value
= syms
[idx
]->value
;
6332 elf_symbol_type
*type_ptr
;
6333 flagword flags
= syms
[idx
]->flags
;
6336 if (!name_local_sections
6337 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6339 /* Local section symbols have no name. */
6344 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6347 if (sym
.st_name
== (unsigned long) -1)
6349 _bfd_stringtab_free (stt
);
6354 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6356 if ((flags
& BSF_SECTION_SYM
) == 0
6357 && bfd_is_com_section (syms
[idx
]->section
))
6359 /* ELF common symbols put the alignment into the `value' field,
6360 and the size into the `size' field. This is backwards from
6361 how BFD handles it, so reverse it here. */
6362 sym
.st_size
= value
;
6363 if (type_ptr
== NULL
6364 || type_ptr
->internal_elf_sym
.st_value
== 0)
6365 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6367 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6368 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6369 (abfd
, syms
[idx
]->section
);
6373 asection
*sec
= syms
[idx
]->section
;
6376 if (sec
->output_section
)
6378 value
+= sec
->output_offset
;
6379 sec
= sec
->output_section
;
6382 /* Don't add in the section vma for relocatable output. */
6383 if (! relocatable_p
)
6385 sym
.st_value
= value
;
6386 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6388 if (bfd_is_abs_section (sec
)
6390 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6392 /* This symbol is in a real ELF section which we did
6393 not create as a BFD section. Undo the mapping done
6394 by copy_private_symbol_data. */
6395 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6399 shndx
= elf_onesymtab (abfd
);
6402 shndx
= elf_dynsymtab (abfd
);
6405 shndx
= elf_tdata (abfd
)->strtab_section
;
6408 shndx
= elf_tdata (abfd
)->shstrtab_section
;
6411 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
6419 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6421 if (shndx
== SHN_BAD
)
6425 /* Writing this would be a hell of a lot easier if
6426 we had some decent documentation on bfd, and
6427 knew what to expect of the library, and what to
6428 demand of applications. For example, it
6429 appears that `objcopy' might not set the
6430 section of a symbol to be a section that is
6431 actually in the output file. */
6432 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6435 _bfd_error_handler (_("\
6436 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6437 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6439 bfd_set_error (bfd_error_invalid_operation
);
6440 _bfd_stringtab_free (stt
);
6444 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6445 BFD_ASSERT (shndx
!= SHN_BAD
);
6449 sym
.st_shndx
= shndx
;
6452 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6454 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
6455 type
= STT_GNU_IFUNC
;
6456 else if ((flags
& BSF_FUNCTION
) != 0)
6458 else if ((flags
& BSF_OBJECT
) != 0)
6460 else if ((flags
& BSF_RELC
) != 0)
6462 else if ((flags
& BSF_SRELC
) != 0)
6467 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6470 /* Processor-specific types. */
6471 if (type_ptr
!= NULL
6472 && bed
->elf_backend_get_symbol_type
)
6473 type
= ((*bed
->elf_backend_get_symbol_type
)
6474 (&type_ptr
->internal_elf_sym
, type
));
6476 if (flags
& BSF_SECTION_SYM
)
6478 if (flags
& BSF_GLOBAL
)
6479 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
6481 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
6483 else if (bfd_is_com_section (syms
[idx
]->section
))
6485 #ifdef USE_STT_COMMON
6486 if (type
== STT_OBJECT
)
6487 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
6490 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
6492 else if (bfd_is_und_section (syms
[idx
]->section
))
6493 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
6497 else if (flags
& BSF_FILE
)
6498 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
6501 int bind
= STB_LOCAL
;
6503 if (flags
& BSF_LOCAL
)
6505 else if (flags
& BSF_GNU_UNIQUE
)
6506 bind
= STB_GNU_UNIQUE
;
6507 else if (flags
& BSF_WEAK
)
6509 else if (flags
& BSF_GLOBAL
)
6512 sym
.st_info
= ELF_ST_INFO (bind
, type
);
6515 if (type_ptr
!= NULL
)
6516 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
6520 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6521 outbound_syms
+= bed
->s
->sizeof_sym
;
6522 if (outbound_shndx
!= NULL
)
6523 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6527 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
6528 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6530 symstrtab_hdr
->sh_flags
= 0;
6531 symstrtab_hdr
->sh_addr
= 0;
6532 symstrtab_hdr
->sh_entsize
= 0;
6533 symstrtab_hdr
->sh_link
= 0;
6534 symstrtab_hdr
->sh_info
= 0;
6535 symstrtab_hdr
->sh_addralign
= 1;
6540 /* Return the number of bytes required to hold the symtab vector.
6542 Note that we base it on the count plus 1, since we will null terminate
6543 the vector allocated based on this size. However, the ELF symbol table
6544 always has a dummy entry as symbol #0, so it ends up even. */
6547 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
6551 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6553 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6554 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6556 symtab_size
-= sizeof (asymbol
*);
6562 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
6566 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
6568 if (elf_dynsymtab (abfd
) == 0)
6570 bfd_set_error (bfd_error_invalid_operation
);
6574 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6575 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6577 symtab_size
-= sizeof (asymbol
*);
6583 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
6586 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
6589 /* Canonicalize the relocs. */
6592 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
6599 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6601 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
6604 tblptr
= section
->relocation
;
6605 for (i
= 0; i
< section
->reloc_count
; i
++)
6606 *relptr
++ = tblptr
++;
6610 return section
->reloc_count
;
6614 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
6616 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6617 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
6620 bfd_get_symcount (abfd
) = symcount
;
6625 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
6626 asymbol
**allocation
)
6628 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6629 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
6632 bfd_get_dynamic_symcount (abfd
) = symcount
;
6636 /* Return the size required for the dynamic reloc entries. Any loadable
6637 section that was actually installed in the BFD, and has type SHT_REL
6638 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6639 dynamic reloc section. */
6642 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
6647 if (elf_dynsymtab (abfd
) == 0)
6649 bfd_set_error (bfd_error_invalid_operation
);
6653 ret
= sizeof (arelent
*);
6654 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6655 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6656 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6657 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6658 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
6659 * sizeof (arelent
*));
6664 /* Canonicalize the dynamic relocation entries. Note that we return the
6665 dynamic relocations as a single block, although they are actually
6666 associated with particular sections; the interface, which was
6667 designed for SunOS style shared libraries, expects that there is only
6668 one set of dynamic relocs. Any loadable section that was actually
6669 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6670 dynamic symbol table, is considered to be a dynamic reloc section. */
6673 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
6677 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
6681 if (elf_dynsymtab (abfd
) == 0)
6683 bfd_set_error (bfd_error_invalid_operation
);
6687 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
6689 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6691 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6692 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6693 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6698 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
6700 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
6702 for (i
= 0; i
< count
; i
++)
6713 /* Read in the version information. */
6716 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
6718 bfd_byte
*contents
= NULL
;
6719 unsigned int freeidx
= 0;
6721 if (elf_dynverref (abfd
) != 0)
6723 Elf_Internal_Shdr
*hdr
;
6724 Elf_External_Verneed
*everneed
;
6725 Elf_Internal_Verneed
*iverneed
;
6727 bfd_byte
*contents_end
;
6729 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
6731 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
6732 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
6733 if (elf_tdata (abfd
)->verref
== NULL
)
6736 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
6738 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
6739 if (contents
== NULL
)
6741 error_return_verref
:
6742 elf_tdata (abfd
)->verref
= NULL
;
6743 elf_tdata (abfd
)->cverrefs
= 0;
6746 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6747 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6748 goto error_return_verref
;
6750 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
6751 goto error_return_verref
;
6753 BFD_ASSERT (sizeof (Elf_External_Verneed
)
6754 == sizeof (Elf_External_Vernaux
));
6755 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
6756 everneed
= (Elf_External_Verneed
*) contents
;
6757 iverneed
= elf_tdata (abfd
)->verref
;
6758 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
6760 Elf_External_Vernaux
*evernaux
;
6761 Elf_Internal_Vernaux
*ivernaux
;
6764 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
6766 iverneed
->vn_bfd
= abfd
;
6768 iverneed
->vn_filename
=
6769 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6771 if (iverneed
->vn_filename
== NULL
)
6772 goto error_return_verref
;
6774 if (iverneed
->vn_cnt
== 0)
6775 iverneed
->vn_auxptr
= NULL
;
6778 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
6779 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
6780 sizeof (Elf_Internal_Vernaux
));
6781 if (iverneed
->vn_auxptr
== NULL
)
6782 goto error_return_verref
;
6785 if (iverneed
->vn_aux
6786 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6787 goto error_return_verref
;
6789 evernaux
= ((Elf_External_Vernaux
*)
6790 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
6791 ivernaux
= iverneed
->vn_auxptr
;
6792 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
6794 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
6796 ivernaux
->vna_nodename
=
6797 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6798 ivernaux
->vna_name
);
6799 if (ivernaux
->vna_nodename
== NULL
)
6800 goto error_return_verref
;
6802 if (j
+ 1 < iverneed
->vn_cnt
)
6803 ivernaux
->vna_nextptr
= ivernaux
+ 1;
6805 ivernaux
->vna_nextptr
= NULL
;
6807 if (ivernaux
->vna_next
6808 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
6809 goto error_return_verref
;
6811 evernaux
= ((Elf_External_Vernaux
*)
6812 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
6814 if (ivernaux
->vna_other
> freeidx
)
6815 freeidx
= ivernaux
->vna_other
;
6818 if (i
+ 1 < hdr
->sh_info
)
6819 iverneed
->vn_nextref
= iverneed
+ 1;
6821 iverneed
->vn_nextref
= NULL
;
6823 if (iverneed
->vn_next
6824 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6825 goto error_return_verref
;
6827 everneed
= ((Elf_External_Verneed
*)
6828 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
6835 if (elf_dynverdef (abfd
) != 0)
6837 Elf_Internal_Shdr
*hdr
;
6838 Elf_External_Verdef
*everdef
;
6839 Elf_Internal_Verdef
*iverdef
;
6840 Elf_Internal_Verdef
*iverdefarr
;
6841 Elf_Internal_Verdef iverdefmem
;
6843 unsigned int maxidx
;
6844 bfd_byte
*contents_end_def
, *contents_end_aux
;
6846 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
6848 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
6849 if (contents
== NULL
)
6851 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6852 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6855 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
6858 BFD_ASSERT (sizeof (Elf_External_Verdef
)
6859 >= sizeof (Elf_External_Verdaux
));
6860 contents_end_def
= contents
+ hdr
->sh_size
6861 - sizeof (Elf_External_Verdef
);
6862 contents_end_aux
= contents
+ hdr
->sh_size
6863 - sizeof (Elf_External_Verdaux
);
6865 /* We know the number of entries in the section but not the maximum
6866 index. Therefore we have to run through all entries and find
6868 everdef
= (Elf_External_Verdef
*) contents
;
6870 for (i
= 0; i
< hdr
->sh_info
; ++i
)
6872 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6874 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
6875 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
6877 if (iverdefmem
.vd_next
6878 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
6881 everdef
= ((Elf_External_Verdef
*)
6882 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
6885 if (default_imported_symver
)
6887 if (freeidx
> maxidx
)
6892 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
6893 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
6894 if (elf_tdata (abfd
)->verdef
== NULL
)
6897 elf_tdata (abfd
)->cverdefs
= maxidx
;
6899 everdef
= (Elf_External_Verdef
*) contents
;
6900 iverdefarr
= elf_tdata (abfd
)->verdef
;
6901 for (i
= 0; i
< hdr
->sh_info
; i
++)
6903 Elf_External_Verdaux
*everdaux
;
6904 Elf_Internal_Verdaux
*iverdaux
;
6907 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6909 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
6911 error_return_verdef
:
6912 elf_tdata (abfd
)->verdef
= NULL
;
6913 elf_tdata (abfd
)->cverdefs
= 0;
6917 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
6918 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
6920 iverdef
->vd_bfd
= abfd
;
6922 if (iverdef
->vd_cnt
== 0)
6923 iverdef
->vd_auxptr
= NULL
;
6926 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
6927 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
6928 sizeof (Elf_Internal_Verdaux
));
6929 if (iverdef
->vd_auxptr
== NULL
)
6930 goto error_return_verdef
;
6934 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
6935 goto error_return_verdef
;
6937 everdaux
= ((Elf_External_Verdaux
*)
6938 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
6939 iverdaux
= iverdef
->vd_auxptr
;
6940 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
6942 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
6944 iverdaux
->vda_nodename
=
6945 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6946 iverdaux
->vda_name
);
6947 if (iverdaux
->vda_nodename
== NULL
)
6948 goto error_return_verdef
;
6950 if (j
+ 1 < iverdef
->vd_cnt
)
6951 iverdaux
->vda_nextptr
= iverdaux
+ 1;
6953 iverdaux
->vda_nextptr
= NULL
;
6955 if (iverdaux
->vda_next
6956 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
6957 goto error_return_verdef
;
6959 everdaux
= ((Elf_External_Verdaux
*)
6960 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
6963 if (iverdef
->vd_cnt
)
6964 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
6966 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
6967 iverdef
->vd_nextdef
= iverdef
+ 1;
6969 iverdef
->vd_nextdef
= NULL
;
6971 everdef
= ((Elf_External_Verdef
*)
6972 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
6978 else if (default_imported_symver
)
6985 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
6986 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
6987 if (elf_tdata (abfd
)->verdef
== NULL
)
6990 elf_tdata (abfd
)->cverdefs
= freeidx
;
6993 /* Create a default version based on the soname. */
6994 if (default_imported_symver
)
6996 Elf_Internal_Verdef
*iverdef
;
6997 Elf_Internal_Verdaux
*iverdaux
;
6999 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];;
7001 iverdef
->vd_version
= VER_DEF_CURRENT
;
7002 iverdef
->vd_flags
= 0;
7003 iverdef
->vd_ndx
= freeidx
;
7004 iverdef
->vd_cnt
= 1;
7006 iverdef
->vd_bfd
= abfd
;
7008 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7009 if (iverdef
->vd_nodename
== NULL
)
7010 goto error_return_verdef
;
7011 iverdef
->vd_nextdef
= NULL
;
7012 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7013 bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
7014 if (iverdef
->vd_auxptr
== NULL
)
7015 goto error_return_verdef
;
7017 iverdaux
= iverdef
->vd_auxptr
;
7018 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
7019 iverdaux
->vda_nextptr
= NULL
;
7025 if (contents
!= NULL
)
7031 _bfd_elf_make_empty_symbol (bfd
*abfd
)
7033 elf_symbol_type
*newsym
;
7034 bfd_size_type amt
= sizeof (elf_symbol_type
);
7036 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
7041 newsym
->symbol
.the_bfd
= abfd
;
7042 return &newsym
->symbol
;
7047 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
7051 bfd_symbol_info (symbol
, ret
);
7054 /* Return whether a symbol name implies a local symbol. Most targets
7055 use this function for the is_local_label_name entry point, but some
7059 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
7062 /* Normal local symbols start with ``.L''. */
7063 if (name
[0] == '.' && name
[1] == 'L')
7066 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7067 DWARF debugging symbols starting with ``..''. */
7068 if (name
[0] == '.' && name
[1] == '.')
7071 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7072 emitting DWARF debugging output. I suspect this is actually a
7073 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7074 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7075 underscore to be emitted on some ELF targets). For ease of use,
7076 we treat such symbols as local. */
7077 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
7084 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
7085 asymbol
*symbol ATTRIBUTE_UNUSED
)
7092 _bfd_elf_set_arch_mach (bfd
*abfd
,
7093 enum bfd_architecture arch
,
7094 unsigned long machine
)
7096 /* If this isn't the right architecture for this backend, and this
7097 isn't the generic backend, fail. */
7098 if (arch
!= get_elf_backend_data (abfd
)->arch
7099 && arch
!= bfd_arch_unknown
7100 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
7103 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
7106 /* Find the function to a particular section and offset,
7107 for error reporting. */
7110 elf_find_function (bfd
*abfd
,
7114 const char **filename_ptr
,
7115 const char **functionname_ptr
)
7117 const char *filename
;
7118 asymbol
*func
, *file
;
7121 /* ??? Given multiple file symbols, it is impossible to reliably
7122 choose the right file name for global symbols. File symbols are
7123 local symbols, and thus all file symbols must sort before any
7124 global symbols. The ELF spec may be interpreted to say that a
7125 file symbol must sort before other local symbols, but currently
7126 ld -r doesn't do this. So, for ld -r output, it is possible to
7127 make a better choice of file name for local symbols by ignoring
7128 file symbols appearing after a given local symbol. */
7129 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
7130 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7136 state
= nothing_seen
;
7138 for (p
= symbols
; *p
!= NULL
; p
++)
7143 q
= (elf_symbol_type
*) *p
;
7145 type
= ELF_ST_TYPE (q
->internal_elf_sym
.st_info
);
7150 if (state
== symbol_seen
)
7151 state
= file_after_symbol_seen
;
7154 if (!bed
->is_function_type (type
))
7157 if (bfd_get_section (&q
->symbol
) == section
7158 && q
->symbol
.value
>= low_func
7159 && q
->symbol
.value
<= offset
)
7161 func
= (asymbol
*) q
;
7162 low_func
= q
->symbol
.value
;
7165 && (ELF_ST_BIND (q
->internal_elf_sym
.st_info
) == STB_LOCAL
7166 || state
!= file_after_symbol_seen
))
7167 filename
= bfd_asymbol_name (file
);
7171 if (state
== nothing_seen
)
7172 state
= symbol_seen
;
7179 *filename_ptr
= filename
;
7180 if (functionname_ptr
)
7181 *functionname_ptr
= bfd_asymbol_name (func
);
7186 /* Find the nearest line to a particular section and offset,
7187 for error reporting. */
7190 _bfd_elf_find_nearest_line (bfd
*abfd
,
7194 const char **filename_ptr
,
7195 const char **functionname_ptr
,
7196 unsigned int *line_ptr
)
7200 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
7201 filename_ptr
, functionname_ptr
,
7204 if (!*functionname_ptr
)
7205 elf_find_function (abfd
, section
, symbols
, offset
,
7206 *filename_ptr
? NULL
: filename_ptr
,
7212 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
7213 filename_ptr
, functionname_ptr
,
7215 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7217 if (!*functionname_ptr
)
7218 elf_find_function (abfd
, section
, symbols
, offset
,
7219 *filename_ptr
? NULL
: filename_ptr
,
7225 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7226 &found
, filename_ptr
,
7227 functionname_ptr
, line_ptr
,
7228 &elf_tdata (abfd
)->line_info
))
7230 if (found
&& (*functionname_ptr
|| *line_ptr
))
7233 if (symbols
== NULL
)
7236 if (! elf_find_function (abfd
, section
, symbols
, offset
,
7237 filename_ptr
, functionname_ptr
))
7244 /* Find the line for a symbol. */
7247 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7248 const char **filename_ptr
, unsigned int *line_ptr
)
7250 return _bfd_dwarf2_find_line (abfd
, symbols
, symbol
,
7251 filename_ptr
, line_ptr
, 0,
7252 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7255 /* After a call to bfd_find_nearest_line, successive calls to
7256 bfd_find_inliner_info can be used to get source information about
7257 each level of function inlining that terminated at the address
7258 passed to bfd_find_nearest_line. Currently this is only supported
7259 for DWARF2 with appropriate DWARF3 extensions. */
7262 _bfd_elf_find_inliner_info (bfd
*abfd
,
7263 const char **filename_ptr
,
7264 const char **functionname_ptr
,
7265 unsigned int *line_ptr
)
7268 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7269 functionname_ptr
, line_ptr
,
7270 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7275 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7277 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7278 int ret
= bed
->s
->sizeof_ehdr
;
7280 if (!info
->relocatable
)
7282 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
7284 if (phdr_size
== (bfd_size_type
) -1)
7286 struct elf_segment_map
*m
;
7289 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
7290 phdr_size
+= bed
->s
->sizeof_phdr
;
7293 phdr_size
= get_program_header_size (abfd
, info
);
7296 elf_tdata (abfd
)->program_header_size
= phdr_size
;
7304 _bfd_elf_set_section_contents (bfd
*abfd
,
7306 const void *location
,
7308 bfd_size_type count
)
7310 Elf_Internal_Shdr
*hdr
;
7313 if (! abfd
->output_has_begun
7314 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7317 hdr
= &elf_section_data (section
)->this_hdr
;
7318 pos
= hdr
->sh_offset
+ offset
;
7319 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7320 || bfd_bwrite (location
, count
, abfd
) != count
)
7327 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7328 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7329 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7334 /* Try to convert a non-ELF reloc into an ELF one. */
7337 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7339 /* Check whether we really have an ELF howto. */
7341 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7343 bfd_reloc_code_real_type code
;
7344 reloc_howto_type
*howto
;
7346 /* Alien reloc: Try to determine its type to replace it with an
7347 equivalent ELF reloc. */
7349 if (areloc
->howto
->pc_relative
)
7351 switch (areloc
->howto
->bitsize
)
7354 code
= BFD_RELOC_8_PCREL
;
7357 code
= BFD_RELOC_12_PCREL
;
7360 code
= BFD_RELOC_16_PCREL
;
7363 code
= BFD_RELOC_24_PCREL
;
7366 code
= BFD_RELOC_32_PCREL
;
7369 code
= BFD_RELOC_64_PCREL
;
7375 howto
= bfd_reloc_type_lookup (abfd
, code
);
7377 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7379 if (howto
->pcrel_offset
)
7380 areloc
->addend
+= areloc
->address
;
7382 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7387 switch (areloc
->howto
->bitsize
)
7393 code
= BFD_RELOC_14
;
7396 code
= BFD_RELOC_16
;
7399 code
= BFD_RELOC_26
;
7402 code
= BFD_RELOC_32
;
7405 code
= BFD_RELOC_64
;
7411 howto
= bfd_reloc_type_lookup (abfd
, code
);
7415 areloc
->howto
= howto
;
7423 (*_bfd_error_handler
)
7424 (_("%B: unsupported relocation type %s"),
7425 abfd
, areloc
->howto
->name
);
7426 bfd_set_error (bfd_error_bad_value
);
7431 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7433 if (bfd_get_format (abfd
) == bfd_object
)
7435 if (elf_tdata (abfd
) != NULL
&& elf_shstrtab (abfd
) != NULL
)
7436 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7437 _bfd_dwarf2_cleanup_debug_info (abfd
);
7440 return _bfd_generic_close_and_cleanup (abfd
);
7443 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7444 in the relocation's offset. Thus we cannot allow any sort of sanity
7445 range-checking to interfere. There is nothing else to do in processing
7448 bfd_reloc_status_type
7449 _bfd_elf_rel_vtable_reloc_fn
7450 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7451 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
7452 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
7453 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
7455 return bfd_reloc_ok
;
7458 /* Elf core file support. Much of this only works on native
7459 toolchains, since we rely on knowing the
7460 machine-dependent procfs structure in order to pick
7461 out details about the corefile. */
7463 #ifdef HAVE_SYS_PROCFS_H
7464 /* Needed for new procfs interface on sparc-solaris. */
7465 # define _STRUCTURED_PROC 1
7466 # include <sys/procfs.h>
7469 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7472 elfcore_make_pid (bfd
*abfd
)
7474 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
7475 + (elf_tdata (abfd
)->core_pid
));
7478 /* If there isn't a section called NAME, make one, using
7479 data from SECT. Note, this function will generate a
7480 reference to NAME, so you shouldn't deallocate or
7484 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
7488 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
7491 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
7495 sect2
->size
= sect
->size
;
7496 sect2
->filepos
= sect
->filepos
;
7497 sect2
->alignment_power
= sect
->alignment_power
;
7501 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7502 actually creates up to two pseudosections:
7503 - For the single-threaded case, a section named NAME, unless
7504 such a section already exists.
7505 - For the multi-threaded case, a section named "NAME/PID", where
7506 PID is elfcore_make_pid (abfd).
7507 Both pseudosections have identical contents. */
7509 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
7515 char *threaded_name
;
7519 /* Build the section name. */
7521 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
7522 len
= strlen (buf
) + 1;
7523 threaded_name
= (char *) bfd_alloc (abfd
, len
);
7524 if (threaded_name
== NULL
)
7526 memcpy (threaded_name
, buf
, len
);
7528 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
7533 sect
->filepos
= filepos
;
7534 sect
->alignment_power
= 2;
7536 return elfcore_maybe_make_sect (abfd
, name
, sect
);
7539 /* prstatus_t exists on:
7541 linux 2.[01] + glibc
7545 #if defined (HAVE_PRSTATUS_T)
7548 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7553 if (note
->descsz
== sizeof (prstatus_t
))
7557 size
= sizeof (prstat
.pr_reg
);
7558 offset
= offsetof (prstatus_t
, pr_reg
);
7559 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7561 /* Do not overwrite the core signal if it
7562 has already been set by another thread. */
7563 if (elf_tdata (abfd
)->core_signal
== 0)
7564 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7565 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7567 /* pr_who exists on:
7570 pr_who doesn't exist on:
7573 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7574 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7577 #if defined (HAVE_PRSTATUS32_T)
7578 else if (note
->descsz
== sizeof (prstatus32_t
))
7580 /* 64-bit host, 32-bit corefile */
7581 prstatus32_t prstat
;
7583 size
= sizeof (prstat
.pr_reg
);
7584 offset
= offsetof (prstatus32_t
, pr_reg
);
7585 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7587 /* Do not overwrite the core signal if it
7588 has already been set by another thread. */
7589 if (elf_tdata (abfd
)->core_signal
== 0)
7590 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7591 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7593 /* pr_who exists on:
7596 pr_who doesn't exist on:
7599 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7600 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7603 #endif /* HAVE_PRSTATUS32_T */
7606 /* Fail - we don't know how to handle any other
7607 note size (ie. data object type). */
7611 /* Make a ".reg/999" section and a ".reg" section. */
7612 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
7613 size
, note
->descpos
+ offset
);
7615 #endif /* defined (HAVE_PRSTATUS_T) */
7617 /* Create a pseudosection containing the exact contents of NOTE. */
7619 elfcore_make_note_pseudosection (bfd
*abfd
,
7621 Elf_Internal_Note
*note
)
7623 return _bfd_elfcore_make_pseudosection (abfd
, name
,
7624 note
->descsz
, note
->descpos
);
7627 /* There isn't a consistent prfpregset_t across platforms,
7628 but it doesn't matter, because we don't have to pick this
7629 data structure apart. */
7632 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7634 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7637 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7638 type of NT_PRXFPREG. Just include the whole note's contents
7642 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7644 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
7647 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
7648 with a note type of NT_X86_XSTATE. Just include the whole note's
7649 contents literally. */
7652 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
7654 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
7658 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
7660 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
7664 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
7666 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
7670 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
7672 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
7675 #if defined (HAVE_PRPSINFO_T)
7676 typedef prpsinfo_t elfcore_psinfo_t
;
7677 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7678 typedef prpsinfo32_t elfcore_psinfo32_t
;
7682 #if defined (HAVE_PSINFO_T)
7683 typedef psinfo_t elfcore_psinfo_t
;
7684 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7685 typedef psinfo32_t elfcore_psinfo32_t
;
7689 /* return a malloc'ed copy of a string at START which is at
7690 most MAX bytes long, possibly without a terminating '\0'.
7691 the copy will always have a terminating '\0'. */
7694 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
7697 char *end
= (char *) memchr (start
, '\0', max
);
7705 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
7709 memcpy (dups
, start
, len
);
7715 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7717 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7719 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
7721 elfcore_psinfo_t psinfo
;
7723 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7725 elf_tdata (abfd
)->core_program
7726 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7727 sizeof (psinfo
.pr_fname
));
7729 elf_tdata (abfd
)->core_command
7730 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7731 sizeof (psinfo
.pr_psargs
));
7733 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7734 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
7736 /* 64-bit host, 32-bit corefile */
7737 elfcore_psinfo32_t psinfo
;
7739 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7741 elf_tdata (abfd
)->core_program
7742 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7743 sizeof (psinfo
.pr_fname
));
7745 elf_tdata (abfd
)->core_command
7746 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7747 sizeof (psinfo
.pr_psargs
));
7753 /* Fail - we don't know how to handle any other
7754 note size (ie. data object type). */
7758 /* Note that for some reason, a spurious space is tacked
7759 onto the end of the args in some (at least one anyway)
7760 implementations, so strip it off if it exists. */
7763 char *command
= elf_tdata (abfd
)->core_command
;
7764 int n
= strlen (command
);
7766 if (0 < n
&& command
[n
- 1] == ' ')
7767 command
[n
- 1] = '\0';
7772 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7774 #if defined (HAVE_PSTATUS_T)
7776 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7778 if (note
->descsz
== sizeof (pstatus_t
)
7779 #if defined (HAVE_PXSTATUS_T)
7780 || note
->descsz
== sizeof (pxstatus_t
)
7786 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
7788 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
7790 #if defined (HAVE_PSTATUS32_T)
7791 else if (note
->descsz
== sizeof (pstatus32_t
))
7793 /* 64-bit host, 32-bit corefile */
7796 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
7798 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
7801 /* Could grab some more details from the "representative"
7802 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7803 NT_LWPSTATUS note, presumably. */
7807 #endif /* defined (HAVE_PSTATUS_T) */
7809 #if defined (HAVE_LWPSTATUS_T)
7811 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7813 lwpstatus_t lwpstat
;
7819 if (note
->descsz
!= sizeof (lwpstat
)
7820 #if defined (HAVE_LWPXSTATUS_T)
7821 && note
->descsz
!= sizeof (lwpxstatus_t
)
7826 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
7828 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
7829 /* Do not overwrite the core signal if it has already been set by
7831 if (elf_tdata (abfd
)->core_signal
== 0)
7832 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
7834 /* Make a ".reg/999" section. */
7836 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
7837 len
= strlen (buf
) + 1;
7838 name
= bfd_alloc (abfd
, len
);
7841 memcpy (name
, buf
, len
);
7843 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7847 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7848 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
7849 sect
->filepos
= note
->descpos
7850 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
7853 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7854 sect
->size
= sizeof (lwpstat
.pr_reg
);
7855 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
7858 sect
->alignment_power
= 2;
7860 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7863 /* Make a ".reg2/999" section */
7865 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
7866 len
= strlen (buf
) + 1;
7867 name
= bfd_alloc (abfd
, len
);
7870 memcpy (name
, buf
, len
);
7872 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7876 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7877 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
7878 sect
->filepos
= note
->descpos
7879 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
7882 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7883 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
7884 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
7887 sect
->alignment_power
= 2;
7889 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
7891 #endif /* defined (HAVE_LWPSTATUS_T) */
7894 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7901 int is_active_thread
;
7904 if (note
->descsz
< 728)
7907 if (! CONST_STRNEQ (note
->namedata
, "win32"))
7910 type
= bfd_get_32 (abfd
, note
->descdata
);
7914 case 1 /* NOTE_INFO_PROCESS */:
7915 /* FIXME: need to add ->core_command. */
7916 /* process_info.pid */
7917 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
7918 /* process_info.signal */
7919 elf_tdata (abfd
)->core_signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
7922 case 2 /* NOTE_INFO_THREAD */:
7923 /* Make a ".reg/999" section. */
7924 /* thread_info.tid */
7925 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
7927 len
= strlen (buf
) + 1;
7928 name
= (char *) bfd_alloc (abfd
, len
);
7932 memcpy (name
, buf
, len
);
7934 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7938 /* sizeof (thread_info.thread_context) */
7940 /* offsetof (thread_info.thread_context) */
7941 sect
->filepos
= note
->descpos
+ 12;
7942 sect
->alignment_power
= 2;
7944 /* thread_info.is_active_thread */
7945 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
7947 if (is_active_thread
)
7948 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7952 case 3 /* NOTE_INFO_MODULE */:
7953 /* Make a ".module/xxxxxxxx" section. */
7954 /* module_info.base_address */
7955 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
7956 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
7958 len
= strlen (buf
) + 1;
7959 name
= (char *) bfd_alloc (abfd
, len
);
7963 memcpy (name
, buf
, len
);
7965 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7970 sect
->size
= note
->descsz
;
7971 sect
->filepos
= note
->descpos
;
7972 sect
->alignment_power
= 2;
7983 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7985 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7993 if (bed
->elf_backend_grok_prstatus
)
7994 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
7996 #if defined (HAVE_PRSTATUS_T)
7997 return elfcore_grok_prstatus (abfd
, note
);
8002 #if defined (HAVE_PSTATUS_T)
8004 return elfcore_grok_pstatus (abfd
, note
);
8007 #if defined (HAVE_LWPSTATUS_T)
8009 return elfcore_grok_lwpstatus (abfd
, note
);
8012 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
8013 return elfcore_grok_prfpreg (abfd
, note
);
8015 case NT_WIN32PSTATUS
:
8016 return elfcore_grok_win32pstatus (abfd
, note
);
8018 case NT_PRXFPREG
: /* Linux SSE extension */
8019 if (note
->namesz
== 6
8020 && strcmp (note
->namedata
, "LINUX") == 0)
8021 return elfcore_grok_prxfpreg (abfd
, note
);
8025 case NT_X86_XSTATE
: /* Linux XSAVE extension */
8026 if (note
->namesz
== 6
8027 && strcmp (note
->namedata
, "LINUX") == 0)
8028 return elfcore_grok_xstatereg (abfd
, note
);
8033 if (note
->namesz
== 6
8034 && strcmp (note
->namedata
, "LINUX") == 0)
8035 return elfcore_grok_ppc_vmx (abfd
, note
);
8040 if (note
->namesz
== 6
8041 && strcmp (note
->namedata
, "LINUX") == 0)
8042 return elfcore_grok_ppc_vsx (abfd
, note
);
8046 case NT_S390_HIGH_GPRS
:
8047 if (note
->namesz
== 6
8048 && strcmp (note
->namedata
, "LINUX") == 0)
8049 return elfcore_grok_s390_high_gprs (abfd
, note
);
8055 if (bed
->elf_backend_grok_psinfo
)
8056 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
8058 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8059 return elfcore_grok_psinfo (abfd
, note
);
8066 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8071 sect
->size
= note
->descsz
;
8072 sect
->filepos
= note
->descpos
;
8073 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8081 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
8083 elf_tdata (abfd
)->build_id_size
= note
->descsz
;
8084 elf_tdata (abfd
)->build_id
= (bfd_byte
*) bfd_alloc (abfd
, note
->descsz
);
8085 if (elf_tdata (abfd
)->build_id
== NULL
)
8088 memcpy (elf_tdata (abfd
)->build_id
, note
->descdata
, note
->descsz
);
8094 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8101 case NT_GNU_BUILD_ID
:
8102 return elfobj_grok_gnu_build_id (abfd
, note
);
8107 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
8111 cp
= strchr (note
->namedata
, '@');
8114 *lwpidp
= atoi(cp
+ 1);
8121 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8123 /* Signal number at offset 0x08. */
8124 elf_tdata (abfd
)->core_signal
8125 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8127 /* Process ID at offset 0x50. */
8128 elf_tdata (abfd
)->core_pid
8129 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
8131 /* Command name at 0x7c (max 32 bytes, including nul). */
8132 elf_tdata (abfd
)->core_command
8133 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
8135 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
8140 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8144 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
8145 elf_tdata (abfd
)->core_lwpid
= lwp
;
8147 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
8149 /* NetBSD-specific core "procinfo". Note that we expect to
8150 find this note before any of the others, which is fine,
8151 since the kernel writes this note out first when it
8152 creates a core file. */
8154 return elfcore_grok_netbsd_procinfo (abfd
, note
);
8157 /* As of Jan 2002 there are no other machine-independent notes
8158 defined for NetBSD core files. If the note type is less
8159 than the start of the machine-dependent note types, we don't
8162 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
8166 switch (bfd_get_arch (abfd
))
8168 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8169 PT_GETFPREGS == mach+2. */
8171 case bfd_arch_alpha
:
8172 case bfd_arch_sparc
:
8175 case NT_NETBSDCORE_FIRSTMACH
+0:
8176 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8178 case NT_NETBSDCORE_FIRSTMACH
+2:
8179 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8185 /* On all other arch's, PT_GETREGS == mach+1 and
8186 PT_GETFPREGS == mach+3. */
8191 case NT_NETBSDCORE_FIRSTMACH
+1:
8192 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8194 case NT_NETBSDCORE_FIRSTMACH
+3:
8195 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8205 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8207 /* Signal number at offset 0x08. */
8208 elf_tdata (abfd
)->core_signal
8209 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8211 /* Process ID at offset 0x20. */
8212 elf_tdata (abfd
)->core_pid
8213 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
8215 /* Command name at 0x48 (max 32 bytes, including nul). */
8216 elf_tdata (abfd
)->core_command
8217 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
8223 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8225 if (note
->type
== NT_OPENBSD_PROCINFO
)
8226 return elfcore_grok_openbsd_procinfo (abfd
, note
);
8228 if (note
->type
== NT_OPENBSD_REGS
)
8229 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8231 if (note
->type
== NT_OPENBSD_FPREGS
)
8232 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8234 if (note
->type
== NT_OPENBSD_XFPREGS
)
8235 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8237 if (note
->type
== NT_OPENBSD_AUXV
)
8239 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8244 sect
->size
= note
->descsz
;
8245 sect
->filepos
= note
->descpos
;
8246 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8251 if (note
->type
== NT_OPENBSD_WCOOKIE
)
8253 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
8258 sect
->size
= note
->descsz
;
8259 sect
->filepos
= note
->descpos
;
8260 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8269 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
8271 void *ddata
= note
->descdata
;
8278 /* nto_procfs_status 'pid' field is at offset 0. */
8279 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
8281 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8282 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
8284 /* nto_procfs_status 'flags' field is at offset 8. */
8285 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
8287 /* nto_procfs_status 'what' field is at offset 14. */
8288 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
8290 elf_tdata (abfd
)->core_signal
= sig
;
8291 elf_tdata (abfd
)->core_lwpid
= *tid
;
8294 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8295 do not come from signals so we make sure we set the current
8296 thread just in case. */
8297 if (flags
& 0x00000080)
8298 elf_tdata (abfd
)->core_lwpid
= *tid
;
8300 /* Make a ".qnx_core_status/%d" section. */
8301 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
8303 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
8308 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8312 sect
->size
= note
->descsz
;
8313 sect
->filepos
= note
->descpos
;
8314 sect
->alignment_power
= 2;
8316 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
8320 elfcore_grok_nto_regs (bfd
*abfd
,
8321 Elf_Internal_Note
*note
,
8329 /* Make a "(base)/%d" section. */
8330 sprintf (buf
, "%s/%ld", base
, tid
);
8332 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
8337 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8341 sect
->size
= note
->descsz
;
8342 sect
->filepos
= note
->descpos
;
8343 sect
->alignment_power
= 2;
8345 /* This is the current thread. */
8346 if (elf_tdata (abfd
)->core_lwpid
== tid
)
8347 return elfcore_maybe_make_sect (abfd
, base
, sect
);
8352 #define BFD_QNT_CORE_INFO 7
8353 #define BFD_QNT_CORE_STATUS 8
8354 #define BFD_QNT_CORE_GREG 9
8355 #define BFD_QNT_CORE_FPREG 10
8358 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8360 /* Every GREG section has a STATUS section before it. Store the
8361 tid from the previous call to pass down to the next gregs
8363 static long tid
= 1;
8367 case BFD_QNT_CORE_INFO
:
8368 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
8369 case BFD_QNT_CORE_STATUS
:
8370 return elfcore_grok_nto_status (abfd
, note
, &tid
);
8371 case BFD_QNT_CORE_GREG
:
8372 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
8373 case BFD_QNT_CORE_FPREG
:
8374 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
8381 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8387 /* Use note name as section name. */
8389 name
= (char *) bfd_alloc (abfd
, len
);
8392 memcpy (name
, note
->namedata
, len
);
8393 name
[len
- 1] = '\0';
8395 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8399 sect
->size
= note
->descsz
;
8400 sect
->filepos
= note
->descpos
;
8401 sect
->alignment_power
= 1;
8406 /* Function: elfcore_write_note
8409 buffer to hold note, and current size of buffer
8413 size of data for note
8415 Writes note to end of buffer. ELF64 notes are written exactly as
8416 for ELF32, despite the current (as of 2006) ELF gabi specifying
8417 that they ought to have 8-byte namesz and descsz field, and have
8418 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8421 Pointer to realloc'd buffer, *BUFSIZ updated. */
8424 elfcore_write_note (bfd
*abfd
,
8432 Elf_External_Note
*xnp
;
8439 namesz
= strlen (name
) + 1;
8441 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
8443 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
8446 dest
= buf
+ *bufsiz
;
8447 *bufsiz
+= newspace
;
8448 xnp
= (Elf_External_Note
*) dest
;
8449 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
8450 H_PUT_32 (abfd
, size
, xnp
->descsz
);
8451 H_PUT_32 (abfd
, type
, xnp
->type
);
8455 memcpy (dest
, name
, namesz
);
8463 memcpy (dest
, input
, size
);
8473 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8475 elfcore_write_prpsinfo (bfd
*abfd
,
8481 const char *note_name
= "CORE";
8482 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8484 if (bed
->elf_backend_write_core_note
!= NULL
)
8487 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
8488 NT_PRPSINFO
, fname
, psargs
);
8493 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8494 if (bed
->s
->elfclass
== ELFCLASS32
)
8496 #if defined (HAVE_PSINFO32_T)
8498 int note_type
= NT_PSINFO
;
8501 int note_type
= NT_PRPSINFO
;
8504 memset (&data
, 0, sizeof (data
));
8505 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
8506 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
8507 return elfcore_write_note (abfd
, buf
, bufsiz
,
8508 note_name
, note_type
, &data
, sizeof (data
));
8513 #if defined (HAVE_PSINFO_T)
8515 int note_type
= NT_PSINFO
;
8518 int note_type
= NT_PRPSINFO
;
8521 memset (&data
, 0, sizeof (data
));
8522 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
8523 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
8524 return elfcore_write_note (abfd
, buf
, bufsiz
,
8525 note_name
, note_type
, &data
, sizeof (data
));
8528 #endif /* PSINFO_T or PRPSINFO_T */
8530 #if defined (HAVE_PRSTATUS_T)
8532 elfcore_write_prstatus (bfd
*abfd
,
8539 const char *note_name
= "CORE";
8540 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8542 if (bed
->elf_backend_write_core_note
!= NULL
)
8545 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
8547 pid
, cursig
, gregs
);
8552 #if defined (HAVE_PRSTATUS32_T)
8553 if (bed
->s
->elfclass
== ELFCLASS32
)
8555 prstatus32_t prstat
;
8557 memset (&prstat
, 0, sizeof (prstat
));
8558 prstat
.pr_pid
= pid
;
8559 prstat
.pr_cursig
= cursig
;
8560 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8561 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8562 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8569 memset (&prstat
, 0, sizeof (prstat
));
8570 prstat
.pr_pid
= pid
;
8571 prstat
.pr_cursig
= cursig
;
8572 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8573 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8574 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8577 #endif /* HAVE_PRSTATUS_T */
8579 #if defined (HAVE_LWPSTATUS_T)
8581 elfcore_write_lwpstatus (bfd
*abfd
,
8588 lwpstatus_t lwpstat
;
8589 const char *note_name
= "CORE";
8591 memset (&lwpstat
, 0, sizeof (lwpstat
));
8592 lwpstat
.pr_lwpid
= pid
>> 16;
8593 lwpstat
.pr_cursig
= cursig
;
8594 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8595 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
8596 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8598 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
8599 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
8601 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
8602 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
8605 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8606 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
8608 #endif /* HAVE_LWPSTATUS_T */
8610 #if defined (HAVE_PSTATUS_T)
8612 elfcore_write_pstatus (bfd
*abfd
,
8616 int cursig ATTRIBUTE_UNUSED
,
8617 const void *gregs ATTRIBUTE_UNUSED
)
8619 const char *note_name
= "CORE";
8620 #if defined (HAVE_PSTATUS32_T)
8621 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8623 if (bed
->s
->elfclass
== ELFCLASS32
)
8627 memset (&pstat
, 0, sizeof (pstat
));
8628 pstat
.pr_pid
= pid
& 0xffff;
8629 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8630 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8638 memset (&pstat
, 0, sizeof (pstat
));
8639 pstat
.pr_pid
= pid
& 0xffff;
8640 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8641 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8645 #endif /* HAVE_PSTATUS_T */
8648 elfcore_write_prfpreg (bfd
*abfd
,
8654 const char *note_name
= "CORE";
8655 return elfcore_write_note (abfd
, buf
, bufsiz
,
8656 note_name
, NT_FPREGSET
, fpregs
, size
);
8660 elfcore_write_prxfpreg (bfd
*abfd
,
8663 const void *xfpregs
,
8666 char *note_name
= "LINUX";
8667 return elfcore_write_note (abfd
, buf
, bufsiz
,
8668 note_name
, NT_PRXFPREG
, xfpregs
, size
);
8672 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
8673 const void *xfpregs
, int size
)
8675 char *note_name
= "LINUX";
8676 return elfcore_write_note (abfd
, buf
, bufsiz
,
8677 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
8681 elfcore_write_ppc_vmx (bfd
*abfd
,
8684 const void *ppc_vmx
,
8687 char *note_name
= "LINUX";
8688 return elfcore_write_note (abfd
, buf
, bufsiz
,
8689 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
8693 elfcore_write_ppc_vsx (bfd
*abfd
,
8696 const void *ppc_vsx
,
8699 char *note_name
= "LINUX";
8700 return elfcore_write_note (abfd
, buf
, bufsiz
,
8701 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
8705 elfcore_write_s390_high_gprs (bfd
*abfd
,
8708 const void *s390_high_gprs
,
8711 char *note_name
= "LINUX";
8712 return elfcore_write_note (abfd
, buf
, bufsiz
,
8713 note_name
, NT_S390_HIGH_GPRS
,
8714 s390_high_gprs
, size
);
8718 elfcore_write_register_note (bfd
*abfd
,
8721 const char *section
,
8725 if (strcmp (section
, ".reg2") == 0)
8726 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
8727 if (strcmp (section
, ".reg-xfp") == 0)
8728 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
8729 if (strcmp (section
, ".reg-xstate") == 0)
8730 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
8731 if (strcmp (section
, ".reg-ppc-vmx") == 0)
8732 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
8733 if (strcmp (section
, ".reg-ppc-vsx") == 0)
8734 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
8735 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
8736 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
8741 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
8746 while (p
< buf
+ size
)
8748 /* FIXME: bad alignment assumption. */
8749 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
8750 Elf_Internal_Note in
;
8752 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
8755 in
.type
= H_GET_32 (abfd
, xnp
->type
);
8757 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
8758 in
.namedata
= xnp
->name
;
8759 if (in
.namesz
> buf
- in
.namedata
+ size
)
8762 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
8763 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
8764 in
.descpos
= offset
+ (in
.descdata
- buf
);
8766 && (in
.descdata
>= buf
+ size
8767 || in
.descsz
> buf
- in
.descdata
+ size
))
8770 switch (bfd_get_format (abfd
))
8776 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
8778 if (! elfcore_grok_netbsd_note (abfd
, &in
))
8781 else if (CONST_STRNEQ (in
.namedata
, "OpenBSD"))
8783 if (! elfcore_grok_openbsd_note (abfd
, &in
))
8786 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
8788 if (! elfcore_grok_nto_note (abfd
, &in
))
8791 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
8793 if (! elfcore_grok_spu_note (abfd
, &in
))
8798 if (! elfcore_grok_note (abfd
, &in
))
8804 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
8806 if (! elfobj_grok_gnu_note (abfd
, &in
))
8812 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
8819 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
8826 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
8829 buf
= (char *) bfd_malloc (size
);
8833 if (bfd_bread (buf
, size
, abfd
) != size
8834 || !elf_parse_notes (abfd
, buf
, size
, offset
))
8844 /* Providing external access to the ELF program header table. */
8846 /* Return an upper bound on the number of bytes required to store a
8847 copy of ABFD's program header table entries. Return -1 if an error
8848 occurs; bfd_get_error will return an appropriate code. */
8851 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
8853 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8855 bfd_set_error (bfd_error_wrong_format
);
8859 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
8862 /* Copy ABFD's program header table entries to *PHDRS. The entries
8863 will be stored as an array of Elf_Internal_Phdr structures, as
8864 defined in include/elf/internal.h. To find out how large the
8865 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8867 Return the number of program header table entries read, or -1 if an
8868 error occurs; bfd_get_error will return an appropriate code. */
8871 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
8875 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8877 bfd_set_error (bfd_error_wrong_format
);
8881 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
8882 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
8883 num_phdrs
* sizeof (Elf_Internal_Phdr
));
8888 enum elf_reloc_type_class
8889 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
8891 return reloc_class_normal
;
8894 /* For RELA architectures, return the relocation value for a
8895 relocation against a local symbol. */
8898 _bfd_elf_rela_local_sym (bfd
*abfd
,
8899 Elf_Internal_Sym
*sym
,
8901 Elf_Internal_Rela
*rel
)
8903 asection
*sec
= *psec
;
8906 relocation
= (sec
->output_section
->vma
8907 + sec
->output_offset
8909 if ((sec
->flags
& SEC_MERGE
)
8910 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
8911 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
8914 _bfd_merged_section_offset (abfd
, psec
,
8915 elf_section_data (sec
)->sec_info
,
8916 sym
->st_value
+ rel
->r_addend
);
8919 /* If we have changed the section, and our original section is
8920 marked with SEC_EXCLUDE, it means that the original
8921 SEC_MERGE section has been completely subsumed in some
8922 other SEC_MERGE section. In this case, we need to leave
8923 some info around for --emit-relocs. */
8924 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
8925 sec
->kept_section
= *psec
;
8928 rel
->r_addend
-= relocation
;
8929 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
8935 _bfd_elf_rel_local_sym (bfd
*abfd
,
8936 Elf_Internal_Sym
*sym
,
8940 asection
*sec
= *psec
;
8942 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
8943 return sym
->st_value
+ addend
;
8945 return _bfd_merged_section_offset (abfd
, psec
,
8946 elf_section_data (sec
)->sec_info
,
8947 sym
->st_value
+ addend
);
8951 _bfd_elf_section_offset (bfd
*abfd
,
8952 struct bfd_link_info
*info
,
8956 switch (sec
->sec_info_type
)
8958 case ELF_INFO_TYPE_STABS
:
8959 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
8961 case ELF_INFO_TYPE_EH_FRAME
:
8962 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
8968 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8969 reconstruct an ELF file by reading the segments out of remote memory
8970 based on the ELF file header at EHDR_VMA and the ELF program headers it
8971 points to. If not null, *LOADBASEP is filled in with the difference
8972 between the VMAs from which the segments were read, and the VMAs the
8973 file headers (and hence BFD's idea of each section's VMA) put them at.
8975 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8976 remote memory at target address VMA into the local buffer at MYADDR; it
8977 should return zero on success or an `errno' code on failure. TEMPL must
8978 be a BFD for an ELF target with the word size and byte order found in
8979 the remote memory. */
8982 bfd_elf_bfd_from_remote_memory
8986 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, int))
8988 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
8989 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
8993 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
8994 long symcount ATTRIBUTE_UNUSED
,
8995 asymbol
**syms ATTRIBUTE_UNUSED
,
9000 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9003 const char *relplt_name
;
9004 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
9008 Elf_Internal_Shdr
*hdr
;
9014 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
9017 if (dynsymcount
<= 0)
9020 if (!bed
->plt_sym_val
)
9023 relplt_name
= bed
->relplt_name
;
9024 if (relplt_name
== NULL
)
9025 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
9026 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
9030 hdr
= &elf_section_data (relplt
)->this_hdr
;
9031 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
9032 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
9035 plt
= bfd_get_section_by_name (abfd
, ".plt");
9039 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
9040 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
9043 count
= relplt
->size
/ hdr
->sh_entsize
;
9044 size
= count
* sizeof (asymbol
);
9045 p
= relplt
->relocation
;
9046 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9048 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
9052 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
9054 size
+= sizeof ("+0x") - 1 + 8;
9059 s
= *ret
= (asymbol
*) bfd_malloc (size
);
9063 names
= (char *) (s
+ count
);
9064 p
= relplt
->relocation
;
9066 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9071 addr
= bed
->plt_sym_val (i
, plt
, p
);
9072 if (addr
== (bfd_vma
) -1)
9075 *s
= **p
->sym_ptr_ptr
;
9076 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
9077 we are defining a symbol, ensure one of them is set. */
9078 if ((s
->flags
& BSF_LOCAL
) == 0)
9079 s
->flags
|= BSF_GLOBAL
;
9080 s
->flags
|= BSF_SYNTHETIC
;
9082 s
->value
= addr
- plt
->vma
;
9085 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
9086 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
9092 memcpy (names
, "+0x", sizeof ("+0x") - 1);
9093 names
+= sizeof ("+0x") - 1;
9094 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
9095 for (a
= buf
; *a
== '0'; ++a
)
9098 memcpy (names
, a
, len
);
9101 memcpy (names
, "@plt", sizeof ("@plt"));
9102 names
+= sizeof ("@plt");
9109 /* It is only used by x86-64 so far. */
9110 asection _bfd_elf_large_com_section
9111 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
9112 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
9115 _bfd_elf_set_osabi (bfd
* abfd
,
9116 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
9118 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
9120 i_ehdrp
= elf_elfheader (abfd
);
9122 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
9124 /* To make things simpler for the loader on Linux systems we set the
9125 osabi field to ELFOSABI_LINUX if the binary contains symbols of
9126 the STT_GNU_IFUNC type. */
9127 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
9128 && elf_tdata (abfd
)->has_ifunc_symbols
)
9129 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_LINUX
;
9133 /* Return TRUE for ELF symbol types that represent functions.
9134 This is the default version of this function, which is sufficient for
9135 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
9138 _bfd_elf_is_function_type (unsigned int type
)
9140 return (type
== STT_FUNC
9141 || type
== STT_GNU_IFUNC
);