1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
31 haven't bothered yet. */
33 /* For sparc64-cross-sparc32. */
41 #include "libiberty.h"
43 static INLINE
struct elf_segment_map
*make_mapping
44 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, boolean
));
45 static boolean map_sections_to_segments
PARAMS ((bfd
*));
46 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
47 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
48 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
49 static boolean prep_headers
PARAMS ((bfd
*));
50 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**, int));
51 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
52 static char *elf_read
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
53 static const char *group_signature
PARAMS ((bfd
*, Elf_Internal_Shdr
*));
54 static boolean setup_group
PARAMS ((bfd
*, Elf_Internal_Shdr
*, asection
*));
55 static void merge_sections_remove_hook
PARAMS ((bfd
*, asection
*));
56 static void elf_fake_sections
PARAMS ((bfd
*, asection
*, PTR
));
57 static boolean assign_section_numbers
PARAMS ((bfd
*));
58 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
59 static boolean elf_map_symbols
PARAMS ((bfd
*));
60 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
61 static boolean elfcore_read_notes
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
62 static boolean elf_find_function
PARAMS ((bfd
*, asection
*, asymbol
**,
63 bfd_vma
, const char **,
65 static int elfcore_make_pid
PARAMS ((bfd
*));
66 static boolean elfcore_maybe_make_sect
PARAMS ((bfd
*, char *, asection
*));
67 static boolean elfcore_make_note_pseudosection
PARAMS ((bfd
*, char *,
68 Elf_Internal_Note
*));
69 static boolean elfcore_grok_prfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
70 static boolean elfcore_grok_prxfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
71 static boolean elfcore_grok_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
73 static boolean elfcore_netbsd_get_lwpid
PARAMS ((Elf_Internal_Note
*, int *));
74 static boolean elfcore_grok_netbsd_procinfo
PARAMS ((bfd
*,
75 Elf_Internal_Note
*));
76 static boolean elfcore_grok_netbsd_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
78 /* Swap version information in and out. The version information is
79 currently size independent. If that ever changes, this code will
80 need to move into elfcode.h. */
82 /* Swap in a Verdef structure. */
85 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
87 const Elf_External_Verdef
*src
;
88 Elf_Internal_Verdef
*dst
;
90 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
91 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
92 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
93 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
94 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
95 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
96 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
99 /* Swap out a Verdef structure. */
102 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
104 const Elf_Internal_Verdef
*src
;
105 Elf_External_Verdef
*dst
;
107 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
108 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
109 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
110 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
111 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
112 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
113 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
116 /* Swap in a Verdaux structure. */
119 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
121 const Elf_External_Verdaux
*src
;
122 Elf_Internal_Verdaux
*dst
;
124 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
125 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
128 /* Swap out a Verdaux structure. */
131 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
133 const Elf_Internal_Verdaux
*src
;
134 Elf_External_Verdaux
*dst
;
136 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
137 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
140 /* Swap in a Verneed structure. */
143 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
145 const Elf_External_Verneed
*src
;
146 Elf_Internal_Verneed
*dst
;
148 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
149 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
150 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
151 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
152 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
155 /* Swap out a Verneed structure. */
158 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
160 const Elf_Internal_Verneed
*src
;
161 Elf_External_Verneed
*dst
;
163 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
164 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
165 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
166 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
167 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
170 /* Swap in a Vernaux structure. */
173 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
175 const Elf_External_Vernaux
*src
;
176 Elf_Internal_Vernaux
*dst
;
178 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
179 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
180 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
181 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
182 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
185 /* Swap out a Vernaux structure. */
188 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
190 const Elf_Internal_Vernaux
*src
;
191 Elf_External_Vernaux
*dst
;
193 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
194 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
195 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
196 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
197 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
200 /* Swap in a Versym structure. */
203 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
205 const Elf_External_Versym
*src
;
206 Elf_Internal_Versym
*dst
;
208 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
211 /* Swap out a Versym structure. */
214 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
216 const Elf_Internal_Versym
*src
;
217 Elf_External_Versym
*dst
;
219 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
222 /* Standard ELF hash function. Do not change this function; you will
223 cause invalid hash tables to be generated. */
226 bfd_elf_hash (namearg
)
229 const unsigned char *name
= (const unsigned char *) namearg
;
234 while ((ch
= *name
++) != '\0')
237 if ((g
= (h
& 0xf0000000)) != 0)
240 /* The ELF ABI says `h &= ~g', but this is equivalent in
241 this case and on some machines one insn instead of two. */
248 /* Read a specified number of bytes at a specified offset in an ELF
249 file, into a newly allocated buffer, and return a pointer to the
253 elf_read (abfd
, offset
, size
)
260 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
262 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
264 if (bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
266 if (bfd_get_error () != bfd_error_system_call
)
267 bfd_set_error (bfd_error_file_truncated
);
274 bfd_elf_mkobject (abfd
)
277 /* This just does initialization. */
278 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
279 bfd_size_type amt
= sizeof (struct elf_obj_tdata
);
280 elf_tdata (abfd
) = (struct elf_obj_tdata
*) bfd_zalloc (abfd
, amt
);
281 if (elf_tdata (abfd
) == 0)
283 /* Since everything is done at close time, do we need any
290 bfd_elf_mkcorefile (abfd
)
293 /* I think this can be done just like an object file. */
294 return bfd_elf_mkobject (abfd
);
298 bfd_elf_get_str_section (abfd
, shindex
)
300 unsigned int shindex
;
302 Elf_Internal_Shdr
**i_shdrp
;
303 char *shstrtab
= NULL
;
305 bfd_size_type shstrtabsize
;
307 i_shdrp
= elf_elfsections (abfd
);
308 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
311 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
312 if (shstrtab
== NULL
)
314 /* No cached one, attempt to read, and cache what we read. */
315 offset
= i_shdrp
[shindex
]->sh_offset
;
316 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
317 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
318 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
324 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
326 unsigned int shindex
;
327 unsigned int strindex
;
329 Elf_Internal_Shdr
*hdr
;
334 hdr
= elf_elfsections (abfd
)[shindex
];
336 if (hdr
->contents
== NULL
337 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
340 if (strindex
>= hdr
->sh_size
)
342 (*_bfd_error_handler
)
343 (_("%s: invalid string offset %u >= %lu for section `%s'"),
344 bfd_archive_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
345 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
346 && strindex
== hdr
->sh_name
)
348 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
352 return ((char *) hdr
->contents
) + strindex
;
355 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
356 sections. The first element is the flags, the rest are section
359 typedef union elf_internal_group
{
360 Elf_Internal_Shdr
*shdr
;
362 } Elf_Internal_Group
;
364 /* Return the name of the group signature symbol. Why isn't the
365 signature just a string? */
368 group_signature (abfd
, ghdr
)
370 Elf_Internal_Shdr
*ghdr
;
372 struct elf_backend_data
*bed
;
375 Elf_Internal_Shdr
*hdr
;
376 Elf_Internal_Shdr
*shndx_hdr
;
377 unsigned char esym
[sizeof (Elf64_External_Sym
)];
378 Elf_External_Sym_Shndx eshndx
;
379 Elf_Internal_Sym isym
;
381 unsigned int shindex
;
383 /* First we need to ensure the symbol table is available. */
384 if (! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
387 /* Go read the symbol. */
388 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
389 bed
= get_elf_backend_data (abfd
);
390 amt
= bed
->s
->sizeof_sym
;
391 pos
= hdr
->sh_offset
+ ghdr
->sh_info
* amt
;
392 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
393 || bfd_bread (esym
, amt
, abfd
) != amt
)
396 /* And possibly the symbol section index extension. */
397 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
398 if (elf_elfsections (abfd
) != NULL
399 && elf_elfsections (abfd
)[shndx_hdr
->sh_link
] == hdr
)
401 amt
= sizeof (Elf_External_Sym_Shndx
);
402 pos
= shndx_hdr
->sh_offset
+ ghdr
->sh_info
* amt
;
403 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
404 || bfd_bread ((PTR
) &eshndx
, amt
, abfd
) != amt
)
408 /* Convert to internal format. */
409 (*bed
->s
->swap_symbol_in
) (abfd
, (const PTR
*) &esym
, (const PTR
*) &eshndx
,
412 /* Look up the symbol name. */
413 iname
= isym
.st_name
;
414 shindex
= hdr
->sh_link
;
415 if (iname
== 0 && ELF_ST_TYPE (isym
.st_info
) == STT_SECTION
)
417 iname
= elf_elfsections (abfd
)[isym
.st_shndx
]->sh_name
;
418 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
421 return bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
424 /* Set next_in_group list pointer, and group name for NEWSECT. */
427 setup_group (abfd
, hdr
, newsect
)
429 Elf_Internal_Shdr
*hdr
;
432 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
434 /* If num_group is zero, read in all SHT_GROUP sections. The count
435 is set to -1 if there are no SHT_GROUP sections. */
438 unsigned int i
, shnum
;
440 /* First count the number of groups. If we have a SHT_GROUP
441 section with just a flag word (ie. sh_size is 4), ignore it. */
442 shnum
= elf_numsections (abfd
);
444 for (i
= 0; i
< shnum
; i
++)
446 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
447 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
452 num_group
= (unsigned) -1;
453 elf_tdata (abfd
)->num_group
= num_group
;
457 /* We keep a list of elf section headers for group sections,
458 so we can find them quickly. */
459 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
460 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
461 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
465 for (i
= 0; i
< shnum
; i
++)
467 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
468 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
471 Elf_Internal_Group
*dest
;
473 /* Add to list of sections. */
474 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
477 /* Read the raw contents. */
478 BFD_ASSERT (sizeof (*dest
) >= 4);
479 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
480 shdr
->contents
= bfd_alloc (abfd
, amt
);
481 if (shdr
->contents
== NULL
482 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
483 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
487 /* Translate raw contents, a flag word followed by an
488 array of elf section indices all in target byte order,
489 to the flag word followed by an array of elf section
491 src
= shdr
->contents
+ shdr
->sh_size
;
492 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
499 idx
= H_GET_32 (abfd
, src
);
500 if (src
== shdr
->contents
)
503 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
504 shdr
->bfd_section
->flags
505 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
510 ((*_bfd_error_handler
)
511 (_("%s: invalid SHT_GROUP entry"),
512 bfd_archive_filename (abfd
)));
515 dest
->shdr
= elf_elfsections (abfd
)[idx
];
522 if (num_group
!= (unsigned) -1)
526 for (i
= 0; i
< num_group
; i
++)
528 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
529 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
530 unsigned int n_elt
= shdr
->sh_size
/ 4;
532 /* Look through this group's sections to see if current
533 section is a member. */
535 if ((++idx
)->shdr
== hdr
)
539 /* We are a member of this group. Go looking through
540 other members to see if any others are linked via
542 idx
= (Elf_Internal_Group
*) shdr
->contents
;
543 n_elt
= shdr
->sh_size
/ 4;
545 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
546 && elf_next_in_group (s
) != NULL
)
550 /* Snarf the group name from other member, and
551 insert current section in circular list. */
552 elf_group_name (newsect
) = elf_group_name (s
);
553 elf_next_in_group (newsect
) = elf_next_in_group (s
);
554 elf_next_in_group (s
) = newsect
;
560 gname
= group_signature (abfd
, shdr
);
563 elf_group_name (newsect
) = gname
;
565 /* Start a circular list with one element. */
566 elf_next_in_group (newsect
) = newsect
;
569 /* If the group section has been created, point to the
571 if (shdr
->bfd_section
!= NULL
)
572 elf_next_in_group (shdr
->bfd_section
) = newsect
;
580 if (elf_group_name (newsect
) == NULL
)
582 (*_bfd_error_handler
) (_("%s: no group info for section %s"),
583 bfd_archive_filename (abfd
), newsect
->name
);
589 bfd_elf_discard_group (abfd
, group
)
590 bfd
*abfd ATTRIBUTE_UNUSED
;
593 asection
*first
= elf_next_in_group (group
);
598 s
->output_section
= bfd_abs_section_ptr
;
599 s
= elf_next_in_group (s
);
600 /* These lists are circular. */
607 /* Make a BFD section from an ELF section. We store a pointer to the
608 BFD section in the bfd_section field of the header. */
611 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
613 Elf_Internal_Shdr
*hdr
;
618 struct elf_backend_data
*bed
;
620 if (hdr
->bfd_section
!= NULL
)
622 BFD_ASSERT (strcmp (name
,
623 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
627 newsect
= bfd_make_section_anyway (abfd
, name
);
631 newsect
->filepos
= hdr
->sh_offset
;
633 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
634 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
635 || ! bfd_set_section_alignment (abfd
, newsect
,
636 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
639 flags
= SEC_NO_FLAGS
;
640 if (hdr
->sh_type
!= SHT_NOBITS
)
641 flags
|= SEC_HAS_CONTENTS
;
642 if (hdr
->sh_type
== SHT_GROUP
)
643 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
644 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
647 if (hdr
->sh_type
!= SHT_NOBITS
)
650 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
651 flags
|= SEC_READONLY
;
652 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
654 else if ((flags
& SEC_LOAD
) != 0)
656 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
659 newsect
->entsize
= hdr
->sh_entsize
;
660 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
661 flags
|= SEC_STRINGS
;
663 if (hdr
->sh_flags
& SHF_GROUP
)
664 if (!setup_group (abfd
, hdr
, newsect
))
666 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
667 flags
|= SEC_THREAD_LOCAL
;
669 /* The debugging sections appear to be recognized only by name, not
672 static const char *debug_sec_names
[] =
681 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
682 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
686 flags
|= SEC_DEBUGGING
;
689 /* As a GNU extension, if the name begins with .gnu.linkonce, we
690 only link a single copy of the section. This is used to support
691 g++. g++ will emit each template expansion in its own section.
692 The symbols will be defined as weak, so that multiple definitions
693 are permitted. The GNU linker extension is to actually discard
694 all but one of the sections. */
695 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
696 && elf_next_in_group (newsect
) == NULL
)
697 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
699 bed
= get_elf_backend_data (abfd
);
700 if (bed
->elf_backend_section_flags
)
701 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
704 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
707 if ((flags
& SEC_ALLOC
) != 0)
709 Elf_Internal_Phdr
*phdr
;
712 /* Look through the phdrs to see if we need to adjust the lma.
713 If all the p_paddr fields are zero, we ignore them, since
714 some ELF linkers produce such output. */
715 phdr
= elf_tdata (abfd
)->phdr
;
716 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
718 if (phdr
->p_paddr
!= 0)
721 if (i
< elf_elfheader (abfd
)->e_phnum
)
723 phdr
= elf_tdata (abfd
)->phdr
;
724 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
726 /* This section is part of this segment if its file
727 offset plus size lies within the segment's memory
728 span and, if the section is loaded, the extent of the
729 loaded data lies within the extent of the segment.
731 Note - we used to check the p_paddr field as well, and
732 refuse to set the LMA if it was 0. This is wrong
733 though, as a perfectly valid initialised segment can
734 have a p_paddr of zero. Some architectures, eg ARM,
735 place special significance on the address 0 and
736 executables need to be able to have a segment which
737 covers this address. */
738 if (phdr
->p_type
== PT_LOAD
739 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
740 && (hdr
->sh_offset
+ hdr
->sh_size
741 <= phdr
->p_offset
+ phdr
->p_memsz
)
742 && ((flags
& SEC_LOAD
) == 0
743 || (hdr
->sh_offset
+ hdr
->sh_size
744 <= phdr
->p_offset
+ phdr
->p_filesz
)))
746 if ((flags
& SEC_LOAD
) == 0)
747 newsect
->lma
= (phdr
->p_paddr
748 + hdr
->sh_addr
- phdr
->p_vaddr
);
750 /* We used to use the same adjustment for SEC_LOAD
751 sections, but that doesn't work if the segment
752 is packed with code from multiple VMAs.
753 Instead we calculate the section LMA based on
754 the segment LMA. It is assumed that the
755 segment will contain sections with contiguous
756 LMAs, even if the VMAs are not. */
757 newsect
->lma
= (phdr
->p_paddr
758 + hdr
->sh_offset
- phdr
->p_offset
);
760 /* With contiguous segments, we can't tell from file
761 offsets whether a section with zero size should
762 be placed at the end of one segment or the
763 beginning of the next. Decide based on vaddr. */
764 if (hdr
->sh_addr
>= phdr
->p_vaddr
765 && (hdr
->sh_addr
+ hdr
->sh_size
766 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
773 hdr
->bfd_section
= newsect
;
774 elf_section_data (newsect
)->this_hdr
= *hdr
;
784 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
787 Helper functions for GDB to locate the string tables.
788 Since BFD hides string tables from callers, GDB needs to use an
789 internal hook to find them. Sun's .stabstr, in particular,
790 isn't even pointed to by the .stab section, so ordinary
791 mechanisms wouldn't work to find it, even if we had some.
794 struct elf_internal_shdr
*
795 bfd_elf_find_section (abfd
, name
)
799 Elf_Internal_Shdr
**i_shdrp
;
804 i_shdrp
= elf_elfsections (abfd
);
807 shstrtab
= bfd_elf_get_str_section (abfd
,
808 elf_elfheader (abfd
)->e_shstrndx
);
809 if (shstrtab
!= NULL
)
811 max
= elf_numsections (abfd
);
812 for (i
= 1; i
< max
; i
++)
813 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
820 const char *const bfd_elf_section_type_names
[] = {
821 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
822 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
823 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
826 /* ELF relocs are against symbols. If we are producing relocateable
827 output, and the reloc is against an external symbol, and nothing
828 has given us any additional addend, the resulting reloc will also
829 be against the same symbol. In such a case, we don't want to
830 change anything about the way the reloc is handled, since it will
831 all be done at final link time. Rather than put special case code
832 into bfd_perform_relocation, all the reloc types use this howto
833 function. It just short circuits the reloc if producing
834 relocateable output against an external symbol. */
836 bfd_reloc_status_type
837 bfd_elf_generic_reloc (abfd
,
844 bfd
*abfd ATTRIBUTE_UNUSED
;
845 arelent
*reloc_entry
;
847 PTR data ATTRIBUTE_UNUSED
;
848 asection
*input_section
;
850 char **error_message ATTRIBUTE_UNUSED
;
852 if (output_bfd
!= (bfd
*) NULL
853 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
854 && (! reloc_entry
->howto
->partial_inplace
855 || reloc_entry
->addend
== 0))
857 reloc_entry
->address
+= input_section
->output_offset
;
861 return bfd_reloc_continue
;
864 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
867 merge_sections_remove_hook (abfd
, sec
)
868 bfd
*abfd ATTRIBUTE_UNUSED
;
871 struct bfd_elf_section_data
*sec_data
;
873 sec_data
= elf_section_data (sec
);
874 BFD_ASSERT (sec_data
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
875 sec_data
->sec_info_type
= ELF_INFO_TYPE_NONE
;
878 /* Finish SHF_MERGE section merging. */
881 _bfd_elf_merge_sections (abfd
, info
)
883 struct bfd_link_info
*info
;
885 if (!is_elf_hash_table (info
))
887 if (elf_hash_table (info
)->merge_info
)
888 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
,
889 merge_sections_remove_hook
);
894 _bfd_elf_link_just_syms (sec
, info
)
896 struct bfd_link_info
*info
;
898 sec
->output_section
= bfd_abs_section_ptr
;
899 sec
->output_offset
= sec
->vma
;
900 if (!is_elf_hash_table (info
))
903 elf_section_data (sec
)->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
906 /* Copy the program header and other data from one object module to
910 _bfd_elf_copy_private_bfd_data (ibfd
, obfd
)
914 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
915 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
918 BFD_ASSERT (!elf_flags_init (obfd
)
919 || (elf_elfheader (obfd
)->e_flags
920 == elf_elfheader (ibfd
)->e_flags
));
922 elf_gp (obfd
) = elf_gp (ibfd
);
923 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
924 elf_flags_init (obfd
) = true;
928 /* Print out the program headers. */
931 _bfd_elf_print_private_bfd_data (abfd
, farg
)
935 FILE *f
= (FILE *) farg
;
936 Elf_Internal_Phdr
*p
;
938 bfd_byte
*dynbuf
= NULL
;
940 p
= elf_tdata (abfd
)->phdr
;
945 fprintf (f
, _("\nProgram Header:\n"));
946 c
= elf_elfheader (abfd
)->e_phnum
;
947 for (i
= 0; i
< c
; i
++, p
++)
954 case PT_NULL
: pt
= "NULL"; break;
955 case PT_LOAD
: pt
= "LOAD"; break;
956 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
957 case PT_INTERP
: pt
= "INTERP"; break;
958 case PT_NOTE
: pt
= "NOTE"; break;
959 case PT_SHLIB
: pt
= "SHLIB"; break;
960 case PT_PHDR
: pt
= "PHDR"; break;
961 case PT_TLS
: pt
= "TLS"; break;
962 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
963 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
965 fprintf (f
, "%8s off 0x", pt
);
966 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
967 fprintf (f
, " vaddr 0x");
968 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
969 fprintf (f
, " paddr 0x");
970 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
971 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
972 fprintf (f
, " filesz 0x");
973 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
974 fprintf (f
, " memsz 0x");
975 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
976 fprintf (f
, " flags %c%c%c",
977 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
978 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
979 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
980 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
981 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
986 s
= bfd_get_section_by_name (abfd
, ".dynamic");
990 unsigned long shlink
;
991 bfd_byte
*extdyn
, *extdynend
;
993 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
995 fprintf (f
, _("\nDynamic Section:\n"));
997 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1000 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1004 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1007 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1009 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1010 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1013 extdynend
= extdyn
+ s
->_raw_size
;
1014 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1016 Elf_Internal_Dyn dyn
;
1021 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1023 if (dyn
.d_tag
== DT_NULL
)
1030 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1034 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
1035 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1036 case DT_PLTGOT
: name
= "PLTGOT"; break;
1037 case DT_HASH
: name
= "HASH"; break;
1038 case DT_STRTAB
: name
= "STRTAB"; break;
1039 case DT_SYMTAB
: name
= "SYMTAB"; break;
1040 case DT_RELA
: name
= "RELA"; break;
1041 case DT_RELASZ
: name
= "RELASZ"; break;
1042 case DT_RELAENT
: name
= "RELAENT"; break;
1043 case DT_STRSZ
: name
= "STRSZ"; break;
1044 case DT_SYMENT
: name
= "SYMENT"; break;
1045 case DT_INIT
: name
= "INIT"; break;
1046 case DT_FINI
: name
= "FINI"; break;
1047 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
1048 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
1049 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1050 case DT_REL
: name
= "REL"; break;
1051 case DT_RELSZ
: name
= "RELSZ"; break;
1052 case DT_RELENT
: name
= "RELENT"; break;
1053 case DT_PLTREL
: name
= "PLTREL"; break;
1054 case DT_DEBUG
: name
= "DEBUG"; break;
1055 case DT_TEXTREL
: name
= "TEXTREL"; break;
1056 case DT_JMPREL
: name
= "JMPREL"; break;
1057 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1058 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1059 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1060 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1061 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1062 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
1063 case DT_FLAGS
: name
= "FLAGS"; break;
1064 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1065 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1066 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1067 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1068 case DT_MOVEENT
: name
= "MOVEENT"; break;
1069 case DT_MOVESZ
: name
= "MOVESZ"; break;
1070 case DT_FEATURE
: name
= "FEATURE"; break;
1071 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1072 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1073 case DT_SYMINENT
: name
= "SYMINENT"; break;
1074 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
1075 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
1076 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
1077 case DT_PLTPAD
: name
= "PLTPAD"; break;
1078 case DT_MOVETAB
: name
= "MOVETAB"; break;
1079 case DT_SYMINFO
: name
= "SYMINFO"; break;
1080 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1081 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1082 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1083 case DT_VERSYM
: name
= "VERSYM"; break;
1084 case DT_VERDEF
: name
= "VERDEF"; break;
1085 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1086 case DT_VERNEED
: name
= "VERNEED"; break;
1087 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1088 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
1089 case DT_USED
: name
= "USED"; break;
1090 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
1093 fprintf (f
, " %-11s ", name
);
1095 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1099 unsigned int tagv
= dyn
.d_un
.d_val
;
1101 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1104 fprintf (f
, "%s", string
);
1113 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1114 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1116 if (! _bfd_elf_slurp_version_tables (abfd
))
1120 if (elf_dynverdef (abfd
) != 0)
1122 Elf_Internal_Verdef
*t
;
1124 fprintf (f
, _("\nVersion definitions:\n"));
1125 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1127 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1128 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1129 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1131 Elf_Internal_Verdaux
*a
;
1134 for (a
= t
->vd_auxptr
->vda_nextptr
;
1137 fprintf (f
, "%s ", a
->vda_nodename
);
1143 if (elf_dynverref (abfd
) != 0)
1145 Elf_Internal_Verneed
*t
;
1147 fprintf (f
, _("\nVersion References:\n"));
1148 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1150 Elf_Internal_Vernaux
*a
;
1152 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1153 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1154 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1155 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1167 /* Display ELF-specific fields of a symbol. */
1170 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
1174 bfd_print_symbol_type how
;
1176 FILE *file
= (FILE *) filep
;
1179 case bfd_print_symbol_name
:
1180 fprintf (file
, "%s", symbol
->name
);
1182 case bfd_print_symbol_more
:
1183 fprintf (file
, "elf ");
1184 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1185 fprintf (file
, " %lx", (long) symbol
->flags
);
1187 case bfd_print_symbol_all
:
1189 const char *section_name
;
1190 const char *name
= NULL
;
1191 struct elf_backend_data
*bed
;
1192 unsigned char st_other
;
1195 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1197 bed
= get_elf_backend_data (abfd
);
1198 if (bed
->elf_backend_print_symbol_all
)
1199 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1203 name
= symbol
->name
;
1204 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
1207 fprintf (file
, " %s\t", section_name
);
1208 /* Print the "other" value for a symbol. For common symbols,
1209 we've already printed the size; now print the alignment.
1210 For other symbols, we have no specified alignment, and
1211 we've printed the address; now print the size. */
1212 if (bfd_is_com_section (symbol
->section
))
1213 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1215 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1216 bfd_fprintf_vma (abfd
, file
, val
);
1218 /* If we have version information, print it. */
1219 if (elf_tdata (abfd
)->dynversym_section
!= 0
1220 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1221 || elf_tdata (abfd
)->dynverref_section
!= 0))
1223 unsigned int vernum
;
1224 const char *version_string
;
1226 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1229 version_string
= "";
1230 else if (vernum
== 1)
1231 version_string
= "Base";
1232 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1234 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1237 Elf_Internal_Verneed
*t
;
1239 version_string
= "";
1240 for (t
= elf_tdata (abfd
)->verref
;
1244 Elf_Internal_Vernaux
*a
;
1246 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1248 if (a
->vna_other
== vernum
)
1250 version_string
= a
->vna_nodename
;
1257 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1258 fprintf (file
, " %-11s", version_string
);
1263 fprintf (file
, " (%s)", version_string
);
1264 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1269 /* If the st_other field is not zero, print it. */
1270 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1275 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1276 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1277 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1279 /* Some other non-defined flags are also present, so print
1281 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1284 fprintf (file
, " %s", name
);
1290 /* Create an entry in an ELF linker hash table. */
1292 struct bfd_hash_entry
*
1293 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
1294 struct bfd_hash_entry
*entry
;
1295 struct bfd_hash_table
*table
;
1298 /* Allocate the structure if it has not already been allocated by a
1302 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1307 /* Call the allocation method of the superclass. */
1308 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1311 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1312 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1314 /* Set local fields. */
1318 ret
->dynstr_index
= 0;
1319 ret
->weakdef
= NULL
;
1320 ret
->got
.refcount
= htab
->init_refcount
;
1321 ret
->plt
.refcount
= htab
->init_refcount
;
1322 ret
->linker_section_pointer
= NULL
;
1323 ret
->verinfo
.verdef
= NULL
;
1324 ret
->vtable_entries_used
= NULL
;
1325 ret
->vtable_entries_size
= 0;
1326 ret
->vtable_parent
= NULL
;
1327 ret
->type
= STT_NOTYPE
;
1329 /* Assume that we have been called by a non-ELF symbol reader.
1330 This flag is then reset by the code which reads an ELF input
1331 file. This ensures that a symbol created by a non-ELF symbol
1332 reader will have the flag set correctly. */
1333 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1339 /* Copy data from an indirect symbol to its direct symbol, hiding the
1340 old indirect symbol. Also used for copying flags to a weakdef. */
1343 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
1344 struct elf_link_hash_entry
*dir
, *ind
;
1348 /* Copy down any references that we may have already seen to the
1349 symbol which just became indirect. */
1351 dir
->elf_link_hash_flags
|=
1352 (ind
->elf_link_hash_flags
1353 & (ELF_LINK_HASH_REF_DYNAMIC
1354 | ELF_LINK_HASH_REF_REGULAR
1355 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1356 | ELF_LINK_NON_GOT_REF
));
1358 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1361 /* Copy over the global and procedure linkage table refcount entries.
1362 These may have been already set up by a check_relocs routine. */
1363 tmp
= dir
->got
.refcount
;
1366 dir
->got
.refcount
= ind
->got
.refcount
;
1367 ind
->got
.refcount
= tmp
;
1370 BFD_ASSERT (ind
->got
.refcount
<= 0);
1372 tmp
= dir
->plt
.refcount
;
1375 dir
->plt
.refcount
= ind
->plt
.refcount
;
1376 ind
->plt
.refcount
= tmp
;
1379 BFD_ASSERT (ind
->plt
.refcount
<= 0);
1381 if (dir
->dynindx
== -1)
1383 dir
->dynindx
= ind
->dynindx
;
1384 dir
->dynstr_index
= ind
->dynstr_index
;
1386 ind
->dynstr_index
= 0;
1389 BFD_ASSERT (ind
->dynindx
== -1);
1393 _bfd_elf_link_hash_hide_symbol (info
, h
, force_local
)
1394 struct bfd_link_info
*info
;
1395 struct elf_link_hash_entry
*h
;
1396 boolean force_local
;
1398 h
->plt
.offset
= (bfd_vma
) -1;
1399 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1402 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1403 if (h
->dynindx
!= -1)
1406 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
1412 /* Initialize an ELF linker hash table. */
1415 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1416 struct elf_link_hash_table
*table
;
1418 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1419 struct bfd_hash_table
*,
1424 table
->dynamic_sections_created
= false;
1425 table
->dynobj
= NULL
;
1426 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
- 1;
1427 /* The first dynamic symbol is a dummy. */
1428 table
->dynsymcount
= 1;
1429 table
->dynstr
= NULL
;
1430 table
->bucketcount
= 0;
1431 table
->needed
= NULL
;
1432 table
->runpath
= NULL
;
1434 table
->stab_info
= NULL
;
1435 table
->merge_info
= NULL
;
1436 table
->dynlocal
= NULL
;
1437 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1438 table
->root
.type
= bfd_link_elf_hash_table
;
1443 /* Create an ELF linker hash table. */
1445 struct bfd_link_hash_table
*
1446 _bfd_elf_link_hash_table_create (abfd
)
1449 struct elf_link_hash_table
*ret
;
1450 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1452 ret
= (struct elf_link_hash_table
*) bfd_malloc (amt
);
1453 if (ret
== (struct elf_link_hash_table
*) NULL
)
1456 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1465 /* This is a hook for the ELF emulation code in the generic linker to
1466 tell the backend linker what file name to use for the DT_NEEDED
1467 entry for a dynamic object. The generic linker passes name as an
1468 empty string to indicate that no DT_NEEDED entry should be made. */
1471 bfd_elf_set_dt_needed_name (abfd
, name
)
1475 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1476 && bfd_get_format (abfd
) == bfd_object
)
1477 elf_dt_name (abfd
) = name
;
1481 bfd_elf_set_dt_needed_soname (abfd
, name
)
1485 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1486 && bfd_get_format (abfd
) == bfd_object
)
1487 elf_dt_soname (abfd
) = name
;
1490 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1491 the linker ELF emulation code. */
1493 struct bfd_link_needed_list
*
1494 bfd_elf_get_needed_list (abfd
, info
)
1495 bfd
*abfd ATTRIBUTE_UNUSED
;
1496 struct bfd_link_info
*info
;
1498 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1500 return elf_hash_table (info
)->needed
;
1503 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1504 hook for the linker ELF emulation code. */
1506 struct bfd_link_needed_list
*
1507 bfd_elf_get_runpath_list (abfd
, info
)
1508 bfd
*abfd ATTRIBUTE_UNUSED
;
1509 struct bfd_link_info
*info
;
1511 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1513 return elf_hash_table (info
)->runpath
;
1516 /* Get the name actually used for a dynamic object for a link. This
1517 is the SONAME entry if there is one. Otherwise, it is the string
1518 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1521 bfd_elf_get_dt_soname (abfd
)
1524 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1525 && bfd_get_format (abfd
) == bfd_object
)
1526 return elf_dt_name (abfd
);
1530 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1531 the ELF linker emulation code. */
1534 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1536 struct bfd_link_needed_list
**pneeded
;
1539 bfd_byte
*dynbuf
= NULL
;
1541 unsigned long shlink
;
1542 bfd_byte
*extdyn
, *extdynend
;
1544 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1548 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1549 || bfd_get_format (abfd
) != bfd_object
)
1552 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1553 if (s
== NULL
|| s
->_raw_size
== 0)
1556 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1560 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1564 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1568 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1570 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1571 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1574 extdynend
= extdyn
+ s
->_raw_size
;
1575 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1577 Elf_Internal_Dyn dyn
;
1579 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1581 if (dyn
.d_tag
== DT_NULL
)
1584 if (dyn
.d_tag
== DT_NEEDED
)
1587 struct bfd_link_needed_list
*l
;
1588 unsigned int tagv
= dyn
.d_un
.d_val
;
1591 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1596 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1617 /* Allocate an ELF string table--force the first byte to be zero. */
1619 struct bfd_strtab_hash
*
1620 _bfd_elf_stringtab_init ()
1622 struct bfd_strtab_hash
*ret
;
1624 ret
= _bfd_stringtab_init ();
1629 loc
= _bfd_stringtab_add (ret
, "", true, false);
1630 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1631 if (loc
== (bfd_size_type
) -1)
1633 _bfd_stringtab_free (ret
);
1640 /* ELF .o/exec file reading */
1642 /* Create a new bfd section from an ELF section header. */
1645 bfd_section_from_shdr (abfd
, shindex
)
1647 unsigned int shindex
;
1649 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1650 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1651 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1654 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1656 switch (hdr
->sh_type
)
1659 /* Inactive section. Throw it away. */
1662 case SHT_PROGBITS
: /* Normal section with contents. */
1663 case SHT_DYNAMIC
: /* Dynamic linking information. */
1664 case SHT_NOBITS
: /* .bss section. */
1665 case SHT_HASH
: /* .hash section. */
1666 case SHT_NOTE
: /* .note section. */
1667 case SHT_INIT_ARRAY
: /* .init_array section. */
1668 case SHT_FINI_ARRAY
: /* .fini_array section. */
1669 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1670 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1672 case SHT_SYMTAB
: /* A symbol table */
1673 if (elf_onesymtab (abfd
) == shindex
)
1676 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1677 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1678 elf_onesymtab (abfd
) = shindex
;
1679 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1680 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1681 abfd
->flags
|= HAS_SYMS
;
1683 /* Sometimes a shared object will map in the symbol table. If
1684 SHF_ALLOC is set, and this is a shared object, then we also
1685 treat this section as a BFD section. We can not base the
1686 decision purely on SHF_ALLOC, because that flag is sometimes
1687 set in a relocateable object file, which would confuse the
1689 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1690 && (abfd
->flags
& DYNAMIC
) != 0
1691 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1696 case SHT_DYNSYM
: /* A dynamic symbol table */
1697 if (elf_dynsymtab (abfd
) == shindex
)
1700 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1701 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1702 elf_dynsymtab (abfd
) = shindex
;
1703 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1704 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1705 abfd
->flags
|= HAS_SYMS
;
1707 /* Besides being a symbol table, we also treat this as a regular
1708 section, so that objcopy can handle it. */
1709 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1711 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1712 if (elf_symtab_shndx (abfd
) == shindex
)
1715 /* Get the associated symbol table. */
1716 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1717 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1720 elf_symtab_shndx (abfd
) = shindex
;
1721 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1722 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1725 case SHT_STRTAB
: /* A string table */
1726 if (hdr
->bfd_section
!= NULL
)
1728 if (ehdr
->e_shstrndx
== shindex
)
1730 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1731 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1735 unsigned int i
, num_sec
;
1737 num_sec
= elf_numsections (abfd
);
1738 for (i
= 1; i
< num_sec
; i
++)
1740 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1741 if (hdr2
->sh_link
== shindex
)
1743 if (! bfd_section_from_shdr (abfd
, i
))
1745 if (elf_onesymtab (abfd
) == i
)
1747 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1748 elf_elfsections (abfd
)[shindex
] =
1749 &elf_tdata (abfd
)->strtab_hdr
;
1752 if (elf_dynsymtab (abfd
) == i
)
1754 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1755 elf_elfsections (abfd
)[shindex
] = hdr
=
1756 &elf_tdata (abfd
)->dynstrtab_hdr
;
1757 /* We also treat this as a regular section, so
1758 that objcopy can handle it. */
1761 #if 0 /* Not handling other string tables specially right now. */
1762 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1763 /* We have a strtab for some random other section. */
1764 newsect
= (asection
*) hdr2
->bfd_section
;
1767 hdr
->bfd_section
= newsect
;
1768 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1770 elf_elfsections (abfd
)[shindex
] = hdr2
;
1776 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1780 /* *These* do a lot of work -- but build no sections! */
1782 asection
*target_sect
;
1783 Elf_Internal_Shdr
*hdr2
;
1784 unsigned int num_sec
= elf_numsections (abfd
);
1786 /* Check for a bogus link to avoid crashing. */
1787 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1788 || hdr
->sh_link
>= num_sec
)
1790 ((*_bfd_error_handler
)
1791 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1792 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1793 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1796 /* For some incomprehensible reason Oracle distributes
1797 libraries for Solaris in which some of the objects have
1798 bogus sh_link fields. It would be nice if we could just
1799 reject them, but, unfortunately, some people need to use
1800 them. We scan through the section headers; if we find only
1801 one suitable symbol table, we clobber the sh_link to point
1802 to it. I hope this doesn't break anything. */
1803 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1804 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1810 for (scan
= 1; scan
< num_sec
; scan
++)
1812 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1813 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1824 hdr
->sh_link
= found
;
1827 /* Get the symbol table. */
1828 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1829 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1832 /* If this reloc section does not use the main symbol table we
1833 don't treat it as a reloc section. BFD can't adequately
1834 represent such a section, so at least for now, we don't
1835 try. We just present it as a normal section. We also
1836 can't use it as a reloc section if it points to the null
1838 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1839 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1841 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1843 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1844 if (target_sect
== NULL
)
1847 if ((target_sect
->flags
& SEC_RELOC
) == 0
1848 || target_sect
->reloc_count
== 0)
1849 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1853 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1854 amt
= sizeof (*hdr2
);
1855 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1856 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1859 elf_elfsections (abfd
)[shindex
] = hdr2
;
1860 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1861 target_sect
->flags
|= SEC_RELOC
;
1862 target_sect
->relocation
= NULL
;
1863 target_sect
->rel_filepos
= hdr
->sh_offset
;
1864 /* In the section to which the relocations apply, mark whether
1865 its relocations are of the REL or RELA variety. */
1866 if (hdr
->sh_size
!= 0)
1867 elf_section_data (target_sect
)->use_rela_p
1868 = (hdr
->sh_type
== SHT_RELA
);
1869 abfd
->flags
|= HAS_RELOC
;
1874 case SHT_GNU_verdef
:
1875 elf_dynverdef (abfd
) = shindex
;
1876 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1877 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1880 case SHT_GNU_versym
:
1881 elf_dynversym (abfd
) = shindex
;
1882 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1883 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1886 case SHT_GNU_verneed
:
1887 elf_dynverref (abfd
) = shindex
;
1888 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1889 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1896 /* We need a BFD section for objcopy and relocatable linking,
1897 and it's handy to have the signature available as the section
1899 name
= group_signature (abfd
, hdr
);
1902 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1904 if (hdr
->contents
!= NULL
)
1906 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1907 unsigned int n_elt
= hdr
->sh_size
/ 4;
1910 if (idx
->flags
& GRP_COMDAT
)
1911 hdr
->bfd_section
->flags
1912 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1914 while (--n_elt
!= 0)
1915 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
1916 && elf_next_in_group (s
) != NULL
)
1918 elf_next_in_group (hdr
->bfd_section
) = s
;
1925 /* Check for any processor-specific section types. */
1927 if (bed
->elf_backend_section_from_shdr
)
1928 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1936 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1937 Return SEC for sections that have no elf section, and NULL on error. */
1940 bfd_section_from_r_symndx (abfd
, cache
, sec
, r_symndx
)
1942 struct sym_sec_cache
*cache
;
1944 unsigned long r_symndx
;
1946 unsigned char esym_shndx
[4];
1947 unsigned int isym_shndx
;
1948 Elf_Internal_Shdr
*symtab_hdr
;
1951 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1953 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
1954 return cache
->sec
[ent
];
1956 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1957 pos
= symtab_hdr
->sh_offset
;
1958 if (get_elf_backend_data (abfd
)->s
->sizeof_sym
1959 == sizeof (Elf64_External_Sym
))
1961 pos
+= r_symndx
* sizeof (Elf64_External_Sym
);
1962 pos
+= offsetof (Elf64_External_Sym
, st_shndx
);
1963 amt
= sizeof (((Elf64_External_Sym
*) 0)->st_shndx
);
1967 pos
+= r_symndx
* sizeof (Elf32_External_Sym
);
1968 pos
+= offsetof (Elf32_External_Sym
, st_shndx
);
1969 amt
= sizeof (((Elf32_External_Sym
*) 0)->st_shndx
);
1971 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1972 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1974 isym_shndx
= H_GET_16 (abfd
, esym_shndx
);
1976 if (isym_shndx
== SHN_XINDEX
)
1978 Elf_Internal_Shdr
*shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1979 if (shndx_hdr
->sh_size
!= 0)
1981 pos
= shndx_hdr
->sh_offset
;
1982 pos
+= r_symndx
* sizeof (Elf_External_Sym_Shndx
);
1983 amt
= sizeof (Elf_External_Sym_Shndx
);
1984 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1985 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1987 isym_shndx
= H_GET_32 (abfd
, esym_shndx
);
1991 if (cache
->abfd
!= abfd
)
1993 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1996 cache
->indx
[ent
] = r_symndx
;
1997 cache
->sec
[ent
] = sec
;
1998 if (isym_shndx
< SHN_LORESERVE
|| isym_shndx
> SHN_HIRESERVE
)
2001 s
= bfd_section_from_elf_index (abfd
, isym_shndx
);
2003 cache
->sec
[ent
] = s
;
2005 return cache
->sec
[ent
];
2008 /* Given an ELF section number, retrieve the corresponding BFD
2012 bfd_section_from_elf_index (abfd
, index
)
2016 if (index
>= elf_numsections (abfd
))
2018 return elf_elfsections (abfd
)[index
]->bfd_section
;
2022 _bfd_elf_new_section_hook (abfd
, sec
)
2026 struct bfd_elf_section_data
*sdata
;
2027 bfd_size_type amt
= sizeof (*sdata
);
2029 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
2032 sec
->used_by_bfd
= (PTR
) sdata
;
2034 /* Indicate whether or not this section should use RELA relocations. */
2036 = get_elf_backend_data (abfd
)->default_use_rela_p
;
2041 /* Create a new bfd section from an ELF program header.
2043 Since program segments have no names, we generate a synthetic name
2044 of the form segment<NUM>, where NUM is generally the index in the
2045 program header table. For segments that are split (see below) we
2046 generate the names segment<NUM>a and segment<NUM>b.
2048 Note that some program segments may have a file size that is different than
2049 (less than) the memory size. All this means is that at execution the
2050 system must allocate the amount of memory specified by the memory size,
2051 but only initialize it with the first "file size" bytes read from the
2052 file. This would occur for example, with program segments consisting
2053 of combined data+bss.
2055 To handle the above situation, this routine generates TWO bfd sections
2056 for the single program segment. The first has the length specified by
2057 the file size of the segment, and the second has the length specified
2058 by the difference between the two sizes. In effect, the segment is split
2059 into it's initialized and uninitialized parts.
2064 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
2066 Elf_Internal_Phdr
*hdr
;
2068 const char *typename
;
2075 split
= ((hdr
->p_memsz
> 0)
2076 && (hdr
->p_filesz
> 0)
2077 && (hdr
->p_memsz
> hdr
->p_filesz
));
2078 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2079 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
2082 strcpy (name
, namebuf
);
2083 newsect
= bfd_make_section (abfd
, name
);
2084 if (newsect
== NULL
)
2086 newsect
->vma
= hdr
->p_vaddr
;
2087 newsect
->lma
= hdr
->p_paddr
;
2088 newsect
->_raw_size
= hdr
->p_filesz
;
2089 newsect
->filepos
= hdr
->p_offset
;
2090 newsect
->flags
|= SEC_HAS_CONTENTS
;
2091 if (hdr
->p_type
== PT_LOAD
)
2093 newsect
->flags
|= SEC_ALLOC
;
2094 newsect
->flags
|= SEC_LOAD
;
2095 if (hdr
->p_flags
& PF_X
)
2097 /* FIXME: all we known is that it has execute PERMISSION,
2099 newsect
->flags
|= SEC_CODE
;
2102 if (!(hdr
->p_flags
& PF_W
))
2104 newsect
->flags
|= SEC_READONLY
;
2109 sprintf (namebuf
, "%s%db", typename
, index
);
2110 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
2113 strcpy (name
, namebuf
);
2114 newsect
= bfd_make_section (abfd
, name
);
2115 if (newsect
== NULL
)
2117 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2118 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2119 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
2120 if (hdr
->p_type
== PT_LOAD
)
2122 newsect
->flags
|= SEC_ALLOC
;
2123 if (hdr
->p_flags
& PF_X
)
2124 newsect
->flags
|= SEC_CODE
;
2126 if (!(hdr
->p_flags
& PF_W
))
2127 newsect
->flags
|= SEC_READONLY
;
2134 bfd_section_from_phdr (abfd
, hdr
, index
)
2136 Elf_Internal_Phdr
*hdr
;
2139 struct elf_backend_data
*bed
;
2141 switch (hdr
->p_type
)
2144 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2147 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2150 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2153 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2156 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2158 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
2163 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2166 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2169 /* Check for any processor-specific program segment types.
2170 If no handler for them, default to making "segment" sections. */
2171 bed
= get_elf_backend_data (abfd
);
2172 if (bed
->elf_backend_section_from_phdr
)
2173 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2175 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2179 /* Initialize REL_HDR, the section-header for new section, containing
2180 relocations against ASECT. If USE_RELA_P is true, we use RELA
2181 relocations; otherwise, we use REL relocations. */
2184 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
2186 Elf_Internal_Shdr
*rel_hdr
;
2191 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2192 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2194 name
= bfd_alloc (abfd
, amt
);
2197 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2199 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2201 if (rel_hdr
->sh_name
== (unsigned int) -1)
2203 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2204 rel_hdr
->sh_entsize
= (use_rela_p
2205 ? bed
->s
->sizeof_rela
2206 : bed
->s
->sizeof_rel
);
2207 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
2208 rel_hdr
->sh_flags
= 0;
2209 rel_hdr
->sh_addr
= 0;
2210 rel_hdr
->sh_size
= 0;
2211 rel_hdr
->sh_offset
= 0;
2216 /* Set up an ELF internal section header for a section. */
2219 elf_fake_sections (abfd
, asect
, failedptrarg
)
2224 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2225 boolean
*failedptr
= (boolean
*) failedptrarg
;
2226 Elf_Internal_Shdr
*this_hdr
;
2230 /* We already failed; just get out of the bfd_map_over_sections
2235 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2237 this_hdr
->sh_name
= (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2238 asect
->name
, false);
2239 if (this_hdr
->sh_name
== (unsigned long) -1)
2245 this_hdr
->sh_flags
= 0;
2247 if ((asect
->flags
& SEC_ALLOC
) != 0
2248 || asect
->user_set_vma
)
2249 this_hdr
->sh_addr
= asect
->vma
;
2251 this_hdr
->sh_addr
= 0;
2253 this_hdr
->sh_offset
= 0;
2254 this_hdr
->sh_size
= asect
->_raw_size
;
2255 this_hdr
->sh_link
= 0;
2256 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2257 /* The sh_entsize and sh_info fields may have been set already by
2258 copy_private_section_data. */
2260 this_hdr
->bfd_section
= asect
;
2261 this_hdr
->contents
= NULL
;
2263 /* FIXME: This should not be based on section names. */
2264 if (strcmp (asect
->name
, ".dynstr") == 0)
2265 this_hdr
->sh_type
= SHT_STRTAB
;
2266 else if (strcmp (asect
->name
, ".hash") == 0)
2268 this_hdr
->sh_type
= SHT_HASH
;
2269 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2271 else if (strcmp (asect
->name
, ".dynsym") == 0)
2273 this_hdr
->sh_type
= SHT_DYNSYM
;
2274 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2276 else if (strcmp (asect
->name
, ".dynamic") == 0)
2278 this_hdr
->sh_type
= SHT_DYNAMIC
;
2279 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2281 else if (strncmp (asect
->name
, ".rela", 5) == 0
2282 && get_elf_backend_data (abfd
)->may_use_rela_p
)
2284 this_hdr
->sh_type
= SHT_RELA
;
2285 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2287 else if (strncmp (asect
->name
, ".rel", 4) == 0
2288 && get_elf_backend_data (abfd
)->may_use_rel_p
)
2290 this_hdr
->sh_type
= SHT_REL
;
2291 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2293 else if (strcmp (asect
->name
, ".init_array") == 0)
2294 this_hdr
->sh_type
= SHT_INIT_ARRAY
;
2295 else if (strcmp (asect
->name
, ".fini_array") == 0)
2296 this_hdr
->sh_type
= SHT_FINI_ARRAY
;
2297 else if (strcmp (asect
->name
, ".preinit_array") == 0)
2298 this_hdr
->sh_type
= SHT_PREINIT_ARRAY
;
2299 else if (strncmp (asect
->name
, ".note", 5) == 0)
2300 this_hdr
->sh_type
= SHT_NOTE
;
2301 else if (strncmp (asect
->name
, ".stab", 5) == 0
2302 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
2303 this_hdr
->sh_type
= SHT_STRTAB
;
2304 else if (strcmp (asect
->name
, ".gnu.version") == 0)
2306 this_hdr
->sh_type
= SHT_GNU_versym
;
2307 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2309 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
2311 this_hdr
->sh_type
= SHT_GNU_verdef
;
2312 this_hdr
->sh_entsize
= 0;
2313 /* objcopy or strip will copy over sh_info, but may not set
2314 cverdefs. The linker will set cverdefs, but sh_info will be
2316 if (this_hdr
->sh_info
== 0)
2317 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2319 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2320 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2322 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
2324 this_hdr
->sh_type
= SHT_GNU_verneed
;
2325 this_hdr
->sh_entsize
= 0;
2326 /* objcopy or strip will copy over sh_info, but may not set
2327 cverrefs. The linker will set cverrefs, but sh_info will be
2329 if (this_hdr
->sh_info
== 0)
2330 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2332 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2333 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2335 else if ((asect
->flags
& SEC_GROUP
) != 0)
2337 this_hdr
->sh_type
= SHT_GROUP
;
2338 this_hdr
->sh_entsize
= 4;
2340 else if ((asect
->flags
& SEC_ALLOC
) != 0
2341 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2342 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2343 this_hdr
->sh_type
= SHT_NOBITS
;
2345 this_hdr
->sh_type
= SHT_PROGBITS
;
2347 if ((asect
->flags
& SEC_ALLOC
) != 0)
2348 this_hdr
->sh_flags
|= SHF_ALLOC
;
2349 if ((asect
->flags
& SEC_READONLY
) == 0)
2350 this_hdr
->sh_flags
|= SHF_WRITE
;
2351 if ((asect
->flags
& SEC_CODE
) != 0)
2352 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2353 if ((asect
->flags
& SEC_MERGE
) != 0)
2355 this_hdr
->sh_flags
|= SHF_MERGE
;
2356 this_hdr
->sh_entsize
= asect
->entsize
;
2357 if ((asect
->flags
& SEC_STRINGS
) != 0)
2358 this_hdr
->sh_flags
|= SHF_STRINGS
;
2360 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2361 this_hdr
->sh_flags
|= SHF_GROUP
;
2362 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2363 this_hdr
->sh_flags
|= SHF_TLS
;
2365 /* Check for processor-specific section types. */
2366 if (bed
->elf_backend_fake_sections
2367 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2370 /* If the section has relocs, set up a section header for the
2371 SHT_REL[A] section. If two relocation sections are required for
2372 this section, it is up to the processor-specific back-end to
2373 create the other. */
2374 if ((asect
->flags
& SEC_RELOC
) != 0
2375 && !_bfd_elf_init_reloc_shdr (abfd
,
2376 &elf_section_data (asect
)->rel_hdr
,
2378 elf_section_data (asect
)->use_rela_p
))
2382 /* Fill in the contents of a SHT_GROUP section. */
2385 bfd_elf_set_group_contents (abfd
, sec
, failedptrarg
)
2390 boolean
*failedptr
= (boolean
*) failedptrarg
;
2391 unsigned long symindx
;
2392 asection
*elt
, *first
;
2394 struct bfd_link_order
*l
;
2397 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2402 if (elf_group_id (sec
) != NULL
)
2403 symindx
= elf_group_id (sec
)->udata
.i
;
2407 /* If called from the assembler, swap_out_syms will have set up
2408 elf_section_syms; If called for "ld -r", use target_index. */
2409 if (elf_section_syms (abfd
) != NULL
)
2410 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2412 symindx
= sec
->target_index
;
2414 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2416 /* The contents won't be allocated for "ld -r" or objcopy. */
2418 if (sec
->contents
== NULL
)
2421 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2423 /* Arrange for the section to be written out. */
2424 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2425 if (sec
->contents
== NULL
)
2432 loc
= sec
->contents
+ sec
->_raw_size
;
2434 /* Get the pointer to the first section in the group that gas
2435 squirreled away here. objcopy arranges for this to be set to the
2436 start of the input section group. */
2437 first
= elt
= elf_next_in_group (sec
);
2439 /* First element is a flag word. Rest of section is elf section
2440 indices for all the sections of the group. Write them backwards
2441 just to keep the group in the same order as given in .section
2442 directives, not that it matters. */
2451 s
= s
->output_section
;
2454 idx
= elf_section_data (s
)->this_idx
;
2455 H_PUT_32 (abfd
, idx
, loc
);
2456 elt
= elf_next_in_group (elt
);
2461 /* If this is a relocatable link, then the above did nothing because
2462 SEC is the output section. Look through the input sections
2464 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2465 if (l
->type
== bfd_indirect_link_order
2466 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2471 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2472 elt
= elf_next_in_group (elt
);
2473 /* During a relocatable link, the lists are circular. */
2475 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2477 /* With ld -r, merging SHT_GROUP sections results in wasted space
2478 due to allowing for the flag word on each input. We may well
2479 duplicate entries too. */
2480 while ((loc
-= 4) > sec
->contents
)
2481 H_PUT_32 (abfd
, 0, loc
);
2483 if (loc
!= sec
->contents
)
2486 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2489 /* Assign all ELF section numbers. The dummy first section is handled here
2490 too. The link/info pointers for the standard section types are filled
2491 in here too, while we're at it. */
2494 assign_section_numbers (abfd
)
2497 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2499 unsigned int section_number
, secn
;
2500 Elf_Internal_Shdr
**i_shdrp
;
2505 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2507 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2509 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2511 if (section_number
== SHN_LORESERVE
)
2512 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2513 d
->this_idx
= section_number
++;
2514 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2515 if ((sec
->flags
& SEC_RELOC
) == 0)
2519 if (section_number
== SHN_LORESERVE
)
2520 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2521 d
->rel_idx
= section_number
++;
2522 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2527 if (section_number
== SHN_LORESERVE
)
2528 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2529 d
->rel_idx2
= section_number
++;
2530 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2536 if (section_number
== SHN_LORESERVE
)
2537 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2538 t
->shstrtab_section
= section_number
++;
2539 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2540 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2542 if (bfd_get_symcount (abfd
) > 0)
2544 if (section_number
== SHN_LORESERVE
)
2545 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2546 t
->symtab_section
= section_number
++;
2547 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2548 if (section_number
> SHN_LORESERVE
- 2)
2550 if (section_number
== SHN_LORESERVE
)
2551 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2552 t
->symtab_shndx_section
= section_number
++;
2553 t
->symtab_shndx_hdr
.sh_name
2554 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2555 ".symtab_shndx", false);
2556 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2559 if (section_number
== SHN_LORESERVE
)
2560 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2561 t
->strtab_section
= section_number
++;
2562 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2565 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2566 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2568 elf_numsections (abfd
) = section_number
;
2569 elf_elfheader (abfd
)->e_shnum
= section_number
;
2570 if (section_number
> SHN_LORESERVE
)
2571 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2573 /* Set up the list of section header pointers, in agreement with the
2575 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2576 i_shdrp
= (Elf_Internal_Shdr
**) bfd_alloc (abfd
, amt
);
2577 if (i_shdrp
== NULL
)
2580 amt
= sizeof (Elf_Internal_Shdr
);
2581 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2582 if (i_shdrp
[0] == NULL
)
2584 bfd_release (abfd
, i_shdrp
);
2587 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
2589 elf_elfsections (abfd
) = i_shdrp
;
2591 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2592 if (bfd_get_symcount (abfd
) > 0)
2594 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2595 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2597 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2598 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2600 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2601 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2603 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2605 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2609 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2610 if (d
->rel_idx
!= 0)
2611 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2612 if (d
->rel_idx2
!= 0)
2613 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2615 /* Fill in the sh_link and sh_info fields while we're at it. */
2617 /* sh_link of a reloc section is the section index of the symbol
2618 table. sh_info is the section index of the section to which
2619 the relocation entries apply. */
2620 if (d
->rel_idx
!= 0)
2622 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2623 d
->rel_hdr
.sh_info
= d
->this_idx
;
2625 if (d
->rel_idx2
!= 0)
2627 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2628 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2631 switch (d
->this_hdr
.sh_type
)
2635 /* A reloc section which we are treating as a normal BFD
2636 section. sh_link is the section index of the symbol
2637 table. sh_info is the section index of the section to
2638 which the relocation entries apply. We assume that an
2639 allocated reloc section uses the dynamic symbol table.
2640 FIXME: How can we be sure? */
2641 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2643 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2645 /* We look up the section the relocs apply to by name. */
2647 if (d
->this_hdr
.sh_type
== SHT_REL
)
2651 s
= bfd_get_section_by_name (abfd
, name
);
2653 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2657 /* We assume that a section named .stab*str is a stabs
2658 string section. We look for a section with the same name
2659 but without the trailing ``str'', and set its sh_link
2660 field to point to this section. */
2661 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2662 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2667 len
= strlen (sec
->name
);
2668 alc
= (char *) bfd_malloc ((bfd_size_type
) len
- 2);
2671 strncpy (alc
, sec
->name
, len
- 3);
2672 alc
[len
- 3] = '\0';
2673 s
= bfd_get_section_by_name (abfd
, alc
);
2677 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2679 /* This is a .stab section. */
2680 elf_section_data (s
)->this_hdr
.sh_entsize
=
2681 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2688 case SHT_GNU_verneed
:
2689 case SHT_GNU_verdef
:
2690 /* sh_link is the section header index of the string table
2691 used for the dynamic entries, or the symbol table, or the
2693 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2695 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2699 case SHT_GNU_versym
:
2700 /* sh_link is the section header index of the symbol table
2701 this hash table or version table is for. */
2702 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2704 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2708 d
->this_hdr
.sh_link
= t
->symtab_section
;
2712 for (secn
= 1; secn
< section_number
; ++secn
)
2713 if (i_shdrp
[secn
] == NULL
)
2714 i_shdrp
[secn
] = i_shdrp
[0];
2716 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2717 i_shdrp
[secn
]->sh_name
);
2721 /* Map symbol from it's internal number to the external number, moving
2722 all local symbols to be at the head of the list. */
2725 sym_is_global (abfd
, sym
)
2729 /* If the backend has a special mapping, use it. */
2730 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2731 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2734 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2735 || bfd_is_und_section (bfd_get_section (sym
))
2736 || bfd_is_com_section (bfd_get_section (sym
)));
2740 elf_map_symbols (abfd
)
2743 unsigned int symcount
= bfd_get_symcount (abfd
);
2744 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2745 asymbol
**sect_syms
;
2746 unsigned int num_locals
= 0;
2747 unsigned int num_globals
= 0;
2748 unsigned int num_locals2
= 0;
2749 unsigned int num_globals2
= 0;
2757 fprintf (stderr
, "elf_map_symbols\n");
2761 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2763 if (max_index
< asect
->index
)
2764 max_index
= asect
->index
;
2768 amt
= max_index
* sizeof (asymbol
*);
2769 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2770 if (sect_syms
== NULL
)
2772 elf_section_syms (abfd
) = sect_syms
;
2773 elf_num_section_syms (abfd
) = max_index
;
2775 /* Init sect_syms entries for any section symbols we have already
2776 decided to output. */
2777 for (idx
= 0; idx
< symcount
; idx
++)
2779 asymbol
*sym
= syms
[idx
];
2781 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2788 if (sec
->owner
!= NULL
)
2790 if (sec
->owner
!= abfd
)
2792 if (sec
->output_offset
!= 0)
2795 sec
= sec
->output_section
;
2797 /* Empty sections in the input files may have had a
2798 section symbol created for them. (See the comment
2799 near the end of _bfd_generic_link_output_symbols in
2800 linker.c). If the linker script discards such
2801 sections then we will reach this point. Since we know
2802 that we cannot avoid this case, we detect it and skip
2803 the abort and the assignment to the sect_syms array.
2804 To reproduce this particular case try running the
2805 linker testsuite test ld-scripts/weak.exp for an ELF
2806 port that uses the generic linker. */
2807 if (sec
->owner
== NULL
)
2810 BFD_ASSERT (sec
->owner
== abfd
);
2812 sect_syms
[sec
->index
] = syms
[idx
];
2817 /* Classify all of the symbols. */
2818 for (idx
= 0; idx
< symcount
; idx
++)
2820 if (!sym_is_global (abfd
, syms
[idx
]))
2826 /* We will be adding a section symbol for each BFD section. Most normal
2827 sections will already have a section symbol in outsymbols, but
2828 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2829 at least in that case. */
2830 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2832 if (sect_syms
[asect
->index
] == NULL
)
2834 if (!sym_is_global (abfd
, asect
->symbol
))
2841 /* Now sort the symbols so the local symbols are first. */
2842 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2843 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2845 if (new_syms
== NULL
)
2848 for (idx
= 0; idx
< symcount
; idx
++)
2850 asymbol
*sym
= syms
[idx
];
2853 if (!sym_is_global (abfd
, sym
))
2856 i
= num_locals
+ num_globals2
++;
2858 sym
->udata
.i
= i
+ 1;
2860 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2862 if (sect_syms
[asect
->index
] == NULL
)
2864 asymbol
*sym
= asect
->symbol
;
2867 sect_syms
[asect
->index
] = sym
;
2868 if (!sym_is_global (abfd
, sym
))
2871 i
= num_locals
+ num_globals2
++;
2873 sym
->udata
.i
= i
+ 1;
2877 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2879 elf_num_locals (abfd
) = num_locals
;
2880 elf_num_globals (abfd
) = num_globals
;
2884 /* Align to the maximum file alignment that could be required for any
2885 ELF data structure. */
2887 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2888 static INLINE file_ptr
2889 align_file_position (off
, align
)
2893 return (off
+ align
- 1) & ~(align
- 1);
2896 /* Assign a file position to a section, optionally aligning to the
2897 required section alignment. */
2900 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2901 Elf_Internal_Shdr
*i_shdrp
;
2909 al
= i_shdrp
->sh_addralign
;
2911 offset
= BFD_ALIGN (offset
, al
);
2913 i_shdrp
->sh_offset
= offset
;
2914 if (i_shdrp
->bfd_section
!= NULL
)
2915 i_shdrp
->bfd_section
->filepos
= offset
;
2916 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2917 offset
+= i_shdrp
->sh_size
;
2921 /* Compute the file positions we are going to put the sections at, and
2922 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2923 is not NULL, this is being called by the ELF backend linker. */
2926 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2928 struct bfd_link_info
*link_info
;
2930 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2932 struct bfd_strtab_hash
*strtab
;
2933 Elf_Internal_Shdr
*shstrtab_hdr
;
2935 if (abfd
->output_has_begun
)
2938 /* Do any elf backend specific processing first. */
2939 if (bed
->elf_backend_begin_write_processing
)
2940 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2942 if (! prep_headers (abfd
))
2945 /* Post process the headers if necessary. */
2946 if (bed
->elf_backend_post_process_headers
)
2947 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2950 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2954 if (!assign_section_numbers (abfd
))
2957 /* The backend linker builds symbol table information itself. */
2958 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2960 /* Non-zero if doing a relocatable link. */
2961 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2963 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2967 if (link_info
== NULL
)
2969 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
2974 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2975 /* sh_name was set in prep_headers. */
2976 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2977 shstrtab_hdr
->sh_flags
= 0;
2978 shstrtab_hdr
->sh_addr
= 0;
2979 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2980 shstrtab_hdr
->sh_entsize
= 0;
2981 shstrtab_hdr
->sh_link
= 0;
2982 shstrtab_hdr
->sh_info
= 0;
2983 /* sh_offset is set in assign_file_positions_except_relocs. */
2984 shstrtab_hdr
->sh_addralign
= 1;
2986 if (!assign_file_positions_except_relocs (abfd
))
2989 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2992 Elf_Internal_Shdr
*hdr
;
2994 off
= elf_tdata (abfd
)->next_file_pos
;
2996 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2997 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2999 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3000 if (hdr
->sh_size
!= 0)
3001 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3003 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3004 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3006 elf_tdata (abfd
)->next_file_pos
= off
;
3008 /* Now that we know where the .strtab section goes, write it
3010 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3011 || ! _bfd_stringtab_emit (abfd
, strtab
))
3013 _bfd_stringtab_free (strtab
);
3016 abfd
->output_has_begun
= true;
3021 /* Create a mapping from a set of sections to a program segment. */
3023 static INLINE
struct elf_segment_map
*
3024 make_mapping (abfd
, sections
, from
, to
, phdr
)
3026 asection
**sections
;
3031 struct elf_segment_map
*m
;
3036 amt
= sizeof (struct elf_segment_map
);
3037 amt
+= (to
- from
- 1) * sizeof (asection
*);
3038 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3042 m
->p_type
= PT_LOAD
;
3043 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3044 m
->sections
[i
- from
] = *hdrpp
;
3045 m
->count
= to
- from
;
3047 if (from
== 0 && phdr
)
3049 /* Include the headers in the first PT_LOAD segment. */
3050 m
->includes_filehdr
= 1;
3051 m
->includes_phdrs
= 1;
3057 /* Set up a mapping from BFD sections to program segments. */
3060 map_sections_to_segments (abfd
)
3063 asection
**sections
= NULL
;
3067 struct elf_segment_map
*mfirst
;
3068 struct elf_segment_map
**pm
;
3069 struct elf_segment_map
*m
;
3071 unsigned int phdr_index
;
3072 bfd_vma maxpagesize
;
3074 boolean phdr_in_segment
= true;
3077 asection
*first_tls
= NULL
;
3078 asection
*dynsec
, *eh_frame_hdr
;
3081 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3084 if (bfd_count_sections (abfd
) == 0)
3087 /* Select the allocated sections, and sort them. */
3089 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
3090 sections
= (asection
**) bfd_malloc (amt
);
3091 if (sections
== NULL
)
3095 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3097 if ((s
->flags
& SEC_ALLOC
) != 0)
3103 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3106 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3108 /* Build the mapping. */
3113 /* If we have a .interp section, then create a PT_PHDR segment for
3114 the program headers and a PT_INTERP segment for the .interp
3116 s
= bfd_get_section_by_name (abfd
, ".interp");
3117 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3119 amt
= sizeof (struct elf_segment_map
);
3120 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3124 m
->p_type
= PT_PHDR
;
3125 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3126 m
->p_flags
= PF_R
| PF_X
;
3127 m
->p_flags_valid
= 1;
3128 m
->includes_phdrs
= 1;
3133 amt
= sizeof (struct elf_segment_map
);
3134 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3138 m
->p_type
= PT_INTERP
;
3146 /* Look through the sections. We put sections in the same program
3147 segment when the start of the second section can be placed within
3148 a few bytes of the end of the first section. */
3151 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
3153 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3155 && (dynsec
->flags
& SEC_LOAD
) == 0)
3158 /* Deal with -Ttext or something similar such that the first section
3159 is not adjacent to the program headers. This is an
3160 approximation, since at this point we don't know exactly how many
3161 program headers we will need. */
3164 bfd_size_type phdr_size
;
3166 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3168 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3169 if ((abfd
->flags
& D_PAGED
) == 0
3170 || sections
[0]->lma
< phdr_size
3171 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3172 phdr_in_segment
= false;
3175 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3178 boolean new_segment
;
3182 /* See if this section and the last one will fit in the same
3185 if (last_hdr
== NULL
)
3187 /* If we don't have a segment yet, then we don't need a new
3188 one (we build the last one after this loop). */
3189 new_segment
= false;
3191 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3193 /* If this section has a different relation between the
3194 virtual address and the load address, then we need a new
3198 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3199 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3201 /* If putting this section in this segment would force us to
3202 skip a page in the segment, then we need a new segment. */
3205 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
3206 && (hdr
->flags
& SEC_LOAD
) != 0)
3208 /* We don't want to put a loadable section after a
3209 nonloadable section in the same segment. */
3212 else if ((abfd
->flags
& D_PAGED
) == 0)
3214 /* If the file is not demand paged, which means that we
3215 don't require the sections to be correctly aligned in the
3216 file, then there is no other reason for a new segment. */
3217 new_segment
= false;
3220 && (hdr
->flags
& SEC_READONLY
) == 0
3221 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3224 /* We don't want to put a writable section in a read only
3225 segment, unless they are on the same page in memory
3226 anyhow. We already know that the last section does not
3227 bring us past the current section on the page, so the
3228 only case in which the new section is not on the same
3229 page as the previous section is when the previous section
3230 ends precisely on a page boundary. */
3235 /* Otherwise, we can use the same segment. */
3236 new_segment
= false;
3241 if ((hdr
->flags
& SEC_READONLY
) == 0)
3247 /* We need a new program segment. We must create a new program
3248 header holding all the sections from phdr_index until hdr. */
3250 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3257 if ((hdr
->flags
& SEC_READONLY
) == 0)
3264 phdr_in_segment
= false;
3267 /* Create a final PT_LOAD program segment. */
3268 if (last_hdr
!= NULL
)
3270 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3278 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3281 amt
= sizeof (struct elf_segment_map
);
3282 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3286 m
->p_type
= PT_DYNAMIC
;
3288 m
->sections
[0] = dynsec
;
3294 /* For each loadable .note section, add a PT_NOTE segment. We don't
3295 use bfd_get_section_by_name, because if we link together
3296 nonloadable .note sections and loadable .note sections, we will
3297 generate two .note sections in the output file. FIXME: Using
3298 names for section types is bogus anyhow. */
3299 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3301 if ((s
->flags
& SEC_LOAD
) != 0
3302 && strncmp (s
->name
, ".note", 5) == 0)
3304 amt
= sizeof (struct elf_segment_map
);
3305 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3309 m
->p_type
= PT_NOTE
;
3316 if (s
->flags
& SEC_THREAD_LOCAL
)
3324 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3329 amt
= sizeof (struct elf_segment_map
);
3330 amt
+= (tls_count
- 1) * sizeof (asection
*);
3331 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3336 m
->count
= tls_count
;
3337 /* Mandated PF_R. */
3339 m
->p_flags_valid
= 1;
3340 for (i
= 0; i
< tls_count
; ++i
)
3342 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3343 m
->sections
[i
] = first_tls
;
3344 first_tls
= first_tls
->next
;
3351 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3353 eh_frame_hdr
= NULL
;
3354 if (elf_tdata (abfd
)->eh_frame_hdr
)
3355 eh_frame_hdr
= bfd_get_section_by_name (abfd
, ".eh_frame_hdr");
3356 if (eh_frame_hdr
!= NULL
&& (eh_frame_hdr
->flags
& SEC_LOAD
))
3358 amt
= sizeof (struct elf_segment_map
);
3359 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3363 m
->p_type
= PT_GNU_EH_FRAME
;
3365 m
->sections
[0] = eh_frame_hdr
;
3374 elf_tdata (abfd
)->segment_map
= mfirst
;
3378 if (sections
!= NULL
)
3383 /* Sort sections by address. */
3386 elf_sort_sections (arg1
, arg2
)
3390 const asection
*sec1
= *(const asection
**) arg1
;
3391 const asection
*sec2
= *(const asection
**) arg2
;
3393 /* Sort by LMA first, since this is the address used to
3394 place the section into a segment. */
3395 if (sec1
->lma
< sec2
->lma
)
3397 else if (sec1
->lma
> sec2
->lma
)
3400 /* Then sort by VMA. Normally the LMA and the VMA will be
3401 the same, and this will do nothing. */
3402 if (sec1
->vma
< sec2
->vma
)
3404 else if (sec1
->vma
> sec2
->vma
)
3407 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3409 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3415 /* If the indicies are the same, do not return 0
3416 here, but continue to try the next comparison. */
3417 if (sec1
->target_index
- sec2
->target_index
!= 0)
3418 return sec1
->target_index
- sec2
->target_index
;
3423 else if (TOEND (sec2
))
3428 /* Sort by size, to put zero sized sections
3429 before others at the same address. */
3431 if (sec1
->_raw_size
< sec2
->_raw_size
)
3433 if (sec1
->_raw_size
> sec2
->_raw_size
)
3436 return sec1
->target_index
- sec2
->target_index
;
3439 /* Assign file positions to the sections based on the mapping from
3440 sections to segments. This function also sets up some fields in
3441 the file header, and writes out the program headers. */
3444 assign_file_positions_for_segments (abfd
)
3447 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3449 struct elf_segment_map
*m
;
3451 Elf_Internal_Phdr
*phdrs
;
3453 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3454 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3455 Elf_Internal_Phdr
*p
;
3458 if (elf_tdata (abfd
)->segment_map
== NULL
)
3460 if (! map_sections_to_segments (abfd
))
3465 /* The placement algorithm assumes that non allocated sections are
3466 not in PT_LOAD segments. We ensure this here by removing such
3467 sections from the segment map. */
3468 for (m
= elf_tdata (abfd
)->segment_map
;
3472 unsigned int new_count
;
3475 if (m
->p_type
!= PT_LOAD
)
3479 for (i
= 0; i
< m
->count
; i
++)
3481 if ((m
->sections
[i
]->flags
& SEC_ALLOC
) != 0)
3484 m
->sections
[new_count
] = m
->sections
[i
];
3490 if (new_count
!= m
->count
)
3491 m
->count
= new_count
;
3495 if (bed
->elf_backend_modify_segment_map
)
3497 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
3502 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3505 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3506 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3507 elf_elfheader (abfd
)->e_phnum
= count
;
3512 /* If we already counted the number of program segments, make sure
3513 that we allocated enough space. This happens when SIZEOF_HEADERS
3514 is used in a linker script. */
3515 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3516 if (alloc
!= 0 && count
> alloc
)
3518 ((*_bfd_error_handler
)
3519 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3520 bfd_get_filename (abfd
), alloc
, count
));
3521 bfd_set_error (bfd_error_bad_value
);
3528 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3529 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
3533 off
= bed
->s
->sizeof_ehdr
;
3534 off
+= alloc
* bed
->s
->sizeof_phdr
;
3541 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3548 /* If elf_segment_map is not from map_sections_to_segments, the
3549 sections may not be correctly ordered. NOTE: sorting should
3550 not be done to the PT_NOTE section of a corefile, which may
3551 contain several pseudo-sections artificially created by bfd.
3552 Sorting these pseudo-sections breaks things badly. */
3554 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3555 && m
->p_type
== PT_NOTE
))
3556 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3559 p
->p_type
= m
->p_type
;
3560 p
->p_flags
= m
->p_flags
;
3562 if (p
->p_type
== PT_LOAD
3564 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3566 if ((abfd
->flags
& D_PAGED
) != 0)
3567 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
3570 bfd_size_type align
;
3573 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3575 bfd_size_type secalign
;
3577 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3578 if (secalign
> align
)
3582 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
3589 p
->p_vaddr
= m
->sections
[0]->vma
;
3591 if (m
->p_paddr_valid
)
3592 p
->p_paddr
= m
->p_paddr
;
3593 else if (m
->count
== 0)
3596 p
->p_paddr
= m
->sections
[0]->lma
;
3598 if (p
->p_type
== PT_LOAD
3599 && (abfd
->flags
& D_PAGED
) != 0)
3600 p
->p_align
= bed
->maxpagesize
;
3601 else if (m
->count
== 0)
3602 p
->p_align
= bed
->s
->file_align
;
3610 if (m
->includes_filehdr
)
3612 if (! m
->p_flags_valid
)
3615 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3616 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3619 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3621 if (p
->p_vaddr
< (bfd_vma
) off
)
3623 (*_bfd_error_handler
)
3624 (_("%s: Not enough room for program headers, try linking with -N"),
3625 bfd_get_filename (abfd
));
3626 bfd_set_error (bfd_error_bad_value
);
3631 if (! m
->p_paddr_valid
)
3634 if (p
->p_type
== PT_LOAD
)
3636 filehdr_vaddr
= p
->p_vaddr
;
3637 filehdr_paddr
= p
->p_paddr
;
3641 if (m
->includes_phdrs
)
3643 if (! m
->p_flags_valid
)
3646 if (m
->includes_filehdr
)
3648 if (p
->p_type
== PT_LOAD
)
3650 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3651 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3656 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3660 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3661 p
->p_vaddr
-= off
- p
->p_offset
;
3662 if (! m
->p_paddr_valid
)
3663 p
->p_paddr
-= off
- p
->p_offset
;
3666 if (p
->p_type
== PT_LOAD
)
3668 phdrs_vaddr
= p
->p_vaddr
;
3669 phdrs_paddr
= p
->p_paddr
;
3672 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3675 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3676 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3679 if (p
->p_type
== PT_LOAD
3680 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3682 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3688 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3689 p
->p_filesz
+= adjust
;
3690 p
->p_memsz
+= adjust
;
3696 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3700 bfd_size_type align
;
3704 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3706 /* The section may have artificial alignment forced by a
3707 link script. Notice this case by the gap between the
3708 cumulative phdr lma and the section's lma. */
3709 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3711 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3713 p
->p_memsz
+= adjust
;
3716 if ((flags
& SEC_LOAD
) != 0)
3717 p
->p_filesz
+= adjust
;
3720 if (p
->p_type
== PT_LOAD
)
3722 bfd_signed_vma adjust
;
3724 if ((flags
& SEC_LOAD
) != 0)
3726 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3730 else if ((flags
& SEC_ALLOC
) != 0)
3732 /* The section VMA must equal the file position
3733 modulo the page size. FIXME: I'm not sure if
3734 this adjustment is really necessary. We used to
3735 not have the SEC_LOAD case just above, and then
3736 this was necessary, but now I'm not sure. */
3737 if ((abfd
->flags
& D_PAGED
) != 0)
3738 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3740 adjust
= (sec
->vma
- voff
) % align
;
3749 (* _bfd_error_handler
) (_("\
3750 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3751 bfd_section_name (abfd
, sec
),
3756 p
->p_memsz
+= adjust
;
3759 if ((flags
& SEC_LOAD
) != 0)
3760 p
->p_filesz
+= adjust
;
3765 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3766 used in a linker script we may have a section with
3767 SEC_LOAD clear but which is supposed to have
3769 if ((flags
& SEC_LOAD
) != 0
3770 || (flags
& SEC_HAS_CONTENTS
) != 0)
3771 off
+= sec
->_raw_size
;
3773 if ((flags
& SEC_ALLOC
) != 0)
3774 voff
+= sec
->_raw_size
;
3777 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3779 /* The actual "note" segment has i == 0.
3780 This is the one that actually contains everything. */
3784 p
->p_filesz
= sec
->_raw_size
;
3785 off
+= sec
->_raw_size
;
3790 /* Fake sections -- don't need to be written. */
3793 flags
= sec
->flags
= 0;
3800 p
->p_memsz
+= sec
->_raw_size
;
3802 if ((flags
& SEC_LOAD
) != 0)
3803 p
->p_filesz
+= sec
->_raw_size
;
3805 if (p
->p_type
== PT_TLS
3806 && sec
->_raw_size
== 0
3807 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
3809 struct bfd_link_order
*o
;
3810 bfd_vma tbss_size
= 0;
3812 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
3813 if (tbss_size
< o
->offset
+ o
->size
)
3814 tbss_size
= o
->offset
+ o
->size
;
3816 p
->p_memsz
+= tbss_size
;
3819 if (align
> p
->p_align
3820 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3824 if (! m
->p_flags_valid
)
3827 if ((flags
& SEC_CODE
) != 0)
3829 if ((flags
& SEC_READONLY
) == 0)
3835 /* Now that we have set the section file positions, we can set up
3836 the file positions for the non PT_LOAD segments. */
3837 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3841 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3843 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3844 p
->p_offset
= m
->sections
[0]->filepos
;
3848 if (m
->includes_filehdr
)
3850 p
->p_vaddr
= filehdr_vaddr
;
3851 if (! m
->p_paddr_valid
)
3852 p
->p_paddr
= filehdr_paddr
;
3854 else if (m
->includes_phdrs
)
3856 p
->p_vaddr
= phdrs_vaddr
;
3857 if (! m
->p_paddr_valid
)
3858 p
->p_paddr
= phdrs_paddr
;
3863 /* If additional nonloadable filepos adjustments are required,
3865 if (bed
->set_nonloadable_filepos
)
3866 (*bed
->set_nonloadable_filepos
) (abfd
, phdrs
);
3868 /* Clear out any program headers we allocated but did not use. */
3869 for (; count
< alloc
; count
++, p
++)
3871 memset (p
, 0, sizeof *p
);
3872 p
->p_type
= PT_NULL
;
3875 elf_tdata (abfd
)->phdr
= phdrs
;
3877 elf_tdata (abfd
)->next_file_pos
= off
;
3879 /* Write out the program headers. */
3880 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3881 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3887 /* Get the size of the program header.
3889 If this is called by the linker before any of the section VMA's are set, it
3890 can't calculate the correct value for a strange memory layout. This only
3891 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3892 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3893 data segment (exclusive of .interp and .dynamic).
3895 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3896 will be two segments. */
3898 static bfd_size_type
3899 get_program_header_size (abfd
)
3904 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3906 /* We can't return a different result each time we're called. */
3907 if (elf_tdata (abfd
)->program_header_size
!= 0)
3908 return elf_tdata (abfd
)->program_header_size
;
3910 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3912 struct elf_segment_map
*m
;
3915 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3917 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3918 return elf_tdata (abfd
)->program_header_size
;
3921 /* Assume we will need exactly two PT_LOAD segments: one for text
3922 and one for data. */
3925 s
= bfd_get_section_by_name (abfd
, ".interp");
3926 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3928 /* If we have a loadable interpreter section, we need a
3929 PT_INTERP segment. In this case, assume we also need a
3930 PT_PHDR segment, although that may not be true for all
3935 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3937 /* We need a PT_DYNAMIC segment. */
3941 if (elf_tdata (abfd
)->eh_frame_hdr
3942 && bfd_get_section_by_name (abfd
, ".eh_frame_hdr") != NULL
)
3944 /* We need a PT_GNU_EH_FRAME segment. */
3948 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3950 if ((s
->flags
& SEC_LOAD
) != 0
3951 && strncmp (s
->name
, ".note", 5) == 0)
3953 /* We need a PT_NOTE segment. */
3958 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3960 if (s
->flags
& SEC_THREAD_LOCAL
)
3962 /* We need a PT_TLS segment. */
3968 /* Let the backend count up any program headers it might need. */
3969 if (bed
->elf_backend_additional_program_headers
)
3973 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3979 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3980 return elf_tdata (abfd
)->program_header_size
;
3983 /* Work out the file positions of all the sections. This is called by
3984 _bfd_elf_compute_section_file_positions. All the section sizes and
3985 VMAs must be known before this is called.
3987 We do not consider reloc sections at this point, unless they form
3988 part of the loadable image. Reloc sections are assigned file
3989 positions in assign_file_positions_for_relocs, which is called by
3990 write_object_contents and final_link.
3992 We also don't set the positions of the .symtab and .strtab here. */
3995 assign_file_positions_except_relocs (abfd
)
3998 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3999 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
4000 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4001 unsigned int num_sec
= elf_numsections (abfd
);
4003 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4005 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4006 && bfd_get_format (abfd
) != bfd_core
)
4008 Elf_Internal_Shdr
**hdrpp
;
4011 /* Start after the ELF header. */
4012 off
= i_ehdrp
->e_ehsize
;
4014 /* We are not creating an executable, which means that we are
4015 not creating a program header, and that the actual order of
4016 the sections in the file is unimportant. */
4017 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4019 Elf_Internal_Shdr
*hdr
;
4022 if (hdr
->sh_type
== SHT_REL
4023 || hdr
->sh_type
== SHT_RELA
4024 || i
== tdata
->symtab_section
4025 || i
== tdata
->symtab_shndx_section
4026 || i
== tdata
->strtab_section
)
4028 hdr
->sh_offset
= -1;
4031 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4033 if (i
== SHN_LORESERVE
- 1)
4035 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4036 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4043 Elf_Internal_Shdr
**hdrpp
;
4045 /* Assign file positions for the loaded sections based on the
4046 assignment of sections to segments. */
4047 if (! assign_file_positions_for_segments (abfd
))
4050 /* Assign file positions for the other sections. */
4052 off
= elf_tdata (abfd
)->next_file_pos
;
4053 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4055 Elf_Internal_Shdr
*hdr
;
4058 if (hdr
->bfd_section
!= NULL
4059 && hdr
->bfd_section
->filepos
!= 0)
4060 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
4061 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4063 ((*_bfd_error_handler
)
4064 (_("%s: warning: allocated section `%s' not in segment"),
4065 bfd_get_filename (abfd
),
4066 (hdr
->bfd_section
== NULL
4068 : hdr
->bfd_section
->name
)));
4069 if ((abfd
->flags
& D_PAGED
) != 0)
4070 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
4072 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
4073 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4076 else if (hdr
->sh_type
== SHT_REL
4077 || hdr
->sh_type
== SHT_RELA
4078 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4079 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4080 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4081 hdr
->sh_offset
= -1;
4083 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4085 if (i
== SHN_LORESERVE
- 1)
4087 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4088 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4093 /* Place the section headers. */
4094 off
= align_file_position (off
, bed
->s
->file_align
);
4095 i_ehdrp
->e_shoff
= off
;
4096 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4098 elf_tdata (abfd
)->next_file_pos
= off
;
4107 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4108 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4109 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4110 struct elf_strtab_hash
*shstrtab
;
4111 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4113 i_ehdrp
= elf_elfheader (abfd
);
4114 i_shdrp
= elf_elfsections (abfd
);
4116 shstrtab
= _bfd_elf_strtab_init ();
4117 if (shstrtab
== NULL
)
4120 elf_shstrtab (abfd
) = shstrtab
;
4122 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4123 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4124 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4125 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4127 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4128 i_ehdrp
->e_ident
[EI_DATA
] =
4129 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4130 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4132 if ((abfd
->flags
& DYNAMIC
) != 0)
4133 i_ehdrp
->e_type
= ET_DYN
;
4134 else if ((abfd
->flags
& EXEC_P
) != 0)
4135 i_ehdrp
->e_type
= ET_EXEC
;
4136 else if (bfd_get_format (abfd
) == bfd_core
)
4137 i_ehdrp
->e_type
= ET_CORE
;
4139 i_ehdrp
->e_type
= ET_REL
;
4141 switch (bfd_get_arch (abfd
))
4143 case bfd_arch_unknown
:
4144 i_ehdrp
->e_machine
= EM_NONE
;
4147 /* There used to be a long list of cases here, each one setting
4148 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4149 in the corresponding bfd definition. To avoid duplication,
4150 the switch was removed. Machines that need special handling
4151 can generally do it in elf_backend_final_write_processing(),
4152 unless they need the information earlier than the final write.
4153 Such need can generally be supplied by replacing the tests for
4154 e_machine with the conditions used to determine it. */
4156 if (get_elf_backend_data (abfd
) != NULL
)
4157 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
4159 i_ehdrp
->e_machine
= EM_NONE
;
4162 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4163 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4165 /* No program header, for now. */
4166 i_ehdrp
->e_phoff
= 0;
4167 i_ehdrp
->e_phentsize
= 0;
4168 i_ehdrp
->e_phnum
= 0;
4170 /* Each bfd section is section header entry. */
4171 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4172 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4174 /* If we're building an executable, we'll need a program header table. */
4175 if (abfd
->flags
& EXEC_P
)
4177 /* It all happens later. */
4179 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
4181 /* elf_build_phdrs() returns a (NULL-terminated) array of
4182 Elf_Internal_Phdrs. */
4183 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
4184 i_ehdrp
->e_phoff
= outbase
;
4185 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
4190 i_ehdrp
->e_phentsize
= 0;
4192 i_ehdrp
->e_phoff
= 0;
4195 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4196 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
4197 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4198 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
4199 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4200 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
4201 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4202 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4203 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4209 /* Assign file positions for all the reloc sections which are not part
4210 of the loadable file image. */
4213 _bfd_elf_assign_file_positions_for_relocs (abfd
)
4217 unsigned int i
, num_sec
;
4218 Elf_Internal_Shdr
**shdrpp
;
4220 off
= elf_tdata (abfd
)->next_file_pos
;
4222 num_sec
= elf_numsections (abfd
);
4223 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4225 Elf_Internal_Shdr
*shdrp
;
4228 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4229 && shdrp
->sh_offset
== -1)
4230 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
4233 elf_tdata (abfd
)->next_file_pos
= off
;
4237 _bfd_elf_write_object_contents (abfd
)
4240 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4241 Elf_Internal_Ehdr
*i_ehdrp
;
4242 Elf_Internal_Shdr
**i_shdrp
;
4244 unsigned int count
, num_sec
;
4246 if (! abfd
->output_has_begun
4247 && ! _bfd_elf_compute_section_file_positions
4248 (abfd
, (struct bfd_link_info
*) NULL
))
4251 i_shdrp
= elf_elfsections (abfd
);
4252 i_ehdrp
= elf_elfheader (abfd
);
4255 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4259 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4261 /* After writing the headers, we need to write the sections too... */
4262 num_sec
= elf_numsections (abfd
);
4263 for (count
= 1; count
< num_sec
; count
++)
4265 if (bed
->elf_backend_section_processing
)
4266 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4267 if (i_shdrp
[count
]->contents
)
4269 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4271 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4272 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4275 if (count
== SHN_LORESERVE
- 1)
4276 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4279 /* Write out the section header names. */
4280 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4281 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4284 if (bed
->elf_backend_final_write_processing
)
4285 (*bed
->elf_backend_final_write_processing
) (abfd
,
4286 elf_tdata (abfd
)->linker
);
4288 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4292 _bfd_elf_write_corefile_contents (abfd
)
4295 /* Hopefully this can be done just like an object file. */
4296 return _bfd_elf_write_object_contents (abfd
);
4299 /* Given a section, search the header to find them. */
4302 _bfd_elf_section_from_bfd_section (abfd
, asect
)
4306 struct elf_backend_data
*bed
;
4309 if (elf_section_data (asect
) != NULL
4310 && elf_section_data (asect
)->this_idx
!= 0)
4311 return elf_section_data (asect
)->this_idx
;
4313 if (bfd_is_abs_section (asect
))
4315 else if (bfd_is_com_section (asect
))
4317 else if (bfd_is_und_section (asect
))
4321 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4322 int maxindex
= elf_numsections (abfd
);
4324 for (index
= 1; index
< maxindex
; index
++)
4326 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4328 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4334 bed
= get_elf_backend_data (abfd
);
4335 if (bed
->elf_backend_section_from_bfd_section
)
4339 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4344 bfd_set_error (bfd_error_nonrepresentable_section
);
4349 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4353 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
4355 asymbol
**asym_ptr_ptr
;
4357 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4359 flagword flags
= asym_ptr
->flags
;
4361 /* When gas creates relocations against local labels, it creates its
4362 own symbol for the section, but does put the symbol into the
4363 symbol chain, so udata is 0. When the linker is generating
4364 relocatable output, this section symbol may be for one of the
4365 input sections rather than the output section. */
4366 if (asym_ptr
->udata
.i
== 0
4367 && (flags
& BSF_SECTION_SYM
)
4368 && asym_ptr
->section
)
4372 if (asym_ptr
->section
->output_section
!= NULL
)
4373 indx
= asym_ptr
->section
->output_section
->index
;
4375 indx
= asym_ptr
->section
->index
;
4376 if (indx
< elf_num_section_syms (abfd
)
4377 && elf_section_syms (abfd
)[indx
] != NULL
)
4378 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4381 idx
= asym_ptr
->udata
.i
;
4385 /* This case can occur when using --strip-symbol on a symbol
4386 which is used in a relocation entry. */
4387 (*_bfd_error_handler
)
4388 (_("%s: symbol `%s' required but not present"),
4389 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4390 bfd_set_error (bfd_error_no_symbols
);
4397 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4398 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4399 elf_symbol_flags (flags
));
4407 /* Copy private BFD data. This copies any program header information. */
4410 copy_private_bfd_data (ibfd
, obfd
)
4414 Elf_Internal_Ehdr
* iehdr
;
4415 struct elf_segment_map
* map
;
4416 struct elf_segment_map
* map_first
;
4417 struct elf_segment_map
** pointer_to_map
;
4418 Elf_Internal_Phdr
* segment
;
4421 unsigned int num_segments
;
4422 boolean phdr_included
= false;
4423 bfd_vma maxpagesize
;
4424 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
4425 unsigned int phdr_adjust_num
= 0;
4426 struct elf_backend_data
* bed
;
4428 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4429 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4432 if (elf_tdata (ibfd
)->phdr
== NULL
)
4435 bed
= get_elf_backend_data (ibfd
);
4436 iehdr
= elf_elfheader (ibfd
);
4439 pointer_to_map
= &map_first
;
4441 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4442 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4444 /* Returns the end address of the segment + 1. */
4445 #define SEGMENT_END(segment, start) \
4446 (start + (segment->p_memsz > segment->p_filesz \
4447 ? segment->p_memsz : segment->p_filesz))
4449 /* Returns true if the given section is contained within
4450 the given segment. VMA addresses are compared. */
4451 #define IS_CONTAINED_BY_VMA(section, segment) \
4452 (section->vma >= segment->p_vaddr \
4453 && (section->vma + section->_raw_size) \
4454 <= (SEGMENT_END (segment, segment->p_vaddr)))
4456 /* Returns true if the given section is contained within
4457 the given segment. LMA addresses are compared. */
4458 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4459 (section->lma >= base \
4460 && (section->lma + section->_raw_size) \
4461 <= SEGMENT_END (segment, base))
4463 /* Returns true if the given section is contained within the
4464 given segment. Filepos addresses are compared in an elf
4465 backend function. */
4466 #define IS_CONTAINED_BY_FILEPOS(sec, seg, bed) \
4467 (bed->is_contained_by_filepos \
4468 && (*bed->is_contained_by_filepos) (sec, seg))
4470 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4471 #define IS_COREFILE_NOTE(p, s) \
4472 (p->p_type == PT_NOTE \
4473 && bfd_get_format (ibfd) == bfd_core \
4474 && s->vma == 0 && s->lma == 0 \
4475 && (bfd_vma) s->filepos >= p->p_offset \
4476 && (bfd_vma) s->filepos + s->_raw_size \
4477 <= p->p_offset + p->p_filesz)
4479 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4480 linker, which generates a PT_INTERP section with p_vaddr and
4481 p_memsz set to 0. */
4482 #define IS_SOLARIS_PT_INTERP(p, s) \
4484 && p->p_filesz > 0 \
4485 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4486 && s->_raw_size > 0 \
4487 && (bfd_vma) s->filepos >= p->p_offset \
4488 && ((bfd_vma) s->filepos + s->_raw_size \
4489 <= p->p_offset + p->p_filesz))
4491 /* Decide if the given section should be included in the given segment.
4492 A section will be included if:
4493 1. It is within the address space of the segment -- we use the LMA
4494 if that is set for the segment and the VMA otherwise,
4495 2. It is an allocated segment,
4496 3. There is an output section associated with it,
4497 4. The section has not already been allocated to a previous segment. */
4498 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4499 (((((segment->p_paddr \
4500 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4501 : IS_CONTAINED_BY_VMA (section, segment)) \
4502 || IS_SOLARIS_PT_INTERP (segment, section)) \
4503 && (section->flags & SEC_ALLOC) != 0) \
4504 || IS_COREFILE_NOTE (segment, section) \
4505 || (IS_CONTAINED_BY_FILEPOS (section, segment, bed) \
4506 && (section->flags & SEC_ALLOC) == 0)) \
4507 && section->output_section != NULL \
4508 && section->segment_mark == false)
4510 /* Returns true iff seg1 starts after the end of seg2. */
4511 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4512 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4514 /* Returns true iff seg1 and seg2 overlap. */
4515 #define SEGMENT_OVERLAPS(seg1, seg2) \
4516 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4518 /* Initialise the segment mark field. */
4519 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4520 section
->segment_mark
= false;
4522 /* Scan through the segments specified in the program header
4523 of the input BFD. For this first scan we look for overlaps
4524 in the loadable segments. These can be created by weird
4525 parameters to objcopy. */
4526 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4531 Elf_Internal_Phdr
*segment2
;
4533 if (segment
->p_type
!= PT_LOAD
)
4536 /* Determine if this segment overlaps any previous segments. */
4537 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4539 bfd_signed_vma extra_length
;
4541 if (segment2
->p_type
!= PT_LOAD
4542 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4545 /* Merge the two segments together. */
4546 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4548 /* Extend SEGMENT2 to include SEGMENT and then delete
4551 SEGMENT_END (segment
, segment
->p_vaddr
)
4552 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4554 if (extra_length
> 0)
4556 segment2
->p_memsz
+= extra_length
;
4557 segment2
->p_filesz
+= extra_length
;
4560 segment
->p_type
= PT_NULL
;
4562 /* Since we have deleted P we must restart the outer loop. */
4564 segment
= elf_tdata (ibfd
)->phdr
;
4569 /* Extend SEGMENT to include SEGMENT2 and then delete
4572 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4573 - SEGMENT_END (segment
, segment
->p_vaddr
);
4575 if (extra_length
> 0)
4577 segment
->p_memsz
+= extra_length
;
4578 segment
->p_filesz
+= extra_length
;
4581 segment2
->p_type
= PT_NULL
;
4586 /* The second scan attempts to assign sections to segments. */
4587 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4591 unsigned int section_count
;
4592 asection
** sections
;
4593 asection
* output_section
;
4595 bfd_vma matching_lma
;
4596 bfd_vma suggested_lma
;
4600 if (segment
->p_type
== PT_NULL
)
4603 /* Compute how many sections might be placed into this segment. */
4605 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4606 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4609 /* Allocate a segment map big enough to contain all of the
4610 sections we have selected. */
4611 amt
= sizeof (struct elf_segment_map
);
4612 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4613 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4617 /* Initialise the fields of the segment map. Default to
4618 using the physical address of the segment in the input BFD. */
4620 map
->p_type
= segment
->p_type
;
4621 map
->p_flags
= segment
->p_flags
;
4622 map
->p_flags_valid
= 1;
4623 map
->p_paddr
= segment
->p_paddr
;
4624 map
->p_paddr_valid
= 1;
4626 /* Determine if this segment contains the ELF file header
4627 and if it contains the program headers themselves. */
4628 map
->includes_filehdr
= (segment
->p_offset
== 0
4629 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4631 map
->includes_phdrs
= 0;
4633 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4635 map
->includes_phdrs
=
4636 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4637 && (segment
->p_offset
+ segment
->p_filesz
4638 >= ((bfd_vma
) iehdr
->e_phoff
4639 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4641 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4642 phdr_included
= true;
4645 if (section_count
== 0)
4647 /* Special segments, such as the PT_PHDR segment, may contain
4648 no sections, but ordinary, loadable segments should contain
4649 something. They are allowed by the ELF spec however, so only
4650 a warning is produced. */
4651 if (segment
->p_type
== PT_LOAD
)
4652 (*_bfd_error_handler
)
4653 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4654 bfd_archive_filename (ibfd
));
4657 *pointer_to_map
= map
;
4658 pointer_to_map
= &map
->next
;
4663 /* Now scan the sections in the input BFD again and attempt
4664 to add their corresponding output sections to the segment map.
4665 The problem here is how to handle an output section which has
4666 been moved (ie had its LMA changed). There are four possibilities:
4668 1. None of the sections have been moved.
4669 In this case we can continue to use the segment LMA from the
4672 2. All of the sections have been moved by the same amount.
4673 In this case we can change the segment's LMA to match the LMA
4674 of the first section.
4676 3. Some of the sections have been moved, others have not.
4677 In this case those sections which have not been moved can be
4678 placed in the current segment which will have to have its size,
4679 and possibly its LMA changed, and a new segment or segments will
4680 have to be created to contain the other sections.
4682 4. The sections have been moved, but not be the same amount.
4683 In this case we can change the segment's LMA to match the LMA
4684 of the first section and we will have to create a new segment
4685 or segments to contain the other sections.
4687 In order to save time, we allocate an array to hold the section
4688 pointers that we are interested in. As these sections get assigned
4689 to a segment, they are removed from this array. */
4691 amt
= (bfd_size_type
) section_count
* sizeof (asection
*);
4692 sections
= (asection
**) bfd_malloc (amt
);
4693 if (sections
== NULL
)
4696 /* Step One: Scan for segment vs section LMA conflicts.
4697 Also add the sections to the section array allocated above.
4698 Also add the sections to the current segment. In the common
4699 case, where the sections have not been moved, this means that
4700 we have completely filled the segment, and there is nothing
4706 for (j
= 0, section
= ibfd
->sections
;
4708 section
= section
->next
)
4710 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4712 output_section
= section
->output_section
;
4714 sections
[j
++] = section
;
4716 /* The Solaris native linker always sets p_paddr to 0.
4717 We try to catch that case here, and set it to the
4719 if (segment
->p_paddr
== 0
4720 && segment
->p_vaddr
!= 0
4722 && output_section
->lma
!= 0
4723 && (output_section
->vma
== (segment
->p_vaddr
4724 + (map
->includes_filehdr
4727 + (map
->includes_phdrs
4729 * iehdr
->e_phentsize
)
4731 map
->p_paddr
= segment
->p_vaddr
;
4733 /* Match up the physical address of the segment with the
4734 LMA address of the output section. */
4735 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4736 || IS_CONTAINED_BY_FILEPOS (section
, segment
, bed
)
4737 || IS_COREFILE_NOTE (segment
, section
))
4739 if (matching_lma
== 0)
4740 matching_lma
= output_section
->lma
;
4742 /* We assume that if the section fits within the segment
4743 then it does not overlap any other section within that
4745 map
->sections
[isec
++] = output_section
;
4747 else if (suggested_lma
== 0)
4748 suggested_lma
= output_section
->lma
;
4752 BFD_ASSERT (j
== section_count
);
4754 /* Step Two: Adjust the physical address of the current segment,
4756 if (isec
== section_count
)
4758 /* All of the sections fitted within the segment as currently
4759 specified. This is the default case. Add the segment to
4760 the list of built segments and carry on to process the next
4761 program header in the input BFD. */
4762 map
->count
= section_count
;
4763 *pointer_to_map
= map
;
4764 pointer_to_map
= &map
->next
;
4771 if (matching_lma
!= 0)
4773 /* At least one section fits inside the current segment.
4774 Keep it, but modify its physical address to match the
4775 LMA of the first section that fitted. */
4776 map
->p_paddr
= matching_lma
;
4780 /* None of the sections fitted inside the current segment.
4781 Change the current segment's physical address to match
4782 the LMA of the first section. */
4783 map
->p_paddr
= suggested_lma
;
4786 /* Offset the segment physical address from the lma
4787 to allow for space taken up by elf headers. */
4788 if (map
->includes_filehdr
)
4789 map
->p_paddr
-= iehdr
->e_ehsize
;
4791 if (map
->includes_phdrs
)
4793 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4795 /* iehdr->e_phnum is just an estimate of the number
4796 of program headers that we will need. Make a note
4797 here of the number we used and the segment we chose
4798 to hold these headers, so that we can adjust the
4799 offset when we know the correct value. */
4800 phdr_adjust_num
= iehdr
->e_phnum
;
4801 phdr_adjust_seg
= map
;
4805 /* Step Three: Loop over the sections again, this time assigning
4806 those that fit to the current segment and removing them from the
4807 sections array; but making sure not to leave large gaps. Once all
4808 possible sections have been assigned to the current segment it is
4809 added to the list of built segments and if sections still remain
4810 to be assigned, a new segment is constructed before repeating
4818 /* Fill the current segment with sections that fit. */
4819 for (j
= 0; j
< section_count
; j
++)
4821 section
= sections
[j
];
4823 if (section
== NULL
)
4826 output_section
= section
->output_section
;
4828 BFD_ASSERT (output_section
!= NULL
);
4830 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4831 || IS_COREFILE_NOTE (segment
, section
))
4833 if (map
->count
== 0)
4835 /* If the first section in a segment does not start at
4836 the beginning of the segment, then something is
4838 if (output_section
->lma
!=
4840 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4841 + (map
->includes_phdrs
4842 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4848 asection
* prev_sec
;
4850 prev_sec
= map
->sections
[map
->count
- 1];
4852 /* If the gap between the end of the previous section
4853 and the start of this section is more than
4854 maxpagesize then we need to start a new segment. */
4855 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
4857 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4858 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
4859 > output_section
->lma
))
4861 if (suggested_lma
== 0)
4862 suggested_lma
= output_section
->lma
;
4868 map
->sections
[map
->count
++] = output_section
;
4871 section
->segment_mark
= true;
4873 else if (suggested_lma
== 0)
4874 suggested_lma
= output_section
->lma
;
4877 BFD_ASSERT (map
->count
> 0);
4879 /* Add the current segment to the list of built segments. */
4880 *pointer_to_map
= map
;
4881 pointer_to_map
= &map
->next
;
4883 if (isec
< section_count
)
4885 /* We still have not allocated all of the sections to
4886 segments. Create a new segment here, initialise it
4887 and carry on looping. */
4888 amt
= sizeof (struct elf_segment_map
);
4889 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4890 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4894 /* Initialise the fields of the segment map. Set the physical
4895 physical address to the LMA of the first section that has
4896 not yet been assigned. */
4898 map
->p_type
= segment
->p_type
;
4899 map
->p_flags
= segment
->p_flags
;
4900 map
->p_flags_valid
= 1;
4901 map
->p_paddr
= suggested_lma
;
4902 map
->p_paddr_valid
= 1;
4903 map
->includes_filehdr
= 0;
4904 map
->includes_phdrs
= 0;
4907 while (isec
< section_count
);
4912 /* The Solaris linker creates program headers in which all the
4913 p_paddr fields are zero. When we try to objcopy or strip such a
4914 file, we get confused. Check for this case, and if we find it
4915 reset the p_paddr_valid fields. */
4916 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4917 if (map
->p_paddr
!= 0)
4921 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4922 map
->p_paddr_valid
= 0;
4925 elf_tdata (obfd
)->segment_map
= map_first
;
4927 /* If we had to estimate the number of program headers that were
4928 going to be needed, then check our estimate now and adjust
4929 the offset if necessary. */
4930 if (phdr_adjust_seg
!= NULL
)
4934 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4937 if (count
> phdr_adjust_num
)
4938 phdr_adjust_seg
->p_paddr
4939 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4943 /* Final Step: Sort the segments into ascending order of physical
4945 if (map_first
!= NULL
)
4947 struct elf_segment_map
*prev
;
4950 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4952 /* Yes I know - its a bubble sort.... */
4953 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4955 /* Swap map and map->next. */
4956 prev
->next
= map
->next
;
4957 map
->next
= map
->next
->next
;
4958 prev
->next
->next
= map
;
4968 #undef IS_CONTAINED_BY_VMA
4969 #undef IS_CONTAINED_BY_LMA
4970 #undef IS_CONTAINED_BY_FILEPOS
4971 #undef IS_COREFILE_NOTE
4972 #undef IS_SOLARIS_PT_INTERP
4973 #undef INCLUDE_SECTION_IN_SEGMENT
4974 #undef SEGMENT_AFTER_SEGMENT
4975 #undef SEGMENT_OVERLAPS
4979 /* Copy private section information. This copies over the entsize
4980 field, and sometimes the info field. */
4983 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4989 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4990 const struct elf_backend_data
*bed
= get_elf_backend_data (ibfd
);
4992 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4993 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4996 /* Copy over private BFD data if it has not already been copied.
4997 This must be done here, rather than in the copy_private_bfd_data
4998 entry point, because the latter is called after the section
4999 contents have been set, which means that the program headers have
5000 already been worked out. The backend function provides a way to
5001 override the test conditions and code path for the call to
5002 copy_private_bfd_data. */
5003 if (bed
->copy_private_bfd_data_p
)
5005 if ((*bed
->copy_private_bfd_data_p
) (ibfd
, isec
, obfd
, osec
))
5006 if (! copy_private_bfd_data (ibfd
, obfd
))
5009 else if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
5013 /* Only set up the segments if there are no more SEC_ALLOC
5014 sections. FIXME: This won't do the right thing if objcopy is
5015 used to remove the last SEC_ALLOC section, since objcopy
5016 won't call this routine in that case. */
5017 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
5018 if ((s
->flags
& SEC_ALLOC
) != 0)
5022 if (! copy_private_bfd_data (ibfd
, obfd
))
5027 ihdr
= &elf_section_data (isec
)->this_hdr
;
5028 ohdr
= &elf_section_data (osec
)->this_hdr
;
5030 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
5032 if (ihdr
->sh_type
== SHT_SYMTAB
5033 || ihdr
->sh_type
== SHT_DYNSYM
5034 || ihdr
->sh_type
== SHT_GNU_verneed
5035 || ihdr
->sh_type
== SHT_GNU_verdef
)
5036 ohdr
->sh_info
= ihdr
->sh_info
;
5038 /* Set things up for objcopy. The output SHT_GROUP section will
5039 have its elf_next_in_group pointing back to the input group
5041 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5042 elf_group_name (osec
) = elf_group_name (isec
);
5044 elf_section_data (osec
)->use_rela_p
5045 = elf_section_data (isec
)->use_rela_p
;
5050 /* Copy private symbol information. If this symbol is in a section
5051 which we did not map into a BFD section, try to map the section
5052 index correctly. We use special macro definitions for the mapped
5053 section indices; these definitions are interpreted by the
5054 swap_out_syms function. */
5056 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5057 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5058 #define MAP_STRTAB (SHN_HIOS + 3)
5059 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5060 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5063 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
5069 elf_symbol_type
*isym
, *osym
;
5071 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5072 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5075 isym
= elf_symbol_from (ibfd
, isymarg
);
5076 osym
= elf_symbol_from (obfd
, osymarg
);
5080 && bfd_is_abs_section (isym
->symbol
.section
))
5084 shndx
= isym
->internal_elf_sym
.st_shndx
;
5085 if (shndx
== elf_onesymtab (ibfd
))
5086 shndx
= MAP_ONESYMTAB
;
5087 else if (shndx
== elf_dynsymtab (ibfd
))
5088 shndx
= MAP_DYNSYMTAB
;
5089 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
5091 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
5092 shndx
= MAP_SHSTRTAB
;
5093 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
5094 shndx
= MAP_SYM_SHNDX
;
5095 osym
->internal_elf_sym
.st_shndx
= shndx
;
5101 /* Swap out the symbols. */
5104 swap_out_syms (abfd
, sttp
, relocatable_p
)
5106 struct bfd_strtab_hash
**sttp
;
5109 struct elf_backend_data
*bed
;
5112 struct bfd_strtab_hash
*stt
;
5113 Elf_Internal_Shdr
*symtab_hdr
;
5114 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5115 Elf_Internal_Shdr
*symstrtab_hdr
;
5116 char *outbound_syms
;
5117 char *outbound_shndx
;
5121 if (!elf_map_symbols (abfd
))
5124 /* Dump out the symtabs. */
5125 stt
= _bfd_elf_stringtab_init ();
5129 bed
= get_elf_backend_data (abfd
);
5130 symcount
= bfd_get_symcount (abfd
);
5131 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5132 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5133 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5134 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5135 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5136 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5138 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5139 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5141 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
5142 outbound_syms
= bfd_alloc (abfd
, amt
);
5143 if (outbound_syms
== NULL
)
5145 symtab_hdr
->contents
= (PTR
) outbound_syms
;
5147 outbound_shndx
= NULL
;
5148 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5149 if (symtab_shndx_hdr
->sh_name
!= 0)
5151 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5152 outbound_shndx
= bfd_zalloc (abfd
, amt
);
5153 if (outbound_shndx
== NULL
)
5155 symtab_shndx_hdr
->contents
= outbound_shndx
;
5156 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5157 symtab_shndx_hdr
->sh_size
= amt
;
5158 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5159 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5162 /* now generate the data (for "contents") */
5164 /* Fill in zeroth symbol and swap it out. */
5165 Elf_Internal_Sym sym
;
5171 sym
.st_shndx
= SHN_UNDEF
;
5172 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5173 outbound_syms
+= bed
->s
->sizeof_sym
;
5174 if (outbound_shndx
!= NULL
)
5175 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5178 syms
= bfd_get_outsymbols (abfd
);
5179 for (idx
= 0; idx
< symcount
; idx
++)
5181 Elf_Internal_Sym sym
;
5182 bfd_vma value
= syms
[idx
]->value
;
5183 elf_symbol_type
*type_ptr
;
5184 flagword flags
= syms
[idx
]->flags
;
5187 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5189 /* Local section symbols have no name. */
5194 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5197 if (sym
.st_name
== (unsigned long) -1)
5201 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5203 if ((flags
& BSF_SECTION_SYM
) == 0
5204 && bfd_is_com_section (syms
[idx
]->section
))
5206 /* ELF common symbols put the alignment into the `value' field,
5207 and the size into the `size' field. This is backwards from
5208 how BFD handles it, so reverse it here. */
5209 sym
.st_size
= value
;
5210 if (type_ptr
== NULL
5211 || type_ptr
->internal_elf_sym
.st_value
== 0)
5212 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5214 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5215 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5216 (abfd
, syms
[idx
]->section
);
5220 asection
*sec
= syms
[idx
]->section
;
5223 if (sec
->output_section
)
5225 value
+= sec
->output_offset
;
5226 sec
= sec
->output_section
;
5228 /* Don't add in the section vma for relocatable output. */
5229 if (! relocatable_p
)
5231 sym
.st_value
= value
;
5232 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5234 if (bfd_is_abs_section (sec
)
5236 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5238 /* This symbol is in a real ELF section which we did
5239 not create as a BFD section. Undo the mapping done
5240 by copy_private_symbol_data. */
5241 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5245 shndx
= elf_onesymtab (abfd
);
5248 shndx
= elf_dynsymtab (abfd
);
5251 shndx
= elf_tdata (abfd
)->strtab_section
;
5254 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5257 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5265 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5271 /* Writing this would be a hell of a lot easier if
5272 we had some decent documentation on bfd, and
5273 knew what to expect of the library, and what to
5274 demand of applications. For example, it
5275 appears that `objcopy' might not set the
5276 section of a symbol to be a section that is
5277 actually in the output file. */
5278 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5279 BFD_ASSERT (sec2
!= 0);
5280 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5281 BFD_ASSERT (shndx
!= -1);
5285 sym
.st_shndx
= shndx
;
5288 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5290 else if ((flags
& BSF_FUNCTION
) != 0)
5292 else if ((flags
& BSF_OBJECT
) != 0)
5297 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5300 /* Processor-specific types */
5301 if (type_ptr
!= NULL
5302 && bed
->elf_backend_get_symbol_type
)
5303 type
= ((*bed
->elf_backend_get_symbol_type
)
5304 (&type_ptr
->internal_elf_sym
, type
));
5306 if (flags
& BSF_SECTION_SYM
)
5308 if (flags
& BSF_GLOBAL
)
5309 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5311 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5313 else if (bfd_is_com_section (syms
[idx
]->section
))
5314 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5315 else if (bfd_is_und_section (syms
[idx
]->section
))
5316 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5320 else if (flags
& BSF_FILE
)
5321 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5324 int bind
= STB_LOCAL
;
5326 if (flags
& BSF_LOCAL
)
5328 else if (flags
& BSF_WEAK
)
5330 else if (flags
& BSF_GLOBAL
)
5333 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5336 if (type_ptr
!= NULL
)
5337 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5341 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5342 outbound_syms
+= bed
->s
->sizeof_sym
;
5343 if (outbound_shndx
!= NULL
)
5344 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5348 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5349 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5351 symstrtab_hdr
->sh_flags
= 0;
5352 symstrtab_hdr
->sh_addr
= 0;
5353 symstrtab_hdr
->sh_entsize
= 0;
5354 symstrtab_hdr
->sh_link
= 0;
5355 symstrtab_hdr
->sh_info
= 0;
5356 symstrtab_hdr
->sh_addralign
= 1;
5361 /* Return the number of bytes required to hold the symtab vector.
5363 Note that we base it on the count plus 1, since we will null terminate
5364 the vector allocated based on this size. However, the ELF symbol table
5365 always has a dummy entry as symbol #0, so it ends up even. */
5368 _bfd_elf_get_symtab_upper_bound (abfd
)
5373 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5375 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5376 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5378 symtab_size
-= sizeof (asymbol
*);
5384 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
5389 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5391 if (elf_dynsymtab (abfd
) == 0)
5393 bfd_set_error (bfd_error_invalid_operation
);
5397 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5398 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5400 symtab_size
-= sizeof (asymbol
*);
5406 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
5407 bfd
*abfd ATTRIBUTE_UNUSED
;
5410 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5413 /* Canonicalize the relocs. */
5416 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
5424 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5426 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
5429 tblptr
= section
->relocation
;
5430 for (i
= 0; i
< section
->reloc_count
; i
++)
5431 *relptr
++ = tblptr
++;
5435 return section
->reloc_count
;
5439 _bfd_elf_get_symtab (abfd
, alocation
)
5441 asymbol
**alocation
;
5443 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5444 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, false);
5447 bfd_get_symcount (abfd
) = symcount
;
5452 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
5454 asymbol
**alocation
;
5456 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5457 return bed
->s
->slurp_symbol_table (abfd
, alocation
, true);
5460 /* Return the size required for the dynamic reloc entries. Any
5461 section that was actually installed in the BFD, and has type
5462 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5463 considered to be a dynamic reloc section. */
5466 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
5472 if (elf_dynsymtab (abfd
) == 0)
5474 bfd_set_error (bfd_error_invalid_operation
);
5478 ret
= sizeof (arelent
*);
5479 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5480 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5481 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5482 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5483 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5484 * sizeof (arelent
*));
5489 /* Canonicalize the dynamic relocation entries. Note that we return
5490 the dynamic relocations as a single block, although they are
5491 actually associated with particular sections; the interface, which
5492 was designed for SunOS style shared libraries, expects that there
5493 is only one set of dynamic relocs. Any section that was actually
5494 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5495 the dynamic symbol table, is considered to be a dynamic reloc
5499 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
5504 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
5508 if (elf_dynsymtab (abfd
) == 0)
5510 bfd_set_error (bfd_error_invalid_operation
);
5514 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5516 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5518 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5519 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5520 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5525 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
5527 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5529 for (i
= 0; i
< count
; i
++)
5540 /* Read in the version information. */
5543 _bfd_elf_slurp_version_tables (abfd
)
5546 bfd_byte
*contents
= NULL
;
5549 if (elf_dynverdef (abfd
) != 0)
5551 Elf_Internal_Shdr
*hdr
;
5552 Elf_External_Verdef
*everdef
;
5553 Elf_Internal_Verdef
*iverdef
;
5554 Elf_Internal_Verdef
*iverdefarr
;
5555 Elf_Internal_Verdef iverdefmem
;
5557 unsigned int maxidx
;
5559 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5561 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5562 if (contents
== NULL
)
5564 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5565 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5568 /* We know the number of entries in the section but not the maximum
5569 index. Therefore we have to run through all entries and find
5571 everdef
= (Elf_External_Verdef
*) contents
;
5573 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5575 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5577 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5578 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5580 everdef
= ((Elf_External_Verdef
*)
5581 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5584 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5585 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
5586 if (elf_tdata (abfd
)->verdef
== NULL
)
5589 elf_tdata (abfd
)->cverdefs
= maxidx
;
5591 everdef
= (Elf_External_Verdef
*) contents
;
5592 iverdefarr
= elf_tdata (abfd
)->verdef
;
5593 for (i
= 0; i
< hdr
->sh_info
; i
++)
5595 Elf_External_Verdaux
*everdaux
;
5596 Elf_Internal_Verdaux
*iverdaux
;
5599 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5601 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5602 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5604 iverdef
->vd_bfd
= abfd
;
5606 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5607 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
5608 if (iverdef
->vd_auxptr
== NULL
)
5611 everdaux
= ((Elf_External_Verdaux
*)
5612 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5613 iverdaux
= iverdef
->vd_auxptr
;
5614 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5616 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5618 iverdaux
->vda_nodename
=
5619 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5620 iverdaux
->vda_name
);
5621 if (iverdaux
->vda_nodename
== NULL
)
5624 if (j
+ 1 < iverdef
->vd_cnt
)
5625 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5627 iverdaux
->vda_nextptr
= NULL
;
5629 everdaux
= ((Elf_External_Verdaux
*)
5630 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5633 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5635 if (i
+ 1 < hdr
->sh_info
)
5636 iverdef
->vd_nextdef
= iverdef
+ 1;
5638 iverdef
->vd_nextdef
= NULL
;
5640 everdef
= ((Elf_External_Verdef
*)
5641 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5648 if (elf_dynverref (abfd
) != 0)
5650 Elf_Internal_Shdr
*hdr
;
5651 Elf_External_Verneed
*everneed
;
5652 Elf_Internal_Verneed
*iverneed
;
5655 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5657 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5658 elf_tdata (abfd
)->verref
=
5659 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
5660 if (elf_tdata (abfd
)->verref
== NULL
)
5663 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5665 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5666 if (contents
== NULL
)
5668 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5669 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5672 everneed
= (Elf_External_Verneed
*) contents
;
5673 iverneed
= elf_tdata (abfd
)->verref
;
5674 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5676 Elf_External_Vernaux
*evernaux
;
5677 Elf_Internal_Vernaux
*ivernaux
;
5680 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5682 iverneed
->vn_bfd
= abfd
;
5684 iverneed
->vn_filename
=
5685 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5687 if (iverneed
->vn_filename
== NULL
)
5690 amt
= iverneed
->vn_cnt
;
5691 amt
*= sizeof (Elf_Internal_Vernaux
);
5692 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
5694 evernaux
= ((Elf_External_Vernaux
*)
5695 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5696 ivernaux
= iverneed
->vn_auxptr
;
5697 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5699 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5701 ivernaux
->vna_nodename
=
5702 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5703 ivernaux
->vna_name
);
5704 if (ivernaux
->vna_nodename
== NULL
)
5707 if (j
+ 1 < iverneed
->vn_cnt
)
5708 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5710 ivernaux
->vna_nextptr
= NULL
;
5712 evernaux
= ((Elf_External_Vernaux
*)
5713 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5716 if (i
+ 1 < hdr
->sh_info
)
5717 iverneed
->vn_nextref
= iverneed
+ 1;
5719 iverneed
->vn_nextref
= NULL
;
5721 everneed
= ((Elf_External_Verneed
*)
5722 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5732 if (contents
== NULL
)
5738 _bfd_elf_make_empty_symbol (abfd
)
5741 elf_symbol_type
*newsym
;
5742 bfd_size_type amt
= sizeof (elf_symbol_type
);
5744 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
5749 newsym
->symbol
.the_bfd
= abfd
;
5750 return &newsym
->symbol
;
5755 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
5756 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5760 bfd_symbol_info (symbol
, ret
);
5763 /* Return whether a symbol name implies a local symbol. Most targets
5764 use this function for the is_local_label_name entry point, but some
5768 _bfd_elf_is_local_label_name (abfd
, name
)
5769 bfd
*abfd ATTRIBUTE_UNUSED
;
5772 /* Normal local symbols start with ``.L''. */
5773 if (name
[0] == '.' && name
[1] == 'L')
5776 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5777 DWARF debugging symbols starting with ``..''. */
5778 if (name
[0] == '.' && name
[1] == '.')
5781 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5782 emitting DWARF debugging output. I suspect this is actually a
5783 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5784 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5785 underscore to be emitted on some ELF targets). For ease of use,
5786 we treat such symbols as local. */
5787 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5794 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5795 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5796 asymbol
*symbol ATTRIBUTE_UNUSED
;
5803 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5805 enum bfd_architecture arch
;
5806 unsigned long machine
;
5808 /* If this isn't the right architecture for this backend, and this
5809 isn't the generic backend, fail. */
5810 if (arch
!= get_elf_backend_data (abfd
)->arch
5811 && arch
!= bfd_arch_unknown
5812 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5815 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5818 /* Find the function to a particular section and offset,
5819 for error reporting. */
5822 elf_find_function (abfd
, section
, symbols
, offset
,
5823 filename_ptr
, functionname_ptr
)
5824 bfd
*abfd ATTRIBUTE_UNUSED
;
5828 const char **filename_ptr
;
5829 const char **functionname_ptr
;
5831 const char *filename
;
5840 for (p
= symbols
; *p
!= NULL
; p
++)
5844 q
= (elf_symbol_type
*) *p
;
5846 if (bfd_get_section (&q
->symbol
) != section
)
5849 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5854 filename
= bfd_asymbol_name (&q
->symbol
);
5858 if (q
->symbol
.section
== section
5859 && q
->symbol
.value
>= low_func
5860 && q
->symbol
.value
<= offset
)
5862 func
= (asymbol
*) q
;
5863 low_func
= q
->symbol
.value
;
5873 *filename_ptr
= filename
;
5874 if (functionname_ptr
)
5875 *functionname_ptr
= bfd_asymbol_name (func
);
5880 /* Find the nearest line to a particular section and offset,
5881 for error reporting. */
5884 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5885 filename_ptr
, functionname_ptr
, line_ptr
)
5890 const char **filename_ptr
;
5891 const char **functionname_ptr
;
5892 unsigned int *line_ptr
;
5896 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5897 filename_ptr
, functionname_ptr
,
5900 if (!*functionname_ptr
)
5901 elf_find_function (abfd
, section
, symbols
, offset
,
5902 *filename_ptr
? NULL
: filename_ptr
,
5908 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5909 filename_ptr
, functionname_ptr
,
5911 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5913 if (!*functionname_ptr
)
5914 elf_find_function (abfd
, section
, symbols
, offset
,
5915 *filename_ptr
? NULL
: filename_ptr
,
5921 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5922 &found
, filename_ptr
,
5923 functionname_ptr
, line_ptr
,
5924 &elf_tdata (abfd
)->line_info
))
5929 if (symbols
== NULL
)
5932 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5933 filename_ptr
, functionname_ptr
))
5941 _bfd_elf_sizeof_headers (abfd
, reloc
)
5947 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5949 ret
+= get_program_header_size (abfd
);
5954 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5959 bfd_size_type count
;
5961 Elf_Internal_Shdr
*hdr
;
5964 if (! abfd
->output_has_begun
5965 && ! _bfd_elf_compute_section_file_positions
5966 (abfd
, (struct bfd_link_info
*) NULL
))
5969 hdr
= &elf_section_data (section
)->this_hdr
;
5970 pos
= hdr
->sh_offset
+ offset
;
5971 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5972 || bfd_bwrite (location
, count
, abfd
) != count
)
5979 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5980 bfd
*abfd ATTRIBUTE_UNUSED
;
5981 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5982 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5989 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5992 Elf_Internal_Rel
*dst
;
5998 /* Try to convert a non-ELF reloc into an ELF one. */
6001 _bfd_elf_validate_reloc (abfd
, areloc
)
6005 /* Check whether we really have an ELF howto. */
6007 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
6009 bfd_reloc_code_real_type code
;
6010 reloc_howto_type
*howto
;
6012 /* Alien reloc: Try to determine its type to replace it with an
6013 equivalent ELF reloc. */
6015 if (areloc
->howto
->pc_relative
)
6017 switch (areloc
->howto
->bitsize
)
6020 code
= BFD_RELOC_8_PCREL
;
6023 code
= BFD_RELOC_12_PCREL
;
6026 code
= BFD_RELOC_16_PCREL
;
6029 code
= BFD_RELOC_24_PCREL
;
6032 code
= BFD_RELOC_32_PCREL
;
6035 code
= BFD_RELOC_64_PCREL
;
6041 howto
= bfd_reloc_type_lookup (abfd
, code
);
6043 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
6045 if (howto
->pcrel_offset
)
6046 areloc
->addend
+= areloc
->address
;
6048 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
6053 switch (areloc
->howto
->bitsize
)
6059 code
= BFD_RELOC_14
;
6062 code
= BFD_RELOC_16
;
6065 code
= BFD_RELOC_26
;
6068 code
= BFD_RELOC_32
;
6071 code
= BFD_RELOC_64
;
6077 howto
= bfd_reloc_type_lookup (abfd
, code
);
6081 areloc
->howto
= howto
;
6089 (*_bfd_error_handler
)
6090 (_("%s: unsupported relocation type %s"),
6091 bfd_archive_filename (abfd
), areloc
->howto
->name
);
6092 bfd_set_error (bfd_error_bad_value
);
6097 _bfd_elf_close_and_cleanup (abfd
)
6100 if (bfd_get_format (abfd
) == bfd_object
)
6102 if (elf_shstrtab (abfd
) != NULL
)
6103 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
6106 return _bfd_generic_close_and_cleanup (abfd
);
6109 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6110 in the relocation's offset. Thus we cannot allow any sort of sanity
6111 range-checking to interfere. There is nothing else to do in processing
6114 bfd_reloc_status_type
6115 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
6116 bfd
*abfd ATTRIBUTE_UNUSED
;
6117 arelent
*re ATTRIBUTE_UNUSED
;
6118 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
6119 PTR data ATTRIBUTE_UNUSED
;
6120 asection
*is ATTRIBUTE_UNUSED
;
6121 bfd
*obfd ATTRIBUTE_UNUSED
;
6122 char **errmsg ATTRIBUTE_UNUSED
;
6124 return bfd_reloc_ok
;
6127 /* Elf core file support. Much of this only works on native
6128 toolchains, since we rely on knowing the
6129 machine-dependent procfs structure in order to pick
6130 out details about the corefile. */
6132 #ifdef HAVE_SYS_PROCFS_H
6133 # include <sys/procfs.h>
6136 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6139 elfcore_make_pid (abfd
)
6142 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
6143 + (elf_tdata (abfd
)->core_pid
));
6146 /* If there isn't a section called NAME, make one, using
6147 data from SECT. Note, this function will generate a
6148 reference to NAME, so you shouldn't deallocate or
6152 elfcore_maybe_make_sect (abfd
, name
, sect
)
6159 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
6162 sect2
= bfd_make_section (abfd
, name
);
6166 sect2
->_raw_size
= sect
->_raw_size
;
6167 sect2
->filepos
= sect
->filepos
;
6168 sect2
->flags
= sect
->flags
;
6169 sect2
->alignment_power
= sect
->alignment_power
;
6173 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6174 actually creates up to two pseudosections:
6175 - For the single-threaded case, a section named NAME, unless
6176 such a section already exists.
6177 - For the multi-threaded case, a section named "NAME/PID", where
6178 PID is elfcore_make_pid (abfd).
6179 Both pseudosections have identical contents. */
6181 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
6188 char *threaded_name
;
6191 /* Build the section name. */
6193 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6194 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6195 if (threaded_name
== NULL
)
6197 strcpy (threaded_name
, buf
);
6199 sect
= bfd_make_section (abfd
, threaded_name
);
6202 sect
->_raw_size
= size
;
6203 sect
->filepos
= filepos
;
6204 sect
->flags
= SEC_HAS_CONTENTS
;
6205 sect
->alignment_power
= 2;
6207 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6210 /* prstatus_t exists on:
6212 linux 2.[01] + glibc
6216 #if defined (HAVE_PRSTATUS_T)
6217 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6220 elfcore_grok_prstatus (abfd
, note
)
6222 Elf_Internal_Note
*note
;
6227 if (note
->descsz
== sizeof (prstatus_t
))
6231 raw_size
= sizeof (prstat
.pr_reg
);
6232 offset
= offsetof (prstatus_t
, pr_reg
);
6233 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6235 /* Do not overwrite the core signal if it
6236 has already been set by another thread. */
6237 if (elf_tdata (abfd
)->core_signal
== 0)
6238 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6239 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6241 /* pr_who exists on:
6244 pr_who doesn't exist on:
6247 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6248 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6251 #if defined (HAVE_PRSTATUS32_T)
6252 else if (note
->descsz
== sizeof (prstatus32_t
))
6254 /* 64-bit host, 32-bit corefile */
6255 prstatus32_t prstat
;
6257 raw_size
= sizeof (prstat
.pr_reg
);
6258 offset
= offsetof (prstatus32_t
, pr_reg
);
6259 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6261 /* Do not overwrite the core signal if it
6262 has already been set by another thread. */
6263 if (elf_tdata (abfd
)->core_signal
== 0)
6264 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6265 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6267 /* pr_who exists on:
6270 pr_who doesn't exist on:
6273 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6274 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6277 #endif /* HAVE_PRSTATUS32_T */
6280 /* Fail - we don't know how to handle any other
6281 note size (ie. data object type). */
6285 /* Make a ".reg/999" section and a ".reg" section. */
6286 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6287 raw_size
, note
->descpos
+ offset
);
6289 #endif /* defined (HAVE_PRSTATUS_T) */
6291 /* Create a pseudosection containing the exact contents of NOTE. */
6293 elfcore_make_note_pseudosection (abfd
, name
, note
)
6296 Elf_Internal_Note
*note
;
6298 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6299 note
->descsz
, note
->descpos
);
6302 /* There isn't a consistent prfpregset_t across platforms,
6303 but it doesn't matter, because we don't have to pick this
6304 data structure apart. */
6307 elfcore_grok_prfpreg (abfd
, note
)
6309 Elf_Internal_Note
*note
;
6311 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6314 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6315 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6319 elfcore_grok_prxfpreg (abfd
, note
)
6321 Elf_Internal_Note
*note
;
6323 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6326 #if defined (HAVE_PRPSINFO_T)
6327 typedef prpsinfo_t elfcore_psinfo_t
;
6328 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6329 typedef prpsinfo32_t elfcore_psinfo32_t
;
6333 #if defined (HAVE_PSINFO_T)
6334 typedef psinfo_t elfcore_psinfo_t
;
6335 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6336 typedef psinfo32_t elfcore_psinfo32_t
;
6340 /* return a malloc'ed copy of a string at START which is at
6341 most MAX bytes long, possibly without a terminating '\0'.
6342 the copy will always have a terminating '\0'. */
6345 _bfd_elfcore_strndup (abfd
, start
, max
)
6351 char *end
= memchr (start
, '\0', max
);
6359 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
6363 memcpy (dups
, start
, len
);
6369 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6370 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
6373 elfcore_grok_psinfo (abfd
, note
)
6375 Elf_Internal_Note
*note
;
6377 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6379 elfcore_psinfo_t psinfo
;
6381 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6383 elf_tdata (abfd
)->core_program
6384 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6385 sizeof (psinfo
.pr_fname
));
6387 elf_tdata (abfd
)->core_command
6388 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6389 sizeof (psinfo
.pr_psargs
));
6391 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6392 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6394 /* 64-bit host, 32-bit corefile */
6395 elfcore_psinfo32_t psinfo
;
6397 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6399 elf_tdata (abfd
)->core_program
6400 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6401 sizeof (psinfo
.pr_fname
));
6403 elf_tdata (abfd
)->core_command
6404 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6405 sizeof (psinfo
.pr_psargs
));
6411 /* Fail - we don't know how to handle any other
6412 note size (ie. data object type). */
6416 /* Note that for some reason, a spurious space is tacked
6417 onto the end of the args in some (at least one anyway)
6418 implementations, so strip it off if it exists. */
6421 char *command
= elf_tdata (abfd
)->core_command
;
6422 int n
= strlen (command
);
6424 if (0 < n
&& command
[n
- 1] == ' ')
6425 command
[n
- 1] = '\0';
6430 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6432 #if defined (HAVE_PSTATUS_T)
6433 static boolean elfcore_grok_pstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6436 elfcore_grok_pstatus (abfd
, note
)
6438 Elf_Internal_Note
*note
;
6440 if (note
->descsz
== sizeof (pstatus_t
)
6441 #if defined (HAVE_PXSTATUS_T)
6442 || note
->descsz
== sizeof (pxstatus_t
)
6448 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6450 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6452 #if defined (HAVE_PSTATUS32_T)
6453 else if (note
->descsz
== sizeof (pstatus32_t
))
6455 /* 64-bit host, 32-bit corefile */
6458 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6460 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6463 /* Could grab some more details from the "representative"
6464 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6465 NT_LWPSTATUS note, presumably. */
6469 #endif /* defined (HAVE_PSTATUS_T) */
6471 #if defined (HAVE_LWPSTATUS_T)
6472 static boolean elfcore_grok_lwpstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6475 elfcore_grok_lwpstatus (abfd
, note
)
6477 Elf_Internal_Note
*note
;
6479 lwpstatus_t lwpstat
;
6484 if (note
->descsz
!= sizeof (lwpstat
)
6485 #if defined (HAVE_LWPXSTATUS_T)
6486 && note
->descsz
!= sizeof (lwpxstatus_t
)
6491 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6493 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6494 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6496 /* Make a ".reg/999" section. */
6498 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6499 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6504 sect
= bfd_make_section (abfd
, name
);
6508 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6509 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6510 sect
->filepos
= note
->descpos
6511 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6514 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6515 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6516 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6519 sect
->flags
= SEC_HAS_CONTENTS
;
6520 sect
->alignment_power
= 2;
6522 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6525 /* Make a ".reg2/999" section */
6527 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6528 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6533 sect
= bfd_make_section (abfd
, name
);
6537 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6538 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6539 sect
->filepos
= note
->descpos
6540 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6543 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6544 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6545 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6548 sect
->flags
= SEC_HAS_CONTENTS
;
6549 sect
->alignment_power
= 2;
6551 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6553 #endif /* defined (HAVE_LWPSTATUS_T) */
6555 #if defined (HAVE_WIN32_PSTATUS_T)
6557 elfcore_grok_win32pstatus (abfd
, note
)
6559 Elf_Internal_Note
*note
;
6564 win32_pstatus_t pstatus
;
6566 if (note
->descsz
< sizeof (pstatus
))
6569 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
6571 switch (pstatus
.data_type
)
6573 case NOTE_INFO_PROCESS
:
6574 /* FIXME: need to add ->core_command. */
6575 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6576 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6579 case NOTE_INFO_THREAD
:
6580 /* Make a ".reg/999" section. */
6581 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6583 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6589 sect
= bfd_make_section (abfd
, name
);
6593 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6594 sect
->filepos
= (note
->descpos
6595 + offsetof (struct win32_pstatus
,
6596 data
.thread_info
.thread_context
));
6597 sect
->flags
= SEC_HAS_CONTENTS
;
6598 sect
->alignment_power
= 2;
6600 if (pstatus
.data
.thread_info
.is_active_thread
)
6601 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6605 case NOTE_INFO_MODULE
:
6606 /* Make a ".module/xxxxxxxx" section. */
6607 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6609 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6615 sect
= bfd_make_section (abfd
, name
);
6620 sect
->_raw_size
= note
->descsz
;
6621 sect
->filepos
= note
->descpos
;
6622 sect
->flags
= SEC_HAS_CONTENTS
;
6623 sect
->alignment_power
= 2;
6632 #endif /* HAVE_WIN32_PSTATUS_T */
6635 elfcore_grok_note (abfd
, note
)
6637 Elf_Internal_Note
*note
;
6639 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6647 if (bed
->elf_backend_grok_prstatus
)
6648 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6650 #if defined (HAVE_PRSTATUS_T)
6651 return elfcore_grok_prstatus (abfd
, note
);
6656 #if defined (HAVE_PSTATUS_T)
6658 return elfcore_grok_pstatus (abfd
, note
);
6661 #if defined (HAVE_LWPSTATUS_T)
6663 return elfcore_grok_lwpstatus (abfd
, note
);
6666 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6667 return elfcore_grok_prfpreg (abfd
, note
);
6669 #if defined (HAVE_WIN32_PSTATUS_T)
6670 case NT_WIN32PSTATUS
:
6671 return elfcore_grok_win32pstatus (abfd
, note
);
6674 case NT_PRXFPREG
: /* Linux SSE extension */
6675 if (note
->namesz
== 5
6676 && ! strcmp (note
->namedata
, "LINUX"))
6677 return elfcore_grok_prxfpreg (abfd
, note
);
6683 if (bed
->elf_backend_grok_psinfo
)
6684 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6686 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6687 return elfcore_grok_psinfo (abfd
, note
);
6695 elfcore_netbsd_get_lwpid (note
, lwpidp
)
6696 Elf_Internal_Note
*note
;
6701 cp
= strchr (note
->namedata
, '@');
6704 *lwpidp
= atoi(cp
+ 1);
6711 elfcore_grok_netbsd_procinfo (abfd
, note
)
6713 Elf_Internal_Note
*note
;
6716 /* Signal number at offset 0x08. */
6717 elf_tdata (abfd
)->core_signal
6718 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6720 /* Process ID at offset 0x50. */
6721 elf_tdata (abfd
)->core_pid
6722 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6724 /* Command name at 0x7c (max 32 bytes, including nul). */
6725 elf_tdata (abfd
)->core_command
6726 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6732 elfcore_grok_netbsd_note (abfd
, note
)
6734 Elf_Internal_Note
*note
;
6738 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6739 elf_tdata (abfd
)->core_lwpid
= lwp
;
6741 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6743 /* NetBSD-specific core "procinfo". Note that we expect to
6744 find this note before any of the others, which is fine,
6745 since the kernel writes this note out first when it
6746 creates a core file. */
6748 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6751 /* As of Jan 2002 there are no other machine-independent notes
6752 defined for NetBSD core files. If the note type is less
6753 than the start of the machine-dependent note types, we don't
6756 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
6760 switch (bfd_get_arch (abfd
))
6762 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6763 PT_GETFPREGS == mach+2. */
6765 case bfd_arch_alpha
:
6766 case bfd_arch_sparc
:
6769 case NT_NETBSDCORE_FIRSTMACH
+0:
6770 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6772 case NT_NETBSDCORE_FIRSTMACH
+2:
6773 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6779 /* On all other arch's, PT_GETREGS == mach+1 and
6780 PT_GETFPREGS == mach+3. */
6785 case NT_NETBSDCORE_FIRSTMACH
+1:
6786 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6788 case NT_NETBSDCORE_FIRSTMACH
+3:
6789 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6798 /* Function: elfcore_write_note
6805 size of data for note
6808 End of buffer containing note. */
6811 elfcore_write_note (abfd
, buf
, bufsiz
, name
, type
, input
, size
)
6820 Elf_External_Note
*xnp
;
6821 int namesz
= strlen (name
);
6822 int newspace
= BFD_ALIGN (sizeof (Elf_External_Note
) + size
+ namesz
- 1, 4);
6825 p
= realloc (buf
, *bufsiz
+ newspace
);
6827 *bufsiz
+= newspace
;
6828 xnp
= (Elf_External_Note
*) dest
;
6829 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
6830 H_PUT_32 (abfd
, size
, xnp
->descsz
);
6831 H_PUT_32 (abfd
, type
, xnp
->type
);
6832 strcpy (xnp
->name
, name
);
6833 memcpy (xnp
->name
+ BFD_ALIGN (namesz
, 4), input
, size
);
6837 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6839 elfcore_write_prpsinfo (abfd
, buf
, bufsiz
, fname
, psargs
)
6847 char *note_name
= "CORE";
6849 #if defined (HAVE_PSINFO_T)
6851 note_type
= NT_PSINFO
;
6854 note_type
= NT_PRPSINFO
;
6857 memset (&data
, 0, sizeof (data
));
6858 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
6859 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
6860 return elfcore_write_note (abfd
, buf
, bufsiz
,
6861 note_name
, note_type
, &data
, sizeof (data
));
6863 #endif /* PSINFO_T or PRPSINFO_T */
6865 #if defined (HAVE_PRSTATUS_T)
6867 elfcore_write_prstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6876 char *note_name
= "CORE";
6878 memset (&prstat
, 0, sizeof (prstat
));
6879 prstat
.pr_pid
= pid
;
6880 prstat
.pr_cursig
= cursig
;
6881 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
6882 return elfcore_write_note (abfd
, buf
, bufsiz
,
6883 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
6885 #endif /* HAVE_PRSTATUS_T */
6887 #if defined (HAVE_LWPSTATUS_T)
6889 elfcore_write_lwpstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6897 lwpstatus_t lwpstat
;
6898 char *note_name
= "CORE";
6900 memset (&lwpstat
, 0, sizeof (lwpstat
));
6901 lwpstat
.pr_lwpid
= pid
>> 16;
6902 lwpstat
.pr_cursig
= cursig
;
6903 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6904 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
6905 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6907 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
6908 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
6910 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
6911 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
6914 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
6915 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
6917 #endif /* HAVE_LWPSTATUS_T */
6919 #if defined (HAVE_PSTATUS_T)
6921 elfcore_write_pstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6930 char *note_name
= "CORE";
6932 memset (&pstat
, 0, sizeof (pstat
));
6933 pstat
.pr_pid
= pid
& 0xffff;
6934 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
6935 NT_PSTATUS
, &pstat
, sizeof (pstat
));
6938 #endif /* HAVE_PSTATUS_T */
6941 elfcore_write_prfpreg (abfd
, buf
, bufsiz
, fpregs
, size
)
6948 char *note_name
= "CORE";
6949 return elfcore_write_note (abfd
, buf
, bufsiz
,
6950 note_name
, NT_FPREGSET
, fpregs
, size
);
6954 elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, xfpregs
, size
)
6961 char *note_name
= "LINUX";
6962 return elfcore_write_note (abfd
, buf
, bufsiz
,
6963 note_name
, NT_PRXFPREG
, xfpregs
, size
);
6967 elfcore_read_notes (abfd
, offset
, size
)
6978 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
6981 buf
= bfd_malloc (size
);
6985 if (bfd_bread (buf
, size
, abfd
) != size
)
6993 while (p
< buf
+ size
)
6995 /* FIXME: bad alignment assumption. */
6996 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
6997 Elf_Internal_Note in
;
6999 in
.type
= H_GET_32 (abfd
, xnp
->type
);
7001 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
7002 in
.namedata
= xnp
->name
;
7004 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
7005 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
7006 in
.descpos
= offset
+ (in
.descdata
- buf
);
7008 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
7010 if (! elfcore_grok_netbsd_note (abfd
, &in
))
7015 if (! elfcore_grok_note (abfd
, &in
))
7019 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
7026 /* Providing external access to the ELF program header table. */
7028 /* Return an upper bound on the number of bytes required to store a
7029 copy of ABFD's program header table entries. Return -1 if an error
7030 occurs; bfd_get_error will return an appropriate code. */
7033 bfd_get_elf_phdr_upper_bound (abfd
)
7036 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7038 bfd_set_error (bfd_error_wrong_format
);
7042 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
7045 /* Copy ABFD's program header table entries to *PHDRS. The entries
7046 will be stored as an array of Elf_Internal_Phdr structures, as
7047 defined in include/elf/internal.h. To find out how large the
7048 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7050 Return the number of program header table entries read, or -1 if an
7051 error occurs; bfd_get_error will return an appropriate code. */
7054 bfd_get_elf_phdrs (abfd
, phdrs
)
7060 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7062 bfd_set_error (bfd_error_wrong_format
);
7066 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
7067 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
7068 num_phdrs
* sizeof (Elf_Internal_Phdr
));
7074 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
7075 bfd
*abfd ATTRIBUTE_UNUSED
;
7080 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7082 i_ehdrp
= elf_elfheader (abfd
);
7083 if (i_ehdrp
== NULL
)
7084 sprintf_vma (buf
, value
);
7087 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7089 #if BFD_HOST_64BIT_LONG
7090 sprintf (buf
, "%016lx", value
);
7092 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
7093 _bfd_int64_low (value
));
7097 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
7100 sprintf_vma (buf
, value
);
7105 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
7106 bfd
*abfd ATTRIBUTE_UNUSED
;
7111 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7113 i_ehdrp
= elf_elfheader (abfd
);
7114 if (i_ehdrp
== NULL
)
7115 fprintf_vma ((FILE *) stream
, value
);
7118 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7120 #if BFD_HOST_64BIT_LONG
7121 fprintf ((FILE *) stream
, "%016lx", value
);
7123 fprintf ((FILE *) stream
, "%08lx%08lx",
7124 _bfd_int64_high (value
), _bfd_int64_low (value
));
7128 fprintf ((FILE *) stream
, "%08lx",
7129 (unsigned long) (value
& 0xffffffff));
7132 fprintf_vma ((FILE *) stream
, value
);
7136 enum elf_reloc_type_class
7137 _bfd_elf_reloc_type_class (rela
)
7138 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
7140 return reloc_class_normal
;
7143 /* For RELA architectures, return what the relocation value for
7144 relocation against a local symbol. */
7147 _bfd_elf_rela_local_sym (abfd
, sym
, sec
, rel
)
7149 Elf_Internal_Sym
*sym
;
7151 Elf_Internal_Rela
*rel
;
7155 relocation
= (sec
->output_section
->vma
7156 + sec
->output_offset
7158 if ((sec
->flags
& SEC_MERGE
)
7159 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
7160 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
7166 _bfd_merged_section_offset (abfd
, &msec
,
7167 elf_section_data (sec
)->sec_info
,
7168 sym
->st_value
+ rel
->r_addend
,
7171 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
7177 _bfd_elf_rel_local_sym (abfd
, sym
, psec
, addend
)
7179 Elf_Internal_Sym
*sym
;
7183 asection
*sec
= *psec
;
7185 if (elf_section_data (sec
)->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7186 return sym
->st_value
+ addend
;
7188 return _bfd_merged_section_offset (abfd
, psec
,
7189 elf_section_data (sec
)->sec_info
,
7190 sym
->st_value
+ addend
, (bfd_vma
) 0);
7194 _bfd_elf_section_offset (abfd
, info
, sec
, offset
)
7196 struct bfd_link_info
*info
;
7200 struct bfd_elf_section_data
*sec_data
;
7202 sec_data
= elf_section_data (sec
);
7203 switch (sec_data
->sec_info_type
)
7205 case ELF_INFO_TYPE_STABS
:
7206 return _bfd_stab_section_offset
7207 (abfd
, &elf_hash_table (info
)->merge_info
, sec
, &sec_data
->sec_info
,
7209 case ELF_INFO_TYPE_EH_FRAME
:
7210 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);