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
60 #include <string.h> /* For strrchr and friends */
66 /* Renaming structures, typedefs, macros and functions to be size-specific. */
67 #define Elf_External_Ehdr NAME(Elf,External_Ehdr)
68 #define Elf_External_Sym NAME(Elf,External_Sym)
69 #define Elf_External_Shdr NAME(Elf,External_Shdr)
70 #define Elf_External_Phdr NAME(Elf,External_Phdr)
71 #define Elf_External_Rel NAME(Elf,External_Rel)
72 #define Elf_External_Rela NAME(Elf,External_Rela)
74 #define elf_symbol_type NAME(elf,symbol_type)
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_hash NAME(bfd_elf,hash)
96 #define elf_new_section_hook NAME(bfd_elf,new_section_hook)
97 #define write_relocs NAME(bfd_elf,_write_relocs)
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
105 #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
106 #define ELF_R_SYM(X) ELF32_R_SYM(X)
107 #define ELFCLASS ELFCLASS32
112 #define INLINE __inline__
118 static int shstrtab_length_fixed
;
120 struct elf_sect_data
{
125 /* Forward declarations of static functions */
127 static struct sec
* section_from_elf_index
PARAMS ((bfd
*, int));
129 static int elf_section_from_bfd_section
PARAMS ((bfd
*, struct sec
*));
131 static boolean elf_slurp_symbol_table
PARAMS ((bfd
*, asymbol
**));
133 static int elf_symbol_from_bfd_symbol
PARAMS ((bfd
*,
134 struct symbol_cache_entry
**));
136 static void elf_map_symbols
PARAMS ((bfd
*));
137 static void swap_out_syms
PARAMS ((bfd
*));
140 static void elf_debug_section
PARAMS ((char *, int, Elf_Internal_Shdr
*));
141 static void elf_debug_file
PARAMS ((Elf_Internal_Ehdr
*));
144 #define elf_string_from_elf_strtab(abfd,strindex) \
145 elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
148 /* Structure swapping routines */
150 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
151 can be handled by explicitly specifying 32 bits or "the long type". */
153 #define put_word bfd_h_put_64
154 #define get_word bfd_h_get_64
157 #define put_word bfd_h_put_32
158 #define get_word bfd_h_get_32
161 /* Translate an ELF symbol in external format into an ELF symbol in internal
165 DEFUN (elf_swap_symbol_in
, (abfd
, src
, dst
),
167 Elf_External_Sym
* src AND
168 Elf_Internal_Sym
* dst
)
170 dst
->st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->st_name
);
171 dst
->st_value
= get_word (abfd
, (bfd_byte
*) src
->st_value
);
172 dst
->st_size
= get_word (abfd
, (bfd_byte
*) src
->st_size
);
173 dst
->st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_info
);
174 dst
->st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_other
);
175 dst
->st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->st_shndx
);
178 /* Translate an ELF symbol in internal format into an ELF symbol in external
182 DEFUN (elf_swap_symbol_out
, (abfd
, src
, dst
),
184 Elf_Internal_Sym
* src AND
185 Elf_External_Sym
* dst
)
187 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
188 put_word (abfd
, src
->st_value
, dst
->st_value
);
189 put_word (abfd
, src
->st_size
, dst
->st_size
);
190 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
191 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
192 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
196 /* Translate an ELF file header in external format into an ELF file header in
200 DEFUN (elf_swap_ehdr_in
, (abfd
, src
, dst
),
202 Elf_External_Ehdr
* src AND
203 Elf_Internal_Ehdr
* dst
)
205 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
206 dst
->e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_type
);
207 dst
->e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_machine
);
208 dst
->e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_version
);
209 dst
->e_entry
= get_word (abfd
, (bfd_byte
*) src
->e_entry
);
210 dst
->e_phoff
= get_word (abfd
, (bfd_byte
*) src
->e_phoff
);
211 dst
->e_shoff
= get_word (abfd
, (bfd_byte
*) src
->e_shoff
);
212 dst
->e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_flags
);
213 dst
->e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_ehsize
);
214 dst
->e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phentsize
);
215 dst
->e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phnum
);
216 dst
->e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shentsize
);
217 dst
->e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shnum
);
218 dst
->e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shstrndx
);
221 /* Translate an ELF file header in internal format into an ELF file header in
225 DEFUN (elf_swap_ehdr_out
, (abfd
, src
, dst
),
227 Elf_Internal_Ehdr
* src AND
228 Elf_External_Ehdr
* dst
)
230 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
231 /* note that all elements of dst are *arrays of unsigned char* already... */
232 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
233 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
234 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
235 put_word (abfd
, src
->e_entry
, dst
->e_entry
);
236 put_word (abfd
, src
->e_phoff
, dst
->e_phoff
);
237 put_word (abfd
, src
->e_shoff
, dst
->e_shoff
);
238 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
239 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
240 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
241 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
242 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
243 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
244 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
248 /* Translate an ELF section header table entry in external format into an
249 ELF section header table entry in internal format. */
252 DEFUN (elf_swap_shdr_in
, (abfd
, src
, dst
),
254 Elf_External_Shdr
* src AND
255 Elf_Internal_Shdr
* dst
)
257 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
258 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
259 dst
->sh_flags
= get_word (abfd
, (bfd_byte
*) src
->sh_flags
);
260 dst
->sh_addr
= get_word (abfd
, (bfd_byte
*) src
->sh_addr
);
261 dst
->sh_offset
= get_word (abfd
, (bfd_byte
*) src
->sh_offset
);
262 dst
->sh_size
= get_word (abfd
, (bfd_byte
*) src
->sh_size
);
263 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
264 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
265 dst
->sh_addralign
= get_word (abfd
, (bfd_byte
*) src
->sh_addralign
);
266 dst
->sh_entsize
= get_word (abfd
, (bfd_byte
*) src
->sh_entsize
);
267 /* we haven't done any processing on it yet, so... */
268 dst
->rawdata
= (void *) 0;
271 /* Translate an ELF section header table entry in internal format into an
272 ELF section header table entry in external format. */
275 DEFUN (elf_swap_shdr_out
, (abfd
, src
, dst
),
277 Elf_Internal_Shdr
* src AND
278 Elf_External_Shdr
* dst
)
280 /* note that all elements of dst are *arrays of unsigned char* already... */
281 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
282 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
283 put_word (abfd
, src
->sh_flags
, dst
->sh_flags
);
284 put_word (abfd
, src
->sh_addr
, dst
->sh_addr
);
285 put_word (abfd
, src
->sh_offset
, dst
->sh_offset
);
286 put_word (abfd
, src
->sh_size
, dst
->sh_size
);
287 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
288 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
289 put_word (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
290 put_word (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
294 /* Translate an ELF program header table entry in external format into an
295 ELF program header table entry in internal format. */
298 DEFUN (elf_swap_phdr_in
, (abfd
, src
, dst
),
300 Elf_External_Phdr
* src AND
301 Elf_Internal_Phdr
* dst
)
303 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
304 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
305 dst
->p_offset
= get_word (abfd
, (bfd_byte
*) src
->p_offset
);
306 dst
->p_vaddr
= get_word (abfd
, (bfd_byte
*) src
->p_vaddr
);
307 dst
->p_paddr
= get_word (abfd
, (bfd_byte
*) src
->p_paddr
);
308 dst
->p_filesz
= get_word (abfd
, (bfd_byte
*) src
->p_filesz
);
309 dst
->p_memsz
= get_word (abfd
, (bfd_byte
*) src
->p_memsz
);
310 dst
->p_align
= get_word (abfd
, (bfd_byte
*) src
->p_align
);
314 DEFUN (elf_swap_phdr_out
, (abfd
, src
, dst
),
316 Elf_Internal_Phdr
* src AND
317 Elf_External_Phdr
* dst
)
319 /* note that all elements of dst are *arrays of unsigned char* already... */
320 bfd_h_put_32 (abfd
, src
->p_type
, dst
->p_type
);
321 put_word (abfd
, src
->p_offset
, dst
->p_offset
);
322 put_word (abfd
, src
->p_vaddr
, dst
->p_vaddr
);
323 put_word (abfd
, src
->p_paddr
, dst
->p_paddr
);
324 put_word (abfd
, src
->p_filesz
, dst
->p_filesz
);
325 put_word (abfd
, src
->p_memsz
, dst
->p_memsz
);
326 bfd_h_put_32 (abfd
, src
->p_flags
, dst
->p_flags
);
327 put_word (abfd
, src
->p_align
, dst
->p_align
);
330 /* Translate an ELF reloc from external format to internal format. */
332 DEFUN (elf_swap_reloc_in
, (abfd
, src
, dst
),
334 Elf_External_Rel
* src AND
335 Elf_Internal_Rel
* dst
)
337 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
338 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
342 DEFUN (elf_swap_reloca_in
, (abfd
, src
, dst
),
344 Elf_External_Rela
* src AND
345 Elf_Internal_Rela
* dst
)
347 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
348 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
349 dst
->r_addend
= get_word (abfd
, (bfd_byte
*) src
->r_addend
);
352 /* Translate an ELF reloc from internal format to external format. */
354 DEFUN (elf_swap_reloc_out
, (abfd
, src
, dst
),
356 Elf_Internal_Rel
* src AND
357 Elf_External_Rel
* dst
)
359 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
360 put_word (abfd
, src
->r_info
, dst
->r_info
);
364 DEFUN (elf_swap_reloca_out
, (abfd
, src
, dst
),
366 Elf_Internal_Rela
* src AND
367 Elf_External_Rela
* dst
)
369 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
370 put_word (abfd
, src
->r_info
, dst
->r_info
);
371 put_word (abfd
, src
->r_addend
, dst
->r_addend
);
376 /* String table creation/manipulation routines */
378 static struct strtab
*
379 DEFUN (bfd_new_strtab
, (abfd
),
384 ss
= (struct strtab
*) bfd_xmalloc (sizeof (struct strtab
));
385 ss
->tab
= bfd_xmalloc (1);
386 BFD_ASSERT (ss
->tab
!= 0);
395 DEFUN (bfd_add_to_strtab
, (abfd
, ss
, str
),
397 struct strtab
*ss AND
400 /* should search first, but for now: */
401 /* include the trailing NUL */
402 int ln
= strlen (str
) + 1;
404 /* should this be using obstacks? */
405 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
407 BFD_ASSERT (ss
->tab
!= 0);
408 strcpy (ss
->tab
+ ss
->length
, str
);
412 return ss
->length
- ln
;
416 DEFUN (bfd_add_2_to_strtab
, (abfd
, ss
, str
, str2
),
418 struct strtab
*ss AND
422 /* should search first, but for now: */
423 /* include the trailing NUL */
424 int ln
= strlen (str
) + strlen (str2
) + 1;
426 /* should this be using obstacks? */
428 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
430 ss
->tab
= bfd_xmalloc (ln
);
432 BFD_ASSERT (ss
->tab
!= 0);
433 strcpy (ss
->tab
+ ss
->length
, str
);
434 strcpy (ss
->tab
+ ss
->length
+ strlen (str
), str2
);
438 return ss
->length
- ln
;
442 /* ELF .o/exec file reading */
444 /* Create a new bfd section from an ELF section header. */
447 DEFUN (bfd_section_from_shdr
, (abfd
, shindex
),
449 unsigned int shindex
)
451 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
452 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
456 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
458 switch (hdr
->sh_type
)
462 /* inactive section. Throw it away. */
466 /* Bits that get saved. This one is real. */
469 newsect
= bfd_make_section (abfd
, name
);
472 newsect
->filepos
= hdr
->sh_offset
; /* so we can read back the bits */
473 newsect
->flags
|= SEC_HAS_CONTENTS
;
474 newsect
->vma
= hdr
->sh_addr
;
475 newsect
->_raw_size
= hdr
->sh_size
;
476 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
478 if (hdr
->sh_flags
& SHF_ALLOC
)
480 newsect
->flags
|= SEC_ALLOC
;
481 newsect
->flags
|= SEC_LOAD
;
484 if (!(hdr
->sh_flags
& SHF_WRITE
))
485 newsect
->flags
|= SEC_READONLY
;
487 if (hdr
->sh_flags
& SHF_EXECINSTR
)
488 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
490 newsect
->flags
|= SEC_DATA
;
492 hdr
->rawdata
= (void *) newsect
;
495 hdr
->rawdata
= (void *) bfd_get_section_by_name (abfd
, name
);
500 /* Bits that get saved. This one is real. */
503 newsect
= bfd_make_section (abfd
, name
);
506 newsect
->vma
= hdr
->sh_addr
;
507 newsect
->_raw_size
= hdr
->sh_size
;
508 newsect
->filepos
= hdr
->sh_offset
; /* fake */
509 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
510 if (hdr
->sh_flags
& SHF_ALLOC
)
511 newsect
->flags
|= SEC_ALLOC
;
513 if (!(hdr
->sh_flags
& SHF_WRITE
))
514 newsect
->flags
|= SEC_READONLY
;
516 if (hdr
->sh_flags
& SHF_EXECINSTR
)
517 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
519 newsect
->flags
|= SEC_DATA
;
521 hdr
->rawdata
= (void *) newsect
;
526 case SHT_SYMTAB
: /* A symbol table */
527 if (elf_onesymtab (abfd
) == shindex
)
530 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
531 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
532 elf_onesymtab (abfd
) = shindex
;
533 elf_tdata(abfd
)->symtab_hdr
= *hdr
;
534 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->symtab_hdr
;
535 abfd
->flags
|= HAS_SYMS
;
538 case SHT_STRTAB
: /* A string table */
541 if (ehdr
->e_shstrndx
== shindex
)
543 elf_tdata(abfd
)->shstrtab_hdr
= *hdr
;
544 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->shstrtab_hdr
;
545 hdr
->rawdata
= (PTR
) &elf_tdata(abfd
)->shstrtab_hdr
;
551 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
553 Elf_Internal_Shdr
*hdr2
= elf_elfsections(abfd
)[i
];
554 if (hdr2
->sh_link
== shindex
)
556 bfd_section_from_shdr (abfd
, i
);
557 if (elf_onesymtab (abfd
) == i
)
559 elf_tdata(abfd
)->strtab_hdr
= *hdr
;
560 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->strtab_hdr
;
563 #if 0 /* Not handling other string tables specially right now. */
564 hdr2
= elf_elfsections(abfd
)[i
]; /* in case it moved */
565 /* We have a strtab for some random other section. */
566 newsect
= (asection
*) hdr2
->rawdata
;
569 hdr
->rawdata
= (PTR
) newsect
;
570 hdr2
= &elf_section_data (newsect
)->str_hdr
;
572 elf_elfsections(abfd
)[shindex
] = hdr2
;
578 newsect
= bfd_make_section (abfd
, name
);
581 newsect
->flags
= SEC_HAS_CONTENTS
;
582 hdr
->rawdata
= (PTR
) newsect
;
583 newsect
->_raw_size
= hdr
->sh_size
;
584 newsect
->alignment_power
= 0;
587 if (hdr
->sh_flags
& SHF_ALLOC
)
588 newsect
->flags
|= SEC_ALLOC
|SEC_LOAD
;
589 if (!(hdr
->sh_flags
& SHF_WRITE
))
590 newsect
->flags
|= SEC_READONLY
;
591 if (hdr
->sh_flags
& SHF_EXECINSTR
)
592 newsect
->flags
|= SEC_CODE
;
594 newsect
->flags
|= SEC_DATA
;
601 /* *These* do a lot of work -- but build no sections!
602 The spec says there can be multiple strtabs, but only one symtab,
603 but there can be lots of REL* sections. */
604 /* FIXME: The above statement is wrong! There are typically at least
605 two symbol tables in a dynamically linked executable, ".dynsym"
606 which is the dynamic linkage symbol table and ".symtab", which is
607 the "traditional" symbol table. -fnf */
610 asection
*target_sect
;
611 Elf_Internal_Shdr
*hdr2
;
612 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
614 /* Don't allow REL relocations on a machine that uses RELA and
616 /* @@ Actually, the generic ABI does suggest that both might be
617 used in one file. But the four ABI Processor Supplements I
618 have access to right now all specify that only one is used on
619 each of those architectures. It's conceivable that, e.g., a
620 bunch of absolute 32-bit relocs might be more compact in REL
621 form even on a RELA machine... */
622 BFD_ASSERT (!(use_rela_p
&& (hdr
->sh_type
== SHT_REL
)));
623 BFD_ASSERT (!(!use_rela_p
&& (hdr
->sh_type
== SHT_RELA
)));
624 BFD_ASSERT (hdr
->sh_entsize
==
626 ? sizeof (Elf_External_Rela
)
627 : sizeof (Elf_External_Rel
)));
629 bfd_section_from_shdr (abfd
, hdr
->sh_info
); /* target */
630 bfd_section_from_shdr (abfd
, hdr
->sh_link
); /* symbol table */
631 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
632 if (target_sect
== NULL
)
635 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
637 elf_elfsections(abfd
)[shindex
] = hdr2
;
638 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
639 target_sect
->flags
|= SEC_RELOC
;
640 target_sect
->relocation
= 0;
641 target_sect
->rel_filepos
= hdr
->sh_offset
;
642 abfd
->flags
|= HAS_RELOC
;
649 case SHT_DYNSYM
: /* could treat this like symtab... */
651 fprintf (stderr
, "Dynamic Linking sections not yet supported.\n");
658 fprintf (stderr
, "Note Sections not yet supported.\n");
665 fprintf (stderr
, "SHLIB Sections not supported (and non conforming.)\n");
677 DEFUN (elf_new_section_hook
, (abfd
, sec
),
681 struct bfd_elf_section_data
*sdata
;
682 sec
->used_by_bfd
= sdata
= bfd_alloc (abfd
, sizeof (*sdata
));
683 memset (sdata
, 0, sizeof (*sdata
));
687 /* Create a new bfd section from an ELF program header.
689 Since program segments have no names, we generate a synthetic name
690 of the form segment<NUM>, where NUM is generally the index in the
691 program header table. For segments that are split (see below) we
692 generate the names segment<NUM>a and segment<NUM>b.
694 Note that some program segments may have a file size that is different than
695 (less than) the memory size. All this means is that at execution the
696 system must allocate the amount of memory specified by the memory size,
697 but only initialize it with the first "file size" bytes read from the
698 file. This would occur for example, with program segments consisting
699 of combined data+bss.
701 To handle the above situation, this routine generates TWO bfd sections
702 for the single program segment. The first has the length specified by
703 the file size of the segment, and the second has the length specified
704 by the difference between the two sizes. In effect, the segment is split
705 into it's initialized and uninitialized parts.
710 DEFUN (bfd_section_from_phdr
, (abfd
, hdr
, index
),
712 Elf_Internal_Phdr
* hdr AND
720 split
= ((hdr
->p_memsz
> 0) &&
721 (hdr
->p_filesz
> 0) &&
722 (hdr
->p_memsz
> hdr
->p_filesz
));
723 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
724 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
725 strcpy (name
, namebuf
);
726 newsect
= bfd_make_section (abfd
, name
);
727 newsect
->vma
= hdr
->p_vaddr
;
728 newsect
->_raw_size
= hdr
->p_filesz
;
729 newsect
->filepos
= hdr
->p_offset
;
730 newsect
->flags
|= SEC_HAS_CONTENTS
;
731 if (hdr
->p_type
== PT_LOAD
)
733 newsect
->flags
|= SEC_ALLOC
;
734 newsect
->flags
|= SEC_LOAD
;
735 if (hdr
->p_flags
& PF_X
)
737 /* FIXME: all we known is that it has execute PERMISSION,
739 newsect
->flags
|= SEC_CODE
;
742 if (!(hdr
->p_flags
& PF_W
))
744 newsect
->flags
|= SEC_READONLY
;
749 sprintf (namebuf
, "segment%db", index
);
750 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
751 strcpy (name
, namebuf
);
752 newsect
= bfd_make_section (abfd
, name
);
753 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
754 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
755 if (hdr
->p_type
== PT_LOAD
)
757 newsect
->flags
|= SEC_ALLOC
;
758 if (hdr
->p_flags
& PF_X
)
759 newsect
->flags
|= SEC_CODE
;
761 if (!(hdr
->p_flags
& PF_W
))
762 newsect
->flags
|= SEC_READONLY
;
768 /* Begin processing a given object.
770 First we validate the file by reading in the ELF header and checking
773 static INLINE boolean
774 DEFUN (elf_file_p
, (x_ehdrp
), Elf_External_Ehdr
* x_ehdrp
)
776 return ((x_ehdrp
->e_ident
[EI_MAG0
] == ELFMAG0
)
777 && (x_ehdrp
->e_ident
[EI_MAG1
] == ELFMAG1
)
778 && (x_ehdrp
->e_ident
[EI_MAG2
] == ELFMAG2
)
779 && (x_ehdrp
->e_ident
[EI_MAG3
] == ELFMAG3
));
783 DEFUN (elf_object_p
, (abfd
), bfd
* abfd
)
785 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
786 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
787 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
788 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
790 char *shstrtab
; /* Internal copy of section header stringtab */
791 struct elf_backend_data
*ebd
; /* Use to get ELF_ARCH stored in xvec */
793 /* Read in the ELF header in external format. */
795 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
797 bfd_error
= system_call_error
;
801 /* Now check to see if we have a valid ELF file, and one that BFD can
802 make use of. The magic number must match, the address size ('class')
803 and byte-swapping must match our XVEC entry, and it must have a
804 section header table (FIXME: See comments re sections at top of this
807 if (elf_file_p (&x_ehdr
) == false)
810 bfd_error
= wrong_format
;
814 if (x_ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
)
817 if (x_ehdr
.e_ident
[EI_CLASS
] != ELFCLASS
)
820 /* Switch xvec to match the specified byte order. */
821 switch (x_ehdr
.e_ident
[EI_DATA
])
823 case ELFDATA2MSB
: /* Big-endian */
824 if (!abfd
->xvec
->header_byteorder_big_p
)
827 case ELFDATA2LSB
: /* Little-endian */
828 if (abfd
->xvec
->header_byteorder_big_p
)
831 case ELFDATANONE
: /* No data encoding specified */
832 default: /* Unknown data encoding specified */
836 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
837 the tdata pointer in the bfd. */
839 if (NULL
== (elf_tdata (abfd
) = (struct elf_obj_tdata
*)
840 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
))))
842 bfd_error
= no_memory
;
846 /* FIXME: Any `wrong' exits below here will leak memory (tdata). */
848 /* Now that we know the byte order, swap in the rest of the header */
849 i_ehdrp
= elf_elfheader (abfd
);
850 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
852 elf_debug_file (i_ehdrp
);
855 /* If there is no section header table, we're hosed. */
856 if (i_ehdrp
->e_shoff
== 0)
859 if (i_ehdrp
->e_type
== ET_EXEC
|| i_ehdrp
->e_type
== ET_DYN
)
860 abfd
->flags
|= EXEC_P
;
862 /* Retrieve the architecture information from the xvec and verify
863 that it matches the machine info stored in the ELF header.
864 This allows us to resolve ambiguous formats that might not
865 otherwise be distinguishable. */
867 ebd
= get_elf_backend_data (abfd
);
869 /* Perhaps the elf architecture value should be another field in the
870 elf backend data? If you change this to work that way, make sure
871 that you still get bfd_arch_unknown for unknown architecture types,
872 and that it still gets accepted by the `generic' elf target. */
875 enum bfd_architecture arch
= bfd_arch_unknown
;
877 for (i
= 0; i
< bfd_elf_arch_map_size
; i
++)
879 if (bfd_elf_arch_map
[i
].elf_arch
== i_ehdrp
->e_machine
)
881 arch
= bfd_elf_arch_map
[i
].bfd_arch
;
885 /* start-sanitize-v9 */
886 if (i_ehdrp
->e_machine
== EM_SPARC64
)
887 arch
= bfd_arch_sparc
;
888 /* end-sanitize-v9 */
889 if (ebd
->arch
!= arch
)
891 bfd_default_set_arch_mach (abfd
, arch
, 0);
894 /* Allocate space for a copy of the section header table in
895 internal form, seek to the section header table in the file,
896 read it in, and convert it to internal form. As a simple sanity
897 check, verify that the what BFD thinks is the size of each section
898 header table entry actually matches the size recorded in the file. */
900 if (i_ehdrp
->e_shentsize
!= sizeof (x_shdr
))
902 i_shdrp
= (Elf_Internal_Shdr
*)
903 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
);
904 elf_elfsections (abfd
) = bfd_alloc (abfd
, sizeof (i_shdrp
) * i_ehdrp
->e_shnum
);
905 if (!i_shdrp
|| !elf_elfsections(abfd
))
907 bfd_error
= no_memory
;
910 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) == -1)
912 bfd_error
= system_call_error
;
915 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
917 if (bfd_read ((PTR
) & x_shdr
, sizeof x_shdr
, 1, abfd
)
920 bfd_error
= system_call_error
;
923 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
924 elf_elfsections(abfd
)[shindex
] = i_shdrp
+ shindex
;
926 if (i_ehdrp
->e_shstrndx
)
928 bfd_section_from_shdr (abfd
, i_ehdrp
->e_shstrndx
);
932 for (shindex
= i_ehdrp
->e_shnum
- 1; shindex
>= 0; shindex
--)
934 if (!strcmp (elf_string_from_elf_strtab (abfd
,
935 i_shdrp
[shindex
].sh_name
),
938 elf_tdata(abfd
)->strtab_hdr
= i_shdrp
[shindex
];
939 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->strtab_hdr
;
941 else if (!strcmp (elf_string_from_elf_strtab (abfd
,
942 i_shdrp
[shindex
].sh_name
),
945 elf_tdata(abfd
)->symtab_hdr
= i_shdrp
[shindex
];
946 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->symtab_hdr
;
947 elf_onesymtab (abfd
) = shindex
;
952 /* Read in the string table containing the names of the sections. We
953 will need the base pointer to this table later. */
954 /* We read this inline now, so that we don't have to go through
955 bfd_section_from_shdr with it (since this particular strtab is
956 used to find all of the ELF section names.) */
958 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
962 /* Once all of the section headers have been read and converted, we
963 can start processing them. Note that the first section header is
964 a dummy placeholder entry, so we ignore it.
966 We also watch for the symbol table section and remember the file
967 offset and section size for both the symbol table section and the
968 associated string table section. */
970 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
972 bfd_section_from_shdr (abfd
, shindex
);
975 /* Remember the entry point specified in the ELF file header. */
977 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
983 /* ELF .o/exec file writing */
985 /* Create a new ELF section from a bfd section. */
989 DEFUN (bfd_shdr_from_section
, (abfd
, hdr
, shstrtab
, indx
),
991 Elf_Internal_Shdr
* hdr AND
992 struct strtab
*shstrtab AND
998 sect
= abfd
->sections
;
999 for (ndx
= indx
; --ndx
;)
1003 hdr
[indx
].sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
1004 bfd_section_name (abfd
, sect
));
1005 hdr
[indx
].sh_addr
= sect
->vma
;
1006 hdr
[indx
].sh_size
= sect
->_raw_size
;
1007 hdr
[indx
].sh_addralign
= 1 << sect
->alignment_power
;
1008 hdr
[indx
].sh_flags
= 0;
1009 /* these need to be preserved on */
1010 hdr
[indx
].sh_link
= 0;
1011 hdr
[indx
].sh_info
= 0;
1012 hdr
[indx
].sh_entsize
= 0;
1014 hdr
[indx
].sh_type
= 0;
1015 if (sect
->flags
& SEC_RELOC
)
1017 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1018 hdr
[indx
].sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1021 if (sect
->flags
& SEC_HAS_CONTENTS
)
1023 hdr
[indx
].sh_offset
= sect
->filepos
;
1024 hdr
[indx
].sh_size
= sect
->_raw_size
;
1026 if (sect
->flags
& SEC_ALLOC
)
1028 hdr
[indx
].sh_flags
|= SHF_ALLOC
;
1029 if (sect
->flags
& SEC_LOAD
)
1031 /* do something with sh_type ? */
1034 if (!(sect
->flags
& SEC_READONLY
))
1035 hdr
[indx
].sh_flags
|= SHF_WRITE
;
1037 if (sect
->flags
& SEC_CODE
)
1038 hdr
[indx
].sh_flags
|= SHF_EXECINSTR
;
1045 Takes a bfd and a symbol, returns a pointer to the elf specific area
1046 of the symbol if there is one.
1048 static INLINE elf_symbol_type
*
1049 DEFUN (elf_symbol_from
, (ignore_abfd
, symbol
),
1050 bfd
* ignore_abfd AND
1053 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
1056 if (symbol
->the_bfd
->tdata
.elf_obj_data
== (struct elf_obj_tdata
*) NULL
)
1059 return (elf_symbol_type
*) symbol
;
1063 Create ELF output from BFD sections.
1065 Essentially, just create the section header and forget about the program
1071 DEFUN (elf_make_sections
, (abfd
, asect
, obj
),
1073 asection
* asect AND
1076 /* most of what is in bfd_shdr_from_section goes in here... */
1077 /* and all of these sections generate at *least* one ELF section. */
1080 Elf_Internal_Shdr
*this_hdr
;
1081 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1083 this_hdr
->sh_addr
= asect
->vma
;
1084 this_hdr
->sh_size
= asect
->_raw_size
;
1085 /* contents already set by elf_set_section_contents */
1087 if ((asect
->flags
& SEC_RELOC
)
1089 /* The flags are sometimes inconsistent. */
1090 && asect
->reloc_count
> 0
1094 /* emit a reloc section, and thus strtab and symtab... */
1095 Elf_Internal_Shdr
*rela_hdr
;
1096 Elf_External_Rela
*outbound_relocas
;
1097 Elf_External_Rel
*outbound_relocs
;
1098 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1100 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1102 /* orelocation has the data, reloc_count has the count... */
1105 rela_hdr
->sh_type
= SHT_RELA
;
1106 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1109 /* REL relocations */
1111 rela_hdr
->sh_type
= SHT_REL
;
1112 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rel
);
1114 rela_hdr
->sh_flags
= 0;
1115 rela_hdr
->sh_addr
= 0;
1116 rela_hdr
->sh_offset
= 0;
1117 rela_hdr
->sh_addralign
= 0;
1120 if (asect
->flags
& SEC_ALLOC
)
1122 this_hdr
->sh_flags
|= SHF_ALLOC
;
1123 if (asect
->flags
& SEC_LOAD
)
1125 /* @@ Do something with sh_type? */
1128 if (!(asect
->flags
& SEC_READONLY
))
1129 this_hdr
->sh_flags
|= SHF_WRITE
;
1131 if (asect
->flags
& SEC_CODE
)
1132 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1136 write_relocs (abfd
, sec
, xxx
)
1141 Elf_Internal_Shdr
*rela_hdr
;
1142 Elf_External_Rela
*outbound_relocas
;
1143 Elf_External_Rel
*outbound_relocs
;
1145 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1148 if ((sec
->flags
& SEC_RELOC
) == 0)
1150 /* Flags are sometimes inconsistent. */
1151 if (sec
->reloc_count
== 0)
1154 rela_hdr
= &elf_section_data (sec
)->rel_hdr
;
1156 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* sec
->reloc_count
;
1157 rela_hdr
->contents
= (void *) bfd_alloc (abfd
, rela_hdr
->sh_size
);
1159 /* orelocation has the data, reloc_count has the count... */
1162 outbound_relocas
= (Elf_External_Rela
*) rela_hdr
->contents
;
1164 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1166 Elf_Internal_Rela dst_rela
;
1167 Elf_External_Rela
*src_rela
;
1171 ptr
= sec
->orelocation
[idx
];
1172 src_rela
= outbound_relocas
+ idx
;
1173 if (!(abfd
->flags
& EXEC_P
))
1174 dst_rela
.r_offset
= ptr
->address
- sec
->vma
;
1176 dst_rela
.r_offset
= ptr
->address
;
1178 sym
= *ptr
->sym_ptr_ptr
;
1180 /* I think this bit is wrong. But doing it right here means
1181 fixing bfd_perform_relocation, and verifying that it doesn't
1182 break other targets. Sigh.
1184 Problem I'm trying to solve here: `ld -r' tends to get
1185 offset of target symbol in output-file section put into
1186 addend, but retains the original symbol, so the net
1187 result is doubling of that offset. */
1188 if (!bfd_is_com_section (sym
->section
)
1189 && sym
->section
!= &bfd_und_section
)
1191 /* Could adjust either the offset or the symbol here.
1192 I'm pretty indifferent. */
1193 sym
= sym
->section
->symbol
;
1197 = ELF_R_INFO (elf_symbol_from_bfd_symbol (abfd
, &sym
),
1200 dst_rela
.r_addend
= ptr
->addend
;
1201 elf_swap_reloca_out (abfd
, &dst_rela
, src_rela
);
1206 /* REL relocations */
1208 outbound_relocs
= (Elf_External_Rel
*) rela_hdr
->contents
;
1210 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1212 Elf_Internal_Rel dst_rel
;
1213 Elf_External_Rel
*src_rel
;
1216 ptr
= sec
->orelocation
[idx
];
1217 src_rel
= outbound_relocs
+ idx
;
1218 if (!(abfd
->flags
& EXEC_P
))
1219 dst_rel
.r_offset
= ptr
->address
- sec
->vma
;
1221 dst_rel
.r_offset
= ptr
->address
;
1224 = ELF_R_INFO (elf_symbol_from_bfd_symbol (abfd
, ptr
->sym_ptr_ptr
),
1227 elf_swap_reloc_out (abfd
, &dst_rel
, src_rel
);
1229 /* Update the addend -- FIXME add 64 bit support. */
1230 bfd_put_32 (abfd
, ptr
->addend
,
1231 (unsigned char *) (elf_section_data (sec
)->this_hdr
.contents
)
1232 + dst_rel
.r_offset
);
1238 fix_up_strtabs (abfd
, asect
, obj
)
1243 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (asect
)->this_hdr
;
1244 int this_idx
= elf_section_data(asect
)->this_idx
;
1246 /* @@ Check flags! */
1247 if (!strncmp (asect
->name
, ".stab", 5)
1248 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
1250 size_t len
= strlen (asect
->name
) + 1;
1251 char *s
= alloca (len
);
1252 strcpy (s
, asect
->name
);
1254 asect
= bfd_get_section_by_name (abfd
, s
);
1257 elf_section_data(asect
)->this_hdr
.sh_link
= this_idx
;
1259 /* @@ Assuming 32 bits! */
1260 this_hdr
->sh_entsize
= 0xc;
1265 DEFUN (elf_fake_sections
, (abfd
, asect
, obj
),
1267 asection
* asect AND
1270 /* most of what is in bfd_shdr_from_section goes in here... */
1271 /* and all of these sections generate at *least* one ELF section. */
1273 Elf_Internal_Shdr
*this_hdr
;
1274 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1276 bfd_add_to_strtab (abfd
, elf_shstrtab (abfd
), asect
->name
);
1277 /* We need to log the type *now* so that elf_section_from_bfd_section
1278 can find us... have to set rawdata too. */
1279 this_hdr
->rawdata
= (void *) asect
;
1280 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1281 if ((asect
->flags
& SEC_ALLOC
) && (asect
->flags
& SEC_LOAD
))
1282 this_hdr
->sh_type
= SHT_PROGBITS
;
1283 /* @@ Select conditions correctly! */
1284 else if (!strcmp (asect
->name
, ".bss"))
1285 this_hdr
->sh_type
= SHT_NOBITS
;
1287 /* what *do* we put here? */
1288 this_hdr
->sh_type
= SHT_PROGBITS
;
1290 this_hdr
->sh_flags
= 0;
1291 this_hdr
->sh_addr
= 0;
1292 this_hdr
->sh_size
= 0;
1293 this_hdr
->sh_entsize
= 0;
1294 this_hdr
->sh_info
= 0;
1295 this_hdr
->sh_link
= 0;
1296 this_hdr
->sh_offset
= 0;
1300 /* Emit a strtab and symtab, and possibly a reloc section. */
1301 Elf_Internal_Shdr
*rela_hdr
;
1302 Elf_Internal_Shdr
*symstrtab_hdr
;
1304 /* Note that only one symtab is used, so just remember it
1307 if ((asect
->flags
& SEC_RELOC
)
1308 /* inconsistent flags... */
1309 && asect
->reloc_count
> 0)
1311 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1313 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1315 bfd_add_2_to_strtab (abfd
, elf_shstrtab (abfd
),
1316 use_rela_p
? ".rela" : ".rel",
1318 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1319 rela_hdr
->sh_entsize
= (use_rela_p
1320 ? sizeof (Elf_External_Rela
)
1321 : sizeof (Elf_External_Rel
));
1323 rela_hdr
->sh_flags
= 0;
1324 rela_hdr
->sh_addr
= 0;
1325 rela_hdr
->sh_size
= 0;
1326 rela_hdr
->sh_offset
= 0;
1327 rela_hdr
->sh_addralign
= 0;
1331 if (asect
->flags
& SEC_ALLOC
)
1333 this_hdr
->sh_flags
|= SHF_ALLOC
;
1334 if (asect
->flags
& SEC_LOAD
)
1336 /* @@ Do something with sh_type? */
1339 if (!(asect
->flags
& SEC_READONLY
))
1340 this_hdr
->sh_flags
|= SHF_WRITE
;
1341 if (asect
->flags
& SEC_CODE
)
1342 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1347 xxxINTERNAL_FUNCTION
1351 struct elf_internal_shdr *bfd_elf_locate_sh (bfd *abfd,
1352 struct strtab *strtab,
1353 struct elf_internal_shdr *shdrp,
1357 Helper function to locate an ELF section header given the
1358 name of a BFD section.
1361 static struct elfNAME (internal_shdr
) *
1362 DEFUN (elf_locate_sh
, (abfd
, strtab
, shdrp
, name
),
1364 struct strtab
*strtab AND
1365 struct elfNAME (internal_shdr
) *shdrp AND
1368 Elf_Internal_Shdr
*gotit
= NULL
;
1371 if (shdrp
!= NULL
&& strtab
!= NULL
)
1373 max
= elf_elfheader (abfd
)->e_shnum
;
1374 for (i
= 1; i
< max
; i
++)
1376 if (!strcmp (strtab
->tab
+ shdrp
[i
].sh_name
, name
))
1385 /* Map symbol from it's internal number to the external number, moving
1386 all local symbols to be at the head of the list. */
1392 if (sym
->flags
& BSF_GLOBAL
)
1394 if (sym
->flags
& BSF_LOCAL
)
1398 if (sym
->section
== &bfd_und_section
)
1400 if (bfd_is_com_section (sym
->section
))
1402 if (sym
->flags
& (BSF_LOCAL
| BSF_SECTION_SYM
| BSF_FILE
))
1408 DEFUN (elf_map_symbols
, (abfd
), bfd
* abfd
)
1410 int symcount
= bfd_get_symcount (abfd
);
1411 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1413 int num_globals
= 0;
1414 int num_locals2
= 0;
1415 int num_globals2
= 0;
1416 int num_sections
= 0;
1422 fprintf (stderr
, "elf_map_symbols\n");
1426 /* Add local symbols for each allocated section
1427 FIXME -- we should only put out symbols for sections that
1428 are actually relocated against. */
1429 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1431 if (/*asect->flags & (SEC_LOAD | SEC_DATA | SEC_CODE)*/1)
1438 syms
= (asymbol
**) bfd_realloc (abfd
, syms
,
1439 ((symcount
+ num_sections
+ 1)
1440 * sizeof (asymbol
*)));
1442 syms
= (asymbol
**) bfd_alloc (abfd
,
1443 (num_sections
+ 1) * sizeof (asymbol
*));
1445 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1447 if (/* asect->flags & (SEC_LOAD | SEC_DATA | SEC_CODE) */ 1)
1449 asymbol
*sym
= syms
[symcount
++] = bfd_make_empty_symbol (abfd
);
1450 sym
->the_bfd
= abfd
;
1451 sym
->name
= asect
->name
;
1452 sym
->value
= asect
->vma
;
1453 sym
->flags
= BSF_SECTION_SYM
;
1454 sym
->section
= asect
;
1458 syms
[symcount
] = (asymbol
*) 0;
1459 bfd_set_symtab (abfd
, syms
, symcount
);
1462 elf_symtab_map (abfd
) = symtab_map
1463 = (int *) bfd_alloc (abfd
, symcount
* sizeof (int *));
1465 /* Identify and classify all of the symbols. */
1466 for (idx
= 0; idx
< symcount
; idx
++)
1468 if (!sym_is_global (syms
[idx
]))
1474 /* Now provide mapping information. Add +1 for skipping over the
1476 for (idx
= 0; idx
< symcount
; idx
++)
1478 if (!sym_is_global (syms
[idx
]))
1479 symtab_map
[idx
] = 1 + num_locals2
++;
1481 symtab_map
[idx
] = 1 + num_locals
+ num_globals2
++;
1484 elf_num_locals (abfd
) = num_locals
;
1485 elf_num_globals (abfd
) = num_globals
;
1488 static void assign_section_numbers ();
1489 static void assign_file_positions_except_relocs ();
1492 DEFUN (elf_compute_section_file_positions
, (abfd
), bfd
* abfd
)
1494 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1495 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1496 struct strtab
*shstrtab
;
1497 int count
, maxsections
;
1499 bfd_map_over_sections (abfd
, elf_fake_sections
, 0);
1501 assign_section_numbers (abfd
);
1503 bfd_map_over_sections (abfd
, elf_make_sections
, 0);
1505 bfd_map_over_sections (abfd
, fix_up_strtabs
, 0); /* .stab/.stabstr &c */
1507 swap_out_syms (abfd
);
1509 assign_file_positions_except_relocs (abfd
);
1515 DEFUN (elf_write_phdrs
, (abfd
, i_ehdrp
, i_phdrp
, phdr_cnt
),
1517 Elf_Internal_Ehdr
* i_ehdrp AND
1518 Elf_Internal_Phdr
* i_phdrp AND
1519 Elf32_Half phdr_cnt
)
1521 /* first program header entry goes after the file header */
1522 int outbase
= i_ehdrp
->e_ehsize
;
1524 Elf_External_Phdr x_phdr
;
1526 for (i
= 0; i
< phdr_cnt
; i
++)
1528 elf_swap_phdr_out (abfd
, i_phdrp
+ i
, &x_phdr
);
1529 bfd_seek (abfd
, outbase
, SEEK_SET
);
1530 bfd_write ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
);
1531 outbase
+= sizeof (x_phdr
);
1537 static Elf_Internal_Phdr
*
1538 DEFUN (elf_build_phdrs
, (abfd
, i_ehdrp
, i_shdrp
, phdr_cnt
),
1540 Elf_Internal_Ehdr
* i_ehdrp AND
1541 Elf_Internal_Shdr
* i_shdrp AND
1542 Elf32_Half
* phdr_cnt
)
1544 Elf_Internal_Phdr
*phdr_buf
;
1547 1. The program header table is *not* loaded as part
1548 of the memory image of the program. If this
1549 changes later, the PT_PHDR entry must come first.
1550 2. there is currently no support for program header
1551 entries of type PT_PHDR, PT_DYNAMIC, PT_INTERP,
1554 /* A. Figure out how many program header table entries are needed
1555 1. PT_LOAD for the text segment
1556 2. PT_LOAD for the data segment
1557 Then, reserve space for one more pointer. This will be NULL
1558 to indicate the end of the program header table. */
1560 #ifdef PHDRS_INCLUDED
1563 /* XXX right now, execve() expects exactly 3 PT entries on HPPA-OSF. */
1567 phdr_buf
= (Elf_Internal_Phdr
*) bfd_xmalloc (((*phdr_cnt
) + 1)
1569 sizeof (Elf_Internal_Phdr
));
1572 #ifdef PHDRS_INCLUDED
1573 /* B. Fill in the PT_PHDR entry. */
1578 /* C. Fill in the PT_LOAD entry for the text segment. */
1580 phdr_buf
[idx
].p_type
= PT_LOAD
;
1582 /* get virtual/physical address from .text section */
1583 phdr_buf
[idx
].p_vaddr
= bfd_get_section_by_name (abfd
, ".text")->vma
;
1584 phdr_buf
[idx
].p_paddr
= 0; /* XXX */
1586 /* Ultimately, we would like the size of the .text load
1587 segment to be the sum of the following sections:
1588 the program header table itself
1599 But, right now, it will be the sum of the following sections:
1604 static char *CONST ld_sect_names
[] =
1605 {".text", ".rodata", NULL
};
1609 for (i
= 0; ld_sect_names
[i
]; i
++)
1611 asection
*asect
= bfd_get_section_by_name (abfd
,
1615 ld_size
+= bfd_section_size (abfd
, asect
);
1617 phdr_buf
[idx
].p_filesz
= ld_size
;
1618 /* XXX: need to fix this */
1619 phdr_buf
[idx
].p_memsz
= ld_size
;
1621 phdr_buf
[idx
].p_flags
= PF_R
+ PF_X
;
1622 phdr_buf
[idx
].p_align
=
1623 bfd_get_section_by_name (abfd
, ".text")->alignment_power
;
1627 /* D. Fill in the PT_LOAD entry for the data segment. */
1629 phdr_buf
[idx
].p_type
= PT_LOAD
;
1631 /* get virtual/physical address from .data section */
1632 phdr_buf
[idx
].p_vaddr
= bfd_get_section_by_name (abfd
, ".data")->vma
;
1633 phdr_buf
[idx
].p_paddr
= 0; /* XXX */
1635 /* Ultimately, we would like the size of the data load segment
1636 to be the sum of the following sections:
1637 the PT_DYNAMIC program header table entry
1643 But, right now, it will be the sum of the following sections:
1647 static char *CONST ld_sect_names
[] =
1652 for (i
= 0; ld_sect_names
[i
]; i
++)
1654 asection
*asect
= bfd_get_section_by_name (abfd
,
1658 ld_size
+= bfd_section_size (abfd
, asect
);
1660 phdr_buf
[idx
].p_filesz
= ld_size
;
1661 /* XXX: need to fix this */
1662 phdr_buf
[idx
].p_memsz
= ld_size
;
1664 phdr_buf
[idx
].p_flags
= PF_R
+ PF_W
+ PF_X
;
1665 phdr_buf
[idx
].p_align
1666 = bfd_get_section_by_name (abfd
, ".data")->alignment_power
;
1670 /* E. Fill in the PT_LOAD entry for the bss segment. */
1672 phdr_buf
[idx
].p_type
= PT_LOAD
;
1674 /* get virtual/physical address from .data section */
1675 phdr_buf
[idx
].p_vaddr
= bfd_get_section_by_name (abfd
, ".bss")->vma
;
1676 phdr_buf
[idx
].p_paddr
= 0; /* XXX */
1679 static char *CONST ld_sect_names
[] =
1684 for (i
= 0; ld_sect_names
[i
]; i
++)
1686 asection
*asect
= bfd_get_section_by_name (abfd
,
1690 ld_size
+= bfd_section_size (abfd
, asect
);
1692 phdr_buf
[idx
].p_filesz
= 0;
1693 /* XXX: need to fix this */
1694 phdr_buf
[idx
].p_memsz
= ld_size
;
1696 phdr_buf
[idx
].p_flags
= PF_R
+ PF_W
+ PF_X
;
1697 phdr_buf
[idx
].p_align
1698 = bfd_get_section_by_name (abfd
, ".bss")->alignment_power
;
1702 /* F. Set up the "end of program header table" sentinel. */
1704 memset ((char *) (phdr_buf
+ idx
), 0, sizeof (Elf_Internal_Phdr
));
1707 BFD_ASSERT (idx
- 1 == *phdr_cnt
);
1712 static const Elf_Internal_Shdr null_shdr
;
1714 /* Assign all ELF section numbers. The dummy first section is handled here
1715 too. The link/info pointers for the standard section types are filled
1716 in here too, while we're at it. (Link pointers for .stab sections are
1717 not filled in here.) */
1719 assign_section_numbers (abfd
)
1722 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1724 int section_number
= 1;
1726 Elf_Internal_Shdr
**i_shdrp
;
1728 t
->shstrtab_hdr
.sh_size
= elf_shstrtab(abfd
)->length
;
1729 t
->shstrtab_hdr
.contents
= (void *) elf_shstrtab(abfd
)->tab
;
1730 shstrtab_length_fixed
= 1;
1732 t
->shstrtab_section
= section_number
++;
1733 elf_elfheader(abfd
)->e_shstrndx
= t
->shstrtab_section
;
1736 t
->symtab_section
= section_number
++;
1737 t
->strtab_section
= section_number
++;
1738 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1740 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1742 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1743 d
->this_idx
= section_number
++;
1744 if (sec
->reloc_count
!= 0)
1746 d
->rel_idx
= section_number
++;
1747 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1748 d
->rel_hdr
.sh_info
= d
->this_idx
;
1752 /* No handling for per-section string tables currently. */
1754 elf_elfheader(abfd
)->e_shnum
= section_number
;
1756 /* Set up the list of section header pointers, in agreement with the
1758 i_shdrp
= bfd_alloc (abfd
,
1759 section_number
* sizeof (Elf_Internal_Shdr
*));
1760 elf_elfsections(abfd
) = i_shdrp
;
1761 for (i
= 0; i
< section_number
; i
++)
1764 i_shdrp
[0] = (Elf_Internal_Shdr
*) &null_shdr
;
1765 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1768 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1769 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1771 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1773 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1774 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1776 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1778 /* Make sure we got everything.... */
1779 for (i
= 0; i
< section_number
; i
++)
1780 if (i_shdrp
[i
] == 0)
1784 static INLINE file_ptr
1785 assign_file_position_for_section (i_shdrp
, offset
)
1786 Elf_Internal_Shdr
*i_shdrp
;
1789 i_shdrp
->sh_offset
= offset
;
1790 offset
+= i_shdrp
->sh_size
;
1795 assign_file_positions_except_relocs (abfd
)
1798 /* For now, we ignore the possibility of having program segments, which
1799 may require some alignment in the file. That'll require padding, and
1800 some interesting calculations to optimize file space usage.
1802 Also, since the application may change the list of relocations for
1803 a given section, we don't figure them in here. We'll put them at the
1804 end of the file, at positions computed during bfd_close.
1806 The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ... */
1810 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1811 Elf_Internal_Shdr
*i_shdrp
;
1812 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1814 off
= i_ehdrp
->e_ehsize
;
1815 i_ehdrp
->e_shoff
= off
;
1816 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1817 off
= assign_file_position_for_section (&elf_tdata(abfd
)->shstrtab_hdr
, off
);
1818 off
= assign_file_position_for_section (&elf_tdata(abfd
)->symtab_hdr
, off
);
1819 off
= assign_file_position_for_section (&elf_tdata(abfd
)->strtab_hdr
, off
);
1820 for (i
= 0; i
< i_ehdrp
->e_shnum
; i
++)
1822 i_shdrp
= i_shdrpp
[i
];
1823 if (i_shdrp
->sh_type
== SHT_REL
|| i_shdrp
->sh_type
== SHT_RELA
)
1825 i_shdrp
->sh_offset
= -1;
1828 off
= assign_file_position_for_section (i_shdrp
, off
);
1830 elf_tdata (abfd
)->next_file_pos
= off
;
1837 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1838 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1839 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
1840 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
1841 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
1845 struct strtab
*shstrtab
;
1847 i_ehdrp
= elf_elfheader (abfd
);
1848 i_shdrp
= elf_elfsections (abfd
);
1850 shstrtab
= bfd_new_strtab (abfd
);
1851 elf_shstrtab (abfd
) = shstrtab
;
1853 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
1854 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
1855 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
1856 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
1858 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS
;
1859 i_ehdrp
->e_ident
[EI_DATA
] =
1860 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
1861 i_ehdrp
->e_ident
[EI_VERSION
] = EV_CURRENT
;
1863 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
1864 i_ehdrp
->e_ident
[count
] = 0;
1866 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
) ? ET_EXEC
: ET_REL
;
1867 switch (bfd_get_arch (abfd
))
1869 case bfd_arch_unknown
:
1870 i_ehdrp
->e_machine
= EM_NONE
;
1872 case bfd_arch_sparc
:
1873 i_ehdrp
->e_machine
= EM_SPARC
;
1874 /* start-sanitize-v9 */
1876 i_ehdrp
->e_machine
= EM_SPARC64
;
1878 /* end-sanitize-v9 */
1881 i_ehdrp
->e_machine
= EM_386
;
1884 i_ehdrp
->e_machine
= EM_68K
;
1887 i_ehdrp
->e_machine
= EM_88K
;
1890 i_ehdrp
->e_machine
= EM_860
;
1892 case bfd_arch_mips
: /* MIPS Rxxxx */
1893 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
1896 i_ehdrp
->e_machine
= EM_HPPA
;
1898 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
1900 i_ehdrp
->e_machine
= EM_NONE
;
1902 i_ehdrp
->e_version
= EV_CURRENT
;
1903 i_ehdrp
->e_ehsize
= sizeof (Elf_External_Ehdr
);
1905 /* no program header, for now. */
1906 i_ehdrp
->e_phoff
= 0;
1907 i_ehdrp
->e_phentsize
= 0;
1908 i_ehdrp
->e_phnum
= 0;
1910 /* each bfd section is section header entry */
1911 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
1912 i_ehdrp
->e_shentsize
= sizeof (Elf_External_Shdr
);
1914 /* if we're building an executable, we'll need a program header table */
1915 if (abfd
->flags
& EXEC_P
)
1920 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
1922 /* elf_build_phdrs() returns a (NULL-terminated) array of
1923 Elf_Internal_Phdrs */
1924 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
1925 i_ehdrp
->e_phoff
= outbase
;
1926 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
1931 i_ehdrp
->e_phentsize
= 0;
1933 i_ehdrp
->e_phoff
= 0;
1936 elf_tdata (abfd
)->symtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
1938 elf_tdata (abfd
)->strtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
1940 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
1946 swap_out_syms (abfd
)
1949 struct strtab
*shstrtab
= elf_shstrtab (abfd
);
1951 elf_map_symbols (abfd
);
1953 /* Dump out the symtabs. */
1955 int symcount
= bfd_get_symcount (abfd
);
1956 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1957 struct strtab
*stt
= bfd_new_strtab (abfd
);
1958 Elf_Internal_Shdr
*symtab_hdr
;
1959 Elf_Internal_Shdr
*symstrtab_hdr
;
1960 Elf_External_Sym
*outbound_syms
;
1963 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1964 symtab_hdr
->sh_type
= SHT_SYMTAB
;
1965 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
1966 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
1967 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
1969 /* see assert in elf_fake_sections that supports this: */
1970 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
1971 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
1973 outbound_syms
= (Elf_External_Sym
*)
1974 bfd_alloc (abfd
, (1 + symcount
) * sizeof (Elf_External_Sym
));
1975 /* now generate the data (for "contents") */
1977 /* Fill in zeroth symbol and swap it out. */
1978 Elf_Internal_Sym sym
;
1984 sym
.st_shndx
= SHN_UNDEF
;
1985 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
1987 for (idx
= 0; idx
< symcount
; idx
++)
1989 Elf_Internal_Sym sym
;
1990 bfd_vma value
= syms
[idx
]->value
;
1992 if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
1993 /* Section symbols have no names. */
1996 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
1998 if (bfd_is_com_section (syms
[idx
]->section
))
2000 /* ELF common symbols put the alignment into the `value' field,
2001 and the size into the `size' field. This is backwards from
2002 how BFD handles it, so reverse it here. */
2003 sym
.st_size
= value
;
2004 /* Should retrieve this from somewhere... */
2006 sym
.st_shndx
= SHN_COMMON
;
2010 asection
*sec
= syms
[idx
]->section
;
2013 if (sec
->output_section
)
2015 value
+= sec
->output_offset
;
2016 sec
= sec
->output_section
;
2019 sym
.st_value
= value
;
2020 sym
.st_size
= (elf_symbol_from (abfd
, syms
[idx
]))->internal_elf_sym
.st_size
;
2021 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec
);
2025 /* Writing this would be a hell of a lot easier if we had
2026 some decent documentation on bfd, and knew what to expect
2027 of the library, and what to demand of applications. For
2028 example, it appears that `objcopy' might not set the
2029 section of a symbol to be a section that is actually in
2031 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2033 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec2
);
2034 assert (shndx
!= -1);
2038 if (bfd_is_com_section (syms
[idx
]->section
))
2039 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2040 else if (syms
[idx
]->section
== &bfd_und_section
)
2041 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2042 else if (syms
[idx
]->flags
& BSF_WEAK
)
2043 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, STT_OBJECT
);
2044 else if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2045 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2046 else if (syms
[idx
]->flags
& BSF_FILE
)
2047 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2048 else if (syms
[idx
]->flags
& (BSF_GLOBAL
| BSF_EXPORT
))
2050 if (syms
[idx
]->flags
& BSF_FUNCTION
)
2051 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_FUNC
);
2053 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2055 else if (syms
[idx
]->flags
& BSF_LOCAL
)
2057 if (syms
[idx
]->flags
& BSF_FUNCTION
)
2058 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FUNC
);
2060 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_OBJECT
);
2063 /* Default to local if flag isn't set at all. */
2064 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_OBJECT
);
2067 elf_swap_symbol_out (abfd
, &sym
,
2068 outbound_syms
+ elf_symtab_map (abfd
)[idx
]);
2071 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2072 symstrtab_hdr
->contents
= (PTR
) stt
->tab
;
2073 symstrtab_hdr
->sh_size
= stt
->length
;
2074 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2076 symstrtab_hdr
->sh_flags
= 0;
2077 symstrtab_hdr
->sh_addr
= 0;
2078 symstrtab_hdr
->sh_entsize
= 0;
2079 symstrtab_hdr
->sh_link
= 0;
2080 symstrtab_hdr
->sh_info
= 0;
2081 symstrtab_hdr
->sh_addralign
= 0;
2082 symstrtab_hdr
->size
= 0;
2085 /* put the strtab out too... */
2087 Elf_Internal_Shdr
*this_hdr
;
2089 this_hdr
= &elf_tdata(abfd
)->shstrtab_hdr
;
2090 this_hdr
->contents
= (PTR
) elf_shstrtab (abfd
)->tab
;
2091 this_hdr
->sh_size
= elf_shstrtab (abfd
)->length
;
2092 this_hdr
->sh_type
= SHT_STRTAB
;
2093 this_hdr
->sh_flags
= 0;
2094 this_hdr
->sh_addr
= 0;
2095 this_hdr
->sh_entsize
= 0;
2096 this_hdr
->sh_addralign
= 0;
2102 write_shdrs_and_ehdr (abfd
)
2105 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
2106 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2107 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2108 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
2109 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2113 struct strtab
*shstrtab
;
2115 i_ehdrp
= elf_elfheader (abfd
);
2116 i_shdrp
= elf_elfsections (abfd
);
2117 shstrtab
= elf_shstrtab (abfd
);
2119 /* swap the header before spitting it out... */
2122 elf_debug_file (i_ehdrp
);
2124 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
2125 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
2126 bfd_write ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
);
2128 /* If we're building an executable, fixup the program header table
2131 @@ For now, assume that the entries are in a fixed order: text,
2134 if (abfd
->flags
& EXEC_P
)
2136 static char *CONST section_name
[] =
2137 {".text", ".data", ".bss"};
2139 for (count
= 0; count
< 3; count
++)
2141 asection
*asect
= bfd_get_section_by_name (abfd
,
2142 section_name
[count
]);
2143 int sh_idx
= elf_section_from_bfd_section (abfd
, asect
);
2145 i_phdrp
[count
].p_offset
= i_shdrp
[sh_idx
]->sh_offset
;
2148 /* write out the program header table entries */
2149 elf_write_phdrs (abfd
, i_ehdrp
, i_phdrp
, i_ehdrp
->e_phnum
);
2152 /* at this point we've concocted all the ELF sections... */
2153 x_shdrp
= (Elf_External_Shdr
*)
2154 bfd_alloc (abfd
, sizeof (*x_shdrp
) * (i_ehdrp
->e_shnum
));
2157 bfd_error
= no_memory
;
2161 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2164 elf_debug_section (shstrtab
->tab
+ i_shdrp
[count
]->sh_name
, count
,
2167 elf_swap_shdr_out (abfd
, i_shdrp
[count
], x_shdrp
+ count
);
2169 bfd_seek (abfd
, (file_ptr
) i_ehdrp
->e_shoff
, SEEK_SET
);
2170 bfd_write ((PTR
) x_shdrp
, sizeof (*x_shdrp
), i_ehdrp
->e_shnum
, abfd
);
2171 /* need to dump the string table too... */
2177 assign_file_positions_for_relocs (abfd
)
2180 file_ptr off
= elf_tdata(abfd
)->next_file_pos
;
2182 Elf_Internal_Shdr
**shdrpp
= elf_elfsections (abfd
);
2183 Elf_Internal_Shdr
*shdrp
;
2184 for (i
= 0; i
< elf_elfheader(abfd
)->e_shnum
; i
++)
2187 if (shdrp
->sh_type
!= SHT_REL
&& shdrp
->sh_type
!= SHT_RELA
)
2189 off
= assign_file_position_for_section (shdrp
, off
);
2191 elf_tdata(abfd
)->next_file_pos
= off
;
2195 DEFUN (NAME(bfd_elf
,write_object_contents
), (abfd
), bfd
* abfd
)
2197 Elf_Internal_Ehdr
*i_ehdrp
;
2198 Elf_Internal_Shdr
**i_shdrp
;
2201 if (abfd
->output_has_begun
== false)
2204 prep_headers (abfd
);
2206 elf_compute_section_file_positions (abfd
);
2208 abfd
->output_has_begun
= true;
2211 i_shdrp
= elf_elfsections (abfd
);
2212 i_ehdrp
= elf_elfheader (abfd
);
2214 bfd_map_over_sections (abfd
, write_relocs
, (PTR
) 0);
2216 assign_file_positions_for_relocs (abfd
);
2218 /* After writing the headers, we need to write the sections too... */
2219 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2220 if (i_shdrp
[count
]->contents
)
2222 bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
);
2223 bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
, 1, abfd
);
2225 return write_shdrs_and_ehdr (abfd
);
2228 /* Given an index of a section, retrieve a pointer to it. Note
2229 that for our purposes, sections are indexed by {1, 2, ...} with
2230 0 being an illegal index. */
2232 /* In the original, each ELF section went into exactly one BFD
2233 section. This doesn't really make sense, so we need a real mapping.
2234 The mapping has to hide in the Elf_Internal_Shdr since asection
2235 doesn't have anything like a tdata field... */
2238 DEFUN (section_from_elf_index
, (abfd
, index
),
2242 /* @@ Is bfd_com_section really correct in all the places it could
2243 be returned from this routine? */
2245 if (index
== SHN_ABS
)
2246 return &bfd_com_section
; /* not abs? */
2247 if (index
== SHN_COMMON
)
2248 return &bfd_com_section
;
2250 if (index
> elf_elfheader (abfd
)->e_shnum
)
2254 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[index
];
2256 switch (hdr
->sh_type
)
2258 /* ELF sections that map to BFD sections */
2262 bfd_section_from_shdr (abfd
, index
);
2263 return (struct sec
*) hdr
->rawdata
;
2266 return (struct sec
*) &bfd_abs_section
;
2271 /* given a section, search the header to find them... */
2273 DEFUN (elf_section_from_bfd_section
, (abfd
, asect
),
2277 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2279 Elf_Internal_Shdr
*hdr
;
2280 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2282 if (asect
== &bfd_abs_section
)
2284 if (asect
== &bfd_com_section
)
2286 if (asect
== &bfd_und_section
)
2289 for (index
= 0; index
< maxindex
; index
++)
2291 hdr
= i_shdrp
[index
];
2292 switch (hdr
->sh_type
)
2294 /* ELF sections that map to BFD sections */
2299 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2310 /* given a symbol, return the bfd index for that symbol. */
2312 DEFUN (elf_symbol_from_bfd_symbol
, (abfd
, asym_ptr_ptr
),
2314 struct symbol_cache_entry
**asym_ptr_ptr
)
2316 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2317 CONST
char *name
= asym_ptr
->name
;
2319 int symcount
= bfd_get_symcount (abfd
);
2320 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2322 /* FIXME -- there has to be a better way than linear search. */
2323 for (idx
= 0; idx
< symcount
; idx
++)
2325 if (syms
[idx
] == asym_ptr
2326 || (name
== syms
[idx
]->name
&& name
)
2327 || ((asym_ptr
->flags
& BSF_SECTION_SYM
)
2328 && (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2329 && asym_ptr
->section
== syms
[idx
]->section
))
2333 if (idx
>= symcount
)
2336 fprintf (stderr
, "bfd app err: can't find sym `%s' in symtab\n",
2340 idx
= elf_symtab_map (abfd
)[idx
];
2344 flagword flags
= asym_ptr
->flags
;
2347 "elfsym<-bfdsym %.8lx `%s' sec=%s symnum=%d {",
2348 (long) asym_ptr
, asym_ptr
->name
, asym_ptr
->section
->name
, idx
);
2350 if (flags
== BSF_NO_FLAGS
)
2351 fprintf (stderr
, " none");
2353 if (flags
& BSF_LOCAL
)
2354 fprintf (stderr
, " local");
2356 if (flags
& BSF_GLOBAL
)
2357 fprintf (stderr
, " global");
2359 if (flags
& BSF_EXPORT
)
2360 fprintf (stderr
, " export");
2362 if (flags
& BSF_DEBUGGING
)
2363 fprintf (stderr
, " debugging");
2365 if (flags
& BSF_KEEP
)
2366 fprintf (stderr
, " keep");
2368 if (flags
& BSF_KEEP_G
)
2369 fprintf (stderr
, " keep_g");
2371 if (flags
& BSF_WEAK
)
2372 fprintf (stderr
, " weak");
2374 if (flags
& BSF_SECTION_SYM
)
2375 fprintf (stderr
, " section_sym");
2377 if (flags
& BSF_OLD_COMMON
)
2378 fprintf (stderr
, " old_common");
2380 if (flags
& BSF_NOT_AT_END
)
2381 fprintf (stderr
, " not_at_end");
2383 if (flags
& BSF_CONSTRUCTOR
)
2384 fprintf (stderr
, " constructor");
2386 if (flags
& BSF_WARNING
)
2387 fprintf (stderr
, " warning");
2389 if (flags
& BSF_INDIRECT
)
2390 fprintf (stderr
, " indirect");
2392 if (flags
& BSF_FILE
)
2393 fprintf (stderr
, " file");
2395 if (flags
& BSF_FUNCTION
)
2396 fprintf (stderr
, " function");
2398 fputs (" }\n", stderr
);
2407 DEFUN (elf_slurp_symbol_table
, (abfd
, symptrs
),
2409 asymbol
** symptrs
) /* Buffer for generated bfd symbols */
2411 Elf_Internal_Shdr
*hdr
= &elf_tdata(abfd
)->symtab_hdr
;
2412 int symcount
; /* Number of external ELF symbols */
2414 elf_symbol_type
*sym
; /* Pointer to current bfd symbol */
2415 elf_symbol_type
*symbase
; /* Buffer for generated bfd symbols */
2416 Elf_Internal_Sym i_sym
;
2417 Elf_External_Sym
*x_symp
;
2419 /* this is only valid because there is only one symtab... */
2420 /* FIXME: This is incorrect, there may also be a dynamic symbol
2421 table which is a subset of the full symbol table. We either need
2422 to be prepared to read both (and merge them) or ensure that we
2423 only read the full symbol table. Currently we only get called to
2424 read the full symbol table. -fnf */
2425 if (bfd_get_outsymbols (abfd
) != NULL
)
2430 /* Read each raw ELF symbol, converting from external ELF form to
2431 internal ELF form, and then using the information to create a
2432 canonical bfd symbol table entry.
2434 Note that we allocate the initial bfd canonical symbol buffer
2435 based on a one-to-one mapping of the ELF symbols to canonical
2436 symbols. We actually use all the ELF symbols, so there will be no
2437 space left over at the end. When we have all the symbols, we
2438 build the caller's pointer vector. */
2440 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2442 bfd_error
= system_call_error
;
2446 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2447 symbase
= (elf_symbol_type
*) bfd_zalloc (abfd
, symcount
* sizeof (elf_symbol_type
));
2450 /* Temporarily allocate room for the raw ELF symbols. */
2451 x_symp
= (Elf_External_Sym
*) bfd_xmalloc (symcount
* sizeof (Elf_External_Sym
));
2453 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
2454 != symcount
* sizeof (Elf_External_Sym
))
2456 free ((PTR
) x_symp
);
2457 bfd_error
= system_call_error
;
2460 /* Skip first symbol, which is a null dummy. */
2461 for (i
= 1; i
< symcount
; i
++)
2463 elf_swap_symbol_in (abfd
, x_symp
+ i
, &i_sym
);
2464 memcpy (&sym
->internal_elf_sym
, &i_sym
, sizeof (Elf_Internal_Sym
));
2465 memcpy (&sym
->native_elf_sym
, x_symp
+ i
, sizeof (Elf_External_Sym
));
2466 sym
->symbol
.the_bfd
= abfd
;
2468 sym
->symbol
.name
= elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2471 sym
->symbol
.value
= i_sym
.st_value
;
2473 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
2475 sym
->symbol
.section
= section_from_elf_index (abfd
, i_sym
.st_shndx
);
2477 else if (i_sym
.st_shndx
== SHN_ABS
)
2479 sym
->symbol
.section
= &bfd_abs_section
;
2481 else if (i_sym
.st_shndx
== SHN_COMMON
)
2483 sym
->symbol
.section
= &bfd_com_section
;
2484 /* Elf puts the alignment into the `value' field, and the size
2485 into the `size' field. BFD wants to see the size in the
2486 value field, and doesn't care (at the moment) about the
2488 sym
->symbol
.value
= i_sym
.st_size
;
2490 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2492 sym
->symbol
.section
= &bfd_und_section
;
2495 sym
->symbol
.section
= &bfd_abs_section
;
2497 sym
->symbol
.value
-= sym
->symbol
.section
->vma
;
2499 switch (ELF_ST_BIND (i_sym
.st_info
))
2502 sym
->symbol
.flags
|= BSF_LOCAL
;
2505 sym
->symbol
.flags
|= (BSF_GLOBAL
| BSF_EXPORT
);
2508 sym
->symbol
.flags
|= BSF_WEAK
;
2512 switch (ELF_ST_TYPE (i_sym
.st_info
))
2515 sym
->symbol
.flags
|= BSF_SECTION_SYM
| BSF_DEBUGGING
;
2518 sym
->symbol
.flags
|= BSF_FILE
| BSF_DEBUGGING
;
2521 sym
->symbol
.flags
|= BSF_FUNCTION
;
2524 /* Is this a definition of $global$? If so, keep it because it will be
2525 needd if any relocations are performed. */
2526 if (!strcmp (sym
->symbol
.name
, "$global$")
2527 && sym
->symbol
.section
!= &bfd_und_section
)
2529 /* @@ Why is this referring to backend data and not a field of
2531 struct elf_backend_data
*be_data
= (struct elf_backend_data
*) abfd
->xvec
->backend_data
;
2533 be_data
->global_sym
= (PTR
) sym
;
2538 /* We rely on the zalloc to clear out the final symbol entry. */
2540 /* obj_raw_syms macro uses a cast... */
2541 elf_tdata (abfd
)->raw_syms
= (PTR
) x_symp
;
2543 bfd_get_symcount (abfd
) = symcount
= sym
- symbase
;
2545 /* Fill in the user's symbol pointer vector if needed. */
2549 while (symcount
-- > 0)
2551 *symptrs
++ = &sym
->symbol
;
2554 *symptrs
= 0; /* Final null pointer */
2560 /* Return the number of bytes required to hold the symtab vector.
2562 Note that we base it on the count plus 1, since we will null terminate
2563 the vector allocated based on this size. However, the ELF symbol table
2564 always has a dummy entry as symbol #0, so it ends up even. */
2567 DEFUN (elf_get_symtab_upper_bound
, (abfd
), bfd
* abfd
)
2569 unsigned int symcount
;
2570 unsigned int symtab_size
= 0;
2572 Elf_Internal_Shdr
*hdr
= &elf_tdata(abfd
)->symtab_hdr
;
2573 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2574 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
));
2580 This function return the number of bytes required to store the
2581 relocation information associated with section <<sect>>
2582 attached to bfd <<abfd>>
2586 elf_get_reloc_upper_bound (abfd
, asect
)
2590 if (asect
->flags
& SEC_RELOC
)
2592 /* either rel or rela */
2593 return elf_section_data(asect
)->rel_hdr
.sh_size
;
2600 DEFUN (elf_slurp_reloca_table
, (abfd
, asect
, symbols
),
2605 Elf_External_Rela
*native_relocs
;
2606 arelent
*reloc_cache
;
2611 if (asect
->relocation
)
2613 if (asect
->reloc_count
== 0)
2615 if (asect
->flags
& SEC_CONSTRUCTOR
)
2618 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2619 native_relocs
= (Elf_External_Rela
*)
2620 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rela
));
2621 bfd_read ((PTR
) native_relocs
,
2622 sizeof (Elf_External_Rela
), asect
->reloc_count
, abfd
);
2624 reloc_cache
= (arelent
*)
2625 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2629 bfd_error
= no_memory
;
2633 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2635 #ifdef RELOC_PROCESSING
2636 Elf_Internal_Rela dst
;
2637 Elf_External_Rela
*src
;
2639 cache_ptr
= reloc_cache
+ idx
;
2640 src
= native_relocs
+ idx
;
2641 elf_swap_reloca_in (abfd
, src
, &dst
);
2643 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2645 Elf_Internal_Rela dst
;
2646 Elf_External_Rela
*src
;
2648 cache_ptr
= reloc_cache
+ idx
;
2649 src
= native_relocs
+ idx
;
2651 elf_swap_reloca_in (abfd
, src
, &dst
);
2653 if (asect
->flags
& SEC_RELOC
)
2655 /* relocatable, so the offset is off of the section */
2656 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2660 /* non-relocatable, so the offset a virtual address */
2661 cache_ptr
->address
= dst
.r_offset
;
2663 /* ELF_R_SYM(dst.r_info) is the symbol table offset; subtract 1
2664 because the first entry is NULL. */
2665 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
2667 /* Is it an ELF section symbol? If so, translate it into a
2668 BFD section symbol. */
2669 asymbol
*s
= *(cache_ptr
->sym_ptr_ptr
);
2670 if (s
->flags
& BSF_SECTION_SYM
)
2671 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
2673 cache_ptr
->addend
= dst
.r_addend
;
2675 /* Fill in the cache_ptr->howto field from dst.r_type */
2677 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2678 (*ebd
->elf_info_to_howto
) (abfd
, cache_ptr
, &dst
);
2683 asect
->relocation
= reloc_cache
;
2689 elf_debug_section (str
, num
, hdr
)
2692 Elf_Internal_Shdr
*hdr
;
2694 fprintf (stderr
, "\nSection#%d '%s' 0x%.8lx\n", num
, str
, (long) hdr
);
2696 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
2697 (long) hdr
->sh_name
,
2698 (long) hdr
->sh_type
,
2699 (long) hdr
->sh_flags
);
2701 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
2702 (long) hdr
->sh_addr
,
2703 (long) hdr
->sh_offset
,
2704 (long) hdr
->sh_size
);
2706 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
2707 (long) hdr
->sh_link
,
2708 (long) hdr
->sh_info
,
2709 (long) hdr
->sh_addralign
);
2710 fprintf (stderr
, "sh_entsize = %ld\n",
2711 (long) hdr
->sh_entsize
);
2712 fprintf (stderr
, "rawdata = 0x%.8lx\n", (long) hdr
->rawdata
);
2713 fprintf (stderr
, "contents = 0x%.8lx\n", (long) hdr
->contents
);
2714 fprintf (stderr
, "size = %ld\n", (long) hdr
->size
);
2719 elf_debug_file (ehdrp
)
2720 Elf_Internal_Ehdr
*ehdrp
;
2722 fprintf (stderr
, "e_entry = 0x%.8lx\n", (long) ehdrp
->e_entry
);
2723 fprintf (stderr
, "e_phoff = %ld\n", (long) ehdrp
->e_phoff
);
2724 fprintf (stderr
, "e_phnum = %ld\n", (long) ehdrp
->e_phnum
);
2725 fprintf (stderr
, "e_phentsize = %ld\n", (long) ehdrp
->e_phentsize
);
2726 fprintf (stderr
, "e_shoff = %ld\n", (long) ehdrp
->e_shoff
);
2727 fprintf (stderr
, "e_shnum = %ld\n", (long) ehdrp
->e_shnum
);
2728 fprintf (stderr
, "e_shentsize = %ld\n", (long) ehdrp
->e_shentsize
);
2733 DEFUN (elf_slurp_reloc_table
, (abfd
, asect
, symbols
),
2738 Elf_External_Rel
*native_relocs
;
2739 arelent
*reloc_cache
;
2741 Elf_Internal_Shdr
*data_hdr
;
2742 ElfNAME (Off
) data_off
;
2743 ElfNAME (Word
) data_max
;
2744 char buf
[4]; /* FIXME -- might be elf64 */
2748 if (asect
->relocation
)
2750 if (asect
->reloc_count
== 0)
2752 if (asect
->flags
& SEC_CONSTRUCTOR
)
2755 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2756 native_relocs
= (Elf_External_Rel
*)
2757 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rel
));
2758 bfd_read ((PTR
) native_relocs
,
2759 sizeof (Elf_External_Rel
), asect
->reloc_count
, abfd
);
2761 reloc_cache
= (arelent
*)
2762 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2766 bfd_error
= no_memory
;
2770 /* Get the offset of the start of the segment we are relocating to read in
2771 the implicit addend. */
2772 data_hdr
= &elf_section_data(asect
)->this_hdr
;
2773 data_off
= data_hdr
->sh_offset
;
2774 data_max
= data_hdr
->sh_size
- sizeof (buf
) + 1;
2777 elf_debug_section ("data section", -1, data_hdr
);
2780 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2782 #ifdef RELOC_PROCESSING
2783 Elf_Internal_Rel dst
;
2784 Elf_External_Rel
*src
;
2786 cache_ptr
= reloc_cache
+ idx
;
2787 src
= native_relocs
+ idx
;
2788 elf_swap_reloc_in (abfd
, src
, &dst
);
2790 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2792 Elf_Internal_Rel dst
;
2793 Elf_External_Rel
*src
;
2795 cache_ptr
= reloc_cache
+ idx
;
2796 src
= native_relocs
+ idx
;
2798 elf_swap_reloc_in (abfd
, src
, &dst
);
2800 if (asect
->flags
& SEC_RELOC
)
2802 /* relocatable, so the offset is off of the section */
2803 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2807 /* non-relocatable, so the offset a virtual address */
2808 cache_ptr
->address
= dst
.r_offset
;
2810 /* ELF_R_SYM(dst.r_info) is the symbol table offset...
2811 -1 is to skip the dummy symbol table entry */
2812 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
2813 BFD_ASSERT (dst
.r_offset
<= data_max
);
2814 if (bfd_seek (abfd
, data_off
+ dst
.r_offset
, SEEK_SET
) != 0
2815 || bfd_read ((PTR
) buf
, sizeof (buf
), 1, abfd
) != sizeof (buf
))
2817 bfd_error
= system_call_error
;
2821 cache_ptr
->addend
= (*abfd
->xvec
->bfd_getx_signed_32
) ((bfd_byte
*) buf
);
2823 /* Fill in the cache_ptr->howto field from dst.r_type */
2825 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2826 (*ebd
->elf_info_to_howto_rel
) (abfd
, cache_ptr
, &dst
);
2831 asect
->relocation
= reloc_cache
;
2836 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
2842 arelent
*tblptr
= section
->relocation
;
2843 unsigned int count
= 0;
2844 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
2846 /* snarfed from coffcode.h */
2848 elf_slurp_reloca_table (abfd
, section
, symbols
);
2850 elf_slurp_reloc_table (abfd
, section
, symbols
);
2852 tblptr
= section
->relocation
;
2856 for (; count
++ < section
->reloc_count
;)
2857 *relptr
++ = tblptr
++;
2860 return section
->reloc_count
;
2864 DEFUN (elf_get_symtab
, (abfd
, alocation
),
2866 asymbol
** alocation
)
2869 if (!elf_slurp_symbol_table (abfd
, alocation
))
2872 return bfd_get_symcount (abfd
);
2876 DEFUN (elf_make_empty_symbol
, (abfd
),
2879 elf_symbol_type
*newsym
;
2881 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
2884 bfd_error
= no_memory
;
2889 newsym
->symbol
.the_bfd
= abfd
;
2890 return &newsym
->symbol
;
2895 DEFUN (elf_get_symbol_info
, (ignore_abfd
, symbol
, ret
),
2896 bfd
* ignore_abfd AND
2897 asymbol
* symbol AND
2900 bfd_symbol_info (symbol
, ret
);
2904 DEFUN (elf_print_symbol
, (ignore_abfd
, filep
, symbol
, how
),
2905 bfd
* ignore_abfd AND
2907 asymbol
* symbol AND
2908 bfd_print_symbol_type how
)
2910 FILE *file
= (FILE *) filep
;
2913 case bfd_print_symbol_name
:
2914 fprintf (file
, "%s", symbol
->name
);
2916 case bfd_print_symbol_more
:
2917 fprintf (file
, "elf ");
2918 fprintf_vma (file
, symbol
->value
);
2919 fprintf (file
, " %lx", (long) symbol
->flags
);
2921 case bfd_print_symbol_all
:
2923 CONST
char *section_name
;
2924 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2925 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
2926 fprintf (file
, " %s\t%s",
2936 DEFUN (elf_get_lineno
, (ignore_abfd
, symbol
),
2937 bfd
* ignore_abfd AND
2940 fprintf (stderr
, "elf_get_lineno unimplemented\n");
2947 DEFUN (elf_set_arch_mach
, (abfd
, arch
, machine
),
2949 enum bfd_architecture arch AND
2950 unsigned long machine
)
2952 /* Allow any architecture to be supported by the elf backend */
2955 case bfd_arch_unknown
: /* EM_NONE */
2956 case bfd_arch_sparc
: /* EM_SPARC */
2957 case bfd_arch_i386
: /* EM_386 */
2958 case bfd_arch_m68k
: /* EM_68K */
2959 case bfd_arch_m88k
: /* EM_88K */
2960 case bfd_arch_i860
: /* EM_860 */
2961 case bfd_arch_mips
: /* EM_MIPS (MIPS R3000) */
2962 case bfd_arch_hppa
: /* EM_HPPA (HP PA_RISC) */
2963 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
2970 DEFUN (elf_find_nearest_line
, (abfd
,
2978 asection
* section AND
2979 asymbol
** symbols AND
2981 CONST
char **filename_ptr AND
2982 CONST
char **functionname_ptr AND
2983 unsigned int *line_ptr
)
2989 DEFUN (elf_sizeof_headers
, (abfd
, reloc
),
2993 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
3000 DEFUN (elf_set_section_contents
, (abfd
, section
, location
, offset
, count
),
3005 bfd_size_type count
)
3007 Elf_Internal_Shdr
*hdr
;
3009 if (abfd
->output_has_begun
== false) /* set by bfd.c handler? */
3011 /* do setup calculations (FIXME) */
3012 prep_headers (abfd
);
3013 elf_compute_section_file_positions (abfd
);
3014 abfd
->output_has_begun
= true;
3017 hdr
= &elf_section_data(section
)->this_hdr
;
3019 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
3021 if (bfd_write (location
, 1, count
, abfd
) != count
)
3028 DEFUN (elf_no_info_to_howto
, (abfd
, cache_ptr
, dst
),
3030 arelent
* cache_ptr AND
3031 Elf_Internal_Rela
* dst
)
3033 fprintf (stderr
, "elf RELA relocation support for target machine unimplemented\n");
3039 DEFUN (elf_no_info_to_howto_rel
, (abfd
, cache_ptr
, dst
),
3041 arelent
* cache_ptr AND
3042 Elf_Internal_Rel
* dst
)
3044 fprintf (stderr
, "elf REL relocation support for target machine unimplemented\n");
3050 /* Core file support */
3052 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3053 #include <sys/procfs.h>
3055 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
3056 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
3057 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
3063 DEFUN (bfd_prstatus
, (abfd
, descdata
, descsz
, filepos
),
3070 prstatus_t
*status
= (prstatus_t
*) 0;
3072 if (descsz
== sizeof (prstatus_t
))
3074 newsect
= bfd_make_section (abfd
, ".reg");
3075 newsect
->_raw_size
= sizeof (status
->pr_reg
);
3076 newsect
->filepos
= filepos
+ (long) &status
->pr_reg
;
3077 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3078 newsect
->alignment_power
= 2;
3079 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3081 memcpy (core_prstatus (abfd
), descdata
, descsz
);
3086 /* Stash a copy of the prpsinfo structure away for future use. */
3089 DEFUN (bfd_prpsinfo
, (abfd
, descdata
, descsz
, filepos
),
3097 if (descsz
== sizeof (prpsinfo_t
))
3099 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3101 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
3107 DEFUN (bfd_fpregset
, (abfd
, descdata
, descsz
, filepos
),
3115 newsect
= bfd_make_section (abfd
, ".reg2");
3116 newsect
->_raw_size
= descsz
;
3117 newsect
->filepos
= filepos
;
3118 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3119 newsect
->alignment_power
= 2;
3122 #endif /* HAVE_PROCFS */
3124 /* Return a pointer to the args (including the command name) that were
3125 seen by the program that generated the core dump. Note that for
3126 some reason, a spurious space is tacked onto the end of the args
3127 in some (at least one anyway) implementations, so strip it off if
3131 DEFUN (elf_core_file_failing_command
, (abfd
),
3135 if (core_prpsinfo (abfd
))
3137 prpsinfo_t
*p
= core_prpsinfo (abfd
);
3138 char *scan
= p
->pr_psargs
;
3143 if ((scan
> p
->pr_psargs
) && (*scan
== ' '))
3147 return p
->pr_psargs
;
3153 /* Return the number of the signal that caused the core dump. Presumably,
3154 since we have a core file, we got a signal of some kind, so don't bother
3155 checking the other process status fields, just return the signal number.
3159 DEFUN (elf_core_file_failing_signal
, (abfd
),
3163 if (core_prstatus (abfd
))
3165 return ((prstatus_t
*) (core_prstatus (abfd
)))->pr_cursig
;
3171 /* Check to see if the core file could reasonably be expected to have
3172 come for the current executable file. Note that by default we return
3173 true unless we find something that indicates that there might be a
3178 DEFUN (elf_core_file_matches_executable_p
, (core_bfd
, exec_bfd
),
3187 /* First, xvecs must match since both are ELF files for the same target. */
3189 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
3191 bfd_error
= system_call_error
;
3197 /* If no prpsinfo, just return true. Otherwise, grab the last component
3198 of the exec'd pathname from the prpsinfo. */
3200 if (core_prpsinfo (core_bfd
))
3202 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
))->pr_fname
);
3209 /* Find the last component of the executable pathname. */
3211 if ((execname
= strrchr (exec_bfd
->filename
, '/')) != NULL
)
3217 execname
= (char *) exec_bfd
->filename
;
3220 /* See if they match */
3222 return strcmp (execname
, corename
) ? false : true;
3228 #endif /* HAVE_PROCFS */
3231 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3232 the information that would normally be available from the /proc interface
3233 for the process, at the time the process dumped core. Currently this
3234 includes copies of the prstatus, prpsinfo, and fpregset structures.
3236 Since these structures are potentially machine dependent in size and
3237 ordering, bfd provides two levels of support for them. The first level,
3238 available on all machines since it does not require that the host
3239 have /proc support or the relevant include files, is to create a bfd
3240 section for each of the prstatus, prpsinfo, and fpregset structures,
3241 without any interpretation of their contents. With just this support,
3242 the bfd client will have to interpret the structures itself. Even with
3243 /proc support, it might want these full structures for it's own reasons.
3245 In the second level of support, where HAVE_PROCFS is defined, bfd will
3246 pick apart the structures to gather some additional information that
3247 clients may want, such as the general register set, the name of the
3248 exec'ed file and its arguments, the signal (if any) that caused the
3254 DEFUN (elf_corefile_note
, (abfd
, hdr
),
3256 Elf_Internal_Phdr
* hdr
)
3258 Elf_External_Note
*x_note_p
; /* Elf note, external form */
3259 Elf_Internal_Note i_note
; /* Elf note, internal form */
3260 char *buf
= NULL
; /* Entire note segment contents */
3261 char *namedata
; /* Name portion of the note */
3262 char *descdata
; /* Descriptor portion of the note */
3263 char *sectname
; /* Name to use for new section */
3264 long filepos
; /* File offset to descriptor data */
3267 if (hdr
->p_filesz
> 0
3268 && (buf
= (char *) bfd_xmalloc (hdr
->p_filesz
)) != NULL
3269 && bfd_seek (abfd
, hdr
->p_offset
, SEEK_SET
) != -1
3270 && bfd_read ((PTR
) buf
, hdr
->p_filesz
, 1, abfd
) == hdr
->p_filesz
)
3272 x_note_p
= (Elf_External_Note
*) buf
;
3273 while ((char *) x_note_p
< (buf
+ hdr
->p_filesz
))
3275 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->namesz
);
3276 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->descsz
);
3277 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->type
);
3278 namedata
= x_note_p
->name
;
3279 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
3280 filepos
= hdr
->p_offset
+ (descdata
- buf
);
3281 switch (i_note
.type
)
3284 /* process descdata as prstatus info */
3285 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
3286 sectname
= ".prstatus";
3289 /* process descdata as fpregset info */
3290 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
3291 sectname
= ".fpregset";
3294 /* process descdata as prpsinfo */
3295 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
3296 sectname
= ".prpsinfo";
3299 /* Unknown descriptor, just ignore it. */
3303 if (sectname
!= NULL
)
3305 newsect
= bfd_make_section (abfd
, sectname
);
3306 newsect
->_raw_size
= i_note
.descsz
;
3307 newsect
->filepos
= filepos
;
3308 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3309 newsect
->alignment_power
= 2;
3311 x_note_p
= (Elf_External_Note
*)
3312 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
3323 /* Core files are simply standard ELF formatted files that partition
3324 the file using the execution view of the file (program header table)
3325 rather than the linking view. In fact, there is no section header
3326 table in a core file.
3328 The process status information (including the contents of the general
3329 register set) and the floating point register set are stored in a
3330 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3331 that allow standard bfd access to the general registers (.reg) and the
3332 floating point registers (.reg2).
3337 DEFUN (elf_core_file_p
, (abfd
), bfd
* abfd
)
3339 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
3340 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3341 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
3342 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
3343 unsigned int phindex
;
3345 /* Read in the ELF header in external format. */
3347 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
3349 bfd_error
= system_call_error
;
3353 /* Now check to see if we have a valid ELF file, and one that BFD can
3354 make use of. The magic number must match, the address size ('class')
3355 and byte-swapping must match our XVEC entry, and it must have a
3356 program header table (FIXME: See comments re segments at top of this
3359 if (elf_file_p (&x_ehdr
) == false)
3362 bfd_error
= wrong_format
;
3366 /* FIXME, Check EI_VERSION here ! */
3370 int desired_address_size
= ELFCLASS32
;
3373 int desired_address_size
= ELFCLASS64
;
3376 if (x_ehdr
.e_ident
[EI_CLASS
] != desired_address_size
)
3380 /* Switch xvec to match the specified byte order. */
3381 switch (x_ehdr
.e_ident
[EI_DATA
])
3383 case ELFDATA2MSB
: /* Big-endian */
3384 if (abfd
->xvec
->byteorder_big_p
== false)
3387 case ELFDATA2LSB
: /* Little-endian */
3388 if (abfd
->xvec
->byteorder_big_p
== true)
3391 case ELFDATANONE
: /* No data encoding specified */
3392 default: /* Unknown data encoding specified */
3396 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3397 the tdata pointer in the bfd. */
3400 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
3401 if (elf_tdata (abfd
) == NULL
)
3403 bfd_error
= no_memory
;
3407 /* FIXME, `wrong' returns from this point onward, leak memory. */
3409 /* Now that we know the byte order, swap in the rest of the header */
3410 i_ehdrp
= elf_elfheader (abfd
);
3411 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
3413 elf_debug_file (i_ehdrp
);
3416 /* If there is no program header, or the type is not a core file, then
3418 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
3421 /* Allocate space for a copy of the program header table in
3422 internal form, seek to the program header table in the file,
3423 read it in, and convert it to internal form. As a simple sanity
3424 check, verify that the what BFD thinks is the size of each program
3425 header table entry actually matches the size recorded in the file. */
3427 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
3429 i_phdrp
= (Elf_Internal_Phdr
*)
3430 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
3433 bfd_error
= no_memory
;
3436 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
3438 bfd_error
= system_call_error
;
3441 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3443 if (bfd_read ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
3446 bfd_error
= system_call_error
;
3449 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
3452 /* Once all of the program headers have been read and converted, we
3453 can start processing them. */
3455 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3457 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
3458 if ((i_phdrp
+ phindex
)->p_type
== PT_NOTE
)
3460 elf_corefile_note (abfd
, i_phdrp
+ phindex
);
3464 /* Remember the entry point specified in the ELF file header. */
3466 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;