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
**));
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 (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
449 if (shstrtab
!= NULL
)
451 max
= elf_elfheader (abfd
)->e_shnum
;
452 for (i
= 1; i
< max
; i
++)
453 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
460 const char *const bfd_elf_section_type_names
[] = {
461 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
462 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
463 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
466 /* ELF relocs are against symbols. If we are producing relocateable
467 output, and the reloc is against an external symbol, and nothing
468 has given us any additional addend, the resulting reloc will also
469 be against the same symbol. In such a case, we don't want to
470 change anything about the way the reloc is handled, since it will
471 all be done at final link time. Rather than put special case code
472 into bfd_perform_relocation, all the reloc types use this howto
473 function. It just short circuits the reloc if producing
474 relocateable output against an external symbol. */
477 bfd_reloc_status_type
478 bfd_elf_generic_reloc (abfd
,
486 arelent
*reloc_entry
;
489 asection
*input_section
;
491 char **error_message
;
493 if (output_bfd
!= (bfd
*) NULL
494 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
495 && (! reloc_entry
->howto
->partial_inplace
496 || reloc_entry
->addend
== 0))
498 reloc_entry
->address
+= input_section
->output_offset
;
502 return bfd_reloc_continue
;
505 /* Print out the program headers. */
508 _bfd_elf_print_private_bfd_data (abfd
, farg
)
512 FILE *f
= (FILE *) farg
;
513 Elf_Internal_Phdr
*p
;
515 bfd_byte
*dynbuf
= NULL
;
517 p
= elf_tdata (abfd
)->phdr
;
522 fprintf (f
, _("\nProgram Header:\n"));
523 c
= elf_elfheader (abfd
)->e_phnum
;
524 for (i
= 0; i
< c
; i
++, p
++)
531 case PT_NULL
: s
= "NULL"; break;
532 case PT_LOAD
: s
= "LOAD"; break;
533 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
534 case PT_INTERP
: s
= "INTERP"; break;
535 case PT_NOTE
: s
= "NOTE"; break;
536 case PT_SHLIB
: s
= "SHLIB"; break;
537 case PT_PHDR
: s
= "PHDR"; break;
538 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
540 fprintf (f
, "%8s off 0x", s
);
541 fprintf_vma (f
, p
->p_offset
);
542 fprintf (f
, " vaddr 0x");
543 fprintf_vma (f
, p
->p_vaddr
);
544 fprintf (f
, " paddr 0x");
545 fprintf_vma (f
, p
->p_paddr
);
546 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
547 fprintf (f
, " filesz 0x");
548 fprintf_vma (f
, p
->p_filesz
);
549 fprintf (f
, " memsz 0x");
550 fprintf_vma (f
, p
->p_memsz
);
551 fprintf (f
, " flags %c%c%c",
552 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
553 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
554 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
555 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
556 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
561 s
= bfd_get_section_by_name (abfd
, ".dynamic");
566 bfd_byte
*extdyn
, *extdynend
;
568 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
570 fprintf (f
, _("\nDynamic Section:\n"));
572 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
575 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
579 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
582 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
584 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
585 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
588 extdynend
= extdyn
+ s
->_raw_size
;
589 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
591 Elf_Internal_Dyn dyn
;
596 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
598 if (dyn
.d_tag
== DT_NULL
)
605 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
609 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
610 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
611 case DT_PLTGOT
: name
= "PLTGOT"; break;
612 case DT_HASH
: name
= "HASH"; break;
613 case DT_STRTAB
: name
= "STRTAB"; break;
614 case DT_SYMTAB
: name
= "SYMTAB"; break;
615 case DT_RELA
: name
= "RELA"; break;
616 case DT_RELASZ
: name
= "RELASZ"; break;
617 case DT_RELAENT
: name
= "RELAENT"; break;
618 case DT_STRSZ
: name
= "STRSZ"; break;
619 case DT_SYMENT
: name
= "SYMENT"; break;
620 case DT_INIT
: name
= "INIT"; break;
621 case DT_FINI
: name
= "FINI"; break;
622 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
623 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
624 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
625 case DT_REL
: name
= "REL"; break;
626 case DT_RELSZ
: name
= "RELSZ"; break;
627 case DT_RELENT
: name
= "RELENT"; break;
628 case DT_PLTREL
: name
= "PLTREL"; break;
629 case DT_DEBUG
: name
= "DEBUG"; break;
630 case DT_TEXTREL
: name
= "TEXTREL"; break;
631 case DT_JMPREL
: name
= "JMPREL"; break;
632 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
633 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
634 case DT_VERSYM
: name
= "VERSYM"; break;
635 case DT_VERDEF
: name
= "VERDEF"; break;
636 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
637 case DT_VERNEED
: name
= "VERNEED"; break;
638 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
641 fprintf (f
, " %-11s ", name
);
643 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
648 string
= bfd_elf_string_from_elf_section (abfd
, link
,
652 fprintf (f
, "%s", string
);
661 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
662 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
664 if (! _bfd_elf_slurp_version_tables (abfd
))
668 if (elf_dynverdef (abfd
) != 0)
670 Elf_Internal_Verdef
*t
;
672 fprintf (f
, _("\nVersion definitions:\n"));
673 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
675 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
676 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
677 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
679 Elf_Internal_Verdaux
*a
;
682 for (a
= t
->vd_auxptr
->vda_nextptr
;
685 fprintf (f
, "%s ", a
->vda_nodename
);
691 if (elf_dynverref (abfd
) != 0)
693 Elf_Internal_Verneed
*t
;
695 fprintf (f
, _("\nVersion References:\n"));
696 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
698 Elf_Internal_Vernaux
*a
;
700 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
701 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
702 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
703 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
715 /* Display ELF-specific fields of a symbol. */
718 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
722 bfd_print_symbol_type how
;
724 FILE *file
= (FILE *) filep
;
727 case bfd_print_symbol_name
:
728 fprintf (file
, "%s", symbol
->name
);
730 case bfd_print_symbol_more
:
731 fprintf (file
, "elf ");
732 fprintf_vma (file
, symbol
->value
);
733 fprintf (file
, " %lx", (long) symbol
->flags
);
735 case bfd_print_symbol_all
:
737 CONST
char *section_name
;
738 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
739 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
740 fprintf (file
, " %s\t", section_name
);
741 /* Print the "other" value for a symbol. For common symbols,
742 we've already printed the size; now print the alignment.
743 For other symbols, we have no specified alignment, and
744 we've printed the address; now print the size. */
746 (bfd_is_com_section (symbol
->section
)
747 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
748 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
750 /* If we have version information, print it. */
751 if (elf_tdata (abfd
)->dynversym_section
!= 0
752 && (elf_tdata (abfd
)->dynverdef_section
!= 0
753 || elf_tdata (abfd
)->dynverref_section
!= 0))
756 const char *version_string
;
758 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
762 else if (vernum
== 1)
763 version_string
= "Base";
764 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
766 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
769 Elf_Internal_Verneed
*t
;
772 for (t
= elf_tdata (abfd
)->verref
;
776 Elf_Internal_Vernaux
*a
;
778 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
780 if (a
->vna_other
== vernum
)
782 version_string
= a
->vna_nodename
;
789 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
790 fprintf (file
, " %-11s", version_string
);
795 fprintf (file
, " (%s)", version_string
);
796 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
801 /* If the st_other field is not zero, print it. */
802 if (((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
!= 0)
803 fprintf (file
, " 0x%02x",
805 ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
));
807 fprintf (file
, " %s", symbol
->name
);
813 /* Create an entry in an ELF linker hash table. */
815 struct bfd_hash_entry
*
816 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
817 struct bfd_hash_entry
*entry
;
818 struct bfd_hash_table
*table
;
821 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
823 /* Allocate the structure if it has not already been allocated by a
825 if (ret
== (struct elf_link_hash_entry
*) NULL
)
826 ret
= ((struct elf_link_hash_entry
*)
827 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
828 if (ret
== (struct elf_link_hash_entry
*) NULL
)
829 return (struct bfd_hash_entry
*) ret
;
831 /* Call the allocation method of the superclass. */
832 ret
= ((struct elf_link_hash_entry
*)
833 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
835 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
837 /* Set local fields. */
841 ret
->dynstr_index
= 0;
843 ret
->got_offset
= (bfd_vma
) -1;
844 ret
->plt_offset
= (bfd_vma
) -1;
845 ret
->linker_section_pointer
= (elf_linker_section_pointers_t
*)0;
846 ret
->verinfo
.verdef
= NULL
;
847 ret
->type
= STT_NOTYPE
;
849 /* Assume that we have been called by a non-ELF symbol reader.
850 This flag is then reset by the code which reads an ELF input
851 file. This ensures that a symbol created by a non-ELF symbol
852 reader will have the flag set correctly. */
853 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
856 return (struct bfd_hash_entry
*) ret
;
859 /* Initialize an ELF linker hash table. */
862 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
863 struct elf_link_hash_table
*table
;
865 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
866 struct bfd_hash_table
*,
869 table
->dynamic_sections_created
= false;
870 table
->dynobj
= NULL
;
871 /* The first dynamic symbol is a dummy. */
872 table
->dynsymcount
= 1;
873 table
->dynstr
= NULL
;
874 table
->bucketcount
= 0;
875 table
->needed
= NULL
;
877 table
->stab_info
= NULL
;
878 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
881 /* Create an ELF linker hash table. */
883 struct bfd_link_hash_table
*
884 _bfd_elf_link_hash_table_create (abfd
)
887 struct elf_link_hash_table
*ret
;
889 ret
= ((struct elf_link_hash_table
*)
890 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
891 if (ret
== (struct elf_link_hash_table
*) NULL
)
894 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
896 bfd_release (abfd
, ret
);
903 /* This is a hook for the ELF emulation code in the generic linker to
904 tell the backend linker what file name to use for the DT_NEEDED
905 entry for a dynamic object. The generic linker passes name as an
906 empty string to indicate that no DT_NEEDED entry should be made. */
909 bfd_elf_set_dt_needed_name (abfd
, name
)
913 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
914 && bfd_get_format (abfd
) == bfd_object
)
915 elf_dt_name (abfd
) = name
;
918 /* Get the list of DT_NEEDED entries for a link. This is a hook for
919 the linker ELF emulation code. */
921 struct bfd_link_needed_list
*
922 bfd_elf_get_needed_list (abfd
, info
)
924 struct bfd_link_info
*info
;
926 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
928 return elf_hash_table (info
)->needed
;
931 /* Get the name actually used for a dynamic object for a link. This
932 is the SONAME entry if there is one. Otherwise, it is the string
933 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
936 bfd_elf_get_dt_soname (abfd
)
939 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
940 && bfd_get_format (abfd
) == bfd_object
)
941 return elf_dt_name (abfd
);
945 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
946 the ELF linker emulation code. */
949 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
951 struct bfd_link_needed_list
**pneeded
;
954 bfd_byte
*dynbuf
= NULL
;
957 bfd_byte
*extdyn
, *extdynend
;
959 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
963 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
964 || bfd_get_format (abfd
) != bfd_object
)
967 s
= bfd_get_section_by_name (abfd
, ".dynamic");
968 if (s
== NULL
|| s
->_raw_size
== 0)
971 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
975 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
979 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
983 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
985 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
986 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
989 extdynend
= extdyn
+ s
->_raw_size
;
990 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
992 Elf_Internal_Dyn dyn
;
994 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
996 if (dyn
.d_tag
== DT_NULL
)
999 if (dyn
.d_tag
== DT_NEEDED
)
1002 struct bfd_link_needed_list
*l
;
1004 string
= bfd_elf_string_from_elf_section (abfd
, link
,
1009 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, sizeof *l
);
1030 /* Allocate an ELF string table--force the first byte to be zero. */
1032 struct bfd_strtab_hash
*
1033 _bfd_elf_stringtab_init ()
1035 struct bfd_strtab_hash
*ret
;
1037 ret
= _bfd_stringtab_init ();
1042 loc
= _bfd_stringtab_add (ret
, "", true, false);
1043 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1044 if (loc
== (bfd_size_type
) -1)
1046 _bfd_stringtab_free (ret
);
1053 /* ELF .o/exec file reading */
1055 /* Create a new bfd section from an ELF section header. */
1058 bfd_section_from_shdr (abfd
, shindex
)
1060 unsigned int shindex
;
1062 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1063 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1064 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1067 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1069 switch (hdr
->sh_type
)
1072 /* Inactive section. Throw it away. */
1075 case SHT_PROGBITS
: /* Normal section with contents. */
1076 case SHT_DYNAMIC
: /* Dynamic linking information. */
1077 case SHT_NOBITS
: /* .bss section. */
1078 case SHT_HASH
: /* .hash section. */
1079 case SHT_NOTE
: /* .note section. */
1080 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1082 case SHT_SYMTAB
: /* A symbol table */
1083 if (elf_onesymtab (abfd
) == shindex
)
1086 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1087 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1088 elf_onesymtab (abfd
) = shindex
;
1089 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1090 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1091 abfd
->flags
|= HAS_SYMS
;
1093 /* Sometimes a shared object will map in the symbol table. If
1094 SHF_ALLOC is set, and this is a shared object, then we also
1095 treat this section as a BFD section. We can not base the
1096 decision purely on SHF_ALLOC, because that flag is sometimes
1097 set in a relocateable object file, which would confuse the
1099 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1100 && (abfd
->flags
& DYNAMIC
) != 0
1101 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1106 case SHT_DYNSYM
: /* A dynamic symbol table */
1107 if (elf_dynsymtab (abfd
) == shindex
)
1110 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1111 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1112 elf_dynsymtab (abfd
) = shindex
;
1113 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1114 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1115 abfd
->flags
|= HAS_SYMS
;
1117 /* Besides being a symbol table, we also treat this as a regular
1118 section, so that objcopy can handle it. */
1119 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1121 case SHT_STRTAB
: /* A string table */
1122 if (hdr
->bfd_section
!= NULL
)
1124 if (ehdr
->e_shstrndx
== shindex
)
1126 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1127 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1133 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1135 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1136 if (hdr2
->sh_link
== shindex
)
1138 if (! bfd_section_from_shdr (abfd
, i
))
1140 if (elf_onesymtab (abfd
) == i
)
1142 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1143 elf_elfsections (abfd
)[shindex
] =
1144 &elf_tdata (abfd
)->strtab_hdr
;
1147 if (elf_dynsymtab (abfd
) == i
)
1149 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1150 elf_elfsections (abfd
)[shindex
] = hdr
=
1151 &elf_tdata (abfd
)->dynstrtab_hdr
;
1152 /* We also treat this as a regular section, so
1153 that objcopy can handle it. */
1156 #if 0 /* Not handling other string tables specially right now. */
1157 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1158 /* We have a strtab for some random other section. */
1159 newsect
= (asection
*) hdr2
->bfd_section
;
1162 hdr
->bfd_section
= newsect
;
1163 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1165 elf_elfsections (abfd
)[shindex
] = hdr2
;
1171 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1175 /* *These* do a lot of work -- but build no sections! */
1177 asection
*target_sect
;
1178 Elf_Internal_Shdr
*hdr2
;
1180 /* For some incomprehensible reason Oracle distributes
1181 libraries for Solaris in which some of the objects have
1182 bogus sh_link fields. It would be nice if we could just
1183 reject them, but, unfortunately, some people need to use
1184 them. We scan through the section headers; if we find only
1185 one suitable symbol table, we clobber the sh_link to point
1186 to it. I hope this doesn't break anything. */
1187 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1188 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1194 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1196 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1197 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1208 hdr
->sh_link
= found
;
1211 /* Get the symbol table. */
1212 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1213 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1216 /* If this reloc section does not use the main symbol table we
1217 don't treat it as a reloc section. BFD can't adequately
1218 represent such a section, so at least for now, we don't
1219 try. We just present it as a normal section. */
1220 if (hdr
->sh_link
!= elf_onesymtab (abfd
))
1221 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1223 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1225 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1226 if (target_sect
== NULL
)
1229 if ((target_sect
->flags
& SEC_RELOC
) == 0
1230 || target_sect
->reloc_count
== 0)
1231 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1234 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1235 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
1236 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1239 elf_elfsections (abfd
)[shindex
] = hdr2
;
1240 target_sect
->reloc_count
+= hdr
->sh_size
/ hdr
->sh_entsize
;
1241 target_sect
->flags
|= SEC_RELOC
;
1242 target_sect
->relocation
= NULL
;
1243 target_sect
->rel_filepos
= hdr
->sh_offset
;
1244 abfd
->flags
|= HAS_RELOC
;
1249 case SHT_GNU_verdef
:
1250 elf_dynverdef (abfd
) = shindex
;
1251 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1252 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1255 case SHT_GNU_versym
:
1256 elf_dynversym (abfd
) = shindex
;
1257 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1258 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1261 case SHT_GNU_verneed
:
1262 elf_dynverref (abfd
) = shindex
;
1263 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1264 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1271 /* Check for any processor-specific section types. */
1273 if (bed
->elf_backend_section_from_shdr
)
1274 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1282 /* Given an ELF section number, retrieve the corresponding BFD
1286 bfd_section_from_elf_index (abfd
, index
)
1290 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1291 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1293 return elf_elfsections (abfd
)[index
]->bfd_section
;
1297 _bfd_elf_new_section_hook (abfd
, sec
)
1301 struct bfd_elf_section_data
*sdata
;
1303 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
1306 sec
->used_by_bfd
= (PTR
) sdata
;
1307 memset (sdata
, 0, sizeof (*sdata
));
1311 /* Create a new bfd section from an ELF program header.
1313 Since program segments have no names, we generate a synthetic name
1314 of the form segment<NUM>, where NUM is generally the index in the
1315 program header table. For segments that are split (see below) we
1316 generate the names segment<NUM>a and segment<NUM>b.
1318 Note that some program segments may have a file size that is different than
1319 (less than) the memory size. All this means is that at execution the
1320 system must allocate the amount of memory specified by the memory size,
1321 but only initialize it with the first "file size" bytes read from the
1322 file. This would occur for example, with program segments consisting
1323 of combined data+bss.
1325 To handle the above situation, this routine generates TWO bfd sections
1326 for the single program segment. The first has the length specified by
1327 the file size of the segment, and the second has the length specified
1328 by the difference between the two sizes. In effect, the segment is split
1329 into it's initialized and uninitialized parts.
1334 bfd_section_from_phdr (abfd
, hdr
, index
)
1336 Elf_Internal_Phdr
*hdr
;
1344 split
= ((hdr
->p_memsz
> 0) &&
1345 (hdr
->p_filesz
> 0) &&
1346 (hdr
->p_memsz
> hdr
->p_filesz
));
1347 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
1348 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1351 strcpy (name
, namebuf
);
1352 newsect
= bfd_make_section (abfd
, name
);
1353 if (newsect
== NULL
)
1355 newsect
->vma
= hdr
->p_vaddr
;
1356 newsect
->lma
= hdr
->p_paddr
;
1357 newsect
->_raw_size
= hdr
->p_filesz
;
1358 newsect
->filepos
= hdr
->p_offset
;
1359 newsect
->flags
|= SEC_HAS_CONTENTS
;
1360 if (hdr
->p_type
== PT_LOAD
)
1362 newsect
->flags
|= SEC_ALLOC
;
1363 newsect
->flags
|= SEC_LOAD
;
1364 if (hdr
->p_flags
& PF_X
)
1366 /* FIXME: all we known is that it has execute PERMISSION,
1368 newsect
->flags
|= SEC_CODE
;
1371 if (!(hdr
->p_flags
& PF_W
))
1373 newsect
->flags
|= SEC_READONLY
;
1378 sprintf (namebuf
, "segment%db", index
);
1379 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1382 strcpy (name
, namebuf
);
1383 newsect
= bfd_make_section (abfd
, name
);
1384 if (newsect
== NULL
)
1386 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1387 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1388 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1389 if (hdr
->p_type
== PT_LOAD
)
1391 newsect
->flags
|= SEC_ALLOC
;
1392 if (hdr
->p_flags
& PF_X
)
1393 newsect
->flags
|= SEC_CODE
;
1395 if (!(hdr
->p_flags
& PF_W
))
1396 newsect
->flags
|= SEC_READONLY
;
1402 /* Set up an ELF internal section header for a section. */
1406 elf_fake_sections (abfd
, asect
, failedptrarg
)
1411 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1412 boolean
*failedptr
= (boolean
*) failedptrarg
;
1413 Elf_Internal_Shdr
*this_hdr
;
1417 /* We already failed; just get out of the bfd_map_over_sections
1422 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1424 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1427 if (this_hdr
->sh_name
== (unsigned long) -1)
1433 this_hdr
->sh_flags
= 0;
1435 if ((asect
->flags
& SEC_ALLOC
) != 0
1436 || asect
->user_set_vma
)
1437 this_hdr
->sh_addr
= asect
->vma
;
1439 this_hdr
->sh_addr
= 0;
1441 this_hdr
->sh_offset
= 0;
1442 this_hdr
->sh_size
= asect
->_raw_size
;
1443 this_hdr
->sh_link
= 0;
1444 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1445 /* The sh_entsize and sh_info fields may have been set already by
1446 copy_private_section_data. */
1448 this_hdr
->bfd_section
= asect
;
1449 this_hdr
->contents
= NULL
;
1451 /* FIXME: This should not be based on section names. */
1452 if (strcmp (asect
->name
, ".dynstr") == 0)
1453 this_hdr
->sh_type
= SHT_STRTAB
;
1454 else if (strcmp (asect
->name
, ".hash") == 0)
1456 this_hdr
->sh_type
= SHT_HASH
;
1457 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
1459 else if (strcmp (asect
->name
, ".dynsym") == 0)
1461 this_hdr
->sh_type
= SHT_DYNSYM
;
1462 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1464 else if (strcmp (asect
->name
, ".dynamic") == 0)
1466 this_hdr
->sh_type
= SHT_DYNAMIC
;
1467 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1469 else if (strncmp (asect
->name
, ".rela", 5) == 0
1470 && get_elf_backend_data (abfd
)->use_rela_p
)
1472 this_hdr
->sh_type
= SHT_RELA
;
1473 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1475 else if (strncmp (asect
->name
, ".rel", 4) == 0
1476 && ! get_elf_backend_data (abfd
)->use_rela_p
)
1478 this_hdr
->sh_type
= SHT_REL
;
1479 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1481 else if (strncmp (asect
->name
, ".note", 5) == 0)
1482 this_hdr
->sh_type
= SHT_NOTE
;
1483 else if (strncmp (asect
->name
, ".stab", 5) == 0
1484 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1485 this_hdr
->sh_type
= SHT_STRTAB
;
1486 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1488 this_hdr
->sh_type
= SHT_GNU_versym
;
1489 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1491 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1493 this_hdr
->sh_type
= SHT_GNU_verdef
;
1494 this_hdr
->sh_entsize
= 0;
1495 /* objcopy or strip will copy over sh_info, but may not set
1496 cverdefs. The linker will set cverdefs, but sh_info will be
1498 if (this_hdr
->sh_info
== 0)
1499 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1501 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1502 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1504 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1506 this_hdr
->sh_type
= SHT_GNU_verneed
;
1507 this_hdr
->sh_entsize
= 0;
1508 /* objcopy or strip will copy over sh_info, but may not set
1509 cverrefs. The linker will set cverrefs, but sh_info will be
1511 if (this_hdr
->sh_info
== 0)
1512 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1514 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1515 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1517 else if ((asect
->flags
& SEC_ALLOC
) != 0
1518 && (asect
->flags
& SEC_LOAD
) != 0)
1519 this_hdr
->sh_type
= SHT_PROGBITS
;
1520 else if ((asect
->flags
& SEC_ALLOC
) != 0
1521 && ((asect
->flags
& SEC_LOAD
) == 0))
1522 this_hdr
->sh_type
= SHT_NOBITS
;
1526 this_hdr
->sh_type
= SHT_PROGBITS
;
1529 if ((asect
->flags
& SEC_ALLOC
) != 0)
1530 this_hdr
->sh_flags
|= SHF_ALLOC
;
1531 if ((asect
->flags
& SEC_READONLY
) == 0)
1532 this_hdr
->sh_flags
|= SHF_WRITE
;
1533 if ((asect
->flags
& SEC_CODE
) != 0)
1534 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1536 /* Check for processor-specific section types. */
1538 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1540 if (bed
->elf_backend_fake_sections
)
1541 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1544 /* If the section has relocs, set up a section header for the
1545 SHT_REL[A] section. */
1546 if ((asect
->flags
& SEC_RELOC
) != 0)
1548 Elf_Internal_Shdr
*rela_hdr
;
1549 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1552 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1553 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1559 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1561 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1563 if (rela_hdr
->sh_name
== (unsigned int) -1)
1568 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1569 rela_hdr
->sh_entsize
= (use_rela_p
1570 ? bed
->s
->sizeof_rela
1571 : bed
->s
->sizeof_rel
);
1572 rela_hdr
->sh_addralign
= bed
->s
->file_align
;
1573 rela_hdr
->sh_flags
= 0;
1574 rela_hdr
->sh_addr
= 0;
1575 rela_hdr
->sh_size
= 0;
1576 rela_hdr
->sh_offset
= 0;
1580 /* Assign all ELF section numbers. The dummy first section is handled here
1581 too. The link/info pointers for the standard section types are filled
1582 in here too, while we're at it. */
1585 assign_section_numbers (abfd
)
1588 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1590 unsigned int section_number
;
1591 Elf_Internal_Shdr
**i_shdrp
;
1592 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1596 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1598 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1600 d
->this_idx
= section_number
++;
1601 if ((sec
->flags
& SEC_RELOC
) == 0)
1604 d
->rel_idx
= section_number
++;
1607 t
->shstrtab_section
= section_number
++;
1608 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1609 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1611 if (abfd
->symcount
> 0)
1613 t
->symtab_section
= section_number
++;
1614 t
->strtab_section
= section_number
++;
1617 elf_elfheader (abfd
)->e_shnum
= section_number
;
1619 /* Set up the list of section header pointers, in agreement with the
1621 i_shdrp
= ((Elf_Internal_Shdr
**)
1622 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1623 if (i_shdrp
== NULL
)
1626 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1627 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1628 if (i_shdrp
[0] == NULL
)
1630 bfd_release (abfd
, i_shdrp
);
1633 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1635 elf_elfsections (abfd
) = i_shdrp
;
1637 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1638 if (abfd
->symcount
> 0)
1640 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1641 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1642 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1644 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1646 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1650 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1651 if (d
->rel_idx
!= 0)
1652 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1654 /* Fill in the sh_link and sh_info fields while we're at it. */
1656 /* sh_link of a reloc section is the section index of the symbol
1657 table. sh_info is the section index of the section to which
1658 the relocation entries apply. */
1659 if (d
->rel_idx
!= 0)
1661 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1662 d
->rel_hdr
.sh_info
= d
->this_idx
;
1665 switch (d
->this_hdr
.sh_type
)
1669 /* A reloc section which we are treating as a normal BFD
1670 section. sh_link is the section index of the symbol
1671 table. sh_info is the section index of the section to
1672 which the relocation entries apply. We assume that an
1673 allocated reloc section uses the dynamic symbol table.
1674 FIXME: How can we be sure? */
1675 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1677 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1679 /* We look up the section the relocs apply to by name. */
1681 if (d
->this_hdr
.sh_type
== SHT_REL
)
1685 s
= bfd_get_section_by_name (abfd
, name
);
1687 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1691 /* We assume that a section named .stab*str is a stabs
1692 string section. We look for a section with the same name
1693 but without the trailing ``str'', and set its sh_link
1694 field to point to this section. */
1695 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1696 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1701 len
= strlen (sec
->name
);
1702 alc
= (char *) bfd_malloc (len
- 2);
1705 strncpy (alc
, sec
->name
, len
- 3);
1706 alc
[len
- 3] = '\0';
1707 s
= bfd_get_section_by_name (abfd
, alc
);
1711 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1713 /* This is a .stab section. */
1714 elf_section_data (s
)->this_hdr
.sh_entsize
=
1715 4 + 2 * (bed
->s
->arch_size
/ 8);
1722 case SHT_GNU_verneed
:
1723 case SHT_GNU_verdef
:
1724 /* sh_link is the section header index of the string table
1725 used for the dynamic entries, or the symbol table, or the
1727 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1729 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1733 case SHT_GNU_versym
:
1734 /* sh_link is the section header index of the symbol table
1735 this hash table or version table is for. */
1736 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1738 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1746 /* Map symbol from it's internal number to the external number, moving
1747 all local symbols to be at the head of the list. */
1750 sym_is_global (abfd
, sym
)
1754 /* If the backend has a special mapping, use it. */
1755 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1756 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1759 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1760 || bfd_is_und_section (bfd_get_section (sym
))
1761 || bfd_is_com_section (bfd_get_section (sym
)));
1765 elf_map_symbols (abfd
)
1768 int symcount
= bfd_get_symcount (abfd
);
1769 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1770 asymbol
**sect_syms
;
1772 int num_globals
= 0;
1773 int num_locals2
= 0;
1774 int num_globals2
= 0;
1776 int num_sections
= 0;
1782 fprintf (stderr
, "elf_map_symbols\n");
1786 /* Add a section symbol for each BFD section. FIXME: Is this really
1788 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1790 if (max_index
< asect
->index
)
1791 max_index
= asect
->index
;
1795 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1796 if (sect_syms
== NULL
)
1798 elf_section_syms (abfd
) = sect_syms
;
1800 for (idx
= 0; idx
< symcount
; idx
++)
1802 if ((syms
[idx
]->flags
& BSF_SECTION_SYM
) != 0
1803 && syms
[idx
]->value
== 0)
1807 sec
= syms
[idx
]->section
;
1808 if (sec
->owner
!= NULL
)
1810 if (sec
->owner
!= abfd
)
1812 if (sec
->output_offset
!= 0)
1814 sec
= sec
->output_section
;
1815 BFD_ASSERT (sec
->owner
== abfd
);
1817 sect_syms
[sec
->index
] = syms
[idx
];
1822 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1826 if (sect_syms
[asect
->index
] != NULL
)
1829 sym
= bfd_make_empty_symbol (abfd
);
1832 sym
->the_bfd
= abfd
;
1833 sym
->name
= asect
->name
;
1835 /* Set the flags to 0 to indicate that this one was newly added. */
1837 sym
->section
= asect
;
1838 sect_syms
[asect
->index
] = sym
;
1842 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
1843 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1847 /* Classify all of the symbols. */
1848 for (idx
= 0; idx
< symcount
; idx
++)
1850 if (!sym_is_global (abfd
, syms
[idx
]))
1855 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1857 if (sect_syms
[asect
->index
] != NULL
1858 && sect_syms
[asect
->index
]->flags
== 0)
1860 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
1861 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
1865 sect_syms
[asect
->index
]->flags
= 0;
1869 /* Now sort the symbols so the local symbols are first. */
1870 new_syms
= ((asymbol
**)
1872 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
1873 if (new_syms
== NULL
)
1876 for (idx
= 0; idx
< symcount
; idx
++)
1878 asymbol
*sym
= syms
[idx
];
1881 if (!sym_is_global (abfd
, sym
))
1884 i
= num_locals
+ num_globals2
++;
1886 sym
->udata
.i
= i
+ 1;
1888 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1890 if (sect_syms
[asect
->index
] != NULL
1891 && sect_syms
[asect
->index
]->flags
== 0)
1893 asymbol
*sym
= sect_syms
[asect
->index
];
1896 sym
->flags
= BSF_SECTION_SYM
;
1897 if (!sym_is_global (abfd
, sym
))
1900 i
= num_locals
+ num_globals2
++;
1902 sym
->udata
.i
= i
+ 1;
1906 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
1908 elf_num_locals (abfd
) = num_locals
;
1909 elf_num_globals (abfd
) = num_globals
;
1913 /* Align to the maximum file alignment that could be required for any
1914 ELF data structure. */
1916 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
1917 static INLINE file_ptr
1918 align_file_position (off
, align
)
1922 return (off
+ align
- 1) & ~(align
- 1);
1925 /* Assign a file position to a section, optionally aligning to the
1926 required section alignment. */
1929 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
1930 Elf_Internal_Shdr
*i_shdrp
;
1938 al
= i_shdrp
->sh_addralign
;
1940 offset
= BFD_ALIGN (offset
, al
);
1942 i_shdrp
->sh_offset
= offset
;
1943 if (i_shdrp
->bfd_section
!= NULL
)
1944 i_shdrp
->bfd_section
->filepos
= offset
;
1945 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1946 offset
+= i_shdrp
->sh_size
;
1950 /* Compute the file positions we are going to put the sections at, and
1951 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1952 is not NULL, this is being called by the ELF backend linker. */
1955 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
1957 struct bfd_link_info
*link_info
;
1959 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1961 struct bfd_strtab_hash
*strtab
;
1962 Elf_Internal_Shdr
*shstrtab_hdr
;
1964 if (abfd
->output_has_begun
)
1967 /* Do any elf backend specific processing first. */
1968 if (bed
->elf_backend_begin_write_processing
)
1969 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
1971 if (! prep_headers (abfd
))
1975 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
1979 if (!assign_section_numbers (abfd
))
1982 /* The backend linker builds symbol table information itself. */
1983 if (link_info
== NULL
&& abfd
->symcount
> 0)
1985 if (! swap_out_syms (abfd
, &strtab
))
1989 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
1990 /* sh_name was set in prep_headers. */
1991 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
1992 shstrtab_hdr
->sh_flags
= 0;
1993 shstrtab_hdr
->sh_addr
= 0;
1994 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1995 shstrtab_hdr
->sh_entsize
= 0;
1996 shstrtab_hdr
->sh_link
= 0;
1997 shstrtab_hdr
->sh_info
= 0;
1998 /* sh_offset is set in assign_file_positions_except_relocs. */
1999 shstrtab_hdr
->sh_addralign
= 1;
2001 if (!assign_file_positions_except_relocs (abfd
))
2004 if (link_info
== NULL
&& abfd
->symcount
> 0)
2007 Elf_Internal_Shdr
*hdr
;
2009 off
= elf_tdata (abfd
)->next_file_pos
;
2011 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2012 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2014 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2015 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2017 elf_tdata (abfd
)->next_file_pos
= off
;
2019 /* Now that we know where the .strtab section goes, write it
2021 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2022 || ! _bfd_stringtab_emit (abfd
, strtab
))
2024 _bfd_stringtab_free (strtab
);
2027 abfd
->output_has_begun
= true;
2032 /* Create a mapping from a set of sections to a program segment. */
2034 static INLINE
struct elf_segment_map
*
2035 make_mapping (abfd
, sections
, from
, to
, phdr
)
2037 asection
**sections
;
2042 struct elf_segment_map
*m
;
2046 m
= ((struct elf_segment_map
*)
2048 (sizeof (struct elf_segment_map
)
2049 + (to
- from
- 1) * sizeof (asection
*))));
2053 m
->p_type
= PT_LOAD
;
2054 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2055 m
->sections
[i
- from
] = *hdrpp
;
2056 m
->count
= to
- from
;
2058 if (from
== 0 && phdr
)
2060 /* Include the headers in the first PT_LOAD segment. */
2061 m
->includes_filehdr
= 1;
2062 m
->includes_phdrs
= 1;
2068 /* Set up a mapping from BFD sections to program segments. */
2071 map_sections_to_segments (abfd
)
2074 asection
**sections
= NULL
;
2078 struct elf_segment_map
*mfirst
;
2079 struct elf_segment_map
**pm
;
2080 struct elf_segment_map
*m
;
2082 unsigned int phdr_index
;
2083 bfd_vma maxpagesize
;
2085 boolean phdr_in_section
= true;
2089 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2092 if (bfd_count_sections (abfd
) == 0)
2095 /* Select the allocated sections, and sort them. */
2097 sections
= (asection
**) bfd_malloc (bfd_count_sections (abfd
)
2098 * sizeof (asection
*));
2099 if (sections
== NULL
)
2103 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2105 if ((s
->flags
& SEC_ALLOC
) != 0)
2111 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2114 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2116 /* Build the mapping. */
2121 /* If we have a .interp section, then create a PT_PHDR segment for
2122 the program headers and a PT_INTERP segment for the .interp
2124 s
= bfd_get_section_by_name (abfd
, ".interp");
2125 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2127 m
= ((struct elf_segment_map
*)
2128 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2132 m
->p_type
= PT_PHDR
;
2133 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2134 m
->p_flags
= PF_R
| PF_X
;
2135 m
->p_flags_valid
= 1;
2136 m
->includes_phdrs
= 1;
2141 m
= ((struct elf_segment_map
*)
2142 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2146 m
->p_type
= PT_INTERP
;
2154 /* Look through the sections. We put sections in the same program
2155 segment when the start of the second section can be placed within
2156 a few bytes of the end of the first section. */
2159 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2161 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2163 && (dynsec
->flags
& SEC_LOAD
) == 0)
2166 /* Deal with -Ttext or something similar such that the first section
2167 is not adjacent to the program headers. This is an
2168 approximation, since at this point we don't know exactly how many
2169 program headers we will need. */
2172 bfd_size_type phdr_size
;
2174 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2176 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2177 if ((abfd
->flags
& D_PAGED
) == 0
2178 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2179 phdr_in_section
= false;
2182 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2185 boolean new_segment
;
2189 /* See if this section and the last one will fit in the same
2192 if (last_hdr
== NULL
)
2194 /* If we don't have a segment yet, then we don't need a new
2195 one (we build the last one after this loop). */
2196 new_segment
= false;
2198 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2200 /* If this section has a different relation between the
2201 virtual address and the load address, then we need a new
2205 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2206 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2208 /* If putting this section in this segment would force us to
2209 skip a page in the segment, then we need a new segment. */
2212 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2213 && (hdr
->flags
& SEC_LOAD
) != 0)
2215 /* We don't want to put a loadable section after a
2216 nonloadable section in the same segment. */
2219 else if ((abfd
->flags
& D_PAGED
) == 0)
2221 /* If the file is not demand paged, which means that we
2222 don't require the sections to be correctly aligned in the
2223 file, then there is no other reason for a new segment. */
2224 new_segment
= false;
2227 && (hdr
->flags
& SEC_READONLY
) == 0
2228 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2231 /* We don't want to put a writable section in a read only
2232 segment, unless they are on the same page in memory
2233 anyhow. We already know that the last section does not
2234 bring us past the current section on the page, so the
2235 only case in which the new section is not on the same
2236 page as the previous section is when the previous section
2237 ends precisely on a page boundary. */
2242 /* Otherwise, we can use the same segment. */
2243 new_segment
= false;
2248 if ((hdr
->flags
& SEC_READONLY
) == 0)
2254 /* We need a new program segment. We must create a new program
2255 header holding all the sections from phdr_index until hdr. */
2257 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_section
);
2264 if ((hdr
->flags
& SEC_READONLY
) == 0)
2271 phdr_in_section
= false;
2274 /* Create a final PT_LOAD program segment. */
2275 if (last_hdr
!= NULL
)
2277 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_section
);
2285 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2288 m
= ((struct elf_segment_map
*)
2289 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2293 m
->p_type
= PT_DYNAMIC
;
2295 m
->sections
[0] = dynsec
;
2301 /* For each loadable .note section, add a PT_NOTE segment. We don't
2302 use bfd_get_section_by_name, because if we link together
2303 nonloadable .note sections and loadable .note sections, we will
2304 generate two .note sections in the output file. FIXME: Using
2305 names for section types is bogus anyhow. */
2306 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2308 if ((s
->flags
& SEC_LOAD
) != 0
2309 && strncmp (s
->name
, ".note", 5) == 0)
2311 m
= ((struct elf_segment_map
*)
2312 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2316 m
->p_type
= PT_NOTE
;
2328 elf_tdata (abfd
)->segment_map
= mfirst
;
2332 if (sections
!= NULL
)
2337 /* Sort sections by VMA. */
2340 elf_sort_sections (arg1
, arg2
)
2344 const asection
*sec1
= *(const asection
**) arg1
;
2345 const asection
*sec2
= *(const asection
**) arg2
;
2347 /* Sort by LMA first, since this is the address used to
2348 place the section into a segment. */
2349 if (sec1
->lma
< sec2
->lma
)
2351 else if (sec1
->lma
> sec2
->lma
)
2354 /* Sort by VMA. Normally the LMA and the VMA will be the same, and
2355 this will do nothing. */
2356 if (sec1
->vma
< sec2
->vma
)
2358 else if (sec1
->vma
> sec2
->vma
)
2361 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2363 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2368 return sec1
->target_index
- sec2
->target_index
;
2378 /* Sort by size, to put zero sized sections before others at the
2381 if (sec1
->_raw_size
< sec2
->_raw_size
)
2383 if (sec1
->_raw_size
> sec2
->_raw_size
)
2386 return sec1
->target_index
- sec2
->target_index
;
2389 /* Assign file positions to the sections based on the mapping from
2390 sections to segments. This function also sets up some fields in
2391 the file header, and writes out the program headers. */
2394 assign_file_positions_for_segments (abfd
)
2397 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2399 struct elf_segment_map
*m
;
2401 Elf_Internal_Phdr
*phdrs
;
2403 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2404 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2405 Elf_Internal_Phdr
*p
;
2407 if (elf_tdata (abfd
)->segment_map
== NULL
)
2409 if (! map_sections_to_segments (abfd
))
2413 if (bed
->elf_backend_modify_segment_map
)
2415 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2420 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2423 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2424 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2425 elf_elfheader (abfd
)->e_phnum
= count
;
2430 /* If we already counted the number of program segments, make sure
2431 that we allocated enough space. This happens when SIZEOF_HEADERS
2432 is used in a linker script. */
2433 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2434 if (alloc
!= 0 && count
> alloc
)
2436 ((*_bfd_error_handler
)
2437 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2438 bfd_get_filename (abfd
), alloc
, count
));
2439 bfd_set_error (bfd_error_bad_value
);
2446 phdrs
= ((Elf_Internal_Phdr
*)
2447 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
2451 off
= bed
->s
->sizeof_ehdr
;
2452 off
+= alloc
* bed
->s
->sizeof_phdr
;
2458 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2465 /* If elf_segment_map is not from map_sections_to_segments, the
2466 sections may not be correctly ordered. */
2468 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2471 p
->p_type
= m
->p_type
;
2473 if (m
->p_flags_valid
)
2474 p
->p_flags
= m
->p_flags
;
2478 if (p
->p_type
== PT_LOAD
2480 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2482 if ((abfd
->flags
& D_PAGED
) != 0)
2483 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2485 off
+= ((m
->sections
[0]->vma
- off
)
2486 % (1 << bfd_get_section_alignment (abfd
, m
->sections
[0])));
2492 p
->p_vaddr
= m
->sections
[0]->vma
;
2494 if (m
->p_paddr_valid
)
2495 p
->p_paddr
= m
->p_paddr
;
2496 else if (m
->count
== 0)
2499 p
->p_paddr
= m
->sections
[0]->lma
;
2501 if (p
->p_type
== PT_LOAD
2502 && (abfd
->flags
& D_PAGED
) != 0)
2503 p
->p_align
= bed
->maxpagesize
;
2504 else if (m
->count
== 0)
2505 p
->p_align
= bed
->s
->file_align
;
2513 if (m
->includes_filehdr
)
2515 if (! m
->p_flags_valid
)
2518 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2519 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2522 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2524 if (p
->p_vaddr
< (bfd_vma
) off
)
2526 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2527 bfd_get_filename (abfd
));
2528 bfd_set_error (bfd_error_bad_value
);
2533 if (! m
->p_paddr_valid
)
2536 if (p
->p_type
== PT_LOAD
)
2538 filehdr_vaddr
= p
->p_vaddr
;
2539 filehdr_paddr
= p
->p_paddr
;
2543 if (m
->includes_phdrs
)
2545 if (! m
->p_flags_valid
)
2547 if (m
->includes_filehdr
)
2549 if (p
->p_type
== PT_LOAD
)
2551 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2552 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2557 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2560 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2561 p
->p_vaddr
-= off
- p
->p_offset
;
2562 if (! m
->p_paddr_valid
)
2563 p
->p_paddr
-= off
- p
->p_offset
;
2565 if (p
->p_type
== PT_LOAD
)
2567 phdrs_vaddr
= p
->p_vaddr
;
2568 phdrs_paddr
= p
->p_paddr
;
2571 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2572 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2575 if (p
->p_type
== PT_LOAD
)
2577 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2583 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2584 p
->p_filesz
+= adjust
;
2585 p
->p_memsz
+= adjust
;
2590 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2594 bfd_size_type align
;
2598 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2600 /* The section may have artificial alignment forced by a
2601 link script. Notice this case by the gap between the
2602 cumulative phdr vma and the section's vma. */
2603 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2605 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2607 p
->p_memsz
+= adjust
;
2610 if ((flags
& SEC_LOAD
) != 0)
2611 p
->p_filesz
+= adjust
;
2614 if (p
->p_type
== PT_LOAD
)
2618 if ((flags
& SEC_LOAD
) != 0)
2619 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2620 else if ((flags
& SEC_ALLOC
) != 0)
2622 /* The section VMA must equal the file position
2623 modulo the page size. FIXME: I'm not sure if
2624 this adjustment is really necessary. We used to
2625 not have the SEC_LOAD case just above, and then
2626 this was necessary, but now I'm not sure. */
2627 if ((abfd
->flags
& D_PAGED
) != 0)
2628 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2630 adjust
= (sec
->vma
- voff
) % align
;
2639 p
->p_memsz
+= adjust
;
2642 if ((flags
& SEC_LOAD
) != 0)
2643 p
->p_filesz
+= adjust
;
2648 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2649 used in a linker script we may have a section with
2650 SEC_LOAD clear but which is supposed to have
2652 if ((flags
& SEC_LOAD
) != 0
2653 || (flags
& SEC_HAS_CONTENTS
) != 0)
2654 off
+= sec
->_raw_size
;
2655 if ((flags
& SEC_ALLOC
) != 0)
2656 voff
+= sec
->_raw_size
;
2659 p
->p_memsz
+= sec
->_raw_size
;
2661 if ((flags
& SEC_LOAD
) != 0)
2662 p
->p_filesz
+= sec
->_raw_size
;
2664 if (align
> p
->p_align
)
2667 if (! m
->p_flags_valid
)
2670 if ((flags
& SEC_CODE
) != 0)
2672 if ((flags
& SEC_READONLY
) == 0)
2678 /* Now that we have set the section file positions, we can set up
2679 the file positions for the non PT_LOAD segments. */
2680 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2684 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
2686 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
2687 p
->p_offset
= m
->sections
[0]->filepos
;
2691 if (m
->includes_filehdr
)
2693 p
->p_vaddr
= filehdr_vaddr
;
2694 if (! m
->p_paddr_valid
)
2695 p
->p_paddr
= filehdr_paddr
;
2697 else if (m
->includes_phdrs
)
2699 p
->p_vaddr
= phdrs_vaddr
;
2700 if (! m
->p_paddr_valid
)
2701 p
->p_paddr
= phdrs_paddr
;
2706 /* Clear out any program headers we allocated but did not use. */
2707 for (; count
< alloc
; count
++, p
++)
2709 memset (p
, 0, sizeof *p
);
2710 p
->p_type
= PT_NULL
;
2713 elf_tdata (abfd
)->phdr
= phdrs
;
2715 elf_tdata (abfd
)->next_file_pos
= off
;
2717 /* Write out the program headers. */
2718 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
2719 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
2725 /* Get the size of the program header.
2727 If this is called by the linker before any of the section VMA's are set, it
2728 can't calculate the correct value for a strange memory layout. This only
2729 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2730 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2731 data segment (exclusive of .interp and .dynamic).
2733 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2734 will be two segments. */
2736 static bfd_size_type
2737 get_program_header_size (abfd
)
2742 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2744 /* We can't return a different result each time we're called. */
2745 if (elf_tdata (abfd
)->program_header_size
!= 0)
2746 return elf_tdata (abfd
)->program_header_size
;
2748 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2750 struct elf_segment_map
*m
;
2753 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2755 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
2756 return elf_tdata (abfd
)->program_header_size
;
2759 /* Assume we will need exactly two PT_LOAD segments: one for text
2760 and one for data. */
2763 s
= bfd_get_section_by_name (abfd
, ".interp");
2764 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2766 /* If we have a loadable interpreter section, we need a
2767 PT_INTERP segment. In this case, assume we also need a
2768 PT_PHDR segment, although that may not be true for all
2773 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
2775 /* We need a PT_DYNAMIC segment. */
2779 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2781 if ((s
->flags
& SEC_LOAD
) != 0
2782 && strncmp (s
->name
, ".note", 5) == 0)
2784 /* We need a PT_NOTE segment. */
2789 /* Let the backend count up any program headers it might need. */
2790 if (bed
->elf_backend_additional_program_headers
)
2794 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
2800 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
2801 return elf_tdata (abfd
)->program_header_size
;
2804 /* Work out the file positions of all the sections. This is called by
2805 _bfd_elf_compute_section_file_positions. All the section sizes and
2806 VMAs must be known before this is called.
2808 We do not consider reloc sections at this point, unless they form
2809 part of the loadable image. Reloc sections are assigned file
2810 positions in assign_file_positions_for_relocs, which is called by
2811 write_object_contents and final_link.
2813 We also don't set the positions of the .symtab and .strtab here. */
2816 assign_file_positions_except_relocs (abfd
)
2819 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
2820 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
2821 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
2823 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2825 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
2827 Elf_Internal_Shdr
**hdrpp
;
2830 /* Start after the ELF header. */
2831 off
= i_ehdrp
->e_ehsize
;
2833 /* We are not creating an executable, which means that we are
2834 not creating a program header, and that the actual order of
2835 the sections in the file is unimportant. */
2836 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2838 Elf_Internal_Shdr
*hdr
;
2841 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
2843 hdr
->sh_offset
= -1;
2846 if (i
== tdata
->symtab_section
2847 || i
== tdata
->strtab_section
)
2849 hdr
->sh_offset
= -1;
2853 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2859 Elf_Internal_Shdr
**hdrpp
;
2861 /* Assign file positions for the loaded sections based on the
2862 assignment of sections to segments. */
2863 if (! assign_file_positions_for_segments (abfd
))
2866 /* Assign file positions for the other sections. */
2868 off
= elf_tdata (abfd
)->next_file_pos
;
2869 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2871 Elf_Internal_Shdr
*hdr
;
2874 if (hdr
->bfd_section
!= NULL
2875 && hdr
->bfd_section
->filepos
!= 0)
2876 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
2877 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2879 ((*_bfd_error_handler
)
2880 (_("%s: warning: allocated section `%s' not in segment"),
2881 bfd_get_filename (abfd
),
2882 (hdr
->bfd_section
== NULL
2884 : hdr
->bfd_section
->name
)));
2885 if ((abfd
->flags
& D_PAGED
) != 0)
2886 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
2888 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
2889 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
2892 else if (hdr
->sh_type
== SHT_REL
2893 || hdr
->sh_type
== SHT_RELA
2894 || hdr
== i_shdrpp
[tdata
->symtab_section
]
2895 || hdr
== i_shdrpp
[tdata
->strtab_section
])
2896 hdr
->sh_offset
= -1;
2898 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2902 /* Place the section headers. */
2903 off
= align_file_position (off
, bed
->s
->file_align
);
2904 i_ehdrp
->e_shoff
= off
;
2905 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
2907 elf_tdata (abfd
)->next_file_pos
= off
;
2916 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2917 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2918 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2920 struct bfd_strtab_hash
*shstrtab
;
2921 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2923 i_ehdrp
= elf_elfheader (abfd
);
2924 i_shdrp
= elf_elfsections (abfd
);
2926 shstrtab
= _bfd_elf_stringtab_init ();
2927 if (shstrtab
== NULL
)
2930 elf_shstrtab (abfd
) = shstrtab
;
2932 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
2933 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
2934 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
2935 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
2937 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
2938 i_ehdrp
->e_ident
[EI_DATA
] =
2939 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
2940 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
2942 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2943 i_ehdrp
->e_ident
[count
] = 0;
2945 if ((abfd
->flags
& DYNAMIC
) != 0)
2946 i_ehdrp
->e_type
= ET_DYN
;
2947 else if ((abfd
->flags
& EXEC_P
) != 0)
2948 i_ehdrp
->e_type
= ET_EXEC
;
2950 i_ehdrp
->e_type
= ET_REL
;
2952 switch (bfd_get_arch (abfd
))
2954 case bfd_arch_unknown
:
2955 i_ehdrp
->e_machine
= EM_NONE
;
2957 case bfd_arch_sparc
:
2958 if (bed
->s
->arch_size
== 64)
2959 i_ehdrp
->e_machine
= EM_SPARCV9
;
2961 i_ehdrp
->e_machine
= EM_SPARC
;
2964 i_ehdrp
->e_machine
= EM_386
;
2967 i_ehdrp
->e_machine
= EM_68K
;
2970 i_ehdrp
->e_machine
= EM_88K
;
2973 i_ehdrp
->e_machine
= EM_860
;
2975 case bfd_arch_mips
: /* MIPS Rxxxx */
2976 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
2979 i_ehdrp
->e_machine
= EM_PARISC
;
2981 case bfd_arch_powerpc
:
2982 i_ehdrp
->e_machine
= EM_PPC
;
2984 case bfd_arch_alpha
:
2985 i_ehdrp
->e_machine
= EM_ALPHA
;
2988 i_ehdrp
->e_machine
= EM_SH
;
2991 i_ehdrp
->e_machine
= EM_CYGNUS_D10V
;
2993 /* start-sanitize-d30v */
2995 i_ehdrp
->e_machine
= EM_CYGNUS_D30V
;
2997 /* end-sanitize-d30v */
2999 switch (bfd_get_mach (abfd
))
3002 case 0: i_ehdrp
->e_machine
= EM_CYGNUS_V850
; break;
3006 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
3009 i_ehdrp
->e_machine
= EM_CYGNUS_M32R
;
3011 case bfd_arch_mn10200
:
3012 i_ehdrp
->e_machine
= EM_CYGNUS_MN10200
;
3014 case bfd_arch_mn10300
:
3015 i_ehdrp
->e_machine
= EM_CYGNUS_MN10300
;
3017 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3019 i_ehdrp
->e_machine
= EM_NONE
;
3021 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3022 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3024 /* no program header, for now. */
3025 i_ehdrp
->e_phoff
= 0;
3026 i_ehdrp
->e_phentsize
= 0;
3027 i_ehdrp
->e_phnum
= 0;
3029 /* each bfd section is section header entry */
3030 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3031 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3033 /* if we're building an executable, we'll need a program header table */
3034 if (abfd
->flags
& EXEC_P
)
3036 /* it all happens later */
3038 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3040 /* elf_build_phdrs() returns a (NULL-terminated) array of
3041 Elf_Internal_Phdrs */
3042 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3043 i_ehdrp
->e_phoff
= outbase
;
3044 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3049 i_ehdrp
->e_phentsize
= 0;
3051 i_ehdrp
->e_phoff
= 0;
3054 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3055 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3056 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3057 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3058 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3059 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3060 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3061 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3062 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3068 /* Assign file positions for all the reloc sections which are not part
3069 of the loadable file image. */
3072 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3077 Elf_Internal_Shdr
**shdrpp
;
3079 off
= elf_tdata (abfd
)->next_file_pos
;
3081 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3082 i
< elf_elfheader (abfd
)->e_shnum
;
3085 Elf_Internal_Shdr
*shdrp
;
3088 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3089 && shdrp
->sh_offset
== -1)
3090 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3093 elf_tdata (abfd
)->next_file_pos
= off
;
3097 _bfd_elf_write_object_contents (abfd
)
3100 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3101 Elf_Internal_Ehdr
*i_ehdrp
;
3102 Elf_Internal_Shdr
**i_shdrp
;
3106 if (! abfd
->output_has_begun
3107 && ! _bfd_elf_compute_section_file_positions (abfd
,
3108 (struct bfd_link_info
*) NULL
))
3111 i_shdrp
= elf_elfsections (abfd
);
3112 i_ehdrp
= elf_elfheader (abfd
);
3115 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3118 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3120 /* After writing the headers, we need to write the sections too... */
3121 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3123 if (bed
->elf_backend_section_processing
)
3124 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3125 if (i_shdrp
[count
]->contents
)
3127 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3128 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
3130 != i_shdrp
[count
]->sh_size
))
3135 /* Write out the section header names. */
3136 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3137 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3140 if (bed
->elf_backend_final_write_processing
)
3141 (*bed
->elf_backend_final_write_processing
) (abfd
,
3142 elf_tdata (abfd
)->linker
);
3144 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3147 /* given a section, search the header to find them... */
3149 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3153 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3154 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3156 Elf_Internal_Shdr
*hdr
;
3157 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3159 for (index
= 0; index
< maxindex
; index
++)
3161 hdr
= i_shdrp
[index
];
3162 if (hdr
->bfd_section
== asect
)
3166 if (bed
->elf_backend_section_from_bfd_section
)
3168 for (index
= 0; index
< maxindex
; index
++)
3172 hdr
= i_shdrp
[index
];
3174 if ((*bed
->elf_backend_section_from_bfd_section
)
3175 (abfd
, hdr
, asect
, &retval
))
3180 if (bfd_is_abs_section (asect
))
3182 if (bfd_is_com_section (asect
))
3184 if (bfd_is_und_section (asect
))
3190 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3194 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3196 asymbol
**asym_ptr_ptr
;
3198 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3200 flagword flags
= asym_ptr
->flags
;
3202 /* When gas creates relocations against local labels, it creates its
3203 own symbol for the section, but does put the symbol into the
3204 symbol chain, so udata is 0. When the linker is generating
3205 relocatable output, this section symbol may be for one of the
3206 input sections rather than the output section. */
3207 if (asym_ptr
->udata
.i
== 0
3208 && (flags
& BSF_SECTION_SYM
)
3209 && asym_ptr
->section
)
3213 if (asym_ptr
->section
->output_section
!= NULL
)
3214 indx
= asym_ptr
->section
->output_section
->index
;
3216 indx
= asym_ptr
->section
->index
;
3217 if (elf_section_syms (abfd
)[indx
])
3218 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3221 idx
= asym_ptr
->udata
.i
;
3225 /* This case can occur when using --strip-symbol on a symbol
3226 which is used in a relocation entry. */
3227 (*_bfd_error_handler
)
3228 (_("%s: symbol `%s' required but not present"),
3229 bfd_get_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3230 bfd_set_error (bfd_error_no_symbols
);
3237 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3238 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3239 elf_symbol_flags (flags
));
3247 /* Copy private BFD data. This copies any program header information. */
3250 copy_private_bfd_data (ibfd
, obfd
)
3254 Elf_Internal_Ehdr
*iehdr
;
3255 struct elf_segment_map
*mfirst
;
3256 struct elf_segment_map
**pm
;
3257 struct elf_segment_map
*m
;
3258 Elf_Internal_Phdr
*p
;
3261 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3262 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3265 if (elf_tdata (ibfd
)->phdr
== NULL
)
3268 iehdr
= elf_elfheader (ibfd
);
3273 c
= elf_elfheader (ibfd
)->e_phnum
;
3274 for (i
= 0, p
= elf_tdata (ibfd
)->phdr
; i
< c
; i
++, p
++)
3282 /* The complicated case when p_vaddr is 0 is to handle the
3283 Solaris linker, which generates a PT_INTERP section with
3284 p_vaddr and p_memsz set to 0. */
3285 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3286 if (((s
->vma
>= p
->p_vaddr
3287 && (s
->vma
+ s
->_raw_size
<= p
->p_vaddr
+ p
->p_memsz
3288 || s
->vma
+ s
->_raw_size
<= p
->p_vaddr
+ p
->p_filesz
))
3291 && (s
->flags
& SEC_HAS_CONTENTS
) != 0
3292 && (bfd_vma
) s
->filepos
>= p
->p_offset
3293 && ((bfd_vma
) s
->filepos
+ s
->_raw_size
3294 <= p
->p_offset
+ p
->p_filesz
)))
3295 && (s
->flags
& SEC_ALLOC
) != 0
3296 && s
->output_section
!= NULL
)
3299 m
= ((struct elf_segment_map
*)
3301 (sizeof (struct elf_segment_map
)
3302 + ((size_t) csecs
- 1) * sizeof (asection
*))));
3307 m
->p_type
= p
->p_type
;
3308 m
->p_flags
= p
->p_flags
;
3309 m
->p_flags_valid
= 1;
3310 /* Default to using the physical address of the segment
3311 in the input BFD. */
3312 m
->p_paddr
= p
->p_paddr
;
3313 m
->p_paddr_valid
= 1;
3315 m
->includes_filehdr
= (p
->p_offset
== 0
3316 && p
->p_filesz
>= iehdr
->e_ehsize
);
3318 m
->includes_phdrs
= (p
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3319 && (p
->p_offset
+ p
->p_filesz
3320 >= ((bfd_vma
) iehdr
->e_phoff
3321 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3324 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3326 boolean matching_lma
= false;
3327 boolean lma_conflict
= false;
3328 bfd_vma suggested_lma
= 0;
3331 #define is_contained_by(addr, len, bottom, phdr) \
3332 ((addr) >= (bottom) \
3333 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3334 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3336 os
= s
->output_section
;
3338 if ((is_contained_by (s
->vma
, s
->_raw_size
, p
->p_vaddr
, p
)
3341 && (s
->flags
& SEC_HAS_CONTENTS
) != 0
3342 && (bfd_vma
) s
->filepos
>= p
->p_offset
3343 && ((bfd_vma
) s
->filepos
+ s
->_raw_size
3344 <= p
->p_offset
+ p
->p_filesz
)))
3345 && (s
->flags
& SEC_ALLOC
) != 0
3348 m
->sections
[isec
] = os
;
3351 /* Match up the physical address of the segment with the
3352 LMA addresses of its sections. */
3354 if (is_contained_by (os
->lma
, os
->_raw_size
, m
->p_paddr
, p
))
3355 matching_lma
= true;
3356 else if (suggested_lma
== 0)
3357 suggested_lma
= os
->lma
;
3359 (! is_contained_by (os
->lma
, os
->_raw_size
, suggested_lma
, p
))
3360 lma_conflict
= true;
3366 (*_bfd_error_handler
)
3367 (_("Warning: Some sections' LMAs lie outside their segment's physical address\n"));
3369 else if (lma_conflict
)
3371 (*_bfd_error_handler
)
3372 (_("Warning: Cannot change segment's physical address to contain all of its sections' LMAs\n"));
3374 else if (suggested_lma
)
3376 m
->p_paddr
= suggested_lma
;
3379 BFD_ASSERT (isec
== csecs
);
3386 /* The Solaris linker creates program headers in which all the
3387 p_paddr fields are zero. When we try to objcopy or strip such a
3388 file, we get confused. Check for this case, and if we find it
3389 reset the p_paddr_valid fields. */
3390 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3391 if (m
->p_paddr
!= 0)
3395 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3396 m
->p_paddr_valid
= 0;
3399 elf_tdata (obfd
)->segment_map
= mfirst
;
3404 /* Copy private section information. This copies over the entsize
3405 field, and sometimes the info field. */
3408 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
3414 Elf_Internal_Shdr
*ihdr
, *ohdr
;
3416 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
3417 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
3420 /* Copy over private BFD data if it has not already been copied.
3421 This must be done here, rather than in the copy_private_bfd_data
3422 entry point, because the latter is called after the section
3423 contents have been set, which means that the program headers have
3424 already been worked out. */
3425 if (elf_tdata (obfd
)->segment_map
== NULL
3426 && elf_tdata (ibfd
)->phdr
!= NULL
)
3430 /* Only set up the segments if there are no more SEC_ALLOC
3431 sections. FIXME: This won't do the right thing if objcopy is
3432 used to remove the last SEC_ALLOC section, since objcopy
3433 won't call this routine in that case. */
3434 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
3435 if ((s
->flags
& SEC_ALLOC
) != 0)
3439 if (! copy_private_bfd_data (ibfd
, obfd
))
3444 ihdr
= &elf_section_data (isec
)->this_hdr
;
3445 ohdr
= &elf_section_data (osec
)->this_hdr
;
3447 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
3449 if (ihdr
->sh_type
== SHT_SYMTAB
3450 || ihdr
->sh_type
== SHT_DYNSYM
3451 || ihdr
->sh_type
== SHT_GNU_verneed
3452 || ihdr
->sh_type
== SHT_GNU_verdef
)
3453 ohdr
->sh_info
= ihdr
->sh_info
;
3458 /* Copy private symbol information. If this symbol is in a section
3459 which we did not map into a BFD section, try to map the section
3460 index correctly. We use special macro definitions for the mapped
3461 section indices; these definitions are interpreted by the
3462 swap_out_syms function. */
3464 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3465 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3466 #define MAP_STRTAB (SHN_LORESERVE - 3)
3467 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3470 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
3476 elf_symbol_type
*isym
, *osym
;
3478 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3479 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3482 isym
= elf_symbol_from (ibfd
, isymarg
);
3483 osym
= elf_symbol_from (obfd
, osymarg
);
3487 && bfd_is_abs_section (isym
->symbol
.section
))
3491 shndx
= isym
->internal_elf_sym
.st_shndx
;
3492 if (shndx
== elf_onesymtab (ibfd
))
3493 shndx
= MAP_ONESYMTAB
;
3494 else if (shndx
== elf_dynsymtab (ibfd
))
3495 shndx
= MAP_DYNSYMTAB
;
3496 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
3498 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
3499 shndx
= MAP_SHSTRTAB
;
3500 osym
->internal_elf_sym
.st_shndx
= shndx
;
3506 /* Swap out the symbols. */
3509 swap_out_syms (abfd
, sttp
)
3511 struct bfd_strtab_hash
**sttp
;
3513 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3515 if (!elf_map_symbols (abfd
))
3518 /* Dump out the symtabs. */
3520 int symcount
= bfd_get_symcount (abfd
);
3521 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3522 struct bfd_strtab_hash
*stt
;
3523 Elf_Internal_Shdr
*symtab_hdr
;
3524 Elf_Internal_Shdr
*symstrtab_hdr
;
3525 char *outbound_syms
;
3528 stt
= _bfd_elf_stringtab_init ();
3532 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3533 symtab_hdr
->sh_type
= SHT_SYMTAB
;
3534 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3535 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
3536 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
3537 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
3539 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3540 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3542 outbound_syms
= bfd_alloc (abfd
,
3543 (1 + symcount
) * bed
->s
->sizeof_sym
);
3544 if (outbound_syms
== NULL
)
3546 symtab_hdr
->contents
= (PTR
) outbound_syms
;
3548 /* now generate the data (for "contents") */
3550 /* Fill in zeroth symbol and swap it out. */
3551 Elf_Internal_Sym sym
;
3557 sym
.st_shndx
= SHN_UNDEF
;
3558 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
3559 outbound_syms
+= bed
->s
->sizeof_sym
;
3561 for (idx
= 0; idx
< symcount
; idx
++)
3563 Elf_Internal_Sym sym
;
3564 bfd_vma value
= syms
[idx
]->value
;
3565 elf_symbol_type
*type_ptr
;
3566 flagword flags
= syms
[idx
]->flags
;
3569 if (flags
& BSF_SECTION_SYM
)
3570 /* Section symbols have no names. */
3574 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
3577 if (sym
.st_name
== (unsigned long) -1)
3581 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
3583 if (bfd_is_com_section (syms
[idx
]->section
))
3585 /* ELF common symbols put the alignment into the `value' field,
3586 and the size into the `size' field. This is backwards from
3587 how BFD handles it, so reverse it here. */
3588 sym
.st_size
= value
;
3589 if (type_ptr
== NULL
3590 || type_ptr
->internal_elf_sym
.st_value
== 0)
3591 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
3593 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
3594 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (abfd
,
3595 syms
[idx
]->section
);
3599 asection
*sec
= syms
[idx
]->section
;
3602 if (sec
->output_section
)
3604 value
+= sec
->output_offset
;
3605 sec
= sec
->output_section
;
3608 sym
.st_value
= value
;
3609 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
3611 if (bfd_is_abs_section (sec
)
3613 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
3615 /* This symbol is in a real ELF section which we did
3616 not create as a BFD section. Undo the mapping done
3617 by copy_private_symbol_data. */
3618 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
3622 shndx
= elf_onesymtab (abfd
);
3625 shndx
= elf_dynsymtab (abfd
);
3628 shndx
= elf_tdata (abfd
)->strtab_section
;
3631 shndx
= elf_tdata (abfd
)->shstrtab_section
;
3639 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3645 /* Writing this would be a hell of a lot easier if
3646 we had some decent documentation on bfd, and
3647 knew what to expect of the library, and what to
3648 demand of applications. For example, it
3649 appears that `objcopy' might not set the
3650 section of a symbol to be a section that is
3651 actually in the output file. */
3652 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
3653 BFD_ASSERT (sec2
!= 0);
3654 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
3655 BFD_ASSERT (shndx
!= -1);
3659 sym
.st_shndx
= shndx
;
3662 if ((flags
& BSF_FUNCTION
) != 0)
3664 else if ((flags
& BSF_OBJECT
) != 0)
3669 if (bfd_is_com_section (syms
[idx
]->section
))
3670 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
3671 else if (bfd_is_und_section (syms
[idx
]->section
))
3672 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
3676 else if (flags
& BSF_SECTION_SYM
)
3677 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3678 else if (flags
& BSF_FILE
)
3679 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
3682 int bind
= STB_LOCAL
;
3684 if (flags
& BSF_LOCAL
)
3686 else if (flags
& BSF_WEAK
)
3688 else if (flags
& BSF_GLOBAL
)
3691 sym
.st_info
= ELF_ST_INFO (bind
, type
);
3694 if (type_ptr
!= NULL
)
3695 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
3699 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
3700 outbound_syms
+= bed
->s
->sizeof_sym
;
3704 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
3705 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3707 symstrtab_hdr
->sh_flags
= 0;
3708 symstrtab_hdr
->sh_addr
= 0;
3709 symstrtab_hdr
->sh_entsize
= 0;
3710 symstrtab_hdr
->sh_link
= 0;
3711 symstrtab_hdr
->sh_info
= 0;
3712 symstrtab_hdr
->sh_addralign
= 1;
3718 /* Return the number of bytes required to hold the symtab vector.
3720 Note that we base it on the count plus 1, since we will null terminate
3721 the vector allocated based on this size. However, the ELF symbol table
3722 always has a dummy entry as symbol #0, so it ends up even. */
3725 _bfd_elf_get_symtab_upper_bound (abfd
)
3730 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3732 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3733 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
3739 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
3744 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3746 if (elf_dynsymtab (abfd
) == 0)
3748 bfd_set_error (bfd_error_invalid_operation
);
3752 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3753 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
3759 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
3763 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
3766 /* Canonicalize the relocs. */
3769 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
3778 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
3784 tblptr
= section
->relocation
;
3785 for (i
= 0; i
< section
->reloc_count
; i
++)
3786 *relptr
++ = tblptr
++;
3790 return section
->reloc_count
;
3794 _bfd_elf_get_symtab (abfd
, alocation
)
3796 asymbol
**alocation
;
3798 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, false);
3801 bfd_get_symcount (abfd
) = symcount
;
3806 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
3808 asymbol
**alocation
;
3810 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, true);
3813 /* Return the size required for the dynamic reloc entries. Any
3814 section that was actually installed in the BFD, and has type
3815 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
3816 considered to be a dynamic reloc section. */
3819 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
3825 if (elf_dynsymtab (abfd
) == 0)
3827 bfd_set_error (bfd_error_invalid_operation
);
3831 ret
= sizeof (arelent
*);
3832 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3833 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
3834 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
3835 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
3836 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
3837 * sizeof (arelent
*));
3842 /* Canonicalize the dynamic relocation entries. Note that we return
3843 the dynamic relocations as a single block, although they are
3844 actually associated with particular sections; the interface, which
3845 was designed for SunOS style shared libraries, expects that there
3846 is only one set of dynamic relocs. Any section that was actually
3847 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
3848 the dynamic symbol table, is considered to be a dynamic reloc
3852 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
3857 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
3861 if (elf_dynsymtab (abfd
) == 0)
3863 bfd_set_error (bfd_error_invalid_operation
);
3867 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
3869 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3871 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
3872 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
3873 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
3878 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
3880 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
3882 for (i
= 0; i
< count
; i
++)
3893 /* Read in the version information. */
3896 _bfd_elf_slurp_version_tables (abfd
)
3899 bfd_byte
*contents
= NULL
;
3901 if (elf_dynverdef (abfd
) != 0)
3903 Elf_Internal_Shdr
*hdr
;
3904 Elf_External_Verdef
*everdef
;
3905 Elf_Internal_Verdef
*iverdef
;
3908 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
3910 elf_tdata (abfd
)->verdef
=
3911 ((Elf_Internal_Verdef
*)
3912 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verdef
)));
3913 if (elf_tdata (abfd
)->verdef
== NULL
)
3916 elf_tdata (abfd
)->cverdefs
= hdr
->sh_info
;
3918 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
3919 if (contents
== NULL
)
3921 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3922 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
3925 everdef
= (Elf_External_Verdef
*) contents
;
3926 iverdef
= elf_tdata (abfd
)->verdef
;
3927 for (i
= 0; i
< hdr
->sh_info
; i
++, iverdef
++)
3929 Elf_External_Verdaux
*everdaux
;
3930 Elf_Internal_Verdaux
*iverdaux
;
3933 _bfd_elf_swap_verdef_in (abfd
, everdef
, iverdef
);
3935 iverdef
->vd_bfd
= abfd
;
3937 iverdef
->vd_auxptr
= ((Elf_Internal_Verdaux
*)
3940 * sizeof (Elf_Internal_Verdaux
))));
3941 if (iverdef
->vd_auxptr
== NULL
)
3944 everdaux
= ((Elf_External_Verdaux
*)
3945 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
3946 iverdaux
= iverdef
->vd_auxptr
;
3947 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
3949 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
3951 iverdaux
->vda_nodename
=
3952 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3953 iverdaux
->vda_name
);
3954 if (iverdaux
->vda_nodename
== NULL
)
3957 if (j
+ 1 < iverdef
->vd_cnt
)
3958 iverdaux
->vda_nextptr
= iverdaux
+ 1;
3960 iverdaux
->vda_nextptr
= NULL
;
3962 everdaux
= ((Elf_External_Verdaux
*)
3963 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
3966 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
3968 if (i
+ 1 < hdr
->sh_info
)
3969 iverdef
->vd_nextdef
= iverdef
+ 1;
3971 iverdef
->vd_nextdef
= NULL
;
3973 everdef
= ((Elf_External_Verdef
*)
3974 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
3981 if (elf_dynverref (abfd
) != 0)
3983 Elf_Internal_Shdr
*hdr
;
3984 Elf_External_Verneed
*everneed
;
3985 Elf_Internal_Verneed
*iverneed
;
3988 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
3990 elf_tdata (abfd
)->verref
=
3991 ((Elf_Internal_Verneed
*)
3992 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verneed
)));
3993 if (elf_tdata (abfd
)->verref
== NULL
)
3996 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
3998 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
3999 if (contents
== NULL
)
4001 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4002 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4005 everneed
= (Elf_External_Verneed
*) contents
;
4006 iverneed
= elf_tdata (abfd
)->verref
;
4007 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4009 Elf_External_Vernaux
*evernaux
;
4010 Elf_Internal_Vernaux
*ivernaux
;
4013 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4015 iverneed
->vn_bfd
= abfd
;
4017 iverneed
->vn_filename
=
4018 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4020 if (iverneed
->vn_filename
== NULL
)
4023 iverneed
->vn_auxptr
=
4024 ((Elf_Internal_Vernaux
*)
4026 iverneed
->vn_cnt
* sizeof (Elf_Internal_Vernaux
)));
4028 evernaux
= ((Elf_External_Vernaux
*)
4029 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4030 ivernaux
= iverneed
->vn_auxptr
;
4031 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4033 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4035 ivernaux
->vna_nodename
=
4036 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4037 ivernaux
->vna_name
);
4038 if (ivernaux
->vna_nodename
== NULL
)
4041 if (j
+ 1 < iverneed
->vn_cnt
)
4042 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4044 ivernaux
->vna_nextptr
= NULL
;
4046 evernaux
= ((Elf_External_Vernaux
*)
4047 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4050 if (i
+ 1 < hdr
->sh_info
)
4051 iverneed
->vn_nextref
= iverneed
+ 1;
4053 iverneed
->vn_nextref
= NULL
;
4055 everneed
= ((Elf_External_Verneed
*)
4056 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4066 if (contents
== NULL
)
4072 _bfd_elf_make_empty_symbol (abfd
)
4075 elf_symbol_type
*newsym
;
4077 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
4082 newsym
->symbol
.the_bfd
= abfd
;
4083 return &newsym
->symbol
;
4088 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4093 bfd_symbol_info (symbol
, ret
);
4096 /* Return whether a symbol name implies a local symbol. Most targets
4097 use this function for the is_local_label_name entry point, but some
4101 _bfd_elf_is_local_label_name (abfd
, name
)
4105 /* Normal local symbols start with ``.L''. */
4106 if (name
[0] == '.' && name
[1] == 'L')
4109 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4110 DWARF debugging symbols starting with ``..''. */
4111 if (name
[0] == '.' && name
[1] == '.')
4114 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4115 emitting DWARF debugging output. I suspect this is actually a
4116 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4117 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4118 underscore to be emitted on some ELF targets). For ease of use,
4119 we treat such symbols as local. */
4120 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
4127 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
4136 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
4138 enum bfd_architecture arch
;
4139 unsigned long machine
;
4141 /* If this isn't the right architecture for this backend, and this
4142 isn't the generic backend, fail. */
4143 if (arch
!= get_elf_backend_data (abfd
)->arch
4144 && arch
!= bfd_arch_unknown
4145 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
4148 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
4151 /* Find the nearest line to a particular section and offset, for error
4155 _bfd_elf_find_nearest_line (abfd
,
4166 CONST
char **filename_ptr
;
4167 CONST
char **functionname_ptr
;
4168 unsigned int *line_ptr
;
4171 const char *filename
;
4176 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
4177 filename_ptr
, functionname_ptr
,
4181 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
4182 &found
, filename_ptr
,
4183 functionname_ptr
, line_ptr
,
4184 &elf_tdata (abfd
)->line_info
))
4189 if (symbols
== NULL
)
4196 for (p
= symbols
; *p
!= NULL
; p
++)
4200 q
= (elf_symbol_type
*) *p
;
4202 if (bfd_get_section (&q
->symbol
) != section
)
4205 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
4210 filename
= bfd_asymbol_name (&q
->symbol
);
4213 if (q
->symbol
.section
== section
4214 && q
->symbol
.value
>= low_func
4215 && q
->symbol
.value
<= offset
)
4217 func
= (asymbol
*) q
;
4218 low_func
= q
->symbol
.value
;
4227 *filename_ptr
= filename
;
4228 *functionname_ptr
= bfd_asymbol_name (func
);
4234 _bfd_elf_sizeof_headers (abfd
, reloc
)
4240 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
4242 ret
+= get_program_header_size (abfd
);
4247 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
4252 bfd_size_type count
;
4254 Elf_Internal_Shdr
*hdr
;
4256 if (! abfd
->output_has_begun
4257 && ! _bfd_elf_compute_section_file_positions (abfd
,
4258 (struct bfd_link_info
*) NULL
))
4261 hdr
= &elf_section_data (section
)->this_hdr
;
4263 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
4265 if (bfd_write (location
, 1, count
, abfd
) != count
)
4272 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
4275 Elf_Internal_Rela
*dst
;
4282 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
4285 Elf_Internal_Rel
*dst
;
4291 /* Try to convert a non-ELF reloc into an ELF one. */
4294 _bfd_elf_validate_reloc (abfd
, areloc
)
4298 /* Check whether we really have an ELF howto. */
4300 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
4302 bfd_reloc_code_real_type code
;
4303 reloc_howto_type
*howto
;
4305 /* Alien reloc: Try to determine its type to replace it with an
4306 equivalent ELF reloc. */
4308 if (areloc
->howto
->pc_relative
)
4310 switch (areloc
->howto
->bitsize
)
4313 code
= BFD_RELOC_8_PCREL
;
4316 code
= BFD_RELOC_12_PCREL
;
4319 code
= BFD_RELOC_16_PCREL
;
4322 code
= BFD_RELOC_24_PCREL
;
4325 code
= BFD_RELOC_32_PCREL
;
4328 code
= BFD_RELOC_64_PCREL
;
4334 howto
= bfd_reloc_type_lookup (abfd
, code
);
4336 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
4338 if (howto
->pcrel_offset
)
4339 areloc
->addend
+= areloc
->address
;
4341 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
4346 switch (areloc
->howto
->bitsize
)
4352 code
= BFD_RELOC_14
;
4355 code
= BFD_RELOC_16
;
4358 code
= BFD_RELOC_26
;
4361 code
= BFD_RELOC_32
;
4364 code
= BFD_RELOC_64
;
4370 howto
= bfd_reloc_type_lookup (abfd
, code
);
4374 areloc
->howto
= howto
;
4382 (*_bfd_error_handler
)
4383 (_("%s: unsupported relocation type %s"),
4384 bfd_get_filename (abfd
), areloc
->howto
->name
);
4385 bfd_set_error (bfd_error_bad_value
);
4390 _bfd_elf_close_and_cleanup (abfd
)
4393 if (bfd_get_format (abfd
) == bfd_object
)
4395 if (elf_shstrtab (abfd
) != NULL
)
4396 _bfd_stringtab_free (elf_shstrtab (abfd
));
4399 return _bfd_generic_close_and_cleanup (abfd
);