1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
32 haven't bothered yet. */
34 /* For sparc64-cross-sparc32. */
42 #include "libiberty.h"
44 static int elf_sort_sections (const void *, const void *);
45 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
46 static bfd_boolean
prep_headers (bfd
*);
47 static bfd_boolean
swap_out_syms (bfd
*, struct bfd_strtab_hash
**, int) ;
48 static bfd_boolean
elfcore_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
50 /* Swap version information in and out. The version information is
51 currently size independent. If that ever changes, this code will
52 need to move into elfcode.h. */
54 /* Swap in a Verdef structure. */
57 _bfd_elf_swap_verdef_in (bfd
*abfd
,
58 const Elf_External_Verdef
*src
,
59 Elf_Internal_Verdef
*dst
)
61 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
62 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
63 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
64 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
65 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
66 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
67 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
70 /* Swap out a Verdef structure. */
73 _bfd_elf_swap_verdef_out (bfd
*abfd
,
74 const Elf_Internal_Verdef
*src
,
75 Elf_External_Verdef
*dst
)
77 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
78 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
79 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
80 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
81 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
82 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
83 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
86 /* Swap in a Verdaux structure. */
89 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
90 const Elf_External_Verdaux
*src
,
91 Elf_Internal_Verdaux
*dst
)
93 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
94 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
97 /* Swap out a Verdaux structure. */
100 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
101 const Elf_Internal_Verdaux
*src
,
102 Elf_External_Verdaux
*dst
)
104 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
105 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
108 /* Swap in a Verneed structure. */
111 _bfd_elf_swap_verneed_in (bfd
*abfd
,
112 const Elf_External_Verneed
*src
,
113 Elf_Internal_Verneed
*dst
)
115 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
116 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
117 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
118 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
119 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
122 /* Swap out a Verneed structure. */
125 _bfd_elf_swap_verneed_out (bfd
*abfd
,
126 const Elf_Internal_Verneed
*src
,
127 Elf_External_Verneed
*dst
)
129 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
130 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
131 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
132 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
133 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
136 /* Swap in a Vernaux structure. */
139 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
140 const Elf_External_Vernaux
*src
,
141 Elf_Internal_Vernaux
*dst
)
143 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
144 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
145 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
146 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
147 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
150 /* Swap out a Vernaux structure. */
153 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
154 const Elf_Internal_Vernaux
*src
,
155 Elf_External_Vernaux
*dst
)
157 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
158 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
159 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
160 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
161 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
164 /* Swap in a Versym structure. */
167 _bfd_elf_swap_versym_in (bfd
*abfd
,
168 const Elf_External_Versym
*src
,
169 Elf_Internal_Versym
*dst
)
171 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
174 /* Swap out a Versym structure. */
177 _bfd_elf_swap_versym_out (bfd
*abfd
,
178 const Elf_Internal_Versym
*src
,
179 Elf_External_Versym
*dst
)
181 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
184 /* Standard ELF hash function. Do not change this function; you will
185 cause invalid hash tables to be generated. */
188 bfd_elf_hash (const char *namearg
)
190 const unsigned char *name
= (const unsigned char *) namearg
;
195 while ((ch
= *name
++) != '\0')
198 if ((g
= (h
& 0xf0000000)) != 0)
201 /* The ELF ABI says `h &= ~g', but this is equivalent in
202 this case and on some machines one insn instead of two. */
206 return h
& 0xffffffff;
209 /* Read a specified number of bytes at a specified offset in an ELF
210 file, into a newly allocated buffer, and return a pointer to the
214 elf_read (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
218 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
220 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
222 if (bfd_bread (buf
, size
, abfd
) != size
)
224 if (bfd_get_error () != bfd_error_system_call
)
225 bfd_set_error (bfd_error_file_truncated
);
232 bfd_elf_mkobject (bfd
*abfd
)
234 /* This just does initialization. */
235 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
236 elf_tdata (abfd
) = bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
237 if (elf_tdata (abfd
) == 0)
239 /* Since everything is done at close time, do we need any
246 bfd_elf_mkcorefile (bfd
*abfd
)
248 /* I think this can be done just like an object file. */
249 return bfd_elf_mkobject (abfd
);
253 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
255 Elf_Internal_Shdr
**i_shdrp
;
256 char *shstrtab
= NULL
;
258 bfd_size_type shstrtabsize
;
260 i_shdrp
= elf_elfsections (abfd
);
261 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
264 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
265 if (shstrtab
== NULL
)
267 /* No cached one, attempt to read, and cache what we read. */
268 offset
= i_shdrp
[shindex
]->sh_offset
;
269 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
270 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
271 i_shdrp
[shindex
]->contents
= shstrtab
;
277 bfd_elf_string_from_elf_section (bfd
*abfd
,
278 unsigned int shindex
,
279 unsigned int strindex
)
281 Elf_Internal_Shdr
*hdr
;
286 hdr
= elf_elfsections (abfd
)[shindex
];
288 if (hdr
->contents
== NULL
289 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
292 if (strindex
>= hdr
->sh_size
)
294 (*_bfd_error_handler
)
295 (_("%B: invalid string offset %u >= %lu for section `%s'"),
296 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
297 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
298 && strindex
== hdr
->sh_name
)
300 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
304 return ((char *) hdr
->contents
) + strindex
;
307 /* Read and convert symbols to internal format.
308 SYMCOUNT specifies the number of symbols to read, starting from
309 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
310 are non-NULL, they are used to store the internal symbols, external
311 symbols, and symbol section index extensions, respectively. */
314 bfd_elf_get_elf_syms (bfd
*ibfd
,
315 Elf_Internal_Shdr
*symtab_hdr
,
318 Elf_Internal_Sym
*intsym_buf
,
320 Elf_External_Sym_Shndx
*extshndx_buf
)
322 Elf_Internal_Shdr
*shndx_hdr
;
324 const bfd_byte
*esym
;
325 Elf_External_Sym_Shndx
*alloc_extshndx
;
326 Elf_External_Sym_Shndx
*shndx
;
327 Elf_Internal_Sym
*isym
;
328 Elf_Internal_Sym
*isymend
;
329 const struct elf_backend_data
*bed
;
337 /* Normal syms might have section extension entries. */
339 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
340 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
342 /* Read the symbols. */
344 alloc_extshndx
= NULL
;
345 bed
= get_elf_backend_data (ibfd
);
346 extsym_size
= bed
->s
->sizeof_sym
;
347 amt
= symcount
* extsym_size
;
348 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
349 if (extsym_buf
== NULL
)
351 alloc_ext
= bfd_malloc (amt
);
352 extsym_buf
= alloc_ext
;
354 if (extsym_buf
== NULL
355 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
356 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
362 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
366 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
367 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
368 if (extshndx_buf
== NULL
)
370 alloc_extshndx
= bfd_malloc (amt
);
371 extshndx_buf
= alloc_extshndx
;
373 if (extshndx_buf
== NULL
374 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
375 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
382 if (intsym_buf
== NULL
)
384 bfd_size_type amt
= symcount
* sizeof (Elf_Internal_Sym
);
385 intsym_buf
= bfd_malloc (amt
);
386 if (intsym_buf
== NULL
)
390 /* Convert the symbols to internal form. */
391 isymend
= intsym_buf
+ symcount
;
392 for (esym
= extsym_buf
, isym
= intsym_buf
, shndx
= extshndx_buf
;
394 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
395 (*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
);
398 if (alloc_ext
!= NULL
)
400 if (alloc_extshndx
!= NULL
)
401 free (alloc_extshndx
);
406 /* Look up a symbol name. */
408 bfd_elf_sym_name (bfd
*abfd
,
409 Elf_Internal_Shdr
*symtab_hdr
,
410 Elf_Internal_Sym
*isym
)
412 unsigned int iname
= isym
->st_name
;
413 unsigned int shindex
= symtab_hdr
->sh_link
;
414 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
415 /* Check for a bogus st_shndx to avoid crashing. */
416 && isym
->st_shndx
< elf_numsections (abfd
)
417 && !(isym
->st_shndx
>= SHN_LORESERVE
&& isym
->st_shndx
<= SHN_HIRESERVE
))
419 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
420 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
423 return bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
426 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
427 sections. The first element is the flags, the rest are section
430 typedef union elf_internal_group
{
431 Elf_Internal_Shdr
*shdr
;
433 } Elf_Internal_Group
;
435 /* Return the name of the group signature symbol. Why isn't the
436 signature just a string? */
439 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
441 Elf_Internal_Shdr
*hdr
;
442 unsigned char esym
[sizeof (Elf64_External_Sym
)];
443 Elf_External_Sym_Shndx eshndx
;
444 Elf_Internal_Sym isym
;
446 /* First we need to ensure the symbol table is available. */
447 if (! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
450 /* Go read the symbol. */
451 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
452 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
453 &isym
, esym
, &eshndx
) == NULL
)
456 return bfd_elf_sym_name (abfd
, hdr
, &isym
);
459 /* Set next_in_group list pointer, and group name for NEWSECT. */
462 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
464 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
466 /* If num_group is zero, read in all SHT_GROUP sections. The count
467 is set to -1 if there are no SHT_GROUP sections. */
470 unsigned int i
, shnum
;
472 /* First count the number of groups. If we have a SHT_GROUP
473 section with just a flag word (ie. sh_size is 4), ignore it. */
474 shnum
= elf_numsections (abfd
);
476 for (i
= 0; i
< shnum
; i
++)
478 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
479 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
484 num_group
= (unsigned) -1;
485 elf_tdata (abfd
)->num_group
= num_group
;
489 /* We keep a list of elf section headers for group sections,
490 so we can find them quickly. */
491 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
492 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
493 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
497 for (i
= 0; i
< shnum
; i
++)
499 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
500 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
503 Elf_Internal_Group
*dest
;
505 /* Add to list of sections. */
506 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
509 /* Read the raw contents. */
510 BFD_ASSERT (sizeof (*dest
) >= 4);
511 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
512 shdr
->contents
= bfd_alloc (abfd
, amt
);
513 if (shdr
->contents
== NULL
514 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
515 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
519 /* Translate raw contents, a flag word followed by an
520 array of elf section indices all in target byte order,
521 to the flag word followed by an array of elf section
523 src
= shdr
->contents
+ shdr
->sh_size
;
524 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
531 idx
= H_GET_32 (abfd
, src
);
532 if (src
== shdr
->contents
)
535 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
536 shdr
->bfd_section
->flags
537 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
542 ((*_bfd_error_handler
)
543 (_("%B: invalid SHT_GROUP entry"), abfd
));
546 dest
->shdr
= elf_elfsections (abfd
)[idx
];
553 if (num_group
!= (unsigned) -1)
557 for (i
= 0; i
< num_group
; i
++)
559 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
560 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
561 unsigned int n_elt
= shdr
->sh_size
/ 4;
563 /* Look through this group's sections to see if current
564 section is a member. */
566 if ((++idx
)->shdr
== hdr
)
570 /* We are a member of this group. Go looking through
571 other members to see if any others are linked via
573 idx
= (Elf_Internal_Group
*) shdr
->contents
;
574 n_elt
= shdr
->sh_size
/ 4;
576 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
577 && elf_next_in_group (s
) != NULL
)
581 /* Snarf the group name from other member, and
582 insert current section in circular list. */
583 elf_group_name (newsect
) = elf_group_name (s
);
584 elf_next_in_group (newsect
) = elf_next_in_group (s
);
585 elf_next_in_group (s
) = newsect
;
591 gname
= group_signature (abfd
, shdr
);
594 elf_group_name (newsect
) = gname
;
596 /* Start a circular list with one element. */
597 elf_next_in_group (newsect
) = newsect
;
600 /* If the group section has been created, point to the
602 if (shdr
->bfd_section
!= NULL
)
603 elf_next_in_group (shdr
->bfd_section
) = newsect
;
611 if (elf_group_name (newsect
) == NULL
)
613 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
620 _bfd_elf_setup_group_pointers (bfd
*abfd
)
623 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
624 bfd_boolean result
= TRUE
;
626 if (num_group
== (unsigned) -1)
629 for (i
= 0; i
< num_group
; i
++)
631 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
632 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
633 unsigned int n_elt
= shdr
->sh_size
/ 4;
636 if ((++idx
)->shdr
->bfd_section
)
637 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
638 else if (idx
->shdr
->sh_type
== SHT_RELA
639 || idx
->shdr
->sh_type
== SHT_REL
)
640 /* We won't include relocation sections in section groups in
641 output object files. We adjust the group section size here
642 so that relocatable link will work correctly when
643 relocation sections are in section group in input object
645 shdr
->bfd_section
->size
-= 4;
648 /* There are some unknown sections in the group. */
649 (*_bfd_error_handler
)
650 (_("%B: unknown [%d] section `%s' in group [%s]"),
652 (unsigned int) idx
->shdr
->sh_type
,
653 elf_string_from_elf_strtab (abfd
, idx
->shdr
->sh_name
),
654 shdr
->bfd_section
->name
);
662 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
664 return elf_next_in_group (sec
) != NULL
;
667 /* Make a BFD section from an ELF section. We store a pointer to the
668 BFD section in the bfd_section field of the header. */
671 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
672 Elf_Internal_Shdr
*hdr
,
677 const struct elf_backend_data
*bed
;
679 if (hdr
->bfd_section
!= NULL
)
681 BFD_ASSERT (strcmp (name
,
682 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
686 newsect
= bfd_make_section_anyway (abfd
, name
);
690 hdr
->bfd_section
= newsect
;
691 elf_section_data (newsect
)->this_hdr
= *hdr
;
693 /* Always use the real type/flags. */
694 elf_section_type (newsect
) = hdr
->sh_type
;
695 elf_section_flags (newsect
) = hdr
->sh_flags
;
697 newsect
->filepos
= hdr
->sh_offset
;
699 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
700 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
701 || ! bfd_set_section_alignment (abfd
, newsect
,
702 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
705 flags
= SEC_NO_FLAGS
;
706 if (hdr
->sh_type
!= SHT_NOBITS
)
707 flags
|= SEC_HAS_CONTENTS
;
708 if (hdr
->sh_type
== SHT_GROUP
)
709 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
710 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
713 if (hdr
->sh_type
!= SHT_NOBITS
)
716 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
717 flags
|= SEC_READONLY
;
718 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
720 else if ((flags
& SEC_LOAD
) != 0)
722 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
725 newsect
->entsize
= hdr
->sh_entsize
;
726 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
727 flags
|= SEC_STRINGS
;
729 if (hdr
->sh_flags
& SHF_GROUP
)
730 if (!setup_group (abfd
, hdr
, newsect
))
732 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
733 flags
|= SEC_THREAD_LOCAL
;
735 /* The debugging sections appear to be recognized only by name, not
738 static const char *debug_sec_names
[] =
747 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
748 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
752 flags
|= SEC_DEBUGGING
;
755 /* As a GNU extension, if the name begins with .gnu.linkonce, we
756 only link a single copy of the section. This is used to support
757 g++. g++ will emit each template expansion in its own section.
758 The symbols will be defined as weak, so that multiple definitions
759 are permitted. The GNU linker extension is to actually discard
760 all but one of the sections. */
761 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
762 && elf_next_in_group (newsect
) == NULL
)
763 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
765 bed
= get_elf_backend_data (abfd
);
766 if (bed
->elf_backend_section_flags
)
767 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
770 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
773 if ((flags
& SEC_ALLOC
) != 0)
775 Elf_Internal_Phdr
*phdr
;
778 /* Look through the phdrs to see if we need to adjust the lma.
779 If all the p_paddr fields are zero, we ignore them, since
780 some ELF linkers produce such output. */
781 phdr
= elf_tdata (abfd
)->phdr
;
782 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
784 if (phdr
->p_paddr
!= 0)
787 if (i
< elf_elfheader (abfd
)->e_phnum
)
789 phdr
= elf_tdata (abfd
)->phdr
;
790 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
792 /* This section is part of this segment if its file
793 offset plus size lies within the segment's memory
794 span and, if the section is loaded, the extent of the
795 loaded data lies within the extent of the segment.
797 Note - we used to check the p_paddr field as well, and
798 refuse to set the LMA if it was 0. This is wrong
799 though, as a perfectly valid initialised segment can
800 have a p_paddr of zero. Some architectures, eg ARM,
801 place special significance on the address 0 and
802 executables need to be able to have a segment which
803 covers this address. */
804 if (phdr
->p_type
== PT_LOAD
805 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
806 && (hdr
->sh_offset
+ hdr
->sh_size
807 <= phdr
->p_offset
+ phdr
->p_memsz
)
808 && ((flags
& SEC_LOAD
) == 0
809 || (hdr
->sh_offset
+ hdr
->sh_size
810 <= phdr
->p_offset
+ phdr
->p_filesz
)))
812 if ((flags
& SEC_LOAD
) == 0)
813 newsect
->lma
= (phdr
->p_paddr
814 + hdr
->sh_addr
- phdr
->p_vaddr
);
816 /* We used to use the same adjustment for SEC_LOAD
817 sections, but that doesn't work if the segment
818 is packed with code from multiple VMAs.
819 Instead we calculate the section LMA based on
820 the segment LMA. It is assumed that the
821 segment will contain sections with contiguous
822 LMAs, even if the VMAs are not. */
823 newsect
->lma
= (phdr
->p_paddr
824 + hdr
->sh_offset
- phdr
->p_offset
);
826 /* With contiguous segments, we can't tell from file
827 offsets whether a section with zero size should
828 be placed at the end of one segment or the
829 beginning of the next. Decide based on vaddr. */
830 if (hdr
->sh_addr
>= phdr
->p_vaddr
831 && (hdr
->sh_addr
+ hdr
->sh_size
832 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
847 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
850 Helper functions for GDB to locate the string tables.
851 Since BFD hides string tables from callers, GDB needs to use an
852 internal hook to find them. Sun's .stabstr, in particular,
853 isn't even pointed to by the .stab section, so ordinary
854 mechanisms wouldn't work to find it, even if we had some.
857 struct elf_internal_shdr
*
858 bfd_elf_find_section (bfd
*abfd
, char *name
)
860 Elf_Internal_Shdr
**i_shdrp
;
865 i_shdrp
= elf_elfsections (abfd
);
868 shstrtab
= bfd_elf_get_str_section (abfd
,
869 elf_elfheader (abfd
)->e_shstrndx
);
870 if (shstrtab
!= NULL
)
872 max
= elf_numsections (abfd
);
873 for (i
= 1; i
< max
; i
++)
874 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
881 const char *const bfd_elf_section_type_names
[] = {
882 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
883 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
884 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
887 /* ELF relocs are against symbols. If we are producing relocatable
888 output, and the reloc is against an external symbol, and nothing
889 has given us any additional addend, the resulting reloc will also
890 be against the same symbol. In such a case, we don't want to
891 change anything about the way the reloc is handled, since it will
892 all be done at final link time. Rather than put special case code
893 into bfd_perform_relocation, all the reloc types use this howto
894 function. It just short circuits the reloc if producing
895 relocatable output against an external symbol. */
897 bfd_reloc_status_type
898 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
899 arelent
*reloc_entry
,
901 void *data ATTRIBUTE_UNUSED
,
902 asection
*input_section
,
904 char **error_message ATTRIBUTE_UNUSED
)
906 if (output_bfd
!= NULL
907 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
908 && (! reloc_entry
->howto
->partial_inplace
909 || reloc_entry
->addend
== 0))
911 reloc_entry
->address
+= input_section
->output_offset
;
915 return bfd_reloc_continue
;
918 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
921 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
924 BFD_ASSERT (sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
925 sec
->sec_info_type
= ELF_INFO_TYPE_NONE
;
928 /* Finish SHF_MERGE section merging. */
931 _bfd_elf_merge_sections (bfd
*abfd
, struct bfd_link_info
*info
)
936 if (!is_elf_hash_table (info
->hash
))
939 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
940 if ((ibfd
->flags
& DYNAMIC
) == 0)
941 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
942 if ((sec
->flags
& SEC_MERGE
) != 0
943 && !bfd_is_abs_section (sec
->output_section
))
945 struct bfd_elf_section_data
*secdata
;
947 secdata
= elf_section_data (sec
);
948 if (! _bfd_add_merge_section (abfd
,
949 &elf_hash_table (info
)->merge_info
,
950 sec
, &secdata
->sec_info
))
952 else if (secdata
->sec_info
)
953 sec
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
956 if (elf_hash_table (info
)->merge_info
!= NULL
)
957 _bfd_merge_sections (abfd
, info
, elf_hash_table (info
)->merge_info
,
958 merge_sections_remove_hook
);
963 _bfd_elf_link_just_syms (asection
*sec
, struct bfd_link_info
*info
)
965 sec
->output_section
= bfd_abs_section_ptr
;
966 sec
->output_offset
= sec
->vma
;
967 if (!is_elf_hash_table (info
->hash
))
970 sec
->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
973 /* Copy the program header and other data from one object module to
977 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
979 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
980 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
983 BFD_ASSERT (!elf_flags_init (obfd
)
984 || (elf_elfheader (obfd
)->e_flags
985 == elf_elfheader (ibfd
)->e_flags
));
987 elf_gp (obfd
) = elf_gp (ibfd
);
988 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
989 elf_flags_init (obfd
) = TRUE
;
993 /* Print out the program headers. */
996 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
999 Elf_Internal_Phdr
*p
;
1001 bfd_byte
*dynbuf
= NULL
;
1003 p
= elf_tdata (abfd
)->phdr
;
1008 fprintf (f
, _("\nProgram Header:\n"));
1009 c
= elf_elfheader (abfd
)->e_phnum
;
1010 for (i
= 0; i
< c
; i
++, p
++)
1017 case PT_NULL
: pt
= "NULL"; break;
1018 case PT_LOAD
: pt
= "LOAD"; break;
1019 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1020 case PT_INTERP
: pt
= "INTERP"; break;
1021 case PT_NOTE
: pt
= "NOTE"; break;
1022 case PT_SHLIB
: pt
= "SHLIB"; break;
1023 case PT_PHDR
: pt
= "PHDR"; break;
1024 case PT_TLS
: pt
= "TLS"; break;
1025 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1026 case PT_GNU_STACK
: pt
= "STACK"; break;
1027 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1028 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
1030 fprintf (f
, "%8s off 0x", pt
);
1031 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1032 fprintf (f
, " vaddr 0x");
1033 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1034 fprintf (f
, " paddr 0x");
1035 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1036 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1037 fprintf (f
, " filesz 0x");
1038 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1039 fprintf (f
, " memsz 0x");
1040 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1041 fprintf (f
, " flags %c%c%c",
1042 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1043 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1044 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1045 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1046 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1051 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1055 unsigned long shlink
;
1056 bfd_byte
*extdyn
, *extdynend
;
1058 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1060 fprintf (f
, _("\nDynamic Section:\n"));
1062 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1065 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1068 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1070 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1071 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1074 extdynend
= extdyn
+ s
->size
;
1075 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1077 Elf_Internal_Dyn dyn
;
1080 bfd_boolean stringp
;
1082 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1084 if (dyn
.d_tag
== DT_NULL
)
1091 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1095 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1096 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1097 case DT_PLTGOT
: name
= "PLTGOT"; break;
1098 case DT_HASH
: name
= "HASH"; break;
1099 case DT_STRTAB
: name
= "STRTAB"; break;
1100 case DT_SYMTAB
: name
= "SYMTAB"; break;
1101 case DT_RELA
: name
= "RELA"; break;
1102 case DT_RELASZ
: name
= "RELASZ"; break;
1103 case DT_RELAENT
: name
= "RELAENT"; break;
1104 case DT_STRSZ
: name
= "STRSZ"; break;
1105 case DT_SYMENT
: name
= "SYMENT"; break;
1106 case DT_INIT
: name
= "INIT"; break;
1107 case DT_FINI
: name
= "FINI"; break;
1108 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1109 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1110 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1111 case DT_REL
: name
= "REL"; break;
1112 case DT_RELSZ
: name
= "RELSZ"; break;
1113 case DT_RELENT
: name
= "RELENT"; break;
1114 case DT_PLTREL
: name
= "PLTREL"; break;
1115 case DT_DEBUG
: name
= "DEBUG"; break;
1116 case DT_TEXTREL
: name
= "TEXTREL"; break;
1117 case DT_JMPREL
: name
= "JMPREL"; break;
1118 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1119 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1120 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1121 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1122 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1123 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1124 case DT_FLAGS
: name
= "FLAGS"; break;
1125 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1126 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1127 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1128 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1129 case DT_MOVEENT
: name
= "MOVEENT"; break;
1130 case DT_MOVESZ
: name
= "MOVESZ"; break;
1131 case DT_FEATURE
: name
= "FEATURE"; break;
1132 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1133 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1134 case DT_SYMINENT
: name
= "SYMINENT"; break;
1135 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1136 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1137 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1138 case DT_PLTPAD
: name
= "PLTPAD"; break;
1139 case DT_MOVETAB
: name
= "MOVETAB"; break;
1140 case DT_SYMINFO
: name
= "SYMINFO"; break;
1141 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1142 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1143 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1144 case DT_VERSYM
: name
= "VERSYM"; break;
1145 case DT_VERDEF
: name
= "VERDEF"; break;
1146 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1147 case DT_VERNEED
: name
= "VERNEED"; break;
1148 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1149 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1150 case DT_USED
: name
= "USED"; break;
1151 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1154 fprintf (f
, " %-11s ", name
);
1156 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1160 unsigned int tagv
= dyn
.d_un
.d_val
;
1162 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1165 fprintf (f
, "%s", string
);
1174 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1175 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1177 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1181 if (elf_dynverdef (abfd
) != 0)
1183 Elf_Internal_Verdef
*t
;
1185 fprintf (f
, _("\nVersion definitions:\n"));
1186 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1188 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1189 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1190 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1192 Elf_Internal_Verdaux
*a
;
1195 for (a
= t
->vd_auxptr
->vda_nextptr
;
1198 fprintf (f
, "%s ", a
->vda_nodename
);
1204 if (elf_dynverref (abfd
) != 0)
1206 Elf_Internal_Verneed
*t
;
1208 fprintf (f
, _("\nVersion References:\n"));
1209 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1211 Elf_Internal_Vernaux
*a
;
1213 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1214 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1215 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1216 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1228 /* Display ELF-specific fields of a symbol. */
1231 bfd_elf_print_symbol (bfd
*abfd
,
1234 bfd_print_symbol_type how
)
1239 case bfd_print_symbol_name
:
1240 fprintf (file
, "%s", symbol
->name
);
1242 case bfd_print_symbol_more
:
1243 fprintf (file
, "elf ");
1244 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1245 fprintf (file
, " %lx", (long) symbol
->flags
);
1247 case bfd_print_symbol_all
:
1249 const char *section_name
;
1250 const char *name
= NULL
;
1251 const struct elf_backend_data
*bed
;
1252 unsigned char st_other
;
1255 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1257 bed
= get_elf_backend_data (abfd
);
1258 if (bed
->elf_backend_print_symbol_all
)
1259 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1263 name
= symbol
->name
;
1264 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1267 fprintf (file
, " %s\t", section_name
);
1268 /* Print the "other" value for a symbol. For common symbols,
1269 we've already printed the size; now print the alignment.
1270 For other symbols, we have no specified alignment, and
1271 we've printed the address; now print the size. */
1272 if (bfd_is_com_section (symbol
->section
))
1273 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1275 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1276 bfd_fprintf_vma (abfd
, file
, val
);
1278 /* If we have version information, print it. */
1279 if (elf_tdata (abfd
)->dynversym_section
!= 0
1280 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1281 || elf_tdata (abfd
)->dynverref_section
!= 0))
1283 unsigned int vernum
;
1284 const char *version_string
;
1286 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1289 version_string
= "";
1290 else if (vernum
== 1)
1291 version_string
= "Base";
1292 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1294 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1297 Elf_Internal_Verneed
*t
;
1299 version_string
= "";
1300 for (t
= elf_tdata (abfd
)->verref
;
1304 Elf_Internal_Vernaux
*a
;
1306 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1308 if (a
->vna_other
== vernum
)
1310 version_string
= a
->vna_nodename
;
1317 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1318 fprintf (file
, " %-11s", version_string
);
1323 fprintf (file
, " (%s)", version_string
);
1324 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1329 /* If the st_other field is not zero, print it. */
1330 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1335 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1336 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1337 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1339 /* Some other non-defined flags are also present, so print
1341 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1344 fprintf (file
, " %s", name
);
1350 /* Create an entry in an ELF linker hash table. */
1352 struct bfd_hash_entry
*
1353 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
1354 struct bfd_hash_table
*table
,
1357 /* Allocate the structure if it has not already been allocated by a
1361 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1366 /* Call the allocation method of the superclass. */
1367 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1370 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1371 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1373 /* Set local fields. */
1376 ret
->got
= ret
->plt
= htab
->init_refcount
;
1377 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
1378 - offsetof (struct elf_link_hash_entry
, size
)));
1379 /* Assume that we have been called by a non-ELF symbol reader.
1380 This flag is then reset by the code which reads an ELF input
1381 file. This ensures that a symbol created by a non-ELF symbol
1382 reader will have the flag set correctly. */
1389 /* Copy data from an indirect symbol to its direct symbol, hiding the
1390 old indirect symbol. Also used for copying flags to a weakdef. */
1393 _bfd_elf_link_hash_copy_indirect (const struct elf_backend_data
*bed
,
1394 struct elf_link_hash_entry
*dir
,
1395 struct elf_link_hash_entry
*ind
)
1398 bfd_signed_vma lowest_valid
= bed
->can_refcount
;
1400 /* Copy down any references that we may have already seen to the
1401 symbol which just became indirect. */
1403 dir
->ref_dynamic
|= ind
->ref_dynamic
;
1404 dir
->ref_regular
|= ind
->ref_regular
;
1405 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
1406 dir
->non_got_ref
|= ind
->non_got_ref
;
1407 dir
->needs_plt
|= ind
->needs_plt
;
1408 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
1410 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1413 /* Copy over the global and procedure linkage table refcount entries.
1414 These may have been already set up by a check_relocs routine. */
1415 tmp
= dir
->got
.refcount
;
1416 if (tmp
< lowest_valid
)
1418 dir
->got
.refcount
= ind
->got
.refcount
;
1419 ind
->got
.refcount
= tmp
;
1422 BFD_ASSERT (ind
->got
.refcount
< lowest_valid
);
1424 tmp
= dir
->plt
.refcount
;
1425 if (tmp
< lowest_valid
)
1427 dir
->plt
.refcount
= ind
->plt
.refcount
;
1428 ind
->plt
.refcount
= tmp
;
1431 BFD_ASSERT (ind
->plt
.refcount
< lowest_valid
);
1433 if (dir
->dynindx
== -1)
1435 dir
->dynindx
= ind
->dynindx
;
1436 dir
->dynstr_index
= ind
->dynstr_index
;
1438 ind
->dynstr_index
= 0;
1441 BFD_ASSERT (ind
->dynindx
== -1);
1445 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
1446 struct elf_link_hash_entry
*h
,
1447 bfd_boolean force_local
)
1449 h
->plt
= elf_hash_table (info
)->init_offset
;
1453 h
->forced_local
= 1;
1454 if (h
->dynindx
!= -1)
1457 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
1463 /* Initialize an ELF linker hash table. */
1466 _bfd_elf_link_hash_table_init
1467 (struct elf_link_hash_table
*table
,
1469 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
1470 struct bfd_hash_table
*,
1475 table
->dynamic_sections_created
= FALSE
;
1476 table
->dynobj
= NULL
;
1477 /* Make sure can_refcount is extended to the width and signedness of
1478 init_refcount before we subtract one from it. */
1479 table
->init_refcount
.refcount
= get_elf_backend_data (abfd
)->can_refcount
;
1480 table
->init_refcount
.refcount
-= 1;
1481 table
->init_offset
.offset
= -(bfd_vma
) 1;
1482 /* The first dynamic symbol is a dummy. */
1483 table
->dynsymcount
= 1;
1484 table
->dynstr
= NULL
;
1485 table
->bucketcount
= 0;
1486 table
->needed
= NULL
;
1488 table
->merge_info
= NULL
;
1489 memset (&table
->stab_info
, 0, sizeof (table
->stab_info
));
1490 memset (&table
->eh_info
, 0, sizeof (table
->eh_info
));
1491 table
->dynlocal
= NULL
;
1492 table
->runpath
= NULL
;
1493 table
->tls_sec
= NULL
;
1494 table
->tls_size
= 0;
1495 table
->loaded
= NULL
;
1497 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
1498 table
->root
.type
= bfd_link_elf_hash_table
;
1503 /* Create an ELF linker hash table. */
1505 struct bfd_link_hash_table
*
1506 _bfd_elf_link_hash_table_create (bfd
*abfd
)
1508 struct elf_link_hash_table
*ret
;
1509 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1511 ret
= bfd_malloc (amt
);
1515 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1524 /* This is a hook for the ELF emulation code in the generic linker to
1525 tell the backend linker what file name to use for the DT_NEEDED
1526 entry for a dynamic object. */
1529 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
1531 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1532 && bfd_get_format (abfd
) == bfd_object
)
1533 elf_dt_name (abfd
) = name
;
1537 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
1540 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1541 && bfd_get_format (abfd
) == bfd_object
)
1542 lib_class
= elf_dyn_lib_class (abfd
);
1549 bfd_elf_set_dyn_lib_class (bfd
*abfd
, int lib_class
)
1551 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1552 && bfd_get_format (abfd
) == bfd_object
)
1553 elf_dyn_lib_class (abfd
) = lib_class
;
1556 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1557 the linker ELF emulation code. */
1559 struct bfd_link_needed_list
*
1560 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
1561 struct bfd_link_info
*info
)
1563 if (! is_elf_hash_table (info
->hash
))
1565 return elf_hash_table (info
)->needed
;
1568 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1569 hook for the linker ELF emulation code. */
1571 struct bfd_link_needed_list
*
1572 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
1573 struct bfd_link_info
*info
)
1575 if (! is_elf_hash_table (info
->hash
))
1577 return elf_hash_table (info
)->runpath
;
1580 /* Get the name actually used for a dynamic object for a link. This
1581 is the SONAME entry if there is one. Otherwise, it is the string
1582 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1585 bfd_elf_get_dt_soname (bfd
*abfd
)
1587 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1588 && bfd_get_format (abfd
) == bfd_object
)
1589 return elf_dt_name (abfd
);
1593 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1594 the ELF linker emulation code. */
1597 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
1598 struct bfd_link_needed_list
**pneeded
)
1601 bfd_byte
*dynbuf
= NULL
;
1603 unsigned long shlink
;
1604 bfd_byte
*extdyn
, *extdynend
;
1606 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1610 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1611 || bfd_get_format (abfd
) != bfd_object
)
1614 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1615 if (s
== NULL
|| s
->size
== 0)
1618 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1621 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1625 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1627 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1628 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1631 extdynend
= extdyn
+ s
->size
;
1632 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1634 Elf_Internal_Dyn dyn
;
1636 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1638 if (dyn
.d_tag
== DT_NULL
)
1641 if (dyn
.d_tag
== DT_NEEDED
)
1644 struct bfd_link_needed_list
*l
;
1645 unsigned int tagv
= dyn
.d_un
.d_val
;
1648 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1653 l
= bfd_alloc (abfd
, amt
);
1674 /* Allocate an ELF string table--force the first byte to be zero. */
1676 struct bfd_strtab_hash
*
1677 _bfd_elf_stringtab_init (void)
1679 struct bfd_strtab_hash
*ret
;
1681 ret
= _bfd_stringtab_init ();
1686 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1687 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1688 if (loc
== (bfd_size_type
) -1)
1690 _bfd_stringtab_free (ret
);
1697 /* ELF .o/exec file reading */
1699 /* Create a new bfd section from an ELF section header. */
1702 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1704 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1705 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1706 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1709 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1711 switch (hdr
->sh_type
)
1714 /* Inactive section. Throw it away. */
1717 case SHT_PROGBITS
: /* Normal section with contents. */
1718 case SHT_NOBITS
: /* .bss section. */
1719 case SHT_HASH
: /* .hash section. */
1720 case SHT_NOTE
: /* .note section. */
1721 case SHT_INIT_ARRAY
: /* .init_array section. */
1722 case SHT_FINI_ARRAY
: /* .fini_array section. */
1723 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1724 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1725 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1727 case SHT_DYNAMIC
: /* Dynamic linking information. */
1728 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1730 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1732 Elf_Internal_Shdr
*dynsymhdr
;
1734 /* The shared libraries distributed with hpux11 have a bogus
1735 sh_link field for the ".dynamic" section. Find the
1736 string table for the ".dynsym" section instead. */
1737 if (elf_dynsymtab (abfd
) != 0)
1739 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1740 hdr
->sh_link
= dynsymhdr
->sh_link
;
1744 unsigned int i
, num_sec
;
1746 num_sec
= elf_numsections (abfd
);
1747 for (i
= 1; i
< num_sec
; i
++)
1749 dynsymhdr
= elf_elfsections (abfd
)[i
];
1750 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1752 hdr
->sh_link
= dynsymhdr
->sh_link
;
1760 case SHT_SYMTAB
: /* A symbol table */
1761 if (elf_onesymtab (abfd
) == shindex
)
1764 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1765 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1766 elf_onesymtab (abfd
) = shindex
;
1767 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1768 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1769 abfd
->flags
|= HAS_SYMS
;
1771 /* Sometimes a shared object will map in the symbol table. If
1772 SHF_ALLOC is set, and this is a shared object, then we also
1773 treat this section as a BFD section. We can not base the
1774 decision purely on SHF_ALLOC, because that flag is sometimes
1775 set in a relocatable object file, which would confuse the
1777 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1778 && (abfd
->flags
& DYNAMIC
) != 0
1779 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1784 case SHT_DYNSYM
: /* A dynamic symbol table */
1785 if (elf_dynsymtab (abfd
) == shindex
)
1788 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1789 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1790 elf_dynsymtab (abfd
) = shindex
;
1791 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1792 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1793 abfd
->flags
|= HAS_SYMS
;
1795 /* Besides being a symbol table, we also treat this as a regular
1796 section, so that objcopy can handle it. */
1797 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1799 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1800 if (elf_symtab_shndx (abfd
) == shindex
)
1803 /* Get the associated symbol table. */
1804 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1805 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1808 elf_symtab_shndx (abfd
) = shindex
;
1809 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1810 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1813 case SHT_STRTAB
: /* A string table */
1814 if (hdr
->bfd_section
!= NULL
)
1816 if (ehdr
->e_shstrndx
== shindex
)
1818 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1819 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1823 unsigned int i
, num_sec
;
1825 num_sec
= elf_numsections (abfd
);
1826 for (i
= 1; i
< num_sec
; i
++)
1828 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1829 if (hdr2
->sh_link
== shindex
)
1831 if (! bfd_section_from_shdr (abfd
, i
))
1833 if (elf_onesymtab (abfd
) == i
)
1835 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1836 elf_elfsections (abfd
)[shindex
] =
1837 &elf_tdata (abfd
)->strtab_hdr
;
1840 if (elf_dynsymtab (abfd
) == i
)
1842 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1843 elf_elfsections (abfd
)[shindex
] = hdr
=
1844 &elf_tdata (abfd
)->dynstrtab_hdr
;
1845 /* We also treat this as a regular section, so
1846 that objcopy can handle it. */
1853 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1857 /* *These* do a lot of work -- but build no sections! */
1859 asection
*target_sect
;
1860 Elf_Internal_Shdr
*hdr2
;
1861 unsigned int num_sec
= elf_numsections (abfd
);
1863 /* Check for a bogus link to avoid crashing. */
1864 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1865 || hdr
->sh_link
>= num_sec
)
1867 ((*_bfd_error_handler
)
1868 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1869 abfd
, hdr
->sh_link
, name
, shindex
));
1870 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1873 /* For some incomprehensible reason Oracle distributes
1874 libraries for Solaris in which some of the objects have
1875 bogus sh_link fields. It would be nice if we could just
1876 reject them, but, unfortunately, some people need to use
1877 them. We scan through the section headers; if we find only
1878 one suitable symbol table, we clobber the sh_link to point
1879 to it. I hope this doesn't break anything. */
1880 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1881 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1887 for (scan
= 1; scan
< num_sec
; scan
++)
1889 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1890 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1901 hdr
->sh_link
= found
;
1904 /* Get the symbol table. */
1905 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1906 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1909 /* If this reloc section does not use the main symbol table we
1910 don't treat it as a reloc section. BFD can't adequately
1911 represent such a section, so at least for now, we don't
1912 try. We just present it as a normal section. We also
1913 can't use it as a reloc section if it points to the null
1915 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1916 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1918 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1920 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1921 if (target_sect
== NULL
)
1924 if ((target_sect
->flags
& SEC_RELOC
) == 0
1925 || target_sect
->reloc_count
== 0)
1926 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1930 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1931 amt
= sizeof (*hdr2
);
1932 hdr2
= bfd_alloc (abfd
, amt
);
1933 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1936 elf_elfsections (abfd
)[shindex
] = hdr2
;
1937 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1938 target_sect
->flags
|= SEC_RELOC
;
1939 target_sect
->relocation
= NULL
;
1940 target_sect
->rel_filepos
= hdr
->sh_offset
;
1941 /* In the section to which the relocations apply, mark whether
1942 its relocations are of the REL or RELA variety. */
1943 if (hdr
->sh_size
!= 0)
1944 target_sect
->use_rela_p
= hdr
->sh_type
== SHT_RELA
;
1945 abfd
->flags
|= HAS_RELOC
;
1950 case SHT_GNU_verdef
:
1951 elf_dynverdef (abfd
) = shindex
;
1952 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1953 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1956 case SHT_GNU_versym
:
1957 elf_dynversym (abfd
) = shindex
;
1958 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1959 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1962 case SHT_GNU_verneed
:
1963 elf_dynverref (abfd
) = shindex
;
1964 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1965 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1972 /* We need a BFD section for objcopy and relocatable linking,
1973 and it's handy to have the signature available as the section
1975 name
= group_signature (abfd
, hdr
);
1978 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1980 if (hdr
->contents
!= NULL
)
1982 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1983 unsigned int n_elt
= hdr
->sh_size
/ 4;
1986 if (idx
->flags
& GRP_COMDAT
)
1987 hdr
->bfd_section
->flags
1988 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1990 /* We try to keep the same section order as it comes in. */
1992 while (--n_elt
!= 0)
1993 if ((s
= (--idx
)->shdr
->bfd_section
) != NULL
1994 && elf_next_in_group (s
) != NULL
)
1996 elf_next_in_group (hdr
->bfd_section
) = s
;
2003 /* Check for any processor-specific section types. */
2005 if (bed
->elf_backend_section_from_shdr
)
2006 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
2014 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2015 Return SEC for sections that have no elf section, and NULL on error. */
2018 bfd_section_from_r_symndx (bfd
*abfd
,
2019 struct sym_sec_cache
*cache
,
2021 unsigned long r_symndx
)
2023 Elf_Internal_Shdr
*symtab_hdr
;
2024 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2025 Elf_External_Sym_Shndx eshndx
;
2026 Elf_Internal_Sym isym
;
2027 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2029 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
2030 return cache
->sec
[ent
];
2032 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2033 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2034 &isym
, esym
, &eshndx
) == NULL
)
2037 if (cache
->abfd
!= abfd
)
2039 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2042 cache
->indx
[ent
] = r_symndx
;
2043 cache
->sec
[ent
] = sec
;
2044 if ((isym
.st_shndx
!= SHN_UNDEF
&& isym
.st_shndx
< SHN_LORESERVE
)
2045 || isym
.st_shndx
> SHN_HIRESERVE
)
2048 s
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
2050 cache
->sec
[ent
] = s
;
2052 return cache
->sec
[ent
];
2055 /* Given an ELF section number, retrieve the corresponding BFD
2059 bfd_section_from_elf_index (bfd
*abfd
, unsigned int index
)
2061 if (index
>= elf_numsections (abfd
))
2063 return elf_elfsections (abfd
)[index
]->bfd_section
;
2066 static struct bfd_elf_special_section
const special_sections
[] =
2068 { ".bss", 4, -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2069 { ".gnu.linkonce.b",15, -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2070 { ".comment", 8, 0, SHT_PROGBITS
, 0 },
2071 { ".data", 5, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2072 { ".data1", 6, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2073 { ".debug", 6, 0, SHT_PROGBITS
, 0 },
2074 { ".fini", 5, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2075 { ".init", 5, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2076 { ".line", 5, 0, SHT_PROGBITS
, 0 },
2077 { ".rodata", 7, -2, SHT_PROGBITS
, SHF_ALLOC
},
2078 { ".rodata1", 8, 0, SHT_PROGBITS
, SHF_ALLOC
},
2079 { ".tbss", 5, -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2080 { ".tdata", 6, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2081 { ".text", 5, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2082 { ".init_array", 11, 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2083 { ".fini_array", 11, 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2084 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2085 { ".debug_line", 11, 0, SHT_PROGBITS
, 0 },
2086 { ".debug_info", 11, 0, SHT_PROGBITS
, 0 },
2087 { ".debug_abbrev", 13, 0, SHT_PROGBITS
, 0 },
2088 { ".debug_aranges", 14, 0, SHT_PROGBITS
, 0 },
2089 { ".dynamic", 8, 0, SHT_DYNAMIC
, SHF_ALLOC
},
2090 { ".dynstr", 7, 0, SHT_STRTAB
, SHF_ALLOC
},
2091 { ".dynsym", 7, 0, SHT_DYNSYM
, SHF_ALLOC
},
2092 { ".got", 4, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2093 { ".hash", 5, 0, SHT_HASH
, SHF_ALLOC
},
2094 { ".interp", 7, 0, SHT_PROGBITS
, 0 },
2095 { ".plt", 4, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2096 { ".shstrtab", 9, 0, SHT_STRTAB
, 0 },
2097 { ".strtab", 7, 0, SHT_STRTAB
, 0 },
2098 { ".symtab", 7, 0, SHT_SYMTAB
, 0 },
2099 { ".gnu.version", 12, 0, SHT_GNU_versym
, 0 },
2100 { ".gnu.version_d", 14, 0, SHT_GNU_verdef
, 0 },
2101 { ".gnu.version_r", 14, 0, SHT_GNU_verneed
, 0 },
2102 { ".note.GNU-stack",15, 0, SHT_PROGBITS
, 0 },
2103 { ".note", 5, -1, SHT_NOTE
, 0 },
2104 { ".rela", 5, -1, SHT_RELA
, 0 },
2105 { ".rel", 4, -1, SHT_REL
, 0 },
2106 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2107 { ".gnu.liblist", 12, 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2108 { ".gnu.conflict", 13, 0, SHT_RELA
, SHF_ALLOC
},
2109 { NULL
, 0, 0, 0, 0 }
2112 static const struct bfd_elf_special_section
*
2113 get_special_section (const char *name
,
2114 const struct bfd_elf_special_section
*special_sections
,
2118 int len
= strlen (name
);
2120 for (i
= 0; special_sections
[i
].prefix
!= NULL
; i
++)
2123 int prefix_len
= special_sections
[i
].prefix_length
;
2125 if (len
< prefix_len
)
2127 if (memcmp (name
, special_sections
[i
].prefix
, prefix_len
) != 0)
2130 suffix_len
= special_sections
[i
].suffix_length
;
2131 if (suffix_len
<= 0)
2133 if (name
[prefix_len
] != 0)
2135 if (suffix_len
== 0)
2137 if (name
[prefix_len
] != '.'
2138 && (suffix_len
== -2
2139 || (rela
&& special_sections
[i
].type
== SHT_REL
)))
2145 if (len
< prefix_len
+ suffix_len
)
2147 if (memcmp (name
+ len
- suffix_len
,
2148 special_sections
[i
].prefix
+ prefix_len
,
2152 return &special_sections
[i
];
2158 const struct bfd_elf_special_section
*
2159 _bfd_elf_get_sec_type_attr (bfd
*abfd
, const char *name
)
2161 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2162 const struct bfd_elf_special_section
*ssect
= NULL
;
2164 /* See if this is one of the special sections. */
2167 unsigned int rela
= bed
->default_use_rela_p
;
2169 if (bed
->special_sections
)
2170 ssect
= get_special_section (name
, bed
->special_sections
, rela
);
2173 ssect
= get_special_section (name
, special_sections
, rela
);
2180 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2182 struct bfd_elf_section_data
*sdata
;
2183 const struct bfd_elf_special_section
*ssect
;
2185 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2188 sdata
= bfd_zalloc (abfd
, sizeof (*sdata
));
2191 sec
->used_by_bfd
= sdata
;
2194 elf_section_type (sec
) = SHT_NULL
;
2195 ssect
= _bfd_elf_get_sec_type_attr (abfd
, sec
->name
);
2198 elf_section_type (sec
) = ssect
->type
;
2199 elf_section_flags (sec
) = ssect
->attr
;
2202 /* Indicate whether or not this section should use RELA relocations. */
2203 sec
->use_rela_p
= get_elf_backend_data (abfd
)->default_use_rela_p
;
2208 /* Create a new bfd section from an ELF program header.
2210 Since program segments have no names, we generate a synthetic name
2211 of the form segment<NUM>, where NUM is generally the index in the
2212 program header table. For segments that are split (see below) we
2213 generate the names segment<NUM>a and segment<NUM>b.
2215 Note that some program segments may have a file size that is different than
2216 (less than) the memory size. All this means is that at execution the
2217 system must allocate the amount of memory specified by the memory size,
2218 but only initialize it with the first "file size" bytes read from the
2219 file. This would occur for example, with program segments consisting
2220 of combined data+bss.
2222 To handle the above situation, this routine generates TWO bfd sections
2223 for the single program segment. The first has the length specified by
2224 the file size of the segment, and the second has the length specified
2225 by the difference between the two sizes. In effect, the segment is split
2226 into it's initialized and uninitialized parts.
2231 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2232 Elf_Internal_Phdr
*hdr
,
2234 const char *typename
)
2242 split
= ((hdr
->p_memsz
> 0)
2243 && (hdr
->p_filesz
> 0)
2244 && (hdr
->p_memsz
> hdr
->p_filesz
));
2245 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2246 len
= strlen (namebuf
) + 1;
2247 name
= bfd_alloc (abfd
, len
);
2250 memcpy (name
, namebuf
, len
);
2251 newsect
= bfd_make_section (abfd
, name
);
2252 if (newsect
== NULL
)
2254 newsect
->vma
= hdr
->p_vaddr
;
2255 newsect
->lma
= hdr
->p_paddr
;
2256 newsect
->size
= hdr
->p_filesz
;
2257 newsect
->filepos
= hdr
->p_offset
;
2258 newsect
->flags
|= SEC_HAS_CONTENTS
;
2259 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2260 if (hdr
->p_type
== PT_LOAD
)
2262 newsect
->flags
|= SEC_ALLOC
;
2263 newsect
->flags
|= SEC_LOAD
;
2264 if (hdr
->p_flags
& PF_X
)
2266 /* FIXME: all we known is that it has execute PERMISSION,
2268 newsect
->flags
|= SEC_CODE
;
2271 if (!(hdr
->p_flags
& PF_W
))
2273 newsect
->flags
|= SEC_READONLY
;
2278 sprintf (namebuf
, "%s%db", typename
, index
);
2279 len
= strlen (namebuf
) + 1;
2280 name
= bfd_alloc (abfd
, len
);
2283 memcpy (name
, namebuf
, len
);
2284 newsect
= bfd_make_section (abfd
, name
);
2285 if (newsect
== NULL
)
2287 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2288 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2289 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2290 if (hdr
->p_type
== PT_LOAD
)
2292 newsect
->flags
|= SEC_ALLOC
;
2293 if (hdr
->p_flags
& PF_X
)
2294 newsect
->flags
|= SEC_CODE
;
2296 if (!(hdr
->p_flags
& PF_W
))
2297 newsect
->flags
|= SEC_READONLY
;
2304 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int index
)
2306 const struct elf_backend_data
*bed
;
2308 switch (hdr
->p_type
)
2311 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2314 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2317 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2320 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2323 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2325 if (! elfcore_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2330 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2333 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2335 case PT_GNU_EH_FRAME
:
2336 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
,
2340 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "stack");
2343 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "relro");
2346 /* Check for any processor-specific program segment types.
2347 If no handler for them, default to making "segment" sections. */
2348 bed
= get_elf_backend_data (abfd
);
2349 if (bed
->elf_backend_section_from_phdr
)
2350 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2352 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2356 /* Initialize REL_HDR, the section-header for new section, containing
2357 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2358 relocations; otherwise, we use REL relocations. */
2361 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2362 Elf_Internal_Shdr
*rel_hdr
,
2364 bfd_boolean use_rela_p
)
2367 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2368 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2370 name
= bfd_alloc (abfd
, amt
);
2373 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2375 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2377 if (rel_hdr
->sh_name
== (unsigned int) -1)
2379 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2380 rel_hdr
->sh_entsize
= (use_rela_p
2381 ? bed
->s
->sizeof_rela
2382 : bed
->s
->sizeof_rel
);
2383 rel_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
2384 rel_hdr
->sh_flags
= 0;
2385 rel_hdr
->sh_addr
= 0;
2386 rel_hdr
->sh_size
= 0;
2387 rel_hdr
->sh_offset
= 0;
2392 /* Set up an ELF internal section header for a section. */
2395 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *failedptrarg
)
2397 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2398 bfd_boolean
*failedptr
= failedptrarg
;
2399 Elf_Internal_Shdr
*this_hdr
;
2403 /* We already failed; just get out of the bfd_map_over_sections
2408 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2410 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2411 asect
->name
, FALSE
);
2412 if (this_hdr
->sh_name
== (unsigned int) -1)
2418 this_hdr
->sh_flags
= 0;
2420 if ((asect
->flags
& SEC_ALLOC
) != 0
2421 || asect
->user_set_vma
)
2422 this_hdr
->sh_addr
= asect
->vma
;
2424 this_hdr
->sh_addr
= 0;
2426 this_hdr
->sh_offset
= 0;
2427 this_hdr
->sh_size
= asect
->size
;
2428 this_hdr
->sh_link
= 0;
2429 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2430 /* The sh_entsize and sh_info fields may have been set already by
2431 copy_private_section_data. */
2433 this_hdr
->bfd_section
= asect
;
2434 this_hdr
->contents
= NULL
;
2436 /* If the section type is unspecified, we set it based on
2438 if (this_hdr
->sh_type
== SHT_NULL
)
2440 if ((asect
->flags
& SEC_GROUP
) != 0)
2442 /* We also need to mark SHF_GROUP here for relocatable
2444 struct bfd_link_order
*l
;
2447 for (l
= asect
->link_order_head
; l
!= NULL
; l
= l
->next
)
2448 if (l
->type
== bfd_indirect_link_order
2449 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2452 /* The name is not important. Anything will do. */
2453 elf_group_name (elt
->output_section
) = "G";
2454 elf_section_flags (elt
->output_section
) |= SHF_GROUP
;
2456 elt
= elf_next_in_group (elt
);
2457 /* During a relocatable link, the lists are
2460 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2462 this_hdr
->sh_type
= SHT_GROUP
;
2464 else if ((asect
->flags
& SEC_ALLOC
) != 0
2465 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2466 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2467 this_hdr
->sh_type
= SHT_NOBITS
;
2469 this_hdr
->sh_type
= SHT_PROGBITS
;
2472 switch (this_hdr
->sh_type
)
2478 case SHT_INIT_ARRAY
:
2479 case SHT_FINI_ARRAY
:
2480 case SHT_PREINIT_ARRAY
:
2487 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2491 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2495 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2499 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2500 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2504 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2505 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2508 case SHT_GNU_versym
:
2509 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2512 case SHT_GNU_verdef
:
2513 this_hdr
->sh_entsize
= 0;
2514 /* objcopy or strip will copy over sh_info, but may not set
2515 cverdefs. The linker will set cverdefs, but sh_info will be
2517 if (this_hdr
->sh_info
== 0)
2518 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2520 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2521 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2524 case SHT_GNU_verneed
:
2525 this_hdr
->sh_entsize
= 0;
2526 /* objcopy or strip will copy over sh_info, but may not set
2527 cverrefs. The linker will set cverrefs, but sh_info will be
2529 if (this_hdr
->sh_info
== 0)
2530 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2532 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2533 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2537 this_hdr
->sh_entsize
= 4;
2541 if ((asect
->flags
& SEC_ALLOC
) != 0)
2542 this_hdr
->sh_flags
|= SHF_ALLOC
;
2543 if ((asect
->flags
& SEC_READONLY
) == 0)
2544 this_hdr
->sh_flags
|= SHF_WRITE
;
2545 if ((asect
->flags
& SEC_CODE
) != 0)
2546 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2547 if ((asect
->flags
& SEC_MERGE
) != 0)
2549 this_hdr
->sh_flags
|= SHF_MERGE
;
2550 this_hdr
->sh_entsize
= asect
->entsize
;
2551 if ((asect
->flags
& SEC_STRINGS
) != 0)
2552 this_hdr
->sh_flags
|= SHF_STRINGS
;
2554 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2555 this_hdr
->sh_flags
|= SHF_GROUP
;
2556 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2558 this_hdr
->sh_flags
|= SHF_TLS
;
2559 if (asect
->size
== 0 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2561 struct bfd_link_order
*o
;
2563 this_hdr
->sh_size
= 0;
2564 for (o
= asect
->link_order_head
; o
!= NULL
; o
= o
->next
)
2565 if (this_hdr
->sh_size
< o
->offset
+ o
->size
)
2566 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2567 if (this_hdr
->sh_size
)
2568 this_hdr
->sh_type
= SHT_NOBITS
;
2572 /* Check for processor-specific section types. */
2573 if (bed
->elf_backend_fake_sections
2574 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2577 /* If the section has relocs, set up a section header for the
2578 SHT_REL[A] section. If two relocation sections are required for
2579 this section, it is up to the processor-specific back-end to
2580 create the other. */
2581 if ((asect
->flags
& SEC_RELOC
) != 0
2582 && !_bfd_elf_init_reloc_shdr (abfd
,
2583 &elf_section_data (asect
)->rel_hdr
,
2589 /* Fill in the contents of a SHT_GROUP section. */
2592 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2594 bfd_boolean
*failedptr
= failedptrarg
;
2595 unsigned long symindx
;
2596 asection
*elt
, *first
;
2598 struct bfd_link_order
*l
;
2601 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2606 if (elf_group_id (sec
) != NULL
)
2607 symindx
= elf_group_id (sec
)->udata
.i
;
2611 /* If called from the assembler, swap_out_syms will have set up
2612 elf_section_syms; If called for "ld -r", use target_index. */
2613 if (elf_section_syms (abfd
) != NULL
)
2614 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2616 symindx
= sec
->target_index
;
2618 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2620 /* The contents won't be allocated for "ld -r" or objcopy. */
2622 if (sec
->contents
== NULL
)
2625 sec
->contents
= bfd_alloc (abfd
, sec
->size
);
2627 /* Arrange for the section to be written out. */
2628 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2629 if (sec
->contents
== NULL
)
2636 loc
= sec
->contents
+ sec
->size
;
2638 /* Get the pointer to the first section in the group that gas
2639 squirreled away here. objcopy arranges for this to be set to the
2640 start of the input section group. */
2641 first
= elt
= elf_next_in_group (sec
);
2643 /* First element is a flag word. Rest of section is elf section
2644 indices for all the sections of the group. Write them backwards
2645 just to keep the group in the same order as given in .section
2646 directives, not that it matters. */
2655 s
= s
->output_section
;
2658 idx
= elf_section_data (s
)->this_idx
;
2659 H_PUT_32 (abfd
, idx
, loc
);
2660 elt
= elf_next_in_group (elt
);
2665 /* If this is a relocatable link, then the above did nothing because
2666 SEC is the output section. Look through the input sections
2668 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2669 if (l
->type
== bfd_indirect_link_order
2670 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2675 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2676 elt
= elf_next_in_group (elt
);
2677 /* During a relocatable link, the lists are circular. */
2679 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2681 if ((loc
-= 4) != sec
->contents
)
2684 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2687 /* Assign all ELF section numbers. The dummy first section is handled here
2688 too. The link/info pointers for the standard section types are filled
2689 in here too, while we're at it. */
2692 assign_section_numbers (bfd
*abfd
)
2694 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2696 unsigned int section_number
, secn
;
2697 Elf_Internal_Shdr
**i_shdrp
;
2699 struct bfd_elf_section_data
*d
;
2703 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2705 /* Put SHT_GROUP sections first. */
2706 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2708 d
= elf_section_data (sec
);
2710 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2712 if (section_number
== SHN_LORESERVE
)
2713 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2714 d
->this_idx
= section_number
++;
2718 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2720 d
= elf_section_data (sec
);
2722 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2724 if (section_number
== SHN_LORESERVE
)
2725 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2726 d
->this_idx
= section_number
++;
2728 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2729 if ((sec
->flags
& SEC_RELOC
) == 0)
2733 if (section_number
== SHN_LORESERVE
)
2734 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2735 d
->rel_idx
= section_number
++;
2736 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2741 if (section_number
== SHN_LORESERVE
)
2742 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2743 d
->rel_idx2
= section_number
++;
2744 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2750 if (section_number
== SHN_LORESERVE
)
2751 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2752 t
->shstrtab_section
= section_number
++;
2753 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2754 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2756 if (bfd_get_symcount (abfd
) > 0)
2758 if (section_number
== SHN_LORESERVE
)
2759 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2760 t
->symtab_section
= section_number
++;
2761 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2762 if (section_number
> SHN_LORESERVE
- 2)
2764 if (section_number
== SHN_LORESERVE
)
2765 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2766 t
->symtab_shndx_section
= section_number
++;
2767 t
->symtab_shndx_hdr
.sh_name
2768 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2769 ".symtab_shndx", FALSE
);
2770 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2773 if (section_number
== SHN_LORESERVE
)
2774 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2775 t
->strtab_section
= section_number
++;
2776 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2779 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2780 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2782 elf_numsections (abfd
) = section_number
;
2783 elf_elfheader (abfd
)->e_shnum
= section_number
;
2784 if (section_number
> SHN_LORESERVE
)
2785 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2787 /* Set up the list of section header pointers, in agreement with the
2789 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2790 i_shdrp
= bfd_zalloc (abfd
, amt
);
2791 if (i_shdrp
== NULL
)
2794 amt
= sizeof (Elf_Internal_Shdr
);
2795 i_shdrp
[0] = bfd_zalloc (abfd
, amt
);
2796 if (i_shdrp
[0] == NULL
)
2798 bfd_release (abfd
, i_shdrp
);
2802 elf_elfsections (abfd
) = i_shdrp
;
2804 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2805 if (bfd_get_symcount (abfd
) > 0)
2807 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2808 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2810 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2811 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2813 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2814 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2817 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2819 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2823 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2824 if (d
->rel_idx
!= 0)
2825 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2826 if (d
->rel_idx2
!= 0)
2827 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2829 /* Fill in the sh_link and sh_info fields while we're at it. */
2831 /* sh_link of a reloc section is the section index of the symbol
2832 table. sh_info is the section index of the section to which
2833 the relocation entries apply. */
2834 if (d
->rel_idx
!= 0)
2836 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2837 d
->rel_hdr
.sh_info
= d
->this_idx
;
2839 if (d
->rel_idx2
!= 0)
2841 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2842 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2845 /* We need to set up sh_link for SHF_LINK_ORDER. */
2846 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
2848 s
= elf_linked_to_section (sec
);
2850 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2853 struct bfd_link_order
*p
;
2855 /* Find out what the corresponding section in output
2857 for (p
= sec
->link_order_head
; p
!= NULL
; p
= p
->next
)
2859 s
= p
->u
.indirect
.section
;
2860 if (p
->type
== bfd_indirect_link_order
2861 && (bfd_get_flavour (s
->owner
)
2862 == bfd_target_elf_flavour
))
2864 Elf_Internal_Shdr
** const elf_shdrp
2865 = elf_elfsections (s
->owner
);
2867 = _bfd_elf_section_from_bfd_section (s
->owner
, s
);
2868 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
2870 The Intel C compiler generates SHT_IA_64_UNWIND with
2871 SHF_LINK_ORDER. But it doesn't set theh sh_link or
2872 sh_info fields. Hence we could get the situation
2873 where elfsec is 0. */
2876 const struct elf_backend_data
*bed
2877 = get_elf_backend_data (abfd
);
2878 if (bed
->link_order_error_handler
)
2879 bed
->link_order_error_handler
2880 (_("%B: warning: sh_link not set for section `%S'"),
2885 s
= elf_shdrp
[elfsec
]->bfd_section
->output_section
;
2886 BFD_ASSERT (s
!= NULL
);
2887 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2895 switch (d
->this_hdr
.sh_type
)
2899 /* A reloc section which we are treating as a normal BFD
2900 section. sh_link is the section index of the symbol
2901 table. sh_info is the section index of the section to
2902 which the relocation entries apply. We assume that an
2903 allocated reloc section uses the dynamic symbol table.
2904 FIXME: How can we be sure? */
2905 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2907 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2909 /* We look up the section the relocs apply to by name. */
2911 if (d
->this_hdr
.sh_type
== SHT_REL
)
2915 s
= bfd_get_section_by_name (abfd
, name
);
2917 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2921 /* We assume that a section named .stab*str is a stabs
2922 string section. We look for a section with the same name
2923 but without the trailing ``str'', and set its sh_link
2924 field to point to this section. */
2925 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2926 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2931 len
= strlen (sec
->name
);
2932 alc
= bfd_malloc (len
- 2);
2935 memcpy (alc
, sec
->name
, len
- 3);
2936 alc
[len
- 3] = '\0';
2937 s
= bfd_get_section_by_name (abfd
, alc
);
2941 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2943 /* This is a .stab section. */
2944 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
2945 elf_section_data (s
)->this_hdr
.sh_entsize
2946 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2953 case SHT_GNU_verneed
:
2954 case SHT_GNU_verdef
:
2955 /* sh_link is the section header index of the string table
2956 used for the dynamic entries, or the symbol table, or the
2958 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2960 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2963 case SHT_GNU_LIBLIST
:
2964 /* sh_link is the section header index of the prelink library
2966 used for the dynamic entries, or the symbol table, or the
2968 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
2969 ? ".dynstr" : ".gnu.libstr");
2971 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2975 case SHT_GNU_versym
:
2976 /* sh_link is the section header index of the symbol table
2977 this hash table or version table is for. */
2978 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2980 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2984 d
->this_hdr
.sh_link
= t
->symtab_section
;
2988 for (secn
= 1; secn
< section_number
; ++secn
)
2989 if (i_shdrp
[secn
] == NULL
)
2990 i_shdrp
[secn
] = i_shdrp
[0];
2992 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2993 i_shdrp
[secn
]->sh_name
);
2997 /* Map symbol from it's internal number to the external number, moving
2998 all local symbols to be at the head of the list. */
3001 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3003 /* If the backend has a special mapping, use it. */
3004 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3005 if (bed
->elf_backend_sym_is_global
)
3006 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3008 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
3009 || bfd_is_und_section (bfd_get_section (sym
))
3010 || bfd_is_com_section (bfd_get_section (sym
)));
3014 elf_map_symbols (bfd
*abfd
)
3016 unsigned int symcount
= bfd_get_symcount (abfd
);
3017 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3018 asymbol
**sect_syms
;
3019 unsigned int num_locals
= 0;
3020 unsigned int num_globals
= 0;
3021 unsigned int num_locals2
= 0;
3022 unsigned int num_globals2
= 0;
3030 fprintf (stderr
, "elf_map_symbols\n");
3034 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3036 if (max_index
< asect
->index
)
3037 max_index
= asect
->index
;
3041 amt
= max_index
* sizeof (asymbol
*);
3042 sect_syms
= bfd_zalloc (abfd
, amt
);
3043 if (sect_syms
== NULL
)
3045 elf_section_syms (abfd
) = sect_syms
;
3046 elf_num_section_syms (abfd
) = max_index
;
3048 /* Init sect_syms entries for any section symbols we have already
3049 decided to output. */
3050 for (idx
= 0; idx
< symcount
; idx
++)
3052 asymbol
*sym
= syms
[idx
];
3054 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3061 if (sec
->owner
!= NULL
)
3063 if (sec
->owner
!= abfd
)
3065 if (sec
->output_offset
!= 0)
3068 sec
= sec
->output_section
;
3070 /* Empty sections in the input files may have had a
3071 section symbol created for them. (See the comment
3072 near the end of _bfd_generic_link_output_symbols in
3073 linker.c). If the linker script discards such
3074 sections then we will reach this point. Since we know
3075 that we cannot avoid this case, we detect it and skip
3076 the abort and the assignment to the sect_syms array.
3077 To reproduce this particular case try running the
3078 linker testsuite test ld-scripts/weak.exp for an ELF
3079 port that uses the generic linker. */
3080 if (sec
->owner
== NULL
)
3083 BFD_ASSERT (sec
->owner
== abfd
);
3085 sect_syms
[sec
->index
] = syms
[idx
];
3090 /* Classify all of the symbols. */
3091 for (idx
= 0; idx
< symcount
; idx
++)
3093 if (!sym_is_global (abfd
, syms
[idx
]))
3099 /* We will be adding a section symbol for each BFD section. Most normal
3100 sections will already have a section symbol in outsymbols, but
3101 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3102 at least in that case. */
3103 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3105 if (sect_syms
[asect
->index
] == NULL
)
3107 if (!sym_is_global (abfd
, asect
->symbol
))
3114 /* Now sort the symbols so the local symbols are first. */
3115 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
3116 new_syms
= bfd_alloc (abfd
, amt
);
3118 if (new_syms
== NULL
)
3121 for (idx
= 0; idx
< symcount
; idx
++)
3123 asymbol
*sym
= syms
[idx
];
3126 if (!sym_is_global (abfd
, sym
))
3129 i
= num_locals
+ num_globals2
++;
3131 sym
->udata
.i
= i
+ 1;
3133 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3135 if (sect_syms
[asect
->index
] == NULL
)
3137 asymbol
*sym
= asect
->symbol
;
3140 sect_syms
[asect
->index
] = sym
;
3141 if (!sym_is_global (abfd
, sym
))
3144 i
= num_locals
+ num_globals2
++;
3146 sym
->udata
.i
= i
+ 1;
3150 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3152 elf_num_locals (abfd
) = num_locals
;
3153 elf_num_globals (abfd
) = num_globals
;
3157 /* Align to the maximum file alignment that could be required for any
3158 ELF data structure. */
3160 static inline file_ptr
3161 align_file_position (file_ptr off
, int align
)
3163 return (off
+ align
- 1) & ~(align
- 1);
3166 /* Assign a file position to a section, optionally aligning to the
3167 required section alignment. */
3170 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3178 al
= i_shdrp
->sh_addralign
;
3180 offset
= BFD_ALIGN (offset
, al
);
3182 i_shdrp
->sh_offset
= offset
;
3183 if (i_shdrp
->bfd_section
!= NULL
)
3184 i_shdrp
->bfd_section
->filepos
= offset
;
3185 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3186 offset
+= i_shdrp
->sh_size
;
3190 /* Compute the file positions we are going to put the sections at, and
3191 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3192 is not NULL, this is being called by the ELF backend linker. */
3195 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3196 struct bfd_link_info
*link_info
)
3198 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3200 struct bfd_strtab_hash
*strtab
;
3201 Elf_Internal_Shdr
*shstrtab_hdr
;
3203 if (abfd
->output_has_begun
)
3206 /* Do any elf backend specific processing first. */
3207 if (bed
->elf_backend_begin_write_processing
)
3208 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3210 if (! prep_headers (abfd
))
3213 /* Post process the headers if necessary. */
3214 if (bed
->elf_backend_post_process_headers
)
3215 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3218 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3222 if (!assign_section_numbers (abfd
))
3225 /* The backend linker builds symbol table information itself. */
3226 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3228 /* Non-zero if doing a relocatable link. */
3229 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3231 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3235 if (link_info
== NULL
)
3237 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3242 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3243 /* sh_name was set in prep_headers. */
3244 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3245 shstrtab_hdr
->sh_flags
= 0;
3246 shstrtab_hdr
->sh_addr
= 0;
3247 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3248 shstrtab_hdr
->sh_entsize
= 0;
3249 shstrtab_hdr
->sh_link
= 0;
3250 shstrtab_hdr
->sh_info
= 0;
3251 /* sh_offset is set in assign_file_positions_except_relocs. */
3252 shstrtab_hdr
->sh_addralign
= 1;
3254 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3257 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3260 Elf_Internal_Shdr
*hdr
;
3262 off
= elf_tdata (abfd
)->next_file_pos
;
3264 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3265 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3267 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3268 if (hdr
->sh_size
!= 0)
3269 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3271 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3272 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3274 elf_tdata (abfd
)->next_file_pos
= off
;
3276 /* Now that we know where the .strtab section goes, write it
3278 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3279 || ! _bfd_stringtab_emit (abfd
, strtab
))
3281 _bfd_stringtab_free (strtab
);
3284 abfd
->output_has_begun
= TRUE
;
3289 /* Create a mapping from a set of sections to a program segment. */
3291 static struct elf_segment_map
*
3292 make_mapping (bfd
*abfd
,
3293 asection
**sections
,
3298 struct elf_segment_map
*m
;
3303 amt
= sizeof (struct elf_segment_map
);
3304 amt
+= (to
- from
- 1) * sizeof (asection
*);
3305 m
= bfd_zalloc (abfd
, amt
);
3309 m
->p_type
= PT_LOAD
;
3310 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3311 m
->sections
[i
- from
] = *hdrpp
;
3312 m
->count
= to
- from
;
3314 if (from
== 0 && phdr
)
3316 /* Include the headers in the first PT_LOAD segment. */
3317 m
->includes_filehdr
= 1;
3318 m
->includes_phdrs
= 1;
3324 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3327 struct elf_segment_map
*
3328 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3330 struct elf_segment_map
*m
;
3332 m
= bfd_zalloc (abfd
, sizeof (struct elf_segment_map
));
3336 m
->p_type
= PT_DYNAMIC
;
3338 m
->sections
[0] = dynsec
;
3343 /* Set up a mapping from BFD sections to program segments. */
3346 map_sections_to_segments (bfd
*abfd
)
3348 asection
**sections
= NULL
;
3352 struct elf_segment_map
*mfirst
;
3353 struct elf_segment_map
**pm
;
3354 struct elf_segment_map
*m
;
3357 unsigned int phdr_index
;
3358 bfd_vma maxpagesize
;
3360 bfd_boolean phdr_in_segment
= TRUE
;
3361 bfd_boolean writable
;
3363 asection
*first_tls
= NULL
;
3364 asection
*dynsec
, *eh_frame_hdr
;
3367 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3370 if (bfd_count_sections (abfd
) == 0)
3373 /* Select the allocated sections, and sort them. */
3375 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
3376 sections
= bfd_malloc (amt
);
3377 if (sections
== NULL
)
3381 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3383 if ((s
->flags
& SEC_ALLOC
) != 0)
3389 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3392 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3394 /* Build the mapping. */
3399 /* If we have a .interp section, then create a PT_PHDR segment for
3400 the program headers and a PT_INTERP segment for the .interp
3402 s
= bfd_get_section_by_name (abfd
, ".interp");
3403 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3405 amt
= sizeof (struct elf_segment_map
);
3406 m
= bfd_zalloc (abfd
, amt
);
3410 m
->p_type
= PT_PHDR
;
3411 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3412 m
->p_flags
= PF_R
| PF_X
;
3413 m
->p_flags_valid
= 1;
3414 m
->includes_phdrs
= 1;
3419 amt
= sizeof (struct elf_segment_map
);
3420 m
= bfd_zalloc (abfd
, amt
);
3424 m
->p_type
= PT_INTERP
;
3432 /* Look through the sections. We put sections in the same program
3433 segment when the start of the second section can be placed within
3434 a few bytes of the end of the first section. */
3438 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
3440 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3442 && (dynsec
->flags
& SEC_LOAD
) == 0)
3445 /* Deal with -Ttext or something similar such that the first section
3446 is not adjacent to the program headers. This is an
3447 approximation, since at this point we don't know exactly how many
3448 program headers we will need. */
3451 bfd_size_type phdr_size
;
3453 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3455 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3456 if ((abfd
->flags
& D_PAGED
) == 0
3457 || sections
[0]->lma
< phdr_size
3458 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3459 phdr_in_segment
= FALSE
;
3462 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3465 bfd_boolean new_segment
;
3469 /* See if this section and the last one will fit in the same
3472 if (last_hdr
== NULL
)
3474 /* If we don't have a segment yet, then we don't need a new
3475 one (we build the last one after this loop). */
3476 new_segment
= FALSE
;
3478 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3480 /* If this section has a different relation between the
3481 virtual address and the load address, then we need a new
3485 else if (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
3486 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3488 /* If putting this section in this segment would force us to
3489 skip a page in the segment, then we need a new segment. */
3492 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3493 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3495 /* We don't want to put a loadable section after a
3496 nonloadable section in the same segment.
3497 Consider .tbss sections as loadable for this purpose. */
3500 else if ((abfd
->flags
& D_PAGED
) == 0)
3502 /* If the file is not demand paged, which means that we
3503 don't require the sections to be correctly aligned in the
3504 file, then there is no other reason for a new segment. */
3505 new_segment
= FALSE
;
3508 && (hdr
->flags
& SEC_READONLY
) == 0
3509 && (((last_hdr
->lma
+ last_size
- 1)
3510 & ~(maxpagesize
- 1))
3511 != (hdr
->lma
& ~(maxpagesize
- 1))))
3513 /* We don't want to put a writable section in a read only
3514 segment, unless they are on the same page in memory
3515 anyhow. We already know that the last section does not
3516 bring us past the current section on the page, so the
3517 only case in which the new section is not on the same
3518 page as the previous section is when the previous section
3519 ends precisely on a page boundary. */
3524 /* Otherwise, we can use the same segment. */
3525 new_segment
= FALSE
;
3530 if ((hdr
->flags
& SEC_READONLY
) == 0)
3533 /* .tbss sections effectively have zero size. */
3534 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3535 last_size
= hdr
->size
;
3541 /* We need a new program segment. We must create a new program
3542 header holding all the sections from phdr_index until hdr. */
3544 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3551 if ((hdr
->flags
& SEC_READONLY
) == 0)
3557 /* .tbss sections effectively have zero size. */
3558 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3559 last_size
= hdr
->size
;
3563 phdr_in_segment
= FALSE
;
3566 /* Create a final PT_LOAD program segment. */
3567 if (last_hdr
!= NULL
)
3569 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3577 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3580 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
3587 /* For each loadable .note section, add a PT_NOTE segment. We don't
3588 use bfd_get_section_by_name, because if we link together
3589 nonloadable .note sections and loadable .note sections, we will
3590 generate two .note sections in the output file. FIXME: Using
3591 names for section types is bogus anyhow. */
3592 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3594 if ((s
->flags
& SEC_LOAD
) != 0
3595 && strncmp (s
->name
, ".note", 5) == 0)
3597 amt
= sizeof (struct elf_segment_map
);
3598 m
= bfd_zalloc (abfd
, amt
);
3602 m
->p_type
= PT_NOTE
;
3609 if (s
->flags
& SEC_THREAD_LOCAL
)
3617 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3622 amt
= sizeof (struct elf_segment_map
);
3623 amt
+= (tls_count
- 1) * sizeof (asection
*);
3624 m
= bfd_zalloc (abfd
, amt
);
3629 m
->count
= tls_count
;
3630 /* Mandated PF_R. */
3632 m
->p_flags_valid
= 1;
3633 for (i
= 0; i
< tls_count
; ++i
)
3635 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3636 m
->sections
[i
] = first_tls
;
3637 first_tls
= first_tls
->next
;
3644 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3646 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
3647 if (eh_frame_hdr
!= NULL
3648 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
3650 amt
= sizeof (struct elf_segment_map
);
3651 m
= bfd_zalloc (abfd
, amt
);
3655 m
->p_type
= PT_GNU_EH_FRAME
;
3657 m
->sections
[0] = eh_frame_hdr
->output_section
;
3663 if (elf_tdata (abfd
)->stack_flags
)
3665 amt
= sizeof (struct elf_segment_map
);
3666 m
= bfd_zalloc (abfd
, amt
);
3670 m
->p_type
= PT_GNU_STACK
;
3671 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
3672 m
->p_flags_valid
= 1;
3678 if (elf_tdata (abfd
)->relro
)
3680 amt
= sizeof (struct elf_segment_map
);
3681 m
= bfd_zalloc (abfd
, amt
);
3685 m
->p_type
= PT_GNU_RELRO
;
3687 m
->p_flags_valid
= 1;
3696 elf_tdata (abfd
)->segment_map
= mfirst
;
3700 if (sections
!= NULL
)
3705 /* Sort sections by address. */
3708 elf_sort_sections (const void *arg1
, const void *arg2
)
3710 const asection
*sec1
= *(const asection
**) arg1
;
3711 const asection
*sec2
= *(const asection
**) arg2
;
3712 bfd_size_type size1
, size2
;
3714 /* Sort by LMA first, since this is the address used to
3715 place the section into a segment. */
3716 if (sec1
->lma
< sec2
->lma
)
3718 else if (sec1
->lma
> sec2
->lma
)
3721 /* Then sort by VMA. Normally the LMA and the VMA will be
3722 the same, and this will do nothing. */
3723 if (sec1
->vma
< sec2
->vma
)
3725 else if (sec1
->vma
> sec2
->vma
)
3728 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3730 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3736 /* If the indicies are the same, do not return 0
3737 here, but continue to try the next comparison. */
3738 if (sec1
->target_index
- sec2
->target_index
!= 0)
3739 return sec1
->target_index
- sec2
->target_index
;
3744 else if (TOEND (sec2
))
3749 /* Sort by size, to put zero sized sections
3750 before others at the same address. */
3752 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
3753 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
3760 return sec1
->target_index
- sec2
->target_index
;
3763 /* Ian Lance Taylor writes:
3765 We shouldn't be using % with a negative signed number. That's just
3766 not good. We have to make sure either that the number is not
3767 negative, or that the number has an unsigned type. When the types
3768 are all the same size they wind up as unsigned. When file_ptr is a
3769 larger signed type, the arithmetic winds up as signed long long,
3772 What we're trying to say here is something like ``increase OFF by
3773 the least amount that will cause it to be equal to the VMA modulo
3775 /* In other words, something like:
3777 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3778 off_offset = off % bed->maxpagesize;
3779 if (vma_offset < off_offset)
3780 adjustment = vma_offset + bed->maxpagesize - off_offset;
3782 adjustment = vma_offset - off_offset;
3784 which can can be collapsed into the expression below. */
3787 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
3789 return ((vma
- off
) % maxpagesize
);
3792 /* Assign file positions to the sections based on the mapping from
3793 sections to segments. This function also sets up some fields in
3794 the file header, and writes out the program headers. */
3797 assign_file_positions_for_segments (bfd
*abfd
, struct bfd_link_info
*link_info
)
3799 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3801 struct elf_segment_map
*m
;
3803 Elf_Internal_Phdr
*phdrs
;
3805 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3806 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3807 Elf_Internal_Phdr
*p
;
3810 if (elf_tdata (abfd
)->segment_map
== NULL
)
3812 if (! map_sections_to_segments (abfd
))
3817 /* The placement algorithm assumes that non allocated sections are
3818 not in PT_LOAD segments. We ensure this here by removing such
3819 sections from the segment map. */
3820 for (m
= elf_tdata (abfd
)->segment_map
;
3824 unsigned int new_count
;
3827 if (m
->p_type
!= PT_LOAD
)
3831 for (i
= 0; i
< m
->count
; i
++)
3833 if ((m
->sections
[i
]->flags
& SEC_ALLOC
) != 0)
3836 m
->sections
[new_count
] = m
->sections
[i
];
3842 if (new_count
!= m
->count
)
3843 m
->count
= new_count
;
3847 if (bed
->elf_backend_modify_segment_map
)
3849 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
, link_info
))
3854 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3857 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3858 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3859 elf_elfheader (abfd
)->e_phnum
= count
;
3863 elf_tdata (abfd
)->next_file_pos
= bed
->s
->sizeof_ehdr
;
3867 /* If we already counted the number of program segments, make sure
3868 that we allocated enough space. This happens when SIZEOF_HEADERS
3869 is used in a linker script. */
3870 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3871 if (alloc
!= 0 && count
> alloc
)
3873 ((*_bfd_error_handler
)
3874 (_("%B: Not enough room for program headers (allocated %u, need %u)"),
3875 abfd
, alloc
, count
));
3876 bfd_set_error (bfd_error_bad_value
);
3883 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3884 phdrs
= bfd_alloc (abfd
, amt
);
3888 off
= bed
->s
->sizeof_ehdr
;
3889 off
+= alloc
* bed
->s
->sizeof_phdr
;
3896 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3903 /* If elf_segment_map is not from map_sections_to_segments, the
3904 sections may not be correctly ordered. NOTE: sorting should
3905 not be done to the PT_NOTE section of a corefile, which may
3906 contain several pseudo-sections artificially created by bfd.
3907 Sorting these pseudo-sections breaks things badly. */
3909 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3910 && m
->p_type
== PT_NOTE
))
3911 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3914 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
3915 number of sections with contents contributing to both p_filesz
3916 and p_memsz, followed by a number of sections with no contents
3917 that just contribute to p_memsz. In this loop, OFF tracks next
3918 available file offset for PT_LOAD and PT_NOTE segments. VOFF is
3919 an adjustment we use for segments that have no file contents
3920 but need zero filled memory allocation. */
3922 p
->p_type
= m
->p_type
;
3923 p
->p_flags
= m
->p_flags
;
3925 if (p
->p_type
== PT_LOAD
3928 bfd_size_type align
;
3931 if ((abfd
->flags
& D_PAGED
) != 0)
3932 align
= bed
->maxpagesize
;
3935 unsigned int align_power
= 0;
3936 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3938 unsigned int secalign
;
3940 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3941 if (secalign
> align_power
)
3942 align_power
= secalign
;
3944 align
= (bfd_size_type
) 1 << align_power
;
3947 adjust
= vma_page_aligned_bias (m
->sections
[0]->vma
, off
, align
);
3950 && !m
->includes_filehdr
3951 && !m
->includes_phdrs
3952 && (ufile_ptr
) off
>= align
)
3954 /* If the first section isn't loadable, the same holds for
3955 any other sections. Since the segment won't need file
3956 space, we can make p_offset overlap some prior segment.
3957 However, .tbss is special. If a segment starts with
3958 .tbss, we need to look at the next section to decide
3959 whether the segment has any loadable sections. */
3961 while ((m
->sections
[i
]->flags
& SEC_LOAD
) == 0)
3963 if ((m
->sections
[i
]->flags
& SEC_THREAD_LOCAL
) == 0
3967 voff
= adjust
- align
;
3973 /* Make sure the .dynamic section is the first section in the
3974 PT_DYNAMIC segment. */
3975 else if (p
->p_type
== PT_DYNAMIC
3977 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
3980 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
3982 bfd_set_error (bfd_error_bad_value
);
3989 p
->p_vaddr
= m
->sections
[0]->vma
;
3991 if (m
->p_paddr_valid
)
3992 p
->p_paddr
= m
->p_paddr
;
3993 else if (m
->count
== 0)
3996 p
->p_paddr
= m
->sections
[0]->lma
;
3998 if (p
->p_type
== PT_LOAD
3999 && (abfd
->flags
& D_PAGED
) != 0)
4000 p
->p_align
= bed
->maxpagesize
;
4001 else if (m
->count
== 0)
4002 p
->p_align
= 1 << bed
->s
->log_file_align
;
4010 if (m
->includes_filehdr
)
4012 if (! m
->p_flags_valid
)
4015 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4016 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4019 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4021 if (p
->p_vaddr
< (bfd_vma
) off
)
4023 (*_bfd_error_handler
)
4024 (_("%B: Not enough room for program headers, try linking with -N"),
4026 bfd_set_error (bfd_error_bad_value
);
4031 if (! m
->p_paddr_valid
)
4034 if (p
->p_type
== PT_LOAD
)
4036 filehdr_vaddr
= p
->p_vaddr
;
4037 filehdr_paddr
= p
->p_paddr
;
4041 if (m
->includes_phdrs
)
4043 if (! m
->p_flags_valid
)
4046 if (m
->includes_filehdr
)
4048 if (p
->p_type
== PT_LOAD
)
4050 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
4051 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
4056 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4060 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4061 p
->p_vaddr
-= off
- p
->p_offset
;
4062 if (! m
->p_paddr_valid
)
4063 p
->p_paddr
-= off
- p
->p_offset
;
4066 if (p
->p_type
== PT_LOAD
)
4068 phdrs_vaddr
= p
->p_vaddr
;
4069 phdrs_paddr
= p
->p_paddr
;
4072 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4075 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4076 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4079 if (p
->p_type
== PT_LOAD
4080 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4082 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
4083 p
->p_offset
= off
+ voff
;
4088 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4089 p
->p_filesz
+= adjust
;
4090 p
->p_memsz
+= adjust
;
4094 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4098 bfd_size_type align
;
4102 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
4104 if (p
->p_type
== PT_LOAD
4105 || p
->p_type
== PT_TLS
)
4107 bfd_signed_vma adjust
;
4109 if ((flags
& SEC_LOAD
) != 0)
4111 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_filesz
);
4114 (*_bfd_error_handler
)
4115 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4116 abfd
, sec
, (unsigned long) sec
->lma
);
4120 p
->p_filesz
+= adjust
;
4121 p
->p_memsz
+= adjust
;
4123 /* .tbss is special. It doesn't contribute to p_memsz of
4125 else if ((flags
& SEC_THREAD_LOCAL
) == 0
4126 || p
->p_type
== PT_TLS
)
4128 /* The section VMA must equal the file position
4129 modulo the page size. */
4130 bfd_size_type page
= align
;
4131 if ((abfd
->flags
& D_PAGED
) != 0)
4132 page
= bed
->maxpagesize
;
4133 adjust
= vma_page_aligned_bias (sec
->vma
,
4134 p
->p_vaddr
+ p
->p_memsz
,
4136 p
->p_memsz
+= adjust
;
4140 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4142 /* The section at i == 0 is the one that actually contains
4148 p
->p_filesz
= sec
->size
;
4154 /* The rest are fake sections that shouldn't be written. */
4163 if (p
->p_type
== PT_LOAD
)
4166 /* FIXME: The SEC_HAS_CONTENTS test here dates back to
4167 1997, and the exact reason for it isn't clear. One
4168 plausible explanation is that it is to work around
4169 a problem we have with linker scripts using data
4170 statements in NOLOAD sections. I don't think it
4171 makes a great deal of sense to have such a section
4172 assigned to a PT_LOAD segment, but apparently
4173 people do this. The data statement results in a
4174 bfd_data_link_order being built, and these need
4175 section contents to write into. Eventually, we get
4176 to _bfd_elf_write_object_contents which writes any
4177 section with contents to the output. Make room
4178 here for the write, so that following segments are
4180 if ((flags
& SEC_LOAD
) != 0
4181 || (flags
& SEC_HAS_CONTENTS
) != 0)
4185 if ((flags
& SEC_LOAD
) != 0)
4187 p
->p_filesz
+= sec
->size
;
4188 p
->p_memsz
+= sec
->size
;
4190 /* .tbss is special. It doesn't contribute to p_memsz of
4192 else if ((flags
& SEC_THREAD_LOCAL
) == 0
4193 || p
->p_type
== PT_TLS
)
4194 p
->p_memsz
+= sec
->size
;
4196 if (p
->p_type
== PT_TLS
4198 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
4200 struct bfd_link_order
*o
;
4201 bfd_vma tbss_size
= 0;
4203 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
4204 if (tbss_size
< o
->offset
+ o
->size
)
4205 tbss_size
= o
->offset
+ o
->size
;
4207 p
->p_memsz
+= tbss_size
;
4210 if (align
> p
->p_align
4211 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
4215 if (! m
->p_flags_valid
)
4218 if ((flags
& SEC_CODE
) != 0)
4220 if ((flags
& SEC_READONLY
) == 0)
4226 /* Now that we have set the section file positions, we can set up
4227 the file positions for the non PT_LOAD segments. */
4228 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4232 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
4234 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
4235 /* If the section has not yet been assigned a file position,
4236 do so now. The ARM BPABI requires that .dynamic section
4237 not be marked SEC_ALLOC because it is not part of any
4238 PT_LOAD segment, so it will not be processed above. */
4239 if (p
->p_type
== PT_DYNAMIC
&& m
->sections
[0]->filepos
== 0)
4242 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4245 while (i_shdrpp
[i
]->bfd_section
!= m
->sections
[0])
4247 off
= (_bfd_elf_assign_file_position_for_section
4248 (i_shdrpp
[i
], off
, TRUE
));
4249 p
->p_filesz
= m
->sections
[0]->size
;
4251 p
->p_offset
= m
->sections
[0]->filepos
;
4255 if (m
->includes_filehdr
)
4257 p
->p_vaddr
= filehdr_vaddr
;
4258 if (! m
->p_paddr_valid
)
4259 p
->p_paddr
= filehdr_paddr
;
4261 else if (m
->includes_phdrs
)
4263 p
->p_vaddr
= phdrs_vaddr
;
4264 if (! m
->p_paddr_valid
)
4265 p
->p_paddr
= phdrs_paddr
;
4267 else if (p
->p_type
== PT_GNU_RELRO
)
4269 Elf_Internal_Phdr
*lp
;
4271 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4273 if (lp
->p_type
== PT_LOAD
4274 && lp
->p_vaddr
<= link_info
->relro_end
4275 && lp
->p_vaddr
>= link_info
->relro_start
4276 && lp
->p_vaddr
+ lp
->p_filesz
4277 >= link_info
->relro_end
)
4281 if (lp
< phdrs
+ count
4282 && link_info
->relro_end
> lp
->p_vaddr
)
4284 p
->p_vaddr
= lp
->p_vaddr
;
4285 p
->p_paddr
= lp
->p_paddr
;
4286 p
->p_offset
= lp
->p_offset
;
4287 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
4288 p
->p_memsz
= p
->p_filesz
;
4290 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
4294 memset (p
, 0, sizeof *p
);
4295 p
->p_type
= PT_NULL
;
4301 /* Clear out any program headers we allocated but did not use. */
4302 for (; count
< alloc
; count
++, p
++)
4304 memset (p
, 0, sizeof *p
);
4305 p
->p_type
= PT_NULL
;
4308 elf_tdata (abfd
)->phdr
= phdrs
;
4310 elf_tdata (abfd
)->next_file_pos
= off
;
4312 /* Write out the program headers. */
4313 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4314 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
4320 /* Get the size of the program header.
4322 If this is called by the linker before any of the section VMA's are set, it
4323 can't calculate the correct value for a strange memory layout. This only
4324 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4325 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4326 data segment (exclusive of .interp and .dynamic).
4328 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4329 will be two segments. */
4331 static bfd_size_type
4332 get_program_header_size (bfd
*abfd
)
4336 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4338 /* We can't return a different result each time we're called. */
4339 if (elf_tdata (abfd
)->program_header_size
!= 0)
4340 return elf_tdata (abfd
)->program_header_size
;
4342 if (elf_tdata (abfd
)->segment_map
!= NULL
)
4344 struct elf_segment_map
*m
;
4347 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4349 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4350 return elf_tdata (abfd
)->program_header_size
;
4353 /* Assume we will need exactly two PT_LOAD segments: one for text
4354 and one for data. */
4357 s
= bfd_get_section_by_name (abfd
, ".interp");
4358 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4360 /* If we have a loadable interpreter section, we need a
4361 PT_INTERP segment. In this case, assume we also need a
4362 PT_PHDR segment, although that may not be true for all
4367 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4369 /* We need a PT_DYNAMIC segment. */
4373 if (elf_tdata (abfd
)->eh_frame_hdr
)
4375 /* We need a PT_GNU_EH_FRAME segment. */
4379 if (elf_tdata (abfd
)->stack_flags
)
4381 /* We need a PT_GNU_STACK segment. */
4385 if (elf_tdata (abfd
)->relro
)
4387 /* We need a PT_GNU_RELRO segment. */
4391 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4393 if ((s
->flags
& SEC_LOAD
) != 0
4394 && strncmp (s
->name
, ".note", 5) == 0)
4396 /* We need a PT_NOTE segment. */
4401 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4403 if (s
->flags
& SEC_THREAD_LOCAL
)
4405 /* We need a PT_TLS segment. */
4411 /* Let the backend count up any program headers it might need. */
4412 if (bed
->elf_backend_additional_program_headers
)
4416 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
4422 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4423 return elf_tdata (abfd
)->program_header_size
;
4426 /* Work out the file positions of all the sections. This is called by
4427 _bfd_elf_compute_section_file_positions. All the section sizes and
4428 VMAs must be known before this is called.
4430 Reloc sections come in two flavours: Those processed specially as
4431 "side-channel" data attached to a section to which they apply, and
4432 those that bfd doesn't process as relocations. The latter sort are
4433 stored in a normal bfd section by bfd_section_from_shdr. We don't
4434 consider the former sort here, unless they form part of the loadable
4435 image. Reloc sections not assigned here will be handled later by
4436 assign_file_positions_for_relocs.
4438 We also don't set the positions of the .symtab and .strtab here. */
4441 assign_file_positions_except_relocs (bfd
*abfd
,
4442 struct bfd_link_info
*link_info
)
4444 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
4445 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
4446 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4447 unsigned int num_sec
= elf_numsections (abfd
);
4449 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4451 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4452 && bfd_get_format (abfd
) != bfd_core
)
4454 Elf_Internal_Shdr
**hdrpp
;
4457 /* Start after the ELF header. */
4458 off
= i_ehdrp
->e_ehsize
;
4460 /* We are not creating an executable, which means that we are
4461 not creating a program header, and that the actual order of
4462 the sections in the file is unimportant. */
4463 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4465 Elf_Internal_Shdr
*hdr
;
4468 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4469 && hdr
->bfd_section
== NULL
)
4470 || i
== tdata
->symtab_section
4471 || i
== tdata
->symtab_shndx_section
4472 || i
== tdata
->strtab_section
)
4474 hdr
->sh_offset
= -1;
4477 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4479 if (i
== SHN_LORESERVE
- 1)
4481 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4482 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4489 Elf_Internal_Shdr
**hdrpp
;
4491 /* Assign file positions for the loaded sections based on the
4492 assignment of sections to segments. */
4493 if (! assign_file_positions_for_segments (abfd
, link_info
))
4496 /* Assign file positions for the other sections. */
4498 off
= elf_tdata (abfd
)->next_file_pos
;
4499 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4501 Elf_Internal_Shdr
*hdr
;
4504 if (hdr
->bfd_section
!= NULL
4505 && hdr
->bfd_section
->filepos
!= 0)
4506 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
4507 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4509 ((*_bfd_error_handler
)
4510 (_("%B: warning: allocated section `%s' not in segment"),
4512 (hdr
->bfd_section
== NULL
4514 : hdr
->bfd_section
->name
)));
4515 if ((abfd
->flags
& D_PAGED
) != 0)
4516 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4519 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4521 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4524 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4525 && hdr
->bfd_section
== NULL
)
4526 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4527 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4528 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4529 hdr
->sh_offset
= -1;
4531 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4533 if (i
== SHN_LORESERVE
- 1)
4535 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4536 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4541 /* Place the section headers. */
4542 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
4543 i_ehdrp
->e_shoff
= off
;
4544 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4546 elf_tdata (abfd
)->next_file_pos
= off
;
4552 prep_headers (bfd
*abfd
)
4554 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4555 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4556 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4557 struct elf_strtab_hash
*shstrtab
;
4558 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4560 i_ehdrp
= elf_elfheader (abfd
);
4561 i_shdrp
= elf_elfsections (abfd
);
4563 shstrtab
= _bfd_elf_strtab_init ();
4564 if (shstrtab
== NULL
)
4567 elf_shstrtab (abfd
) = shstrtab
;
4569 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4570 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4571 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4572 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4574 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4575 i_ehdrp
->e_ident
[EI_DATA
] =
4576 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4577 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4579 if ((abfd
->flags
& DYNAMIC
) != 0)
4580 i_ehdrp
->e_type
= ET_DYN
;
4581 else if ((abfd
->flags
& EXEC_P
) != 0)
4582 i_ehdrp
->e_type
= ET_EXEC
;
4583 else if (bfd_get_format (abfd
) == bfd_core
)
4584 i_ehdrp
->e_type
= ET_CORE
;
4586 i_ehdrp
->e_type
= ET_REL
;
4588 switch (bfd_get_arch (abfd
))
4590 case bfd_arch_unknown
:
4591 i_ehdrp
->e_machine
= EM_NONE
;
4594 /* There used to be a long list of cases here, each one setting
4595 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4596 in the corresponding bfd definition. To avoid duplication,
4597 the switch was removed. Machines that need special handling
4598 can generally do it in elf_backend_final_write_processing(),
4599 unless they need the information earlier than the final write.
4600 Such need can generally be supplied by replacing the tests for
4601 e_machine with the conditions used to determine it. */
4603 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
4606 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4607 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4609 /* No program header, for now. */
4610 i_ehdrp
->e_phoff
= 0;
4611 i_ehdrp
->e_phentsize
= 0;
4612 i_ehdrp
->e_phnum
= 0;
4614 /* Each bfd section is section header entry. */
4615 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4616 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4618 /* If we're building an executable, we'll need a program header table. */
4619 if (abfd
->flags
& EXEC_P
)
4620 /* It all happens later. */
4624 i_ehdrp
->e_phentsize
= 0;
4626 i_ehdrp
->e_phoff
= 0;
4629 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4630 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
4631 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4632 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
4633 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4634 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
4635 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4636 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4637 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4643 /* Assign file positions for all the reloc sections which are not part
4644 of the loadable file image. */
4647 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
4650 unsigned int i
, num_sec
;
4651 Elf_Internal_Shdr
**shdrpp
;
4653 off
= elf_tdata (abfd
)->next_file_pos
;
4655 num_sec
= elf_numsections (abfd
);
4656 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4658 Elf_Internal_Shdr
*shdrp
;
4661 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4662 && shdrp
->sh_offset
== -1)
4663 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
4666 elf_tdata (abfd
)->next_file_pos
= off
;
4670 _bfd_elf_write_object_contents (bfd
*abfd
)
4672 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4673 Elf_Internal_Ehdr
*i_ehdrp
;
4674 Elf_Internal_Shdr
**i_shdrp
;
4676 unsigned int count
, num_sec
;
4678 if (! abfd
->output_has_begun
4679 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
4682 i_shdrp
= elf_elfsections (abfd
);
4683 i_ehdrp
= elf_elfheader (abfd
);
4686 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4690 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4692 /* After writing the headers, we need to write the sections too... */
4693 num_sec
= elf_numsections (abfd
);
4694 for (count
= 1; count
< num_sec
; count
++)
4696 if (bed
->elf_backend_section_processing
)
4697 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4698 if (i_shdrp
[count
]->contents
)
4700 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4702 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4703 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4706 if (count
== SHN_LORESERVE
- 1)
4707 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4710 /* Write out the section header names. */
4711 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4712 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4715 if (bed
->elf_backend_final_write_processing
)
4716 (*bed
->elf_backend_final_write_processing
) (abfd
,
4717 elf_tdata (abfd
)->linker
);
4719 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4723 _bfd_elf_write_corefile_contents (bfd
*abfd
)
4725 /* Hopefully this can be done just like an object file. */
4726 return _bfd_elf_write_object_contents (abfd
);
4729 /* Given a section, search the header to find them. */
4732 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
4734 const struct elf_backend_data
*bed
;
4737 if (elf_section_data (asect
) != NULL
4738 && elf_section_data (asect
)->this_idx
!= 0)
4739 return elf_section_data (asect
)->this_idx
;
4741 if (bfd_is_abs_section (asect
))
4743 else if (bfd_is_com_section (asect
))
4745 else if (bfd_is_und_section (asect
))
4749 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4750 int maxindex
= elf_numsections (abfd
);
4752 for (index
= 1; index
< maxindex
; index
++)
4754 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4756 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4762 bed
= get_elf_backend_data (abfd
);
4763 if (bed
->elf_backend_section_from_bfd_section
)
4767 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4772 bfd_set_error (bfd_error_nonrepresentable_section
);
4777 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4781 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
4783 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4785 flagword flags
= asym_ptr
->flags
;
4787 /* When gas creates relocations against local labels, it creates its
4788 own symbol for the section, but does put the symbol into the
4789 symbol chain, so udata is 0. When the linker is generating
4790 relocatable output, this section symbol may be for one of the
4791 input sections rather than the output section. */
4792 if (asym_ptr
->udata
.i
== 0
4793 && (flags
& BSF_SECTION_SYM
)
4794 && asym_ptr
->section
)
4798 if (asym_ptr
->section
->output_section
!= NULL
)
4799 indx
= asym_ptr
->section
->output_section
->index
;
4801 indx
= asym_ptr
->section
->index
;
4802 if (indx
< elf_num_section_syms (abfd
)
4803 && elf_section_syms (abfd
)[indx
] != NULL
)
4804 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4807 idx
= asym_ptr
->udata
.i
;
4811 /* This case can occur when using --strip-symbol on a symbol
4812 which is used in a relocation entry. */
4813 (*_bfd_error_handler
)
4814 (_("%B: symbol `%s' required but not present"),
4815 abfd
, bfd_asymbol_name (asym_ptr
));
4816 bfd_set_error (bfd_error_no_symbols
);
4823 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4824 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4825 elf_symbol_flags (flags
));
4833 /* Copy private BFD data. This copies any program header information. */
4836 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
4838 Elf_Internal_Ehdr
*iehdr
;
4839 struct elf_segment_map
*map
;
4840 struct elf_segment_map
*map_first
;
4841 struct elf_segment_map
**pointer_to_map
;
4842 Elf_Internal_Phdr
*segment
;
4845 unsigned int num_segments
;
4846 bfd_boolean phdr_included
= FALSE
;
4847 bfd_vma maxpagesize
;
4848 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
4849 unsigned int phdr_adjust_num
= 0;
4850 const struct elf_backend_data
*bed
;
4852 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4853 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4856 if (elf_tdata (ibfd
)->phdr
== NULL
)
4859 bed
= get_elf_backend_data (ibfd
);
4860 iehdr
= elf_elfheader (ibfd
);
4863 pointer_to_map
= &map_first
;
4865 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4866 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4868 /* Returns the end address of the segment + 1. */
4869 #define SEGMENT_END(segment, start) \
4870 (start + (segment->p_memsz > segment->p_filesz \
4871 ? segment->p_memsz : segment->p_filesz))
4873 #define SECTION_SIZE(section, segment) \
4874 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4875 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4876 ? section->size : 0)
4878 /* Returns TRUE if the given section is contained within
4879 the given segment. VMA addresses are compared. */
4880 #define IS_CONTAINED_BY_VMA(section, segment) \
4881 (section->vma >= segment->p_vaddr \
4882 && (section->vma + SECTION_SIZE (section, segment) \
4883 <= (SEGMENT_END (segment, segment->p_vaddr))))
4885 /* Returns TRUE if the given section is contained within
4886 the given segment. LMA addresses are compared. */
4887 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4888 (section->lma >= base \
4889 && (section->lma + SECTION_SIZE (section, segment) \
4890 <= SEGMENT_END (segment, base)))
4892 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4893 #define IS_COREFILE_NOTE(p, s) \
4894 (p->p_type == PT_NOTE \
4895 && bfd_get_format (ibfd) == bfd_core \
4896 && s->vma == 0 && s->lma == 0 \
4897 && (bfd_vma) s->filepos >= p->p_offset \
4898 && ((bfd_vma) s->filepos + s->size \
4899 <= p->p_offset + p->p_filesz))
4901 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4902 linker, which generates a PT_INTERP section with p_vaddr and
4903 p_memsz set to 0. */
4904 #define IS_SOLARIS_PT_INTERP(p, s) \
4906 && p->p_paddr == 0 \
4907 && p->p_memsz == 0 \
4908 && p->p_filesz > 0 \
4909 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4911 && (bfd_vma) s->filepos >= p->p_offset \
4912 && ((bfd_vma) s->filepos + s->size \
4913 <= p->p_offset + p->p_filesz))
4915 /* Decide if the given section should be included in the given segment.
4916 A section will be included if:
4917 1. It is within the address space of the segment -- we use the LMA
4918 if that is set for the segment and the VMA otherwise,
4919 2. It is an allocated segment,
4920 3. There is an output section associated with it,
4921 4. The section has not already been allocated to a previous segment.
4922 5. PT_GNU_STACK segments do not include any sections.
4923 6. PT_TLS segment includes only SHF_TLS sections.
4924 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
4925 8. PT_DYNAMIC should not contain empty sections at the beginning
4926 (with the possible exception of .dynamic). */
4927 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4928 ((((segment->p_paddr \
4929 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4930 : IS_CONTAINED_BY_VMA (section, segment)) \
4931 && (section->flags & SEC_ALLOC) != 0) \
4932 || IS_COREFILE_NOTE (segment, section)) \
4933 && section->output_section != NULL \
4934 && segment->p_type != PT_GNU_STACK \
4935 && (segment->p_type != PT_TLS \
4936 || (section->flags & SEC_THREAD_LOCAL)) \
4937 && (segment->p_type == PT_LOAD \
4938 || segment->p_type == PT_TLS \
4939 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4940 && (segment->p_type != PT_DYNAMIC \
4941 || SECTION_SIZE (section, segment) > 0 \
4942 || (segment->p_paddr \
4943 ? segment->p_paddr != section->lma \
4944 : segment->p_vaddr != section->vma) \
4945 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
4947 && ! section->segment_mark)
4949 /* Returns TRUE iff seg1 starts after the end of seg2. */
4950 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4951 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4953 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4954 their VMA address ranges and their LMA address ranges overlap.
4955 It is possible to have overlapping VMA ranges without overlapping LMA
4956 ranges. RedBoot images for example can have both .data and .bss mapped
4957 to the same VMA range, but with the .data section mapped to a different
4959 #define SEGMENT_OVERLAPS(seg1, seg2) \
4960 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4961 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4962 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4963 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4965 /* Initialise the segment mark field. */
4966 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4967 section
->segment_mark
= FALSE
;
4969 /* Scan through the segments specified in the program header
4970 of the input BFD. For this first scan we look for overlaps
4971 in the loadable segments. These can be created by weird
4972 parameters to objcopy. Also, fix some solaris weirdness. */
4973 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4978 Elf_Internal_Phdr
*segment2
;
4980 if (segment
->p_type
== PT_INTERP
)
4981 for (section
= ibfd
->sections
; section
; section
= section
->next
)
4982 if (IS_SOLARIS_PT_INTERP (segment
, section
))
4984 /* Mininal change so that the normal section to segment
4985 assignment code will work. */
4986 segment
->p_vaddr
= section
->vma
;
4990 if (segment
->p_type
!= PT_LOAD
)
4993 /* Determine if this segment overlaps any previous segments. */
4994 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4996 bfd_signed_vma extra_length
;
4998 if (segment2
->p_type
!= PT_LOAD
4999 || ! SEGMENT_OVERLAPS (segment
, segment2
))
5002 /* Merge the two segments together. */
5003 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5005 /* Extend SEGMENT2 to include SEGMENT and then delete
5008 SEGMENT_END (segment
, segment
->p_vaddr
)
5009 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
5011 if (extra_length
> 0)
5013 segment2
->p_memsz
+= extra_length
;
5014 segment2
->p_filesz
+= extra_length
;
5017 segment
->p_type
= PT_NULL
;
5019 /* Since we have deleted P we must restart the outer loop. */
5021 segment
= elf_tdata (ibfd
)->phdr
;
5026 /* Extend SEGMENT to include SEGMENT2 and then delete
5029 SEGMENT_END (segment2
, segment2
->p_vaddr
)
5030 - SEGMENT_END (segment
, segment
->p_vaddr
);
5032 if (extra_length
> 0)
5034 segment
->p_memsz
+= extra_length
;
5035 segment
->p_filesz
+= extra_length
;
5038 segment2
->p_type
= PT_NULL
;
5043 /* The second scan attempts to assign sections to segments. */
5044 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5048 unsigned int section_count
;
5049 asection
** sections
;
5050 asection
* output_section
;
5052 bfd_vma matching_lma
;
5053 bfd_vma suggested_lma
;
5057 if (segment
->p_type
== PT_NULL
)
5060 /* Compute how many sections might be placed into this segment. */
5061 for (section
= ibfd
->sections
, section_count
= 0;
5063 section
= section
->next
)
5064 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5067 /* Allocate a segment map big enough to contain
5068 all of the sections we have selected. */
5069 amt
= sizeof (struct elf_segment_map
);
5070 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5071 map
= bfd_alloc (obfd
, amt
);
5075 /* Initialise the fields of the segment map. Default to
5076 using the physical address of the segment in the input BFD. */
5078 map
->p_type
= segment
->p_type
;
5079 map
->p_flags
= segment
->p_flags
;
5080 map
->p_flags_valid
= 1;
5081 map
->p_paddr
= segment
->p_paddr
;
5082 map
->p_paddr_valid
= 1;
5084 /* Determine if this segment contains the ELF file header
5085 and if it contains the program headers themselves. */
5086 map
->includes_filehdr
= (segment
->p_offset
== 0
5087 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5089 map
->includes_phdrs
= 0;
5091 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
5093 map
->includes_phdrs
=
5094 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5095 && (segment
->p_offset
+ segment
->p_filesz
5096 >= ((bfd_vma
) iehdr
->e_phoff
5097 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5099 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5100 phdr_included
= TRUE
;
5103 if (section_count
== 0)
5105 /* Special segments, such as the PT_PHDR segment, may contain
5106 no sections, but ordinary, loadable segments should contain
5107 something. They are allowed by the ELF spec however, so only
5108 a warning is produced. */
5109 if (segment
->p_type
== PT_LOAD
)
5110 (*_bfd_error_handler
)
5111 (_("%B: warning: Empty loadable segment detected, is this intentional ?\n"),
5115 *pointer_to_map
= map
;
5116 pointer_to_map
= &map
->next
;
5121 /* Now scan the sections in the input BFD again and attempt
5122 to add their corresponding output sections to the segment map.
5123 The problem here is how to handle an output section which has
5124 been moved (ie had its LMA changed). There are four possibilities:
5126 1. None of the sections have been moved.
5127 In this case we can continue to use the segment LMA from the
5130 2. All of the sections have been moved by the same amount.
5131 In this case we can change the segment's LMA to match the LMA
5132 of the first section.
5134 3. Some of the sections have been moved, others have not.
5135 In this case those sections which have not been moved can be
5136 placed in the current segment which will have to have its size,
5137 and possibly its LMA changed, and a new segment or segments will
5138 have to be created to contain the other sections.
5140 4. The sections have been moved, but not by the same amount.
5141 In this case we can change the segment's LMA to match the LMA
5142 of the first section and we will have to create a new segment
5143 or segments to contain the other sections.
5145 In order to save time, we allocate an array to hold the section
5146 pointers that we are interested in. As these sections get assigned
5147 to a segment, they are removed from this array. */
5149 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5150 to work around this long long bug. */
5151 amt
= section_count
* sizeof (asection
*);
5152 sections
= bfd_malloc (amt
);
5153 if (sections
== NULL
)
5156 /* Step One: Scan for segment vs section LMA conflicts.
5157 Also add the sections to the section array allocated above.
5158 Also add the sections to the current segment. In the common
5159 case, where the sections have not been moved, this means that
5160 we have completely filled the segment, and there is nothing
5166 for (j
= 0, section
= ibfd
->sections
;
5168 section
= section
->next
)
5170 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5172 output_section
= section
->output_section
;
5174 sections
[j
++] = section
;
5176 /* The Solaris native linker always sets p_paddr to 0.
5177 We try to catch that case here, and set it to the
5178 correct value. Note - some backends require that
5179 p_paddr be left as zero. */
5180 if (segment
->p_paddr
== 0
5181 && segment
->p_vaddr
!= 0
5182 && (! bed
->want_p_paddr_set_to_zero
)
5184 && output_section
->lma
!= 0
5185 && (output_section
->vma
== (segment
->p_vaddr
5186 + (map
->includes_filehdr
5189 + (map
->includes_phdrs
5191 * iehdr
->e_phentsize
)
5193 map
->p_paddr
= segment
->p_vaddr
;
5195 /* Match up the physical address of the segment with the
5196 LMA address of the output section. */
5197 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5198 || IS_COREFILE_NOTE (segment
, section
)
5199 || (bed
->want_p_paddr_set_to_zero
&&
5200 IS_CONTAINED_BY_VMA (output_section
, segment
))
5203 if (matching_lma
== 0)
5204 matching_lma
= output_section
->lma
;
5206 /* We assume that if the section fits within the segment
5207 then it does not overlap any other section within that
5209 map
->sections
[isec
++] = output_section
;
5211 else if (suggested_lma
== 0)
5212 suggested_lma
= output_section
->lma
;
5216 BFD_ASSERT (j
== section_count
);
5218 /* Step Two: Adjust the physical address of the current segment,
5220 if (isec
== section_count
)
5222 /* All of the sections fitted within the segment as currently
5223 specified. This is the default case. Add the segment to
5224 the list of built segments and carry on to process the next
5225 program header in the input BFD. */
5226 map
->count
= section_count
;
5227 *pointer_to_map
= map
;
5228 pointer_to_map
= &map
->next
;
5235 if (matching_lma
!= 0)
5237 /* At least one section fits inside the current segment.
5238 Keep it, but modify its physical address to match the
5239 LMA of the first section that fitted. */
5240 map
->p_paddr
= matching_lma
;
5244 /* None of the sections fitted inside the current segment.
5245 Change the current segment's physical address to match
5246 the LMA of the first section. */
5247 map
->p_paddr
= suggested_lma
;
5250 /* Offset the segment physical address from the lma
5251 to allow for space taken up by elf headers. */
5252 if (map
->includes_filehdr
)
5253 map
->p_paddr
-= iehdr
->e_ehsize
;
5255 if (map
->includes_phdrs
)
5257 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5259 /* iehdr->e_phnum is just an estimate of the number
5260 of program headers that we will need. Make a note
5261 here of the number we used and the segment we chose
5262 to hold these headers, so that we can adjust the
5263 offset when we know the correct value. */
5264 phdr_adjust_num
= iehdr
->e_phnum
;
5265 phdr_adjust_seg
= map
;
5269 /* Step Three: Loop over the sections again, this time assigning
5270 those that fit to the current segment and removing them from the
5271 sections array; but making sure not to leave large gaps. Once all
5272 possible sections have been assigned to the current segment it is
5273 added to the list of built segments and if sections still remain
5274 to be assigned, a new segment is constructed before repeating
5282 /* Fill the current segment with sections that fit. */
5283 for (j
= 0; j
< section_count
; j
++)
5285 section
= sections
[j
];
5287 if (section
== NULL
)
5290 output_section
= section
->output_section
;
5292 BFD_ASSERT (output_section
!= NULL
);
5294 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5295 || IS_COREFILE_NOTE (segment
, section
))
5297 if (map
->count
== 0)
5299 /* If the first section in a segment does not start at
5300 the beginning of the segment, then something is
5302 if (output_section
->lma
!=
5304 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5305 + (map
->includes_phdrs
5306 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5312 asection
* prev_sec
;
5314 prev_sec
= map
->sections
[map
->count
- 1];
5316 /* If the gap between the end of the previous section
5317 and the start of this section is more than
5318 maxpagesize then we need to start a new segment. */
5319 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
5321 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5322 || ((prev_sec
->lma
+ prev_sec
->size
)
5323 > output_section
->lma
))
5325 if (suggested_lma
== 0)
5326 suggested_lma
= output_section
->lma
;
5332 map
->sections
[map
->count
++] = output_section
;
5335 section
->segment_mark
= TRUE
;
5337 else if (suggested_lma
== 0)
5338 suggested_lma
= output_section
->lma
;
5341 BFD_ASSERT (map
->count
> 0);
5343 /* Add the current segment to the list of built segments. */
5344 *pointer_to_map
= map
;
5345 pointer_to_map
= &map
->next
;
5347 if (isec
< section_count
)
5349 /* We still have not allocated all of the sections to
5350 segments. Create a new segment here, initialise it
5351 and carry on looping. */
5352 amt
= sizeof (struct elf_segment_map
);
5353 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5354 map
= bfd_alloc (obfd
, amt
);
5361 /* Initialise the fields of the segment map. Set the physical
5362 physical address to the LMA of the first section that has
5363 not yet been assigned. */
5365 map
->p_type
= segment
->p_type
;
5366 map
->p_flags
= segment
->p_flags
;
5367 map
->p_flags_valid
= 1;
5368 map
->p_paddr
= suggested_lma
;
5369 map
->p_paddr_valid
= 1;
5370 map
->includes_filehdr
= 0;
5371 map
->includes_phdrs
= 0;
5374 while (isec
< section_count
);
5379 /* The Solaris linker creates program headers in which all the
5380 p_paddr fields are zero. When we try to objcopy or strip such a
5381 file, we get confused. Check for this case, and if we find it
5382 reset the p_paddr_valid fields. */
5383 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5384 if (map
->p_paddr
!= 0)
5387 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5388 map
->p_paddr_valid
= 0;
5390 elf_tdata (obfd
)->segment_map
= map_first
;
5392 /* If we had to estimate the number of program headers that were
5393 going to be needed, then check our estimate now and adjust
5394 the offset if necessary. */
5395 if (phdr_adjust_seg
!= NULL
)
5399 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5402 if (count
> phdr_adjust_num
)
5403 phdr_adjust_seg
->p_paddr
5404 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5409 #undef IS_CONTAINED_BY_VMA
5410 #undef IS_CONTAINED_BY_LMA
5411 #undef IS_COREFILE_NOTE
5412 #undef IS_SOLARIS_PT_INTERP
5413 #undef INCLUDE_SECTION_IN_SEGMENT
5414 #undef SEGMENT_AFTER_SEGMENT
5415 #undef SEGMENT_OVERLAPS
5419 /* Copy private section information. This copies over the entsize
5420 field, and sometimes the info field. */
5423 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
5428 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5430 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5431 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5434 ihdr
= &elf_section_data (isec
)->this_hdr
;
5435 ohdr
= &elf_section_data (osec
)->this_hdr
;
5437 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
5439 if (ihdr
->sh_type
== SHT_SYMTAB
5440 || ihdr
->sh_type
== SHT_DYNSYM
5441 || ihdr
->sh_type
== SHT_GNU_verneed
5442 || ihdr
->sh_type
== SHT_GNU_verdef
)
5443 ohdr
->sh_info
= ihdr
->sh_info
;
5445 /* Set things up for objcopy. The output SHT_GROUP section will
5446 have its elf_next_in_group pointing back to the input group
5448 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5449 elf_group_name (osec
) = elf_group_name (isec
);
5451 osec
->use_rela_p
= isec
->use_rela_p
;
5456 /* Copy private header information. */
5459 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
5461 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5462 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5465 /* Copy over private BFD data if it has not already been copied.
5466 This must be done here, rather than in the copy_private_bfd_data
5467 entry point, because the latter is called after the section
5468 contents have been set, which means that the program headers have
5469 already been worked out. */
5470 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
5472 if (! copy_private_bfd_data (ibfd
, obfd
))
5479 /* Copy private symbol information. If this symbol is in a section
5480 which we did not map into a BFD section, try to map the section
5481 index correctly. We use special macro definitions for the mapped
5482 section indices; these definitions are interpreted by the
5483 swap_out_syms function. */
5485 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5486 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5487 #define MAP_STRTAB (SHN_HIOS + 3)
5488 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5489 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5492 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
5497 elf_symbol_type
*isym
, *osym
;
5499 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5500 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5503 isym
= elf_symbol_from (ibfd
, isymarg
);
5504 osym
= elf_symbol_from (obfd
, osymarg
);
5508 && bfd_is_abs_section (isym
->symbol
.section
))
5512 shndx
= isym
->internal_elf_sym
.st_shndx
;
5513 if (shndx
== elf_onesymtab (ibfd
))
5514 shndx
= MAP_ONESYMTAB
;
5515 else if (shndx
== elf_dynsymtab (ibfd
))
5516 shndx
= MAP_DYNSYMTAB
;
5517 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
5519 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
5520 shndx
= MAP_SHSTRTAB
;
5521 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
5522 shndx
= MAP_SYM_SHNDX
;
5523 osym
->internal_elf_sym
.st_shndx
= shndx
;
5529 /* Swap out the symbols. */
5532 swap_out_syms (bfd
*abfd
,
5533 struct bfd_strtab_hash
**sttp
,
5536 const struct elf_backend_data
*bed
;
5539 struct bfd_strtab_hash
*stt
;
5540 Elf_Internal_Shdr
*symtab_hdr
;
5541 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5542 Elf_Internal_Shdr
*symstrtab_hdr
;
5543 char *outbound_syms
;
5544 char *outbound_shndx
;
5547 bfd_boolean name_local_sections
;
5549 if (!elf_map_symbols (abfd
))
5552 /* Dump out the symtabs. */
5553 stt
= _bfd_elf_stringtab_init ();
5557 bed
= get_elf_backend_data (abfd
);
5558 symcount
= bfd_get_symcount (abfd
);
5559 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5560 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5561 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5562 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5563 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5564 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
5566 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5567 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5569 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
5570 outbound_syms
= bfd_alloc (abfd
, amt
);
5571 if (outbound_syms
== NULL
)
5573 _bfd_stringtab_free (stt
);
5576 symtab_hdr
->contents
= outbound_syms
;
5578 outbound_shndx
= NULL
;
5579 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5580 if (symtab_shndx_hdr
->sh_name
!= 0)
5582 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5583 outbound_shndx
= bfd_zalloc (abfd
, amt
);
5584 if (outbound_shndx
== NULL
)
5586 _bfd_stringtab_free (stt
);
5590 symtab_shndx_hdr
->contents
= outbound_shndx
;
5591 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5592 symtab_shndx_hdr
->sh_size
= amt
;
5593 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5594 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5597 /* Now generate the data (for "contents"). */
5599 /* Fill in zeroth symbol and swap it out. */
5600 Elf_Internal_Sym sym
;
5606 sym
.st_shndx
= SHN_UNDEF
;
5607 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5608 outbound_syms
+= bed
->s
->sizeof_sym
;
5609 if (outbound_shndx
!= NULL
)
5610 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5614 = (bed
->elf_backend_name_local_section_symbols
5615 && bed
->elf_backend_name_local_section_symbols (abfd
));
5617 syms
= bfd_get_outsymbols (abfd
);
5618 for (idx
= 0; idx
< symcount
; idx
++)
5620 Elf_Internal_Sym sym
;
5621 bfd_vma value
= syms
[idx
]->value
;
5622 elf_symbol_type
*type_ptr
;
5623 flagword flags
= syms
[idx
]->flags
;
5626 if (!name_local_sections
5627 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5629 /* Local section symbols have no name. */
5634 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5637 if (sym
.st_name
== (unsigned long) -1)
5639 _bfd_stringtab_free (stt
);
5644 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5646 if ((flags
& BSF_SECTION_SYM
) == 0
5647 && bfd_is_com_section (syms
[idx
]->section
))
5649 /* ELF common symbols put the alignment into the `value' field,
5650 and the size into the `size' field. This is backwards from
5651 how BFD handles it, so reverse it here. */
5652 sym
.st_size
= value
;
5653 if (type_ptr
== NULL
5654 || type_ptr
->internal_elf_sym
.st_value
== 0)
5655 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5657 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5658 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5659 (abfd
, syms
[idx
]->section
);
5663 asection
*sec
= syms
[idx
]->section
;
5666 if (sec
->output_section
)
5668 value
+= sec
->output_offset
;
5669 sec
= sec
->output_section
;
5672 /* Don't add in the section vma for relocatable output. */
5673 if (! relocatable_p
)
5675 sym
.st_value
= value
;
5676 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5678 if (bfd_is_abs_section (sec
)
5680 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5682 /* This symbol is in a real ELF section which we did
5683 not create as a BFD section. Undo the mapping done
5684 by copy_private_symbol_data. */
5685 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5689 shndx
= elf_onesymtab (abfd
);
5692 shndx
= elf_dynsymtab (abfd
);
5695 shndx
= elf_tdata (abfd
)->strtab_section
;
5698 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5701 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5709 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5715 /* Writing this would be a hell of a lot easier if
5716 we had some decent documentation on bfd, and
5717 knew what to expect of the library, and what to
5718 demand of applications. For example, it
5719 appears that `objcopy' might not set the
5720 section of a symbol to be a section that is
5721 actually in the output file. */
5722 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5725 _bfd_error_handler (_("\
5726 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5727 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
5729 bfd_set_error (bfd_error_invalid_operation
);
5730 _bfd_stringtab_free (stt
);
5734 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5735 BFD_ASSERT (shndx
!= -1);
5739 sym
.st_shndx
= shndx
;
5742 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5744 else if ((flags
& BSF_FUNCTION
) != 0)
5746 else if ((flags
& BSF_OBJECT
) != 0)
5751 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5754 /* Processor-specific types. */
5755 if (type_ptr
!= NULL
5756 && bed
->elf_backend_get_symbol_type
)
5757 type
= ((*bed
->elf_backend_get_symbol_type
)
5758 (&type_ptr
->internal_elf_sym
, type
));
5760 if (flags
& BSF_SECTION_SYM
)
5762 if (flags
& BSF_GLOBAL
)
5763 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5765 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5767 else if (bfd_is_com_section (syms
[idx
]->section
))
5768 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5769 else if (bfd_is_und_section (syms
[idx
]->section
))
5770 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5774 else if (flags
& BSF_FILE
)
5775 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5778 int bind
= STB_LOCAL
;
5780 if (flags
& BSF_LOCAL
)
5782 else if (flags
& BSF_WEAK
)
5784 else if (flags
& BSF_GLOBAL
)
5787 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5790 if (type_ptr
!= NULL
)
5791 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5795 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5796 outbound_syms
+= bed
->s
->sizeof_sym
;
5797 if (outbound_shndx
!= NULL
)
5798 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5802 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5803 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5805 symstrtab_hdr
->sh_flags
= 0;
5806 symstrtab_hdr
->sh_addr
= 0;
5807 symstrtab_hdr
->sh_entsize
= 0;
5808 symstrtab_hdr
->sh_link
= 0;
5809 symstrtab_hdr
->sh_info
= 0;
5810 symstrtab_hdr
->sh_addralign
= 1;
5815 /* Return the number of bytes required to hold the symtab vector.
5817 Note that we base it on the count plus 1, since we will null terminate
5818 the vector allocated based on this size. However, the ELF symbol table
5819 always has a dummy entry as symbol #0, so it ends up even. */
5822 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
5826 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5828 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5829 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5831 symtab_size
-= sizeof (asymbol
*);
5837 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
5841 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5843 if (elf_dynsymtab (abfd
) == 0)
5845 bfd_set_error (bfd_error_invalid_operation
);
5849 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5850 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5852 symtab_size
-= sizeof (asymbol
*);
5858 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
5861 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5864 /* Canonicalize the relocs. */
5867 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
5874 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5876 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
5879 tblptr
= section
->relocation
;
5880 for (i
= 0; i
< section
->reloc_count
; i
++)
5881 *relptr
++ = tblptr
++;
5885 return section
->reloc_count
;
5889 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
5891 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5892 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
5895 bfd_get_symcount (abfd
) = symcount
;
5900 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
5901 asymbol
**allocation
)
5903 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5904 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
5907 bfd_get_dynamic_symcount (abfd
) = symcount
;
5911 /* Return the size required for the dynamic reloc entries. Any loadable
5912 section that was actually installed in the BFD, and has type SHT_REL
5913 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
5914 dynamic reloc section. */
5917 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
5922 if (elf_dynsymtab (abfd
) == 0)
5924 bfd_set_error (bfd_error_invalid_operation
);
5928 ret
= sizeof (arelent
*);
5929 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5930 if ((s
->flags
& SEC_LOAD
) != 0
5931 && elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5932 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5933 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5934 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5935 * sizeof (arelent
*));
5940 /* Canonicalize the dynamic relocation entries. Note that we return the
5941 dynamic relocations as a single block, although they are actually
5942 associated with particular sections; the interface, which was
5943 designed for SunOS style shared libraries, expects that there is only
5944 one set of dynamic relocs. Any loadable section that was actually
5945 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
5946 dynamic symbol table, is considered to be a dynamic reloc section. */
5949 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
5953 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
5957 if (elf_dynsymtab (abfd
) == 0)
5959 bfd_set_error (bfd_error_invalid_operation
);
5963 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5965 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5967 if ((s
->flags
& SEC_LOAD
) != 0
5968 && elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5969 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5970 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5975 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
5977 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5979 for (i
= 0; i
< count
; i
++)
5990 /* Read in the version information. */
5993 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
5995 bfd_byte
*contents
= NULL
;
5997 unsigned int freeidx
= 0;
5999 if (elf_dynverref (abfd
) != 0)
6001 Elf_Internal_Shdr
*hdr
;
6002 Elf_External_Verneed
*everneed
;
6003 Elf_Internal_Verneed
*iverneed
;
6006 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
6008 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
6009 elf_tdata (abfd
)->verref
= bfd_zalloc (abfd
, amt
);
6010 if (elf_tdata (abfd
)->verref
== NULL
)
6013 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
6015 contents
= bfd_malloc (hdr
->sh_size
);
6016 if (contents
== NULL
)
6018 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6019 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6022 everneed
= (Elf_External_Verneed
*) contents
;
6023 iverneed
= elf_tdata (abfd
)->verref
;
6024 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
6026 Elf_External_Vernaux
*evernaux
;
6027 Elf_Internal_Vernaux
*ivernaux
;
6030 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
6032 iverneed
->vn_bfd
= abfd
;
6034 iverneed
->vn_filename
=
6035 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6037 if (iverneed
->vn_filename
== NULL
)
6040 amt
= iverneed
->vn_cnt
;
6041 amt
*= sizeof (Elf_Internal_Vernaux
);
6042 iverneed
->vn_auxptr
= bfd_alloc (abfd
, amt
);
6044 evernaux
= ((Elf_External_Vernaux
*)
6045 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
6046 ivernaux
= iverneed
->vn_auxptr
;
6047 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
6049 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
6051 ivernaux
->vna_nodename
=
6052 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6053 ivernaux
->vna_name
);
6054 if (ivernaux
->vna_nodename
== NULL
)
6057 if (j
+ 1 < iverneed
->vn_cnt
)
6058 ivernaux
->vna_nextptr
= ivernaux
+ 1;
6060 ivernaux
->vna_nextptr
= NULL
;
6062 evernaux
= ((Elf_External_Vernaux
*)
6063 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
6065 if (ivernaux
->vna_other
> freeidx
)
6066 freeidx
= ivernaux
->vna_other
;
6069 if (i
+ 1 < hdr
->sh_info
)
6070 iverneed
->vn_nextref
= iverneed
+ 1;
6072 iverneed
->vn_nextref
= NULL
;
6074 everneed
= ((Elf_External_Verneed
*)
6075 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
6082 if (elf_dynverdef (abfd
) != 0)
6084 Elf_Internal_Shdr
*hdr
;
6085 Elf_External_Verdef
*everdef
;
6086 Elf_Internal_Verdef
*iverdef
;
6087 Elf_Internal_Verdef
*iverdefarr
;
6088 Elf_Internal_Verdef iverdefmem
;
6090 unsigned int maxidx
;
6092 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
6094 contents
= bfd_malloc (hdr
->sh_size
);
6095 if (contents
== NULL
)
6097 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6098 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6101 /* We know the number of entries in the section but not the maximum
6102 index. Therefore we have to run through all entries and find
6104 everdef
= (Elf_External_Verdef
*) contents
;
6106 for (i
= 0; i
< hdr
->sh_info
; ++i
)
6108 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6110 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
6111 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
6113 everdef
= ((Elf_External_Verdef
*)
6114 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
6117 if (default_imported_symver
)
6119 if (freeidx
> maxidx
)
6124 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
6125 elf_tdata (abfd
)->verdef
= bfd_zalloc (abfd
, amt
);
6126 if (elf_tdata (abfd
)->verdef
== NULL
)
6129 elf_tdata (abfd
)->cverdefs
= maxidx
;
6131 everdef
= (Elf_External_Verdef
*) contents
;
6132 iverdefarr
= elf_tdata (abfd
)->verdef
;
6133 for (i
= 0; i
< hdr
->sh_info
; i
++)
6135 Elf_External_Verdaux
*everdaux
;
6136 Elf_Internal_Verdaux
*iverdaux
;
6139 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6141 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
6142 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
6144 iverdef
->vd_bfd
= abfd
;
6146 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
6147 iverdef
->vd_auxptr
= bfd_alloc (abfd
, amt
);
6148 if (iverdef
->vd_auxptr
== NULL
)
6151 everdaux
= ((Elf_External_Verdaux
*)
6152 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
6153 iverdaux
= iverdef
->vd_auxptr
;
6154 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
6156 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
6158 iverdaux
->vda_nodename
=
6159 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6160 iverdaux
->vda_name
);
6161 if (iverdaux
->vda_nodename
== NULL
)
6164 if (j
+ 1 < iverdef
->vd_cnt
)
6165 iverdaux
->vda_nextptr
= iverdaux
+ 1;
6167 iverdaux
->vda_nextptr
= NULL
;
6169 everdaux
= ((Elf_External_Verdaux
*)
6170 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
6173 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
6175 if (i
+ 1 < hdr
->sh_info
)
6176 iverdef
->vd_nextdef
= iverdef
+ 1;
6178 iverdef
->vd_nextdef
= NULL
;
6180 everdef
= ((Elf_External_Verdef
*)
6181 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
6187 else if (default_imported_symver
)
6194 amt
= (bfd_size_type
) freeidx
* sizeof (Elf_Internal_Verdef
);
6195 elf_tdata (abfd
)->verdef
= bfd_zalloc (abfd
, amt
);
6196 if (elf_tdata (abfd
)->verdef
== NULL
)
6199 elf_tdata (abfd
)->cverdefs
= freeidx
;
6202 /* Create a default version based on the soname. */
6203 if (default_imported_symver
)
6205 Elf_Internal_Verdef
*iverdef
;
6206 Elf_Internal_Verdaux
*iverdaux
;
6208 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];;
6210 iverdef
->vd_version
= VER_DEF_CURRENT
;
6211 iverdef
->vd_flags
= 0;
6212 iverdef
->vd_ndx
= freeidx
;
6213 iverdef
->vd_cnt
= 1;
6215 iverdef
->vd_bfd
= abfd
;
6217 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
6218 if (iverdef
->vd_nodename
== NULL
)
6220 iverdef
->vd_nextdef
= NULL
;
6221 amt
= (bfd_size_type
) sizeof (Elf_Internal_Verdaux
);
6222 iverdef
->vd_auxptr
= bfd_alloc (abfd
, amt
);
6224 iverdaux
= iverdef
->vd_auxptr
;
6225 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
6226 iverdaux
->vda_nextptr
= NULL
;
6232 if (contents
!= NULL
)
6238 _bfd_elf_make_empty_symbol (bfd
*abfd
)
6240 elf_symbol_type
*newsym
;
6241 bfd_size_type amt
= sizeof (elf_symbol_type
);
6243 newsym
= bfd_zalloc (abfd
, amt
);
6248 newsym
->symbol
.the_bfd
= abfd
;
6249 return &newsym
->symbol
;
6254 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
6258 bfd_symbol_info (symbol
, ret
);
6261 /* Return whether a symbol name implies a local symbol. Most targets
6262 use this function for the is_local_label_name entry point, but some
6266 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
6269 /* Normal local symbols start with ``.L''. */
6270 if (name
[0] == '.' && name
[1] == 'L')
6273 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6274 DWARF debugging symbols starting with ``..''. */
6275 if (name
[0] == '.' && name
[1] == '.')
6278 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6279 emitting DWARF debugging output. I suspect this is actually a
6280 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6281 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6282 underscore to be emitted on some ELF targets). For ease of use,
6283 we treat such symbols as local. */
6284 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
6291 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
6292 asymbol
*symbol ATTRIBUTE_UNUSED
)
6299 _bfd_elf_set_arch_mach (bfd
*abfd
,
6300 enum bfd_architecture arch
,
6301 unsigned long machine
)
6303 /* If this isn't the right architecture for this backend, and this
6304 isn't the generic backend, fail. */
6305 if (arch
!= get_elf_backend_data (abfd
)->arch
6306 && arch
!= bfd_arch_unknown
6307 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
6310 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
6313 /* Find the function to a particular section and offset,
6314 for error reporting. */
6317 elf_find_function (bfd
*abfd ATTRIBUTE_UNUSED
,
6321 const char **filename_ptr
,
6322 const char **functionname_ptr
)
6324 const char *filename
;
6325 asymbol
*func
, *file
;
6328 /* ??? Given multiple file symbols, it is impossible to reliably
6329 choose the right file name for global symbols. File symbols are
6330 local symbols, and thus all file symbols must sort before any
6331 global symbols. The ELF spec may be interpreted to say that a
6332 file symbol must sort before other local symbols, but currently
6333 ld -r doesn't do this. So, for ld -r output, it is possible to
6334 make a better choice of file name for local symbols by ignoring
6335 file symbols appearing after a given local symbol. */
6336 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
6342 state
= nothing_seen
;
6344 for (p
= symbols
; *p
!= NULL
; p
++)
6348 q
= (elf_symbol_type
*) *p
;
6350 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
6356 if (state
== symbol_seen
)
6357 state
= file_after_symbol_seen
;
6363 if (bfd_get_section (&q
->symbol
) == section
6364 && q
->symbol
.value
>= low_func
6365 && q
->symbol
.value
<= offset
)
6367 func
= (asymbol
*) q
;
6368 low_func
= q
->symbol
.value
;
6371 else if (ELF_ST_BIND (q
->internal_elf_sym
.st_info
) != STB_LOCAL
6372 && state
== file_after_symbol_seen
)
6375 filename
= bfd_asymbol_name (file
);
6379 if (state
== nothing_seen
)
6380 state
= symbol_seen
;
6387 *filename_ptr
= filename
;
6388 if (functionname_ptr
)
6389 *functionname_ptr
= bfd_asymbol_name (func
);
6394 /* Find the nearest line to a particular section and offset,
6395 for error reporting. */
6398 _bfd_elf_find_nearest_line (bfd
*abfd
,
6402 const char **filename_ptr
,
6403 const char **functionname_ptr
,
6404 unsigned int *line_ptr
)
6408 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
6409 filename_ptr
, functionname_ptr
,
6412 if (!*functionname_ptr
)
6413 elf_find_function (abfd
, section
, symbols
, offset
,
6414 *filename_ptr
? NULL
: filename_ptr
,
6420 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
6421 filename_ptr
, functionname_ptr
,
6423 &elf_tdata (abfd
)->dwarf2_find_line_info
))
6425 if (!*functionname_ptr
)
6426 elf_find_function (abfd
, section
, symbols
, offset
,
6427 *filename_ptr
? NULL
: filename_ptr
,
6433 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
6434 &found
, filename_ptr
,
6435 functionname_ptr
, line_ptr
,
6436 &elf_tdata (abfd
)->line_info
))
6438 if (found
&& (*functionname_ptr
|| *line_ptr
))
6441 if (symbols
== NULL
)
6444 if (! elf_find_function (abfd
, section
, symbols
, offset
,
6445 filename_ptr
, functionname_ptr
))
6453 _bfd_elf_sizeof_headers (bfd
*abfd
, bfd_boolean reloc
)
6457 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
6459 ret
+= get_program_header_size (abfd
);
6464 _bfd_elf_set_section_contents (bfd
*abfd
,
6466 const void *location
,
6468 bfd_size_type count
)
6470 Elf_Internal_Shdr
*hdr
;
6473 if (! abfd
->output_has_begun
6474 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6477 hdr
= &elf_section_data (section
)->this_hdr
;
6478 pos
= hdr
->sh_offset
+ offset
;
6479 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6480 || bfd_bwrite (location
, count
, abfd
) != count
)
6487 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
6488 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
6489 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
6494 /* Try to convert a non-ELF reloc into an ELF one. */
6497 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
6499 /* Check whether we really have an ELF howto. */
6501 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
6503 bfd_reloc_code_real_type code
;
6504 reloc_howto_type
*howto
;
6506 /* Alien reloc: Try to determine its type to replace it with an
6507 equivalent ELF reloc. */
6509 if (areloc
->howto
->pc_relative
)
6511 switch (areloc
->howto
->bitsize
)
6514 code
= BFD_RELOC_8_PCREL
;
6517 code
= BFD_RELOC_12_PCREL
;
6520 code
= BFD_RELOC_16_PCREL
;
6523 code
= BFD_RELOC_24_PCREL
;
6526 code
= BFD_RELOC_32_PCREL
;
6529 code
= BFD_RELOC_64_PCREL
;
6535 howto
= bfd_reloc_type_lookup (abfd
, code
);
6537 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
6539 if (howto
->pcrel_offset
)
6540 areloc
->addend
+= areloc
->address
;
6542 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
6547 switch (areloc
->howto
->bitsize
)
6553 code
= BFD_RELOC_14
;
6556 code
= BFD_RELOC_16
;
6559 code
= BFD_RELOC_26
;
6562 code
= BFD_RELOC_32
;
6565 code
= BFD_RELOC_64
;
6571 howto
= bfd_reloc_type_lookup (abfd
, code
);
6575 areloc
->howto
= howto
;
6583 (*_bfd_error_handler
)
6584 (_("%B: unsupported relocation type %s"),
6585 abfd
, areloc
->howto
->name
);
6586 bfd_set_error (bfd_error_bad_value
);
6591 _bfd_elf_close_and_cleanup (bfd
*abfd
)
6593 if (bfd_get_format (abfd
) == bfd_object
)
6595 if (elf_shstrtab (abfd
) != NULL
)
6596 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
6599 return _bfd_generic_close_and_cleanup (abfd
);
6602 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6603 in the relocation's offset. Thus we cannot allow any sort of sanity
6604 range-checking to interfere. There is nothing else to do in processing
6607 bfd_reloc_status_type
6608 _bfd_elf_rel_vtable_reloc_fn
6609 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
6610 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
6611 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
6612 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
6614 return bfd_reloc_ok
;
6617 /* Elf core file support. Much of this only works on native
6618 toolchains, since we rely on knowing the
6619 machine-dependent procfs structure in order to pick
6620 out details about the corefile. */
6622 #ifdef HAVE_SYS_PROCFS_H
6623 # include <sys/procfs.h>
6626 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6629 elfcore_make_pid (bfd
*abfd
)
6631 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
6632 + (elf_tdata (abfd
)->core_pid
));
6635 /* If there isn't a section called NAME, make one, using
6636 data from SECT. Note, this function will generate a
6637 reference to NAME, so you shouldn't deallocate or
6641 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
6645 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
6648 sect2
= bfd_make_section (abfd
, name
);
6652 sect2
->size
= sect
->size
;
6653 sect2
->filepos
= sect
->filepos
;
6654 sect2
->flags
= sect
->flags
;
6655 sect2
->alignment_power
= sect
->alignment_power
;
6659 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6660 actually creates up to two pseudosections:
6661 - For the single-threaded case, a section named NAME, unless
6662 such a section already exists.
6663 - For the multi-threaded case, a section named "NAME/PID", where
6664 PID is elfcore_make_pid (abfd).
6665 Both pseudosections have identical contents. */
6667 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
6673 char *threaded_name
;
6677 /* Build the section name. */
6679 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6680 len
= strlen (buf
) + 1;
6681 threaded_name
= bfd_alloc (abfd
, len
);
6682 if (threaded_name
== NULL
)
6684 memcpy (threaded_name
, buf
, len
);
6686 sect
= bfd_make_section_anyway (abfd
, threaded_name
);
6690 sect
->filepos
= filepos
;
6691 sect
->flags
= SEC_HAS_CONTENTS
;
6692 sect
->alignment_power
= 2;
6694 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6697 /* prstatus_t exists on:
6699 linux 2.[01] + glibc
6703 #if defined (HAVE_PRSTATUS_T)
6706 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6711 if (note
->descsz
== sizeof (prstatus_t
))
6715 size
= sizeof (prstat
.pr_reg
);
6716 offset
= offsetof (prstatus_t
, pr_reg
);
6717 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6719 /* Do not overwrite the core signal if it
6720 has already been set by another thread. */
6721 if (elf_tdata (abfd
)->core_signal
== 0)
6722 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6723 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6725 /* pr_who exists on:
6728 pr_who doesn't exist on:
6731 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6732 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6735 #if defined (HAVE_PRSTATUS32_T)
6736 else if (note
->descsz
== sizeof (prstatus32_t
))
6738 /* 64-bit host, 32-bit corefile */
6739 prstatus32_t prstat
;
6741 size
= sizeof (prstat
.pr_reg
);
6742 offset
= offsetof (prstatus32_t
, pr_reg
);
6743 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6745 /* Do not overwrite the core signal if it
6746 has already been set by another thread. */
6747 if (elf_tdata (abfd
)->core_signal
== 0)
6748 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6749 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6751 /* pr_who exists on:
6754 pr_who doesn't exist on:
6757 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6758 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6761 #endif /* HAVE_PRSTATUS32_T */
6764 /* Fail - we don't know how to handle any other
6765 note size (ie. data object type). */
6769 /* Make a ".reg/999" section and a ".reg" section. */
6770 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6771 size
, note
->descpos
+ offset
);
6773 #endif /* defined (HAVE_PRSTATUS_T) */
6775 /* Create a pseudosection containing the exact contents of NOTE. */
6777 elfcore_make_note_pseudosection (bfd
*abfd
,
6779 Elf_Internal_Note
*note
)
6781 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6782 note
->descsz
, note
->descpos
);
6785 /* There isn't a consistent prfpregset_t across platforms,
6786 but it doesn't matter, because we don't have to pick this
6787 data structure apart. */
6790 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
6792 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6795 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6796 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6800 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
6802 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6805 #if defined (HAVE_PRPSINFO_T)
6806 typedef prpsinfo_t elfcore_psinfo_t
;
6807 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6808 typedef prpsinfo32_t elfcore_psinfo32_t
;
6812 #if defined (HAVE_PSINFO_T)
6813 typedef psinfo_t elfcore_psinfo_t
;
6814 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6815 typedef psinfo32_t elfcore_psinfo32_t
;
6819 /* return a malloc'ed copy of a string at START which is at
6820 most MAX bytes long, possibly without a terminating '\0'.
6821 the copy will always have a terminating '\0'. */
6824 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
6827 char *end
= memchr (start
, '\0', max
);
6835 dups
= bfd_alloc (abfd
, len
+ 1);
6839 memcpy (dups
, start
, len
);
6845 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6847 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
6849 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6851 elfcore_psinfo_t psinfo
;
6853 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6855 elf_tdata (abfd
)->core_program
6856 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6857 sizeof (psinfo
.pr_fname
));
6859 elf_tdata (abfd
)->core_command
6860 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6861 sizeof (psinfo
.pr_psargs
));
6863 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6864 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6866 /* 64-bit host, 32-bit corefile */
6867 elfcore_psinfo32_t psinfo
;
6869 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6871 elf_tdata (abfd
)->core_program
6872 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6873 sizeof (psinfo
.pr_fname
));
6875 elf_tdata (abfd
)->core_command
6876 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6877 sizeof (psinfo
.pr_psargs
));
6883 /* Fail - we don't know how to handle any other
6884 note size (ie. data object type). */
6888 /* Note that for some reason, a spurious space is tacked
6889 onto the end of the args in some (at least one anyway)
6890 implementations, so strip it off if it exists. */
6893 char *command
= elf_tdata (abfd
)->core_command
;
6894 int n
= strlen (command
);
6896 if (0 < n
&& command
[n
- 1] == ' ')
6897 command
[n
- 1] = '\0';
6902 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6904 #if defined (HAVE_PSTATUS_T)
6906 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6908 if (note
->descsz
== sizeof (pstatus_t
)
6909 #if defined (HAVE_PXSTATUS_T)
6910 || note
->descsz
== sizeof (pxstatus_t
)
6916 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6918 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6920 #if defined (HAVE_PSTATUS32_T)
6921 else if (note
->descsz
== sizeof (pstatus32_t
))
6923 /* 64-bit host, 32-bit corefile */
6926 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6928 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6931 /* Could grab some more details from the "representative"
6932 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6933 NT_LWPSTATUS note, presumably. */
6937 #endif /* defined (HAVE_PSTATUS_T) */
6939 #if defined (HAVE_LWPSTATUS_T)
6941 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6943 lwpstatus_t lwpstat
;
6949 if (note
->descsz
!= sizeof (lwpstat
)
6950 #if defined (HAVE_LWPXSTATUS_T)
6951 && note
->descsz
!= sizeof (lwpxstatus_t
)
6956 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6958 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6959 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6961 /* Make a ".reg/999" section. */
6963 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6964 len
= strlen (buf
) + 1;
6965 name
= bfd_alloc (abfd
, len
);
6968 memcpy (name
, buf
, len
);
6970 sect
= bfd_make_section_anyway (abfd
, name
);
6974 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6975 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6976 sect
->filepos
= note
->descpos
6977 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6980 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6981 sect
->size
= sizeof (lwpstat
.pr_reg
);
6982 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6985 sect
->flags
= SEC_HAS_CONTENTS
;
6986 sect
->alignment_power
= 2;
6988 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6991 /* Make a ".reg2/999" section */
6993 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6994 len
= strlen (buf
) + 1;
6995 name
= bfd_alloc (abfd
, len
);
6998 memcpy (name
, buf
, len
);
7000 sect
= bfd_make_section_anyway (abfd
, name
);
7004 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7005 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
7006 sect
->filepos
= note
->descpos
7007 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
7010 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7011 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
7012 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
7015 sect
->flags
= SEC_HAS_CONTENTS
;
7016 sect
->alignment_power
= 2;
7018 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
7020 #endif /* defined (HAVE_LWPSTATUS_T) */
7022 #if defined (HAVE_WIN32_PSTATUS_T)
7024 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7030 win32_pstatus_t pstatus
;
7032 if (note
->descsz
< sizeof (pstatus
))
7035 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
7037 switch (pstatus
.data_type
)
7039 case NOTE_INFO_PROCESS
:
7040 /* FIXME: need to add ->core_command. */
7041 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
7042 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
7045 case NOTE_INFO_THREAD
:
7046 /* Make a ".reg/999" section. */
7047 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
7049 len
= strlen (buf
) + 1;
7050 name
= bfd_alloc (abfd
, len
);
7054 memcpy (name
, buf
, len
);
7056 sect
= bfd_make_section_anyway (abfd
, name
);
7060 sect
->size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
7061 sect
->filepos
= (note
->descpos
7062 + offsetof (struct win32_pstatus
,
7063 data
.thread_info
.thread_context
));
7064 sect
->flags
= SEC_HAS_CONTENTS
;
7065 sect
->alignment_power
= 2;
7067 if (pstatus
.data
.thread_info
.is_active_thread
)
7068 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7072 case NOTE_INFO_MODULE
:
7073 /* Make a ".module/xxxxxxxx" section. */
7074 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
7076 len
= strlen (buf
) + 1;
7077 name
= bfd_alloc (abfd
, len
);
7081 memcpy (name
, buf
, len
);
7083 sect
= bfd_make_section_anyway (abfd
, name
);
7088 sect
->size
= note
->descsz
;
7089 sect
->filepos
= note
->descpos
;
7090 sect
->flags
= SEC_HAS_CONTENTS
;
7091 sect
->alignment_power
= 2;
7100 #endif /* HAVE_WIN32_PSTATUS_T */
7103 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7105 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7113 if (bed
->elf_backend_grok_prstatus
)
7114 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
7116 #if defined (HAVE_PRSTATUS_T)
7117 return elfcore_grok_prstatus (abfd
, note
);
7122 #if defined (HAVE_PSTATUS_T)
7124 return elfcore_grok_pstatus (abfd
, note
);
7127 #if defined (HAVE_LWPSTATUS_T)
7129 return elfcore_grok_lwpstatus (abfd
, note
);
7132 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
7133 return elfcore_grok_prfpreg (abfd
, note
);
7135 #if defined (HAVE_WIN32_PSTATUS_T)
7136 case NT_WIN32PSTATUS
:
7137 return elfcore_grok_win32pstatus (abfd
, note
);
7140 case NT_PRXFPREG
: /* Linux SSE extension */
7141 if (note
->namesz
== 6
7142 && strcmp (note
->namedata
, "LINUX") == 0)
7143 return elfcore_grok_prxfpreg (abfd
, note
);
7149 if (bed
->elf_backend_grok_psinfo
)
7150 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
7152 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7153 return elfcore_grok_psinfo (abfd
, note
);
7160 asection
*sect
= bfd_make_section_anyway (abfd
, ".auxv");
7164 sect
->size
= note
->descsz
;
7165 sect
->filepos
= note
->descpos
;
7166 sect
->flags
= SEC_HAS_CONTENTS
;
7167 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
7175 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
7179 cp
= strchr (note
->namedata
, '@');
7182 *lwpidp
= atoi(cp
+ 1);
7189 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7192 /* Signal number at offset 0x08. */
7193 elf_tdata (abfd
)->core_signal
7194 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
7196 /* Process ID at offset 0x50. */
7197 elf_tdata (abfd
)->core_pid
7198 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
7200 /* Command name at 0x7c (max 32 bytes, including nul). */
7201 elf_tdata (abfd
)->core_command
7202 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
7204 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
7209 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7213 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
7214 elf_tdata (abfd
)->core_lwpid
= lwp
;
7216 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
7218 /* NetBSD-specific core "procinfo". Note that we expect to
7219 find this note before any of the others, which is fine,
7220 since the kernel writes this note out first when it
7221 creates a core file. */
7223 return elfcore_grok_netbsd_procinfo (abfd
, note
);
7226 /* As of Jan 2002 there are no other machine-independent notes
7227 defined for NetBSD core files. If the note type is less
7228 than the start of the machine-dependent note types, we don't
7231 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
7235 switch (bfd_get_arch (abfd
))
7237 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7238 PT_GETFPREGS == mach+2. */
7240 case bfd_arch_alpha
:
7241 case bfd_arch_sparc
:
7244 case NT_NETBSDCORE_FIRSTMACH
+0:
7245 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
7247 case NT_NETBSDCORE_FIRSTMACH
+2:
7248 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7254 /* On all other arch's, PT_GETREGS == mach+1 and
7255 PT_GETFPREGS == mach+3. */
7260 case NT_NETBSDCORE_FIRSTMACH
+1:
7261 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
7263 case NT_NETBSDCORE_FIRSTMACH
+3:
7264 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7274 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, pid_t
*tid
)
7276 void *ddata
= note
->descdata
;
7283 /* nto_procfs_status 'pid' field is at offset 0. */
7284 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
7286 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7287 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
7289 /* nto_procfs_status 'flags' field is at offset 8. */
7290 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
7292 /* nto_procfs_status 'what' field is at offset 14. */
7293 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
7295 elf_tdata (abfd
)->core_signal
= sig
;
7296 elf_tdata (abfd
)->core_lwpid
= *tid
;
7299 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7300 do not come from signals so we make sure we set the current
7301 thread just in case. */
7302 if (flags
& 0x00000080)
7303 elf_tdata (abfd
)->core_lwpid
= *tid
;
7305 /* Make a ".qnx_core_status/%d" section. */
7306 sprintf (buf
, ".qnx_core_status/%d", *tid
);
7308 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
7313 sect
= bfd_make_section_anyway (abfd
, name
);
7317 sect
->size
= note
->descsz
;
7318 sect
->filepos
= note
->descpos
;
7319 sect
->flags
= SEC_HAS_CONTENTS
;
7320 sect
->alignment_power
= 2;
7322 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
7326 elfcore_grok_nto_regs (bfd
*abfd
,
7327 Elf_Internal_Note
*note
,
7335 /* Make a "(base)/%d" section. */
7336 sprintf (buf
, "%s/%d", base
, tid
);
7338 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
7343 sect
= bfd_make_section_anyway (abfd
, name
);
7347 sect
->size
= note
->descsz
;
7348 sect
->filepos
= note
->descpos
;
7349 sect
->flags
= SEC_HAS_CONTENTS
;
7350 sect
->alignment_power
= 2;
7352 /* This is the current thread. */
7353 if (elf_tdata (abfd
)->core_lwpid
== tid
)
7354 return elfcore_maybe_make_sect (abfd
, base
, sect
);
7359 #define BFD_QNT_CORE_INFO 7
7360 #define BFD_QNT_CORE_STATUS 8
7361 #define BFD_QNT_CORE_GREG 9
7362 #define BFD_QNT_CORE_FPREG 10
7365 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7367 /* Every GREG section has a STATUS section before it. Store the
7368 tid from the previous call to pass down to the next gregs
7370 static pid_t tid
= 1;
7374 case BFD_QNT_CORE_INFO
:
7375 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
7376 case BFD_QNT_CORE_STATUS
:
7377 return elfcore_grok_nto_status (abfd
, note
, &tid
);
7378 case BFD_QNT_CORE_GREG
:
7379 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
7380 case BFD_QNT_CORE_FPREG
:
7381 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
7387 /* Function: elfcore_write_note
7394 size of data for note
7397 End of buffer containing note. */
7400 elfcore_write_note (bfd
*abfd
,
7408 Elf_External_Note
*xnp
;
7418 const struct elf_backend_data
*bed
;
7420 namesz
= strlen (name
) + 1;
7421 bed
= get_elf_backend_data (abfd
);
7422 pad
= -namesz
& ((1 << bed
->s
->log_file_align
) - 1);
7425 newspace
= 12 + namesz
+ pad
+ size
;
7427 p
= realloc (buf
, *bufsiz
+ newspace
);
7429 *bufsiz
+= newspace
;
7430 xnp
= (Elf_External_Note
*) dest
;
7431 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
7432 H_PUT_32 (abfd
, size
, xnp
->descsz
);
7433 H_PUT_32 (abfd
, type
, xnp
->type
);
7437 memcpy (dest
, name
, namesz
);
7445 memcpy (dest
, input
, size
);
7449 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7451 elfcore_write_prpsinfo (bfd
*abfd
,
7458 char *note_name
= "CORE";
7460 #if defined (HAVE_PSINFO_T)
7462 note_type
= NT_PSINFO
;
7465 note_type
= NT_PRPSINFO
;
7468 memset (&data
, 0, sizeof (data
));
7469 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
7470 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
7471 return elfcore_write_note (abfd
, buf
, bufsiz
,
7472 note_name
, note_type
, &data
, sizeof (data
));
7474 #endif /* PSINFO_T or PRPSINFO_T */
7476 #if defined (HAVE_PRSTATUS_T)
7478 elfcore_write_prstatus (bfd
*abfd
,
7486 char *note_name
= "CORE";
7488 memset (&prstat
, 0, sizeof (prstat
));
7489 prstat
.pr_pid
= pid
;
7490 prstat
.pr_cursig
= cursig
;
7491 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
7492 return elfcore_write_note (abfd
, buf
, bufsiz
,
7493 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
7495 #endif /* HAVE_PRSTATUS_T */
7497 #if defined (HAVE_LWPSTATUS_T)
7499 elfcore_write_lwpstatus (bfd
*abfd
,
7506 lwpstatus_t lwpstat
;
7507 char *note_name
= "CORE";
7509 memset (&lwpstat
, 0, sizeof (lwpstat
));
7510 lwpstat
.pr_lwpid
= pid
>> 16;
7511 lwpstat
.pr_cursig
= cursig
;
7512 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7513 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
7514 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7516 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
7517 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
7519 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
7520 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
7523 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7524 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
7526 #endif /* HAVE_LWPSTATUS_T */
7528 #if defined (HAVE_PSTATUS_T)
7530 elfcore_write_pstatus (bfd
*abfd
,
7538 char *note_name
= "CORE";
7540 memset (&pstat
, 0, sizeof (pstat
));
7541 pstat
.pr_pid
= pid
& 0xffff;
7542 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7543 NT_PSTATUS
, &pstat
, sizeof (pstat
));
7546 #endif /* HAVE_PSTATUS_T */
7549 elfcore_write_prfpreg (bfd
*abfd
,
7555 char *note_name
= "CORE";
7556 return elfcore_write_note (abfd
, buf
, bufsiz
,
7557 note_name
, NT_FPREGSET
, fpregs
, size
);
7561 elfcore_write_prxfpreg (bfd
*abfd
,
7564 const void *xfpregs
,
7567 char *note_name
= "LINUX";
7568 return elfcore_write_note (abfd
, buf
, bufsiz
,
7569 note_name
, NT_PRXFPREG
, xfpregs
, size
);
7573 elfcore_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
7581 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
7584 buf
= bfd_malloc (size
);
7588 if (bfd_bread (buf
, size
, abfd
) != size
)
7596 while (p
< buf
+ size
)
7598 /* FIXME: bad alignment assumption. */
7599 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
7600 Elf_Internal_Note in
;
7602 in
.type
= H_GET_32 (abfd
, xnp
->type
);
7604 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
7605 in
.namedata
= xnp
->name
;
7607 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
7608 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
7609 in
.descpos
= offset
+ (in
.descdata
- buf
);
7611 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
7613 if (! elfcore_grok_netbsd_note (abfd
, &in
))
7616 else if (strncmp (in
.namedata
, "QNX", 3) == 0)
7618 if (! elfcore_grok_nto_note (abfd
, &in
))
7623 if (! elfcore_grok_note (abfd
, &in
))
7627 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
7634 /* Providing external access to the ELF program header table. */
7636 /* Return an upper bound on the number of bytes required to store a
7637 copy of ABFD's program header table entries. Return -1 if an error
7638 occurs; bfd_get_error will return an appropriate code. */
7641 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
7643 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7645 bfd_set_error (bfd_error_wrong_format
);
7649 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
7652 /* Copy ABFD's program header table entries to *PHDRS. The entries
7653 will be stored as an array of Elf_Internal_Phdr structures, as
7654 defined in include/elf/internal.h. To find out how large the
7655 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7657 Return the number of program header table entries read, or -1 if an
7658 error occurs; bfd_get_error will return an appropriate code. */
7661 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
7665 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7667 bfd_set_error (bfd_error_wrong_format
);
7671 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
7672 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
7673 num_phdrs
* sizeof (Elf_Internal_Phdr
));
7679 _bfd_elf_sprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, char *buf
, bfd_vma value
)
7682 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7684 i_ehdrp
= elf_elfheader (abfd
);
7685 if (i_ehdrp
== NULL
)
7686 sprintf_vma (buf
, value
);
7689 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7691 #if BFD_HOST_64BIT_LONG
7692 sprintf (buf
, "%016lx", value
);
7694 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
7695 _bfd_int64_low (value
));
7699 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
7702 sprintf_vma (buf
, value
);
7707 _bfd_elf_fprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, void *stream
, bfd_vma value
)
7710 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7712 i_ehdrp
= elf_elfheader (abfd
);
7713 if (i_ehdrp
== NULL
)
7714 fprintf_vma ((FILE *) stream
, value
);
7717 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7719 #if BFD_HOST_64BIT_LONG
7720 fprintf ((FILE *) stream
, "%016lx", value
);
7722 fprintf ((FILE *) stream
, "%08lx%08lx",
7723 _bfd_int64_high (value
), _bfd_int64_low (value
));
7727 fprintf ((FILE *) stream
, "%08lx",
7728 (unsigned long) (value
& 0xffffffff));
7731 fprintf_vma ((FILE *) stream
, value
);
7735 enum elf_reloc_type_class
7736 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
7738 return reloc_class_normal
;
7741 /* For RELA architectures, return the relocation value for a
7742 relocation against a local symbol. */
7745 _bfd_elf_rela_local_sym (bfd
*abfd
,
7746 Elf_Internal_Sym
*sym
,
7748 Elf_Internal_Rela
*rel
)
7750 asection
*sec
= *psec
;
7753 relocation
= (sec
->output_section
->vma
7754 + sec
->output_offset
7756 if ((sec
->flags
& SEC_MERGE
)
7757 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
7758 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
7761 _bfd_merged_section_offset (abfd
, psec
,
7762 elf_section_data (sec
)->sec_info
,
7763 sym
->st_value
+ rel
->r_addend
);
7766 /* If we have changed the section, and our original section is
7767 marked with SEC_EXCLUDE, it means that the original
7768 SEC_MERGE section has been completely subsumed in some
7769 other SEC_MERGE section. In this case, we need to leave
7770 some info around for --emit-relocs. */
7771 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
7772 sec
->kept_section
= *psec
;
7775 rel
->r_addend
-= relocation
;
7776 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
7782 _bfd_elf_rel_local_sym (bfd
*abfd
,
7783 Elf_Internal_Sym
*sym
,
7787 asection
*sec
= *psec
;
7789 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7790 return sym
->st_value
+ addend
;
7792 return _bfd_merged_section_offset (abfd
, psec
,
7793 elf_section_data (sec
)->sec_info
,
7794 sym
->st_value
+ addend
);
7798 _bfd_elf_section_offset (bfd
*abfd
,
7799 struct bfd_link_info
*info
,
7803 switch (sec
->sec_info_type
)
7805 case ELF_INFO_TYPE_STABS
:
7806 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
7808 case ELF_INFO_TYPE_EH_FRAME
:
7809 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
7815 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7816 reconstruct an ELF file by reading the segments out of remote memory
7817 based on the ELF file header at EHDR_VMA and the ELF program headers it
7818 points to. If not null, *LOADBASEP is filled in with the difference
7819 between the VMAs from which the segments were read, and the VMAs the
7820 file headers (and hence BFD's idea of each section's VMA) put them at.
7822 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7823 remote memory at target address VMA into the local buffer at MYADDR; it
7824 should return zero on success or an `errno' code on failure. TEMPL must
7825 be a BFD for an ELF target with the word size and byte order found in
7826 the remote memory. */
7829 bfd_elf_bfd_from_remote_memory
7833 int (*target_read_memory
) (bfd_vma
, char *, int))
7835 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
7836 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
7840 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
7841 long symcount ATTRIBUTE_UNUSED
,
7842 asymbol
**syms ATTRIBUTE_UNUSED
,
7847 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7850 const char *relplt_name
;
7851 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7855 Elf_Internal_Shdr
*hdr
;
7861 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
7864 if (dynsymcount
<= 0)
7867 if (!bed
->plt_sym_val
)
7870 relplt_name
= bed
->relplt_name
;
7871 if (relplt_name
== NULL
)
7872 relplt_name
= bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt";
7873 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
7877 hdr
= &elf_section_data (relplt
)->this_hdr
;
7878 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
7879 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
7882 plt
= bfd_get_section_by_name (abfd
, ".plt");
7886 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7887 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
7890 count
= relplt
->size
/ hdr
->sh_entsize
;
7891 size
= count
* sizeof (asymbol
);
7892 p
= relplt
->relocation
;
7893 for (i
= 0; i
< count
; i
++, s
++, p
++)
7894 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
7896 s
= *ret
= bfd_malloc (size
);
7900 names
= (char *) (s
+ count
);
7901 p
= relplt
->relocation
;
7903 for (i
= 0; i
< count
; i
++, s
++, p
++)
7908 addr
= bed
->plt_sym_val (i
, plt
, p
);
7909 if (addr
== (bfd_vma
) -1)
7912 *s
= **p
->sym_ptr_ptr
;
7914 s
->value
= addr
- plt
->vma
;
7916 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
7917 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
7919 memcpy (names
, "@plt", sizeof ("@plt"));
7920 names
+= sizeof ("@plt");
7927 /* Sort symbol by binding and section. We want to put definitions
7928 sorted by section at the beginning. */
7931 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7933 const Elf_Internal_Sym
*s1
;
7934 const Elf_Internal_Sym
*s2
;
7937 /* Make sure that undefined symbols are at the end. */
7938 s1
= (const Elf_Internal_Sym
*) arg1
;
7939 if (s1
->st_shndx
== SHN_UNDEF
)
7941 s2
= (const Elf_Internal_Sym
*) arg2
;
7942 if (s2
->st_shndx
== SHN_UNDEF
)
7945 /* Sorted by section index. */
7946 shndx
= s1
->st_shndx
- s2
->st_shndx
;
7950 /* Sorted by binding. */
7951 return ELF_ST_BIND (s1
->st_info
) - ELF_ST_BIND (s2
->st_info
);
7956 Elf_Internal_Sym
*sym
;
7961 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7963 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7964 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7965 return strcmp (s1
->name
, s2
->name
);
7968 /* Check if 2 sections define the same set of local and global
7972 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
)
7975 const struct elf_backend_data
*bed1
, *bed2
;
7976 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7977 bfd_size_type symcount1
, symcount2
;
7978 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7979 Elf_Internal_Sym
*isymstart1
= NULL
, *isymstart2
= NULL
, *isym
;
7980 Elf_Internal_Sym
*isymend
;
7981 struct elf_symbol
*symp
, *symtable1
= NULL
, *symtable2
= NULL
;
7982 bfd_size_type count1
, count2
, i
;
7989 /* If both are .gnu.linkonce sections, they have to have the same
7991 if (strncmp (sec1
->name
, ".gnu.linkonce",
7992 sizeof ".gnu.linkonce" - 1) == 0
7993 && strncmp (sec2
->name
, ".gnu.linkonce",
7994 sizeof ".gnu.linkonce" - 1) == 0)
7995 return strcmp (sec1
->name
+ sizeof ".gnu.linkonce",
7996 sec2
->name
+ sizeof ".gnu.linkonce") == 0;
7998 /* Both sections have to be in ELF. */
7999 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
8000 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
8003 if (elf_section_type (sec1
) != elf_section_type (sec2
))
8006 if ((elf_section_flags (sec1
) & SHF_GROUP
) != 0
8007 && (elf_section_flags (sec2
) & SHF_GROUP
) != 0)
8009 /* If both are members of section groups, they have to have the
8011 if (strcmp (elf_group_name (sec1
), elf_group_name (sec2
)) != 0)
8015 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
8016 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
8017 if (shndx1
== -1 || shndx2
== -1)
8020 bed1
= get_elf_backend_data (bfd1
);
8021 bed2
= get_elf_backend_data (bfd2
);
8022 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
8023 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
8024 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
8025 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
8027 if (symcount1
== 0 || symcount2
== 0)
8030 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
8032 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
8036 if (isymbuf1
== NULL
|| isymbuf2
== NULL
)
8039 /* Sort symbols by binding and section. Global definitions are at
8041 qsort (isymbuf1
, symcount1
, sizeof (Elf_Internal_Sym
),
8042 elf_sort_elf_symbol
);
8043 qsort (isymbuf2
, symcount2
, sizeof (Elf_Internal_Sym
),
8044 elf_sort_elf_symbol
);
8046 /* Count definitions in the section. */
8048 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
;
8049 isym
< isymend
; isym
++)
8051 if (isym
->st_shndx
== (unsigned int) shndx1
)
8058 if (count1
&& isym
->st_shndx
!= (unsigned int) shndx1
)
8063 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
;
8064 isym
< isymend
; isym
++)
8066 if (isym
->st_shndx
== (unsigned int) shndx2
)
8073 if (count2
&& isym
->st_shndx
!= (unsigned int) shndx2
)
8077 if (count1
== 0 || count2
== 0 || count1
!= count2
)
8080 symtable1
= bfd_malloc (count1
* sizeof (struct elf_symbol
));
8081 symtable2
= bfd_malloc (count1
* sizeof (struct elf_symbol
));
8083 if (symtable1
== NULL
|| symtable2
== NULL
)
8087 for (isym
= isymstart1
, isymend
= isym
+ count1
;
8088 isym
< isymend
; isym
++)
8091 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
8098 for (isym
= isymstart2
, isymend
= isym
+ count1
;
8099 isym
< isymend
; isym
++)
8102 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
8108 /* Sort symbol by name. */
8109 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
8110 elf_sym_name_compare
);
8111 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
8112 elf_sym_name_compare
);
8114 for (i
= 0; i
< count1
; i
++)
8115 /* Two symbols must have the same binding, type and name. */
8116 if (symtable1
[i
].sym
->st_info
!= symtable2
[i
].sym
->st_info
8117 || symtable1
[i
].sym
->st_other
!= symtable2
[i
].sym
->st_other
8118 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)