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 NAME(bfd_elf,core_file_matches_executable_p)
79 #define elf_object_p NAME(bfd_elf,object_p)
80 #define elf_core_file_p NAME(bfd_elf,core_file_p)
81 #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
82 #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
83 #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
84 #define elf_get_symtab NAME(bfd_elf,get_symtab)
85 #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol)
86 #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info)
87 #define elf_print_symbol NAME(bfd_elf,print_symbol)
88 #define elf_get_lineno NAME(bfd_elf,get_lineno)
89 #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach)
90 #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line)
91 #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers)
92 #define elf_set_section_contents NAME(bfd_elf,set_section_contents)
93 #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto)
94 #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel)
95 #define elf_new_section_hook NAME(bfd_elf,new_section_hook)
96 #define write_relocs NAME(bfd_elf,_write_relocs)
97 #define elf_find_section NAME(bfd_elf,find_section)
100 #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
101 #define ELF_R_SYM(X) ELF64_R_SYM(X)
102 #define ELFCLASS ELFCLASS64
106 #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
107 #define ELF_R_SYM(X) ELF32_R_SYM(X)
108 #define ELFCLASS ELFCLASS32
112 static int shstrtab_length_fixed
;
114 struct elf_sect_data
{
119 /* Forward declarations of static functions */
121 static struct sec
* section_from_elf_index
PARAMS ((bfd
*, unsigned int));
123 static int elf_section_from_bfd_section
PARAMS ((bfd
*, struct sec
*));
125 static boolean elf_slurp_symbol_table
PARAMS ((bfd
*, asymbol
**));
127 static int elf_symbol_from_bfd_symbol
PARAMS ((bfd
*,
128 struct symbol_cache_entry
**));
130 static boolean elf_map_symbols
PARAMS ((bfd
*));
131 static boolean swap_out_syms
PARAMS ((bfd
*));
134 static void elf_debug_section
PARAMS ((char *, int, Elf_Internal_Shdr
*));
135 static void elf_debug_file
PARAMS ((Elf_Internal_Ehdr
*));
138 #define elf_string_from_elf_strtab(abfd,strindex) \
139 elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
142 /* Structure swapping routines */
144 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
145 can be handled by explicitly specifying 32 bits or "the long type". */
147 #define put_word bfd_h_put_64
148 #define get_word bfd_h_get_64
151 #define put_word bfd_h_put_32
152 #define get_word bfd_h_get_32
155 /* Translate an ELF symbol in external format into an ELF symbol in internal
159 DEFUN (elf_swap_symbol_in
, (abfd
, src
, dst
),
161 Elf_External_Sym
* src AND
162 Elf_Internal_Sym
* dst
)
164 dst
->st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->st_name
);
165 dst
->st_value
= get_word (abfd
, (bfd_byte
*) src
->st_value
);
166 dst
->st_size
= get_word (abfd
, (bfd_byte
*) src
->st_size
);
167 dst
->st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_info
);
168 dst
->st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_other
);
169 dst
->st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->st_shndx
);
172 /* Translate an ELF symbol in internal format into an ELF symbol in external
176 DEFUN (elf_swap_symbol_out
, (abfd
, src
, dst
),
178 Elf_Internal_Sym
* src AND
179 Elf_External_Sym
* dst
)
181 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
182 put_word (abfd
, src
->st_value
, dst
->st_value
);
183 put_word (abfd
, src
->st_size
, dst
->st_size
);
184 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
185 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
186 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
190 /* Translate an ELF file header in external format into an ELF file header in
194 DEFUN (elf_swap_ehdr_in
, (abfd
, src
, dst
),
196 Elf_External_Ehdr
* src AND
197 Elf_Internal_Ehdr
* dst
)
199 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
200 dst
->e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_type
);
201 dst
->e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_machine
);
202 dst
->e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_version
);
203 dst
->e_entry
= get_word (abfd
, (bfd_byte
*) src
->e_entry
);
204 dst
->e_phoff
= get_word (abfd
, (bfd_byte
*) src
->e_phoff
);
205 dst
->e_shoff
= get_word (abfd
, (bfd_byte
*) src
->e_shoff
);
206 dst
->e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_flags
);
207 dst
->e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_ehsize
);
208 dst
->e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phentsize
);
209 dst
->e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phnum
);
210 dst
->e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shentsize
);
211 dst
->e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shnum
);
212 dst
->e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shstrndx
);
215 /* Translate an ELF file header in internal format into an ELF file header in
219 DEFUN (elf_swap_ehdr_out
, (abfd
, src
, dst
),
221 Elf_Internal_Ehdr
* src AND
222 Elf_External_Ehdr
* dst
)
224 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
225 /* note that all elements of dst are *arrays of unsigned char* already... */
226 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
227 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
228 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
229 put_word (abfd
, src
->e_entry
, dst
->e_entry
);
230 put_word (abfd
, src
->e_phoff
, dst
->e_phoff
);
231 put_word (abfd
, src
->e_shoff
, dst
->e_shoff
);
232 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
233 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
234 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
235 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
236 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
237 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
238 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
242 /* Translate an ELF section header table entry in external format into an
243 ELF section header table entry in internal format. */
246 DEFUN (elf_swap_shdr_in
, (abfd
, src
, dst
),
248 Elf_External_Shdr
* src AND
249 Elf_Internal_Shdr
* dst
)
251 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
252 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
253 dst
->sh_flags
= get_word (abfd
, (bfd_byte
*) src
->sh_flags
);
254 dst
->sh_addr
= get_word (abfd
, (bfd_byte
*) src
->sh_addr
);
255 dst
->sh_offset
= get_word (abfd
, (bfd_byte
*) src
->sh_offset
);
256 dst
->sh_size
= get_word (abfd
, (bfd_byte
*) src
->sh_size
);
257 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
258 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
259 dst
->sh_addralign
= get_word (abfd
, (bfd_byte
*) src
->sh_addralign
);
260 dst
->sh_entsize
= get_word (abfd
, (bfd_byte
*) src
->sh_entsize
);
261 /* we haven't done any processing on it yet, so... */
262 dst
->rawdata
= (void *) 0;
265 /* Translate an ELF section header table entry in internal format into an
266 ELF section header table entry in external format. */
269 DEFUN (elf_swap_shdr_out
, (abfd
, src
, dst
),
271 Elf_Internal_Shdr
* src AND
272 Elf_External_Shdr
* dst
)
274 /* note that all elements of dst are *arrays of unsigned char* already... */
275 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
276 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
277 put_word (abfd
, src
->sh_flags
, dst
->sh_flags
);
278 put_word (abfd
, src
->sh_addr
, dst
->sh_addr
);
279 put_word (abfd
, src
->sh_offset
, dst
->sh_offset
);
280 put_word (abfd
, src
->sh_size
, dst
->sh_size
);
281 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
282 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
283 put_word (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
284 put_word (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
288 /* Translate an ELF program header table entry in external format into an
289 ELF program header table entry in internal format. */
292 DEFUN (elf_swap_phdr_in
, (abfd
, src
, dst
),
294 Elf_External_Phdr
* src AND
295 Elf_Internal_Phdr
* dst
)
297 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
298 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
299 dst
->p_offset
= get_word (abfd
, (bfd_byte
*) src
->p_offset
);
300 dst
->p_vaddr
= get_word (abfd
, (bfd_byte
*) src
->p_vaddr
);
301 dst
->p_paddr
= get_word (abfd
, (bfd_byte
*) src
->p_paddr
);
302 dst
->p_filesz
= get_word (abfd
, (bfd_byte
*) src
->p_filesz
);
303 dst
->p_memsz
= get_word (abfd
, (bfd_byte
*) src
->p_memsz
);
304 dst
->p_align
= get_word (abfd
, (bfd_byte
*) src
->p_align
);
308 DEFUN (elf_swap_phdr_out
, (abfd
, src
, dst
),
310 Elf_Internal_Phdr
* src AND
311 Elf_External_Phdr
* dst
)
313 /* note that all elements of dst are *arrays of unsigned char* already... */
314 bfd_h_put_32 (abfd
, src
->p_type
, dst
->p_type
);
315 put_word (abfd
, src
->p_offset
, dst
->p_offset
);
316 put_word (abfd
, src
->p_vaddr
, dst
->p_vaddr
);
317 put_word (abfd
, src
->p_paddr
, dst
->p_paddr
);
318 put_word (abfd
, src
->p_filesz
, dst
->p_filesz
);
319 put_word (abfd
, src
->p_memsz
, dst
->p_memsz
);
320 bfd_h_put_32 (abfd
, src
->p_flags
, dst
->p_flags
);
321 put_word (abfd
, src
->p_align
, dst
->p_align
);
324 /* Translate an ELF reloc from external format to internal format. */
326 DEFUN (elf_swap_reloc_in
, (abfd
, src
, dst
),
328 Elf_External_Rel
* src AND
329 Elf_Internal_Rel
* dst
)
331 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
332 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
336 DEFUN (elf_swap_reloca_in
, (abfd
, src
, dst
),
338 Elf_External_Rela
* src AND
339 Elf_Internal_Rela
* dst
)
341 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
342 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
343 dst
->r_addend
= get_word (abfd
, (bfd_byte
*) src
->r_addend
);
346 /* Translate an ELF reloc from internal format to external format. */
348 DEFUN (elf_swap_reloc_out
, (abfd
, src
, dst
),
350 Elf_Internal_Rel
* src AND
351 Elf_External_Rel
* dst
)
353 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
354 put_word (abfd
, src
->r_info
, dst
->r_info
);
358 DEFUN (elf_swap_reloca_out
, (abfd
, src
, dst
),
360 Elf_Internal_Rela
* src AND
361 Elf_External_Rela
* dst
)
363 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
364 put_word (abfd
, src
->r_info
, dst
->r_info
);
365 put_word (abfd
, src
->r_addend
, dst
->r_addend
);
370 /* String table creation/manipulation routines */
372 static struct strtab
*
373 DEFUN (bfd_new_strtab
, (abfd
),
378 ss
= (struct strtab
*) malloc (sizeof (struct strtab
));
381 bfd_set_error (bfd_error_no_memory
);
384 ss
->tab
= malloc (1);
387 bfd_set_error (bfd_error_no_memory
);
398 DEFUN (bfd_add_to_strtab
, (abfd
, ss
, str
),
400 struct strtab
*ss AND
403 /* should search first, but for now: */
404 /* include the trailing NUL */
405 int ln
= strlen (str
) + 1;
407 /* should this be using obstacks? */
408 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
410 BFD_ASSERT (ss
->tab
!= 0); /* FIXME */
411 strcpy (ss
->tab
+ ss
->length
, str
);
415 return ss
->length
- ln
;
419 DEFUN (bfd_add_2_to_strtab
, (abfd
, ss
, str
, str2
),
421 struct strtab
*ss AND
425 /* should search first, but for now: */
426 /* include the trailing NUL */
427 int ln
= strlen (str
) + strlen (str2
) + 1;
429 /* should this be using obstacks? */
431 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
433 ss
->tab
= malloc (ln
);
435 BFD_ASSERT (ss
->tab
!= 0); /* FIXME */
436 strcpy (ss
->tab
+ ss
->length
, str
);
437 strcpy (ss
->tab
+ ss
->length
+ strlen (str
), str2
);
441 return ss
->length
- ln
;
445 /* ELF .o/exec file reading */
447 /* Create a new bfd section from an ELF section header. */
450 DEFUN (bfd_section_from_shdr
, (abfd
, shindex
),
452 unsigned int shindex
)
454 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
455 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
459 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
461 switch (hdr
->sh_type
)
465 /* inactive section. Throw it away. */
470 /* Bits that get saved. This one is real. */
473 newsect
= bfd_make_section (abfd
, name
);
476 newsect
->filepos
= hdr
->sh_offset
; /* so we can read back the bits */
477 newsect
->flags
|= SEC_HAS_CONTENTS
;
478 newsect
->vma
= hdr
->sh_addr
;
479 newsect
->_raw_size
= hdr
->sh_size
;
480 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
482 if (hdr
->sh_flags
& SHF_ALLOC
)
484 newsect
->flags
|= SEC_ALLOC
;
485 newsect
->flags
|= SEC_LOAD
;
488 if (!(hdr
->sh_flags
& SHF_WRITE
))
489 newsect
->flags
|= SEC_READONLY
;
491 if (hdr
->sh_flags
& SHF_EXECINSTR
)
492 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
493 else if (newsect
->flags
& SEC_ALLOC
)
494 newsect
->flags
|= SEC_DATA
;
496 /* The debugging sections appear to recognized only by
498 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
499 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
500 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
501 newsect
->flags
|= SEC_DEBUGGING
;
503 hdr
->rawdata
= (void *) newsect
;
506 hdr
->rawdata
= (void *) bfd_get_section_by_name (abfd
, name
);
511 /* Bits that get saved. This one is real. */
514 newsect
= bfd_make_section (abfd
, name
);
517 newsect
->vma
= hdr
->sh_addr
;
518 newsect
->_raw_size
= hdr
->sh_size
;
519 newsect
->filepos
= hdr
->sh_offset
; /* fake */
520 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
521 if (hdr
->sh_flags
& SHF_ALLOC
)
522 newsect
->flags
|= SEC_ALLOC
;
524 if (!(hdr
->sh_flags
& SHF_WRITE
))
525 newsect
->flags
|= SEC_READONLY
;
527 /* FIXME: This section is empty. Does it really make
528 sense to set SEC_CODE for it? */
529 if (hdr
->sh_flags
& SHF_EXECINSTR
)
530 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
532 hdr
->rawdata
= (void *) newsect
;
537 case SHT_SYMTAB
: /* A symbol table */
538 if (elf_onesymtab (abfd
) == shindex
)
541 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
542 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
543 elf_onesymtab (abfd
) = shindex
;
544 elf_tdata(abfd
)->symtab_hdr
= *hdr
;
545 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->symtab_hdr
;
546 abfd
->flags
|= HAS_SYMS
;
549 case SHT_STRTAB
: /* A string table */
552 if (ehdr
->e_shstrndx
== shindex
)
554 elf_tdata(abfd
)->shstrtab_hdr
= *hdr
;
555 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->shstrtab_hdr
;
556 hdr
->rawdata
= (PTR
) &elf_tdata(abfd
)->shstrtab_hdr
;
562 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
564 Elf_Internal_Shdr
*hdr2
= elf_elfsections(abfd
)[i
];
565 if (hdr2
->sh_link
== shindex
)
567 bfd_section_from_shdr (abfd
, i
);
568 if (elf_onesymtab (abfd
) == i
)
570 elf_tdata(abfd
)->strtab_hdr
= *hdr
;
571 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->strtab_hdr
;
574 #if 0 /* Not handling other string tables specially right now. */
575 hdr2
= elf_elfsections(abfd
)[i
]; /* in case it moved */
576 /* We have a strtab for some random other section. */
577 newsect
= (asection
*) hdr2
->rawdata
;
580 hdr
->rawdata
= (PTR
) newsect
;
581 hdr2
= &elf_section_data (newsect
)->str_hdr
;
583 elf_elfsections(abfd
)[shindex
] = hdr2
;
589 newsect
= bfd_make_section (abfd
, name
);
592 newsect
->flags
= SEC_HAS_CONTENTS
;
593 hdr
->rawdata
= (PTR
) newsect
;
594 newsect
->_raw_size
= hdr
->sh_size
;
595 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
596 newsect
->vma
= hdr
->sh_addr
;
597 newsect
->filepos
= hdr
->sh_offset
;
599 if (hdr
->sh_flags
& SHF_ALLOC
)
600 newsect
->flags
|= SEC_ALLOC
|SEC_LOAD
;
601 if (!(hdr
->sh_flags
& SHF_WRITE
))
602 newsect
->flags
|= SEC_READONLY
;
603 if (hdr
->sh_flags
& SHF_EXECINSTR
)
604 newsect
->flags
|= SEC_CODE
;
605 else if (newsect
->flags
& SEC_ALLOC
)
606 newsect
->flags
|= SEC_DATA
;
608 /* Check for debugging string tables. */
609 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
610 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
611 newsect
->flags
|= SEC_DEBUGGING
;
618 /* *These* do a lot of work -- but build no sections!
619 The spec says there can be multiple strtabs, but only one symtab,
620 but there can be lots of REL* sections. */
621 /* FIXME: The above statement is wrong! There are typically at least
622 two symbol tables in a dynamically linked executable, ".dynsym"
623 which is the dynamic linkage symbol table and ".symtab", which is
624 the "traditional" symbol table. -fnf */
627 asection
*target_sect
;
628 Elf_Internal_Shdr
*hdr2
;
629 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
631 /* Don't allow REL relocations on a machine that uses RELA and
633 /* @@ Actually, the generic ABI does suggest that both might be
634 used in one file. But the four ABI Processor Supplements I
635 have access to right now all specify that only one is used on
636 each of those architectures. It's conceivable that, e.g., a
637 bunch of absolute 32-bit relocs might be more compact in REL
638 form even on a RELA machine... */
639 BFD_ASSERT (!(use_rela_p
&& (hdr
->sh_type
== SHT_REL
)));
640 BFD_ASSERT (!(!use_rela_p
&& (hdr
->sh_type
== SHT_RELA
)));
641 BFD_ASSERT (hdr
->sh_entsize
==
643 ? sizeof (Elf_External_Rela
)
644 : sizeof (Elf_External_Rel
)));
646 bfd_section_from_shdr (abfd
, hdr
->sh_info
); /* target */
647 bfd_section_from_shdr (abfd
, hdr
->sh_link
); /* symbol table */
648 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
649 if (target_sect
== NULL
650 || elf_section_data (target_sect
) == NULL
)
653 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
655 elf_elfsections(abfd
)[shindex
] = hdr2
;
656 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
657 target_sect
->flags
|= SEC_RELOC
;
658 target_sect
->relocation
= 0;
659 target_sect
->rel_filepos
= hdr
->sh_offset
;
660 abfd
->flags
|= HAS_RELOC
;
666 case SHT_DYNSYM
: /* could treat this like symtab... */
668 fprintf (stderr
, "Dynamic Linking sections not yet supported.\n");
675 fprintf (stderr
, "Note Sections not yet supported.\n");
682 fprintf (stderr
, "SHLIB Sections not supported (and non conforming.)\n");
687 /* Check for any processor-specific section types. */
689 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
691 if (bed
->elf_backend_section_from_shdr
)
692 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
701 DEFUN (elf_new_section_hook
, (abfd
, sec
),
705 struct bfd_elf_section_data
*sdata
;
707 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
710 bfd_set_error (bfd_error_no_memory
);
713 sec
->used_by_bfd
= (PTR
) sdata
;
714 memset (sdata
, 0, sizeof (*sdata
));
718 /* Create a new bfd section from an ELF program header.
720 Since program segments have no names, we generate a synthetic name
721 of the form segment<NUM>, where NUM is generally the index in the
722 program header table. For segments that are split (see below) we
723 generate the names segment<NUM>a and segment<NUM>b.
725 Note that some program segments may have a file size that is different than
726 (less than) the memory size. All this means is that at execution the
727 system must allocate the amount of memory specified by the memory size,
728 but only initialize it with the first "file size" bytes read from the
729 file. This would occur for example, with program segments consisting
730 of combined data+bss.
732 To handle the above situation, this routine generates TWO bfd sections
733 for the single program segment. The first has the length specified by
734 the file size of the segment, and the second has the length specified
735 by the difference between the two sizes. In effect, the segment is split
736 into it's initialized and uninitialized parts.
741 DEFUN (bfd_section_from_phdr
, (abfd
, hdr
, index
),
743 Elf_Internal_Phdr
* hdr AND
751 split
= ((hdr
->p_memsz
> 0) &&
752 (hdr
->p_filesz
> 0) &&
753 (hdr
->p_memsz
> hdr
->p_filesz
));
754 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
755 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
758 bfd_set_error (bfd_error_no_memory
);
761 strcpy (name
, namebuf
);
762 newsect
= bfd_make_section (abfd
, name
);
763 newsect
->vma
= hdr
->p_vaddr
;
764 newsect
->_raw_size
= hdr
->p_filesz
;
765 newsect
->filepos
= hdr
->p_offset
;
766 newsect
->flags
|= SEC_HAS_CONTENTS
;
767 if (hdr
->p_type
== PT_LOAD
)
769 newsect
->flags
|= SEC_ALLOC
;
770 newsect
->flags
|= SEC_LOAD
;
771 if (hdr
->p_flags
& PF_X
)
773 /* FIXME: all we known is that it has execute PERMISSION,
775 newsect
->flags
|= SEC_CODE
;
778 if (!(hdr
->p_flags
& PF_W
))
780 newsect
->flags
|= SEC_READONLY
;
785 sprintf (namebuf
, "segment%db", index
);
786 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
789 bfd_set_error (bfd_error_no_memory
);
792 strcpy (name
, namebuf
);
793 newsect
= bfd_make_section (abfd
, name
);
794 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
795 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
796 if (hdr
->p_type
== PT_LOAD
)
798 newsect
->flags
|= SEC_ALLOC
;
799 if (hdr
->p_flags
& PF_X
)
800 newsect
->flags
|= SEC_CODE
;
802 if (!(hdr
->p_flags
& PF_W
))
803 newsect
->flags
|= SEC_READONLY
;
809 /* Begin processing a given object.
811 First we validate the file by reading in the ELF header and checking
814 static INLINE boolean
815 DEFUN (elf_file_p
, (x_ehdrp
), Elf_External_Ehdr
* x_ehdrp
)
817 return ((x_ehdrp
->e_ident
[EI_MAG0
] == ELFMAG0
)
818 && (x_ehdrp
->e_ident
[EI_MAG1
] == ELFMAG1
)
819 && (x_ehdrp
->e_ident
[EI_MAG2
] == ELFMAG2
)
820 && (x_ehdrp
->e_ident
[EI_MAG3
] == ELFMAG3
));
823 /* Check to see if the file associated with ABFD matches the target vector
826 Note that we may be called several times with the same ABFD, but different
827 target vectors, most of which will not match. We have to avoid leaving
828 any side effects in ABFD, or any data it points to (like tdata), if the
829 file does not match the target vector.
831 FIXME: There is memory leak if we are called more than once with the same
832 ABFD, and that bfd already has tdata allocated, since we allocate more tdata
833 and the old tdata is orphaned. Since it's in the bfd obstack, there isn't
834 much we can do about this except possibly rewrite the code. There are
835 also other bfd_allocs that may be the source of memory leaks as well. */
838 DEFUN (elf_object_p
, (abfd
), bfd
* abfd
)
840 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
841 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
842 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
843 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
844 unsigned int shindex
;
845 char *shstrtab
; /* Internal copy of section header stringtab */
846 struct elf_backend_data
*ebd
;
847 struct elf_obj_tdata
*preserved_tdata
= elf_tdata (abfd
);
849 /* Read in the ELF header in external format. */
851 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
852 goto got_system_call
;
854 /* Now check to see if we have a valid ELF file, and one that BFD can
855 make use of. The magic number must match, the address size ('class')
856 and byte-swapping must match our XVEC entry, and it must have a
857 section header table (FIXME: See comments re sections at top of this
860 if ((elf_file_p (&x_ehdr
) == false) ||
861 (x_ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) ||
862 (x_ehdr
.e_ident
[EI_CLASS
] != ELFCLASS
))
863 goto got_wrong_format_error
;
865 /* Check that file's byte order matches xvec's */
866 switch (x_ehdr
.e_ident
[EI_DATA
])
868 case ELFDATA2MSB
: /* Big-endian */
869 if (!abfd
->xvec
->header_byteorder_big_p
)
870 goto got_wrong_format_error
;
872 case ELFDATA2LSB
: /* Little-endian */
873 if (abfd
->xvec
->header_byteorder_big_p
)
874 goto got_wrong_format_error
;
876 case ELFDATANONE
: /* No data encoding specified */
877 default: /* Unknown data encoding specified */
878 goto got_wrong_format_error
;
881 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
882 the tdata pointer in the bfd. FIXME: memory leak, see above. */
885 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
886 if (elf_tdata (abfd
) == NULL
)
887 goto got_no_memory_error
;
889 /* Now that we know the byte order, swap in the rest of the header */
890 i_ehdrp
= elf_elfheader (abfd
);
891 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
893 elf_debug_file (i_ehdrp
);
896 /* If there is no section header table, we're hosed. */
897 if (i_ehdrp
->e_shoff
== 0)
898 goto got_wrong_format_error
;
900 /* As a simple sanity check, verify that the what BFD thinks is the
901 size of each section header table entry actually matches the size
902 recorded in the file. */
903 if (i_ehdrp
->e_shentsize
!= sizeof (x_shdr
))
904 goto got_wrong_format_error
;
906 ebd
= get_elf_backend_data (abfd
);
908 /* Check that the ELF e_machine field matches what this particular
909 BFD format expects. */
910 if (ebd
->elf_machine_code
!= i_ehdrp
->e_machine
)
912 bfd_target
**target_ptr
;
914 if (ebd
->elf_machine_code
!= EM_NONE
)
915 goto got_wrong_format_error
;
917 /* This is the generic ELF target. Let it match any ELF target
918 for which we do not have a specific backend. */
919 for (target_ptr
= bfd_target_vector
; *target_ptr
!= NULL
; target_ptr
++)
921 struct elf_backend_data
*back
;
923 if ((*target_ptr
)->flavour
!= bfd_target_elf_flavour
)
925 back
= (struct elf_backend_data
*) (*target_ptr
)->backend_data
;
926 if (back
->elf_machine_code
== i_ehdrp
->e_machine
)
928 /* target_ptr is an ELF backend which matches this
929 object file, so reject the generic ELF target. */
930 goto got_wrong_format_error
;
936 /* Set the flags and architecture before calling the backend so that
937 it can override them. */
938 if (i_ehdrp
->e_type
== ET_EXEC
)
939 abfd
->flags
|= EXEC_P
;
940 else if (i_ehdrp
->e_type
== ET_DYN
)
941 abfd
->flags
|= DYNAMIC
;
943 bfd_default_set_arch_mach (abfd
, ebd
->arch
, 0);
945 /* Remember the entry point specified in the ELF file header. */
946 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
948 /* Let the backend double check the format and override global
950 if (ebd
->elf_backend_object_p
)
952 if ((*ebd
->elf_backend_object_p
) (abfd
) == false)
953 goto got_wrong_format_error
;
956 /* Allocate space for a copy of the section header table in
957 internal form, seek to the section header table in the file,
958 read it in, and convert it to internal form. */
959 i_shdrp
= (Elf_Internal_Shdr
*)
960 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
);
961 elf_elfsections (abfd
) =
962 (Elf_Internal_Shdr
**) bfd_alloc (abfd
, sizeof (i_shdrp
) * i_ehdrp
->e_shnum
);
963 if (!i_shdrp
|| !elf_elfsections(abfd
))
964 goto got_no_memory_error
;
965 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) == -1)
966 goto got_system_call
;
967 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
969 if (bfd_read ((PTR
) & x_shdr
, sizeof x_shdr
, 1, abfd
) != sizeof (x_shdr
))
970 goto got_system_call
;
971 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
972 elf_elfsections(abfd
)[shindex
] = i_shdrp
+ shindex
;
974 /* If this is a .dynamic section, mark the object file as being
975 dynamically linked. */
976 if (i_shdrp
[shindex
].sh_type
== SHT_DYNAMIC
)
977 abfd
->flags
|= DYNAMIC
;
979 if (i_ehdrp
->e_shstrndx
)
981 bfd_section_from_shdr (abfd
, i_ehdrp
->e_shstrndx
);
984 /* Read in the string table containing the names of the sections. We
985 will need the base pointer to this table later. */
986 /* We read this inline now, so that we don't have to go through
987 bfd_section_from_shdr with it (since this particular strtab is
988 used to find all of the ELF section names.) */
990 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
992 goto got_wrong_format_error
;
994 /* Once all of the section headers have been read and converted, we
995 can start processing them. Note that the first section header is
996 a dummy placeholder entry, so we ignore it.
998 We also watch for the symbol table section and remember the file
999 offset and section size for both the symbol table section and the
1000 associated string table section. */
1002 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1004 bfd_section_from_shdr (abfd
, shindex
);
1007 return (abfd
->xvec
);
1009 /* If we are going to use goto's to avoid duplicating error setting
1010 and return(NULL) code, then this at least makes it more maintainable. */
1013 bfd_set_error (bfd_error_system_call
);
1015 got_wrong_format_error
:
1016 bfd_set_error (bfd_error_wrong_format
);
1018 got_no_memory_error
:
1019 bfd_set_error (bfd_error_no_memory
);
1022 elf_tdata (abfd
) = preserved_tdata
;
1027 /* ELF .o/exec file writing */
1029 /* Takes a bfd and a symbol, returns a pointer to the elf specific area
1030 of the symbol if there is one. */
1031 static INLINE elf_symbol_type
*
1032 DEFUN (elf_symbol_from
, (ignore_abfd
, symbol
),
1033 bfd
* ignore_abfd AND
1036 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
1039 if (symbol
->the_bfd
->tdata
.elf_obj_data
== (struct elf_obj_tdata
*) NULL
)
1042 return (elf_symbol_type
*) symbol
;
1045 /* Create ELF output from BFD sections.
1047 Essentially, just create the section header and forget about the program
1051 DEFUN (elf_make_sections
, (abfd
, asect
, obj
),
1053 asection
* asect AND
1056 /* most of what is in bfd_shdr_from_section goes in here... */
1057 /* and all of these sections generate at *least* one ELF section. */
1058 Elf_Internal_Shdr
*this_hdr
;
1059 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1061 this_hdr
->sh_addr
= asect
->vma
;
1062 this_hdr
->sh_size
= asect
->_raw_size
;
1063 /* contents already set by elf_set_section_contents */
1065 if (asect
->flags
& SEC_RELOC
)
1067 /* emit a reloc section, and thus strtab and symtab... */
1068 Elf_Internal_Shdr
*rela_hdr
;
1069 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1071 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1073 /* orelocation has the data, reloc_count has the count... */
1076 rela_hdr
->sh_type
= SHT_RELA
;
1077 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1080 /* REL relocations */
1082 rela_hdr
->sh_type
= SHT_REL
;
1083 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rel
);
1085 rela_hdr
->sh_flags
= 0;
1086 rela_hdr
->sh_addr
= 0;
1087 rela_hdr
->sh_offset
= 0;
1089 /* FIXME: Systems I've checked use an alignment of 4, but it is
1090 possible that some systems use a different alignment. */
1091 rela_hdr
->sh_addralign
= 4;
1095 if (asect
->flags
& SEC_ALLOC
)
1097 this_hdr
->sh_flags
|= SHF_ALLOC
;
1098 if (asect
->flags
& SEC_LOAD
)
1100 /* @@ Do something with sh_type? */
1105 /* If this section is not part of the program image during
1106 execution, leave the address fields at 0. */
1107 this_hdr
->sh_addr
= 0;
1110 if (!(asect
->flags
& SEC_READONLY
))
1111 this_hdr
->sh_flags
|= SHF_WRITE
;
1113 if (asect
->flags
& SEC_CODE
)
1114 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1118 write_relocs (abfd
, sec
, xxx
)
1123 Elf_Internal_Shdr
*rela_hdr
;
1124 Elf_External_Rela
*outbound_relocas
;
1125 Elf_External_Rel
*outbound_relocs
;
1127 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1128 asymbol
*last_sym
= 0;
1129 int last_sym_idx
= 9999999; /* should always be written before use */
1131 if ((sec
->flags
& SEC_RELOC
) == 0)
1133 /* Flags are sometimes inconsistent. */
1134 if (sec
->reloc_count
== 0)
1137 rela_hdr
= &elf_section_data (sec
)->rel_hdr
;
1139 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* sec
->reloc_count
;
1140 rela_hdr
->contents
= (void *) bfd_alloc (abfd
, rela_hdr
->sh_size
);
1141 if (!rela_hdr
->contents
)
1143 bfd_set_error (bfd_error_no_memory
);
1144 abort(); /* FIXME */
1147 /* orelocation has the data, reloc_count has the count... */
1150 outbound_relocas
= (Elf_External_Rela
*) rela_hdr
->contents
;
1152 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1154 Elf_Internal_Rela dst_rela
;
1155 Elf_External_Rela
*src_rela
;
1160 ptr
= sec
->orelocation
[idx
];
1161 src_rela
= outbound_relocas
+ idx
;
1162 if (!(abfd
->flags
& EXEC_P
))
1163 dst_rela
.r_offset
= ptr
->address
- sec
->vma
;
1165 dst_rela
.r_offset
= ptr
->address
;
1167 sym
= *ptr
->sym_ptr_ptr
;
1168 if (sym
== last_sym
)
1173 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1175 dst_rela
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1177 dst_rela
.r_addend
= ptr
->addend
;
1178 elf_swap_reloca_out (abfd
, &dst_rela
, src_rela
);
1182 /* REL relocations */
1184 outbound_relocs
= (Elf_External_Rel
*) rela_hdr
->contents
;
1186 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1188 Elf_Internal_Rel dst_rel
;
1189 Elf_External_Rel
*src_rel
;
1194 ptr
= sec
->orelocation
[idx
];
1195 sym
= *ptr
->sym_ptr_ptr
;
1196 src_rel
= outbound_relocs
+ idx
;
1197 if (!(abfd
->flags
& EXEC_P
))
1198 dst_rel
.r_offset
= ptr
->address
- sec
->vma
;
1200 dst_rel
.r_offset
= ptr
->address
;
1202 if (sym
== last_sym
)
1207 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1209 dst_rel
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1211 elf_swap_reloc_out (abfd
, &dst_rel
, src_rel
);
1217 fix_up_strtabs (abfd
, asect
, obj
)
1222 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (asect
)->this_hdr
;
1223 int this_idx
= elf_section_data(asect
)->this_idx
;
1225 /* @@ Check flags! */
1226 if (!strncmp (asect
->name
, ".stab", 5)
1227 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
1229 size_t len
= strlen (asect
->name
) + 1;
1230 char *s
= (char *) malloc (len
);
1232 /* FIXME: Should deal more gracefully with errors. */
1234 strcpy (s
, asect
->name
);
1236 asect
= bfd_get_section_by_name (abfd
, s
);
1240 elf_section_data(asect
)->this_hdr
.sh_link
= this_idx
;
1241 /* @@ Assuming 32 bits! */
1242 elf_section_data(asect
)->this_hdr
.sh_entsize
= 0xc;
1244 this_hdr
->sh_type
= SHT_STRTAB
;
1249 DEFUN (elf_fake_sections
, (abfd
, asect
, obj
),
1251 asection
* asect AND
1254 /* most of what is in bfd_shdr_from_section goes in here... */
1255 /* and all of these sections generate at *least* one ELF section. */
1257 Elf_Internal_Shdr
*this_hdr
;
1258 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1260 bfd_add_to_strtab (abfd
, elf_shstrtab (abfd
), asect
->name
);
1261 /* We need to log the type *now* so that elf_section_from_bfd_section
1262 can find us... have to set rawdata too. */
1263 this_hdr
->rawdata
= (void *) asect
;
1264 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1265 if ((asect
->flags
& SEC_ALLOC
) && (asect
->flags
& SEC_LOAD
))
1266 this_hdr
->sh_type
= SHT_PROGBITS
;
1267 else if ((asect
->flags
& SEC_ALLOC
) && ((asect
->flags
& SEC_LOAD
) == 0))
1269 BFD_ASSERT (strcmp (asect
->name
, ".bss") == 0
1270 || strcmp (asect
->name
, ".sbss") == 0);
1271 this_hdr
->sh_type
= SHT_NOBITS
;
1273 /* FIXME I am not sure how to detect a .note section from the flags
1274 word of an `asection'. */
1275 else if (!strcmp (asect
->name
, ".note"))
1276 this_hdr
->sh_type
= SHT_NOTE
;
1278 this_hdr
->sh_type
= SHT_PROGBITS
;
1280 this_hdr
->sh_flags
= 0;
1281 this_hdr
->sh_addr
= 0;
1282 this_hdr
->sh_size
= 0;
1283 this_hdr
->sh_entsize
= 0;
1284 this_hdr
->sh_info
= 0;
1285 this_hdr
->sh_link
= 0;
1286 this_hdr
->sh_offset
= 0;
1289 /* Now, check for processor-specific section types. */
1291 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1293 if (bed
->elf_backend_fake_sections
)
1294 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1298 /* Emit a strtab and symtab, and possibly a reloc section. */
1299 Elf_Internal_Shdr
*rela_hdr
;
1301 /* Note that only one symtab is used, so just remember it
1304 if (asect
->flags
& SEC_RELOC
)
1306 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1308 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1310 bfd_add_2_to_strtab (abfd
, elf_shstrtab (abfd
),
1311 use_rela_p
? ".rela" : ".rel",
1313 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1314 rela_hdr
->sh_entsize
= (use_rela_p
1315 ? sizeof (Elf_External_Rela
)
1316 : sizeof (Elf_External_Rel
));
1318 rela_hdr
->sh_flags
= 0;
1319 rela_hdr
->sh_addr
= 0;
1320 rela_hdr
->sh_size
= 0;
1321 rela_hdr
->sh_offset
= 0;
1323 /* FIXME: Systems I've checked use an alignment of 4, but some
1324 systems may use a different alignment. */
1325 rela_hdr
->sh_addralign
= 4;
1330 if (asect
->flags
& SEC_ALLOC
)
1332 this_hdr
->sh_flags
|= SHF_ALLOC
;
1333 if (asect
->flags
& SEC_LOAD
)
1335 /* @@ Do something with sh_type? */
1338 if (!(asect
->flags
& SEC_READONLY
))
1339 this_hdr
->sh_flags
|= SHF_WRITE
;
1340 if (asect
->flags
& SEC_CODE
)
1341 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1344 /* Map symbol from it's internal number to the external number, moving
1345 all local symbols to be at the head of the list. */
1348 sym_is_global (abfd
, sym
)
1352 /* If the backend has a special mapping, use it. */
1353 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1354 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1357 if (sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
))
1359 if (sym
->flags
& BSF_LOCAL
)
1363 if (sym
->section
== 0)
1365 /* Is this valid? */
1370 if (sym
->section
== &bfd_und_section
)
1372 if (bfd_is_com_section (sym
->section
))
1374 if (sym
->flags
& (BSF_LOCAL
| BSF_SECTION_SYM
| BSF_FILE
))
1380 DEFUN (elf_map_symbols
, (abfd
), bfd
* abfd
)
1382 int symcount
= bfd_get_symcount (abfd
);
1383 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1384 asymbol
**sect_syms
;
1386 int num_globals
= 0;
1387 int num_locals2
= 0;
1388 int num_globals2
= 0;
1390 int num_sections
= 0;
1391 Elf_Sym_Extra
*sym_extra
;
1396 fprintf (stderr
, "elf_map_symbols\n");
1400 /* Add local symbols for each section for which there are relocs.
1401 FIXME: How can we tell which sections have relocs at this point?
1402 Will reloc_count always be accurate? Actually, I think most ELF
1403 targets create section symbols for all sections anyhow. */
1404 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1406 if (max_index
< asect
->index
)
1407 max_index
= asect
->index
;
1411 elf_num_section_syms (abfd
) = max_index
;
1412 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1413 elf_section_syms (abfd
) = sect_syms
;
1417 bfd_set_error (bfd_error_no_memory
);
1421 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1423 asymbol
*sym
= bfd_make_empty_symbol (abfd
);
1426 bfd_set_error (bfd_error_no_memory
);
1429 sym
->the_bfd
= abfd
;
1430 sym
->name
= asect
->name
;
1431 sym
->value
= asect
->vma
;
1432 sym
->flags
= BSF_SECTION_SYM
;
1433 sym
->section
= asect
;
1434 sect_syms
[asect
->index
] = sym
;
1438 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1439 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1446 syms
= (asymbol
**) bfd_realloc (abfd
, syms
,
1447 ((symcount
+ num_sections
+ 1)
1448 * sizeof (asymbol
*)));
1450 syms
= (asymbol
**) bfd_alloc (abfd
,
1451 (num_sections
+ 1) * sizeof (asymbol
*));
1454 bfd_set_error (bfd_error_no_memory
);
1458 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1460 if (sect_syms
[asect
->index
])
1461 syms
[symcount
++] = sect_syms
[asect
->index
];
1464 syms
[symcount
] = (asymbol
*) 0;
1465 bfd_set_symtab (abfd
, syms
, symcount
);
1468 elf_sym_extra (abfd
) = sym_extra
1469 = (Elf_Sym_Extra
*) bfd_alloc (abfd
, symcount
* sizeof (Elf_Sym_Extra
));
1472 bfd_set_error (bfd_error_no_memory
);
1476 /* Identify and classify all of the symbols. */
1477 for (idx
= 0; idx
< symcount
; idx
++)
1479 if (!sym_is_global (abfd
, syms
[idx
]))
1485 /* Now provide mapping information. Add +1 for skipping over the
1487 for (idx
= 0; idx
< symcount
; idx
++)
1489 syms
[idx
]->udata
= (PTR
) &sym_extra
[idx
];
1490 if (!sym_is_global (abfd
, syms
[idx
]))
1491 sym_extra
[idx
].elf_sym_num
= 1 + num_locals2
++;
1493 sym_extra
[idx
].elf_sym_num
= 1 + num_locals
+ num_globals2
++;
1496 elf_num_locals (abfd
) = num_locals
;
1497 elf_num_globals (abfd
) = num_globals
;
1501 static boolean
assign_section_numbers ();
1502 static boolean
assign_file_positions_except_relocs ();
1505 DEFUN (elf_compute_section_file_positions
, (abfd
), bfd
* abfd
)
1507 bfd_map_over_sections (abfd
, elf_fake_sections
, 0);
1509 if (!assign_section_numbers (abfd
))
1512 bfd_map_over_sections (abfd
, elf_make_sections
, 0);
1514 bfd_map_over_sections (abfd
, fix_up_strtabs
, 0); /* .stab/.stabstr &c */
1516 if (swap_out_syms (abfd
) == false)
1519 if (!assign_file_positions_except_relocs (abfd
))
1526 DEFUN (elf_write_phdrs
, (abfd
, i_ehdrp
, i_phdrp
, phdr_cnt
),
1528 Elf_Internal_Ehdr
* i_ehdrp AND
1529 Elf_Internal_Phdr
* i_phdrp AND
1530 unsigned short phdr_cnt
)
1532 /* first program header entry goes after the file header */
1533 int outbase
= i_ehdrp
->e_phoff
;
1535 Elf_External_Phdr x_phdr
;
1537 for (i
= 0; i
< phdr_cnt
; i
++)
1539 elf_swap_phdr_out (abfd
, i_phdrp
+ i
, &x_phdr
);
1540 bfd_seek (abfd
, outbase
, SEEK_SET
);
1541 bfd_write ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
);
1542 outbase
+= sizeof (x_phdr
);
1548 static const Elf_Internal_Shdr null_shdr
;
1550 /* Assign all ELF section numbers. The dummy first section is handled here
1551 too. The link/info pointers for the standard section types are filled
1552 in here too, while we're at it. (Link pointers for .stab sections are
1553 not filled in here.) */
1555 assign_section_numbers (abfd
)
1558 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1560 int section_number
= 1;
1562 Elf_Internal_Shdr
**i_shdrp
;
1564 t
->shstrtab_hdr
.sh_size
= elf_shstrtab(abfd
)->length
;
1565 t
->shstrtab_hdr
.contents
= (void *) elf_shstrtab(abfd
)->tab
;
1566 shstrtab_length_fixed
= 1;
1568 t
->shstrtab_section
= section_number
++;
1569 elf_elfheader(abfd
)->e_shstrndx
= t
->shstrtab_section
;
1572 t
->symtab_section
= section_number
++;
1573 t
->strtab_section
= section_number
++;
1574 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1576 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1578 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1579 d
->this_idx
= section_number
++;
1580 if (sec
->flags
& SEC_RELOC
)
1582 d
->rel_idx
= section_number
++;
1583 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1584 d
->rel_hdr
.sh_info
= d
->this_idx
;
1588 /* No handling for per-section string tables currently. */
1590 elf_elfheader(abfd
)->e_shnum
= section_number
;
1592 /* Set up the list of section header pointers, in agreement with the
1594 i_shdrp
= (Elf_Internal_Shdr
**)
1595 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*));
1598 bfd_set_error (bfd_error_no_memory
);
1601 elf_elfsections(abfd
) = i_shdrp
;
1602 for (i
= 0; i
< section_number
; i
++)
1605 i_shdrp
[0] = (Elf_Internal_Shdr
*) &null_shdr
;
1606 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1609 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1610 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1612 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1614 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1615 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1617 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1619 /* Make sure we got everything.... */
1620 for (i
= 0; i
< section_number
; i
++)
1621 if (i_shdrp
[i
] == 0)
1626 static INLINE file_ptr
1627 assign_file_position_for_section (i_shdrp
, offset
)
1628 Elf_Internal_Shdr
*i_shdrp
;
1633 if (i_shdrp
->sh_addralign
!= 0)
1634 align
= i_shdrp
->sh_addralign
;
1637 i_shdrp
->sh_offset
= offset
= BFD_ALIGN (offset
, align
);
1638 if (i_shdrp
->rawdata
!= NULL
)
1639 ((asection
*) i_shdrp
->rawdata
)->filepos
= offset
;
1640 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1641 offset
+= i_shdrp
->sh_size
;
1645 static INLINE file_ptr
1646 align_file_position (off
)
1649 return (off
+ FILE_ALIGN
- 1) & ~(FILE_ALIGN
- 1);
1652 static INLINE file_ptr
1653 assign_file_positions_for_symtab_and_strtabs (abfd
, off
)
1657 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1659 off
= align_file_position (off
);
1660 off
= assign_file_position_for_section (&t
->symtab_hdr
, off
);
1661 off
= assign_file_position_for_section (&t
->shstrtab_hdr
, off
);
1662 off
= assign_file_position_for_section (&t
->strtab_hdr
, off
);
1667 bfd_vma low
, mem_size
;
1671 struct seg_info
*next
;
1675 map_program_segments (abfd
)
1678 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1679 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1680 Elf_Internal_Shdr
*i_shdrp
;
1681 Elf_Internal_Phdr
*phdr
;
1683 unsigned int i
, n_left
= 0;
1684 file_ptr lowest_offset
= 0;
1685 struct seg_info
*seg
= NULL
;
1687 done
= (char *) malloc (i_ehdrp
->e_shnum
);
1690 bfd_set_error (bfd_error_no_memory
);
1693 memset (done
, 0, i_ehdrp
->e_shnum
);
1694 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1696 i_shdrp
= i_shdrpp
[i
];
1697 /* If it's going to be mapped in, it's been assigned a position. */
1698 if (i_shdrp
->sh_offset
+ 1 == 0)
1700 /* Well, not really, but we won't process it here. */
1704 if (i_shdrp
->sh_offset
< lowest_offset
1705 || lowest_offset
== 0)
1706 lowest_offset
= i_shdrp
->sh_offset
;
1707 /* Only interested in PROGBITS or NOBITS for generating segments. */
1708 switch (i_shdrp
->sh_type
)
1721 bfd_vma lowest_vma
= -1, high
;
1725 struct seg_info
*snew
;
1726 struct seg_info
**s_ptr
;
1728 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1730 i_shdrp
= i_shdrpp
[i
];
1731 if (!done
[i
] && i_shdrp
->sh_addr
< lowest_vma
)
1733 lowest_vma
= i_shdrp
->sh_addr
;
1739 /* So now we know the lowest vma of any unassigned sections; start
1741 snew
= (struct seg_info
*) bfd_alloc (abfd
, sizeof (struct seg_info
));
1744 bfd_set_error (bfd_error_no_memory
);
1748 while (*s_ptr
!= (struct seg_info
*) NULL
)
1749 s_ptr
= &(*s_ptr
)->next
;
1752 snew
->low
= lowest_vma
;
1753 i_shdrp
= i_shdrpp
[low_sec
];
1754 snew
->start_pos
= i_shdrp
->sh_offset
;
1755 snew
->sh_flags
= i_shdrp
->sh_flags
;
1756 done
[low_sec
] = 1, n_left
--;
1757 mem_size
= i_shdrp
->sh_size
;
1758 high
= lowest_vma
+ i_shdrp
->sh_size
;
1760 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1761 file_size
= i_shdrp
->sh_size
;
1763 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1769 i_shdrp
= i_shdrpp
[i
];
1770 /* position of next byte on disk */
1771 f1
= snew
->start_pos
+ file_size
;
1772 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1774 if (i_shdrp
->sh_offset
- f1
!= i_shdrp
->sh_addr
- high
)
1776 if (file_size
!= mem_size
)
1779 else /* sh_type == NOBITS */
1781 /* If the section in question has no contents in the disk
1782 file, we really don't care where it supposedly starts.
1783 But we don't want to bother merging it into this segment
1784 if it doesn't start on this memory page. */
1785 bfd_vma page1
, page2
;
1786 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1788 /* page number in address space of current end of snew */
1789 page1
= (high
- 1 + maxpagesize
- 1) / maxpagesize
;
1790 /* page number in address space of start of this section */
1791 page2
= (i_shdrp
->sh_addr
+ maxpagesize
- 1) / maxpagesize
;
1796 done
[i
] = 1, n_left
--;
1797 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1798 file_size
= i_shdrp
->sh_offset
+ i_shdrp
->sh_size
- snew
->start_pos
;
1799 mem_size
= i_shdrp
->sh_addr
+ i_shdrp
->sh_size
- snew
->low
;
1800 high
= i_shdrp
->sh_addr
+ i_shdrp
->sh_size
;
1803 snew
->file_size
= file_size
;
1804 snew
->mem_size
= mem_size
;
1806 /* Now do something with the list of segments we've built up. */
1808 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1813 for (s
= seg
; s
; s
= s
->next
)
1817 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
1818 sz
= sizeof (Elf_External_Phdr
) * n_segs
;
1819 if (align_file_position (i_ehdrp
->e_ehsize
) + sz
<= lowest_offset
)
1820 i_ehdrp
->e_phoff
= align_file_position (i_ehdrp
->e_ehsize
);
1823 i_ehdrp
->e_phoff
= align_file_position (elf_tdata (abfd
)->next_file_pos
);
1824 elf_tdata (abfd
)->next_file_pos
= i_ehdrp
->e_phoff
+ sz
;
1826 phdr
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
,
1827 n_segs
* sizeof (Elf_Internal_Phdr
));
1830 bfd_set_error (bfd_error_no_memory
);
1831 abort(); /* FIXME */
1833 elf_tdata (abfd
)->phdr
= phdr
;
1836 phdr
->p_type
= PT_LOAD
; /* only type we really support so far */
1837 phdr
->p_offset
= seg
->start_pos
;
1838 phdr
->p_vaddr
= seg
->low
;
1840 phdr
->p_filesz
= seg
->file_size
;
1841 phdr
->p_memsz
= seg
->mem_size
;
1842 phdr
->p_flags
= PF_R
;
1843 phdr
->p_align
= maxpagesize
; /* ? */
1844 if (seg
->sh_flags
& SHF_WRITE
)
1845 /* SysVr4 ELF docs say "data segments normally have read, write,
1846 and execute permissions." */
1847 phdr
->p_flags
|= (PF_W
| PF_X
);
1848 if (seg
->sh_flags
& SHF_EXECINSTR
)
1849 phdr
->p_flags
|= PF_X
;
1853 i_ehdrp
->e_phnum
= n_segs
;
1855 elf_write_phdrs (abfd
, i_ehdrp
, elf_tdata (abfd
)->phdr
, i_ehdrp
->e_phnum
);
1866 assign_file_positions_except_relocs (abfd
)
1869 /* For now, we ignore the possibility of having program segments, which
1870 may require some alignment in the file. That'll require padding, and
1871 some interesting calculations to optimize file space usage.
1873 Also, since the application may change the list of relocations for
1874 a given section, we don't figure them in here. We'll put them at the
1875 end of the file, at positions computed during bfd_close.
1877 The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ...
1878 or: <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */
1880 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1883 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1884 Elf_Internal_Shdr
*i_shdrp
;
1885 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1886 int exec_p
= (abfd
->flags
& EXEC_P
) != 0;
1887 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1889 /* Everything starts after the ELF file header. */
1890 off
= i_ehdrp
->e_ehsize
;
1894 /* Section headers. */
1895 off
= align_file_position (off
);
1896 i_ehdrp
->e_shoff
= off
;
1897 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1898 off
= assign_file_positions_for_symtab_and_strtabs (abfd
, off
);
1900 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1902 /* The symtab and strtab sections are placed by
1903 assign_file_positions_for_symtab_and_strtabs. */
1904 if (i
== t
->symtab_section
1905 || i
== t
->strtab_section
1906 || i
== t
->shstrtab_section
)
1909 i_shdrp
= i_shdrpp
[i
];
1910 if (i_shdrp
->sh_type
== SHT_REL
|| i_shdrp
->sh_type
== SHT_RELA
)
1912 i_shdrp
->sh_offset
= -1;
1917 if (maxpagesize
== 0)
1918 maxpagesize
= 1; /* make the arithmetic work */
1919 /* This isn't necessarily going to give the best packing, if the
1920 segments require padding between them, but since that isn't
1921 usually the case, this'll do. */
1922 if ((i_shdrp
->sh_flags
& SHF_ALLOC
) == 0)
1924 i_shdrp
->sh_offset
= -1;
1927 /* Blindly assume that the segments are ordered optimally. With
1928 the default LD script, they will be. */
1929 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1931 /* need big unsigned type */
1933 addtl_off
= i_shdrp
->sh_addr
- off
;
1934 addtl_off
= addtl_off
% maxpagesize
;
1941 off
= assign_file_position_for_section (i_shdrp
, off
);
1944 && i_shdrp
->sh_type
== SHT_NOBITS
1945 && (i
== i_ehdrp
->e_shnum
1946 || i_shdrpp
[i
+ 1]->sh_type
!= SHT_NOBITS
))
1948 /* Skip to the next page to ensure that when the file is
1949 loaded the bss section is loaded with zeroes. I don't
1950 know if this is required on all platforms, but it
1951 shouldn't really hurt. */
1952 off
= BFD_ALIGN (off
, maxpagesize
);
1956 && get_elf_backend_data(abfd
)->maxpagesize
> 1
1957 && i_shdrp
->sh_type
== SHT_PROGBITS
1958 && (i_shdrp
->sh_flags
& SHF_ALLOC
)
1959 && (i_shdrp
->sh_offset
- i_shdrp
->sh_addr
) % get_elf_backend_data (abfd
)->maxpagesize
!= 0)
1964 elf_tdata (abfd
)->next_file_pos
= off
;
1965 if (!map_program_segments (abfd
))
1967 off
= elf_tdata (abfd
)->next_file_pos
;
1969 /* Section headers. */
1970 off
= align_file_position (off
);
1971 i_ehdrp
->e_shoff
= off
;
1972 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1974 off
= assign_file_positions_for_symtab_and_strtabs (abfd
, off
);
1976 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1978 i_shdrp
= i_shdrpp
[i
];
1979 if (i_shdrp
->sh_offset
+ 1 == 0
1980 && i_shdrp
->sh_type
!= SHT_REL
1981 && i_shdrp
->sh_type
!= SHT_RELA
)
1982 off
= assign_file_position_for_section (i_shdrp
, off
);
1985 elf_tdata (abfd
)->next_file_pos
= off
;
1993 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1994 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
1995 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
1997 struct strtab
*shstrtab
;
1999 i_ehdrp
= elf_elfheader (abfd
);
2000 i_shdrp
= elf_elfsections (abfd
);
2002 shstrtab
= bfd_new_strtab (abfd
);
2006 elf_shstrtab (abfd
) = shstrtab
;
2008 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
2009 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
2010 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
2011 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
2013 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS
;
2014 i_ehdrp
->e_ident
[EI_DATA
] =
2015 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
2016 i_ehdrp
->e_ident
[EI_VERSION
] = EV_CURRENT
;
2018 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2019 i_ehdrp
->e_ident
[count
] = 0;
2021 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
) ? ET_EXEC
: ET_REL
;
2022 switch (bfd_get_arch (abfd
))
2024 case bfd_arch_unknown
:
2025 i_ehdrp
->e_machine
= EM_NONE
;
2027 case bfd_arch_sparc
:
2028 i_ehdrp
->e_machine
= EM_SPARC
;
2029 /* start-sanitize-v9 */
2031 i_ehdrp
->e_machine
= EM_SPARC64
;
2033 /* end-sanitize-v9 */
2036 i_ehdrp
->e_machine
= EM_386
;
2039 i_ehdrp
->e_machine
= EM_68K
;
2042 i_ehdrp
->e_machine
= EM_88K
;
2045 i_ehdrp
->e_machine
= EM_860
;
2047 case bfd_arch_mips
: /* MIPS Rxxxx */
2048 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
2051 i_ehdrp
->e_machine
= EM_HPPA
;
2053 case bfd_arch_powerpc
:
2054 i_ehdrp
->e_machine
= EM_CYGNUS_POWERPC
;
2056 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2058 i_ehdrp
->e_machine
= EM_NONE
;
2060 i_ehdrp
->e_version
= EV_CURRENT
;
2061 i_ehdrp
->e_ehsize
= sizeof (Elf_External_Ehdr
);
2063 /* no program header, for now. */
2064 i_ehdrp
->e_phoff
= 0;
2065 i_ehdrp
->e_phentsize
= 0;
2066 i_ehdrp
->e_phnum
= 0;
2068 /* each bfd section is section header entry */
2069 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
2070 i_ehdrp
->e_shentsize
= sizeof (Elf_External_Shdr
);
2072 /* if we're building an executable, we'll need a program header table */
2073 if (abfd
->flags
& EXEC_P
)
2075 /* it all happens later */
2077 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
2079 /* elf_build_phdrs() returns a (NULL-terminated) array of
2080 Elf_Internal_Phdrs */
2081 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
2082 i_ehdrp
->e_phoff
= outbase
;
2083 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
2088 i_ehdrp
->e_phentsize
= 0;
2090 i_ehdrp
->e_phoff
= 0;
2093 elf_tdata (abfd
)->symtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2095 elf_tdata (abfd
)->strtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2097 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2103 swap_out_syms (abfd
)
2106 if (!elf_map_symbols (abfd
))
2109 /* Dump out the symtabs. */
2111 int symcount
= bfd_get_symcount (abfd
);
2112 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2113 struct strtab
*stt
= bfd_new_strtab (abfd
);
2114 Elf_Internal_Shdr
*symtab_hdr
;
2115 Elf_Internal_Shdr
*symstrtab_hdr
;
2116 Elf_External_Sym
*outbound_syms
;
2121 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2122 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2123 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
2124 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2125 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2127 /* FIXME: Systems I've checked use 4 byte alignment for .symtab,
2128 but it is possible that there are systems which use a different
2130 symtab_hdr
->sh_addralign
= 4;
2132 /* see assert in elf_fake_sections that supports this: */
2133 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2134 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2136 outbound_syms
= (Elf_External_Sym
*)
2137 bfd_alloc (abfd
, (1 + symcount
) * sizeof (Elf_External_Sym
));
2140 bfd_set_error (bfd_error_no_memory
);
2143 /* now generate the data (for "contents") */
2145 /* Fill in zeroth symbol and swap it out. */
2146 Elf_Internal_Sym sym
;
2152 sym
.st_shndx
= SHN_UNDEF
;
2153 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
2155 for (idx
= 0; idx
< symcount
; idx
++)
2157 Elf_Internal_Sym sym
;
2158 bfd_vma value
= syms
[idx
]->value
;
2160 if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2161 /* Section symbols have no names. */
2164 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
2166 if (bfd_is_com_section (syms
[idx
]->section
))
2168 /* ELF common symbols put the alignment into the `value' field,
2169 and the size into the `size' field. This is backwards from
2170 how BFD handles it, so reverse it here. */
2171 sym
.st_size
= value
;
2172 /* Should retrieve this from somewhere... */
2174 sym
.st_shndx
= elf_section_from_bfd_section (abfd
,
2175 syms
[idx
]->section
);
2179 asection
*sec
= syms
[idx
]->section
;
2180 elf_symbol_type
*type_ptr
;
2183 if (sec
->output_section
)
2185 value
+= sec
->output_offset
;
2186 sec
= sec
->output_section
;
2189 sym
.st_value
= value
;
2190 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2191 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2192 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec
);
2196 /* Writing this would be a hell of a lot easier if we had
2197 some decent documentation on bfd, and knew what to expect
2198 of the library, and what to demand of applications. For
2199 example, it appears that `objcopy' might not set the
2200 section of a symbol to be a section that is actually in
2202 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2203 BFD_ASSERT (sec2
!= 0);
2204 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec2
);
2205 BFD_ASSERT (shndx
!= -1);
2209 if (bfd_is_com_section (syms
[idx
]->section
))
2210 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2211 else if (syms
[idx
]->section
== &bfd_und_section
)
2212 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2213 else if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2214 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2215 else if (syms
[idx
]->flags
& BSF_FILE
)
2216 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2219 int bind
= STB_LOCAL
;
2220 int type
= STT_OBJECT
;
2221 unsigned int flags
= syms
[idx
]->flags
;
2223 if (flags
& BSF_LOCAL
)
2225 else if (flags
& BSF_WEAK
)
2227 else if (flags
& BSF_GLOBAL
)
2230 if (flags
& BSF_FUNCTION
)
2233 sym
.st_info
= ELF_ST_INFO (bind
, type
);
2237 elf_swap_symbol_out (abfd
, &sym
,
2239 + elf_sym_extra (abfd
)[idx
].elf_sym_num
));
2242 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2243 symstrtab_hdr
->contents
= (PTR
) stt
->tab
;
2244 symstrtab_hdr
->sh_size
= stt
->length
;
2245 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2247 symstrtab_hdr
->sh_flags
= 0;
2248 symstrtab_hdr
->sh_addr
= 0;
2249 symstrtab_hdr
->sh_entsize
= 0;
2250 symstrtab_hdr
->sh_link
= 0;
2251 symstrtab_hdr
->sh_info
= 0;
2252 symstrtab_hdr
->sh_addralign
= 1;
2253 symstrtab_hdr
->size
= 0;
2256 /* put the strtab out too... */
2258 Elf_Internal_Shdr
*this_hdr
;
2260 this_hdr
= &elf_tdata(abfd
)->shstrtab_hdr
;
2261 this_hdr
->contents
= (PTR
) elf_shstrtab (abfd
)->tab
;
2262 this_hdr
->sh_size
= elf_shstrtab (abfd
)->length
;
2263 this_hdr
->sh_type
= SHT_STRTAB
;
2264 this_hdr
->sh_flags
= 0;
2265 this_hdr
->sh_addr
= 0;
2266 this_hdr
->sh_entsize
= 0;
2267 this_hdr
->sh_addralign
= 1;
2274 write_shdrs_and_ehdr (abfd
)
2277 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
2278 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2279 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
2280 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2282 struct strtab
*shstrtab
;
2284 i_ehdrp
= elf_elfheader (abfd
);
2285 i_shdrp
= elf_elfsections (abfd
);
2286 shstrtab
= elf_shstrtab (abfd
);
2288 /* swap the header before spitting it out... */
2291 elf_debug_file (i_ehdrp
);
2293 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
2294 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
2295 bfd_write ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
);
2297 /* at this point we've concocted all the ELF sections... */
2298 x_shdrp
= (Elf_External_Shdr
*)
2299 bfd_alloc (abfd
, sizeof (*x_shdrp
) * (i_ehdrp
->e_shnum
));
2302 bfd_set_error (bfd_error_no_memory
);
2306 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2309 elf_debug_section (shstrtab
->tab
+ i_shdrp
[count
]->sh_name
, count
,
2312 elf_swap_shdr_out (abfd
, i_shdrp
[count
], x_shdrp
+ count
);
2314 bfd_seek (abfd
, (file_ptr
) i_ehdrp
->e_shoff
, SEEK_SET
);
2315 bfd_write ((PTR
) x_shdrp
, sizeof (*x_shdrp
), i_ehdrp
->e_shnum
, abfd
);
2316 /* need to dump the string table too... */
2322 assign_file_positions_for_relocs (abfd
)
2325 file_ptr off
= elf_tdata(abfd
)->next_file_pos
;
2327 Elf_Internal_Shdr
**shdrpp
= elf_elfsections (abfd
);
2328 Elf_Internal_Shdr
*shdrp
;
2329 for (i
= 1; i
< elf_elfheader(abfd
)->e_shnum
; i
++)
2332 if (shdrp
->sh_type
!= SHT_REL
&& shdrp
->sh_type
!= SHT_RELA
)
2334 off
= align_file_position (off
);
2335 off
= assign_file_position_for_section (shdrp
, off
);
2337 elf_tdata(abfd
)->next_file_pos
= off
;
2341 DEFUN (NAME(bfd_elf
,write_object_contents
), (abfd
), bfd
* abfd
)
2343 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2344 Elf_Internal_Ehdr
*i_ehdrp
;
2345 Elf_Internal_Shdr
**i_shdrp
;
2348 /* We don't know how to write dynamic objects. Specifically, we
2349 don't know how to construct the program header. */
2350 if ((abfd
->flags
& DYNAMIC
) != 0)
2352 fprintf (stderr
, "Writing ELF dynamic objects is not supported\n");
2353 bfd_set_error (bfd_error_wrong_format
);
2357 if (abfd
->output_has_begun
== false)
2359 if (prep_headers (abfd
) == false)
2361 if (elf_compute_section_file_positions (abfd
) == false)
2363 abfd
->output_has_begun
= true;
2366 i_shdrp
= elf_elfsections (abfd
);
2367 i_ehdrp
= elf_elfheader (abfd
);
2369 bfd_map_over_sections (abfd
, write_relocs
, (PTR
) 0);
2370 assign_file_positions_for_relocs (abfd
);
2372 /* After writing the headers, we need to write the sections too... */
2373 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2375 if (bed
->elf_backend_section_processing
)
2376 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2377 if (i_shdrp
[count
]->contents
)
2379 bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
);
2380 bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
, 1,
2385 if (bed
->elf_backend_final_write_processing
)
2386 (*bed
->elf_backend_final_write_processing
) (abfd
);
2388 return write_shdrs_and_ehdr (abfd
);
2391 /* Given an index of a section, retrieve a pointer to it. Note
2392 that for our purposes, sections are indexed by {1, 2, ...} with
2393 0 being an illegal index. */
2395 /* In the original, each ELF section went into exactly one BFD
2396 section. This doesn't really make sense, so we need a real mapping.
2397 The mapping has to hide in the Elf_Internal_Shdr since asection
2398 doesn't have anything like a tdata field... */
2401 DEFUN (section_from_elf_index
, (abfd
, index
),
2405 /* @@ Is bfd_com_section really correct in all the places it could
2406 be returned from this routine? */
2408 if (index
== SHN_ABS
)
2409 return &bfd_com_section
; /* not abs? */
2410 if (index
== SHN_COMMON
)
2411 return &bfd_com_section
;
2413 if (index
> elf_elfheader (abfd
)->e_shnum
)
2417 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[index
];
2419 switch (hdr
->sh_type
)
2421 /* ELF sections that map to BFD sections */
2425 bfd_section_from_shdr (abfd
, index
);
2426 return (struct sec
*) hdr
->rawdata
;
2429 return (struct sec
*) &bfd_abs_section
;
2434 /* given a section, search the header to find them... */
2436 DEFUN (elf_section_from_bfd_section
, (abfd
, asect
),
2440 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2442 Elf_Internal_Shdr
*hdr
;
2443 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2445 if (asect
== &bfd_abs_section
)
2447 if (asect
== &bfd_com_section
)
2449 if (asect
== &bfd_und_section
)
2452 for (index
= 0; index
< maxindex
; index
++)
2454 hdr
= i_shdrp
[index
];
2455 switch (hdr
->sh_type
)
2457 /* ELF sections that map to BFD sections */
2463 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2469 /* fix_up_strtabs will generate STRTAB sections with names
2471 if (!strncmp (asect
->name
, ".stab", 5)
2472 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
2476 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2484 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2486 if (bed
->elf_backend_section_from_bfd_section
)
2491 if ((*bed
->elf_backend_section_from_bfd_section
)
2492 (abfd
, hdr
, asect
, &retval
))
2502 /* given a symbol, return the bfd index for that symbol. */
2504 DEFUN (elf_symbol_from_bfd_symbol
, (abfd
, asym_ptr_ptr
),
2506 struct symbol_cache_entry
**asym_ptr_ptr
)
2508 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2510 flagword flags
= asym_ptr
->flags
;
2512 /* When gas creates relocations against local labels, it creates its
2513 own symbol for the section, but does put the symbol into the
2514 symbol chain, so udata is 0. When the linker is generating
2515 relocatable output, this section symbol may be for one of the
2516 input sections rather than the output section. */
2517 if (asym_ptr
->udata
== (PTR
) 0
2518 && (flags
& BSF_SECTION_SYM
)
2519 && asym_ptr
->section
)
2523 if (asym_ptr
->section
->output_section
!= NULL
)
2524 indx
= asym_ptr
->section
->output_section
->index
;
2526 indx
= asym_ptr
->section
->index
;
2527 if (elf_section_syms (abfd
)[indx
])
2528 asym_ptr
->udata
= elf_section_syms (abfd
)[indx
]->udata
;
2531 if (asym_ptr
->udata
)
2532 idx
= ((Elf_Sym_Extra
*)asym_ptr
->udata
)->elf_sym_num
;
2542 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
2543 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
, elf_symbol_flags (flags
));
2552 DEFUN (elf_slurp_symbol_table
, (abfd
, symptrs
),
2554 asymbol
** symptrs
) /* Buffer for generated bfd symbols */
2556 Elf_Internal_Shdr
*hdr
= &elf_tdata(abfd
)->symtab_hdr
;
2557 long symcount
; /* Number of external ELF symbols */
2558 elf_symbol_type
*sym
; /* Pointer to current bfd symbol */
2559 elf_symbol_type
*symbase
; /* Buffer for generated bfd symbols */
2560 Elf_Internal_Sym i_sym
;
2561 Elf_External_Sym
*x_symp
= NULL
;
2563 /* this is only valid because there is only one symtab... */
2564 /* FIXME: This is incorrect, there may also be a dynamic symbol
2565 table which is a subset of the full symbol table. We either need
2566 to be prepared to read both (and merge them) or ensure that we
2567 only read the full symbol table. Currently we only get called to
2568 read the full symbol table. -fnf */
2570 /* Read each raw ELF symbol, converting from external ELF form to
2571 internal ELF form, and then using the information to create a
2572 canonical bfd symbol table entry.
2574 Note that we allocate the initial bfd canonical symbol buffer
2575 based on a one-to-one mapping of the ELF symbols to canonical
2576 symbols. We actually use all the ELF symbols, so there will be no
2577 space left over at the end. When we have all the symbols, we
2578 build the caller's pointer vector. */
2580 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2582 bfd_set_error (bfd_error_system_call
);
2586 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2589 sym
= symbase
= NULL
;
2594 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2596 bfd_set_error (bfd_error_system_call
);
2600 symbase
= ((elf_symbol_type
*)
2601 bfd_zalloc (abfd
, symcount
* sizeof (elf_symbol_type
)));
2602 if (symbase
== (elf_symbol_type
*) NULL
)
2604 bfd_set_error (bfd_error_no_memory
);
2609 /* Temporarily allocate room for the raw ELF symbols. */
2610 x_symp
= ((Elf_External_Sym
*)
2611 malloc (symcount
* sizeof (Elf_External_Sym
)));
2614 bfd_set_error (bfd_error_no_memory
);
2618 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
2619 != symcount
* sizeof (Elf_External_Sym
))
2621 bfd_set_error (bfd_error_system_call
);
2624 /* Skip first symbol, which is a null dummy. */
2625 for (i
= 1; i
< symcount
; i
++)
2627 elf_swap_symbol_in (abfd
, x_symp
+ i
, &i_sym
);
2628 memcpy (&sym
->internal_elf_sym
, &i_sym
, sizeof (Elf_Internal_Sym
));
2629 #ifdef ELF_KEEP_EXTSYM
2630 memcpy (&sym
->native_elf_sym
, x_symp
+ i
, sizeof (Elf_External_Sym
));
2632 sym
->symbol
.the_bfd
= abfd
;
2634 sym
->symbol
.name
= elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2637 sym
->symbol
.value
= i_sym
.st_value
;
2639 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
2641 sym
->symbol
.section
= section_from_elf_index (abfd
,
2644 else if (i_sym
.st_shndx
== SHN_ABS
)
2646 sym
->symbol
.section
= &bfd_abs_section
;
2648 else if (i_sym
.st_shndx
== SHN_COMMON
)
2650 sym
->symbol
.section
= &bfd_com_section
;
2651 /* Elf puts the alignment into the `value' field, and
2652 the size into the `size' field. BFD wants to see the
2653 size in the value field, and doesn't care (at the
2654 moment) about the alignment. */
2655 sym
->symbol
.value
= i_sym
.st_size
;
2657 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2659 sym
->symbol
.section
= &bfd_und_section
;
2662 sym
->symbol
.section
= &bfd_abs_section
;
2664 sym
->symbol
.value
-= sym
->symbol
.section
->vma
;
2666 switch (ELF_ST_BIND (i_sym
.st_info
))
2669 sym
->symbol
.flags
|= BSF_LOCAL
;
2672 sym
->symbol
.flags
|= BSF_GLOBAL
;
2675 sym
->symbol
.flags
|= BSF_WEAK
;
2679 switch (ELF_ST_TYPE (i_sym
.st_info
))
2682 sym
->symbol
.flags
|= BSF_SECTION_SYM
| BSF_DEBUGGING
;
2685 sym
->symbol
.flags
|= BSF_FILE
| BSF_DEBUGGING
;
2688 sym
->symbol
.flags
|= BSF_FUNCTION
;
2692 /* Do some backend-specific processing on this symbol. */
2694 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2695 if (ebd
->elf_backend_symbol_processing
)
2696 (*ebd
->elf_backend_symbol_processing
) (abfd
, &sym
->symbol
);
2703 /* Do some backend-specific processing on this symbol table. */
2705 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2706 if (ebd
->elf_backend_symbol_table_processing
)
2707 (*ebd
->elf_backend_symbol_table_processing
) (abfd
, symbase
, symcount
);
2710 /* We rely on the zalloc to clear out the final symbol entry. */
2712 bfd_get_symcount (abfd
) = symcount
= sym
- symbase
;
2714 /* Fill in the user's symbol pointer vector if needed. */
2718 while (symcount
-- > 0)
2720 *symptrs
++ = &sym
->symbol
;
2723 *symptrs
= 0; /* Final null pointer */
2735 /* Return the number of bytes required to hold the symtab vector.
2737 Note that we base it on the count plus 1, since we will null terminate
2738 the vector allocated based on this size. However, the ELF symbol table
2739 always has a dummy entry as symbol #0, so it ends up even. */
2742 DEFUN (elf_get_symtab_upper_bound
, (abfd
), bfd
* abfd
)
2744 unsigned int symcount
;
2745 unsigned int symtab_size
= 0;
2747 Elf_Internal_Shdr
*hdr
= &elf_tdata(abfd
)->symtab_hdr
;
2748 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2749 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2755 This function return the number of bytes required to store the
2756 relocation information associated with section <<sect>>
2757 attached to bfd <<abfd>>
2761 elf_get_reloc_upper_bound (abfd
, asect
)
2765 if (asect
->flags
& SEC_RELOC
)
2767 /* either rel or rela */
2768 return elf_section_data(asect
)->rel_hdr
.sh_size
;
2775 DEFUN (elf_slurp_reloca_table
, (abfd
, asect
, symbols
),
2780 Elf_External_Rela
*native_relocs
;
2781 arelent
*reloc_cache
;
2786 if (asect
->relocation
)
2788 if (asect
->reloc_count
== 0)
2790 if (asect
->flags
& SEC_CONSTRUCTOR
)
2793 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2794 native_relocs
= (Elf_External_Rela
*)
2795 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rela
));
2798 bfd_set_error (bfd_error_no_memory
);
2801 bfd_read ((PTR
) native_relocs
,
2802 sizeof (Elf_External_Rela
), asect
->reloc_count
, abfd
);
2804 reloc_cache
= (arelent
*)
2805 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2809 bfd_set_error (bfd_error_no_memory
);
2813 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2815 Elf_Internal_Rela dst
;
2816 Elf_External_Rela
*src
;
2818 cache_ptr
= reloc_cache
+ idx
;
2819 src
= native_relocs
+ idx
;
2820 elf_swap_reloca_in (abfd
, src
, &dst
);
2822 #ifdef RELOC_PROCESSING
2823 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2825 if (asect
->flags
& SEC_RELOC
)
2827 /* relocatable, so the offset is off of the section */
2828 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2832 /* non-relocatable, so the offset a virtual address */
2833 cache_ptr
->address
= dst
.r_offset
;
2836 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
2837 of zero points to the dummy symbol, which was not read into
2838 the symbol table SYMBOLS. */
2839 if (ELF_R_SYM (dst
.r_info
) == 0)
2840 cache_ptr
->sym_ptr_ptr
= bfd_abs_section
.symbol_ptr_ptr
;
2845 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
2847 /* Translate any ELF section symbol into a BFD section
2849 s
= *(cache_ptr
->sym_ptr_ptr
);
2850 if (s
->flags
& BSF_SECTION_SYM
)
2852 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
2853 s
= *cache_ptr
->sym_ptr_ptr
;
2854 if (s
->name
== 0 || s
->name
[0] == 0)
2858 cache_ptr
->addend
= dst
.r_addend
;
2860 /* Fill in the cache_ptr->howto field from dst.r_type */
2862 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2863 (*ebd
->elf_info_to_howto
) (abfd
, cache_ptr
, &dst
);
2868 asect
->relocation
= reloc_cache
;
2874 elf_debug_section (str
, num
, hdr
)
2877 Elf_Internal_Shdr
*hdr
;
2879 fprintf (stderr
, "\nSection#%d '%s' 0x%.8lx\n", num
, str
, (long) hdr
);
2881 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
2882 (long) hdr
->sh_name
,
2883 (long) hdr
->sh_type
,
2884 (long) hdr
->sh_flags
);
2886 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
2887 (long) hdr
->sh_addr
,
2888 (long) hdr
->sh_offset
,
2889 (long) hdr
->sh_size
);
2891 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
2892 (long) hdr
->sh_link
,
2893 (long) hdr
->sh_info
,
2894 (long) hdr
->sh_addralign
);
2895 fprintf (stderr
, "sh_entsize = %ld\n",
2896 (long) hdr
->sh_entsize
);
2897 fprintf (stderr
, "rawdata = 0x%.8lx\n", (long) hdr
->rawdata
);
2898 fprintf (stderr
, "contents = 0x%.8lx\n", (long) hdr
->contents
);
2899 fprintf (stderr
, "size = %ld\n", (long) hdr
->size
);
2904 elf_debug_file (ehdrp
)
2905 Elf_Internal_Ehdr
*ehdrp
;
2907 fprintf (stderr
, "e_entry = 0x%.8lx\n", (long) ehdrp
->e_entry
);
2908 fprintf (stderr
, "e_phoff = %ld\n", (long) ehdrp
->e_phoff
);
2909 fprintf (stderr
, "e_phnum = %ld\n", (long) ehdrp
->e_phnum
);
2910 fprintf (stderr
, "e_phentsize = %ld\n", (long) ehdrp
->e_phentsize
);
2911 fprintf (stderr
, "e_shoff = %ld\n", (long) ehdrp
->e_shoff
);
2912 fprintf (stderr
, "e_shnum = %ld\n", (long) ehdrp
->e_shnum
);
2913 fprintf (stderr
, "e_shentsize = %ld\n", (long) ehdrp
->e_shentsize
);
2918 DEFUN (elf_slurp_reloc_table
, (abfd
, asect
, symbols
),
2923 Elf_External_Rel
*native_relocs
;
2924 arelent
*reloc_cache
;
2926 Elf_Internal_Shdr
*data_hdr
;
2928 unsigned long data_max
;
2929 char buf
[4]; /* FIXME -- might be elf64 */
2933 if (asect
->relocation
)
2935 if (asect
->reloc_count
== 0)
2937 if (asect
->flags
& SEC_CONSTRUCTOR
)
2940 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2941 native_relocs
= (Elf_External_Rel
*)
2942 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rel
));
2945 bfd_set_error (bfd_error_no_memory
);
2948 bfd_read ((PTR
) native_relocs
,
2949 sizeof (Elf_External_Rel
), asect
->reloc_count
, abfd
);
2951 reloc_cache
= (arelent
*)
2952 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2956 bfd_set_error (bfd_error_no_memory
);
2960 /* Get the offset of the start of the segment we are relocating to read in
2961 the implicit addend. */
2962 data_hdr
= &elf_section_data(asect
)->this_hdr
;
2963 data_off
= data_hdr
->sh_offset
;
2964 data_max
= data_hdr
->sh_size
- sizeof (buf
) + 1;
2967 elf_debug_section ("data section", -1, data_hdr
);
2970 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2972 #ifdef RELOC_PROCESSING
2973 Elf_Internal_Rel dst
;
2974 Elf_External_Rel
*src
;
2976 cache_ptr
= reloc_cache
+ idx
;
2977 src
= native_relocs
+ idx
;
2978 elf_swap_reloc_in (abfd
, src
, &dst
);
2980 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2982 Elf_Internal_Rel dst
;
2983 Elf_External_Rel
*src
;
2985 cache_ptr
= reloc_cache
+ idx
;
2986 src
= native_relocs
+ idx
;
2988 elf_swap_reloc_in (abfd
, src
, &dst
);
2990 if (asect
->flags
& SEC_RELOC
)
2992 /* relocatable, so the offset is off of the section */
2993 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2997 /* non-relocatable, so the offset a virtual address */
2998 cache_ptr
->address
= dst
.r_offset
;
3001 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
3002 of zero points to the dummy symbol, which was not read into
3003 the symbol table SYMBOLS. */
3004 if (ELF_R_SYM (dst
.r_info
) == 0)
3005 cache_ptr
->sym_ptr_ptr
= bfd_abs_section
.symbol_ptr_ptr
;
3010 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
3012 /* Translate any ELF section symbol into a BFD section
3014 s
= *(cache_ptr
->sym_ptr_ptr
);
3015 if (s
->flags
& BSF_SECTION_SYM
)
3017 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
3018 s
= *cache_ptr
->sym_ptr_ptr
;
3019 if (s
->name
== 0 || s
->name
[0] == 0)
3023 BFD_ASSERT (dst
.r_offset
<= data_max
);
3024 cache_ptr
->addend
= 0;
3026 /* Fill in the cache_ptr->howto field from dst.r_type */
3028 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
3029 (*ebd
->elf_info_to_howto_rel
) (abfd
, cache_ptr
, &dst
);
3034 asect
->relocation
= reloc_cache
;
3039 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
3045 arelent
*tblptr
= section
->relocation
;
3046 unsigned int count
= 0;
3047 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
3049 /* snarfed from coffcode.h */
3051 elf_slurp_reloca_table (abfd
, section
, symbols
);
3053 elf_slurp_reloc_table (abfd
, section
, symbols
);
3055 tblptr
= section
->relocation
;
3059 for (; count
++ < section
->reloc_count
;)
3060 *relptr
++ = tblptr
++;
3063 return section
->reloc_count
;
3067 DEFUN (elf_get_symtab
, (abfd
, alocation
),
3069 asymbol
** alocation
)
3072 if (!elf_slurp_symbol_table (abfd
, alocation
))
3075 return bfd_get_symcount (abfd
);
3079 DEFUN (elf_make_empty_symbol
, (abfd
),
3082 elf_symbol_type
*newsym
;
3084 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
3087 bfd_set_error (bfd_error_no_memory
);
3092 newsym
->symbol
.the_bfd
= abfd
;
3093 return &newsym
->symbol
;
3098 DEFUN (elf_get_symbol_info
, (ignore_abfd
, symbol
, ret
),
3099 bfd
* ignore_abfd AND
3100 asymbol
* symbol AND
3103 bfd_symbol_info (symbol
, ret
);
3107 DEFUN (elf_print_symbol
, (ignore_abfd
, filep
, symbol
, how
),
3108 bfd
* ignore_abfd AND
3110 asymbol
* symbol AND
3111 bfd_print_symbol_type how
)
3113 FILE *file
= (FILE *) filep
;
3116 case bfd_print_symbol_name
:
3117 fprintf (file
, "%s", symbol
->name
);
3119 case bfd_print_symbol_more
:
3120 fprintf (file
, "elf ");
3121 fprintf_vma (file
, symbol
->value
);
3122 fprintf (file
, " %lx", (long) symbol
->flags
);
3124 case bfd_print_symbol_all
:
3126 CONST
char *section_name
;
3127 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
3128 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
3129 fprintf (file
, " %s\t%s",
3139 DEFUN (elf_get_lineno
, (ignore_abfd
, symbol
),
3140 bfd
* ignore_abfd AND
3143 fprintf (stderr
, "elf_get_lineno unimplemented\n");
3150 DEFUN (elf_set_arch_mach
, (abfd
, arch
, machine
),
3152 enum bfd_architecture arch AND
3153 unsigned long machine
)
3155 /* Allow any architecture to be supported by the elf backend */
3158 case bfd_arch_unknown
: /* EM_NONE */
3159 case bfd_arch_sparc
: /* EM_SPARC */
3160 case bfd_arch_i386
: /* EM_386 */
3161 case bfd_arch_m68k
: /* EM_68K */
3162 case bfd_arch_m88k
: /* EM_88K */
3163 case bfd_arch_i860
: /* EM_860 */
3164 case bfd_arch_mips
: /* EM_MIPS (MIPS R3000) */
3165 case bfd_arch_hppa
: /* EM_HPPA (HP PA_RISC) */
3166 case bfd_arch_powerpc
: /* EM_CYGNUS_POWERPC */
3167 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
3174 DEFUN (elf_find_nearest_line
, (abfd
,
3182 asection
* section AND
3183 asymbol
** symbols AND
3185 CONST
char **filename_ptr AND
3186 CONST
char **functionname_ptr AND
3187 unsigned int *line_ptr
)
3193 DEFUN (elf_sizeof_headers
, (abfd
, reloc
),
3197 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
3204 DEFUN (elf_set_section_contents
, (abfd
, section
, location
, offset
, count
),
3209 bfd_size_type count
)
3211 Elf_Internal_Shdr
*hdr
;
3213 if (abfd
->output_has_begun
== false) /* set by bfd.c handler? */
3215 /* do setup calculations (FIXME) */
3216 if (prep_headers (abfd
) == false)
3218 if (elf_compute_section_file_positions (abfd
) == false)
3220 abfd
->output_has_begun
= true;
3223 hdr
= &elf_section_data(section
)->this_hdr
;
3225 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
3227 if (bfd_write (location
, 1, count
, abfd
) != count
)
3234 DEFUN (elf_no_info_to_howto
, (abfd
, cache_ptr
, dst
),
3236 arelent
* cache_ptr AND
3237 Elf_Internal_Rela
* dst
)
3239 fprintf (stderr
, "elf RELA relocation support for target machine unimplemented\n");
3245 DEFUN (elf_no_info_to_howto_rel
, (abfd
, cache_ptr
, dst
),
3247 arelent
* cache_ptr AND
3248 Elf_Internal_Rel
* dst
)
3250 fprintf (stderr
, "elf REL relocation support for target machine unimplemented\n");
3256 /* Core file support */
3258 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3259 #include <sys/procfs.h>
3261 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
3262 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
3263 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
3269 DEFUN (bfd_prstatus
, (abfd
, descdata
, descsz
, filepos
),
3276 prstatus_t
*status
= (prstatus_t
*) 0;
3278 if (descsz
== sizeof (prstatus_t
))
3280 newsect
= bfd_make_section (abfd
, ".reg");
3281 newsect
->_raw_size
= sizeof (status
->pr_reg
);
3282 newsect
->filepos
= filepos
+ (long) &status
->pr_reg
;
3283 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3284 newsect
->alignment_power
= 2;
3285 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3287 memcpy (core_prstatus (abfd
), descdata
, descsz
);
3292 /* Stash a copy of the prpsinfo structure away for future use. */
3295 DEFUN (bfd_prpsinfo
, (abfd
, descdata
, descsz
, filepos
),
3303 if (descsz
== sizeof (prpsinfo_t
))
3305 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3307 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
3313 DEFUN (bfd_fpregset
, (abfd
, descdata
, descsz
, filepos
),
3321 newsect
= bfd_make_section (abfd
, ".reg2");
3322 newsect
->_raw_size
= descsz
;
3323 newsect
->filepos
= filepos
;
3324 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3325 newsect
->alignment_power
= 2;
3328 #endif /* HAVE_PROCFS */
3330 /* Return a pointer to the args (including the command name) that were
3331 seen by the program that generated the core dump. Note that for
3332 some reason, a spurious space is tacked onto the end of the args
3333 in some (at least one anyway) implementations, so strip it off if
3337 DEFUN (elf_core_file_failing_command
, (abfd
),
3341 if (core_prpsinfo (abfd
))
3343 prpsinfo_t
*p
= core_prpsinfo (abfd
);
3344 char *scan
= p
->pr_psargs
;
3349 if ((scan
> p
->pr_psargs
) && (*scan
== ' '))
3353 return p
->pr_psargs
;
3359 /* Return the number of the signal that caused the core dump. Presumably,
3360 since we have a core file, we got a signal of some kind, so don't bother
3361 checking the other process status fields, just return the signal number.
3365 DEFUN (elf_core_file_failing_signal
, (abfd
),
3369 if (core_prstatus (abfd
))
3371 return ((prstatus_t
*) (core_prstatus (abfd
)))->pr_cursig
;
3377 /* Check to see if the core file could reasonably be expected to have
3378 come for the current executable file. Note that by default we return
3379 true unless we find something that indicates that there might be a
3384 DEFUN (elf_core_file_matches_executable_p
, (core_bfd
, exec_bfd
),
3393 /* First, xvecs must match since both are ELF files for the same target. */
3395 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
3397 bfd_set_error (bfd_error_system_call
);
3403 /* If no prpsinfo, just return true. Otherwise, grab the last component
3404 of the exec'd pathname from the prpsinfo. */
3406 if (core_prpsinfo (core_bfd
))
3408 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
))->pr_fname
);
3415 /* Find the last component of the executable pathname. */
3417 if ((execname
= strrchr (exec_bfd
->filename
, '/')) != NULL
)
3423 execname
= (char *) exec_bfd
->filename
;
3426 /* See if they match */
3428 return strcmp (execname
, corename
) ? false : true;
3434 #endif /* HAVE_PROCFS */
3437 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3438 the information that would normally be available from the /proc interface
3439 for the process, at the time the process dumped core. Currently this
3440 includes copies of the prstatus, prpsinfo, and fpregset structures.
3442 Since these structures are potentially machine dependent in size and
3443 ordering, bfd provides two levels of support for them. The first level,
3444 available on all machines since it does not require that the host
3445 have /proc support or the relevant include files, is to create a bfd
3446 section for each of the prstatus, prpsinfo, and fpregset structures,
3447 without any interpretation of their contents. With just this support,
3448 the bfd client will have to interpret the structures itself. Even with
3449 /proc support, it might want these full structures for it's own reasons.
3451 In the second level of support, where HAVE_PROCFS is defined, bfd will
3452 pick apart the structures to gather some additional information that
3453 clients may want, such as the general register set, the name of the
3454 exec'ed file and its arguments, the signal (if any) that caused the
3460 DEFUN (elf_corefile_note
, (abfd
, hdr
),
3462 Elf_Internal_Phdr
* hdr
)
3464 Elf_External_Note
*x_note_p
; /* Elf note, external form */
3465 Elf_Internal_Note i_note
; /* Elf note, internal form */
3466 char *buf
= NULL
; /* Entire note segment contents */
3467 char *namedata
; /* Name portion of the note */
3468 char *descdata
; /* Descriptor portion of the note */
3469 char *sectname
; /* Name to use for new section */
3470 long filepos
; /* File offset to descriptor data */
3473 if (hdr
->p_filesz
> 0
3474 && (buf
= (char *) malloc (hdr
->p_filesz
)) != NULL
3475 && bfd_seek (abfd
, hdr
->p_offset
, SEEK_SET
) != -1
3476 && bfd_read ((PTR
) buf
, hdr
->p_filesz
, 1, abfd
) == hdr
->p_filesz
)
3478 x_note_p
= (Elf_External_Note
*) buf
;
3479 while ((char *) x_note_p
< (buf
+ hdr
->p_filesz
))
3481 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->namesz
);
3482 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->descsz
);
3483 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->type
);
3484 namedata
= x_note_p
->name
;
3485 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
3486 filepos
= hdr
->p_offset
+ (descdata
- buf
);
3487 switch (i_note
.type
)
3490 /* process descdata as prstatus info */
3491 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
3492 sectname
= ".prstatus";
3495 /* process descdata as fpregset info */
3496 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
3497 sectname
= ".fpregset";
3500 /* process descdata as prpsinfo */
3501 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
3502 sectname
= ".prpsinfo";
3505 /* Unknown descriptor, just ignore it. */
3509 if (sectname
!= NULL
)
3511 newsect
= bfd_make_section (abfd
, sectname
);
3512 newsect
->_raw_size
= i_note
.descsz
;
3513 newsect
->filepos
= filepos
;
3514 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3515 newsect
->alignment_power
= 2;
3517 x_note_p
= (Elf_External_Note
*)
3518 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
3525 else if (hdr
->p_filesz
> 0)
3527 bfd_set_error (bfd_error_no_memory
);
3534 /* Core files are simply standard ELF formatted files that partition
3535 the file using the execution view of the file (program header table)
3536 rather than the linking view. In fact, there is no section header
3537 table in a core file.
3539 The process status information (including the contents of the general
3540 register set) and the floating point register set are stored in a
3541 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3542 that allow standard bfd access to the general registers (.reg) and the
3543 floating point registers (.reg2).
3548 DEFUN (elf_core_file_p
, (abfd
), bfd
* abfd
)
3550 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
3551 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3552 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
3553 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
3554 unsigned int phindex
;
3555 struct elf_backend_data
*ebd
;
3557 /* Read in the ELF header in external format. */
3559 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
3561 bfd_set_error (bfd_error_system_call
);
3565 /* Now check to see if we have a valid ELF file, and one that BFD can
3566 make use of. The magic number must match, the address size ('class')
3567 and byte-swapping must match our XVEC entry, and it must have a
3568 program header table (FIXME: See comments re segments at top of this
3571 if (elf_file_p (&x_ehdr
) == false)
3574 bfd_set_error (bfd_error_wrong_format
);
3578 /* FIXME, Check EI_VERSION here ! */
3582 int desired_address_size
= ELFCLASS32
;
3585 int desired_address_size
= ELFCLASS64
;
3588 if (x_ehdr
.e_ident
[EI_CLASS
] != desired_address_size
)
3592 /* Switch xvec to match the specified byte order. */
3593 switch (x_ehdr
.e_ident
[EI_DATA
])
3595 case ELFDATA2MSB
: /* Big-endian */
3596 if (abfd
->xvec
->byteorder_big_p
== false)
3599 case ELFDATA2LSB
: /* Little-endian */
3600 if (abfd
->xvec
->byteorder_big_p
== true)
3603 case ELFDATANONE
: /* No data encoding specified */
3604 default: /* Unknown data encoding specified */
3608 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3609 the tdata pointer in the bfd. */
3612 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
3613 if (elf_tdata (abfd
) == NULL
)
3615 bfd_set_error (bfd_error_no_memory
);
3619 /* FIXME, `wrong' returns from this point onward, leak memory. */
3621 /* Now that we know the byte order, swap in the rest of the header */
3622 i_ehdrp
= elf_elfheader (abfd
);
3623 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
3625 elf_debug_file (i_ehdrp
);
3628 ebd
= get_elf_backend_data (abfd
);
3630 /* Check that the ELF e_machine field matches what this particular
3631 BFD format expects. */
3632 if (ebd
->elf_machine_code
!= i_ehdrp
->e_machine
)
3634 bfd_target
**target_ptr
;
3636 if (ebd
->elf_machine_code
!= EM_NONE
)
3639 /* This is the generic ELF target. Let it match any ELF target
3640 for which we do not have a specific backend. */
3641 for (target_ptr
= bfd_target_vector
; *target_ptr
!= NULL
; target_ptr
++)
3643 struct elf_backend_data
*back
;
3645 if ((*target_ptr
)->flavour
!= bfd_target_elf_flavour
)
3647 back
= (struct elf_backend_data
*) (*target_ptr
)->backend_data
;
3648 if (back
->elf_machine_code
== i_ehdrp
->e_machine
)
3650 /* target_ptr is an ELF backend which matches this
3651 object file, so reject the generic ELF target. */
3657 /* If there is no program header, or the type is not a core file, then
3659 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
3662 /* Allocate space for a copy of the program header table in
3663 internal form, seek to the program header table in the file,
3664 read it in, and convert it to internal form. As a simple sanity
3665 check, verify that the what BFD thinks is the size of each program
3666 header table entry actually matches the size recorded in the file. */
3668 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
3670 i_phdrp
= (Elf_Internal_Phdr
*)
3671 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
3674 bfd_set_error (bfd_error_no_memory
);
3677 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
3679 bfd_set_error (bfd_error_system_call
);
3682 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3684 if (bfd_read ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
3687 bfd_set_error (bfd_error_system_call
);
3690 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
3693 /* Once all of the program headers have been read and converted, we
3694 can start processing them. */
3696 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3698 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
3699 if ((i_phdrp
+ phindex
)->p_type
== PT_NOTE
)
3701 elf_corefile_note (abfd
, i_phdrp
+ phindex
);
3705 /* Remember the entry point specified in the ELF file header. */
3707 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;