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 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4692 to work around this long long bug. */
4693 amt
= section_count
* sizeof (asection
*);
4694 sections
= (asection
**) bfd_malloc (amt
);
4695 if (sections
== NULL
)
4698 /* Step One: Scan for segment vs section LMA conflicts.
4699 Also add the sections to the section array allocated above.
4700 Also add the sections to the current segment. In the common
4701 case, where the sections have not been moved, this means that
4702 we have completely filled the segment, and there is nothing
4708 for (j
= 0, section
= ibfd
->sections
;
4710 section
= section
->next
)
4712 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4714 output_section
= section
->output_section
;
4716 sections
[j
++] = section
;
4718 /* The Solaris native linker always sets p_paddr to 0.
4719 We try to catch that case here, and set it to the
4721 if (segment
->p_paddr
== 0
4722 && segment
->p_vaddr
!= 0
4724 && output_section
->lma
!= 0
4725 && (output_section
->vma
== (segment
->p_vaddr
4726 + (map
->includes_filehdr
4729 + (map
->includes_phdrs
4731 * iehdr
->e_phentsize
)
4733 map
->p_paddr
= segment
->p_vaddr
;
4735 /* Match up the physical address of the segment with the
4736 LMA address of the output section. */
4737 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4738 || IS_CONTAINED_BY_FILEPOS (section
, segment
, bed
)
4739 || IS_COREFILE_NOTE (segment
, section
))
4741 if (matching_lma
== 0)
4742 matching_lma
= output_section
->lma
;
4744 /* We assume that if the section fits within the segment
4745 then it does not overlap any other section within that
4747 map
->sections
[isec
++] = output_section
;
4749 else if (suggested_lma
== 0)
4750 suggested_lma
= output_section
->lma
;
4754 BFD_ASSERT (j
== section_count
);
4756 /* Step Two: Adjust the physical address of the current segment,
4758 if (isec
== section_count
)
4760 /* All of the sections fitted within the segment as currently
4761 specified. This is the default case. Add the segment to
4762 the list of built segments and carry on to process the next
4763 program header in the input BFD. */
4764 map
->count
= section_count
;
4765 *pointer_to_map
= map
;
4766 pointer_to_map
= &map
->next
;
4773 if (matching_lma
!= 0)
4775 /* At least one section fits inside the current segment.
4776 Keep it, but modify its physical address to match the
4777 LMA of the first section that fitted. */
4778 map
->p_paddr
= matching_lma
;
4782 /* None of the sections fitted inside the current segment.
4783 Change the current segment's physical address to match
4784 the LMA of the first section. */
4785 map
->p_paddr
= suggested_lma
;
4788 /* Offset the segment physical address from the lma
4789 to allow for space taken up by elf headers. */
4790 if (map
->includes_filehdr
)
4791 map
->p_paddr
-= iehdr
->e_ehsize
;
4793 if (map
->includes_phdrs
)
4795 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4797 /* iehdr->e_phnum is just an estimate of the number
4798 of program headers that we will need. Make a note
4799 here of the number we used and the segment we chose
4800 to hold these headers, so that we can adjust the
4801 offset when we know the correct value. */
4802 phdr_adjust_num
= iehdr
->e_phnum
;
4803 phdr_adjust_seg
= map
;
4807 /* Step Three: Loop over the sections again, this time assigning
4808 those that fit to the current segment and removing them from the
4809 sections array; but making sure not to leave large gaps. Once all
4810 possible sections have been assigned to the current segment it is
4811 added to the list of built segments and if sections still remain
4812 to be assigned, a new segment is constructed before repeating
4820 /* Fill the current segment with sections that fit. */
4821 for (j
= 0; j
< section_count
; j
++)
4823 section
= sections
[j
];
4825 if (section
== NULL
)
4828 output_section
= section
->output_section
;
4830 BFD_ASSERT (output_section
!= NULL
);
4832 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4833 || IS_COREFILE_NOTE (segment
, section
))
4835 if (map
->count
== 0)
4837 /* If the first section in a segment does not start at
4838 the beginning of the segment, then something is
4840 if (output_section
->lma
!=
4842 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4843 + (map
->includes_phdrs
4844 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4850 asection
* prev_sec
;
4852 prev_sec
= map
->sections
[map
->count
- 1];
4854 /* If the gap between the end of the previous section
4855 and the start of this section is more than
4856 maxpagesize then we need to start a new segment. */
4857 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
4859 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4860 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
4861 > output_section
->lma
))
4863 if (suggested_lma
== 0)
4864 suggested_lma
= output_section
->lma
;
4870 map
->sections
[map
->count
++] = output_section
;
4873 section
->segment_mark
= true;
4875 else if (suggested_lma
== 0)
4876 suggested_lma
= output_section
->lma
;
4879 BFD_ASSERT (map
->count
> 0);
4881 /* Add the current segment to the list of built segments. */
4882 *pointer_to_map
= map
;
4883 pointer_to_map
= &map
->next
;
4885 if (isec
< section_count
)
4887 /* We still have not allocated all of the sections to
4888 segments. Create a new segment here, initialise it
4889 and carry on looping. */
4890 amt
= sizeof (struct elf_segment_map
);
4891 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4892 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4896 /* Initialise the fields of the segment map. Set the physical
4897 physical address to the LMA of the first section that has
4898 not yet been assigned. */
4900 map
->p_type
= segment
->p_type
;
4901 map
->p_flags
= segment
->p_flags
;
4902 map
->p_flags_valid
= 1;
4903 map
->p_paddr
= suggested_lma
;
4904 map
->p_paddr_valid
= 1;
4905 map
->includes_filehdr
= 0;
4906 map
->includes_phdrs
= 0;
4909 while (isec
< section_count
);
4914 /* The Solaris linker creates program headers in which all the
4915 p_paddr fields are zero. When we try to objcopy or strip such a
4916 file, we get confused. Check for this case, and if we find it
4917 reset the p_paddr_valid fields. */
4918 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4919 if (map
->p_paddr
!= 0)
4923 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4924 map
->p_paddr_valid
= 0;
4927 elf_tdata (obfd
)->segment_map
= map_first
;
4929 /* If we had to estimate the number of program headers that were
4930 going to be needed, then check our estimate now and adjust
4931 the offset if necessary. */
4932 if (phdr_adjust_seg
!= NULL
)
4936 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4939 if (count
> phdr_adjust_num
)
4940 phdr_adjust_seg
->p_paddr
4941 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4945 /* Final Step: Sort the segments into ascending order of physical
4947 if (map_first
!= NULL
)
4949 struct elf_segment_map
*prev
;
4952 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4954 /* Yes I know - its a bubble sort.... */
4955 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4957 /* Swap map and map->next. */
4958 prev
->next
= map
->next
;
4959 map
->next
= map
->next
->next
;
4960 prev
->next
->next
= map
;
4970 #undef IS_CONTAINED_BY_VMA
4971 #undef IS_CONTAINED_BY_LMA
4972 #undef IS_CONTAINED_BY_FILEPOS
4973 #undef IS_COREFILE_NOTE
4974 #undef IS_SOLARIS_PT_INTERP
4975 #undef INCLUDE_SECTION_IN_SEGMENT
4976 #undef SEGMENT_AFTER_SEGMENT
4977 #undef SEGMENT_OVERLAPS
4981 /* Copy private section information. This copies over the entsize
4982 field, and sometimes the info field. */
4985 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4991 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4992 const struct elf_backend_data
*bed
= get_elf_backend_data (ibfd
);
4994 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4995 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4998 /* Copy over private BFD data if it has not already been copied.
4999 This must be done here, rather than in the copy_private_bfd_data
5000 entry point, because the latter is called after the section
5001 contents have been set, which means that the program headers have
5002 already been worked out. The backend function provides a way to
5003 override the test conditions and code path for the call to
5004 copy_private_bfd_data. */
5005 if (bed
->copy_private_bfd_data_p
)
5007 if ((*bed
->copy_private_bfd_data_p
) (ibfd
, isec
, obfd
, osec
))
5008 if (! copy_private_bfd_data (ibfd
, obfd
))
5011 else if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
5015 /* Only set up the segments if there are no more SEC_ALLOC
5016 sections. FIXME: This won't do the right thing if objcopy is
5017 used to remove the last SEC_ALLOC section, since objcopy
5018 won't call this routine in that case. */
5019 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
5020 if ((s
->flags
& SEC_ALLOC
) != 0)
5024 if (! copy_private_bfd_data (ibfd
, obfd
))
5029 ihdr
= &elf_section_data (isec
)->this_hdr
;
5030 ohdr
= &elf_section_data (osec
)->this_hdr
;
5032 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
5034 if (ihdr
->sh_type
== SHT_SYMTAB
5035 || ihdr
->sh_type
== SHT_DYNSYM
5036 || ihdr
->sh_type
== SHT_GNU_verneed
5037 || ihdr
->sh_type
== SHT_GNU_verdef
)
5038 ohdr
->sh_info
= ihdr
->sh_info
;
5040 /* Set things up for objcopy. The output SHT_GROUP section will
5041 have its elf_next_in_group pointing back to the input group
5043 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5044 elf_group_name (osec
) = elf_group_name (isec
);
5046 elf_section_data (osec
)->use_rela_p
5047 = elf_section_data (isec
)->use_rela_p
;
5052 /* Copy private symbol information. If this symbol is in a section
5053 which we did not map into a BFD section, try to map the section
5054 index correctly. We use special macro definitions for the mapped
5055 section indices; these definitions are interpreted by the
5056 swap_out_syms function. */
5058 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5059 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5060 #define MAP_STRTAB (SHN_HIOS + 3)
5061 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5062 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5065 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
5071 elf_symbol_type
*isym
, *osym
;
5073 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5074 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5077 isym
= elf_symbol_from (ibfd
, isymarg
);
5078 osym
= elf_symbol_from (obfd
, osymarg
);
5082 && bfd_is_abs_section (isym
->symbol
.section
))
5086 shndx
= isym
->internal_elf_sym
.st_shndx
;
5087 if (shndx
== elf_onesymtab (ibfd
))
5088 shndx
= MAP_ONESYMTAB
;
5089 else if (shndx
== elf_dynsymtab (ibfd
))
5090 shndx
= MAP_DYNSYMTAB
;
5091 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
5093 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
5094 shndx
= MAP_SHSTRTAB
;
5095 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
5096 shndx
= MAP_SYM_SHNDX
;
5097 osym
->internal_elf_sym
.st_shndx
= shndx
;
5103 /* Swap out the symbols. */
5106 swap_out_syms (abfd
, sttp
, relocatable_p
)
5108 struct bfd_strtab_hash
**sttp
;
5111 struct elf_backend_data
*bed
;
5114 struct bfd_strtab_hash
*stt
;
5115 Elf_Internal_Shdr
*symtab_hdr
;
5116 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5117 Elf_Internal_Shdr
*symstrtab_hdr
;
5118 char *outbound_syms
;
5119 char *outbound_shndx
;
5123 if (!elf_map_symbols (abfd
))
5126 /* Dump out the symtabs. */
5127 stt
= _bfd_elf_stringtab_init ();
5131 bed
= get_elf_backend_data (abfd
);
5132 symcount
= bfd_get_symcount (abfd
);
5133 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5134 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5135 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5136 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5137 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5138 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5140 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5141 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5143 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
5144 outbound_syms
= bfd_alloc (abfd
, amt
);
5145 if (outbound_syms
== NULL
)
5147 symtab_hdr
->contents
= (PTR
) outbound_syms
;
5149 outbound_shndx
= NULL
;
5150 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5151 if (symtab_shndx_hdr
->sh_name
!= 0)
5153 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5154 outbound_shndx
= bfd_zalloc (abfd
, amt
);
5155 if (outbound_shndx
== NULL
)
5157 symtab_shndx_hdr
->contents
= outbound_shndx
;
5158 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5159 symtab_shndx_hdr
->sh_size
= amt
;
5160 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5161 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5164 /* now generate the data (for "contents") */
5166 /* Fill in zeroth symbol and swap it out. */
5167 Elf_Internal_Sym sym
;
5173 sym
.st_shndx
= SHN_UNDEF
;
5174 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5175 outbound_syms
+= bed
->s
->sizeof_sym
;
5176 if (outbound_shndx
!= NULL
)
5177 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5180 syms
= bfd_get_outsymbols (abfd
);
5181 for (idx
= 0; idx
< symcount
; idx
++)
5183 Elf_Internal_Sym sym
;
5184 bfd_vma value
= syms
[idx
]->value
;
5185 elf_symbol_type
*type_ptr
;
5186 flagword flags
= syms
[idx
]->flags
;
5189 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5191 /* Local section symbols have no name. */
5196 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5199 if (sym
.st_name
== (unsigned long) -1)
5203 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5205 if ((flags
& BSF_SECTION_SYM
) == 0
5206 && bfd_is_com_section (syms
[idx
]->section
))
5208 /* ELF common symbols put the alignment into the `value' field,
5209 and the size into the `size' field. This is backwards from
5210 how BFD handles it, so reverse it here. */
5211 sym
.st_size
= value
;
5212 if (type_ptr
== NULL
5213 || type_ptr
->internal_elf_sym
.st_value
== 0)
5214 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5216 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5217 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5218 (abfd
, syms
[idx
]->section
);
5222 asection
*sec
= syms
[idx
]->section
;
5225 if (sec
->output_section
)
5227 value
+= sec
->output_offset
;
5228 sec
= sec
->output_section
;
5230 /* Don't add in the section vma for relocatable output. */
5231 if (! relocatable_p
)
5233 sym
.st_value
= value
;
5234 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5236 if (bfd_is_abs_section (sec
)
5238 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5240 /* This symbol is in a real ELF section which we did
5241 not create as a BFD section. Undo the mapping done
5242 by copy_private_symbol_data. */
5243 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5247 shndx
= elf_onesymtab (abfd
);
5250 shndx
= elf_dynsymtab (abfd
);
5253 shndx
= elf_tdata (abfd
)->strtab_section
;
5256 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5259 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5267 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5273 /* Writing this would be a hell of a lot easier if
5274 we had some decent documentation on bfd, and
5275 knew what to expect of the library, and what to
5276 demand of applications. For example, it
5277 appears that `objcopy' might not set the
5278 section of a symbol to be a section that is
5279 actually in the output file. */
5280 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5281 BFD_ASSERT (sec2
!= 0);
5282 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5283 BFD_ASSERT (shndx
!= -1);
5287 sym
.st_shndx
= shndx
;
5290 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5292 else if ((flags
& BSF_FUNCTION
) != 0)
5294 else if ((flags
& BSF_OBJECT
) != 0)
5299 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5302 /* Processor-specific types */
5303 if (type_ptr
!= NULL
5304 && bed
->elf_backend_get_symbol_type
)
5305 type
= ((*bed
->elf_backend_get_symbol_type
)
5306 (&type_ptr
->internal_elf_sym
, type
));
5308 if (flags
& BSF_SECTION_SYM
)
5310 if (flags
& BSF_GLOBAL
)
5311 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5313 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5315 else if (bfd_is_com_section (syms
[idx
]->section
))
5316 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5317 else if (bfd_is_und_section (syms
[idx
]->section
))
5318 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5322 else if (flags
& BSF_FILE
)
5323 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5326 int bind
= STB_LOCAL
;
5328 if (flags
& BSF_LOCAL
)
5330 else if (flags
& BSF_WEAK
)
5332 else if (flags
& BSF_GLOBAL
)
5335 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5338 if (type_ptr
!= NULL
)
5339 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5343 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5344 outbound_syms
+= bed
->s
->sizeof_sym
;
5345 if (outbound_shndx
!= NULL
)
5346 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5350 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5351 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5353 symstrtab_hdr
->sh_flags
= 0;
5354 symstrtab_hdr
->sh_addr
= 0;
5355 symstrtab_hdr
->sh_entsize
= 0;
5356 symstrtab_hdr
->sh_link
= 0;
5357 symstrtab_hdr
->sh_info
= 0;
5358 symstrtab_hdr
->sh_addralign
= 1;
5363 /* Return the number of bytes required to hold the symtab vector.
5365 Note that we base it on the count plus 1, since we will null terminate
5366 the vector allocated based on this size. However, the ELF symbol table
5367 always has a dummy entry as symbol #0, so it ends up even. */
5370 _bfd_elf_get_symtab_upper_bound (abfd
)
5375 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5377 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5378 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5380 symtab_size
-= sizeof (asymbol
*);
5386 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
5391 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5393 if (elf_dynsymtab (abfd
) == 0)
5395 bfd_set_error (bfd_error_invalid_operation
);
5399 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5400 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5402 symtab_size
-= sizeof (asymbol
*);
5408 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
5409 bfd
*abfd ATTRIBUTE_UNUSED
;
5412 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5415 /* Canonicalize the relocs. */
5418 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
5426 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5428 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
5431 tblptr
= section
->relocation
;
5432 for (i
= 0; i
< section
->reloc_count
; i
++)
5433 *relptr
++ = tblptr
++;
5437 return section
->reloc_count
;
5441 _bfd_elf_get_symtab (abfd
, alocation
)
5443 asymbol
**alocation
;
5445 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5446 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, false);
5449 bfd_get_symcount (abfd
) = symcount
;
5454 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
5456 asymbol
**alocation
;
5458 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5459 return bed
->s
->slurp_symbol_table (abfd
, alocation
, true);
5462 /* Return the size required for the dynamic reloc entries. Any
5463 section that was actually installed in the BFD, and has type
5464 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5465 considered to be a dynamic reloc section. */
5468 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
5474 if (elf_dynsymtab (abfd
) == 0)
5476 bfd_set_error (bfd_error_invalid_operation
);
5480 ret
= sizeof (arelent
*);
5481 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5482 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5483 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5484 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5485 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5486 * sizeof (arelent
*));
5491 /* Canonicalize the dynamic relocation entries. Note that we return
5492 the dynamic relocations as a single block, although they are
5493 actually associated with particular sections; the interface, which
5494 was designed for SunOS style shared libraries, expects that there
5495 is only one set of dynamic relocs. Any section that was actually
5496 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5497 the dynamic symbol table, is considered to be a dynamic reloc
5501 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
5506 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
5510 if (elf_dynsymtab (abfd
) == 0)
5512 bfd_set_error (bfd_error_invalid_operation
);
5516 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5518 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5520 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5521 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5522 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5527 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
5529 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5531 for (i
= 0; i
< count
; i
++)
5542 /* Read in the version information. */
5545 _bfd_elf_slurp_version_tables (abfd
)
5548 bfd_byte
*contents
= NULL
;
5551 if (elf_dynverdef (abfd
) != 0)
5553 Elf_Internal_Shdr
*hdr
;
5554 Elf_External_Verdef
*everdef
;
5555 Elf_Internal_Verdef
*iverdef
;
5556 Elf_Internal_Verdef
*iverdefarr
;
5557 Elf_Internal_Verdef iverdefmem
;
5559 unsigned int maxidx
;
5561 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5563 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5564 if (contents
== NULL
)
5566 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5567 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5570 /* We know the number of entries in the section but not the maximum
5571 index. Therefore we have to run through all entries and find
5573 everdef
= (Elf_External_Verdef
*) contents
;
5575 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5577 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5579 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5580 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5582 everdef
= ((Elf_External_Verdef
*)
5583 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5586 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5587 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
5588 if (elf_tdata (abfd
)->verdef
== NULL
)
5591 elf_tdata (abfd
)->cverdefs
= maxidx
;
5593 everdef
= (Elf_External_Verdef
*) contents
;
5594 iverdefarr
= elf_tdata (abfd
)->verdef
;
5595 for (i
= 0; i
< hdr
->sh_info
; i
++)
5597 Elf_External_Verdaux
*everdaux
;
5598 Elf_Internal_Verdaux
*iverdaux
;
5601 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5603 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5604 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5606 iverdef
->vd_bfd
= abfd
;
5608 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5609 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
5610 if (iverdef
->vd_auxptr
== NULL
)
5613 everdaux
= ((Elf_External_Verdaux
*)
5614 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5615 iverdaux
= iverdef
->vd_auxptr
;
5616 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5618 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5620 iverdaux
->vda_nodename
=
5621 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5622 iverdaux
->vda_name
);
5623 if (iverdaux
->vda_nodename
== NULL
)
5626 if (j
+ 1 < iverdef
->vd_cnt
)
5627 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5629 iverdaux
->vda_nextptr
= NULL
;
5631 everdaux
= ((Elf_External_Verdaux
*)
5632 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5635 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5637 if (i
+ 1 < hdr
->sh_info
)
5638 iverdef
->vd_nextdef
= iverdef
+ 1;
5640 iverdef
->vd_nextdef
= NULL
;
5642 everdef
= ((Elf_External_Verdef
*)
5643 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5650 if (elf_dynverref (abfd
) != 0)
5652 Elf_Internal_Shdr
*hdr
;
5653 Elf_External_Verneed
*everneed
;
5654 Elf_Internal_Verneed
*iverneed
;
5657 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5659 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5660 elf_tdata (abfd
)->verref
=
5661 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
5662 if (elf_tdata (abfd
)->verref
== NULL
)
5665 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5667 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5668 if (contents
== NULL
)
5670 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5671 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5674 everneed
= (Elf_External_Verneed
*) contents
;
5675 iverneed
= elf_tdata (abfd
)->verref
;
5676 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5678 Elf_External_Vernaux
*evernaux
;
5679 Elf_Internal_Vernaux
*ivernaux
;
5682 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5684 iverneed
->vn_bfd
= abfd
;
5686 iverneed
->vn_filename
=
5687 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5689 if (iverneed
->vn_filename
== NULL
)
5692 amt
= iverneed
->vn_cnt
;
5693 amt
*= sizeof (Elf_Internal_Vernaux
);
5694 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
5696 evernaux
= ((Elf_External_Vernaux
*)
5697 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5698 ivernaux
= iverneed
->vn_auxptr
;
5699 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5701 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5703 ivernaux
->vna_nodename
=
5704 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5705 ivernaux
->vna_name
);
5706 if (ivernaux
->vna_nodename
== NULL
)
5709 if (j
+ 1 < iverneed
->vn_cnt
)
5710 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5712 ivernaux
->vna_nextptr
= NULL
;
5714 evernaux
= ((Elf_External_Vernaux
*)
5715 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5718 if (i
+ 1 < hdr
->sh_info
)
5719 iverneed
->vn_nextref
= iverneed
+ 1;
5721 iverneed
->vn_nextref
= NULL
;
5723 everneed
= ((Elf_External_Verneed
*)
5724 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5734 if (contents
== NULL
)
5740 _bfd_elf_make_empty_symbol (abfd
)
5743 elf_symbol_type
*newsym
;
5744 bfd_size_type amt
= sizeof (elf_symbol_type
);
5746 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
5751 newsym
->symbol
.the_bfd
= abfd
;
5752 return &newsym
->symbol
;
5757 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
5758 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5762 bfd_symbol_info (symbol
, ret
);
5765 /* Return whether a symbol name implies a local symbol. Most targets
5766 use this function for the is_local_label_name entry point, but some
5770 _bfd_elf_is_local_label_name (abfd
, name
)
5771 bfd
*abfd ATTRIBUTE_UNUSED
;
5774 /* Normal local symbols start with ``.L''. */
5775 if (name
[0] == '.' && name
[1] == 'L')
5778 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5779 DWARF debugging symbols starting with ``..''. */
5780 if (name
[0] == '.' && name
[1] == '.')
5783 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5784 emitting DWARF debugging output. I suspect this is actually a
5785 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5786 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5787 underscore to be emitted on some ELF targets). For ease of use,
5788 we treat such symbols as local. */
5789 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5796 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5797 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5798 asymbol
*symbol ATTRIBUTE_UNUSED
;
5805 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5807 enum bfd_architecture arch
;
5808 unsigned long machine
;
5810 /* If this isn't the right architecture for this backend, and this
5811 isn't the generic backend, fail. */
5812 if (arch
!= get_elf_backend_data (abfd
)->arch
5813 && arch
!= bfd_arch_unknown
5814 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5817 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5820 /* Find the function to a particular section and offset,
5821 for error reporting. */
5824 elf_find_function (abfd
, section
, symbols
, offset
,
5825 filename_ptr
, functionname_ptr
)
5826 bfd
*abfd ATTRIBUTE_UNUSED
;
5830 const char **filename_ptr
;
5831 const char **functionname_ptr
;
5833 const char *filename
;
5842 for (p
= symbols
; *p
!= NULL
; p
++)
5846 q
= (elf_symbol_type
*) *p
;
5848 if (bfd_get_section (&q
->symbol
) != section
)
5851 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5856 filename
= bfd_asymbol_name (&q
->symbol
);
5860 if (q
->symbol
.section
== section
5861 && q
->symbol
.value
>= low_func
5862 && q
->symbol
.value
<= offset
)
5864 func
= (asymbol
*) q
;
5865 low_func
= q
->symbol
.value
;
5875 *filename_ptr
= filename
;
5876 if (functionname_ptr
)
5877 *functionname_ptr
= bfd_asymbol_name (func
);
5882 /* Find the nearest line to a particular section and offset,
5883 for error reporting. */
5886 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5887 filename_ptr
, functionname_ptr
, line_ptr
)
5892 const char **filename_ptr
;
5893 const char **functionname_ptr
;
5894 unsigned int *line_ptr
;
5898 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5899 filename_ptr
, functionname_ptr
,
5902 if (!*functionname_ptr
)
5903 elf_find_function (abfd
, section
, symbols
, offset
,
5904 *filename_ptr
? NULL
: filename_ptr
,
5910 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5911 filename_ptr
, functionname_ptr
,
5913 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5915 if (!*functionname_ptr
)
5916 elf_find_function (abfd
, section
, symbols
, offset
,
5917 *filename_ptr
? NULL
: filename_ptr
,
5923 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5924 &found
, filename_ptr
,
5925 functionname_ptr
, line_ptr
,
5926 &elf_tdata (abfd
)->line_info
))
5931 if (symbols
== NULL
)
5934 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5935 filename_ptr
, functionname_ptr
))
5943 _bfd_elf_sizeof_headers (abfd
, reloc
)
5949 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5951 ret
+= get_program_header_size (abfd
);
5956 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5961 bfd_size_type count
;
5963 Elf_Internal_Shdr
*hdr
;
5966 if (! abfd
->output_has_begun
5967 && ! _bfd_elf_compute_section_file_positions
5968 (abfd
, (struct bfd_link_info
*) NULL
))
5971 hdr
= &elf_section_data (section
)->this_hdr
;
5972 pos
= hdr
->sh_offset
+ offset
;
5973 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5974 || bfd_bwrite (location
, count
, abfd
) != count
)
5981 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5982 bfd
*abfd ATTRIBUTE_UNUSED
;
5983 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5984 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5991 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5994 Elf_Internal_Rel
*dst
;
6000 /* Try to convert a non-ELF reloc into an ELF one. */
6003 _bfd_elf_validate_reloc (abfd
, areloc
)
6007 /* Check whether we really have an ELF howto. */
6009 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
6011 bfd_reloc_code_real_type code
;
6012 reloc_howto_type
*howto
;
6014 /* Alien reloc: Try to determine its type to replace it with an
6015 equivalent ELF reloc. */
6017 if (areloc
->howto
->pc_relative
)
6019 switch (areloc
->howto
->bitsize
)
6022 code
= BFD_RELOC_8_PCREL
;
6025 code
= BFD_RELOC_12_PCREL
;
6028 code
= BFD_RELOC_16_PCREL
;
6031 code
= BFD_RELOC_24_PCREL
;
6034 code
= BFD_RELOC_32_PCREL
;
6037 code
= BFD_RELOC_64_PCREL
;
6043 howto
= bfd_reloc_type_lookup (abfd
, code
);
6045 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
6047 if (howto
->pcrel_offset
)
6048 areloc
->addend
+= areloc
->address
;
6050 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
6055 switch (areloc
->howto
->bitsize
)
6061 code
= BFD_RELOC_14
;
6064 code
= BFD_RELOC_16
;
6067 code
= BFD_RELOC_26
;
6070 code
= BFD_RELOC_32
;
6073 code
= BFD_RELOC_64
;
6079 howto
= bfd_reloc_type_lookup (abfd
, code
);
6083 areloc
->howto
= howto
;
6091 (*_bfd_error_handler
)
6092 (_("%s: unsupported relocation type %s"),
6093 bfd_archive_filename (abfd
), areloc
->howto
->name
);
6094 bfd_set_error (bfd_error_bad_value
);
6099 _bfd_elf_close_and_cleanup (abfd
)
6102 if (bfd_get_format (abfd
) == bfd_object
)
6104 if (elf_shstrtab (abfd
) != NULL
)
6105 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
6108 return _bfd_generic_close_and_cleanup (abfd
);
6111 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6112 in the relocation's offset. Thus we cannot allow any sort of sanity
6113 range-checking to interfere. There is nothing else to do in processing
6116 bfd_reloc_status_type
6117 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
6118 bfd
*abfd ATTRIBUTE_UNUSED
;
6119 arelent
*re ATTRIBUTE_UNUSED
;
6120 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
6121 PTR data ATTRIBUTE_UNUSED
;
6122 asection
*is ATTRIBUTE_UNUSED
;
6123 bfd
*obfd ATTRIBUTE_UNUSED
;
6124 char **errmsg ATTRIBUTE_UNUSED
;
6126 return bfd_reloc_ok
;
6129 /* Elf core file support. Much of this only works on native
6130 toolchains, since we rely on knowing the
6131 machine-dependent procfs structure in order to pick
6132 out details about the corefile. */
6134 #ifdef HAVE_SYS_PROCFS_H
6135 # include <sys/procfs.h>
6138 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6141 elfcore_make_pid (abfd
)
6144 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
6145 + (elf_tdata (abfd
)->core_pid
));
6148 /* If there isn't a section called NAME, make one, using
6149 data from SECT. Note, this function will generate a
6150 reference to NAME, so you shouldn't deallocate or
6154 elfcore_maybe_make_sect (abfd
, name
, sect
)
6161 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
6164 sect2
= bfd_make_section (abfd
, name
);
6168 sect2
->_raw_size
= sect
->_raw_size
;
6169 sect2
->filepos
= sect
->filepos
;
6170 sect2
->flags
= sect
->flags
;
6171 sect2
->alignment_power
= sect
->alignment_power
;
6175 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6176 actually creates up to two pseudosections:
6177 - For the single-threaded case, a section named NAME, unless
6178 such a section already exists.
6179 - For the multi-threaded case, a section named "NAME/PID", where
6180 PID is elfcore_make_pid (abfd).
6181 Both pseudosections have identical contents. */
6183 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
6190 char *threaded_name
;
6193 /* Build the section name. */
6195 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6196 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6197 if (threaded_name
== NULL
)
6199 strcpy (threaded_name
, buf
);
6201 sect
= bfd_make_section (abfd
, threaded_name
);
6204 sect
->_raw_size
= size
;
6205 sect
->filepos
= filepos
;
6206 sect
->flags
= SEC_HAS_CONTENTS
;
6207 sect
->alignment_power
= 2;
6209 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6212 /* prstatus_t exists on:
6214 linux 2.[01] + glibc
6218 #if defined (HAVE_PRSTATUS_T)
6219 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6222 elfcore_grok_prstatus (abfd
, note
)
6224 Elf_Internal_Note
*note
;
6229 if (note
->descsz
== sizeof (prstatus_t
))
6233 raw_size
= sizeof (prstat
.pr_reg
);
6234 offset
= offsetof (prstatus_t
, pr_reg
);
6235 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6237 /* Do not overwrite the core signal if it
6238 has already been set by another thread. */
6239 if (elf_tdata (abfd
)->core_signal
== 0)
6240 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6241 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6243 /* pr_who exists on:
6246 pr_who doesn't exist on:
6249 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6250 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6253 #if defined (HAVE_PRSTATUS32_T)
6254 else if (note
->descsz
== sizeof (prstatus32_t
))
6256 /* 64-bit host, 32-bit corefile */
6257 prstatus32_t prstat
;
6259 raw_size
= sizeof (prstat
.pr_reg
);
6260 offset
= offsetof (prstatus32_t
, pr_reg
);
6261 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6263 /* Do not overwrite the core signal if it
6264 has already been set by another thread. */
6265 if (elf_tdata (abfd
)->core_signal
== 0)
6266 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6267 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6269 /* pr_who exists on:
6272 pr_who doesn't exist on:
6275 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6276 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6279 #endif /* HAVE_PRSTATUS32_T */
6282 /* Fail - we don't know how to handle any other
6283 note size (ie. data object type). */
6287 /* Make a ".reg/999" section and a ".reg" section. */
6288 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6289 raw_size
, note
->descpos
+ offset
);
6291 #endif /* defined (HAVE_PRSTATUS_T) */
6293 /* Create a pseudosection containing the exact contents of NOTE. */
6295 elfcore_make_note_pseudosection (abfd
, name
, note
)
6298 Elf_Internal_Note
*note
;
6300 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6301 note
->descsz
, note
->descpos
);
6304 /* There isn't a consistent prfpregset_t across platforms,
6305 but it doesn't matter, because we don't have to pick this
6306 data structure apart. */
6309 elfcore_grok_prfpreg (abfd
, note
)
6311 Elf_Internal_Note
*note
;
6313 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6316 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6317 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6321 elfcore_grok_prxfpreg (abfd
, note
)
6323 Elf_Internal_Note
*note
;
6325 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6328 #if defined (HAVE_PRPSINFO_T)
6329 typedef prpsinfo_t elfcore_psinfo_t
;
6330 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6331 typedef prpsinfo32_t elfcore_psinfo32_t
;
6335 #if defined (HAVE_PSINFO_T)
6336 typedef psinfo_t elfcore_psinfo_t
;
6337 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6338 typedef psinfo32_t elfcore_psinfo32_t
;
6342 /* return a malloc'ed copy of a string at START which is at
6343 most MAX bytes long, possibly without a terminating '\0'.
6344 the copy will always have a terminating '\0'. */
6347 _bfd_elfcore_strndup (abfd
, start
, max
)
6353 char *end
= memchr (start
, '\0', max
);
6361 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
6365 memcpy (dups
, start
, len
);
6371 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6372 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
6375 elfcore_grok_psinfo (abfd
, note
)
6377 Elf_Internal_Note
*note
;
6379 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6381 elfcore_psinfo_t psinfo
;
6383 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6385 elf_tdata (abfd
)->core_program
6386 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6387 sizeof (psinfo
.pr_fname
));
6389 elf_tdata (abfd
)->core_command
6390 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6391 sizeof (psinfo
.pr_psargs
));
6393 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6394 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6396 /* 64-bit host, 32-bit corefile */
6397 elfcore_psinfo32_t psinfo
;
6399 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6401 elf_tdata (abfd
)->core_program
6402 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6403 sizeof (psinfo
.pr_fname
));
6405 elf_tdata (abfd
)->core_command
6406 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6407 sizeof (psinfo
.pr_psargs
));
6413 /* Fail - we don't know how to handle any other
6414 note size (ie. data object type). */
6418 /* Note that for some reason, a spurious space is tacked
6419 onto the end of the args in some (at least one anyway)
6420 implementations, so strip it off if it exists. */
6423 char *command
= elf_tdata (abfd
)->core_command
;
6424 int n
= strlen (command
);
6426 if (0 < n
&& command
[n
- 1] == ' ')
6427 command
[n
- 1] = '\0';
6432 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6434 #if defined (HAVE_PSTATUS_T)
6435 static boolean elfcore_grok_pstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6438 elfcore_grok_pstatus (abfd
, note
)
6440 Elf_Internal_Note
*note
;
6442 if (note
->descsz
== sizeof (pstatus_t
)
6443 #if defined (HAVE_PXSTATUS_T)
6444 || note
->descsz
== sizeof (pxstatus_t
)
6450 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6452 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6454 #if defined (HAVE_PSTATUS32_T)
6455 else if (note
->descsz
== sizeof (pstatus32_t
))
6457 /* 64-bit host, 32-bit corefile */
6460 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6462 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6465 /* Could grab some more details from the "representative"
6466 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6467 NT_LWPSTATUS note, presumably. */
6471 #endif /* defined (HAVE_PSTATUS_T) */
6473 #if defined (HAVE_LWPSTATUS_T)
6474 static boolean elfcore_grok_lwpstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6477 elfcore_grok_lwpstatus (abfd
, note
)
6479 Elf_Internal_Note
*note
;
6481 lwpstatus_t lwpstat
;
6486 if (note
->descsz
!= sizeof (lwpstat
)
6487 #if defined (HAVE_LWPXSTATUS_T)
6488 && note
->descsz
!= sizeof (lwpxstatus_t
)
6493 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6495 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6496 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6498 /* Make a ".reg/999" section. */
6500 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6501 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6506 sect
= bfd_make_section (abfd
, name
);
6510 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6511 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6512 sect
->filepos
= note
->descpos
6513 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6516 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6517 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6518 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6521 sect
->flags
= SEC_HAS_CONTENTS
;
6522 sect
->alignment_power
= 2;
6524 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6527 /* Make a ".reg2/999" section */
6529 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6530 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6535 sect
= bfd_make_section (abfd
, name
);
6539 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6540 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6541 sect
->filepos
= note
->descpos
6542 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6545 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6546 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6547 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6550 sect
->flags
= SEC_HAS_CONTENTS
;
6551 sect
->alignment_power
= 2;
6553 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6555 #endif /* defined (HAVE_LWPSTATUS_T) */
6557 #if defined (HAVE_WIN32_PSTATUS_T)
6559 elfcore_grok_win32pstatus (abfd
, note
)
6561 Elf_Internal_Note
*note
;
6566 win32_pstatus_t pstatus
;
6568 if (note
->descsz
< sizeof (pstatus
))
6571 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
6573 switch (pstatus
.data_type
)
6575 case NOTE_INFO_PROCESS
:
6576 /* FIXME: need to add ->core_command. */
6577 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6578 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6581 case NOTE_INFO_THREAD
:
6582 /* Make a ".reg/999" section. */
6583 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6585 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6591 sect
= bfd_make_section (abfd
, name
);
6595 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6596 sect
->filepos
= (note
->descpos
6597 + offsetof (struct win32_pstatus
,
6598 data
.thread_info
.thread_context
));
6599 sect
->flags
= SEC_HAS_CONTENTS
;
6600 sect
->alignment_power
= 2;
6602 if (pstatus
.data
.thread_info
.is_active_thread
)
6603 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6607 case NOTE_INFO_MODULE
:
6608 /* Make a ".module/xxxxxxxx" section. */
6609 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6611 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6617 sect
= bfd_make_section (abfd
, name
);
6622 sect
->_raw_size
= note
->descsz
;
6623 sect
->filepos
= note
->descpos
;
6624 sect
->flags
= SEC_HAS_CONTENTS
;
6625 sect
->alignment_power
= 2;
6634 #endif /* HAVE_WIN32_PSTATUS_T */
6637 elfcore_grok_note (abfd
, note
)
6639 Elf_Internal_Note
*note
;
6641 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6649 if (bed
->elf_backend_grok_prstatus
)
6650 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6652 #if defined (HAVE_PRSTATUS_T)
6653 return elfcore_grok_prstatus (abfd
, note
);
6658 #if defined (HAVE_PSTATUS_T)
6660 return elfcore_grok_pstatus (abfd
, note
);
6663 #if defined (HAVE_LWPSTATUS_T)
6665 return elfcore_grok_lwpstatus (abfd
, note
);
6668 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6669 return elfcore_grok_prfpreg (abfd
, note
);
6671 #if defined (HAVE_WIN32_PSTATUS_T)
6672 case NT_WIN32PSTATUS
:
6673 return elfcore_grok_win32pstatus (abfd
, note
);
6676 case NT_PRXFPREG
: /* Linux SSE extension */
6677 if (note
->namesz
== 5
6678 && ! strcmp (note
->namedata
, "LINUX"))
6679 return elfcore_grok_prxfpreg (abfd
, note
);
6685 if (bed
->elf_backend_grok_psinfo
)
6686 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6688 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6689 return elfcore_grok_psinfo (abfd
, note
);
6697 elfcore_netbsd_get_lwpid (note
, lwpidp
)
6698 Elf_Internal_Note
*note
;
6703 cp
= strchr (note
->namedata
, '@');
6706 *lwpidp
= atoi(cp
+ 1);
6713 elfcore_grok_netbsd_procinfo (abfd
, note
)
6715 Elf_Internal_Note
*note
;
6718 /* Signal number at offset 0x08. */
6719 elf_tdata (abfd
)->core_signal
6720 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6722 /* Process ID at offset 0x50. */
6723 elf_tdata (abfd
)->core_pid
6724 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6726 /* Command name at 0x7c (max 32 bytes, including nul). */
6727 elf_tdata (abfd
)->core_command
6728 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6734 elfcore_grok_netbsd_note (abfd
, note
)
6736 Elf_Internal_Note
*note
;
6740 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6741 elf_tdata (abfd
)->core_lwpid
= lwp
;
6743 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6745 /* NetBSD-specific core "procinfo". Note that we expect to
6746 find this note before any of the others, which is fine,
6747 since the kernel writes this note out first when it
6748 creates a core file. */
6750 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6753 /* As of Jan 2002 there are no other machine-independent notes
6754 defined for NetBSD core files. If the note type is less
6755 than the start of the machine-dependent note types, we don't
6758 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
6762 switch (bfd_get_arch (abfd
))
6764 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6765 PT_GETFPREGS == mach+2. */
6767 case bfd_arch_alpha
:
6768 case bfd_arch_sparc
:
6771 case NT_NETBSDCORE_FIRSTMACH
+0:
6772 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6774 case NT_NETBSDCORE_FIRSTMACH
+2:
6775 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6781 /* On all other arch's, PT_GETREGS == mach+1 and
6782 PT_GETFPREGS == mach+3. */
6787 case NT_NETBSDCORE_FIRSTMACH
+1:
6788 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6790 case NT_NETBSDCORE_FIRSTMACH
+3:
6791 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6800 /* Function: elfcore_write_note
6807 size of data for note
6810 End of buffer containing note. */
6813 elfcore_write_note (abfd
, buf
, bufsiz
, name
, type
, input
, size
)
6822 Elf_External_Note
*xnp
;
6823 int namesz
= strlen (name
);
6824 int newspace
= BFD_ALIGN (sizeof (Elf_External_Note
) + size
+ namesz
- 1, 4);
6827 p
= realloc (buf
, *bufsiz
+ newspace
);
6829 *bufsiz
+= newspace
;
6830 xnp
= (Elf_External_Note
*) dest
;
6831 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
6832 H_PUT_32 (abfd
, size
, xnp
->descsz
);
6833 H_PUT_32 (abfd
, type
, xnp
->type
);
6834 strcpy (xnp
->name
, name
);
6835 memcpy (xnp
->name
+ BFD_ALIGN (namesz
, 4), input
, size
);
6839 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6841 elfcore_write_prpsinfo (abfd
, buf
, bufsiz
, fname
, psargs
)
6849 char *note_name
= "CORE";
6851 #if defined (HAVE_PSINFO_T)
6853 note_type
= NT_PSINFO
;
6856 note_type
= NT_PRPSINFO
;
6859 memset (&data
, 0, sizeof (data
));
6860 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
6861 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
6862 return elfcore_write_note (abfd
, buf
, bufsiz
,
6863 note_name
, note_type
, &data
, sizeof (data
));
6865 #endif /* PSINFO_T or PRPSINFO_T */
6867 #if defined (HAVE_PRSTATUS_T)
6869 elfcore_write_prstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6878 char *note_name
= "CORE";
6880 memset (&prstat
, 0, sizeof (prstat
));
6881 prstat
.pr_pid
= pid
;
6882 prstat
.pr_cursig
= cursig
;
6883 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
6884 return elfcore_write_note (abfd
, buf
, bufsiz
,
6885 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
6887 #endif /* HAVE_PRSTATUS_T */
6889 #if defined (HAVE_LWPSTATUS_T)
6891 elfcore_write_lwpstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6899 lwpstatus_t lwpstat
;
6900 char *note_name
= "CORE";
6902 memset (&lwpstat
, 0, sizeof (lwpstat
));
6903 lwpstat
.pr_lwpid
= pid
>> 16;
6904 lwpstat
.pr_cursig
= cursig
;
6905 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6906 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
6907 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6909 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
6910 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
6912 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
6913 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
6916 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
6917 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
6919 #endif /* HAVE_LWPSTATUS_T */
6921 #if defined (HAVE_PSTATUS_T)
6923 elfcore_write_pstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6932 char *note_name
= "CORE";
6934 memset (&pstat
, 0, sizeof (pstat
));
6935 pstat
.pr_pid
= pid
& 0xffff;
6936 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
6937 NT_PSTATUS
, &pstat
, sizeof (pstat
));
6940 #endif /* HAVE_PSTATUS_T */
6943 elfcore_write_prfpreg (abfd
, buf
, bufsiz
, fpregs
, size
)
6950 char *note_name
= "CORE";
6951 return elfcore_write_note (abfd
, buf
, bufsiz
,
6952 note_name
, NT_FPREGSET
, fpregs
, size
);
6956 elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, xfpregs
, size
)
6963 char *note_name
= "LINUX";
6964 return elfcore_write_note (abfd
, buf
, bufsiz
,
6965 note_name
, NT_PRXFPREG
, xfpregs
, size
);
6969 elfcore_read_notes (abfd
, offset
, size
)
6980 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
6983 buf
= bfd_malloc (size
);
6987 if (bfd_bread (buf
, size
, abfd
) != size
)
6995 while (p
< buf
+ size
)
6997 /* FIXME: bad alignment assumption. */
6998 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
6999 Elf_Internal_Note in
;
7001 in
.type
= H_GET_32 (abfd
, xnp
->type
);
7003 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
7004 in
.namedata
= xnp
->name
;
7006 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
7007 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
7008 in
.descpos
= offset
+ (in
.descdata
- buf
);
7010 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
7012 if (! elfcore_grok_netbsd_note (abfd
, &in
))
7017 if (! elfcore_grok_note (abfd
, &in
))
7021 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
7028 /* Providing external access to the ELF program header table. */
7030 /* Return an upper bound on the number of bytes required to store a
7031 copy of ABFD's program header table entries. Return -1 if an error
7032 occurs; bfd_get_error will return an appropriate code. */
7035 bfd_get_elf_phdr_upper_bound (abfd
)
7038 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7040 bfd_set_error (bfd_error_wrong_format
);
7044 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
7047 /* Copy ABFD's program header table entries to *PHDRS. The entries
7048 will be stored as an array of Elf_Internal_Phdr structures, as
7049 defined in include/elf/internal.h. To find out how large the
7050 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7052 Return the number of program header table entries read, or -1 if an
7053 error occurs; bfd_get_error will return an appropriate code. */
7056 bfd_get_elf_phdrs (abfd
, phdrs
)
7062 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7064 bfd_set_error (bfd_error_wrong_format
);
7068 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
7069 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
7070 num_phdrs
* sizeof (Elf_Internal_Phdr
));
7076 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
7077 bfd
*abfd ATTRIBUTE_UNUSED
;
7082 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7084 i_ehdrp
= elf_elfheader (abfd
);
7085 if (i_ehdrp
== NULL
)
7086 sprintf_vma (buf
, value
);
7089 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7091 #if BFD_HOST_64BIT_LONG
7092 sprintf (buf
, "%016lx", value
);
7094 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
7095 _bfd_int64_low (value
));
7099 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
7102 sprintf_vma (buf
, value
);
7107 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
7108 bfd
*abfd ATTRIBUTE_UNUSED
;
7113 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7115 i_ehdrp
= elf_elfheader (abfd
);
7116 if (i_ehdrp
== NULL
)
7117 fprintf_vma ((FILE *) stream
, value
);
7120 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7122 #if BFD_HOST_64BIT_LONG
7123 fprintf ((FILE *) stream
, "%016lx", value
);
7125 fprintf ((FILE *) stream
, "%08lx%08lx",
7126 _bfd_int64_high (value
), _bfd_int64_low (value
));
7130 fprintf ((FILE *) stream
, "%08lx",
7131 (unsigned long) (value
& 0xffffffff));
7134 fprintf_vma ((FILE *) stream
, value
);
7138 enum elf_reloc_type_class
7139 _bfd_elf_reloc_type_class (rela
)
7140 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
7142 return reloc_class_normal
;
7145 /* For RELA architectures, return what the relocation value for
7146 relocation against a local symbol. */
7149 _bfd_elf_rela_local_sym (abfd
, sym
, sec
, rel
)
7151 Elf_Internal_Sym
*sym
;
7153 Elf_Internal_Rela
*rel
;
7157 relocation
= (sec
->output_section
->vma
7158 + sec
->output_offset
7160 if ((sec
->flags
& SEC_MERGE
)
7161 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
7162 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
7168 _bfd_merged_section_offset (abfd
, &msec
,
7169 elf_section_data (sec
)->sec_info
,
7170 sym
->st_value
+ rel
->r_addend
,
7173 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
7179 _bfd_elf_rel_local_sym (abfd
, sym
, psec
, addend
)
7181 Elf_Internal_Sym
*sym
;
7185 asection
*sec
= *psec
;
7187 if (elf_section_data (sec
)->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7188 return sym
->st_value
+ addend
;
7190 return _bfd_merged_section_offset (abfd
, psec
,
7191 elf_section_data (sec
)->sec_info
,
7192 sym
->st_value
+ addend
, (bfd_vma
) 0);
7196 _bfd_elf_section_offset (abfd
, info
, sec
, offset
)
7198 struct bfd_link_info
*info
;
7202 struct bfd_elf_section_data
*sec_data
;
7204 sec_data
= elf_section_data (sec
);
7205 switch (sec_data
->sec_info_type
)
7207 case ELF_INFO_TYPE_STABS
:
7208 return _bfd_stab_section_offset
7209 (abfd
, &elf_hash_table (info
)->merge_info
, sec
, &sec_data
->sec_info
,
7211 case ELF_INFO_TYPE_EH_FRAME
:
7212 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);