1 /* ELF executable support for BFD.
2 Copyright 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
41 static INLINE
struct elf_segment_map
*make_mapping
42 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, boolean
));
43 static boolean map_sections_to_segments
PARAMS ((bfd
*));
44 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
45 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
46 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
47 static boolean prep_headers
PARAMS ((bfd
*));
48 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**, int));
49 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
50 static char *elf_read
PARAMS ((bfd
*, long, unsigned int));
51 static void elf_fake_sections
PARAMS ((bfd
*, asection
*, PTR
));
52 static boolean assign_section_numbers
PARAMS ((bfd
*));
53 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
54 static boolean elf_map_symbols
PARAMS ((bfd
*));
55 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
57 /* Swap version information in and out. The version information is
58 currently size independent. If that ever changes, this code will
59 need to move into elfcode.h. */
61 /* Swap in a Verdef structure. */
64 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
66 const Elf_External_Verdef
*src
;
67 Elf_Internal_Verdef
*dst
;
69 dst
->vd_version
= bfd_h_get_16 (abfd
, src
->vd_version
);
70 dst
->vd_flags
= bfd_h_get_16 (abfd
, src
->vd_flags
);
71 dst
->vd_ndx
= bfd_h_get_16 (abfd
, src
->vd_ndx
);
72 dst
->vd_cnt
= bfd_h_get_16 (abfd
, src
->vd_cnt
);
73 dst
->vd_hash
= bfd_h_get_32 (abfd
, src
->vd_hash
);
74 dst
->vd_aux
= bfd_h_get_32 (abfd
, src
->vd_aux
);
75 dst
->vd_next
= bfd_h_get_32 (abfd
, src
->vd_next
);
78 /* Swap out a Verdef structure. */
81 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
83 const Elf_Internal_Verdef
*src
;
84 Elf_External_Verdef
*dst
;
86 bfd_h_put_16 (abfd
, src
->vd_version
, dst
->vd_version
);
87 bfd_h_put_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
88 bfd_h_put_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
89 bfd_h_put_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
90 bfd_h_put_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
91 bfd_h_put_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
92 bfd_h_put_32 (abfd
, src
->vd_next
, dst
->vd_next
);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
100 const Elf_External_Verdaux
*src
;
101 Elf_Internal_Verdaux
*dst
;
103 dst
->vda_name
= bfd_h_get_32 (abfd
, src
->vda_name
);
104 dst
->vda_next
= bfd_h_get_32 (abfd
, src
->vda_next
);
107 /* Swap out a Verdaux structure. */
110 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
112 const Elf_Internal_Verdaux
*src
;
113 Elf_External_Verdaux
*dst
;
115 bfd_h_put_32 (abfd
, src
->vda_name
, dst
->vda_name
);
116 bfd_h_put_32 (abfd
, src
->vda_next
, dst
->vda_next
);
119 /* Swap in a Verneed structure. */
122 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
124 const Elf_External_Verneed
*src
;
125 Elf_Internal_Verneed
*dst
;
127 dst
->vn_version
= bfd_h_get_16 (abfd
, src
->vn_version
);
128 dst
->vn_cnt
= bfd_h_get_16 (abfd
, src
->vn_cnt
);
129 dst
->vn_file
= bfd_h_get_32 (abfd
, src
->vn_file
);
130 dst
->vn_aux
= bfd_h_get_32 (abfd
, src
->vn_aux
);
131 dst
->vn_next
= bfd_h_get_32 (abfd
, src
->vn_next
);
134 /* Swap out a Verneed structure. */
137 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
139 const Elf_Internal_Verneed
*src
;
140 Elf_External_Verneed
*dst
;
142 bfd_h_put_16 (abfd
, src
->vn_version
, dst
->vn_version
);
143 bfd_h_put_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
144 bfd_h_put_32 (abfd
, src
->vn_file
, dst
->vn_file
);
145 bfd_h_put_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
146 bfd_h_put_32 (abfd
, src
->vn_next
, dst
->vn_next
);
149 /* Swap in a Vernaux structure. */
152 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
154 const Elf_External_Vernaux
*src
;
155 Elf_Internal_Vernaux
*dst
;
157 dst
->vna_hash
= bfd_h_get_32 (abfd
, src
->vna_hash
);
158 dst
->vna_flags
= bfd_h_get_16 (abfd
, src
->vna_flags
);
159 dst
->vna_other
= bfd_h_get_16 (abfd
, src
->vna_other
);
160 dst
->vna_name
= bfd_h_get_32 (abfd
, src
->vna_name
);
161 dst
->vna_next
= bfd_h_get_32 (abfd
, src
->vna_next
);
164 /* Swap out a Vernaux structure. */
167 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
169 const Elf_Internal_Vernaux
*src
;
170 Elf_External_Vernaux
*dst
;
172 bfd_h_put_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
173 bfd_h_put_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
174 bfd_h_put_16 (abfd
, src
->vna_other
, dst
->vna_other
);
175 bfd_h_put_32 (abfd
, src
->vna_name
, dst
->vna_name
);
176 bfd_h_put_32 (abfd
, src
->vna_next
, dst
->vna_next
);
179 /* Swap in a Versym structure. */
182 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
184 const Elf_External_Versym
*src
;
185 Elf_Internal_Versym
*dst
;
187 dst
->vs_vers
= bfd_h_get_16 (abfd
, src
->vs_vers
);
190 /* Swap out a Versym structure. */
193 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
195 const Elf_Internal_Versym
*src
;
196 Elf_External_Versym
*dst
;
198 bfd_h_put_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
201 /* Standard ELF hash function. Do not change this function; you will
202 cause invalid hash tables to be generated. */
205 bfd_elf_hash (namearg
)
208 const unsigned char *name
= (const unsigned char *) namearg
;
213 while ((ch
= *name
++) != '\0')
216 if ((g
= (h
& 0xf0000000)) != 0)
219 /* The ELF ABI says `h &= ~g', but this is equivalent in
220 this case and on some machines one insn instead of two. */
227 /* Read a specified number of bytes at a specified offset in an ELF
228 file, into a newly allocated buffer, and return a pointer to the
232 elf_read (abfd
, offset
, size
)
239 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
241 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
243 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
245 if (bfd_get_error () != bfd_error_system_call
)
246 bfd_set_error (bfd_error_file_truncated
);
253 bfd_elf_mkobject (abfd
)
256 /* this just does initialization */
257 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
258 elf_tdata (abfd
) = (struct elf_obj_tdata
*)
259 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
260 if (elf_tdata (abfd
) == 0)
262 /* since everything is done at close time, do we need any
269 bfd_elf_mkcorefile (abfd
)
272 /* I think this can be done just like an object file. */
273 return bfd_elf_mkobject (abfd
);
277 bfd_elf_get_str_section (abfd
, shindex
)
279 unsigned int shindex
;
281 Elf_Internal_Shdr
**i_shdrp
;
282 char *shstrtab
= NULL
;
284 unsigned int shstrtabsize
;
286 i_shdrp
= elf_elfsections (abfd
);
287 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
290 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
291 if (shstrtab
== NULL
)
293 /* No cached one, attempt to read, and cache what we read. */
294 offset
= i_shdrp
[shindex
]->sh_offset
;
295 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
296 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
297 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
303 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
305 unsigned int shindex
;
306 unsigned int strindex
;
308 Elf_Internal_Shdr
*hdr
;
313 hdr
= elf_elfsections (abfd
)[shindex
];
315 if (hdr
->contents
== NULL
316 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
319 if (strindex
>= hdr
->sh_size
)
321 (*_bfd_error_handler
)
322 (_("%s: invalid string offset %u >= %lu for section `%s'"),
323 bfd_get_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
324 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
325 && strindex
== hdr
->sh_name
)
327 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
331 return ((char *) hdr
->contents
) + strindex
;
334 /* Make a BFD section from an ELF section. We store a pointer to the
335 BFD section in the bfd_section field of the header. */
338 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
340 Elf_Internal_Shdr
*hdr
;
346 if (hdr
->bfd_section
!= NULL
)
348 BFD_ASSERT (strcmp (name
,
349 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
353 newsect
= bfd_make_section_anyway (abfd
, name
);
357 newsect
->filepos
= hdr
->sh_offset
;
359 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
360 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
361 || ! bfd_set_section_alignment (abfd
, newsect
,
362 bfd_log2 (hdr
->sh_addralign
)))
365 flags
= SEC_NO_FLAGS
;
366 if (hdr
->sh_type
!= SHT_NOBITS
)
367 flags
|= SEC_HAS_CONTENTS
;
368 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
371 if (hdr
->sh_type
!= SHT_NOBITS
)
374 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
375 flags
|= SEC_READONLY
;
376 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
378 else if ((flags
& SEC_LOAD
) != 0)
381 /* The debugging sections appear to be recognized only by name, not
383 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
384 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
385 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
386 flags
|= SEC_DEBUGGING
;
388 /* As a GNU extension, if the name begins with .gnu.linkonce, we
389 only link a single copy of the section. This is used to support
390 g++. g++ will emit each template expansion in its own section.
391 The symbols will be defined as weak, so that multiple definitions
392 are permitted. The GNU linker extension is to actually discard
393 all but one of the sections. */
394 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
395 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
397 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
400 if ((flags
& SEC_ALLOC
) != 0)
402 Elf_Internal_Phdr
*phdr
;
405 /* Look through the phdrs to see if we need to adjust the lma.
406 If all the p_paddr fields are zero, we ignore them, since
407 some ELF linkers produce such output. */
408 phdr
= elf_tdata (abfd
)->phdr
;
409 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
411 if (phdr
->p_paddr
!= 0)
414 if (i
< elf_elfheader (abfd
)->e_phnum
)
416 phdr
= elf_tdata (abfd
)->phdr
;
417 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
419 if (phdr
->p_type
== PT_LOAD
420 && phdr
->p_vaddr
!= phdr
->p_paddr
421 && phdr
->p_vaddr
<= hdr
->sh_addr
422 && (phdr
->p_vaddr
+ phdr
->p_memsz
423 >= hdr
->sh_addr
+ hdr
->sh_size
)
424 && ((flags
& SEC_LOAD
) == 0
425 || (phdr
->p_offset
<= (bfd_vma
) hdr
->sh_offset
426 && (phdr
->p_offset
+ phdr
->p_filesz
427 >= hdr
->sh_offset
+ hdr
->sh_size
))))
429 newsect
->lma
+= phdr
->p_paddr
- phdr
->p_vaddr
;
436 hdr
->bfd_section
= newsect
;
437 elf_section_data (newsect
)->this_hdr
= *hdr
;
447 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
450 Helper functions for GDB to locate the string tables.
451 Since BFD hides string tables from callers, GDB needs to use an
452 internal hook to find them. Sun's .stabstr, in particular,
453 isn't even pointed to by the .stab section, so ordinary
454 mechanisms wouldn't work to find it, even if we had some.
457 struct elf_internal_shdr
*
458 bfd_elf_find_section (abfd
, name
)
462 Elf_Internal_Shdr
**i_shdrp
;
467 i_shdrp
= elf_elfsections (abfd
);
470 shstrtab
= bfd_elf_get_str_section
471 (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
472 if (shstrtab
!= NULL
)
474 max
= elf_elfheader (abfd
)->e_shnum
;
475 for (i
= 1; i
< max
; i
++)
476 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
483 const char *const bfd_elf_section_type_names
[] = {
484 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
485 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
486 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
489 /* ELF relocs are against symbols. If we are producing relocateable
490 output, and the reloc is against an external symbol, and nothing
491 has given us any additional addend, the resulting reloc will also
492 be against the same symbol. In such a case, we don't want to
493 change anything about the way the reloc is handled, since it will
494 all be done at final link time. Rather than put special case code
495 into bfd_perform_relocation, all the reloc types use this howto
496 function. It just short circuits the reloc if producing
497 relocateable output against an external symbol. */
500 bfd_reloc_status_type
501 bfd_elf_generic_reloc (abfd
,
508 bfd
*abfd ATTRIBUTE_UNUSED
;
509 arelent
*reloc_entry
;
511 PTR data ATTRIBUTE_UNUSED
;
512 asection
*input_section
;
514 char **error_message ATTRIBUTE_UNUSED
;
516 if (output_bfd
!= (bfd
*) NULL
517 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
518 && (! reloc_entry
->howto
->partial_inplace
519 || reloc_entry
->addend
== 0))
521 reloc_entry
->address
+= input_section
->output_offset
;
525 return bfd_reloc_continue
;
528 /* Print out the program headers. */
531 _bfd_elf_print_private_bfd_data (abfd
, farg
)
535 FILE *f
= (FILE *) farg
;
536 Elf_Internal_Phdr
*p
;
538 bfd_byte
*dynbuf
= NULL
;
540 p
= elf_tdata (abfd
)->phdr
;
545 fprintf (f
, _("\nProgram Header:\n"));
546 c
= elf_elfheader (abfd
)->e_phnum
;
547 for (i
= 0; i
< c
; i
++, p
++)
554 case PT_NULL
: s
= "NULL"; break;
555 case PT_LOAD
: s
= "LOAD"; break;
556 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
557 case PT_INTERP
: s
= "INTERP"; break;
558 case PT_NOTE
: s
= "NOTE"; break;
559 case PT_SHLIB
: s
= "SHLIB"; break;
560 case PT_PHDR
: s
= "PHDR"; break;
561 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
563 fprintf (f
, "%8s off 0x", s
);
564 fprintf_vma (f
, p
->p_offset
);
565 fprintf (f
, " vaddr 0x");
566 fprintf_vma (f
, p
->p_vaddr
);
567 fprintf (f
, " paddr 0x");
568 fprintf_vma (f
, p
->p_paddr
);
569 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
570 fprintf (f
, " filesz 0x");
571 fprintf_vma (f
, p
->p_filesz
);
572 fprintf (f
, " memsz 0x");
573 fprintf_vma (f
, p
->p_memsz
);
574 fprintf (f
, " flags %c%c%c",
575 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
576 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
577 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
578 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
579 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
584 s
= bfd_get_section_by_name (abfd
, ".dynamic");
589 bfd_byte
*extdyn
, *extdynend
;
591 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
593 fprintf (f
, _("\nDynamic Section:\n"));
595 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
598 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
602 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
605 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
607 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
608 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
611 extdynend
= extdyn
+ s
->_raw_size
;
612 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
614 Elf_Internal_Dyn dyn
;
619 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
621 if (dyn
.d_tag
== DT_NULL
)
628 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
632 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
633 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
634 case DT_PLTGOT
: name
= "PLTGOT"; break;
635 case DT_HASH
: name
= "HASH"; break;
636 case DT_STRTAB
: name
= "STRTAB"; break;
637 case DT_SYMTAB
: name
= "SYMTAB"; break;
638 case DT_RELA
: name
= "RELA"; break;
639 case DT_RELASZ
: name
= "RELASZ"; break;
640 case DT_RELAENT
: name
= "RELAENT"; break;
641 case DT_STRSZ
: name
= "STRSZ"; break;
642 case DT_SYMENT
: name
= "SYMENT"; break;
643 case DT_INIT
: name
= "INIT"; break;
644 case DT_FINI
: name
= "FINI"; break;
645 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
646 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
647 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
648 case DT_REL
: name
= "REL"; break;
649 case DT_RELSZ
: name
= "RELSZ"; break;
650 case DT_RELENT
: name
= "RELENT"; break;
651 case DT_PLTREL
: name
= "PLTREL"; break;
652 case DT_DEBUG
: name
= "DEBUG"; break;
653 case DT_TEXTREL
: name
= "TEXTREL"; break;
654 case DT_JMPREL
: name
= "JMPREL"; break;
655 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
656 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
657 case DT_VERSYM
: name
= "VERSYM"; break;
658 case DT_VERDEF
: name
= "VERDEF"; break;
659 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
660 case DT_VERNEED
: name
= "VERNEED"; break;
661 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
664 fprintf (f
, " %-11s ", name
);
666 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
671 string
= bfd_elf_string_from_elf_section (abfd
, link
,
675 fprintf (f
, "%s", string
);
684 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
685 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
687 if (! _bfd_elf_slurp_version_tables (abfd
))
691 if (elf_dynverdef (abfd
) != 0)
693 Elf_Internal_Verdef
*t
;
695 fprintf (f
, _("\nVersion definitions:\n"));
696 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
698 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
699 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
700 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
702 Elf_Internal_Verdaux
*a
;
705 for (a
= t
->vd_auxptr
->vda_nextptr
;
708 fprintf (f
, "%s ", a
->vda_nodename
);
714 if (elf_dynverref (abfd
) != 0)
716 Elf_Internal_Verneed
*t
;
718 fprintf (f
, _("\nVersion References:\n"));
719 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
721 Elf_Internal_Vernaux
*a
;
723 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
724 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
725 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
726 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
738 /* Display ELF-specific fields of a symbol. */
741 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
745 bfd_print_symbol_type how
;
747 FILE *file
= (FILE *) filep
;
750 case bfd_print_symbol_name
:
751 fprintf (file
, "%s", symbol
->name
);
753 case bfd_print_symbol_more
:
754 fprintf (file
, "elf ");
755 fprintf_vma (file
, symbol
->value
);
756 fprintf (file
, " %lx", (long) symbol
->flags
);
758 case bfd_print_symbol_all
:
760 CONST
char *section_name
;
761 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
762 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
763 fprintf (file
, " %s\t", section_name
);
764 /* Print the "other" value for a symbol. For common symbols,
765 we've already printed the size; now print the alignment.
766 For other symbols, we have no specified alignment, and
767 we've printed the address; now print the size. */
769 (bfd_is_com_section (symbol
->section
)
770 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
771 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
773 /* If we have version information, print it. */
774 if (elf_tdata (abfd
)->dynversym_section
!= 0
775 && (elf_tdata (abfd
)->dynverdef_section
!= 0
776 || elf_tdata (abfd
)->dynverref_section
!= 0))
779 const char *version_string
;
781 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
785 else if (vernum
== 1)
786 version_string
= "Base";
787 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
789 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
792 Elf_Internal_Verneed
*t
;
795 for (t
= elf_tdata (abfd
)->verref
;
799 Elf_Internal_Vernaux
*a
;
801 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
803 if (a
->vna_other
== vernum
)
805 version_string
= a
->vna_nodename
;
812 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
813 fprintf (file
, " %-11s", version_string
);
818 fprintf (file
, " (%s)", version_string
);
819 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
824 /* If the st_other field is not zero, print it. */
825 if (((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
!= 0)
826 fprintf (file
, " 0x%02x",
828 ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
));
830 fprintf (file
, " %s", symbol
->name
);
836 /* Create an entry in an ELF linker hash table. */
838 struct bfd_hash_entry
*
839 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
840 struct bfd_hash_entry
*entry
;
841 struct bfd_hash_table
*table
;
844 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
846 /* Allocate the structure if it has not already been allocated by a
848 if (ret
== (struct elf_link_hash_entry
*) NULL
)
849 ret
= ((struct elf_link_hash_entry
*)
850 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
851 if (ret
== (struct elf_link_hash_entry
*) NULL
)
852 return (struct bfd_hash_entry
*) ret
;
854 /* Call the allocation method of the superclass. */
855 ret
= ((struct elf_link_hash_entry
*)
856 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
858 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
860 /* Set local fields. */
864 ret
->dynstr_index
= 0;
866 ret
->got
.offset
= (bfd_vma
) -1;
867 ret
->plt
.offset
= (bfd_vma
) -1;
868 ret
->linker_section_pointer
= (elf_linker_section_pointers_t
*)0;
869 ret
->verinfo
.verdef
= NULL
;
870 ret
->vtable_entries_used
= NULL
;
871 ret
->vtable_entries_size
= 0;
872 ret
->vtable_parent
= NULL
;
873 ret
->type
= STT_NOTYPE
;
875 /* Assume that we have been called by a non-ELF symbol reader.
876 This flag is then reset by the code which reads an ELF input
877 file. This ensures that a symbol created by a non-ELF symbol
878 reader will have the flag set correctly. */
879 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
882 return (struct bfd_hash_entry
*) ret
;
885 /* Initialize an ELF linker hash table. */
888 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
889 struct elf_link_hash_table
*table
;
891 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
892 struct bfd_hash_table
*,
895 table
->dynamic_sections_created
= false;
896 table
->dynobj
= NULL
;
897 /* The first dynamic symbol is a dummy. */
898 table
->dynsymcount
= 1;
899 table
->dynstr
= NULL
;
900 table
->bucketcount
= 0;
901 table
->needed
= NULL
;
903 table
->stab_info
= NULL
;
904 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
907 /* Create an ELF linker hash table. */
909 struct bfd_link_hash_table
*
910 _bfd_elf_link_hash_table_create (abfd
)
913 struct elf_link_hash_table
*ret
;
915 ret
= ((struct elf_link_hash_table
*)
916 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
917 if (ret
== (struct elf_link_hash_table
*) NULL
)
920 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
922 bfd_release (abfd
, ret
);
929 /* This is a hook for the ELF emulation code in the generic linker to
930 tell the backend linker what file name to use for the DT_NEEDED
931 entry for a dynamic object. The generic linker passes name as an
932 empty string to indicate that no DT_NEEDED entry should be made. */
935 bfd_elf_set_dt_needed_name (abfd
, name
)
939 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
940 && bfd_get_format (abfd
) == bfd_object
)
941 elf_dt_name (abfd
) = name
;
944 /* Get the list of DT_NEEDED entries for a link. This is a hook for
945 the linker ELF emulation code. */
947 struct bfd_link_needed_list
*
948 bfd_elf_get_needed_list (abfd
, info
)
949 bfd
*abfd ATTRIBUTE_UNUSED
;
950 struct bfd_link_info
*info
;
952 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
954 return elf_hash_table (info
)->needed
;
957 /* Get the name actually used for a dynamic object for a link. This
958 is the SONAME entry if there is one. Otherwise, it is the string
959 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
962 bfd_elf_get_dt_soname (abfd
)
965 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
966 && bfd_get_format (abfd
) == bfd_object
)
967 return elf_dt_name (abfd
);
971 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
972 the ELF linker emulation code. */
975 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
977 struct bfd_link_needed_list
**pneeded
;
980 bfd_byte
*dynbuf
= NULL
;
983 bfd_byte
*extdyn
, *extdynend
;
985 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
989 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
990 || bfd_get_format (abfd
) != bfd_object
)
993 s
= bfd_get_section_by_name (abfd
, ".dynamic");
994 if (s
== NULL
|| s
->_raw_size
== 0)
997 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1001 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1005 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1009 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1011 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1012 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1015 extdynend
= extdyn
+ s
->_raw_size
;
1016 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1018 Elf_Internal_Dyn dyn
;
1020 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1022 if (dyn
.d_tag
== DT_NULL
)
1025 if (dyn
.d_tag
== DT_NEEDED
)
1028 struct bfd_link_needed_list
*l
;
1030 string
= bfd_elf_string_from_elf_section (abfd
, link
,
1035 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, sizeof *l
);
1056 /* Allocate an ELF string table--force the first byte to be zero. */
1058 struct bfd_strtab_hash
*
1059 _bfd_elf_stringtab_init ()
1061 struct bfd_strtab_hash
*ret
;
1063 ret
= _bfd_stringtab_init ();
1068 loc
= _bfd_stringtab_add (ret
, "", true, false);
1069 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1070 if (loc
== (bfd_size_type
) -1)
1072 _bfd_stringtab_free (ret
);
1079 /* ELF .o/exec file reading */
1081 /* Create a new bfd section from an ELF section header. */
1084 bfd_section_from_shdr (abfd
, shindex
)
1086 unsigned int shindex
;
1088 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1089 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1090 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1093 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1095 switch (hdr
->sh_type
)
1098 /* Inactive section. Throw it away. */
1101 case SHT_PROGBITS
: /* Normal section with contents. */
1102 case SHT_DYNAMIC
: /* Dynamic linking information. */
1103 case SHT_NOBITS
: /* .bss section. */
1104 case SHT_HASH
: /* .hash section. */
1105 case SHT_NOTE
: /* .note section. */
1106 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1108 case SHT_SYMTAB
: /* A symbol table */
1109 if (elf_onesymtab (abfd
) == shindex
)
1112 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1113 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1114 elf_onesymtab (abfd
) = shindex
;
1115 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1116 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1117 abfd
->flags
|= HAS_SYMS
;
1119 /* Sometimes a shared object will map in the symbol table. If
1120 SHF_ALLOC is set, and this is a shared object, then we also
1121 treat this section as a BFD section. We can not base the
1122 decision purely on SHF_ALLOC, because that flag is sometimes
1123 set in a relocateable object file, which would confuse the
1125 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1126 && (abfd
->flags
& DYNAMIC
) != 0
1127 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1132 case SHT_DYNSYM
: /* A dynamic symbol table */
1133 if (elf_dynsymtab (abfd
) == shindex
)
1136 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1137 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1138 elf_dynsymtab (abfd
) = shindex
;
1139 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1140 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1141 abfd
->flags
|= HAS_SYMS
;
1143 /* Besides being a symbol table, we also treat this as a regular
1144 section, so that objcopy can handle it. */
1145 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1147 case SHT_STRTAB
: /* A string table */
1148 if (hdr
->bfd_section
!= NULL
)
1150 if (ehdr
->e_shstrndx
== shindex
)
1152 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1153 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1159 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1161 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1162 if (hdr2
->sh_link
== shindex
)
1164 if (! bfd_section_from_shdr (abfd
, i
))
1166 if (elf_onesymtab (abfd
) == i
)
1168 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1169 elf_elfsections (abfd
)[shindex
] =
1170 &elf_tdata (abfd
)->strtab_hdr
;
1173 if (elf_dynsymtab (abfd
) == i
)
1175 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1176 elf_elfsections (abfd
)[shindex
] = hdr
=
1177 &elf_tdata (abfd
)->dynstrtab_hdr
;
1178 /* We also treat this as a regular section, so
1179 that objcopy can handle it. */
1182 #if 0 /* Not handling other string tables specially right now. */
1183 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1184 /* We have a strtab for some random other section. */
1185 newsect
= (asection
*) hdr2
->bfd_section
;
1188 hdr
->bfd_section
= newsect
;
1189 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1191 elf_elfsections (abfd
)[shindex
] = hdr2
;
1197 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1201 /* *These* do a lot of work -- but build no sections! */
1203 asection
*target_sect
;
1204 Elf_Internal_Shdr
*hdr2
;
1206 /* Check for a bogus link to avoid crashing. */
1207 if (hdr
->sh_link
>= ehdr
->e_shnum
)
1209 ((*_bfd_error_handler
)
1210 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1211 bfd_get_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1212 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1215 /* For some incomprehensible reason Oracle distributes
1216 libraries for Solaris in which some of the objects have
1217 bogus sh_link fields. It would be nice if we could just
1218 reject them, but, unfortunately, some people need to use
1219 them. We scan through the section headers; if we find only
1220 one suitable symbol table, we clobber the sh_link to point
1221 to it. I hope this doesn't break anything. */
1222 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1223 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1229 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1231 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1232 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1243 hdr
->sh_link
= found
;
1246 /* Get the symbol table. */
1247 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1248 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1251 /* If this reloc section does not use the main symbol table we
1252 don't treat it as a reloc section. BFD can't adequately
1253 represent such a section, so at least for now, we don't
1254 try. We just present it as a normal section. */
1255 if (hdr
->sh_link
!= elf_onesymtab (abfd
))
1256 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1258 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1260 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1261 if (target_sect
== NULL
)
1264 if ((target_sect
->flags
& SEC_RELOC
) == 0
1265 || target_sect
->reloc_count
== 0)
1266 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1269 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1270 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
1271 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1274 elf_elfsections (abfd
)[shindex
] = hdr2
;
1275 target_sect
->reloc_count
+= hdr
->sh_size
/ hdr
->sh_entsize
;
1276 target_sect
->flags
|= SEC_RELOC
;
1277 target_sect
->relocation
= NULL
;
1278 target_sect
->rel_filepos
= hdr
->sh_offset
;
1279 /* In the section to which the relocations apply, mark whether
1280 its relocations are of the REL or RELA variety. */
1281 elf_section_data (target_sect
)->use_rela_p
1282 = (hdr
->sh_type
== SHT_RELA
);
1283 abfd
->flags
|= HAS_RELOC
;
1288 case SHT_GNU_verdef
:
1289 elf_dynverdef (abfd
) = shindex
;
1290 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1291 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1294 case SHT_GNU_versym
:
1295 elf_dynversym (abfd
) = shindex
;
1296 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1297 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1300 case SHT_GNU_verneed
:
1301 elf_dynverref (abfd
) = shindex
;
1302 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1303 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1310 /* Check for any processor-specific section types. */
1312 if (bed
->elf_backend_section_from_shdr
)
1313 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1321 /* Given an ELF section number, retrieve the corresponding BFD
1325 bfd_section_from_elf_index (abfd
, index
)
1329 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1330 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1332 return elf_elfsections (abfd
)[index
]->bfd_section
;
1336 _bfd_elf_new_section_hook (abfd
, sec
)
1340 struct bfd_elf_section_data
*sdata
;
1342 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, sizeof (*sdata
));
1345 sec
->used_by_bfd
= (PTR
) sdata
;
1347 /* Indicate whether or not this section should use RELA relocations. */
1349 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1354 /* Create a new bfd section from an ELF program header.
1356 Since program segments have no names, we generate a synthetic name
1357 of the form segment<NUM>, where NUM is generally the index in the
1358 program header table. For segments that are split (see below) we
1359 generate the names segment<NUM>a and segment<NUM>b.
1361 Note that some program segments may have a file size that is different than
1362 (less than) the memory size. All this means is that at execution the
1363 system must allocate the amount of memory specified by the memory size,
1364 but only initialize it with the first "file size" bytes read from the
1365 file. This would occur for example, with program segments consisting
1366 of combined data+bss.
1368 To handle the above situation, this routine generates TWO bfd sections
1369 for the single program segment. The first has the length specified by
1370 the file size of the segment, and the second has the length specified
1371 by the difference between the two sizes. In effect, the segment is split
1372 into it's initialized and uninitialized parts.
1377 bfd_section_from_phdr (abfd
, hdr
, index
)
1379 Elf_Internal_Phdr
*hdr
;
1387 split
= ((hdr
->p_memsz
> 0)
1388 && (hdr
->p_filesz
> 0)
1389 && (hdr
->p_memsz
> hdr
->p_filesz
));
1390 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
1391 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1394 strcpy (name
, namebuf
);
1395 newsect
= bfd_make_section (abfd
, name
);
1396 if (newsect
== NULL
)
1398 newsect
->vma
= hdr
->p_vaddr
;
1399 newsect
->lma
= hdr
->p_paddr
;
1400 newsect
->_raw_size
= hdr
->p_filesz
;
1401 newsect
->filepos
= hdr
->p_offset
;
1402 newsect
->flags
|= SEC_HAS_CONTENTS
;
1403 if (hdr
->p_type
== PT_LOAD
)
1405 newsect
->flags
|= SEC_ALLOC
;
1406 newsect
->flags
|= SEC_LOAD
;
1407 if (hdr
->p_flags
& PF_X
)
1409 /* FIXME: all we known is that it has execute PERMISSION,
1411 newsect
->flags
|= SEC_CODE
;
1414 if (!(hdr
->p_flags
& PF_W
))
1416 newsect
->flags
|= SEC_READONLY
;
1421 sprintf (namebuf
, "segment%db", index
);
1422 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1425 strcpy (name
, namebuf
);
1426 newsect
= bfd_make_section (abfd
, name
);
1427 if (newsect
== NULL
)
1429 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1430 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1431 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1432 if (hdr
->p_type
== PT_LOAD
)
1434 newsect
->flags
|= SEC_ALLOC
;
1435 if (hdr
->p_flags
& PF_X
)
1436 newsect
->flags
|= SEC_CODE
;
1438 if (!(hdr
->p_flags
& PF_W
))
1439 newsect
->flags
|= SEC_READONLY
;
1445 /* Initialize REL_HDR, the section-header for new section, containing
1446 relocations against ASECT. If USE_RELA_P is true, we use RELA
1447 relocations; otherwise, we use REL relocations. */
1450 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
1452 Elf_Internal_Shdr
*rel_hdr
;
1457 struct elf_backend_data
*bed
;
1459 bed
= get_elf_backend_data (abfd
);
1460 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1463 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1465 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1467 if (rel_hdr
->sh_name
== (unsigned int) -1)
1469 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1470 rel_hdr
->sh_entsize
= (use_rela_p
1471 ? bed
->s
->sizeof_rela
1472 : bed
->s
->sizeof_rel
);
1473 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
1474 rel_hdr
->sh_flags
= 0;
1475 rel_hdr
->sh_addr
= 0;
1476 rel_hdr
->sh_size
= 0;
1477 rel_hdr
->sh_offset
= 0;
1482 /* Set up an ELF internal section header for a section. */
1486 elf_fake_sections (abfd
, asect
, failedptrarg
)
1491 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1492 boolean
*failedptr
= (boolean
*) failedptrarg
;
1493 Elf_Internal_Shdr
*this_hdr
;
1497 /* We already failed; just get out of the bfd_map_over_sections
1502 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1504 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1507 if (this_hdr
->sh_name
== (unsigned long) -1)
1513 this_hdr
->sh_flags
= 0;
1515 if ((asect
->flags
& SEC_ALLOC
) != 0
1516 || asect
->user_set_vma
)
1517 this_hdr
->sh_addr
= asect
->vma
;
1519 this_hdr
->sh_addr
= 0;
1521 this_hdr
->sh_offset
= 0;
1522 this_hdr
->sh_size
= asect
->_raw_size
;
1523 this_hdr
->sh_link
= 0;
1524 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1525 /* The sh_entsize and sh_info fields may have been set already by
1526 copy_private_section_data. */
1528 this_hdr
->bfd_section
= asect
;
1529 this_hdr
->contents
= NULL
;
1531 /* FIXME: This should not be based on section names. */
1532 if (strcmp (asect
->name
, ".dynstr") == 0)
1533 this_hdr
->sh_type
= SHT_STRTAB
;
1534 else if (strcmp (asect
->name
, ".hash") == 0)
1536 this_hdr
->sh_type
= SHT_HASH
;
1537 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
1539 else if (strcmp (asect
->name
, ".dynsym") == 0)
1541 this_hdr
->sh_type
= SHT_DYNSYM
;
1542 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1544 else if (strcmp (asect
->name
, ".dynamic") == 0)
1546 this_hdr
->sh_type
= SHT_DYNAMIC
;
1547 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1549 else if (strncmp (asect
->name
, ".rela", 5) == 0
1550 && get_elf_backend_data (abfd
)->may_use_rela_p
)
1552 this_hdr
->sh_type
= SHT_RELA
;
1553 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1555 else if (strncmp (asect
->name
, ".rel", 4) == 0
1556 && get_elf_backend_data (abfd
)->may_use_rel_p
)
1558 this_hdr
->sh_type
= SHT_REL
;
1559 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1561 else if (strncmp (asect
->name
, ".note", 5) == 0)
1562 this_hdr
->sh_type
= SHT_NOTE
;
1563 else if (strncmp (asect
->name
, ".stab", 5) == 0
1564 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1565 this_hdr
->sh_type
= SHT_STRTAB
;
1566 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1568 this_hdr
->sh_type
= SHT_GNU_versym
;
1569 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1571 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1573 this_hdr
->sh_type
= SHT_GNU_verdef
;
1574 this_hdr
->sh_entsize
= 0;
1575 /* objcopy or strip will copy over sh_info, but may not set
1576 cverdefs. The linker will set cverdefs, but sh_info will be
1578 if (this_hdr
->sh_info
== 0)
1579 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1581 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1582 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1584 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1586 this_hdr
->sh_type
= SHT_GNU_verneed
;
1587 this_hdr
->sh_entsize
= 0;
1588 /* objcopy or strip will copy over sh_info, but may not set
1589 cverrefs. The linker will set cverrefs, but sh_info will be
1591 if (this_hdr
->sh_info
== 0)
1592 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1594 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1595 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1597 else if ((asect
->flags
& SEC_ALLOC
) != 0
1598 && (asect
->flags
& SEC_LOAD
) != 0)
1599 this_hdr
->sh_type
= SHT_PROGBITS
;
1600 else if ((asect
->flags
& SEC_ALLOC
) != 0
1601 && ((asect
->flags
& SEC_LOAD
) == 0))
1602 this_hdr
->sh_type
= SHT_NOBITS
;
1606 this_hdr
->sh_type
= SHT_PROGBITS
;
1609 if ((asect
->flags
& SEC_ALLOC
) != 0)
1610 this_hdr
->sh_flags
|= SHF_ALLOC
;
1611 if ((asect
->flags
& SEC_READONLY
) == 0)
1612 this_hdr
->sh_flags
|= SHF_WRITE
;
1613 if ((asect
->flags
& SEC_CODE
) != 0)
1614 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1616 /* Check for processor-specific section types. */
1617 if (bed
->elf_backend_fake_sections
)
1618 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1620 /* If the section has relocs, set up a section header for the
1621 SHT_REL[A] section. If two relocation sections are required for
1622 this section, it is up to the processor-specific back-end to
1623 create the other. */
1624 if ((asect
->flags
& SEC_RELOC
) != 0
1625 && !_bfd_elf_init_reloc_shdr (abfd
,
1626 &elf_section_data (asect
)->rel_hdr
,
1628 elf_section_data (asect
)->use_rela_p
))
1632 /* Assign all ELF section numbers. The dummy first section is handled here
1633 too. The link/info pointers for the standard section types are filled
1634 in here too, while we're at it. */
1637 assign_section_numbers (abfd
)
1640 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1642 unsigned int section_number
;
1643 Elf_Internal_Shdr
**i_shdrp
;
1644 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1648 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1650 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1652 d
->this_idx
= section_number
++;
1653 if ((sec
->flags
& SEC_RELOC
) == 0)
1656 d
->rel_idx
= section_number
++;
1659 d
->rel_idx2
= section_number
++;
1664 t
->shstrtab_section
= section_number
++;
1665 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1666 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1668 if (bfd_get_symcount (abfd
) > 0)
1670 t
->symtab_section
= section_number
++;
1671 t
->strtab_section
= section_number
++;
1674 elf_elfheader (abfd
)->e_shnum
= section_number
;
1676 /* Set up the list of section header pointers, in agreement with the
1678 i_shdrp
= ((Elf_Internal_Shdr
**)
1679 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1680 if (i_shdrp
== NULL
)
1683 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1684 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1685 if (i_shdrp
[0] == NULL
)
1687 bfd_release (abfd
, i_shdrp
);
1690 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1692 elf_elfsections (abfd
) = i_shdrp
;
1694 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1695 if (bfd_get_symcount (abfd
) > 0)
1697 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1698 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1699 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1701 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1703 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1707 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1708 if (d
->rel_idx
!= 0)
1709 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1710 if (d
->rel_idx2
!= 0)
1711 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
1713 /* Fill in the sh_link and sh_info fields while we're at it. */
1715 /* sh_link of a reloc section is the section index of the symbol
1716 table. sh_info is the section index of the section to which
1717 the relocation entries apply. */
1718 if (d
->rel_idx
!= 0)
1720 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1721 d
->rel_hdr
.sh_info
= d
->this_idx
;
1723 if (d
->rel_idx2
!= 0)
1725 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
1726 d
->rel_hdr2
->sh_info
= d
->this_idx
;
1729 switch (d
->this_hdr
.sh_type
)
1733 /* A reloc section which we are treating as a normal BFD
1734 section. sh_link is the section index of the symbol
1735 table. sh_info is the section index of the section to
1736 which the relocation entries apply. We assume that an
1737 allocated reloc section uses the dynamic symbol table.
1738 FIXME: How can we be sure? */
1739 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1741 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1743 /* We look up the section the relocs apply to by name. */
1745 if (d
->this_hdr
.sh_type
== SHT_REL
)
1749 s
= bfd_get_section_by_name (abfd
, name
);
1751 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1755 /* We assume that a section named .stab*str is a stabs
1756 string section. We look for a section with the same name
1757 but without the trailing ``str'', and set its sh_link
1758 field to point to this section. */
1759 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1760 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1765 len
= strlen (sec
->name
);
1766 alc
= (char *) bfd_malloc (len
- 2);
1769 strncpy (alc
, sec
->name
, len
- 3);
1770 alc
[len
- 3] = '\0';
1771 s
= bfd_get_section_by_name (abfd
, alc
);
1775 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1777 /* This is a .stab section. */
1778 elf_section_data (s
)->this_hdr
.sh_entsize
=
1779 4 + 2 * (bed
->s
->arch_size
/ 8);
1786 case SHT_GNU_verneed
:
1787 case SHT_GNU_verdef
:
1788 /* sh_link is the section header index of the string table
1789 used for the dynamic entries, or the symbol table, or the
1791 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1793 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1797 case SHT_GNU_versym
:
1798 /* sh_link is the section header index of the symbol table
1799 this hash table or version table is for. */
1800 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1802 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1810 /* Map symbol from it's internal number to the external number, moving
1811 all local symbols to be at the head of the list. */
1814 sym_is_global (abfd
, sym
)
1818 /* If the backend has a special mapping, use it. */
1819 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1820 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1823 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1824 || bfd_is_und_section (bfd_get_section (sym
))
1825 || bfd_is_com_section (bfd_get_section (sym
)));
1829 elf_map_symbols (abfd
)
1832 int symcount
= bfd_get_symcount (abfd
);
1833 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1834 asymbol
**sect_syms
;
1836 int num_globals
= 0;
1837 int num_locals2
= 0;
1838 int num_globals2
= 0;
1840 int num_sections
= 0;
1847 fprintf (stderr
, "elf_map_symbols\n");
1851 /* Add a section symbol for each BFD section. FIXME: Is this really
1853 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1855 if (max_index
< asect
->index
)
1856 max_index
= asect
->index
;
1860 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1861 if (sect_syms
== NULL
)
1863 elf_section_syms (abfd
) = sect_syms
;
1865 for (idx
= 0; idx
< symcount
; idx
++)
1869 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
1876 if (sec
->owner
!= NULL
)
1878 if (sec
->owner
!= abfd
)
1880 if (sec
->output_offset
!= 0)
1883 sec
= sec
->output_section
;
1885 /* Empty sections in the input files may have had a section
1886 symbol created for them. (See the comment near the end of
1887 _bfd_generic_link_output_symbols in linker.c). If the linker
1888 script discards such sections then we will reach this point.
1889 Since we know that we cannot avoid this case, we detect it
1890 and skip the abort and the assignment to the sect_syms array.
1891 To reproduce this particular case try running the linker
1892 testsuite test ld-scripts/weak.exp for an ELF port that uses
1893 the generic linker. */
1894 if (sec
->owner
== NULL
)
1897 BFD_ASSERT (sec
->owner
== abfd
);
1899 sect_syms
[sec
->index
] = syms
[idx
];
1904 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1906 if (sect_syms
[asect
->index
] != NULL
)
1909 sym
= bfd_make_empty_symbol (abfd
);
1912 sym
->the_bfd
= abfd
;
1913 sym
->name
= asect
->name
;
1915 /* Set the flags to 0 to indicate that this one was newly added. */
1917 sym
->section
= asect
;
1918 sect_syms
[asect
->index
] = sym
;
1922 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
1923 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1927 /* Classify all of the symbols. */
1928 for (idx
= 0; idx
< symcount
; idx
++)
1930 if (!sym_is_global (abfd
, syms
[idx
]))
1935 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1937 if (sect_syms
[asect
->index
] != NULL
1938 && sect_syms
[asect
->index
]->flags
== 0)
1940 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
1941 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
1945 sect_syms
[asect
->index
]->flags
= 0;
1949 /* Now sort the symbols so the local symbols are first. */
1950 new_syms
= ((asymbol
**)
1952 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
1953 if (new_syms
== NULL
)
1956 for (idx
= 0; idx
< symcount
; idx
++)
1958 asymbol
*sym
= syms
[idx
];
1961 if (!sym_is_global (abfd
, sym
))
1964 i
= num_locals
+ num_globals2
++;
1966 sym
->udata
.i
= i
+ 1;
1968 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1970 if (sect_syms
[asect
->index
] != NULL
1971 && sect_syms
[asect
->index
]->flags
== 0)
1973 asymbol
*sym
= sect_syms
[asect
->index
];
1976 sym
->flags
= BSF_SECTION_SYM
;
1977 if (!sym_is_global (abfd
, sym
))
1980 i
= num_locals
+ num_globals2
++;
1982 sym
->udata
.i
= i
+ 1;
1986 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
1988 elf_num_locals (abfd
) = num_locals
;
1989 elf_num_globals (abfd
) = num_globals
;
1993 /* Align to the maximum file alignment that could be required for any
1994 ELF data structure. */
1996 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
1997 static INLINE file_ptr
1998 align_file_position (off
, align
)
2002 return (off
+ align
- 1) & ~(align
- 1);
2005 /* Assign a file position to a section, optionally aligning to the
2006 required section alignment. */
2009 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2010 Elf_Internal_Shdr
*i_shdrp
;
2018 al
= i_shdrp
->sh_addralign
;
2020 offset
= BFD_ALIGN (offset
, al
);
2022 i_shdrp
->sh_offset
= offset
;
2023 if (i_shdrp
->bfd_section
!= NULL
)
2024 i_shdrp
->bfd_section
->filepos
= offset
;
2025 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2026 offset
+= i_shdrp
->sh_size
;
2030 /* Compute the file positions we are going to put the sections at, and
2031 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2032 is not NULL, this is being called by the ELF backend linker. */
2035 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2037 struct bfd_link_info
*link_info
;
2039 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2041 struct bfd_strtab_hash
*strtab
;
2042 Elf_Internal_Shdr
*shstrtab_hdr
;
2044 if (abfd
->output_has_begun
)
2047 /* Do any elf backend specific processing first. */
2048 if (bed
->elf_backend_begin_write_processing
)
2049 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2051 if (! prep_headers (abfd
))
2054 /* Post process the headers if necessary. */
2055 if (bed
->elf_backend_post_process_headers
)
2056 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2059 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2063 if (!assign_section_numbers (abfd
))
2066 /* The backend linker builds symbol table information itself. */
2067 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2069 /* Non-zero if doing a relocatable link. */
2070 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2072 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2076 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2077 /* sh_name was set in prep_headers. */
2078 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2079 shstrtab_hdr
->sh_flags
= 0;
2080 shstrtab_hdr
->sh_addr
= 0;
2081 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2082 shstrtab_hdr
->sh_entsize
= 0;
2083 shstrtab_hdr
->sh_link
= 0;
2084 shstrtab_hdr
->sh_info
= 0;
2085 /* sh_offset is set in assign_file_positions_except_relocs. */
2086 shstrtab_hdr
->sh_addralign
= 1;
2088 if (!assign_file_positions_except_relocs (abfd
))
2091 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2094 Elf_Internal_Shdr
*hdr
;
2096 off
= elf_tdata (abfd
)->next_file_pos
;
2098 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2099 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2101 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2102 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2104 elf_tdata (abfd
)->next_file_pos
= off
;
2106 /* Now that we know where the .strtab section goes, write it
2108 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2109 || ! _bfd_stringtab_emit (abfd
, strtab
))
2111 _bfd_stringtab_free (strtab
);
2114 abfd
->output_has_begun
= true;
2119 /* Create a mapping from a set of sections to a program segment. */
2121 static INLINE
struct elf_segment_map
*
2122 make_mapping (abfd
, sections
, from
, to
, phdr
)
2124 asection
**sections
;
2129 struct elf_segment_map
*m
;
2133 m
= ((struct elf_segment_map
*)
2135 (sizeof (struct elf_segment_map
)
2136 + (to
- from
- 1) * sizeof (asection
*))));
2140 m
->p_type
= PT_LOAD
;
2141 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2142 m
->sections
[i
- from
] = *hdrpp
;
2143 m
->count
= to
- from
;
2145 if (from
== 0 && phdr
)
2147 /* Include the headers in the first PT_LOAD segment. */
2148 m
->includes_filehdr
= 1;
2149 m
->includes_phdrs
= 1;
2155 /* Set up a mapping from BFD sections to program segments. */
2158 map_sections_to_segments (abfd
)
2161 asection
**sections
= NULL
;
2165 struct elf_segment_map
*mfirst
;
2166 struct elf_segment_map
**pm
;
2167 struct elf_segment_map
*m
;
2169 unsigned int phdr_index
;
2170 bfd_vma maxpagesize
;
2172 boolean phdr_in_segment
= true;
2176 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2179 if (bfd_count_sections (abfd
) == 0)
2182 /* Select the allocated sections, and sort them. */
2184 sections
= (asection
**) bfd_malloc (bfd_count_sections (abfd
)
2185 * sizeof (asection
*));
2186 if (sections
== NULL
)
2190 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2192 if ((s
->flags
& SEC_ALLOC
) != 0)
2198 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2201 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2203 /* Build the mapping. */
2208 /* If we have a .interp section, then create a PT_PHDR segment for
2209 the program headers and a PT_INTERP segment for the .interp
2211 s
= bfd_get_section_by_name (abfd
, ".interp");
2212 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2214 m
= ((struct elf_segment_map
*)
2215 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2219 m
->p_type
= PT_PHDR
;
2220 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2221 m
->p_flags
= PF_R
| PF_X
;
2222 m
->p_flags_valid
= 1;
2223 m
->includes_phdrs
= 1;
2228 m
= ((struct elf_segment_map
*)
2229 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2233 m
->p_type
= PT_INTERP
;
2241 /* Look through the sections. We put sections in the same program
2242 segment when the start of the second section can be placed within
2243 a few bytes of the end of the first section. */
2246 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2248 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2250 && (dynsec
->flags
& SEC_LOAD
) == 0)
2253 /* Deal with -Ttext or something similar such that the first section
2254 is not adjacent to the program headers. This is an
2255 approximation, since at this point we don't know exactly how many
2256 program headers we will need. */
2259 bfd_size_type phdr_size
;
2261 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2263 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2264 if ((abfd
->flags
& D_PAGED
) == 0
2265 || sections
[0]->lma
< phdr_size
2266 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2267 phdr_in_segment
= false;
2270 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2273 boolean new_segment
;
2277 /* See if this section and the last one will fit in the same
2280 if (last_hdr
== NULL
)
2282 /* If we don't have a segment yet, then we don't need a new
2283 one (we build the last one after this loop). */
2284 new_segment
= false;
2286 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2288 /* If this section has a different relation between the
2289 virtual address and the load address, then we need a new
2293 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2294 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2296 /* If putting this section in this segment would force us to
2297 skip a page in the segment, then we need a new segment. */
2300 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2301 && (hdr
->flags
& SEC_LOAD
) != 0)
2303 /* We don't want to put a loadable section after a
2304 nonloadable section in the same segment. */
2307 else if ((abfd
->flags
& D_PAGED
) == 0)
2309 /* If the file is not demand paged, which means that we
2310 don't require the sections to be correctly aligned in the
2311 file, then there is no other reason for a new segment. */
2312 new_segment
= false;
2315 && (hdr
->flags
& SEC_READONLY
) == 0
2316 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2319 /* We don't want to put a writable section in a read only
2320 segment, unless they are on the same page in memory
2321 anyhow. We already know that the last section does not
2322 bring us past the current section on the page, so the
2323 only case in which the new section is not on the same
2324 page as the previous section is when the previous section
2325 ends precisely on a page boundary. */
2330 /* Otherwise, we can use the same segment. */
2331 new_segment
= false;
2336 if ((hdr
->flags
& SEC_READONLY
) == 0)
2342 /* We need a new program segment. We must create a new program
2343 header holding all the sections from phdr_index until hdr. */
2345 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2352 if ((hdr
->flags
& SEC_READONLY
) == 0)
2359 phdr_in_segment
= false;
2362 /* Create a final PT_LOAD program segment. */
2363 if (last_hdr
!= NULL
)
2365 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2373 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2376 m
= ((struct elf_segment_map
*)
2377 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2381 m
->p_type
= PT_DYNAMIC
;
2383 m
->sections
[0] = dynsec
;
2389 /* For each loadable .note section, add a PT_NOTE segment. We don't
2390 use bfd_get_section_by_name, because if we link together
2391 nonloadable .note sections and loadable .note sections, we will
2392 generate two .note sections in the output file. FIXME: Using
2393 names for section types is bogus anyhow. */
2394 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2396 if ((s
->flags
& SEC_LOAD
) != 0
2397 && strncmp (s
->name
, ".note", 5) == 0)
2399 m
= ((struct elf_segment_map
*)
2400 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2404 m
->p_type
= PT_NOTE
;
2416 elf_tdata (abfd
)->segment_map
= mfirst
;
2420 if (sections
!= NULL
)
2425 /* Sort sections by address. */
2428 elf_sort_sections (arg1
, arg2
)
2432 const asection
*sec1
= *(const asection
**) arg1
;
2433 const asection
*sec2
= *(const asection
**) arg2
;
2435 /* Sort by LMA first, since this is the address used to
2436 place the section into a segment. */
2437 if (sec1
->lma
< sec2
->lma
)
2439 else if (sec1
->lma
> sec2
->lma
)
2442 /* Then sort by VMA. Normally the LMA and the VMA will be
2443 the same, and this will do nothing. */
2444 if (sec1
->vma
< sec2
->vma
)
2446 else if (sec1
->vma
> sec2
->vma
)
2449 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2451 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2456 return sec1
->target_index
- sec2
->target_index
;
2466 /* Sort by size, to put zero sized sections before others at the
2469 if (sec1
->_raw_size
< sec2
->_raw_size
)
2471 if (sec1
->_raw_size
> sec2
->_raw_size
)
2474 return sec1
->target_index
- sec2
->target_index
;
2477 /* Assign file positions to the sections based on the mapping from
2478 sections to segments. This function also sets up some fields in
2479 the file header, and writes out the program headers. */
2482 assign_file_positions_for_segments (abfd
)
2485 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2487 struct elf_segment_map
*m
;
2489 Elf_Internal_Phdr
*phdrs
;
2491 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2492 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2493 Elf_Internal_Phdr
*p
;
2495 if (elf_tdata (abfd
)->segment_map
== NULL
)
2497 if (! map_sections_to_segments (abfd
))
2501 if (bed
->elf_backend_modify_segment_map
)
2503 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2508 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2511 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2512 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2513 elf_elfheader (abfd
)->e_phnum
= count
;
2518 /* If we already counted the number of program segments, make sure
2519 that we allocated enough space. This happens when SIZEOF_HEADERS
2520 is used in a linker script. */
2521 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2522 if (alloc
!= 0 && count
> alloc
)
2524 ((*_bfd_error_handler
)
2525 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2526 bfd_get_filename (abfd
), alloc
, count
));
2527 bfd_set_error (bfd_error_bad_value
);
2534 phdrs
= ((Elf_Internal_Phdr
*)
2535 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
2539 off
= bed
->s
->sizeof_ehdr
;
2540 off
+= alloc
* bed
->s
->sizeof_phdr
;
2547 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2554 /* If elf_segment_map is not from map_sections_to_segments, the
2555 sections may not be correctly ordered. */
2557 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2560 p
->p_type
= m
->p_type
;
2562 if (m
->p_flags_valid
)
2563 p
->p_flags
= m
->p_flags
;
2567 if (p
->p_type
== PT_LOAD
2569 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2571 if ((abfd
->flags
& D_PAGED
) != 0)
2572 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2575 bfd_size_type align
;
2578 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2580 bfd_size_type secalign
;
2582 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
2583 if (secalign
> align
)
2587 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
2594 p
->p_vaddr
= m
->sections
[0]->vma
;
2596 if (m
->p_paddr_valid
)
2597 p
->p_paddr
= m
->p_paddr
;
2598 else if (m
->count
== 0)
2601 p
->p_paddr
= m
->sections
[0]->lma
;
2603 if (p
->p_type
== PT_LOAD
2604 && (abfd
->flags
& D_PAGED
) != 0)
2605 p
->p_align
= bed
->maxpagesize
;
2606 else if (m
->count
== 0)
2607 p
->p_align
= bed
->s
->file_align
;
2615 if (m
->includes_filehdr
)
2617 if (! m
->p_flags_valid
)
2620 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2621 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2624 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2626 if (p
->p_vaddr
< (bfd_vma
) off
)
2628 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2629 bfd_get_filename (abfd
));
2630 bfd_set_error (bfd_error_bad_value
);
2635 if (! m
->p_paddr_valid
)
2638 if (p
->p_type
== PT_LOAD
)
2640 filehdr_vaddr
= p
->p_vaddr
;
2641 filehdr_paddr
= p
->p_paddr
;
2645 if (m
->includes_phdrs
)
2647 if (! m
->p_flags_valid
)
2650 if (m
->includes_filehdr
)
2652 if (p
->p_type
== PT_LOAD
)
2654 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2655 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2660 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2664 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2665 p
->p_vaddr
-= off
- p
->p_offset
;
2666 if (! m
->p_paddr_valid
)
2667 p
->p_paddr
-= off
- p
->p_offset
;
2670 if (p
->p_type
== PT_LOAD
)
2672 phdrs_vaddr
= p
->p_vaddr
;
2673 phdrs_paddr
= p
->p_paddr
;
2676 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
2679 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2680 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2683 if (p
->p_type
== PT_LOAD
2684 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
2686 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2692 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2693 p
->p_filesz
+= adjust
;
2694 p
->p_memsz
+= adjust
;
2700 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2704 bfd_size_type align
;
2708 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2710 /* The section may have artificial alignment forced by a
2711 link script. Notice this case by the gap between the
2712 cumulative phdr vma and the section's vma. */
2713 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2715 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2717 p
->p_memsz
+= adjust
;
2720 if ((flags
& SEC_LOAD
) != 0)
2721 p
->p_filesz
+= adjust
;
2724 if (p
->p_type
== PT_LOAD
)
2726 bfd_signed_vma adjust
;
2728 if ((flags
& SEC_LOAD
) != 0)
2730 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2734 else if ((flags
& SEC_ALLOC
) != 0)
2736 /* The section VMA must equal the file position
2737 modulo the page size. FIXME: I'm not sure if
2738 this adjustment is really necessary. We used to
2739 not have the SEC_LOAD case just above, and then
2740 this was necessary, but now I'm not sure. */
2741 if ((abfd
->flags
& D_PAGED
) != 0)
2742 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2744 adjust
= (sec
->vma
- voff
) % align
;
2753 (* _bfd_error_handler
)
2754 (_("Error: First section in segment (%s) starts at 0x%x"),
2755 bfd_section_name (abfd
, sec
), sec
->lma
);
2756 (* _bfd_error_handler
)
2757 (_(" whereas segment starts at 0x%x"),
2762 p
->p_memsz
+= adjust
;
2765 if ((flags
& SEC_LOAD
) != 0)
2766 p
->p_filesz
+= adjust
;
2771 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2772 used in a linker script we may have a section with
2773 SEC_LOAD clear but which is supposed to have
2775 if ((flags
& SEC_LOAD
) != 0
2776 || (flags
& SEC_HAS_CONTENTS
) != 0)
2777 off
+= sec
->_raw_size
;
2779 if ((flags
& SEC_ALLOC
) != 0)
2780 voff
+= sec
->_raw_size
;
2783 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
2785 if (i
== 0) /* the actual "note" segment */
2786 { /* this one actually contains everything. */
2788 p
->p_filesz
= sec
->_raw_size
;
2789 off
+= sec
->_raw_size
;
2792 else /* fake sections -- don't need to be written */
2796 flags
= sec
->flags
= 0; /* no contents */
2803 p
->p_memsz
+= sec
->_raw_size
;
2805 if ((flags
& SEC_LOAD
) != 0)
2806 p
->p_filesz
+= sec
->_raw_size
;
2808 if (align
> p
->p_align
2809 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
2813 if (! m
->p_flags_valid
)
2816 if ((flags
& SEC_CODE
) != 0)
2818 if ((flags
& SEC_READONLY
) == 0)
2824 /* Now that we have set the section file positions, we can set up
2825 the file positions for the non PT_LOAD segments. */
2826 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2830 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
2832 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
2833 p
->p_offset
= m
->sections
[0]->filepos
;
2837 if (m
->includes_filehdr
)
2839 p
->p_vaddr
= filehdr_vaddr
;
2840 if (! m
->p_paddr_valid
)
2841 p
->p_paddr
= filehdr_paddr
;
2843 else if (m
->includes_phdrs
)
2845 p
->p_vaddr
= phdrs_vaddr
;
2846 if (! m
->p_paddr_valid
)
2847 p
->p_paddr
= phdrs_paddr
;
2852 /* Clear out any program headers we allocated but did not use. */
2853 for (; count
< alloc
; count
++, p
++)
2855 memset (p
, 0, sizeof *p
);
2856 p
->p_type
= PT_NULL
;
2859 elf_tdata (abfd
)->phdr
= phdrs
;
2861 elf_tdata (abfd
)->next_file_pos
= off
;
2863 /* Write out the program headers. */
2864 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
2865 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
2871 /* Get the size of the program header.
2873 If this is called by the linker before any of the section VMA's are set, it
2874 can't calculate the correct value for a strange memory layout. This only
2875 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2876 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2877 data segment (exclusive of .interp and .dynamic).
2879 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2880 will be two segments. */
2882 static bfd_size_type
2883 get_program_header_size (abfd
)
2888 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2890 /* We can't return a different result each time we're called. */
2891 if (elf_tdata (abfd
)->program_header_size
!= 0)
2892 return elf_tdata (abfd
)->program_header_size
;
2894 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2896 struct elf_segment_map
*m
;
2899 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2901 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
2902 return elf_tdata (abfd
)->program_header_size
;
2905 /* Assume we will need exactly two PT_LOAD segments: one for text
2906 and one for data. */
2909 s
= bfd_get_section_by_name (abfd
, ".interp");
2910 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2912 /* If we have a loadable interpreter section, we need a
2913 PT_INTERP segment. In this case, assume we also need a
2914 PT_PHDR segment, although that may not be true for all
2919 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
2921 /* We need a PT_DYNAMIC segment. */
2925 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2927 if ((s
->flags
& SEC_LOAD
) != 0
2928 && strncmp (s
->name
, ".note", 5) == 0)
2930 /* We need a PT_NOTE segment. */
2935 /* Let the backend count up any program headers it might need. */
2936 if (bed
->elf_backend_additional_program_headers
)
2940 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
2946 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
2947 return elf_tdata (abfd
)->program_header_size
;
2950 /* Work out the file positions of all the sections. This is called by
2951 _bfd_elf_compute_section_file_positions. All the section sizes and
2952 VMAs must be known before this is called.
2954 We do not consider reloc sections at this point, unless they form
2955 part of the loadable image. Reloc sections are assigned file
2956 positions in assign_file_positions_for_relocs, which is called by
2957 write_object_contents and final_link.
2959 We also don't set the positions of the .symtab and .strtab here. */
2962 assign_file_positions_except_relocs (abfd
)
2965 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
2966 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
2967 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
2969 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2971 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
2972 && bfd_get_format (abfd
) != bfd_core
)
2974 Elf_Internal_Shdr
**hdrpp
;
2977 /* Start after the ELF header. */
2978 off
= i_ehdrp
->e_ehsize
;
2980 /* We are not creating an executable, which means that we are
2981 not creating a program header, and that the actual order of
2982 the sections in the file is unimportant. */
2983 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2985 Elf_Internal_Shdr
*hdr
;
2988 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
2990 hdr
->sh_offset
= -1;
2993 if (i
== tdata
->symtab_section
2994 || i
== tdata
->strtab_section
)
2996 hdr
->sh_offset
= -1;
3000 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3006 Elf_Internal_Shdr
**hdrpp
;
3008 /* Assign file positions for the loaded sections based on the
3009 assignment of sections to segments. */
3010 if (! assign_file_positions_for_segments (abfd
))
3013 /* Assign file positions for the other sections. */
3015 off
= elf_tdata (abfd
)->next_file_pos
;
3016 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3018 Elf_Internal_Shdr
*hdr
;
3021 if (hdr
->bfd_section
!= NULL
3022 && hdr
->bfd_section
->filepos
!= 0)
3023 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3024 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3026 ((*_bfd_error_handler
)
3027 (_("%s: warning: allocated section `%s' not in segment"),
3028 bfd_get_filename (abfd
),
3029 (hdr
->bfd_section
== NULL
3031 : hdr
->bfd_section
->name
)));
3032 if ((abfd
->flags
& D_PAGED
) != 0)
3033 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3035 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3036 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3039 else if (hdr
->sh_type
== SHT_REL
3040 || hdr
->sh_type
== SHT_RELA
3041 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3042 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3043 hdr
->sh_offset
= -1;
3045 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3049 /* Place the section headers. */
3050 off
= align_file_position (off
, bed
->s
->file_align
);
3051 i_ehdrp
->e_shoff
= off
;
3052 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3054 elf_tdata (abfd
)->next_file_pos
= off
;
3063 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3064 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3065 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3067 struct bfd_strtab_hash
*shstrtab
;
3068 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3070 i_ehdrp
= elf_elfheader (abfd
);
3071 i_shdrp
= elf_elfsections (abfd
);
3073 shstrtab
= _bfd_elf_stringtab_init ();
3074 if (shstrtab
== NULL
)
3077 elf_shstrtab (abfd
) = shstrtab
;
3079 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3080 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3081 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3082 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3084 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3085 i_ehdrp
->e_ident
[EI_DATA
] =
3086 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3087 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3089 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_SYSV
;
3090 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3092 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3093 i_ehdrp
->e_ident
[count
] = 0;
3095 if ((abfd
->flags
& DYNAMIC
) != 0)
3096 i_ehdrp
->e_type
= ET_DYN
;
3097 else if ((abfd
->flags
& EXEC_P
) != 0)
3098 i_ehdrp
->e_type
= ET_EXEC
;
3099 else if (bfd_get_format (abfd
) == bfd_core
)
3100 i_ehdrp
->e_type
= ET_CORE
;
3102 i_ehdrp
->e_type
= ET_REL
;
3104 switch (bfd_get_arch (abfd
))
3106 case bfd_arch_unknown
:
3107 i_ehdrp
->e_machine
= EM_NONE
;
3109 case bfd_arch_sparc
:
3110 if (bed
->s
->arch_size
== 64)
3111 i_ehdrp
->e_machine
= EM_SPARCV9
;
3113 i_ehdrp
->e_machine
= EM_SPARC
;
3116 i_ehdrp
->e_machine
= EM_386
;
3119 i_ehdrp
->e_machine
= EM_68K
;
3122 i_ehdrp
->e_machine
= EM_88K
;
3125 i_ehdrp
->e_machine
= EM_860
;
3128 i_ehdrp
->e_machine
= EM_960
;
3130 case bfd_arch_mips
: /* MIPS Rxxxx */
3131 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
3134 i_ehdrp
->e_machine
= EM_PARISC
;
3136 case bfd_arch_powerpc
:
3137 i_ehdrp
->e_machine
= EM_PPC
;
3139 case bfd_arch_alpha
:
3140 i_ehdrp
->e_machine
= EM_ALPHA
;
3143 i_ehdrp
->e_machine
= EM_SH
;
3146 i_ehdrp
->e_machine
= EM_CYGNUS_D10V
;
3149 i_ehdrp
->e_machine
= EM_CYGNUS_D30V
;
3152 i_ehdrp
->e_machine
= EM_CYGNUS_FR30
;
3154 case bfd_arch_mcore
:
3155 i_ehdrp
->e_machine
= EM_MCORE
;
3158 switch (bfd_get_mach (abfd
))
3161 case 0: i_ehdrp
->e_machine
= EM_CYGNUS_V850
; break;
3165 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
3168 i_ehdrp
->e_machine
= EM_ARM
;
3171 i_ehdrp
->e_machine
= EM_CYGNUS_M32R
;
3173 case bfd_arch_mn10200
:
3174 i_ehdrp
->e_machine
= EM_CYGNUS_MN10200
;
3176 case bfd_arch_mn10300
:
3177 i_ehdrp
->e_machine
= EM_CYGNUS_MN10300
;
3179 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3181 i_ehdrp
->e_machine
= EM_NONE
;
3183 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3184 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3186 /* no program header, for now. */
3187 i_ehdrp
->e_phoff
= 0;
3188 i_ehdrp
->e_phentsize
= 0;
3189 i_ehdrp
->e_phnum
= 0;
3191 /* each bfd section is section header entry */
3192 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3193 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3195 /* if we're building an executable, we'll need a program header table */
3196 if (abfd
->flags
& EXEC_P
)
3198 /* it all happens later */
3200 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3202 /* elf_build_phdrs() returns a (NULL-terminated) array of
3203 Elf_Internal_Phdrs */
3204 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3205 i_ehdrp
->e_phoff
= outbase
;
3206 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3211 i_ehdrp
->e_phentsize
= 0;
3213 i_ehdrp
->e_phoff
= 0;
3216 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3217 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3218 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3219 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3220 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3221 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3222 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3223 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3224 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3230 /* Assign file positions for all the reloc sections which are not part
3231 of the loadable file image. */
3234 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3239 Elf_Internal_Shdr
**shdrpp
;
3241 off
= elf_tdata (abfd
)->next_file_pos
;
3243 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3244 i
< elf_elfheader (abfd
)->e_shnum
;
3247 Elf_Internal_Shdr
*shdrp
;
3250 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3251 && shdrp
->sh_offset
== -1)
3252 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3255 elf_tdata (abfd
)->next_file_pos
= off
;
3259 _bfd_elf_write_object_contents (abfd
)
3262 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3263 Elf_Internal_Ehdr
*i_ehdrp
;
3264 Elf_Internal_Shdr
**i_shdrp
;
3268 if (! abfd
->output_has_begun
3269 && ! _bfd_elf_compute_section_file_positions
3270 (abfd
, (struct bfd_link_info
*) NULL
))
3273 i_shdrp
= elf_elfsections (abfd
);
3274 i_ehdrp
= elf_elfheader (abfd
);
3277 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3281 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3283 /* After writing the headers, we need to write the sections too... */
3284 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3286 if (bed
->elf_backend_section_processing
)
3287 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3288 if (i_shdrp
[count
]->contents
)
3290 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3291 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
3293 != i_shdrp
[count
]->sh_size
))
3298 /* Write out the section header names. */
3299 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3300 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3303 if (bed
->elf_backend_final_write_processing
)
3304 (*bed
->elf_backend_final_write_processing
) (abfd
,
3305 elf_tdata (abfd
)->linker
);
3307 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3311 _bfd_elf_write_corefile_contents (abfd
)
3314 /* Hopefully this can be done just like an object file. */
3315 return _bfd_elf_write_object_contents (abfd
);
3317 /* given a section, search the header to find them... */
3319 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3323 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3324 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3326 Elf_Internal_Shdr
*hdr
;
3327 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3329 for (index
= 0; index
< maxindex
; index
++)
3331 hdr
= i_shdrp
[index
];
3332 if (hdr
->bfd_section
== asect
)
3336 if (bed
->elf_backend_section_from_bfd_section
)
3338 for (index
= 0; index
< maxindex
; index
++)
3342 hdr
= i_shdrp
[index
];
3344 if ((*bed
->elf_backend_section_from_bfd_section
)
3345 (abfd
, hdr
, asect
, &retval
))
3350 if (bfd_is_abs_section (asect
))
3352 if (bfd_is_com_section (asect
))
3354 if (bfd_is_und_section (asect
))
3357 bfd_set_error (bfd_error_nonrepresentable_section
);
3362 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3366 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3368 asymbol
**asym_ptr_ptr
;
3370 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3372 flagword flags
= asym_ptr
->flags
;
3374 /* When gas creates relocations against local labels, it creates its
3375 own symbol for the section, but does put the symbol into the
3376 symbol chain, so udata is 0. When the linker is generating
3377 relocatable output, this section symbol may be for one of the
3378 input sections rather than the output section. */
3379 if (asym_ptr
->udata
.i
== 0
3380 && (flags
& BSF_SECTION_SYM
)
3381 && asym_ptr
->section
)
3385 if (asym_ptr
->section
->output_section
!= NULL
)
3386 indx
= asym_ptr
->section
->output_section
->index
;
3388 indx
= asym_ptr
->section
->index
;
3389 if (elf_section_syms (abfd
)[indx
])
3390 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3393 idx
= asym_ptr
->udata
.i
;
3397 /* This case can occur when using --strip-symbol on a symbol
3398 which is used in a relocation entry. */
3399 (*_bfd_error_handler
)
3400 (_("%s: symbol `%s' required but not present"),
3401 bfd_get_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3402 bfd_set_error (bfd_error_no_symbols
);
3409 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3410 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3411 elf_symbol_flags (flags
));
3419 /* Copy private BFD data. This copies any program header information. */
3422 copy_private_bfd_data (ibfd
, obfd
)
3426 Elf_Internal_Ehdr
*iehdr
;
3427 struct elf_segment_map
*mfirst
;
3428 struct elf_segment_map
**pm
;
3429 struct elf_segment_map
*m
;
3430 Elf_Internal_Phdr
*p
;
3432 unsigned int num_segments
;
3433 boolean phdr_included
= false;
3435 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3436 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3439 if (elf_tdata (ibfd
)->phdr
== NULL
)
3442 iehdr
= elf_elfheader (ibfd
);
3447 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3449 #define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3450 ((addr) >= (bottom) \
3451 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3452 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3454 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3456 #define IS_COREFILE_NOTE(p, s) \
3457 (p->p_type == PT_NOTE \
3458 && bfd_get_format (ibfd) == bfd_core \
3459 && s->vma == 0 && s->lma == 0 \
3460 && (bfd_vma) s->filepos >= p->p_offset \
3461 && (bfd_vma) s->filepos + s->_raw_size \
3462 <= p->p_offset + p->p_filesz)
3464 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3465 linker, which generates a PT_INTERP section with p_vaddr and
3466 p_memsz set to 0. */
3468 #define IS_SOLARIS_PT_INTERP(p, s) \
3470 && p->p_filesz > 0 \
3471 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3472 && s->_raw_size > 0 \
3473 && (bfd_vma) s->filepos >= p->p_offset \
3474 && ((bfd_vma) s->filepos + s->_raw_size \
3475 <= p->p_offset + p->p_filesz))
3477 /* Scan through the segments specified in the program header
3478 of the input BFD. */
3479 for (i
= 0, p
= elf_tdata (ibfd
)->phdr
; i
< num_segments
; i
++, p
++)
3483 asection
**sections
;
3486 bfd_vma matching_lma
;
3487 bfd_vma suggested_lma
;
3490 /* For each section in the input BFD, decide if it should be
3491 included in the current segment. A section will be included
3492 if it is within the address space of the segment, and it is
3493 an allocated segment, and there is an output section
3494 associated with it. */
3496 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3497 if (s
->output_section
!= NULL
)
3499 if ((IS_CONTAINED_BY (s
->vma
, s
->_raw_size
, p
->p_vaddr
, p
)
3500 || IS_SOLARIS_PT_INTERP (p
, s
))
3501 && (s
->flags
& SEC_ALLOC
) != 0)
3503 else if (IS_COREFILE_NOTE (p
, s
))
3507 /* Allocate a segment map big enough to contain all of the
3508 sections we have selected. */
3509 m
= ((struct elf_segment_map
*)
3511 (sizeof (struct elf_segment_map
)
3512 + ((size_t) csecs
- 1) * sizeof (asection
*))));
3516 /* Initialise the fields of the segment map. Default to
3517 using the physical address of the segment in the input BFD. */
3519 m
->p_type
= p
->p_type
;
3520 m
->p_flags
= p
->p_flags
;
3521 m
->p_flags_valid
= 1;
3522 m
->p_paddr
= p
->p_paddr
;
3523 m
->p_paddr_valid
= 1;
3525 /* Determine if this segment contains the ELF file header
3526 and if it contains the program headers themselves. */
3527 m
->includes_filehdr
= (p
->p_offset
== 0
3528 && p
->p_filesz
>= iehdr
->e_ehsize
);
3530 m
->includes_phdrs
= 0;
3532 if (! phdr_included
|| p
->p_type
!= PT_LOAD
)
3535 (p
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3536 && (p
->p_offset
+ p
->p_filesz
3537 >= ((bfd_vma
) iehdr
->e_phoff
3538 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3539 if (p
->p_type
== PT_LOAD
&& m
->includes_phdrs
)
3540 phdr_included
= true;
3545 /* Special segments, such as the PT_PHDR segment, may contain
3546 no sections, but ordinary, loadable segments should contain
3549 if (p
->p_type
== PT_LOAD
)
3551 (_("%s: warning: Empty loadable segment detected\n"),
3552 bfd_get_filename (ibfd
));
3561 /* Now scan the sections in the input BFD again and attempt
3562 to add their corresponding output sections to the segment map.
3563 The problem here is how to handle an output section which has
3564 been moved (ie had its LMA changed). There are four possibilities:
3566 1. None of the sections have been moved.
3567 In this case we can continue to use the segment LMA from the
3570 2. All of the sections have been moved by the same amount.
3571 In this case we can change the segment's LMA to match the LMA
3572 of the first section.
3574 3. Some of the sections have been moved, others have not.
3575 In this case those sections which have not been moved can be
3576 placed in the current segment which will have to have its size,
3577 and possibly its LMA changed, and a new segment or segments will
3578 have to be created to contain the other sections.
3580 4. The sections have been moved, but not be the same amount.
3581 In this case we can change the segment's LMA to match the LMA
3582 of the first section and we will have to create a new segment
3583 or segments to contain the other sections.
3585 In order to save time, we allocate an array to hold the section
3586 pointers that we are interested in. As these sections get assigned
3587 to a segment, they are removed from this array. */
3589 sections
= (asection
**) bfd_malloc (sizeof (asection
*) * csecs
);
3590 if (sections
== NULL
)
3593 /* Step One: Scan for segment vs section LMA conflicts.
3594 Also add the sections to the section array allocated above.
3595 Also add the sections to the current segment. In the common
3596 case, where the sections have not been moved, this means that
3597 we have completely filled the segment, and there is nothing
3601 matching_lma
= false;
3604 for (j
= 0, s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3606 os
= s
->output_section
;
3608 if ((((IS_CONTAINED_BY (s
->vma
, s
->_raw_size
, p
->p_vaddr
, p
)
3609 || IS_SOLARIS_PT_INTERP (p
, s
))
3610 && (s
->flags
& SEC_ALLOC
) != 0)
3611 || IS_COREFILE_NOTE (p
, s
))
3616 /* The Solaris native linker always sets p_paddr to 0.
3617 We try to catch that case here, and set it to the
3623 && (os
->vma
== (p
->p_vaddr
3624 + (m
->includes_filehdr
3627 + (m
->includes_phdrs
3628 ? iehdr
->e_phnum
* iehdr
->e_phentsize
3630 m
->p_paddr
= p
->p_vaddr
;
3632 /* Match up the physical address of the segment with the
3633 LMA address of the output section. */
3634 if (IS_CONTAINED_BY (os
->lma
, os
->_raw_size
, m
->p_paddr
, p
)
3635 || IS_COREFILE_NOTE (p
, s
))
3637 if (matching_lma
== 0)
3638 matching_lma
= os
->lma
;
3640 /* We assume that if the section fits within the segment
3641 that it does not overlap any other section within that
3643 m
->sections
[isec
++] = os
;
3645 else if (suggested_lma
== 0)
3646 suggested_lma
= os
->lma
;
3650 BFD_ASSERT (j
== csecs
);
3652 /* Step Two: Adjust the physical address of the current segment,
3656 /* All of the sections fitted within the segment as currently
3657 specified. This is the default case. Add the segment to
3658 the list of built segments and carry on to process the next
3659 program header in the input BFD. */
3667 else if (matching_lma
!= 0)
3669 /* At least one section fits inside the current segment.
3670 Keep it, but modify its physical address to match the
3671 LMA of the first section that fitted. */
3673 m
->p_paddr
= matching_lma
;
3677 /* None of the sections fitted inside the current segment.
3678 Change the current segment's physical address to match
3679 the LMA of the first section. */
3681 m
->p_paddr
= suggested_lma
;
3684 /* Step Three: Loop over the sections again, this time assigning
3685 those that fit to the current segment and remvoing them from the
3686 sections array; but making sure not to leave large gaps. Once all
3687 possible sections have been assigned to the current segment it is
3688 added to the list of built segments and if sections still remain
3689 to be assigned, a new segment is constructed before repeating
3697 /* Fill the current segment with sections that fit. */
3698 for (j
= 0; j
< csecs
; j
++)
3705 os
= s
->output_section
;
3707 if (IS_CONTAINED_BY (os
->lma
, os
->_raw_size
, m
->p_paddr
, p
)
3708 || IS_COREFILE_NOTE (p
, s
))
3712 /* If the first section in a segment does not start at
3713 the beginning of the segment, then something is wrong. */
3714 if (os
->lma
!= m
->p_paddr
)
3719 asection
* prev_sec
;
3720 bfd_vma maxpagesize
;
3722 prev_sec
= m
->sections
[m
->count
- 1];
3723 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3725 /* If the gap between the end of the previous section
3726 and the start of this section is more than maxpagesize
3727 then we need to start a new segment. */
3728 if (BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
3729 < BFD_ALIGN (os
->lma
, maxpagesize
))
3731 if (suggested_lma
== 0)
3732 suggested_lma
= os
->lma
;
3738 m
->sections
[m
->count
++] = os
;
3742 else if (suggested_lma
== 0)
3743 suggested_lma
= os
->lma
;
3746 BFD_ASSERT (m
->count
> 0);
3748 /* Add the current segment to the list of built segments. */
3754 /* We still have not allocated all of the sections to
3755 segments. Create a new segment here, initialise it
3756 and carry on looping. */
3758 m
= ((struct elf_segment_map
*)
3760 (sizeof (struct elf_segment_map
)
3761 + ((size_t) csecs
- 1) * sizeof (asection
*))));
3765 /* Initialise the fields of the segment map. Set the physical
3766 physical address to the LMA of the first section that has
3767 not yet been assigned. */
3770 m
->p_type
= p
->p_type
;
3771 m
->p_flags
= p
->p_flags
;
3772 m
->p_flags_valid
= 1;
3773 m
->p_paddr
= suggested_lma
;
3774 m
->p_paddr_valid
= 1;
3775 m
->includes_filehdr
= 0;
3776 m
->includes_phdrs
= 0;
3779 while (isec
< csecs
);
3784 /* The Solaris linker creates program headers in which all the
3785 p_paddr fields are zero. When we try to objcopy or strip such a
3786 file, we get confused. Check for this case, and if we find it
3787 reset the p_paddr_valid fields. */
3788 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3789 if (m
->p_paddr
!= 0)
3793 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3794 m
->p_paddr_valid
= 0;
3797 elf_tdata (obfd
)->segment_map
= mfirst
;
3800 /* Final Step: Sort the segments into ascending order of physical address. */
3803 struct elf_segment_map
* prev
;
3806 for (m
= mfirst
->next
; m
!= NULL
; prev
= m
, m
= m
->next
)
3808 /* Yes I know - its a bubble sort....*/
3809 if (m
->next
!= NULL
&& (m
->next
->p_paddr
< m
->p_paddr
))
3811 /* swap m and m->next */
3812 prev
->next
= m
->next
;
3813 m
->next
= m
->next
->next
;
3814 prev
->next
->next
= m
;
3823 #undef IS_CONTAINED_BY
3824 #undef IS_SOLARIS_PT_INTERP
3825 #undef IS_COREFILE_NOTE
3829 /* Copy private section information. This copies over the entsize
3830 field, and sometimes the info field. */
3833 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
3839 Elf_Internal_Shdr
*ihdr
, *ohdr
;
3841 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
3842 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
3845 /* Copy over private BFD data if it has not already been copied.
3846 This must be done here, rather than in the copy_private_bfd_data
3847 entry point, because the latter is called after the section
3848 contents have been set, which means that the program headers have
3849 already been worked out. */
3850 if (elf_tdata (obfd
)->segment_map
== NULL
3851 && elf_tdata (ibfd
)->phdr
!= NULL
)
3855 /* Only set up the segments if there are no more SEC_ALLOC
3856 sections. FIXME: This won't do the right thing if objcopy is
3857 used to remove the last SEC_ALLOC section, since objcopy
3858 won't call this routine in that case. */
3859 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
3860 if ((s
->flags
& SEC_ALLOC
) != 0)
3864 if (! copy_private_bfd_data (ibfd
, obfd
))
3869 ihdr
= &elf_section_data (isec
)->this_hdr
;
3870 ohdr
= &elf_section_data (osec
)->this_hdr
;
3872 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
3874 if (ihdr
->sh_type
== SHT_SYMTAB
3875 || ihdr
->sh_type
== SHT_DYNSYM
3876 || ihdr
->sh_type
== SHT_GNU_verneed
3877 || ihdr
->sh_type
== SHT_GNU_verdef
)
3878 ohdr
->sh_info
= ihdr
->sh_info
;
3880 elf_section_data (osec
)->use_rela_p
3881 = elf_section_data (isec
)->use_rela_p
;
3886 /* Copy private symbol information. If this symbol is in a section
3887 which we did not map into a BFD section, try to map the section
3888 index correctly. We use special macro definitions for the mapped
3889 section indices; these definitions are interpreted by the
3890 swap_out_syms function. */
3892 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3893 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3894 #define MAP_STRTAB (SHN_LORESERVE - 3)
3895 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3898 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
3904 elf_symbol_type
*isym
, *osym
;
3906 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3907 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3910 isym
= elf_symbol_from (ibfd
, isymarg
);
3911 osym
= elf_symbol_from (obfd
, osymarg
);
3915 && bfd_is_abs_section (isym
->symbol
.section
))
3919 shndx
= isym
->internal_elf_sym
.st_shndx
;
3920 if (shndx
== elf_onesymtab (ibfd
))
3921 shndx
= MAP_ONESYMTAB
;
3922 else if (shndx
== elf_dynsymtab (ibfd
))
3923 shndx
= MAP_DYNSYMTAB
;
3924 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
3926 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
3927 shndx
= MAP_SHSTRTAB
;
3928 osym
->internal_elf_sym
.st_shndx
= shndx
;
3934 /* Swap out the symbols. */
3937 swap_out_syms (abfd
, sttp
, relocatable_p
)
3939 struct bfd_strtab_hash
**sttp
;
3942 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3944 if (!elf_map_symbols (abfd
))
3947 /* Dump out the symtabs. */
3949 int symcount
= bfd_get_symcount (abfd
);
3950 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3951 struct bfd_strtab_hash
*stt
;
3952 Elf_Internal_Shdr
*symtab_hdr
;
3953 Elf_Internal_Shdr
*symstrtab_hdr
;
3954 char *outbound_syms
;
3957 stt
= _bfd_elf_stringtab_init ();
3961 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3962 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3963 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3964 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
3965 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
3966 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
3968 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3969 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3971 outbound_syms
= bfd_alloc (abfd
,
3972 (1 + symcount
) * bed
->s
->sizeof_sym
);
3973 if (outbound_syms
== NULL
)
3975 symtab_hdr
->contents
= (PTR
) outbound_syms
;
3977 /* now generate the data (for "contents") */
3979 /* Fill in zeroth symbol and swap it out. */
3980 Elf_Internal_Sym sym
;
3986 sym
.st_shndx
= SHN_UNDEF
;
3987 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
3988 outbound_syms
+= bed
->s
->sizeof_sym
;
3990 for (idx
= 0; idx
< symcount
; idx
++)
3992 Elf_Internal_Sym sym
;
3993 bfd_vma value
= syms
[idx
]->value
;
3994 elf_symbol_type
*type_ptr
;
3995 flagword flags
= syms
[idx
]->flags
;
3998 if (flags
& BSF_SECTION_SYM
)
3999 /* Section symbols have no names. */
4003 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4006 if (sym
.st_name
== (unsigned long) -1)
4010 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4012 if ((flags
& BSF_SECTION_SYM
) == 0
4013 && bfd_is_com_section (syms
[idx
]->section
))
4015 /* ELF common symbols put the alignment into the `value' field,
4016 and the size into the `size' field. This is backwards from
4017 how BFD handles it, so reverse it here. */
4018 sym
.st_size
= value
;
4019 if (type_ptr
== NULL
4020 || type_ptr
->internal_elf_sym
.st_value
== 0)
4021 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4023 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4024 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4025 (abfd
, syms
[idx
]->section
);
4029 asection
*sec
= syms
[idx
]->section
;
4032 if (sec
->output_section
)
4034 value
+= sec
->output_offset
;
4035 sec
= sec
->output_section
;
4037 /* Don't add in the section vma for relocatable output. */
4038 if (! relocatable_p
)
4040 sym
.st_value
= value
;
4041 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4043 if (bfd_is_abs_section (sec
)
4045 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4047 /* This symbol is in a real ELF section which we did
4048 not create as a BFD section. Undo the mapping done
4049 by copy_private_symbol_data. */
4050 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4054 shndx
= elf_onesymtab (abfd
);
4057 shndx
= elf_dynsymtab (abfd
);
4060 shndx
= elf_tdata (abfd
)->strtab_section
;
4063 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4071 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4077 /* Writing this would be a hell of a lot easier if
4078 we had some decent documentation on bfd, and
4079 knew what to expect of the library, and what to
4080 demand of applications. For example, it
4081 appears that `objcopy' might not set the
4082 section of a symbol to be a section that is
4083 actually in the output file. */
4084 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4085 BFD_ASSERT (sec2
!= 0);
4086 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
4087 BFD_ASSERT (shndx
!= -1);
4091 sym
.st_shndx
= shndx
;
4094 if ((flags
& BSF_FUNCTION
) != 0)
4096 else if ((flags
& BSF_OBJECT
) != 0)
4101 /* Processor-specific types */
4102 if (bed
->elf_backend_get_symbol_type
)
4103 type
= (*bed
->elf_backend_get_symbol_type
) (&type_ptr
->internal_elf_sym
, type
);
4105 if (flags
& BSF_SECTION_SYM
)
4106 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4107 else if (bfd_is_com_section (syms
[idx
]->section
))
4108 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4109 else if (bfd_is_und_section (syms
[idx
]->section
))
4110 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4114 else if (flags
& BSF_FILE
)
4115 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4118 int bind
= STB_LOCAL
;
4120 if (flags
& BSF_LOCAL
)
4122 else if (flags
& BSF_WEAK
)
4124 else if (flags
& BSF_GLOBAL
)
4127 sym
.st_info
= ELF_ST_INFO (bind
, type
);
4130 if (type_ptr
!= NULL
)
4131 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
4135 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4136 outbound_syms
+= bed
->s
->sizeof_sym
;
4140 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
4141 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4143 symstrtab_hdr
->sh_flags
= 0;
4144 symstrtab_hdr
->sh_addr
= 0;
4145 symstrtab_hdr
->sh_entsize
= 0;
4146 symstrtab_hdr
->sh_link
= 0;
4147 symstrtab_hdr
->sh_info
= 0;
4148 symstrtab_hdr
->sh_addralign
= 1;
4154 /* Return the number of bytes required to hold the symtab vector.
4156 Note that we base it on the count plus 1, since we will null terminate
4157 the vector allocated based on this size. However, the ELF symbol table
4158 always has a dummy entry as symbol #0, so it ends up even. */
4161 _bfd_elf_get_symtab_upper_bound (abfd
)
4166 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4168 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4169 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4175 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4180 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4182 if (elf_dynsymtab (abfd
) == 0)
4184 bfd_set_error (bfd_error_invalid_operation
);
4188 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4189 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4195 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4196 bfd
*abfd ATTRIBUTE_UNUSED
;
4199 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4202 /* Canonicalize the relocs. */
4205 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4214 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
4220 tblptr
= section
->relocation
;
4221 for (i
= 0; i
< section
->reloc_count
; i
++)
4222 *relptr
++ = tblptr
++;
4226 return section
->reloc_count
;
4230 _bfd_elf_get_symtab (abfd
, alocation
)
4232 asymbol
**alocation
;
4234 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4235 (abfd
, alocation
, false);
4238 bfd_get_symcount (abfd
) = symcount
;
4243 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4245 asymbol
**alocation
;
4247 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4248 (abfd
, alocation
, true);
4251 /* Return the size required for the dynamic reloc entries. Any
4252 section that was actually installed in the BFD, and has type
4253 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4254 considered to be a dynamic reloc section. */
4257 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4263 if (elf_dynsymtab (abfd
) == 0)
4265 bfd_set_error (bfd_error_invalid_operation
);
4269 ret
= sizeof (arelent
*);
4270 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4271 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4272 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4273 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4274 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4275 * sizeof (arelent
*));
4280 /* Canonicalize the dynamic relocation entries. Note that we return
4281 the dynamic relocations as a single block, although they are
4282 actually associated with particular sections; the interface, which
4283 was designed for SunOS style shared libraries, expects that there
4284 is only one set of dynamic relocs. Any section that was actually
4285 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4286 the dynamic symbol table, is considered to be a dynamic reloc
4290 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4295 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4299 if (elf_dynsymtab (abfd
) == 0)
4301 bfd_set_error (bfd_error_invalid_operation
);
4305 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4307 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4309 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4310 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4311 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4316 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4318 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4320 for (i
= 0; i
< count
; i
++)
4331 /* Read in the version information. */
4334 _bfd_elf_slurp_version_tables (abfd
)
4337 bfd_byte
*contents
= NULL
;
4339 if (elf_dynverdef (abfd
) != 0)
4341 Elf_Internal_Shdr
*hdr
;
4342 Elf_External_Verdef
*everdef
;
4343 Elf_Internal_Verdef
*iverdef
;
4346 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
4348 elf_tdata (abfd
)->verdef
=
4349 ((Elf_Internal_Verdef
*)
4350 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verdef
)));
4351 if (elf_tdata (abfd
)->verdef
== NULL
)
4354 elf_tdata (abfd
)->cverdefs
= hdr
->sh_info
;
4356 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4357 if (contents
== NULL
)
4359 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4360 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4363 everdef
= (Elf_External_Verdef
*) contents
;
4364 iverdef
= elf_tdata (abfd
)->verdef
;
4365 for (i
= 0; i
< hdr
->sh_info
; i
++, iverdef
++)
4367 Elf_External_Verdaux
*everdaux
;
4368 Elf_Internal_Verdaux
*iverdaux
;
4371 _bfd_elf_swap_verdef_in (abfd
, everdef
, iverdef
);
4373 iverdef
->vd_bfd
= abfd
;
4375 iverdef
->vd_auxptr
= ((Elf_Internal_Verdaux
*)
4378 * sizeof (Elf_Internal_Verdaux
))));
4379 if (iverdef
->vd_auxptr
== NULL
)
4382 everdaux
= ((Elf_External_Verdaux
*)
4383 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
4384 iverdaux
= iverdef
->vd_auxptr
;
4385 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
4387 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
4389 iverdaux
->vda_nodename
=
4390 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4391 iverdaux
->vda_name
);
4392 if (iverdaux
->vda_nodename
== NULL
)
4395 if (j
+ 1 < iverdef
->vd_cnt
)
4396 iverdaux
->vda_nextptr
= iverdaux
+ 1;
4398 iverdaux
->vda_nextptr
= NULL
;
4400 everdaux
= ((Elf_External_Verdaux
*)
4401 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
4404 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
4406 if (i
+ 1 < hdr
->sh_info
)
4407 iverdef
->vd_nextdef
= iverdef
+ 1;
4409 iverdef
->vd_nextdef
= NULL
;
4411 everdef
= ((Elf_External_Verdef
*)
4412 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
4419 if (elf_dynverref (abfd
) != 0)
4421 Elf_Internal_Shdr
*hdr
;
4422 Elf_External_Verneed
*everneed
;
4423 Elf_Internal_Verneed
*iverneed
;
4426 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
4428 elf_tdata (abfd
)->verref
=
4429 ((Elf_Internal_Verneed
*)
4430 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verneed
)));
4431 if (elf_tdata (abfd
)->verref
== NULL
)
4434 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
4436 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4437 if (contents
== NULL
)
4439 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4440 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4443 everneed
= (Elf_External_Verneed
*) contents
;
4444 iverneed
= elf_tdata (abfd
)->verref
;
4445 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4447 Elf_External_Vernaux
*evernaux
;
4448 Elf_Internal_Vernaux
*ivernaux
;
4451 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4453 iverneed
->vn_bfd
= abfd
;
4455 iverneed
->vn_filename
=
4456 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4458 if (iverneed
->vn_filename
== NULL
)
4461 iverneed
->vn_auxptr
=
4462 ((Elf_Internal_Vernaux
*)
4464 iverneed
->vn_cnt
* sizeof (Elf_Internal_Vernaux
)));
4466 evernaux
= ((Elf_External_Vernaux
*)
4467 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4468 ivernaux
= iverneed
->vn_auxptr
;
4469 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4471 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4473 ivernaux
->vna_nodename
=
4474 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4475 ivernaux
->vna_name
);
4476 if (ivernaux
->vna_nodename
== NULL
)
4479 if (j
+ 1 < iverneed
->vn_cnt
)
4480 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4482 ivernaux
->vna_nextptr
= NULL
;
4484 evernaux
= ((Elf_External_Vernaux
*)
4485 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4488 if (i
+ 1 < hdr
->sh_info
)
4489 iverneed
->vn_nextref
= iverneed
+ 1;
4491 iverneed
->vn_nextref
= NULL
;
4493 everneed
= ((Elf_External_Verneed
*)
4494 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4504 if (contents
== NULL
)
4510 _bfd_elf_make_empty_symbol (abfd
)
4513 elf_symbol_type
*newsym
;
4515 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
4520 newsym
->symbol
.the_bfd
= abfd
;
4521 return &newsym
->symbol
;
4526 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4527 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4531 bfd_symbol_info (symbol
, ret
);
4534 /* Return whether a symbol name implies a local symbol. Most targets
4535 use this function for the is_local_label_name entry point, but some
4539 _bfd_elf_is_local_label_name (abfd
, name
)
4540 bfd
*abfd ATTRIBUTE_UNUSED
;
4543 /* Normal local symbols start with ``.L''. */
4544 if (name
[0] == '.' && name
[1] == 'L')
4547 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4548 DWARF debugging symbols starting with ``..''. */
4549 if (name
[0] == '.' && name
[1] == '.')
4552 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4553 emitting DWARF debugging output. I suspect this is actually a
4554 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4555 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4556 underscore to be emitted on some ELF targets). For ease of use,
4557 we treat such symbols as local. */
4558 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
4565 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
4566 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4567 asymbol
*symbol ATTRIBUTE_UNUSED
;
4574 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
4576 enum bfd_architecture arch
;
4577 unsigned long machine
;
4579 /* If this isn't the right architecture for this backend, and this
4580 isn't the generic backend, fail. */
4581 if (arch
!= get_elf_backend_data (abfd
)->arch
4582 && arch
!= bfd_arch_unknown
4583 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
4586 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
4589 /* Find the nearest line to a particular section and offset, for error
4593 _bfd_elf_find_nearest_line (abfd
,
4604 CONST
char **filename_ptr
;
4605 CONST
char **functionname_ptr
;
4606 unsigned int *line_ptr
;
4609 const char *filename
;
4614 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
4615 filename_ptr
, functionname_ptr
,
4619 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
4620 filename_ptr
, functionname_ptr
,
4624 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
4625 &found
, filename_ptr
,
4626 functionname_ptr
, line_ptr
,
4627 &elf_tdata (abfd
)->line_info
))
4632 if (symbols
== NULL
)
4639 for (p
= symbols
; *p
!= NULL
; p
++)
4643 q
= (elf_symbol_type
*) *p
;
4645 if (bfd_get_section (&q
->symbol
) != section
)
4648 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
4653 filename
= bfd_asymbol_name (&q
->symbol
);
4657 if (q
->symbol
.section
== section
4658 && q
->symbol
.value
>= low_func
4659 && q
->symbol
.value
<= offset
)
4661 func
= (asymbol
*) q
;
4662 low_func
= q
->symbol
.value
;
4671 *filename_ptr
= filename
;
4672 *functionname_ptr
= bfd_asymbol_name (func
);
4678 _bfd_elf_sizeof_headers (abfd
, reloc
)
4684 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
4686 ret
+= get_program_header_size (abfd
);
4691 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
4696 bfd_size_type count
;
4698 Elf_Internal_Shdr
*hdr
;
4700 if (! abfd
->output_has_begun
4701 && ! _bfd_elf_compute_section_file_positions
4702 (abfd
, (struct bfd_link_info
*) NULL
))
4705 hdr
= &elf_section_data (section
)->this_hdr
;
4707 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
4709 if (bfd_write (location
, 1, count
, abfd
) != count
)
4716 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
4717 bfd
*abfd ATTRIBUTE_UNUSED
;
4718 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
4719 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
4726 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
4729 Elf_Internal_Rel
*dst
;
4735 /* Try to convert a non-ELF reloc into an ELF one. */
4738 _bfd_elf_validate_reloc (abfd
, areloc
)
4742 /* Check whether we really have an ELF howto. */
4744 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
4746 bfd_reloc_code_real_type code
;
4747 reloc_howto_type
*howto
;
4749 /* Alien reloc: Try to determine its type to replace it with an
4750 equivalent ELF reloc. */
4752 if (areloc
->howto
->pc_relative
)
4754 switch (areloc
->howto
->bitsize
)
4757 code
= BFD_RELOC_8_PCREL
;
4760 code
= BFD_RELOC_12_PCREL
;
4763 code
= BFD_RELOC_16_PCREL
;
4766 code
= BFD_RELOC_24_PCREL
;
4769 code
= BFD_RELOC_32_PCREL
;
4772 code
= BFD_RELOC_64_PCREL
;
4778 howto
= bfd_reloc_type_lookup (abfd
, code
);
4780 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
4782 if (howto
->pcrel_offset
)
4783 areloc
->addend
+= areloc
->address
;
4785 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
4790 switch (areloc
->howto
->bitsize
)
4796 code
= BFD_RELOC_14
;
4799 code
= BFD_RELOC_16
;
4802 code
= BFD_RELOC_26
;
4805 code
= BFD_RELOC_32
;
4808 code
= BFD_RELOC_64
;
4814 howto
= bfd_reloc_type_lookup (abfd
, code
);
4818 areloc
->howto
= howto
;
4826 (*_bfd_error_handler
)
4827 (_("%s: unsupported relocation type %s"),
4828 bfd_get_filename (abfd
), areloc
->howto
->name
);
4829 bfd_set_error (bfd_error_bad_value
);
4834 _bfd_elf_close_and_cleanup (abfd
)
4837 if (bfd_get_format (abfd
) == bfd_object
)
4839 if (elf_shstrtab (abfd
) != NULL
)
4840 _bfd_stringtab_free (elf_shstrtab (abfd
));
4843 return _bfd_generic_close_and_cleanup (abfd
);
4846 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
4847 in the relocation's offset. Thus we cannot allow any sort of sanity
4848 range-checking to interfere. There is nothing else to do in processing
4851 bfd_reloc_status_type
4852 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
4853 bfd
*abfd ATTRIBUTE_UNUSED
;
4854 arelent
*re ATTRIBUTE_UNUSED
;
4855 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
4856 PTR data ATTRIBUTE_UNUSED
;
4857 asection
*is ATTRIBUTE_UNUSED
;
4858 bfd
*obfd ATTRIBUTE_UNUSED
;
4859 char **errmsg ATTRIBUTE_UNUSED
;
4861 return bfd_reloc_ok
;
4865 /* Elf core file support. Much of this only works on native
4866 toolchains, since we rely on knowing the
4867 machine-dependent procfs structure in order to pick
4868 out details about the corefile. */
4870 #ifdef HAVE_SYS_PROCFS_H
4871 # include <sys/procfs.h>
4875 /* Define offsetof for those systems which lack it. */
4878 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
4882 /* FIXME: this is kinda wrong, but it's what gdb wants. */
4885 elfcore_make_pid (abfd
)
4888 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
4889 + (elf_tdata (abfd
)->core_pid
));
4893 /* If there isn't a section called NAME, make one, using
4894 data from SECT. Note, this function will generate a
4895 reference to NAME, so you shouldn't deallocate or
4899 elfcore_maybe_make_sect (abfd
, name
, sect
)
4906 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
4909 sect2
= bfd_make_section (abfd
, name
);
4913 sect2
->_raw_size
= sect
->_raw_size
;
4914 sect2
->filepos
= sect
->filepos
;
4915 sect2
->flags
= sect
->flags
;
4916 sect2
->alignment_power
= sect
->alignment_power
;
4921 /* prstatus_t exists on:
4923 linux 2.[01] + glibc
4927 #if defined (HAVE_PRSTATUS_T)
4929 elfcore_grok_prstatus (abfd
, note
)
4931 Elf_Internal_Note
* note
;
4938 if (note
->descsz
!= sizeof (prstat
))
4941 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
4943 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
4944 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
4946 /* pr_who exists on:
4949 pr_who doesn't exist on:
4952 #if defined (HAVE_PRSTATUS_T_PR_WHO)
4953 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
4956 /* Make a ".reg/999" section. */
4958 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
4959 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
4964 sect
= bfd_make_section (abfd
, name
);
4967 sect
->_raw_size
= sizeof (prstat
.pr_reg
);
4968 sect
->filepos
= note
->descpos
+ offsetof (prstatus_t
, pr_reg
);
4969 sect
->flags
= SEC_HAS_CONTENTS
;
4970 sect
->alignment_power
= 2;
4972 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
4977 #endif /* defined (HAVE_PRSTATUS_T) */
4980 /* There isn't a consistent prfpregset_t across platforms,
4981 but it doesn't matter, because we don't have to pick this
4982 data structure apart. */
4985 elfcore_grok_prfpreg (abfd
, note
)
4987 Elf_Internal_Note
* note
;
4993 /* Make a ".reg2/999" section. */
4995 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
4996 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5001 sect
= bfd_make_section (abfd
, name
);
5004 sect
->_raw_size
= note
->descsz
;
5005 sect
->filepos
= note
->descpos
;
5006 sect
->flags
= SEC_HAS_CONTENTS
;
5007 sect
->alignment_power
= 2;
5009 if (! elfcore_maybe_make_sect (abfd
, ".reg2", sect
))
5015 #if defined (HAVE_PRPSINFO_T)
5016 # define elfcore_psinfo_t prpsinfo_t
5019 #if defined (HAVE_PSINFO_T)
5020 # define elfcore_psinfo_t psinfo_t
5024 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5026 /* return a malloc'ed copy of a string at START which is at
5027 most MAX bytes long, possibly without a terminating '\0'.
5028 the copy will always have a terminating '\0'. */
5031 elfcore_strndup (abfd
, start
, max
)
5037 char* end
= memchr (start
, '\0', max
);
5045 dup
= bfd_alloc (abfd
, len
+ 1);
5049 memcpy (dup
, start
, len
);
5056 elfcore_grok_psinfo (abfd
, note
)
5058 Elf_Internal_Note
* note
;
5060 elfcore_psinfo_t psinfo
;
5062 if (note
->descsz
!= sizeof (elfcore_psinfo_t
))
5065 memcpy (&psinfo
, note
->descdata
, note
->descsz
);
5067 elf_tdata (abfd
)->core_program
5068 = elfcore_strndup (abfd
, psinfo
.pr_fname
, sizeof (psinfo
.pr_fname
));
5070 elf_tdata (abfd
)->core_command
5071 = elfcore_strndup (abfd
, psinfo
.pr_psargs
, sizeof (psinfo
.pr_psargs
));
5073 /* Note that for some reason, a spurious space is tacked
5074 onto the end of the args in some (at least one anyway)
5075 implementations, so strip it off if it exists. */
5078 char* command
= elf_tdata (abfd
)->core_command
;
5079 int n
= strlen (command
);
5081 if (0 < n
&& command
[n
- 1] == ' ')
5082 command
[n
- 1] = '\0';
5087 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5090 #if defined (HAVE_PSTATUS_T)
5092 elfcore_grok_pstatus (abfd
, note
)
5094 Elf_Internal_Note
* note
;
5098 if (note
->descsz
!= sizeof (pstat
))
5101 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5103 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5105 /* Could grab some more details from the "representative"
5106 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5107 NT_LWPSTATUS note, presumably. */
5111 #endif /* defined (HAVE_PSTATUS_T) */
5114 #if defined (HAVE_LWPSTATUS_T)
5116 elfcore_grok_lwpstatus (abfd
, note
)
5118 Elf_Internal_Note
* note
;
5120 lwpstatus_t lwpstat
;
5125 if (note
->descsz
!= sizeof (lwpstat
))
5128 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
5130 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
5131 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
5133 /* Make a ".reg/999" section. */
5135 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5136 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5141 sect
= bfd_make_section (abfd
, name
);
5145 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5146 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
5147 sect
->filepos
= note
->descpos
5148 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
5151 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5152 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
5153 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
5156 sect
->flags
= SEC_HAS_CONTENTS
;
5157 sect
->alignment_power
= 2;
5159 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5162 /* Make a ".reg2/999" section */
5164 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
5165 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5170 sect
= bfd_make_section (abfd
, name
);
5174 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5175 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
5176 sect
->filepos
= note
->descpos
5177 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
5180 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5181 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
5182 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
5185 sect
->flags
= SEC_HAS_CONTENTS
;
5186 sect
->alignment_power
= 2;
5188 if (!elfcore_maybe_make_sect (abfd
, ".reg2", sect
))
5193 #endif /* defined (HAVE_LWPSTATUS_T) */
5198 elfcore_grok_note (abfd
, note
)
5200 Elf_Internal_Note
* note
;
5207 #if defined (HAVE_PRSTATUS_T)
5209 return elfcore_grok_prstatus (abfd
, note
);
5212 #if defined (HAVE_PSTATUS_T)
5214 return elfcore_grok_pstatus (abfd
, note
);
5217 #if defined (HAVE_LWPSTATUS_T)
5219 return elfcore_grok_lwpstatus (abfd
, note
);
5222 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
5223 return elfcore_grok_prfpreg (abfd
, note
);
5225 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5228 return elfcore_grok_psinfo (abfd
, note
);
5235 elfcore_read_notes (abfd
, offset
, size
)
5246 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
5249 buf
= bfd_malloc ((size_t) size
);
5253 if (bfd_read (buf
, size
, 1, abfd
) != size
)
5261 while (p
< buf
+ size
)
5263 /* FIXME: bad alignment assumption. */
5264 Elf_External_Note
* xnp
= (Elf_External_Note
*) p
;
5265 Elf_Internal_Note in
;
5267 in
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->type
);
5269 in
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->namesz
);
5270 in
.namedata
= xnp
->name
;
5272 in
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->descsz
);
5273 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
5274 in
.descpos
= offset
+ (in
.descdata
- buf
);
5276 if (! elfcore_grok_note (abfd
, &in
))
5279 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
5289 _bfd_elfcore_section_from_phdr (abfd
, phdr
, sec_num
)
5291 Elf_Internal_Phdr
* phdr
;
5294 if (! bfd_section_from_phdr (abfd
, phdr
, sec_num
))
5297 if (phdr
->p_type
== PT_NOTE
5298 && ! elfcore_read_notes (abfd
, phdr
->p_offset
, phdr
->p_filesz
))