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 void set_group_contents
PARAMS ((bfd
*, asection
*, PTR
));
58 static boolean assign_section_numbers
PARAMS ((bfd
*));
59 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
60 static boolean elf_map_symbols
PARAMS ((bfd
*));
61 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
62 static boolean elfcore_read_notes
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
63 static boolean elf_find_function
PARAMS ((bfd
*, asection
*, asymbol
**,
64 bfd_vma
, const char **,
66 static int elfcore_make_pid
PARAMS ((bfd
*));
67 static boolean elfcore_maybe_make_sect
PARAMS ((bfd
*, char *, asection
*));
68 static boolean elfcore_make_note_pseudosection
PARAMS ((bfd
*, char *,
69 Elf_Internal_Note
*));
70 static boolean elfcore_grok_prfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
71 static boolean elfcore_grok_prxfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
72 static boolean elfcore_grok_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
74 static boolean elfcore_netbsd_get_lwpid
PARAMS ((Elf_Internal_Note
*, int *));
75 static boolean elfcore_grok_netbsd_procinfo
PARAMS ((bfd
*,
76 Elf_Internal_Note
*));
77 static boolean elfcore_grok_netbsd_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
79 /* Swap version information in and out. The version information is
80 currently size independent. If that ever changes, this code will
81 need to move into elfcode.h. */
83 /* Swap in a Verdef structure. */
86 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
88 const Elf_External_Verdef
*src
;
89 Elf_Internal_Verdef
*dst
;
91 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
92 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
93 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
94 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
95 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
96 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
97 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
100 /* Swap out a Verdef structure. */
103 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
105 const Elf_Internal_Verdef
*src
;
106 Elf_External_Verdef
*dst
;
108 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
109 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
110 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
111 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
112 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
113 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
114 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
117 /* Swap in a Verdaux structure. */
120 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
122 const Elf_External_Verdaux
*src
;
123 Elf_Internal_Verdaux
*dst
;
125 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
126 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
129 /* Swap out a Verdaux structure. */
132 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
134 const Elf_Internal_Verdaux
*src
;
135 Elf_External_Verdaux
*dst
;
137 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
138 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
141 /* Swap in a Verneed structure. */
144 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
146 const Elf_External_Verneed
*src
;
147 Elf_Internal_Verneed
*dst
;
149 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
150 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
151 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
152 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
153 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
156 /* Swap out a Verneed structure. */
159 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
161 const Elf_Internal_Verneed
*src
;
162 Elf_External_Verneed
*dst
;
164 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
165 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
166 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
167 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
168 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
171 /* Swap in a Vernaux structure. */
174 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
176 const Elf_External_Vernaux
*src
;
177 Elf_Internal_Vernaux
*dst
;
179 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
180 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
181 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
182 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
183 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
186 /* Swap out a Vernaux structure. */
189 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
191 const Elf_Internal_Vernaux
*src
;
192 Elf_External_Vernaux
*dst
;
194 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
195 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
196 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
197 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
198 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
201 /* Swap in a Versym structure. */
204 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
206 const Elf_External_Versym
*src
;
207 Elf_Internal_Versym
*dst
;
209 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
212 /* Swap out a Versym structure. */
215 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
217 const Elf_Internal_Versym
*src
;
218 Elf_External_Versym
*dst
;
220 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
223 /* Standard ELF hash function. Do not change this function; you will
224 cause invalid hash tables to be generated. */
227 bfd_elf_hash (namearg
)
230 const unsigned char *name
= (const unsigned char *) namearg
;
235 while ((ch
= *name
++) != '\0')
238 if ((g
= (h
& 0xf0000000)) != 0)
241 /* The ELF ABI says `h &= ~g', but this is equivalent in
242 this case and on some machines one insn instead of two. */
249 /* Read a specified number of bytes at a specified offset in an ELF
250 file, into a newly allocated buffer, and return a pointer to the
254 elf_read (abfd
, offset
, size
)
261 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
263 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
265 if (bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
267 if (bfd_get_error () != bfd_error_system_call
)
268 bfd_set_error (bfd_error_file_truncated
);
275 bfd_elf_mkobject (abfd
)
278 /* This just does initialization. */
279 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
280 bfd_size_type amt
= sizeof (struct elf_obj_tdata
);
281 elf_tdata (abfd
) = (struct elf_obj_tdata
*) bfd_zalloc (abfd
, amt
);
282 if (elf_tdata (abfd
) == 0)
284 /* Since everything is done at close time, do we need any
291 bfd_elf_mkcorefile (abfd
)
294 /* I think this can be done just like an object file. */
295 return bfd_elf_mkobject (abfd
);
299 bfd_elf_get_str_section (abfd
, shindex
)
301 unsigned int shindex
;
303 Elf_Internal_Shdr
**i_shdrp
;
304 char *shstrtab
= NULL
;
306 bfd_size_type shstrtabsize
;
308 i_shdrp
= elf_elfsections (abfd
);
309 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
312 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
313 if (shstrtab
== NULL
)
315 /* No cached one, attempt to read, and cache what we read. */
316 offset
= i_shdrp
[shindex
]->sh_offset
;
317 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
318 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
319 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
325 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
327 unsigned int shindex
;
328 unsigned int strindex
;
330 Elf_Internal_Shdr
*hdr
;
335 hdr
= elf_elfsections (abfd
)[shindex
];
337 if (hdr
->contents
== NULL
338 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
341 if (strindex
>= hdr
->sh_size
)
343 (*_bfd_error_handler
)
344 (_("%s: invalid string offset %u >= %lu for section `%s'"),
345 bfd_archive_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
346 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
347 && strindex
== hdr
->sh_name
)
349 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
353 return ((char *) hdr
->contents
) + strindex
;
356 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
357 sections. The first element is the flags, the rest are section
360 typedef union elf_internal_group
{
361 Elf_Internal_Shdr
*shdr
;
363 } Elf_Internal_Group
;
365 /* Return the name of the group signature symbol. Why isn't the
366 signature just a string? */
369 group_signature (abfd
, ghdr
)
371 Elf_Internal_Shdr
*ghdr
;
373 struct elf_backend_data
*bed
;
376 Elf_Internal_Shdr
*hdr
;
377 Elf_Internal_Shdr
*shndx_hdr
;
378 unsigned char esym
[sizeof (Elf64_External_Sym
)];
379 Elf_External_Sym_Shndx eshndx
;
380 Elf_Internal_Sym isym
;
382 unsigned int shindex
;
384 /* First we need to ensure the symbol table is available. */
385 if (! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
388 /* Go read the symbol. */
389 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
390 bed
= get_elf_backend_data (abfd
);
391 amt
= bed
->s
->sizeof_sym
;
392 pos
= hdr
->sh_offset
+ ghdr
->sh_info
* amt
;
393 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
394 || bfd_bread (esym
, amt
, abfd
) != amt
)
397 /* And possibly the symbol section index extension. */
398 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
399 if (elf_elfsections (abfd
) != NULL
400 && elf_elfsections (abfd
)[shndx_hdr
->sh_link
] == hdr
)
402 amt
= sizeof (Elf_External_Sym_Shndx
);
403 pos
= shndx_hdr
->sh_offset
+ ghdr
->sh_info
* amt
;
404 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
405 || bfd_bread ((PTR
) &eshndx
, amt
, abfd
) != amt
)
409 /* Convert to internal format. */
410 (*bed
->s
->swap_symbol_in
) (abfd
, (const PTR
*) &esym
, (const PTR
*) &eshndx
,
413 /* Look up the symbol name. */
414 iname
= isym
.st_name
;
415 shindex
= hdr
->sh_link
;
416 if (iname
== 0 && ELF_ST_TYPE (isym
.st_info
) == STT_SECTION
)
418 iname
= elf_elfsections (abfd
)[isym
.st_shndx
]->sh_name
;
419 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
422 return bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
425 /* Set next_in_group list pointer, and group name for NEWSECT. */
428 setup_group (abfd
, hdr
, newsect
)
430 Elf_Internal_Shdr
*hdr
;
433 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
435 /* If num_group is zero, read in all SHT_GROUP sections. The count
436 is set to -1 if there are no SHT_GROUP sections. */
439 unsigned int i
, shnum
;
441 /* First count the number of groups. If we have a SHT_GROUP
442 section with just a flag word (ie. sh_size is 4), ignore it. */
443 shnum
= elf_numsections (abfd
);
445 for (i
= 0; i
< shnum
; i
++)
447 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
448 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
453 num_group
= (unsigned) -1;
454 elf_tdata (abfd
)->num_group
= num_group
;
458 /* We keep a list of elf section headers for group sections,
459 so we can find them quickly. */
460 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
461 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
462 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
466 for (i
= 0; i
< shnum
; i
++)
468 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
469 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
472 Elf_Internal_Group
*dest
;
474 /* Add to list of sections. */
475 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
478 /* Read the raw contents. */
479 BFD_ASSERT (sizeof (*dest
) >= 4);
480 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
481 shdr
->contents
= bfd_alloc (abfd
, amt
);
482 if (shdr
->contents
== NULL
483 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
484 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
488 /* Translate raw contents, a flag word followed by an
489 array of elf section indices all in target byte order,
490 to the flag word followed by an array of elf section
492 src
= shdr
->contents
+ shdr
->sh_size
;
493 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
500 idx
= H_GET_32 (abfd
, src
);
501 if (src
== shdr
->contents
)
504 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
505 shdr
->bfd_section
->flags
506 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
511 ((*_bfd_error_handler
)
512 (_("%s: invalid SHT_GROUP entry"),
513 bfd_archive_filename (abfd
)));
516 dest
->shdr
= elf_elfsections (abfd
)[idx
];
523 if (num_group
!= (unsigned) -1)
527 for (i
= 0; i
< num_group
; i
++)
529 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
530 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
531 unsigned int n_elt
= shdr
->sh_size
/ 4;
533 /* Look through this group's sections to see if current
534 section is a member. */
536 if ((++idx
)->shdr
== hdr
)
540 /* We are a member of this group. Go looking through
541 other members to see if any others are linked via
543 idx
= (Elf_Internal_Group
*) shdr
->contents
;
544 n_elt
= shdr
->sh_size
/ 4;
546 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
547 && elf_next_in_group (s
) != NULL
)
551 /* Snarf the group name from other member, and
552 insert current section in circular list. */
553 elf_group_name (newsect
) = elf_group_name (s
);
554 elf_next_in_group (newsect
) = elf_next_in_group (s
);
555 elf_next_in_group (s
) = newsect
;
561 gname
= group_signature (abfd
, shdr
);
564 elf_group_name (newsect
) = gname
;
566 /* Start a circular list with one element. */
567 elf_next_in_group (newsect
) = newsect
;
570 /* If the group section has been created, point to the
572 if (shdr
->bfd_section
!= NULL
)
573 elf_next_in_group (shdr
->bfd_section
) = newsect
;
581 if (elf_group_name (newsect
) == NULL
)
583 (*_bfd_error_handler
) (_("%s: no group info for section %s"),
584 bfd_archive_filename (abfd
), newsect
->name
);
590 bfd_elf_discard_group (abfd
, group
)
591 bfd
*abfd ATTRIBUTE_UNUSED
;
594 asection
*first
= elf_next_in_group (group
);
599 s
->output_section
= bfd_abs_section_ptr
;
600 s
= elf_next_in_group (s
);
601 /* These lists are circular. */
608 /* Make a BFD section from an ELF section. We store a pointer to the
609 BFD section in the bfd_section field of the header. */
612 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
614 Elf_Internal_Shdr
*hdr
;
619 struct elf_backend_data
*bed
;
621 if (hdr
->bfd_section
!= NULL
)
623 BFD_ASSERT (strcmp (name
,
624 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
628 newsect
= bfd_make_section_anyway (abfd
, name
);
632 newsect
->filepos
= hdr
->sh_offset
;
634 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
635 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
636 || ! bfd_set_section_alignment (abfd
, newsect
,
637 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
640 flags
= SEC_NO_FLAGS
;
641 if (hdr
->sh_type
!= SHT_NOBITS
)
642 flags
|= SEC_HAS_CONTENTS
;
643 if (hdr
->sh_type
== SHT_GROUP
)
644 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
645 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
648 if (hdr
->sh_type
!= SHT_NOBITS
)
651 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
652 flags
|= SEC_READONLY
;
653 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
655 else if ((flags
& SEC_LOAD
) != 0)
657 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
660 newsect
->entsize
= hdr
->sh_entsize
;
661 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
662 flags
|= SEC_STRINGS
;
664 if (hdr
->sh_flags
& SHF_GROUP
)
665 if (!setup_group (abfd
, hdr
, newsect
))
667 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
668 flags
|= SEC_THREAD_LOCAL
;
670 /* The debugging sections appear to be recognized only by name, not
673 static const char *debug_sec_names
[] =
682 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
683 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
687 flags
|= SEC_DEBUGGING
;
690 /* As a GNU extension, if the name begins with .gnu.linkonce, we
691 only link a single copy of the section. This is used to support
692 g++. g++ will emit each template expansion in its own section.
693 The symbols will be defined as weak, so that multiple definitions
694 are permitted. The GNU linker extension is to actually discard
695 all but one of the sections. */
696 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
697 && elf_next_in_group (newsect
) == NULL
)
698 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
700 bed
= get_elf_backend_data (abfd
);
701 if (bed
->elf_backend_section_flags
)
702 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
705 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
708 if ((flags
& SEC_ALLOC
) != 0)
710 Elf_Internal_Phdr
*phdr
;
713 /* Look through the phdrs to see if we need to adjust the lma.
714 If all the p_paddr fields are zero, we ignore them, since
715 some ELF linkers produce such output. */
716 phdr
= elf_tdata (abfd
)->phdr
;
717 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
719 if (phdr
->p_paddr
!= 0)
722 if (i
< elf_elfheader (abfd
)->e_phnum
)
724 phdr
= elf_tdata (abfd
)->phdr
;
725 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
727 /* This section is part of this segment if its file
728 offset plus size lies within the segment's memory
729 span and, if the section is loaded, the extent of the
730 loaded data lies within the extent of the segment.
732 Note - we used to check the p_paddr field as well, and
733 refuse to set the LMA if it was 0. This is wrong
734 though, as a perfectly valid initialised segment can
735 have a p_paddr of zero. Some architectures, eg ARM,
736 place special significance on the address 0 and
737 executables need to be able to have a segment which
738 covers this address. */
739 if (phdr
->p_type
== PT_LOAD
740 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
741 && (hdr
->sh_offset
+ hdr
->sh_size
742 <= phdr
->p_offset
+ phdr
->p_memsz
)
743 && ((flags
& SEC_LOAD
) == 0
744 || (hdr
->sh_offset
+ hdr
->sh_size
745 <= phdr
->p_offset
+ phdr
->p_filesz
)))
747 if ((flags
& SEC_LOAD
) == 0)
748 newsect
->lma
= (phdr
->p_paddr
749 + hdr
->sh_addr
- phdr
->p_vaddr
);
751 /* We used to use the same adjustment for SEC_LOAD
752 sections, but that doesn't work if the segment
753 is packed with code from multiple VMAs.
754 Instead we calculate the section LMA based on
755 the segment LMA. It is assumed that the
756 segment will contain sections with contiguous
757 LMAs, even if the VMAs are not. */
758 newsect
->lma
= (phdr
->p_paddr
759 + hdr
->sh_offset
- phdr
->p_offset
);
761 /* With contiguous segments, we can't tell from file
762 offsets whether a section with zero size should
763 be placed at the end of one segment or the
764 beginning of the next. Decide based on vaddr. */
765 if (hdr
->sh_addr
>= phdr
->p_vaddr
766 && (hdr
->sh_addr
+ hdr
->sh_size
767 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
774 hdr
->bfd_section
= newsect
;
775 elf_section_data (newsect
)->this_hdr
= *hdr
;
785 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
788 Helper functions for GDB to locate the string tables.
789 Since BFD hides string tables from callers, GDB needs to use an
790 internal hook to find them. Sun's .stabstr, in particular,
791 isn't even pointed to by the .stab section, so ordinary
792 mechanisms wouldn't work to find it, even if we had some.
795 struct elf_internal_shdr
*
796 bfd_elf_find_section (abfd
, name
)
800 Elf_Internal_Shdr
**i_shdrp
;
805 i_shdrp
= elf_elfsections (abfd
);
808 shstrtab
= bfd_elf_get_str_section (abfd
,
809 elf_elfheader (abfd
)->e_shstrndx
);
810 if (shstrtab
!= NULL
)
812 max
= elf_numsections (abfd
);
813 for (i
= 1; i
< max
; i
++)
814 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
821 const char *const bfd_elf_section_type_names
[] = {
822 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
823 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
824 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
827 /* ELF relocs are against symbols. If we are producing relocateable
828 output, and the reloc is against an external symbol, and nothing
829 has given us any additional addend, the resulting reloc will also
830 be against the same symbol. In such a case, we don't want to
831 change anything about the way the reloc is handled, since it will
832 all be done at final link time. Rather than put special case code
833 into bfd_perform_relocation, all the reloc types use this howto
834 function. It just short circuits the reloc if producing
835 relocateable output against an external symbol. */
837 bfd_reloc_status_type
838 bfd_elf_generic_reloc (abfd
,
845 bfd
*abfd ATTRIBUTE_UNUSED
;
846 arelent
*reloc_entry
;
848 PTR data ATTRIBUTE_UNUSED
;
849 asection
*input_section
;
851 char **error_message ATTRIBUTE_UNUSED
;
853 if (output_bfd
!= (bfd
*) NULL
854 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
855 && (! reloc_entry
->howto
->partial_inplace
856 || reloc_entry
->addend
== 0))
858 reloc_entry
->address
+= input_section
->output_offset
;
862 return bfd_reloc_continue
;
865 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
868 merge_sections_remove_hook (abfd
, sec
)
869 bfd
*abfd ATTRIBUTE_UNUSED
;
872 struct bfd_elf_section_data
*sec_data
;
874 sec_data
= elf_section_data (sec
);
875 BFD_ASSERT (sec_data
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
876 sec_data
->sec_info_type
= ELF_INFO_TYPE_NONE
;
879 /* Finish SHF_MERGE section merging. */
882 _bfd_elf_merge_sections (abfd
, info
)
884 struct bfd_link_info
*info
;
886 if (!is_elf_hash_table (info
))
888 if (elf_hash_table (info
)->merge_info
)
889 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
,
890 merge_sections_remove_hook
);
895 _bfd_elf_link_just_syms (sec
, info
)
897 struct bfd_link_info
*info
;
899 sec
->output_section
= bfd_abs_section_ptr
;
900 sec
->output_offset
= sec
->vma
;
901 if (!is_elf_hash_table (info
))
904 elf_section_data (sec
)->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
907 /* Copy the program header and other data from one object module to
911 _bfd_elf_copy_private_bfd_data (ibfd
, obfd
)
915 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
916 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
919 BFD_ASSERT (!elf_flags_init (obfd
)
920 || (elf_elfheader (obfd
)->e_flags
921 == elf_elfheader (ibfd
)->e_flags
));
923 elf_gp (obfd
) = elf_gp (ibfd
);
924 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
925 elf_flags_init (obfd
) = true;
929 /* Print out the program headers. */
932 _bfd_elf_print_private_bfd_data (abfd
, farg
)
936 FILE *f
= (FILE *) farg
;
937 Elf_Internal_Phdr
*p
;
939 bfd_byte
*dynbuf
= NULL
;
941 p
= elf_tdata (abfd
)->phdr
;
946 fprintf (f
, _("\nProgram Header:\n"));
947 c
= elf_elfheader (abfd
)->e_phnum
;
948 for (i
= 0; i
< c
; i
++, p
++)
955 case PT_NULL
: pt
= "NULL"; break;
956 case PT_LOAD
: pt
= "LOAD"; break;
957 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
958 case PT_INTERP
: pt
= "INTERP"; break;
959 case PT_NOTE
: pt
= "NOTE"; break;
960 case PT_SHLIB
: pt
= "SHLIB"; break;
961 case PT_PHDR
: pt
= "PHDR"; break;
962 case PT_TLS
: pt
= "TLS"; break;
963 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
964 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
966 fprintf (f
, "%8s off 0x", pt
);
967 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
968 fprintf (f
, " vaddr 0x");
969 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
970 fprintf (f
, " paddr 0x");
971 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
972 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
973 fprintf (f
, " filesz 0x");
974 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
975 fprintf (f
, " memsz 0x");
976 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
977 fprintf (f
, " flags %c%c%c",
978 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
979 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
980 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
981 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
982 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
987 s
= bfd_get_section_by_name (abfd
, ".dynamic");
991 unsigned long shlink
;
992 bfd_byte
*extdyn
, *extdynend
;
994 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
996 fprintf (f
, _("\nDynamic Section:\n"));
998 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1001 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1005 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1008 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1010 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1011 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1014 extdynend
= extdyn
+ s
->_raw_size
;
1015 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1017 Elf_Internal_Dyn dyn
;
1022 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1024 if (dyn
.d_tag
== DT_NULL
)
1031 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1035 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
1036 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1037 case DT_PLTGOT
: name
= "PLTGOT"; break;
1038 case DT_HASH
: name
= "HASH"; break;
1039 case DT_STRTAB
: name
= "STRTAB"; break;
1040 case DT_SYMTAB
: name
= "SYMTAB"; break;
1041 case DT_RELA
: name
= "RELA"; break;
1042 case DT_RELASZ
: name
= "RELASZ"; break;
1043 case DT_RELAENT
: name
= "RELAENT"; break;
1044 case DT_STRSZ
: name
= "STRSZ"; break;
1045 case DT_SYMENT
: name
= "SYMENT"; break;
1046 case DT_INIT
: name
= "INIT"; break;
1047 case DT_FINI
: name
= "FINI"; break;
1048 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
1049 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
1050 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1051 case DT_REL
: name
= "REL"; break;
1052 case DT_RELSZ
: name
= "RELSZ"; break;
1053 case DT_RELENT
: name
= "RELENT"; break;
1054 case DT_PLTREL
: name
= "PLTREL"; break;
1055 case DT_DEBUG
: name
= "DEBUG"; break;
1056 case DT_TEXTREL
: name
= "TEXTREL"; break;
1057 case DT_JMPREL
: name
= "JMPREL"; break;
1058 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1059 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1060 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1061 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1062 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1063 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
1064 case DT_FLAGS
: name
= "FLAGS"; break;
1065 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1066 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1067 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1068 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1069 case DT_MOVEENT
: name
= "MOVEENT"; break;
1070 case DT_MOVESZ
: name
= "MOVESZ"; break;
1071 case DT_FEATURE
: name
= "FEATURE"; break;
1072 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1073 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1074 case DT_SYMINENT
: name
= "SYMINENT"; break;
1075 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
1076 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
1077 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
1078 case DT_PLTPAD
: name
= "PLTPAD"; break;
1079 case DT_MOVETAB
: name
= "MOVETAB"; break;
1080 case DT_SYMINFO
: name
= "SYMINFO"; break;
1081 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1082 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1083 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1084 case DT_VERSYM
: name
= "VERSYM"; break;
1085 case DT_VERDEF
: name
= "VERDEF"; break;
1086 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1087 case DT_VERNEED
: name
= "VERNEED"; break;
1088 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1089 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
1090 case DT_USED
: name
= "USED"; break;
1091 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
1094 fprintf (f
, " %-11s ", name
);
1096 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1100 unsigned int tagv
= dyn
.d_un
.d_val
;
1102 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1105 fprintf (f
, "%s", string
);
1114 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1115 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1117 if (! _bfd_elf_slurp_version_tables (abfd
))
1121 if (elf_dynverdef (abfd
) != 0)
1123 Elf_Internal_Verdef
*t
;
1125 fprintf (f
, _("\nVersion definitions:\n"));
1126 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1128 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1129 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1130 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1132 Elf_Internal_Verdaux
*a
;
1135 for (a
= t
->vd_auxptr
->vda_nextptr
;
1138 fprintf (f
, "%s ", a
->vda_nodename
);
1144 if (elf_dynverref (abfd
) != 0)
1146 Elf_Internal_Verneed
*t
;
1148 fprintf (f
, _("\nVersion References:\n"));
1149 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1151 Elf_Internal_Vernaux
*a
;
1153 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1154 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1155 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1156 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1168 /* Display ELF-specific fields of a symbol. */
1171 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
1175 bfd_print_symbol_type how
;
1177 FILE *file
= (FILE *) filep
;
1180 case bfd_print_symbol_name
:
1181 fprintf (file
, "%s", symbol
->name
);
1183 case bfd_print_symbol_more
:
1184 fprintf (file
, "elf ");
1185 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1186 fprintf (file
, " %lx", (long) symbol
->flags
);
1188 case bfd_print_symbol_all
:
1190 const char *section_name
;
1191 const char *name
= NULL
;
1192 struct elf_backend_data
*bed
;
1193 unsigned char st_other
;
1196 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1198 bed
= get_elf_backend_data (abfd
);
1199 if (bed
->elf_backend_print_symbol_all
)
1200 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1204 name
= symbol
->name
;
1205 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
1208 fprintf (file
, " %s\t", section_name
);
1209 /* Print the "other" value for a symbol. For common symbols,
1210 we've already printed the size; now print the alignment.
1211 For other symbols, we have no specified alignment, and
1212 we've printed the address; now print the size. */
1213 if (bfd_is_com_section (symbol
->section
))
1214 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1216 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1217 bfd_fprintf_vma (abfd
, file
, val
);
1219 /* If we have version information, print it. */
1220 if (elf_tdata (abfd
)->dynversym_section
!= 0
1221 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1222 || elf_tdata (abfd
)->dynverref_section
!= 0))
1224 unsigned int vernum
;
1225 const char *version_string
;
1227 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1230 version_string
= "";
1231 else if (vernum
== 1)
1232 version_string
= "Base";
1233 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1235 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1238 Elf_Internal_Verneed
*t
;
1240 version_string
= "";
1241 for (t
= elf_tdata (abfd
)->verref
;
1245 Elf_Internal_Vernaux
*a
;
1247 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1249 if (a
->vna_other
== vernum
)
1251 version_string
= a
->vna_nodename
;
1258 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1259 fprintf (file
, " %-11s", version_string
);
1264 fprintf (file
, " (%s)", version_string
);
1265 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1270 /* If the st_other field is not zero, print it. */
1271 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1276 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1277 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1278 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1280 /* Some other non-defined flags are also present, so print
1282 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1285 fprintf (file
, " %s", name
);
1291 /* Create an entry in an ELF linker hash table. */
1293 struct bfd_hash_entry
*
1294 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
1295 struct bfd_hash_entry
*entry
;
1296 struct bfd_hash_table
*table
;
1299 /* Allocate the structure if it has not already been allocated by a
1303 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1308 /* Call the allocation method of the superclass. */
1309 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1312 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1313 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1315 /* Set local fields. */
1319 ret
->dynstr_index
= 0;
1320 ret
->weakdef
= NULL
;
1321 ret
->got
.refcount
= htab
->init_refcount
;
1322 ret
->plt
.refcount
= htab
->init_refcount
;
1323 ret
->linker_section_pointer
= NULL
;
1324 ret
->verinfo
.verdef
= NULL
;
1325 ret
->vtable_entries_used
= NULL
;
1326 ret
->vtable_entries_size
= 0;
1327 ret
->vtable_parent
= NULL
;
1328 ret
->type
= STT_NOTYPE
;
1330 /* Assume that we have been called by a non-ELF symbol reader.
1331 This flag is then reset by the code which reads an ELF input
1332 file. This ensures that a symbol created by a non-ELF symbol
1333 reader will have the flag set correctly. */
1334 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1340 /* Copy data from an indirect symbol to its direct symbol, hiding the
1341 old indirect symbol. Also used for copying flags to a weakdef. */
1344 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
1345 struct elf_link_hash_entry
*dir
, *ind
;
1349 /* Copy down any references that we may have already seen to the
1350 symbol which just became indirect. */
1352 dir
->elf_link_hash_flags
|=
1353 (ind
->elf_link_hash_flags
1354 & (ELF_LINK_HASH_REF_DYNAMIC
1355 | ELF_LINK_HASH_REF_REGULAR
1356 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1357 | ELF_LINK_NON_GOT_REF
));
1359 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1362 /* Copy over the global and procedure linkage table refcount entries.
1363 These may have been already set up by a check_relocs routine. */
1364 tmp
= dir
->got
.refcount
;
1367 dir
->got
.refcount
= ind
->got
.refcount
;
1368 ind
->got
.refcount
= tmp
;
1371 BFD_ASSERT (ind
->got
.refcount
<= 0);
1373 tmp
= dir
->plt
.refcount
;
1376 dir
->plt
.refcount
= ind
->plt
.refcount
;
1377 ind
->plt
.refcount
= tmp
;
1380 BFD_ASSERT (ind
->plt
.refcount
<= 0);
1382 if (dir
->dynindx
== -1)
1384 dir
->dynindx
= ind
->dynindx
;
1385 dir
->dynstr_index
= ind
->dynstr_index
;
1387 ind
->dynstr_index
= 0;
1390 BFD_ASSERT (ind
->dynindx
== -1);
1394 _bfd_elf_link_hash_hide_symbol (info
, h
, force_local
)
1395 struct bfd_link_info
*info
;
1396 struct elf_link_hash_entry
*h
;
1397 boolean force_local
;
1399 h
->plt
.offset
= (bfd_vma
) -1;
1400 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1403 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1404 if (h
->dynindx
!= -1)
1407 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
1413 /* Initialize an ELF linker hash table. */
1416 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1417 struct elf_link_hash_table
*table
;
1419 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1420 struct bfd_hash_table
*,
1425 table
->dynamic_sections_created
= false;
1426 table
->dynobj
= NULL
;
1427 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
- 1;
1428 /* The first dynamic symbol is a dummy. */
1429 table
->dynsymcount
= 1;
1430 table
->dynstr
= NULL
;
1431 table
->bucketcount
= 0;
1432 table
->needed
= NULL
;
1433 table
->runpath
= NULL
;
1435 table
->stab_info
= NULL
;
1436 table
->merge_info
= NULL
;
1437 table
->dynlocal
= NULL
;
1438 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1439 table
->root
.type
= bfd_link_elf_hash_table
;
1444 /* Create an ELF linker hash table. */
1446 struct bfd_link_hash_table
*
1447 _bfd_elf_link_hash_table_create (abfd
)
1450 struct elf_link_hash_table
*ret
;
1451 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1453 ret
= (struct elf_link_hash_table
*) bfd_malloc (amt
);
1454 if (ret
== (struct elf_link_hash_table
*) NULL
)
1457 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1466 /* This is a hook for the ELF emulation code in the generic linker to
1467 tell the backend linker what file name to use for the DT_NEEDED
1468 entry for a dynamic object. The generic linker passes name as an
1469 empty string to indicate that no DT_NEEDED entry should be made. */
1472 bfd_elf_set_dt_needed_name (abfd
, name
)
1476 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1477 && bfd_get_format (abfd
) == bfd_object
)
1478 elf_dt_name (abfd
) = name
;
1482 bfd_elf_set_dt_needed_soname (abfd
, name
)
1486 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1487 && bfd_get_format (abfd
) == bfd_object
)
1488 elf_dt_soname (abfd
) = name
;
1491 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1492 the linker ELF emulation code. */
1494 struct bfd_link_needed_list
*
1495 bfd_elf_get_needed_list (abfd
, info
)
1496 bfd
*abfd ATTRIBUTE_UNUSED
;
1497 struct bfd_link_info
*info
;
1499 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1501 return elf_hash_table (info
)->needed
;
1504 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1505 hook for the linker ELF emulation code. */
1507 struct bfd_link_needed_list
*
1508 bfd_elf_get_runpath_list (abfd
, info
)
1509 bfd
*abfd ATTRIBUTE_UNUSED
;
1510 struct bfd_link_info
*info
;
1512 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1514 return elf_hash_table (info
)->runpath
;
1517 /* Get the name actually used for a dynamic object for a link. This
1518 is the SONAME entry if there is one. Otherwise, it is the string
1519 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1522 bfd_elf_get_dt_soname (abfd
)
1525 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1526 && bfd_get_format (abfd
) == bfd_object
)
1527 return elf_dt_name (abfd
);
1531 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1532 the ELF linker emulation code. */
1535 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1537 struct bfd_link_needed_list
**pneeded
;
1540 bfd_byte
*dynbuf
= NULL
;
1542 unsigned long shlink
;
1543 bfd_byte
*extdyn
, *extdynend
;
1545 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1549 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1550 || bfd_get_format (abfd
) != bfd_object
)
1553 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1554 if (s
== NULL
|| s
->_raw_size
== 0)
1557 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1561 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1565 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1569 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1571 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1572 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1575 extdynend
= extdyn
+ s
->_raw_size
;
1576 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1578 Elf_Internal_Dyn dyn
;
1580 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1582 if (dyn
.d_tag
== DT_NULL
)
1585 if (dyn
.d_tag
== DT_NEEDED
)
1588 struct bfd_link_needed_list
*l
;
1589 unsigned int tagv
= dyn
.d_un
.d_val
;
1592 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1597 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1618 /* Allocate an ELF string table--force the first byte to be zero. */
1620 struct bfd_strtab_hash
*
1621 _bfd_elf_stringtab_init ()
1623 struct bfd_strtab_hash
*ret
;
1625 ret
= _bfd_stringtab_init ();
1630 loc
= _bfd_stringtab_add (ret
, "", true, false);
1631 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1632 if (loc
== (bfd_size_type
) -1)
1634 _bfd_stringtab_free (ret
);
1641 /* ELF .o/exec file reading */
1643 /* Create a new bfd section from an ELF section header. */
1646 bfd_section_from_shdr (abfd
, shindex
)
1648 unsigned int shindex
;
1650 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1651 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1652 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1655 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1657 switch (hdr
->sh_type
)
1660 /* Inactive section. Throw it away. */
1663 case SHT_PROGBITS
: /* Normal section with contents. */
1664 case SHT_DYNAMIC
: /* Dynamic linking information. */
1665 case SHT_NOBITS
: /* .bss section. */
1666 case SHT_HASH
: /* .hash section. */
1667 case SHT_NOTE
: /* .note section. */
1668 case SHT_INIT_ARRAY
: /* .init_array section. */
1669 case SHT_FINI_ARRAY
: /* .fini_array section. */
1670 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1671 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1673 case SHT_SYMTAB
: /* A symbol table */
1674 if (elf_onesymtab (abfd
) == shindex
)
1677 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1678 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1679 elf_onesymtab (abfd
) = shindex
;
1680 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1681 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1682 abfd
->flags
|= HAS_SYMS
;
1684 /* Sometimes a shared object will map in the symbol table. If
1685 SHF_ALLOC is set, and this is a shared object, then we also
1686 treat this section as a BFD section. We can not base the
1687 decision purely on SHF_ALLOC, because that flag is sometimes
1688 set in a relocateable object file, which would confuse the
1690 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1691 && (abfd
->flags
& DYNAMIC
) != 0
1692 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1697 case SHT_DYNSYM
: /* A dynamic symbol table */
1698 if (elf_dynsymtab (abfd
) == shindex
)
1701 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1702 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1703 elf_dynsymtab (abfd
) = shindex
;
1704 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1705 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1706 abfd
->flags
|= HAS_SYMS
;
1708 /* Besides being a symbol table, we also treat this as a regular
1709 section, so that objcopy can handle it. */
1710 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1712 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1713 if (elf_symtab_shndx (abfd
) == shindex
)
1716 /* Get the associated symbol table. */
1717 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1718 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1721 elf_symtab_shndx (abfd
) = shindex
;
1722 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1723 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1726 case SHT_STRTAB
: /* A string table */
1727 if (hdr
->bfd_section
!= NULL
)
1729 if (ehdr
->e_shstrndx
== shindex
)
1731 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1732 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1736 unsigned int i
, num_sec
;
1738 num_sec
= elf_numsections (abfd
);
1739 for (i
= 1; i
< num_sec
; i
++)
1741 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1742 if (hdr2
->sh_link
== shindex
)
1744 if (! bfd_section_from_shdr (abfd
, i
))
1746 if (elf_onesymtab (abfd
) == i
)
1748 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1749 elf_elfsections (abfd
)[shindex
] =
1750 &elf_tdata (abfd
)->strtab_hdr
;
1753 if (elf_dynsymtab (abfd
) == i
)
1755 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1756 elf_elfsections (abfd
)[shindex
] = hdr
=
1757 &elf_tdata (abfd
)->dynstrtab_hdr
;
1758 /* We also treat this as a regular section, so
1759 that objcopy can handle it. */
1762 #if 0 /* Not handling other string tables specially right now. */
1763 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1764 /* We have a strtab for some random other section. */
1765 newsect
= (asection
*) hdr2
->bfd_section
;
1768 hdr
->bfd_section
= newsect
;
1769 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1771 elf_elfsections (abfd
)[shindex
] = hdr2
;
1777 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1781 /* *These* do a lot of work -- but build no sections! */
1783 asection
*target_sect
;
1784 Elf_Internal_Shdr
*hdr2
;
1785 unsigned int num_sec
= elf_numsections (abfd
);
1787 /* Check for a bogus link to avoid crashing. */
1788 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1789 || hdr
->sh_link
>= num_sec
)
1791 ((*_bfd_error_handler
)
1792 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1793 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1794 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1797 /* For some incomprehensible reason Oracle distributes
1798 libraries for Solaris in which some of the objects have
1799 bogus sh_link fields. It would be nice if we could just
1800 reject them, but, unfortunately, some people need to use
1801 them. We scan through the section headers; if we find only
1802 one suitable symbol table, we clobber the sh_link to point
1803 to it. I hope this doesn't break anything. */
1804 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1805 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1811 for (scan
= 1; scan
< num_sec
; scan
++)
1813 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1814 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1825 hdr
->sh_link
= found
;
1828 /* Get the symbol table. */
1829 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1830 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1833 /* If this reloc section does not use the main symbol table we
1834 don't treat it as a reloc section. BFD can't adequately
1835 represent such a section, so at least for now, we don't
1836 try. We just present it as a normal section. We also
1837 can't use it as a reloc section if it points to the null
1839 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1840 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1842 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1844 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1845 if (target_sect
== NULL
)
1848 if ((target_sect
->flags
& SEC_RELOC
) == 0
1849 || target_sect
->reloc_count
== 0)
1850 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1854 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1855 amt
= sizeof (*hdr2
);
1856 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1857 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1860 elf_elfsections (abfd
)[shindex
] = hdr2
;
1861 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1862 target_sect
->flags
|= SEC_RELOC
;
1863 target_sect
->relocation
= NULL
;
1864 target_sect
->rel_filepos
= hdr
->sh_offset
;
1865 /* In the section to which the relocations apply, mark whether
1866 its relocations are of the REL or RELA variety. */
1867 if (hdr
->sh_size
!= 0)
1868 elf_section_data (target_sect
)->use_rela_p
1869 = (hdr
->sh_type
== SHT_RELA
);
1870 abfd
->flags
|= HAS_RELOC
;
1875 case SHT_GNU_verdef
:
1876 elf_dynverdef (abfd
) = shindex
;
1877 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1878 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1881 case SHT_GNU_versym
:
1882 elf_dynversym (abfd
) = shindex
;
1883 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1884 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1887 case SHT_GNU_verneed
:
1888 elf_dynverref (abfd
) = shindex
;
1889 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1890 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1897 /* We need a BFD section for objcopy and relocatable linking,
1898 and it's handy to have the signature available as the section
1900 name
= group_signature (abfd
, hdr
);
1903 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1905 if (hdr
->contents
!= NULL
)
1907 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1908 unsigned int n_elt
= hdr
->sh_size
/ 4;
1911 if (idx
->flags
& GRP_COMDAT
)
1912 hdr
->bfd_section
->flags
1913 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1915 while (--n_elt
!= 0)
1916 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
1917 && elf_next_in_group (s
) != NULL
)
1919 elf_next_in_group (hdr
->bfd_section
) = s
;
1926 /* Check for any processor-specific section types. */
1928 if (bed
->elf_backend_section_from_shdr
)
1929 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1937 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1938 Return SEC for sections that have no elf section, and NULL on error. */
1941 bfd_section_from_r_symndx (abfd
, cache
, sec
, r_symndx
)
1943 struct sym_sec_cache
*cache
;
1945 unsigned long r_symndx
;
1947 unsigned char esym_shndx
[4];
1948 unsigned int isym_shndx
;
1949 Elf_Internal_Shdr
*symtab_hdr
;
1952 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1954 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
1955 return cache
->sec
[ent
];
1957 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1958 pos
= symtab_hdr
->sh_offset
;
1959 if (get_elf_backend_data (abfd
)->s
->sizeof_sym
1960 == sizeof (Elf64_External_Sym
))
1962 pos
+= r_symndx
* sizeof (Elf64_External_Sym
);
1963 pos
+= offsetof (Elf64_External_Sym
, st_shndx
);
1964 amt
= sizeof (((Elf64_External_Sym
*) 0)->st_shndx
);
1968 pos
+= r_symndx
* sizeof (Elf32_External_Sym
);
1969 pos
+= offsetof (Elf32_External_Sym
, st_shndx
);
1970 amt
= sizeof (((Elf32_External_Sym
*) 0)->st_shndx
);
1972 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1973 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1975 isym_shndx
= H_GET_16 (abfd
, esym_shndx
);
1977 if (isym_shndx
== SHN_XINDEX
)
1979 Elf_Internal_Shdr
*shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1980 if (shndx_hdr
->sh_size
!= 0)
1982 pos
= shndx_hdr
->sh_offset
;
1983 pos
+= r_symndx
* sizeof (Elf_External_Sym_Shndx
);
1984 amt
= sizeof (Elf_External_Sym_Shndx
);
1985 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1986 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1988 isym_shndx
= H_GET_32 (abfd
, esym_shndx
);
1992 if (cache
->abfd
!= abfd
)
1994 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1997 cache
->indx
[ent
] = r_symndx
;
1998 cache
->sec
[ent
] = sec
;
1999 if (isym_shndx
< SHN_LORESERVE
|| isym_shndx
> SHN_HIRESERVE
)
2002 s
= bfd_section_from_elf_index (abfd
, isym_shndx
);
2004 cache
->sec
[ent
] = s
;
2006 return cache
->sec
[ent
];
2009 /* Given an ELF section number, retrieve the corresponding BFD
2013 bfd_section_from_elf_index (abfd
, index
)
2017 if (index
>= elf_numsections (abfd
))
2019 return elf_elfsections (abfd
)[index
]->bfd_section
;
2023 _bfd_elf_new_section_hook (abfd
, sec
)
2027 struct bfd_elf_section_data
*sdata
;
2028 bfd_size_type amt
= sizeof (*sdata
);
2030 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
2033 sec
->used_by_bfd
= (PTR
) sdata
;
2035 /* Indicate whether or not this section should use RELA relocations. */
2037 = get_elf_backend_data (abfd
)->default_use_rela_p
;
2042 /* Create a new bfd section from an ELF program header.
2044 Since program segments have no names, we generate a synthetic name
2045 of the form segment<NUM>, where NUM is generally the index in the
2046 program header table. For segments that are split (see below) we
2047 generate the names segment<NUM>a and segment<NUM>b.
2049 Note that some program segments may have a file size that is different than
2050 (less than) the memory size. All this means is that at execution the
2051 system must allocate the amount of memory specified by the memory size,
2052 but only initialize it with the first "file size" bytes read from the
2053 file. This would occur for example, with program segments consisting
2054 of combined data+bss.
2056 To handle the above situation, this routine generates TWO bfd sections
2057 for the single program segment. The first has the length specified by
2058 the file size of the segment, and the second has the length specified
2059 by the difference between the two sizes. In effect, the segment is split
2060 into it's initialized and uninitialized parts.
2065 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
2067 Elf_Internal_Phdr
*hdr
;
2069 const char *typename
;
2076 split
= ((hdr
->p_memsz
> 0)
2077 && (hdr
->p_filesz
> 0)
2078 && (hdr
->p_memsz
> hdr
->p_filesz
));
2079 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2080 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
2083 strcpy (name
, namebuf
);
2084 newsect
= bfd_make_section (abfd
, name
);
2085 if (newsect
== NULL
)
2087 newsect
->vma
= hdr
->p_vaddr
;
2088 newsect
->lma
= hdr
->p_paddr
;
2089 newsect
->_raw_size
= hdr
->p_filesz
;
2090 newsect
->filepos
= hdr
->p_offset
;
2091 newsect
->flags
|= SEC_HAS_CONTENTS
;
2092 if (hdr
->p_type
== PT_LOAD
)
2094 newsect
->flags
|= SEC_ALLOC
;
2095 newsect
->flags
|= SEC_LOAD
;
2096 if (hdr
->p_flags
& PF_X
)
2098 /* FIXME: all we known is that it has execute PERMISSION,
2100 newsect
->flags
|= SEC_CODE
;
2103 if (!(hdr
->p_flags
& PF_W
))
2105 newsect
->flags
|= SEC_READONLY
;
2110 sprintf (namebuf
, "%s%db", typename
, index
);
2111 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
2114 strcpy (name
, namebuf
);
2115 newsect
= bfd_make_section (abfd
, name
);
2116 if (newsect
== NULL
)
2118 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2119 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2120 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
2121 if (hdr
->p_type
== PT_LOAD
)
2123 newsect
->flags
|= SEC_ALLOC
;
2124 if (hdr
->p_flags
& PF_X
)
2125 newsect
->flags
|= SEC_CODE
;
2127 if (!(hdr
->p_flags
& PF_W
))
2128 newsect
->flags
|= SEC_READONLY
;
2135 bfd_section_from_phdr (abfd
, hdr
, index
)
2137 Elf_Internal_Phdr
*hdr
;
2140 struct elf_backend_data
*bed
;
2142 switch (hdr
->p_type
)
2145 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2148 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2151 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2154 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2157 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2159 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
2164 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2167 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2170 /* Check for any processor-specific program segment types.
2171 If no handler for them, default to making "segment" sections. */
2172 bed
= get_elf_backend_data (abfd
);
2173 if (bed
->elf_backend_section_from_phdr
)
2174 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2176 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2180 /* Initialize REL_HDR, the section-header for new section, containing
2181 relocations against ASECT. If USE_RELA_P is true, we use RELA
2182 relocations; otherwise, we use REL relocations. */
2185 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
2187 Elf_Internal_Shdr
*rel_hdr
;
2192 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2193 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2195 name
= bfd_alloc (abfd
, amt
);
2198 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2200 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2202 if (rel_hdr
->sh_name
== (unsigned int) -1)
2204 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2205 rel_hdr
->sh_entsize
= (use_rela_p
2206 ? bed
->s
->sizeof_rela
2207 : bed
->s
->sizeof_rel
);
2208 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
2209 rel_hdr
->sh_flags
= 0;
2210 rel_hdr
->sh_addr
= 0;
2211 rel_hdr
->sh_size
= 0;
2212 rel_hdr
->sh_offset
= 0;
2217 /* Set up an ELF internal section header for a section. */
2220 elf_fake_sections (abfd
, asect
, failedptrarg
)
2225 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2226 boolean
*failedptr
= (boolean
*) failedptrarg
;
2227 Elf_Internal_Shdr
*this_hdr
;
2231 /* We already failed; just get out of the bfd_map_over_sections
2236 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2238 this_hdr
->sh_name
= (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2239 asect
->name
, false);
2240 if (this_hdr
->sh_name
== (unsigned long) -1)
2246 this_hdr
->sh_flags
= 0;
2248 if ((asect
->flags
& SEC_ALLOC
) != 0
2249 || asect
->user_set_vma
)
2250 this_hdr
->sh_addr
= asect
->vma
;
2252 this_hdr
->sh_addr
= 0;
2254 this_hdr
->sh_offset
= 0;
2255 this_hdr
->sh_size
= asect
->_raw_size
;
2256 this_hdr
->sh_link
= 0;
2257 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2258 /* The sh_entsize and sh_info fields may have been set already by
2259 copy_private_section_data. */
2261 this_hdr
->bfd_section
= asect
;
2262 this_hdr
->contents
= NULL
;
2264 /* FIXME: This should not be based on section names. */
2265 if (strcmp (asect
->name
, ".dynstr") == 0)
2266 this_hdr
->sh_type
= SHT_STRTAB
;
2267 else if (strcmp (asect
->name
, ".hash") == 0)
2269 this_hdr
->sh_type
= SHT_HASH
;
2270 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2272 else if (strcmp (asect
->name
, ".dynsym") == 0)
2274 this_hdr
->sh_type
= SHT_DYNSYM
;
2275 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2277 else if (strcmp (asect
->name
, ".dynamic") == 0)
2279 this_hdr
->sh_type
= SHT_DYNAMIC
;
2280 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2282 else if (strncmp (asect
->name
, ".rela", 5) == 0
2283 && get_elf_backend_data (abfd
)->may_use_rela_p
)
2285 this_hdr
->sh_type
= SHT_RELA
;
2286 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2288 else if (strncmp (asect
->name
, ".rel", 4) == 0
2289 && get_elf_backend_data (abfd
)->may_use_rel_p
)
2291 this_hdr
->sh_type
= SHT_REL
;
2292 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2294 else if (strcmp (asect
->name
, ".init_array") == 0)
2295 this_hdr
->sh_type
= SHT_INIT_ARRAY
;
2296 else if (strcmp (asect
->name
, ".fini_array") == 0)
2297 this_hdr
->sh_type
= SHT_FINI_ARRAY
;
2298 else if (strcmp (asect
->name
, ".preinit_array") == 0)
2299 this_hdr
->sh_type
= SHT_PREINIT_ARRAY
;
2300 else if (strncmp (asect
->name
, ".note", 5) == 0)
2301 this_hdr
->sh_type
= SHT_NOTE
;
2302 else if (strncmp (asect
->name
, ".stab", 5) == 0
2303 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
2304 this_hdr
->sh_type
= SHT_STRTAB
;
2305 else if (strcmp (asect
->name
, ".gnu.version") == 0)
2307 this_hdr
->sh_type
= SHT_GNU_versym
;
2308 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2310 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
2312 this_hdr
->sh_type
= SHT_GNU_verdef
;
2313 this_hdr
->sh_entsize
= 0;
2314 /* objcopy or strip will copy over sh_info, but may not set
2315 cverdefs. The linker will set cverdefs, but sh_info will be
2317 if (this_hdr
->sh_info
== 0)
2318 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2320 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2321 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2323 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
2325 this_hdr
->sh_type
= SHT_GNU_verneed
;
2326 this_hdr
->sh_entsize
= 0;
2327 /* objcopy or strip will copy over sh_info, but may not set
2328 cverrefs. The linker will set cverrefs, but sh_info will be
2330 if (this_hdr
->sh_info
== 0)
2331 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2333 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2334 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2336 else if ((asect
->flags
& SEC_GROUP
) != 0)
2338 this_hdr
->sh_type
= SHT_GROUP
;
2339 this_hdr
->sh_entsize
= 4;
2341 else if ((asect
->flags
& SEC_ALLOC
) != 0
2342 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2343 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2344 this_hdr
->sh_type
= SHT_NOBITS
;
2346 this_hdr
->sh_type
= SHT_PROGBITS
;
2348 if ((asect
->flags
& SEC_ALLOC
) != 0)
2349 this_hdr
->sh_flags
|= SHF_ALLOC
;
2350 if ((asect
->flags
& SEC_READONLY
) == 0)
2351 this_hdr
->sh_flags
|= SHF_WRITE
;
2352 if ((asect
->flags
& SEC_CODE
) != 0)
2353 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2354 if ((asect
->flags
& SEC_MERGE
) != 0)
2356 this_hdr
->sh_flags
|= SHF_MERGE
;
2357 this_hdr
->sh_entsize
= asect
->entsize
;
2358 if ((asect
->flags
& SEC_STRINGS
) != 0)
2359 this_hdr
->sh_flags
|= SHF_STRINGS
;
2361 if (elf_group_name (asect
) != NULL
)
2362 this_hdr
->sh_flags
|= SHF_GROUP
;
2363 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2364 this_hdr
->sh_flags
|= SHF_TLS
;
2366 /* Check for processor-specific section types. */
2367 if (bed
->elf_backend_fake_sections
2368 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2371 /* If the section has relocs, set up a section header for the
2372 SHT_REL[A] section. If two relocation sections are required for
2373 this section, it is up to the processor-specific back-end to
2374 create the other. */
2375 if ((asect
->flags
& SEC_RELOC
) != 0
2376 && !_bfd_elf_init_reloc_shdr (abfd
,
2377 &elf_section_data (asect
)->rel_hdr
,
2379 elf_section_data (asect
)->use_rela_p
))
2383 /* Fill in the contents of a SHT_GROUP section. */
2386 set_group_contents (abfd
, sec
, failedptrarg
)
2389 PTR failedptrarg ATTRIBUTE_UNUSED
;
2391 boolean
*failedptr
= (boolean
*) failedptrarg
;
2392 unsigned long symindx
;
2393 asection
*elt
, *first
;
2395 struct bfd_link_order
*l
;
2398 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2402 /* If called from the assembler, swap_out_syms will have set up
2403 elf_section_syms; If called for "ld -r", the symbols won't yet
2404 be mapped, so emulate elf_bfd_final_link. */
2405 if (elf_section_syms (abfd
) != NULL
)
2406 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2408 symindx
= elf_section_data (sec
)->this_idx
;
2409 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2411 /* Nor will the contents be allocated for "ld -r" or objcopy. */
2413 if (sec
->contents
== NULL
)
2416 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2418 /* Arrange for the section to be written out. */
2419 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2420 if (sec
->contents
== NULL
)
2427 loc
= sec
->contents
+ sec
->_raw_size
;
2429 /* Get the pointer to the first section in the group that gas
2430 squirreled away here. objcopy arranges for this to be set to the
2431 start of the input section group. */
2432 first
= elt
= elf_next_in_group (sec
);
2434 /* First element is a flag word. Rest of section is elf section
2435 indices for all the sections of the group. Write them backwards
2436 just to keep the group in the same order as given in .section
2437 directives, not that it matters. */
2446 s
= s
->output_section
;
2449 idx
= elf_section_data (s
)->this_idx
;
2450 H_PUT_32 (abfd
, idx
, loc
);
2451 elt
= elf_next_in_group (elt
);
2456 /* If this is a relocatable link, then the above did nothing because
2457 SEC is the output section. Look through the input sections
2459 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2460 if (l
->type
== bfd_indirect_link_order
2461 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2466 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2467 elt
= elf_next_in_group (elt
);
2468 /* During a relocatable link, the lists are circular. */
2470 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2472 /* With ld -r, merging SHT_GROUP sections results in wasted space
2473 due to allowing for the flag word on each input. We may well
2474 duplicate entries too. */
2475 while ((loc
-= 4) > sec
->contents
)
2476 H_PUT_32 (abfd
, 0, loc
);
2478 if (loc
!= sec
->contents
)
2481 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2484 /* Assign all ELF section numbers. The dummy first section is handled here
2485 too. The link/info pointers for the standard section types are filled
2486 in here too, while we're at it. */
2489 assign_section_numbers (abfd
)
2492 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2494 unsigned int section_number
, secn
;
2495 Elf_Internal_Shdr
**i_shdrp
;
2500 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2502 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2504 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2506 if (section_number
== SHN_LORESERVE
)
2507 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2508 d
->this_idx
= section_number
++;
2509 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2510 if ((sec
->flags
& SEC_RELOC
) == 0)
2514 if (section_number
== SHN_LORESERVE
)
2515 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2516 d
->rel_idx
= section_number
++;
2517 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2522 if (section_number
== SHN_LORESERVE
)
2523 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2524 d
->rel_idx2
= section_number
++;
2525 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2531 if (section_number
== SHN_LORESERVE
)
2532 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2533 t
->shstrtab_section
= section_number
++;
2534 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2535 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2537 if (bfd_get_symcount (abfd
) > 0)
2539 if (section_number
== SHN_LORESERVE
)
2540 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2541 t
->symtab_section
= section_number
++;
2542 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2543 if (section_number
> SHN_LORESERVE
- 2)
2545 if (section_number
== SHN_LORESERVE
)
2546 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2547 t
->symtab_shndx_section
= section_number
++;
2548 t
->symtab_shndx_hdr
.sh_name
2549 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2550 ".symtab_shndx", false);
2551 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2554 if (section_number
== SHN_LORESERVE
)
2555 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2556 t
->strtab_section
= section_number
++;
2557 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2560 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2561 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2563 elf_numsections (abfd
) = section_number
;
2564 elf_elfheader (abfd
)->e_shnum
= section_number
;
2565 if (section_number
> SHN_LORESERVE
)
2566 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2568 /* Set up the list of section header pointers, in agreement with the
2570 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2571 i_shdrp
= (Elf_Internal_Shdr
**) bfd_alloc (abfd
, amt
);
2572 if (i_shdrp
== NULL
)
2575 amt
= sizeof (Elf_Internal_Shdr
);
2576 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2577 if (i_shdrp
[0] == NULL
)
2579 bfd_release (abfd
, i_shdrp
);
2582 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
2584 elf_elfsections (abfd
) = i_shdrp
;
2586 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2587 if (bfd_get_symcount (abfd
) > 0)
2589 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2590 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2592 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2593 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2595 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2596 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2598 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2600 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2604 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2605 if (d
->rel_idx
!= 0)
2606 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2607 if (d
->rel_idx2
!= 0)
2608 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2610 /* Fill in the sh_link and sh_info fields while we're at it. */
2612 /* sh_link of a reloc section is the section index of the symbol
2613 table. sh_info is the section index of the section to which
2614 the relocation entries apply. */
2615 if (d
->rel_idx
!= 0)
2617 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2618 d
->rel_hdr
.sh_info
= d
->this_idx
;
2620 if (d
->rel_idx2
!= 0)
2622 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2623 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2626 switch (d
->this_hdr
.sh_type
)
2630 /* A reloc section which we are treating as a normal BFD
2631 section. sh_link is the section index of the symbol
2632 table. sh_info is the section index of the section to
2633 which the relocation entries apply. We assume that an
2634 allocated reloc section uses the dynamic symbol table.
2635 FIXME: How can we be sure? */
2636 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2638 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2640 /* We look up the section the relocs apply to by name. */
2642 if (d
->this_hdr
.sh_type
== SHT_REL
)
2646 s
= bfd_get_section_by_name (abfd
, name
);
2648 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2652 /* We assume that a section named .stab*str is a stabs
2653 string section. We look for a section with the same name
2654 but without the trailing ``str'', and set its sh_link
2655 field to point to this section. */
2656 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2657 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2662 len
= strlen (sec
->name
);
2663 alc
= (char *) bfd_malloc ((bfd_size_type
) len
- 2);
2666 strncpy (alc
, sec
->name
, len
- 3);
2667 alc
[len
- 3] = '\0';
2668 s
= bfd_get_section_by_name (abfd
, alc
);
2672 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2674 /* This is a .stab section. */
2675 elf_section_data (s
)->this_hdr
.sh_entsize
=
2676 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2683 case SHT_GNU_verneed
:
2684 case SHT_GNU_verdef
:
2685 /* sh_link is the section header index of the string table
2686 used for the dynamic entries, or the symbol table, or the
2688 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2690 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2694 case SHT_GNU_versym
:
2695 /* sh_link is the section header index of the symbol table
2696 this hash table or version table is for. */
2697 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2699 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2703 d
->this_hdr
.sh_link
= t
->symtab_section
;
2707 for (secn
= 1; secn
< section_number
; ++secn
)
2708 if (i_shdrp
[secn
] == NULL
)
2709 i_shdrp
[secn
] = i_shdrp
[0];
2711 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2712 i_shdrp
[secn
]->sh_name
);
2716 /* Map symbol from it's internal number to the external number, moving
2717 all local symbols to be at the head of the list. */
2720 sym_is_global (abfd
, sym
)
2724 /* If the backend has a special mapping, use it. */
2725 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2726 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2729 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2730 || bfd_is_und_section (bfd_get_section (sym
))
2731 || bfd_is_com_section (bfd_get_section (sym
)));
2735 elf_map_symbols (abfd
)
2738 unsigned int symcount
= bfd_get_symcount (abfd
);
2739 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2740 asymbol
**sect_syms
;
2741 unsigned int num_locals
= 0;
2742 unsigned int num_globals
= 0;
2743 unsigned int num_locals2
= 0;
2744 unsigned int num_globals2
= 0;
2752 fprintf (stderr
, "elf_map_symbols\n");
2756 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2758 if (max_index
< asect
->index
)
2759 max_index
= asect
->index
;
2763 amt
= max_index
* sizeof (asymbol
*);
2764 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2765 if (sect_syms
== NULL
)
2767 elf_section_syms (abfd
) = sect_syms
;
2768 elf_num_section_syms (abfd
) = max_index
;
2770 /* Init sect_syms entries for any section symbols we have already
2771 decided to output. */
2772 for (idx
= 0; idx
< symcount
; idx
++)
2774 asymbol
*sym
= syms
[idx
];
2776 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2783 if (sec
->owner
!= NULL
)
2785 if (sec
->owner
!= abfd
)
2787 if (sec
->output_offset
!= 0)
2790 sec
= sec
->output_section
;
2792 /* Empty sections in the input files may have had a
2793 section symbol created for them. (See the comment
2794 near the end of _bfd_generic_link_output_symbols in
2795 linker.c). If the linker script discards such
2796 sections then we will reach this point. Since we know
2797 that we cannot avoid this case, we detect it and skip
2798 the abort and the assignment to the sect_syms array.
2799 To reproduce this particular case try running the
2800 linker testsuite test ld-scripts/weak.exp for an ELF
2801 port that uses the generic linker. */
2802 if (sec
->owner
== NULL
)
2805 BFD_ASSERT (sec
->owner
== abfd
);
2807 sect_syms
[sec
->index
] = syms
[idx
];
2812 /* Classify all of the symbols. */
2813 for (idx
= 0; idx
< symcount
; idx
++)
2815 if (!sym_is_global (abfd
, syms
[idx
]))
2821 /* We will be adding a section symbol for each BFD section. Most normal
2822 sections will already have a section symbol in outsymbols, but
2823 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2824 at least in that case. */
2825 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2827 if (sect_syms
[asect
->index
] == NULL
)
2829 if (!sym_is_global (abfd
, asect
->symbol
))
2836 /* Now sort the symbols so the local symbols are first. */
2837 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2838 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2840 if (new_syms
== NULL
)
2843 for (idx
= 0; idx
< symcount
; idx
++)
2845 asymbol
*sym
= syms
[idx
];
2848 if (!sym_is_global (abfd
, sym
))
2851 i
= num_locals
+ num_globals2
++;
2853 sym
->udata
.i
= i
+ 1;
2855 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2857 if (sect_syms
[asect
->index
] == NULL
)
2859 asymbol
*sym
= asect
->symbol
;
2862 sect_syms
[asect
->index
] = sym
;
2863 if (!sym_is_global (abfd
, sym
))
2866 i
= num_locals
+ num_globals2
++;
2868 sym
->udata
.i
= i
+ 1;
2872 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2874 elf_num_locals (abfd
) = num_locals
;
2875 elf_num_globals (abfd
) = num_globals
;
2879 /* Align to the maximum file alignment that could be required for any
2880 ELF data structure. */
2882 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2883 static INLINE file_ptr
2884 align_file_position (off
, align
)
2888 return (off
+ align
- 1) & ~(align
- 1);
2891 /* Assign a file position to a section, optionally aligning to the
2892 required section alignment. */
2895 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2896 Elf_Internal_Shdr
*i_shdrp
;
2904 al
= i_shdrp
->sh_addralign
;
2906 offset
= BFD_ALIGN (offset
, al
);
2908 i_shdrp
->sh_offset
= offset
;
2909 if (i_shdrp
->bfd_section
!= NULL
)
2910 i_shdrp
->bfd_section
->filepos
= offset
;
2911 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2912 offset
+= i_shdrp
->sh_size
;
2916 /* Compute the file positions we are going to put the sections at, and
2917 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2918 is not NULL, this is being called by the ELF backend linker. */
2921 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2923 struct bfd_link_info
*link_info
;
2925 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2927 struct bfd_strtab_hash
*strtab
;
2928 Elf_Internal_Shdr
*shstrtab_hdr
;
2930 if (abfd
->output_has_begun
)
2933 /* Do any elf backend specific processing first. */
2934 if (bed
->elf_backend_begin_write_processing
)
2935 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2937 if (! prep_headers (abfd
))
2940 /* Post process the headers if necessary. */
2941 if (bed
->elf_backend_post_process_headers
)
2942 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2945 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2949 if (!assign_section_numbers (abfd
))
2952 /* The backend linker builds symbol table information itself. */
2953 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2955 /* Non-zero if doing a relocatable link. */
2956 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2958 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2962 if (link_info
== NULL
|| link_info
->relocateable
)
2964 bfd_map_over_sections (abfd
, set_group_contents
, &failed
);
2969 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2970 /* sh_name was set in prep_headers. */
2971 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2972 shstrtab_hdr
->sh_flags
= 0;
2973 shstrtab_hdr
->sh_addr
= 0;
2974 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2975 shstrtab_hdr
->sh_entsize
= 0;
2976 shstrtab_hdr
->sh_link
= 0;
2977 shstrtab_hdr
->sh_info
= 0;
2978 /* sh_offset is set in assign_file_positions_except_relocs. */
2979 shstrtab_hdr
->sh_addralign
= 1;
2981 if (!assign_file_positions_except_relocs (abfd
))
2984 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2987 Elf_Internal_Shdr
*hdr
;
2989 off
= elf_tdata (abfd
)->next_file_pos
;
2991 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2992 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2994 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
2995 if (hdr
->sh_size
!= 0)
2996 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2998 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2999 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3001 elf_tdata (abfd
)->next_file_pos
= off
;
3003 /* Now that we know where the .strtab section goes, write it
3005 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3006 || ! _bfd_stringtab_emit (abfd
, strtab
))
3008 _bfd_stringtab_free (strtab
);
3011 abfd
->output_has_begun
= true;
3016 /* Create a mapping from a set of sections to a program segment. */
3018 static INLINE
struct elf_segment_map
*
3019 make_mapping (abfd
, sections
, from
, to
, phdr
)
3021 asection
**sections
;
3026 struct elf_segment_map
*m
;
3031 amt
= sizeof (struct elf_segment_map
);
3032 amt
+= (to
- from
- 1) * sizeof (asection
*);
3033 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3037 m
->p_type
= PT_LOAD
;
3038 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3039 m
->sections
[i
- from
] = *hdrpp
;
3040 m
->count
= to
- from
;
3042 if (from
== 0 && phdr
)
3044 /* Include the headers in the first PT_LOAD segment. */
3045 m
->includes_filehdr
= 1;
3046 m
->includes_phdrs
= 1;
3052 /* Set up a mapping from BFD sections to program segments. */
3055 map_sections_to_segments (abfd
)
3058 asection
**sections
= NULL
;
3062 struct elf_segment_map
*mfirst
;
3063 struct elf_segment_map
**pm
;
3064 struct elf_segment_map
*m
;
3066 unsigned int phdr_index
;
3067 bfd_vma maxpagesize
;
3069 boolean phdr_in_segment
= true;
3072 asection
*first_tls
= NULL
;
3073 asection
*dynsec
, *eh_frame_hdr
;
3076 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3079 if (bfd_count_sections (abfd
) == 0)
3082 /* Select the allocated sections, and sort them. */
3084 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
3085 sections
= (asection
**) bfd_malloc (amt
);
3086 if (sections
== NULL
)
3090 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3092 if ((s
->flags
& SEC_ALLOC
) != 0)
3098 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3101 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3103 /* Build the mapping. */
3108 /* If we have a .interp section, then create a PT_PHDR segment for
3109 the program headers and a PT_INTERP segment for the .interp
3111 s
= bfd_get_section_by_name (abfd
, ".interp");
3112 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3114 amt
= sizeof (struct elf_segment_map
);
3115 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3119 m
->p_type
= PT_PHDR
;
3120 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3121 m
->p_flags
= PF_R
| PF_X
;
3122 m
->p_flags_valid
= 1;
3123 m
->includes_phdrs
= 1;
3128 amt
= sizeof (struct elf_segment_map
);
3129 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3133 m
->p_type
= PT_INTERP
;
3141 /* Look through the sections. We put sections in the same program
3142 segment when the start of the second section can be placed within
3143 a few bytes of the end of the first section. */
3146 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
3148 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3150 && (dynsec
->flags
& SEC_LOAD
) == 0)
3153 /* Deal with -Ttext or something similar such that the first section
3154 is not adjacent to the program headers. This is an
3155 approximation, since at this point we don't know exactly how many
3156 program headers we will need. */
3159 bfd_size_type phdr_size
;
3161 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3163 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3164 if ((abfd
->flags
& D_PAGED
) == 0
3165 || sections
[0]->lma
< phdr_size
3166 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3167 phdr_in_segment
= false;
3170 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3173 boolean new_segment
;
3177 /* See if this section and the last one will fit in the same
3180 if (last_hdr
== NULL
)
3182 /* If we don't have a segment yet, then we don't need a new
3183 one (we build the last one after this loop). */
3184 new_segment
= false;
3186 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3188 /* If this section has a different relation between the
3189 virtual address and the load address, then we need a new
3193 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3194 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3196 /* If putting this section in this segment would force us to
3197 skip a page in the segment, then we need a new segment. */
3200 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
3201 && (hdr
->flags
& SEC_LOAD
) != 0)
3203 /* We don't want to put a loadable section after a
3204 nonloadable section in the same segment. */
3207 else if ((abfd
->flags
& D_PAGED
) == 0)
3209 /* If the file is not demand paged, which means that we
3210 don't require the sections to be correctly aligned in the
3211 file, then there is no other reason for a new segment. */
3212 new_segment
= false;
3215 && (hdr
->flags
& SEC_READONLY
) == 0
3216 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3219 /* We don't want to put a writable section in a read only
3220 segment, unless they are on the same page in memory
3221 anyhow. We already know that the last section does not
3222 bring us past the current section on the page, so the
3223 only case in which the new section is not on the same
3224 page as the previous section is when the previous section
3225 ends precisely on a page boundary. */
3230 /* Otherwise, we can use the same segment. */
3231 new_segment
= false;
3236 if ((hdr
->flags
& SEC_READONLY
) == 0)
3242 /* We need a new program segment. We must create a new program
3243 header holding all the sections from phdr_index until hdr. */
3245 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3252 if ((hdr
->flags
& SEC_READONLY
) == 0)
3259 phdr_in_segment
= false;
3262 /* Create a final PT_LOAD program segment. */
3263 if (last_hdr
!= NULL
)
3265 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3273 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3276 amt
= sizeof (struct elf_segment_map
);
3277 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3281 m
->p_type
= PT_DYNAMIC
;
3283 m
->sections
[0] = dynsec
;
3289 /* For each loadable .note section, add a PT_NOTE segment. We don't
3290 use bfd_get_section_by_name, because if we link together
3291 nonloadable .note sections and loadable .note sections, we will
3292 generate two .note sections in the output file. FIXME: Using
3293 names for section types is bogus anyhow. */
3294 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3296 if ((s
->flags
& SEC_LOAD
) != 0
3297 && strncmp (s
->name
, ".note", 5) == 0)
3299 amt
= sizeof (struct elf_segment_map
);
3300 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3304 m
->p_type
= PT_NOTE
;
3311 if (s
->flags
& SEC_THREAD_LOCAL
)
3319 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3324 amt
= sizeof (struct elf_segment_map
);
3325 amt
+= (tls_count
- 1) * sizeof (asection
*);
3326 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3331 m
->count
= tls_count
;
3332 /* Mandated PF_R. */
3334 m
->p_flags_valid
= 1;
3335 for (i
= 0; i
< tls_count
; ++i
)
3337 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3338 m
->sections
[i
] = first_tls
;
3339 first_tls
= first_tls
->next
;
3346 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3348 eh_frame_hdr
= NULL
;
3349 if (elf_tdata (abfd
)->eh_frame_hdr
)
3350 eh_frame_hdr
= bfd_get_section_by_name (abfd
, ".eh_frame_hdr");
3351 if (eh_frame_hdr
!= NULL
&& (eh_frame_hdr
->flags
& SEC_LOAD
))
3353 amt
= sizeof (struct elf_segment_map
);
3354 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3358 m
->p_type
= PT_GNU_EH_FRAME
;
3360 m
->sections
[0] = eh_frame_hdr
;
3369 elf_tdata (abfd
)->segment_map
= mfirst
;
3373 if (sections
!= NULL
)
3378 /* Sort sections by address. */
3381 elf_sort_sections (arg1
, arg2
)
3385 const asection
*sec1
= *(const asection
**) arg1
;
3386 const asection
*sec2
= *(const asection
**) arg2
;
3388 /* Sort by LMA first, since this is the address used to
3389 place the section into a segment. */
3390 if (sec1
->lma
< sec2
->lma
)
3392 else if (sec1
->lma
> sec2
->lma
)
3395 /* Then sort by VMA. Normally the LMA and the VMA will be
3396 the same, and this will do nothing. */
3397 if (sec1
->vma
< sec2
->vma
)
3399 else if (sec1
->vma
> sec2
->vma
)
3402 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3404 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3410 /* If the indicies are the same, do not return 0
3411 here, but continue to try the next comparison. */
3412 if (sec1
->target_index
- sec2
->target_index
!= 0)
3413 return sec1
->target_index
- sec2
->target_index
;
3418 else if (TOEND (sec2
))
3423 /* Sort by size, to put zero sized sections
3424 before others at the same address. */
3426 if (sec1
->_raw_size
< sec2
->_raw_size
)
3428 if (sec1
->_raw_size
> sec2
->_raw_size
)
3431 return sec1
->target_index
- sec2
->target_index
;
3434 /* Assign file positions to the sections based on the mapping from
3435 sections to segments. This function also sets up some fields in
3436 the file header, and writes out the program headers. */
3439 assign_file_positions_for_segments (abfd
)
3442 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3444 struct elf_segment_map
*m
;
3446 Elf_Internal_Phdr
*phdrs
;
3448 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3449 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3450 Elf_Internal_Phdr
*p
;
3453 if (elf_tdata (abfd
)->segment_map
== NULL
)
3455 if (! map_sections_to_segments (abfd
))
3460 /* The placement algorithm assumes that non allocated sections are
3461 not in PT_LOAD segments. We ensure this here by removing such
3462 sections from the segment map. */
3463 for (m
= elf_tdata (abfd
)->segment_map
;
3467 unsigned int new_count
;
3470 if (m
->p_type
!= PT_LOAD
)
3474 for (i
= 0; i
< m
->count
; i
++)
3476 if ((m
->sections
[i
]->flags
& SEC_ALLOC
) != 0)
3479 m
->sections
[new_count
] = m
->sections
[i
];
3485 if (new_count
!= m
->count
)
3486 m
->count
= new_count
;
3490 if (bed
->elf_backend_modify_segment_map
)
3492 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
3497 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3500 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3501 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3502 elf_elfheader (abfd
)->e_phnum
= count
;
3507 /* If we already counted the number of program segments, make sure
3508 that we allocated enough space. This happens when SIZEOF_HEADERS
3509 is used in a linker script. */
3510 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3511 if (alloc
!= 0 && count
> alloc
)
3513 ((*_bfd_error_handler
)
3514 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3515 bfd_get_filename (abfd
), alloc
, count
));
3516 bfd_set_error (bfd_error_bad_value
);
3523 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3524 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
3528 off
= bed
->s
->sizeof_ehdr
;
3529 off
+= alloc
* bed
->s
->sizeof_phdr
;
3536 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3543 /* If elf_segment_map is not from map_sections_to_segments, the
3544 sections may not be correctly ordered. NOTE: sorting should
3545 not be done to the PT_NOTE section of a corefile, which may
3546 contain several pseudo-sections artificially created by bfd.
3547 Sorting these pseudo-sections breaks things badly. */
3549 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3550 && m
->p_type
== PT_NOTE
))
3551 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3554 p
->p_type
= m
->p_type
;
3555 p
->p_flags
= m
->p_flags
;
3557 if (p
->p_type
== PT_LOAD
3559 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3561 if ((abfd
->flags
& D_PAGED
) != 0)
3562 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
3565 bfd_size_type align
;
3568 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3570 bfd_size_type secalign
;
3572 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3573 if (secalign
> align
)
3577 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
3584 p
->p_vaddr
= m
->sections
[0]->vma
;
3586 if (m
->p_paddr_valid
)
3587 p
->p_paddr
= m
->p_paddr
;
3588 else if (m
->count
== 0)
3591 p
->p_paddr
= m
->sections
[0]->lma
;
3593 if (p
->p_type
== PT_LOAD
3594 && (abfd
->flags
& D_PAGED
) != 0)
3595 p
->p_align
= bed
->maxpagesize
;
3596 else if (m
->count
== 0)
3597 p
->p_align
= bed
->s
->file_align
;
3605 if (m
->includes_filehdr
)
3607 if (! m
->p_flags_valid
)
3610 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3611 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3614 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3616 if (p
->p_vaddr
< (bfd_vma
) off
)
3618 (*_bfd_error_handler
)
3619 (_("%s: Not enough room for program headers, try linking with -N"),
3620 bfd_get_filename (abfd
));
3621 bfd_set_error (bfd_error_bad_value
);
3626 if (! m
->p_paddr_valid
)
3629 if (p
->p_type
== PT_LOAD
)
3631 filehdr_vaddr
= p
->p_vaddr
;
3632 filehdr_paddr
= p
->p_paddr
;
3636 if (m
->includes_phdrs
)
3638 if (! m
->p_flags_valid
)
3641 if (m
->includes_filehdr
)
3643 if (p
->p_type
== PT_LOAD
)
3645 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3646 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3651 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3655 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3656 p
->p_vaddr
-= off
- p
->p_offset
;
3657 if (! m
->p_paddr_valid
)
3658 p
->p_paddr
-= off
- p
->p_offset
;
3661 if (p
->p_type
== PT_LOAD
)
3663 phdrs_vaddr
= p
->p_vaddr
;
3664 phdrs_paddr
= p
->p_paddr
;
3667 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3670 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3671 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3674 if (p
->p_type
== PT_LOAD
3675 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3677 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3683 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3684 p
->p_filesz
+= adjust
;
3685 p
->p_memsz
+= adjust
;
3691 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3695 bfd_size_type align
;
3699 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3701 /* The section may have artificial alignment forced by a
3702 link script. Notice this case by the gap between the
3703 cumulative phdr lma and the section's lma. */
3704 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3706 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3708 p
->p_memsz
+= adjust
;
3711 if ((flags
& SEC_LOAD
) != 0)
3712 p
->p_filesz
+= adjust
;
3715 if (p
->p_type
== PT_LOAD
)
3717 bfd_signed_vma adjust
;
3719 if ((flags
& SEC_LOAD
) != 0)
3721 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3725 else if ((flags
& SEC_ALLOC
) != 0)
3727 /* The section VMA must equal the file position
3728 modulo the page size. FIXME: I'm not sure if
3729 this adjustment is really necessary. We used to
3730 not have the SEC_LOAD case just above, and then
3731 this was necessary, but now I'm not sure. */
3732 if ((abfd
->flags
& D_PAGED
) != 0)
3733 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3735 adjust
= (sec
->vma
- voff
) % align
;
3744 (* _bfd_error_handler
) (_("\
3745 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3746 bfd_section_name (abfd
, sec
),
3751 p
->p_memsz
+= adjust
;
3754 if ((flags
& SEC_LOAD
) != 0)
3755 p
->p_filesz
+= adjust
;
3760 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3761 used in a linker script we may have a section with
3762 SEC_LOAD clear but which is supposed to have
3764 if ((flags
& SEC_LOAD
) != 0
3765 || (flags
& SEC_HAS_CONTENTS
) != 0)
3766 off
+= sec
->_raw_size
;
3768 if ((flags
& SEC_ALLOC
) != 0)
3769 voff
+= sec
->_raw_size
;
3772 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3774 /* The actual "note" segment has i == 0.
3775 This is the one that actually contains everything. */
3779 p
->p_filesz
= sec
->_raw_size
;
3780 off
+= sec
->_raw_size
;
3785 /* Fake sections -- don't need to be written. */
3788 flags
= sec
->flags
= 0;
3795 p
->p_memsz
+= sec
->_raw_size
;
3797 if ((flags
& SEC_LOAD
) != 0)
3798 p
->p_filesz
+= sec
->_raw_size
;
3800 if (p
->p_type
== PT_TLS
3801 && sec
->_raw_size
== 0
3802 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
3804 struct bfd_link_order
*o
;
3805 bfd_vma tbss_size
= 0;
3807 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
3808 if (tbss_size
< o
->offset
+ o
->size
)
3809 tbss_size
= o
->offset
+ o
->size
;
3811 p
->p_memsz
+= tbss_size
;
3814 if (align
> p
->p_align
3815 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3819 if (! m
->p_flags_valid
)
3822 if ((flags
& SEC_CODE
) != 0)
3824 if ((flags
& SEC_READONLY
) == 0)
3830 /* Now that we have set the section file positions, we can set up
3831 the file positions for the non PT_LOAD segments. */
3832 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3836 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3838 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3839 p
->p_offset
= m
->sections
[0]->filepos
;
3843 if (m
->includes_filehdr
)
3845 p
->p_vaddr
= filehdr_vaddr
;
3846 if (! m
->p_paddr_valid
)
3847 p
->p_paddr
= filehdr_paddr
;
3849 else if (m
->includes_phdrs
)
3851 p
->p_vaddr
= phdrs_vaddr
;
3852 if (! m
->p_paddr_valid
)
3853 p
->p_paddr
= phdrs_paddr
;
3858 /* If additional nonloadable filepos adjustments are required,
3860 if (bed
->set_nonloadable_filepos
)
3861 (*bed
->set_nonloadable_filepos
) (abfd
, phdrs
);
3863 /* Clear out any program headers we allocated but did not use. */
3864 for (; count
< alloc
; count
++, p
++)
3866 memset (p
, 0, sizeof *p
);
3867 p
->p_type
= PT_NULL
;
3870 elf_tdata (abfd
)->phdr
= phdrs
;
3872 elf_tdata (abfd
)->next_file_pos
= off
;
3874 /* Write out the program headers. */
3875 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3876 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3882 /* Get the size of the program header.
3884 If this is called by the linker before any of the section VMA's are set, it
3885 can't calculate the correct value for a strange memory layout. This only
3886 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3887 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3888 data segment (exclusive of .interp and .dynamic).
3890 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3891 will be two segments. */
3893 static bfd_size_type
3894 get_program_header_size (abfd
)
3899 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3901 /* We can't return a different result each time we're called. */
3902 if (elf_tdata (abfd
)->program_header_size
!= 0)
3903 return elf_tdata (abfd
)->program_header_size
;
3905 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3907 struct elf_segment_map
*m
;
3910 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3912 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3913 return elf_tdata (abfd
)->program_header_size
;
3916 /* Assume we will need exactly two PT_LOAD segments: one for text
3917 and one for data. */
3920 s
= bfd_get_section_by_name (abfd
, ".interp");
3921 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3923 /* If we have a loadable interpreter section, we need a
3924 PT_INTERP segment. In this case, assume we also need a
3925 PT_PHDR segment, although that may not be true for all
3930 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3932 /* We need a PT_DYNAMIC segment. */
3936 if (elf_tdata (abfd
)->eh_frame_hdr
3937 && bfd_get_section_by_name (abfd
, ".eh_frame_hdr") != NULL
)
3939 /* We need a PT_GNU_EH_FRAME segment. */
3943 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3945 if ((s
->flags
& SEC_LOAD
) != 0
3946 && strncmp (s
->name
, ".note", 5) == 0)
3948 /* We need a PT_NOTE segment. */
3953 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3955 if (s
->flags
& SEC_THREAD_LOCAL
)
3957 /* We need a PT_TLS segment. */
3963 /* Let the backend count up any program headers it might need. */
3964 if (bed
->elf_backend_additional_program_headers
)
3968 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3974 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3975 return elf_tdata (abfd
)->program_header_size
;
3978 /* Work out the file positions of all the sections. This is called by
3979 _bfd_elf_compute_section_file_positions. All the section sizes and
3980 VMAs must be known before this is called.
3982 We do not consider reloc sections at this point, unless they form
3983 part of the loadable image. Reloc sections are assigned file
3984 positions in assign_file_positions_for_relocs, which is called by
3985 write_object_contents and final_link.
3987 We also don't set the positions of the .symtab and .strtab here. */
3990 assign_file_positions_except_relocs (abfd
)
3993 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3994 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3995 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3996 unsigned int num_sec
= elf_numsections (abfd
);
3998 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4000 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4001 && bfd_get_format (abfd
) != bfd_core
)
4003 Elf_Internal_Shdr
**hdrpp
;
4006 /* Start after the ELF header. */
4007 off
= i_ehdrp
->e_ehsize
;
4009 /* We are not creating an executable, which means that we are
4010 not creating a program header, and that the actual order of
4011 the sections in the file is unimportant. */
4012 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4014 Elf_Internal_Shdr
*hdr
;
4017 if (hdr
->sh_type
== SHT_REL
4018 || hdr
->sh_type
== SHT_RELA
4019 || i
== tdata
->symtab_section
4020 || i
== tdata
->symtab_shndx_section
4021 || i
== tdata
->strtab_section
)
4023 hdr
->sh_offset
= -1;
4026 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4028 if (i
== SHN_LORESERVE
- 1)
4030 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4031 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4038 Elf_Internal_Shdr
**hdrpp
;
4040 /* Assign file positions for the loaded sections based on the
4041 assignment of sections to segments. */
4042 if (! assign_file_positions_for_segments (abfd
))
4045 /* Assign file positions for the other sections. */
4047 off
= elf_tdata (abfd
)->next_file_pos
;
4048 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4050 Elf_Internal_Shdr
*hdr
;
4053 if (hdr
->bfd_section
!= NULL
4054 && hdr
->bfd_section
->filepos
!= 0)
4055 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
4056 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4058 ((*_bfd_error_handler
)
4059 (_("%s: warning: allocated section `%s' not in segment"),
4060 bfd_get_filename (abfd
),
4061 (hdr
->bfd_section
== NULL
4063 : hdr
->bfd_section
->name
)));
4064 if ((abfd
->flags
& D_PAGED
) != 0)
4065 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
4067 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
4068 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4071 else if (hdr
->sh_type
== SHT_REL
4072 || hdr
->sh_type
== SHT_RELA
4073 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4074 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4075 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4076 hdr
->sh_offset
= -1;
4078 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4080 if (i
== SHN_LORESERVE
- 1)
4082 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4083 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4088 /* Place the section headers. */
4089 off
= align_file_position (off
, bed
->s
->file_align
);
4090 i_ehdrp
->e_shoff
= off
;
4091 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4093 elf_tdata (abfd
)->next_file_pos
= off
;
4102 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4103 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4104 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4105 struct elf_strtab_hash
*shstrtab
;
4106 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4108 i_ehdrp
= elf_elfheader (abfd
);
4109 i_shdrp
= elf_elfsections (abfd
);
4111 shstrtab
= _bfd_elf_strtab_init ();
4112 if (shstrtab
== NULL
)
4115 elf_shstrtab (abfd
) = shstrtab
;
4117 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4118 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4119 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4120 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4122 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4123 i_ehdrp
->e_ident
[EI_DATA
] =
4124 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4125 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4127 if ((abfd
->flags
& DYNAMIC
) != 0)
4128 i_ehdrp
->e_type
= ET_DYN
;
4129 else if ((abfd
->flags
& EXEC_P
) != 0)
4130 i_ehdrp
->e_type
= ET_EXEC
;
4131 else if (bfd_get_format (abfd
) == bfd_core
)
4132 i_ehdrp
->e_type
= ET_CORE
;
4134 i_ehdrp
->e_type
= ET_REL
;
4136 switch (bfd_get_arch (abfd
))
4138 case bfd_arch_unknown
:
4139 i_ehdrp
->e_machine
= EM_NONE
;
4142 /* There used to be a long list of cases here, each one setting
4143 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4144 in the corresponding bfd definition. To avoid duplication,
4145 the switch was removed. Machines that need special handling
4146 can generally do it in elf_backend_final_write_processing(),
4147 unless they need the information earlier than the final write.
4148 Such need can generally be supplied by replacing the tests for
4149 e_machine with the conditions used to determine it. */
4151 if (get_elf_backend_data (abfd
) != NULL
)
4152 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
4154 i_ehdrp
->e_machine
= EM_NONE
;
4157 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4158 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4160 /* No program header, for now. */
4161 i_ehdrp
->e_phoff
= 0;
4162 i_ehdrp
->e_phentsize
= 0;
4163 i_ehdrp
->e_phnum
= 0;
4165 /* Each bfd section is section header entry. */
4166 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4167 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4169 /* If we're building an executable, we'll need a program header table. */
4170 if (abfd
->flags
& EXEC_P
)
4172 /* It all happens later. */
4174 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
4176 /* elf_build_phdrs() returns a (NULL-terminated) array of
4177 Elf_Internal_Phdrs. */
4178 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
4179 i_ehdrp
->e_phoff
= outbase
;
4180 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
4185 i_ehdrp
->e_phentsize
= 0;
4187 i_ehdrp
->e_phoff
= 0;
4190 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4191 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
4192 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4193 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
4194 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4195 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
4196 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4197 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4198 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4204 /* Assign file positions for all the reloc sections which are not part
4205 of the loadable file image. */
4208 _bfd_elf_assign_file_positions_for_relocs (abfd
)
4212 unsigned int i
, num_sec
;
4213 Elf_Internal_Shdr
**shdrpp
;
4215 off
= elf_tdata (abfd
)->next_file_pos
;
4217 num_sec
= elf_numsections (abfd
);
4218 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4220 Elf_Internal_Shdr
*shdrp
;
4223 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4224 && shdrp
->sh_offset
== -1)
4225 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
4228 elf_tdata (abfd
)->next_file_pos
= off
;
4232 _bfd_elf_write_object_contents (abfd
)
4235 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4236 Elf_Internal_Ehdr
*i_ehdrp
;
4237 Elf_Internal_Shdr
**i_shdrp
;
4239 unsigned int count
, num_sec
;
4241 if (! abfd
->output_has_begun
4242 && ! _bfd_elf_compute_section_file_positions
4243 (abfd
, (struct bfd_link_info
*) NULL
))
4246 i_shdrp
= elf_elfsections (abfd
);
4247 i_ehdrp
= elf_elfheader (abfd
);
4250 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4254 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4256 /* After writing the headers, we need to write the sections too... */
4257 num_sec
= elf_numsections (abfd
);
4258 for (count
= 1; count
< num_sec
; count
++)
4260 if (bed
->elf_backend_section_processing
)
4261 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4262 if (i_shdrp
[count
]->contents
)
4264 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4266 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4267 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4270 if (count
== SHN_LORESERVE
- 1)
4271 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4274 /* Write out the section header names. */
4275 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4276 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4279 if (bed
->elf_backend_final_write_processing
)
4280 (*bed
->elf_backend_final_write_processing
) (abfd
,
4281 elf_tdata (abfd
)->linker
);
4283 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4287 _bfd_elf_write_corefile_contents (abfd
)
4290 /* Hopefully this can be done just like an object file. */
4291 return _bfd_elf_write_object_contents (abfd
);
4294 /* Given a section, search the header to find them. */
4297 _bfd_elf_section_from_bfd_section (abfd
, asect
)
4301 struct elf_backend_data
*bed
;
4304 if (elf_section_data (asect
) != NULL
4305 && elf_section_data (asect
)->this_idx
!= 0)
4306 return elf_section_data (asect
)->this_idx
;
4308 if (bfd_is_abs_section (asect
))
4310 else if (bfd_is_com_section (asect
))
4312 else if (bfd_is_und_section (asect
))
4316 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4317 int maxindex
= elf_numsections (abfd
);
4319 for (index
= 1; index
< maxindex
; index
++)
4321 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4323 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4329 bed
= get_elf_backend_data (abfd
);
4330 if (bed
->elf_backend_section_from_bfd_section
)
4334 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4339 bfd_set_error (bfd_error_nonrepresentable_section
);
4344 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4348 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
4350 asymbol
**asym_ptr_ptr
;
4352 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4354 flagword flags
= asym_ptr
->flags
;
4356 /* When gas creates relocations against local labels, it creates its
4357 own symbol for the section, but does put the symbol into the
4358 symbol chain, so udata is 0. When the linker is generating
4359 relocatable output, this section symbol may be for one of the
4360 input sections rather than the output section. */
4361 if (asym_ptr
->udata
.i
== 0
4362 && (flags
& BSF_SECTION_SYM
)
4363 && asym_ptr
->section
)
4367 if (asym_ptr
->section
->output_section
!= NULL
)
4368 indx
= asym_ptr
->section
->output_section
->index
;
4370 indx
= asym_ptr
->section
->index
;
4371 if (indx
< elf_num_section_syms (abfd
)
4372 && elf_section_syms (abfd
)[indx
] != NULL
)
4373 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4376 idx
= asym_ptr
->udata
.i
;
4380 /* This case can occur when using --strip-symbol on a symbol
4381 which is used in a relocation entry. */
4382 (*_bfd_error_handler
)
4383 (_("%s: symbol `%s' required but not present"),
4384 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4385 bfd_set_error (bfd_error_no_symbols
);
4392 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4393 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4394 elf_symbol_flags (flags
));
4402 /* Copy private BFD data. This copies any program header information. */
4405 copy_private_bfd_data (ibfd
, obfd
)
4409 Elf_Internal_Ehdr
* iehdr
;
4410 struct elf_segment_map
* map
;
4411 struct elf_segment_map
* map_first
;
4412 struct elf_segment_map
** pointer_to_map
;
4413 Elf_Internal_Phdr
* segment
;
4416 unsigned int num_segments
;
4417 boolean phdr_included
= false;
4418 bfd_vma maxpagesize
;
4419 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
4420 unsigned int phdr_adjust_num
= 0;
4421 struct elf_backend_data
* bed
;
4423 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4424 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4427 if (elf_tdata (ibfd
)->phdr
== NULL
)
4430 bed
= get_elf_backend_data (ibfd
);
4431 iehdr
= elf_elfheader (ibfd
);
4434 pointer_to_map
= &map_first
;
4436 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4437 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4439 /* Returns the end address of the segment + 1. */
4440 #define SEGMENT_END(segment, start) \
4441 (start + (segment->p_memsz > segment->p_filesz \
4442 ? segment->p_memsz : segment->p_filesz))
4444 /* Returns true if the given section is contained within
4445 the given segment. VMA addresses are compared. */
4446 #define IS_CONTAINED_BY_VMA(section, segment) \
4447 (section->vma >= segment->p_vaddr \
4448 && (section->vma + section->_raw_size) \
4449 <= (SEGMENT_END (segment, segment->p_vaddr)))
4451 /* Returns true if the given section is contained within
4452 the given segment. LMA addresses are compared. */
4453 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4454 (section->lma >= base \
4455 && (section->lma + section->_raw_size) \
4456 <= SEGMENT_END (segment, base))
4458 /* Returns true if the given section is contained within the
4459 given segment. Filepos addresses are compared in an elf
4460 backend function. */
4461 #define IS_CONTAINED_BY_FILEPOS(sec, seg, bed) \
4462 (bed->is_contained_by_filepos \
4463 && (*bed->is_contained_by_filepos) (sec, seg))
4465 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4466 #define IS_COREFILE_NOTE(p, s) \
4467 (p->p_type == PT_NOTE \
4468 && bfd_get_format (ibfd) == bfd_core \
4469 && s->vma == 0 && s->lma == 0 \
4470 && (bfd_vma) s->filepos >= p->p_offset \
4471 && (bfd_vma) s->filepos + s->_raw_size \
4472 <= p->p_offset + p->p_filesz)
4474 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4475 linker, which generates a PT_INTERP section with p_vaddr and
4476 p_memsz set to 0. */
4477 #define IS_SOLARIS_PT_INTERP(p, s) \
4479 && p->p_filesz > 0 \
4480 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4481 && s->_raw_size > 0 \
4482 && (bfd_vma) s->filepos >= p->p_offset \
4483 && ((bfd_vma) s->filepos + s->_raw_size \
4484 <= p->p_offset + p->p_filesz))
4486 /* Decide if the given section should be included in the given segment.
4487 A section will be included if:
4488 1. It is within the address space of the segment -- we use the LMA
4489 if that is set for the segment and the VMA otherwise,
4490 2. It is an allocated segment,
4491 3. There is an output section associated with it,
4492 4. The section has not already been allocated to a previous segment. */
4493 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4494 (((((segment->p_paddr \
4495 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4496 : IS_CONTAINED_BY_VMA (section, segment)) \
4497 || IS_SOLARIS_PT_INTERP (segment, section)) \
4498 && (section->flags & SEC_ALLOC) != 0) \
4499 || IS_COREFILE_NOTE (segment, section) \
4500 || (IS_CONTAINED_BY_FILEPOS (section, segment, bed) \
4501 && (section->flags & SEC_ALLOC) == 0)) \
4502 && section->output_section != NULL \
4503 && section->segment_mark == false)
4505 /* Returns true iff seg1 starts after the end of seg2. */
4506 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4507 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4509 /* Returns true iff seg1 and seg2 overlap. */
4510 #define SEGMENT_OVERLAPS(seg1, seg2) \
4511 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4513 /* Initialise the segment mark field. */
4514 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4515 section
->segment_mark
= false;
4517 /* Scan through the segments specified in the program header
4518 of the input BFD. For this first scan we look for overlaps
4519 in the loadable segments. These can be created by weird
4520 parameters to objcopy. */
4521 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4526 Elf_Internal_Phdr
*segment2
;
4528 if (segment
->p_type
!= PT_LOAD
)
4531 /* Determine if this segment overlaps any previous segments. */
4532 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4534 bfd_signed_vma extra_length
;
4536 if (segment2
->p_type
!= PT_LOAD
4537 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4540 /* Merge the two segments together. */
4541 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4543 /* Extend SEGMENT2 to include SEGMENT and then delete
4546 SEGMENT_END (segment
, segment
->p_vaddr
)
4547 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4549 if (extra_length
> 0)
4551 segment2
->p_memsz
+= extra_length
;
4552 segment2
->p_filesz
+= extra_length
;
4555 segment
->p_type
= PT_NULL
;
4557 /* Since we have deleted P we must restart the outer loop. */
4559 segment
= elf_tdata (ibfd
)->phdr
;
4564 /* Extend SEGMENT to include SEGMENT2 and then delete
4567 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4568 - SEGMENT_END (segment
, segment
->p_vaddr
);
4570 if (extra_length
> 0)
4572 segment
->p_memsz
+= extra_length
;
4573 segment
->p_filesz
+= extra_length
;
4576 segment2
->p_type
= PT_NULL
;
4581 /* The second scan attempts to assign sections to segments. */
4582 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4586 unsigned int section_count
;
4587 asection
** sections
;
4588 asection
* output_section
;
4590 bfd_vma matching_lma
;
4591 bfd_vma suggested_lma
;
4595 if (segment
->p_type
== PT_NULL
)
4598 /* Compute how many sections might be placed into this segment. */
4600 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4601 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4604 /* Allocate a segment map big enough to contain all of the
4605 sections we have selected. */
4606 amt
= sizeof (struct elf_segment_map
);
4607 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4608 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4612 /* Initialise the fields of the segment map. Default to
4613 using the physical address of the segment in the input BFD. */
4615 map
->p_type
= segment
->p_type
;
4616 map
->p_flags
= segment
->p_flags
;
4617 map
->p_flags_valid
= 1;
4618 map
->p_paddr
= segment
->p_paddr
;
4619 map
->p_paddr_valid
= 1;
4621 /* Determine if this segment contains the ELF file header
4622 and if it contains the program headers themselves. */
4623 map
->includes_filehdr
= (segment
->p_offset
== 0
4624 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4626 map
->includes_phdrs
= 0;
4628 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4630 map
->includes_phdrs
=
4631 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4632 && (segment
->p_offset
+ segment
->p_filesz
4633 >= ((bfd_vma
) iehdr
->e_phoff
4634 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4636 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4637 phdr_included
= true;
4640 if (section_count
== 0)
4642 /* Special segments, such as the PT_PHDR segment, may contain
4643 no sections, but ordinary, loadable segments should contain
4644 something. They are allowed by the ELF spec however, so only
4645 a warning is produced. */
4646 if (segment
->p_type
== PT_LOAD
)
4647 (*_bfd_error_handler
)
4648 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4649 bfd_archive_filename (ibfd
));
4652 *pointer_to_map
= map
;
4653 pointer_to_map
= &map
->next
;
4658 /* Now scan the sections in the input BFD again and attempt
4659 to add their corresponding output sections to the segment map.
4660 The problem here is how to handle an output section which has
4661 been moved (ie had its LMA changed). There are four possibilities:
4663 1. None of the sections have been moved.
4664 In this case we can continue to use the segment LMA from the
4667 2. All of the sections have been moved by the same amount.
4668 In this case we can change the segment's LMA to match the LMA
4669 of the first section.
4671 3. Some of the sections have been moved, others have not.
4672 In this case those sections which have not been moved can be
4673 placed in the current segment which will have to have its size,
4674 and possibly its LMA changed, and a new segment or segments will
4675 have to be created to contain the other sections.
4677 4. The sections have been moved, but not be the same amount.
4678 In this case we can change the segment's LMA to match the LMA
4679 of the first section and we will have to create a new segment
4680 or segments to contain the other sections.
4682 In order to save time, we allocate an array to hold the section
4683 pointers that we are interested in. As these sections get assigned
4684 to a segment, they are removed from this array. */
4686 amt
= (bfd_size_type
) section_count
* sizeof (asection
*);
4687 sections
= (asection
**) bfd_malloc (amt
);
4688 if (sections
== NULL
)
4691 /* Step One: Scan for segment vs section LMA conflicts.
4692 Also add the sections to the section array allocated above.
4693 Also add the sections to the current segment. In the common
4694 case, where the sections have not been moved, this means that
4695 we have completely filled the segment, and there is nothing
4701 for (j
= 0, section
= ibfd
->sections
;
4703 section
= section
->next
)
4705 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4707 output_section
= section
->output_section
;
4709 sections
[j
++] = section
;
4711 /* The Solaris native linker always sets p_paddr to 0.
4712 We try to catch that case here, and set it to the
4714 if (segment
->p_paddr
== 0
4715 && segment
->p_vaddr
!= 0
4717 && output_section
->lma
!= 0
4718 && (output_section
->vma
== (segment
->p_vaddr
4719 + (map
->includes_filehdr
4722 + (map
->includes_phdrs
4724 * iehdr
->e_phentsize
)
4726 map
->p_paddr
= segment
->p_vaddr
;
4728 /* Match up the physical address of the segment with the
4729 LMA address of the output section. */
4730 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4731 || IS_CONTAINED_BY_FILEPOS (section
, segment
, bed
)
4732 || IS_COREFILE_NOTE (segment
, section
))
4734 if (matching_lma
== 0)
4735 matching_lma
= output_section
->lma
;
4737 /* We assume that if the section fits within the segment
4738 then it does not overlap any other section within that
4740 map
->sections
[isec
++] = output_section
;
4742 else if (suggested_lma
== 0)
4743 suggested_lma
= output_section
->lma
;
4747 BFD_ASSERT (j
== section_count
);
4749 /* Step Two: Adjust the physical address of the current segment,
4751 if (isec
== section_count
)
4753 /* All of the sections fitted within the segment as currently
4754 specified. This is the default case. Add the segment to
4755 the list of built segments and carry on to process the next
4756 program header in the input BFD. */
4757 map
->count
= section_count
;
4758 *pointer_to_map
= map
;
4759 pointer_to_map
= &map
->next
;
4766 if (matching_lma
!= 0)
4768 /* At least one section fits inside the current segment.
4769 Keep it, but modify its physical address to match the
4770 LMA of the first section that fitted. */
4771 map
->p_paddr
= matching_lma
;
4775 /* None of the sections fitted inside the current segment.
4776 Change the current segment's physical address to match
4777 the LMA of the first section. */
4778 map
->p_paddr
= suggested_lma
;
4781 /* Offset the segment physical address from the lma
4782 to allow for space taken up by elf headers. */
4783 if (map
->includes_filehdr
)
4784 map
->p_paddr
-= iehdr
->e_ehsize
;
4786 if (map
->includes_phdrs
)
4788 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4790 /* iehdr->e_phnum is just an estimate of the number
4791 of program headers that we will need. Make a note
4792 here of the number we used and the segment we chose
4793 to hold these headers, so that we can adjust the
4794 offset when we know the correct value. */
4795 phdr_adjust_num
= iehdr
->e_phnum
;
4796 phdr_adjust_seg
= map
;
4800 /* Step Three: Loop over the sections again, this time assigning
4801 those that fit to the current segment and removing them from the
4802 sections array; but making sure not to leave large gaps. Once all
4803 possible sections have been assigned to the current segment it is
4804 added to the list of built segments and if sections still remain
4805 to be assigned, a new segment is constructed before repeating
4813 /* Fill the current segment with sections that fit. */
4814 for (j
= 0; j
< section_count
; j
++)
4816 section
= sections
[j
];
4818 if (section
== NULL
)
4821 output_section
= section
->output_section
;
4823 BFD_ASSERT (output_section
!= NULL
);
4825 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4826 || IS_COREFILE_NOTE (segment
, section
))
4828 if (map
->count
== 0)
4830 /* If the first section in a segment does not start at
4831 the beginning of the segment, then something is
4833 if (output_section
->lma
!=
4835 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4836 + (map
->includes_phdrs
4837 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4843 asection
* prev_sec
;
4845 prev_sec
= map
->sections
[map
->count
- 1];
4847 /* If the gap between the end of the previous section
4848 and the start of this section is more than
4849 maxpagesize then we need to start a new segment. */
4850 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
4852 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4853 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
4854 > output_section
->lma
))
4856 if (suggested_lma
== 0)
4857 suggested_lma
= output_section
->lma
;
4863 map
->sections
[map
->count
++] = output_section
;
4866 section
->segment_mark
= true;
4868 else if (suggested_lma
== 0)
4869 suggested_lma
= output_section
->lma
;
4872 BFD_ASSERT (map
->count
> 0);
4874 /* Add the current segment to the list of built segments. */
4875 *pointer_to_map
= map
;
4876 pointer_to_map
= &map
->next
;
4878 if (isec
< section_count
)
4880 /* We still have not allocated all of the sections to
4881 segments. Create a new segment here, initialise it
4882 and carry on looping. */
4883 amt
= sizeof (struct elf_segment_map
);
4884 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4885 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4889 /* Initialise the fields of the segment map. Set the physical
4890 physical address to the LMA of the first section that has
4891 not yet been assigned. */
4893 map
->p_type
= segment
->p_type
;
4894 map
->p_flags
= segment
->p_flags
;
4895 map
->p_flags_valid
= 1;
4896 map
->p_paddr
= suggested_lma
;
4897 map
->p_paddr_valid
= 1;
4898 map
->includes_filehdr
= 0;
4899 map
->includes_phdrs
= 0;
4902 while (isec
< section_count
);
4907 /* The Solaris linker creates program headers in which all the
4908 p_paddr fields are zero. When we try to objcopy or strip such a
4909 file, we get confused. Check for this case, and if we find it
4910 reset the p_paddr_valid fields. */
4911 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4912 if (map
->p_paddr
!= 0)
4916 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4917 map
->p_paddr_valid
= 0;
4920 elf_tdata (obfd
)->segment_map
= map_first
;
4922 /* If we had to estimate the number of program headers that were
4923 going to be needed, then check our estimate now and adjust
4924 the offset if necessary. */
4925 if (phdr_adjust_seg
!= NULL
)
4929 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4932 if (count
> phdr_adjust_num
)
4933 phdr_adjust_seg
->p_paddr
4934 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4938 /* Final Step: Sort the segments into ascending order of physical
4940 if (map_first
!= NULL
)
4942 struct elf_segment_map
*prev
;
4945 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4947 /* Yes I know - its a bubble sort.... */
4948 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4950 /* Swap map and map->next. */
4951 prev
->next
= map
->next
;
4952 map
->next
= map
->next
->next
;
4953 prev
->next
->next
= map
;
4963 #undef IS_CONTAINED_BY_VMA
4964 #undef IS_CONTAINED_BY_LMA
4965 #undef IS_CONTAINED_BY_FILEPOS
4966 #undef IS_COREFILE_NOTE
4967 #undef IS_SOLARIS_PT_INTERP
4968 #undef INCLUDE_SECTION_IN_SEGMENT
4969 #undef SEGMENT_AFTER_SEGMENT
4970 #undef SEGMENT_OVERLAPS
4974 /* Copy private section information. This copies over the entsize
4975 field, and sometimes the info field. */
4978 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4984 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4985 const struct elf_backend_data
*bed
= get_elf_backend_data (ibfd
);
4987 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4988 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4991 /* Copy over private BFD data if it has not already been copied.
4992 This must be done here, rather than in the copy_private_bfd_data
4993 entry point, because the latter is called after the section
4994 contents have been set, which means that the program headers have
4995 already been worked out. The backend function provides a way to
4996 override the test conditions and code path for the call to
4997 copy_private_bfd_data. */
4998 if (bed
->copy_private_bfd_data_p
)
5000 if ((*bed
->copy_private_bfd_data_p
) (ibfd
, isec
, obfd
, osec
))
5001 if (! copy_private_bfd_data (ibfd
, obfd
))
5004 else if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
5008 /* Only set up the segments if there are no more SEC_ALLOC
5009 sections. FIXME: This won't do the right thing if objcopy is
5010 used to remove the last SEC_ALLOC section, since objcopy
5011 won't call this routine in that case. */
5012 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
5013 if ((s
->flags
& SEC_ALLOC
) != 0)
5017 if (! copy_private_bfd_data (ibfd
, obfd
))
5022 ihdr
= &elf_section_data (isec
)->this_hdr
;
5023 ohdr
= &elf_section_data (osec
)->this_hdr
;
5025 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
5027 if (ihdr
->sh_type
== SHT_SYMTAB
5028 || ihdr
->sh_type
== SHT_DYNSYM
5029 || ihdr
->sh_type
== SHT_GNU_verneed
5030 || ihdr
->sh_type
== SHT_GNU_verdef
)
5031 ohdr
->sh_info
= ihdr
->sh_info
;
5033 /* Set things up for objcopy. The output SHT_GROUP section will
5034 have its elf_next_in_group pointing back to the input group
5036 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5037 elf_group_name (osec
) = elf_group_name (isec
);
5039 elf_section_data (osec
)->use_rela_p
5040 = elf_section_data (isec
)->use_rela_p
;
5045 /* Copy private symbol information. If this symbol is in a section
5046 which we did not map into a BFD section, try to map the section
5047 index correctly. We use special macro definitions for the mapped
5048 section indices; these definitions are interpreted by the
5049 swap_out_syms function. */
5051 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5052 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5053 #define MAP_STRTAB (SHN_HIOS + 3)
5054 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5055 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5058 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
5064 elf_symbol_type
*isym
, *osym
;
5066 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5067 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5070 isym
= elf_symbol_from (ibfd
, isymarg
);
5071 osym
= elf_symbol_from (obfd
, osymarg
);
5075 && bfd_is_abs_section (isym
->symbol
.section
))
5079 shndx
= isym
->internal_elf_sym
.st_shndx
;
5080 if (shndx
== elf_onesymtab (ibfd
))
5081 shndx
= MAP_ONESYMTAB
;
5082 else if (shndx
== elf_dynsymtab (ibfd
))
5083 shndx
= MAP_DYNSYMTAB
;
5084 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
5086 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
5087 shndx
= MAP_SHSTRTAB
;
5088 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
5089 shndx
= MAP_SYM_SHNDX
;
5090 osym
->internal_elf_sym
.st_shndx
= shndx
;
5096 /* Swap out the symbols. */
5099 swap_out_syms (abfd
, sttp
, relocatable_p
)
5101 struct bfd_strtab_hash
**sttp
;
5104 struct elf_backend_data
*bed
;
5107 struct bfd_strtab_hash
*stt
;
5108 Elf_Internal_Shdr
*symtab_hdr
;
5109 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5110 Elf_Internal_Shdr
*symstrtab_hdr
;
5111 char *outbound_syms
;
5112 char *outbound_shndx
;
5116 if (!elf_map_symbols (abfd
))
5119 /* Dump out the symtabs. */
5120 stt
= _bfd_elf_stringtab_init ();
5124 bed
= get_elf_backend_data (abfd
);
5125 symcount
= bfd_get_symcount (abfd
);
5126 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5127 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5128 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5129 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5130 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5131 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5133 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5134 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5136 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
5137 outbound_syms
= bfd_alloc (abfd
, amt
);
5138 if (outbound_syms
== NULL
)
5140 symtab_hdr
->contents
= (PTR
) outbound_syms
;
5142 outbound_shndx
= NULL
;
5143 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5144 if (symtab_shndx_hdr
->sh_name
!= 0)
5146 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5147 outbound_shndx
= bfd_alloc (abfd
, amt
);
5148 if (outbound_shndx
== NULL
)
5150 memset (outbound_shndx
, 0, (unsigned long) amt
);
5151 symtab_shndx_hdr
->contents
= outbound_shndx
;
5152 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5153 symtab_shndx_hdr
->sh_size
= amt
;
5154 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5155 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5158 /* now generate the data (for "contents") */
5160 /* Fill in zeroth symbol and swap it out. */
5161 Elf_Internal_Sym sym
;
5167 sym
.st_shndx
= SHN_UNDEF
;
5168 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5169 outbound_syms
+= bed
->s
->sizeof_sym
;
5170 if (outbound_shndx
!= NULL
)
5171 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5174 syms
= bfd_get_outsymbols (abfd
);
5175 for (idx
= 0; idx
< symcount
; idx
++)
5177 Elf_Internal_Sym sym
;
5178 bfd_vma value
= syms
[idx
]->value
;
5179 elf_symbol_type
*type_ptr
;
5180 flagword flags
= syms
[idx
]->flags
;
5183 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5185 /* Local section symbols have no name. */
5190 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5193 if (sym
.st_name
== (unsigned long) -1)
5197 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5199 if ((flags
& BSF_SECTION_SYM
) == 0
5200 && bfd_is_com_section (syms
[idx
]->section
))
5202 /* ELF common symbols put the alignment into the `value' field,
5203 and the size into the `size' field. This is backwards from
5204 how BFD handles it, so reverse it here. */
5205 sym
.st_size
= value
;
5206 if (type_ptr
== NULL
5207 || type_ptr
->internal_elf_sym
.st_value
== 0)
5208 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5210 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5211 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5212 (abfd
, syms
[idx
]->section
);
5216 asection
*sec
= syms
[idx
]->section
;
5219 if (sec
->output_section
)
5221 value
+= sec
->output_offset
;
5222 sec
= sec
->output_section
;
5224 /* Don't add in the section vma for relocatable output. */
5225 if (! relocatable_p
)
5227 sym
.st_value
= value
;
5228 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5230 if (bfd_is_abs_section (sec
)
5232 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5234 /* This symbol is in a real ELF section which we did
5235 not create as a BFD section. Undo the mapping done
5236 by copy_private_symbol_data. */
5237 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5241 shndx
= elf_onesymtab (abfd
);
5244 shndx
= elf_dynsymtab (abfd
);
5247 shndx
= elf_tdata (abfd
)->strtab_section
;
5250 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5253 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5261 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5267 /* Writing this would be a hell of a lot easier if
5268 we had some decent documentation on bfd, and
5269 knew what to expect of the library, and what to
5270 demand of applications. For example, it
5271 appears that `objcopy' might not set the
5272 section of a symbol to be a section that is
5273 actually in the output file. */
5274 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5275 BFD_ASSERT (sec2
!= 0);
5276 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5277 BFD_ASSERT (shndx
!= -1);
5281 sym
.st_shndx
= shndx
;
5284 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5286 else if ((flags
& BSF_FUNCTION
) != 0)
5288 else if ((flags
& BSF_OBJECT
) != 0)
5293 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5296 /* Processor-specific types */
5297 if (type_ptr
!= NULL
5298 && bed
->elf_backend_get_symbol_type
)
5299 type
= ((*bed
->elf_backend_get_symbol_type
)
5300 (&type_ptr
->internal_elf_sym
, type
));
5302 if (flags
& BSF_SECTION_SYM
)
5304 if (flags
& BSF_GLOBAL
)
5305 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5307 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5309 else if (bfd_is_com_section (syms
[idx
]->section
))
5310 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5311 else if (bfd_is_und_section (syms
[idx
]->section
))
5312 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5316 else if (flags
& BSF_FILE
)
5317 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5320 int bind
= STB_LOCAL
;
5322 if (flags
& BSF_LOCAL
)
5324 else if (flags
& BSF_WEAK
)
5326 else if (flags
& BSF_GLOBAL
)
5329 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5332 if (type_ptr
!= NULL
)
5333 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5337 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5338 outbound_syms
+= bed
->s
->sizeof_sym
;
5339 if (outbound_shndx
!= NULL
)
5340 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5344 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5345 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5347 symstrtab_hdr
->sh_flags
= 0;
5348 symstrtab_hdr
->sh_addr
= 0;
5349 symstrtab_hdr
->sh_entsize
= 0;
5350 symstrtab_hdr
->sh_link
= 0;
5351 symstrtab_hdr
->sh_info
= 0;
5352 symstrtab_hdr
->sh_addralign
= 1;
5357 /* Return the number of bytes required to hold the symtab vector.
5359 Note that we base it on the count plus 1, since we will null terminate
5360 the vector allocated based on this size. However, the ELF symbol table
5361 always has a dummy entry as symbol #0, so it ends up even. */
5364 _bfd_elf_get_symtab_upper_bound (abfd
)
5369 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5371 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5372 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5374 symtab_size
-= sizeof (asymbol
*);
5380 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
5385 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5387 if (elf_dynsymtab (abfd
) == 0)
5389 bfd_set_error (bfd_error_invalid_operation
);
5393 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5394 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5396 symtab_size
-= sizeof (asymbol
*);
5402 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
5403 bfd
*abfd ATTRIBUTE_UNUSED
;
5406 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5409 /* Canonicalize the relocs. */
5412 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
5420 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5422 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
5425 tblptr
= section
->relocation
;
5426 for (i
= 0; i
< section
->reloc_count
; i
++)
5427 *relptr
++ = tblptr
++;
5431 return section
->reloc_count
;
5435 _bfd_elf_get_symtab (abfd
, alocation
)
5437 asymbol
**alocation
;
5439 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5440 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, false);
5443 bfd_get_symcount (abfd
) = symcount
;
5448 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
5450 asymbol
**alocation
;
5452 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5453 return bed
->s
->slurp_symbol_table (abfd
, alocation
, true);
5456 /* Return the size required for the dynamic reloc entries. Any
5457 section that was actually installed in the BFD, and has type
5458 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5459 considered to be a dynamic reloc section. */
5462 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
5468 if (elf_dynsymtab (abfd
) == 0)
5470 bfd_set_error (bfd_error_invalid_operation
);
5474 ret
= sizeof (arelent
*);
5475 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5476 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5477 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5478 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5479 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5480 * sizeof (arelent
*));
5485 /* Canonicalize the dynamic relocation entries. Note that we return
5486 the dynamic relocations as a single block, although they are
5487 actually associated with particular sections; the interface, which
5488 was designed for SunOS style shared libraries, expects that there
5489 is only one set of dynamic relocs. Any section that was actually
5490 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5491 the dynamic symbol table, is considered to be a dynamic reloc
5495 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
5500 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
5504 if (elf_dynsymtab (abfd
) == 0)
5506 bfd_set_error (bfd_error_invalid_operation
);
5510 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5512 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5514 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5515 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5516 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5521 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
5523 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5525 for (i
= 0; i
< count
; i
++)
5536 /* Read in the version information. */
5539 _bfd_elf_slurp_version_tables (abfd
)
5542 bfd_byte
*contents
= NULL
;
5545 if (elf_dynverdef (abfd
) != 0)
5547 Elf_Internal_Shdr
*hdr
;
5548 Elf_External_Verdef
*everdef
;
5549 Elf_Internal_Verdef
*iverdef
;
5550 Elf_Internal_Verdef
*iverdefarr
;
5551 Elf_Internal_Verdef iverdefmem
;
5553 unsigned int maxidx
;
5555 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5557 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5558 if (contents
== NULL
)
5560 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5561 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5564 /* We know the number of entries in the section but not the maximum
5565 index. Therefore we have to run through all entries and find
5567 everdef
= (Elf_External_Verdef
*) contents
;
5569 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5571 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5573 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5574 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5576 everdef
= ((Elf_External_Verdef
*)
5577 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5580 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5581 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
5582 if (elf_tdata (abfd
)->verdef
== NULL
)
5585 elf_tdata (abfd
)->cverdefs
= maxidx
;
5587 everdef
= (Elf_External_Verdef
*) contents
;
5588 iverdefarr
= elf_tdata (abfd
)->verdef
;
5589 for (i
= 0; i
< hdr
->sh_info
; i
++)
5591 Elf_External_Verdaux
*everdaux
;
5592 Elf_Internal_Verdaux
*iverdaux
;
5595 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5597 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5598 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5600 iverdef
->vd_bfd
= abfd
;
5602 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5603 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
5604 if (iverdef
->vd_auxptr
== NULL
)
5607 everdaux
= ((Elf_External_Verdaux
*)
5608 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5609 iverdaux
= iverdef
->vd_auxptr
;
5610 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5612 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5614 iverdaux
->vda_nodename
=
5615 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5616 iverdaux
->vda_name
);
5617 if (iverdaux
->vda_nodename
== NULL
)
5620 if (j
+ 1 < iverdef
->vd_cnt
)
5621 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5623 iverdaux
->vda_nextptr
= NULL
;
5625 everdaux
= ((Elf_External_Verdaux
*)
5626 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5629 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5631 if (i
+ 1 < hdr
->sh_info
)
5632 iverdef
->vd_nextdef
= iverdef
+ 1;
5634 iverdef
->vd_nextdef
= NULL
;
5636 everdef
= ((Elf_External_Verdef
*)
5637 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5644 if (elf_dynverref (abfd
) != 0)
5646 Elf_Internal_Shdr
*hdr
;
5647 Elf_External_Verneed
*everneed
;
5648 Elf_Internal_Verneed
*iverneed
;
5651 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5653 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5654 elf_tdata (abfd
)->verref
=
5655 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
5656 if (elf_tdata (abfd
)->verref
== NULL
)
5659 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5661 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5662 if (contents
== NULL
)
5664 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5665 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5668 everneed
= (Elf_External_Verneed
*) contents
;
5669 iverneed
= elf_tdata (abfd
)->verref
;
5670 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5672 Elf_External_Vernaux
*evernaux
;
5673 Elf_Internal_Vernaux
*ivernaux
;
5676 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5678 iverneed
->vn_bfd
= abfd
;
5680 iverneed
->vn_filename
=
5681 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5683 if (iverneed
->vn_filename
== NULL
)
5686 amt
= iverneed
->vn_cnt
;
5687 amt
*= sizeof (Elf_Internal_Vernaux
);
5688 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
5690 evernaux
= ((Elf_External_Vernaux
*)
5691 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5692 ivernaux
= iverneed
->vn_auxptr
;
5693 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5695 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5697 ivernaux
->vna_nodename
=
5698 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5699 ivernaux
->vna_name
);
5700 if (ivernaux
->vna_nodename
== NULL
)
5703 if (j
+ 1 < iverneed
->vn_cnt
)
5704 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5706 ivernaux
->vna_nextptr
= NULL
;
5708 evernaux
= ((Elf_External_Vernaux
*)
5709 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5712 if (i
+ 1 < hdr
->sh_info
)
5713 iverneed
->vn_nextref
= iverneed
+ 1;
5715 iverneed
->vn_nextref
= NULL
;
5717 everneed
= ((Elf_External_Verneed
*)
5718 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5728 if (contents
== NULL
)
5734 _bfd_elf_make_empty_symbol (abfd
)
5737 elf_symbol_type
*newsym
;
5738 bfd_size_type amt
= sizeof (elf_symbol_type
);
5740 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
5745 newsym
->symbol
.the_bfd
= abfd
;
5746 return &newsym
->symbol
;
5751 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
5752 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5756 bfd_symbol_info (symbol
, ret
);
5759 /* Return whether a symbol name implies a local symbol. Most targets
5760 use this function for the is_local_label_name entry point, but some
5764 _bfd_elf_is_local_label_name (abfd
, name
)
5765 bfd
*abfd ATTRIBUTE_UNUSED
;
5768 /* Normal local symbols start with ``.L''. */
5769 if (name
[0] == '.' && name
[1] == 'L')
5772 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5773 DWARF debugging symbols starting with ``..''. */
5774 if (name
[0] == '.' && name
[1] == '.')
5777 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5778 emitting DWARF debugging output. I suspect this is actually a
5779 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5780 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5781 underscore to be emitted on some ELF targets). For ease of use,
5782 we treat such symbols as local. */
5783 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5790 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5791 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5792 asymbol
*symbol ATTRIBUTE_UNUSED
;
5799 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5801 enum bfd_architecture arch
;
5802 unsigned long machine
;
5804 /* If this isn't the right architecture for this backend, and this
5805 isn't the generic backend, fail. */
5806 if (arch
!= get_elf_backend_data (abfd
)->arch
5807 && arch
!= bfd_arch_unknown
5808 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5811 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5814 /* Find the function to a particular section and offset,
5815 for error reporting. */
5818 elf_find_function (abfd
, section
, symbols
, offset
,
5819 filename_ptr
, functionname_ptr
)
5820 bfd
*abfd ATTRIBUTE_UNUSED
;
5824 const char **filename_ptr
;
5825 const char **functionname_ptr
;
5827 const char *filename
;
5836 for (p
= symbols
; *p
!= NULL
; p
++)
5840 q
= (elf_symbol_type
*) *p
;
5842 if (bfd_get_section (&q
->symbol
) != section
)
5845 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5850 filename
= bfd_asymbol_name (&q
->symbol
);
5854 if (q
->symbol
.section
== section
5855 && q
->symbol
.value
>= low_func
5856 && q
->symbol
.value
<= offset
)
5858 func
= (asymbol
*) q
;
5859 low_func
= q
->symbol
.value
;
5869 *filename_ptr
= filename
;
5870 if (functionname_ptr
)
5871 *functionname_ptr
= bfd_asymbol_name (func
);
5876 /* Find the nearest line to a particular section and offset,
5877 for error reporting. */
5880 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5881 filename_ptr
, functionname_ptr
, line_ptr
)
5886 const char **filename_ptr
;
5887 const char **functionname_ptr
;
5888 unsigned int *line_ptr
;
5892 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5893 filename_ptr
, functionname_ptr
,
5896 if (!*functionname_ptr
)
5897 elf_find_function (abfd
, section
, symbols
, offset
,
5898 *filename_ptr
? NULL
: filename_ptr
,
5904 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5905 filename_ptr
, functionname_ptr
,
5907 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5909 if (!*functionname_ptr
)
5910 elf_find_function (abfd
, section
, symbols
, offset
,
5911 *filename_ptr
? NULL
: filename_ptr
,
5917 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5918 &found
, filename_ptr
,
5919 functionname_ptr
, line_ptr
,
5920 &elf_tdata (abfd
)->line_info
))
5925 if (symbols
== NULL
)
5928 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5929 filename_ptr
, functionname_ptr
))
5937 _bfd_elf_sizeof_headers (abfd
, reloc
)
5943 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5945 ret
+= get_program_header_size (abfd
);
5950 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5955 bfd_size_type count
;
5957 Elf_Internal_Shdr
*hdr
;
5960 if (! abfd
->output_has_begun
5961 && ! _bfd_elf_compute_section_file_positions
5962 (abfd
, (struct bfd_link_info
*) NULL
))
5965 hdr
= &elf_section_data (section
)->this_hdr
;
5966 pos
= hdr
->sh_offset
+ offset
;
5967 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5968 || bfd_bwrite (location
, count
, abfd
) != count
)
5975 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5976 bfd
*abfd ATTRIBUTE_UNUSED
;
5977 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5978 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5985 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5988 Elf_Internal_Rel
*dst
;
5994 /* Try to convert a non-ELF reloc into an ELF one. */
5997 _bfd_elf_validate_reloc (abfd
, areloc
)
6001 /* Check whether we really have an ELF howto. */
6003 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
6005 bfd_reloc_code_real_type code
;
6006 reloc_howto_type
*howto
;
6008 /* Alien reloc: Try to determine its type to replace it with an
6009 equivalent ELF reloc. */
6011 if (areloc
->howto
->pc_relative
)
6013 switch (areloc
->howto
->bitsize
)
6016 code
= BFD_RELOC_8_PCREL
;
6019 code
= BFD_RELOC_12_PCREL
;
6022 code
= BFD_RELOC_16_PCREL
;
6025 code
= BFD_RELOC_24_PCREL
;
6028 code
= BFD_RELOC_32_PCREL
;
6031 code
= BFD_RELOC_64_PCREL
;
6037 howto
= bfd_reloc_type_lookup (abfd
, code
);
6039 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
6041 if (howto
->pcrel_offset
)
6042 areloc
->addend
+= areloc
->address
;
6044 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
6049 switch (areloc
->howto
->bitsize
)
6055 code
= BFD_RELOC_14
;
6058 code
= BFD_RELOC_16
;
6061 code
= BFD_RELOC_26
;
6064 code
= BFD_RELOC_32
;
6067 code
= BFD_RELOC_64
;
6073 howto
= bfd_reloc_type_lookup (abfd
, code
);
6077 areloc
->howto
= howto
;
6085 (*_bfd_error_handler
)
6086 (_("%s: unsupported relocation type %s"),
6087 bfd_archive_filename (abfd
), areloc
->howto
->name
);
6088 bfd_set_error (bfd_error_bad_value
);
6093 _bfd_elf_close_and_cleanup (abfd
)
6096 if (bfd_get_format (abfd
) == bfd_object
)
6098 if (elf_shstrtab (abfd
) != NULL
)
6099 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
6102 return _bfd_generic_close_and_cleanup (abfd
);
6105 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6106 in the relocation's offset. Thus we cannot allow any sort of sanity
6107 range-checking to interfere. There is nothing else to do in processing
6110 bfd_reloc_status_type
6111 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
6112 bfd
*abfd ATTRIBUTE_UNUSED
;
6113 arelent
*re ATTRIBUTE_UNUSED
;
6114 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
6115 PTR data ATTRIBUTE_UNUSED
;
6116 asection
*is ATTRIBUTE_UNUSED
;
6117 bfd
*obfd ATTRIBUTE_UNUSED
;
6118 char **errmsg ATTRIBUTE_UNUSED
;
6120 return bfd_reloc_ok
;
6123 /* Elf core file support. Much of this only works on native
6124 toolchains, since we rely on knowing the
6125 machine-dependent procfs structure in order to pick
6126 out details about the corefile. */
6128 #ifdef HAVE_SYS_PROCFS_H
6129 # include <sys/procfs.h>
6132 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6135 elfcore_make_pid (abfd
)
6138 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
6139 + (elf_tdata (abfd
)->core_pid
));
6142 /* If there isn't a section called NAME, make one, using
6143 data from SECT. Note, this function will generate a
6144 reference to NAME, so you shouldn't deallocate or
6148 elfcore_maybe_make_sect (abfd
, name
, sect
)
6155 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
6158 sect2
= bfd_make_section (abfd
, name
);
6162 sect2
->_raw_size
= sect
->_raw_size
;
6163 sect2
->filepos
= sect
->filepos
;
6164 sect2
->flags
= sect
->flags
;
6165 sect2
->alignment_power
= sect
->alignment_power
;
6169 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6170 actually creates up to two pseudosections:
6171 - For the single-threaded case, a section named NAME, unless
6172 such a section already exists.
6173 - For the multi-threaded case, a section named "NAME/PID", where
6174 PID is elfcore_make_pid (abfd).
6175 Both pseudosections have identical contents. */
6177 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
6184 char *threaded_name
;
6187 /* Build the section name. */
6189 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6190 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6191 if (threaded_name
== NULL
)
6193 strcpy (threaded_name
, buf
);
6195 sect
= bfd_make_section (abfd
, threaded_name
);
6198 sect
->_raw_size
= size
;
6199 sect
->filepos
= filepos
;
6200 sect
->flags
= SEC_HAS_CONTENTS
;
6201 sect
->alignment_power
= 2;
6203 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6206 /* prstatus_t exists on:
6208 linux 2.[01] + glibc
6212 #if defined (HAVE_PRSTATUS_T)
6213 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6216 elfcore_grok_prstatus (abfd
, note
)
6218 Elf_Internal_Note
*note
;
6223 if (note
->descsz
== sizeof (prstatus_t
))
6227 raw_size
= sizeof (prstat
.pr_reg
);
6228 offset
= offsetof (prstatus_t
, pr_reg
);
6229 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6231 /* Do not overwrite the core signal if it
6232 has already been set by another thread. */
6233 if (elf_tdata (abfd
)->core_signal
== 0)
6234 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6235 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6237 /* pr_who exists on:
6240 pr_who doesn't exist on:
6243 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6244 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6247 #if defined (HAVE_PRSTATUS32_T)
6248 else if (note
->descsz
== sizeof (prstatus32_t
))
6250 /* 64-bit host, 32-bit corefile */
6251 prstatus32_t prstat
;
6253 raw_size
= sizeof (prstat
.pr_reg
);
6254 offset
= offsetof (prstatus32_t
, pr_reg
);
6255 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6257 /* Do not overwrite the core signal if it
6258 has already been set by another thread. */
6259 if (elf_tdata (abfd
)->core_signal
== 0)
6260 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6261 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6263 /* pr_who exists on:
6266 pr_who doesn't exist on:
6269 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6270 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6273 #endif /* HAVE_PRSTATUS32_T */
6276 /* Fail - we don't know how to handle any other
6277 note size (ie. data object type). */
6281 /* Make a ".reg/999" section and a ".reg" section. */
6282 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6283 raw_size
, note
->descpos
+ offset
);
6285 #endif /* defined (HAVE_PRSTATUS_T) */
6287 /* Create a pseudosection containing the exact contents of NOTE. */
6289 elfcore_make_note_pseudosection (abfd
, name
, note
)
6292 Elf_Internal_Note
*note
;
6294 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6295 note
->descsz
, note
->descpos
);
6298 /* There isn't a consistent prfpregset_t across platforms,
6299 but it doesn't matter, because we don't have to pick this
6300 data structure apart. */
6303 elfcore_grok_prfpreg (abfd
, note
)
6305 Elf_Internal_Note
*note
;
6307 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6310 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6311 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6315 elfcore_grok_prxfpreg (abfd
, note
)
6317 Elf_Internal_Note
*note
;
6319 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6322 #if defined (HAVE_PRPSINFO_T)
6323 typedef prpsinfo_t elfcore_psinfo_t
;
6324 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6325 typedef prpsinfo32_t elfcore_psinfo32_t
;
6329 #if defined (HAVE_PSINFO_T)
6330 typedef psinfo_t elfcore_psinfo_t
;
6331 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6332 typedef psinfo32_t elfcore_psinfo32_t
;
6336 /* return a malloc'ed copy of a string at START which is at
6337 most MAX bytes long, possibly without a terminating '\0'.
6338 the copy will always have a terminating '\0'. */
6341 _bfd_elfcore_strndup (abfd
, start
, max
)
6347 char *end
= memchr (start
, '\0', max
);
6355 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
6359 memcpy (dups
, start
, len
);
6365 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6366 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
6369 elfcore_grok_psinfo (abfd
, note
)
6371 Elf_Internal_Note
*note
;
6373 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6375 elfcore_psinfo_t psinfo
;
6377 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6379 elf_tdata (abfd
)->core_program
6380 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6381 sizeof (psinfo
.pr_fname
));
6383 elf_tdata (abfd
)->core_command
6384 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6385 sizeof (psinfo
.pr_psargs
));
6387 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6388 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6390 /* 64-bit host, 32-bit corefile */
6391 elfcore_psinfo32_t psinfo
;
6393 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6395 elf_tdata (abfd
)->core_program
6396 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6397 sizeof (psinfo
.pr_fname
));
6399 elf_tdata (abfd
)->core_command
6400 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6401 sizeof (psinfo
.pr_psargs
));
6407 /* Fail - we don't know how to handle any other
6408 note size (ie. data object type). */
6412 /* Note that for some reason, a spurious space is tacked
6413 onto the end of the args in some (at least one anyway)
6414 implementations, so strip it off if it exists. */
6417 char *command
= elf_tdata (abfd
)->core_command
;
6418 int n
= strlen (command
);
6420 if (0 < n
&& command
[n
- 1] == ' ')
6421 command
[n
- 1] = '\0';
6426 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6428 #if defined (HAVE_PSTATUS_T)
6429 static boolean elfcore_grok_pstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6432 elfcore_grok_pstatus (abfd
, note
)
6434 Elf_Internal_Note
*note
;
6436 if (note
->descsz
== sizeof (pstatus_t
)
6437 #if defined (HAVE_PXSTATUS_T)
6438 || note
->descsz
== sizeof (pxstatus_t
)
6444 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6446 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6448 #if defined (HAVE_PSTATUS32_T)
6449 else if (note
->descsz
== sizeof (pstatus32_t
))
6451 /* 64-bit host, 32-bit corefile */
6454 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6456 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6459 /* Could grab some more details from the "representative"
6460 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6461 NT_LWPSTATUS note, presumably. */
6465 #endif /* defined (HAVE_PSTATUS_T) */
6467 #if defined (HAVE_LWPSTATUS_T)
6468 static boolean elfcore_grok_lwpstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6471 elfcore_grok_lwpstatus (abfd
, note
)
6473 Elf_Internal_Note
*note
;
6475 lwpstatus_t lwpstat
;
6480 if (note
->descsz
!= sizeof (lwpstat
)
6481 #if defined (HAVE_LWPXSTATUS_T)
6482 && note
->descsz
!= sizeof (lwpxstatus_t
)
6487 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6489 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6490 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6492 /* Make a ".reg/999" section. */
6494 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6495 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6500 sect
= bfd_make_section (abfd
, name
);
6504 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6505 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6506 sect
->filepos
= note
->descpos
6507 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6510 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6511 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6512 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6515 sect
->flags
= SEC_HAS_CONTENTS
;
6516 sect
->alignment_power
= 2;
6518 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6521 /* Make a ".reg2/999" section */
6523 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6524 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6529 sect
= bfd_make_section (abfd
, name
);
6533 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6534 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6535 sect
->filepos
= note
->descpos
6536 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6539 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6540 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6541 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6544 sect
->flags
= SEC_HAS_CONTENTS
;
6545 sect
->alignment_power
= 2;
6547 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6549 #endif /* defined (HAVE_LWPSTATUS_T) */
6551 #if defined (HAVE_WIN32_PSTATUS_T)
6553 elfcore_grok_win32pstatus (abfd
, note
)
6555 Elf_Internal_Note
*note
;
6560 win32_pstatus_t pstatus
;
6562 if (note
->descsz
< sizeof (pstatus
))
6565 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
6567 switch (pstatus
.data_type
)
6569 case NOTE_INFO_PROCESS
:
6570 /* FIXME: need to add ->core_command. */
6571 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6572 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6575 case NOTE_INFO_THREAD
:
6576 /* Make a ".reg/999" section. */
6577 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6579 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6585 sect
= bfd_make_section (abfd
, name
);
6589 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6590 sect
->filepos
= (note
->descpos
6591 + offsetof (struct win32_pstatus
,
6592 data
.thread_info
.thread_context
));
6593 sect
->flags
= SEC_HAS_CONTENTS
;
6594 sect
->alignment_power
= 2;
6596 if (pstatus
.data
.thread_info
.is_active_thread
)
6597 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6601 case NOTE_INFO_MODULE
:
6602 /* Make a ".module/xxxxxxxx" section. */
6603 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6605 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6611 sect
= bfd_make_section (abfd
, name
);
6616 sect
->_raw_size
= note
->descsz
;
6617 sect
->filepos
= note
->descpos
;
6618 sect
->flags
= SEC_HAS_CONTENTS
;
6619 sect
->alignment_power
= 2;
6628 #endif /* HAVE_WIN32_PSTATUS_T */
6631 elfcore_grok_note (abfd
, note
)
6633 Elf_Internal_Note
*note
;
6635 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6643 if (bed
->elf_backend_grok_prstatus
)
6644 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6646 #if defined (HAVE_PRSTATUS_T)
6647 return elfcore_grok_prstatus (abfd
, note
);
6652 #if defined (HAVE_PSTATUS_T)
6654 return elfcore_grok_pstatus (abfd
, note
);
6657 #if defined (HAVE_LWPSTATUS_T)
6659 return elfcore_grok_lwpstatus (abfd
, note
);
6662 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6663 return elfcore_grok_prfpreg (abfd
, note
);
6665 #if defined (HAVE_WIN32_PSTATUS_T)
6666 case NT_WIN32PSTATUS
:
6667 return elfcore_grok_win32pstatus (abfd
, note
);
6670 case NT_PRXFPREG
: /* Linux SSE extension */
6671 if (note
->namesz
== 5
6672 && ! strcmp (note
->namedata
, "LINUX"))
6673 return elfcore_grok_prxfpreg (abfd
, note
);
6679 if (bed
->elf_backend_grok_psinfo
)
6680 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6682 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6683 return elfcore_grok_psinfo (abfd
, note
);
6691 elfcore_netbsd_get_lwpid (note
, lwpidp
)
6692 Elf_Internal_Note
*note
;
6697 cp
= strchr (note
->namedata
, '@');
6700 *lwpidp
= atoi(cp
+ 1);
6707 elfcore_grok_netbsd_procinfo (abfd
, note
)
6709 Elf_Internal_Note
*note
;
6712 /* Signal number at offset 0x08. */
6713 elf_tdata (abfd
)->core_signal
6714 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6716 /* Process ID at offset 0x50. */
6717 elf_tdata (abfd
)->core_pid
6718 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6720 /* Command name at 0x7c (max 32 bytes, including nul). */
6721 elf_tdata (abfd
)->core_command
6722 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6728 elfcore_grok_netbsd_note (abfd
, note
)
6730 Elf_Internal_Note
*note
;
6734 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6735 elf_tdata (abfd
)->core_lwpid
= lwp
;
6737 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6739 /* NetBSD-specific core "procinfo". Note that we expect to
6740 find this note before any of the others, which is fine,
6741 since the kernel writes this note out first when it
6742 creates a core file. */
6744 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6747 /* As of Jan 2002 there are no other machine-independent notes
6748 defined for NetBSD core files. If the note type is less
6749 than the start of the machine-dependent note types, we don't
6752 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
6756 switch (bfd_get_arch (abfd
))
6758 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6759 PT_GETFPREGS == mach+2. */
6761 case bfd_arch_alpha
:
6762 case bfd_arch_sparc
:
6765 case NT_NETBSDCORE_FIRSTMACH
+0:
6766 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6768 case NT_NETBSDCORE_FIRSTMACH
+2:
6769 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6775 /* On all other arch's, PT_GETREGS == mach+1 and
6776 PT_GETFPREGS == mach+3. */
6781 case NT_NETBSDCORE_FIRSTMACH
+1:
6782 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6784 case NT_NETBSDCORE_FIRSTMACH
+3:
6785 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6794 /* Function: elfcore_write_note
6801 size of data for note
6804 End of buffer containing note. */
6807 elfcore_write_note (abfd
, buf
, bufsiz
, name
, type
, input
, size
)
6816 Elf_External_Note
*xnp
;
6817 int namesz
= strlen (name
);
6818 int newspace
= BFD_ALIGN (sizeof (Elf_External_Note
) + size
+ namesz
- 1, 4);
6821 p
= realloc (buf
, *bufsiz
+ newspace
);
6823 *bufsiz
+= newspace
;
6824 xnp
= (Elf_External_Note
*) dest
;
6825 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
6826 H_PUT_32 (abfd
, size
, xnp
->descsz
);
6827 H_PUT_32 (abfd
, type
, xnp
->type
);
6828 strcpy (xnp
->name
, name
);
6829 memcpy (xnp
->name
+ BFD_ALIGN (namesz
, 4), input
, size
);
6833 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6835 elfcore_write_prpsinfo (abfd
, buf
, bufsiz
, fname
, psargs
)
6843 char *note_name
= "CORE";
6845 #if defined (HAVE_PSINFO_T)
6847 note_type
= NT_PSINFO
;
6850 note_type
= NT_PRPSINFO
;
6853 memset (&data
, 0, sizeof (data
));
6854 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
6855 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
6856 return elfcore_write_note (abfd
, buf
, bufsiz
,
6857 note_name
, note_type
, &data
, sizeof (data
));
6859 #endif /* PSINFO_T or PRPSINFO_T */
6861 #if defined (HAVE_PRSTATUS_T)
6863 elfcore_write_prstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6872 char *note_name
= "CORE";
6874 memset (&prstat
, 0, sizeof (prstat
));
6875 prstat
.pr_pid
= pid
;
6876 prstat
.pr_cursig
= cursig
;
6877 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
6878 return elfcore_write_note (abfd
, buf
, bufsiz
,
6879 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
6881 #endif /* HAVE_PRSTATUS_T */
6883 #if defined (HAVE_LWPSTATUS_T)
6885 elfcore_write_lwpstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6893 lwpstatus_t lwpstat
;
6894 char *note_name
= "CORE";
6896 memset (&lwpstat
, 0, sizeof (lwpstat
));
6897 lwpstat
.pr_lwpid
= pid
>> 16;
6898 lwpstat
.pr_cursig
= cursig
;
6899 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6900 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
6901 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6903 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
6904 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
6906 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
6907 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
6910 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
6911 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
6913 #endif /* HAVE_LWPSTATUS_T */
6915 #if defined (HAVE_PSTATUS_T)
6917 elfcore_write_pstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6926 char *note_name
= "CORE";
6928 memset (&pstat
, 0, sizeof (pstat
));
6929 pstat
.pr_pid
= pid
& 0xffff;
6930 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
6931 NT_PSTATUS
, &pstat
, sizeof (pstat
));
6934 #endif /* HAVE_PSTATUS_T */
6937 elfcore_write_prfpreg (abfd
, buf
, bufsiz
, fpregs
, size
)
6944 char *note_name
= "CORE";
6945 return elfcore_write_note (abfd
, buf
, bufsiz
,
6946 note_name
, NT_FPREGSET
, fpregs
, size
);
6950 elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, xfpregs
, size
)
6957 char *note_name
= "LINUX";
6958 return elfcore_write_note (abfd
, buf
, bufsiz
,
6959 note_name
, NT_PRXFPREG
, xfpregs
, size
);
6963 elfcore_read_notes (abfd
, offset
, size
)
6974 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
6977 buf
= bfd_malloc (size
);
6981 if (bfd_bread (buf
, size
, abfd
) != size
)
6989 while (p
< buf
+ size
)
6991 /* FIXME: bad alignment assumption. */
6992 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
6993 Elf_Internal_Note in
;
6995 in
.type
= H_GET_32 (abfd
, xnp
->type
);
6997 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
6998 in
.namedata
= xnp
->name
;
7000 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
7001 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
7002 in
.descpos
= offset
+ (in
.descdata
- buf
);
7004 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
7006 if (! elfcore_grok_netbsd_note (abfd
, &in
))
7011 if (! elfcore_grok_note (abfd
, &in
))
7015 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
7022 /* Providing external access to the ELF program header table. */
7024 /* Return an upper bound on the number of bytes required to store a
7025 copy of ABFD's program header table entries. Return -1 if an error
7026 occurs; bfd_get_error will return an appropriate code. */
7029 bfd_get_elf_phdr_upper_bound (abfd
)
7032 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7034 bfd_set_error (bfd_error_wrong_format
);
7038 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
7041 /* Copy ABFD's program header table entries to *PHDRS. The entries
7042 will be stored as an array of Elf_Internal_Phdr structures, as
7043 defined in include/elf/internal.h. To find out how large the
7044 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7046 Return the number of program header table entries read, or -1 if an
7047 error occurs; bfd_get_error will return an appropriate code. */
7050 bfd_get_elf_phdrs (abfd
, phdrs
)
7056 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7058 bfd_set_error (bfd_error_wrong_format
);
7062 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
7063 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
7064 num_phdrs
* sizeof (Elf_Internal_Phdr
));
7070 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
7071 bfd
*abfd ATTRIBUTE_UNUSED
;
7076 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7078 i_ehdrp
= elf_elfheader (abfd
);
7079 if (i_ehdrp
== NULL
)
7080 sprintf_vma (buf
, value
);
7083 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7085 #if BFD_HOST_64BIT_LONG
7086 sprintf (buf
, "%016lx", value
);
7088 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
7089 _bfd_int64_low (value
));
7093 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
7096 sprintf_vma (buf
, value
);
7101 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
7102 bfd
*abfd ATTRIBUTE_UNUSED
;
7107 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7109 i_ehdrp
= elf_elfheader (abfd
);
7110 if (i_ehdrp
== NULL
)
7111 fprintf_vma ((FILE *) stream
, value
);
7114 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7116 #if BFD_HOST_64BIT_LONG
7117 fprintf ((FILE *) stream
, "%016lx", value
);
7119 fprintf ((FILE *) stream
, "%08lx%08lx",
7120 _bfd_int64_high (value
), _bfd_int64_low (value
));
7124 fprintf ((FILE *) stream
, "%08lx",
7125 (unsigned long) (value
& 0xffffffff));
7128 fprintf_vma ((FILE *) stream
, value
);
7132 enum elf_reloc_type_class
7133 _bfd_elf_reloc_type_class (rela
)
7134 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
7136 return reloc_class_normal
;
7139 /* For RELA architectures, return what the relocation value for
7140 relocation against a local symbol. */
7143 _bfd_elf_rela_local_sym (abfd
, sym
, sec
, rel
)
7145 Elf_Internal_Sym
*sym
;
7147 Elf_Internal_Rela
*rel
;
7151 relocation
= (sec
->output_section
->vma
7152 + sec
->output_offset
7154 if ((sec
->flags
& SEC_MERGE
)
7155 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
7156 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
7162 _bfd_merged_section_offset (abfd
, &msec
,
7163 elf_section_data (sec
)->sec_info
,
7164 sym
->st_value
+ rel
->r_addend
,
7167 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
7173 _bfd_elf_rel_local_sym (abfd
, sym
, psec
, addend
)
7175 Elf_Internal_Sym
*sym
;
7179 asection
*sec
= *psec
;
7181 if (elf_section_data (sec
)->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7182 return sym
->st_value
+ addend
;
7184 return _bfd_merged_section_offset (abfd
, psec
,
7185 elf_section_data (sec
)->sec_info
,
7186 sym
->st_value
+ addend
, (bfd_vma
) 0);
7190 _bfd_elf_section_offset (abfd
, info
, sec
, offset
)
7192 struct bfd_link_info
*info
;
7196 struct bfd_elf_section_data
*sec_data
;
7198 sec_data
= elf_section_data (sec
);
7199 switch (sec_data
->sec_info_type
)
7201 case ELF_INFO_TYPE_STABS
:
7202 return _bfd_stab_section_offset
7203 (abfd
, &elf_hash_table (info
)->merge_info
, sec
, &sec_data
->sec_info
,
7205 case ELF_INFO_TYPE_EH_FRAME
:
7206 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);