1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
32 haven't bothered yet. */
34 /* For sparc64-cross-sparc32. */
42 #include "libiberty.h"
44 static int elf_sort_sections (const void *, const void *);
45 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
46 static bfd_boolean
prep_headers (bfd
*);
47 static bfd_boolean
swap_out_syms (bfd
*, struct bfd_strtab_hash
**, int) ;
48 static bfd_boolean
elfcore_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
50 /* Swap version information in and out. The version information is
51 currently size independent. If that ever changes, this code will
52 need to move into elfcode.h. */
54 /* Swap in a Verdef structure. */
57 _bfd_elf_swap_verdef_in (bfd
*abfd
,
58 const Elf_External_Verdef
*src
,
59 Elf_Internal_Verdef
*dst
)
61 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
62 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
63 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
64 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
65 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
66 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
67 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
70 /* Swap out a Verdef structure. */
73 _bfd_elf_swap_verdef_out (bfd
*abfd
,
74 const Elf_Internal_Verdef
*src
,
75 Elf_External_Verdef
*dst
)
77 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
78 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
79 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
80 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
81 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
82 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
83 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
86 /* Swap in a Verdaux structure. */
89 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
90 const Elf_External_Verdaux
*src
,
91 Elf_Internal_Verdaux
*dst
)
93 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
94 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
97 /* Swap out a Verdaux structure. */
100 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
101 const Elf_Internal_Verdaux
*src
,
102 Elf_External_Verdaux
*dst
)
104 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
105 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
108 /* Swap in a Verneed structure. */
111 _bfd_elf_swap_verneed_in (bfd
*abfd
,
112 const Elf_External_Verneed
*src
,
113 Elf_Internal_Verneed
*dst
)
115 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
116 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
117 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
118 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
119 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
122 /* Swap out a Verneed structure. */
125 _bfd_elf_swap_verneed_out (bfd
*abfd
,
126 const Elf_Internal_Verneed
*src
,
127 Elf_External_Verneed
*dst
)
129 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
130 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
131 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
132 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
133 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
136 /* Swap in a Vernaux structure. */
139 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
140 const Elf_External_Vernaux
*src
,
141 Elf_Internal_Vernaux
*dst
)
143 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
144 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
145 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
146 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
147 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
150 /* Swap out a Vernaux structure. */
153 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
154 const Elf_Internal_Vernaux
*src
,
155 Elf_External_Vernaux
*dst
)
157 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
158 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
159 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
160 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
161 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
164 /* Swap in a Versym structure. */
167 _bfd_elf_swap_versym_in (bfd
*abfd
,
168 const Elf_External_Versym
*src
,
169 Elf_Internal_Versym
*dst
)
171 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
174 /* Swap out a Versym structure. */
177 _bfd_elf_swap_versym_out (bfd
*abfd
,
178 const Elf_Internal_Versym
*src
,
179 Elf_External_Versym
*dst
)
181 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
184 /* Standard ELF hash function. Do not change this function; you will
185 cause invalid hash tables to be generated. */
188 bfd_elf_hash (const char *namearg
)
190 const unsigned char *name
= (const unsigned char *) namearg
;
195 while ((ch
= *name
++) != '\0')
198 if ((g
= (h
& 0xf0000000)) != 0)
201 /* The ELF ABI says `h &= ~g', but this is equivalent in
202 this case and on some machines one insn instead of two. */
206 return h
& 0xffffffff;
209 /* Read a specified number of bytes at a specified offset in an ELF
210 file, into a newly allocated buffer, and return a pointer to the
214 elf_read (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
218 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
220 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
222 if (bfd_bread (buf
, size
, abfd
) != size
)
224 if (bfd_get_error () != bfd_error_system_call
)
225 bfd_set_error (bfd_error_file_truncated
);
232 bfd_elf_mkobject (bfd
*abfd
)
234 /* This just does initialization. */
235 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
236 elf_tdata (abfd
) = bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
237 if (elf_tdata (abfd
) == 0)
239 /* Since everything is done at close time, do we need any
246 bfd_elf_mkcorefile (bfd
*abfd
)
248 /* I think this can be done just like an object file. */
249 return bfd_elf_mkobject (abfd
);
253 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
255 Elf_Internal_Shdr
**i_shdrp
;
256 char *shstrtab
= NULL
;
258 bfd_size_type shstrtabsize
;
260 i_shdrp
= elf_elfsections (abfd
);
261 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
264 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
265 if (shstrtab
== NULL
)
267 /* No cached one, attempt to read, and cache what we read. */
268 offset
= i_shdrp
[shindex
]->sh_offset
;
269 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
270 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
271 i_shdrp
[shindex
]->contents
= shstrtab
;
277 bfd_elf_string_from_elf_section (bfd
*abfd
,
278 unsigned int shindex
,
279 unsigned int strindex
)
281 Elf_Internal_Shdr
*hdr
;
286 hdr
= elf_elfsections (abfd
)[shindex
];
288 if (hdr
->contents
== NULL
289 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
292 if (strindex
>= hdr
->sh_size
)
294 (*_bfd_error_handler
)
295 (_("%s: invalid string offset %u >= %lu for section `%s'"),
296 bfd_archive_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
297 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
298 && strindex
== hdr
->sh_name
)
300 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
304 return ((char *) hdr
->contents
) + strindex
;
307 /* Read and convert symbols to internal format.
308 SYMCOUNT specifies the number of symbols to read, starting from
309 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
310 are non-NULL, they are used to store the internal symbols, external
311 symbols, and symbol section index extensions, respectively. */
314 bfd_elf_get_elf_syms (bfd
*ibfd
,
315 Elf_Internal_Shdr
*symtab_hdr
,
318 Elf_Internal_Sym
*intsym_buf
,
320 Elf_External_Sym_Shndx
*extshndx_buf
)
322 Elf_Internal_Shdr
*shndx_hdr
;
324 const bfd_byte
*esym
;
325 Elf_External_Sym_Shndx
*alloc_extshndx
;
326 Elf_External_Sym_Shndx
*shndx
;
327 Elf_Internal_Sym
*isym
;
328 Elf_Internal_Sym
*isymend
;
329 const struct elf_backend_data
*bed
;
337 /* Normal syms might have section extension entries. */
339 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
340 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
342 /* Read the symbols. */
344 alloc_extshndx
= NULL
;
345 bed
= get_elf_backend_data (ibfd
);
346 extsym_size
= bed
->s
->sizeof_sym
;
347 amt
= symcount
* extsym_size
;
348 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
349 if (extsym_buf
== NULL
)
351 alloc_ext
= bfd_malloc (amt
);
352 extsym_buf
= alloc_ext
;
354 if (extsym_buf
== NULL
355 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
356 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
362 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
366 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
367 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
368 if (extshndx_buf
== NULL
)
370 alloc_extshndx
= bfd_malloc (amt
);
371 extshndx_buf
= alloc_extshndx
;
373 if (extshndx_buf
== NULL
374 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
375 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
382 if (intsym_buf
== NULL
)
384 bfd_size_type amt
= symcount
* sizeof (Elf_Internal_Sym
);
385 intsym_buf
= bfd_malloc (amt
);
386 if (intsym_buf
== NULL
)
390 /* Convert the symbols to internal form. */
391 isymend
= intsym_buf
+ symcount
;
392 for (esym
= extsym_buf
, isym
= intsym_buf
, shndx
= extshndx_buf
;
394 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
395 (*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
);
398 if (alloc_ext
!= NULL
)
400 if (alloc_extshndx
!= NULL
)
401 free (alloc_extshndx
);
406 /* Look up a symbol name. */
408 bfd_elf_local_sym_name (bfd
*abfd
, Elf_Internal_Sym
*isym
)
410 unsigned int iname
= isym
->st_name
;
411 unsigned int shindex
= elf_tdata (abfd
)->symtab_hdr
.sh_link
;
412 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
414 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
415 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
418 return bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
421 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
422 sections. The first element is the flags, the rest are section
425 typedef union elf_internal_group
{
426 Elf_Internal_Shdr
*shdr
;
428 } Elf_Internal_Group
;
430 /* Return the name of the group signature symbol. Why isn't the
431 signature just a string? */
434 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
436 Elf_Internal_Shdr
*hdr
;
437 unsigned char esym
[sizeof (Elf64_External_Sym
)];
438 Elf_External_Sym_Shndx eshndx
;
439 Elf_Internal_Sym isym
;
441 /* First we need to ensure the symbol table is available. */
442 if (! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
445 /* Go read the symbol. */
446 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
447 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
448 &isym
, esym
, &eshndx
) == NULL
)
451 return bfd_elf_local_sym_name (abfd
, &isym
);
454 /* Set next_in_group list pointer, and group name for NEWSECT. */
457 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
459 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
461 /* If num_group is zero, read in all SHT_GROUP sections. The count
462 is set to -1 if there are no SHT_GROUP sections. */
465 unsigned int i
, shnum
;
467 /* First count the number of groups. If we have a SHT_GROUP
468 section with just a flag word (ie. sh_size is 4), ignore it. */
469 shnum
= elf_numsections (abfd
);
471 for (i
= 0; i
< shnum
; i
++)
473 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
474 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
479 num_group
= (unsigned) -1;
480 elf_tdata (abfd
)->num_group
= num_group
;
484 /* We keep a list of elf section headers for group sections,
485 so we can find them quickly. */
486 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
487 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
488 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
492 for (i
= 0; i
< shnum
; i
++)
494 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
495 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
498 Elf_Internal_Group
*dest
;
500 /* Add to list of sections. */
501 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
504 /* Read the raw contents. */
505 BFD_ASSERT (sizeof (*dest
) >= 4);
506 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
507 shdr
->contents
= bfd_alloc (abfd
, amt
);
508 if (shdr
->contents
== NULL
509 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
510 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
514 /* Translate raw contents, a flag word followed by an
515 array of elf section indices all in target byte order,
516 to the flag word followed by an array of elf section
518 src
= shdr
->contents
+ shdr
->sh_size
;
519 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
526 idx
= H_GET_32 (abfd
, src
);
527 if (src
== shdr
->contents
)
530 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
531 shdr
->bfd_section
->flags
532 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
537 ((*_bfd_error_handler
)
538 (_("%s: invalid SHT_GROUP entry"),
539 bfd_archive_filename (abfd
)));
542 dest
->shdr
= elf_elfsections (abfd
)[idx
];
549 if (num_group
!= (unsigned) -1)
553 for (i
= 0; i
< num_group
; i
++)
555 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
556 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
557 unsigned int n_elt
= shdr
->sh_size
/ 4;
559 /* Look through this group's sections to see if current
560 section is a member. */
562 if ((++idx
)->shdr
== hdr
)
566 /* We are a member of this group. Go looking through
567 other members to see if any others are linked via
569 idx
= (Elf_Internal_Group
*) shdr
->contents
;
570 n_elt
= shdr
->sh_size
/ 4;
572 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
573 && elf_next_in_group (s
) != NULL
)
577 /* Snarf the group name from other member, and
578 insert current section in circular list. */
579 elf_group_name (newsect
) = elf_group_name (s
);
580 elf_next_in_group (newsect
) = elf_next_in_group (s
);
581 elf_next_in_group (s
) = newsect
;
587 gname
= group_signature (abfd
, shdr
);
590 elf_group_name (newsect
) = gname
;
592 /* Start a circular list with one element. */
593 elf_next_in_group (newsect
) = newsect
;
596 /* If the group section has been created, point to the
598 if (shdr
->bfd_section
!= NULL
)
599 elf_next_in_group (shdr
->bfd_section
) = newsect
;
607 if (elf_group_name (newsect
) == NULL
)
609 (*_bfd_error_handler
) (_("%s: no group info for section %s"),
610 bfd_archive_filename (abfd
), newsect
->name
);
616 bfd_elf_discard_group (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*group
)
618 asection
*first
= elf_next_in_group (group
);
623 s
->output_section
= bfd_abs_section_ptr
;
624 s
= elf_next_in_group (s
);
625 /* These lists are circular. */
632 /* Make a BFD section from an ELF section. We store a pointer to the
633 BFD section in the bfd_section field of the header. */
636 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
637 Elf_Internal_Shdr
*hdr
,
642 const struct elf_backend_data
*bed
;
644 if (hdr
->bfd_section
!= NULL
)
646 BFD_ASSERT (strcmp (name
,
647 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
651 newsect
= bfd_make_section_anyway (abfd
, name
);
655 /* Always use the real type/flags. */
656 elf_section_type (newsect
) = hdr
->sh_type
;
657 elf_section_flags (newsect
) = hdr
->sh_flags
;
659 newsect
->filepos
= hdr
->sh_offset
;
661 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
662 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
663 || ! bfd_set_section_alignment (abfd
, newsect
,
664 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
667 flags
= SEC_NO_FLAGS
;
668 if (hdr
->sh_type
!= SHT_NOBITS
)
669 flags
|= SEC_HAS_CONTENTS
;
670 if (hdr
->sh_type
== SHT_GROUP
)
671 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
672 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
675 if (hdr
->sh_type
!= SHT_NOBITS
)
678 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
679 flags
|= SEC_READONLY
;
680 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
682 else if ((flags
& SEC_LOAD
) != 0)
684 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
687 newsect
->entsize
= hdr
->sh_entsize
;
688 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
689 flags
|= SEC_STRINGS
;
691 if (hdr
->sh_flags
& SHF_GROUP
)
692 if (!setup_group (abfd
, hdr
, newsect
))
694 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
695 flags
|= SEC_THREAD_LOCAL
;
697 /* The debugging sections appear to be recognized only by name, not
700 static const char *debug_sec_names
[] =
709 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
710 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
714 flags
|= SEC_DEBUGGING
;
717 /* As a GNU extension, if the name begins with .gnu.linkonce, we
718 only link a single copy of the section. This is used to support
719 g++. g++ will emit each template expansion in its own section.
720 The symbols will be defined as weak, so that multiple definitions
721 are permitted. The GNU linker extension is to actually discard
722 all but one of the sections. */
723 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
724 && elf_next_in_group (newsect
) == NULL
)
725 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
727 bed
= get_elf_backend_data (abfd
);
728 if (bed
->elf_backend_section_flags
)
729 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
732 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
735 if ((flags
& SEC_ALLOC
) != 0)
737 Elf_Internal_Phdr
*phdr
;
740 /* Look through the phdrs to see if we need to adjust the lma.
741 If all the p_paddr fields are zero, we ignore them, since
742 some ELF linkers produce such output. */
743 phdr
= elf_tdata (abfd
)->phdr
;
744 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
746 if (phdr
->p_paddr
!= 0)
749 if (i
< elf_elfheader (abfd
)->e_phnum
)
751 phdr
= elf_tdata (abfd
)->phdr
;
752 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
754 /* This section is part of this segment if its file
755 offset plus size lies within the segment's memory
756 span and, if the section is loaded, the extent of the
757 loaded data lies within the extent of the segment.
759 Note - we used to check the p_paddr field as well, and
760 refuse to set the LMA if it was 0. This is wrong
761 though, as a perfectly valid initialised segment can
762 have a p_paddr of zero. Some architectures, eg ARM,
763 place special significance on the address 0 and
764 executables need to be able to have a segment which
765 covers this address. */
766 if (phdr
->p_type
== PT_LOAD
767 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
768 && (hdr
->sh_offset
+ hdr
->sh_size
769 <= phdr
->p_offset
+ phdr
->p_memsz
)
770 && ((flags
& SEC_LOAD
) == 0
771 || (hdr
->sh_offset
+ hdr
->sh_size
772 <= phdr
->p_offset
+ phdr
->p_filesz
)))
774 if ((flags
& SEC_LOAD
) == 0)
775 newsect
->lma
= (phdr
->p_paddr
776 + hdr
->sh_addr
- phdr
->p_vaddr
);
778 /* We used to use the same adjustment for SEC_LOAD
779 sections, but that doesn't work if the segment
780 is packed with code from multiple VMAs.
781 Instead we calculate the section LMA based on
782 the segment LMA. It is assumed that the
783 segment will contain sections with contiguous
784 LMAs, even if the VMAs are not. */
785 newsect
->lma
= (phdr
->p_paddr
786 + hdr
->sh_offset
- phdr
->p_offset
);
788 /* With contiguous segments, we can't tell from file
789 offsets whether a section with zero size should
790 be placed at the end of one segment or the
791 beginning of the next. Decide based on vaddr. */
792 if (hdr
->sh_addr
>= phdr
->p_vaddr
793 && (hdr
->sh_addr
+ hdr
->sh_size
794 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
801 hdr
->bfd_section
= newsect
;
802 elf_section_data (newsect
)->this_hdr
= *hdr
;
812 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
815 Helper functions for GDB to locate the string tables.
816 Since BFD hides string tables from callers, GDB needs to use an
817 internal hook to find them. Sun's .stabstr, in particular,
818 isn't even pointed to by the .stab section, so ordinary
819 mechanisms wouldn't work to find it, even if we had some.
822 struct elf_internal_shdr
*
823 bfd_elf_find_section (bfd
*abfd
, char *name
)
825 Elf_Internal_Shdr
**i_shdrp
;
830 i_shdrp
= elf_elfsections (abfd
);
833 shstrtab
= bfd_elf_get_str_section (abfd
,
834 elf_elfheader (abfd
)->e_shstrndx
);
835 if (shstrtab
!= NULL
)
837 max
= elf_numsections (abfd
);
838 for (i
= 1; i
< max
; i
++)
839 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
846 const char *const bfd_elf_section_type_names
[] = {
847 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
848 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
849 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
852 /* ELF relocs are against symbols. If we are producing relocatable
853 output, and the reloc is against an external symbol, and nothing
854 has given us any additional addend, the resulting reloc will also
855 be against the same symbol. In such a case, we don't want to
856 change anything about the way the reloc is handled, since it will
857 all be done at final link time. Rather than put special case code
858 into bfd_perform_relocation, all the reloc types use this howto
859 function. It just short circuits the reloc if producing
860 relocatable output against an external symbol. */
862 bfd_reloc_status_type
863 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
864 arelent
*reloc_entry
,
866 void *data ATTRIBUTE_UNUSED
,
867 asection
*input_section
,
869 char **error_message ATTRIBUTE_UNUSED
)
871 if (output_bfd
!= NULL
872 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
873 && (! reloc_entry
->howto
->partial_inplace
874 || reloc_entry
->addend
== 0))
876 reloc_entry
->address
+= input_section
->output_offset
;
880 return bfd_reloc_continue
;
883 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
886 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
889 BFD_ASSERT (sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
890 sec
->sec_info_type
= ELF_INFO_TYPE_NONE
;
893 /* Finish SHF_MERGE section merging. */
896 _bfd_elf_merge_sections (bfd
*abfd
, struct bfd_link_info
*info
)
898 if (!is_elf_hash_table (info
->hash
))
900 if (elf_hash_table (info
)->merge_info
)
901 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
,
902 merge_sections_remove_hook
);
907 _bfd_elf_link_just_syms (asection
*sec
, struct bfd_link_info
*info
)
909 sec
->output_section
= bfd_abs_section_ptr
;
910 sec
->output_offset
= sec
->vma
;
911 if (!is_elf_hash_table (info
->hash
))
914 sec
->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
917 /* Copy the program header and other data from one object module to
921 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
923 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
924 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
927 BFD_ASSERT (!elf_flags_init (obfd
)
928 || (elf_elfheader (obfd
)->e_flags
929 == elf_elfheader (ibfd
)->e_flags
));
931 elf_gp (obfd
) = elf_gp (ibfd
);
932 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
933 elf_flags_init (obfd
) = TRUE
;
937 /* Print out the program headers. */
940 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
943 Elf_Internal_Phdr
*p
;
945 bfd_byte
*dynbuf
= NULL
;
947 p
= elf_tdata (abfd
)->phdr
;
952 fprintf (f
, _("\nProgram Header:\n"));
953 c
= elf_elfheader (abfd
)->e_phnum
;
954 for (i
= 0; i
< c
; i
++, p
++)
961 case PT_NULL
: pt
= "NULL"; break;
962 case PT_LOAD
: pt
= "LOAD"; break;
963 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
964 case PT_INTERP
: pt
= "INTERP"; break;
965 case PT_NOTE
: pt
= "NOTE"; break;
966 case PT_SHLIB
: pt
= "SHLIB"; break;
967 case PT_PHDR
: pt
= "PHDR"; break;
968 case PT_TLS
: pt
= "TLS"; break;
969 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
970 case PT_GNU_STACK
: pt
= "STACK"; break;
971 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
973 fprintf (f
, "%8s off 0x", pt
);
974 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
975 fprintf (f
, " vaddr 0x");
976 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
977 fprintf (f
, " paddr 0x");
978 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
979 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
980 fprintf (f
, " filesz 0x");
981 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
982 fprintf (f
, " memsz 0x");
983 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
984 fprintf (f
, " flags %c%c%c",
985 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
986 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
987 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
988 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
989 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
994 s
= bfd_get_section_by_name (abfd
, ".dynamic");
998 unsigned long shlink
;
999 bfd_byte
*extdyn
, *extdynend
;
1001 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1003 fprintf (f
, _("\nDynamic Section:\n"));
1005 dynbuf
= bfd_malloc (s
->_raw_size
);
1008 if (! bfd_get_section_contents (abfd
, s
, dynbuf
, 0, s
->_raw_size
))
1011 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1014 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1016 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1017 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1020 extdynend
= extdyn
+ s
->_raw_size
;
1021 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1023 Elf_Internal_Dyn dyn
;
1026 bfd_boolean stringp
;
1028 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1030 if (dyn
.d_tag
== DT_NULL
)
1037 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1041 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1042 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1043 case DT_PLTGOT
: name
= "PLTGOT"; break;
1044 case DT_HASH
: name
= "HASH"; break;
1045 case DT_STRTAB
: name
= "STRTAB"; break;
1046 case DT_SYMTAB
: name
= "SYMTAB"; break;
1047 case DT_RELA
: name
= "RELA"; break;
1048 case DT_RELASZ
: name
= "RELASZ"; break;
1049 case DT_RELAENT
: name
= "RELAENT"; break;
1050 case DT_STRSZ
: name
= "STRSZ"; break;
1051 case DT_SYMENT
: name
= "SYMENT"; break;
1052 case DT_INIT
: name
= "INIT"; break;
1053 case DT_FINI
: name
= "FINI"; break;
1054 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1055 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1056 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1057 case DT_REL
: name
= "REL"; break;
1058 case DT_RELSZ
: name
= "RELSZ"; break;
1059 case DT_RELENT
: name
= "RELENT"; break;
1060 case DT_PLTREL
: name
= "PLTREL"; break;
1061 case DT_DEBUG
: name
= "DEBUG"; break;
1062 case DT_TEXTREL
: name
= "TEXTREL"; break;
1063 case DT_JMPREL
: name
= "JMPREL"; break;
1064 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1065 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1066 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1067 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1068 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1069 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1070 case DT_FLAGS
: name
= "FLAGS"; break;
1071 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1072 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1073 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1074 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1075 case DT_MOVEENT
: name
= "MOVEENT"; break;
1076 case DT_MOVESZ
: name
= "MOVESZ"; break;
1077 case DT_FEATURE
: name
= "FEATURE"; break;
1078 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1079 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1080 case DT_SYMINENT
: name
= "SYMINENT"; break;
1081 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1082 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1083 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1084 case DT_PLTPAD
: name
= "PLTPAD"; break;
1085 case DT_MOVETAB
: name
= "MOVETAB"; break;
1086 case DT_SYMINFO
: name
= "SYMINFO"; break;
1087 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1088 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1089 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1090 case DT_VERSYM
: name
= "VERSYM"; break;
1091 case DT_VERDEF
: name
= "VERDEF"; break;
1092 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1093 case DT_VERNEED
: name
= "VERNEED"; break;
1094 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1095 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1096 case DT_USED
: name
= "USED"; break;
1097 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1100 fprintf (f
, " %-11s ", name
);
1102 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1106 unsigned int tagv
= dyn
.d_un
.d_val
;
1108 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1111 fprintf (f
, "%s", string
);
1120 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1121 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1123 if (! _bfd_elf_slurp_version_tables (abfd
))
1127 if (elf_dynverdef (abfd
) != 0)
1129 Elf_Internal_Verdef
*t
;
1131 fprintf (f
, _("\nVersion definitions:\n"));
1132 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1134 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1135 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1136 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1138 Elf_Internal_Verdaux
*a
;
1141 for (a
= t
->vd_auxptr
->vda_nextptr
;
1144 fprintf (f
, "%s ", a
->vda_nodename
);
1150 if (elf_dynverref (abfd
) != 0)
1152 Elf_Internal_Verneed
*t
;
1154 fprintf (f
, _("\nVersion References:\n"));
1155 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1157 Elf_Internal_Vernaux
*a
;
1159 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1160 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1161 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1162 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1174 /* Display ELF-specific fields of a symbol. */
1177 bfd_elf_print_symbol (bfd
*abfd
,
1180 bfd_print_symbol_type how
)
1185 case bfd_print_symbol_name
:
1186 fprintf (file
, "%s", symbol
->name
);
1188 case bfd_print_symbol_more
:
1189 fprintf (file
, "elf ");
1190 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1191 fprintf (file
, " %lx", (long) symbol
->flags
);
1193 case bfd_print_symbol_all
:
1195 const char *section_name
;
1196 const char *name
= NULL
;
1197 const struct elf_backend_data
*bed
;
1198 unsigned char st_other
;
1201 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1203 bed
= get_elf_backend_data (abfd
);
1204 if (bed
->elf_backend_print_symbol_all
)
1205 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1209 name
= symbol
->name
;
1210 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1213 fprintf (file
, " %s\t", section_name
);
1214 /* Print the "other" value for a symbol. For common symbols,
1215 we've already printed the size; now print the alignment.
1216 For other symbols, we have no specified alignment, and
1217 we've printed the address; now print the size. */
1218 if (bfd_is_com_section (symbol
->section
))
1219 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1221 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1222 bfd_fprintf_vma (abfd
, file
, val
);
1224 /* If we have version information, print it. */
1225 if (elf_tdata (abfd
)->dynversym_section
!= 0
1226 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1227 || elf_tdata (abfd
)->dynverref_section
!= 0))
1229 unsigned int vernum
;
1230 const char *version_string
;
1232 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1235 version_string
= "";
1236 else if (vernum
== 1)
1237 version_string
= "Base";
1238 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1240 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1243 Elf_Internal_Verneed
*t
;
1245 version_string
= "";
1246 for (t
= elf_tdata (abfd
)->verref
;
1250 Elf_Internal_Vernaux
*a
;
1252 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1254 if (a
->vna_other
== vernum
)
1256 version_string
= a
->vna_nodename
;
1263 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1264 fprintf (file
, " %-11s", version_string
);
1269 fprintf (file
, " (%s)", version_string
);
1270 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1275 /* If the st_other field is not zero, print it. */
1276 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1281 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1282 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1283 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1285 /* Some other non-defined flags are also present, so print
1287 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1290 fprintf (file
, " %s", name
);
1296 /* Create an entry in an ELF linker hash table. */
1298 struct bfd_hash_entry
*
1299 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
1300 struct bfd_hash_table
*table
,
1303 /* Allocate the structure if it has not already been allocated by a
1307 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1312 /* Call the allocation method of the superclass. */
1313 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1316 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1317 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1319 /* Set local fields. */
1322 ret
->dynstr_index
= 0;
1323 ret
->elf_hash_value
= 0;
1324 ret
->weakdef
= NULL
;
1325 ret
->verinfo
.verdef
= NULL
;
1326 ret
->vtable_entries_size
= 0;
1327 ret
->vtable_entries_used
= NULL
;
1328 ret
->vtable_parent
= NULL
;
1329 ret
->got
= htab
->init_refcount
;
1330 ret
->plt
= htab
->init_refcount
;
1332 ret
->type
= STT_NOTYPE
;
1334 /* Assume that we have been called by a non-ELF symbol reader.
1335 This flag is then reset by the code which reads an ELF input
1336 file. This ensures that a symbol created by a non-ELF symbol
1337 reader will have the flag set correctly. */
1338 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1344 /* Copy data from an indirect symbol to its direct symbol, hiding the
1345 old indirect symbol. Also used for copying flags to a weakdef. */
1348 _bfd_elf_link_hash_copy_indirect (const struct elf_backend_data
*bed
,
1349 struct elf_link_hash_entry
*dir
,
1350 struct elf_link_hash_entry
*ind
)
1353 bfd_signed_vma lowest_valid
= bed
->can_refcount
;
1355 /* Copy down any references that we may have already seen to the
1356 symbol which just became indirect. */
1358 dir
->elf_link_hash_flags
1359 |= ind
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_DYNAMIC
1360 | ELF_LINK_HASH_REF_REGULAR
1361 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1362 | ELF_LINK_NON_GOT_REF
1363 | ELF_LINK_HASH_NEEDS_PLT
1364 | ELF_LINK_POINTER_EQUALITY_NEEDED
);
1366 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1369 /* Copy over the global and procedure linkage table refcount entries.
1370 These may have been already set up by a check_relocs routine. */
1371 tmp
= dir
->got
.refcount
;
1372 if (tmp
< lowest_valid
)
1374 dir
->got
.refcount
= ind
->got
.refcount
;
1375 ind
->got
.refcount
= tmp
;
1378 BFD_ASSERT (ind
->got
.refcount
< lowest_valid
);
1380 tmp
= dir
->plt
.refcount
;
1381 if (tmp
< lowest_valid
)
1383 dir
->plt
.refcount
= ind
->plt
.refcount
;
1384 ind
->plt
.refcount
= tmp
;
1387 BFD_ASSERT (ind
->plt
.refcount
< lowest_valid
);
1389 if (dir
->dynindx
== -1)
1391 dir
->dynindx
= ind
->dynindx
;
1392 dir
->dynstr_index
= ind
->dynstr_index
;
1394 ind
->dynstr_index
= 0;
1397 BFD_ASSERT (ind
->dynindx
== -1);
1401 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
1402 struct elf_link_hash_entry
*h
,
1403 bfd_boolean force_local
)
1405 h
->plt
= elf_hash_table (info
)->init_offset
;
1406 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1409 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1410 if (h
->dynindx
!= -1)
1413 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
1419 /* Initialize an ELF linker hash table. */
1422 _bfd_elf_link_hash_table_init
1423 (struct elf_link_hash_table
*table
,
1425 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
1426 struct bfd_hash_table
*,
1431 table
->dynamic_sections_created
= FALSE
;
1432 table
->dynobj
= NULL
;
1433 /* Make sure can_refcount is extended to the width and signedness of
1434 init_refcount before we subtract one from it. */
1435 table
->init_refcount
.refcount
= get_elf_backend_data (abfd
)->can_refcount
;
1436 table
->init_refcount
.refcount
-= 1;
1437 table
->init_offset
.offset
= -(bfd_vma
) 1;
1438 /* The first dynamic symbol is a dummy. */
1439 table
->dynsymcount
= 1;
1440 table
->dynstr
= NULL
;
1441 table
->bucketcount
= 0;
1442 table
->needed
= NULL
;
1444 table
->stab_info
= NULL
;
1445 table
->merge_info
= NULL
;
1446 memset (&table
->eh_info
, 0, sizeof (table
->eh_info
));
1447 table
->dynlocal
= NULL
;
1448 table
->runpath
= NULL
;
1449 table
->tls_sec
= NULL
;
1450 table
->tls_size
= 0;
1451 table
->loaded
= NULL
;
1453 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
1454 table
->root
.type
= bfd_link_elf_hash_table
;
1459 /* Create an ELF linker hash table. */
1461 struct bfd_link_hash_table
*
1462 _bfd_elf_link_hash_table_create (bfd
*abfd
)
1464 struct elf_link_hash_table
*ret
;
1465 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1467 ret
= bfd_malloc (amt
);
1471 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1480 /* This is a hook for the ELF emulation code in the generic linker to
1481 tell the backend linker what file name to use for the DT_NEEDED
1482 entry for a dynamic object. The generic linker passes name as an
1483 empty string to indicate that no DT_NEEDED entry should be made. */
1486 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
1488 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1489 && bfd_get_format (abfd
) == bfd_object
)
1490 elf_dt_name (abfd
) = name
;
1494 bfd_elf_set_dt_needed_soname (bfd
*abfd
, const char *name
)
1496 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1497 && bfd_get_format (abfd
) == bfd_object
)
1498 elf_dt_soname (abfd
) = name
;
1501 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1502 the linker ELF emulation code. */
1504 struct bfd_link_needed_list
*
1505 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
1506 struct bfd_link_info
*info
)
1508 if (! is_elf_hash_table (info
->hash
))
1510 return elf_hash_table (info
)->needed
;
1513 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1514 hook for the linker ELF emulation code. */
1516 struct bfd_link_needed_list
*
1517 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
1518 struct bfd_link_info
*info
)
1520 if (! is_elf_hash_table (info
->hash
))
1522 return elf_hash_table (info
)->runpath
;
1525 /* Get the name actually used for a dynamic object for a link. This
1526 is the SONAME entry if there is one. Otherwise, it is the string
1527 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1530 bfd_elf_get_dt_soname (bfd
*abfd
)
1532 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1533 && bfd_get_format (abfd
) == bfd_object
)
1534 return elf_dt_name (abfd
);
1538 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1539 the ELF linker emulation code. */
1542 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
1543 struct bfd_link_needed_list
**pneeded
)
1546 bfd_byte
*dynbuf
= NULL
;
1548 unsigned long shlink
;
1549 bfd_byte
*extdyn
, *extdynend
;
1551 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1555 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1556 || bfd_get_format (abfd
) != bfd_object
)
1559 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1560 if (s
== NULL
|| s
->_raw_size
== 0)
1563 dynbuf
= bfd_malloc (s
->_raw_size
);
1567 if (! bfd_get_section_contents (abfd
, s
, dynbuf
, 0, s
->_raw_size
))
1570 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1574 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1576 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1577 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1580 extdynend
= extdyn
+ s
->_raw_size
;
1581 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1583 Elf_Internal_Dyn dyn
;
1585 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1587 if (dyn
.d_tag
== DT_NULL
)
1590 if (dyn
.d_tag
== DT_NEEDED
)
1593 struct bfd_link_needed_list
*l
;
1594 unsigned int tagv
= dyn
.d_un
.d_val
;
1597 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1602 l
= bfd_alloc (abfd
, amt
);
1623 /* Allocate an ELF string table--force the first byte to be zero. */
1625 struct bfd_strtab_hash
*
1626 _bfd_elf_stringtab_init (void)
1628 struct bfd_strtab_hash
*ret
;
1630 ret
= _bfd_stringtab_init ();
1635 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1636 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1637 if (loc
== (bfd_size_type
) -1)
1639 _bfd_stringtab_free (ret
);
1646 /* ELF .o/exec file reading */
1648 /* Create a new bfd section from an ELF section header. */
1651 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1653 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1654 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1655 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1658 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1660 switch (hdr
->sh_type
)
1663 /* Inactive section. Throw it away. */
1666 case SHT_PROGBITS
: /* Normal section with contents. */
1667 case SHT_NOBITS
: /* .bss section. */
1668 case SHT_HASH
: /* .hash section. */
1669 case SHT_NOTE
: /* .note section. */
1670 case SHT_INIT_ARRAY
: /* .init_array section. */
1671 case SHT_FINI_ARRAY
: /* .fini_array section. */
1672 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1673 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1675 case SHT_DYNAMIC
: /* Dynamic linking information. */
1676 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1678 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1680 Elf_Internal_Shdr
*dynsymhdr
;
1682 /* The shared libraries distributed with hpux11 have a bogus
1683 sh_link field for the ".dynamic" section. Find the
1684 string table for the ".dynsym" section instead. */
1685 if (elf_dynsymtab (abfd
) != 0)
1687 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1688 hdr
->sh_link
= dynsymhdr
->sh_link
;
1692 unsigned int i
, num_sec
;
1694 num_sec
= elf_numsections (abfd
);
1695 for (i
= 1; i
< num_sec
; i
++)
1697 dynsymhdr
= elf_elfsections (abfd
)[i
];
1698 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1700 hdr
->sh_link
= dynsymhdr
->sh_link
;
1708 case SHT_SYMTAB
: /* A symbol table */
1709 if (elf_onesymtab (abfd
) == shindex
)
1712 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1713 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1714 elf_onesymtab (abfd
) = shindex
;
1715 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1716 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1717 abfd
->flags
|= HAS_SYMS
;
1719 /* Sometimes a shared object will map in the symbol table. If
1720 SHF_ALLOC is set, and this is a shared object, then we also
1721 treat this section as a BFD section. We can not base the
1722 decision purely on SHF_ALLOC, because that flag is sometimes
1723 set in a relocatable object file, which would confuse the
1725 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1726 && (abfd
->flags
& DYNAMIC
) != 0
1727 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1732 case SHT_DYNSYM
: /* A dynamic symbol table */
1733 if (elf_dynsymtab (abfd
) == shindex
)
1736 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1737 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1738 elf_dynsymtab (abfd
) = shindex
;
1739 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1740 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1741 abfd
->flags
|= HAS_SYMS
;
1743 /* Besides being a symbol table, we also treat this as a regular
1744 section, so that objcopy can handle it. */
1745 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1747 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1748 if (elf_symtab_shndx (abfd
) == shindex
)
1751 /* Get the associated symbol table. */
1752 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1753 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1756 elf_symtab_shndx (abfd
) = shindex
;
1757 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1758 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1761 case SHT_STRTAB
: /* A string table */
1762 if (hdr
->bfd_section
!= NULL
)
1764 if (ehdr
->e_shstrndx
== shindex
)
1766 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1767 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1771 unsigned int i
, num_sec
;
1773 num_sec
= elf_numsections (abfd
);
1774 for (i
= 1; i
< num_sec
; i
++)
1776 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1777 if (hdr2
->sh_link
== shindex
)
1779 if (! bfd_section_from_shdr (abfd
, i
))
1781 if (elf_onesymtab (abfd
) == i
)
1783 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1784 elf_elfsections (abfd
)[shindex
] =
1785 &elf_tdata (abfd
)->strtab_hdr
;
1788 if (elf_dynsymtab (abfd
) == i
)
1790 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1791 elf_elfsections (abfd
)[shindex
] = hdr
=
1792 &elf_tdata (abfd
)->dynstrtab_hdr
;
1793 /* We also treat this as a regular section, so
1794 that objcopy can handle it. */
1797 #if 0 /* Not handling other string tables specially right now. */
1798 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1799 /* We have a strtab for some random other section. */
1800 newsect
= (asection
*) hdr2
->bfd_section
;
1803 hdr
->bfd_section
= newsect
;
1804 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1806 elf_elfsections (abfd
)[shindex
] = hdr2
;
1812 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1816 /* *These* do a lot of work -- but build no sections! */
1818 asection
*target_sect
;
1819 Elf_Internal_Shdr
*hdr2
;
1820 unsigned int num_sec
= elf_numsections (abfd
);
1822 /* Check for a bogus link to avoid crashing. */
1823 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1824 || hdr
->sh_link
>= num_sec
)
1826 ((*_bfd_error_handler
)
1827 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1828 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1829 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1832 /* For some incomprehensible reason Oracle distributes
1833 libraries for Solaris in which some of the objects have
1834 bogus sh_link fields. It would be nice if we could just
1835 reject them, but, unfortunately, some people need to use
1836 them. We scan through the section headers; if we find only
1837 one suitable symbol table, we clobber the sh_link to point
1838 to it. I hope this doesn't break anything. */
1839 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1840 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1846 for (scan
= 1; scan
< num_sec
; scan
++)
1848 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1849 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1860 hdr
->sh_link
= found
;
1863 /* Get the symbol table. */
1864 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1865 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1868 /* If this reloc section does not use the main symbol table we
1869 don't treat it as a reloc section. BFD can't adequately
1870 represent such a section, so at least for now, we don't
1871 try. We just present it as a normal section. We also
1872 can't use it as a reloc section if it points to the null
1874 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1875 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1877 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1879 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1880 if (target_sect
== NULL
)
1883 if ((target_sect
->flags
& SEC_RELOC
) == 0
1884 || target_sect
->reloc_count
== 0)
1885 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1889 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1890 amt
= sizeof (*hdr2
);
1891 hdr2
= bfd_alloc (abfd
, amt
);
1892 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1895 elf_elfsections (abfd
)[shindex
] = hdr2
;
1896 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1897 target_sect
->flags
|= SEC_RELOC
;
1898 target_sect
->relocation
= NULL
;
1899 target_sect
->rel_filepos
= hdr
->sh_offset
;
1900 /* In the section to which the relocations apply, mark whether
1901 its relocations are of the REL or RELA variety. */
1902 if (hdr
->sh_size
!= 0)
1903 target_sect
->use_rela_p
= hdr
->sh_type
== SHT_RELA
;
1904 abfd
->flags
|= HAS_RELOC
;
1909 case SHT_GNU_verdef
:
1910 elf_dynverdef (abfd
) = shindex
;
1911 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1912 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1915 case SHT_GNU_versym
:
1916 elf_dynversym (abfd
) = shindex
;
1917 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1918 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1921 case SHT_GNU_verneed
:
1922 elf_dynverref (abfd
) = shindex
;
1923 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1924 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1931 /* We need a BFD section for objcopy and relocatable linking,
1932 and it's handy to have the signature available as the section
1934 name
= group_signature (abfd
, hdr
);
1937 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1939 if (hdr
->contents
!= NULL
)
1941 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1942 unsigned int n_elt
= hdr
->sh_size
/ 4;
1945 if (idx
->flags
& GRP_COMDAT
)
1946 hdr
->bfd_section
->flags
1947 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1949 while (--n_elt
!= 0)
1950 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
1951 && elf_next_in_group (s
) != NULL
)
1953 elf_next_in_group (hdr
->bfd_section
) = s
;
1960 /* Check for any processor-specific section types. */
1962 if (bed
->elf_backend_section_from_shdr
)
1963 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1971 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1972 Return SEC for sections that have no elf section, and NULL on error. */
1975 bfd_section_from_r_symndx (bfd
*abfd
,
1976 struct sym_sec_cache
*cache
,
1978 unsigned long r_symndx
)
1980 Elf_Internal_Shdr
*symtab_hdr
;
1981 unsigned char esym
[sizeof (Elf64_External_Sym
)];
1982 Elf_External_Sym_Shndx eshndx
;
1983 Elf_Internal_Sym isym
;
1984 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1986 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
1987 return cache
->sec
[ent
];
1989 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1990 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
1991 &isym
, esym
, &eshndx
) == NULL
)
1994 if (cache
->abfd
!= abfd
)
1996 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1999 cache
->indx
[ent
] = r_symndx
;
2000 cache
->sec
[ent
] = sec
;
2001 if ((isym
.st_shndx
!= SHN_UNDEF
&& isym
.st_shndx
< SHN_LORESERVE
)
2002 || isym
.st_shndx
> SHN_HIRESERVE
)
2005 s
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
2007 cache
->sec
[ent
] = s
;
2009 return cache
->sec
[ent
];
2012 /* Given an ELF section number, retrieve the corresponding BFD
2016 bfd_section_from_elf_index (bfd
*abfd
, unsigned int index
)
2018 if (index
>= elf_numsections (abfd
))
2020 return elf_elfsections (abfd
)[index
]->bfd_section
;
2023 static struct bfd_elf_special_section
const special_sections
[] =
2025 { ".bss", 4, -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2026 { ".comment", 8, 0, SHT_PROGBITS
, 0 },
2027 { ".data", 5, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2028 { ".data1", 6, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2029 { ".debug", 6, 0, SHT_PROGBITS
, 0 },
2030 { ".fini", 5, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2031 { ".init", 5, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2032 { ".line", 5, 0, SHT_PROGBITS
, 0 },
2033 { ".rodata", 7, -2, SHT_PROGBITS
, SHF_ALLOC
},
2034 { ".rodata1", 8, 0, SHT_PROGBITS
, SHF_ALLOC
},
2035 { ".tbss", 5, -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2036 { ".tdata", 6, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2037 { ".text", 5, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2038 { ".init_array", 11, 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2039 { ".fini_array", 11, 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2040 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2041 { ".debug_line", 11, 0, SHT_PROGBITS
, 0 },
2042 { ".debug_info", 11, 0, SHT_PROGBITS
, 0 },
2043 { ".debug_abbrev", 13, 0, SHT_PROGBITS
, 0 },
2044 { ".debug_aranges", 14, 0, SHT_PROGBITS
, 0 },
2045 { ".dynamic", 8, 0, SHT_DYNAMIC
, SHF_ALLOC
},
2046 { ".dynstr", 7, 0, SHT_STRTAB
, SHF_ALLOC
},
2047 { ".dynsym", 7, 0, SHT_DYNSYM
, SHF_ALLOC
},
2048 { ".got", 4, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2049 { ".hash", 5, 0, SHT_HASH
, SHF_ALLOC
},
2050 { ".interp", 7, 0, SHT_PROGBITS
, 0 },
2051 { ".plt", 4, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2052 { ".shstrtab", 9, 0, SHT_STRTAB
, 0 },
2053 { ".strtab", 7, 0, SHT_STRTAB
, 0 },
2054 { ".symtab", 7, 0, SHT_SYMTAB
, 0 },
2055 { ".gnu.version", 12, 0, SHT_GNU_versym
, 0 },
2056 { ".gnu.version_d", 14, 0, SHT_GNU_verdef
, 0 },
2057 { ".gnu.version_r", 14, 0, SHT_GNU_verneed
, 0 },
2058 { ".note", 5, -1, SHT_NOTE
, 0 },
2059 { ".rela", 5, -1, SHT_RELA
, 0 },
2060 { ".rel", 4, -1, SHT_REL
, 0 },
2061 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2062 { NULL
, 0, 0, 0, 0 }
2065 static const struct bfd_elf_special_section
*
2066 get_special_section (const char *name
,
2067 const struct bfd_elf_special_section
*special_sections
,
2071 int len
= strlen (name
);
2073 for (i
= 0; special_sections
[i
].prefix
!= NULL
; i
++)
2076 int prefix_len
= special_sections
[i
].prefix_length
;
2078 if (len
< prefix_len
)
2080 if (memcmp (name
, special_sections
[i
].prefix
, prefix_len
) != 0)
2083 suffix_len
= special_sections
[i
].suffix_length
;
2084 if (suffix_len
<= 0)
2086 if (name
[prefix_len
] != 0)
2088 if (suffix_len
== 0)
2090 if (name
[prefix_len
] != '.'
2091 && (suffix_len
== -2
2092 || (rela
&& special_sections
[i
].type
== SHT_REL
)))
2098 if (len
< prefix_len
+ suffix_len
)
2100 if (memcmp (name
+ len
- suffix_len
,
2101 special_sections
[i
].prefix
+ prefix_len
,
2105 return &special_sections
[i
];
2111 const struct bfd_elf_special_section
*
2112 _bfd_elf_get_sec_type_attr (bfd
*abfd
, const char *name
)
2114 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2115 const struct bfd_elf_special_section
*ssect
= NULL
;
2117 /* See if this is one of the special sections. */
2120 unsigned int rela
= bed
->default_use_rela_p
;
2122 if (bed
->special_sections
)
2123 ssect
= get_special_section (name
, bed
->special_sections
, rela
);
2126 ssect
= get_special_section (name
, special_sections
, rela
);
2133 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2135 struct bfd_elf_section_data
*sdata
;
2136 const struct bfd_elf_special_section
*ssect
;
2138 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2141 sdata
= bfd_zalloc (abfd
, sizeof (*sdata
));
2144 sec
->used_by_bfd
= sdata
;
2147 elf_section_type (sec
) = SHT_NULL
;
2148 ssect
= _bfd_elf_get_sec_type_attr (abfd
, sec
->name
);
2151 elf_section_type (sec
) = ssect
->type
;
2152 elf_section_flags (sec
) = ssect
->attr
;
2155 /* Indicate whether or not this section should use RELA relocations. */
2156 sec
->use_rela_p
= get_elf_backend_data (abfd
)->default_use_rela_p
;
2161 /* Create a new bfd section from an ELF program header.
2163 Since program segments have no names, we generate a synthetic name
2164 of the form segment<NUM>, where NUM is generally the index in the
2165 program header table. For segments that are split (see below) we
2166 generate the names segment<NUM>a and segment<NUM>b.
2168 Note that some program segments may have a file size that is different than
2169 (less than) the memory size. All this means is that at execution the
2170 system must allocate the amount of memory specified by the memory size,
2171 but only initialize it with the first "file size" bytes read from the
2172 file. This would occur for example, with program segments consisting
2173 of combined data+bss.
2175 To handle the above situation, this routine generates TWO bfd sections
2176 for the single program segment. The first has the length specified by
2177 the file size of the segment, and the second has the length specified
2178 by the difference between the two sizes. In effect, the segment is split
2179 into it's initialized and uninitialized parts.
2184 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2185 Elf_Internal_Phdr
*hdr
,
2187 const char *typename
)
2195 split
= ((hdr
->p_memsz
> 0)
2196 && (hdr
->p_filesz
> 0)
2197 && (hdr
->p_memsz
> hdr
->p_filesz
));
2198 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2199 len
= strlen (namebuf
) + 1;
2200 name
= bfd_alloc (abfd
, len
);
2203 memcpy (name
, namebuf
, len
);
2204 newsect
= bfd_make_section (abfd
, name
);
2205 if (newsect
== NULL
)
2207 newsect
->vma
= hdr
->p_vaddr
;
2208 newsect
->lma
= hdr
->p_paddr
;
2209 newsect
->_raw_size
= hdr
->p_filesz
;
2210 newsect
->filepos
= hdr
->p_offset
;
2211 newsect
->flags
|= SEC_HAS_CONTENTS
;
2212 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2213 if (hdr
->p_type
== PT_LOAD
)
2215 newsect
->flags
|= SEC_ALLOC
;
2216 newsect
->flags
|= SEC_LOAD
;
2217 if (hdr
->p_flags
& PF_X
)
2219 /* FIXME: all we known is that it has execute PERMISSION,
2221 newsect
->flags
|= SEC_CODE
;
2224 if (!(hdr
->p_flags
& PF_W
))
2226 newsect
->flags
|= SEC_READONLY
;
2231 sprintf (namebuf
, "%s%db", typename
, index
);
2232 len
= strlen (namebuf
) + 1;
2233 name
= bfd_alloc (abfd
, len
);
2236 memcpy (name
, namebuf
, len
);
2237 newsect
= bfd_make_section (abfd
, name
);
2238 if (newsect
== NULL
)
2240 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2241 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2242 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
2243 if (hdr
->p_type
== PT_LOAD
)
2245 newsect
->flags
|= SEC_ALLOC
;
2246 if (hdr
->p_flags
& PF_X
)
2247 newsect
->flags
|= SEC_CODE
;
2249 if (!(hdr
->p_flags
& PF_W
))
2250 newsect
->flags
|= SEC_READONLY
;
2257 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int index
)
2259 const struct elf_backend_data
*bed
;
2261 switch (hdr
->p_type
)
2264 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2267 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2270 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2273 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2276 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2278 if (! elfcore_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2283 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2286 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2288 case PT_GNU_EH_FRAME
:
2289 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
,
2293 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "stack");
2296 /* Check for any processor-specific program segment types.
2297 If no handler for them, default to making "segment" sections. */
2298 bed
= get_elf_backend_data (abfd
);
2299 if (bed
->elf_backend_section_from_phdr
)
2300 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2302 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2306 /* Initialize REL_HDR, the section-header for new section, containing
2307 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2308 relocations; otherwise, we use REL relocations. */
2311 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2312 Elf_Internal_Shdr
*rel_hdr
,
2314 bfd_boolean use_rela_p
)
2317 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2318 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2320 name
= bfd_alloc (abfd
, amt
);
2323 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2325 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2327 if (rel_hdr
->sh_name
== (unsigned int) -1)
2329 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2330 rel_hdr
->sh_entsize
= (use_rela_p
2331 ? bed
->s
->sizeof_rela
2332 : bed
->s
->sizeof_rel
);
2333 rel_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
2334 rel_hdr
->sh_flags
= 0;
2335 rel_hdr
->sh_addr
= 0;
2336 rel_hdr
->sh_size
= 0;
2337 rel_hdr
->sh_offset
= 0;
2342 /* Set up an ELF internal section header for a section. */
2345 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *failedptrarg
)
2347 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2348 bfd_boolean
*failedptr
= failedptrarg
;
2349 Elf_Internal_Shdr
*this_hdr
;
2353 /* We already failed; just get out of the bfd_map_over_sections
2358 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2360 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2361 asect
->name
, FALSE
);
2362 if (this_hdr
->sh_name
== (unsigned int) -1)
2368 this_hdr
->sh_flags
= 0;
2370 if ((asect
->flags
& SEC_ALLOC
) != 0
2371 || asect
->user_set_vma
)
2372 this_hdr
->sh_addr
= asect
->vma
;
2374 this_hdr
->sh_addr
= 0;
2376 this_hdr
->sh_offset
= 0;
2377 this_hdr
->sh_size
= asect
->_raw_size
;
2378 this_hdr
->sh_link
= 0;
2379 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2380 /* The sh_entsize and sh_info fields may have been set already by
2381 copy_private_section_data. */
2383 this_hdr
->bfd_section
= asect
;
2384 this_hdr
->contents
= NULL
;
2386 /* If the section type is unspecified, we set it based on
2388 if (this_hdr
->sh_type
== SHT_NULL
)
2390 if ((asect
->flags
& SEC_ALLOC
) != 0
2391 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2392 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2393 this_hdr
->sh_type
= SHT_NOBITS
;
2395 this_hdr
->sh_type
= SHT_PROGBITS
;
2398 switch (this_hdr
->sh_type
)
2404 case SHT_INIT_ARRAY
:
2405 case SHT_FINI_ARRAY
:
2406 case SHT_PREINIT_ARRAY
:
2413 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2417 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2421 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2425 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2426 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2430 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2431 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2434 case SHT_GNU_versym
:
2435 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2438 case SHT_GNU_verdef
:
2439 this_hdr
->sh_entsize
= 0;
2440 /* objcopy or strip will copy over sh_info, but may not set
2441 cverdefs. The linker will set cverdefs, but sh_info will be
2443 if (this_hdr
->sh_info
== 0)
2444 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2446 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2447 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2450 case SHT_GNU_verneed
:
2451 this_hdr
->sh_entsize
= 0;
2452 /* objcopy or strip will copy over sh_info, but may not set
2453 cverrefs. The linker will set cverrefs, but sh_info will be
2455 if (this_hdr
->sh_info
== 0)
2456 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2458 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2459 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2463 this_hdr
->sh_entsize
= 4;
2467 if ((asect
->flags
& SEC_ALLOC
) != 0)
2468 this_hdr
->sh_flags
|= SHF_ALLOC
;
2469 if ((asect
->flags
& SEC_READONLY
) == 0)
2470 this_hdr
->sh_flags
|= SHF_WRITE
;
2471 if ((asect
->flags
& SEC_CODE
) != 0)
2472 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2473 if ((asect
->flags
& SEC_MERGE
) != 0)
2475 this_hdr
->sh_flags
|= SHF_MERGE
;
2476 this_hdr
->sh_entsize
= asect
->entsize
;
2477 if ((asect
->flags
& SEC_STRINGS
) != 0)
2478 this_hdr
->sh_flags
|= SHF_STRINGS
;
2480 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2481 this_hdr
->sh_flags
|= SHF_GROUP
;
2482 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2484 this_hdr
->sh_flags
|= SHF_TLS
;
2485 if (asect
->_raw_size
== 0 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2487 struct bfd_link_order
*o
;
2489 this_hdr
->sh_size
= 0;
2490 for (o
= asect
->link_order_head
; o
!= NULL
; o
= o
->next
)
2491 if (this_hdr
->sh_size
< o
->offset
+ o
->size
)
2492 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2493 if (this_hdr
->sh_size
)
2494 this_hdr
->sh_type
= SHT_NOBITS
;
2498 /* Check for processor-specific section types. */
2499 if (bed
->elf_backend_fake_sections
2500 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2503 /* If the section has relocs, set up a section header for the
2504 SHT_REL[A] section. If two relocation sections are required for
2505 this section, it is up to the processor-specific back-end to
2506 create the other. */
2507 if ((asect
->flags
& SEC_RELOC
) != 0
2508 && !_bfd_elf_init_reloc_shdr (abfd
,
2509 &elf_section_data (asect
)->rel_hdr
,
2515 /* Fill in the contents of a SHT_GROUP section. */
2518 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2520 bfd_boolean
*failedptr
= failedptrarg
;
2521 unsigned long symindx
;
2522 asection
*elt
, *first
;
2524 struct bfd_link_order
*l
;
2527 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2532 if (elf_group_id (sec
) != NULL
)
2533 symindx
= elf_group_id (sec
)->udata
.i
;
2537 /* If called from the assembler, swap_out_syms will have set up
2538 elf_section_syms; If called for "ld -r", use target_index. */
2539 if (elf_section_syms (abfd
) != NULL
)
2540 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2542 symindx
= sec
->target_index
;
2544 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2546 /* The contents won't be allocated for "ld -r" or objcopy. */
2548 if (sec
->contents
== NULL
)
2551 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2553 /* Arrange for the section to be written out. */
2554 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2555 if (sec
->contents
== NULL
)
2562 loc
= sec
->contents
+ sec
->_raw_size
;
2564 /* Get the pointer to the first section in the group that gas
2565 squirreled away here. objcopy arranges for this to be set to the
2566 start of the input section group. */
2567 first
= elt
= elf_next_in_group (sec
);
2569 /* First element is a flag word. Rest of section is elf section
2570 indices for all the sections of the group. Write them backwards
2571 just to keep the group in the same order as given in .section
2572 directives, not that it matters. */
2581 s
= s
->output_section
;
2584 idx
= elf_section_data (s
)->this_idx
;
2585 H_PUT_32 (abfd
, idx
, loc
);
2586 elt
= elf_next_in_group (elt
);
2591 /* If this is a relocatable link, then the above did nothing because
2592 SEC is the output section. Look through the input sections
2594 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2595 if (l
->type
== bfd_indirect_link_order
2596 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2601 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2602 elt
= elf_next_in_group (elt
);
2603 /* During a relocatable link, the lists are circular. */
2605 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2607 /* With ld -r, merging SHT_GROUP sections results in wasted space
2608 due to allowing for the flag word on each input. We may well
2609 duplicate entries too. */
2610 while ((loc
-= 4) > sec
->contents
)
2611 H_PUT_32 (abfd
, 0, loc
);
2613 if (loc
!= sec
->contents
)
2616 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2619 /* Assign all ELF section numbers. The dummy first section is handled here
2620 too. The link/info pointers for the standard section types are filled
2621 in here too, while we're at it. */
2624 assign_section_numbers (bfd
*abfd
)
2626 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2628 unsigned int section_number
, secn
;
2629 Elf_Internal_Shdr
**i_shdrp
;
2634 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2636 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2638 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2640 if (section_number
== SHN_LORESERVE
)
2641 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2642 d
->this_idx
= section_number
++;
2643 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2644 if ((sec
->flags
& SEC_RELOC
) == 0)
2648 if (section_number
== SHN_LORESERVE
)
2649 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2650 d
->rel_idx
= section_number
++;
2651 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2656 if (section_number
== SHN_LORESERVE
)
2657 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2658 d
->rel_idx2
= section_number
++;
2659 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2665 if (section_number
== SHN_LORESERVE
)
2666 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2667 t
->shstrtab_section
= section_number
++;
2668 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2669 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2671 if (bfd_get_symcount (abfd
) > 0)
2673 if (section_number
== SHN_LORESERVE
)
2674 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2675 t
->symtab_section
= section_number
++;
2676 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2677 if (section_number
> SHN_LORESERVE
- 2)
2679 if (section_number
== SHN_LORESERVE
)
2680 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2681 t
->symtab_shndx_section
= section_number
++;
2682 t
->symtab_shndx_hdr
.sh_name
2683 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2684 ".symtab_shndx", FALSE
);
2685 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2688 if (section_number
== SHN_LORESERVE
)
2689 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2690 t
->strtab_section
= section_number
++;
2691 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2694 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2695 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2697 elf_numsections (abfd
) = section_number
;
2698 elf_elfheader (abfd
)->e_shnum
= section_number
;
2699 if (section_number
> SHN_LORESERVE
)
2700 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2702 /* Set up the list of section header pointers, in agreement with the
2704 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2705 i_shdrp
= bfd_zalloc (abfd
, amt
);
2706 if (i_shdrp
== NULL
)
2709 amt
= sizeof (Elf_Internal_Shdr
);
2710 i_shdrp
[0] = bfd_zalloc (abfd
, amt
);
2711 if (i_shdrp
[0] == NULL
)
2713 bfd_release (abfd
, i_shdrp
);
2717 elf_elfsections (abfd
) = i_shdrp
;
2719 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2720 if (bfd_get_symcount (abfd
) > 0)
2722 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2723 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2725 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2726 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2728 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2729 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2731 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2733 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2737 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2738 if (d
->rel_idx
!= 0)
2739 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2740 if (d
->rel_idx2
!= 0)
2741 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2743 /* Fill in the sh_link and sh_info fields while we're at it. */
2745 /* sh_link of a reloc section is the section index of the symbol
2746 table. sh_info is the section index of the section to which
2747 the relocation entries apply. */
2748 if (d
->rel_idx
!= 0)
2750 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2751 d
->rel_hdr
.sh_info
= d
->this_idx
;
2753 if (d
->rel_idx2
!= 0)
2755 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2756 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2759 switch (d
->this_hdr
.sh_type
)
2763 /* A reloc section which we are treating as a normal BFD
2764 section. sh_link is the section index of the symbol
2765 table. sh_info is the section index of the section to
2766 which the relocation entries apply. We assume that an
2767 allocated reloc section uses the dynamic symbol table.
2768 FIXME: How can we be sure? */
2769 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2771 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2773 /* We look up the section the relocs apply to by name. */
2775 if (d
->this_hdr
.sh_type
== SHT_REL
)
2779 s
= bfd_get_section_by_name (abfd
, name
);
2781 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2785 /* We assume that a section named .stab*str is a stabs
2786 string section. We look for a section with the same name
2787 but without the trailing ``str'', and set its sh_link
2788 field to point to this section. */
2789 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2790 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2795 len
= strlen (sec
->name
);
2796 alc
= bfd_malloc (len
- 2);
2799 memcpy (alc
, sec
->name
, len
- 3);
2800 alc
[len
- 3] = '\0';
2801 s
= bfd_get_section_by_name (abfd
, alc
);
2805 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2807 /* This is a .stab section. */
2808 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
2809 elf_section_data (s
)->this_hdr
.sh_entsize
2810 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2817 case SHT_GNU_verneed
:
2818 case SHT_GNU_verdef
:
2819 /* sh_link is the section header index of the string table
2820 used for the dynamic entries, or the symbol table, or the
2822 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2824 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2828 case SHT_GNU_versym
:
2829 /* sh_link is the section header index of the symbol table
2830 this hash table or version table is for. */
2831 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2833 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2837 d
->this_hdr
.sh_link
= t
->symtab_section
;
2841 for (secn
= 1; secn
< section_number
; ++secn
)
2842 if (i_shdrp
[secn
] == NULL
)
2843 i_shdrp
[secn
] = i_shdrp
[0];
2845 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2846 i_shdrp
[secn
]->sh_name
);
2850 /* Map symbol from it's internal number to the external number, moving
2851 all local symbols to be at the head of the list. */
2854 sym_is_global (bfd
*abfd
, asymbol
*sym
)
2856 /* If the backend has a special mapping, use it. */
2857 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2858 if (bed
->elf_backend_sym_is_global
)
2859 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
2861 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2862 || bfd_is_und_section (bfd_get_section (sym
))
2863 || bfd_is_com_section (bfd_get_section (sym
)));
2867 elf_map_symbols (bfd
*abfd
)
2869 unsigned int symcount
= bfd_get_symcount (abfd
);
2870 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2871 asymbol
**sect_syms
;
2872 unsigned int num_locals
= 0;
2873 unsigned int num_globals
= 0;
2874 unsigned int num_locals2
= 0;
2875 unsigned int num_globals2
= 0;
2883 fprintf (stderr
, "elf_map_symbols\n");
2887 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2889 if (max_index
< asect
->index
)
2890 max_index
= asect
->index
;
2894 amt
= max_index
* sizeof (asymbol
*);
2895 sect_syms
= bfd_zalloc (abfd
, amt
);
2896 if (sect_syms
== NULL
)
2898 elf_section_syms (abfd
) = sect_syms
;
2899 elf_num_section_syms (abfd
) = max_index
;
2901 /* Init sect_syms entries for any section symbols we have already
2902 decided to output. */
2903 for (idx
= 0; idx
< symcount
; idx
++)
2905 asymbol
*sym
= syms
[idx
];
2907 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2914 if (sec
->owner
!= NULL
)
2916 if (sec
->owner
!= abfd
)
2918 if (sec
->output_offset
!= 0)
2921 sec
= sec
->output_section
;
2923 /* Empty sections in the input files may have had a
2924 section symbol created for them. (See the comment
2925 near the end of _bfd_generic_link_output_symbols in
2926 linker.c). If the linker script discards such
2927 sections then we will reach this point. Since we know
2928 that we cannot avoid this case, we detect it and skip
2929 the abort and the assignment to the sect_syms array.
2930 To reproduce this particular case try running the
2931 linker testsuite test ld-scripts/weak.exp for an ELF
2932 port that uses the generic linker. */
2933 if (sec
->owner
== NULL
)
2936 BFD_ASSERT (sec
->owner
== abfd
);
2938 sect_syms
[sec
->index
] = syms
[idx
];
2943 /* Classify all of the symbols. */
2944 for (idx
= 0; idx
< symcount
; idx
++)
2946 if (!sym_is_global (abfd
, syms
[idx
]))
2952 /* We will be adding a section symbol for each BFD section. Most normal
2953 sections will already have a section symbol in outsymbols, but
2954 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2955 at least in that case. */
2956 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2958 if (sect_syms
[asect
->index
] == NULL
)
2960 if (!sym_is_global (abfd
, asect
->symbol
))
2967 /* Now sort the symbols so the local symbols are first. */
2968 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2969 new_syms
= bfd_alloc (abfd
, amt
);
2971 if (new_syms
== NULL
)
2974 for (idx
= 0; idx
< symcount
; idx
++)
2976 asymbol
*sym
= syms
[idx
];
2979 if (!sym_is_global (abfd
, sym
))
2982 i
= num_locals
+ num_globals2
++;
2984 sym
->udata
.i
= i
+ 1;
2986 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2988 if (sect_syms
[asect
->index
] == NULL
)
2990 asymbol
*sym
= asect
->symbol
;
2993 sect_syms
[asect
->index
] = sym
;
2994 if (!sym_is_global (abfd
, sym
))
2997 i
= num_locals
+ num_globals2
++;
2999 sym
->udata
.i
= i
+ 1;
3003 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3005 elf_num_locals (abfd
) = num_locals
;
3006 elf_num_globals (abfd
) = num_globals
;
3010 /* Align to the maximum file alignment that could be required for any
3011 ELF data structure. */
3013 static inline file_ptr
3014 align_file_position (file_ptr off
, int align
)
3016 return (off
+ align
- 1) & ~(align
- 1);
3019 /* Assign a file position to a section, optionally aligning to the
3020 required section alignment. */
3023 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3031 al
= i_shdrp
->sh_addralign
;
3033 offset
= BFD_ALIGN (offset
, al
);
3035 i_shdrp
->sh_offset
= offset
;
3036 if (i_shdrp
->bfd_section
!= NULL
)
3037 i_shdrp
->bfd_section
->filepos
= offset
;
3038 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3039 offset
+= i_shdrp
->sh_size
;
3043 /* Compute the file positions we are going to put the sections at, and
3044 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3045 is not NULL, this is being called by the ELF backend linker. */
3048 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3049 struct bfd_link_info
*link_info
)
3051 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3053 struct bfd_strtab_hash
*strtab
;
3054 Elf_Internal_Shdr
*shstrtab_hdr
;
3056 if (abfd
->output_has_begun
)
3059 /* Do any elf backend specific processing first. */
3060 if (bed
->elf_backend_begin_write_processing
)
3061 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3063 if (! prep_headers (abfd
))
3066 /* Post process the headers if necessary. */
3067 if (bed
->elf_backend_post_process_headers
)
3068 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3071 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3075 if (!assign_section_numbers (abfd
))
3078 /* The backend linker builds symbol table information itself. */
3079 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3081 /* Non-zero if doing a relocatable link. */
3082 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3084 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3088 if (link_info
== NULL
)
3090 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3095 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3096 /* sh_name was set in prep_headers. */
3097 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3098 shstrtab_hdr
->sh_flags
= 0;
3099 shstrtab_hdr
->sh_addr
= 0;
3100 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3101 shstrtab_hdr
->sh_entsize
= 0;
3102 shstrtab_hdr
->sh_link
= 0;
3103 shstrtab_hdr
->sh_info
= 0;
3104 /* sh_offset is set in assign_file_positions_except_relocs. */
3105 shstrtab_hdr
->sh_addralign
= 1;
3107 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3110 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3113 Elf_Internal_Shdr
*hdr
;
3115 off
= elf_tdata (abfd
)->next_file_pos
;
3117 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3118 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3120 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3121 if (hdr
->sh_size
!= 0)
3122 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3124 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3125 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3127 elf_tdata (abfd
)->next_file_pos
= off
;
3129 /* Now that we know where the .strtab section goes, write it
3131 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3132 || ! _bfd_stringtab_emit (abfd
, strtab
))
3134 _bfd_stringtab_free (strtab
);
3137 abfd
->output_has_begun
= TRUE
;
3142 /* Create a mapping from a set of sections to a program segment. */
3144 static struct elf_segment_map
*
3145 make_mapping (bfd
*abfd
,
3146 asection
**sections
,
3151 struct elf_segment_map
*m
;
3156 amt
= sizeof (struct elf_segment_map
);
3157 amt
+= (to
- from
- 1) * sizeof (asection
*);
3158 m
= bfd_zalloc (abfd
, amt
);
3162 m
->p_type
= PT_LOAD
;
3163 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3164 m
->sections
[i
- from
] = *hdrpp
;
3165 m
->count
= to
- from
;
3167 if (from
== 0 && phdr
)
3169 /* Include the headers in the first PT_LOAD segment. */
3170 m
->includes_filehdr
= 1;
3171 m
->includes_phdrs
= 1;
3177 /* Set up a mapping from BFD sections to program segments. */
3180 map_sections_to_segments (bfd
*abfd
)
3182 asection
**sections
= NULL
;
3186 struct elf_segment_map
*mfirst
;
3187 struct elf_segment_map
**pm
;
3188 struct elf_segment_map
*m
;
3190 unsigned int phdr_index
;
3191 bfd_vma maxpagesize
;
3193 bfd_boolean phdr_in_segment
= TRUE
;
3194 bfd_boolean writable
;
3196 asection
*first_tls
= NULL
;
3197 asection
*dynsec
, *eh_frame_hdr
;
3200 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3203 if (bfd_count_sections (abfd
) == 0)
3206 /* Select the allocated sections, and sort them. */
3208 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
3209 sections
= bfd_malloc (amt
);
3210 if (sections
== NULL
)
3214 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3216 if ((s
->flags
& SEC_ALLOC
) != 0)
3222 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3225 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3227 /* Build the mapping. */
3232 /* If we have a .interp section, then create a PT_PHDR segment for
3233 the program headers and a PT_INTERP segment for the .interp
3235 s
= bfd_get_section_by_name (abfd
, ".interp");
3236 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3238 amt
= sizeof (struct elf_segment_map
);
3239 m
= bfd_zalloc (abfd
, amt
);
3243 m
->p_type
= PT_PHDR
;
3244 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3245 m
->p_flags
= PF_R
| PF_X
;
3246 m
->p_flags_valid
= 1;
3247 m
->includes_phdrs
= 1;
3252 amt
= sizeof (struct elf_segment_map
);
3253 m
= bfd_zalloc (abfd
, amt
);
3257 m
->p_type
= PT_INTERP
;
3265 /* Look through the sections. We put sections in the same program
3266 segment when the start of the second section can be placed within
3267 a few bytes of the end of the first section. */
3270 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
3272 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3274 && (dynsec
->flags
& SEC_LOAD
) == 0)
3277 /* Deal with -Ttext or something similar such that the first section
3278 is not adjacent to the program headers. This is an
3279 approximation, since at this point we don't know exactly how many
3280 program headers we will need. */
3283 bfd_size_type phdr_size
;
3285 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3287 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3288 if ((abfd
->flags
& D_PAGED
) == 0
3289 || sections
[0]->lma
< phdr_size
3290 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3291 phdr_in_segment
= FALSE
;
3294 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3297 bfd_boolean new_segment
;
3301 /* See if this section and the last one will fit in the same
3304 if (last_hdr
== NULL
)
3306 /* If we don't have a segment yet, then we don't need a new
3307 one (we build the last one after this loop). */
3308 new_segment
= FALSE
;
3310 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3312 /* If this section has a different relation between the
3313 virtual address and the load address, then we need a new
3317 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3318 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3320 /* If putting this section in this segment would force us to
3321 skip a page in the segment, then we need a new segment. */
3324 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
3325 && (hdr
->flags
& SEC_LOAD
) != 0)
3327 /* We don't want to put a loadable section after a
3328 nonloadable section in the same segment. */
3331 else if ((abfd
->flags
& D_PAGED
) == 0)
3333 /* If the file is not demand paged, which means that we
3334 don't require the sections to be correctly aligned in the
3335 file, then there is no other reason for a new segment. */
3336 new_segment
= FALSE
;
3339 && (hdr
->flags
& SEC_READONLY
) == 0
3340 && (((last_hdr
->lma
+ last_hdr
->_raw_size
- 1)
3341 & ~(maxpagesize
- 1))
3342 != (hdr
->lma
& ~(maxpagesize
- 1))))
3344 /* We don't want to put a writable section in a read only
3345 segment, unless they are on the same page in memory
3346 anyhow. We already know that the last section does not
3347 bring us past the current section on the page, so the
3348 only case in which the new section is not on the same
3349 page as the previous section is when the previous section
3350 ends precisely on a page boundary. */
3355 /* Otherwise, we can use the same segment. */
3356 new_segment
= FALSE
;
3361 if ((hdr
->flags
& SEC_READONLY
) == 0)
3363 /* Ignore .tbss section for segment layout purposes. */
3364 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3369 /* We need a new program segment. We must create a new program
3370 header holding all the sections from phdr_index until hdr. */
3372 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3379 if ((hdr
->flags
& SEC_READONLY
) == 0)
3386 phdr_in_segment
= FALSE
;
3389 /* Create a final PT_LOAD program segment. */
3390 if (last_hdr
!= NULL
)
3392 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3400 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3403 amt
= sizeof (struct elf_segment_map
);
3404 m
= bfd_zalloc (abfd
, amt
);
3408 m
->p_type
= PT_DYNAMIC
;
3410 m
->sections
[0] = dynsec
;
3416 /* For each loadable .note section, add a PT_NOTE segment. We don't
3417 use bfd_get_section_by_name, because if we link together
3418 nonloadable .note sections and loadable .note sections, we will
3419 generate two .note sections in the output file. FIXME: Using
3420 names for section types is bogus anyhow. */
3421 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3423 if ((s
->flags
& SEC_LOAD
) != 0
3424 && strncmp (s
->name
, ".note", 5) == 0)
3426 amt
= sizeof (struct elf_segment_map
);
3427 m
= bfd_zalloc (abfd
, amt
);
3431 m
->p_type
= PT_NOTE
;
3438 if (s
->flags
& SEC_THREAD_LOCAL
)
3446 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3451 amt
= sizeof (struct elf_segment_map
);
3452 amt
+= (tls_count
- 1) * sizeof (asection
*);
3453 m
= bfd_zalloc (abfd
, amt
);
3458 m
->count
= tls_count
;
3459 /* Mandated PF_R. */
3461 m
->p_flags_valid
= 1;
3462 for (i
= 0; i
< tls_count
; ++i
)
3464 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3465 m
->sections
[i
] = first_tls
;
3466 first_tls
= first_tls
->next
;
3473 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3475 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
3476 if (eh_frame_hdr
!= NULL
3477 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
3479 amt
= sizeof (struct elf_segment_map
);
3480 m
= bfd_zalloc (abfd
, amt
);
3484 m
->p_type
= PT_GNU_EH_FRAME
;
3486 m
->sections
[0] = eh_frame_hdr
->output_section
;
3492 if (elf_tdata (abfd
)->stack_flags
)
3494 amt
= sizeof (struct elf_segment_map
);
3495 m
= bfd_zalloc (abfd
, amt
);
3499 m
->p_type
= PT_GNU_STACK
;
3500 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
3501 m
->p_flags_valid
= 1;
3510 elf_tdata (abfd
)->segment_map
= mfirst
;
3514 if (sections
!= NULL
)
3519 /* Sort sections by address. */
3522 elf_sort_sections (const void *arg1
, const void *arg2
)
3524 const asection
*sec1
= *(const asection
**) arg1
;
3525 const asection
*sec2
= *(const asection
**) arg2
;
3526 bfd_size_type size1
, size2
;
3528 /* Sort by LMA first, since this is the address used to
3529 place the section into a segment. */
3530 if (sec1
->lma
< sec2
->lma
)
3532 else if (sec1
->lma
> sec2
->lma
)
3535 /* Then sort by VMA. Normally the LMA and the VMA will be
3536 the same, and this will do nothing. */
3537 if (sec1
->vma
< sec2
->vma
)
3539 else if (sec1
->vma
> sec2
->vma
)
3542 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3544 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3550 /* If the indicies are the same, do not return 0
3551 here, but continue to try the next comparison. */
3552 if (sec1
->target_index
- sec2
->target_index
!= 0)
3553 return sec1
->target_index
- sec2
->target_index
;
3558 else if (TOEND (sec2
))
3563 /* Sort by size, to put zero sized sections
3564 before others at the same address. */
3566 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->_raw_size
: 0;
3567 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->_raw_size
: 0;
3574 return sec1
->target_index
- sec2
->target_index
;
3577 /* Ian Lance Taylor writes:
3579 We shouldn't be using % with a negative signed number. That's just
3580 not good. We have to make sure either that the number is not
3581 negative, or that the number has an unsigned type. When the types
3582 are all the same size they wind up as unsigned. When file_ptr is a
3583 larger signed type, the arithmetic winds up as signed long long,
3586 What we're trying to say here is something like ``increase OFF by
3587 the least amount that will cause it to be equal to the VMA modulo
3589 /* In other words, something like:
3591 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3592 off_offset = off % bed->maxpagesize;
3593 if (vma_offset < off_offset)
3594 adjustment = vma_offset + bed->maxpagesize - off_offset;
3596 adjustment = vma_offset - off_offset;
3598 which can can be collapsed into the expression below. */
3601 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
3603 return ((vma
- off
) % maxpagesize
);
3606 /* Assign file positions to the sections based on the mapping from
3607 sections to segments. This function also sets up some fields in
3608 the file header, and writes out the program headers. */
3611 assign_file_positions_for_segments (bfd
*abfd
, struct bfd_link_info
*link_info
)
3613 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3615 struct elf_segment_map
*m
;
3617 Elf_Internal_Phdr
*phdrs
;
3619 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3620 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3621 Elf_Internal_Phdr
*p
;
3624 if (elf_tdata (abfd
)->segment_map
== NULL
)
3626 if (! map_sections_to_segments (abfd
))
3631 /* The placement algorithm assumes that non allocated sections are
3632 not in PT_LOAD segments. We ensure this here by removing such
3633 sections from the segment map. */
3634 for (m
= elf_tdata (abfd
)->segment_map
;
3638 unsigned int new_count
;
3641 if (m
->p_type
!= PT_LOAD
)
3645 for (i
= 0; i
< m
->count
; i
++)
3647 if ((m
->sections
[i
]->flags
& SEC_ALLOC
) != 0)
3650 m
->sections
[new_count
] = m
->sections
[i
];
3656 if (new_count
!= m
->count
)
3657 m
->count
= new_count
;
3661 if (bed
->elf_backend_modify_segment_map
)
3663 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
, link_info
))
3668 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3671 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3672 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3673 elf_elfheader (abfd
)->e_phnum
= count
;
3678 /* If we already counted the number of program segments, make sure
3679 that we allocated enough space. This happens when SIZEOF_HEADERS
3680 is used in a linker script. */
3681 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3682 if (alloc
!= 0 && count
> alloc
)
3684 ((*_bfd_error_handler
)
3685 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3686 bfd_get_filename (abfd
), alloc
, count
));
3687 bfd_set_error (bfd_error_bad_value
);
3694 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3695 phdrs
= bfd_alloc (abfd
, amt
);
3699 off
= bed
->s
->sizeof_ehdr
;
3700 off
+= alloc
* bed
->s
->sizeof_phdr
;
3707 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3714 /* If elf_segment_map is not from map_sections_to_segments, the
3715 sections may not be correctly ordered. NOTE: sorting should
3716 not be done to the PT_NOTE section of a corefile, which may
3717 contain several pseudo-sections artificially created by bfd.
3718 Sorting these pseudo-sections breaks things badly. */
3720 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3721 && m
->p_type
== PT_NOTE
))
3722 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3725 p
->p_type
= m
->p_type
;
3726 p
->p_flags
= m
->p_flags
;
3728 if (p
->p_type
== PT_LOAD
3730 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3732 if ((abfd
->flags
& D_PAGED
) != 0)
3733 off
+= vma_page_aligned_bias (m
->sections
[0]->vma
, off
,
3737 bfd_size_type align
;
3740 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3742 bfd_size_type secalign
;
3744 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3745 if (secalign
> align
)
3749 off
+= vma_page_aligned_bias (m
->sections
[0]->vma
, off
,
3757 p
->p_vaddr
= m
->sections
[0]->vma
;
3759 if (m
->p_paddr_valid
)
3760 p
->p_paddr
= m
->p_paddr
;
3761 else if (m
->count
== 0)
3764 p
->p_paddr
= m
->sections
[0]->lma
;
3766 if (p
->p_type
== PT_LOAD
3767 && (abfd
->flags
& D_PAGED
) != 0)
3768 p
->p_align
= bed
->maxpagesize
;
3769 else if (m
->count
== 0)
3770 p
->p_align
= 1 << bed
->s
->log_file_align
;
3778 if (m
->includes_filehdr
)
3780 if (! m
->p_flags_valid
)
3783 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3784 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3787 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3789 if (p
->p_vaddr
< (bfd_vma
) off
)
3791 (*_bfd_error_handler
)
3792 (_("%s: Not enough room for program headers, try linking with -N"),
3793 bfd_get_filename (abfd
));
3794 bfd_set_error (bfd_error_bad_value
);
3799 if (! m
->p_paddr_valid
)
3802 if (p
->p_type
== PT_LOAD
)
3804 filehdr_vaddr
= p
->p_vaddr
;
3805 filehdr_paddr
= p
->p_paddr
;
3809 if (m
->includes_phdrs
)
3811 if (! m
->p_flags_valid
)
3814 if (m
->includes_filehdr
)
3816 if (p
->p_type
== PT_LOAD
)
3818 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3819 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3824 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3828 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3829 p
->p_vaddr
-= off
- p
->p_offset
;
3830 if (! m
->p_paddr_valid
)
3831 p
->p_paddr
-= off
- p
->p_offset
;
3834 if (p
->p_type
== PT_LOAD
)
3836 phdrs_vaddr
= p
->p_vaddr
;
3837 phdrs_paddr
= p
->p_paddr
;
3840 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3843 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3844 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3847 if (p
->p_type
== PT_LOAD
3848 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3850 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3856 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3857 p
->p_filesz
+= adjust
;
3858 p
->p_memsz
+= adjust
;
3864 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3868 bfd_size_type align
;
3872 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3874 /* The section may have artificial alignment forced by a
3875 link script. Notice this case by the gap between the
3876 cumulative phdr lma and the section's lma. */
3877 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3879 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3881 p
->p_memsz
+= adjust
;
3882 if (p
->p_type
== PT_LOAD
3883 || (p
->p_type
== PT_NOTE
3884 && bfd_get_format (abfd
) == bfd_core
))
3889 if ((flags
& SEC_LOAD
) != 0
3890 || (flags
& SEC_THREAD_LOCAL
) != 0)
3891 p
->p_filesz
+= adjust
;
3894 if (p
->p_type
== PT_LOAD
)
3896 bfd_signed_vma adjust
;
3898 if ((flags
& SEC_LOAD
) != 0)
3900 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3904 else if ((flags
& SEC_ALLOC
) != 0)
3906 /* The section VMA must equal the file position
3907 modulo the page size. FIXME: I'm not sure if
3908 this adjustment is really necessary. We used to
3909 not have the SEC_LOAD case just above, and then
3910 this was necessary, but now I'm not sure. */
3911 if ((abfd
->flags
& D_PAGED
) != 0)
3912 adjust
= vma_page_aligned_bias (sec
->vma
, voff
,
3915 adjust
= vma_page_aligned_bias (sec
->vma
, voff
,
3925 (* _bfd_error_handler
) (_("\
3926 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3927 bfd_section_name (abfd
, sec
),
3932 p
->p_memsz
+= adjust
;
3935 if ((flags
& SEC_LOAD
) != 0)
3936 p
->p_filesz
+= adjust
;
3941 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3942 used in a linker script we may have a section with
3943 SEC_LOAD clear but which is supposed to have
3945 if ((flags
& SEC_LOAD
) != 0
3946 || (flags
& SEC_HAS_CONTENTS
) != 0)
3947 off
+= sec
->_raw_size
;
3949 if ((flags
& SEC_ALLOC
) != 0
3950 && ((flags
& SEC_LOAD
) != 0
3951 || (flags
& SEC_THREAD_LOCAL
) == 0))
3952 voff
+= sec
->_raw_size
;
3955 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3957 /* The actual "note" segment has i == 0.
3958 This is the one that actually contains everything. */
3962 p
->p_filesz
= sec
->_raw_size
;
3963 off
+= sec
->_raw_size
;
3968 /* Fake sections -- don't need to be written. */
3971 flags
= sec
->flags
= 0;
3978 if ((sec
->flags
& SEC_LOAD
) != 0
3979 || (sec
->flags
& SEC_THREAD_LOCAL
) == 0
3980 || p
->p_type
== PT_TLS
)
3981 p
->p_memsz
+= sec
->_raw_size
;
3983 if ((flags
& SEC_LOAD
) != 0)
3984 p
->p_filesz
+= sec
->_raw_size
;
3986 if (p
->p_type
== PT_TLS
3987 && sec
->_raw_size
== 0
3988 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
3990 struct bfd_link_order
*o
;
3991 bfd_vma tbss_size
= 0;
3993 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
3994 if (tbss_size
< o
->offset
+ o
->size
)
3995 tbss_size
= o
->offset
+ o
->size
;
3997 p
->p_memsz
+= tbss_size
;
4000 if (align
> p
->p_align
4001 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
4005 if (! m
->p_flags_valid
)
4008 if ((flags
& SEC_CODE
) != 0)
4010 if ((flags
& SEC_READONLY
) == 0)
4016 /* Now that we have set the section file positions, we can set up
4017 the file positions for the non PT_LOAD segments. */
4018 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4022 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
4024 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
4025 p
->p_offset
= m
->sections
[0]->filepos
;
4029 if (m
->includes_filehdr
)
4031 p
->p_vaddr
= filehdr_vaddr
;
4032 if (! m
->p_paddr_valid
)
4033 p
->p_paddr
= filehdr_paddr
;
4035 else if (m
->includes_phdrs
)
4037 p
->p_vaddr
= phdrs_vaddr
;
4038 if (! m
->p_paddr_valid
)
4039 p
->p_paddr
= phdrs_paddr
;
4044 /* Clear out any program headers we allocated but did not use. */
4045 for (; count
< alloc
; count
++, p
++)
4047 memset (p
, 0, sizeof *p
);
4048 p
->p_type
= PT_NULL
;
4051 elf_tdata (abfd
)->phdr
= phdrs
;
4053 elf_tdata (abfd
)->next_file_pos
= off
;
4055 /* Write out the program headers. */
4056 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4057 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
4063 /* Get the size of the program header.
4065 If this is called by the linker before any of the section VMA's are set, it
4066 can't calculate the correct value for a strange memory layout. This only
4067 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4068 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4069 data segment (exclusive of .interp and .dynamic).
4071 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4072 will be two segments. */
4074 static bfd_size_type
4075 get_program_header_size (bfd
*abfd
)
4079 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4081 /* We can't return a different result each time we're called. */
4082 if (elf_tdata (abfd
)->program_header_size
!= 0)
4083 return elf_tdata (abfd
)->program_header_size
;
4085 if (elf_tdata (abfd
)->segment_map
!= NULL
)
4087 struct elf_segment_map
*m
;
4090 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4092 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4093 return elf_tdata (abfd
)->program_header_size
;
4096 /* Assume we will need exactly two PT_LOAD segments: one for text
4097 and one for data. */
4100 s
= bfd_get_section_by_name (abfd
, ".interp");
4101 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4103 /* If we have a loadable interpreter section, we need a
4104 PT_INTERP segment. In this case, assume we also need a
4105 PT_PHDR segment, although that may not be true for all
4110 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4112 /* We need a PT_DYNAMIC segment. */
4116 if (elf_tdata (abfd
)->eh_frame_hdr
)
4118 /* We need a PT_GNU_EH_FRAME segment. */
4122 if (elf_tdata (abfd
)->stack_flags
)
4124 /* We need a PT_GNU_STACK segment. */
4128 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4130 if ((s
->flags
& SEC_LOAD
) != 0
4131 && strncmp (s
->name
, ".note", 5) == 0)
4133 /* We need a PT_NOTE segment. */
4138 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4140 if (s
->flags
& SEC_THREAD_LOCAL
)
4142 /* We need a PT_TLS segment. */
4148 /* Let the backend count up any program headers it might need. */
4149 if (bed
->elf_backend_additional_program_headers
)
4153 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
4159 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4160 return elf_tdata (abfd
)->program_header_size
;
4163 /* Work out the file positions of all the sections. This is called by
4164 _bfd_elf_compute_section_file_positions. All the section sizes and
4165 VMAs must be known before this is called.
4167 We do not consider reloc sections at this point, unless they form
4168 part of the loadable image. Reloc sections are assigned file
4169 positions in assign_file_positions_for_relocs, which is called by
4170 write_object_contents and final_link.
4172 We also don't set the positions of the .symtab and .strtab here. */
4175 assign_file_positions_except_relocs (bfd
*abfd
,
4176 struct bfd_link_info
*link_info
)
4178 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
4179 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
4180 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4181 unsigned int num_sec
= elf_numsections (abfd
);
4183 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4185 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4186 && bfd_get_format (abfd
) != bfd_core
)
4188 Elf_Internal_Shdr
**hdrpp
;
4191 /* Start after the ELF header. */
4192 off
= i_ehdrp
->e_ehsize
;
4194 /* We are not creating an executable, which means that we are
4195 not creating a program header, and that the actual order of
4196 the sections in the file is unimportant. */
4197 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4199 Elf_Internal_Shdr
*hdr
;
4202 if (hdr
->sh_type
== SHT_REL
4203 || hdr
->sh_type
== SHT_RELA
4204 || i
== tdata
->symtab_section
4205 || i
== tdata
->symtab_shndx_section
4206 || i
== tdata
->strtab_section
)
4208 hdr
->sh_offset
= -1;
4211 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4213 if (i
== SHN_LORESERVE
- 1)
4215 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4216 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4223 Elf_Internal_Shdr
**hdrpp
;
4225 /* Assign file positions for the loaded sections based on the
4226 assignment of sections to segments. */
4227 if (! assign_file_positions_for_segments (abfd
, link_info
))
4230 /* Assign file positions for the other sections. */
4232 off
= elf_tdata (abfd
)->next_file_pos
;
4233 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4235 Elf_Internal_Shdr
*hdr
;
4238 if (hdr
->bfd_section
!= NULL
4239 && hdr
->bfd_section
->filepos
!= 0)
4240 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
4241 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4243 ((*_bfd_error_handler
)
4244 (_("%s: warning: allocated section `%s' not in segment"),
4245 bfd_get_filename (abfd
),
4246 (hdr
->bfd_section
== NULL
4248 : hdr
->bfd_section
->name
)));
4249 if ((abfd
->flags
& D_PAGED
) != 0)
4250 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4253 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4255 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4258 else if (hdr
->sh_type
== SHT_REL
4259 || hdr
->sh_type
== SHT_RELA
4260 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4261 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4262 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4263 hdr
->sh_offset
= -1;
4265 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4267 if (i
== SHN_LORESERVE
- 1)
4269 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4270 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4275 /* Place the section headers. */
4276 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
4277 i_ehdrp
->e_shoff
= off
;
4278 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4280 elf_tdata (abfd
)->next_file_pos
= off
;
4286 prep_headers (bfd
*abfd
)
4288 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4289 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4290 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4291 struct elf_strtab_hash
*shstrtab
;
4292 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4294 i_ehdrp
= elf_elfheader (abfd
);
4295 i_shdrp
= elf_elfsections (abfd
);
4297 shstrtab
= _bfd_elf_strtab_init ();
4298 if (shstrtab
== NULL
)
4301 elf_shstrtab (abfd
) = shstrtab
;
4303 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4304 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4305 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4306 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4308 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4309 i_ehdrp
->e_ident
[EI_DATA
] =
4310 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4311 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4313 if ((abfd
->flags
& DYNAMIC
) != 0)
4314 i_ehdrp
->e_type
= ET_DYN
;
4315 else if ((abfd
->flags
& EXEC_P
) != 0)
4316 i_ehdrp
->e_type
= ET_EXEC
;
4317 else if (bfd_get_format (abfd
) == bfd_core
)
4318 i_ehdrp
->e_type
= ET_CORE
;
4320 i_ehdrp
->e_type
= ET_REL
;
4322 switch (bfd_get_arch (abfd
))
4324 case bfd_arch_unknown
:
4325 i_ehdrp
->e_machine
= EM_NONE
;
4328 /* There used to be a long list of cases here, each one setting
4329 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4330 in the corresponding bfd definition. To avoid duplication,
4331 the switch was removed. Machines that need special handling
4332 can generally do it in elf_backend_final_write_processing(),
4333 unless they need the information earlier than the final write.
4334 Such need can generally be supplied by replacing the tests for
4335 e_machine with the conditions used to determine it. */
4337 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
4340 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4341 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4343 /* No program header, for now. */
4344 i_ehdrp
->e_phoff
= 0;
4345 i_ehdrp
->e_phentsize
= 0;
4346 i_ehdrp
->e_phnum
= 0;
4348 /* Each bfd section is section header entry. */
4349 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4350 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4352 /* If we're building an executable, we'll need a program header table. */
4353 if (abfd
->flags
& EXEC_P
)
4355 /* It all happens later. */
4357 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
4359 /* elf_build_phdrs() returns a (NULL-terminated) array of
4360 Elf_Internal_Phdrs. */
4361 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
4362 i_ehdrp
->e_phoff
= outbase
;
4363 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
4368 i_ehdrp
->e_phentsize
= 0;
4370 i_ehdrp
->e_phoff
= 0;
4373 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4374 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
4375 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4376 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
4377 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4378 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
4379 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4380 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4381 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4387 /* Assign file positions for all the reloc sections which are not part
4388 of the loadable file image. */
4391 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
4394 unsigned int i
, num_sec
;
4395 Elf_Internal_Shdr
**shdrpp
;
4397 off
= elf_tdata (abfd
)->next_file_pos
;
4399 num_sec
= elf_numsections (abfd
);
4400 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4402 Elf_Internal_Shdr
*shdrp
;
4405 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4406 && shdrp
->sh_offset
== -1)
4407 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
4410 elf_tdata (abfd
)->next_file_pos
= off
;
4414 _bfd_elf_write_object_contents (bfd
*abfd
)
4416 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4417 Elf_Internal_Ehdr
*i_ehdrp
;
4418 Elf_Internal_Shdr
**i_shdrp
;
4420 unsigned int count
, num_sec
;
4422 if (! abfd
->output_has_begun
4423 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
4426 i_shdrp
= elf_elfsections (abfd
);
4427 i_ehdrp
= elf_elfheader (abfd
);
4430 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4434 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4436 /* After writing the headers, we need to write the sections too... */
4437 num_sec
= elf_numsections (abfd
);
4438 for (count
= 1; count
< num_sec
; count
++)
4440 if (bed
->elf_backend_section_processing
)
4441 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4442 if (i_shdrp
[count
]->contents
)
4444 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4446 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4447 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4450 if (count
== SHN_LORESERVE
- 1)
4451 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4454 /* Write out the section header names. */
4455 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4456 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4459 if (bed
->elf_backend_final_write_processing
)
4460 (*bed
->elf_backend_final_write_processing
) (abfd
,
4461 elf_tdata (abfd
)->linker
);
4463 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4467 _bfd_elf_write_corefile_contents (bfd
*abfd
)
4469 /* Hopefully this can be done just like an object file. */
4470 return _bfd_elf_write_object_contents (abfd
);
4473 /* Given a section, search the header to find them. */
4476 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
4478 const struct elf_backend_data
*bed
;
4481 if (elf_section_data (asect
) != NULL
4482 && elf_section_data (asect
)->this_idx
!= 0)
4483 return elf_section_data (asect
)->this_idx
;
4485 if (bfd_is_abs_section (asect
))
4487 else if (bfd_is_com_section (asect
))
4489 else if (bfd_is_und_section (asect
))
4493 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4494 int maxindex
= elf_numsections (abfd
);
4496 for (index
= 1; index
< maxindex
; index
++)
4498 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4500 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4506 bed
= get_elf_backend_data (abfd
);
4507 if (bed
->elf_backend_section_from_bfd_section
)
4511 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4516 bfd_set_error (bfd_error_nonrepresentable_section
);
4521 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4525 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
4527 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4529 flagword flags
= asym_ptr
->flags
;
4531 /* When gas creates relocations against local labels, it creates its
4532 own symbol for the section, but does put the symbol into the
4533 symbol chain, so udata is 0. When the linker is generating
4534 relocatable output, this section symbol may be for one of the
4535 input sections rather than the output section. */
4536 if (asym_ptr
->udata
.i
== 0
4537 && (flags
& BSF_SECTION_SYM
)
4538 && asym_ptr
->section
)
4542 if (asym_ptr
->section
->output_section
!= NULL
)
4543 indx
= asym_ptr
->section
->output_section
->index
;
4545 indx
= asym_ptr
->section
->index
;
4546 if (indx
< elf_num_section_syms (abfd
)
4547 && elf_section_syms (abfd
)[indx
] != NULL
)
4548 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4551 idx
= asym_ptr
->udata
.i
;
4555 /* This case can occur when using --strip-symbol on a symbol
4556 which is used in a relocation entry. */
4557 (*_bfd_error_handler
)
4558 (_("%s: symbol `%s' required but not present"),
4559 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4560 bfd_set_error (bfd_error_no_symbols
);
4567 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4568 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4569 elf_symbol_flags (flags
));
4577 /* Copy private BFD data. This copies any program header information. */
4580 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
4582 Elf_Internal_Ehdr
*iehdr
;
4583 struct elf_segment_map
*map
;
4584 struct elf_segment_map
*map_first
;
4585 struct elf_segment_map
**pointer_to_map
;
4586 Elf_Internal_Phdr
*segment
;
4589 unsigned int num_segments
;
4590 bfd_boolean phdr_included
= FALSE
;
4591 bfd_vma maxpagesize
;
4592 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
4593 unsigned int phdr_adjust_num
= 0;
4594 const struct elf_backend_data
*bed
;
4596 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4597 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4600 if (elf_tdata (ibfd
)->phdr
== NULL
)
4603 bed
= get_elf_backend_data (ibfd
);
4604 iehdr
= elf_elfheader (ibfd
);
4607 pointer_to_map
= &map_first
;
4609 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4610 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4612 /* Returns the end address of the segment + 1. */
4613 #define SEGMENT_END(segment, start) \
4614 (start + (segment->p_memsz > segment->p_filesz \
4615 ? segment->p_memsz : segment->p_filesz))
4617 #define SECTION_SIZE(section, segment) \
4618 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4619 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4620 ? section->_raw_size : 0)
4622 /* Returns TRUE if the given section is contained within
4623 the given segment. VMA addresses are compared. */
4624 #define IS_CONTAINED_BY_VMA(section, segment) \
4625 (section->vma >= segment->p_vaddr \
4626 && (section->vma + SECTION_SIZE (section, segment) \
4627 <= (SEGMENT_END (segment, segment->p_vaddr))))
4629 /* Returns TRUE if the given section is contained within
4630 the given segment. LMA addresses are compared. */
4631 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4632 (section->lma >= base \
4633 && (section->lma + SECTION_SIZE (section, segment) \
4634 <= SEGMENT_END (segment, base)))
4636 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4637 #define IS_COREFILE_NOTE(p, s) \
4638 (p->p_type == PT_NOTE \
4639 && bfd_get_format (ibfd) == bfd_core \
4640 && s->vma == 0 && s->lma == 0 \
4641 && (bfd_vma) s->filepos >= p->p_offset \
4642 && ((bfd_vma) s->filepos + s->_raw_size \
4643 <= p->p_offset + p->p_filesz))
4645 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4646 linker, which generates a PT_INTERP section with p_vaddr and
4647 p_memsz set to 0. */
4648 #define IS_SOLARIS_PT_INTERP(p, s) \
4650 && p->p_paddr == 0 \
4651 && p->p_memsz == 0 \
4652 && p->p_filesz > 0 \
4653 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4654 && s->_raw_size > 0 \
4655 && (bfd_vma) s->filepos >= p->p_offset \
4656 && ((bfd_vma) s->filepos + s->_raw_size \
4657 <= p->p_offset + p->p_filesz))
4659 /* Decide if the given section should be included in the given segment.
4660 A section will be included if:
4661 1. It is within the address space of the segment -- we use the LMA
4662 if that is set for the segment and the VMA otherwise,
4663 2. It is an allocated segment,
4664 3. There is an output section associated with it,
4665 4. The section has not already been allocated to a previous segment.
4666 5. PT_GNU_STACK segments do not include any sections.
4667 6. PT_TLS segment includes only SHF_TLS sections.
4668 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4669 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4670 ((((segment->p_paddr \
4671 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4672 : IS_CONTAINED_BY_VMA (section, segment)) \
4673 && (section->flags & SEC_ALLOC) != 0) \
4674 || IS_COREFILE_NOTE (segment, section)) \
4675 && section->output_section != NULL \
4676 && segment->p_type != PT_GNU_STACK \
4677 && (segment->p_type != PT_TLS \
4678 || (section->flags & SEC_THREAD_LOCAL)) \
4679 && (segment->p_type == PT_LOAD \
4680 || segment->p_type == PT_TLS \
4681 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4682 && ! section->segment_mark)
4684 /* Returns TRUE iff seg1 starts after the end of seg2. */
4685 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4686 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4688 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4689 their VMA address ranges and their LMA address ranges overlap.
4690 It is possible to have overlapping VMA ranges without overlapping LMA
4691 ranges. RedBoot images for example can have both .data and .bss mapped
4692 to the same VMA range, but with the .data section mapped to a different
4694 #define SEGMENT_OVERLAPS(seg1, seg2) \
4695 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4696 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4697 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4698 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4700 /* Initialise the segment mark field. */
4701 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4702 section
->segment_mark
= FALSE
;
4704 /* Scan through the segments specified in the program header
4705 of the input BFD. For this first scan we look for overlaps
4706 in the loadable segments. These can be created by weird
4707 parameters to objcopy. Also, fix some solaris weirdness. */
4708 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4713 Elf_Internal_Phdr
*segment2
;
4715 if (segment
->p_type
== PT_INTERP
)
4716 for (section
= ibfd
->sections
; section
; section
= section
->next
)
4717 if (IS_SOLARIS_PT_INTERP (segment
, section
))
4719 /* Mininal change so that the normal section to segment
4720 assignment code will work. */
4721 segment
->p_vaddr
= section
->vma
;
4725 if (segment
->p_type
!= PT_LOAD
)
4728 /* Determine if this segment overlaps any previous segments. */
4729 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4731 bfd_signed_vma extra_length
;
4733 if (segment2
->p_type
!= PT_LOAD
4734 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4737 /* Merge the two segments together. */
4738 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4740 /* Extend SEGMENT2 to include SEGMENT and then delete
4743 SEGMENT_END (segment
, segment
->p_vaddr
)
4744 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4746 if (extra_length
> 0)
4748 segment2
->p_memsz
+= extra_length
;
4749 segment2
->p_filesz
+= extra_length
;
4752 segment
->p_type
= PT_NULL
;
4754 /* Since we have deleted P we must restart the outer loop. */
4756 segment
= elf_tdata (ibfd
)->phdr
;
4761 /* Extend SEGMENT to include SEGMENT2 and then delete
4764 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4765 - SEGMENT_END (segment
, segment
->p_vaddr
);
4767 if (extra_length
> 0)
4769 segment
->p_memsz
+= extra_length
;
4770 segment
->p_filesz
+= extra_length
;
4773 segment2
->p_type
= PT_NULL
;
4778 /* The second scan attempts to assign sections to segments. */
4779 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4783 unsigned int section_count
;
4784 asection
** sections
;
4785 asection
* output_section
;
4787 bfd_vma matching_lma
;
4788 bfd_vma suggested_lma
;
4792 if (segment
->p_type
== PT_NULL
)
4795 /* Compute how many sections might be placed into this segment. */
4796 for (section
= ibfd
->sections
, section_count
= 0;
4798 section
= section
->next
)
4799 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4802 /* Allocate a segment map big enough to contain
4803 all of the sections we have selected. */
4804 amt
= sizeof (struct elf_segment_map
);
4805 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4806 map
= bfd_alloc (obfd
, amt
);
4810 /* Initialise the fields of the segment map. Default to
4811 using the physical address of the segment in the input BFD. */
4813 map
->p_type
= segment
->p_type
;
4814 map
->p_flags
= segment
->p_flags
;
4815 map
->p_flags_valid
= 1;
4816 map
->p_paddr
= segment
->p_paddr
;
4817 map
->p_paddr_valid
= 1;
4819 /* Determine if this segment contains the ELF file header
4820 and if it contains the program headers themselves. */
4821 map
->includes_filehdr
= (segment
->p_offset
== 0
4822 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4824 map
->includes_phdrs
= 0;
4826 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4828 map
->includes_phdrs
=
4829 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4830 && (segment
->p_offset
+ segment
->p_filesz
4831 >= ((bfd_vma
) iehdr
->e_phoff
4832 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4834 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4835 phdr_included
= TRUE
;
4838 if (section_count
== 0)
4840 /* Special segments, such as the PT_PHDR segment, may contain
4841 no sections, but ordinary, loadable segments should contain
4842 something. They are allowed by the ELF spec however, so only
4843 a warning is produced. */
4844 if (segment
->p_type
== PT_LOAD
)
4845 (*_bfd_error_handler
)
4846 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4847 bfd_archive_filename (ibfd
));
4850 *pointer_to_map
= map
;
4851 pointer_to_map
= &map
->next
;
4856 /* Now scan the sections in the input BFD again and attempt
4857 to add their corresponding output sections to the segment map.
4858 The problem here is how to handle an output section which has
4859 been moved (ie had its LMA changed). There are four possibilities:
4861 1. None of the sections have been moved.
4862 In this case we can continue to use the segment LMA from the
4865 2. All of the sections have been moved by the same amount.
4866 In this case we can change the segment's LMA to match the LMA
4867 of the first section.
4869 3. Some of the sections have been moved, others have not.
4870 In this case those sections which have not been moved can be
4871 placed in the current segment which will have to have its size,
4872 and possibly its LMA changed, and a new segment or segments will
4873 have to be created to contain the other sections.
4875 4. The sections have been moved, but not by the same amount.
4876 In this case we can change the segment's LMA to match the LMA
4877 of the first section and we will have to create a new segment
4878 or segments to contain the other sections.
4880 In order to save time, we allocate an array to hold the section
4881 pointers that we are interested in. As these sections get assigned
4882 to a segment, they are removed from this array. */
4884 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4885 to work around this long long bug. */
4886 amt
= section_count
* sizeof (asection
*);
4887 sections
= bfd_malloc (amt
);
4888 if (sections
== NULL
)
4891 /* Step One: Scan for segment vs section LMA conflicts.
4892 Also add the sections to the section array allocated above.
4893 Also add the sections to the current segment. In the common
4894 case, where the sections have not been moved, this means that
4895 we have completely filled the segment, and there is nothing
4901 for (j
= 0, section
= ibfd
->sections
;
4903 section
= section
->next
)
4905 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4907 output_section
= section
->output_section
;
4909 sections
[j
++] = section
;
4911 /* The Solaris native linker always sets p_paddr to 0.
4912 We try to catch that case here, and set it to the
4913 correct value. Note - some backends require that
4914 p_paddr be left as zero. */
4915 if (segment
->p_paddr
== 0
4916 && segment
->p_vaddr
!= 0
4917 && (! bed
->want_p_paddr_set_to_zero
)
4919 && output_section
->lma
!= 0
4920 && (output_section
->vma
== (segment
->p_vaddr
4921 + (map
->includes_filehdr
4924 + (map
->includes_phdrs
4926 * iehdr
->e_phentsize
)
4928 map
->p_paddr
= segment
->p_vaddr
;
4930 /* Match up the physical address of the segment with the
4931 LMA address of the output section. */
4932 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4933 || IS_COREFILE_NOTE (segment
, section
)
4934 || (bed
->want_p_paddr_set_to_zero
&&
4935 IS_CONTAINED_BY_VMA (output_section
, segment
))
4938 if (matching_lma
== 0)
4939 matching_lma
= output_section
->lma
;
4941 /* We assume that if the section fits within the segment
4942 then it does not overlap any other section within that
4944 map
->sections
[isec
++] = output_section
;
4946 else if (suggested_lma
== 0)
4947 suggested_lma
= output_section
->lma
;
4951 BFD_ASSERT (j
== section_count
);
4953 /* Step Two: Adjust the physical address of the current segment,
4955 if (isec
== section_count
)
4957 /* All of the sections fitted within the segment as currently
4958 specified. This is the default case. Add the segment to
4959 the list of built segments and carry on to process the next
4960 program header in the input BFD. */
4961 map
->count
= section_count
;
4962 *pointer_to_map
= map
;
4963 pointer_to_map
= &map
->next
;
4970 if (matching_lma
!= 0)
4972 /* At least one section fits inside the current segment.
4973 Keep it, but modify its physical address to match the
4974 LMA of the first section that fitted. */
4975 map
->p_paddr
= matching_lma
;
4979 /* None of the sections fitted inside the current segment.
4980 Change the current segment's physical address to match
4981 the LMA of the first section. */
4982 map
->p_paddr
= suggested_lma
;
4985 /* Offset the segment physical address from the lma
4986 to allow for space taken up by elf headers. */
4987 if (map
->includes_filehdr
)
4988 map
->p_paddr
-= iehdr
->e_ehsize
;
4990 if (map
->includes_phdrs
)
4992 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4994 /* iehdr->e_phnum is just an estimate of the number
4995 of program headers that we will need. Make a note
4996 here of the number we used and the segment we chose
4997 to hold these headers, so that we can adjust the
4998 offset when we know the correct value. */
4999 phdr_adjust_num
= iehdr
->e_phnum
;
5000 phdr_adjust_seg
= map
;
5004 /* Step Three: Loop over the sections again, this time assigning
5005 those that fit to the current segment and removing them from the
5006 sections array; but making sure not to leave large gaps. Once all
5007 possible sections have been assigned to the current segment it is
5008 added to the list of built segments and if sections still remain
5009 to be assigned, a new segment is constructed before repeating
5017 /* Fill the current segment with sections that fit. */
5018 for (j
= 0; j
< section_count
; j
++)
5020 section
= sections
[j
];
5022 if (section
== NULL
)
5025 output_section
= section
->output_section
;
5027 BFD_ASSERT (output_section
!= NULL
);
5029 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5030 || IS_COREFILE_NOTE (segment
, section
))
5032 if (map
->count
== 0)
5034 /* If the first section in a segment does not start at
5035 the beginning of the segment, then something is
5037 if (output_section
->lma
!=
5039 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5040 + (map
->includes_phdrs
5041 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5047 asection
* prev_sec
;
5049 prev_sec
= map
->sections
[map
->count
- 1];
5051 /* If the gap between the end of the previous section
5052 and the start of this section is more than
5053 maxpagesize then we need to start a new segment. */
5054 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
5056 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5057 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
5058 > output_section
->lma
))
5060 if (suggested_lma
== 0)
5061 suggested_lma
= output_section
->lma
;
5067 map
->sections
[map
->count
++] = output_section
;
5070 section
->segment_mark
= TRUE
;
5072 else if (suggested_lma
== 0)
5073 suggested_lma
= output_section
->lma
;
5076 BFD_ASSERT (map
->count
> 0);
5078 /* Add the current segment to the list of built segments. */
5079 *pointer_to_map
= map
;
5080 pointer_to_map
= &map
->next
;
5082 if (isec
< section_count
)
5084 /* We still have not allocated all of the sections to
5085 segments. Create a new segment here, initialise it
5086 and carry on looping. */
5087 amt
= sizeof (struct elf_segment_map
);
5088 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5089 map
= bfd_alloc (obfd
, amt
);
5096 /* Initialise the fields of the segment map. Set the physical
5097 physical address to the LMA of the first section that has
5098 not yet been assigned. */
5100 map
->p_type
= segment
->p_type
;
5101 map
->p_flags
= segment
->p_flags
;
5102 map
->p_flags_valid
= 1;
5103 map
->p_paddr
= suggested_lma
;
5104 map
->p_paddr_valid
= 1;
5105 map
->includes_filehdr
= 0;
5106 map
->includes_phdrs
= 0;
5109 while (isec
< section_count
);
5114 /* The Solaris linker creates program headers in which all the
5115 p_paddr fields are zero. When we try to objcopy or strip such a
5116 file, we get confused. Check for this case, and if we find it
5117 reset the p_paddr_valid fields. */
5118 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5119 if (map
->p_paddr
!= 0)
5122 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5123 map
->p_paddr_valid
= 0;
5125 elf_tdata (obfd
)->segment_map
= map_first
;
5127 /* If we had to estimate the number of program headers that were
5128 going to be needed, then check our estimate now and adjust
5129 the offset if necessary. */
5130 if (phdr_adjust_seg
!= NULL
)
5134 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5137 if (count
> phdr_adjust_num
)
5138 phdr_adjust_seg
->p_paddr
5139 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5143 /* Final Step: Sort the segments into ascending order of physical
5145 if (map_first
!= NULL
)
5147 struct elf_segment_map
*prev
;
5150 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
5152 /* Yes I know - its a bubble sort.... */
5153 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
5155 /* Swap map and map->next. */
5156 prev
->next
= map
->next
;
5157 map
->next
= map
->next
->next
;
5158 prev
->next
->next
= map
;
5169 #undef IS_CONTAINED_BY_VMA
5170 #undef IS_CONTAINED_BY_LMA
5171 #undef IS_COREFILE_NOTE
5172 #undef IS_SOLARIS_PT_INTERP
5173 #undef INCLUDE_SECTION_IN_SEGMENT
5174 #undef SEGMENT_AFTER_SEGMENT
5175 #undef SEGMENT_OVERLAPS
5179 /* Copy private section information. This copies over the entsize
5180 field, and sometimes the info field. */
5183 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
5188 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5190 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5191 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5194 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
5198 /* Only set up the segments if there are no more SEC_ALLOC
5199 sections. FIXME: This won't do the right thing if objcopy is
5200 used to remove the last SEC_ALLOC section, since objcopy
5201 won't call this routine in that case. */
5202 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
5203 if ((s
->flags
& SEC_ALLOC
) != 0)
5207 if (! copy_private_bfd_data (ibfd
, obfd
))
5212 ihdr
= &elf_section_data (isec
)->this_hdr
;
5213 ohdr
= &elf_section_data (osec
)->this_hdr
;
5215 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
5217 if (ihdr
->sh_type
== SHT_SYMTAB
5218 || ihdr
->sh_type
== SHT_DYNSYM
5219 || ihdr
->sh_type
== SHT_GNU_verneed
5220 || ihdr
->sh_type
== SHT_GNU_verdef
)
5221 ohdr
->sh_info
= ihdr
->sh_info
;
5223 /* Set things up for objcopy. The output SHT_GROUP section will
5224 have its elf_next_in_group pointing back to the input group
5226 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5227 elf_group_name (osec
) = elf_group_name (isec
);
5229 osec
->use_rela_p
= isec
->use_rela_p
;
5234 /* Copy private symbol information. If this symbol is in a section
5235 which we did not map into a BFD section, try to map the section
5236 index correctly. We use special macro definitions for the mapped
5237 section indices; these definitions are interpreted by the
5238 swap_out_syms function. */
5240 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5241 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5242 #define MAP_STRTAB (SHN_HIOS + 3)
5243 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5244 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5247 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
5252 elf_symbol_type
*isym
, *osym
;
5254 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5255 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5258 isym
= elf_symbol_from (ibfd
, isymarg
);
5259 osym
= elf_symbol_from (obfd
, osymarg
);
5263 && bfd_is_abs_section (isym
->symbol
.section
))
5267 shndx
= isym
->internal_elf_sym
.st_shndx
;
5268 if (shndx
== elf_onesymtab (ibfd
))
5269 shndx
= MAP_ONESYMTAB
;
5270 else if (shndx
== elf_dynsymtab (ibfd
))
5271 shndx
= MAP_DYNSYMTAB
;
5272 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
5274 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
5275 shndx
= MAP_SHSTRTAB
;
5276 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
5277 shndx
= MAP_SYM_SHNDX
;
5278 osym
->internal_elf_sym
.st_shndx
= shndx
;
5284 /* Swap out the symbols. */
5287 swap_out_syms (bfd
*abfd
,
5288 struct bfd_strtab_hash
**sttp
,
5291 const struct elf_backend_data
*bed
;
5294 struct bfd_strtab_hash
*stt
;
5295 Elf_Internal_Shdr
*symtab_hdr
;
5296 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5297 Elf_Internal_Shdr
*symstrtab_hdr
;
5298 char *outbound_syms
;
5299 char *outbound_shndx
;
5302 bfd_boolean name_local_sections
;
5304 if (!elf_map_symbols (abfd
))
5307 /* Dump out the symtabs. */
5308 stt
= _bfd_elf_stringtab_init ();
5312 bed
= get_elf_backend_data (abfd
);
5313 symcount
= bfd_get_symcount (abfd
);
5314 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5315 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5316 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5317 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5318 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5319 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
5321 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5322 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5324 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
5325 outbound_syms
= bfd_alloc (abfd
, amt
);
5326 if (outbound_syms
== NULL
)
5328 _bfd_stringtab_free (stt
);
5331 symtab_hdr
->contents
= outbound_syms
;
5333 outbound_shndx
= NULL
;
5334 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5335 if (symtab_shndx_hdr
->sh_name
!= 0)
5337 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5338 outbound_shndx
= bfd_zalloc (abfd
, amt
);
5339 if (outbound_shndx
== NULL
)
5341 _bfd_stringtab_free (stt
);
5345 symtab_shndx_hdr
->contents
= outbound_shndx
;
5346 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5347 symtab_shndx_hdr
->sh_size
= amt
;
5348 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5349 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5352 /* Now generate the data (for "contents"). */
5354 /* Fill in zeroth symbol and swap it out. */
5355 Elf_Internal_Sym sym
;
5361 sym
.st_shndx
= SHN_UNDEF
;
5362 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5363 outbound_syms
+= bed
->s
->sizeof_sym
;
5364 if (outbound_shndx
!= NULL
)
5365 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5369 = (bed
->elf_backend_name_local_section_symbols
5370 && bed
->elf_backend_name_local_section_symbols (abfd
));
5372 syms
= bfd_get_outsymbols (abfd
);
5373 for (idx
= 0; idx
< symcount
; idx
++)
5375 Elf_Internal_Sym sym
;
5376 bfd_vma value
= syms
[idx
]->value
;
5377 elf_symbol_type
*type_ptr
;
5378 flagword flags
= syms
[idx
]->flags
;
5381 if (!name_local_sections
5382 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5384 /* Local section symbols have no name. */
5389 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5392 if (sym
.st_name
== (unsigned long) -1)
5394 _bfd_stringtab_free (stt
);
5399 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5401 if ((flags
& BSF_SECTION_SYM
) == 0
5402 && bfd_is_com_section (syms
[idx
]->section
))
5404 /* ELF common symbols put the alignment into the `value' field,
5405 and the size into the `size' field. This is backwards from
5406 how BFD handles it, so reverse it here. */
5407 sym
.st_size
= value
;
5408 if (type_ptr
== NULL
5409 || type_ptr
->internal_elf_sym
.st_value
== 0)
5410 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5412 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5413 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5414 (abfd
, syms
[idx
]->section
);
5418 asection
*sec
= syms
[idx
]->section
;
5421 if (sec
->output_section
)
5423 value
+= sec
->output_offset
;
5424 sec
= sec
->output_section
;
5427 /* Don't add in the section vma for relocatable output. */
5428 if (! relocatable_p
)
5430 sym
.st_value
= value
;
5431 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5433 if (bfd_is_abs_section (sec
)
5435 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5437 /* This symbol is in a real ELF section which we did
5438 not create as a BFD section. Undo the mapping done
5439 by copy_private_symbol_data. */
5440 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5444 shndx
= elf_onesymtab (abfd
);
5447 shndx
= elf_dynsymtab (abfd
);
5450 shndx
= elf_tdata (abfd
)->strtab_section
;
5453 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5456 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5464 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5470 /* Writing this would be a hell of a lot easier if
5471 we had some decent documentation on bfd, and
5472 knew what to expect of the library, and what to
5473 demand of applications. For example, it
5474 appears that `objcopy' might not set the
5475 section of a symbol to be a section that is
5476 actually in the output file. */
5477 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5480 _bfd_error_handler (_("\
5481 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5482 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
5484 bfd_set_error (bfd_error_invalid_operation
);
5485 _bfd_stringtab_free (stt
);
5489 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5490 BFD_ASSERT (shndx
!= -1);
5494 sym
.st_shndx
= shndx
;
5497 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5499 else if ((flags
& BSF_FUNCTION
) != 0)
5501 else if ((flags
& BSF_OBJECT
) != 0)
5506 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5509 /* Processor-specific types. */
5510 if (type_ptr
!= NULL
5511 && bed
->elf_backend_get_symbol_type
)
5512 type
= ((*bed
->elf_backend_get_symbol_type
)
5513 (&type_ptr
->internal_elf_sym
, type
));
5515 if (flags
& BSF_SECTION_SYM
)
5517 if (flags
& BSF_GLOBAL
)
5518 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5520 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5522 else if (bfd_is_com_section (syms
[idx
]->section
))
5523 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5524 else if (bfd_is_und_section (syms
[idx
]->section
))
5525 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5529 else if (flags
& BSF_FILE
)
5530 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5533 int bind
= STB_LOCAL
;
5535 if (flags
& BSF_LOCAL
)
5537 else if (flags
& BSF_WEAK
)
5539 else if (flags
& BSF_GLOBAL
)
5542 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5545 if (type_ptr
!= NULL
)
5546 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5550 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5551 outbound_syms
+= bed
->s
->sizeof_sym
;
5552 if (outbound_shndx
!= NULL
)
5553 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5557 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5558 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5560 symstrtab_hdr
->sh_flags
= 0;
5561 symstrtab_hdr
->sh_addr
= 0;
5562 symstrtab_hdr
->sh_entsize
= 0;
5563 symstrtab_hdr
->sh_link
= 0;
5564 symstrtab_hdr
->sh_info
= 0;
5565 symstrtab_hdr
->sh_addralign
= 1;
5570 /* Return the number of bytes required to hold the symtab vector.
5572 Note that we base it on the count plus 1, since we will null terminate
5573 the vector allocated based on this size. However, the ELF symbol table
5574 always has a dummy entry as symbol #0, so it ends up even. */
5577 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
5581 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5583 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5584 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5586 symtab_size
-= sizeof (asymbol
*);
5592 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
5596 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5598 if (elf_dynsymtab (abfd
) == 0)
5600 bfd_set_error (bfd_error_invalid_operation
);
5604 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5605 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5607 symtab_size
-= sizeof (asymbol
*);
5613 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
5616 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5619 /* Canonicalize the relocs. */
5622 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
5629 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5631 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
5634 tblptr
= section
->relocation
;
5635 for (i
= 0; i
< section
->reloc_count
; i
++)
5636 *relptr
++ = tblptr
++;
5640 return section
->reloc_count
;
5644 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
5646 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5647 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
5650 bfd_get_symcount (abfd
) = symcount
;
5655 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
5656 asymbol
**allocation
)
5658 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5659 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
5662 bfd_get_dynamic_symcount (abfd
) = symcount
;
5666 /* Return the size required for the dynamic reloc entries. Any
5667 section that was actually installed in the BFD, and has type
5668 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5669 considered to be a dynamic reloc section. */
5672 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
5677 if (elf_dynsymtab (abfd
) == 0)
5679 bfd_set_error (bfd_error_invalid_operation
);
5683 ret
= sizeof (arelent
*);
5684 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5685 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5686 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5687 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5688 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5689 * sizeof (arelent
*));
5694 /* Canonicalize the dynamic relocation entries. Note that we return
5695 the dynamic relocations as a single block, although they are
5696 actually associated with particular sections; the interface, which
5697 was designed for SunOS style shared libraries, expects that there
5698 is only one set of dynamic relocs. Any section that was actually
5699 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5700 the dynamic symbol table, is considered to be a dynamic reloc
5704 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
5708 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
5712 if (elf_dynsymtab (abfd
) == 0)
5714 bfd_set_error (bfd_error_invalid_operation
);
5718 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5720 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5722 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5723 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5724 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5729 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
5731 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5733 for (i
= 0; i
< count
; i
++)
5744 /* Read in the version information. */
5747 _bfd_elf_slurp_version_tables (bfd
*abfd
)
5749 bfd_byte
*contents
= NULL
;
5752 if (elf_dynverdef (abfd
) != 0)
5754 Elf_Internal_Shdr
*hdr
;
5755 Elf_External_Verdef
*everdef
;
5756 Elf_Internal_Verdef
*iverdef
;
5757 Elf_Internal_Verdef
*iverdefarr
;
5758 Elf_Internal_Verdef iverdefmem
;
5760 unsigned int maxidx
;
5762 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5764 contents
= bfd_malloc (hdr
->sh_size
);
5765 if (contents
== NULL
)
5767 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5768 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5771 /* We know the number of entries in the section but not the maximum
5772 index. Therefore we have to run through all entries and find
5774 everdef
= (Elf_External_Verdef
*) contents
;
5776 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5778 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5780 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5781 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5783 everdef
= ((Elf_External_Verdef
*)
5784 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5787 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5788 elf_tdata (abfd
)->verdef
= bfd_zalloc (abfd
, amt
);
5789 if (elf_tdata (abfd
)->verdef
== NULL
)
5792 elf_tdata (abfd
)->cverdefs
= maxidx
;
5794 everdef
= (Elf_External_Verdef
*) contents
;
5795 iverdefarr
= elf_tdata (abfd
)->verdef
;
5796 for (i
= 0; i
< hdr
->sh_info
; i
++)
5798 Elf_External_Verdaux
*everdaux
;
5799 Elf_Internal_Verdaux
*iverdaux
;
5802 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5804 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5805 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5807 iverdef
->vd_bfd
= abfd
;
5809 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5810 iverdef
->vd_auxptr
= bfd_alloc (abfd
, amt
);
5811 if (iverdef
->vd_auxptr
== NULL
)
5814 everdaux
= ((Elf_External_Verdaux
*)
5815 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5816 iverdaux
= iverdef
->vd_auxptr
;
5817 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5819 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5821 iverdaux
->vda_nodename
=
5822 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5823 iverdaux
->vda_name
);
5824 if (iverdaux
->vda_nodename
== NULL
)
5827 if (j
+ 1 < iverdef
->vd_cnt
)
5828 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5830 iverdaux
->vda_nextptr
= NULL
;
5832 everdaux
= ((Elf_External_Verdaux
*)
5833 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5836 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5838 if (i
+ 1 < hdr
->sh_info
)
5839 iverdef
->vd_nextdef
= iverdef
+ 1;
5841 iverdef
->vd_nextdef
= NULL
;
5843 everdef
= ((Elf_External_Verdef
*)
5844 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5851 if (elf_dynverref (abfd
) != 0)
5853 Elf_Internal_Shdr
*hdr
;
5854 Elf_External_Verneed
*everneed
;
5855 Elf_Internal_Verneed
*iverneed
;
5858 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5860 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5861 elf_tdata (abfd
)->verref
= bfd_zalloc (abfd
, amt
);
5862 if (elf_tdata (abfd
)->verref
== NULL
)
5865 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5867 contents
= bfd_malloc (hdr
->sh_size
);
5868 if (contents
== NULL
)
5870 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5871 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5874 everneed
= (Elf_External_Verneed
*) contents
;
5875 iverneed
= elf_tdata (abfd
)->verref
;
5876 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5878 Elf_External_Vernaux
*evernaux
;
5879 Elf_Internal_Vernaux
*ivernaux
;
5882 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5884 iverneed
->vn_bfd
= abfd
;
5886 iverneed
->vn_filename
=
5887 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5889 if (iverneed
->vn_filename
== NULL
)
5892 amt
= iverneed
->vn_cnt
;
5893 amt
*= sizeof (Elf_Internal_Vernaux
);
5894 iverneed
->vn_auxptr
= bfd_alloc (abfd
, amt
);
5896 evernaux
= ((Elf_External_Vernaux
*)
5897 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5898 ivernaux
= iverneed
->vn_auxptr
;
5899 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5901 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5903 ivernaux
->vna_nodename
=
5904 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5905 ivernaux
->vna_name
);
5906 if (ivernaux
->vna_nodename
== NULL
)
5909 if (j
+ 1 < iverneed
->vn_cnt
)
5910 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5912 ivernaux
->vna_nextptr
= NULL
;
5914 evernaux
= ((Elf_External_Vernaux
*)
5915 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5918 if (i
+ 1 < hdr
->sh_info
)
5919 iverneed
->vn_nextref
= iverneed
+ 1;
5921 iverneed
->vn_nextref
= NULL
;
5923 everneed
= ((Elf_External_Verneed
*)
5924 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5934 if (contents
!= NULL
)
5940 _bfd_elf_make_empty_symbol (bfd
*abfd
)
5942 elf_symbol_type
*newsym
;
5943 bfd_size_type amt
= sizeof (elf_symbol_type
);
5945 newsym
= bfd_zalloc (abfd
, amt
);
5950 newsym
->symbol
.the_bfd
= abfd
;
5951 return &newsym
->symbol
;
5956 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
5960 bfd_symbol_info (symbol
, ret
);
5963 /* Return whether a symbol name implies a local symbol. Most targets
5964 use this function for the is_local_label_name entry point, but some
5968 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
5971 /* Normal local symbols start with ``.L''. */
5972 if (name
[0] == '.' && name
[1] == 'L')
5975 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5976 DWARF debugging symbols starting with ``..''. */
5977 if (name
[0] == '.' && name
[1] == '.')
5980 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5981 emitting DWARF debugging output. I suspect this is actually a
5982 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5983 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5984 underscore to be emitted on some ELF targets). For ease of use,
5985 we treat such symbols as local. */
5986 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5993 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
5994 asymbol
*symbol ATTRIBUTE_UNUSED
)
6001 _bfd_elf_set_arch_mach (bfd
*abfd
,
6002 enum bfd_architecture arch
,
6003 unsigned long machine
)
6005 /* If this isn't the right architecture for this backend, and this
6006 isn't the generic backend, fail. */
6007 if (arch
!= get_elf_backend_data (abfd
)->arch
6008 && arch
!= bfd_arch_unknown
6009 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
6012 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
6015 /* Find the function to a particular section and offset,
6016 for error reporting. */
6019 elf_find_function (bfd
*abfd ATTRIBUTE_UNUSED
,
6023 const char **filename_ptr
,
6024 const char **functionname_ptr
)
6026 const char *filename
;
6035 for (p
= symbols
; *p
!= NULL
; p
++)
6039 q
= (elf_symbol_type
*) *p
;
6041 if (bfd_get_section (&q
->symbol
) != section
)
6044 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
6049 filename
= bfd_asymbol_name (&q
->symbol
);
6053 if (q
->symbol
.section
== section
6054 && q
->symbol
.value
>= low_func
6055 && q
->symbol
.value
<= offset
)
6057 func
= (asymbol
*) q
;
6058 low_func
= q
->symbol
.value
;
6068 *filename_ptr
= filename
;
6069 if (functionname_ptr
)
6070 *functionname_ptr
= bfd_asymbol_name (func
);
6075 /* Find the nearest line to a particular section and offset,
6076 for error reporting. */
6079 _bfd_elf_find_nearest_line (bfd
*abfd
,
6083 const char **filename_ptr
,
6084 const char **functionname_ptr
,
6085 unsigned int *line_ptr
)
6089 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
6090 filename_ptr
, functionname_ptr
,
6093 if (!*functionname_ptr
)
6094 elf_find_function (abfd
, section
, symbols
, offset
,
6095 *filename_ptr
? NULL
: filename_ptr
,
6101 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
6102 filename_ptr
, functionname_ptr
,
6104 &elf_tdata (abfd
)->dwarf2_find_line_info
))
6106 if (!*functionname_ptr
)
6107 elf_find_function (abfd
, section
, symbols
, offset
,
6108 *filename_ptr
? NULL
: filename_ptr
,
6114 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
6115 &found
, filename_ptr
,
6116 functionname_ptr
, line_ptr
,
6117 &elf_tdata (abfd
)->line_info
))
6119 if (found
&& (*functionname_ptr
|| *line_ptr
))
6122 if (symbols
== NULL
)
6125 if (! elf_find_function (abfd
, section
, symbols
, offset
,
6126 filename_ptr
, functionname_ptr
))
6134 _bfd_elf_sizeof_headers (bfd
*abfd
, bfd_boolean reloc
)
6138 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
6140 ret
+= get_program_header_size (abfd
);
6145 _bfd_elf_set_section_contents (bfd
*abfd
,
6147 const void *location
,
6149 bfd_size_type count
)
6151 Elf_Internal_Shdr
*hdr
;
6154 if (! abfd
->output_has_begun
6155 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6158 hdr
= &elf_section_data (section
)->this_hdr
;
6159 pos
= hdr
->sh_offset
+ offset
;
6160 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6161 || bfd_bwrite (location
, count
, abfd
) != count
)
6168 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
6169 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
6170 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
6175 /* Try to convert a non-ELF reloc into an ELF one. */
6178 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
6180 /* Check whether we really have an ELF howto. */
6182 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
6184 bfd_reloc_code_real_type code
;
6185 reloc_howto_type
*howto
;
6187 /* Alien reloc: Try to determine its type to replace it with an
6188 equivalent ELF reloc. */
6190 if (areloc
->howto
->pc_relative
)
6192 switch (areloc
->howto
->bitsize
)
6195 code
= BFD_RELOC_8_PCREL
;
6198 code
= BFD_RELOC_12_PCREL
;
6201 code
= BFD_RELOC_16_PCREL
;
6204 code
= BFD_RELOC_24_PCREL
;
6207 code
= BFD_RELOC_32_PCREL
;
6210 code
= BFD_RELOC_64_PCREL
;
6216 howto
= bfd_reloc_type_lookup (abfd
, code
);
6218 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
6220 if (howto
->pcrel_offset
)
6221 areloc
->addend
+= areloc
->address
;
6223 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
6228 switch (areloc
->howto
->bitsize
)
6234 code
= BFD_RELOC_14
;
6237 code
= BFD_RELOC_16
;
6240 code
= BFD_RELOC_26
;
6243 code
= BFD_RELOC_32
;
6246 code
= BFD_RELOC_64
;
6252 howto
= bfd_reloc_type_lookup (abfd
, code
);
6256 areloc
->howto
= howto
;
6264 (*_bfd_error_handler
)
6265 (_("%s: unsupported relocation type %s"),
6266 bfd_archive_filename (abfd
), areloc
->howto
->name
);
6267 bfd_set_error (bfd_error_bad_value
);
6272 _bfd_elf_close_and_cleanup (bfd
*abfd
)
6274 if (bfd_get_format (abfd
) == bfd_object
)
6276 if (elf_shstrtab (abfd
) != NULL
)
6277 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
6280 return _bfd_generic_close_and_cleanup (abfd
);
6283 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6284 in the relocation's offset. Thus we cannot allow any sort of sanity
6285 range-checking to interfere. There is nothing else to do in processing
6288 bfd_reloc_status_type
6289 _bfd_elf_rel_vtable_reloc_fn
6290 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
6291 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
6292 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
6293 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
6295 return bfd_reloc_ok
;
6298 /* Elf core file support. Much of this only works on native
6299 toolchains, since we rely on knowing the
6300 machine-dependent procfs structure in order to pick
6301 out details about the corefile. */
6303 #ifdef HAVE_SYS_PROCFS_H
6304 # include <sys/procfs.h>
6307 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6310 elfcore_make_pid (bfd
*abfd
)
6312 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
6313 + (elf_tdata (abfd
)->core_pid
));
6316 /* If there isn't a section called NAME, make one, using
6317 data from SECT. Note, this function will generate a
6318 reference to NAME, so you shouldn't deallocate or
6322 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
6326 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
6329 sect2
= bfd_make_section (abfd
, name
);
6333 sect2
->_raw_size
= sect
->_raw_size
;
6334 sect2
->filepos
= sect
->filepos
;
6335 sect2
->flags
= sect
->flags
;
6336 sect2
->alignment_power
= sect
->alignment_power
;
6340 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6341 actually creates up to two pseudosections:
6342 - For the single-threaded case, a section named NAME, unless
6343 such a section already exists.
6344 - For the multi-threaded case, a section named "NAME/PID", where
6345 PID is elfcore_make_pid (abfd).
6346 Both pseudosections have identical contents. */
6348 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
6354 char *threaded_name
;
6358 /* Build the section name. */
6360 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6361 len
= strlen (buf
) + 1;
6362 threaded_name
= bfd_alloc (abfd
, len
);
6363 if (threaded_name
== NULL
)
6365 memcpy (threaded_name
, buf
, len
);
6367 sect
= bfd_make_section_anyway (abfd
, threaded_name
);
6370 sect
->_raw_size
= size
;
6371 sect
->filepos
= filepos
;
6372 sect
->flags
= SEC_HAS_CONTENTS
;
6373 sect
->alignment_power
= 2;
6375 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6378 /* prstatus_t exists on:
6380 linux 2.[01] + glibc
6384 #if defined (HAVE_PRSTATUS_T)
6387 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6392 if (note
->descsz
== sizeof (prstatus_t
))
6396 raw_size
= sizeof (prstat
.pr_reg
);
6397 offset
= offsetof (prstatus_t
, pr_reg
);
6398 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6400 /* Do not overwrite the core signal if it
6401 has already been set by another thread. */
6402 if (elf_tdata (abfd
)->core_signal
== 0)
6403 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6404 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6406 /* pr_who exists on:
6409 pr_who doesn't exist on:
6412 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6413 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6416 #if defined (HAVE_PRSTATUS32_T)
6417 else if (note
->descsz
== sizeof (prstatus32_t
))
6419 /* 64-bit host, 32-bit corefile */
6420 prstatus32_t prstat
;
6422 raw_size
= sizeof (prstat
.pr_reg
);
6423 offset
= offsetof (prstatus32_t
, pr_reg
);
6424 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6426 /* Do not overwrite the core signal if it
6427 has already been set by another thread. */
6428 if (elf_tdata (abfd
)->core_signal
== 0)
6429 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6430 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6432 /* pr_who exists on:
6435 pr_who doesn't exist on:
6438 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6439 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6442 #endif /* HAVE_PRSTATUS32_T */
6445 /* Fail - we don't know how to handle any other
6446 note size (ie. data object type). */
6450 /* Make a ".reg/999" section and a ".reg" section. */
6451 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6452 raw_size
, note
->descpos
+ offset
);
6454 #endif /* defined (HAVE_PRSTATUS_T) */
6456 /* Create a pseudosection containing the exact contents of NOTE. */
6458 elfcore_make_note_pseudosection (bfd
*abfd
,
6460 Elf_Internal_Note
*note
)
6462 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6463 note
->descsz
, note
->descpos
);
6466 /* There isn't a consistent prfpregset_t across platforms,
6467 but it doesn't matter, because we don't have to pick this
6468 data structure apart. */
6471 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
6473 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6476 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6477 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6481 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
6483 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6486 #if defined (HAVE_PRPSINFO_T)
6487 typedef prpsinfo_t elfcore_psinfo_t
;
6488 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6489 typedef prpsinfo32_t elfcore_psinfo32_t
;
6493 #if defined (HAVE_PSINFO_T)
6494 typedef psinfo_t elfcore_psinfo_t
;
6495 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6496 typedef psinfo32_t elfcore_psinfo32_t
;
6500 /* return a malloc'ed copy of a string at START which is at
6501 most MAX bytes long, possibly without a terminating '\0'.
6502 the copy will always have a terminating '\0'. */
6505 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
6508 char *end
= memchr (start
, '\0', max
);
6516 dups
= bfd_alloc (abfd
, len
+ 1);
6520 memcpy (dups
, start
, len
);
6526 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6528 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
6530 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6532 elfcore_psinfo_t psinfo
;
6534 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6536 elf_tdata (abfd
)->core_program
6537 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6538 sizeof (psinfo
.pr_fname
));
6540 elf_tdata (abfd
)->core_command
6541 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6542 sizeof (psinfo
.pr_psargs
));
6544 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6545 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6547 /* 64-bit host, 32-bit corefile */
6548 elfcore_psinfo32_t psinfo
;
6550 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6552 elf_tdata (abfd
)->core_program
6553 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6554 sizeof (psinfo
.pr_fname
));
6556 elf_tdata (abfd
)->core_command
6557 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6558 sizeof (psinfo
.pr_psargs
));
6564 /* Fail - we don't know how to handle any other
6565 note size (ie. data object type). */
6569 /* Note that for some reason, a spurious space is tacked
6570 onto the end of the args in some (at least one anyway)
6571 implementations, so strip it off if it exists. */
6574 char *command
= elf_tdata (abfd
)->core_command
;
6575 int n
= strlen (command
);
6577 if (0 < n
&& command
[n
- 1] == ' ')
6578 command
[n
- 1] = '\0';
6583 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6585 #if defined (HAVE_PSTATUS_T)
6587 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6589 if (note
->descsz
== sizeof (pstatus_t
)
6590 #if defined (HAVE_PXSTATUS_T)
6591 || note
->descsz
== sizeof (pxstatus_t
)
6597 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6599 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6601 #if defined (HAVE_PSTATUS32_T)
6602 else if (note
->descsz
== sizeof (pstatus32_t
))
6604 /* 64-bit host, 32-bit corefile */
6607 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6609 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6612 /* Could grab some more details from the "representative"
6613 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6614 NT_LWPSTATUS note, presumably. */
6618 #endif /* defined (HAVE_PSTATUS_T) */
6620 #if defined (HAVE_LWPSTATUS_T)
6622 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6624 lwpstatus_t lwpstat
;
6630 if (note
->descsz
!= sizeof (lwpstat
)
6631 #if defined (HAVE_LWPXSTATUS_T)
6632 && note
->descsz
!= sizeof (lwpxstatus_t
)
6637 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6639 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6640 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6642 /* Make a ".reg/999" section. */
6644 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6645 len
= strlen (buf
) + 1;
6646 name
= bfd_alloc (abfd
, len
);
6649 memcpy (name
, buf
, len
);
6651 sect
= bfd_make_section_anyway (abfd
, name
);
6655 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6656 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6657 sect
->filepos
= note
->descpos
6658 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6661 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6662 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6663 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6666 sect
->flags
= SEC_HAS_CONTENTS
;
6667 sect
->alignment_power
= 2;
6669 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6672 /* Make a ".reg2/999" section */
6674 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6675 len
= strlen (buf
) + 1;
6676 name
= bfd_alloc (abfd
, len
);
6679 memcpy (name
, buf
, len
);
6681 sect
= bfd_make_section_anyway (abfd
, name
);
6685 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6686 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6687 sect
->filepos
= note
->descpos
6688 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6691 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6692 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6693 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6696 sect
->flags
= SEC_HAS_CONTENTS
;
6697 sect
->alignment_power
= 2;
6699 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6701 #endif /* defined (HAVE_LWPSTATUS_T) */
6703 #if defined (HAVE_WIN32_PSTATUS_T)
6705 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6711 win32_pstatus_t pstatus
;
6713 if (note
->descsz
< sizeof (pstatus
))
6716 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
6718 switch (pstatus
.data_type
)
6720 case NOTE_INFO_PROCESS
:
6721 /* FIXME: need to add ->core_command. */
6722 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6723 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6726 case NOTE_INFO_THREAD
:
6727 /* Make a ".reg/999" section. */
6728 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6730 len
= strlen (buf
) + 1;
6731 name
= bfd_alloc (abfd
, len
);
6735 memcpy (name
, buf
, len
);
6737 sect
= bfd_make_section_anyway (abfd
, name
);
6741 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6742 sect
->filepos
= (note
->descpos
6743 + offsetof (struct win32_pstatus
,
6744 data
.thread_info
.thread_context
));
6745 sect
->flags
= SEC_HAS_CONTENTS
;
6746 sect
->alignment_power
= 2;
6748 if (pstatus
.data
.thread_info
.is_active_thread
)
6749 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6753 case NOTE_INFO_MODULE
:
6754 /* Make a ".module/xxxxxxxx" section. */
6755 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6757 len
= strlen (buf
) + 1;
6758 name
= bfd_alloc (abfd
, len
);
6762 memcpy (name
, buf
, len
);
6764 sect
= bfd_make_section_anyway (abfd
, name
);
6769 sect
->_raw_size
= note
->descsz
;
6770 sect
->filepos
= note
->descpos
;
6771 sect
->flags
= SEC_HAS_CONTENTS
;
6772 sect
->alignment_power
= 2;
6781 #endif /* HAVE_WIN32_PSTATUS_T */
6784 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
6786 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6794 if (bed
->elf_backend_grok_prstatus
)
6795 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6797 #if defined (HAVE_PRSTATUS_T)
6798 return elfcore_grok_prstatus (abfd
, note
);
6803 #if defined (HAVE_PSTATUS_T)
6805 return elfcore_grok_pstatus (abfd
, note
);
6808 #if defined (HAVE_LWPSTATUS_T)
6810 return elfcore_grok_lwpstatus (abfd
, note
);
6813 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6814 return elfcore_grok_prfpreg (abfd
, note
);
6816 #if defined (HAVE_WIN32_PSTATUS_T)
6817 case NT_WIN32PSTATUS
:
6818 return elfcore_grok_win32pstatus (abfd
, note
);
6821 case NT_PRXFPREG
: /* Linux SSE extension */
6822 if (note
->namesz
== 6
6823 && strcmp (note
->namedata
, "LINUX") == 0)
6824 return elfcore_grok_prxfpreg (abfd
, note
);
6830 if (bed
->elf_backend_grok_psinfo
)
6831 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6833 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6834 return elfcore_grok_psinfo (abfd
, note
);
6841 asection
*sect
= bfd_make_section_anyway (abfd
, ".auxv");
6845 sect
->_raw_size
= note
->descsz
;
6846 sect
->filepos
= note
->descpos
;
6847 sect
->flags
= SEC_HAS_CONTENTS
;
6848 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
6856 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
6860 cp
= strchr (note
->namedata
, '@');
6863 *lwpidp
= atoi(cp
+ 1);
6870 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
6873 /* Signal number at offset 0x08. */
6874 elf_tdata (abfd
)->core_signal
6875 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6877 /* Process ID at offset 0x50. */
6878 elf_tdata (abfd
)->core_pid
6879 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6881 /* Command name at 0x7c (max 32 bytes, including nul). */
6882 elf_tdata (abfd
)->core_command
6883 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6885 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
6890 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
6894 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6895 elf_tdata (abfd
)->core_lwpid
= lwp
;
6897 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6899 /* NetBSD-specific core "procinfo". Note that we expect to
6900 find this note before any of the others, which is fine,
6901 since the kernel writes this note out first when it
6902 creates a core file. */
6904 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6907 /* As of Jan 2002 there are no other machine-independent notes
6908 defined for NetBSD core files. If the note type is less
6909 than the start of the machine-dependent note types, we don't
6912 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
6916 switch (bfd_get_arch (abfd
))
6918 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6919 PT_GETFPREGS == mach+2. */
6921 case bfd_arch_alpha
:
6922 case bfd_arch_sparc
:
6925 case NT_NETBSDCORE_FIRSTMACH
+0:
6926 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6928 case NT_NETBSDCORE_FIRSTMACH
+2:
6929 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6935 /* On all other arch's, PT_GETREGS == mach+1 and
6936 PT_GETFPREGS == mach+3. */
6941 case NT_NETBSDCORE_FIRSTMACH
+1:
6942 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6944 case NT_NETBSDCORE_FIRSTMACH
+3:
6945 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6955 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, pid_t
*tid
)
6957 void *ddata
= note
->descdata
;
6964 /* nto_procfs_status 'pid' field is at offset 0. */
6965 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
6967 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
6968 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
6970 /* nto_procfs_status 'flags' field is at offset 8. */
6971 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
6973 /* nto_procfs_status 'what' field is at offset 14. */
6974 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
6976 elf_tdata (abfd
)->core_signal
= sig
;
6977 elf_tdata (abfd
)->core_lwpid
= *tid
;
6980 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
6981 do not come from signals so we make sure we set the current
6982 thread just in case. */
6983 if (flags
& 0x00000080)
6984 elf_tdata (abfd
)->core_lwpid
= *tid
;
6986 /* Make a ".qnx_core_status/%d" section. */
6987 sprintf (buf
, ".qnx_core_status/%d", *tid
);
6989 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
6994 sect
= bfd_make_section_anyway (abfd
, name
);
6998 sect
->_raw_size
= note
->descsz
;
6999 sect
->filepos
= note
->descpos
;
7000 sect
->flags
= SEC_HAS_CONTENTS
;
7001 sect
->alignment_power
= 2;
7003 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
7007 elfcore_grok_nto_gregs (bfd
*abfd
, Elf_Internal_Note
*note
, pid_t tid
)
7013 /* Make a ".reg/%d" section. */
7014 sprintf (buf
, ".reg/%d", tid
);
7016 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
7021 sect
= bfd_make_section_anyway (abfd
, name
);
7025 sect
->_raw_size
= note
->descsz
;
7026 sect
->filepos
= note
->descpos
;
7027 sect
->flags
= SEC_HAS_CONTENTS
;
7028 sect
->alignment_power
= 2;
7030 /* This is the current thread. */
7031 if (elf_tdata (abfd
)->core_lwpid
== tid
)
7032 return elfcore_maybe_make_sect (abfd
, ".reg", sect
);
7037 #define BFD_QNT_CORE_INFO 7
7038 #define BFD_QNT_CORE_STATUS 8
7039 #define BFD_QNT_CORE_GREG 9
7040 #define BFD_QNT_CORE_FPREG 10
7043 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7045 /* Every GREG section has a STATUS section before it. Store the
7046 tid from the previous call to pass down to the next gregs
7048 static pid_t tid
= 1;
7052 case BFD_QNT_CORE_INFO
: return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
7053 case BFD_QNT_CORE_STATUS
: return elfcore_grok_nto_status (abfd
, note
, &tid
);
7054 case BFD_QNT_CORE_GREG
: return elfcore_grok_nto_gregs (abfd
, note
, tid
);
7055 case BFD_QNT_CORE_FPREG
: return elfcore_grok_prfpreg (abfd
, note
);
7056 default: return TRUE
;
7060 /* Function: elfcore_write_note
7067 size of data for note
7070 End of buffer containing note. */
7073 elfcore_write_note (bfd
*abfd
,
7081 Elf_External_Note
*xnp
;
7091 const struct elf_backend_data
*bed
;
7093 namesz
= strlen (name
) + 1;
7094 bed
= get_elf_backend_data (abfd
);
7095 pad
= -namesz
& ((1 << bed
->s
->log_file_align
) - 1);
7098 newspace
= 12 + namesz
+ pad
+ size
;
7100 p
= realloc (buf
, *bufsiz
+ newspace
);
7102 *bufsiz
+= newspace
;
7103 xnp
= (Elf_External_Note
*) dest
;
7104 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
7105 H_PUT_32 (abfd
, size
, xnp
->descsz
);
7106 H_PUT_32 (abfd
, type
, xnp
->type
);
7110 memcpy (dest
, name
, namesz
);
7118 memcpy (dest
, input
, size
);
7122 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7124 elfcore_write_prpsinfo (bfd
*abfd
,
7131 char *note_name
= "CORE";
7133 #if defined (HAVE_PSINFO_T)
7135 note_type
= NT_PSINFO
;
7138 note_type
= NT_PRPSINFO
;
7141 memset (&data
, 0, sizeof (data
));
7142 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
7143 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
7144 return elfcore_write_note (abfd
, buf
, bufsiz
,
7145 note_name
, note_type
, &data
, sizeof (data
));
7147 #endif /* PSINFO_T or PRPSINFO_T */
7149 #if defined (HAVE_PRSTATUS_T)
7151 elfcore_write_prstatus (bfd
*abfd
,
7159 char *note_name
= "CORE";
7161 memset (&prstat
, 0, sizeof (prstat
));
7162 prstat
.pr_pid
= pid
;
7163 prstat
.pr_cursig
= cursig
;
7164 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
7165 return elfcore_write_note (abfd
, buf
, bufsiz
,
7166 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
7168 #endif /* HAVE_PRSTATUS_T */
7170 #if defined (HAVE_LWPSTATUS_T)
7172 elfcore_write_lwpstatus (bfd
*abfd
,
7179 lwpstatus_t lwpstat
;
7180 char *note_name
= "CORE";
7182 memset (&lwpstat
, 0, sizeof (lwpstat
));
7183 lwpstat
.pr_lwpid
= pid
>> 16;
7184 lwpstat
.pr_cursig
= cursig
;
7185 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7186 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
7187 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7189 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
7190 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
7192 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
7193 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
7196 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7197 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
7199 #endif /* HAVE_LWPSTATUS_T */
7201 #if defined (HAVE_PSTATUS_T)
7203 elfcore_write_pstatus (bfd
*abfd
,
7211 char *note_name
= "CORE";
7213 memset (&pstat
, 0, sizeof (pstat
));
7214 pstat
.pr_pid
= pid
& 0xffff;
7215 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7216 NT_PSTATUS
, &pstat
, sizeof (pstat
));
7219 #endif /* HAVE_PSTATUS_T */
7222 elfcore_write_prfpreg (bfd
*abfd
,
7228 char *note_name
= "CORE";
7229 return elfcore_write_note (abfd
, buf
, bufsiz
,
7230 note_name
, NT_FPREGSET
, fpregs
, size
);
7234 elfcore_write_prxfpreg (bfd
*abfd
,
7237 const void *xfpregs
,
7240 char *note_name
= "LINUX";
7241 return elfcore_write_note (abfd
, buf
, bufsiz
,
7242 note_name
, NT_PRXFPREG
, xfpregs
, size
);
7246 elfcore_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
7254 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
7257 buf
= bfd_malloc (size
);
7261 if (bfd_bread (buf
, size
, abfd
) != size
)
7269 while (p
< buf
+ size
)
7271 /* FIXME: bad alignment assumption. */
7272 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
7273 Elf_Internal_Note in
;
7275 in
.type
= H_GET_32 (abfd
, xnp
->type
);
7277 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
7278 in
.namedata
= xnp
->name
;
7280 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
7281 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
7282 in
.descpos
= offset
+ (in
.descdata
- buf
);
7284 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
7286 if (! elfcore_grok_netbsd_note (abfd
, &in
))
7289 else if (strncmp (in
.namedata
, "QNX", 3) == 0)
7291 if (! elfcore_grok_nto_note (abfd
, &in
))
7296 if (! elfcore_grok_note (abfd
, &in
))
7300 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
7307 /* Providing external access to the ELF program header table. */
7309 /* Return an upper bound on the number of bytes required to store a
7310 copy of ABFD's program header table entries. Return -1 if an error
7311 occurs; bfd_get_error will return an appropriate code. */
7314 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
7316 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7318 bfd_set_error (bfd_error_wrong_format
);
7322 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
7325 /* Copy ABFD's program header table entries to *PHDRS. The entries
7326 will be stored as an array of Elf_Internal_Phdr structures, as
7327 defined in include/elf/internal.h. To find out how large the
7328 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7330 Return the number of program header table entries read, or -1 if an
7331 error occurs; bfd_get_error will return an appropriate code. */
7334 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
7338 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7340 bfd_set_error (bfd_error_wrong_format
);
7344 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
7345 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
7346 num_phdrs
* sizeof (Elf_Internal_Phdr
));
7352 _bfd_elf_sprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, char *buf
, bfd_vma value
)
7355 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7357 i_ehdrp
= elf_elfheader (abfd
);
7358 if (i_ehdrp
== NULL
)
7359 sprintf_vma (buf
, value
);
7362 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7364 #if BFD_HOST_64BIT_LONG
7365 sprintf (buf
, "%016lx", value
);
7367 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
7368 _bfd_int64_low (value
));
7372 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
7375 sprintf_vma (buf
, value
);
7380 _bfd_elf_fprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, void *stream
, bfd_vma value
)
7383 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7385 i_ehdrp
= elf_elfheader (abfd
);
7386 if (i_ehdrp
== NULL
)
7387 fprintf_vma ((FILE *) stream
, value
);
7390 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7392 #if BFD_HOST_64BIT_LONG
7393 fprintf ((FILE *) stream
, "%016lx", value
);
7395 fprintf ((FILE *) stream
, "%08lx%08lx",
7396 _bfd_int64_high (value
), _bfd_int64_low (value
));
7400 fprintf ((FILE *) stream
, "%08lx",
7401 (unsigned long) (value
& 0xffffffff));
7404 fprintf_vma ((FILE *) stream
, value
);
7408 enum elf_reloc_type_class
7409 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
7411 return reloc_class_normal
;
7414 /* For RELA architectures, return the relocation value for a
7415 relocation against a local symbol. */
7418 _bfd_elf_rela_local_sym (bfd
*abfd
,
7419 Elf_Internal_Sym
*sym
,
7421 Elf_Internal_Rela
*rel
)
7423 asection
*sec
= *psec
;
7426 relocation
= (sec
->output_section
->vma
7427 + sec
->output_offset
7429 if ((sec
->flags
& SEC_MERGE
)
7430 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
7431 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
7434 _bfd_merged_section_offset (abfd
, psec
,
7435 elf_section_data (sec
)->sec_info
,
7436 sym
->st_value
+ rel
->r_addend
,
7439 rel
->r_addend
-= relocation
;
7440 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
7446 _bfd_elf_rel_local_sym (bfd
*abfd
,
7447 Elf_Internal_Sym
*sym
,
7451 asection
*sec
= *psec
;
7453 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7454 return sym
->st_value
+ addend
;
7456 return _bfd_merged_section_offset (abfd
, psec
,
7457 elf_section_data (sec
)->sec_info
,
7458 sym
->st_value
+ addend
, 0);
7462 _bfd_elf_section_offset (bfd
*abfd
,
7463 struct bfd_link_info
*info
,
7467 struct bfd_elf_section_data
*sec_data
;
7469 sec_data
= elf_section_data (sec
);
7470 switch (sec
->sec_info_type
)
7472 case ELF_INFO_TYPE_STABS
:
7473 return _bfd_stab_section_offset (abfd
,
7474 &elf_hash_table (info
)->merge_info
,
7475 sec
, &sec_data
->sec_info
, offset
);
7476 case ELF_INFO_TYPE_EH_FRAME
:
7477 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);
7483 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7484 reconstruct an ELF file by reading the segments out of remote memory
7485 based on the ELF file header at EHDR_VMA and the ELF program headers it
7486 points to. If not null, *LOADBASEP is filled in with the difference
7487 between the VMAs from which the segments were read, and the VMAs the
7488 file headers (and hence BFD's idea of each section's VMA) put them at.
7490 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7491 remote memory at target address VMA into the local buffer at MYADDR; it
7492 should return zero on success or an `errno' code on failure. TEMPL must
7493 be a BFD for an ELF target with the word size and byte order found in
7494 the remote memory. */
7497 bfd_elf_bfd_from_remote_memory
7501 int (*target_read_memory
) (bfd_vma
, char *, int))
7503 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
7504 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);