1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
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. */
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
35 /* For sparc64-cross-sparc32. */
44 static INLINE
struct elf_segment_map
*make_mapping
45 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, boolean
));
46 static boolean map_sections_to_segments
PARAMS ((bfd
*));
47 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
48 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
49 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
50 static boolean prep_headers
PARAMS ((bfd
*));
51 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**, int));
52 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
53 static char *elf_read
PARAMS ((bfd
*, long, unsigned int));
54 static void elf_fake_sections
PARAMS ((bfd
*, asection
*, PTR
));
55 static boolean assign_section_numbers
PARAMS ((bfd
*));
56 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
57 static boolean elf_map_symbols
PARAMS ((bfd
*));
58 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
59 static boolean elfcore_read_notes
PARAMS ((bfd
*, bfd_vma
, bfd_vma
));
60 static boolean elf_find_function
PARAMS ((bfd
*, asection
*, asymbol
**,
61 bfd_vma
, const char **,
63 static int elfcore_make_pid
PARAMS ((bfd
*));
64 static boolean elfcore_maybe_make_sect
PARAMS ((bfd
*, char *, asection
*));
65 static boolean elfcore_make_note_pseudosection
PARAMS ((bfd
*, char *,
66 Elf_Internal_Note
*));
67 static boolean elfcore_grok_prfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
68 static boolean elfcore_grok_prxfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
69 static boolean elfcore_grok_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
71 /* Swap version information in and out. The version information is
72 currently size independent. If that ever changes, this code will
73 need to move into elfcode.h. */
75 /* Swap in a Verdef structure. */
78 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
80 const Elf_External_Verdef
*src
;
81 Elf_Internal_Verdef
*dst
;
83 dst
->vd_version
= bfd_h_get_16 (abfd
, src
->vd_version
);
84 dst
->vd_flags
= bfd_h_get_16 (abfd
, src
->vd_flags
);
85 dst
->vd_ndx
= bfd_h_get_16 (abfd
, src
->vd_ndx
);
86 dst
->vd_cnt
= bfd_h_get_16 (abfd
, src
->vd_cnt
);
87 dst
->vd_hash
= bfd_h_get_32 (abfd
, src
->vd_hash
);
88 dst
->vd_aux
= bfd_h_get_32 (abfd
, src
->vd_aux
);
89 dst
->vd_next
= bfd_h_get_32 (abfd
, src
->vd_next
);
92 /* Swap out a Verdef structure. */
95 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
97 const Elf_Internal_Verdef
*src
;
98 Elf_External_Verdef
*dst
;
100 bfd_h_put_16 (abfd
, src
->vd_version
, dst
->vd_version
);
101 bfd_h_put_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
102 bfd_h_put_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
103 bfd_h_put_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
104 bfd_h_put_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
105 bfd_h_put_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
106 bfd_h_put_32 (abfd
, src
->vd_next
, dst
->vd_next
);
109 /* Swap in a Verdaux structure. */
112 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
114 const Elf_External_Verdaux
*src
;
115 Elf_Internal_Verdaux
*dst
;
117 dst
->vda_name
= bfd_h_get_32 (abfd
, src
->vda_name
);
118 dst
->vda_next
= bfd_h_get_32 (abfd
, src
->vda_next
);
121 /* Swap out a Verdaux structure. */
124 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
126 const Elf_Internal_Verdaux
*src
;
127 Elf_External_Verdaux
*dst
;
129 bfd_h_put_32 (abfd
, src
->vda_name
, dst
->vda_name
);
130 bfd_h_put_32 (abfd
, src
->vda_next
, dst
->vda_next
);
133 /* Swap in a Verneed structure. */
136 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
138 const Elf_External_Verneed
*src
;
139 Elf_Internal_Verneed
*dst
;
141 dst
->vn_version
= bfd_h_get_16 (abfd
, src
->vn_version
);
142 dst
->vn_cnt
= bfd_h_get_16 (abfd
, src
->vn_cnt
);
143 dst
->vn_file
= bfd_h_get_32 (abfd
, src
->vn_file
);
144 dst
->vn_aux
= bfd_h_get_32 (abfd
, src
->vn_aux
);
145 dst
->vn_next
= bfd_h_get_32 (abfd
, src
->vn_next
);
148 /* Swap out a Verneed structure. */
151 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
153 const Elf_Internal_Verneed
*src
;
154 Elf_External_Verneed
*dst
;
156 bfd_h_put_16 (abfd
, src
->vn_version
, dst
->vn_version
);
157 bfd_h_put_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
158 bfd_h_put_32 (abfd
, src
->vn_file
, dst
->vn_file
);
159 bfd_h_put_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
160 bfd_h_put_32 (abfd
, src
->vn_next
, dst
->vn_next
);
163 /* Swap in a Vernaux structure. */
166 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
168 const Elf_External_Vernaux
*src
;
169 Elf_Internal_Vernaux
*dst
;
171 dst
->vna_hash
= bfd_h_get_32 (abfd
, src
->vna_hash
);
172 dst
->vna_flags
= bfd_h_get_16 (abfd
, src
->vna_flags
);
173 dst
->vna_other
= bfd_h_get_16 (abfd
, src
->vna_other
);
174 dst
->vna_name
= bfd_h_get_32 (abfd
, src
->vna_name
);
175 dst
->vna_next
= bfd_h_get_32 (abfd
, src
->vna_next
);
178 /* Swap out a Vernaux structure. */
181 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
183 const Elf_Internal_Vernaux
*src
;
184 Elf_External_Vernaux
*dst
;
186 bfd_h_put_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
187 bfd_h_put_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
188 bfd_h_put_16 (abfd
, src
->vna_other
, dst
->vna_other
);
189 bfd_h_put_32 (abfd
, src
->vna_name
, dst
->vna_name
);
190 bfd_h_put_32 (abfd
, src
->vna_next
, dst
->vna_next
);
193 /* Swap in a Versym structure. */
196 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
198 const Elf_External_Versym
*src
;
199 Elf_Internal_Versym
*dst
;
201 dst
->vs_vers
= bfd_h_get_16 (abfd
, src
->vs_vers
);
204 /* Swap out a Versym structure. */
207 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
209 const Elf_Internal_Versym
*src
;
210 Elf_External_Versym
*dst
;
212 bfd_h_put_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
215 /* Standard ELF hash function. Do not change this function; you will
216 cause invalid hash tables to be generated. */
219 bfd_elf_hash (namearg
)
222 const unsigned char *name
= (const unsigned char *) namearg
;
227 while ((ch
= *name
++) != '\0')
230 if ((g
= (h
& 0xf0000000)) != 0)
233 /* The ELF ABI says `h &= ~g', but this is equivalent in
234 this case and on some machines one insn instead of two. */
241 /* Read a specified number of bytes at a specified offset in an ELF
242 file, into a newly allocated buffer, and return a pointer to the
246 elf_read (abfd
, offset
, size
)
253 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
255 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
257 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
259 if (bfd_get_error () != bfd_error_system_call
)
260 bfd_set_error (bfd_error_file_truncated
);
267 bfd_elf_mkobject (abfd
)
270 /* This just does initialization. */
271 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
272 elf_tdata (abfd
) = (struct elf_obj_tdata
*)
273 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
274 if (elf_tdata (abfd
) == 0)
276 /* Since everything is done at close time, do we need any
283 bfd_elf_mkcorefile (abfd
)
286 /* I think this can be done just like an object file. */
287 return bfd_elf_mkobject (abfd
);
291 bfd_elf_get_str_section (abfd
, shindex
)
293 unsigned int shindex
;
295 Elf_Internal_Shdr
**i_shdrp
;
296 char *shstrtab
= NULL
;
298 unsigned int shstrtabsize
;
300 i_shdrp
= elf_elfsections (abfd
);
301 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
304 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
305 if (shstrtab
== NULL
)
307 /* No cached one, attempt to read, and cache what we read. */
308 offset
= i_shdrp
[shindex
]->sh_offset
;
309 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
310 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
311 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
317 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
319 unsigned int shindex
;
320 unsigned int strindex
;
322 Elf_Internal_Shdr
*hdr
;
327 hdr
= elf_elfsections (abfd
)[shindex
];
329 if (hdr
->contents
== NULL
330 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
333 if (strindex
>= hdr
->sh_size
)
335 (*_bfd_error_handler
)
336 (_("%s: invalid string offset %u >= %lu for section `%s'"),
337 bfd_get_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
338 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
339 && strindex
== hdr
->sh_name
)
341 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
345 return ((char *) hdr
->contents
) + strindex
;
348 /* Make a BFD section from an ELF section. We store a pointer to the
349 BFD section in the bfd_section field of the header. */
352 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
354 Elf_Internal_Shdr
*hdr
;
359 struct elf_backend_data
*bed
;
361 if (hdr
->bfd_section
!= NULL
)
363 BFD_ASSERT (strcmp (name
,
364 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
368 newsect
= bfd_make_section_anyway (abfd
, name
);
372 newsect
->filepos
= hdr
->sh_offset
;
374 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
375 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
376 || ! bfd_set_section_alignment (abfd
, newsect
,
377 bfd_log2 (hdr
->sh_addralign
)))
380 flags
= SEC_NO_FLAGS
;
381 if (hdr
->sh_type
!= SHT_NOBITS
)
382 flags
|= SEC_HAS_CONTENTS
;
383 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
386 if (hdr
->sh_type
!= SHT_NOBITS
)
389 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
390 flags
|= SEC_READONLY
;
391 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
393 else if ((flags
& SEC_LOAD
) != 0)
395 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
398 newsect
->entsize
= hdr
->sh_entsize
;
399 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
400 flags
|= SEC_STRINGS
;
403 /* The debugging sections appear to be recognized only by name, not
406 static const char *debug_sec_names
[] =
415 for (i
= sizeof (debug_sec_names
) / sizeof (debug_sec_names
[0]); i
--;)
416 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
420 flags
|= SEC_DEBUGGING
;
423 /* As a GNU extension, if the name begins with .gnu.linkonce, we
424 only link a single copy of the section. This is used to support
425 g++. g++ will emit each template expansion in its own section.
426 The symbols will be defined as weak, so that multiple definitions
427 are permitted. The GNU linker extension is to actually discard
428 all but one of the sections. */
429 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
430 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
432 bed
= get_elf_backend_data (abfd
);
433 if (bed
->elf_backend_section_flags
)
434 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
437 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
440 if ((flags
& SEC_ALLOC
) != 0)
442 Elf_Internal_Phdr
*phdr
;
445 /* Look through the phdrs to see if we need to adjust the lma.
446 If all the p_paddr fields are zero, we ignore them, since
447 some ELF linkers produce such output. */
448 phdr
= elf_tdata (abfd
)->phdr
;
449 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
451 if (phdr
->p_paddr
!= 0)
454 if (i
< elf_elfheader (abfd
)->e_phnum
)
456 phdr
= elf_tdata (abfd
)->phdr
;
457 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
459 if (phdr
->p_type
== PT_LOAD
460 && phdr
->p_vaddr
!= phdr
->p_paddr
461 && phdr
->p_vaddr
<= hdr
->sh_addr
462 && (phdr
->p_vaddr
+ phdr
->p_memsz
463 >= hdr
->sh_addr
+ hdr
->sh_size
)
464 && ((flags
& SEC_LOAD
) == 0
465 || (phdr
->p_offset
<= (bfd_vma
) hdr
->sh_offset
466 && (phdr
->p_offset
+ phdr
->p_filesz
467 >= hdr
->sh_offset
+ hdr
->sh_size
))))
469 newsect
->lma
+= phdr
->p_paddr
- phdr
->p_vaddr
;
476 hdr
->bfd_section
= newsect
;
477 elf_section_data (newsect
)->this_hdr
= *hdr
;
487 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
490 Helper functions for GDB to locate the string tables.
491 Since BFD hides string tables from callers, GDB needs to use an
492 internal hook to find them. Sun's .stabstr, in particular,
493 isn't even pointed to by the .stab section, so ordinary
494 mechanisms wouldn't work to find it, even if we had some.
497 struct elf_internal_shdr
*
498 bfd_elf_find_section (abfd
, name
)
502 Elf_Internal_Shdr
**i_shdrp
;
507 i_shdrp
= elf_elfsections (abfd
);
510 shstrtab
= bfd_elf_get_str_section
511 (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
512 if (shstrtab
!= NULL
)
514 max
= elf_elfheader (abfd
)->e_shnum
;
515 for (i
= 1; i
< max
; i
++)
516 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
523 const char *const bfd_elf_section_type_names
[] = {
524 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
525 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
526 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
529 /* ELF relocs are against symbols. If we are producing relocateable
530 output, and the reloc is against an external symbol, and nothing
531 has given us any additional addend, the resulting reloc will also
532 be against the same symbol. In such a case, we don't want to
533 change anything about the way the reloc is handled, since it will
534 all be done at final link time. Rather than put special case code
535 into bfd_perform_relocation, all the reloc types use this howto
536 function. It just short circuits the reloc if producing
537 relocateable output against an external symbol. */
539 bfd_reloc_status_type
540 bfd_elf_generic_reloc (abfd
,
547 bfd
*abfd ATTRIBUTE_UNUSED
;
548 arelent
*reloc_entry
;
550 PTR data ATTRIBUTE_UNUSED
;
551 asection
*input_section
;
553 char **error_message ATTRIBUTE_UNUSED
;
555 if (output_bfd
!= (bfd
*) NULL
556 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
557 && (! reloc_entry
->howto
->partial_inplace
558 || reloc_entry
->addend
== 0))
560 reloc_entry
->address
+= input_section
->output_offset
;
564 return bfd_reloc_continue
;
567 /* Finish SHF_MERGE section merging. */
570 _bfd_elf_merge_sections (abfd
, info
)
572 struct bfd_link_info
*info
;
574 struct elf_link_hash_table
* hash_table
;
576 hash_table
= elf_hash_table (info
);
578 if (hash_table
== NULL
)
581 if (hash_table
->merge_info
)
582 _bfd_merge_sections (abfd
, hash_table
->merge_info
);
586 /* Print out the program headers. */
589 _bfd_elf_print_private_bfd_data (abfd
, farg
)
593 FILE *f
= (FILE *) farg
;
594 Elf_Internal_Phdr
*p
;
596 bfd_byte
*dynbuf
= NULL
;
598 p
= elf_tdata (abfd
)->phdr
;
603 fprintf (f
, _("\nProgram Header:\n"));
604 c
= elf_elfheader (abfd
)->e_phnum
;
605 for (i
= 0; i
< c
; i
++, p
++)
612 case PT_NULL
: s
= "NULL"; break;
613 case PT_LOAD
: s
= "LOAD"; break;
614 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
615 case PT_INTERP
: s
= "INTERP"; break;
616 case PT_NOTE
: s
= "NOTE"; break;
617 case PT_SHLIB
: s
= "SHLIB"; break;
618 case PT_PHDR
: s
= "PHDR"; break;
619 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
621 fprintf (f
, "%8s off 0x", s
);
622 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
623 fprintf (f
, " vaddr 0x");
624 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
625 fprintf (f
, " paddr 0x");
626 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
627 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
628 fprintf (f
, " filesz 0x");
629 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
630 fprintf (f
, " memsz 0x");
631 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
632 fprintf (f
, " flags %c%c%c",
633 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
634 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
635 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
636 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
637 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
642 s
= bfd_get_section_by_name (abfd
, ".dynamic");
647 bfd_byte
*extdyn
, *extdynend
;
649 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
651 fprintf (f
, _("\nDynamic Section:\n"));
653 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
656 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
660 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
663 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
665 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
666 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
669 extdynend
= extdyn
+ s
->_raw_size
;
670 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
672 Elf_Internal_Dyn dyn
;
677 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
679 if (dyn
.d_tag
== DT_NULL
)
686 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
690 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
691 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
692 case DT_PLTGOT
: name
= "PLTGOT"; break;
693 case DT_HASH
: name
= "HASH"; break;
694 case DT_STRTAB
: name
= "STRTAB"; break;
695 case DT_SYMTAB
: name
= "SYMTAB"; break;
696 case DT_RELA
: name
= "RELA"; break;
697 case DT_RELASZ
: name
= "RELASZ"; break;
698 case DT_RELAENT
: name
= "RELAENT"; break;
699 case DT_STRSZ
: name
= "STRSZ"; break;
700 case DT_SYMENT
: name
= "SYMENT"; break;
701 case DT_INIT
: name
= "INIT"; break;
702 case DT_FINI
: name
= "FINI"; break;
703 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
704 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
705 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
706 case DT_REL
: name
= "REL"; break;
707 case DT_RELSZ
: name
= "RELSZ"; break;
708 case DT_RELENT
: name
= "RELENT"; break;
709 case DT_PLTREL
: name
= "PLTREL"; break;
710 case DT_DEBUG
: name
= "DEBUG"; break;
711 case DT_TEXTREL
: name
= "TEXTREL"; break;
712 case DT_JMPREL
: name
= "JMPREL"; break;
713 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
714 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
715 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
716 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
717 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
718 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
719 case DT_FLAGS
: name
= "FLAGS"; break;
720 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
721 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
722 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
723 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
724 case DT_MOVEENT
: name
= "MOVEENT"; break;
725 case DT_MOVESZ
: name
= "MOVESZ"; break;
726 case DT_FEATURE
: name
= "FEATURE"; break;
727 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
728 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
729 case DT_SYMINENT
: name
= "SYMINENT"; break;
730 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
731 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
732 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
733 case DT_PLTPAD
: name
= "PLTPAD"; break;
734 case DT_MOVETAB
: name
= "MOVETAB"; break;
735 case DT_SYMINFO
: name
= "SYMINFO"; break;
736 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
737 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
738 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
739 case DT_VERSYM
: name
= "VERSYM"; break;
740 case DT_VERDEF
: name
= "VERDEF"; break;
741 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
742 case DT_VERNEED
: name
= "VERNEED"; break;
743 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
744 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
745 case DT_USED
: name
= "USED"; break;
746 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
749 fprintf (f
, " %-11s ", name
);
751 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
756 string
= bfd_elf_string_from_elf_section (abfd
, link
,
760 fprintf (f
, "%s", string
);
769 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
770 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
772 if (! _bfd_elf_slurp_version_tables (abfd
))
776 if (elf_dynverdef (abfd
) != 0)
778 Elf_Internal_Verdef
*t
;
780 fprintf (f
, _("\nVersion definitions:\n"));
781 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
783 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
784 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
785 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
787 Elf_Internal_Verdaux
*a
;
790 for (a
= t
->vd_auxptr
->vda_nextptr
;
793 fprintf (f
, "%s ", a
->vda_nodename
);
799 if (elf_dynverref (abfd
) != 0)
801 Elf_Internal_Verneed
*t
;
803 fprintf (f
, _("\nVersion References:\n"));
804 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
806 Elf_Internal_Vernaux
*a
;
808 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
809 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
810 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
811 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
823 /* Display ELF-specific fields of a symbol. */
826 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
830 bfd_print_symbol_type how
;
832 FILE *file
= (FILE *) filep
;
835 case bfd_print_symbol_name
:
836 fprintf (file
, "%s", symbol
->name
);
838 case bfd_print_symbol_more
:
839 fprintf (file
, "elf ");
840 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
841 fprintf (file
, " %lx", (long) symbol
->flags
);
843 case bfd_print_symbol_all
:
845 const char *section_name
;
846 const char *name
= NULL
;
847 struct elf_backend_data
*bed
;
848 unsigned char st_other
;
850 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
852 bed
= get_elf_backend_data (abfd
);
853 if (bed
->elf_backend_print_symbol_all
)
854 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
859 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
862 fprintf (file
, " %s\t", section_name
);
863 /* Print the "other" value for a symbol. For common symbols,
864 we've already printed the size; now print the alignment.
865 For other symbols, we have no specified alignment, and
866 we've printed the address; now print the size. */
867 bfd_fprintf_vma (abfd
, file
,
868 (bfd_is_com_section (symbol
->section
)
869 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
870 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
872 /* If we have version information, print it. */
873 if (elf_tdata (abfd
)->dynversym_section
!= 0
874 && (elf_tdata (abfd
)->dynverdef_section
!= 0
875 || elf_tdata (abfd
)->dynverref_section
!= 0))
878 const char *version_string
;
880 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
884 else if (vernum
== 1)
885 version_string
= "Base";
886 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
888 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
891 Elf_Internal_Verneed
*t
;
894 for (t
= elf_tdata (abfd
)->verref
;
898 Elf_Internal_Vernaux
*a
;
900 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
902 if (a
->vna_other
== vernum
)
904 version_string
= a
->vna_nodename
;
911 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
912 fprintf (file
, " %-11s", version_string
);
917 fprintf (file
, " (%s)", version_string
);
918 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
923 /* If the st_other field is not zero, print it. */
924 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
929 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
930 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
931 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
933 /* Some other non-defined flags are also present, so print
935 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
938 fprintf (file
, " %s", name
);
944 /* Create an entry in an ELF linker hash table. */
946 struct bfd_hash_entry
*
947 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
948 struct bfd_hash_entry
*entry
;
949 struct bfd_hash_table
*table
;
952 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
954 /* Allocate the structure if it has not already been allocated by a
956 if (ret
== (struct elf_link_hash_entry
*) NULL
)
957 ret
= ((struct elf_link_hash_entry
*)
958 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
959 if (ret
== (struct elf_link_hash_entry
*) NULL
)
960 return (struct bfd_hash_entry
*) ret
;
962 /* Call the allocation method of the superclass. */
963 ret
= ((struct elf_link_hash_entry
*)
964 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
966 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
968 /* Set local fields. */
972 ret
->dynstr_index
= 0;
974 ret
->got
.offset
= (bfd_vma
) -1;
975 ret
->plt
.offset
= (bfd_vma
) -1;
976 ret
->linker_section_pointer
= (elf_linker_section_pointers_t
*)0;
977 ret
->verinfo
.verdef
= NULL
;
978 ret
->vtable_entries_used
= NULL
;
979 ret
->vtable_entries_size
= 0;
980 ret
->vtable_parent
= NULL
;
981 ret
->type
= STT_NOTYPE
;
983 /* Assume that we have been called by a non-ELF symbol reader.
984 This flag is then reset by the code which reads an ELF input
985 file. This ensures that a symbol created by a non-ELF symbol
986 reader will have the flag set correctly. */
987 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
990 return (struct bfd_hash_entry
*) ret
;
993 /* Copy data from an indirect symbol to its direct symbol, hiding the
994 old indirect symbol. */
997 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
998 struct elf_link_hash_entry
*dir
, *ind
;
1000 /* Copy down any references that we may have already seen to the
1001 symbol which just became indirect. */
1003 dir
->elf_link_hash_flags
|=
1004 (ind
->elf_link_hash_flags
1005 & (ELF_LINK_HASH_REF_DYNAMIC
1006 | ELF_LINK_HASH_REF_REGULAR
1007 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1008 | ELF_LINK_NON_GOT_REF
));
1010 /* Copy over the global and procedure linkage table offset entries.
1011 These may have been already set up by a check_relocs routine. */
1012 if (dir
->got
.offset
== (bfd_vma
) -1)
1014 dir
->got
.offset
= ind
->got
.offset
;
1015 ind
->got
.offset
= (bfd_vma
) -1;
1017 BFD_ASSERT (ind
->got
.offset
== (bfd_vma
) -1);
1019 if (dir
->plt
.offset
== (bfd_vma
) -1)
1021 dir
->plt
.offset
= ind
->plt
.offset
;
1022 ind
->plt
.offset
= (bfd_vma
) -1;
1024 BFD_ASSERT (ind
->plt
.offset
== (bfd_vma
) -1);
1026 if (dir
->dynindx
== -1)
1028 dir
->dynindx
= ind
->dynindx
;
1029 dir
->dynstr_index
= ind
->dynstr_index
;
1031 ind
->dynstr_index
= 0;
1033 BFD_ASSERT (ind
->dynindx
== -1);
1037 _bfd_elf_link_hash_hide_symbol (info
, h
)
1038 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1039 struct elf_link_hash_entry
*h
;
1041 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1042 h
->plt
.offset
= (bfd_vma
) -1;
1043 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1047 /* Initialize an ELF linker hash table. */
1050 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1051 struct elf_link_hash_table
*table
;
1053 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1054 struct bfd_hash_table
*,
1059 table
->dynamic_sections_created
= false;
1060 table
->dynobj
= NULL
;
1061 /* The first dynamic symbol is a dummy. */
1062 table
->dynsymcount
= 1;
1063 table
->dynstr
= NULL
;
1064 table
->bucketcount
= 0;
1065 table
->needed
= NULL
;
1066 table
->runpath
= NULL
;
1068 table
->stab_info
= NULL
;
1069 table
->merge_info
= NULL
;
1070 table
->dynlocal
= NULL
;
1071 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1072 table
->root
.type
= bfd_link_elf_hash_table
;
1077 /* Create an ELF linker hash table. */
1079 struct bfd_link_hash_table
*
1080 _bfd_elf_link_hash_table_create (abfd
)
1083 struct elf_link_hash_table
*ret
;
1085 ret
= ((struct elf_link_hash_table
*)
1086 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
1087 if (ret
== (struct elf_link_hash_table
*) NULL
)
1090 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1092 bfd_release (abfd
, ret
);
1099 /* This is a hook for the ELF emulation code in the generic linker to
1100 tell the backend linker what file name to use for the DT_NEEDED
1101 entry for a dynamic object. The generic linker passes name as an
1102 empty string to indicate that no DT_NEEDED entry should be made. */
1105 bfd_elf_set_dt_needed_name (abfd
, name
)
1109 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1110 && bfd_get_format (abfd
) == bfd_object
)
1111 elf_dt_name (abfd
) = name
;
1115 bfd_elf_set_dt_needed_soname (abfd
, name
)
1119 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1120 && bfd_get_format (abfd
) == bfd_object
)
1121 elf_dt_soname (abfd
) = name
;
1124 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1125 the linker ELF emulation code. */
1127 struct bfd_link_needed_list
*
1128 bfd_elf_get_needed_list (abfd
, info
)
1129 bfd
*abfd ATTRIBUTE_UNUSED
;
1130 struct bfd_link_info
*info
;
1132 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1134 return elf_hash_table (info
)->needed
;
1137 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1138 hook for the linker ELF emulation code. */
1140 struct bfd_link_needed_list
*
1141 bfd_elf_get_runpath_list (abfd
, info
)
1142 bfd
*abfd ATTRIBUTE_UNUSED
;
1143 struct bfd_link_info
*info
;
1145 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1147 return elf_hash_table (info
)->runpath
;
1150 /* Get the name actually used for a dynamic object for a link. This
1151 is the SONAME entry if there is one. Otherwise, it is the string
1152 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1155 bfd_elf_get_dt_soname (abfd
)
1158 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1159 && bfd_get_format (abfd
) == bfd_object
)
1160 return elf_dt_name (abfd
);
1164 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1165 the ELF linker emulation code. */
1168 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1170 struct bfd_link_needed_list
**pneeded
;
1173 bfd_byte
*dynbuf
= NULL
;
1176 bfd_byte
*extdyn
, *extdynend
;
1178 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1182 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1183 || bfd_get_format (abfd
) != bfd_object
)
1186 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1187 if (s
== NULL
|| s
->_raw_size
== 0)
1190 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1194 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1198 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1202 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1204 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1205 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1208 extdynend
= extdyn
+ s
->_raw_size
;
1209 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1211 Elf_Internal_Dyn dyn
;
1213 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1215 if (dyn
.d_tag
== DT_NULL
)
1218 if (dyn
.d_tag
== DT_NEEDED
)
1221 struct bfd_link_needed_list
*l
;
1223 string
= bfd_elf_string_from_elf_section (abfd
, link
,
1228 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, sizeof *l
);
1249 /* Allocate an ELF string table--force the first byte to be zero. */
1251 struct bfd_strtab_hash
*
1252 _bfd_elf_stringtab_init ()
1254 struct bfd_strtab_hash
*ret
;
1256 ret
= _bfd_stringtab_init ();
1261 loc
= _bfd_stringtab_add (ret
, "", true, false);
1262 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1263 if (loc
== (bfd_size_type
) -1)
1265 _bfd_stringtab_free (ret
);
1272 /* ELF .o/exec file reading */
1274 /* Create a new bfd section from an ELF section header. */
1277 bfd_section_from_shdr (abfd
, shindex
)
1279 unsigned int shindex
;
1281 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1282 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1283 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1286 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1288 switch (hdr
->sh_type
)
1291 /* Inactive section. Throw it away. */
1294 case SHT_PROGBITS
: /* Normal section with contents. */
1295 case SHT_DYNAMIC
: /* Dynamic linking information. */
1296 case SHT_NOBITS
: /* .bss section. */
1297 case SHT_HASH
: /* .hash section. */
1298 case SHT_NOTE
: /* .note section. */
1299 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1301 case SHT_SYMTAB
: /* A symbol table */
1302 if (elf_onesymtab (abfd
) == shindex
)
1305 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1306 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1307 elf_onesymtab (abfd
) = shindex
;
1308 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1309 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1310 abfd
->flags
|= HAS_SYMS
;
1312 /* Sometimes a shared object will map in the symbol table. If
1313 SHF_ALLOC is set, and this is a shared object, then we also
1314 treat this section as a BFD section. We can not base the
1315 decision purely on SHF_ALLOC, because that flag is sometimes
1316 set in a relocateable object file, which would confuse the
1318 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1319 && (abfd
->flags
& DYNAMIC
) != 0
1320 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1325 case SHT_DYNSYM
: /* A dynamic symbol table */
1326 if (elf_dynsymtab (abfd
) == shindex
)
1329 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1330 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1331 elf_dynsymtab (abfd
) = shindex
;
1332 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1333 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1334 abfd
->flags
|= HAS_SYMS
;
1336 /* Besides being a symbol table, we also treat this as a regular
1337 section, so that objcopy can handle it. */
1338 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1340 case SHT_STRTAB
: /* A string table */
1341 if (hdr
->bfd_section
!= NULL
)
1343 if (ehdr
->e_shstrndx
== shindex
)
1345 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1346 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1352 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1354 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1355 if (hdr2
->sh_link
== shindex
)
1357 if (! bfd_section_from_shdr (abfd
, i
))
1359 if (elf_onesymtab (abfd
) == i
)
1361 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1362 elf_elfsections (abfd
)[shindex
] =
1363 &elf_tdata (abfd
)->strtab_hdr
;
1366 if (elf_dynsymtab (abfd
) == i
)
1368 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1369 elf_elfsections (abfd
)[shindex
] = hdr
=
1370 &elf_tdata (abfd
)->dynstrtab_hdr
;
1371 /* We also treat this as a regular section, so
1372 that objcopy can handle it. */
1375 #if 0 /* Not handling other string tables specially right now. */
1376 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1377 /* We have a strtab for some random other section. */
1378 newsect
= (asection
*) hdr2
->bfd_section
;
1381 hdr
->bfd_section
= newsect
;
1382 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1384 elf_elfsections (abfd
)[shindex
] = hdr2
;
1390 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1394 /* *These* do a lot of work -- but build no sections! */
1396 asection
*target_sect
;
1397 Elf_Internal_Shdr
*hdr2
;
1399 /* Check for a bogus link to avoid crashing. */
1400 if (hdr
->sh_link
>= ehdr
->e_shnum
)
1402 ((*_bfd_error_handler
)
1403 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1404 bfd_get_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1405 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1408 /* For some incomprehensible reason Oracle distributes
1409 libraries for Solaris in which some of the objects have
1410 bogus sh_link fields. It would be nice if we could just
1411 reject them, but, unfortunately, some people need to use
1412 them. We scan through the section headers; if we find only
1413 one suitable symbol table, we clobber the sh_link to point
1414 to it. I hope this doesn't break anything. */
1415 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1416 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1422 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1424 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1425 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1436 hdr
->sh_link
= found
;
1439 /* Get the symbol table. */
1440 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1441 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1444 /* If this reloc section does not use the main symbol table we
1445 don't treat it as a reloc section. BFD can't adequately
1446 represent such a section, so at least for now, we don't
1447 try. We just present it as a normal section. We also
1448 can't use it as a reloc section if it points to the null
1450 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1451 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1453 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1455 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1456 if (target_sect
== NULL
)
1459 if ((target_sect
->flags
& SEC_RELOC
) == 0
1460 || target_sect
->reloc_count
== 0)
1461 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1464 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1465 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
1466 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1469 elf_elfsections (abfd
)[shindex
] = hdr2
;
1470 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1471 target_sect
->flags
|= SEC_RELOC
;
1472 target_sect
->relocation
= NULL
;
1473 target_sect
->rel_filepos
= hdr
->sh_offset
;
1474 /* In the section to which the relocations apply, mark whether
1475 its relocations are of the REL or RELA variety. */
1476 if (hdr
->sh_size
!= 0)
1477 elf_section_data (target_sect
)->use_rela_p
1478 = (hdr
->sh_type
== SHT_RELA
);
1479 abfd
->flags
|= HAS_RELOC
;
1484 case SHT_GNU_verdef
:
1485 elf_dynverdef (abfd
) = shindex
;
1486 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1487 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1490 case SHT_GNU_versym
:
1491 elf_dynversym (abfd
) = shindex
;
1492 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1493 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1496 case SHT_GNU_verneed
:
1497 elf_dynverref (abfd
) = shindex
;
1498 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1499 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1506 /* Check for any processor-specific section types. */
1508 if (bed
->elf_backend_section_from_shdr
)
1509 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1517 /* Given an ELF section number, retrieve the corresponding BFD
1521 bfd_section_from_elf_index (abfd
, index
)
1525 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1526 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1528 return elf_elfsections (abfd
)[index
]->bfd_section
;
1532 _bfd_elf_new_section_hook (abfd
, sec
)
1536 struct bfd_elf_section_data
*sdata
;
1538 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, sizeof (*sdata
));
1541 sec
->used_by_bfd
= (PTR
) sdata
;
1543 /* Indicate whether or not this section should use RELA relocations. */
1545 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1550 /* Create a new bfd section from an ELF program header.
1552 Since program segments have no names, we generate a synthetic name
1553 of the form segment<NUM>, where NUM is generally the index in the
1554 program header table. For segments that are split (see below) we
1555 generate the names segment<NUM>a and segment<NUM>b.
1557 Note that some program segments may have a file size that is different than
1558 (less than) the memory size. All this means is that at execution the
1559 system must allocate the amount of memory specified by the memory size,
1560 but only initialize it with the first "file size" bytes read from the
1561 file. This would occur for example, with program segments consisting
1562 of combined data+bss.
1564 To handle the above situation, this routine generates TWO bfd sections
1565 for the single program segment. The first has the length specified by
1566 the file size of the segment, and the second has the length specified
1567 by the difference between the two sizes. In effect, the segment is split
1568 into it's initialized and uninitialized parts.
1573 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1575 Elf_Internal_Phdr
*hdr
;
1577 const char *typename
;
1584 split
= ((hdr
->p_memsz
> 0)
1585 && (hdr
->p_filesz
> 0)
1586 && (hdr
->p_memsz
> hdr
->p_filesz
));
1587 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1588 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1591 strcpy (name
, namebuf
);
1592 newsect
= bfd_make_section (abfd
, name
);
1593 if (newsect
== NULL
)
1595 newsect
->vma
= hdr
->p_vaddr
;
1596 newsect
->lma
= hdr
->p_paddr
;
1597 newsect
->_raw_size
= hdr
->p_filesz
;
1598 newsect
->filepos
= hdr
->p_offset
;
1599 newsect
->flags
|= SEC_HAS_CONTENTS
;
1600 if (hdr
->p_type
== PT_LOAD
)
1602 newsect
->flags
|= SEC_ALLOC
;
1603 newsect
->flags
|= SEC_LOAD
;
1604 if (hdr
->p_flags
& PF_X
)
1606 /* FIXME: all we known is that it has execute PERMISSION,
1608 newsect
->flags
|= SEC_CODE
;
1611 if (!(hdr
->p_flags
& PF_W
))
1613 newsect
->flags
|= SEC_READONLY
;
1618 sprintf (namebuf
, "%s%db", typename
, index
);
1619 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1622 strcpy (name
, namebuf
);
1623 newsect
= bfd_make_section (abfd
, name
);
1624 if (newsect
== NULL
)
1626 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1627 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1628 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1629 if (hdr
->p_type
== PT_LOAD
)
1631 newsect
->flags
|= SEC_ALLOC
;
1632 if (hdr
->p_flags
& PF_X
)
1633 newsect
->flags
|= SEC_CODE
;
1635 if (!(hdr
->p_flags
& PF_W
))
1636 newsect
->flags
|= SEC_READONLY
;
1643 bfd_section_from_phdr (abfd
, hdr
, index
)
1645 Elf_Internal_Phdr
*hdr
;
1648 struct elf_backend_data
*bed
;
1650 switch (hdr
->p_type
)
1653 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
1656 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
1659 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
1662 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
1665 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
1667 if (! elfcore_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
1672 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
1675 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
1678 /* Check for any processor-specific program segment types.
1679 If no handler for them, default to making "segment" sections. */
1680 bed
= get_elf_backend_data (abfd
);
1681 if (bed
->elf_backend_section_from_phdr
)
1682 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
1684 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
1688 /* Initialize REL_HDR, the section-header for new section, containing
1689 relocations against ASECT. If USE_RELA_P is true, we use RELA
1690 relocations; otherwise, we use REL relocations. */
1693 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
1695 Elf_Internal_Shdr
*rel_hdr
;
1700 struct elf_backend_data
*bed
;
1702 bed
= get_elf_backend_data (abfd
);
1703 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1706 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1708 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1710 if (rel_hdr
->sh_name
== (unsigned int) -1)
1712 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1713 rel_hdr
->sh_entsize
= (use_rela_p
1714 ? bed
->s
->sizeof_rela
1715 : bed
->s
->sizeof_rel
);
1716 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
1717 rel_hdr
->sh_flags
= 0;
1718 rel_hdr
->sh_addr
= 0;
1719 rel_hdr
->sh_size
= 0;
1720 rel_hdr
->sh_offset
= 0;
1725 /* Set up an ELF internal section header for a section. */
1728 elf_fake_sections (abfd
, asect
, failedptrarg
)
1733 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1734 boolean
*failedptr
= (boolean
*) failedptrarg
;
1735 Elf_Internal_Shdr
*this_hdr
;
1739 /* We already failed; just get out of the bfd_map_over_sections
1744 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1746 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1749 if (this_hdr
->sh_name
== (unsigned long) -1)
1755 this_hdr
->sh_flags
= 0;
1757 if ((asect
->flags
& SEC_ALLOC
) != 0
1758 || asect
->user_set_vma
)
1759 this_hdr
->sh_addr
= asect
->vma
;
1761 this_hdr
->sh_addr
= 0;
1763 this_hdr
->sh_offset
= 0;
1764 this_hdr
->sh_size
= asect
->_raw_size
;
1765 this_hdr
->sh_link
= 0;
1766 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1767 /* The sh_entsize and sh_info fields may have been set already by
1768 copy_private_section_data. */
1770 this_hdr
->bfd_section
= asect
;
1771 this_hdr
->contents
= NULL
;
1773 /* FIXME: This should not be based on section names. */
1774 if (strcmp (asect
->name
, ".dynstr") == 0)
1775 this_hdr
->sh_type
= SHT_STRTAB
;
1776 else if (strcmp (asect
->name
, ".hash") == 0)
1778 this_hdr
->sh_type
= SHT_HASH
;
1779 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
1781 else if (strcmp (asect
->name
, ".dynsym") == 0)
1783 this_hdr
->sh_type
= SHT_DYNSYM
;
1784 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1786 else if (strcmp (asect
->name
, ".dynamic") == 0)
1788 this_hdr
->sh_type
= SHT_DYNAMIC
;
1789 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1791 else if (strncmp (asect
->name
, ".rela", 5) == 0
1792 && get_elf_backend_data (abfd
)->may_use_rela_p
)
1794 this_hdr
->sh_type
= SHT_RELA
;
1795 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1797 else if (strncmp (asect
->name
, ".rel", 4) == 0
1798 && get_elf_backend_data (abfd
)->may_use_rel_p
)
1800 this_hdr
->sh_type
= SHT_REL
;
1801 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1803 else if (strncmp (asect
->name
, ".note", 5) == 0)
1804 this_hdr
->sh_type
= SHT_NOTE
;
1805 else if (strncmp (asect
->name
, ".stab", 5) == 0
1806 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1807 this_hdr
->sh_type
= SHT_STRTAB
;
1808 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1810 this_hdr
->sh_type
= SHT_GNU_versym
;
1811 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1813 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1815 this_hdr
->sh_type
= SHT_GNU_verdef
;
1816 this_hdr
->sh_entsize
= 0;
1817 /* objcopy or strip will copy over sh_info, but may not set
1818 cverdefs. The linker will set cverdefs, but sh_info will be
1820 if (this_hdr
->sh_info
== 0)
1821 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1823 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1824 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1826 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1828 this_hdr
->sh_type
= SHT_GNU_verneed
;
1829 this_hdr
->sh_entsize
= 0;
1830 /* objcopy or strip will copy over sh_info, but may not set
1831 cverrefs. The linker will set cverrefs, but sh_info will be
1833 if (this_hdr
->sh_info
== 0)
1834 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1836 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1837 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1839 else if ((asect
->flags
& SEC_ALLOC
) != 0
1840 && ((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0))
1841 this_hdr
->sh_type
= SHT_NOBITS
;
1843 this_hdr
->sh_type
= SHT_PROGBITS
;
1845 if ((asect
->flags
& SEC_ALLOC
) != 0)
1846 this_hdr
->sh_flags
|= SHF_ALLOC
;
1847 if ((asect
->flags
& SEC_READONLY
) == 0)
1848 this_hdr
->sh_flags
|= SHF_WRITE
;
1849 if ((asect
->flags
& SEC_CODE
) != 0)
1850 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1851 if ((asect
->flags
& SEC_MERGE
) != 0)
1853 this_hdr
->sh_flags
|= SHF_MERGE
;
1854 this_hdr
->sh_entsize
= asect
->entsize
;
1855 if ((asect
->flags
& SEC_STRINGS
) != 0)
1856 this_hdr
->sh_flags
|= SHF_STRINGS
;
1859 /* Check for processor-specific section types. */
1860 if (bed
->elf_backend_fake_sections
)
1861 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1863 /* If the section has relocs, set up a section header for the
1864 SHT_REL[A] section. If two relocation sections are required for
1865 this section, it is up to the processor-specific back-end to
1866 create the other. */
1867 if ((asect
->flags
& SEC_RELOC
) != 0
1868 && !_bfd_elf_init_reloc_shdr (abfd
,
1869 &elf_section_data (asect
)->rel_hdr
,
1871 elf_section_data (asect
)->use_rela_p
))
1875 /* Assign all ELF section numbers. The dummy first section is handled here
1876 too. The link/info pointers for the standard section types are filled
1877 in here too, while we're at it. */
1880 assign_section_numbers (abfd
)
1883 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1885 unsigned int section_number
;
1886 Elf_Internal_Shdr
**i_shdrp
;
1890 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1892 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1894 d
->this_idx
= section_number
++;
1895 if ((sec
->flags
& SEC_RELOC
) == 0)
1898 d
->rel_idx
= section_number
++;
1901 d
->rel_idx2
= section_number
++;
1906 t
->shstrtab_section
= section_number
++;
1907 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1908 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1910 if (bfd_get_symcount (abfd
) > 0)
1912 t
->symtab_section
= section_number
++;
1913 t
->strtab_section
= section_number
++;
1916 elf_elfheader (abfd
)->e_shnum
= section_number
;
1918 /* Set up the list of section header pointers, in agreement with the
1920 i_shdrp
= ((Elf_Internal_Shdr
**)
1921 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1922 if (i_shdrp
== NULL
)
1925 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1926 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1927 if (i_shdrp
[0] == NULL
)
1929 bfd_release (abfd
, i_shdrp
);
1932 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1934 elf_elfsections (abfd
) = i_shdrp
;
1936 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1937 if (bfd_get_symcount (abfd
) > 0)
1939 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1940 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1941 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1943 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1945 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1949 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1950 if (d
->rel_idx
!= 0)
1951 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1952 if (d
->rel_idx2
!= 0)
1953 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
1955 /* Fill in the sh_link and sh_info fields while we're at it. */
1957 /* sh_link of a reloc section is the section index of the symbol
1958 table. sh_info is the section index of the section to which
1959 the relocation entries apply. */
1960 if (d
->rel_idx
!= 0)
1962 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1963 d
->rel_hdr
.sh_info
= d
->this_idx
;
1965 if (d
->rel_idx2
!= 0)
1967 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
1968 d
->rel_hdr2
->sh_info
= d
->this_idx
;
1971 switch (d
->this_hdr
.sh_type
)
1975 /* A reloc section which we are treating as a normal BFD
1976 section. sh_link is the section index of the symbol
1977 table. sh_info is the section index of the section to
1978 which the relocation entries apply. We assume that an
1979 allocated reloc section uses the dynamic symbol table.
1980 FIXME: How can we be sure? */
1981 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1983 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1985 /* We look up the section the relocs apply to by name. */
1987 if (d
->this_hdr
.sh_type
== SHT_REL
)
1991 s
= bfd_get_section_by_name (abfd
, name
);
1993 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1997 /* We assume that a section named .stab*str is a stabs
1998 string section. We look for a section with the same name
1999 but without the trailing ``str'', and set its sh_link
2000 field to point to this section. */
2001 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2002 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2007 len
= strlen (sec
->name
);
2008 alc
= (char *) bfd_malloc (len
- 2);
2011 strncpy (alc
, sec
->name
, len
- 3);
2012 alc
[len
- 3] = '\0';
2013 s
= bfd_get_section_by_name (abfd
, alc
);
2017 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2019 /* This is a .stab section. */
2020 elf_section_data (s
)->this_hdr
.sh_entsize
=
2021 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2028 case SHT_GNU_verneed
:
2029 case SHT_GNU_verdef
:
2030 /* sh_link is the section header index of the string table
2031 used for the dynamic entries, or the symbol table, or the
2033 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2035 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2039 case SHT_GNU_versym
:
2040 /* sh_link is the section header index of the symbol table
2041 this hash table or version table is for. */
2042 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2044 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2052 /* Map symbol from it's internal number to the external number, moving
2053 all local symbols to be at the head of the list. */
2056 sym_is_global (abfd
, sym
)
2060 /* If the backend has a special mapping, use it. */
2061 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2062 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2065 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2066 || bfd_is_und_section (bfd_get_section (sym
))
2067 || bfd_is_com_section (bfd_get_section (sym
)));
2071 elf_map_symbols (abfd
)
2074 int symcount
= bfd_get_symcount (abfd
);
2075 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2076 asymbol
**sect_syms
;
2078 int num_globals
= 0;
2079 int num_locals2
= 0;
2080 int num_globals2
= 0;
2082 int num_sections
= 0;
2089 fprintf (stderr
, "elf_map_symbols\n");
2093 /* Add a section symbol for each BFD section. FIXME: Is this really
2095 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2097 if (max_index
< asect
->index
)
2098 max_index
= asect
->index
;
2102 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
2103 if (sect_syms
== NULL
)
2105 elf_section_syms (abfd
) = sect_syms
;
2106 elf_num_section_syms (abfd
) = max_index
;
2108 for (idx
= 0; idx
< symcount
; idx
++)
2112 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2119 if (sec
->owner
!= NULL
)
2121 if (sec
->owner
!= abfd
)
2123 if (sec
->output_offset
!= 0)
2126 sec
= sec
->output_section
;
2128 /* Empty sections in the input files may have had a section
2129 symbol created for them. (See the comment near the end of
2130 _bfd_generic_link_output_symbols in linker.c). If the linker
2131 script discards such sections then we will reach this point.
2132 Since we know that we cannot avoid this case, we detect it
2133 and skip the abort and the assignment to the sect_syms array.
2134 To reproduce this particular case try running the linker
2135 testsuite test ld-scripts/weak.exp for an ELF port that uses
2136 the generic linker. */
2137 if (sec
->owner
== NULL
)
2140 BFD_ASSERT (sec
->owner
== abfd
);
2142 sect_syms
[sec
->index
] = syms
[idx
];
2147 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2149 if (sect_syms
[asect
->index
] != NULL
)
2152 sym
= bfd_make_empty_symbol (abfd
);
2155 sym
->the_bfd
= abfd
;
2156 sym
->name
= asect
->name
;
2158 /* Set the flags to 0 to indicate that this one was newly added. */
2160 sym
->section
= asect
;
2161 sect_syms
[asect
->index
] = sym
;
2165 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2166 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
2170 /* Classify all of the symbols. */
2171 for (idx
= 0; idx
< symcount
; idx
++)
2173 if (!sym_is_global (abfd
, syms
[idx
]))
2178 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2180 if (sect_syms
[asect
->index
] != NULL
2181 && sect_syms
[asect
->index
]->flags
== 0)
2183 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
2184 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
2188 sect_syms
[asect
->index
]->flags
= 0;
2192 /* Now sort the symbols so the local symbols are first. */
2193 new_syms
= ((asymbol
**)
2195 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
2196 if (new_syms
== NULL
)
2199 for (idx
= 0; idx
< symcount
; idx
++)
2201 asymbol
*sym
= syms
[idx
];
2204 if (!sym_is_global (abfd
, sym
))
2207 i
= num_locals
+ num_globals2
++;
2209 sym
->udata
.i
= i
+ 1;
2211 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2213 if (sect_syms
[asect
->index
] != NULL
2214 && sect_syms
[asect
->index
]->flags
== 0)
2216 asymbol
*sym
= sect_syms
[asect
->index
];
2219 sym
->flags
= BSF_SECTION_SYM
;
2220 if (!sym_is_global (abfd
, sym
))
2223 i
= num_locals
+ num_globals2
++;
2225 sym
->udata
.i
= i
+ 1;
2229 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2231 elf_num_locals (abfd
) = num_locals
;
2232 elf_num_globals (abfd
) = num_globals
;
2236 /* Align to the maximum file alignment that could be required for any
2237 ELF data structure. */
2239 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2240 static INLINE file_ptr
2241 align_file_position (off
, align
)
2245 return (off
+ align
- 1) & ~(align
- 1);
2248 /* Assign a file position to a section, optionally aligning to the
2249 required section alignment. */
2252 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2253 Elf_Internal_Shdr
*i_shdrp
;
2261 al
= i_shdrp
->sh_addralign
;
2263 offset
= BFD_ALIGN (offset
, al
);
2265 i_shdrp
->sh_offset
= offset
;
2266 if (i_shdrp
->bfd_section
!= NULL
)
2267 i_shdrp
->bfd_section
->filepos
= offset
;
2268 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2269 offset
+= i_shdrp
->sh_size
;
2273 /* Compute the file positions we are going to put the sections at, and
2274 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2275 is not NULL, this is being called by the ELF backend linker. */
2278 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2280 struct bfd_link_info
*link_info
;
2282 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2284 struct bfd_strtab_hash
*strtab
;
2285 Elf_Internal_Shdr
*shstrtab_hdr
;
2287 if (abfd
->output_has_begun
)
2290 /* Do any elf backend specific processing first. */
2291 if (bed
->elf_backend_begin_write_processing
)
2292 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2294 if (! prep_headers (abfd
))
2297 /* Post process the headers if necessary. */
2298 if (bed
->elf_backend_post_process_headers
)
2299 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2302 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2306 if (!assign_section_numbers (abfd
))
2309 /* The backend linker builds symbol table information itself. */
2310 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2312 /* Non-zero if doing a relocatable link. */
2313 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2315 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2319 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2320 /* sh_name was set in prep_headers. */
2321 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2322 shstrtab_hdr
->sh_flags
= 0;
2323 shstrtab_hdr
->sh_addr
= 0;
2324 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2325 shstrtab_hdr
->sh_entsize
= 0;
2326 shstrtab_hdr
->sh_link
= 0;
2327 shstrtab_hdr
->sh_info
= 0;
2328 /* sh_offset is set in assign_file_positions_except_relocs. */
2329 shstrtab_hdr
->sh_addralign
= 1;
2331 if (!assign_file_positions_except_relocs (abfd
))
2334 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2337 Elf_Internal_Shdr
*hdr
;
2339 off
= elf_tdata (abfd
)->next_file_pos
;
2341 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2342 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2344 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2345 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2347 elf_tdata (abfd
)->next_file_pos
= off
;
2349 /* Now that we know where the .strtab section goes, write it
2351 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2352 || ! _bfd_stringtab_emit (abfd
, strtab
))
2354 _bfd_stringtab_free (strtab
);
2357 abfd
->output_has_begun
= true;
2362 /* Create a mapping from a set of sections to a program segment. */
2364 static INLINE
struct elf_segment_map
*
2365 make_mapping (abfd
, sections
, from
, to
, phdr
)
2367 asection
**sections
;
2372 struct elf_segment_map
*m
;
2376 m
= ((struct elf_segment_map
*)
2378 (sizeof (struct elf_segment_map
)
2379 + (to
- from
- 1) * sizeof (asection
*))));
2383 m
->p_type
= PT_LOAD
;
2384 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2385 m
->sections
[i
- from
] = *hdrpp
;
2386 m
->count
= to
- from
;
2388 if (from
== 0 && phdr
)
2390 /* Include the headers in the first PT_LOAD segment. */
2391 m
->includes_filehdr
= 1;
2392 m
->includes_phdrs
= 1;
2398 /* Set up a mapping from BFD sections to program segments. */
2401 map_sections_to_segments (abfd
)
2404 asection
**sections
= NULL
;
2408 struct elf_segment_map
*mfirst
;
2409 struct elf_segment_map
**pm
;
2410 struct elf_segment_map
*m
;
2412 unsigned int phdr_index
;
2413 bfd_vma maxpagesize
;
2415 boolean phdr_in_segment
= true;
2419 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2422 if (bfd_count_sections (abfd
) == 0)
2425 /* Select the allocated sections, and sort them. */
2427 sections
= (asection
**) bfd_malloc (bfd_count_sections (abfd
)
2428 * sizeof (asection
*));
2429 if (sections
== NULL
)
2433 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2435 if ((s
->flags
& SEC_ALLOC
) != 0)
2441 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2444 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2446 /* Build the mapping. */
2451 /* If we have a .interp section, then create a PT_PHDR segment for
2452 the program headers and a PT_INTERP segment for the .interp
2454 s
= bfd_get_section_by_name (abfd
, ".interp");
2455 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2457 m
= ((struct elf_segment_map
*)
2458 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2462 m
->p_type
= PT_PHDR
;
2463 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2464 m
->p_flags
= PF_R
| PF_X
;
2465 m
->p_flags_valid
= 1;
2466 m
->includes_phdrs
= 1;
2471 m
= ((struct elf_segment_map
*)
2472 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2476 m
->p_type
= PT_INTERP
;
2484 /* Look through the sections. We put sections in the same program
2485 segment when the start of the second section can be placed within
2486 a few bytes of the end of the first section. */
2489 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2491 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2493 && (dynsec
->flags
& SEC_LOAD
) == 0)
2496 /* Deal with -Ttext or something similar such that the first section
2497 is not adjacent to the program headers. This is an
2498 approximation, since at this point we don't know exactly how many
2499 program headers we will need. */
2502 bfd_size_type phdr_size
;
2504 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2506 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2507 if ((abfd
->flags
& D_PAGED
) == 0
2508 || sections
[0]->lma
< phdr_size
2509 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2510 phdr_in_segment
= false;
2513 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2516 boolean new_segment
;
2520 /* See if this section and the last one will fit in the same
2523 if (last_hdr
== NULL
)
2525 /* If we don't have a segment yet, then we don't need a new
2526 one (we build the last one after this loop). */
2527 new_segment
= false;
2529 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2531 /* If this section has a different relation between the
2532 virtual address and the load address, then we need a new
2536 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2537 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2539 /* If putting this section in this segment would force us to
2540 skip a page in the segment, then we need a new segment. */
2543 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2544 && (hdr
->flags
& SEC_LOAD
) != 0)
2546 /* We don't want to put a loadable section after a
2547 nonloadable section in the same segment. */
2550 else if ((abfd
->flags
& D_PAGED
) == 0)
2552 /* If the file is not demand paged, which means that we
2553 don't require the sections to be correctly aligned in the
2554 file, then there is no other reason for a new segment. */
2555 new_segment
= false;
2558 && (hdr
->flags
& SEC_READONLY
) == 0
2559 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2562 /* We don't want to put a writable section in a read only
2563 segment, unless they are on the same page in memory
2564 anyhow. We already know that the last section does not
2565 bring us past the current section on the page, so the
2566 only case in which the new section is not on the same
2567 page as the previous section is when the previous section
2568 ends precisely on a page boundary. */
2573 /* Otherwise, we can use the same segment. */
2574 new_segment
= false;
2579 if ((hdr
->flags
& SEC_READONLY
) == 0)
2585 /* We need a new program segment. We must create a new program
2586 header holding all the sections from phdr_index until hdr. */
2588 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2595 if ((hdr
->flags
& SEC_READONLY
) == 0)
2602 phdr_in_segment
= false;
2605 /* Create a final PT_LOAD program segment. */
2606 if (last_hdr
!= NULL
)
2608 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2616 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2619 m
= ((struct elf_segment_map
*)
2620 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2624 m
->p_type
= PT_DYNAMIC
;
2626 m
->sections
[0] = dynsec
;
2632 /* For each loadable .note section, add a PT_NOTE segment. We don't
2633 use bfd_get_section_by_name, because if we link together
2634 nonloadable .note sections and loadable .note sections, we will
2635 generate two .note sections in the output file. FIXME: Using
2636 names for section types is bogus anyhow. */
2637 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2639 if ((s
->flags
& SEC_LOAD
) != 0
2640 && strncmp (s
->name
, ".note", 5) == 0)
2642 m
= ((struct elf_segment_map
*)
2643 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2647 m
->p_type
= PT_NOTE
;
2659 elf_tdata (abfd
)->segment_map
= mfirst
;
2663 if (sections
!= NULL
)
2668 /* Sort sections by address. */
2671 elf_sort_sections (arg1
, arg2
)
2675 const asection
*sec1
= *(const asection
**) arg1
;
2676 const asection
*sec2
= *(const asection
**) arg2
;
2678 /* Sort by LMA first, since this is the address used to
2679 place the section into a segment. */
2680 if (sec1
->lma
< sec2
->lma
)
2682 else if (sec1
->lma
> sec2
->lma
)
2685 /* Then sort by VMA. Normally the LMA and the VMA will be
2686 the same, and this will do nothing. */
2687 if (sec1
->vma
< sec2
->vma
)
2689 else if (sec1
->vma
> sec2
->vma
)
2692 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2694 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2700 /* If the indicies are the same, do not return 0
2701 here, but continue to try the next comparison. */
2702 if (sec1
->target_index
- sec2
->target_index
!= 0)
2703 return sec1
->target_index
- sec2
->target_index
;
2708 else if (TOEND (sec2
))
2713 /* Sort by size, to put zero sized sections
2714 before others at the same address. */
2716 if (sec1
->_raw_size
< sec2
->_raw_size
)
2718 if (sec1
->_raw_size
> sec2
->_raw_size
)
2721 return sec1
->target_index
- sec2
->target_index
;
2724 /* Assign file positions to the sections based on the mapping from
2725 sections to segments. This function also sets up some fields in
2726 the file header, and writes out the program headers. */
2729 assign_file_positions_for_segments (abfd
)
2732 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2734 struct elf_segment_map
*m
;
2736 Elf_Internal_Phdr
*phdrs
;
2738 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2739 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2740 Elf_Internal_Phdr
*p
;
2742 if (elf_tdata (abfd
)->segment_map
== NULL
)
2744 if (! map_sections_to_segments (abfd
))
2748 if (bed
->elf_backend_modify_segment_map
)
2750 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2755 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2758 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2759 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2760 elf_elfheader (abfd
)->e_phnum
= count
;
2765 /* If we already counted the number of program segments, make sure
2766 that we allocated enough space. This happens when SIZEOF_HEADERS
2767 is used in a linker script. */
2768 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2769 if (alloc
!= 0 && count
> alloc
)
2771 ((*_bfd_error_handler
)
2772 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2773 bfd_get_filename (abfd
), alloc
, count
));
2774 bfd_set_error (bfd_error_bad_value
);
2781 phdrs
= ((Elf_Internal_Phdr
*)
2782 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
2786 off
= bed
->s
->sizeof_ehdr
;
2787 off
+= alloc
* bed
->s
->sizeof_phdr
;
2794 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2801 /* If elf_segment_map is not from map_sections_to_segments, the
2802 sections may not be correctly ordered. */
2804 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2807 p
->p_type
= m
->p_type
;
2808 p
->p_flags
= m
->p_flags
;
2810 if (p
->p_type
== PT_LOAD
2812 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2814 if ((abfd
->flags
& D_PAGED
) != 0)
2815 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2818 bfd_size_type align
;
2821 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2823 bfd_size_type secalign
;
2825 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
2826 if (secalign
> align
)
2830 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
2837 p
->p_vaddr
= m
->sections
[0]->vma
;
2839 if (m
->p_paddr_valid
)
2840 p
->p_paddr
= m
->p_paddr
;
2841 else if (m
->count
== 0)
2844 p
->p_paddr
= m
->sections
[0]->lma
;
2846 if (p
->p_type
== PT_LOAD
2847 && (abfd
->flags
& D_PAGED
) != 0)
2848 p
->p_align
= bed
->maxpagesize
;
2849 else if (m
->count
== 0)
2850 p
->p_align
= bed
->s
->file_align
;
2858 if (m
->includes_filehdr
)
2860 if (! m
->p_flags_valid
)
2863 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2864 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2867 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2869 if (p
->p_vaddr
< (bfd_vma
) off
)
2871 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2872 bfd_get_filename (abfd
));
2873 bfd_set_error (bfd_error_bad_value
);
2878 if (! m
->p_paddr_valid
)
2881 if (p
->p_type
== PT_LOAD
)
2883 filehdr_vaddr
= p
->p_vaddr
;
2884 filehdr_paddr
= p
->p_paddr
;
2888 if (m
->includes_phdrs
)
2890 if (! m
->p_flags_valid
)
2893 if (m
->includes_filehdr
)
2895 if (p
->p_type
== PT_LOAD
)
2897 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2898 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2903 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2907 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2908 p
->p_vaddr
-= off
- p
->p_offset
;
2909 if (! m
->p_paddr_valid
)
2910 p
->p_paddr
-= off
- p
->p_offset
;
2913 if (p
->p_type
== PT_LOAD
)
2915 phdrs_vaddr
= p
->p_vaddr
;
2916 phdrs_paddr
= p
->p_paddr
;
2919 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
2922 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2923 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2926 if (p
->p_type
== PT_LOAD
2927 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
2929 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2935 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2936 p
->p_filesz
+= adjust
;
2937 p
->p_memsz
+= adjust
;
2943 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2947 bfd_size_type align
;
2951 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2953 /* The section may have artificial alignment forced by a
2954 link script. Notice this case by the gap between the
2955 cumulative phdr vma and the section's vma. */
2956 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2958 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2960 p
->p_memsz
+= adjust
;
2963 if ((flags
& SEC_LOAD
) != 0)
2964 p
->p_filesz
+= adjust
;
2967 if (p
->p_type
== PT_LOAD
)
2969 bfd_signed_vma adjust
;
2971 if ((flags
& SEC_LOAD
) != 0)
2973 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2977 else if ((flags
& SEC_ALLOC
) != 0)
2979 /* The section VMA must equal the file position
2980 modulo the page size. FIXME: I'm not sure if
2981 this adjustment is really necessary. We used to
2982 not have the SEC_LOAD case just above, and then
2983 this was necessary, but now I'm not sure. */
2984 if ((abfd
->flags
& D_PAGED
) != 0)
2985 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2987 adjust
= (sec
->vma
- voff
) % align
;
2996 (* _bfd_error_handler
)
2997 (_("Error: First section in segment (%s) starts at 0x%x"),
2998 bfd_section_name (abfd
, sec
), sec
->lma
);
2999 (* _bfd_error_handler
)
3000 (_(" whereas segment starts at 0x%x"),
3005 p
->p_memsz
+= adjust
;
3008 if ((flags
& SEC_LOAD
) != 0)
3009 p
->p_filesz
+= adjust
;
3014 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3015 used in a linker script we may have a section with
3016 SEC_LOAD clear but which is supposed to have
3018 if ((flags
& SEC_LOAD
) != 0
3019 || (flags
& SEC_HAS_CONTENTS
) != 0)
3020 off
+= sec
->_raw_size
;
3022 if ((flags
& SEC_ALLOC
) != 0)
3023 voff
+= sec
->_raw_size
;
3026 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3028 /* The actual "note" segment has i == 0.
3029 This is the one that actually contains everything. */
3033 p
->p_filesz
= sec
->_raw_size
;
3034 off
+= sec
->_raw_size
;
3039 /* Fake sections -- don't need to be written. */
3042 flags
= sec
->flags
= 0;
3049 p
->p_memsz
+= sec
->_raw_size
;
3051 if ((flags
& SEC_LOAD
) != 0)
3052 p
->p_filesz
+= sec
->_raw_size
;
3054 if (align
> p
->p_align
3055 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3059 if (! m
->p_flags_valid
)
3062 if ((flags
& SEC_CODE
) != 0)
3064 if ((flags
& SEC_READONLY
) == 0)
3070 /* Now that we have set the section file positions, we can set up
3071 the file positions for the non PT_LOAD segments. */
3072 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3076 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3078 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3079 p
->p_offset
= m
->sections
[0]->filepos
;
3083 if (m
->includes_filehdr
)
3085 p
->p_vaddr
= filehdr_vaddr
;
3086 if (! m
->p_paddr_valid
)
3087 p
->p_paddr
= filehdr_paddr
;
3089 else if (m
->includes_phdrs
)
3091 p
->p_vaddr
= phdrs_vaddr
;
3092 if (! m
->p_paddr_valid
)
3093 p
->p_paddr
= phdrs_paddr
;
3098 /* Clear out any program headers we allocated but did not use. */
3099 for (; count
< alloc
; count
++, p
++)
3101 memset (p
, 0, sizeof *p
);
3102 p
->p_type
= PT_NULL
;
3105 elf_tdata (abfd
)->phdr
= phdrs
;
3107 elf_tdata (abfd
)->next_file_pos
= off
;
3109 /* Write out the program headers. */
3110 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3111 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3117 /* Get the size of the program header.
3119 If this is called by the linker before any of the section VMA's are set, it
3120 can't calculate the correct value for a strange memory layout. This only
3121 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3122 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3123 data segment (exclusive of .interp and .dynamic).
3125 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3126 will be two segments. */
3128 static bfd_size_type
3129 get_program_header_size (abfd
)
3134 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3136 /* We can't return a different result each time we're called. */
3137 if (elf_tdata (abfd
)->program_header_size
!= 0)
3138 return elf_tdata (abfd
)->program_header_size
;
3140 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3142 struct elf_segment_map
*m
;
3145 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3147 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3148 return elf_tdata (abfd
)->program_header_size
;
3151 /* Assume we will need exactly two PT_LOAD segments: one for text
3152 and one for data. */
3155 s
= bfd_get_section_by_name (abfd
, ".interp");
3156 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3158 /* If we have a loadable interpreter section, we need a
3159 PT_INTERP segment. In this case, assume we also need a
3160 PT_PHDR segment, although that may not be true for all
3165 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3167 /* We need a PT_DYNAMIC segment. */
3171 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3173 if ((s
->flags
& SEC_LOAD
) != 0
3174 && strncmp (s
->name
, ".note", 5) == 0)
3176 /* We need a PT_NOTE segment. */
3181 /* Let the backend count up any program headers it might need. */
3182 if (bed
->elf_backend_additional_program_headers
)
3186 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3192 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3193 return elf_tdata (abfd
)->program_header_size
;
3196 /* Work out the file positions of all the sections. This is called by
3197 _bfd_elf_compute_section_file_positions. All the section sizes and
3198 VMAs must be known before this is called.
3200 We do not consider reloc sections at this point, unless they form
3201 part of the loadable image. Reloc sections are assigned file
3202 positions in assign_file_positions_for_relocs, which is called by
3203 write_object_contents and final_link.
3205 We also don't set the positions of the .symtab and .strtab here. */
3208 assign_file_positions_except_relocs (abfd
)
3211 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3212 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3213 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3215 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3217 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3218 && bfd_get_format (abfd
) != bfd_core
)
3220 Elf_Internal_Shdr
**hdrpp
;
3223 /* Start after the ELF header. */
3224 off
= i_ehdrp
->e_ehsize
;
3226 /* We are not creating an executable, which means that we are
3227 not creating a program header, and that the actual order of
3228 the sections in the file is unimportant. */
3229 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3231 Elf_Internal_Shdr
*hdr
;
3234 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
3236 hdr
->sh_offset
= -1;
3239 if (i
== tdata
->symtab_section
3240 || i
== tdata
->strtab_section
)
3242 hdr
->sh_offset
= -1;
3246 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3252 Elf_Internal_Shdr
**hdrpp
;
3254 /* Assign file positions for the loaded sections based on the
3255 assignment of sections to segments. */
3256 if (! assign_file_positions_for_segments (abfd
))
3259 /* Assign file positions for the other sections. */
3261 off
= elf_tdata (abfd
)->next_file_pos
;
3262 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3264 Elf_Internal_Shdr
*hdr
;
3267 if (hdr
->bfd_section
!= NULL
3268 && hdr
->bfd_section
->filepos
!= 0)
3269 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3270 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3272 ((*_bfd_error_handler
)
3273 (_("%s: warning: allocated section `%s' not in segment"),
3274 bfd_get_filename (abfd
),
3275 (hdr
->bfd_section
== NULL
3277 : hdr
->bfd_section
->name
)));
3278 if ((abfd
->flags
& D_PAGED
) != 0)
3279 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3281 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3282 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3285 else if (hdr
->sh_type
== SHT_REL
3286 || hdr
->sh_type
== SHT_RELA
3287 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3288 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3289 hdr
->sh_offset
= -1;
3291 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3295 /* Place the section headers. */
3296 off
= align_file_position (off
, bed
->s
->file_align
);
3297 i_ehdrp
->e_shoff
= off
;
3298 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3300 elf_tdata (abfd
)->next_file_pos
= off
;
3309 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3310 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3311 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3313 struct bfd_strtab_hash
*shstrtab
;
3314 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3316 i_ehdrp
= elf_elfheader (abfd
);
3317 i_shdrp
= elf_elfsections (abfd
);
3319 shstrtab
= _bfd_elf_stringtab_init ();
3320 if (shstrtab
== NULL
)
3323 elf_shstrtab (abfd
) = shstrtab
;
3325 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3326 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3327 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3328 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3330 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3331 i_ehdrp
->e_ident
[EI_DATA
] =
3332 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3333 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3335 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3336 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3338 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3339 i_ehdrp
->e_ident
[count
] = 0;
3341 if ((abfd
->flags
& DYNAMIC
) != 0)
3342 i_ehdrp
->e_type
= ET_DYN
;
3343 else if ((abfd
->flags
& EXEC_P
) != 0)
3344 i_ehdrp
->e_type
= ET_EXEC
;
3345 else if (bfd_get_format (abfd
) == bfd_core
)
3346 i_ehdrp
->e_type
= ET_CORE
;
3348 i_ehdrp
->e_type
= ET_REL
;
3350 switch (bfd_get_arch (abfd
))
3352 case bfd_arch_unknown
:
3353 i_ehdrp
->e_machine
= EM_NONE
;
3355 case bfd_arch_sparc
:
3356 if (bfd_get_arch_size (abfd
) == 64)
3357 i_ehdrp
->e_machine
= EM_SPARCV9
;
3359 i_ehdrp
->e_machine
= EM_SPARC
;
3362 i_ehdrp
->e_machine
= EM_S370
;
3365 if (bfd_get_arch_size (abfd
) == 64)
3366 i_ehdrp
->e_machine
= EM_X86_64
;
3368 i_ehdrp
->e_machine
= EM_386
;
3371 i_ehdrp
->e_machine
= EM_IA_64
;
3373 case bfd_arch_m68hc11
:
3374 i_ehdrp
->e_machine
= EM_68HC11
;
3376 case bfd_arch_m68hc12
:
3377 i_ehdrp
->e_machine
= EM_68HC12
;
3380 i_ehdrp
->e_machine
= EM_S390
;
3383 i_ehdrp
->e_machine
= EM_68K
;
3386 i_ehdrp
->e_machine
= EM_88K
;
3389 i_ehdrp
->e_machine
= EM_860
;
3392 i_ehdrp
->e_machine
= EM_960
;
3394 case bfd_arch_mips
: /* MIPS Rxxxx */
3395 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
3398 i_ehdrp
->e_machine
= EM_PARISC
;
3400 case bfd_arch_powerpc
:
3401 i_ehdrp
->e_machine
= EM_PPC
;
3403 case bfd_arch_alpha
:
3404 i_ehdrp
->e_machine
= EM_ALPHA
;
3407 i_ehdrp
->e_machine
= EM_SH
;
3410 i_ehdrp
->e_machine
= EM_CYGNUS_D10V
;
3413 i_ehdrp
->e_machine
= EM_CYGNUS_D30V
;
3416 i_ehdrp
->e_machine
= EM_CYGNUS_FR30
;
3418 case bfd_arch_mcore
:
3419 i_ehdrp
->e_machine
= EM_MCORE
;
3422 i_ehdrp
->e_machine
= EM_AVR
;
3425 switch (bfd_get_mach (abfd
))
3428 case 0: i_ehdrp
->e_machine
= EM_CYGNUS_V850
; break;
3432 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
3435 i_ehdrp
->e_machine
= EM_ARM
;
3438 i_ehdrp
->e_machine
= EM_CYGNUS_M32R
;
3440 case bfd_arch_mn10200
:
3441 i_ehdrp
->e_machine
= EM_CYGNUS_MN10200
;
3443 case bfd_arch_mn10300
:
3444 i_ehdrp
->e_machine
= EM_CYGNUS_MN10300
;
3447 i_ehdrp
->e_machine
= EM_PJ
;
3450 i_ehdrp
->e_machine
= EM_CRIS
;
3452 case bfd_arch_openrisc
:
3453 i_ehdrp
->e_machine
= EM_OPENRISC
;
3455 /* Also note that EM_M32, AT&T WE32100 is unknown to bfd. */
3457 i_ehdrp
->e_machine
= EM_NONE
;
3459 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3460 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3462 /* No program header, for now. */
3463 i_ehdrp
->e_phoff
= 0;
3464 i_ehdrp
->e_phentsize
= 0;
3465 i_ehdrp
->e_phnum
= 0;
3467 /* Each bfd section is section header entry. */
3468 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3469 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3471 /* If we're building an executable, we'll need a program header table. */
3472 if (abfd
->flags
& EXEC_P
)
3474 /* It all happens later. */
3476 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3478 /* elf_build_phdrs() returns a (NULL-terminated) array of
3479 Elf_Internal_Phdrs. */
3480 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3481 i_ehdrp
->e_phoff
= outbase
;
3482 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3487 i_ehdrp
->e_phentsize
= 0;
3489 i_ehdrp
->e_phoff
= 0;
3492 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3493 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3494 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3495 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3496 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3497 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3498 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3499 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3500 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3506 /* Assign file positions for all the reloc sections which are not part
3507 of the loadable file image. */
3510 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3515 Elf_Internal_Shdr
**shdrpp
;
3517 off
= elf_tdata (abfd
)->next_file_pos
;
3519 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3520 i
< elf_elfheader (abfd
)->e_shnum
;
3523 Elf_Internal_Shdr
*shdrp
;
3526 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3527 && shdrp
->sh_offset
== -1)
3528 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3531 elf_tdata (abfd
)->next_file_pos
= off
;
3535 _bfd_elf_write_object_contents (abfd
)
3538 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3539 Elf_Internal_Ehdr
*i_ehdrp
;
3540 Elf_Internal_Shdr
**i_shdrp
;
3544 if (! abfd
->output_has_begun
3545 && ! _bfd_elf_compute_section_file_positions
3546 (abfd
, (struct bfd_link_info
*) NULL
))
3549 i_shdrp
= elf_elfsections (abfd
);
3550 i_ehdrp
= elf_elfheader (abfd
);
3553 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3557 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3559 /* After writing the headers, we need to write the sections too... */
3560 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3562 if (bed
->elf_backend_section_processing
)
3563 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3564 if (i_shdrp
[count
]->contents
)
3566 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3567 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
3569 != i_shdrp
[count
]->sh_size
))
3574 /* Write out the section header names. */
3575 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3576 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3579 if (bed
->elf_backend_final_write_processing
)
3580 (*bed
->elf_backend_final_write_processing
) (abfd
,
3581 elf_tdata (abfd
)->linker
);
3583 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3587 _bfd_elf_write_corefile_contents (abfd
)
3590 /* Hopefully this can be done just like an object file. */
3591 return _bfd_elf_write_object_contents (abfd
);
3594 /* Given a section, search the header to find them. */
3597 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3601 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3602 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3604 Elf_Internal_Shdr
*hdr
;
3605 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3607 for (index
= 0; index
< maxindex
; index
++)
3609 hdr
= i_shdrp
[index
];
3610 if (hdr
->bfd_section
== asect
)
3614 if (bed
->elf_backend_section_from_bfd_section
)
3616 for (index
= 0; index
< maxindex
; index
++)
3620 hdr
= i_shdrp
[index
];
3622 if ((*bed
->elf_backend_section_from_bfd_section
)
3623 (abfd
, hdr
, asect
, &retval
))
3628 if (bfd_is_abs_section (asect
))
3630 if (bfd_is_com_section (asect
))
3632 if (bfd_is_und_section (asect
))
3635 bfd_set_error (bfd_error_nonrepresentable_section
);
3640 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3644 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3646 asymbol
**asym_ptr_ptr
;
3648 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3650 flagword flags
= asym_ptr
->flags
;
3652 /* When gas creates relocations against local labels, it creates its
3653 own symbol for the section, but does put the symbol into the
3654 symbol chain, so udata is 0. When the linker is generating
3655 relocatable output, this section symbol may be for one of the
3656 input sections rather than the output section. */
3657 if (asym_ptr
->udata
.i
== 0
3658 && (flags
& BSF_SECTION_SYM
)
3659 && asym_ptr
->section
)
3663 if (asym_ptr
->section
->output_section
!= NULL
)
3664 indx
= asym_ptr
->section
->output_section
->index
;
3666 indx
= asym_ptr
->section
->index
;
3667 if (indx
< elf_num_section_syms (abfd
)
3668 && elf_section_syms (abfd
)[indx
] != NULL
)
3669 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3672 idx
= asym_ptr
->udata
.i
;
3676 /* This case can occur when using --strip-symbol on a symbol
3677 which is used in a relocation entry. */
3678 (*_bfd_error_handler
)
3679 (_("%s: symbol `%s' required but not present"),
3680 bfd_get_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3681 bfd_set_error (bfd_error_no_symbols
);
3688 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3689 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3690 elf_symbol_flags (flags
));
3698 /* Copy private BFD data. This copies any program header information. */
3701 copy_private_bfd_data (ibfd
, obfd
)
3705 Elf_Internal_Ehdr
* iehdr
;
3706 struct elf_segment_map
* map
;
3707 struct elf_segment_map
* map_first
;
3708 struct elf_segment_map
** pointer_to_map
;
3709 Elf_Internal_Phdr
* segment
;
3712 unsigned int num_segments
;
3713 boolean phdr_included
= false;
3714 bfd_vma maxpagesize
;
3715 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
3716 unsigned int phdr_adjust_num
= 0;
3718 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3719 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3722 if (elf_tdata (ibfd
)->phdr
== NULL
)
3725 iehdr
= elf_elfheader (ibfd
);
3728 pointer_to_map
= &map_first
;
3730 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3731 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3733 /* Returns the end address of the segment + 1. */
3734 #define SEGMENT_END(segment, start) \
3735 (start + (segment->p_memsz > segment->p_filesz \
3736 ? segment->p_memsz : segment->p_filesz))
3738 /* Returns true if the given section is contained within
3739 the given segment. VMA addresses are compared. */
3740 #define IS_CONTAINED_BY_VMA(section, segment) \
3741 (section->vma >= segment->p_vaddr \
3742 && (section->vma + section->_raw_size) \
3743 <= (SEGMENT_END (segment, segment->p_vaddr)))
3745 /* Returns true if the given section is contained within
3746 the given segment. LMA addresses are compared. */
3747 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3748 (section->lma >= base \
3749 && (section->lma + section->_raw_size) \
3750 <= SEGMENT_END (segment, base))
3752 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3753 #define IS_COREFILE_NOTE(p, s) \
3754 (p->p_type == PT_NOTE \
3755 && bfd_get_format (ibfd) == bfd_core \
3756 && s->vma == 0 && s->lma == 0 \
3757 && (bfd_vma) s->filepos >= p->p_offset \
3758 && (bfd_vma) s->filepos + s->_raw_size \
3759 <= p->p_offset + p->p_filesz)
3761 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3762 linker, which generates a PT_INTERP section with p_vaddr and
3763 p_memsz set to 0. */
3764 #define IS_SOLARIS_PT_INTERP(p, s) \
3766 && p->p_filesz > 0 \
3767 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3768 && s->_raw_size > 0 \
3769 && (bfd_vma) s->filepos >= p->p_offset \
3770 && ((bfd_vma) s->filepos + s->_raw_size \
3771 <= p->p_offset + p->p_filesz))
3773 /* Decide if the given section should be included in the given segment.
3774 A section will be included if:
3775 1. It is within the address space of the segment,
3776 2. It is an allocated segment,
3777 3. There is an output section associated with it,
3778 4. The section has not already been allocated to a previous segment. */
3779 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3780 ((((IS_CONTAINED_BY_VMA (section, segment) \
3781 || IS_SOLARIS_PT_INTERP (segment, section)) \
3782 && (section->flags & SEC_ALLOC) != 0) \
3783 || IS_COREFILE_NOTE (segment, section)) \
3784 && section->output_section != NULL \
3785 && section->segment_mark == false)
3787 /* Returns true iff seg1 starts after the end of seg2. */
3788 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3789 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3791 /* Returns true iff seg1 and seg2 overlap. */
3792 #define SEGMENT_OVERLAPS(seg1, seg2) \
3793 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3795 /* Initialise the segment mark field. */
3796 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3797 section
->segment_mark
= false;
3799 /* Scan through the segments specified in the program header
3800 of the input BFD. For this first scan we look for overlaps
3801 in the loadable segments. These can be created by wierd
3802 parameters to objcopy. */
3803 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3808 Elf_Internal_Phdr
*segment2
;
3810 if (segment
->p_type
!= PT_LOAD
)
3813 /* Determine if this segment overlaps any previous segments. */
3814 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
3816 bfd_signed_vma extra_length
;
3818 if (segment2
->p_type
!= PT_LOAD
3819 || ! SEGMENT_OVERLAPS (segment
, segment2
))
3822 /* Merge the two segments together. */
3823 if (segment2
->p_vaddr
< segment
->p_vaddr
)
3825 /* Extend SEGMENT2 to include SEGMENT and then delete
3828 SEGMENT_END (segment
, segment
->p_vaddr
)
3829 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
3831 if (extra_length
> 0)
3833 segment2
->p_memsz
+= extra_length
;
3834 segment2
->p_filesz
+= extra_length
;
3837 segment
->p_type
= PT_NULL
;
3839 /* Since we have deleted P we must restart the outer loop. */
3841 segment
= elf_tdata (ibfd
)->phdr
;
3846 /* Extend SEGMENT to include SEGMENT2 and then delete
3849 SEGMENT_END (segment2
, segment2
->p_vaddr
)
3850 - SEGMENT_END (segment
, segment
->p_vaddr
);
3852 if (extra_length
> 0)
3854 segment
->p_memsz
+= extra_length
;
3855 segment
->p_filesz
+= extra_length
;
3858 segment2
->p_type
= PT_NULL
;
3863 /* The second scan attempts to assign sections to segments. */
3864 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3868 unsigned int section_count
;
3869 asection
** sections
;
3870 asection
* output_section
;
3872 bfd_vma matching_lma
;
3873 bfd_vma suggested_lma
;
3876 if (segment
->p_type
== PT_NULL
)
3879 /* Compute how many sections might be placed into this segment. */
3881 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3882 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3885 /* Allocate a segment map big enough to contain all of the
3886 sections we have selected. */
3887 map
= ((struct elf_segment_map
*)
3889 (sizeof (struct elf_segment_map
)
3890 + ((size_t) section_count
- 1) * sizeof (asection
*))));
3894 /* Initialise the fields of the segment map. Default to
3895 using the physical address of the segment in the input BFD. */
3897 map
->p_type
= segment
->p_type
;
3898 map
->p_flags
= segment
->p_flags
;
3899 map
->p_flags_valid
= 1;
3900 map
->p_paddr
= segment
->p_paddr
;
3901 map
->p_paddr_valid
= 1;
3903 /* Determine if this segment contains the ELF file header
3904 and if it contains the program headers themselves. */
3905 map
->includes_filehdr
= (segment
->p_offset
== 0
3906 && segment
->p_filesz
>= iehdr
->e_ehsize
);
3908 map
->includes_phdrs
= 0;
3910 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
3912 map
->includes_phdrs
=
3913 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3914 && (segment
->p_offset
+ segment
->p_filesz
3915 >= ((bfd_vma
) iehdr
->e_phoff
3916 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3918 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
3919 phdr_included
= true;
3922 if (section_count
== 0)
3924 /* Special segments, such as the PT_PHDR segment, may contain
3925 no sections, but ordinary, loadable segments should contain
3927 if (segment
->p_type
== PT_LOAD
)
3929 (_("%s: warning: Empty loadable segment detected\n"),
3930 bfd_get_filename (ibfd
));
3933 *pointer_to_map
= map
;
3934 pointer_to_map
= &map
->next
;
3939 /* Now scan the sections in the input BFD again and attempt
3940 to add their corresponding output sections to the segment map.
3941 The problem here is how to handle an output section which has
3942 been moved (ie had its LMA changed). There are four possibilities:
3944 1. None of the sections have been moved.
3945 In this case we can continue to use the segment LMA from the
3948 2. All of the sections have been moved by the same amount.
3949 In this case we can change the segment's LMA to match the LMA
3950 of the first section.
3952 3. Some of the sections have been moved, others have not.
3953 In this case those sections which have not been moved can be
3954 placed in the current segment which will have to have its size,
3955 and possibly its LMA changed, and a new segment or segments will
3956 have to be created to contain the other sections.
3958 4. The sections have been moved, but not be the same amount.
3959 In this case we can change the segment's LMA to match the LMA
3960 of the first section and we will have to create a new segment
3961 or segments to contain the other sections.
3963 In order to save time, we allocate an array to hold the section
3964 pointers that we are interested in. As these sections get assigned
3965 to a segment, they are removed from this array. */
3967 sections
= (asection
**) bfd_malloc
3968 (sizeof (asection
*) * section_count
);
3969 if (sections
== NULL
)
3972 /* Step One: Scan for segment vs section LMA conflicts.
3973 Also add the sections to the section array allocated above.
3974 Also add the sections to the current segment. In the common
3975 case, where the sections have not been moved, this means that
3976 we have completely filled the segment, and there is nothing
3982 for (j
= 0, section
= ibfd
->sections
;
3984 section
= section
->next
)
3986 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3988 output_section
= section
->output_section
;
3990 sections
[j
++] = section
;
3992 /* The Solaris native linker always sets p_paddr to 0.
3993 We try to catch that case here, and set it to the
3995 if (segment
->p_paddr
== 0
3996 && segment
->p_vaddr
!= 0
3998 && output_section
->lma
!= 0
3999 && (output_section
->vma
== (segment
->p_vaddr
4000 + (map
->includes_filehdr
4003 + (map
->includes_phdrs
4004 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4006 map
->p_paddr
= segment
->p_vaddr
;
4008 /* Match up the physical address of the segment with the
4009 LMA address of the output section. */
4010 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4011 || IS_COREFILE_NOTE (segment
, section
))
4013 if (matching_lma
== 0)
4014 matching_lma
= output_section
->lma
;
4016 /* We assume that if the section fits within the segment
4017 then it does not overlap any other section within that
4019 map
->sections
[isec
++] = output_section
;
4021 else if (suggested_lma
== 0)
4022 suggested_lma
= output_section
->lma
;
4026 BFD_ASSERT (j
== section_count
);
4028 /* Step Two: Adjust the physical address of the current segment,
4030 if (isec
== section_count
)
4032 /* All of the sections fitted within the segment as currently
4033 specified. This is the default case. Add the segment to
4034 the list of built segments and carry on to process the next
4035 program header in the input BFD. */
4036 map
->count
= section_count
;
4037 *pointer_to_map
= map
;
4038 pointer_to_map
= &map
->next
;
4045 if (matching_lma
!= 0)
4047 /* At least one section fits inside the current segment.
4048 Keep it, but modify its physical address to match the
4049 LMA of the first section that fitted. */
4050 map
->p_paddr
= matching_lma
;
4054 /* None of the sections fitted inside the current segment.
4055 Change the current segment's physical address to match
4056 the LMA of the first section. */
4057 map
->p_paddr
= suggested_lma
;
4060 /* Offset the segment physical address from the lma
4061 to allow for space taken up by elf headers. */
4062 if (map
->includes_filehdr
)
4063 map
->p_paddr
-= iehdr
->e_ehsize
;
4065 if (map
->includes_phdrs
)
4067 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4069 /* iehdr->e_phnum is just an estimate of the number
4070 of program headers that we will need. Make a note
4071 here of the number we used and the segment we chose
4072 to hold these headers, so that we can adjust the
4073 offset when we know the correct value. */
4074 phdr_adjust_num
= iehdr
->e_phnum
;
4075 phdr_adjust_seg
= map
;
4079 /* Step Three: Loop over the sections again, this time assigning
4080 those that fit to the current segment and remvoing them from the
4081 sections array; but making sure not to leave large gaps. Once all
4082 possible sections have been assigned to the current segment it is
4083 added to the list of built segments and if sections still remain
4084 to be assigned, a new segment is constructed before repeating
4092 /* Fill the current segment with sections that fit. */
4093 for (j
= 0; j
< section_count
; j
++)
4095 section
= sections
[j
];
4097 if (section
== NULL
)
4100 output_section
= section
->output_section
;
4102 BFD_ASSERT (output_section
!= NULL
);
4104 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4105 || IS_COREFILE_NOTE (segment
, section
))
4107 if (map
->count
== 0)
4109 /* If the first section in a segment does not start at
4110 the beginning of the segment, then something is
4112 if (output_section
->lma
!=
4114 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4115 + (map
->includes_phdrs
4116 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4122 asection
* prev_sec
;
4124 prev_sec
= map
->sections
[map
->count
- 1];
4126 /* If the gap between the end of the previous section
4127 and the start of this section is more than
4128 maxpagesize then we need to start a new segment. */
4129 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
4130 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4131 || ((prev_sec
->lma
+ prev_sec
->_raw_size
) > output_section
->lma
))
4133 if (suggested_lma
== 0)
4134 suggested_lma
= output_section
->lma
;
4140 map
->sections
[map
->count
++] = output_section
;
4143 section
->segment_mark
= true;
4145 else if (suggested_lma
== 0)
4146 suggested_lma
= output_section
->lma
;
4149 BFD_ASSERT (map
->count
> 0);
4151 /* Add the current segment to the list of built segments. */
4152 *pointer_to_map
= map
;
4153 pointer_to_map
= &map
->next
;
4155 if (isec
< section_count
)
4157 /* We still have not allocated all of the sections to
4158 segments. Create a new segment here, initialise it
4159 and carry on looping. */
4160 map
= ((struct elf_segment_map
*)
4162 (sizeof (struct elf_segment_map
)
4163 + ((size_t) section_count
- 1)
4164 * sizeof (asection
*))));
4168 /* Initialise the fields of the segment map. Set the physical
4169 physical address to the LMA of the first section that has
4170 not yet been assigned. */
4172 map
->p_type
= segment
->p_type
;
4173 map
->p_flags
= segment
->p_flags
;
4174 map
->p_flags_valid
= 1;
4175 map
->p_paddr
= suggested_lma
;
4176 map
->p_paddr_valid
= 1;
4177 map
->includes_filehdr
= 0;
4178 map
->includes_phdrs
= 0;
4181 while (isec
< section_count
);
4186 /* The Solaris linker creates program headers in which all the
4187 p_paddr fields are zero. When we try to objcopy or strip such a
4188 file, we get confused. Check for this case, and if we find it
4189 reset the p_paddr_valid fields. */
4190 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4191 if (map
->p_paddr
!= 0)
4195 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4196 map
->p_paddr_valid
= 0;
4199 elf_tdata (obfd
)->segment_map
= map_first
;
4201 /* If we had to estimate the number of program headers that were
4202 going to be needed, then check our estimate know and adjust
4203 the offset if necessary. */
4204 if (phdr_adjust_seg
!= NULL
)
4208 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4211 if (count
> phdr_adjust_num
)
4212 phdr_adjust_seg
->p_paddr
4213 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4217 /* Final Step: Sort the segments into ascending order of physical
4219 if (map_first
!= NULL
)
4221 struct elf_segment_map
*prev
;
4224 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4226 /* Yes I know - its a bubble sort.... */
4227 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4229 /* Swap map and map->next. */
4230 prev
->next
= map
->next
;
4231 map
->next
= map
->next
->next
;
4232 prev
->next
->next
= map
;
4242 #undef IS_CONTAINED_BY_VMA
4243 #undef IS_CONTAINED_BY_LMA
4244 #undef IS_COREFILE_NOTE
4245 #undef IS_SOLARIS_PT_INTERP
4246 #undef INCLUDE_SECTION_IN_SEGMENT
4247 #undef SEGMENT_AFTER_SEGMENT
4248 #undef SEGMENT_OVERLAPS
4252 /* Copy private section information. This copies over the entsize
4253 field, and sometimes the info field. */
4256 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4262 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4264 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4265 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4268 /* Copy over private BFD data if it has not already been copied.
4269 This must be done here, rather than in the copy_private_bfd_data
4270 entry point, because the latter is called after the section
4271 contents have been set, which means that the program headers have
4272 already been worked out. */
4273 if (elf_tdata (obfd
)->segment_map
== NULL
4274 && elf_tdata (ibfd
)->phdr
!= NULL
)
4278 /* Only set up the segments if there are no more SEC_ALLOC
4279 sections. FIXME: This won't do the right thing if objcopy is
4280 used to remove the last SEC_ALLOC section, since objcopy
4281 won't call this routine in that case. */
4282 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4283 if ((s
->flags
& SEC_ALLOC
) != 0)
4287 if (! copy_private_bfd_data (ibfd
, obfd
))
4292 ihdr
= &elf_section_data (isec
)->this_hdr
;
4293 ohdr
= &elf_section_data (osec
)->this_hdr
;
4295 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4297 if (ihdr
->sh_type
== SHT_SYMTAB
4298 || ihdr
->sh_type
== SHT_DYNSYM
4299 || ihdr
->sh_type
== SHT_GNU_verneed
4300 || ihdr
->sh_type
== SHT_GNU_verdef
)
4301 ohdr
->sh_info
= ihdr
->sh_info
;
4303 elf_section_data (osec
)->use_rela_p
4304 = elf_section_data (isec
)->use_rela_p
;
4309 /* Copy private symbol information. If this symbol is in a section
4310 which we did not map into a BFD section, try to map the section
4311 index correctly. We use special macro definitions for the mapped
4312 section indices; these definitions are interpreted by the
4313 swap_out_syms function. */
4315 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4316 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4317 #define MAP_STRTAB (SHN_LORESERVE - 3)
4318 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4321 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4327 elf_symbol_type
*isym
, *osym
;
4329 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4330 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4333 isym
= elf_symbol_from (ibfd
, isymarg
);
4334 osym
= elf_symbol_from (obfd
, osymarg
);
4338 && bfd_is_abs_section (isym
->symbol
.section
))
4342 shndx
= isym
->internal_elf_sym
.st_shndx
;
4343 if (shndx
== elf_onesymtab (ibfd
))
4344 shndx
= MAP_ONESYMTAB
;
4345 else if (shndx
== elf_dynsymtab (ibfd
))
4346 shndx
= MAP_DYNSYMTAB
;
4347 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4349 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4350 shndx
= MAP_SHSTRTAB
;
4351 osym
->internal_elf_sym
.st_shndx
= shndx
;
4357 /* Swap out the symbols. */
4360 swap_out_syms (abfd
, sttp
, relocatable_p
)
4362 struct bfd_strtab_hash
**sttp
;
4365 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4367 if (!elf_map_symbols (abfd
))
4370 /* Dump out the symtabs. */
4372 int symcount
= bfd_get_symcount (abfd
);
4373 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4374 struct bfd_strtab_hash
*stt
;
4375 Elf_Internal_Shdr
*symtab_hdr
;
4376 Elf_Internal_Shdr
*symstrtab_hdr
;
4377 char *outbound_syms
;
4380 stt
= _bfd_elf_stringtab_init ();
4384 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4385 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4386 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4387 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4388 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4389 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4391 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4392 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4394 outbound_syms
= bfd_alloc (abfd
,
4395 (1 + symcount
) * bed
->s
->sizeof_sym
);
4396 if (outbound_syms
== NULL
)
4398 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4400 /* now generate the data (for "contents") */
4402 /* Fill in zeroth symbol and swap it out. */
4403 Elf_Internal_Sym sym
;
4409 sym
.st_shndx
= SHN_UNDEF
;
4410 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4411 outbound_syms
+= bed
->s
->sizeof_sym
;
4413 for (idx
= 0; idx
< symcount
; idx
++)
4415 Elf_Internal_Sym sym
;
4416 bfd_vma value
= syms
[idx
]->value
;
4417 elf_symbol_type
*type_ptr
;
4418 flagword flags
= syms
[idx
]->flags
;
4421 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
4423 /* Local section symbols have no name. */
4428 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4431 if (sym
.st_name
== (unsigned long) -1)
4435 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4437 if ((flags
& BSF_SECTION_SYM
) == 0
4438 && bfd_is_com_section (syms
[idx
]->section
))
4440 /* ELF common symbols put the alignment into the `value' field,
4441 and the size into the `size' field. This is backwards from
4442 how BFD handles it, so reverse it here. */
4443 sym
.st_size
= value
;
4444 if (type_ptr
== NULL
4445 || type_ptr
->internal_elf_sym
.st_value
== 0)
4446 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4448 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4449 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4450 (abfd
, syms
[idx
]->section
);
4454 asection
*sec
= syms
[idx
]->section
;
4457 if (sec
->output_section
)
4459 value
+= sec
->output_offset
;
4460 sec
= sec
->output_section
;
4462 /* Don't add in the section vma for relocatable output. */
4463 if (! relocatable_p
)
4465 sym
.st_value
= value
;
4466 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4468 if (bfd_is_abs_section (sec
)
4470 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4472 /* This symbol is in a real ELF section which we did
4473 not create as a BFD section. Undo the mapping done
4474 by copy_private_symbol_data. */
4475 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4479 shndx
= elf_onesymtab (abfd
);
4482 shndx
= elf_dynsymtab (abfd
);
4485 shndx
= elf_tdata (abfd
)->strtab_section
;
4488 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4496 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4502 /* Writing this would be a hell of a lot easier if
4503 we had some decent documentation on bfd, and
4504 knew what to expect of the library, and what to
4505 demand of applications. For example, it
4506 appears that `objcopy' might not set the
4507 section of a symbol to be a section that is
4508 actually in the output file. */
4509 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4510 BFD_ASSERT (sec2
!= 0);
4511 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
4512 BFD_ASSERT (shndx
!= -1);
4516 sym
.st_shndx
= shndx
;
4519 if ((flags
& BSF_FUNCTION
) != 0)
4521 else if ((flags
& BSF_OBJECT
) != 0)
4526 /* Processor-specific types */
4527 if (type_ptr
!= NULL
4528 && bed
->elf_backend_get_symbol_type
)
4529 type
= (*bed
->elf_backend_get_symbol_type
) (&type_ptr
->internal_elf_sym
, type
);
4531 if (flags
& BSF_SECTION_SYM
)
4533 if (flags
& BSF_GLOBAL
)
4534 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
4536 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4538 else if (bfd_is_com_section (syms
[idx
]->section
))
4539 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4540 else if (bfd_is_und_section (syms
[idx
]->section
))
4541 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4545 else if (flags
& BSF_FILE
)
4546 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4549 int bind
= STB_LOCAL
;
4551 if (flags
& BSF_LOCAL
)
4553 else if (flags
& BSF_WEAK
)
4555 else if (flags
& BSF_GLOBAL
)
4558 sym
.st_info
= ELF_ST_INFO (bind
, type
);
4561 if (type_ptr
!= NULL
)
4562 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
4566 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4567 outbound_syms
+= bed
->s
->sizeof_sym
;
4571 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
4572 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4574 symstrtab_hdr
->sh_flags
= 0;
4575 symstrtab_hdr
->sh_addr
= 0;
4576 symstrtab_hdr
->sh_entsize
= 0;
4577 symstrtab_hdr
->sh_link
= 0;
4578 symstrtab_hdr
->sh_info
= 0;
4579 symstrtab_hdr
->sh_addralign
= 1;
4585 /* Return the number of bytes required to hold the symtab vector.
4587 Note that we base it on the count plus 1, since we will null terminate
4588 the vector allocated based on this size. However, the ELF symbol table
4589 always has a dummy entry as symbol #0, so it ends up even. */
4592 _bfd_elf_get_symtab_upper_bound (abfd
)
4597 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4599 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4600 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4606 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4611 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4613 if (elf_dynsymtab (abfd
) == 0)
4615 bfd_set_error (bfd_error_invalid_operation
);
4619 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4620 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4626 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4627 bfd
*abfd ATTRIBUTE_UNUSED
;
4630 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4633 /* Canonicalize the relocs. */
4636 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4645 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
4651 tblptr
= section
->relocation
;
4652 for (i
= 0; i
< section
->reloc_count
; i
++)
4653 *relptr
++ = tblptr
++;
4657 return section
->reloc_count
;
4661 _bfd_elf_get_symtab (abfd
, alocation
)
4663 asymbol
**alocation
;
4665 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4666 (abfd
, alocation
, false);
4669 bfd_get_symcount (abfd
) = symcount
;
4674 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4676 asymbol
**alocation
;
4678 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4679 (abfd
, alocation
, true);
4682 /* Return the size required for the dynamic reloc entries. Any
4683 section that was actually installed in the BFD, and has type
4684 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4685 considered to be a dynamic reloc section. */
4688 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4694 if (elf_dynsymtab (abfd
) == 0)
4696 bfd_set_error (bfd_error_invalid_operation
);
4700 ret
= sizeof (arelent
*);
4701 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4702 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4703 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4704 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4705 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4706 * sizeof (arelent
*));
4711 /* Canonicalize the dynamic relocation entries. Note that we return
4712 the dynamic relocations as a single block, although they are
4713 actually associated with particular sections; the interface, which
4714 was designed for SunOS style shared libraries, expects that there
4715 is only one set of dynamic relocs. Any section that was actually
4716 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4717 the dynamic symbol table, is considered to be a dynamic reloc
4721 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4726 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4730 if (elf_dynsymtab (abfd
) == 0)
4732 bfd_set_error (bfd_error_invalid_operation
);
4736 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4738 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4740 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4741 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4742 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4747 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4749 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4751 for (i
= 0; i
< count
; i
++)
4762 /* Read in the version information. */
4765 _bfd_elf_slurp_version_tables (abfd
)
4768 bfd_byte
*contents
= NULL
;
4770 if (elf_dynverdef (abfd
) != 0)
4772 Elf_Internal_Shdr
*hdr
;
4773 Elf_External_Verdef
*everdef
;
4774 Elf_Internal_Verdef
*iverdef
;
4775 Elf_Internal_Verdef
*iverdefarr
;
4776 Elf_Internal_Verdef iverdefmem
;
4778 unsigned int maxidx
;
4780 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
4782 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4783 if (contents
== NULL
)
4785 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4786 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4789 /* We know the number of entries in the section but not the maximum
4790 index. Therefore we have to run through all entries and find
4792 everdef
= (Elf_External_Verdef
*) contents
;
4794 for (i
= 0; i
< hdr
->sh_info
; ++i
)
4796 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4798 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
4799 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
4801 everdef
= ((Elf_External_Verdef
*)
4802 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
4805 elf_tdata (abfd
)->verdef
=
4806 ((Elf_Internal_Verdef
*)
4807 bfd_zalloc (abfd
, maxidx
* sizeof (Elf_Internal_Verdef
)));
4808 if (elf_tdata (abfd
)->verdef
== NULL
)
4811 elf_tdata (abfd
)->cverdefs
= maxidx
;
4813 everdef
= (Elf_External_Verdef
*) contents
;
4814 iverdefarr
= elf_tdata (abfd
)->verdef
;
4815 for (i
= 0; i
< hdr
->sh_info
; i
++)
4817 Elf_External_Verdaux
*everdaux
;
4818 Elf_Internal_Verdaux
*iverdaux
;
4821 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4823 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
4824 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
4826 iverdef
->vd_bfd
= abfd
;
4828 iverdef
->vd_auxptr
= ((Elf_Internal_Verdaux
*)
4831 * sizeof (Elf_Internal_Verdaux
))));
4832 if (iverdef
->vd_auxptr
== NULL
)
4835 everdaux
= ((Elf_External_Verdaux
*)
4836 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
4837 iverdaux
= iverdef
->vd_auxptr
;
4838 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
4840 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
4842 iverdaux
->vda_nodename
=
4843 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4844 iverdaux
->vda_name
);
4845 if (iverdaux
->vda_nodename
== NULL
)
4848 if (j
+ 1 < iverdef
->vd_cnt
)
4849 iverdaux
->vda_nextptr
= iverdaux
+ 1;
4851 iverdaux
->vda_nextptr
= NULL
;
4853 everdaux
= ((Elf_External_Verdaux
*)
4854 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
4857 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
4859 if (i
+ 1 < hdr
->sh_info
)
4860 iverdef
->vd_nextdef
= iverdef
+ 1;
4862 iverdef
->vd_nextdef
= NULL
;
4864 everdef
= ((Elf_External_Verdef
*)
4865 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
4872 if (elf_dynverref (abfd
) != 0)
4874 Elf_Internal_Shdr
*hdr
;
4875 Elf_External_Verneed
*everneed
;
4876 Elf_Internal_Verneed
*iverneed
;
4879 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
4881 elf_tdata (abfd
)->verref
=
4882 ((Elf_Internal_Verneed
*)
4883 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verneed
)));
4884 if (elf_tdata (abfd
)->verref
== NULL
)
4887 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
4889 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4890 if (contents
== NULL
)
4892 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4893 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4896 everneed
= (Elf_External_Verneed
*) contents
;
4897 iverneed
= elf_tdata (abfd
)->verref
;
4898 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4900 Elf_External_Vernaux
*evernaux
;
4901 Elf_Internal_Vernaux
*ivernaux
;
4904 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4906 iverneed
->vn_bfd
= abfd
;
4908 iverneed
->vn_filename
=
4909 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4911 if (iverneed
->vn_filename
== NULL
)
4914 iverneed
->vn_auxptr
=
4915 ((Elf_Internal_Vernaux
*)
4917 iverneed
->vn_cnt
* sizeof (Elf_Internal_Vernaux
)));
4919 evernaux
= ((Elf_External_Vernaux
*)
4920 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4921 ivernaux
= iverneed
->vn_auxptr
;
4922 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4924 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4926 ivernaux
->vna_nodename
=
4927 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4928 ivernaux
->vna_name
);
4929 if (ivernaux
->vna_nodename
== NULL
)
4932 if (j
+ 1 < iverneed
->vn_cnt
)
4933 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4935 ivernaux
->vna_nextptr
= NULL
;
4937 evernaux
= ((Elf_External_Vernaux
*)
4938 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4941 if (i
+ 1 < hdr
->sh_info
)
4942 iverneed
->vn_nextref
= iverneed
+ 1;
4944 iverneed
->vn_nextref
= NULL
;
4946 everneed
= ((Elf_External_Verneed
*)
4947 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4957 if (contents
== NULL
)
4963 _bfd_elf_make_empty_symbol (abfd
)
4966 elf_symbol_type
*newsym
;
4968 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
4973 newsym
->symbol
.the_bfd
= abfd
;
4974 return &newsym
->symbol
;
4979 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4980 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4984 bfd_symbol_info (symbol
, ret
);
4987 /* Return whether a symbol name implies a local symbol. Most targets
4988 use this function for the is_local_label_name entry point, but some
4992 _bfd_elf_is_local_label_name (abfd
, name
)
4993 bfd
*abfd ATTRIBUTE_UNUSED
;
4996 /* Normal local symbols start with ``.L''. */
4997 if (name
[0] == '.' && name
[1] == 'L')
5000 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5001 DWARF debugging symbols starting with ``..''. */
5002 if (name
[0] == '.' && name
[1] == '.')
5005 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5006 emitting DWARF debugging output. I suspect this is actually a
5007 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5008 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5009 underscore to be emitted on some ELF targets). For ease of use,
5010 we treat such symbols as local. */
5011 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5018 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5019 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5020 asymbol
*symbol ATTRIBUTE_UNUSED
;
5027 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5029 enum bfd_architecture arch
;
5030 unsigned long machine
;
5032 /* If this isn't the right architecture for this backend, and this
5033 isn't the generic backend, fail. */
5034 if (arch
!= get_elf_backend_data (abfd
)->arch
5035 && arch
!= bfd_arch_unknown
5036 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5039 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5042 /* Find the function to a particular section and offset,
5043 for error reporting. */
5046 elf_find_function (abfd
, section
, symbols
, offset
,
5047 filename_ptr
, functionname_ptr
)
5048 bfd
*abfd ATTRIBUTE_UNUSED
;
5052 const char **filename_ptr
;
5053 const char **functionname_ptr
;
5055 const char *filename
;
5064 for (p
= symbols
; *p
!= NULL
; p
++)
5068 q
= (elf_symbol_type
*) *p
;
5070 if (bfd_get_section (&q
->symbol
) != section
)
5073 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5078 filename
= bfd_asymbol_name (&q
->symbol
);
5082 if (q
->symbol
.section
== section
5083 && q
->symbol
.value
>= low_func
5084 && q
->symbol
.value
<= offset
)
5086 func
= (asymbol
*) q
;
5087 low_func
= q
->symbol
.value
;
5097 *filename_ptr
= filename
;
5098 if (functionname_ptr
)
5099 *functionname_ptr
= bfd_asymbol_name (func
);
5104 /* Find the nearest line to a particular section and offset,
5105 for error reporting. */
5108 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5109 filename_ptr
, functionname_ptr
, line_ptr
)
5114 const char **filename_ptr
;
5115 const char **functionname_ptr
;
5116 unsigned int *line_ptr
;
5120 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5121 filename_ptr
, functionname_ptr
,
5124 if (!*functionname_ptr
)
5125 elf_find_function (abfd
, section
, symbols
, offset
,
5126 *filename_ptr
? NULL
: filename_ptr
,
5132 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5133 filename_ptr
, functionname_ptr
,
5135 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5137 if (!*functionname_ptr
)
5138 elf_find_function (abfd
, section
, symbols
, offset
,
5139 *filename_ptr
? NULL
: filename_ptr
,
5145 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5146 &found
, filename_ptr
,
5147 functionname_ptr
, line_ptr
,
5148 &elf_tdata (abfd
)->line_info
))
5153 if (symbols
== NULL
)
5156 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5157 filename_ptr
, functionname_ptr
))
5165 _bfd_elf_sizeof_headers (abfd
, reloc
)
5171 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5173 ret
+= get_program_header_size (abfd
);
5178 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5183 bfd_size_type count
;
5185 Elf_Internal_Shdr
*hdr
;
5187 if (! abfd
->output_has_begun
5188 && ! _bfd_elf_compute_section_file_positions
5189 (abfd
, (struct bfd_link_info
*) NULL
))
5192 hdr
= &elf_section_data (section
)->this_hdr
;
5194 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
5196 if (bfd_write (location
, 1, count
, abfd
) != count
)
5203 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5204 bfd
*abfd ATTRIBUTE_UNUSED
;
5205 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5206 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5213 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5216 Elf_Internal_Rel
*dst
;
5222 /* Try to convert a non-ELF reloc into an ELF one. */
5225 _bfd_elf_validate_reloc (abfd
, areloc
)
5229 /* Check whether we really have an ELF howto. */
5231 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5233 bfd_reloc_code_real_type code
;
5234 reloc_howto_type
*howto
;
5236 /* Alien reloc: Try to determine its type to replace it with an
5237 equivalent ELF reloc. */
5239 if (areloc
->howto
->pc_relative
)
5241 switch (areloc
->howto
->bitsize
)
5244 code
= BFD_RELOC_8_PCREL
;
5247 code
= BFD_RELOC_12_PCREL
;
5250 code
= BFD_RELOC_16_PCREL
;
5253 code
= BFD_RELOC_24_PCREL
;
5256 code
= BFD_RELOC_32_PCREL
;
5259 code
= BFD_RELOC_64_PCREL
;
5265 howto
= bfd_reloc_type_lookup (abfd
, code
);
5267 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5269 if (howto
->pcrel_offset
)
5270 areloc
->addend
+= areloc
->address
;
5272 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5277 switch (areloc
->howto
->bitsize
)
5283 code
= BFD_RELOC_14
;
5286 code
= BFD_RELOC_16
;
5289 code
= BFD_RELOC_26
;
5292 code
= BFD_RELOC_32
;
5295 code
= BFD_RELOC_64
;
5301 howto
= bfd_reloc_type_lookup (abfd
, code
);
5305 areloc
->howto
= howto
;
5313 (*_bfd_error_handler
)
5314 (_("%s: unsupported relocation type %s"),
5315 bfd_get_filename (abfd
), areloc
->howto
->name
);
5316 bfd_set_error (bfd_error_bad_value
);
5321 _bfd_elf_close_and_cleanup (abfd
)
5324 if (bfd_get_format (abfd
) == bfd_object
)
5326 if (elf_shstrtab (abfd
) != NULL
)
5327 _bfd_stringtab_free (elf_shstrtab (abfd
));
5330 return _bfd_generic_close_and_cleanup (abfd
);
5333 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5334 in the relocation's offset. Thus we cannot allow any sort of sanity
5335 range-checking to interfere. There is nothing else to do in processing
5338 bfd_reloc_status_type
5339 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5340 bfd
*abfd ATTRIBUTE_UNUSED
;
5341 arelent
*re ATTRIBUTE_UNUSED
;
5342 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5343 PTR data ATTRIBUTE_UNUSED
;
5344 asection
*is ATTRIBUTE_UNUSED
;
5345 bfd
*obfd ATTRIBUTE_UNUSED
;
5346 char **errmsg ATTRIBUTE_UNUSED
;
5348 return bfd_reloc_ok
;
5351 /* Elf core file support. Much of this only works on native
5352 toolchains, since we rely on knowing the
5353 machine-dependent procfs structure in order to pick
5354 out details about the corefile. */
5356 #ifdef HAVE_SYS_PROCFS_H
5357 # include <sys/procfs.h>
5360 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5363 elfcore_make_pid (abfd
)
5366 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5367 + (elf_tdata (abfd
)->core_pid
));
5370 /* If there isn't a section called NAME, make one, using
5371 data from SECT. Note, this function will generate a
5372 reference to NAME, so you shouldn't deallocate or
5376 elfcore_maybe_make_sect (abfd
, name
, sect
)
5383 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5386 sect2
= bfd_make_section (abfd
, name
);
5390 sect2
->_raw_size
= sect
->_raw_size
;
5391 sect2
->filepos
= sect
->filepos
;
5392 sect2
->flags
= sect
->flags
;
5393 sect2
->alignment_power
= sect
->alignment_power
;
5397 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5398 actually creates up to two pseudosections:
5399 - For the single-threaded case, a section named NAME, unless
5400 such a section already exists.
5401 - For the multi-threaded case, a section named "NAME/PID", where
5402 PID is elfcore_make_pid (abfd).
5403 Both pseudosections have identical contents. */
5405 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
5412 char *threaded_name
;
5415 /* Build the section name. */
5417 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5418 threaded_name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5419 if (threaded_name
== NULL
)
5421 strcpy (threaded_name
, buf
);
5423 sect
= bfd_make_section (abfd
, threaded_name
);
5426 sect
->_raw_size
= size
;
5427 sect
->filepos
= filepos
;
5428 sect
->flags
= SEC_HAS_CONTENTS
;
5429 sect
->alignment_power
= 2;
5431 return elfcore_maybe_make_sect (abfd
, name
, sect
);
5434 /* prstatus_t exists on:
5436 linux 2.[01] + glibc
5440 #if defined (HAVE_PRSTATUS_T)
5441 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
5444 elfcore_grok_prstatus (abfd
, note
)
5446 Elf_Internal_Note
*note
;
5451 if (note
->descsz
== sizeof (prstatus_t
))
5455 raw_size
= sizeof (prstat
.pr_reg
);
5456 offset
= offsetof (prstatus_t
, pr_reg
);
5457 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5459 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5460 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5462 /* pr_who exists on:
5465 pr_who doesn't exist on:
5468 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5469 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5472 #if defined (HAVE_PRSTATUS32_T)
5473 else if (note
->descsz
== sizeof (prstatus32_t
))
5475 /* 64-bit host, 32-bit corefile */
5476 prstatus32_t prstat
;
5478 raw_size
= sizeof (prstat
.pr_reg
);
5479 offset
= offsetof (prstatus32_t
, pr_reg
);
5480 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5482 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5483 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5485 /* pr_who exists on:
5488 pr_who doesn't exist on:
5491 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5492 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5495 #endif /* HAVE_PRSTATUS32_T */
5498 /* Fail - we don't know how to handle any other
5499 note size (ie. data object type). */
5503 /* Make a ".reg/999" section and a ".reg" section. */
5504 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
5505 raw_size
, note
->descpos
+ offset
);
5507 #endif /* defined (HAVE_PRSTATUS_T) */
5509 /* Create a pseudosection containing the exact contents of NOTE. */
5511 elfcore_make_note_pseudosection (abfd
, name
, note
)
5514 Elf_Internal_Note
*note
;
5516 return _bfd_elfcore_make_pseudosection (abfd
, name
,
5517 note
->descsz
, note
->descpos
);
5520 /* There isn't a consistent prfpregset_t across platforms,
5521 but it doesn't matter, because we don't have to pick this
5522 data structure apart. */
5525 elfcore_grok_prfpreg (abfd
, note
)
5527 Elf_Internal_Note
*note
;
5529 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
5532 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5533 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5537 elfcore_grok_prxfpreg (abfd
, note
)
5539 Elf_Internal_Note
*note
;
5541 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
5544 #if defined (HAVE_PRPSINFO_T)
5545 typedef prpsinfo_t elfcore_psinfo_t
;
5546 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5547 typedef prpsinfo32_t elfcore_psinfo32_t
;
5551 #if defined (HAVE_PSINFO_T)
5552 typedef psinfo_t elfcore_psinfo_t
;
5553 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5554 typedef psinfo32_t elfcore_psinfo32_t
;
5558 /* return a malloc'ed copy of a string at START which is at
5559 most MAX bytes long, possibly without a terminating '\0'.
5560 the copy will always have a terminating '\0'. */
5563 _bfd_elfcore_strndup (abfd
, start
, max
)
5569 char *end
= memchr (start
, '\0', max
);
5577 dup
= bfd_alloc (abfd
, len
+ 1);
5581 memcpy (dup
, start
, len
);
5587 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5588 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
5591 elfcore_grok_psinfo (abfd
, note
)
5593 Elf_Internal_Note
*note
;
5595 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
5597 elfcore_psinfo_t psinfo
;
5599 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5601 elf_tdata (abfd
)->core_program
5602 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
5603 sizeof (psinfo
.pr_fname
));
5605 elf_tdata (abfd
)->core_command
5606 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
5607 sizeof (psinfo
.pr_psargs
));
5609 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5610 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
5612 /* 64-bit host, 32-bit corefile */
5613 elfcore_psinfo32_t psinfo
;
5615 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5617 elf_tdata (abfd
)->core_program
5618 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
5619 sizeof (psinfo
.pr_fname
));
5621 elf_tdata (abfd
)->core_command
5622 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
5623 sizeof (psinfo
.pr_psargs
));
5629 /* Fail - we don't know how to handle any other
5630 note size (ie. data object type). */
5634 /* Note that for some reason, a spurious space is tacked
5635 onto the end of the args in some (at least one anyway)
5636 implementations, so strip it off if it exists. */
5639 char *command
= elf_tdata (abfd
)->core_command
;
5640 int n
= strlen (command
);
5642 if (0 < n
&& command
[n
- 1] == ' ')
5643 command
[n
- 1] = '\0';
5648 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5650 #if defined (HAVE_PSTATUS_T)
5652 elfcore_grok_pstatus (abfd
, note
)
5654 Elf_Internal_Note
*note
;
5656 if (note
->descsz
== sizeof (pstatus_t
)
5657 #if defined (HAVE_PXSTATUS_T)
5658 || note
->descsz
== sizeof (pxstatus_t
)
5664 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5666 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5668 #if defined (HAVE_PSTATUS32_T)
5669 else if (note
->descsz
== sizeof (pstatus32_t
))
5671 /* 64-bit host, 32-bit corefile */
5674 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5676 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5679 /* Could grab some more details from the "representative"
5680 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5681 NT_LWPSTATUS note, presumably. */
5685 #endif /* defined (HAVE_PSTATUS_T) */
5687 #if defined (HAVE_LWPSTATUS_T)
5689 elfcore_grok_lwpstatus (abfd
, note
)
5691 Elf_Internal_Note
*note
;
5693 lwpstatus_t lwpstat
;
5698 if (note
->descsz
!= sizeof (lwpstat
)
5699 #if defined (HAVE_LWPXSTATUS_T)
5700 && note
->descsz
!= sizeof (lwpxstatus_t
)
5705 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
5707 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
5708 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
5710 /* Make a ".reg/999" section. */
5712 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5713 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5718 sect
= bfd_make_section (abfd
, name
);
5722 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5723 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
5724 sect
->filepos
= note
->descpos
5725 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
5728 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5729 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
5730 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
5733 sect
->flags
= SEC_HAS_CONTENTS
;
5734 sect
->alignment_power
= 2;
5736 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5739 /* Make a ".reg2/999" section */
5741 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
5742 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5747 sect
= bfd_make_section (abfd
, name
);
5751 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5752 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
5753 sect
->filepos
= note
->descpos
5754 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
5757 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5758 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
5759 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
5762 sect
->flags
= SEC_HAS_CONTENTS
;
5763 sect
->alignment_power
= 2;
5765 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
5767 #endif /* defined (HAVE_LWPSTATUS_T) */
5769 #if defined (HAVE_WIN32_PSTATUS_T)
5771 elfcore_grok_win32pstatus (abfd
, note
)
5773 Elf_Internal_Note
*note
;
5778 win32_pstatus_t pstatus
;
5780 if (note
->descsz
< sizeof (pstatus
))
5783 memcpy (&pstatus
, note
->descdata
, note
->descsz
);
5785 switch (pstatus
.data_type
)
5787 case NOTE_INFO_PROCESS
:
5788 /* FIXME: need to add ->core_command. */
5789 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
5790 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
5793 case NOTE_INFO_THREAD
:
5794 /* Make a ".reg/999" section. */
5795 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
5797 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5803 sect
= bfd_make_section (abfd
, name
);
5807 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
5808 sect
->filepos
= note
->descpos
+ offsetof (struct win32_pstatus
,
5809 data
.thread_info
.thread_context
);
5810 sect
->flags
= SEC_HAS_CONTENTS
;
5811 sect
->alignment_power
= 2;
5813 if (pstatus
.data
.thread_info
.is_active_thread
)
5814 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5818 case NOTE_INFO_MODULE
:
5819 /* Make a ".module/xxxxxxxx" section. */
5820 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
5822 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5828 sect
= bfd_make_section (abfd
, name
);
5833 sect
->_raw_size
= note
->descsz
;
5834 sect
->filepos
= note
->descpos
;
5835 sect
->flags
= SEC_HAS_CONTENTS
;
5836 sect
->alignment_power
= 2;
5845 #endif /* HAVE_WIN32_PSTATUS_T */
5848 elfcore_grok_note (abfd
, note
)
5850 Elf_Internal_Note
*note
;
5852 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5860 if (bed
->elf_backend_grok_prstatus
)
5861 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
5863 #if defined (HAVE_PRSTATUS_T)
5864 return elfcore_grok_prstatus (abfd
, note
);
5869 #if defined (HAVE_PSTATUS_T)
5871 return elfcore_grok_pstatus (abfd
, note
);
5874 #if defined (HAVE_LWPSTATUS_T)
5876 return elfcore_grok_lwpstatus (abfd
, note
);
5879 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
5880 return elfcore_grok_prfpreg (abfd
, note
);
5882 #if defined (HAVE_WIN32_PSTATUS_T)
5883 case NT_WIN32PSTATUS
:
5884 return elfcore_grok_win32pstatus (abfd
, note
);
5887 case NT_PRXFPREG
: /* Linux SSE extension */
5888 if (note
->namesz
== 5
5889 && ! strcmp (note
->namedata
, "LINUX"))
5890 return elfcore_grok_prxfpreg (abfd
, note
);
5896 if (bed
->elf_backend_grok_psinfo
)
5897 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
5899 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5900 return elfcore_grok_psinfo (abfd
, note
);
5908 elfcore_read_notes (abfd
, offset
, size
)
5919 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
5922 buf
= bfd_malloc ((size_t) size
);
5926 if (bfd_read (buf
, size
, 1, abfd
) != size
)
5934 while (p
< buf
+ size
)
5936 /* FIXME: bad alignment assumption. */
5937 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
5938 Elf_Internal_Note in
;
5940 in
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->type
);
5942 in
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->namesz
);
5943 in
.namedata
= xnp
->name
;
5945 in
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->descsz
);
5946 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
5947 in
.descpos
= offset
+ (in
.descdata
- buf
);
5949 if (! elfcore_grok_note (abfd
, &in
))
5952 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
5959 /* Providing external access to the ELF program header table. */
5961 /* Return an upper bound on the number of bytes required to store a
5962 copy of ABFD's program header table entries. Return -1 if an error
5963 occurs; bfd_get_error will return an appropriate code. */
5966 bfd_get_elf_phdr_upper_bound (abfd
)
5969 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5971 bfd_set_error (bfd_error_wrong_format
);
5975 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
5978 /* Copy ABFD's program header table entries to *PHDRS. The entries
5979 will be stored as an array of Elf_Internal_Phdr structures, as
5980 defined in include/elf/internal.h. To find out how large the
5981 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5983 Return the number of program header table entries read, or -1 if an
5984 error occurs; bfd_get_error will return an appropriate code. */
5987 bfd_get_elf_phdrs (abfd
, phdrs
)
5993 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5995 bfd_set_error (bfd_error_wrong_format
);
5999 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
6000 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
6001 num_phdrs
* sizeof (Elf_Internal_Phdr
));
6007 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
6008 bfd
*abfd ATTRIBUTE_UNUSED
;
6013 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6015 i_ehdrp
= elf_elfheader (abfd
);
6016 if (i_ehdrp
== NULL
)
6017 sprintf_vma (buf
, value
);
6020 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6022 #if BFD_HOST_64BIT_LONG
6023 sprintf (buf
, "%016lx", value
);
6025 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
6026 _bfd_int64_low (value
));
6030 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
6033 sprintf_vma (buf
, value
);
6038 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
6039 bfd
*abfd ATTRIBUTE_UNUSED
;
6044 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6046 i_ehdrp
= elf_elfheader (abfd
);
6047 if (i_ehdrp
== NULL
)
6048 fprintf_vma ((FILE *) stream
, value
);
6051 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6053 #if BFD_HOST_64BIT_LONG
6054 fprintf ((FILE *) stream
, "%016lx", value
);
6056 fprintf ((FILE *) stream
, "%08lx%08lx",
6057 _bfd_int64_high (value
), _bfd_int64_low (value
));
6061 fprintf ((FILE *) stream
, "%08lx",
6062 (unsigned long) (value
& 0xffffffff));
6065 fprintf_vma ((FILE *) stream
, value
);
6069 enum elf_reloc_type_class
6070 _bfd_elf_reloc_type_class (type
)
6073 return reloc_class_normal
;