1 /* ELF executable support for BFD.
2 Copyright 1993, 94, 95, 96, 97, 1998 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. (Well, you would if this
203 were being used yet.) */
206 CONST
unsigned char *name
;
212 while ((ch
= *name
++) != '\0')
215 if ((g
= (h
& 0xf0000000)) != 0)
224 /* Read a specified number of bytes at a specified offset in an ELF
225 file, into a newly allocated buffer, and return a pointer to the
229 elf_read (abfd
, offset
, size
)
236 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
238 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
240 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
242 if (bfd_get_error () != bfd_error_system_call
)
243 bfd_set_error (bfd_error_file_truncated
);
250 bfd_elf_mkobject (abfd
)
253 /* this just does initialization */
254 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
255 elf_tdata (abfd
) = (struct elf_obj_tdata
*)
256 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
257 if (elf_tdata (abfd
) == 0)
259 /* since everything is done at close time, do we need any
266 bfd_elf_mkcorefile (abfd
)
269 /* I think this can be done just like an object file. */
270 return bfd_elf_mkobject (abfd
);
274 bfd_elf_get_str_section (abfd
, shindex
)
276 unsigned int shindex
;
278 Elf_Internal_Shdr
**i_shdrp
;
279 char *shstrtab
= NULL
;
281 unsigned int shstrtabsize
;
283 i_shdrp
= elf_elfsections (abfd
);
284 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
287 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
288 if (shstrtab
== NULL
)
290 /* No cached one, attempt to read, and cache what we read. */
291 offset
= i_shdrp
[shindex
]->sh_offset
;
292 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
293 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
294 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
300 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
302 unsigned int shindex
;
303 unsigned int strindex
;
305 Elf_Internal_Shdr
*hdr
;
310 hdr
= elf_elfsections (abfd
)[shindex
];
312 if (hdr
->contents
== NULL
313 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
316 if (strindex
>= hdr
->sh_size
)
318 (*_bfd_error_handler
)
319 (_("%s: invalid string offset %u >= %lu for section `%s'"),
320 bfd_get_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
321 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
322 && strindex
== hdr
->sh_name
)
324 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
328 return ((char *) hdr
->contents
) + strindex
;
331 /* Make a BFD section from an ELF section. We store a pointer to the
332 BFD section in the bfd_section field of the header. */
335 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
337 Elf_Internal_Shdr
*hdr
;
343 if (hdr
->bfd_section
!= NULL
)
345 BFD_ASSERT (strcmp (name
,
346 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
350 newsect
= bfd_make_section_anyway (abfd
, name
);
354 newsect
->filepos
= hdr
->sh_offset
;
356 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
357 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
358 || ! bfd_set_section_alignment (abfd
, newsect
,
359 bfd_log2 (hdr
->sh_addralign
)))
362 flags
= SEC_NO_FLAGS
;
363 if (hdr
->sh_type
!= SHT_NOBITS
)
364 flags
|= SEC_HAS_CONTENTS
;
365 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
368 if (hdr
->sh_type
!= SHT_NOBITS
)
371 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
372 flags
|= SEC_READONLY
;
373 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
375 else if ((flags
& SEC_LOAD
) != 0)
378 /* The debugging sections appear to be recognized only by name, not
380 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
381 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
382 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
383 flags
|= SEC_DEBUGGING
;
385 /* As a GNU extension, if the name begins with .gnu.linkonce, we
386 only link a single copy of the section. This is used to support
387 g++. g++ will emit each template expansion in its own section.
388 The symbols will be defined as weak, so that multiple definitions
389 are permitted. The GNU linker extension is to actually discard
390 all but one of the sections. */
391 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
392 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
394 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
397 if ((flags
& SEC_ALLOC
) != 0)
399 Elf_Internal_Phdr
*phdr
;
402 /* Look through the phdrs to see if we need to adjust the lma.
403 If all the p_paddr fields are zero, we ignore them, since
404 some ELF linkers produce such output. */
405 phdr
= elf_tdata (abfd
)->phdr
;
406 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
408 if (phdr
->p_paddr
!= 0)
411 if (i
< elf_elfheader (abfd
)->e_phnum
)
413 phdr
= elf_tdata (abfd
)->phdr
;
414 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
416 if (phdr
->p_type
== PT_LOAD
417 && phdr
->p_vaddr
!= phdr
->p_paddr
418 && phdr
->p_vaddr
<= hdr
->sh_addr
419 && (phdr
->p_vaddr
+ phdr
->p_memsz
420 >= hdr
->sh_addr
+ hdr
->sh_size
)
421 && ((flags
& SEC_LOAD
) == 0
422 || (phdr
->p_offset
<= (bfd_vma
) hdr
->sh_offset
423 && (phdr
->p_offset
+ phdr
->p_filesz
424 >= hdr
->sh_offset
+ hdr
->sh_size
))))
426 newsect
->lma
+= phdr
->p_paddr
- phdr
->p_vaddr
;
433 hdr
->bfd_section
= newsect
;
434 elf_section_data (newsect
)->this_hdr
= *hdr
;
444 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
447 Helper functions for GDB to locate the string tables.
448 Since BFD hides string tables from callers, GDB needs to use an
449 internal hook to find them. Sun's .stabstr, in particular,
450 isn't even pointed to by the .stab section, so ordinary
451 mechanisms wouldn't work to find it, even if we had some.
454 struct elf_internal_shdr
*
455 bfd_elf_find_section (abfd
, name
)
459 Elf_Internal_Shdr
**i_shdrp
;
464 i_shdrp
= elf_elfsections (abfd
);
467 shstrtab
= bfd_elf_get_str_section
468 (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
469 if (shstrtab
!= NULL
)
471 max
= elf_elfheader (abfd
)->e_shnum
;
472 for (i
= 1; i
< max
; i
++)
473 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
480 const char *const bfd_elf_section_type_names
[] = {
481 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
482 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
483 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
486 /* ELF relocs are against symbols. If we are producing relocateable
487 output, and the reloc is against an external symbol, and nothing
488 has given us any additional addend, the resulting reloc will also
489 be against the same symbol. In such a case, we don't want to
490 change anything about the way the reloc is handled, since it will
491 all be done at final link time. Rather than put special case code
492 into bfd_perform_relocation, all the reloc types use this howto
493 function. It just short circuits the reloc if producing
494 relocateable output against an external symbol. */
497 bfd_reloc_status_type
498 bfd_elf_generic_reloc (abfd
,
506 arelent
*reloc_entry
;
509 asection
*input_section
;
511 char **error_message
;
513 if (output_bfd
!= (bfd
*) NULL
514 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
515 && (! reloc_entry
->howto
->partial_inplace
516 || reloc_entry
->addend
== 0))
518 reloc_entry
->address
+= input_section
->output_offset
;
522 return bfd_reloc_continue
;
525 /* Print out the program headers. */
528 _bfd_elf_print_private_bfd_data (abfd
, farg
)
532 FILE *f
= (FILE *) farg
;
533 Elf_Internal_Phdr
*p
;
535 bfd_byte
*dynbuf
= NULL
;
537 p
= elf_tdata (abfd
)->phdr
;
542 fprintf (f
, _("\nProgram Header:\n"));
543 c
= elf_elfheader (abfd
)->e_phnum
;
544 for (i
= 0; i
< c
; i
++, p
++)
551 case PT_NULL
: s
= "NULL"; break;
552 case PT_LOAD
: s
= "LOAD"; break;
553 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
554 case PT_INTERP
: s
= "INTERP"; break;
555 case PT_NOTE
: s
= "NOTE"; break;
556 case PT_SHLIB
: s
= "SHLIB"; break;
557 case PT_PHDR
: s
= "PHDR"; break;
558 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
560 fprintf (f
, "%8s off 0x", s
);
561 fprintf_vma (f
, p
->p_offset
);
562 fprintf (f
, " vaddr 0x");
563 fprintf_vma (f
, p
->p_vaddr
);
564 fprintf (f
, " paddr 0x");
565 fprintf_vma (f
, p
->p_paddr
);
566 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
567 fprintf (f
, " filesz 0x");
568 fprintf_vma (f
, p
->p_filesz
);
569 fprintf (f
, " memsz 0x");
570 fprintf_vma (f
, p
->p_memsz
);
571 fprintf (f
, " flags %c%c%c",
572 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
573 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
574 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
575 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
576 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
581 s
= bfd_get_section_by_name (abfd
, ".dynamic");
586 bfd_byte
*extdyn
, *extdynend
;
588 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
590 fprintf (f
, _("\nDynamic Section:\n"));
592 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
595 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
599 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
602 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
604 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
605 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
608 extdynend
= extdyn
+ s
->_raw_size
;
609 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
611 Elf_Internal_Dyn dyn
;
616 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
618 if (dyn
.d_tag
== DT_NULL
)
625 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
629 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
630 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
631 case DT_PLTGOT
: name
= "PLTGOT"; break;
632 case DT_HASH
: name
= "HASH"; break;
633 case DT_STRTAB
: name
= "STRTAB"; break;
634 case DT_SYMTAB
: name
= "SYMTAB"; break;
635 case DT_RELA
: name
= "RELA"; break;
636 case DT_RELASZ
: name
= "RELASZ"; break;
637 case DT_RELAENT
: name
= "RELAENT"; break;
638 case DT_STRSZ
: name
= "STRSZ"; break;
639 case DT_SYMENT
: name
= "SYMENT"; break;
640 case DT_INIT
: name
= "INIT"; break;
641 case DT_FINI
: name
= "FINI"; break;
642 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
643 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
644 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
645 case DT_REL
: name
= "REL"; break;
646 case DT_RELSZ
: name
= "RELSZ"; break;
647 case DT_RELENT
: name
= "RELENT"; break;
648 case DT_PLTREL
: name
= "PLTREL"; break;
649 case DT_DEBUG
: name
= "DEBUG"; break;
650 case DT_TEXTREL
: name
= "TEXTREL"; break;
651 case DT_JMPREL
: name
= "JMPREL"; break;
652 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
653 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
654 case DT_VERSYM
: name
= "VERSYM"; break;
655 case DT_VERDEF
: name
= "VERDEF"; break;
656 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
657 case DT_VERNEED
: name
= "VERNEED"; break;
658 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
661 fprintf (f
, " %-11s ", name
);
663 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
668 string
= bfd_elf_string_from_elf_section (abfd
, link
,
672 fprintf (f
, "%s", string
);
681 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
682 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
684 if (! _bfd_elf_slurp_version_tables (abfd
))
688 if (elf_dynverdef (abfd
) != 0)
690 Elf_Internal_Verdef
*t
;
692 fprintf (f
, _("\nVersion definitions:\n"));
693 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
695 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
696 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
697 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
699 Elf_Internal_Verdaux
*a
;
702 for (a
= t
->vd_auxptr
->vda_nextptr
;
705 fprintf (f
, "%s ", a
->vda_nodename
);
711 if (elf_dynverref (abfd
) != 0)
713 Elf_Internal_Verneed
*t
;
715 fprintf (f
, _("\nVersion References:\n"));
716 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
718 Elf_Internal_Vernaux
*a
;
720 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
721 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
722 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
723 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
735 /* Display ELF-specific fields of a symbol. */
738 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
742 bfd_print_symbol_type how
;
744 FILE *file
= (FILE *) filep
;
747 case bfd_print_symbol_name
:
748 fprintf (file
, "%s", symbol
->name
);
750 case bfd_print_symbol_more
:
751 fprintf (file
, "elf ");
752 fprintf_vma (file
, symbol
->value
);
753 fprintf (file
, " %lx", (long) symbol
->flags
);
755 case bfd_print_symbol_all
:
757 CONST
char *section_name
;
758 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
759 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
760 fprintf (file
, " %s\t", section_name
);
761 /* Print the "other" value for a symbol. For common symbols,
762 we've already printed the size; now print the alignment.
763 For other symbols, we have no specified alignment, and
764 we've printed the address; now print the size. */
766 (bfd_is_com_section (symbol
->section
)
767 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
768 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
770 /* If we have version information, print it. */
771 if (elf_tdata (abfd
)->dynversym_section
!= 0
772 && (elf_tdata (abfd
)->dynverdef_section
!= 0
773 || elf_tdata (abfd
)->dynverref_section
!= 0))
776 const char *version_string
;
778 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
782 else if (vernum
== 1)
783 version_string
= "Base";
784 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
786 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
789 Elf_Internal_Verneed
*t
;
792 for (t
= elf_tdata (abfd
)->verref
;
796 Elf_Internal_Vernaux
*a
;
798 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
800 if (a
->vna_other
== vernum
)
802 version_string
= a
->vna_nodename
;
809 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
810 fprintf (file
, " %-11s", version_string
);
815 fprintf (file
, " (%s)", version_string
);
816 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
821 /* If the st_other field is not zero, print it. */
822 if (((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
!= 0)
823 fprintf (file
, " 0x%02x",
825 ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
));
827 fprintf (file
, " %s", symbol
->name
);
833 /* Create an entry in an ELF linker hash table. */
835 struct bfd_hash_entry
*
836 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
837 struct bfd_hash_entry
*entry
;
838 struct bfd_hash_table
*table
;
841 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
843 /* Allocate the structure if it has not already been allocated by a
845 if (ret
== (struct elf_link_hash_entry
*) NULL
)
846 ret
= ((struct elf_link_hash_entry
*)
847 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
848 if (ret
== (struct elf_link_hash_entry
*) NULL
)
849 return (struct bfd_hash_entry
*) ret
;
851 /* Call the allocation method of the superclass. */
852 ret
= ((struct elf_link_hash_entry
*)
853 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
855 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
857 /* Set local fields. */
861 ret
->dynstr_index
= 0;
863 ret
->got
.offset
= (bfd_vma
) -1;
864 ret
->plt
.offset
= (bfd_vma
) -1;
865 ret
->linker_section_pointer
= (elf_linker_section_pointers_t
*)0;
866 ret
->verinfo
.verdef
= NULL
;
867 ret
->vtable_entries_used
= NULL
;
868 ret
->vtable_parent
= NULL
;
869 ret
->type
= STT_NOTYPE
;
871 /* Assume that we have been called by a non-ELF symbol reader.
872 This flag is then reset by the code which reads an ELF input
873 file. This ensures that a symbol created by a non-ELF symbol
874 reader will have the flag set correctly. */
875 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
878 return (struct bfd_hash_entry
*) ret
;
881 /* Initialize an ELF linker hash table. */
884 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
885 struct elf_link_hash_table
*table
;
887 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
888 struct bfd_hash_table
*,
891 table
->dynamic_sections_created
= false;
892 table
->dynobj
= NULL
;
893 /* The first dynamic symbol is a dummy. */
894 table
->dynsymcount
= 1;
895 table
->dynstr
= NULL
;
896 table
->bucketcount
= 0;
897 table
->needed
= NULL
;
899 table
->stab_info
= NULL
;
900 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
903 /* Create an ELF linker hash table. */
905 struct bfd_link_hash_table
*
906 _bfd_elf_link_hash_table_create (abfd
)
909 struct elf_link_hash_table
*ret
;
911 ret
= ((struct elf_link_hash_table
*)
912 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
913 if (ret
== (struct elf_link_hash_table
*) NULL
)
916 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
918 bfd_release (abfd
, ret
);
925 /* This is a hook for the ELF emulation code in the generic linker to
926 tell the backend linker what file name to use for the DT_NEEDED
927 entry for a dynamic object. The generic linker passes name as an
928 empty string to indicate that no DT_NEEDED entry should be made. */
931 bfd_elf_set_dt_needed_name (abfd
, name
)
935 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
936 && bfd_get_format (abfd
) == bfd_object
)
937 elf_dt_name (abfd
) = name
;
940 /* Get the list of DT_NEEDED entries for a link. This is a hook for
941 the linker ELF emulation code. */
943 struct bfd_link_needed_list
*
944 bfd_elf_get_needed_list (abfd
, info
)
946 struct bfd_link_info
*info
;
948 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
950 return elf_hash_table (info
)->needed
;
953 /* Get the name actually used for a dynamic object for a link. This
954 is the SONAME entry if there is one. Otherwise, it is the string
955 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
958 bfd_elf_get_dt_soname (abfd
)
961 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
962 && bfd_get_format (abfd
) == bfd_object
)
963 return elf_dt_name (abfd
);
967 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
968 the ELF linker emulation code. */
971 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
973 struct bfd_link_needed_list
**pneeded
;
976 bfd_byte
*dynbuf
= NULL
;
979 bfd_byte
*extdyn
, *extdynend
;
981 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
985 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
986 || bfd_get_format (abfd
) != bfd_object
)
989 s
= bfd_get_section_by_name (abfd
, ".dynamic");
990 if (s
== NULL
|| s
->_raw_size
== 0)
993 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
997 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1001 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1005 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1007 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1008 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1011 extdynend
= extdyn
+ s
->_raw_size
;
1012 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1014 Elf_Internal_Dyn dyn
;
1016 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1018 if (dyn
.d_tag
== DT_NULL
)
1021 if (dyn
.d_tag
== DT_NEEDED
)
1024 struct bfd_link_needed_list
*l
;
1026 string
= bfd_elf_string_from_elf_section (abfd
, link
,
1031 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, sizeof *l
);
1052 /* Allocate an ELF string table--force the first byte to be zero. */
1054 struct bfd_strtab_hash
*
1055 _bfd_elf_stringtab_init ()
1057 struct bfd_strtab_hash
*ret
;
1059 ret
= _bfd_stringtab_init ();
1064 loc
= _bfd_stringtab_add (ret
, "", true, false);
1065 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1066 if (loc
== (bfd_size_type
) -1)
1068 _bfd_stringtab_free (ret
);
1075 /* ELF .o/exec file reading */
1077 /* Create a new bfd section from an ELF section header. */
1080 bfd_section_from_shdr (abfd
, shindex
)
1082 unsigned int shindex
;
1084 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1085 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1086 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1089 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1091 switch (hdr
->sh_type
)
1094 /* Inactive section. Throw it away. */
1097 case SHT_PROGBITS
: /* Normal section with contents. */
1098 case SHT_DYNAMIC
: /* Dynamic linking information. */
1099 case SHT_NOBITS
: /* .bss section. */
1100 case SHT_HASH
: /* .hash section. */
1101 case SHT_NOTE
: /* .note section. */
1102 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1104 case SHT_SYMTAB
: /* A symbol table */
1105 if (elf_onesymtab (abfd
) == shindex
)
1108 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1109 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1110 elf_onesymtab (abfd
) = shindex
;
1111 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1112 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1113 abfd
->flags
|= HAS_SYMS
;
1115 /* Sometimes a shared object will map in the symbol table. If
1116 SHF_ALLOC is set, and this is a shared object, then we also
1117 treat this section as a BFD section. We can not base the
1118 decision purely on SHF_ALLOC, because that flag is sometimes
1119 set in a relocateable object file, which would confuse the
1121 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1122 && (abfd
->flags
& DYNAMIC
) != 0
1123 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1128 case SHT_DYNSYM
: /* A dynamic symbol table */
1129 if (elf_dynsymtab (abfd
) == shindex
)
1132 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1133 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1134 elf_dynsymtab (abfd
) = shindex
;
1135 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1136 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1137 abfd
->flags
|= HAS_SYMS
;
1139 /* Besides being a symbol table, we also treat this as a regular
1140 section, so that objcopy can handle it. */
1141 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1143 case SHT_STRTAB
: /* A string table */
1144 if (hdr
->bfd_section
!= NULL
)
1146 if (ehdr
->e_shstrndx
== shindex
)
1148 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1149 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1155 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1157 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1158 if (hdr2
->sh_link
== shindex
)
1160 if (! bfd_section_from_shdr (abfd
, i
))
1162 if (elf_onesymtab (abfd
) == i
)
1164 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1165 elf_elfsections (abfd
)[shindex
] =
1166 &elf_tdata (abfd
)->strtab_hdr
;
1169 if (elf_dynsymtab (abfd
) == i
)
1171 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1172 elf_elfsections (abfd
)[shindex
] = hdr
=
1173 &elf_tdata (abfd
)->dynstrtab_hdr
;
1174 /* We also treat this as a regular section, so
1175 that objcopy can handle it. */
1178 #if 0 /* Not handling other string tables specially right now. */
1179 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1180 /* We have a strtab for some random other section. */
1181 newsect
= (asection
*) hdr2
->bfd_section
;
1184 hdr
->bfd_section
= newsect
;
1185 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1187 elf_elfsections (abfd
)[shindex
] = hdr2
;
1193 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1197 /* *These* do a lot of work -- but build no sections! */
1199 asection
*target_sect
;
1200 Elf_Internal_Shdr
*hdr2
;
1202 /* For some incomprehensible reason Oracle distributes
1203 libraries for Solaris in which some of the objects have
1204 bogus sh_link fields. It would be nice if we could just
1205 reject them, but, unfortunately, some people need to use
1206 them. We scan through the section headers; if we find only
1207 one suitable symbol table, we clobber the sh_link to point
1208 to it. I hope this doesn't break anything. */
1209 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1210 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1216 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1218 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1219 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1230 hdr
->sh_link
= found
;
1233 /* Get the symbol table. */
1234 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1235 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1238 /* If this reloc section does not use the main symbol table we
1239 don't treat it as a reloc section. BFD can't adequately
1240 represent such a section, so at least for now, we don't
1241 try. We just present it as a normal section. */
1242 if (hdr
->sh_link
!= elf_onesymtab (abfd
))
1243 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1245 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1247 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1248 if (target_sect
== NULL
)
1251 if ((target_sect
->flags
& SEC_RELOC
) == 0
1252 || target_sect
->reloc_count
== 0)
1253 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1256 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1257 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
1258 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1261 elf_elfsections (abfd
)[shindex
] = hdr2
;
1262 target_sect
->reloc_count
+= hdr
->sh_size
/ hdr
->sh_entsize
;
1263 target_sect
->flags
|= SEC_RELOC
;
1264 target_sect
->relocation
= NULL
;
1265 target_sect
->rel_filepos
= hdr
->sh_offset
;
1266 abfd
->flags
|= HAS_RELOC
;
1271 case SHT_GNU_verdef
:
1272 elf_dynverdef (abfd
) = shindex
;
1273 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1274 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1277 case SHT_GNU_versym
:
1278 elf_dynversym (abfd
) = shindex
;
1279 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1280 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1283 case SHT_GNU_verneed
:
1284 elf_dynverref (abfd
) = shindex
;
1285 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1286 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1293 /* Check for any processor-specific section types. */
1295 if (bed
->elf_backend_section_from_shdr
)
1296 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1304 /* Given an ELF section number, retrieve the corresponding BFD
1308 bfd_section_from_elf_index (abfd
, index
)
1312 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1313 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1315 return elf_elfsections (abfd
)[index
]->bfd_section
;
1319 _bfd_elf_new_section_hook (abfd
, sec
)
1323 struct bfd_elf_section_data
*sdata
;
1325 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
1328 sec
->used_by_bfd
= (PTR
) sdata
;
1329 memset (sdata
, 0, sizeof (*sdata
));
1333 /* Create a new bfd section from an ELF program header.
1335 Since program segments have no names, we generate a synthetic name
1336 of the form segment<NUM>, where NUM is generally the index in the
1337 program header table. For segments that are split (see below) we
1338 generate the names segment<NUM>a and segment<NUM>b.
1340 Note that some program segments may have a file size that is different than
1341 (less than) the memory size. All this means is that at execution the
1342 system must allocate the amount of memory specified by the memory size,
1343 but only initialize it with the first "file size" bytes read from the
1344 file. This would occur for example, with program segments consisting
1345 of combined data+bss.
1347 To handle the above situation, this routine generates TWO bfd sections
1348 for the single program segment. The first has the length specified by
1349 the file size of the segment, and the second has the length specified
1350 by the difference between the two sizes. In effect, the segment is split
1351 into it's initialized and uninitialized parts.
1356 bfd_section_from_phdr (abfd
, hdr
, index
)
1358 Elf_Internal_Phdr
*hdr
;
1366 split
= ((hdr
->p_memsz
> 0)
1367 && (hdr
->p_filesz
> 0)
1368 && (hdr
->p_memsz
> hdr
->p_filesz
));
1369 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
1370 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1373 strcpy (name
, namebuf
);
1374 newsect
= bfd_make_section (abfd
, name
);
1375 if (newsect
== NULL
)
1377 newsect
->vma
= hdr
->p_vaddr
;
1378 newsect
->lma
= hdr
->p_paddr
;
1379 newsect
->_raw_size
= hdr
->p_filesz
;
1380 newsect
->filepos
= hdr
->p_offset
;
1381 newsect
->flags
|= SEC_HAS_CONTENTS
;
1382 if (hdr
->p_type
== PT_LOAD
)
1384 newsect
->flags
|= SEC_ALLOC
;
1385 newsect
->flags
|= SEC_LOAD
;
1386 if (hdr
->p_flags
& PF_X
)
1388 /* FIXME: all we known is that it has execute PERMISSION,
1390 newsect
->flags
|= SEC_CODE
;
1393 if (!(hdr
->p_flags
& PF_W
))
1395 newsect
->flags
|= SEC_READONLY
;
1400 sprintf (namebuf
, "segment%db", index
);
1401 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1404 strcpy (name
, namebuf
);
1405 newsect
= bfd_make_section (abfd
, name
);
1406 if (newsect
== NULL
)
1408 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1409 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1410 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1411 if (hdr
->p_type
== PT_LOAD
)
1413 newsect
->flags
|= SEC_ALLOC
;
1414 if (hdr
->p_flags
& PF_X
)
1415 newsect
->flags
|= SEC_CODE
;
1417 if (!(hdr
->p_flags
& PF_W
))
1418 newsect
->flags
|= SEC_READONLY
;
1424 /* Set up an ELF internal section header for a section. */
1428 elf_fake_sections (abfd
, asect
, failedptrarg
)
1433 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1434 boolean
*failedptr
= (boolean
*) failedptrarg
;
1435 Elf_Internal_Shdr
*this_hdr
;
1439 /* We already failed; just get out of the bfd_map_over_sections
1444 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1446 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1449 if (this_hdr
->sh_name
== (unsigned long) -1)
1455 this_hdr
->sh_flags
= 0;
1457 if ((asect
->flags
& SEC_ALLOC
) != 0
1458 || asect
->user_set_vma
)
1459 this_hdr
->sh_addr
= asect
->vma
;
1461 this_hdr
->sh_addr
= 0;
1463 this_hdr
->sh_offset
= 0;
1464 this_hdr
->sh_size
= asect
->_raw_size
;
1465 this_hdr
->sh_link
= 0;
1466 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1467 /* The sh_entsize and sh_info fields may have been set already by
1468 copy_private_section_data. */
1470 this_hdr
->bfd_section
= asect
;
1471 this_hdr
->contents
= NULL
;
1473 /* FIXME: This should not be based on section names. */
1474 if (strcmp (asect
->name
, ".dynstr") == 0)
1475 this_hdr
->sh_type
= SHT_STRTAB
;
1476 else if (strcmp (asect
->name
, ".hash") == 0)
1478 this_hdr
->sh_type
= SHT_HASH
;
1479 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
1481 else if (strcmp (asect
->name
, ".dynsym") == 0)
1483 this_hdr
->sh_type
= SHT_DYNSYM
;
1484 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1486 else if (strcmp (asect
->name
, ".dynamic") == 0)
1488 this_hdr
->sh_type
= SHT_DYNAMIC
;
1489 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1491 else if (strncmp (asect
->name
, ".rela", 5) == 0
1492 && get_elf_backend_data (abfd
)->use_rela_p
)
1494 this_hdr
->sh_type
= SHT_RELA
;
1495 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1497 else if (strncmp (asect
->name
, ".rel", 4) == 0
1498 && ! get_elf_backend_data (abfd
)->use_rela_p
)
1500 this_hdr
->sh_type
= SHT_REL
;
1501 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1503 else if (strncmp (asect
->name
, ".note", 5) == 0)
1504 this_hdr
->sh_type
= SHT_NOTE
;
1505 else if (strncmp (asect
->name
, ".stab", 5) == 0
1506 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1507 this_hdr
->sh_type
= SHT_STRTAB
;
1508 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1510 this_hdr
->sh_type
= SHT_GNU_versym
;
1511 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1513 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1515 this_hdr
->sh_type
= SHT_GNU_verdef
;
1516 this_hdr
->sh_entsize
= 0;
1517 /* objcopy or strip will copy over sh_info, but may not set
1518 cverdefs. The linker will set cverdefs, but sh_info will be
1520 if (this_hdr
->sh_info
== 0)
1521 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1523 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1524 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1526 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1528 this_hdr
->sh_type
= SHT_GNU_verneed
;
1529 this_hdr
->sh_entsize
= 0;
1530 /* objcopy or strip will copy over sh_info, but may not set
1531 cverrefs. The linker will set cverrefs, but sh_info will be
1533 if (this_hdr
->sh_info
== 0)
1534 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1536 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1537 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1539 else if ((asect
->flags
& SEC_ALLOC
) != 0
1540 && (asect
->flags
& SEC_LOAD
) != 0)
1541 this_hdr
->sh_type
= SHT_PROGBITS
;
1542 else if ((asect
->flags
& SEC_ALLOC
) != 0
1543 && ((asect
->flags
& SEC_LOAD
) == 0))
1544 this_hdr
->sh_type
= SHT_NOBITS
;
1548 this_hdr
->sh_type
= SHT_PROGBITS
;
1551 if ((asect
->flags
& SEC_ALLOC
) != 0)
1552 this_hdr
->sh_flags
|= SHF_ALLOC
;
1553 if ((asect
->flags
& SEC_READONLY
) == 0)
1554 this_hdr
->sh_flags
|= SHF_WRITE
;
1555 if ((asect
->flags
& SEC_CODE
) != 0)
1556 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1558 /* Check for processor-specific section types. */
1560 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1562 if (bed
->elf_backend_fake_sections
)
1563 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1566 /* If the section has relocs, set up a section header for the
1567 SHT_REL[A] section. */
1568 if ((asect
->flags
& SEC_RELOC
) != 0)
1570 Elf_Internal_Shdr
*rela_hdr
;
1571 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1574 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1575 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1581 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1583 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1585 if (rela_hdr
->sh_name
== (unsigned int) -1)
1590 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1591 rela_hdr
->sh_entsize
= (use_rela_p
1592 ? bed
->s
->sizeof_rela
1593 : bed
->s
->sizeof_rel
);
1594 rela_hdr
->sh_addralign
= bed
->s
->file_align
;
1595 rela_hdr
->sh_flags
= 0;
1596 rela_hdr
->sh_addr
= 0;
1597 rela_hdr
->sh_size
= 0;
1598 rela_hdr
->sh_offset
= 0;
1602 /* Assign all ELF section numbers. The dummy first section is handled here
1603 too. The link/info pointers for the standard section types are filled
1604 in here too, while we're at it. */
1607 assign_section_numbers (abfd
)
1610 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1612 unsigned int section_number
;
1613 Elf_Internal_Shdr
**i_shdrp
;
1614 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1618 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1620 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1622 d
->this_idx
= section_number
++;
1623 if ((sec
->flags
& SEC_RELOC
) == 0)
1626 d
->rel_idx
= section_number
++;
1629 t
->shstrtab_section
= section_number
++;
1630 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1631 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1633 if (abfd
->symcount
> 0)
1635 t
->symtab_section
= section_number
++;
1636 t
->strtab_section
= section_number
++;
1639 elf_elfheader (abfd
)->e_shnum
= section_number
;
1641 /* Set up the list of section header pointers, in agreement with the
1643 i_shdrp
= ((Elf_Internal_Shdr
**)
1644 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1645 if (i_shdrp
== NULL
)
1648 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1649 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1650 if (i_shdrp
[0] == NULL
)
1652 bfd_release (abfd
, i_shdrp
);
1655 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1657 elf_elfsections (abfd
) = i_shdrp
;
1659 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1660 if (abfd
->symcount
> 0)
1662 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1663 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1664 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1666 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1668 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1672 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1673 if (d
->rel_idx
!= 0)
1674 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1676 /* Fill in the sh_link and sh_info fields while we're at it. */
1678 /* sh_link of a reloc section is the section index of the symbol
1679 table. sh_info is the section index of the section to which
1680 the relocation entries apply. */
1681 if (d
->rel_idx
!= 0)
1683 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1684 d
->rel_hdr
.sh_info
= d
->this_idx
;
1687 switch (d
->this_hdr
.sh_type
)
1691 /* A reloc section which we are treating as a normal BFD
1692 section. sh_link is the section index of the symbol
1693 table. sh_info is the section index of the section to
1694 which the relocation entries apply. We assume that an
1695 allocated reloc section uses the dynamic symbol table.
1696 FIXME: How can we be sure? */
1697 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1699 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1701 /* We look up the section the relocs apply to by name. */
1703 if (d
->this_hdr
.sh_type
== SHT_REL
)
1707 s
= bfd_get_section_by_name (abfd
, name
);
1709 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1713 /* We assume that a section named .stab*str is a stabs
1714 string section. We look for a section with the same name
1715 but without the trailing ``str'', and set its sh_link
1716 field to point to this section. */
1717 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1718 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1723 len
= strlen (sec
->name
);
1724 alc
= (char *) bfd_malloc (len
- 2);
1727 strncpy (alc
, sec
->name
, len
- 3);
1728 alc
[len
- 3] = '\0';
1729 s
= bfd_get_section_by_name (abfd
, alc
);
1733 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1735 /* This is a .stab section. */
1736 elf_section_data (s
)->this_hdr
.sh_entsize
=
1737 4 + 2 * (bed
->s
->arch_size
/ 8);
1744 case SHT_GNU_verneed
:
1745 case SHT_GNU_verdef
:
1746 /* sh_link is the section header index of the string table
1747 used for the dynamic entries, or the symbol table, or the
1749 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1751 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1755 case SHT_GNU_versym
:
1756 /* sh_link is the section header index of the symbol table
1757 this hash table or version table is for. */
1758 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1760 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1768 /* Map symbol from it's internal number to the external number, moving
1769 all local symbols to be at the head of the list. */
1772 sym_is_global (abfd
, sym
)
1776 /* If the backend has a special mapping, use it. */
1777 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1778 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1781 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1782 || bfd_is_und_section (bfd_get_section (sym
))
1783 || bfd_is_com_section (bfd_get_section (sym
)));
1787 elf_map_symbols (abfd
)
1790 int symcount
= bfd_get_symcount (abfd
);
1791 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1792 asymbol
**sect_syms
;
1794 int num_globals
= 0;
1795 int num_locals2
= 0;
1796 int num_globals2
= 0;
1798 int num_sections
= 0;
1804 fprintf (stderr
, "elf_map_symbols\n");
1808 /* Add a section symbol for each BFD section. FIXME: Is this really
1810 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1812 if (max_index
< asect
->index
)
1813 max_index
= asect
->index
;
1817 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1818 if (sect_syms
== NULL
)
1820 elf_section_syms (abfd
) = sect_syms
;
1822 for (idx
= 0; idx
< symcount
; idx
++)
1824 if ((syms
[idx
]->flags
& BSF_SECTION_SYM
) != 0
1825 && syms
[idx
]->value
== 0)
1829 sec
= syms
[idx
]->section
;
1830 if (sec
->owner
!= NULL
)
1832 if (sec
->owner
!= abfd
)
1834 if (sec
->output_offset
!= 0)
1836 sec
= sec
->output_section
;
1837 BFD_ASSERT (sec
->owner
== abfd
);
1839 sect_syms
[sec
->index
] = syms
[idx
];
1844 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1848 if (sect_syms
[asect
->index
] != NULL
)
1851 sym
= bfd_make_empty_symbol (abfd
);
1854 sym
->the_bfd
= abfd
;
1855 sym
->name
= asect
->name
;
1857 /* Set the flags to 0 to indicate that this one was newly added. */
1859 sym
->section
= asect
;
1860 sect_syms
[asect
->index
] = sym
;
1864 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
1865 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1869 /* Classify all of the symbols. */
1870 for (idx
= 0; idx
< symcount
; idx
++)
1872 if (!sym_is_global (abfd
, syms
[idx
]))
1877 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1879 if (sect_syms
[asect
->index
] != NULL
1880 && sect_syms
[asect
->index
]->flags
== 0)
1882 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
1883 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
1887 sect_syms
[asect
->index
]->flags
= 0;
1891 /* Now sort the symbols so the local symbols are first. */
1892 new_syms
= ((asymbol
**)
1894 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
1895 if (new_syms
== NULL
)
1898 for (idx
= 0; idx
< symcount
; idx
++)
1900 asymbol
*sym
= syms
[idx
];
1903 if (!sym_is_global (abfd
, sym
))
1906 i
= num_locals
+ num_globals2
++;
1908 sym
->udata
.i
= i
+ 1;
1910 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1912 if (sect_syms
[asect
->index
] != NULL
1913 && sect_syms
[asect
->index
]->flags
== 0)
1915 asymbol
*sym
= sect_syms
[asect
->index
];
1918 sym
->flags
= BSF_SECTION_SYM
;
1919 if (!sym_is_global (abfd
, sym
))
1922 i
= num_locals
+ num_globals2
++;
1924 sym
->udata
.i
= i
+ 1;
1928 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
1930 elf_num_locals (abfd
) = num_locals
;
1931 elf_num_globals (abfd
) = num_globals
;
1935 /* Align to the maximum file alignment that could be required for any
1936 ELF data structure. */
1938 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
1939 static INLINE file_ptr
1940 align_file_position (off
, align
)
1944 return (off
+ align
- 1) & ~(align
- 1);
1947 /* Assign a file position to a section, optionally aligning to the
1948 required section alignment. */
1951 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
1952 Elf_Internal_Shdr
*i_shdrp
;
1960 al
= i_shdrp
->sh_addralign
;
1962 offset
= BFD_ALIGN (offset
, al
);
1964 i_shdrp
->sh_offset
= offset
;
1965 if (i_shdrp
->bfd_section
!= NULL
)
1966 i_shdrp
->bfd_section
->filepos
= offset
;
1967 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1968 offset
+= i_shdrp
->sh_size
;
1972 /* Compute the file positions we are going to put the sections at, and
1973 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1974 is not NULL, this is being called by the ELF backend linker. */
1977 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
1979 struct bfd_link_info
*link_info
;
1981 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1983 struct bfd_strtab_hash
*strtab
;
1984 Elf_Internal_Shdr
*shstrtab_hdr
;
1986 if (abfd
->output_has_begun
)
1989 /* Do any elf backend specific processing first. */
1990 if (bed
->elf_backend_begin_write_processing
)
1991 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
1993 if (! prep_headers (abfd
))
1997 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2001 if (!assign_section_numbers (abfd
))
2004 /* The backend linker builds symbol table information itself. */
2005 if (link_info
== NULL
&& abfd
->symcount
> 0)
2007 /* Non-zero if doing a relocatable link. */
2008 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2010 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2014 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2015 /* sh_name was set in prep_headers. */
2016 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2017 shstrtab_hdr
->sh_flags
= 0;
2018 shstrtab_hdr
->sh_addr
= 0;
2019 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2020 shstrtab_hdr
->sh_entsize
= 0;
2021 shstrtab_hdr
->sh_link
= 0;
2022 shstrtab_hdr
->sh_info
= 0;
2023 /* sh_offset is set in assign_file_positions_except_relocs. */
2024 shstrtab_hdr
->sh_addralign
= 1;
2026 if (!assign_file_positions_except_relocs (abfd
))
2029 if (link_info
== NULL
&& abfd
->symcount
> 0)
2032 Elf_Internal_Shdr
*hdr
;
2034 off
= elf_tdata (abfd
)->next_file_pos
;
2036 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2037 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2039 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2040 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2042 elf_tdata (abfd
)->next_file_pos
= off
;
2044 /* Now that we know where the .strtab section goes, write it
2046 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2047 || ! _bfd_stringtab_emit (abfd
, strtab
))
2049 _bfd_stringtab_free (strtab
);
2052 abfd
->output_has_begun
= true;
2057 /* Create a mapping from a set of sections to a program segment. */
2059 static INLINE
struct elf_segment_map
*
2060 make_mapping (abfd
, sections
, from
, to
, phdr
)
2062 asection
**sections
;
2067 struct elf_segment_map
*m
;
2071 m
= ((struct elf_segment_map
*)
2073 (sizeof (struct elf_segment_map
)
2074 + (to
- from
- 1) * sizeof (asection
*))));
2078 m
->p_type
= PT_LOAD
;
2079 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2080 m
->sections
[i
- from
] = *hdrpp
;
2081 m
->count
= to
- from
;
2083 if (from
== 0 && phdr
)
2085 /* Include the headers in the first PT_LOAD segment. */
2086 m
->includes_filehdr
= 1;
2087 m
->includes_phdrs
= 1;
2093 /* Set up a mapping from BFD sections to program segments. */
2096 map_sections_to_segments (abfd
)
2099 asection
**sections
= NULL
;
2103 struct elf_segment_map
*mfirst
;
2104 struct elf_segment_map
**pm
;
2105 struct elf_segment_map
*m
;
2107 unsigned int phdr_index
;
2108 bfd_vma maxpagesize
;
2110 boolean phdr_in_segment
= true;
2114 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2117 if (bfd_count_sections (abfd
) == 0)
2120 /* Select the allocated sections, and sort them. */
2122 sections
= (asection
**) bfd_malloc (bfd_count_sections (abfd
)
2123 * sizeof (asection
*));
2124 if (sections
== NULL
)
2128 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2130 if ((s
->flags
& SEC_ALLOC
) != 0)
2136 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2139 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2141 /* Build the mapping. */
2146 /* If we have a .interp section, then create a PT_PHDR segment for
2147 the program headers and a PT_INTERP segment for the .interp
2149 s
= bfd_get_section_by_name (abfd
, ".interp");
2150 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2152 m
= ((struct elf_segment_map
*)
2153 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2157 m
->p_type
= PT_PHDR
;
2158 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2159 m
->p_flags
= PF_R
| PF_X
;
2160 m
->p_flags_valid
= 1;
2161 m
->includes_phdrs
= 1;
2166 m
= ((struct elf_segment_map
*)
2167 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2171 m
->p_type
= PT_INTERP
;
2179 /* Look through the sections. We put sections in the same program
2180 segment when the start of the second section can be placed within
2181 a few bytes of the end of the first section. */
2184 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2186 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2188 && (dynsec
->flags
& SEC_LOAD
) == 0)
2191 /* Deal with -Ttext or something similar such that the first section
2192 is not adjacent to the program headers. This is an
2193 approximation, since at this point we don't know exactly how many
2194 program headers we will need. */
2197 bfd_size_type phdr_size
;
2199 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2201 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2202 if ((abfd
->flags
& D_PAGED
) == 0
2203 || sections
[0]->lma
< phdr_size
2204 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2205 phdr_in_segment
= false;
2208 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2211 boolean new_segment
;
2215 /* See if this section and the last one will fit in the same
2218 if (last_hdr
== NULL
)
2220 /* If we don't have a segment yet, then we don't need a new
2221 one (we build the last one after this loop). */
2222 new_segment
= false;
2224 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2226 /* If this section has a different relation between the
2227 virtual address and the load address, then we need a new
2231 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2232 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2234 /* If putting this section in this segment would force us to
2235 skip a page in the segment, then we need a new segment. */
2238 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2239 && (hdr
->flags
& SEC_LOAD
) != 0)
2241 /* We don't want to put a loadable section after a
2242 nonloadable section in the same segment. */
2245 else if ((abfd
->flags
& D_PAGED
) == 0)
2247 /* If the file is not demand paged, which means that we
2248 don't require the sections to be correctly aligned in the
2249 file, then there is no other reason for a new segment. */
2250 new_segment
= false;
2253 && (hdr
->flags
& SEC_READONLY
) == 0
2254 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2257 /* We don't want to put a writable section in a read only
2258 segment, unless they are on the same page in memory
2259 anyhow. We already know that the last section does not
2260 bring us past the current section on the page, so the
2261 only case in which the new section is not on the same
2262 page as the previous section is when the previous section
2263 ends precisely on a page boundary. */
2268 /* Otherwise, we can use the same segment. */
2269 new_segment
= false;
2274 if ((hdr
->flags
& SEC_READONLY
) == 0)
2280 /* We need a new program segment. We must create a new program
2281 header holding all the sections from phdr_index until hdr. */
2283 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2290 if ((hdr
->flags
& SEC_READONLY
) == 0)
2297 phdr_in_segment
= false;
2300 /* Create a final PT_LOAD program segment. */
2301 if (last_hdr
!= NULL
)
2303 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2311 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2314 m
= ((struct elf_segment_map
*)
2315 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2319 m
->p_type
= PT_DYNAMIC
;
2321 m
->sections
[0] = dynsec
;
2327 /* For each loadable .note section, add a PT_NOTE segment. We don't
2328 use bfd_get_section_by_name, because if we link together
2329 nonloadable .note sections and loadable .note sections, we will
2330 generate two .note sections in the output file. FIXME: Using
2331 names for section types is bogus anyhow. */
2332 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2334 if ((s
->flags
& SEC_LOAD
) != 0
2335 && strncmp (s
->name
, ".note", 5) == 0)
2337 m
= ((struct elf_segment_map
*)
2338 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2342 m
->p_type
= PT_NOTE
;
2354 elf_tdata (abfd
)->segment_map
= mfirst
;
2358 if (sections
!= NULL
)
2363 /* Sort sections by address. */
2366 elf_sort_sections (arg1
, arg2
)
2370 const asection
*sec1
= *(const asection
**) arg1
;
2371 const asection
*sec2
= *(const asection
**) arg2
;
2373 /* Sort by LMA first, since this is the address used to
2374 place the section into a segment. */
2375 if (sec1
->lma
< sec2
->lma
)
2377 else if (sec1
->lma
> sec2
->lma
)
2380 /* Then sort by VMA. Normally the LMA and the VMA will be
2381 the same, and this will do nothing. */
2382 if (sec1
->vma
< sec2
->vma
)
2384 else if (sec1
->vma
> sec2
->vma
)
2387 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2389 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2394 return sec1
->target_index
- sec2
->target_index
;
2404 /* Sort by size, to put zero sized sections before others at the
2407 if (sec1
->_raw_size
< sec2
->_raw_size
)
2409 if (sec1
->_raw_size
> sec2
->_raw_size
)
2412 return sec1
->target_index
- sec2
->target_index
;
2415 /* Assign file positions to the sections based on the mapping from
2416 sections to segments. This function also sets up some fields in
2417 the file header, and writes out the program headers. */
2420 assign_file_positions_for_segments (abfd
)
2423 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2425 struct elf_segment_map
*m
;
2427 Elf_Internal_Phdr
*phdrs
;
2429 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2430 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2431 Elf_Internal_Phdr
*p
;
2433 if (elf_tdata (abfd
)->segment_map
== NULL
)
2435 if (! map_sections_to_segments (abfd
))
2439 if (bed
->elf_backend_modify_segment_map
)
2441 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2446 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2449 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2450 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2451 elf_elfheader (abfd
)->e_phnum
= count
;
2456 /* If we already counted the number of program segments, make sure
2457 that we allocated enough space. This happens when SIZEOF_HEADERS
2458 is used in a linker script. */
2459 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2460 if (alloc
!= 0 && count
> alloc
)
2462 ((*_bfd_error_handler
)
2463 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2464 bfd_get_filename (abfd
), alloc
, count
));
2465 bfd_set_error (bfd_error_bad_value
);
2472 phdrs
= ((Elf_Internal_Phdr
*)
2473 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
2477 off
= bed
->s
->sizeof_ehdr
;
2478 off
+= alloc
* bed
->s
->sizeof_phdr
;
2485 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2492 /* If elf_segment_map is not from map_sections_to_segments, the
2493 sections may not be correctly ordered. */
2495 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2498 p
->p_type
= m
->p_type
;
2500 if (m
->p_flags_valid
)
2501 p
->p_flags
= m
->p_flags
;
2505 if (p
->p_type
== PT_LOAD
2507 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2509 if ((abfd
->flags
& D_PAGED
) != 0)
2510 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2512 off
+= ((m
->sections
[0]->vma
- off
)
2513 % (1 << bfd_get_section_alignment (abfd
, m
->sections
[0])));
2519 p
->p_vaddr
= m
->sections
[0]->vma
;
2521 if (m
->p_paddr_valid
)
2522 p
->p_paddr
= m
->p_paddr
;
2523 else if (m
->count
== 0)
2526 p
->p_paddr
= m
->sections
[0]->lma
;
2528 if (p
->p_type
== PT_LOAD
2529 && (abfd
->flags
& D_PAGED
) != 0)
2530 p
->p_align
= bed
->maxpagesize
;
2531 else if (m
->count
== 0)
2532 p
->p_align
= bed
->s
->file_align
;
2540 if (m
->includes_filehdr
)
2542 if (! m
->p_flags_valid
)
2545 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2546 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2549 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2551 if (p
->p_vaddr
< (bfd_vma
) off
)
2553 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2554 bfd_get_filename (abfd
));
2555 bfd_set_error (bfd_error_bad_value
);
2560 if (! m
->p_paddr_valid
)
2563 if (p
->p_type
== PT_LOAD
)
2565 filehdr_vaddr
= p
->p_vaddr
;
2566 filehdr_paddr
= p
->p_paddr
;
2570 if (m
->includes_phdrs
)
2572 if (! m
->p_flags_valid
)
2575 if (m
->includes_filehdr
)
2577 if (p
->p_type
== PT_LOAD
)
2579 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2580 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2585 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2589 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2590 p
->p_vaddr
-= off
- p
->p_offset
;
2591 if (! m
->p_paddr_valid
)
2592 p
->p_paddr
-= off
- p
->p_offset
;
2595 if (p
->p_type
== PT_LOAD
)
2597 phdrs_vaddr
= p
->p_vaddr
;
2598 phdrs_paddr
= p
->p_paddr
;
2601 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
2604 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2605 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2608 if (p
->p_type
== PT_LOAD
2609 || (p
->p_type
== PT_NOTE
&& abfd
->format
== bfd_core
))
2611 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2617 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2618 p
->p_filesz
+= adjust
;
2619 p
->p_memsz
+= adjust
;
2625 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2629 bfd_size_type align
;
2633 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2635 /* The section may have artificial alignment forced by a
2636 link script. Notice this case by the gap between the
2637 cumulative phdr vma and the section's vma. */
2638 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2640 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2642 p
->p_memsz
+= adjust
;
2645 if ((flags
& SEC_LOAD
) != 0)
2646 p
->p_filesz
+= adjust
;
2649 if (p
->p_type
== PT_LOAD
)
2651 bfd_signed_vma adjust
;
2653 if ((flags
& SEC_LOAD
) != 0)
2655 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2659 else if ((flags
& SEC_ALLOC
) != 0)
2661 /* The section VMA must equal the file position
2662 modulo the page size. FIXME: I'm not sure if
2663 this adjustment is really necessary. We used to
2664 not have the SEC_LOAD case just above, and then
2665 this was necessary, but now I'm not sure. */
2666 if ((abfd
->flags
& D_PAGED
) != 0)
2667 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2669 adjust
= (sec
->vma
- voff
) % align
;
2678 (* _bfd_error_handler
)
2679 (_("Error: First section in segment (%s) starts at 0x%x"),
2680 bfd_section_name (abfd
, sec
), sec
->lma
);
2681 (* _bfd_error_handler
)
2682 (_(" whereas segment starts at 0x%x"),
2687 p
->p_memsz
+= adjust
;
2690 if ((flags
& SEC_LOAD
) != 0)
2691 p
->p_filesz
+= adjust
;
2696 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2697 used in a linker script we may have a section with
2698 SEC_LOAD clear but which is supposed to have
2700 if ((flags
& SEC_LOAD
) != 0
2701 || (flags
& SEC_HAS_CONTENTS
) != 0)
2702 off
+= sec
->_raw_size
;
2704 if ((flags
& SEC_ALLOC
) != 0)
2705 voff
+= sec
->_raw_size
;
2708 if (p
->p_type
== PT_NOTE
&& abfd
->format
== bfd_core
)
2710 if (i
== 0) /* the actual "note" segment */
2711 { /* this one actually contains everything. */
2713 p
->p_filesz
= sec
->_raw_size
;
2714 off
+= sec
->_raw_size
;
2717 else /* fake sections -- don't need to be written */
2721 flags
= sec
->flags
= 0; /* no contents */
2728 p
->p_memsz
+= sec
->_raw_size
;
2730 if ((flags
& SEC_LOAD
) != 0)
2731 p
->p_filesz
+= sec
->_raw_size
;
2733 if (align
> p
->p_align
)
2737 if (! m
->p_flags_valid
)
2740 if ((flags
& SEC_CODE
) != 0)
2742 if ((flags
& SEC_READONLY
) == 0)
2748 /* Now that we have set the section file positions, we can set up
2749 the file positions for the non PT_LOAD segments. */
2750 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2754 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
2756 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
2757 p
->p_offset
= m
->sections
[0]->filepos
;
2761 if (m
->includes_filehdr
)
2763 p
->p_vaddr
= filehdr_vaddr
;
2764 if (! m
->p_paddr_valid
)
2765 p
->p_paddr
= filehdr_paddr
;
2767 else if (m
->includes_phdrs
)
2769 p
->p_vaddr
= phdrs_vaddr
;
2770 if (! m
->p_paddr_valid
)
2771 p
->p_paddr
= phdrs_paddr
;
2776 /* Clear out any program headers we allocated but did not use. */
2777 for (; count
< alloc
; count
++, p
++)
2779 memset (p
, 0, sizeof *p
);
2780 p
->p_type
= PT_NULL
;
2783 elf_tdata (abfd
)->phdr
= phdrs
;
2785 elf_tdata (abfd
)->next_file_pos
= off
;
2787 /* Write out the program headers. */
2788 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
2789 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
2795 /* Get the size of the program header.
2797 If this is called by the linker before any of the section VMA's are set, it
2798 can't calculate the correct value for a strange memory layout. This only
2799 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2800 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2801 data segment (exclusive of .interp and .dynamic).
2803 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2804 will be two segments. */
2806 static bfd_size_type
2807 get_program_header_size (abfd
)
2812 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2814 /* We can't return a different result each time we're called. */
2815 if (elf_tdata (abfd
)->program_header_size
!= 0)
2816 return elf_tdata (abfd
)->program_header_size
;
2818 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2820 struct elf_segment_map
*m
;
2823 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2825 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
2826 return elf_tdata (abfd
)->program_header_size
;
2829 /* Assume we will need exactly two PT_LOAD segments: one for text
2830 and one for data. */
2833 s
= bfd_get_section_by_name (abfd
, ".interp");
2834 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2836 /* If we have a loadable interpreter section, we need a
2837 PT_INTERP segment. In this case, assume we also need a
2838 PT_PHDR segment, although that may not be true for all
2843 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
2845 /* We need a PT_DYNAMIC segment. */
2849 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2851 if ((s
->flags
& SEC_LOAD
) != 0
2852 && strncmp (s
->name
, ".note", 5) == 0)
2854 /* We need a PT_NOTE segment. */
2859 /* Let the backend count up any program headers it might need. */
2860 if (bed
->elf_backend_additional_program_headers
)
2864 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
2870 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
2871 return elf_tdata (abfd
)->program_header_size
;
2874 /* Work out the file positions of all the sections. This is called by
2875 _bfd_elf_compute_section_file_positions. All the section sizes and
2876 VMAs must be known before this is called.
2878 We do not consider reloc sections at this point, unless they form
2879 part of the loadable image. Reloc sections are assigned file
2880 positions in assign_file_positions_for_relocs, which is called by
2881 write_object_contents and final_link.
2883 We also don't set the positions of the .symtab and .strtab here. */
2886 assign_file_positions_except_relocs (abfd
)
2889 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
2890 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
2891 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
2893 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2895 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
2896 && abfd
->format
!= bfd_core
)
2898 Elf_Internal_Shdr
**hdrpp
;
2901 /* Start after the ELF header. */
2902 off
= i_ehdrp
->e_ehsize
;
2904 /* We are not creating an executable, which means that we are
2905 not creating a program header, and that the actual order of
2906 the sections in the file is unimportant. */
2907 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2909 Elf_Internal_Shdr
*hdr
;
2912 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
2914 hdr
->sh_offset
= -1;
2917 if (i
== tdata
->symtab_section
2918 || i
== tdata
->strtab_section
)
2920 hdr
->sh_offset
= -1;
2924 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2930 Elf_Internal_Shdr
**hdrpp
;
2932 /* Assign file positions for the loaded sections based on the
2933 assignment of sections to segments. */
2934 if (! assign_file_positions_for_segments (abfd
))
2937 /* Assign file positions for the other sections. */
2939 off
= elf_tdata (abfd
)->next_file_pos
;
2940 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2942 Elf_Internal_Shdr
*hdr
;
2945 if (hdr
->bfd_section
!= NULL
2946 && hdr
->bfd_section
->filepos
!= 0)
2947 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
2948 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2950 ((*_bfd_error_handler
)
2951 (_("%s: warning: allocated section `%s' not in segment"),
2952 bfd_get_filename (abfd
),
2953 (hdr
->bfd_section
== NULL
2955 : hdr
->bfd_section
->name
)));
2956 if ((abfd
->flags
& D_PAGED
) != 0)
2957 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
2959 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
2960 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
2963 else if (hdr
->sh_type
== SHT_REL
2964 || hdr
->sh_type
== SHT_RELA
2965 || hdr
== i_shdrpp
[tdata
->symtab_section
]
2966 || hdr
== i_shdrpp
[tdata
->strtab_section
])
2967 hdr
->sh_offset
= -1;
2969 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2973 /* Place the section headers. */
2974 off
= align_file_position (off
, bed
->s
->file_align
);
2975 i_ehdrp
->e_shoff
= off
;
2976 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
2978 elf_tdata (abfd
)->next_file_pos
= off
;
2987 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2988 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2989 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2991 struct bfd_strtab_hash
*shstrtab
;
2992 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2994 i_ehdrp
= elf_elfheader (abfd
);
2995 i_shdrp
= elf_elfsections (abfd
);
2997 shstrtab
= _bfd_elf_stringtab_init ();
2998 if (shstrtab
== NULL
)
3001 elf_shstrtab (abfd
) = shstrtab
;
3003 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3004 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3005 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3006 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3008 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3009 i_ehdrp
->e_ident
[EI_DATA
] =
3010 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3011 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3013 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3014 i_ehdrp
->e_ident
[count
] = 0;
3016 if ((abfd
->flags
& DYNAMIC
) != 0)
3017 i_ehdrp
->e_type
= ET_DYN
;
3018 else if ((abfd
->flags
& EXEC_P
) != 0)
3019 i_ehdrp
->e_type
= ET_EXEC
;
3020 else if (bfd_get_format (abfd
) == bfd_core
)
3021 i_ehdrp
->e_type
= ET_CORE
;
3023 i_ehdrp
->e_type
= ET_REL
;
3025 switch (bfd_get_arch (abfd
))
3027 case bfd_arch_unknown
:
3028 i_ehdrp
->e_machine
= EM_NONE
;
3030 case bfd_arch_sparc
:
3031 if (bed
->s
->arch_size
== 64)
3032 i_ehdrp
->e_machine
= EM_SPARCV9
;
3034 i_ehdrp
->e_machine
= EM_SPARC
;
3037 i_ehdrp
->e_machine
= EM_386
;
3040 i_ehdrp
->e_machine
= EM_68K
;
3043 i_ehdrp
->e_machine
= EM_88K
;
3046 i_ehdrp
->e_machine
= EM_860
;
3048 case bfd_arch_mips
: /* MIPS Rxxxx */
3049 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
3052 i_ehdrp
->e_machine
= EM_PARISC
;
3054 case bfd_arch_powerpc
:
3055 i_ehdrp
->e_machine
= EM_PPC
;
3057 case bfd_arch_alpha
:
3058 i_ehdrp
->e_machine
= EM_ALPHA
;
3061 i_ehdrp
->e_machine
= EM_SH
;
3064 i_ehdrp
->e_machine
= EM_CYGNUS_D10V
;
3067 i_ehdrp
->e_machine
= EM_CYGNUS_D30V
;
3070 switch (bfd_get_mach (abfd
))
3073 case 0: i_ehdrp
->e_machine
= EM_CYGNUS_V850
; break;
3077 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
3080 i_ehdrp
->e_machine
= EM_ARM
;
3083 i_ehdrp
->e_machine
= EM_CYGNUS_M32R
;
3085 case bfd_arch_mn10200
:
3086 i_ehdrp
->e_machine
= EM_CYGNUS_MN10200
;
3088 case bfd_arch_mn10300
:
3089 i_ehdrp
->e_machine
= EM_CYGNUS_MN10300
;
3091 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3093 i_ehdrp
->e_machine
= EM_NONE
;
3095 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3096 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3098 /* no program header, for now. */
3099 i_ehdrp
->e_phoff
= 0;
3100 i_ehdrp
->e_phentsize
= 0;
3101 i_ehdrp
->e_phnum
= 0;
3103 /* each bfd section is section header entry */
3104 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3105 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3107 /* if we're building an executable, we'll need a program header table */
3108 if (abfd
->flags
& EXEC_P
)
3110 /* it all happens later */
3112 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3114 /* elf_build_phdrs() returns a (NULL-terminated) array of
3115 Elf_Internal_Phdrs */
3116 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3117 i_ehdrp
->e_phoff
= outbase
;
3118 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3123 i_ehdrp
->e_phentsize
= 0;
3125 i_ehdrp
->e_phoff
= 0;
3128 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3129 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3130 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3131 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3132 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3133 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3134 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3135 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3136 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3142 /* Assign file positions for all the reloc sections which are not part
3143 of the loadable file image. */
3146 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3151 Elf_Internal_Shdr
**shdrpp
;
3153 off
= elf_tdata (abfd
)->next_file_pos
;
3155 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3156 i
< elf_elfheader (abfd
)->e_shnum
;
3159 Elf_Internal_Shdr
*shdrp
;
3162 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3163 && shdrp
->sh_offset
== -1)
3164 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3167 elf_tdata (abfd
)->next_file_pos
= off
;
3171 _bfd_elf_write_object_contents (abfd
)
3174 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3175 Elf_Internal_Ehdr
*i_ehdrp
;
3176 Elf_Internal_Shdr
**i_shdrp
;
3180 if (! abfd
->output_has_begun
3181 && ! _bfd_elf_compute_section_file_positions
3182 (abfd
, (struct bfd_link_info
*) NULL
))
3185 i_shdrp
= elf_elfsections (abfd
);
3186 i_ehdrp
= elf_elfheader (abfd
);
3189 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3193 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3195 /* After writing the headers, we need to write the sections too... */
3196 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3198 if (bed
->elf_backend_section_processing
)
3199 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3200 if (i_shdrp
[count
]->contents
)
3202 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3203 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
3205 != i_shdrp
[count
]->sh_size
))
3210 /* Write out the section header names. */
3211 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3212 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3215 if (bed
->elf_backend_final_write_processing
)
3216 (*bed
->elf_backend_final_write_processing
) (abfd
,
3217 elf_tdata (abfd
)->linker
);
3219 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3223 _bfd_elf_write_corefile_contents (abfd
)
3226 /* Hopefully this can be done just like an object file. */
3227 return _bfd_elf_write_object_contents (abfd
);
3229 /* given a section, search the header to find them... */
3231 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3235 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3236 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3238 Elf_Internal_Shdr
*hdr
;
3239 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3241 for (index
= 0; index
< maxindex
; index
++)
3243 hdr
= i_shdrp
[index
];
3244 if (hdr
->bfd_section
== asect
)
3248 if (bed
->elf_backend_section_from_bfd_section
)
3250 for (index
= 0; index
< maxindex
; index
++)
3254 hdr
= i_shdrp
[index
];
3256 if ((*bed
->elf_backend_section_from_bfd_section
)
3257 (abfd
, hdr
, asect
, &retval
))
3262 if (bfd_is_abs_section (asect
))
3264 if (bfd_is_com_section (asect
))
3266 if (bfd_is_und_section (asect
))
3269 bfd_set_error (bfd_error_nonrepresentable_section
);
3274 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3278 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3280 asymbol
**asym_ptr_ptr
;
3282 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3284 flagword flags
= asym_ptr
->flags
;
3286 /* When gas creates relocations against local labels, it creates its
3287 own symbol for the section, but does put the symbol into the
3288 symbol chain, so udata is 0. When the linker is generating
3289 relocatable output, this section symbol may be for one of the
3290 input sections rather than the output section. */
3291 if (asym_ptr
->udata
.i
== 0
3292 && (flags
& BSF_SECTION_SYM
)
3293 && asym_ptr
->section
)
3297 if (asym_ptr
->section
->output_section
!= NULL
)
3298 indx
= asym_ptr
->section
->output_section
->index
;
3300 indx
= asym_ptr
->section
->index
;
3301 if (elf_section_syms (abfd
)[indx
])
3302 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3305 idx
= asym_ptr
->udata
.i
;
3309 /* This case can occur when using --strip-symbol on a symbol
3310 which is used in a relocation entry. */
3311 (*_bfd_error_handler
)
3312 (_("%s: symbol `%s' required but not present"),
3313 bfd_get_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3314 bfd_set_error (bfd_error_no_symbols
);
3321 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3322 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3323 elf_symbol_flags (flags
));
3331 /* Copy private BFD data. This copies any program header information. */
3334 copy_private_bfd_data (ibfd
, obfd
)
3338 Elf_Internal_Ehdr
*iehdr
;
3339 struct elf_segment_map
*mfirst
;
3340 struct elf_segment_map
**pm
;
3341 struct elf_segment_map
*m
;
3342 Elf_Internal_Phdr
*p
;
3344 unsigned int num_segments
;
3345 boolean phdr_included
= false;
3347 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3348 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3351 if (elf_tdata (ibfd
)->phdr
== NULL
)
3354 iehdr
= elf_elfheader (ibfd
);
3359 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3361 #define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3362 ((addr) >= (bottom) \
3363 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3364 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3366 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3368 #define IS_COREFILE_NOTE(p, s) \
3369 (p->p_type == PT_NOTE \
3370 && ibfd->format == bfd_core \
3371 && s->vma == 0 && s->lma == 0 \
3372 && (bfd_vma) s->filepos >= p->p_offset \
3373 && (bfd_vma) s->filepos + s->_raw_size \
3374 <= p->p_offset + p->p_filesz)
3376 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3377 linker, which generates a PT_INTERP section with p_vaddr and
3378 p_memsz set to 0. */
3380 #define IS_SOLARIS_PT_INTERP(p, s) \
3382 && p->p_filesz > 0 \
3383 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3384 && s->_raw_size > 0 \
3385 && (bfd_vma) s->filepos >= p->p_offset \
3386 && ((bfd_vma) s->filepos + s->_raw_size \
3387 <= p->p_offset + p->p_filesz))
3389 /* Scan through the segments specified in the program header
3390 of the input BFD. */
3391 for (i
= 0, p
= elf_tdata (ibfd
)->phdr
; i
< num_segments
; i
++, p
++)
3395 asection
**sections
;
3398 bfd_vma matching_lma
;
3399 bfd_vma suggested_lma
;
3402 /* For each section in the input BFD, decide if it should be
3403 included in the current segment. A section will be included
3404 if it is within the address space of the segment, and it is
3405 an allocated segment, and there is an output section
3406 associated with it. */
3408 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3409 if (s
->output_section
!= NULL
)
3411 if ((IS_CONTAINED_BY (s
->vma
, s
->_raw_size
, p
->p_vaddr
, p
)
3412 || IS_SOLARIS_PT_INTERP (p
, s
))
3413 && (s
->flags
& SEC_ALLOC
) != 0)
3415 else if (IS_COREFILE_NOTE (p
, s
))
3419 /* Allocate a segment map big enough to contain all of the
3420 sections we have selected. */
3421 m
= ((struct elf_segment_map
*)
3423 (sizeof (struct elf_segment_map
)
3424 + ((size_t) csecs
- 1) * sizeof (asection
*))));
3428 /* Initialise the fields of the segment map. Default to
3429 using the physical address of the segment in the input BFD. */
3431 m
->p_type
= p
->p_type
;
3432 m
->p_flags
= p
->p_flags
;
3433 m
->p_flags_valid
= 1;
3434 m
->p_paddr
= p
->p_paddr
;
3435 m
->p_paddr_valid
= 1;
3437 /* Determine if this segment contains the ELF file header
3438 and if it contains the program headers themselves. */
3439 m
->includes_filehdr
= (p
->p_offset
== 0
3440 && p
->p_filesz
>= iehdr
->e_ehsize
);
3442 if (! phdr_included
|| p
->p_type
!= PT_LOAD
)
3445 (p
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3446 && (p
->p_offset
+ p
->p_filesz
3447 >= ((bfd_vma
) iehdr
->e_phoff
3448 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3449 if (p
->p_type
== PT_LOAD
&& m
->includes_phdrs
)
3450 phdr_included
= true;
3455 /* Special segments, such as the PT_PHDR segment, may contain
3456 no sections, but ordinary, loadable segments should contain
3459 if (p
->p_type
== PT_LOAD
)
3461 (_("%s: warning: Empty loadable segment detected\n"),
3462 bfd_get_filename (ibfd
));
3471 /* Now scan the sections in the input BFD again and attempt
3472 to add their corresponding output sections to the segment map.
3473 The problem here is how to handle an output section which has
3474 been moved (ie had its LMA changed). There are four possibilities:
3476 1. None of the sections have been moved.
3477 In this case we can continue to use the segment LMA from the
3480 2. All of the sections have been moved by the same amount.
3481 In this case we can change the segment's LMA to match the LMA
3482 of the first section.
3484 3. Some of the sections have been moved, others have not.
3485 In this case those sections which have not been moved can be
3486 placed in the current segment which will have to have its size,
3487 and possibly its LMA changed, and a new segment or segments will
3488 have to be created to contain the other sections.
3490 4. The sections have been moved, but not be the same amount.
3491 In this case we can change the segment's LMA to match the LMA
3492 of the first section and we will have to create a new segment
3493 or segments to contain the other sections.
3495 In order to save time, we allocate an array to hold the section
3496 pointers that we are interested in. As these sections get assigned
3497 to a segment, they are removed from this array. */
3499 sections
= (asection
**) bfd_malloc (sizeof (asection
*) * csecs
);
3500 if (sections
== NULL
)
3503 /* Step One: Scan for segment vs section LMA conflicts.
3504 Also add the sections to the section array allocated above.
3505 Also add the sections to the current segment. In the common
3506 case, where the sections have not been moved, this means that
3507 we have completely filled the segment, and there is nothing
3511 matching_lma
= false;
3514 for (j
= 0, s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3516 os
= s
->output_section
;
3518 if ((((IS_CONTAINED_BY (s
->vma
, s
->_raw_size
, p
->p_vaddr
, p
)
3519 || IS_SOLARIS_PT_INTERP (p
, s
))
3520 && (s
->flags
& SEC_ALLOC
) != 0)
3521 || IS_COREFILE_NOTE (p
, s
))
3526 /* The Solaris native linker always sets p_paddr to 0.
3527 We try to catch that case here, and set it to the
3533 && (os
->vma
== (p
->p_vaddr
3534 + (m
->includes_filehdr
3537 + (m
->includes_phdrs
3538 ? iehdr
->e_phnum
* iehdr
->e_phentsize
3540 m
->p_paddr
= p
->p_vaddr
;
3542 /* Match up the physical address of the segment with the
3543 LMA address of the output section. */
3544 if (IS_CONTAINED_BY (os
->lma
, os
->_raw_size
, m
->p_paddr
, p
)
3545 || IS_COREFILE_NOTE (p
, s
))
3547 if (matching_lma
== 0)
3548 matching_lma
= os
->lma
;
3550 /* We assume that if the section fits within the segment
3551 that it does not overlap any other section within that
3553 m
->sections
[isec
++] = os
;
3555 else if (suggested_lma
== 0)
3556 suggested_lma
= os
->lma
;
3560 BFD_ASSERT (j
== csecs
);
3562 /* Step Two: Adjust the physical address of the current segment,
3566 /* All of the sections fitted within the segment as currently
3567 specified. This is the default case. Add the segment to
3568 the list of built segments and carry on to process the next
3569 program header in the input BFD. */
3577 else if (matching_lma
!= 0)
3579 /* At least one section fits inside the current segment.
3580 Keep it, but modify its physical address to match the
3581 LMA of the first section that fitted. */
3583 m
->p_paddr
= matching_lma
;
3587 /* None of the sections fitted inside the current segment.
3588 Change the current segment's physical address to match
3589 the LMA of the first section. */
3591 m
->p_paddr
= suggested_lma
;
3594 /* Step Three: Loop over the sections again, this time assigning
3595 those that fit to the current segment and remvoing them from the
3596 sections array; but making sure not to leave large gaps. Once all
3597 possible sections have been assigned to the current segment it is
3598 added to the list of built segments and if sections still remain
3599 to be assigned, a new segment is constructed before repeating
3607 /* Fill the current segment with sections that fit. */
3608 for (j
= 0; j
< csecs
; j
++)
3615 os
= s
->output_section
;
3617 if (IS_CONTAINED_BY (os
->lma
, os
->_raw_size
, m
->p_paddr
, p
)
3618 || IS_COREFILE_NOTE (p
, s
))
3622 /* If the first section in a segment does not start at
3623 the beginning of the segment, then something is wrong. */
3624 if (os
->lma
!= m
->p_paddr
)
3629 asection
* prev_sec
;
3630 bfd_vma maxpagesize
;
3632 prev_sec
= m
->sections
[m
->count
- 1];
3633 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3635 /* If the gap between the end of the previous section
3636 and the start of this section is more than maxpagesize
3637 then we need to start a new segment. */
3638 if (BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
3639 < BFD_ALIGN (os
->lma
, maxpagesize
))
3641 if (suggested_lma
== 0)
3642 suggested_lma
= os
->lma
;
3648 m
->sections
[m
->count
++] = os
;
3652 else if (suggested_lma
== 0)
3653 suggested_lma
= os
->lma
;
3656 BFD_ASSERT (m
->count
> 0);
3658 /* Add the current segment to the list of built segments. */
3664 /* We still have not allocated all of the sections to
3665 segments. Create a new segment here, initialise it
3666 and carry on looping. */
3668 m
= ((struct elf_segment_map
*)
3670 (sizeof (struct elf_segment_map
)
3671 + ((size_t) csecs
- 1) * sizeof (asection
*))));
3675 /* Initialise the fields of the segment map. Set the physical
3676 physical address to the LMA of the first section that has
3677 not yet been assigned. */
3680 m
->p_type
= p
->p_type
;
3681 m
->p_flags
= p
->p_flags
;
3682 m
->p_flags_valid
= 1;
3683 m
->p_paddr
= suggested_lma
;
3684 m
->p_paddr_valid
= 1;
3685 m
->includes_filehdr
= 0;
3686 m
->includes_phdrs
= 0;
3689 while (isec
< csecs
);
3694 /* The Solaris linker creates program headers in which all the
3695 p_paddr fields are zero. When we try to objcopy or strip such a
3696 file, we get confused. Check for this case, and if we find it
3697 reset the p_paddr_valid fields. */
3698 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3699 if (m
->p_paddr
!= 0)
3703 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3704 m
->p_paddr_valid
= 0;
3707 elf_tdata (obfd
)->segment_map
= mfirst
;
3710 /* Final Step: Sort the segments into ascending order of physical address. */
3713 struct elf_segment_map
* prev
;
3716 for (m
= mfirst
->next
; m
!= NULL
; prev
= m
, m
= m
->next
)
3718 /* Yes I know - its a bubble sort....*/
3719 if (m
->next
!= NULL
&& (m
->next
->p_paddr
< m
->p_paddr
))
3721 /* swap m and m->next */
3722 prev
->next
= m
->next
;
3723 m
->next
= m
->next
->next
;
3724 prev
->next
->next
= m
;
3733 #undef IS_CONTAINED_BY
3734 #undef IS_SOLARIS_PT_INTERP
3735 #undef IS_COREFILE_NOTE
3739 /* Copy private section information. This copies over the entsize
3740 field, and sometimes the info field. */
3743 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
3749 Elf_Internal_Shdr
*ihdr
, *ohdr
;
3751 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
3752 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
3755 /* Copy over private BFD data if it has not already been copied.
3756 This must be done here, rather than in the copy_private_bfd_data
3757 entry point, because the latter is called after the section
3758 contents have been set, which means that the program headers have
3759 already been worked out. */
3760 if (elf_tdata (obfd
)->segment_map
== NULL
3761 && elf_tdata (ibfd
)->phdr
!= NULL
)
3765 /* Only set up the segments if there are no more SEC_ALLOC
3766 sections. FIXME: This won't do the right thing if objcopy is
3767 used to remove the last SEC_ALLOC section, since objcopy
3768 won't call this routine in that case. */
3769 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
3770 if ((s
->flags
& SEC_ALLOC
) != 0)
3774 if (! copy_private_bfd_data (ibfd
, obfd
))
3779 ihdr
= &elf_section_data (isec
)->this_hdr
;
3780 ohdr
= &elf_section_data (osec
)->this_hdr
;
3782 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
3784 if (ihdr
->sh_type
== SHT_SYMTAB
3785 || ihdr
->sh_type
== SHT_DYNSYM
3786 || ihdr
->sh_type
== SHT_GNU_verneed
3787 || ihdr
->sh_type
== SHT_GNU_verdef
)
3788 ohdr
->sh_info
= ihdr
->sh_info
;
3793 /* Copy private symbol information. If this symbol is in a section
3794 which we did not map into a BFD section, try to map the section
3795 index correctly. We use special macro definitions for the mapped
3796 section indices; these definitions are interpreted by the
3797 swap_out_syms function. */
3799 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3800 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3801 #define MAP_STRTAB (SHN_LORESERVE - 3)
3802 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3805 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
3811 elf_symbol_type
*isym
, *osym
;
3813 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3814 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3817 isym
= elf_symbol_from (ibfd
, isymarg
);
3818 osym
= elf_symbol_from (obfd
, osymarg
);
3822 && bfd_is_abs_section (isym
->symbol
.section
))
3826 shndx
= isym
->internal_elf_sym
.st_shndx
;
3827 if (shndx
== elf_onesymtab (ibfd
))
3828 shndx
= MAP_ONESYMTAB
;
3829 else if (shndx
== elf_dynsymtab (ibfd
))
3830 shndx
= MAP_DYNSYMTAB
;
3831 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
3833 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
3834 shndx
= MAP_SHSTRTAB
;
3835 osym
->internal_elf_sym
.st_shndx
= shndx
;
3841 /* Swap out the symbols. */
3844 swap_out_syms (abfd
, sttp
, relocatable_p
)
3846 struct bfd_strtab_hash
**sttp
;
3849 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3851 if (!elf_map_symbols (abfd
))
3854 /* Dump out the symtabs. */
3856 int symcount
= bfd_get_symcount (abfd
);
3857 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3858 struct bfd_strtab_hash
*stt
;
3859 Elf_Internal_Shdr
*symtab_hdr
;
3860 Elf_Internal_Shdr
*symstrtab_hdr
;
3861 char *outbound_syms
;
3864 stt
= _bfd_elf_stringtab_init ();
3868 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3869 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3870 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3871 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
3872 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
3873 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
3875 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3876 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3878 outbound_syms
= bfd_alloc (abfd
,
3879 (1 + symcount
) * bed
->s
->sizeof_sym
);
3880 if (outbound_syms
== NULL
)
3882 symtab_hdr
->contents
= (PTR
) outbound_syms
;
3884 /* now generate the data (for "contents") */
3886 /* Fill in zeroth symbol and swap it out. */
3887 Elf_Internal_Sym sym
;
3893 sym
.st_shndx
= SHN_UNDEF
;
3894 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
3895 outbound_syms
+= bed
->s
->sizeof_sym
;
3897 for (idx
= 0; idx
< symcount
; idx
++)
3899 Elf_Internal_Sym sym
;
3900 bfd_vma value
= syms
[idx
]->value
;
3901 elf_symbol_type
*type_ptr
;
3902 flagword flags
= syms
[idx
]->flags
;
3905 if (flags
& BSF_SECTION_SYM
)
3906 /* Section symbols have no names. */
3910 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
3913 if (sym
.st_name
== (unsigned long) -1)
3917 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
3919 if (bfd_is_com_section (syms
[idx
]->section
))
3921 /* ELF common symbols put the alignment into the `value' field,
3922 and the size into the `size' field. This is backwards from
3923 how BFD handles it, so reverse it here. */
3924 sym
.st_size
= value
;
3925 if (type_ptr
== NULL
3926 || type_ptr
->internal_elf_sym
.st_value
== 0)
3927 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
3929 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
3930 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
3931 (abfd
, syms
[idx
]->section
);
3935 asection
*sec
= syms
[idx
]->section
;
3938 if (sec
->output_section
)
3940 value
+= sec
->output_offset
;
3941 sec
= sec
->output_section
;
3943 /* Don't add in the section vma for relocatable output. */
3944 if (! relocatable_p
)
3946 sym
.st_value
= value
;
3947 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
3949 if (bfd_is_abs_section (sec
)
3951 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
3953 /* This symbol is in a real ELF section which we did
3954 not create as a BFD section. Undo the mapping done
3955 by copy_private_symbol_data. */
3956 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
3960 shndx
= elf_onesymtab (abfd
);
3963 shndx
= elf_dynsymtab (abfd
);
3966 shndx
= elf_tdata (abfd
)->strtab_section
;
3969 shndx
= elf_tdata (abfd
)->shstrtab_section
;
3977 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3983 /* Writing this would be a hell of a lot easier if
3984 we had some decent documentation on bfd, and
3985 knew what to expect of the library, and what to
3986 demand of applications. For example, it
3987 appears that `objcopy' might not set the
3988 section of a symbol to be a section that is
3989 actually in the output file. */
3990 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
3991 BFD_ASSERT (sec2
!= 0);
3992 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
3993 BFD_ASSERT (shndx
!= -1);
3997 sym
.st_shndx
= shndx
;
4000 if ((flags
& BSF_FUNCTION
) != 0)
4002 else if ((flags
& BSF_OBJECT
) != 0)
4007 /* Processor-specific types */
4008 if (bed
->elf_backend_get_symbol_type
)
4009 type
= (*bed
->elf_backend_get_symbol_type
) (&type_ptr
->internal_elf_sym
);
4011 if (bfd_is_com_section (syms
[idx
]->section
))
4012 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4013 else if (bfd_is_und_section (syms
[idx
]->section
))
4014 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4018 else if (flags
& BSF_SECTION_SYM
)
4019 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4020 else if (flags
& BSF_FILE
)
4021 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4024 int bind
= STB_LOCAL
;
4026 if (flags
& BSF_LOCAL
)
4028 else if (flags
& BSF_WEAK
)
4030 else if (flags
& BSF_GLOBAL
)
4033 sym
.st_info
= ELF_ST_INFO (bind
, type
);
4036 if (type_ptr
!= NULL
)
4037 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
4041 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4042 outbound_syms
+= bed
->s
->sizeof_sym
;
4046 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
4047 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4049 symstrtab_hdr
->sh_flags
= 0;
4050 symstrtab_hdr
->sh_addr
= 0;
4051 symstrtab_hdr
->sh_entsize
= 0;
4052 symstrtab_hdr
->sh_link
= 0;
4053 symstrtab_hdr
->sh_info
= 0;
4054 symstrtab_hdr
->sh_addralign
= 1;
4060 /* Return the number of bytes required to hold the symtab vector.
4062 Note that we base it on the count plus 1, since we will null terminate
4063 the vector allocated based on this size. However, the ELF symbol table
4064 always has a dummy entry as symbol #0, so it ends up even. */
4067 _bfd_elf_get_symtab_upper_bound (abfd
)
4072 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4074 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4075 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4081 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4086 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4088 if (elf_dynsymtab (abfd
) == 0)
4090 bfd_set_error (bfd_error_invalid_operation
);
4094 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4095 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4101 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4105 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4108 /* Canonicalize the relocs. */
4111 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4120 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
4126 tblptr
= section
->relocation
;
4127 for (i
= 0; i
< section
->reloc_count
; i
++)
4128 *relptr
++ = tblptr
++;
4132 return section
->reloc_count
;
4136 _bfd_elf_get_symtab (abfd
, alocation
)
4138 asymbol
**alocation
;
4140 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4141 (abfd
, alocation
, false);
4144 bfd_get_symcount (abfd
) = symcount
;
4149 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4151 asymbol
**alocation
;
4153 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4154 (abfd
, alocation
, true);
4157 /* Return the size required for the dynamic reloc entries. Any
4158 section that was actually installed in the BFD, and has type
4159 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4160 considered to be a dynamic reloc section. */
4163 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4169 if (elf_dynsymtab (abfd
) == 0)
4171 bfd_set_error (bfd_error_invalid_operation
);
4175 ret
= sizeof (arelent
*);
4176 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4177 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4178 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4179 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4180 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4181 * sizeof (arelent
*));
4186 /* Canonicalize the dynamic relocation entries. Note that we return
4187 the dynamic relocations as a single block, although they are
4188 actually associated with particular sections; the interface, which
4189 was designed for SunOS style shared libraries, expects that there
4190 is only one set of dynamic relocs. Any section that was actually
4191 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4192 the dynamic symbol table, is considered to be a dynamic reloc
4196 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4201 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4205 if (elf_dynsymtab (abfd
) == 0)
4207 bfd_set_error (bfd_error_invalid_operation
);
4211 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4213 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4215 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4216 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4217 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4222 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4224 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4226 for (i
= 0; i
< count
; i
++)
4237 /* Read in the version information. */
4240 _bfd_elf_slurp_version_tables (abfd
)
4243 bfd_byte
*contents
= NULL
;
4245 if (elf_dynverdef (abfd
) != 0)
4247 Elf_Internal_Shdr
*hdr
;
4248 Elf_External_Verdef
*everdef
;
4249 Elf_Internal_Verdef
*iverdef
;
4252 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
4254 elf_tdata (abfd
)->verdef
=
4255 ((Elf_Internal_Verdef
*)
4256 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verdef
)));
4257 if (elf_tdata (abfd
)->verdef
== NULL
)
4260 elf_tdata (abfd
)->cverdefs
= hdr
->sh_info
;
4262 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4263 if (contents
== NULL
)
4265 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4266 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4269 everdef
= (Elf_External_Verdef
*) contents
;
4270 iverdef
= elf_tdata (abfd
)->verdef
;
4271 for (i
= 0; i
< hdr
->sh_info
; i
++, iverdef
++)
4273 Elf_External_Verdaux
*everdaux
;
4274 Elf_Internal_Verdaux
*iverdaux
;
4277 _bfd_elf_swap_verdef_in (abfd
, everdef
, iverdef
);
4279 iverdef
->vd_bfd
= abfd
;
4281 iverdef
->vd_auxptr
= ((Elf_Internal_Verdaux
*)
4284 * sizeof (Elf_Internal_Verdaux
))));
4285 if (iverdef
->vd_auxptr
== NULL
)
4288 everdaux
= ((Elf_External_Verdaux
*)
4289 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
4290 iverdaux
= iverdef
->vd_auxptr
;
4291 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
4293 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
4295 iverdaux
->vda_nodename
=
4296 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4297 iverdaux
->vda_name
);
4298 if (iverdaux
->vda_nodename
== NULL
)
4301 if (j
+ 1 < iverdef
->vd_cnt
)
4302 iverdaux
->vda_nextptr
= iverdaux
+ 1;
4304 iverdaux
->vda_nextptr
= NULL
;
4306 everdaux
= ((Elf_External_Verdaux
*)
4307 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
4310 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
4312 if (i
+ 1 < hdr
->sh_info
)
4313 iverdef
->vd_nextdef
= iverdef
+ 1;
4315 iverdef
->vd_nextdef
= NULL
;
4317 everdef
= ((Elf_External_Verdef
*)
4318 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
4325 if (elf_dynverref (abfd
) != 0)
4327 Elf_Internal_Shdr
*hdr
;
4328 Elf_External_Verneed
*everneed
;
4329 Elf_Internal_Verneed
*iverneed
;
4332 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
4334 elf_tdata (abfd
)->verref
=
4335 ((Elf_Internal_Verneed
*)
4336 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verneed
)));
4337 if (elf_tdata (abfd
)->verref
== NULL
)
4340 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
4342 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4343 if (contents
== NULL
)
4345 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4346 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4349 everneed
= (Elf_External_Verneed
*) contents
;
4350 iverneed
= elf_tdata (abfd
)->verref
;
4351 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4353 Elf_External_Vernaux
*evernaux
;
4354 Elf_Internal_Vernaux
*ivernaux
;
4357 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4359 iverneed
->vn_bfd
= abfd
;
4361 iverneed
->vn_filename
=
4362 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4364 if (iverneed
->vn_filename
== NULL
)
4367 iverneed
->vn_auxptr
=
4368 ((Elf_Internal_Vernaux
*)
4370 iverneed
->vn_cnt
* sizeof (Elf_Internal_Vernaux
)));
4372 evernaux
= ((Elf_External_Vernaux
*)
4373 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4374 ivernaux
= iverneed
->vn_auxptr
;
4375 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4377 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4379 ivernaux
->vna_nodename
=
4380 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4381 ivernaux
->vna_name
);
4382 if (ivernaux
->vna_nodename
== NULL
)
4385 if (j
+ 1 < iverneed
->vn_cnt
)
4386 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4388 ivernaux
->vna_nextptr
= NULL
;
4390 evernaux
= ((Elf_External_Vernaux
*)
4391 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4394 if (i
+ 1 < hdr
->sh_info
)
4395 iverneed
->vn_nextref
= iverneed
+ 1;
4397 iverneed
->vn_nextref
= NULL
;
4399 everneed
= ((Elf_External_Verneed
*)
4400 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4410 if (contents
== NULL
)
4416 _bfd_elf_make_empty_symbol (abfd
)
4419 elf_symbol_type
*newsym
;
4421 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
4426 newsym
->symbol
.the_bfd
= abfd
;
4427 return &newsym
->symbol
;
4432 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4437 bfd_symbol_info (symbol
, ret
);
4440 /* Return whether a symbol name implies a local symbol. Most targets
4441 use this function for the is_local_label_name entry point, but some
4445 _bfd_elf_is_local_label_name (abfd
, name
)
4449 /* Normal local symbols start with ``.L''. */
4450 if (name
[0] == '.' && name
[1] == 'L')
4453 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4454 DWARF debugging symbols starting with ``..''. */
4455 if (name
[0] == '.' && name
[1] == '.')
4458 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4459 emitting DWARF debugging output. I suspect this is actually a
4460 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4461 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4462 underscore to be emitted on some ELF targets). For ease of use,
4463 we treat such symbols as local. */
4464 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
4471 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
4480 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
4482 enum bfd_architecture arch
;
4483 unsigned long machine
;
4485 /* If this isn't the right architecture for this backend, and this
4486 isn't the generic backend, fail. */
4487 if (arch
!= get_elf_backend_data (abfd
)->arch
4488 && arch
!= bfd_arch_unknown
4489 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
4492 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
4495 /* Find the nearest line to a particular section and offset, for error
4499 _bfd_elf_find_nearest_line (abfd
,
4510 CONST
char **filename_ptr
;
4511 CONST
char **functionname_ptr
;
4512 unsigned int *line_ptr
;
4515 const char *filename
;
4520 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
4521 filename_ptr
, functionname_ptr
,
4525 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
4526 &found
, filename_ptr
,
4527 functionname_ptr
, line_ptr
,
4528 &elf_tdata (abfd
)->line_info
))
4533 if (symbols
== NULL
)
4540 for (p
= symbols
; *p
!= NULL
; p
++)
4544 q
= (elf_symbol_type
*) *p
;
4546 if (bfd_get_section (&q
->symbol
) != section
)
4549 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
4554 filename
= bfd_asymbol_name (&q
->symbol
);
4558 if (q
->symbol
.section
== section
4559 && q
->symbol
.value
>= low_func
4560 && q
->symbol
.value
<= offset
)
4562 func
= (asymbol
*) q
;
4563 low_func
= q
->symbol
.value
;
4572 *filename_ptr
= filename
;
4573 *functionname_ptr
= bfd_asymbol_name (func
);
4579 _bfd_elf_sizeof_headers (abfd
, reloc
)
4585 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
4587 ret
+= get_program_header_size (abfd
);
4592 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
4597 bfd_size_type count
;
4599 Elf_Internal_Shdr
*hdr
;
4601 if (! abfd
->output_has_begun
4602 && ! _bfd_elf_compute_section_file_positions
4603 (abfd
, (struct bfd_link_info
*) NULL
))
4606 hdr
= &elf_section_data (section
)->this_hdr
;
4608 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
4610 if (bfd_write (location
, 1, count
, abfd
) != count
)
4617 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
4620 Elf_Internal_Rela
*dst
;
4627 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
4630 Elf_Internal_Rel
*dst
;
4636 /* Try to convert a non-ELF reloc into an ELF one. */
4639 _bfd_elf_validate_reloc (abfd
, areloc
)
4643 /* Check whether we really have an ELF howto. */
4645 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
4647 bfd_reloc_code_real_type code
;
4648 reloc_howto_type
*howto
;
4650 /* Alien reloc: Try to determine its type to replace it with an
4651 equivalent ELF reloc. */
4653 if (areloc
->howto
->pc_relative
)
4655 switch (areloc
->howto
->bitsize
)
4658 code
= BFD_RELOC_8_PCREL
;
4661 code
= BFD_RELOC_12_PCREL
;
4664 code
= BFD_RELOC_16_PCREL
;
4667 code
= BFD_RELOC_24_PCREL
;
4670 code
= BFD_RELOC_32_PCREL
;
4673 code
= BFD_RELOC_64_PCREL
;
4679 howto
= bfd_reloc_type_lookup (abfd
, code
);
4681 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
4683 if (howto
->pcrel_offset
)
4684 areloc
->addend
+= areloc
->address
;
4686 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
4691 switch (areloc
->howto
->bitsize
)
4697 code
= BFD_RELOC_14
;
4700 code
= BFD_RELOC_16
;
4703 code
= BFD_RELOC_26
;
4706 code
= BFD_RELOC_32
;
4709 code
= BFD_RELOC_64
;
4715 howto
= bfd_reloc_type_lookup (abfd
, code
);
4719 areloc
->howto
= howto
;
4727 (*_bfd_error_handler
)
4728 (_("%s: unsupported relocation type %s"),
4729 bfd_get_filename (abfd
), areloc
->howto
->name
);
4730 bfd_set_error (bfd_error_bad_value
);
4735 _bfd_elf_close_and_cleanup (abfd
)
4738 if (bfd_get_format (abfd
) == bfd_object
)
4740 if (elf_shstrtab (abfd
) != NULL
)
4741 _bfd_stringtab_free (elf_shstrtab (abfd
));
4744 return _bfd_generic_close_and_cleanup (abfd
);
4747 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
4748 in the relocation's offset. Thus we cannot allow any sort of sanity
4749 range-checking to interfere. There is nothing else to do in processing
4752 bfd_reloc_status_type
4753 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
4756 struct symbol_cache_entry
*symbol
;
4762 return bfd_reloc_ok
;