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 int elf_sort_sections (const void *, const void *);
44 static bfd_boolean
assign_file_positions_except_relocs (bfd
*);
45 static bfd_boolean
prep_headers (bfd
*);
46 static bfd_boolean
swap_out_syms (bfd
*, struct bfd_strtab_hash
**, int) ;
47 static bfd_boolean
elfcore_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
49 /* Swap version information in and out. The version information is
50 currently size independent. If that ever changes, this code will
51 need to move into elfcode.h. */
53 /* Swap in a Verdef structure. */
56 _bfd_elf_swap_verdef_in (bfd
*abfd
,
57 const Elf_External_Verdef
*src
,
58 Elf_Internal_Verdef
*dst
)
60 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
61 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
62 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
63 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
64 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
65 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
66 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
69 /* Swap out a Verdef structure. */
72 _bfd_elf_swap_verdef_out (bfd
*abfd
,
73 const Elf_Internal_Verdef
*src
,
74 Elf_External_Verdef
*dst
)
76 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
77 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
78 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
79 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
80 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
81 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
82 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
85 /* Swap in a Verdaux structure. */
88 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
89 const Elf_External_Verdaux
*src
,
90 Elf_Internal_Verdaux
*dst
)
92 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
93 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
96 /* Swap out a Verdaux structure. */
99 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
100 const Elf_Internal_Verdaux
*src
,
101 Elf_External_Verdaux
*dst
)
103 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
104 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
107 /* Swap in a Verneed structure. */
110 _bfd_elf_swap_verneed_in (bfd
*abfd
,
111 const Elf_External_Verneed
*src
,
112 Elf_Internal_Verneed
*dst
)
114 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
115 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
116 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
117 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
118 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
121 /* Swap out a Verneed structure. */
124 _bfd_elf_swap_verneed_out (bfd
*abfd
,
125 const Elf_Internal_Verneed
*src
,
126 Elf_External_Verneed
*dst
)
128 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
129 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
130 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
131 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
132 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
135 /* Swap in a Vernaux structure. */
138 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
139 const Elf_External_Vernaux
*src
,
140 Elf_Internal_Vernaux
*dst
)
142 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
143 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
144 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
145 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
146 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
149 /* Swap out a Vernaux structure. */
152 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
153 const Elf_Internal_Vernaux
*src
,
154 Elf_External_Vernaux
*dst
)
156 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
157 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
158 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
159 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
160 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
163 /* Swap in a Versym structure. */
166 _bfd_elf_swap_versym_in (bfd
*abfd
,
167 const Elf_External_Versym
*src
,
168 Elf_Internal_Versym
*dst
)
170 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
173 /* Swap out a Versym structure. */
176 _bfd_elf_swap_versym_out (bfd
*abfd
,
177 const Elf_Internal_Versym
*src
,
178 Elf_External_Versym
*dst
)
180 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
183 /* Standard ELF hash function. Do not change this function; you will
184 cause invalid hash tables to be generated. */
187 bfd_elf_hash (const char *namearg
)
189 const unsigned char *name
= (const unsigned char *) namearg
;
194 while ((ch
= *name
++) != '\0')
197 if ((g
= (h
& 0xf0000000)) != 0)
200 /* The ELF ABI says `h &= ~g', but this is equivalent in
201 this case and on some machines one insn instead of two. */
205 return h
& 0xffffffff;
208 /* Read a specified number of bytes at a specified offset in an ELF
209 file, into a newly allocated buffer, and return a pointer to the
213 elf_read (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
217 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
219 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
221 if (bfd_bread (buf
, size
, abfd
) != size
)
223 if (bfd_get_error () != bfd_error_system_call
)
224 bfd_set_error (bfd_error_file_truncated
);
231 bfd_elf_mkobject (bfd
*abfd
)
233 /* This just does initialization. */
234 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
235 elf_tdata (abfd
) = bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
236 if (elf_tdata (abfd
) == 0)
238 /* Since everything is done at close time, do we need any
245 bfd_elf_mkcorefile (bfd
*abfd
)
247 /* I think this can be done just like an object file. */
248 return bfd_elf_mkobject (abfd
);
252 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
254 Elf_Internal_Shdr
**i_shdrp
;
255 char *shstrtab
= NULL
;
257 bfd_size_type shstrtabsize
;
259 i_shdrp
= elf_elfsections (abfd
);
260 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
263 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
264 if (shstrtab
== NULL
)
266 /* No cached one, attempt to read, and cache what we read. */
267 offset
= i_shdrp
[shindex
]->sh_offset
;
268 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
269 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
270 i_shdrp
[shindex
]->contents
= shstrtab
;
276 bfd_elf_string_from_elf_section (bfd
*abfd
,
277 unsigned int shindex
,
278 unsigned int strindex
)
280 Elf_Internal_Shdr
*hdr
;
285 hdr
= elf_elfsections (abfd
)[shindex
];
287 if (hdr
->contents
== NULL
288 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
291 if (strindex
>= hdr
->sh_size
)
293 (*_bfd_error_handler
)
294 (_("%s: invalid string offset %u >= %lu for section `%s'"),
295 bfd_archive_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
296 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
297 && strindex
== hdr
->sh_name
)
299 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
303 return ((char *) hdr
->contents
) + strindex
;
306 /* Read and convert symbols to internal format.
307 SYMCOUNT specifies the number of symbols to read, starting from
308 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
309 are non-NULL, they are used to store the internal symbols, external
310 symbols, and symbol section index extensions, respectively. */
313 bfd_elf_get_elf_syms (bfd
*ibfd
,
314 Elf_Internal_Shdr
*symtab_hdr
,
317 Elf_Internal_Sym
*intsym_buf
,
319 Elf_External_Sym_Shndx
*extshndx_buf
)
321 Elf_Internal_Shdr
*shndx_hdr
;
323 const bfd_byte
*esym
;
324 Elf_External_Sym_Shndx
*alloc_extshndx
;
325 Elf_External_Sym_Shndx
*shndx
;
326 Elf_Internal_Sym
*isym
;
327 Elf_Internal_Sym
*isymend
;
328 const struct elf_backend_data
*bed
;
336 /* Normal syms might have section extension entries. */
338 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
339 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
341 /* Read the symbols. */
343 alloc_extshndx
= NULL
;
344 bed
= get_elf_backend_data (ibfd
);
345 extsym_size
= bed
->s
->sizeof_sym
;
346 amt
= symcount
* extsym_size
;
347 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
348 if (extsym_buf
== NULL
)
350 alloc_ext
= bfd_malloc (amt
);
351 extsym_buf
= alloc_ext
;
353 if (extsym_buf
== NULL
354 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
355 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
361 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
365 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
366 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
367 if (extshndx_buf
== NULL
)
369 alloc_extshndx
= bfd_malloc (amt
);
370 extshndx_buf
= alloc_extshndx
;
372 if (extshndx_buf
== NULL
373 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
374 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
381 if (intsym_buf
== NULL
)
383 bfd_size_type amt
= symcount
* sizeof (Elf_Internal_Sym
);
384 intsym_buf
= bfd_malloc (amt
);
385 if (intsym_buf
== NULL
)
389 /* Convert the symbols to internal form. */
390 isymend
= intsym_buf
+ symcount
;
391 for (esym
= extsym_buf
, isym
= intsym_buf
, shndx
= extshndx_buf
;
393 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
394 (*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
);
397 if (alloc_ext
!= NULL
)
399 if (alloc_extshndx
!= NULL
)
400 free (alloc_extshndx
);
405 /* Look up a symbol name. */
407 bfd_elf_local_sym_name (bfd
*abfd
, Elf_Internal_Sym
*isym
)
409 unsigned int iname
= isym
->st_name
;
410 unsigned int shindex
= elf_tdata (abfd
)->symtab_hdr
.sh_link
;
411 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
413 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
414 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
417 return bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
420 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
421 sections. The first element is the flags, the rest are section
424 typedef union elf_internal_group
{
425 Elf_Internal_Shdr
*shdr
;
427 } Elf_Internal_Group
;
429 /* Return the name of the group signature symbol. Why isn't the
430 signature just a string? */
433 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
435 Elf_Internal_Shdr
*hdr
;
436 unsigned char esym
[sizeof (Elf64_External_Sym
)];
437 Elf_External_Sym_Shndx eshndx
;
438 Elf_Internal_Sym isym
;
440 /* First we need to ensure the symbol table is available. */
441 if (! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
444 /* Go read the symbol. */
445 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
446 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
447 &isym
, esym
, &eshndx
) == NULL
)
450 return bfd_elf_local_sym_name (abfd
, &isym
);
453 /* Set next_in_group list pointer, and group name for NEWSECT. */
456 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
458 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
460 /* If num_group is zero, read in all SHT_GROUP sections. The count
461 is set to -1 if there are no SHT_GROUP sections. */
464 unsigned int i
, shnum
;
466 /* First count the number of groups. If we have a SHT_GROUP
467 section with just a flag word (ie. sh_size is 4), ignore it. */
468 shnum
= elf_numsections (abfd
);
470 for (i
= 0; i
< shnum
; i
++)
472 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
473 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
478 num_group
= (unsigned) -1;
479 elf_tdata (abfd
)->num_group
= num_group
;
483 /* We keep a list of elf section headers for group sections,
484 so we can find them quickly. */
485 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
486 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
487 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
491 for (i
= 0; i
< shnum
; i
++)
493 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
494 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
497 Elf_Internal_Group
*dest
;
499 /* Add to list of sections. */
500 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
503 /* Read the raw contents. */
504 BFD_ASSERT (sizeof (*dest
) >= 4);
505 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
506 shdr
->contents
= bfd_alloc (abfd
, amt
);
507 if (shdr
->contents
== NULL
508 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
509 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
513 /* Translate raw contents, a flag word followed by an
514 array of elf section indices all in target byte order,
515 to the flag word followed by an array of elf section
517 src
= shdr
->contents
+ shdr
->sh_size
;
518 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
525 idx
= H_GET_32 (abfd
, src
);
526 if (src
== shdr
->contents
)
529 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
530 shdr
->bfd_section
->flags
531 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
536 ((*_bfd_error_handler
)
537 (_("%s: invalid SHT_GROUP entry"),
538 bfd_archive_filename (abfd
)));
541 dest
->shdr
= elf_elfsections (abfd
)[idx
];
548 if (num_group
!= (unsigned) -1)
552 for (i
= 0; i
< num_group
; i
++)
554 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
555 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
556 unsigned int n_elt
= shdr
->sh_size
/ 4;
558 /* Look through this group's sections to see if current
559 section is a member. */
561 if ((++idx
)->shdr
== hdr
)
565 /* We are a member of this group. Go looking through
566 other members to see if any others are linked via
568 idx
= (Elf_Internal_Group
*) shdr
->contents
;
569 n_elt
= shdr
->sh_size
/ 4;
571 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
572 && elf_next_in_group (s
) != NULL
)
576 /* Snarf the group name from other member, and
577 insert current section in circular list. */
578 elf_group_name (newsect
) = elf_group_name (s
);
579 elf_next_in_group (newsect
) = elf_next_in_group (s
);
580 elf_next_in_group (s
) = newsect
;
586 gname
= group_signature (abfd
, shdr
);
589 elf_group_name (newsect
) = gname
;
591 /* Start a circular list with one element. */
592 elf_next_in_group (newsect
) = newsect
;
595 /* If the group section has been created, point to the
597 if (shdr
->bfd_section
!= NULL
)
598 elf_next_in_group (shdr
->bfd_section
) = newsect
;
606 if (elf_group_name (newsect
) == NULL
)
608 (*_bfd_error_handler
) (_("%s: no group info for section %s"),
609 bfd_archive_filename (abfd
), newsect
->name
);
615 bfd_elf_discard_group (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*group
)
617 asection
*first
= elf_next_in_group (group
);
622 s
->output_section
= bfd_abs_section_ptr
;
623 s
= elf_next_in_group (s
);
624 /* These lists are circular. */
631 /* Make a BFD section from an ELF section. We store a pointer to the
632 BFD section in the bfd_section field of the header. */
635 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
636 Elf_Internal_Shdr
*hdr
,
641 const struct elf_backend_data
*bed
;
643 if (hdr
->bfd_section
!= NULL
)
645 BFD_ASSERT (strcmp (name
,
646 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
650 newsect
= bfd_make_section_anyway (abfd
, name
);
654 /* Always use the real type/flags. */
655 elf_section_type (newsect
) = hdr
->sh_type
;
656 elf_section_flags (newsect
) = hdr
->sh_flags
;
658 newsect
->filepos
= hdr
->sh_offset
;
660 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
661 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
662 || ! bfd_set_section_alignment (abfd
, newsect
,
663 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
666 flags
= SEC_NO_FLAGS
;
667 if (hdr
->sh_type
!= SHT_NOBITS
)
668 flags
|= SEC_HAS_CONTENTS
;
669 if (hdr
->sh_type
== SHT_GROUP
)
670 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
671 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
674 if (hdr
->sh_type
!= SHT_NOBITS
)
677 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
678 flags
|= SEC_READONLY
;
679 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
681 else if ((flags
& SEC_LOAD
) != 0)
683 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
686 newsect
->entsize
= hdr
->sh_entsize
;
687 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
688 flags
|= SEC_STRINGS
;
690 if (hdr
->sh_flags
& SHF_GROUP
)
691 if (!setup_group (abfd
, hdr
, newsect
))
693 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
694 flags
|= SEC_THREAD_LOCAL
;
696 /* The debugging sections appear to be recognized only by name, not
699 static const char *debug_sec_names
[] =
708 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
709 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
713 flags
|= SEC_DEBUGGING
;
716 /* As a GNU extension, if the name begins with .gnu.linkonce, we
717 only link a single copy of the section. This is used to support
718 g++. g++ will emit each template expansion in its own section.
719 The symbols will be defined as weak, so that multiple definitions
720 are permitted. The GNU linker extension is to actually discard
721 all but one of the sections. */
722 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
723 && elf_next_in_group (newsect
) == NULL
)
724 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
726 bed
= get_elf_backend_data (abfd
);
727 if (bed
->elf_backend_section_flags
)
728 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
731 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
734 if ((flags
& SEC_ALLOC
) != 0)
736 Elf_Internal_Phdr
*phdr
;
739 /* Look through the phdrs to see if we need to adjust the lma.
740 If all the p_paddr fields are zero, we ignore them, since
741 some ELF linkers produce such output. */
742 phdr
= elf_tdata (abfd
)->phdr
;
743 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
745 if (phdr
->p_paddr
!= 0)
748 if (i
< elf_elfheader (abfd
)->e_phnum
)
750 phdr
= elf_tdata (abfd
)->phdr
;
751 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
753 /* This section is part of this segment if its file
754 offset plus size lies within the segment's memory
755 span and, if the section is loaded, the extent of the
756 loaded data lies within the extent of the segment.
758 Note - we used to check the p_paddr field as well, and
759 refuse to set the LMA if it was 0. This is wrong
760 though, as a perfectly valid initialised segment can
761 have a p_paddr of zero. Some architectures, eg ARM,
762 place special significance on the address 0 and
763 executables need to be able to have a segment which
764 covers this address. */
765 if (phdr
->p_type
== PT_LOAD
766 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
767 && (hdr
->sh_offset
+ hdr
->sh_size
768 <= phdr
->p_offset
+ phdr
->p_memsz
)
769 && ((flags
& SEC_LOAD
) == 0
770 || (hdr
->sh_offset
+ hdr
->sh_size
771 <= phdr
->p_offset
+ phdr
->p_filesz
)))
773 if ((flags
& SEC_LOAD
) == 0)
774 newsect
->lma
= (phdr
->p_paddr
775 + hdr
->sh_addr
- phdr
->p_vaddr
);
777 /* We used to use the same adjustment for SEC_LOAD
778 sections, but that doesn't work if the segment
779 is packed with code from multiple VMAs.
780 Instead we calculate the section LMA based on
781 the segment LMA. It is assumed that the
782 segment will contain sections with contiguous
783 LMAs, even if the VMAs are not. */
784 newsect
->lma
= (phdr
->p_paddr
785 + hdr
->sh_offset
- phdr
->p_offset
);
787 /* With contiguous segments, we can't tell from file
788 offsets whether a section with zero size should
789 be placed at the end of one segment or the
790 beginning of the next. Decide based on vaddr. */
791 if (hdr
->sh_addr
>= phdr
->p_vaddr
792 && (hdr
->sh_addr
+ hdr
->sh_size
793 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
800 hdr
->bfd_section
= newsect
;
801 elf_section_data (newsect
)->this_hdr
= *hdr
;
811 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
814 Helper functions for GDB to locate the string tables.
815 Since BFD hides string tables from callers, GDB needs to use an
816 internal hook to find them. Sun's .stabstr, in particular,
817 isn't even pointed to by the .stab section, so ordinary
818 mechanisms wouldn't work to find it, even if we had some.
821 struct elf_internal_shdr
*
822 bfd_elf_find_section (bfd
*abfd
, char *name
)
824 Elf_Internal_Shdr
**i_shdrp
;
829 i_shdrp
= elf_elfsections (abfd
);
832 shstrtab
= bfd_elf_get_str_section (abfd
,
833 elf_elfheader (abfd
)->e_shstrndx
);
834 if (shstrtab
!= NULL
)
836 max
= elf_numsections (abfd
);
837 for (i
= 1; i
< max
; i
++)
838 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
845 const char *const bfd_elf_section_type_names
[] = {
846 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
847 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
848 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
851 /* ELF relocs are against symbols. If we are producing relocatable
852 output, and the reloc is against an external symbol, and nothing
853 has given us any additional addend, the resulting reloc will also
854 be against the same symbol. In such a case, we don't want to
855 change anything about the way the reloc is handled, since it will
856 all be done at final link time. Rather than put special case code
857 into bfd_perform_relocation, all the reloc types use this howto
858 function. It just short circuits the reloc if producing
859 relocatable output against an external symbol. */
861 bfd_reloc_status_type
862 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
863 arelent
*reloc_entry
,
865 void *data ATTRIBUTE_UNUSED
,
866 asection
*input_section
,
868 char **error_message ATTRIBUTE_UNUSED
)
870 if (output_bfd
!= NULL
871 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
872 && (! reloc_entry
->howto
->partial_inplace
873 || reloc_entry
->addend
== 0))
875 reloc_entry
->address
+= input_section
->output_offset
;
879 return bfd_reloc_continue
;
882 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
885 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
888 BFD_ASSERT (sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
889 sec
->sec_info_type
= ELF_INFO_TYPE_NONE
;
892 /* Finish SHF_MERGE section merging. */
895 _bfd_elf_merge_sections (bfd
*abfd
, struct bfd_link_info
*info
)
897 if (!is_elf_hash_table (info
))
899 if (elf_hash_table (info
)->merge_info
)
900 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
,
901 merge_sections_remove_hook
);
906 _bfd_elf_link_just_syms (asection
*sec
, struct bfd_link_info
*info
)
908 sec
->output_section
= bfd_abs_section_ptr
;
909 sec
->output_offset
= sec
->vma
;
910 if (!is_elf_hash_table (info
))
913 sec
->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
916 /* Copy the program header and other data from one object module to
920 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
922 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
923 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
926 BFD_ASSERT (!elf_flags_init (obfd
)
927 || (elf_elfheader (obfd
)->e_flags
928 == elf_elfheader (ibfd
)->e_flags
));
930 elf_gp (obfd
) = elf_gp (ibfd
);
931 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
932 elf_flags_init (obfd
) = TRUE
;
936 /* Print out the program headers. */
939 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
942 Elf_Internal_Phdr
*p
;
944 bfd_byte
*dynbuf
= NULL
;
946 p
= elf_tdata (abfd
)->phdr
;
951 fprintf (f
, _("\nProgram Header:\n"));
952 c
= elf_elfheader (abfd
)->e_phnum
;
953 for (i
= 0; i
< c
; i
++, p
++)
960 case PT_NULL
: pt
= "NULL"; break;
961 case PT_LOAD
: pt
= "LOAD"; break;
962 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
963 case PT_INTERP
: pt
= "INTERP"; break;
964 case PT_NOTE
: pt
= "NOTE"; break;
965 case PT_SHLIB
: pt
= "SHLIB"; break;
966 case PT_PHDR
: pt
= "PHDR"; break;
967 case PT_TLS
: pt
= "TLS"; break;
968 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
969 case PT_GNU_STACK
: pt
= "STACK"; break;
970 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
972 fprintf (f
, "%8s off 0x", pt
);
973 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
974 fprintf (f
, " vaddr 0x");
975 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
976 fprintf (f
, " paddr 0x");
977 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
978 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
979 fprintf (f
, " filesz 0x");
980 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
981 fprintf (f
, " memsz 0x");
982 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
983 fprintf (f
, " flags %c%c%c",
984 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
985 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
986 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
987 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
988 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
993 s
= bfd_get_section_by_name (abfd
, ".dynamic");
997 unsigned long shlink
;
998 bfd_byte
*extdyn
, *extdynend
;
1000 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1002 fprintf (f
, _("\nDynamic Section:\n"));
1004 dynbuf
= bfd_malloc (s
->_raw_size
);
1007 if (! bfd_get_section_contents (abfd
, s
, dynbuf
, 0, s
->_raw_size
))
1010 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1013 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1015 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1016 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1019 extdynend
= extdyn
+ s
->_raw_size
;
1020 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1022 Elf_Internal_Dyn dyn
;
1025 bfd_boolean stringp
;
1027 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1029 if (dyn
.d_tag
== DT_NULL
)
1036 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1040 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1041 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1042 case DT_PLTGOT
: name
= "PLTGOT"; break;
1043 case DT_HASH
: name
= "HASH"; break;
1044 case DT_STRTAB
: name
= "STRTAB"; break;
1045 case DT_SYMTAB
: name
= "SYMTAB"; break;
1046 case DT_RELA
: name
= "RELA"; break;
1047 case DT_RELASZ
: name
= "RELASZ"; break;
1048 case DT_RELAENT
: name
= "RELAENT"; break;
1049 case DT_STRSZ
: name
= "STRSZ"; break;
1050 case DT_SYMENT
: name
= "SYMENT"; break;
1051 case DT_INIT
: name
= "INIT"; break;
1052 case DT_FINI
: name
= "FINI"; break;
1053 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1054 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1055 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1056 case DT_REL
: name
= "REL"; break;
1057 case DT_RELSZ
: name
= "RELSZ"; break;
1058 case DT_RELENT
: name
= "RELENT"; break;
1059 case DT_PLTREL
: name
= "PLTREL"; break;
1060 case DT_DEBUG
: name
= "DEBUG"; break;
1061 case DT_TEXTREL
: name
= "TEXTREL"; break;
1062 case DT_JMPREL
: name
= "JMPREL"; break;
1063 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1064 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1065 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1066 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1067 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1068 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1069 case DT_FLAGS
: name
= "FLAGS"; break;
1070 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1071 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1072 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1073 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1074 case DT_MOVEENT
: name
= "MOVEENT"; break;
1075 case DT_MOVESZ
: name
= "MOVESZ"; break;
1076 case DT_FEATURE
: name
= "FEATURE"; break;
1077 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1078 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1079 case DT_SYMINENT
: name
= "SYMINENT"; break;
1080 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1081 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1082 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1083 case DT_PLTPAD
: name
= "PLTPAD"; break;
1084 case DT_MOVETAB
: name
= "MOVETAB"; break;
1085 case DT_SYMINFO
: name
= "SYMINFO"; break;
1086 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1087 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1088 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1089 case DT_VERSYM
: name
= "VERSYM"; break;
1090 case DT_VERDEF
: name
= "VERDEF"; break;
1091 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1092 case DT_VERNEED
: name
= "VERNEED"; break;
1093 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1094 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1095 case DT_USED
: name
= "USED"; break;
1096 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1099 fprintf (f
, " %-11s ", name
);
1101 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1105 unsigned int tagv
= dyn
.d_un
.d_val
;
1107 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1110 fprintf (f
, "%s", string
);
1119 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1120 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1122 if (! _bfd_elf_slurp_version_tables (abfd
))
1126 if (elf_dynverdef (abfd
) != 0)
1128 Elf_Internal_Verdef
*t
;
1130 fprintf (f
, _("\nVersion definitions:\n"));
1131 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1133 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1134 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1135 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1137 Elf_Internal_Verdaux
*a
;
1140 for (a
= t
->vd_auxptr
->vda_nextptr
;
1143 fprintf (f
, "%s ", a
->vda_nodename
);
1149 if (elf_dynverref (abfd
) != 0)
1151 Elf_Internal_Verneed
*t
;
1153 fprintf (f
, _("\nVersion References:\n"));
1154 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1156 Elf_Internal_Vernaux
*a
;
1158 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1159 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1160 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1161 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1173 /* Display ELF-specific fields of a symbol. */
1176 bfd_elf_print_symbol (bfd
*abfd
,
1179 bfd_print_symbol_type how
)
1184 case bfd_print_symbol_name
:
1185 fprintf (file
, "%s", symbol
->name
);
1187 case bfd_print_symbol_more
:
1188 fprintf (file
, "elf ");
1189 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1190 fprintf (file
, " %lx", (long) symbol
->flags
);
1192 case bfd_print_symbol_all
:
1194 const char *section_name
;
1195 const char *name
= NULL
;
1196 const struct elf_backend_data
*bed
;
1197 unsigned char st_other
;
1200 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1202 bed
= get_elf_backend_data (abfd
);
1203 if (bed
->elf_backend_print_symbol_all
)
1204 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1208 name
= symbol
->name
;
1209 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1212 fprintf (file
, " %s\t", section_name
);
1213 /* Print the "other" value for a symbol. For common symbols,
1214 we've already printed the size; now print the alignment.
1215 For other symbols, we have no specified alignment, and
1216 we've printed the address; now print the size. */
1217 if (bfd_is_com_section (symbol
->section
))
1218 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1220 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1221 bfd_fprintf_vma (abfd
, file
, val
);
1223 /* If we have version information, print it. */
1224 if (elf_tdata (abfd
)->dynversym_section
!= 0
1225 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1226 || elf_tdata (abfd
)->dynverref_section
!= 0))
1228 unsigned int vernum
;
1229 const char *version_string
;
1231 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1234 version_string
= "";
1235 else if (vernum
== 1)
1236 version_string
= "Base";
1237 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1239 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1242 Elf_Internal_Verneed
*t
;
1244 version_string
= "";
1245 for (t
= elf_tdata (abfd
)->verref
;
1249 Elf_Internal_Vernaux
*a
;
1251 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1253 if (a
->vna_other
== vernum
)
1255 version_string
= a
->vna_nodename
;
1262 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1263 fprintf (file
, " %-11s", version_string
);
1268 fprintf (file
, " (%s)", version_string
);
1269 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1274 /* If the st_other field is not zero, print it. */
1275 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1280 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1281 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1282 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1284 /* Some other non-defined flags are also present, so print
1286 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1289 fprintf (file
, " %s", name
);
1295 /* Create an entry in an ELF linker hash table. */
1297 struct bfd_hash_entry
*
1298 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
1299 struct bfd_hash_table
*table
,
1302 /* Allocate the structure if it has not already been allocated by a
1306 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1311 /* Call the allocation method of the superclass. */
1312 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1315 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1316 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1318 /* Set local fields. */
1321 ret
->dynstr_index
= 0;
1322 ret
->elf_hash_value
= 0;
1323 ret
->weakdef
= NULL
;
1324 ret
->verinfo
.verdef
= NULL
;
1325 ret
->vtable_entries_size
= 0;
1326 ret
->vtable_entries_used
= NULL
;
1327 ret
->vtable_parent
= NULL
;
1328 ret
->got
= htab
->init_refcount
;
1329 ret
->plt
= htab
->init_refcount
;
1331 ret
->type
= STT_NOTYPE
;
1333 /* Assume that we have been called by a non-ELF symbol reader.
1334 This flag is then reset by the code which reads an ELF input
1335 file. This ensures that a symbol created by a non-ELF symbol
1336 reader will have the flag set correctly. */
1337 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1343 /* Copy data from an indirect symbol to its direct symbol, hiding the
1344 old indirect symbol. Also used for copying flags to a weakdef. */
1347 _bfd_elf_link_hash_copy_indirect (const struct elf_backend_data
*bed
,
1348 struct elf_link_hash_entry
*dir
,
1349 struct elf_link_hash_entry
*ind
)
1352 bfd_signed_vma lowest_valid
= bed
->can_refcount
;
1354 /* Copy down any references that we may have already seen to the
1355 symbol which just became indirect. */
1357 dir
->elf_link_hash_flags
|=
1358 (ind
->elf_link_hash_flags
1359 & (ELF_LINK_HASH_REF_DYNAMIC
1360 | ELF_LINK_HASH_REF_REGULAR
1361 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1362 | ELF_LINK_NON_GOT_REF
));
1364 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1367 /* Copy over the global and procedure linkage table refcount entries.
1368 These may have been already set up by a check_relocs routine. */
1369 tmp
= dir
->got
.refcount
;
1370 if (tmp
< lowest_valid
)
1372 dir
->got
.refcount
= ind
->got
.refcount
;
1373 ind
->got
.refcount
= tmp
;
1376 BFD_ASSERT (ind
->got
.refcount
< lowest_valid
);
1378 tmp
= dir
->plt
.refcount
;
1379 if (tmp
< lowest_valid
)
1381 dir
->plt
.refcount
= ind
->plt
.refcount
;
1382 ind
->plt
.refcount
= tmp
;
1385 BFD_ASSERT (ind
->plt
.refcount
< lowest_valid
);
1387 if (dir
->dynindx
== -1)
1389 dir
->dynindx
= ind
->dynindx
;
1390 dir
->dynstr_index
= ind
->dynstr_index
;
1392 ind
->dynstr_index
= 0;
1395 BFD_ASSERT (ind
->dynindx
== -1);
1399 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
1400 struct elf_link_hash_entry
*h
,
1401 bfd_boolean force_local
)
1403 h
->plt
= elf_hash_table (info
)->init_offset
;
1404 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1407 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1408 if (h
->dynindx
!= -1)
1411 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
1417 /* Initialize an ELF linker hash table. */
1420 _bfd_elf_link_hash_table_init
1421 (struct elf_link_hash_table
*table
,
1423 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
1424 struct bfd_hash_table
*,
1429 table
->dynamic_sections_created
= FALSE
;
1430 table
->dynobj
= NULL
;
1431 /* Make sure can_refcount is extended to the width and signedness of
1432 init_refcount before we subtract one from it. */
1433 table
->init_refcount
.refcount
= get_elf_backend_data (abfd
)->can_refcount
;
1434 table
->init_refcount
.refcount
-= 1;
1435 table
->init_offset
.offset
= -(bfd_vma
) 1;
1436 /* The first dynamic symbol is a dummy. */
1437 table
->dynsymcount
= 1;
1438 table
->dynstr
= NULL
;
1439 table
->bucketcount
= 0;
1440 table
->needed
= NULL
;
1442 table
->stab_info
= NULL
;
1443 table
->merge_info
= NULL
;
1444 memset (&table
->eh_info
, 0, sizeof (table
->eh_info
));
1445 table
->dynlocal
= NULL
;
1446 table
->runpath
= NULL
;
1447 table
->tls_segment
= NULL
;
1448 table
->loaded
= NULL
;
1450 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
1451 table
->root
.type
= bfd_link_elf_hash_table
;
1456 /* Create an ELF linker hash table. */
1458 struct bfd_link_hash_table
*
1459 _bfd_elf_link_hash_table_create (bfd
*abfd
)
1461 struct elf_link_hash_table
*ret
;
1462 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1464 ret
= bfd_malloc (amt
);
1468 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1477 /* This is a hook for the ELF emulation code in the generic linker to
1478 tell the backend linker what file name to use for the DT_NEEDED
1479 entry for a dynamic object. The generic linker passes name as an
1480 empty string to indicate that no DT_NEEDED entry should be made. */
1483 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
1485 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1486 && bfd_get_format (abfd
) == bfd_object
)
1487 elf_dt_name (abfd
) = name
;
1491 bfd_elf_set_dt_needed_soname (bfd
*abfd
, const char *name
)
1493 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1494 && bfd_get_format (abfd
) == bfd_object
)
1495 elf_dt_soname (abfd
) = name
;
1498 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1499 the linker ELF emulation code. */
1501 struct bfd_link_needed_list
*
1502 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
1503 struct bfd_link_info
*info
)
1505 if (! is_elf_hash_table (info
))
1507 return elf_hash_table (info
)->needed
;
1510 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1511 hook for the linker ELF emulation code. */
1513 struct bfd_link_needed_list
*
1514 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
1515 struct bfd_link_info
*info
)
1517 if (! is_elf_hash_table (info
))
1519 return elf_hash_table (info
)->runpath
;
1522 /* Get the name actually used for a dynamic object for a link. This
1523 is the SONAME entry if there is one. Otherwise, it is the string
1524 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1527 bfd_elf_get_dt_soname (bfd
*abfd
)
1529 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1530 && bfd_get_format (abfd
) == bfd_object
)
1531 return elf_dt_name (abfd
);
1535 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1536 the ELF linker emulation code. */
1539 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
1540 struct bfd_link_needed_list
**pneeded
)
1543 bfd_byte
*dynbuf
= NULL
;
1545 unsigned long shlink
;
1546 bfd_byte
*extdyn
, *extdynend
;
1548 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1552 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1553 || bfd_get_format (abfd
) != bfd_object
)
1556 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1557 if (s
== NULL
|| s
->_raw_size
== 0)
1560 dynbuf
= bfd_malloc (s
->_raw_size
);
1564 if (! bfd_get_section_contents (abfd
, s
, dynbuf
, 0, s
->_raw_size
))
1567 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1571 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1573 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1574 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1577 extdynend
= extdyn
+ s
->_raw_size
;
1578 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1580 Elf_Internal_Dyn dyn
;
1582 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1584 if (dyn
.d_tag
== DT_NULL
)
1587 if (dyn
.d_tag
== DT_NEEDED
)
1590 struct bfd_link_needed_list
*l
;
1591 unsigned int tagv
= dyn
.d_un
.d_val
;
1594 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1599 l
= bfd_alloc (abfd
, amt
);
1620 /* Allocate an ELF string table--force the first byte to be zero. */
1622 struct bfd_strtab_hash
*
1623 _bfd_elf_stringtab_init (void)
1625 struct bfd_strtab_hash
*ret
;
1627 ret
= _bfd_stringtab_init ();
1632 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1633 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1634 if (loc
== (bfd_size_type
) -1)
1636 _bfd_stringtab_free (ret
);
1643 /* ELF .o/exec file reading */
1645 /* Create a new bfd section from an ELF section header. */
1648 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1650 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1651 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1652 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1655 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1657 switch (hdr
->sh_type
)
1660 /* Inactive section. Throw it away. */
1663 case SHT_PROGBITS
: /* Normal section with contents. */
1664 case SHT_NOBITS
: /* .bss section. */
1665 case SHT_HASH
: /* .hash section. */
1666 case SHT_NOTE
: /* .note section. */
1667 case SHT_INIT_ARRAY
: /* .init_array section. */
1668 case SHT_FINI_ARRAY
: /* .fini_array section. */
1669 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1670 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1672 case SHT_DYNAMIC
: /* Dynamic linking information. */
1673 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1675 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1677 Elf_Internal_Shdr
*dynsymhdr
;
1679 /* The shared libraries distributed with hpux11 have a bogus
1680 sh_link field for the ".dynamic" section. Find the
1681 string table for the ".dynsym" section instead. */
1682 if (elf_dynsymtab (abfd
) != 0)
1684 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1685 hdr
->sh_link
= dynsymhdr
->sh_link
;
1689 unsigned int i
, num_sec
;
1691 num_sec
= elf_numsections (abfd
);
1692 for (i
= 1; i
< num_sec
; i
++)
1694 dynsymhdr
= elf_elfsections (abfd
)[i
];
1695 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1697 hdr
->sh_link
= dynsymhdr
->sh_link
;
1705 case SHT_SYMTAB
: /* A symbol table */
1706 if (elf_onesymtab (abfd
) == shindex
)
1709 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1710 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1711 elf_onesymtab (abfd
) = shindex
;
1712 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1713 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1714 abfd
->flags
|= HAS_SYMS
;
1716 /* Sometimes a shared object will map in the symbol table. If
1717 SHF_ALLOC is set, and this is a shared object, then we also
1718 treat this section as a BFD section. We can not base the
1719 decision purely on SHF_ALLOC, because that flag is sometimes
1720 set in a relocatable object file, which would confuse the
1722 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1723 && (abfd
->flags
& DYNAMIC
) != 0
1724 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1729 case SHT_DYNSYM
: /* A dynamic symbol table */
1730 if (elf_dynsymtab (abfd
) == shindex
)
1733 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1734 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1735 elf_dynsymtab (abfd
) = shindex
;
1736 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1737 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1738 abfd
->flags
|= HAS_SYMS
;
1740 /* Besides being a symbol table, we also treat this as a regular
1741 section, so that objcopy can handle it. */
1742 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1744 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1745 if (elf_symtab_shndx (abfd
) == shindex
)
1748 /* Get the associated symbol table. */
1749 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1750 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1753 elf_symtab_shndx (abfd
) = shindex
;
1754 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1755 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1758 case SHT_STRTAB
: /* A string table */
1759 if (hdr
->bfd_section
!= NULL
)
1761 if (ehdr
->e_shstrndx
== shindex
)
1763 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1764 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1768 unsigned int i
, num_sec
;
1770 num_sec
= elf_numsections (abfd
);
1771 for (i
= 1; i
< num_sec
; i
++)
1773 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1774 if (hdr2
->sh_link
== shindex
)
1776 if (! bfd_section_from_shdr (abfd
, i
))
1778 if (elf_onesymtab (abfd
) == i
)
1780 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1781 elf_elfsections (abfd
)[shindex
] =
1782 &elf_tdata (abfd
)->strtab_hdr
;
1785 if (elf_dynsymtab (abfd
) == i
)
1787 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1788 elf_elfsections (abfd
)[shindex
] = hdr
=
1789 &elf_tdata (abfd
)->dynstrtab_hdr
;
1790 /* We also treat this as a regular section, so
1791 that objcopy can handle it. */
1794 #if 0 /* Not handling other string tables specially right now. */
1795 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1796 /* We have a strtab for some random other section. */
1797 newsect
= (asection
*) hdr2
->bfd_section
;
1800 hdr
->bfd_section
= newsect
;
1801 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1803 elf_elfsections (abfd
)[shindex
] = hdr2
;
1809 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1813 /* *These* do a lot of work -- but build no sections! */
1815 asection
*target_sect
;
1816 Elf_Internal_Shdr
*hdr2
;
1817 unsigned int num_sec
= elf_numsections (abfd
);
1819 /* Check for a bogus link to avoid crashing. */
1820 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1821 || hdr
->sh_link
>= num_sec
)
1823 ((*_bfd_error_handler
)
1824 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1825 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1826 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1829 /* For some incomprehensible reason Oracle distributes
1830 libraries for Solaris in which some of the objects have
1831 bogus sh_link fields. It would be nice if we could just
1832 reject them, but, unfortunately, some people need to use
1833 them. We scan through the section headers; if we find only
1834 one suitable symbol table, we clobber the sh_link to point
1835 to it. I hope this doesn't break anything. */
1836 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1837 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1843 for (scan
= 1; scan
< num_sec
; scan
++)
1845 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1846 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1857 hdr
->sh_link
= found
;
1860 /* Get the symbol table. */
1861 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1862 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1865 /* If this reloc section does not use the main symbol table we
1866 don't treat it as a reloc section. BFD can't adequately
1867 represent such a section, so at least for now, we don't
1868 try. We just present it as a normal section. We also
1869 can't use it as a reloc section if it points to the null
1871 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1872 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1874 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1876 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1877 if (target_sect
== NULL
)
1880 if ((target_sect
->flags
& SEC_RELOC
) == 0
1881 || target_sect
->reloc_count
== 0)
1882 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1886 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1887 amt
= sizeof (*hdr2
);
1888 hdr2
= bfd_alloc (abfd
, amt
);
1889 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1892 elf_elfsections (abfd
)[shindex
] = hdr2
;
1893 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1894 target_sect
->flags
|= SEC_RELOC
;
1895 target_sect
->relocation
= NULL
;
1896 target_sect
->rel_filepos
= hdr
->sh_offset
;
1897 /* In the section to which the relocations apply, mark whether
1898 its relocations are of the REL or RELA variety. */
1899 if (hdr
->sh_size
!= 0)
1900 target_sect
->use_rela_p
= hdr
->sh_type
== SHT_RELA
;
1901 abfd
->flags
|= HAS_RELOC
;
1906 case SHT_GNU_verdef
:
1907 elf_dynverdef (abfd
) = shindex
;
1908 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1909 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1912 case SHT_GNU_versym
:
1913 elf_dynversym (abfd
) = shindex
;
1914 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1915 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1918 case SHT_GNU_verneed
:
1919 elf_dynverref (abfd
) = shindex
;
1920 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1921 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1928 /* We need a BFD section for objcopy and relocatable linking,
1929 and it's handy to have the signature available as the section
1931 name
= group_signature (abfd
, hdr
);
1934 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1936 if (hdr
->contents
!= NULL
)
1938 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1939 unsigned int n_elt
= hdr
->sh_size
/ 4;
1942 if (idx
->flags
& GRP_COMDAT
)
1943 hdr
->bfd_section
->flags
1944 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1946 while (--n_elt
!= 0)
1947 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
1948 && elf_next_in_group (s
) != NULL
)
1950 elf_next_in_group (hdr
->bfd_section
) = s
;
1957 /* Check for any processor-specific section types. */
1959 if (bed
->elf_backend_section_from_shdr
)
1960 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1968 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1969 Return SEC for sections that have no elf section, and NULL on error. */
1972 bfd_section_from_r_symndx (bfd
*abfd
,
1973 struct sym_sec_cache
*cache
,
1975 unsigned long r_symndx
)
1977 Elf_Internal_Shdr
*symtab_hdr
;
1978 unsigned char esym
[sizeof (Elf64_External_Sym
)];
1979 Elf_External_Sym_Shndx eshndx
;
1980 Elf_Internal_Sym isym
;
1981 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1983 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
1984 return cache
->sec
[ent
];
1986 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1987 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
1988 &isym
, esym
, &eshndx
) == NULL
)
1991 if (cache
->abfd
!= abfd
)
1993 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1996 cache
->indx
[ent
] = r_symndx
;
1997 cache
->sec
[ent
] = sec
;
1998 if ((isym
.st_shndx
!= SHN_UNDEF
&& isym
.st_shndx
< SHN_LORESERVE
)
1999 || isym
.st_shndx
> SHN_HIRESERVE
)
2002 s
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
2004 cache
->sec
[ent
] = s
;
2006 return cache
->sec
[ent
];
2009 /* Given an ELF section number, retrieve the corresponding BFD
2013 bfd_section_from_elf_index (bfd
*abfd
, unsigned int index
)
2015 if (index
>= elf_numsections (abfd
))
2017 return elf_elfsections (abfd
)[index
]->bfd_section
;
2020 static struct bfd_elf_special_section
const special_sections
[] =
2022 { ".bss", 4, -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2023 { ".comment", 8, 0, SHT_PROGBITS
, 0 },
2024 { ".data", 5, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2025 { ".data1", 6, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2026 { ".debug", 6, 0, SHT_PROGBITS
, 0 },
2027 { ".fini", 5, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2028 { ".init", 5, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2029 { ".line", 5, 0, SHT_PROGBITS
, 0 },
2030 { ".rodata", 7, -2, SHT_PROGBITS
, SHF_ALLOC
},
2031 { ".rodata1", 8, 0, SHT_PROGBITS
, SHF_ALLOC
},
2032 { ".tbss", 5, -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2033 { ".tdata", 6, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2034 { ".text", 5, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2035 { ".init_array", 11, 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2036 { ".fini_array", 11, 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2037 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2038 { ".debug_line", 11, 0, SHT_PROGBITS
, 0 },
2039 { ".debug_info", 11, 0, SHT_PROGBITS
, 0 },
2040 { ".debug_abbrev", 13, 0, SHT_PROGBITS
, 0 },
2041 { ".debug_aranges", 14, 0, SHT_PROGBITS
, 0 },
2042 { ".dynamic", 8, 0, SHT_DYNAMIC
, SHF_ALLOC
},
2043 { ".dynstr", 7, 0, SHT_STRTAB
, SHF_ALLOC
},
2044 { ".dynsym", 7, 0, SHT_DYNSYM
, SHF_ALLOC
},
2045 { ".got", 4, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2046 { ".hash", 5, 0, SHT_HASH
, SHF_ALLOC
},
2047 { ".interp", 7, 0, SHT_PROGBITS
, 0 },
2048 { ".plt", 4, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2049 { ".shstrtab", 9, 0, SHT_STRTAB
, 0 },
2050 { ".strtab", 7, 0, SHT_STRTAB
, 0 },
2051 { ".symtab", 7, 0, SHT_SYMTAB
, 0 },
2052 { ".gnu.version", 12, 0, SHT_GNU_versym
, 0 },
2053 { ".gnu.version_d", 14, 0, SHT_GNU_verdef
, 0 },
2054 { ".gnu.version_r", 14, 0, SHT_GNU_verneed
, 0 },
2055 { ".note", 5, -1, SHT_NOTE
, 0 },
2056 { ".rela", 5, -1, SHT_RELA
, 0 },
2057 { ".rel", 4, -1, SHT_REL
, 0 },
2058 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2059 { NULL
, 0, 0, 0, 0 }
2062 static const struct bfd_elf_special_section
*
2063 get_special_section (const char *name
,
2064 const struct bfd_elf_special_section
*special_sections
,
2068 int len
= strlen (name
);
2070 for (i
= 0; special_sections
[i
].prefix
!= NULL
; i
++)
2073 int prefix_len
= special_sections
[i
].prefix_length
;
2075 if (len
< prefix_len
)
2077 if (memcmp (name
, special_sections
[i
].prefix
, prefix_len
) != 0)
2080 suffix_len
= special_sections
[i
].suffix_length
;
2081 if (suffix_len
<= 0)
2083 if (name
[prefix_len
] != 0)
2085 if (suffix_len
== 0)
2087 if (name
[prefix_len
] != '.'
2088 && (suffix_len
== -2
2089 || (rela
&& special_sections
[i
].type
== SHT_REL
)))
2095 if (len
< prefix_len
+ suffix_len
)
2097 if (memcmp (name
+ len
- suffix_len
,
2098 special_sections
[i
].prefix
+ prefix_len
,
2102 return &special_sections
[i
];
2108 const struct bfd_elf_special_section
*
2109 _bfd_elf_get_sec_type_attr (bfd
*abfd
, const char *name
)
2111 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2112 const struct bfd_elf_special_section
*ssect
= NULL
;
2114 /* See if this is one of the special sections. */
2117 unsigned int rela
= bed
->default_use_rela_p
;
2119 if (bed
->special_sections
)
2120 ssect
= get_special_section (name
, bed
->special_sections
, rela
);
2123 ssect
= get_special_section (name
, special_sections
, rela
);
2130 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2132 struct bfd_elf_section_data
*sdata
;
2133 const struct bfd_elf_special_section
*ssect
;
2135 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2138 sdata
= bfd_zalloc (abfd
, sizeof (*sdata
));
2141 sec
->used_by_bfd
= sdata
;
2144 elf_section_type (sec
) = SHT_NULL
;
2145 ssect
= _bfd_elf_get_sec_type_attr (abfd
, sec
->name
);
2148 elf_section_type (sec
) = ssect
->type
;
2149 elf_section_flags (sec
) = ssect
->attr
;
2152 /* Indicate whether or not this section should use RELA relocations. */
2153 sec
->use_rela_p
= get_elf_backend_data (abfd
)->default_use_rela_p
;
2158 /* Create a new bfd section from an ELF program header.
2160 Since program segments have no names, we generate a synthetic name
2161 of the form segment<NUM>, where NUM is generally the index in the
2162 program header table. For segments that are split (see below) we
2163 generate the names segment<NUM>a and segment<NUM>b.
2165 Note that some program segments may have a file size that is different than
2166 (less than) the memory size. All this means is that at execution the
2167 system must allocate the amount of memory specified by the memory size,
2168 but only initialize it with the first "file size" bytes read from the
2169 file. This would occur for example, with program segments consisting
2170 of combined data+bss.
2172 To handle the above situation, this routine generates TWO bfd sections
2173 for the single program segment. The first has the length specified by
2174 the file size of the segment, and the second has the length specified
2175 by the difference between the two sizes. In effect, the segment is split
2176 into it's initialized and uninitialized parts.
2181 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2182 Elf_Internal_Phdr
*hdr
,
2184 const char *typename
)
2192 split
= ((hdr
->p_memsz
> 0)
2193 && (hdr
->p_filesz
> 0)
2194 && (hdr
->p_memsz
> hdr
->p_filesz
));
2195 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2196 len
= strlen (namebuf
) + 1;
2197 name
= bfd_alloc (abfd
, len
);
2200 memcpy (name
, namebuf
, len
);
2201 newsect
= bfd_make_section (abfd
, name
);
2202 if (newsect
== NULL
)
2204 newsect
->vma
= hdr
->p_vaddr
;
2205 newsect
->lma
= hdr
->p_paddr
;
2206 newsect
->_raw_size
= hdr
->p_filesz
;
2207 newsect
->filepos
= hdr
->p_offset
;
2208 newsect
->flags
|= SEC_HAS_CONTENTS
;
2209 if (hdr
->p_type
== PT_LOAD
)
2211 newsect
->flags
|= SEC_ALLOC
;
2212 newsect
->flags
|= SEC_LOAD
;
2213 if (hdr
->p_flags
& PF_X
)
2215 /* FIXME: all we known is that it has execute PERMISSION,
2217 newsect
->flags
|= SEC_CODE
;
2220 if (!(hdr
->p_flags
& PF_W
))
2222 newsect
->flags
|= SEC_READONLY
;
2227 sprintf (namebuf
, "%s%db", typename
, index
);
2228 len
= strlen (namebuf
) + 1;
2229 name
= bfd_alloc (abfd
, len
);
2232 memcpy (name
, namebuf
, len
);
2233 newsect
= bfd_make_section (abfd
, name
);
2234 if (newsect
== NULL
)
2236 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2237 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2238 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
2239 if (hdr
->p_type
== PT_LOAD
)
2241 newsect
->flags
|= SEC_ALLOC
;
2242 if (hdr
->p_flags
& PF_X
)
2243 newsect
->flags
|= SEC_CODE
;
2245 if (!(hdr
->p_flags
& PF_W
))
2246 newsect
->flags
|= SEC_READONLY
;
2253 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int index
)
2255 const struct elf_backend_data
*bed
;
2257 switch (hdr
->p_type
)
2260 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2263 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2266 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2269 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2272 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2274 if (! elfcore_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2279 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2282 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2284 case PT_GNU_EH_FRAME
:
2285 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
,
2289 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "stack");
2292 /* Check for any processor-specific program segment types.
2293 If no handler for them, default to making "segment" sections. */
2294 bed
= get_elf_backend_data (abfd
);
2295 if (bed
->elf_backend_section_from_phdr
)
2296 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2298 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2302 /* Initialize REL_HDR, the section-header for new section, containing
2303 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2304 relocations; otherwise, we use REL relocations. */
2307 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2308 Elf_Internal_Shdr
*rel_hdr
,
2310 bfd_boolean use_rela_p
)
2313 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2314 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2316 name
= bfd_alloc (abfd
, amt
);
2319 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2321 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2323 if (rel_hdr
->sh_name
== (unsigned int) -1)
2325 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2326 rel_hdr
->sh_entsize
= (use_rela_p
2327 ? bed
->s
->sizeof_rela
2328 : bed
->s
->sizeof_rel
);
2329 rel_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
2330 rel_hdr
->sh_flags
= 0;
2331 rel_hdr
->sh_addr
= 0;
2332 rel_hdr
->sh_size
= 0;
2333 rel_hdr
->sh_offset
= 0;
2338 /* Set up an ELF internal section header for a section. */
2341 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *failedptrarg
)
2343 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2344 bfd_boolean
*failedptr
= failedptrarg
;
2345 Elf_Internal_Shdr
*this_hdr
;
2349 /* We already failed; just get out of the bfd_map_over_sections
2354 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2356 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2357 asect
->name
, FALSE
);
2358 if (this_hdr
->sh_name
== (unsigned int) -1)
2364 this_hdr
->sh_flags
= 0;
2366 if ((asect
->flags
& SEC_ALLOC
) != 0
2367 || asect
->user_set_vma
)
2368 this_hdr
->sh_addr
= asect
->vma
;
2370 this_hdr
->sh_addr
= 0;
2372 this_hdr
->sh_offset
= 0;
2373 this_hdr
->sh_size
= asect
->_raw_size
;
2374 this_hdr
->sh_link
= 0;
2375 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2376 /* The sh_entsize and sh_info fields may have been set already by
2377 copy_private_section_data. */
2379 this_hdr
->bfd_section
= asect
;
2380 this_hdr
->contents
= NULL
;
2382 /* If the section type is unspecified, we set it based on
2384 if (this_hdr
->sh_type
== SHT_NULL
)
2386 if ((asect
->flags
& SEC_ALLOC
) != 0
2387 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2388 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2389 this_hdr
->sh_type
= SHT_NOBITS
;
2391 this_hdr
->sh_type
= SHT_PROGBITS
;
2394 switch (this_hdr
->sh_type
)
2400 case SHT_INIT_ARRAY
:
2401 case SHT_FINI_ARRAY
:
2402 case SHT_PREINIT_ARRAY
:
2409 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2413 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2417 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2421 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2422 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2426 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2427 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2430 case SHT_GNU_versym
:
2431 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2434 case SHT_GNU_verdef
:
2435 this_hdr
->sh_entsize
= 0;
2436 /* objcopy or strip will copy over sh_info, but may not set
2437 cverdefs. The linker will set cverdefs, but sh_info will be
2439 if (this_hdr
->sh_info
== 0)
2440 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2442 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2443 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2446 case SHT_GNU_verneed
:
2447 this_hdr
->sh_entsize
= 0;
2448 /* objcopy or strip will copy over sh_info, but may not set
2449 cverrefs. The linker will set cverrefs, but sh_info will be
2451 if (this_hdr
->sh_info
== 0)
2452 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2454 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2455 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2459 this_hdr
->sh_entsize
= 4;
2463 if ((asect
->flags
& SEC_ALLOC
) != 0)
2464 this_hdr
->sh_flags
|= SHF_ALLOC
;
2465 if ((asect
->flags
& SEC_READONLY
) == 0)
2466 this_hdr
->sh_flags
|= SHF_WRITE
;
2467 if ((asect
->flags
& SEC_CODE
) != 0)
2468 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2469 if ((asect
->flags
& SEC_MERGE
) != 0)
2471 this_hdr
->sh_flags
|= SHF_MERGE
;
2472 this_hdr
->sh_entsize
= asect
->entsize
;
2473 if ((asect
->flags
& SEC_STRINGS
) != 0)
2474 this_hdr
->sh_flags
|= SHF_STRINGS
;
2476 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2477 this_hdr
->sh_flags
|= SHF_GROUP
;
2478 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2480 this_hdr
->sh_flags
|= SHF_TLS
;
2481 if (asect
->_raw_size
== 0 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2483 struct bfd_link_order
*o
;
2485 this_hdr
->sh_size
= 0;
2486 for (o
= asect
->link_order_head
; o
!= NULL
; o
= o
->next
)
2487 if (this_hdr
->sh_size
< o
->offset
+ o
->size
)
2488 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2489 if (this_hdr
->sh_size
)
2490 this_hdr
->sh_type
= SHT_NOBITS
;
2494 /* Check for processor-specific section types. */
2495 if (bed
->elf_backend_fake_sections
2496 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2499 /* If the section has relocs, set up a section header for the
2500 SHT_REL[A] section. If two relocation sections are required for
2501 this section, it is up to the processor-specific back-end to
2502 create the other. */
2503 if ((asect
->flags
& SEC_RELOC
) != 0
2504 && !_bfd_elf_init_reloc_shdr (abfd
,
2505 &elf_section_data (asect
)->rel_hdr
,
2511 /* Fill in the contents of a SHT_GROUP section. */
2514 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2516 bfd_boolean
*failedptr
= failedptrarg
;
2517 unsigned long symindx
;
2518 asection
*elt
, *first
;
2520 struct bfd_link_order
*l
;
2523 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2528 if (elf_group_id (sec
) != NULL
)
2529 symindx
= elf_group_id (sec
)->udata
.i
;
2533 /* If called from the assembler, swap_out_syms will have set up
2534 elf_section_syms; If called for "ld -r", use target_index. */
2535 if (elf_section_syms (abfd
) != NULL
)
2536 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2538 symindx
= sec
->target_index
;
2540 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2542 /* The contents won't be allocated for "ld -r" or objcopy. */
2544 if (sec
->contents
== NULL
)
2547 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2549 /* Arrange for the section to be written out. */
2550 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2551 if (sec
->contents
== NULL
)
2558 loc
= sec
->contents
+ sec
->_raw_size
;
2560 /* Get the pointer to the first section in the group that gas
2561 squirreled away here. objcopy arranges for this to be set to the
2562 start of the input section group. */
2563 first
= elt
= elf_next_in_group (sec
);
2565 /* First element is a flag word. Rest of section is elf section
2566 indices for all the sections of the group. Write them backwards
2567 just to keep the group in the same order as given in .section
2568 directives, not that it matters. */
2577 s
= s
->output_section
;
2580 idx
= elf_section_data (s
)->this_idx
;
2581 H_PUT_32 (abfd
, idx
, loc
);
2582 elt
= elf_next_in_group (elt
);
2587 /* If this is a relocatable link, then the above did nothing because
2588 SEC is the output section. Look through the input sections
2590 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2591 if (l
->type
== bfd_indirect_link_order
2592 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2597 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2598 elt
= elf_next_in_group (elt
);
2599 /* During a relocatable link, the lists are circular. */
2601 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2603 /* With ld -r, merging SHT_GROUP sections results in wasted space
2604 due to allowing for the flag word on each input. We may well
2605 duplicate entries too. */
2606 while ((loc
-= 4) > sec
->contents
)
2607 H_PUT_32 (abfd
, 0, loc
);
2609 if (loc
!= sec
->contents
)
2612 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2615 /* Assign all ELF section numbers. The dummy first section is handled here
2616 too. The link/info pointers for the standard section types are filled
2617 in here too, while we're at it. */
2620 assign_section_numbers (bfd
*abfd
)
2622 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2624 unsigned int section_number
, secn
;
2625 Elf_Internal_Shdr
**i_shdrp
;
2630 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2632 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2634 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2636 if (section_number
== SHN_LORESERVE
)
2637 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2638 d
->this_idx
= section_number
++;
2639 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2640 if ((sec
->flags
& SEC_RELOC
) == 0)
2644 if (section_number
== SHN_LORESERVE
)
2645 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2646 d
->rel_idx
= section_number
++;
2647 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2652 if (section_number
== SHN_LORESERVE
)
2653 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2654 d
->rel_idx2
= section_number
++;
2655 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2661 if (section_number
== SHN_LORESERVE
)
2662 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2663 t
->shstrtab_section
= section_number
++;
2664 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2665 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2667 if (bfd_get_symcount (abfd
) > 0)
2669 if (section_number
== SHN_LORESERVE
)
2670 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2671 t
->symtab_section
= section_number
++;
2672 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2673 if (section_number
> SHN_LORESERVE
- 2)
2675 if (section_number
== SHN_LORESERVE
)
2676 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2677 t
->symtab_shndx_section
= section_number
++;
2678 t
->symtab_shndx_hdr
.sh_name
2679 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2680 ".symtab_shndx", FALSE
);
2681 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2684 if (section_number
== SHN_LORESERVE
)
2685 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2686 t
->strtab_section
= section_number
++;
2687 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2690 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2691 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2693 elf_numsections (abfd
) = section_number
;
2694 elf_elfheader (abfd
)->e_shnum
= section_number
;
2695 if (section_number
> SHN_LORESERVE
)
2696 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2698 /* Set up the list of section header pointers, in agreement with the
2700 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2701 i_shdrp
= bfd_zalloc (abfd
, amt
);
2702 if (i_shdrp
== NULL
)
2705 amt
= sizeof (Elf_Internal_Shdr
);
2706 i_shdrp
[0] = bfd_zalloc (abfd
, amt
);
2707 if (i_shdrp
[0] == NULL
)
2709 bfd_release (abfd
, i_shdrp
);
2713 elf_elfsections (abfd
) = i_shdrp
;
2715 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2716 if (bfd_get_symcount (abfd
) > 0)
2718 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2719 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2721 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2722 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2724 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2725 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2727 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2729 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2733 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2734 if (d
->rel_idx
!= 0)
2735 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2736 if (d
->rel_idx2
!= 0)
2737 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2739 /* Fill in the sh_link and sh_info fields while we're at it. */
2741 /* sh_link of a reloc section is the section index of the symbol
2742 table. sh_info is the section index of the section to which
2743 the relocation entries apply. */
2744 if (d
->rel_idx
!= 0)
2746 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2747 d
->rel_hdr
.sh_info
= d
->this_idx
;
2749 if (d
->rel_idx2
!= 0)
2751 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2752 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2755 switch (d
->this_hdr
.sh_type
)
2759 /* A reloc section which we are treating as a normal BFD
2760 section. sh_link is the section index of the symbol
2761 table. sh_info is the section index of the section to
2762 which the relocation entries apply. We assume that an
2763 allocated reloc section uses the dynamic symbol table.
2764 FIXME: How can we be sure? */
2765 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2767 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2769 /* We look up the section the relocs apply to by name. */
2771 if (d
->this_hdr
.sh_type
== SHT_REL
)
2775 s
= bfd_get_section_by_name (abfd
, name
);
2777 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2781 /* We assume that a section named .stab*str is a stabs
2782 string section. We look for a section with the same name
2783 but without the trailing ``str'', and set its sh_link
2784 field to point to this section. */
2785 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2786 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2791 len
= strlen (sec
->name
);
2792 alc
= bfd_malloc (len
- 2);
2795 memcpy (alc
, sec
->name
, len
- 3);
2796 alc
[len
- 3] = '\0';
2797 s
= bfd_get_section_by_name (abfd
, alc
);
2801 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2803 /* This is a .stab section. */
2804 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
2805 elf_section_data (s
)->this_hdr
.sh_entsize
2806 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2813 case SHT_GNU_verneed
:
2814 case SHT_GNU_verdef
:
2815 /* sh_link is the section header index of the string table
2816 used for the dynamic entries, or the symbol table, or the
2818 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2820 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2824 case SHT_GNU_versym
:
2825 /* sh_link is the section header index of the symbol table
2826 this hash table or version table is for. */
2827 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2829 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2833 d
->this_hdr
.sh_link
= t
->symtab_section
;
2837 for (secn
= 1; secn
< section_number
; ++secn
)
2838 if (i_shdrp
[secn
] == NULL
)
2839 i_shdrp
[secn
] = i_shdrp
[0];
2841 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2842 i_shdrp
[secn
]->sh_name
);
2846 /* Map symbol from it's internal number to the external number, moving
2847 all local symbols to be at the head of the list. */
2850 sym_is_global (bfd
*abfd
, asymbol
*sym
)
2852 /* If the backend has a special mapping, use it. */
2853 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2854 if (bed
->elf_backend_sym_is_global
)
2855 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
2857 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2858 || bfd_is_und_section (bfd_get_section (sym
))
2859 || bfd_is_com_section (bfd_get_section (sym
)));
2863 elf_map_symbols (bfd
*abfd
)
2865 unsigned int symcount
= bfd_get_symcount (abfd
);
2866 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2867 asymbol
**sect_syms
;
2868 unsigned int num_locals
= 0;
2869 unsigned int num_globals
= 0;
2870 unsigned int num_locals2
= 0;
2871 unsigned int num_globals2
= 0;
2879 fprintf (stderr
, "elf_map_symbols\n");
2883 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2885 if (max_index
< asect
->index
)
2886 max_index
= asect
->index
;
2890 amt
= max_index
* sizeof (asymbol
*);
2891 sect_syms
= bfd_zalloc (abfd
, amt
);
2892 if (sect_syms
== NULL
)
2894 elf_section_syms (abfd
) = sect_syms
;
2895 elf_num_section_syms (abfd
) = max_index
;
2897 /* Init sect_syms entries for any section symbols we have already
2898 decided to output. */
2899 for (idx
= 0; idx
< symcount
; idx
++)
2901 asymbol
*sym
= syms
[idx
];
2903 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2910 if (sec
->owner
!= NULL
)
2912 if (sec
->owner
!= abfd
)
2914 if (sec
->output_offset
!= 0)
2917 sec
= sec
->output_section
;
2919 /* Empty sections in the input files may have had a
2920 section symbol created for them. (See the comment
2921 near the end of _bfd_generic_link_output_symbols in
2922 linker.c). If the linker script discards such
2923 sections then we will reach this point. Since we know
2924 that we cannot avoid this case, we detect it and skip
2925 the abort and the assignment to the sect_syms array.
2926 To reproduce this particular case try running the
2927 linker testsuite test ld-scripts/weak.exp for an ELF
2928 port that uses the generic linker. */
2929 if (sec
->owner
== NULL
)
2932 BFD_ASSERT (sec
->owner
== abfd
);
2934 sect_syms
[sec
->index
] = syms
[idx
];
2939 /* Classify all of the symbols. */
2940 for (idx
= 0; idx
< symcount
; idx
++)
2942 if (!sym_is_global (abfd
, syms
[idx
]))
2948 /* We will be adding a section symbol for each BFD section. Most normal
2949 sections will already have a section symbol in outsymbols, but
2950 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2951 at least in that case. */
2952 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2954 if (sect_syms
[asect
->index
] == NULL
)
2956 if (!sym_is_global (abfd
, asect
->symbol
))
2963 /* Now sort the symbols so the local symbols are first. */
2964 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2965 new_syms
= bfd_alloc (abfd
, amt
);
2967 if (new_syms
== NULL
)
2970 for (idx
= 0; idx
< symcount
; idx
++)
2972 asymbol
*sym
= syms
[idx
];
2975 if (!sym_is_global (abfd
, sym
))
2978 i
= num_locals
+ num_globals2
++;
2980 sym
->udata
.i
= i
+ 1;
2982 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2984 if (sect_syms
[asect
->index
] == NULL
)
2986 asymbol
*sym
= asect
->symbol
;
2989 sect_syms
[asect
->index
] = sym
;
2990 if (!sym_is_global (abfd
, sym
))
2993 i
= num_locals
+ num_globals2
++;
2995 sym
->udata
.i
= i
+ 1;
2999 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3001 elf_num_locals (abfd
) = num_locals
;
3002 elf_num_globals (abfd
) = num_globals
;
3006 /* Align to the maximum file alignment that could be required for any
3007 ELF data structure. */
3009 static inline file_ptr
3010 align_file_position (file_ptr off
, int align
)
3012 return (off
+ align
- 1) & ~(align
- 1);
3015 /* Assign a file position to a section, optionally aligning to the
3016 required section alignment. */
3019 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3027 al
= i_shdrp
->sh_addralign
;
3029 offset
= BFD_ALIGN (offset
, al
);
3031 i_shdrp
->sh_offset
= offset
;
3032 if (i_shdrp
->bfd_section
!= NULL
)
3033 i_shdrp
->bfd_section
->filepos
= offset
;
3034 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3035 offset
+= i_shdrp
->sh_size
;
3039 /* Compute the file positions we are going to put the sections at, and
3040 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3041 is not NULL, this is being called by the ELF backend linker. */
3044 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3045 struct bfd_link_info
*link_info
)
3047 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3049 struct bfd_strtab_hash
*strtab
;
3050 Elf_Internal_Shdr
*shstrtab_hdr
;
3052 if (abfd
->output_has_begun
)
3055 /* Do any elf backend specific processing first. */
3056 if (bed
->elf_backend_begin_write_processing
)
3057 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3059 if (! prep_headers (abfd
))
3062 /* Post process the headers if necessary. */
3063 if (bed
->elf_backend_post_process_headers
)
3064 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3067 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3071 if (!assign_section_numbers (abfd
))
3074 /* The backend linker builds symbol table information itself. */
3075 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3077 /* Non-zero if doing a relocatable link. */
3078 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3080 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3084 if (link_info
== NULL
)
3086 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3091 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3092 /* sh_name was set in prep_headers. */
3093 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3094 shstrtab_hdr
->sh_flags
= 0;
3095 shstrtab_hdr
->sh_addr
= 0;
3096 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3097 shstrtab_hdr
->sh_entsize
= 0;
3098 shstrtab_hdr
->sh_link
= 0;
3099 shstrtab_hdr
->sh_info
= 0;
3100 /* sh_offset is set in assign_file_positions_except_relocs. */
3101 shstrtab_hdr
->sh_addralign
= 1;
3103 if (!assign_file_positions_except_relocs (abfd
))
3106 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3109 Elf_Internal_Shdr
*hdr
;
3111 off
= elf_tdata (abfd
)->next_file_pos
;
3113 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3114 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3116 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3117 if (hdr
->sh_size
!= 0)
3118 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3120 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3121 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3123 elf_tdata (abfd
)->next_file_pos
= off
;
3125 /* Now that we know where the .strtab section goes, write it
3127 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3128 || ! _bfd_stringtab_emit (abfd
, strtab
))
3130 _bfd_stringtab_free (strtab
);
3133 abfd
->output_has_begun
= TRUE
;
3138 /* Create a mapping from a set of sections to a program segment. */
3140 static struct elf_segment_map
*
3141 make_mapping (bfd
*abfd
,
3142 asection
**sections
,
3147 struct elf_segment_map
*m
;
3152 amt
= sizeof (struct elf_segment_map
);
3153 amt
+= (to
- from
- 1) * sizeof (asection
*);
3154 m
= bfd_zalloc (abfd
, amt
);
3158 m
->p_type
= PT_LOAD
;
3159 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3160 m
->sections
[i
- from
] = *hdrpp
;
3161 m
->count
= to
- from
;
3163 if (from
== 0 && phdr
)
3165 /* Include the headers in the first PT_LOAD segment. */
3166 m
->includes_filehdr
= 1;
3167 m
->includes_phdrs
= 1;
3173 /* Set up a mapping from BFD sections to program segments. */
3176 map_sections_to_segments (bfd
*abfd
)
3178 asection
**sections
= NULL
;
3182 struct elf_segment_map
*mfirst
;
3183 struct elf_segment_map
**pm
;
3184 struct elf_segment_map
*m
;
3186 unsigned int phdr_index
;
3187 bfd_vma maxpagesize
;
3189 bfd_boolean phdr_in_segment
= TRUE
;
3190 bfd_boolean writable
;
3192 asection
*first_tls
= NULL
;
3193 asection
*dynsec
, *eh_frame_hdr
;
3196 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3199 if (bfd_count_sections (abfd
) == 0)
3202 /* Select the allocated sections, and sort them. */
3204 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
3205 sections
= bfd_malloc (amt
);
3206 if (sections
== NULL
)
3210 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3212 if ((s
->flags
& SEC_ALLOC
) != 0)
3218 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3221 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3223 /* Build the mapping. */
3228 /* If we have a .interp section, then create a PT_PHDR segment for
3229 the program headers and a PT_INTERP segment for the .interp
3231 s
= bfd_get_section_by_name (abfd
, ".interp");
3232 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3234 amt
= sizeof (struct elf_segment_map
);
3235 m
= bfd_zalloc (abfd
, amt
);
3239 m
->p_type
= PT_PHDR
;
3240 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3241 m
->p_flags
= PF_R
| PF_X
;
3242 m
->p_flags_valid
= 1;
3243 m
->includes_phdrs
= 1;
3248 amt
= sizeof (struct elf_segment_map
);
3249 m
= bfd_zalloc (abfd
, amt
);
3253 m
->p_type
= PT_INTERP
;
3261 /* Look through the sections. We put sections in the same program
3262 segment when the start of the second section can be placed within
3263 a few bytes of the end of the first section. */
3266 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
3268 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3270 && (dynsec
->flags
& SEC_LOAD
) == 0)
3273 /* Deal with -Ttext or something similar such that the first section
3274 is not adjacent to the program headers. This is an
3275 approximation, since at this point we don't know exactly how many
3276 program headers we will need. */
3279 bfd_size_type phdr_size
;
3281 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3283 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3284 if ((abfd
->flags
& D_PAGED
) == 0
3285 || sections
[0]->lma
< phdr_size
3286 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3287 phdr_in_segment
= FALSE
;
3290 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3293 bfd_boolean new_segment
;
3297 /* See if this section and the last one will fit in the same
3300 if (last_hdr
== NULL
)
3302 /* If we don't have a segment yet, then we don't need a new
3303 one (we build the last one after this loop). */
3304 new_segment
= FALSE
;
3306 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3308 /* If this section has a different relation between the
3309 virtual address and the load address, then we need a new
3313 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3314 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3316 /* If putting this section in this segment would force us to
3317 skip a page in the segment, then we need a new segment. */
3320 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
3321 && (hdr
->flags
& SEC_LOAD
) != 0)
3323 /* We don't want to put a loadable section after a
3324 nonloadable section in the same segment. */
3327 else if ((abfd
->flags
& D_PAGED
) == 0)
3329 /* If the file is not demand paged, which means that we
3330 don't require the sections to be correctly aligned in the
3331 file, then there is no other reason for a new segment. */
3332 new_segment
= FALSE
;
3335 && (hdr
->flags
& SEC_READONLY
) == 0
3336 && (((last_hdr
->lma
+ last_hdr
->_raw_size
- 1)
3337 & ~(maxpagesize
- 1))
3338 != (hdr
->lma
& ~(maxpagesize
- 1))))
3340 /* We don't want to put a writable section in a read only
3341 segment, unless they are on the same page in memory
3342 anyhow. We already know that the last section does not
3343 bring us past the current section on the page, so the
3344 only case in which the new section is not on the same
3345 page as the previous section is when the previous section
3346 ends precisely on a page boundary. */
3351 /* Otherwise, we can use the same segment. */
3352 new_segment
= FALSE
;
3357 if ((hdr
->flags
& SEC_READONLY
) == 0)
3363 /* We need a new program segment. We must create a new program
3364 header holding all the sections from phdr_index until hdr. */
3366 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3373 if ((hdr
->flags
& SEC_READONLY
) == 0)
3380 phdr_in_segment
= FALSE
;
3383 /* Create a final PT_LOAD program segment. */
3384 if (last_hdr
!= NULL
)
3386 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3394 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3397 amt
= sizeof (struct elf_segment_map
);
3398 m
= bfd_zalloc (abfd
, amt
);
3402 m
->p_type
= PT_DYNAMIC
;
3404 m
->sections
[0] = dynsec
;
3410 /* For each loadable .note section, add a PT_NOTE segment. We don't
3411 use bfd_get_section_by_name, because if we link together
3412 nonloadable .note sections and loadable .note sections, we will
3413 generate two .note sections in the output file. FIXME: Using
3414 names for section types is bogus anyhow. */
3415 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3417 if ((s
->flags
& SEC_LOAD
) != 0
3418 && strncmp (s
->name
, ".note", 5) == 0)
3420 amt
= sizeof (struct elf_segment_map
);
3421 m
= bfd_zalloc (abfd
, amt
);
3425 m
->p_type
= PT_NOTE
;
3432 if (s
->flags
& SEC_THREAD_LOCAL
)
3440 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3445 amt
= sizeof (struct elf_segment_map
);
3446 amt
+= (tls_count
- 1) * sizeof (asection
*);
3447 m
= bfd_zalloc (abfd
, amt
);
3452 m
->count
= tls_count
;
3453 /* Mandated PF_R. */
3455 m
->p_flags_valid
= 1;
3456 for (i
= 0; i
< tls_count
; ++i
)
3458 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3459 m
->sections
[i
] = first_tls
;
3460 first_tls
= first_tls
->next
;
3467 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3469 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
3470 if (eh_frame_hdr
!= NULL
3471 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
3473 amt
= sizeof (struct elf_segment_map
);
3474 m
= bfd_zalloc (abfd
, amt
);
3478 m
->p_type
= PT_GNU_EH_FRAME
;
3480 m
->sections
[0] = eh_frame_hdr
->output_section
;
3486 if (elf_tdata (abfd
)->stack_flags
)
3488 amt
= sizeof (struct elf_segment_map
);
3489 m
= bfd_zalloc (abfd
, amt
);
3493 m
->p_type
= PT_GNU_STACK
;
3494 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
3495 m
->p_flags_valid
= 1;
3504 elf_tdata (abfd
)->segment_map
= mfirst
;
3508 if (sections
!= NULL
)
3513 /* Sort sections by address. */
3516 elf_sort_sections (const void *arg1
, const void *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 (bfd
*abfd
)
3578 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3580 struct elf_segment_map
*m
;
3582 Elf_Internal_Phdr
*phdrs
;
3584 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3585 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3586 Elf_Internal_Phdr
*p
;
3589 if (elf_tdata (abfd
)->segment_map
== NULL
)
3591 if (! map_sections_to_segments (abfd
))
3596 /* The placement algorithm assumes that non allocated sections are
3597 not in PT_LOAD segments. We ensure this here by removing such
3598 sections from the segment map. */
3599 for (m
= elf_tdata (abfd
)->segment_map
;
3603 unsigned int new_count
;
3606 if (m
->p_type
!= PT_LOAD
)
3610 for (i
= 0; i
< m
->count
; i
++)
3612 if ((m
->sections
[i
]->flags
& SEC_ALLOC
) != 0)
3615 m
->sections
[new_count
] = m
->sections
[i
];
3621 if (new_count
!= m
->count
)
3622 m
->count
= new_count
;
3626 if (bed
->elf_backend_modify_segment_map
)
3628 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
3633 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3636 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3637 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3638 elf_elfheader (abfd
)->e_phnum
= count
;
3643 /* If we already counted the number of program segments, make sure
3644 that we allocated enough space. This happens when SIZEOF_HEADERS
3645 is used in a linker script. */
3646 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3647 if (alloc
!= 0 && count
> alloc
)
3649 ((*_bfd_error_handler
)
3650 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3651 bfd_get_filename (abfd
), alloc
, count
));
3652 bfd_set_error (bfd_error_bad_value
);
3659 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3660 phdrs
= bfd_alloc (abfd
, amt
);
3664 off
= bed
->s
->sizeof_ehdr
;
3665 off
+= alloc
* bed
->s
->sizeof_phdr
;
3672 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3679 /* If elf_segment_map is not from map_sections_to_segments, the
3680 sections may not be correctly ordered. NOTE: sorting should
3681 not be done to the PT_NOTE section of a corefile, which may
3682 contain several pseudo-sections artificially created by bfd.
3683 Sorting these pseudo-sections breaks things badly. */
3685 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3686 && m
->p_type
== PT_NOTE
))
3687 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3690 p
->p_type
= m
->p_type
;
3691 p
->p_flags
= m
->p_flags
;
3693 if (p
->p_type
== PT_LOAD
3695 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3697 if ((abfd
->flags
& D_PAGED
) != 0)
3698 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
3701 bfd_size_type align
;
3704 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3706 bfd_size_type secalign
;
3708 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3709 if (secalign
> align
)
3713 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
3720 p
->p_vaddr
= m
->sections
[0]->vma
;
3722 if (m
->p_paddr_valid
)
3723 p
->p_paddr
= m
->p_paddr
;
3724 else if (m
->count
== 0)
3727 p
->p_paddr
= m
->sections
[0]->lma
;
3729 if (p
->p_type
== PT_LOAD
3730 && (abfd
->flags
& D_PAGED
) != 0)
3731 p
->p_align
= bed
->maxpagesize
;
3732 else if (m
->count
== 0)
3733 p
->p_align
= 1 << bed
->s
->log_file_align
;
3741 if (m
->includes_filehdr
)
3743 if (! m
->p_flags_valid
)
3746 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3747 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3750 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3752 if (p
->p_vaddr
< (bfd_vma
) off
)
3754 (*_bfd_error_handler
)
3755 (_("%s: Not enough room for program headers, try linking with -N"),
3756 bfd_get_filename (abfd
));
3757 bfd_set_error (bfd_error_bad_value
);
3762 if (! m
->p_paddr_valid
)
3765 if (p
->p_type
== PT_LOAD
)
3767 filehdr_vaddr
= p
->p_vaddr
;
3768 filehdr_paddr
= p
->p_paddr
;
3772 if (m
->includes_phdrs
)
3774 if (! m
->p_flags_valid
)
3777 if (m
->includes_filehdr
)
3779 if (p
->p_type
== PT_LOAD
)
3781 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3782 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3787 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3791 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3792 p
->p_vaddr
-= off
- p
->p_offset
;
3793 if (! m
->p_paddr_valid
)
3794 p
->p_paddr
-= off
- p
->p_offset
;
3797 if (p
->p_type
== PT_LOAD
)
3799 phdrs_vaddr
= p
->p_vaddr
;
3800 phdrs_paddr
= p
->p_paddr
;
3803 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3806 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3807 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3810 if (p
->p_type
== PT_LOAD
3811 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3813 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3819 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3820 p
->p_filesz
+= adjust
;
3821 p
->p_memsz
+= adjust
;
3827 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3831 bfd_size_type align
;
3835 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3837 /* The section may have artificial alignment forced by a
3838 link script. Notice this case by the gap between the
3839 cumulative phdr lma and the section's lma. */
3840 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3842 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3844 p
->p_memsz
+= adjust
;
3845 if (p
->p_type
== PT_LOAD
3846 || (p
->p_type
== PT_NOTE
3847 && bfd_get_format (abfd
) == bfd_core
))
3852 if ((flags
& SEC_LOAD
) != 0
3853 || (flags
& SEC_THREAD_LOCAL
) != 0)
3854 p
->p_filesz
+= adjust
;
3857 if (p
->p_type
== PT_LOAD
)
3859 bfd_signed_vma adjust
;
3861 if ((flags
& SEC_LOAD
) != 0)
3863 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3867 else if ((flags
& SEC_ALLOC
) != 0)
3869 /* The section VMA must equal the file position
3870 modulo the page size. FIXME: I'm not sure if
3871 this adjustment is really necessary. We used to
3872 not have the SEC_LOAD case just above, and then
3873 this was necessary, but now I'm not sure. */
3874 if ((abfd
->flags
& D_PAGED
) != 0)
3875 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3877 adjust
= (sec
->vma
- voff
) % align
;
3886 (* _bfd_error_handler
) (_("\
3887 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3888 bfd_section_name (abfd
, sec
),
3893 p
->p_memsz
+= adjust
;
3896 if ((flags
& SEC_LOAD
) != 0)
3897 p
->p_filesz
+= adjust
;
3902 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3903 used in a linker script we may have a section with
3904 SEC_LOAD clear but which is supposed to have
3906 if ((flags
& SEC_LOAD
) != 0
3907 || (flags
& SEC_HAS_CONTENTS
) != 0)
3908 off
+= sec
->_raw_size
;
3910 if ((flags
& SEC_ALLOC
) != 0
3911 && ((flags
& SEC_LOAD
) != 0
3912 || (flags
& SEC_THREAD_LOCAL
) == 0))
3913 voff
+= sec
->_raw_size
;
3916 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3918 /* The actual "note" segment has i == 0.
3919 This is the one that actually contains everything. */
3923 p
->p_filesz
= sec
->_raw_size
;
3924 off
+= sec
->_raw_size
;
3929 /* Fake sections -- don't need to be written. */
3932 flags
= sec
->flags
= 0;
3939 if ((sec
->flags
& SEC_LOAD
) != 0
3940 || (sec
->flags
& SEC_THREAD_LOCAL
) == 0
3941 || p
->p_type
== PT_TLS
)
3942 p
->p_memsz
+= sec
->_raw_size
;
3944 if ((flags
& SEC_LOAD
) != 0)
3945 p
->p_filesz
+= sec
->_raw_size
;
3947 if (p
->p_type
== PT_TLS
3948 && sec
->_raw_size
== 0
3949 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
3951 struct bfd_link_order
*o
;
3952 bfd_vma tbss_size
= 0;
3954 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
3955 if (tbss_size
< o
->offset
+ o
->size
)
3956 tbss_size
= o
->offset
+ o
->size
;
3958 p
->p_memsz
+= tbss_size
;
3961 if (align
> p
->p_align
3962 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3966 if (! m
->p_flags_valid
)
3969 if ((flags
& SEC_CODE
) != 0)
3971 if ((flags
& SEC_READONLY
) == 0)
3977 /* Now that we have set the section file positions, we can set up
3978 the file positions for the non PT_LOAD segments. */
3979 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3983 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3985 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3986 p
->p_offset
= m
->sections
[0]->filepos
;
3990 if (m
->includes_filehdr
)
3992 p
->p_vaddr
= filehdr_vaddr
;
3993 if (! m
->p_paddr_valid
)
3994 p
->p_paddr
= filehdr_paddr
;
3996 else if (m
->includes_phdrs
)
3998 p
->p_vaddr
= phdrs_vaddr
;
3999 if (! m
->p_paddr_valid
)
4000 p
->p_paddr
= phdrs_paddr
;
4005 /* Clear out any program headers we allocated but did not use. */
4006 for (; count
< alloc
; count
++, p
++)
4008 memset (p
, 0, sizeof *p
);
4009 p
->p_type
= PT_NULL
;
4012 elf_tdata (abfd
)->phdr
= phdrs
;
4014 elf_tdata (abfd
)->next_file_pos
= off
;
4016 /* Write out the program headers. */
4017 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4018 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
4024 /* Get the size of the program header.
4026 If this is called by the linker before any of the section VMA's are set, it
4027 can't calculate the correct value for a strange memory layout. This only
4028 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4029 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4030 data segment (exclusive of .interp and .dynamic).
4032 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4033 will be two segments. */
4035 static bfd_size_type
4036 get_program_header_size (bfd
*abfd
)
4040 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4042 /* We can't return a different result each time we're called. */
4043 if (elf_tdata (abfd
)->program_header_size
!= 0)
4044 return elf_tdata (abfd
)->program_header_size
;
4046 if (elf_tdata (abfd
)->segment_map
!= NULL
)
4048 struct elf_segment_map
*m
;
4051 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4053 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4054 return elf_tdata (abfd
)->program_header_size
;
4057 /* Assume we will need exactly two PT_LOAD segments: one for text
4058 and one for data. */
4061 s
= bfd_get_section_by_name (abfd
, ".interp");
4062 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4064 /* If we have a loadable interpreter section, we need a
4065 PT_INTERP segment. In this case, assume we also need a
4066 PT_PHDR segment, although that may not be true for all
4071 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4073 /* We need a PT_DYNAMIC segment. */
4077 if (elf_tdata (abfd
)->eh_frame_hdr
)
4079 /* We need a PT_GNU_EH_FRAME segment. */
4083 if (elf_tdata (abfd
)->stack_flags
)
4085 /* We need a PT_GNU_STACK segment. */
4089 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4091 if ((s
->flags
& SEC_LOAD
) != 0
4092 && strncmp (s
->name
, ".note", 5) == 0)
4094 /* We need a PT_NOTE segment. */
4099 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4101 if (s
->flags
& SEC_THREAD_LOCAL
)
4103 /* We need a PT_TLS segment. */
4109 /* Let the backend count up any program headers it might need. */
4110 if (bed
->elf_backend_additional_program_headers
)
4114 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
4120 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4121 return elf_tdata (abfd
)->program_header_size
;
4124 /* Work out the file positions of all the sections. This is called by
4125 _bfd_elf_compute_section_file_positions. All the section sizes and
4126 VMAs must be known before this is called.
4128 We do not consider reloc sections at this point, unless they form
4129 part of the loadable image. Reloc sections are assigned file
4130 positions in assign_file_positions_for_relocs, which is called by
4131 write_object_contents and final_link.
4133 We also don't set the positions of the .symtab and .strtab here. */
4136 assign_file_positions_except_relocs (bfd
*abfd
)
4138 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
4139 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
4140 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4141 unsigned int num_sec
= elf_numsections (abfd
);
4143 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4145 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4146 && bfd_get_format (abfd
) != bfd_core
)
4148 Elf_Internal_Shdr
**hdrpp
;
4151 /* Start after the ELF header. */
4152 off
= i_ehdrp
->e_ehsize
;
4154 /* We are not creating an executable, which means that we are
4155 not creating a program header, and that the actual order of
4156 the sections in the file is unimportant. */
4157 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4159 Elf_Internal_Shdr
*hdr
;
4162 if (hdr
->sh_type
== SHT_REL
4163 || hdr
->sh_type
== SHT_RELA
4164 || i
== tdata
->symtab_section
4165 || i
== tdata
->symtab_shndx_section
4166 || i
== tdata
->strtab_section
)
4168 hdr
->sh_offset
= -1;
4171 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4173 if (i
== SHN_LORESERVE
- 1)
4175 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4176 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4183 Elf_Internal_Shdr
**hdrpp
;
4185 /* Assign file positions for the loaded sections based on the
4186 assignment of sections to segments. */
4187 if (! assign_file_positions_for_segments (abfd
))
4190 /* Assign file positions for the other sections. */
4192 off
= elf_tdata (abfd
)->next_file_pos
;
4193 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4195 Elf_Internal_Shdr
*hdr
;
4198 if (hdr
->bfd_section
!= NULL
4199 && hdr
->bfd_section
->filepos
!= 0)
4200 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
4201 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4203 ((*_bfd_error_handler
)
4204 (_("%s: warning: allocated section `%s' not in segment"),
4205 bfd_get_filename (abfd
),
4206 (hdr
->bfd_section
== NULL
4208 : hdr
->bfd_section
->name
)));
4209 if ((abfd
->flags
& D_PAGED
) != 0)
4210 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
4212 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
4213 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4216 else if (hdr
->sh_type
== SHT_REL
4217 || hdr
->sh_type
== SHT_RELA
4218 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4219 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4220 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4221 hdr
->sh_offset
= -1;
4223 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4225 if (i
== SHN_LORESERVE
- 1)
4227 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4228 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4233 /* Place the section headers. */
4234 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
4235 i_ehdrp
->e_shoff
= off
;
4236 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4238 elf_tdata (abfd
)->next_file_pos
= off
;
4244 prep_headers (bfd
*abfd
)
4246 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4247 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4248 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4249 struct elf_strtab_hash
*shstrtab
;
4250 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4252 i_ehdrp
= elf_elfheader (abfd
);
4253 i_shdrp
= elf_elfsections (abfd
);
4255 shstrtab
= _bfd_elf_strtab_init ();
4256 if (shstrtab
== NULL
)
4259 elf_shstrtab (abfd
) = shstrtab
;
4261 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4262 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4263 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4264 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4266 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4267 i_ehdrp
->e_ident
[EI_DATA
] =
4268 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4269 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4271 if ((abfd
->flags
& DYNAMIC
) != 0)
4272 i_ehdrp
->e_type
= ET_DYN
;
4273 else if ((abfd
->flags
& EXEC_P
) != 0)
4274 i_ehdrp
->e_type
= ET_EXEC
;
4275 else if (bfd_get_format (abfd
) == bfd_core
)
4276 i_ehdrp
->e_type
= ET_CORE
;
4278 i_ehdrp
->e_type
= ET_REL
;
4280 switch (bfd_get_arch (abfd
))
4282 case bfd_arch_unknown
:
4283 i_ehdrp
->e_machine
= EM_NONE
;
4286 /* There used to be a long list of cases here, each one setting
4287 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4288 in the corresponding bfd definition. To avoid duplication,
4289 the switch was removed. Machines that need special handling
4290 can generally do it in elf_backend_final_write_processing(),
4291 unless they need the information earlier than the final write.
4292 Such need can generally be supplied by replacing the tests for
4293 e_machine with the conditions used to determine it. */
4295 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
4298 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4299 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4301 /* No program header, for now. */
4302 i_ehdrp
->e_phoff
= 0;
4303 i_ehdrp
->e_phentsize
= 0;
4304 i_ehdrp
->e_phnum
= 0;
4306 /* Each bfd section is section header entry. */
4307 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4308 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4310 /* If we're building an executable, we'll need a program header table. */
4311 if (abfd
->flags
& EXEC_P
)
4313 /* It all happens later. */
4315 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
4317 /* elf_build_phdrs() returns a (NULL-terminated) array of
4318 Elf_Internal_Phdrs. */
4319 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
4320 i_ehdrp
->e_phoff
= outbase
;
4321 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
4326 i_ehdrp
->e_phentsize
= 0;
4328 i_ehdrp
->e_phoff
= 0;
4331 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4332 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
4333 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4334 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
4335 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4336 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
4337 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4338 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4339 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4345 /* Assign file positions for all the reloc sections which are not part
4346 of the loadable file image. */
4349 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
4352 unsigned int i
, num_sec
;
4353 Elf_Internal_Shdr
**shdrpp
;
4355 off
= elf_tdata (abfd
)->next_file_pos
;
4357 num_sec
= elf_numsections (abfd
);
4358 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4360 Elf_Internal_Shdr
*shdrp
;
4363 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4364 && shdrp
->sh_offset
== -1)
4365 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
4368 elf_tdata (abfd
)->next_file_pos
= off
;
4372 _bfd_elf_write_object_contents (bfd
*abfd
)
4374 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4375 Elf_Internal_Ehdr
*i_ehdrp
;
4376 Elf_Internal_Shdr
**i_shdrp
;
4378 unsigned int count
, num_sec
;
4380 if (! abfd
->output_has_begun
4381 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
4384 i_shdrp
= elf_elfsections (abfd
);
4385 i_ehdrp
= elf_elfheader (abfd
);
4388 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4392 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4394 /* After writing the headers, we need to write the sections too... */
4395 num_sec
= elf_numsections (abfd
);
4396 for (count
= 1; count
< num_sec
; count
++)
4398 if (bed
->elf_backend_section_processing
)
4399 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4400 if (i_shdrp
[count
]->contents
)
4402 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4404 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4405 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4408 if (count
== SHN_LORESERVE
- 1)
4409 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4412 /* Write out the section header names. */
4413 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4414 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4417 if (bed
->elf_backend_final_write_processing
)
4418 (*bed
->elf_backend_final_write_processing
) (abfd
,
4419 elf_tdata (abfd
)->linker
);
4421 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4425 _bfd_elf_write_corefile_contents (bfd
*abfd
)
4427 /* Hopefully this can be done just like an object file. */
4428 return _bfd_elf_write_object_contents (abfd
);
4431 /* Given a section, search the header to find them. */
4434 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct sec
*asect
)
4436 const struct elf_backend_data
*bed
;
4439 if (elf_section_data (asect
) != NULL
4440 && elf_section_data (asect
)->this_idx
!= 0)
4441 return elf_section_data (asect
)->this_idx
;
4443 if (bfd_is_abs_section (asect
))
4445 else if (bfd_is_com_section (asect
))
4447 else if (bfd_is_und_section (asect
))
4451 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4452 int maxindex
= elf_numsections (abfd
);
4454 for (index
= 1; index
< maxindex
; index
++)
4456 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4458 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4464 bed
= get_elf_backend_data (abfd
);
4465 if (bed
->elf_backend_section_from_bfd_section
)
4469 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4474 bfd_set_error (bfd_error_nonrepresentable_section
);
4479 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4483 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
4485 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4487 flagword flags
= asym_ptr
->flags
;
4489 /* When gas creates relocations against local labels, it creates its
4490 own symbol for the section, but does put the symbol into the
4491 symbol chain, so udata is 0. When the linker is generating
4492 relocatable output, this section symbol may be for one of the
4493 input sections rather than the output section. */
4494 if (asym_ptr
->udata
.i
== 0
4495 && (flags
& BSF_SECTION_SYM
)
4496 && asym_ptr
->section
)
4500 if (asym_ptr
->section
->output_section
!= NULL
)
4501 indx
= asym_ptr
->section
->output_section
->index
;
4503 indx
= asym_ptr
->section
->index
;
4504 if (indx
< elf_num_section_syms (abfd
)
4505 && elf_section_syms (abfd
)[indx
] != NULL
)
4506 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4509 idx
= asym_ptr
->udata
.i
;
4513 /* This case can occur when using --strip-symbol on a symbol
4514 which is used in a relocation entry. */
4515 (*_bfd_error_handler
)
4516 (_("%s: symbol `%s' required but not present"),
4517 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4518 bfd_set_error (bfd_error_no_symbols
);
4525 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4526 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4527 elf_symbol_flags (flags
));
4535 /* Copy private BFD data. This copies any program header information. */
4538 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
4540 Elf_Internal_Ehdr
*iehdr
;
4541 struct elf_segment_map
*map
;
4542 struct elf_segment_map
*map_first
;
4543 struct elf_segment_map
**pointer_to_map
;
4544 Elf_Internal_Phdr
*segment
;
4547 unsigned int num_segments
;
4548 bfd_boolean phdr_included
= FALSE
;
4549 bfd_vma maxpagesize
;
4550 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
4551 unsigned int phdr_adjust_num
= 0;
4552 const struct elf_backend_data
*bed
;
4554 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4555 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4558 if (elf_tdata (ibfd
)->phdr
== NULL
)
4561 bed
= get_elf_backend_data (ibfd
);
4562 iehdr
= elf_elfheader (ibfd
);
4565 pointer_to_map
= &map_first
;
4567 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4568 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4570 /* Returns the end address of the segment + 1. */
4571 #define SEGMENT_END(segment, start) \
4572 (start + (segment->p_memsz > segment->p_filesz \
4573 ? segment->p_memsz : segment->p_filesz))
4575 #define SECTION_SIZE(section, segment) \
4576 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4577 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4578 ? section->_raw_size : 0)
4580 /* Returns TRUE if the given section is contained within
4581 the given segment. VMA addresses are compared. */
4582 #define IS_CONTAINED_BY_VMA(section, segment) \
4583 (section->vma >= segment->p_vaddr \
4584 && (section->vma + SECTION_SIZE (section, segment) \
4585 <= (SEGMENT_END (segment, segment->p_vaddr))))
4587 /* Returns TRUE if the given section is contained within
4588 the given segment. LMA addresses are compared. */
4589 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4590 (section->lma >= base \
4591 && (section->lma + SECTION_SIZE (section, segment) \
4592 <= SEGMENT_END (segment, base)))
4594 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4595 #define IS_COREFILE_NOTE(p, s) \
4596 (p->p_type == PT_NOTE \
4597 && bfd_get_format (ibfd) == bfd_core \
4598 && s->vma == 0 && s->lma == 0 \
4599 && (bfd_vma) s->filepos >= p->p_offset \
4600 && ((bfd_vma) s->filepos + s->_raw_size \
4601 <= p->p_offset + p->p_filesz))
4603 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4604 linker, which generates a PT_INTERP section with p_vaddr and
4605 p_memsz set to 0. */
4606 #define IS_SOLARIS_PT_INTERP(p, s) \
4608 && p->p_paddr == 0 \
4609 && p->p_memsz == 0 \
4610 && p->p_filesz > 0 \
4611 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4612 && s->_raw_size > 0 \
4613 && (bfd_vma) s->filepos >= p->p_offset \
4614 && ((bfd_vma) s->filepos + s->_raw_size \
4615 <= p->p_offset + p->p_filesz))
4617 /* Decide if the given section should be included in the given segment.
4618 A section will be included if:
4619 1. It is within the address space of the segment -- we use the LMA
4620 if that is set for the segment and the VMA otherwise,
4621 2. It is an allocated segment,
4622 3. There is an output section associated with it,
4623 4. The section has not already been allocated to a previous segment.
4624 5. PT_TLS segment includes only SHF_TLS sections.
4625 6. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4626 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4627 ((((segment->p_paddr \
4628 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4629 : IS_CONTAINED_BY_VMA (section, segment)) \
4630 && (section->flags & SEC_ALLOC) != 0) \
4631 || IS_COREFILE_NOTE (segment, section)) \
4632 && section->output_section != NULL \
4633 && (segment->p_type != PT_TLS \
4634 || (section->flags & SEC_THREAD_LOCAL)) \
4635 && (segment->p_type == PT_LOAD \
4636 || segment->p_type == PT_TLS \
4637 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4638 && ! section->segment_mark)
4640 /* Returns TRUE iff seg1 starts after the end of seg2. */
4641 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4642 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4644 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4645 their VMA address ranges and their LMA address ranges overlap.
4646 It is possible to have overlapping VMA ranges without overlapping LMA
4647 ranges. RedBoot images for example can have both .data and .bss mapped
4648 to the same VMA range, but with the .data section mapped to a different
4650 #define SEGMENT_OVERLAPS(seg1, seg2) \
4651 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4652 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4653 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4654 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4656 /* Initialise the segment mark field. */
4657 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4658 section
->segment_mark
= FALSE
;
4660 /* Scan through the segments specified in the program header
4661 of the input BFD. For this first scan we look for overlaps
4662 in the loadable segments. These can be created by weird
4663 parameters to objcopy. Also, fix some solaris weirdness. */
4664 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4669 Elf_Internal_Phdr
*segment2
;
4671 if (segment
->p_type
== PT_INTERP
)
4672 for (section
= ibfd
->sections
; section
; section
= section
->next
)
4673 if (IS_SOLARIS_PT_INTERP (segment
, section
))
4675 /* Mininal change so that the normal section to segment
4676 assigment code will work. */
4677 segment
->p_vaddr
= section
->vma
;
4681 if (segment
->p_type
!= PT_LOAD
)
4684 /* Determine if this segment overlaps any previous segments. */
4685 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4687 bfd_signed_vma extra_length
;
4689 if (segment2
->p_type
!= PT_LOAD
4690 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4693 /* Merge the two segments together. */
4694 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4696 /* Extend SEGMENT2 to include SEGMENT and then delete
4699 SEGMENT_END (segment
, segment
->p_vaddr
)
4700 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4702 if (extra_length
> 0)
4704 segment2
->p_memsz
+= extra_length
;
4705 segment2
->p_filesz
+= extra_length
;
4708 segment
->p_type
= PT_NULL
;
4710 /* Since we have deleted P we must restart the outer loop. */
4712 segment
= elf_tdata (ibfd
)->phdr
;
4717 /* Extend SEGMENT to include SEGMENT2 and then delete
4720 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4721 - SEGMENT_END (segment
, segment
->p_vaddr
);
4723 if (extra_length
> 0)
4725 segment
->p_memsz
+= extra_length
;
4726 segment
->p_filesz
+= extra_length
;
4729 segment2
->p_type
= PT_NULL
;
4734 /* The second scan attempts to assign sections to segments. */
4735 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4739 unsigned int section_count
;
4740 asection
** sections
;
4741 asection
* output_section
;
4743 bfd_vma matching_lma
;
4744 bfd_vma suggested_lma
;
4748 if (segment
->p_type
== PT_NULL
)
4751 /* Compute how many sections might be placed into this segment. */
4752 for (section
= ibfd
->sections
, section_count
= 0;
4754 section
= section
->next
)
4755 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4758 /* Allocate a segment map big enough to contain
4759 all of the sections we have selected. */
4760 amt
= sizeof (struct elf_segment_map
);
4761 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4762 map
= bfd_alloc (obfd
, amt
);
4766 /* Initialise the fields of the segment map. Default to
4767 using the physical address of the segment in the input BFD. */
4769 map
->p_type
= segment
->p_type
;
4770 map
->p_flags
= segment
->p_flags
;
4771 map
->p_flags_valid
= 1;
4772 map
->p_paddr
= segment
->p_paddr
;
4773 map
->p_paddr_valid
= 1;
4775 /* Determine if this segment contains the ELF file header
4776 and if it contains the program headers themselves. */
4777 map
->includes_filehdr
= (segment
->p_offset
== 0
4778 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4780 map
->includes_phdrs
= 0;
4782 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4784 map
->includes_phdrs
=
4785 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4786 && (segment
->p_offset
+ segment
->p_filesz
4787 >= ((bfd_vma
) iehdr
->e_phoff
4788 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4790 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4791 phdr_included
= TRUE
;
4794 if (section_count
== 0)
4796 /* Special segments, such as the PT_PHDR segment, may contain
4797 no sections, but ordinary, loadable segments should contain
4798 something. They are allowed by the ELF spec however, so only
4799 a warning is produced. */
4800 if (segment
->p_type
== PT_LOAD
)
4801 (*_bfd_error_handler
)
4802 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4803 bfd_archive_filename (ibfd
));
4806 *pointer_to_map
= map
;
4807 pointer_to_map
= &map
->next
;
4812 /* Now scan the sections in the input BFD again and attempt
4813 to add their corresponding output sections to the segment map.
4814 The problem here is how to handle an output section which has
4815 been moved (ie had its LMA changed). There are four possibilities:
4817 1. None of the sections have been moved.
4818 In this case we can continue to use the segment LMA from the
4821 2. All of the sections have been moved by the same amount.
4822 In this case we can change the segment's LMA to match the LMA
4823 of the first section.
4825 3. Some of the sections have been moved, others have not.
4826 In this case those sections which have not been moved can be
4827 placed in the current segment which will have to have its size,
4828 and possibly its LMA changed, and a new segment or segments will
4829 have to be created to contain the other sections.
4831 4. The sections have been moved, but not by the same amount.
4832 In this case we can change the segment's LMA to match the LMA
4833 of the first section and we will have to create a new segment
4834 or segments to contain the other sections.
4836 In order to save time, we allocate an array to hold the section
4837 pointers that we are interested in. As these sections get assigned
4838 to a segment, they are removed from this array. */
4840 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4841 to work around this long long bug. */
4842 amt
= section_count
* sizeof (asection
*);
4843 sections
= bfd_malloc (amt
);
4844 if (sections
== NULL
)
4847 /* Step One: Scan for segment vs section LMA conflicts.
4848 Also add the sections to the section array allocated above.
4849 Also add the sections to the current segment. In the common
4850 case, where the sections have not been moved, this means that
4851 we have completely filled the segment, and there is nothing
4857 for (j
= 0, section
= ibfd
->sections
;
4859 section
= section
->next
)
4861 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4863 output_section
= section
->output_section
;
4865 sections
[j
++] = section
;
4867 /* The Solaris native linker always sets p_paddr to 0.
4868 We try to catch that case here, and set it to the
4869 correct value. Note - some backends require that
4870 p_paddr be left as zero. */
4871 if (segment
->p_paddr
== 0
4872 && segment
->p_vaddr
!= 0
4873 && (! bed
->want_p_paddr_set_to_zero
)
4875 && output_section
->lma
!= 0
4876 && (output_section
->vma
== (segment
->p_vaddr
4877 + (map
->includes_filehdr
4880 + (map
->includes_phdrs
4882 * iehdr
->e_phentsize
)
4884 map
->p_paddr
= segment
->p_vaddr
;
4886 /* Match up the physical address of the segment with the
4887 LMA address of the output section. */
4888 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4889 || IS_COREFILE_NOTE (segment
, section
)
4890 || (bed
->want_p_paddr_set_to_zero
&&
4891 IS_CONTAINED_BY_VMA (output_section
, segment
))
4894 if (matching_lma
== 0)
4895 matching_lma
= output_section
->lma
;
4897 /* We assume that if the section fits within the segment
4898 then it does not overlap any other section within that
4900 map
->sections
[isec
++] = output_section
;
4902 else if (suggested_lma
== 0)
4903 suggested_lma
= output_section
->lma
;
4907 BFD_ASSERT (j
== section_count
);
4909 /* Step Two: Adjust the physical address of the current segment,
4911 if (isec
== section_count
)
4913 /* All of the sections fitted within the segment as currently
4914 specified. This is the default case. Add the segment to
4915 the list of built segments and carry on to process the next
4916 program header in the input BFD. */
4917 map
->count
= section_count
;
4918 *pointer_to_map
= map
;
4919 pointer_to_map
= &map
->next
;
4926 if (matching_lma
!= 0)
4928 /* At least one section fits inside the current segment.
4929 Keep it, but modify its physical address to match the
4930 LMA of the first section that fitted. */
4931 map
->p_paddr
= matching_lma
;
4935 /* None of the sections fitted inside the current segment.
4936 Change the current segment's physical address to match
4937 the LMA of the first section. */
4938 map
->p_paddr
= suggested_lma
;
4941 /* Offset the segment physical address from the lma
4942 to allow for space taken up by elf headers. */
4943 if (map
->includes_filehdr
)
4944 map
->p_paddr
-= iehdr
->e_ehsize
;
4946 if (map
->includes_phdrs
)
4948 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4950 /* iehdr->e_phnum is just an estimate of the number
4951 of program headers that we will need. Make a note
4952 here of the number we used and the segment we chose
4953 to hold these headers, so that we can adjust the
4954 offset when we know the correct value. */
4955 phdr_adjust_num
= iehdr
->e_phnum
;
4956 phdr_adjust_seg
= map
;
4960 /* Step Three: Loop over the sections again, this time assigning
4961 those that fit to the current segment and removing them from the
4962 sections array; but making sure not to leave large gaps. Once all
4963 possible sections have been assigned to the current segment it is
4964 added to the list of built segments and if sections still remain
4965 to be assigned, a new segment is constructed before repeating
4973 /* Fill the current segment with sections that fit. */
4974 for (j
= 0; j
< section_count
; j
++)
4976 section
= sections
[j
];
4978 if (section
== NULL
)
4981 output_section
= section
->output_section
;
4983 BFD_ASSERT (output_section
!= NULL
);
4985 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4986 || IS_COREFILE_NOTE (segment
, section
))
4988 if (map
->count
== 0)
4990 /* If the first section in a segment does not start at
4991 the beginning of the segment, then something is
4993 if (output_section
->lma
!=
4995 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4996 + (map
->includes_phdrs
4997 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5003 asection
* prev_sec
;
5005 prev_sec
= map
->sections
[map
->count
- 1];
5007 /* If the gap between the end of the previous section
5008 and the start of this section is more than
5009 maxpagesize then we need to start a new segment. */
5010 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
5012 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5013 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
5014 > output_section
->lma
))
5016 if (suggested_lma
== 0)
5017 suggested_lma
= output_section
->lma
;
5023 map
->sections
[map
->count
++] = output_section
;
5026 section
->segment_mark
= TRUE
;
5028 else if (suggested_lma
== 0)
5029 suggested_lma
= output_section
->lma
;
5032 BFD_ASSERT (map
->count
> 0);
5034 /* Add the current segment to the list of built segments. */
5035 *pointer_to_map
= map
;
5036 pointer_to_map
= &map
->next
;
5038 if (isec
< section_count
)
5040 /* We still have not allocated all of the sections to
5041 segments. Create a new segment here, initialise it
5042 and carry on looping. */
5043 amt
= sizeof (struct elf_segment_map
);
5044 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5045 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)
5078 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5079 map
->p_paddr_valid
= 0;
5081 elf_tdata (obfd
)->segment_map
= map_first
;
5083 /* If we had to estimate the number of program headers that were
5084 going to be needed, then check our estimate now and adjust
5085 the offset if necessary. */
5086 if (phdr_adjust_seg
!= NULL
)
5090 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5093 if (count
> phdr_adjust_num
)
5094 phdr_adjust_seg
->p_paddr
5095 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5099 /* Final Step: Sort the segments into ascending order of physical
5101 if (map_first
!= NULL
)
5103 struct elf_segment_map
*prev
;
5106 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
5108 /* Yes I know - its a bubble sort.... */
5109 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
5111 /* Swap map and map->next. */
5112 prev
->next
= map
->next
;
5113 map
->next
= map
->next
->next
;
5114 prev
->next
->next
= map
;
5125 #undef IS_CONTAINED_BY_VMA
5126 #undef IS_CONTAINED_BY_LMA
5127 #undef IS_COREFILE_NOTE
5128 #undef IS_SOLARIS_PT_INTERP
5129 #undef INCLUDE_SECTION_IN_SEGMENT
5130 #undef SEGMENT_AFTER_SEGMENT
5131 #undef SEGMENT_OVERLAPS
5135 /* Copy private section information. This copies over the entsize
5136 field, and sometimes the info field. */
5139 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
5144 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5146 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5147 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5150 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
5154 /* Only set up the segments if there are no more SEC_ALLOC
5155 sections. FIXME: This won't do the right thing if objcopy is
5156 used to remove the last SEC_ALLOC section, since objcopy
5157 won't call this routine in that case. */
5158 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
5159 if ((s
->flags
& SEC_ALLOC
) != 0)
5163 if (! copy_private_bfd_data (ibfd
, obfd
))
5168 ihdr
= &elf_section_data (isec
)->this_hdr
;
5169 ohdr
= &elf_section_data (osec
)->this_hdr
;
5171 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
5173 if (ihdr
->sh_type
== SHT_SYMTAB
5174 || ihdr
->sh_type
== SHT_DYNSYM
5175 || ihdr
->sh_type
== SHT_GNU_verneed
5176 || ihdr
->sh_type
== SHT_GNU_verdef
)
5177 ohdr
->sh_info
= ihdr
->sh_info
;
5179 /* Set things up for objcopy. The output SHT_GROUP section will
5180 have its elf_next_in_group pointing back to the input group
5182 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5183 elf_group_name (osec
) = elf_group_name (isec
);
5185 osec
->use_rela_p
= isec
->use_rela_p
;
5190 /* Copy private symbol information. If this symbol is in a section
5191 which we did not map into a BFD section, try to map the section
5192 index correctly. We use special macro definitions for the mapped
5193 section indices; these definitions are interpreted by the
5194 swap_out_syms function. */
5196 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5197 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5198 #define MAP_STRTAB (SHN_HIOS + 3)
5199 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5200 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5203 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
5208 elf_symbol_type
*isym
, *osym
;
5210 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5211 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5214 isym
= elf_symbol_from (ibfd
, isymarg
);
5215 osym
= elf_symbol_from (obfd
, osymarg
);
5219 && bfd_is_abs_section (isym
->symbol
.section
))
5223 shndx
= isym
->internal_elf_sym
.st_shndx
;
5224 if (shndx
== elf_onesymtab (ibfd
))
5225 shndx
= MAP_ONESYMTAB
;
5226 else if (shndx
== elf_dynsymtab (ibfd
))
5227 shndx
= MAP_DYNSYMTAB
;
5228 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
5230 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
5231 shndx
= MAP_SHSTRTAB
;
5232 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
5233 shndx
= MAP_SYM_SHNDX
;
5234 osym
->internal_elf_sym
.st_shndx
= shndx
;
5240 /* Swap out the symbols. */
5243 swap_out_syms (bfd
*abfd
,
5244 struct bfd_strtab_hash
**sttp
,
5247 const struct elf_backend_data
*bed
;
5250 struct bfd_strtab_hash
*stt
;
5251 Elf_Internal_Shdr
*symtab_hdr
;
5252 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5253 Elf_Internal_Shdr
*symstrtab_hdr
;
5254 char *outbound_syms
;
5255 char *outbound_shndx
;
5259 if (!elf_map_symbols (abfd
))
5262 /* Dump out the symtabs. */
5263 stt
= _bfd_elf_stringtab_init ();
5267 bed
= get_elf_backend_data (abfd
);
5268 symcount
= bfd_get_symcount (abfd
);
5269 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5270 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5271 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5272 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5273 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5274 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
5276 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5277 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5279 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
5280 outbound_syms
= bfd_alloc (abfd
, amt
);
5281 if (outbound_syms
== NULL
)
5283 _bfd_stringtab_free (stt
);
5286 symtab_hdr
->contents
= outbound_syms
;
5288 outbound_shndx
= NULL
;
5289 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5290 if (symtab_shndx_hdr
->sh_name
!= 0)
5292 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5293 outbound_shndx
= bfd_zalloc (abfd
, amt
);
5294 if (outbound_shndx
== NULL
)
5296 _bfd_stringtab_free (stt
);
5300 symtab_shndx_hdr
->contents
= outbound_shndx
;
5301 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5302 symtab_shndx_hdr
->sh_size
= amt
;
5303 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5304 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5307 /* Now generate the data (for "contents"). */
5309 /* Fill in zeroth symbol and swap it out. */
5310 Elf_Internal_Sym sym
;
5316 sym
.st_shndx
= SHN_UNDEF
;
5317 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5318 outbound_syms
+= bed
->s
->sizeof_sym
;
5319 if (outbound_shndx
!= NULL
)
5320 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5323 syms
= bfd_get_outsymbols (abfd
);
5324 for (idx
= 0; idx
< symcount
; idx
++)
5326 Elf_Internal_Sym sym
;
5327 bfd_vma value
= syms
[idx
]->value
;
5328 elf_symbol_type
*type_ptr
;
5329 flagword flags
= syms
[idx
]->flags
;
5332 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5334 /* Local section symbols have no name. */
5339 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5342 if (sym
.st_name
== (unsigned long) -1)
5344 _bfd_stringtab_free (stt
);
5349 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5351 if ((flags
& BSF_SECTION_SYM
) == 0
5352 && bfd_is_com_section (syms
[idx
]->section
))
5354 /* ELF common symbols put the alignment into the `value' field,
5355 and the size into the `size' field. This is backwards from
5356 how BFD handles it, so reverse it here. */
5357 sym
.st_size
= value
;
5358 if (type_ptr
== NULL
5359 || type_ptr
->internal_elf_sym
.st_value
== 0)
5360 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5362 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5363 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5364 (abfd
, syms
[idx
]->section
);
5368 asection
*sec
= syms
[idx
]->section
;
5371 if (sec
->output_section
)
5373 value
+= sec
->output_offset
;
5374 sec
= sec
->output_section
;
5377 /* Don't add in the section vma for relocatable output. */
5378 if (! relocatable_p
)
5380 sym
.st_value
= value
;
5381 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5383 if (bfd_is_abs_section (sec
)
5385 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5387 /* This symbol is in a real ELF section which we did
5388 not create as a BFD section. Undo the mapping done
5389 by copy_private_symbol_data. */
5390 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5394 shndx
= elf_onesymtab (abfd
);
5397 shndx
= elf_dynsymtab (abfd
);
5400 shndx
= elf_tdata (abfd
)->strtab_section
;
5403 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5406 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5414 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5420 /* Writing this would be a hell of a lot easier if
5421 we had some decent documentation on bfd, and
5422 knew what to expect of the library, and what to
5423 demand of applications. For example, it
5424 appears that `objcopy' might not set the
5425 section of a symbol to be a section that is
5426 actually in the output file. */
5427 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5430 _bfd_error_handler (_("\
5431 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5432 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
5434 bfd_set_error (bfd_error_invalid_operation
);
5435 _bfd_stringtab_free (stt
);
5439 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5440 BFD_ASSERT (shndx
!= -1);
5444 sym
.st_shndx
= shndx
;
5447 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5449 else if ((flags
& BSF_FUNCTION
) != 0)
5451 else if ((flags
& BSF_OBJECT
) != 0)
5456 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5459 /* Processor-specific types. */
5460 if (type_ptr
!= NULL
5461 && bed
->elf_backend_get_symbol_type
)
5462 type
= ((*bed
->elf_backend_get_symbol_type
)
5463 (&type_ptr
->internal_elf_sym
, type
));
5465 if (flags
& BSF_SECTION_SYM
)
5467 if (flags
& BSF_GLOBAL
)
5468 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5470 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5472 else if (bfd_is_com_section (syms
[idx
]->section
))
5473 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5474 else if (bfd_is_und_section (syms
[idx
]->section
))
5475 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5479 else if (flags
& BSF_FILE
)
5480 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5483 int bind
= STB_LOCAL
;
5485 if (flags
& BSF_LOCAL
)
5487 else if (flags
& BSF_WEAK
)
5489 else if (flags
& BSF_GLOBAL
)
5492 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5495 if (type_ptr
!= NULL
)
5496 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5500 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5501 outbound_syms
+= bed
->s
->sizeof_sym
;
5502 if (outbound_shndx
!= NULL
)
5503 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5507 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5508 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5510 symstrtab_hdr
->sh_flags
= 0;
5511 symstrtab_hdr
->sh_addr
= 0;
5512 symstrtab_hdr
->sh_entsize
= 0;
5513 symstrtab_hdr
->sh_link
= 0;
5514 symstrtab_hdr
->sh_info
= 0;
5515 symstrtab_hdr
->sh_addralign
= 1;
5520 /* Return the number of bytes required to hold the symtab vector.
5522 Note that we base it on the count plus 1, since we will null terminate
5523 the vector allocated based on this size. However, the ELF symbol table
5524 always has a dummy entry as symbol #0, so it ends up even. */
5527 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
5531 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5533 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5534 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5536 symtab_size
-= sizeof (asymbol
*);
5542 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
5546 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5548 if (elf_dynsymtab (abfd
) == 0)
5550 bfd_set_error (bfd_error_invalid_operation
);
5554 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5555 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5557 symtab_size
-= sizeof (asymbol
*);
5563 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
5566 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5569 /* Canonicalize the relocs. */
5572 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
5579 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5581 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
5584 tblptr
= section
->relocation
;
5585 for (i
= 0; i
< section
->reloc_count
; i
++)
5586 *relptr
++ = tblptr
++;
5590 return section
->reloc_count
;
5594 _bfd_elf_get_symtab (bfd
*abfd
, asymbol
**allocation
)
5596 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5597 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
5600 bfd_get_symcount (abfd
) = symcount
;
5605 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
5606 asymbol
**allocation
)
5608 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5609 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
5612 bfd_get_dynamic_symcount (abfd
) = symcount
;
5616 /* Return the size required for the dynamic reloc entries. Any
5617 section that was actually installed in the BFD, and has type
5618 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5619 considered to be a dynamic reloc section. */
5622 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
5627 if (elf_dynsymtab (abfd
) == 0)
5629 bfd_set_error (bfd_error_invalid_operation
);
5633 ret
= sizeof (arelent
*);
5634 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5635 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5636 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5637 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5638 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5639 * sizeof (arelent
*));
5644 /* Canonicalize the dynamic relocation entries. Note that we return
5645 the dynamic relocations as a single block, although they are
5646 actually associated with particular sections; the interface, which
5647 was designed for SunOS style shared libraries, expects that there
5648 is only one set of dynamic relocs. Any section that was actually
5649 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5650 the dynamic symbol table, is considered to be a dynamic reloc
5654 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
5658 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
5662 if (elf_dynsymtab (abfd
) == 0)
5664 bfd_set_error (bfd_error_invalid_operation
);
5668 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5670 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5672 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5673 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5674 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5679 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
5681 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5683 for (i
= 0; i
< count
; i
++)
5694 /* Read in the version information. */
5697 _bfd_elf_slurp_version_tables (bfd
*abfd
)
5699 bfd_byte
*contents
= NULL
;
5702 if (elf_dynverdef (abfd
) != 0)
5704 Elf_Internal_Shdr
*hdr
;
5705 Elf_External_Verdef
*everdef
;
5706 Elf_Internal_Verdef
*iverdef
;
5707 Elf_Internal_Verdef
*iverdefarr
;
5708 Elf_Internal_Verdef iverdefmem
;
5710 unsigned int maxidx
;
5712 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5714 contents
= bfd_malloc (hdr
->sh_size
);
5715 if (contents
== NULL
)
5717 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5718 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5721 /* We know the number of entries in the section but not the maximum
5722 index. Therefore we have to run through all entries and find
5724 everdef
= (Elf_External_Verdef
*) contents
;
5726 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5728 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5730 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5731 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5733 everdef
= ((Elf_External_Verdef
*)
5734 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5737 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5738 elf_tdata (abfd
)->verdef
= bfd_zalloc (abfd
, amt
);
5739 if (elf_tdata (abfd
)->verdef
== NULL
)
5742 elf_tdata (abfd
)->cverdefs
= maxidx
;
5744 everdef
= (Elf_External_Verdef
*) contents
;
5745 iverdefarr
= elf_tdata (abfd
)->verdef
;
5746 for (i
= 0; i
< hdr
->sh_info
; i
++)
5748 Elf_External_Verdaux
*everdaux
;
5749 Elf_Internal_Verdaux
*iverdaux
;
5752 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5754 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5755 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5757 iverdef
->vd_bfd
= abfd
;
5759 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5760 iverdef
->vd_auxptr
= bfd_alloc (abfd
, amt
);
5761 if (iverdef
->vd_auxptr
== NULL
)
5764 everdaux
= ((Elf_External_Verdaux
*)
5765 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5766 iverdaux
= iverdef
->vd_auxptr
;
5767 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5769 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5771 iverdaux
->vda_nodename
=
5772 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5773 iverdaux
->vda_name
);
5774 if (iverdaux
->vda_nodename
== NULL
)
5777 if (j
+ 1 < iverdef
->vd_cnt
)
5778 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5780 iverdaux
->vda_nextptr
= NULL
;
5782 everdaux
= ((Elf_External_Verdaux
*)
5783 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5786 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5788 if (i
+ 1 < hdr
->sh_info
)
5789 iverdef
->vd_nextdef
= iverdef
+ 1;
5791 iverdef
->vd_nextdef
= NULL
;
5793 everdef
= ((Elf_External_Verdef
*)
5794 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5801 if (elf_dynverref (abfd
) != 0)
5803 Elf_Internal_Shdr
*hdr
;
5804 Elf_External_Verneed
*everneed
;
5805 Elf_Internal_Verneed
*iverneed
;
5808 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5810 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5811 elf_tdata (abfd
)->verref
= bfd_zalloc (abfd
, amt
);
5812 if (elf_tdata (abfd
)->verref
== NULL
)
5815 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5817 contents
= bfd_malloc (hdr
->sh_size
);
5818 if (contents
== NULL
)
5820 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5821 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5824 everneed
= (Elf_External_Verneed
*) contents
;
5825 iverneed
= elf_tdata (abfd
)->verref
;
5826 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5828 Elf_External_Vernaux
*evernaux
;
5829 Elf_Internal_Vernaux
*ivernaux
;
5832 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5834 iverneed
->vn_bfd
= abfd
;
5836 iverneed
->vn_filename
=
5837 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5839 if (iverneed
->vn_filename
== NULL
)
5842 amt
= iverneed
->vn_cnt
;
5843 amt
*= sizeof (Elf_Internal_Vernaux
);
5844 iverneed
->vn_auxptr
= bfd_alloc (abfd
, amt
);
5846 evernaux
= ((Elf_External_Vernaux
*)
5847 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5848 ivernaux
= iverneed
->vn_auxptr
;
5849 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5851 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5853 ivernaux
->vna_nodename
=
5854 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5855 ivernaux
->vna_name
);
5856 if (ivernaux
->vna_nodename
== NULL
)
5859 if (j
+ 1 < iverneed
->vn_cnt
)
5860 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5862 ivernaux
->vna_nextptr
= NULL
;
5864 evernaux
= ((Elf_External_Vernaux
*)
5865 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5868 if (i
+ 1 < hdr
->sh_info
)
5869 iverneed
->vn_nextref
= iverneed
+ 1;
5871 iverneed
->vn_nextref
= NULL
;
5873 everneed
= ((Elf_External_Verneed
*)
5874 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5884 if (contents
!= NULL
)
5890 _bfd_elf_make_empty_symbol (bfd
*abfd
)
5892 elf_symbol_type
*newsym
;
5893 bfd_size_type amt
= sizeof (elf_symbol_type
);
5895 newsym
= bfd_zalloc (abfd
, amt
);
5900 newsym
->symbol
.the_bfd
= abfd
;
5901 return &newsym
->symbol
;
5906 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
5910 bfd_symbol_info (symbol
, ret
);
5913 /* Return whether a symbol name implies a local symbol. Most targets
5914 use this function for the is_local_label_name entry point, but some
5918 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
5921 /* Normal local symbols start with ``.L''. */
5922 if (name
[0] == '.' && name
[1] == 'L')
5925 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5926 DWARF debugging symbols starting with ``..''. */
5927 if (name
[0] == '.' && name
[1] == '.')
5930 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5931 emitting DWARF debugging output. I suspect this is actually a
5932 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5933 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5934 underscore to be emitted on some ELF targets). For ease of use,
5935 we treat such symbols as local. */
5936 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5943 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
5944 asymbol
*symbol ATTRIBUTE_UNUSED
)
5951 _bfd_elf_set_arch_mach (bfd
*abfd
,
5952 enum bfd_architecture arch
,
5953 unsigned long machine
)
5955 /* If this isn't the right architecture for this backend, and this
5956 isn't the generic backend, fail. */
5957 if (arch
!= get_elf_backend_data (abfd
)->arch
5958 && arch
!= bfd_arch_unknown
5959 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5962 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5965 /* Find the function to a particular section and offset,
5966 for error reporting. */
5969 elf_find_function (bfd
*abfd ATTRIBUTE_UNUSED
,
5973 const char **filename_ptr
,
5974 const char **functionname_ptr
)
5976 const char *filename
;
5985 for (p
= symbols
; *p
!= NULL
; p
++)
5989 q
= (elf_symbol_type
*) *p
;
5991 if (bfd_get_section (&q
->symbol
) != section
)
5994 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5999 filename
= bfd_asymbol_name (&q
->symbol
);
6003 if (q
->symbol
.section
== section
6004 && q
->symbol
.value
>= low_func
6005 && q
->symbol
.value
<= offset
)
6007 func
= (asymbol
*) q
;
6008 low_func
= q
->symbol
.value
;
6018 *filename_ptr
= filename
;
6019 if (functionname_ptr
)
6020 *functionname_ptr
= bfd_asymbol_name (func
);
6025 /* Find the nearest line to a particular section and offset,
6026 for error reporting. */
6029 _bfd_elf_find_nearest_line (bfd
*abfd
,
6033 const char **filename_ptr
,
6034 const char **functionname_ptr
,
6035 unsigned int *line_ptr
)
6039 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
6040 filename_ptr
, functionname_ptr
,
6043 if (!*functionname_ptr
)
6044 elf_find_function (abfd
, section
, symbols
, offset
,
6045 *filename_ptr
? NULL
: filename_ptr
,
6051 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
6052 filename_ptr
, functionname_ptr
,
6054 &elf_tdata (abfd
)->dwarf2_find_line_info
))
6056 if (!*functionname_ptr
)
6057 elf_find_function (abfd
, section
, symbols
, offset
,
6058 *filename_ptr
? NULL
: filename_ptr
,
6064 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
6065 &found
, filename_ptr
,
6066 functionname_ptr
, line_ptr
,
6067 &elf_tdata (abfd
)->line_info
))
6069 if (found
&& (*functionname_ptr
|| *line_ptr
))
6072 if (symbols
== NULL
)
6075 if (! elf_find_function (abfd
, section
, symbols
, offset
,
6076 filename_ptr
, functionname_ptr
))
6084 _bfd_elf_sizeof_headers (bfd
*abfd
, bfd_boolean reloc
)
6088 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
6090 ret
+= get_program_header_size (abfd
);
6095 _bfd_elf_set_section_contents (bfd
*abfd
,
6099 bfd_size_type count
)
6101 Elf_Internal_Shdr
*hdr
;
6104 if (! abfd
->output_has_begun
6105 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6108 hdr
= &elf_section_data (section
)->this_hdr
;
6109 pos
= hdr
->sh_offset
+ offset
;
6110 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6111 || bfd_bwrite (location
, count
, abfd
) != count
)
6118 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
6119 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
6120 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
6125 /* Try to convert a non-ELF reloc into an ELF one. */
6128 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
6130 /* Check whether we really have an ELF howto. */
6132 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
6134 bfd_reloc_code_real_type code
;
6135 reloc_howto_type
*howto
;
6137 /* Alien reloc: Try to determine its type to replace it with an
6138 equivalent ELF reloc. */
6140 if (areloc
->howto
->pc_relative
)
6142 switch (areloc
->howto
->bitsize
)
6145 code
= BFD_RELOC_8_PCREL
;
6148 code
= BFD_RELOC_12_PCREL
;
6151 code
= BFD_RELOC_16_PCREL
;
6154 code
= BFD_RELOC_24_PCREL
;
6157 code
= BFD_RELOC_32_PCREL
;
6160 code
= BFD_RELOC_64_PCREL
;
6166 howto
= bfd_reloc_type_lookup (abfd
, code
);
6168 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
6170 if (howto
->pcrel_offset
)
6171 areloc
->addend
+= areloc
->address
;
6173 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
6178 switch (areloc
->howto
->bitsize
)
6184 code
= BFD_RELOC_14
;
6187 code
= BFD_RELOC_16
;
6190 code
= BFD_RELOC_26
;
6193 code
= BFD_RELOC_32
;
6196 code
= BFD_RELOC_64
;
6202 howto
= bfd_reloc_type_lookup (abfd
, code
);
6206 areloc
->howto
= howto
;
6214 (*_bfd_error_handler
)
6215 (_("%s: unsupported relocation type %s"),
6216 bfd_archive_filename (abfd
), areloc
->howto
->name
);
6217 bfd_set_error (bfd_error_bad_value
);
6222 _bfd_elf_close_and_cleanup (bfd
*abfd
)
6224 if (bfd_get_format (abfd
) == bfd_object
)
6226 if (elf_shstrtab (abfd
) != NULL
)
6227 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
6230 return _bfd_generic_close_and_cleanup (abfd
);
6233 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6234 in the relocation's offset. Thus we cannot allow any sort of sanity
6235 range-checking to interfere. There is nothing else to do in processing
6238 bfd_reloc_status_type
6239 _bfd_elf_rel_vtable_reloc_fn
6240 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
6241 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
,
6242 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
6243 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
6245 return bfd_reloc_ok
;
6248 /* Elf core file support. Much of this only works on native
6249 toolchains, since we rely on knowing the
6250 machine-dependent procfs structure in order to pick
6251 out details about the corefile. */
6253 #ifdef HAVE_SYS_PROCFS_H
6254 # include <sys/procfs.h>
6257 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6260 elfcore_make_pid (bfd
*abfd
)
6262 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
6263 + (elf_tdata (abfd
)->core_pid
));
6266 /* If there isn't a section called NAME, make one, using
6267 data from SECT. Note, this function will generate a
6268 reference to NAME, so you shouldn't deallocate or
6272 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
6276 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
6279 sect2
= bfd_make_section (abfd
, name
);
6283 sect2
->_raw_size
= sect
->_raw_size
;
6284 sect2
->filepos
= sect
->filepos
;
6285 sect2
->flags
= sect
->flags
;
6286 sect2
->alignment_power
= sect
->alignment_power
;
6290 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6291 actually creates up to two pseudosections:
6292 - For the single-threaded case, a section named NAME, unless
6293 such a section already exists.
6294 - For the multi-threaded case, a section named "NAME/PID", where
6295 PID is elfcore_make_pid (abfd).
6296 Both pseudosections have identical contents. */
6298 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
6304 char *threaded_name
;
6308 /* Build the section name. */
6310 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6311 len
= strlen (buf
) + 1;
6312 threaded_name
= bfd_alloc (abfd
, len
);
6313 if (threaded_name
== NULL
)
6315 memcpy (threaded_name
, buf
, len
);
6317 sect
= bfd_make_section (abfd
, threaded_name
);
6320 sect
->_raw_size
= size
;
6321 sect
->filepos
= filepos
;
6322 sect
->flags
= SEC_HAS_CONTENTS
;
6323 sect
->alignment_power
= 2;
6325 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6328 /* prstatus_t exists on:
6330 linux 2.[01] + glibc
6334 #if defined (HAVE_PRSTATUS_T)
6337 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6342 if (note
->descsz
== sizeof (prstatus_t
))
6346 raw_size
= sizeof (prstat
.pr_reg
);
6347 offset
= offsetof (prstatus_t
, pr_reg
);
6348 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6350 /* Do not overwrite the core signal if it
6351 has already been set by another thread. */
6352 if (elf_tdata (abfd
)->core_signal
== 0)
6353 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6354 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6356 /* pr_who exists on:
6359 pr_who doesn't exist on:
6362 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6363 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6366 #if defined (HAVE_PRSTATUS32_T)
6367 else if (note
->descsz
== sizeof (prstatus32_t
))
6369 /* 64-bit host, 32-bit corefile */
6370 prstatus32_t prstat
;
6372 raw_size
= sizeof (prstat
.pr_reg
);
6373 offset
= offsetof (prstatus32_t
, pr_reg
);
6374 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6376 /* Do not overwrite the core signal if it
6377 has already been set by another thread. */
6378 if (elf_tdata (abfd
)->core_signal
== 0)
6379 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6380 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6382 /* pr_who exists on:
6385 pr_who doesn't exist on:
6388 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6389 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6392 #endif /* HAVE_PRSTATUS32_T */
6395 /* Fail - we don't know how to handle any other
6396 note size (ie. data object type). */
6400 /* Make a ".reg/999" section and a ".reg" section. */
6401 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6402 raw_size
, note
->descpos
+ offset
);
6404 #endif /* defined (HAVE_PRSTATUS_T) */
6406 /* Create a pseudosection containing the exact contents of NOTE. */
6408 elfcore_make_note_pseudosection (bfd
*abfd
,
6410 Elf_Internal_Note
*note
)
6412 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6413 note
->descsz
, note
->descpos
);
6416 /* There isn't a consistent prfpregset_t across platforms,
6417 but it doesn't matter, because we don't have to pick this
6418 data structure apart. */
6421 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
6423 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6426 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6427 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6431 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
6433 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6436 #if defined (HAVE_PRPSINFO_T)
6437 typedef prpsinfo_t elfcore_psinfo_t
;
6438 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6439 typedef prpsinfo32_t elfcore_psinfo32_t
;
6443 #if defined (HAVE_PSINFO_T)
6444 typedef psinfo_t elfcore_psinfo_t
;
6445 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6446 typedef psinfo32_t elfcore_psinfo32_t
;
6450 /* return a malloc'ed copy of a string at START which is at
6451 most MAX bytes long, possibly without a terminating '\0'.
6452 the copy will always have a terminating '\0'. */
6455 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
6458 char *end
= memchr (start
, '\0', max
);
6466 dups
= bfd_alloc (abfd
, len
+ 1);
6470 memcpy (dups
, start
, len
);
6476 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6478 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
6480 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6482 elfcore_psinfo_t psinfo
;
6484 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6486 elf_tdata (abfd
)->core_program
6487 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6488 sizeof (psinfo
.pr_fname
));
6490 elf_tdata (abfd
)->core_command
6491 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6492 sizeof (psinfo
.pr_psargs
));
6494 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6495 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6497 /* 64-bit host, 32-bit corefile */
6498 elfcore_psinfo32_t psinfo
;
6500 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6502 elf_tdata (abfd
)->core_program
6503 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6504 sizeof (psinfo
.pr_fname
));
6506 elf_tdata (abfd
)->core_command
6507 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6508 sizeof (psinfo
.pr_psargs
));
6514 /* Fail - we don't know how to handle any other
6515 note size (ie. data object type). */
6519 /* Note that for some reason, a spurious space is tacked
6520 onto the end of the args in some (at least one anyway)
6521 implementations, so strip it off if it exists. */
6524 char *command
= elf_tdata (abfd
)->core_command
;
6525 int n
= strlen (command
);
6527 if (0 < n
&& command
[n
- 1] == ' ')
6528 command
[n
- 1] = '\0';
6533 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6535 #if defined (HAVE_PSTATUS_T)
6537 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6539 if (note
->descsz
== sizeof (pstatus_t
)
6540 #if defined (HAVE_PXSTATUS_T)
6541 || note
->descsz
== sizeof (pxstatus_t
)
6547 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6549 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6551 #if defined (HAVE_PSTATUS32_T)
6552 else if (note
->descsz
== sizeof (pstatus32_t
))
6554 /* 64-bit host, 32-bit corefile */
6557 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6559 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6562 /* Could grab some more details from the "representative"
6563 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6564 NT_LWPSTATUS note, presumably. */
6568 #endif /* defined (HAVE_PSTATUS_T) */
6570 #if defined (HAVE_LWPSTATUS_T)
6572 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6574 lwpstatus_t lwpstat
;
6580 if (note
->descsz
!= sizeof (lwpstat
)
6581 #if defined (HAVE_LWPXSTATUS_T)
6582 && note
->descsz
!= sizeof (lwpxstatus_t
)
6587 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6589 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6590 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6592 /* Make a ".reg/999" section. */
6594 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6595 len
= strlen (buf
) + 1;
6596 name
= bfd_alloc (abfd
, len
);
6599 memcpy (name
, buf
, len
);
6601 sect
= bfd_make_section (abfd
, name
);
6605 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6606 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6607 sect
->filepos
= note
->descpos
6608 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6611 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6612 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6613 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6616 sect
->flags
= SEC_HAS_CONTENTS
;
6617 sect
->alignment_power
= 2;
6619 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6622 /* Make a ".reg2/999" section */
6624 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6625 len
= strlen (buf
) + 1;
6626 name
= bfd_alloc (abfd
, len
);
6629 memcpy (name
, buf
, len
);
6631 sect
= bfd_make_section (abfd
, name
);
6635 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6636 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6637 sect
->filepos
= note
->descpos
6638 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6641 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6642 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6643 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6646 sect
->flags
= SEC_HAS_CONTENTS
;
6647 sect
->alignment_power
= 2;
6649 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6651 #endif /* defined (HAVE_LWPSTATUS_T) */
6653 #if defined (HAVE_WIN32_PSTATUS_T)
6655 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6661 win32_pstatus_t pstatus
;
6663 if (note
->descsz
< sizeof (pstatus
))
6666 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
6668 switch (pstatus
.data_type
)
6670 case NOTE_INFO_PROCESS
:
6671 /* FIXME: need to add ->core_command. */
6672 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6673 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6676 case NOTE_INFO_THREAD
:
6677 /* Make a ".reg/999" section. */
6678 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6680 len
= strlen (buf
) + 1;
6681 name
= bfd_alloc (abfd
, len
);
6685 memcpy (name
, buf
, len
);
6687 sect
= bfd_make_section (abfd
, name
);
6691 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6692 sect
->filepos
= (note
->descpos
6693 + offsetof (struct win32_pstatus
,
6694 data
.thread_info
.thread_context
));
6695 sect
->flags
= SEC_HAS_CONTENTS
;
6696 sect
->alignment_power
= 2;
6698 if (pstatus
.data
.thread_info
.is_active_thread
)
6699 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6703 case NOTE_INFO_MODULE
:
6704 /* Make a ".module/xxxxxxxx" section. */
6705 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6707 len
= strlen (buf
) + 1;
6708 name
= bfd_alloc (abfd
, len
);
6712 memcpy (name
, buf
, len
);
6714 sect
= bfd_make_section (abfd
, name
);
6719 sect
->_raw_size
= note
->descsz
;
6720 sect
->filepos
= note
->descpos
;
6721 sect
->flags
= SEC_HAS_CONTENTS
;
6722 sect
->alignment_power
= 2;
6731 #endif /* HAVE_WIN32_PSTATUS_T */
6734 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
6736 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6744 if (bed
->elf_backend_grok_prstatus
)
6745 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6747 #if defined (HAVE_PRSTATUS_T)
6748 return elfcore_grok_prstatus (abfd
, note
);
6753 #if defined (HAVE_PSTATUS_T)
6755 return elfcore_grok_pstatus (abfd
, note
);
6758 #if defined (HAVE_LWPSTATUS_T)
6760 return elfcore_grok_lwpstatus (abfd
, note
);
6763 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6764 return elfcore_grok_prfpreg (abfd
, note
);
6766 #if defined (HAVE_WIN32_PSTATUS_T)
6767 case NT_WIN32PSTATUS
:
6768 return elfcore_grok_win32pstatus (abfd
, note
);
6771 case NT_PRXFPREG
: /* Linux SSE extension */
6772 if (note
->namesz
== 6
6773 && strcmp (note
->namedata
, "LINUX") == 0)
6774 return elfcore_grok_prxfpreg (abfd
, note
);
6780 if (bed
->elf_backend_grok_psinfo
)
6781 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6783 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6784 return elfcore_grok_psinfo (abfd
, note
);
6791 asection
*sect
= bfd_make_section (abfd
, ".auxv");
6795 sect
->_raw_size
= note
->descsz
;
6796 sect
->filepos
= note
->descpos
;
6797 sect
->flags
= SEC_HAS_CONTENTS
;
6798 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
6806 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
6810 cp
= strchr (note
->namedata
, '@');
6813 *lwpidp
= atoi(cp
+ 1);
6820 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
6823 /* Signal number at offset 0x08. */
6824 elf_tdata (abfd
)->core_signal
6825 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6827 /* Process ID at offset 0x50. */
6828 elf_tdata (abfd
)->core_pid
6829 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6831 /* Command name at 0x7c (max 32 bytes, including nul). */
6832 elf_tdata (abfd
)->core_command
6833 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6839 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
6843 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6844 elf_tdata (abfd
)->core_lwpid
= lwp
;
6846 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6848 /* NetBSD-specific core "procinfo". Note that we expect to
6849 find this note before any of the others, which is fine,
6850 since the kernel writes this note out first when it
6851 creates a core file. */
6853 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6856 /* As of Jan 2002 there are no other machine-independent notes
6857 defined for NetBSD core files. If the note type is less
6858 than the start of the machine-dependent note types, we don't
6861 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
6865 switch (bfd_get_arch (abfd
))
6867 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6868 PT_GETFPREGS == mach+2. */
6870 case bfd_arch_alpha
:
6871 case bfd_arch_sparc
:
6874 case NT_NETBSDCORE_FIRSTMACH
+0:
6875 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6877 case NT_NETBSDCORE_FIRSTMACH
+2:
6878 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6884 /* On all other arch's, PT_GETREGS == mach+1 and
6885 PT_GETFPREGS == mach+3. */
6890 case NT_NETBSDCORE_FIRSTMACH
+1:
6891 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6893 case NT_NETBSDCORE_FIRSTMACH
+3:
6894 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6904 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, pid_t
*tid
)
6906 void *ddata
= note
->descdata
;
6913 /* nto_procfs_status 'pid' field is at offset 0. */
6914 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
6916 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
6917 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
6919 /* nto_procfs_status 'flags' field is at offset 8. */
6920 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
6922 /* nto_procfs_status 'what' field is at offset 14. */
6923 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
6925 elf_tdata (abfd
)->core_signal
= sig
;
6926 elf_tdata (abfd
)->core_lwpid
= *tid
;
6929 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
6930 do not come from signals so we make sure we set the current
6931 thread just in case. */
6932 if (flags
& 0x00000080)
6933 elf_tdata (abfd
)->core_lwpid
= *tid
;
6935 /* Make a ".qnx_core_status/%d" section. */
6936 sprintf (buf
, ".qnx_core_status/%d", *tid
);
6938 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
6943 sect
= bfd_make_section (abfd
, name
);
6947 sect
->_raw_size
= note
->descsz
;
6948 sect
->filepos
= note
->descpos
;
6949 sect
->flags
= SEC_HAS_CONTENTS
;
6950 sect
->alignment_power
= 2;
6952 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
6956 elfcore_grok_nto_gregs (bfd
*abfd
, Elf_Internal_Note
*note
, pid_t tid
)
6962 /* Make a ".reg/%d" section. */
6963 sprintf (buf
, ".reg/%d", tid
);
6965 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
6970 sect
= bfd_make_section (abfd
, name
);
6974 sect
->_raw_size
= note
->descsz
;
6975 sect
->filepos
= note
->descpos
;
6976 sect
->flags
= SEC_HAS_CONTENTS
;
6977 sect
->alignment_power
= 2;
6979 /* This is the current thread. */
6980 if (elf_tdata (abfd
)->core_lwpid
== tid
)
6981 return elfcore_maybe_make_sect (abfd
, ".reg", sect
);
6986 #define BFD_QNT_CORE_INFO 7
6987 #define BFD_QNT_CORE_STATUS 8
6988 #define BFD_QNT_CORE_GREG 9
6989 #define BFD_QNT_CORE_FPREG 10
6992 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
6994 /* Every GREG section has a STATUS section before it. Store the
6995 tid from the previous call to pass down to the next gregs
6997 static pid_t tid
= 1;
7001 case BFD_QNT_CORE_INFO
: return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
7002 case BFD_QNT_CORE_STATUS
: return elfcore_grok_nto_status (abfd
, note
, &tid
);
7003 case BFD_QNT_CORE_GREG
: return elfcore_grok_nto_gregs (abfd
, note
, tid
);
7004 case BFD_QNT_CORE_FPREG
: return elfcore_grok_prfpreg (abfd
, note
);
7005 default: return TRUE
;
7009 /* Function: elfcore_write_note
7016 size of data for note
7019 End of buffer containing note. */
7022 elfcore_write_note (bfd
*abfd
,
7030 Elf_External_Note
*xnp
;
7040 const struct elf_backend_data
*bed
;
7042 namesz
= strlen (name
) + 1;
7043 bed
= get_elf_backend_data (abfd
);
7044 pad
= -namesz
& ((1 << bed
->s
->log_file_align
) - 1);
7047 newspace
= 12 + namesz
+ pad
+ size
;
7049 p
= realloc (buf
, *bufsiz
+ newspace
);
7051 *bufsiz
+= newspace
;
7052 xnp
= (Elf_External_Note
*) dest
;
7053 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
7054 H_PUT_32 (abfd
, size
, xnp
->descsz
);
7055 H_PUT_32 (abfd
, type
, xnp
->type
);
7059 memcpy (dest
, name
, namesz
);
7067 memcpy (dest
, input
, size
);
7071 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7073 elfcore_write_prpsinfo (bfd
*abfd
,
7080 char *note_name
= "CORE";
7082 #if defined (HAVE_PSINFO_T)
7084 note_type
= NT_PSINFO
;
7087 note_type
= NT_PRPSINFO
;
7090 memset (&data
, 0, sizeof (data
));
7091 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
7092 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
7093 return elfcore_write_note (abfd
, buf
, bufsiz
,
7094 note_name
, note_type
, &data
, sizeof (data
));
7096 #endif /* PSINFO_T or PRPSINFO_T */
7098 #if defined (HAVE_PRSTATUS_T)
7100 elfcore_write_prstatus (bfd
*abfd
,
7108 char *note_name
= "CORE";
7110 memset (&prstat
, 0, sizeof (prstat
));
7111 prstat
.pr_pid
= pid
;
7112 prstat
.pr_cursig
= cursig
;
7113 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
7114 return elfcore_write_note (abfd
, buf
, bufsiz
,
7115 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
7117 #endif /* HAVE_PRSTATUS_T */
7119 #if defined (HAVE_LWPSTATUS_T)
7121 elfcore_write_lwpstatus (bfd
*abfd
,
7128 lwpstatus_t lwpstat
;
7129 char *note_name
= "CORE";
7131 memset (&lwpstat
, 0, sizeof (lwpstat
));
7132 lwpstat
.pr_lwpid
= pid
>> 16;
7133 lwpstat
.pr_cursig
= cursig
;
7134 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7135 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
7136 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7138 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
7139 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
7141 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
7142 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
7145 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7146 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
7148 #endif /* HAVE_LWPSTATUS_T */
7150 #if defined (HAVE_PSTATUS_T)
7152 elfcore_write_pstatus (bfd
*abfd
,
7160 char *note_name
= "CORE";
7162 memset (&pstat
, 0, sizeof (pstat
));
7163 pstat
.pr_pid
= pid
& 0xffff;
7164 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7165 NT_PSTATUS
, &pstat
, sizeof (pstat
));
7168 #endif /* HAVE_PSTATUS_T */
7171 elfcore_write_prfpreg (bfd
*abfd
,
7177 char *note_name
= "CORE";
7178 return elfcore_write_note (abfd
, buf
, bufsiz
,
7179 note_name
, NT_FPREGSET
, fpregs
, size
);
7183 elfcore_write_prxfpreg (bfd
*abfd
,
7186 const void *xfpregs
,
7189 char *note_name
= "LINUX";
7190 return elfcore_write_note (abfd
, buf
, bufsiz
,
7191 note_name
, NT_PRXFPREG
, xfpregs
, size
);
7195 elfcore_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
7203 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
7206 buf
= bfd_malloc (size
);
7210 if (bfd_bread (buf
, size
, abfd
) != size
)
7218 while (p
< buf
+ size
)
7220 /* FIXME: bad alignment assumption. */
7221 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
7222 Elf_Internal_Note in
;
7224 in
.type
= H_GET_32 (abfd
, xnp
->type
);
7226 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
7227 in
.namedata
= xnp
->name
;
7229 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
7230 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
7231 in
.descpos
= offset
+ (in
.descdata
- buf
);
7233 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
7235 if (! elfcore_grok_netbsd_note (abfd
, &in
))
7238 else if (strncmp (in
.namedata
, "QNX", 3) == 0)
7240 if (! elfcore_grok_nto_note (abfd
, &in
))
7245 if (! elfcore_grok_note (abfd
, &in
))
7249 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
7256 /* Providing external access to the ELF program header table. */
7258 /* Return an upper bound on the number of bytes required to store a
7259 copy of ABFD's program header table entries. Return -1 if an error
7260 occurs; bfd_get_error will return an appropriate code. */
7263 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
7265 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7267 bfd_set_error (bfd_error_wrong_format
);
7271 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
7274 /* Copy ABFD's program header table entries to *PHDRS. The entries
7275 will be stored as an array of Elf_Internal_Phdr structures, as
7276 defined in include/elf/internal.h. To find out how large the
7277 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7279 Return the number of program header table entries read, or -1 if an
7280 error occurs; bfd_get_error will return an appropriate code. */
7283 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
7287 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7289 bfd_set_error (bfd_error_wrong_format
);
7293 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
7294 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
7295 num_phdrs
* sizeof (Elf_Internal_Phdr
));
7301 _bfd_elf_sprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, char *buf
, bfd_vma value
)
7304 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7306 i_ehdrp
= elf_elfheader (abfd
);
7307 if (i_ehdrp
== NULL
)
7308 sprintf_vma (buf
, value
);
7311 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7313 #if BFD_HOST_64BIT_LONG
7314 sprintf (buf
, "%016lx", value
);
7316 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
7317 _bfd_int64_low (value
));
7321 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
7324 sprintf_vma (buf
, value
);
7329 _bfd_elf_fprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, void *stream
, bfd_vma value
)
7332 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7334 i_ehdrp
= elf_elfheader (abfd
);
7335 if (i_ehdrp
== NULL
)
7336 fprintf_vma ((FILE *) stream
, value
);
7339 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7341 #if BFD_HOST_64BIT_LONG
7342 fprintf ((FILE *) stream
, "%016lx", value
);
7344 fprintf ((FILE *) stream
, "%08lx%08lx",
7345 _bfd_int64_high (value
), _bfd_int64_low (value
));
7349 fprintf ((FILE *) stream
, "%08lx",
7350 (unsigned long) (value
& 0xffffffff));
7353 fprintf_vma ((FILE *) stream
, value
);
7357 enum elf_reloc_type_class
7358 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
7360 return reloc_class_normal
;
7363 /* For RELA architectures, return the relocation value for a
7364 relocation against a local symbol. */
7367 _bfd_elf_rela_local_sym (bfd
*abfd
,
7368 Elf_Internal_Sym
*sym
,
7370 Elf_Internal_Rela
*rel
)
7374 relocation
= (sec
->output_section
->vma
7375 + sec
->output_offset
7377 if ((sec
->flags
& SEC_MERGE
)
7378 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
7379 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
7385 _bfd_merged_section_offset (abfd
, &msec
,
7386 elf_section_data (sec
)->sec_info
,
7387 sym
->st_value
+ rel
->r_addend
,
7390 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
7396 _bfd_elf_rel_local_sym (bfd
*abfd
,
7397 Elf_Internal_Sym
*sym
,
7401 asection
*sec
= *psec
;
7403 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7404 return sym
->st_value
+ addend
;
7406 return _bfd_merged_section_offset (abfd
, psec
,
7407 elf_section_data (sec
)->sec_info
,
7408 sym
->st_value
+ addend
, 0);
7412 _bfd_elf_section_offset (bfd
*abfd
,
7413 struct bfd_link_info
*info
,
7417 struct bfd_elf_section_data
*sec_data
;
7419 sec_data
= elf_section_data (sec
);
7420 switch (sec
->sec_info_type
)
7422 case ELF_INFO_TYPE_STABS
:
7423 return _bfd_stab_section_offset (abfd
,
7424 &elf_hash_table (info
)->merge_info
,
7425 sec
, &sec_data
->sec_info
, offset
);
7426 case ELF_INFO_TYPE_EH_FRAME
:
7427 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);
7433 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7434 reconstruct an ELF file by reading the segments out of remote memory
7435 based on the ELF file header at EHDR_VMA and the ELF program headers it
7436 points to. If not null, *LOADBASEP is filled in with the difference
7437 between the VMAs from which the segments were read, and the VMAs the
7438 file headers (and hence BFD's idea of each section's VMA) put them at.
7440 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7441 remote memory at target address VMA into the local buffer at MYADDR; it
7442 should return zero on success or an `errno' code on failure. TEMPL must
7443 be a BFD for an ELF target with the word size and byte order found in
7444 the remote memory. */
7447 bfd_elf_bfd_from_remote_memory
7451 int (*target_read_memory
) (bfd_vma
, char *, int))
7453 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
7454 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);