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
;
120 /* Forward declarations of static functions */
122 static struct sec
*section_from_elf_index
PARAMS ((bfd
*, unsigned int));
124 static int elf_section_from_bfd_section
PARAMS ((bfd
*, struct sec
*));
126 static boolean elf_slurp_symbol_table
PARAMS ((bfd
*, asymbol
**));
128 static int elf_symbol_from_bfd_symbol
PARAMS ((bfd
*,
129 struct symbol_cache_entry
**));
131 static boolean elf_map_symbols
PARAMS ((bfd
*));
132 static boolean swap_out_syms
PARAMS ((bfd
*));
135 static void elf_debug_section
PARAMS ((char *, int, Elf_Internal_Shdr
*));
136 static void elf_debug_file
PARAMS ((Elf_Internal_Ehdr
*));
139 #define elf_string_from_elf_strtab(abfd,strindex) \
140 elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
143 /* Structure swapping routines */
145 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
146 can be handled by explicitly specifying 32 bits or "the long type". */
148 #define put_word bfd_h_put_64
149 #define get_word bfd_h_get_64
152 #define put_word bfd_h_put_32
153 #define get_word bfd_h_get_32
156 /* Translate an ELF symbol in external format into an ELF symbol in internal
160 elf_swap_symbol_in (abfd
, src
, dst
)
162 Elf_External_Sym
*src
;
163 Elf_Internal_Sym
*dst
;
165 dst
->st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->st_name
);
166 dst
->st_value
= get_word (abfd
, (bfd_byte
*) src
->st_value
);
167 dst
->st_size
= get_word (abfd
, (bfd_byte
*) src
->st_size
);
168 dst
->st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_info
);
169 dst
->st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_other
);
170 dst
->st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->st_shndx
);
173 /* Translate an ELF symbol in internal format into an ELF symbol in external
177 elf_swap_symbol_out (abfd
, src
, dst
)
179 Elf_Internal_Sym
*src
;
180 Elf_External_Sym
*dst
;
182 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
183 put_word (abfd
, src
->st_value
, dst
->st_value
);
184 put_word (abfd
, src
->st_size
, dst
->st_size
);
185 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
186 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
187 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
191 /* Translate an ELF file header in external format into an ELF file header in
195 elf_swap_ehdr_in (abfd
, src
, dst
)
197 Elf_External_Ehdr
*src
;
198 Elf_Internal_Ehdr
*dst
;
200 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
201 dst
->e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_type
);
202 dst
->e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_machine
);
203 dst
->e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_version
);
204 dst
->e_entry
= get_word (abfd
, (bfd_byte
*) src
->e_entry
);
205 dst
->e_phoff
= get_word (abfd
, (bfd_byte
*) src
->e_phoff
);
206 dst
->e_shoff
= get_word (abfd
, (bfd_byte
*) src
->e_shoff
);
207 dst
->e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_flags
);
208 dst
->e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_ehsize
);
209 dst
->e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phentsize
);
210 dst
->e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phnum
);
211 dst
->e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shentsize
);
212 dst
->e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shnum
);
213 dst
->e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shstrndx
);
216 /* Translate an ELF file header in internal format into an ELF file header in
220 elf_swap_ehdr_out (abfd
, src
, dst
)
222 Elf_Internal_Ehdr
*src
;
223 Elf_External_Ehdr
*dst
;
225 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
226 /* note that all elements of dst are *arrays of unsigned char* already... */
227 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
228 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
229 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
230 put_word (abfd
, src
->e_entry
, dst
->e_entry
);
231 put_word (abfd
, src
->e_phoff
, dst
->e_phoff
);
232 put_word (abfd
, src
->e_shoff
, dst
->e_shoff
);
233 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
234 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
235 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
236 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
237 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
238 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
239 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
243 /* Translate an ELF section header table entry in external format into an
244 ELF section header table entry in internal format. */
247 elf_swap_shdr_in (abfd
, src
, dst
)
249 Elf_External_Shdr
*src
;
250 Elf_Internal_Shdr
*dst
;
252 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
253 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
254 dst
->sh_flags
= get_word (abfd
, (bfd_byte
*) src
->sh_flags
);
255 dst
->sh_addr
= get_word (abfd
, (bfd_byte
*) src
->sh_addr
);
256 dst
->sh_offset
= get_word (abfd
, (bfd_byte
*) src
->sh_offset
);
257 dst
->sh_size
= get_word (abfd
, (bfd_byte
*) src
->sh_size
);
258 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
259 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
260 dst
->sh_addralign
= get_word (abfd
, (bfd_byte
*) src
->sh_addralign
);
261 dst
->sh_entsize
= get_word (abfd
, (bfd_byte
*) src
->sh_entsize
);
262 /* we haven't done any processing on it yet, so... */
263 dst
->rawdata
= (void *) 0;
266 /* Translate an ELF section header table entry in internal format into an
267 ELF section header table entry in external format. */
270 elf_swap_shdr_out (abfd
, src
, dst
)
272 Elf_Internal_Shdr
*src
;
273 Elf_External_Shdr
*dst
;
275 /* note that all elements of dst are *arrays of unsigned char* already... */
276 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
277 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
278 put_word (abfd
, src
->sh_flags
, dst
->sh_flags
);
279 put_word (abfd
, src
->sh_addr
, dst
->sh_addr
);
280 put_word (abfd
, src
->sh_offset
, dst
->sh_offset
);
281 put_word (abfd
, src
->sh_size
, dst
->sh_size
);
282 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
283 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
284 put_word (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
285 put_word (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
289 /* Translate an ELF program header table entry in external format into an
290 ELF program header table entry in internal format. */
293 elf_swap_phdr_in (abfd
, src
, dst
)
295 Elf_External_Phdr
*src
;
296 Elf_Internal_Phdr
*dst
;
298 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
299 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
300 dst
->p_offset
= get_word (abfd
, (bfd_byte
*) src
->p_offset
);
301 dst
->p_vaddr
= get_word (abfd
, (bfd_byte
*) src
->p_vaddr
);
302 dst
->p_paddr
= get_word (abfd
, (bfd_byte
*) src
->p_paddr
);
303 dst
->p_filesz
= get_word (abfd
, (bfd_byte
*) src
->p_filesz
);
304 dst
->p_memsz
= get_word (abfd
, (bfd_byte
*) src
->p_memsz
);
305 dst
->p_align
= get_word (abfd
, (bfd_byte
*) src
->p_align
);
309 elf_swap_phdr_out (abfd
, src
, dst
)
311 Elf_Internal_Phdr
*src
;
312 Elf_External_Phdr
*dst
;
314 /* note that all elements of dst are *arrays of unsigned char* already... */
315 bfd_h_put_32 (abfd
, src
->p_type
, dst
->p_type
);
316 put_word (abfd
, src
->p_offset
, dst
->p_offset
);
317 put_word (abfd
, src
->p_vaddr
, dst
->p_vaddr
);
318 put_word (abfd
, src
->p_paddr
, dst
->p_paddr
);
319 put_word (abfd
, src
->p_filesz
, dst
->p_filesz
);
320 put_word (abfd
, src
->p_memsz
, dst
->p_memsz
);
321 bfd_h_put_32 (abfd
, src
->p_flags
, dst
->p_flags
);
322 put_word (abfd
, src
->p_align
, dst
->p_align
);
325 /* Translate an ELF reloc from external format to internal format. */
327 elf_swap_reloc_in (abfd
, src
, dst
)
329 Elf_External_Rel
*src
;
330 Elf_Internal_Rel
*dst
;
332 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
333 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
337 elf_swap_reloca_in (abfd
, src
, dst
)
339 Elf_External_Rela
*src
;
340 Elf_Internal_Rela
*dst
;
342 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
343 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
344 dst
->r_addend
= get_word (abfd
, (bfd_byte
*) src
->r_addend
);
347 /* Translate an ELF reloc from internal format to external format. */
349 elf_swap_reloc_out (abfd
, src
, dst
)
351 Elf_Internal_Rel
*src
;
352 Elf_External_Rel
*dst
;
354 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
355 put_word (abfd
, src
->r_info
, dst
->r_info
);
359 elf_swap_reloca_out (abfd
, src
, dst
)
361 Elf_Internal_Rela
*src
;
362 Elf_External_Rela
*dst
;
364 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
365 put_word (abfd
, src
->r_info
, dst
->r_info
);
366 put_word (abfd
, src
->r_addend
, dst
->r_addend
);
371 /* String table creation/manipulation routines */
373 static struct strtab
*
374 bfd_new_strtab (abfd
)
379 ss
= (struct strtab
*) malloc (sizeof (struct strtab
));
382 bfd_set_error (bfd_error_no_memory
);
385 ss
->tab
= malloc (1);
388 bfd_set_error (bfd_error_no_memory
);
399 bfd_add_to_strtab (abfd
, ss
, str
)
404 /* should search first, but for now: */
405 /* include the trailing NUL */
406 int ln
= strlen (str
) + 1;
408 /* should this be using obstacks? */
409 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
411 BFD_ASSERT (ss
->tab
!= 0); /* FIXME */
412 strcpy (ss
->tab
+ ss
->length
, str
);
416 return ss
->length
- ln
;
420 bfd_add_2_to_strtab (abfd
, ss
, str
, str2
)
426 /* should search first, but for now: */
427 /* include the trailing NUL */
428 int ln
= strlen (str
) + strlen (str2
) + 1;
430 /* should this be using obstacks? */
432 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
434 ss
->tab
= malloc (ln
);
436 BFD_ASSERT (ss
->tab
!= 0); /* FIXME */
437 strcpy (ss
->tab
+ ss
->length
, str
);
438 strcpy (ss
->tab
+ ss
->length
+ strlen (str
), str2
);
442 return ss
->length
- ln
;
446 /* ELF .o/exec file reading */
448 /* Create a new bfd section from an ELF section header. */
451 bfd_section_from_shdr (abfd
, shindex
)
453 unsigned int shindex
;
455 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
456 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
460 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
462 switch (hdr
->sh_type
)
466 /* inactive section. Throw it away. */
471 /* Bits that get saved. This one is real. */
474 newsect
= bfd_make_section (abfd
, name
);
477 newsect
->filepos
= hdr
->sh_offset
; /* so we can read back the bits */
478 newsect
->flags
|= SEC_HAS_CONTENTS
;
479 newsect
->vma
= hdr
->sh_addr
;
480 newsect
->_raw_size
= hdr
->sh_size
;
481 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
483 if (hdr
->sh_flags
& SHF_ALLOC
)
485 newsect
->flags
|= SEC_ALLOC
;
486 newsect
->flags
|= SEC_LOAD
;
489 if (!(hdr
->sh_flags
& SHF_WRITE
))
490 newsect
->flags
|= SEC_READONLY
;
492 if (hdr
->sh_flags
& SHF_EXECINSTR
)
493 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
494 else if (newsect
->flags
& SEC_ALLOC
)
495 newsect
->flags
|= SEC_DATA
;
497 /* The debugging sections appear to recognized only by
499 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
500 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
501 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
502 newsect
->flags
|= SEC_DEBUGGING
;
504 hdr
->rawdata
= (void *) newsect
;
507 hdr
->rawdata
= (void *) bfd_get_section_by_name (abfd
, name
);
512 /* Bits that get saved. This one is real. */
515 newsect
= bfd_make_section (abfd
, name
);
518 newsect
->vma
= hdr
->sh_addr
;
519 newsect
->_raw_size
= hdr
->sh_size
;
520 newsect
->filepos
= hdr
->sh_offset
; /* fake */
521 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
522 if (hdr
->sh_flags
& SHF_ALLOC
)
523 newsect
->flags
|= SEC_ALLOC
;
525 if (!(hdr
->sh_flags
& SHF_WRITE
))
526 newsect
->flags
|= SEC_READONLY
;
528 /* FIXME: This section is empty. Does it really make
529 sense to set SEC_CODE for it? */
530 if (hdr
->sh_flags
& SHF_EXECINSTR
)
531 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
533 hdr
->rawdata
= (void *) newsect
;
538 case SHT_SYMTAB
: /* A symbol table */
539 if (elf_onesymtab (abfd
) == shindex
)
542 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
543 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
544 elf_onesymtab (abfd
) = shindex
;
545 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
546 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_hdr
;
547 abfd
->flags
|= HAS_SYMS
;
550 case SHT_STRTAB
: /* A string table */
553 if (ehdr
->e_shstrndx
== shindex
)
555 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
556 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
557 hdr
->rawdata
= (PTR
) & elf_tdata (abfd
)->shstrtab_hdr
;
563 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
565 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
566 if (hdr2
->sh_link
== shindex
)
568 bfd_section_from_shdr (abfd
, i
);
569 if (elf_onesymtab (abfd
) == i
)
571 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
572 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
575 #if 0 /* Not handling other string tables specially right now. */
576 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
577 /* We have a strtab for some random other section. */
578 newsect
= (asection
*) hdr2
->rawdata
;
581 hdr
->rawdata
= (PTR
) newsect
;
582 hdr2
= &elf_section_data (newsect
)->str_hdr
;
584 elf_elfsections (abfd
)[shindex
] = hdr2
;
590 newsect
= bfd_make_section (abfd
, name
);
593 newsect
->flags
= SEC_HAS_CONTENTS
;
594 hdr
->rawdata
= (PTR
) newsect
;
595 newsect
->_raw_size
= hdr
->sh_size
;
596 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
597 newsect
->vma
= hdr
->sh_addr
;
598 newsect
->filepos
= hdr
->sh_offset
;
600 if (hdr
->sh_flags
& SHF_ALLOC
)
601 newsect
->flags
|= SEC_ALLOC
| SEC_LOAD
;
602 if (!(hdr
->sh_flags
& SHF_WRITE
))
603 newsect
->flags
|= SEC_READONLY
;
604 if (hdr
->sh_flags
& SHF_EXECINSTR
)
605 newsect
->flags
|= SEC_CODE
;
606 else if (newsect
->flags
& SEC_ALLOC
)
607 newsect
->flags
|= SEC_DATA
;
609 /* Check for debugging string tables. */
610 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
611 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
612 newsect
->flags
|= SEC_DEBUGGING
;
619 /* *These* do a lot of work -- but build no sections!
620 The spec says there can be multiple strtabs, but only one symtab,
621 but there can be lots of REL* sections. */
622 /* FIXME: The above statement is wrong! There are typically at least
623 two symbol tables in a dynamically linked executable, ".dynsym"
624 which is the dynamic linkage symbol table and ".symtab", which is
625 the "traditional" symbol table. -fnf */
628 asection
*target_sect
;
629 Elf_Internal_Shdr
*hdr2
;
630 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
632 /* Don't allow REL relocations on a machine that uses RELA and
634 /* @@ Actually, the generic ABI does suggest that both might be
635 used in one file. But the four ABI Processor Supplements I
636 have access to right now all specify that only one is used on
637 each of those architectures. It's conceivable that, e.g., a
638 bunch of absolute 32-bit relocs might be more compact in REL
639 form even on a RELA machine... */
640 BFD_ASSERT (!(use_rela_p
&& (hdr
->sh_type
== SHT_REL
)));
641 BFD_ASSERT (!(!use_rela_p
&& (hdr
->sh_type
== SHT_RELA
)));
642 BFD_ASSERT (hdr
->sh_entsize
==
644 ? sizeof (Elf_External_Rela
)
645 : sizeof (Elf_External_Rel
)));
647 bfd_section_from_shdr (abfd
, hdr
->sh_info
); /* target */
648 bfd_section_from_shdr (abfd
, hdr
->sh_link
); /* symbol table */
649 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
650 if (target_sect
== NULL
651 || elf_section_data (target_sect
) == NULL
)
654 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
656 elf_elfsections (abfd
)[shindex
] = hdr2
;
657 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
658 target_sect
->flags
|= SEC_RELOC
;
659 target_sect
->relocation
= 0;
660 target_sect
->rel_filepos
= hdr
->sh_offset
;
661 abfd
->flags
|= HAS_RELOC
;
667 case SHT_DYNSYM
: /* could treat this like symtab... */
669 fprintf (stderr
, "Dynamic Linking sections not yet supported.\n");
676 fprintf (stderr
, "Note Sections not yet supported.\n");
683 fprintf (stderr
, "SHLIB Sections not supported (and non conforming.)\n");
688 /* Check for any processor-specific section types. */
690 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
692 if (bed
->elf_backend_section_from_shdr
)
693 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
702 elf_new_section_hook (abfd
, sec
)
707 struct bfd_elf_section_data
*sdata
;
709 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
712 bfd_set_error (bfd_error_no_memory
);
715 sec
->used_by_bfd
= (PTR
) sdata
;
716 memset (sdata
, 0, sizeof (*sdata
));
720 /* Create a new bfd section from an ELF program header.
722 Since program segments have no names, we generate a synthetic name
723 of the form segment<NUM>, where NUM is generally the index in the
724 program header table. For segments that are split (see below) we
725 generate the names segment<NUM>a and segment<NUM>b.
727 Note that some program segments may have a file size that is different than
728 (less than) the memory size. All this means is that at execution the
729 system must allocate the amount of memory specified by the memory size,
730 but only initialize it with the first "file size" bytes read from the
731 file. This would occur for example, with program segments consisting
732 of combined data+bss.
734 To handle the above situation, this routine generates TWO bfd sections
735 for the single program segment. The first has the length specified by
736 the file size of the segment, and the second has the length specified
737 by the difference between the two sizes. In effect, the segment is split
738 into it's initialized and uninitialized parts.
743 bfd_section_from_phdr (abfd
, hdr
, index
)
745 Elf_Internal_Phdr
*hdr
;
753 split
= ((hdr
->p_memsz
> 0) &&
754 (hdr
->p_filesz
> 0) &&
755 (hdr
->p_memsz
> hdr
->p_filesz
));
756 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
757 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
760 bfd_set_error (bfd_error_no_memory
);
763 strcpy (name
, namebuf
);
764 newsect
= bfd_make_section (abfd
, name
);
765 newsect
->vma
= hdr
->p_vaddr
;
766 newsect
->_raw_size
= hdr
->p_filesz
;
767 newsect
->filepos
= hdr
->p_offset
;
768 newsect
->flags
|= SEC_HAS_CONTENTS
;
769 if (hdr
->p_type
== PT_LOAD
)
771 newsect
->flags
|= SEC_ALLOC
;
772 newsect
->flags
|= SEC_LOAD
;
773 if (hdr
->p_flags
& PF_X
)
775 /* FIXME: all we known is that it has execute PERMISSION,
777 newsect
->flags
|= SEC_CODE
;
780 if (!(hdr
->p_flags
& PF_W
))
782 newsect
->flags
|= SEC_READONLY
;
787 sprintf (namebuf
, "segment%db", index
);
788 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
791 bfd_set_error (bfd_error_no_memory
);
794 strcpy (name
, namebuf
);
795 newsect
= bfd_make_section (abfd
, name
);
796 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
797 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
798 if (hdr
->p_type
== PT_LOAD
)
800 newsect
->flags
|= SEC_ALLOC
;
801 if (hdr
->p_flags
& PF_X
)
802 newsect
->flags
|= SEC_CODE
;
804 if (!(hdr
->p_flags
& PF_W
))
805 newsect
->flags
|= SEC_READONLY
;
811 /* Begin processing a given object.
813 First we validate the file by reading in the ELF header and checking
816 static INLINE boolean
818 Elf_External_Ehdr
*x_ehdrp
;
820 return ((x_ehdrp
->e_ident
[EI_MAG0
] == ELFMAG0
)
821 && (x_ehdrp
->e_ident
[EI_MAG1
] == ELFMAG1
)
822 && (x_ehdrp
->e_ident
[EI_MAG2
] == ELFMAG2
)
823 && (x_ehdrp
->e_ident
[EI_MAG3
] == ELFMAG3
));
826 /* Check to see if the file associated with ABFD matches the target vector
829 Note that we may be called several times with the same ABFD, but different
830 target vectors, most of which will not match. We have to avoid leaving
831 any side effects in ABFD, or any data it points to (like tdata), if the
832 file does not match the target vector.
834 FIXME: There is memory leak if we are called more than once with the same
835 ABFD, and that bfd already has tdata allocated, since we allocate more tdata
836 and the old tdata is orphaned. Since it's in the bfd obstack, there isn't
837 much we can do about this except possibly rewrite the code. There are
838 also other bfd_allocs that may be the source of memory leaks as well. */
844 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
845 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
846 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
847 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
848 unsigned int shindex
;
849 char *shstrtab
; /* Internal copy of section header stringtab */
850 struct elf_backend_data
*ebd
;
851 struct elf_obj_tdata
*preserved_tdata
= elf_tdata (abfd
);
853 /* Read in the ELF header in external format. */
855 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
857 if (bfd_get_error () != bfd_error_system_call
)
858 goto got_wrong_format_error
;
863 /* Now check to see if we have a valid ELF file, and one that BFD can
864 make use of. The magic number must match, the address size ('class')
865 and byte-swapping must match our XVEC entry, and it must have a
866 section header table (FIXME: See comments re sections at top of this
869 if ((elf_file_p (&x_ehdr
) == false) ||
870 (x_ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) ||
871 (x_ehdr
.e_ident
[EI_CLASS
] != ELFCLASS
))
872 goto got_wrong_format_error
;
874 /* Check that file's byte order matches xvec's */
875 switch (x_ehdr
.e_ident
[EI_DATA
])
877 case ELFDATA2MSB
: /* Big-endian */
878 if (!abfd
->xvec
->header_byteorder_big_p
)
879 goto got_wrong_format_error
;
881 case ELFDATA2LSB
: /* Little-endian */
882 if (abfd
->xvec
->header_byteorder_big_p
)
883 goto got_wrong_format_error
;
885 case ELFDATANONE
: /* No data encoding specified */
886 default: /* Unknown data encoding specified */
887 goto got_wrong_format_error
;
890 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
891 the tdata pointer in the bfd. FIXME: memory leak, see above. */
894 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
895 if (elf_tdata (abfd
) == NULL
)
896 goto got_no_memory_error
;
898 /* Now that we know the byte order, swap in the rest of the header */
899 i_ehdrp
= elf_elfheader (abfd
);
900 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
902 elf_debug_file (i_ehdrp
);
905 /* If there is no section header table, we're hosed. */
906 if (i_ehdrp
->e_shoff
== 0)
907 goto got_wrong_format_error
;
909 /* As a simple sanity check, verify that the what BFD thinks is the
910 size of each section header table entry actually matches the size
911 recorded in the file. */
912 if (i_ehdrp
->e_shentsize
!= sizeof (x_shdr
))
913 goto got_wrong_format_error
;
915 ebd
= get_elf_backend_data (abfd
);
917 /* Check that the ELF e_machine field matches what this particular
918 BFD format expects. */
919 if (ebd
->elf_machine_code
!= i_ehdrp
->e_machine
)
921 bfd_target
**target_ptr
;
923 if (ebd
->elf_machine_code
!= EM_NONE
)
924 goto got_wrong_format_error
;
926 /* This is the generic ELF target. Let it match any ELF target
927 for which we do not have a specific backend. */
928 for (target_ptr
= bfd_target_vector
; *target_ptr
!= NULL
; target_ptr
++)
930 struct elf_backend_data
*back
;
932 if ((*target_ptr
)->flavour
!= bfd_target_elf_flavour
)
934 back
= (struct elf_backend_data
*) (*target_ptr
)->backend_data
;
935 if (back
->elf_machine_code
== i_ehdrp
->e_machine
)
937 /* target_ptr is an ELF backend which matches this
938 object file, so reject the generic ELF target. */
939 goto got_wrong_format_error
;
945 /* Set the flags and architecture before calling the backend so that
946 it can override them. */
947 if (i_ehdrp
->e_type
== ET_EXEC
)
948 abfd
->flags
|= EXEC_P
;
949 else if (i_ehdrp
->e_type
== ET_DYN
)
950 abfd
->flags
|= DYNAMIC
;
952 bfd_default_set_arch_mach (abfd
, ebd
->arch
, 0);
954 /* Remember the entry point specified in the ELF file header. */
955 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
957 /* Let the backend double check the format and override global
959 if (ebd
->elf_backend_object_p
)
961 if ((*ebd
->elf_backend_object_p
) (abfd
) == false)
962 goto got_wrong_format_error
;
965 /* Allocate space for a copy of the section header table in
966 internal form, seek to the section header table in the file,
967 read it in, and convert it to internal form. */
968 i_shdrp
= (Elf_Internal_Shdr
*)
969 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
);
970 elf_elfsections (abfd
) =
971 (Elf_Internal_Shdr
**) bfd_alloc (abfd
, sizeof (i_shdrp
) * i_ehdrp
->e_shnum
);
972 if (!i_shdrp
|| !elf_elfsections (abfd
))
973 goto got_no_memory_error
;
974 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) == -1)
976 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
978 if (bfd_read ((PTR
) & x_shdr
, sizeof x_shdr
, 1, abfd
) != sizeof (x_shdr
))
980 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
981 elf_elfsections (abfd
)[shindex
] = i_shdrp
+ shindex
;
983 /* If this is a .dynamic section, mark the object file as being
984 dynamically linked. */
985 if (i_shdrp
[shindex
].sh_type
== SHT_DYNAMIC
)
986 abfd
->flags
|= DYNAMIC
;
988 if (i_ehdrp
->e_shstrndx
)
990 bfd_section_from_shdr (abfd
, i_ehdrp
->e_shstrndx
);
993 /* Read in the string table containing the names of the sections. We
994 will need the base pointer to this table later. */
995 /* We read this inline now, so that we don't have to go through
996 bfd_section_from_shdr with it (since this particular strtab is
997 used to find all of the ELF section names.) */
999 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
1001 goto got_wrong_format_error
;
1003 /* Once all of the section headers have been read and converted, we
1004 can start processing them. Note that the first section header is
1005 a dummy placeholder entry, so we ignore it.
1007 We also watch for the symbol table section and remember the file
1008 offset and section size for both the symbol table section and the
1009 associated string table section. */
1011 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1013 bfd_section_from_shdr (abfd
, shindex
);
1016 return (abfd
->xvec
);
1018 /* If we are going to use goto's to avoid duplicating error setting
1019 and return(NULL) code, then this at least makes it more maintainable. */
1021 got_wrong_format_error
:
1022 bfd_set_error (bfd_error_wrong_format
);
1024 got_no_memory_error
:
1025 bfd_set_error (bfd_error_no_memory
);
1028 elf_tdata (abfd
) = preserved_tdata
;
1033 /* ELF .o/exec file writing */
1035 /* Takes a bfd and a symbol, returns a pointer to the elf specific area
1036 of the symbol if there is one. */
1037 static INLINE elf_symbol_type
*
1038 elf_symbol_from (ignore_abfd
, symbol
)
1042 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
1045 if (symbol
->the_bfd
->tdata
.elf_obj_data
== (struct elf_obj_tdata
*) NULL
)
1048 return (elf_symbol_type
*) symbol
;
1051 /* Create ELF output from BFD sections.
1053 Essentially, just create the section header and forget about the program
1057 elf_make_sections (abfd
, asect
, obj
)
1062 /* most of what is in bfd_shdr_from_section goes in here... */
1063 /* and all of these sections generate at *least* one ELF section. */
1064 Elf_Internal_Shdr
*this_hdr
;
1065 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1067 this_hdr
->sh_addr
= asect
->vma
;
1068 this_hdr
->sh_size
= asect
->_raw_size
;
1069 /* contents already set by elf_set_section_contents */
1071 if (asect
->flags
& SEC_RELOC
)
1073 /* emit a reloc section, and thus strtab and symtab... */
1074 Elf_Internal_Shdr
*rela_hdr
;
1075 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1077 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1079 /* orelocation has the data, reloc_count has the count... */
1082 rela_hdr
->sh_type
= SHT_RELA
;
1083 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1086 /* REL relocations */
1088 rela_hdr
->sh_type
= SHT_REL
;
1089 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rel
);
1091 rela_hdr
->sh_flags
= 0;
1092 rela_hdr
->sh_addr
= 0;
1093 rela_hdr
->sh_offset
= 0;
1095 /* FIXME: Systems I've checked use an alignment of 4, but it is
1096 possible that some systems use a different alignment. */
1097 rela_hdr
->sh_addralign
= 4;
1101 if (asect
->flags
& SEC_ALLOC
)
1103 this_hdr
->sh_flags
|= SHF_ALLOC
;
1104 if (asect
->flags
& SEC_LOAD
)
1106 /* @@ Do something with sh_type? */
1111 /* If this section is not part of the program image during
1112 execution, leave the address fields at 0. */
1113 this_hdr
->sh_addr
= 0;
1116 if (!(asect
->flags
& SEC_READONLY
))
1117 this_hdr
->sh_flags
|= SHF_WRITE
;
1119 if (asect
->flags
& SEC_CODE
)
1120 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1124 write_relocs (abfd
, sec
, xxx
)
1129 Elf_Internal_Shdr
*rela_hdr
;
1130 Elf_External_Rela
*outbound_relocas
;
1131 Elf_External_Rel
*outbound_relocs
;
1133 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1134 asymbol
*last_sym
= 0;
1135 int last_sym_idx
= 9999999; /* should always be written before use */
1137 if ((sec
->flags
& SEC_RELOC
) == 0)
1139 /* Flags are sometimes inconsistent. */
1140 if (sec
->reloc_count
== 0)
1143 rela_hdr
= &elf_section_data (sec
)->rel_hdr
;
1145 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* sec
->reloc_count
;
1146 rela_hdr
->contents
= (void *) bfd_alloc (abfd
, rela_hdr
->sh_size
);
1147 if (!rela_hdr
->contents
)
1149 bfd_set_error (bfd_error_no_memory
);
1150 abort (); /* FIXME */
1153 /* orelocation has the data, reloc_count has the count... */
1156 outbound_relocas
= (Elf_External_Rela
*) rela_hdr
->contents
;
1158 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1160 Elf_Internal_Rela dst_rela
;
1161 Elf_External_Rela
*src_rela
;
1166 ptr
= sec
->orelocation
[idx
];
1167 src_rela
= outbound_relocas
+ idx
;
1168 if (!(abfd
->flags
& EXEC_P
))
1169 dst_rela
.r_offset
= ptr
->address
- sec
->vma
;
1171 dst_rela
.r_offset
= ptr
->address
;
1173 sym
= *ptr
->sym_ptr_ptr
;
1174 if (sym
== last_sym
)
1179 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1181 dst_rela
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1183 dst_rela
.r_addend
= ptr
->addend
;
1184 elf_swap_reloca_out (abfd
, &dst_rela
, src_rela
);
1188 /* REL relocations */
1190 outbound_relocs
= (Elf_External_Rel
*) rela_hdr
->contents
;
1192 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1194 Elf_Internal_Rel dst_rel
;
1195 Elf_External_Rel
*src_rel
;
1200 ptr
= sec
->orelocation
[idx
];
1201 sym
= *ptr
->sym_ptr_ptr
;
1202 src_rel
= outbound_relocs
+ idx
;
1203 if (!(abfd
->flags
& EXEC_P
))
1204 dst_rel
.r_offset
= ptr
->address
- sec
->vma
;
1206 dst_rel
.r_offset
= ptr
->address
;
1208 if (sym
== last_sym
)
1213 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1215 dst_rel
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1217 elf_swap_reloc_out (abfd
, &dst_rel
, src_rel
);
1223 fix_up_strtabs (abfd
, asect
, obj
)
1228 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (asect
)->this_hdr
;
1229 int this_idx
= elf_section_data (asect
)->this_idx
;
1231 /* @@ Check flags! */
1232 if (!strncmp (asect
->name
, ".stab", 5)
1233 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
1235 size_t len
= strlen (asect
->name
) + 1;
1236 char *s
= (char *) malloc (len
);
1238 /* FIXME: Should deal more gracefully with errors. */
1240 strcpy (s
, asect
->name
);
1242 asect
= bfd_get_section_by_name (abfd
, s
);
1246 elf_section_data (asect
)->this_hdr
.sh_link
= this_idx
;
1247 /* @@ Assuming 32 bits! */
1248 elf_section_data (asect
)->this_hdr
.sh_entsize
= 0xc;
1250 this_hdr
->sh_type
= SHT_STRTAB
;
1255 elf_fake_sections (abfd
, asect
, obj
)
1260 /* most of what is in bfd_shdr_from_section goes in here... */
1261 /* and all of these sections generate at *least* one ELF section. */
1263 Elf_Internal_Shdr
*this_hdr
;
1264 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1266 bfd_add_to_strtab (abfd
, elf_shstrtab (abfd
), asect
->name
);
1267 /* We need to log the type *now* so that elf_section_from_bfd_section
1268 can find us... have to set rawdata too. */
1269 this_hdr
->rawdata
= (void *) asect
;
1270 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1271 if ((asect
->flags
& SEC_ALLOC
) && (asect
->flags
& SEC_LOAD
))
1272 this_hdr
->sh_type
= SHT_PROGBITS
;
1273 else if ((asect
->flags
& SEC_ALLOC
) && ((asect
->flags
& SEC_LOAD
) == 0))
1275 BFD_ASSERT (strcmp (asect
->name
, ".bss") == 0
1276 || strcmp (asect
->name
, ".sbss") == 0);
1277 this_hdr
->sh_type
= SHT_NOBITS
;
1279 /* FIXME I am not sure how to detect a .note section from the flags
1280 word of an `asection'. */
1281 else if (!strcmp (asect
->name
, ".note"))
1282 this_hdr
->sh_type
= SHT_NOTE
;
1284 this_hdr
->sh_type
= SHT_PROGBITS
;
1286 this_hdr
->sh_flags
= 0;
1287 this_hdr
->sh_addr
= 0;
1288 this_hdr
->sh_size
= 0;
1289 this_hdr
->sh_entsize
= 0;
1290 this_hdr
->sh_info
= 0;
1291 this_hdr
->sh_link
= 0;
1292 this_hdr
->sh_offset
= 0;
1295 /* Now, check for processor-specific section types. */
1297 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1299 if (bed
->elf_backend_fake_sections
)
1300 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1304 /* Emit a strtab and symtab, and possibly a reloc section. */
1305 Elf_Internal_Shdr
*rela_hdr
;
1307 /* Note that only one symtab is used, so just remember it
1310 if (asect
->flags
& SEC_RELOC
)
1312 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1314 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1316 bfd_add_2_to_strtab (abfd
, elf_shstrtab (abfd
),
1317 use_rela_p
? ".rela" : ".rel",
1319 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1320 rela_hdr
->sh_entsize
= (use_rela_p
1321 ? sizeof (Elf_External_Rela
)
1322 : sizeof (Elf_External_Rel
));
1324 rela_hdr
->sh_flags
= 0;
1325 rela_hdr
->sh_addr
= 0;
1326 rela_hdr
->sh_size
= 0;
1327 rela_hdr
->sh_offset
= 0;
1329 /* FIXME: Systems I've checked use an alignment of 4, but some
1330 systems may use a different alignment. */
1331 rela_hdr
->sh_addralign
= 4;
1336 if (asect
->flags
& SEC_ALLOC
)
1338 this_hdr
->sh_flags
|= SHF_ALLOC
;
1339 if (asect
->flags
& SEC_LOAD
)
1341 /* @@ Do something with sh_type? */
1344 if (!(asect
->flags
& SEC_READONLY
))
1345 this_hdr
->sh_flags
|= SHF_WRITE
;
1346 if (asect
->flags
& SEC_CODE
)
1347 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1350 /* Map symbol from it's internal number to the external number, moving
1351 all local symbols to be at the head of the list. */
1354 sym_is_global (abfd
, sym
)
1358 /* If the backend has a special mapping, use it. */
1359 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1360 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1363 if (sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
))
1365 if (sym
->flags
& BSF_LOCAL
)
1369 if (sym
->section
== 0)
1371 /* Is this valid? */
1376 if (sym
->section
== &bfd_und_section
)
1378 if (bfd_is_com_section (sym
->section
))
1380 if (sym
->flags
& (BSF_LOCAL
| BSF_SECTION_SYM
| BSF_FILE
))
1386 elf_map_symbols (abfd
)
1389 int symcount
= bfd_get_symcount (abfd
);
1390 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1391 asymbol
**sect_syms
;
1393 int num_globals
= 0;
1394 int num_locals2
= 0;
1395 int num_globals2
= 0;
1397 int num_sections
= 0;
1398 Elf_Sym_Extra
*sym_extra
;
1403 fprintf (stderr
, "elf_map_symbols\n");
1407 /* Add local symbols for each section for which there are relocs.
1408 FIXME: How can we tell which sections have relocs at this point?
1409 Will reloc_count always be accurate? Actually, I think most ELF
1410 targets create section symbols for all sections anyhow. */
1411 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1413 if (max_index
< asect
->index
)
1414 max_index
= asect
->index
;
1418 elf_num_section_syms (abfd
) = max_index
;
1419 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1420 elf_section_syms (abfd
) = sect_syms
;
1424 bfd_set_error (bfd_error_no_memory
);
1428 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1430 asymbol
*sym
= bfd_make_empty_symbol (abfd
);
1433 bfd_set_error (bfd_error_no_memory
);
1436 sym
->the_bfd
= abfd
;
1437 sym
->name
= asect
->name
;
1438 sym
->value
= asect
->vma
;
1439 sym
->flags
= BSF_SECTION_SYM
;
1440 sym
->section
= asect
;
1441 sect_syms
[asect
->index
] = sym
;
1445 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1446 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1453 syms
= (asymbol
**) bfd_realloc (abfd
, syms
,
1454 ((symcount
+ num_sections
+ 1)
1455 * sizeof (asymbol
*)));
1457 syms
= (asymbol
**) bfd_alloc (abfd
,
1458 (num_sections
+ 1) * sizeof (asymbol
*));
1461 bfd_set_error (bfd_error_no_memory
);
1465 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1467 if (sect_syms
[asect
->index
])
1468 syms
[symcount
++] = sect_syms
[asect
->index
];
1471 syms
[symcount
] = (asymbol
*) 0;
1472 bfd_set_symtab (abfd
, syms
, symcount
);
1475 elf_sym_extra (abfd
) = sym_extra
1476 = (Elf_Sym_Extra
*) bfd_alloc (abfd
, symcount
* sizeof (Elf_Sym_Extra
));
1479 bfd_set_error (bfd_error_no_memory
);
1483 /* Identify and classify all of the symbols. */
1484 for (idx
= 0; idx
< symcount
; idx
++)
1486 if (!sym_is_global (abfd
, syms
[idx
]))
1492 /* Now provide mapping information. Add +1 for skipping over the
1494 for (idx
= 0; idx
< symcount
; idx
++)
1496 syms
[idx
]->udata
= (PTR
) & sym_extra
[idx
];
1497 if (!sym_is_global (abfd
, syms
[idx
]))
1498 sym_extra
[idx
].elf_sym_num
= 1 + num_locals2
++;
1500 sym_extra
[idx
].elf_sym_num
= 1 + num_locals
+ num_globals2
++;
1503 elf_num_locals (abfd
) = num_locals
;
1504 elf_num_globals (abfd
) = num_globals
;
1508 static boolean
assign_section_numbers ();
1509 static boolean
assign_file_positions_except_relocs ();
1512 elf_compute_section_file_positions (abfd
)
1515 bfd_map_over_sections (abfd
, elf_fake_sections
, 0);
1517 if (!assign_section_numbers (abfd
))
1520 bfd_map_over_sections (abfd
, elf_make_sections
, 0);
1522 bfd_map_over_sections (abfd
, fix_up_strtabs
, 0); /* .stab/.stabstr &c */
1524 if (swap_out_syms (abfd
) == false)
1527 if (!assign_file_positions_except_relocs (abfd
))
1534 elf_write_phdrs (abfd
, i_ehdrp
, i_phdrp
, phdr_cnt
)
1536 Elf_Internal_Ehdr
*i_ehdrp
;
1537 Elf_Internal_Phdr
*i_phdrp
;
1538 unsigned short phdr_cnt
;
1540 /* first program header entry goes after the file header */
1541 int outbase
= i_ehdrp
->e_phoff
;
1543 Elf_External_Phdr x_phdr
;
1545 for (i
= 0; i
< phdr_cnt
; i
++)
1547 elf_swap_phdr_out (abfd
, i_phdrp
+ i
, &x_phdr
);
1548 bfd_seek (abfd
, outbase
, SEEK_SET
);
1549 bfd_write ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
);
1550 outbase
+= sizeof (x_phdr
);
1556 static const Elf_Internal_Shdr null_shdr
;
1558 /* Assign all ELF section numbers. The dummy first section is handled here
1559 too. The link/info pointers for the standard section types are filled
1560 in here too, while we're at it. (Link pointers for .stab sections are
1561 not filled in here.) */
1563 assign_section_numbers (abfd
)
1566 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1568 int section_number
= 1;
1570 Elf_Internal_Shdr
**i_shdrp
;
1572 t
->shstrtab_hdr
.sh_size
= elf_shstrtab (abfd
)->length
;
1573 t
->shstrtab_hdr
.contents
= (void *) elf_shstrtab (abfd
)->tab
;
1574 shstrtab_length_fixed
= 1;
1576 t
->shstrtab_section
= section_number
++;
1577 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1580 t
->symtab_section
= section_number
++;
1581 t
->strtab_section
= section_number
++;
1582 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1584 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1586 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1587 d
->this_idx
= section_number
++;
1588 if (sec
->flags
& SEC_RELOC
)
1590 d
->rel_idx
= section_number
++;
1591 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1592 d
->rel_hdr
.sh_info
= d
->this_idx
;
1596 /* No handling for per-section string tables currently. */
1598 elf_elfheader (abfd
)->e_shnum
= section_number
;
1600 /* Set up the list of section header pointers, in agreement with the
1602 i_shdrp
= (Elf_Internal_Shdr
**)
1603 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*));
1606 bfd_set_error (bfd_error_no_memory
);
1609 elf_elfsections (abfd
) = i_shdrp
;
1610 for (i
= 0; i
< section_number
; i
++)
1613 i_shdrp
[0] = (Elf_Internal_Shdr
*) & null_shdr
;
1614 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1617 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1618 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1620 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1622 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1623 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1625 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1627 /* Make sure we got everything.... */
1628 for (i
= 0; i
< section_number
; i
++)
1629 if (i_shdrp
[i
] == 0)
1634 static INLINE file_ptr
1635 assign_file_position_for_section (i_shdrp
, offset
)
1636 Elf_Internal_Shdr
*i_shdrp
;
1641 if (i_shdrp
->sh_addralign
!= 0)
1642 align
= i_shdrp
->sh_addralign
;
1645 i_shdrp
->sh_offset
= offset
= BFD_ALIGN (offset
, align
);
1646 if (i_shdrp
->rawdata
!= NULL
)
1647 ((asection
*) i_shdrp
->rawdata
)->filepos
= offset
;
1648 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1649 offset
+= i_shdrp
->sh_size
;
1653 static INLINE file_ptr
1654 align_file_position (off
)
1657 return (off
+ FILE_ALIGN
- 1) & ~(FILE_ALIGN
- 1);
1660 static INLINE file_ptr
1661 assign_file_positions_for_symtab_and_strtabs (abfd
, off
)
1665 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1667 off
= align_file_position (off
);
1668 off
= assign_file_position_for_section (&t
->symtab_hdr
, off
);
1669 off
= assign_file_position_for_section (&t
->shstrtab_hdr
, off
);
1670 off
= assign_file_position_for_section (&t
->strtab_hdr
, off
);
1676 bfd_vma low
, mem_size
;
1680 struct seg_info
*next
;
1684 map_program_segments (abfd
)
1687 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1688 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1689 Elf_Internal_Shdr
*i_shdrp
;
1690 Elf_Internal_Phdr
*phdr
;
1692 unsigned int i
, n_left
= 0;
1693 file_ptr lowest_offset
= 0;
1694 struct seg_info
*seg
= NULL
;
1696 done
= (char *) malloc (i_ehdrp
->e_shnum
);
1697 if (done
== NULL
&& i_ehdrp
->e_shnum
!= 0)
1699 bfd_set_error (bfd_error_no_memory
);
1702 memset (done
, 0, i_ehdrp
->e_shnum
);
1703 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1705 i_shdrp
= i_shdrpp
[i
];
1706 /* If it's going to be mapped in, it's been assigned a position. */
1707 if (i_shdrp
->sh_offset
+ 1 == 0)
1709 /* Well, not really, but we won't process it here. */
1713 if (i_shdrp
->sh_offset
< lowest_offset
1714 || lowest_offset
== 0)
1715 lowest_offset
= i_shdrp
->sh_offset
;
1716 /* Only interested in PROGBITS or NOBITS for generating segments. */
1717 switch (i_shdrp
->sh_type
)
1730 bfd_vma lowest_vma
= -1, high
;
1734 struct seg_info
*snew
;
1735 struct seg_info
**s_ptr
;
1737 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1739 i_shdrp
= i_shdrpp
[i
];
1740 if (!done
[i
] && i_shdrp
->sh_addr
< lowest_vma
)
1742 lowest_vma
= i_shdrp
->sh_addr
;
1748 /* So now we know the lowest vma of any unassigned sections; start
1750 snew
= (struct seg_info
*) bfd_alloc (abfd
, sizeof (struct seg_info
));
1753 bfd_set_error (bfd_error_no_memory
);
1757 while (*s_ptr
!= (struct seg_info
*) NULL
)
1758 s_ptr
= &(*s_ptr
)->next
;
1761 snew
->low
= lowest_vma
;
1762 i_shdrp
= i_shdrpp
[low_sec
];
1763 snew
->start_pos
= i_shdrp
->sh_offset
;
1764 snew
->sh_flags
= i_shdrp
->sh_flags
;
1765 done
[low_sec
] = 1, n_left
--;
1766 mem_size
= i_shdrp
->sh_size
;
1767 high
= lowest_vma
+ i_shdrp
->sh_size
;
1769 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1770 file_size
= i_shdrp
->sh_size
;
1772 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1778 i_shdrp
= i_shdrpp
[i
];
1779 /* position of next byte on disk */
1780 f1
= snew
->start_pos
+ file_size
;
1781 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1783 if (i_shdrp
->sh_offset
- f1
!= i_shdrp
->sh_addr
- high
)
1785 if (file_size
!= mem_size
)
1789 /* sh_type == NOBITS */
1791 /* If the section in question has no contents in the disk
1792 file, we really don't care where it supposedly starts.
1793 But we don't want to bother merging it into this segment
1794 if it doesn't start on this memory page. */
1795 bfd_vma page1
, page2
;
1796 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1798 /* page number in address space of current end of snew */
1799 page1
= (high
- 1 + maxpagesize
- 1) / maxpagesize
;
1800 /* page number in address space of start of this section */
1801 page2
= (i_shdrp
->sh_addr
+ maxpagesize
- 1) / maxpagesize
;
1806 done
[i
] = 1, n_left
--;
1807 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1808 file_size
= i_shdrp
->sh_offset
+ i_shdrp
->sh_size
- snew
->start_pos
;
1809 mem_size
= i_shdrp
->sh_addr
+ i_shdrp
->sh_size
- snew
->low
;
1810 high
= i_shdrp
->sh_addr
+ i_shdrp
->sh_size
;
1813 snew
->file_size
= file_size
;
1814 snew
->mem_size
= mem_size
;
1816 /* Now do something with the list of segments we've built up. */
1818 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1823 for (s
= seg
; s
; s
= s
->next
)
1827 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
1828 sz
= sizeof (Elf_External_Phdr
) * n_segs
;
1829 if (align_file_position (i_ehdrp
->e_ehsize
) + sz
<= lowest_offset
)
1830 i_ehdrp
->e_phoff
= align_file_position (i_ehdrp
->e_ehsize
);
1833 i_ehdrp
->e_phoff
= align_file_position (elf_tdata (abfd
)->next_file_pos
);
1834 elf_tdata (abfd
)->next_file_pos
= i_ehdrp
->e_phoff
+ sz
;
1836 phdr
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
,
1837 n_segs
* sizeof (Elf_Internal_Phdr
));
1840 bfd_set_error (bfd_error_no_memory
);
1841 abort (); /* FIXME */
1843 elf_tdata (abfd
)->phdr
= phdr
;
1846 phdr
->p_type
= PT_LOAD
; /* only type we really support so far */
1847 phdr
->p_offset
= seg
->start_pos
;
1848 phdr
->p_vaddr
= seg
->low
;
1850 phdr
->p_filesz
= seg
->file_size
;
1851 phdr
->p_memsz
= seg
->mem_size
;
1852 phdr
->p_flags
= PF_R
;
1853 phdr
->p_align
= maxpagesize
; /* ? */
1854 if (seg
->sh_flags
& SHF_WRITE
)
1855 /* SysVr4 ELF docs say "data segments normally have read, write,
1856 and execute permissions." */
1857 phdr
->p_flags
|= (PF_W
| PF_X
);
1858 if (seg
->sh_flags
& SHF_EXECINSTR
)
1859 phdr
->p_flags
|= PF_X
;
1863 i_ehdrp
->e_phnum
= n_segs
;
1865 elf_write_phdrs (abfd
, i_ehdrp
, elf_tdata (abfd
)->phdr
, i_ehdrp
->e_phnum
);
1876 assign_file_positions_except_relocs (abfd
)
1879 /* For now, we ignore the possibility of having program segments, which
1880 may require some alignment in the file. That'll require padding, and
1881 some interesting calculations to optimize file space usage.
1883 Also, since the application may change the list of relocations for
1884 a given section, we don't figure them in here. We'll put them at the
1885 end of the file, at positions computed during bfd_close.
1887 The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ...
1888 or: <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */
1890 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1893 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1894 Elf_Internal_Shdr
*i_shdrp
;
1895 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1896 int exec_p
= (abfd
->flags
& EXEC_P
) != 0;
1897 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1899 /* Everything starts after the ELF file header. */
1900 off
= i_ehdrp
->e_ehsize
;
1904 /* Section headers. */
1905 off
= align_file_position (off
);
1906 i_ehdrp
->e_shoff
= off
;
1907 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1908 off
= assign_file_positions_for_symtab_and_strtabs (abfd
, off
);
1910 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1912 /* The symtab and strtab sections are placed by
1913 assign_file_positions_for_symtab_and_strtabs. */
1914 if (i
== t
->symtab_section
1915 || i
== t
->strtab_section
1916 || i
== t
->shstrtab_section
)
1919 i_shdrp
= i_shdrpp
[i
];
1920 if (i_shdrp
->sh_type
== SHT_REL
|| i_shdrp
->sh_type
== SHT_RELA
)
1922 i_shdrp
->sh_offset
= -1;
1927 if (maxpagesize
== 0)
1928 maxpagesize
= 1; /* make the arithmetic work */
1929 /* This isn't necessarily going to give the best packing, if the
1930 segments require padding between them, but since that isn't
1931 usually the case, this'll do. */
1932 if ((i_shdrp
->sh_flags
& SHF_ALLOC
) == 0)
1934 i_shdrp
->sh_offset
= -1;
1937 /* Blindly assume that the segments are ordered optimally. With
1938 the default LD script, they will be. */
1939 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1941 /* need big unsigned type */
1943 addtl_off
= i_shdrp
->sh_addr
- off
;
1944 addtl_off
= addtl_off
% maxpagesize
;
1951 off
= assign_file_position_for_section (i_shdrp
, off
);
1954 && i_shdrp
->sh_type
== SHT_NOBITS
1955 && (i
== i_ehdrp
->e_shnum
1956 || i_shdrpp
[i
+ 1]->sh_type
!= SHT_NOBITS
))
1958 /* Skip to the next page to ensure that when the file is
1959 loaded the bss section is loaded with zeroes. I don't
1960 know if this is required on all platforms, but it
1961 shouldn't really hurt. */
1962 off
= BFD_ALIGN (off
, maxpagesize
);
1966 && get_elf_backend_data (abfd
)->maxpagesize
> 1
1967 && i_shdrp
->sh_type
== SHT_PROGBITS
1968 && (i_shdrp
->sh_flags
& SHF_ALLOC
)
1969 && (i_shdrp
->sh_offset
- i_shdrp
->sh_addr
) % get_elf_backend_data (abfd
)->maxpagesize
!= 0)
1974 elf_tdata (abfd
)->next_file_pos
= off
;
1975 if (!map_program_segments (abfd
))
1977 off
= elf_tdata (abfd
)->next_file_pos
;
1979 /* Section headers. */
1980 off
= align_file_position (off
);
1981 i_ehdrp
->e_shoff
= off
;
1982 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1984 off
= assign_file_positions_for_symtab_and_strtabs (abfd
, off
);
1986 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1988 i_shdrp
= i_shdrpp
[i
];
1989 if (i_shdrp
->sh_offset
+ 1 == 0
1990 && i_shdrp
->sh_type
!= SHT_REL
1991 && i_shdrp
->sh_type
!= SHT_RELA
)
1992 off
= assign_file_position_for_section (i_shdrp
, off
);
1995 elf_tdata (abfd
)->next_file_pos
= off
;
2003 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2004 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2005 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2007 struct strtab
*shstrtab
;
2009 i_ehdrp
= elf_elfheader (abfd
);
2010 i_shdrp
= elf_elfsections (abfd
);
2012 shstrtab
= bfd_new_strtab (abfd
);
2016 elf_shstrtab (abfd
) = shstrtab
;
2018 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
2019 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
2020 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
2021 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
2023 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS
;
2024 i_ehdrp
->e_ident
[EI_DATA
] =
2025 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
2026 i_ehdrp
->e_ident
[EI_VERSION
] = EV_CURRENT
;
2028 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2029 i_ehdrp
->e_ident
[count
] = 0;
2031 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
) ? ET_EXEC
: ET_REL
;
2032 switch (bfd_get_arch (abfd
))
2034 case bfd_arch_unknown
:
2035 i_ehdrp
->e_machine
= EM_NONE
;
2037 case bfd_arch_sparc
:
2038 i_ehdrp
->e_machine
= EM_SPARC
;
2039 /* start-sanitize-v9 */
2041 i_ehdrp
->e_machine
= EM_SPARC64
;
2043 /* end-sanitize-v9 */
2046 i_ehdrp
->e_machine
= EM_386
;
2049 i_ehdrp
->e_machine
= EM_68K
;
2052 i_ehdrp
->e_machine
= EM_88K
;
2055 i_ehdrp
->e_machine
= EM_860
;
2057 case bfd_arch_mips
: /* MIPS Rxxxx */
2058 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
2061 i_ehdrp
->e_machine
= EM_HPPA
;
2063 case bfd_arch_powerpc
:
2064 i_ehdrp
->e_machine
= EM_CYGNUS_POWERPC
;
2066 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2068 i_ehdrp
->e_machine
= EM_NONE
;
2070 i_ehdrp
->e_version
= EV_CURRENT
;
2071 i_ehdrp
->e_ehsize
= sizeof (Elf_External_Ehdr
);
2073 /* no program header, for now. */
2074 i_ehdrp
->e_phoff
= 0;
2075 i_ehdrp
->e_phentsize
= 0;
2076 i_ehdrp
->e_phnum
= 0;
2078 /* each bfd section is section header entry */
2079 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
2080 i_ehdrp
->e_shentsize
= sizeof (Elf_External_Shdr
);
2082 /* if we're building an executable, we'll need a program header table */
2083 if (abfd
->flags
& EXEC_P
)
2085 /* it all happens later */
2087 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
2089 /* elf_build_phdrs() returns a (NULL-terminated) array of
2090 Elf_Internal_Phdrs */
2091 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
2092 i_ehdrp
->e_phoff
= outbase
;
2093 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
2098 i_ehdrp
->e_phentsize
= 0;
2100 i_ehdrp
->e_phoff
= 0;
2103 elf_tdata (abfd
)->symtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2105 elf_tdata (abfd
)->strtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2107 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2113 swap_out_syms (abfd
)
2116 if (!elf_map_symbols (abfd
))
2119 /* Dump out the symtabs. */
2121 int symcount
= bfd_get_symcount (abfd
);
2122 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2123 struct strtab
*stt
= bfd_new_strtab (abfd
);
2124 Elf_Internal_Shdr
*symtab_hdr
;
2125 Elf_Internal_Shdr
*symstrtab_hdr
;
2126 Elf_External_Sym
*outbound_syms
;
2131 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2132 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2133 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
2134 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2135 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2137 /* FIXME: Systems I've checked use 4 byte alignment for .symtab,
2138 but it is possible that there are systems which use a different
2140 symtab_hdr
->sh_addralign
= 4;
2142 /* see assert in elf_fake_sections that supports this: */
2143 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2144 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2146 outbound_syms
= (Elf_External_Sym
*)
2147 bfd_alloc (abfd
, (1 + symcount
) * sizeof (Elf_External_Sym
));
2150 bfd_set_error (bfd_error_no_memory
);
2153 /* now generate the data (for "contents") */
2155 /* Fill in zeroth symbol and swap it out. */
2156 Elf_Internal_Sym sym
;
2162 sym
.st_shndx
= SHN_UNDEF
;
2163 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
2165 for (idx
= 0; idx
< symcount
; idx
++)
2167 Elf_Internal_Sym sym
;
2168 bfd_vma value
= syms
[idx
]->value
;
2170 if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2171 /* Section symbols have no names. */
2174 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
2176 if (bfd_is_com_section (syms
[idx
]->section
))
2178 /* ELF common symbols put the alignment into the `value' field,
2179 and the size into the `size' field. This is backwards from
2180 how BFD handles it, so reverse it here. */
2181 sym
.st_size
= value
;
2182 /* Should retrieve this from somewhere... */
2184 sym
.st_shndx
= elf_section_from_bfd_section (abfd
,
2185 syms
[idx
]->section
);
2189 asection
*sec
= syms
[idx
]->section
;
2190 elf_symbol_type
*type_ptr
;
2193 if (sec
->output_section
)
2195 value
+= sec
->output_offset
;
2196 sec
= sec
->output_section
;
2199 sym
.st_value
= value
;
2200 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2201 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2202 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec
);
2206 /* Writing this would be a hell of a lot easier if we had
2207 some decent documentation on bfd, and knew what to expect
2208 of the library, and what to demand of applications. For
2209 example, it appears that `objcopy' might not set the
2210 section of a symbol to be a section that is actually in
2212 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2213 BFD_ASSERT (sec2
!= 0);
2214 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec2
);
2215 BFD_ASSERT (shndx
!= -1);
2219 if (bfd_is_com_section (syms
[idx
]->section
))
2220 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2221 else if (syms
[idx
]->section
== &bfd_und_section
)
2222 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2223 else if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2224 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2225 else if (syms
[idx
]->flags
& BSF_FILE
)
2226 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2229 int bind
= STB_LOCAL
;
2230 int type
= STT_OBJECT
;
2231 unsigned int flags
= syms
[idx
]->flags
;
2233 if (flags
& BSF_LOCAL
)
2235 else if (flags
& BSF_WEAK
)
2237 else if (flags
& BSF_GLOBAL
)
2240 if (flags
& BSF_FUNCTION
)
2243 sym
.st_info
= ELF_ST_INFO (bind
, type
);
2247 elf_swap_symbol_out (abfd
, &sym
,
2249 + elf_sym_extra (abfd
)[idx
].elf_sym_num
));
2252 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2253 symstrtab_hdr
->contents
= (PTR
) stt
->tab
;
2254 symstrtab_hdr
->sh_size
= stt
->length
;
2255 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2257 symstrtab_hdr
->sh_flags
= 0;
2258 symstrtab_hdr
->sh_addr
= 0;
2259 symstrtab_hdr
->sh_entsize
= 0;
2260 symstrtab_hdr
->sh_link
= 0;
2261 symstrtab_hdr
->sh_info
= 0;
2262 symstrtab_hdr
->sh_addralign
= 1;
2263 symstrtab_hdr
->size
= 0;
2266 /* put the strtab out too... */
2268 Elf_Internal_Shdr
*this_hdr
;
2270 this_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2271 this_hdr
->contents
= (PTR
) elf_shstrtab (abfd
)->tab
;
2272 this_hdr
->sh_size
= elf_shstrtab (abfd
)->length
;
2273 this_hdr
->sh_type
= SHT_STRTAB
;
2274 this_hdr
->sh_flags
= 0;
2275 this_hdr
->sh_addr
= 0;
2276 this_hdr
->sh_entsize
= 0;
2277 this_hdr
->sh_addralign
= 1;
2284 write_shdrs_and_ehdr (abfd
)
2287 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
2288 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2289 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
2290 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2292 struct strtab
*shstrtab
;
2294 i_ehdrp
= elf_elfheader (abfd
);
2295 i_shdrp
= elf_elfsections (abfd
);
2296 shstrtab
= elf_shstrtab (abfd
);
2298 /* swap the header before spitting it out... */
2301 elf_debug_file (i_ehdrp
);
2303 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
2304 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
2305 bfd_write ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
);
2307 /* at this point we've concocted all the ELF sections... */
2308 x_shdrp
= (Elf_External_Shdr
*)
2309 bfd_alloc (abfd
, sizeof (*x_shdrp
) * (i_ehdrp
->e_shnum
));
2312 bfd_set_error (bfd_error_no_memory
);
2316 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2319 elf_debug_section (shstrtab
->tab
+ i_shdrp
[count
]->sh_name
, count
,
2322 elf_swap_shdr_out (abfd
, i_shdrp
[count
], x_shdrp
+ count
);
2324 bfd_seek (abfd
, (file_ptr
) i_ehdrp
->e_shoff
, SEEK_SET
);
2325 bfd_write ((PTR
) x_shdrp
, sizeof (*x_shdrp
), i_ehdrp
->e_shnum
, abfd
);
2326 /* need to dump the string table too... */
2332 assign_file_positions_for_relocs (abfd
)
2335 file_ptr off
= elf_tdata (abfd
)->next_file_pos
;
2337 Elf_Internal_Shdr
**shdrpp
= elf_elfsections (abfd
);
2338 Elf_Internal_Shdr
*shdrp
;
2339 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
2342 if (shdrp
->sh_type
!= SHT_REL
&& shdrp
->sh_type
!= SHT_RELA
)
2344 off
= align_file_position (off
);
2345 off
= assign_file_position_for_section (shdrp
, off
);
2347 elf_tdata (abfd
)->next_file_pos
= off
;
2351 NAME(bfd_elf
,write_object_contents
) (abfd
)
2354 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2355 Elf_Internal_Ehdr
*i_ehdrp
;
2356 Elf_Internal_Shdr
**i_shdrp
;
2359 /* We don't know how to write dynamic objects. Specifically, we
2360 don't know how to construct the program header. */
2361 if ((abfd
->flags
& DYNAMIC
) != 0)
2363 fprintf (stderr
, "Writing ELF dynamic objects is not supported\n");
2364 bfd_set_error (bfd_error_wrong_format
);
2368 if (abfd
->output_has_begun
== false)
2370 if (prep_headers (abfd
) == false)
2372 if (elf_compute_section_file_positions (abfd
) == false)
2374 abfd
->output_has_begun
= true;
2377 i_shdrp
= elf_elfsections (abfd
);
2378 i_ehdrp
= elf_elfheader (abfd
);
2380 bfd_map_over_sections (abfd
, write_relocs
, (PTR
) 0);
2381 assign_file_positions_for_relocs (abfd
);
2383 /* After writing the headers, we need to write the sections too... */
2384 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2386 if (bed
->elf_backend_section_processing
)
2387 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2388 if (i_shdrp
[count
]->contents
)
2390 bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
);
2391 bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
, 1,
2396 if (bed
->elf_backend_final_write_processing
)
2397 (*bed
->elf_backend_final_write_processing
) (abfd
);
2399 return write_shdrs_and_ehdr (abfd
);
2402 /* Given an index of a section, retrieve a pointer to it. Note
2403 that for our purposes, sections are indexed by {1, 2, ...} with
2404 0 being an illegal index. */
2406 /* In the original, each ELF section went into exactly one BFD
2407 section. This doesn't really make sense, so we need a real mapping.
2408 The mapping has to hide in the Elf_Internal_Shdr since asection
2409 doesn't have anything like a tdata field... */
2412 section_from_elf_index (abfd
, index
)
2416 /* @@ Is bfd_com_section really correct in all the places it could
2417 be returned from this routine? */
2419 if (index
== SHN_ABS
)
2420 return &bfd_com_section
; /* not abs? */
2421 if (index
== SHN_COMMON
)
2422 return &bfd_com_section
;
2424 if (index
> elf_elfheader (abfd
)->e_shnum
)
2428 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[index
];
2430 switch (hdr
->sh_type
)
2432 /* ELF sections that map to BFD sections */
2436 bfd_section_from_shdr (abfd
, index
);
2437 return (struct sec
*) hdr
->rawdata
;
2440 return (struct sec
*) &bfd_abs_section
;
2445 /* given a section, search the header to find them... */
2447 elf_section_from_bfd_section (abfd
, asect
)
2451 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2453 Elf_Internal_Shdr
*hdr
;
2454 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2456 if (asect
== &bfd_abs_section
)
2458 if (asect
== &bfd_com_section
)
2460 if (asect
== &bfd_und_section
)
2463 for (index
= 0; index
< maxindex
; index
++)
2465 hdr
= i_shdrp
[index
];
2466 switch (hdr
->sh_type
)
2468 /* ELF sections that map to BFD sections */
2474 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2480 /* fix_up_strtabs will generate STRTAB sections with names
2482 if (!strncmp (asect
->name
, ".stab", 5)
2483 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
2487 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2495 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2497 if (bed
->elf_backend_section_from_bfd_section
)
2502 if ((*bed
->elf_backend_section_from_bfd_section
)
2503 (abfd
, hdr
, asect
, &retval
))
2513 /* given a symbol, return the bfd index for that symbol. */
2515 elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
2517 struct symbol_cache_entry
**asym_ptr_ptr
;
2519 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2521 flagword flags
= asym_ptr
->flags
;
2523 /* When gas creates relocations against local labels, it creates its
2524 own symbol for the section, but does put the symbol into the
2525 symbol chain, so udata is 0. When the linker is generating
2526 relocatable output, this section symbol may be for one of the
2527 input sections rather than the output section. */
2528 if (asym_ptr
->udata
== (PTR
) 0
2529 && (flags
& BSF_SECTION_SYM
)
2530 && asym_ptr
->section
)
2534 if (asym_ptr
->section
->output_section
!= NULL
)
2535 indx
= asym_ptr
->section
->output_section
->index
;
2537 indx
= asym_ptr
->section
->index
;
2538 if (elf_section_syms (abfd
)[indx
])
2539 asym_ptr
->udata
= elf_section_syms (abfd
)[indx
]->udata
;
2542 if (asym_ptr
->udata
)
2543 idx
= ((Elf_Sym_Extra
*) asym_ptr
->udata
)->elf_sym_num
;
2553 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
2554 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
, elf_symbol_flags (flags
));
2563 elf_slurp_symbol_table (abfd
, symptrs
)
2565 asymbol
**symptrs
; /* Buffer for generated bfd symbols */
2567 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2568 long symcount
; /* Number of external ELF symbols */
2569 elf_symbol_type
*sym
; /* Pointer to current bfd symbol */
2570 elf_symbol_type
*symbase
; /* Buffer for generated bfd symbols */
2571 Elf_Internal_Sym i_sym
;
2572 Elf_External_Sym
*x_symp
= NULL
;
2574 /* this is only valid because there is only one symtab... */
2575 /* FIXME: This is incorrect, there may also be a dynamic symbol
2576 table which is a subset of the full symbol table. We either need
2577 to be prepared to read both (and merge them) or ensure that we
2578 only read the full symbol table. Currently we only get called to
2579 read the full symbol table. -fnf */
2581 /* Read each raw ELF symbol, converting from external ELF form to
2582 internal ELF form, and then using the information to create a
2583 canonical bfd symbol table entry.
2585 Note that we allocate the initial bfd canonical symbol buffer
2586 based on a one-to-one mapping of the ELF symbols to canonical
2587 symbols. We actually use all the ELF symbols, so there will be no
2588 space left over at the end. When we have all the symbols, we
2589 build the caller's pointer vector. */
2591 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2594 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2597 sym
= symbase
= NULL
;
2602 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2605 symbase
= ((elf_symbol_type
*)
2606 bfd_zalloc (abfd
, symcount
* sizeof (elf_symbol_type
)));
2607 if (symbase
== (elf_symbol_type
*) NULL
)
2609 bfd_set_error (bfd_error_no_memory
);
2614 /* Temporarily allocate room for the raw ELF symbols. */
2615 x_symp
= ((Elf_External_Sym
*)
2616 malloc (symcount
* sizeof (Elf_External_Sym
)));
2617 if (x_symp
== NULL
&& symcount
!= 0)
2619 bfd_set_error (bfd_error_no_memory
);
2623 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
2624 != symcount
* sizeof (Elf_External_Sym
))
2626 /* Skip first symbol, which is a null dummy. */
2627 for (i
= 1; i
< symcount
; i
++)
2629 elf_swap_symbol_in (abfd
, x_symp
+ i
, &i_sym
);
2630 memcpy (&sym
->internal_elf_sym
, &i_sym
, sizeof (Elf_Internal_Sym
));
2631 #ifdef ELF_KEEP_EXTSYM
2632 memcpy (&sym
->native_elf_sym
, x_symp
+ i
, sizeof (Elf_External_Sym
));
2634 sym
->symbol
.the_bfd
= abfd
;
2636 sym
->symbol
.name
= elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2639 sym
->symbol
.value
= i_sym
.st_value
;
2641 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
2643 sym
->symbol
.section
= section_from_elf_index (abfd
,
2646 else if (i_sym
.st_shndx
== SHN_ABS
)
2648 sym
->symbol
.section
= &bfd_abs_section
;
2650 else if (i_sym
.st_shndx
== SHN_COMMON
)
2652 sym
->symbol
.section
= &bfd_com_section
;
2653 /* Elf puts the alignment into the `value' field, and
2654 the size into the `size' field. BFD wants to see the
2655 size in the value field, and doesn't care (at the
2656 moment) about the alignment. */
2657 sym
->symbol
.value
= i_sym
.st_size
;
2659 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2661 sym
->symbol
.section
= &bfd_und_section
;
2664 sym
->symbol
.section
= &bfd_abs_section
;
2666 sym
->symbol
.value
-= sym
->symbol
.section
->vma
;
2668 switch (ELF_ST_BIND (i_sym
.st_info
))
2671 sym
->symbol
.flags
|= BSF_LOCAL
;
2674 sym
->symbol
.flags
|= BSF_GLOBAL
;
2677 sym
->symbol
.flags
|= BSF_WEAK
;
2681 switch (ELF_ST_TYPE (i_sym
.st_info
))
2684 sym
->symbol
.flags
|= BSF_SECTION_SYM
| BSF_DEBUGGING
;
2687 sym
->symbol
.flags
|= BSF_FILE
| BSF_DEBUGGING
;
2690 sym
->symbol
.flags
|= BSF_FUNCTION
;
2694 /* Do some backend-specific processing on this symbol. */
2696 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2697 if (ebd
->elf_backend_symbol_processing
)
2698 (*ebd
->elf_backend_symbol_processing
) (abfd
, &sym
->symbol
);
2705 /* Do some backend-specific processing on this symbol table. */
2707 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2708 if (ebd
->elf_backend_symbol_table_processing
)
2709 (*ebd
->elf_backend_symbol_table_processing
) (abfd
, symbase
, symcount
);
2712 /* We rely on the zalloc to clear out the final symbol entry. */
2714 bfd_get_symcount (abfd
) = symcount
= sym
- symbase
;
2716 /* Fill in the user's symbol pointer vector if needed. */
2720 while (symcount
-- > 0)
2722 *symptrs
++ = &sym
->symbol
;
2725 *symptrs
= 0; /* Final null pointer */
2737 /* Return the number of bytes required to hold the symtab vector.
2739 Note that we base it on the count plus 1, since we will null terminate
2740 the vector allocated based on this size. However, the ELF symbol table
2741 always has a dummy entry as symbol #0, so it ends up even. */
2744 elf_get_symtab_upper_bound (abfd
)
2749 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2751 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2752 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2758 This function return the number of bytes required to store the
2759 relocation information associated with section <<sect>>
2760 attached to bfd <<abfd>>
2764 elf_get_reloc_upper_bound (abfd
, asect
)
2768 if (asect
->flags
& SEC_RELOC
)
2770 /* either rel or rela */
2771 return elf_section_data (asect
)->rel_hdr
.sh_size
;
2778 elf_slurp_reloca_table (abfd
, asect
, symbols
)
2783 Elf_External_Rela
*native_relocs
;
2784 arelent
*reloc_cache
;
2789 if (asect
->relocation
)
2791 if (asect
->reloc_count
== 0)
2793 if (asect
->flags
& SEC_CONSTRUCTOR
)
2796 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2797 native_relocs
= (Elf_External_Rela
*)
2798 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rela
));
2801 bfd_set_error (bfd_error_no_memory
);
2804 bfd_read ((PTR
) native_relocs
,
2805 sizeof (Elf_External_Rela
), asect
->reloc_count
, abfd
);
2807 reloc_cache
= (arelent
*)
2808 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2812 bfd_set_error (bfd_error_no_memory
);
2816 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2818 Elf_Internal_Rela dst
;
2819 Elf_External_Rela
*src
;
2821 cache_ptr
= reloc_cache
+ idx
;
2822 src
= native_relocs
+ idx
;
2823 elf_swap_reloca_in (abfd
, src
, &dst
);
2825 #ifdef RELOC_PROCESSING
2826 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2828 if (asect
->flags
& SEC_RELOC
)
2830 /* relocatable, so the offset is off of the section */
2831 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2835 /* non-relocatable, so the offset a virtual address */
2836 cache_ptr
->address
= dst
.r_offset
;
2839 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
2840 of zero points to the dummy symbol, which was not read into
2841 the symbol table SYMBOLS. */
2842 if (ELF_R_SYM (dst
.r_info
) == 0)
2843 cache_ptr
->sym_ptr_ptr
= bfd_abs_section
.symbol_ptr_ptr
;
2848 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
2850 /* Translate any ELF section symbol into a BFD section
2852 s
= *(cache_ptr
->sym_ptr_ptr
);
2853 if (s
->flags
& BSF_SECTION_SYM
)
2855 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
2856 s
= *cache_ptr
->sym_ptr_ptr
;
2857 if (s
->name
== 0 || s
->name
[0] == 0)
2861 cache_ptr
->addend
= dst
.r_addend
;
2863 /* Fill in the cache_ptr->howto field from dst.r_type */
2865 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2866 (*ebd
->elf_info_to_howto
) (abfd
, cache_ptr
, &dst
);
2871 asect
->relocation
= reloc_cache
;
2877 elf_debug_section (str
, num
, hdr
)
2880 Elf_Internal_Shdr
*hdr
;
2882 fprintf (stderr
, "\nSection#%d '%s' 0x%.8lx\n", num
, str
, (long) hdr
);
2884 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
2885 (long) hdr
->sh_name
,
2886 (long) hdr
->sh_type
,
2887 (long) hdr
->sh_flags
);
2889 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
2890 (long) hdr
->sh_addr
,
2891 (long) hdr
->sh_offset
,
2892 (long) hdr
->sh_size
);
2894 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
2895 (long) hdr
->sh_link
,
2896 (long) hdr
->sh_info
,
2897 (long) hdr
->sh_addralign
);
2898 fprintf (stderr
, "sh_entsize = %ld\n",
2899 (long) hdr
->sh_entsize
);
2900 fprintf (stderr
, "rawdata = 0x%.8lx\n", (long) hdr
->rawdata
);
2901 fprintf (stderr
, "contents = 0x%.8lx\n", (long) hdr
->contents
);
2902 fprintf (stderr
, "size = %ld\n", (long) hdr
->size
);
2907 elf_debug_file (ehdrp
)
2908 Elf_Internal_Ehdr
*ehdrp
;
2910 fprintf (stderr
, "e_entry = 0x%.8lx\n", (long) ehdrp
->e_entry
);
2911 fprintf (stderr
, "e_phoff = %ld\n", (long) ehdrp
->e_phoff
);
2912 fprintf (stderr
, "e_phnum = %ld\n", (long) ehdrp
->e_phnum
);
2913 fprintf (stderr
, "e_phentsize = %ld\n", (long) ehdrp
->e_phentsize
);
2914 fprintf (stderr
, "e_shoff = %ld\n", (long) ehdrp
->e_shoff
);
2915 fprintf (stderr
, "e_shnum = %ld\n", (long) ehdrp
->e_shnum
);
2916 fprintf (stderr
, "e_shentsize = %ld\n", (long) ehdrp
->e_shentsize
);
2921 elf_slurp_reloc_table (abfd
, asect
, symbols
)
2926 Elf_External_Rel
*native_relocs
;
2927 arelent
*reloc_cache
;
2929 Elf_Internal_Shdr
*data_hdr
;
2931 unsigned long data_max
;
2932 char buf
[4]; /* FIXME -- might be elf64 */
2936 if (asect
->relocation
)
2938 if (asect
->reloc_count
== 0)
2940 if (asect
->flags
& SEC_CONSTRUCTOR
)
2943 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2944 native_relocs
= (Elf_External_Rel
*)
2945 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rel
));
2948 bfd_set_error (bfd_error_no_memory
);
2951 bfd_read ((PTR
) native_relocs
,
2952 sizeof (Elf_External_Rel
), asect
->reloc_count
, abfd
);
2954 reloc_cache
= (arelent
*)
2955 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2959 bfd_set_error (bfd_error_no_memory
);
2963 /* Get the offset of the start of the segment we are relocating to read in
2964 the implicit addend. */
2965 data_hdr
= &elf_section_data (asect
)->this_hdr
;
2966 data_off
= data_hdr
->sh_offset
;
2967 data_max
= data_hdr
->sh_size
- sizeof (buf
) + 1;
2970 elf_debug_section ("data section", -1, data_hdr
);
2973 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2975 #ifdef RELOC_PROCESSING
2976 Elf_Internal_Rel dst
;
2977 Elf_External_Rel
*src
;
2979 cache_ptr
= reloc_cache
+ idx
;
2980 src
= native_relocs
+ idx
;
2981 elf_swap_reloc_in (abfd
, src
, &dst
);
2983 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2985 Elf_Internal_Rel dst
;
2986 Elf_External_Rel
*src
;
2988 cache_ptr
= reloc_cache
+ idx
;
2989 src
= native_relocs
+ idx
;
2991 elf_swap_reloc_in (abfd
, src
, &dst
);
2993 if (asect
->flags
& SEC_RELOC
)
2995 /* relocatable, so the offset is off of the section */
2996 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
3000 /* non-relocatable, so the offset a virtual address */
3001 cache_ptr
->address
= dst
.r_offset
;
3004 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
3005 of zero points to the dummy symbol, which was not read into
3006 the symbol table SYMBOLS. */
3007 if (ELF_R_SYM (dst
.r_info
) == 0)
3008 cache_ptr
->sym_ptr_ptr
= bfd_abs_section
.symbol_ptr_ptr
;
3013 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
3015 /* Translate any ELF section symbol into a BFD section
3017 s
= *(cache_ptr
->sym_ptr_ptr
);
3018 if (s
->flags
& BSF_SECTION_SYM
)
3020 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
3021 s
= *cache_ptr
->sym_ptr_ptr
;
3022 if (s
->name
== 0 || s
->name
[0] == 0)
3026 BFD_ASSERT (dst
.r_offset
<= data_max
);
3027 cache_ptr
->addend
= 0;
3029 /* Fill in the cache_ptr->howto field from dst.r_type */
3031 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
3032 (*ebd
->elf_info_to_howto_rel
) (abfd
, cache_ptr
, &dst
);
3037 asect
->relocation
= reloc_cache
;
3042 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
3048 arelent
*tblptr
= section
->relocation
;
3049 unsigned int count
= 0;
3050 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
3052 /* snarfed from coffcode.h */
3055 if (! elf_slurp_reloca_table (abfd
, section
, symbols
))
3060 if (! elf_slurp_reloc_table (abfd
, section
, symbols
))
3064 tblptr
= section
->relocation
;
3068 for (; count
++ < section
->reloc_count
;)
3069 *relptr
++ = tblptr
++;
3072 return section
->reloc_count
;
3076 elf_get_symtab (abfd
, alocation
)
3078 asymbol
**alocation
;
3080 if (!elf_slurp_symbol_table (abfd
, alocation
))
3083 return bfd_get_symcount (abfd
);
3087 elf_make_empty_symbol (abfd
)
3090 elf_symbol_type
*newsym
;
3092 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
3095 bfd_set_error (bfd_error_no_memory
);
3100 newsym
->symbol
.the_bfd
= abfd
;
3101 return &newsym
->symbol
;
3106 elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
3111 bfd_symbol_info (symbol
, ret
);
3115 elf_print_symbol (ignore_abfd
, filep
, symbol
, how
)
3119 bfd_print_symbol_type how
;
3121 FILE *file
= (FILE *) filep
;
3124 case bfd_print_symbol_name
:
3125 fprintf (file
, "%s", symbol
->name
);
3127 case bfd_print_symbol_more
:
3128 fprintf (file
, "elf ");
3129 fprintf_vma (file
, symbol
->value
);
3130 fprintf (file
, " %lx", (long) symbol
->flags
);
3132 case bfd_print_symbol_all
:
3134 CONST
char *section_name
;
3135 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
3136 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
3137 fprintf (file
, " %s\t%s",
3147 elf_get_lineno (ignore_abfd
, symbol
)
3151 fprintf (stderr
, "elf_get_lineno unimplemented\n");
3158 elf_set_arch_mach (abfd
, arch
, machine
)
3160 enum bfd_architecture arch
;
3161 unsigned long machine
;
3163 /* Allow any architecture to be supported by the elf backend */
3166 case bfd_arch_unknown
: /* EM_NONE */
3167 case bfd_arch_sparc
: /* EM_SPARC */
3168 case bfd_arch_i386
: /* EM_386 */
3169 case bfd_arch_m68k
: /* EM_68K */
3170 case bfd_arch_m88k
: /* EM_88K */
3171 case bfd_arch_i860
: /* EM_860 */
3172 case bfd_arch_mips
: /* EM_MIPS (MIPS R3000) */
3173 case bfd_arch_hppa
: /* EM_HPPA (HP PA_RISC) */
3174 case bfd_arch_powerpc
: /* EM_CYGNUS_POWERPC */
3175 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
3182 elf_find_nearest_line (abfd
,
3193 CONST
char **filename_ptr
;
3194 CONST
char **functionname_ptr
;
3195 unsigned int *line_ptr
;
3201 elf_sizeof_headers (abfd
, reloc
)
3205 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
3212 elf_set_section_contents (abfd
, section
, location
, offset
, count
)
3217 bfd_size_type count
;
3219 Elf_Internal_Shdr
*hdr
;
3221 if (abfd
->output_has_begun
== false) /* set by bfd.c handler? */
3223 /* do setup calculations (FIXME) */
3224 if (prep_headers (abfd
) == false)
3226 if (elf_compute_section_file_positions (abfd
) == false)
3228 abfd
->output_has_begun
= true;
3231 hdr
= &elf_section_data (section
)->this_hdr
;
3233 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
3235 if (bfd_write (location
, 1, count
, abfd
) != count
)
3242 elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
3245 Elf_Internal_Rela
*dst
;
3247 fprintf (stderr
, "elf RELA relocation support for target machine unimplemented\n");
3253 elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
3256 Elf_Internal_Rel
*dst
;
3258 fprintf (stderr
, "elf REL relocation support for target machine unimplemented\n");
3264 /* Core file support */
3266 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3267 #include <sys/procfs.h>
3269 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
3270 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
3271 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
3277 bfd_prstatus (abfd
, descdata
, descsz
, filepos
)
3284 prstatus_t
*status
= (prstatus_t
*) 0;
3286 if (descsz
== sizeof (prstatus_t
))
3288 newsect
= bfd_make_section (abfd
, ".reg");
3289 newsect
->_raw_size
= sizeof (status
->pr_reg
);
3290 newsect
->filepos
= filepos
+ (long) &status
->pr_reg
;
3291 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3292 newsect
->alignment_power
= 2;
3293 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3295 memcpy (core_prstatus (abfd
), descdata
, descsz
);
3300 /* Stash a copy of the prpsinfo structure away for future use. */
3303 bfd_prpsinfo (abfd
, descdata
, descsz
, filepos
)
3311 if (descsz
== sizeof (prpsinfo_t
))
3313 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3315 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
3321 bfd_fpregset (abfd
, descdata
, descsz
, filepos
)
3329 newsect
= bfd_make_section (abfd
, ".reg2");
3330 newsect
->_raw_size
= descsz
;
3331 newsect
->filepos
= filepos
;
3332 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3333 newsect
->alignment_power
= 2;
3336 #endif /* HAVE_PROCFS */
3338 /* Return a pointer to the args (including the command name) that were
3339 seen by the program that generated the core dump. Note that for
3340 some reason, a spurious space is tacked onto the end of the args
3341 in some (at least one anyway) implementations, so strip it off if
3345 elf_core_file_failing_command (abfd
)
3349 if (core_prpsinfo (abfd
))
3351 prpsinfo_t
*p
= core_prpsinfo (abfd
);
3352 char *scan
= p
->pr_psargs
;
3357 if ((scan
> p
->pr_psargs
) && (*scan
== ' '))
3361 return p
->pr_psargs
;
3367 /* Return the number of the signal that caused the core dump. Presumably,
3368 since we have a core file, we got a signal of some kind, so don't bother
3369 checking the other process status fields, just return the signal number.
3373 elf_core_file_failing_signal (abfd
)
3377 if (core_prstatus (abfd
))
3379 return ((prstatus_t
*) (core_prstatus (abfd
)))->pr_cursig
;
3385 /* Check to see if the core file could reasonably be expected to have
3386 come for the current executable file. Note that by default we return
3387 true unless we find something that indicates that there might be a
3392 elf_core_file_matches_executable_p (core_bfd
, exec_bfd
)
3401 /* First, xvecs must match since both are ELF files for the same target. */
3403 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
3405 bfd_set_error (bfd_error_system_call
);
3411 /* If no prpsinfo, just return true. Otherwise, grab the last component
3412 of the exec'd pathname from the prpsinfo. */
3414 if (core_prpsinfo (core_bfd
))
3416 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
))->pr_fname
);
3423 /* Find the last component of the executable pathname. */
3425 if ((execname
= strrchr (exec_bfd
->filename
, '/')) != NULL
)
3431 execname
= (char *) exec_bfd
->filename
;
3434 /* See if they match */
3436 return strcmp (execname
, corename
) ? false : true;
3442 #endif /* HAVE_PROCFS */
3445 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3446 the information that would normally be available from the /proc interface
3447 for the process, at the time the process dumped core. Currently this
3448 includes copies of the prstatus, prpsinfo, and fpregset structures.
3450 Since these structures are potentially machine dependent in size and
3451 ordering, bfd provides two levels of support for them. The first level,
3452 available on all machines since it does not require that the host
3453 have /proc support or the relevant include files, is to create a bfd
3454 section for each of the prstatus, prpsinfo, and fpregset structures,
3455 without any interpretation of their contents. With just this support,
3456 the bfd client will have to interpret the structures itself. Even with
3457 /proc support, it might want these full structures for it's own reasons.
3459 In the second level of support, where HAVE_PROCFS is defined, bfd will
3460 pick apart the structures to gather some additional information that
3461 clients may want, such as the general register set, the name of the
3462 exec'ed file and its arguments, the signal (if any) that caused the
3468 elf_corefile_note (abfd
, hdr
)
3470 Elf_Internal_Phdr
*hdr
;
3472 Elf_External_Note
*x_note_p
; /* Elf note, external form */
3473 Elf_Internal_Note i_note
; /* Elf note, internal form */
3474 char *buf
= NULL
; /* Entire note segment contents */
3475 char *namedata
; /* Name portion of the note */
3476 char *descdata
; /* Descriptor portion of the note */
3477 char *sectname
; /* Name to use for new section */
3478 long filepos
; /* File offset to descriptor data */
3481 if (hdr
->p_filesz
> 0
3482 && (buf
= (char *) malloc (hdr
->p_filesz
)) != NULL
3483 && bfd_seek (abfd
, hdr
->p_offset
, SEEK_SET
) != -1
3484 && bfd_read ((PTR
) buf
, hdr
->p_filesz
, 1, abfd
) == hdr
->p_filesz
)
3486 x_note_p
= (Elf_External_Note
*) buf
;
3487 while ((char *) x_note_p
< (buf
+ hdr
->p_filesz
))
3489 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->namesz
);
3490 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->descsz
);
3491 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->type
);
3492 namedata
= x_note_p
->name
;
3493 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
3494 filepos
= hdr
->p_offset
+ (descdata
- buf
);
3495 switch (i_note
.type
)
3498 /* process descdata as prstatus info */
3499 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
3500 sectname
= ".prstatus";
3503 /* process descdata as fpregset info */
3504 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
3505 sectname
= ".fpregset";
3508 /* process descdata as prpsinfo */
3509 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
3510 sectname
= ".prpsinfo";
3513 /* Unknown descriptor, just ignore it. */
3517 if (sectname
!= NULL
)
3519 newsect
= bfd_make_section (abfd
, sectname
);
3520 newsect
->_raw_size
= i_note
.descsz
;
3521 newsect
->filepos
= filepos
;
3522 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3523 newsect
->alignment_power
= 2;
3525 x_note_p
= (Elf_External_Note
*)
3526 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
3533 else if (hdr
->p_filesz
> 0)
3535 bfd_set_error (bfd_error_no_memory
);
3542 /* Core files are simply standard ELF formatted files that partition
3543 the file using the execution view of the file (program header table)
3544 rather than the linking view. In fact, there is no section header
3545 table in a core file.
3547 The process status information (including the contents of the general
3548 register set) and the floating point register set are stored in a
3549 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3550 that allow standard bfd access to the general registers (.reg) and the
3551 floating point registers (.reg2).
3556 elf_core_file_p (abfd
)
3559 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
3560 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3561 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
3562 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
3563 unsigned int phindex
;
3564 struct elf_backend_data
*ebd
;
3566 /* Read in the ELF header in external format. */
3568 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
3570 if (bfd_get_error () != bfd_error_system_call
)
3571 bfd_set_error (bfd_error_wrong_format
);
3575 /* Now check to see if we have a valid ELF file, and one that BFD can
3576 make use of. The magic number must match, the address size ('class')
3577 and byte-swapping must match our XVEC entry, and it must have a
3578 program header table (FIXME: See comments re segments at top of this
3581 if (elf_file_p (&x_ehdr
) == false)
3584 bfd_set_error (bfd_error_wrong_format
);
3588 /* FIXME, Check EI_VERSION here ! */
3592 int desired_address_size
= ELFCLASS32
;
3595 int desired_address_size
= ELFCLASS64
;
3598 if (x_ehdr
.e_ident
[EI_CLASS
] != desired_address_size
)
3602 /* Switch xvec to match the specified byte order. */
3603 switch (x_ehdr
.e_ident
[EI_DATA
])
3605 case ELFDATA2MSB
: /* Big-endian */
3606 if (abfd
->xvec
->byteorder_big_p
== false)
3609 case ELFDATA2LSB
: /* Little-endian */
3610 if (abfd
->xvec
->byteorder_big_p
== true)
3613 case ELFDATANONE
: /* No data encoding specified */
3614 default: /* Unknown data encoding specified */
3618 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3619 the tdata pointer in the bfd. */
3622 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
3623 if (elf_tdata (abfd
) == NULL
)
3625 bfd_set_error (bfd_error_no_memory
);
3629 /* FIXME, `wrong' returns from this point onward, leak memory. */
3631 /* Now that we know the byte order, swap in the rest of the header */
3632 i_ehdrp
= elf_elfheader (abfd
);
3633 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
3635 elf_debug_file (i_ehdrp
);
3638 ebd
= get_elf_backend_data (abfd
);
3640 /* Check that the ELF e_machine field matches what this particular
3641 BFD format expects. */
3642 if (ebd
->elf_machine_code
!= i_ehdrp
->e_machine
)
3644 bfd_target
**target_ptr
;
3646 if (ebd
->elf_machine_code
!= EM_NONE
)
3649 /* This is the generic ELF target. Let it match any ELF target
3650 for which we do not have a specific backend. */
3651 for (target_ptr
= bfd_target_vector
; *target_ptr
!= NULL
; target_ptr
++)
3653 struct elf_backend_data
*back
;
3655 if ((*target_ptr
)->flavour
!= bfd_target_elf_flavour
)
3657 back
= (struct elf_backend_data
*) (*target_ptr
)->backend_data
;
3658 if (back
->elf_machine_code
== i_ehdrp
->e_machine
)
3660 /* target_ptr is an ELF backend which matches this
3661 object file, so reject the generic ELF target. */
3667 /* If there is no program header, or the type is not a core file, then
3669 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
3672 /* Allocate space for a copy of the program header table in
3673 internal form, seek to the program header table in the file,
3674 read it in, and convert it to internal form. As a simple sanity
3675 check, verify that the what BFD thinks is the size of each program
3676 header table entry actually matches the size recorded in the file. */
3678 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
3680 i_phdrp
= (Elf_Internal_Phdr
*)
3681 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
3684 bfd_set_error (bfd_error_no_memory
);
3687 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
3689 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3691 if (bfd_read ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
3694 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
3697 /* Once all of the program headers have been read and converted, we
3698 can start processing them. */
3700 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3702 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
3703 if ((i_phdrp
+ phindex
)->p_type
== PT_NOTE
)
3705 elf_corefile_note (abfd
, i_phdrp
+ phindex
);
3709 /* Remember the entry point specified in the ELF file header. */
3711 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;