1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
31 haven't bothered yet. */
33 /* For sparc64-cross-sparc32. */
41 #include "libiberty.h"
43 static INLINE
struct elf_segment_map
*make_mapping
44 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, bfd_boolean
));
45 static bfd_boolean map_sections_to_segments
47 static int elf_sort_sections
48 PARAMS ((const PTR
, const PTR
));
49 static bfd_boolean assign_file_positions_for_segments
51 static bfd_boolean assign_file_positions_except_relocs
53 static bfd_boolean prep_headers
55 static bfd_boolean swap_out_syms
56 PARAMS ((bfd
*, struct bfd_strtab_hash
**, int));
57 static bfd_boolean copy_private_bfd_data
58 PARAMS ((bfd
*, bfd
*));
60 PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
61 static const char *group_signature
62 PARAMS ((bfd
*, Elf_Internal_Shdr
*));
63 static bfd_boolean setup_group
64 PARAMS ((bfd
*, Elf_Internal_Shdr
*, asection
*));
65 static void merge_sections_remove_hook
66 PARAMS ((bfd
*, asection
*));
67 static void elf_fake_sections
68 PARAMS ((bfd
*, asection
*, PTR
));
69 static bfd_boolean assign_section_numbers
71 static INLINE
int sym_is_global
72 PARAMS ((bfd
*, asymbol
*));
73 static bfd_boolean elf_map_symbols
75 static bfd_size_type get_program_header_size
77 static bfd_boolean elfcore_read_notes
78 PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
79 static bfd_boolean elf_find_function
80 PARAMS ((bfd
*, asection
*, asymbol
**, bfd_vma
, const char **,
82 static int elfcore_make_pid
84 static bfd_boolean elfcore_maybe_make_sect
85 PARAMS ((bfd
*, char *, asection
*));
86 static bfd_boolean elfcore_make_note_pseudosection
87 PARAMS ((bfd
*, char *, Elf_Internal_Note
*));
88 static bfd_boolean elfcore_grok_prfpreg
89 PARAMS ((bfd
*, Elf_Internal_Note
*));
90 static bfd_boolean elfcore_grok_prxfpreg
91 PARAMS ((bfd
*, Elf_Internal_Note
*));
92 static bfd_boolean elfcore_grok_note
93 PARAMS ((bfd
*, Elf_Internal_Note
*));
94 static bfd_boolean elfcore_netbsd_get_lwpid
95 PARAMS ((Elf_Internal_Note
*, int *));
96 static bfd_boolean elfcore_grok_netbsd_procinfo
97 PARAMS ((bfd
*, Elf_Internal_Note
*));
98 static bfd_boolean elfcore_grok_netbsd_note
99 PARAMS ((bfd
*, Elf_Internal_Note
*));
101 /* Swap version information in and out. The version information is
102 currently size independent. If that ever changes, this code will
103 need to move into elfcode.h. */
105 /* Swap in a Verdef structure. */
108 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
110 const Elf_External_Verdef
*src
;
111 Elf_Internal_Verdef
*dst
;
113 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
114 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
115 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
116 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
117 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
118 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
119 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
122 /* Swap out a Verdef structure. */
125 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
127 const Elf_Internal_Verdef
*src
;
128 Elf_External_Verdef
*dst
;
130 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
131 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
132 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
133 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
134 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
135 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
136 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
139 /* Swap in a Verdaux structure. */
142 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
144 const Elf_External_Verdaux
*src
;
145 Elf_Internal_Verdaux
*dst
;
147 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
148 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
151 /* Swap out a Verdaux structure. */
154 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
156 const Elf_Internal_Verdaux
*src
;
157 Elf_External_Verdaux
*dst
;
159 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
160 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
163 /* Swap in a Verneed structure. */
166 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
168 const Elf_External_Verneed
*src
;
169 Elf_Internal_Verneed
*dst
;
171 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
172 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
173 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
174 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
175 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
178 /* Swap out a Verneed structure. */
181 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
183 const Elf_Internal_Verneed
*src
;
184 Elf_External_Verneed
*dst
;
186 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
187 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
188 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
189 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
190 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
193 /* Swap in a Vernaux structure. */
196 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
198 const Elf_External_Vernaux
*src
;
199 Elf_Internal_Vernaux
*dst
;
201 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
202 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
203 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
204 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
205 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
208 /* Swap out a Vernaux structure. */
211 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
213 const Elf_Internal_Vernaux
*src
;
214 Elf_External_Vernaux
*dst
;
216 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
217 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
218 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
219 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
220 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
223 /* Swap in a Versym structure. */
226 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
228 const Elf_External_Versym
*src
;
229 Elf_Internal_Versym
*dst
;
231 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
234 /* Swap out a Versym structure. */
237 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
239 const Elf_Internal_Versym
*src
;
240 Elf_External_Versym
*dst
;
242 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
245 /* Standard ELF hash function. Do not change this function; you will
246 cause invalid hash tables to be generated. */
249 bfd_elf_hash (namearg
)
252 const unsigned char *name
= (const unsigned char *) namearg
;
257 while ((ch
= *name
++) != '\0')
260 if ((g
= (h
& 0xf0000000)) != 0)
263 /* The ELF ABI says `h &= ~g', but this is equivalent in
264 this case and on some machines one insn instead of two. */
271 /* Read a specified number of bytes at a specified offset in an ELF
272 file, into a newly allocated buffer, and return a pointer to the
276 elf_read (abfd
, offset
, size
)
283 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
285 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
287 if (bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
289 if (bfd_get_error () != bfd_error_system_call
)
290 bfd_set_error (bfd_error_file_truncated
);
297 bfd_elf_mkobject (abfd
)
300 /* This just does initialization. */
301 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
302 bfd_size_type amt
= sizeof (struct elf_obj_tdata
);
303 elf_tdata (abfd
) = (struct elf_obj_tdata
*) bfd_zalloc (abfd
, amt
);
304 if (elf_tdata (abfd
) == 0)
306 /* Since everything is done at close time, do we need any
313 bfd_elf_mkcorefile (abfd
)
316 /* I think this can be done just like an object file. */
317 return bfd_elf_mkobject (abfd
);
321 bfd_elf_get_str_section (abfd
, shindex
)
323 unsigned int shindex
;
325 Elf_Internal_Shdr
**i_shdrp
;
326 char *shstrtab
= NULL
;
328 bfd_size_type shstrtabsize
;
330 i_shdrp
= elf_elfsections (abfd
);
331 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
334 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
335 if (shstrtab
== NULL
)
337 /* No cached one, attempt to read, and cache what we read. */
338 offset
= i_shdrp
[shindex
]->sh_offset
;
339 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
340 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
341 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
347 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
349 unsigned int shindex
;
350 unsigned int strindex
;
352 Elf_Internal_Shdr
*hdr
;
357 hdr
= elf_elfsections (abfd
)[shindex
];
359 if (hdr
->contents
== NULL
360 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
363 if (strindex
>= hdr
->sh_size
)
365 (*_bfd_error_handler
)
366 (_("%s: invalid string offset %u >= %lu for section `%s'"),
367 bfd_archive_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
368 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
369 && strindex
== hdr
->sh_name
)
371 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
375 return ((char *) hdr
->contents
) + strindex
;
378 /* Read and convert symbols to internal format.
379 SYMCOUNT specifies the number of symbols to read, starting from
380 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
381 are non-NULL, they are used to store the internal symbols, external
382 symbols, and symbol section index extensions, respectively. */
385 bfd_elf_get_elf_syms (ibfd
, symtab_hdr
, symcount
, symoffset
,
386 intsym_buf
, extsym_buf
, extshndx_buf
)
388 Elf_Internal_Shdr
*symtab_hdr
;
391 Elf_Internal_Sym
*intsym_buf
;
393 Elf_External_Sym_Shndx
*extshndx_buf
;
395 Elf_Internal_Shdr
*shndx_hdr
;
397 const bfd_byte
*esym
;
398 Elf_External_Sym_Shndx
*alloc_extshndx
;
399 Elf_External_Sym_Shndx
*shndx
;
400 Elf_Internal_Sym
*isym
;
401 Elf_Internal_Sym
*isymend
;
402 struct elf_backend_data
*bed
;
410 /* Normal syms might have section extension entries. */
412 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
413 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
415 /* Read the symbols. */
417 alloc_extshndx
= NULL
;
418 bed
= get_elf_backend_data (ibfd
);
419 extsym_size
= bed
->s
->sizeof_sym
;
420 amt
= symcount
* extsym_size
;
421 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
422 if (extsym_buf
== NULL
)
424 alloc_ext
= bfd_malloc (amt
);
425 extsym_buf
= alloc_ext
;
427 if (extsym_buf
== NULL
428 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
429 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
435 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
439 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
440 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
441 if (extshndx_buf
== NULL
)
443 alloc_extshndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
444 extshndx_buf
= alloc_extshndx
;
446 if (extshndx_buf
== NULL
447 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
448 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
455 if (intsym_buf
== NULL
)
457 bfd_size_type amt
= symcount
* sizeof (Elf_Internal_Sym
);
458 intsym_buf
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
459 if (intsym_buf
== NULL
)
463 /* Convert the symbols to internal form. */
464 isymend
= intsym_buf
+ symcount
;
465 for (esym
= extsym_buf
, isym
= intsym_buf
, shndx
= extshndx_buf
;
467 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
468 (*bed
->s
->swap_symbol_in
) (ibfd
, esym
, (const PTR
) shndx
, isym
);
471 if (alloc_ext
!= NULL
)
473 if (alloc_extshndx
!= NULL
)
474 free (alloc_extshndx
);
479 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
480 sections. The first element is the flags, the rest are section
483 typedef union elf_internal_group
{
484 Elf_Internal_Shdr
*shdr
;
486 } Elf_Internal_Group
;
488 /* Return the name of the group signature symbol. Why isn't the
489 signature just a string? */
492 group_signature (abfd
, ghdr
)
494 Elf_Internal_Shdr
*ghdr
;
496 Elf_Internal_Shdr
*hdr
;
497 unsigned char esym
[sizeof (Elf64_External_Sym
)];
498 Elf_External_Sym_Shndx eshndx
;
499 Elf_Internal_Sym isym
;
501 unsigned int shindex
;
503 /* First we need to ensure the symbol table is available. */
504 if (! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
507 /* Go read the symbol. */
508 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
509 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
510 &isym
, esym
, &eshndx
) == NULL
)
513 /* Look up the symbol name. */
514 iname
= isym
.st_name
;
515 shindex
= hdr
->sh_link
;
516 if (iname
== 0 && ELF_ST_TYPE (isym
.st_info
) == STT_SECTION
)
518 iname
= elf_elfsections (abfd
)[isym
.st_shndx
]->sh_name
;
519 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
522 return bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
525 /* Set next_in_group list pointer, and group name for NEWSECT. */
528 setup_group (abfd
, hdr
, newsect
)
530 Elf_Internal_Shdr
*hdr
;
533 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
535 /* If num_group is zero, read in all SHT_GROUP sections. The count
536 is set to -1 if there are no SHT_GROUP sections. */
539 unsigned int i
, shnum
;
541 /* First count the number of groups. If we have a SHT_GROUP
542 section with just a flag word (ie. sh_size is 4), ignore it. */
543 shnum
= elf_numsections (abfd
);
545 for (i
= 0; i
< shnum
; i
++)
547 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
548 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
553 num_group
= (unsigned) -1;
554 elf_tdata (abfd
)->num_group
= num_group
;
558 /* We keep a list of elf section headers for group sections,
559 so we can find them quickly. */
560 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
561 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
562 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
566 for (i
= 0; i
< shnum
; i
++)
568 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
569 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
572 Elf_Internal_Group
*dest
;
574 /* Add to list of sections. */
575 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
578 /* Read the raw contents. */
579 BFD_ASSERT (sizeof (*dest
) >= 4);
580 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
581 shdr
->contents
= bfd_alloc (abfd
, amt
);
582 if (shdr
->contents
== NULL
583 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
584 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
588 /* Translate raw contents, a flag word followed by an
589 array of elf section indices all in target byte order,
590 to the flag word followed by an array of elf section
592 src
= shdr
->contents
+ shdr
->sh_size
;
593 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
600 idx
= H_GET_32 (abfd
, src
);
601 if (src
== shdr
->contents
)
604 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
605 shdr
->bfd_section
->flags
606 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
611 ((*_bfd_error_handler
)
612 (_("%s: invalid SHT_GROUP entry"),
613 bfd_archive_filename (abfd
)));
616 dest
->shdr
= elf_elfsections (abfd
)[idx
];
623 if (num_group
!= (unsigned) -1)
627 for (i
= 0; i
< num_group
; i
++)
629 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
630 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
631 unsigned int n_elt
= shdr
->sh_size
/ 4;
633 /* Look through this group's sections to see if current
634 section is a member. */
636 if ((++idx
)->shdr
== hdr
)
640 /* We are a member of this group. Go looking through
641 other members to see if any others are linked via
643 idx
= (Elf_Internal_Group
*) shdr
->contents
;
644 n_elt
= shdr
->sh_size
/ 4;
646 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
647 && elf_next_in_group (s
) != NULL
)
651 /* Snarf the group name from other member, and
652 insert current section in circular list. */
653 elf_group_name (newsect
) = elf_group_name (s
);
654 elf_next_in_group (newsect
) = elf_next_in_group (s
);
655 elf_next_in_group (s
) = newsect
;
661 gname
= group_signature (abfd
, shdr
);
664 elf_group_name (newsect
) = gname
;
666 /* Start a circular list with one element. */
667 elf_next_in_group (newsect
) = newsect
;
670 /* If the group section has been created, point to the
672 if (shdr
->bfd_section
!= NULL
)
673 elf_next_in_group (shdr
->bfd_section
) = newsect
;
681 if (elf_group_name (newsect
) == NULL
)
683 (*_bfd_error_handler
) (_("%s: no group info for section %s"),
684 bfd_archive_filename (abfd
), newsect
->name
);
690 bfd_elf_discard_group (abfd
, group
)
691 bfd
*abfd ATTRIBUTE_UNUSED
;
694 asection
*first
= elf_next_in_group (group
);
699 s
->output_section
= bfd_abs_section_ptr
;
700 s
= elf_next_in_group (s
);
701 /* These lists are circular. */
708 /* Make a BFD section from an ELF section. We store a pointer to the
709 BFD section in the bfd_section field of the header. */
712 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
714 Elf_Internal_Shdr
*hdr
;
719 struct elf_backend_data
*bed
;
721 if (hdr
->bfd_section
!= NULL
)
723 BFD_ASSERT (strcmp (name
,
724 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
728 newsect
= bfd_make_section_anyway (abfd
, name
);
732 newsect
->filepos
= hdr
->sh_offset
;
734 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
735 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
736 || ! bfd_set_section_alignment (abfd
, newsect
,
737 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
740 flags
= SEC_NO_FLAGS
;
741 if (hdr
->sh_type
!= SHT_NOBITS
)
742 flags
|= SEC_HAS_CONTENTS
;
743 if (hdr
->sh_type
== SHT_GROUP
)
744 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
745 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
748 if (hdr
->sh_type
!= SHT_NOBITS
)
751 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
752 flags
|= SEC_READONLY
;
753 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
755 else if ((flags
& SEC_LOAD
) != 0)
757 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
760 newsect
->entsize
= hdr
->sh_entsize
;
761 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
762 flags
|= SEC_STRINGS
;
764 if (hdr
->sh_flags
& SHF_GROUP
)
765 if (!setup_group (abfd
, hdr
, newsect
))
767 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
768 flags
|= SEC_THREAD_LOCAL
;
770 /* The debugging sections appear to be recognized only by name, not
773 static const char *debug_sec_names
[] =
782 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
783 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
787 flags
|= SEC_DEBUGGING
;
790 /* As a GNU extension, if the name begins with .gnu.linkonce, we
791 only link a single copy of the section. This is used to support
792 g++. g++ will emit each template expansion in its own section.
793 The symbols will be defined as weak, so that multiple definitions
794 are permitted. The GNU linker extension is to actually discard
795 all but one of the sections. */
796 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
797 && elf_next_in_group (newsect
) == NULL
)
798 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
800 bed
= get_elf_backend_data (abfd
);
801 if (bed
->elf_backend_section_flags
)
802 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
805 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
808 if ((flags
& SEC_ALLOC
) != 0)
810 Elf_Internal_Phdr
*phdr
;
813 /* Look through the phdrs to see if we need to adjust the lma.
814 If all the p_paddr fields are zero, we ignore them, since
815 some ELF linkers produce such output. */
816 phdr
= elf_tdata (abfd
)->phdr
;
817 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
819 if (phdr
->p_paddr
!= 0)
822 if (i
< elf_elfheader (abfd
)->e_phnum
)
824 phdr
= elf_tdata (abfd
)->phdr
;
825 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
827 /* This section is part of this segment if its file
828 offset plus size lies within the segment's memory
829 span and, if the section is loaded, the extent of the
830 loaded data lies within the extent of the segment.
832 Note - we used to check the p_paddr field as well, and
833 refuse to set the LMA if it was 0. This is wrong
834 though, as a perfectly valid initialised segment can
835 have a p_paddr of zero. Some architectures, eg ARM,
836 place special significance on the address 0 and
837 executables need to be able to have a segment which
838 covers this address. */
839 if (phdr
->p_type
== PT_LOAD
840 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
841 && (hdr
->sh_offset
+ hdr
->sh_size
842 <= phdr
->p_offset
+ phdr
->p_memsz
)
843 && ((flags
& SEC_LOAD
) == 0
844 || (hdr
->sh_offset
+ hdr
->sh_size
845 <= phdr
->p_offset
+ phdr
->p_filesz
)))
847 if ((flags
& SEC_LOAD
) == 0)
848 newsect
->lma
= (phdr
->p_paddr
849 + hdr
->sh_addr
- phdr
->p_vaddr
);
851 /* We used to use the same adjustment for SEC_LOAD
852 sections, but that doesn't work if the segment
853 is packed with code from multiple VMAs.
854 Instead we calculate the section LMA based on
855 the segment LMA. It is assumed that the
856 segment will contain sections with contiguous
857 LMAs, even if the VMAs are not. */
858 newsect
->lma
= (phdr
->p_paddr
859 + hdr
->sh_offset
- phdr
->p_offset
);
861 /* With contiguous segments, we can't tell from file
862 offsets whether a section with zero size should
863 be placed at the end of one segment or the
864 beginning of the next. Decide based on vaddr. */
865 if (hdr
->sh_addr
>= phdr
->p_vaddr
866 && (hdr
->sh_addr
+ hdr
->sh_size
867 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
874 hdr
->bfd_section
= newsect
;
875 elf_section_data (newsect
)->this_hdr
= *hdr
;
885 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
888 Helper functions for GDB to locate the string tables.
889 Since BFD hides string tables from callers, GDB needs to use an
890 internal hook to find them. Sun's .stabstr, in particular,
891 isn't even pointed to by the .stab section, so ordinary
892 mechanisms wouldn't work to find it, even if we had some.
895 struct elf_internal_shdr
*
896 bfd_elf_find_section (abfd
, name
)
900 Elf_Internal_Shdr
**i_shdrp
;
905 i_shdrp
= elf_elfsections (abfd
);
908 shstrtab
= bfd_elf_get_str_section (abfd
,
909 elf_elfheader (abfd
)->e_shstrndx
);
910 if (shstrtab
!= NULL
)
912 max
= elf_numsections (abfd
);
913 for (i
= 1; i
< max
; i
++)
914 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
921 const char *const bfd_elf_section_type_names
[] = {
922 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
923 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
924 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
927 /* ELF relocs are against symbols. If we are producing relocateable
928 output, and the reloc is against an external symbol, and nothing
929 has given us any additional addend, the resulting reloc will also
930 be against the same symbol. In such a case, we don't want to
931 change anything about the way the reloc is handled, since it will
932 all be done at final link time. Rather than put special case code
933 into bfd_perform_relocation, all the reloc types use this howto
934 function. It just short circuits the reloc if producing
935 relocateable output against an external symbol. */
937 bfd_reloc_status_type
938 bfd_elf_generic_reloc (abfd
,
945 bfd
*abfd ATTRIBUTE_UNUSED
;
946 arelent
*reloc_entry
;
948 PTR data ATTRIBUTE_UNUSED
;
949 asection
*input_section
;
951 char **error_message ATTRIBUTE_UNUSED
;
953 if (output_bfd
!= (bfd
*) NULL
954 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
955 && (! reloc_entry
->howto
->partial_inplace
956 || reloc_entry
->addend
== 0))
958 reloc_entry
->address
+= input_section
->output_offset
;
962 return bfd_reloc_continue
;
965 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
968 merge_sections_remove_hook (abfd
, sec
)
969 bfd
*abfd ATTRIBUTE_UNUSED
;
972 BFD_ASSERT (sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
973 sec
->sec_info_type
= ELF_INFO_TYPE_NONE
;
976 /* Finish SHF_MERGE section merging. */
979 _bfd_elf_merge_sections (abfd
, info
)
981 struct bfd_link_info
*info
;
983 if (!is_elf_hash_table (info
))
985 if (elf_hash_table (info
)->merge_info
)
986 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
,
987 merge_sections_remove_hook
);
992 _bfd_elf_link_just_syms (sec
, info
)
994 struct bfd_link_info
*info
;
996 sec
->output_section
= bfd_abs_section_ptr
;
997 sec
->output_offset
= sec
->vma
;
998 if (!is_elf_hash_table (info
))
1001 sec
->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
1004 /* Copy the program header and other data from one object module to
1008 _bfd_elf_copy_private_bfd_data (ibfd
, obfd
)
1012 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1013 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1016 BFD_ASSERT (!elf_flags_init (obfd
)
1017 || (elf_elfheader (obfd
)->e_flags
1018 == elf_elfheader (ibfd
)->e_flags
));
1020 elf_gp (obfd
) = elf_gp (ibfd
);
1021 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1022 elf_flags_init (obfd
) = TRUE
;
1026 /* Print out the program headers. */
1029 _bfd_elf_print_private_bfd_data (abfd
, farg
)
1033 FILE *f
= (FILE *) farg
;
1034 Elf_Internal_Phdr
*p
;
1036 bfd_byte
*dynbuf
= NULL
;
1038 p
= elf_tdata (abfd
)->phdr
;
1043 fprintf (f
, _("\nProgram Header:\n"));
1044 c
= elf_elfheader (abfd
)->e_phnum
;
1045 for (i
= 0; i
< c
; i
++, p
++)
1052 case PT_NULL
: pt
= "NULL"; break;
1053 case PT_LOAD
: pt
= "LOAD"; break;
1054 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1055 case PT_INTERP
: pt
= "INTERP"; break;
1056 case PT_NOTE
: pt
= "NOTE"; break;
1057 case PT_SHLIB
: pt
= "SHLIB"; break;
1058 case PT_PHDR
: pt
= "PHDR"; break;
1059 case PT_TLS
: pt
= "TLS"; break;
1060 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1061 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
1063 fprintf (f
, "%8s off 0x", pt
);
1064 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1065 fprintf (f
, " vaddr 0x");
1066 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1067 fprintf (f
, " paddr 0x");
1068 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1069 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1070 fprintf (f
, " filesz 0x");
1071 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1072 fprintf (f
, " memsz 0x");
1073 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1074 fprintf (f
, " flags %c%c%c",
1075 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1076 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1077 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1078 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1079 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1084 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1088 unsigned long shlink
;
1089 bfd_byte
*extdyn
, *extdynend
;
1091 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1093 fprintf (f
, _("\nDynamic Section:\n"));
1095 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1098 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1102 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1105 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1107 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1108 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1111 extdynend
= extdyn
+ s
->_raw_size
;
1112 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1114 Elf_Internal_Dyn dyn
;
1117 bfd_boolean stringp
;
1119 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1121 if (dyn
.d_tag
== DT_NULL
)
1128 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1132 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1133 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1134 case DT_PLTGOT
: name
= "PLTGOT"; break;
1135 case DT_HASH
: name
= "HASH"; break;
1136 case DT_STRTAB
: name
= "STRTAB"; break;
1137 case DT_SYMTAB
: name
= "SYMTAB"; break;
1138 case DT_RELA
: name
= "RELA"; break;
1139 case DT_RELASZ
: name
= "RELASZ"; break;
1140 case DT_RELAENT
: name
= "RELAENT"; break;
1141 case DT_STRSZ
: name
= "STRSZ"; break;
1142 case DT_SYMENT
: name
= "SYMENT"; break;
1143 case DT_INIT
: name
= "INIT"; break;
1144 case DT_FINI
: name
= "FINI"; break;
1145 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1146 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1147 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1148 case DT_REL
: name
= "REL"; break;
1149 case DT_RELSZ
: name
= "RELSZ"; break;
1150 case DT_RELENT
: name
= "RELENT"; break;
1151 case DT_PLTREL
: name
= "PLTREL"; break;
1152 case DT_DEBUG
: name
= "DEBUG"; break;
1153 case DT_TEXTREL
: name
= "TEXTREL"; break;
1154 case DT_JMPREL
: name
= "JMPREL"; break;
1155 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1156 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1157 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1158 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1159 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1160 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1161 case DT_FLAGS
: name
= "FLAGS"; break;
1162 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1163 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1164 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1165 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1166 case DT_MOVEENT
: name
= "MOVEENT"; break;
1167 case DT_MOVESZ
: name
= "MOVESZ"; break;
1168 case DT_FEATURE
: name
= "FEATURE"; break;
1169 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1170 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1171 case DT_SYMINENT
: name
= "SYMINENT"; break;
1172 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1173 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1174 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1175 case DT_PLTPAD
: name
= "PLTPAD"; break;
1176 case DT_MOVETAB
: name
= "MOVETAB"; break;
1177 case DT_SYMINFO
: name
= "SYMINFO"; break;
1178 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1179 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1180 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1181 case DT_VERSYM
: name
= "VERSYM"; break;
1182 case DT_VERDEF
: name
= "VERDEF"; break;
1183 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1184 case DT_VERNEED
: name
= "VERNEED"; break;
1185 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1186 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1187 case DT_USED
: name
= "USED"; break;
1188 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1191 fprintf (f
, " %-11s ", name
);
1193 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1197 unsigned int tagv
= dyn
.d_un
.d_val
;
1199 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1202 fprintf (f
, "%s", string
);
1211 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1212 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1214 if (! _bfd_elf_slurp_version_tables (abfd
))
1218 if (elf_dynverdef (abfd
) != 0)
1220 Elf_Internal_Verdef
*t
;
1222 fprintf (f
, _("\nVersion definitions:\n"));
1223 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1225 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1226 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1227 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1229 Elf_Internal_Verdaux
*a
;
1232 for (a
= t
->vd_auxptr
->vda_nextptr
;
1235 fprintf (f
, "%s ", a
->vda_nodename
);
1241 if (elf_dynverref (abfd
) != 0)
1243 Elf_Internal_Verneed
*t
;
1245 fprintf (f
, _("\nVersion References:\n"));
1246 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1248 Elf_Internal_Vernaux
*a
;
1250 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1251 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1252 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1253 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1265 /* Display ELF-specific fields of a symbol. */
1268 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
1272 bfd_print_symbol_type how
;
1274 FILE *file
= (FILE *) filep
;
1277 case bfd_print_symbol_name
:
1278 fprintf (file
, "%s", symbol
->name
);
1280 case bfd_print_symbol_more
:
1281 fprintf (file
, "elf ");
1282 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1283 fprintf (file
, " %lx", (long) symbol
->flags
);
1285 case bfd_print_symbol_all
:
1287 const char *section_name
;
1288 const char *name
= NULL
;
1289 struct elf_backend_data
*bed
;
1290 unsigned char st_other
;
1293 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1295 bed
= get_elf_backend_data (abfd
);
1296 if (bed
->elf_backend_print_symbol_all
)
1297 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1301 name
= symbol
->name
;
1302 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
1305 fprintf (file
, " %s\t", section_name
);
1306 /* Print the "other" value for a symbol. For common symbols,
1307 we've already printed the size; now print the alignment.
1308 For other symbols, we have no specified alignment, and
1309 we've printed the address; now print the size. */
1310 if (bfd_is_com_section (symbol
->section
))
1311 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1313 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1314 bfd_fprintf_vma (abfd
, file
, val
);
1316 /* If we have version information, print it. */
1317 if (elf_tdata (abfd
)->dynversym_section
!= 0
1318 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1319 || elf_tdata (abfd
)->dynverref_section
!= 0))
1321 unsigned int vernum
;
1322 const char *version_string
;
1324 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1327 version_string
= "";
1328 else if (vernum
== 1)
1329 version_string
= "Base";
1330 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1332 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1335 Elf_Internal_Verneed
*t
;
1337 version_string
= "";
1338 for (t
= elf_tdata (abfd
)->verref
;
1342 Elf_Internal_Vernaux
*a
;
1344 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1346 if (a
->vna_other
== vernum
)
1348 version_string
= a
->vna_nodename
;
1355 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1356 fprintf (file
, " %-11s", version_string
);
1361 fprintf (file
, " (%s)", version_string
);
1362 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1367 /* If the st_other field is not zero, print it. */
1368 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1373 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1374 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1375 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1377 /* Some other non-defined flags are also present, so print
1379 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1382 fprintf (file
, " %s", name
);
1388 /* Create an entry in an ELF linker hash table. */
1390 struct bfd_hash_entry
*
1391 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
1392 struct bfd_hash_entry
*entry
;
1393 struct bfd_hash_table
*table
;
1396 /* Allocate the structure if it has not already been allocated by a
1400 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1405 /* Call the allocation method of the superclass. */
1406 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1409 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1410 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1412 /* Set local fields. */
1415 ret
->dynstr_index
= 0;
1416 ret
->elf_hash_value
= 0;
1417 ret
->weakdef
= NULL
;
1418 ret
->linker_section_pointer
= NULL
;
1419 ret
->verinfo
.verdef
= NULL
;
1420 ret
->vtable_entries_size
= 0;
1421 ret
->vtable_entries_used
= NULL
;
1422 ret
->vtable_parent
= NULL
;
1423 ret
->got
.refcount
= htab
->init_refcount
;
1424 ret
->plt
.refcount
= htab
->init_refcount
;
1426 ret
->type
= STT_NOTYPE
;
1428 /* Assume that we have been called by a non-ELF symbol reader.
1429 This flag is then reset by the code which reads an ELF input
1430 file. This ensures that a symbol created by a non-ELF symbol
1431 reader will have the flag set correctly. */
1432 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1438 /* Copy data from an indirect symbol to its direct symbol, hiding the
1439 old indirect symbol. Also used for copying flags to a weakdef. */
1442 _bfd_elf_link_hash_copy_indirect (bed
, dir
, ind
)
1443 struct elf_backend_data
*bed
;
1444 struct elf_link_hash_entry
*dir
, *ind
;
1447 bfd_signed_vma lowest_valid
= bed
->can_refcount
;
1449 /* Copy down any references that we may have already seen to the
1450 symbol which just became indirect. */
1452 dir
->elf_link_hash_flags
|=
1453 (ind
->elf_link_hash_flags
1454 & (ELF_LINK_HASH_REF_DYNAMIC
1455 | ELF_LINK_HASH_REF_REGULAR
1456 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1457 | ELF_LINK_NON_GOT_REF
));
1459 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1462 /* Copy over the global and procedure linkage table refcount entries.
1463 These may have been already set up by a check_relocs routine. */
1464 tmp
= dir
->got
.refcount
;
1465 if (tmp
< lowest_valid
)
1467 dir
->got
.refcount
= ind
->got
.refcount
;
1468 ind
->got
.refcount
= tmp
;
1471 BFD_ASSERT (ind
->got
.refcount
< lowest_valid
);
1473 tmp
= dir
->plt
.refcount
;
1474 if (tmp
< lowest_valid
)
1476 dir
->plt
.refcount
= ind
->plt
.refcount
;
1477 ind
->plt
.refcount
= tmp
;
1480 BFD_ASSERT (ind
->plt
.refcount
< lowest_valid
);
1482 if (dir
->dynindx
== -1)
1484 dir
->dynindx
= ind
->dynindx
;
1485 dir
->dynstr_index
= ind
->dynstr_index
;
1487 ind
->dynstr_index
= 0;
1490 BFD_ASSERT (ind
->dynindx
== -1);
1494 _bfd_elf_link_hash_hide_symbol (info
, h
, force_local
)
1495 struct bfd_link_info
*info
;
1496 struct elf_link_hash_entry
*h
;
1497 bfd_boolean force_local
;
1499 h
->plt
.offset
= (bfd_vma
) -1;
1500 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1503 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1504 if (h
->dynindx
!= -1)
1507 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
1513 /* Initialize an ELF linker hash table. */
1516 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1517 struct elf_link_hash_table
*table
;
1519 struct bfd_hash_entry
*(*newfunc
)
1520 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*,
1525 table
->dynamic_sections_created
= FALSE
;
1526 table
->dynobj
= NULL
;
1527 /* Make sure can_refcount is extended to the width and signedness of
1528 init_refcount before we subtract one from it. */
1529 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
1530 --table
->init_refcount
;
1531 /* The first dynamic symbol is a dummy. */
1532 table
->dynsymcount
= 1;
1533 table
->dynstr
= NULL
;
1534 table
->bucketcount
= 0;
1535 table
->needed
= NULL
;
1537 table
->stab_info
= NULL
;
1538 table
->merge_info
= NULL
;
1539 memset (&table
->eh_info
, 0, sizeof (table
->eh_info
));
1540 table
->dynlocal
= NULL
;
1541 table
->runpath
= NULL
;
1542 table
->tls_segment
= NULL
;
1543 table
->loaded
= NULL
;
1545 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
1546 table
->root
.type
= bfd_link_elf_hash_table
;
1551 /* Create an ELF linker hash table. */
1553 struct bfd_link_hash_table
*
1554 _bfd_elf_link_hash_table_create (abfd
)
1557 struct elf_link_hash_table
*ret
;
1558 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1560 ret
= (struct elf_link_hash_table
*) bfd_malloc (amt
);
1561 if (ret
== (struct elf_link_hash_table
*) NULL
)
1564 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1573 /* This is a hook for the ELF emulation code in the generic linker to
1574 tell the backend linker what file name to use for the DT_NEEDED
1575 entry for a dynamic object. The generic linker passes name as an
1576 empty string to indicate that no DT_NEEDED entry should be made. */
1579 bfd_elf_set_dt_needed_name (abfd
, name
)
1583 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1584 && bfd_get_format (abfd
) == bfd_object
)
1585 elf_dt_name (abfd
) = name
;
1589 bfd_elf_set_dt_needed_soname (abfd
, name
)
1593 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1594 && bfd_get_format (abfd
) == bfd_object
)
1595 elf_dt_soname (abfd
) = name
;
1598 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1599 the linker ELF emulation code. */
1601 struct bfd_link_needed_list
*
1602 bfd_elf_get_needed_list (abfd
, info
)
1603 bfd
*abfd ATTRIBUTE_UNUSED
;
1604 struct bfd_link_info
*info
;
1606 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1608 return elf_hash_table (info
)->needed
;
1611 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1612 hook for the linker ELF emulation code. */
1614 struct bfd_link_needed_list
*
1615 bfd_elf_get_runpath_list (abfd
, info
)
1616 bfd
*abfd ATTRIBUTE_UNUSED
;
1617 struct bfd_link_info
*info
;
1619 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1621 return elf_hash_table (info
)->runpath
;
1624 /* Get the name actually used for a dynamic object for a link. This
1625 is the SONAME entry if there is one. Otherwise, it is the string
1626 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1629 bfd_elf_get_dt_soname (abfd
)
1632 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1633 && bfd_get_format (abfd
) == bfd_object
)
1634 return elf_dt_name (abfd
);
1638 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1639 the ELF linker emulation code. */
1642 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1644 struct bfd_link_needed_list
**pneeded
;
1647 bfd_byte
*dynbuf
= NULL
;
1649 unsigned long shlink
;
1650 bfd_byte
*extdyn
, *extdynend
;
1652 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1656 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1657 || bfd_get_format (abfd
) != bfd_object
)
1660 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1661 if (s
== NULL
|| s
->_raw_size
== 0)
1664 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1668 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1672 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1676 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1678 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1679 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1682 extdynend
= extdyn
+ s
->_raw_size
;
1683 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1685 Elf_Internal_Dyn dyn
;
1687 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1689 if (dyn
.d_tag
== DT_NULL
)
1692 if (dyn
.d_tag
== DT_NEEDED
)
1695 struct bfd_link_needed_list
*l
;
1696 unsigned int tagv
= dyn
.d_un
.d_val
;
1699 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1704 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1725 /* Allocate an ELF string table--force the first byte to be zero. */
1727 struct bfd_strtab_hash
*
1728 _bfd_elf_stringtab_init ()
1730 struct bfd_strtab_hash
*ret
;
1732 ret
= _bfd_stringtab_init ();
1737 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1738 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1739 if (loc
== (bfd_size_type
) -1)
1741 _bfd_stringtab_free (ret
);
1748 /* ELF .o/exec file reading */
1750 /* Create a new bfd section from an ELF section header. */
1753 bfd_section_from_shdr (abfd
, shindex
)
1755 unsigned int shindex
;
1757 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1758 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1759 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1762 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1764 switch (hdr
->sh_type
)
1767 /* Inactive section. Throw it away. */
1770 case SHT_PROGBITS
: /* Normal section with contents. */
1771 case SHT_NOBITS
: /* .bss section. */
1772 case SHT_HASH
: /* .hash section. */
1773 case SHT_NOTE
: /* .note section. */
1774 case SHT_INIT_ARRAY
: /* .init_array section. */
1775 case SHT_FINI_ARRAY
: /* .fini_array section. */
1776 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1777 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1779 case SHT_DYNAMIC
: /* Dynamic linking information. */
1780 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1782 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1784 Elf_Internal_Shdr
*dynsymhdr
;
1786 /* The shared libraries distributed with hpux11 have a bogus
1787 sh_link field for the ".dynamic" section. Find the
1788 string table for the ".dynsym" section instead. */
1789 if (elf_dynsymtab (abfd
) != 0)
1791 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1792 hdr
->sh_link
= dynsymhdr
->sh_link
;
1796 unsigned int i
, num_sec
;
1798 num_sec
= elf_numsections (abfd
);
1799 for (i
= 1; i
< num_sec
; i
++)
1801 dynsymhdr
= elf_elfsections (abfd
)[i
];
1802 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1804 hdr
->sh_link
= dynsymhdr
->sh_link
;
1812 case SHT_SYMTAB
: /* A symbol table */
1813 if (elf_onesymtab (abfd
) == shindex
)
1816 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1817 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1818 elf_onesymtab (abfd
) = shindex
;
1819 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1820 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1821 abfd
->flags
|= HAS_SYMS
;
1823 /* Sometimes a shared object will map in the symbol table. If
1824 SHF_ALLOC is set, and this is a shared object, then we also
1825 treat this section as a BFD section. We can not base the
1826 decision purely on SHF_ALLOC, because that flag is sometimes
1827 set in a relocateable object file, which would confuse the
1829 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1830 && (abfd
->flags
& DYNAMIC
) != 0
1831 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1836 case SHT_DYNSYM
: /* A dynamic symbol table */
1837 if (elf_dynsymtab (abfd
) == shindex
)
1840 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1841 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1842 elf_dynsymtab (abfd
) = shindex
;
1843 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1844 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1845 abfd
->flags
|= HAS_SYMS
;
1847 /* Besides being a symbol table, we also treat this as a regular
1848 section, so that objcopy can handle it. */
1849 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1851 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1852 if (elf_symtab_shndx (abfd
) == shindex
)
1855 /* Get the associated symbol table. */
1856 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1857 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1860 elf_symtab_shndx (abfd
) = shindex
;
1861 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1862 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1865 case SHT_STRTAB
: /* A string table */
1866 if (hdr
->bfd_section
!= NULL
)
1868 if (ehdr
->e_shstrndx
== shindex
)
1870 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1871 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1875 unsigned int i
, num_sec
;
1877 num_sec
= elf_numsections (abfd
);
1878 for (i
= 1; i
< num_sec
; i
++)
1880 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1881 if (hdr2
->sh_link
== shindex
)
1883 if (! bfd_section_from_shdr (abfd
, i
))
1885 if (elf_onesymtab (abfd
) == i
)
1887 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1888 elf_elfsections (abfd
)[shindex
] =
1889 &elf_tdata (abfd
)->strtab_hdr
;
1892 if (elf_dynsymtab (abfd
) == i
)
1894 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1895 elf_elfsections (abfd
)[shindex
] = hdr
=
1896 &elf_tdata (abfd
)->dynstrtab_hdr
;
1897 /* We also treat this as a regular section, so
1898 that objcopy can handle it. */
1901 #if 0 /* Not handling other string tables specially right now. */
1902 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1903 /* We have a strtab for some random other section. */
1904 newsect
= (asection
*) hdr2
->bfd_section
;
1907 hdr
->bfd_section
= newsect
;
1908 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1910 elf_elfsections (abfd
)[shindex
] = hdr2
;
1916 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1920 /* *These* do a lot of work -- but build no sections! */
1922 asection
*target_sect
;
1923 Elf_Internal_Shdr
*hdr2
;
1924 unsigned int num_sec
= elf_numsections (abfd
);
1926 /* Check for a bogus link to avoid crashing. */
1927 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1928 || hdr
->sh_link
>= num_sec
)
1930 ((*_bfd_error_handler
)
1931 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1932 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1933 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1936 /* For some incomprehensible reason Oracle distributes
1937 libraries for Solaris in which some of the objects have
1938 bogus sh_link fields. It would be nice if we could just
1939 reject them, but, unfortunately, some people need to use
1940 them. We scan through the section headers; if we find only
1941 one suitable symbol table, we clobber the sh_link to point
1942 to it. I hope this doesn't break anything. */
1943 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1944 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1950 for (scan
= 1; scan
< num_sec
; scan
++)
1952 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1953 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1964 hdr
->sh_link
= found
;
1967 /* Get the symbol table. */
1968 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1969 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1972 /* If this reloc section does not use the main symbol table we
1973 don't treat it as a reloc section. BFD can't adequately
1974 represent such a section, so at least for now, we don't
1975 try. We just present it as a normal section. We also
1976 can't use it as a reloc section if it points to the null
1978 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1979 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1981 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1983 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1984 if (target_sect
== NULL
)
1987 if ((target_sect
->flags
& SEC_RELOC
) == 0
1988 || target_sect
->reloc_count
== 0)
1989 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1993 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1994 amt
= sizeof (*hdr2
);
1995 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1996 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1999 elf_elfsections (abfd
)[shindex
] = hdr2
;
2000 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
2001 target_sect
->flags
|= SEC_RELOC
;
2002 target_sect
->relocation
= NULL
;
2003 target_sect
->rel_filepos
= hdr
->sh_offset
;
2004 /* In the section to which the relocations apply, mark whether
2005 its relocations are of the REL or RELA variety. */
2006 if (hdr
->sh_size
!= 0)
2007 target_sect
->use_rela_p
= hdr
->sh_type
== SHT_RELA
;
2008 abfd
->flags
|= HAS_RELOC
;
2013 case SHT_GNU_verdef
:
2014 elf_dynverdef (abfd
) = shindex
;
2015 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2016 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
2019 case SHT_GNU_versym
:
2020 elf_dynversym (abfd
) = shindex
;
2021 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2022 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
2025 case SHT_GNU_verneed
:
2026 elf_dynverref (abfd
) = shindex
;
2027 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2028 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
2035 /* We need a BFD section for objcopy and relocatable linking,
2036 and it's handy to have the signature available as the section
2038 name
= group_signature (abfd
, hdr
);
2041 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
2043 if (hdr
->contents
!= NULL
)
2045 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2046 unsigned int n_elt
= hdr
->sh_size
/ 4;
2049 if (idx
->flags
& GRP_COMDAT
)
2050 hdr
->bfd_section
->flags
2051 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2053 while (--n_elt
!= 0)
2054 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
2055 && elf_next_in_group (s
) != NULL
)
2057 elf_next_in_group (hdr
->bfd_section
) = s
;
2064 /* Check for any processor-specific section types. */
2066 if (bed
->elf_backend_section_from_shdr
)
2067 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
2075 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2076 Return SEC for sections that have no elf section, and NULL on error. */
2079 bfd_section_from_r_symndx (abfd
, cache
, sec
, r_symndx
)
2081 struct sym_sec_cache
*cache
;
2083 unsigned long r_symndx
;
2085 Elf_Internal_Shdr
*symtab_hdr
;
2086 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2087 Elf_External_Sym_Shndx eshndx
;
2088 Elf_Internal_Sym isym
;
2089 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2091 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
2092 return cache
->sec
[ent
];
2094 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2095 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2096 &isym
, esym
, &eshndx
) == NULL
)
2099 if (cache
->abfd
!= abfd
)
2101 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2104 cache
->indx
[ent
] = r_symndx
;
2105 cache
->sec
[ent
] = sec
;
2106 if (isym
.st_shndx
< SHN_LORESERVE
|| isym
.st_shndx
> SHN_HIRESERVE
)
2109 s
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
2111 cache
->sec
[ent
] = s
;
2113 return cache
->sec
[ent
];
2116 /* Given an ELF section number, retrieve the corresponding BFD
2120 bfd_section_from_elf_index (abfd
, index
)
2124 if (index
>= elf_numsections (abfd
))
2126 return elf_elfsections (abfd
)[index
]->bfd_section
;
2130 _bfd_elf_new_section_hook (abfd
, sec
)
2134 struct bfd_elf_section_data
*sdata
;
2136 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2139 bfd_size_type amt
= sizeof (*sdata
);
2140 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
2143 sec
->used_by_bfd
= (PTR
) sdata
;
2146 /* Indicate whether or not this section should use RELA relocations. */
2147 sec
->use_rela_p
= get_elf_backend_data (abfd
)->default_use_rela_p
;
2152 /* Create a new bfd section from an ELF program header.
2154 Since program segments have no names, we generate a synthetic name
2155 of the form segment<NUM>, where NUM is generally the index in the
2156 program header table. For segments that are split (see below) we
2157 generate the names segment<NUM>a and segment<NUM>b.
2159 Note that some program segments may have a file size that is different than
2160 (less than) the memory size. All this means is that at execution the
2161 system must allocate the amount of memory specified by the memory size,
2162 but only initialize it with the first "file size" bytes read from the
2163 file. This would occur for example, with program segments consisting
2164 of combined data+bss.
2166 To handle the above situation, this routine generates TWO bfd sections
2167 for the single program segment. The first has the length specified by
2168 the file size of the segment, and the second has the length specified
2169 by the difference between the two sizes. In effect, the segment is split
2170 into it's initialized and uninitialized parts.
2175 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
2177 Elf_Internal_Phdr
*hdr
;
2179 const char *typename
;
2187 split
= ((hdr
->p_memsz
> 0)
2188 && (hdr
->p_filesz
> 0)
2189 && (hdr
->p_memsz
> hdr
->p_filesz
));
2190 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2191 len
= strlen (namebuf
) + 1;
2192 name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
2195 memcpy (name
, namebuf
, len
);
2196 newsect
= bfd_make_section (abfd
, name
);
2197 if (newsect
== NULL
)
2199 newsect
->vma
= hdr
->p_vaddr
;
2200 newsect
->lma
= hdr
->p_paddr
;
2201 newsect
->_raw_size
= hdr
->p_filesz
;
2202 newsect
->filepos
= hdr
->p_offset
;
2203 newsect
->flags
|= SEC_HAS_CONTENTS
;
2204 if (hdr
->p_type
== PT_LOAD
)
2206 newsect
->flags
|= SEC_ALLOC
;
2207 newsect
->flags
|= SEC_LOAD
;
2208 if (hdr
->p_flags
& PF_X
)
2210 /* FIXME: all we known is that it has execute PERMISSION,
2212 newsect
->flags
|= SEC_CODE
;
2215 if (!(hdr
->p_flags
& PF_W
))
2217 newsect
->flags
|= SEC_READONLY
;
2222 sprintf (namebuf
, "%s%db", typename
, index
);
2223 len
= strlen (namebuf
) + 1;
2224 name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
2227 memcpy (name
, namebuf
, len
);
2228 newsect
= bfd_make_section (abfd
, name
);
2229 if (newsect
== NULL
)
2231 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2232 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2233 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
2234 if (hdr
->p_type
== PT_LOAD
)
2236 newsect
->flags
|= SEC_ALLOC
;
2237 if (hdr
->p_flags
& PF_X
)
2238 newsect
->flags
|= SEC_CODE
;
2240 if (!(hdr
->p_flags
& PF_W
))
2241 newsect
->flags
|= SEC_READONLY
;
2248 bfd_section_from_phdr (abfd
, hdr
, index
)
2250 Elf_Internal_Phdr
*hdr
;
2253 struct elf_backend_data
*bed
;
2255 switch (hdr
->p_type
)
2258 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2261 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2264 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2267 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2270 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2272 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
2277 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2280 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2283 /* Check for any processor-specific program segment types.
2284 If no handler for them, default to making "segment" sections. */
2285 bed
= get_elf_backend_data (abfd
);
2286 if (bed
->elf_backend_section_from_phdr
)
2287 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2289 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2293 /* Initialize REL_HDR, the section-header for new section, containing
2294 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2295 relocations; otherwise, we use REL relocations. */
2298 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
2300 Elf_Internal_Shdr
*rel_hdr
;
2302 bfd_boolean use_rela_p
;
2305 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2306 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2308 name
= bfd_alloc (abfd
, amt
);
2311 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2313 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2315 if (rel_hdr
->sh_name
== (unsigned int) -1)
2317 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2318 rel_hdr
->sh_entsize
= (use_rela_p
2319 ? bed
->s
->sizeof_rela
2320 : bed
->s
->sizeof_rel
);
2321 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
2322 rel_hdr
->sh_flags
= 0;
2323 rel_hdr
->sh_addr
= 0;
2324 rel_hdr
->sh_size
= 0;
2325 rel_hdr
->sh_offset
= 0;
2330 /* Set up an ELF internal section header for a section. */
2333 elf_fake_sections (abfd
, asect
, failedptrarg
)
2338 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2339 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2340 Elf_Internal_Shdr
*this_hdr
;
2344 /* We already failed; just get out of the bfd_map_over_sections
2349 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2351 this_hdr
->sh_name
= (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2352 asect
->name
, FALSE
);
2353 if (this_hdr
->sh_name
== (unsigned long) -1)
2359 this_hdr
->sh_flags
= 0;
2361 if ((asect
->flags
& SEC_ALLOC
) != 0
2362 || asect
->user_set_vma
)
2363 this_hdr
->sh_addr
= asect
->vma
;
2365 this_hdr
->sh_addr
= 0;
2367 this_hdr
->sh_offset
= 0;
2368 this_hdr
->sh_size
= asect
->_raw_size
;
2369 this_hdr
->sh_link
= 0;
2370 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2371 /* The sh_entsize and sh_info fields may have been set already by
2372 copy_private_section_data. */
2374 this_hdr
->bfd_section
= asect
;
2375 this_hdr
->contents
= NULL
;
2377 /* FIXME: This should not be based on section names. */
2378 if (strcmp (asect
->name
, ".dynstr") == 0)
2379 this_hdr
->sh_type
= SHT_STRTAB
;
2380 else if (strcmp (asect
->name
, ".hash") == 0)
2382 this_hdr
->sh_type
= SHT_HASH
;
2383 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2385 else if (strcmp (asect
->name
, ".dynsym") == 0)
2387 this_hdr
->sh_type
= SHT_DYNSYM
;
2388 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2390 else if (strcmp (asect
->name
, ".dynamic") == 0)
2392 this_hdr
->sh_type
= SHT_DYNAMIC
;
2393 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2395 else if (strncmp (asect
->name
, ".rela", 5) == 0
2396 && get_elf_backend_data (abfd
)->may_use_rela_p
)
2398 this_hdr
->sh_type
= SHT_RELA
;
2399 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2401 else if (strncmp (asect
->name
, ".rel", 4) == 0
2402 && get_elf_backend_data (abfd
)->may_use_rel_p
)
2404 this_hdr
->sh_type
= SHT_REL
;
2405 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2407 else if (strcmp (asect
->name
, ".init_array") == 0)
2408 this_hdr
->sh_type
= SHT_INIT_ARRAY
;
2409 else if (strcmp (asect
->name
, ".fini_array") == 0)
2410 this_hdr
->sh_type
= SHT_FINI_ARRAY
;
2411 else if (strcmp (asect
->name
, ".preinit_array") == 0)
2412 this_hdr
->sh_type
= SHT_PREINIT_ARRAY
;
2413 else if (strncmp (asect
->name
, ".note", 5) == 0)
2414 this_hdr
->sh_type
= SHT_NOTE
;
2415 else if (strncmp (asect
->name
, ".stab", 5) == 0
2416 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
2417 this_hdr
->sh_type
= SHT_STRTAB
;
2418 else if (strcmp (asect
->name
, ".gnu.version") == 0)
2420 this_hdr
->sh_type
= SHT_GNU_versym
;
2421 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2423 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
2425 this_hdr
->sh_type
= SHT_GNU_verdef
;
2426 this_hdr
->sh_entsize
= 0;
2427 /* objcopy or strip will copy over sh_info, but may not set
2428 cverdefs. The linker will set cverdefs, but sh_info will be
2430 if (this_hdr
->sh_info
== 0)
2431 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2433 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2434 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2436 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
2438 this_hdr
->sh_type
= SHT_GNU_verneed
;
2439 this_hdr
->sh_entsize
= 0;
2440 /* objcopy or strip will copy over sh_info, but may not set
2441 cverrefs. The linker will set cverrefs, but sh_info will be
2443 if (this_hdr
->sh_info
== 0)
2444 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2446 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2447 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2449 else if ((asect
->flags
& SEC_GROUP
) != 0)
2451 this_hdr
->sh_type
= SHT_GROUP
;
2452 this_hdr
->sh_entsize
= 4;
2454 else if ((asect
->flags
& SEC_ALLOC
) != 0
2455 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2456 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2457 this_hdr
->sh_type
= SHT_NOBITS
;
2459 this_hdr
->sh_type
= SHT_PROGBITS
;
2461 if ((asect
->flags
& SEC_ALLOC
) != 0)
2462 this_hdr
->sh_flags
|= SHF_ALLOC
;
2463 if ((asect
->flags
& SEC_READONLY
) == 0)
2464 this_hdr
->sh_flags
|= SHF_WRITE
;
2465 if ((asect
->flags
& SEC_CODE
) != 0)
2466 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2467 if ((asect
->flags
& SEC_MERGE
) != 0)
2469 this_hdr
->sh_flags
|= SHF_MERGE
;
2470 this_hdr
->sh_entsize
= asect
->entsize
;
2471 if ((asect
->flags
& SEC_STRINGS
) != 0)
2472 this_hdr
->sh_flags
|= SHF_STRINGS
;
2474 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2475 this_hdr
->sh_flags
|= SHF_GROUP
;
2476 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2478 this_hdr
->sh_flags
|= SHF_TLS
;
2479 if (asect
->_raw_size
== 0 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2481 struct bfd_link_order
*o
;
2483 this_hdr
->sh_size
= 0;
2484 for (o
= asect
->link_order_head
; o
!= NULL
; o
= o
->next
)
2485 if (this_hdr
->sh_size
< o
->offset
+ o
->size
)
2486 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2487 if (this_hdr
->sh_size
)
2488 this_hdr
->sh_type
= SHT_NOBITS
;
2492 /* Check for processor-specific section types. */
2493 if (bed
->elf_backend_fake_sections
2494 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2497 /* If the section has relocs, set up a section header for the
2498 SHT_REL[A] section. If two relocation sections are required for
2499 this section, it is up to the processor-specific back-end to
2500 create the other. */
2501 if ((asect
->flags
& SEC_RELOC
) != 0
2502 && !_bfd_elf_init_reloc_shdr (abfd
,
2503 &elf_section_data (asect
)->rel_hdr
,
2509 /* Fill in the contents of a SHT_GROUP section. */
2512 bfd_elf_set_group_contents (abfd
, sec
, failedptrarg
)
2517 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2518 unsigned long symindx
;
2519 asection
*elt
, *first
;
2521 struct bfd_link_order
*l
;
2524 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2529 if (elf_group_id (sec
) != NULL
)
2530 symindx
= elf_group_id (sec
)->udata
.i
;
2534 /* If called from the assembler, swap_out_syms will have set up
2535 elf_section_syms; If called for "ld -r", use target_index. */
2536 if (elf_section_syms (abfd
) != NULL
)
2537 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2539 symindx
= sec
->target_index
;
2541 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2543 /* The contents won't be allocated for "ld -r" or objcopy. */
2545 if (sec
->contents
== NULL
)
2548 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2550 /* Arrange for the section to be written out. */
2551 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2552 if (sec
->contents
== NULL
)
2559 loc
= sec
->contents
+ sec
->_raw_size
;
2561 /* Get the pointer to the first section in the group that gas
2562 squirreled away here. objcopy arranges for this to be set to the
2563 start of the input section group. */
2564 first
= elt
= elf_next_in_group (sec
);
2566 /* First element is a flag word. Rest of section is elf section
2567 indices for all the sections of the group. Write them backwards
2568 just to keep the group in the same order as given in .section
2569 directives, not that it matters. */
2578 s
= s
->output_section
;
2581 idx
= elf_section_data (s
)->this_idx
;
2582 H_PUT_32 (abfd
, idx
, loc
);
2583 elt
= elf_next_in_group (elt
);
2588 /* If this is a relocatable link, then the above did nothing because
2589 SEC is the output section. Look through the input sections
2591 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2592 if (l
->type
== bfd_indirect_link_order
2593 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2598 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2599 elt
= elf_next_in_group (elt
);
2600 /* During a relocatable link, the lists are circular. */
2602 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2604 /* With ld -r, merging SHT_GROUP sections results in wasted space
2605 due to allowing for the flag word on each input. We may well
2606 duplicate entries too. */
2607 while ((loc
-= 4) > sec
->contents
)
2608 H_PUT_32 (abfd
, 0, loc
);
2610 if (loc
!= sec
->contents
)
2613 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2616 /* Assign all ELF section numbers. The dummy first section is handled here
2617 too. The link/info pointers for the standard section types are filled
2618 in here too, while we're at it. */
2621 assign_section_numbers (abfd
)
2624 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2626 unsigned int section_number
, secn
;
2627 Elf_Internal_Shdr
**i_shdrp
;
2632 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2634 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2636 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2638 if (section_number
== SHN_LORESERVE
)
2639 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2640 d
->this_idx
= section_number
++;
2641 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2642 if ((sec
->flags
& SEC_RELOC
) == 0)
2646 if (section_number
== SHN_LORESERVE
)
2647 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2648 d
->rel_idx
= section_number
++;
2649 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2654 if (section_number
== SHN_LORESERVE
)
2655 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2656 d
->rel_idx2
= section_number
++;
2657 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2663 if (section_number
== SHN_LORESERVE
)
2664 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2665 t
->shstrtab_section
= section_number
++;
2666 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2667 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2669 if (bfd_get_symcount (abfd
) > 0)
2671 if (section_number
== SHN_LORESERVE
)
2672 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2673 t
->symtab_section
= section_number
++;
2674 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2675 if (section_number
> SHN_LORESERVE
- 2)
2677 if (section_number
== SHN_LORESERVE
)
2678 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2679 t
->symtab_shndx_section
= section_number
++;
2680 t
->symtab_shndx_hdr
.sh_name
2681 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2682 ".symtab_shndx", FALSE
);
2683 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2686 if (section_number
== SHN_LORESERVE
)
2687 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2688 t
->strtab_section
= section_number
++;
2689 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2692 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2693 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2695 elf_numsections (abfd
) = section_number
;
2696 elf_elfheader (abfd
)->e_shnum
= section_number
;
2697 if (section_number
> SHN_LORESERVE
)
2698 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2700 /* Set up the list of section header pointers, in agreement with the
2702 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2703 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
2704 if (i_shdrp
== NULL
)
2707 amt
= sizeof (Elf_Internal_Shdr
);
2708 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
, amt
);
2709 if (i_shdrp
[0] == NULL
)
2711 bfd_release (abfd
, i_shdrp
);
2715 elf_elfsections (abfd
) = i_shdrp
;
2717 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2718 if (bfd_get_symcount (abfd
) > 0)
2720 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2721 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2723 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2724 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2726 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2727 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2729 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2731 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2735 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2736 if (d
->rel_idx
!= 0)
2737 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2738 if (d
->rel_idx2
!= 0)
2739 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2741 /* Fill in the sh_link and sh_info fields while we're at it. */
2743 /* sh_link of a reloc section is the section index of the symbol
2744 table. sh_info is the section index of the section to which
2745 the relocation entries apply. */
2746 if (d
->rel_idx
!= 0)
2748 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2749 d
->rel_hdr
.sh_info
= d
->this_idx
;
2751 if (d
->rel_idx2
!= 0)
2753 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2754 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2757 switch (d
->this_hdr
.sh_type
)
2761 /* A reloc section which we are treating as a normal BFD
2762 section. sh_link is the section index of the symbol
2763 table. sh_info is the section index of the section to
2764 which the relocation entries apply. We assume that an
2765 allocated reloc section uses the dynamic symbol table.
2766 FIXME: How can we be sure? */
2767 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2769 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2771 /* We look up the section the relocs apply to by name. */
2773 if (d
->this_hdr
.sh_type
== SHT_REL
)
2777 s
= bfd_get_section_by_name (abfd
, name
);
2779 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2783 /* We assume that a section named .stab*str is a stabs
2784 string section. We look for a section with the same name
2785 but without the trailing ``str'', and set its sh_link
2786 field to point to this section. */
2787 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2788 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2793 len
= strlen (sec
->name
);
2794 alc
= (char *) bfd_malloc ((bfd_size_type
) (len
- 2));
2797 memcpy (alc
, sec
->name
, len
- 3);
2798 alc
[len
- 3] = '\0';
2799 s
= bfd_get_section_by_name (abfd
, alc
);
2803 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2805 /* This is a .stab section. */
2806 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
2807 elf_section_data (s
)->this_hdr
.sh_entsize
2808 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2815 case SHT_GNU_verneed
:
2816 case SHT_GNU_verdef
:
2817 /* sh_link is the section header index of the string table
2818 used for the dynamic entries, or the symbol table, or the
2820 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2822 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2826 case SHT_GNU_versym
:
2827 /* sh_link is the section header index of the symbol table
2828 this hash table or version table is for. */
2829 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2831 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2835 d
->this_hdr
.sh_link
= t
->symtab_section
;
2839 for (secn
= 1; secn
< section_number
; ++secn
)
2840 if (i_shdrp
[secn
] == NULL
)
2841 i_shdrp
[secn
] = i_shdrp
[0];
2843 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2844 i_shdrp
[secn
]->sh_name
);
2848 /* Map symbol from it's internal number to the external number, moving
2849 all local symbols to be at the head of the list. */
2852 sym_is_global (abfd
, sym
)
2856 /* If the backend has a special mapping, use it. */
2857 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2858 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2861 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2862 || bfd_is_und_section (bfd_get_section (sym
))
2863 || bfd_is_com_section (bfd_get_section (sym
)));
2867 elf_map_symbols (abfd
)
2870 unsigned int symcount
= bfd_get_symcount (abfd
);
2871 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2872 asymbol
**sect_syms
;
2873 unsigned int num_locals
= 0;
2874 unsigned int num_globals
= 0;
2875 unsigned int num_locals2
= 0;
2876 unsigned int num_globals2
= 0;
2884 fprintf (stderr
, "elf_map_symbols\n");
2888 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2890 if (max_index
< asect
->index
)
2891 max_index
= asect
->index
;
2895 amt
= max_index
* sizeof (asymbol
*);
2896 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2897 if (sect_syms
== NULL
)
2899 elf_section_syms (abfd
) = sect_syms
;
2900 elf_num_section_syms (abfd
) = max_index
;
2902 /* Init sect_syms entries for any section symbols we have already
2903 decided to output. */
2904 for (idx
= 0; idx
< symcount
; idx
++)
2906 asymbol
*sym
= syms
[idx
];
2908 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2915 if (sec
->owner
!= NULL
)
2917 if (sec
->owner
!= abfd
)
2919 if (sec
->output_offset
!= 0)
2922 sec
= sec
->output_section
;
2924 /* Empty sections in the input files may have had a
2925 section symbol created for them. (See the comment
2926 near the end of _bfd_generic_link_output_symbols in
2927 linker.c). If the linker script discards such
2928 sections then we will reach this point. Since we know
2929 that we cannot avoid this case, we detect it and skip
2930 the abort and the assignment to the sect_syms array.
2931 To reproduce this particular case try running the
2932 linker testsuite test ld-scripts/weak.exp for an ELF
2933 port that uses the generic linker. */
2934 if (sec
->owner
== NULL
)
2937 BFD_ASSERT (sec
->owner
== abfd
);
2939 sect_syms
[sec
->index
] = syms
[idx
];
2944 /* Classify all of the symbols. */
2945 for (idx
= 0; idx
< symcount
; idx
++)
2947 if (!sym_is_global (abfd
, syms
[idx
]))
2953 /* We will be adding a section symbol for each BFD section. Most normal
2954 sections will already have a section symbol in outsymbols, but
2955 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2956 at least in that case. */
2957 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2959 if (sect_syms
[asect
->index
] == NULL
)
2961 if (!sym_is_global (abfd
, asect
->symbol
))
2968 /* Now sort the symbols so the local symbols are first. */
2969 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2970 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2972 if (new_syms
== NULL
)
2975 for (idx
= 0; idx
< symcount
; idx
++)
2977 asymbol
*sym
= syms
[idx
];
2980 if (!sym_is_global (abfd
, sym
))
2983 i
= num_locals
+ num_globals2
++;
2985 sym
->udata
.i
= i
+ 1;
2987 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2989 if (sect_syms
[asect
->index
] == NULL
)
2991 asymbol
*sym
= asect
->symbol
;
2994 sect_syms
[asect
->index
] = sym
;
2995 if (!sym_is_global (abfd
, sym
))
2998 i
= num_locals
+ num_globals2
++;
3000 sym
->udata
.i
= i
+ 1;
3004 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3006 elf_num_locals (abfd
) = num_locals
;
3007 elf_num_globals (abfd
) = num_globals
;
3011 /* Align to the maximum file alignment that could be required for any
3012 ELF data structure. */
3014 static INLINE file_ptr align_file_position
3015 PARAMS ((file_ptr
, int));
3016 static INLINE file_ptr
3017 align_file_position (off
, align
)
3021 return (off
+ align
- 1) & ~(align
- 1);
3024 /* Assign a file position to a section, optionally aligning to the
3025 required section alignment. */
3028 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
3029 Elf_Internal_Shdr
*i_shdrp
;
3037 al
= i_shdrp
->sh_addralign
;
3039 offset
= BFD_ALIGN (offset
, al
);
3041 i_shdrp
->sh_offset
= offset
;
3042 if (i_shdrp
->bfd_section
!= NULL
)
3043 i_shdrp
->bfd_section
->filepos
= offset
;
3044 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3045 offset
+= i_shdrp
->sh_size
;
3049 /* Compute the file positions we are going to put the sections at, and
3050 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3051 is not NULL, this is being called by the ELF backend linker. */
3054 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
3056 struct bfd_link_info
*link_info
;
3058 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3060 struct bfd_strtab_hash
*strtab
;
3061 Elf_Internal_Shdr
*shstrtab_hdr
;
3063 if (abfd
->output_has_begun
)
3066 /* Do any elf backend specific processing first. */
3067 if (bed
->elf_backend_begin_write_processing
)
3068 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3070 if (! prep_headers (abfd
))
3073 /* Post process the headers if necessary. */
3074 if (bed
->elf_backend_post_process_headers
)
3075 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3078 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3082 if (!assign_section_numbers (abfd
))
3085 /* The backend linker builds symbol table information itself. */
3086 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3088 /* Non-zero if doing a relocatable link. */
3089 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3091 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3095 if (link_info
== NULL
)
3097 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3102 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3103 /* sh_name was set in prep_headers. */
3104 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3105 shstrtab_hdr
->sh_flags
= 0;
3106 shstrtab_hdr
->sh_addr
= 0;
3107 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3108 shstrtab_hdr
->sh_entsize
= 0;
3109 shstrtab_hdr
->sh_link
= 0;
3110 shstrtab_hdr
->sh_info
= 0;
3111 /* sh_offset is set in assign_file_positions_except_relocs. */
3112 shstrtab_hdr
->sh_addralign
= 1;
3114 if (!assign_file_positions_except_relocs (abfd
))
3117 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3120 Elf_Internal_Shdr
*hdr
;
3122 off
= elf_tdata (abfd
)->next_file_pos
;
3124 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3125 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3127 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3128 if (hdr
->sh_size
!= 0)
3129 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3131 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3132 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3134 elf_tdata (abfd
)->next_file_pos
= off
;
3136 /* Now that we know where the .strtab section goes, write it
3138 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3139 || ! _bfd_stringtab_emit (abfd
, strtab
))
3141 _bfd_stringtab_free (strtab
);
3144 abfd
->output_has_begun
= TRUE
;
3149 /* Create a mapping from a set of sections to a program segment. */
3151 static INLINE
struct elf_segment_map
*
3152 make_mapping (abfd
, sections
, from
, to
, phdr
)
3154 asection
**sections
;
3159 struct elf_segment_map
*m
;
3164 amt
= sizeof (struct elf_segment_map
);
3165 amt
+= (to
- from
- 1) * sizeof (asection
*);
3166 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3170 m
->p_type
= PT_LOAD
;
3171 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3172 m
->sections
[i
- from
] = *hdrpp
;
3173 m
->count
= to
- from
;
3175 if (from
== 0 && phdr
)
3177 /* Include the headers in the first PT_LOAD segment. */
3178 m
->includes_filehdr
= 1;
3179 m
->includes_phdrs
= 1;
3185 /* Set up a mapping from BFD sections to program segments. */
3188 map_sections_to_segments (abfd
)
3191 asection
**sections
= NULL
;
3195 struct elf_segment_map
*mfirst
;
3196 struct elf_segment_map
**pm
;
3197 struct elf_segment_map
*m
;
3199 unsigned int phdr_index
;
3200 bfd_vma maxpagesize
;
3202 bfd_boolean phdr_in_segment
= TRUE
;
3203 bfd_boolean writable
;
3205 asection
*first_tls
= NULL
;
3206 asection
*dynsec
, *eh_frame_hdr
;
3209 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3212 if (bfd_count_sections (abfd
) == 0)
3215 /* Select the allocated sections, and sort them. */
3217 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
3218 sections
= (asection
**) bfd_malloc (amt
);
3219 if (sections
== NULL
)
3223 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3225 if ((s
->flags
& SEC_ALLOC
) != 0)
3231 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3234 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3236 /* Build the mapping. */
3241 /* If we have a .interp section, then create a PT_PHDR segment for
3242 the program headers and a PT_INTERP segment for the .interp
3244 s
= bfd_get_section_by_name (abfd
, ".interp");
3245 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3247 amt
= sizeof (struct elf_segment_map
);
3248 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3252 m
->p_type
= PT_PHDR
;
3253 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3254 m
->p_flags
= PF_R
| PF_X
;
3255 m
->p_flags_valid
= 1;
3256 m
->includes_phdrs
= 1;
3261 amt
= sizeof (struct elf_segment_map
);
3262 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3266 m
->p_type
= PT_INTERP
;
3274 /* Look through the sections. We put sections in the same program
3275 segment when the start of the second section can be placed within
3276 a few bytes of the end of the first section. */
3279 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
3281 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3283 && (dynsec
->flags
& SEC_LOAD
) == 0)
3286 /* Deal with -Ttext or something similar such that the first section
3287 is not adjacent to the program headers. This is an
3288 approximation, since at this point we don't know exactly how many
3289 program headers we will need. */
3292 bfd_size_type phdr_size
;
3294 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3296 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3297 if ((abfd
->flags
& D_PAGED
) == 0
3298 || sections
[0]->lma
< phdr_size
3299 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3300 phdr_in_segment
= FALSE
;
3303 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3306 bfd_boolean new_segment
;
3310 /* See if this section and the last one will fit in the same
3313 if (last_hdr
== NULL
)
3315 /* If we don't have a segment yet, then we don't need a new
3316 one (we build the last one after this loop). */
3317 new_segment
= FALSE
;
3319 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3321 /* If this section has a different relation between the
3322 virtual address and the load address, then we need a new
3326 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3327 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3329 /* If putting this section in this segment would force us to
3330 skip a page in the segment, then we need a new segment. */
3333 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
3334 && (hdr
->flags
& SEC_LOAD
) != 0)
3336 /* We don't want to put a loadable section after a
3337 nonloadable section in the same segment. */
3340 else if ((abfd
->flags
& D_PAGED
) == 0)
3342 /* If the file is not demand paged, which means that we
3343 don't require the sections to be correctly aligned in the
3344 file, then there is no other reason for a new segment. */
3345 new_segment
= FALSE
;
3348 && (hdr
->flags
& SEC_READONLY
) == 0
3349 && (((last_hdr
->lma
+ last_hdr
->_raw_size
- 1)
3350 & ~(maxpagesize
- 1))
3351 != (hdr
->lma
& ~(maxpagesize
- 1))))
3353 /* We don't want to put a writable section in a read only
3354 segment, unless they are on the same page in memory
3355 anyhow. We already know that the last section does not
3356 bring us past the current section on the page, so the
3357 only case in which the new section is not on the same
3358 page as the previous section is when the previous section
3359 ends precisely on a page boundary. */
3364 /* Otherwise, we can use the same segment. */
3365 new_segment
= FALSE
;
3370 if ((hdr
->flags
& SEC_READONLY
) == 0)
3376 /* We need a new program segment. We must create a new program
3377 header holding all the sections from phdr_index until hdr. */
3379 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3386 if ((hdr
->flags
& SEC_READONLY
) == 0)
3393 phdr_in_segment
= FALSE
;
3396 /* Create a final PT_LOAD program segment. */
3397 if (last_hdr
!= NULL
)
3399 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3407 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3410 amt
= sizeof (struct elf_segment_map
);
3411 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3415 m
->p_type
= PT_DYNAMIC
;
3417 m
->sections
[0] = dynsec
;
3423 /* For each loadable .note section, add a PT_NOTE segment. We don't
3424 use bfd_get_section_by_name, because if we link together
3425 nonloadable .note sections and loadable .note sections, we will
3426 generate two .note sections in the output file. FIXME: Using
3427 names for section types is bogus anyhow. */
3428 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3430 if ((s
->flags
& SEC_LOAD
) != 0
3431 && strncmp (s
->name
, ".note", 5) == 0)
3433 amt
= sizeof (struct elf_segment_map
);
3434 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3438 m
->p_type
= PT_NOTE
;
3445 if (s
->flags
& SEC_THREAD_LOCAL
)
3453 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3458 amt
= sizeof (struct elf_segment_map
);
3459 amt
+= (tls_count
- 1) * sizeof (asection
*);
3460 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3465 m
->count
= tls_count
;
3466 /* Mandated PF_R. */
3468 m
->p_flags_valid
= 1;
3469 for (i
= 0; i
< tls_count
; ++i
)
3471 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3472 m
->sections
[i
] = first_tls
;
3473 first_tls
= first_tls
->next
;
3480 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3482 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
3483 if (eh_frame_hdr
!= NULL
3484 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
3486 amt
= sizeof (struct elf_segment_map
);
3487 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3491 m
->p_type
= PT_GNU_EH_FRAME
;
3493 m
->sections
[0] = eh_frame_hdr
->output_section
;
3502 elf_tdata (abfd
)->segment_map
= mfirst
;
3506 if (sections
!= NULL
)
3511 /* Sort sections by address. */
3514 elf_sort_sections (arg1
, arg2
)
3518 const asection
*sec1
= *(const asection
**) arg1
;
3519 const asection
*sec2
= *(const asection
**) arg2
;
3520 bfd_size_type size1
, size2
;
3522 /* Sort by LMA first, since this is the address used to
3523 place the section into a segment. */
3524 if (sec1
->lma
< sec2
->lma
)
3526 else if (sec1
->lma
> sec2
->lma
)
3529 /* Then sort by VMA. Normally the LMA and the VMA will be
3530 the same, and this will do nothing. */
3531 if (sec1
->vma
< sec2
->vma
)
3533 else if (sec1
->vma
> sec2
->vma
)
3536 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3538 #define TOEND(x) (((x)->flags & (SEC_LOAD|SEC_THREAD_LOCAL)) == 0)
3544 /* If the indicies are the same, do not return 0
3545 here, but continue to try the next comparison. */
3546 if (sec1
->target_index
- sec2
->target_index
!= 0)
3547 return sec1
->target_index
- sec2
->target_index
;
3552 else if (TOEND (sec2
))
3557 /* Sort by size, to put zero sized sections
3558 before others at the same address. */
3560 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->_raw_size
: 0;
3561 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->_raw_size
: 0;
3568 return sec1
->target_index
- sec2
->target_index
;
3571 /* Assign file positions to the sections based on the mapping from
3572 sections to segments. This function also sets up some fields in
3573 the file header, and writes out the program headers. */
3576 assign_file_positions_for_segments (abfd
)
3579 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3581 struct elf_segment_map
*m
;
3583 Elf_Internal_Phdr
*phdrs
;
3585 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3586 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3587 Elf_Internal_Phdr
*p
;
3590 if (elf_tdata (abfd
)->segment_map
== NULL
)
3592 if (! map_sections_to_segments (abfd
))
3597 /* The placement algorithm assumes that non allocated sections are
3598 not in PT_LOAD segments. We ensure this here by removing such
3599 sections from the segment map. */
3600 for (m
= elf_tdata (abfd
)->segment_map
;
3604 unsigned int new_count
;
3607 if (m
->p_type
!= PT_LOAD
)
3611 for (i
= 0; i
< m
->count
; i
++)
3613 if ((m
->sections
[i
]->flags
& SEC_ALLOC
) != 0)
3616 m
->sections
[new_count
] = m
->sections
[i
];
3622 if (new_count
!= m
->count
)
3623 m
->count
= new_count
;
3627 if (bed
->elf_backend_modify_segment_map
)
3629 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
3634 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3637 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3638 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3639 elf_elfheader (abfd
)->e_phnum
= count
;
3644 /* If we already counted the number of program segments, make sure
3645 that we allocated enough space. This happens when SIZEOF_HEADERS
3646 is used in a linker script. */
3647 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3648 if (alloc
!= 0 && count
> alloc
)
3650 ((*_bfd_error_handler
)
3651 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3652 bfd_get_filename (abfd
), alloc
, count
));
3653 bfd_set_error (bfd_error_bad_value
);
3660 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3661 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
3665 off
= bed
->s
->sizeof_ehdr
;
3666 off
+= alloc
* bed
->s
->sizeof_phdr
;
3673 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3680 /* If elf_segment_map is not from map_sections_to_segments, the
3681 sections may not be correctly ordered. NOTE: sorting should
3682 not be done to the PT_NOTE section of a corefile, which may
3683 contain several pseudo-sections artificially created by bfd.
3684 Sorting these pseudo-sections breaks things badly. */
3686 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3687 && m
->p_type
== PT_NOTE
))
3688 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3691 p
->p_type
= m
->p_type
;
3692 p
->p_flags
= m
->p_flags
;
3694 if (p
->p_type
== PT_LOAD
3696 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3698 if ((abfd
->flags
& D_PAGED
) != 0)
3699 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
3702 bfd_size_type align
;
3705 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3707 bfd_size_type secalign
;
3709 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3710 if (secalign
> align
)
3714 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
3721 p
->p_vaddr
= m
->sections
[0]->vma
;
3723 if (m
->p_paddr_valid
)
3724 p
->p_paddr
= m
->p_paddr
;
3725 else if (m
->count
== 0)
3728 p
->p_paddr
= m
->sections
[0]->lma
;
3730 if (p
->p_type
== PT_LOAD
3731 && (abfd
->flags
& D_PAGED
) != 0)
3732 p
->p_align
= bed
->maxpagesize
;
3733 else if (m
->count
== 0)
3734 p
->p_align
= bed
->s
->file_align
;
3742 if (m
->includes_filehdr
)
3744 if (! m
->p_flags_valid
)
3747 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3748 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3751 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3753 if (p
->p_vaddr
< (bfd_vma
) off
)
3755 (*_bfd_error_handler
)
3756 (_("%s: Not enough room for program headers, try linking with -N"),
3757 bfd_get_filename (abfd
));
3758 bfd_set_error (bfd_error_bad_value
);
3763 if (! m
->p_paddr_valid
)
3766 if (p
->p_type
== PT_LOAD
)
3768 filehdr_vaddr
= p
->p_vaddr
;
3769 filehdr_paddr
= p
->p_paddr
;
3773 if (m
->includes_phdrs
)
3775 if (! m
->p_flags_valid
)
3778 if (m
->includes_filehdr
)
3780 if (p
->p_type
== PT_LOAD
)
3782 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3783 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3788 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3792 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3793 p
->p_vaddr
-= off
- p
->p_offset
;
3794 if (! m
->p_paddr_valid
)
3795 p
->p_paddr
-= off
- p
->p_offset
;
3798 if (p
->p_type
== PT_LOAD
)
3800 phdrs_vaddr
= p
->p_vaddr
;
3801 phdrs_paddr
= p
->p_paddr
;
3804 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3807 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3808 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3811 if (p
->p_type
== PT_LOAD
3812 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3814 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3820 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3821 p
->p_filesz
+= adjust
;
3822 p
->p_memsz
+= adjust
;
3828 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3832 bfd_size_type align
;
3836 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3838 /* The section may have artificial alignment forced by a
3839 link script. Notice this case by the gap between the
3840 cumulative phdr lma and the section's lma. */
3841 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3843 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3845 p
->p_memsz
+= adjust
;
3846 if (p
->p_type
== PT_LOAD
3847 || (p
->p_type
== PT_NOTE
3848 && bfd_get_format (abfd
) == bfd_core
))
3853 if ((flags
& SEC_LOAD
) != 0
3854 || (flags
& SEC_THREAD_LOCAL
) != 0)
3855 p
->p_filesz
+= adjust
;
3858 if (p
->p_type
== PT_LOAD
)
3860 bfd_signed_vma adjust
;
3862 if ((flags
& SEC_LOAD
) != 0)
3864 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3868 else if ((flags
& SEC_ALLOC
) != 0)
3870 /* The section VMA must equal the file position
3871 modulo the page size. FIXME: I'm not sure if
3872 this adjustment is really necessary. We used to
3873 not have the SEC_LOAD case just above, and then
3874 this was necessary, but now I'm not sure. */
3875 if ((abfd
->flags
& D_PAGED
) != 0)
3876 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3878 adjust
= (sec
->vma
- voff
) % align
;
3887 (* _bfd_error_handler
) (_("\
3888 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3889 bfd_section_name (abfd
, sec
),
3894 p
->p_memsz
+= adjust
;
3897 if ((flags
& SEC_LOAD
) != 0)
3898 p
->p_filesz
+= adjust
;
3903 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3904 used in a linker script we may have a section with
3905 SEC_LOAD clear but which is supposed to have
3907 if ((flags
& SEC_LOAD
) != 0
3908 || (flags
& SEC_HAS_CONTENTS
) != 0)
3909 off
+= sec
->_raw_size
;
3911 if ((flags
& SEC_ALLOC
) != 0
3912 && ((flags
& SEC_LOAD
) != 0
3913 || (flags
& SEC_THREAD_LOCAL
) == 0))
3914 voff
+= sec
->_raw_size
;
3917 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3919 /* The actual "note" segment has i == 0.
3920 This is the one that actually contains everything. */
3924 p
->p_filesz
= sec
->_raw_size
;
3925 off
+= sec
->_raw_size
;
3930 /* Fake sections -- don't need to be written. */
3933 flags
= sec
->flags
= 0;
3940 if ((sec
->flags
& SEC_LOAD
) != 0
3941 || (sec
->flags
& SEC_THREAD_LOCAL
) == 0
3942 || p
->p_type
== PT_TLS
)
3943 p
->p_memsz
+= sec
->_raw_size
;
3945 if ((flags
& SEC_LOAD
) != 0)
3946 p
->p_filesz
+= sec
->_raw_size
;
3948 if (p
->p_type
== PT_TLS
3949 && sec
->_raw_size
== 0
3950 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
3952 struct bfd_link_order
*o
;
3953 bfd_vma tbss_size
= 0;
3955 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
3956 if (tbss_size
< o
->offset
+ o
->size
)
3957 tbss_size
= o
->offset
+ o
->size
;
3959 p
->p_memsz
+= tbss_size
;
3962 if (align
> p
->p_align
3963 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3967 if (! m
->p_flags_valid
)
3970 if ((flags
& SEC_CODE
) != 0)
3972 if ((flags
& SEC_READONLY
) == 0)
3978 /* Now that we have set the section file positions, we can set up
3979 the file positions for the non PT_LOAD segments. */
3980 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3984 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3986 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3987 p
->p_offset
= m
->sections
[0]->filepos
;
3991 if (m
->includes_filehdr
)
3993 p
->p_vaddr
= filehdr_vaddr
;
3994 if (! m
->p_paddr_valid
)
3995 p
->p_paddr
= filehdr_paddr
;
3997 else if (m
->includes_phdrs
)
3999 p
->p_vaddr
= phdrs_vaddr
;
4000 if (! m
->p_paddr_valid
)
4001 p
->p_paddr
= phdrs_paddr
;
4006 /* Clear out any program headers we allocated but did not use. */
4007 for (; count
< alloc
; count
++, p
++)
4009 memset (p
, 0, sizeof *p
);
4010 p
->p_type
= PT_NULL
;
4013 elf_tdata (abfd
)->phdr
= phdrs
;
4015 elf_tdata (abfd
)->next_file_pos
= off
;
4017 /* Write out the program headers. */
4018 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4019 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
4025 /* Get the size of the program header.
4027 If this is called by the linker before any of the section VMA's are set, it
4028 can't calculate the correct value for a strange memory layout. This only
4029 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4030 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4031 data segment (exclusive of .interp and .dynamic).
4033 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4034 will be two segments. */
4036 static bfd_size_type
4037 get_program_header_size (abfd
)
4042 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4044 /* We can't return a different result each time we're called. */
4045 if (elf_tdata (abfd
)->program_header_size
!= 0)
4046 return elf_tdata (abfd
)->program_header_size
;
4048 if (elf_tdata (abfd
)->segment_map
!= NULL
)
4050 struct elf_segment_map
*m
;
4053 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4055 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4056 return elf_tdata (abfd
)->program_header_size
;
4059 /* Assume we will need exactly two PT_LOAD segments: one for text
4060 and one for data. */
4063 s
= bfd_get_section_by_name (abfd
, ".interp");
4064 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4066 /* If we have a loadable interpreter section, we need a
4067 PT_INTERP segment. In this case, assume we also need a
4068 PT_PHDR segment, although that may not be true for all
4073 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4075 /* We need a PT_DYNAMIC segment. */
4079 if (elf_tdata (abfd
)->eh_frame_hdr
)
4081 /* We need a PT_GNU_EH_FRAME segment. */
4085 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4087 if ((s
->flags
& SEC_LOAD
) != 0
4088 && strncmp (s
->name
, ".note", 5) == 0)
4090 /* We need a PT_NOTE segment. */
4095 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4097 if (s
->flags
& SEC_THREAD_LOCAL
)
4099 /* We need a PT_TLS segment. */
4105 /* Let the backend count up any program headers it might need. */
4106 if (bed
->elf_backend_additional_program_headers
)
4110 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
4116 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4117 return elf_tdata (abfd
)->program_header_size
;
4120 /* Work out the file positions of all the sections. This is called by
4121 _bfd_elf_compute_section_file_positions. All the section sizes and
4122 VMAs must be known before this is called.
4124 We do not consider reloc sections at this point, unless they form
4125 part of the loadable image. Reloc sections are assigned file
4126 positions in assign_file_positions_for_relocs, which is called by
4127 write_object_contents and final_link.
4129 We also don't set the positions of the .symtab and .strtab here. */
4132 assign_file_positions_except_relocs (abfd
)
4135 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
4136 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
4137 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4138 unsigned int num_sec
= elf_numsections (abfd
);
4140 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4142 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4143 && bfd_get_format (abfd
) != bfd_core
)
4145 Elf_Internal_Shdr
**hdrpp
;
4148 /* Start after the ELF header. */
4149 off
= i_ehdrp
->e_ehsize
;
4151 /* We are not creating an executable, which means that we are
4152 not creating a program header, and that the actual order of
4153 the sections in the file is unimportant. */
4154 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4156 Elf_Internal_Shdr
*hdr
;
4159 if (hdr
->sh_type
== SHT_REL
4160 || hdr
->sh_type
== SHT_RELA
4161 || i
== tdata
->symtab_section
4162 || i
== tdata
->symtab_shndx_section
4163 || i
== tdata
->strtab_section
)
4165 hdr
->sh_offset
= -1;
4168 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4170 if (i
== SHN_LORESERVE
- 1)
4172 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4173 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4180 Elf_Internal_Shdr
**hdrpp
;
4182 /* Assign file positions for the loaded sections based on the
4183 assignment of sections to segments. */
4184 if (! assign_file_positions_for_segments (abfd
))
4187 /* Assign file positions for the other sections. */
4189 off
= elf_tdata (abfd
)->next_file_pos
;
4190 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4192 Elf_Internal_Shdr
*hdr
;
4195 if (hdr
->bfd_section
!= NULL
4196 && hdr
->bfd_section
->filepos
!= 0)
4197 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
4198 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4200 ((*_bfd_error_handler
)
4201 (_("%s: warning: allocated section `%s' not in segment"),
4202 bfd_get_filename (abfd
),
4203 (hdr
->bfd_section
== NULL
4205 : hdr
->bfd_section
->name
)));
4206 if ((abfd
->flags
& D_PAGED
) != 0)
4207 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
4209 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
4210 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4213 else if (hdr
->sh_type
== SHT_REL
4214 || hdr
->sh_type
== SHT_RELA
4215 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4216 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4217 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4218 hdr
->sh_offset
= -1;
4220 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4222 if (i
== SHN_LORESERVE
- 1)
4224 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4225 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4230 /* Place the section headers. */
4231 off
= align_file_position (off
, bed
->s
->file_align
);
4232 i_ehdrp
->e_shoff
= off
;
4233 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4235 elf_tdata (abfd
)->next_file_pos
= off
;
4244 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4245 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4246 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4247 struct elf_strtab_hash
*shstrtab
;
4248 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4250 i_ehdrp
= elf_elfheader (abfd
);
4251 i_shdrp
= elf_elfsections (abfd
);
4253 shstrtab
= _bfd_elf_strtab_init ();
4254 if (shstrtab
== NULL
)
4257 elf_shstrtab (abfd
) = shstrtab
;
4259 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4260 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4261 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4262 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4264 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4265 i_ehdrp
->e_ident
[EI_DATA
] =
4266 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4267 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4269 if ((abfd
->flags
& DYNAMIC
) != 0)
4270 i_ehdrp
->e_type
= ET_DYN
;
4271 else if ((abfd
->flags
& EXEC_P
) != 0)
4272 i_ehdrp
->e_type
= ET_EXEC
;
4273 else if (bfd_get_format (abfd
) == bfd_core
)
4274 i_ehdrp
->e_type
= ET_CORE
;
4276 i_ehdrp
->e_type
= ET_REL
;
4278 switch (bfd_get_arch (abfd
))
4280 case bfd_arch_unknown
:
4281 i_ehdrp
->e_machine
= EM_NONE
;
4284 /* There used to be a long list of cases here, each one setting
4285 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4286 in the corresponding bfd definition. To avoid duplication,
4287 the switch was removed. Machines that need special handling
4288 can generally do it in elf_backend_final_write_processing(),
4289 unless they need the information earlier than the final write.
4290 Such need can generally be supplied by replacing the tests for
4291 e_machine with the conditions used to determine it. */
4293 if (get_elf_backend_data (abfd
) != NULL
)
4294 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
4296 i_ehdrp
->e_machine
= EM_NONE
;
4299 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4300 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4302 /* No program header, for now. */
4303 i_ehdrp
->e_phoff
= 0;
4304 i_ehdrp
->e_phentsize
= 0;
4305 i_ehdrp
->e_phnum
= 0;
4307 /* Each bfd section is section header entry. */
4308 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4309 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4311 /* If we're building an executable, we'll need a program header table. */
4312 if (abfd
->flags
& EXEC_P
)
4314 /* It all happens later. */
4316 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
4318 /* elf_build_phdrs() returns a (NULL-terminated) array of
4319 Elf_Internal_Phdrs. */
4320 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
4321 i_ehdrp
->e_phoff
= outbase
;
4322 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
4327 i_ehdrp
->e_phentsize
= 0;
4329 i_ehdrp
->e_phoff
= 0;
4332 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4333 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
4334 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4335 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
4336 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4337 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
4338 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4339 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4340 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4346 /* Assign file positions for all the reloc sections which are not part
4347 of the loadable file image. */
4350 _bfd_elf_assign_file_positions_for_relocs (abfd
)
4354 unsigned int i
, num_sec
;
4355 Elf_Internal_Shdr
**shdrpp
;
4357 off
= elf_tdata (abfd
)->next_file_pos
;
4359 num_sec
= elf_numsections (abfd
);
4360 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4362 Elf_Internal_Shdr
*shdrp
;
4365 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4366 && shdrp
->sh_offset
== -1)
4367 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
4370 elf_tdata (abfd
)->next_file_pos
= off
;
4374 _bfd_elf_write_object_contents (abfd
)
4377 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4378 Elf_Internal_Ehdr
*i_ehdrp
;
4379 Elf_Internal_Shdr
**i_shdrp
;
4381 unsigned int count
, num_sec
;
4383 if (! abfd
->output_has_begun
4384 && ! _bfd_elf_compute_section_file_positions
4385 (abfd
, (struct bfd_link_info
*) NULL
))
4388 i_shdrp
= elf_elfsections (abfd
);
4389 i_ehdrp
= elf_elfheader (abfd
);
4392 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4396 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4398 /* After writing the headers, we need to write the sections too... */
4399 num_sec
= elf_numsections (abfd
);
4400 for (count
= 1; count
< num_sec
; count
++)
4402 if (bed
->elf_backend_section_processing
)
4403 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4404 if (i_shdrp
[count
]->contents
)
4406 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4408 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4409 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4412 if (count
== SHN_LORESERVE
- 1)
4413 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4416 /* Write out the section header names. */
4417 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4418 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4421 if (bed
->elf_backend_final_write_processing
)
4422 (*bed
->elf_backend_final_write_processing
) (abfd
,
4423 elf_tdata (abfd
)->linker
);
4425 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4429 _bfd_elf_write_corefile_contents (abfd
)
4432 /* Hopefully this can be done just like an object file. */
4433 return _bfd_elf_write_object_contents (abfd
);
4436 /* Given a section, search the header to find them. */
4439 _bfd_elf_section_from_bfd_section (abfd
, asect
)
4443 struct elf_backend_data
*bed
;
4446 if (elf_section_data (asect
) != NULL
4447 && elf_section_data (asect
)->this_idx
!= 0)
4448 return elf_section_data (asect
)->this_idx
;
4450 if (bfd_is_abs_section (asect
))
4452 else if (bfd_is_com_section (asect
))
4454 else if (bfd_is_und_section (asect
))
4458 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4459 int maxindex
= elf_numsections (abfd
);
4461 for (index
= 1; index
< maxindex
; index
++)
4463 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4465 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4471 bed
= get_elf_backend_data (abfd
);
4472 if (bed
->elf_backend_section_from_bfd_section
)
4476 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4481 bfd_set_error (bfd_error_nonrepresentable_section
);
4486 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4490 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
4492 asymbol
**asym_ptr_ptr
;
4494 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4496 flagword flags
= asym_ptr
->flags
;
4498 /* When gas creates relocations against local labels, it creates its
4499 own symbol for the section, but does put the symbol into the
4500 symbol chain, so udata is 0. When the linker is generating
4501 relocatable output, this section symbol may be for one of the
4502 input sections rather than the output section. */
4503 if (asym_ptr
->udata
.i
== 0
4504 && (flags
& BSF_SECTION_SYM
)
4505 && asym_ptr
->section
)
4509 if (asym_ptr
->section
->output_section
!= NULL
)
4510 indx
= asym_ptr
->section
->output_section
->index
;
4512 indx
= asym_ptr
->section
->index
;
4513 if (indx
< elf_num_section_syms (abfd
)
4514 && elf_section_syms (abfd
)[indx
] != NULL
)
4515 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4518 idx
= asym_ptr
->udata
.i
;
4522 /* This case can occur when using --strip-symbol on a symbol
4523 which is used in a relocation entry. */
4524 (*_bfd_error_handler
)
4525 (_("%s: symbol `%s' required but not present"),
4526 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4527 bfd_set_error (bfd_error_no_symbols
);
4534 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4535 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4536 elf_symbol_flags (flags
));
4544 /* Copy private BFD data. This copies any program header information. */
4547 copy_private_bfd_data (ibfd
, obfd
)
4551 Elf_Internal_Ehdr
*iehdr
;
4552 struct elf_segment_map
*map
;
4553 struct elf_segment_map
*map_first
;
4554 struct elf_segment_map
**pointer_to_map
;
4555 Elf_Internal_Phdr
*segment
;
4558 unsigned int num_segments
;
4559 bfd_boolean phdr_included
= FALSE
;
4560 bfd_vma maxpagesize
;
4561 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
4562 unsigned int phdr_adjust_num
= 0;
4563 struct elf_backend_data
*bed
;
4565 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4566 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4569 if (elf_tdata (ibfd
)->phdr
== NULL
)
4572 bed
= get_elf_backend_data (ibfd
);
4573 iehdr
= elf_elfheader (ibfd
);
4576 pointer_to_map
= &map_first
;
4578 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4579 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4581 /* Returns the end address of the segment + 1. */
4582 #define SEGMENT_END(segment, start) \
4583 (start + (segment->p_memsz > segment->p_filesz \
4584 ? segment->p_memsz : segment->p_filesz))
4586 #define SECTION_SIZE(section, segment) \
4587 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4588 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4589 ? section->_raw_size : 0)
4591 /* Returns TRUE if the given section is contained within
4592 the given segment. VMA addresses are compared. */
4593 #define IS_CONTAINED_BY_VMA(section, segment) \
4594 (section->vma >= segment->p_vaddr \
4595 && (section->vma + SECTION_SIZE (section, segment) \
4596 <= (SEGMENT_END (segment, segment->p_vaddr))))
4598 /* Returns TRUE if the given section is contained within
4599 the given segment. LMA addresses are compared. */
4600 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4601 (section->lma >= base \
4602 && (section->lma + SECTION_SIZE (section, segment) \
4603 <= SEGMENT_END (segment, base)))
4605 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4606 #define IS_COREFILE_NOTE(p, s) \
4607 (p->p_type == PT_NOTE \
4608 && bfd_get_format (ibfd) == bfd_core \
4609 && s->vma == 0 && s->lma == 0 \
4610 && (bfd_vma) s->filepos >= p->p_offset \
4611 && ((bfd_vma) s->filepos + s->_raw_size \
4612 <= p->p_offset + p->p_filesz))
4614 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4615 linker, which generates a PT_INTERP section with p_vaddr and
4616 p_memsz set to 0. */
4617 #define IS_SOLARIS_PT_INTERP(p, s) \
4619 && p->p_paddr == 0 \
4620 && p->p_memsz == 0 \
4621 && p->p_filesz > 0 \
4622 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4623 && s->_raw_size > 0 \
4624 && (bfd_vma) s->filepos >= p->p_offset \
4625 && ((bfd_vma) s->filepos + s->_raw_size \
4626 <= p->p_offset + p->p_filesz))
4628 /* Decide if the given section should be included in the given segment.
4629 A section will be included if:
4630 1. It is within the address space of the segment -- we use the LMA
4631 if that is set for the segment and the VMA otherwise,
4632 2. It is an allocated segment,
4633 3. There is an output section associated with it,
4634 4. The section has not already been allocated to a previous segment.
4635 5. PT_TLS segment includes only SHF_TLS sections.
4636 6. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4637 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4638 ((((segment->p_paddr \
4639 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4640 : IS_CONTAINED_BY_VMA (section, segment)) \
4641 && (section->flags & SEC_ALLOC) != 0) \
4642 || IS_COREFILE_NOTE (segment, section)) \
4643 && section->output_section != NULL \
4644 && (segment->p_type != PT_TLS \
4645 || (section->flags & SEC_THREAD_LOCAL)) \
4646 && (segment->p_type == PT_LOAD \
4647 || segment->p_type == PT_TLS \
4648 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4649 && ! section->segment_mark)
4651 /* Returns TRUE iff seg1 starts after the end of seg2. */
4652 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4653 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4655 /* Returns TRUE iff seg1 and seg2 overlap. */
4656 #define SEGMENT_OVERLAPS(seg1, seg2) \
4657 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) \
4658 || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4660 /* Initialise the segment mark field. */
4661 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4662 section
->segment_mark
= FALSE
;
4664 /* Scan through the segments specified in the program header
4665 of the input BFD. For this first scan we look for overlaps
4666 in the loadable segments. These can be created by weird
4667 parameters to objcopy. Also, fix some solaris weirdness. */
4668 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4673 Elf_Internal_Phdr
*segment2
;
4675 if (segment
->p_type
== PT_INTERP
)
4676 for (section
= ibfd
->sections
; section
; section
= section
->next
)
4677 if (IS_SOLARIS_PT_INTERP (segment
, section
))
4679 /* Mininal change so that the normal section to segment
4680 assigment code will work. */
4681 segment
->p_vaddr
= section
->vma
;
4685 if (segment
->p_type
!= PT_LOAD
)
4688 /* Determine if this segment overlaps any previous segments. */
4689 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4691 bfd_signed_vma extra_length
;
4693 if (segment2
->p_type
!= PT_LOAD
4694 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4697 /* Merge the two segments together. */
4698 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4700 /* Extend SEGMENT2 to include SEGMENT and then delete
4703 SEGMENT_END (segment
, segment
->p_vaddr
)
4704 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4706 if (extra_length
> 0)
4708 segment2
->p_memsz
+= extra_length
;
4709 segment2
->p_filesz
+= extra_length
;
4712 segment
->p_type
= PT_NULL
;
4714 /* Since we have deleted P we must restart the outer loop. */
4716 segment
= elf_tdata (ibfd
)->phdr
;
4721 /* Extend SEGMENT to include SEGMENT2 and then delete
4724 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4725 - SEGMENT_END (segment
, segment
->p_vaddr
);
4727 if (extra_length
> 0)
4729 segment
->p_memsz
+= extra_length
;
4730 segment
->p_filesz
+= extra_length
;
4733 segment2
->p_type
= PT_NULL
;
4738 /* The second scan attempts to assign sections to segments. */
4739 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4743 unsigned int section_count
;
4744 asection
** sections
;
4745 asection
* output_section
;
4747 bfd_vma matching_lma
;
4748 bfd_vma suggested_lma
;
4752 if (segment
->p_type
== PT_NULL
)
4755 /* Compute how many sections might be placed into this segment. */
4757 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4758 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4761 /* Allocate a segment map big enough to contain all of the
4762 sections we have selected. */
4763 amt
= sizeof (struct elf_segment_map
);
4764 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4765 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4769 /* Initialise the fields of the segment map. Default to
4770 using the physical address of the segment in the input BFD. */
4772 map
->p_type
= segment
->p_type
;
4773 map
->p_flags
= segment
->p_flags
;
4774 map
->p_flags_valid
= 1;
4775 map
->p_paddr
= segment
->p_paddr
;
4776 map
->p_paddr_valid
= 1;
4778 /* Determine if this segment contains the ELF file header
4779 and if it contains the program headers themselves. */
4780 map
->includes_filehdr
= (segment
->p_offset
== 0
4781 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4783 map
->includes_phdrs
= 0;
4785 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4787 map
->includes_phdrs
=
4788 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4789 && (segment
->p_offset
+ segment
->p_filesz
4790 >= ((bfd_vma
) iehdr
->e_phoff
4791 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4793 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4794 phdr_included
= TRUE
;
4797 if (section_count
== 0)
4799 /* Special segments, such as the PT_PHDR segment, may contain
4800 no sections, but ordinary, loadable segments should contain
4801 something. They are allowed by the ELF spec however, so only
4802 a warning is produced. */
4803 if (segment
->p_type
== PT_LOAD
)
4804 (*_bfd_error_handler
)
4805 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4806 bfd_archive_filename (ibfd
));
4809 *pointer_to_map
= map
;
4810 pointer_to_map
= &map
->next
;
4815 /* Now scan the sections in the input BFD again and attempt
4816 to add their corresponding output sections to the segment map.
4817 The problem here is how to handle an output section which has
4818 been moved (ie had its LMA changed). There are four possibilities:
4820 1. None of the sections have been moved.
4821 In this case we can continue to use the segment LMA from the
4824 2. All of the sections have been moved by the same amount.
4825 In this case we can change the segment's LMA to match the LMA
4826 of the first section.
4828 3. Some of the sections have been moved, others have not.
4829 In this case those sections which have not been moved can be
4830 placed in the current segment which will have to have its size,
4831 and possibly its LMA changed, and a new segment or segments will
4832 have to be created to contain the other sections.
4834 4. The sections have been moved, but not be the same amount.
4835 In this case we can change the segment's LMA to match the LMA
4836 of the first section and we will have to create a new segment
4837 or segments to contain the other sections.
4839 In order to save time, we allocate an array to hold the section
4840 pointers that we are interested in. As these sections get assigned
4841 to a segment, they are removed from this array. */
4843 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4844 to work around this long long bug. */
4845 amt
= section_count
* sizeof (asection
*);
4846 sections
= (asection
**) bfd_malloc (amt
);
4847 if (sections
== NULL
)
4850 /* Step One: Scan for segment vs section LMA conflicts.
4851 Also add the sections to the section array allocated above.
4852 Also add the sections to the current segment. In the common
4853 case, where the sections have not been moved, this means that
4854 we have completely filled the segment, and there is nothing
4860 for (j
= 0, section
= ibfd
->sections
;
4862 section
= section
->next
)
4864 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4866 output_section
= section
->output_section
;
4868 sections
[j
++] = section
;
4870 /* The Solaris native linker always sets p_paddr to 0.
4871 We try to catch that case here, and set it to the
4872 correct value. Note - some backends require that
4873 p_paddr be left as zero. */
4874 if (segment
->p_paddr
== 0
4875 && segment
->p_vaddr
!= 0
4876 && (! bed
->want_p_paddr_set_to_zero
)
4878 && output_section
->lma
!= 0
4879 && (output_section
->vma
== (segment
->p_vaddr
4880 + (map
->includes_filehdr
4883 + (map
->includes_phdrs
4885 * iehdr
->e_phentsize
)
4887 map
->p_paddr
= segment
->p_vaddr
;
4889 /* Match up the physical address of the segment with the
4890 LMA address of the output section. */
4891 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4892 || IS_COREFILE_NOTE (segment
, section
)
4893 || (bed
->want_p_paddr_set_to_zero
&&
4894 IS_CONTAINED_BY_VMA (output_section
, segment
))
4897 if (matching_lma
== 0)
4898 matching_lma
= output_section
->lma
;
4900 /* We assume that if the section fits within the segment
4901 then it does not overlap any other section within that
4903 map
->sections
[isec
++] = output_section
;
4905 else if (suggested_lma
== 0)
4906 suggested_lma
= output_section
->lma
;
4910 BFD_ASSERT (j
== section_count
);
4912 /* Step Two: Adjust the physical address of the current segment,
4914 if (isec
== section_count
)
4916 /* All of the sections fitted within the segment as currently
4917 specified. This is the default case. Add the segment to
4918 the list of built segments and carry on to process the next
4919 program header in the input BFD. */
4920 map
->count
= section_count
;
4921 *pointer_to_map
= map
;
4922 pointer_to_map
= &map
->next
;
4929 if (matching_lma
!= 0)
4931 /* At least one section fits inside the current segment.
4932 Keep it, but modify its physical address to match the
4933 LMA of the first section that fitted. */
4934 map
->p_paddr
= matching_lma
;
4938 /* None of the sections fitted inside the current segment.
4939 Change the current segment's physical address to match
4940 the LMA of the first section. */
4941 map
->p_paddr
= suggested_lma
;
4944 /* Offset the segment physical address from the lma
4945 to allow for space taken up by elf headers. */
4946 if (map
->includes_filehdr
)
4947 map
->p_paddr
-= iehdr
->e_ehsize
;
4949 if (map
->includes_phdrs
)
4951 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4953 /* iehdr->e_phnum is just an estimate of the number
4954 of program headers that we will need. Make a note
4955 here of the number we used and the segment we chose
4956 to hold these headers, so that we can adjust the
4957 offset when we know the correct value. */
4958 phdr_adjust_num
= iehdr
->e_phnum
;
4959 phdr_adjust_seg
= map
;
4963 /* Step Three: Loop over the sections again, this time assigning
4964 those that fit to the current segment and removing them from the
4965 sections array; but making sure not to leave large gaps. Once all
4966 possible sections have been assigned to the current segment it is
4967 added to the list of built segments and if sections still remain
4968 to be assigned, a new segment is constructed before repeating
4976 /* Fill the current segment with sections that fit. */
4977 for (j
= 0; j
< section_count
; j
++)
4979 section
= sections
[j
];
4981 if (section
== NULL
)
4984 output_section
= section
->output_section
;
4986 BFD_ASSERT (output_section
!= NULL
);
4988 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4989 || IS_COREFILE_NOTE (segment
, section
))
4991 if (map
->count
== 0)
4993 /* If the first section in a segment does not start at
4994 the beginning of the segment, then something is
4996 if (output_section
->lma
!=
4998 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4999 + (map
->includes_phdrs
5000 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5006 asection
* prev_sec
;
5008 prev_sec
= map
->sections
[map
->count
- 1];
5010 /* If the gap between the end of the previous section
5011 and the start of this section is more than
5012 maxpagesize then we need to start a new segment. */
5013 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
5015 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5016 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
5017 > output_section
->lma
))
5019 if (suggested_lma
== 0)
5020 suggested_lma
= output_section
->lma
;
5026 map
->sections
[map
->count
++] = output_section
;
5029 section
->segment_mark
= TRUE
;
5031 else if (suggested_lma
== 0)
5032 suggested_lma
= output_section
->lma
;
5035 BFD_ASSERT (map
->count
> 0);
5037 /* Add the current segment to the list of built segments. */
5038 *pointer_to_map
= map
;
5039 pointer_to_map
= &map
->next
;
5041 if (isec
< section_count
)
5043 /* We still have not allocated all of the sections to
5044 segments. Create a new segment here, initialise it
5045 and carry on looping. */
5046 amt
= sizeof (struct elf_segment_map
);
5047 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5048 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
5052 /* Initialise the fields of the segment map. Set the physical
5053 physical address to the LMA of the first section that has
5054 not yet been assigned. */
5056 map
->p_type
= segment
->p_type
;
5057 map
->p_flags
= segment
->p_flags
;
5058 map
->p_flags_valid
= 1;
5059 map
->p_paddr
= suggested_lma
;
5060 map
->p_paddr_valid
= 1;
5061 map
->includes_filehdr
= 0;
5062 map
->includes_phdrs
= 0;
5065 while (isec
< section_count
);
5070 /* The Solaris linker creates program headers in which all the
5071 p_paddr fields are zero. When we try to objcopy or strip such a
5072 file, we get confused. Check for this case, and if we find it
5073 reset the p_paddr_valid fields. */
5074 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5075 if (map
->p_paddr
!= 0)
5079 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5080 map
->p_paddr_valid
= 0;
5083 elf_tdata (obfd
)->segment_map
= map_first
;
5085 /* If we had to estimate the number of program headers that were
5086 going to be needed, then check our estimate now and adjust
5087 the offset if necessary. */
5088 if (phdr_adjust_seg
!= NULL
)
5092 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5095 if (count
> phdr_adjust_num
)
5096 phdr_adjust_seg
->p_paddr
5097 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5101 /* Final Step: Sort the segments into ascending order of physical
5103 if (map_first
!= NULL
)
5105 struct elf_segment_map
*prev
;
5108 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
5110 /* Yes I know - its a bubble sort.... */
5111 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
5113 /* Swap map and map->next. */
5114 prev
->next
= map
->next
;
5115 map
->next
= map
->next
->next
;
5116 prev
->next
->next
= map
;
5127 #undef IS_CONTAINED_BY_VMA
5128 #undef IS_CONTAINED_BY_LMA
5129 #undef IS_COREFILE_NOTE
5130 #undef IS_SOLARIS_PT_INTERP
5131 #undef INCLUDE_SECTION_IN_SEGMENT
5132 #undef SEGMENT_AFTER_SEGMENT
5133 #undef SEGMENT_OVERLAPS
5137 /* Copy private section information. This copies over the entsize
5138 field, and sometimes the info field. */
5141 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
5147 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5149 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5150 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5153 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
5157 /* Only set up the segments if there are no more SEC_ALLOC
5158 sections. FIXME: This won't do the right thing if objcopy is
5159 used to remove the last SEC_ALLOC section, since objcopy
5160 won't call this routine in that case. */
5161 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
5162 if ((s
->flags
& SEC_ALLOC
) != 0)
5166 if (! copy_private_bfd_data (ibfd
, obfd
))
5171 ihdr
= &elf_section_data (isec
)->this_hdr
;
5172 ohdr
= &elf_section_data (osec
)->this_hdr
;
5174 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
5176 if (ihdr
->sh_type
== SHT_SYMTAB
5177 || ihdr
->sh_type
== SHT_DYNSYM
5178 || ihdr
->sh_type
== SHT_GNU_verneed
5179 || ihdr
->sh_type
== SHT_GNU_verdef
)
5180 ohdr
->sh_info
= ihdr
->sh_info
;
5182 /* Set things up for objcopy. The output SHT_GROUP section will
5183 have its elf_next_in_group pointing back to the input group
5185 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5186 elf_group_name (osec
) = elf_group_name (isec
);
5188 osec
->use_rela_p
= isec
->use_rela_p
;
5193 /* Copy private symbol information. If this symbol is in a section
5194 which we did not map into a BFD section, try to map the section
5195 index correctly. We use special macro definitions for the mapped
5196 section indices; these definitions are interpreted by the
5197 swap_out_syms function. */
5199 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5200 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5201 #define MAP_STRTAB (SHN_HIOS + 3)
5202 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5203 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5206 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
5212 elf_symbol_type
*isym
, *osym
;
5214 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5215 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5218 isym
= elf_symbol_from (ibfd
, isymarg
);
5219 osym
= elf_symbol_from (obfd
, osymarg
);
5223 && bfd_is_abs_section (isym
->symbol
.section
))
5227 shndx
= isym
->internal_elf_sym
.st_shndx
;
5228 if (shndx
== elf_onesymtab (ibfd
))
5229 shndx
= MAP_ONESYMTAB
;
5230 else if (shndx
== elf_dynsymtab (ibfd
))
5231 shndx
= MAP_DYNSYMTAB
;
5232 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
5234 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
5235 shndx
= MAP_SHSTRTAB
;
5236 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
5237 shndx
= MAP_SYM_SHNDX
;
5238 osym
->internal_elf_sym
.st_shndx
= shndx
;
5244 /* Swap out the symbols. */
5247 swap_out_syms (abfd
, sttp
, relocatable_p
)
5249 struct bfd_strtab_hash
**sttp
;
5252 struct elf_backend_data
*bed
;
5255 struct bfd_strtab_hash
*stt
;
5256 Elf_Internal_Shdr
*symtab_hdr
;
5257 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5258 Elf_Internal_Shdr
*symstrtab_hdr
;
5259 char *outbound_syms
;
5260 char *outbound_shndx
;
5264 if (!elf_map_symbols (abfd
))
5267 /* Dump out the symtabs. */
5268 stt
= _bfd_elf_stringtab_init ();
5272 bed
= get_elf_backend_data (abfd
);
5273 symcount
= bfd_get_symcount (abfd
);
5274 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5275 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5276 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5277 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5278 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5279 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5281 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5282 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5284 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
5285 outbound_syms
= bfd_alloc (abfd
, amt
);
5286 if (outbound_syms
== NULL
)
5288 symtab_hdr
->contents
= (PTR
) outbound_syms
;
5290 outbound_shndx
= NULL
;
5291 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5292 if (symtab_shndx_hdr
->sh_name
!= 0)
5294 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5295 outbound_shndx
= bfd_zalloc (abfd
, amt
);
5296 if (outbound_shndx
== NULL
)
5298 symtab_shndx_hdr
->contents
= outbound_shndx
;
5299 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5300 symtab_shndx_hdr
->sh_size
= amt
;
5301 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5302 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5305 /* now generate the data (for "contents") */
5307 /* Fill in zeroth symbol and swap it out. */
5308 Elf_Internal_Sym sym
;
5314 sym
.st_shndx
= SHN_UNDEF
;
5315 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5316 outbound_syms
+= bed
->s
->sizeof_sym
;
5317 if (outbound_shndx
!= NULL
)
5318 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5321 syms
= bfd_get_outsymbols (abfd
);
5322 for (idx
= 0; idx
< symcount
; idx
++)
5324 Elf_Internal_Sym sym
;
5325 bfd_vma value
= syms
[idx
]->value
;
5326 elf_symbol_type
*type_ptr
;
5327 flagword flags
= syms
[idx
]->flags
;
5330 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5332 /* Local section symbols have no name. */
5337 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5340 if (sym
.st_name
== (unsigned long) -1)
5344 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5346 if ((flags
& BSF_SECTION_SYM
) == 0
5347 && bfd_is_com_section (syms
[idx
]->section
))
5349 /* ELF common symbols put the alignment into the `value' field,
5350 and the size into the `size' field. This is backwards from
5351 how BFD handles it, so reverse it here. */
5352 sym
.st_size
= value
;
5353 if (type_ptr
== NULL
5354 || type_ptr
->internal_elf_sym
.st_value
== 0)
5355 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5357 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5358 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5359 (abfd
, syms
[idx
]->section
);
5363 asection
*sec
= syms
[idx
]->section
;
5366 if (sec
->output_section
)
5368 value
+= sec
->output_offset
;
5369 sec
= sec
->output_section
;
5371 /* Don't add in the section vma for relocatable output. */
5372 if (! relocatable_p
)
5374 sym
.st_value
= value
;
5375 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5377 if (bfd_is_abs_section (sec
)
5379 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5381 /* This symbol is in a real ELF section which we did
5382 not create as a BFD section. Undo the mapping done
5383 by copy_private_symbol_data. */
5384 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5388 shndx
= elf_onesymtab (abfd
);
5391 shndx
= elf_dynsymtab (abfd
);
5394 shndx
= elf_tdata (abfd
)->strtab_section
;
5397 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5400 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5408 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5414 /* Writing this would be a hell of a lot easier if
5415 we had some decent documentation on bfd, and
5416 knew what to expect of the library, and what to
5417 demand of applications. For example, it
5418 appears that `objcopy' might not set the
5419 section of a symbol to be a section that is
5420 actually in the output file. */
5421 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5422 BFD_ASSERT (sec2
!= 0);
5423 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5424 BFD_ASSERT (shndx
!= -1);
5428 sym
.st_shndx
= shndx
;
5431 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5433 else if ((flags
& BSF_FUNCTION
) != 0)
5435 else if ((flags
& BSF_OBJECT
) != 0)
5440 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5443 /* Processor-specific types */
5444 if (type_ptr
!= NULL
5445 && bed
->elf_backend_get_symbol_type
)
5446 type
= ((*bed
->elf_backend_get_symbol_type
)
5447 (&type_ptr
->internal_elf_sym
, type
));
5449 if (flags
& BSF_SECTION_SYM
)
5451 if (flags
& BSF_GLOBAL
)
5452 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5454 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5456 else if (bfd_is_com_section (syms
[idx
]->section
))
5457 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5458 else if (bfd_is_und_section (syms
[idx
]->section
))
5459 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5463 else if (flags
& BSF_FILE
)
5464 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5467 int bind
= STB_LOCAL
;
5469 if (flags
& BSF_LOCAL
)
5471 else if (flags
& BSF_WEAK
)
5473 else if (flags
& BSF_GLOBAL
)
5476 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5479 if (type_ptr
!= NULL
)
5480 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5484 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5485 outbound_syms
+= bed
->s
->sizeof_sym
;
5486 if (outbound_shndx
!= NULL
)
5487 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5491 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5492 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5494 symstrtab_hdr
->sh_flags
= 0;
5495 symstrtab_hdr
->sh_addr
= 0;
5496 symstrtab_hdr
->sh_entsize
= 0;
5497 symstrtab_hdr
->sh_link
= 0;
5498 symstrtab_hdr
->sh_info
= 0;
5499 symstrtab_hdr
->sh_addralign
= 1;
5504 /* Return the number of bytes required to hold the symtab vector.
5506 Note that we base it on the count plus 1, since we will null terminate
5507 the vector allocated based on this size. However, the ELF symbol table
5508 always has a dummy entry as symbol #0, so it ends up even. */
5511 _bfd_elf_get_symtab_upper_bound (abfd
)
5516 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5518 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5519 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5521 symtab_size
-= sizeof (asymbol
*);
5527 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
5532 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5534 if (elf_dynsymtab (abfd
) == 0)
5536 bfd_set_error (bfd_error_invalid_operation
);
5540 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5541 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5543 symtab_size
-= sizeof (asymbol
*);
5549 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
5550 bfd
*abfd ATTRIBUTE_UNUSED
;
5553 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5556 /* Canonicalize the relocs. */
5559 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
5567 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5569 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
5572 tblptr
= section
->relocation
;
5573 for (i
= 0; i
< section
->reloc_count
; i
++)
5574 *relptr
++ = tblptr
++;
5578 return section
->reloc_count
;
5582 _bfd_elf_get_symtab (abfd
, alocation
)
5584 asymbol
**alocation
;
5586 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5587 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, FALSE
);
5590 bfd_get_symcount (abfd
) = symcount
;
5595 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
5597 asymbol
**alocation
;
5599 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5600 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, TRUE
);
5603 bfd_get_dynamic_symcount (abfd
) = symcount
;
5607 /* Return the size required for the dynamic reloc entries. Any
5608 section that was actually installed in the BFD, and has type
5609 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5610 considered to be a dynamic reloc section. */
5613 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
5619 if (elf_dynsymtab (abfd
) == 0)
5621 bfd_set_error (bfd_error_invalid_operation
);
5625 ret
= sizeof (arelent
*);
5626 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5627 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5628 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5629 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5630 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5631 * sizeof (arelent
*));
5636 /* Canonicalize the dynamic relocation entries. Note that we return
5637 the dynamic relocations as a single block, although they are
5638 actually associated with particular sections; the interface, which
5639 was designed for SunOS style shared libraries, expects that there
5640 is only one set of dynamic relocs. Any section that was actually
5641 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5642 the dynamic symbol table, is considered to be a dynamic reloc
5646 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
5651 bfd_boolean (*slurp_relocs
)
5652 PARAMS ((bfd
*, asection
*, asymbol
**, bfd_boolean
));
5656 if (elf_dynsymtab (abfd
) == 0)
5658 bfd_set_error (bfd_error_invalid_operation
);
5662 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5664 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5666 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5667 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5668 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5673 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
5675 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5677 for (i
= 0; i
< count
; i
++)
5688 /* Read in the version information. */
5691 _bfd_elf_slurp_version_tables (abfd
)
5694 bfd_byte
*contents
= NULL
;
5697 if (elf_dynverdef (abfd
) != 0)
5699 Elf_Internal_Shdr
*hdr
;
5700 Elf_External_Verdef
*everdef
;
5701 Elf_Internal_Verdef
*iverdef
;
5702 Elf_Internal_Verdef
*iverdefarr
;
5703 Elf_Internal_Verdef iverdefmem
;
5705 unsigned int maxidx
;
5707 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5709 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5710 if (contents
== NULL
)
5712 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5713 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5716 /* We know the number of entries in the section but not the maximum
5717 index. Therefore we have to run through all entries and find
5719 everdef
= (Elf_External_Verdef
*) contents
;
5721 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5723 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5725 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5726 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5728 everdef
= ((Elf_External_Verdef
*)
5729 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5732 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5733 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
5734 if (elf_tdata (abfd
)->verdef
== NULL
)
5737 elf_tdata (abfd
)->cverdefs
= maxidx
;
5739 everdef
= (Elf_External_Verdef
*) contents
;
5740 iverdefarr
= elf_tdata (abfd
)->verdef
;
5741 for (i
= 0; i
< hdr
->sh_info
; i
++)
5743 Elf_External_Verdaux
*everdaux
;
5744 Elf_Internal_Verdaux
*iverdaux
;
5747 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5749 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5750 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5752 iverdef
->vd_bfd
= abfd
;
5754 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5755 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
5756 if (iverdef
->vd_auxptr
== NULL
)
5759 everdaux
= ((Elf_External_Verdaux
*)
5760 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5761 iverdaux
= iverdef
->vd_auxptr
;
5762 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5764 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5766 iverdaux
->vda_nodename
=
5767 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5768 iverdaux
->vda_name
);
5769 if (iverdaux
->vda_nodename
== NULL
)
5772 if (j
+ 1 < iverdef
->vd_cnt
)
5773 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5775 iverdaux
->vda_nextptr
= NULL
;
5777 everdaux
= ((Elf_External_Verdaux
*)
5778 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5781 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5783 if (i
+ 1 < hdr
->sh_info
)
5784 iverdef
->vd_nextdef
= iverdef
+ 1;
5786 iverdef
->vd_nextdef
= NULL
;
5788 everdef
= ((Elf_External_Verdef
*)
5789 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5796 if (elf_dynverref (abfd
) != 0)
5798 Elf_Internal_Shdr
*hdr
;
5799 Elf_External_Verneed
*everneed
;
5800 Elf_Internal_Verneed
*iverneed
;
5803 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5805 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5806 elf_tdata (abfd
)->verref
=
5807 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
5808 if (elf_tdata (abfd
)->verref
== NULL
)
5811 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5813 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5814 if (contents
== NULL
)
5816 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5817 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5820 everneed
= (Elf_External_Verneed
*) contents
;
5821 iverneed
= elf_tdata (abfd
)->verref
;
5822 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5824 Elf_External_Vernaux
*evernaux
;
5825 Elf_Internal_Vernaux
*ivernaux
;
5828 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5830 iverneed
->vn_bfd
= abfd
;
5832 iverneed
->vn_filename
=
5833 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5835 if (iverneed
->vn_filename
== NULL
)
5838 amt
= iverneed
->vn_cnt
;
5839 amt
*= sizeof (Elf_Internal_Vernaux
);
5840 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
5842 evernaux
= ((Elf_External_Vernaux
*)
5843 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5844 ivernaux
= iverneed
->vn_auxptr
;
5845 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5847 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5849 ivernaux
->vna_nodename
=
5850 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5851 ivernaux
->vna_name
);
5852 if (ivernaux
->vna_nodename
== NULL
)
5855 if (j
+ 1 < iverneed
->vn_cnt
)
5856 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5858 ivernaux
->vna_nextptr
= NULL
;
5860 evernaux
= ((Elf_External_Vernaux
*)
5861 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5864 if (i
+ 1 < hdr
->sh_info
)
5865 iverneed
->vn_nextref
= iverneed
+ 1;
5867 iverneed
->vn_nextref
= NULL
;
5869 everneed
= ((Elf_External_Verneed
*)
5870 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5880 if (contents
== NULL
)
5886 _bfd_elf_make_empty_symbol (abfd
)
5889 elf_symbol_type
*newsym
;
5890 bfd_size_type amt
= sizeof (elf_symbol_type
);
5892 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
5897 newsym
->symbol
.the_bfd
= abfd
;
5898 return &newsym
->symbol
;
5903 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
5904 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5908 bfd_symbol_info (symbol
, ret
);
5911 /* Return whether a symbol name implies a local symbol. Most targets
5912 use this function for the is_local_label_name entry point, but some
5916 _bfd_elf_is_local_label_name (abfd
, name
)
5917 bfd
*abfd ATTRIBUTE_UNUSED
;
5920 /* Normal local symbols start with ``.L''. */
5921 if (name
[0] == '.' && name
[1] == 'L')
5924 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5925 DWARF debugging symbols starting with ``..''. */
5926 if (name
[0] == '.' && name
[1] == '.')
5929 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5930 emitting DWARF debugging output. I suspect this is actually a
5931 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5932 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5933 underscore to be emitted on some ELF targets). For ease of use,
5934 we treat such symbols as local. */
5935 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5942 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5943 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5944 asymbol
*symbol ATTRIBUTE_UNUSED
;
5951 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5953 enum bfd_architecture arch
;
5954 unsigned long machine
;
5956 /* If this isn't the right architecture for this backend, and this
5957 isn't the generic backend, fail. */
5958 if (arch
!= get_elf_backend_data (abfd
)->arch
5959 && arch
!= bfd_arch_unknown
5960 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5963 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5966 /* Find the function to a particular section and offset,
5967 for error reporting. */
5970 elf_find_function (abfd
, section
, symbols
, offset
,
5971 filename_ptr
, functionname_ptr
)
5972 bfd
*abfd ATTRIBUTE_UNUSED
;
5976 const char **filename_ptr
;
5977 const char **functionname_ptr
;
5979 const char *filename
;
5988 for (p
= symbols
; *p
!= NULL
; p
++)
5992 q
= (elf_symbol_type
*) *p
;
5994 if (bfd_get_section (&q
->symbol
) != section
)
5997 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
6002 filename
= bfd_asymbol_name (&q
->symbol
);
6006 if (q
->symbol
.section
== section
6007 && q
->symbol
.value
>= low_func
6008 && q
->symbol
.value
<= offset
)
6010 func
= (asymbol
*) q
;
6011 low_func
= q
->symbol
.value
;
6021 *filename_ptr
= filename
;
6022 if (functionname_ptr
)
6023 *functionname_ptr
= bfd_asymbol_name (func
);
6028 /* Find the nearest line to a particular section and offset,
6029 for error reporting. */
6032 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
6033 filename_ptr
, functionname_ptr
, line_ptr
)
6038 const char **filename_ptr
;
6039 const char **functionname_ptr
;
6040 unsigned int *line_ptr
;
6044 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
6045 filename_ptr
, functionname_ptr
,
6048 if (!*functionname_ptr
)
6049 elf_find_function (abfd
, section
, symbols
, offset
,
6050 *filename_ptr
? NULL
: filename_ptr
,
6056 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
6057 filename_ptr
, functionname_ptr
,
6059 &elf_tdata (abfd
)->dwarf2_find_line_info
))
6061 if (!*functionname_ptr
)
6062 elf_find_function (abfd
, section
, symbols
, offset
,
6063 *filename_ptr
? NULL
: filename_ptr
,
6069 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
6070 &found
, filename_ptr
,
6071 functionname_ptr
, line_ptr
,
6072 &elf_tdata (abfd
)->line_info
))
6074 if (found
&& (*functionname_ptr
|| *line_ptr
))
6077 if (symbols
== NULL
)
6080 if (! elf_find_function (abfd
, section
, symbols
, offset
,
6081 filename_ptr
, functionname_ptr
))
6089 _bfd_elf_sizeof_headers (abfd
, reloc
)
6095 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
6097 ret
+= get_program_header_size (abfd
);
6102 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
6107 bfd_size_type count
;
6109 Elf_Internal_Shdr
*hdr
;
6112 if (! abfd
->output_has_begun
6113 && ! (_bfd_elf_compute_section_file_positions
6114 (abfd
, (struct bfd_link_info
*) NULL
)))
6117 hdr
= &elf_section_data (section
)->this_hdr
;
6118 pos
= hdr
->sh_offset
+ offset
;
6119 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6120 || bfd_bwrite (location
, count
, abfd
) != count
)
6127 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
6128 bfd
*abfd ATTRIBUTE_UNUSED
;
6129 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
6130 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
6135 /* Try to convert a non-ELF reloc into an ELF one. */
6138 _bfd_elf_validate_reloc (abfd
, areloc
)
6142 /* Check whether we really have an ELF howto. */
6144 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
6146 bfd_reloc_code_real_type code
;
6147 reloc_howto_type
*howto
;
6149 /* Alien reloc: Try to determine its type to replace it with an
6150 equivalent ELF reloc. */
6152 if (areloc
->howto
->pc_relative
)
6154 switch (areloc
->howto
->bitsize
)
6157 code
= BFD_RELOC_8_PCREL
;
6160 code
= BFD_RELOC_12_PCREL
;
6163 code
= BFD_RELOC_16_PCREL
;
6166 code
= BFD_RELOC_24_PCREL
;
6169 code
= BFD_RELOC_32_PCREL
;
6172 code
= BFD_RELOC_64_PCREL
;
6178 howto
= bfd_reloc_type_lookup (abfd
, code
);
6180 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
6182 if (howto
->pcrel_offset
)
6183 areloc
->addend
+= areloc
->address
;
6185 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
6190 switch (areloc
->howto
->bitsize
)
6196 code
= BFD_RELOC_14
;
6199 code
= BFD_RELOC_16
;
6202 code
= BFD_RELOC_26
;
6205 code
= BFD_RELOC_32
;
6208 code
= BFD_RELOC_64
;
6214 howto
= bfd_reloc_type_lookup (abfd
, code
);
6218 areloc
->howto
= howto
;
6226 (*_bfd_error_handler
)
6227 (_("%s: unsupported relocation type %s"),
6228 bfd_archive_filename (abfd
), areloc
->howto
->name
);
6229 bfd_set_error (bfd_error_bad_value
);
6234 _bfd_elf_close_and_cleanup (abfd
)
6237 if (bfd_get_format (abfd
) == bfd_object
)
6239 if (elf_shstrtab (abfd
) != NULL
)
6240 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
6243 return _bfd_generic_close_and_cleanup (abfd
);
6246 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6247 in the relocation's offset. Thus we cannot allow any sort of sanity
6248 range-checking to interfere. There is nothing else to do in processing
6251 bfd_reloc_status_type
6252 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
6253 bfd
*abfd ATTRIBUTE_UNUSED
;
6254 arelent
*re ATTRIBUTE_UNUSED
;
6255 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
6256 PTR data ATTRIBUTE_UNUSED
;
6257 asection
*is ATTRIBUTE_UNUSED
;
6258 bfd
*obfd ATTRIBUTE_UNUSED
;
6259 char **errmsg ATTRIBUTE_UNUSED
;
6261 return bfd_reloc_ok
;
6264 /* Elf core file support. Much of this only works on native
6265 toolchains, since we rely on knowing the
6266 machine-dependent procfs structure in order to pick
6267 out details about the corefile. */
6269 #ifdef HAVE_SYS_PROCFS_H
6270 # include <sys/procfs.h>
6273 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6276 elfcore_make_pid (abfd
)
6279 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
6280 + (elf_tdata (abfd
)->core_pid
));
6283 /* If there isn't a section called NAME, make one, using
6284 data from SECT. Note, this function will generate a
6285 reference to NAME, so you shouldn't deallocate or
6289 elfcore_maybe_make_sect (abfd
, name
, sect
)
6296 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
6299 sect2
= bfd_make_section (abfd
, name
);
6303 sect2
->_raw_size
= sect
->_raw_size
;
6304 sect2
->filepos
= sect
->filepos
;
6305 sect2
->flags
= sect
->flags
;
6306 sect2
->alignment_power
= sect
->alignment_power
;
6310 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6311 actually creates up to two pseudosections:
6312 - For the single-threaded case, a section named NAME, unless
6313 such a section already exists.
6314 - For the multi-threaded case, a section named "NAME/PID", where
6315 PID is elfcore_make_pid (abfd).
6316 Both pseudosections have identical contents. */
6318 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
6325 char *threaded_name
;
6329 /* Build the section name. */
6331 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6332 len
= strlen (buf
) + 1;
6333 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
6334 if (threaded_name
== NULL
)
6336 memcpy (threaded_name
, buf
, len
);
6338 sect
= bfd_make_section (abfd
, threaded_name
);
6341 sect
->_raw_size
= size
;
6342 sect
->filepos
= filepos
;
6343 sect
->flags
= SEC_HAS_CONTENTS
;
6344 sect
->alignment_power
= 2;
6346 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6349 /* prstatus_t exists on:
6351 linux 2.[01] + glibc
6355 #if defined (HAVE_PRSTATUS_T)
6356 static bfd_boolean elfcore_grok_prstatus
6357 PARAMS ((bfd
*, Elf_Internal_Note
*));
6360 elfcore_grok_prstatus (abfd
, note
)
6362 Elf_Internal_Note
*note
;
6367 if (note
->descsz
== sizeof (prstatus_t
))
6371 raw_size
= sizeof (prstat
.pr_reg
);
6372 offset
= offsetof (prstatus_t
, pr_reg
);
6373 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6375 /* Do not overwrite the core signal if it
6376 has already been set by another thread. */
6377 if (elf_tdata (abfd
)->core_signal
== 0)
6378 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6379 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6381 /* pr_who exists on:
6384 pr_who doesn't exist on:
6387 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6388 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6391 #if defined (HAVE_PRSTATUS32_T)
6392 else if (note
->descsz
== sizeof (prstatus32_t
))
6394 /* 64-bit host, 32-bit corefile */
6395 prstatus32_t prstat
;
6397 raw_size
= sizeof (prstat
.pr_reg
);
6398 offset
= offsetof (prstatus32_t
, pr_reg
);
6399 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6401 /* Do not overwrite the core signal if it
6402 has already been set by another thread. */
6403 if (elf_tdata (abfd
)->core_signal
== 0)
6404 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6405 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6407 /* pr_who exists on:
6410 pr_who doesn't exist on:
6413 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6414 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6417 #endif /* HAVE_PRSTATUS32_T */
6420 /* Fail - we don't know how to handle any other
6421 note size (ie. data object type). */
6425 /* Make a ".reg/999" section and a ".reg" section. */
6426 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6427 raw_size
, note
->descpos
+ offset
);
6429 #endif /* defined (HAVE_PRSTATUS_T) */
6431 /* Create a pseudosection containing the exact contents of NOTE. */
6433 elfcore_make_note_pseudosection (abfd
, name
, note
)
6436 Elf_Internal_Note
*note
;
6438 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6439 note
->descsz
, note
->descpos
);
6442 /* There isn't a consistent prfpregset_t across platforms,
6443 but it doesn't matter, because we don't have to pick this
6444 data structure apart. */
6447 elfcore_grok_prfpreg (abfd
, note
)
6449 Elf_Internal_Note
*note
;
6451 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6454 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6455 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6459 elfcore_grok_prxfpreg (abfd
, note
)
6461 Elf_Internal_Note
*note
;
6463 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6466 #if defined (HAVE_PRPSINFO_T)
6467 typedef prpsinfo_t elfcore_psinfo_t
;
6468 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6469 typedef prpsinfo32_t elfcore_psinfo32_t
;
6473 #if defined (HAVE_PSINFO_T)
6474 typedef psinfo_t elfcore_psinfo_t
;
6475 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6476 typedef psinfo32_t elfcore_psinfo32_t
;
6480 /* return a malloc'ed copy of a string at START which is at
6481 most MAX bytes long, possibly without a terminating '\0'.
6482 the copy will always have a terminating '\0'. */
6485 _bfd_elfcore_strndup (abfd
, start
, max
)
6491 char *end
= memchr (start
, '\0', max
);
6499 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
6503 memcpy (dups
, start
, len
);
6509 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6510 static bfd_boolean elfcore_grok_psinfo
6511 PARAMS ((bfd
*, Elf_Internal_Note
*));
6514 elfcore_grok_psinfo (abfd
, note
)
6516 Elf_Internal_Note
*note
;
6518 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6520 elfcore_psinfo_t psinfo
;
6522 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6524 elf_tdata (abfd
)->core_program
6525 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6526 sizeof (psinfo
.pr_fname
));
6528 elf_tdata (abfd
)->core_command
6529 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6530 sizeof (psinfo
.pr_psargs
));
6532 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6533 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6535 /* 64-bit host, 32-bit corefile */
6536 elfcore_psinfo32_t psinfo
;
6538 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6540 elf_tdata (abfd
)->core_program
6541 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6542 sizeof (psinfo
.pr_fname
));
6544 elf_tdata (abfd
)->core_command
6545 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6546 sizeof (psinfo
.pr_psargs
));
6552 /* Fail - we don't know how to handle any other
6553 note size (ie. data object type). */
6557 /* Note that for some reason, a spurious space is tacked
6558 onto the end of the args in some (at least one anyway)
6559 implementations, so strip it off if it exists. */
6562 char *command
= elf_tdata (abfd
)->core_command
;
6563 int n
= strlen (command
);
6565 if (0 < n
&& command
[n
- 1] == ' ')
6566 command
[n
- 1] = '\0';
6571 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6573 #if defined (HAVE_PSTATUS_T)
6574 static bfd_boolean elfcore_grok_pstatus
6575 PARAMS ((bfd
*, Elf_Internal_Note
*));
6578 elfcore_grok_pstatus (abfd
, note
)
6580 Elf_Internal_Note
*note
;
6582 if (note
->descsz
== sizeof (pstatus_t
)
6583 #if defined (HAVE_PXSTATUS_T)
6584 || note
->descsz
== sizeof (pxstatus_t
)
6590 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6592 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6594 #if defined (HAVE_PSTATUS32_T)
6595 else if (note
->descsz
== sizeof (pstatus32_t
))
6597 /* 64-bit host, 32-bit corefile */
6600 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6602 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6605 /* Could grab some more details from the "representative"
6606 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6607 NT_LWPSTATUS note, presumably. */
6611 #endif /* defined (HAVE_PSTATUS_T) */
6613 #if defined (HAVE_LWPSTATUS_T)
6614 static bfd_boolean elfcore_grok_lwpstatus
6615 PARAMS ((bfd
*, Elf_Internal_Note
*));
6618 elfcore_grok_lwpstatus (abfd
, note
)
6620 Elf_Internal_Note
*note
;
6622 lwpstatus_t lwpstat
;
6628 if (note
->descsz
!= sizeof (lwpstat
)
6629 #if defined (HAVE_LWPXSTATUS_T)
6630 && note
->descsz
!= sizeof (lwpxstatus_t
)
6635 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6637 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6638 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6640 /* Make a ".reg/999" section. */
6642 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6643 len
= strlen (buf
) + 1;
6644 name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
6647 memcpy (name
, buf
, len
);
6649 sect
= bfd_make_section (abfd
, name
);
6653 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6654 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6655 sect
->filepos
= note
->descpos
6656 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6659 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6660 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6661 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6664 sect
->flags
= SEC_HAS_CONTENTS
;
6665 sect
->alignment_power
= 2;
6667 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6670 /* Make a ".reg2/999" section */
6672 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6673 len
= strlen (buf
) + 1;
6674 name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
6677 memcpy (name
, buf
, len
);
6679 sect
= bfd_make_section (abfd
, name
);
6683 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6684 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6685 sect
->filepos
= note
->descpos
6686 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6689 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6690 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6691 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6694 sect
->flags
= SEC_HAS_CONTENTS
;
6695 sect
->alignment_power
= 2;
6697 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6699 #endif /* defined (HAVE_LWPSTATUS_T) */
6701 #if defined (HAVE_WIN32_PSTATUS_T)
6703 elfcore_grok_win32pstatus (abfd
, note
)
6705 Elf_Internal_Note
*note
;
6711 win32_pstatus_t pstatus
;
6713 if (note
->descsz
< sizeof (pstatus
))
6716 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
6718 switch (pstatus
.data_type
)
6720 case NOTE_INFO_PROCESS
:
6721 /* FIXME: need to add ->core_command. */
6722 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6723 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6726 case NOTE_INFO_THREAD
:
6727 /* Make a ".reg/999" section. */
6728 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6730 len
= strlen (buf
) + 1;
6731 name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
6735 memcpy (name
, buf
, len
);
6737 sect
= bfd_make_section (abfd
, name
);
6741 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6742 sect
->filepos
= (note
->descpos
6743 + offsetof (struct win32_pstatus
,
6744 data
.thread_info
.thread_context
));
6745 sect
->flags
= SEC_HAS_CONTENTS
;
6746 sect
->alignment_power
= 2;
6748 if (pstatus
.data
.thread_info
.is_active_thread
)
6749 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6753 case NOTE_INFO_MODULE
:
6754 /* Make a ".module/xxxxxxxx" section. */
6755 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6757 len
= strlen (buf
) + 1;
6758 name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
6762 memcpy (name
, buf
, len
);
6764 sect
= bfd_make_section (abfd
, name
);
6769 sect
->_raw_size
= note
->descsz
;
6770 sect
->filepos
= note
->descpos
;
6771 sect
->flags
= SEC_HAS_CONTENTS
;
6772 sect
->alignment_power
= 2;
6781 #endif /* HAVE_WIN32_PSTATUS_T */
6784 elfcore_grok_note (abfd
, note
)
6786 Elf_Internal_Note
*note
;
6788 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6796 if (bed
->elf_backend_grok_prstatus
)
6797 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6799 #if defined (HAVE_PRSTATUS_T)
6800 return elfcore_grok_prstatus (abfd
, note
);
6805 #if defined (HAVE_PSTATUS_T)
6807 return elfcore_grok_pstatus (abfd
, note
);
6810 #if defined (HAVE_LWPSTATUS_T)
6812 return elfcore_grok_lwpstatus (abfd
, note
);
6815 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6816 return elfcore_grok_prfpreg (abfd
, note
);
6818 #if defined (HAVE_WIN32_PSTATUS_T)
6819 case NT_WIN32PSTATUS
:
6820 return elfcore_grok_win32pstatus (abfd
, note
);
6823 case NT_PRXFPREG
: /* Linux SSE extension */
6824 if (note
->namesz
== 6
6825 && strcmp (note
->namedata
, "LINUX") == 0)
6826 return elfcore_grok_prxfpreg (abfd
, note
);
6832 if (bed
->elf_backend_grok_psinfo
)
6833 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6835 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6836 return elfcore_grok_psinfo (abfd
, note
);
6844 elfcore_netbsd_get_lwpid (note
, lwpidp
)
6845 Elf_Internal_Note
*note
;
6850 cp
= strchr (note
->namedata
, '@');
6853 *lwpidp
= atoi(cp
+ 1);
6860 elfcore_grok_netbsd_procinfo (abfd
, note
)
6862 Elf_Internal_Note
*note
;
6865 /* Signal number at offset 0x08. */
6866 elf_tdata (abfd
)->core_signal
6867 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6869 /* Process ID at offset 0x50. */
6870 elf_tdata (abfd
)->core_pid
6871 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6873 /* Command name at 0x7c (max 32 bytes, including nul). */
6874 elf_tdata (abfd
)->core_command
6875 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6881 elfcore_grok_netbsd_note (abfd
, note
)
6883 Elf_Internal_Note
*note
;
6887 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6888 elf_tdata (abfd
)->core_lwpid
= lwp
;
6890 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6892 /* NetBSD-specific core "procinfo". Note that we expect to
6893 find this note before any of the others, which is fine,
6894 since the kernel writes this note out first when it
6895 creates a core file. */
6897 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6900 /* As of Jan 2002 there are no other machine-independent notes
6901 defined for NetBSD core files. If the note type is less
6902 than the start of the machine-dependent note types, we don't
6905 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
6909 switch (bfd_get_arch (abfd
))
6911 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6912 PT_GETFPREGS == mach+2. */
6914 case bfd_arch_alpha
:
6915 case bfd_arch_sparc
:
6918 case NT_NETBSDCORE_FIRSTMACH
+0:
6919 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6921 case NT_NETBSDCORE_FIRSTMACH
+2:
6922 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6928 /* On all other arch's, PT_GETREGS == mach+1 and
6929 PT_GETFPREGS == mach+3. */
6934 case NT_NETBSDCORE_FIRSTMACH
+1:
6935 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6937 case NT_NETBSDCORE_FIRSTMACH
+3:
6938 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6947 /* Function: elfcore_write_note
6954 size of data for note
6957 End of buffer containing note. */
6960 elfcore_write_note (abfd
, buf
, bufsiz
, name
, type
, input
, size
)
6969 Elf_External_Note
*xnp
;
6979 struct elf_backend_data
*bed
;
6981 namesz
= strlen (name
) + 1;
6982 bed
= get_elf_backend_data (abfd
);
6983 pad
= -namesz
& (bed
->s
->file_align
- 1);
6986 newspace
= sizeof (Elf_External_Note
) - 1 + namesz
+ pad
+ size
;
6988 p
= realloc (buf
, *bufsiz
+ newspace
);
6990 *bufsiz
+= newspace
;
6991 xnp
= (Elf_External_Note
*) dest
;
6992 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
6993 H_PUT_32 (abfd
, size
, xnp
->descsz
);
6994 H_PUT_32 (abfd
, type
, xnp
->type
);
6998 memcpy (dest
, name
, namesz
);
7006 memcpy (dest
, input
, size
);
7010 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7012 elfcore_write_prpsinfo (abfd
, buf
, bufsiz
, fname
, psargs
)
7020 char *note_name
= "CORE";
7022 #if defined (HAVE_PSINFO_T)
7024 note_type
= NT_PSINFO
;
7027 note_type
= NT_PRPSINFO
;
7030 memset (&data
, 0, sizeof (data
));
7031 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
7032 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
7033 return elfcore_write_note (abfd
, buf
, bufsiz
,
7034 note_name
, note_type
, &data
, sizeof (data
));
7036 #endif /* PSINFO_T or PRPSINFO_T */
7038 #if defined (HAVE_PRSTATUS_T)
7040 elfcore_write_prstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
7049 char *note_name
= "CORE";
7051 memset (&prstat
, 0, sizeof (prstat
));
7052 prstat
.pr_pid
= pid
;
7053 prstat
.pr_cursig
= cursig
;
7054 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
7055 return elfcore_write_note (abfd
, buf
, bufsiz
,
7056 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
7058 #endif /* HAVE_PRSTATUS_T */
7060 #if defined (HAVE_LWPSTATUS_T)
7062 elfcore_write_lwpstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
7070 lwpstatus_t lwpstat
;
7071 char *note_name
= "CORE";
7073 memset (&lwpstat
, 0, sizeof (lwpstat
));
7074 lwpstat
.pr_lwpid
= pid
>> 16;
7075 lwpstat
.pr_cursig
= cursig
;
7076 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7077 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
7078 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7080 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
7081 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
7083 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
7084 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
7087 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7088 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
7090 #endif /* HAVE_LWPSTATUS_T */
7092 #if defined (HAVE_PSTATUS_T)
7094 elfcore_write_pstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
7103 char *note_name
= "CORE";
7105 memset (&pstat
, 0, sizeof (pstat
));
7106 pstat
.pr_pid
= pid
& 0xffff;
7107 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7108 NT_PSTATUS
, &pstat
, sizeof (pstat
));
7111 #endif /* HAVE_PSTATUS_T */
7114 elfcore_write_prfpreg (abfd
, buf
, bufsiz
, fpregs
, size
)
7121 char *note_name
= "CORE";
7122 return elfcore_write_note (abfd
, buf
, bufsiz
,
7123 note_name
, NT_FPREGSET
, fpregs
, size
);
7127 elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, xfpregs
, size
)
7134 char *note_name
= "LINUX";
7135 return elfcore_write_note (abfd
, buf
, bufsiz
,
7136 note_name
, NT_PRXFPREG
, xfpregs
, size
);
7140 elfcore_read_notes (abfd
, offset
, size
)
7151 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
7154 buf
= bfd_malloc (size
);
7158 if (bfd_bread (buf
, size
, abfd
) != size
)
7166 while (p
< buf
+ size
)
7168 /* FIXME: bad alignment assumption. */
7169 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
7170 Elf_Internal_Note in
;
7172 in
.type
= H_GET_32 (abfd
, xnp
->type
);
7174 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
7175 in
.namedata
= xnp
->name
;
7177 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
7178 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
7179 in
.descpos
= offset
+ (in
.descdata
- buf
);
7181 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
7183 if (! elfcore_grok_netbsd_note (abfd
, &in
))
7188 if (! elfcore_grok_note (abfd
, &in
))
7192 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
7199 /* Providing external access to the ELF program header table. */
7201 /* Return an upper bound on the number of bytes required to store a
7202 copy of ABFD's program header table entries. Return -1 if an error
7203 occurs; bfd_get_error will return an appropriate code. */
7206 bfd_get_elf_phdr_upper_bound (abfd
)
7209 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7211 bfd_set_error (bfd_error_wrong_format
);
7215 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
7218 /* Copy ABFD's program header table entries to *PHDRS. The entries
7219 will be stored as an array of Elf_Internal_Phdr structures, as
7220 defined in include/elf/internal.h. To find out how large the
7221 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7223 Return the number of program header table entries read, or -1 if an
7224 error occurs; bfd_get_error will return an appropriate code. */
7227 bfd_get_elf_phdrs (abfd
, phdrs
)
7233 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7235 bfd_set_error (bfd_error_wrong_format
);
7239 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
7240 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
7241 num_phdrs
* sizeof (Elf_Internal_Phdr
));
7247 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
7248 bfd
*abfd ATTRIBUTE_UNUSED
;
7253 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7255 i_ehdrp
= elf_elfheader (abfd
);
7256 if (i_ehdrp
== NULL
)
7257 sprintf_vma (buf
, value
);
7260 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7262 #if BFD_HOST_64BIT_LONG
7263 sprintf (buf
, "%016lx", value
);
7265 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
7266 _bfd_int64_low (value
));
7270 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
7273 sprintf_vma (buf
, value
);
7278 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
7279 bfd
*abfd ATTRIBUTE_UNUSED
;
7284 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7286 i_ehdrp
= elf_elfheader (abfd
);
7287 if (i_ehdrp
== NULL
)
7288 fprintf_vma ((FILE *) stream
, value
);
7291 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7293 #if BFD_HOST_64BIT_LONG
7294 fprintf ((FILE *) stream
, "%016lx", value
);
7296 fprintf ((FILE *) stream
, "%08lx%08lx",
7297 _bfd_int64_high (value
), _bfd_int64_low (value
));
7301 fprintf ((FILE *) stream
, "%08lx",
7302 (unsigned long) (value
& 0xffffffff));
7305 fprintf_vma ((FILE *) stream
, value
);
7309 enum elf_reloc_type_class
7310 _bfd_elf_reloc_type_class (rela
)
7311 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
7313 return reloc_class_normal
;
7316 /* For RELA architectures, return the relocation value for a
7317 relocation against a local symbol. */
7320 _bfd_elf_rela_local_sym (abfd
, sym
, sec
, rel
)
7322 Elf_Internal_Sym
*sym
;
7324 Elf_Internal_Rela
*rel
;
7328 relocation
= (sec
->output_section
->vma
7329 + sec
->output_offset
7331 if ((sec
->flags
& SEC_MERGE
)
7332 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
7333 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
7339 _bfd_merged_section_offset (abfd
, &msec
,
7340 elf_section_data (sec
)->sec_info
,
7341 sym
->st_value
+ rel
->r_addend
,
7344 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
7350 _bfd_elf_rel_local_sym (abfd
, sym
, psec
, addend
)
7352 Elf_Internal_Sym
*sym
;
7356 asection
*sec
= *psec
;
7358 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7359 return sym
->st_value
+ addend
;
7361 return _bfd_merged_section_offset (abfd
, psec
,
7362 elf_section_data (sec
)->sec_info
,
7363 sym
->st_value
+ addend
, (bfd_vma
) 0);
7367 _bfd_elf_section_offset (abfd
, info
, sec
, offset
)
7369 struct bfd_link_info
*info
;
7373 struct bfd_elf_section_data
*sec_data
;
7375 sec_data
= elf_section_data (sec
);
7376 switch (sec
->sec_info_type
)
7378 case ELF_INFO_TYPE_STABS
:
7379 return _bfd_stab_section_offset (abfd
,
7380 &elf_hash_table (info
)->merge_info
,
7381 sec
, &sec_data
->sec_info
, offset
);
7382 case ELF_INFO_TYPE_EH_FRAME
:
7383 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);