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
;
997 /* Copy down any references that we may have already seen to the
998 symbol which just became indirect. */
1000 dir
->elf_link_hash_flags
|=
1001 (ind
->elf_link_hash_flags
1002 & (ELF_LINK_HASH_REF_DYNAMIC
1003 | ELF_LINK_HASH_REF_REGULAR
1004 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1005 | ELF_LINK_NON_GOT_REF
));
1007 /* Copy over the global and procedure linkage table refcount entries.
1008 These may have been already set up by a check_relocs routine. */
1009 tmp
= dir
->got
.refcount
;
1012 dir
->got
.refcount
= ind
->got
.refcount
;
1013 ind
->got
.refcount
= tmp
;
1016 BFD_ASSERT (ind
->got
.refcount
<= 0);
1018 tmp
= dir
->plt
.refcount
;
1021 dir
->plt
.refcount
= ind
->plt
.refcount
;
1022 ind
->plt
.refcount
= tmp
;
1025 BFD_ASSERT (ind
->plt
.refcount
<= 0);
1027 if (dir
->dynindx
== -1)
1029 dir
->dynindx
= ind
->dynindx
;
1030 dir
->dynstr_index
= ind
->dynstr_index
;
1032 ind
->dynstr_index
= 0;
1035 BFD_ASSERT (ind
->dynindx
== -1);
1039 _bfd_elf_link_hash_hide_symbol (info
, h
)
1040 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1041 struct elf_link_hash_entry
*h
;
1043 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1044 h
->plt
.offset
= (bfd_vma
) -1;
1045 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1049 /* Initialize an ELF linker hash table. */
1052 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1053 struct elf_link_hash_table
*table
;
1055 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1056 struct bfd_hash_table
*,
1061 table
->dynamic_sections_created
= false;
1062 table
->dynobj
= NULL
;
1063 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
- 1;
1064 /* The first dynamic symbol is a dummy. */
1065 table
->dynsymcount
= 1;
1066 table
->dynstr
= NULL
;
1067 table
->bucketcount
= 0;
1068 table
->needed
= NULL
;
1069 table
->runpath
= NULL
;
1071 table
->stab_info
= NULL
;
1072 table
->merge_info
= NULL
;
1073 table
->dynlocal
= NULL
;
1074 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1075 table
->root
.type
= bfd_link_elf_hash_table
;
1080 /* Create an ELF linker hash table. */
1082 struct bfd_link_hash_table
*
1083 _bfd_elf_link_hash_table_create (abfd
)
1086 struct elf_link_hash_table
*ret
;
1087 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1089 ret
= (struct elf_link_hash_table
*) bfd_alloc (abfd
, amt
);
1090 if (ret
== (struct elf_link_hash_table
*) NULL
)
1093 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1095 bfd_release (abfd
, ret
);
1102 /* This is a hook for the ELF emulation code in the generic linker to
1103 tell the backend linker what file name to use for the DT_NEEDED
1104 entry for a dynamic object. The generic linker passes name as an
1105 empty string to indicate that no DT_NEEDED entry should be made. */
1108 bfd_elf_set_dt_needed_name (abfd
, name
)
1112 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1113 && bfd_get_format (abfd
) == bfd_object
)
1114 elf_dt_name (abfd
) = name
;
1118 bfd_elf_set_dt_needed_soname (abfd
, name
)
1122 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1123 && bfd_get_format (abfd
) == bfd_object
)
1124 elf_dt_soname (abfd
) = name
;
1127 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1128 the linker ELF emulation code. */
1130 struct bfd_link_needed_list
*
1131 bfd_elf_get_needed_list (abfd
, info
)
1132 bfd
*abfd ATTRIBUTE_UNUSED
;
1133 struct bfd_link_info
*info
;
1135 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1137 return elf_hash_table (info
)->needed
;
1140 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1141 hook for the linker ELF emulation code. */
1143 struct bfd_link_needed_list
*
1144 bfd_elf_get_runpath_list (abfd
, info
)
1145 bfd
*abfd ATTRIBUTE_UNUSED
;
1146 struct bfd_link_info
*info
;
1148 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1150 return elf_hash_table (info
)->runpath
;
1153 /* Get the name actually used for a dynamic object for a link. This
1154 is the SONAME entry if there is one. Otherwise, it is the string
1155 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1158 bfd_elf_get_dt_soname (abfd
)
1161 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1162 && bfd_get_format (abfd
) == bfd_object
)
1163 return elf_dt_name (abfd
);
1167 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1168 the ELF linker emulation code. */
1171 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1173 struct bfd_link_needed_list
**pneeded
;
1176 bfd_byte
*dynbuf
= NULL
;
1178 unsigned long shlink
;
1179 bfd_byte
*extdyn
, *extdynend
;
1181 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1185 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1186 || bfd_get_format (abfd
) != bfd_object
)
1189 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1190 if (s
== NULL
|| s
->_raw_size
== 0)
1193 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1197 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1201 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1205 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1207 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1208 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1211 extdynend
= extdyn
+ s
->_raw_size
;
1212 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1214 Elf_Internal_Dyn dyn
;
1216 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1218 if (dyn
.d_tag
== DT_NULL
)
1221 if (dyn
.d_tag
== DT_NEEDED
)
1224 struct bfd_link_needed_list
*l
;
1225 unsigned int tagv
= dyn
.d_un
.d_val
;
1228 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1233 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1254 /* Allocate an ELF string table--force the first byte to be zero. */
1256 struct bfd_strtab_hash
*
1257 _bfd_elf_stringtab_init ()
1259 struct bfd_strtab_hash
*ret
;
1261 ret
= _bfd_stringtab_init ();
1266 loc
= _bfd_stringtab_add (ret
, "", true, false);
1267 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1268 if (loc
== (bfd_size_type
) -1)
1270 _bfd_stringtab_free (ret
);
1277 /* ELF .o/exec file reading */
1279 /* Create a new bfd section from an ELF section header. */
1282 bfd_section_from_shdr (abfd
, shindex
)
1284 unsigned int shindex
;
1286 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1287 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1288 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1291 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1293 switch (hdr
->sh_type
)
1296 /* Inactive section. Throw it away. */
1299 case SHT_PROGBITS
: /* Normal section with contents. */
1300 case SHT_DYNAMIC
: /* Dynamic linking information. */
1301 case SHT_NOBITS
: /* .bss section. */
1302 case SHT_HASH
: /* .hash section. */
1303 case SHT_NOTE
: /* .note section. */
1304 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1306 case SHT_SYMTAB
: /* A symbol table */
1307 if (elf_onesymtab (abfd
) == shindex
)
1310 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1311 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1312 elf_onesymtab (abfd
) = shindex
;
1313 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1314 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1315 abfd
->flags
|= HAS_SYMS
;
1317 /* Sometimes a shared object will map in the symbol table. If
1318 SHF_ALLOC is set, and this is a shared object, then we also
1319 treat this section as a BFD section. We can not base the
1320 decision purely on SHF_ALLOC, because that flag is sometimes
1321 set in a relocateable object file, which would confuse the
1323 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1324 && (abfd
->flags
& DYNAMIC
) != 0
1325 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1330 case SHT_DYNSYM
: /* A dynamic symbol table */
1331 if (elf_dynsymtab (abfd
) == shindex
)
1334 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1335 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1336 elf_dynsymtab (abfd
) = shindex
;
1337 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1338 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1339 abfd
->flags
|= HAS_SYMS
;
1341 /* Besides being a symbol table, we also treat this as a regular
1342 section, so that objcopy can handle it. */
1343 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1345 case SHT_STRTAB
: /* A string table */
1346 if (hdr
->bfd_section
!= NULL
)
1348 if (ehdr
->e_shstrndx
== shindex
)
1350 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1351 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1357 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1359 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1360 if (hdr2
->sh_link
== shindex
)
1362 if (! bfd_section_from_shdr (abfd
, i
))
1364 if (elf_onesymtab (abfd
) == i
)
1366 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1367 elf_elfsections (abfd
)[shindex
] =
1368 &elf_tdata (abfd
)->strtab_hdr
;
1371 if (elf_dynsymtab (abfd
) == i
)
1373 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1374 elf_elfsections (abfd
)[shindex
] = hdr
=
1375 &elf_tdata (abfd
)->dynstrtab_hdr
;
1376 /* We also treat this as a regular section, so
1377 that objcopy can handle it. */
1380 #if 0 /* Not handling other string tables specially right now. */
1381 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1382 /* We have a strtab for some random other section. */
1383 newsect
= (asection
*) hdr2
->bfd_section
;
1386 hdr
->bfd_section
= newsect
;
1387 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1389 elf_elfsections (abfd
)[shindex
] = hdr2
;
1395 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1399 /* *These* do a lot of work -- but build no sections! */
1401 asection
*target_sect
;
1402 Elf_Internal_Shdr
*hdr2
;
1404 /* Check for a bogus link to avoid crashing. */
1405 if (hdr
->sh_link
>= ehdr
->e_shnum
)
1407 ((*_bfd_error_handler
)
1408 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1409 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1410 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1413 /* For some incomprehensible reason Oracle distributes
1414 libraries for Solaris in which some of the objects have
1415 bogus sh_link fields. It would be nice if we could just
1416 reject them, but, unfortunately, some people need to use
1417 them. We scan through the section headers; if we find only
1418 one suitable symbol table, we clobber the sh_link to point
1419 to it. I hope this doesn't break anything. */
1420 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1421 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1427 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1429 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1430 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1441 hdr
->sh_link
= found
;
1444 /* Get the symbol table. */
1445 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1446 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1449 /* If this reloc section does not use the main symbol table we
1450 don't treat it as a reloc section. BFD can't adequately
1451 represent such a section, so at least for now, we don't
1452 try. We just present it as a normal section. We also
1453 can't use it as a reloc section if it points to the null
1455 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1456 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1458 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1460 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1461 if (target_sect
== NULL
)
1464 if ((target_sect
->flags
& SEC_RELOC
) == 0
1465 || target_sect
->reloc_count
== 0)
1466 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1470 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1471 amt
= sizeof (*hdr2
);
1472 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1473 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1476 elf_elfsections (abfd
)[shindex
] = hdr2
;
1477 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1478 target_sect
->flags
|= SEC_RELOC
;
1479 target_sect
->relocation
= NULL
;
1480 target_sect
->rel_filepos
= hdr
->sh_offset
;
1481 /* In the section to which the relocations apply, mark whether
1482 its relocations are of the REL or RELA variety. */
1483 if (hdr
->sh_size
!= 0)
1484 elf_section_data (target_sect
)->use_rela_p
1485 = (hdr
->sh_type
== SHT_RELA
);
1486 abfd
->flags
|= HAS_RELOC
;
1491 case SHT_GNU_verdef
:
1492 elf_dynverdef (abfd
) = shindex
;
1493 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1494 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1497 case SHT_GNU_versym
:
1498 elf_dynversym (abfd
) = shindex
;
1499 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1500 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1503 case SHT_GNU_verneed
:
1504 elf_dynverref (abfd
) = shindex
;
1505 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1506 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1513 /* Check for any processor-specific section types. */
1515 if (bed
->elf_backend_section_from_shdr
)
1516 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1524 /* Given an ELF section number, retrieve the corresponding BFD
1528 bfd_section_from_elf_index (abfd
, index
)
1532 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1533 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1535 return elf_elfsections (abfd
)[index
]->bfd_section
;
1539 _bfd_elf_new_section_hook (abfd
, sec
)
1543 struct bfd_elf_section_data
*sdata
;
1544 bfd_size_type amt
= sizeof (*sdata
);
1546 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
1549 sec
->used_by_bfd
= (PTR
) sdata
;
1551 /* Indicate whether or not this section should use RELA relocations. */
1553 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1558 /* Create a new bfd section from an ELF program header.
1560 Since program segments have no names, we generate a synthetic name
1561 of the form segment<NUM>, where NUM is generally the index in the
1562 program header table. For segments that are split (see below) we
1563 generate the names segment<NUM>a and segment<NUM>b.
1565 Note that some program segments may have a file size that is different than
1566 (less than) the memory size. All this means is that at execution the
1567 system must allocate the amount of memory specified by the memory size,
1568 but only initialize it with the first "file size" bytes read from the
1569 file. This would occur for example, with program segments consisting
1570 of combined data+bss.
1572 To handle the above situation, this routine generates TWO bfd sections
1573 for the single program segment. The first has the length specified by
1574 the file size of the segment, and the second has the length specified
1575 by the difference between the two sizes. In effect, the segment is split
1576 into it's initialized and uninitialized parts.
1581 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1583 Elf_Internal_Phdr
*hdr
;
1585 const char *typename
;
1592 split
= ((hdr
->p_memsz
> 0)
1593 && (hdr
->p_filesz
> 0)
1594 && (hdr
->p_memsz
> hdr
->p_filesz
));
1595 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1596 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1599 strcpy (name
, namebuf
);
1600 newsect
= bfd_make_section (abfd
, name
);
1601 if (newsect
== NULL
)
1603 newsect
->vma
= hdr
->p_vaddr
;
1604 newsect
->lma
= hdr
->p_paddr
;
1605 newsect
->_raw_size
= hdr
->p_filesz
;
1606 newsect
->filepos
= hdr
->p_offset
;
1607 newsect
->flags
|= SEC_HAS_CONTENTS
;
1608 if (hdr
->p_type
== PT_LOAD
)
1610 newsect
->flags
|= SEC_ALLOC
;
1611 newsect
->flags
|= SEC_LOAD
;
1612 if (hdr
->p_flags
& PF_X
)
1614 /* FIXME: all we known is that it has execute PERMISSION,
1616 newsect
->flags
|= SEC_CODE
;
1619 if (!(hdr
->p_flags
& PF_W
))
1621 newsect
->flags
|= SEC_READONLY
;
1626 sprintf (namebuf
, "%s%db", typename
, index
);
1627 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1630 strcpy (name
, namebuf
);
1631 newsect
= bfd_make_section (abfd
, name
);
1632 if (newsect
== NULL
)
1634 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1635 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1636 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1637 if (hdr
->p_type
== PT_LOAD
)
1639 newsect
->flags
|= SEC_ALLOC
;
1640 if (hdr
->p_flags
& PF_X
)
1641 newsect
->flags
|= SEC_CODE
;
1643 if (!(hdr
->p_flags
& PF_W
))
1644 newsect
->flags
|= SEC_READONLY
;
1651 bfd_section_from_phdr (abfd
, hdr
, index
)
1653 Elf_Internal_Phdr
*hdr
;
1656 struct elf_backend_data
*bed
;
1658 switch (hdr
->p_type
)
1661 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
1664 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
1667 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
1670 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
1673 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
1675 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
1680 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
1683 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
1686 /* Check for any processor-specific program segment types.
1687 If no handler for them, default to making "segment" sections. */
1688 bed
= get_elf_backend_data (abfd
);
1689 if (bed
->elf_backend_section_from_phdr
)
1690 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
1692 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
1696 /* Initialize REL_HDR, the section-header for new section, containing
1697 relocations against ASECT. If USE_RELA_P is true, we use RELA
1698 relocations; otherwise, we use REL relocations. */
1701 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
1703 Elf_Internal_Shdr
*rel_hdr
;
1708 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1709 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
1711 name
= bfd_alloc (abfd
, amt
);
1714 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1716 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1718 if (rel_hdr
->sh_name
== (unsigned int) -1)
1720 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1721 rel_hdr
->sh_entsize
= (use_rela_p
1722 ? bed
->s
->sizeof_rela
1723 : bed
->s
->sizeof_rel
);
1724 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
1725 rel_hdr
->sh_flags
= 0;
1726 rel_hdr
->sh_addr
= 0;
1727 rel_hdr
->sh_size
= 0;
1728 rel_hdr
->sh_offset
= 0;
1733 /* Set up an ELF internal section header for a section. */
1736 elf_fake_sections (abfd
, asect
, failedptrarg
)
1741 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1742 boolean
*failedptr
= (boolean
*) failedptrarg
;
1743 Elf_Internal_Shdr
*this_hdr
;
1747 /* We already failed; just get out of the bfd_map_over_sections
1752 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1754 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1757 if (this_hdr
->sh_name
== (unsigned long) -1)
1763 this_hdr
->sh_flags
= 0;
1765 if ((asect
->flags
& SEC_ALLOC
) != 0
1766 || asect
->user_set_vma
)
1767 this_hdr
->sh_addr
= asect
->vma
;
1769 this_hdr
->sh_addr
= 0;
1771 this_hdr
->sh_offset
= 0;
1772 this_hdr
->sh_size
= asect
->_raw_size
;
1773 this_hdr
->sh_link
= 0;
1774 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1775 /* The sh_entsize and sh_info fields may have been set already by
1776 copy_private_section_data. */
1778 this_hdr
->bfd_section
= asect
;
1779 this_hdr
->contents
= NULL
;
1781 /* FIXME: This should not be based on section names. */
1782 if (strcmp (asect
->name
, ".dynstr") == 0)
1783 this_hdr
->sh_type
= SHT_STRTAB
;
1784 else if (strcmp (asect
->name
, ".hash") == 0)
1786 this_hdr
->sh_type
= SHT_HASH
;
1787 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
1789 else if (strcmp (asect
->name
, ".dynsym") == 0)
1791 this_hdr
->sh_type
= SHT_DYNSYM
;
1792 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1794 else if (strcmp (asect
->name
, ".dynamic") == 0)
1796 this_hdr
->sh_type
= SHT_DYNAMIC
;
1797 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1799 else if (strncmp (asect
->name
, ".rela", 5) == 0
1800 && get_elf_backend_data (abfd
)->may_use_rela_p
)
1802 this_hdr
->sh_type
= SHT_RELA
;
1803 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1805 else if (strncmp (asect
->name
, ".rel", 4) == 0
1806 && get_elf_backend_data (abfd
)->may_use_rel_p
)
1808 this_hdr
->sh_type
= SHT_REL
;
1809 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1811 else if (strncmp (asect
->name
, ".note", 5) == 0)
1812 this_hdr
->sh_type
= SHT_NOTE
;
1813 else if (strncmp (asect
->name
, ".stab", 5) == 0
1814 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1815 this_hdr
->sh_type
= SHT_STRTAB
;
1816 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1818 this_hdr
->sh_type
= SHT_GNU_versym
;
1819 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1821 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1823 this_hdr
->sh_type
= SHT_GNU_verdef
;
1824 this_hdr
->sh_entsize
= 0;
1825 /* objcopy or strip will copy over sh_info, but may not set
1826 cverdefs. The linker will set cverdefs, but sh_info will be
1828 if (this_hdr
->sh_info
== 0)
1829 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1831 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1832 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1834 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1836 this_hdr
->sh_type
= SHT_GNU_verneed
;
1837 this_hdr
->sh_entsize
= 0;
1838 /* objcopy or strip will copy over sh_info, but may not set
1839 cverrefs. The linker will set cverrefs, but sh_info will be
1841 if (this_hdr
->sh_info
== 0)
1842 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1844 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1845 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1847 else if ((asect
->flags
& SEC_ALLOC
) != 0
1848 && ((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0))
1849 this_hdr
->sh_type
= SHT_NOBITS
;
1851 this_hdr
->sh_type
= SHT_PROGBITS
;
1853 if ((asect
->flags
& SEC_ALLOC
) != 0)
1854 this_hdr
->sh_flags
|= SHF_ALLOC
;
1855 if ((asect
->flags
& SEC_READONLY
) == 0)
1856 this_hdr
->sh_flags
|= SHF_WRITE
;
1857 if ((asect
->flags
& SEC_CODE
) != 0)
1858 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1859 if ((asect
->flags
& SEC_MERGE
) != 0)
1861 this_hdr
->sh_flags
|= SHF_MERGE
;
1862 this_hdr
->sh_entsize
= asect
->entsize
;
1863 if ((asect
->flags
& SEC_STRINGS
) != 0)
1864 this_hdr
->sh_flags
|= SHF_STRINGS
;
1867 /* Check for processor-specific section types. */
1868 if (bed
->elf_backend_fake_sections
)
1869 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1871 /* If the section has relocs, set up a section header for the
1872 SHT_REL[A] section. If two relocation sections are required for
1873 this section, it is up to the processor-specific back-end to
1874 create the other. */
1875 if ((asect
->flags
& SEC_RELOC
) != 0
1876 && !_bfd_elf_init_reloc_shdr (abfd
,
1877 &elf_section_data (asect
)->rel_hdr
,
1879 elf_section_data (asect
)->use_rela_p
))
1883 /* Assign all ELF section numbers. The dummy first section is handled here
1884 too. The link/info pointers for the standard section types are filled
1885 in here too, while we're at it. */
1888 assign_section_numbers (abfd
)
1891 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1893 unsigned int section_number
;
1894 Elf_Internal_Shdr
**i_shdrp
;
1899 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1901 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1903 d
->this_idx
= section_number
++;
1904 if ((sec
->flags
& SEC_RELOC
) == 0)
1907 d
->rel_idx
= section_number
++;
1910 d
->rel_idx2
= section_number
++;
1915 t
->shstrtab_section
= section_number
++;
1916 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1917 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1919 if (bfd_get_symcount (abfd
) > 0)
1921 t
->symtab_section
= section_number
++;
1922 t
->strtab_section
= section_number
++;
1925 elf_elfheader (abfd
)->e_shnum
= section_number
;
1927 /* Set up the list of section header pointers, in agreement with the
1929 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
1930 i_shdrp
= (Elf_Internal_Shdr
**) bfd_alloc (abfd
, amt
);
1931 if (i_shdrp
== NULL
)
1934 amt
= sizeof (Elf_Internal_Shdr
);
1935 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1936 if (i_shdrp
[0] == NULL
)
1938 bfd_release (abfd
, i_shdrp
);
1941 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1943 elf_elfsections (abfd
) = i_shdrp
;
1945 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1946 if (bfd_get_symcount (abfd
) > 0)
1948 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1949 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1950 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1952 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1954 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1958 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1959 if (d
->rel_idx
!= 0)
1960 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1961 if (d
->rel_idx2
!= 0)
1962 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
1964 /* Fill in the sh_link and sh_info fields while we're at it. */
1966 /* sh_link of a reloc section is the section index of the symbol
1967 table. sh_info is the section index of the section to which
1968 the relocation entries apply. */
1969 if (d
->rel_idx
!= 0)
1971 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1972 d
->rel_hdr
.sh_info
= d
->this_idx
;
1974 if (d
->rel_idx2
!= 0)
1976 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
1977 d
->rel_hdr2
->sh_info
= d
->this_idx
;
1980 switch (d
->this_hdr
.sh_type
)
1984 /* A reloc section which we are treating as a normal BFD
1985 section. sh_link is the section index of the symbol
1986 table. sh_info is the section index of the section to
1987 which the relocation entries apply. We assume that an
1988 allocated reloc section uses the dynamic symbol table.
1989 FIXME: How can we be sure? */
1990 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1992 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1994 /* We look up the section the relocs apply to by name. */
1996 if (d
->this_hdr
.sh_type
== SHT_REL
)
2000 s
= bfd_get_section_by_name (abfd
, name
);
2002 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2006 /* We assume that a section named .stab*str is a stabs
2007 string section. We look for a section with the same name
2008 but without the trailing ``str'', and set its sh_link
2009 field to point to this section. */
2010 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2011 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2016 len
= strlen (sec
->name
);
2017 alc
= (char *) bfd_malloc ((bfd_size_type
) len
- 2);
2020 strncpy (alc
, sec
->name
, len
- 3);
2021 alc
[len
- 3] = '\0';
2022 s
= bfd_get_section_by_name (abfd
, alc
);
2026 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2028 /* This is a .stab section. */
2029 elf_section_data (s
)->this_hdr
.sh_entsize
=
2030 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2037 case SHT_GNU_verneed
:
2038 case SHT_GNU_verdef
:
2039 /* sh_link is the section header index of the string table
2040 used for the dynamic entries, or the symbol table, or the
2042 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2044 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2048 case SHT_GNU_versym
:
2049 /* sh_link is the section header index of the symbol table
2050 this hash table or version table is for. */
2051 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2053 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2061 /* Map symbol from it's internal number to the external number, moving
2062 all local symbols to be at the head of the list. */
2065 sym_is_global (abfd
, sym
)
2069 /* If the backend has a special mapping, use it. */
2070 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2071 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2074 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2075 || bfd_is_und_section (bfd_get_section (sym
))
2076 || bfd_is_com_section (bfd_get_section (sym
)));
2080 elf_map_symbols (abfd
)
2083 unsigned int symcount
= bfd_get_symcount (abfd
);
2084 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2085 asymbol
**sect_syms
;
2086 unsigned int num_locals
= 0;
2087 unsigned int num_globals
= 0;
2088 unsigned int num_locals2
= 0;
2089 unsigned int num_globals2
= 0;
2091 unsigned int num_sections
= 0;
2098 fprintf (stderr
, "elf_map_symbols\n");
2102 /* Add a section symbol for each BFD section. FIXME: Is this really
2104 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2106 if (max_index
< asect
->index
)
2107 max_index
= asect
->index
;
2111 amt
= max_index
* sizeof (asymbol
*);
2112 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2113 if (sect_syms
== NULL
)
2115 elf_section_syms (abfd
) = sect_syms
;
2116 elf_num_section_syms (abfd
) = max_index
;
2118 for (idx
= 0; idx
< symcount
; idx
++)
2120 asymbol
*sym
= syms
[idx
];
2122 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2129 if (sec
->owner
!= NULL
)
2131 if (sec
->owner
!= abfd
)
2133 if (sec
->output_offset
!= 0)
2136 sec
= sec
->output_section
;
2138 /* Empty sections in the input files may have had a section
2139 symbol created for them. (See the comment near the end of
2140 _bfd_generic_link_output_symbols in linker.c). If the linker
2141 script discards such sections then we will reach this point.
2142 Since we know that we cannot avoid this case, we detect it
2143 and skip the abort and the assignment to the sect_syms array.
2144 To reproduce this particular case try running the linker
2145 testsuite test ld-scripts/weak.exp for an ELF port that uses
2146 the generic linker. */
2147 if (sec
->owner
== NULL
)
2150 BFD_ASSERT (sec
->owner
== abfd
);
2152 sect_syms
[sec
->index
] = syms
[idx
];
2157 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2161 if (sect_syms
[asect
->index
] != NULL
)
2164 sym
= bfd_make_empty_symbol (abfd
);
2167 sym
->the_bfd
= abfd
;
2168 sym
->name
= asect
->name
;
2170 /* Set the flags to 0 to indicate that this one was newly added. */
2172 sym
->section
= asect
;
2173 sect_syms
[asect
->index
] = sym
;
2177 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2178 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
2182 /* Classify all of the symbols. */
2183 for (idx
= 0; idx
< symcount
; idx
++)
2185 if (!sym_is_global (abfd
, syms
[idx
]))
2190 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2192 if (sect_syms
[asect
->index
] != NULL
2193 && sect_syms
[asect
->index
]->flags
== 0)
2195 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
2196 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
2200 sect_syms
[asect
->index
]->flags
= 0;
2204 /* Now sort the symbols so the local symbols are first. */
2205 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2206 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2208 if (new_syms
== NULL
)
2211 for (idx
= 0; idx
< symcount
; idx
++)
2213 asymbol
*sym
= syms
[idx
];
2216 if (!sym_is_global (abfd
, sym
))
2219 i
= num_locals
+ num_globals2
++;
2221 sym
->udata
.i
= i
+ 1;
2223 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2225 if (sect_syms
[asect
->index
] != NULL
2226 && sect_syms
[asect
->index
]->flags
== 0)
2228 asymbol
*sym
= sect_syms
[asect
->index
];
2231 sym
->flags
= BSF_SECTION_SYM
;
2232 if (!sym_is_global (abfd
, sym
))
2235 i
= num_locals
+ num_globals2
++;
2237 sym
->udata
.i
= i
+ 1;
2241 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2243 elf_num_locals (abfd
) = num_locals
;
2244 elf_num_globals (abfd
) = num_globals
;
2248 /* Align to the maximum file alignment that could be required for any
2249 ELF data structure. */
2251 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2252 static INLINE file_ptr
2253 align_file_position (off
, align
)
2257 return (off
+ align
- 1) & ~(align
- 1);
2260 /* Assign a file position to a section, optionally aligning to the
2261 required section alignment. */
2264 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2265 Elf_Internal_Shdr
*i_shdrp
;
2273 al
= i_shdrp
->sh_addralign
;
2275 offset
= BFD_ALIGN (offset
, al
);
2277 i_shdrp
->sh_offset
= offset
;
2278 if (i_shdrp
->bfd_section
!= NULL
)
2279 i_shdrp
->bfd_section
->filepos
= offset
;
2280 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2281 offset
+= i_shdrp
->sh_size
;
2285 /* Compute the file positions we are going to put the sections at, and
2286 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2287 is not NULL, this is being called by the ELF backend linker. */
2290 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2292 struct bfd_link_info
*link_info
;
2294 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2296 struct bfd_strtab_hash
*strtab
;
2297 Elf_Internal_Shdr
*shstrtab_hdr
;
2299 if (abfd
->output_has_begun
)
2302 /* Do any elf backend specific processing first. */
2303 if (bed
->elf_backend_begin_write_processing
)
2304 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2306 if (! prep_headers (abfd
))
2309 /* Post process the headers if necessary. */
2310 if (bed
->elf_backend_post_process_headers
)
2311 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2314 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2318 if (!assign_section_numbers (abfd
))
2321 /* The backend linker builds symbol table information itself. */
2322 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2324 /* Non-zero if doing a relocatable link. */
2325 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2327 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2331 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2332 /* sh_name was set in prep_headers. */
2333 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2334 shstrtab_hdr
->sh_flags
= 0;
2335 shstrtab_hdr
->sh_addr
= 0;
2336 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2337 shstrtab_hdr
->sh_entsize
= 0;
2338 shstrtab_hdr
->sh_link
= 0;
2339 shstrtab_hdr
->sh_info
= 0;
2340 /* sh_offset is set in assign_file_positions_except_relocs. */
2341 shstrtab_hdr
->sh_addralign
= 1;
2343 if (!assign_file_positions_except_relocs (abfd
))
2346 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2349 Elf_Internal_Shdr
*hdr
;
2351 off
= elf_tdata (abfd
)->next_file_pos
;
2353 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2354 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2356 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2357 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2359 elf_tdata (abfd
)->next_file_pos
= off
;
2361 /* Now that we know where the .strtab section goes, write it
2363 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2364 || ! _bfd_stringtab_emit (abfd
, strtab
))
2366 _bfd_stringtab_free (strtab
);
2369 abfd
->output_has_begun
= true;
2374 /* Create a mapping from a set of sections to a program segment. */
2376 static INLINE
struct elf_segment_map
*
2377 make_mapping (abfd
, sections
, from
, to
, phdr
)
2379 asection
**sections
;
2384 struct elf_segment_map
*m
;
2389 amt
= sizeof (struct elf_segment_map
);
2390 amt
+= (to
- from
- 1) * sizeof (asection
*);
2391 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2395 m
->p_type
= PT_LOAD
;
2396 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2397 m
->sections
[i
- from
] = *hdrpp
;
2398 m
->count
= to
- from
;
2400 if (from
== 0 && phdr
)
2402 /* Include the headers in the first PT_LOAD segment. */
2403 m
->includes_filehdr
= 1;
2404 m
->includes_phdrs
= 1;
2410 /* Set up a mapping from BFD sections to program segments. */
2413 map_sections_to_segments (abfd
)
2416 asection
**sections
= NULL
;
2420 struct elf_segment_map
*mfirst
;
2421 struct elf_segment_map
**pm
;
2422 struct elf_segment_map
*m
;
2424 unsigned int phdr_index
;
2425 bfd_vma maxpagesize
;
2427 boolean phdr_in_segment
= true;
2432 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2435 if (bfd_count_sections (abfd
) == 0)
2438 /* Select the allocated sections, and sort them. */
2440 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
2441 sections
= (asection
**) bfd_malloc (amt
);
2442 if (sections
== NULL
)
2446 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2448 if ((s
->flags
& SEC_ALLOC
) != 0)
2454 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2457 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2459 /* Build the mapping. */
2464 /* If we have a .interp section, then create a PT_PHDR segment for
2465 the program headers and a PT_INTERP segment for the .interp
2467 s
= bfd_get_section_by_name (abfd
, ".interp");
2468 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2470 amt
= sizeof (struct elf_segment_map
);
2471 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2475 m
->p_type
= PT_PHDR
;
2476 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2477 m
->p_flags
= PF_R
| PF_X
;
2478 m
->p_flags_valid
= 1;
2479 m
->includes_phdrs
= 1;
2484 amt
= sizeof (struct elf_segment_map
);
2485 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2489 m
->p_type
= PT_INTERP
;
2497 /* Look through the sections. We put sections in the same program
2498 segment when the start of the second section can be placed within
2499 a few bytes of the end of the first section. */
2502 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2504 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2506 && (dynsec
->flags
& SEC_LOAD
) == 0)
2509 /* Deal with -Ttext or something similar such that the first section
2510 is not adjacent to the program headers. This is an
2511 approximation, since at this point we don't know exactly how many
2512 program headers we will need. */
2515 bfd_size_type phdr_size
;
2517 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2519 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2520 if ((abfd
->flags
& D_PAGED
) == 0
2521 || sections
[0]->lma
< phdr_size
2522 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2523 phdr_in_segment
= false;
2526 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2529 boolean new_segment
;
2533 /* See if this section and the last one will fit in the same
2536 if (last_hdr
== NULL
)
2538 /* If we don't have a segment yet, then we don't need a new
2539 one (we build the last one after this loop). */
2540 new_segment
= false;
2542 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2544 /* If this section has a different relation between the
2545 virtual address and the load address, then we need a new
2549 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2550 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2552 /* If putting this section in this segment would force us to
2553 skip a page in the segment, then we need a new segment. */
2556 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2557 && (hdr
->flags
& SEC_LOAD
) != 0)
2559 /* We don't want to put a loadable section after a
2560 nonloadable section in the same segment. */
2563 else if ((abfd
->flags
& D_PAGED
) == 0)
2565 /* If the file is not demand paged, which means that we
2566 don't require the sections to be correctly aligned in the
2567 file, then there is no other reason for a new segment. */
2568 new_segment
= false;
2571 && (hdr
->flags
& SEC_READONLY
) == 0
2572 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2575 /* We don't want to put a writable section in a read only
2576 segment, unless they are on the same page in memory
2577 anyhow. We already know that the last section does not
2578 bring us past the current section on the page, so the
2579 only case in which the new section is not on the same
2580 page as the previous section is when the previous section
2581 ends precisely on a page boundary. */
2586 /* Otherwise, we can use the same segment. */
2587 new_segment
= false;
2592 if ((hdr
->flags
& SEC_READONLY
) == 0)
2598 /* We need a new program segment. We must create a new program
2599 header holding all the sections from phdr_index until hdr. */
2601 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2608 if ((hdr
->flags
& SEC_READONLY
) == 0)
2615 phdr_in_segment
= false;
2618 /* Create a final PT_LOAD program segment. */
2619 if (last_hdr
!= NULL
)
2621 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2629 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2632 amt
= sizeof (struct elf_segment_map
);
2633 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2637 m
->p_type
= PT_DYNAMIC
;
2639 m
->sections
[0] = dynsec
;
2645 /* For each loadable .note section, add a PT_NOTE segment. We don't
2646 use bfd_get_section_by_name, because if we link together
2647 nonloadable .note sections and loadable .note sections, we will
2648 generate two .note sections in the output file. FIXME: Using
2649 names for section types is bogus anyhow. */
2650 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2652 if ((s
->flags
& SEC_LOAD
) != 0
2653 && strncmp (s
->name
, ".note", 5) == 0)
2655 amt
= sizeof (struct elf_segment_map
);
2656 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2660 m
->p_type
= PT_NOTE
;
2672 elf_tdata (abfd
)->segment_map
= mfirst
;
2676 if (sections
!= NULL
)
2681 /* Sort sections by address. */
2684 elf_sort_sections (arg1
, arg2
)
2688 const asection
*sec1
= *(const asection
**) arg1
;
2689 const asection
*sec2
= *(const asection
**) arg2
;
2691 /* Sort by LMA first, since this is the address used to
2692 place the section into a segment. */
2693 if (sec1
->lma
< sec2
->lma
)
2695 else if (sec1
->lma
> sec2
->lma
)
2698 /* Then sort by VMA. Normally the LMA and the VMA will be
2699 the same, and this will do nothing. */
2700 if (sec1
->vma
< sec2
->vma
)
2702 else if (sec1
->vma
> sec2
->vma
)
2705 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2707 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2713 /* If the indicies are the same, do not return 0
2714 here, but continue to try the next comparison. */
2715 if (sec1
->target_index
- sec2
->target_index
!= 0)
2716 return sec1
->target_index
- sec2
->target_index
;
2721 else if (TOEND (sec2
))
2726 /* Sort by size, to put zero sized sections
2727 before others at the same address. */
2729 if (sec1
->_raw_size
< sec2
->_raw_size
)
2731 if (sec1
->_raw_size
> sec2
->_raw_size
)
2734 return sec1
->target_index
- sec2
->target_index
;
2737 /* Assign file positions to the sections based on the mapping from
2738 sections to segments. This function also sets up some fields in
2739 the file header, and writes out the program headers. */
2742 assign_file_positions_for_segments (abfd
)
2745 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2747 struct elf_segment_map
*m
;
2749 Elf_Internal_Phdr
*phdrs
;
2751 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2752 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2753 Elf_Internal_Phdr
*p
;
2756 if (elf_tdata (abfd
)->segment_map
== NULL
)
2758 if (! map_sections_to_segments (abfd
))
2762 if (bed
->elf_backend_modify_segment_map
)
2764 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2769 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2772 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2773 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2774 elf_elfheader (abfd
)->e_phnum
= count
;
2779 /* If we already counted the number of program segments, make sure
2780 that we allocated enough space. This happens when SIZEOF_HEADERS
2781 is used in a linker script. */
2782 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2783 if (alloc
!= 0 && count
> alloc
)
2785 ((*_bfd_error_handler
)
2786 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2787 bfd_get_filename (abfd
), alloc
, count
));
2788 bfd_set_error (bfd_error_bad_value
);
2795 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
2796 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
2800 off
= bed
->s
->sizeof_ehdr
;
2801 off
+= alloc
* bed
->s
->sizeof_phdr
;
2808 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2815 /* If elf_segment_map is not from map_sections_to_segments, the
2816 sections may not be correctly ordered. */
2818 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2821 p
->p_type
= m
->p_type
;
2822 p
->p_flags
= m
->p_flags
;
2824 if (p
->p_type
== PT_LOAD
2826 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2828 if ((abfd
->flags
& D_PAGED
) != 0)
2829 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2832 bfd_size_type align
;
2835 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2837 bfd_size_type secalign
;
2839 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
2840 if (secalign
> align
)
2844 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
2851 p
->p_vaddr
= m
->sections
[0]->vma
;
2853 if (m
->p_paddr_valid
)
2854 p
->p_paddr
= m
->p_paddr
;
2855 else if (m
->count
== 0)
2858 p
->p_paddr
= m
->sections
[0]->lma
;
2860 if (p
->p_type
== PT_LOAD
2861 && (abfd
->flags
& D_PAGED
) != 0)
2862 p
->p_align
= bed
->maxpagesize
;
2863 else if (m
->count
== 0)
2864 p
->p_align
= bed
->s
->file_align
;
2872 if (m
->includes_filehdr
)
2874 if (! m
->p_flags_valid
)
2877 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2878 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2881 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2883 if (p
->p_vaddr
< (bfd_vma
) off
)
2885 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2886 bfd_get_filename (abfd
));
2887 bfd_set_error (bfd_error_bad_value
);
2892 if (! m
->p_paddr_valid
)
2895 if (p
->p_type
== PT_LOAD
)
2897 filehdr_vaddr
= p
->p_vaddr
;
2898 filehdr_paddr
= p
->p_paddr
;
2902 if (m
->includes_phdrs
)
2904 if (! m
->p_flags_valid
)
2907 if (m
->includes_filehdr
)
2909 if (p
->p_type
== PT_LOAD
)
2911 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2912 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2917 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2921 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2922 p
->p_vaddr
-= off
- p
->p_offset
;
2923 if (! m
->p_paddr_valid
)
2924 p
->p_paddr
-= off
- p
->p_offset
;
2927 if (p
->p_type
== PT_LOAD
)
2929 phdrs_vaddr
= p
->p_vaddr
;
2930 phdrs_paddr
= p
->p_paddr
;
2933 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
2936 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2937 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2940 if (p
->p_type
== PT_LOAD
2941 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
2943 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2949 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2950 p
->p_filesz
+= adjust
;
2951 p
->p_memsz
+= adjust
;
2957 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2961 bfd_size_type align
;
2965 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2967 /* The section may have artificial alignment forced by a
2968 link script. Notice this case by the gap between the
2969 cumulative phdr vma and the section's vma. */
2970 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2972 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2974 p
->p_memsz
+= adjust
;
2977 if ((flags
& SEC_LOAD
) != 0)
2978 p
->p_filesz
+= adjust
;
2981 if (p
->p_type
== PT_LOAD
)
2983 bfd_signed_vma adjust
;
2985 if ((flags
& SEC_LOAD
) != 0)
2987 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2991 else if ((flags
& SEC_ALLOC
) != 0)
2993 /* The section VMA must equal the file position
2994 modulo the page size. FIXME: I'm not sure if
2995 this adjustment is really necessary. We used to
2996 not have the SEC_LOAD case just above, and then
2997 this was necessary, but now I'm not sure. */
2998 if ((abfd
->flags
& D_PAGED
) != 0)
2999 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3001 adjust
= (sec
->vma
- voff
) % align
;
3010 (* _bfd_error_handler
)
3011 (_("Error: First section in segment (%s) starts at 0x%x"),
3012 bfd_section_name (abfd
, sec
), sec
->lma
);
3013 (* _bfd_error_handler
)
3014 (_(" whereas segment starts at 0x%x"),
3019 p
->p_memsz
+= adjust
;
3022 if ((flags
& SEC_LOAD
) != 0)
3023 p
->p_filesz
+= adjust
;
3028 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3029 used in a linker script we may have a section with
3030 SEC_LOAD clear but which is supposed to have
3032 if ((flags
& SEC_LOAD
) != 0
3033 || (flags
& SEC_HAS_CONTENTS
) != 0)
3034 off
+= sec
->_raw_size
;
3036 if ((flags
& SEC_ALLOC
) != 0)
3037 voff
+= sec
->_raw_size
;
3040 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3042 /* The actual "note" segment has i == 0.
3043 This is the one that actually contains everything. */
3047 p
->p_filesz
= sec
->_raw_size
;
3048 off
+= sec
->_raw_size
;
3053 /* Fake sections -- don't need to be written. */
3056 flags
= sec
->flags
= 0;
3063 p
->p_memsz
+= sec
->_raw_size
;
3065 if ((flags
& SEC_LOAD
) != 0)
3066 p
->p_filesz
+= sec
->_raw_size
;
3068 if (align
> p
->p_align
3069 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3073 if (! m
->p_flags_valid
)
3076 if ((flags
& SEC_CODE
) != 0)
3078 if ((flags
& SEC_READONLY
) == 0)
3084 /* Now that we have set the section file positions, we can set up
3085 the file positions for the non PT_LOAD segments. */
3086 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3090 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3092 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3093 p
->p_offset
= m
->sections
[0]->filepos
;
3097 if (m
->includes_filehdr
)
3099 p
->p_vaddr
= filehdr_vaddr
;
3100 if (! m
->p_paddr_valid
)
3101 p
->p_paddr
= filehdr_paddr
;
3103 else if (m
->includes_phdrs
)
3105 p
->p_vaddr
= phdrs_vaddr
;
3106 if (! m
->p_paddr_valid
)
3107 p
->p_paddr
= phdrs_paddr
;
3112 /* Clear out any program headers we allocated but did not use. */
3113 for (; count
< alloc
; count
++, p
++)
3115 memset (p
, 0, sizeof *p
);
3116 p
->p_type
= PT_NULL
;
3119 elf_tdata (abfd
)->phdr
= phdrs
;
3121 elf_tdata (abfd
)->next_file_pos
= off
;
3123 /* Write out the program headers. */
3124 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3125 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3131 /* Get the size of the program header.
3133 If this is called by the linker before any of the section VMA's are set, it
3134 can't calculate the correct value for a strange memory layout. This only
3135 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3136 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3137 data segment (exclusive of .interp and .dynamic).
3139 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3140 will be two segments. */
3142 static bfd_size_type
3143 get_program_header_size (abfd
)
3148 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3150 /* We can't return a different result each time we're called. */
3151 if (elf_tdata (abfd
)->program_header_size
!= 0)
3152 return elf_tdata (abfd
)->program_header_size
;
3154 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3156 struct elf_segment_map
*m
;
3159 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3161 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3162 return elf_tdata (abfd
)->program_header_size
;
3165 /* Assume we will need exactly two PT_LOAD segments: one for text
3166 and one for data. */
3169 s
= bfd_get_section_by_name (abfd
, ".interp");
3170 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3172 /* If we have a loadable interpreter section, we need a
3173 PT_INTERP segment. In this case, assume we also need a
3174 PT_PHDR segment, although that may not be true for all
3179 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3181 /* We need a PT_DYNAMIC segment. */
3185 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3187 if ((s
->flags
& SEC_LOAD
) != 0
3188 && strncmp (s
->name
, ".note", 5) == 0)
3190 /* We need a PT_NOTE segment. */
3195 /* Let the backend count up any program headers it might need. */
3196 if (bed
->elf_backend_additional_program_headers
)
3200 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3206 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3207 return elf_tdata (abfd
)->program_header_size
;
3210 /* Work out the file positions of all the sections. This is called by
3211 _bfd_elf_compute_section_file_positions. All the section sizes and
3212 VMAs must be known before this is called.
3214 We do not consider reloc sections at this point, unless they form
3215 part of the loadable image. Reloc sections are assigned file
3216 positions in assign_file_positions_for_relocs, which is called by
3217 write_object_contents and final_link.
3219 We also don't set the positions of the .symtab and .strtab here. */
3222 assign_file_positions_except_relocs (abfd
)
3225 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3226 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3227 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3229 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3231 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3232 && bfd_get_format (abfd
) != bfd_core
)
3234 Elf_Internal_Shdr
**hdrpp
;
3237 /* Start after the ELF header. */
3238 off
= i_ehdrp
->e_ehsize
;
3240 /* We are not creating an executable, which means that we are
3241 not creating a program header, and that the actual order of
3242 the sections in the file is unimportant. */
3243 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3245 Elf_Internal_Shdr
*hdr
;
3248 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
3250 hdr
->sh_offset
= -1;
3253 if (i
== tdata
->symtab_section
3254 || i
== tdata
->strtab_section
)
3256 hdr
->sh_offset
= -1;
3260 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3266 Elf_Internal_Shdr
**hdrpp
;
3268 /* Assign file positions for the loaded sections based on the
3269 assignment of sections to segments. */
3270 if (! assign_file_positions_for_segments (abfd
))
3273 /* Assign file positions for the other sections. */
3275 off
= elf_tdata (abfd
)->next_file_pos
;
3276 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3278 Elf_Internal_Shdr
*hdr
;
3281 if (hdr
->bfd_section
!= NULL
3282 && hdr
->bfd_section
->filepos
!= 0)
3283 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3284 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3286 ((*_bfd_error_handler
)
3287 (_("%s: warning: allocated section `%s' not in segment"),
3288 bfd_get_filename (abfd
),
3289 (hdr
->bfd_section
== NULL
3291 : hdr
->bfd_section
->name
)));
3292 if ((abfd
->flags
& D_PAGED
) != 0)
3293 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3295 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3296 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3299 else if (hdr
->sh_type
== SHT_REL
3300 || hdr
->sh_type
== SHT_RELA
3301 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3302 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3303 hdr
->sh_offset
= -1;
3305 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3309 /* Place the section headers. */
3310 off
= align_file_position (off
, bed
->s
->file_align
);
3311 i_ehdrp
->e_shoff
= off
;
3312 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3314 elf_tdata (abfd
)->next_file_pos
= off
;
3323 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3324 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3325 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3327 struct bfd_strtab_hash
*shstrtab
;
3328 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3330 i_ehdrp
= elf_elfheader (abfd
);
3331 i_shdrp
= elf_elfsections (abfd
);
3333 shstrtab
= _bfd_elf_stringtab_init ();
3334 if (shstrtab
== NULL
)
3337 elf_shstrtab (abfd
) = shstrtab
;
3339 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3340 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3341 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3342 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3344 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3345 i_ehdrp
->e_ident
[EI_DATA
] =
3346 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3347 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3349 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3350 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3352 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3353 i_ehdrp
->e_ident
[count
] = 0;
3355 if ((abfd
->flags
& DYNAMIC
) != 0)
3356 i_ehdrp
->e_type
= ET_DYN
;
3357 else if ((abfd
->flags
& EXEC_P
) != 0)
3358 i_ehdrp
->e_type
= ET_EXEC
;
3359 else if (bfd_get_format (abfd
) == bfd_core
)
3360 i_ehdrp
->e_type
= ET_CORE
;
3362 i_ehdrp
->e_type
= ET_REL
;
3364 switch (bfd_get_arch (abfd
))
3366 case bfd_arch_unknown
:
3367 i_ehdrp
->e_machine
= EM_NONE
;
3370 /* There used to be a long list of cases here, each one setting
3371 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3372 in the corresponding bfd definition. To avoid duplication,
3373 the switch was removed. Machines that need special handling
3374 can generally do it in elf_backend_final_write_processing(),
3375 unless they need the information earlier than the final write.
3376 Such need can generally be supplied by replacing the tests for
3377 e_machine with the conditions used to determine it. */
3379 if (get_elf_backend_data (abfd
) != NULL
)
3380 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
3382 i_ehdrp
->e_machine
= EM_NONE
;
3385 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3386 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3388 /* No program header, for now. */
3389 i_ehdrp
->e_phoff
= 0;
3390 i_ehdrp
->e_phentsize
= 0;
3391 i_ehdrp
->e_phnum
= 0;
3393 /* Each bfd section is section header entry. */
3394 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3395 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3397 /* If we're building an executable, we'll need a program header table. */
3398 if (abfd
->flags
& EXEC_P
)
3400 /* It all happens later. */
3402 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3404 /* elf_build_phdrs() returns a (NULL-terminated) array of
3405 Elf_Internal_Phdrs. */
3406 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3407 i_ehdrp
->e_phoff
= outbase
;
3408 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3413 i_ehdrp
->e_phentsize
= 0;
3415 i_ehdrp
->e_phoff
= 0;
3418 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3419 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3420 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3421 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3422 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3423 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3424 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3425 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3426 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3432 /* Assign file positions for all the reloc sections which are not part
3433 of the loadable file image. */
3436 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3441 Elf_Internal_Shdr
**shdrpp
;
3443 off
= elf_tdata (abfd
)->next_file_pos
;
3445 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3446 i
< elf_elfheader (abfd
)->e_shnum
;
3449 Elf_Internal_Shdr
*shdrp
;
3452 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3453 && shdrp
->sh_offset
== -1)
3454 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3457 elf_tdata (abfd
)->next_file_pos
= off
;
3461 _bfd_elf_write_object_contents (abfd
)
3464 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3465 Elf_Internal_Ehdr
*i_ehdrp
;
3466 Elf_Internal_Shdr
**i_shdrp
;
3470 if (! abfd
->output_has_begun
3471 && ! _bfd_elf_compute_section_file_positions
3472 (abfd
, (struct bfd_link_info
*) NULL
))
3475 i_shdrp
= elf_elfsections (abfd
);
3476 i_ehdrp
= elf_elfheader (abfd
);
3479 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3483 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3485 /* After writing the headers, we need to write the sections too... */
3486 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3488 if (bed
->elf_backend_section_processing
)
3489 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3490 if (i_shdrp
[count
]->contents
)
3492 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
3494 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3495 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
3500 /* Write out the section header names. */
3501 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3502 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3505 if (bed
->elf_backend_final_write_processing
)
3506 (*bed
->elf_backend_final_write_processing
) (abfd
,
3507 elf_tdata (abfd
)->linker
);
3509 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3513 _bfd_elf_write_corefile_contents (abfd
)
3516 /* Hopefully this can be done just like an object file. */
3517 return _bfd_elf_write_object_contents (abfd
);
3520 /* Given a section, search the header to find them. */
3523 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3527 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3528 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3530 Elf_Internal_Shdr
*hdr
;
3531 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3533 for (index
= 0; index
< maxindex
; index
++)
3535 hdr
= i_shdrp
[index
];
3536 if (hdr
->bfd_section
== asect
)
3540 if (bed
->elf_backend_section_from_bfd_section
)
3542 for (index
= 0; index
< maxindex
; index
++)
3546 hdr
= i_shdrp
[index
];
3548 if ((*bed
->elf_backend_section_from_bfd_section
)
3549 (abfd
, hdr
, asect
, &retval
))
3554 if (bfd_is_abs_section (asect
))
3556 if (bfd_is_com_section (asect
))
3558 if (bfd_is_und_section (asect
))
3561 bfd_set_error (bfd_error_nonrepresentable_section
);
3566 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3570 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3572 asymbol
**asym_ptr_ptr
;
3574 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3576 flagword flags
= asym_ptr
->flags
;
3578 /* When gas creates relocations against local labels, it creates its
3579 own symbol for the section, but does put the symbol into the
3580 symbol chain, so udata is 0. When the linker is generating
3581 relocatable output, this section symbol may be for one of the
3582 input sections rather than the output section. */
3583 if (asym_ptr
->udata
.i
== 0
3584 && (flags
& BSF_SECTION_SYM
)
3585 && asym_ptr
->section
)
3589 if (asym_ptr
->section
->output_section
!= NULL
)
3590 indx
= asym_ptr
->section
->output_section
->index
;
3592 indx
= asym_ptr
->section
->index
;
3593 if (indx
< elf_num_section_syms (abfd
)
3594 && elf_section_syms (abfd
)[indx
] != NULL
)
3595 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3598 idx
= asym_ptr
->udata
.i
;
3602 /* This case can occur when using --strip-symbol on a symbol
3603 which is used in a relocation entry. */
3604 (*_bfd_error_handler
)
3605 (_("%s: symbol `%s' required but not present"),
3606 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3607 bfd_set_error (bfd_error_no_symbols
);
3614 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3615 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3616 elf_symbol_flags (flags
));
3624 /* Copy private BFD data. This copies any program header information. */
3627 copy_private_bfd_data (ibfd
, obfd
)
3631 Elf_Internal_Ehdr
* iehdr
;
3632 struct elf_segment_map
* map
;
3633 struct elf_segment_map
* map_first
;
3634 struct elf_segment_map
** pointer_to_map
;
3635 Elf_Internal_Phdr
* segment
;
3638 unsigned int num_segments
;
3639 boolean phdr_included
= false;
3640 bfd_vma maxpagesize
;
3641 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
3642 unsigned int phdr_adjust_num
= 0;
3644 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3645 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3648 if (elf_tdata (ibfd
)->phdr
== NULL
)
3651 iehdr
= elf_elfheader (ibfd
);
3654 pointer_to_map
= &map_first
;
3656 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3657 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3659 /* Returns the end address of the segment + 1. */
3660 #define SEGMENT_END(segment, start) \
3661 (start + (segment->p_memsz > segment->p_filesz \
3662 ? segment->p_memsz : segment->p_filesz))
3664 /* Returns true if the given section is contained within
3665 the given segment. VMA addresses are compared. */
3666 #define IS_CONTAINED_BY_VMA(section, segment) \
3667 (section->vma >= segment->p_vaddr \
3668 && (section->vma + section->_raw_size) \
3669 <= (SEGMENT_END (segment, segment->p_vaddr)))
3671 /* Returns true if the given section is contained within
3672 the given segment. LMA addresses are compared. */
3673 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3674 (section->lma >= base \
3675 && (section->lma + section->_raw_size) \
3676 <= SEGMENT_END (segment, base))
3678 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3679 #define IS_COREFILE_NOTE(p, s) \
3680 (p->p_type == PT_NOTE \
3681 && bfd_get_format (ibfd) == bfd_core \
3682 && s->vma == 0 && s->lma == 0 \
3683 && (bfd_vma) s->filepos >= p->p_offset \
3684 && (bfd_vma) s->filepos + s->_raw_size \
3685 <= p->p_offset + p->p_filesz)
3687 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3688 linker, which generates a PT_INTERP section with p_vaddr and
3689 p_memsz set to 0. */
3690 #define IS_SOLARIS_PT_INTERP(p, s) \
3692 && p->p_filesz > 0 \
3693 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3694 && s->_raw_size > 0 \
3695 && (bfd_vma) s->filepos >= p->p_offset \
3696 && ((bfd_vma) s->filepos + s->_raw_size \
3697 <= p->p_offset + p->p_filesz))
3699 /* Decide if the given section should be included in the given segment.
3700 A section will be included if:
3701 1. It is within the address space of the segment,
3702 2. It is an allocated segment,
3703 3. There is an output section associated with it,
3704 4. The section has not already been allocated to a previous segment. */
3705 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3706 ((((IS_CONTAINED_BY_VMA (section, segment) \
3707 || IS_SOLARIS_PT_INTERP (segment, section)) \
3708 && (section->flags & SEC_ALLOC) != 0) \
3709 || IS_COREFILE_NOTE (segment, section)) \
3710 && section->output_section != NULL \
3711 && section->segment_mark == false)
3713 /* Returns true iff seg1 starts after the end of seg2. */
3714 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3715 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3717 /* Returns true iff seg1 and seg2 overlap. */
3718 #define SEGMENT_OVERLAPS(seg1, seg2) \
3719 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3721 /* Initialise the segment mark field. */
3722 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3723 section
->segment_mark
= false;
3725 /* Scan through the segments specified in the program header
3726 of the input BFD. For this first scan we look for overlaps
3727 in the loadable segments. These can be created by wierd
3728 parameters to objcopy. */
3729 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3734 Elf_Internal_Phdr
*segment2
;
3736 if (segment
->p_type
!= PT_LOAD
)
3739 /* Determine if this segment overlaps any previous segments. */
3740 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
3742 bfd_signed_vma extra_length
;
3744 if (segment2
->p_type
!= PT_LOAD
3745 || ! SEGMENT_OVERLAPS (segment
, segment2
))
3748 /* Merge the two segments together. */
3749 if (segment2
->p_vaddr
< segment
->p_vaddr
)
3751 /* Extend SEGMENT2 to include SEGMENT and then delete
3754 SEGMENT_END (segment
, segment
->p_vaddr
)
3755 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
3757 if (extra_length
> 0)
3759 segment2
->p_memsz
+= extra_length
;
3760 segment2
->p_filesz
+= extra_length
;
3763 segment
->p_type
= PT_NULL
;
3765 /* Since we have deleted P we must restart the outer loop. */
3767 segment
= elf_tdata (ibfd
)->phdr
;
3772 /* Extend SEGMENT to include SEGMENT2 and then delete
3775 SEGMENT_END (segment2
, segment2
->p_vaddr
)
3776 - SEGMENT_END (segment
, segment
->p_vaddr
);
3778 if (extra_length
> 0)
3780 segment
->p_memsz
+= extra_length
;
3781 segment
->p_filesz
+= extra_length
;
3784 segment2
->p_type
= PT_NULL
;
3789 /* The second scan attempts to assign sections to segments. */
3790 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3794 unsigned int section_count
;
3795 asection
** sections
;
3796 asection
* output_section
;
3798 bfd_vma matching_lma
;
3799 bfd_vma suggested_lma
;
3803 if (segment
->p_type
== PT_NULL
)
3806 /* Compute how many sections might be placed into this segment. */
3808 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3809 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3812 /* Allocate a segment map big enough to contain all of the
3813 sections we have selected. */
3814 amt
= sizeof (struct elf_segment_map
);
3815 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
3816 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
3820 /* Initialise the fields of the segment map. Default to
3821 using the physical address of the segment in the input BFD. */
3823 map
->p_type
= segment
->p_type
;
3824 map
->p_flags
= segment
->p_flags
;
3825 map
->p_flags_valid
= 1;
3826 map
->p_paddr
= segment
->p_paddr
;
3827 map
->p_paddr_valid
= 1;
3829 /* Determine if this segment contains the ELF file header
3830 and if it contains the program headers themselves. */
3831 map
->includes_filehdr
= (segment
->p_offset
== 0
3832 && segment
->p_filesz
>= iehdr
->e_ehsize
);
3834 map
->includes_phdrs
= 0;
3836 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
3838 map
->includes_phdrs
=
3839 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3840 && (segment
->p_offset
+ segment
->p_filesz
3841 >= ((bfd_vma
) iehdr
->e_phoff
3842 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3844 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
3845 phdr_included
= true;
3848 if (section_count
== 0)
3850 /* Special segments, such as the PT_PHDR segment, may contain
3851 no sections, but ordinary, loadable segments should contain
3853 if (segment
->p_type
== PT_LOAD
)
3855 (_("%s: warning: Empty loadable segment detected\n"),
3856 bfd_archive_filename (ibfd
));
3859 *pointer_to_map
= map
;
3860 pointer_to_map
= &map
->next
;
3865 /* Now scan the sections in the input BFD again and attempt
3866 to add their corresponding output sections to the segment map.
3867 The problem here is how to handle an output section which has
3868 been moved (ie had its LMA changed). There are four possibilities:
3870 1. None of the sections have been moved.
3871 In this case we can continue to use the segment LMA from the
3874 2. All of the sections have been moved by the same amount.
3875 In this case we can change the segment's LMA to match the LMA
3876 of the first section.
3878 3. Some of the sections have been moved, others have not.
3879 In this case those sections which have not been moved can be
3880 placed in the current segment which will have to have its size,
3881 and possibly its LMA changed, and a new segment or segments will
3882 have to be created to contain the other sections.
3884 4. The sections have been moved, but not be the same amount.
3885 In this case we can change the segment's LMA to match the LMA
3886 of the first section and we will have to create a new segment
3887 or segments to contain the other sections.
3889 In order to save time, we allocate an array to hold the section
3890 pointers that we are interested in. As these sections get assigned
3891 to a segment, they are removed from this array. */
3893 amt
= (bfd_size_type
) section_count
* sizeof (asection
*);
3894 sections
= (asection
**) bfd_malloc (amt
);
3895 if (sections
== NULL
)
3898 /* Step One: Scan for segment vs section LMA conflicts.
3899 Also add the sections to the section array allocated above.
3900 Also add the sections to the current segment. In the common
3901 case, where the sections have not been moved, this means that
3902 we have completely filled the segment, and there is nothing
3908 for (j
= 0, section
= ibfd
->sections
;
3910 section
= section
->next
)
3912 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3914 output_section
= section
->output_section
;
3916 sections
[j
++] = section
;
3918 /* The Solaris native linker always sets p_paddr to 0.
3919 We try to catch that case here, and set it to the
3921 if (segment
->p_paddr
== 0
3922 && segment
->p_vaddr
!= 0
3924 && output_section
->lma
!= 0
3925 && (output_section
->vma
== (segment
->p_vaddr
3926 + (map
->includes_filehdr
3929 + (map
->includes_phdrs
3930 ? iehdr
->e_phnum
* iehdr
->e_phentsize
3932 map
->p_paddr
= segment
->p_vaddr
;
3934 /* Match up the physical address of the segment with the
3935 LMA address of the output section. */
3936 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
3937 || IS_COREFILE_NOTE (segment
, section
))
3939 if (matching_lma
== 0)
3940 matching_lma
= output_section
->lma
;
3942 /* We assume that if the section fits within the segment
3943 then it does not overlap any other section within that
3945 map
->sections
[isec
++] = output_section
;
3947 else if (suggested_lma
== 0)
3948 suggested_lma
= output_section
->lma
;
3952 BFD_ASSERT (j
== section_count
);
3954 /* Step Two: Adjust the physical address of the current segment,
3956 if (isec
== section_count
)
3958 /* All of the sections fitted within the segment as currently
3959 specified. This is the default case. Add the segment to
3960 the list of built segments and carry on to process the next
3961 program header in the input BFD. */
3962 map
->count
= section_count
;
3963 *pointer_to_map
= map
;
3964 pointer_to_map
= &map
->next
;
3971 if (matching_lma
!= 0)
3973 /* At least one section fits inside the current segment.
3974 Keep it, but modify its physical address to match the
3975 LMA of the first section that fitted. */
3976 map
->p_paddr
= matching_lma
;
3980 /* None of the sections fitted inside the current segment.
3981 Change the current segment's physical address to match
3982 the LMA of the first section. */
3983 map
->p_paddr
= suggested_lma
;
3986 /* Offset the segment physical address from the lma
3987 to allow for space taken up by elf headers. */
3988 if (map
->includes_filehdr
)
3989 map
->p_paddr
-= iehdr
->e_ehsize
;
3991 if (map
->includes_phdrs
)
3993 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
3995 /* iehdr->e_phnum is just an estimate of the number
3996 of program headers that we will need. Make a note
3997 here of the number we used and the segment we chose
3998 to hold these headers, so that we can adjust the
3999 offset when we know the correct value. */
4000 phdr_adjust_num
= iehdr
->e_phnum
;
4001 phdr_adjust_seg
= map
;
4005 /* Step Three: Loop over the sections again, this time assigning
4006 those that fit to the current segment and remvoing them from the
4007 sections array; but making sure not to leave large gaps. Once all
4008 possible sections have been assigned to the current segment it is
4009 added to the list of built segments and if sections still remain
4010 to be assigned, a new segment is constructed before repeating
4018 /* Fill the current segment with sections that fit. */
4019 for (j
= 0; j
< section_count
; j
++)
4021 section
= sections
[j
];
4023 if (section
== NULL
)
4026 output_section
= section
->output_section
;
4028 BFD_ASSERT (output_section
!= NULL
);
4030 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4031 || IS_COREFILE_NOTE (segment
, section
))
4033 if (map
->count
== 0)
4035 /* If the first section in a segment does not start at
4036 the beginning of the segment, then something is
4038 if (output_section
->lma
!=
4040 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4041 + (map
->includes_phdrs
4042 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4048 asection
* prev_sec
;
4050 prev_sec
= map
->sections
[map
->count
- 1];
4052 /* If the gap between the end of the previous section
4053 and the start of this section is more than
4054 maxpagesize then we need to start a new segment. */
4055 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
4056 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4057 || ((prev_sec
->lma
+ prev_sec
->_raw_size
) > output_section
->lma
))
4059 if (suggested_lma
== 0)
4060 suggested_lma
= output_section
->lma
;
4066 map
->sections
[map
->count
++] = output_section
;
4069 section
->segment_mark
= true;
4071 else if (suggested_lma
== 0)
4072 suggested_lma
= output_section
->lma
;
4075 BFD_ASSERT (map
->count
> 0);
4077 /* Add the current segment to the list of built segments. */
4078 *pointer_to_map
= map
;
4079 pointer_to_map
= &map
->next
;
4081 if (isec
< section_count
)
4083 /* We still have not allocated all of the sections to
4084 segments. Create a new segment here, initialise it
4085 and carry on looping. */
4086 amt
= sizeof (struct elf_segment_map
);
4087 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4088 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4092 /* Initialise the fields of the segment map. Set the physical
4093 physical address to the LMA of the first section that has
4094 not yet been assigned. */
4096 map
->p_type
= segment
->p_type
;
4097 map
->p_flags
= segment
->p_flags
;
4098 map
->p_flags_valid
= 1;
4099 map
->p_paddr
= suggested_lma
;
4100 map
->p_paddr_valid
= 1;
4101 map
->includes_filehdr
= 0;
4102 map
->includes_phdrs
= 0;
4105 while (isec
< section_count
);
4110 /* The Solaris linker creates program headers in which all the
4111 p_paddr fields are zero. When we try to objcopy or strip such a
4112 file, we get confused. Check for this case, and if we find it
4113 reset the p_paddr_valid fields. */
4114 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4115 if (map
->p_paddr
!= 0)
4119 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4120 map
->p_paddr_valid
= 0;
4123 elf_tdata (obfd
)->segment_map
= map_first
;
4125 /* If we had to estimate the number of program headers that were
4126 going to be needed, then check our estimate know and adjust
4127 the offset if necessary. */
4128 if (phdr_adjust_seg
!= NULL
)
4132 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4135 if (count
> phdr_adjust_num
)
4136 phdr_adjust_seg
->p_paddr
4137 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4141 /* Final Step: Sort the segments into ascending order of physical
4143 if (map_first
!= NULL
)
4145 struct elf_segment_map
*prev
;
4148 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4150 /* Yes I know - its a bubble sort.... */
4151 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4153 /* Swap map and map->next. */
4154 prev
->next
= map
->next
;
4155 map
->next
= map
->next
->next
;
4156 prev
->next
->next
= map
;
4166 #undef IS_CONTAINED_BY_VMA
4167 #undef IS_CONTAINED_BY_LMA
4168 #undef IS_COREFILE_NOTE
4169 #undef IS_SOLARIS_PT_INTERP
4170 #undef INCLUDE_SECTION_IN_SEGMENT
4171 #undef SEGMENT_AFTER_SEGMENT
4172 #undef SEGMENT_OVERLAPS
4176 /* Copy private section information. This copies over the entsize
4177 field, and sometimes the info field. */
4180 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4186 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4188 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4189 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4192 /* Copy over private BFD data if it has not already been copied.
4193 This must be done here, rather than in the copy_private_bfd_data
4194 entry point, because the latter is called after the section
4195 contents have been set, which means that the program headers have
4196 already been worked out. */
4197 if (elf_tdata (obfd
)->segment_map
== NULL
4198 && elf_tdata (ibfd
)->phdr
!= NULL
)
4202 /* Only set up the segments if there are no more SEC_ALLOC
4203 sections. FIXME: This won't do the right thing if objcopy is
4204 used to remove the last SEC_ALLOC section, since objcopy
4205 won't call this routine in that case. */
4206 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4207 if ((s
->flags
& SEC_ALLOC
) != 0)
4211 if (! copy_private_bfd_data (ibfd
, obfd
))
4216 ihdr
= &elf_section_data (isec
)->this_hdr
;
4217 ohdr
= &elf_section_data (osec
)->this_hdr
;
4219 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4221 if (ihdr
->sh_type
== SHT_SYMTAB
4222 || ihdr
->sh_type
== SHT_DYNSYM
4223 || ihdr
->sh_type
== SHT_GNU_verneed
4224 || ihdr
->sh_type
== SHT_GNU_verdef
)
4225 ohdr
->sh_info
= ihdr
->sh_info
;
4227 elf_section_data (osec
)->use_rela_p
4228 = elf_section_data (isec
)->use_rela_p
;
4233 /* Copy private symbol information. If this symbol is in a section
4234 which we did not map into a BFD section, try to map the section
4235 index correctly. We use special macro definitions for the mapped
4236 section indices; these definitions are interpreted by the
4237 swap_out_syms function. */
4239 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4240 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4241 #define MAP_STRTAB (SHN_LORESERVE - 3)
4242 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4245 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4251 elf_symbol_type
*isym
, *osym
;
4253 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4254 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4257 isym
= elf_symbol_from (ibfd
, isymarg
);
4258 osym
= elf_symbol_from (obfd
, osymarg
);
4262 && bfd_is_abs_section (isym
->symbol
.section
))
4266 shndx
= isym
->internal_elf_sym
.st_shndx
;
4267 if (shndx
== elf_onesymtab (ibfd
))
4268 shndx
= MAP_ONESYMTAB
;
4269 else if (shndx
== elf_dynsymtab (ibfd
))
4270 shndx
= MAP_DYNSYMTAB
;
4271 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4273 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4274 shndx
= MAP_SHSTRTAB
;
4275 osym
->internal_elf_sym
.st_shndx
= shndx
;
4281 /* Swap out the symbols. */
4284 swap_out_syms (abfd
, sttp
, relocatable_p
)
4286 struct bfd_strtab_hash
**sttp
;
4289 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4291 if (!elf_map_symbols (abfd
))
4294 /* Dump out the symtabs. */
4296 int symcount
= bfd_get_symcount (abfd
);
4297 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4298 struct bfd_strtab_hash
*stt
;
4299 Elf_Internal_Shdr
*symtab_hdr
;
4300 Elf_Internal_Shdr
*symstrtab_hdr
;
4301 char *outbound_syms
;
4305 stt
= _bfd_elf_stringtab_init ();
4309 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4310 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4311 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4312 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4313 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4314 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4316 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4317 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4319 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
4320 outbound_syms
= bfd_alloc (abfd
, amt
);
4321 if (outbound_syms
== NULL
)
4323 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4325 /* now generate the data (for "contents") */
4327 /* Fill in zeroth symbol and swap it out. */
4328 Elf_Internal_Sym sym
;
4334 sym
.st_shndx
= SHN_UNDEF
;
4335 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4336 outbound_syms
+= bed
->s
->sizeof_sym
;
4338 for (idx
= 0; idx
< symcount
; idx
++)
4340 Elf_Internal_Sym sym
;
4341 bfd_vma value
= syms
[idx
]->value
;
4342 elf_symbol_type
*type_ptr
;
4343 flagword flags
= syms
[idx
]->flags
;
4346 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
4348 /* Local section symbols have no name. */
4353 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4356 if (sym
.st_name
== (unsigned long) -1)
4360 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4362 if ((flags
& BSF_SECTION_SYM
) == 0
4363 && bfd_is_com_section (syms
[idx
]->section
))
4365 /* ELF common symbols put the alignment into the `value' field,
4366 and the size into the `size' field. This is backwards from
4367 how BFD handles it, so reverse it here. */
4368 sym
.st_size
= value
;
4369 if (type_ptr
== NULL
4370 || type_ptr
->internal_elf_sym
.st_value
== 0)
4371 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4373 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4374 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4375 (abfd
, syms
[idx
]->section
);
4379 asection
*sec
= syms
[idx
]->section
;
4382 if (sec
->output_section
)
4384 value
+= sec
->output_offset
;
4385 sec
= sec
->output_section
;
4387 /* Don't add in the section vma for relocatable output. */
4388 if (! relocatable_p
)
4390 sym
.st_value
= value
;
4391 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4393 if (bfd_is_abs_section (sec
)
4395 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4397 /* This symbol is in a real ELF section which we did
4398 not create as a BFD section. Undo the mapping done
4399 by copy_private_symbol_data. */
4400 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4404 shndx
= elf_onesymtab (abfd
);
4407 shndx
= elf_dynsymtab (abfd
);
4410 shndx
= elf_tdata (abfd
)->strtab_section
;
4413 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4421 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4427 /* Writing this would be a hell of a lot easier if
4428 we had some decent documentation on bfd, and
4429 knew what to expect of the library, and what to
4430 demand of applications. For example, it
4431 appears that `objcopy' might not set the
4432 section of a symbol to be a section that is
4433 actually in the output file. */
4434 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4435 BFD_ASSERT (sec2
!= 0);
4436 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
4437 BFD_ASSERT (shndx
!= -1);
4441 sym
.st_shndx
= shndx
;
4444 if ((flags
& BSF_FUNCTION
) != 0)
4446 else if ((flags
& BSF_OBJECT
) != 0)
4451 /* Processor-specific types */
4452 if (type_ptr
!= NULL
4453 && bed
->elf_backend_get_symbol_type
)
4454 type
= (*bed
->elf_backend_get_symbol_type
) (&type_ptr
->internal_elf_sym
, type
);
4456 if (flags
& BSF_SECTION_SYM
)
4458 if (flags
& BSF_GLOBAL
)
4459 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
4461 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4463 else if (bfd_is_com_section (syms
[idx
]->section
))
4464 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4465 else if (bfd_is_und_section (syms
[idx
]->section
))
4466 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4470 else if (flags
& BSF_FILE
)
4471 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4474 int bind
= STB_LOCAL
;
4476 if (flags
& BSF_LOCAL
)
4478 else if (flags
& BSF_WEAK
)
4480 else if (flags
& BSF_GLOBAL
)
4483 sym
.st_info
= ELF_ST_INFO (bind
, type
);
4486 if (type_ptr
!= NULL
)
4487 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
4491 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4492 outbound_syms
+= bed
->s
->sizeof_sym
;
4496 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
4497 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4499 symstrtab_hdr
->sh_flags
= 0;
4500 symstrtab_hdr
->sh_addr
= 0;
4501 symstrtab_hdr
->sh_entsize
= 0;
4502 symstrtab_hdr
->sh_link
= 0;
4503 symstrtab_hdr
->sh_info
= 0;
4504 symstrtab_hdr
->sh_addralign
= 1;
4510 /* Return the number of bytes required to hold the symtab vector.
4512 Note that we base it on the count plus 1, since we will null terminate
4513 the vector allocated based on this size. However, the ELF symbol table
4514 always has a dummy entry as symbol #0, so it ends up even. */
4517 _bfd_elf_get_symtab_upper_bound (abfd
)
4522 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4524 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4525 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4531 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4536 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4538 if (elf_dynsymtab (abfd
) == 0)
4540 bfd_set_error (bfd_error_invalid_operation
);
4544 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4545 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4551 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4552 bfd
*abfd ATTRIBUTE_UNUSED
;
4555 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4558 /* Canonicalize the relocs. */
4561 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4570 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
4576 tblptr
= section
->relocation
;
4577 for (i
= 0; i
< section
->reloc_count
; i
++)
4578 *relptr
++ = tblptr
++;
4582 return section
->reloc_count
;
4586 _bfd_elf_get_symtab (abfd
, alocation
)
4588 asymbol
**alocation
;
4590 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4591 (abfd
, alocation
, false);
4594 bfd_get_symcount (abfd
) = symcount
;
4599 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4601 asymbol
**alocation
;
4603 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4604 (abfd
, alocation
, true);
4607 /* Return the size required for the dynamic reloc entries. Any
4608 section that was actually installed in the BFD, and has type
4609 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4610 considered to be a dynamic reloc section. */
4613 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4619 if (elf_dynsymtab (abfd
) == 0)
4621 bfd_set_error (bfd_error_invalid_operation
);
4625 ret
= sizeof (arelent
*);
4626 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4627 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4628 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4629 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4630 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4631 * sizeof (arelent
*));
4636 /* Canonicalize the dynamic relocation entries. Note that we return
4637 the dynamic relocations as a single block, although they are
4638 actually associated with particular sections; the interface, which
4639 was designed for SunOS style shared libraries, expects that there
4640 is only one set of dynamic relocs. Any section that was actually
4641 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4642 the dynamic symbol table, is considered to be a dynamic reloc
4646 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4651 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4655 if (elf_dynsymtab (abfd
) == 0)
4657 bfd_set_error (bfd_error_invalid_operation
);
4661 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4663 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4665 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4666 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4667 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4672 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4674 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4676 for (i
= 0; i
< count
; i
++)
4687 /* Read in the version information. */
4690 _bfd_elf_slurp_version_tables (abfd
)
4693 bfd_byte
*contents
= NULL
;
4696 if (elf_dynverdef (abfd
) != 0)
4698 Elf_Internal_Shdr
*hdr
;
4699 Elf_External_Verdef
*everdef
;
4700 Elf_Internal_Verdef
*iverdef
;
4701 Elf_Internal_Verdef
*iverdefarr
;
4702 Elf_Internal_Verdef iverdefmem
;
4704 unsigned int maxidx
;
4706 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
4708 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4709 if (contents
== NULL
)
4711 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4712 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4715 /* We know the number of entries in the section but not the maximum
4716 index. Therefore we have to run through all entries and find
4718 everdef
= (Elf_External_Verdef
*) contents
;
4720 for (i
= 0; i
< hdr
->sh_info
; ++i
)
4722 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4724 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
4725 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
4727 everdef
= ((Elf_External_Verdef
*)
4728 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
4731 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
4732 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
4733 if (elf_tdata (abfd
)->verdef
== NULL
)
4736 elf_tdata (abfd
)->cverdefs
= maxidx
;
4738 everdef
= (Elf_External_Verdef
*) contents
;
4739 iverdefarr
= elf_tdata (abfd
)->verdef
;
4740 for (i
= 0; i
< hdr
->sh_info
; i
++)
4742 Elf_External_Verdaux
*everdaux
;
4743 Elf_Internal_Verdaux
*iverdaux
;
4746 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4748 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
4749 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
4751 iverdef
->vd_bfd
= abfd
;
4753 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
4754 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
4755 if (iverdef
->vd_auxptr
== NULL
)
4758 everdaux
= ((Elf_External_Verdaux
*)
4759 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
4760 iverdaux
= iverdef
->vd_auxptr
;
4761 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
4763 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
4765 iverdaux
->vda_nodename
=
4766 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4767 iverdaux
->vda_name
);
4768 if (iverdaux
->vda_nodename
== NULL
)
4771 if (j
+ 1 < iverdef
->vd_cnt
)
4772 iverdaux
->vda_nextptr
= iverdaux
+ 1;
4774 iverdaux
->vda_nextptr
= NULL
;
4776 everdaux
= ((Elf_External_Verdaux
*)
4777 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
4780 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
4782 if (i
+ 1 < hdr
->sh_info
)
4783 iverdef
->vd_nextdef
= iverdef
+ 1;
4785 iverdef
->vd_nextdef
= NULL
;
4787 everdef
= ((Elf_External_Verdef
*)
4788 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
4795 if (elf_dynverref (abfd
) != 0)
4797 Elf_Internal_Shdr
*hdr
;
4798 Elf_External_Verneed
*everneed
;
4799 Elf_Internal_Verneed
*iverneed
;
4802 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
4804 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
4805 elf_tdata (abfd
)->verref
=
4806 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
4807 if (elf_tdata (abfd
)->verref
== NULL
)
4810 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
4812 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4813 if (contents
== NULL
)
4815 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4816 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4819 everneed
= (Elf_External_Verneed
*) contents
;
4820 iverneed
= elf_tdata (abfd
)->verref
;
4821 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4823 Elf_External_Vernaux
*evernaux
;
4824 Elf_Internal_Vernaux
*ivernaux
;
4827 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4829 iverneed
->vn_bfd
= abfd
;
4831 iverneed
->vn_filename
=
4832 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4834 if (iverneed
->vn_filename
== NULL
)
4837 amt
= iverneed
->vn_cnt
;
4838 amt
*= sizeof (Elf_Internal_Vernaux
);
4839 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
4841 evernaux
= ((Elf_External_Vernaux
*)
4842 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4843 ivernaux
= iverneed
->vn_auxptr
;
4844 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4846 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4848 ivernaux
->vna_nodename
=
4849 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4850 ivernaux
->vna_name
);
4851 if (ivernaux
->vna_nodename
== NULL
)
4854 if (j
+ 1 < iverneed
->vn_cnt
)
4855 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4857 ivernaux
->vna_nextptr
= NULL
;
4859 evernaux
= ((Elf_External_Vernaux
*)
4860 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4863 if (i
+ 1 < hdr
->sh_info
)
4864 iverneed
->vn_nextref
= iverneed
+ 1;
4866 iverneed
->vn_nextref
= NULL
;
4868 everneed
= ((Elf_External_Verneed
*)
4869 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4879 if (contents
== NULL
)
4885 _bfd_elf_make_empty_symbol (abfd
)
4888 elf_symbol_type
*newsym
;
4889 bfd_size_type amt
= sizeof (elf_symbol_type
);
4891 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
4896 newsym
->symbol
.the_bfd
= abfd
;
4897 return &newsym
->symbol
;
4902 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4903 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4907 bfd_symbol_info (symbol
, ret
);
4910 /* Return whether a symbol name implies a local symbol. Most targets
4911 use this function for the is_local_label_name entry point, but some
4915 _bfd_elf_is_local_label_name (abfd
, name
)
4916 bfd
*abfd ATTRIBUTE_UNUSED
;
4919 /* Normal local symbols start with ``.L''. */
4920 if (name
[0] == '.' && name
[1] == 'L')
4923 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4924 DWARF debugging symbols starting with ``..''. */
4925 if (name
[0] == '.' && name
[1] == '.')
4928 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4929 emitting DWARF debugging output. I suspect this is actually a
4930 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4931 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4932 underscore to be emitted on some ELF targets). For ease of use,
4933 we treat such symbols as local. */
4934 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
4941 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
4942 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4943 asymbol
*symbol ATTRIBUTE_UNUSED
;
4950 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
4952 enum bfd_architecture arch
;
4953 unsigned long machine
;
4955 /* If this isn't the right architecture for this backend, and this
4956 isn't the generic backend, fail. */
4957 if (arch
!= get_elf_backend_data (abfd
)->arch
4958 && arch
!= bfd_arch_unknown
4959 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
4962 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
4965 /* Find the function to a particular section and offset,
4966 for error reporting. */
4969 elf_find_function (abfd
, section
, symbols
, offset
,
4970 filename_ptr
, functionname_ptr
)
4971 bfd
*abfd ATTRIBUTE_UNUSED
;
4975 const char **filename_ptr
;
4976 const char **functionname_ptr
;
4978 const char *filename
;
4987 for (p
= symbols
; *p
!= NULL
; p
++)
4991 q
= (elf_symbol_type
*) *p
;
4993 if (bfd_get_section (&q
->symbol
) != section
)
4996 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5001 filename
= bfd_asymbol_name (&q
->symbol
);
5005 if (q
->symbol
.section
== section
5006 && q
->symbol
.value
>= low_func
5007 && q
->symbol
.value
<= offset
)
5009 func
= (asymbol
*) q
;
5010 low_func
= q
->symbol
.value
;
5020 *filename_ptr
= filename
;
5021 if (functionname_ptr
)
5022 *functionname_ptr
= bfd_asymbol_name (func
);
5027 /* Find the nearest line to a particular section and offset,
5028 for error reporting. */
5031 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5032 filename_ptr
, functionname_ptr
, line_ptr
)
5037 const char **filename_ptr
;
5038 const char **functionname_ptr
;
5039 unsigned int *line_ptr
;
5043 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5044 filename_ptr
, functionname_ptr
,
5047 if (!*functionname_ptr
)
5048 elf_find_function (abfd
, section
, symbols
, offset
,
5049 *filename_ptr
? NULL
: filename_ptr
,
5055 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5056 filename_ptr
, functionname_ptr
,
5058 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5060 if (!*functionname_ptr
)
5061 elf_find_function (abfd
, section
, symbols
, offset
,
5062 *filename_ptr
? NULL
: filename_ptr
,
5068 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5069 &found
, filename_ptr
,
5070 functionname_ptr
, line_ptr
,
5071 &elf_tdata (abfd
)->line_info
))
5076 if (symbols
== NULL
)
5079 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5080 filename_ptr
, functionname_ptr
))
5088 _bfd_elf_sizeof_headers (abfd
, reloc
)
5094 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5096 ret
+= get_program_header_size (abfd
);
5101 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5106 bfd_size_type count
;
5108 Elf_Internal_Shdr
*hdr
;
5111 if (! abfd
->output_has_begun
5112 && ! _bfd_elf_compute_section_file_positions
5113 (abfd
, (struct bfd_link_info
*) NULL
))
5116 hdr
= &elf_section_data (section
)->this_hdr
;
5117 pos
= hdr
->sh_offset
+ offset
;
5118 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5119 || bfd_bwrite (location
, count
, abfd
) != count
)
5126 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5127 bfd
*abfd ATTRIBUTE_UNUSED
;
5128 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5129 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5136 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5139 Elf_Internal_Rel
*dst
;
5145 /* Try to convert a non-ELF reloc into an ELF one. */
5148 _bfd_elf_validate_reloc (abfd
, areloc
)
5152 /* Check whether we really have an ELF howto. */
5154 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5156 bfd_reloc_code_real_type code
;
5157 reloc_howto_type
*howto
;
5159 /* Alien reloc: Try to determine its type to replace it with an
5160 equivalent ELF reloc. */
5162 if (areloc
->howto
->pc_relative
)
5164 switch (areloc
->howto
->bitsize
)
5167 code
= BFD_RELOC_8_PCREL
;
5170 code
= BFD_RELOC_12_PCREL
;
5173 code
= BFD_RELOC_16_PCREL
;
5176 code
= BFD_RELOC_24_PCREL
;
5179 code
= BFD_RELOC_32_PCREL
;
5182 code
= BFD_RELOC_64_PCREL
;
5188 howto
= bfd_reloc_type_lookup (abfd
, code
);
5190 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5192 if (howto
->pcrel_offset
)
5193 areloc
->addend
+= areloc
->address
;
5195 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5200 switch (areloc
->howto
->bitsize
)
5206 code
= BFD_RELOC_14
;
5209 code
= BFD_RELOC_16
;
5212 code
= BFD_RELOC_26
;
5215 code
= BFD_RELOC_32
;
5218 code
= BFD_RELOC_64
;
5224 howto
= bfd_reloc_type_lookup (abfd
, code
);
5228 areloc
->howto
= howto
;
5236 (*_bfd_error_handler
)
5237 (_("%s: unsupported relocation type %s"),
5238 bfd_archive_filename (abfd
), areloc
->howto
->name
);
5239 bfd_set_error (bfd_error_bad_value
);
5244 _bfd_elf_close_and_cleanup (abfd
)
5247 if (bfd_get_format (abfd
) == bfd_object
)
5249 if (elf_shstrtab (abfd
) != NULL
)
5250 _bfd_stringtab_free (elf_shstrtab (abfd
));
5253 return _bfd_generic_close_and_cleanup (abfd
);
5256 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5257 in the relocation's offset. Thus we cannot allow any sort of sanity
5258 range-checking to interfere. There is nothing else to do in processing
5261 bfd_reloc_status_type
5262 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5263 bfd
*abfd ATTRIBUTE_UNUSED
;
5264 arelent
*re ATTRIBUTE_UNUSED
;
5265 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5266 PTR data ATTRIBUTE_UNUSED
;
5267 asection
*is ATTRIBUTE_UNUSED
;
5268 bfd
*obfd ATTRIBUTE_UNUSED
;
5269 char **errmsg ATTRIBUTE_UNUSED
;
5271 return bfd_reloc_ok
;
5274 /* Elf core file support. Much of this only works on native
5275 toolchains, since we rely on knowing the
5276 machine-dependent procfs structure in order to pick
5277 out details about the corefile. */
5279 #ifdef HAVE_SYS_PROCFS_H
5280 # include <sys/procfs.h>
5283 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5286 elfcore_make_pid (abfd
)
5289 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5290 + (elf_tdata (abfd
)->core_pid
));
5293 /* If there isn't a section called NAME, make one, using
5294 data from SECT. Note, this function will generate a
5295 reference to NAME, so you shouldn't deallocate or
5299 elfcore_maybe_make_sect (abfd
, name
, sect
)
5306 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5309 sect2
= bfd_make_section (abfd
, name
);
5313 sect2
->_raw_size
= sect
->_raw_size
;
5314 sect2
->filepos
= sect
->filepos
;
5315 sect2
->flags
= sect
->flags
;
5316 sect2
->alignment_power
= sect
->alignment_power
;
5320 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5321 actually creates up to two pseudosections:
5322 - For the single-threaded case, a section named NAME, unless
5323 such a section already exists.
5324 - For the multi-threaded case, a section named "NAME/PID", where
5325 PID is elfcore_make_pid (abfd).
5326 Both pseudosections have identical contents. */
5328 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
5335 char *threaded_name
;
5338 /* Build the section name. */
5340 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5341 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5342 if (threaded_name
== NULL
)
5344 strcpy (threaded_name
, buf
);
5346 sect
= bfd_make_section (abfd
, threaded_name
);
5349 sect
->_raw_size
= size
;
5350 sect
->filepos
= filepos
;
5351 sect
->flags
= SEC_HAS_CONTENTS
;
5352 sect
->alignment_power
= 2;
5354 return elfcore_maybe_make_sect (abfd
, name
, sect
);
5357 /* prstatus_t exists on:
5359 linux 2.[01] + glibc
5363 #if defined (HAVE_PRSTATUS_T)
5364 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
5367 elfcore_grok_prstatus (abfd
, note
)
5369 Elf_Internal_Note
*note
;
5374 if (note
->descsz
== sizeof (prstatus_t
))
5378 raw_size
= sizeof (prstat
.pr_reg
);
5379 offset
= offsetof (prstatus_t
, pr_reg
);
5380 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5382 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5383 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5385 /* pr_who exists on:
5388 pr_who doesn't exist on:
5391 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5392 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5395 #if defined (HAVE_PRSTATUS32_T)
5396 else if (note
->descsz
== sizeof (prstatus32_t
))
5398 /* 64-bit host, 32-bit corefile */
5399 prstatus32_t prstat
;
5401 raw_size
= sizeof (prstat
.pr_reg
);
5402 offset
= offsetof (prstatus32_t
, pr_reg
);
5403 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5405 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5406 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5408 /* pr_who exists on:
5411 pr_who doesn't exist on:
5414 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5415 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5418 #endif /* HAVE_PRSTATUS32_T */
5421 /* Fail - we don't know how to handle any other
5422 note size (ie. data object type). */
5426 /* Make a ".reg/999" section and a ".reg" section. */
5427 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
5428 raw_size
, note
->descpos
+ offset
);
5430 #endif /* defined (HAVE_PRSTATUS_T) */
5432 /* Create a pseudosection containing the exact contents of NOTE. */
5434 elfcore_make_note_pseudosection (abfd
, name
, note
)
5437 Elf_Internal_Note
*note
;
5439 return _bfd_elfcore_make_pseudosection (abfd
, name
,
5440 note
->descsz
, note
->descpos
);
5443 /* There isn't a consistent prfpregset_t across platforms,
5444 but it doesn't matter, because we don't have to pick this
5445 data structure apart. */
5448 elfcore_grok_prfpreg (abfd
, note
)
5450 Elf_Internal_Note
*note
;
5452 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
5455 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5456 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5460 elfcore_grok_prxfpreg (abfd
, note
)
5462 Elf_Internal_Note
*note
;
5464 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
5467 #if defined (HAVE_PRPSINFO_T)
5468 typedef prpsinfo_t elfcore_psinfo_t
;
5469 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5470 typedef prpsinfo32_t elfcore_psinfo32_t
;
5474 #if defined (HAVE_PSINFO_T)
5475 typedef psinfo_t elfcore_psinfo_t
;
5476 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5477 typedef psinfo32_t elfcore_psinfo32_t
;
5481 /* return a malloc'ed copy of a string at START which is at
5482 most MAX bytes long, possibly without a terminating '\0'.
5483 the copy will always have a terminating '\0'. */
5486 _bfd_elfcore_strndup (abfd
, start
, max
)
5492 char *end
= memchr (start
, '\0', max
);
5500 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
5504 memcpy (dups
, start
, len
);
5510 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5511 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
5514 elfcore_grok_psinfo (abfd
, note
)
5516 Elf_Internal_Note
*note
;
5518 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
5520 elfcore_psinfo_t psinfo
;
5522 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5524 elf_tdata (abfd
)->core_program
5525 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
5526 sizeof (psinfo
.pr_fname
));
5528 elf_tdata (abfd
)->core_command
5529 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
5530 sizeof (psinfo
.pr_psargs
));
5532 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5533 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
5535 /* 64-bit host, 32-bit corefile */
5536 elfcore_psinfo32_t psinfo
;
5538 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5540 elf_tdata (abfd
)->core_program
5541 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
5542 sizeof (psinfo
.pr_fname
));
5544 elf_tdata (abfd
)->core_command
5545 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
5546 sizeof (psinfo
.pr_psargs
));
5552 /* Fail - we don't know how to handle any other
5553 note size (ie. data object type). */
5557 /* Note that for some reason, a spurious space is tacked
5558 onto the end of the args in some (at least one anyway)
5559 implementations, so strip it off if it exists. */
5562 char *command
= elf_tdata (abfd
)->core_command
;
5563 int n
= strlen (command
);
5565 if (0 < n
&& command
[n
- 1] == ' ')
5566 command
[n
- 1] = '\0';
5571 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5573 #if defined (HAVE_PSTATUS_T)
5575 elfcore_grok_pstatus (abfd
, note
)
5577 Elf_Internal_Note
*note
;
5579 if (note
->descsz
== sizeof (pstatus_t
)
5580 #if defined (HAVE_PXSTATUS_T)
5581 || note
->descsz
== sizeof (pxstatus_t
)
5587 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5589 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5591 #if defined (HAVE_PSTATUS32_T)
5592 else if (note
->descsz
== sizeof (pstatus32_t
))
5594 /* 64-bit host, 32-bit corefile */
5597 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5599 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5602 /* Could grab some more details from the "representative"
5603 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5604 NT_LWPSTATUS note, presumably. */
5608 #endif /* defined (HAVE_PSTATUS_T) */
5610 #if defined (HAVE_LWPSTATUS_T)
5612 elfcore_grok_lwpstatus (abfd
, note
)
5614 Elf_Internal_Note
*note
;
5616 lwpstatus_t lwpstat
;
5621 if (note
->descsz
!= sizeof (lwpstat
)
5622 #if defined (HAVE_LWPXSTATUS_T)
5623 && note
->descsz
!= sizeof (lwpxstatus_t
)
5628 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
5630 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
5631 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
5633 /* Make a ".reg/999" section. */
5635 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5636 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5641 sect
= bfd_make_section (abfd
, name
);
5645 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5646 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
5647 sect
->filepos
= note
->descpos
5648 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
5651 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5652 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
5653 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
5656 sect
->flags
= SEC_HAS_CONTENTS
;
5657 sect
->alignment_power
= 2;
5659 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5662 /* Make a ".reg2/999" section */
5664 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
5665 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5670 sect
= bfd_make_section (abfd
, name
);
5674 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5675 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
5676 sect
->filepos
= note
->descpos
5677 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
5680 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5681 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
5682 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
5685 sect
->flags
= SEC_HAS_CONTENTS
;
5686 sect
->alignment_power
= 2;
5688 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
5690 #endif /* defined (HAVE_LWPSTATUS_T) */
5692 #if defined (HAVE_WIN32_PSTATUS_T)
5694 elfcore_grok_win32pstatus (abfd
, note
)
5696 Elf_Internal_Note
*note
;
5701 win32_pstatus_t pstatus
;
5703 if (note
->descsz
< sizeof (pstatus
))
5706 memcpy (&pstatus
, note
->descdata
, note
->descsz
);
5708 switch (pstatus
.data_type
)
5710 case NOTE_INFO_PROCESS
:
5711 /* FIXME: need to add ->core_command. */
5712 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
5713 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
5716 case NOTE_INFO_THREAD
:
5717 /* Make a ".reg/999" section. */
5718 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
5720 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5726 sect
= bfd_make_section (abfd
, name
);
5730 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
5731 sect
->filepos
= note
->descpos
+ offsetof (struct win32_pstatus
,
5732 data
.thread_info
.thread_context
);
5733 sect
->flags
= SEC_HAS_CONTENTS
;
5734 sect
->alignment_power
= 2;
5736 if (pstatus
.data
.thread_info
.is_active_thread
)
5737 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5741 case NOTE_INFO_MODULE
:
5742 /* Make a ".module/xxxxxxxx" section. */
5743 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
5745 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5751 sect
= bfd_make_section (abfd
, name
);
5756 sect
->_raw_size
= note
->descsz
;
5757 sect
->filepos
= note
->descpos
;
5758 sect
->flags
= SEC_HAS_CONTENTS
;
5759 sect
->alignment_power
= 2;
5768 #endif /* HAVE_WIN32_PSTATUS_T */
5771 elfcore_grok_note (abfd
, note
)
5773 Elf_Internal_Note
*note
;
5775 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5783 if (bed
->elf_backend_grok_prstatus
)
5784 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
5786 #if defined (HAVE_PRSTATUS_T)
5787 return elfcore_grok_prstatus (abfd
, note
);
5792 #if defined (HAVE_PSTATUS_T)
5794 return elfcore_grok_pstatus (abfd
, note
);
5797 #if defined (HAVE_LWPSTATUS_T)
5799 return elfcore_grok_lwpstatus (abfd
, note
);
5802 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
5803 return elfcore_grok_prfpreg (abfd
, note
);
5805 #if defined (HAVE_WIN32_PSTATUS_T)
5806 case NT_WIN32PSTATUS
:
5807 return elfcore_grok_win32pstatus (abfd
, note
);
5810 case NT_PRXFPREG
: /* Linux SSE extension */
5811 if (note
->namesz
== 5
5812 && ! strcmp (note
->namedata
, "LINUX"))
5813 return elfcore_grok_prxfpreg (abfd
, note
);
5819 if (bed
->elf_backend_grok_psinfo
)
5820 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
5822 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5823 return elfcore_grok_psinfo (abfd
, note
);
5831 elfcore_read_notes (abfd
, offset
, size
)
5842 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
5845 buf
= bfd_malloc (size
);
5849 if (bfd_bread (buf
, size
, abfd
) != size
)
5857 while (p
< buf
+ size
)
5859 /* FIXME: bad alignment assumption. */
5860 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
5861 Elf_Internal_Note in
;
5863 in
.type
= H_GET_32 (abfd
, xnp
->type
);
5865 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
5866 in
.namedata
= xnp
->name
;
5868 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
5869 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
5870 in
.descpos
= offset
+ (in
.descdata
- buf
);
5872 if (! elfcore_grok_note (abfd
, &in
))
5875 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
5882 /* Providing external access to the ELF program header table. */
5884 /* Return an upper bound on the number of bytes required to store a
5885 copy of ABFD's program header table entries. Return -1 if an error
5886 occurs; bfd_get_error will return an appropriate code. */
5889 bfd_get_elf_phdr_upper_bound (abfd
)
5892 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5894 bfd_set_error (bfd_error_wrong_format
);
5898 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
5901 /* Copy ABFD's program header table entries to *PHDRS. The entries
5902 will be stored as an array of Elf_Internal_Phdr structures, as
5903 defined in include/elf/internal.h. To find out how large the
5904 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5906 Return the number of program header table entries read, or -1 if an
5907 error occurs; bfd_get_error will return an appropriate code. */
5910 bfd_get_elf_phdrs (abfd
, phdrs
)
5916 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5918 bfd_set_error (bfd_error_wrong_format
);
5922 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
5923 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
5924 num_phdrs
* sizeof (Elf_Internal_Phdr
));
5930 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
5931 bfd
*abfd ATTRIBUTE_UNUSED
;
5936 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
5938 i_ehdrp
= elf_elfheader (abfd
);
5939 if (i_ehdrp
== NULL
)
5940 sprintf_vma (buf
, value
);
5943 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
5945 #if BFD_HOST_64BIT_LONG
5946 sprintf (buf
, "%016lx", value
);
5948 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
5949 _bfd_int64_low (value
));
5953 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
5956 sprintf_vma (buf
, value
);
5961 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
5962 bfd
*abfd ATTRIBUTE_UNUSED
;
5967 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
5969 i_ehdrp
= elf_elfheader (abfd
);
5970 if (i_ehdrp
== NULL
)
5971 fprintf_vma ((FILE *) stream
, value
);
5974 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
5976 #if BFD_HOST_64BIT_LONG
5977 fprintf ((FILE *) stream
, "%016lx", value
);
5979 fprintf ((FILE *) stream
, "%08lx%08lx",
5980 _bfd_int64_high (value
), _bfd_int64_low (value
));
5984 fprintf ((FILE *) stream
, "%08lx",
5985 (unsigned long) (value
& 0xffffffff));
5988 fprintf_vma ((FILE *) stream
, value
);
5992 enum elf_reloc_type_class
5993 _bfd_elf_reloc_type_class (rela
)
5994 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
5996 return reloc_class_normal
;