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
)
907 if (!is_elf_hash_table (info
->hash
))
910 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
911 if ((ibfd
->flags
& DYNAMIC
) == 0)
912 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
913 if ((sec
->flags
& SEC_MERGE
) != 0
914 && !bfd_is_abs_section (sec
->output_section
))
916 struct bfd_elf_section_data
*secdata
;
918 secdata
= elf_section_data (sec
);
919 if (! _bfd_add_merge_section (abfd
,
920 &elf_hash_table (info
)->merge_info
,
921 sec
, &secdata
->sec_info
))
923 else if (secdata
->sec_info
)
924 sec
->sec_info_type
= ELF_INFO_TYPE_MERGE
;
927 if (elf_hash_table (info
)->merge_info
!= NULL
)
928 _bfd_merge_sections (abfd
, info
, elf_hash_table (info
)->merge_info
,
929 merge_sections_remove_hook
);
934 _bfd_elf_link_just_syms (asection
*sec
, struct bfd_link_info
*info
)
936 sec
->output_section
= bfd_abs_section_ptr
;
937 sec
->output_offset
= sec
->vma
;
938 if (!is_elf_hash_table (info
->hash
))
941 sec
->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
944 /* Copy the program header and other data from one object module to
948 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
950 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
951 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
954 BFD_ASSERT (!elf_flags_init (obfd
)
955 || (elf_elfheader (obfd
)->e_flags
956 == elf_elfheader (ibfd
)->e_flags
));
958 elf_gp (obfd
) = elf_gp (ibfd
);
959 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
960 elf_flags_init (obfd
) = TRUE
;
964 /* Print out the program headers. */
967 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
970 Elf_Internal_Phdr
*p
;
972 bfd_byte
*dynbuf
= NULL
;
974 p
= elf_tdata (abfd
)->phdr
;
979 fprintf (f
, _("\nProgram Header:\n"));
980 c
= elf_elfheader (abfd
)->e_phnum
;
981 for (i
= 0; i
< c
; i
++, p
++)
988 case PT_NULL
: pt
= "NULL"; break;
989 case PT_LOAD
: pt
= "LOAD"; break;
990 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
991 case PT_INTERP
: pt
= "INTERP"; break;
992 case PT_NOTE
: pt
= "NOTE"; break;
993 case PT_SHLIB
: pt
= "SHLIB"; break;
994 case PT_PHDR
: pt
= "PHDR"; break;
995 case PT_TLS
: pt
= "TLS"; break;
996 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
997 case PT_GNU_STACK
: pt
= "STACK"; break;
998 case PT_GNU_RELRO
: pt
= "RELRO"; break;
999 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
1001 fprintf (f
, "%8s off 0x", pt
);
1002 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1003 fprintf (f
, " vaddr 0x");
1004 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1005 fprintf (f
, " paddr 0x");
1006 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1007 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1008 fprintf (f
, " filesz 0x");
1009 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1010 fprintf (f
, " memsz 0x");
1011 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1012 fprintf (f
, " flags %c%c%c",
1013 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1014 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1015 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1016 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1017 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1022 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1026 unsigned long shlink
;
1027 bfd_byte
*extdyn
, *extdynend
;
1029 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1031 fprintf (f
, _("\nDynamic Section:\n"));
1033 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1036 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1039 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1041 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1042 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1045 extdynend
= extdyn
+ s
->size
;
1046 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1048 Elf_Internal_Dyn dyn
;
1051 bfd_boolean stringp
;
1053 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1055 if (dyn
.d_tag
== DT_NULL
)
1062 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1066 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1067 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1068 case DT_PLTGOT
: name
= "PLTGOT"; break;
1069 case DT_HASH
: name
= "HASH"; break;
1070 case DT_STRTAB
: name
= "STRTAB"; break;
1071 case DT_SYMTAB
: name
= "SYMTAB"; break;
1072 case DT_RELA
: name
= "RELA"; break;
1073 case DT_RELASZ
: name
= "RELASZ"; break;
1074 case DT_RELAENT
: name
= "RELAENT"; break;
1075 case DT_STRSZ
: name
= "STRSZ"; break;
1076 case DT_SYMENT
: name
= "SYMENT"; break;
1077 case DT_INIT
: name
= "INIT"; break;
1078 case DT_FINI
: name
= "FINI"; break;
1079 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1080 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1081 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1082 case DT_REL
: name
= "REL"; break;
1083 case DT_RELSZ
: name
= "RELSZ"; break;
1084 case DT_RELENT
: name
= "RELENT"; break;
1085 case DT_PLTREL
: name
= "PLTREL"; break;
1086 case DT_DEBUG
: name
= "DEBUG"; break;
1087 case DT_TEXTREL
: name
= "TEXTREL"; break;
1088 case DT_JMPREL
: name
= "JMPREL"; break;
1089 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1090 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1091 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1092 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1093 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1094 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1095 case DT_FLAGS
: name
= "FLAGS"; break;
1096 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1097 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1098 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1099 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1100 case DT_MOVEENT
: name
= "MOVEENT"; break;
1101 case DT_MOVESZ
: name
= "MOVESZ"; break;
1102 case DT_FEATURE
: name
= "FEATURE"; break;
1103 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1104 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1105 case DT_SYMINENT
: name
= "SYMINENT"; break;
1106 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1107 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1108 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1109 case DT_PLTPAD
: name
= "PLTPAD"; break;
1110 case DT_MOVETAB
: name
= "MOVETAB"; break;
1111 case DT_SYMINFO
: name
= "SYMINFO"; break;
1112 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1113 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1114 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1115 case DT_VERSYM
: name
= "VERSYM"; break;
1116 case DT_VERDEF
: name
= "VERDEF"; break;
1117 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1118 case DT_VERNEED
: name
= "VERNEED"; break;
1119 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1120 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1121 case DT_USED
: name
= "USED"; break;
1122 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1125 fprintf (f
, " %-11s ", name
);
1127 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1131 unsigned int tagv
= dyn
.d_un
.d_val
;
1133 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1136 fprintf (f
, "%s", string
);
1145 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1146 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1148 if (! _bfd_elf_slurp_version_tables (abfd
))
1152 if (elf_dynverdef (abfd
) != 0)
1154 Elf_Internal_Verdef
*t
;
1156 fprintf (f
, _("\nVersion definitions:\n"));
1157 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1159 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1160 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1161 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1163 Elf_Internal_Verdaux
*a
;
1166 for (a
= t
->vd_auxptr
->vda_nextptr
;
1169 fprintf (f
, "%s ", a
->vda_nodename
);
1175 if (elf_dynverref (abfd
) != 0)
1177 Elf_Internal_Verneed
*t
;
1179 fprintf (f
, _("\nVersion References:\n"));
1180 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1182 Elf_Internal_Vernaux
*a
;
1184 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1185 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1186 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1187 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1199 /* Display ELF-specific fields of a symbol. */
1202 bfd_elf_print_symbol (bfd
*abfd
,
1205 bfd_print_symbol_type how
)
1210 case bfd_print_symbol_name
:
1211 fprintf (file
, "%s", symbol
->name
);
1213 case bfd_print_symbol_more
:
1214 fprintf (file
, "elf ");
1215 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1216 fprintf (file
, " %lx", (long) symbol
->flags
);
1218 case bfd_print_symbol_all
:
1220 const char *section_name
;
1221 const char *name
= NULL
;
1222 const struct elf_backend_data
*bed
;
1223 unsigned char st_other
;
1226 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1228 bed
= get_elf_backend_data (abfd
);
1229 if (bed
->elf_backend_print_symbol_all
)
1230 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1234 name
= symbol
->name
;
1235 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1238 fprintf (file
, " %s\t", section_name
);
1239 /* Print the "other" value for a symbol. For common symbols,
1240 we've already printed the size; now print the alignment.
1241 For other symbols, we have no specified alignment, and
1242 we've printed the address; now print the size. */
1243 if (bfd_is_com_section (symbol
->section
))
1244 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1246 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1247 bfd_fprintf_vma (abfd
, file
, val
);
1249 /* If we have version information, print it. */
1250 if (elf_tdata (abfd
)->dynversym_section
!= 0
1251 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1252 || elf_tdata (abfd
)->dynverref_section
!= 0))
1254 unsigned int vernum
;
1255 const char *version_string
;
1257 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1260 version_string
= "";
1261 else if (vernum
== 1)
1262 version_string
= "Base";
1263 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1265 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1268 Elf_Internal_Verneed
*t
;
1270 version_string
= "";
1271 for (t
= elf_tdata (abfd
)->verref
;
1275 Elf_Internal_Vernaux
*a
;
1277 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1279 if (a
->vna_other
== vernum
)
1281 version_string
= a
->vna_nodename
;
1288 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1289 fprintf (file
, " %-11s", version_string
);
1294 fprintf (file
, " (%s)", version_string
);
1295 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1300 /* If the st_other field is not zero, print it. */
1301 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1306 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1307 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1308 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1310 /* Some other non-defined flags are also present, so print
1312 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1315 fprintf (file
, " %s", name
);
1321 /* Create an entry in an ELF linker hash table. */
1323 struct bfd_hash_entry
*
1324 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
1325 struct bfd_hash_table
*table
,
1328 /* Allocate the structure if it has not already been allocated by a
1332 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1337 /* Call the allocation method of the superclass. */
1338 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1341 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1342 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1344 /* Set local fields. */
1347 ret
->dynstr_index
= 0;
1348 ret
->elf_hash_value
= 0;
1349 ret
->weakdef
= NULL
;
1350 ret
->verinfo
.verdef
= NULL
;
1351 ret
->vtable_entries_size
= 0;
1352 ret
->vtable_entries_used
= NULL
;
1353 ret
->vtable_parent
= NULL
;
1354 ret
->got
= htab
->init_refcount
;
1355 ret
->plt
= htab
->init_refcount
;
1357 ret
->type
= STT_NOTYPE
;
1359 /* Assume that we have been called by a non-ELF symbol reader.
1360 This flag is then reset by the code which reads an ELF input
1361 file. This ensures that a symbol created by a non-ELF symbol
1362 reader will have the flag set correctly. */
1363 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1369 /* Copy data from an indirect symbol to its direct symbol, hiding the
1370 old indirect symbol. Also used for copying flags to a weakdef. */
1373 _bfd_elf_link_hash_copy_indirect (const struct elf_backend_data
*bed
,
1374 struct elf_link_hash_entry
*dir
,
1375 struct elf_link_hash_entry
*ind
)
1378 bfd_signed_vma lowest_valid
= bed
->can_refcount
;
1380 /* Copy down any references that we may have already seen to the
1381 symbol which just became indirect. */
1383 dir
->elf_link_hash_flags
1384 |= ind
->elf_link_hash_flags
& (ELF_LINK_HASH_REF_DYNAMIC
1385 | ELF_LINK_HASH_REF_REGULAR
1386 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1387 | ELF_LINK_NON_GOT_REF
1388 | ELF_LINK_HASH_NEEDS_PLT
1389 | ELF_LINK_POINTER_EQUALITY_NEEDED
);
1391 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1394 /* Copy over the global and procedure linkage table refcount entries.
1395 These may have been already set up by a check_relocs routine. */
1396 tmp
= dir
->got
.refcount
;
1397 if (tmp
< lowest_valid
)
1399 dir
->got
.refcount
= ind
->got
.refcount
;
1400 ind
->got
.refcount
= tmp
;
1403 BFD_ASSERT (ind
->got
.refcount
< lowest_valid
);
1405 tmp
= dir
->plt
.refcount
;
1406 if (tmp
< lowest_valid
)
1408 dir
->plt
.refcount
= ind
->plt
.refcount
;
1409 ind
->plt
.refcount
= tmp
;
1412 BFD_ASSERT (ind
->plt
.refcount
< lowest_valid
);
1414 if (dir
->dynindx
== -1)
1416 dir
->dynindx
= ind
->dynindx
;
1417 dir
->dynstr_index
= ind
->dynstr_index
;
1419 ind
->dynstr_index
= 0;
1422 BFD_ASSERT (ind
->dynindx
== -1);
1426 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
1427 struct elf_link_hash_entry
*h
,
1428 bfd_boolean force_local
)
1430 h
->plt
= elf_hash_table (info
)->init_offset
;
1431 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1434 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1435 if (h
->dynindx
!= -1)
1438 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
1444 /* Initialize an ELF linker hash table. */
1447 _bfd_elf_link_hash_table_init
1448 (struct elf_link_hash_table
*table
,
1450 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
1451 struct bfd_hash_table
*,
1456 table
->dynamic_sections_created
= FALSE
;
1457 table
->dynobj
= NULL
;
1458 /* Make sure can_refcount is extended to the width and signedness of
1459 init_refcount before we subtract one from it. */
1460 table
->init_refcount
.refcount
= get_elf_backend_data (abfd
)->can_refcount
;
1461 table
->init_refcount
.refcount
-= 1;
1462 table
->init_offset
.offset
= -(bfd_vma
) 1;
1463 /* The first dynamic symbol is a dummy. */
1464 table
->dynsymcount
= 1;
1465 table
->dynstr
= NULL
;
1466 table
->bucketcount
= 0;
1467 table
->needed
= NULL
;
1469 table
->merge_info
= NULL
;
1470 memset (&table
->stab_info
, 0, sizeof (table
->stab_info
));
1471 memset (&table
->eh_info
, 0, sizeof (table
->eh_info
));
1472 table
->dynlocal
= NULL
;
1473 table
->runpath
= NULL
;
1474 table
->tls_sec
= NULL
;
1475 table
->tls_size
= 0;
1476 table
->loaded
= NULL
;
1478 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
1479 table
->root
.type
= bfd_link_elf_hash_table
;
1484 /* Create an ELF linker hash table. */
1486 struct bfd_link_hash_table
*
1487 _bfd_elf_link_hash_table_create (bfd
*abfd
)
1489 struct elf_link_hash_table
*ret
;
1490 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1492 ret
= bfd_malloc (amt
);
1496 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1505 /* This is a hook for the ELF emulation code in the generic linker to
1506 tell the backend linker what file name to use for the DT_NEEDED
1507 entry for a dynamic object. */
1510 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
1512 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1513 && bfd_get_format (abfd
) == bfd_object
)
1514 elf_dt_name (abfd
) = name
;
1518 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
1521 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1522 && bfd_get_format (abfd
) == bfd_object
)
1523 lib_class
= elf_dyn_lib_class (abfd
);
1530 bfd_elf_set_dyn_lib_class (bfd
*abfd
, int lib_class
)
1532 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1533 && bfd_get_format (abfd
) == bfd_object
)
1534 elf_dyn_lib_class (abfd
) = lib_class
;
1537 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1538 the linker ELF emulation code. */
1540 struct bfd_link_needed_list
*
1541 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
1542 struct bfd_link_info
*info
)
1544 if (! is_elf_hash_table (info
->hash
))
1546 return elf_hash_table (info
)->needed
;
1549 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1550 hook for the linker ELF emulation code. */
1552 struct bfd_link_needed_list
*
1553 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
1554 struct bfd_link_info
*info
)
1556 if (! is_elf_hash_table (info
->hash
))
1558 return elf_hash_table (info
)->runpath
;
1561 /* Get the name actually used for a dynamic object for a link. This
1562 is the SONAME entry if there is one. Otherwise, it is the string
1563 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1566 bfd_elf_get_dt_soname (bfd
*abfd
)
1568 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1569 && bfd_get_format (abfd
) == bfd_object
)
1570 return elf_dt_name (abfd
);
1574 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1575 the ELF linker emulation code. */
1578 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
1579 struct bfd_link_needed_list
**pneeded
)
1582 bfd_byte
*dynbuf
= NULL
;
1584 unsigned long shlink
;
1585 bfd_byte
*extdyn
, *extdynend
;
1587 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1591 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1592 || bfd_get_format (abfd
) != bfd_object
)
1595 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1596 if (s
== NULL
|| s
->size
== 0)
1599 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1602 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1606 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1608 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1609 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1612 extdynend
= extdyn
+ s
->size
;
1613 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1615 Elf_Internal_Dyn dyn
;
1617 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1619 if (dyn
.d_tag
== DT_NULL
)
1622 if (dyn
.d_tag
== DT_NEEDED
)
1625 struct bfd_link_needed_list
*l
;
1626 unsigned int tagv
= dyn
.d_un
.d_val
;
1629 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1634 l
= bfd_alloc (abfd
, amt
);
1655 /* Allocate an ELF string table--force the first byte to be zero. */
1657 struct bfd_strtab_hash
*
1658 _bfd_elf_stringtab_init (void)
1660 struct bfd_strtab_hash
*ret
;
1662 ret
= _bfd_stringtab_init ();
1667 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1668 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1669 if (loc
== (bfd_size_type
) -1)
1671 _bfd_stringtab_free (ret
);
1678 /* ELF .o/exec file reading */
1680 /* Create a new bfd section from an ELF section header. */
1683 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1685 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1686 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1687 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1690 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1692 switch (hdr
->sh_type
)
1695 /* Inactive section. Throw it away. */
1698 case SHT_PROGBITS
: /* Normal section with contents. */
1699 case SHT_NOBITS
: /* .bss section. */
1700 case SHT_HASH
: /* .hash section. */
1701 case SHT_NOTE
: /* .note section. */
1702 case SHT_INIT_ARRAY
: /* .init_array section. */
1703 case SHT_FINI_ARRAY
: /* .fini_array section. */
1704 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1705 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1707 case SHT_DYNAMIC
: /* Dynamic linking information. */
1708 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1710 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1712 Elf_Internal_Shdr
*dynsymhdr
;
1714 /* The shared libraries distributed with hpux11 have a bogus
1715 sh_link field for the ".dynamic" section. Find the
1716 string table for the ".dynsym" section instead. */
1717 if (elf_dynsymtab (abfd
) != 0)
1719 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1720 hdr
->sh_link
= dynsymhdr
->sh_link
;
1724 unsigned int i
, num_sec
;
1726 num_sec
= elf_numsections (abfd
);
1727 for (i
= 1; i
< num_sec
; i
++)
1729 dynsymhdr
= elf_elfsections (abfd
)[i
];
1730 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1732 hdr
->sh_link
= dynsymhdr
->sh_link
;
1740 case SHT_SYMTAB
: /* A symbol table */
1741 if (elf_onesymtab (abfd
) == shindex
)
1744 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1745 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1746 elf_onesymtab (abfd
) = shindex
;
1747 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1748 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1749 abfd
->flags
|= HAS_SYMS
;
1751 /* Sometimes a shared object will map in the symbol table. If
1752 SHF_ALLOC is set, and this is a shared object, then we also
1753 treat this section as a BFD section. We can not base the
1754 decision purely on SHF_ALLOC, because that flag is sometimes
1755 set in a relocatable object file, which would confuse the
1757 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1758 && (abfd
->flags
& DYNAMIC
) != 0
1759 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1764 case SHT_DYNSYM
: /* A dynamic symbol table */
1765 if (elf_dynsymtab (abfd
) == shindex
)
1768 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1769 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1770 elf_dynsymtab (abfd
) = shindex
;
1771 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1772 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1773 abfd
->flags
|= HAS_SYMS
;
1775 /* Besides being a symbol table, we also treat this as a regular
1776 section, so that objcopy can handle it. */
1777 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1779 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1780 if (elf_symtab_shndx (abfd
) == shindex
)
1783 /* Get the associated symbol table. */
1784 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1785 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1788 elf_symtab_shndx (abfd
) = shindex
;
1789 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1790 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1793 case SHT_STRTAB
: /* A string table */
1794 if (hdr
->bfd_section
!= NULL
)
1796 if (ehdr
->e_shstrndx
== shindex
)
1798 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1799 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1803 unsigned int i
, num_sec
;
1805 num_sec
= elf_numsections (abfd
);
1806 for (i
= 1; i
< num_sec
; i
++)
1808 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1809 if (hdr2
->sh_link
== shindex
)
1811 if (! bfd_section_from_shdr (abfd
, i
))
1813 if (elf_onesymtab (abfd
) == i
)
1815 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1816 elf_elfsections (abfd
)[shindex
] =
1817 &elf_tdata (abfd
)->strtab_hdr
;
1820 if (elf_dynsymtab (abfd
) == i
)
1822 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1823 elf_elfsections (abfd
)[shindex
] = hdr
=
1824 &elf_tdata (abfd
)->dynstrtab_hdr
;
1825 /* We also treat this as a regular section, so
1826 that objcopy can handle it. */
1829 #if 0 /* Not handling other string tables specially right now. */
1830 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1831 /* We have a strtab for some random other section. */
1832 newsect
= (asection
*) hdr2
->bfd_section
;
1835 hdr
->bfd_section
= newsect
;
1836 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1838 elf_elfsections (abfd
)[shindex
] = hdr2
;
1844 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1848 /* *These* do a lot of work -- but build no sections! */
1850 asection
*target_sect
;
1851 Elf_Internal_Shdr
*hdr2
;
1852 unsigned int num_sec
= elf_numsections (abfd
);
1854 /* Check for a bogus link to avoid crashing. */
1855 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1856 || hdr
->sh_link
>= num_sec
)
1858 ((*_bfd_error_handler
)
1859 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1860 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1861 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1864 /* For some incomprehensible reason Oracle distributes
1865 libraries for Solaris in which some of the objects have
1866 bogus sh_link fields. It would be nice if we could just
1867 reject them, but, unfortunately, some people need to use
1868 them. We scan through the section headers; if we find only
1869 one suitable symbol table, we clobber the sh_link to point
1870 to it. I hope this doesn't break anything. */
1871 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1872 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1878 for (scan
= 1; scan
< num_sec
; scan
++)
1880 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1881 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1892 hdr
->sh_link
= found
;
1895 /* Get the symbol table. */
1896 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1897 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1900 /* If this reloc section does not use the main symbol table we
1901 don't treat it as a reloc section. BFD can't adequately
1902 represent such a section, so at least for now, we don't
1903 try. We just present it as a normal section. We also
1904 can't use it as a reloc section if it points to the null
1906 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1907 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1909 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1911 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1912 if (target_sect
== NULL
)
1915 if ((target_sect
->flags
& SEC_RELOC
) == 0
1916 || target_sect
->reloc_count
== 0)
1917 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1921 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1922 amt
= sizeof (*hdr2
);
1923 hdr2
= bfd_alloc (abfd
, amt
);
1924 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1927 elf_elfsections (abfd
)[shindex
] = hdr2
;
1928 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1929 target_sect
->flags
|= SEC_RELOC
;
1930 target_sect
->relocation
= NULL
;
1931 target_sect
->rel_filepos
= hdr
->sh_offset
;
1932 /* In the section to which the relocations apply, mark whether
1933 its relocations are of the REL or RELA variety. */
1934 if (hdr
->sh_size
!= 0)
1935 target_sect
->use_rela_p
= hdr
->sh_type
== SHT_RELA
;
1936 abfd
->flags
|= HAS_RELOC
;
1941 case SHT_GNU_verdef
:
1942 elf_dynverdef (abfd
) = shindex
;
1943 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1944 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1947 case SHT_GNU_versym
:
1948 elf_dynversym (abfd
) = shindex
;
1949 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1950 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1953 case SHT_GNU_verneed
:
1954 elf_dynverref (abfd
) = shindex
;
1955 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1956 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1963 /* We need a BFD section for objcopy and relocatable linking,
1964 and it's handy to have the signature available as the section
1966 name
= group_signature (abfd
, hdr
);
1969 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1971 if (hdr
->contents
!= NULL
)
1973 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1974 unsigned int n_elt
= hdr
->sh_size
/ 4;
1977 if (idx
->flags
& GRP_COMDAT
)
1978 hdr
->bfd_section
->flags
1979 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1981 /* We try to keep the same section order as it comes in. */
1983 while (--n_elt
!= 0)
1984 if ((s
= (--idx
)->shdr
->bfd_section
) != NULL
1985 && elf_next_in_group (s
) != NULL
)
1987 elf_next_in_group (hdr
->bfd_section
) = s
;
1994 /* Check for any processor-specific section types. */
1996 if (bed
->elf_backend_section_from_shdr
)
1997 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
2005 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2006 Return SEC for sections that have no elf section, and NULL on error. */
2009 bfd_section_from_r_symndx (bfd
*abfd
,
2010 struct sym_sec_cache
*cache
,
2012 unsigned long r_symndx
)
2014 Elf_Internal_Shdr
*symtab_hdr
;
2015 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2016 Elf_External_Sym_Shndx eshndx
;
2017 Elf_Internal_Sym isym
;
2018 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2020 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
2021 return cache
->sec
[ent
];
2023 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2024 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2025 &isym
, esym
, &eshndx
) == NULL
)
2028 if (cache
->abfd
!= abfd
)
2030 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2033 cache
->indx
[ent
] = r_symndx
;
2034 cache
->sec
[ent
] = sec
;
2035 if ((isym
.st_shndx
!= SHN_UNDEF
&& isym
.st_shndx
< SHN_LORESERVE
)
2036 || isym
.st_shndx
> SHN_HIRESERVE
)
2039 s
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
2041 cache
->sec
[ent
] = s
;
2043 return cache
->sec
[ent
];
2046 /* Given an ELF section number, retrieve the corresponding BFD
2050 bfd_section_from_elf_index (bfd
*abfd
, unsigned int index
)
2052 if (index
>= elf_numsections (abfd
))
2054 return elf_elfsections (abfd
)[index
]->bfd_section
;
2057 static struct bfd_elf_special_section
const special_sections
[] =
2059 { ".bss", 4, -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2060 { ".comment", 8, 0, SHT_PROGBITS
, 0 },
2061 { ".data", 5, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2062 { ".data1", 6, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2063 { ".debug", 6, 0, SHT_PROGBITS
, 0 },
2064 { ".fini", 5, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2065 { ".init", 5, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2066 { ".line", 5, 0, SHT_PROGBITS
, 0 },
2067 { ".rodata", 7, -2, SHT_PROGBITS
, SHF_ALLOC
},
2068 { ".rodata1", 8, 0, SHT_PROGBITS
, SHF_ALLOC
},
2069 { ".tbss", 5, -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2070 { ".tdata", 6, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2071 { ".text", 5, -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2072 { ".init_array", 11, 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2073 { ".fini_array", 11, 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2074 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2075 { ".debug_line", 11, 0, SHT_PROGBITS
, 0 },
2076 { ".debug_info", 11, 0, SHT_PROGBITS
, 0 },
2077 { ".debug_abbrev", 13, 0, SHT_PROGBITS
, 0 },
2078 { ".debug_aranges", 14, 0, SHT_PROGBITS
, 0 },
2079 { ".dynamic", 8, 0, SHT_DYNAMIC
, SHF_ALLOC
},
2080 { ".dynstr", 7, 0, SHT_STRTAB
, SHF_ALLOC
},
2081 { ".dynsym", 7, 0, SHT_DYNSYM
, SHF_ALLOC
},
2082 { ".got", 4, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2083 { ".hash", 5, 0, SHT_HASH
, SHF_ALLOC
},
2084 { ".interp", 7, 0, SHT_PROGBITS
, 0 },
2085 { ".plt", 4, 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2086 { ".shstrtab", 9, 0, SHT_STRTAB
, 0 },
2087 { ".strtab", 7, 0, SHT_STRTAB
, 0 },
2088 { ".symtab", 7, 0, SHT_SYMTAB
, 0 },
2089 { ".gnu.version", 12, 0, SHT_GNU_versym
, 0 },
2090 { ".gnu.version_d", 14, 0, SHT_GNU_verdef
, 0 },
2091 { ".gnu.version_r", 14, 0, SHT_GNU_verneed
, 0 },
2092 { ".note.GNU-stack",15, 0, SHT_PROGBITS
, 0 },
2093 { ".note", 5, -1, SHT_NOTE
, 0 },
2094 { ".rela", 5, -1, SHT_RELA
, 0 },
2095 { ".rel", 4, -1, SHT_REL
, 0 },
2096 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2097 { NULL
, 0, 0, 0, 0 }
2100 static const struct bfd_elf_special_section
*
2101 get_special_section (const char *name
,
2102 const struct bfd_elf_special_section
*special_sections
,
2106 int len
= strlen (name
);
2108 for (i
= 0; special_sections
[i
].prefix
!= NULL
; i
++)
2111 int prefix_len
= special_sections
[i
].prefix_length
;
2113 if (len
< prefix_len
)
2115 if (memcmp (name
, special_sections
[i
].prefix
, prefix_len
) != 0)
2118 suffix_len
= special_sections
[i
].suffix_length
;
2119 if (suffix_len
<= 0)
2121 if (name
[prefix_len
] != 0)
2123 if (suffix_len
== 0)
2125 if (name
[prefix_len
] != '.'
2126 && (suffix_len
== -2
2127 || (rela
&& special_sections
[i
].type
== SHT_REL
)))
2133 if (len
< prefix_len
+ suffix_len
)
2135 if (memcmp (name
+ len
- suffix_len
,
2136 special_sections
[i
].prefix
+ prefix_len
,
2140 return &special_sections
[i
];
2146 const struct bfd_elf_special_section
*
2147 _bfd_elf_get_sec_type_attr (bfd
*abfd
, const char *name
)
2149 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2150 const struct bfd_elf_special_section
*ssect
= NULL
;
2152 /* See if this is one of the special sections. */
2155 unsigned int rela
= bed
->default_use_rela_p
;
2157 if (bed
->special_sections
)
2158 ssect
= get_special_section (name
, bed
->special_sections
, rela
);
2161 ssect
= get_special_section (name
, special_sections
, rela
);
2168 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2170 struct bfd_elf_section_data
*sdata
;
2171 const struct bfd_elf_special_section
*ssect
;
2173 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2176 sdata
= bfd_zalloc (abfd
, sizeof (*sdata
));
2179 sec
->used_by_bfd
= sdata
;
2182 elf_section_type (sec
) = SHT_NULL
;
2183 ssect
= _bfd_elf_get_sec_type_attr (abfd
, sec
->name
);
2186 elf_section_type (sec
) = ssect
->type
;
2187 elf_section_flags (sec
) = ssect
->attr
;
2190 /* Indicate whether or not this section should use RELA relocations. */
2191 sec
->use_rela_p
= get_elf_backend_data (abfd
)->default_use_rela_p
;
2196 /* Create a new bfd section from an ELF program header.
2198 Since program segments have no names, we generate a synthetic name
2199 of the form segment<NUM>, where NUM is generally the index in the
2200 program header table. For segments that are split (see below) we
2201 generate the names segment<NUM>a and segment<NUM>b.
2203 Note that some program segments may have a file size that is different than
2204 (less than) the memory size. All this means is that at execution the
2205 system must allocate the amount of memory specified by the memory size,
2206 but only initialize it with the first "file size" bytes read from the
2207 file. This would occur for example, with program segments consisting
2208 of combined data+bss.
2210 To handle the above situation, this routine generates TWO bfd sections
2211 for the single program segment. The first has the length specified by
2212 the file size of the segment, and the second has the length specified
2213 by the difference between the two sizes. In effect, the segment is split
2214 into it's initialized and uninitialized parts.
2219 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2220 Elf_Internal_Phdr
*hdr
,
2222 const char *typename
)
2230 split
= ((hdr
->p_memsz
> 0)
2231 && (hdr
->p_filesz
> 0)
2232 && (hdr
->p_memsz
> hdr
->p_filesz
));
2233 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2234 len
= strlen (namebuf
) + 1;
2235 name
= bfd_alloc (abfd
, len
);
2238 memcpy (name
, namebuf
, len
);
2239 newsect
= bfd_make_section (abfd
, name
);
2240 if (newsect
== NULL
)
2242 newsect
->vma
= hdr
->p_vaddr
;
2243 newsect
->lma
= hdr
->p_paddr
;
2244 newsect
->size
= hdr
->p_filesz
;
2245 newsect
->filepos
= hdr
->p_offset
;
2246 newsect
->flags
|= SEC_HAS_CONTENTS
;
2247 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2248 if (hdr
->p_type
== PT_LOAD
)
2250 newsect
->flags
|= SEC_ALLOC
;
2251 newsect
->flags
|= SEC_LOAD
;
2252 if (hdr
->p_flags
& PF_X
)
2254 /* FIXME: all we known is that it has execute PERMISSION,
2256 newsect
->flags
|= SEC_CODE
;
2259 if (!(hdr
->p_flags
& PF_W
))
2261 newsect
->flags
|= SEC_READONLY
;
2266 sprintf (namebuf
, "%s%db", typename
, index
);
2267 len
= strlen (namebuf
) + 1;
2268 name
= bfd_alloc (abfd
, len
);
2271 memcpy (name
, namebuf
, len
);
2272 newsect
= bfd_make_section (abfd
, name
);
2273 if (newsect
== NULL
)
2275 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2276 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2277 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2278 if (hdr
->p_type
== PT_LOAD
)
2280 newsect
->flags
|= SEC_ALLOC
;
2281 if (hdr
->p_flags
& PF_X
)
2282 newsect
->flags
|= SEC_CODE
;
2284 if (!(hdr
->p_flags
& PF_W
))
2285 newsect
->flags
|= SEC_READONLY
;
2292 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int index
)
2294 const struct elf_backend_data
*bed
;
2296 switch (hdr
->p_type
)
2299 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2302 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2305 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2308 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2311 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2313 if (! elfcore_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2318 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2321 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2323 case PT_GNU_EH_FRAME
:
2324 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
,
2328 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "stack");
2331 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "relro");
2334 /* Check for any processor-specific program segment types.
2335 If no handler for them, default to making "segment" sections. */
2336 bed
= get_elf_backend_data (abfd
);
2337 if (bed
->elf_backend_section_from_phdr
)
2338 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2340 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2344 /* Initialize REL_HDR, the section-header for new section, containing
2345 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2346 relocations; otherwise, we use REL relocations. */
2349 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2350 Elf_Internal_Shdr
*rel_hdr
,
2352 bfd_boolean use_rela_p
)
2355 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2356 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2358 name
= bfd_alloc (abfd
, amt
);
2361 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2363 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2365 if (rel_hdr
->sh_name
== (unsigned int) -1)
2367 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2368 rel_hdr
->sh_entsize
= (use_rela_p
2369 ? bed
->s
->sizeof_rela
2370 : bed
->s
->sizeof_rel
);
2371 rel_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
2372 rel_hdr
->sh_flags
= 0;
2373 rel_hdr
->sh_addr
= 0;
2374 rel_hdr
->sh_size
= 0;
2375 rel_hdr
->sh_offset
= 0;
2380 /* Set up an ELF internal section header for a section. */
2383 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *failedptrarg
)
2385 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2386 bfd_boolean
*failedptr
= failedptrarg
;
2387 Elf_Internal_Shdr
*this_hdr
;
2391 /* We already failed; just get out of the bfd_map_over_sections
2396 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2398 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2399 asect
->name
, FALSE
);
2400 if (this_hdr
->sh_name
== (unsigned int) -1)
2406 this_hdr
->sh_flags
= 0;
2408 if ((asect
->flags
& SEC_ALLOC
) != 0
2409 || asect
->user_set_vma
)
2410 this_hdr
->sh_addr
= asect
->vma
;
2412 this_hdr
->sh_addr
= 0;
2414 this_hdr
->sh_offset
= 0;
2415 this_hdr
->sh_size
= asect
->size
;
2416 this_hdr
->sh_link
= 0;
2417 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2418 /* The sh_entsize and sh_info fields may have been set already by
2419 copy_private_section_data. */
2421 this_hdr
->bfd_section
= asect
;
2422 this_hdr
->contents
= NULL
;
2424 /* If the section type is unspecified, we set it based on
2426 if (this_hdr
->sh_type
== SHT_NULL
)
2428 if ((asect
->flags
& SEC_GROUP
) != 0)
2430 /* We also need to mark SHF_GROUP here for relocatable
2432 struct bfd_link_order
*l
;
2435 for (l
= asect
->link_order_head
; l
!= NULL
; l
= l
->next
)
2436 if (l
->type
== bfd_indirect_link_order
2437 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2440 /* The name is not important. Anything will do. */
2441 elf_group_name (elt
->output_section
) = "G";
2442 elf_section_flags (elt
->output_section
) |= SHF_GROUP
;
2444 elt
= elf_next_in_group (elt
);
2445 /* During a relocatable link, the lists are
2448 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2450 this_hdr
->sh_type
= SHT_GROUP
;
2452 else if ((asect
->flags
& SEC_ALLOC
) != 0
2453 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2454 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2455 this_hdr
->sh_type
= SHT_NOBITS
;
2457 this_hdr
->sh_type
= SHT_PROGBITS
;
2460 switch (this_hdr
->sh_type
)
2466 case SHT_INIT_ARRAY
:
2467 case SHT_FINI_ARRAY
:
2468 case SHT_PREINIT_ARRAY
:
2475 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2479 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2483 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2487 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2488 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2492 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2493 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2496 case SHT_GNU_versym
:
2497 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2500 case SHT_GNU_verdef
:
2501 this_hdr
->sh_entsize
= 0;
2502 /* objcopy or strip will copy over sh_info, but may not set
2503 cverdefs. The linker will set cverdefs, but sh_info will be
2505 if (this_hdr
->sh_info
== 0)
2506 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2508 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2509 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2512 case SHT_GNU_verneed
:
2513 this_hdr
->sh_entsize
= 0;
2514 /* objcopy or strip will copy over sh_info, but may not set
2515 cverrefs. The linker will set cverrefs, but sh_info will be
2517 if (this_hdr
->sh_info
== 0)
2518 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2520 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2521 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2525 this_hdr
->sh_entsize
= 4;
2529 if ((asect
->flags
& SEC_ALLOC
) != 0)
2530 this_hdr
->sh_flags
|= SHF_ALLOC
;
2531 if ((asect
->flags
& SEC_READONLY
) == 0)
2532 this_hdr
->sh_flags
|= SHF_WRITE
;
2533 if ((asect
->flags
& SEC_CODE
) != 0)
2534 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2535 if ((asect
->flags
& SEC_MERGE
) != 0)
2537 this_hdr
->sh_flags
|= SHF_MERGE
;
2538 this_hdr
->sh_entsize
= asect
->entsize
;
2539 if ((asect
->flags
& SEC_STRINGS
) != 0)
2540 this_hdr
->sh_flags
|= SHF_STRINGS
;
2542 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2543 this_hdr
->sh_flags
|= SHF_GROUP
;
2544 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2546 this_hdr
->sh_flags
|= SHF_TLS
;
2547 if (asect
->size
== 0 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2549 struct bfd_link_order
*o
;
2551 this_hdr
->sh_size
= 0;
2552 for (o
= asect
->link_order_head
; o
!= NULL
; o
= o
->next
)
2553 if (this_hdr
->sh_size
< o
->offset
+ o
->size
)
2554 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2555 if (this_hdr
->sh_size
)
2556 this_hdr
->sh_type
= SHT_NOBITS
;
2560 /* Check for processor-specific section types. */
2561 if (bed
->elf_backend_fake_sections
2562 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2565 /* If the section has relocs, set up a section header for the
2566 SHT_REL[A] section. If two relocation sections are required for
2567 this section, it is up to the processor-specific back-end to
2568 create the other. */
2569 if ((asect
->flags
& SEC_RELOC
) != 0
2570 && !_bfd_elf_init_reloc_shdr (abfd
,
2571 &elf_section_data (asect
)->rel_hdr
,
2577 /* Fill in the contents of a SHT_GROUP section. */
2580 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2582 bfd_boolean
*failedptr
= failedptrarg
;
2583 unsigned long symindx
;
2584 asection
*elt
, *first
;
2586 struct bfd_link_order
*l
;
2589 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2594 if (elf_group_id (sec
) != NULL
)
2595 symindx
= elf_group_id (sec
)->udata
.i
;
2599 /* If called from the assembler, swap_out_syms will have set up
2600 elf_section_syms; If called for "ld -r", use target_index. */
2601 if (elf_section_syms (abfd
) != NULL
)
2602 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2604 symindx
= sec
->target_index
;
2606 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2608 /* The contents won't be allocated for "ld -r" or objcopy. */
2610 if (sec
->contents
== NULL
)
2613 sec
->contents
= bfd_alloc (abfd
, sec
->size
);
2615 /* Arrange for the section to be written out. */
2616 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2617 if (sec
->contents
== NULL
)
2624 loc
= sec
->contents
+ sec
->size
;
2626 /* Get the pointer to the first section in the group that gas
2627 squirreled away here. objcopy arranges for this to be set to the
2628 start of the input section group. */
2629 first
= elt
= elf_next_in_group (sec
);
2631 /* First element is a flag word. Rest of section is elf section
2632 indices for all the sections of the group. Write them backwards
2633 just to keep the group in the same order as given in .section
2634 directives, not that it matters. */
2643 s
= s
->output_section
;
2646 idx
= elf_section_data (s
)->this_idx
;
2647 H_PUT_32 (abfd
, idx
, loc
);
2648 elt
= elf_next_in_group (elt
);
2653 /* If this is a relocatable link, then the above did nothing because
2654 SEC is the output section. Look through the input sections
2656 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2657 if (l
->type
== bfd_indirect_link_order
2658 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2663 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2664 elt
= elf_next_in_group (elt
);
2665 /* During a relocatable link, the lists are circular. */
2667 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2669 /* With ld -r, merging SHT_GROUP sections results in wasted space
2670 due to allowing for the flag word on each input. We may well
2671 duplicate entries too. */
2672 while ((loc
-= 4) > sec
->contents
)
2673 H_PUT_32 (abfd
, 0, loc
);
2675 if (loc
!= sec
->contents
)
2678 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2681 /* Assign all ELF section numbers. The dummy first section is handled here
2682 too. The link/info pointers for the standard section types are filled
2683 in here too, while we're at it. */
2686 assign_section_numbers (bfd
*abfd
)
2688 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2690 unsigned int section_number
, secn
;
2691 Elf_Internal_Shdr
**i_shdrp
;
2696 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2698 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2700 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2702 if (section_number
== SHN_LORESERVE
)
2703 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2704 d
->this_idx
= section_number
++;
2705 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2706 if ((sec
->flags
& SEC_RELOC
) == 0)
2710 if (section_number
== SHN_LORESERVE
)
2711 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2712 d
->rel_idx
= section_number
++;
2713 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2718 if (section_number
== SHN_LORESERVE
)
2719 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2720 d
->rel_idx2
= section_number
++;
2721 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2727 if (section_number
== SHN_LORESERVE
)
2728 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2729 t
->shstrtab_section
= section_number
++;
2730 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2731 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2733 if (bfd_get_symcount (abfd
) > 0)
2735 if (section_number
== SHN_LORESERVE
)
2736 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2737 t
->symtab_section
= section_number
++;
2738 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2739 if (section_number
> SHN_LORESERVE
- 2)
2741 if (section_number
== SHN_LORESERVE
)
2742 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2743 t
->symtab_shndx_section
= section_number
++;
2744 t
->symtab_shndx_hdr
.sh_name
2745 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2746 ".symtab_shndx", FALSE
);
2747 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2750 if (section_number
== SHN_LORESERVE
)
2751 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2752 t
->strtab_section
= section_number
++;
2753 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2756 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2757 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2759 elf_numsections (abfd
) = section_number
;
2760 elf_elfheader (abfd
)->e_shnum
= section_number
;
2761 if (section_number
> SHN_LORESERVE
)
2762 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2764 /* Set up the list of section header pointers, in agreement with the
2766 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2767 i_shdrp
= bfd_zalloc (abfd
, amt
);
2768 if (i_shdrp
== NULL
)
2771 amt
= sizeof (Elf_Internal_Shdr
);
2772 i_shdrp
[0] = bfd_zalloc (abfd
, amt
);
2773 if (i_shdrp
[0] == NULL
)
2775 bfd_release (abfd
, i_shdrp
);
2779 elf_elfsections (abfd
) = i_shdrp
;
2781 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2782 if (bfd_get_symcount (abfd
) > 0)
2784 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2785 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2787 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2788 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2790 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2791 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2793 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2795 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2799 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2800 if (d
->rel_idx
!= 0)
2801 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2802 if (d
->rel_idx2
!= 0)
2803 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2805 /* Fill in the sh_link and sh_info fields while we're at it. */
2807 /* sh_link of a reloc section is the section index of the symbol
2808 table. sh_info is the section index of the section to which
2809 the relocation entries apply. */
2810 if (d
->rel_idx
!= 0)
2812 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2813 d
->rel_hdr
.sh_info
= d
->this_idx
;
2815 if (d
->rel_idx2
!= 0)
2817 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2818 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2821 switch (d
->this_hdr
.sh_type
)
2825 /* A reloc section which we are treating as a normal BFD
2826 section. sh_link is the section index of the symbol
2827 table. sh_info is the section index of the section to
2828 which the relocation entries apply. We assume that an
2829 allocated reloc section uses the dynamic symbol table.
2830 FIXME: How can we be sure? */
2831 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2833 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2835 /* We look up the section the relocs apply to by name. */
2837 if (d
->this_hdr
.sh_type
== SHT_REL
)
2841 s
= bfd_get_section_by_name (abfd
, name
);
2843 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2847 /* We assume that a section named .stab*str is a stabs
2848 string section. We look for a section with the same name
2849 but without the trailing ``str'', and set its sh_link
2850 field to point to this section. */
2851 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2852 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2857 len
= strlen (sec
->name
);
2858 alc
= bfd_malloc (len
- 2);
2861 memcpy (alc
, sec
->name
, len
- 3);
2862 alc
[len
- 3] = '\0';
2863 s
= bfd_get_section_by_name (abfd
, alc
);
2867 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2869 /* This is a .stab section. */
2870 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
2871 elf_section_data (s
)->this_hdr
.sh_entsize
2872 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2879 case SHT_GNU_verneed
:
2880 case SHT_GNU_verdef
:
2881 /* sh_link is the section header index of the string table
2882 used for the dynamic entries, or the symbol table, or the
2884 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2886 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2890 case SHT_GNU_versym
:
2891 /* sh_link is the section header index of the symbol table
2892 this hash table or version table is for. */
2893 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2895 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2899 d
->this_hdr
.sh_link
= t
->symtab_section
;
2903 for (secn
= 1; secn
< section_number
; ++secn
)
2904 if (i_shdrp
[secn
] == NULL
)
2905 i_shdrp
[secn
] = i_shdrp
[0];
2907 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2908 i_shdrp
[secn
]->sh_name
);
2912 /* Map symbol from it's internal number to the external number, moving
2913 all local symbols to be at the head of the list. */
2916 sym_is_global (bfd
*abfd
, asymbol
*sym
)
2918 /* If the backend has a special mapping, use it. */
2919 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2920 if (bed
->elf_backend_sym_is_global
)
2921 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
2923 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2924 || bfd_is_und_section (bfd_get_section (sym
))
2925 || bfd_is_com_section (bfd_get_section (sym
)));
2929 elf_map_symbols (bfd
*abfd
)
2931 unsigned int symcount
= bfd_get_symcount (abfd
);
2932 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2933 asymbol
**sect_syms
;
2934 unsigned int num_locals
= 0;
2935 unsigned int num_globals
= 0;
2936 unsigned int num_locals2
= 0;
2937 unsigned int num_globals2
= 0;
2945 fprintf (stderr
, "elf_map_symbols\n");
2949 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2951 if (max_index
< asect
->index
)
2952 max_index
= asect
->index
;
2956 amt
= max_index
* sizeof (asymbol
*);
2957 sect_syms
= bfd_zalloc (abfd
, amt
);
2958 if (sect_syms
== NULL
)
2960 elf_section_syms (abfd
) = sect_syms
;
2961 elf_num_section_syms (abfd
) = max_index
;
2963 /* Init sect_syms entries for any section symbols we have already
2964 decided to output. */
2965 for (idx
= 0; idx
< symcount
; idx
++)
2967 asymbol
*sym
= syms
[idx
];
2969 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2976 if (sec
->owner
!= NULL
)
2978 if (sec
->owner
!= abfd
)
2980 if (sec
->output_offset
!= 0)
2983 sec
= sec
->output_section
;
2985 /* Empty sections in the input files may have had a
2986 section symbol created for them. (See the comment
2987 near the end of _bfd_generic_link_output_symbols in
2988 linker.c). If the linker script discards such
2989 sections then we will reach this point. Since we know
2990 that we cannot avoid this case, we detect it and skip
2991 the abort and the assignment to the sect_syms array.
2992 To reproduce this particular case try running the
2993 linker testsuite test ld-scripts/weak.exp for an ELF
2994 port that uses the generic linker. */
2995 if (sec
->owner
== NULL
)
2998 BFD_ASSERT (sec
->owner
== abfd
);
3000 sect_syms
[sec
->index
] = syms
[idx
];
3005 /* Classify all of the symbols. */
3006 for (idx
= 0; idx
< symcount
; idx
++)
3008 if (!sym_is_global (abfd
, syms
[idx
]))
3014 /* We will be adding a section symbol for each BFD section. Most normal
3015 sections will already have a section symbol in outsymbols, but
3016 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3017 at least in that case. */
3018 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3020 if (sect_syms
[asect
->index
] == NULL
)
3022 if (!sym_is_global (abfd
, asect
->symbol
))
3029 /* Now sort the symbols so the local symbols are first. */
3030 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
3031 new_syms
= bfd_alloc (abfd
, amt
);
3033 if (new_syms
== NULL
)
3036 for (idx
= 0; idx
< symcount
; idx
++)
3038 asymbol
*sym
= syms
[idx
];
3041 if (!sym_is_global (abfd
, sym
))
3044 i
= num_locals
+ num_globals2
++;
3046 sym
->udata
.i
= i
+ 1;
3048 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3050 if (sect_syms
[asect
->index
] == NULL
)
3052 asymbol
*sym
= asect
->symbol
;
3055 sect_syms
[asect
->index
] = sym
;
3056 if (!sym_is_global (abfd
, sym
))
3059 i
= num_locals
+ num_globals2
++;
3061 sym
->udata
.i
= i
+ 1;
3065 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3067 elf_num_locals (abfd
) = num_locals
;
3068 elf_num_globals (abfd
) = num_globals
;
3072 /* Align to the maximum file alignment that could be required for any
3073 ELF data structure. */
3075 static inline file_ptr
3076 align_file_position (file_ptr off
, int align
)
3078 return (off
+ align
- 1) & ~(align
- 1);
3081 /* Assign a file position to a section, optionally aligning to the
3082 required section alignment. */
3085 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3093 al
= i_shdrp
->sh_addralign
;
3095 offset
= BFD_ALIGN (offset
, al
);
3097 i_shdrp
->sh_offset
= offset
;
3098 if (i_shdrp
->bfd_section
!= NULL
)
3099 i_shdrp
->bfd_section
->filepos
= offset
;
3100 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3101 offset
+= i_shdrp
->sh_size
;
3105 /* Compute the file positions we are going to put the sections at, and
3106 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3107 is not NULL, this is being called by the ELF backend linker. */
3110 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3111 struct bfd_link_info
*link_info
)
3113 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3115 struct bfd_strtab_hash
*strtab
;
3116 Elf_Internal_Shdr
*shstrtab_hdr
;
3118 if (abfd
->output_has_begun
)
3121 /* Do any elf backend specific processing first. */
3122 if (bed
->elf_backend_begin_write_processing
)
3123 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3125 if (! prep_headers (abfd
))
3128 /* Post process the headers if necessary. */
3129 if (bed
->elf_backend_post_process_headers
)
3130 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3133 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3137 if (!assign_section_numbers (abfd
))
3140 /* The backend linker builds symbol table information itself. */
3141 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3143 /* Non-zero if doing a relocatable link. */
3144 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3146 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3150 if (link_info
== NULL
)
3152 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3157 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3158 /* sh_name was set in prep_headers. */
3159 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3160 shstrtab_hdr
->sh_flags
= 0;
3161 shstrtab_hdr
->sh_addr
= 0;
3162 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3163 shstrtab_hdr
->sh_entsize
= 0;
3164 shstrtab_hdr
->sh_link
= 0;
3165 shstrtab_hdr
->sh_info
= 0;
3166 /* sh_offset is set in assign_file_positions_except_relocs. */
3167 shstrtab_hdr
->sh_addralign
= 1;
3169 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3172 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3175 Elf_Internal_Shdr
*hdr
;
3177 off
= elf_tdata (abfd
)->next_file_pos
;
3179 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3180 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3182 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3183 if (hdr
->sh_size
!= 0)
3184 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3186 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3187 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3189 elf_tdata (abfd
)->next_file_pos
= off
;
3191 /* Now that we know where the .strtab section goes, write it
3193 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3194 || ! _bfd_stringtab_emit (abfd
, strtab
))
3196 _bfd_stringtab_free (strtab
);
3199 abfd
->output_has_begun
= TRUE
;
3204 /* Create a mapping from a set of sections to a program segment. */
3206 static struct elf_segment_map
*
3207 make_mapping (bfd
*abfd
,
3208 asection
**sections
,
3213 struct elf_segment_map
*m
;
3218 amt
= sizeof (struct elf_segment_map
);
3219 amt
+= (to
- from
- 1) * sizeof (asection
*);
3220 m
= bfd_zalloc (abfd
, amt
);
3224 m
->p_type
= PT_LOAD
;
3225 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3226 m
->sections
[i
- from
] = *hdrpp
;
3227 m
->count
= to
- from
;
3229 if (from
== 0 && phdr
)
3231 /* Include the headers in the first PT_LOAD segment. */
3232 m
->includes_filehdr
= 1;
3233 m
->includes_phdrs
= 1;
3239 /* Set up a mapping from BFD sections to program segments. */
3242 map_sections_to_segments (bfd
*abfd
)
3244 asection
**sections
= NULL
;
3248 struct elf_segment_map
*mfirst
;
3249 struct elf_segment_map
**pm
;
3250 struct elf_segment_map
*m
;
3253 unsigned int phdr_index
;
3254 bfd_vma maxpagesize
;
3256 bfd_boolean phdr_in_segment
= TRUE
;
3257 bfd_boolean writable
;
3259 asection
*first_tls
= NULL
;
3260 asection
*dynsec
, *eh_frame_hdr
;
3263 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3266 if (bfd_count_sections (abfd
) == 0)
3269 /* Select the allocated sections, and sort them. */
3271 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
3272 sections
= bfd_malloc (amt
);
3273 if (sections
== NULL
)
3277 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3279 if ((s
->flags
& SEC_ALLOC
) != 0)
3285 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3288 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3290 /* Build the mapping. */
3295 /* If we have a .interp section, then create a PT_PHDR segment for
3296 the program headers and a PT_INTERP segment for the .interp
3298 s
= bfd_get_section_by_name (abfd
, ".interp");
3299 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3301 amt
= sizeof (struct elf_segment_map
);
3302 m
= bfd_zalloc (abfd
, amt
);
3306 m
->p_type
= PT_PHDR
;
3307 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3308 m
->p_flags
= PF_R
| PF_X
;
3309 m
->p_flags_valid
= 1;
3310 m
->includes_phdrs
= 1;
3315 amt
= sizeof (struct elf_segment_map
);
3316 m
= bfd_zalloc (abfd
, amt
);
3320 m
->p_type
= PT_INTERP
;
3328 /* Look through the sections. We put sections in the same program
3329 segment when the start of the second section can be placed within
3330 a few bytes of the end of the first section. */
3334 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
3336 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3338 && (dynsec
->flags
& SEC_LOAD
) == 0)
3341 /* Deal with -Ttext or something similar such that the first section
3342 is not adjacent to the program headers. This is an
3343 approximation, since at this point we don't know exactly how many
3344 program headers we will need. */
3347 bfd_size_type phdr_size
;
3349 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3351 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3352 if ((abfd
->flags
& D_PAGED
) == 0
3353 || sections
[0]->lma
< phdr_size
3354 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3355 phdr_in_segment
= FALSE
;
3358 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3361 bfd_boolean new_segment
;
3365 /* See if this section and the last one will fit in the same
3368 if (last_hdr
== NULL
)
3370 /* If we don't have a segment yet, then we don't need a new
3371 one (we build the last one after this loop). */
3372 new_segment
= FALSE
;
3374 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3376 /* If this section has a different relation between the
3377 virtual address and the load address, then we need a new
3381 else if (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
3382 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3384 /* If putting this section in this segment would force us to
3385 skip a page in the segment, then we need a new segment. */
3388 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3389 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3391 /* We don't want to put a loadable section after a
3392 nonloadable section in the same segment.
3393 Consider .tbss sections as loadable for this purpose. */
3396 else if ((abfd
->flags
& D_PAGED
) == 0)
3398 /* If the file is not demand paged, which means that we
3399 don't require the sections to be correctly aligned in the
3400 file, then there is no other reason for a new segment. */
3401 new_segment
= FALSE
;
3404 && (hdr
->flags
& SEC_READONLY
) == 0
3405 && (((last_hdr
->lma
+ last_size
- 1)
3406 & ~(maxpagesize
- 1))
3407 != (hdr
->lma
& ~(maxpagesize
- 1))))
3409 /* We don't want to put a writable section in a read only
3410 segment, unless they are on the same page in memory
3411 anyhow. We already know that the last section does not
3412 bring us past the current section on the page, so the
3413 only case in which the new section is not on the same
3414 page as the previous section is when the previous section
3415 ends precisely on a page boundary. */
3420 /* Otherwise, we can use the same segment. */
3421 new_segment
= FALSE
;
3426 if ((hdr
->flags
& SEC_READONLY
) == 0)
3429 /* .tbss sections effectively have zero size. */
3430 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3431 last_size
= hdr
->size
;
3437 /* We need a new program segment. We must create a new program
3438 header holding all the sections from phdr_index until hdr. */
3440 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3447 if ((hdr
->flags
& SEC_READONLY
) == 0)
3453 /* .tbss sections effectively have zero size. */
3454 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3455 last_size
= hdr
->size
;
3459 phdr_in_segment
= FALSE
;
3462 /* Create a final PT_LOAD program segment. */
3463 if (last_hdr
!= NULL
)
3465 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3473 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3476 amt
= sizeof (struct elf_segment_map
);
3477 m
= bfd_zalloc (abfd
, amt
);
3481 m
->p_type
= PT_DYNAMIC
;
3483 m
->sections
[0] = dynsec
;
3489 /* For each loadable .note section, add a PT_NOTE segment. We don't
3490 use bfd_get_section_by_name, because if we link together
3491 nonloadable .note sections and loadable .note sections, we will
3492 generate two .note sections in the output file. FIXME: Using
3493 names for section types is bogus anyhow. */
3494 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3496 if ((s
->flags
& SEC_LOAD
) != 0
3497 && strncmp (s
->name
, ".note", 5) == 0)
3499 amt
= sizeof (struct elf_segment_map
);
3500 m
= bfd_zalloc (abfd
, amt
);
3504 m
->p_type
= PT_NOTE
;
3511 if (s
->flags
& SEC_THREAD_LOCAL
)
3519 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3524 amt
= sizeof (struct elf_segment_map
);
3525 amt
+= (tls_count
- 1) * sizeof (asection
*);
3526 m
= bfd_zalloc (abfd
, amt
);
3531 m
->count
= tls_count
;
3532 /* Mandated PF_R. */
3534 m
->p_flags_valid
= 1;
3535 for (i
= 0; i
< tls_count
; ++i
)
3537 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3538 m
->sections
[i
] = first_tls
;
3539 first_tls
= first_tls
->next
;
3546 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3548 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
3549 if (eh_frame_hdr
!= NULL
3550 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
3552 amt
= sizeof (struct elf_segment_map
);
3553 m
= bfd_zalloc (abfd
, amt
);
3557 m
->p_type
= PT_GNU_EH_FRAME
;
3559 m
->sections
[0] = eh_frame_hdr
->output_section
;
3565 if (elf_tdata (abfd
)->stack_flags
)
3567 amt
= sizeof (struct elf_segment_map
);
3568 m
= bfd_zalloc (abfd
, amt
);
3572 m
->p_type
= PT_GNU_STACK
;
3573 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
3574 m
->p_flags_valid
= 1;
3580 if (elf_tdata (abfd
)->relro
)
3582 amt
= sizeof (struct elf_segment_map
);
3583 m
= bfd_zalloc (abfd
, amt
);
3587 m
->p_type
= PT_GNU_RELRO
;
3589 m
->p_flags_valid
= 1;
3598 elf_tdata (abfd
)->segment_map
= mfirst
;
3602 if (sections
!= NULL
)
3607 /* Sort sections by address. */
3610 elf_sort_sections (const void *arg1
, const void *arg2
)
3612 const asection
*sec1
= *(const asection
**) arg1
;
3613 const asection
*sec2
= *(const asection
**) arg2
;
3614 bfd_size_type size1
, size2
;
3616 /* Sort by LMA first, since this is the address used to
3617 place the section into a segment. */
3618 if (sec1
->lma
< sec2
->lma
)
3620 else if (sec1
->lma
> sec2
->lma
)
3623 /* Then sort by VMA. Normally the LMA and the VMA will be
3624 the same, and this will do nothing. */
3625 if (sec1
->vma
< sec2
->vma
)
3627 else if (sec1
->vma
> sec2
->vma
)
3630 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3632 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3638 /* If the indicies are the same, do not return 0
3639 here, but continue to try the next comparison. */
3640 if (sec1
->target_index
- sec2
->target_index
!= 0)
3641 return sec1
->target_index
- sec2
->target_index
;
3646 else if (TOEND (sec2
))
3651 /* Sort by size, to put zero sized sections
3652 before others at the same address. */
3654 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
3655 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
3662 return sec1
->target_index
- sec2
->target_index
;
3665 /* Ian Lance Taylor writes:
3667 We shouldn't be using % with a negative signed number. That's just
3668 not good. We have to make sure either that the number is not
3669 negative, or that the number has an unsigned type. When the types
3670 are all the same size they wind up as unsigned. When file_ptr is a
3671 larger signed type, the arithmetic winds up as signed long long,
3674 What we're trying to say here is something like ``increase OFF by
3675 the least amount that will cause it to be equal to the VMA modulo
3677 /* In other words, something like:
3679 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3680 off_offset = off % bed->maxpagesize;
3681 if (vma_offset < off_offset)
3682 adjustment = vma_offset + bed->maxpagesize - off_offset;
3684 adjustment = vma_offset - off_offset;
3686 which can can be collapsed into the expression below. */
3689 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
3691 return ((vma
- off
) % maxpagesize
);
3694 /* Assign file positions to the sections based on the mapping from
3695 sections to segments. This function also sets up some fields in
3696 the file header, and writes out the program headers. */
3699 assign_file_positions_for_segments (bfd
*abfd
, struct bfd_link_info
*link_info
)
3701 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3703 struct elf_segment_map
*m
;
3705 Elf_Internal_Phdr
*phdrs
;
3707 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3708 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3709 Elf_Internal_Phdr
*p
;
3712 if (elf_tdata (abfd
)->segment_map
== NULL
)
3714 if (! map_sections_to_segments (abfd
))
3719 /* The placement algorithm assumes that non allocated sections are
3720 not in PT_LOAD segments. We ensure this here by removing such
3721 sections from the segment map. */
3722 for (m
= elf_tdata (abfd
)->segment_map
;
3726 unsigned int new_count
;
3729 if (m
->p_type
!= PT_LOAD
)
3733 for (i
= 0; i
< m
->count
; i
++)
3735 if ((m
->sections
[i
]->flags
& SEC_ALLOC
) != 0)
3738 m
->sections
[new_count
] = m
->sections
[i
];
3744 if (new_count
!= m
->count
)
3745 m
->count
= new_count
;
3749 if (bed
->elf_backend_modify_segment_map
)
3751 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
, link_info
))
3756 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3759 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3760 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3761 elf_elfheader (abfd
)->e_phnum
= count
;
3766 /* If we already counted the number of program segments, make sure
3767 that we allocated enough space. This happens when SIZEOF_HEADERS
3768 is used in a linker script. */
3769 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3770 if (alloc
!= 0 && count
> alloc
)
3772 ((*_bfd_error_handler
)
3773 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3774 bfd_get_filename (abfd
), alloc
, count
));
3775 bfd_set_error (bfd_error_bad_value
);
3782 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3783 phdrs
= bfd_alloc (abfd
, amt
);
3787 off
= bed
->s
->sizeof_ehdr
;
3788 off
+= alloc
* bed
->s
->sizeof_phdr
;
3795 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3802 /* If elf_segment_map is not from map_sections_to_segments, the
3803 sections may not be correctly ordered. NOTE: sorting should
3804 not be done to the PT_NOTE section of a corefile, which may
3805 contain several pseudo-sections artificially created by bfd.
3806 Sorting these pseudo-sections breaks things badly. */
3808 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3809 && m
->p_type
== PT_NOTE
))
3810 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3813 p
->p_type
= m
->p_type
;
3814 p
->p_flags
= m
->p_flags
;
3816 if (p
->p_type
== PT_LOAD
3818 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3820 if ((abfd
->flags
& D_PAGED
) != 0)
3821 off
+= vma_page_aligned_bias (m
->sections
[0]->vma
, off
,
3825 bfd_size_type align
;
3828 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3830 bfd_size_type secalign
;
3832 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3833 if (secalign
> align
)
3837 off
+= vma_page_aligned_bias (m
->sections
[0]->vma
, off
,
3841 /* Make sure the .dynamic section is the first section in the
3842 PT_DYNAMIC segment. */
3843 else if (p
->p_type
== PT_DYNAMIC
3845 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
3848 (_("%s: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
3849 bfd_get_filename (abfd
));
3850 bfd_set_error (bfd_error_bad_value
);
3857 p
->p_vaddr
= m
->sections
[0]->vma
;
3859 if (m
->p_paddr_valid
)
3860 p
->p_paddr
= m
->p_paddr
;
3861 else if (m
->count
== 0)
3864 p
->p_paddr
= m
->sections
[0]->lma
;
3866 if (p
->p_type
== PT_LOAD
3867 && (abfd
->flags
& D_PAGED
) != 0)
3868 p
->p_align
= bed
->maxpagesize
;
3869 else if (m
->count
== 0)
3870 p
->p_align
= 1 << bed
->s
->log_file_align
;
3878 if (m
->includes_filehdr
)
3880 if (! m
->p_flags_valid
)
3883 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3884 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3887 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3889 if (p
->p_vaddr
< (bfd_vma
) off
)
3891 (*_bfd_error_handler
)
3892 (_("%s: Not enough room for program headers, try linking with -N"),
3893 bfd_get_filename (abfd
));
3894 bfd_set_error (bfd_error_bad_value
);
3899 if (! m
->p_paddr_valid
)
3902 if (p
->p_type
== PT_LOAD
)
3904 filehdr_vaddr
= p
->p_vaddr
;
3905 filehdr_paddr
= p
->p_paddr
;
3909 if (m
->includes_phdrs
)
3911 if (! m
->p_flags_valid
)
3914 if (m
->includes_filehdr
)
3916 if (p
->p_type
== PT_LOAD
)
3918 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3919 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3924 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3928 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3929 p
->p_vaddr
-= off
- p
->p_offset
;
3930 if (! m
->p_paddr_valid
)
3931 p
->p_paddr
-= off
- p
->p_offset
;
3934 if (p
->p_type
== PT_LOAD
)
3936 phdrs_vaddr
= p
->p_vaddr
;
3937 phdrs_paddr
= p
->p_paddr
;
3940 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3943 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3944 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3947 if (p
->p_type
== PT_LOAD
3948 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3950 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3956 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3957 p
->p_filesz
+= adjust
;
3958 p
->p_memsz
+= adjust
;
3964 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3968 bfd_size_type align
;
3972 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3974 /* The section may have artificial alignment forced by a
3975 link script. Notice this case by the gap between the
3976 cumulative phdr lma and the section's lma. */
3977 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3979 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3981 p
->p_memsz
+= adjust
;
3982 if (p
->p_type
== PT_LOAD
3983 || (p
->p_type
== PT_NOTE
3984 && bfd_get_format (abfd
) == bfd_core
))
3989 if ((flags
& SEC_LOAD
) != 0
3990 || (flags
& SEC_THREAD_LOCAL
) != 0)
3991 p
->p_filesz
+= adjust
;
3994 if (p
->p_type
== PT_LOAD
)
3996 bfd_signed_vma adjust
;
3998 if ((flags
& SEC_LOAD
) != 0)
4000 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
4004 else if ((flags
& SEC_ALLOC
) != 0)
4006 /* The section VMA must equal the file position
4007 modulo the page size. FIXME: I'm not sure if
4008 this adjustment is really necessary. We used to
4009 not have the SEC_LOAD case just above, and then
4010 this was necessary, but now I'm not sure. */
4011 if ((abfd
->flags
& D_PAGED
) != 0)
4012 adjust
= vma_page_aligned_bias (sec
->vma
, voff
,
4015 adjust
= vma_page_aligned_bias (sec
->vma
, voff
,
4025 (* _bfd_error_handler
) (_("\
4026 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
4027 bfd_section_name (abfd
, sec
),
4032 p
->p_memsz
+= adjust
;
4035 if ((flags
& SEC_LOAD
) != 0)
4036 p
->p_filesz
+= adjust
;
4041 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
4042 used in a linker script we may have a section with
4043 SEC_LOAD clear but which is supposed to have
4045 if ((flags
& SEC_LOAD
) != 0
4046 || (flags
& SEC_HAS_CONTENTS
) != 0)
4049 if ((flags
& SEC_ALLOC
) != 0
4050 && ((flags
& SEC_LOAD
) != 0
4051 || (flags
& SEC_THREAD_LOCAL
) == 0))
4055 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4057 /* The actual "note" segment has i == 0.
4058 This is the one that actually contains everything. */
4062 p
->p_filesz
= sec
->size
;
4068 /* Fake sections -- don't need to be written. */
4071 flags
= sec
->flags
= 0;
4078 if ((sec
->flags
& SEC_LOAD
) != 0
4079 || (sec
->flags
& SEC_THREAD_LOCAL
) == 0
4080 || p
->p_type
== PT_TLS
)
4081 p
->p_memsz
+= sec
->size
;
4083 if ((flags
& SEC_LOAD
) != 0)
4084 p
->p_filesz
+= sec
->size
;
4086 if (p
->p_type
== PT_TLS
4088 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
4090 struct bfd_link_order
*o
;
4091 bfd_vma tbss_size
= 0;
4093 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
4094 if (tbss_size
< o
->offset
+ o
->size
)
4095 tbss_size
= o
->offset
+ o
->size
;
4097 p
->p_memsz
+= tbss_size
;
4100 if (align
> p
->p_align
4101 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
4105 if (! m
->p_flags_valid
)
4108 if ((flags
& SEC_CODE
) != 0)
4110 if ((flags
& SEC_READONLY
) == 0)
4116 /* Now that we have set the section file positions, we can set up
4117 the file positions for the non PT_LOAD segments. */
4118 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4122 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
4124 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
4125 p
->p_offset
= m
->sections
[0]->filepos
;
4129 if (m
->includes_filehdr
)
4131 p
->p_vaddr
= filehdr_vaddr
;
4132 if (! m
->p_paddr_valid
)
4133 p
->p_paddr
= filehdr_paddr
;
4135 else if (m
->includes_phdrs
)
4137 p
->p_vaddr
= phdrs_vaddr
;
4138 if (! m
->p_paddr_valid
)
4139 p
->p_paddr
= phdrs_paddr
;
4141 else if (p
->p_type
== PT_GNU_RELRO
)
4143 Elf_Internal_Phdr
*lp
;
4145 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4147 if (lp
->p_type
== PT_LOAD
4148 && lp
->p_vaddr
<= link_info
->relro_end
4149 && lp
->p_vaddr
>= link_info
->relro_start
4150 && lp
->p_vaddr
+ lp
->p_filesz
4151 >= link_info
->relro_end
)
4155 if (lp
< phdrs
+ count
4156 && link_info
->relro_end
> lp
->p_vaddr
)
4158 p
->p_vaddr
= lp
->p_vaddr
;
4159 p
->p_paddr
= lp
->p_paddr
;
4160 p
->p_offset
= lp
->p_offset
;
4161 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
4162 p
->p_memsz
= p
->p_filesz
;
4164 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
4168 memset (p
, 0, sizeof *p
);
4169 p
->p_type
= PT_NULL
;
4175 /* Clear out any program headers we allocated but did not use. */
4176 for (; count
< alloc
; count
++, p
++)
4178 memset (p
, 0, sizeof *p
);
4179 p
->p_type
= PT_NULL
;
4182 elf_tdata (abfd
)->phdr
= phdrs
;
4184 elf_tdata (abfd
)->next_file_pos
= off
;
4186 /* Write out the program headers. */
4187 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4188 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
4194 /* Get the size of the program header.
4196 If this is called by the linker before any of the section VMA's are set, it
4197 can't calculate the correct value for a strange memory layout. This only
4198 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4199 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4200 data segment (exclusive of .interp and .dynamic).
4202 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4203 will be two segments. */
4205 static bfd_size_type
4206 get_program_header_size (bfd
*abfd
)
4210 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4212 /* We can't return a different result each time we're called. */
4213 if (elf_tdata (abfd
)->program_header_size
!= 0)
4214 return elf_tdata (abfd
)->program_header_size
;
4216 if (elf_tdata (abfd
)->segment_map
!= NULL
)
4218 struct elf_segment_map
*m
;
4221 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4223 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4224 return elf_tdata (abfd
)->program_header_size
;
4227 /* Assume we will need exactly two PT_LOAD segments: one for text
4228 and one for data. */
4231 s
= bfd_get_section_by_name (abfd
, ".interp");
4232 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4234 /* If we have a loadable interpreter section, we need a
4235 PT_INTERP segment. In this case, assume we also need a
4236 PT_PHDR segment, although that may not be true for all
4241 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4243 /* We need a PT_DYNAMIC segment. */
4247 if (elf_tdata (abfd
)->eh_frame_hdr
)
4249 /* We need a PT_GNU_EH_FRAME segment. */
4253 if (elf_tdata (abfd
)->stack_flags
)
4255 /* We need a PT_GNU_STACK segment. */
4259 if (elf_tdata (abfd
)->relro
)
4261 /* We need a PT_GNU_RELRO segment. */
4265 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4267 if ((s
->flags
& SEC_LOAD
) != 0
4268 && strncmp (s
->name
, ".note", 5) == 0)
4270 /* We need a PT_NOTE segment. */
4275 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4277 if (s
->flags
& SEC_THREAD_LOCAL
)
4279 /* We need a PT_TLS segment. */
4285 /* Let the backend count up any program headers it might need. */
4286 if (bed
->elf_backend_additional_program_headers
)
4290 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
4296 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4297 return elf_tdata (abfd
)->program_header_size
;
4300 /* Work out the file positions of all the sections. This is called by
4301 _bfd_elf_compute_section_file_positions. All the section sizes and
4302 VMAs must be known before this is called.
4304 We do not consider reloc sections at this point, unless they form
4305 part of the loadable image. Reloc sections are assigned file
4306 positions in assign_file_positions_for_relocs, which is called by
4307 write_object_contents and final_link.
4309 We also don't set the positions of the .symtab and .strtab here. */
4312 assign_file_positions_except_relocs (bfd
*abfd
,
4313 struct bfd_link_info
*link_info
)
4315 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
4316 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
4317 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4318 unsigned int num_sec
= elf_numsections (abfd
);
4320 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4322 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4323 && bfd_get_format (abfd
) != bfd_core
)
4325 Elf_Internal_Shdr
**hdrpp
;
4328 /* Start after the ELF header. */
4329 off
= i_ehdrp
->e_ehsize
;
4331 /* We are not creating an executable, which means that we are
4332 not creating a program header, and that the actual order of
4333 the sections in the file is unimportant. */
4334 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4336 Elf_Internal_Shdr
*hdr
;
4339 if (hdr
->sh_type
== SHT_REL
4340 || hdr
->sh_type
== SHT_RELA
4341 || i
== tdata
->symtab_section
4342 || i
== tdata
->symtab_shndx_section
4343 || i
== tdata
->strtab_section
)
4345 hdr
->sh_offset
= -1;
4348 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4350 if (i
== SHN_LORESERVE
- 1)
4352 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4353 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4360 Elf_Internal_Shdr
**hdrpp
;
4362 /* Assign file positions for the loaded sections based on the
4363 assignment of sections to segments. */
4364 if (! assign_file_positions_for_segments (abfd
, link_info
))
4367 /* Assign file positions for the other sections. */
4369 off
= elf_tdata (abfd
)->next_file_pos
;
4370 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4372 Elf_Internal_Shdr
*hdr
;
4375 if (hdr
->bfd_section
!= NULL
4376 && hdr
->bfd_section
->filepos
!= 0)
4377 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
4378 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4380 ((*_bfd_error_handler
)
4381 (_("%s: warning: allocated section `%s' not in segment"),
4382 bfd_get_filename (abfd
),
4383 (hdr
->bfd_section
== NULL
4385 : hdr
->bfd_section
->name
)));
4386 if ((abfd
->flags
& D_PAGED
) != 0)
4387 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4390 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4392 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4395 else if (hdr
->sh_type
== SHT_REL
4396 || hdr
->sh_type
== SHT_RELA
4397 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4398 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4399 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4400 hdr
->sh_offset
= -1;
4402 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4404 if (i
== SHN_LORESERVE
- 1)
4406 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4407 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4412 /* Place the section headers. */
4413 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
4414 i_ehdrp
->e_shoff
= off
;
4415 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4417 elf_tdata (abfd
)->next_file_pos
= off
;
4423 prep_headers (bfd
*abfd
)
4425 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4426 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4427 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4428 struct elf_strtab_hash
*shstrtab
;
4429 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4431 i_ehdrp
= elf_elfheader (abfd
);
4432 i_shdrp
= elf_elfsections (abfd
);
4434 shstrtab
= _bfd_elf_strtab_init ();
4435 if (shstrtab
== NULL
)
4438 elf_shstrtab (abfd
) = shstrtab
;
4440 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4441 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4442 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4443 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4445 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4446 i_ehdrp
->e_ident
[EI_DATA
] =
4447 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4448 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4450 if ((abfd
->flags
& DYNAMIC
) != 0)
4451 i_ehdrp
->e_type
= ET_DYN
;
4452 else if ((abfd
->flags
& EXEC_P
) != 0)
4453 i_ehdrp
->e_type
= ET_EXEC
;
4454 else if (bfd_get_format (abfd
) == bfd_core
)
4455 i_ehdrp
->e_type
= ET_CORE
;
4457 i_ehdrp
->e_type
= ET_REL
;
4459 switch (bfd_get_arch (abfd
))
4461 case bfd_arch_unknown
:
4462 i_ehdrp
->e_machine
= EM_NONE
;
4465 /* There used to be a long list of cases here, each one setting
4466 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4467 in the corresponding bfd definition. To avoid duplication,
4468 the switch was removed. Machines that need special handling
4469 can generally do it in elf_backend_final_write_processing(),
4470 unless they need the information earlier than the final write.
4471 Such need can generally be supplied by replacing the tests for
4472 e_machine with the conditions used to determine it. */
4474 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
4477 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4478 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4480 /* No program header, for now. */
4481 i_ehdrp
->e_phoff
= 0;
4482 i_ehdrp
->e_phentsize
= 0;
4483 i_ehdrp
->e_phnum
= 0;
4485 /* Each bfd section is section header entry. */
4486 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4487 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4489 /* If we're building an executable, we'll need a program header table. */
4490 if (abfd
->flags
& EXEC_P
)
4492 /* It all happens later. */
4494 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
4496 /* elf_build_phdrs() returns a (NULL-terminated) array of
4497 Elf_Internal_Phdrs. */
4498 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
4499 i_ehdrp
->e_phoff
= outbase
;
4500 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
4505 i_ehdrp
->e_phentsize
= 0;
4507 i_ehdrp
->e_phoff
= 0;
4510 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4511 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
4512 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4513 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
4514 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4515 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
4516 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4517 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4518 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4524 /* Assign file positions for all the reloc sections which are not part
4525 of the loadable file image. */
4528 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
4531 unsigned int i
, num_sec
;
4532 Elf_Internal_Shdr
**shdrpp
;
4534 off
= elf_tdata (abfd
)->next_file_pos
;
4536 num_sec
= elf_numsections (abfd
);
4537 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4539 Elf_Internal_Shdr
*shdrp
;
4542 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4543 && shdrp
->sh_offset
== -1)
4544 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
4547 elf_tdata (abfd
)->next_file_pos
= off
;
4551 _bfd_elf_write_object_contents (bfd
*abfd
)
4553 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4554 Elf_Internal_Ehdr
*i_ehdrp
;
4555 Elf_Internal_Shdr
**i_shdrp
;
4557 unsigned int count
, num_sec
;
4559 if (! abfd
->output_has_begun
4560 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
4563 i_shdrp
= elf_elfsections (abfd
);
4564 i_ehdrp
= elf_elfheader (abfd
);
4567 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4571 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4573 /* After writing the headers, we need to write the sections too... */
4574 num_sec
= elf_numsections (abfd
);
4575 for (count
= 1; count
< num_sec
; count
++)
4577 if (bed
->elf_backend_section_processing
)
4578 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4579 if (i_shdrp
[count
]->contents
)
4581 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4583 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4584 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4587 if (count
== SHN_LORESERVE
- 1)
4588 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4591 /* Write out the section header names. */
4592 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4593 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4596 if (bed
->elf_backend_final_write_processing
)
4597 (*bed
->elf_backend_final_write_processing
) (abfd
,
4598 elf_tdata (abfd
)->linker
);
4600 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4604 _bfd_elf_write_corefile_contents (bfd
*abfd
)
4606 /* Hopefully this can be done just like an object file. */
4607 return _bfd_elf_write_object_contents (abfd
);
4610 /* Given a section, search the header to find them. */
4613 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
4615 const struct elf_backend_data
*bed
;
4618 if (elf_section_data (asect
) != NULL
4619 && elf_section_data (asect
)->this_idx
!= 0)
4620 return elf_section_data (asect
)->this_idx
;
4622 if (bfd_is_abs_section (asect
))
4624 else if (bfd_is_com_section (asect
))
4626 else if (bfd_is_und_section (asect
))
4630 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4631 int maxindex
= elf_numsections (abfd
);
4633 for (index
= 1; index
< maxindex
; index
++)
4635 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4637 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4643 bed
= get_elf_backend_data (abfd
);
4644 if (bed
->elf_backend_section_from_bfd_section
)
4648 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4653 bfd_set_error (bfd_error_nonrepresentable_section
);
4658 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4662 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
4664 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4666 flagword flags
= asym_ptr
->flags
;
4668 /* When gas creates relocations against local labels, it creates its
4669 own symbol for the section, but does put the symbol into the
4670 symbol chain, so udata is 0. When the linker is generating
4671 relocatable output, this section symbol may be for one of the
4672 input sections rather than the output section. */
4673 if (asym_ptr
->udata
.i
== 0
4674 && (flags
& BSF_SECTION_SYM
)
4675 && asym_ptr
->section
)
4679 if (asym_ptr
->section
->output_section
!= NULL
)
4680 indx
= asym_ptr
->section
->output_section
->index
;
4682 indx
= asym_ptr
->section
->index
;
4683 if (indx
< elf_num_section_syms (abfd
)
4684 && elf_section_syms (abfd
)[indx
] != NULL
)
4685 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4688 idx
= asym_ptr
->udata
.i
;
4692 /* This case can occur when using --strip-symbol on a symbol
4693 which is used in a relocation entry. */
4694 (*_bfd_error_handler
)
4695 (_("%s: symbol `%s' required but not present"),
4696 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4697 bfd_set_error (bfd_error_no_symbols
);
4704 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4705 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4706 elf_symbol_flags (flags
));
4714 /* Copy private BFD data. This copies any program header information. */
4717 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
4719 Elf_Internal_Ehdr
*iehdr
;
4720 struct elf_segment_map
*map
;
4721 struct elf_segment_map
*map_first
;
4722 struct elf_segment_map
**pointer_to_map
;
4723 Elf_Internal_Phdr
*segment
;
4726 unsigned int num_segments
;
4727 bfd_boolean phdr_included
= FALSE
;
4728 bfd_vma maxpagesize
;
4729 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
4730 unsigned int phdr_adjust_num
= 0;
4731 const struct elf_backend_data
*bed
;
4733 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4734 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4737 if (elf_tdata (ibfd
)->phdr
== NULL
)
4740 bed
= get_elf_backend_data (ibfd
);
4741 iehdr
= elf_elfheader (ibfd
);
4744 pointer_to_map
= &map_first
;
4746 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4747 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4749 /* Returns the end address of the segment + 1. */
4750 #define SEGMENT_END(segment, start) \
4751 (start + (segment->p_memsz > segment->p_filesz \
4752 ? segment->p_memsz : segment->p_filesz))
4754 #define SECTION_SIZE(section, segment) \
4755 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4756 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4757 ? section->size : 0)
4759 /* Returns TRUE if the given section is contained within
4760 the given segment. VMA addresses are compared. */
4761 #define IS_CONTAINED_BY_VMA(section, segment) \
4762 (section->vma >= segment->p_vaddr \
4763 && (section->vma + SECTION_SIZE (section, segment) \
4764 <= (SEGMENT_END (segment, segment->p_vaddr))))
4766 /* Returns TRUE if the given section is contained within
4767 the given segment. LMA addresses are compared. */
4768 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4769 (section->lma >= base \
4770 && (section->lma + SECTION_SIZE (section, segment) \
4771 <= SEGMENT_END (segment, base)))
4773 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4774 #define IS_COREFILE_NOTE(p, s) \
4775 (p->p_type == PT_NOTE \
4776 && bfd_get_format (ibfd) == bfd_core \
4777 && s->vma == 0 && s->lma == 0 \
4778 && (bfd_vma) s->filepos >= p->p_offset \
4779 && ((bfd_vma) s->filepos + s->size \
4780 <= p->p_offset + p->p_filesz))
4782 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4783 linker, which generates a PT_INTERP section with p_vaddr and
4784 p_memsz set to 0. */
4785 #define IS_SOLARIS_PT_INTERP(p, s) \
4787 && p->p_paddr == 0 \
4788 && p->p_memsz == 0 \
4789 && p->p_filesz > 0 \
4790 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4792 && (bfd_vma) s->filepos >= p->p_offset \
4793 && ((bfd_vma) s->filepos + s->size \
4794 <= p->p_offset + p->p_filesz))
4796 /* Decide if the given section should be included in the given segment.
4797 A section will be included if:
4798 1. It is within the address space of the segment -- we use the LMA
4799 if that is set for the segment and the VMA otherwise,
4800 2. It is an allocated segment,
4801 3. There is an output section associated with it,
4802 4. The section has not already been allocated to a previous segment.
4803 5. PT_GNU_STACK segments do not include any sections.
4804 6. PT_TLS segment includes only SHF_TLS sections.
4805 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4806 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4807 ((((segment->p_paddr \
4808 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4809 : IS_CONTAINED_BY_VMA (section, segment)) \
4810 && (section->flags & SEC_ALLOC) != 0) \
4811 || IS_COREFILE_NOTE (segment, section)) \
4812 && section->output_section != NULL \
4813 && segment->p_type != PT_GNU_STACK \
4814 && (segment->p_type != PT_TLS \
4815 || (section->flags & SEC_THREAD_LOCAL)) \
4816 && (segment->p_type == PT_LOAD \
4817 || segment->p_type == PT_TLS \
4818 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4819 && ! section->segment_mark)
4821 /* Returns TRUE iff seg1 starts after the end of seg2. */
4822 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4823 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4825 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4826 their VMA address ranges and their LMA address ranges overlap.
4827 It is possible to have overlapping VMA ranges without overlapping LMA
4828 ranges. RedBoot images for example can have both .data and .bss mapped
4829 to the same VMA range, but with the .data section mapped to a different
4831 #define SEGMENT_OVERLAPS(seg1, seg2) \
4832 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4833 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4834 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4835 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4837 /* Initialise the segment mark field. */
4838 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4839 section
->segment_mark
= FALSE
;
4841 /* Scan through the segments specified in the program header
4842 of the input BFD. For this first scan we look for overlaps
4843 in the loadable segments. These can be created by weird
4844 parameters to objcopy. Also, fix some solaris weirdness. */
4845 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4850 Elf_Internal_Phdr
*segment2
;
4852 if (segment
->p_type
== PT_INTERP
)
4853 for (section
= ibfd
->sections
; section
; section
= section
->next
)
4854 if (IS_SOLARIS_PT_INTERP (segment
, section
))
4856 /* Mininal change so that the normal section to segment
4857 assignment code will work. */
4858 segment
->p_vaddr
= section
->vma
;
4862 if (segment
->p_type
!= PT_LOAD
)
4865 /* Determine if this segment overlaps any previous segments. */
4866 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4868 bfd_signed_vma extra_length
;
4870 if (segment2
->p_type
!= PT_LOAD
4871 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4874 /* Merge the two segments together. */
4875 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4877 /* Extend SEGMENT2 to include SEGMENT and then delete
4880 SEGMENT_END (segment
, segment
->p_vaddr
)
4881 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4883 if (extra_length
> 0)
4885 segment2
->p_memsz
+= extra_length
;
4886 segment2
->p_filesz
+= extra_length
;
4889 segment
->p_type
= PT_NULL
;
4891 /* Since we have deleted P we must restart the outer loop. */
4893 segment
= elf_tdata (ibfd
)->phdr
;
4898 /* Extend SEGMENT to include SEGMENT2 and then delete
4901 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4902 - SEGMENT_END (segment
, segment
->p_vaddr
);
4904 if (extra_length
> 0)
4906 segment
->p_memsz
+= extra_length
;
4907 segment
->p_filesz
+= extra_length
;
4910 segment2
->p_type
= PT_NULL
;
4915 /* The second scan attempts to assign sections to segments. */
4916 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4920 unsigned int section_count
;
4921 asection
** sections
;
4922 asection
* output_section
;
4924 bfd_vma matching_lma
;
4925 bfd_vma suggested_lma
;
4929 if (segment
->p_type
== PT_NULL
)
4932 /* Compute how many sections might be placed into this segment. */
4933 for (section
= ibfd
->sections
, section_count
= 0;
4935 section
= section
->next
)
4936 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4939 /* Allocate a segment map big enough to contain
4940 all of the sections we have selected. */
4941 amt
= sizeof (struct elf_segment_map
);
4942 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4943 map
= bfd_alloc (obfd
, amt
);
4947 /* Initialise the fields of the segment map. Default to
4948 using the physical address of the segment in the input BFD. */
4950 map
->p_type
= segment
->p_type
;
4951 map
->p_flags
= segment
->p_flags
;
4952 map
->p_flags_valid
= 1;
4953 map
->p_paddr
= segment
->p_paddr
;
4954 map
->p_paddr_valid
= 1;
4956 /* Determine if this segment contains the ELF file header
4957 and if it contains the program headers themselves. */
4958 map
->includes_filehdr
= (segment
->p_offset
== 0
4959 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4961 map
->includes_phdrs
= 0;
4963 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4965 map
->includes_phdrs
=
4966 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4967 && (segment
->p_offset
+ segment
->p_filesz
4968 >= ((bfd_vma
) iehdr
->e_phoff
4969 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4971 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4972 phdr_included
= TRUE
;
4975 if (section_count
== 0)
4977 /* Special segments, such as the PT_PHDR segment, may contain
4978 no sections, but ordinary, loadable segments should contain
4979 something. They are allowed by the ELF spec however, so only
4980 a warning is produced. */
4981 if (segment
->p_type
== PT_LOAD
)
4982 (*_bfd_error_handler
)
4983 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4984 bfd_archive_filename (ibfd
));
4987 *pointer_to_map
= map
;
4988 pointer_to_map
= &map
->next
;
4993 /* Now scan the sections in the input BFD again and attempt
4994 to add their corresponding output sections to the segment map.
4995 The problem here is how to handle an output section which has
4996 been moved (ie had its LMA changed). There are four possibilities:
4998 1. None of the sections have been moved.
4999 In this case we can continue to use the segment LMA from the
5002 2. All of the sections have been moved by the same amount.
5003 In this case we can change the segment's LMA to match the LMA
5004 of the first section.
5006 3. Some of the sections have been moved, others have not.
5007 In this case those sections which have not been moved can be
5008 placed in the current segment which will have to have its size,
5009 and possibly its LMA changed, and a new segment or segments will
5010 have to be created to contain the other sections.
5012 4. The sections have been moved, but not by the same amount.
5013 In this case we can change the segment's LMA to match the LMA
5014 of the first section and we will have to create a new segment
5015 or segments to contain the other sections.
5017 In order to save time, we allocate an array to hold the section
5018 pointers that we are interested in. As these sections get assigned
5019 to a segment, they are removed from this array. */
5021 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5022 to work around this long long bug. */
5023 amt
= section_count
* sizeof (asection
*);
5024 sections
= bfd_malloc (amt
);
5025 if (sections
== NULL
)
5028 /* Step One: Scan for segment vs section LMA conflicts.
5029 Also add the sections to the section array allocated above.
5030 Also add the sections to the current segment. In the common
5031 case, where the sections have not been moved, this means that
5032 we have completely filled the segment, and there is nothing
5038 for (j
= 0, section
= ibfd
->sections
;
5040 section
= section
->next
)
5042 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5044 output_section
= section
->output_section
;
5046 sections
[j
++] = section
;
5048 /* The Solaris native linker always sets p_paddr to 0.
5049 We try to catch that case here, and set it to the
5050 correct value. Note - some backends require that
5051 p_paddr be left as zero. */
5052 if (segment
->p_paddr
== 0
5053 && segment
->p_vaddr
!= 0
5054 && (! bed
->want_p_paddr_set_to_zero
)
5056 && output_section
->lma
!= 0
5057 && (output_section
->vma
== (segment
->p_vaddr
5058 + (map
->includes_filehdr
5061 + (map
->includes_phdrs
5063 * iehdr
->e_phentsize
)
5065 map
->p_paddr
= segment
->p_vaddr
;
5067 /* Match up the physical address of the segment with the
5068 LMA address of the output section. */
5069 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5070 || IS_COREFILE_NOTE (segment
, section
)
5071 || (bed
->want_p_paddr_set_to_zero
&&
5072 IS_CONTAINED_BY_VMA (output_section
, segment
))
5075 if (matching_lma
== 0)
5076 matching_lma
= output_section
->lma
;
5078 /* We assume that if the section fits within the segment
5079 then it does not overlap any other section within that
5081 map
->sections
[isec
++] = output_section
;
5083 else if (suggested_lma
== 0)
5084 suggested_lma
= output_section
->lma
;
5088 BFD_ASSERT (j
== section_count
);
5090 /* Step Two: Adjust the physical address of the current segment,
5092 if (isec
== section_count
)
5094 /* All of the sections fitted within the segment as currently
5095 specified. This is the default case. Add the segment to
5096 the list of built segments and carry on to process the next
5097 program header in the input BFD. */
5098 map
->count
= section_count
;
5099 *pointer_to_map
= map
;
5100 pointer_to_map
= &map
->next
;
5107 if (matching_lma
!= 0)
5109 /* At least one section fits inside the current segment.
5110 Keep it, but modify its physical address to match the
5111 LMA of the first section that fitted. */
5112 map
->p_paddr
= matching_lma
;
5116 /* None of the sections fitted inside the current segment.
5117 Change the current segment's physical address to match
5118 the LMA of the first section. */
5119 map
->p_paddr
= suggested_lma
;
5122 /* Offset the segment physical address from the lma
5123 to allow for space taken up by elf headers. */
5124 if (map
->includes_filehdr
)
5125 map
->p_paddr
-= iehdr
->e_ehsize
;
5127 if (map
->includes_phdrs
)
5129 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5131 /* iehdr->e_phnum is just an estimate of the number
5132 of program headers that we will need. Make a note
5133 here of the number we used and the segment we chose
5134 to hold these headers, so that we can adjust the
5135 offset when we know the correct value. */
5136 phdr_adjust_num
= iehdr
->e_phnum
;
5137 phdr_adjust_seg
= map
;
5141 /* Step Three: Loop over the sections again, this time assigning
5142 those that fit to the current segment and removing them from the
5143 sections array; but making sure not to leave large gaps. Once all
5144 possible sections have been assigned to the current segment it is
5145 added to the list of built segments and if sections still remain
5146 to be assigned, a new segment is constructed before repeating
5154 /* Fill the current segment with sections that fit. */
5155 for (j
= 0; j
< section_count
; j
++)
5157 section
= sections
[j
];
5159 if (section
== NULL
)
5162 output_section
= section
->output_section
;
5164 BFD_ASSERT (output_section
!= NULL
);
5166 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5167 || IS_COREFILE_NOTE (segment
, section
))
5169 if (map
->count
== 0)
5171 /* If the first section in a segment does not start at
5172 the beginning of the segment, then something is
5174 if (output_section
->lma
!=
5176 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5177 + (map
->includes_phdrs
5178 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5184 asection
* prev_sec
;
5186 prev_sec
= map
->sections
[map
->count
- 1];
5188 /* If the gap between the end of the previous section
5189 and the start of this section is more than
5190 maxpagesize then we need to start a new segment. */
5191 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
5193 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5194 || ((prev_sec
->lma
+ prev_sec
->size
)
5195 > output_section
->lma
))
5197 if (suggested_lma
== 0)
5198 suggested_lma
= output_section
->lma
;
5204 map
->sections
[map
->count
++] = output_section
;
5207 section
->segment_mark
= TRUE
;
5209 else if (suggested_lma
== 0)
5210 suggested_lma
= output_section
->lma
;
5213 BFD_ASSERT (map
->count
> 0);
5215 /* Add the current segment to the list of built segments. */
5216 *pointer_to_map
= map
;
5217 pointer_to_map
= &map
->next
;
5219 if (isec
< section_count
)
5221 /* We still have not allocated all of the sections to
5222 segments. Create a new segment here, initialise it
5223 and carry on looping. */
5224 amt
= sizeof (struct elf_segment_map
);
5225 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5226 map
= bfd_alloc (obfd
, amt
);
5233 /* Initialise the fields of the segment map. Set the physical
5234 physical address to the LMA of the first section that has
5235 not yet been assigned. */
5237 map
->p_type
= segment
->p_type
;
5238 map
->p_flags
= segment
->p_flags
;
5239 map
->p_flags_valid
= 1;
5240 map
->p_paddr
= suggested_lma
;
5241 map
->p_paddr_valid
= 1;
5242 map
->includes_filehdr
= 0;
5243 map
->includes_phdrs
= 0;
5246 while (isec
< section_count
);
5251 /* The Solaris linker creates program headers in which all the
5252 p_paddr fields are zero. When we try to objcopy or strip such a
5253 file, we get confused. Check for this case, and if we find it
5254 reset the p_paddr_valid fields. */
5255 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5256 if (map
->p_paddr
!= 0)
5259 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5260 map
->p_paddr_valid
= 0;
5262 elf_tdata (obfd
)->segment_map
= map_first
;
5264 /* If we had to estimate the number of program headers that were
5265 going to be needed, then check our estimate now and adjust
5266 the offset if necessary. */
5267 if (phdr_adjust_seg
!= NULL
)
5271 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5274 if (count
> phdr_adjust_num
)
5275 phdr_adjust_seg
->p_paddr
5276 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5280 /* Final Step: Sort the segments into ascending order of physical
5282 if (map_first
!= NULL
)
5284 struct elf_segment_map
*prev
;
5287 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
5289 /* Yes I know - its a bubble sort.... */
5290 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
5292 /* Swap map and map->next. */
5293 prev
->next
= map
->next
;
5294 map
->next
= map
->next
->next
;
5295 prev
->next
->next
= map
;
5306 #undef IS_CONTAINED_BY_VMA
5307 #undef IS_CONTAINED_BY_LMA
5308 #undef IS_COREFILE_NOTE
5309 #undef IS_SOLARIS_PT_INTERP
5310 #undef INCLUDE_SECTION_IN_SEGMENT
5311 #undef SEGMENT_AFTER_SEGMENT
5312 #undef SEGMENT_OVERLAPS
5316 /* Copy private section information. This copies over the entsize
5317 field, and sometimes the info field. */
5320 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
5325 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5327 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5328 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5331 ihdr
= &elf_section_data (isec
)->this_hdr
;
5332 ohdr
= &elf_section_data (osec
)->this_hdr
;
5334 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
5336 if (ihdr
->sh_type
== SHT_SYMTAB
5337 || ihdr
->sh_type
== SHT_DYNSYM
5338 || ihdr
->sh_type
== SHT_GNU_verneed
5339 || ihdr
->sh_type
== SHT_GNU_verdef
)
5340 ohdr
->sh_info
= ihdr
->sh_info
;
5342 /* Set things up for objcopy. The output SHT_GROUP section will
5343 have its elf_next_in_group pointing back to the input group
5345 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5346 elf_group_name (osec
) = elf_group_name (isec
);
5348 osec
->use_rela_p
= isec
->use_rela_p
;
5353 /* Copy private header information. */
5356 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
5358 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5359 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5362 /* Copy over private BFD data if it has not already been copied.
5363 This must be done here, rather than in the copy_private_bfd_data
5364 entry point, because the latter is called after the section
5365 contents have been set, which means that the program headers have
5366 already been worked out. */
5367 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
5369 if (! copy_private_bfd_data (ibfd
, obfd
))
5376 /* Copy private symbol information. If this symbol is in a section
5377 which we did not map into a BFD section, try to map the section
5378 index correctly. We use special macro definitions for the mapped
5379 section indices; these definitions are interpreted by the
5380 swap_out_syms function. */
5382 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5383 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5384 #define MAP_STRTAB (SHN_HIOS + 3)
5385 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5386 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5389 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
5394 elf_symbol_type
*isym
, *osym
;
5396 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5397 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5400 isym
= elf_symbol_from (ibfd
, isymarg
);
5401 osym
= elf_symbol_from (obfd
, osymarg
);
5405 && bfd_is_abs_section (isym
->symbol
.section
))
5409 shndx
= isym
->internal_elf_sym
.st_shndx
;
5410 if (shndx
== elf_onesymtab (ibfd
))
5411 shndx
= MAP_ONESYMTAB
;
5412 else if (shndx
== elf_dynsymtab (ibfd
))
5413 shndx
= MAP_DYNSYMTAB
;
5414 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
5416 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
5417 shndx
= MAP_SHSTRTAB
;
5418 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
5419 shndx
= MAP_SYM_SHNDX
;
5420 osym
->internal_elf_sym
.st_shndx
= shndx
;
5426 /* Swap out the symbols. */
5429 swap_out_syms (bfd
*abfd
,
5430 struct bfd_strtab_hash
**sttp
,
5433 const struct elf_backend_data
*bed
;
5436 struct bfd_strtab_hash
*stt
;
5437 Elf_Internal_Shdr
*symtab_hdr
;
5438 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5439 Elf_Internal_Shdr
*symstrtab_hdr
;
5440 char *outbound_syms
;
5441 char *outbound_shndx
;
5444 bfd_boolean name_local_sections
;
5446 if (!elf_map_symbols (abfd
))
5449 /* Dump out the symtabs. */
5450 stt
= _bfd_elf_stringtab_init ();
5454 bed
= get_elf_backend_data (abfd
);
5455 symcount
= bfd_get_symcount (abfd
);
5456 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5457 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5458 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5459 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5460 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5461 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
5463 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5464 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5466 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
5467 outbound_syms
= bfd_alloc (abfd
, amt
);
5468 if (outbound_syms
== NULL
)
5470 _bfd_stringtab_free (stt
);
5473 symtab_hdr
->contents
= outbound_syms
;
5475 outbound_shndx
= NULL
;
5476 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5477 if (symtab_shndx_hdr
->sh_name
!= 0)
5479 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5480 outbound_shndx
= bfd_zalloc (abfd
, amt
);
5481 if (outbound_shndx
== NULL
)
5483 _bfd_stringtab_free (stt
);
5487 symtab_shndx_hdr
->contents
= outbound_shndx
;
5488 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5489 symtab_shndx_hdr
->sh_size
= amt
;
5490 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5491 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5494 /* Now generate the data (for "contents"). */
5496 /* Fill in zeroth symbol and swap it out. */
5497 Elf_Internal_Sym sym
;
5503 sym
.st_shndx
= SHN_UNDEF
;
5504 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5505 outbound_syms
+= bed
->s
->sizeof_sym
;
5506 if (outbound_shndx
!= NULL
)
5507 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5511 = (bed
->elf_backend_name_local_section_symbols
5512 && bed
->elf_backend_name_local_section_symbols (abfd
));
5514 syms
= bfd_get_outsymbols (abfd
);
5515 for (idx
= 0; idx
< symcount
; idx
++)
5517 Elf_Internal_Sym sym
;
5518 bfd_vma value
= syms
[idx
]->value
;
5519 elf_symbol_type
*type_ptr
;
5520 flagword flags
= syms
[idx
]->flags
;
5523 if (!name_local_sections
5524 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5526 /* Local section symbols have no name. */
5531 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5534 if (sym
.st_name
== (unsigned long) -1)
5536 _bfd_stringtab_free (stt
);
5541 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5543 if ((flags
& BSF_SECTION_SYM
) == 0
5544 && bfd_is_com_section (syms
[idx
]->section
))
5546 /* ELF common symbols put the alignment into the `value' field,
5547 and the size into the `size' field. This is backwards from
5548 how BFD handles it, so reverse it here. */
5549 sym
.st_size
= value
;
5550 if (type_ptr
== NULL
5551 || type_ptr
->internal_elf_sym
.st_value
== 0)
5552 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5554 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5555 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5556 (abfd
, syms
[idx
]->section
);
5560 asection
*sec
= syms
[idx
]->section
;
5563 if (sec
->output_section
)
5565 value
+= sec
->output_offset
;
5566 sec
= sec
->output_section
;
5569 /* Don't add in the section vma for relocatable output. */
5570 if (! relocatable_p
)
5572 sym
.st_value
= value
;
5573 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5575 if (bfd_is_abs_section (sec
)
5577 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5579 /* This symbol is in a real ELF section which we did
5580 not create as a BFD section. Undo the mapping done
5581 by copy_private_symbol_data. */
5582 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5586 shndx
= elf_onesymtab (abfd
);
5589 shndx
= elf_dynsymtab (abfd
);
5592 shndx
= elf_tdata (abfd
)->strtab_section
;
5595 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5598 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5606 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5612 /* Writing this would be a hell of a lot easier if
5613 we had some decent documentation on bfd, and
5614 knew what to expect of the library, and what to
5615 demand of applications. For example, it
5616 appears that `objcopy' might not set the
5617 section of a symbol to be a section that is
5618 actually in the output file. */
5619 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5622 _bfd_error_handler (_("\
5623 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5624 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
5626 bfd_set_error (bfd_error_invalid_operation
);
5627 _bfd_stringtab_free (stt
);
5631 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5632 BFD_ASSERT (shndx
!= -1);
5636 sym
.st_shndx
= shndx
;
5639 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5641 else if ((flags
& BSF_FUNCTION
) != 0)
5643 else if ((flags
& BSF_OBJECT
) != 0)
5648 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5651 /* Processor-specific types. */
5652 if (type_ptr
!= NULL
5653 && bed
->elf_backend_get_symbol_type
)
5654 type
= ((*bed
->elf_backend_get_symbol_type
)
5655 (&type_ptr
->internal_elf_sym
, type
));
5657 if (flags
& BSF_SECTION_SYM
)
5659 if (flags
& BSF_GLOBAL
)
5660 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5662 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5664 else if (bfd_is_com_section (syms
[idx
]->section
))
5665 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5666 else if (bfd_is_und_section (syms
[idx
]->section
))
5667 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5671 else if (flags
& BSF_FILE
)
5672 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5675 int bind
= STB_LOCAL
;
5677 if (flags
& BSF_LOCAL
)
5679 else if (flags
& BSF_WEAK
)
5681 else if (flags
& BSF_GLOBAL
)
5684 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5687 if (type_ptr
!= NULL
)
5688 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5692 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5693 outbound_syms
+= bed
->s
->sizeof_sym
;
5694 if (outbound_shndx
!= NULL
)
5695 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5699 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5700 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5702 symstrtab_hdr
->sh_flags
= 0;
5703 symstrtab_hdr
->sh_addr
= 0;
5704 symstrtab_hdr
->sh_entsize
= 0;
5705 symstrtab_hdr
->sh_link
= 0;
5706 symstrtab_hdr
->sh_info
= 0;
5707 symstrtab_hdr
->sh_addralign
= 1;
5712 /* Return the number of bytes required to hold the symtab vector.
5714 Note that we base it on the count plus 1, since we will null terminate
5715 the vector allocated based on this size. However, the ELF symbol table
5716 always has a dummy entry as symbol #0, so it ends up even. */
5719 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
5723 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5725 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5726 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5728 symtab_size
-= sizeof (asymbol
*);
5734 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
5738 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5740 if (elf_dynsymtab (abfd
) == 0)
5742 bfd_set_error (bfd_error_invalid_operation
);
5746 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5747 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5749 symtab_size
-= sizeof (asymbol
*);
5755 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
5758 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5761 /* Canonicalize the relocs. */
5764 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
5771 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5773 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
5776 tblptr
= section
->relocation
;
5777 for (i
= 0; i
< section
->reloc_count
; i
++)
5778 *relptr
++ = tblptr
++;
5782 return section
->reloc_count
;
5786 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
5788 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5789 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
5792 bfd_get_symcount (abfd
) = symcount
;
5797 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
5798 asymbol
**allocation
)
5800 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5801 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
5804 bfd_get_dynamic_symcount (abfd
) = symcount
;
5808 /* Return the size required for the dynamic reloc entries. Any
5809 section that was actually installed in the BFD, and has type
5810 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5811 considered to be a dynamic reloc section. */
5814 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
5819 if (elf_dynsymtab (abfd
) == 0)
5821 bfd_set_error (bfd_error_invalid_operation
);
5825 ret
= sizeof (arelent
*);
5826 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5827 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5828 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5829 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5830 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5831 * sizeof (arelent
*));
5836 /* Canonicalize the dynamic relocation entries. Note that we return
5837 the dynamic relocations as a single block, although they are
5838 actually associated with particular sections; the interface, which
5839 was designed for SunOS style shared libraries, expects that there
5840 is only one set of dynamic relocs. Any section that was actually
5841 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5842 the dynamic symbol table, is considered to be a dynamic reloc
5846 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
5850 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
5854 if (elf_dynsymtab (abfd
) == 0)
5856 bfd_set_error (bfd_error_invalid_operation
);
5860 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5862 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5864 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5865 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5866 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5871 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
5873 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5875 for (i
= 0; i
< count
; i
++)
5886 /* Read in the version information. */
5889 _bfd_elf_slurp_version_tables (bfd
*abfd
)
5891 bfd_byte
*contents
= NULL
;
5894 if (elf_dynverdef (abfd
) != 0)
5896 Elf_Internal_Shdr
*hdr
;
5897 Elf_External_Verdef
*everdef
;
5898 Elf_Internal_Verdef
*iverdef
;
5899 Elf_Internal_Verdef
*iverdefarr
;
5900 Elf_Internal_Verdef iverdefmem
;
5902 unsigned int maxidx
;
5904 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5906 contents
= bfd_malloc (hdr
->sh_size
);
5907 if (contents
== NULL
)
5909 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5910 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5913 /* We know the number of entries in the section but not the maximum
5914 index. Therefore we have to run through all entries and find
5916 everdef
= (Elf_External_Verdef
*) contents
;
5918 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5920 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5922 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5923 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5925 everdef
= ((Elf_External_Verdef
*)
5926 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5929 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5930 elf_tdata (abfd
)->verdef
= bfd_zalloc (abfd
, amt
);
5931 if (elf_tdata (abfd
)->verdef
== NULL
)
5934 elf_tdata (abfd
)->cverdefs
= maxidx
;
5936 everdef
= (Elf_External_Verdef
*) contents
;
5937 iverdefarr
= elf_tdata (abfd
)->verdef
;
5938 for (i
= 0; i
< hdr
->sh_info
; i
++)
5940 Elf_External_Verdaux
*everdaux
;
5941 Elf_Internal_Verdaux
*iverdaux
;
5944 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5946 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5947 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5949 iverdef
->vd_bfd
= abfd
;
5951 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5952 iverdef
->vd_auxptr
= bfd_alloc (abfd
, amt
);
5953 if (iverdef
->vd_auxptr
== NULL
)
5956 everdaux
= ((Elf_External_Verdaux
*)
5957 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5958 iverdaux
= iverdef
->vd_auxptr
;
5959 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5961 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5963 iverdaux
->vda_nodename
=
5964 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5965 iverdaux
->vda_name
);
5966 if (iverdaux
->vda_nodename
== NULL
)
5969 if (j
+ 1 < iverdef
->vd_cnt
)
5970 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5972 iverdaux
->vda_nextptr
= NULL
;
5974 everdaux
= ((Elf_External_Verdaux
*)
5975 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5978 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5980 if (i
+ 1 < hdr
->sh_info
)
5981 iverdef
->vd_nextdef
= iverdef
+ 1;
5983 iverdef
->vd_nextdef
= NULL
;
5985 everdef
= ((Elf_External_Verdef
*)
5986 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5993 if (elf_dynverref (abfd
) != 0)
5995 Elf_Internal_Shdr
*hdr
;
5996 Elf_External_Verneed
*everneed
;
5997 Elf_Internal_Verneed
*iverneed
;
6000 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
6002 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
6003 elf_tdata (abfd
)->verref
= bfd_zalloc (abfd
, amt
);
6004 if (elf_tdata (abfd
)->verref
== NULL
)
6007 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
6009 contents
= bfd_malloc (hdr
->sh_size
);
6010 if (contents
== NULL
)
6012 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6013 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6016 everneed
= (Elf_External_Verneed
*) contents
;
6017 iverneed
= elf_tdata (abfd
)->verref
;
6018 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
6020 Elf_External_Vernaux
*evernaux
;
6021 Elf_Internal_Vernaux
*ivernaux
;
6024 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
6026 iverneed
->vn_bfd
= abfd
;
6028 iverneed
->vn_filename
=
6029 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6031 if (iverneed
->vn_filename
== NULL
)
6034 amt
= iverneed
->vn_cnt
;
6035 amt
*= sizeof (Elf_Internal_Vernaux
);
6036 iverneed
->vn_auxptr
= bfd_alloc (abfd
, amt
);
6038 evernaux
= ((Elf_External_Vernaux
*)
6039 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
6040 ivernaux
= iverneed
->vn_auxptr
;
6041 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
6043 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
6045 ivernaux
->vna_nodename
=
6046 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6047 ivernaux
->vna_name
);
6048 if (ivernaux
->vna_nodename
== NULL
)
6051 if (j
+ 1 < iverneed
->vn_cnt
)
6052 ivernaux
->vna_nextptr
= ivernaux
+ 1;
6054 ivernaux
->vna_nextptr
= NULL
;
6056 evernaux
= ((Elf_External_Vernaux
*)
6057 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
6060 if (i
+ 1 < hdr
->sh_info
)
6061 iverneed
->vn_nextref
= iverneed
+ 1;
6063 iverneed
->vn_nextref
= NULL
;
6065 everneed
= ((Elf_External_Verneed
*)
6066 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
6076 if (contents
!= NULL
)
6082 _bfd_elf_make_empty_symbol (bfd
*abfd
)
6084 elf_symbol_type
*newsym
;
6085 bfd_size_type amt
= sizeof (elf_symbol_type
);
6087 newsym
= bfd_zalloc (abfd
, amt
);
6092 newsym
->symbol
.the_bfd
= abfd
;
6093 return &newsym
->symbol
;
6098 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
6102 bfd_symbol_info (symbol
, ret
);
6105 /* Return whether a symbol name implies a local symbol. Most targets
6106 use this function for the is_local_label_name entry point, but some
6110 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
6113 /* Normal local symbols start with ``.L''. */
6114 if (name
[0] == '.' && name
[1] == 'L')
6117 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6118 DWARF debugging symbols starting with ``..''. */
6119 if (name
[0] == '.' && name
[1] == '.')
6122 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6123 emitting DWARF debugging output. I suspect this is actually a
6124 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6125 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6126 underscore to be emitted on some ELF targets). For ease of use,
6127 we treat such symbols as local. */
6128 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
6135 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
6136 asymbol
*symbol ATTRIBUTE_UNUSED
)
6143 _bfd_elf_set_arch_mach (bfd
*abfd
,
6144 enum bfd_architecture arch
,
6145 unsigned long machine
)
6147 /* If this isn't the right architecture for this backend, and this
6148 isn't the generic backend, fail. */
6149 if (arch
!= get_elf_backend_data (abfd
)->arch
6150 && arch
!= bfd_arch_unknown
6151 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
6154 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
6157 /* Find the function to a particular section and offset,
6158 for error reporting. */
6161 elf_find_function (bfd
*abfd ATTRIBUTE_UNUSED
,
6165 const char **filename_ptr
,
6166 const char **functionname_ptr
)
6168 const char *filename
;
6177 for (p
= symbols
; *p
!= NULL
; p
++)
6181 q
= (elf_symbol_type
*) *p
;
6183 if (bfd_get_section (&q
->symbol
) != section
)
6186 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
6191 filename
= bfd_asymbol_name (&q
->symbol
);
6195 if (q
->symbol
.section
== section
6196 && q
->symbol
.value
>= low_func
6197 && q
->symbol
.value
<= offset
)
6199 func
= (asymbol
*) q
;
6200 low_func
= q
->symbol
.value
;
6210 *filename_ptr
= filename
;
6211 if (functionname_ptr
)
6212 *functionname_ptr
= bfd_asymbol_name (func
);
6217 /* Find the nearest line to a particular section and offset,
6218 for error reporting. */
6221 _bfd_elf_find_nearest_line (bfd
*abfd
,
6225 const char **filename_ptr
,
6226 const char **functionname_ptr
,
6227 unsigned int *line_ptr
)
6231 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
6232 filename_ptr
, functionname_ptr
,
6235 if (!*functionname_ptr
)
6236 elf_find_function (abfd
, section
, symbols
, offset
,
6237 *filename_ptr
? NULL
: filename_ptr
,
6243 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
6244 filename_ptr
, functionname_ptr
,
6246 &elf_tdata (abfd
)->dwarf2_find_line_info
))
6248 if (!*functionname_ptr
)
6249 elf_find_function (abfd
, section
, symbols
, offset
,
6250 *filename_ptr
? NULL
: filename_ptr
,
6256 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
6257 &found
, filename_ptr
,
6258 functionname_ptr
, line_ptr
,
6259 &elf_tdata (abfd
)->line_info
))
6261 if (found
&& (*functionname_ptr
|| *line_ptr
))
6264 if (symbols
== NULL
)
6267 if (! elf_find_function (abfd
, section
, symbols
, offset
,
6268 filename_ptr
, functionname_ptr
))
6276 _bfd_elf_sizeof_headers (bfd
*abfd
, bfd_boolean reloc
)
6280 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
6282 ret
+= get_program_header_size (abfd
);
6287 _bfd_elf_set_section_contents (bfd
*abfd
,
6289 const void *location
,
6291 bfd_size_type count
)
6293 Elf_Internal_Shdr
*hdr
;
6296 if (! abfd
->output_has_begun
6297 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6300 hdr
= &elf_section_data (section
)->this_hdr
;
6301 pos
= hdr
->sh_offset
+ offset
;
6302 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6303 || bfd_bwrite (location
, count
, abfd
) != count
)
6310 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
6311 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
6312 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
6317 /* Try to convert a non-ELF reloc into an ELF one. */
6320 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
6322 /* Check whether we really have an ELF howto. */
6324 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
6326 bfd_reloc_code_real_type code
;
6327 reloc_howto_type
*howto
;
6329 /* Alien reloc: Try to determine its type to replace it with an
6330 equivalent ELF reloc. */
6332 if (areloc
->howto
->pc_relative
)
6334 switch (areloc
->howto
->bitsize
)
6337 code
= BFD_RELOC_8_PCREL
;
6340 code
= BFD_RELOC_12_PCREL
;
6343 code
= BFD_RELOC_16_PCREL
;
6346 code
= BFD_RELOC_24_PCREL
;
6349 code
= BFD_RELOC_32_PCREL
;
6352 code
= BFD_RELOC_64_PCREL
;
6358 howto
= bfd_reloc_type_lookup (abfd
, code
);
6360 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
6362 if (howto
->pcrel_offset
)
6363 areloc
->addend
+= areloc
->address
;
6365 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
6370 switch (areloc
->howto
->bitsize
)
6376 code
= BFD_RELOC_14
;
6379 code
= BFD_RELOC_16
;
6382 code
= BFD_RELOC_26
;
6385 code
= BFD_RELOC_32
;
6388 code
= BFD_RELOC_64
;
6394 howto
= bfd_reloc_type_lookup (abfd
, code
);
6398 areloc
->howto
= howto
;
6406 (*_bfd_error_handler
)
6407 (_("%s: unsupported relocation type %s"),
6408 bfd_archive_filename (abfd
), areloc
->howto
->name
);
6409 bfd_set_error (bfd_error_bad_value
);
6414 _bfd_elf_close_and_cleanup (bfd
*abfd
)
6416 if (bfd_get_format (abfd
) == bfd_object
)
6418 if (elf_shstrtab (abfd
) != NULL
)
6419 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
6422 return _bfd_generic_close_and_cleanup (abfd
);
6425 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6426 in the relocation's offset. Thus we cannot allow any sort of sanity
6427 range-checking to interfere. There is nothing else to do in processing
6430 bfd_reloc_status_type
6431 _bfd_elf_rel_vtable_reloc_fn
6432 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
6433 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
6434 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
6435 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
6437 return bfd_reloc_ok
;
6440 /* Elf core file support. Much of this only works on native
6441 toolchains, since we rely on knowing the
6442 machine-dependent procfs structure in order to pick
6443 out details about the corefile. */
6445 #ifdef HAVE_SYS_PROCFS_H
6446 # include <sys/procfs.h>
6449 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6452 elfcore_make_pid (bfd
*abfd
)
6454 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
6455 + (elf_tdata (abfd
)->core_pid
));
6458 /* If there isn't a section called NAME, make one, using
6459 data from SECT. Note, this function will generate a
6460 reference to NAME, so you shouldn't deallocate or
6464 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
6468 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
6471 sect2
= bfd_make_section (abfd
, name
);
6475 sect2
->size
= sect
->size
;
6476 sect2
->filepos
= sect
->filepos
;
6477 sect2
->flags
= sect
->flags
;
6478 sect2
->alignment_power
= sect
->alignment_power
;
6482 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6483 actually creates up to two pseudosections:
6484 - For the single-threaded case, a section named NAME, unless
6485 such a section already exists.
6486 - For the multi-threaded case, a section named "NAME/PID", where
6487 PID is elfcore_make_pid (abfd).
6488 Both pseudosections have identical contents. */
6490 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
6496 char *threaded_name
;
6500 /* Build the section name. */
6502 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6503 len
= strlen (buf
) + 1;
6504 threaded_name
= bfd_alloc (abfd
, len
);
6505 if (threaded_name
== NULL
)
6507 memcpy (threaded_name
, buf
, len
);
6509 sect
= bfd_make_section_anyway (abfd
, threaded_name
);
6513 sect
->filepos
= filepos
;
6514 sect
->flags
= SEC_HAS_CONTENTS
;
6515 sect
->alignment_power
= 2;
6517 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6520 /* prstatus_t exists on:
6522 linux 2.[01] + glibc
6526 #if defined (HAVE_PRSTATUS_T)
6529 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6534 if (note
->descsz
== sizeof (prstatus_t
))
6538 size
= sizeof (prstat
.pr_reg
);
6539 offset
= offsetof (prstatus_t
, pr_reg
);
6540 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6542 /* Do not overwrite the core signal if it
6543 has already been set by another thread. */
6544 if (elf_tdata (abfd
)->core_signal
== 0)
6545 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6546 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6548 /* pr_who exists on:
6551 pr_who doesn't exist on:
6554 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6555 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6558 #if defined (HAVE_PRSTATUS32_T)
6559 else if (note
->descsz
== sizeof (prstatus32_t
))
6561 /* 64-bit host, 32-bit corefile */
6562 prstatus32_t prstat
;
6564 size
= sizeof (prstat
.pr_reg
);
6565 offset
= offsetof (prstatus32_t
, pr_reg
);
6566 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6568 /* Do not overwrite the core signal if it
6569 has already been set by another thread. */
6570 if (elf_tdata (abfd
)->core_signal
== 0)
6571 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6572 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6574 /* pr_who exists on:
6577 pr_who doesn't exist on:
6580 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6581 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6584 #endif /* HAVE_PRSTATUS32_T */
6587 /* Fail - we don't know how to handle any other
6588 note size (ie. data object type). */
6592 /* Make a ".reg/999" section and a ".reg" section. */
6593 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6594 size
, note
->descpos
+ offset
);
6596 #endif /* defined (HAVE_PRSTATUS_T) */
6598 /* Create a pseudosection containing the exact contents of NOTE. */
6600 elfcore_make_note_pseudosection (bfd
*abfd
,
6602 Elf_Internal_Note
*note
)
6604 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6605 note
->descsz
, note
->descpos
);
6608 /* There isn't a consistent prfpregset_t across platforms,
6609 but it doesn't matter, because we don't have to pick this
6610 data structure apart. */
6613 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
6615 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6618 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6619 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6623 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
6625 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6628 #if defined (HAVE_PRPSINFO_T)
6629 typedef prpsinfo_t elfcore_psinfo_t
;
6630 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6631 typedef prpsinfo32_t elfcore_psinfo32_t
;
6635 #if defined (HAVE_PSINFO_T)
6636 typedef psinfo_t elfcore_psinfo_t
;
6637 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6638 typedef psinfo32_t elfcore_psinfo32_t
;
6642 /* return a malloc'ed copy of a string at START which is at
6643 most MAX bytes long, possibly without a terminating '\0'.
6644 the copy will always have a terminating '\0'. */
6647 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
6650 char *end
= memchr (start
, '\0', max
);
6658 dups
= bfd_alloc (abfd
, len
+ 1);
6662 memcpy (dups
, start
, len
);
6668 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6670 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
6672 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6674 elfcore_psinfo_t psinfo
;
6676 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6678 elf_tdata (abfd
)->core_program
6679 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6680 sizeof (psinfo
.pr_fname
));
6682 elf_tdata (abfd
)->core_command
6683 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6684 sizeof (psinfo
.pr_psargs
));
6686 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6687 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6689 /* 64-bit host, 32-bit corefile */
6690 elfcore_psinfo32_t psinfo
;
6692 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6694 elf_tdata (abfd
)->core_program
6695 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6696 sizeof (psinfo
.pr_fname
));
6698 elf_tdata (abfd
)->core_command
6699 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6700 sizeof (psinfo
.pr_psargs
));
6706 /* Fail - we don't know how to handle any other
6707 note size (ie. data object type). */
6711 /* Note that for some reason, a spurious space is tacked
6712 onto the end of the args in some (at least one anyway)
6713 implementations, so strip it off if it exists. */
6716 char *command
= elf_tdata (abfd
)->core_command
;
6717 int n
= strlen (command
);
6719 if (0 < n
&& command
[n
- 1] == ' ')
6720 command
[n
- 1] = '\0';
6725 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6727 #if defined (HAVE_PSTATUS_T)
6729 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6731 if (note
->descsz
== sizeof (pstatus_t
)
6732 #if defined (HAVE_PXSTATUS_T)
6733 || note
->descsz
== sizeof (pxstatus_t
)
6739 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6741 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6743 #if defined (HAVE_PSTATUS32_T)
6744 else if (note
->descsz
== sizeof (pstatus32_t
))
6746 /* 64-bit host, 32-bit corefile */
6749 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6751 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6754 /* Could grab some more details from the "representative"
6755 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6756 NT_LWPSTATUS note, presumably. */
6760 #endif /* defined (HAVE_PSTATUS_T) */
6762 #if defined (HAVE_LWPSTATUS_T)
6764 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6766 lwpstatus_t lwpstat
;
6772 if (note
->descsz
!= sizeof (lwpstat
)
6773 #if defined (HAVE_LWPXSTATUS_T)
6774 && note
->descsz
!= sizeof (lwpxstatus_t
)
6779 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6781 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6782 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6784 /* Make a ".reg/999" section. */
6786 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6787 len
= strlen (buf
) + 1;
6788 name
= bfd_alloc (abfd
, len
);
6791 memcpy (name
, buf
, len
);
6793 sect
= bfd_make_section_anyway (abfd
, name
);
6797 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6798 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6799 sect
->filepos
= note
->descpos
6800 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6803 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6804 sect
->size
= sizeof (lwpstat
.pr_reg
);
6805 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6808 sect
->flags
= SEC_HAS_CONTENTS
;
6809 sect
->alignment_power
= 2;
6811 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6814 /* Make a ".reg2/999" section */
6816 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6817 len
= strlen (buf
) + 1;
6818 name
= bfd_alloc (abfd
, len
);
6821 memcpy (name
, buf
, len
);
6823 sect
= bfd_make_section_anyway (abfd
, name
);
6827 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6828 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6829 sect
->filepos
= note
->descpos
6830 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6833 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6834 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
6835 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6838 sect
->flags
= SEC_HAS_CONTENTS
;
6839 sect
->alignment_power
= 2;
6841 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6843 #endif /* defined (HAVE_LWPSTATUS_T) */
6845 #if defined (HAVE_WIN32_PSTATUS_T)
6847 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6853 win32_pstatus_t pstatus
;
6855 if (note
->descsz
< sizeof (pstatus
))
6858 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
6860 switch (pstatus
.data_type
)
6862 case NOTE_INFO_PROCESS
:
6863 /* FIXME: need to add ->core_command. */
6864 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6865 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6868 case NOTE_INFO_THREAD
:
6869 /* Make a ".reg/999" section. */
6870 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6872 len
= strlen (buf
) + 1;
6873 name
= bfd_alloc (abfd
, len
);
6877 memcpy (name
, buf
, len
);
6879 sect
= bfd_make_section_anyway (abfd
, name
);
6883 sect
->size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6884 sect
->filepos
= (note
->descpos
6885 + offsetof (struct win32_pstatus
,
6886 data
.thread_info
.thread_context
));
6887 sect
->flags
= SEC_HAS_CONTENTS
;
6888 sect
->alignment_power
= 2;
6890 if (pstatus
.data
.thread_info
.is_active_thread
)
6891 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6895 case NOTE_INFO_MODULE
:
6896 /* Make a ".module/xxxxxxxx" section. */
6897 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6899 len
= strlen (buf
) + 1;
6900 name
= bfd_alloc (abfd
, len
);
6904 memcpy (name
, buf
, len
);
6906 sect
= bfd_make_section_anyway (abfd
, name
);
6911 sect
->size
= note
->descsz
;
6912 sect
->filepos
= note
->descpos
;
6913 sect
->flags
= SEC_HAS_CONTENTS
;
6914 sect
->alignment_power
= 2;
6923 #endif /* HAVE_WIN32_PSTATUS_T */
6926 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
6928 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6936 if (bed
->elf_backend_grok_prstatus
)
6937 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6939 #if defined (HAVE_PRSTATUS_T)
6940 return elfcore_grok_prstatus (abfd
, note
);
6945 #if defined (HAVE_PSTATUS_T)
6947 return elfcore_grok_pstatus (abfd
, note
);
6950 #if defined (HAVE_LWPSTATUS_T)
6952 return elfcore_grok_lwpstatus (abfd
, note
);
6955 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6956 return elfcore_grok_prfpreg (abfd
, note
);
6958 #if defined (HAVE_WIN32_PSTATUS_T)
6959 case NT_WIN32PSTATUS
:
6960 return elfcore_grok_win32pstatus (abfd
, note
);
6963 case NT_PRXFPREG
: /* Linux SSE extension */
6964 if (note
->namesz
== 6
6965 && strcmp (note
->namedata
, "LINUX") == 0)
6966 return elfcore_grok_prxfpreg (abfd
, note
);
6972 if (bed
->elf_backend_grok_psinfo
)
6973 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6975 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6976 return elfcore_grok_psinfo (abfd
, note
);
6983 asection
*sect
= bfd_make_section_anyway (abfd
, ".auxv");
6987 sect
->size
= note
->descsz
;
6988 sect
->filepos
= note
->descpos
;
6989 sect
->flags
= SEC_HAS_CONTENTS
;
6990 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
6998 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
7002 cp
= strchr (note
->namedata
, '@');
7005 *lwpidp
= atoi(cp
+ 1);
7012 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7015 /* Signal number at offset 0x08. */
7016 elf_tdata (abfd
)->core_signal
7017 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
7019 /* Process ID at offset 0x50. */
7020 elf_tdata (abfd
)->core_pid
7021 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
7023 /* Command name at 0x7c (max 32 bytes, including nul). */
7024 elf_tdata (abfd
)->core_command
7025 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
7027 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
7032 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7036 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
7037 elf_tdata (abfd
)->core_lwpid
= lwp
;
7039 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
7041 /* NetBSD-specific core "procinfo". Note that we expect to
7042 find this note before any of the others, which is fine,
7043 since the kernel writes this note out first when it
7044 creates a core file. */
7046 return elfcore_grok_netbsd_procinfo (abfd
, note
);
7049 /* As of Jan 2002 there are no other machine-independent notes
7050 defined for NetBSD core files. If the note type is less
7051 than the start of the machine-dependent note types, we don't
7054 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
7058 switch (bfd_get_arch (abfd
))
7060 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7061 PT_GETFPREGS == mach+2. */
7063 case bfd_arch_alpha
:
7064 case bfd_arch_sparc
:
7067 case NT_NETBSDCORE_FIRSTMACH
+0:
7068 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
7070 case NT_NETBSDCORE_FIRSTMACH
+2:
7071 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7077 /* On all other arch's, PT_GETREGS == mach+1 and
7078 PT_GETFPREGS == mach+3. */
7083 case NT_NETBSDCORE_FIRSTMACH
+1:
7084 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
7086 case NT_NETBSDCORE_FIRSTMACH
+3:
7087 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7097 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, pid_t
*tid
)
7099 void *ddata
= note
->descdata
;
7106 /* nto_procfs_status 'pid' field is at offset 0. */
7107 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
7109 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7110 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
7112 /* nto_procfs_status 'flags' field is at offset 8. */
7113 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
7115 /* nto_procfs_status 'what' field is at offset 14. */
7116 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
7118 elf_tdata (abfd
)->core_signal
= sig
;
7119 elf_tdata (abfd
)->core_lwpid
= *tid
;
7122 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7123 do not come from signals so we make sure we set the current
7124 thread just in case. */
7125 if (flags
& 0x00000080)
7126 elf_tdata (abfd
)->core_lwpid
= *tid
;
7128 /* Make a ".qnx_core_status/%d" section. */
7129 sprintf (buf
, ".qnx_core_status/%d", *tid
);
7131 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
7136 sect
= bfd_make_section_anyway (abfd
, name
);
7140 sect
->size
= note
->descsz
;
7141 sect
->filepos
= note
->descpos
;
7142 sect
->flags
= SEC_HAS_CONTENTS
;
7143 sect
->alignment_power
= 2;
7145 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
7149 elfcore_grok_nto_gregs (bfd
*abfd
, Elf_Internal_Note
*note
, pid_t tid
)
7155 /* Make a ".reg/%d" section. */
7156 sprintf (buf
, ".reg/%d", tid
);
7158 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
7163 sect
= bfd_make_section_anyway (abfd
, name
);
7167 sect
->size
= note
->descsz
;
7168 sect
->filepos
= note
->descpos
;
7169 sect
->flags
= SEC_HAS_CONTENTS
;
7170 sect
->alignment_power
= 2;
7172 /* This is the current thread. */
7173 if (elf_tdata (abfd
)->core_lwpid
== tid
)
7174 return elfcore_maybe_make_sect (abfd
, ".reg", sect
);
7179 #define BFD_QNT_CORE_INFO 7
7180 #define BFD_QNT_CORE_STATUS 8
7181 #define BFD_QNT_CORE_GREG 9
7182 #define BFD_QNT_CORE_FPREG 10
7185 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7187 /* Every GREG section has a STATUS section before it. Store the
7188 tid from the previous call to pass down to the next gregs
7190 static pid_t tid
= 1;
7194 case BFD_QNT_CORE_INFO
: return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
7195 case BFD_QNT_CORE_STATUS
: return elfcore_grok_nto_status (abfd
, note
, &tid
);
7196 case BFD_QNT_CORE_GREG
: return elfcore_grok_nto_gregs (abfd
, note
, tid
);
7197 case BFD_QNT_CORE_FPREG
: return elfcore_grok_prfpreg (abfd
, note
);
7198 default: return TRUE
;
7202 /* Function: elfcore_write_note
7209 size of data for note
7212 End of buffer containing note. */
7215 elfcore_write_note (bfd
*abfd
,
7223 Elf_External_Note
*xnp
;
7233 const struct elf_backend_data
*bed
;
7235 namesz
= strlen (name
) + 1;
7236 bed
= get_elf_backend_data (abfd
);
7237 pad
= -namesz
& ((1 << bed
->s
->log_file_align
) - 1);
7240 newspace
= 12 + namesz
+ pad
+ size
;
7242 p
= realloc (buf
, *bufsiz
+ newspace
);
7244 *bufsiz
+= newspace
;
7245 xnp
= (Elf_External_Note
*) dest
;
7246 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
7247 H_PUT_32 (abfd
, size
, xnp
->descsz
);
7248 H_PUT_32 (abfd
, type
, xnp
->type
);
7252 memcpy (dest
, name
, namesz
);
7260 memcpy (dest
, input
, size
);
7264 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7266 elfcore_write_prpsinfo (bfd
*abfd
,
7273 char *note_name
= "CORE";
7275 #if defined (HAVE_PSINFO_T)
7277 note_type
= NT_PSINFO
;
7280 note_type
= NT_PRPSINFO
;
7283 memset (&data
, 0, sizeof (data
));
7284 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
7285 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
7286 return elfcore_write_note (abfd
, buf
, bufsiz
,
7287 note_name
, note_type
, &data
, sizeof (data
));
7289 #endif /* PSINFO_T or PRPSINFO_T */
7291 #if defined (HAVE_PRSTATUS_T)
7293 elfcore_write_prstatus (bfd
*abfd
,
7301 char *note_name
= "CORE";
7303 memset (&prstat
, 0, sizeof (prstat
));
7304 prstat
.pr_pid
= pid
;
7305 prstat
.pr_cursig
= cursig
;
7306 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
7307 return elfcore_write_note (abfd
, buf
, bufsiz
,
7308 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
7310 #endif /* HAVE_PRSTATUS_T */
7312 #if defined (HAVE_LWPSTATUS_T)
7314 elfcore_write_lwpstatus (bfd
*abfd
,
7321 lwpstatus_t lwpstat
;
7322 char *note_name
= "CORE";
7324 memset (&lwpstat
, 0, sizeof (lwpstat
));
7325 lwpstat
.pr_lwpid
= pid
>> 16;
7326 lwpstat
.pr_cursig
= cursig
;
7327 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7328 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
7329 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7331 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
7332 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
7334 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
7335 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
7338 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7339 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
7341 #endif /* HAVE_LWPSTATUS_T */
7343 #if defined (HAVE_PSTATUS_T)
7345 elfcore_write_pstatus (bfd
*abfd
,
7353 char *note_name
= "CORE";
7355 memset (&pstat
, 0, sizeof (pstat
));
7356 pstat
.pr_pid
= pid
& 0xffff;
7357 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7358 NT_PSTATUS
, &pstat
, sizeof (pstat
));
7361 #endif /* HAVE_PSTATUS_T */
7364 elfcore_write_prfpreg (bfd
*abfd
,
7370 char *note_name
= "CORE";
7371 return elfcore_write_note (abfd
, buf
, bufsiz
,
7372 note_name
, NT_FPREGSET
, fpregs
, size
);
7376 elfcore_write_prxfpreg (bfd
*abfd
,
7379 const void *xfpregs
,
7382 char *note_name
= "LINUX";
7383 return elfcore_write_note (abfd
, buf
, bufsiz
,
7384 note_name
, NT_PRXFPREG
, xfpregs
, size
);
7388 elfcore_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
7396 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
7399 buf
= bfd_malloc (size
);
7403 if (bfd_bread (buf
, size
, abfd
) != size
)
7411 while (p
< buf
+ size
)
7413 /* FIXME: bad alignment assumption. */
7414 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
7415 Elf_Internal_Note in
;
7417 in
.type
= H_GET_32 (abfd
, xnp
->type
);
7419 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
7420 in
.namedata
= xnp
->name
;
7422 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
7423 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
7424 in
.descpos
= offset
+ (in
.descdata
- buf
);
7426 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
7428 if (! elfcore_grok_netbsd_note (abfd
, &in
))
7431 else if (strncmp (in
.namedata
, "QNX", 3) == 0)
7433 if (! elfcore_grok_nto_note (abfd
, &in
))
7438 if (! elfcore_grok_note (abfd
, &in
))
7442 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
7449 /* Providing external access to the ELF program header table. */
7451 /* Return an upper bound on the number of bytes required to store a
7452 copy of ABFD's program header table entries. Return -1 if an error
7453 occurs; bfd_get_error will return an appropriate code. */
7456 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
7458 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7460 bfd_set_error (bfd_error_wrong_format
);
7464 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
7467 /* Copy ABFD's program header table entries to *PHDRS. The entries
7468 will be stored as an array of Elf_Internal_Phdr structures, as
7469 defined in include/elf/internal.h. To find out how large the
7470 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7472 Return the number of program header table entries read, or -1 if an
7473 error occurs; bfd_get_error will return an appropriate code. */
7476 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
7480 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7482 bfd_set_error (bfd_error_wrong_format
);
7486 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
7487 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
7488 num_phdrs
* sizeof (Elf_Internal_Phdr
));
7494 _bfd_elf_sprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, char *buf
, bfd_vma value
)
7497 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7499 i_ehdrp
= elf_elfheader (abfd
);
7500 if (i_ehdrp
== NULL
)
7501 sprintf_vma (buf
, value
);
7504 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7506 #if BFD_HOST_64BIT_LONG
7507 sprintf (buf
, "%016lx", value
);
7509 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
7510 _bfd_int64_low (value
));
7514 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
7517 sprintf_vma (buf
, value
);
7522 _bfd_elf_fprintf_vma (bfd
*abfd ATTRIBUTE_UNUSED
, void *stream
, bfd_vma value
)
7525 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7527 i_ehdrp
= elf_elfheader (abfd
);
7528 if (i_ehdrp
== NULL
)
7529 fprintf_vma ((FILE *) stream
, value
);
7532 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7534 #if BFD_HOST_64BIT_LONG
7535 fprintf ((FILE *) stream
, "%016lx", value
);
7537 fprintf ((FILE *) stream
, "%08lx%08lx",
7538 _bfd_int64_high (value
), _bfd_int64_low (value
));
7542 fprintf ((FILE *) stream
, "%08lx",
7543 (unsigned long) (value
& 0xffffffff));
7546 fprintf_vma ((FILE *) stream
, value
);
7550 enum elf_reloc_type_class
7551 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
7553 return reloc_class_normal
;
7556 /* For RELA architectures, return the relocation value for a
7557 relocation against a local symbol. */
7560 _bfd_elf_rela_local_sym (bfd
*abfd
,
7561 Elf_Internal_Sym
*sym
,
7563 Elf_Internal_Rela
*rel
)
7565 asection
*sec
= *psec
;
7568 relocation
= (sec
->output_section
->vma
7569 + sec
->output_offset
7571 if ((sec
->flags
& SEC_MERGE
)
7572 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
7573 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
7576 _bfd_merged_section_offset (abfd
, psec
,
7577 elf_section_data (sec
)->sec_info
,
7578 sym
->st_value
+ rel
->r_addend
);
7581 /* If we have changed the section, and our original section is
7582 marked with SEC_EXCLUDE, it means that the original
7583 SEC_MERGE section has been completely subsumed in some
7584 other SEC_MERGE section. In this case, we need to leave
7585 some info around for --emit-relocs. */
7586 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
7587 sec
->kept_section
= *psec
;
7590 rel
->r_addend
-= relocation
;
7591 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
7597 _bfd_elf_rel_local_sym (bfd
*abfd
,
7598 Elf_Internal_Sym
*sym
,
7602 asection
*sec
= *psec
;
7604 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7605 return sym
->st_value
+ addend
;
7607 return _bfd_merged_section_offset (abfd
, psec
,
7608 elf_section_data (sec
)->sec_info
,
7609 sym
->st_value
+ addend
);
7613 _bfd_elf_section_offset (bfd
*abfd
,
7614 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
7618 switch (sec
->sec_info_type
)
7620 case ELF_INFO_TYPE_STABS
:
7621 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
7623 case ELF_INFO_TYPE_EH_FRAME
:
7624 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);
7630 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7631 reconstruct an ELF file by reading the segments out of remote memory
7632 based on the ELF file header at EHDR_VMA and the ELF program headers it
7633 points to. If not null, *LOADBASEP is filled in with the difference
7634 between the VMAs from which the segments were read, and the VMAs the
7635 file headers (and hence BFD's idea of each section's VMA) put them at.
7637 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7638 remote memory at target address VMA into the local buffer at MYADDR; it
7639 should return zero on success or an `errno' code on failure. TEMPL must
7640 be a BFD for an ELF target with the word size and byte order found in
7641 the remote memory. */
7644 bfd_elf_bfd_from_remote_memory
7648 int (*target_read_memory
) (bfd_vma
, char *, int))
7650 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
7651 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
7655 _bfd_elf_get_synthetic_symtab (bfd
*abfd
, asymbol
**dynsyms
, asymbol
**ret
)
7657 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7660 const char *relplt_name
;
7661 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7665 Elf_Internal_Shdr
*hdr
;
7670 if (!bed
->plt_sym_val
)
7673 relplt_name
= bed
->relplt_name
;
7674 if (relplt_name
== NULL
)
7675 relplt_name
= bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt";
7676 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
7680 hdr
= &elf_section_data (relplt
)->this_hdr
;
7681 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
7682 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
7685 plt
= bfd_get_section_by_name (abfd
, ".plt");
7689 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7690 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
7693 count
= relplt
->size
/ hdr
->sh_entsize
;
7694 size
= count
* sizeof (asymbol
);
7695 p
= relplt
->relocation
;
7696 for (i
= 0; i
< count
; i
++, s
++, p
++)
7697 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
7699 s
= *ret
= bfd_malloc (size
);
7703 names
= (char *) (s
+ count
);
7704 p
= relplt
->relocation
;
7706 for (i
= 0; i
< count
; i
++, s
++, p
++)
7711 addr
= bed
->plt_sym_val (i
, plt
, p
);
7712 if (addr
== (bfd_vma
) -1)
7715 *s
= **p
->sym_ptr_ptr
;
7717 s
->value
= addr
- plt
->vma
;
7719 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
7720 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
7722 memcpy (names
, "@plt", sizeof ("@plt"));
7723 names
+= sizeof ("@plt");