1 /* ELF executable support for BFD.
2 Copyright 1991, 1992, 1993 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
59 #include <string.h> /* For strrchr and friends */
65 #define Elf_External_Ehdr NAME(Elf,External_Ehdr)
66 #define Elf_External_Sym NAME(Elf,External_Sym)
67 #define Elf_External_Shdr NAME(Elf,External_Shdr)
68 #define Elf_External_Phdr NAME(Elf,External_Phdr)
69 #define Elf_External_Rel NAME(Elf,External_Rel)
70 #define Elf_External_Rela NAME(Elf,External_Rela)
72 #define elf_symbol_type NAME(elf,symbol_type)
74 #define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command)
75 #define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal)
76 #define elf_core_file_matches_executable_p NAME(bfd_elf,core_file_matches_executable_p)
77 #define elf_object_p NAME(bfd_elf,object_p)
78 #define elf_core_file_p NAME(bfd_elf,core_file_p)
79 #define elf_write_object_contents NAME(bfd_elf,write_object_contents)
80 #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
81 #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
82 #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
83 #define elf_get_symtab NAME(bfd_elf,get_symtab)
84 #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol)
85 #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info)
86 #define elf_print_symbol NAME(bfd_elf,print_symbol)
87 #define elf_get_lineno NAME(bfd_elf,get_lineno)
88 #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach)
89 #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line)
90 #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers)
91 #define elf_set_section_contents NAME(bfd_elf,set_section_contents)
92 #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto)
93 #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel)
94 #define elf_get_sect_thunk NAME(bfd_elf,get_sect_thunk)
95 #define elf_hash NAME(bfd_elf,hash)
96 #define elf_new_section_hook NAME(bfd_elf,new_section_hook)
99 #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
100 #define ELF_R_SYM(X) ELF64_R_SYM(X)
103 #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
104 #define ELF_R_SYM(X) ELF32_R_SYM(X)
107 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
108 #include <sys/procfs.h>
110 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
111 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
112 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
117 #define INLINE __inline__
123 /* Forward declarations of static functions */
125 static struct sec
* section_from_elf_index
PARAMS ((bfd
*, int));
127 static int elf_section_from_bfd_section
PARAMS ((bfd
*, struct sec
*));
129 static boolean elf_slurp_symbol_table
PARAMS ((bfd
*, asymbol
**));
131 static int elf_symbol_from_bfd_symbol
PARAMS ((bfd
*,
132 struct symbol_cache_entry
**));
134 static void elf_map_symbols
PARAMS ((bfd
*));
137 static void elf_debug_section
PARAMS ((char *, int, Elf_Internal_Shdr
*));
138 static void elf_debug_file
PARAMS ((Elf_Internal_Ehdr
*));
141 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
142 can be handled by explicitly specifying 32 bits or "the long type". */
144 #define put_word bfd_h_put_64
145 #define get_word bfd_h_get_64
148 #define put_word bfd_h_put_32
149 #define get_word bfd_h_get_32
152 /* Translate an ELF symbol in external format into an ELF symbol in internal
156 DEFUN (elf_swap_symbol_in
, (abfd
, src
, dst
),
158 Elf_External_Sym
* src AND
159 Elf_Internal_Sym
* dst
)
161 dst
->st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->st_name
);
162 dst
->st_value
= get_word (abfd
, (bfd_byte
*) src
->st_value
);
163 dst
->st_size
= get_word (abfd
, (bfd_byte
*) src
->st_size
);
164 dst
->st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_info
);
165 dst
->st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_other
);
166 dst
->st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->st_shndx
);
169 /* Translate an ELF symbol in internal format into an ELF symbol in external
173 DEFUN (elf_swap_symbol_out
, (abfd
, src
, dst
),
175 Elf_Internal_Sym
* src AND
176 Elf_External_Sym
* dst
)
178 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
179 put_word (abfd
, src
->st_value
, dst
->st_value
);
180 put_word (abfd
, src
->st_size
, dst
->st_size
);
181 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
182 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
183 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
187 /* Translate an ELF file header in external format into an ELF file header in
191 DEFUN (elf_swap_ehdr_in
, (abfd
, src
, dst
),
193 Elf_External_Ehdr
* src AND
194 Elf_Internal_Ehdr
* dst
)
196 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
197 dst
->e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_type
);
198 dst
->e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_machine
);
199 dst
->e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_version
);
200 dst
->e_entry
= get_word (abfd
, (bfd_byte
*) src
->e_entry
);
201 dst
->e_phoff
= get_word (abfd
, (bfd_byte
*) src
->e_phoff
);
202 dst
->e_shoff
= get_word (abfd
, (bfd_byte
*) src
->e_shoff
);
203 dst
->e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_flags
);
204 dst
->e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_ehsize
);
205 dst
->e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phentsize
);
206 dst
->e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phnum
);
207 dst
->e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shentsize
);
208 dst
->e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shnum
);
209 dst
->e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shstrndx
);
212 /* Translate an ELF file header in internal format into an ELF file header in
216 DEFUN (elf_swap_ehdr_out
, (abfd
, src
, dst
),
218 Elf_Internal_Ehdr
* src AND
219 Elf_External_Ehdr
* dst
)
221 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
222 /* note that all elements of dst are *arrays of unsigned char* already... */
223 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
224 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
225 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
226 put_word (abfd
, src
->e_entry
, dst
->e_entry
);
227 put_word (abfd
, src
->e_phoff
, dst
->e_phoff
);
228 put_word (abfd
, src
->e_shoff
, dst
->e_shoff
);
229 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
230 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
231 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
232 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
233 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
234 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
235 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
239 /* Translate an ELF section header table entry in external format into an
240 ELF section header table entry in internal format. */
243 DEFUN (elf_swap_shdr_in
, (abfd
, src
, dst
),
245 Elf_External_Shdr
* src AND
246 Elf_Internal_Shdr
* dst
)
248 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
249 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
250 dst
->sh_flags
= get_word (abfd
, (bfd_byte
*) src
->sh_flags
);
251 dst
->sh_addr
= get_word (abfd
, (bfd_byte
*) src
->sh_addr
);
252 dst
->sh_offset
= get_word (abfd
, (bfd_byte
*) src
->sh_offset
);
253 dst
->sh_size
= get_word (abfd
, (bfd_byte
*) src
->sh_size
);
254 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
255 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
256 dst
->sh_addralign
= get_word (abfd
, (bfd_byte
*) src
->sh_addralign
);
257 dst
->sh_entsize
= get_word (abfd
, (bfd_byte
*) src
->sh_entsize
);
258 /* we haven't done any processing on it yet, so... */
259 dst
->rawdata
= (void *) 0;
262 /* Translate an ELF section header table entry in internal format into an
263 ELF section header table entry in external format. */
266 DEFUN (elf_swap_shdr_out
, (abfd
, src
, dst
),
268 Elf_Internal_Shdr
* src AND
269 Elf_External_Shdr
* dst
)
271 /* note that all elements of dst are *arrays of unsigned char* already... */
272 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
273 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
274 put_word (abfd
, src
->sh_flags
, dst
->sh_flags
);
275 put_word (abfd
, src
->sh_addr
, dst
->sh_addr
);
276 put_word (abfd
, src
->sh_offset
, dst
->sh_offset
);
277 put_word (abfd
, src
->sh_size
, dst
->sh_size
);
278 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
279 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
280 put_word (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
281 put_word (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
285 /* Translate an ELF program header table entry in external format into an
286 ELF program header table entry in internal format. */
289 DEFUN (elf_swap_phdr_in
, (abfd
, src
, dst
),
291 Elf_External_Phdr
* src AND
292 Elf_Internal_Phdr
* dst
)
294 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
295 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
296 dst
->p_offset
= get_word (abfd
, (bfd_byte
*) src
->p_offset
);
297 dst
->p_vaddr
= get_word (abfd
, (bfd_byte
*) src
->p_vaddr
);
298 dst
->p_paddr
= get_word (abfd
, (bfd_byte
*) src
->p_paddr
);
299 dst
->p_filesz
= get_word (abfd
, (bfd_byte
*) src
->p_filesz
);
300 dst
->p_memsz
= get_word (abfd
, (bfd_byte
*) src
->p_memsz
);
301 dst
->p_align
= get_word (abfd
, (bfd_byte
*) src
->p_align
);
307 DEFUN (elf_swap_phdr_out
, (abfd
, src
, dst
),
309 Elf_Internal_Phdr
* src AND
310 Elf_External_Phdr
* dst
)
312 /* note that all elements of dst are *arrays of unsigned char* already... */
313 bfd_h_put_32 (abfd
, src
->p_type
, dst
->p_type
);
314 put_word (abfd
, src
->p_offset
, dst
->p_offset
);
315 put_word (abfd
, src
->p_vaddr
, dst
->p_vaddr
);
316 put_word (abfd
, src
->p_paddr
, dst
->p_paddr
);
317 put_word (abfd
, src
->p_filesz
, dst
->p_filesz
);
318 put_word (abfd
, src
->p_memsz
, dst
->p_memsz
);
319 bfd_h_put_32 (abfd
, src
->p_flags
, dst
->p_flags
);
320 put_word (abfd
, src
->p_align
, dst
->p_align
);
323 /* Translate an ELF reloc from external format to internal format. */
325 DEFUN (elf_swap_reloc_in
, (abfd
, src
, dst
),
327 Elf_External_Rel
* src AND
328 Elf_Internal_Rel
* dst
)
330 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
331 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
335 DEFUN (elf_swap_reloca_in
, (abfd
, src
, dst
),
337 Elf_External_Rela
* src AND
338 Elf_Internal_Rela
* dst
)
340 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
341 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
342 dst
->r_addend
= get_word (abfd
, (bfd_byte
*) src
->r_addend
);
345 /* Translate an ELF reloc from internal format to external format. */
347 DEFUN (elf_swap_reloc_out
, (abfd
, src
, dst
),
349 Elf_Internal_Rel
* src AND
350 Elf_External_Rel
* dst
)
352 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
353 put_word (abfd
, src
->r_info
, dst
->r_info
);
357 DEFUN (elf_swap_reloca_out
, (abfd
, src
, dst
),
359 Elf_Internal_Rela
* src AND
360 Elf_External_Rela
* dst
)
362 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
363 put_word (abfd
, src
->r_info
, dst
->r_info
);
364 put_word (abfd
, src
->r_addend
, dst
->r_addend
);
367 /* Create a new bfd section from an ELF section header. */
370 DEFUN (elf_string_from_elf_strtab
, (abfd
, strindex
),
374 return elf_string_from_elf_section (abfd
, elf_elfheader (abfd
)->e_shstrndx
,
379 DEFUN (bfd_section_from_shdr
, (abfd
, shindex
),
381 unsigned int shindex
)
383 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
384 Elf_Internal_Shdr
*hdr
= i_shdrp
+ shindex
;
388 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
390 switch (hdr
->sh_type
)
394 /* inactive section. Throw it away. */
398 /* Bits that get saved. This one is real. */
401 newsect
= bfd_make_section (abfd
, name
);
404 newsect
->vma
= hdr
->sh_addr
;
405 newsect
->_raw_size
= hdr
->sh_size
;
406 newsect
->filepos
= hdr
->sh_offset
; /* so we can read back the bits */
407 newsect
->flags
|= SEC_HAS_CONTENTS
;
408 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
410 if (hdr
->sh_flags
& SHF_ALLOC
)
412 newsect
->flags
|= SEC_ALLOC
;
413 newsect
->flags
|= SEC_LOAD
;
416 if (!(hdr
->sh_flags
& SHF_WRITE
))
417 newsect
->flags
|= SEC_READONLY
;
419 if (hdr
->sh_flags
& SHF_EXECINSTR
)
420 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
422 newsect
->flags
|= SEC_DATA
;
424 hdr
->rawdata
= (void *) newsect
;
427 hdr
->rawdata
= (void *) bfd_get_section_by_name (abfd
, name
);
432 /* Bits that get saved. This one is real. */
435 newsect
= bfd_make_section (abfd
, name
);
438 newsect
->vma
= hdr
->sh_addr
;
439 newsect
->_raw_size
= hdr
->sh_size
;
440 newsect
->filepos
= hdr
->sh_offset
; /* fake */
441 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
442 if (hdr
->sh_flags
& SHF_ALLOC
)
443 newsect
->flags
|= SEC_ALLOC
;
445 if (!(hdr
->sh_flags
& SHF_WRITE
))
446 newsect
->flags
|= SEC_READONLY
;
448 if (hdr
->sh_flags
& SHF_EXECINSTR
)
449 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
451 newsect
->flags
|= SEC_DATA
;
453 hdr
->rawdata
= (void *) newsect
;
458 case SHT_SYMTAB
: /* A symbol table */
459 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
460 elf_onesymtab (abfd
) = shindex
;
461 abfd
->flags
|= HAS_SYMS
;
464 case SHT_STRTAB
: /* A string table */
465 if (!strcmp (name
, ".strtab") || !strcmp (name
, ".shstrtab"))
470 newsect
= bfd_make_section (abfd
, name
);
472 newsect
->flags
= SEC_HAS_CONTENTS
;
479 /* *these* do a lot of work -- but build no sections! */
480 /* the spec says there can be multiple strtabs, but only one symtab */
481 /* but there can be lots of REL* sections. */
482 /* FIXME: The above statement is wrong! There are typically at least
483 two symbol tables in a dynamically linked executable, ".dynsym"
484 which is the dynamic linkage symbol table and ".symtab", which is
485 the "traditional" symbol table. -fnf */
488 asection
*target_sect
;
489 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
491 /* Don't allow REL relocations on a machine that uses RELA and
493 /* @@ Actually, the generic ABI does suggest that both might be
494 used in one file. But the four ABI Processor Supplements I
495 have access to right now all specify that only one is used on
496 each of those architectures. It's conceivable that, e.g., a
497 bunch of absolute 32-bit relocs might be more compact in REL
498 form even on a RELA machine... */
499 BFD_ASSERT (!(use_rela_p
&& (hdr
->sh_type
== SHT_REL
)));
500 BFD_ASSERT (!(!use_rela_p
&& (hdr
->sh_type
== SHT_RELA
)));
501 BFD_ASSERT (hdr
->sh_entsize
==
503 ? sizeof (Elf_External_Rela
)
504 : sizeof (Elf_External_Rel
)));
506 bfd_section_from_shdr (abfd
, hdr
->sh_link
); /* symbol table */
507 bfd_section_from_shdr (abfd
, hdr
->sh_info
); /* target */
508 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
509 if (target_sect
== NULL
)
513 /* FIXME: We are only prepared to read one symbol table, so
514 do NOT read the dynamic symbol table since it is only a
515 subset of the full symbol table. Also see comment above. -fnf */
516 if (!elf_slurp_symbol_table (abfd
, i_shdrp
+ hdr
->sh_link
))
520 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
521 target_sect
->flags
|= SEC_RELOC
;
522 target_sect
->relocation
= 0;
523 target_sect
->rel_filepos
= hdr
->sh_offset
;
530 case SHT_DYNSYM
: /* could treat this like symtab... */
532 fprintf (stderr
, "Dynamic Linking sections not yet supported.\n");
539 fprintf (stderr
, "Note Sections not yet supported.\n");
546 fprintf (stderr
, "SHLIB Sections not supported (and non conforming.)\n");
558 DEFUN (elf_new_section_hook
, (abfd
, sec
),
562 /* sec->symbol->name = "";*/
566 static struct strtab
*
567 DEFUN (bfd_new_strtab
, (abfd
),
572 ss
= (struct strtab
*) bfd_xmalloc (sizeof (struct strtab
));
573 ss
->tab
= bfd_xmalloc (1);
574 BFD_ASSERT (ss
->tab
!= 0);
583 DEFUN (bfd_add_to_strtab
, (abfd
, ss
, str
),
585 struct strtab
*ss AND
588 /* should search first, but for now: */
589 /* include the trailing NUL */
590 int ln
= strlen (str
) + 1;
592 /* should this be using obstacks? */
593 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
595 BFD_ASSERT (ss
->tab
!= 0);
596 strcpy (ss
->tab
+ ss
->length
, str
);
600 return ss
->length
- ln
;
604 DEFUN (bfd_add_2_to_strtab
, (abfd
, ss
, str
, str2
),
606 struct strtab
*ss AND
610 /* should search first, but for now: */
611 /* include the trailing NUL */
612 int ln
= strlen (str
) + strlen (str2
) + 1;
614 /* should this be using obstacks? */
616 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
618 ss
->tab
= bfd_xmalloc (ln
);
620 BFD_ASSERT (ss
->tab
!= 0);
621 strcpy (ss
->tab
+ ss
->length
, str
);
622 strcpy (ss
->tab
+ ss
->length
+ strlen (str
), str2
);
626 return ss
->length
- ln
;
629 /* Create a new ELF section from a bfd section. */
633 DEFUN (bfd_shdr_from_section
, (abfd
, hdr
, shstrtab
, indx
),
635 Elf_Internal_Shdr
* hdr AND
636 struct strtab
*shstrtab AND
642 sect
= abfd
->sections
;
643 for (ndx
= indx
; --ndx
;)
647 hdr
[indx
].sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
648 bfd_section_name (abfd
, sect
));
649 hdr
[indx
].sh_addr
= sect
->vma
;
650 hdr
[indx
].sh_size
= sect
->_raw_size
;
651 hdr
[indx
].sh_addralign
= 1 << sect
->alignment_power
;
652 hdr
[indx
].sh_flags
= 0;
653 /* these need to be preserved on */
654 hdr
[indx
].sh_link
= 0;
655 hdr
[indx
].sh_info
= 0;
656 hdr
[indx
].sh_entsize
= 0;
658 hdr
[indx
].sh_type
= 0;
659 if (sect
->flags
& SEC_RELOC
)
661 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
662 hdr
[indx
].sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
665 if (sect
->flags
& SEC_HAS_CONTENTS
)
667 hdr
[indx
].sh_offset
= sect
->filepos
;
668 hdr
[indx
].sh_size
= sect
->_raw_size
;
670 if (sect
->flags
& SEC_ALLOC
)
672 hdr
[indx
].sh_flags
|= SHF_ALLOC
;
673 if (sect
->flags
& SEC_LOAD
)
675 /* do something with sh_type ? */
678 if (!(sect
->flags
& SEC_READONLY
))
679 hdr
[indx
].sh_flags
|= SHF_WRITE
;
681 if (sect
->flags
& SEC_CODE
)
682 hdr
[indx
].sh_flags
|= SHF_EXECINSTR
;
688 /* Create a new bfd section from an ELF program header.
690 Since program segments have no names, we generate a synthetic name
691 of the form segment<NUM>, where NUM is generally the index in the
692 program header table. For segments that are split (see below) we
693 generate the names segment<NUM>a and segment<NUM>b.
695 Note that some program segments may have a file size that is different than
696 (less than) the memory size. All this means is that at execution the
697 system must allocate the amount of memory specified by the memory size,
698 but only initialize it with the first "file size" bytes read from the
699 file. This would occur for example, with program segments consisting
700 of combined data+bss.
702 To handle the above situation, this routine generates TWO bfd sections
703 for the single program segment. The first has the length specified by
704 the file size of the segment, and the second has the length specified
705 by the difference between the two sizes. In effect, the segment is split
706 into it's initialized and uninitialized parts.
711 DEFUN (bfd_section_from_phdr
, (abfd
, hdr
, index
),
713 Elf_Internal_Phdr
* hdr AND
721 split
= ((hdr
->p_memsz
> 0) &&
722 (hdr
->p_filesz
> 0) &&
723 (hdr
->p_memsz
> hdr
->p_filesz
));
724 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
725 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
726 strcpy (name
, namebuf
);
727 newsect
= bfd_make_section (abfd
, name
);
728 newsect
->vma
= hdr
->p_vaddr
;
729 newsect
->_raw_size
= hdr
->p_filesz
;
730 newsect
->filepos
= hdr
->p_offset
;
731 newsect
->flags
|= SEC_HAS_CONTENTS
;
732 if (hdr
->p_type
== PT_LOAD
)
734 newsect
->flags
|= SEC_ALLOC
;
735 newsect
->flags
|= SEC_LOAD
;
736 if (hdr
->p_flags
& PF_X
)
738 /* FIXME: all we known is that it has execute PERMISSION,
740 newsect
->flags
|= SEC_CODE
;
743 if (!(hdr
->p_flags
& PF_W
))
745 newsect
->flags
|= SEC_READONLY
;
750 sprintf (namebuf
, "segment%db", index
);
751 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
752 strcpy (name
, namebuf
);
753 newsect
= bfd_make_section (abfd
, name
);
754 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
755 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
756 if (hdr
->p_type
== PT_LOAD
)
758 newsect
->flags
|= SEC_ALLOC
;
759 if (hdr
->p_flags
& PF_X
)
760 newsect
->flags
|= SEC_CODE
;
762 if (!(hdr
->p_flags
& PF_W
))
763 newsect
->flags
|= SEC_READONLY
;
772 DEFUN (bfd_prstatus
, (abfd
, descdata
, descsz
, filepos
),
779 prstatus_t
*status
= (prstatus_t
*) 0;
781 if (descsz
== sizeof (prstatus_t
))
783 newsect
= bfd_make_section (abfd
, ".reg");
784 newsect
->_raw_size
= sizeof (status
->pr_reg
);
785 newsect
->filepos
= filepos
+ (long) &status
->pr_reg
;
786 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
787 newsect
->alignment_power
= 2;
788 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
790 memcpy (core_prstatus (abfd
), descdata
, descsz
);
795 /* Stash a copy of the prpsinfo structure away for future use. */
798 DEFUN (bfd_prpsinfo
, (abfd
, descdata
, descsz
, filepos
),
806 if (descsz
== sizeof (prpsinfo_t
))
808 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
810 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
816 DEFUN (bfd_fpregset
, (abfd
, descdata
, descsz
, filepos
),
824 newsect
= bfd_make_section (abfd
, ".reg2");
825 newsect
->_raw_size
= descsz
;
826 newsect
->filepos
= filepos
;
827 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
828 newsect
->alignment_power
= 2;
831 #endif /* HAVE_PROCFS */
833 /* Return a pointer to the args (including the command name) that were
834 seen by the program that generated the core dump. Note that for
835 some reason, a spurious space is tacked onto the end of the args
836 in some (at least one anyway) implementations, so strip it off if
840 DEFUN (elf_core_file_failing_command
, (abfd
),
844 if (core_prpsinfo (abfd
))
846 prpsinfo_t
*p
= core_prpsinfo (abfd
);
847 char *scan
= p
->pr_psargs
;
852 if ((scan
> p
->pr_psargs
) && (*scan
== ' '))
862 /* Return the number of the signal that caused the core dump. Presumably,
863 since we have a core file, we got a signal of some kind, so don't bother
864 checking the other process status fields, just return the signal number.
868 DEFUN (elf_core_file_failing_signal
, (abfd
),
872 if (core_prstatus (abfd
))
874 return ((prstatus_t
*) (core_prstatus (abfd
)))->pr_cursig
;
880 /* Check to see if the core file could reasonably be expected to have
881 come for the current executable file. Note that by default we return
882 true unless we find something that indicates that there might be a
887 DEFUN (elf_core_file_matches_executable_p
, (core_bfd
, exec_bfd
),
896 /* First, xvecs must match since both are ELF files for the same target. */
898 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
900 bfd_error
= system_call_error
;
906 /* If no prpsinfo, just return true. Otherwise, grab the last component
907 of the exec'd pathname from the prpsinfo. */
909 if (core_prpsinfo (core_bfd
))
911 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
))->pr_fname
);
918 /* Find the last component of the executable pathname. */
920 if ((execname
= strrchr (exec_bfd
->filename
, '/')) != NULL
)
926 execname
= (char *) exec_bfd
->filename
;
929 /* See if they match */
931 return strcmp (execname
, corename
) ? false : true;
937 #endif /* HAVE_PROCFS */
940 /* ELF core files contain a segment of type PT_NOTE, that holds much of
941 the information that would normally be available from the /proc interface
942 for the process, at the time the process dumped core. Currently this
943 includes copies of the prstatus, prpsinfo, and fpregset structures.
945 Since these structures are potentially machine dependent in size and
946 ordering, bfd provides two levels of support for them. The first level,
947 available on all machines since it does not require that the host
948 have /proc support or the relevant include files, is to create a bfd
949 section for each of the prstatus, prpsinfo, and fpregset structures,
950 without any interpretation of their contents. With just this support,
951 the bfd client will have to interpret the structures itself. Even with
952 /proc support, it might want these full structures for it's own reasons.
954 In the second level of support, where HAVE_PROCFS is defined, bfd will
955 pick apart the structures to gather some additional information that
956 clients may want, such as the general register set, the name of the
957 exec'ed file and its arguments, the signal (if any) that caused the
963 DEFUN (elf_corefile_note
, (abfd
, hdr
),
965 Elf_Internal_Phdr
* hdr
)
967 Elf_External_Note
*x_note_p
; /* Elf note, external form */
968 Elf_Internal_Note i_note
; /* Elf note, internal form */
969 char *buf
= NULL
; /* Entire note segment contents */
970 char *namedata
; /* Name portion of the note */
971 char *descdata
; /* Descriptor portion of the note */
972 char *sectname
; /* Name to use for new section */
973 long filepos
; /* File offset to descriptor data */
976 if (hdr
->p_filesz
> 0
977 && (buf
= (char *) bfd_xmalloc (hdr
->p_filesz
)) != NULL
978 && bfd_seek (abfd
, hdr
->p_offset
, SEEK_SET
) != -1
979 && bfd_read ((PTR
) buf
, hdr
->p_filesz
, 1, abfd
) == hdr
->p_filesz
)
981 x_note_p
= (Elf_External_Note
*) buf
;
982 while ((char *) x_note_p
< (buf
+ hdr
->p_filesz
))
984 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->namesz
);
985 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->descsz
);
986 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->type
);
987 namedata
= x_note_p
->name
;
988 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
989 filepos
= hdr
->p_offset
+ (descdata
- buf
);
993 /* process descdata as prstatus info */
994 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
995 sectname
= ".prstatus";
998 /* process descdata as fpregset info */
999 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
1000 sectname
= ".fpregset";
1003 /* process descdata as prpsinfo */
1004 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
1005 sectname
= ".prpsinfo";
1008 /* Unknown descriptor, just ignore it. */
1012 if (sectname
!= NULL
)
1014 newsect
= bfd_make_section (abfd
, sectname
);
1015 newsect
->_raw_size
= i_note
.descsz
;
1016 newsect
->filepos
= filepos
;
1017 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
1018 newsect
->alignment_power
= 2;
1020 x_note_p
= (Elf_External_Note
*)
1021 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
1033 /* Begin processing a given object.
1035 First we validate the file by reading in the ELF header and checking
1036 the magic number. */
1038 static INLINE boolean
1039 DEFUN (elf_file_p
, (x_ehdrp
), Elf_External_Ehdr
* x_ehdrp
)
1041 return ((x_ehdrp
->e_ident
[EI_MAG0
] == ELFMAG0
)
1042 && (x_ehdrp
->e_ident
[EI_MAG1
] == ELFMAG1
)
1043 && (x_ehdrp
->e_ident
[EI_MAG2
] == ELFMAG2
)
1044 && (x_ehdrp
->e_ident
[EI_MAG3
] == ELFMAG3
));
1048 DEFUN (elf_object_p
, (abfd
), bfd
* abfd
)
1050 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1051 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1052 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
1053 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1055 char *shstrtab
; /* Internal copy of section header stringtab */
1056 struct elf_backend_data
*ebd
; /* Use to get ELF_ARCH stored in xvec */
1058 /* Read in the ELF header in external format. */
1060 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
1062 bfd_error
= system_call_error
;
1066 /* Now check to see if we have a valid ELF file, and one that BFD can
1067 make use of. The magic number must match, the address size ('class')
1068 and byte-swapping must match our XVEC entry, and it must have a
1069 section header table (FIXME: See comments re sections at top of this
1072 if (elf_file_p (&x_ehdr
) == false)
1075 bfd_error
= wrong_format
;
1079 if (x_ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
)
1084 unsigned int class = ELFCLASS64
;
1087 unsigned int class = ELFCLASS32
;
1089 if (x_ehdr
.e_ident
[EI_CLASS
] != class)
1093 /* Switch xvec to match the specified byte order. */
1094 switch (x_ehdr
.e_ident
[EI_DATA
])
1096 case ELFDATA2MSB
: /* Big-endian */
1097 if (!abfd
->xvec
->header_byteorder_big_p
)
1100 case ELFDATA2LSB
: /* Little-endian */
1101 if (abfd
->xvec
->header_byteorder_big_p
)
1104 case ELFDATANONE
: /* No data encoding specified */
1105 default: /* Unknown data encoding specified */
1109 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
1110 the tdata pointer in the bfd. */
1112 if (NULL
== (elf_tdata (abfd
) = (struct elf_obj_tdata
*)
1113 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
))))
1115 bfd_error
= no_memory
;
1119 /* FIXME: Any `wrong' exits below here will leak memory (tdata). */
1121 /* Now that we know the byte order, swap in the rest of the header */
1122 i_ehdrp
= elf_elfheader (abfd
);
1123 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
1125 elf_debug_file (i_ehdrp
);
1128 /* If there is no section header table, we're hosed. */
1129 if (i_ehdrp
->e_shoff
== 0)
1132 if (i_ehdrp
->e_type
== ET_EXEC
|| i_ehdrp
->e_type
== ET_DYN
)
1133 abfd
->flags
|= EXEC_P
;
1135 /* Retrieve the architecture information from the xvec and verify
1136 that it matches the machine info stored in the ELF header.
1137 This allows us to resolve ambiguous formats that might not
1138 otherwise be distinguishable. */
1140 ebd
= get_elf_backend_data (abfd
);
1141 switch (i_ehdrp
->e_machine
)
1146 /* Arguably EM_M32 should be bfd_arch_obscure, but then we would
1147 need both an elf32-obscure target and an elf32-unknown target.
1148 The distinction is probably not worth worrying about. */
1149 if (ebd
->arch
!= bfd_arch_unknown
)
1151 bfd_default_set_arch_mach (abfd
, bfd_arch_unknown
, 0);
1154 if (ebd
->arch
!= bfd_arch_sparc
)
1156 bfd_default_set_arch_mach (abfd
, bfd_arch_sparc
, 0);
1159 /* start-sanitize-v9 */
1160 case EM_SPARC64
: /* v9 */
1161 if (ebd
->arch
!= bfd_arch_sparc
) /* v9 */
1162 goto wrong
; /* v9 */
1163 bfd_default_set_arch_mach (abfd
, bfd_arch_sparc
, 0); /* v9 */
1165 /* end-sanitize-v9 */
1168 if (ebd
->arch
!= bfd_arch_i386
)
1170 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, 0);
1173 if (ebd
->arch
!= bfd_arch_m68k
)
1175 bfd_default_set_arch_mach (abfd
, bfd_arch_m68k
, 0);
1178 if (ebd
->arch
!= bfd_arch_m88k
)
1180 bfd_default_set_arch_mach (abfd
, bfd_arch_m88k
, 0);
1183 if (ebd
->arch
!= bfd_arch_i860
)
1185 bfd_default_set_arch_mach (abfd
, bfd_arch_i860
, 0);
1188 if (ebd
->arch
!= bfd_arch_mips
)
1190 bfd_default_set_arch_mach (abfd
, bfd_arch_mips
, 0);
1193 if (ebd
->arch
!= bfd_arch_hppa
)
1195 bfd_default_set_arch_mach (abfd
, bfd_arch_hppa
, 0);
1199 /* Allocate space for a copy of the section header table in
1200 internal form, seek to the section header table in the file,
1201 read it in, and convert it to internal form. As a simple sanity
1202 check, verify that the what BFD thinks is the size of each section
1203 header table entry actually matches the size recorded in the file. */
1205 if (i_ehdrp
->e_shentsize
!= sizeof (x_shdr
))
1207 i_shdrp
= (Elf_Internal_Shdr
*)
1208 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
);
1211 bfd_error
= no_memory
;
1214 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) == -1)
1216 bfd_error
= system_call_error
;
1219 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1221 if (bfd_read ((PTR
) & x_shdr
, sizeof x_shdr
, 1, abfd
)
1224 bfd_error
= system_call_error
;
1227 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
1230 elf_elfsections (abfd
) = i_shdrp
;
1232 /* Read in the string table containing the names of the sections. We
1233 will need the base pointer to this table later. */
1234 /* We read this inline now, so that we don't have to go through
1235 bfd_section_from_shdr with it (since this particular strtab is
1236 used to find all of the ELF section names.) */
1238 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
1242 /* Once all of the section headers have been read and converted, we
1243 can start processing them. Note that the first section header is
1244 a dummy placeholder entry, so we ignore it.
1246 We also watch for the symbol table section and remember the file
1247 offset and section size for both the symbol table section and the
1248 associated string table section. */
1250 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1252 bfd_section_from_shdr (abfd
, shindex
);
1255 /* Remember the entry point specified in the ELF file header. */
1257 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
1263 Takes a bfd and a symbol, returns a pointer to the elf specific area
1264 of the symbol if there is one.
1266 static INLINE elf_symbol_type
*
1267 DEFUN (elf_symbol_from
, (ignore_abfd
, symbol
),
1268 bfd
* ignore_abfd AND
1271 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
1274 if (symbol
->the_bfd
->tdata
.elf_obj_data
== (struct elf_obj_tdata
*) NULL
)
1277 return (elf_symbol_type
*) symbol
;
1280 /* Core files are simply standard ELF formatted files that partition
1281 the file using the execution view of the file (program header table)
1282 rather than the linking view. In fact, there is no section header
1283 table in a core file.
1285 The process status information (including the contents of the general
1286 register set) and the floating point register set are stored in a
1287 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
1288 that allow standard bfd access to the general registers (.reg) and the
1289 floating point registers (.reg2).
1294 DEFUN (elf_core_file_p
, (abfd
), bfd
* abfd
)
1296 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1297 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1298 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
1299 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
1300 unsigned int phindex
;
1302 /* Read in the ELF header in external format. */
1304 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
1306 bfd_error
= system_call_error
;
1310 /* Now check to see if we have a valid ELF file, and one that BFD can
1311 make use of. The magic number must match, the address size ('class')
1312 and byte-swapping must match our XVEC entry, and it must have a
1313 program header table (FIXME: See comments re segments at top of this
1316 if (elf_file_p (&x_ehdr
) == false)
1319 bfd_error
= wrong_format
;
1323 /* FIXME, Check EI_VERSION here ! */
1327 int desired_address_size
= ELFCLASS32
;
1330 int desired_address_size
= ELFCLASS64
;
1333 if (x_ehdr
.e_ident
[EI_CLASS
] != desired_address_size
)
1337 /* Switch xvec to match the specified byte order. */
1338 switch (x_ehdr
.e_ident
[EI_DATA
])
1340 case ELFDATA2MSB
: /* Big-endian */
1341 if (abfd
->xvec
->byteorder_big_p
== false)
1344 case ELFDATA2LSB
: /* Little-endian */
1345 if (abfd
->xvec
->byteorder_big_p
== true)
1348 case ELFDATANONE
: /* No data encoding specified */
1349 default: /* Unknown data encoding specified */
1353 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
1354 the tdata pointer in the bfd. */
1357 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
1358 if (elf_tdata (abfd
) == NULL
)
1360 bfd_error
= no_memory
;
1364 /* FIXME, `wrong' returns from this point onward, leak memory. */
1366 /* Now that we know the byte order, swap in the rest of the header */
1367 i_ehdrp
= elf_elfheader (abfd
);
1368 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
1370 elf_debug_file (i_ehdrp
);
1373 /* If there is no program header, or the type is not a core file, then
1375 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
1378 /* Allocate space for a copy of the program header table in
1379 internal form, seek to the program header table in the file,
1380 read it in, and convert it to internal form. As a simple sanity
1381 check, verify that the what BFD thinks is the size of each program
1382 header table entry actually matches the size recorded in the file. */
1384 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
1386 i_phdrp
= (Elf_Internal_Phdr
*)
1387 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
1390 bfd_error
= no_memory
;
1393 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
1395 bfd_error
= system_call_error
;
1398 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
1400 if (bfd_read ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
1403 bfd_error
= system_call_error
;
1406 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
1409 /* Once all of the program headers have been read and converted, we
1410 can start processing them. */
1412 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
1414 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
1415 if ((i_phdrp
+ phindex
)->p_type
== PT_NOTE
)
1417 elf_corefile_note (abfd
, i_phdrp
+ phindex
);
1421 /* Remember the entry point specified in the ELF file header. */
1423 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
1429 Create ELF output from BFD sections.
1431 Essentially, just create the section header and forget about the program
1436 #if 0 /* not used */
1438 elf_idx_of_sym (abfd
, sym
)
1443 for (i
= 0; i
< abfd
->symcount
; i
++)
1445 if (sym
== (asymbol
*) abfd
->outsymbols
[i
])
1448 BFD_ASSERT ((strcmp (sym
->name
, abfd
->outsymbols
[i
]->name
) == 0)
1449 || (strlen (sym
->name
) == 0));
1458 DEFUN (elf_make_sections
, (abfd
, asect
, obj
),
1460 asection
* asect AND
1463 elf_sect_thunk
*thunk
= (elf_sect_thunk
*) obj
;
1464 /* most of what is in bfd_shdr_from_section goes in here... */
1465 /* and all of these sections generate at *least* one ELF section. */
1469 Elf_Internal_Shdr
*this_hdr
;
1470 this_section
= elf_section_from_bfd_section (abfd
, asect
);
1471 this_hdr
= &thunk
->i_shdrp
[this_section
];
1473 this_hdr
->sh_addr
= asect
->vma
;
1474 this_hdr
->sh_size
= asect
->_raw_size
;
1475 /* contents already set by elf_set_section_contents */
1477 if (asect
->flags
& SEC_RELOC
)
1479 /* emit a reloc section, and thus strtab and symtab... */
1480 Elf_Internal_Shdr
*rela_hdr
;
1481 Elf_Internal_Shdr
*symtab_hdr
;
1482 Elf_External_Rela
*outbound_relocas
;
1483 Elf_External_Rel
*outbound_relocs
;
1485 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1487 symtab_hdr
= &thunk
->i_shdrp
[thunk
->symtab_section
];
1489 if (thunk
->symtab_section
== this_section
+ 1)
1490 rela_section
= thunk
->symtab_section
+ 2; /* symtab + symstrtab */
1492 rela_section
= this_section
+ 1;
1493 rela_hdr
= &thunk
->i_shdrp
[rela_section
];
1494 rela_hdr
->sh_link
= thunk
->symtab_section
;
1495 rela_hdr
->sh_info
= this_section
;
1497 /* orelocation has the data, reloc_count has the count... */
1500 rela_hdr
->sh_type
= SHT_RELA
;
1501 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1502 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* asect
->reloc_count
;
1503 outbound_relocas
= (Elf_External_Rela
*) bfd_alloc (abfd
, rela_hdr
->sh_size
);
1505 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
1507 Elf_Internal_Rela dst_rela
;
1508 Elf_External_Rela
*src_rela
;
1511 ptr
= asect
->orelocation
[idx
];
1512 src_rela
= outbound_relocas
+ idx
;
1513 if (!(abfd
->flags
& EXEC_P
))
1514 dst_rela
.r_offset
= ptr
->address
- asect
->vma
;
1516 dst_rela
.r_offset
= ptr
->address
;
1519 = ELF_R_INFO (elf_symbol_from_bfd_symbol (abfd
, ptr
->sym_ptr_ptr
),
1522 dst_rela
.r_addend
= ptr
->addend
;
1523 elf_swap_reloca_out (abfd
, &dst_rela
, src_rela
);
1526 rela_hdr
->contents
= (void *) outbound_relocas
;
1528 rela_hdr
->sh_flags
= 0;
1529 rela_hdr
->sh_addr
= 0;
1530 rela_hdr
->sh_offset
= 0;
1531 rela_hdr
->sh_addralign
= 0;
1535 /* REL relocations */
1537 rela_hdr
->sh_type
= SHT_REL
;
1538 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rel
);
1539 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* asect
->reloc_count
;
1540 outbound_relocs
= (Elf_External_Rel
*)
1541 bfd_alloc (abfd
, rela_hdr
->sh_size
);
1543 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
1545 Elf_Internal_Rel dst_rel
;
1546 Elf_External_Rel
*src_rel
;
1549 ptr
= asect
->orelocation
[idx
];
1550 src_rel
= outbound_relocs
+ idx
;
1551 if (!(abfd
->flags
& EXEC_P
))
1552 dst_rel
.r_offset
= ptr
->address
- asect
->vma
;
1554 dst_rel
.r_offset
= ptr
->address
;
1557 = ELF_R_INFO (elf_symbol_from_bfd_symbol (abfd
, ptr
->sym_ptr_ptr
),
1560 elf_swap_reloc_out (abfd
, &dst_rel
, src_rel
);
1562 /* Update the addend -- FIXME add 64 bit support. */
1564 fprintf (stderr
, "Updating addend: 0x%.8lx = %d, this_section = %d\n",
1565 (long) ((unsigned char *) (elf_elfsections (abfd
)[this_section
].contents
)
1566 + dst_rel
.r_offset
), ptr
->addend
, this_section
);
1569 bfd_put_32 (abfd
, ptr
->addend
,
1570 (unsigned char *) (elf_elfsections (abfd
)[this_section
].contents
)
1571 + dst_rel
.r_offset
);
1573 rela_hdr
->contents
= (void *) outbound_relocs
;
1575 rela_hdr
->sh_flags
= 0;
1576 rela_hdr
->sh_addr
= 0;
1577 rela_hdr
->sh_offset
= 0;
1578 rela_hdr
->sh_addralign
= 0;
1582 if (asect
->flags
& SEC_ALLOC
)
1584 this_hdr
->sh_flags
|= SHF_ALLOC
;
1585 if (asect
->flags
& SEC_LOAD
)
1587 /* @@ Do something with sh_type? */
1590 if (!(asect
->flags
& SEC_READONLY
))
1591 this_hdr
->sh_flags
|= SHF_WRITE
;
1593 if (asect
->flags
& SEC_CODE
)
1594 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1598 fix_up_strtabs (abfd
, asect
, obj
)
1603 int this_section
= elf_section_from_bfd_section (abfd
, asect
);
1604 elf_sect_thunk
*thunk
= (elf_sect_thunk
*) obj
;
1605 Elf_Internal_Shdr
*this_hdr
= &thunk
->i_shdrp
[this_section
];
1607 /* @@ Check flags! */
1608 if (!strncmp (asect
->name
, ".stab", 5)
1609 && strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
1612 char strtab
[100]; /* @@ fixed size buffer -- eliminate */
1615 strcpy (strtab
, asect
->name
);
1616 strcat (strtab
, "str");
1618 s
= bfd_get_section_by_name (abfd
, strtab
);
1620 fprintf (stderr
, "`%s' -> 0x%x\n", strtab
, s
);
1624 Elf_Internal_Shdr
*s2
= thunk
->i_shdrp
;
1626 for (stridx
= 0; /* ?? */; s2
++, stridx
++)
1628 if (!strcmp (strtab
, s2
->sh_name
+ elf_shstrtab (abfd
)->tab
))
1637 asection
*s2
= abfd
->sections
;
1638 fprintf (stderr
, " not in:");
1641 fprintf (stderr
, " %s", s2
->name
);
1644 fprintf (stderr
, "\n");
1648 this_hdr
->sh_link
= stridx
;
1649 /* @@ Assuming 32 bits! */
1650 this_hdr
->sh_entsize
= 0xc;
1655 DEFUN (elf_fake_sections
, (abfd
, asect
, obj
),
1657 asection
* asect AND
1660 elf_sect_thunk
*thunk
= (elf_sect_thunk
*) obj
;
1661 /* most of what is in bfd_shdr_from_section goes in here... */
1662 /* and all of these sections generate at *least* one ELF section. */
1665 /* check if we're making a PROGBITS section... */
1666 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1667 /* this was too strict... what *do* we want to check here? */
1670 Elf_Internal_Shdr
*this_hdr
;
1671 this_section
= thunk
->i_ehdr
->e_shnum
++;
1672 this_hdr
= &thunk
->i_shdrp
[this_section
];
1674 bfd_add_to_strtab (abfd
, thunk
->shstrtab
, asect
->name
);
1675 /* we need to log the type *now* so that elf_section_from_bfd_section
1676 can find us... have to set rawdata too. */
1677 this_hdr
->rawdata
= (void *) asect
;
1678 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1679 if ((asect
->flags
& SEC_ALLOC
) && (asect
->flags
& SEC_LOAD
))
1680 this_hdr
->sh_type
= SHT_PROGBITS
;
1681 /* @@ Select conditions correctly! */
1682 else if (!strcmp (asect
->name
, ".bss"))
1683 this_hdr
->sh_type
= SHT_NOBITS
;
1685 /* what *do* we put here? */
1686 this_hdr
->sh_type
= SHT_PROGBITS
;
1688 this_hdr
->sh_flags
= 0;
1689 this_hdr
->sh_addr
= 0;
1690 this_hdr
->sh_size
= 0;
1691 this_hdr
->sh_entsize
= 0;
1692 this_hdr
->sh_info
= 0;
1693 this_hdr
->sh_link
= 0;
1694 this_hdr
->sh_offset
= 0;
1698 /* Emit a strtab and symtab, and possibly a reloc section. */
1699 Elf_Internal_Shdr
*rela_hdr
;
1700 Elf_Internal_Shdr
*symtab_hdr
;
1701 Elf_Internal_Shdr
*symstrtab_hdr
;
1703 int symstrtab_section
;
1705 /* Note that only one symtab is used, so just remember it
1707 if (!thunk
->symtab_section
)
1709 thunk
->symtab_section
= thunk
->i_ehdr
->e_shnum
++;
1710 symtab_hdr
= &thunk
->i_shdrp
[thunk
->symtab_section
];
1711 symtab_hdr
->sh_name
=
1712 bfd_add_to_strtab (abfd
, thunk
->shstrtab
, ".symtab");
1713 symtab_hdr
->sh_type
= SHT_SYMTAB
;
1714 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
1716 symstrtab_section
= thunk
->i_ehdr
->e_shnum
++;
1717 BFD_ASSERT (symstrtab_section
== thunk
->symtab_section
+ 1);
1718 symstrtab_hdr
= &thunk
->i_shdrp
[symstrtab_section
];
1719 symtab_hdr
->sh_link
= symstrtab_section
;
1720 symstrtab_hdr
->sh_name
=
1721 bfd_add_to_strtab (abfd
, thunk
->shstrtab
, ".strtab");
1722 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
1724 symtab_hdr
->contents
= 0;
1725 symtab_hdr
->sh_flags
= 0;
1726 symtab_hdr
->sh_addr
= 0;
1727 symtab_hdr
->sh_offset
= 0;
1728 symtab_hdr
->sh_addralign
= 0;
1729 symtab_hdr
->size
= 0;
1730 symstrtab_hdr
->contents
= 0;
1731 symstrtab_hdr
->sh_size
= 0;
1732 symstrtab_hdr
->sh_flags
= 0;
1733 symstrtab_hdr
->sh_addr
= 0;
1734 symstrtab_hdr
->sh_entsize
= 0;
1735 symstrtab_hdr
->sh_link
= 0;
1736 symstrtab_hdr
->sh_info
= 0;
1737 symstrtab_hdr
->sh_offset
= 0;
1738 symstrtab_hdr
->sh_addralign
= 0;
1739 symstrtab_hdr
->size
= 0;
1742 symtab_hdr
= &thunk
->i_shdrp
[thunk
->symtab_section
];
1744 if (asect
->flags
& SEC_RELOC
)
1746 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1748 rela_section
= thunk
->i_ehdr
->e_shnum
++;
1749 rela_hdr
= &thunk
->i_shdrp
[rela_section
];
1751 bfd_add_2_to_strtab (abfd
, thunk
->shstrtab
,
1752 use_rela_p
? ".rela" : ".rel",
1754 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1755 rela_hdr
->sh_link
= thunk
->symtab_section
;
1756 rela_hdr
->sh_info
= this_section
;
1757 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1759 rela_hdr
->sh_flags
= 0;
1760 rela_hdr
->sh_addr
= 0;
1761 rela_hdr
->sh_size
= 0;
1762 rela_hdr
->sh_offset
= 0;
1763 rela_hdr
->sh_addralign
= 0;
1767 if (asect
->flags
& SEC_ALLOC
)
1769 this_hdr
->sh_flags
|= SHF_ALLOC
;
1770 if (asect
->flags
& SEC_LOAD
)
1772 /* @@ Do something with sh_type? */
1775 if (!(asect
->flags
& SEC_READONLY
))
1776 this_hdr
->sh_flags
|= SHF_WRITE
;
1777 if (asect
->flags
& SEC_CODE
)
1778 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1784 xxxINTERNAL_FUNCTION
1788 struct elf_internal_shdr *bfd_elf_locate_sh (bfd *abfd,
1789 struct strtab *strtab,
1790 struct elf_internal_shdr *shdrp,
1794 Helper function to locate an ELF section header given the
1795 name of a BFD section.
1798 static struct elfNAME (internal_shdr
) *
1799 DEFUN (elf_locate_sh
, (abfd
, strtab
, shdrp
, name
),
1801 struct strtab
*strtab AND
1802 struct elfNAME (internal_shdr
) *shdrp AND
1805 Elf_Internal_Shdr
*gotit
= NULL
;
1808 if (shdrp
!= NULL
&& strtab
!= NULL
)
1810 max
= elf_elfheader (abfd
)->e_shnum
;
1811 for (i
= 1; i
< max
; i
++)
1813 if (!strcmp (strtab
->tab
+ shdrp
[i
].sh_name
, name
))
1822 /* Map symbol from it's internal number to the external number, moving
1823 all local symbols to be at the head of the list. */
1829 if (sym
->flags
& BSF_GLOBAL
)
1831 if (sym
->flags
& BSF_LOCAL
)
1835 if (sym
->section
== &bfd_und_section
)
1837 if (bfd_is_com_section (sym
->section
))
1839 if (sym
->flags
& (BSF_LOCAL
| BSF_SECTION_SYM
| BSF_FILE
))
1845 DEFUN (elf_map_symbols
, (abfd
), bfd
* abfd
)
1847 int symcount
= bfd_get_symcount (abfd
);
1848 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1850 int num_globals
= 0;
1851 int num_locals2
= 0;
1852 int num_globals2
= 0;
1853 int num_sections
= 0;
1859 fprintf (stderr
, "elf_map_symbols\n");
1863 /* Add local symbols for each allocated section
1864 FIXME -- we should only put out symbols for sections that
1865 are actually relocated against. */
1866 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1868 if (/*asect->flags & (SEC_LOAD | SEC_DATA | SEC_CODE)*/1)
1875 syms
= (asymbol
**) bfd_realloc (abfd
, syms
,
1876 ((symcount
+ num_sections
+ 1)
1877 * sizeof (asymbol
*)));
1879 syms
= (asymbol
**) bfd_alloc (abfd
,
1880 (num_sections
+ 1) * sizeof (asymbol
*));
1882 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1884 if (/* asect->flags & (SEC_LOAD | SEC_DATA | SEC_CODE) */ 1)
1886 asymbol
*sym
= syms
[symcount
++] = bfd_make_empty_symbol (abfd
);
1887 sym
->the_bfd
= abfd
;
1888 sym
->name
= asect
->name
;
1889 sym
->value
= asect
->vma
;
1890 sym
->flags
= BSF_SECTION_SYM
;
1891 sym
->section
= asect
;
1895 syms
[symcount
] = (asymbol
*) 0;
1896 bfd_set_symtab (abfd
, syms
, symcount
);
1899 elf_symtab_map (abfd
) = symtab_map
1900 = (int *) bfd_alloc (abfd
, symcount
* sizeof (int *));
1902 /* Identify and classify all of the symbols. */
1903 for (idx
= 0; idx
< symcount
; idx
++)
1905 if (!sym_is_global (syms
[idx
]))
1911 /* Now provide mapping information. Add +1 for skipping over the
1913 for (idx
= 0; idx
< symcount
; idx
++)
1915 if (!sym_is_global (syms
[idx
]))
1916 symtab_map
[idx
] = 1 + num_locals2
++;
1918 symtab_map
[idx
] = 1 + num_locals
+ num_globals2
++;
1921 elf_num_locals (abfd
) = num_locals
;
1922 elf_num_globals (abfd
) = num_globals
;
1927 DEFUN (elf_compute_section_file_positions
, (abfd
), bfd
* abfd
)
1929 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1930 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1931 struct strtab
*shstrtab
;
1932 int count
, maxsections
;
1935 if (!elf_shstrtab (abfd
))
1937 i_ehdrp
= elf_elfheader (abfd
); /* build new header in tdata memory */
1938 shstrtab
= bfd_new_strtab (abfd
);
1940 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
1941 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
1942 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
1943 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
1946 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS32
;
1949 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS64
;
1951 i_ehdrp
->e_ident
[EI_DATA
] =
1952 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
1953 i_ehdrp
->e_ident
[EI_VERSION
] = EV_CURRENT
;
1955 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
1956 i_ehdrp
->e_ident
[count
] = 0;
1958 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
) ? ET_EXEC
: ET_REL
;
1959 switch (bfd_get_arch (abfd
))
1961 case bfd_arch_unknown
:
1962 i_ehdrp
->e_machine
= EM_NONE
;
1964 case bfd_arch_sparc
:
1965 i_ehdrp
->e_machine
= EM_SPARC
;
1966 /* start-sanitize-v9 */
1967 #if ARCH_SIZE == 64 /* v9 */
1968 i_ehdrp
->e_machine
= EM_SPARC64
; /* v9 */
1970 /* end-sanitize-v9 */
1973 i_ehdrp
->e_machine
= EM_386
;
1976 i_ehdrp
->e_machine
= EM_68K
;
1979 i_ehdrp
->e_machine
= EM_88K
;
1982 i_ehdrp
->e_machine
= EM_860
;
1984 case bfd_arch_mips
: /* MIPS Rxxxx */
1985 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
1988 i_ehdrp
->e_machine
= EM_HPPA
;
1990 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
1992 i_ehdrp
->e_machine
= EM_NONE
;
1994 i_ehdrp
->e_version
= EV_CURRENT
;
1995 i_ehdrp
->e_ehsize
= sizeof (Elf_External_Ehdr
);
1997 /* no program header, for now. */
1998 i_ehdrp
->e_phoff
= 0;
1999 i_ehdrp
->e_phentsize
= 0;
2000 i_ehdrp
->e_phnum
= 0;
2002 /* each bfd section is section header entry */
2003 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
2004 i_ehdrp
->e_shentsize
= sizeof (Elf_External_Shdr
);
2006 /* figure at most each section can have a rel, strtab, symtab */
2007 maxsections
= 4 * bfd_count_sections (abfd
) + 2;
2009 i_ehdrp
->e_shoff
= i_ehdrp
->e_ehsize
;
2011 /* and we'll just have to fix up the offsets later. */
2012 /* outbase += i_ehdr.e_shentsize * i_ehdr.e_shnum; */
2014 i_shdrp
= (Elf_Internal_Shdr
*)
2015 bfd_alloc (abfd
, sizeof (*i_shdrp
) * maxsections
);
2018 bfd_error
= no_memory
;
2021 for (count
= 0; count
< maxsections
; count
++)
2023 i_shdrp
[count
].rawdata
= 0;
2024 i_shdrp
[count
].contents
= 0;
2028 i_shdrp
[0].sh_name
= 0;
2029 i_shdrp
[0].sh_type
= SHT_NULL
;
2030 i_shdrp
[0].sh_flags
= 0;
2031 i_shdrp
[0].sh_addr
= 0;
2032 i_shdrp
[0].sh_offset
= 0;
2033 i_shdrp
[0].sh_size
= 0;
2034 i_shdrp
[0].sh_link
= SHN_UNDEF
;
2035 i_shdrp
[0].sh_info
= 0;
2036 i_shdrp
[0].sh_addralign
= 0;
2037 i_shdrp
[0].sh_entsize
= 0;
2039 i_ehdrp
->e_shnum
= 1;
2041 elf_elfsections (abfd
) = i_shdrp
;
2042 elf_shstrtab (abfd
) = shstrtab
;
2044 est
.i_ehdr
= elf_elfheader (abfd
);
2045 est
.i_shdrp
= elf_elfsections (abfd
);
2046 est
.shstrtab
= elf_shstrtab (abfd
);
2047 est
.symtab_section
= 0; /* elf_fake_sections fills it in */
2049 elf_map_symbols (abfd
);
2050 bfd_map_over_sections (abfd
, elf_fake_sections
, &est
);
2053 asection
*psecp
= (asection
*) 0;
2055 Elf_Internal_Shdr
*shp
;
2057 for (idx
= 0, secp
= abfd
->sections
; secp
; idx
++, secp
= secp
->next
)
2059 if (psecp
== (asection
*) 0)
2061 secp
->filepos
= est
.i_ehdr
->e_shoff
+ (est
.i_ehdr
->e_shnum
+ 1) * sizeof (Elf_External_Shdr
);
2065 secp
->filepos
= psecp
->filepos
+ psecp
->_cooked_size
;
2067 shp
= elf_locate_sh (abfd
, est
.shstrtab
, est
.i_shdrp
, secp
->name
);
2069 shp
->sh_offset
= secp
->filepos
;
2073 elf_onesymtab (abfd
) = est
.symtab_section
;
2078 DEFUN (elf_write_phdrs
, (abfd
, i_ehdrp
, i_phdrp
, phdr_cnt
),
2080 Elf_Internal_Ehdr
* i_ehdrp AND
2081 Elf_Internal_Phdr
* i_phdrp AND
2082 Elf32_Half phdr_cnt
)
2084 /* first program header entry goes after the file header */
2085 int outbase
= i_ehdrp
->e_ehsize
;
2087 Elf_External_Phdr x_phdr
;
2089 for (i
= 0; i
< phdr_cnt
; i
++)
2091 elf_swap_phdr_out (abfd
, i_phdrp
+ i
, &x_phdr
);
2092 bfd_seek (abfd
, outbase
, SEEK_SET
);
2093 bfd_write ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
);
2094 outbase
+= sizeof (x_phdr
);
2100 static Elf_Internal_Phdr
*
2101 DEFUN (elf_build_phdrs
, (abfd
, i_ehdrp
, i_shdrp
, phdr_cnt
),
2103 Elf_Internal_Ehdr
* i_ehdrp AND
2104 Elf_Internal_Shdr
* i_shdrp AND
2105 Elf32_Half
* phdr_cnt
)
2107 Elf_Internal_Phdr
*phdr_buf
;
2110 1. The program header table is *not* loaded as part
2111 of the memory image of the program. If this
2112 changes later, the PT_PHDR entry must come first.
2113 2. there is currently no support for program header
2114 entries of type PT_PHDR, PT_DYNAMIC, PT_INTERP,
2117 /* A. Figure out how many program header table entries are needed
2118 1. PT_LOAD for the text segment
2119 2. PT_LOAD for the data segment
2120 Then, reserve space for one more pointer. This will be NULL
2121 to indicate the end of the program header table. */
2123 #ifdef PHDRS_INCLUDED
2126 /* XXX right now, execve() expects exactly 3 PT entries on HPPA-OSF. */
2130 phdr_buf
= (Elf_Internal_Phdr
*) bfd_xmalloc (((*phdr_cnt
) + 1)
2132 sizeof (Elf_Internal_Phdr
));
2135 #ifdef PHDRS_INCLUDED
2136 /* B. Fill in the PT_PHDR entry. */
2141 /* C. Fill in the PT_LOAD entry for the text segment. */
2143 phdr_buf
[idx
].p_type
= PT_LOAD
;
2145 /* get virtual/physical address from .text section */
2146 phdr_buf
[idx
].p_vaddr
= bfd_get_section_by_name (abfd
, ".text")->vma
;
2147 phdr_buf
[idx
].p_paddr
= 0; /* XXX */
2149 /* Ultimately, we would like the size of the .text load
2150 segment to be the sum of the following sections:
2151 the program header table itself
2162 But, right now, it will be the sum of the following sections:
2167 static char *CONST ld_sect_names
[] =
2168 {".text", ".rodata", NULL
};
2172 for (i
= 0; ld_sect_names
[i
]; i
++)
2174 asection
*asect
= bfd_get_section_by_name (abfd
,
2178 ld_size
+= bfd_section_size (abfd
, asect
);
2180 phdr_buf
[idx
].p_filesz
= ld_size
;
2181 /* XXX: need to fix this */
2182 phdr_buf
[idx
].p_memsz
= ld_size
;
2184 phdr_buf
[idx
].p_flags
= PF_R
+ PF_X
;
2185 phdr_buf
[idx
].p_align
=
2186 bfd_get_section_by_name (abfd
, ".text")->alignment_power
;
2190 /* D. Fill in the PT_LOAD entry for the data segment. */
2192 phdr_buf
[idx
].p_type
= PT_LOAD
;
2194 /* get virtual/physical address from .data section */
2195 phdr_buf
[idx
].p_vaddr
= bfd_get_section_by_name (abfd
, ".data")->vma
;
2196 phdr_buf
[idx
].p_paddr
= 0; /* XXX */
2198 /* Ultimately, we would like the size of the data load segment
2199 to be the sum of the following sections:
2200 the PT_DYNAMIC program header table entry
2206 But, right now, it will be the sum of the following sections:
2210 static char *CONST ld_sect_names
[] =
2215 for (i
= 0; ld_sect_names
[i
]; i
++)
2217 asection
*asect
= bfd_get_section_by_name (abfd
,
2221 ld_size
+= bfd_section_size (abfd
, asect
);
2223 phdr_buf
[idx
].p_filesz
= ld_size
;
2224 /* XXX: need to fix this */
2225 phdr_buf
[idx
].p_memsz
= ld_size
;
2227 phdr_buf
[idx
].p_flags
= PF_R
+ PF_W
+ PF_X
;
2228 phdr_buf
[idx
].p_align
2229 = bfd_get_section_by_name (abfd
, ".data")->alignment_power
;
2233 /* E. Fill in the PT_LOAD entry for the bss segment. */
2235 phdr_buf
[idx
].p_type
= PT_LOAD
;
2237 /* get virtual/physical address from .data section */
2238 phdr_buf
[idx
].p_vaddr
= bfd_get_section_by_name (abfd
, ".bss")->vma
;
2239 phdr_buf
[idx
].p_paddr
= 0; /* XXX */
2242 static char *CONST ld_sect_names
[] =
2247 for (i
= 0; ld_sect_names
[i
]; i
++)
2249 asection
*asect
= bfd_get_section_by_name (abfd
,
2253 ld_size
+= bfd_section_size (abfd
, asect
);
2255 phdr_buf
[idx
].p_filesz
= 0;
2256 /* XXX: need to fix this */
2257 phdr_buf
[idx
].p_memsz
= ld_size
;
2259 phdr_buf
[idx
].p_flags
= PF_R
+ PF_W
+ PF_X
;
2260 phdr_buf
[idx
].p_align
2261 = bfd_get_section_by_name (abfd
, ".bss")->alignment_power
;
2265 /* F. Set up the "end of program header table" sentinel. */
2267 memset ((char *) (phdr_buf
+ idx
), 0, sizeof (Elf_Internal_Phdr
));
2270 BFD_ASSERT (idx
- 1 == *phdr_cnt
);
2276 DEFUN (elf_write_object_contents
, (abfd
), bfd
* abfd
)
2278 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
2279 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2280 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2281 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
2282 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
2289 struct strtab
*shstrtab
;
2291 if (abfd
->output_has_begun
== false)
2293 elf_compute_section_file_positions (abfd
);
2294 abfd
->output_has_begun
= true;
2297 i_ehdrp
= elf_elfheader (abfd
);
2298 i_shdrp
= elf_elfsections (abfd
);
2299 shstrtab
= elf_shstrtab (abfd
);
2301 est
.i_ehdr
= i_ehdrp
;
2302 est
.i_shdrp
= i_shdrp
;
2303 est
.shstrtab
= shstrtab
;
2304 est
.symtab_section
= elf_onesymtab (abfd
); /* filled in by elf_fake */
2306 bfd_map_over_sections (abfd
, elf_make_sections
, &est
);
2307 bfd_map_over_sections (abfd
, fix_up_strtabs
, &est
);
2309 /* Dump out the symtabs. */
2311 int symcount
= bfd_get_symcount (abfd
);
2312 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2313 struct strtab
*stt
= bfd_new_strtab (abfd
);
2314 Elf_Internal_Shdr
*symtab_hdr
;
2315 Elf_Internal_Shdr
*symstrtab_hdr
;
2316 int symstrtab_section
;
2317 Elf_External_Sym
*outbound_syms
;
2320 symtab_hdr
= &i_shdrp
[est
.symtab_section
];
2321 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2322 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
2323 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2324 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2326 /* see assert in elf_fake_sections that supports this: */
2327 symstrtab_section
= est
.symtab_section
+ 1;
2328 symstrtab_hdr
= &i_shdrp
[symstrtab_section
];
2329 symtab_hdr
->sh_link
= symstrtab_section
;
2330 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2332 outbound_syms
= (Elf_External_Sym
*)
2333 bfd_alloc (abfd
, (1 + symcount
) * sizeof (Elf_External_Sym
));
2334 /* now generate the data (for "contents") */
2336 /* Fill in zeroth symbol and swap it out. */
2337 Elf_Internal_Sym sym
;
2343 sym
.st_shndx
= SHN_UNDEF
;
2344 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
2346 for (idx
= 0; idx
< symcount
; idx
++)
2348 Elf_Internal_Sym sym
;
2349 bfd_vma value
= syms
[idx
]->value
;
2351 if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2352 /* Section symbols have no names. */
2355 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
2357 if (bfd_is_com_section (syms
[idx
]->section
))
2359 /* ELF common symbols put the alignment into the `value' field,
2360 and the size into the `size' field. This is backwards from
2361 how BFD handles it, so reverse it here. */
2362 sym
.st_size
= value
;
2363 /* Should retrieve this from somewhere... */
2368 value
+= syms
[idx
]->section
->output_section
->vma
2369 + syms
[idx
]->section
->output_offset
;
2370 sym
.st_value
= value
;
2372 sym
.st_size
= (elf_symbol_from (abfd
, syms
[idx
]))->internal_elf_sym
.st_size
;
2375 if (bfd_is_com_section (syms
[idx
]->section
))
2376 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2377 else if (syms
[idx
]->section
== &bfd_und_section
)
2378 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2379 else if (syms
[idx
]->flags
& BSF_WEAK
)
2380 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, STT_OBJECT
);
2381 else if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2382 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2383 else if (syms
[idx
]->flags
& BSF_FILE
)
2384 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2385 else if (syms
[idx
]->flags
& (BSF_GLOBAL
| BSF_EXPORT
))
2387 if (syms
[idx
]->flags
& BSF_FUNCTION
)
2388 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_FUNC
);
2390 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2392 else if (syms
[idx
]->flags
& BSF_LOCAL
)
2394 if (syms
[idx
]->flags
& BSF_FUNCTION
)
2395 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FUNC
);
2397 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_OBJECT
);
2400 /* Default to local if flag isn't set at all. */
2401 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_OBJECT
);
2404 if (syms
[idx
]->section
)
2406 elf_section_from_bfd_section (abfd
,
2407 syms
[idx
]->section
->output_section
);
2409 sym
.st_shndx
= SHN_UNDEF
;
2411 elf_swap_symbol_out (abfd
, &sym
,
2412 outbound_syms
+ elf_symtab_map (abfd
)[idx
]);
2415 symtab_hdr
->contents
= (void *) outbound_syms
;
2416 symstrtab_hdr
->contents
= (void *) stt
->tab
;
2417 symstrtab_hdr
->sh_size
= stt
->length
;
2418 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2420 symstrtab_hdr
->sh_flags
= 0;
2421 symstrtab_hdr
->sh_addr
= 0;
2422 symstrtab_hdr
->sh_entsize
= 0;
2423 symstrtab_hdr
->sh_link
= 0;
2424 symstrtab_hdr
->sh_info
= 0;
2425 symstrtab_hdr
->sh_offset
= 0;
2426 symstrtab_hdr
->sh_addralign
= 0;
2427 symstrtab_hdr
->size
= 0;
2430 /* put the strtab out too... */
2432 Elf_Internal_Shdr
*this_hdr
;
2435 this_section
= i_ehdrp
->e_shnum
++;
2436 i_ehdrp
->e_shstrndx
= this_section
;
2437 this_hdr
= &i_shdrp
[this_section
];
2438 this_hdr
->sh_name
= bfd_add_to_strtab (abfd
, shstrtab
, ".shstrtab");
2439 this_hdr
->sh_type
= SHT_STRTAB
;
2440 this_hdr
->sh_size
= shstrtab
->length
;
2441 this_hdr
->contents
= (void *) shstrtab
->tab
;
2443 this_hdr
->sh_flags
= 0;
2444 this_hdr
->sh_addr
= 0;
2445 this_hdr
->sh_entsize
= 0;
2446 this_hdr
->sh_link
= 0;
2447 this_hdr
->sh_info
= 0;
2448 this_hdr
->sh_offset
= 0;
2449 this_hdr
->sh_addralign
= 0;
2453 outbase
= i_ehdrp
->e_ehsize
;
2455 /* if we're building an executable, we'll need a program header table */
2456 if (abfd
->flags
& EXEC_P
)
2458 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
2460 /* elf_build_phdrs() returns a (NULL-terminated) array of
2461 Elf_Internal_Phdrs */
2462 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
2463 i_ehdrp
->e_phoff
= i_ehdrp
->e_ehsize
;
2464 i_ehdrp
->e_shoff
= i_ehdrp
->e_phoff
+ (i_ehdrp
->e_phentsize
2465 * i_ehdrp
->e_phnum
);
2468 /* swap the header before spitting it out... */
2471 elf_debug_file (i_ehdrp
);
2473 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
2474 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
2475 bfd_write ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
);
2478 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
2479 outbase
+= i_ehdrp
->e_shentsize
* i_ehdrp
->e_shnum
;
2481 /* find the next available location in the file
2482 (just beyond the sections that have already been written). */
2484 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2486 if (i_shdrp
[count
].sh_offset
&& outbase
< i_shdrp
[count
].sh_offset
)
2487 outbase
= i_shdrp
[count
].sh_offset
+ i_shdrp
[count
].sh_size
;
2490 /* now we fix up the offsets... */
2491 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2493 if (i_shdrp
[count
].sh_offset
== 0)
2494 i_shdrp
[count
].sh_offset
= outbase
;
2495 outbase
+= i_shdrp
[count
].sh_size
;
2498 /* If we're building an executable, fixup the program header table
2501 @@ For now, assume that the entries are in a fixed order: text,
2504 if (abfd
->flags
& EXEC_P
)
2506 static char *CONST section_name
[] =
2507 {".text", ".data", ".bss"};
2509 for (count
= 0; count
< 3; count
++)
2511 asection
*asect
= bfd_get_section_by_name (abfd
, section_name
[count
]);
2512 int sh_idx
= elf_section_from_bfd_section (abfd
, asect
);
2514 i_phdrp
[count
].p_offset
= i_shdrp
[sh_idx
].sh_offset
;
2517 /* write out the program header table entries */
2518 elf_write_phdrs (abfd
, i_ehdrp
, i_phdrp
, i_ehdrp
->e_phnum
);
2521 /* at this point we've concocted all the ELF sections... */
2522 x_shdrp
= (Elf_External_Shdr
*)
2523 bfd_alloc (abfd
, sizeof (*x_shdrp
) * (i_ehdrp
->e_shnum
));
2526 bfd_error
= no_memory
;
2530 for (count
= 0, scnt
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2533 elf_debug_section (shstrtab
->tab
+ i_shdrp
[scnt
].sh_name
, count
, i_shdrp
+ scnt
);
2535 elf_swap_shdr_out (abfd
, i_shdrp
+ count
, x_shdrp
+ scnt
);
2538 bfd_write ((PTR
) x_shdrp
, sizeof (*x_shdrp
), i_ehdrp
->e_shnum
, abfd
);
2539 /* need to dump the string table too... */
2541 /* after writing the headers, we need to write the sections too... */
2542 nsect
= abfd
->sections
;
2543 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2545 if (i_shdrp
[count
].contents
)
2547 bfd_seek (abfd
, i_shdrp
[count
].sh_offset
, SEEK_SET
);
2548 bfd_write (i_shdrp
[count
].contents
, i_shdrp
[count
].sh_size
, 1, abfd
);
2555 /* Given an index of a section, retrieve a pointer to it. Note
2556 that for our purposes, sections are indexed by {1, 2, ...} with
2557 0 being an illegal index. */
2559 /* In the original, each ELF section went into exactly one BFD
2560 section. This doesn't really make sense, so we need a real mapping.
2561 The mapping has to hide in the Elf_Internal_Shdr since asection
2562 doesn't have anything like a tdata field... */
2565 DEFUN (section_from_elf_index
, (abfd
, index
),
2569 /* @@ Is bfd_com_section really correct in all the places it could
2570 be returned from this routine? */
2572 if (index
== SHN_ABS
)
2573 return &bfd_com_section
; /* not abs? */
2574 if (index
== SHN_COMMON
)
2575 return &bfd_com_section
;
2578 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
2579 Elf_Internal_Shdr
*hdr
= i_shdrp
+ index
;
2581 switch (hdr
->sh_type
)
2583 /* ELF sections that map to BFD sections */
2587 bfd_section_from_shdr (abfd
, index
);
2588 return (struct sec
*) hdr
->rawdata
;
2591 return (struct sec
*) &bfd_abs_section
;
2596 /* given a section, search the header to find them... */
2598 DEFUN (elf_section_from_bfd_section
, (abfd
, asect
),
2602 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
2604 Elf_Internal_Shdr
*hdr
;
2605 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2607 if (asect
== &bfd_abs_section
)
2609 if (asect
== &bfd_com_section
)
2612 for (index
= 0; index
< maxindex
; index
++)
2614 hdr
= &i_shdrp
[index
];
2615 switch (hdr
->sh_type
)
2617 /* ELF sections that map to BFD sections */
2622 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2633 /* given a symbol, return the bfd index for that symbol. */
2635 DEFUN (elf_symbol_from_bfd_symbol
, (abfd
, asym_ptr_ptr
),
2637 struct symbol_cache_entry
**asym_ptr_ptr
)
2639 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2640 CONST
char *name
= asym_ptr
->name
;
2642 int symcount
= bfd_get_symcount (abfd
);
2643 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2645 /* FIXME -- there has to be a better way than linear search. */
2646 for (idx
= 0; idx
< symcount
; idx
++)
2648 if (syms
[idx
] == asym_ptr
2649 || (name
== syms
[idx
]->name
&& name
)
2650 || ((asym_ptr
->flags
& BSF_SECTION_SYM
)
2651 && (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2652 && asym_ptr
->section
== syms
[idx
]->section
))
2656 if (idx
>= symcount
)
2659 fprintf (stderr
, "bfd app err: can't find sym `%s' in symtab\n",
2663 idx
= elf_symtab_map (abfd
)[idx
];
2667 flagword flags
= asym_ptr
->flags
;
2670 "elfsym<-bfdsym %.8lx `%s' sec=%s symnum=%d {",
2671 (long) asym_ptr
, asym_ptr
->name
, asym_ptr
->section
->name
, idx
);
2673 if (flags
== BSF_NO_FLAGS
)
2674 fprintf (stderr
, " none");
2676 if (flags
& BSF_LOCAL
)
2677 fprintf (stderr
, " local");
2679 if (flags
& BSF_GLOBAL
)
2680 fprintf (stderr
, " global");
2682 if (flags
& BSF_EXPORT
)
2683 fprintf (stderr
, " export");
2685 if (flags
& BSF_DEBUGGING
)
2686 fprintf (stderr
, " debugging");
2688 if (flags
& BSF_KEEP
)
2689 fprintf (stderr
, " keep");
2691 if (flags
& BSF_KEEP_G
)
2692 fprintf (stderr
, " keep_g");
2694 if (flags
& BSF_WEAK
)
2695 fprintf (stderr
, " weak");
2697 if (flags
& BSF_SECTION_SYM
)
2698 fprintf (stderr
, " section_sym");
2700 if (flags
& BSF_OLD_COMMON
)
2701 fprintf (stderr
, " old_common");
2703 if (flags
& BSF_NOT_AT_END
)
2704 fprintf (stderr
, " not_at_end");
2706 if (flags
& BSF_CONSTRUCTOR
)
2707 fprintf (stderr
, " constructor");
2709 if (flags
& BSF_WARNING
)
2710 fprintf (stderr
, " warning");
2712 if (flags
& BSF_INDIRECT
)
2713 fprintf (stderr
, " indirect");
2715 if (flags
& BSF_FILE
)
2716 fprintf (stderr
, " file");
2718 if (flags
& BSF_FUNCTION
)
2719 fprintf (stderr
, " function");
2721 fputs (" }\n", stderr
);
2730 DEFUN (elf_slurp_symbol_table
, (abfd
, symptrs
),
2732 asymbol
** symptrs
) /* Buffer for generated bfd symbols */
2734 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
2735 Elf_Internal_Shdr
*hdr
= i_shdrp
+ elf_onesymtab (abfd
);
2736 int symcount
; /* Number of external ELF symbols */
2738 elf_symbol_type
*sym
; /* Pointer to current bfd symbol */
2739 elf_symbol_type
*symbase
; /* Buffer for generated bfd symbols */
2740 Elf_Internal_Sym i_sym
;
2741 Elf_External_Sym
*x_symp
;
2743 /* this is only valid because there is only one symtab... */
2744 /* FIXME: This is incorrect, there may also be a dynamic symbol
2745 table which is a subset of the full symbol table. We either need
2746 to be prepared to read both (and merge them) or ensure that we
2747 only read the full symbol table. Currently we only get called to
2748 read the full symbol table. -fnf */
2749 if (bfd_get_outsymbols (abfd
) != NULL
)
2754 /* Read each raw ELF symbol, converting from external ELF form to
2755 internal ELF form, and then using the information to create a
2756 canonical bfd symbol table entry.
2758 Note that we allocate the initial bfd canonical symbol buffer
2759 based on a one-to-one mapping of the ELF symbols to canonical
2760 symbols. We actually use all the ELF symbols, so there will be no
2761 space left over at the end. When we have all the symbols, we
2762 build the caller's pointer vector. */
2764 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2766 bfd_error
= system_call_error
;
2770 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2771 symbase
= (elf_symbol_type
*) bfd_zalloc (abfd
, symcount
* sizeof (elf_symbol_type
));
2774 /* Temporarily allocate room for the raw ELF symbols. */
2775 x_symp
= (Elf_External_Sym
*) bfd_xmalloc (symcount
* sizeof (Elf_External_Sym
));
2777 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
2778 != symcount
* sizeof (Elf_External_Sym
))
2780 free ((PTR
) x_symp
);
2781 bfd_error
= system_call_error
;
2784 /* Skip first symbol, which is a null dummy. */
2785 for (i
= 1; i
< symcount
; i
++)
2787 elf_swap_symbol_in (abfd
, x_symp
+ i
, &i_sym
);
2788 memcpy (&sym
->internal_elf_sym
, &i_sym
, sizeof (Elf_Internal_Sym
));
2789 memcpy (&sym
->native_elf_sym
, x_symp
+ i
, sizeof (Elf_External_Sym
));
2790 sym
->symbol
.the_bfd
= abfd
;
2792 sym
->symbol
.name
= elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2795 sym
->symbol
.value
= i_sym
.st_value
;
2797 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
2799 sym
->symbol
.section
= section_from_elf_index (abfd
, i_sym
.st_shndx
);
2801 else if (i_sym
.st_shndx
== SHN_ABS
)
2803 sym
->symbol
.section
= &bfd_abs_section
;
2805 else if (i_sym
.st_shndx
== SHN_COMMON
)
2807 sym
->symbol
.section
= &bfd_com_section
;
2808 /* Elf puts the alignment into the `value' field, and the size
2809 into the `size' field. BFD wants to see the size in the
2810 value field, and doesn't care (at the moment) about the
2812 sym
->symbol
.value
= i_sym
.st_size
;
2814 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2816 sym
->symbol
.section
= &bfd_und_section
;
2819 sym
->symbol
.section
= &bfd_abs_section
;
2821 sym
->symbol
.value
-= sym
->symbol
.section
->vma
;
2823 switch (ELF_ST_BIND (i_sym
.st_info
))
2826 sym
->symbol
.flags
|= BSF_LOCAL
;
2829 sym
->symbol
.flags
|= (BSF_GLOBAL
| BSF_EXPORT
);
2832 sym
->symbol
.flags
|= BSF_WEAK
;
2836 switch (ELF_ST_TYPE (i_sym
.st_info
))
2839 sym
->symbol
.flags
|= BSF_SECTION_SYM
| BSF_DEBUGGING
;
2842 sym
->symbol
.flags
|= BSF_FILE
| BSF_DEBUGGING
;
2845 sym
->symbol
.flags
|= BSF_FUNCTION
;
2848 /* Is this a definition of $global$? If so, keep it because it will be
2849 needd if any relocations are performed. */
2850 if (!strcmp (sym
->symbol
.name
, "$global$")
2851 && sym
->symbol
.section
!= &bfd_und_section
)
2853 /* @@ Why is this referring to backend data and not a field of
2855 struct elf_backend_data
*be_data
= (struct elf_backend_data
*) abfd
->xvec
->backend_data
;
2857 be_data
->global_sym
= (PTR
) sym
;
2862 /* We rely on the zalloc to clear out the final symbol entry. */
2864 /* obj_raw_syms macro uses a cast... */
2865 elf_tdata (abfd
)->raw_syms
= (PTR
) x_symp
;
2867 bfd_get_symcount (abfd
) = symcount
= sym
- symbase
;
2869 /* Fill in the user's symbol pointer vector if needed. */
2873 while (symcount
-- > 0)
2875 *symptrs
++ = &sym
->symbol
;
2878 *symptrs
= 0; /* Final null pointer */
2884 /* Return the number of bytes required to hold the symtab vector.
2886 Note that we base it on the count plus 1, since we will null terminate
2887 the vector allocated based on this size. However, the ELF symbol table
2888 always has a dummy entry as symbol #0, so it ends up even. */
2891 DEFUN (elf_get_symtab_upper_bound
, (abfd
), bfd
* abfd
)
2893 unsigned int symcount
;
2894 unsigned int symtab_size
= 0;
2895 Elf_Internal_Shdr
*i_shdrp
;
2896 Elf_Internal_Shdr
*hdr
;
2898 i_shdrp
= elf_elfsections (abfd
);
2899 if (i_shdrp
!= NULL
)
2901 hdr
= i_shdrp
+ elf_onesymtab (abfd
);
2902 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2903 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
));
2909 This function return the number of bytes required to store the
2910 relocation information associated with section <<sect>>
2911 attached to bfd <<abfd>>
2915 elf_get_reloc_upper_bound (abfd
, asect
)
2919 if (asect
->flags
& SEC_RELOC
)
2921 /* either rel or rela */
2922 return asect
->_raw_size
;
2929 DEFUN (elf_slurp_reloca_table
, (abfd
, asect
, symbols
),
2934 Elf_External_Rela
*native_relocs
;
2935 arelent
*reloc_cache
;
2940 if (asect
->relocation
)
2942 if (asect
->reloc_count
== 0)
2944 if (asect
->flags
& SEC_CONSTRUCTOR
)
2947 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2948 native_relocs
= (Elf_External_Rela
*)
2949 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rela
));
2950 bfd_read ((PTR
) native_relocs
,
2951 sizeof (Elf_External_Rela
), asect
->reloc_count
, abfd
);
2953 reloc_cache
= (arelent
*)
2954 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2958 bfd_error
= no_memory
;
2962 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2964 #ifdef RELOC_PROCESSING
2965 Elf_Internal_Rela dst
;
2966 Elf_External_Rela
*src
;
2968 cache_ptr
= reloc_cache
+ idx
;
2969 src
= native_relocs
+ idx
;
2970 elf_swap_reloca_in (abfd
, src
, &dst
);
2972 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2974 Elf_Internal_Rela dst
;
2975 Elf_External_Rela
*src
;
2977 cache_ptr
= reloc_cache
+ idx
;
2978 src
= native_relocs
+ idx
;
2980 elf_swap_reloca_in (abfd
, src
, &dst
);
2982 if (asect
->flags
& SEC_RELOC
)
2984 /* relocatable, so the offset is off of the section */
2985 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2989 /* non-relocatable, so the offset a virtual address */
2990 cache_ptr
->address
= dst
.r_offset
;
2992 /* ELF_R_SYM(dst.r_info) is the symbol table offset; subtract 1
2993 because the first entry is NULL. */
2994 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
2996 /* Is it an ELF section symbol? If so, translate it into a
2997 BFD section symbol. */
2998 asymbol
*s
= *(cache_ptr
->sym_ptr_ptr
);
2999 if (s
->flags
& BSF_SECTION_SYM
)
3000 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
3002 cache_ptr
->addend
= dst
.r_addend
;
3004 /* Fill in the cache_ptr->howto field from dst.r_type */
3006 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
3007 (*ebd
->elf_info_to_howto
) (abfd
, cache_ptr
, &dst
);
3012 asect
->relocation
= reloc_cache
;
3018 elf_debug_section (str
, num
, hdr
)
3021 Elf_Internal_Shdr
*hdr
;
3023 fprintf (stderr
, "\nSection#%d '%s' 0x%.8lx\n", num
, str
, (long) hdr
);
3024 fprintf (stderr
, "sh_name = %ld\n", (long) hdr
->sh_name
);
3025 fprintf (stderr
, "sh_type = %ld\n", (long) hdr
->sh_type
);
3026 fprintf (stderr
, "sh_flags = %ld\n", (long) hdr
->sh_flags
);
3027 fprintf (stderr
, "sh_addr = %ld\n", (long) hdr
->sh_addr
);
3028 fprintf (stderr
, "sh_offset = %ld\n", (long) hdr
->sh_offset
);
3029 fprintf (stderr
, "sh_size = %ld\n", (long) hdr
->sh_size
);
3030 fprintf (stderr
, "sh_link = %ld\n", (long) hdr
->sh_link
);
3031 fprintf (stderr
, "sh_info = %ld\n", (long) hdr
->sh_info
);
3032 fprintf (stderr
, "sh_addralign = %ld\n", (long) hdr
->sh_addralign
);
3033 fprintf (stderr
, "sh_entsize = %ld\n", (long) hdr
->sh_entsize
);
3034 fprintf (stderr
, "rawdata = 0x%.8lx\n", (long) hdr
->rawdata
);
3035 fprintf (stderr
, "contents = 0x%.8lx\n", (long) hdr
->contents
);
3036 fprintf (stderr
, "size = %ld\n", (long) hdr
->size
);
3041 elf_debug_file (ehdrp
)
3042 Elf_Internal_Ehdr
*ehdrp
;
3044 fprintf (stderr
, "e_entry = 0x%.8lx\n", (long) ehdrp
->e_entry
);
3045 fprintf (stderr
, "e_phoff = %ld\n", (long) ehdrp
->e_phoff
);
3046 fprintf (stderr
, "e_phnum = %ld\n", (long) ehdrp
->e_phnum
);
3047 fprintf (stderr
, "e_phentsize = %ld\n", (long) ehdrp
->e_phentsize
);
3048 fprintf (stderr
, "e_shoff = %ld\n", (long) ehdrp
->e_shoff
);
3049 fprintf (stderr
, "e_shnum = %ld\n", (long) ehdrp
->e_shnum
);
3050 fprintf (stderr
, "e_shentsize = %ld\n", (long) ehdrp
->e_shentsize
);
3055 DEFUN (elf_slurp_reloc_table
, (abfd
, asect
, symbols
),
3060 Elf_External_Rel
*native_relocs
;
3061 arelent
*reloc_cache
;
3063 Elf_Internal_Shdr
*i_shdrp
;
3064 Elf_Internal_Shdr
*data_hdr
;
3065 ElfNAME (Off
) data_off
;
3066 ElfNAME (Word
) data_max
;
3067 char buf
[4]; /* FIXME -- might be elf64 */
3071 if (asect
->relocation
)
3073 if (asect
->reloc_count
== 0)
3075 if (asect
->flags
& SEC_CONSTRUCTOR
)
3078 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
3079 native_relocs
= (Elf_External_Rel
*)
3080 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rel
));
3081 bfd_read ((PTR
) native_relocs
,
3082 sizeof (Elf_External_Rel
), asect
->reloc_count
, abfd
);
3084 reloc_cache
= (arelent
*)
3085 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
3089 bfd_error
= no_memory
;
3093 /* Get the offset of the start of the segment we are relocating to read in
3094 the implicit addend. */
3095 i_shdrp
= elf_elfsections (abfd
);
3096 data_hdr
= i_shdrp
+ elf_section_from_bfd_section (abfd
, asect
);
3097 data_off
= data_hdr
->sh_offset
;
3098 data_max
= data_hdr
->sh_size
- sizeof (buf
) + 1;
3101 elf_debug_section ("data section", data_hdr
- i_shdrp
, data_hdr
);
3104 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
3106 #ifdef RELOC_PROCESSING
3107 Elf_Internal_Rel dst
;
3108 Elf_External_Rel
*src
;
3110 cache_ptr
= reloc_cache
+ idx
;
3111 src
= native_relocs
+ idx
;
3112 elf_swap_reloc_in (abfd
, src
, &dst
);
3114 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
3116 Elf_Internal_Rel dst
;
3117 Elf_External_Rel
*src
;
3119 cache_ptr
= reloc_cache
+ idx
;
3120 src
= native_relocs
+ idx
;
3122 elf_swap_reloc_in (abfd
, src
, &dst
);
3124 if (asect
->flags
& SEC_RELOC
)
3126 /* relocatable, so the offset is off of the section */
3127 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
3131 /* non-relocatable, so the offset a virtual address */
3132 cache_ptr
->address
= dst
.r_offset
;
3134 /* ELF_R_SYM(dst.r_info) is the symbol table offset...
3135 -1 is to skip the dummy symbol table entry */
3136 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
3137 BFD_ASSERT (dst
.r_offset
<= data_max
);
3138 if (bfd_seek (abfd
, data_off
+ dst
.r_offset
, SEEK_SET
) != 0
3139 || bfd_read ((PTR
) buf
, sizeof (buf
), 1, abfd
) != sizeof (buf
))
3141 bfd_error
= system_call_error
;
3145 cache_ptr
->addend
= (*abfd
->xvec
->bfd_getx_signed_32
) ((bfd_byte
*) buf
);
3147 /* Fill in the cache_ptr->howto field from dst.r_type */
3149 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
3150 (*ebd
->elf_info_to_howto_rel
) (abfd
, cache_ptr
, &dst
);
3155 asect
->relocation
= reloc_cache
;
3160 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
3166 arelent
*tblptr
= section
->relocation
;
3167 unsigned int count
= 0;
3168 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
3170 /* snarfed from coffcode.h */
3172 elf_slurp_reloca_table (abfd
, section
, symbols
);
3174 elf_slurp_reloc_table (abfd
, section
, symbols
);
3176 tblptr
= section
->relocation
;
3180 for (; count
++ < section
->reloc_count
;)
3181 *relptr
++ = tblptr
++;
3184 return section
->reloc_count
;
3188 DEFUN (elf_get_symtab
, (abfd
, alocation
),
3190 asymbol
** alocation
)
3193 if (!elf_slurp_symbol_table (abfd
, alocation
))
3196 return bfd_get_symcount (abfd
);
3200 DEFUN (elf_make_empty_symbol
, (abfd
),
3203 elf_symbol_type
*newsym
;
3205 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
3208 bfd_error
= no_memory
;
3213 newsym
->symbol
.the_bfd
= abfd
;
3214 return &newsym
->symbol
;
3219 DEFUN (elf_get_symbol_info
, (ignore_abfd
, symbol
, ret
),
3220 bfd
* ignore_abfd AND
3221 asymbol
* symbol AND
3224 bfd_symbol_info (symbol
, ret
);
3228 DEFUN (elf_print_symbol
, (ignore_abfd
, filep
, symbol
, how
),
3229 bfd
* ignore_abfd AND
3231 asymbol
* symbol AND
3232 bfd_print_symbol_type how
)
3234 FILE *file
= (FILE *) filep
;
3237 case bfd_print_symbol_name
:
3238 fprintf (file
, "%s", symbol
->name
);
3240 case bfd_print_symbol_more
:
3241 fprintf (file
, "elf ");
3242 fprintf_vma (file
, symbol
->value
);
3243 fprintf (file
, " %lx", (long) symbol
->flags
);
3245 case bfd_print_symbol_all
:
3247 CONST
char *section_name
;
3248 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
3249 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
3250 fprintf (file
, " %s\t%s",
3260 DEFUN (elf_get_lineno
, (ignore_abfd
, symbol
),
3261 bfd
* ignore_abfd AND
3264 fprintf (stderr
, "elf_get_lineno unimplemented\n");
3271 DEFUN (elf_set_arch_mach
, (abfd
, arch
, machine
),
3273 enum bfd_architecture arch AND
3274 unsigned long machine
)
3276 /* Allow any architecture to be supported by the elf backend */
3279 case bfd_arch_unknown
: /* EM_NONE */
3280 case bfd_arch_sparc
: /* EM_SPARC */
3281 case bfd_arch_i386
: /* EM_386 */
3282 case bfd_arch_m68k
: /* EM_68K */
3283 case bfd_arch_m88k
: /* EM_88K */
3284 case bfd_arch_i860
: /* EM_860 */
3285 case bfd_arch_mips
: /* EM_MIPS (MIPS R3000) */
3286 case bfd_arch_hppa
: /* EM_HPPA (HP PA_RISC) */
3287 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
3294 DEFUN (elf_find_nearest_line
, (abfd
,
3302 asection
* section AND
3303 asymbol
** symbols AND
3305 CONST
char **filename_ptr AND
3306 CONST
char **functionname_ptr AND
3307 unsigned int *line_ptr
)
3313 DEFUN (elf_sizeof_headers
, (abfd
, reloc
),
3317 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
3324 DEFUN (elf_set_section_contents
, (abfd
, section
, location
, offset
, count
),
3329 bfd_size_type count
)
3333 if (abfd
->output_has_begun
== false) /* set by bfd.c handler? */
3335 /* do setup calculations (FIXME) */
3336 elf_compute_section_file_positions (abfd
);
3337 abfd
->output_has_begun
= true;
3340 dest_sect
= elf_section_from_bfd_section (abfd
, section
);
3344 if (bfd_seek (abfd
, elf_elfsections (abfd
)[dest_sect
].sh_offset
+ offset
, SEEK_SET
) == -1)
3346 if (bfd_write (location
, 1, count
, abfd
) != count
)
3352 DEFUN (elf_no_info_to_howto
, (abfd
, cache_ptr
, dst
),
3354 arelent
* cache_ptr AND
3355 Elf_Internal_Rela
* dst
)
3357 fprintf (stderr
, "elf RELA relocation support for target machine unimplemented\n");
3363 DEFUN (elf_no_info_to_howto_rel
, (abfd
, cache_ptr
, dst
),
3365 arelent
* cache_ptr AND
3366 Elf_Internal_Rel
* dst
)
3368 fprintf (stderr
, "elf REL relocation support for target machine unimplemented\n");
3374 DEFUN (elf_get_sect_thunk
, (abfd
, est
),
3376 elf_sect_thunk
* est
)
3378 if (est
== (elf_sect_thunk
*) NULL
)
3381 est
->i_ehdr
= elf_elfheader (abfd
);
3382 est
->i_shdrp
= elf_elfsections (abfd
);
3383 est
->shstrtab
= elf_shstrtab (abfd
);
3384 est
->symtab_section
= elf_onesymtab (abfd
); /* filled in by elf_fake */