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
*, file_ptr
, bfd_size_type
));
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
*, file_ptr
, bfd_size_type
));
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
= H_GET_16 (abfd
, src
->vd_version
);
84 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
85 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
86 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
87 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
88 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
89 dst
->vd_next
= 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 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
101 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
102 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
103 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
104 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
105 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
106 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
= H_GET_32 (abfd
, src
->vda_name
);
118 dst
->vda_next
= 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 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
130 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
= H_GET_16 (abfd
, src
->vn_version
);
142 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
143 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
144 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
145 dst
->vn_next
= 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 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
157 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
158 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
159 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
160 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
= H_GET_32 (abfd
, src
->vna_hash
);
172 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
173 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
174 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
175 dst
->vna_next
= 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 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
187 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
188 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
189 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
190 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
= 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 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
) != 0)
257 if (bfd_bread ((PTR
) buf
, size
, 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 bfd_size_type amt
= sizeof (struct elf_obj_tdata
);
273 elf_tdata (abfd
) = (struct elf_obj_tdata
*) bfd_zalloc (abfd
, amt
);
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 bfd_size_type 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_archive_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 ((bfd_vma
) 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 if (!is_elf_hash_table (info
))
576 if (elf_hash_table (info
)->merge_info
)
577 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
);
581 /* Print out the program headers. */
584 _bfd_elf_print_private_bfd_data (abfd
, farg
)
588 FILE *f
= (FILE *) farg
;
589 Elf_Internal_Phdr
*p
;
591 bfd_byte
*dynbuf
= NULL
;
593 p
= elf_tdata (abfd
)->phdr
;
598 fprintf (f
, _("\nProgram Header:\n"));
599 c
= elf_elfheader (abfd
)->e_phnum
;
600 for (i
= 0; i
< c
; i
++, p
++)
607 case PT_NULL
: pt
= "NULL"; break;
608 case PT_LOAD
: pt
= "LOAD"; break;
609 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
610 case PT_INTERP
: pt
= "INTERP"; break;
611 case PT_NOTE
: pt
= "NOTE"; break;
612 case PT_SHLIB
: pt
= "SHLIB"; break;
613 case PT_PHDR
: pt
= "PHDR"; break;
614 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
616 fprintf (f
, "%8s off 0x", pt
);
617 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
618 fprintf (f
, " vaddr 0x");
619 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
620 fprintf (f
, " paddr 0x");
621 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
622 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
623 fprintf (f
, " filesz 0x");
624 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
625 fprintf (f
, " memsz 0x");
626 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
627 fprintf (f
, " flags %c%c%c",
628 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
629 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
630 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
631 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
632 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
637 s
= bfd_get_section_by_name (abfd
, ".dynamic");
641 unsigned long shlink
;
642 bfd_byte
*extdyn
, *extdynend
;
644 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
646 fprintf (f
, _("\nDynamic Section:\n"));
648 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
651 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
655 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
658 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
660 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
661 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
664 extdynend
= extdyn
+ s
->_raw_size
;
665 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
667 Elf_Internal_Dyn dyn
;
672 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
674 if (dyn
.d_tag
== DT_NULL
)
681 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
685 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
686 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
687 case DT_PLTGOT
: name
= "PLTGOT"; break;
688 case DT_HASH
: name
= "HASH"; break;
689 case DT_STRTAB
: name
= "STRTAB"; break;
690 case DT_SYMTAB
: name
= "SYMTAB"; break;
691 case DT_RELA
: name
= "RELA"; break;
692 case DT_RELASZ
: name
= "RELASZ"; break;
693 case DT_RELAENT
: name
= "RELAENT"; break;
694 case DT_STRSZ
: name
= "STRSZ"; break;
695 case DT_SYMENT
: name
= "SYMENT"; break;
696 case DT_INIT
: name
= "INIT"; break;
697 case DT_FINI
: name
= "FINI"; break;
698 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
699 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
700 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
701 case DT_REL
: name
= "REL"; break;
702 case DT_RELSZ
: name
= "RELSZ"; break;
703 case DT_RELENT
: name
= "RELENT"; break;
704 case DT_PLTREL
: name
= "PLTREL"; break;
705 case DT_DEBUG
: name
= "DEBUG"; break;
706 case DT_TEXTREL
: name
= "TEXTREL"; break;
707 case DT_JMPREL
: name
= "JMPREL"; break;
708 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
709 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
710 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
711 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
712 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
713 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
714 case DT_FLAGS
: name
= "FLAGS"; break;
715 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
716 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
717 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
718 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
719 case DT_MOVEENT
: name
= "MOVEENT"; break;
720 case DT_MOVESZ
: name
= "MOVESZ"; break;
721 case DT_FEATURE
: name
= "FEATURE"; break;
722 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
723 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
724 case DT_SYMINENT
: name
= "SYMINENT"; break;
725 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
726 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
727 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
728 case DT_PLTPAD
: name
= "PLTPAD"; break;
729 case DT_MOVETAB
: name
= "MOVETAB"; break;
730 case DT_SYMINFO
: name
= "SYMINFO"; break;
731 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
732 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
733 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
734 case DT_VERSYM
: name
= "VERSYM"; break;
735 case DT_VERDEF
: name
= "VERDEF"; break;
736 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
737 case DT_VERNEED
: name
= "VERNEED"; break;
738 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
739 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
740 case DT_USED
: name
= "USED"; break;
741 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
744 fprintf (f
, " %-11s ", name
);
746 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
750 unsigned int tagv
= dyn
.d_un
.d_val
;
752 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
755 fprintf (f
, "%s", string
);
764 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
765 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
767 if (! _bfd_elf_slurp_version_tables (abfd
))
771 if (elf_dynverdef (abfd
) != 0)
773 Elf_Internal_Verdef
*t
;
775 fprintf (f
, _("\nVersion definitions:\n"));
776 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
778 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
779 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
780 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
782 Elf_Internal_Verdaux
*a
;
785 for (a
= t
->vd_auxptr
->vda_nextptr
;
788 fprintf (f
, "%s ", a
->vda_nodename
);
794 if (elf_dynverref (abfd
) != 0)
796 Elf_Internal_Verneed
*t
;
798 fprintf (f
, _("\nVersion References:\n"));
799 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
801 Elf_Internal_Vernaux
*a
;
803 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
804 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
805 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
806 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
818 /* Display ELF-specific fields of a symbol. */
821 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
825 bfd_print_symbol_type how
;
827 FILE *file
= (FILE *) filep
;
830 case bfd_print_symbol_name
:
831 fprintf (file
, "%s", symbol
->name
);
833 case bfd_print_symbol_more
:
834 fprintf (file
, "elf ");
835 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
836 fprintf (file
, " %lx", (long) symbol
->flags
);
838 case bfd_print_symbol_all
:
840 const char *section_name
;
841 const char *name
= NULL
;
842 struct elf_backend_data
*bed
;
843 unsigned char st_other
;
845 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
847 bed
= get_elf_backend_data (abfd
);
848 if (bed
->elf_backend_print_symbol_all
)
849 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
854 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
857 fprintf (file
, " %s\t", section_name
);
858 /* Print the "other" value for a symbol. For common symbols,
859 we've already printed the size; now print the alignment.
860 For other symbols, we have no specified alignment, and
861 we've printed the address; now print the size. */
862 bfd_fprintf_vma (abfd
, file
,
863 (bfd_is_com_section (symbol
->section
)
864 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
865 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
867 /* If we have version information, print it. */
868 if (elf_tdata (abfd
)->dynversym_section
!= 0
869 && (elf_tdata (abfd
)->dynverdef_section
!= 0
870 || elf_tdata (abfd
)->dynverref_section
!= 0))
873 const char *version_string
;
875 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
879 else if (vernum
== 1)
880 version_string
= "Base";
881 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
883 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
886 Elf_Internal_Verneed
*t
;
889 for (t
= elf_tdata (abfd
)->verref
;
893 Elf_Internal_Vernaux
*a
;
895 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
897 if (a
->vna_other
== vernum
)
899 version_string
= a
->vna_nodename
;
906 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
907 fprintf (file
, " %-11s", version_string
);
912 fprintf (file
, " (%s)", version_string
);
913 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
918 /* If the st_other field is not zero, print it. */
919 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
924 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
925 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
926 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
928 /* Some other non-defined flags are also present, so print
930 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
933 fprintf (file
, " %s", name
);
939 /* Create an entry in an ELF linker hash table. */
941 struct bfd_hash_entry
*
942 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
943 struct bfd_hash_entry
*entry
;
944 struct bfd_hash_table
*table
;
947 /* Allocate the structure if it has not already been allocated by a
951 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
956 /* Call the allocation method of the superclass. */
957 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
960 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
961 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
963 /* Set local fields. */
967 ret
->dynstr_index
= 0;
969 ret
->got
.refcount
= htab
->init_refcount
;
970 ret
->plt
.refcount
= htab
->init_refcount
;
971 ret
->linker_section_pointer
= NULL
;
972 ret
->verinfo
.verdef
= NULL
;
973 ret
->vtable_entries_used
= NULL
;
974 ret
->vtable_entries_size
= 0;
975 ret
->vtable_parent
= NULL
;
976 ret
->type
= STT_NOTYPE
;
978 /* Assume that we have been called by a non-ELF symbol reader.
979 This flag is then reset by the code which reads an ELF input
980 file. This ensures that a symbol created by a non-ELF symbol
981 reader will have the flag set correctly. */
982 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
988 /* Copy data from an indirect symbol to its direct symbol, hiding the
989 old indirect symbol. */
992 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
993 struct elf_link_hash_entry
*dir
, *ind
;
995 /* Copy down any references that we may have already seen to the
996 symbol which just became indirect. */
998 dir
->elf_link_hash_flags
|=
999 (ind
->elf_link_hash_flags
1000 & (ELF_LINK_HASH_REF_DYNAMIC
1001 | ELF_LINK_HASH_REF_REGULAR
1002 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1003 | ELF_LINK_NON_GOT_REF
));
1005 /* Copy over the global and procedure linkage table refcount entries.
1006 These may have been already set up by a check_relocs routine. */
1007 if (dir
->got
.refcount
<= 0)
1009 dir
->got
.refcount
= ind
->got
.refcount
;
1010 ind
->got
.refcount
= 0;
1012 BFD_ASSERT (ind
->got
.refcount
<= 0);
1014 if (dir
->plt
.refcount
<= 0)
1016 dir
->plt
.refcount
= ind
->plt
.refcount
;
1017 ind
->plt
.refcount
= 0;
1019 BFD_ASSERT (ind
->plt
.refcount
<= 0);
1021 if (dir
->dynindx
== -1)
1023 dir
->dynindx
= ind
->dynindx
;
1024 dir
->dynstr_index
= ind
->dynstr_index
;
1026 ind
->dynstr_index
= 0;
1028 BFD_ASSERT (ind
->dynindx
== -1);
1032 _bfd_elf_link_hash_hide_symbol (info
, h
)
1033 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1034 struct elf_link_hash_entry
*h
;
1036 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1037 h
->plt
.offset
= (bfd_vma
) -1;
1038 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1042 /* Initialize an ELF linker hash table. */
1045 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1046 struct elf_link_hash_table
*table
;
1048 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1049 struct bfd_hash_table
*,
1054 table
->dynamic_sections_created
= false;
1055 table
->dynobj
= NULL
;
1056 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
- 1;
1057 /* The first dynamic symbol is a dummy. */
1058 table
->dynsymcount
= 1;
1059 table
->dynstr
= NULL
;
1060 table
->bucketcount
= 0;
1061 table
->needed
= NULL
;
1062 table
->runpath
= NULL
;
1064 table
->stab_info
= NULL
;
1065 table
->merge_info
= NULL
;
1066 table
->dynlocal
= NULL
;
1067 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1068 table
->root
.type
= bfd_link_elf_hash_table
;
1073 /* Create an ELF linker hash table. */
1075 struct bfd_link_hash_table
*
1076 _bfd_elf_link_hash_table_create (abfd
)
1079 struct elf_link_hash_table
*ret
;
1080 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1082 ret
= (struct elf_link_hash_table
*) bfd_alloc (abfd
, amt
);
1083 if (ret
== (struct elf_link_hash_table
*) NULL
)
1086 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1088 bfd_release (abfd
, ret
);
1095 /* This is a hook for the ELF emulation code in the generic linker to
1096 tell the backend linker what file name to use for the DT_NEEDED
1097 entry for a dynamic object. The generic linker passes name as an
1098 empty string to indicate that no DT_NEEDED entry should be made. */
1101 bfd_elf_set_dt_needed_name (abfd
, name
)
1105 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1106 && bfd_get_format (abfd
) == bfd_object
)
1107 elf_dt_name (abfd
) = name
;
1111 bfd_elf_set_dt_needed_soname (abfd
, name
)
1115 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1116 && bfd_get_format (abfd
) == bfd_object
)
1117 elf_dt_soname (abfd
) = name
;
1120 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1121 the linker ELF emulation code. */
1123 struct bfd_link_needed_list
*
1124 bfd_elf_get_needed_list (abfd
, info
)
1125 bfd
*abfd ATTRIBUTE_UNUSED
;
1126 struct bfd_link_info
*info
;
1128 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1130 return elf_hash_table (info
)->needed
;
1133 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1134 hook for the linker ELF emulation code. */
1136 struct bfd_link_needed_list
*
1137 bfd_elf_get_runpath_list (abfd
, info
)
1138 bfd
*abfd ATTRIBUTE_UNUSED
;
1139 struct bfd_link_info
*info
;
1141 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1143 return elf_hash_table (info
)->runpath
;
1146 /* Get the name actually used for a dynamic object for a link. This
1147 is the SONAME entry if there is one. Otherwise, it is the string
1148 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1151 bfd_elf_get_dt_soname (abfd
)
1154 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1155 && bfd_get_format (abfd
) == bfd_object
)
1156 return elf_dt_name (abfd
);
1160 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1161 the ELF linker emulation code. */
1164 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1166 struct bfd_link_needed_list
**pneeded
;
1169 bfd_byte
*dynbuf
= NULL
;
1171 unsigned long shlink
;
1172 bfd_byte
*extdyn
, *extdynend
;
1174 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1178 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1179 || bfd_get_format (abfd
) != bfd_object
)
1182 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1183 if (s
== NULL
|| s
->_raw_size
== 0)
1186 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1190 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1194 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1198 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1200 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1201 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1204 extdynend
= extdyn
+ s
->_raw_size
;
1205 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1207 Elf_Internal_Dyn dyn
;
1209 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1211 if (dyn
.d_tag
== DT_NULL
)
1214 if (dyn
.d_tag
== DT_NEEDED
)
1217 struct bfd_link_needed_list
*l
;
1218 unsigned int tagv
= dyn
.d_un
.d_val
;
1221 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1226 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1247 /* Allocate an ELF string table--force the first byte to be zero. */
1249 struct bfd_strtab_hash
*
1250 _bfd_elf_stringtab_init ()
1252 struct bfd_strtab_hash
*ret
;
1254 ret
= _bfd_stringtab_init ();
1259 loc
= _bfd_stringtab_add (ret
, "", true, false);
1260 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1261 if (loc
== (bfd_size_type
) -1)
1263 _bfd_stringtab_free (ret
);
1270 /* ELF .o/exec file reading */
1272 /* Create a new bfd section from an ELF section header. */
1275 bfd_section_from_shdr (abfd
, shindex
)
1277 unsigned int shindex
;
1279 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1280 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1281 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1284 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1286 switch (hdr
->sh_type
)
1289 /* Inactive section. Throw it away. */
1292 case SHT_PROGBITS
: /* Normal section with contents. */
1293 case SHT_DYNAMIC
: /* Dynamic linking information. */
1294 case SHT_NOBITS
: /* .bss section. */
1295 case SHT_HASH
: /* .hash section. */
1296 case SHT_NOTE
: /* .note section. */
1297 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1299 case SHT_SYMTAB
: /* A symbol table */
1300 if (elf_onesymtab (abfd
) == shindex
)
1303 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1304 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1305 elf_onesymtab (abfd
) = shindex
;
1306 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1307 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1308 abfd
->flags
|= HAS_SYMS
;
1310 /* Sometimes a shared object will map in the symbol table. If
1311 SHF_ALLOC is set, and this is a shared object, then we also
1312 treat this section as a BFD section. We can not base the
1313 decision purely on SHF_ALLOC, because that flag is sometimes
1314 set in a relocateable object file, which would confuse the
1316 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1317 && (abfd
->flags
& DYNAMIC
) != 0
1318 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1323 case SHT_DYNSYM
: /* A dynamic symbol table */
1324 if (elf_dynsymtab (abfd
) == shindex
)
1327 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1328 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1329 elf_dynsymtab (abfd
) = shindex
;
1330 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1331 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1332 abfd
->flags
|= HAS_SYMS
;
1334 /* Besides being a symbol table, we also treat this as a regular
1335 section, so that objcopy can handle it. */
1336 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1338 case SHT_STRTAB
: /* A string table */
1339 if (hdr
->bfd_section
!= NULL
)
1341 if (ehdr
->e_shstrndx
== shindex
)
1343 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1344 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1350 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1352 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1353 if (hdr2
->sh_link
== shindex
)
1355 if (! bfd_section_from_shdr (abfd
, i
))
1357 if (elf_onesymtab (abfd
) == i
)
1359 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1360 elf_elfsections (abfd
)[shindex
] =
1361 &elf_tdata (abfd
)->strtab_hdr
;
1364 if (elf_dynsymtab (abfd
) == i
)
1366 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1367 elf_elfsections (abfd
)[shindex
] = hdr
=
1368 &elf_tdata (abfd
)->dynstrtab_hdr
;
1369 /* We also treat this as a regular section, so
1370 that objcopy can handle it. */
1373 #if 0 /* Not handling other string tables specially right now. */
1374 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1375 /* We have a strtab for some random other section. */
1376 newsect
= (asection
*) hdr2
->bfd_section
;
1379 hdr
->bfd_section
= newsect
;
1380 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1382 elf_elfsections (abfd
)[shindex
] = hdr2
;
1388 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1392 /* *These* do a lot of work -- but build no sections! */
1394 asection
*target_sect
;
1395 Elf_Internal_Shdr
*hdr2
;
1397 /* Check for a bogus link to avoid crashing. */
1398 if (hdr
->sh_link
>= ehdr
->e_shnum
)
1400 ((*_bfd_error_handler
)
1401 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1402 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1403 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1406 /* For some incomprehensible reason Oracle distributes
1407 libraries for Solaris in which some of the objects have
1408 bogus sh_link fields. It would be nice if we could just
1409 reject them, but, unfortunately, some people need to use
1410 them. We scan through the section headers; if we find only
1411 one suitable symbol table, we clobber the sh_link to point
1412 to it. I hope this doesn't break anything. */
1413 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1414 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1420 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1422 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1423 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1434 hdr
->sh_link
= found
;
1437 /* Get the symbol table. */
1438 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1439 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1442 /* If this reloc section does not use the main symbol table we
1443 don't treat it as a reloc section. BFD can't adequately
1444 represent such a section, so at least for now, we don't
1445 try. We just present it as a normal section. We also
1446 can't use it as a reloc section if it points to the null
1448 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1449 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1451 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1453 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1454 if (target_sect
== NULL
)
1457 if ((target_sect
->flags
& SEC_RELOC
) == 0
1458 || target_sect
->reloc_count
== 0)
1459 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1463 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1464 amt
= sizeof (*hdr2
);
1465 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
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
;
1537 bfd_size_type amt
= sizeof (*sdata
);
1539 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
1542 sec
->used_by_bfd
= (PTR
) sdata
;
1544 /* Indicate whether or not this section should use RELA relocations. */
1546 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1551 /* Create a new bfd section from an ELF program header.
1553 Since program segments have no names, we generate a synthetic name
1554 of the form segment<NUM>, where NUM is generally the index in the
1555 program header table. For segments that are split (see below) we
1556 generate the names segment<NUM>a and segment<NUM>b.
1558 Note that some program segments may have a file size that is different than
1559 (less than) the memory size. All this means is that at execution the
1560 system must allocate the amount of memory specified by the memory size,
1561 but only initialize it with the first "file size" bytes read from the
1562 file. This would occur for example, with program segments consisting
1563 of combined data+bss.
1565 To handle the above situation, this routine generates TWO bfd sections
1566 for the single program segment. The first has the length specified by
1567 the file size of the segment, and the second has the length specified
1568 by the difference between the two sizes. In effect, the segment is split
1569 into it's initialized and uninitialized parts.
1574 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1576 Elf_Internal_Phdr
*hdr
;
1578 const char *typename
;
1585 split
= ((hdr
->p_memsz
> 0)
1586 && (hdr
->p_filesz
> 0)
1587 && (hdr
->p_memsz
> hdr
->p_filesz
));
1588 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1589 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1592 strcpy (name
, namebuf
);
1593 newsect
= bfd_make_section (abfd
, name
);
1594 if (newsect
== NULL
)
1596 newsect
->vma
= hdr
->p_vaddr
;
1597 newsect
->lma
= hdr
->p_paddr
;
1598 newsect
->_raw_size
= hdr
->p_filesz
;
1599 newsect
->filepos
= hdr
->p_offset
;
1600 newsect
->flags
|= SEC_HAS_CONTENTS
;
1601 if (hdr
->p_type
== PT_LOAD
)
1603 newsect
->flags
|= SEC_ALLOC
;
1604 newsect
->flags
|= SEC_LOAD
;
1605 if (hdr
->p_flags
& PF_X
)
1607 /* FIXME: all we known is that it has execute PERMISSION,
1609 newsect
->flags
|= SEC_CODE
;
1612 if (!(hdr
->p_flags
& PF_W
))
1614 newsect
->flags
|= SEC_READONLY
;
1619 sprintf (namebuf
, "%s%db", typename
, index
);
1620 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1623 strcpy (name
, namebuf
);
1624 newsect
= bfd_make_section (abfd
, name
);
1625 if (newsect
== NULL
)
1627 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1628 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1629 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1630 if (hdr
->p_type
== PT_LOAD
)
1632 newsect
->flags
|= SEC_ALLOC
;
1633 if (hdr
->p_flags
& PF_X
)
1634 newsect
->flags
|= SEC_CODE
;
1636 if (!(hdr
->p_flags
& PF_W
))
1637 newsect
->flags
|= SEC_READONLY
;
1644 bfd_section_from_phdr (abfd
, hdr
, index
)
1646 Elf_Internal_Phdr
*hdr
;
1649 struct elf_backend_data
*bed
;
1651 switch (hdr
->p_type
)
1654 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
1657 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
1660 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
1663 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
1666 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
1668 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
1673 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
1676 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
1679 /* Check for any processor-specific program segment types.
1680 If no handler for them, default to making "segment" sections. */
1681 bed
= get_elf_backend_data (abfd
);
1682 if (bed
->elf_backend_section_from_phdr
)
1683 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
1685 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
1689 /* Initialize REL_HDR, the section-header for new section, containing
1690 relocations against ASECT. If USE_RELA_P is true, we use RELA
1691 relocations; otherwise, we use REL relocations. */
1694 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
1696 Elf_Internal_Shdr
*rel_hdr
;
1701 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1702 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
1704 name
= bfd_alloc (abfd
, amt
);
1707 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1709 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1711 if (rel_hdr
->sh_name
== (unsigned int) -1)
1713 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1714 rel_hdr
->sh_entsize
= (use_rela_p
1715 ? bed
->s
->sizeof_rela
1716 : bed
->s
->sizeof_rel
);
1717 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
1718 rel_hdr
->sh_flags
= 0;
1719 rel_hdr
->sh_addr
= 0;
1720 rel_hdr
->sh_size
= 0;
1721 rel_hdr
->sh_offset
= 0;
1726 /* Set up an ELF internal section header for a section. */
1729 elf_fake_sections (abfd
, asect
, failedptrarg
)
1734 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1735 boolean
*failedptr
= (boolean
*) failedptrarg
;
1736 Elf_Internal_Shdr
*this_hdr
;
1740 /* We already failed; just get out of the bfd_map_over_sections
1745 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1747 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1750 if (this_hdr
->sh_name
== (unsigned long) -1)
1756 this_hdr
->sh_flags
= 0;
1758 if ((asect
->flags
& SEC_ALLOC
) != 0
1759 || asect
->user_set_vma
)
1760 this_hdr
->sh_addr
= asect
->vma
;
1762 this_hdr
->sh_addr
= 0;
1764 this_hdr
->sh_offset
= 0;
1765 this_hdr
->sh_size
= asect
->_raw_size
;
1766 this_hdr
->sh_link
= 0;
1767 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1768 /* The sh_entsize and sh_info fields may have been set already by
1769 copy_private_section_data. */
1771 this_hdr
->bfd_section
= asect
;
1772 this_hdr
->contents
= NULL
;
1774 /* FIXME: This should not be based on section names. */
1775 if (strcmp (asect
->name
, ".dynstr") == 0)
1776 this_hdr
->sh_type
= SHT_STRTAB
;
1777 else if (strcmp (asect
->name
, ".hash") == 0)
1779 this_hdr
->sh_type
= SHT_HASH
;
1780 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
1782 else if (strcmp (asect
->name
, ".dynsym") == 0)
1784 this_hdr
->sh_type
= SHT_DYNSYM
;
1785 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1787 else if (strcmp (asect
->name
, ".dynamic") == 0)
1789 this_hdr
->sh_type
= SHT_DYNAMIC
;
1790 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1792 else if (strncmp (asect
->name
, ".rela", 5) == 0
1793 && get_elf_backend_data (abfd
)->may_use_rela_p
)
1795 this_hdr
->sh_type
= SHT_RELA
;
1796 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1798 else if (strncmp (asect
->name
, ".rel", 4) == 0
1799 && get_elf_backend_data (abfd
)->may_use_rel_p
)
1801 this_hdr
->sh_type
= SHT_REL
;
1802 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1804 else if (strncmp (asect
->name
, ".note", 5) == 0)
1805 this_hdr
->sh_type
= SHT_NOTE
;
1806 else if (strncmp (asect
->name
, ".stab", 5) == 0
1807 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1808 this_hdr
->sh_type
= SHT_STRTAB
;
1809 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1811 this_hdr
->sh_type
= SHT_GNU_versym
;
1812 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1814 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1816 this_hdr
->sh_type
= SHT_GNU_verdef
;
1817 this_hdr
->sh_entsize
= 0;
1818 /* objcopy or strip will copy over sh_info, but may not set
1819 cverdefs. The linker will set cverdefs, but sh_info will be
1821 if (this_hdr
->sh_info
== 0)
1822 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1824 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1825 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1827 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1829 this_hdr
->sh_type
= SHT_GNU_verneed
;
1830 this_hdr
->sh_entsize
= 0;
1831 /* objcopy or strip will copy over sh_info, but may not set
1832 cverrefs. The linker will set cverrefs, but sh_info will be
1834 if (this_hdr
->sh_info
== 0)
1835 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1837 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1838 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1840 else if ((asect
->flags
& SEC_ALLOC
) != 0
1841 && ((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0))
1842 this_hdr
->sh_type
= SHT_NOBITS
;
1844 this_hdr
->sh_type
= SHT_PROGBITS
;
1846 if ((asect
->flags
& SEC_ALLOC
) != 0)
1847 this_hdr
->sh_flags
|= SHF_ALLOC
;
1848 if ((asect
->flags
& SEC_READONLY
) == 0)
1849 this_hdr
->sh_flags
|= SHF_WRITE
;
1850 if ((asect
->flags
& SEC_CODE
) != 0)
1851 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1852 if ((asect
->flags
& SEC_MERGE
) != 0)
1854 this_hdr
->sh_flags
|= SHF_MERGE
;
1855 this_hdr
->sh_entsize
= asect
->entsize
;
1856 if ((asect
->flags
& SEC_STRINGS
) != 0)
1857 this_hdr
->sh_flags
|= SHF_STRINGS
;
1860 /* Check for processor-specific section types. */
1861 if (bed
->elf_backend_fake_sections
)
1862 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1864 /* If the section has relocs, set up a section header for the
1865 SHT_REL[A] section. If two relocation sections are required for
1866 this section, it is up to the processor-specific back-end to
1867 create the other. */
1868 if ((asect
->flags
& SEC_RELOC
) != 0
1869 && !_bfd_elf_init_reloc_shdr (abfd
,
1870 &elf_section_data (asect
)->rel_hdr
,
1872 elf_section_data (asect
)->use_rela_p
))
1876 /* Assign all ELF section numbers. The dummy first section is handled here
1877 too. The link/info pointers for the standard section types are filled
1878 in here too, while we're at it. */
1881 assign_section_numbers (abfd
)
1884 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1886 unsigned int section_number
;
1887 Elf_Internal_Shdr
**i_shdrp
;
1892 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1894 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1896 d
->this_idx
= section_number
++;
1897 if ((sec
->flags
& SEC_RELOC
) == 0)
1900 d
->rel_idx
= section_number
++;
1903 d
->rel_idx2
= section_number
++;
1908 t
->shstrtab_section
= section_number
++;
1909 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1910 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1912 if (bfd_get_symcount (abfd
) > 0)
1914 t
->symtab_section
= section_number
++;
1915 t
->strtab_section
= section_number
++;
1918 elf_elfheader (abfd
)->e_shnum
= section_number
;
1920 /* Set up the list of section header pointers, in agreement with the
1922 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
1923 i_shdrp
= (Elf_Internal_Shdr
**) bfd_alloc (abfd
, amt
);
1924 if (i_shdrp
== NULL
)
1927 amt
= sizeof (Elf_Internal_Shdr
);
1928 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1929 if (i_shdrp
[0] == NULL
)
1931 bfd_release (abfd
, i_shdrp
);
1934 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1936 elf_elfsections (abfd
) = i_shdrp
;
1938 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1939 if (bfd_get_symcount (abfd
) > 0)
1941 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1942 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1943 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1945 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1947 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1951 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1952 if (d
->rel_idx
!= 0)
1953 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1954 if (d
->rel_idx2
!= 0)
1955 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
1957 /* Fill in the sh_link and sh_info fields while we're at it. */
1959 /* sh_link of a reloc section is the section index of the symbol
1960 table. sh_info is the section index of the section to which
1961 the relocation entries apply. */
1962 if (d
->rel_idx
!= 0)
1964 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1965 d
->rel_hdr
.sh_info
= d
->this_idx
;
1967 if (d
->rel_idx2
!= 0)
1969 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
1970 d
->rel_hdr2
->sh_info
= d
->this_idx
;
1973 switch (d
->this_hdr
.sh_type
)
1977 /* A reloc section which we are treating as a normal BFD
1978 section. sh_link is the section index of the symbol
1979 table. sh_info is the section index of the section to
1980 which the relocation entries apply. We assume that an
1981 allocated reloc section uses the dynamic symbol table.
1982 FIXME: How can we be sure? */
1983 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1985 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1987 /* We look up the section the relocs apply to by name. */
1989 if (d
->this_hdr
.sh_type
== SHT_REL
)
1993 s
= bfd_get_section_by_name (abfd
, name
);
1995 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1999 /* We assume that a section named .stab*str is a stabs
2000 string section. We look for a section with the same name
2001 but without the trailing ``str'', and set its sh_link
2002 field to point to this section. */
2003 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2004 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2009 len
= strlen (sec
->name
);
2010 alc
= (char *) bfd_malloc ((bfd_size_type
) len
- 2);
2013 strncpy (alc
, sec
->name
, len
- 3);
2014 alc
[len
- 3] = '\0';
2015 s
= bfd_get_section_by_name (abfd
, alc
);
2019 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2021 /* This is a .stab section. */
2022 elf_section_data (s
)->this_hdr
.sh_entsize
=
2023 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2030 case SHT_GNU_verneed
:
2031 case SHT_GNU_verdef
:
2032 /* sh_link is the section header index of the string table
2033 used for the dynamic entries, or the symbol table, or the
2035 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2037 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2041 case SHT_GNU_versym
:
2042 /* sh_link is the section header index of the symbol table
2043 this hash table or version table is for. */
2044 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2046 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2054 /* Map symbol from it's internal number to the external number, moving
2055 all local symbols to be at the head of the list. */
2058 sym_is_global (abfd
, sym
)
2062 /* If the backend has a special mapping, use it. */
2063 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2064 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2067 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2068 || bfd_is_und_section (bfd_get_section (sym
))
2069 || bfd_is_com_section (bfd_get_section (sym
)));
2073 elf_map_symbols (abfd
)
2076 unsigned int symcount
= bfd_get_symcount (abfd
);
2077 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2078 asymbol
**sect_syms
;
2079 unsigned int num_locals
= 0;
2080 unsigned int num_globals
= 0;
2081 unsigned int num_locals2
= 0;
2082 unsigned int num_globals2
= 0;
2084 unsigned int num_sections
= 0;
2091 fprintf (stderr
, "elf_map_symbols\n");
2095 /* Add a section symbol for each BFD section. FIXME: Is this really
2097 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2099 if (max_index
< asect
->index
)
2100 max_index
= asect
->index
;
2104 amt
= max_index
* sizeof (asymbol
*);
2105 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2106 if (sect_syms
== NULL
)
2108 elf_section_syms (abfd
) = sect_syms
;
2109 elf_num_section_syms (abfd
) = max_index
;
2111 for (idx
= 0; idx
< symcount
; idx
++)
2113 asymbol
*sym
= syms
[idx
];
2115 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2122 if (sec
->owner
!= NULL
)
2124 if (sec
->owner
!= abfd
)
2126 if (sec
->output_offset
!= 0)
2129 sec
= sec
->output_section
;
2131 /* Empty sections in the input files may have had a section
2132 symbol created for them. (See the comment near the end of
2133 _bfd_generic_link_output_symbols in linker.c). If the linker
2134 script discards such sections then we will reach this point.
2135 Since we know that we cannot avoid this case, we detect it
2136 and skip the abort and the assignment to the sect_syms array.
2137 To reproduce this particular case try running the linker
2138 testsuite test ld-scripts/weak.exp for an ELF port that uses
2139 the generic linker. */
2140 if (sec
->owner
== NULL
)
2143 BFD_ASSERT (sec
->owner
== abfd
);
2145 sect_syms
[sec
->index
] = syms
[idx
];
2150 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2154 if (sect_syms
[asect
->index
] != NULL
)
2157 sym
= bfd_make_empty_symbol (abfd
);
2160 sym
->the_bfd
= abfd
;
2161 sym
->name
= asect
->name
;
2163 /* Set the flags to 0 to indicate that this one was newly added. */
2165 sym
->section
= asect
;
2166 sect_syms
[asect
->index
] = sym
;
2170 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2171 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
2175 /* Classify all of the symbols. */
2176 for (idx
= 0; idx
< symcount
; idx
++)
2178 if (!sym_is_global (abfd
, syms
[idx
]))
2183 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2185 if (sect_syms
[asect
->index
] != NULL
2186 && sect_syms
[asect
->index
]->flags
== 0)
2188 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
2189 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
2193 sect_syms
[asect
->index
]->flags
= 0;
2197 /* Now sort the symbols so the local symbols are first. */
2198 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2199 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2201 if (new_syms
== NULL
)
2204 for (idx
= 0; idx
< symcount
; idx
++)
2206 asymbol
*sym
= syms
[idx
];
2209 if (!sym_is_global (abfd
, sym
))
2212 i
= num_locals
+ num_globals2
++;
2214 sym
->udata
.i
= i
+ 1;
2216 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2218 if (sect_syms
[asect
->index
] != NULL
2219 && sect_syms
[asect
->index
]->flags
== 0)
2221 asymbol
*sym
= sect_syms
[asect
->index
];
2224 sym
->flags
= BSF_SECTION_SYM
;
2225 if (!sym_is_global (abfd
, sym
))
2228 i
= num_locals
+ num_globals2
++;
2230 sym
->udata
.i
= i
+ 1;
2234 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2236 elf_num_locals (abfd
) = num_locals
;
2237 elf_num_globals (abfd
) = num_globals
;
2241 /* Align to the maximum file alignment that could be required for any
2242 ELF data structure. */
2244 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2245 static INLINE file_ptr
2246 align_file_position (off
, align
)
2250 return (off
+ align
- 1) & ~(align
- 1);
2253 /* Assign a file position to a section, optionally aligning to the
2254 required section alignment. */
2257 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2258 Elf_Internal_Shdr
*i_shdrp
;
2266 al
= i_shdrp
->sh_addralign
;
2268 offset
= BFD_ALIGN (offset
, al
);
2270 i_shdrp
->sh_offset
= offset
;
2271 if (i_shdrp
->bfd_section
!= NULL
)
2272 i_shdrp
->bfd_section
->filepos
= offset
;
2273 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2274 offset
+= i_shdrp
->sh_size
;
2278 /* Compute the file positions we are going to put the sections at, and
2279 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2280 is not NULL, this is being called by the ELF backend linker. */
2283 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2285 struct bfd_link_info
*link_info
;
2287 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2289 struct bfd_strtab_hash
*strtab
;
2290 Elf_Internal_Shdr
*shstrtab_hdr
;
2292 if (abfd
->output_has_begun
)
2295 /* Do any elf backend specific processing first. */
2296 if (bed
->elf_backend_begin_write_processing
)
2297 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2299 if (! prep_headers (abfd
))
2302 /* Post process the headers if necessary. */
2303 if (bed
->elf_backend_post_process_headers
)
2304 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2307 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2311 if (!assign_section_numbers (abfd
))
2314 /* The backend linker builds symbol table information itself. */
2315 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2317 /* Non-zero if doing a relocatable link. */
2318 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2320 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2324 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2325 /* sh_name was set in prep_headers. */
2326 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2327 shstrtab_hdr
->sh_flags
= 0;
2328 shstrtab_hdr
->sh_addr
= 0;
2329 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2330 shstrtab_hdr
->sh_entsize
= 0;
2331 shstrtab_hdr
->sh_link
= 0;
2332 shstrtab_hdr
->sh_info
= 0;
2333 /* sh_offset is set in assign_file_positions_except_relocs. */
2334 shstrtab_hdr
->sh_addralign
= 1;
2336 if (!assign_file_positions_except_relocs (abfd
))
2339 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2342 Elf_Internal_Shdr
*hdr
;
2344 off
= elf_tdata (abfd
)->next_file_pos
;
2346 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2347 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2349 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2350 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2352 elf_tdata (abfd
)->next_file_pos
= off
;
2354 /* Now that we know where the .strtab section goes, write it
2356 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2357 || ! _bfd_stringtab_emit (abfd
, strtab
))
2359 _bfd_stringtab_free (strtab
);
2362 abfd
->output_has_begun
= true;
2367 /* Create a mapping from a set of sections to a program segment. */
2369 static INLINE
struct elf_segment_map
*
2370 make_mapping (abfd
, sections
, from
, to
, phdr
)
2372 asection
**sections
;
2377 struct elf_segment_map
*m
;
2382 amt
= sizeof (struct elf_segment_map
);
2383 amt
+= (to
- from
- 1) * sizeof (asection
*);
2384 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2388 m
->p_type
= PT_LOAD
;
2389 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2390 m
->sections
[i
- from
] = *hdrpp
;
2391 m
->count
= to
- from
;
2393 if (from
== 0 && phdr
)
2395 /* Include the headers in the first PT_LOAD segment. */
2396 m
->includes_filehdr
= 1;
2397 m
->includes_phdrs
= 1;
2403 /* Set up a mapping from BFD sections to program segments. */
2406 map_sections_to_segments (abfd
)
2409 asection
**sections
= NULL
;
2413 struct elf_segment_map
*mfirst
;
2414 struct elf_segment_map
**pm
;
2415 struct elf_segment_map
*m
;
2417 unsigned int phdr_index
;
2418 bfd_vma maxpagesize
;
2420 boolean phdr_in_segment
= true;
2425 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2428 if (bfd_count_sections (abfd
) == 0)
2431 /* Select the allocated sections, and sort them. */
2433 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
2434 sections
= (asection
**) bfd_malloc (amt
);
2435 if (sections
== NULL
)
2439 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2441 if ((s
->flags
& SEC_ALLOC
) != 0)
2447 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2450 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2452 /* Build the mapping. */
2457 /* If we have a .interp section, then create a PT_PHDR segment for
2458 the program headers and a PT_INTERP segment for the .interp
2460 s
= bfd_get_section_by_name (abfd
, ".interp");
2461 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2463 amt
= sizeof (struct elf_segment_map
);
2464 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2468 m
->p_type
= PT_PHDR
;
2469 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2470 m
->p_flags
= PF_R
| PF_X
;
2471 m
->p_flags_valid
= 1;
2472 m
->includes_phdrs
= 1;
2477 amt
= sizeof (struct elf_segment_map
);
2478 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2482 m
->p_type
= PT_INTERP
;
2490 /* Look through the sections. We put sections in the same program
2491 segment when the start of the second section can be placed within
2492 a few bytes of the end of the first section. */
2495 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2497 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2499 && (dynsec
->flags
& SEC_LOAD
) == 0)
2502 /* Deal with -Ttext or something similar such that the first section
2503 is not adjacent to the program headers. This is an
2504 approximation, since at this point we don't know exactly how many
2505 program headers we will need. */
2508 bfd_size_type phdr_size
;
2510 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2512 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2513 if ((abfd
->flags
& D_PAGED
) == 0
2514 || sections
[0]->lma
< phdr_size
2515 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2516 phdr_in_segment
= false;
2519 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2522 boolean new_segment
;
2526 /* See if this section and the last one will fit in the same
2529 if (last_hdr
== NULL
)
2531 /* If we don't have a segment yet, then we don't need a new
2532 one (we build the last one after this loop). */
2533 new_segment
= false;
2535 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2537 /* If this section has a different relation between the
2538 virtual address and the load address, then we need a new
2542 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2543 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2545 /* If putting this section in this segment would force us to
2546 skip a page in the segment, then we need a new segment. */
2549 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2550 && (hdr
->flags
& SEC_LOAD
) != 0)
2552 /* We don't want to put a loadable section after a
2553 nonloadable section in the same segment. */
2556 else if ((abfd
->flags
& D_PAGED
) == 0)
2558 /* If the file is not demand paged, which means that we
2559 don't require the sections to be correctly aligned in the
2560 file, then there is no other reason for a new segment. */
2561 new_segment
= false;
2564 && (hdr
->flags
& SEC_READONLY
) == 0
2565 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2568 /* We don't want to put a writable section in a read only
2569 segment, unless they are on the same page in memory
2570 anyhow. We already know that the last section does not
2571 bring us past the current section on the page, so the
2572 only case in which the new section is not on the same
2573 page as the previous section is when the previous section
2574 ends precisely on a page boundary. */
2579 /* Otherwise, we can use the same segment. */
2580 new_segment
= false;
2585 if ((hdr
->flags
& SEC_READONLY
) == 0)
2591 /* We need a new program segment. We must create a new program
2592 header holding all the sections from phdr_index until hdr. */
2594 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2601 if ((hdr
->flags
& SEC_READONLY
) == 0)
2608 phdr_in_segment
= false;
2611 /* Create a final PT_LOAD program segment. */
2612 if (last_hdr
!= NULL
)
2614 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2622 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2625 amt
= sizeof (struct elf_segment_map
);
2626 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2630 m
->p_type
= PT_DYNAMIC
;
2632 m
->sections
[0] = dynsec
;
2638 /* For each loadable .note section, add a PT_NOTE segment. We don't
2639 use bfd_get_section_by_name, because if we link together
2640 nonloadable .note sections and loadable .note sections, we will
2641 generate two .note sections in the output file. FIXME: Using
2642 names for section types is bogus anyhow. */
2643 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2645 if ((s
->flags
& SEC_LOAD
) != 0
2646 && strncmp (s
->name
, ".note", 5) == 0)
2648 amt
= sizeof (struct elf_segment_map
);
2649 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2653 m
->p_type
= PT_NOTE
;
2665 elf_tdata (abfd
)->segment_map
= mfirst
;
2669 if (sections
!= NULL
)
2674 /* Sort sections by address. */
2677 elf_sort_sections (arg1
, arg2
)
2681 const asection
*sec1
= *(const asection
**) arg1
;
2682 const asection
*sec2
= *(const asection
**) arg2
;
2684 /* Sort by LMA first, since this is the address used to
2685 place the section into a segment. */
2686 if (sec1
->lma
< sec2
->lma
)
2688 else if (sec1
->lma
> sec2
->lma
)
2691 /* Then sort by VMA. Normally the LMA and the VMA will be
2692 the same, and this will do nothing. */
2693 if (sec1
->vma
< sec2
->vma
)
2695 else if (sec1
->vma
> sec2
->vma
)
2698 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2700 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2706 /* If the indicies are the same, do not return 0
2707 here, but continue to try the next comparison. */
2708 if (sec1
->target_index
- sec2
->target_index
!= 0)
2709 return sec1
->target_index
- sec2
->target_index
;
2714 else if (TOEND (sec2
))
2719 /* Sort by size, to put zero sized sections
2720 before others at the same address. */
2722 if (sec1
->_raw_size
< sec2
->_raw_size
)
2724 if (sec1
->_raw_size
> sec2
->_raw_size
)
2727 return sec1
->target_index
- sec2
->target_index
;
2730 /* Assign file positions to the sections based on the mapping from
2731 sections to segments. This function also sets up some fields in
2732 the file header, and writes out the program headers. */
2735 assign_file_positions_for_segments (abfd
)
2738 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2740 struct elf_segment_map
*m
;
2742 Elf_Internal_Phdr
*phdrs
;
2744 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2745 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2746 Elf_Internal_Phdr
*p
;
2749 if (elf_tdata (abfd
)->segment_map
== NULL
)
2751 if (! map_sections_to_segments (abfd
))
2755 if (bed
->elf_backend_modify_segment_map
)
2757 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2762 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2765 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2766 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2767 elf_elfheader (abfd
)->e_phnum
= count
;
2772 /* If we already counted the number of program segments, make sure
2773 that we allocated enough space. This happens when SIZEOF_HEADERS
2774 is used in a linker script. */
2775 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2776 if (alloc
!= 0 && count
> alloc
)
2778 ((*_bfd_error_handler
)
2779 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2780 bfd_get_filename (abfd
), alloc
, count
));
2781 bfd_set_error (bfd_error_bad_value
);
2788 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
2789 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
2793 off
= bed
->s
->sizeof_ehdr
;
2794 off
+= alloc
* bed
->s
->sizeof_phdr
;
2801 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2808 /* If elf_segment_map is not from map_sections_to_segments, the
2809 sections may not be correctly ordered. */
2811 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2814 p
->p_type
= m
->p_type
;
2815 p
->p_flags
= m
->p_flags
;
2817 if (p
->p_type
== PT_LOAD
2819 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2821 if ((abfd
->flags
& D_PAGED
) != 0)
2822 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2825 bfd_size_type align
;
2828 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2830 bfd_size_type secalign
;
2832 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
2833 if (secalign
> align
)
2837 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
2844 p
->p_vaddr
= m
->sections
[0]->vma
;
2846 if (m
->p_paddr_valid
)
2847 p
->p_paddr
= m
->p_paddr
;
2848 else if (m
->count
== 0)
2851 p
->p_paddr
= m
->sections
[0]->lma
;
2853 if (p
->p_type
== PT_LOAD
2854 && (abfd
->flags
& D_PAGED
) != 0)
2855 p
->p_align
= bed
->maxpagesize
;
2856 else if (m
->count
== 0)
2857 p
->p_align
= bed
->s
->file_align
;
2865 if (m
->includes_filehdr
)
2867 if (! m
->p_flags_valid
)
2870 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2871 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2874 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2876 if (p
->p_vaddr
< (bfd_vma
) off
)
2878 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2879 bfd_get_filename (abfd
));
2880 bfd_set_error (bfd_error_bad_value
);
2885 if (! m
->p_paddr_valid
)
2888 if (p
->p_type
== PT_LOAD
)
2890 filehdr_vaddr
= p
->p_vaddr
;
2891 filehdr_paddr
= p
->p_paddr
;
2895 if (m
->includes_phdrs
)
2897 if (! m
->p_flags_valid
)
2900 if (m
->includes_filehdr
)
2902 if (p
->p_type
== PT_LOAD
)
2904 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2905 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2910 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2914 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2915 p
->p_vaddr
-= off
- p
->p_offset
;
2916 if (! m
->p_paddr_valid
)
2917 p
->p_paddr
-= off
- p
->p_offset
;
2920 if (p
->p_type
== PT_LOAD
)
2922 phdrs_vaddr
= p
->p_vaddr
;
2923 phdrs_paddr
= p
->p_paddr
;
2926 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
2929 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2930 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2933 if (p
->p_type
== PT_LOAD
2934 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
2936 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2942 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2943 p
->p_filesz
+= adjust
;
2944 p
->p_memsz
+= adjust
;
2950 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2954 bfd_size_type align
;
2958 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2960 /* The section may have artificial alignment forced by a
2961 link script. Notice this case by the gap between the
2962 cumulative phdr vma and the section's vma. */
2963 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2965 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2967 p
->p_memsz
+= adjust
;
2970 if ((flags
& SEC_LOAD
) != 0)
2971 p
->p_filesz
+= adjust
;
2974 if (p
->p_type
== PT_LOAD
)
2976 bfd_signed_vma adjust
;
2978 if ((flags
& SEC_LOAD
) != 0)
2980 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2984 else if ((flags
& SEC_ALLOC
) != 0)
2986 /* The section VMA must equal the file position
2987 modulo the page size. FIXME: I'm not sure if
2988 this adjustment is really necessary. We used to
2989 not have the SEC_LOAD case just above, and then
2990 this was necessary, but now I'm not sure. */
2991 if ((abfd
->flags
& D_PAGED
) != 0)
2992 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2994 adjust
= (sec
->vma
- voff
) % align
;
3003 (* _bfd_error_handler
)
3004 (_("Error: First section in segment (%s) starts at 0x%x"),
3005 bfd_section_name (abfd
, sec
), sec
->lma
);
3006 (* _bfd_error_handler
)
3007 (_(" whereas segment starts at 0x%x"),
3012 p
->p_memsz
+= adjust
;
3015 if ((flags
& SEC_LOAD
) != 0)
3016 p
->p_filesz
+= adjust
;
3021 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3022 used in a linker script we may have a section with
3023 SEC_LOAD clear but which is supposed to have
3025 if ((flags
& SEC_LOAD
) != 0
3026 || (flags
& SEC_HAS_CONTENTS
) != 0)
3027 off
+= sec
->_raw_size
;
3029 if ((flags
& SEC_ALLOC
) != 0)
3030 voff
+= sec
->_raw_size
;
3033 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3035 /* The actual "note" segment has i == 0.
3036 This is the one that actually contains everything. */
3040 p
->p_filesz
= sec
->_raw_size
;
3041 off
+= sec
->_raw_size
;
3046 /* Fake sections -- don't need to be written. */
3049 flags
= sec
->flags
= 0;
3056 p
->p_memsz
+= sec
->_raw_size
;
3058 if ((flags
& SEC_LOAD
) != 0)
3059 p
->p_filesz
+= sec
->_raw_size
;
3061 if (align
> p
->p_align
3062 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3066 if (! m
->p_flags_valid
)
3069 if ((flags
& SEC_CODE
) != 0)
3071 if ((flags
& SEC_READONLY
) == 0)
3077 /* Now that we have set the section file positions, we can set up
3078 the file positions for the non PT_LOAD segments. */
3079 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3083 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3085 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3086 p
->p_offset
= m
->sections
[0]->filepos
;
3090 if (m
->includes_filehdr
)
3092 p
->p_vaddr
= filehdr_vaddr
;
3093 if (! m
->p_paddr_valid
)
3094 p
->p_paddr
= filehdr_paddr
;
3096 else if (m
->includes_phdrs
)
3098 p
->p_vaddr
= phdrs_vaddr
;
3099 if (! m
->p_paddr_valid
)
3100 p
->p_paddr
= phdrs_paddr
;
3105 /* Clear out any program headers we allocated but did not use. */
3106 for (; count
< alloc
; count
++, p
++)
3108 memset (p
, 0, sizeof *p
);
3109 p
->p_type
= PT_NULL
;
3112 elf_tdata (abfd
)->phdr
= phdrs
;
3114 elf_tdata (abfd
)->next_file_pos
= off
;
3116 /* Write out the program headers. */
3117 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3118 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3124 /* Get the size of the program header.
3126 If this is called by the linker before any of the section VMA's are set, it
3127 can't calculate the correct value for a strange memory layout. This only
3128 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3129 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3130 data segment (exclusive of .interp and .dynamic).
3132 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3133 will be two segments. */
3135 static bfd_size_type
3136 get_program_header_size (abfd
)
3141 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3143 /* We can't return a different result each time we're called. */
3144 if (elf_tdata (abfd
)->program_header_size
!= 0)
3145 return elf_tdata (abfd
)->program_header_size
;
3147 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3149 struct elf_segment_map
*m
;
3152 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3154 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3155 return elf_tdata (abfd
)->program_header_size
;
3158 /* Assume we will need exactly two PT_LOAD segments: one for text
3159 and one for data. */
3162 s
= bfd_get_section_by_name (abfd
, ".interp");
3163 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3165 /* If we have a loadable interpreter section, we need a
3166 PT_INTERP segment. In this case, assume we also need a
3167 PT_PHDR segment, although that may not be true for all
3172 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3174 /* We need a PT_DYNAMIC segment. */
3178 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3180 if ((s
->flags
& SEC_LOAD
) != 0
3181 && strncmp (s
->name
, ".note", 5) == 0)
3183 /* We need a PT_NOTE segment. */
3188 /* Let the backend count up any program headers it might need. */
3189 if (bed
->elf_backend_additional_program_headers
)
3193 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3199 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3200 return elf_tdata (abfd
)->program_header_size
;
3203 /* Work out the file positions of all the sections. This is called by
3204 _bfd_elf_compute_section_file_positions. All the section sizes and
3205 VMAs must be known before this is called.
3207 We do not consider reloc sections at this point, unless they form
3208 part of the loadable image. Reloc sections are assigned file
3209 positions in assign_file_positions_for_relocs, which is called by
3210 write_object_contents and final_link.
3212 We also don't set the positions of the .symtab and .strtab here. */
3215 assign_file_positions_except_relocs (abfd
)
3218 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3219 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3220 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3222 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3224 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3225 && bfd_get_format (abfd
) != bfd_core
)
3227 Elf_Internal_Shdr
**hdrpp
;
3230 /* Start after the ELF header. */
3231 off
= i_ehdrp
->e_ehsize
;
3233 /* We are not creating an executable, which means that we are
3234 not creating a program header, and that the actual order of
3235 the sections in the file is unimportant. */
3236 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3238 Elf_Internal_Shdr
*hdr
;
3241 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
3243 hdr
->sh_offset
= -1;
3246 if (i
== tdata
->symtab_section
3247 || i
== tdata
->strtab_section
)
3249 hdr
->sh_offset
= -1;
3253 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3259 Elf_Internal_Shdr
**hdrpp
;
3261 /* Assign file positions for the loaded sections based on the
3262 assignment of sections to segments. */
3263 if (! assign_file_positions_for_segments (abfd
))
3266 /* Assign file positions for the other sections. */
3268 off
= elf_tdata (abfd
)->next_file_pos
;
3269 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3271 Elf_Internal_Shdr
*hdr
;
3274 if (hdr
->bfd_section
!= NULL
3275 && hdr
->bfd_section
->filepos
!= 0)
3276 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3277 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3279 ((*_bfd_error_handler
)
3280 (_("%s: warning: allocated section `%s' not in segment"),
3281 bfd_get_filename (abfd
),
3282 (hdr
->bfd_section
== NULL
3284 : hdr
->bfd_section
->name
)));
3285 if ((abfd
->flags
& D_PAGED
) != 0)
3286 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3288 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3289 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3292 else if (hdr
->sh_type
== SHT_REL
3293 || hdr
->sh_type
== SHT_RELA
3294 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3295 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3296 hdr
->sh_offset
= -1;
3298 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3302 /* Place the section headers. */
3303 off
= align_file_position (off
, bed
->s
->file_align
);
3304 i_ehdrp
->e_shoff
= off
;
3305 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3307 elf_tdata (abfd
)->next_file_pos
= off
;
3316 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3317 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3318 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3320 struct bfd_strtab_hash
*shstrtab
;
3321 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3323 i_ehdrp
= elf_elfheader (abfd
);
3324 i_shdrp
= elf_elfsections (abfd
);
3326 shstrtab
= _bfd_elf_stringtab_init ();
3327 if (shstrtab
== NULL
)
3330 elf_shstrtab (abfd
) = shstrtab
;
3332 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3333 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3334 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3335 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3337 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3338 i_ehdrp
->e_ident
[EI_DATA
] =
3339 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3340 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3342 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3343 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3345 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3346 i_ehdrp
->e_ident
[count
] = 0;
3348 if ((abfd
->flags
& DYNAMIC
) != 0)
3349 i_ehdrp
->e_type
= ET_DYN
;
3350 else if ((abfd
->flags
& EXEC_P
) != 0)
3351 i_ehdrp
->e_type
= ET_EXEC
;
3352 else if (bfd_get_format (abfd
) == bfd_core
)
3353 i_ehdrp
->e_type
= ET_CORE
;
3355 i_ehdrp
->e_type
= ET_REL
;
3357 switch (bfd_get_arch (abfd
))
3359 case bfd_arch_unknown
:
3360 i_ehdrp
->e_machine
= EM_NONE
;
3363 /* There used to be a long list of cases here, each one setting
3364 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3365 in the corresponding bfd definition. To avoid duplication,
3366 the switch was removed. Machines that need special handling
3367 can generally do it in elf_backend_final_write_processing(),
3368 unless they need the information earlier than the final write.
3369 Such need can generally be supplied by replacing the tests for
3370 e_machine with the conditions used to determine it. */
3372 if (get_elf_backend_data (abfd
) != NULL
)
3373 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
3375 i_ehdrp
->e_machine
= EM_NONE
;
3378 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3379 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3381 /* No program header, for now. */
3382 i_ehdrp
->e_phoff
= 0;
3383 i_ehdrp
->e_phentsize
= 0;
3384 i_ehdrp
->e_phnum
= 0;
3386 /* Each bfd section is section header entry. */
3387 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3388 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3390 /* If we're building an executable, we'll need a program header table. */
3391 if (abfd
->flags
& EXEC_P
)
3393 /* It all happens later. */
3395 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3397 /* elf_build_phdrs() returns a (NULL-terminated) array of
3398 Elf_Internal_Phdrs. */
3399 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3400 i_ehdrp
->e_phoff
= outbase
;
3401 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3406 i_ehdrp
->e_phentsize
= 0;
3408 i_ehdrp
->e_phoff
= 0;
3411 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3412 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3413 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3414 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3415 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3416 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3417 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3418 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3419 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3425 /* Assign file positions for all the reloc sections which are not part
3426 of the loadable file image. */
3429 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3434 Elf_Internal_Shdr
**shdrpp
;
3436 off
= elf_tdata (abfd
)->next_file_pos
;
3438 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3439 i
< elf_elfheader (abfd
)->e_shnum
;
3442 Elf_Internal_Shdr
*shdrp
;
3445 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3446 && shdrp
->sh_offset
== -1)
3447 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3450 elf_tdata (abfd
)->next_file_pos
= off
;
3454 _bfd_elf_write_object_contents (abfd
)
3457 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3458 Elf_Internal_Ehdr
*i_ehdrp
;
3459 Elf_Internal_Shdr
**i_shdrp
;
3463 if (! abfd
->output_has_begun
3464 && ! _bfd_elf_compute_section_file_positions
3465 (abfd
, (struct bfd_link_info
*) NULL
))
3468 i_shdrp
= elf_elfsections (abfd
);
3469 i_ehdrp
= elf_elfheader (abfd
);
3472 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3476 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3478 /* After writing the headers, we need to write the sections too... */
3479 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3481 if (bed
->elf_backend_section_processing
)
3482 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3483 if (i_shdrp
[count
]->contents
)
3485 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
3487 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3488 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
3493 /* Write out the section header names. */
3494 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3495 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3498 if (bed
->elf_backend_final_write_processing
)
3499 (*bed
->elf_backend_final_write_processing
) (abfd
,
3500 elf_tdata (abfd
)->linker
);
3502 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3506 _bfd_elf_write_corefile_contents (abfd
)
3509 /* Hopefully this can be done just like an object file. */
3510 return _bfd_elf_write_object_contents (abfd
);
3513 /* Given a section, search the header to find them. */
3516 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3520 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3521 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3523 Elf_Internal_Shdr
*hdr
;
3524 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3526 for (index
= 0; index
< maxindex
; index
++)
3528 hdr
= i_shdrp
[index
];
3529 if (hdr
->bfd_section
== asect
)
3533 if (bed
->elf_backend_section_from_bfd_section
)
3535 for (index
= 0; index
< maxindex
; index
++)
3539 hdr
= i_shdrp
[index
];
3541 if ((*bed
->elf_backend_section_from_bfd_section
)
3542 (abfd
, hdr
, asect
, &retval
))
3547 if (bfd_is_abs_section (asect
))
3549 if (bfd_is_com_section (asect
))
3551 if (bfd_is_und_section (asect
))
3554 bfd_set_error (bfd_error_nonrepresentable_section
);
3559 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3563 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3565 asymbol
**asym_ptr_ptr
;
3567 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3569 flagword flags
= asym_ptr
->flags
;
3571 /* When gas creates relocations against local labels, it creates its
3572 own symbol for the section, but does put the symbol into the
3573 symbol chain, so udata is 0. When the linker is generating
3574 relocatable output, this section symbol may be for one of the
3575 input sections rather than the output section. */
3576 if (asym_ptr
->udata
.i
== 0
3577 && (flags
& BSF_SECTION_SYM
)
3578 && asym_ptr
->section
)
3582 if (asym_ptr
->section
->output_section
!= NULL
)
3583 indx
= asym_ptr
->section
->output_section
->index
;
3585 indx
= asym_ptr
->section
->index
;
3586 if (indx
< elf_num_section_syms (abfd
)
3587 && elf_section_syms (abfd
)[indx
] != NULL
)
3588 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3591 idx
= asym_ptr
->udata
.i
;
3595 /* This case can occur when using --strip-symbol on a symbol
3596 which is used in a relocation entry. */
3597 (*_bfd_error_handler
)
3598 (_("%s: symbol `%s' required but not present"),
3599 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3600 bfd_set_error (bfd_error_no_symbols
);
3607 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3608 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3609 elf_symbol_flags (flags
));
3617 /* Copy private BFD data. This copies any program header information. */
3620 copy_private_bfd_data (ibfd
, obfd
)
3624 Elf_Internal_Ehdr
* iehdr
;
3625 struct elf_segment_map
* map
;
3626 struct elf_segment_map
* map_first
;
3627 struct elf_segment_map
** pointer_to_map
;
3628 Elf_Internal_Phdr
* segment
;
3631 unsigned int num_segments
;
3632 boolean phdr_included
= false;
3633 bfd_vma maxpagesize
;
3634 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
3635 unsigned int phdr_adjust_num
= 0;
3637 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3638 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3641 if (elf_tdata (ibfd
)->phdr
== NULL
)
3644 iehdr
= elf_elfheader (ibfd
);
3647 pointer_to_map
= &map_first
;
3649 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3650 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3652 /* Returns the end address of the segment + 1. */
3653 #define SEGMENT_END(segment, start) \
3654 (start + (segment->p_memsz > segment->p_filesz \
3655 ? segment->p_memsz : segment->p_filesz))
3657 /* Returns true if the given section is contained within
3658 the given segment. VMA addresses are compared. */
3659 #define IS_CONTAINED_BY_VMA(section, segment) \
3660 (section->vma >= segment->p_vaddr \
3661 && (section->vma + section->_raw_size) \
3662 <= (SEGMENT_END (segment, segment->p_vaddr)))
3664 /* Returns true if the given section is contained within
3665 the given segment. LMA addresses are compared. */
3666 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3667 (section->lma >= base \
3668 && (section->lma + section->_raw_size) \
3669 <= SEGMENT_END (segment, base))
3671 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3672 #define IS_COREFILE_NOTE(p, s) \
3673 (p->p_type == PT_NOTE \
3674 && bfd_get_format (ibfd) == bfd_core \
3675 && s->vma == 0 && s->lma == 0 \
3676 && (bfd_vma) s->filepos >= p->p_offset \
3677 && (bfd_vma) s->filepos + s->_raw_size \
3678 <= p->p_offset + p->p_filesz)
3680 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3681 linker, which generates a PT_INTERP section with p_vaddr and
3682 p_memsz set to 0. */
3683 #define IS_SOLARIS_PT_INTERP(p, s) \
3685 && p->p_filesz > 0 \
3686 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3687 && s->_raw_size > 0 \
3688 && (bfd_vma) s->filepos >= p->p_offset \
3689 && ((bfd_vma) s->filepos + s->_raw_size \
3690 <= p->p_offset + p->p_filesz))
3692 /* Decide if the given section should be included in the given segment.
3693 A section will be included if:
3694 1. It is within the address space of the segment,
3695 2. It is an allocated segment,
3696 3. There is an output section associated with it,
3697 4. The section has not already been allocated to a previous segment. */
3698 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3699 ((((IS_CONTAINED_BY_VMA (section, segment) \
3700 || IS_SOLARIS_PT_INTERP (segment, section)) \
3701 && (section->flags & SEC_ALLOC) != 0) \
3702 || IS_COREFILE_NOTE (segment, section)) \
3703 && section->output_section != NULL \
3704 && section->segment_mark == false)
3706 /* Returns true iff seg1 starts after the end of seg2. */
3707 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3708 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3710 /* Returns true iff seg1 and seg2 overlap. */
3711 #define SEGMENT_OVERLAPS(seg1, seg2) \
3712 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3714 /* Initialise the segment mark field. */
3715 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3716 section
->segment_mark
= false;
3718 /* Scan through the segments specified in the program header
3719 of the input BFD. For this first scan we look for overlaps
3720 in the loadable segments. These can be created by wierd
3721 parameters to objcopy. */
3722 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3727 Elf_Internal_Phdr
*segment2
;
3729 if (segment
->p_type
!= PT_LOAD
)
3732 /* Determine if this segment overlaps any previous segments. */
3733 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
3735 bfd_signed_vma extra_length
;
3737 if (segment2
->p_type
!= PT_LOAD
3738 || ! SEGMENT_OVERLAPS (segment
, segment2
))
3741 /* Merge the two segments together. */
3742 if (segment2
->p_vaddr
< segment
->p_vaddr
)
3744 /* Extend SEGMENT2 to include SEGMENT and then delete
3747 SEGMENT_END (segment
, segment
->p_vaddr
)
3748 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
3750 if (extra_length
> 0)
3752 segment2
->p_memsz
+= extra_length
;
3753 segment2
->p_filesz
+= extra_length
;
3756 segment
->p_type
= PT_NULL
;
3758 /* Since we have deleted P we must restart the outer loop. */
3760 segment
= elf_tdata (ibfd
)->phdr
;
3765 /* Extend SEGMENT to include SEGMENT2 and then delete
3768 SEGMENT_END (segment2
, segment2
->p_vaddr
)
3769 - SEGMENT_END (segment
, segment
->p_vaddr
);
3771 if (extra_length
> 0)
3773 segment
->p_memsz
+= extra_length
;
3774 segment
->p_filesz
+= extra_length
;
3777 segment2
->p_type
= PT_NULL
;
3782 /* The second scan attempts to assign sections to segments. */
3783 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3787 unsigned int section_count
;
3788 asection
** sections
;
3789 asection
* output_section
;
3791 bfd_vma matching_lma
;
3792 bfd_vma suggested_lma
;
3796 if (segment
->p_type
== PT_NULL
)
3799 /* Compute how many sections might be placed into this segment. */
3801 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3802 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3805 /* Allocate a segment map big enough to contain all of the
3806 sections we have selected. */
3807 amt
= sizeof (struct elf_segment_map
);
3808 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
3809 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
3813 /* Initialise the fields of the segment map. Default to
3814 using the physical address of the segment in the input BFD. */
3816 map
->p_type
= segment
->p_type
;
3817 map
->p_flags
= segment
->p_flags
;
3818 map
->p_flags_valid
= 1;
3819 map
->p_paddr
= segment
->p_paddr
;
3820 map
->p_paddr_valid
= 1;
3822 /* Determine if this segment contains the ELF file header
3823 and if it contains the program headers themselves. */
3824 map
->includes_filehdr
= (segment
->p_offset
== 0
3825 && segment
->p_filesz
>= iehdr
->e_ehsize
);
3827 map
->includes_phdrs
= 0;
3829 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
3831 map
->includes_phdrs
=
3832 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3833 && (segment
->p_offset
+ segment
->p_filesz
3834 >= ((bfd_vma
) iehdr
->e_phoff
3835 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3837 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
3838 phdr_included
= true;
3841 if (section_count
== 0)
3843 /* Special segments, such as the PT_PHDR segment, may contain
3844 no sections, but ordinary, loadable segments should contain
3846 if (segment
->p_type
== PT_LOAD
)
3848 (_("%s: warning: Empty loadable segment detected\n"),
3849 bfd_archive_filename (ibfd
));
3852 *pointer_to_map
= map
;
3853 pointer_to_map
= &map
->next
;
3858 /* Now scan the sections in the input BFD again and attempt
3859 to add their corresponding output sections to the segment map.
3860 The problem here is how to handle an output section which has
3861 been moved (ie had its LMA changed). There are four possibilities:
3863 1. None of the sections have been moved.
3864 In this case we can continue to use the segment LMA from the
3867 2. All of the sections have been moved by the same amount.
3868 In this case we can change the segment's LMA to match the LMA
3869 of the first section.
3871 3. Some of the sections have been moved, others have not.
3872 In this case those sections which have not been moved can be
3873 placed in the current segment which will have to have its size,
3874 and possibly its LMA changed, and a new segment or segments will
3875 have to be created to contain the other sections.
3877 4. The sections have been moved, but not be the same amount.
3878 In this case we can change the segment's LMA to match the LMA
3879 of the first section and we will have to create a new segment
3880 or segments to contain the other sections.
3882 In order to save time, we allocate an array to hold the section
3883 pointers that we are interested in. As these sections get assigned
3884 to a segment, they are removed from this array. */
3886 amt
= (bfd_size_type
) section_count
* sizeof (asection
*);
3887 sections
= (asection
**) bfd_malloc (amt
);
3888 if (sections
== NULL
)
3891 /* Step One: Scan for segment vs section LMA conflicts.
3892 Also add the sections to the section array allocated above.
3893 Also add the sections to the current segment. In the common
3894 case, where the sections have not been moved, this means that
3895 we have completely filled the segment, and there is nothing
3901 for (j
= 0, section
= ibfd
->sections
;
3903 section
= section
->next
)
3905 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3907 output_section
= section
->output_section
;
3909 sections
[j
++] = section
;
3911 /* The Solaris native linker always sets p_paddr to 0.
3912 We try to catch that case here, and set it to the
3914 if (segment
->p_paddr
== 0
3915 && segment
->p_vaddr
!= 0
3917 && output_section
->lma
!= 0
3918 && (output_section
->vma
== (segment
->p_vaddr
3919 + (map
->includes_filehdr
3922 + (map
->includes_phdrs
3923 ? iehdr
->e_phnum
* iehdr
->e_phentsize
3925 map
->p_paddr
= segment
->p_vaddr
;
3927 /* Match up the physical address of the segment with the
3928 LMA address of the output section. */
3929 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
3930 || IS_COREFILE_NOTE (segment
, section
))
3932 if (matching_lma
== 0)
3933 matching_lma
= output_section
->lma
;
3935 /* We assume that if the section fits within the segment
3936 then it does not overlap any other section within that
3938 map
->sections
[isec
++] = output_section
;
3940 else if (suggested_lma
== 0)
3941 suggested_lma
= output_section
->lma
;
3945 BFD_ASSERT (j
== section_count
);
3947 /* Step Two: Adjust the physical address of the current segment,
3949 if (isec
== section_count
)
3951 /* All of the sections fitted within the segment as currently
3952 specified. This is the default case. Add the segment to
3953 the list of built segments and carry on to process the next
3954 program header in the input BFD. */
3955 map
->count
= section_count
;
3956 *pointer_to_map
= map
;
3957 pointer_to_map
= &map
->next
;
3964 if (matching_lma
!= 0)
3966 /* At least one section fits inside the current segment.
3967 Keep it, but modify its physical address to match the
3968 LMA of the first section that fitted. */
3969 map
->p_paddr
= matching_lma
;
3973 /* None of the sections fitted inside the current segment.
3974 Change the current segment's physical address to match
3975 the LMA of the first section. */
3976 map
->p_paddr
= suggested_lma
;
3979 /* Offset the segment physical address from the lma
3980 to allow for space taken up by elf headers. */
3981 if (map
->includes_filehdr
)
3982 map
->p_paddr
-= iehdr
->e_ehsize
;
3984 if (map
->includes_phdrs
)
3986 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
3988 /* iehdr->e_phnum is just an estimate of the number
3989 of program headers that we will need. Make a note
3990 here of the number we used and the segment we chose
3991 to hold these headers, so that we can adjust the
3992 offset when we know the correct value. */
3993 phdr_adjust_num
= iehdr
->e_phnum
;
3994 phdr_adjust_seg
= map
;
3998 /* Step Three: Loop over the sections again, this time assigning
3999 those that fit to the current segment and remvoing them from the
4000 sections array; but making sure not to leave large gaps. Once all
4001 possible sections have been assigned to the current segment it is
4002 added to the list of built segments and if sections still remain
4003 to be assigned, a new segment is constructed before repeating
4011 /* Fill the current segment with sections that fit. */
4012 for (j
= 0; j
< section_count
; j
++)
4014 section
= sections
[j
];
4016 if (section
== NULL
)
4019 output_section
= section
->output_section
;
4021 BFD_ASSERT (output_section
!= NULL
);
4023 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4024 || IS_COREFILE_NOTE (segment
, section
))
4026 if (map
->count
== 0)
4028 /* If the first section in a segment does not start at
4029 the beginning of the segment, then something is
4031 if (output_section
->lma
!=
4033 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4034 + (map
->includes_phdrs
4035 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4041 asection
* prev_sec
;
4043 prev_sec
= map
->sections
[map
->count
- 1];
4045 /* If the gap between the end of the previous section
4046 and the start of this section is more than
4047 maxpagesize then we need to start a new segment. */
4048 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
4049 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4050 || ((prev_sec
->lma
+ prev_sec
->_raw_size
) > output_section
->lma
))
4052 if (suggested_lma
== 0)
4053 suggested_lma
= output_section
->lma
;
4059 map
->sections
[map
->count
++] = output_section
;
4062 section
->segment_mark
= true;
4064 else if (suggested_lma
== 0)
4065 suggested_lma
= output_section
->lma
;
4068 BFD_ASSERT (map
->count
> 0);
4070 /* Add the current segment to the list of built segments. */
4071 *pointer_to_map
= map
;
4072 pointer_to_map
= &map
->next
;
4074 if (isec
< section_count
)
4076 /* We still have not allocated all of the sections to
4077 segments. Create a new segment here, initialise it
4078 and carry on looping. */
4079 amt
= sizeof (struct elf_segment_map
);
4080 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4081 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4085 /* Initialise the fields of the segment map. Set the physical
4086 physical address to the LMA of the first section that has
4087 not yet been assigned. */
4089 map
->p_type
= segment
->p_type
;
4090 map
->p_flags
= segment
->p_flags
;
4091 map
->p_flags_valid
= 1;
4092 map
->p_paddr
= suggested_lma
;
4093 map
->p_paddr_valid
= 1;
4094 map
->includes_filehdr
= 0;
4095 map
->includes_phdrs
= 0;
4098 while (isec
< section_count
);
4103 /* The Solaris linker creates program headers in which all the
4104 p_paddr fields are zero. When we try to objcopy or strip such a
4105 file, we get confused. Check for this case, and if we find it
4106 reset the p_paddr_valid fields. */
4107 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4108 if (map
->p_paddr
!= 0)
4112 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4113 map
->p_paddr_valid
= 0;
4116 elf_tdata (obfd
)->segment_map
= map_first
;
4118 /* If we had to estimate the number of program headers that were
4119 going to be needed, then check our estimate know and adjust
4120 the offset if necessary. */
4121 if (phdr_adjust_seg
!= NULL
)
4125 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4128 if (count
> phdr_adjust_num
)
4129 phdr_adjust_seg
->p_paddr
4130 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4134 /* Final Step: Sort the segments into ascending order of physical
4136 if (map_first
!= NULL
)
4138 struct elf_segment_map
*prev
;
4141 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4143 /* Yes I know - its a bubble sort.... */
4144 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4146 /* Swap map and map->next. */
4147 prev
->next
= map
->next
;
4148 map
->next
= map
->next
->next
;
4149 prev
->next
->next
= map
;
4159 #undef IS_CONTAINED_BY_VMA
4160 #undef IS_CONTAINED_BY_LMA
4161 #undef IS_COREFILE_NOTE
4162 #undef IS_SOLARIS_PT_INTERP
4163 #undef INCLUDE_SECTION_IN_SEGMENT
4164 #undef SEGMENT_AFTER_SEGMENT
4165 #undef SEGMENT_OVERLAPS
4169 /* Copy private section information. This copies over the entsize
4170 field, and sometimes the info field. */
4173 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4179 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4181 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4182 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4185 /* Copy over private BFD data if it has not already been copied.
4186 This must be done here, rather than in the copy_private_bfd_data
4187 entry point, because the latter is called after the section
4188 contents have been set, which means that the program headers have
4189 already been worked out. */
4190 if (elf_tdata (obfd
)->segment_map
== NULL
4191 && elf_tdata (ibfd
)->phdr
!= NULL
)
4195 /* Only set up the segments if there are no more SEC_ALLOC
4196 sections. FIXME: This won't do the right thing if objcopy is
4197 used to remove the last SEC_ALLOC section, since objcopy
4198 won't call this routine in that case. */
4199 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4200 if ((s
->flags
& SEC_ALLOC
) != 0)
4204 if (! copy_private_bfd_data (ibfd
, obfd
))
4209 ihdr
= &elf_section_data (isec
)->this_hdr
;
4210 ohdr
= &elf_section_data (osec
)->this_hdr
;
4212 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4214 if (ihdr
->sh_type
== SHT_SYMTAB
4215 || ihdr
->sh_type
== SHT_DYNSYM
4216 || ihdr
->sh_type
== SHT_GNU_verneed
4217 || ihdr
->sh_type
== SHT_GNU_verdef
)
4218 ohdr
->sh_info
= ihdr
->sh_info
;
4220 elf_section_data (osec
)->use_rela_p
4221 = elf_section_data (isec
)->use_rela_p
;
4226 /* Copy private symbol information. If this symbol is in a section
4227 which we did not map into a BFD section, try to map the section
4228 index correctly. We use special macro definitions for the mapped
4229 section indices; these definitions are interpreted by the
4230 swap_out_syms function. */
4232 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4233 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4234 #define MAP_STRTAB (SHN_LORESERVE - 3)
4235 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4238 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4244 elf_symbol_type
*isym
, *osym
;
4246 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4247 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4250 isym
= elf_symbol_from (ibfd
, isymarg
);
4251 osym
= elf_symbol_from (obfd
, osymarg
);
4255 && bfd_is_abs_section (isym
->symbol
.section
))
4259 shndx
= isym
->internal_elf_sym
.st_shndx
;
4260 if (shndx
== elf_onesymtab (ibfd
))
4261 shndx
= MAP_ONESYMTAB
;
4262 else if (shndx
== elf_dynsymtab (ibfd
))
4263 shndx
= MAP_DYNSYMTAB
;
4264 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4266 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4267 shndx
= MAP_SHSTRTAB
;
4268 osym
->internal_elf_sym
.st_shndx
= shndx
;
4274 /* Swap out the symbols. */
4277 swap_out_syms (abfd
, sttp
, relocatable_p
)
4279 struct bfd_strtab_hash
**sttp
;
4282 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4284 if (!elf_map_symbols (abfd
))
4287 /* Dump out the symtabs. */
4289 int symcount
= bfd_get_symcount (abfd
);
4290 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4291 struct bfd_strtab_hash
*stt
;
4292 Elf_Internal_Shdr
*symtab_hdr
;
4293 Elf_Internal_Shdr
*symstrtab_hdr
;
4294 char *outbound_syms
;
4298 stt
= _bfd_elf_stringtab_init ();
4302 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4303 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4304 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4305 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4306 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4307 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4309 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4310 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4312 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
4313 outbound_syms
= bfd_alloc (abfd
, amt
);
4314 if (outbound_syms
== NULL
)
4316 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4318 /* now generate the data (for "contents") */
4320 /* Fill in zeroth symbol and swap it out. */
4321 Elf_Internal_Sym sym
;
4327 sym
.st_shndx
= SHN_UNDEF
;
4328 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4329 outbound_syms
+= bed
->s
->sizeof_sym
;
4331 for (idx
= 0; idx
< symcount
; idx
++)
4333 Elf_Internal_Sym sym
;
4334 bfd_vma value
= syms
[idx
]->value
;
4335 elf_symbol_type
*type_ptr
;
4336 flagword flags
= syms
[idx
]->flags
;
4339 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
4341 /* Local section symbols have no name. */
4346 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4349 if (sym
.st_name
== (unsigned long) -1)
4353 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4355 if ((flags
& BSF_SECTION_SYM
) == 0
4356 && bfd_is_com_section (syms
[idx
]->section
))
4358 /* ELF common symbols put the alignment into the `value' field,
4359 and the size into the `size' field. This is backwards from
4360 how BFD handles it, so reverse it here. */
4361 sym
.st_size
= value
;
4362 if (type_ptr
== NULL
4363 || type_ptr
->internal_elf_sym
.st_value
== 0)
4364 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4366 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4367 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4368 (abfd
, syms
[idx
]->section
);
4372 asection
*sec
= syms
[idx
]->section
;
4375 if (sec
->output_section
)
4377 value
+= sec
->output_offset
;
4378 sec
= sec
->output_section
;
4380 /* Don't add in the section vma for relocatable output. */
4381 if (! relocatable_p
)
4383 sym
.st_value
= value
;
4384 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4386 if (bfd_is_abs_section (sec
)
4388 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4390 /* This symbol is in a real ELF section which we did
4391 not create as a BFD section. Undo the mapping done
4392 by copy_private_symbol_data. */
4393 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4397 shndx
= elf_onesymtab (abfd
);
4400 shndx
= elf_dynsymtab (abfd
);
4403 shndx
= elf_tdata (abfd
)->strtab_section
;
4406 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4414 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4420 /* Writing this would be a hell of a lot easier if
4421 we had some decent documentation on bfd, and
4422 knew what to expect of the library, and what to
4423 demand of applications. For example, it
4424 appears that `objcopy' might not set the
4425 section of a symbol to be a section that is
4426 actually in the output file. */
4427 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4428 BFD_ASSERT (sec2
!= 0);
4429 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
4430 BFD_ASSERT (shndx
!= -1);
4434 sym
.st_shndx
= shndx
;
4437 if ((flags
& BSF_FUNCTION
) != 0)
4439 else if ((flags
& BSF_OBJECT
) != 0)
4444 /* Processor-specific types */
4445 if (type_ptr
!= NULL
4446 && bed
->elf_backend_get_symbol_type
)
4447 type
= (*bed
->elf_backend_get_symbol_type
) (&type_ptr
->internal_elf_sym
, type
);
4449 if (flags
& BSF_SECTION_SYM
)
4451 if (flags
& BSF_GLOBAL
)
4452 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
4454 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4456 else if (bfd_is_com_section (syms
[idx
]->section
))
4457 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4458 else if (bfd_is_und_section (syms
[idx
]->section
))
4459 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4463 else if (flags
& BSF_FILE
)
4464 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4467 int bind
= STB_LOCAL
;
4469 if (flags
& BSF_LOCAL
)
4471 else if (flags
& BSF_WEAK
)
4473 else if (flags
& BSF_GLOBAL
)
4476 sym
.st_info
= ELF_ST_INFO (bind
, type
);
4479 if (type_ptr
!= NULL
)
4480 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
4484 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4485 outbound_syms
+= bed
->s
->sizeof_sym
;
4489 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
4490 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4492 symstrtab_hdr
->sh_flags
= 0;
4493 symstrtab_hdr
->sh_addr
= 0;
4494 symstrtab_hdr
->sh_entsize
= 0;
4495 symstrtab_hdr
->sh_link
= 0;
4496 symstrtab_hdr
->sh_info
= 0;
4497 symstrtab_hdr
->sh_addralign
= 1;
4503 /* Return the number of bytes required to hold the symtab vector.
4505 Note that we base it on the count plus 1, since we will null terminate
4506 the vector allocated based on this size. However, the ELF symbol table
4507 always has a dummy entry as symbol #0, so it ends up even. */
4510 _bfd_elf_get_symtab_upper_bound (abfd
)
4515 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4517 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4518 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4524 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4529 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4531 if (elf_dynsymtab (abfd
) == 0)
4533 bfd_set_error (bfd_error_invalid_operation
);
4537 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4538 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4544 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4545 bfd
*abfd ATTRIBUTE_UNUSED
;
4548 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4551 /* Canonicalize the relocs. */
4554 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4563 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
4569 tblptr
= section
->relocation
;
4570 for (i
= 0; i
< section
->reloc_count
; i
++)
4571 *relptr
++ = tblptr
++;
4575 return section
->reloc_count
;
4579 _bfd_elf_get_symtab (abfd
, alocation
)
4581 asymbol
**alocation
;
4583 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4584 (abfd
, alocation
, false);
4587 bfd_get_symcount (abfd
) = symcount
;
4592 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4594 asymbol
**alocation
;
4596 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4597 (abfd
, alocation
, true);
4600 /* Return the size required for the dynamic reloc entries. Any
4601 section that was actually installed in the BFD, and has type
4602 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4603 considered to be a dynamic reloc section. */
4606 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4612 if (elf_dynsymtab (abfd
) == 0)
4614 bfd_set_error (bfd_error_invalid_operation
);
4618 ret
= sizeof (arelent
*);
4619 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4620 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4621 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4622 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4623 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4624 * sizeof (arelent
*));
4629 /* Canonicalize the dynamic relocation entries. Note that we return
4630 the dynamic relocations as a single block, although they are
4631 actually associated with particular sections; the interface, which
4632 was designed for SunOS style shared libraries, expects that there
4633 is only one set of dynamic relocs. Any section that was actually
4634 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4635 the dynamic symbol table, is considered to be a dynamic reloc
4639 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4644 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4648 if (elf_dynsymtab (abfd
) == 0)
4650 bfd_set_error (bfd_error_invalid_operation
);
4654 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4656 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4658 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4659 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4660 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4665 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4667 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4669 for (i
= 0; i
< count
; i
++)
4680 /* Read in the version information. */
4683 _bfd_elf_slurp_version_tables (abfd
)
4686 bfd_byte
*contents
= NULL
;
4689 if (elf_dynverdef (abfd
) != 0)
4691 Elf_Internal_Shdr
*hdr
;
4692 Elf_External_Verdef
*everdef
;
4693 Elf_Internal_Verdef
*iverdef
;
4694 Elf_Internal_Verdef
*iverdefarr
;
4695 Elf_Internal_Verdef iverdefmem
;
4697 unsigned int maxidx
;
4699 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
4701 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4702 if (contents
== NULL
)
4704 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4705 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4708 /* We know the number of entries in the section but not the maximum
4709 index. Therefore we have to run through all entries and find
4711 everdef
= (Elf_External_Verdef
*) contents
;
4713 for (i
= 0; i
< hdr
->sh_info
; ++i
)
4715 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4717 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
4718 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
4720 everdef
= ((Elf_External_Verdef
*)
4721 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
4724 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
4725 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
4726 if (elf_tdata (abfd
)->verdef
== NULL
)
4729 elf_tdata (abfd
)->cverdefs
= maxidx
;
4731 everdef
= (Elf_External_Verdef
*) contents
;
4732 iverdefarr
= elf_tdata (abfd
)->verdef
;
4733 for (i
= 0; i
< hdr
->sh_info
; i
++)
4735 Elf_External_Verdaux
*everdaux
;
4736 Elf_Internal_Verdaux
*iverdaux
;
4739 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4741 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
4742 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
4744 iverdef
->vd_bfd
= abfd
;
4746 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
4747 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
4748 if (iverdef
->vd_auxptr
== NULL
)
4751 everdaux
= ((Elf_External_Verdaux
*)
4752 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
4753 iverdaux
= iverdef
->vd_auxptr
;
4754 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
4756 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
4758 iverdaux
->vda_nodename
=
4759 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4760 iverdaux
->vda_name
);
4761 if (iverdaux
->vda_nodename
== NULL
)
4764 if (j
+ 1 < iverdef
->vd_cnt
)
4765 iverdaux
->vda_nextptr
= iverdaux
+ 1;
4767 iverdaux
->vda_nextptr
= NULL
;
4769 everdaux
= ((Elf_External_Verdaux
*)
4770 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
4773 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
4775 if (i
+ 1 < hdr
->sh_info
)
4776 iverdef
->vd_nextdef
= iverdef
+ 1;
4778 iverdef
->vd_nextdef
= NULL
;
4780 everdef
= ((Elf_External_Verdef
*)
4781 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
4788 if (elf_dynverref (abfd
) != 0)
4790 Elf_Internal_Shdr
*hdr
;
4791 Elf_External_Verneed
*everneed
;
4792 Elf_Internal_Verneed
*iverneed
;
4795 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
4797 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
4798 elf_tdata (abfd
)->verref
=
4799 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
4800 if (elf_tdata (abfd
)->verref
== NULL
)
4803 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
4805 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4806 if (contents
== NULL
)
4808 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4809 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4812 everneed
= (Elf_External_Verneed
*) contents
;
4813 iverneed
= elf_tdata (abfd
)->verref
;
4814 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4816 Elf_External_Vernaux
*evernaux
;
4817 Elf_Internal_Vernaux
*ivernaux
;
4820 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4822 iverneed
->vn_bfd
= abfd
;
4824 iverneed
->vn_filename
=
4825 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4827 if (iverneed
->vn_filename
== NULL
)
4830 amt
= iverneed
->vn_cnt
;
4831 amt
*= sizeof (Elf_Internal_Vernaux
);
4832 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
4834 evernaux
= ((Elf_External_Vernaux
*)
4835 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4836 ivernaux
= iverneed
->vn_auxptr
;
4837 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4839 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4841 ivernaux
->vna_nodename
=
4842 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4843 ivernaux
->vna_name
);
4844 if (ivernaux
->vna_nodename
== NULL
)
4847 if (j
+ 1 < iverneed
->vn_cnt
)
4848 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4850 ivernaux
->vna_nextptr
= NULL
;
4852 evernaux
= ((Elf_External_Vernaux
*)
4853 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4856 if (i
+ 1 < hdr
->sh_info
)
4857 iverneed
->vn_nextref
= iverneed
+ 1;
4859 iverneed
->vn_nextref
= NULL
;
4861 everneed
= ((Elf_External_Verneed
*)
4862 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4872 if (contents
== NULL
)
4878 _bfd_elf_make_empty_symbol (abfd
)
4881 elf_symbol_type
*newsym
;
4882 bfd_size_type amt
= sizeof (elf_symbol_type
);
4884 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
4889 newsym
->symbol
.the_bfd
= abfd
;
4890 return &newsym
->symbol
;
4895 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4896 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4900 bfd_symbol_info (symbol
, ret
);
4903 /* Return whether a symbol name implies a local symbol. Most targets
4904 use this function for the is_local_label_name entry point, but some
4908 _bfd_elf_is_local_label_name (abfd
, name
)
4909 bfd
*abfd ATTRIBUTE_UNUSED
;
4912 /* Normal local symbols start with ``.L''. */
4913 if (name
[0] == '.' && name
[1] == 'L')
4916 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4917 DWARF debugging symbols starting with ``..''. */
4918 if (name
[0] == '.' && name
[1] == '.')
4921 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4922 emitting DWARF debugging output. I suspect this is actually a
4923 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4924 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4925 underscore to be emitted on some ELF targets). For ease of use,
4926 we treat such symbols as local. */
4927 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
4934 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
4935 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4936 asymbol
*symbol ATTRIBUTE_UNUSED
;
4943 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
4945 enum bfd_architecture arch
;
4946 unsigned long machine
;
4948 /* If this isn't the right architecture for this backend, and this
4949 isn't the generic backend, fail. */
4950 if (arch
!= get_elf_backend_data (abfd
)->arch
4951 && arch
!= bfd_arch_unknown
4952 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
4955 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
4958 /* Find the function to a particular section and offset,
4959 for error reporting. */
4962 elf_find_function (abfd
, section
, symbols
, offset
,
4963 filename_ptr
, functionname_ptr
)
4964 bfd
*abfd ATTRIBUTE_UNUSED
;
4968 const char **filename_ptr
;
4969 const char **functionname_ptr
;
4971 const char *filename
;
4980 for (p
= symbols
; *p
!= NULL
; p
++)
4984 q
= (elf_symbol_type
*) *p
;
4986 if (bfd_get_section (&q
->symbol
) != section
)
4989 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
4994 filename
= bfd_asymbol_name (&q
->symbol
);
4998 if (q
->symbol
.section
== section
4999 && q
->symbol
.value
>= low_func
5000 && q
->symbol
.value
<= offset
)
5002 func
= (asymbol
*) q
;
5003 low_func
= q
->symbol
.value
;
5013 *filename_ptr
= filename
;
5014 if (functionname_ptr
)
5015 *functionname_ptr
= bfd_asymbol_name (func
);
5020 /* Find the nearest line to a particular section and offset,
5021 for error reporting. */
5024 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5025 filename_ptr
, functionname_ptr
, line_ptr
)
5030 const char **filename_ptr
;
5031 const char **functionname_ptr
;
5032 unsigned int *line_ptr
;
5036 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5037 filename_ptr
, functionname_ptr
,
5040 if (!*functionname_ptr
)
5041 elf_find_function (abfd
, section
, symbols
, offset
,
5042 *filename_ptr
? NULL
: filename_ptr
,
5048 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5049 filename_ptr
, functionname_ptr
,
5051 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5053 if (!*functionname_ptr
)
5054 elf_find_function (abfd
, section
, symbols
, offset
,
5055 *filename_ptr
? NULL
: filename_ptr
,
5061 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5062 &found
, filename_ptr
,
5063 functionname_ptr
, line_ptr
,
5064 &elf_tdata (abfd
)->line_info
))
5069 if (symbols
== NULL
)
5072 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5073 filename_ptr
, functionname_ptr
))
5081 _bfd_elf_sizeof_headers (abfd
, reloc
)
5087 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5089 ret
+= get_program_header_size (abfd
);
5094 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5099 bfd_size_type count
;
5101 Elf_Internal_Shdr
*hdr
;
5104 if (! abfd
->output_has_begun
5105 && ! _bfd_elf_compute_section_file_positions
5106 (abfd
, (struct bfd_link_info
*) NULL
))
5109 hdr
= &elf_section_data (section
)->this_hdr
;
5110 pos
= hdr
->sh_offset
+ offset
;
5111 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5112 || bfd_bwrite (location
, count
, abfd
) != count
)
5119 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5120 bfd
*abfd ATTRIBUTE_UNUSED
;
5121 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5122 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5129 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5132 Elf_Internal_Rel
*dst
;
5138 /* Try to convert a non-ELF reloc into an ELF one. */
5141 _bfd_elf_validate_reloc (abfd
, areloc
)
5145 /* Check whether we really have an ELF howto. */
5147 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5149 bfd_reloc_code_real_type code
;
5150 reloc_howto_type
*howto
;
5152 /* Alien reloc: Try to determine its type to replace it with an
5153 equivalent ELF reloc. */
5155 if (areloc
->howto
->pc_relative
)
5157 switch (areloc
->howto
->bitsize
)
5160 code
= BFD_RELOC_8_PCREL
;
5163 code
= BFD_RELOC_12_PCREL
;
5166 code
= BFD_RELOC_16_PCREL
;
5169 code
= BFD_RELOC_24_PCREL
;
5172 code
= BFD_RELOC_32_PCREL
;
5175 code
= BFD_RELOC_64_PCREL
;
5181 howto
= bfd_reloc_type_lookup (abfd
, code
);
5183 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5185 if (howto
->pcrel_offset
)
5186 areloc
->addend
+= areloc
->address
;
5188 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5193 switch (areloc
->howto
->bitsize
)
5199 code
= BFD_RELOC_14
;
5202 code
= BFD_RELOC_16
;
5205 code
= BFD_RELOC_26
;
5208 code
= BFD_RELOC_32
;
5211 code
= BFD_RELOC_64
;
5217 howto
= bfd_reloc_type_lookup (abfd
, code
);
5221 areloc
->howto
= howto
;
5229 (*_bfd_error_handler
)
5230 (_("%s: unsupported relocation type %s"),
5231 bfd_archive_filename (abfd
), areloc
->howto
->name
);
5232 bfd_set_error (bfd_error_bad_value
);
5237 _bfd_elf_close_and_cleanup (abfd
)
5240 if (bfd_get_format (abfd
) == bfd_object
)
5242 if (elf_shstrtab (abfd
) != NULL
)
5243 _bfd_stringtab_free (elf_shstrtab (abfd
));
5246 return _bfd_generic_close_and_cleanup (abfd
);
5249 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5250 in the relocation's offset. Thus we cannot allow any sort of sanity
5251 range-checking to interfere. There is nothing else to do in processing
5254 bfd_reloc_status_type
5255 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5256 bfd
*abfd ATTRIBUTE_UNUSED
;
5257 arelent
*re ATTRIBUTE_UNUSED
;
5258 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5259 PTR data ATTRIBUTE_UNUSED
;
5260 asection
*is ATTRIBUTE_UNUSED
;
5261 bfd
*obfd ATTRIBUTE_UNUSED
;
5262 char **errmsg ATTRIBUTE_UNUSED
;
5264 return bfd_reloc_ok
;
5267 /* Elf core file support. Much of this only works on native
5268 toolchains, since we rely on knowing the
5269 machine-dependent procfs structure in order to pick
5270 out details about the corefile. */
5272 #ifdef HAVE_SYS_PROCFS_H
5273 # include <sys/procfs.h>
5276 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5279 elfcore_make_pid (abfd
)
5282 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5283 + (elf_tdata (abfd
)->core_pid
));
5286 /* If there isn't a section called NAME, make one, using
5287 data from SECT. Note, this function will generate a
5288 reference to NAME, so you shouldn't deallocate or
5292 elfcore_maybe_make_sect (abfd
, name
, sect
)
5299 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5302 sect2
= bfd_make_section (abfd
, name
);
5306 sect2
->_raw_size
= sect
->_raw_size
;
5307 sect2
->filepos
= sect
->filepos
;
5308 sect2
->flags
= sect
->flags
;
5309 sect2
->alignment_power
= sect
->alignment_power
;
5313 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5314 actually creates up to two pseudosections:
5315 - For the single-threaded case, a section named NAME, unless
5316 such a section already exists.
5317 - For the multi-threaded case, a section named "NAME/PID", where
5318 PID is elfcore_make_pid (abfd).
5319 Both pseudosections have identical contents. */
5321 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
5328 char *threaded_name
;
5331 /* Build the section name. */
5333 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5334 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5335 if (threaded_name
== NULL
)
5337 strcpy (threaded_name
, buf
);
5339 sect
= bfd_make_section (abfd
, threaded_name
);
5342 sect
->_raw_size
= size
;
5343 sect
->filepos
= filepos
;
5344 sect
->flags
= SEC_HAS_CONTENTS
;
5345 sect
->alignment_power
= 2;
5347 return elfcore_maybe_make_sect (abfd
, name
, sect
);
5350 /* prstatus_t exists on:
5352 linux 2.[01] + glibc
5356 #if defined (HAVE_PRSTATUS_T)
5357 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
5360 elfcore_grok_prstatus (abfd
, note
)
5362 Elf_Internal_Note
*note
;
5367 if (note
->descsz
== sizeof (prstatus_t
))
5371 raw_size
= sizeof (prstat
.pr_reg
);
5372 offset
= offsetof (prstatus_t
, pr_reg
);
5373 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5375 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5376 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5378 /* pr_who exists on:
5381 pr_who doesn't exist on:
5384 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5385 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5388 #if defined (HAVE_PRSTATUS32_T)
5389 else if (note
->descsz
== sizeof (prstatus32_t
))
5391 /* 64-bit host, 32-bit corefile */
5392 prstatus32_t prstat
;
5394 raw_size
= sizeof (prstat
.pr_reg
);
5395 offset
= offsetof (prstatus32_t
, pr_reg
);
5396 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5398 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5399 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5401 /* pr_who exists on:
5404 pr_who doesn't exist on:
5407 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5408 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5411 #endif /* HAVE_PRSTATUS32_T */
5414 /* Fail - we don't know how to handle any other
5415 note size (ie. data object type). */
5419 /* Make a ".reg/999" section and a ".reg" section. */
5420 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
5421 raw_size
, note
->descpos
+ offset
);
5423 #endif /* defined (HAVE_PRSTATUS_T) */
5425 /* Create a pseudosection containing the exact contents of NOTE. */
5427 elfcore_make_note_pseudosection (abfd
, name
, note
)
5430 Elf_Internal_Note
*note
;
5432 return _bfd_elfcore_make_pseudosection (abfd
, name
,
5433 note
->descsz
, note
->descpos
);
5436 /* There isn't a consistent prfpregset_t across platforms,
5437 but it doesn't matter, because we don't have to pick this
5438 data structure apart. */
5441 elfcore_grok_prfpreg (abfd
, note
)
5443 Elf_Internal_Note
*note
;
5445 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
5448 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5449 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5453 elfcore_grok_prxfpreg (abfd
, note
)
5455 Elf_Internal_Note
*note
;
5457 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
5460 #if defined (HAVE_PRPSINFO_T)
5461 typedef prpsinfo_t elfcore_psinfo_t
;
5462 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5463 typedef prpsinfo32_t elfcore_psinfo32_t
;
5467 #if defined (HAVE_PSINFO_T)
5468 typedef psinfo_t elfcore_psinfo_t
;
5469 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5470 typedef psinfo32_t elfcore_psinfo32_t
;
5474 /* return a malloc'ed copy of a string at START which is at
5475 most MAX bytes long, possibly without a terminating '\0'.
5476 the copy will always have a terminating '\0'. */
5479 _bfd_elfcore_strndup (abfd
, start
, max
)
5485 char *end
= memchr (start
, '\0', max
);
5493 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
5497 memcpy (dups
, start
, len
);
5503 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5504 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
5507 elfcore_grok_psinfo (abfd
, note
)
5509 Elf_Internal_Note
*note
;
5511 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
5513 elfcore_psinfo_t psinfo
;
5515 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5517 elf_tdata (abfd
)->core_program
5518 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
5519 sizeof (psinfo
.pr_fname
));
5521 elf_tdata (abfd
)->core_command
5522 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
5523 sizeof (psinfo
.pr_psargs
));
5525 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5526 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
5528 /* 64-bit host, 32-bit corefile */
5529 elfcore_psinfo32_t psinfo
;
5531 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5533 elf_tdata (abfd
)->core_program
5534 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
5535 sizeof (psinfo
.pr_fname
));
5537 elf_tdata (abfd
)->core_command
5538 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
5539 sizeof (psinfo
.pr_psargs
));
5545 /* Fail - we don't know how to handle any other
5546 note size (ie. data object type). */
5550 /* Note that for some reason, a spurious space is tacked
5551 onto the end of the args in some (at least one anyway)
5552 implementations, so strip it off if it exists. */
5555 char *command
= elf_tdata (abfd
)->core_command
;
5556 int n
= strlen (command
);
5558 if (0 < n
&& command
[n
- 1] == ' ')
5559 command
[n
- 1] = '\0';
5564 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5566 #if defined (HAVE_PSTATUS_T)
5568 elfcore_grok_pstatus (abfd
, note
)
5570 Elf_Internal_Note
*note
;
5572 if (note
->descsz
== sizeof (pstatus_t
)
5573 #if defined (HAVE_PXSTATUS_T)
5574 || note
->descsz
== sizeof (pxstatus_t
)
5580 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5582 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5584 #if defined (HAVE_PSTATUS32_T)
5585 else if (note
->descsz
== sizeof (pstatus32_t
))
5587 /* 64-bit host, 32-bit corefile */
5590 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5592 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5595 /* Could grab some more details from the "representative"
5596 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5597 NT_LWPSTATUS note, presumably. */
5601 #endif /* defined (HAVE_PSTATUS_T) */
5603 #if defined (HAVE_LWPSTATUS_T)
5605 elfcore_grok_lwpstatus (abfd
, note
)
5607 Elf_Internal_Note
*note
;
5609 lwpstatus_t lwpstat
;
5614 if (note
->descsz
!= sizeof (lwpstat
)
5615 #if defined (HAVE_LWPXSTATUS_T)
5616 && note
->descsz
!= sizeof (lwpxstatus_t
)
5621 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
5623 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
5624 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
5626 /* Make a ".reg/999" section. */
5628 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5629 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5634 sect
= bfd_make_section (abfd
, name
);
5638 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5639 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
5640 sect
->filepos
= note
->descpos
5641 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
5644 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5645 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
5646 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
5649 sect
->flags
= SEC_HAS_CONTENTS
;
5650 sect
->alignment_power
= 2;
5652 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5655 /* Make a ".reg2/999" section */
5657 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
5658 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5663 sect
= bfd_make_section (abfd
, name
);
5667 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5668 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
5669 sect
->filepos
= note
->descpos
5670 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
5673 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5674 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
5675 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
5678 sect
->flags
= SEC_HAS_CONTENTS
;
5679 sect
->alignment_power
= 2;
5681 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
5683 #endif /* defined (HAVE_LWPSTATUS_T) */
5685 #if defined (HAVE_WIN32_PSTATUS_T)
5687 elfcore_grok_win32pstatus (abfd
, note
)
5689 Elf_Internal_Note
*note
;
5694 win32_pstatus_t pstatus
;
5696 if (note
->descsz
< sizeof (pstatus
))
5699 memcpy (&pstatus
, note
->descdata
, note
->descsz
);
5701 switch (pstatus
.data_type
)
5703 case NOTE_INFO_PROCESS
:
5704 /* FIXME: need to add ->core_command. */
5705 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
5706 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
5709 case NOTE_INFO_THREAD
:
5710 /* Make a ".reg/999" section. */
5711 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
5713 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5719 sect
= bfd_make_section (abfd
, name
);
5723 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
5724 sect
->filepos
= note
->descpos
+ offsetof (struct win32_pstatus
,
5725 data
.thread_info
.thread_context
);
5726 sect
->flags
= SEC_HAS_CONTENTS
;
5727 sect
->alignment_power
= 2;
5729 if (pstatus
.data
.thread_info
.is_active_thread
)
5730 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5734 case NOTE_INFO_MODULE
:
5735 /* Make a ".module/xxxxxxxx" section. */
5736 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
5738 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5744 sect
= bfd_make_section (abfd
, name
);
5749 sect
->_raw_size
= note
->descsz
;
5750 sect
->filepos
= note
->descpos
;
5751 sect
->flags
= SEC_HAS_CONTENTS
;
5752 sect
->alignment_power
= 2;
5761 #endif /* HAVE_WIN32_PSTATUS_T */
5764 elfcore_grok_note (abfd
, note
)
5766 Elf_Internal_Note
*note
;
5768 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5776 if (bed
->elf_backend_grok_prstatus
)
5777 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
5779 #if defined (HAVE_PRSTATUS_T)
5780 return elfcore_grok_prstatus (abfd
, note
);
5785 #if defined (HAVE_PSTATUS_T)
5787 return elfcore_grok_pstatus (abfd
, note
);
5790 #if defined (HAVE_LWPSTATUS_T)
5792 return elfcore_grok_lwpstatus (abfd
, note
);
5795 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
5796 return elfcore_grok_prfpreg (abfd
, note
);
5798 #if defined (HAVE_WIN32_PSTATUS_T)
5799 case NT_WIN32PSTATUS
:
5800 return elfcore_grok_win32pstatus (abfd
, note
);
5803 case NT_PRXFPREG
: /* Linux SSE extension */
5804 if (note
->namesz
== 5
5805 && ! strcmp (note
->namedata
, "LINUX"))
5806 return elfcore_grok_prxfpreg (abfd
, note
);
5812 if (bed
->elf_backend_grok_psinfo
)
5813 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
5815 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5816 return elfcore_grok_psinfo (abfd
, note
);
5824 elfcore_read_notes (abfd
, offset
, size
)
5835 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
5838 buf
= bfd_malloc (size
);
5842 if (bfd_bread (buf
, size
, abfd
) != size
)
5850 while (p
< buf
+ size
)
5852 /* FIXME: bad alignment assumption. */
5853 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
5854 Elf_Internal_Note in
;
5856 in
.type
= H_GET_32 (abfd
, xnp
->type
);
5858 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
5859 in
.namedata
= xnp
->name
;
5861 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
5862 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
5863 in
.descpos
= offset
+ (in
.descdata
- buf
);
5865 if (! elfcore_grok_note (abfd
, &in
))
5868 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
5875 /* Providing external access to the ELF program header table. */
5877 /* Return an upper bound on the number of bytes required to store a
5878 copy of ABFD's program header table entries. Return -1 if an error
5879 occurs; bfd_get_error will return an appropriate code. */
5882 bfd_get_elf_phdr_upper_bound (abfd
)
5885 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5887 bfd_set_error (bfd_error_wrong_format
);
5891 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
5894 /* Copy ABFD's program header table entries to *PHDRS. The entries
5895 will be stored as an array of Elf_Internal_Phdr structures, as
5896 defined in include/elf/internal.h. To find out how large the
5897 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5899 Return the number of program header table entries read, or -1 if an
5900 error occurs; bfd_get_error will return an appropriate code. */
5903 bfd_get_elf_phdrs (abfd
, phdrs
)
5909 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5911 bfd_set_error (bfd_error_wrong_format
);
5915 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
5916 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
5917 num_phdrs
* sizeof (Elf_Internal_Phdr
));
5923 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
5924 bfd
*abfd ATTRIBUTE_UNUSED
;
5929 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
5931 i_ehdrp
= elf_elfheader (abfd
);
5932 if (i_ehdrp
== NULL
)
5933 sprintf_vma (buf
, value
);
5936 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
5938 #if BFD_HOST_64BIT_LONG
5939 sprintf (buf
, "%016lx", value
);
5941 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
5942 _bfd_int64_low (value
));
5946 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
5949 sprintf_vma (buf
, value
);
5954 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
5955 bfd
*abfd ATTRIBUTE_UNUSED
;
5960 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
5962 i_ehdrp
= elf_elfheader (abfd
);
5963 if (i_ehdrp
== NULL
)
5964 fprintf_vma ((FILE *) stream
, value
);
5967 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
5969 #if BFD_HOST_64BIT_LONG
5970 fprintf ((FILE *) stream
, "%016lx", value
);
5972 fprintf ((FILE *) stream
, "%08lx%08lx",
5973 _bfd_int64_high (value
), _bfd_int64_low (value
));
5977 fprintf ((FILE *) stream
, "%08lx",
5978 (unsigned long) (value
& 0xffffffff));
5981 fprintf_vma ((FILE *) stream
, value
);
5985 enum elf_reloc_type_class
5986 _bfd_elf_reloc_type_class (rela
)
5987 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
5989 return reloc_class_normal
;