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 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
949 /* Allocate the structure if it has not already been allocated by a
951 if (ret
== (struct elf_link_hash_entry
*) NULL
)
952 ret
= ((struct elf_link_hash_entry
*)
953 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
954 if (ret
== (struct elf_link_hash_entry
*) NULL
)
955 return (struct bfd_hash_entry
*) ret
;
957 /* Call the allocation method of the superclass. */
958 ret
= ((struct elf_link_hash_entry
*)
959 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
961 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
963 /* Set local fields. */
967 ret
->dynstr_index
= 0;
969 ret
->got
.offset
= (bfd_vma
) -1;
970 ret
->plt
.offset
= (bfd_vma
) -1;
971 ret
->linker_section_pointer
= (elf_linker_section_pointers_t
*)0;
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
;
985 return (struct bfd_hash_entry
*) ret
;
988 /* Copy data from an indirect symbol to its direct symbol, hiding the
989 old indirect symbol. */
992 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
993 struct elf_link_hash_entry
*dir
, *ind
;
995 /* Copy down any references that we may have already seen to the
996 symbol which just became indirect. */
998 dir
->elf_link_hash_flags
|=
999 (ind
->elf_link_hash_flags
1000 & (ELF_LINK_HASH_REF_DYNAMIC
1001 | ELF_LINK_HASH_REF_REGULAR
1002 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1003 | ELF_LINK_NON_GOT_REF
));
1005 /* Copy over the global and procedure linkage table offset entries.
1006 These may have been already set up by a check_relocs routine. */
1007 if (dir
->got
.offset
== (bfd_vma
) -1)
1009 dir
->got
.offset
= ind
->got
.offset
;
1010 ind
->got
.offset
= (bfd_vma
) -1;
1012 BFD_ASSERT (ind
->got
.offset
== (bfd_vma
) -1);
1014 if (dir
->plt
.offset
== (bfd_vma
) -1)
1016 dir
->plt
.offset
= ind
->plt
.offset
;
1017 ind
->plt
.offset
= (bfd_vma
) -1;
1019 BFD_ASSERT (ind
->plt
.offset
== (bfd_vma
) -1);
1021 if (dir
->dynindx
== -1)
1023 dir
->dynindx
= ind
->dynindx
;
1024 dir
->dynstr_index
= ind
->dynstr_index
;
1026 ind
->dynstr_index
= 0;
1028 BFD_ASSERT (ind
->dynindx
== -1);
1032 _bfd_elf_link_hash_hide_symbol (info
, h
)
1033 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1034 struct elf_link_hash_entry
*h
;
1036 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1037 h
->plt
.offset
= (bfd_vma
) -1;
1038 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1042 /* Initialize an ELF linker hash table. */
1045 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1046 struct elf_link_hash_table
*table
;
1048 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1049 struct bfd_hash_table
*,
1054 table
->dynamic_sections_created
= false;
1055 table
->dynobj
= NULL
;
1056 /* The first dynamic symbol is a dummy. */
1057 table
->dynsymcount
= 1;
1058 table
->dynstr
= NULL
;
1059 table
->bucketcount
= 0;
1060 table
->needed
= NULL
;
1061 table
->runpath
= NULL
;
1063 table
->stab_info
= NULL
;
1064 table
->merge_info
= NULL
;
1065 table
->dynlocal
= NULL
;
1066 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1067 table
->root
.type
= bfd_link_elf_hash_table
;
1072 /* Create an ELF linker hash table. */
1074 struct bfd_link_hash_table
*
1075 _bfd_elf_link_hash_table_create (abfd
)
1078 struct elf_link_hash_table
*ret
;
1079 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1081 ret
= (struct elf_link_hash_table
*) bfd_alloc (abfd
, amt
);
1082 if (ret
== (struct elf_link_hash_table
*) NULL
)
1085 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1087 bfd_release (abfd
, ret
);
1094 /* This is a hook for the ELF emulation code in the generic linker to
1095 tell the backend linker what file name to use for the DT_NEEDED
1096 entry for a dynamic object. The generic linker passes name as an
1097 empty string to indicate that no DT_NEEDED entry should be made. */
1100 bfd_elf_set_dt_needed_name (abfd
, name
)
1104 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1105 && bfd_get_format (abfd
) == bfd_object
)
1106 elf_dt_name (abfd
) = name
;
1110 bfd_elf_set_dt_needed_soname (abfd
, name
)
1114 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1115 && bfd_get_format (abfd
) == bfd_object
)
1116 elf_dt_soname (abfd
) = name
;
1119 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1120 the linker ELF emulation code. */
1122 struct bfd_link_needed_list
*
1123 bfd_elf_get_needed_list (abfd
, info
)
1124 bfd
*abfd ATTRIBUTE_UNUSED
;
1125 struct bfd_link_info
*info
;
1127 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1129 return elf_hash_table (info
)->needed
;
1132 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1133 hook for the linker ELF emulation code. */
1135 struct bfd_link_needed_list
*
1136 bfd_elf_get_runpath_list (abfd
, info
)
1137 bfd
*abfd ATTRIBUTE_UNUSED
;
1138 struct bfd_link_info
*info
;
1140 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1142 return elf_hash_table (info
)->runpath
;
1145 /* Get the name actually used for a dynamic object for a link. This
1146 is the SONAME entry if there is one. Otherwise, it is the string
1147 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1150 bfd_elf_get_dt_soname (abfd
)
1153 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1154 && bfd_get_format (abfd
) == bfd_object
)
1155 return elf_dt_name (abfd
);
1159 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1160 the ELF linker emulation code. */
1163 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1165 struct bfd_link_needed_list
**pneeded
;
1168 bfd_byte
*dynbuf
= NULL
;
1170 unsigned long shlink
;
1171 bfd_byte
*extdyn
, *extdynend
;
1173 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1177 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1178 || bfd_get_format (abfd
) != bfd_object
)
1181 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1182 if (s
== NULL
|| s
->_raw_size
== 0)
1185 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1189 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1193 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1197 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1199 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1200 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1203 extdynend
= extdyn
+ s
->_raw_size
;
1204 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1206 Elf_Internal_Dyn dyn
;
1208 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1210 if (dyn
.d_tag
== DT_NULL
)
1213 if (dyn
.d_tag
== DT_NEEDED
)
1216 struct bfd_link_needed_list
*l
;
1217 unsigned int tagv
= dyn
.d_un
.d_val
;
1220 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1225 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1246 /* Allocate an ELF string table--force the first byte to be zero. */
1248 struct bfd_strtab_hash
*
1249 _bfd_elf_stringtab_init ()
1251 struct bfd_strtab_hash
*ret
;
1253 ret
= _bfd_stringtab_init ();
1258 loc
= _bfd_stringtab_add (ret
, "", true, false);
1259 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1260 if (loc
== (bfd_size_type
) -1)
1262 _bfd_stringtab_free (ret
);
1269 /* ELF .o/exec file reading */
1271 /* Create a new bfd section from an ELF section header. */
1274 bfd_section_from_shdr (abfd
, shindex
)
1276 unsigned int shindex
;
1278 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1279 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1280 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1283 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1285 switch (hdr
->sh_type
)
1288 /* Inactive section. Throw it away. */
1291 case SHT_PROGBITS
: /* Normal section with contents. */
1292 case SHT_DYNAMIC
: /* Dynamic linking information. */
1293 case SHT_NOBITS
: /* .bss section. */
1294 case SHT_HASH
: /* .hash section. */
1295 case SHT_NOTE
: /* .note section. */
1296 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1298 case SHT_SYMTAB
: /* A symbol table */
1299 if (elf_onesymtab (abfd
) == shindex
)
1302 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1303 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1304 elf_onesymtab (abfd
) = shindex
;
1305 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1306 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1307 abfd
->flags
|= HAS_SYMS
;
1309 /* Sometimes a shared object will map in the symbol table. If
1310 SHF_ALLOC is set, and this is a shared object, then we also
1311 treat this section as a BFD section. We can not base the
1312 decision purely on SHF_ALLOC, because that flag is sometimes
1313 set in a relocateable object file, which would confuse the
1315 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1316 && (abfd
->flags
& DYNAMIC
) != 0
1317 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1322 case SHT_DYNSYM
: /* A dynamic symbol table */
1323 if (elf_dynsymtab (abfd
) == shindex
)
1326 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1327 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1328 elf_dynsymtab (abfd
) = shindex
;
1329 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1330 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1331 abfd
->flags
|= HAS_SYMS
;
1333 /* Besides being a symbol table, we also treat this as a regular
1334 section, so that objcopy can handle it. */
1335 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1337 case SHT_STRTAB
: /* A string table */
1338 if (hdr
->bfd_section
!= NULL
)
1340 if (ehdr
->e_shstrndx
== shindex
)
1342 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1343 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1349 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1351 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1352 if (hdr2
->sh_link
== shindex
)
1354 if (! bfd_section_from_shdr (abfd
, i
))
1356 if (elf_onesymtab (abfd
) == i
)
1358 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1359 elf_elfsections (abfd
)[shindex
] =
1360 &elf_tdata (abfd
)->strtab_hdr
;
1363 if (elf_dynsymtab (abfd
) == i
)
1365 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1366 elf_elfsections (abfd
)[shindex
] = hdr
=
1367 &elf_tdata (abfd
)->dynstrtab_hdr
;
1368 /* We also treat this as a regular section, so
1369 that objcopy can handle it. */
1372 #if 0 /* Not handling other string tables specially right now. */
1373 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1374 /* We have a strtab for some random other section. */
1375 newsect
= (asection
*) hdr2
->bfd_section
;
1378 hdr
->bfd_section
= newsect
;
1379 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1381 elf_elfsections (abfd
)[shindex
] = hdr2
;
1387 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1391 /* *These* do a lot of work -- but build no sections! */
1393 asection
*target_sect
;
1394 Elf_Internal_Shdr
*hdr2
;
1396 /* Check for a bogus link to avoid crashing. */
1397 if (hdr
->sh_link
>= ehdr
->e_shnum
)
1399 ((*_bfd_error_handler
)
1400 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1401 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1402 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1405 /* For some incomprehensible reason Oracle distributes
1406 libraries for Solaris in which some of the objects have
1407 bogus sh_link fields. It would be nice if we could just
1408 reject them, but, unfortunately, some people need to use
1409 them. We scan through the section headers; if we find only
1410 one suitable symbol table, we clobber the sh_link to point
1411 to it. I hope this doesn't break anything. */
1412 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1413 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1419 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1421 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1422 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1433 hdr
->sh_link
= found
;
1436 /* Get the symbol table. */
1437 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1438 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1441 /* If this reloc section does not use the main symbol table we
1442 don't treat it as a reloc section. BFD can't adequately
1443 represent such a section, so at least for now, we don't
1444 try. We just present it as a normal section. We also
1445 can't use it as a reloc section if it points to the null
1447 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1448 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1450 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1452 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1453 if (target_sect
== NULL
)
1456 if ((target_sect
->flags
& SEC_RELOC
) == 0
1457 || target_sect
->reloc_count
== 0)
1458 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1462 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1463 amt
= sizeof (*hdr2
);
1464 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1465 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1468 elf_elfsections (abfd
)[shindex
] = hdr2
;
1469 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1470 target_sect
->flags
|= SEC_RELOC
;
1471 target_sect
->relocation
= NULL
;
1472 target_sect
->rel_filepos
= hdr
->sh_offset
;
1473 /* In the section to which the relocations apply, mark whether
1474 its relocations are of the REL or RELA variety. */
1475 if (hdr
->sh_size
!= 0)
1476 elf_section_data (target_sect
)->use_rela_p
1477 = (hdr
->sh_type
== SHT_RELA
);
1478 abfd
->flags
|= HAS_RELOC
;
1483 case SHT_GNU_verdef
:
1484 elf_dynverdef (abfd
) = shindex
;
1485 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1486 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1489 case SHT_GNU_versym
:
1490 elf_dynversym (abfd
) = shindex
;
1491 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1492 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1495 case SHT_GNU_verneed
:
1496 elf_dynverref (abfd
) = shindex
;
1497 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1498 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1505 /* Check for any processor-specific section types. */
1507 if (bed
->elf_backend_section_from_shdr
)
1508 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1516 /* Given an ELF section number, retrieve the corresponding BFD
1520 bfd_section_from_elf_index (abfd
, index
)
1524 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1525 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1527 return elf_elfsections (abfd
)[index
]->bfd_section
;
1531 _bfd_elf_new_section_hook (abfd
, sec
)
1535 struct bfd_elf_section_data
*sdata
;
1536 bfd_size_type amt
= sizeof (*sdata
);
1538 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
1541 sec
->used_by_bfd
= (PTR
) sdata
;
1543 /* Indicate whether or not this section should use RELA relocations. */
1545 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1550 /* Create a new bfd section from an ELF program header.
1552 Since program segments have no names, we generate a synthetic name
1553 of the form segment<NUM>, where NUM is generally the index in the
1554 program header table. For segments that are split (see below) we
1555 generate the names segment<NUM>a and segment<NUM>b.
1557 Note that some program segments may have a file size that is different than
1558 (less than) the memory size. All this means is that at execution the
1559 system must allocate the amount of memory specified by the memory size,
1560 but only initialize it with the first "file size" bytes read from the
1561 file. This would occur for example, with program segments consisting
1562 of combined data+bss.
1564 To handle the above situation, this routine generates TWO bfd sections
1565 for the single program segment. The first has the length specified by
1566 the file size of the segment, and the second has the length specified
1567 by the difference between the two sizes. In effect, the segment is split
1568 into it's initialized and uninitialized parts.
1573 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1575 Elf_Internal_Phdr
*hdr
;
1577 const char *typename
;
1584 split
= ((hdr
->p_memsz
> 0)
1585 && (hdr
->p_filesz
> 0)
1586 && (hdr
->p_memsz
> hdr
->p_filesz
));
1587 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1588 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1591 strcpy (name
, namebuf
);
1592 newsect
= bfd_make_section (abfd
, name
);
1593 if (newsect
== NULL
)
1595 newsect
->vma
= hdr
->p_vaddr
;
1596 newsect
->lma
= hdr
->p_paddr
;
1597 newsect
->_raw_size
= hdr
->p_filesz
;
1598 newsect
->filepos
= hdr
->p_offset
;
1599 newsect
->flags
|= SEC_HAS_CONTENTS
;
1600 if (hdr
->p_type
== PT_LOAD
)
1602 newsect
->flags
|= SEC_ALLOC
;
1603 newsect
->flags
|= SEC_LOAD
;
1604 if (hdr
->p_flags
& PF_X
)
1606 /* FIXME: all we known is that it has execute PERMISSION,
1608 newsect
->flags
|= SEC_CODE
;
1611 if (!(hdr
->p_flags
& PF_W
))
1613 newsect
->flags
|= SEC_READONLY
;
1618 sprintf (namebuf
, "%s%db", typename
, index
);
1619 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1622 strcpy (name
, namebuf
);
1623 newsect
= bfd_make_section (abfd
, name
);
1624 if (newsect
== NULL
)
1626 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1627 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1628 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1629 if (hdr
->p_type
== PT_LOAD
)
1631 newsect
->flags
|= SEC_ALLOC
;
1632 if (hdr
->p_flags
& PF_X
)
1633 newsect
->flags
|= SEC_CODE
;
1635 if (!(hdr
->p_flags
& PF_W
))
1636 newsect
->flags
|= SEC_READONLY
;
1643 bfd_section_from_phdr (abfd
, hdr
, index
)
1645 Elf_Internal_Phdr
*hdr
;
1648 struct elf_backend_data
*bed
;
1650 switch (hdr
->p_type
)
1653 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
1656 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
1659 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
1662 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
1665 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
1667 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
1672 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
1675 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
1678 /* Check for any processor-specific program segment types.
1679 If no handler for them, default to making "segment" sections. */
1680 bed
= get_elf_backend_data (abfd
);
1681 if (bed
->elf_backend_section_from_phdr
)
1682 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
1684 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
1688 /* Initialize REL_HDR, the section-header for new section, containing
1689 relocations against ASECT. If USE_RELA_P is true, we use RELA
1690 relocations; otherwise, we use REL relocations. */
1693 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
1695 Elf_Internal_Shdr
*rel_hdr
;
1700 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1701 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
1703 name
= bfd_alloc (abfd
, amt
);
1706 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1708 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1710 if (rel_hdr
->sh_name
== (unsigned int) -1)
1712 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1713 rel_hdr
->sh_entsize
= (use_rela_p
1714 ? bed
->s
->sizeof_rela
1715 : bed
->s
->sizeof_rel
);
1716 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
1717 rel_hdr
->sh_flags
= 0;
1718 rel_hdr
->sh_addr
= 0;
1719 rel_hdr
->sh_size
= 0;
1720 rel_hdr
->sh_offset
= 0;
1725 /* Set up an ELF internal section header for a section. */
1728 elf_fake_sections (abfd
, asect
, failedptrarg
)
1733 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1734 boolean
*failedptr
= (boolean
*) failedptrarg
;
1735 Elf_Internal_Shdr
*this_hdr
;
1739 /* We already failed; just get out of the bfd_map_over_sections
1744 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1746 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1749 if (this_hdr
->sh_name
== (unsigned long) -1)
1755 this_hdr
->sh_flags
= 0;
1757 if ((asect
->flags
& SEC_ALLOC
) != 0
1758 || asect
->user_set_vma
)
1759 this_hdr
->sh_addr
= asect
->vma
;
1761 this_hdr
->sh_addr
= 0;
1763 this_hdr
->sh_offset
= 0;
1764 this_hdr
->sh_size
= asect
->_raw_size
;
1765 this_hdr
->sh_link
= 0;
1766 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1767 /* The sh_entsize and sh_info fields may have been set already by
1768 copy_private_section_data. */
1770 this_hdr
->bfd_section
= asect
;
1771 this_hdr
->contents
= NULL
;
1773 /* FIXME: This should not be based on section names. */
1774 if (strcmp (asect
->name
, ".dynstr") == 0)
1775 this_hdr
->sh_type
= SHT_STRTAB
;
1776 else if (strcmp (asect
->name
, ".hash") == 0)
1778 this_hdr
->sh_type
= SHT_HASH
;
1779 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
1781 else if (strcmp (asect
->name
, ".dynsym") == 0)
1783 this_hdr
->sh_type
= SHT_DYNSYM
;
1784 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1786 else if (strcmp (asect
->name
, ".dynamic") == 0)
1788 this_hdr
->sh_type
= SHT_DYNAMIC
;
1789 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1791 else if (strncmp (asect
->name
, ".rela", 5) == 0
1792 && get_elf_backend_data (abfd
)->may_use_rela_p
)
1794 this_hdr
->sh_type
= SHT_RELA
;
1795 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1797 else if (strncmp (asect
->name
, ".rel", 4) == 0
1798 && get_elf_backend_data (abfd
)->may_use_rel_p
)
1800 this_hdr
->sh_type
= SHT_REL
;
1801 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1803 else if (strncmp (asect
->name
, ".note", 5) == 0)
1804 this_hdr
->sh_type
= SHT_NOTE
;
1805 else if (strncmp (asect
->name
, ".stab", 5) == 0
1806 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1807 this_hdr
->sh_type
= SHT_STRTAB
;
1808 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1810 this_hdr
->sh_type
= SHT_GNU_versym
;
1811 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1813 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1815 this_hdr
->sh_type
= SHT_GNU_verdef
;
1816 this_hdr
->sh_entsize
= 0;
1817 /* objcopy or strip will copy over sh_info, but may not set
1818 cverdefs. The linker will set cverdefs, but sh_info will be
1820 if (this_hdr
->sh_info
== 0)
1821 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1823 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1824 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1826 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1828 this_hdr
->sh_type
= SHT_GNU_verneed
;
1829 this_hdr
->sh_entsize
= 0;
1830 /* objcopy or strip will copy over sh_info, but may not set
1831 cverrefs. The linker will set cverrefs, but sh_info will be
1833 if (this_hdr
->sh_info
== 0)
1834 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1836 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1837 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1839 else if ((asect
->flags
& SEC_ALLOC
) != 0
1840 && ((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0))
1841 this_hdr
->sh_type
= SHT_NOBITS
;
1843 this_hdr
->sh_type
= SHT_PROGBITS
;
1845 if ((asect
->flags
& SEC_ALLOC
) != 0)
1846 this_hdr
->sh_flags
|= SHF_ALLOC
;
1847 if ((asect
->flags
& SEC_READONLY
) == 0)
1848 this_hdr
->sh_flags
|= SHF_WRITE
;
1849 if ((asect
->flags
& SEC_CODE
) != 0)
1850 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1851 if ((asect
->flags
& SEC_MERGE
) != 0)
1853 this_hdr
->sh_flags
|= SHF_MERGE
;
1854 this_hdr
->sh_entsize
= asect
->entsize
;
1855 if ((asect
->flags
& SEC_STRINGS
) != 0)
1856 this_hdr
->sh_flags
|= SHF_STRINGS
;
1859 /* Check for processor-specific section types. */
1860 if (bed
->elf_backend_fake_sections
)
1861 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1863 /* If the section has relocs, set up a section header for the
1864 SHT_REL[A] section. If two relocation sections are required for
1865 this section, it is up to the processor-specific back-end to
1866 create the other. */
1867 if ((asect
->flags
& SEC_RELOC
) != 0
1868 && !_bfd_elf_init_reloc_shdr (abfd
,
1869 &elf_section_data (asect
)->rel_hdr
,
1871 elf_section_data (asect
)->use_rela_p
))
1875 /* Assign all ELF section numbers. The dummy first section is handled here
1876 too. The link/info pointers for the standard section types are filled
1877 in here too, while we're at it. */
1880 assign_section_numbers (abfd
)
1883 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1885 unsigned int section_number
;
1886 Elf_Internal_Shdr
**i_shdrp
;
1891 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1893 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1895 d
->this_idx
= section_number
++;
1896 if ((sec
->flags
& SEC_RELOC
) == 0)
1899 d
->rel_idx
= section_number
++;
1902 d
->rel_idx2
= section_number
++;
1907 t
->shstrtab_section
= section_number
++;
1908 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1909 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1911 if (bfd_get_symcount (abfd
) > 0)
1913 t
->symtab_section
= section_number
++;
1914 t
->strtab_section
= section_number
++;
1917 elf_elfheader (abfd
)->e_shnum
= section_number
;
1919 /* Set up the list of section header pointers, in agreement with the
1921 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
1922 i_shdrp
= (Elf_Internal_Shdr
**) bfd_alloc (abfd
, amt
);
1923 if (i_shdrp
== NULL
)
1926 amt
= sizeof (Elf_Internal_Shdr
);
1927 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1928 if (i_shdrp
[0] == NULL
)
1930 bfd_release (abfd
, i_shdrp
);
1933 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1935 elf_elfsections (abfd
) = i_shdrp
;
1937 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1938 if (bfd_get_symcount (abfd
) > 0)
1940 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1941 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1942 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1944 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1946 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1950 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1951 if (d
->rel_idx
!= 0)
1952 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1953 if (d
->rel_idx2
!= 0)
1954 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
1956 /* Fill in the sh_link and sh_info fields while we're at it. */
1958 /* sh_link of a reloc section is the section index of the symbol
1959 table. sh_info is the section index of the section to which
1960 the relocation entries apply. */
1961 if (d
->rel_idx
!= 0)
1963 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1964 d
->rel_hdr
.sh_info
= d
->this_idx
;
1966 if (d
->rel_idx2
!= 0)
1968 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
1969 d
->rel_hdr2
->sh_info
= d
->this_idx
;
1972 switch (d
->this_hdr
.sh_type
)
1976 /* A reloc section which we are treating as a normal BFD
1977 section. sh_link is the section index of the symbol
1978 table. sh_info is the section index of the section to
1979 which the relocation entries apply. We assume that an
1980 allocated reloc section uses the dynamic symbol table.
1981 FIXME: How can we be sure? */
1982 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1984 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1986 /* We look up the section the relocs apply to by name. */
1988 if (d
->this_hdr
.sh_type
== SHT_REL
)
1992 s
= bfd_get_section_by_name (abfd
, name
);
1994 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1998 /* We assume that a section named .stab*str is a stabs
1999 string section. We look for a section with the same name
2000 but without the trailing ``str'', and set its sh_link
2001 field to point to this section. */
2002 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2003 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2008 len
= strlen (sec
->name
);
2009 alc
= (char *) bfd_malloc ((bfd_size_type
) len
- 2);
2012 strncpy (alc
, sec
->name
, len
- 3);
2013 alc
[len
- 3] = '\0';
2014 s
= bfd_get_section_by_name (abfd
, alc
);
2018 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2020 /* This is a .stab section. */
2021 elf_section_data (s
)->this_hdr
.sh_entsize
=
2022 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2029 case SHT_GNU_verneed
:
2030 case SHT_GNU_verdef
:
2031 /* sh_link is the section header index of the string table
2032 used for the dynamic entries, or the symbol table, or the
2034 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2036 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2040 case SHT_GNU_versym
:
2041 /* sh_link is the section header index of the symbol table
2042 this hash table or version table is for. */
2043 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2045 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2053 /* Map symbol from it's internal number to the external number, moving
2054 all local symbols to be at the head of the list. */
2057 sym_is_global (abfd
, sym
)
2061 /* If the backend has a special mapping, use it. */
2062 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2063 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2066 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2067 || bfd_is_und_section (bfd_get_section (sym
))
2068 || bfd_is_com_section (bfd_get_section (sym
)));
2072 elf_map_symbols (abfd
)
2075 unsigned int symcount
= bfd_get_symcount (abfd
);
2076 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2077 asymbol
**sect_syms
;
2078 unsigned int num_locals
= 0;
2079 unsigned int num_globals
= 0;
2080 unsigned int num_locals2
= 0;
2081 unsigned int num_globals2
= 0;
2083 unsigned int num_sections
= 0;
2090 fprintf (stderr
, "elf_map_symbols\n");
2094 /* Add a section symbol for each BFD section. FIXME: Is this really
2096 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2098 if (max_index
< asect
->index
)
2099 max_index
= asect
->index
;
2103 amt
= max_index
* sizeof (asymbol
*);
2104 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2105 if (sect_syms
== NULL
)
2107 elf_section_syms (abfd
) = sect_syms
;
2108 elf_num_section_syms (abfd
) = max_index
;
2110 for (idx
= 0; idx
< symcount
; idx
++)
2112 asymbol
*sym
= syms
[idx
];
2114 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2121 if (sec
->owner
!= NULL
)
2123 if (sec
->owner
!= abfd
)
2125 if (sec
->output_offset
!= 0)
2128 sec
= sec
->output_section
;
2130 /* Empty sections in the input files may have had a section
2131 symbol created for them. (See the comment near the end of
2132 _bfd_generic_link_output_symbols in linker.c). If the linker
2133 script discards such sections then we will reach this point.
2134 Since we know that we cannot avoid this case, we detect it
2135 and skip the abort and the assignment to the sect_syms array.
2136 To reproduce this particular case try running the linker
2137 testsuite test ld-scripts/weak.exp for an ELF port that uses
2138 the generic linker. */
2139 if (sec
->owner
== NULL
)
2142 BFD_ASSERT (sec
->owner
== abfd
);
2144 sect_syms
[sec
->index
] = syms
[idx
];
2149 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2153 if (sect_syms
[asect
->index
] != NULL
)
2156 sym
= bfd_make_empty_symbol (abfd
);
2159 sym
->the_bfd
= abfd
;
2160 sym
->name
= asect
->name
;
2162 /* Set the flags to 0 to indicate that this one was newly added. */
2164 sym
->section
= asect
;
2165 sect_syms
[asect
->index
] = sym
;
2169 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2170 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
2174 /* Classify all of the symbols. */
2175 for (idx
= 0; idx
< symcount
; idx
++)
2177 if (!sym_is_global (abfd
, syms
[idx
]))
2182 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2184 if (sect_syms
[asect
->index
] != NULL
2185 && sect_syms
[asect
->index
]->flags
== 0)
2187 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
2188 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
2192 sect_syms
[asect
->index
]->flags
= 0;
2196 /* Now sort the symbols so the local symbols are first. */
2197 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2198 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2200 if (new_syms
== NULL
)
2203 for (idx
= 0; idx
< symcount
; idx
++)
2205 asymbol
*sym
= syms
[idx
];
2208 if (!sym_is_global (abfd
, sym
))
2211 i
= num_locals
+ num_globals2
++;
2213 sym
->udata
.i
= i
+ 1;
2215 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2217 if (sect_syms
[asect
->index
] != NULL
2218 && sect_syms
[asect
->index
]->flags
== 0)
2220 asymbol
*sym
= sect_syms
[asect
->index
];
2223 sym
->flags
= BSF_SECTION_SYM
;
2224 if (!sym_is_global (abfd
, sym
))
2227 i
= num_locals
+ num_globals2
++;
2229 sym
->udata
.i
= i
+ 1;
2233 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2235 elf_num_locals (abfd
) = num_locals
;
2236 elf_num_globals (abfd
) = num_globals
;
2240 /* Align to the maximum file alignment that could be required for any
2241 ELF data structure. */
2243 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2244 static INLINE file_ptr
2245 align_file_position (off
, align
)
2249 return (off
+ align
- 1) & ~(align
- 1);
2252 /* Assign a file position to a section, optionally aligning to the
2253 required section alignment. */
2256 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2257 Elf_Internal_Shdr
*i_shdrp
;
2265 al
= i_shdrp
->sh_addralign
;
2267 offset
= BFD_ALIGN (offset
, al
);
2269 i_shdrp
->sh_offset
= offset
;
2270 if (i_shdrp
->bfd_section
!= NULL
)
2271 i_shdrp
->bfd_section
->filepos
= offset
;
2272 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2273 offset
+= i_shdrp
->sh_size
;
2277 /* Compute the file positions we are going to put the sections at, and
2278 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2279 is not NULL, this is being called by the ELF backend linker. */
2282 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2284 struct bfd_link_info
*link_info
;
2286 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2288 struct bfd_strtab_hash
*strtab
;
2289 Elf_Internal_Shdr
*shstrtab_hdr
;
2291 if (abfd
->output_has_begun
)
2294 /* Do any elf backend specific processing first. */
2295 if (bed
->elf_backend_begin_write_processing
)
2296 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2298 if (! prep_headers (abfd
))
2301 /* Post process the headers if necessary. */
2302 if (bed
->elf_backend_post_process_headers
)
2303 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2306 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2310 if (!assign_section_numbers (abfd
))
2313 /* The backend linker builds symbol table information itself. */
2314 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2316 /* Non-zero if doing a relocatable link. */
2317 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2319 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2323 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2324 /* sh_name was set in prep_headers. */
2325 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2326 shstrtab_hdr
->sh_flags
= 0;
2327 shstrtab_hdr
->sh_addr
= 0;
2328 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2329 shstrtab_hdr
->sh_entsize
= 0;
2330 shstrtab_hdr
->sh_link
= 0;
2331 shstrtab_hdr
->sh_info
= 0;
2332 /* sh_offset is set in assign_file_positions_except_relocs. */
2333 shstrtab_hdr
->sh_addralign
= 1;
2335 if (!assign_file_positions_except_relocs (abfd
))
2338 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2341 Elf_Internal_Shdr
*hdr
;
2343 off
= elf_tdata (abfd
)->next_file_pos
;
2345 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2346 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2348 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2349 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2351 elf_tdata (abfd
)->next_file_pos
= off
;
2353 /* Now that we know where the .strtab section goes, write it
2355 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2356 || ! _bfd_stringtab_emit (abfd
, strtab
))
2358 _bfd_stringtab_free (strtab
);
2361 abfd
->output_has_begun
= true;
2366 /* Create a mapping from a set of sections to a program segment. */
2368 static INLINE
struct elf_segment_map
*
2369 make_mapping (abfd
, sections
, from
, to
, phdr
)
2371 asection
**sections
;
2376 struct elf_segment_map
*m
;
2381 amt
= sizeof (struct elf_segment_map
);
2382 amt
+= (to
- from
- 1) * sizeof (asection
*);
2383 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2387 m
->p_type
= PT_LOAD
;
2388 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2389 m
->sections
[i
- from
] = *hdrpp
;
2390 m
->count
= to
- from
;
2392 if (from
== 0 && phdr
)
2394 /* Include the headers in the first PT_LOAD segment. */
2395 m
->includes_filehdr
= 1;
2396 m
->includes_phdrs
= 1;
2402 /* Set up a mapping from BFD sections to program segments. */
2405 map_sections_to_segments (abfd
)
2408 asection
**sections
= NULL
;
2412 struct elf_segment_map
*mfirst
;
2413 struct elf_segment_map
**pm
;
2414 struct elf_segment_map
*m
;
2416 unsigned int phdr_index
;
2417 bfd_vma maxpagesize
;
2419 boolean phdr_in_segment
= true;
2424 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2427 if (bfd_count_sections (abfd
) == 0)
2430 /* Select the allocated sections, and sort them. */
2432 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
2433 sections
= (asection
**) bfd_malloc (amt
);
2434 if (sections
== NULL
)
2438 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2440 if ((s
->flags
& SEC_ALLOC
) != 0)
2446 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2449 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2451 /* Build the mapping. */
2456 /* If we have a .interp section, then create a PT_PHDR segment for
2457 the program headers and a PT_INTERP segment for the .interp
2459 s
= bfd_get_section_by_name (abfd
, ".interp");
2460 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2462 amt
= sizeof (struct elf_segment_map
);
2463 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2467 m
->p_type
= PT_PHDR
;
2468 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2469 m
->p_flags
= PF_R
| PF_X
;
2470 m
->p_flags_valid
= 1;
2471 m
->includes_phdrs
= 1;
2476 amt
= sizeof (struct elf_segment_map
);
2477 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2481 m
->p_type
= PT_INTERP
;
2489 /* Look through the sections. We put sections in the same program
2490 segment when the start of the second section can be placed within
2491 a few bytes of the end of the first section. */
2494 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2496 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2498 && (dynsec
->flags
& SEC_LOAD
) == 0)
2501 /* Deal with -Ttext or something similar such that the first section
2502 is not adjacent to the program headers. This is an
2503 approximation, since at this point we don't know exactly how many
2504 program headers we will need. */
2507 bfd_size_type phdr_size
;
2509 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2511 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2512 if ((abfd
->flags
& D_PAGED
) == 0
2513 || sections
[0]->lma
< phdr_size
2514 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2515 phdr_in_segment
= false;
2518 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2521 boolean new_segment
;
2525 /* See if this section and the last one will fit in the same
2528 if (last_hdr
== NULL
)
2530 /* If we don't have a segment yet, then we don't need a new
2531 one (we build the last one after this loop). */
2532 new_segment
= false;
2534 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2536 /* If this section has a different relation between the
2537 virtual address and the load address, then we need a new
2541 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2542 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2544 /* If putting this section in this segment would force us to
2545 skip a page in the segment, then we need a new segment. */
2548 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2549 && (hdr
->flags
& SEC_LOAD
) != 0)
2551 /* We don't want to put a loadable section after a
2552 nonloadable section in the same segment. */
2555 else if ((abfd
->flags
& D_PAGED
) == 0)
2557 /* If the file is not demand paged, which means that we
2558 don't require the sections to be correctly aligned in the
2559 file, then there is no other reason for a new segment. */
2560 new_segment
= false;
2563 && (hdr
->flags
& SEC_READONLY
) == 0
2564 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2567 /* We don't want to put a writable section in a read only
2568 segment, unless they are on the same page in memory
2569 anyhow. We already know that the last section does not
2570 bring us past the current section on the page, so the
2571 only case in which the new section is not on the same
2572 page as the previous section is when the previous section
2573 ends precisely on a page boundary. */
2578 /* Otherwise, we can use the same segment. */
2579 new_segment
= false;
2584 if ((hdr
->flags
& SEC_READONLY
) == 0)
2590 /* We need a new program segment. We must create a new program
2591 header holding all the sections from phdr_index until hdr. */
2593 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2600 if ((hdr
->flags
& SEC_READONLY
) == 0)
2607 phdr_in_segment
= false;
2610 /* Create a final PT_LOAD program segment. */
2611 if (last_hdr
!= NULL
)
2613 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2621 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2624 amt
= sizeof (struct elf_segment_map
);
2625 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2629 m
->p_type
= PT_DYNAMIC
;
2631 m
->sections
[0] = dynsec
;
2637 /* For each loadable .note section, add a PT_NOTE segment. We don't
2638 use bfd_get_section_by_name, because if we link together
2639 nonloadable .note sections and loadable .note sections, we will
2640 generate two .note sections in the output file. FIXME: Using
2641 names for section types is bogus anyhow. */
2642 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2644 if ((s
->flags
& SEC_LOAD
) != 0
2645 && strncmp (s
->name
, ".note", 5) == 0)
2647 amt
= sizeof (struct elf_segment_map
);
2648 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2652 m
->p_type
= PT_NOTE
;
2664 elf_tdata (abfd
)->segment_map
= mfirst
;
2668 if (sections
!= NULL
)
2673 /* Sort sections by address. */
2676 elf_sort_sections (arg1
, arg2
)
2680 const asection
*sec1
= *(const asection
**) arg1
;
2681 const asection
*sec2
= *(const asection
**) arg2
;
2683 /* Sort by LMA first, since this is the address used to
2684 place the section into a segment. */
2685 if (sec1
->lma
< sec2
->lma
)
2687 else if (sec1
->lma
> sec2
->lma
)
2690 /* Then sort by VMA. Normally the LMA and the VMA will be
2691 the same, and this will do nothing. */
2692 if (sec1
->vma
< sec2
->vma
)
2694 else if (sec1
->vma
> sec2
->vma
)
2697 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2699 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2705 /* If the indicies are the same, do not return 0
2706 here, but continue to try the next comparison. */
2707 if (sec1
->target_index
- sec2
->target_index
!= 0)
2708 return sec1
->target_index
- sec2
->target_index
;
2713 else if (TOEND (sec2
))
2718 /* Sort by size, to put zero sized sections
2719 before others at the same address. */
2721 if (sec1
->_raw_size
< sec2
->_raw_size
)
2723 if (sec1
->_raw_size
> sec2
->_raw_size
)
2726 return sec1
->target_index
- sec2
->target_index
;
2729 /* Assign file positions to the sections based on the mapping from
2730 sections to segments. This function also sets up some fields in
2731 the file header, and writes out the program headers. */
2734 assign_file_positions_for_segments (abfd
)
2737 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2739 struct elf_segment_map
*m
;
2741 Elf_Internal_Phdr
*phdrs
;
2743 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2744 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2745 Elf_Internal_Phdr
*p
;
2748 if (elf_tdata (abfd
)->segment_map
== NULL
)
2750 if (! map_sections_to_segments (abfd
))
2754 if (bed
->elf_backend_modify_segment_map
)
2756 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2761 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2764 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2765 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2766 elf_elfheader (abfd
)->e_phnum
= count
;
2771 /* If we already counted the number of program segments, make sure
2772 that we allocated enough space. This happens when SIZEOF_HEADERS
2773 is used in a linker script. */
2774 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2775 if (alloc
!= 0 && count
> alloc
)
2777 ((*_bfd_error_handler
)
2778 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2779 bfd_get_filename (abfd
), alloc
, count
));
2780 bfd_set_error (bfd_error_bad_value
);
2787 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
2788 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
2792 off
= bed
->s
->sizeof_ehdr
;
2793 off
+= alloc
* bed
->s
->sizeof_phdr
;
2800 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2807 /* If elf_segment_map is not from map_sections_to_segments, the
2808 sections may not be correctly ordered. */
2810 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2813 p
->p_type
= m
->p_type
;
2814 p
->p_flags
= m
->p_flags
;
2816 if (p
->p_type
== PT_LOAD
2818 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2820 if ((abfd
->flags
& D_PAGED
) != 0)
2821 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2824 bfd_size_type align
;
2827 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2829 bfd_size_type secalign
;
2831 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
2832 if (secalign
> align
)
2836 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
2843 p
->p_vaddr
= m
->sections
[0]->vma
;
2845 if (m
->p_paddr_valid
)
2846 p
->p_paddr
= m
->p_paddr
;
2847 else if (m
->count
== 0)
2850 p
->p_paddr
= m
->sections
[0]->lma
;
2852 if (p
->p_type
== PT_LOAD
2853 && (abfd
->flags
& D_PAGED
) != 0)
2854 p
->p_align
= bed
->maxpagesize
;
2855 else if (m
->count
== 0)
2856 p
->p_align
= bed
->s
->file_align
;
2864 if (m
->includes_filehdr
)
2866 if (! m
->p_flags_valid
)
2869 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2870 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2873 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2875 if (p
->p_vaddr
< (bfd_vma
) off
)
2877 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2878 bfd_get_filename (abfd
));
2879 bfd_set_error (bfd_error_bad_value
);
2884 if (! m
->p_paddr_valid
)
2887 if (p
->p_type
== PT_LOAD
)
2889 filehdr_vaddr
= p
->p_vaddr
;
2890 filehdr_paddr
= p
->p_paddr
;
2894 if (m
->includes_phdrs
)
2896 if (! m
->p_flags_valid
)
2899 if (m
->includes_filehdr
)
2901 if (p
->p_type
== PT_LOAD
)
2903 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2904 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2909 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2913 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2914 p
->p_vaddr
-= off
- p
->p_offset
;
2915 if (! m
->p_paddr_valid
)
2916 p
->p_paddr
-= off
- p
->p_offset
;
2919 if (p
->p_type
== PT_LOAD
)
2921 phdrs_vaddr
= p
->p_vaddr
;
2922 phdrs_paddr
= p
->p_paddr
;
2925 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
2928 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2929 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2932 if (p
->p_type
== PT_LOAD
2933 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
2935 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2941 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2942 p
->p_filesz
+= adjust
;
2943 p
->p_memsz
+= adjust
;
2949 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2953 bfd_size_type align
;
2957 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2959 /* The section may have artificial alignment forced by a
2960 link script. Notice this case by the gap between the
2961 cumulative phdr vma and the section's vma. */
2962 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2964 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2966 p
->p_memsz
+= adjust
;
2969 if ((flags
& SEC_LOAD
) != 0)
2970 p
->p_filesz
+= adjust
;
2973 if (p
->p_type
== PT_LOAD
)
2975 bfd_signed_vma adjust
;
2977 if ((flags
& SEC_LOAD
) != 0)
2979 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2983 else if ((flags
& SEC_ALLOC
) != 0)
2985 /* The section VMA must equal the file position
2986 modulo the page size. FIXME: I'm not sure if
2987 this adjustment is really necessary. We used to
2988 not have the SEC_LOAD case just above, and then
2989 this was necessary, but now I'm not sure. */
2990 if ((abfd
->flags
& D_PAGED
) != 0)
2991 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2993 adjust
= (sec
->vma
- voff
) % align
;
3002 (* _bfd_error_handler
)
3003 (_("Error: First section in segment (%s) starts at 0x%x"),
3004 bfd_section_name (abfd
, sec
), sec
->lma
);
3005 (* _bfd_error_handler
)
3006 (_(" whereas segment starts at 0x%x"),
3011 p
->p_memsz
+= adjust
;
3014 if ((flags
& SEC_LOAD
) != 0)
3015 p
->p_filesz
+= adjust
;
3020 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3021 used in a linker script we may have a section with
3022 SEC_LOAD clear but which is supposed to have
3024 if ((flags
& SEC_LOAD
) != 0
3025 || (flags
& SEC_HAS_CONTENTS
) != 0)
3026 off
+= sec
->_raw_size
;
3028 if ((flags
& SEC_ALLOC
) != 0)
3029 voff
+= sec
->_raw_size
;
3032 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3034 /* The actual "note" segment has i == 0.
3035 This is the one that actually contains everything. */
3039 p
->p_filesz
= sec
->_raw_size
;
3040 off
+= sec
->_raw_size
;
3045 /* Fake sections -- don't need to be written. */
3048 flags
= sec
->flags
= 0;
3055 p
->p_memsz
+= sec
->_raw_size
;
3057 if ((flags
& SEC_LOAD
) != 0)
3058 p
->p_filesz
+= sec
->_raw_size
;
3060 if (align
> p
->p_align
3061 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3065 if (! m
->p_flags_valid
)
3068 if ((flags
& SEC_CODE
) != 0)
3070 if ((flags
& SEC_READONLY
) == 0)
3076 /* Now that we have set the section file positions, we can set up
3077 the file positions for the non PT_LOAD segments. */
3078 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3082 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3084 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3085 p
->p_offset
= m
->sections
[0]->filepos
;
3089 if (m
->includes_filehdr
)
3091 p
->p_vaddr
= filehdr_vaddr
;
3092 if (! m
->p_paddr_valid
)
3093 p
->p_paddr
= filehdr_paddr
;
3095 else if (m
->includes_phdrs
)
3097 p
->p_vaddr
= phdrs_vaddr
;
3098 if (! m
->p_paddr_valid
)
3099 p
->p_paddr
= phdrs_paddr
;
3104 /* Clear out any program headers we allocated but did not use. */
3105 for (; count
< alloc
; count
++, p
++)
3107 memset (p
, 0, sizeof *p
);
3108 p
->p_type
= PT_NULL
;
3111 elf_tdata (abfd
)->phdr
= phdrs
;
3113 elf_tdata (abfd
)->next_file_pos
= off
;
3115 /* Write out the program headers. */
3116 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3117 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3123 /* Get the size of the program header.
3125 If this is called by the linker before any of the section VMA's are set, it
3126 can't calculate the correct value for a strange memory layout. This only
3127 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3128 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3129 data segment (exclusive of .interp and .dynamic).
3131 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3132 will be two segments. */
3134 static bfd_size_type
3135 get_program_header_size (abfd
)
3140 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3142 /* We can't return a different result each time we're called. */
3143 if (elf_tdata (abfd
)->program_header_size
!= 0)
3144 return elf_tdata (abfd
)->program_header_size
;
3146 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3148 struct elf_segment_map
*m
;
3151 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3153 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3154 return elf_tdata (abfd
)->program_header_size
;
3157 /* Assume we will need exactly two PT_LOAD segments: one for text
3158 and one for data. */
3161 s
= bfd_get_section_by_name (abfd
, ".interp");
3162 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3164 /* If we have a loadable interpreter section, we need a
3165 PT_INTERP segment. In this case, assume we also need a
3166 PT_PHDR segment, although that may not be true for all
3171 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3173 /* We need a PT_DYNAMIC segment. */
3177 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3179 if ((s
->flags
& SEC_LOAD
) != 0
3180 && strncmp (s
->name
, ".note", 5) == 0)
3182 /* We need a PT_NOTE segment. */
3187 /* Let the backend count up any program headers it might need. */
3188 if (bed
->elf_backend_additional_program_headers
)
3192 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3198 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3199 return elf_tdata (abfd
)->program_header_size
;
3202 /* Work out the file positions of all the sections. This is called by
3203 _bfd_elf_compute_section_file_positions. All the section sizes and
3204 VMAs must be known before this is called.
3206 We do not consider reloc sections at this point, unless they form
3207 part of the loadable image. Reloc sections are assigned file
3208 positions in assign_file_positions_for_relocs, which is called by
3209 write_object_contents and final_link.
3211 We also don't set the positions of the .symtab and .strtab here. */
3214 assign_file_positions_except_relocs (abfd
)
3217 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3218 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3219 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3221 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3223 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3224 && bfd_get_format (abfd
) != bfd_core
)
3226 Elf_Internal_Shdr
**hdrpp
;
3229 /* Start after the ELF header. */
3230 off
= i_ehdrp
->e_ehsize
;
3232 /* We are not creating an executable, which means that we are
3233 not creating a program header, and that the actual order of
3234 the sections in the file is unimportant. */
3235 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3237 Elf_Internal_Shdr
*hdr
;
3240 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
3242 hdr
->sh_offset
= -1;
3245 if (i
== tdata
->symtab_section
3246 || i
== tdata
->strtab_section
)
3248 hdr
->sh_offset
= -1;
3252 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3258 Elf_Internal_Shdr
**hdrpp
;
3260 /* Assign file positions for the loaded sections based on the
3261 assignment of sections to segments. */
3262 if (! assign_file_positions_for_segments (abfd
))
3265 /* Assign file positions for the other sections. */
3267 off
= elf_tdata (abfd
)->next_file_pos
;
3268 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3270 Elf_Internal_Shdr
*hdr
;
3273 if (hdr
->bfd_section
!= NULL
3274 && hdr
->bfd_section
->filepos
!= 0)
3275 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3276 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3278 ((*_bfd_error_handler
)
3279 (_("%s: warning: allocated section `%s' not in segment"),
3280 bfd_get_filename (abfd
),
3281 (hdr
->bfd_section
== NULL
3283 : hdr
->bfd_section
->name
)));
3284 if ((abfd
->flags
& D_PAGED
) != 0)
3285 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3287 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3288 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3291 else if (hdr
->sh_type
== SHT_REL
3292 || hdr
->sh_type
== SHT_RELA
3293 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3294 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3295 hdr
->sh_offset
= -1;
3297 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3301 /* Place the section headers. */
3302 off
= align_file_position (off
, bed
->s
->file_align
);
3303 i_ehdrp
->e_shoff
= off
;
3304 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3306 elf_tdata (abfd
)->next_file_pos
= off
;
3315 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3316 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3317 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3319 struct bfd_strtab_hash
*shstrtab
;
3320 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3322 i_ehdrp
= elf_elfheader (abfd
);
3323 i_shdrp
= elf_elfsections (abfd
);
3325 shstrtab
= _bfd_elf_stringtab_init ();
3326 if (shstrtab
== NULL
)
3329 elf_shstrtab (abfd
) = shstrtab
;
3331 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3332 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3333 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3334 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3336 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3337 i_ehdrp
->e_ident
[EI_DATA
] =
3338 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3339 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3341 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3342 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3344 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3345 i_ehdrp
->e_ident
[count
] = 0;
3347 if ((abfd
->flags
& DYNAMIC
) != 0)
3348 i_ehdrp
->e_type
= ET_DYN
;
3349 else if ((abfd
->flags
& EXEC_P
) != 0)
3350 i_ehdrp
->e_type
= ET_EXEC
;
3351 else if (bfd_get_format (abfd
) == bfd_core
)
3352 i_ehdrp
->e_type
= ET_CORE
;
3354 i_ehdrp
->e_type
= ET_REL
;
3356 switch (bfd_get_arch (abfd
))
3358 case bfd_arch_unknown
:
3359 i_ehdrp
->e_machine
= EM_NONE
;
3362 /* There used to be a long list of cases here, each one setting
3363 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3364 in the corresponding bfd definition. To avoid duplication,
3365 the switch was removed. Machines that need special handling
3366 can generally do it in elf_backend_final_write_processing(),
3367 unless they need the information earlier than the final write.
3368 Such need can generally be supplied by replacing the tests for
3369 e_machine with the conditions used to determine it. */
3371 if (get_elf_backend_data (abfd
) != NULL
)
3372 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
3374 i_ehdrp
->e_machine
= EM_NONE
;
3377 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3378 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3380 /* No program header, for now. */
3381 i_ehdrp
->e_phoff
= 0;
3382 i_ehdrp
->e_phentsize
= 0;
3383 i_ehdrp
->e_phnum
= 0;
3385 /* Each bfd section is section header entry. */
3386 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3387 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3389 /* If we're building an executable, we'll need a program header table. */
3390 if (abfd
->flags
& EXEC_P
)
3392 /* It all happens later. */
3394 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3396 /* elf_build_phdrs() returns a (NULL-terminated) array of
3397 Elf_Internal_Phdrs. */
3398 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3399 i_ehdrp
->e_phoff
= outbase
;
3400 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3405 i_ehdrp
->e_phentsize
= 0;
3407 i_ehdrp
->e_phoff
= 0;
3410 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3411 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3412 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3413 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3414 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3415 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3416 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3417 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3418 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3424 /* Assign file positions for all the reloc sections which are not part
3425 of the loadable file image. */
3428 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3433 Elf_Internal_Shdr
**shdrpp
;
3435 off
= elf_tdata (abfd
)->next_file_pos
;
3437 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3438 i
< elf_elfheader (abfd
)->e_shnum
;
3441 Elf_Internal_Shdr
*shdrp
;
3444 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3445 && shdrp
->sh_offset
== -1)
3446 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3449 elf_tdata (abfd
)->next_file_pos
= off
;
3453 _bfd_elf_write_object_contents (abfd
)
3456 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3457 Elf_Internal_Ehdr
*i_ehdrp
;
3458 Elf_Internal_Shdr
**i_shdrp
;
3462 if (! abfd
->output_has_begun
3463 && ! _bfd_elf_compute_section_file_positions
3464 (abfd
, (struct bfd_link_info
*) NULL
))
3467 i_shdrp
= elf_elfsections (abfd
);
3468 i_ehdrp
= elf_elfheader (abfd
);
3471 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3475 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3477 /* After writing the headers, we need to write the sections too... */
3478 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3480 if (bed
->elf_backend_section_processing
)
3481 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3482 if (i_shdrp
[count
]->contents
)
3484 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
3486 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3487 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
3492 /* Write out the section header names. */
3493 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3494 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3497 if (bed
->elf_backend_final_write_processing
)
3498 (*bed
->elf_backend_final_write_processing
) (abfd
,
3499 elf_tdata (abfd
)->linker
);
3501 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3505 _bfd_elf_write_corefile_contents (abfd
)
3508 /* Hopefully this can be done just like an object file. */
3509 return _bfd_elf_write_object_contents (abfd
);
3512 /* Given a section, search the header to find them. */
3515 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3519 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3520 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3522 Elf_Internal_Shdr
*hdr
;
3523 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3525 for (index
= 0; index
< maxindex
; index
++)
3527 hdr
= i_shdrp
[index
];
3528 if (hdr
->bfd_section
== asect
)
3532 if (bed
->elf_backend_section_from_bfd_section
)
3534 for (index
= 0; index
< maxindex
; index
++)
3538 hdr
= i_shdrp
[index
];
3540 if ((*bed
->elf_backend_section_from_bfd_section
)
3541 (abfd
, hdr
, asect
, &retval
))
3546 if (bfd_is_abs_section (asect
))
3548 if (bfd_is_com_section (asect
))
3550 if (bfd_is_und_section (asect
))
3553 bfd_set_error (bfd_error_nonrepresentable_section
);
3558 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3562 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3564 asymbol
**asym_ptr_ptr
;
3566 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3568 flagword flags
= asym_ptr
->flags
;
3570 /* When gas creates relocations against local labels, it creates its
3571 own symbol for the section, but does put the symbol into the
3572 symbol chain, so udata is 0. When the linker is generating
3573 relocatable output, this section symbol may be for one of the
3574 input sections rather than the output section. */
3575 if (asym_ptr
->udata
.i
== 0
3576 && (flags
& BSF_SECTION_SYM
)
3577 && asym_ptr
->section
)
3581 if (asym_ptr
->section
->output_section
!= NULL
)
3582 indx
= asym_ptr
->section
->output_section
->index
;
3584 indx
= asym_ptr
->section
->index
;
3585 if (indx
< elf_num_section_syms (abfd
)
3586 && elf_section_syms (abfd
)[indx
] != NULL
)
3587 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3590 idx
= asym_ptr
->udata
.i
;
3594 /* This case can occur when using --strip-symbol on a symbol
3595 which is used in a relocation entry. */
3596 (*_bfd_error_handler
)
3597 (_("%s: symbol `%s' required but not present"),
3598 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3599 bfd_set_error (bfd_error_no_symbols
);
3606 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3607 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3608 elf_symbol_flags (flags
));
3616 /* Copy private BFD data. This copies any program header information. */
3619 copy_private_bfd_data (ibfd
, obfd
)
3623 Elf_Internal_Ehdr
* iehdr
;
3624 struct elf_segment_map
* map
;
3625 struct elf_segment_map
* map_first
;
3626 struct elf_segment_map
** pointer_to_map
;
3627 Elf_Internal_Phdr
* segment
;
3630 unsigned int num_segments
;
3631 boolean phdr_included
= false;
3632 bfd_vma maxpagesize
;
3633 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
3634 unsigned int phdr_adjust_num
= 0;
3636 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3637 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3640 if (elf_tdata (ibfd
)->phdr
== NULL
)
3643 iehdr
= elf_elfheader (ibfd
);
3646 pointer_to_map
= &map_first
;
3648 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3649 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3651 /* Returns the end address of the segment + 1. */
3652 #define SEGMENT_END(segment, start) \
3653 (start + (segment->p_memsz > segment->p_filesz \
3654 ? segment->p_memsz : segment->p_filesz))
3656 /* Returns true if the given section is contained within
3657 the given segment. VMA addresses are compared. */
3658 #define IS_CONTAINED_BY_VMA(section, segment) \
3659 (section->vma >= segment->p_vaddr \
3660 && (section->vma + section->_raw_size) \
3661 <= (SEGMENT_END (segment, segment->p_vaddr)))
3663 /* Returns true if the given section is contained within
3664 the given segment. LMA addresses are compared. */
3665 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3666 (section->lma >= base \
3667 && (section->lma + section->_raw_size) \
3668 <= SEGMENT_END (segment, base))
3670 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3671 #define IS_COREFILE_NOTE(p, s) \
3672 (p->p_type == PT_NOTE \
3673 && bfd_get_format (ibfd) == bfd_core \
3674 && s->vma == 0 && s->lma == 0 \
3675 && (bfd_vma) s->filepos >= p->p_offset \
3676 && (bfd_vma) s->filepos + s->_raw_size \
3677 <= p->p_offset + p->p_filesz)
3679 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3680 linker, which generates a PT_INTERP section with p_vaddr and
3681 p_memsz set to 0. */
3682 #define IS_SOLARIS_PT_INTERP(p, s) \
3684 && p->p_filesz > 0 \
3685 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3686 && s->_raw_size > 0 \
3687 && (bfd_vma) s->filepos >= p->p_offset \
3688 && ((bfd_vma) s->filepos + s->_raw_size \
3689 <= p->p_offset + p->p_filesz))
3691 /* Decide if the given section should be included in the given segment.
3692 A section will be included if:
3693 1. It is within the address space of the segment,
3694 2. It is an allocated segment,
3695 3. There is an output section associated with it,
3696 4. The section has not already been allocated to a previous segment. */
3697 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3698 ((((IS_CONTAINED_BY_VMA (section, segment) \
3699 || IS_SOLARIS_PT_INTERP (segment, section)) \
3700 && (section->flags & SEC_ALLOC) != 0) \
3701 || IS_COREFILE_NOTE (segment, section)) \
3702 && section->output_section != NULL \
3703 && section->segment_mark == false)
3705 /* Returns true iff seg1 starts after the end of seg2. */
3706 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3707 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3709 /* Returns true iff seg1 and seg2 overlap. */
3710 #define SEGMENT_OVERLAPS(seg1, seg2) \
3711 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3713 /* Initialise the segment mark field. */
3714 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3715 section
->segment_mark
= false;
3717 /* Scan through the segments specified in the program header
3718 of the input BFD. For this first scan we look for overlaps
3719 in the loadable segments. These can be created by wierd
3720 parameters to objcopy. */
3721 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3726 Elf_Internal_Phdr
*segment2
;
3728 if (segment
->p_type
!= PT_LOAD
)
3731 /* Determine if this segment overlaps any previous segments. */
3732 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
3734 bfd_signed_vma extra_length
;
3736 if (segment2
->p_type
!= PT_LOAD
3737 || ! SEGMENT_OVERLAPS (segment
, segment2
))
3740 /* Merge the two segments together. */
3741 if (segment2
->p_vaddr
< segment
->p_vaddr
)
3743 /* Extend SEGMENT2 to include SEGMENT and then delete
3746 SEGMENT_END (segment
, segment
->p_vaddr
)
3747 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
3749 if (extra_length
> 0)
3751 segment2
->p_memsz
+= extra_length
;
3752 segment2
->p_filesz
+= extra_length
;
3755 segment
->p_type
= PT_NULL
;
3757 /* Since we have deleted P we must restart the outer loop. */
3759 segment
= elf_tdata (ibfd
)->phdr
;
3764 /* Extend SEGMENT to include SEGMENT2 and then delete
3767 SEGMENT_END (segment2
, segment2
->p_vaddr
)
3768 - SEGMENT_END (segment
, segment
->p_vaddr
);
3770 if (extra_length
> 0)
3772 segment
->p_memsz
+= extra_length
;
3773 segment
->p_filesz
+= extra_length
;
3776 segment2
->p_type
= PT_NULL
;
3781 /* The second scan attempts to assign sections to segments. */
3782 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3786 unsigned int section_count
;
3787 asection
** sections
;
3788 asection
* output_section
;
3790 bfd_vma matching_lma
;
3791 bfd_vma suggested_lma
;
3795 if (segment
->p_type
== PT_NULL
)
3798 /* Compute how many sections might be placed into this segment. */
3800 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3801 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3804 /* Allocate a segment map big enough to contain all of the
3805 sections we have selected. */
3806 amt
= sizeof (struct elf_segment_map
);
3807 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
3808 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
3812 /* Initialise the fields of the segment map. Default to
3813 using the physical address of the segment in the input BFD. */
3815 map
->p_type
= segment
->p_type
;
3816 map
->p_flags
= segment
->p_flags
;
3817 map
->p_flags_valid
= 1;
3818 map
->p_paddr
= segment
->p_paddr
;
3819 map
->p_paddr_valid
= 1;
3821 /* Determine if this segment contains the ELF file header
3822 and if it contains the program headers themselves. */
3823 map
->includes_filehdr
= (segment
->p_offset
== 0
3824 && segment
->p_filesz
>= iehdr
->e_ehsize
);
3826 map
->includes_phdrs
= 0;
3828 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
3830 map
->includes_phdrs
=
3831 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3832 && (segment
->p_offset
+ segment
->p_filesz
3833 >= ((bfd_vma
) iehdr
->e_phoff
3834 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3836 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
3837 phdr_included
= true;
3840 if (section_count
== 0)
3842 /* Special segments, such as the PT_PHDR segment, may contain
3843 no sections, but ordinary, loadable segments should contain
3845 if (segment
->p_type
== PT_LOAD
)
3847 (_("%s: warning: Empty loadable segment detected\n"),
3848 bfd_archive_filename (ibfd
));
3851 *pointer_to_map
= map
;
3852 pointer_to_map
= &map
->next
;
3857 /* Now scan the sections in the input BFD again and attempt
3858 to add their corresponding output sections to the segment map.
3859 The problem here is how to handle an output section which has
3860 been moved (ie had its LMA changed). There are four possibilities:
3862 1. None of the sections have been moved.
3863 In this case we can continue to use the segment LMA from the
3866 2. All of the sections have been moved by the same amount.
3867 In this case we can change the segment's LMA to match the LMA
3868 of the first section.
3870 3. Some of the sections have been moved, others have not.
3871 In this case those sections which have not been moved can be
3872 placed in the current segment which will have to have its size,
3873 and possibly its LMA changed, and a new segment or segments will
3874 have to be created to contain the other sections.
3876 4. The sections have been moved, but not be the same amount.
3877 In this case we can change the segment's LMA to match the LMA
3878 of the first section and we will have to create a new segment
3879 or segments to contain the other sections.
3881 In order to save time, we allocate an array to hold the section
3882 pointers that we are interested in. As these sections get assigned
3883 to a segment, they are removed from this array. */
3885 amt
= (bfd_size_type
) section_count
* sizeof (asection
*);
3886 sections
= (asection
**) bfd_malloc (amt
);
3887 if (sections
== NULL
)
3890 /* Step One: Scan for segment vs section LMA conflicts.
3891 Also add the sections to the section array allocated above.
3892 Also add the sections to the current segment. In the common
3893 case, where the sections have not been moved, this means that
3894 we have completely filled the segment, and there is nothing
3900 for (j
= 0, section
= ibfd
->sections
;
3902 section
= section
->next
)
3904 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3906 output_section
= section
->output_section
;
3908 sections
[j
++] = section
;
3910 /* The Solaris native linker always sets p_paddr to 0.
3911 We try to catch that case here, and set it to the
3913 if (segment
->p_paddr
== 0
3914 && segment
->p_vaddr
!= 0
3916 && output_section
->lma
!= 0
3917 && (output_section
->vma
== (segment
->p_vaddr
3918 + (map
->includes_filehdr
3921 + (map
->includes_phdrs
3922 ? iehdr
->e_phnum
* iehdr
->e_phentsize
3924 map
->p_paddr
= segment
->p_vaddr
;
3926 /* Match up the physical address of the segment with the
3927 LMA address of the output section. */
3928 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
3929 || IS_COREFILE_NOTE (segment
, section
))
3931 if (matching_lma
== 0)
3932 matching_lma
= output_section
->lma
;
3934 /* We assume that if the section fits within the segment
3935 then it does not overlap any other section within that
3937 map
->sections
[isec
++] = output_section
;
3939 else if (suggested_lma
== 0)
3940 suggested_lma
= output_section
->lma
;
3944 BFD_ASSERT (j
== section_count
);
3946 /* Step Two: Adjust the physical address of the current segment,
3948 if (isec
== section_count
)
3950 /* All of the sections fitted within the segment as currently
3951 specified. This is the default case. Add the segment to
3952 the list of built segments and carry on to process the next
3953 program header in the input BFD. */
3954 map
->count
= section_count
;
3955 *pointer_to_map
= map
;
3956 pointer_to_map
= &map
->next
;
3963 if (matching_lma
!= 0)
3965 /* At least one section fits inside the current segment.
3966 Keep it, but modify its physical address to match the
3967 LMA of the first section that fitted. */
3968 map
->p_paddr
= matching_lma
;
3972 /* None of the sections fitted inside the current segment.
3973 Change the current segment's physical address to match
3974 the LMA of the first section. */
3975 map
->p_paddr
= suggested_lma
;
3978 /* Offset the segment physical address from the lma
3979 to allow for space taken up by elf headers. */
3980 if (map
->includes_filehdr
)
3981 map
->p_paddr
-= iehdr
->e_ehsize
;
3983 if (map
->includes_phdrs
)
3985 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
3987 /* iehdr->e_phnum is just an estimate of the number
3988 of program headers that we will need. Make a note
3989 here of the number we used and the segment we chose
3990 to hold these headers, so that we can adjust the
3991 offset when we know the correct value. */
3992 phdr_adjust_num
= iehdr
->e_phnum
;
3993 phdr_adjust_seg
= map
;
3997 /* Step Three: Loop over the sections again, this time assigning
3998 those that fit to the current segment and remvoing them from the
3999 sections array; but making sure not to leave large gaps. Once all
4000 possible sections have been assigned to the current segment it is
4001 added to the list of built segments and if sections still remain
4002 to be assigned, a new segment is constructed before repeating
4010 /* Fill the current segment with sections that fit. */
4011 for (j
= 0; j
< section_count
; j
++)
4013 section
= sections
[j
];
4015 if (section
== NULL
)
4018 output_section
= section
->output_section
;
4020 BFD_ASSERT (output_section
!= NULL
);
4022 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4023 || IS_COREFILE_NOTE (segment
, section
))
4025 if (map
->count
== 0)
4027 /* If the first section in a segment does not start at
4028 the beginning of the segment, then something is
4030 if (output_section
->lma
!=
4032 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4033 + (map
->includes_phdrs
4034 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4040 asection
* prev_sec
;
4042 prev_sec
= map
->sections
[map
->count
- 1];
4044 /* If the gap between the end of the previous section
4045 and the start of this section is more than
4046 maxpagesize then we need to start a new segment. */
4047 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
4048 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4049 || ((prev_sec
->lma
+ prev_sec
->_raw_size
) > output_section
->lma
))
4051 if (suggested_lma
== 0)
4052 suggested_lma
= output_section
->lma
;
4058 map
->sections
[map
->count
++] = output_section
;
4061 section
->segment_mark
= true;
4063 else if (suggested_lma
== 0)
4064 suggested_lma
= output_section
->lma
;
4067 BFD_ASSERT (map
->count
> 0);
4069 /* Add the current segment to the list of built segments. */
4070 *pointer_to_map
= map
;
4071 pointer_to_map
= &map
->next
;
4073 if (isec
< section_count
)
4075 /* We still have not allocated all of the sections to
4076 segments. Create a new segment here, initialise it
4077 and carry on looping. */
4078 amt
= sizeof (struct elf_segment_map
);
4079 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4080 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4084 /* Initialise the fields of the segment map. Set the physical
4085 physical address to the LMA of the first section that has
4086 not yet been assigned. */
4088 map
->p_type
= segment
->p_type
;
4089 map
->p_flags
= segment
->p_flags
;
4090 map
->p_flags_valid
= 1;
4091 map
->p_paddr
= suggested_lma
;
4092 map
->p_paddr_valid
= 1;
4093 map
->includes_filehdr
= 0;
4094 map
->includes_phdrs
= 0;
4097 while (isec
< section_count
);
4102 /* The Solaris linker creates program headers in which all the
4103 p_paddr fields are zero. When we try to objcopy or strip such a
4104 file, we get confused. Check for this case, and if we find it
4105 reset the p_paddr_valid fields. */
4106 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4107 if (map
->p_paddr
!= 0)
4111 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4112 map
->p_paddr_valid
= 0;
4115 elf_tdata (obfd
)->segment_map
= map_first
;
4117 /* If we had to estimate the number of program headers that were
4118 going to be needed, then check our estimate know and adjust
4119 the offset if necessary. */
4120 if (phdr_adjust_seg
!= NULL
)
4124 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4127 if (count
> phdr_adjust_num
)
4128 phdr_adjust_seg
->p_paddr
4129 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4133 /* Final Step: Sort the segments into ascending order of physical
4135 if (map_first
!= NULL
)
4137 struct elf_segment_map
*prev
;
4140 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4142 /* Yes I know - its a bubble sort.... */
4143 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4145 /* Swap map and map->next. */
4146 prev
->next
= map
->next
;
4147 map
->next
= map
->next
->next
;
4148 prev
->next
->next
= map
;
4158 #undef IS_CONTAINED_BY_VMA
4159 #undef IS_CONTAINED_BY_LMA
4160 #undef IS_COREFILE_NOTE
4161 #undef IS_SOLARIS_PT_INTERP
4162 #undef INCLUDE_SECTION_IN_SEGMENT
4163 #undef SEGMENT_AFTER_SEGMENT
4164 #undef SEGMENT_OVERLAPS
4168 /* Copy private section information. This copies over the entsize
4169 field, and sometimes the info field. */
4172 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4178 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4180 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4181 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4184 /* Copy over private BFD data if it has not already been copied.
4185 This must be done here, rather than in the copy_private_bfd_data
4186 entry point, because the latter is called after the section
4187 contents have been set, which means that the program headers have
4188 already been worked out. */
4189 if (elf_tdata (obfd
)->segment_map
== NULL
4190 && elf_tdata (ibfd
)->phdr
!= NULL
)
4194 /* Only set up the segments if there are no more SEC_ALLOC
4195 sections. FIXME: This won't do the right thing if objcopy is
4196 used to remove the last SEC_ALLOC section, since objcopy
4197 won't call this routine in that case. */
4198 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4199 if ((s
->flags
& SEC_ALLOC
) != 0)
4203 if (! copy_private_bfd_data (ibfd
, obfd
))
4208 ihdr
= &elf_section_data (isec
)->this_hdr
;
4209 ohdr
= &elf_section_data (osec
)->this_hdr
;
4211 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4213 if (ihdr
->sh_type
== SHT_SYMTAB
4214 || ihdr
->sh_type
== SHT_DYNSYM
4215 || ihdr
->sh_type
== SHT_GNU_verneed
4216 || ihdr
->sh_type
== SHT_GNU_verdef
)
4217 ohdr
->sh_info
= ihdr
->sh_info
;
4219 elf_section_data (osec
)->use_rela_p
4220 = elf_section_data (isec
)->use_rela_p
;
4225 /* Copy private symbol information. If this symbol is in a section
4226 which we did not map into a BFD section, try to map the section
4227 index correctly. We use special macro definitions for the mapped
4228 section indices; these definitions are interpreted by the
4229 swap_out_syms function. */
4231 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4232 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4233 #define MAP_STRTAB (SHN_LORESERVE - 3)
4234 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4237 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4243 elf_symbol_type
*isym
, *osym
;
4245 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4246 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4249 isym
= elf_symbol_from (ibfd
, isymarg
);
4250 osym
= elf_symbol_from (obfd
, osymarg
);
4254 && bfd_is_abs_section (isym
->symbol
.section
))
4258 shndx
= isym
->internal_elf_sym
.st_shndx
;
4259 if (shndx
== elf_onesymtab (ibfd
))
4260 shndx
= MAP_ONESYMTAB
;
4261 else if (shndx
== elf_dynsymtab (ibfd
))
4262 shndx
= MAP_DYNSYMTAB
;
4263 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4265 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4266 shndx
= MAP_SHSTRTAB
;
4267 osym
->internal_elf_sym
.st_shndx
= shndx
;
4273 /* Swap out the symbols. */
4276 swap_out_syms (abfd
, sttp
, relocatable_p
)
4278 struct bfd_strtab_hash
**sttp
;
4281 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4283 if (!elf_map_symbols (abfd
))
4286 /* Dump out the symtabs. */
4288 int symcount
= bfd_get_symcount (abfd
);
4289 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4290 struct bfd_strtab_hash
*stt
;
4291 Elf_Internal_Shdr
*symtab_hdr
;
4292 Elf_Internal_Shdr
*symstrtab_hdr
;
4293 char *outbound_syms
;
4297 stt
= _bfd_elf_stringtab_init ();
4301 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4302 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4303 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4304 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4305 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4306 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4308 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4309 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4311 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
4312 outbound_syms
= bfd_alloc (abfd
, amt
);
4313 if (outbound_syms
== NULL
)
4315 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4317 /* now generate the data (for "contents") */
4319 /* Fill in zeroth symbol and swap it out. */
4320 Elf_Internal_Sym sym
;
4326 sym
.st_shndx
= SHN_UNDEF
;
4327 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4328 outbound_syms
+= bed
->s
->sizeof_sym
;
4330 for (idx
= 0; idx
< symcount
; idx
++)
4332 Elf_Internal_Sym sym
;
4333 bfd_vma value
= syms
[idx
]->value
;
4334 elf_symbol_type
*type_ptr
;
4335 flagword flags
= syms
[idx
]->flags
;
4338 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
4340 /* Local section symbols have no name. */
4345 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4348 if (sym
.st_name
== (unsigned long) -1)
4352 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4354 if ((flags
& BSF_SECTION_SYM
) == 0
4355 && bfd_is_com_section (syms
[idx
]->section
))
4357 /* ELF common symbols put the alignment into the `value' field,
4358 and the size into the `size' field. This is backwards from
4359 how BFD handles it, so reverse it here. */
4360 sym
.st_size
= value
;
4361 if (type_ptr
== NULL
4362 || type_ptr
->internal_elf_sym
.st_value
== 0)
4363 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4365 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4366 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4367 (abfd
, syms
[idx
]->section
);
4371 asection
*sec
= syms
[idx
]->section
;
4374 if (sec
->output_section
)
4376 value
+= sec
->output_offset
;
4377 sec
= sec
->output_section
;
4379 /* Don't add in the section vma for relocatable output. */
4380 if (! relocatable_p
)
4382 sym
.st_value
= value
;
4383 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4385 if (bfd_is_abs_section (sec
)
4387 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4389 /* This symbol is in a real ELF section which we did
4390 not create as a BFD section. Undo the mapping done
4391 by copy_private_symbol_data. */
4392 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4396 shndx
= elf_onesymtab (abfd
);
4399 shndx
= elf_dynsymtab (abfd
);
4402 shndx
= elf_tdata (abfd
)->strtab_section
;
4405 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4413 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4419 /* Writing this would be a hell of a lot easier if
4420 we had some decent documentation on bfd, and
4421 knew what to expect of the library, and what to
4422 demand of applications. For example, it
4423 appears that `objcopy' might not set the
4424 section of a symbol to be a section that is
4425 actually in the output file. */
4426 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4427 BFD_ASSERT (sec2
!= 0);
4428 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
4429 BFD_ASSERT (shndx
!= -1);
4433 sym
.st_shndx
= shndx
;
4436 if ((flags
& BSF_FUNCTION
) != 0)
4438 else if ((flags
& BSF_OBJECT
) != 0)
4443 /* Processor-specific types */
4444 if (type_ptr
!= NULL
4445 && bed
->elf_backend_get_symbol_type
)
4446 type
= (*bed
->elf_backend_get_symbol_type
) (&type_ptr
->internal_elf_sym
, type
);
4448 if (flags
& BSF_SECTION_SYM
)
4450 if (flags
& BSF_GLOBAL
)
4451 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
4453 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4455 else if (bfd_is_com_section (syms
[idx
]->section
))
4456 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4457 else if (bfd_is_und_section (syms
[idx
]->section
))
4458 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4462 else if (flags
& BSF_FILE
)
4463 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4466 int bind
= STB_LOCAL
;
4468 if (flags
& BSF_LOCAL
)
4470 else if (flags
& BSF_WEAK
)
4472 else if (flags
& BSF_GLOBAL
)
4475 sym
.st_info
= ELF_ST_INFO (bind
, type
);
4478 if (type_ptr
!= NULL
)
4479 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
4483 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4484 outbound_syms
+= bed
->s
->sizeof_sym
;
4488 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
4489 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4491 symstrtab_hdr
->sh_flags
= 0;
4492 symstrtab_hdr
->sh_addr
= 0;
4493 symstrtab_hdr
->sh_entsize
= 0;
4494 symstrtab_hdr
->sh_link
= 0;
4495 symstrtab_hdr
->sh_info
= 0;
4496 symstrtab_hdr
->sh_addralign
= 1;
4502 /* Return the number of bytes required to hold the symtab vector.
4504 Note that we base it on the count plus 1, since we will null terminate
4505 the vector allocated based on this size. However, the ELF symbol table
4506 always has a dummy entry as symbol #0, so it ends up even. */
4509 _bfd_elf_get_symtab_upper_bound (abfd
)
4514 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4516 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4517 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4523 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4528 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4530 if (elf_dynsymtab (abfd
) == 0)
4532 bfd_set_error (bfd_error_invalid_operation
);
4536 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4537 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4543 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4544 bfd
*abfd ATTRIBUTE_UNUSED
;
4547 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4550 /* Canonicalize the relocs. */
4553 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4562 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
4568 tblptr
= section
->relocation
;
4569 for (i
= 0; i
< section
->reloc_count
; i
++)
4570 *relptr
++ = tblptr
++;
4574 return section
->reloc_count
;
4578 _bfd_elf_get_symtab (abfd
, alocation
)
4580 asymbol
**alocation
;
4582 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4583 (abfd
, alocation
, false);
4586 bfd_get_symcount (abfd
) = symcount
;
4591 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4593 asymbol
**alocation
;
4595 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4596 (abfd
, alocation
, true);
4599 /* Return the size required for the dynamic reloc entries. Any
4600 section that was actually installed in the BFD, and has type
4601 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4602 considered to be a dynamic reloc section. */
4605 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4611 if (elf_dynsymtab (abfd
) == 0)
4613 bfd_set_error (bfd_error_invalid_operation
);
4617 ret
= sizeof (arelent
*);
4618 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4619 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4620 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4621 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4622 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4623 * sizeof (arelent
*));
4628 /* Canonicalize the dynamic relocation entries. Note that we return
4629 the dynamic relocations as a single block, although they are
4630 actually associated with particular sections; the interface, which
4631 was designed for SunOS style shared libraries, expects that there
4632 is only one set of dynamic relocs. Any section that was actually
4633 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4634 the dynamic symbol table, is considered to be a dynamic reloc
4638 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4643 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4647 if (elf_dynsymtab (abfd
) == 0)
4649 bfd_set_error (bfd_error_invalid_operation
);
4653 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4655 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4657 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4658 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4659 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4664 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4666 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4668 for (i
= 0; i
< count
; i
++)
4679 /* Read in the version information. */
4682 _bfd_elf_slurp_version_tables (abfd
)
4685 bfd_byte
*contents
= NULL
;
4688 if (elf_dynverdef (abfd
) != 0)
4690 Elf_Internal_Shdr
*hdr
;
4691 Elf_External_Verdef
*everdef
;
4692 Elf_Internal_Verdef
*iverdef
;
4693 Elf_Internal_Verdef
*iverdefarr
;
4694 Elf_Internal_Verdef iverdefmem
;
4696 unsigned int maxidx
;
4698 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
4700 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4701 if (contents
== NULL
)
4703 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4704 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4707 /* We know the number of entries in the section but not the maximum
4708 index. Therefore we have to run through all entries and find
4710 everdef
= (Elf_External_Verdef
*) contents
;
4712 for (i
= 0; i
< hdr
->sh_info
; ++i
)
4714 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4716 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
4717 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
4719 everdef
= ((Elf_External_Verdef
*)
4720 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
4723 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
4724 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
4725 if (elf_tdata (abfd
)->verdef
== NULL
)
4728 elf_tdata (abfd
)->cverdefs
= maxidx
;
4730 everdef
= (Elf_External_Verdef
*) contents
;
4731 iverdefarr
= elf_tdata (abfd
)->verdef
;
4732 for (i
= 0; i
< hdr
->sh_info
; i
++)
4734 Elf_External_Verdaux
*everdaux
;
4735 Elf_Internal_Verdaux
*iverdaux
;
4738 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4740 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
4741 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
4743 iverdef
->vd_bfd
= abfd
;
4745 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
4746 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
4747 if (iverdef
->vd_auxptr
== NULL
)
4750 everdaux
= ((Elf_External_Verdaux
*)
4751 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
4752 iverdaux
= iverdef
->vd_auxptr
;
4753 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
4755 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
4757 iverdaux
->vda_nodename
=
4758 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4759 iverdaux
->vda_name
);
4760 if (iverdaux
->vda_nodename
== NULL
)
4763 if (j
+ 1 < iverdef
->vd_cnt
)
4764 iverdaux
->vda_nextptr
= iverdaux
+ 1;
4766 iverdaux
->vda_nextptr
= NULL
;
4768 everdaux
= ((Elf_External_Verdaux
*)
4769 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
4772 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
4774 if (i
+ 1 < hdr
->sh_info
)
4775 iverdef
->vd_nextdef
= iverdef
+ 1;
4777 iverdef
->vd_nextdef
= NULL
;
4779 everdef
= ((Elf_External_Verdef
*)
4780 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
4787 if (elf_dynverref (abfd
) != 0)
4789 Elf_Internal_Shdr
*hdr
;
4790 Elf_External_Verneed
*everneed
;
4791 Elf_Internal_Verneed
*iverneed
;
4794 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
4796 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
4797 elf_tdata (abfd
)->verref
=
4798 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
4799 if (elf_tdata (abfd
)->verref
== NULL
)
4802 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
4804 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4805 if (contents
== NULL
)
4807 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4808 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4811 everneed
= (Elf_External_Verneed
*) contents
;
4812 iverneed
= elf_tdata (abfd
)->verref
;
4813 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4815 Elf_External_Vernaux
*evernaux
;
4816 Elf_Internal_Vernaux
*ivernaux
;
4819 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4821 iverneed
->vn_bfd
= abfd
;
4823 iverneed
->vn_filename
=
4824 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4826 if (iverneed
->vn_filename
== NULL
)
4829 amt
= iverneed
->vn_cnt
;
4830 amt
*= sizeof (Elf_Internal_Vernaux
);
4831 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
4833 evernaux
= ((Elf_External_Vernaux
*)
4834 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4835 ivernaux
= iverneed
->vn_auxptr
;
4836 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4838 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4840 ivernaux
->vna_nodename
=
4841 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4842 ivernaux
->vna_name
);
4843 if (ivernaux
->vna_nodename
== NULL
)
4846 if (j
+ 1 < iverneed
->vn_cnt
)
4847 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4849 ivernaux
->vna_nextptr
= NULL
;
4851 evernaux
= ((Elf_External_Vernaux
*)
4852 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4855 if (i
+ 1 < hdr
->sh_info
)
4856 iverneed
->vn_nextref
= iverneed
+ 1;
4858 iverneed
->vn_nextref
= NULL
;
4860 everneed
= ((Elf_External_Verneed
*)
4861 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4871 if (contents
== NULL
)
4877 _bfd_elf_make_empty_symbol (abfd
)
4880 elf_symbol_type
*newsym
;
4881 bfd_size_type amt
= sizeof (elf_symbol_type
);
4883 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
4888 newsym
->symbol
.the_bfd
= abfd
;
4889 return &newsym
->symbol
;
4894 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4895 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4899 bfd_symbol_info (symbol
, ret
);
4902 /* Return whether a symbol name implies a local symbol. Most targets
4903 use this function for the is_local_label_name entry point, but some
4907 _bfd_elf_is_local_label_name (abfd
, name
)
4908 bfd
*abfd ATTRIBUTE_UNUSED
;
4911 /* Normal local symbols start with ``.L''. */
4912 if (name
[0] == '.' && name
[1] == 'L')
4915 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4916 DWARF debugging symbols starting with ``..''. */
4917 if (name
[0] == '.' && name
[1] == '.')
4920 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4921 emitting DWARF debugging output. I suspect this is actually a
4922 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4923 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4924 underscore to be emitted on some ELF targets). For ease of use,
4925 we treat such symbols as local. */
4926 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
4933 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
4934 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4935 asymbol
*symbol ATTRIBUTE_UNUSED
;
4942 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
4944 enum bfd_architecture arch
;
4945 unsigned long machine
;
4947 /* If this isn't the right architecture for this backend, and this
4948 isn't the generic backend, fail. */
4949 if (arch
!= get_elf_backend_data (abfd
)->arch
4950 && arch
!= bfd_arch_unknown
4951 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
4954 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
4957 /* Find the function to a particular section and offset,
4958 for error reporting. */
4961 elf_find_function (abfd
, section
, symbols
, offset
,
4962 filename_ptr
, functionname_ptr
)
4963 bfd
*abfd ATTRIBUTE_UNUSED
;
4967 const char **filename_ptr
;
4968 const char **functionname_ptr
;
4970 const char *filename
;
4979 for (p
= symbols
; *p
!= NULL
; p
++)
4983 q
= (elf_symbol_type
*) *p
;
4985 if (bfd_get_section (&q
->symbol
) != section
)
4988 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
4993 filename
= bfd_asymbol_name (&q
->symbol
);
4997 if (q
->symbol
.section
== section
4998 && q
->symbol
.value
>= low_func
4999 && q
->symbol
.value
<= offset
)
5001 func
= (asymbol
*) q
;
5002 low_func
= q
->symbol
.value
;
5012 *filename_ptr
= filename
;
5013 if (functionname_ptr
)
5014 *functionname_ptr
= bfd_asymbol_name (func
);
5019 /* Find the nearest line to a particular section and offset,
5020 for error reporting. */
5023 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5024 filename_ptr
, functionname_ptr
, line_ptr
)
5029 const char **filename_ptr
;
5030 const char **functionname_ptr
;
5031 unsigned int *line_ptr
;
5035 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5036 filename_ptr
, functionname_ptr
,
5039 if (!*functionname_ptr
)
5040 elf_find_function (abfd
, section
, symbols
, offset
,
5041 *filename_ptr
? NULL
: filename_ptr
,
5047 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5048 filename_ptr
, functionname_ptr
,
5050 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5052 if (!*functionname_ptr
)
5053 elf_find_function (abfd
, section
, symbols
, offset
,
5054 *filename_ptr
? NULL
: filename_ptr
,
5060 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5061 &found
, filename_ptr
,
5062 functionname_ptr
, line_ptr
,
5063 &elf_tdata (abfd
)->line_info
))
5068 if (symbols
== NULL
)
5071 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5072 filename_ptr
, functionname_ptr
))
5080 _bfd_elf_sizeof_headers (abfd
, reloc
)
5086 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5088 ret
+= get_program_header_size (abfd
);
5093 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5098 bfd_size_type count
;
5100 Elf_Internal_Shdr
*hdr
;
5103 if (! abfd
->output_has_begun
5104 && ! _bfd_elf_compute_section_file_positions
5105 (abfd
, (struct bfd_link_info
*) NULL
))
5108 hdr
= &elf_section_data (section
)->this_hdr
;
5109 pos
= hdr
->sh_offset
+ offset
;
5110 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5111 || bfd_bwrite (location
, count
, abfd
) != count
)
5118 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5119 bfd
*abfd ATTRIBUTE_UNUSED
;
5120 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5121 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5128 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5131 Elf_Internal_Rel
*dst
;
5137 /* Try to convert a non-ELF reloc into an ELF one. */
5140 _bfd_elf_validate_reloc (abfd
, areloc
)
5144 /* Check whether we really have an ELF howto. */
5146 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5148 bfd_reloc_code_real_type code
;
5149 reloc_howto_type
*howto
;
5151 /* Alien reloc: Try to determine its type to replace it with an
5152 equivalent ELF reloc. */
5154 if (areloc
->howto
->pc_relative
)
5156 switch (areloc
->howto
->bitsize
)
5159 code
= BFD_RELOC_8_PCREL
;
5162 code
= BFD_RELOC_12_PCREL
;
5165 code
= BFD_RELOC_16_PCREL
;
5168 code
= BFD_RELOC_24_PCREL
;
5171 code
= BFD_RELOC_32_PCREL
;
5174 code
= BFD_RELOC_64_PCREL
;
5180 howto
= bfd_reloc_type_lookup (abfd
, code
);
5182 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5184 if (howto
->pcrel_offset
)
5185 areloc
->addend
+= areloc
->address
;
5187 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5192 switch (areloc
->howto
->bitsize
)
5198 code
= BFD_RELOC_14
;
5201 code
= BFD_RELOC_16
;
5204 code
= BFD_RELOC_26
;
5207 code
= BFD_RELOC_32
;
5210 code
= BFD_RELOC_64
;
5216 howto
= bfd_reloc_type_lookup (abfd
, code
);
5220 areloc
->howto
= howto
;
5228 (*_bfd_error_handler
)
5229 (_("%s: unsupported relocation type %s"),
5230 bfd_archive_filename (abfd
), areloc
->howto
->name
);
5231 bfd_set_error (bfd_error_bad_value
);
5236 _bfd_elf_close_and_cleanup (abfd
)
5239 if (bfd_get_format (abfd
) == bfd_object
)
5241 if (elf_shstrtab (abfd
) != NULL
)
5242 _bfd_stringtab_free (elf_shstrtab (abfd
));
5245 return _bfd_generic_close_and_cleanup (abfd
);
5248 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5249 in the relocation's offset. Thus we cannot allow any sort of sanity
5250 range-checking to interfere. There is nothing else to do in processing
5253 bfd_reloc_status_type
5254 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5255 bfd
*abfd ATTRIBUTE_UNUSED
;
5256 arelent
*re ATTRIBUTE_UNUSED
;
5257 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5258 PTR data ATTRIBUTE_UNUSED
;
5259 asection
*is ATTRIBUTE_UNUSED
;
5260 bfd
*obfd ATTRIBUTE_UNUSED
;
5261 char **errmsg ATTRIBUTE_UNUSED
;
5263 return bfd_reloc_ok
;
5266 /* Elf core file support. Much of this only works on native
5267 toolchains, since we rely on knowing the
5268 machine-dependent procfs structure in order to pick
5269 out details about the corefile. */
5271 #ifdef HAVE_SYS_PROCFS_H
5272 # include <sys/procfs.h>
5275 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5278 elfcore_make_pid (abfd
)
5281 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5282 + (elf_tdata (abfd
)->core_pid
));
5285 /* If there isn't a section called NAME, make one, using
5286 data from SECT. Note, this function will generate a
5287 reference to NAME, so you shouldn't deallocate or
5291 elfcore_maybe_make_sect (abfd
, name
, sect
)
5298 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5301 sect2
= bfd_make_section (abfd
, name
);
5305 sect2
->_raw_size
= sect
->_raw_size
;
5306 sect2
->filepos
= sect
->filepos
;
5307 sect2
->flags
= sect
->flags
;
5308 sect2
->alignment_power
= sect
->alignment_power
;
5312 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5313 actually creates up to two pseudosections:
5314 - For the single-threaded case, a section named NAME, unless
5315 such a section already exists.
5316 - For the multi-threaded case, a section named "NAME/PID", where
5317 PID is elfcore_make_pid (abfd).
5318 Both pseudosections have identical contents. */
5320 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
5327 char *threaded_name
;
5330 /* Build the section name. */
5332 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5333 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5334 if (threaded_name
== NULL
)
5336 strcpy (threaded_name
, buf
);
5338 sect
= bfd_make_section (abfd
, threaded_name
);
5341 sect
->_raw_size
= size
;
5342 sect
->filepos
= filepos
;
5343 sect
->flags
= SEC_HAS_CONTENTS
;
5344 sect
->alignment_power
= 2;
5346 return elfcore_maybe_make_sect (abfd
, name
, sect
);
5349 /* prstatus_t exists on:
5351 linux 2.[01] + glibc
5355 #if defined (HAVE_PRSTATUS_T)
5356 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
5359 elfcore_grok_prstatus (abfd
, note
)
5361 Elf_Internal_Note
*note
;
5366 if (note
->descsz
== sizeof (prstatus_t
))
5370 raw_size
= sizeof (prstat
.pr_reg
);
5371 offset
= offsetof (prstatus_t
, pr_reg
);
5372 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5374 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5375 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5377 /* pr_who exists on:
5380 pr_who doesn't exist on:
5383 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5384 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5387 #if defined (HAVE_PRSTATUS32_T)
5388 else if (note
->descsz
== sizeof (prstatus32_t
))
5390 /* 64-bit host, 32-bit corefile */
5391 prstatus32_t prstat
;
5393 raw_size
= sizeof (prstat
.pr_reg
);
5394 offset
= offsetof (prstatus32_t
, pr_reg
);
5395 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5397 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5398 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5400 /* pr_who exists on:
5403 pr_who doesn't exist on:
5406 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5407 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5410 #endif /* HAVE_PRSTATUS32_T */
5413 /* Fail - we don't know how to handle any other
5414 note size (ie. data object type). */
5418 /* Make a ".reg/999" section and a ".reg" section. */
5419 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
5420 raw_size
, note
->descpos
+ offset
);
5422 #endif /* defined (HAVE_PRSTATUS_T) */
5424 /* Create a pseudosection containing the exact contents of NOTE. */
5426 elfcore_make_note_pseudosection (abfd
, name
, note
)
5429 Elf_Internal_Note
*note
;
5431 return _bfd_elfcore_make_pseudosection (abfd
, name
,
5432 note
->descsz
, note
->descpos
);
5435 /* There isn't a consistent prfpregset_t across platforms,
5436 but it doesn't matter, because we don't have to pick this
5437 data structure apart. */
5440 elfcore_grok_prfpreg (abfd
, note
)
5442 Elf_Internal_Note
*note
;
5444 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
5447 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5448 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5452 elfcore_grok_prxfpreg (abfd
, note
)
5454 Elf_Internal_Note
*note
;
5456 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
5459 #if defined (HAVE_PRPSINFO_T)
5460 typedef prpsinfo_t elfcore_psinfo_t
;
5461 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5462 typedef prpsinfo32_t elfcore_psinfo32_t
;
5466 #if defined (HAVE_PSINFO_T)
5467 typedef psinfo_t elfcore_psinfo_t
;
5468 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5469 typedef psinfo32_t elfcore_psinfo32_t
;
5473 /* return a malloc'ed copy of a string at START which is at
5474 most MAX bytes long, possibly without a terminating '\0'.
5475 the copy will always have a terminating '\0'. */
5478 _bfd_elfcore_strndup (abfd
, start
, max
)
5484 char *end
= memchr (start
, '\0', max
);
5492 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
5496 memcpy (dups
, start
, len
);
5502 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5503 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
5506 elfcore_grok_psinfo (abfd
, note
)
5508 Elf_Internal_Note
*note
;
5510 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
5512 elfcore_psinfo_t psinfo
;
5514 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5516 elf_tdata (abfd
)->core_program
5517 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
5518 sizeof (psinfo
.pr_fname
));
5520 elf_tdata (abfd
)->core_command
5521 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
5522 sizeof (psinfo
.pr_psargs
));
5524 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5525 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
5527 /* 64-bit host, 32-bit corefile */
5528 elfcore_psinfo32_t psinfo
;
5530 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5532 elf_tdata (abfd
)->core_program
5533 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
5534 sizeof (psinfo
.pr_fname
));
5536 elf_tdata (abfd
)->core_command
5537 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
5538 sizeof (psinfo
.pr_psargs
));
5544 /* Fail - we don't know how to handle any other
5545 note size (ie. data object type). */
5549 /* Note that for some reason, a spurious space is tacked
5550 onto the end of the args in some (at least one anyway)
5551 implementations, so strip it off if it exists. */
5554 char *command
= elf_tdata (abfd
)->core_command
;
5555 int n
= strlen (command
);
5557 if (0 < n
&& command
[n
- 1] == ' ')
5558 command
[n
- 1] = '\0';
5563 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5565 #if defined (HAVE_PSTATUS_T)
5567 elfcore_grok_pstatus (abfd
, note
)
5569 Elf_Internal_Note
*note
;
5571 if (note
->descsz
== sizeof (pstatus_t
)
5572 #if defined (HAVE_PXSTATUS_T)
5573 || note
->descsz
== sizeof (pxstatus_t
)
5579 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5581 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5583 #if defined (HAVE_PSTATUS32_T)
5584 else if (note
->descsz
== sizeof (pstatus32_t
))
5586 /* 64-bit host, 32-bit corefile */
5589 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5591 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5594 /* Could grab some more details from the "representative"
5595 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5596 NT_LWPSTATUS note, presumably. */
5600 #endif /* defined (HAVE_PSTATUS_T) */
5602 #if defined (HAVE_LWPSTATUS_T)
5604 elfcore_grok_lwpstatus (abfd
, note
)
5606 Elf_Internal_Note
*note
;
5608 lwpstatus_t lwpstat
;
5613 if (note
->descsz
!= sizeof (lwpstat
)
5614 #if defined (HAVE_LWPXSTATUS_T)
5615 && note
->descsz
!= sizeof (lwpxstatus_t
)
5620 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
5622 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
5623 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
5625 /* Make a ".reg/999" section. */
5627 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5628 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5633 sect
= bfd_make_section (abfd
, name
);
5637 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5638 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
5639 sect
->filepos
= note
->descpos
5640 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
5643 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5644 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
5645 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
5648 sect
->flags
= SEC_HAS_CONTENTS
;
5649 sect
->alignment_power
= 2;
5651 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5654 /* Make a ".reg2/999" section */
5656 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
5657 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5662 sect
= bfd_make_section (abfd
, name
);
5666 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5667 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
5668 sect
->filepos
= note
->descpos
5669 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
5672 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5673 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
5674 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
5677 sect
->flags
= SEC_HAS_CONTENTS
;
5678 sect
->alignment_power
= 2;
5680 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
5682 #endif /* defined (HAVE_LWPSTATUS_T) */
5684 #if defined (HAVE_WIN32_PSTATUS_T)
5686 elfcore_grok_win32pstatus (abfd
, note
)
5688 Elf_Internal_Note
*note
;
5693 win32_pstatus_t pstatus
;
5695 if (note
->descsz
< sizeof (pstatus
))
5698 memcpy (&pstatus
, note
->descdata
, note
->descsz
);
5700 switch (pstatus
.data_type
)
5702 case NOTE_INFO_PROCESS
:
5703 /* FIXME: need to add ->core_command. */
5704 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
5705 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
5708 case NOTE_INFO_THREAD
:
5709 /* Make a ".reg/999" section. */
5710 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
5712 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5718 sect
= bfd_make_section (abfd
, name
);
5722 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
5723 sect
->filepos
= note
->descpos
+ offsetof (struct win32_pstatus
,
5724 data
.thread_info
.thread_context
);
5725 sect
->flags
= SEC_HAS_CONTENTS
;
5726 sect
->alignment_power
= 2;
5728 if (pstatus
.data
.thread_info
.is_active_thread
)
5729 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5733 case NOTE_INFO_MODULE
:
5734 /* Make a ".module/xxxxxxxx" section. */
5735 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
5737 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5743 sect
= bfd_make_section (abfd
, name
);
5748 sect
->_raw_size
= note
->descsz
;
5749 sect
->filepos
= note
->descpos
;
5750 sect
->flags
= SEC_HAS_CONTENTS
;
5751 sect
->alignment_power
= 2;
5760 #endif /* HAVE_WIN32_PSTATUS_T */
5763 elfcore_grok_note (abfd
, note
)
5765 Elf_Internal_Note
*note
;
5767 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5775 if (bed
->elf_backend_grok_prstatus
)
5776 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
5778 #if defined (HAVE_PRSTATUS_T)
5779 return elfcore_grok_prstatus (abfd
, note
);
5784 #if defined (HAVE_PSTATUS_T)
5786 return elfcore_grok_pstatus (abfd
, note
);
5789 #if defined (HAVE_LWPSTATUS_T)
5791 return elfcore_grok_lwpstatus (abfd
, note
);
5794 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
5795 return elfcore_grok_prfpreg (abfd
, note
);
5797 #if defined (HAVE_WIN32_PSTATUS_T)
5798 case NT_WIN32PSTATUS
:
5799 return elfcore_grok_win32pstatus (abfd
, note
);
5802 case NT_PRXFPREG
: /* Linux SSE extension */
5803 if (note
->namesz
== 5
5804 && ! strcmp (note
->namedata
, "LINUX"))
5805 return elfcore_grok_prxfpreg (abfd
, note
);
5811 if (bed
->elf_backend_grok_psinfo
)
5812 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
5814 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5815 return elfcore_grok_psinfo (abfd
, note
);
5823 elfcore_read_notes (abfd
, offset
, size
)
5834 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
5837 buf
= bfd_malloc (size
);
5841 if (bfd_bread (buf
, size
, abfd
) != size
)
5849 while (p
< buf
+ size
)
5851 /* FIXME: bad alignment assumption. */
5852 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
5853 Elf_Internal_Note in
;
5855 in
.type
= H_GET_32 (abfd
, xnp
->type
);
5857 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
5858 in
.namedata
= xnp
->name
;
5860 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
5861 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
5862 in
.descpos
= offset
+ (in
.descdata
- buf
);
5864 if (! elfcore_grok_note (abfd
, &in
))
5867 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
5874 /* Providing external access to the ELF program header table. */
5876 /* Return an upper bound on the number of bytes required to store a
5877 copy of ABFD's program header table entries. Return -1 if an error
5878 occurs; bfd_get_error will return an appropriate code. */
5881 bfd_get_elf_phdr_upper_bound (abfd
)
5884 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5886 bfd_set_error (bfd_error_wrong_format
);
5890 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
5893 /* Copy ABFD's program header table entries to *PHDRS. The entries
5894 will be stored as an array of Elf_Internal_Phdr structures, as
5895 defined in include/elf/internal.h. To find out how large the
5896 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5898 Return the number of program header table entries read, or -1 if an
5899 error occurs; bfd_get_error will return an appropriate code. */
5902 bfd_get_elf_phdrs (abfd
, phdrs
)
5908 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5910 bfd_set_error (bfd_error_wrong_format
);
5914 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
5915 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
5916 num_phdrs
* sizeof (Elf_Internal_Phdr
));
5922 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
5923 bfd
*abfd ATTRIBUTE_UNUSED
;
5928 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
5930 i_ehdrp
= elf_elfheader (abfd
);
5931 if (i_ehdrp
== NULL
)
5932 sprintf_vma (buf
, value
);
5935 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
5937 #if BFD_HOST_64BIT_LONG
5938 sprintf (buf
, "%016lx", value
);
5940 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
5941 _bfd_int64_low (value
));
5945 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
5948 sprintf_vma (buf
, value
);
5953 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
5954 bfd
*abfd ATTRIBUTE_UNUSED
;
5959 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
5961 i_ehdrp
= elf_elfheader (abfd
);
5962 if (i_ehdrp
== NULL
)
5963 fprintf_vma ((FILE *) stream
, value
);
5966 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
5968 #if BFD_HOST_64BIT_LONG
5969 fprintf ((FILE *) stream
, "%016lx", value
);
5971 fprintf ((FILE *) stream
, "%08lx%08lx",
5972 _bfd_int64_high (value
), _bfd_int64_low (value
));
5976 fprintf ((FILE *) stream
, "%08lx",
5977 (unsigned long) (value
& 0xffffffff));
5980 fprintf_vma ((FILE *) stream
, value
);
5984 enum elf_reloc_type_class
5985 _bfd_elf_reloc_type_class (type
)
5986 int type ATTRIBUTE_UNUSED
;
5988 return reloc_class_normal
;