1 /* ELF executable support for BFD.
2 Copyright 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 Written by Fred Fish @ Cygnus Support, from information published
5 in "UNIX System V Release 4, Programmers Guide: ANSI C and
6 Programming Support Tools". Sufficient support for gdb.
8 Rewritten by Mark Eichin @ Cygnus Support, from information
9 published in "System V Application Binary Interface", chapters 4
10 and 5, as well as the various "Processor Supplement" documents
11 derived from it. Added support for assembler and other object file
12 utilities. Further work done by Ken Raeburn (Cygnus Support), Michael
13 Meissner (Open Software Foundation), and Peter Hoogenboom (University
14 of Utah) to finish and extend this.
16 This file is part of BFD, the Binary File Descriptor library.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
32 /* Problems and other issues to resolve.
34 (1) BFD expects there to be some fixed number of "sections" in
35 the object file. I.E. there is a "section_count" variable in the
36 bfd structure which contains the number of sections. However, ELF
37 supports multiple "views" of a file. In particular, with current
38 implementations, executable files typically have two tables, a
39 program header table and a section header table, both of which
40 partition the executable.
42 In ELF-speak, the "linking view" of the file uses the section header
43 table to access "sections" within the file, and the "execution view"
44 uses the program header table to access "segments" within the file.
45 "Segments" typically may contain all the data from one or more
48 Note that the section header table is optional in ELF executables,
49 but it is this information that is most useful to gdb. If the
50 section header table is missing, then gdb should probably try
51 to make do with the program header table. (FIXME)
53 (2) The code in this file is compiled twice, once in 32-bit mode and
54 once in 64-bit mode. More of it should be made size-independent
57 (3) ELF section symbols are handled rather sloppily now. This should
58 be cleaned up, and ELF section symbols reconciled with BFD section
62 #include <string.h> /* For strrchr and friends */
68 /* Renaming structures, typedefs, macros and functions to be size-specific. */
69 #define Elf_External_Ehdr NAME(Elf,External_Ehdr)
70 #define Elf_External_Sym NAME(Elf,External_Sym)
71 #define Elf_External_Shdr NAME(Elf,External_Shdr)
72 #define Elf_External_Phdr NAME(Elf,External_Phdr)
73 #define Elf_External_Rel NAME(Elf,External_Rel)
74 #define Elf_External_Rela NAME(Elf,External_Rela)
76 #define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command)
77 #define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal)
78 #define elf_core_file_matches_executable_p \
79 NAME(bfd_elf,core_file_matches_executable_p)
80 #define elf_object_p NAME(bfd_elf,object_p)
81 #define elf_core_file_p NAME(bfd_elf,core_file_p)
82 #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
83 #define elf_get_dynamic_symtab_upper_bound \
84 NAME(bfd_elf,get_dynamic_symtab_upper_bound)
85 #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
86 #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
87 #define elf_get_symtab NAME(bfd_elf,get_symtab)
88 #define elf_canonicalize_dynamic_symtab \
89 NAME(bfd_elf,canonicalize_dynamic_symtab)
90 #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol)
91 #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info)
92 #define elf_print_symbol NAME(bfd_elf,print_symbol)
93 #define elf_get_lineno NAME(bfd_elf,get_lineno)
94 #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach)
95 #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line)
96 #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers)
97 #define elf_set_section_contents NAME(bfd_elf,set_section_contents)
98 #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto)
99 #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel)
100 #define elf_new_section_hook NAME(bfd_elf,new_section_hook)
101 #define write_relocs NAME(bfd_elf,_write_relocs)
102 #define elf_find_section NAME(bfd_elf,find_section)
105 #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
106 #define ELF_R_SYM(X) ELF64_R_SYM(X)
107 #define ELFCLASS ELFCLASS64
111 #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
112 #define ELF_R_SYM(X) ELF32_R_SYM(X)
113 #define ELFCLASS ELFCLASS32
117 static int shstrtab_length_fixed
;
125 /* Forward declarations of static functions */
127 static struct sec
*section_from_elf_index
PARAMS ((bfd
*, unsigned int));
129 static int elf_section_from_bfd_section
PARAMS ((bfd
*, struct sec
*));
131 static long elf_slurp_symbol_table
PARAMS ((bfd
*, asymbol
**, boolean
));
133 static int elf_symbol_from_bfd_symbol
PARAMS ((bfd
*,
134 struct symbol_cache_entry
**));
136 static boolean elf_map_symbols
PARAMS ((bfd
*));
137 static boolean swap_out_syms
PARAMS ((bfd
*));
140 static void elf_debug_section
PARAMS ((char *, int, Elf_Internal_Shdr
*));
141 static void elf_debug_file
PARAMS ((Elf_Internal_Ehdr
*));
144 #define elf_string_from_elf_strtab(abfd,strindex) \
145 elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
148 /* Structure swapping routines */
150 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
151 can be handled by explicitly specifying 32 bits or "the long type". */
153 #define put_word bfd_h_put_64
154 #define get_word bfd_h_get_64
157 #define put_word bfd_h_put_32
158 #define get_word bfd_h_get_32
161 /* Translate an ELF symbol in external format into an ELF symbol in internal
165 elf_swap_symbol_in (abfd
, src
, dst
)
167 Elf_External_Sym
*src
;
168 Elf_Internal_Sym
*dst
;
170 dst
->st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->st_name
);
171 dst
->st_value
= get_word (abfd
, (bfd_byte
*) src
->st_value
);
172 dst
->st_size
= get_word (abfd
, (bfd_byte
*) src
->st_size
);
173 dst
->st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_info
);
174 dst
->st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_other
);
175 dst
->st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->st_shndx
);
178 /* Translate an ELF symbol in internal format into an ELF symbol in external
182 elf_swap_symbol_out (abfd
, src
, dst
)
184 Elf_Internal_Sym
*src
;
185 Elf_External_Sym
*dst
;
187 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
188 put_word (abfd
, src
->st_value
, dst
->st_value
);
189 put_word (abfd
, src
->st_size
, dst
->st_size
);
190 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
191 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
192 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
196 /* Translate an ELF file header in external format into an ELF file header in
200 elf_swap_ehdr_in (abfd
, src
, dst
)
202 Elf_External_Ehdr
*src
;
203 Elf_Internal_Ehdr
*dst
;
205 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
206 dst
->e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_type
);
207 dst
->e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_machine
);
208 dst
->e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_version
);
209 dst
->e_entry
= get_word (abfd
, (bfd_byte
*) src
->e_entry
);
210 dst
->e_phoff
= get_word (abfd
, (bfd_byte
*) src
->e_phoff
);
211 dst
->e_shoff
= get_word (abfd
, (bfd_byte
*) src
->e_shoff
);
212 dst
->e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_flags
);
213 dst
->e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_ehsize
);
214 dst
->e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phentsize
);
215 dst
->e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phnum
);
216 dst
->e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shentsize
);
217 dst
->e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shnum
);
218 dst
->e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shstrndx
);
221 /* Translate an ELF file header in internal format into an ELF file header in
225 elf_swap_ehdr_out (abfd
, src
, dst
)
227 Elf_Internal_Ehdr
*src
;
228 Elf_External_Ehdr
*dst
;
230 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
231 /* note that all elements of dst are *arrays of unsigned char* already... */
232 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
233 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
234 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
235 put_word (abfd
, src
->e_entry
, dst
->e_entry
);
236 put_word (abfd
, src
->e_phoff
, dst
->e_phoff
);
237 put_word (abfd
, src
->e_shoff
, dst
->e_shoff
);
238 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
239 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
240 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
241 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
242 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
243 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
244 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
248 /* Translate an ELF section header table entry in external format into an
249 ELF section header table entry in internal format. */
252 elf_swap_shdr_in (abfd
, src
, dst
)
254 Elf_External_Shdr
*src
;
255 Elf_Internal_Shdr
*dst
;
257 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
258 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
259 dst
->sh_flags
= get_word (abfd
, (bfd_byte
*) src
->sh_flags
);
260 dst
->sh_addr
= get_word (abfd
, (bfd_byte
*) src
->sh_addr
);
261 dst
->sh_offset
= get_word (abfd
, (bfd_byte
*) src
->sh_offset
);
262 dst
->sh_size
= get_word (abfd
, (bfd_byte
*) src
->sh_size
);
263 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
264 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
265 dst
->sh_addralign
= get_word (abfd
, (bfd_byte
*) src
->sh_addralign
);
266 dst
->sh_entsize
= get_word (abfd
, (bfd_byte
*) src
->sh_entsize
);
267 /* we haven't done any processing on it yet, so... */
268 dst
->rawdata
= (void *) 0;
271 /* Translate an ELF section header table entry in internal format into an
272 ELF section header table entry in external format. */
275 elf_swap_shdr_out (abfd
, src
, dst
)
277 Elf_Internal_Shdr
*src
;
278 Elf_External_Shdr
*dst
;
280 /* note that all elements of dst are *arrays of unsigned char* already... */
281 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
282 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
283 put_word (abfd
, src
->sh_flags
, dst
->sh_flags
);
284 put_word (abfd
, src
->sh_addr
, dst
->sh_addr
);
285 put_word (abfd
, src
->sh_offset
, dst
->sh_offset
);
286 put_word (abfd
, src
->sh_size
, dst
->sh_size
);
287 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
288 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
289 put_word (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
290 put_word (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
294 /* Translate an ELF program header table entry in external format into an
295 ELF program header table entry in internal format. */
298 elf_swap_phdr_in (abfd
, src
, dst
)
300 Elf_External_Phdr
*src
;
301 Elf_Internal_Phdr
*dst
;
303 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
304 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
305 dst
->p_offset
= get_word (abfd
, (bfd_byte
*) src
->p_offset
);
306 dst
->p_vaddr
= get_word (abfd
, (bfd_byte
*) src
->p_vaddr
);
307 dst
->p_paddr
= get_word (abfd
, (bfd_byte
*) src
->p_paddr
);
308 dst
->p_filesz
= get_word (abfd
, (bfd_byte
*) src
->p_filesz
);
309 dst
->p_memsz
= get_word (abfd
, (bfd_byte
*) src
->p_memsz
);
310 dst
->p_align
= get_word (abfd
, (bfd_byte
*) src
->p_align
);
314 elf_swap_phdr_out (abfd
, src
, dst
)
316 Elf_Internal_Phdr
*src
;
317 Elf_External_Phdr
*dst
;
319 /* note that all elements of dst are *arrays of unsigned char* already... */
320 bfd_h_put_32 (abfd
, src
->p_type
, dst
->p_type
);
321 put_word (abfd
, src
->p_offset
, dst
->p_offset
);
322 put_word (abfd
, src
->p_vaddr
, dst
->p_vaddr
);
323 put_word (abfd
, src
->p_paddr
, dst
->p_paddr
);
324 put_word (abfd
, src
->p_filesz
, dst
->p_filesz
);
325 put_word (abfd
, src
->p_memsz
, dst
->p_memsz
);
326 bfd_h_put_32 (abfd
, src
->p_flags
, dst
->p_flags
);
327 put_word (abfd
, src
->p_align
, dst
->p_align
);
330 /* Translate an ELF reloc from external format to internal format. */
332 elf_swap_reloc_in (abfd
, src
, dst
)
334 Elf_External_Rel
*src
;
335 Elf_Internal_Rel
*dst
;
337 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
338 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
342 elf_swap_reloca_in (abfd
, src
, dst
)
344 Elf_External_Rela
*src
;
345 Elf_Internal_Rela
*dst
;
347 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
348 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
349 dst
->r_addend
= get_word (abfd
, (bfd_byte
*) src
->r_addend
);
352 /* Translate an ELF reloc from internal format to external format. */
354 elf_swap_reloc_out (abfd
, src
, dst
)
356 Elf_Internal_Rel
*src
;
357 Elf_External_Rel
*dst
;
359 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
360 put_word (abfd
, src
->r_info
, dst
->r_info
);
364 elf_swap_reloca_out (abfd
, src
, dst
)
366 Elf_Internal_Rela
*src
;
367 Elf_External_Rela
*dst
;
369 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
370 put_word (abfd
, src
->r_info
, dst
->r_info
);
371 put_word (abfd
, src
->r_addend
, dst
->r_addend
);
376 /* String table creation/manipulation routines */
378 static struct strtab
*
379 bfd_new_strtab (abfd
)
384 ss
= (struct strtab
*) malloc (sizeof (struct strtab
));
387 bfd_set_error (bfd_error_no_memory
);
390 ss
->tab
= malloc (1);
393 bfd_set_error (bfd_error_no_memory
);
404 bfd_add_to_strtab (abfd
, ss
, str
)
409 /* should search first, but for now: */
410 /* include the trailing NUL */
411 int ln
= strlen (str
) + 1;
413 /* should this be using obstacks? */
414 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
416 BFD_ASSERT (ss
->tab
!= 0); /* FIXME */
417 strcpy (ss
->tab
+ ss
->length
, str
);
421 return ss
->length
- ln
;
425 bfd_add_2_to_strtab (abfd
, ss
, str
, str2
)
431 /* should search first, but for now: */
432 /* include the trailing NUL */
433 int ln
= strlen (str
) + strlen (str2
) + 1;
435 /* should this be using obstacks? */
437 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
439 ss
->tab
= malloc (ln
);
441 BFD_ASSERT (ss
->tab
!= 0); /* FIXME */
442 strcpy (ss
->tab
+ ss
->length
, str
);
443 strcpy (ss
->tab
+ ss
->length
+ strlen (str
), str2
);
447 return ss
->length
- ln
;
451 /* ELF .o/exec file reading */
453 /* Create a new bfd section from an ELF section header. */
456 bfd_section_from_shdr (abfd
, shindex
)
458 unsigned int shindex
;
460 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
461 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
465 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
467 switch (hdr
->sh_type
)
471 /* inactive section. Throw it away. */
476 /* Bits that get saved. This one is real. */
479 newsect
= bfd_make_section (abfd
, name
);
482 newsect
->filepos
= hdr
->sh_offset
; /* so we can read back the bits */
483 newsect
->flags
|= SEC_HAS_CONTENTS
;
484 newsect
->vma
= hdr
->sh_addr
;
485 newsect
->_raw_size
= hdr
->sh_size
;
486 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
488 if (hdr
->sh_flags
& SHF_ALLOC
)
490 newsect
->flags
|= SEC_ALLOC
;
491 newsect
->flags
|= SEC_LOAD
;
494 if (!(hdr
->sh_flags
& SHF_WRITE
))
495 newsect
->flags
|= SEC_READONLY
;
497 if (hdr
->sh_flags
& SHF_EXECINSTR
)
498 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
499 else if (newsect
->flags
& SEC_ALLOC
)
500 newsect
->flags
|= SEC_DATA
;
502 /* The debugging sections appear to recognized only by
504 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
505 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
506 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
507 newsect
->flags
|= SEC_DEBUGGING
;
509 hdr
->rawdata
= (void *) newsect
;
512 hdr
->rawdata
= (void *) bfd_get_section_by_name (abfd
, name
);
517 /* Bits that get saved. This one is real. */
520 newsect
= bfd_make_section (abfd
, name
);
523 newsect
->vma
= hdr
->sh_addr
;
524 newsect
->_raw_size
= hdr
->sh_size
;
525 newsect
->filepos
= hdr
->sh_offset
; /* fake */
526 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
527 if (hdr
->sh_flags
& SHF_ALLOC
)
528 newsect
->flags
|= SEC_ALLOC
;
530 if (!(hdr
->sh_flags
& SHF_WRITE
))
531 newsect
->flags
|= SEC_READONLY
;
533 /* FIXME: This section is empty. Does it really make
534 sense to set SEC_CODE for it? */
535 if (hdr
->sh_flags
& SHF_EXECINSTR
)
536 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
538 hdr
->rawdata
= (void *) newsect
;
543 case SHT_SYMTAB
: /* A symbol table */
544 if (elf_onesymtab (abfd
) == shindex
)
547 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
548 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
549 elf_onesymtab (abfd
) = shindex
;
550 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
551 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_hdr
;
552 abfd
->flags
|= HAS_SYMS
;
555 case SHT_DYNSYM
: /* A dynamic symbol table */
556 if (elf_dynsymtab (abfd
) == shindex
)
559 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
560 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
561 elf_dynsymtab (abfd
) = shindex
;
562 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
563 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->dynsymtab_hdr
;
564 abfd
->flags
|= HAS_SYMS
;
567 case SHT_STRTAB
: /* A string table */
570 if (ehdr
->e_shstrndx
== shindex
)
572 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
573 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
574 hdr
->rawdata
= (PTR
) & elf_tdata (abfd
)->shstrtab_hdr
;
580 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
582 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
583 if (hdr2
->sh_link
== shindex
)
585 bfd_section_from_shdr (abfd
, i
);
586 if (elf_onesymtab (abfd
) == i
)
588 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
589 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
592 if (elf_dynsymtab (abfd
) == i
)
594 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
595 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->dynstrtab_hdr
;
598 #if 0 /* Not handling other string tables specially right now. */
599 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
600 /* We have a strtab for some random other section. */
601 newsect
= (asection
*) hdr2
->rawdata
;
604 hdr
->rawdata
= (PTR
) newsect
;
605 hdr2
= &elf_section_data (newsect
)->str_hdr
;
607 elf_elfsections (abfd
)[shindex
] = hdr2
;
613 newsect
= bfd_make_section (abfd
, name
);
616 newsect
->flags
= SEC_HAS_CONTENTS
;
617 hdr
->rawdata
= (PTR
) newsect
;
618 newsect
->_raw_size
= hdr
->sh_size
;
619 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
620 newsect
->vma
= hdr
->sh_addr
;
621 newsect
->filepos
= hdr
->sh_offset
;
623 if (hdr
->sh_flags
& SHF_ALLOC
)
624 newsect
->flags
|= SEC_ALLOC
| SEC_LOAD
;
625 if (!(hdr
->sh_flags
& SHF_WRITE
))
626 newsect
->flags
|= SEC_READONLY
;
627 if (hdr
->sh_flags
& SHF_EXECINSTR
)
628 newsect
->flags
|= SEC_CODE
;
629 else if (newsect
->flags
& SEC_ALLOC
)
630 newsect
->flags
|= SEC_DATA
;
632 /* Check for debugging string tables. */
633 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
634 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
635 newsect
->flags
|= SEC_DEBUGGING
;
642 /* *These* do a lot of work -- but build no sections!
643 The spec says there can be multiple strtabs, but only one symtab,
644 but there can be lots of REL* sections. */
645 /* FIXME: The above statement is wrong! There are typically at least
646 two symbol tables in a dynamically linked executable, ".dynsym"
647 which is the dynamic linkage symbol table and ".symtab", which is
648 the "traditional" symbol table. -fnf */
651 asection
*target_sect
;
652 Elf_Internal_Shdr
*hdr2
;
653 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
655 /* Don't allow REL relocations on a machine that uses RELA and
657 /* @@ Actually, the generic ABI does suggest that both might be
658 used in one file. But the four ABI Processor Supplements I
659 have access to right now all specify that only one is used on
660 each of those architectures. It's conceivable that, e.g., a
661 bunch of absolute 32-bit relocs might be more compact in REL
662 form even on a RELA machine... */
663 BFD_ASSERT (!(use_rela_p
&& (hdr
->sh_type
== SHT_REL
)));
664 BFD_ASSERT (!(!use_rela_p
&& (hdr
->sh_type
== SHT_RELA
)));
665 BFD_ASSERT (hdr
->sh_entsize
==
667 ? sizeof (Elf_External_Rela
)
668 : sizeof (Elf_External_Rel
)));
670 bfd_section_from_shdr (abfd
, hdr
->sh_info
); /* target */
671 bfd_section_from_shdr (abfd
, hdr
->sh_link
); /* symbol table */
672 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
673 if (target_sect
== NULL
674 || elf_section_data (target_sect
) == NULL
)
677 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
679 elf_elfsections (abfd
)[shindex
] = hdr2
;
680 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
681 target_sect
->flags
|= SEC_RELOC
;
682 target_sect
->relocation
= 0;
683 target_sect
->rel_filepos
= hdr
->sh_offset
;
684 abfd
->flags
|= HAS_RELOC
;
691 fprintf (stderr
, "Dynamic Linking sections not yet supported.\n");
698 fprintf (stderr
, "Note Sections not yet supported.\n");
705 fprintf (stderr
, "SHLIB Sections not supported (and non conforming.)\n");
710 /* Check for any processor-specific section types. */
712 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
714 if (bed
->elf_backend_section_from_shdr
)
715 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
724 elf_new_section_hook (abfd
, sec
)
729 struct bfd_elf_section_data
*sdata
;
731 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
734 bfd_set_error (bfd_error_no_memory
);
737 sec
->used_by_bfd
= (PTR
) sdata
;
738 memset (sdata
, 0, sizeof (*sdata
));
742 /* Create a new bfd section from an ELF program header.
744 Since program segments have no names, we generate a synthetic name
745 of the form segment<NUM>, where NUM is generally the index in the
746 program header table. For segments that are split (see below) we
747 generate the names segment<NUM>a and segment<NUM>b.
749 Note that some program segments may have a file size that is different than
750 (less than) the memory size. All this means is that at execution the
751 system must allocate the amount of memory specified by the memory size,
752 but only initialize it with the first "file size" bytes read from the
753 file. This would occur for example, with program segments consisting
754 of combined data+bss.
756 To handle the above situation, this routine generates TWO bfd sections
757 for the single program segment. The first has the length specified by
758 the file size of the segment, and the second has the length specified
759 by the difference between the two sizes. In effect, the segment is split
760 into it's initialized and uninitialized parts.
765 bfd_section_from_phdr (abfd
, hdr
, index
)
767 Elf_Internal_Phdr
*hdr
;
775 split
= ((hdr
->p_memsz
> 0) &&
776 (hdr
->p_filesz
> 0) &&
777 (hdr
->p_memsz
> hdr
->p_filesz
));
778 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
779 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
782 bfd_set_error (bfd_error_no_memory
);
785 strcpy (name
, namebuf
);
786 newsect
= bfd_make_section (abfd
, name
);
787 newsect
->vma
= hdr
->p_vaddr
;
788 newsect
->_raw_size
= hdr
->p_filesz
;
789 newsect
->filepos
= hdr
->p_offset
;
790 newsect
->flags
|= SEC_HAS_CONTENTS
;
791 if (hdr
->p_type
== PT_LOAD
)
793 newsect
->flags
|= SEC_ALLOC
;
794 newsect
->flags
|= SEC_LOAD
;
795 if (hdr
->p_flags
& PF_X
)
797 /* FIXME: all we known is that it has execute PERMISSION,
799 newsect
->flags
|= SEC_CODE
;
802 if (!(hdr
->p_flags
& PF_W
))
804 newsect
->flags
|= SEC_READONLY
;
809 sprintf (namebuf
, "segment%db", index
);
810 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
813 bfd_set_error (bfd_error_no_memory
);
816 strcpy (name
, namebuf
);
817 newsect
= bfd_make_section (abfd
, name
);
818 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
819 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
820 if (hdr
->p_type
== PT_LOAD
)
822 newsect
->flags
|= SEC_ALLOC
;
823 if (hdr
->p_flags
& PF_X
)
824 newsect
->flags
|= SEC_CODE
;
826 if (!(hdr
->p_flags
& PF_W
))
827 newsect
->flags
|= SEC_READONLY
;
833 /* Begin processing a given object.
835 First we validate the file by reading in the ELF header and checking
838 static INLINE boolean
840 Elf_External_Ehdr
*x_ehdrp
;
842 return ((x_ehdrp
->e_ident
[EI_MAG0
] == ELFMAG0
)
843 && (x_ehdrp
->e_ident
[EI_MAG1
] == ELFMAG1
)
844 && (x_ehdrp
->e_ident
[EI_MAG2
] == ELFMAG2
)
845 && (x_ehdrp
->e_ident
[EI_MAG3
] == ELFMAG3
));
848 /* Check to see if the file associated with ABFD matches the target vector
851 Note that we may be called several times with the same ABFD, but different
852 target vectors, most of which will not match. We have to avoid leaving
853 any side effects in ABFD, or any data it points to (like tdata), if the
854 file does not match the target vector.
856 FIXME: There is memory leak if we are called more than once with the same
857 ABFD, and that bfd already has tdata allocated, since we allocate more tdata
858 and the old tdata is orphaned. Since it's in the bfd obstack, there isn't
859 much we can do about this except possibly rewrite the code. There are
860 also other bfd_allocs that may be the source of memory leaks as well. */
866 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
867 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
868 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
869 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
870 unsigned int shindex
;
871 char *shstrtab
; /* Internal copy of section header stringtab */
872 struct elf_backend_data
*ebd
;
873 struct elf_obj_tdata
*preserved_tdata
= elf_tdata (abfd
);
875 /* Read in the ELF header in external format. */
877 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
879 if (bfd_get_error () != bfd_error_system_call
)
880 goto got_wrong_format_error
;
885 /* Now check to see if we have a valid ELF file, and one that BFD can
886 make use of. The magic number must match, the address size ('class')
887 and byte-swapping must match our XVEC entry, and it must have a
888 section header table (FIXME: See comments re sections at top of this
891 if ((elf_file_p (&x_ehdr
) == false) ||
892 (x_ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) ||
893 (x_ehdr
.e_ident
[EI_CLASS
] != ELFCLASS
))
894 goto got_wrong_format_error
;
896 /* Check that file's byte order matches xvec's */
897 switch (x_ehdr
.e_ident
[EI_DATA
])
899 case ELFDATA2MSB
: /* Big-endian */
900 if (!abfd
->xvec
->header_byteorder_big_p
)
901 goto got_wrong_format_error
;
903 case ELFDATA2LSB
: /* Little-endian */
904 if (abfd
->xvec
->header_byteorder_big_p
)
905 goto got_wrong_format_error
;
907 case ELFDATANONE
: /* No data encoding specified */
908 default: /* Unknown data encoding specified */
909 goto got_wrong_format_error
;
912 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
913 the tdata pointer in the bfd. FIXME: memory leak, see above. */
916 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
917 if (elf_tdata (abfd
) == NULL
)
918 goto got_no_memory_error
;
920 /* Now that we know the byte order, swap in the rest of the header */
921 i_ehdrp
= elf_elfheader (abfd
);
922 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
924 elf_debug_file (i_ehdrp
);
927 /* If there is no section header table, we're hosed. */
928 if (i_ehdrp
->e_shoff
== 0)
929 goto got_wrong_format_error
;
931 /* As a simple sanity check, verify that the what BFD thinks is the
932 size of each section header table entry actually matches the size
933 recorded in the file. */
934 if (i_ehdrp
->e_shentsize
!= sizeof (x_shdr
))
935 goto got_wrong_format_error
;
937 ebd
= get_elf_backend_data (abfd
);
939 /* Check that the ELF e_machine field matches what this particular
940 BFD format expects. */
941 if (ebd
->elf_machine_code
!= i_ehdrp
->e_machine
)
943 bfd_target
**target_ptr
;
945 if (ebd
->elf_machine_code
!= EM_NONE
)
946 goto got_wrong_format_error
;
948 /* This is the generic ELF target. Let it match any ELF target
949 for which we do not have a specific backend. */
950 for (target_ptr
= bfd_target_vector
; *target_ptr
!= NULL
; target_ptr
++)
952 struct elf_backend_data
*back
;
954 if ((*target_ptr
)->flavour
!= bfd_target_elf_flavour
)
956 back
= (struct elf_backend_data
*) (*target_ptr
)->backend_data
;
957 if (back
->elf_machine_code
== i_ehdrp
->e_machine
)
959 /* target_ptr is an ELF backend which matches this
960 object file, so reject the generic ELF target. */
961 goto got_wrong_format_error
;
967 /* Set the flags and architecture before calling the backend so that
968 it can override them. */
969 if (i_ehdrp
->e_type
== ET_EXEC
)
970 abfd
->flags
|= EXEC_P
;
971 else if (i_ehdrp
->e_type
== ET_DYN
)
972 abfd
->flags
|= DYNAMIC
;
974 bfd_default_set_arch_mach (abfd
, ebd
->arch
, 0);
976 /* Remember the entry point specified in the ELF file header. */
977 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
979 /* Let the backend double check the format and override global
981 if (ebd
->elf_backend_object_p
)
983 if ((*ebd
->elf_backend_object_p
) (abfd
) == false)
984 goto got_wrong_format_error
;
987 /* Allocate space for a copy of the section header table in
988 internal form, seek to the section header table in the file,
989 read it in, and convert it to internal form. */
990 i_shdrp
= (Elf_Internal_Shdr
*)
991 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
);
992 elf_elfsections (abfd
) =
993 (Elf_Internal_Shdr
**) bfd_alloc (abfd
, sizeof (i_shdrp
) * i_ehdrp
->e_shnum
);
994 if (!i_shdrp
|| !elf_elfsections (abfd
))
995 goto got_no_memory_error
;
996 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) == -1)
998 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1000 if (bfd_read ((PTR
) & x_shdr
, sizeof x_shdr
, 1, abfd
) != sizeof (x_shdr
))
1002 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
1003 elf_elfsections (abfd
)[shindex
] = i_shdrp
+ shindex
;
1005 /* If this is a .dynamic section, mark the object file as being
1006 dynamically linked. */
1007 if (i_shdrp
[shindex
].sh_type
== SHT_DYNAMIC
)
1008 abfd
->flags
|= DYNAMIC
;
1010 if (i_ehdrp
->e_shstrndx
)
1012 bfd_section_from_shdr (abfd
, i_ehdrp
->e_shstrndx
);
1015 /* Read in the string table containing the names of the sections. We
1016 will need the base pointer to this table later. */
1017 /* We read this inline now, so that we don't have to go through
1018 bfd_section_from_shdr with it (since this particular strtab is
1019 used to find all of the ELF section names.) */
1021 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
1023 goto got_wrong_format_error
;
1025 /* Once all of the section headers have been read and converted, we
1026 can start processing them. Note that the first section header is
1027 a dummy placeholder entry, so we ignore it.
1029 We also watch for the symbol table section and remember the file
1030 offset and section size for both the symbol table section and the
1031 associated string table section. */
1033 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1035 bfd_section_from_shdr (abfd
, shindex
);
1038 return (abfd
->xvec
);
1040 /* If we are going to use goto's to avoid duplicating error setting
1041 and return(NULL) code, then this at least makes it more maintainable. */
1043 got_wrong_format_error
:
1044 bfd_set_error (bfd_error_wrong_format
);
1046 got_no_memory_error
:
1047 bfd_set_error (bfd_error_no_memory
);
1050 elf_tdata (abfd
) = preserved_tdata
;
1055 /* ELF .o/exec file writing */
1057 /* Takes a bfd and a symbol, returns a pointer to the elf specific area
1058 of the symbol if there is one. */
1059 static INLINE elf_symbol_type
*
1060 elf_symbol_from (ignore_abfd
, symbol
)
1064 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
1067 if (symbol
->the_bfd
->tdata
.elf_obj_data
== (struct elf_obj_tdata
*) NULL
)
1070 return (elf_symbol_type
*) symbol
;
1073 /* Create ELF output from BFD sections.
1075 Essentially, just create the section header and forget about the program
1079 elf_make_sections (abfd
, asect
, obj
)
1084 /* most of what is in bfd_shdr_from_section goes in here... */
1085 /* and all of these sections generate at *least* one ELF section. */
1086 Elf_Internal_Shdr
*this_hdr
;
1087 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1089 this_hdr
->sh_addr
= asect
->vma
;
1090 this_hdr
->sh_size
= asect
->_raw_size
;
1091 /* contents already set by elf_set_section_contents */
1093 if (asect
->flags
& SEC_RELOC
)
1095 /* emit a reloc section, and thus strtab and symtab... */
1096 Elf_Internal_Shdr
*rela_hdr
;
1097 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1099 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1101 /* orelocation has the data, reloc_count has the count... */
1104 rela_hdr
->sh_type
= SHT_RELA
;
1105 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1108 /* REL relocations */
1110 rela_hdr
->sh_type
= SHT_REL
;
1111 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rel
);
1113 rela_hdr
->sh_flags
= 0;
1114 rela_hdr
->sh_addr
= 0;
1115 rela_hdr
->sh_offset
= 0;
1117 /* FIXME: Systems I've checked use an alignment of 4, but it is
1118 possible that some systems use a different alignment. */
1119 rela_hdr
->sh_addralign
= 4;
1123 if (asect
->flags
& SEC_ALLOC
)
1125 this_hdr
->sh_flags
|= SHF_ALLOC
;
1126 if (asect
->flags
& SEC_LOAD
)
1128 /* @@ Do something with sh_type? */
1133 /* If this section is not part of the program image during
1134 execution, leave the address fields at 0. */
1135 this_hdr
->sh_addr
= 0;
1138 if (!(asect
->flags
& SEC_READONLY
))
1139 this_hdr
->sh_flags
|= SHF_WRITE
;
1141 if (asect
->flags
& SEC_CODE
)
1142 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1146 write_relocs (abfd
, sec
, xxx
)
1151 Elf_Internal_Shdr
*rela_hdr
;
1152 Elf_External_Rela
*outbound_relocas
;
1153 Elf_External_Rel
*outbound_relocs
;
1155 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1156 asymbol
*last_sym
= 0;
1157 int last_sym_idx
= 9999999; /* should always be written before use */
1159 if ((sec
->flags
& SEC_RELOC
) == 0)
1161 /* Flags are sometimes inconsistent. */
1162 if (sec
->reloc_count
== 0)
1165 rela_hdr
= &elf_section_data (sec
)->rel_hdr
;
1167 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* sec
->reloc_count
;
1168 rela_hdr
->contents
= (void *) bfd_alloc (abfd
, rela_hdr
->sh_size
);
1169 if (!rela_hdr
->contents
)
1171 bfd_set_error (bfd_error_no_memory
);
1172 abort (); /* FIXME */
1175 /* orelocation has the data, reloc_count has the count... */
1178 outbound_relocas
= (Elf_External_Rela
*) rela_hdr
->contents
;
1180 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1182 Elf_Internal_Rela dst_rela
;
1183 Elf_External_Rela
*src_rela
;
1188 ptr
= sec
->orelocation
[idx
];
1189 src_rela
= outbound_relocas
+ idx
;
1190 if (!(abfd
->flags
& EXEC_P
))
1191 dst_rela
.r_offset
= ptr
->address
- sec
->vma
;
1193 dst_rela
.r_offset
= ptr
->address
;
1195 sym
= *ptr
->sym_ptr_ptr
;
1197 /* If SYM is a section symbol for an input section, which
1198 has been combined with other similar input sections (ld -r),
1199 then adjust the addend by the output_offset of sym->section.
1201 Apparently elf_symbol_from_bfd_symbol doesn't provide a 1:1
1202 mapping from bfd symbol to elf symbols in this case. */
1203 if ((sym
->flags
& BSF_SECTION_SYM
)
1205 ptr
->addend
+= sym
->section
->output_offset
;
1207 if (sym
== last_sym
)
1212 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1214 dst_rela
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1216 dst_rela
.r_addend
= ptr
->addend
;
1217 elf_swap_reloca_out (abfd
, &dst_rela
, src_rela
);
1221 /* REL relocations */
1223 outbound_relocs
= (Elf_External_Rel
*) rela_hdr
->contents
;
1225 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1227 Elf_Internal_Rel dst_rel
;
1228 Elf_External_Rel
*src_rel
;
1233 ptr
= sec
->orelocation
[idx
];
1234 sym
= *ptr
->sym_ptr_ptr
;
1235 src_rel
= outbound_relocs
+ idx
;
1236 if (!(abfd
->flags
& EXEC_P
))
1237 dst_rel
.r_offset
= ptr
->address
- sec
->vma
;
1239 dst_rel
.r_offset
= ptr
->address
;
1241 if (sym
== last_sym
)
1246 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1248 dst_rel
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1250 elf_swap_reloc_out (abfd
, &dst_rel
, src_rel
);
1256 fix_up_strtabs (abfd
, asect
, obj
)
1261 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (asect
)->this_hdr
;
1262 int this_idx
= elf_section_data (asect
)->this_idx
;
1264 /* @@ Check flags! */
1265 if (!strncmp (asect
->name
, ".stab", 5)
1266 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
1268 size_t len
= strlen (asect
->name
) + 1;
1269 char *s
= (char *) malloc (len
);
1271 /* FIXME: Should deal more gracefully with errors. */
1273 strcpy (s
, asect
->name
);
1275 asect
= bfd_get_section_by_name (abfd
, s
);
1279 elf_section_data (asect
)->this_hdr
.sh_link
= this_idx
;
1280 /* @@ Assuming 32 bits! */
1281 elf_section_data (asect
)->this_hdr
.sh_entsize
= 0xc;
1283 this_hdr
->sh_type
= SHT_STRTAB
;
1288 elf_fake_sections (abfd
, asect
, obj
)
1293 /* most of what is in bfd_shdr_from_section goes in here... */
1294 /* and all of these sections generate at *least* one ELF section. */
1296 Elf_Internal_Shdr
*this_hdr
;
1297 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1299 bfd_add_to_strtab (abfd
, elf_shstrtab (abfd
), asect
->name
);
1300 /* We need to log the type *now* so that elf_section_from_bfd_section
1301 can find us... have to set rawdata too. */
1302 this_hdr
->rawdata
= (void *) asect
;
1303 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1304 if ((asect
->flags
& SEC_ALLOC
) && (asect
->flags
& SEC_LOAD
))
1305 this_hdr
->sh_type
= SHT_PROGBITS
;
1306 else if ((asect
->flags
& SEC_ALLOC
) && ((asect
->flags
& SEC_LOAD
) == 0))
1308 BFD_ASSERT (strcmp (asect
->name
, ".bss") == 0
1309 || strcmp (asect
->name
, ".sbss") == 0);
1310 this_hdr
->sh_type
= SHT_NOBITS
;
1312 /* FIXME I am not sure how to detect a .note section from the flags
1313 word of an `asection'. */
1314 else if (!strcmp (asect
->name
, ".note"))
1315 this_hdr
->sh_type
= SHT_NOTE
;
1317 this_hdr
->sh_type
= SHT_PROGBITS
;
1319 this_hdr
->sh_flags
= 0;
1320 this_hdr
->sh_addr
= 0;
1321 this_hdr
->sh_size
= 0;
1322 this_hdr
->sh_entsize
= 0;
1323 this_hdr
->sh_info
= 0;
1324 this_hdr
->sh_link
= 0;
1325 this_hdr
->sh_offset
= 0;
1328 /* Now, check for processor-specific section types. */
1330 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1332 if (bed
->elf_backend_fake_sections
)
1333 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1337 /* Emit a strtab and symtab, and possibly a reloc section. */
1338 Elf_Internal_Shdr
*rela_hdr
;
1340 /* Note that only one symtab is used, so just remember it
1343 if (asect
->flags
& SEC_RELOC
)
1345 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1347 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1349 bfd_add_2_to_strtab (abfd
, elf_shstrtab (abfd
),
1350 use_rela_p
? ".rela" : ".rel",
1352 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1353 rela_hdr
->sh_entsize
= (use_rela_p
1354 ? sizeof (Elf_External_Rela
)
1355 : sizeof (Elf_External_Rel
));
1357 rela_hdr
->sh_flags
= 0;
1358 rela_hdr
->sh_addr
= 0;
1359 rela_hdr
->sh_size
= 0;
1360 rela_hdr
->sh_offset
= 0;
1362 /* FIXME: Systems I've checked use an alignment of 4, but some
1363 systems may use a different alignment. */
1364 rela_hdr
->sh_addralign
= 4;
1369 if (asect
->flags
& SEC_ALLOC
)
1371 this_hdr
->sh_flags
|= SHF_ALLOC
;
1372 if (asect
->flags
& SEC_LOAD
)
1374 /* @@ Do something with sh_type? */
1377 if (!(asect
->flags
& SEC_READONLY
))
1378 this_hdr
->sh_flags
|= SHF_WRITE
;
1379 if (asect
->flags
& SEC_CODE
)
1380 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1383 /* Map symbol from it's internal number to the external number, moving
1384 all local symbols to be at the head of the list. */
1387 sym_is_global (abfd
, sym
)
1391 /* If the backend has a special mapping, use it. */
1392 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1393 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1396 if (sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
))
1398 if (sym
->flags
& BSF_LOCAL
)
1402 if (sym
->section
== 0)
1404 /* Is this valid? */
1409 if (sym
->section
== &bfd_und_section
)
1411 if (bfd_is_com_section (sym
->section
))
1413 if (sym
->flags
& (BSF_LOCAL
| BSF_SECTION_SYM
| BSF_FILE
))
1419 elf_map_symbols (abfd
)
1422 int symcount
= bfd_get_symcount (abfd
);
1423 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1424 asymbol
**sect_syms
;
1426 int num_globals
= 0;
1427 int num_locals2
= 0;
1428 int num_globals2
= 0;
1430 int num_sections
= 0;
1431 Elf_Sym_Extra
*sym_extra
;
1436 fprintf (stderr
, "elf_map_symbols\n");
1440 /* Add local symbols for each section for which there are relocs.
1441 FIXME: How can we tell which sections have relocs at this point?
1442 Will reloc_count always be accurate? Actually, I think most ELF
1443 targets create section symbols for all sections anyhow. */
1444 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1446 if (max_index
< asect
->index
)
1447 max_index
= asect
->index
;
1451 elf_num_section_syms (abfd
) = max_index
;
1452 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1453 elf_section_syms (abfd
) = sect_syms
;
1457 bfd_set_error (bfd_error_no_memory
);
1461 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1463 asymbol
*sym
= bfd_make_empty_symbol (abfd
);
1466 bfd_set_error (bfd_error_no_memory
);
1469 sym
->the_bfd
= abfd
;
1470 sym
->name
= asect
->name
;
1471 sym
->value
= asect
->vma
;
1472 sym
->flags
= BSF_SECTION_SYM
;
1473 sym
->section
= asect
;
1474 sect_syms
[asect
->index
] = sym
;
1478 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1479 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1486 syms
= (asymbol
**) bfd_realloc (abfd
, syms
,
1487 ((symcount
+ num_sections
+ 1)
1488 * sizeof (asymbol
*)));
1490 syms
= (asymbol
**) bfd_alloc (abfd
,
1491 (num_sections
+ 1) * sizeof (asymbol
*));
1494 bfd_set_error (bfd_error_no_memory
);
1498 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1500 if (sect_syms
[asect
->index
])
1501 syms
[symcount
++] = sect_syms
[asect
->index
];
1504 syms
[symcount
] = (asymbol
*) 0;
1505 bfd_set_symtab (abfd
, syms
, symcount
);
1508 elf_sym_extra (abfd
) = sym_extra
1509 = (Elf_Sym_Extra
*) bfd_alloc (abfd
, symcount
* sizeof (Elf_Sym_Extra
));
1512 bfd_set_error (bfd_error_no_memory
);
1516 /* Identify and classify all of the symbols. */
1517 for (idx
= 0; idx
< symcount
; idx
++)
1519 if (!sym_is_global (abfd
, syms
[idx
]))
1525 /* Now provide mapping information. Add +1 for skipping over the
1527 for (idx
= 0; idx
< symcount
; idx
++)
1529 syms
[idx
]->udata
= (PTR
) & sym_extra
[idx
];
1530 if (!sym_is_global (abfd
, syms
[idx
]))
1531 sym_extra
[idx
].elf_sym_num
= 1 + num_locals2
++;
1533 sym_extra
[idx
].elf_sym_num
= 1 + num_locals
+ num_globals2
++;
1536 elf_num_locals (abfd
) = num_locals
;
1537 elf_num_globals (abfd
) = num_globals
;
1541 static boolean
assign_section_numbers ();
1542 static boolean
assign_file_positions_except_relocs ();
1545 elf_compute_section_file_positions (abfd
)
1548 bfd_map_over_sections (abfd
, elf_fake_sections
, 0);
1550 if (!assign_section_numbers (abfd
))
1553 bfd_map_over_sections (abfd
, elf_make_sections
, 0);
1555 bfd_map_over_sections (abfd
, fix_up_strtabs
, 0); /* .stab/.stabstr &c */
1557 if (swap_out_syms (abfd
) == false)
1560 if (!assign_file_positions_except_relocs (abfd
))
1567 elf_write_phdrs (abfd
, i_ehdrp
, i_phdrp
, phdr_cnt
)
1569 Elf_Internal_Ehdr
*i_ehdrp
;
1570 Elf_Internal_Phdr
*i_phdrp
;
1571 unsigned short phdr_cnt
;
1573 /* first program header entry goes after the file header */
1574 int outbase
= i_ehdrp
->e_phoff
;
1576 Elf_External_Phdr x_phdr
;
1578 for (i
= 0; i
< phdr_cnt
; i
++)
1580 elf_swap_phdr_out (abfd
, i_phdrp
+ i
, &x_phdr
);
1581 if (bfd_seek (abfd
, outbase
, SEEK_SET
) != 0
1582 || (bfd_write ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
1583 != sizeof (x_phdr
)))
1585 outbase
+= sizeof (x_phdr
);
1591 static const Elf_Internal_Shdr null_shdr
;
1593 /* Assign all ELF section numbers. The dummy first section is handled here
1594 too. The link/info pointers for the standard section types are filled
1595 in here too, while we're at it. (Link pointers for .stab sections are
1596 not filled in here.) */
1598 assign_section_numbers (abfd
)
1601 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1603 int section_number
= 1;
1605 Elf_Internal_Shdr
**i_shdrp
;
1607 t
->shstrtab_hdr
.sh_size
= elf_shstrtab (abfd
)->length
;
1608 t
->shstrtab_hdr
.contents
= (void *) elf_shstrtab (abfd
)->tab
;
1609 shstrtab_length_fixed
= 1;
1611 t
->shstrtab_section
= section_number
++;
1612 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1615 t
->symtab_section
= section_number
++;
1616 t
->strtab_section
= section_number
++;
1617 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1619 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1621 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1622 d
->this_idx
= section_number
++;
1623 if (sec
->flags
& SEC_RELOC
)
1625 d
->rel_idx
= section_number
++;
1626 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1627 d
->rel_hdr
.sh_info
= d
->this_idx
;
1631 /* No handling for per-section string tables currently. */
1633 elf_elfheader (abfd
)->e_shnum
= section_number
;
1635 /* Set up the list of section header pointers, in agreement with the
1637 i_shdrp
= (Elf_Internal_Shdr
**)
1638 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*));
1641 bfd_set_error (bfd_error_no_memory
);
1644 elf_elfsections (abfd
) = i_shdrp
;
1645 for (i
= 0; i
< section_number
; i
++)
1648 i_shdrp
[0] = (Elf_Internal_Shdr
*) & null_shdr
;
1649 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1652 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1653 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1655 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1657 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1658 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1660 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1662 /* Make sure we got everything.... */
1663 for (i
= 0; i
< section_number
; i
++)
1664 if (i_shdrp
[i
] == 0)
1669 static INLINE file_ptr
1670 assign_file_position_for_section (i_shdrp
, offset
)
1671 Elf_Internal_Shdr
*i_shdrp
;
1676 if (i_shdrp
->sh_addralign
!= 0)
1677 align
= i_shdrp
->sh_addralign
;
1680 i_shdrp
->sh_offset
= offset
= BFD_ALIGN (offset
, align
);
1681 if (i_shdrp
->rawdata
!= NULL
)
1682 ((asection
*) i_shdrp
->rawdata
)->filepos
= offset
;
1683 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1684 offset
+= i_shdrp
->sh_size
;
1688 static INLINE file_ptr
1689 align_file_position (off
)
1692 return (off
+ FILE_ALIGN
- 1) & ~(FILE_ALIGN
- 1);
1695 static INLINE file_ptr
1696 assign_file_positions_for_symtab_and_strtabs (abfd
, off
)
1700 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1702 off
= align_file_position (off
);
1703 off
= assign_file_position_for_section (&t
->symtab_hdr
, off
);
1704 off
= assign_file_position_for_section (&t
->shstrtab_hdr
, off
);
1705 off
= assign_file_position_for_section (&t
->strtab_hdr
, off
);
1711 bfd_vma low
, mem_size
;
1715 struct seg_info
*next
;
1719 map_program_segments (abfd
)
1722 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1723 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1724 Elf_Internal_Shdr
*i_shdrp
;
1725 Elf_Internal_Phdr
*phdr
;
1727 unsigned int i
, n_left
= 0;
1728 file_ptr lowest_offset
= 0;
1729 struct seg_info
*seg
= NULL
;
1731 done
= (char *) malloc (i_ehdrp
->e_shnum
);
1732 if (done
== NULL
&& i_ehdrp
->e_shnum
!= 0)
1734 bfd_set_error (bfd_error_no_memory
);
1737 memset (done
, 0, i_ehdrp
->e_shnum
);
1738 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1740 i_shdrp
= i_shdrpp
[i
];
1741 /* If it's going to be mapped in, it's been assigned a position. */
1742 if (i_shdrp
->sh_offset
+ 1 == 0)
1744 /* Well, not really, but we won't process it here. */
1748 if (i_shdrp
->sh_offset
< lowest_offset
1749 || lowest_offset
== 0)
1750 lowest_offset
= i_shdrp
->sh_offset
;
1751 /* Only interested in PROGBITS or NOBITS for generating segments. */
1752 switch (i_shdrp
->sh_type
)
1765 bfd_vma lowest_vma
= -1, high
;
1769 struct seg_info
*snew
;
1770 struct seg_info
**s_ptr
;
1772 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1774 i_shdrp
= i_shdrpp
[i
];
1775 if (!done
[i
] && i_shdrp
->sh_addr
< lowest_vma
)
1777 lowest_vma
= i_shdrp
->sh_addr
;
1783 /* So now we know the lowest vma of any unassigned sections; start
1785 snew
= (struct seg_info
*) bfd_alloc (abfd
, sizeof (struct seg_info
));
1788 bfd_set_error (bfd_error_no_memory
);
1792 while (*s_ptr
!= (struct seg_info
*) NULL
)
1793 s_ptr
= &(*s_ptr
)->next
;
1796 snew
->low
= lowest_vma
;
1797 i_shdrp
= i_shdrpp
[low_sec
];
1798 snew
->start_pos
= i_shdrp
->sh_offset
;
1799 snew
->sh_flags
= i_shdrp
->sh_flags
;
1800 done
[low_sec
] = 1, n_left
--;
1801 mem_size
= i_shdrp
->sh_size
;
1802 high
= lowest_vma
+ i_shdrp
->sh_size
;
1804 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1805 file_size
= i_shdrp
->sh_size
;
1807 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1813 i_shdrp
= i_shdrpp
[i
];
1814 /* position of next byte on disk */
1815 f1
= snew
->start_pos
+ file_size
;
1816 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1818 if (i_shdrp
->sh_offset
- f1
!= i_shdrp
->sh_addr
- high
)
1820 if (file_size
!= mem_size
)
1824 /* sh_type == NOBITS */
1826 /* If the section in question has no contents in the disk
1827 file, we really don't care where it supposedly starts.
1828 But we don't want to bother merging it into this segment
1829 if it doesn't start on this memory page. */
1830 bfd_vma page1
, page2
;
1831 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1833 /* page number in address space of current end of snew */
1834 page1
= (high
- 1 + maxpagesize
- 1) / maxpagesize
;
1835 /* page number in address space of start of this section */
1836 page2
= (i_shdrp
->sh_addr
+ maxpagesize
- 1) / maxpagesize
;
1841 done
[i
] = 1, n_left
--;
1842 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1843 file_size
= i_shdrp
->sh_offset
+ i_shdrp
->sh_size
- snew
->start_pos
;
1844 mem_size
= i_shdrp
->sh_addr
+ i_shdrp
->sh_size
- snew
->low
;
1845 high
= i_shdrp
->sh_addr
+ i_shdrp
->sh_size
;
1848 snew
->file_size
= file_size
;
1849 snew
->mem_size
= mem_size
;
1851 /* Now do something with the list of segments we've built up. */
1853 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1858 for (s
= seg
; s
; s
= s
->next
)
1862 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
1863 sz
= sizeof (Elf_External_Phdr
) * n_segs
;
1864 if (align_file_position (i_ehdrp
->e_ehsize
) + sz
<= lowest_offset
)
1865 i_ehdrp
->e_phoff
= align_file_position (i_ehdrp
->e_ehsize
);
1868 i_ehdrp
->e_phoff
= align_file_position (elf_tdata (abfd
)->next_file_pos
);
1869 elf_tdata (abfd
)->next_file_pos
= i_ehdrp
->e_phoff
+ sz
;
1871 phdr
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
,
1872 n_segs
* sizeof (Elf_Internal_Phdr
));
1875 bfd_set_error (bfd_error_no_memory
);
1876 abort (); /* FIXME */
1878 elf_tdata (abfd
)->phdr
= phdr
;
1881 phdr
->p_type
= PT_LOAD
; /* only type we really support so far */
1882 phdr
->p_offset
= seg
->start_pos
;
1883 phdr
->p_vaddr
= seg
->low
;
1885 phdr
->p_filesz
= seg
->file_size
;
1886 phdr
->p_memsz
= seg
->mem_size
;
1887 phdr
->p_flags
= PF_R
;
1888 phdr
->p_align
= maxpagesize
; /* ? */
1889 if (seg
->sh_flags
& SHF_WRITE
)
1890 /* SysVr4 ELF docs say "data segments normally have read, write,
1891 and execute permissions." */
1892 phdr
->p_flags
|= (PF_W
| PF_X
);
1893 if (seg
->sh_flags
& SHF_EXECINSTR
)
1894 phdr
->p_flags
|= PF_X
;
1898 i_ehdrp
->e_phnum
= n_segs
;
1900 elf_write_phdrs (abfd
, i_ehdrp
, elf_tdata (abfd
)->phdr
, i_ehdrp
->e_phnum
);
1911 assign_file_positions_except_relocs (abfd
)
1914 /* For now, we ignore the possibility of having program segments, which
1915 may require some alignment in the file. That'll require padding, and
1916 some interesting calculations to optimize file space usage.
1918 Also, since the application may change the list of relocations for
1919 a given section, we don't figure them in here. We'll put them at the
1920 end of the file, at positions computed during bfd_close.
1922 The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ...
1923 or: <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */
1925 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1928 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1929 Elf_Internal_Shdr
*i_shdrp
;
1930 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1931 int exec_p
= (abfd
->flags
& EXEC_P
) != 0;
1932 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1934 /* Everything starts after the ELF file header. */
1935 off
= i_ehdrp
->e_ehsize
;
1939 /* Section headers. */
1940 off
= align_file_position (off
);
1941 i_ehdrp
->e_shoff
= off
;
1942 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1943 off
= assign_file_positions_for_symtab_and_strtabs (abfd
, off
);
1945 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1947 /* The symtab and strtab sections are placed by
1948 assign_file_positions_for_symtab_and_strtabs. */
1949 if (i
== t
->symtab_section
1950 || i
== t
->strtab_section
1951 || i
== t
->shstrtab_section
)
1954 i_shdrp
= i_shdrpp
[i
];
1955 if (i_shdrp
->sh_type
== SHT_REL
|| i_shdrp
->sh_type
== SHT_RELA
)
1957 i_shdrp
->sh_offset
= -1;
1962 if (maxpagesize
== 0)
1963 maxpagesize
= 1; /* make the arithmetic work */
1964 /* This isn't necessarily going to give the best packing, if the
1965 segments require padding between them, but since that isn't
1966 usually the case, this'll do. */
1967 if ((i_shdrp
->sh_flags
& SHF_ALLOC
) == 0)
1969 i_shdrp
->sh_offset
= -1;
1972 /* Blindly assume that the segments are ordered optimally. With
1973 the default LD script, they will be. */
1974 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1976 /* need big unsigned type */
1978 addtl_off
= i_shdrp
->sh_addr
- off
;
1979 addtl_off
= addtl_off
% maxpagesize
;
1986 off
= assign_file_position_for_section (i_shdrp
, off
);
1989 && i_shdrp
->sh_type
== SHT_NOBITS
1990 && (i
== i_ehdrp
->e_shnum
1991 || i_shdrpp
[i
+ 1]->sh_type
!= SHT_NOBITS
))
1993 /* Skip to the next page to ensure that when the file is
1994 loaded the bss section is loaded with zeroes. I don't
1995 know if this is required on all platforms, but it
1996 shouldn't really hurt. */
1997 off
= BFD_ALIGN (off
, maxpagesize
);
2001 && (abfd
->flags
& D_PAGED
) != 0
2002 && get_elf_backend_data (abfd
)->maxpagesize
> 1
2003 && i_shdrp
->sh_type
== SHT_PROGBITS
2004 && (i_shdrp
->sh_flags
& SHF_ALLOC
)
2005 && (i_shdrp
->sh_offset
- i_shdrp
->sh_addr
) % get_elf_backend_data (abfd
)->maxpagesize
!= 0)
2010 elf_tdata (abfd
)->next_file_pos
= off
;
2011 if (!map_program_segments (abfd
))
2013 off
= elf_tdata (abfd
)->next_file_pos
;
2015 /* Section headers. */
2016 off
= align_file_position (off
);
2017 i_ehdrp
->e_shoff
= off
;
2018 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
2020 off
= assign_file_positions_for_symtab_and_strtabs (abfd
, off
);
2022 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
2024 i_shdrp
= i_shdrpp
[i
];
2025 if (i_shdrp
->sh_offset
+ 1 == 0
2026 && i_shdrp
->sh_type
!= SHT_REL
2027 && i_shdrp
->sh_type
!= SHT_RELA
)
2028 off
= assign_file_position_for_section (i_shdrp
, off
);
2031 elf_tdata (abfd
)->next_file_pos
= off
;
2039 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2040 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2041 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2043 struct strtab
*shstrtab
;
2045 i_ehdrp
= elf_elfheader (abfd
);
2046 i_shdrp
= elf_elfsections (abfd
);
2048 shstrtab
= bfd_new_strtab (abfd
);
2052 elf_shstrtab (abfd
) = shstrtab
;
2054 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
2055 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
2056 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
2057 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
2059 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS
;
2060 i_ehdrp
->e_ident
[EI_DATA
] =
2061 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
2062 i_ehdrp
->e_ident
[EI_VERSION
] = EV_CURRENT
;
2064 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2065 i_ehdrp
->e_ident
[count
] = 0;
2067 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
) ? ET_EXEC
: ET_REL
;
2068 switch (bfd_get_arch (abfd
))
2070 case bfd_arch_unknown
:
2071 i_ehdrp
->e_machine
= EM_NONE
;
2073 case bfd_arch_sparc
:
2074 i_ehdrp
->e_machine
= EM_SPARC
;
2075 /* start-sanitize-v9 */
2077 i_ehdrp
->e_machine
= EM_SPARC64
;
2079 /* end-sanitize-v9 */
2082 i_ehdrp
->e_machine
= EM_386
;
2085 i_ehdrp
->e_machine
= EM_68K
;
2088 i_ehdrp
->e_machine
= EM_88K
;
2091 i_ehdrp
->e_machine
= EM_860
;
2093 case bfd_arch_mips
: /* MIPS Rxxxx */
2094 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
2097 i_ehdrp
->e_machine
= EM_HPPA
;
2099 case bfd_arch_powerpc
:
2100 i_ehdrp
->e_machine
= EM_CYGNUS_POWERPC
;
2102 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2104 i_ehdrp
->e_machine
= EM_NONE
;
2106 i_ehdrp
->e_version
= EV_CURRENT
;
2107 i_ehdrp
->e_ehsize
= sizeof (Elf_External_Ehdr
);
2109 /* no program header, for now. */
2110 i_ehdrp
->e_phoff
= 0;
2111 i_ehdrp
->e_phentsize
= 0;
2112 i_ehdrp
->e_phnum
= 0;
2114 /* each bfd section is section header entry */
2115 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
2116 i_ehdrp
->e_shentsize
= sizeof (Elf_External_Shdr
);
2118 /* if we're building an executable, we'll need a program header table */
2119 if (abfd
->flags
& EXEC_P
)
2121 /* it all happens later */
2123 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
2125 /* elf_build_phdrs() returns a (NULL-terminated) array of
2126 Elf_Internal_Phdrs */
2127 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
2128 i_ehdrp
->e_phoff
= outbase
;
2129 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
2134 i_ehdrp
->e_phentsize
= 0;
2136 i_ehdrp
->e_phoff
= 0;
2139 elf_tdata (abfd
)->symtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2141 elf_tdata (abfd
)->strtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2143 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2149 swap_out_syms (abfd
)
2152 if (!elf_map_symbols (abfd
))
2155 /* Dump out the symtabs. */
2157 int symcount
= bfd_get_symcount (abfd
);
2158 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2159 struct strtab
*stt
= bfd_new_strtab (abfd
);
2160 Elf_Internal_Shdr
*symtab_hdr
;
2161 Elf_Internal_Shdr
*symstrtab_hdr
;
2162 Elf_External_Sym
*outbound_syms
;
2167 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2168 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2169 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
2170 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2171 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2173 /* FIXME: Systems I've checked use 4 byte alignment for .symtab,
2174 but it is possible that there are systems which use a different
2176 symtab_hdr
->sh_addralign
= 4;
2178 /* see assert in elf_fake_sections that supports this: */
2179 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2180 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2182 outbound_syms
= (Elf_External_Sym
*)
2183 bfd_alloc (abfd
, (1 + symcount
) * sizeof (Elf_External_Sym
));
2186 bfd_set_error (bfd_error_no_memory
);
2189 /* now generate the data (for "contents") */
2191 /* Fill in zeroth symbol and swap it out. */
2192 Elf_Internal_Sym sym
;
2198 sym
.st_shndx
= SHN_UNDEF
;
2199 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
2201 for (idx
= 0; idx
< symcount
; idx
++)
2203 Elf_Internal_Sym sym
;
2204 bfd_vma value
= syms
[idx
]->value
;
2206 if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2207 /* Section symbols have no names. */
2210 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
2212 if (bfd_is_com_section (syms
[idx
]->section
))
2214 /* ELF common symbols put the alignment into the `value' field,
2215 and the size into the `size' field. This is backwards from
2216 how BFD handles it, so reverse it here. */
2217 sym
.st_size
= value
;
2218 /* Should retrieve this from somewhere... */
2220 sym
.st_shndx
= elf_section_from_bfd_section (abfd
,
2221 syms
[idx
]->section
);
2225 asection
*sec
= syms
[idx
]->section
;
2226 elf_symbol_type
*type_ptr
;
2229 if (sec
->output_section
)
2231 value
+= sec
->output_offset
;
2232 sec
= sec
->output_section
;
2235 sym
.st_value
= value
;
2236 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2237 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2238 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec
);
2242 /* Writing this would be a hell of a lot easier if we had
2243 some decent documentation on bfd, and knew what to expect
2244 of the library, and what to demand of applications. For
2245 example, it appears that `objcopy' might not set the
2246 section of a symbol to be a section that is actually in
2248 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2249 BFD_ASSERT (sec2
!= 0);
2250 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec2
);
2251 BFD_ASSERT (shndx
!= -1);
2255 if (bfd_is_com_section (syms
[idx
]->section
))
2256 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2257 else if (syms
[idx
]->section
== &bfd_und_section
)
2258 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2259 else if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2260 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2261 else if (syms
[idx
]->flags
& BSF_FILE
)
2262 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2265 int bind
= STB_LOCAL
;
2266 int type
= STT_OBJECT
;
2267 unsigned int flags
= syms
[idx
]->flags
;
2269 if (flags
& BSF_LOCAL
)
2271 else if (flags
& BSF_WEAK
)
2273 else if (flags
& BSF_GLOBAL
)
2276 if (flags
& BSF_FUNCTION
)
2279 sym
.st_info
= ELF_ST_INFO (bind
, type
);
2283 elf_swap_symbol_out (abfd
, &sym
,
2285 + elf_sym_extra (abfd
)[idx
].elf_sym_num
));
2288 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2289 symstrtab_hdr
->contents
= (PTR
) stt
->tab
;
2290 symstrtab_hdr
->sh_size
= stt
->length
;
2291 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2293 symstrtab_hdr
->sh_flags
= 0;
2294 symstrtab_hdr
->sh_addr
= 0;
2295 symstrtab_hdr
->sh_entsize
= 0;
2296 symstrtab_hdr
->sh_link
= 0;
2297 symstrtab_hdr
->sh_info
= 0;
2298 symstrtab_hdr
->sh_addralign
= 1;
2299 symstrtab_hdr
->size
= 0;
2302 /* put the strtab out too... */
2304 Elf_Internal_Shdr
*this_hdr
;
2306 this_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2307 this_hdr
->contents
= (PTR
) elf_shstrtab (abfd
)->tab
;
2308 this_hdr
->sh_size
= elf_shstrtab (abfd
)->length
;
2309 this_hdr
->sh_type
= SHT_STRTAB
;
2310 this_hdr
->sh_flags
= 0;
2311 this_hdr
->sh_addr
= 0;
2312 this_hdr
->sh_entsize
= 0;
2313 this_hdr
->sh_addralign
= 1;
2320 write_shdrs_and_ehdr (abfd
)
2323 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
2324 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2325 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
2326 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2328 struct strtab
*shstrtab
;
2330 i_ehdrp
= elf_elfheader (abfd
);
2331 i_shdrp
= elf_elfsections (abfd
);
2332 shstrtab
= elf_shstrtab (abfd
);
2334 /* swap the header before spitting it out... */
2337 elf_debug_file (i_ehdrp
);
2339 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
2340 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0
2341 || (bfd_write ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
)
2342 != sizeof (x_ehdr
)))
2345 /* at this point we've concocted all the ELF sections... */
2346 x_shdrp
= (Elf_External_Shdr
*)
2347 bfd_alloc (abfd
, sizeof (*x_shdrp
) * (i_ehdrp
->e_shnum
));
2350 bfd_set_error (bfd_error_no_memory
);
2354 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2357 elf_debug_section (shstrtab
->tab
+ i_shdrp
[count
]->sh_name
, count
,
2360 elf_swap_shdr_out (abfd
, i_shdrp
[count
], x_shdrp
+ count
);
2362 if (bfd_seek (abfd
, (file_ptr
) i_ehdrp
->e_shoff
, SEEK_SET
) != 0
2363 || (bfd_write ((PTR
) x_shdrp
, sizeof (*x_shdrp
), i_ehdrp
->e_shnum
, abfd
)
2364 != sizeof (*x_shdrp
) * i_ehdrp
->e_shnum
))
2367 /* need to dump the string table too... */
2373 assign_file_positions_for_relocs (abfd
)
2376 file_ptr off
= elf_tdata (abfd
)->next_file_pos
;
2378 Elf_Internal_Shdr
**shdrpp
= elf_elfsections (abfd
);
2379 Elf_Internal_Shdr
*shdrp
;
2380 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
2383 if (shdrp
->sh_type
!= SHT_REL
&& shdrp
->sh_type
!= SHT_RELA
)
2385 off
= align_file_position (off
);
2386 off
= assign_file_position_for_section (shdrp
, off
);
2388 elf_tdata (abfd
)->next_file_pos
= off
;
2392 NAME(bfd_elf
,write_object_contents
) (abfd
)
2395 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2396 Elf_Internal_Ehdr
*i_ehdrp
;
2397 Elf_Internal_Shdr
**i_shdrp
;
2400 /* We don't know how to write dynamic objects. Specifically, we
2401 don't know how to construct the program header. */
2402 if ((abfd
->flags
& DYNAMIC
) != 0)
2404 fprintf (stderr
, "Writing ELF dynamic objects is not supported\n");
2405 bfd_set_error (bfd_error_wrong_format
);
2409 if (abfd
->output_has_begun
== false)
2411 /* Do any elf backend specific processing first. */
2412 if (bed
->elf_backend_begin_write_processing
)
2413 (*bed
->elf_backend_begin_write_processing
) (abfd
);
2415 if (prep_headers (abfd
) == false)
2417 if (elf_compute_section_file_positions (abfd
) == false)
2419 abfd
->output_has_begun
= true;
2422 i_shdrp
= elf_elfsections (abfd
);
2423 i_ehdrp
= elf_elfheader (abfd
);
2425 bfd_map_over_sections (abfd
, write_relocs
, (PTR
) 0);
2426 assign_file_positions_for_relocs (abfd
);
2428 /* After writing the headers, we need to write the sections too... */
2429 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2431 if (bed
->elf_backend_section_processing
)
2432 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2433 if (i_shdrp
[count
]->contents
)
2435 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
2436 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
2438 != i_shdrp
[count
]->sh_size
))
2443 if (bed
->elf_backend_final_write_processing
)
2444 (*bed
->elf_backend_final_write_processing
) (abfd
);
2446 return write_shdrs_and_ehdr (abfd
);
2449 /* Given an index of a section, retrieve a pointer to it. Note
2450 that for our purposes, sections are indexed by {1, 2, ...} with
2451 0 being an illegal index. */
2453 /* In the original, each ELF section went into exactly one BFD
2454 section. This doesn't really make sense, so we need a real mapping.
2455 The mapping has to hide in the Elf_Internal_Shdr since asection
2456 doesn't have anything like a tdata field... */
2459 section_from_elf_index (abfd
, index
)
2463 /* @@ Is bfd_com_section really correct in all the places it could
2464 be returned from this routine? */
2466 if (index
== SHN_ABS
)
2467 return &bfd_com_section
; /* not abs? */
2468 if (index
== SHN_COMMON
)
2469 return &bfd_com_section
;
2471 if (index
> elf_elfheader (abfd
)->e_shnum
)
2475 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[index
];
2477 switch (hdr
->sh_type
)
2479 /* ELF sections that map to BFD sections */
2483 bfd_section_from_shdr (abfd
, index
);
2484 return (struct sec
*) hdr
->rawdata
;
2487 return (struct sec
*) &bfd_abs_section
;
2492 /* given a section, search the header to find them... */
2494 elf_section_from_bfd_section (abfd
, asect
)
2498 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2500 Elf_Internal_Shdr
*hdr
;
2501 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2503 if (asect
== &bfd_abs_section
)
2505 if (asect
== &bfd_com_section
)
2507 if (asect
== &bfd_und_section
)
2510 for (index
= 0; index
< maxindex
; index
++)
2512 hdr
= i_shdrp
[index
];
2513 switch (hdr
->sh_type
)
2515 /* ELF sections that map to BFD sections */
2521 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2527 /* fix_up_strtabs will generate STRTAB sections with names
2529 if (!strncmp (asect
->name
, ".stab", 5)
2530 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
2534 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2542 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2544 if (bed
->elf_backend_section_from_bfd_section
)
2549 if ((*bed
->elf_backend_section_from_bfd_section
)
2550 (abfd
, hdr
, asect
, &retval
))
2560 /* given a symbol, return the bfd index for that symbol. */
2562 elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
2564 struct symbol_cache_entry
**asym_ptr_ptr
;
2566 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2568 flagword flags
= asym_ptr
->flags
;
2570 /* When gas creates relocations against local labels, it creates its
2571 own symbol for the section, but does put the symbol into the
2572 symbol chain, so udata is 0. When the linker is generating
2573 relocatable output, this section symbol may be for one of the
2574 input sections rather than the output section. */
2575 if (asym_ptr
->udata
== (PTR
) 0
2576 && (flags
& BSF_SECTION_SYM
)
2577 && asym_ptr
->section
)
2581 if (asym_ptr
->section
->output_section
!= NULL
)
2582 indx
= asym_ptr
->section
->output_section
->index
;
2584 indx
= asym_ptr
->section
->index
;
2585 if (elf_section_syms (abfd
)[indx
])
2586 asym_ptr
->udata
= elf_section_syms (abfd
)[indx
]->udata
;
2589 if (asym_ptr
->udata
)
2590 idx
= ((Elf_Sym_Extra
*) asym_ptr
->udata
)->elf_sym_num
;
2600 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
2601 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
, elf_symbol_flags (flags
));
2610 elf_slurp_symbol_table (abfd
, symptrs
, dynamic
)
2612 asymbol
**symptrs
; /* Buffer for generated bfd symbols */
2615 Elf_Internal_Shdr
*hdr
;
2616 long symcount
; /* Number of external ELF symbols */
2617 elf_symbol_type
*sym
; /* Pointer to current bfd symbol */
2618 elf_symbol_type
*symbase
; /* Buffer for generated bfd symbols */
2619 Elf_Internal_Sym i_sym
;
2620 Elf_External_Sym
*x_symp
= NULL
;
2622 /* Read each raw ELF symbol, converting from external ELF form to
2623 internal ELF form, and then using the information to create a
2624 canonical bfd symbol table entry.
2626 Note that we allocate the initial bfd canonical symbol buffer
2627 based on a one-to-one mapping of the ELF symbols to canonical
2628 symbols. We actually use all the ELF symbols, so there will be no
2629 space left over at the end. When we have all the symbols, we
2630 build the caller's pointer vector. */
2633 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2635 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2636 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2639 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2642 sym
= symbase
= NULL
;
2647 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2650 symbase
= ((elf_symbol_type
*)
2651 bfd_zalloc (abfd
, symcount
* sizeof (elf_symbol_type
)));
2652 if (symbase
== (elf_symbol_type
*) NULL
)
2654 bfd_set_error (bfd_error_no_memory
);
2659 /* Temporarily allocate room for the raw ELF symbols. */
2660 x_symp
= ((Elf_External_Sym
*)
2661 malloc (symcount
* sizeof (Elf_External_Sym
)));
2662 if (x_symp
== NULL
&& symcount
!= 0)
2664 bfd_set_error (bfd_error_no_memory
);
2668 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
2669 != symcount
* sizeof (Elf_External_Sym
))
2671 /* Skip first symbol, which is a null dummy. */
2672 for (i
= 1; i
< symcount
; i
++)
2674 elf_swap_symbol_in (abfd
, x_symp
+ i
, &i_sym
);
2675 memcpy (&sym
->internal_elf_sym
, &i_sym
, sizeof (Elf_Internal_Sym
));
2676 #ifdef ELF_KEEP_EXTSYM
2677 memcpy (&sym
->native_elf_sym
, x_symp
+ i
, sizeof (Elf_External_Sym
));
2679 sym
->symbol
.the_bfd
= abfd
;
2681 sym
->symbol
.name
= elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2684 sym
->symbol
.value
= i_sym
.st_value
;
2686 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
2688 sym
->symbol
.section
= section_from_elf_index (abfd
,
2691 else if (i_sym
.st_shndx
== SHN_ABS
)
2693 sym
->symbol
.section
= &bfd_abs_section
;
2695 else if (i_sym
.st_shndx
== SHN_COMMON
)
2697 sym
->symbol
.section
= &bfd_com_section
;
2698 /* Elf puts the alignment into the `value' field, and
2699 the size into the `size' field. BFD wants to see the
2700 size in the value field, and doesn't care (at the
2701 moment) about the alignment. */
2702 sym
->symbol
.value
= i_sym
.st_size
;
2704 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2706 sym
->symbol
.section
= &bfd_und_section
;
2709 sym
->symbol
.section
= &bfd_abs_section
;
2711 sym
->symbol
.value
-= sym
->symbol
.section
->vma
;
2713 switch (ELF_ST_BIND (i_sym
.st_info
))
2716 sym
->symbol
.flags
|= BSF_LOCAL
;
2719 sym
->symbol
.flags
|= BSF_GLOBAL
;
2722 sym
->symbol
.flags
|= BSF_WEAK
;
2726 switch (ELF_ST_TYPE (i_sym
.st_info
))
2729 sym
->symbol
.flags
|= BSF_SECTION_SYM
| BSF_DEBUGGING
;
2732 sym
->symbol
.flags
|= BSF_FILE
| BSF_DEBUGGING
;
2735 sym
->symbol
.flags
|= BSF_FUNCTION
;
2740 sym
->symbol
.flags
|= BSF_DYNAMIC
;
2742 /* Do some backend-specific processing on this symbol. */
2744 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2745 if (ebd
->elf_backend_symbol_processing
)
2746 (*ebd
->elf_backend_symbol_processing
) (abfd
, &sym
->symbol
);
2753 /* Do some backend-specific processing on this symbol table. */
2755 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2756 if (ebd
->elf_backend_symbol_table_processing
)
2757 (*ebd
->elf_backend_symbol_table_processing
) (abfd
, symbase
, symcount
);
2760 /* We rely on the zalloc to clear out the final symbol entry. */
2762 symcount
= sym
- symbase
;
2764 /* Fill in the user's symbol pointer vector if needed. */
2772 *symptrs
++ = &sym
->symbol
;
2775 *symptrs
= 0; /* Final null pointer */
2787 /* Return the number of bytes required to hold the symtab vector.
2789 Note that we base it on the count plus 1, since we will null terminate
2790 the vector allocated based on this size. However, the ELF symbol table
2791 always has a dummy entry as symbol #0, so it ends up even. */
2794 elf_get_symtab_upper_bound (abfd
)
2799 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2801 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2802 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2808 elf_get_dynamic_symtab_upper_bound (abfd
)
2813 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2815 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2816 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2822 This function return the number of bytes required to store the
2823 relocation information associated with section <<sect>>
2824 attached to bfd <<abfd>>
2828 elf_get_reloc_upper_bound (abfd
, asect
)
2832 if (asect
->flags
& SEC_RELOC
)
2834 /* either rel or rela */
2835 return elf_section_data (asect
)->rel_hdr
.sh_size
;
2842 elf_slurp_reloca_table (abfd
, asect
, symbols
)
2847 Elf_External_Rela
*native_relocs
;
2848 arelent
*reloc_cache
;
2853 if (asect
->relocation
)
2855 if (asect
->reloc_count
== 0)
2857 if (asect
->flags
& SEC_CONSTRUCTOR
)
2860 if (bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
) != 0)
2862 native_relocs
= (Elf_External_Rela
*)
2863 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rela
));
2866 bfd_set_error (bfd_error_no_memory
);
2869 if (bfd_read ((PTR
) native_relocs
,
2870 sizeof (Elf_External_Rela
), asect
->reloc_count
, abfd
)
2871 != sizeof (Elf_External_Rela
) * asect
->reloc_count
)
2874 reloc_cache
= (arelent
*)
2875 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2879 bfd_set_error (bfd_error_no_memory
);
2883 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2885 Elf_Internal_Rela dst
;
2886 Elf_External_Rela
*src
;
2888 cache_ptr
= reloc_cache
+ idx
;
2889 src
= native_relocs
+ idx
;
2890 elf_swap_reloca_in (abfd
, src
, &dst
);
2892 #ifdef RELOC_PROCESSING
2893 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2895 if (asect
->flags
& SEC_RELOC
)
2897 /* relocatable, so the offset is off of the section */
2898 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2902 /* non-relocatable, so the offset a virtual address */
2903 cache_ptr
->address
= dst
.r_offset
;
2906 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
2907 of zero points to the dummy symbol, which was not read into
2908 the symbol table SYMBOLS. */
2909 if (ELF_R_SYM (dst
.r_info
) == 0)
2910 cache_ptr
->sym_ptr_ptr
= bfd_abs_section
.symbol_ptr_ptr
;
2915 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
2917 /* Translate any ELF section symbol into a BFD section
2919 s
= *(cache_ptr
->sym_ptr_ptr
);
2920 if (s
->flags
& BSF_SECTION_SYM
)
2922 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
2923 s
= *cache_ptr
->sym_ptr_ptr
;
2924 if (s
->name
== 0 || s
->name
[0] == 0)
2928 cache_ptr
->addend
= dst
.r_addend
;
2930 /* Fill in the cache_ptr->howto field from dst.r_type */
2932 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2933 (*ebd
->elf_info_to_howto
) (abfd
, cache_ptr
, &dst
);
2938 asect
->relocation
= reloc_cache
;
2944 elf_debug_section (str
, num
, hdr
)
2947 Elf_Internal_Shdr
*hdr
;
2949 fprintf (stderr
, "\nSection#%d '%s' 0x%.8lx\n", num
, str
, (long) hdr
);
2951 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
2952 (long) hdr
->sh_name
,
2953 (long) hdr
->sh_type
,
2954 (long) hdr
->sh_flags
);
2956 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
2957 (long) hdr
->sh_addr
,
2958 (long) hdr
->sh_offset
,
2959 (long) hdr
->sh_size
);
2961 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
2962 (long) hdr
->sh_link
,
2963 (long) hdr
->sh_info
,
2964 (long) hdr
->sh_addralign
);
2965 fprintf (stderr
, "sh_entsize = %ld\n",
2966 (long) hdr
->sh_entsize
);
2967 fprintf (stderr
, "rawdata = 0x%.8lx\n", (long) hdr
->rawdata
);
2968 fprintf (stderr
, "contents = 0x%.8lx\n", (long) hdr
->contents
);
2969 fprintf (stderr
, "size = %ld\n", (long) hdr
->size
);
2974 elf_debug_file (ehdrp
)
2975 Elf_Internal_Ehdr
*ehdrp
;
2977 fprintf (stderr
, "e_entry = 0x%.8lx\n", (long) ehdrp
->e_entry
);
2978 fprintf (stderr
, "e_phoff = %ld\n", (long) ehdrp
->e_phoff
);
2979 fprintf (stderr
, "e_phnum = %ld\n", (long) ehdrp
->e_phnum
);
2980 fprintf (stderr
, "e_phentsize = %ld\n", (long) ehdrp
->e_phentsize
);
2981 fprintf (stderr
, "e_shoff = %ld\n", (long) ehdrp
->e_shoff
);
2982 fprintf (stderr
, "e_shnum = %ld\n", (long) ehdrp
->e_shnum
);
2983 fprintf (stderr
, "e_shentsize = %ld\n", (long) ehdrp
->e_shentsize
);
2988 elf_slurp_reloc_table (abfd
, asect
, symbols
)
2993 Elf_External_Rel
*native_relocs
;
2994 arelent
*reloc_cache
;
2996 Elf_Internal_Shdr
*data_hdr
;
2998 unsigned long data_max
;
2999 char buf
[4]; /* FIXME -- might be elf64 */
3003 if (asect
->relocation
)
3005 if (asect
->reloc_count
== 0)
3007 if (asect
->flags
& SEC_CONSTRUCTOR
)
3010 if (bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
) != 0)
3012 native_relocs
= (Elf_External_Rel
*)
3013 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rel
));
3016 bfd_set_error (bfd_error_no_memory
);
3019 if (bfd_read ((PTR
) native_relocs
,
3020 sizeof (Elf_External_Rel
), asect
->reloc_count
, abfd
)
3021 != sizeof (Elf_External_Rel
) * asect
->reloc_count
)
3024 reloc_cache
= (arelent
*)
3025 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
3029 bfd_set_error (bfd_error_no_memory
);
3033 /* Get the offset of the start of the segment we are relocating to read in
3034 the implicit addend. */
3035 data_hdr
= &elf_section_data (asect
)->this_hdr
;
3036 data_off
= data_hdr
->sh_offset
;
3037 data_max
= data_hdr
->sh_size
- sizeof (buf
) + 1;
3040 elf_debug_section ("data section", -1, data_hdr
);
3043 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
3045 #ifdef RELOC_PROCESSING
3046 Elf_Internal_Rel dst
;
3047 Elf_External_Rel
*src
;
3049 cache_ptr
= reloc_cache
+ idx
;
3050 src
= native_relocs
+ idx
;
3051 elf_swap_reloc_in (abfd
, src
, &dst
);
3053 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
3055 Elf_Internal_Rel dst
;
3056 Elf_External_Rel
*src
;
3058 cache_ptr
= reloc_cache
+ idx
;
3059 src
= native_relocs
+ idx
;
3061 elf_swap_reloc_in (abfd
, src
, &dst
);
3063 if (asect
->flags
& SEC_RELOC
)
3065 /* relocatable, so the offset is off of the section */
3066 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
3070 /* non-relocatable, so the offset a virtual address */
3071 cache_ptr
->address
= dst
.r_offset
;
3074 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
3075 of zero points to the dummy symbol, which was not read into
3076 the symbol table SYMBOLS. */
3077 if (ELF_R_SYM (dst
.r_info
) == 0)
3078 cache_ptr
->sym_ptr_ptr
= bfd_abs_section
.symbol_ptr_ptr
;
3083 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
3085 /* Translate any ELF section symbol into a BFD section
3087 s
= *(cache_ptr
->sym_ptr_ptr
);
3088 if (s
->flags
& BSF_SECTION_SYM
)
3090 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
3091 s
= *cache_ptr
->sym_ptr_ptr
;
3092 if (s
->name
== 0 || s
->name
[0] == 0)
3096 BFD_ASSERT (dst
.r_offset
<= data_max
);
3097 cache_ptr
->addend
= 0;
3099 /* Fill in the cache_ptr->howto field from dst.r_type */
3101 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
3102 (*ebd
->elf_info_to_howto_rel
) (abfd
, cache_ptr
, &dst
);
3107 asect
->relocation
= reloc_cache
;
3112 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
3118 arelent
*tblptr
= section
->relocation
;
3119 unsigned int count
= 0;
3120 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
3122 /* snarfed from coffcode.h */
3125 if (! elf_slurp_reloca_table (abfd
, section
, symbols
))
3130 if (! elf_slurp_reloc_table (abfd
, section
, symbols
))
3134 tblptr
= section
->relocation
;
3136 for (; count
++ < section
->reloc_count
;)
3137 *relptr
++ = tblptr
++;
3140 return section
->reloc_count
;
3144 elf_get_symtab (abfd
, alocation
)
3146 asymbol
**alocation
;
3148 long symcount
= elf_slurp_symbol_table (abfd
, alocation
, false);
3151 bfd_get_symcount (abfd
) = symcount
;
3156 elf_canonicalize_dynamic_symtab (abfd
, alocation
)
3158 asymbol
**alocation
;
3160 return elf_slurp_symbol_table (abfd
, alocation
, true);
3164 elf_make_empty_symbol (abfd
)
3167 elf_symbol_type
*newsym
;
3169 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
3172 bfd_set_error (bfd_error_no_memory
);
3177 newsym
->symbol
.the_bfd
= abfd
;
3178 return &newsym
->symbol
;
3183 elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
3188 bfd_symbol_info (symbol
, ret
);
3192 elf_print_symbol (ignore_abfd
, filep
, symbol
, how
)
3196 bfd_print_symbol_type how
;
3198 FILE *file
= (FILE *) filep
;
3201 case bfd_print_symbol_name
:
3202 fprintf (file
, "%s", symbol
->name
);
3204 case bfd_print_symbol_more
:
3205 fprintf (file
, "elf ");
3206 fprintf_vma (file
, symbol
->value
);
3207 fprintf (file
, " %lx", (long) symbol
->flags
);
3209 case bfd_print_symbol_all
:
3211 CONST
char *section_name
;
3212 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
3213 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
3214 fprintf (file
, " %s\t%s",
3224 elf_get_lineno (ignore_abfd
, symbol
)
3228 fprintf (stderr
, "elf_get_lineno unimplemented\n");
3235 elf_set_arch_mach (abfd
, arch
, machine
)
3237 enum bfd_architecture arch
;
3238 unsigned long machine
;
3240 /* If this isn't the right architecture for this backend, and this
3241 isn't the generic backend, fail. */
3242 if (arch
!= get_elf_backend_data (abfd
)->arch
3243 && arch
!= bfd_arch_unknown
3244 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
3247 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
3251 elf_find_nearest_line (abfd
,
3262 CONST
char **filename_ptr
;
3263 CONST
char **functionname_ptr
;
3264 unsigned int *line_ptr
;
3270 elf_sizeof_headers (abfd
, reloc
)
3274 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
3281 elf_set_section_contents (abfd
, section
, location
, offset
, count
)
3286 bfd_size_type count
;
3288 Elf_Internal_Shdr
*hdr
;
3290 if (abfd
->output_has_begun
== false) /* set by bfd.c handler? */
3292 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3294 /* Do any elf backend specific processing first. */
3295 if (bed
->elf_backend_begin_write_processing
)
3296 (*bed
->elf_backend_begin_write_processing
) (abfd
);
3298 /* do setup calculations (FIXME) */
3299 if (prep_headers (abfd
) == false)
3301 if (elf_compute_section_file_positions (abfd
) == false)
3303 abfd
->output_has_begun
= true;
3306 hdr
= &elf_section_data (section
)->this_hdr
;
3308 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
3310 if (bfd_write (location
, 1, count
, abfd
) != count
)
3317 elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
3320 Elf_Internal_Rela
*dst
;
3322 fprintf (stderr
, "elf RELA relocation support for target machine unimplemented\n");
3328 elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
3331 Elf_Internal_Rel
*dst
;
3333 fprintf (stderr
, "elf REL relocation support for target machine unimplemented\n");
3339 /* Core file support */
3341 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3342 #include <sys/procfs.h>
3344 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
3345 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
3346 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
3352 bfd_prstatus (abfd
, descdata
, descsz
, filepos
)
3359 prstatus_t
*status
= (prstatus_t
*) 0;
3361 if (descsz
== sizeof (prstatus_t
))
3363 newsect
= bfd_make_section (abfd
, ".reg");
3364 newsect
->_raw_size
= sizeof (status
->pr_reg
);
3365 newsect
->filepos
= filepos
+ (long) &status
->pr_reg
;
3366 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3367 newsect
->alignment_power
= 2;
3368 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3370 memcpy (core_prstatus (abfd
), descdata
, descsz
);
3375 /* Stash a copy of the prpsinfo structure away for future use. */
3378 bfd_prpsinfo (abfd
, descdata
, descsz
, filepos
)
3386 if (descsz
== sizeof (prpsinfo_t
))
3388 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3390 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
3396 bfd_fpregset (abfd
, descdata
, descsz
, filepos
)
3404 newsect
= bfd_make_section (abfd
, ".reg2");
3405 newsect
->_raw_size
= descsz
;
3406 newsect
->filepos
= filepos
;
3407 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3408 newsect
->alignment_power
= 2;
3411 #endif /* HAVE_PROCFS */
3413 /* Return a pointer to the args (including the command name) that were
3414 seen by the program that generated the core dump. Note that for
3415 some reason, a spurious space is tacked onto the end of the args
3416 in some (at least one anyway) implementations, so strip it off if
3420 elf_core_file_failing_command (abfd
)
3424 if (core_prpsinfo (abfd
))
3426 prpsinfo_t
*p
= core_prpsinfo (abfd
);
3427 char *scan
= p
->pr_psargs
;
3432 if ((scan
> p
->pr_psargs
) && (*scan
== ' '))
3436 return p
->pr_psargs
;
3442 /* Return the number of the signal that caused the core dump. Presumably,
3443 since we have a core file, we got a signal of some kind, so don't bother
3444 checking the other process status fields, just return the signal number.
3448 elf_core_file_failing_signal (abfd
)
3452 if (core_prstatus (abfd
))
3454 return ((prstatus_t
*) (core_prstatus (abfd
)))->pr_cursig
;
3460 /* Check to see if the core file could reasonably be expected to have
3461 come for the current executable file. Note that by default we return
3462 true unless we find something that indicates that there might be a
3467 elf_core_file_matches_executable_p (core_bfd
, exec_bfd
)
3476 /* First, xvecs must match since both are ELF files for the same target. */
3478 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
3480 bfd_set_error (bfd_error_system_call
);
3486 /* If no prpsinfo, just return true. Otherwise, grab the last component
3487 of the exec'd pathname from the prpsinfo. */
3489 if (core_prpsinfo (core_bfd
))
3491 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
))->pr_fname
);
3498 /* Find the last component of the executable pathname. */
3500 if ((execname
= strrchr (exec_bfd
->filename
, '/')) != NULL
)
3506 execname
= (char *) exec_bfd
->filename
;
3509 /* See if they match */
3511 return strcmp (execname
, corename
) ? false : true;
3517 #endif /* HAVE_PROCFS */
3520 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3521 the information that would normally be available from the /proc interface
3522 for the process, at the time the process dumped core. Currently this
3523 includes copies of the prstatus, prpsinfo, and fpregset structures.
3525 Since these structures are potentially machine dependent in size and
3526 ordering, bfd provides two levels of support for them. The first level,
3527 available on all machines since it does not require that the host
3528 have /proc support or the relevant include files, is to create a bfd
3529 section for each of the prstatus, prpsinfo, and fpregset structures,
3530 without any interpretation of their contents. With just this support,
3531 the bfd client will have to interpret the structures itself. Even with
3532 /proc support, it might want these full structures for it's own reasons.
3534 In the second level of support, where HAVE_PROCFS is defined, bfd will
3535 pick apart the structures to gather some additional information that
3536 clients may want, such as the general register set, the name of the
3537 exec'ed file and its arguments, the signal (if any) that caused the
3543 elf_corefile_note (abfd
, hdr
)
3545 Elf_Internal_Phdr
*hdr
;
3547 Elf_External_Note
*x_note_p
; /* Elf note, external form */
3548 Elf_Internal_Note i_note
; /* Elf note, internal form */
3549 char *buf
= NULL
; /* Entire note segment contents */
3550 char *namedata
; /* Name portion of the note */
3551 char *descdata
; /* Descriptor portion of the note */
3552 char *sectname
; /* Name to use for new section */
3553 long filepos
; /* File offset to descriptor data */
3556 if (hdr
->p_filesz
> 0
3557 && (buf
= (char *) malloc (hdr
->p_filesz
)) != NULL
3558 && bfd_seek (abfd
, hdr
->p_offset
, SEEK_SET
) != -1
3559 && bfd_read ((PTR
) buf
, hdr
->p_filesz
, 1, abfd
) == hdr
->p_filesz
)
3561 x_note_p
= (Elf_External_Note
*) buf
;
3562 while ((char *) x_note_p
< (buf
+ hdr
->p_filesz
))
3564 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->namesz
);
3565 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->descsz
);
3566 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->type
);
3567 namedata
= x_note_p
->name
;
3568 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
3569 filepos
= hdr
->p_offset
+ (descdata
- buf
);
3570 switch (i_note
.type
)
3573 /* process descdata as prstatus info */
3574 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
3575 sectname
= ".prstatus";
3578 /* process descdata as fpregset info */
3579 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
3580 sectname
= ".fpregset";
3583 /* process descdata as prpsinfo */
3584 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
3585 sectname
= ".prpsinfo";
3588 /* Unknown descriptor, just ignore it. */
3592 if (sectname
!= NULL
)
3594 newsect
= bfd_make_section (abfd
, sectname
);
3595 newsect
->_raw_size
= i_note
.descsz
;
3596 newsect
->filepos
= filepos
;
3597 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3598 newsect
->alignment_power
= 2;
3600 x_note_p
= (Elf_External_Note
*)
3601 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
3608 else if (hdr
->p_filesz
> 0)
3610 bfd_set_error (bfd_error_no_memory
);
3617 /* Core files are simply standard ELF formatted files that partition
3618 the file using the execution view of the file (program header table)
3619 rather than the linking view. In fact, there is no section header
3620 table in a core file.
3622 The process status information (including the contents of the general
3623 register set) and the floating point register set are stored in a
3624 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3625 that allow standard bfd access to the general registers (.reg) and the
3626 floating point registers (.reg2).
3631 elf_core_file_p (abfd
)
3634 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
3635 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3636 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
3637 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
3638 unsigned int phindex
;
3639 struct elf_backend_data
*ebd
;
3641 /* Read in the ELF header in external format. */
3643 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
3645 if (bfd_get_error () != bfd_error_system_call
)
3646 bfd_set_error (bfd_error_wrong_format
);
3650 /* Now check to see if we have a valid ELF file, and one that BFD can
3651 make use of. The magic number must match, the address size ('class')
3652 and byte-swapping must match our XVEC entry, and it must have a
3653 program header table (FIXME: See comments re segments at top of this
3656 if (elf_file_p (&x_ehdr
) == false)
3659 bfd_set_error (bfd_error_wrong_format
);
3663 /* FIXME, Check EI_VERSION here ! */
3667 int desired_address_size
= ELFCLASS32
;
3670 int desired_address_size
= ELFCLASS64
;
3673 if (x_ehdr
.e_ident
[EI_CLASS
] != desired_address_size
)
3677 /* Switch xvec to match the specified byte order. */
3678 switch (x_ehdr
.e_ident
[EI_DATA
])
3680 case ELFDATA2MSB
: /* Big-endian */
3681 if (abfd
->xvec
->byteorder_big_p
== false)
3684 case ELFDATA2LSB
: /* Little-endian */
3685 if (abfd
->xvec
->byteorder_big_p
== true)
3688 case ELFDATANONE
: /* No data encoding specified */
3689 default: /* Unknown data encoding specified */
3693 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3694 the tdata pointer in the bfd. */
3697 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
3698 if (elf_tdata (abfd
) == NULL
)
3700 bfd_set_error (bfd_error_no_memory
);
3704 /* FIXME, `wrong' returns from this point onward, leak memory. */
3706 /* Now that we know the byte order, swap in the rest of the header */
3707 i_ehdrp
= elf_elfheader (abfd
);
3708 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
3710 elf_debug_file (i_ehdrp
);
3713 ebd
= get_elf_backend_data (abfd
);
3715 /* Check that the ELF e_machine field matches what this particular
3716 BFD format expects. */
3717 if (ebd
->elf_machine_code
!= i_ehdrp
->e_machine
)
3719 bfd_target
**target_ptr
;
3721 if (ebd
->elf_machine_code
!= EM_NONE
)
3724 /* This is the generic ELF target. Let it match any ELF target
3725 for which we do not have a specific backend. */
3726 for (target_ptr
= bfd_target_vector
; *target_ptr
!= NULL
; target_ptr
++)
3728 struct elf_backend_data
*back
;
3730 if ((*target_ptr
)->flavour
!= bfd_target_elf_flavour
)
3732 back
= (struct elf_backend_data
*) (*target_ptr
)->backend_data
;
3733 if (back
->elf_machine_code
== i_ehdrp
->e_machine
)
3735 /* target_ptr is an ELF backend which matches this
3736 object file, so reject the generic ELF target. */
3742 /* If there is no program header, or the type is not a core file, then
3744 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
3747 /* Allocate space for a copy of the program header table in
3748 internal form, seek to the program header table in the file,
3749 read it in, and convert it to internal form. As a simple sanity
3750 check, verify that the what BFD thinks is the size of each program
3751 header table entry actually matches the size recorded in the file. */
3753 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
3755 i_phdrp
= (Elf_Internal_Phdr
*)
3756 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
3759 bfd_set_error (bfd_error_no_memory
);
3762 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
3764 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3766 if (bfd_read ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
3769 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
3772 /* Once all of the program headers have been read and converted, we
3773 can start processing them. */
3775 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3777 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
3778 if ((i_phdrp
+ phindex
)->p_type
== PT_NOTE
)
3780 elf_corefile_note (abfd
, i_phdrp
+ phindex
);
3784 /* Remember the entry point specified in the ELF file header. */
3786 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;