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 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
28 BFD support for ELF formats is being worked on.
29 Currently, the best supported back ends are for sparc and i386
30 (running svr4 or Solaris 2).
32 Documentation of the internals of the support code still needs
33 to be written. The code is changing quickly enough that we
34 haven't bothered yet. */
36 /* For sparc64-cross-sparc32. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
47 static int elf_sort_sections (const void *, const void *);
48 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
49 static bfd_boolean
prep_headers (bfd
*);
50 static bfd_boolean
swap_out_syms (bfd
*, struct bfd_strtab_hash
**, int) ;
51 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
52 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
55 /* Swap version information in and out. The version information is
56 currently size independent. If that ever changes, this code will
57 need to move into elfcode.h. */
59 /* Swap in a Verdef structure. */
62 _bfd_elf_swap_verdef_in (bfd
*abfd
,
63 const Elf_External_Verdef
*src
,
64 Elf_Internal_Verdef
*dst
)
66 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
67 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
68 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
69 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
70 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
71 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
72 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
75 /* Swap out a Verdef structure. */
78 _bfd_elf_swap_verdef_out (bfd
*abfd
,
79 const Elf_Internal_Verdef
*src
,
80 Elf_External_Verdef
*dst
)
82 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
83 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
84 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
85 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
86 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
87 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
88 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
91 /* Swap in a Verdaux structure. */
94 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
95 const Elf_External_Verdaux
*src
,
96 Elf_Internal_Verdaux
*dst
)
98 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
99 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
102 /* Swap out a Verdaux structure. */
105 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
106 const Elf_Internal_Verdaux
*src
,
107 Elf_External_Verdaux
*dst
)
109 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
110 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
113 /* Swap in a Verneed structure. */
116 _bfd_elf_swap_verneed_in (bfd
*abfd
,
117 const Elf_External_Verneed
*src
,
118 Elf_Internal_Verneed
*dst
)
120 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
121 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
122 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
123 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
124 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
127 /* Swap out a Verneed structure. */
130 _bfd_elf_swap_verneed_out (bfd
*abfd
,
131 const Elf_Internal_Verneed
*src
,
132 Elf_External_Verneed
*dst
)
134 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
135 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
136 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
137 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
138 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
141 /* Swap in a Vernaux structure. */
144 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
145 const Elf_External_Vernaux
*src
,
146 Elf_Internal_Vernaux
*dst
)
148 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
149 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
150 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
151 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
152 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
155 /* Swap out a Vernaux structure. */
158 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
159 const Elf_Internal_Vernaux
*src
,
160 Elf_External_Vernaux
*dst
)
162 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
163 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
164 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
165 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
166 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
169 /* Swap in a Versym structure. */
172 _bfd_elf_swap_versym_in (bfd
*abfd
,
173 const Elf_External_Versym
*src
,
174 Elf_Internal_Versym
*dst
)
176 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
179 /* Swap out a Versym structure. */
182 _bfd_elf_swap_versym_out (bfd
*abfd
,
183 const Elf_Internal_Versym
*src
,
184 Elf_External_Versym
*dst
)
186 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
189 /* Standard ELF hash function. Do not change this function; you will
190 cause invalid hash tables to be generated. */
193 bfd_elf_hash (const char *namearg
)
195 const unsigned char *name
= (const unsigned char *) namearg
;
200 while ((ch
= *name
++) != '\0')
203 if ((g
= (h
& 0xf0000000)) != 0)
206 /* The ELF ABI says `h &= ~g', but this is equivalent in
207 this case and on some machines one insn instead of two. */
211 return h
& 0xffffffff;
214 /* DT_GNU_HASH hash function. Do not change this function; you will
215 cause invalid hash tables to be generated. */
218 bfd_elf_gnu_hash (const char *namearg
)
220 const unsigned char *name
= (const unsigned char *) namearg
;
221 unsigned long h
= 5381;
224 while ((ch
= *name
++) != '\0')
225 h
= (h
<< 5) + h
+ ch
;
226 return h
& 0xffffffff;
229 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
230 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
232 bfd_elf_allocate_object (bfd
*abfd
,
234 enum elf_object_id object_id
)
236 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
237 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
238 if (abfd
->tdata
.any
== NULL
)
241 elf_object_id (abfd
) = object_id
;
242 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
248 bfd_elf_make_generic_object (bfd
*abfd
)
250 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
255 bfd_elf_mkcorefile (bfd
*abfd
)
257 /* I think this can be done just like an object file. */
258 return bfd_elf_make_generic_object (abfd
);
262 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
264 Elf_Internal_Shdr
**i_shdrp
;
265 bfd_byte
*shstrtab
= NULL
;
267 bfd_size_type shstrtabsize
;
269 i_shdrp
= elf_elfsections (abfd
);
271 || shindex
>= elf_numsections (abfd
)
272 || i_shdrp
[shindex
] == 0)
275 shstrtab
= i_shdrp
[shindex
]->contents
;
276 if (shstrtab
== NULL
)
278 /* No cached one, attempt to read, and cache what we read. */
279 offset
= i_shdrp
[shindex
]->sh_offset
;
280 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
282 /* Allocate and clear an extra byte at the end, to prevent crashes
283 in case the string table is not terminated. */
284 if (shstrtabsize
+ 1 == 0
285 || (shstrtab
= bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
286 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
288 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
290 if (bfd_get_error () != bfd_error_system_call
)
291 bfd_set_error (bfd_error_file_truncated
);
295 shstrtab
[shstrtabsize
] = '\0';
296 i_shdrp
[shindex
]->contents
= shstrtab
;
298 return (char *) shstrtab
;
302 bfd_elf_string_from_elf_section (bfd
*abfd
,
303 unsigned int shindex
,
304 unsigned int strindex
)
306 Elf_Internal_Shdr
*hdr
;
311 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
314 hdr
= elf_elfsections (abfd
)[shindex
];
316 if (hdr
->contents
== NULL
317 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
320 if (strindex
>= hdr
->sh_size
)
322 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
323 (*_bfd_error_handler
)
324 (_("%B: invalid string offset %u >= %lu for section `%s'"),
325 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
326 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
328 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
332 return ((char *) hdr
->contents
) + strindex
;
335 /* Read and convert symbols to internal format.
336 SYMCOUNT specifies the number of symbols to read, starting from
337 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
338 are non-NULL, they are used to store the internal symbols, external
339 symbols, and symbol section index extensions, respectively.
340 Returns a pointer to the internal symbol buffer (malloced if necessary)
341 or NULL if there were no symbols or some kind of problem. */
344 bfd_elf_get_elf_syms (bfd
*ibfd
,
345 Elf_Internal_Shdr
*symtab_hdr
,
348 Elf_Internal_Sym
*intsym_buf
,
350 Elf_External_Sym_Shndx
*extshndx_buf
)
352 Elf_Internal_Shdr
*shndx_hdr
;
354 const bfd_byte
*esym
;
355 Elf_External_Sym_Shndx
*alloc_extshndx
;
356 Elf_External_Sym_Shndx
*shndx
;
357 Elf_Internal_Sym
*isym
;
358 Elf_Internal_Sym
*isymend
;
359 const struct elf_backend_data
*bed
;
364 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
370 /* Normal syms might have section extension entries. */
372 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
373 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
375 /* Read the symbols. */
377 alloc_extshndx
= NULL
;
378 bed
= get_elf_backend_data (ibfd
);
379 extsym_size
= bed
->s
->sizeof_sym
;
380 amt
= symcount
* extsym_size
;
381 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
382 if (extsym_buf
== NULL
)
384 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
385 extsym_buf
= alloc_ext
;
387 if (extsym_buf
== NULL
388 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
389 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
395 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
399 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
400 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
401 if (extshndx_buf
== NULL
)
403 alloc_extshndx
= bfd_malloc2 (symcount
,
404 sizeof (Elf_External_Sym_Shndx
));
405 extshndx_buf
= alloc_extshndx
;
407 if (extshndx_buf
== NULL
408 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
409 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
416 if (intsym_buf
== NULL
)
418 intsym_buf
= bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
419 if (intsym_buf
== NULL
)
423 /* Convert the symbols to internal form. */
424 isymend
= intsym_buf
+ symcount
;
425 for (esym
= extsym_buf
, isym
= intsym_buf
, shndx
= extshndx_buf
;
427 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
428 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
430 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
431 (*_bfd_error_handler
) (_("%B symbol number %lu references "
432 "nonexistent SHT_SYMTAB_SHNDX section"),
433 ibfd
, (unsigned long) symoffset
);
439 if (alloc_ext
!= NULL
)
441 if (alloc_extshndx
!= NULL
)
442 free (alloc_extshndx
);
447 /* Look up a symbol name. */
449 bfd_elf_sym_name (bfd
*abfd
,
450 Elf_Internal_Shdr
*symtab_hdr
,
451 Elf_Internal_Sym
*isym
,
455 unsigned int iname
= isym
->st_name
;
456 unsigned int shindex
= symtab_hdr
->sh_link
;
458 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
459 /* Check for a bogus st_shndx to avoid crashing. */
460 && isym
->st_shndx
< elf_numsections (abfd
))
462 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
463 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
466 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
469 else if (sym_sec
&& *name
== '\0')
470 name
= bfd_section_name (abfd
, sym_sec
);
475 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
476 sections. The first element is the flags, the rest are section
479 typedef union elf_internal_group
{
480 Elf_Internal_Shdr
*shdr
;
482 } Elf_Internal_Group
;
484 /* Return the name of the group signature symbol. Why isn't the
485 signature just a string? */
488 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
490 Elf_Internal_Shdr
*hdr
;
491 unsigned char esym
[sizeof (Elf64_External_Sym
)];
492 Elf_External_Sym_Shndx eshndx
;
493 Elf_Internal_Sym isym
;
495 /* First we need to ensure the symbol table is available. Make sure
496 that it is a symbol table section. */
497 if (ghdr
->sh_link
>= elf_numsections (abfd
))
499 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
500 if (hdr
->sh_type
!= SHT_SYMTAB
501 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
504 /* Go read the symbol. */
505 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
506 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
507 &isym
, esym
, &eshndx
) == NULL
)
510 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
513 /* Set next_in_group list pointer, and group name for NEWSECT. */
516 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
518 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
520 /* If num_group is zero, read in all SHT_GROUP sections. The count
521 is set to -1 if there are no SHT_GROUP sections. */
524 unsigned int i
, shnum
;
526 /* First count the number of groups. If we have a SHT_GROUP
527 section with just a flag word (ie. sh_size is 4), ignore it. */
528 shnum
= elf_numsections (abfd
);
531 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
532 ( (shdr)->sh_type == SHT_GROUP \
533 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
534 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
535 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
537 for (i
= 0; i
< shnum
; i
++)
539 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
541 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
547 num_group
= (unsigned) -1;
548 elf_tdata (abfd
)->num_group
= num_group
;
552 /* We keep a list of elf section headers for group sections,
553 so we can find them quickly. */
556 elf_tdata (abfd
)->num_group
= num_group
;
557 elf_tdata (abfd
)->group_sect_ptr
558 = bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
559 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
563 for (i
= 0; i
< shnum
; i
++)
565 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
567 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
570 Elf_Internal_Group
*dest
;
572 /* Add to list of sections. */
573 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
576 /* Read the raw contents. */
577 BFD_ASSERT (sizeof (*dest
) >= 4);
578 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
579 shdr
->contents
= bfd_alloc2 (abfd
, shdr
->sh_size
,
581 /* PR binutils/4110: Handle corrupt group headers. */
582 if (shdr
->contents
== NULL
)
585 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
586 bfd_set_error (bfd_error_bad_value
);
590 memset (shdr
->contents
, 0, amt
);
592 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
593 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
597 /* Translate raw contents, a flag word followed by an
598 array of elf section indices all in target byte order,
599 to the flag word followed by an array of elf section
601 src
= shdr
->contents
+ shdr
->sh_size
;
602 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
609 idx
= H_GET_32 (abfd
, src
);
610 if (src
== shdr
->contents
)
613 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
614 shdr
->bfd_section
->flags
615 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
620 ((*_bfd_error_handler
)
621 (_("%B: invalid SHT_GROUP entry"), abfd
));
624 dest
->shdr
= elf_elfsections (abfd
)[idx
];
631 if (num_group
!= (unsigned) -1)
635 for (i
= 0; i
< num_group
; i
++)
637 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
638 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
639 unsigned int n_elt
= shdr
->sh_size
/ 4;
641 /* Look through this group's sections to see if current
642 section is a member. */
644 if ((++idx
)->shdr
== hdr
)
648 /* We are a member of this group. Go looking through
649 other members to see if any others are linked via
651 idx
= (Elf_Internal_Group
*) shdr
->contents
;
652 n_elt
= shdr
->sh_size
/ 4;
654 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
655 && elf_next_in_group (s
) != NULL
)
659 /* Snarf the group name from other member, and
660 insert current section in circular list. */
661 elf_group_name (newsect
) = elf_group_name (s
);
662 elf_next_in_group (newsect
) = elf_next_in_group (s
);
663 elf_next_in_group (s
) = newsect
;
669 gname
= group_signature (abfd
, shdr
);
672 elf_group_name (newsect
) = gname
;
674 /* Start a circular list with one element. */
675 elf_next_in_group (newsect
) = newsect
;
678 /* If the group section has been created, point to the
680 if (shdr
->bfd_section
!= NULL
)
681 elf_next_in_group (shdr
->bfd_section
) = newsect
;
689 if (elf_group_name (newsect
) == NULL
)
691 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
698 _bfd_elf_setup_sections (bfd
*abfd
)
701 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
702 bfd_boolean result
= TRUE
;
705 /* Process SHF_LINK_ORDER. */
706 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
708 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
709 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
711 unsigned int elfsec
= this_hdr
->sh_link
;
712 /* FIXME: The old Intel compiler and old strip/objcopy may
713 not set the sh_link or sh_info fields. Hence we could
714 get the situation where elfsec is 0. */
717 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
718 if (bed
->link_order_error_handler
)
719 bed
->link_order_error_handler
720 (_("%B: warning: sh_link not set for section `%A'"),
725 asection
*link
= NULL
;
727 if (elfsec
< elf_numsections (abfd
))
729 this_hdr
= elf_elfsections (abfd
)[elfsec
];
730 link
= this_hdr
->bfd_section
;
734 Some strip/objcopy may leave an incorrect value in
735 sh_link. We don't want to proceed. */
738 (*_bfd_error_handler
)
739 (_("%B: sh_link [%d] in section `%A' is incorrect"),
740 s
->owner
, s
, elfsec
);
744 elf_linked_to_section (s
) = link
;
749 /* Process section groups. */
750 if (num_group
== (unsigned) -1)
753 for (i
= 0; i
< num_group
; i
++)
755 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
756 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
757 unsigned int n_elt
= shdr
->sh_size
/ 4;
760 if ((++idx
)->shdr
->bfd_section
)
761 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
762 else if (idx
->shdr
->sh_type
== SHT_RELA
763 || idx
->shdr
->sh_type
== SHT_REL
)
764 /* We won't include relocation sections in section groups in
765 output object files. We adjust the group section size here
766 so that relocatable link will work correctly when
767 relocation sections are in section group in input object
769 shdr
->bfd_section
->size
-= 4;
772 /* There are some unknown sections in the group. */
773 (*_bfd_error_handler
)
774 (_("%B: unknown [%d] section `%s' in group [%s]"),
776 (unsigned int) idx
->shdr
->sh_type
,
777 bfd_elf_string_from_elf_section (abfd
,
778 (elf_elfheader (abfd
)
781 shdr
->bfd_section
->name
);
789 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
791 return elf_next_in_group (sec
) != NULL
;
794 /* Make a BFD section from an ELF section. We store a pointer to the
795 BFD section in the bfd_section field of the header. */
798 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
799 Elf_Internal_Shdr
*hdr
,
805 const struct elf_backend_data
*bed
;
807 if (hdr
->bfd_section
!= NULL
)
809 BFD_ASSERT (strcmp (name
,
810 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
814 newsect
= bfd_make_section_anyway (abfd
, name
);
818 hdr
->bfd_section
= newsect
;
819 elf_section_data (newsect
)->this_hdr
= *hdr
;
820 elf_section_data (newsect
)->this_idx
= shindex
;
822 /* Always use the real type/flags. */
823 elf_section_type (newsect
) = hdr
->sh_type
;
824 elf_section_flags (newsect
) = hdr
->sh_flags
;
826 newsect
->filepos
= hdr
->sh_offset
;
828 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
829 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
830 || ! bfd_set_section_alignment (abfd
, newsect
,
831 bfd_log2 (hdr
->sh_addralign
)))
834 flags
= SEC_NO_FLAGS
;
835 if (hdr
->sh_type
!= SHT_NOBITS
)
836 flags
|= SEC_HAS_CONTENTS
;
837 if (hdr
->sh_type
== SHT_GROUP
)
838 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
839 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
842 if (hdr
->sh_type
!= SHT_NOBITS
)
845 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
846 flags
|= SEC_READONLY
;
847 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
849 else if ((flags
& SEC_LOAD
) != 0)
851 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
854 newsect
->entsize
= hdr
->sh_entsize
;
855 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
856 flags
|= SEC_STRINGS
;
858 if (hdr
->sh_flags
& SHF_GROUP
)
859 if (!setup_group (abfd
, hdr
, newsect
))
861 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
862 flags
|= SEC_THREAD_LOCAL
;
864 if ((flags
& SEC_ALLOC
) == 0)
866 /* The debugging sections appear to be recognized only by name,
867 not any sort of flag. Their SEC_ALLOC bits are cleared. */
872 } debug_sections
[] =
874 { STRING_COMMA_LEN ("debug") }, /* 'd' */
875 { NULL
, 0 }, /* 'e' */
876 { NULL
, 0 }, /* 'f' */
877 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
878 { NULL
, 0 }, /* 'h' */
879 { NULL
, 0 }, /* 'i' */
880 { NULL
, 0 }, /* 'j' */
881 { NULL
, 0 }, /* 'k' */
882 { STRING_COMMA_LEN ("line") }, /* 'l' */
883 { NULL
, 0 }, /* 'm' */
884 { NULL
, 0 }, /* 'n' */
885 { NULL
, 0 }, /* 'o' */
886 { NULL
, 0 }, /* 'p' */
887 { NULL
, 0 }, /* 'q' */
888 { NULL
, 0 }, /* 'r' */
889 { STRING_COMMA_LEN ("stab") } /* 's' */
894 int i
= name
[1] - 'd';
896 && i
< (int) ARRAY_SIZE (debug_sections
)
897 && debug_sections
[i
].name
!= NULL
898 && strncmp (&name
[1], debug_sections
[i
].name
,
899 debug_sections
[i
].len
) == 0)
900 flags
|= SEC_DEBUGGING
;
904 /* As a GNU extension, if the name begins with .gnu.linkonce, we
905 only link a single copy of the section. This is used to support
906 g++. g++ will emit each template expansion in its own section.
907 The symbols will be defined as weak, so that multiple definitions
908 are permitted. The GNU linker extension is to actually discard
909 all but one of the sections. */
910 if (CONST_STRNEQ (name
, ".gnu.linkonce")
911 && elf_next_in_group (newsect
) == NULL
)
912 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
914 bed
= get_elf_backend_data (abfd
);
915 if (bed
->elf_backend_section_flags
)
916 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
919 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
922 /* We do not parse the PT_NOTE segments as we are interested even in the
923 separate debug info files which may have the segments offsets corrupted.
924 PT_NOTEs from the core files are currently not parsed using BFD. */
925 if (hdr
->sh_type
== SHT_NOTE
)
929 contents
= bfd_malloc (hdr
->sh_size
);
933 if (!bfd_get_section_contents (abfd
, hdr
->bfd_section
, contents
, 0,
935 || !elf_parse_notes (abfd
, contents
, hdr
->sh_size
, -1))
944 if ((flags
& SEC_ALLOC
) != 0)
946 Elf_Internal_Phdr
*phdr
;
949 /* Look through the phdrs to see if we need to adjust the lma.
950 If all the p_paddr fields are zero, we ignore them, since
951 some ELF linkers produce such output. */
952 phdr
= elf_tdata (abfd
)->phdr
;
953 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
955 if (phdr
->p_paddr
!= 0)
958 if (i
< elf_elfheader (abfd
)->e_phnum
)
960 phdr
= elf_tdata (abfd
)->phdr
;
961 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
963 /* This section is part of this segment if its file
964 offset plus size lies within the segment's memory
965 span and, if the section is loaded, the extent of the
966 loaded data lies within the extent of the segment.
968 Note - we used to check the p_paddr field as well, and
969 refuse to set the LMA if it was 0. This is wrong
970 though, as a perfectly valid initialised segment can
971 have a p_paddr of zero. Some architectures, eg ARM,
972 place special significance on the address 0 and
973 executables need to be able to have a segment which
974 covers this address. */
975 if (phdr
->p_type
== PT_LOAD
976 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
977 && (hdr
->sh_offset
+ hdr
->sh_size
978 <= phdr
->p_offset
+ phdr
->p_memsz
)
979 && ((flags
& SEC_LOAD
) == 0
980 || (hdr
->sh_offset
+ hdr
->sh_size
981 <= phdr
->p_offset
+ phdr
->p_filesz
)))
983 if ((flags
& SEC_LOAD
) == 0)
984 newsect
->lma
= (phdr
->p_paddr
985 + hdr
->sh_addr
- phdr
->p_vaddr
);
987 /* We used to use the same adjustment for SEC_LOAD
988 sections, but that doesn't work if the segment
989 is packed with code from multiple VMAs.
990 Instead we calculate the section LMA based on
991 the segment LMA. It is assumed that the
992 segment will contain sections with contiguous
993 LMAs, even if the VMAs are not. */
994 newsect
->lma
= (phdr
->p_paddr
995 + hdr
->sh_offset
- phdr
->p_offset
);
997 /* With contiguous segments, we can't tell from file
998 offsets whether a section with zero size should
999 be placed at the end of one segment or the
1000 beginning of the next. Decide based on vaddr. */
1001 if (hdr
->sh_addr
>= phdr
->p_vaddr
1002 && (hdr
->sh_addr
+ hdr
->sh_size
1003 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1015 bfd_elf_find_section
1018 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
1021 Helper functions for GDB to locate the string tables.
1022 Since BFD hides string tables from callers, GDB needs to use an
1023 internal hook to find them. Sun's .stabstr, in particular,
1024 isn't even pointed to by the .stab section, so ordinary
1025 mechanisms wouldn't work to find it, even if we had some.
1028 struct elf_internal_shdr
*
1029 bfd_elf_find_section (bfd
*abfd
, char *name
)
1031 Elf_Internal_Shdr
**i_shdrp
;
1036 i_shdrp
= elf_elfsections (abfd
);
1037 if (i_shdrp
!= NULL
)
1039 shstrtab
= bfd_elf_get_str_section (abfd
,
1040 elf_elfheader (abfd
)->e_shstrndx
);
1041 if (shstrtab
!= NULL
)
1043 max
= elf_numsections (abfd
);
1044 for (i
= 1; i
< max
; i
++)
1045 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
1052 const char *const bfd_elf_section_type_names
[] = {
1053 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1054 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1055 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1058 /* ELF relocs are against symbols. If we are producing relocatable
1059 output, and the reloc is against an external symbol, and nothing
1060 has given us any additional addend, the resulting reloc will also
1061 be against the same symbol. In such a case, we don't want to
1062 change anything about the way the reloc is handled, since it will
1063 all be done at final link time. Rather than put special case code
1064 into bfd_perform_relocation, all the reloc types use this howto
1065 function. It just short circuits the reloc if producing
1066 relocatable output against an external symbol. */
1068 bfd_reloc_status_type
1069 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1070 arelent
*reloc_entry
,
1072 void *data ATTRIBUTE_UNUSED
,
1073 asection
*input_section
,
1075 char **error_message ATTRIBUTE_UNUSED
)
1077 if (output_bfd
!= NULL
1078 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1079 && (! reloc_entry
->howto
->partial_inplace
1080 || reloc_entry
->addend
== 0))
1082 reloc_entry
->address
+= input_section
->output_offset
;
1083 return bfd_reloc_ok
;
1086 return bfd_reloc_continue
;
1089 /* Copy the program header and other data from one object module to
1093 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1095 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1096 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1099 BFD_ASSERT (!elf_flags_init (obfd
)
1100 || (elf_elfheader (obfd
)->e_flags
1101 == elf_elfheader (ibfd
)->e_flags
));
1103 elf_gp (obfd
) = elf_gp (ibfd
);
1104 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1105 elf_flags_init (obfd
) = TRUE
;
1107 /* Copy object attributes. */
1108 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1114 get_segment_type (unsigned int p_type
)
1119 case PT_NULL
: pt
= "NULL"; break;
1120 case PT_LOAD
: pt
= "LOAD"; break;
1121 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1122 case PT_INTERP
: pt
= "INTERP"; break;
1123 case PT_NOTE
: pt
= "NOTE"; break;
1124 case PT_SHLIB
: pt
= "SHLIB"; break;
1125 case PT_PHDR
: pt
= "PHDR"; break;
1126 case PT_TLS
: pt
= "TLS"; break;
1127 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1128 case PT_GNU_STACK
: pt
= "STACK"; break;
1129 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1130 default: pt
= NULL
; break;
1135 /* Print out the program headers. */
1138 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1141 Elf_Internal_Phdr
*p
;
1143 bfd_byte
*dynbuf
= NULL
;
1145 p
= elf_tdata (abfd
)->phdr
;
1150 fprintf (f
, _("\nProgram Header:\n"));
1151 c
= elf_elfheader (abfd
)->e_phnum
;
1152 for (i
= 0; i
< c
; i
++, p
++)
1154 const char *pt
= get_segment_type (p
->p_type
);
1159 sprintf (buf
, "0x%lx", p
->p_type
);
1162 fprintf (f
, "%8s off 0x", pt
);
1163 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1164 fprintf (f
, " vaddr 0x");
1165 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1166 fprintf (f
, " paddr 0x");
1167 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1168 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1169 fprintf (f
, " filesz 0x");
1170 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1171 fprintf (f
, " memsz 0x");
1172 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1173 fprintf (f
, " flags %c%c%c",
1174 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1175 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1176 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1177 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1178 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1183 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1186 unsigned int elfsec
;
1187 unsigned long shlink
;
1188 bfd_byte
*extdyn
, *extdynend
;
1190 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1192 fprintf (f
, _("\nDynamic Section:\n"));
1194 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1197 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1198 if (elfsec
== SHN_BAD
)
1200 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1202 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1203 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1206 extdynend
= extdyn
+ s
->size
;
1207 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1209 Elf_Internal_Dyn dyn
;
1210 const char *name
= "";
1212 bfd_boolean stringp
;
1213 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1215 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1217 if (dyn
.d_tag
== DT_NULL
)
1224 if (bed
->elf_backend_get_target_dtag
)
1225 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1227 if (!strcmp (name
, ""))
1229 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1234 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1235 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1236 case DT_PLTGOT
: name
= "PLTGOT"; break;
1237 case DT_HASH
: name
= "HASH"; break;
1238 case DT_STRTAB
: name
= "STRTAB"; break;
1239 case DT_SYMTAB
: name
= "SYMTAB"; break;
1240 case DT_RELA
: name
= "RELA"; break;
1241 case DT_RELASZ
: name
= "RELASZ"; break;
1242 case DT_RELAENT
: name
= "RELAENT"; break;
1243 case DT_STRSZ
: name
= "STRSZ"; break;
1244 case DT_SYMENT
: name
= "SYMENT"; break;
1245 case DT_INIT
: name
= "INIT"; break;
1246 case DT_FINI
: name
= "FINI"; break;
1247 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1248 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1249 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1250 case DT_REL
: name
= "REL"; break;
1251 case DT_RELSZ
: name
= "RELSZ"; break;
1252 case DT_RELENT
: name
= "RELENT"; break;
1253 case DT_PLTREL
: name
= "PLTREL"; break;
1254 case DT_DEBUG
: name
= "DEBUG"; break;
1255 case DT_TEXTREL
: name
= "TEXTREL"; break;
1256 case DT_JMPREL
: name
= "JMPREL"; break;
1257 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1258 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1259 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1260 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1261 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1262 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1263 case DT_FLAGS
: name
= "FLAGS"; break;
1264 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1265 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1266 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1267 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1268 case DT_MOVEENT
: name
= "MOVEENT"; break;
1269 case DT_MOVESZ
: name
= "MOVESZ"; break;
1270 case DT_FEATURE
: name
= "FEATURE"; break;
1271 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1272 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1273 case DT_SYMINENT
: name
= "SYMINENT"; break;
1274 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1275 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1276 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1277 case DT_PLTPAD
: name
= "PLTPAD"; break;
1278 case DT_MOVETAB
: name
= "MOVETAB"; break;
1279 case DT_SYMINFO
: name
= "SYMINFO"; break;
1280 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1281 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1282 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1283 case DT_VERSYM
: name
= "VERSYM"; break;
1284 case DT_VERDEF
: name
= "VERDEF"; break;
1285 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1286 case DT_VERNEED
: name
= "VERNEED"; break;
1287 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1288 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1289 case DT_USED
: name
= "USED"; break;
1290 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1291 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1294 fprintf (f
, " %-20s ", name
);
1296 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1300 unsigned int tagv
= dyn
.d_un
.d_val
;
1302 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1305 fprintf (f
, "%s", string
);
1314 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1315 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1317 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1321 if (elf_dynverdef (abfd
) != 0)
1323 Elf_Internal_Verdef
*t
;
1325 fprintf (f
, _("\nVersion definitions:\n"));
1326 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1328 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1329 t
->vd_flags
, t
->vd_hash
,
1330 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1331 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1333 Elf_Internal_Verdaux
*a
;
1336 for (a
= t
->vd_auxptr
->vda_nextptr
;
1340 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1346 if (elf_dynverref (abfd
) != 0)
1348 Elf_Internal_Verneed
*t
;
1350 fprintf (f
, _("\nVersion References:\n"));
1351 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1353 Elf_Internal_Vernaux
*a
;
1355 fprintf (f
, _(" required from %s:\n"),
1356 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1357 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1358 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1359 a
->vna_flags
, a
->vna_other
,
1360 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1372 /* Display ELF-specific fields of a symbol. */
1375 bfd_elf_print_symbol (bfd
*abfd
,
1378 bfd_print_symbol_type how
)
1383 case bfd_print_symbol_name
:
1384 fprintf (file
, "%s", symbol
->name
);
1386 case bfd_print_symbol_more
:
1387 fprintf (file
, "elf ");
1388 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1389 fprintf (file
, " %lx", (long) symbol
->flags
);
1391 case bfd_print_symbol_all
:
1393 const char *section_name
;
1394 const char *name
= NULL
;
1395 const struct elf_backend_data
*bed
;
1396 unsigned char st_other
;
1399 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1401 bed
= get_elf_backend_data (abfd
);
1402 if (bed
->elf_backend_print_symbol_all
)
1403 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1407 name
= symbol
->name
;
1408 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1411 fprintf (file
, " %s\t", section_name
);
1412 /* Print the "other" value for a symbol. For common symbols,
1413 we've already printed the size; now print the alignment.
1414 For other symbols, we have no specified alignment, and
1415 we've printed the address; now print the size. */
1416 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1417 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1419 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1420 bfd_fprintf_vma (abfd
, file
, val
);
1422 /* If we have version information, print it. */
1423 if (elf_tdata (abfd
)->dynversym_section
!= 0
1424 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1425 || elf_tdata (abfd
)->dynverref_section
!= 0))
1427 unsigned int vernum
;
1428 const char *version_string
;
1430 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1433 version_string
= "";
1434 else if (vernum
== 1)
1435 version_string
= "Base";
1436 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1438 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1441 Elf_Internal_Verneed
*t
;
1443 version_string
= "";
1444 for (t
= elf_tdata (abfd
)->verref
;
1448 Elf_Internal_Vernaux
*a
;
1450 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1452 if (a
->vna_other
== vernum
)
1454 version_string
= a
->vna_nodename
;
1461 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1462 fprintf (file
, " %-11s", version_string
);
1467 fprintf (file
, " (%s)", version_string
);
1468 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1473 /* If the st_other field is not zero, print it. */
1474 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1479 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1480 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1481 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1483 /* Some other non-defined flags are also present, so print
1485 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1488 fprintf (file
, " %s", name
);
1494 /* Allocate an ELF string table--force the first byte to be zero. */
1496 struct bfd_strtab_hash
*
1497 _bfd_elf_stringtab_init (void)
1499 struct bfd_strtab_hash
*ret
;
1501 ret
= _bfd_stringtab_init ();
1506 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1507 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1508 if (loc
== (bfd_size_type
) -1)
1510 _bfd_stringtab_free (ret
);
1517 /* ELF .o/exec file reading */
1519 /* Create a new bfd section from an ELF section header. */
1522 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1524 Elf_Internal_Shdr
*hdr
;
1525 Elf_Internal_Ehdr
*ehdr
;
1526 const struct elf_backend_data
*bed
;
1529 if (shindex
>= elf_numsections (abfd
))
1532 hdr
= elf_elfsections (abfd
)[shindex
];
1533 ehdr
= elf_elfheader (abfd
);
1534 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1539 bed
= get_elf_backend_data (abfd
);
1540 switch (hdr
->sh_type
)
1543 /* Inactive section. Throw it away. */
1546 case SHT_PROGBITS
: /* Normal section with contents. */
1547 case SHT_NOBITS
: /* .bss section. */
1548 case SHT_HASH
: /* .hash section. */
1549 case SHT_NOTE
: /* .note section. */
1550 case SHT_INIT_ARRAY
: /* .init_array section. */
1551 case SHT_FINI_ARRAY
: /* .fini_array section. */
1552 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1553 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1554 case SHT_GNU_HASH
: /* .gnu.hash section. */
1555 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1557 case SHT_DYNAMIC
: /* Dynamic linking information. */
1558 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1560 if (hdr
->sh_link
> elf_numsections (abfd
)
1561 || elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1563 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1565 Elf_Internal_Shdr
*dynsymhdr
;
1567 /* The shared libraries distributed with hpux11 have a bogus
1568 sh_link field for the ".dynamic" section. Find the
1569 string table for the ".dynsym" section instead. */
1570 if (elf_dynsymtab (abfd
) != 0)
1572 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1573 hdr
->sh_link
= dynsymhdr
->sh_link
;
1577 unsigned int i
, num_sec
;
1579 num_sec
= elf_numsections (abfd
);
1580 for (i
= 1; i
< num_sec
; i
++)
1582 dynsymhdr
= elf_elfsections (abfd
)[i
];
1583 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1585 hdr
->sh_link
= dynsymhdr
->sh_link
;
1593 case SHT_SYMTAB
: /* A symbol table */
1594 if (elf_onesymtab (abfd
) == shindex
)
1597 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1599 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1600 elf_onesymtab (abfd
) = shindex
;
1601 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1602 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1603 abfd
->flags
|= HAS_SYMS
;
1605 /* Sometimes a shared object will map in the symbol table. If
1606 SHF_ALLOC is set, and this is a shared object, then we also
1607 treat this section as a BFD section. We can not base the
1608 decision purely on SHF_ALLOC, because that flag is sometimes
1609 set in a relocatable object file, which would confuse the
1611 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1612 && (abfd
->flags
& DYNAMIC
) != 0
1613 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1617 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1618 can't read symbols without that section loaded as well. It
1619 is most likely specified by the next section header. */
1620 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1622 unsigned int i
, num_sec
;
1624 num_sec
= elf_numsections (abfd
);
1625 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1627 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1628 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1629 && hdr2
->sh_link
== shindex
)
1633 for (i
= 1; i
< shindex
; i
++)
1635 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1636 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1637 && hdr2
->sh_link
== shindex
)
1641 return bfd_section_from_shdr (abfd
, i
);
1645 case SHT_DYNSYM
: /* A dynamic symbol table */
1646 if (elf_dynsymtab (abfd
) == shindex
)
1649 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1651 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1652 elf_dynsymtab (abfd
) = shindex
;
1653 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1654 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1655 abfd
->flags
|= HAS_SYMS
;
1657 /* Besides being a symbol table, we also treat this as a regular
1658 section, so that objcopy can handle it. */
1659 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1661 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1662 if (elf_symtab_shndx (abfd
) == shindex
)
1665 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1666 elf_symtab_shndx (abfd
) = shindex
;
1667 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1668 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1671 case SHT_STRTAB
: /* A string table */
1672 if (hdr
->bfd_section
!= NULL
)
1674 if (ehdr
->e_shstrndx
== shindex
)
1676 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1677 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1680 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1683 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1684 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1687 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1690 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1691 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1692 elf_elfsections (abfd
)[shindex
] = hdr
;
1693 /* We also treat this as a regular section, so that objcopy
1695 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1699 /* If the string table isn't one of the above, then treat it as a
1700 regular section. We need to scan all the headers to be sure,
1701 just in case this strtab section appeared before the above. */
1702 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1704 unsigned int i
, num_sec
;
1706 num_sec
= elf_numsections (abfd
);
1707 for (i
= 1; i
< num_sec
; i
++)
1709 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1710 if (hdr2
->sh_link
== shindex
)
1712 /* Prevent endless recursion on broken objects. */
1715 if (! bfd_section_from_shdr (abfd
, i
))
1717 if (elf_onesymtab (abfd
) == i
)
1719 if (elf_dynsymtab (abfd
) == i
)
1720 goto dynsymtab_strtab
;
1724 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1728 /* *These* do a lot of work -- but build no sections! */
1730 asection
*target_sect
;
1731 Elf_Internal_Shdr
*hdr2
;
1732 unsigned int num_sec
= elf_numsections (abfd
);
1735 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1736 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1739 /* Check for a bogus link to avoid crashing. */
1740 if (hdr
->sh_link
>= num_sec
)
1742 ((*_bfd_error_handler
)
1743 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1744 abfd
, hdr
->sh_link
, name
, shindex
));
1745 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1749 /* For some incomprehensible reason Oracle distributes
1750 libraries for Solaris in which some of the objects have
1751 bogus sh_link fields. It would be nice if we could just
1752 reject them, but, unfortunately, some people need to use
1753 them. We scan through the section headers; if we find only
1754 one suitable symbol table, we clobber the sh_link to point
1755 to it. I hope this doesn't break anything. */
1756 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1757 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1763 for (scan
= 1; scan
< num_sec
; scan
++)
1765 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1766 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1777 hdr
->sh_link
= found
;
1780 /* Get the symbol table. */
1781 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1782 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1783 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1786 /* If this reloc section does not use the main symbol table we
1787 don't treat it as a reloc section. BFD can't adequately
1788 represent such a section, so at least for now, we don't
1789 try. We just present it as a normal section. We also
1790 can't use it as a reloc section if it points to the null
1791 section, an invalid section, or another reloc section. */
1792 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1793 || hdr
->sh_info
== SHN_UNDEF
1794 || hdr
->sh_info
>= num_sec
1795 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1796 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1797 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1800 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1802 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1803 if (target_sect
== NULL
)
1806 if ((target_sect
->flags
& SEC_RELOC
) == 0
1807 || target_sect
->reloc_count
== 0)
1808 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1812 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1813 amt
= sizeof (*hdr2
);
1814 hdr2
= bfd_alloc (abfd
, amt
);
1817 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1820 elf_elfsections (abfd
)[shindex
] = hdr2
;
1821 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1822 target_sect
->flags
|= SEC_RELOC
;
1823 target_sect
->relocation
= NULL
;
1824 target_sect
->rel_filepos
= hdr
->sh_offset
;
1825 /* In the section to which the relocations apply, mark whether
1826 its relocations are of the REL or RELA variety. */
1827 if (hdr
->sh_size
!= 0)
1828 target_sect
->use_rela_p
= hdr
->sh_type
== SHT_RELA
;
1829 abfd
->flags
|= HAS_RELOC
;
1833 case SHT_GNU_verdef
:
1834 elf_dynverdef (abfd
) = shindex
;
1835 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1836 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1838 case SHT_GNU_versym
:
1839 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1841 elf_dynversym (abfd
) = shindex
;
1842 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1843 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1845 case SHT_GNU_verneed
:
1846 elf_dynverref (abfd
) = shindex
;
1847 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1848 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1854 /* We need a BFD section for objcopy and relocatable linking,
1855 and it's handy to have the signature available as the section
1857 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
))
1859 name
= group_signature (abfd
, hdr
);
1862 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1864 if (hdr
->contents
!= NULL
)
1866 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1867 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
1870 if (idx
->flags
& GRP_COMDAT
)
1871 hdr
->bfd_section
->flags
1872 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1874 /* We try to keep the same section order as it comes in. */
1876 while (--n_elt
!= 0)
1880 if (idx
->shdr
!= NULL
1881 && (s
= idx
->shdr
->bfd_section
) != NULL
1882 && elf_next_in_group (s
) != NULL
)
1884 elf_next_in_group (hdr
->bfd_section
) = s
;
1892 /* Possibly an attributes section. */
1893 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
1894 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
1896 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1898 _bfd_elf_parse_attributes (abfd
, hdr
);
1902 /* Check for any processor-specific section types. */
1903 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
1906 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
1908 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1909 /* FIXME: How to properly handle allocated section reserved
1910 for applications? */
1911 (*_bfd_error_handler
)
1912 (_("%B: don't know how to handle allocated, application "
1913 "specific section `%s' [0x%8x]"),
1914 abfd
, name
, hdr
->sh_type
);
1916 /* Allow sections reserved for applications. */
1917 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1920 else if (hdr
->sh_type
>= SHT_LOPROC
1921 && hdr
->sh_type
<= SHT_HIPROC
)
1922 /* FIXME: We should handle this section. */
1923 (*_bfd_error_handler
)
1924 (_("%B: don't know how to handle processor specific section "
1926 abfd
, name
, hdr
->sh_type
);
1927 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
1929 /* Unrecognised OS-specific sections. */
1930 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
1931 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1932 required to correctly process the section and the file should
1933 be rejected with an error message. */
1934 (*_bfd_error_handler
)
1935 (_("%B: don't know how to handle OS specific section "
1937 abfd
, name
, hdr
->sh_type
);
1939 /* Otherwise it should be processed. */
1940 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1943 /* FIXME: We should handle this section. */
1944 (*_bfd_error_handler
)
1945 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1946 abfd
, name
, hdr
->sh_type
);
1954 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1955 Return SEC for sections that have no elf section, and NULL on error. */
1958 bfd_section_from_r_symndx (bfd
*abfd
,
1959 struct sym_sec_cache
*cache
,
1961 unsigned long r_symndx
)
1963 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1966 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
1968 Elf_Internal_Shdr
*symtab_hdr
;
1969 unsigned char esym
[sizeof (Elf64_External_Sym
)];
1970 Elf_External_Sym_Shndx eshndx
;
1971 Elf_Internal_Sym isym
;
1973 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1974 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
1975 &isym
, esym
, &eshndx
) == NULL
)
1978 if (cache
->abfd
!= abfd
)
1980 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1983 cache
->indx
[ent
] = r_symndx
;
1984 cache
->shndx
[ent
] = isym
.st_shndx
;
1987 s
= bfd_section_from_elf_index (abfd
, cache
->shndx
[ent
]);
1994 /* Given an ELF section number, retrieve the corresponding BFD
1998 bfd_section_from_elf_index (bfd
*abfd
, unsigned int index
)
2000 if (index
>= elf_numsections (abfd
))
2002 return elf_elfsections (abfd
)[index
]->bfd_section
;
2005 static const struct bfd_elf_special_section special_sections_b
[] =
2007 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2008 { NULL
, 0, 0, 0, 0 }
2011 static const struct bfd_elf_special_section special_sections_c
[] =
2013 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2014 { NULL
, 0, 0, 0, 0 }
2017 static const struct bfd_elf_special_section special_sections_d
[] =
2019 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2020 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2021 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2022 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2023 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2024 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2025 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2026 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2027 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2028 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2029 { NULL
, 0, 0, 0, 0 }
2032 static const struct bfd_elf_special_section special_sections_f
[] =
2034 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2035 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2036 { NULL
, 0, 0, 0, 0 }
2039 static const struct bfd_elf_special_section special_sections_g
[] =
2041 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2042 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2043 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2044 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2045 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2046 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2047 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2048 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2049 { NULL
, 0, 0, 0, 0 }
2052 static const struct bfd_elf_special_section special_sections_h
[] =
2054 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2055 { NULL
, 0, 0, 0, 0 }
2058 static const struct bfd_elf_special_section special_sections_i
[] =
2060 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2061 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2062 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2063 { NULL
, 0, 0, 0, 0 }
2066 static const struct bfd_elf_special_section special_sections_l
[] =
2068 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2069 { NULL
, 0, 0, 0, 0 }
2072 static const struct bfd_elf_special_section special_sections_n
[] =
2074 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2075 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2076 { NULL
, 0, 0, 0, 0 }
2079 static const struct bfd_elf_special_section special_sections_p
[] =
2081 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2082 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2083 { NULL
, 0, 0, 0, 0 }
2086 static const struct bfd_elf_special_section special_sections_r
[] =
2088 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2089 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2090 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2091 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2092 { NULL
, 0, 0, 0, 0 }
2095 static const struct bfd_elf_special_section special_sections_s
[] =
2097 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2098 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2099 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2100 /* See struct bfd_elf_special_section declaration for the semantics of
2101 this special case where .prefix_length != strlen (.prefix). */
2102 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2103 { NULL
, 0, 0, 0, 0 }
2106 static const struct bfd_elf_special_section special_sections_t
[] =
2108 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2109 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2110 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2111 { NULL
, 0, 0, 0, 0 }
2114 static const struct bfd_elf_special_section
*special_sections
[] =
2116 special_sections_b
, /* 'b' */
2117 special_sections_c
, /* 'c' */
2118 special_sections_d
, /* 'd' */
2120 special_sections_f
, /* 'f' */
2121 special_sections_g
, /* 'g' */
2122 special_sections_h
, /* 'h' */
2123 special_sections_i
, /* 'i' */
2126 special_sections_l
, /* 'l' */
2128 special_sections_n
, /* 'n' */
2130 special_sections_p
, /* 'p' */
2132 special_sections_r
, /* 'r' */
2133 special_sections_s
, /* 's' */
2134 special_sections_t
, /* 't' */
2137 const struct bfd_elf_special_section
*
2138 _bfd_elf_get_special_section (const char *name
,
2139 const struct bfd_elf_special_section
*spec
,
2145 len
= strlen (name
);
2147 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2150 int prefix_len
= spec
[i
].prefix_length
;
2152 if (len
< prefix_len
)
2154 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2157 suffix_len
= spec
[i
].suffix_length
;
2158 if (suffix_len
<= 0)
2160 if (name
[prefix_len
] != 0)
2162 if (suffix_len
== 0)
2164 if (name
[prefix_len
] != '.'
2165 && (suffix_len
== -2
2166 || (rela
&& spec
[i
].type
== SHT_REL
)))
2172 if (len
< prefix_len
+ suffix_len
)
2174 if (memcmp (name
+ len
- suffix_len
,
2175 spec
[i
].prefix
+ prefix_len
,
2185 const struct bfd_elf_special_section
*
2186 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2189 const struct bfd_elf_special_section
*spec
;
2190 const struct elf_backend_data
*bed
;
2192 /* See if this is one of the special sections. */
2193 if (sec
->name
== NULL
)
2196 bed
= get_elf_backend_data (abfd
);
2197 spec
= bed
->special_sections
;
2200 spec
= _bfd_elf_get_special_section (sec
->name
,
2201 bed
->special_sections
,
2207 if (sec
->name
[0] != '.')
2210 i
= sec
->name
[1] - 'b';
2211 if (i
< 0 || i
> 't' - 'b')
2214 spec
= special_sections
[i
];
2219 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2223 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2225 struct bfd_elf_section_data
*sdata
;
2226 const struct elf_backend_data
*bed
;
2227 const struct bfd_elf_special_section
*ssect
;
2229 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2232 sdata
= bfd_zalloc (abfd
, sizeof (*sdata
));
2235 sec
->used_by_bfd
= sdata
;
2238 /* Indicate whether or not this section should use RELA relocations. */
2239 bed
= get_elf_backend_data (abfd
);
2240 sec
->use_rela_p
= bed
->default_use_rela_p
;
2242 /* When we read a file, we don't need to set ELF section type and
2243 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2244 anyway. We will set ELF section type and flags for all linker
2245 created sections. If user specifies BFD section flags, we will
2246 set ELF section type and flags based on BFD section flags in
2247 elf_fake_sections. */
2248 if ((!sec
->flags
&& abfd
->direction
!= read_direction
)
2249 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2251 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2254 elf_section_type (sec
) = ssect
->type
;
2255 elf_section_flags (sec
) = ssect
->attr
;
2259 return _bfd_generic_new_section_hook (abfd
, sec
);
2262 /* Create a new bfd section from an ELF program header.
2264 Since program segments have no names, we generate a synthetic name
2265 of the form segment<NUM>, where NUM is generally the index in the
2266 program header table. For segments that are split (see below) we
2267 generate the names segment<NUM>a and segment<NUM>b.
2269 Note that some program segments may have a file size that is different than
2270 (less than) the memory size. All this means is that at execution the
2271 system must allocate the amount of memory specified by the memory size,
2272 but only initialize it with the first "file size" bytes read from the
2273 file. This would occur for example, with program segments consisting
2274 of combined data+bss.
2276 To handle the above situation, this routine generates TWO bfd sections
2277 for the single program segment. The first has the length specified by
2278 the file size of the segment, and the second has the length specified
2279 by the difference between the two sizes. In effect, the segment is split
2280 into its initialized and uninitialized parts.
2285 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2286 Elf_Internal_Phdr
*hdr
,
2288 const char *typename
)
2296 split
= ((hdr
->p_memsz
> 0)
2297 && (hdr
->p_filesz
> 0)
2298 && (hdr
->p_memsz
> hdr
->p_filesz
));
2300 if (hdr
->p_filesz
> 0)
2302 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2303 len
= strlen (namebuf
) + 1;
2304 name
= bfd_alloc (abfd
, len
);
2307 memcpy (name
, namebuf
, len
);
2308 newsect
= bfd_make_section (abfd
, name
);
2309 if (newsect
== NULL
)
2311 newsect
->vma
= hdr
->p_vaddr
;
2312 newsect
->lma
= hdr
->p_paddr
;
2313 newsect
->size
= hdr
->p_filesz
;
2314 newsect
->filepos
= hdr
->p_offset
;
2315 newsect
->flags
|= SEC_HAS_CONTENTS
;
2316 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2317 if (hdr
->p_type
== PT_LOAD
)
2319 newsect
->flags
|= SEC_ALLOC
;
2320 newsect
->flags
|= SEC_LOAD
;
2321 if (hdr
->p_flags
& PF_X
)
2323 /* FIXME: all we known is that it has execute PERMISSION,
2325 newsect
->flags
|= SEC_CODE
;
2328 if (!(hdr
->p_flags
& PF_W
))
2330 newsect
->flags
|= SEC_READONLY
;
2334 if (hdr
->p_memsz
> hdr
->p_filesz
)
2338 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "b" : "");
2339 len
= strlen (namebuf
) + 1;
2340 name
= bfd_alloc (abfd
, len
);
2343 memcpy (name
, namebuf
, len
);
2344 newsect
= bfd_make_section (abfd
, name
);
2345 if (newsect
== NULL
)
2347 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2348 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2349 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2350 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2351 align
= newsect
->vma
& -newsect
->vma
;
2352 if (align
== 0 || align
> hdr
->p_align
)
2353 align
= hdr
->p_align
;
2354 newsect
->alignment_power
= bfd_log2 (align
);
2355 if (hdr
->p_type
== PT_LOAD
)
2357 /* Hack for gdb. Segments that have not been modified do
2358 not have their contents written to a core file, on the
2359 assumption that a debugger can find the contents in the
2360 executable. We flag this case by setting the fake
2361 section size to zero. Note that "real" bss sections will
2362 always have their contents dumped to the core file. */
2363 if (bfd_get_format (abfd
) == bfd_core
)
2365 newsect
->flags
|= SEC_ALLOC
;
2366 if (hdr
->p_flags
& PF_X
)
2367 newsect
->flags
|= SEC_CODE
;
2369 if (!(hdr
->p_flags
& PF_W
))
2370 newsect
->flags
|= SEC_READONLY
;
2377 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int index
)
2379 const struct elf_backend_data
*bed
;
2381 switch (hdr
->p_type
)
2384 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2387 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2390 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2393 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2396 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2398 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2403 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2406 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2408 case PT_GNU_EH_FRAME
:
2409 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
,
2413 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "stack");
2416 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "relro");
2419 /* Check for any processor-specific program segment types. */
2420 bed
= get_elf_backend_data (abfd
);
2421 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, index
, "proc");
2425 /* Initialize REL_HDR, the section-header for new section, containing
2426 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2427 relocations; otherwise, we use REL relocations. */
2430 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2431 Elf_Internal_Shdr
*rel_hdr
,
2433 bfd_boolean use_rela_p
)
2436 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2437 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2439 name
= bfd_alloc (abfd
, amt
);
2442 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2444 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2446 if (rel_hdr
->sh_name
== (unsigned int) -1)
2448 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2449 rel_hdr
->sh_entsize
= (use_rela_p
2450 ? bed
->s
->sizeof_rela
2451 : bed
->s
->sizeof_rel
);
2452 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2453 rel_hdr
->sh_flags
= 0;
2454 rel_hdr
->sh_addr
= 0;
2455 rel_hdr
->sh_size
= 0;
2456 rel_hdr
->sh_offset
= 0;
2461 /* Set up an ELF internal section header for a section. */
2464 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *failedptrarg
)
2466 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2467 bfd_boolean
*failedptr
= failedptrarg
;
2468 Elf_Internal_Shdr
*this_hdr
;
2469 unsigned int sh_type
;
2473 /* We already failed; just get out of the bfd_map_over_sections
2478 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2480 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2481 asect
->name
, FALSE
);
2482 if (this_hdr
->sh_name
== (unsigned int) -1)
2488 /* Don't clear sh_flags. Assembler may set additional bits. */
2490 if ((asect
->flags
& SEC_ALLOC
) != 0
2491 || asect
->user_set_vma
)
2492 this_hdr
->sh_addr
= asect
->vma
;
2494 this_hdr
->sh_addr
= 0;
2496 this_hdr
->sh_offset
= 0;
2497 this_hdr
->sh_size
= asect
->size
;
2498 this_hdr
->sh_link
= 0;
2499 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2500 /* The sh_entsize and sh_info fields may have been set already by
2501 copy_private_section_data. */
2503 this_hdr
->bfd_section
= asect
;
2504 this_hdr
->contents
= NULL
;
2506 /* If the section type is unspecified, we set it based on
2508 if ((asect
->flags
& SEC_GROUP
) != 0)
2509 sh_type
= SHT_GROUP
;
2510 else if ((asect
->flags
& SEC_ALLOC
) != 0
2511 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2512 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2513 sh_type
= SHT_NOBITS
;
2515 sh_type
= SHT_PROGBITS
;
2517 if (this_hdr
->sh_type
== SHT_NULL
)
2518 this_hdr
->sh_type
= sh_type
;
2519 else if (this_hdr
->sh_type
== SHT_NOBITS
2520 && sh_type
== SHT_PROGBITS
2521 && (asect
->flags
& SEC_ALLOC
) != 0)
2523 /* Warn if we are changing a NOBITS section to PROGBITS, but
2524 allow the link to proceed. This can happen when users link
2525 non-bss input sections to bss output sections, or emit data
2526 to a bss output section via a linker script. */
2527 (*_bfd_error_handler
)
2528 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2529 this_hdr
->sh_type
= sh_type
;
2532 switch (this_hdr
->sh_type
)
2538 case SHT_INIT_ARRAY
:
2539 case SHT_FINI_ARRAY
:
2540 case SHT_PREINIT_ARRAY
:
2547 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2551 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2555 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2559 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2560 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2564 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2565 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2568 case SHT_GNU_versym
:
2569 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2572 case SHT_GNU_verdef
:
2573 this_hdr
->sh_entsize
= 0;
2574 /* objcopy or strip will copy over sh_info, but may not set
2575 cverdefs. The linker will set cverdefs, but sh_info will be
2577 if (this_hdr
->sh_info
== 0)
2578 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2580 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2581 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2584 case SHT_GNU_verneed
:
2585 this_hdr
->sh_entsize
= 0;
2586 /* objcopy or strip will copy over sh_info, but may not set
2587 cverrefs. The linker will set cverrefs, but sh_info will be
2589 if (this_hdr
->sh_info
== 0)
2590 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2592 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2593 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2597 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2601 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2605 if ((asect
->flags
& SEC_ALLOC
) != 0)
2606 this_hdr
->sh_flags
|= SHF_ALLOC
;
2607 if ((asect
->flags
& SEC_READONLY
) == 0)
2608 this_hdr
->sh_flags
|= SHF_WRITE
;
2609 if ((asect
->flags
& SEC_CODE
) != 0)
2610 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2611 if ((asect
->flags
& SEC_MERGE
) != 0)
2613 this_hdr
->sh_flags
|= SHF_MERGE
;
2614 this_hdr
->sh_entsize
= asect
->entsize
;
2615 if ((asect
->flags
& SEC_STRINGS
) != 0)
2616 this_hdr
->sh_flags
|= SHF_STRINGS
;
2618 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2619 this_hdr
->sh_flags
|= SHF_GROUP
;
2620 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2622 this_hdr
->sh_flags
|= SHF_TLS
;
2623 if (asect
->size
== 0
2624 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2626 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2628 this_hdr
->sh_size
= 0;
2631 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2632 if (this_hdr
->sh_size
!= 0)
2633 this_hdr
->sh_type
= SHT_NOBITS
;
2638 /* Check for processor-specific section types. */
2639 sh_type
= this_hdr
->sh_type
;
2640 if (bed
->elf_backend_fake_sections
2641 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2644 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2646 /* Don't change the header type from NOBITS if we are being
2647 called for objcopy --only-keep-debug. */
2648 this_hdr
->sh_type
= sh_type
;
2651 /* If the section has relocs, set up a section header for the
2652 SHT_REL[A] section. If two relocation sections are required for
2653 this section, it is up to the processor-specific back-end to
2654 create the other. */
2655 if ((asect
->flags
& SEC_RELOC
) != 0
2656 && !_bfd_elf_init_reloc_shdr (abfd
,
2657 &elf_section_data (asect
)->rel_hdr
,
2663 /* Fill in the contents of a SHT_GROUP section. */
2666 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2668 bfd_boolean
*failedptr
= failedptrarg
;
2669 unsigned long symindx
;
2670 asection
*elt
, *first
;
2674 /* Ignore linker created group section. See elfNN_ia64_object_p in
2676 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2681 if (elf_group_id (sec
) != NULL
)
2682 symindx
= elf_group_id (sec
)->udata
.i
;
2686 /* If called from the assembler, swap_out_syms will have set up
2687 elf_section_syms; If called for "ld -r", use target_index. */
2688 if (elf_section_syms (abfd
) != NULL
)
2689 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2691 symindx
= sec
->target_index
;
2693 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2695 /* The contents won't be allocated for "ld -r" or objcopy. */
2697 if (sec
->contents
== NULL
)
2700 sec
->contents
= bfd_alloc (abfd
, sec
->size
);
2702 /* Arrange for the section to be written out. */
2703 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2704 if (sec
->contents
== NULL
)
2711 loc
= sec
->contents
+ sec
->size
;
2713 /* Get the pointer to the first section in the group that gas
2714 squirreled away here. objcopy arranges for this to be set to the
2715 start of the input section group. */
2716 first
= elt
= elf_next_in_group (sec
);
2718 /* First element is a flag word. Rest of section is elf section
2719 indices for all the sections of the group. Write them backwards
2720 just to keep the group in the same order as given in .section
2721 directives, not that it matters. */
2730 s
= s
->output_section
;
2733 idx
= elf_section_data (s
)->this_idx
;
2734 H_PUT_32 (abfd
, idx
, loc
);
2735 elt
= elf_next_in_group (elt
);
2740 if ((loc
-= 4) != sec
->contents
)
2743 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2746 /* Assign all ELF section numbers. The dummy first section is handled here
2747 too. The link/info pointers for the standard section types are filled
2748 in here too, while we're at it. */
2751 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
2753 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2755 unsigned int section_number
, secn
;
2756 Elf_Internal_Shdr
**i_shdrp
;
2757 struct bfd_elf_section_data
*d
;
2761 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2763 /* SHT_GROUP sections are in relocatable files only. */
2764 if (link_info
== NULL
|| link_info
->relocatable
)
2766 /* Put SHT_GROUP sections first. */
2767 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2769 d
= elf_section_data (sec
);
2771 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2773 if (sec
->flags
& SEC_LINKER_CREATED
)
2775 /* Remove the linker created SHT_GROUP sections. */
2776 bfd_section_list_remove (abfd
, sec
);
2777 abfd
->section_count
--;
2780 d
->this_idx
= section_number
++;
2785 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2787 d
= elf_section_data (sec
);
2789 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2790 d
->this_idx
= section_number
++;
2791 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2792 if ((sec
->flags
& SEC_RELOC
) == 0)
2796 d
->rel_idx
= section_number
++;
2797 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2802 d
->rel_idx2
= section_number
++;
2803 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2809 t
->shstrtab_section
= section_number
++;
2810 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2811 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2813 if (bfd_get_symcount (abfd
) > 0)
2815 t
->symtab_section
= section_number
++;
2816 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2817 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
2819 t
->symtab_shndx_section
= section_number
++;
2820 t
->symtab_shndx_hdr
.sh_name
2821 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2822 ".symtab_shndx", FALSE
);
2823 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2826 t
->strtab_section
= section_number
++;
2827 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2830 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2831 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2833 elf_numsections (abfd
) = section_number
;
2834 elf_elfheader (abfd
)->e_shnum
= section_number
;
2836 /* Set up the list of section header pointers, in agreement with the
2838 i_shdrp
= bfd_zalloc2 (abfd
, section_number
, sizeof (Elf_Internal_Shdr
*));
2839 if (i_shdrp
== NULL
)
2842 i_shdrp
[0] = bfd_zalloc (abfd
, sizeof (Elf_Internal_Shdr
));
2843 if (i_shdrp
[0] == NULL
)
2845 bfd_release (abfd
, i_shdrp
);
2849 elf_elfsections (abfd
) = i_shdrp
;
2851 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2852 if (bfd_get_symcount (abfd
) > 0)
2854 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2855 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
2857 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2858 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2860 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2861 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2864 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2866 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2870 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2871 if (d
->rel_idx
!= 0)
2872 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2873 if (d
->rel_idx2
!= 0)
2874 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2876 /* Fill in the sh_link and sh_info fields while we're at it. */
2878 /* sh_link of a reloc section is the section index of the symbol
2879 table. sh_info is the section index of the section to which
2880 the relocation entries apply. */
2881 if (d
->rel_idx
!= 0)
2883 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2884 d
->rel_hdr
.sh_info
= d
->this_idx
;
2886 if (d
->rel_idx2
!= 0)
2888 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2889 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2892 /* We need to set up sh_link for SHF_LINK_ORDER. */
2893 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
2895 s
= elf_linked_to_section (sec
);
2898 /* elf_linked_to_section points to the input section. */
2899 if (link_info
!= NULL
)
2901 /* Check discarded linkonce section. */
2902 if (elf_discarded_section (s
))
2905 (*_bfd_error_handler
)
2906 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2907 abfd
, d
->this_hdr
.bfd_section
,
2909 /* Point to the kept section if it has the same
2910 size as the discarded one. */
2911 kept
= _bfd_elf_check_kept_section (s
, link_info
);
2914 bfd_set_error (bfd_error_bad_value
);
2920 s
= s
->output_section
;
2921 BFD_ASSERT (s
!= NULL
);
2925 /* Handle objcopy. */
2926 if (s
->output_section
== NULL
)
2928 (*_bfd_error_handler
)
2929 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2930 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
2931 bfd_set_error (bfd_error_bad_value
);
2934 s
= s
->output_section
;
2936 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2941 The Intel C compiler generates SHT_IA_64_UNWIND with
2942 SHF_LINK_ORDER. But it doesn't set the sh_link or
2943 sh_info fields. Hence we could get the situation
2945 const struct elf_backend_data
*bed
2946 = get_elf_backend_data (abfd
);
2947 if (bed
->link_order_error_handler
)
2948 bed
->link_order_error_handler
2949 (_("%B: warning: sh_link not set for section `%A'"),
2954 switch (d
->this_hdr
.sh_type
)
2958 /* A reloc section which we are treating as a normal BFD
2959 section. sh_link is the section index of the symbol
2960 table. sh_info is the section index of the section to
2961 which the relocation entries apply. We assume that an
2962 allocated reloc section uses the dynamic symbol table.
2963 FIXME: How can we be sure? */
2964 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2966 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2968 /* We look up the section the relocs apply to by name. */
2970 if (d
->this_hdr
.sh_type
== SHT_REL
)
2974 s
= bfd_get_section_by_name (abfd
, name
);
2976 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2980 /* We assume that a section named .stab*str is a stabs
2981 string section. We look for a section with the same name
2982 but without the trailing ``str'', and set its sh_link
2983 field to point to this section. */
2984 if (CONST_STRNEQ (sec
->name
, ".stab")
2985 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2990 len
= strlen (sec
->name
);
2991 alc
= bfd_malloc (len
- 2);
2994 memcpy (alc
, sec
->name
, len
- 3);
2995 alc
[len
- 3] = '\0';
2996 s
= bfd_get_section_by_name (abfd
, alc
);
3000 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3002 /* This is a .stab section. */
3003 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3004 elf_section_data (s
)->this_hdr
.sh_entsize
3005 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3012 case SHT_GNU_verneed
:
3013 case SHT_GNU_verdef
:
3014 /* sh_link is the section header index of the string table
3015 used for the dynamic entries, or the symbol table, or the
3017 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3019 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3022 case SHT_GNU_LIBLIST
:
3023 /* sh_link is the section header index of the prelink library
3024 list used for the dynamic entries, or the symbol table, or
3025 the version strings. */
3026 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3027 ? ".dynstr" : ".gnu.libstr");
3029 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3034 case SHT_GNU_versym
:
3035 /* sh_link is the section header index of the symbol table
3036 this hash table or version table is for. */
3037 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3039 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3043 d
->this_hdr
.sh_link
= t
->symtab_section
;
3047 for (secn
= 1; secn
< section_number
; ++secn
)
3048 if (i_shdrp
[secn
] == NULL
)
3049 i_shdrp
[secn
] = i_shdrp
[0];
3051 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3052 i_shdrp
[secn
]->sh_name
);
3056 /* Map symbol from it's internal number to the external number, moving
3057 all local symbols to be at the head of the list. */
3060 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3062 /* If the backend has a special mapping, use it. */
3063 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3064 if (bed
->elf_backend_sym_is_global
)
3065 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3067 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
3068 || bfd_is_und_section (bfd_get_section (sym
))
3069 || bfd_is_com_section (bfd_get_section (sym
)));
3072 /* Don't output section symbols for sections that are not going to be
3073 output. Also, don't output section symbols for reloc and other
3074 special sections. */
3077 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3079 return ((sym
->flags
& BSF_SECTION_SYM
) != 0
3081 || (sym
->section
->owner
!= abfd
3082 && (sym
->section
->output_section
->owner
!= abfd
3083 || sym
->section
->output_offset
!= 0))));
3087 elf_map_symbols (bfd
*abfd
)
3089 unsigned int symcount
= bfd_get_symcount (abfd
);
3090 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3091 asymbol
**sect_syms
;
3092 unsigned int num_locals
= 0;
3093 unsigned int num_globals
= 0;
3094 unsigned int num_locals2
= 0;
3095 unsigned int num_globals2
= 0;
3102 fprintf (stderr
, "elf_map_symbols\n");
3106 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3108 if (max_index
< asect
->index
)
3109 max_index
= asect
->index
;
3113 sect_syms
= bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3114 if (sect_syms
== NULL
)
3116 elf_section_syms (abfd
) = sect_syms
;
3117 elf_num_section_syms (abfd
) = max_index
;
3119 /* Init sect_syms entries for any section symbols we have already
3120 decided to output. */
3121 for (idx
= 0; idx
< symcount
; idx
++)
3123 asymbol
*sym
= syms
[idx
];
3125 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3126 && !ignore_section_sym (abfd
, sym
))
3128 asection
*sec
= sym
->section
;
3130 if (sec
->owner
!= abfd
)
3131 sec
= sec
->output_section
;
3133 sect_syms
[sec
->index
] = syms
[idx
];
3137 /* Classify all of the symbols. */
3138 for (idx
= 0; idx
< symcount
; idx
++)
3140 if (ignore_section_sym (abfd
, syms
[idx
]))
3142 if (!sym_is_global (abfd
, syms
[idx
]))
3148 /* We will be adding a section symbol for each normal BFD section. Most
3149 sections will already have a section symbol in outsymbols, but
3150 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3151 at least in that case. */
3152 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3154 if (sect_syms
[asect
->index
] == NULL
)
3156 if (!sym_is_global (abfd
, asect
->symbol
))
3163 /* Now sort the symbols so the local symbols are first. */
3164 new_syms
= bfd_alloc2 (abfd
, num_locals
+ num_globals
, sizeof (asymbol
*));
3166 if (new_syms
== NULL
)
3169 for (idx
= 0; idx
< symcount
; idx
++)
3171 asymbol
*sym
= syms
[idx
];
3174 if (ignore_section_sym (abfd
, sym
))
3176 if (!sym_is_global (abfd
, sym
))
3179 i
= num_locals
+ num_globals2
++;
3181 sym
->udata
.i
= i
+ 1;
3183 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3185 if (sect_syms
[asect
->index
] == NULL
)
3187 asymbol
*sym
= asect
->symbol
;
3190 sect_syms
[asect
->index
] = sym
;
3191 if (!sym_is_global (abfd
, sym
))
3194 i
= num_locals
+ num_globals2
++;
3196 sym
->udata
.i
= i
+ 1;
3200 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3202 elf_num_locals (abfd
) = num_locals
;
3203 elf_num_globals (abfd
) = num_globals
;
3207 /* Align to the maximum file alignment that could be required for any
3208 ELF data structure. */
3210 static inline file_ptr
3211 align_file_position (file_ptr off
, int align
)
3213 return (off
+ align
- 1) & ~(align
- 1);
3216 /* Assign a file position to a section, optionally aligning to the
3217 required section alignment. */
3220 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3224 if (align
&& i_shdrp
->sh_addralign
> 1)
3225 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3226 i_shdrp
->sh_offset
= offset
;
3227 if (i_shdrp
->bfd_section
!= NULL
)
3228 i_shdrp
->bfd_section
->filepos
= offset
;
3229 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3230 offset
+= i_shdrp
->sh_size
;
3234 /* Compute the file positions we are going to put the sections at, and
3235 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3236 is not NULL, this is being called by the ELF backend linker. */
3239 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3240 struct bfd_link_info
*link_info
)
3242 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3244 struct bfd_strtab_hash
*strtab
= NULL
;
3245 Elf_Internal_Shdr
*shstrtab_hdr
;
3247 if (abfd
->output_has_begun
)
3250 /* Do any elf backend specific processing first. */
3251 if (bed
->elf_backend_begin_write_processing
)
3252 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3254 if (! prep_headers (abfd
))
3257 /* Post process the headers if necessary. */
3258 if (bed
->elf_backend_post_process_headers
)
3259 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3262 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3266 if (!assign_section_numbers (abfd
, link_info
))
3269 /* The backend linker builds symbol table information itself. */
3270 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3272 /* Non-zero if doing a relocatable link. */
3273 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3275 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3279 if (link_info
== NULL
)
3281 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3286 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3287 /* sh_name was set in prep_headers. */
3288 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3289 shstrtab_hdr
->sh_flags
= 0;
3290 shstrtab_hdr
->sh_addr
= 0;
3291 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3292 shstrtab_hdr
->sh_entsize
= 0;
3293 shstrtab_hdr
->sh_link
= 0;
3294 shstrtab_hdr
->sh_info
= 0;
3295 /* sh_offset is set in assign_file_positions_except_relocs. */
3296 shstrtab_hdr
->sh_addralign
= 1;
3298 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3301 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3304 Elf_Internal_Shdr
*hdr
;
3306 off
= elf_tdata (abfd
)->next_file_pos
;
3308 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3309 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3311 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3312 if (hdr
->sh_size
!= 0)
3313 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3315 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3316 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3318 elf_tdata (abfd
)->next_file_pos
= off
;
3320 /* Now that we know where the .strtab section goes, write it
3322 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3323 || ! _bfd_stringtab_emit (abfd
, strtab
))
3325 _bfd_stringtab_free (strtab
);
3328 abfd
->output_has_begun
= TRUE
;
3333 /* Make an initial estimate of the size of the program header. If we
3334 get the number wrong here, we'll redo section placement. */
3336 static bfd_size_type
3337 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3341 const struct elf_backend_data
*bed
;
3343 /* Assume we will need exactly two PT_LOAD segments: one for text
3344 and one for data. */
3347 s
= bfd_get_section_by_name (abfd
, ".interp");
3348 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3350 /* If we have a loadable interpreter section, we need a
3351 PT_INTERP segment. In this case, assume we also need a
3352 PT_PHDR segment, although that may not be true for all
3357 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3359 /* We need a PT_DYNAMIC segment. */
3365 /* We need a PT_GNU_RELRO segment. */
3369 if (elf_tdata (abfd
)->eh_frame_hdr
)
3371 /* We need a PT_GNU_EH_FRAME segment. */
3375 if (elf_tdata (abfd
)->stack_flags
)
3377 /* We need a PT_GNU_STACK segment. */
3381 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3383 if ((s
->flags
& SEC_LOAD
) != 0
3384 && CONST_STRNEQ (s
->name
, ".note"))
3386 /* We need a PT_NOTE segment. */
3388 /* Try to create just one PT_NOTE segment
3389 for all adjacent loadable .note* sections.
3390 gABI requires that within a PT_NOTE segment
3391 (and also inside of each SHT_NOTE section)
3392 each note is padded to a multiple of 4 size,
3393 so we check whether the sections are correctly
3395 if (s
->alignment_power
== 2)
3396 while (s
->next
!= NULL
3397 && s
->next
->alignment_power
== 2
3398 && (s
->next
->flags
& SEC_LOAD
) != 0
3399 && CONST_STRNEQ (s
->next
->name
, ".note"))
3404 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3406 if (s
->flags
& SEC_THREAD_LOCAL
)
3408 /* We need a PT_TLS segment. */
3414 /* Let the backend count up any program headers it might need. */
3415 bed
= get_elf_backend_data (abfd
);
3416 if (bed
->elf_backend_additional_program_headers
)
3420 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3426 return segs
* bed
->s
->sizeof_phdr
;
3429 /* Find the segment that contains the output_section of section. */
3432 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3434 struct elf_segment_map
*m
;
3435 Elf_Internal_Phdr
*p
;
3437 for (m
= elf_tdata (abfd
)->segment_map
,
3438 p
= elf_tdata (abfd
)->phdr
;
3444 for (i
= m
->count
- 1; i
>= 0; i
--)
3445 if (m
->sections
[i
] == section
)
3452 /* Create a mapping from a set of sections to a program segment. */
3454 static struct elf_segment_map
*
3455 make_mapping (bfd
*abfd
,
3456 asection
**sections
,
3461 struct elf_segment_map
*m
;
3466 amt
= sizeof (struct elf_segment_map
);
3467 amt
+= (to
- from
- 1) * sizeof (asection
*);
3468 m
= bfd_zalloc (abfd
, amt
);
3472 m
->p_type
= PT_LOAD
;
3473 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3474 m
->sections
[i
- from
] = *hdrpp
;
3475 m
->count
= to
- from
;
3477 if (from
== 0 && phdr
)
3479 /* Include the headers in the first PT_LOAD segment. */
3480 m
->includes_filehdr
= 1;
3481 m
->includes_phdrs
= 1;
3487 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3490 struct elf_segment_map
*
3491 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3493 struct elf_segment_map
*m
;
3495 m
= bfd_zalloc (abfd
, sizeof (struct elf_segment_map
));
3499 m
->p_type
= PT_DYNAMIC
;
3501 m
->sections
[0] = dynsec
;
3506 /* Possibly add or remove segments from the segment map. */
3509 elf_modify_segment_map (bfd
*abfd
,
3510 struct bfd_link_info
*info
,
3511 bfd_boolean remove_empty_load
)
3513 struct elf_segment_map
**m
;
3514 const struct elf_backend_data
*bed
;
3516 /* The placement algorithm assumes that non allocated sections are
3517 not in PT_LOAD segments. We ensure this here by removing such
3518 sections from the segment map. We also remove excluded
3519 sections. Finally, any PT_LOAD segment without sections is
3521 m
= &elf_tdata (abfd
)->segment_map
;
3524 unsigned int i
, new_count
;
3526 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3528 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3529 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3530 || (*m
)->p_type
!= PT_LOAD
))
3532 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3536 (*m
)->count
= new_count
;
3538 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3544 bed
= get_elf_backend_data (abfd
);
3545 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3547 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3554 /* Set up a mapping from BFD sections to program segments. */
3557 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3560 struct elf_segment_map
*m
;
3561 asection
**sections
= NULL
;
3562 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3563 bfd_boolean no_user_phdrs
;
3565 no_user_phdrs
= elf_tdata (abfd
)->segment_map
== NULL
;
3566 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3570 struct elf_segment_map
*mfirst
;
3571 struct elf_segment_map
**pm
;
3574 unsigned int phdr_index
;
3575 bfd_vma maxpagesize
;
3577 bfd_boolean phdr_in_segment
= TRUE
;
3578 bfd_boolean writable
;
3580 asection
*first_tls
= NULL
;
3581 asection
*dynsec
, *eh_frame_hdr
;
3584 /* Select the allocated sections, and sort them. */
3586 sections
= bfd_malloc2 (bfd_count_sections (abfd
), sizeof (asection
*));
3587 if (sections
== NULL
)
3591 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3593 if ((s
->flags
& SEC_ALLOC
) != 0)
3599 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3602 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3604 /* Build the mapping. */
3609 /* If we have a .interp section, then create a PT_PHDR segment for
3610 the program headers and a PT_INTERP segment for the .interp
3612 s
= bfd_get_section_by_name (abfd
, ".interp");
3613 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3615 amt
= sizeof (struct elf_segment_map
);
3616 m
= bfd_zalloc (abfd
, amt
);
3620 m
->p_type
= PT_PHDR
;
3621 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3622 m
->p_flags
= PF_R
| PF_X
;
3623 m
->p_flags_valid
= 1;
3624 m
->includes_phdrs
= 1;
3629 amt
= sizeof (struct elf_segment_map
);
3630 m
= bfd_zalloc (abfd
, amt
);
3634 m
->p_type
= PT_INTERP
;
3642 /* Look through the sections. We put sections in the same program
3643 segment when the start of the second section can be placed within
3644 a few bytes of the end of the first section. */
3648 maxpagesize
= bed
->maxpagesize
;
3650 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3652 && (dynsec
->flags
& SEC_LOAD
) == 0)
3655 /* Deal with -Ttext or something similar such that the first section
3656 is not adjacent to the program headers. This is an
3657 approximation, since at this point we don't know exactly how many
3658 program headers we will need. */
3661 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
3663 if (phdr_size
== (bfd_size_type
) -1)
3664 phdr_size
= get_program_header_size (abfd
, info
);
3665 if ((abfd
->flags
& D_PAGED
) == 0
3666 || sections
[0]->lma
< phdr_size
3667 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3668 phdr_in_segment
= FALSE
;
3671 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3674 bfd_boolean new_segment
;
3678 /* See if this section and the last one will fit in the same
3681 if (last_hdr
== NULL
)
3683 /* If we don't have a segment yet, then we don't need a new
3684 one (we build the last one after this loop). */
3685 new_segment
= FALSE
;
3687 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3689 /* If this section has a different relation between the
3690 virtual address and the load address, then we need a new
3694 else if (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
3695 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3697 /* If putting this section in this segment would force us to
3698 skip a page in the segment, then we need a new segment. */
3701 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3702 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3704 /* We don't want to put a loadable section after a
3705 nonloadable section in the same segment.
3706 Consider .tbss sections as loadable for this purpose. */
3709 else if ((abfd
->flags
& D_PAGED
) == 0)
3711 /* If the file is not demand paged, which means that we
3712 don't require the sections to be correctly aligned in the
3713 file, then there is no other reason for a new segment. */
3714 new_segment
= FALSE
;
3717 && (hdr
->flags
& SEC_READONLY
) == 0
3718 && (((last_hdr
->lma
+ last_size
- 1)
3719 & ~(maxpagesize
- 1))
3720 != (hdr
->lma
& ~(maxpagesize
- 1))))
3722 /* We don't want to put a writable section in a read only
3723 segment, unless they are on the same page in memory
3724 anyhow. We already know that the last section does not
3725 bring us past the current section on the page, so the
3726 only case in which the new section is not on the same
3727 page as the previous section is when the previous section
3728 ends precisely on a page boundary. */
3733 /* Otherwise, we can use the same segment. */
3734 new_segment
= FALSE
;
3737 /* Allow interested parties a chance to override our decision. */
3738 if (last_hdr
&& info
->callbacks
->override_segment_assignment
)
3739 new_segment
= info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
, last_hdr
, new_segment
);
3743 if ((hdr
->flags
& SEC_READONLY
) == 0)
3746 /* .tbss sections effectively have zero size. */
3747 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
3748 != SEC_THREAD_LOCAL
)
3749 last_size
= hdr
->size
;
3755 /* We need a new program segment. We must create a new program
3756 header holding all the sections from phdr_index until hdr. */
3758 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3765 if ((hdr
->flags
& SEC_READONLY
) == 0)
3771 /* .tbss sections effectively have zero size. */
3772 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3773 last_size
= hdr
->size
;
3777 phdr_in_segment
= FALSE
;
3780 /* Create a final PT_LOAD program segment. */
3781 if (last_hdr
!= NULL
)
3783 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3791 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3794 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
3801 /* For each batch of consecutive loadable .note sections,
3802 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3803 because if we link together nonloadable .note sections and
3804 loadable .note sections, we will generate two .note sections
3805 in the output file. FIXME: Using names for section types is
3807 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3809 if ((s
->flags
& SEC_LOAD
) != 0
3810 && CONST_STRNEQ (s
->name
, ".note"))
3814 amt
= sizeof (struct elf_segment_map
);
3815 if (s
->alignment_power
== 2)
3816 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
3818 if (s2
->next
->alignment_power
== 2
3819 && (s2
->next
->flags
& SEC_LOAD
) != 0
3820 && CONST_STRNEQ (s2
->next
->name
, ".note")
3821 && align_power (s2
->vma
+ s2
->size
, 2)
3827 amt
+= (count
- 1) * sizeof (asection
*);
3828 m
= bfd_zalloc (abfd
, amt
);
3832 m
->p_type
= PT_NOTE
;
3836 m
->sections
[m
->count
- count
--] = s
;
3837 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
3840 m
->sections
[m
->count
- 1] = s
;
3841 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
3845 if (s
->flags
& SEC_THREAD_LOCAL
)
3853 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3858 amt
= sizeof (struct elf_segment_map
);
3859 amt
+= (tls_count
- 1) * sizeof (asection
*);
3860 m
= bfd_zalloc (abfd
, amt
);
3865 m
->count
= tls_count
;
3866 /* Mandated PF_R. */
3868 m
->p_flags_valid
= 1;
3869 for (i
= 0; i
< tls_count
; ++i
)
3871 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3872 m
->sections
[i
] = first_tls
;
3873 first_tls
= first_tls
->next
;
3880 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3882 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
3883 if (eh_frame_hdr
!= NULL
3884 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
3886 amt
= sizeof (struct elf_segment_map
);
3887 m
= bfd_zalloc (abfd
, amt
);
3891 m
->p_type
= PT_GNU_EH_FRAME
;
3893 m
->sections
[0] = eh_frame_hdr
->output_section
;
3899 if (elf_tdata (abfd
)->stack_flags
)
3901 amt
= sizeof (struct elf_segment_map
);
3902 m
= bfd_zalloc (abfd
, amt
);
3906 m
->p_type
= PT_GNU_STACK
;
3907 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
3908 m
->p_flags_valid
= 1;
3916 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3918 if (m
->p_type
== PT_LOAD
)
3920 asection
*last
= m
->sections
[m
->count
- 1];
3921 bfd_vma vaddr
= m
->sections
[0]->vma
;
3922 bfd_vma filesz
= last
->vma
- vaddr
+ last
->size
;
3924 if (vaddr
< info
->relro_end
3925 && vaddr
>= info
->relro_start
3926 && (vaddr
+ filesz
) >= info
->relro_end
)
3931 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3934 amt
= sizeof (struct elf_segment_map
);
3935 m
= bfd_zalloc (abfd
, amt
);
3939 m
->p_type
= PT_GNU_RELRO
;
3941 m
->p_flags_valid
= 1;
3949 elf_tdata (abfd
)->segment_map
= mfirst
;
3952 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
3955 for (count
= 0, m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3957 elf_tdata (abfd
)->program_header_size
= count
* bed
->s
->sizeof_phdr
;
3962 if (sections
!= NULL
)
3967 /* Sort sections by address. */
3970 elf_sort_sections (const void *arg1
, const void *arg2
)
3972 const asection
*sec1
= *(const asection
**) arg1
;
3973 const asection
*sec2
= *(const asection
**) arg2
;
3974 bfd_size_type size1
, size2
;
3976 /* Sort by LMA first, since this is the address used to
3977 place the section into a segment. */
3978 if (sec1
->lma
< sec2
->lma
)
3980 else if (sec1
->lma
> sec2
->lma
)
3983 /* Then sort by VMA. Normally the LMA and the VMA will be
3984 the same, and this will do nothing. */
3985 if (sec1
->vma
< sec2
->vma
)
3987 else if (sec1
->vma
> sec2
->vma
)
3990 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3992 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3998 /* If the indicies are the same, do not return 0
3999 here, but continue to try the next comparison. */
4000 if (sec1
->target_index
- sec2
->target_index
!= 0)
4001 return sec1
->target_index
- sec2
->target_index
;
4006 else if (TOEND (sec2
))
4011 /* Sort by size, to put zero sized sections
4012 before others at the same address. */
4014 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4015 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4022 return sec1
->target_index
- sec2
->target_index
;
4025 /* Ian Lance Taylor writes:
4027 We shouldn't be using % with a negative signed number. That's just
4028 not good. We have to make sure either that the number is not
4029 negative, or that the number has an unsigned type. When the types
4030 are all the same size they wind up as unsigned. When file_ptr is a
4031 larger signed type, the arithmetic winds up as signed long long,
4034 What we're trying to say here is something like ``increase OFF by
4035 the least amount that will cause it to be equal to the VMA modulo
4037 /* In other words, something like:
4039 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4040 off_offset = off % bed->maxpagesize;
4041 if (vma_offset < off_offset)
4042 adjustment = vma_offset + bed->maxpagesize - off_offset;
4044 adjustment = vma_offset - off_offset;
4046 which can can be collapsed into the expression below. */
4049 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4051 return ((vma
- off
) % maxpagesize
);
4055 print_segment_map (const struct elf_segment_map
*m
)
4058 const char *pt
= get_segment_type (m
->p_type
);
4063 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4064 sprintf (buf
, "LOPROC+%7.7x",
4065 (unsigned int) (m
->p_type
- PT_LOPROC
));
4066 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4067 sprintf (buf
, "LOOS+%7.7x",
4068 (unsigned int) (m
->p_type
- PT_LOOS
));
4070 snprintf (buf
, sizeof (buf
), "%8.8x",
4071 (unsigned int) m
->p_type
);
4074 fprintf (stderr
, "%s:", pt
);
4075 for (j
= 0; j
< m
->count
; j
++)
4076 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4080 /* Assign file positions to the sections based on the mapping from
4081 sections to segments. This function also sets up some fields in
4085 assign_file_positions_for_load_sections (bfd
*abfd
,
4086 struct bfd_link_info
*link_info
)
4088 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4089 struct elf_segment_map
*m
;
4090 Elf_Internal_Phdr
*phdrs
;
4091 Elf_Internal_Phdr
*p
;
4093 bfd_size_type maxpagesize
;
4097 if (link_info
== NULL
4098 && !elf_modify_segment_map (abfd
, link_info
, FALSE
))
4102 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4105 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4106 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4107 elf_elfheader (abfd
)->e_phnum
= alloc
;
4109 if (elf_tdata (abfd
)->program_header_size
== (bfd_size_type
) -1)
4110 elf_tdata (abfd
)->program_header_size
= alloc
* bed
->s
->sizeof_phdr
;
4112 BFD_ASSERT (elf_tdata (abfd
)->program_header_size
4113 >= alloc
* bed
->s
->sizeof_phdr
);
4117 elf_tdata (abfd
)->next_file_pos
= bed
->s
->sizeof_ehdr
;
4121 phdrs
= bfd_alloc2 (abfd
, alloc
, sizeof (Elf_Internal_Phdr
));
4122 elf_tdata (abfd
)->phdr
= phdrs
;
4127 if ((abfd
->flags
& D_PAGED
) != 0)
4128 maxpagesize
= bed
->maxpagesize
;
4130 off
= bed
->s
->sizeof_ehdr
;
4131 off
+= alloc
* bed
->s
->sizeof_phdr
;
4133 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
, j
= 0;
4135 m
= m
->next
, p
++, j
++)
4139 bfd_boolean no_contents
;
4141 /* If elf_segment_map is not from map_sections_to_segments, the
4142 sections may not be correctly ordered. NOTE: sorting should
4143 not be done to the PT_NOTE section of a corefile, which may
4144 contain several pseudo-sections artificially created by bfd.
4145 Sorting these pseudo-sections breaks things badly. */
4147 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4148 && m
->p_type
== PT_NOTE
))
4149 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4152 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4153 number of sections with contents contributing to both p_filesz
4154 and p_memsz, followed by a number of sections with no contents
4155 that just contribute to p_memsz. In this loop, OFF tracks next
4156 available file offset for PT_LOAD and PT_NOTE segments. */
4157 p
->p_type
= m
->p_type
;
4158 p
->p_flags
= m
->p_flags
;
4163 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4165 if (m
->p_paddr_valid
)
4166 p
->p_paddr
= m
->p_paddr
;
4167 else if (m
->count
== 0)
4170 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4172 if (p
->p_type
== PT_LOAD
4173 && (abfd
->flags
& D_PAGED
) != 0)
4175 /* p_align in demand paged PT_LOAD segments effectively stores
4176 the maximum page size. When copying an executable with
4177 objcopy, we set m->p_align from the input file. Use this
4178 value for maxpagesize rather than bed->maxpagesize, which
4179 may be different. Note that we use maxpagesize for PT_TLS
4180 segment alignment later in this function, so we are relying
4181 on at least one PT_LOAD segment appearing before a PT_TLS
4183 if (m
->p_align_valid
)
4184 maxpagesize
= m
->p_align
;
4186 p
->p_align
= maxpagesize
;
4188 else if (m
->p_align_valid
)
4189 p
->p_align
= m
->p_align
;
4190 else if (m
->count
== 0)
4191 p
->p_align
= 1 << bed
->s
->log_file_align
;
4195 no_contents
= FALSE
;
4197 if (p
->p_type
== PT_LOAD
4200 bfd_size_type align
;
4201 unsigned int align_power
= 0;
4203 if (m
->p_align_valid
)
4207 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4209 unsigned int secalign
;
4211 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4212 if (secalign
> align_power
)
4213 align_power
= secalign
;
4215 align
= (bfd_size_type
) 1 << align_power
;
4216 if (align
< maxpagesize
)
4217 align
= maxpagesize
;
4220 for (i
= 0; i
< m
->count
; i
++)
4221 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4222 /* If we aren't making room for this section, then
4223 it must be SHT_NOBITS regardless of what we've
4224 set via struct bfd_elf_special_section. */
4225 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4227 /* Find out whether this segment contains any loadable
4228 sections. If the first section isn't loadable, the same
4229 holds for any other sections. */
4231 while (elf_section_type (m
->sections
[i
]) == SHT_NOBITS
)
4233 /* If a segment starts with .tbss, we need to look
4234 at the next section to decide whether the segment
4235 has any loadable sections. */
4236 if ((elf_section_flags (m
->sections
[i
]) & SHF_TLS
) == 0
4244 off_adjust
= vma_page_aligned_bias (m
->sections
[0]->vma
, off
, align
);
4248 /* We shouldn't need to align the segment on disk since
4249 the segment doesn't need file space, but the gABI
4250 arguably requires the alignment and glibc ld.so
4251 checks it. So to comply with the alignment
4252 requirement but not waste file space, we adjust
4253 p_offset for just this segment. (OFF_ADJUST is
4254 subtracted from OFF later.) This may put p_offset
4255 past the end of file, but that shouldn't matter. */
4260 /* Make sure the .dynamic section is the first section in the
4261 PT_DYNAMIC segment. */
4262 else if (p
->p_type
== PT_DYNAMIC
4264 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4267 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4269 bfd_set_error (bfd_error_bad_value
);
4272 /* Set the note section type to SHT_NOTE. */
4273 else if (p
->p_type
== PT_NOTE
)
4274 for (i
= 0; i
< m
->count
; i
++)
4275 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4281 if (m
->includes_filehdr
)
4283 if (!m
->p_flags_valid
)
4285 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4286 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4289 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4291 if (p
->p_vaddr
< (bfd_vma
) off
)
4293 (*_bfd_error_handler
)
4294 (_("%B: Not enough room for program headers, try linking with -N"),
4296 bfd_set_error (bfd_error_bad_value
);
4301 if (!m
->p_paddr_valid
)
4306 if (m
->includes_phdrs
)
4308 if (!m
->p_flags_valid
)
4311 if (!m
->includes_filehdr
)
4313 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4317 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4318 p
->p_vaddr
-= off
- p
->p_offset
;
4319 if (!m
->p_paddr_valid
)
4320 p
->p_paddr
-= off
- p
->p_offset
;
4324 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4325 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4328 if (p
->p_type
== PT_LOAD
4329 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4331 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4337 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4339 p
->p_filesz
+= adjust
;
4340 p
->p_memsz
+= adjust
;
4344 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4345 maps. Set filepos for sections in PT_LOAD segments, and in
4346 core files, for sections in PT_NOTE segments.
4347 assign_file_positions_for_non_load_sections will set filepos
4348 for other sections and update p_filesz for other segments. */
4349 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4352 bfd_size_type align
;
4353 Elf_Internal_Shdr
*this_hdr
;
4356 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4357 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4359 if (p
->p_type
== PT_LOAD
4360 || p
->p_type
== PT_TLS
)
4362 bfd_signed_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
4364 if (this_hdr
->sh_type
!= SHT_NOBITS
4365 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4366 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4367 || p
->p_type
== PT_TLS
)))
4371 (*_bfd_error_handler
)
4372 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4373 abfd
, sec
, (unsigned long) sec
->lma
);
4376 p
->p_memsz
+= adjust
;
4378 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4381 p
->p_filesz
+= adjust
;
4386 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4388 /* The section at i == 0 is the one that actually contains
4392 this_hdr
->sh_offset
= sec
->filepos
= off
;
4393 off
+= this_hdr
->sh_size
;
4394 p
->p_filesz
= this_hdr
->sh_size
;
4400 /* The rest are fake sections that shouldn't be written. */
4409 if (p
->p_type
== PT_LOAD
)
4411 this_hdr
->sh_offset
= sec
->filepos
= off
;
4412 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4413 off
+= this_hdr
->sh_size
;
4416 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4418 p
->p_filesz
+= this_hdr
->sh_size
;
4419 /* A load section without SHF_ALLOC is something like
4420 a note section in a PT_NOTE segment. These take
4421 file space but are not loaded into memory. */
4422 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4423 p
->p_memsz
+= this_hdr
->sh_size
;
4425 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4427 if (p
->p_type
== PT_TLS
)
4428 p
->p_memsz
+= this_hdr
->sh_size
;
4430 /* .tbss is special. It doesn't contribute to p_memsz of
4432 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4433 p
->p_memsz
+= this_hdr
->sh_size
;
4436 if (align
> p
->p_align
4437 && !m
->p_align_valid
4438 && (p
->p_type
!= PT_LOAD
4439 || (abfd
->flags
& D_PAGED
) == 0))
4443 if (!m
->p_flags_valid
)
4446 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4448 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4454 /* Check that all sections are in a PT_LOAD segment.
4455 Don't check funky gdb generated core files. */
4456 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4457 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4459 Elf_Internal_Shdr
*this_hdr
;
4463 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4464 if (this_hdr
->sh_size
!= 0
4465 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, p
))
4467 (*_bfd_error_handler
)
4468 (_("%B: section `%A' can't be allocated in segment %d"),
4470 print_segment_map (m
);
4471 bfd_set_error (bfd_error_bad_value
);
4477 elf_tdata (abfd
)->next_file_pos
= off
;
4481 /* Assign file positions for the other sections. */
4484 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4485 struct bfd_link_info
*link_info
)
4487 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4488 Elf_Internal_Shdr
**i_shdrpp
;
4489 Elf_Internal_Shdr
**hdrpp
;
4490 Elf_Internal_Phdr
*phdrs
;
4491 Elf_Internal_Phdr
*p
;
4492 struct elf_segment_map
*m
;
4493 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4494 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4496 unsigned int num_sec
;
4500 i_shdrpp
= elf_elfsections (abfd
);
4501 num_sec
= elf_numsections (abfd
);
4502 off
= elf_tdata (abfd
)->next_file_pos
;
4503 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4505 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4506 Elf_Internal_Shdr
*hdr
;
4509 if (hdr
->bfd_section
!= NULL
4510 && (hdr
->bfd_section
->filepos
!= 0
4511 || (hdr
->sh_type
== SHT_NOBITS
4512 && hdr
->contents
== NULL
)))
4513 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4514 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4516 if (hdr
->sh_size
!= 0)
4517 ((*_bfd_error_handler
)
4518 (_("%B: warning: allocated section `%s' not in segment"),
4520 (hdr
->bfd_section
== NULL
4522 : hdr
->bfd_section
->name
)));
4523 /* We don't need to page align empty sections. */
4524 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4525 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4528 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4530 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4533 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4534 && hdr
->bfd_section
== NULL
)
4535 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4536 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4537 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4538 hdr
->sh_offset
= -1;
4540 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4543 /* Now that we have set the section file positions, we can set up
4544 the file positions for the non PT_LOAD segments. */
4548 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4550 phdrs
= elf_tdata (abfd
)->phdr
;
4551 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4556 if (p
->p_type
!= PT_LOAD
)
4559 if (m
->includes_filehdr
)
4561 filehdr_vaddr
= p
->p_vaddr
;
4562 filehdr_paddr
= p
->p_paddr
;
4564 if (m
->includes_phdrs
)
4566 phdrs_vaddr
= p
->p_vaddr
;
4567 phdrs_paddr
= p
->p_paddr
;
4568 if (m
->includes_filehdr
)
4570 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
4571 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
4576 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4582 if (p
->p_type
!= PT_LOAD
4583 && (p
->p_type
!= PT_NOTE
4584 || bfd_get_format (abfd
) != bfd_core
))
4586 Elf_Internal_Shdr
*hdr
;
4589 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
4591 sect
= m
->sections
[m
->count
- 1];
4592 hdr
= &elf_section_data (sect
)->this_hdr
;
4593 p
->p_filesz
= sect
->filepos
- m
->sections
[0]->filepos
;
4594 if (hdr
->sh_type
!= SHT_NOBITS
)
4595 p
->p_filesz
+= hdr
->sh_size
;
4597 if (p
->p_type
== PT_GNU_RELRO
)
4599 /* When we get here, we are copying executable
4600 or shared library. But we need to use the same
4602 Elf_Internal_Phdr
*lp
;
4604 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4606 if (lp
->p_type
== PT_LOAD
4607 && lp
->p_paddr
== p
->p_paddr
)
4611 if (lp
< phdrs
+ count
)
4613 /* We should use p_size if it is valid since it
4614 may contain the first few bytes of the next
4615 SEC_ALLOC section. */
4616 if (m
->p_size_valid
)
4617 p
->p_filesz
= m
->p_size
;
4620 p
->p_vaddr
= lp
->p_vaddr
;
4621 p
->p_offset
= lp
->p_offset
;
4622 p
->p_memsz
= p
->p_filesz
;
4629 p
->p_offset
= m
->sections
[0]->filepos
;
4634 if (m
->includes_filehdr
)
4636 p
->p_vaddr
= filehdr_vaddr
;
4637 if (! m
->p_paddr_valid
)
4638 p
->p_paddr
= filehdr_paddr
;
4640 else if (m
->includes_phdrs
)
4642 p
->p_vaddr
= phdrs_vaddr
;
4643 if (! m
->p_paddr_valid
)
4644 p
->p_paddr
= phdrs_paddr
;
4646 else if (p
->p_type
== PT_GNU_RELRO
)
4648 Elf_Internal_Phdr
*lp
;
4650 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4652 if (lp
->p_type
== PT_LOAD
4653 && lp
->p_vaddr
<= link_info
->relro_end
4654 && lp
->p_vaddr
>= link_info
->relro_start
4655 && (lp
->p_vaddr
+ lp
->p_filesz
4656 >= link_info
->relro_end
))
4660 if (lp
< phdrs
+ count
4661 && link_info
->relro_end
> lp
->p_vaddr
)
4663 p
->p_vaddr
= lp
->p_vaddr
;
4664 p
->p_paddr
= lp
->p_paddr
;
4665 p
->p_offset
= lp
->p_offset
;
4666 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
4667 p
->p_memsz
= p
->p_filesz
;
4669 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
4673 memset (p
, 0, sizeof *p
);
4674 p
->p_type
= PT_NULL
;
4680 elf_tdata (abfd
)->next_file_pos
= off
;
4685 /* Work out the file positions of all the sections. This is called by
4686 _bfd_elf_compute_section_file_positions. All the section sizes and
4687 VMAs must be known before this is called.
4689 Reloc sections come in two flavours: Those processed specially as
4690 "side-channel" data attached to a section to which they apply, and
4691 those that bfd doesn't process as relocations. The latter sort are
4692 stored in a normal bfd section by bfd_section_from_shdr. We don't
4693 consider the former sort here, unless they form part of the loadable
4694 image. Reloc sections not assigned here will be handled later by
4695 assign_file_positions_for_relocs.
4697 We also don't set the positions of the .symtab and .strtab here. */
4700 assign_file_positions_except_relocs (bfd
*abfd
,
4701 struct bfd_link_info
*link_info
)
4703 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4704 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
4706 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4708 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4709 && bfd_get_format (abfd
) != bfd_core
)
4711 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4712 unsigned int num_sec
= elf_numsections (abfd
);
4713 Elf_Internal_Shdr
**hdrpp
;
4716 /* Start after the ELF header. */
4717 off
= i_ehdrp
->e_ehsize
;
4719 /* We are not creating an executable, which means that we are
4720 not creating a program header, and that the actual order of
4721 the sections in the file is unimportant. */
4722 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4724 Elf_Internal_Shdr
*hdr
;
4727 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4728 && hdr
->bfd_section
== NULL
)
4729 || i
== tdata
->symtab_section
4730 || i
== tdata
->symtab_shndx_section
4731 || i
== tdata
->strtab_section
)
4733 hdr
->sh_offset
= -1;
4736 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4743 /* Assign file positions for the loaded sections based on the
4744 assignment of sections to segments. */
4745 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
4748 /* And for non-load sections. */
4749 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
4752 if (bed
->elf_backend_modify_program_headers
!= NULL
)
4754 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
4758 /* Write out the program headers. */
4759 alloc
= tdata
->program_header_size
/ bed
->s
->sizeof_phdr
;
4760 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4761 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
4764 off
= tdata
->next_file_pos
;
4767 /* Place the section headers. */
4768 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
4769 i_ehdrp
->e_shoff
= off
;
4770 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4772 tdata
->next_file_pos
= off
;
4778 prep_headers (bfd
*abfd
)
4780 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4781 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4782 struct elf_strtab_hash
*shstrtab
;
4783 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4785 i_ehdrp
= elf_elfheader (abfd
);
4787 shstrtab
= _bfd_elf_strtab_init ();
4788 if (shstrtab
== NULL
)
4791 elf_shstrtab (abfd
) = shstrtab
;
4793 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4794 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4795 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4796 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4798 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4799 i_ehdrp
->e_ident
[EI_DATA
] =
4800 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4801 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4803 if ((abfd
->flags
& DYNAMIC
) != 0)
4804 i_ehdrp
->e_type
= ET_DYN
;
4805 else if ((abfd
->flags
& EXEC_P
) != 0)
4806 i_ehdrp
->e_type
= ET_EXEC
;
4807 else if (bfd_get_format (abfd
) == bfd_core
)
4808 i_ehdrp
->e_type
= ET_CORE
;
4810 i_ehdrp
->e_type
= ET_REL
;
4812 switch (bfd_get_arch (abfd
))
4814 case bfd_arch_unknown
:
4815 i_ehdrp
->e_machine
= EM_NONE
;
4818 /* There used to be a long list of cases here, each one setting
4819 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4820 in the corresponding bfd definition. To avoid duplication,
4821 the switch was removed. Machines that need special handling
4822 can generally do it in elf_backend_final_write_processing(),
4823 unless they need the information earlier than the final write.
4824 Such need can generally be supplied by replacing the tests for
4825 e_machine with the conditions used to determine it. */
4827 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
4830 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4831 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4833 /* No program header, for now. */
4834 i_ehdrp
->e_phoff
= 0;
4835 i_ehdrp
->e_phentsize
= 0;
4836 i_ehdrp
->e_phnum
= 0;
4838 /* Each bfd section is section header entry. */
4839 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4840 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4842 /* If we're building an executable, we'll need a program header table. */
4843 if (abfd
->flags
& EXEC_P
)
4844 /* It all happens later. */
4848 i_ehdrp
->e_phentsize
= 0;
4850 i_ehdrp
->e_phoff
= 0;
4853 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4854 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
4855 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4856 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
4857 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4858 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
4859 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4860 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4861 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4867 /* Assign file positions for all the reloc sections which are not part
4868 of the loadable file image. */
4871 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
4874 unsigned int i
, num_sec
;
4875 Elf_Internal_Shdr
**shdrpp
;
4877 off
= elf_tdata (abfd
)->next_file_pos
;
4879 num_sec
= elf_numsections (abfd
);
4880 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4882 Elf_Internal_Shdr
*shdrp
;
4885 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4886 && shdrp
->sh_offset
== -1)
4887 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
4890 elf_tdata (abfd
)->next_file_pos
= off
;
4894 _bfd_elf_write_object_contents (bfd
*abfd
)
4896 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4897 Elf_Internal_Ehdr
*i_ehdrp
;
4898 Elf_Internal_Shdr
**i_shdrp
;
4900 unsigned int count
, num_sec
;
4902 if (! abfd
->output_has_begun
4903 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
4906 i_shdrp
= elf_elfsections (abfd
);
4907 i_ehdrp
= elf_elfheader (abfd
);
4910 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4914 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4916 /* After writing the headers, we need to write the sections too... */
4917 num_sec
= elf_numsections (abfd
);
4918 for (count
= 1; count
< num_sec
; count
++)
4920 if (bed
->elf_backend_section_processing
)
4921 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4922 if (i_shdrp
[count
]->contents
)
4924 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4926 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4927 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4932 /* Write out the section header names. */
4933 if (elf_shstrtab (abfd
) != NULL
4934 && (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4935 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
4938 if (bed
->elf_backend_final_write_processing
)
4939 (*bed
->elf_backend_final_write_processing
) (abfd
,
4940 elf_tdata (abfd
)->linker
);
4942 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
4945 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4946 if (elf_tdata (abfd
)->after_write_object_contents
)
4947 return (*elf_tdata (abfd
)->after_write_object_contents
) (abfd
);
4953 _bfd_elf_write_corefile_contents (bfd
*abfd
)
4955 /* Hopefully this can be done just like an object file. */
4956 return _bfd_elf_write_object_contents (abfd
);
4959 /* Given a section, search the header to find them. */
4962 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
4964 const struct elf_backend_data
*bed
;
4967 if (elf_section_data (asect
) != NULL
4968 && elf_section_data (asect
)->this_idx
!= 0)
4969 return elf_section_data (asect
)->this_idx
;
4971 if (bfd_is_abs_section (asect
))
4973 else if (bfd_is_com_section (asect
))
4975 else if (bfd_is_und_section (asect
))
4980 bed
= get_elf_backend_data (abfd
);
4981 if (bed
->elf_backend_section_from_bfd_section
)
4985 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4989 if (index
== SHN_BAD
)
4990 bfd_set_error (bfd_error_nonrepresentable_section
);
4995 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4999 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5001 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5003 flagword flags
= asym_ptr
->flags
;
5005 /* When gas creates relocations against local labels, it creates its
5006 own symbol for the section, but does put the symbol into the
5007 symbol chain, so udata is 0. When the linker is generating
5008 relocatable output, this section symbol may be for one of the
5009 input sections rather than the output section. */
5010 if (asym_ptr
->udata
.i
== 0
5011 && (flags
& BSF_SECTION_SYM
)
5012 && asym_ptr
->section
)
5017 sec
= asym_ptr
->section
;
5018 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5019 sec
= sec
->output_section
;
5020 if (sec
->owner
== abfd
5021 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5022 && elf_section_syms (abfd
)[indx
] != NULL
)
5023 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5026 idx
= asym_ptr
->udata
.i
;
5030 /* This case can occur when using --strip-symbol on a symbol
5031 which is used in a relocation entry. */
5032 (*_bfd_error_handler
)
5033 (_("%B: symbol `%s' required but not present"),
5034 abfd
, bfd_asymbol_name (asym_ptr
));
5035 bfd_set_error (bfd_error_no_symbols
);
5042 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5043 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
5044 elf_symbol_flags (flags
));
5052 /* Rewrite program header information. */
5055 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5057 Elf_Internal_Ehdr
*iehdr
;
5058 struct elf_segment_map
*map
;
5059 struct elf_segment_map
*map_first
;
5060 struct elf_segment_map
**pointer_to_map
;
5061 Elf_Internal_Phdr
*segment
;
5064 unsigned int num_segments
;
5065 bfd_boolean phdr_included
= FALSE
;
5066 bfd_vma maxpagesize
;
5067 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5068 unsigned int phdr_adjust_num
= 0;
5069 const struct elf_backend_data
*bed
;
5071 bed
= get_elf_backend_data (ibfd
);
5072 iehdr
= elf_elfheader (ibfd
);
5075 pointer_to_map
= &map_first
;
5077 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5078 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5080 /* Returns the end address of the segment + 1. */
5081 #define SEGMENT_END(segment, start) \
5082 (start + (segment->p_memsz > segment->p_filesz \
5083 ? segment->p_memsz : segment->p_filesz))
5085 #define SECTION_SIZE(section, segment) \
5086 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5087 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5088 ? section->size : 0)
5090 /* Returns TRUE if the given section is contained within
5091 the given segment. VMA addresses are compared. */
5092 #define IS_CONTAINED_BY_VMA(section, segment) \
5093 (section->vma >= segment->p_vaddr \
5094 && (section->vma + SECTION_SIZE (section, segment) \
5095 <= (SEGMENT_END (segment, segment->p_vaddr))))
5097 /* Returns TRUE if the given section is contained within
5098 the given segment. LMA addresses are compared. */
5099 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5100 (section->lma >= base \
5101 && (section->lma + SECTION_SIZE (section, segment) \
5102 <= SEGMENT_END (segment, base)))
5104 /* Handle PT_NOTE segment. */
5105 #define IS_NOTE(p, s) \
5106 (p->p_type == PT_NOTE \
5107 && elf_section_type (s) == SHT_NOTE \
5108 && (bfd_vma) s->filepos >= p->p_offset \
5109 && ((bfd_vma) s->filepos + s->size \
5110 <= p->p_offset + p->p_filesz))
5112 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5114 #define IS_COREFILE_NOTE(p, s) \
5116 && bfd_get_format (ibfd) == bfd_core \
5120 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5121 linker, which generates a PT_INTERP section with p_vaddr and
5122 p_memsz set to 0. */
5123 #define IS_SOLARIS_PT_INTERP(p, s) \
5125 && p->p_paddr == 0 \
5126 && p->p_memsz == 0 \
5127 && p->p_filesz > 0 \
5128 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5130 && (bfd_vma) s->filepos >= p->p_offset \
5131 && ((bfd_vma) s->filepos + s->size \
5132 <= p->p_offset + p->p_filesz))
5134 /* Decide if the given section should be included in the given segment.
5135 A section will be included if:
5136 1. It is within the address space of the segment -- we use the LMA
5137 if that is set for the segment and the VMA otherwise,
5138 2. It is an allocated section or a NOTE section in a PT_NOTE
5140 3. There is an output section associated with it,
5141 4. The section has not already been allocated to a previous segment.
5142 5. PT_GNU_STACK segments do not include any sections.
5143 6. PT_TLS segment includes only SHF_TLS sections.
5144 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5145 8. PT_DYNAMIC should not contain empty sections at the beginning
5146 (with the possible exception of .dynamic). */
5147 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5148 ((((segment->p_paddr \
5149 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5150 : IS_CONTAINED_BY_VMA (section, segment)) \
5151 && (section->flags & SEC_ALLOC) != 0) \
5152 || IS_NOTE (segment, section)) \
5153 && segment->p_type != PT_GNU_STACK \
5154 && (segment->p_type != PT_TLS \
5155 || (section->flags & SEC_THREAD_LOCAL)) \
5156 && (segment->p_type == PT_LOAD \
5157 || segment->p_type == PT_TLS \
5158 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5159 && (segment->p_type != PT_DYNAMIC \
5160 || SECTION_SIZE (section, segment) > 0 \
5161 || (segment->p_paddr \
5162 ? segment->p_paddr != section->lma \
5163 : segment->p_vaddr != section->vma) \
5164 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5166 && !section->segment_mark)
5168 /* If the output section of a section in the input segment is NULL,
5169 it is removed from the corresponding output segment. */
5170 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5171 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5172 && section->output_section != NULL)
5174 /* Returns TRUE iff seg1 starts after the end of seg2. */
5175 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5176 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5178 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5179 their VMA address ranges and their LMA address ranges overlap.
5180 It is possible to have overlapping VMA ranges without overlapping LMA
5181 ranges. RedBoot images for example can have both .data and .bss mapped
5182 to the same VMA range, but with the .data section mapped to a different
5184 #define SEGMENT_OVERLAPS(seg1, seg2) \
5185 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5186 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5187 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5188 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5190 /* Initialise the segment mark field. */
5191 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5192 section
->segment_mark
= FALSE
;
5194 /* Scan through the segments specified in the program header
5195 of the input BFD. For this first scan we look for overlaps
5196 in the loadable segments. These can be created by weird
5197 parameters to objcopy. Also, fix some solaris weirdness. */
5198 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5203 Elf_Internal_Phdr
*segment2
;
5205 if (segment
->p_type
== PT_INTERP
)
5206 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5207 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5209 /* Mininal change so that the normal section to segment
5210 assignment code will work. */
5211 segment
->p_vaddr
= section
->vma
;
5215 if (segment
->p_type
!= PT_LOAD
)
5217 /* Remove PT_GNU_RELRO segment. */
5218 if (segment
->p_type
== PT_GNU_RELRO
)
5219 segment
->p_type
= PT_NULL
;
5223 /* Determine if this segment overlaps any previous segments. */
5224 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5226 bfd_signed_vma extra_length
;
5228 if (segment2
->p_type
!= PT_LOAD
5229 || !SEGMENT_OVERLAPS (segment
, segment2
))
5232 /* Merge the two segments together. */
5233 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5235 /* Extend SEGMENT2 to include SEGMENT and then delete
5237 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5238 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5240 if (extra_length
> 0)
5242 segment2
->p_memsz
+= extra_length
;
5243 segment2
->p_filesz
+= extra_length
;
5246 segment
->p_type
= PT_NULL
;
5248 /* Since we have deleted P we must restart the outer loop. */
5250 segment
= elf_tdata (ibfd
)->phdr
;
5255 /* Extend SEGMENT to include SEGMENT2 and then delete
5257 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5258 - SEGMENT_END (segment
, segment
->p_vaddr
));
5260 if (extra_length
> 0)
5262 segment
->p_memsz
+= extra_length
;
5263 segment
->p_filesz
+= extra_length
;
5266 segment2
->p_type
= PT_NULL
;
5271 /* The second scan attempts to assign sections to segments. */
5272 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5276 unsigned int section_count
;
5277 asection
**sections
;
5278 asection
*output_section
;
5280 bfd_vma matching_lma
;
5281 bfd_vma suggested_lma
;
5284 asection
*first_section
;
5285 bfd_boolean first_matching_lma
;
5286 bfd_boolean first_suggested_lma
;
5288 if (segment
->p_type
== PT_NULL
)
5291 first_section
= NULL
;
5292 /* Compute how many sections might be placed into this segment. */
5293 for (section
= ibfd
->sections
, section_count
= 0;
5295 section
= section
->next
)
5297 /* Find the first section in the input segment, which may be
5298 removed from the corresponding output segment. */
5299 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5301 if (first_section
== NULL
)
5302 first_section
= section
;
5303 if (section
->output_section
!= NULL
)
5308 /* Allocate a segment map big enough to contain
5309 all of the sections we have selected. */
5310 amt
= sizeof (struct elf_segment_map
);
5311 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5312 map
= bfd_zalloc (obfd
, amt
);
5316 /* Initialise the fields of the segment map. Default to
5317 using the physical address of the segment in the input BFD. */
5319 map
->p_type
= segment
->p_type
;
5320 map
->p_flags
= segment
->p_flags
;
5321 map
->p_flags_valid
= 1;
5323 /* If the first section in the input segment is removed, there is
5324 no need to preserve segment physical address in the corresponding
5326 if (!first_section
|| first_section
->output_section
!= NULL
)
5328 map
->p_paddr
= segment
->p_paddr
;
5329 map
->p_paddr_valid
= 1;
5332 /* Determine if this segment contains the ELF file header
5333 and if it contains the program headers themselves. */
5334 map
->includes_filehdr
= (segment
->p_offset
== 0
5335 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5336 map
->includes_phdrs
= 0;
5338 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5340 map
->includes_phdrs
=
5341 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5342 && (segment
->p_offset
+ segment
->p_filesz
5343 >= ((bfd_vma
) iehdr
->e_phoff
5344 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5346 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5347 phdr_included
= TRUE
;
5350 if (section_count
== 0)
5352 /* Special segments, such as the PT_PHDR segment, may contain
5353 no sections, but ordinary, loadable segments should contain
5354 something. They are allowed by the ELF spec however, so only
5355 a warning is produced. */
5356 if (segment
->p_type
== PT_LOAD
)
5357 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5358 " detected, is this intentional ?\n"),
5362 *pointer_to_map
= map
;
5363 pointer_to_map
= &map
->next
;
5368 /* Now scan the sections in the input BFD again and attempt
5369 to add their corresponding output sections to the segment map.
5370 The problem here is how to handle an output section which has
5371 been moved (ie had its LMA changed). There are four possibilities:
5373 1. None of the sections have been moved.
5374 In this case we can continue to use the segment LMA from the
5377 2. All of the sections have been moved by the same amount.
5378 In this case we can change the segment's LMA to match the LMA
5379 of the first section.
5381 3. Some of the sections have been moved, others have not.
5382 In this case those sections which have not been moved can be
5383 placed in the current segment which will have to have its size,
5384 and possibly its LMA changed, and a new segment or segments will
5385 have to be created to contain the other sections.
5387 4. The sections have been moved, but not by the same amount.
5388 In this case we can change the segment's LMA to match the LMA
5389 of the first section and we will have to create a new segment
5390 or segments to contain the other sections.
5392 In order to save time, we allocate an array to hold the section
5393 pointers that we are interested in. As these sections get assigned
5394 to a segment, they are removed from this array. */
5396 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5397 to work around this long long bug. */
5398 sections
= bfd_malloc2 (section_count
, sizeof (asection
*));
5399 if (sections
== NULL
)
5402 /* Step One: Scan for segment vs section LMA conflicts.
5403 Also add the sections to the section array allocated above.
5404 Also add the sections to the current segment. In the common
5405 case, where the sections have not been moved, this means that
5406 we have completely filled the segment, and there is nothing
5411 first_matching_lma
= TRUE
;
5412 first_suggested_lma
= TRUE
;
5414 for (section
= ibfd
->sections
;
5416 section
= section
->next
)
5417 if (section
== first_section
)
5420 for (j
= 0; section
!= NULL
; section
= section
->next
)
5422 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5424 output_section
= section
->output_section
;
5426 sections
[j
++] = section
;
5428 /* The Solaris native linker always sets p_paddr to 0.
5429 We try to catch that case here, and set it to the
5430 correct value. Note - some backends require that
5431 p_paddr be left as zero. */
5432 if (segment
->p_paddr
== 0
5433 && segment
->p_vaddr
!= 0
5434 && !bed
->want_p_paddr_set_to_zero
5436 && output_section
->lma
!= 0
5437 && output_section
->vma
== (segment
->p_vaddr
5438 + (map
->includes_filehdr
5441 + (map
->includes_phdrs
5443 * iehdr
->e_phentsize
)
5445 map
->p_paddr
= segment
->p_vaddr
;
5447 /* Match up the physical address of the segment with the
5448 LMA address of the output section. */
5449 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5450 || IS_COREFILE_NOTE (segment
, section
)
5451 || (bed
->want_p_paddr_set_to_zero
5452 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
5454 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
5456 matching_lma
= output_section
->lma
;
5457 first_matching_lma
= FALSE
;
5460 /* We assume that if the section fits within the segment
5461 then it does not overlap any other section within that
5463 map
->sections
[isec
++] = output_section
;
5465 else if (first_suggested_lma
)
5467 suggested_lma
= output_section
->lma
;
5468 first_suggested_lma
= FALSE
;
5471 if (j
== section_count
)
5476 BFD_ASSERT (j
== section_count
);
5478 /* Step Two: Adjust the physical address of the current segment,
5480 if (isec
== section_count
)
5482 /* All of the sections fitted within the segment as currently
5483 specified. This is the default case. Add the segment to
5484 the list of built segments and carry on to process the next
5485 program header in the input BFD. */
5486 map
->count
= section_count
;
5487 *pointer_to_map
= map
;
5488 pointer_to_map
= &map
->next
;
5490 if (!bed
->want_p_paddr_set_to_zero
5491 && matching_lma
!= map
->p_paddr
5492 && !map
->includes_filehdr
&& !map
->includes_phdrs
)
5493 /* There is some padding before the first section in the
5494 segment. So, we must account for that in the output
5496 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
5503 if (!first_matching_lma
)
5505 /* At least one section fits inside the current segment.
5506 Keep it, but modify its physical address to match the
5507 LMA of the first section that fitted. */
5508 map
->p_paddr
= matching_lma
;
5512 /* None of the sections fitted inside the current segment.
5513 Change the current segment's physical address to match
5514 the LMA of the first section. */
5515 map
->p_paddr
= suggested_lma
;
5518 /* Offset the segment physical address from the lma
5519 to allow for space taken up by elf headers. */
5520 if (map
->includes_filehdr
)
5521 map
->p_paddr
-= iehdr
->e_ehsize
;
5523 if (map
->includes_phdrs
)
5525 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5527 /* iehdr->e_phnum is just an estimate of the number
5528 of program headers that we will need. Make a note
5529 here of the number we used and the segment we chose
5530 to hold these headers, so that we can adjust the
5531 offset when we know the correct value. */
5532 phdr_adjust_num
= iehdr
->e_phnum
;
5533 phdr_adjust_seg
= map
;
5537 /* Step Three: Loop over the sections again, this time assigning
5538 those that fit to the current segment and removing them from the
5539 sections array; but making sure not to leave large gaps. Once all
5540 possible sections have been assigned to the current segment it is
5541 added to the list of built segments and if sections still remain
5542 to be assigned, a new segment is constructed before repeating
5549 first_suggested_lma
= TRUE
;
5551 /* Fill the current segment with sections that fit. */
5552 for (j
= 0; j
< section_count
; j
++)
5554 section
= sections
[j
];
5556 if (section
== NULL
)
5559 output_section
= section
->output_section
;
5561 BFD_ASSERT (output_section
!= NULL
);
5563 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5564 || IS_COREFILE_NOTE (segment
, section
))
5566 if (map
->count
== 0)
5568 /* If the first section in a segment does not start at
5569 the beginning of the segment, then something is
5571 if (output_section
->lma
5573 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5574 + (map
->includes_phdrs
5575 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5583 prev_sec
= map
->sections
[map
->count
- 1];
5585 /* If the gap between the end of the previous section
5586 and the start of this section is more than
5587 maxpagesize then we need to start a new segment. */
5588 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
5590 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5591 || (prev_sec
->lma
+ prev_sec
->size
5592 > output_section
->lma
))
5594 if (first_suggested_lma
)
5596 suggested_lma
= output_section
->lma
;
5597 first_suggested_lma
= FALSE
;
5604 map
->sections
[map
->count
++] = output_section
;
5607 section
->segment_mark
= TRUE
;
5609 else if (first_suggested_lma
)
5611 suggested_lma
= output_section
->lma
;
5612 first_suggested_lma
= FALSE
;
5616 BFD_ASSERT (map
->count
> 0);
5618 /* Add the current segment to the list of built segments. */
5619 *pointer_to_map
= map
;
5620 pointer_to_map
= &map
->next
;
5622 if (isec
< section_count
)
5624 /* We still have not allocated all of the sections to
5625 segments. Create a new segment here, initialise it
5626 and carry on looping. */
5627 amt
= sizeof (struct elf_segment_map
);
5628 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5629 map
= bfd_alloc (obfd
, amt
);
5636 /* Initialise the fields of the segment map. Set the physical
5637 physical address to the LMA of the first section that has
5638 not yet been assigned. */
5640 map
->p_type
= segment
->p_type
;
5641 map
->p_flags
= segment
->p_flags
;
5642 map
->p_flags_valid
= 1;
5643 map
->p_paddr
= suggested_lma
;
5644 map
->p_paddr_valid
= 1;
5645 map
->includes_filehdr
= 0;
5646 map
->includes_phdrs
= 0;
5649 while (isec
< section_count
);
5654 /* The Solaris linker creates program headers in which all the
5655 p_paddr fields are zero. When we try to objcopy or strip such a
5656 file, we get confused. Check for this case, and if we find it
5657 reset the p_paddr_valid fields. */
5658 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5659 if (map
->p_paddr
!= 0)
5662 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5663 map
->p_paddr_valid
= 0;
5665 elf_tdata (obfd
)->segment_map
= map_first
;
5667 /* If we had to estimate the number of program headers that were
5668 going to be needed, then check our estimate now and adjust
5669 the offset if necessary. */
5670 if (phdr_adjust_seg
!= NULL
)
5674 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5677 if (count
> phdr_adjust_num
)
5678 phdr_adjust_seg
->p_paddr
5679 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5684 #undef IS_CONTAINED_BY_VMA
5685 #undef IS_CONTAINED_BY_LMA
5687 #undef IS_COREFILE_NOTE
5688 #undef IS_SOLARIS_PT_INTERP
5689 #undef IS_SECTION_IN_INPUT_SEGMENT
5690 #undef INCLUDE_SECTION_IN_SEGMENT
5691 #undef SEGMENT_AFTER_SEGMENT
5692 #undef SEGMENT_OVERLAPS
5696 /* Copy ELF program header information. */
5699 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5701 Elf_Internal_Ehdr
*iehdr
;
5702 struct elf_segment_map
*map
;
5703 struct elf_segment_map
*map_first
;
5704 struct elf_segment_map
**pointer_to_map
;
5705 Elf_Internal_Phdr
*segment
;
5707 unsigned int num_segments
;
5708 bfd_boolean phdr_included
= FALSE
;
5710 iehdr
= elf_elfheader (ibfd
);
5713 pointer_to_map
= &map_first
;
5715 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5716 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5721 unsigned int section_count
;
5723 Elf_Internal_Shdr
*this_hdr
;
5724 asection
*first_section
= NULL
;
5725 asection
*lowest_section
= NULL
;
5727 /* Compute how many sections are in this segment. */
5728 for (section
= ibfd
->sections
, section_count
= 0;
5730 section
= section
->next
)
5732 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5733 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5736 first_section
= lowest_section
= section
;
5737 if (section
->lma
< lowest_section
->lma
)
5738 lowest_section
= section
;
5743 /* Allocate a segment map big enough to contain
5744 all of the sections we have selected. */
5745 amt
= sizeof (struct elf_segment_map
);
5746 if (section_count
!= 0)
5747 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5748 map
= bfd_zalloc (obfd
, amt
);
5752 /* Initialize the fields of the output segment map with the
5755 map
->p_type
= segment
->p_type
;
5756 map
->p_flags
= segment
->p_flags
;
5757 map
->p_flags_valid
= 1;
5758 map
->p_paddr
= segment
->p_paddr
;
5759 map
->p_paddr_valid
= 1;
5760 map
->p_align
= segment
->p_align
;
5761 map
->p_align_valid
= 1;
5762 map
->p_vaddr_offset
= 0;
5764 if (map
->p_type
== PT_GNU_RELRO
5765 && segment
->p_filesz
== segment
->p_memsz
)
5767 /* The PT_GNU_RELRO segment may contain the first a few
5768 bytes in the .got.plt section even if the whole .got.plt
5769 section isn't in the PT_GNU_RELRO segment. We won't
5770 change the size of the PT_GNU_RELRO segment. */
5771 map
->p_size
= segment
->p_filesz
;
5772 map
->p_size_valid
= 1;
5775 /* Determine if this segment contains the ELF file header
5776 and if it contains the program headers themselves. */
5777 map
->includes_filehdr
= (segment
->p_offset
== 0
5778 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5780 map
->includes_phdrs
= 0;
5781 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
5783 map
->includes_phdrs
=
5784 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5785 && (segment
->p_offset
+ segment
->p_filesz
5786 >= ((bfd_vma
) iehdr
->e_phoff
5787 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5789 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5790 phdr_included
= TRUE
;
5793 if (!map
->includes_phdrs
&& !map
->includes_filehdr
)
5794 /* There is some other padding before the first section. */
5795 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
5796 - segment
->p_paddr
);
5798 if (section_count
!= 0)
5800 unsigned int isec
= 0;
5802 for (section
= first_section
;
5804 section
= section
->next
)
5806 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5807 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5809 map
->sections
[isec
++] = section
->output_section
;
5810 if (isec
== section_count
)
5816 map
->count
= section_count
;
5817 *pointer_to_map
= map
;
5818 pointer_to_map
= &map
->next
;
5821 elf_tdata (obfd
)->segment_map
= map_first
;
5825 /* Copy private BFD data. This copies or rewrites ELF program header
5829 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
5831 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5832 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5835 if (elf_tdata (ibfd
)->phdr
== NULL
)
5838 if (ibfd
->xvec
== obfd
->xvec
)
5840 /* Check to see if any sections in the input BFD
5841 covered by ELF program header have changed. */
5842 Elf_Internal_Phdr
*segment
;
5843 asection
*section
, *osec
;
5844 unsigned int i
, num_segments
;
5845 Elf_Internal_Shdr
*this_hdr
;
5846 const struct elf_backend_data
*bed
;
5848 bed
= get_elf_backend_data (ibfd
);
5850 /* Regenerate the segment map if p_paddr is set to 0. */
5851 if (bed
->want_p_paddr_set_to_zero
)
5854 /* Initialize the segment mark field. */
5855 for (section
= obfd
->sections
; section
!= NULL
;
5856 section
= section
->next
)
5857 section
->segment_mark
= FALSE
;
5859 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5860 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5864 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5865 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5866 which severly confuses things, so always regenerate the segment
5867 map in this case. */
5868 if (segment
->p_paddr
== 0
5869 && segment
->p_memsz
== 0
5870 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
5873 for (section
= ibfd
->sections
;
5874 section
!= NULL
; section
= section
->next
)
5876 /* We mark the output section so that we know it comes
5877 from the input BFD. */
5878 osec
= section
->output_section
;
5880 osec
->segment_mark
= TRUE
;
5882 /* Check if this section is covered by the segment. */
5883 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5884 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5886 /* FIXME: Check if its output section is changed or
5887 removed. What else do we need to check? */
5889 || section
->flags
!= osec
->flags
5890 || section
->lma
!= osec
->lma
5891 || section
->vma
!= osec
->vma
5892 || section
->size
!= osec
->size
5893 || section
->rawsize
!= osec
->rawsize
5894 || section
->alignment_power
!= osec
->alignment_power
)
5900 /* Check to see if any output section do not come from the
5902 for (section
= obfd
->sections
; section
!= NULL
;
5903 section
= section
->next
)
5905 if (section
->segment_mark
== FALSE
)
5908 section
->segment_mark
= FALSE
;
5911 return copy_elf_program_header (ibfd
, obfd
);
5915 return rewrite_elf_program_header (ibfd
, obfd
);
5918 /* Initialize private output section information from input section. */
5921 _bfd_elf_init_private_section_data (bfd
*ibfd
,
5925 struct bfd_link_info
*link_info
)
5928 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5929 bfd_boolean need_group
= link_info
== NULL
|| link_info
->relocatable
;
5931 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5932 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5935 /* Don't copy the output ELF section type from input if the
5936 output BFD section flags have been set to something different.
5937 elf_fake_sections will set ELF section type based on BFD
5939 if (elf_section_type (osec
) == SHT_NULL
5940 && (osec
->flags
== isec
->flags
|| !osec
->flags
))
5941 elf_section_type (osec
) = elf_section_type (isec
);
5943 /* FIXME: Is this correct for all OS/PROC specific flags? */
5944 elf_section_flags (osec
) |= (elf_section_flags (isec
)
5945 & (SHF_MASKOS
| SHF_MASKPROC
));
5947 /* Set things up for objcopy and relocatable link. The output
5948 SHT_GROUP section will have its elf_next_in_group pointing back
5949 to the input group members. Ignore linker created group section.
5950 See elfNN_ia64_object_p in elfxx-ia64.c. */
5953 if (elf_sec_group (isec
) == NULL
5954 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
5956 if (elf_section_flags (isec
) & SHF_GROUP
)
5957 elf_section_flags (osec
) |= SHF_GROUP
;
5958 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5959 elf_group_name (osec
) = elf_group_name (isec
);
5963 ihdr
= &elf_section_data (isec
)->this_hdr
;
5965 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
5966 don't use the output section of the linked-to section since it
5967 may be NULL at this point. */
5968 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
5970 ohdr
= &elf_section_data (osec
)->this_hdr
;
5971 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
5972 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
5975 osec
->use_rela_p
= isec
->use_rela_p
;
5980 /* Copy private section information. This copies over the entsize
5981 field, and sometimes the info field. */
5984 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
5989 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5991 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5992 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5995 ihdr
= &elf_section_data (isec
)->this_hdr
;
5996 ohdr
= &elf_section_data (osec
)->this_hdr
;
5998 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6000 if (ihdr
->sh_type
== SHT_SYMTAB
6001 || ihdr
->sh_type
== SHT_DYNSYM
6002 || ihdr
->sh_type
== SHT_GNU_verneed
6003 || ihdr
->sh_type
== SHT_GNU_verdef
)
6004 ohdr
->sh_info
= ihdr
->sh_info
;
6006 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6010 /* Copy private header information. */
6013 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6017 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6018 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6021 /* Copy over private BFD data if it has not already been copied.
6022 This must be done here, rather than in the copy_private_bfd_data
6023 entry point, because the latter is called after the section
6024 contents have been set, which means that the program headers have
6025 already been worked out. */
6026 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6028 if (! copy_private_bfd_data (ibfd
, obfd
))
6032 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6033 but this might be wrong if we deleted the group section. */
6034 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6035 if (elf_section_type (isec
) == SHT_GROUP
6036 && isec
->output_section
== NULL
)
6038 asection
*first
= elf_next_in_group (isec
);
6039 asection
*s
= first
;
6042 if (s
->output_section
!= NULL
)
6044 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6045 elf_group_name (s
->output_section
) = NULL
;
6047 s
= elf_next_in_group (s
);
6056 /* Copy private symbol information. If this symbol is in a section
6057 which we did not map into a BFD section, try to map the section
6058 index correctly. We use special macro definitions for the mapped
6059 section indices; these definitions are interpreted by the
6060 swap_out_syms function. */
6062 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6063 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6064 #define MAP_STRTAB (SHN_HIOS + 3)
6065 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6066 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6069 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6074 elf_symbol_type
*isym
, *osym
;
6076 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6077 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6080 isym
= elf_symbol_from (ibfd
, isymarg
);
6081 osym
= elf_symbol_from (obfd
, osymarg
);
6084 && isym
->internal_elf_sym
.st_shndx
!= 0
6086 && bfd_is_abs_section (isym
->symbol
.section
))
6090 shndx
= isym
->internal_elf_sym
.st_shndx
;
6091 if (shndx
== elf_onesymtab (ibfd
))
6092 shndx
= MAP_ONESYMTAB
;
6093 else if (shndx
== elf_dynsymtab (ibfd
))
6094 shndx
= MAP_DYNSYMTAB
;
6095 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
6097 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
6098 shndx
= MAP_SHSTRTAB
;
6099 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
6100 shndx
= MAP_SYM_SHNDX
;
6101 osym
->internal_elf_sym
.st_shndx
= shndx
;
6107 /* Swap out the symbols. */
6110 swap_out_syms (bfd
*abfd
,
6111 struct bfd_strtab_hash
**sttp
,
6114 const struct elf_backend_data
*bed
;
6117 struct bfd_strtab_hash
*stt
;
6118 Elf_Internal_Shdr
*symtab_hdr
;
6119 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6120 Elf_Internal_Shdr
*symstrtab_hdr
;
6121 bfd_byte
*outbound_syms
;
6122 bfd_byte
*outbound_shndx
;
6125 bfd_boolean name_local_sections
;
6127 if (!elf_map_symbols (abfd
))
6130 /* Dump out the symtabs. */
6131 stt
= _bfd_elf_stringtab_init ();
6135 bed
= get_elf_backend_data (abfd
);
6136 symcount
= bfd_get_symcount (abfd
);
6137 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6138 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6139 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6140 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6141 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
6142 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6144 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6145 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6147 outbound_syms
= bfd_alloc2 (abfd
, 1 + symcount
, bed
->s
->sizeof_sym
);
6148 if (outbound_syms
== NULL
)
6150 _bfd_stringtab_free (stt
);
6153 symtab_hdr
->contents
= outbound_syms
;
6155 outbound_shndx
= NULL
;
6156 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6157 if (symtab_shndx_hdr
->sh_name
!= 0)
6159 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6160 outbound_shndx
= bfd_zalloc2 (abfd
, 1 + symcount
,
6161 sizeof (Elf_External_Sym_Shndx
));
6162 if (outbound_shndx
== NULL
)
6164 _bfd_stringtab_free (stt
);
6168 symtab_shndx_hdr
->contents
= outbound_shndx
;
6169 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6170 symtab_shndx_hdr
->sh_size
= amt
;
6171 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6172 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6175 /* Now generate the data (for "contents"). */
6177 /* Fill in zeroth symbol and swap it out. */
6178 Elf_Internal_Sym sym
;
6184 sym
.st_shndx
= SHN_UNDEF
;
6185 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6186 outbound_syms
+= bed
->s
->sizeof_sym
;
6187 if (outbound_shndx
!= NULL
)
6188 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6192 = (bed
->elf_backend_name_local_section_symbols
6193 && bed
->elf_backend_name_local_section_symbols (abfd
));
6195 syms
= bfd_get_outsymbols (abfd
);
6196 for (idx
= 0; idx
< symcount
; idx
++)
6198 Elf_Internal_Sym sym
;
6199 bfd_vma value
= syms
[idx
]->value
;
6200 elf_symbol_type
*type_ptr
;
6201 flagword flags
= syms
[idx
]->flags
;
6204 if (!name_local_sections
6205 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6207 /* Local section symbols have no name. */
6212 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6215 if (sym
.st_name
== (unsigned long) -1)
6217 _bfd_stringtab_free (stt
);
6222 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6224 if ((flags
& BSF_SECTION_SYM
) == 0
6225 && bfd_is_com_section (syms
[idx
]->section
))
6227 /* ELF common symbols put the alignment into the `value' field,
6228 and the size into the `size' field. This is backwards from
6229 how BFD handles it, so reverse it here. */
6230 sym
.st_size
= value
;
6231 if (type_ptr
== NULL
6232 || type_ptr
->internal_elf_sym
.st_value
== 0)
6233 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6235 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6236 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6237 (abfd
, syms
[idx
]->section
);
6241 asection
*sec
= syms
[idx
]->section
;
6244 if (sec
->output_section
)
6246 value
+= sec
->output_offset
;
6247 sec
= sec
->output_section
;
6250 /* Don't add in the section vma for relocatable output. */
6251 if (! relocatable_p
)
6253 sym
.st_value
= value
;
6254 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6256 if (bfd_is_abs_section (sec
)
6258 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6260 /* This symbol is in a real ELF section which we did
6261 not create as a BFD section. Undo the mapping done
6262 by copy_private_symbol_data. */
6263 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6267 shndx
= elf_onesymtab (abfd
);
6270 shndx
= elf_dynsymtab (abfd
);
6273 shndx
= elf_tdata (abfd
)->strtab_section
;
6276 shndx
= elf_tdata (abfd
)->shstrtab_section
;
6279 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
6287 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6289 if (shndx
== SHN_BAD
)
6293 /* Writing this would be a hell of a lot easier if
6294 we had some decent documentation on bfd, and
6295 knew what to expect of the library, and what to
6296 demand of applications. For example, it
6297 appears that `objcopy' might not set the
6298 section of a symbol to be a section that is
6299 actually in the output file. */
6300 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6303 _bfd_error_handler (_("\
6304 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6305 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6307 bfd_set_error (bfd_error_invalid_operation
);
6308 _bfd_stringtab_free (stt
);
6312 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6313 BFD_ASSERT (shndx
!= SHN_BAD
);
6317 sym
.st_shndx
= shndx
;
6320 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6322 else if ((flags
& BSF_FUNCTION
) != 0)
6324 else if ((flags
& BSF_OBJECT
) != 0)
6326 else if ((flags
& BSF_RELC
) != 0)
6328 else if ((flags
& BSF_SRELC
) != 0)
6333 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6336 /* Processor-specific types. */
6337 if (type_ptr
!= NULL
6338 && bed
->elf_backend_get_symbol_type
)
6339 type
= ((*bed
->elf_backend_get_symbol_type
)
6340 (&type_ptr
->internal_elf_sym
, type
));
6342 if (flags
& BSF_SECTION_SYM
)
6344 if (flags
& BSF_GLOBAL
)
6345 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
6347 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
6349 else if (bfd_is_com_section (syms
[idx
]->section
))
6351 #ifdef USE_STT_COMMON
6352 if (type
== STT_OBJECT
)
6353 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
6356 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
6359 else if (bfd_is_und_section (syms
[idx
]->section
))
6360 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
6364 else if (flags
& BSF_FILE
)
6365 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
6368 int bind
= STB_LOCAL
;
6370 if (flags
& BSF_LOCAL
)
6372 else if (flags
& BSF_WEAK
)
6374 else if (flags
& BSF_GLOBAL
)
6377 sym
.st_info
= ELF_ST_INFO (bind
, type
);
6380 if (type_ptr
!= NULL
)
6381 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
6385 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6386 outbound_syms
+= bed
->s
->sizeof_sym
;
6387 if (outbound_shndx
!= NULL
)
6388 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6392 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
6393 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6395 symstrtab_hdr
->sh_flags
= 0;
6396 symstrtab_hdr
->sh_addr
= 0;
6397 symstrtab_hdr
->sh_entsize
= 0;
6398 symstrtab_hdr
->sh_link
= 0;
6399 symstrtab_hdr
->sh_info
= 0;
6400 symstrtab_hdr
->sh_addralign
= 1;
6405 /* Return the number of bytes required to hold the symtab vector.
6407 Note that we base it on the count plus 1, since we will null terminate
6408 the vector allocated based on this size. However, the ELF symbol table
6409 always has a dummy entry as symbol #0, so it ends up even. */
6412 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
6416 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6418 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6419 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6421 symtab_size
-= sizeof (asymbol
*);
6427 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
6431 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
6433 if (elf_dynsymtab (abfd
) == 0)
6435 bfd_set_error (bfd_error_invalid_operation
);
6439 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6440 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6442 symtab_size
-= sizeof (asymbol
*);
6448 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
6451 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
6454 /* Canonicalize the relocs. */
6457 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
6464 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6466 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
6469 tblptr
= section
->relocation
;
6470 for (i
= 0; i
< section
->reloc_count
; i
++)
6471 *relptr
++ = tblptr
++;
6475 return section
->reloc_count
;
6479 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
6481 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6482 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
6485 bfd_get_symcount (abfd
) = symcount
;
6490 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
6491 asymbol
**allocation
)
6493 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6494 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
6497 bfd_get_dynamic_symcount (abfd
) = symcount
;
6501 /* Return the size required for the dynamic reloc entries. Any loadable
6502 section that was actually installed in the BFD, and has type SHT_REL
6503 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6504 dynamic reloc section. */
6507 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
6512 if (elf_dynsymtab (abfd
) == 0)
6514 bfd_set_error (bfd_error_invalid_operation
);
6518 ret
= sizeof (arelent
*);
6519 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6520 if ((s
->flags
& SEC_LOAD
) != 0
6521 && elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6522 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6523 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6524 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
6525 * sizeof (arelent
*));
6530 /* Canonicalize the dynamic relocation entries. Note that we return the
6531 dynamic relocations as a single block, although they are actually
6532 associated with particular sections; the interface, which was
6533 designed for SunOS style shared libraries, expects that there is only
6534 one set of dynamic relocs. Any loadable section that was actually
6535 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6536 dynamic symbol table, is considered to be a dynamic reloc section. */
6539 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
6543 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
6547 if (elf_dynsymtab (abfd
) == 0)
6549 bfd_set_error (bfd_error_invalid_operation
);
6553 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
6555 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6557 if ((s
->flags
& SEC_LOAD
) != 0
6558 && elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6559 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6560 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6565 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
6567 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
6569 for (i
= 0; i
< count
; i
++)
6580 /* Read in the version information. */
6583 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
6585 bfd_byte
*contents
= NULL
;
6586 unsigned int freeidx
= 0;
6588 if (elf_dynverref (abfd
) != 0)
6590 Elf_Internal_Shdr
*hdr
;
6591 Elf_External_Verneed
*everneed
;
6592 Elf_Internal_Verneed
*iverneed
;
6594 bfd_byte
*contents_end
;
6596 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
6598 elf_tdata (abfd
)->verref
= bfd_zalloc2 (abfd
, hdr
->sh_info
,
6599 sizeof (Elf_Internal_Verneed
));
6600 if (elf_tdata (abfd
)->verref
== NULL
)
6603 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
6605 contents
= bfd_malloc (hdr
->sh_size
);
6606 if (contents
== NULL
)
6608 error_return_verref
:
6609 elf_tdata (abfd
)->verref
= NULL
;
6610 elf_tdata (abfd
)->cverrefs
= 0;
6613 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6614 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6615 goto error_return_verref
;
6617 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
6618 goto error_return_verref
;
6620 BFD_ASSERT (sizeof (Elf_External_Verneed
)
6621 == sizeof (Elf_External_Vernaux
));
6622 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
6623 everneed
= (Elf_External_Verneed
*) contents
;
6624 iverneed
= elf_tdata (abfd
)->verref
;
6625 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
6627 Elf_External_Vernaux
*evernaux
;
6628 Elf_Internal_Vernaux
*ivernaux
;
6631 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
6633 iverneed
->vn_bfd
= abfd
;
6635 iverneed
->vn_filename
=
6636 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6638 if (iverneed
->vn_filename
== NULL
)
6639 goto error_return_verref
;
6641 if (iverneed
->vn_cnt
== 0)
6642 iverneed
->vn_auxptr
= NULL
;
6645 iverneed
->vn_auxptr
= bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
6646 sizeof (Elf_Internal_Vernaux
));
6647 if (iverneed
->vn_auxptr
== NULL
)
6648 goto error_return_verref
;
6651 if (iverneed
->vn_aux
6652 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6653 goto error_return_verref
;
6655 evernaux
= ((Elf_External_Vernaux
*)
6656 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
6657 ivernaux
= iverneed
->vn_auxptr
;
6658 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
6660 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
6662 ivernaux
->vna_nodename
=
6663 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6664 ivernaux
->vna_name
);
6665 if (ivernaux
->vna_nodename
== NULL
)
6666 goto error_return_verref
;
6668 if (j
+ 1 < iverneed
->vn_cnt
)
6669 ivernaux
->vna_nextptr
= ivernaux
+ 1;
6671 ivernaux
->vna_nextptr
= NULL
;
6673 if (ivernaux
->vna_next
6674 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
6675 goto error_return_verref
;
6677 evernaux
= ((Elf_External_Vernaux
*)
6678 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
6680 if (ivernaux
->vna_other
> freeidx
)
6681 freeidx
= ivernaux
->vna_other
;
6684 if (i
+ 1 < hdr
->sh_info
)
6685 iverneed
->vn_nextref
= iverneed
+ 1;
6687 iverneed
->vn_nextref
= NULL
;
6689 if (iverneed
->vn_next
6690 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6691 goto error_return_verref
;
6693 everneed
= ((Elf_External_Verneed
*)
6694 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
6701 if (elf_dynverdef (abfd
) != 0)
6703 Elf_Internal_Shdr
*hdr
;
6704 Elf_External_Verdef
*everdef
;
6705 Elf_Internal_Verdef
*iverdef
;
6706 Elf_Internal_Verdef
*iverdefarr
;
6707 Elf_Internal_Verdef iverdefmem
;
6709 unsigned int maxidx
;
6710 bfd_byte
*contents_end_def
, *contents_end_aux
;
6712 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
6714 contents
= bfd_malloc (hdr
->sh_size
);
6715 if (contents
== NULL
)
6717 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6718 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6721 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
6724 BFD_ASSERT (sizeof (Elf_External_Verdef
)
6725 >= sizeof (Elf_External_Verdaux
));
6726 contents_end_def
= contents
+ hdr
->sh_size
6727 - sizeof (Elf_External_Verdef
);
6728 contents_end_aux
= contents
+ hdr
->sh_size
6729 - sizeof (Elf_External_Verdaux
);
6731 /* We know the number of entries in the section but not the maximum
6732 index. Therefore we have to run through all entries and find
6734 everdef
= (Elf_External_Verdef
*) contents
;
6736 for (i
= 0; i
< hdr
->sh_info
; ++i
)
6738 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6740 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
6741 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
6743 if (iverdefmem
.vd_next
6744 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
6747 everdef
= ((Elf_External_Verdef
*)
6748 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
6751 if (default_imported_symver
)
6753 if (freeidx
> maxidx
)
6758 elf_tdata (abfd
)->verdef
= bfd_zalloc2 (abfd
, maxidx
,
6759 sizeof (Elf_Internal_Verdef
));
6760 if (elf_tdata (abfd
)->verdef
== NULL
)
6763 elf_tdata (abfd
)->cverdefs
= maxidx
;
6765 everdef
= (Elf_External_Verdef
*) contents
;
6766 iverdefarr
= elf_tdata (abfd
)->verdef
;
6767 for (i
= 0; i
< hdr
->sh_info
; i
++)
6769 Elf_External_Verdaux
*everdaux
;
6770 Elf_Internal_Verdaux
*iverdaux
;
6773 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6775 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
6777 error_return_verdef
:
6778 elf_tdata (abfd
)->verdef
= NULL
;
6779 elf_tdata (abfd
)->cverdefs
= 0;
6783 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
6784 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
6786 iverdef
->vd_bfd
= abfd
;
6788 if (iverdef
->vd_cnt
== 0)
6789 iverdef
->vd_auxptr
= NULL
;
6792 iverdef
->vd_auxptr
= bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
6793 sizeof (Elf_Internal_Verdaux
));
6794 if (iverdef
->vd_auxptr
== NULL
)
6795 goto error_return_verdef
;
6799 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
6800 goto error_return_verdef
;
6802 everdaux
= ((Elf_External_Verdaux
*)
6803 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
6804 iverdaux
= iverdef
->vd_auxptr
;
6805 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
6807 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
6809 iverdaux
->vda_nodename
=
6810 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6811 iverdaux
->vda_name
);
6812 if (iverdaux
->vda_nodename
== NULL
)
6813 goto error_return_verdef
;
6815 if (j
+ 1 < iverdef
->vd_cnt
)
6816 iverdaux
->vda_nextptr
= iverdaux
+ 1;
6818 iverdaux
->vda_nextptr
= NULL
;
6820 if (iverdaux
->vda_next
6821 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
6822 goto error_return_verdef
;
6824 everdaux
= ((Elf_External_Verdaux
*)
6825 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
6828 if (iverdef
->vd_cnt
)
6829 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
6831 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
6832 iverdef
->vd_nextdef
= iverdef
+ 1;
6834 iverdef
->vd_nextdef
= NULL
;
6836 everdef
= ((Elf_External_Verdef
*)
6837 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
6843 else if (default_imported_symver
)
6850 elf_tdata (abfd
)->verdef
= bfd_zalloc2 (abfd
, freeidx
,
6851 sizeof (Elf_Internal_Verdef
));
6852 if (elf_tdata (abfd
)->verdef
== NULL
)
6855 elf_tdata (abfd
)->cverdefs
= freeidx
;
6858 /* Create a default version based on the soname. */
6859 if (default_imported_symver
)
6861 Elf_Internal_Verdef
*iverdef
;
6862 Elf_Internal_Verdaux
*iverdaux
;
6864 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];;
6866 iverdef
->vd_version
= VER_DEF_CURRENT
;
6867 iverdef
->vd_flags
= 0;
6868 iverdef
->vd_ndx
= freeidx
;
6869 iverdef
->vd_cnt
= 1;
6871 iverdef
->vd_bfd
= abfd
;
6873 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
6874 if (iverdef
->vd_nodename
== NULL
)
6875 goto error_return_verdef
;
6876 iverdef
->vd_nextdef
= NULL
;
6877 iverdef
->vd_auxptr
= bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
6878 if (iverdef
->vd_auxptr
== NULL
)
6879 goto error_return_verdef
;
6881 iverdaux
= iverdef
->vd_auxptr
;
6882 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
6883 iverdaux
->vda_nextptr
= NULL
;
6889 if (contents
!= NULL
)
6895 _bfd_elf_make_empty_symbol (bfd
*abfd
)
6897 elf_symbol_type
*newsym
;
6898 bfd_size_type amt
= sizeof (elf_symbol_type
);
6900 newsym
= bfd_zalloc (abfd
, amt
);
6905 newsym
->symbol
.the_bfd
= abfd
;
6906 return &newsym
->symbol
;
6911 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
6915 bfd_symbol_info (symbol
, ret
);
6918 /* Return whether a symbol name implies a local symbol. Most targets
6919 use this function for the is_local_label_name entry point, but some
6923 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
6926 /* Normal local symbols start with ``.L''. */
6927 if (name
[0] == '.' && name
[1] == 'L')
6930 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6931 DWARF debugging symbols starting with ``..''. */
6932 if (name
[0] == '.' && name
[1] == '.')
6935 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6936 emitting DWARF debugging output. I suspect this is actually a
6937 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6938 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6939 underscore to be emitted on some ELF targets). For ease of use,
6940 we treat such symbols as local. */
6941 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
6948 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
6949 asymbol
*symbol ATTRIBUTE_UNUSED
)
6956 _bfd_elf_set_arch_mach (bfd
*abfd
,
6957 enum bfd_architecture arch
,
6958 unsigned long machine
)
6960 /* If this isn't the right architecture for this backend, and this
6961 isn't the generic backend, fail. */
6962 if (arch
!= get_elf_backend_data (abfd
)->arch
6963 && arch
!= bfd_arch_unknown
6964 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
6967 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
6970 /* Find the function to a particular section and offset,
6971 for error reporting. */
6974 elf_find_function (bfd
*abfd ATTRIBUTE_UNUSED
,
6978 const char **filename_ptr
,
6979 const char **functionname_ptr
)
6981 const char *filename
;
6982 asymbol
*func
, *file
;
6985 /* ??? Given multiple file symbols, it is impossible to reliably
6986 choose the right file name for global symbols. File symbols are
6987 local symbols, and thus all file symbols must sort before any
6988 global symbols. The ELF spec may be interpreted to say that a
6989 file symbol must sort before other local symbols, but currently
6990 ld -r doesn't do this. So, for ld -r output, it is possible to
6991 make a better choice of file name for local symbols by ignoring
6992 file symbols appearing after a given local symbol. */
6993 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
6999 state
= nothing_seen
;
7001 for (p
= symbols
; *p
!= NULL
; p
++)
7005 q
= (elf_symbol_type
*) *p
;
7007 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
7013 if (state
== symbol_seen
)
7014 state
= file_after_symbol_seen
;
7018 if (bfd_get_section (&q
->symbol
) == section
7019 && q
->symbol
.value
>= low_func
7020 && q
->symbol
.value
<= offset
)
7022 func
= (asymbol
*) q
;
7023 low_func
= q
->symbol
.value
;
7026 && (ELF_ST_BIND (q
->internal_elf_sym
.st_info
) == STB_LOCAL
7027 || state
!= file_after_symbol_seen
))
7028 filename
= bfd_asymbol_name (file
);
7032 if (state
== nothing_seen
)
7033 state
= symbol_seen
;
7040 *filename_ptr
= filename
;
7041 if (functionname_ptr
)
7042 *functionname_ptr
= bfd_asymbol_name (func
);
7047 /* Find the nearest line to a particular section and offset,
7048 for error reporting. */
7051 _bfd_elf_find_nearest_line (bfd
*abfd
,
7055 const char **filename_ptr
,
7056 const char **functionname_ptr
,
7057 unsigned int *line_ptr
)
7061 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
7062 filename_ptr
, functionname_ptr
,
7065 if (!*functionname_ptr
)
7066 elf_find_function (abfd
, section
, symbols
, offset
,
7067 *filename_ptr
? NULL
: filename_ptr
,
7073 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
7074 filename_ptr
, functionname_ptr
,
7076 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7078 if (!*functionname_ptr
)
7079 elf_find_function (abfd
, section
, symbols
, offset
,
7080 *filename_ptr
? NULL
: filename_ptr
,
7086 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7087 &found
, filename_ptr
,
7088 functionname_ptr
, line_ptr
,
7089 &elf_tdata (abfd
)->line_info
))
7091 if (found
&& (*functionname_ptr
|| *line_ptr
))
7094 if (symbols
== NULL
)
7097 if (! elf_find_function (abfd
, section
, symbols
, offset
,
7098 filename_ptr
, functionname_ptr
))
7105 /* Find the line for a symbol. */
7108 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7109 const char **filename_ptr
, unsigned int *line_ptr
)
7111 return _bfd_dwarf2_find_line (abfd
, symbols
, symbol
,
7112 filename_ptr
, line_ptr
, 0,
7113 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7116 /* After a call to bfd_find_nearest_line, successive calls to
7117 bfd_find_inliner_info can be used to get source information about
7118 each level of function inlining that terminated at the address
7119 passed to bfd_find_nearest_line. Currently this is only supported
7120 for DWARF2 with appropriate DWARF3 extensions. */
7123 _bfd_elf_find_inliner_info (bfd
*abfd
,
7124 const char **filename_ptr
,
7125 const char **functionname_ptr
,
7126 unsigned int *line_ptr
)
7129 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7130 functionname_ptr
, line_ptr
,
7131 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7136 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7138 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7139 int ret
= bed
->s
->sizeof_ehdr
;
7141 if (!info
->relocatable
)
7143 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
7145 if (phdr_size
== (bfd_size_type
) -1)
7147 struct elf_segment_map
*m
;
7150 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
7151 phdr_size
+= bed
->s
->sizeof_phdr
;
7154 phdr_size
= get_program_header_size (abfd
, info
);
7157 elf_tdata (abfd
)->program_header_size
= phdr_size
;
7165 _bfd_elf_set_section_contents (bfd
*abfd
,
7167 const void *location
,
7169 bfd_size_type count
)
7171 Elf_Internal_Shdr
*hdr
;
7174 if (! abfd
->output_has_begun
7175 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7178 hdr
= &elf_section_data (section
)->this_hdr
;
7179 pos
= hdr
->sh_offset
+ offset
;
7180 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7181 || bfd_bwrite (location
, count
, abfd
) != count
)
7188 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7189 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7190 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7195 /* Try to convert a non-ELF reloc into an ELF one. */
7198 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7200 /* Check whether we really have an ELF howto. */
7202 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7204 bfd_reloc_code_real_type code
;
7205 reloc_howto_type
*howto
;
7207 /* Alien reloc: Try to determine its type to replace it with an
7208 equivalent ELF reloc. */
7210 if (areloc
->howto
->pc_relative
)
7212 switch (areloc
->howto
->bitsize
)
7215 code
= BFD_RELOC_8_PCREL
;
7218 code
= BFD_RELOC_12_PCREL
;
7221 code
= BFD_RELOC_16_PCREL
;
7224 code
= BFD_RELOC_24_PCREL
;
7227 code
= BFD_RELOC_32_PCREL
;
7230 code
= BFD_RELOC_64_PCREL
;
7236 howto
= bfd_reloc_type_lookup (abfd
, code
);
7238 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7240 if (howto
->pcrel_offset
)
7241 areloc
->addend
+= areloc
->address
;
7243 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7248 switch (areloc
->howto
->bitsize
)
7254 code
= BFD_RELOC_14
;
7257 code
= BFD_RELOC_16
;
7260 code
= BFD_RELOC_26
;
7263 code
= BFD_RELOC_32
;
7266 code
= BFD_RELOC_64
;
7272 howto
= bfd_reloc_type_lookup (abfd
, code
);
7276 areloc
->howto
= howto
;
7284 (*_bfd_error_handler
)
7285 (_("%B: unsupported relocation type %s"),
7286 abfd
, areloc
->howto
->name
);
7287 bfd_set_error (bfd_error_bad_value
);
7292 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7294 if (bfd_get_format (abfd
) == bfd_object
)
7296 if (elf_tdata (abfd
) != NULL
&& elf_shstrtab (abfd
) != NULL
)
7297 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7298 _bfd_dwarf2_cleanup_debug_info (abfd
);
7301 return _bfd_generic_close_and_cleanup (abfd
);
7304 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7305 in the relocation's offset. Thus we cannot allow any sort of sanity
7306 range-checking to interfere. There is nothing else to do in processing
7309 bfd_reloc_status_type
7310 _bfd_elf_rel_vtable_reloc_fn
7311 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7312 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
7313 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
7314 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
7316 return bfd_reloc_ok
;
7319 /* Elf core file support. Much of this only works on native
7320 toolchains, since we rely on knowing the
7321 machine-dependent procfs structure in order to pick
7322 out details about the corefile. */
7324 #ifdef HAVE_SYS_PROCFS_H
7325 # include <sys/procfs.h>
7328 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7331 elfcore_make_pid (bfd
*abfd
)
7333 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
7334 + (elf_tdata (abfd
)->core_pid
));
7337 /* If there isn't a section called NAME, make one, using
7338 data from SECT. Note, this function will generate a
7339 reference to NAME, so you shouldn't deallocate or
7343 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
7347 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
7350 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
7354 sect2
->size
= sect
->size
;
7355 sect2
->filepos
= sect
->filepos
;
7356 sect2
->alignment_power
= sect
->alignment_power
;
7360 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7361 actually creates up to two pseudosections:
7362 - For the single-threaded case, a section named NAME, unless
7363 such a section already exists.
7364 - For the multi-threaded case, a section named "NAME/PID", where
7365 PID is elfcore_make_pid (abfd).
7366 Both pseudosections have identical contents. */
7368 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
7374 char *threaded_name
;
7378 /* Build the section name. */
7380 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
7381 len
= strlen (buf
) + 1;
7382 threaded_name
= bfd_alloc (abfd
, len
);
7383 if (threaded_name
== NULL
)
7385 memcpy (threaded_name
, buf
, len
);
7387 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
7392 sect
->filepos
= filepos
;
7393 sect
->alignment_power
= 2;
7395 return elfcore_maybe_make_sect (abfd
, name
, sect
);
7398 /* prstatus_t exists on:
7400 linux 2.[01] + glibc
7404 #if defined (HAVE_PRSTATUS_T)
7407 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7412 if (note
->descsz
== sizeof (prstatus_t
))
7416 size
= sizeof (prstat
.pr_reg
);
7417 offset
= offsetof (prstatus_t
, pr_reg
);
7418 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7420 /* Do not overwrite the core signal if it
7421 has already been set by another thread. */
7422 if (elf_tdata (abfd
)->core_signal
== 0)
7423 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7424 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7426 /* pr_who exists on:
7429 pr_who doesn't exist on:
7432 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7433 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7436 #if defined (HAVE_PRSTATUS32_T)
7437 else if (note
->descsz
== sizeof (prstatus32_t
))
7439 /* 64-bit host, 32-bit corefile */
7440 prstatus32_t prstat
;
7442 size
= sizeof (prstat
.pr_reg
);
7443 offset
= offsetof (prstatus32_t
, pr_reg
);
7444 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7446 /* Do not overwrite the core signal if it
7447 has already been set by another thread. */
7448 if (elf_tdata (abfd
)->core_signal
== 0)
7449 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7450 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7452 /* pr_who exists on:
7455 pr_who doesn't exist on:
7458 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7459 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7462 #endif /* HAVE_PRSTATUS32_T */
7465 /* Fail - we don't know how to handle any other
7466 note size (ie. data object type). */
7470 /* Make a ".reg/999" section and a ".reg" section. */
7471 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
7472 size
, note
->descpos
+ offset
);
7474 #endif /* defined (HAVE_PRSTATUS_T) */
7476 /* Create a pseudosection containing the exact contents of NOTE. */
7478 elfcore_make_note_pseudosection (bfd
*abfd
,
7480 Elf_Internal_Note
*note
)
7482 return _bfd_elfcore_make_pseudosection (abfd
, name
,
7483 note
->descsz
, note
->descpos
);
7486 /* There isn't a consistent prfpregset_t across platforms,
7487 but it doesn't matter, because we don't have to pick this
7488 data structure apart. */
7491 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7493 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7496 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7497 type of NT_PRXFPREG. Just include the whole note's contents
7501 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7503 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
7507 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
7509 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
7513 #if defined (HAVE_PRPSINFO_T)
7514 typedef prpsinfo_t elfcore_psinfo_t
;
7515 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7516 typedef prpsinfo32_t elfcore_psinfo32_t
;
7520 #if defined (HAVE_PSINFO_T)
7521 typedef psinfo_t elfcore_psinfo_t
;
7522 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7523 typedef psinfo32_t elfcore_psinfo32_t
;
7527 /* return a malloc'ed copy of a string at START which is at
7528 most MAX bytes long, possibly without a terminating '\0'.
7529 the copy will always have a terminating '\0'. */
7532 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
7535 char *end
= memchr (start
, '\0', max
);
7543 dups
= bfd_alloc (abfd
, len
+ 1);
7547 memcpy (dups
, start
, len
);
7553 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7555 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7557 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
7559 elfcore_psinfo_t psinfo
;
7561 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7563 elf_tdata (abfd
)->core_program
7564 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7565 sizeof (psinfo
.pr_fname
));
7567 elf_tdata (abfd
)->core_command
7568 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7569 sizeof (psinfo
.pr_psargs
));
7571 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7572 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
7574 /* 64-bit host, 32-bit corefile */
7575 elfcore_psinfo32_t psinfo
;
7577 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7579 elf_tdata (abfd
)->core_program
7580 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7581 sizeof (psinfo
.pr_fname
));
7583 elf_tdata (abfd
)->core_command
7584 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7585 sizeof (psinfo
.pr_psargs
));
7591 /* Fail - we don't know how to handle any other
7592 note size (ie. data object type). */
7596 /* Note that for some reason, a spurious space is tacked
7597 onto the end of the args in some (at least one anyway)
7598 implementations, so strip it off if it exists. */
7601 char *command
= elf_tdata (abfd
)->core_command
;
7602 int n
= strlen (command
);
7604 if (0 < n
&& command
[n
- 1] == ' ')
7605 command
[n
- 1] = '\0';
7610 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7612 #if defined (HAVE_PSTATUS_T)
7614 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7616 if (note
->descsz
== sizeof (pstatus_t
)
7617 #if defined (HAVE_PXSTATUS_T)
7618 || note
->descsz
== sizeof (pxstatus_t
)
7624 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
7626 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
7628 #if defined (HAVE_PSTATUS32_T)
7629 else if (note
->descsz
== sizeof (pstatus32_t
))
7631 /* 64-bit host, 32-bit corefile */
7634 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
7636 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
7639 /* Could grab some more details from the "representative"
7640 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7641 NT_LWPSTATUS note, presumably. */
7645 #endif /* defined (HAVE_PSTATUS_T) */
7647 #if defined (HAVE_LWPSTATUS_T)
7649 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7651 lwpstatus_t lwpstat
;
7657 if (note
->descsz
!= sizeof (lwpstat
)
7658 #if defined (HAVE_LWPXSTATUS_T)
7659 && note
->descsz
!= sizeof (lwpxstatus_t
)
7664 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
7666 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
7667 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
7669 /* Make a ".reg/999" section. */
7671 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
7672 len
= strlen (buf
) + 1;
7673 name
= bfd_alloc (abfd
, len
);
7676 memcpy (name
, buf
, len
);
7678 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7682 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7683 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
7684 sect
->filepos
= note
->descpos
7685 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
7688 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7689 sect
->size
= sizeof (lwpstat
.pr_reg
);
7690 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
7693 sect
->alignment_power
= 2;
7695 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7698 /* Make a ".reg2/999" section */
7700 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
7701 len
= strlen (buf
) + 1;
7702 name
= bfd_alloc (abfd
, len
);
7705 memcpy (name
, buf
, len
);
7707 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7711 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7712 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
7713 sect
->filepos
= note
->descpos
7714 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
7717 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7718 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
7719 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
7722 sect
->alignment_power
= 2;
7724 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
7726 #endif /* defined (HAVE_LWPSTATUS_T) */
7729 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7736 int is_active_thread
;
7739 if (note
->descsz
< 728)
7742 if (! CONST_STRNEQ (note
->namedata
, "win32"))
7745 type
= bfd_get_32 (abfd
, note
->descdata
);
7749 case 1 /* NOTE_INFO_PROCESS */:
7750 /* FIXME: need to add ->core_command. */
7751 /* process_info.pid */
7752 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
7753 /* process_info.signal */
7754 elf_tdata (abfd
)->core_signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
7757 case 2 /* NOTE_INFO_THREAD */:
7758 /* Make a ".reg/999" section. */
7759 /* thread_info.tid */
7760 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
7762 len
= strlen (buf
) + 1;
7763 name
= bfd_alloc (abfd
, len
);
7767 memcpy (name
, buf
, len
);
7769 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7773 /* sizeof (thread_info.thread_context) */
7775 /* offsetof (thread_info.thread_context) */
7776 sect
->filepos
= note
->descpos
+ 12;
7777 sect
->alignment_power
= 2;
7779 /* thread_info.is_active_thread */
7780 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
7782 if (is_active_thread
)
7783 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7787 case 3 /* NOTE_INFO_MODULE */:
7788 /* Make a ".module/xxxxxxxx" section. */
7789 /* module_info.base_address */
7790 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
7791 sprintf (buf
, ".module/%08lx", (long) base_addr
);
7793 len
= strlen (buf
) + 1;
7794 name
= bfd_alloc (abfd
, len
);
7798 memcpy (name
, buf
, len
);
7800 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7805 sect
->size
= note
->descsz
;
7806 sect
->filepos
= note
->descpos
;
7807 sect
->alignment_power
= 2;
7818 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7820 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7828 if (bed
->elf_backend_grok_prstatus
)
7829 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
7831 #if defined (HAVE_PRSTATUS_T)
7832 return elfcore_grok_prstatus (abfd
, note
);
7837 #if defined (HAVE_PSTATUS_T)
7839 return elfcore_grok_pstatus (abfd
, note
);
7842 #if defined (HAVE_LWPSTATUS_T)
7844 return elfcore_grok_lwpstatus (abfd
, note
);
7847 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
7848 return elfcore_grok_prfpreg (abfd
, note
);
7850 case NT_WIN32PSTATUS
:
7851 return elfcore_grok_win32pstatus (abfd
, note
);
7853 case NT_PRXFPREG
: /* Linux SSE extension */
7854 if (note
->namesz
== 6
7855 && strcmp (note
->namedata
, "LINUX") == 0)
7856 return elfcore_grok_prxfpreg (abfd
, note
);
7861 if (note
->namesz
== 6
7862 && strcmp (note
->namedata
, "LINUX") == 0)
7863 return elfcore_grok_ppc_vmx (abfd
, note
);
7869 if (bed
->elf_backend_grok_psinfo
)
7870 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
7872 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7873 return elfcore_grok_psinfo (abfd
, note
);
7880 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
7885 sect
->size
= note
->descsz
;
7886 sect
->filepos
= note
->descpos
;
7887 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
7895 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
7897 elf_tdata (abfd
)->build_id_size
= note
->descsz
;
7898 elf_tdata (abfd
)->build_id
= bfd_alloc (abfd
, note
->descsz
);
7899 if (elf_tdata (abfd
)->build_id
== NULL
)
7902 memcpy (elf_tdata (abfd
)->build_id
, note
->descdata
, note
->descsz
);
7908 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7915 case NT_GNU_BUILD_ID
:
7916 return elfobj_grok_gnu_build_id (abfd
, note
);
7921 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
7925 cp
= strchr (note
->namedata
, '@');
7928 *lwpidp
= atoi(cp
+ 1);
7935 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7937 /* Signal number at offset 0x08. */
7938 elf_tdata (abfd
)->core_signal
7939 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
7941 /* Process ID at offset 0x50. */
7942 elf_tdata (abfd
)->core_pid
7943 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
7945 /* Command name at 0x7c (max 32 bytes, including nul). */
7946 elf_tdata (abfd
)->core_command
7947 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
7949 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
7954 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7958 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
7959 elf_tdata (abfd
)->core_lwpid
= lwp
;
7961 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
7963 /* NetBSD-specific core "procinfo". Note that we expect to
7964 find this note before any of the others, which is fine,
7965 since the kernel writes this note out first when it
7966 creates a core file. */
7968 return elfcore_grok_netbsd_procinfo (abfd
, note
);
7971 /* As of Jan 2002 there are no other machine-independent notes
7972 defined for NetBSD core files. If the note type is less
7973 than the start of the machine-dependent note types, we don't
7976 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
7980 switch (bfd_get_arch (abfd
))
7982 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7983 PT_GETFPREGS == mach+2. */
7985 case bfd_arch_alpha
:
7986 case bfd_arch_sparc
:
7989 case NT_NETBSDCORE_FIRSTMACH
+0:
7990 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
7992 case NT_NETBSDCORE_FIRSTMACH
+2:
7993 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7999 /* On all other arch's, PT_GETREGS == mach+1 and
8000 PT_GETFPREGS == mach+3. */
8005 case NT_NETBSDCORE_FIRSTMACH
+1:
8006 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8008 case NT_NETBSDCORE_FIRSTMACH
+3:
8009 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8019 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
8021 void *ddata
= note
->descdata
;
8028 /* nto_procfs_status 'pid' field is at offset 0. */
8029 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
8031 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8032 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
8034 /* nto_procfs_status 'flags' field is at offset 8. */
8035 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
8037 /* nto_procfs_status 'what' field is at offset 14. */
8038 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
8040 elf_tdata (abfd
)->core_signal
= sig
;
8041 elf_tdata (abfd
)->core_lwpid
= *tid
;
8044 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8045 do not come from signals so we make sure we set the current
8046 thread just in case. */
8047 if (flags
& 0x00000080)
8048 elf_tdata (abfd
)->core_lwpid
= *tid
;
8050 /* Make a ".qnx_core_status/%d" section. */
8051 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
8053 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
8058 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8062 sect
->size
= note
->descsz
;
8063 sect
->filepos
= note
->descpos
;
8064 sect
->alignment_power
= 2;
8066 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
8070 elfcore_grok_nto_regs (bfd
*abfd
,
8071 Elf_Internal_Note
*note
,
8079 /* Make a "(base)/%d" section. */
8080 sprintf (buf
, "%s/%ld", base
, tid
);
8082 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
8087 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8091 sect
->size
= note
->descsz
;
8092 sect
->filepos
= note
->descpos
;
8093 sect
->alignment_power
= 2;
8095 /* This is the current thread. */
8096 if (elf_tdata (abfd
)->core_lwpid
== tid
)
8097 return elfcore_maybe_make_sect (abfd
, base
, sect
);
8102 #define BFD_QNT_CORE_INFO 7
8103 #define BFD_QNT_CORE_STATUS 8
8104 #define BFD_QNT_CORE_GREG 9
8105 #define BFD_QNT_CORE_FPREG 10
8108 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8110 /* Every GREG section has a STATUS section before it. Store the
8111 tid from the previous call to pass down to the next gregs
8113 static long tid
= 1;
8117 case BFD_QNT_CORE_INFO
:
8118 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
8119 case BFD_QNT_CORE_STATUS
:
8120 return elfcore_grok_nto_status (abfd
, note
, &tid
);
8121 case BFD_QNT_CORE_GREG
:
8122 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
8123 case BFD_QNT_CORE_FPREG
:
8124 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
8131 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8137 /* Use note name as section name. */
8139 name
= bfd_alloc (abfd
, len
);
8142 memcpy (name
, note
->namedata
, len
);
8143 name
[len
- 1] = '\0';
8145 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8149 sect
->size
= note
->descsz
;
8150 sect
->filepos
= note
->descpos
;
8151 sect
->alignment_power
= 1;
8156 /* Function: elfcore_write_note
8159 buffer to hold note, and current size of buffer
8163 size of data for note
8165 Writes note to end of buffer. ELF64 notes are written exactly as
8166 for ELF32, despite the current (as of 2006) ELF gabi specifying
8167 that they ought to have 8-byte namesz and descsz field, and have
8168 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8171 Pointer to realloc'd buffer, *BUFSIZ updated. */
8174 elfcore_write_note (bfd
*abfd
,
8182 Elf_External_Note
*xnp
;
8189 namesz
= strlen (name
) + 1;
8191 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
8193 buf
= realloc (buf
, *bufsiz
+ newspace
);
8196 dest
= buf
+ *bufsiz
;
8197 *bufsiz
+= newspace
;
8198 xnp
= (Elf_External_Note
*) dest
;
8199 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
8200 H_PUT_32 (abfd
, size
, xnp
->descsz
);
8201 H_PUT_32 (abfd
, type
, xnp
->type
);
8205 memcpy (dest
, name
, namesz
);
8213 memcpy (dest
, input
, size
);
8223 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8225 elfcore_write_prpsinfo (bfd
*abfd
,
8231 const char *note_name
= "CORE";
8232 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8234 if (bed
->elf_backend_write_core_note
!= NULL
)
8237 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
8238 NT_PRPSINFO
, fname
, psargs
);
8243 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8244 if (bed
->s
->elfclass
== ELFCLASS32
)
8246 #if defined (HAVE_PSINFO32_T)
8248 int note_type
= NT_PSINFO
;
8251 int note_type
= NT_PRPSINFO
;
8254 memset (&data
, 0, sizeof (data
));
8255 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
8256 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
8257 return elfcore_write_note (abfd
, buf
, bufsiz
,
8258 note_name
, note_type
, &data
, sizeof (data
));
8263 #if defined (HAVE_PSINFO_T)
8265 int note_type
= NT_PSINFO
;
8268 int note_type
= NT_PRPSINFO
;
8271 memset (&data
, 0, sizeof (data
));
8272 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
8273 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
8274 return elfcore_write_note (abfd
, buf
, bufsiz
,
8275 note_name
, note_type
, &data
, sizeof (data
));
8278 #endif /* PSINFO_T or PRPSINFO_T */
8280 #if defined (HAVE_PRSTATUS_T)
8282 elfcore_write_prstatus (bfd
*abfd
,
8289 const char *note_name
= "CORE";
8290 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8292 if (bed
->elf_backend_write_core_note
!= NULL
)
8295 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
8297 pid
, cursig
, gregs
);
8302 #if defined (HAVE_PRSTATUS32_T)
8303 if (bed
->s
->elfclass
== ELFCLASS32
)
8305 prstatus32_t prstat
;
8307 memset (&prstat
, 0, sizeof (prstat
));
8308 prstat
.pr_pid
= pid
;
8309 prstat
.pr_cursig
= cursig
;
8310 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8311 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8312 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8319 memset (&prstat
, 0, sizeof (prstat
));
8320 prstat
.pr_pid
= pid
;
8321 prstat
.pr_cursig
= cursig
;
8322 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8323 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8324 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8327 #endif /* HAVE_PRSTATUS_T */
8329 #if defined (HAVE_LWPSTATUS_T)
8331 elfcore_write_lwpstatus (bfd
*abfd
,
8338 lwpstatus_t lwpstat
;
8339 const char *note_name
= "CORE";
8341 memset (&lwpstat
, 0, sizeof (lwpstat
));
8342 lwpstat
.pr_lwpid
= pid
>> 16;
8343 lwpstat
.pr_cursig
= cursig
;
8344 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8345 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
8346 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8348 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
8349 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
8351 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
8352 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
8355 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8356 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
8358 #endif /* HAVE_LWPSTATUS_T */
8360 #if defined (HAVE_PSTATUS_T)
8362 elfcore_write_pstatus (bfd
*abfd
,
8366 int cursig ATTRIBUTE_UNUSED
,
8367 const void *gregs ATTRIBUTE_UNUSED
)
8369 const char *note_name
= "CORE";
8370 #if defined (HAVE_PSTATUS32_T)
8371 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8373 if (bed
->s
->elfclass
== ELFCLASS32
)
8377 memset (&pstat
, 0, sizeof (pstat
));
8378 pstat
.pr_pid
= pid
& 0xffff;
8379 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8380 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8388 memset (&pstat
, 0, sizeof (pstat
));
8389 pstat
.pr_pid
= pid
& 0xffff;
8390 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8391 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8395 #endif /* HAVE_PSTATUS_T */
8398 elfcore_write_prfpreg (bfd
*abfd
,
8404 const char *note_name
= "CORE";
8405 return elfcore_write_note (abfd
, buf
, bufsiz
,
8406 note_name
, NT_FPREGSET
, fpregs
, size
);
8410 elfcore_write_prxfpreg (bfd
*abfd
,
8413 const void *xfpregs
,
8416 char *note_name
= "LINUX";
8417 return elfcore_write_note (abfd
, buf
, bufsiz
,
8418 note_name
, NT_PRXFPREG
, xfpregs
, size
);
8422 elfcore_write_ppc_vmx (bfd
*abfd
,
8425 const void *ppc_vmx
,
8428 char *note_name
= "LINUX";
8429 return elfcore_write_note (abfd
, buf
, bufsiz
,
8430 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
8434 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
8439 while (p
< buf
+ size
)
8441 /* FIXME: bad alignment assumption. */
8442 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
8443 Elf_Internal_Note in
;
8445 in
.type
= H_GET_32 (abfd
, xnp
->type
);
8447 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
8448 in
.namedata
= xnp
->name
;
8450 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
8451 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
8452 in
.descpos
= offset
+ (in
.descdata
- buf
);
8454 switch (bfd_get_format (abfd
))
8460 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
8462 if (! elfcore_grok_netbsd_note (abfd
, &in
))
8465 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
8467 if (! elfcore_grok_nto_note (abfd
, &in
))
8470 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
8472 if (! elfcore_grok_spu_note (abfd
, &in
))
8477 if (! elfcore_grok_note (abfd
, &in
))
8483 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
8485 if (! elfobj_grok_gnu_note (abfd
, &in
))
8491 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
8498 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
8505 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
8508 buf
= bfd_malloc (size
);
8512 if (bfd_bread (buf
, size
, abfd
) != size
8513 || !elf_parse_notes (abfd
, buf
, size
, offset
))
8523 /* Providing external access to the ELF program header table. */
8525 /* Return an upper bound on the number of bytes required to store a
8526 copy of ABFD's program header table entries. Return -1 if an error
8527 occurs; bfd_get_error will return an appropriate code. */
8530 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
8532 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8534 bfd_set_error (bfd_error_wrong_format
);
8538 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
8541 /* Copy ABFD's program header table entries to *PHDRS. The entries
8542 will be stored as an array of Elf_Internal_Phdr structures, as
8543 defined in include/elf/internal.h. To find out how large the
8544 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8546 Return the number of program header table entries read, or -1 if an
8547 error occurs; bfd_get_error will return an appropriate code. */
8550 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
8554 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8556 bfd_set_error (bfd_error_wrong_format
);
8560 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
8561 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
8562 num_phdrs
* sizeof (Elf_Internal_Phdr
));
8567 enum elf_reloc_type_class
8568 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
8570 return reloc_class_normal
;
8573 /* For RELA architectures, return the relocation value for a
8574 relocation against a local symbol. */
8577 _bfd_elf_rela_local_sym (bfd
*abfd
,
8578 Elf_Internal_Sym
*sym
,
8580 Elf_Internal_Rela
*rel
)
8582 asection
*sec
= *psec
;
8585 relocation
= (sec
->output_section
->vma
8586 + sec
->output_offset
8588 if ((sec
->flags
& SEC_MERGE
)
8589 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
8590 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
8593 _bfd_merged_section_offset (abfd
, psec
,
8594 elf_section_data (sec
)->sec_info
,
8595 sym
->st_value
+ rel
->r_addend
);
8598 /* If we have changed the section, and our original section is
8599 marked with SEC_EXCLUDE, it means that the original
8600 SEC_MERGE section has been completely subsumed in some
8601 other SEC_MERGE section. In this case, we need to leave
8602 some info around for --emit-relocs. */
8603 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
8604 sec
->kept_section
= *psec
;
8607 rel
->r_addend
-= relocation
;
8608 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
8614 _bfd_elf_rel_local_sym (bfd
*abfd
,
8615 Elf_Internal_Sym
*sym
,
8619 asection
*sec
= *psec
;
8621 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
8622 return sym
->st_value
+ addend
;
8624 return _bfd_merged_section_offset (abfd
, psec
,
8625 elf_section_data (sec
)->sec_info
,
8626 sym
->st_value
+ addend
);
8630 _bfd_elf_section_offset (bfd
*abfd
,
8631 struct bfd_link_info
*info
,
8635 switch (sec
->sec_info_type
)
8637 case ELF_INFO_TYPE_STABS
:
8638 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
8640 case ELF_INFO_TYPE_EH_FRAME
:
8641 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
8647 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8648 reconstruct an ELF file by reading the segments out of remote memory
8649 based on the ELF file header at EHDR_VMA and the ELF program headers it
8650 points to. If not null, *LOADBASEP is filled in with the difference
8651 between the VMAs from which the segments were read, and the VMAs the
8652 file headers (and hence BFD's idea of each section's VMA) put them at.
8654 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8655 remote memory at target address VMA into the local buffer at MYADDR; it
8656 should return zero on success or an `errno' code on failure. TEMPL must
8657 be a BFD for an ELF target with the word size and byte order found in
8658 the remote memory. */
8661 bfd_elf_bfd_from_remote_memory
8665 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, int))
8667 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
8668 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
8672 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
8673 long symcount ATTRIBUTE_UNUSED
,
8674 asymbol
**syms ATTRIBUTE_UNUSED
,
8679 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8682 const char *relplt_name
;
8683 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8687 Elf_Internal_Shdr
*hdr
;
8693 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
8696 if (dynsymcount
<= 0)
8699 if (!bed
->plt_sym_val
)
8702 relplt_name
= bed
->relplt_name
;
8703 if (relplt_name
== NULL
)
8704 relplt_name
= bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt";
8705 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
8709 hdr
= &elf_section_data (relplt
)->this_hdr
;
8710 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
8711 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
8714 plt
= bfd_get_section_by_name (abfd
, ".plt");
8718 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8719 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
8722 count
= relplt
->size
/ hdr
->sh_entsize
;
8723 size
= count
* sizeof (asymbol
);
8724 p
= relplt
->relocation
;
8725 for (i
= 0; i
< count
; i
++, p
++)
8726 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
8728 s
= *ret
= bfd_malloc (size
);
8732 names
= (char *) (s
+ count
);
8733 p
= relplt
->relocation
;
8735 for (i
= 0; i
< count
; i
++, p
++)
8740 addr
= bed
->plt_sym_val (i
, plt
, p
);
8741 if (addr
== (bfd_vma
) -1)
8744 *s
= **p
->sym_ptr_ptr
;
8745 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8746 we are defining a symbol, ensure one of them is set. */
8747 if ((s
->flags
& BSF_LOCAL
) == 0)
8748 s
->flags
|= BSF_GLOBAL
;
8750 s
->value
= addr
- plt
->vma
;
8753 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
8754 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
8756 memcpy (names
, "@plt", sizeof ("@plt"));
8757 names
+= sizeof ("@plt");
8764 /* It is only used by x86-64 so far. */
8765 asection _bfd_elf_large_com_section
8766 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
8767 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
8770 _bfd_elf_set_osabi (bfd
* abfd
,
8771 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
8773 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
8775 i_ehdrp
= elf_elfheader (abfd
);
8777 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
8781 /* Return TRUE for ELF symbol types that represent functions.
8782 This is the default version of this function, which is sufficient for
8783 most targets. It returns true if TYPE is STT_FUNC. */
8786 _bfd_elf_is_function_type (unsigned int type
)
8788 return (type
== STT_FUNC
);