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_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
618 return elf_next_in_group (sec
) != NULL
;
622 bfd_elf_discard_group (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*group
)
624 asection
*first
= elf_next_in_group (group
);
629 s
->output_section
= bfd_abs_section_ptr
;
630 s
= elf_next_in_group (s
);
631 /* These lists are circular. */
638 /* Make a BFD section from an ELF section. We store a pointer to the
639 BFD section in the bfd_section field of the header. */
642 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
643 Elf_Internal_Shdr
*hdr
,
648 const struct elf_backend_data
*bed
;
650 if (hdr
->bfd_section
!= NULL
)
652 BFD_ASSERT (strcmp (name
,
653 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
657 newsect
= bfd_make_section_anyway (abfd
, name
);
661 hdr
->bfd_section
= newsect
;
662 elf_section_data (newsect
)->this_hdr
= *hdr
;
664 /* Always use the real type/flags. */
665 elf_section_type (newsect
) = hdr
->sh_type
;
666 elf_section_flags (newsect
) = hdr
->sh_flags
;
668 newsect
->filepos
= hdr
->sh_offset
;
670 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
671 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
672 || ! bfd_set_section_alignment (abfd
, newsect
,
673 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
676 flags
= SEC_NO_FLAGS
;
677 if (hdr
->sh_type
!= SHT_NOBITS
)
678 flags
|= SEC_HAS_CONTENTS
;
679 if (hdr
->sh_type
== SHT_GROUP
)
680 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
681 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
684 if (hdr
->sh_type
!= SHT_NOBITS
)
687 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
688 flags
|= SEC_READONLY
;
689 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
691 else if ((flags
& SEC_LOAD
) != 0)
693 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
696 newsect
->entsize
= hdr
->sh_entsize
;
697 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
698 flags
|= SEC_STRINGS
;
700 if (hdr
->sh_flags
& SHF_GROUP
)
701 if (!setup_group (abfd
, hdr
, newsect
))
703 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
704 flags
|= SEC_THREAD_LOCAL
;
706 /* The debugging sections appear to be recognized only by name, not
709 static const char *debug_sec_names
[] =
718 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
719 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
723 flags
|= SEC_DEBUGGING
;
726 /* As a GNU extension, if the name begins with .gnu.linkonce, we
727 only link a single copy of the section. This is used to support
728 g++. g++ will emit each template expansion in its own section.
729 The symbols will be defined as weak, so that multiple definitions
730 are permitted. The GNU linker extension is to actually discard
731 all but one of the sections. */
732 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
733 && elf_next_in_group (newsect
) == NULL
)
734 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
736 bed
= get_elf_backend_data (abfd
);
737 if (bed
->elf_backend_section_flags
)
738 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
741 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
744 if ((flags
& SEC_ALLOC
) != 0)
746 Elf_Internal_Phdr
*phdr
;
749 /* Look through the phdrs to see if we need to adjust the lma.
750 If all the p_paddr fields are zero, we ignore them, since
751 some ELF linkers produce such output. */
752 phdr
= elf_tdata (abfd
)->phdr
;
753 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
755 if (phdr
->p_paddr
!= 0)
758 if (i
< elf_elfheader (abfd
)->e_phnum
)
760 phdr
= elf_tdata (abfd
)->phdr
;
761 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
763 /* This section is part of this segment if its file
764 offset plus size lies within the segment's memory
765 span and, if the section is loaded, the extent of the
766 loaded data lies within the extent of the segment.
768 Note - we used to check the p_paddr field as well, and
769 refuse to set the LMA if it was 0. This is wrong
770 though, as a perfectly valid initialised segment can
771 have a p_paddr of zero. Some architectures, eg ARM,
772 place special significance on the address 0 and
773 executables need to be able to have a segment which
774 covers this address. */
775 if (phdr
->p_type
== PT_LOAD
776 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
777 && (hdr
->sh_offset
+ hdr
->sh_size
778 <= phdr
->p_offset
+ phdr
->p_memsz
)
779 && ((flags
& SEC_LOAD
) == 0
780 || (hdr
->sh_offset
+ hdr
->sh_size
781 <= phdr
->p_offset
+ phdr
->p_filesz
)))
783 if ((flags
& SEC_LOAD
) == 0)
784 newsect
->lma
= (phdr
->p_paddr
785 + hdr
->sh_addr
- phdr
->p_vaddr
);
787 /* We used to use the same adjustment for SEC_LOAD
788 sections, but that doesn't work if the segment
789 is packed with code from multiple VMAs.
790 Instead we calculate the section LMA based on
791 the segment LMA. It is assumed that the
792 segment will contain sections with contiguous
793 LMAs, even if the VMAs are not. */
794 newsect
->lma
= (phdr
->p_paddr
795 + hdr
->sh_offset
- phdr
->p_offset
);
797 /* With contiguous segments, we can't tell from file
798 offsets whether a section with zero size should
799 be placed at the end of one segment or the
800 beginning of the next. Decide based on vaddr. */
801 if (hdr
->sh_addr
>= phdr
->p_vaddr
802 && (hdr
->sh_addr
+ hdr
->sh_size
803 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
818 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
821 Helper functions for GDB to locate the string tables.
822 Since BFD hides string tables from callers, GDB needs to use an
823 internal hook to find them. Sun's .stabstr, in particular,
824 isn't even pointed to by the .stab section, so ordinary
825 mechanisms wouldn't work to find it, even if we had some.
828 struct elf_internal_shdr
*
829 bfd_elf_find_section (bfd
*abfd
, char *name
)
831 Elf_Internal_Shdr
**i_shdrp
;
836 i_shdrp
= elf_elfsections (abfd
);
839 shstrtab
= bfd_elf_get_str_section (abfd
,
840 elf_elfheader (abfd
)->e_shstrndx
);
841 if (shstrtab
!= NULL
)
843 max
= elf_numsections (abfd
);
844 for (i
= 1; i
< max
; i
++)
845 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
852 const char *const bfd_elf_section_type_names
[] = {
853 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
854 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
855 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
858 /* ELF relocs are against symbols. If we are producing relocatable
859 output, and the reloc is against an external symbol, and nothing
860 has given us any additional addend, the resulting reloc will also
861 be against the same symbol. In such a case, we don't want to
862 change anything about the way the reloc is handled, since it will
863 all be done at final link time. Rather than put special case code
864 into bfd_perform_relocation, all the reloc types use this howto
865 function. It just short circuits the reloc if producing
866 relocatable output against an external symbol. */
868 bfd_reloc_status_type
869 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
870 arelent
*reloc_entry
,
872 void *data ATTRIBUTE_UNUSED
,
873 asection
*input_section
,
875 char **error_message ATTRIBUTE_UNUSED
)
877 if (output_bfd
!= NULL
878 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
879 && (! reloc_entry
->howto
->partial_inplace
880 || reloc_entry
->addend
== 0))
882 reloc_entry
->address
+= input_section
->output_offset
;
886 return bfd_reloc_continue
;
889 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
892 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
895 BFD_ASSERT (sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
896 sec
->sec_info_type
= ELF_INFO_TYPE_NONE
;
899 /* Finish SHF_MERGE section merging. */
902 _bfd_elf_merge_sections (bfd
*abfd
, struct bfd_link_info
*info
)
904 if (!is_elf_hash_table (info
->hash
))
906 if (elf_hash_table (info
)->merge_info
)
907 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
,
908 merge_sections_remove_hook
);
913 _bfd_elf_link_just_syms (asection
*sec
, struct bfd_link_info
*info
)
915 sec
->output_section
= bfd_abs_section_ptr
;
916 sec
->output_offset
= sec
->vma
;
917 if (!is_elf_hash_table (info
->hash
))
920 sec
->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
923 /* Copy the program header and other data from one object module to
927 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
929 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
930 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
933 BFD_ASSERT (!elf_flags_init (obfd
)
934 || (elf_elfheader (obfd
)->e_flags
935 == elf_elfheader (ibfd
)->e_flags
));
937 elf_gp (obfd
) = elf_gp (ibfd
);
938 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
939 elf_flags_init (obfd
) = TRUE
;
943 /* Print out the program headers. */
946 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
949 Elf_Internal_Phdr
*p
;
951 bfd_byte
*dynbuf
= NULL
;
953 p
= elf_tdata (abfd
)->phdr
;
958 fprintf (f
, _("\nProgram Header:\n"));
959 c
= elf_elfheader (abfd
)->e_phnum
;
960 for (i
= 0; i
< c
; i
++, p
++)
967 case PT_NULL
: pt
= "NULL"; break;
968 case PT_LOAD
: pt
= "LOAD"; break;
969 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
970 case PT_INTERP
: pt
= "INTERP"; break;
971 case PT_NOTE
: pt
= "NOTE"; break;
972 case PT_SHLIB
: pt
= "SHLIB"; break;
973 case PT_PHDR
: pt
= "PHDR"; break;
974 case PT_TLS
: pt
= "TLS"; break;
975 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
976 case PT_GNU_STACK
: pt
= "STACK"; break;
977 case PT_GNU_RELRO
: pt
= "RELRO"; break;
978 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
980 fprintf (f
, "%8s off 0x", pt
);
981 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
982 fprintf (f
, " vaddr 0x");
983 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
984 fprintf (f
, " paddr 0x");
985 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
986 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
987 fprintf (f
, " filesz 0x");
988 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
989 fprintf (f
, " memsz 0x");
990 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
991 fprintf (f
, " flags %c%c%c",
992 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
993 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
994 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
995 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
996 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1001 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1005 unsigned long shlink
;
1006 bfd_byte
*extdyn
, *extdynend
;
1008 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1010 fprintf (f
, _("\nDynamic Section:\n"));
1012 dynbuf
= bfd_malloc (s
->_raw_size
);
1015 if (! bfd_get_section_contents (abfd
, s
, dynbuf
, 0, s
->_raw_size
))
1018 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1021 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1023 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1024 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1027 extdynend
= extdyn
+ s
->_raw_size
;
1028 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1030 Elf_Internal_Dyn dyn
;
1033 bfd_boolean stringp
;
1035 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1037 if (dyn
.d_tag
== DT_NULL
)
1044 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1048 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1049 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1050 case DT_PLTGOT
: name
= "PLTGOT"; break;
1051 case DT_HASH
: name
= "HASH"; break;
1052 case DT_STRTAB
: name
= "STRTAB"; break;
1053 case DT_SYMTAB
: name
= "SYMTAB"; break;
1054 case DT_RELA
: name
= "RELA"; break;
1055 case DT_RELASZ
: name
= "RELASZ"; break;
1056 case DT_RELAENT
: name
= "RELAENT"; break;
1057 case DT_STRSZ
: name
= "STRSZ"; break;
1058 case DT_SYMENT
: name
= "SYMENT"; break;
1059 case DT_INIT
: name
= "INIT"; break;
1060 case DT_FINI
: name
= "FINI"; break;
1061 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1062 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1063 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1064 case DT_REL
: name
= "REL"; break;
1065 case DT_RELSZ
: name
= "RELSZ"; break;
1066 case DT_RELENT
: name
= "RELENT"; break;
1067 case DT_PLTREL
: name
= "PLTREL"; break;
1068 case DT_DEBUG
: name
= "DEBUG"; break;
1069 case DT_TEXTREL
: name
= "TEXTREL"; break;
1070 case DT_JMPREL
: name
= "JMPREL"; break;
1071 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1072 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1073 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1074 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1075 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1076 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1077 case DT_FLAGS
: name
= "FLAGS"; break;
1078 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1079 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1080 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1081 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1082 case DT_MOVEENT
: name
= "MOVEENT"; break;
1083 case DT_MOVESZ
: name
= "MOVESZ"; break;
1084 case DT_FEATURE
: name
= "FEATURE"; break;
1085 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1086 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1087 case DT_SYMINENT
: name
= "SYMINENT"; break;
1088 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1089 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1090 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1091 case DT_PLTPAD
: name
= "PLTPAD"; break;
1092 case DT_MOVETAB
: name
= "MOVETAB"; break;
1093 case DT_SYMINFO
: name
= "SYMINFO"; break;
1094 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1095 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1096 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1097 case DT_VERSYM
: name
= "VERSYM"; break;
1098 case DT_VERDEF
: name
= "VERDEF"; break;
1099 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1100 case DT_VERNEED
: name
= "VERNEED"; break;
1101 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1102 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1103 case DT_USED
: name
= "USED"; break;
1104 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1107 fprintf (f
, " %-11s ", name
);
1109 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1113 unsigned int tagv
= dyn
.d_un
.d_val
;
1115 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1118 fprintf (f
, "%s", string
);
1127 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1128 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1130 if (! _bfd_elf_slurp_version_tables (abfd
))
1134 if (elf_dynverdef (abfd
) != 0)
1136 Elf_Internal_Verdef
*t
;
1138 fprintf (f
, _("\nVersion definitions:\n"));
1139 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1141 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1142 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1143 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1145 Elf_Internal_Verdaux
*a
;
1148 for (a
= t
->vd_auxptr
->vda_nextptr
;
1151 fprintf (f
, "%s ", a
->vda_nodename
);
1157 if (elf_dynverref (abfd
) != 0)
1159 Elf_Internal_Verneed
*t
;
1161 fprintf (f
, _("\nVersion References:\n"));
1162 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1164 Elf_Internal_Vernaux
*a
;
1166 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1167 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1168 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1169 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1181 /* Display ELF-specific fields of a symbol. */
1184 bfd_elf_print_symbol (bfd
*abfd
,
1187 bfd_print_symbol_type how
)
1192 case bfd_print_symbol_name
:
1193 fprintf (file
, "%s", symbol
->name
);
1195 case bfd_print_symbol_more
:
1196 fprintf (file
, "elf ");
1197 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1198 fprintf (file
, " %lx", (long) symbol
->flags
);
1200 case bfd_print_symbol_all
:
1202 const char *section_name
;
1203 const char *name
= NULL
;
1204 const struct elf_backend_data
*bed
;
1205 unsigned char st_other
;
1208 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1210 bed
= get_elf_backend_data (abfd
);
1211 if (bed
->elf_backend_print_symbol_all
)
1212 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1216 name
= symbol
->name
;
1217 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1220 fprintf (file
, " %s\t", section_name
);
1221 /* Print the "other" value for a symbol. For common symbols,
1222 we've already printed the size; now print the alignment.
1223 For other symbols, we have no specified alignment, and
1224 we've printed the address; now print the size. */
1225 if (bfd_is_com_section (symbol
->section
))
1226 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1228 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1229 bfd_fprintf_vma (abfd
, file
, val
);
1231 /* If we have version information, print it. */
1232 if (elf_tdata (abfd
)->dynversym_section
!= 0
1233 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1234 || elf_tdata (abfd
)->dynverref_section
!= 0))
1236 unsigned int vernum
;
1237 const char *version_string
;
1239 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1242 version_string
= "";
1243 else if (vernum
== 1)
1244 version_string
= "Base";
1245 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1247 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1250 Elf_Internal_Verneed
*t
;
1252 version_string
= "";
1253 for (t
= elf_tdata (abfd
)->verref
;
1257 Elf_Internal_Vernaux
*a
;
1259 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1261 if (a
->vna_other
== vernum
)
1263 version_string
= a
->vna_nodename
;
1270 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1271 fprintf (file
, " %-11s", version_string
);
1276 fprintf (file
, " (%s)", version_string
);
1277 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1282 /* If the st_other field is not zero, print it. */
1283 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1288 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1289 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1290 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1292 /* Some other non-defined flags are also present, so print
1294 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1297 fprintf (file
, " %s", name
);
1303 /* Create an entry in an ELF linker hash table. */
1305 struct bfd_hash_entry
*
1306 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
1307 struct bfd_hash_table
*table
,
1310 /* Allocate the structure if it has not already been allocated by a
1314 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1319 /* Call the allocation method of the superclass. */
1320 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1323 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1324 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1326 /* Set local fields. */
1329 ret
->dynstr_index
= 0;
1330 ret
->elf_hash_value
= 0;
1331 ret
->weakdef
= NULL
;
1332 ret
->verinfo
.verdef
= NULL
;
1333 ret
->vtable_entries_size
= 0;
1334 ret
->vtable_entries_used
= NULL
;
1335 ret
->vtable_parent
= NULL
;
1336 ret
->got
= htab
->init_refcount
;
1337 ret
->plt
= htab
->init_refcount
;
1339 ret
->type
= STT_NOTYPE
;
1341 /* Assume that we have been called by a non-ELF symbol reader.
1342 This flag is then reset by the code which reads an ELF input
1343 file. This ensures that a symbol created by a non-ELF symbol
1344 reader will have the flag set correctly. */
1345 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1351 /* Copy data from an indirect symbol to its direct symbol, hiding the
1352 old indirect symbol. Also used for copying flags to a weakdef. */
1355 _bfd_elf_link_hash_copy_indirect (const struct elf_backend_data
*bed
,
1356 struct elf_link_hash_entry
*dir
,
1357 struct elf_link_hash_entry
*ind
)
1360 bfd_signed_vma lowest_valid
= bed
->can_refcount
;
1362 /* Copy down any references that we may have already seen to the
1363 symbol which just became indirect. */
1365 dir
->elf_link_hash_flags
1366 |= ind
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_DYNAMIC
1367 | ELF_LINK_HASH_REF_REGULAR
1368 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1369 | ELF_LINK_NON_GOT_REF
1370 | ELF_LINK_HASH_NEEDS_PLT
1371 | ELF_LINK_POINTER_EQUALITY_NEEDED
);
1373 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1376 /* Copy over the global and procedure linkage table refcount entries.
1377 These may have been already set up by a check_relocs routine. */
1378 tmp
= dir
->got
.refcount
;
1379 if (tmp
< lowest_valid
)
1381 dir
->got
.refcount
= ind
->got
.refcount
;
1382 ind
->got
.refcount
= tmp
;
1385 BFD_ASSERT (ind
->got
.refcount
< lowest_valid
);
1387 tmp
= dir
->plt
.refcount
;
1388 if (tmp
< lowest_valid
)
1390 dir
->plt
.refcount
= ind
->plt
.refcount
;
1391 ind
->plt
.refcount
= tmp
;
1394 BFD_ASSERT (ind
->plt
.refcount
< lowest_valid
);
1396 if (dir
->dynindx
== -1)
1398 dir
->dynindx
= ind
->dynindx
;
1399 dir
->dynstr_index
= ind
->dynstr_index
;
1401 ind
->dynstr_index
= 0;
1404 BFD_ASSERT (ind
->dynindx
== -1);
1408 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
1409 struct elf_link_hash_entry
*h
,
1410 bfd_boolean force_local
)
1412 h
->plt
= elf_hash_table (info
)->init_offset
;
1413 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1416 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1417 if (h
->dynindx
!= -1)
1420 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
1426 /* Initialize an ELF linker hash table. */
1429 _bfd_elf_link_hash_table_init
1430 (struct elf_link_hash_table
*table
,
1432 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
1433 struct bfd_hash_table
*,
1438 table
->dynamic_sections_created
= FALSE
;
1439 table
->dynobj
= NULL
;
1440 /* Make sure can_refcount is extended to the width and signedness of
1441 init_refcount before we subtract one from it. */
1442 table
->init_refcount
.refcount
= get_elf_backend_data (abfd
)->can_refcount
;
1443 table
->init_refcount
.refcount
-= 1;
1444 table
->init_offset
.offset
= -(bfd_vma
) 1;
1445 /* The first dynamic symbol is a dummy. */
1446 table
->dynsymcount
= 1;
1447 table
->dynstr
= NULL
;
1448 table
->bucketcount
= 0;
1449 table
->needed
= NULL
;
1451 table
->stab_info
= NULL
;
1452 table
->merge_info
= NULL
;
1453 memset (&table
->eh_info
, 0, sizeof (table
->eh_info
));
1454 table
->dynlocal
= NULL
;
1455 table
->runpath
= NULL
;
1456 table
->tls_sec
= NULL
;
1457 table
->tls_size
= 0;
1458 table
->loaded
= NULL
;
1460 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
1461 table
->root
.type
= bfd_link_elf_hash_table
;
1466 /* Create an ELF linker hash table. */
1468 struct bfd_link_hash_table
*
1469 _bfd_elf_link_hash_table_create (bfd
*abfd
)
1471 struct elf_link_hash_table
*ret
;
1472 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1474 ret
= bfd_malloc (amt
);
1478 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1487 /* This is a hook for the ELF emulation code in the generic linker to
1488 tell the backend linker what file name to use for the DT_NEEDED
1489 entry for a dynamic object. */
1492 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
1494 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1495 && bfd_get_format (abfd
) == bfd_object
)
1496 elf_dt_name (abfd
) = name
;
1500 bfd_elf_set_dyn_lib_class (bfd
*abfd
, int lib_class
)
1502 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1503 && bfd_get_format (abfd
) == bfd_object
)
1504 elf_dyn_lib_class (abfd
) = lib_class
;
1507 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1508 the linker ELF emulation code. */
1510 struct bfd_link_needed_list
*
1511 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
1512 struct bfd_link_info
*info
)
1514 if (! is_elf_hash_table (info
->hash
))
1516 return elf_hash_table (info
)->needed
;
1519 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1520 hook for the linker ELF emulation code. */
1522 struct bfd_link_needed_list
*
1523 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
1524 struct bfd_link_info
*info
)
1526 if (! is_elf_hash_table (info
->hash
))
1528 return elf_hash_table (info
)->runpath
;
1531 /* Get the name actually used for a dynamic object for a link. This
1532 is the SONAME entry if there is one. Otherwise, it is the string
1533 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1536 bfd_elf_get_dt_soname (bfd
*abfd
)
1538 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1539 && bfd_get_format (abfd
) == bfd_object
)
1540 return elf_dt_name (abfd
);
1544 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1545 the ELF linker emulation code. */
1548 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
1549 struct bfd_link_needed_list
**pneeded
)
1552 bfd_byte
*dynbuf
= NULL
;
1554 unsigned long shlink
;
1555 bfd_byte
*extdyn
, *extdynend
;
1557 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1561 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1562 || bfd_get_format (abfd
) != bfd_object
)
1565 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1566 if (s
== NULL
|| s
->_raw_size
== 0)
1569 dynbuf
= bfd_malloc (s
->_raw_size
);
1573 if (! bfd_get_section_contents (abfd
, s
, dynbuf
, 0, s
->_raw_size
))
1576 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1580 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1582 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1583 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1586 extdynend
= extdyn
+ s
->_raw_size
;
1587 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1589 Elf_Internal_Dyn dyn
;
1591 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1593 if (dyn
.d_tag
== DT_NULL
)
1596 if (dyn
.d_tag
== DT_NEEDED
)
1599 struct bfd_link_needed_list
*l
;
1600 unsigned int tagv
= dyn
.d_un
.d_val
;
1603 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1608 l
= bfd_alloc (abfd
, amt
);
1629 /* Allocate an ELF string table--force the first byte to be zero. */
1631 struct bfd_strtab_hash
*
1632 _bfd_elf_stringtab_init (void)
1634 struct bfd_strtab_hash
*ret
;
1636 ret
= _bfd_stringtab_init ();
1641 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1642 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1643 if (loc
== (bfd_size_type
) -1)
1645 _bfd_stringtab_free (ret
);
1652 /* ELF .o/exec file reading */
1654 /* Create a new bfd section from an ELF section header. */
1657 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1659 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1660 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1661 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1664 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1666 switch (hdr
->sh_type
)
1669 /* Inactive section. Throw it away. */
1672 case SHT_PROGBITS
: /* Normal section with contents. */
1673 case SHT_NOBITS
: /* .bss section. */
1674 case SHT_HASH
: /* .hash section. */
1675 case SHT_NOTE
: /* .note section. */
1676 case SHT_INIT_ARRAY
: /* .init_array section. */
1677 case SHT_FINI_ARRAY
: /* .fini_array section. */
1678 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1679 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1681 case SHT_DYNAMIC
: /* Dynamic linking information. */
1682 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1684 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1686 Elf_Internal_Shdr
*dynsymhdr
;
1688 /* The shared libraries distributed with hpux11 have a bogus
1689 sh_link field for the ".dynamic" section. Find the
1690 string table for the ".dynsym" section instead. */
1691 if (elf_dynsymtab (abfd
) != 0)
1693 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1694 hdr
->sh_link
= dynsymhdr
->sh_link
;
1698 unsigned int i
, num_sec
;
1700 num_sec
= elf_numsections (abfd
);
1701 for (i
= 1; i
< num_sec
; i
++)
1703 dynsymhdr
= elf_elfsections (abfd
)[i
];
1704 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1706 hdr
->sh_link
= dynsymhdr
->sh_link
;
1714 case SHT_SYMTAB
: /* A symbol table */
1715 if (elf_onesymtab (abfd
) == shindex
)
1718 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1719 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1720 elf_onesymtab (abfd
) = shindex
;
1721 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1722 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1723 abfd
->flags
|= HAS_SYMS
;
1725 /* Sometimes a shared object will map in the symbol table. If
1726 SHF_ALLOC is set, and this is a shared object, then we also
1727 treat this section as a BFD section. We can not base the
1728 decision purely on SHF_ALLOC, because that flag is sometimes
1729 set in a relocatable object file, which would confuse the
1731 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1732 && (abfd
->flags
& DYNAMIC
) != 0
1733 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1738 case SHT_DYNSYM
: /* A dynamic symbol table */
1739 if (elf_dynsymtab (abfd
) == shindex
)
1742 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1743 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1744 elf_dynsymtab (abfd
) = shindex
;
1745 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1746 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1747 abfd
->flags
|= HAS_SYMS
;
1749 /* Besides being a symbol table, we also treat this as a regular
1750 section, so that objcopy can handle it. */
1751 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1753 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1754 if (elf_symtab_shndx (abfd
) == shindex
)
1757 /* Get the associated symbol table. */
1758 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1759 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1762 elf_symtab_shndx (abfd
) = shindex
;
1763 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1764 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1767 case SHT_STRTAB
: /* A string table */
1768 if (hdr
->bfd_section
!= NULL
)
1770 if (ehdr
->e_shstrndx
== shindex
)
1772 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1773 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1777 unsigned int i
, num_sec
;
1779 num_sec
= elf_numsections (abfd
);
1780 for (i
= 1; i
< num_sec
; i
++)
1782 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1783 if (hdr2
->sh_link
== shindex
)
1785 if (! bfd_section_from_shdr (abfd
, i
))
1787 if (elf_onesymtab (abfd
) == i
)
1789 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1790 elf_elfsections (abfd
)[shindex
] =
1791 &elf_tdata (abfd
)->strtab_hdr
;
1794 if (elf_dynsymtab (abfd
) == i
)
1796 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1797 elf_elfsections (abfd
)[shindex
] = hdr
=
1798 &elf_tdata (abfd
)->dynstrtab_hdr
;
1799 /* We also treat this as a regular section, so
1800 that objcopy can handle it. */
1803 #if 0 /* Not handling other string tables specially right now. */
1804 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1805 /* We have a strtab for some random other section. */
1806 newsect
= (asection
*) hdr2
->bfd_section
;
1809 hdr
->bfd_section
= newsect
;
1810 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1812 elf_elfsections (abfd
)[shindex
] = hdr2
;
1818 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1822 /* *These* do a lot of work -- but build no sections! */
1824 asection
*target_sect
;
1825 Elf_Internal_Shdr
*hdr2
;
1826 unsigned int num_sec
= elf_numsections (abfd
);
1828 /* Check for a bogus link to avoid crashing. */
1829 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1830 || hdr
->sh_link
>= num_sec
)
1832 ((*_bfd_error_handler
)
1833 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1834 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1835 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1838 /* For some incomprehensible reason Oracle distributes
1839 libraries for Solaris in which some of the objects have
1840 bogus sh_link fields. It would be nice if we could just
1841 reject them, but, unfortunately, some people need to use
1842 them. We scan through the section headers; if we find only
1843 one suitable symbol table, we clobber the sh_link to point
1844 to it. I hope this doesn't break anything. */
1845 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1846 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1852 for (scan
= 1; scan
< num_sec
; scan
++)
1854 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1855 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1866 hdr
->sh_link
= found
;
1869 /* Get the symbol table. */
1870 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1871 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1874 /* If this reloc section does not use the main symbol table we
1875 don't treat it as a reloc section. BFD can't adequately
1876 represent such a section, so at least for now, we don't
1877 try. We just present it as a normal section. We also
1878 can't use it as a reloc section if it points to the null
1880 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1881 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1883 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1885 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1886 if (target_sect
== NULL
)
1889 if ((target_sect
->flags
& SEC_RELOC
) == 0
1890 || target_sect
->reloc_count
== 0)
1891 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1895 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1896 amt
= sizeof (*hdr2
);
1897 hdr2
= bfd_alloc (abfd
, amt
);
1898 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1901 elf_elfsections (abfd
)[shindex
] = hdr2
;
1902 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1903 target_sect
->flags
|= SEC_RELOC
;
1904 target_sect
->relocation
= NULL
;
1905 target_sect
->rel_filepos
= hdr
->sh_offset
;
1906 /* In the section to which the relocations apply, mark whether
1907 its relocations are of the REL or RELA variety. */
1908 if (hdr
->sh_size
!= 0)
1909 target_sect
->use_rela_p
= hdr
->sh_type
== SHT_RELA
;
1910 abfd
->flags
|= HAS_RELOC
;
1915 case SHT_GNU_verdef
:
1916 elf_dynverdef (abfd
) = shindex
;
1917 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1918 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1921 case SHT_GNU_versym
:
1922 elf_dynversym (abfd
) = shindex
;
1923 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1924 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1927 case SHT_GNU_verneed
:
1928 elf_dynverref (abfd
) = shindex
;
1929 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1930 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1937 /* We need a BFD section for objcopy and relocatable linking,
1938 and it's handy to have the signature available as the section
1940 name
= group_signature (abfd
, hdr
);
1943 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1945 if (hdr
->contents
!= NULL
)
1947 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1948 unsigned int n_elt
= hdr
->sh_size
/ 4;
1951 if (idx
->flags
& GRP_COMDAT
)
1952 hdr
->bfd_section
->flags
1953 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1955 /* We try to keep the same section order as it comes in. */
1957 while (--n_elt
!= 0)
1958 if ((s
= (--idx
)->shdr
->bfd_section
) != NULL
1959 && elf_next_in_group (s
) != NULL
)
1961 elf_next_in_group (hdr
->bfd_section
) = s
;
1968 /* Check for any processor-specific section types. */
1970 if (bed
->elf_backend_section_from_shdr
)
1971 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1979 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1980 Return SEC for sections that have no elf section, and NULL on error. */
1983 bfd_section_from_r_symndx (bfd
*abfd
,
1984 struct sym_sec_cache
*cache
,
1986 unsigned long r_symndx
)
1988 Elf_Internal_Shdr
*symtab_hdr
;
1989 unsigned char esym
[sizeof (Elf64_External_Sym
)];
1990 Elf_External_Sym_Shndx eshndx
;
1991 Elf_Internal_Sym isym
;
1992 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1994 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
1995 return cache
->sec
[ent
];
1997 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1998 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
1999 &isym
, esym
, &eshndx
) == NULL
)
2002 if (cache
->abfd
!= abfd
)
2004 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2007 cache
->indx
[ent
] = r_symndx
;
2008 cache
->sec
[ent
] = sec
;
2009 if ((isym
.st_shndx
!= SHN_UNDEF
&& isym
.st_shndx
< SHN_LORESERVE
)
2010 || isym
.st_shndx
> SHN_HIRESERVE
)
2013 s
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
2015 cache
->sec
[ent
] = s
;
2017 return cache
->sec
[ent
];
2020 /* Given an ELF section number, retrieve the corresponding BFD
2024 bfd_section_from_elf_index (bfd
*abfd
, unsigned int index
)
2026 if (index
>= elf_numsections (abfd
))
2028 return elf_elfsections (abfd
)[index
]->bfd_section
;
2031 static struct bfd_elf_special_section
const special_sections
[] =
2033 { ".bss", 4, -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2034 { ".comment", 8, 0, SHT_PROGBITS
, 0 },
2035 { ".data", 5, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2036 { ".data1", 6, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2037 { ".debug", 6, 0, SHT_PROGBITS
, 0 },
2038 { ".fini", 5, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2039 { ".init", 5, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2040 { ".line", 5, 0, SHT_PROGBITS
, 0 },
2041 { ".rodata", 7, -2, SHT_PROGBITS
, SHF_ALLOC
},
2042 { ".rodata1", 8, 0, SHT_PROGBITS
, SHF_ALLOC
},
2043 { ".tbss", 5, -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2044 { ".tdata", 6, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2045 { ".text", 5, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2046 { ".init_array", 11, 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2047 { ".fini_array", 11, 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2048 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2049 { ".debug_line", 11, 0, SHT_PROGBITS
, 0 },
2050 { ".debug_info", 11, 0, SHT_PROGBITS
, 0 },
2051 { ".debug_abbrev", 13, 0, SHT_PROGBITS
, 0 },
2052 { ".debug_aranges", 14, 0, SHT_PROGBITS
, 0 },
2053 { ".dynamic", 8, 0, SHT_DYNAMIC
, SHF_ALLOC
},
2054 { ".dynstr", 7, 0, SHT_STRTAB
, SHF_ALLOC
},
2055 { ".dynsym", 7, 0, SHT_DYNSYM
, SHF_ALLOC
},
2056 { ".got", 4, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2057 { ".hash", 5, 0, SHT_HASH
, SHF_ALLOC
},
2058 { ".interp", 7, 0, SHT_PROGBITS
, 0 },
2059 { ".plt", 4, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2060 { ".shstrtab", 9, 0, SHT_STRTAB
, 0 },
2061 { ".strtab", 7, 0, SHT_STRTAB
, 0 },
2062 { ".symtab", 7, 0, SHT_SYMTAB
, 0 },
2063 { ".gnu.version", 12, 0, SHT_GNU_versym
, 0 },
2064 { ".gnu.version_d", 14, 0, SHT_GNU_verdef
, 0 },
2065 { ".gnu.version_r", 14, 0, SHT_GNU_verneed
, 0 },
2066 { ".note.GNU-stack",15, 0, SHT_PROGBITS
, 0 },
2067 { ".note", 5, -1, SHT_NOTE
, 0 },
2068 { ".rela", 5, -1, SHT_RELA
, 0 },
2069 { ".rel", 4, -1, SHT_REL
, 0 },
2070 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2071 { NULL
, 0, 0, 0, 0 }
2074 static const struct bfd_elf_special_section
*
2075 get_special_section (const char *name
,
2076 const struct bfd_elf_special_section
*special_sections
,
2080 int len
= strlen (name
);
2082 for (i
= 0; special_sections
[i
].prefix
!= NULL
; i
++)
2085 int prefix_len
= special_sections
[i
].prefix_length
;
2087 if (len
< prefix_len
)
2089 if (memcmp (name
, special_sections
[i
].prefix
, prefix_len
) != 0)
2092 suffix_len
= special_sections
[i
].suffix_length
;
2093 if (suffix_len
<= 0)
2095 if (name
[prefix_len
] != 0)
2097 if (suffix_len
== 0)
2099 if (name
[prefix_len
] != '.'
2100 && (suffix_len
== -2
2101 || (rela
&& special_sections
[i
].type
== SHT_REL
)))
2107 if (len
< prefix_len
+ suffix_len
)
2109 if (memcmp (name
+ len
- suffix_len
,
2110 special_sections
[i
].prefix
+ prefix_len
,
2114 return &special_sections
[i
];
2120 const struct bfd_elf_special_section
*
2121 _bfd_elf_get_sec_type_attr (bfd
*abfd
, const char *name
)
2123 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2124 const struct bfd_elf_special_section
*ssect
= NULL
;
2126 /* See if this is one of the special sections. */
2129 unsigned int rela
= bed
->default_use_rela_p
;
2131 if (bed
->special_sections
)
2132 ssect
= get_special_section (name
, bed
->special_sections
, rela
);
2135 ssect
= get_special_section (name
, special_sections
, rela
);
2142 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2144 struct bfd_elf_section_data
*sdata
;
2145 const struct bfd_elf_special_section
*ssect
;
2147 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2150 sdata
= bfd_zalloc (abfd
, sizeof (*sdata
));
2153 sec
->used_by_bfd
= sdata
;
2156 elf_section_type (sec
) = SHT_NULL
;
2157 ssect
= _bfd_elf_get_sec_type_attr (abfd
, sec
->name
);
2160 elf_section_type (sec
) = ssect
->type
;
2161 elf_section_flags (sec
) = ssect
->attr
;
2164 /* Indicate whether or not this section should use RELA relocations. */
2165 sec
->use_rela_p
= get_elf_backend_data (abfd
)->default_use_rela_p
;
2170 /* Create a new bfd section from an ELF program header.
2172 Since program segments have no names, we generate a synthetic name
2173 of the form segment<NUM>, where NUM is generally the index in the
2174 program header table. For segments that are split (see below) we
2175 generate the names segment<NUM>a and segment<NUM>b.
2177 Note that some program segments may have a file size that is different than
2178 (less than) the memory size. All this means is that at execution the
2179 system must allocate the amount of memory specified by the memory size,
2180 but only initialize it with the first "file size" bytes read from the
2181 file. This would occur for example, with program segments consisting
2182 of combined data+bss.
2184 To handle the above situation, this routine generates TWO bfd sections
2185 for the single program segment. The first has the length specified by
2186 the file size of the segment, and the second has the length specified
2187 by the difference between the two sizes. In effect, the segment is split
2188 into it's initialized and uninitialized parts.
2193 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2194 Elf_Internal_Phdr
*hdr
,
2196 const char *typename
)
2204 split
= ((hdr
->p_memsz
> 0)
2205 && (hdr
->p_filesz
> 0)
2206 && (hdr
->p_memsz
> hdr
->p_filesz
));
2207 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2208 len
= strlen (namebuf
) + 1;
2209 name
= bfd_alloc (abfd
, len
);
2212 memcpy (name
, namebuf
, len
);
2213 newsect
= bfd_make_section (abfd
, name
);
2214 if (newsect
== NULL
)
2216 newsect
->vma
= hdr
->p_vaddr
;
2217 newsect
->lma
= hdr
->p_paddr
;
2218 newsect
->_raw_size
= hdr
->p_filesz
;
2219 newsect
->filepos
= hdr
->p_offset
;
2220 newsect
->flags
|= SEC_HAS_CONTENTS
;
2221 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2222 if (hdr
->p_type
== PT_LOAD
)
2224 newsect
->flags
|= SEC_ALLOC
;
2225 newsect
->flags
|= SEC_LOAD
;
2226 if (hdr
->p_flags
& PF_X
)
2228 /* FIXME: all we known is that it has execute PERMISSION,
2230 newsect
->flags
|= SEC_CODE
;
2233 if (!(hdr
->p_flags
& PF_W
))
2235 newsect
->flags
|= SEC_READONLY
;
2240 sprintf (namebuf
, "%s%db", typename
, index
);
2241 len
= strlen (namebuf
) + 1;
2242 name
= bfd_alloc (abfd
, len
);
2245 memcpy (name
, namebuf
, len
);
2246 newsect
= bfd_make_section (abfd
, name
);
2247 if (newsect
== NULL
)
2249 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2250 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2251 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
2252 if (hdr
->p_type
== PT_LOAD
)
2254 newsect
->flags
|= SEC_ALLOC
;
2255 if (hdr
->p_flags
& PF_X
)
2256 newsect
->flags
|= SEC_CODE
;
2258 if (!(hdr
->p_flags
& PF_W
))
2259 newsect
->flags
|= SEC_READONLY
;
2266 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int index
)
2268 const struct elf_backend_data
*bed
;
2270 switch (hdr
->p_type
)
2273 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2276 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2279 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2282 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2285 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2287 if (! elfcore_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2292 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2295 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2297 case PT_GNU_EH_FRAME
:
2298 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
,
2302 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "stack");
2305 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "relro");
2308 /* Check for any processor-specific program segment types.
2309 If no handler for them, default to making "segment" sections. */
2310 bed
= get_elf_backend_data (abfd
);
2311 if (bed
->elf_backend_section_from_phdr
)
2312 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2314 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2318 /* Initialize REL_HDR, the section-header for new section, containing
2319 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2320 relocations; otherwise, we use REL relocations. */
2323 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2324 Elf_Internal_Shdr
*rel_hdr
,
2326 bfd_boolean use_rela_p
)
2329 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2330 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2332 name
= bfd_alloc (abfd
, amt
);
2335 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2337 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2339 if (rel_hdr
->sh_name
== (unsigned int) -1)
2341 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2342 rel_hdr
->sh_entsize
= (use_rela_p
2343 ? bed
->s
->sizeof_rela
2344 : bed
->s
->sizeof_rel
);
2345 rel_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
2346 rel_hdr
->sh_flags
= 0;
2347 rel_hdr
->sh_addr
= 0;
2348 rel_hdr
->sh_size
= 0;
2349 rel_hdr
->sh_offset
= 0;
2354 /* Set up an ELF internal section header for a section. */
2357 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *failedptrarg
)
2359 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2360 bfd_boolean
*failedptr
= failedptrarg
;
2361 Elf_Internal_Shdr
*this_hdr
;
2365 /* We already failed; just get out of the bfd_map_over_sections
2370 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2372 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2373 asect
->name
, FALSE
);
2374 if (this_hdr
->sh_name
== (unsigned int) -1)
2380 this_hdr
->sh_flags
= 0;
2382 if ((asect
->flags
& SEC_ALLOC
) != 0
2383 || asect
->user_set_vma
)
2384 this_hdr
->sh_addr
= asect
->vma
;
2386 this_hdr
->sh_addr
= 0;
2388 this_hdr
->sh_offset
= 0;
2389 this_hdr
->sh_size
= asect
->_raw_size
;
2390 this_hdr
->sh_link
= 0;
2391 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2392 /* The sh_entsize and sh_info fields may have been set already by
2393 copy_private_section_data. */
2395 this_hdr
->bfd_section
= asect
;
2396 this_hdr
->contents
= NULL
;
2398 /* If the section type is unspecified, we set it based on
2400 if (this_hdr
->sh_type
== SHT_NULL
)
2402 if ((asect
->flags
& SEC_GROUP
) != 0)
2404 /* We also need to mark SHF_GROUP here for relocatable
2406 struct bfd_link_order
*l
;
2409 for (l
= asect
->link_order_head
; l
!= NULL
; l
= l
->next
)
2410 if (l
->type
== bfd_indirect_link_order
2411 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2414 /* The name is not important. Anything will do. */
2415 elf_group_name (elt
->output_section
) = "G";
2416 elf_section_flags (elt
->output_section
) |= SHF_GROUP
;
2418 elt
= elf_next_in_group (elt
);
2419 /* During a relocatable link, the lists are
2422 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2424 this_hdr
->sh_type
= SHT_GROUP
;
2426 else if ((asect
->flags
& SEC_ALLOC
) != 0
2427 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2428 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2429 this_hdr
->sh_type
= SHT_NOBITS
;
2431 this_hdr
->sh_type
= SHT_PROGBITS
;
2434 switch (this_hdr
->sh_type
)
2440 case SHT_INIT_ARRAY
:
2441 case SHT_FINI_ARRAY
:
2442 case SHT_PREINIT_ARRAY
:
2449 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2453 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2457 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2461 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2462 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2466 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2467 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2470 case SHT_GNU_versym
:
2471 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2474 case SHT_GNU_verdef
:
2475 this_hdr
->sh_entsize
= 0;
2476 /* objcopy or strip will copy over sh_info, but may not set
2477 cverdefs. The linker will set cverdefs, but sh_info will be
2479 if (this_hdr
->sh_info
== 0)
2480 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2482 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2483 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2486 case SHT_GNU_verneed
:
2487 this_hdr
->sh_entsize
= 0;
2488 /* objcopy or strip will copy over sh_info, but may not set
2489 cverrefs. The linker will set cverrefs, but sh_info will be
2491 if (this_hdr
->sh_info
== 0)
2492 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2494 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2495 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2499 this_hdr
->sh_entsize
= 4;
2503 if ((asect
->flags
& SEC_ALLOC
) != 0)
2504 this_hdr
->sh_flags
|= SHF_ALLOC
;
2505 if ((asect
->flags
& SEC_READONLY
) == 0)
2506 this_hdr
->sh_flags
|= SHF_WRITE
;
2507 if ((asect
->flags
& SEC_CODE
) != 0)
2508 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2509 if ((asect
->flags
& SEC_MERGE
) != 0)
2511 this_hdr
->sh_flags
|= SHF_MERGE
;
2512 this_hdr
->sh_entsize
= asect
->entsize
;
2513 if ((asect
->flags
& SEC_STRINGS
) != 0)
2514 this_hdr
->sh_flags
|= SHF_STRINGS
;
2516 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2517 this_hdr
->sh_flags
|= SHF_GROUP
;
2518 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2520 this_hdr
->sh_flags
|= SHF_TLS
;
2521 if (asect
->_raw_size
== 0 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2523 struct bfd_link_order
*o
;
2525 this_hdr
->sh_size
= 0;
2526 for (o
= asect
->link_order_head
; o
!= NULL
; o
= o
->next
)
2527 if (this_hdr
->sh_size
< o
->offset
+ o
->size
)
2528 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2529 if (this_hdr
->sh_size
)
2530 this_hdr
->sh_type
= SHT_NOBITS
;
2534 /* Check for processor-specific section types. */
2535 if (bed
->elf_backend_fake_sections
2536 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2539 /* If the section has relocs, set up a section header for the
2540 SHT_REL[A] section. If two relocation sections are required for
2541 this section, it is up to the processor-specific back-end to
2542 create the other. */
2543 if ((asect
->flags
& SEC_RELOC
) != 0
2544 && !_bfd_elf_init_reloc_shdr (abfd
,
2545 &elf_section_data (asect
)->rel_hdr
,
2551 /* Fill in the contents of a SHT_GROUP section. */
2554 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2556 bfd_boolean
*failedptr
= failedptrarg
;
2557 unsigned long symindx
;
2558 asection
*elt
, *first
;
2560 struct bfd_link_order
*l
;
2563 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2568 if (elf_group_id (sec
) != NULL
)
2569 symindx
= elf_group_id (sec
)->udata
.i
;
2573 /* If called from the assembler, swap_out_syms will have set up
2574 elf_section_syms; If called for "ld -r", use target_index. */
2575 if (elf_section_syms (abfd
) != NULL
)
2576 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2578 symindx
= sec
->target_index
;
2580 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2582 /* The contents won't be allocated for "ld -r" or objcopy. */
2584 if (sec
->contents
== NULL
)
2587 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2589 /* Arrange for the section to be written out. */
2590 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2591 if (sec
->contents
== NULL
)
2598 loc
= sec
->contents
+ sec
->_raw_size
;
2600 /* Get the pointer to the first section in the group that gas
2601 squirreled away here. objcopy arranges for this to be set to the
2602 start of the input section group. */
2603 first
= elt
= elf_next_in_group (sec
);
2605 /* First element is a flag word. Rest of section is elf section
2606 indices for all the sections of the group. Write them backwards
2607 just to keep the group in the same order as given in .section
2608 directives, not that it matters. */
2617 s
= s
->output_section
;
2620 idx
= elf_section_data (s
)->this_idx
;
2621 H_PUT_32 (abfd
, idx
, loc
);
2622 elt
= elf_next_in_group (elt
);
2627 /* If this is a relocatable link, then the above did nothing because
2628 SEC is the output section. Look through the input sections
2630 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2631 if (l
->type
== bfd_indirect_link_order
2632 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2637 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2638 elt
= elf_next_in_group (elt
);
2639 /* During a relocatable link, the lists are circular. */
2641 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2643 /* With ld -r, merging SHT_GROUP sections results in wasted space
2644 due to allowing for the flag word on each input. We may well
2645 duplicate entries too. */
2646 while ((loc
-= 4) > sec
->contents
)
2647 H_PUT_32 (abfd
, 0, loc
);
2649 if (loc
!= sec
->contents
)
2652 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2655 /* Assign all ELF section numbers. The dummy first section is handled here
2656 too. The link/info pointers for the standard section types are filled
2657 in here too, while we're at it. */
2660 assign_section_numbers (bfd
*abfd
)
2662 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2664 unsigned int section_number
, secn
;
2665 Elf_Internal_Shdr
**i_shdrp
;
2670 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2672 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2674 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2676 if (section_number
== SHN_LORESERVE
)
2677 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2678 d
->this_idx
= section_number
++;
2679 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2680 if ((sec
->flags
& SEC_RELOC
) == 0)
2684 if (section_number
== SHN_LORESERVE
)
2685 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2686 d
->rel_idx
= section_number
++;
2687 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2692 if (section_number
== SHN_LORESERVE
)
2693 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2694 d
->rel_idx2
= section_number
++;
2695 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2701 if (section_number
== SHN_LORESERVE
)
2702 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2703 t
->shstrtab_section
= section_number
++;
2704 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2705 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2707 if (bfd_get_symcount (abfd
) > 0)
2709 if (section_number
== SHN_LORESERVE
)
2710 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2711 t
->symtab_section
= section_number
++;
2712 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2713 if (section_number
> SHN_LORESERVE
- 2)
2715 if (section_number
== SHN_LORESERVE
)
2716 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2717 t
->symtab_shndx_section
= section_number
++;
2718 t
->symtab_shndx_hdr
.sh_name
2719 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2720 ".symtab_shndx", FALSE
);
2721 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2724 if (section_number
== SHN_LORESERVE
)
2725 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2726 t
->strtab_section
= section_number
++;
2727 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2730 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2731 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2733 elf_numsections (abfd
) = section_number
;
2734 elf_elfheader (abfd
)->e_shnum
= section_number
;
2735 if (section_number
> SHN_LORESERVE
)
2736 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2738 /* Set up the list of section header pointers, in agreement with the
2740 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2741 i_shdrp
= bfd_zalloc (abfd
, amt
);
2742 if (i_shdrp
== NULL
)
2745 amt
= sizeof (Elf_Internal_Shdr
);
2746 i_shdrp
[0] = bfd_zalloc (abfd
, amt
);
2747 if (i_shdrp
[0] == NULL
)
2749 bfd_release (abfd
, i_shdrp
);
2753 elf_elfsections (abfd
) = i_shdrp
;
2755 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2756 if (bfd_get_symcount (abfd
) > 0)
2758 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2759 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2761 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2762 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2764 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2765 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2767 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2769 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2773 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2774 if (d
->rel_idx
!= 0)
2775 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2776 if (d
->rel_idx2
!= 0)
2777 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2779 /* Fill in the sh_link and sh_info fields while we're at it. */
2781 /* sh_link of a reloc section is the section index of the symbol
2782 table. sh_info is the section index of the section to which
2783 the relocation entries apply. */
2784 if (d
->rel_idx
!= 0)
2786 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2787 d
->rel_hdr
.sh_info
= d
->this_idx
;
2789 if (d
->rel_idx2
!= 0)
2791 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2792 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2795 switch (d
->this_hdr
.sh_type
)
2799 /* A reloc section which we are treating as a normal BFD
2800 section. sh_link is the section index of the symbol
2801 table. sh_info is the section index of the section to
2802 which the relocation entries apply. We assume that an
2803 allocated reloc section uses the dynamic symbol table.
2804 FIXME: How can we be sure? */
2805 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2807 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2809 /* We look up the section the relocs apply to by name. */
2811 if (d
->this_hdr
.sh_type
== SHT_REL
)
2815 s
= bfd_get_section_by_name (abfd
, name
);
2817 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2821 /* We assume that a section named .stab*str is a stabs
2822 string section. We look for a section with the same name
2823 but without the trailing ``str'', and set its sh_link
2824 field to point to this section. */
2825 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2826 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2831 len
= strlen (sec
->name
);
2832 alc
= bfd_malloc (len
- 2);
2835 memcpy (alc
, sec
->name
, len
- 3);
2836 alc
[len
- 3] = '\0';
2837 s
= bfd_get_section_by_name (abfd
, alc
);
2841 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2843 /* This is a .stab section. */
2844 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
2845 elf_section_data (s
)->this_hdr
.sh_entsize
2846 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2853 case SHT_GNU_verneed
:
2854 case SHT_GNU_verdef
:
2855 /* sh_link is the section header index of the string table
2856 used for the dynamic entries, or the symbol table, or the
2858 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2860 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2864 case SHT_GNU_versym
:
2865 /* sh_link is the section header index of the symbol table
2866 this hash table or version table is for. */
2867 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2869 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2873 d
->this_hdr
.sh_link
= t
->symtab_section
;
2877 for (secn
= 1; secn
< section_number
; ++secn
)
2878 if (i_shdrp
[secn
] == NULL
)
2879 i_shdrp
[secn
] = i_shdrp
[0];
2881 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2882 i_shdrp
[secn
]->sh_name
);
2886 /* Map symbol from it's internal number to the external number, moving
2887 all local symbols to be at the head of the list. */
2890 sym_is_global (bfd
*abfd
, asymbol
*sym
)
2892 /* If the backend has a special mapping, use it. */
2893 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2894 if (bed
->elf_backend_sym_is_global
)
2895 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
2897 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2898 || bfd_is_und_section (bfd_get_section (sym
))
2899 || bfd_is_com_section (bfd_get_section (sym
)));
2903 elf_map_symbols (bfd
*abfd
)
2905 unsigned int symcount
= bfd_get_symcount (abfd
);
2906 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2907 asymbol
**sect_syms
;
2908 unsigned int num_locals
= 0;
2909 unsigned int num_globals
= 0;
2910 unsigned int num_locals2
= 0;
2911 unsigned int num_globals2
= 0;
2919 fprintf (stderr
, "elf_map_symbols\n");
2923 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2925 if (max_index
< asect
->index
)
2926 max_index
= asect
->index
;
2930 amt
= max_index
* sizeof (asymbol
*);
2931 sect_syms
= bfd_zalloc (abfd
, amt
);
2932 if (sect_syms
== NULL
)
2934 elf_section_syms (abfd
) = sect_syms
;
2935 elf_num_section_syms (abfd
) = max_index
;
2937 /* Init sect_syms entries for any section symbols we have already
2938 decided to output. */
2939 for (idx
= 0; idx
< symcount
; idx
++)
2941 asymbol
*sym
= syms
[idx
];
2943 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2950 if (sec
->owner
!= NULL
)
2952 if (sec
->owner
!= abfd
)
2954 if (sec
->output_offset
!= 0)
2957 sec
= sec
->output_section
;
2959 /* Empty sections in the input files may have had a
2960 section symbol created for them. (See the comment
2961 near the end of _bfd_generic_link_output_symbols in
2962 linker.c). If the linker script discards such
2963 sections then we will reach this point. Since we know
2964 that we cannot avoid this case, we detect it and skip
2965 the abort and the assignment to the sect_syms array.
2966 To reproduce this particular case try running the
2967 linker testsuite test ld-scripts/weak.exp for an ELF
2968 port that uses the generic linker. */
2969 if (sec
->owner
== NULL
)
2972 BFD_ASSERT (sec
->owner
== abfd
);
2974 sect_syms
[sec
->index
] = syms
[idx
];
2979 /* Classify all of the symbols. */
2980 for (idx
= 0; idx
< symcount
; idx
++)
2982 if (!sym_is_global (abfd
, syms
[idx
]))
2988 /* We will be adding a section symbol for each BFD section. Most normal
2989 sections will already have a section symbol in outsymbols, but
2990 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2991 at least in that case. */
2992 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2994 if (sect_syms
[asect
->index
] == NULL
)
2996 if (!sym_is_global (abfd
, asect
->symbol
))
3003 /* Now sort the symbols so the local symbols are first. */
3004 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
3005 new_syms
= bfd_alloc (abfd
, amt
);
3007 if (new_syms
== NULL
)
3010 for (idx
= 0; idx
< symcount
; idx
++)
3012 asymbol
*sym
= syms
[idx
];
3015 if (!sym_is_global (abfd
, sym
))
3018 i
= num_locals
+ num_globals2
++;
3020 sym
->udata
.i
= i
+ 1;
3022 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3024 if (sect_syms
[asect
->index
] == NULL
)
3026 asymbol
*sym
= asect
->symbol
;
3029 sect_syms
[asect
->index
] = sym
;
3030 if (!sym_is_global (abfd
, sym
))
3033 i
= num_locals
+ num_globals2
++;
3035 sym
->udata
.i
= i
+ 1;
3039 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3041 elf_num_locals (abfd
) = num_locals
;
3042 elf_num_globals (abfd
) = num_globals
;
3046 /* Align to the maximum file alignment that could be required for any
3047 ELF data structure. */
3049 static inline file_ptr
3050 align_file_position (file_ptr off
, int align
)
3052 return (off
+ align
- 1) & ~(align
- 1);
3055 /* Assign a file position to a section, optionally aligning to the
3056 required section alignment. */
3059 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3067 al
= i_shdrp
->sh_addralign
;
3069 offset
= BFD_ALIGN (offset
, al
);
3071 i_shdrp
->sh_offset
= offset
;
3072 if (i_shdrp
->bfd_section
!= NULL
)
3073 i_shdrp
->bfd_section
->filepos
= offset
;
3074 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3075 offset
+= i_shdrp
->sh_size
;
3079 /* Compute the file positions we are going to put the sections at, and
3080 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3081 is not NULL, this is being called by the ELF backend linker. */
3084 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3085 struct bfd_link_info
*link_info
)
3087 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3089 struct bfd_strtab_hash
*strtab
;
3090 Elf_Internal_Shdr
*shstrtab_hdr
;
3092 if (abfd
->output_has_begun
)
3095 /* Do any elf backend specific processing first. */
3096 if (bed
->elf_backend_begin_write_processing
)
3097 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3099 if (! prep_headers (abfd
))
3102 /* Post process the headers if necessary. */
3103 if (bed
->elf_backend_post_process_headers
)
3104 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3107 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3111 if (!assign_section_numbers (abfd
))
3114 /* The backend linker builds symbol table information itself. */
3115 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3117 /* Non-zero if doing a relocatable link. */
3118 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3120 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3124 if (link_info
== NULL
)
3126 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3131 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3132 /* sh_name was set in prep_headers. */
3133 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3134 shstrtab_hdr
->sh_flags
= 0;
3135 shstrtab_hdr
->sh_addr
= 0;
3136 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3137 shstrtab_hdr
->sh_entsize
= 0;
3138 shstrtab_hdr
->sh_link
= 0;
3139 shstrtab_hdr
->sh_info
= 0;
3140 /* sh_offset is set in assign_file_positions_except_relocs. */
3141 shstrtab_hdr
->sh_addralign
= 1;
3143 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3146 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3149 Elf_Internal_Shdr
*hdr
;
3151 off
= elf_tdata (abfd
)->next_file_pos
;
3153 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3154 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3156 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3157 if (hdr
->sh_size
!= 0)
3158 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3160 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3161 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3163 elf_tdata (abfd
)->next_file_pos
= off
;
3165 /* Now that we know where the .strtab section goes, write it
3167 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3168 || ! _bfd_stringtab_emit (abfd
, strtab
))
3170 _bfd_stringtab_free (strtab
);
3173 abfd
->output_has_begun
= TRUE
;
3178 /* Create a mapping from a set of sections to a program segment. */
3180 static struct elf_segment_map
*
3181 make_mapping (bfd
*abfd
,
3182 asection
**sections
,
3187 struct elf_segment_map
*m
;
3192 amt
= sizeof (struct elf_segment_map
);
3193 amt
+= (to
- from
- 1) * sizeof (asection
*);
3194 m
= bfd_zalloc (abfd
, amt
);
3198 m
->p_type
= PT_LOAD
;
3199 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3200 m
->sections
[i
- from
] = *hdrpp
;
3201 m
->count
= to
- from
;
3203 if (from
== 0 && phdr
)
3205 /* Include the headers in the first PT_LOAD segment. */
3206 m
->includes_filehdr
= 1;
3207 m
->includes_phdrs
= 1;
3213 /* Set up a mapping from BFD sections to program segments. */
3216 map_sections_to_segments (bfd
*abfd
)
3218 asection
**sections
= NULL
;
3222 struct elf_segment_map
*mfirst
;
3223 struct elf_segment_map
**pm
;
3224 struct elf_segment_map
*m
;
3227 unsigned int phdr_index
;
3228 bfd_vma maxpagesize
;
3230 bfd_boolean phdr_in_segment
= TRUE
;
3231 bfd_boolean writable
;
3233 asection
*first_tls
= NULL
;
3234 asection
*dynsec
, *eh_frame_hdr
;
3237 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3240 if (bfd_count_sections (abfd
) == 0)
3243 /* Select the allocated sections, and sort them. */
3245 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
3246 sections
= bfd_malloc (amt
);
3247 if (sections
== NULL
)
3251 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3253 if ((s
->flags
& SEC_ALLOC
) != 0)
3259 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3262 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3264 /* Build the mapping. */
3269 /* If we have a .interp section, then create a PT_PHDR segment for
3270 the program headers and a PT_INTERP segment for the .interp
3272 s
= bfd_get_section_by_name (abfd
, ".interp");
3273 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3275 amt
= sizeof (struct elf_segment_map
);
3276 m
= bfd_zalloc (abfd
, amt
);
3280 m
->p_type
= PT_PHDR
;
3281 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3282 m
->p_flags
= PF_R
| PF_X
;
3283 m
->p_flags_valid
= 1;
3284 m
->includes_phdrs
= 1;
3289 amt
= sizeof (struct elf_segment_map
);
3290 m
= bfd_zalloc (abfd
, amt
);
3294 m
->p_type
= PT_INTERP
;
3302 /* Look through the sections. We put sections in the same program
3303 segment when the start of the second section can be placed within
3304 a few bytes of the end of the first section. */
3308 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
3310 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3312 && (dynsec
->flags
& SEC_LOAD
) == 0)
3315 /* Deal with -Ttext or something similar such that the first section
3316 is not adjacent to the program headers. This is an
3317 approximation, since at this point we don't know exactly how many
3318 program headers we will need. */
3321 bfd_size_type phdr_size
;
3323 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3325 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3326 if ((abfd
->flags
& D_PAGED
) == 0
3327 || sections
[0]->lma
< phdr_size
3328 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3329 phdr_in_segment
= FALSE
;
3332 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3335 bfd_boolean new_segment
;
3339 /* See if this section and the last one will fit in the same
3342 if (last_hdr
== NULL
)
3344 /* If we don't have a segment yet, then we don't need a new
3345 one (we build the last one after this loop). */
3346 new_segment
= FALSE
;
3348 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3350 /* If this section has a different relation between the
3351 virtual address and the load address, then we need a new
3355 else if (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
3356 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3358 /* If putting this section in this segment would force us to
3359 skip a page in the segment, then we need a new segment. */
3362 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3363 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3365 /* We don't want to put a loadable section after a
3366 nonloadable section in the same segment.
3367 Consider .tbss sections as loadable for this purpose. */
3370 else if ((abfd
->flags
& D_PAGED
) == 0)
3372 /* If the file is not demand paged, which means that we
3373 don't require the sections to be correctly aligned in the
3374 file, then there is no other reason for a new segment. */
3375 new_segment
= FALSE
;
3378 && (hdr
->flags
& SEC_READONLY
) == 0
3379 && (((last_hdr
->lma
+ last_size
- 1)
3380 & ~(maxpagesize
- 1))
3381 != (hdr
->lma
& ~(maxpagesize
- 1))))
3383 /* We don't want to put a writable section in a read only
3384 segment, unless they are on the same page in memory
3385 anyhow. We already know that the last section does not
3386 bring us past the current section on the page, so the
3387 only case in which the new section is not on the same
3388 page as the previous section is when the previous section
3389 ends precisely on a page boundary. */
3394 /* Otherwise, we can use the same segment. */
3395 new_segment
= FALSE
;
3400 if ((hdr
->flags
& SEC_READONLY
) == 0)
3403 /* .tbss sections effectively have zero size. */
3404 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3405 last_size
= hdr
->_raw_size
;
3411 /* We need a new program segment. We must create a new program
3412 header holding all the sections from phdr_index until hdr. */
3414 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3421 if ((hdr
->flags
& SEC_READONLY
) == 0)
3427 /* .tbss sections effectively have zero size. */
3428 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3429 last_size
= hdr
->_raw_size
;
3433 phdr_in_segment
= FALSE
;
3436 /* Create a final PT_LOAD program segment. */
3437 if (last_hdr
!= NULL
)
3439 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3447 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3450 amt
= sizeof (struct elf_segment_map
);
3451 m
= bfd_zalloc (abfd
, amt
);
3455 m
->p_type
= PT_DYNAMIC
;
3457 m
->sections
[0] = dynsec
;
3463 /* For each loadable .note section, add a PT_NOTE segment. We don't
3464 use bfd_get_section_by_name, because if we link together
3465 nonloadable .note sections and loadable .note sections, we will
3466 generate two .note sections in the output file. FIXME: Using
3467 names for section types is bogus anyhow. */
3468 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3470 if ((s
->flags
& SEC_LOAD
) != 0
3471 && strncmp (s
->name
, ".note", 5) == 0)
3473 amt
= sizeof (struct elf_segment_map
);
3474 m
= bfd_zalloc (abfd
, amt
);
3478 m
->p_type
= PT_NOTE
;
3485 if (s
->flags
& SEC_THREAD_LOCAL
)
3493 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3498 amt
= sizeof (struct elf_segment_map
);
3499 amt
+= (tls_count
- 1) * sizeof (asection
*);
3500 m
= bfd_zalloc (abfd
, amt
);
3505 m
->count
= tls_count
;
3506 /* Mandated PF_R. */
3508 m
->p_flags_valid
= 1;
3509 for (i
= 0; i
< tls_count
; ++i
)
3511 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3512 m
->sections
[i
] = first_tls
;
3513 first_tls
= first_tls
->next
;
3520 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3522 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
3523 if (eh_frame_hdr
!= NULL
3524 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
3526 amt
= sizeof (struct elf_segment_map
);
3527 m
= bfd_zalloc (abfd
, amt
);
3531 m
->p_type
= PT_GNU_EH_FRAME
;
3533 m
->sections
[0] = eh_frame_hdr
->output_section
;
3539 if (elf_tdata (abfd
)->stack_flags
)
3541 amt
= sizeof (struct elf_segment_map
);
3542 m
= bfd_zalloc (abfd
, amt
);
3546 m
->p_type
= PT_GNU_STACK
;
3547 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
3548 m
->p_flags_valid
= 1;
3554 if (elf_tdata (abfd
)->relro
)
3556 amt
= sizeof (struct elf_segment_map
);
3557 m
= bfd_zalloc (abfd
, amt
);
3561 m
->p_type
= PT_GNU_RELRO
;
3563 m
->p_flags_valid
= 1;
3572 elf_tdata (abfd
)->segment_map
= mfirst
;
3576 if (sections
!= NULL
)
3581 /* Sort sections by address. */
3584 elf_sort_sections (const void *arg1
, const void *arg2
)
3586 const asection
*sec1
= *(const asection
**) arg1
;
3587 const asection
*sec2
= *(const asection
**) arg2
;
3588 bfd_size_type size1
, size2
;
3590 /* Sort by LMA first, since this is the address used to
3591 place the section into a segment. */
3592 if (sec1
->lma
< sec2
->lma
)
3594 else if (sec1
->lma
> sec2
->lma
)
3597 /* Then sort by VMA. Normally the LMA and the VMA will be
3598 the same, and this will do nothing. */
3599 if (sec1
->vma
< sec2
->vma
)
3601 else if (sec1
->vma
> sec2
->vma
)
3604 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3606 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3612 /* If the indicies are the same, do not return 0
3613 here, but continue to try the next comparison. */
3614 if (sec1
->target_index
- sec2
->target_index
!= 0)
3615 return sec1
->target_index
- sec2
->target_index
;
3620 else if (TOEND (sec2
))
3625 /* Sort by size, to put zero sized sections
3626 before others at the same address. */
3628 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->_raw_size
: 0;
3629 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->_raw_size
: 0;
3636 return sec1
->target_index
- sec2
->target_index
;
3639 /* Ian Lance Taylor writes:
3641 We shouldn't be using % with a negative signed number. That's just
3642 not good. We have to make sure either that the number is not
3643 negative, or that the number has an unsigned type. When the types
3644 are all the same size they wind up as unsigned. When file_ptr is a
3645 larger signed type, the arithmetic winds up as signed long long,
3648 What we're trying to say here is something like ``increase OFF by
3649 the least amount that will cause it to be equal to the VMA modulo
3651 /* In other words, something like:
3653 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3654 off_offset = off % bed->maxpagesize;
3655 if (vma_offset < off_offset)
3656 adjustment = vma_offset + bed->maxpagesize - off_offset;
3658 adjustment = vma_offset - off_offset;
3660 which can can be collapsed into the expression below. */
3663 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
3665 return ((vma
- off
) % maxpagesize
);
3668 /* Assign file positions to the sections based on the mapping from
3669 sections to segments. This function also sets up some fields in
3670 the file header, and writes out the program headers. */
3673 assign_file_positions_for_segments (bfd
*abfd
, struct bfd_link_info
*link_info
)
3675 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3677 struct elf_segment_map
*m
;
3679 Elf_Internal_Phdr
*phdrs
;
3681 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3682 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3683 Elf_Internal_Phdr
*p
;
3686 if (elf_tdata (abfd
)->segment_map
== NULL
)
3688 if (! map_sections_to_segments (abfd
))
3693 /* The placement algorithm assumes that non allocated sections are
3694 not in PT_LOAD segments. We ensure this here by removing such
3695 sections from the segment map. */
3696 for (m
= elf_tdata (abfd
)->segment_map
;
3700 unsigned int new_count
;
3703 if (m
->p_type
!= PT_LOAD
)
3707 for (i
= 0; i
< m
->count
; i
++)
3709 if ((m
->sections
[i
]->flags
& SEC_ALLOC
) != 0)
3712 m
->sections
[new_count
] = m
->sections
[i
];
3718 if (new_count
!= m
->count
)
3719 m
->count
= new_count
;
3723 if (bed
->elf_backend_modify_segment_map
)
3725 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
, link_info
))
3730 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3733 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3734 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3735 elf_elfheader (abfd
)->e_phnum
= count
;
3740 /* If we already counted the number of program segments, make sure
3741 that we allocated enough space. This happens when SIZEOF_HEADERS
3742 is used in a linker script. */
3743 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3744 if (alloc
!= 0 && count
> alloc
)
3746 ((*_bfd_error_handler
)
3747 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3748 bfd_get_filename (abfd
), alloc
, count
));
3749 bfd_set_error (bfd_error_bad_value
);
3756 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3757 phdrs
= bfd_alloc (abfd
, amt
);
3761 off
= bed
->s
->sizeof_ehdr
;
3762 off
+= alloc
* bed
->s
->sizeof_phdr
;
3769 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3776 /* If elf_segment_map is not from map_sections_to_segments, the
3777 sections may not be correctly ordered. NOTE: sorting should
3778 not be done to the PT_NOTE section of a corefile, which may
3779 contain several pseudo-sections artificially created by bfd.
3780 Sorting these pseudo-sections breaks things badly. */
3782 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3783 && m
->p_type
== PT_NOTE
))
3784 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3787 p
->p_type
= m
->p_type
;
3788 p
->p_flags
= m
->p_flags
;
3790 if (p
->p_type
== PT_LOAD
3792 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3794 if ((abfd
->flags
& D_PAGED
) != 0)
3795 off
+= vma_page_aligned_bias (m
->sections
[0]->vma
, off
,
3799 bfd_size_type align
;
3802 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3804 bfd_size_type secalign
;
3806 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3807 if (secalign
> align
)
3811 off
+= vma_page_aligned_bias (m
->sections
[0]->vma
, off
,
3819 p
->p_vaddr
= m
->sections
[0]->vma
;
3821 if (m
->p_paddr_valid
)
3822 p
->p_paddr
= m
->p_paddr
;
3823 else if (m
->count
== 0)
3826 p
->p_paddr
= m
->sections
[0]->lma
;
3828 if (p
->p_type
== PT_LOAD
3829 && (abfd
->flags
& D_PAGED
) != 0)
3830 p
->p_align
= bed
->maxpagesize
;
3831 else if (m
->count
== 0)
3832 p
->p_align
= 1 << bed
->s
->log_file_align
;
3840 if (m
->includes_filehdr
)
3842 if (! m
->p_flags_valid
)
3845 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3846 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3849 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3851 if (p
->p_vaddr
< (bfd_vma
) off
)
3853 (*_bfd_error_handler
)
3854 (_("%s: Not enough room for program headers, try linking with -N"),
3855 bfd_get_filename (abfd
));
3856 bfd_set_error (bfd_error_bad_value
);
3861 if (! m
->p_paddr_valid
)
3864 if (p
->p_type
== PT_LOAD
)
3866 filehdr_vaddr
= p
->p_vaddr
;
3867 filehdr_paddr
= p
->p_paddr
;
3871 if (m
->includes_phdrs
)
3873 if (! m
->p_flags_valid
)
3876 if (m
->includes_filehdr
)
3878 if (p
->p_type
== PT_LOAD
)
3880 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3881 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3886 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3890 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3891 p
->p_vaddr
-= off
- p
->p_offset
;
3892 if (! m
->p_paddr_valid
)
3893 p
->p_paddr
-= off
- p
->p_offset
;
3896 if (p
->p_type
== PT_LOAD
)
3898 phdrs_vaddr
= p
->p_vaddr
;
3899 phdrs_paddr
= p
->p_paddr
;
3902 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3905 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3906 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3909 if (p
->p_type
== PT_LOAD
3910 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3912 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3918 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3919 p
->p_filesz
+= adjust
;
3920 p
->p_memsz
+= adjust
;
3926 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3930 bfd_size_type align
;
3934 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3936 /* The section may have artificial alignment forced by a
3937 link script. Notice this case by the gap between the
3938 cumulative phdr lma and the section's lma. */
3939 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3941 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3943 p
->p_memsz
+= adjust
;
3944 if (p
->p_type
== PT_LOAD
3945 || (p
->p_type
== PT_NOTE
3946 && bfd_get_format (abfd
) == bfd_core
))
3951 if ((flags
& SEC_LOAD
) != 0
3952 || (flags
& SEC_THREAD_LOCAL
) != 0)
3953 p
->p_filesz
+= adjust
;
3956 if (p
->p_type
== PT_LOAD
)
3958 bfd_signed_vma adjust
;
3960 if ((flags
& SEC_LOAD
) != 0)
3962 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3966 else if ((flags
& SEC_ALLOC
) != 0)
3968 /* The section VMA must equal the file position
3969 modulo the page size. FIXME: I'm not sure if
3970 this adjustment is really necessary. We used to
3971 not have the SEC_LOAD case just above, and then
3972 this was necessary, but now I'm not sure. */
3973 if ((abfd
->flags
& D_PAGED
) != 0)
3974 adjust
= vma_page_aligned_bias (sec
->vma
, voff
,
3977 adjust
= vma_page_aligned_bias (sec
->vma
, voff
,
3987 (* _bfd_error_handler
) (_("\
3988 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3989 bfd_section_name (abfd
, sec
),
3994 p
->p_memsz
+= adjust
;
3997 if ((flags
& SEC_LOAD
) != 0)
3998 p
->p_filesz
+= adjust
;
4003 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
4004 used in a linker script we may have a section with
4005 SEC_LOAD clear but which is supposed to have
4007 if ((flags
& SEC_LOAD
) != 0
4008 || (flags
& SEC_HAS_CONTENTS
) != 0)
4009 off
+= sec
->_raw_size
;
4011 if ((flags
& SEC_ALLOC
) != 0
4012 && ((flags
& SEC_LOAD
) != 0
4013 || (flags
& SEC_THREAD_LOCAL
) == 0))
4014 voff
+= sec
->_raw_size
;
4017 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4019 /* The actual "note" segment has i == 0.
4020 This is the one that actually contains everything. */
4024 p
->p_filesz
= sec
->_raw_size
;
4025 off
+= sec
->_raw_size
;
4030 /* Fake sections -- don't need to be written. */
4033 flags
= sec
->flags
= 0;
4040 if ((sec
->flags
& SEC_LOAD
) != 0
4041 || (sec
->flags
& SEC_THREAD_LOCAL
) == 0
4042 || p
->p_type
== PT_TLS
)
4043 p
->p_memsz
+= sec
->_raw_size
;
4045 if ((flags
& SEC_LOAD
) != 0)
4046 p
->p_filesz
+= sec
->_raw_size
;
4048 if (p
->p_type
== PT_TLS
4049 && sec
->_raw_size
== 0
4050 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
4052 struct bfd_link_order
*o
;
4053 bfd_vma tbss_size
= 0;
4055 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
4056 if (tbss_size
< o
->offset
+ o
->size
)
4057 tbss_size
= o
->offset
+ o
->size
;
4059 p
->p_memsz
+= tbss_size
;
4062 if (align
> p
->p_align
4063 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
4067 if (! m
->p_flags_valid
)
4070 if ((flags
& SEC_CODE
) != 0)
4072 if ((flags
& SEC_READONLY
) == 0)
4078 /* Now that we have set the section file positions, we can set up
4079 the file positions for the non PT_LOAD segments. */
4080 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4084 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
4086 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
4087 p
->p_offset
= m
->sections
[0]->filepos
;
4091 if (m
->includes_filehdr
)
4093 p
->p_vaddr
= filehdr_vaddr
;
4094 if (! m
->p_paddr_valid
)
4095 p
->p_paddr
= filehdr_paddr
;
4097 else if (m
->includes_phdrs
)
4099 p
->p_vaddr
= phdrs_vaddr
;
4100 if (! m
->p_paddr_valid
)
4101 p
->p_paddr
= phdrs_paddr
;
4103 else if (p
->p_type
== PT_GNU_RELRO
)
4105 Elf_Internal_Phdr
*lp
;
4107 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4109 if (lp
->p_type
== PT_LOAD
4110 && lp
->p_vaddr
<= link_info
->relro_end
4111 && lp
->p_vaddr
>= link_info
->relro_start
4112 && lp
->p_vaddr
+ lp
->p_filesz
4113 >= link_info
->relro_end
)
4117 if (lp
< phdrs
+ count
4118 && link_info
->relro_end
> lp
->p_vaddr
)
4120 p
->p_vaddr
= lp
->p_vaddr
;
4121 p
->p_paddr
= lp
->p_paddr
;
4122 p
->p_offset
= lp
->p_offset
;
4123 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
4124 p
->p_memsz
= p
->p_filesz
;
4126 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
4130 memset (p
, 0, sizeof *p
);
4131 p
->p_type
= PT_NULL
;
4137 /* Clear out any program headers we allocated but did not use. */
4138 for (; count
< alloc
; count
++, p
++)
4140 memset (p
, 0, sizeof *p
);
4141 p
->p_type
= PT_NULL
;
4144 elf_tdata (abfd
)->phdr
= phdrs
;
4146 elf_tdata (abfd
)->next_file_pos
= off
;
4148 /* Write out the program headers. */
4149 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4150 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
4156 /* Get the size of the program header.
4158 If this is called by the linker before any of the section VMA's are set, it
4159 can't calculate the correct value for a strange memory layout. This only
4160 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4161 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4162 data segment (exclusive of .interp and .dynamic).
4164 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4165 will be two segments. */
4167 static bfd_size_type
4168 get_program_header_size (bfd
*abfd
)
4172 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4174 /* We can't return a different result each time we're called. */
4175 if (elf_tdata (abfd
)->program_header_size
!= 0)
4176 return elf_tdata (abfd
)->program_header_size
;
4178 if (elf_tdata (abfd
)->segment_map
!= NULL
)
4180 struct elf_segment_map
*m
;
4183 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4185 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4186 return elf_tdata (abfd
)->program_header_size
;
4189 /* Assume we will need exactly two PT_LOAD segments: one for text
4190 and one for data. */
4193 s
= bfd_get_section_by_name (abfd
, ".interp");
4194 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4196 /* If we have a loadable interpreter section, we need a
4197 PT_INTERP segment. In this case, assume we also need a
4198 PT_PHDR segment, although that may not be true for all
4203 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4205 /* We need a PT_DYNAMIC segment. */
4209 if (elf_tdata (abfd
)->eh_frame_hdr
)
4211 /* We need a PT_GNU_EH_FRAME segment. */
4215 if (elf_tdata (abfd
)->stack_flags
)
4217 /* We need a PT_GNU_STACK segment. */
4221 if (elf_tdata (abfd
)->relro
)
4223 /* We need a PT_GNU_RELRO segment. */
4227 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4229 if ((s
->flags
& SEC_LOAD
) != 0
4230 && strncmp (s
->name
, ".note", 5) == 0)
4232 /* We need a PT_NOTE segment. */
4237 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4239 if (s
->flags
& SEC_THREAD_LOCAL
)
4241 /* We need a PT_TLS segment. */
4247 /* Let the backend count up any program headers it might need. */
4248 if (bed
->elf_backend_additional_program_headers
)
4252 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
4258 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4259 return elf_tdata (abfd
)->program_header_size
;
4262 /* Work out the file positions of all the sections. This is called by
4263 _bfd_elf_compute_section_file_positions. All the section sizes and
4264 VMAs must be known before this is called.
4266 We do not consider reloc sections at this point, unless they form
4267 part of the loadable image. Reloc sections are assigned file
4268 positions in assign_file_positions_for_relocs, which is called by
4269 write_object_contents and final_link.
4271 We also don't set the positions of the .symtab and .strtab here. */
4274 assign_file_positions_except_relocs (bfd
*abfd
,
4275 struct bfd_link_info
*link_info
)
4277 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
4278 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
4279 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4280 unsigned int num_sec
= elf_numsections (abfd
);
4282 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4284 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4285 && bfd_get_format (abfd
) != bfd_core
)
4287 Elf_Internal_Shdr
**hdrpp
;
4290 /* Start after the ELF header. */
4291 off
= i_ehdrp
->e_ehsize
;
4293 /* We are not creating an executable, which means that we are
4294 not creating a program header, and that the actual order of
4295 the sections in the file is unimportant. */
4296 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4298 Elf_Internal_Shdr
*hdr
;
4301 if (hdr
->sh_type
== SHT_REL
4302 || hdr
->sh_type
== SHT_RELA
4303 || i
== tdata
->symtab_section
4304 || i
== tdata
->symtab_shndx_section
4305 || i
== tdata
->strtab_section
)
4307 hdr
->sh_offset
= -1;
4310 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4312 if (i
== SHN_LORESERVE
- 1)
4314 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4315 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4322 Elf_Internal_Shdr
**hdrpp
;
4324 /* Assign file positions for the loaded sections based on the
4325 assignment of sections to segments. */
4326 if (! assign_file_positions_for_segments (abfd
, link_info
))
4329 /* Assign file positions for the other sections. */
4331 off
= elf_tdata (abfd
)->next_file_pos
;
4332 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4334 Elf_Internal_Shdr
*hdr
;
4337 if (hdr
->bfd_section
!= NULL
4338 && hdr
->bfd_section
->filepos
!= 0)
4339 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
4340 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4342 ((*_bfd_error_handler
)
4343 (_("%s: warning: allocated section `%s' not in segment"),
4344 bfd_get_filename (abfd
),
4345 (hdr
->bfd_section
== NULL
4347 : hdr
->bfd_section
->name
)));
4348 if ((abfd
->flags
& D_PAGED
) != 0)
4349 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4352 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4354 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4357 else if (hdr
->sh_type
== SHT_REL
4358 || hdr
->sh_type
== SHT_RELA
4359 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4360 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4361 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4362 hdr
->sh_offset
= -1;
4364 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4366 if (i
== SHN_LORESERVE
- 1)
4368 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4369 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4374 /* Place the section headers. */
4375 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
4376 i_ehdrp
->e_shoff
= off
;
4377 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4379 elf_tdata (abfd
)->next_file_pos
= off
;
4385 prep_headers (bfd
*abfd
)
4387 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4388 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4389 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4390 struct elf_strtab_hash
*shstrtab
;
4391 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4393 i_ehdrp
= elf_elfheader (abfd
);
4394 i_shdrp
= elf_elfsections (abfd
);
4396 shstrtab
= _bfd_elf_strtab_init ();
4397 if (shstrtab
== NULL
)
4400 elf_shstrtab (abfd
) = shstrtab
;
4402 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4403 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4404 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4405 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4407 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4408 i_ehdrp
->e_ident
[EI_DATA
] =
4409 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4410 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4412 if ((abfd
->flags
& DYNAMIC
) != 0)
4413 i_ehdrp
->e_type
= ET_DYN
;
4414 else if ((abfd
->flags
& EXEC_P
) != 0)
4415 i_ehdrp
->e_type
= ET_EXEC
;
4416 else if (bfd_get_format (abfd
) == bfd_core
)
4417 i_ehdrp
->e_type
= ET_CORE
;
4419 i_ehdrp
->e_type
= ET_REL
;
4421 switch (bfd_get_arch (abfd
))
4423 case bfd_arch_unknown
:
4424 i_ehdrp
->e_machine
= EM_NONE
;
4427 /* There used to be a long list of cases here, each one setting
4428 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4429 in the corresponding bfd definition. To avoid duplication,
4430 the switch was removed. Machines that need special handling
4431 can generally do it in elf_backend_final_write_processing(),
4432 unless they need the information earlier than the final write.
4433 Such need can generally be supplied by replacing the tests for
4434 e_machine with the conditions used to determine it. */
4436 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
4439 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4440 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4442 /* No program header, for now. */
4443 i_ehdrp
->e_phoff
= 0;
4444 i_ehdrp
->e_phentsize
= 0;
4445 i_ehdrp
->e_phnum
= 0;
4447 /* Each bfd section is section header entry. */
4448 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4449 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4451 /* If we're building an executable, we'll need a program header table. */
4452 if (abfd
->flags
& EXEC_P
)
4454 /* It all happens later. */
4456 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
4458 /* elf_build_phdrs() returns a (NULL-terminated) array of
4459 Elf_Internal_Phdrs. */
4460 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
4461 i_ehdrp
->e_phoff
= outbase
;
4462 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
4467 i_ehdrp
->e_phentsize
= 0;
4469 i_ehdrp
->e_phoff
= 0;
4472 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4473 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
4474 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4475 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
4476 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4477 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
4478 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4479 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4480 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4486 /* Assign file positions for all the reloc sections which are not part
4487 of the loadable file image. */
4490 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
4493 unsigned int i
, num_sec
;
4494 Elf_Internal_Shdr
**shdrpp
;
4496 off
= elf_tdata (abfd
)->next_file_pos
;
4498 num_sec
= elf_numsections (abfd
);
4499 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4501 Elf_Internal_Shdr
*shdrp
;
4504 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4505 && shdrp
->sh_offset
== -1)
4506 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
4509 elf_tdata (abfd
)->next_file_pos
= off
;
4513 _bfd_elf_write_object_contents (bfd
*abfd
)
4515 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4516 Elf_Internal_Ehdr
*i_ehdrp
;
4517 Elf_Internal_Shdr
**i_shdrp
;
4519 unsigned int count
, num_sec
;
4521 if (! abfd
->output_has_begun
4522 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
4525 i_shdrp
= elf_elfsections (abfd
);
4526 i_ehdrp
= elf_elfheader (abfd
);
4529 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4533 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4535 /* After writing the headers, we need to write the sections too... */
4536 num_sec
= elf_numsections (abfd
);
4537 for (count
= 1; count
< num_sec
; count
++)
4539 if (bed
->elf_backend_section_processing
)
4540 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4541 if (i_shdrp
[count
]->contents
)
4543 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4545 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4546 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4549 if (count
== SHN_LORESERVE
- 1)
4550 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4553 /* Write out the section header names. */
4554 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4555 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4558 if (bed
->elf_backend_final_write_processing
)
4559 (*bed
->elf_backend_final_write_processing
) (abfd
,
4560 elf_tdata (abfd
)->linker
);
4562 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4566 _bfd_elf_write_corefile_contents (bfd
*abfd
)
4568 /* Hopefully this can be done just like an object file. */
4569 return _bfd_elf_write_object_contents (abfd
);
4572 /* Given a section, search the header to find them. */
4575 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
4577 const struct elf_backend_data
*bed
;
4580 if (elf_section_data (asect
) != NULL
4581 && elf_section_data (asect
)->this_idx
!= 0)
4582 return elf_section_data (asect
)->this_idx
;
4584 if (bfd_is_abs_section (asect
))
4586 else if (bfd_is_com_section (asect
))
4588 else if (bfd_is_und_section (asect
))
4592 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4593 int maxindex
= elf_numsections (abfd
);
4595 for (index
= 1; index
< maxindex
; index
++)
4597 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4599 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4605 bed
= get_elf_backend_data (abfd
);
4606 if (bed
->elf_backend_section_from_bfd_section
)
4610 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4615 bfd_set_error (bfd_error_nonrepresentable_section
);
4620 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4624 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
4626 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4628 flagword flags
= asym_ptr
->flags
;
4630 /* When gas creates relocations against local labels, it creates its
4631 own symbol for the section, but does put the symbol into the
4632 symbol chain, so udata is 0. When the linker is generating
4633 relocatable output, this section symbol may be for one of the
4634 input sections rather than the output section. */
4635 if (asym_ptr
->udata
.i
== 0
4636 && (flags
& BSF_SECTION_SYM
)
4637 && asym_ptr
->section
)
4641 if (asym_ptr
->section
->output_section
!= NULL
)
4642 indx
= asym_ptr
->section
->output_section
->index
;
4644 indx
= asym_ptr
->section
->index
;
4645 if (indx
< elf_num_section_syms (abfd
)
4646 && elf_section_syms (abfd
)[indx
] != NULL
)
4647 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4650 idx
= asym_ptr
->udata
.i
;
4654 /* This case can occur when using --strip-symbol on a symbol
4655 which is used in a relocation entry. */
4656 (*_bfd_error_handler
)
4657 (_("%s: symbol `%s' required but not present"),
4658 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4659 bfd_set_error (bfd_error_no_symbols
);
4666 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4667 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4668 elf_symbol_flags (flags
));
4676 /* Copy private BFD data. This copies any program header information. */
4679 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
4681 Elf_Internal_Ehdr
*iehdr
;
4682 struct elf_segment_map
*map
;
4683 struct elf_segment_map
*map_first
;
4684 struct elf_segment_map
**pointer_to_map
;
4685 Elf_Internal_Phdr
*segment
;
4688 unsigned int num_segments
;
4689 bfd_boolean phdr_included
= FALSE
;
4690 bfd_vma maxpagesize
;
4691 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
4692 unsigned int phdr_adjust_num
= 0;
4693 const struct elf_backend_data
*bed
;
4695 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4696 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4699 if (elf_tdata (ibfd
)->phdr
== NULL
)
4702 bed
= get_elf_backend_data (ibfd
);
4703 iehdr
= elf_elfheader (ibfd
);
4706 pointer_to_map
= &map_first
;
4708 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4709 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4711 /* Returns the end address of the segment + 1. */
4712 #define SEGMENT_END(segment, start) \
4713 (start + (segment->p_memsz > segment->p_filesz \
4714 ? segment->p_memsz : segment->p_filesz))
4716 #define SECTION_SIZE(section, segment) \
4717 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4718 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4719 ? section->_raw_size : 0)
4721 /* Returns TRUE if the given section is contained within
4722 the given segment. VMA addresses are compared. */
4723 #define IS_CONTAINED_BY_VMA(section, segment) \
4724 (section->vma >= segment->p_vaddr \
4725 && (section->vma + SECTION_SIZE (section, segment) \
4726 <= (SEGMENT_END (segment, segment->p_vaddr))))
4728 /* Returns TRUE if the given section is contained within
4729 the given segment. LMA addresses are compared. */
4730 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4731 (section->lma >= base \
4732 && (section->lma + SECTION_SIZE (section, segment) \
4733 <= SEGMENT_END (segment, base)))
4735 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4736 #define IS_COREFILE_NOTE(p, s) \
4737 (p->p_type == PT_NOTE \
4738 && bfd_get_format (ibfd) == bfd_core \
4739 && s->vma == 0 && s->lma == 0 \
4740 && (bfd_vma) s->filepos >= p->p_offset \
4741 && ((bfd_vma) s->filepos + s->_raw_size \
4742 <= p->p_offset + p->p_filesz))
4744 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4745 linker, which generates a PT_INTERP section with p_vaddr and
4746 p_memsz set to 0. */
4747 #define IS_SOLARIS_PT_INTERP(p, s) \
4749 && p->p_paddr == 0 \
4750 && p->p_memsz == 0 \
4751 && p->p_filesz > 0 \
4752 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4753 && s->_raw_size > 0 \
4754 && (bfd_vma) s->filepos >= p->p_offset \
4755 && ((bfd_vma) s->filepos + s->_raw_size \
4756 <= p->p_offset + p->p_filesz))
4758 /* Decide if the given section should be included in the given segment.
4759 A section will be included if:
4760 1. It is within the address space of the segment -- we use the LMA
4761 if that is set for the segment and the VMA otherwise,
4762 2. It is an allocated segment,
4763 3. There is an output section associated with it,
4764 4. The section has not already been allocated to a previous segment.
4765 5. PT_GNU_STACK segments do not include any sections.
4766 6. PT_TLS segment includes only SHF_TLS sections.
4767 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4768 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4769 ((((segment->p_paddr \
4770 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4771 : IS_CONTAINED_BY_VMA (section, segment)) \
4772 && (section->flags & SEC_ALLOC) != 0) \
4773 || IS_COREFILE_NOTE (segment, section)) \
4774 && section->output_section != NULL \
4775 && segment->p_type != PT_GNU_STACK \
4776 && (segment->p_type != PT_TLS \
4777 || (section->flags & SEC_THREAD_LOCAL)) \
4778 && (segment->p_type == PT_LOAD \
4779 || segment->p_type == PT_TLS \
4780 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4781 && ! section->segment_mark)
4783 /* Returns TRUE iff seg1 starts after the end of seg2. */
4784 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4785 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4787 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4788 their VMA address ranges and their LMA address ranges overlap.
4789 It is possible to have overlapping VMA ranges without overlapping LMA
4790 ranges. RedBoot images for example can have both .data and .bss mapped
4791 to the same VMA range, but with the .data section mapped to a different
4793 #define SEGMENT_OVERLAPS(seg1, seg2) \
4794 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4795 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4796 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4797 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4799 /* Initialise the segment mark field. */
4800 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4801 section
->segment_mark
= FALSE
;
4803 /* Scan through the segments specified in the program header
4804 of the input BFD. For this first scan we look for overlaps
4805 in the loadable segments. These can be created by weird
4806 parameters to objcopy. Also, fix some solaris weirdness. */
4807 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4812 Elf_Internal_Phdr
*segment2
;
4814 if (segment
->p_type
== PT_INTERP
)
4815 for (section
= ibfd
->sections
; section
; section
= section
->next
)
4816 if (IS_SOLARIS_PT_INTERP (segment
, section
))
4818 /* Mininal change so that the normal section to segment
4819 assignment code will work. */
4820 segment
->p_vaddr
= section
->vma
;
4824 if (segment
->p_type
!= PT_LOAD
)
4827 /* Determine if this segment overlaps any previous segments. */
4828 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4830 bfd_signed_vma extra_length
;
4832 if (segment2
->p_type
!= PT_LOAD
4833 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4836 /* Merge the two segments together. */
4837 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4839 /* Extend SEGMENT2 to include SEGMENT and then delete
4842 SEGMENT_END (segment
, segment
->p_vaddr
)
4843 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4845 if (extra_length
> 0)
4847 segment2
->p_memsz
+= extra_length
;
4848 segment2
->p_filesz
+= extra_length
;
4851 segment
->p_type
= PT_NULL
;
4853 /* Since we have deleted P we must restart the outer loop. */
4855 segment
= elf_tdata (ibfd
)->phdr
;
4860 /* Extend SEGMENT to include SEGMENT2 and then delete
4863 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4864 - SEGMENT_END (segment
, segment
->p_vaddr
);
4866 if (extra_length
> 0)
4868 segment
->p_memsz
+= extra_length
;
4869 segment
->p_filesz
+= extra_length
;
4872 segment2
->p_type
= PT_NULL
;
4877 /* The second scan attempts to assign sections to segments. */
4878 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4882 unsigned int section_count
;
4883 asection
** sections
;
4884 asection
* output_section
;
4886 bfd_vma matching_lma
;
4887 bfd_vma suggested_lma
;
4891 if (segment
->p_type
== PT_NULL
)
4894 /* Compute how many sections might be placed into this segment. */
4895 for (section
= ibfd
->sections
, section_count
= 0;
4897 section
= section
->next
)
4898 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4901 /* Allocate a segment map big enough to contain
4902 all of the sections we have selected. */
4903 amt
= sizeof (struct elf_segment_map
);
4904 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4905 map
= bfd_alloc (obfd
, amt
);
4909 /* Initialise the fields of the segment map. Default to
4910 using the physical address of the segment in the input BFD. */
4912 map
->p_type
= segment
->p_type
;
4913 map
->p_flags
= segment
->p_flags
;
4914 map
->p_flags_valid
= 1;
4915 map
->p_paddr
= segment
->p_paddr
;
4916 map
->p_paddr_valid
= 1;
4918 /* Determine if this segment contains the ELF file header
4919 and if it contains the program headers themselves. */
4920 map
->includes_filehdr
= (segment
->p_offset
== 0
4921 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4923 map
->includes_phdrs
= 0;
4925 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4927 map
->includes_phdrs
=
4928 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4929 && (segment
->p_offset
+ segment
->p_filesz
4930 >= ((bfd_vma
) iehdr
->e_phoff
4931 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4933 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4934 phdr_included
= TRUE
;
4937 if (section_count
== 0)
4939 /* Special segments, such as the PT_PHDR segment, may contain
4940 no sections, but ordinary, loadable segments should contain
4941 something. They are allowed by the ELF spec however, so only
4942 a warning is produced. */
4943 if (segment
->p_type
== PT_LOAD
)
4944 (*_bfd_error_handler
)
4945 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4946 bfd_archive_filename (ibfd
));
4949 *pointer_to_map
= map
;
4950 pointer_to_map
= &map
->next
;
4955 /* Now scan the sections in the input BFD again and attempt
4956 to add their corresponding output sections to the segment map.
4957 The problem here is how to handle an output section which has
4958 been moved (ie had its LMA changed). There are four possibilities:
4960 1. None of the sections have been moved.
4961 In this case we can continue to use the segment LMA from the
4964 2. All of the sections have been moved by the same amount.
4965 In this case we can change the segment's LMA to match the LMA
4966 of the first section.
4968 3. Some of the sections have been moved, others have not.
4969 In this case those sections which have not been moved can be
4970 placed in the current segment which will have to have its size,
4971 and possibly its LMA changed, and a new segment or segments will
4972 have to be created to contain the other sections.
4974 4. The sections have been moved, but not by the same amount.
4975 In this case we can change the segment's LMA to match the LMA
4976 of the first section and we will have to create a new segment
4977 or segments to contain the other sections.
4979 In order to save time, we allocate an array to hold the section
4980 pointers that we are interested in. As these sections get assigned
4981 to a segment, they are removed from this array. */
4983 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4984 to work around this long long bug. */
4985 amt
= section_count
* sizeof (asection
*);
4986 sections
= bfd_malloc (amt
);
4987 if (sections
== NULL
)
4990 /* Step One: Scan for segment vs section LMA conflicts.
4991 Also add the sections to the section array allocated above.
4992 Also add the sections to the current segment. In the common
4993 case, where the sections have not been moved, this means that
4994 we have completely filled the segment, and there is nothing
5000 for (j
= 0, section
= ibfd
->sections
;
5002 section
= section
->next
)
5004 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5006 output_section
= section
->output_section
;
5008 sections
[j
++] = section
;
5010 /* The Solaris native linker always sets p_paddr to 0.
5011 We try to catch that case here, and set it to the
5012 correct value. Note - some backends require that
5013 p_paddr be left as zero. */
5014 if (segment
->p_paddr
== 0
5015 && segment
->p_vaddr
!= 0
5016 && (! bed
->want_p_paddr_set_to_zero
)
5018 && output_section
->lma
!= 0
5019 && (output_section
->vma
== (segment
->p_vaddr
5020 + (map
->includes_filehdr
5023 + (map
->includes_phdrs
5025 * iehdr
->e_phentsize
)
5027 map
->p_paddr
= segment
->p_vaddr
;
5029 /* Match up the physical address of the segment with the
5030 LMA address of the output section. */
5031 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5032 || IS_COREFILE_NOTE (segment
, section
)
5033 || (bed
->want_p_paddr_set_to_zero
&&
5034 IS_CONTAINED_BY_VMA (output_section
, segment
))
5037 if (matching_lma
== 0)
5038 matching_lma
= output_section
->lma
;
5040 /* We assume that if the section fits within the segment
5041 then it does not overlap any other section within that
5043 map
->sections
[isec
++] = output_section
;
5045 else if (suggested_lma
== 0)
5046 suggested_lma
= output_section
->lma
;
5050 BFD_ASSERT (j
== section_count
);
5052 /* Step Two: Adjust the physical address of the current segment,
5054 if (isec
== section_count
)
5056 /* All of the sections fitted within the segment as currently
5057 specified. This is the default case. Add the segment to
5058 the list of built segments and carry on to process the next
5059 program header in the input BFD. */
5060 map
->count
= section_count
;
5061 *pointer_to_map
= map
;
5062 pointer_to_map
= &map
->next
;
5069 if (matching_lma
!= 0)
5071 /* At least one section fits inside the current segment.
5072 Keep it, but modify its physical address to match the
5073 LMA of the first section that fitted. */
5074 map
->p_paddr
= matching_lma
;
5078 /* None of the sections fitted inside the current segment.
5079 Change the current segment's physical address to match
5080 the LMA of the first section. */
5081 map
->p_paddr
= suggested_lma
;
5084 /* Offset the segment physical address from the lma
5085 to allow for space taken up by elf headers. */
5086 if (map
->includes_filehdr
)
5087 map
->p_paddr
-= iehdr
->e_ehsize
;
5089 if (map
->includes_phdrs
)
5091 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5093 /* iehdr->e_phnum is just an estimate of the number
5094 of program headers that we will need. Make a note
5095 here of the number we used and the segment we chose
5096 to hold these headers, so that we can adjust the
5097 offset when we know the correct value. */
5098 phdr_adjust_num
= iehdr
->e_phnum
;
5099 phdr_adjust_seg
= map
;
5103 /* Step Three: Loop over the sections again, this time assigning
5104 those that fit to the current segment and removing them from the
5105 sections array; but making sure not to leave large gaps. Once all
5106 possible sections have been assigned to the current segment it is
5107 added to the list of built segments and if sections still remain
5108 to be assigned, a new segment is constructed before repeating
5116 /* Fill the current segment with sections that fit. */
5117 for (j
= 0; j
< section_count
; j
++)
5119 section
= sections
[j
];
5121 if (section
== NULL
)
5124 output_section
= section
->output_section
;
5126 BFD_ASSERT (output_section
!= NULL
);
5128 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5129 || IS_COREFILE_NOTE (segment
, section
))
5131 if (map
->count
== 0)
5133 /* If the first section in a segment does not start at
5134 the beginning of the segment, then something is
5136 if (output_section
->lma
!=
5138 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5139 + (map
->includes_phdrs
5140 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5146 asection
* prev_sec
;
5148 prev_sec
= map
->sections
[map
->count
- 1];
5150 /* If the gap between the end of the previous section
5151 and the start of this section is more than
5152 maxpagesize then we need to start a new segment. */
5153 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
5155 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5156 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
5157 > output_section
->lma
))
5159 if (suggested_lma
== 0)
5160 suggested_lma
= output_section
->lma
;
5166 map
->sections
[map
->count
++] = output_section
;
5169 section
->segment_mark
= TRUE
;
5171 else if (suggested_lma
== 0)
5172 suggested_lma
= output_section
->lma
;
5175 BFD_ASSERT (map
->count
> 0);
5177 /* Add the current segment to the list of built segments. */
5178 *pointer_to_map
= map
;
5179 pointer_to_map
= &map
->next
;
5181 if (isec
< section_count
)
5183 /* We still have not allocated all of the sections to
5184 segments. Create a new segment here, initialise it
5185 and carry on looping. */
5186 amt
= sizeof (struct elf_segment_map
);
5187 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5188 map
= bfd_alloc (obfd
, amt
);
5195 /* Initialise the fields of the segment map. Set the physical
5196 physical address to the LMA of the first section that has
5197 not yet been assigned. */
5199 map
->p_type
= segment
->p_type
;
5200 map
->p_flags
= segment
->p_flags
;
5201 map
->p_flags_valid
= 1;
5202 map
->p_paddr
= suggested_lma
;
5203 map
->p_paddr_valid
= 1;
5204 map
->includes_filehdr
= 0;
5205 map
->includes_phdrs
= 0;
5208 while (isec
< section_count
);
5213 /* The Solaris linker creates program headers in which all the
5214 p_paddr fields are zero. When we try to objcopy or strip such a
5215 file, we get confused. Check for this case, and if we find it
5216 reset the p_paddr_valid fields. */
5217 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5218 if (map
->p_paddr
!= 0)
5221 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5222 map
->p_paddr_valid
= 0;
5224 elf_tdata (obfd
)->segment_map
= map_first
;
5226 /* If we had to estimate the number of program headers that were
5227 going to be needed, then check our estimate now and adjust
5228 the offset if necessary. */
5229 if (phdr_adjust_seg
!= NULL
)
5233 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5236 if (count
> phdr_adjust_num
)
5237 phdr_adjust_seg
->p_paddr
5238 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5242 /* Final Step: Sort the segments into ascending order of physical
5244 if (map_first
!= NULL
)
5246 struct elf_segment_map
*prev
;
5249 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
5251 /* Yes I know - its a bubble sort.... */
5252 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
5254 /* Swap map and map->next. */
5255 prev
->next
= map
->next
;
5256 map
->next
= map
->next
->next
;
5257 prev
->next
->next
= map
;
5268 #undef IS_CONTAINED_BY_VMA
5269 #undef IS_CONTAINED_BY_LMA
5270 #undef IS_COREFILE_NOTE
5271 #undef IS_SOLARIS_PT_INTERP
5272 #undef INCLUDE_SECTION_IN_SEGMENT
5273 #undef SEGMENT_AFTER_SEGMENT
5274 #undef SEGMENT_OVERLAPS
5278 /* Copy private section information. This copies over the entsize
5279 field, and sometimes the info field. */
5282 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
5287 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5289 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5290 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5293 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
5297 /* Only set up the segments if there are no more SEC_ALLOC
5298 sections. FIXME: This won't do the right thing if objcopy is
5299 used to remove the last SEC_ALLOC section, since objcopy
5300 won't call this routine in that case. */
5301 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
5302 if ((s
->flags
& SEC_ALLOC
) != 0)
5306 if (! copy_private_bfd_data (ibfd
, obfd
))
5311 ihdr
= &elf_section_data (isec
)->this_hdr
;
5312 ohdr
= &elf_section_data (osec
)->this_hdr
;
5314 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
5316 if (ihdr
->sh_type
== SHT_SYMTAB
5317 || ihdr
->sh_type
== SHT_DYNSYM
5318 || ihdr
->sh_type
== SHT_GNU_verneed
5319 || ihdr
->sh_type
== SHT_GNU_verdef
)
5320 ohdr
->sh_info
= ihdr
->sh_info
;
5322 /* Set things up for objcopy. The output SHT_GROUP section will
5323 have its elf_next_in_group pointing back to the input group
5325 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5326 elf_group_name (osec
) = elf_group_name (isec
);
5328 osec
->use_rela_p
= isec
->use_rela_p
;
5333 /* Copy private symbol information. If this symbol is in a section
5334 which we did not map into a BFD section, try to map the section
5335 index correctly. We use special macro definitions for the mapped
5336 section indices; these definitions are interpreted by the
5337 swap_out_syms function. */
5339 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5340 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5341 #define MAP_STRTAB (SHN_HIOS + 3)
5342 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5343 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5346 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
5351 elf_symbol_type
*isym
, *osym
;
5353 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5354 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5357 isym
= elf_symbol_from (ibfd
, isymarg
);
5358 osym
= elf_symbol_from (obfd
, osymarg
);
5362 && bfd_is_abs_section (isym
->symbol
.section
))
5366 shndx
= isym
->internal_elf_sym
.st_shndx
;
5367 if (shndx
== elf_onesymtab (ibfd
))
5368 shndx
= MAP_ONESYMTAB
;
5369 else if (shndx
== elf_dynsymtab (ibfd
))
5370 shndx
= MAP_DYNSYMTAB
;
5371 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
5373 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
5374 shndx
= MAP_SHSTRTAB
;
5375 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
5376 shndx
= MAP_SYM_SHNDX
;
5377 osym
->internal_elf_sym
.st_shndx
= shndx
;
5383 /* Swap out the symbols. */
5386 swap_out_syms (bfd
*abfd
,
5387 struct bfd_strtab_hash
**sttp
,
5390 const struct elf_backend_data
*bed
;
5393 struct bfd_strtab_hash
*stt
;
5394 Elf_Internal_Shdr
*symtab_hdr
;
5395 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5396 Elf_Internal_Shdr
*symstrtab_hdr
;
5397 char *outbound_syms
;
5398 char *outbound_shndx
;
5401 bfd_boolean name_local_sections
;
5403 if (!elf_map_symbols (abfd
))
5406 /* Dump out the symtabs. */
5407 stt
= _bfd_elf_stringtab_init ();
5411 bed
= get_elf_backend_data (abfd
);
5412 symcount
= bfd_get_symcount (abfd
);
5413 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5414 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5415 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5416 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5417 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5418 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
5420 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5421 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5423 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
5424 outbound_syms
= bfd_alloc (abfd
, amt
);
5425 if (outbound_syms
== NULL
)
5427 _bfd_stringtab_free (stt
);
5430 symtab_hdr
->contents
= outbound_syms
;
5432 outbound_shndx
= NULL
;
5433 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5434 if (symtab_shndx_hdr
->sh_name
!= 0)
5436 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5437 outbound_shndx
= bfd_zalloc (abfd
, amt
);
5438 if (outbound_shndx
== NULL
)
5440 _bfd_stringtab_free (stt
);
5444 symtab_shndx_hdr
->contents
= outbound_shndx
;
5445 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5446 symtab_shndx_hdr
->sh_size
= amt
;
5447 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5448 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5451 /* Now generate the data (for "contents"). */
5453 /* Fill in zeroth symbol and swap it out. */
5454 Elf_Internal_Sym sym
;
5460 sym
.st_shndx
= SHN_UNDEF
;
5461 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5462 outbound_syms
+= bed
->s
->sizeof_sym
;
5463 if (outbound_shndx
!= NULL
)
5464 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5468 = (bed
->elf_backend_name_local_section_symbols
5469 && bed
->elf_backend_name_local_section_symbols (abfd
));
5471 syms
= bfd_get_outsymbols (abfd
);
5472 for (idx
= 0; idx
< symcount
; idx
++)
5474 Elf_Internal_Sym sym
;
5475 bfd_vma value
= syms
[idx
]->value
;
5476 elf_symbol_type
*type_ptr
;
5477 flagword flags
= syms
[idx
]->flags
;
5480 if (!name_local_sections
5481 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5483 /* Local section symbols have no name. */
5488 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5491 if (sym
.st_name
== (unsigned long) -1)
5493 _bfd_stringtab_free (stt
);
5498 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5500 if ((flags
& BSF_SECTION_SYM
) == 0
5501 && bfd_is_com_section (syms
[idx
]->section
))
5503 /* ELF common symbols put the alignment into the `value' field,
5504 and the size into the `size' field. This is backwards from
5505 how BFD handles it, so reverse it here. */
5506 sym
.st_size
= value
;
5507 if (type_ptr
== NULL
5508 || type_ptr
->internal_elf_sym
.st_value
== 0)
5509 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5511 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5512 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5513 (abfd
, syms
[idx
]->section
);
5517 asection
*sec
= syms
[idx
]->section
;
5520 if (sec
->output_section
)
5522 value
+= sec
->output_offset
;
5523 sec
= sec
->output_section
;
5526 /* Don't add in the section vma for relocatable output. */
5527 if (! relocatable_p
)
5529 sym
.st_value
= value
;
5530 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5532 if (bfd_is_abs_section (sec
)
5534 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5536 /* This symbol is in a real ELF section which we did
5537 not create as a BFD section. Undo the mapping done
5538 by copy_private_symbol_data. */
5539 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5543 shndx
= elf_onesymtab (abfd
);
5546 shndx
= elf_dynsymtab (abfd
);
5549 shndx
= elf_tdata (abfd
)->strtab_section
;
5552 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5555 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5563 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5569 /* Writing this would be a hell of a lot easier if
5570 we had some decent documentation on bfd, and
5571 knew what to expect of the library, and what to
5572 demand of applications. For example, it
5573 appears that `objcopy' might not set the
5574 section of a symbol to be a section that is
5575 actually in the output file. */
5576 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5579 _bfd_error_handler (_("\
5580 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5581 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
5583 bfd_set_error (bfd_error_invalid_operation
);
5584 _bfd_stringtab_free (stt
);
5588 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5589 BFD_ASSERT (shndx
!= -1);
5593 sym
.st_shndx
= shndx
;
5596 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5598 else if ((flags
& BSF_FUNCTION
) != 0)
5600 else if ((flags
& BSF_OBJECT
) != 0)
5605 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5608 /* Processor-specific types. */
5609 if (type_ptr
!= NULL
5610 && bed
->elf_backend_get_symbol_type
)
5611 type
= ((*bed
->elf_backend_get_symbol_type
)
5612 (&type_ptr
->internal_elf_sym
, type
));
5614 if (flags
& BSF_SECTION_SYM
)
5616 if (flags
& BSF_GLOBAL
)
5617 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5619 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5621 else if (bfd_is_com_section (syms
[idx
]->section
))
5622 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5623 else if (bfd_is_und_section (syms
[idx
]->section
))
5624 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5628 else if (flags
& BSF_FILE
)
5629 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5632 int bind
= STB_LOCAL
;
5634 if (flags
& BSF_LOCAL
)
5636 else if (flags
& BSF_WEAK
)
5638 else if (flags
& BSF_GLOBAL
)
5641 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5644 if (type_ptr
!= NULL
)
5645 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5649 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5650 outbound_syms
+= bed
->s
->sizeof_sym
;
5651 if (outbound_shndx
!= NULL
)
5652 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5656 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5657 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5659 symstrtab_hdr
->sh_flags
= 0;
5660 symstrtab_hdr
->sh_addr
= 0;
5661 symstrtab_hdr
->sh_entsize
= 0;
5662 symstrtab_hdr
->sh_link
= 0;
5663 symstrtab_hdr
->sh_info
= 0;
5664 symstrtab_hdr
->sh_addralign
= 1;
5669 /* Return the number of bytes required to hold the symtab vector.
5671 Note that we base it on the count plus 1, since we will null terminate
5672 the vector allocated based on this size. However, the ELF symbol table
5673 always has a dummy entry as symbol #0, so it ends up even. */
5676 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
5680 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5682 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5683 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5685 symtab_size
-= sizeof (asymbol
*);
5691 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
5695 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5697 if (elf_dynsymtab (abfd
) == 0)
5699 bfd_set_error (bfd_error_invalid_operation
);
5703 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5704 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5706 symtab_size
-= sizeof (asymbol
*);
5712 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
5715 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5718 /* Canonicalize the relocs. */
5721 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
5728 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5730 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
5733 tblptr
= section
->relocation
;
5734 for (i
= 0; i
< section
->reloc_count
; i
++)
5735 *relptr
++ = tblptr
++;
5739 return section
->reloc_count
;
5743 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
5745 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5746 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
5749 bfd_get_symcount (abfd
) = symcount
;
5754 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
5755 asymbol
**allocation
)
5757 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5758 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
5761 bfd_get_dynamic_symcount (abfd
) = symcount
;
5765 /* Return the size required for the dynamic reloc entries. Any
5766 section that was actually installed in the BFD, and has type
5767 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5768 considered to be a dynamic reloc section. */
5771 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
5776 if (elf_dynsymtab (abfd
) == 0)
5778 bfd_set_error (bfd_error_invalid_operation
);
5782 ret
= sizeof (arelent
*);
5783 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5784 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5785 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5786 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5787 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5788 * sizeof (arelent
*));
5793 /* Canonicalize the dynamic relocation entries. Note that we return
5794 the dynamic relocations as a single block, although they are
5795 actually associated with particular sections; the interface, which
5796 was designed for SunOS style shared libraries, expects that there
5797 is only one set of dynamic relocs. Any section that was actually
5798 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5799 the dynamic symbol table, is considered to be a dynamic reloc
5803 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
5807 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
5811 if (elf_dynsymtab (abfd
) == 0)
5813 bfd_set_error (bfd_error_invalid_operation
);
5817 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5819 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5821 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5822 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5823 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5828 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
5830 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5832 for (i
= 0; i
< count
; i
++)
5843 /* Read in the version information. */
5846 _bfd_elf_slurp_version_tables (bfd
*abfd
)
5848 bfd_byte
*contents
= NULL
;
5851 if (elf_dynverdef (abfd
) != 0)
5853 Elf_Internal_Shdr
*hdr
;
5854 Elf_External_Verdef
*everdef
;
5855 Elf_Internal_Verdef
*iverdef
;
5856 Elf_Internal_Verdef
*iverdefarr
;
5857 Elf_Internal_Verdef iverdefmem
;
5859 unsigned int maxidx
;
5861 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5863 contents
= bfd_malloc (hdr
->sh_size
);
5864 if (contents
== NULL
)
5866 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5867 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5870 /* We know the number of entries in the section but not the maximum
5871 index. Therefore we have to run through all entries and find
5873 everdef
= (Elf_External_Verdef
*) contents
;
5875 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5877 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5879 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5880 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5882 everdef
= ((Elf_External_Verdef
*)
5883 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5886 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5887 elf_tdata (abfd
)->verdef
= bfd_zalloc (abfd
, amt
);
5888 if (elf_tdata (abfd
)->verdef
== NULL
)
5891 elf_tdata (abfd
)->cverdefs
= maxidx
;
5893 everdef
= (Elf_External_Verdef
*) contents
;
5894 iverdefarr
= elf_tdata (abfd
)->verdef
;
5895 for (i
= 0; i
< hdr
->sh_info
; i
++)
5897 Elf_External_Verdaux
*everdaux
;
5898 Elf_Internal_Verdaux
*iverdaux
;
5901 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5903 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5904 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5906 iverdef
->vd_bfd
= abfd
;
5908 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5909 iverdef
->vd_auxptr
= bfd_alloc (abfd
, amt
);
5910 if (iverdef
->vd_auxptr
== NULL
)
5913 everdaux
= ((Elf_External_Verdaux
*)
5914 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5915 iverdaux
= iverdef
->vd_auxptr
;
5916 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5918 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5920 iverdaux
->vda_nodename
=
5921 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5922 iverdaux
->vda_name
);
5923 if (iverdaux
->vda_nodename
== NULL
)
5926 if (j
+ 1 < iverdef
->vd_cnt
)
5927 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5929 iverdaux
->vda_nextptr
= NULL
;
5931 everdaux
= ((Elf_External_Verdaux
*)
5932 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5935 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5937 if (i
+ 1 < hdr
->sh_info
)
5938 iverdef
->vd_nextdef
= iverdef
+ 1;
5940 iverdef
->vd_nextdef
= NULL
;
5942 everdef
= ((Elf_External_Verdef
*)
5943 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5950 if (elf_dynverref (abfd
) != 0)
5952 Elf_Internal_Shdr
*hdr
;
5953 Elf_External_Verneed
*everneed
;
5954 Elf_Internal_Verneed
*iverneed
;
5957 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5959 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5960 elf_tdata (abfd
)->verref
= bfd_zalloc (abfd
, amt
);
5961 if (elf_tdata (abfd
)->verref
== NULL
)
5964 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5966 contents
= bfd_malloc (hdr
->sh_size
);
5967 if (contents
== NULL
)
5969 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5970 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5973 everneed
= (Elf_External_Verneed
*) contents
;
5974 iverneed
= elf_tdata (abfd
)->verref
;
5975 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5977 Elf_External_Vernaux
*evernaux
;
5978 Elf_Internal_Vernaux
*ivernaux
;
5981 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5983 iverneed
->vn_bfd
= abfd
;
5985 iverneed
->vn_filename
=
5986 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5988 if (iverneed
->vn_filename
== NULL
)
5991 amt
= iverneed
->vn_cnt
;
5992 amt
*= sizeof (Elf_Internal_Vernaux
);
5993 iverneed
->vn_auxptr
= bfd_alloc (abfd
, amt
);
5995 evernaux
= ((Elf_External_Vernaux
*)
5996 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5997 ivernaux
= iverneed
->vn_auxptr
;
5998 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
6000 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
6002 ivernaux
->vna_nodename
=
6003 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6004 ivernaux
->vna_name
);
6005 if (ivernaux
->vna_nodename
== NULL
)
6008 if (j
+ 1 < iverneed
->vn_cnt
)
6009 ivernaux
->vna_nextptr
= ivernaux
+ 1;
6011 ivernaux
->vna_nextptr
= NULL
;
6013 evernaux
= ((Elf_External_Vernaux
*)
6014 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
6017 if (i
+ 1 < hdr
->sh_info
)
6018 iverneed
->vn_nextref
= iverneed
+ 1;
6020 iverneed
->vn_nextref
= NULL
;
6022 everneed
= ((Elf_External_Verneed
*)
6023 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
6033 if (contents
!= NULL
)
6039 _bfd_elf_make_empty_symbol (bfd
*abfd
)
6041 elf_symbol_type
*newsym
;
6042 bfd_size_type amt
= sizeof (elf_symbol_type
);
6044 newsym
= bfd_zalloc (abfd
, amt
);
6049 newsym
->symbol
.the_bfd
= abfd
;
6050 return &newsym
->symbol
;
6055 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
6059 bfd_symbol_info (symbol
, ret
);
6062 /* Return whether a symbol name implies a local symbol. Most targets
6063 use this function for the is_local_label_name entry point, but some
6067 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
6070 /* Normal local symbols start with ``.L''. */
6071 if (name
[0] == '.' && name
[1] == 'L')
6074 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6075 DWARF debugging symbols starting with ``..''. */
6076 if (name
[0] == '.' && name
[1] == '.')
6079 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6080 emitting DWARF debugging output. I suspect this is actually a
6081 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6082 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6083 underscore to be emitted on some ELF targets). For ease of use,
6084 we treat such symbols as local. */
6085 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
6092 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
6093 asymbol
*symbol ATTRIBUTE_UNUSED
)
6100 _bfd_elf_set_arch_mach (bfd
*abfd
,
6101 enum bfd_architecture arch
,
6102 unsigned long machine
)
6104 /* If this isn't the right architecture for this backend, and this
6105 isn't the generic backend, fail. */
6106 if (arch
!= get_elf_backend_data (abfd
)->arch
6107 && arch
!= bfd_arch_unknown
6108 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
6111 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
6114 /* Find the function to a particular section and offset,
6115 for error reporting. */
6118 elf_find_function (bfd
*abfd ATTRIBUTE_UNUSED
,
6122 const char **filename_ptr
,
6123 const char **functionname_ptr
)
6125 const char *filename
;
6134 for (p
= symbols
; *p
!= NULL
; p
++)
6138 q
= (elf_symbol_type
*) *p
;
6140 if (bfd_get_section (&q
->symbol
) != section
)
6143 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
6148 filename
= bfd_asymbol_name (&q
->symbol
);
6152 if (q
->symbol
.section
== section
6153 && q
->symbol
.value
>= low_func
6154 && q
->symbol
.value
<= offset
)
6156 func
= (asymbol
*) q
;
6157 low_func
= q
->symbol
.value
;
6167 *filename_ptr
= filename
;
6168 if (functionname_ptr
)
6169 *functionname_ptr
= bfd_asymbol_name (func
);
6174 /* Find the nearest line to a particular section and offset,
6175 for error reporting. */
6178 _bfd_elf_find_nearest_line (bfd
*abfd
,
6182 const char **filename_ptr
,
6183 const char **functionname_ptr
,
6184 unsigned int *line_ptr
)
6188 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
6189 filename_ptr
, functionname_ptr
,
6192 if (!*functionname_ptr
)
6193 elf_find_function (abfd
, section
, symbols
, offset
,
6194 *filename_ptr
? NULL
: filename_ptr
,
6200 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
6201 filename_ptr
, functionname_ptr
,
6203 &elf_tdata (abfd
)->dwarf2_find_line_info
))
6205 if (!*functionname_ptr
)
6206 elf_find_function (abfd
, section
, symbols
, offset
,
6207 *filename_ptr
? NULL
: filename_ptr
,
6213 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
6214 &found
, filename_ptr
,
6215 functionname_ptr
, line_ptr
,
6216 &elf_tdata (abfd
)->line_info
))
6218 if (found
&& (*functionname_ptr
|| *line_ptr
))
6221 if (symbols
== NULL
)
6224 if (! elf_find_function (abfd
, section
, symbols
, offset
,
6225 filename_ptr
, functionname_ptr
))
6233 _bfd_elf_sizeof_headers (bfd
*abfd
, bfd_boolean reloc
)
6237 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
6239 ret
+= get_program_header_size (abfd
);
6244 _bfd_elf_set_section_contents (bfd
*abfd
,
6246 const void *location
,
6248 bfd_size_type count
)
6250 Elf_Internal_Shdr
*hdr
;
6253 if (! abfd
->output_has_begun
6254 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6257 hdr
= &elf_section_data (section
)->this_hdr
;
6258 pos
= hdr
->sh_offset
+ offset
;
6259 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6260 || bfd_bwrite (location
, count
, abfd
) != count
)
6267 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
6268 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
6269 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
6274 /* Try to convert a non-ELF reloc into an ELF one. */
6277 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
6279 /* Check whether we really have an ELF howto. */
6281 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
6283 bfd_reloc_code_real_type code
;
6284 reloc_howto_type
*howto
;
6286 /* Alien reloc: Try to determine its type to replace it with an
6287 equivalent ELF reloc. */
6289 if (areloc
->howto
->pc_relative
)
6291 switch (areloc
->howto
->bitsize
)
6294 code
= BFD_RELOC_8_PCREL
;
6297 code
= BFD_RELOC_12_PCREL
;
6300 code
= BFD_RELOC_16_PCREL
;
6303 code
= BFD_RELOC_24_PCREL
;
6306 code
= BFD_RELOC_32_PCREL
;
6309 code
= BFD_RELOC_64_PCREL
;
6315 howto
= bfd_reloc_type_lookup (abfd
, code
);
6317 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
6319 if (howto
->pcrel_offset
)
6320 areloc
->addend
+= areloc
->address
;
6322 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
6327 switch (areloc
->howto
->bitsize
)
6333 code
= BFD_RELOC_14
;
6336 code
= BFD_RELOC_16
;
6339 code
= BFD_RELOC_26
;
6342 code
= BFD_RELOC_32
;
6345 code
= BFD_RELOC_64
;
6351 howto
= bfd_reloc_type_lookup (abfd
, code
);
6355 areloc
->howto
= howto
;
6363 (*_bfd_error_handler
)
6364 (_("%s: unsupported relocation type %s"),
6365 bfd_archive_filename (abfd
), areloc
->howto
->name
);
6366 bfd_set_error (bfd_error_bad_value
);
6371 _bfd_elf_close_and_cleanup (bfd
*abfd
)
6373 if (bfd_get_format (abfd
) == bfd_object
)
6375 if (elf_shstrtab (abfd
) != NULL
)
6376 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
6379 return _bfd_generic_close_and_cleanup (abfd
);
6382 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6383 in the relocation's offset. Thus we cannot allow any sort of sanity
6384 range-checking to interfere. There is nothing else to do in processing
6387 bfd_reloc_status_type
6388 _bfd_elf_rel_vtable_reloc_fn
6389 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
6390 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
6391 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
6392 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
6394 return bfd_reloc_ok
;
6397 /* Elf core file support. Much of this only works on native
6398 toolchains, since we rely on knowing the
6399 machine-dependent procfs structure in order to pick
6400 out details about the corefile. */
6402 #ifdef HAVE_SYS_PROCFS_H
6403 # include <sys/procfs.h>
6406 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6409 elfcore_make_pid (bfd
*abfd
)
6411 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
6412 + (elf_tdata (abfd
)->core_pid
));
6415 /* If there isn't a section called NAME, make one, using
6416 data from SECT. Note, this function will generate a
6417 reference to NAME, so you shouldn't deallocate or
6421 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
6425 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
6428 sect2
= bfd_make_section (abfd
, name
);
6432 sect2
->_raw_size
= sect
->_raw_size
;
6433 sect2
->filepos
= sect
->filepos
;
6434 sect2
->flags
= sect
->flags
;
6435 sect2
->alignment_power
= sect
->alignment_power
;
6439 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6440 actually creates up to two pseudosections:
6441 - For the single-threaded case, a section named NAME, unless
6442 such a section already exists.
6443 - For the multi-threaded case, a section named "NAME/PID", where
6444 PID is elfcore_make_pid (abfd).
6445 Both pseudosections have identical contents. */
6447 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
6453 char *threaded_name
;
6457 /* Build the section name. */
6459 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6460 len
= strlen (buf
) + 1;
6461 threaded_name
= bfd_alloc (abfd
, len
);
6462 if (threaded_name
== NULL
)
6464 memcpy (threaded_name
, buf
, len
);
6466 sect
= bfd_make_section_anyway (abfd
, threaded_name
);
6469 sect
->_raw_size
= size
;
6470 sect
->filepos
= filepos
;
6471 sect
->flags
= SEC_HAS_CONTENTS
;
6472 sect
->alignment_power
= 2;
6474 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6477 /* prstatus_t exists on:
6479 linux 2.[01] + glibc
6483 #if defined (HAVE_PRSTATUS_T)
6486 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6491 if (note
->descsz
== sizeof (prstatus_t
))
6495 raw_size
= sizeof (prstat
.pr_reg
);
6496 offset
= offsetof (prstatus_t
, pr_reg
);
6497 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6499 /* Do not overwrite the core signal if it
6500 has already been set by another thread. */
6501 if (elf_tdata (abfd
)->core_signal
== 0)
6502 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6503 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6505 /* pr_who exists on:
6508 pr_who doesn't exist on:
6511 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6512 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6515 #if defined (HAVE_PRSTATUS32_T)
6516 else if (note
->descsz
== sizeof (prstatus32_t
))
6518 /* 64-bit host, 32-bit corefile */
6519 prstatus32_t prstat
;
6521 raw_size
= sizeof (prstat
.pr_reg
);
6522 offset
= offsetof (prstatus32_t
, pr_reg
);
6523 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6525 /* Do not overwrite the core signal if it
6526 has already been set by another thread. */
6527 if (elf_tdata (abfd
)->core_signal
== 0)
6528 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6529 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6531 /* pr_who exists on:
6534 pr_who doesn't exist on:
6537 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6538 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6541 #endif /* HAVE_PRSTATUS32_T */
6544 /* Fail - we don't know how to handle any other
6545 note size (ie. data object type). */
6549 /* Make a ".reg/999" section and a ".reg" section. */
6550 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6551 raw_size
, note
->descpos
+ offset
);
6553 #endif /* defined (HAVE_PRSTATUS_T) */
6555 /* Create a pseudosection containing the exact contents of NOTE. */
6557 elfcore_make_note_pseudosection (bfd
*abfd
,
6559 Elf_Internal_Note
*note
)
6561 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6562 note
->descsz
, note
->descpos
);
6565 /* There isn't a consistent prfpregset_t across platforms,
6566 but it doesn't matter, because we don't have to pick this
6567 data structure apart. */
6570 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
6572 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6575 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6576 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6580 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
6582 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6585 #if defined (HAVE_PRPSINFO_T)
6586 typedef prpsinfo_t elfcore_psinfo_t
;
6587 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6588 typedef prpsinfo32_t elfcore_psinfo32_t
;
6592 #if defined (HAVE_PSINFO_T)
6593 typedef psinfo_t elfcore_psinfo_t
;
6594 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6595 typedef psinfo32_t elfcore_psinfo32_t
;
6599 /* return a malloc'ed copy of a string at START which is at
6600 most MAX bytes long, possibly without a terminating '\0'.
6601 the copy will always have a terminating '\0'. */
6604 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
6607 char *end
= memchr (start
, '\0', max
);
6615 dups
= bfd_alloc (abfd
, len
+ 1);
6619 memcpy (dups
, start
, len
);
6625 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6627 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
6629 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6631 elfcore_psinfo_t psinfo
;
6633 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6635 elf_tdata (abfd
)->core_program
6636 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6637 sizeof (psinfo
.pr_fname
));
6639 elf_tdata (abfd
)->core_command
6640 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6641 sizeof (psinfo
.pr_psargs
));
6643 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6644 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6646 /* 64-bit host, 32-bit corefile */
6647 elfcore_psinfo32_t psinfo
;
6649 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6651 elf_tdata (abfd
)->core_program
6652 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6653 sizeof (psinfo
.pr_fname
));
6655 elf_tdata (abfd
)->core_command
6656 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6657 sizeof (psinfo
.pr_psargs
));
6663 /* Fail - we don't know how to handle any other
6664 note size (ie. data object type). */
6668 /* Note that for some reason, a spurious space is tacked
6669 onto the end of the args in some (at least one anyway)
6670 implementations, so strip it off if it exists. */
6673 char *command
= elf_tdata (abfd
)->core_command
;
6674 int n
= strlen (command
);
6676 if (0 < n
&& command
[n
- 1] == ' ')
6677 command
[n
- 1] = '\0';
6682 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6684 #if defined (HAVE_PSTATUS_T)
6686 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6688 if (note
->descsz
== sizeof (pstatus_t
)
6689 #if defined (HAVE_PXSTATUS_T)
6690 || note
->descsz
== sizeof (pxstatus_t
)
6696 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6698 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6700 #if defined (HAVE_PSTATUS32_T)
6701 else if (note
->descsz
== sizeof (pstatus32_t
))
6703 /* 64-bit host, 32-bit corefile */
6706 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6708 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6711 /* Could grab some more details from the "representative"
6712 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6713 NT_LWPSTATUS note, presumably. */
6717 #endif /* defined (HAVE_PSTATUS_T) */
6719 #if defined (HAVE_LWPSTATUS_T)
6721 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6723 lwpstatus_t lwpstat
;
6729 if (note
->descsz
!= sizeof (lwpstat
)
6730 #if defined (HAVE_LWPXSTATUS_T)
6731 && note
->descsz
!= sizeof (lwpxstatus_t
)
6736 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6738 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6739 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6741 /* Make a ".reg/999" section. */
6743 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6744 len
= strlen (buf
) + 1;
6745 name
= bfd_alloc (abfd
, len
);
6748 memcpy (name
, buf
, len
);
6750 sect
= bfd_make_section_anyway (abfd
, name
);
6754 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6755 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6756 sect
->filepos
= note
->descpos
6757 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6760 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6761 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6762 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6765 sect
->flags
= SEC_HAS_CONTENTS
;
6766 sect
->alignment_power
= 2;
6768 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6771 /* Make a ".reg2/999" section */
6773 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6774 len
= strlen (buf
) + 1;
6775 name
= bfd_alloc (abfd
, len
);
6778 memcpy (name
, buf
, len
);
6780 sect
= bfd_make_section_anyway (abfd
, name
);
6784 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6785 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6786 sect
->filepos
= note
->descpos
6787 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6790 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6791 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6792 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6795 sect
->flags
= SEC_HAS_CONTENTS
;
6796 sect
->alignment_power
= 2;
6798 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6800 #endif /* defined (HAVE_LWPSTATUS_T) */
6802 #if defined (HAVE_WIN32_PSTATUS_T)
6804 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6810 win32_pstatus_t pstatus
;
6812 if (note
->descsz
< sizeof (pstatus
))
6815 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
6817 switch (pstatus
.data_type
)
6819 case NOTE_INFO_PROCESS
:
6820 /* FIXME: need to add ->core_command. */
6821 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6822 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6825 case NOTE_INFO_THREAD
:
6826 /* Make a ".reg/999" section. */
6827 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6829 len
= strlen (buf
) + 1;
6830 name
= bfd_alloc (abfd
, len
);
6834 memcpy (name
, buf
, len
);
6836 sect
= bfd_make_section_anyway (abfd
, name
);
6840 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6841 sect
->filepos
= (note
->descpos
6842 + offsetof (struct win32_pstatus
,
6843 data
.thread_info
.thread_context
));
6844 sect
->flags
= SEC_HAS_CONTENTS
;
6845 sect
->alignment_power
= 2;
6847 if (pstatus
.data
.thread_info
.is_active_thread
)
6848 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6852 case NOTE_INFO_MODULE
:
6853 /* Make a ".module/xxxxxxxx" section. */
6854 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6856 len
= strlen (buf
) + 1;
6857 name
= bfd_alloc (abfd
, len
);
6861 memcpy (name
, buf
, len
);
6863 sect
= bfd_make_section_anyway (abfd
, name
);
6868 sect
->_raw_size
= note
->descsz
;
6869 sect
->filepos
= note
->descpos
;
6870 sect
->flags
= SEC_HAS_CONTENTS
;
6871 sect
->alignment_power
= 2;
6880 #endif /* HAVE_WIN32_PSTATUS_T */
6883 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
6885 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6893 if (bed
->elf_backend_grok_prstatus
)
6894 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6896 #if defined (HAVE_PRSTATUS_T)
6897 return elfcore_grok_prstatus (abfd
, note
);
6902 #if defined (HAVE_PSTATUS_T)
6904 return elfcore_grok_pstatus (abfd
, note
);
6907 #if defined (HAVE_LWPSTATUS_T)
6909 return elfcore_grok_lwpstatus (abfd
, note
);
6912 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6913 return elfcore_grok_prfpreg (abfd
, note
);
6915 #if defined (HAVE_WIN32_PSTATUS_T)
6916 case NT_WIN32PSTATUS
:
6917 return elfcore_grok_win32pstatus (abfd
, note
);
6920 case NT_PRXFPREG
: /* Linux SSE extension */
6921 if (note
->namesz
== 6
6922 && strcmp (note
->namedata
, "LINUX") == 0)
6923 return elfcore_grok_prxfpreg (abfd
, note
);
6929 if (bed
->elf_backend_grok_psinfo
)
6930 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6932 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6933 return elfcore_grok_psinfo (abfd
, note
);
6940 asection
*sect
= bfd_make_section_anyway (abfd
, ".auxv");
6944 sect
->_raw_size
= note
->descsz
;
6945 sect
->filepos
= note
->descpos
;
6946 sect
->flags
= SEC_HAS_CONTENTS
;
6947 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
6955 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
6959 cp
= strchr (note
->namedata
, '@');
6962 *lwpidp
= atoi(cp
+ 1);
6969 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
6972 /* Signal number at offset 0x08. */
6973 elf_tdata (abfd
)->core_signal
6974 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6976 /* Process ID at offset 0x50. */
6977 elf_tdata (abfd
)->core_pid
6978 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6980 /* Command name at 0x7c (max 32 bytes, including nul). */
6981 elf_tdata (abfd
)->core_command
6982 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6984 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
6989 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
6993 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6994 elf_tdata (abfd
)->core_lwpid
= lwp
;
6996 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6998 /* NetBSD-specific core "procinfo". Note that we expect to
6999 find this note before any of the others, which is fine,
7000 since the kernel writes this note out first when it
7001 creates a core file. */
7003 return elfcore_grok_netbsd_procinfo (abfd
, note
);
7006 /* As of Jan 2002 there are no other machine-independent notes
7007 defined for NetBSD core files. If the note type is less
7008 than the start of the machine-dependent note types, we don't
7011 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
7015 switch (bfd_get_arch (abfd
))
7017 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7018 PT_GETFPREGS == mach+2. */
7020 case bfd_arch_alpha
:
7021 case bfd_arch_sparc
:
7024 case NT_NETBSDCORE_FIRSTMACH
+0:
7025 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
7027 case NT_NETBSDCORE_FIRSTMACH
+2:
7028 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7034 /* On all other arch's, PT_GETREGS == mach+1 and
7035 PT_GETFPREGS == mach+3. */
7040 case NT_NETBSDCORE_FIRSTMACH
+1:
7041 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
7043 case NT_NETBSDCORE_FIRSTMACH
+3:
7044 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7054 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, pid_t
*tid
)
7056 void *ddata
= note
->descdata
;
7063 /* nto_procfs_status 'pid' field is at offset 0. */
7064 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
7066 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7067 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
7069 /* nto_procfs_status 'flags' field is at offset 8. */
7070 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
7072 /* nto_procfs_status 'what' field is at offset 14. */
7073 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
7075 elf_tdata (abfd
)->core_signal
= sig
;
7076 elf_tdata (abfd
)->core_lwpid
= *tid
;
7079 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7080 do not come from signals so we make sure we set the current
7081 thread just in case. */
7082 if (flags
& 0x00000080)
7083 elf_tdata (abfd
)->core_lwpid
= *tid
;
7085 /* Make a ".qnx_core_status/%d" section. */
7086 sprintf (buf
, ".qnx_core_status/%d", *tid
);
7088 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
7093 sect
= bfd_make_section_anyway (abfd
, name
);
7097 sect
->_raw_size
= note
->descsz
;
7098 sect
->filepos
= note
->descpos
;
7099 sect
->flags
= SEC_HAS_CONTENTS
;
7100 sect
->alignment_power
= 2;
7102 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
7106 elfcore_grok_nto_gregs (bfd
*abfd
, Elf_Internal_Note
*note
, pid_t tid
)
7112 /* Make a ".reg/%d" section. */
7113 sprintf (buf
, ".reg/%d", tid
);
7115 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
7120 sect
= bfd_make_section_anyway (abfd
, name
);
7124 sect
->_raw_size
= note
->descsz
;
7125 sect
->filepos
= note
->descpos
;
7126 sect
->flags
= SEC_HAS_CONTENTS
;
7127 sect
->alignment_power
= 2;
7129 /* This is the current thread. */
7130 if (elf_tdata (abfd
)->core_lwpid
== tid
)
7131 return elfcore_maybe_make_sect (abfd
, ".reg", sect
);
7136 #define BFD_QNT_CORE_INFO 7
7137 #define BFD_QNT_CORE_STATUS 8
7138 #define BFD_QNT_CORE_GREG 9
7139 #define BFD_QNT_CORE_FPREG 10
7142 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7144 /* Every GREG section has a STATUS section before it. Store the
7145 tid from the previous call to pass down to the next gregs
7147 static pid_t tid
= 1;
7151 case BFD_QNT_CORE_INFO
: return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
7152 case BFD_QNT_CORE_STATUS
: return elfcore_grok_nto_status (abfd
, note
, &tid
);
7153 case BFD_QNT_CORE_GREG
: return elfcore_grok_nto_gregs (abfd
, note
, tid
);
7154 case BFD_QNT_CORE_FPREG
: return elfcore_grok_prfpreg (abfd
, note
);
7155 default: return TRUE
;
7159 /* Function: elfcore_write_note
7166 size of data for note
7169 End of buffer containing note. */
7172 elfcore_write_note (bfd
*abfd
,
7180 Elf_External_Note
*xnp
;
7190 const struct elf_backend_data
*bed
;
7192 namesz
= strlen (name
) + 1;
7193 bed
= get_elf_backend_data (abfd
);
7194 pad
= -namesz
& ((1 << bed
->s
->log_file_align
) - 1);
7197 newspace
= 12 + namesz
+ pad
+ size
;
7199 p
= realloc (buf
, *bufsiz
+ newspace
);
7201 *bufsiz
+= newspace
;
7202 xnp
= (Elf_External_Note
*) dest
;
7203 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
7204 H_PUT_32 (abfd
, size
, xnp
->descsz
);
7205 H_PUT_32 (abfd
, type
, xnp
->type
);
7209 memcpy (dest
, name
, namesz
);
7217 memcpy (dest
, input
, size
);
7221 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7223 elfcore_write_prpsinfo (bfd
*abfd
,
7230 char *note_name
= "CORE";
7232 #if defined (HAVE_PSINFO_T)
7234 note_type
= NT_PSINFO
;
7237 note_type
= NT_PRPSINFO
;
7240 memset (&data
, 0, sizeof (data
));
7241 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
7242 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
7243 return elfcore_write_note (abfd
, buf
, bufsiz
,
7244 note_name
, note_type
, &data
, sizeof (data
));
7246 #endif /* PSINFO_T or PRPSINFO_T */
7248 #if defined (HAVE_PRSTATUS_T)
7250 elfcore_write_prstatus (bfd
*abfd
,
7258 char *note_name
= "CORE";
7260 memset (&prstat
, 0, sizeof (prstat
));
7261 prstat
.pr_pid
= pid
;
7262 prstat
.pr_cursig
= cursig
;
7263 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
7264 return elfcore_write_note (abfd
, buf
, bufsiz
,
7265 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
7267 #endif /* HAVE_PRSTATUS_T */
7269 #if defined (HAVE_LWPSTATUS_T)
7271 elfcore_write_lwpstatus (bfd
*abfd
,
7278 lwpstatus_t lwpstat
;
7279 char *note_name
= "CORE";
7281 memset (&lwpstat
, 0, sizeof (lwpstat
));
7282 lwpstat
.pr_lwpid
= pid
>> 16;
7283 lwpstat
.pr_cursig
= cursig
;
7284 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7285 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
7286 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7288 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
7289 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
7291 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
7292 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
7295 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7296 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
7298 #endif /* HAVE_LWPSTATUS_T */
7300 #if defined (HAVE_PSTATUS_T)
7302 elfcore_write_pstatus (bfd
*abfd
,
7310 char *note_name
= "CORE";
7312 memset (&pstat
, 0, sizeof (pstat
));
7313 pstat
.pr_pid
= pid
& 0xffff;
7314 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7315 NT_PSTATUS
, &pstat
, sizeof (pstat
));
7318 #endif /* HAVE_PSTATUS_T */
7321 elfcore_write_prfpreg (bfd
*abfd
,
7327 char *note_name
= "CORE";
7328 return elfcore_write_note (abfd
, buf
, bufsiz
,
7329 note_name
, NT_FPREGSET
, fpregs
, size
);
7333 elfcore_write_prxfpreg (bfd
*abfd
,
7336 const void *xfpregs
,
7339 char *note_name
= "LINUX";
7340 return elfcore_write_note (abfd
, buf
, bufsiz
,
7341 note_name
, NT_PRXFPREG
, xfpregs
, size
);
7345 elfcore_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
7353 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
7356 buf
= bfd_malloc (size
);
7360 if (bfd_bread (buf
, size
, abfd
) != size
)
7368 while (p
< buf
+ size
)
7370 /* FIXME: bad alignment assumption. */
7371 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
7372 Elf_Internal_Note in
;
7374 in
.type
= H_GET_32 (abfd
, xnp
->type
);
7376 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
7377 in
.namedata
= xnp
->name
;
7379 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
7380 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
7381 in
.descpos
= offset
+ (in
.descdata
- buf
);
7383 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
7385 if (! elfcore_grok_netbsd_note (abfd
, &in
))
7388 else if (strncmp (in
.namedata
, "QNX", 3) == 0)
7390 if (! elfcore_grok_nto_note (abfd
, &in
))
7395 if (! elfcore_grok_note (abfd
, &in
))
7399 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
7406 /* Providing external access to the ELF program header table. */
7408 /* Return an upper bound on the number of bytes required to store a
7409 copy of ABFD's program header table entries. Return -1 if an error
7410 occurs; bfd_get_error will return an appropriate code. */
7413 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
7415 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7417 bfd_set_error (bfd_error_wrong_format
);
7421 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
7424 /* Copy ABFD's program header table entries to *PHDRS. The entries
7425 will be stored as an array of Elf_Internal_Phdr structures, as
7426 defined in include/elf/internal.h. To find out how large the
7427 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7429 Return the number of program header table entries read, or -1 if an
7430 error occurs; bfd_get_error will return an appropriate code. */
7433 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
7437 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7439 bfd_set_error (bfd_error_wrong_format
);
7443 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
7444 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
7445 num_phdrs
* sizeof (Elf_Internal_Phdr
));
7451 _bfd_elf_sprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, char *buf
, bfd_vma value
)
7454 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7456 i_ehdrp
= elf_elfheader (abfd
);
7457 if (i_ehdrp
== NULL
)
7458 sprintf_vma (buf
, value
);
7461 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7463 #if BFD_HOST_64BIT_LONG
7464 sprintf (buf
, "%016lx", value
);
7466 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
7467 _bfd_int64_low (value
));
7471 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
7474 sprintf_vma (buf
, value
);
7479 _bfd_elf_fprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, void *stream
, bfd_vma value
)
7482 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7484 i_ehdrp
= elf_elfheader (abfd
);
7485 if (i_ehdrp
== NULL
)
7486 fprintf_vma ((FILE *) stream
, value
);
7489 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7491 #if BFD_HOST_64BIT_LONG
7492 fprintf ((FILE *) stream
, "%016lx", value
);
7494 fprintf ((FILE *) stream
, "%08lx%08lx",
7495 _bfd_int64_high (value
), _bfd_int64_low (value
));
7499 fprintf ((FILE *) stream
, "%08lx",
7500 (unsigned long) (value
& 0xffffffff));
7503 fprintf_vma ((FILE *) stream
, value
);
7507 enum elf_reloc_type_class
7508 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
7510 return reloc_class_normal
;
7513 /* For RELA architectures, return the relocation value for a
7514 relocation against a local symbol. */
7517 _bfd_elf_rela_local_sym (bfd
*abfd
,
7518 Elf_Internal_Sym
*sym
,
7520 Elf_Internal_Rela
*rel
)
7522 asection
*sec
= *psec
;
7525 relocation
= (sec
->output_section
->vma
7526 + sec
->output_offset
7528 if ((sec
->flags
& SEC_MERGE
)
7529 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
7530 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
7533 _bfd_merged_section_offset (abfd
, psec
,
7534 elf_section_data (sec
)->sec_info
,
7535 sym
->st_value
+ rel
->r_addend
,
7538 rel
->r_addend
-= relocation
;
7539 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
7545 _bfd_elf_rel_local_sym (bfd
*abfd
,
7546 Elf_Internal_Sym
*sym
,
7550 asection
*sec
= *psec
;
7552 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7553 return sym
->st_value
+ addend
;
7555 return _bfd_merged_section_offset (abfd
, psec
,
7556 elf_section_data (sec
)->sec_info
,
7557 sym
->st_value
+ addend
, 0);
7561 _bfd_elf_section_offset (bfd
*abfd
,
7562 struct bfd_link_info
*info
,
7566 struct bfd_elf_section_data
*sec_data
;
7568 sec_data
= elf_section_data (sec
);
7569 switch (sec
->sec_info_type
)
7571 case ELF_INFO_TYPE_STABS
:
7572 return _bfd_stab_section_offset (abfd
,
7573 &elf_hash_table (info
)->merge_info
,
7574 sec
, &sec_data
->sec_info
, offset
);
7575 case ELF_INFO_TYPE_EH_FRAME
:
7576 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);
7582 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7583 reconstruct an ELF file by reading the segments out of remote memory
7584 based on the ELF file header at EHDR_VMA and the ELF program headers it
7585 points to. If not null, *LOADBASEP is filled in with the difference
7586 between the VMAs from which the segments were read, and the VMAs the
7587 file headers (and hence BFD's idea of each section's VMA) put them at.
7589 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7590 remote memory at target address VMA into the local buffer at MYADDR; it
7591 should return zero on success or an `errno' code on failure. TEMPL must
7592 be a BFD for an ELF target with the word size and byte order found in
7593 the remote memory. */
7596 bfd_elf_bfd_from_remote_memory
7600 int (*target_read_memory
) (bfd_vma
, char *, int))
7602 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
7603 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
7607 _bfd_elf_get_synthetic_symtab (bfd
*abfd
, asymbol
**dynsyms
, asymbol
**ret
)
7609 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7612 const char *relplt_name
;
7613 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7617 Elf_Internal_Shdr
*hdr
;
7622 if (!bed
->plt_sym_val
)
7625 relplt_name
= bed
->relplt_name
;
7626 if (relplt_name
== NULL
)
7627 relplt_name
= bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt";
7628 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
7632 hdr
= &elf_section_data (relplt
)->this_hdr
;
7633 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
7634 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
7637 plt
= bfd_get_section_by_name (abfd
, ".plt");
7641 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7642 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
7645 count
= relplt
->_raw_size
/ hdr
->sh_entsize
;
7646 size
= count
* sizeof (asymbol
);
7647 p
= relplt
->relocation
;
7648 for (i
= 0; i
< count
; i
++, s
++, p
++)
7649 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
7651 s
= *ret
= bfd_malloc (size
);
7655 names
= (char *) (s
+ count
);
7656 p
= relplt
->relocation
;
7658 for (i
= 0; i
< count
; i
++, s
++, p
++)
7663 addr
= bed
->plt_sym_val (i
, plt
, p
);
7664 if (addr
== (bfd_vma
) -1)
7667 *s
= **p
->sym_ptr_ptr
;
7669 s
->value
= addr
- plt
->vma
;
7671 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
7672 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
7674 memcpy (names
, "@plt", sizeof ("@plt"));
7675 names
+= sizeof ("@plt");