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_get_str_section (abfd
, shindex
)
268 unsigned int shindex
;
270 Elf_Internal_Shdr
**i_shdrp
;
271 char *shstrtab
= NULL
;
273 unsigned int shstrtabsize
;
275 i_shdrp
= elf_elfsections (abfd
);
276 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
279 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
280 if (shstrtab
== NULL
)
282 /* No cached one, attempt to read, and cache what we read. */
283 offset
= i_shdrp
[shindex
]->sh_offset
;
284 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
285 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
286 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
292 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
294 unsigned int shindex
;
295 unsigned int strindex
;
297 Elf_Internal_Shdr
*hdr
;
302 hdr
= elf_elfsections (abfd
)[shindex
];
304 if (hdr
->contents
== NULL
305 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
308 if (strindex
>= hdr
->sh_size
)
310 (*_bfd_error_handler
)
311 (_("%s: invalid string offset %u >= %lu for section `%s'"),
312 bfd_get_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
313 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
314 && strindex
== hdr
->sh_name
)
316 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
320 return ((char *) hdr
->contents
) + strindex
;
323 /* Make a BFD section from an ELF section. We store a pointer to the
324 BFD section in the bfd_section field of the header. */
327 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
329 Elf_Internal_Shdr
*hdr
;
335 if (hdr
->bfd_section
!= NULL
)
337 BFD_ASSERT (strcmp (name
,
338 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
342 newsect
= bfd_make_section_anyway (abfd
, name
);
346 newsect
->filepos
= hdr
->sh_offset
;
348 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
349 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
350 || ! bfd_set_section_alignment (abfd
, newsect
,
351 bfd_log2 (hdr
->sh_addralign
)))
354 flags
= SEC_NO_FLAGS
;
355 if (hdr
->sh_type
!= SHT_NOBITS
)
356 flags
|= SEC_HAS_CONTENTS
;
357 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
360 if (hdr
->sh_type
!= SHT_NOBITS
)
363 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
364 flags
|= SEC_READONLY
;
365 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
367 else if ((flags
& SEC_LOAD
) != 0)
370 /* The debugging sections appear to be recognized only by name, not
372 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
373 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
374 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
375 flags
|= SEC_DEBUGGING
;
377 /* As a GNU extension, if the name begins with .gnu.linkonce, we
378 only link a single copy of the section. This is used to support
379 g++. g++ will emit each template expansion in its own section.
380 The symbols will be defined as weak, so that multiple definitions
381 are permitted. The GNU linker extension is to actually discard
382 all but one of the sections. */
383 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
384 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
386 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
389 if ((flags
& SEC_ALLOC
) != 0)
391 Elf_Internal_Phdr
*phdr
;
394 /* Look through the phdrs to see if we need to adjust the lma. */
395 phdr
= elf_tdata (abfd
)->phdr
;
396 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
398 if (phdr
->p_type
== PT_LOAD
399 && phdr
->p_paddr
!= 0
400 && phdr
->p_vaddr
!= phdr
->p_paddr
401 && phdr
->p_vaddr
<= hdr
->sh_addr
402 && phdr
->p_vaddr
+ phdr
->p_memsz
>= hdr
->sh_addr
+ hdr
->sh_size
403 && ((flags
& SEC_LOAD
) == 0
404 || (phdr
->p_offset
<= (bfd_vma
) hdr
->sh_offset
405 && (phdr
->p_offset
+ phdr
->p_filesz
406 >= hdr
->sh_offset
+ hdr
->sh_size
))))
408 newsect
->lma
+= phdr
->p_paddr
- phdr
->p_vaddr
;
414 hdr
->bfd_section
= newsect
;
415 elf_section_data (newsect
)->this_hdr
= *hdr
;
425 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
428 Helper functions for GDB to locate the string tables.
429 Since BFD hides string tables from callers, GDB needs to use an
430 internal hook to find them. Sun's .stabstr, in particular,
431 isn't even pointed to by the .stab section, so ordinary
432 mechanisms wouldn't work to find it, even if we had some.
435 struct elf_internal_shdr
*
436 bfd_elf_find_section (abfd
, name
)
440 Elf_Internal_Shdr
**i_shdrp
;
445 i_shdrp
= elf_elfsections (abfd
);
448 shstrtab
= bfd_elf_get_str_section
449 (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
450 if (shstrtab
!= NULL
)
452 max
= elf_elfheader (abfd
)->e_shnum
;
453 for (i
= 1; i
< max
; i
++)
454 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
461 const char *const bfd_elf_section_type_names
[] = {
462 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
463 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
464 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
467 /* ELF relocs are against symbols. If we are producing relocateable
468 output, and the reloc is against an external symbol, and nothing
469 has given us any additional addend, the resulting reloc will also
470 be against the same symbol. In such a case, we don't want to
471 change anything about the way the reloc is handled, since it will
472 all be done at final link time. Rather than put special case code
473 into bfd_perform_relocation, all the reloc types use this howto
474 function. It just short circuits the reloc if producing
475 relocateable output against an external symbol. */
478 bfd_reloc_status_type
479 bfd_elf_generic_reloc (abfd
,
487 arelent
*reloc_entry
;
490 asection
*input_section
;
492 char **error_message
;
494 if (output_bfd
!= (bfd
*) NULL
495 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
496 && (! reloc_entry
->howto
->partial_inplace
497 || reloc_entry
->addend
== 0))
499 reloc_entry
->address
+= input_section
->output_offset
;
503 return bfd_reloc_continue
;
506 /* Print out the program headers. */
509 _bfd_elf_print_private_bfd_data (abfd
, farg
)
513 FILE *f
= (FILE *) farg
;
514 Elf_Internal_Phdr
*p
;
516 bfd_byte
*dynbuf
= NULL
;
518 p
= elf_tdata (abfd
)->phdr
;
523 fprintf (f
, _("\nProgram Header:\n"));
524 c
= elf_elfheader (abfd
)->e_phnum
;
525 for (i
= 0; i
< c
; i
++, p
++)
532 case PT_NULL
: s
= "NULL"; break;
533 case PT_LOAD
: s
= "LOAD"; break;
534 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
535 case PT_INTERP
: s
= "INTERP"; break;
536 case PT_NOTE
: s
= "NOTE"; break;
537 case PT_SHLIB
: s
= "SHLIB"; break;
538 case PT_PHDR
: s
= "PHDR"; break;
539 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
541 fprintf (f
, "%8s off 0x", s
);
542 fprintf_vma (f
, p
->p_offset
);
543 fprintf (f
, " vaddr 0x");
544 fprintf_vma (f
, p
->p_vaddr
);
545 fprintf (f
, " paddr 0x");
546 fprintf_vma (f
, p
->p_paddr
);
547 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
548 fprintf (f
, " filesz 0x");
549 fprintf_vma (f
, p
->p_filesz
);
550 fprintf (f
, " memsz 0x");
551 fprintf_vma (f
, p
->p_memsz
);
552 fprintf (f
, " flags %c%c%c",
553 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
554 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
555 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
556 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
557 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
562 s
= bfd_get_section_by_name (abfd
, ".dynamic");
567 bfd_byte
*extdyn
, *extdynend
;
569 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
571 fprintf (f
, _("\nDynamic Section:\n"));
573 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
576 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
580 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
583 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
585 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
586 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
589 extdynend
= extdyn
+ s
->_raw_size
;
590 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
592 Elf_Internal_Dyn dyn
;
597 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
599 if (dyn
.d_tag
== DT_NULL
)
606 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
610 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
611 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
612 case DT_PLTGOT
: name
= "PLTGOT"; break;
613 case DT_HASH
: name
= "HASH"; break;
614 case DT_STRTAB
: name
= "STRTAB"; break;
615 case DT_SYMTAB
: name
= "SYMTAB"; break;
616 case DT_RELA
: name
= "RELA"; break;
617 case DT_RELASZ
: name
= "RELASZ"; break;
618 case DT_RELAENT
: name
= "RELAENT"; break;
619 case DT_STRSZ
: name
= "STRSZ"; break;
620 case DT_SYMENT
: name
= "SYMENT"; break;
621 case DT_INIT
: name
= "INIT"; break;
622 case DT_FINI
: name
= "FINI"; break;
623 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
624 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
625 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
626 case DT_REL
: name
= "REL"; break;
627 case DT_RELSZ
: name
= "RELSZ"; break;
628 case DT_RELENT
: name
= "RELENT"; break;
629 case DT_PLTREL
: name
= "PLTREL"; break;
630 case DT_DEBUG
: name
= "DEBUG"; break;
631 case DT_TEXTREL
: name
= "TEXTREL"; break;
632 case DT_JMPREL
: name
= "JMPREL"; break;
633 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
634 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
635 case DT_VERSYM
: name
= "VERSYM"; break;
636 case DT_VERDEF
: name
= "VERDEF"; break;
637 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
638 case DT_VERNEED
: name
= "VERNEED"; break;
639 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
642 fprintf (f
, " %-11s ", name
);
644 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
649 string
= bfd_elf_string_from_elf_section (abfd
, link
,
653 fprintf (f
, "%s", string
);
662 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
663 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
665 if (! _bfd_elf_slurp_version_tables (abfd
))
669 if (elf_dynverdef (abfd
) != 0)
671 Elf_Internal_Verdef
*t
;
673 fprintf (f
, _("\nVersion definitions:\n"));
674 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
676 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
677 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
678 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
680 Elf_Internal_Verdaux
*a
;
683 for (a
= t
->vd_auxptr
->vda_nextptr
;
686 fprintf (f
, "%s ", a
->vda_nodename
);
692 if (elf_dynverref (abfd
) != 0)
694 Elf_Internal_Verneed
*t
;
696 fprintf (f
, _("\nVersion References:\n"));
697 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
699 Elf_Internal_Vernaux
*a
;
701 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
702 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
703 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
704 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
716 /* Display ELF-specific fields of a symbol. */
719 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
723 bfd_print_symbol_type how
;
725 FILE *file
= (FILE *) filep
;
728 case bfd_print_symbol_name
:
729 fprintf (file
, "%s", symbol
->name
);
731 case bfd_print_symbol_more
:
732 fprintf (file
, "elf ");
733 fprintf_vma (file
, symbol
->value
);
734 fprintf (file
, " %lx", (long) symbol
->flags
);
736 case bfd_print_symbol_all
:
738 CONST
char *section_name
;
739 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
740 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
741 fprintf (file
, " %s\t", section_name
);
742 /* Print the "other" value for a symbol. For common symbols,
743 we've already printed the size; now print the alignment.
744 For other symbols, we have no specified alignment, and
745 we've printed the address; now print the size. */
747 (bfd_is_com_section (symbol
->section
)
748 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
749 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
751 /* If we have version information, print it. */
752 if (elf_tdata (abfd
)->dynversym_section
!= 0
753 && (elf_tdata (abfd
)->dynverdef_section
!= 0
754 || elf_tdata (abfd
)->dynverref_section
!= 0))
757 const char *version_string
;
759 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
763 else if (vernum
== 1)
764 version_string
= "Base";
765 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
767 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
770 Elf_Internal_Verneed
*t
;
773 for (t
= elf_tdata (abfd
)->verref
;
777 Elf_Internal_Vernaux
*a
;
779 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
781 if (a
->vna_other
== vernum
)
783 version_string
= a
->vna_nodename
;
790 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
791 fprintf (file
, " %-11s", version_string
);
796 fprintf (file
, " (%s)", version_string
);
797 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
802 /* If the st_other field is not zero, print it. */
803 if (((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
!= 0)
804 fprintf (file
, " 0x%02x",
806 ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
));
808 fprintf (file
, " %s", symbol
->name
);
814 /* Create an entry in an ELF linker hash table. */
816 struct bfd_hash_entry
*
817 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
818 struct bfd_hash_entry
*entry
;
819 struct bfd_hash_table
*table
;
822 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
824 /* Allocate the structure if it has not already been allocated by a
826 if (ret
== (struct elf_link_hash_entry
*) NULL
)
827 ret
= ((struct elf_link_hash_entry
*)
828 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
829 if (ret
== (struct elf_link_hash_entry
*) NULL
)
830 return (struct bfd_hash_entry
*) ret
;
832 /* Call the allocation method of the superclass. */
833 ret
= ((struct elf_link_hash_entry
*)
834 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
836 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
838 /* Set local fields. */
842 ret
->dynstr_index
= 0;
844 ret
->got
.offset
= (bfd_vma
) -1;
845 ret
->plt
.offset
= (bfd_vma
) -1;
846 ret
->linker_section_pointer
= (elf_linker_section_pointers_t
*)0;
847 ret
->verinfo
.verdef
= NULL
;
848 ret
->vtable_entries_used
= NULL
;
849 ret
->vtable_parent
= NULL
;
850 ret
->type
= STT_NOTYPE
;
852 /* Assume that we have been called by a non-ELF symbol reader.
853 This flag is then reset by the code which reads an ELF input
854 file. This ensures that a symbol created by a non-ELF symbol
855 reader will have the flag set correctly. */
856 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
859 return (struct bfd_hash_entry
*) ret
;
862 /* Initialize an ELF linker hash table. */
865 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
866 struct elf_link_hash_table
*table
;
868 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
869 struct bfd_hash_table
*,
872 table
->dynamic_sections_created
= false;
873 table
->dynobj
= NULL
;
874 /* The first dynamic symbol is a dummy. */
875 table
->dynsymcount
= 1;
876 table
->dynstr
= NULL
;
877 table
->bucketcount
= 0;
878 table
->needed
= NULL
;
880 table
->stab_info
= NULL
;
881 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
884 /* Create an ELF linker hash table. */
886 struct bfd_link_hash_table
*
887 _bfd_elf_link_hash_table_create (abfd
)
890 struct elf_link_hash_table
*ret
;
892 ret
= ((struct elf_link_hash_table
*)
893 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
894 if (ret
== (struct elf_link_hash_table
*) NULL
)
897 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
899 bfd_release (abfd
, ret
);
906 /* This is a hook for the ELF emulation code in the generic linker to
907 tell the backend linker what file name to use for the DT_NEEDED
908 entry for a dynamic object. The generic linker passes name as an
909 empty string to indicate that no DT_NEEDED entry should be made. */
912 bfd_elf_set_dt_needed_name (abfd
, name
)
916 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
917 && bfd_get_format (abfd
) == bfd_object
)
918 elf_dt_name (abfd
) = name
;
921 /* Get the list of DT_NEEDED entries for a link. This is a hook for
922 the linker ELF emulation code. */
924 struct bfd_link_needed_list
*
925 bfd_elf_get_needed_list (abfd
, info
)
927 struct bfd_link_info
*info
;
929 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
931 return elf_hash_table (info
)->needed
;
934 /* Get the name actually used for a dynamic object for a link. This
935 is the SONAME entry if there is one. Otherwise, it is the string
936 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
939 bfd_elf_get_dt_soname (abfd
)
942 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
943 && bfd_get_format (abfd
) == bfd_object
)
944 return elf_dt_name (abfd
);
948 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
949 the ELF linker emulation code. */
952 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
954 struct bfd_link_needed_list
**pneeded
;
957 bfd_byte
*dynbuf
= NULL
;
960 bfd_byte
*extdyn
, *extdynend
;
962 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
966 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
967 || bfd_get_format (abfd
) != bfd_object
)
970 s
= bfd_get_section_by_name (abfd
, ".dynamic");
971 if (s
== NULL
|| s
->_raw_size
== 0)
974 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
978 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
982 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
986 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
988 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
989 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
992 extdynend
= extdyn
+ s
->_raw_size
;
993 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
995 Elf_Internal_Dyn dyn
;
997 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
999 if (dyn
.d_tag
== DT_NULL
)
1002 if (dyn
.d_tag
== DT_NEEDED
)
1005 struct bfd_link_needed_list
*l
;
1007 string
= bfd_elf_string_from_elf_section (abfd
, link
,
1012 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, sizeof *l
);
1033 /* Allocate an ELF string table--force the first byte to be zero. */
1035 struct bfd_strtab_hash
*
1036 _bfd_elf_stringtab_init ()
1038 struct bfd_strtab_hash
*ret
;
1040 ret
= _bfd_stringtab_init ();
1045 loc
= _bfd_stringtab_add (ret
, "", true, false);
1046 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1047 if (loc
== (bfd_size_type
) -1)
1049 _bfd_stringtab_free (ret
);
1056 /* ELF .o/exec file reading */
1058 /* Create a new bfd section from an ELF section header. */
1061 bfd_section_from_shdr (abfd
, shindex
)
1063 unsigned int shindex
;
1065 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1066 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1067 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1070 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1072 switch (hdr
->sh_type
)
1075 /* Inactive section. Throw it away. */
1078 case SHT_PROGBITS
: /* Normal section with contents. */
1079 case SHT_DYNAMIC
: /* Dynamic linking information. */
1080 case SHT_NOBITS
: /* .bss section. */
1081 case SHT_HASH
: /* .hash section. */
1082 case SHT_NOTE
: /* .note section. */
1083 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1085 case SHT_SYMTAB
: /* A symbol table */
1086 if (elf_onesymtab (abfd
) == shindex
)
1089 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1090 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1091 elf_onesymtab (abfd
) = shindex
;
1092 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1093 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1094 abfd
->flags
|= HAS_SYMS
;
1096 /* Sometimes a shared object will map in the symbol table. If
1097 SHF_ALLOC is set, and this is a shared object, then we also
1098 treat this section as a BFD section. We can not base the
1099 decision purely on SHF_ALLOC, because that flag is sometimes
1100 set in a relocateable object file, which would confuse the
1102 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1103 && (abfd
->flags
& DYNAMIC
) != 0
1104 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1109 case SHT_DYNSYM
: /* A dynamic symbol table */
1110 if (elf_dynsymtab (abfd
) == shindex
)
1113 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1114 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1115 elf_dynsymtab (abfd
) = shindex
;
1116 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1117 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1118 abfd
->flags
|= HAS_SYMS
;
1120 /* Besides being a symbol table, we also treat this as a regular
1121 section, so that objcopy can handle it. */
1122 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1124 case SHT_STRTAB
: /* A string table */
1125 if (hdr
->bfd_section
!= NULL
)
1127 if (ehdr
->e_shstrndx
== shindex
)
1129 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1130 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1136 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1138 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1139 if (hdr2
->sh_link
== shindex
)
1141 if (! bfd_section_from_shdr (abfd
, i
))
1143 if (elf_onesymtab (abfd
) == i
)
1145 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1146 elf_elfsections (abfd
)[shindex
] =
1147 &elf_tdata (abfd
)->strtab_hdr
;
1150 if (elf_dynsymtab (abfd
) == i
)
1152 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1153 elf_elfsections (abfd
)[shindex
] = hdr
=
1154 &elf_tdata (abfd
)->dynstrtab_hdr
;
1155 /* We also treat this as a regular section, so
1156 that objcopy can handle it. */
1159 #if 0 /* Not handling other string tables specially right now. */
1160 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1161 /* We have a strtab for some random other section. */
1162 newsect
= (asection
*) hdr2
->bfd_section
;
1165 hdr
->bfd_section
= newsect
;
1166 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1168 elf_elfsections (abfd
)[shindex
] = hdr2
;
1174 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1178 /* *These* do a lot of work -- but build no sections! */
1180 asection
*target_sect
;
1181 Elf_Internal_Shdr
*hdr2
;
1183 /* For some incomprehensible reason Oracle distributes
1184 libraries for Solaris in which some of the objects have
1185 bogus sh_link fields. It would be nice if we could just
1186 reject them, but, unfortunately, some people need to use
1187 them. We scan through the section headers; if we find only
1188 one suitable symbol table, we clobber the sh_link to point
1189 to it. I hope this doesn't break anything. */
1190 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1191 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1197 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1199 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1200 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1211 hdr
->sh_link
= found
;
1214 /* Get the symbol table. */
1215 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1216 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1219 /* If this reloc section does not use the main symbol table we
1220 don't treat it as a reloc section. BFD can't adequately
1221 represent such a section, so at least for now, we don't
1222 try. We just present it as a normal section. */
1223 if (hdr
->sh_link
!= elf_onesymtab (abfd
))
1224 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1226 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1228 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1229 if (target_sect
== NULL
)
1232 if ((target_sect
->flags
& SEC_RELOC
) == 0
1233 || target_sect
->reloc_count
== 0)
1234 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1237 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1238 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
1239 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1242 elf_elfsections (abfd
)[shindex
] = hdr2
;
1243 target_sect
->reloc_count
+= hdr
->sh_size
/ hdr
->sh_entsize
;
1244 target_sect
->flags
|= SEC_RELOC
;
1245 target_sect
->relocation
= NULL
;
1246 target_sect
->rel_filepos
= hdr
->sh_offset
;
1247 abfd
->flags
|= HAS_RELOC
;
1252 case SHT_GNU_verdef
:
1253 elf_dynverdef (abfd
) = shindex
;
1254 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1255 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1258 case SHT_GNU_versym
:
1259 elf_dynversym (abfd
) = shindex
;
1260 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1261 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1264 case SHT_GNU_verneed
:
1265 elf_dynverref (abfd
) = shindex
;
1266 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1267 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1274 /* Check for any processor-specific section types. */
1276 if (bed
->elf_backend_section_from_shdr
)
1277 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1285 /* Given an ELF section number, retrieve the corresponding BFD
1289 bfd_section_from_elf_index (abfd
, index
)
1293 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1294 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1296 return elf_elfsections (abfd
)[index
]->bfd_section
;
1300 _bfd_elf_new_section_hook (abfd
, sec
)
1304 struct bfd_elf_section_data
*sdata
;
1306 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
1309 sec
->used_by_bfd
= (PTR
) sdata
;
1310 memset (sdata
, 0, sizeof (*sdata
));
1314 /* Create a new bfd section from an ELF program header.
1316 Since program segments have no names, we generate a synthetic name
1317 of the form segment<NUM>, where NUM is generally the index in the
1318 program header table. For segments that are split (see below) we
1319 generate the names segment<NUM>a and segment<NUM>b.
1321 Note that some program segments may have a file size that is different than
1322 (less than) the memory size. All this means is that at execution the
1323 system must allocate the amount of memory specified by the memory size,
1324 but only initialize it with the first "file size" bytes read from the
1325 file. This would occur for example, with program segments consisting
1326 of combined data+bss.
1328 To handle the above situation, this routine generates TWO bfd sections
1329 for the single program segment. The first has the length specified by
1330 the file size of the segment, and the second has the length specified
1331 by the difference between the two sizes. In effect, the segment is split
1332 into it's initialized and uninitialized parts.
1337 bfd_section_from_phdr (abfd
, hdr
, index
)
1339 Elf_Internal_Phdr
*hdr
;
1347 split
= ((hdr
->p_memsz
> 0) &&
1348 (hdr
->p_filesz
> 0) &&
1349 (hdr
->p_memsz
> hdr
->p_filesz
));
1350 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
1351 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1354 strcpy (name
, namebuf
);
1355 newsect
= bfd_make_section (abfd
, name
);
1356 if (newsect
== NULL
)
1358 newsect
->vma
= hdr
->p_vaddr
;
1359 newsect
->lma
= hdr
->p_paddr
;
1360 newsect
->_raw_size
= hdr
->p_filesz
;
1361 newsect
->filepos
= hdr
->p_offset
;
1362 newsect
->flags
|= SEC_HAS_CONTENTS
;
1363 if (hdr
->p_type
== PT_LOAD
)
1365 newsect
->flags
|= SEC_ALLOC
;
1366 newsect
->flags
|= SEC_LOAD
;
1367 if (hdr
->p_flags
& PF_X
)
1369 /* FIXME: all we known is that it has execute PERMISSION,
1371 newsect
->flags
|= SEC_CODE
;
1374 if (!(hdr
->p_flags
& PF_W
))
1376 newsect
->flags
|= SEC_READONLY
;
1381 sprintf (namebuf
, "segment%db", index
);
1382 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1385 strcpy (name
, namebuf
);
1386 newsect
= bfd_make_section (abfd
, name
);
1387 if (newsect
== NULL
)
1389 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1390 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1391 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1392 if (hdr
->p_type
== PT_LOAD
)
1394 newsect
->flags
|= SEC_ALLOC
;
1395 if (hdr
->p_flags
& PF_X
)
1396 newsect
->flags
|= SEC_CODE
;
1398 if (!(hdr
->p_flags
& PF_W
))
1399 newsect
->flags
|= SEC_READONLY
;
1405 /* Set up an ELF internal section header for a section. */
1409 elf_fake_sections (abfd
, asect
, failedptrarg
)
1414 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1415 boolean
*failedptr
= (boolean
*) failedptrarg
;
1416 Elf_Internal_Shdr
*this_hdr
;
1420 /* We already failed; just get out of the bfd_map_over_sections
1425 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1427 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1430 if (this_hdr
->sh_name
== (unsigned long) -1)
1436 this_hdr
->sh_flags
= 0;
1438 if ((asect
->flags
& SEC_ALLOC
) != 0
1439 || asect
->user_set_vma
)
1440 this_hdr
->sh_addr
= asect
->vma
;
1442 this_hdr
->sh_addr
= 0;
1444 this_hdr
->sh_offset
= 0;
1445 this_hdr
->sh_size
= asect
->_raw_size
;
1446 this_hdr
->sh_link
= 0;
1447 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1448 /* The sh_entsize and sh_info fields may have been set already by
1449 copy_private_section_data. */
1451 this_hdr
->bfd_section
= asect
;
1452 this_hdr
->contents
= NULL
;
1454 /* FIXME: This should not be based on section names. */
1455 if (strcmp (asect
->name
, ".dynstr") == 0)
1456 this_hdr
->sh_type
= SHT_STRTAB
;
1457 else if (strcmp (asect
->name
, ".hash") == 0)
1459 this_hdr
->sh_type
= SHT_HASH
;
1460 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
1462 else if (strcmp (asect
->name
, ".dynsym") == 0)
1464 this_hdr
->sh_type
= SHT_DYNSYM
;
1465 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1467 else if (strcmp (asect
->name
, ".dynamic") == 0)
1469 this_hdr
->sh_type
= SHT_DYNAMIC
;
1470 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1472 else if (strncmp (asect
->name
, ".rela", 5) == 0
1473 && get_elf_backend_data (abfd
)->use_rela_p
)
1475 this_hdr
->sh_type
= SHT_RELA
;
1476 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1478 else if (strncmp (asect
->name
, ".rel", 4) == 0
1479 && ! get_elf_backend_data (abfd
)->use_rela_p
)
1481 this_hdr
->sh_type
= SHT_REL
;
1482 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1484 else if (strncmp (asect
->name
, ".note", 5) == 0)
1485 this_hdr
->sh_type
= SHT_NOTE
;
1486 else if (strncmp (asect
->name
, ".stab", 5) == 0
1487 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1488 this_hdr
->sh_type
= SHT_STRTAB
;
1489 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1491 this_hdr
->sh_type
= SHT_GNU_versym
;
1492 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1494 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1496 this_hdr
->sh_type
= SHT_GNU_verdef
;
1497 this_hdr
->sh_entsize
= 0;
1498 /* objcopy or strip will copy over sh_info, but may not set
1499 cverdefs. The linker will set cverdefs, but sh_info will be
1501 if (this_hdr
->sh_info
== 0)
1502 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1504 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1505 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1507 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1509 this_hdr
->sh_type
= SHT_GNU_verneed
;
1510 this_hdr
->sh_entsize
= 0;
1511 /* objcopy or strip will copy over sh_info, but may not set
1512 cverrefs. The linker will set cverrefs, but sh_info will be
1514 if (this_hdr
->sh_info
== 0)
1515 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1517 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1518 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1520 else if ((asect
->flags
& SEC_ALLOC
) != 0
1521 && (asect
->flags
& SEC_LOAD
) != 0)
1522 this_hdr
->sh_type
= SHT_PROGBITS
;
1523 else if ((asect
->flags
& SEC_ALLOC
) != 0
1524 && ((asect
->flags
& SEC_LOAD
) == 0))
1525 this_hdr
->sh_type
= SHT_NOBITS
;
1529 this_hdr
->sh_type
= SHT_PROGBITS
;
1532 if ((asect
->flags
& SEC_ALLOC
) != 0)
1533 this_hdr
->sh_flags
|= SHF_ALLOC
;
1534 if ((asect
->flags
& SEC_READONLY
) == 0)
1535 this_hdr
->sh_flags
|= SHF_WRITE
;
1536 if ((asect
->flags
& SEC_CODE
) != 0)
1537 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1539 /* Check for processor-specific section types. */
1541 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1543 if (bed
->elf_backend_fake_sections
)
1544 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1547 /* If the section has relocs, set up a section header for the
1548 SHT_REL[A] section. */
1549 if ((asect
->flags
& SEC_RELOC
) != 0)
1551 Elf_Internal_Shdr
*rela_hdr
;
1552 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1555 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1556 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1562 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1564 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1566 if (rela_hdr
->sh_name
== (unsigned int) -1)
1571 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1572 rela_hdr
->sh_entsize
= (use_rela_p
1573 ? bed
->s
->sizeof_rela
1574 : bed
->s
->sizeof_rel
);
1575 rela_hdr
->sh_addralign
= bed
->s
->file_align
;
1576 rela_hdr
->sh_flags
= 0;
1577 rela_hdr
->sh_addr
= 0;
1578 rela_hdr
->sh_size
= 0;
1579 rela_hdr
->sh_offset
= 0;
1583 /* Assign all ELF section numbers. The dummy first section is handled here
1584 too. The link/info pointers for the standard section types are filled
1585 in here too, while we're at it. */
1588 assign_section_numbers (abfd
)
1591 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1593 unsigned int section_number
;
1594 Elf_Internal_Shdr
**i_shdrp
;
1595 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1599 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1601 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1603 d
->this_idx
= section_number
++;
1604 if ((sec
->flags
& SEC_RELOC
) == 0)
1607 d
->rel_idx
= section_number
++;
1610 t
->shstrtab_section
= section_number
++;
1611 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1612 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1614 if (abfd
->symcount
> 0)
1616 t
->symtab_section
= section_number
++;
1617 t
->strtab_section
= section_number
++;
1620 elf_elfheader (abfd
)->e_shnum
= section_number
;
1622 /* Set up the list of section header pointers, in agreement with the
1624 i_shdrp
= ((Elf_Internal_Shdr
**)
1625 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1626 if (i_shdrp
== NULL
)
1629 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1630 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1631 if (i_shdrp
[0] == NULL
)
1633 bfd_release (abfd
, i_shdrp
);
1636 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1638 elf_elfsections (abfd
) = i_shdrp
;
1640 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1641 if (abfd
->symcount
> 0)
1643 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1644 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1645 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1647 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1649 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1653 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1654 if (d
->rel_idx
!= 0)
1655 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1657 /* Fill in the sh_link and sh_info fields while we're at it. */
1659 /* sh_link of a reloc section is the section index of the symbol
1660 table. sh_info is the section index of the section to which
1661 the relocation entries apply. */
1662 if (d
->rel_idx
!= 0)
1664 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1665 d
->rel_hdr
.sh_info
= d
->this_idx
;
1668 switch (d
->this_hdr
.sh_type
)
1672 /* A reloc section which we are treating as a normal BFD
1673 section. sh_link is the section index of the symbol
1674 table. sh_info is the section index of the section to
1675 which the relocation entries apply. We assume that an
1676 allocated reloc section uses the dynamic symbol table.
1677 FIXME: How can we be sure? */
1678 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1680 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1682 /* We look up the section the relocs apply to by name. */
1684 if (d
->this_hdr
.sh_type
== SHT_REL
)
1688 s
= bfd_get_section_by_name (abfd
, name
);
1690 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1694 /* We assume that a section named .stab*str is a stabs
1695 string section. We look for a section with the same name
1696 but without the trailing ``str'', and set its sh_link
1697 field to point to this section. */
1698 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1699 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1704 len
= strlen (sec
->name
);
1705 alc
= (char *) bfd_malloc (len
- 2);
1708 strncpy (alc
, sec
->name
, len
- 3);
1709 alc
[len
- 3] = '\0';
1710 s
= bfd_get_section_by_name (abfd
, alc
);
1714 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1716 /* This is a .stab section. */
1717 elf_section_data (s
)->this_hdr
.sh_entsize
=
1718 4 + 2 * (bed
->s
->arch_size
/ 8);
1725 case SHT_GNU_verneed
:
1726 case SHT_GNU_verdef
:
1727 /* sh_link is the section header index of the string table
1728 used for the dynamic entries, or the symbol table, or the
1730 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1732 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1736 case SHT_GNU_versym
:
1737 /* sh_link is the section header index of the symbol table
1738 this hash table or version table is for. */
1739 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1741 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1749 /* Map symbol from it's internal number to the external number, moving
1750 all local symbols to be at the head of the list. */
1753 sym_is_global (abfd
, sym
)
1757 /* If the backend has a special mapping, use it. */
1758 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1759 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1762 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1763 || bfd_is_und_section (bfd_get_section (sym
))
1764 || bfd_is_com_section (bfd_get_section (sym
)));
1768 elf_map_symbols (abfd
)
1771 int symcount
= bfd_get_symcount (abfd
);
1772 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1773 asymbol
**sect_syms
;
1775 int num_globals
= 0;
1776 int num_locals2
= 0;
1777 int num_globals2
= 0;
1779 int num_sections
= 0;
1785 fprintf (stderr
, "elf_map_symbols\n");
1789 /* Add a section symbol for each BFD section. FIXME: Is this really
1791 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1793 if (max_index
< asect
->index
)
1794 max_index
= asect
->index
;
1798 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1799 if (sect_syms
== NULL
)
1801 elf_section_syms (abfd
) = sect_syms
;
1803 for (idx
= 0; idx
< symcount
; idx
++)
1805 if ((syms
[idx
]->flags
& BSF_SECTION_SYM
) != 0
1806 && syms
[idx
]->value
== 0)
1810 sec
= syms
[idx
]->section
;
1811 if (sec
->owner
!= NULL
)
1813 if (sec
->owner
!= abfd
)
1815 if (sec
->output_offset
!= 0)
1817 sec
= sec
->output_section
;
1818 BFD_ASSERT (sec
->owner
== abfd
);
1820 sect_syms
[sec
->index
] = syms
[idx
];
1825 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1829 if (sect_syms
[asect
->index
] != NULL
)
1832 sym
= bfd_make_empty_symbol (abfd
);
1835 sym
->the_bfd
= abfd
;
1836 sym
->name
= asect
->name
;
1838 /* Set the flags to 0 to indicate that this one was newly added. */
1840 sym
->section
= asect
;
1841 sect_syms
[asect
->index
] = sym
;
1845 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
1846 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1850 /* Classify all of the symbols. */
1851 for (idx
= 0; idx
< symcount
; idx
++)
1853 if (!sym_is_global (abfd
, syms
[idx
]))
1858 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1860 if (sect_syms
[asect
->index
] != NULL
1861 && sect_syms
[asect
->index
]->flags
== 0)
1863 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
1864 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
1868 sect_syms
[asect
->index
]->flags
= 0;
1872 /* Now sort the symbols so the local symbols are first. */
1873 new_syms
= ((asymbol
**)
1875 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
1876 if (new_syms
== NULL
)
1879 for (idx
= 0; idx
< symcount
; idx
++)
1881 asymbol
*sym
= syms
[idx
];
1884 if (!sym_is_global (abfd
, sym
))
1887 i
= num_locals
+ num_globals2
++;
1889 sym
->udata
.i
= i
+ 1;
1891 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1893 if (sect_syms
[asect
->index
] != NULL
1894 && sect_syms
[asect
->index
]->flags
== 0)
1896 asymbol
*sym
= sect_syms
[asect
->index
];
1899 sym
->flags
= BSF_SECTION_SYM
;
1900 if (!sym_is_global (abfd
, sym
))
1903 i
= num_locals
+ num_globals2
++;
1905 sym
->udata
.i
= i
+ 1;
1909 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
1911 elf_num_locals (abfd
) = num_locals
;
1912 elf_num_globals (abfd
) = num_globals
;
1916 /* Align to the maximum file alignment that could be required for any
1917 ELF data structure. */
1919 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
1920 static INLINE file_ptr
1921 align_file_position (off
, align
)
1925 return (off
+ align
- 1) & ~(align
- 1);
1928 /* Assign a file position to a section, optionally aligning to the
1929 required section alignment. */
1932 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
1933 Elf_Internal_Shdr
*i_shdrp
;
1941 al
= i_shdrp
->sh_addralign
;
1943 offset
= BFD_ALIGN (offset
, al
);
1945 i_shdrp
->sh_offset
= offset
;
1946 if (i_shdrp
->bfd_section
!= NULL
)
1947 i_shdrp
->bfd_section
->filepos
= offset
;
1948 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1949 offset
+= i_shdrp
->sh_size
;
1953 /* Compute the file positions we are going to put the sections at, and
1954 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1955 is not NULL, this is being called by the ELF backend linker. */
1958 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
1960 struct bfd_link_info
*link_info
;
1962 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1964 struct bfd_strtab_hash
*strtab
;
1965 Elf_Internal_Shdr
*shstrtab_hdr
;
1967 if (abfd
->output_has_begun
)
1970 /* Do any elf backend specific processing first. */
1971 if (bed
->elf_backend_begin_write_processing
)
1972 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
1974 if (! prep_headers (abfd
))
1978 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
1982 if (!assign_section_numbers (abfd
))
1985 /* The backend linker builds symbol table information itself. */
1986 if (link_info
== NULL
&& abfd
->symcount
> 0)
1988 /* Non-zero if doing a relocatable link. */
1989 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
1991 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
1995 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
1996 /* sh_name was set in prep_headers. */
1997 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
1998 shstrtab_hdr
->sh_flags
= 0;
1999 shstrtab_hdr
->sh_addr
= 0;
2000 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2001 shstrtab_hdr
->sh_entsize
= 0;
2002 shstrtab_hdr
->sh_link
= 0;
2003 shstrtab_hdr
->sh_info
= 0;
2004 /* sh_offset is set in assign_file_positions_except_relocs. */
2005 shstrtab_hdr
->sh_addralign
= 1;
2007 if (!assign_file_positions_except_relocs (abfd
))
2010 if (link_info
== NULL
&& abfd
->symcount
> 0)
2013 Elf_Internal_Shdr
*hdr
;
2015 off
= elf_tdata (abfd
)->next_file_pos
;
2017 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2018 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2020 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2021 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2023 elf_tdata (abfd
)->next_file_pos
= off
;
2025 /* Now that we know where the .strtab section goes, write it
2027 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2028 || ! _bfd_stringtab_emit (abfd
, strtab
))
2030 _bfd_stringtab_free (strtab
);
2033 abfd
->output_has_begun
= true;
2038 /* Create a mapping from a set of sections to a program segment. */
2040 static INLINE
struct elf_segment_map
*
2041 make_mapping (abfd
, sections
, from
, to
, phdr
)
2043 asection
**sections
;
2048 struct elf_segment_map
*m
;
2052 m
= ((struct elf_segment_map
*)
2054 (sizeof (struct elf_segment_map
)
2055 + (to
- from
- 1) * sizeof (asection
*))));
2059 m
->p_type
= PT_LOAD
;
2060 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2061 m
->sections
[i
- from
] = *hdrpp
;
2062 m
->count
= to
- from
;
2064 if (from
== 0 && phdr
)
2066 /* Include the headers in the first PT_LOAD segment. */
2067 m
->includes_filehdr
= 1;
2068 m
->includes_phdrs
= 1;
2074 /* Set up a mapping from BFD sections to program segments. */
2077 map_sections_to_segments (abfd
)
2080 asection
**sections
= NULL
;
2084 struct elf_segment_map
*mfirst
;
2085 struct elf_segment_map
**pm
;
2086 struct elf_segment_map
*m
;
2088 unsigned int phdr_index
;
2089 bfd_vma maxpagesize
;
2091 boolean phdr_in_segment
= true;
2095 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2098 if (bfd_count_sections (abfd
) == 0)
2101 /* Select the allocated sections, and sort them. */
2103 sections
= (asection
**) bfd_malloc (bfd_count_sections (abfd
)
2104 * sizeof (asection
*));
2105 if (sections
== NULL
)
2109 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2111 if ((s
->flags
& SEC_ALLOC
) != 0)
2117 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2120 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2122 /* Build the mapping. */
2127 /* If we have a .interp section, then create a PT_PHDR segment for
2128 the program headers and a PT_INTERP segment for the .interp
2130 s
= bfd_get_section_by_name (abfd
, ".interp");
2131 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2133 m
= ((struct elf_segment_map
*)
2134 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2138 m
->p_type
= PT_PHDR
;
2139 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2140 m
->p_flags
= PF_R
| PF_X
;
2141 m
->p_flags_valid
= 1;
2142 m
->includes_phdrs
= 1;
2147 m
= ((struct elf_segment_map
*)
2148 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2152 m
->p_type
= PT_INTERP
;
2160 /* Look through the sections. We put sections in the same program
2161 segment when the start of the second section can be placed within
2162 a few bytes of the end of the first section. */
2165 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2167 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2169 && (dynsec
->flags
& SEC_LOAD
) == 0)
2172 /* Deal with -Ttext or something similar such that the first section
2173 is not adjacent to the program headers. This is an
2174 approximation, since at this point we don't know exactly how many
2175 program headers we will need. */
2178 bfd_size_type phdr_size
;
2180 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2182 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2183 if ((abfd
->flags
& D_PAGED
) == 0
2184 || sections
[0]->lma
< phdr_size
2185 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2186 phdr_in_segment
= false;
2189 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2192 boolean new_segment
;
2196 /* See if this section and the last one will fit in the same
2199 if (last_hdr
== NULL
)
2201 /* If we don't have a segment yet, then we don't need a new
2202 one (we build the last one after this loop). */
2203 new_segment
= false;
2205 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2207 /* If this section has a different relation between the
2208 virtual address and the load address, then we need a new
2212 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2213 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2215 /* If putting this section in this segment would force us to
2216 skip a page in the segment, then we need a new segment. */
2219 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2220 && (hdr
->flags
& SEC_LOAD
) != 0)
2222 /* We don't want to put a loadable section after a
2223 nonloadable section in the same segment. */
2226 else if ((abfd
->flags
& D_PAGED
) == 0)
2228 /* If the file is not demand paged, which means that we
2229 don't require the sections to be correctly aligned in the
2230 file, then there is no other reason for a new segment. */
2231 new_segment
= false;
2234 && (hdr
->flags
& SEC_READONLY
) == 0
2235 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2238 /* We don't want to put a writable section in a read only
2239 segment, unless they are on the same page in memory
2240 anyhow. We already know that the last section does not
2241 bring us past the current section on the page, so the
2242 only case in which the new section is not on the same
2243 page as the previous section is when the previous section
2244 ends precisely on a page boundary. */
2249 /* Otherwise, we can use the same segment. */
2250 new_segment
= false;
2255 if ((hdr
->flags
& SEC_READONLY
) == 0)
2261 /* We need a new program segment. We must create a new program
2262 header holding all the sections from phdr_index until hdr. */
2264 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2271 if ((hdr
->flags
& SEC_READONLY
) == 0)
2278 phdr_in_segment
= false;
2281 /* Create a final PT_LOAD program segment. */
2282 if (last_hdr
!= NULL
)
2284 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2292 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2295 m
= ((struct elf_segment_map
*)
2296 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2300 m
->p_type
= PT_DYNAMIC
;
2302 m
->sections
[0] = dynsec
;
2308 /* For each loadable .note section, add a PT_NOTE segment. We don't
2309 use bfd_get_section_by_name, because if we link together
2310 nonloadable .note sections and loadable .note sections, we will
2311 generate two .note sections in the output file. FIXME: Using
2312 names for section types is bogus anyhow. */
2313 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2315 if ((s
->flags
& SEC_LOAD
) != 0
2316 && strncmp (s
->name
, ".note", 5) == 0)
2318 m
= ((struct elf_segment_map
*)
2319 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2323 m
->p_type
= PT_NOTE
;
2335 elf_tdata (abfd
)->segment_map
= mfirst
;
2339 if (sections
!= NULL
)
2344 /* Sort sections by address. */
2347 elf_sort_sections (arg1
, arg2
)
2351 const asection
*sec1
= *(const asection
**) arg1
;
2352 const asection
*sec2
= *(const asection
**) arg2
;
2354 /* Sort by LMA first, since this is the address used to
2355 place the section into a segment. */
2356 if (sec1
->lma
< sec2
->lma
)
2358 else if (sec1
->lma
> sec2
->lma
)
2361 /* Then sort by VMA. Normally the LMA and the VMA will be
2362 the same, and this will do nothing. */
2363 if (sec1
->vma
< sec2
->vma
)
2365 else if (sec1
->vma
> sec2
->vma
)
2368 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2370 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2375 return sec1
->target_index
- sec2
->target_index
;
2385 /* Sort by size, to put zero sized sections before others at the
2388 if (sec1
->_raw_size
< sec2
->_raw_size
)
2390 if (sec1
->_raw_size
> sec2
->_raw_size
)
2393 return sec1
->target_index
- sec2
->target_index
;
2396 /* Assign file positions to the sections based on the mapping from
2397 sections to segments. This function also sets up some fields in
2398 the file header, and writes out the program headers. */
2401 assign_file_positions_for_segments (abfd
)
2404 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2406 struct elf_segment_map
*m
;
2408 Elf_Internal_Phdr
*phdrs
;
2410 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2411 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2412 Elf_Internal_Phdr
*p
;
2414 if (elf_tdata (abfd
)->segment_map
== NULL
)
2416 if (! map_sections_to_segments (abfd
))
2420 if (bed
->elf_backend_modify_segment_map
)
2422 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2427 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2430 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2431 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2432 elf_elfheader (abfd
)->e_phnum
= count
;
2437 /* If we already counted the number of program segments, make sure
2438 that we allocated enough space. This happens when SIZEOF_HEADERS
2439 is used in a linker script. */
2440 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2441 if (alloc
!= 0 && count
> alloc
)
2443 ((*_bfd_error_handler
)
2444 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2445 bfd_get_filename (abfd
), alloc
, count
));
2446 bfd_set_error (bfd_error_bad_value
);
2453 phdrs
= ((Elf_Internal_Phdr
*)
2454 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
2458 off
= bed
->s
->sizeof_ehdr
;
2459 off
+= alloc
* bed
->s
->sizeof_phdr
;
2466 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2473 /* If elf_segment_map is not from map_sections_to_segments, the
2474 sections may not be correctly ordered. */
2476 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2479 p
->p_type
= m
->p_type
;
2481 if (m
->p_flags_valid
)
2482 p
->p_flags
= m
->p_flags
;
2486 if (p
->p_type
== PT_LOAD
2488 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2490 if ((abfd
->flags
& D_PAGED
) != 0)
2491 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2493 off
+= ((m
->sections
[0]->vma
- off
)
2494 % (1 << bfd_get_section_alignment (abfd
, m
->sections
[0])));
2500 p
->p_vaddr
= m
->sections
[0]->vma
;
2502 if (m
->p_paddr_valid
)
2503 p
->p_paddr
= m
->p_paddr
;
2504 else if (m
->count
== 0)
2507 p
->p_paddr
= m
->sections
[0]->lma
;
2509 if (p
->p_type
== PT_LOAD
2510 && (abfd
->flags
& D_PAGED
) != 0)
2511 p
->p_align
= bed
->maxpagesize
;
2512 else if (m
->count
== 0)
2513 p
->p_align
= bed
->s
->file_align
;
2521 if (m
->includes_filehdr
)
2523 if (! m
->p_flags_valid
)
2526 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2527 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2530 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2532 if (p
->p_vaddr
< (bfd_vma
) off
)
2534 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2535 bfd_get_filename (abfd
));
2536 bfd_set_error (bfd_error_bad_value
);
2541 if (! m
->p_paddr_valid
)
2544 if (p
->p_type
== PT_LOAD
)
2546 filehdr_vaddr
= p
->p_vaddr
;
2547 filehdr_paddr
= p
->p_paddr
;
2551 if (m
->includes_phdrs
)
2553 if (! m
->p_flags_valid
)
2556 if (m
->includes_filehdr
)
2558 if (p
->p_type
== PT_LOAD
)
2560 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2561 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2566 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2570 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2571 p
->p_vaddr
-= off
- p
->p_offset
;
2572 if (! m
->p_paddr_valid
)
2573 p
->p_paddr
-= off
- p
->p_offset
;
2576 if (p
->p_type
== PT_LOAD
)
2578 phdrs_vaddr
= p
->p_vaddr
;
2579 phdrs_paddr
= p
->p_paddr
;
2582 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
2585 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2586 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2589 if (p
->p_type
== PT_LOAD
)
2591 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2597 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2598 p
->p_filesz
+= adjust
;
2599 p
->p_memsz
+= adjust
;
2605 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2609 bfd_size_type align
;
2613 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2615 /* The section may have artificial alignment forced by a
2616 link script. Notice this case by the gap between the
2617 cumulative phdr vma and the section's vma. */
2618 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2620 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2622 p
->p_memsz
+= adjust
;
2625 if ((flags
& SEC_LOAD
) != 0)
2626 p
->p_filesz
+= adjust
;
2629 if (p
->p_type
== PT_LOAD
)
2631 bfd_signed_vma adjust
;
2633 if ((flags
& SEC_LOAD
) != 0)
2635 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2639 else if ((flags
& SEC_ALLOC
) != 0)
2641 /* The section VMA must equal the file position
2642 modulo the page size. FIXME: I'm not sure if
2643 this adjustment is really necessary. We used to
2644 not have the SEC_LOAD case just above, and then
2645 this was necessary, but now I'm not sure. */
2646 if ((abfd
->flags
& D_PAGED
) != 0)
2647 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2649 adjust
= (sec
->vma
- voff
) % align
;
2658 (* _bfd_error_handler
)
2659 (_("Error: First section in segment (%s) starts at 0x%x"),
2660 bfd_section_name (abfd
, sec
), sec
->lma
);
2661 (* _bfd_error_handler
)
2662 (_(" whereas segment starts at 0x%x"),
2667 p
->p_memsz
+= adjust
;
2670 if ((flags
& SEC_LOAD
) != 0)
2671 p
->p_filesz
+= adjust
;
2676 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2677 used in a linker script we may have a section with
2678 SEC_LOAD clear but which is supposed to have
2680 if ((flags
& SEC_LOAD
) != 0
2681 || (flags
& SEC_HAS_CONTENTS
) != 0)
2682 off
+= sec
->_raw_size
;
2684 if ((flags
& SEC_ALLOC
) != 0)
2685 voff
+= sec
->_raw_size
;
2688 p
->p_memsz
+= sec
->_raw_size
;
2690 if ((flags
& SEC_LOAD
) != 0)
2691 p
->p_filesz
+= sec
->_raw_size
;
2693 if (align
> p
->p_align
)
2696 if (! m
->p_flags_valid
)
2699 if ((flags
& SEC_CODE
) != 0)
2701 if ((flags
& SEC_READONLY
) == 0)
2707 /* Now that we have set the section file positions, we can set up
2708 the file positions for the non PT_LOAD segments. */
2709 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2713 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
2715 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
2716 p
->p_offset
= m
->sections
[0]->filepos
;
2720 if (m
->includes_filehdr
)
2722 p
->p_vaddr
= filehdr_vaddr
;
2723 if (! m
->p_paddr_valid
)
2724 p
->p_paddr
= filehdr_paddr
;
2726 else if (m
->includes_phdrs
)
2728 p
->p_vaddr
= phdrs_vaddr
;
2729 if (! m
->p_paddr_valid
)
2730 p
->p_paddr
= phdrs_paddr
;
2735 /* Clear out any program headers we allocated but did not use. */
2736 for (; count
< alloc
; count
++, p
++)
2738 memset (p
, 0, sizeof *p
);
2739 p
->p_type
= PT_NULL
;
2742 elf_tdata (abfd
)->phdr
= phdrs
;
2744 elf_tdata (abfd
)->next_file_pos
= off
;
2746 /* Write out the program headers. */
2747 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
2748 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
2754 /* Get the size of the program header.
2756 If this is called by the linker before any of the section VMA's are set, it
2757 can't calculate the correct value for a strange memory layout. This only
2758 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2759 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2760 data segment (exclusive of .interp and .dynamic).
2762 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2763 will be two segments. */
2765 static bfd_size_type
2766 get_program_header_size (abfd
)
2771 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2773 /* We can't return a different result each time we're called. */
2774 if (elf_tdata (abfd
)->program_header_size
!= 0)
2775 return elf_tdata (abfd
)->program_header_size
;
2777 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2779 struct elf_segment_map
*m
;
2782 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2784 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
2785 return elf_tdata (abfd
)->program_header_size
;
2788 /* Assume we will need exactly two PT_LOAD segments: one for text
2789 and one for data. */
2792 s
= bfd_get_section_by_name (abfd
, ".interp");
2793 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2795 /* If we have a loadable interpreter section, we need a
2796 PT_INTERP segment. In this case, assume we also need a
2797 PT_PHDR segment, although that may not be true for all
2802 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
2804 /* We need a PT_DYNAMIC segment. */
2808 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2810 if ((s
->flags
& SEC_LOAD
) != 0
2811 && strncmp (s
->name
, ".note", 5) == 0)
2813 /* We need a PT_NOTE segment. */
2818 /* Let the backend count up any program headers it might need. */
2819 if (bed
->elf_backend_additional_program_headers
)
2823 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
2829 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
2830 return elf_tdata (abfd
)->program_header_size
;
2833 /* Work out the file positions of all the sections. This is called by
2834 _bfd_elf_compute_section_file_positions. All the section sizes and
2835 VMAs must be known before this is called.
2837 We do not consider reloc sections at this point, unless they form
2838 part of the loadable image. Reloc sections are assigned file
2839 positions in assign_file_positions_for_relocs, which is called by
2840 write_object_contents and final_link.
2842 We also don't set the positions of the .symtab and .strtab here. */
2845 assign_file_positions_except_relocs (abfd
)
2848 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
2849 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
2850 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
2852 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2854 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
2856 Elf_Internal_Shdr
**hdrpp
;
2859 /* Start after the ELF header. */
2860 off
= i_ehdrp
->e_ehsize
;
2862 /* We are not creating an executable, which means that we are
2863 not creating a program header, and that the actual order of
2864 the sections in the file is unimportant. */
2865 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2867 Elf_Internal_Shdr
*hdr
;
2870 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
2872 hdr
->sh_offset
= -1;
2875 if (i
== tdata
->symtab_section
2876 || i
== tdata
->strtab_section
)
2878 hdr
->sh_offset
= -1;
2882 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2888 Elf_Internal_Shdr
**hdrpp
;
2890 /* Assign file positions for the loaded sections based on the
2891 assignment of sections to segments. */
2892 if (! assign_file_positions_for_segments (abfd
))
2895 /* Assign file positions for the other sections. */
2897 off
= elf_tdata (abfd
)->next_file_pos
;
2898 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2900 Elf_Internal_Shdr
*hdr
;
2903 if (hdr
->bfd_section
!= NULL
2904 && hdr
->bfd_section
->filepos
!= 0)
2905 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
2906 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2908 ((*_bfd_error_handler
)
2909 (_("%s: warning: allocated section `%s' not in segment"),
2910 bfd_get_filename (abfd
),
2911 (hdr
->bfd_section
== NULL
2913 : hdr
->bfd_section
->name
)));
2914 if ((abfd
->flags
& D_PAGED
) != 0)
2915 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
2917 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
2918 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
2921 else if (hdr
->sh_type
== SHT_REL
2922 || hdr
->sh_type
== SHT_RELA
2923 || hdr
== i_shdrpp
[tdata
->symtab_section
]
2924 || hdr
== i_shdrpp
[tdata
->strtab_section
])
2925 hdr
->sh_offset
= -1;
2927 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2931 /* Place the section headers. */
2932 off
= align_file_position (off
, bed
->s
->file_align
);
2933 i_ehdrp
->e_shoff
= off
;
2934 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
2936 elf_tdata (abfd
)->next_file_pos
= off
;
2945 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2946 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2947 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2949 struct bfd_strtab_hash
*shstrtab
;
2950 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2952 i_ehdrp
= elf_elfheader (abfd
);
2953 i_shdrp
= elf_elfsections (abfd
);
2955 shstrtab
= _bfd_elf_stringtab_init ();
2956 if (shstrtab
== NULL
)
2959 elf_shstrtab (abfd
) = shstrtab
;
2961 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
2962 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
2963 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
2964 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
2966 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
2967 i_ehdrp
->e_ident
[EI_DATA
] =
2968 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
2969 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
2971 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2972 i_ehdrp
->e_ident
[count
] = 0;
2974 if ((abfd
->flags
& DYNAMIC
) != 0)
2975 i_ehdrp
->e_type
= ET_DYN
;
2976 else if ((abfd
->flags
& EXEC_P
) != 0)
2977 i_ehdrp
->e_type
= ET_EXEC
;
2979 i_ehdrp
->e_type
= ET_REL
;
2981 switch (bfd_get_arch (abfd
))
2983 case bfd_arch_unknown
:
2984 i_ehdrp
->e_machine
= EM_NONE
;
2986 case bfd_arch_sparc
:
2987 if (bed
->s
->arch_size
== 64)
2988 i_ehdrp
->e_machine
= EM_SPARCV9
;
2990 i_ehdrp
->e_machine
= EM_SPARC
;
2993 i_ehdrp
->e_machine
= EM_386
;
2996 i_ehdrp
->e_machine
= EM_68K
;
2999 i_ehdrp
->e_machine
= EM_88K
;
3002 i_ehdrp
->e_machine
= EM_860
;
3004 case bfd_arch_mips
: /* MIPS Rxxxx */
3005 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
3008 i_ehdrp
->e_machine
= EM_PARISC
;
3010 case bfd_arch_powerpc
:
3011 i_ehdrp
->e_machine
= EM_PPC
;
3013 case bfd_arch_alpha
:
3014 i_ehdrp
->e_machine
= EM_ALPHA
;
3017 i_ehdrp
->e_machine
= EM_SH
;
3020 i_ehdrp
->e_machine
= EM_CYGNUS_D10V
;
3022 /* start-sanitize-d30v */
3024 i_ehdrp
->e_machine
= EM_CYGNUS_D30V
;
3026 /* end-sanitize-d30v */
3028 switch (bfd_get_mach (abfd
))
3031 case 0: i_ehdrp
->e_machine
= EM_CYGNUS_V850
; break;
3035 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
3038 i_ehdrp
->e_machine
= EM_CYGNUS_M32R
;
3040 case bfd_arch_mn10200
:
3041 i_ehdrp
->e_machine
= EM_CYGNUS_MN10200
;
3043 case bfd_arch_mn10300
:
3044 i_ehdrp
->e_machine
= EM_CYGNUS_MN10300
;
3046 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3048 i_ehdrp
->e_machine
= EM_NONE
;
3050 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3051 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3053 /* no program header, for now. */
3054 i_ehdrp
->e_phoff
= 0;
3055 i_ehdrp
->e_phentsize
= 0;
3056 i_ehdrp
->e_phnum
= 0;
3058 /* each bfd section is section header entry */
3059 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3060 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3062 /* if we're building an executable, we'll need a program header table */
3063 if (abfd
->flags
& EXEC_P
)
3065 /* it all happens later */
3067 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3069 /* elf_build_phdrs() returns a (NULL-terminated) array of
3070 Elf_Internal_Phdrs */
3071 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3072 i_ehdrp
->e_phoff
= outbase
;
3073 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3078 i_ehdrp
->e_phentsize
= 0;
3080 i_ehdrp
->e_phoff
= 0;
3083 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3084 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3085 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3086 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3087 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3088 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3089 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3090 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3091 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3097 /* Assign file positions for all the reloc sections which are not part
3098 of the loadable file image. */
3101 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3106 Elf_Internal_Shdr
**shdrpp
;
3108 off
= elf_tdata (abfd
)->next_file_pos
;
3110 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3111 i
< elf_elfheader (abfd
)->e_shnum
;
3114 Elf_Internal_Shdr
*shdrp
;
3117 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3118 && shdrp
->sh_offset
== -1)
3119 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3122 elf_tdata (abfd
)->next_file_pos
= off
;
3126 _bfd_elf_write_object_contents (abfd
)
3129 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3130 Elf_Internal_Ehdr
*i_ehdrp
;
3131 Elf_Internal_Shdr
**i_shdrp
;
3135 if (! abfd
->output_has_begun
3136 && ! _bfd_elf_compute_section_file_positions
3137 (abfd
, (struct bfd_link_info
*) NULL
))
3140 i_shdrp
= elf_elfsections (abfd
);
3141 i_ehdrp
= elf_elfheader (abfd
);
3144 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3148 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3150 /* After writing the headers, we need to write the sections too... */
3151 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3153 if (bed
->elf_backend_section_processing
)
3154 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3155 if (i_shdrp
[count
]->contents
)
3157 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3158 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
3160 != i_shdrp
[count
]->sh_size
))
3165 /* Write out the section header names. */
3166 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3167 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3170 if (bed
->elf_backend_final_write_processing
)
3171 (*bed
->elf_backend_final_write_processing
) (abfd
,
3172 elf_tdata (abfd
)->linker
);
3174 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3177 /* given a section, search the header to find them... */
3179 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3183 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3184 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3186 Elf_Internal_Shdr
*hdr
;
3187 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3189 for (index
= 0; index
< maxindex
; index
++)
3191 hdr
= i_shdrp
[index
];
3192 if (hdr
->bfd_section
== asect
)
3196 if (bed
->elf_backend_section_from_bfd_section
)
3198 for (index
= 0; index
< maxindex
; index
++)
3202 hdr
= i_shdrp
[index
];
3204 if ((*bed
->elf_backend_section_from_bfd_section
)
3205 (abfd
, hdr
, asect
, &retval
))
3210 if (bfd_is_abs_section (asect
))
3212 if (bfd_is_com_section (asect
))
3214 if (bfd_is_und_section (asect
))
3217 bfd_set_error (bfd_error_nonrepresentable_section
);
3222 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3226 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3228 asymbol
**asym_ptr_ptr
;
3230 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3232 flagword flags
= asym_ptr
->flags
;
3234 /* When gas creates relocations against local labels, it creates its
3235 own symbol for the section, but does put the symbol into the
3236 symbol chain, so udata is 0. When the linker is generating
3237 relocatable output, this section symbol may be for one of the
3238 input sections rather than the output section. */
3239 if (asym_ptr
->udata
.i
== 0
3240 && (flags
& BSF_SECTION_SYM
)
3241 && asym_ptr
->section
)
3245 if (asym_ptr
->section
->output_section
!= NULL
)
3246 indx
= asym_ptr
->section
->output_section
->index
;
3248 indx
= asym_ptr
->section
->index
;
3249 if (elf_section_syms (abfd
)[indx
])
3250 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3253 idx
= asym_ptr
->udata
.i
;
3257 /* This case can occur when using --strip-symbol on a symbol
3258 which is used in a relocation entry. */
3259 (*_bfd_error_handler
)
3260 (_("%s: symbol `%s' required but not present"),
3261 bfd_get_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3262 bfd_set_error (bfd_error_no_symbols
);
3269 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3270 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3271 elf_symbol_flags (flags
));
3279 /* Copy private BFD data. This copies any program header information. */
3282 copy_private_bfd_data (ibfd
, obfd
)
3286 Elf_Internal_Ehdr
*iehdr
;
3287 struct elf_segment_map
*mfirst
;
3288 struct elf_segment_map
**pm
;
3289 struct elf_segment_map
*m
;
3290 Elf_Internal_Phdr
*p
;
3292 unsigned int num_segments
;
3293 unsigned int phdr_included
= false;
3295 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3296 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3299 if (elf_tdata (ibfd
)->phdr
== NULL
)
3302 iehdr
= elf_elfheader (ibfd
);
3307 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3309 #define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3310 ((addr) >= (bottom) \
3311 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3312 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3314 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3315 linker, which generates a PT_INTERP section with p_vaddr and
3316 p_memsz set to 0. */
3318 #define IS_SOLARIS_PT_INTERP(p, s) \
3320 && p->p_filesz > 0 \
3321 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3322 && s->_raw_size > 0 \
3323 && (bfd_vma) s->filepos >= p->p_offset \
3324 && ((bfd_vma) s->filepos + s->_raw_size \
3325 <= p->p_offset + p->p_filesz))
3328 /* Scan through the segments specified in the program header
3329 of the input BFD. */
3330 for (i
= 0, p
= elf_tdata (ibfd
)->phdr
; i
< num_segments
; i
++, p
++)
3334 asection
**sections
;
3337 bfd_vma matching_lma
;
3338 bfd_vma suggested_lma
;
3341 /* For each section in the input BFD, decide if it should be included
3342 in the current segment. A section will be included if it is within
3343 the address space of the segment, and it is an allocated segment,
3344 and there is an output section associated with it. */
3346 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3347 if ((IS_CONTAINED_BY (s
->vma
, s
->_raw_size
, p
->p_vaddr
, p
)
3348 || IS_SOLARIS_PT_INTERP (p
, s
))
3349 && (s
->flags
& SEC_ALLOC
) != 0
3350 && s
->output_section
!= NULL
)
3353 /* Allocate a segment map big enough to contain all of the
3354 sections we have selected. */
3355 m
= ((struct elf_segment_map
*)
3357 (sizeof (struct elf_segment_map
)
3358 + ((size_t) csecs
- 1) * sizeof (asection
*))));
3362 /* Initialise the fields of the segment map. Default to
3363 using the physical address of the segment in the input BFD. */
3365 m
->p_type
= p
->p_type
;
3366 m
->p_flags
= p
->p_flags
;
3367 m
->p_flags_valid
= 1;
3368 m
->p_paddr
= p
->p_paddr
;
3369 m
->p_paddr_valid
= 1;
3371 /* Determine if this segment contains the ELF file header
3372 and if it contains the program headers themselves. */
3373 m
->includes_filehdr
= (p
->p_offset
== 0
3374 && p
->p_filesz
>= iehdr
->e_ehsize
);
3376 if (! phdr_included
)
3378 phdr_included
= m
->includes_phdrs
=
3379 (p
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3380 && (p
->p_offset
+ p
->p_filesz
3381 >= ((bfd_vma
) iehdr
->e_phoff
3382 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3387 /* Special segments, such as the PT_PHDR segment, may contain
3388 no sections, but ordinary, loadable segments should contain
3391 if (p
->p_type
== PT_LOAD
)
3393 (_("%s: warning: Empty loadable segment detected\n"),
3394 bfd_get_filename (ibfd
));
3403 /* Now scan the sections in the input BFD again and attempt
3404 to add their corresponding output sections to the segment map.
3405 The problem here is how to handle an output section which has
3406 been moved (ie had its LMA changed). There are four possibilities:
3408 1. None of the sections have been moved.
3409 In this case we can continue to use the segment LMA from the
3412 2. All of the sections have been moved by the same amount.
3413 In this case we can change the segment's LMA to match the LMA
3414 of the first section.
3416 3. Some of the sections have been moved, others have not.
3417 In this case those sections which have not been moved can be
3418 placed in the current segment which will have to have its size,
3419 and possibly its LMA changed, and a new segment or segments will
3420 have to be created to contain the other sections.
3422 4. The sections have been moved, but not be the same amount.
3423 In this case we can change the segment's LMA to match the LMA
3424 of the first section and we will have to create a new segment
3425 or segments to contain the other sections.
3427 In order to save time, we allocate an array to hold the section
3428 pointers that we are interested in. As these sections get assigned
3429 to a segment, they are removed from this array. */
3431 sections
= (asection
**) bfd_malloc (sizeof (asection
*) * csecs
);
3432 if (sections
== NULL
)
3435 /* Step One: Scan for segment vs section LMA conflicts.
3436 Also add the sections to the section array allocated above.
3437 Also add the sections to the current segment. In the common
3438 case, where the sections have not been moved, this means that
3439 we have completely filled the segment, and there is nothing
3443 matching_lma
= false;
3446 for (j
= 0, s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3448 os
= s
->output_section
;
3450 if ((IS_CONTAINED_BY (s
->vma
, s
->_raw_size
, p
->p_vaddr
, p
)
3451 || IS_SOLARIS_PT_INTERP (p
, s
))
3452 && (s
->flags
& SEC_ALLOC
) != 0
3457 /* The Solaris native linker always sets p_paddr to 0.
3458 We try to catch that case here, and set it to the
3464 && (os
->vma
== (p
->p_vaddr
3465 + (m
->includes_filehdr
3468 + (m
->includes_phdrs
3469 ? iehdr
->e_phnum
* iehdr
->e_phentsize
3471 m
->p_paddr
= p
->p_vaddr
;
3473 /* Match up the physical address of the segment with the
3474 LMA address of the output section. */
3475 if (IS_CONTAINED_BY (os
->lma
, os
->_raw_size
, m
->p_paddr
, p
))
3477 if (matching_lma
== 0)
3478 matching_lma
= os
->lma
;
3480 /* We assume that if the section fits within the segment
3481 that it does not overlap any other section within that
3483 m
->sections
[isec
++] = os
;
3485 else if (suggested_lma
== 0)
3486 suggested_lma
= os
->lma
;
3490 BFD_ASSERT (j
== csecs
);
3492 /* Step Two: Adjust the physical address of the current segment,
3496 /* All of the sections fitted within the segment as currently
3497 specified. This is the default case. Add the segment to
3498 the list of built segments and carry on to process the next
3499 program header in the input BFD. */
3507 else if (matching_lma
!= 0)
3509 /* At least one section fits inside the current segment.
3510 Keep it, but modify its physical address to match the
3511 LMA of the first section that fitted. */
3513 m
->p_paddr
= matching_lma
;
3517 /* None of the sections fitted inside the current segment.
3518 Change the current segment's physical address to match
3519 the LMA of the first section. */
3521 m
->p_paddr
= suggested_lma
;
3524 /* Step Three: Loop over the sections again, this time assigning
3525 those that fit to the current segment and remvoing them from the
3526 sections array; but making sure not to leave large gaps. Once all
3527 possible sections have been assigned to the current segment it is
3528 added to the list of built segments and if sections still remain
3529 to be assigned, a new segment is constructed before repeating
3537 /* Fill the current segment with sections that fit. */
3538 for (j
= 0; j
< csecs
; j
++)
3545 os
= s
->output_section
;
3547 if (IS_CONTAINED_BY (os
->lma
, os
->_raw_size
, m
->p_paddr
, p
))
3551 /* If the first section in a segment does not start at
3552 the beginning of the segment, then something is wrong. */
3553 if (os
->lma
!= m
->p_paddr
)
3558 asection
* prev_sec
;
3559 bfd_vma maxpagesize
;
3561 prev_sec
= m
->sections
[m
->count
- 1];
3562 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3564 /* If the gap between the end of the previous section
3565 and the start of this section is more than maxpagesize
3566 then we need to start a new segment. */
3567 if (BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
3568 < BFD_ALIGN (os
->lma
, maxpagesize
))
3570 if (suggested_lma
== 0)
3571 suggested_lma
= os
->lma
;
3577 m
->sections
[m
->count
++] = os
;
3581 else if (suggested_lma
== 0)
3582 suggested_lma
= os
->lma
;
3585 BFD_ASSERT (m
->count
> 0);
3587 /* Add the current segment to the list of built segments. */
3593 /* We still have not allocated all of the sections to
3594 segments. Create a new segment here, initialise it
3595 and carry on looping. */
3597 m
= ((struct elf_segment_map
*)
3599 (sizeof (struct elf_segment_map
)
3600 + ((size_t) csecs
- 1) * sizeof (asection
*))));
3604 /* Initialise the fields of the segment map. Set the physical
3605 physical address to the LMA of the first section that has
3606 not yet been assigned. */
3609 m
->p_type
= p
->p_type
;
3610 m
->p_flags
= p
->p_flags
;
3611 m
->p_flags_valid
= 1;
3612 m
->p_paddr
= suggested_lma
;
3613 m
->p_paddr_valid
= 1;
3614 m
->includes_filehdr
= 0;
3615 m
->includes_phdrs
= 0;
3618 while (isec
< csecs
);
3623 /* The Solaris linker creates program headers in which all the
3624 p_paddr fields are zero. When we try to objcopy or strip such a
3625 file, we get confused. Check for this case, and if we find it
3626 reset the p_paddr_valid fields. */
3627 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3628 if (m
->p_paddr
!= 0)
3632 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3633 m
->p_paddr_valid
= 0;
3636 elf_tdata (obfd
)->segment_map
= mfirst
;
3639 /* Final Step: Sort the segments into ascending order of physical address. */
3642 struct elf_segment_map
* prev
;
3645 for (m
= mfirst
->next
; m
!= NULL
; prev
= m
, m
= m
->next
)
3647 /* Yes I know - its a bubble sort....*/
3648 if (m
->next
!= NULL
&& (m
->next
->p_paddr
< m
->p_paddr
))
3650 /* swap m and m->next */
3651 prev
->next
= m
->next
;
3652 m
->next
= m
->next
->next
;
3653 prev
->next
->next
= m
;
3662 #undef IS_CONTAINED_BY
3663 #undef IS_SOLARIS_PT_INTERP
3668 /* Copy private section information. This copies over the entsize
3669 field, and sometimes the info field. */
3672 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
3678 Elf_Internal_Shdr
*ihdr
, *ohdr
;
3680 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
3681 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
3684 /* Copy over private BFD data if it has not already been copied.
3685 This must be done here, rather than in the copy_private_bfd_data
3686 entry point, because the latter is called after the section
3687 contents have been set, which means that the program headers have
3688 already been worked out. */
3689 if (elf_tdata (obfd
)->segment_map
== NULL
3690 && elf_tdata (ibfd
)->phdr
!= NULL
)
3694 /* Only set up the segments if there are no more SEC_ALLOC
3695 sections. FIXME: This won't do the right thing if objcopy is
3696 used to remove the last SEC_ALLOC section, since objcopy
3697 won't call this routine in that case. */
3698 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
3699 if ((s
->flags
& SEC_ALLOC
) != 0)
3703 if (! copy_private_bfd_data (ibfd
, obfd
))
3708 ihdr
= &elf_section_data (isec
)->this_hdr
;
3709 ohdr
= &elf_section_data (osec
)->this_hdr
;
3711 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
3713 if (ihdr
->sh_type
== SHT_SYMTAB
3714 || ihdr
->sh_type
== SHT_DYNSYM
3715 || ihdr
->sh_type
== SHT_GNU_verneed
3716 || ihdr
->sh_type
== SHT_GNU_verdef
)
3717 ohdr
->sh_info
= ihdr
->sh_info
;
3722 /* Copy private symbol information. If this symbol is in a section
3723 which we did not map into a BFD section, try to map the section
3724 index correctly. We use special macro definitions for the mapped
3725 section indices; these definitions are interpreted by the
3726 swap_out_syms function. */
3728 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3729 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3730 #define MAP_STRTAB (SHN_LORESERVE - 3)
3731 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3734 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
3740 elf_symbol_type
*isym
, *osym
;
3742 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3743 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3746 isym
= elf_symbol_from (ibfd
, isymarg
);
3747 osym
= elf_symbol_from (obfd
, osymarg
);
3751 && bfd_is_abs_section (isym
->symbol
.section
))
3755 shndx
= isym
->internal_elf_sym
.st_shndx
;
3756 if (shndx
== elf_onesymtab (ibfd
))
3757 shndx
= MAP_ONESYMTAB
;
3758 else if (shndx
== elf_dynsymtab (ibfd
))
3759 shndx
= MAP_DYNSYMTAB
;
3760 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
3762 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
3763 shndx
= MAP_SHSTRTAB
;
3764 osym
->internal_elf_sym
.st_shndx
= shndx
;
3770 /* Swap out the symbols. */
3773 swap_out_syms (abfd
, sttp
, relocatable_p
)
3775 struct bfd_strtab_hash
**sttp
;
3778 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3780 if (!elf_map_symbols (abfd
))
3783 /* Dump out the symtabs. */
3785 int symcount
= bfd_get_symcount (abfd
);
3786 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3787 struct bfd_strtab_hash
*stt
;
3788 Elf_Internal_Shdr
*symtab_hdr
;
3789 Elf_Internal_Shdr
*symstrtab_hdr
;
3790 char *outbound_syms
;
3793 stt
= _bfd_elf_stringtab_init ();
3797 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3798 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3799 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3800 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
3801 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
3802 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
3804 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3805 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3807 outbound_syms
= bfd_alloc (abfd
,
3808 (1 + symcount
) * bed
->s
->sizeof_sym
);
3809 if (outbound_syms
== NULL
)
3811 symtab_hdr
->contents
= (PTR
) outbound_syms
;
3813 /* now generate the data (for "contents") */
3815 /* Fill in zeroth symbol and swap it out. */
3816 Elf_Internal_Sym sym
;
3822 sym
.st_shndx
= SHN_UNDEF
;
3823 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
3824 outbound_syms
+= bed
->s
->sizeof_sym
;
3826 for (idx
= 0; idx
< symcount
; idx
++)
3828 Elf_Internal_Sym sym
;
3829 bfd_vma value
= syms
[idx
]->value
;
3830 elf_symbol_type
*type_ptr
;
3831 flagword flags
= syms
[idx
]->flags
;
3834 if (flags
& BSF_SECTION_SYM
)
3835 /* Section symbols have no names. */
3839 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
3842 if (sym
.st_name
== (unsigned long) -1)
3846 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
3848 if (bfd_is_com_section (syms
[idx
]->section
))
3850 /* ELF common symbols put the alignment into the `value' field,
3851 and the size into the `size' field. This is backwards from
3852 how BFD handles it, so reverse it here. */
3853 sym
.st_size
= value
;
3854 if (type_ptr
== NULL
3855 || type_ptr
->internal_elf_sym
.st_value
== 0)
3856 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
3858 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
3859 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
3860 (abfd
, syms
[idx
]->section
);
3864 asection
*sec
= syms
[idx
]->section
;
3867 if (sec
->output_section
)
3869 value
+= sec
->output_offset
;
3870 sec
= sec
->output_section
;
3872 /* Don't add in the section vma for relocatable output. */
3873 if (! relocatable_p
)
3875 sym
.st_value
= value
;
3876 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
3878 if (bfd_is_abs_section (sec
)
3880 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
3882 /* This symbol is in a real ELF section which we did
3883 not create as a BFD section. Undo the mapping done
3884 by copy_private_symbol_data. */
3885 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
3889 shndx
= elf_onesymtab (abfd
);
3892 shndx
= elf_dynsymtab (abfd
);
3895 shndx
= elf_tdata (abfd
)->strtab_section
;
3898 shndx
= elf_tdata (abfd
)->shstrtab_section
;
3906 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3912 /* Writing this would be a hell of a lot easier if
3913 we had some decent documentation on bfd, and
3914 knew what to expect of the library, and what to
3915 demand of applications. For example, it
3916 appears that `objcopy' might not set the
3917 section of a symbol to be a section that is
3918 actually in the output file. */
3919 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
3920 BFD_ASSERT (sec2
!= 0);
3921 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
3922 BFD_ASSERT (shndx
!= -1);
3926 sym
.st_shndx
= shndx
;
3929 if ((flags
& BSF_FUNCTION
) != 0)
3931 else if ((flags
& BSF_OBJECT
) != 0)
3936 if (bfd_is_com_section (syms
[idx
]->section
))
3937 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
3938 else if (bfd_is_und_section (syms
[idx
]->section
))
3939 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
3943 else if (flags
& BSF_SECTION_SYM
)
3944 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3945 else if (flags
& BSF_FILE
)
3946 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
3949 int bind
= STB_LOCAL
;
3951 if (flags
& BSF_LOCAL
)
3953 else if (flags
& BSF_WEAK
)
3955 else if (flags
& BSF_GLOBAL
)
3958 sym
.st_info
= ELF_ST_INFO (bind
, type
);
3961 if (type_ptr
!= NULL
)
3962 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
3966 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
3967 outbound_syms
+= bed
->s
->sizeof_sym
;
3971 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
3972 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3974 symstrtab_hdr
->sh_flags
= 0;
3975 symstrtab_hdr
->sh_addr
= 0;
3976 symstrtab_hdr
->sh_entsize
= 0;
3977 symstrtab_hdr
->sh_link
= 0;
3978 symstrtab_hdr
->sh_info
= 0;
3979 symstrtab_hdr
->sh_addralign
= 1;
3985 /* Return the number of bytes required to hold the symtab vector.
3987 Note that we base it on the count plus 1, since we will null terminate
3988 the vector allocated based on this size. However, the ELF symbol table
3989 always has a dummy entry as symbol #0, so it ends up even. */
3992 _bfd_elf_get_symtab_upper_bound (abfd
)
3997 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3999 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4000 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4006 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4011 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4013 if (elf_dynsymtab (abfd
) == 0)
4015 bfd_set_error (bfd_error_invalid_operation
);
4019 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4020 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4026 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4030 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4033 /* Canonicalize the relocs. */
4036 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4045 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
4051 tblptr
= section
->relocation
;
4052 for (i
= 0; i
< section
->reloc_count
; i
++)
4053 *relptr
++ = tblptr
++;
4057 return section
->reloc_count
;
4061 _bfd_elf_get_symtab (abfd
, alocation
)
4063 asymbol
**alocation
;
4065 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4066 (abfd
, alocation
, false);
4069 bfd_get_symcount (abfd
) = symcount
;
4074 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4076 asymbol
**alocation
;
4078 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4079 (abfd
, alocation
, true);
4082 /* Return the size required for the dynamic reloc entries. Any
4083 section that was actually installed in the BFD, and has type
4084 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4085 considered to be a dynamic reloc section. */
4088 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4094 if (elf_dynsymtab (abfd
) == 0)
4096 bfd_set_error (bfd_error_invalid_operation
);
4100 ret
= sizeof (arelent
*);
4101 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4102 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4103 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4104 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4105 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4106 * sizeof (arelent
*));
4111 /* Canonicalize the dynamic relocation entries. Note that we return
4112 the dynamic relocations as a single block, although they are
4113 actually associated with particular sections; the interface, which
4114 was designed for SunOS style shared libraries, expects that there
4115 is only one set of dynamic relocs. Any section that was actually
4116 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4117 the dynamic symbol table, is considered to be a dynamic reloc
4121 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4126 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4130 if (elf_dynsymtab (abfd
) == 0)
4132 bfd_set_error (bfd_error_invalid_operation
);
4136 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4138 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4140 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4141 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4142 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4147 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4149 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4151 for (i
= 0; i
< count
; i
++)
4162 /* Read in the version information. */
4165 _bfd_elf_slurp_version_tables (abfd
)
4168 bfd_byte
*contents
= NULL
;
4170 if (elf_dynverdef (abfd
) != 0)
4172 Elf_Internal_Shdr
*hdr
;
4173 Elf_External_Verdef
*everdef
;
4174 Elf_Internal_Verdef
*iverdef
;
4177 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
4179 elf_tdata (abfd
)->verdef
=
4180 ((Elf_Internal_Verdef
*)
4181 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verdef
)));
4182 if (elf_tdata (abfd
)->verdef
== NULL
)
4185 elf_tdata (abfd
)->cverdefs
= hdr
->sh_info
;
4187 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4188 if (contents
== NULL
)
4190 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4191 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4194 everdef
= (Elf_External_Verdef
*) contents
;
4195 iverdef
= elf_tdata (abfd
)->verdef
;
4196 for (i
= 0; i
< hdr
->sh_info
; i
++, iverdef
++)
4198 Elf_External_Verdaux
*everdaux
;
4199 Elf_Internal_Verdaux
*iverdaux
;
4202 _bfd_elf_swap_verdef_in (abfd
, everdef
, iverdef
);
4204 iverdef
->vd_bfd
= abfd
;
4206 iverdef
->vd_auxptr
= ((Elf_Internal_Verdaux
*)
4209 * sizeof (Elf_Internal_Verdaux
))));
4210 if (iverdef
->vd_auxptr
== NULL
)
4213 everdaux
= ((Elf_External_Verdaux
*)
4214 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
4215 iverdaux
= iverdef
->vd_auxptr
;
4216 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
4218 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
4220 iverdaux
->vda_nodename
=
4221 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4222 iverdaux
->vda_name
);
4223 if (iverdaux
->vda_nodename
== NULL
)
4226 if (j
+ 1 < iverdef
->vd_cnt
)
4227 iverdaux
->vda_nextptr
= iverdaux
+ 1;
4229 iverdaux
->vda_nextptr
= NULL
;
4231 everdaux
= ((Elf_External_Verdaux
*)
4232 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
4235 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
4237 if (i
+ 1 < hdr
->sh_info
)
4238 iverdef
->vd_nextdef
= iverdef
+ 1;
4240 iverdef
->vd_nextdef
= NULL
;
4242 everdef
= ((Elf_External_Verdef
*)
4243 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
4250 if (elf_dynverref (abfd
) != 0)
4252 Elf_Internal_Shdr
*hdr
;
4253 Elf_External_Verneed
*everneed
;
4254 Elf_Internal_Verneed
*iverneed
;
4257 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
4259 elf_tdata (abfd
)->verref
=
4260 ((Elf_Internal_Verneed
*)
4261 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verneed
)));
4262 if (elf_tdata (abfd
)->verref
== NULL
)
4265 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
4267 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4268 if (contents
== NULL
)
4270 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4271 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4274 everneed
= (Elf_External_Verneed
*) contents
;
4275 iverneed
= elf_tdata (abfd
)->verref
;
4276 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4278 Elf_External_Vernaux
*evernaux
;
4279 Elf_Internal_Vernaux
*ivernaux
;
4282 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4284 iverneed
->vn_bfd
= abfd
;
4286 iverneed
->vn_filename
=
4287 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4289 if (iverneed
->vn_filename
== NULL
)
4292 iverneed
->vn_auxptr
=
4293 ((Elf_Internal_Vernaux
*)
4295 iverneed
->vn_cnt
* sizeof (Elf_Internal_Vernaux
)));
4297 evernaux
= ((Elf_External_Vernaux
*)
4298 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4299 ivernaux
= iverneed
->vn_auxptr
;
4300 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4302 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4304 ivernaux
->vna_nodename
=
4305 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4306 ivernaux
->vna_name
);
4307 if (ivernaux
->vna_nodename
== NULL
)
4310 if (j
+ 1 < iverneed
->vn_cnt
)
4311 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4313 ivernaux
->vna_nextptr
= NULL
;
4315 evernaux
= ((Elf_External_Vernaux
*)
4316 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4319 if (i
+ 1 < hdr
->sh_info
)
4320 iverneed
->vn_nextref
= iverneed
+ 1;
4322 iverneed
->vn_nextref
= NULL
;
4324 everneed
= ((Elf_External_Verneed
*)
4325 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4335 if (contents
== NULL
)
4341 _bfd_elf_make_empty_symbol (abfd
)
4344 elf_symbol_type
*newsym
;
4346 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
4351 newsym
->symbol
.the_bfd
= abfd
;
4352 return &newsym
->symbol
;
4357 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4362 bfd_symbol_info (symbol
, ret
);
4365 /* Return whether a symbol name implies a local symbol. Most targets
4366 use this function for the is_local_label_name entry point, but some
4370 _bfd_elf_is_local_label_name (abfd
, name
)
4374 /* Normal local symbols start with ``.L''. */
4375 if (name
[0] == '.' && name
[1] == 'L')
4378 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4379 DWARF debugging symbols starting with ``..''. */
4380 if (name
[0] == '.' && name
[1] == '.')
4383 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4384 emitting DWARF debugging output. I suspect this is actually a
4385 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4386 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4387 underscore to be emitted on some ELF targets). For ease of use,
4388 we treat such symbols as local. */
4389 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
4396 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
4405 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
4407 enum bfd_architecture arch
;
4408 unsigned long machine
;
4410 /* If this isn't the right architecture for this backend, and this
4411 isn't the generic backend, fail. */
4412 if (arch
!= get_elf_backend_data (abfd
)->arch
4413 && arch
!= bfd_arch_unknown
4414 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
4417 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
4420 /* Find the nearest line to a particular section and offset, for error
4424 _bfd_elf_find_nearest_line (abfd
,
4435 CONST
char **filename_ptr
;
4436 CONST
char **functionname_ptr
;
4437 unsigned int *line_ptr
;
4440 const char *filename
;
4445 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
4446 filename_ptr
, functionname_ptr
,
4450 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
4451 &found
, filename_ptr
,
4452 functionname_ptr
, line_ptr
,
4453 &elf_tdata (abfd
)->line_info
))
4458 if (symbols
== NULL
)
4465 for (p
= symbols
; *p
!= NULL
; p
++)
4469 q
= (elf_symbol_type
*) *p
;
4471 if (bfd_get_section (&q
->symbol
) != section
)
4474 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
4479 filename
= bfd_asymbol_name (&q
->symbol
);
4482 if (q
->symbol
.section
== section
4483 && q
->symbol
.value
>= low_func
4484 && q
->symbol
.value
<= offset
)
4486 func
= (asymbol
*) q
;
4487 low_func
= q
->symbol
.value
;
4496 *filename_ptr
= filename
;
4497 *functionname_ptr
= bfd_asymbol_name (func
);
4503 _bfd_elf_sizeof_headers (abfd
, reloc
)
4509 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
4511 ret
+= get_program_header_size (abfd
);
4516 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
4521 bfd_size_type count
;
4523 Elf_Internal_Shdr
*hdr
;
4525 if (! abfd
->output_has_begun
4526 && ! _bfd_elf_compute_section_file_positions
4527 (abfd
, (struct bfd_link_info
*) NULL
))
4530 hdr
= &elf_section_data (section
)->this_hdr
;
4532 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
4534 if (bfd_write (location
, 1, count
, abfd
) != count
)
4541 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
4544 Elf_Internal_Rela
*dst
;
4551 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
4554 Elf_Internal_Rel
*dst
;
4560 /* Try to convert a non-ELF reloc into an ELF one. */
4563 _bfd_elf_validate_reloc (abfd
, areloc
)
4567 /* Check whether we really have an ELF howto. */
4569 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
4571 bfd_reloc_code_real_type code
;
4572 reloc_howto_type
*howto
;
4574 /* Alien reloc: Try to determine its type to replace it with an
4575 equivalent ELF reloc. */
4577 if (areloc
->howto
->pc_relative
)
4579 switch (areloc
->howto
->bitsize
)
4582 code
= BFD_RELOC_8_PCREL
;
4585 code
= BFD_RELOC_12_PCREL
;
4588 code
= BFD_RELOC_16_PCREL
;
4591 code
= BFD_RELOC_24_PCREL
;
4594 code
= BFD_RELOC_32_PCREL
;
4597 code
= BFD_RELOC_64_PCREL
;
4603 howto
= bfd_reloc_type_lookup (abfd
, code
);
4605 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
4607 if (howto
->pcrel_offset
)
4608 areloc
->addend
+= areloc
->address
;
4610 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
4615 switch (areloc
->howto
->bitsize
)
4621 code
= BFD_RELOC_14
;
4624 code
= BFD_RELOC_16
;
4627 code
= BFD_RELOC_26
;
4630 code
= BFD_RELOC_32
;
4633 code
= BFD_RELOC_64
;
4639 howto
= bfd_reloc_type_lookup (abfd
, code
);
4643 areloc
->howto
= howto
;
4651 (*_bfd_error_handler
)
4652 (_("%s: unsupported relocation type %s"),
4653 bfd_get_filename (abfd
), areloc
->howto
->name
);
4654 bfd_set_error (bfd_error_bad_value
);
4659 _bfd_elf_close_and_cleanup (abfd
)
4662 if (bfd_get_format (abfd
) == bfd_object
)
4664 if (elf_shstrtab (abfd
) != NULL
)
4665 _bfd_stringtab_free (elf_shstrtab (abfd
));
4668 return _bfd_generic_close_and_cleanup (abfd
);