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
110 static int shstrtab_length_fixed
;
112 struct elf_sect_data
{
117 /* Forward declarations of static functions */
119 static struct sec
* section_from_elf_index
PARAMS ((bfd
*, int));
121 static int elf_section_from_bfd_section
PARAMS ((bfd
*, struct sec
*));
123 static boolean elf_slurp_symbol_table
PARAMS ((bfd
*, asymbol
**));
125 static int elf_symbol_from_bfd_symbol
PARAMS ((bfd
*,
126 struct symbol_cache_entry
**));
128 static void elf_map_symbols
PARAMS ((bfd
*));
129 static void swap_out_syms
PARAMS ((bfd
*));
132 static void elf_debug_section
PARAMS ((char *, int, Elf_Internal_Shdr
*));
133 static void elf_debug_file
PARAMS ((Elf_Internal_Ehdr
*));
136 #define elf_string_from_elf_strtab(abfd,strindex) \
137 elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
140 /* Structure swapping routines */
142 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
143 can be handled by explicitly specifying 32 bits or "the long type". */
145 #define put_word bfd_h_put_64
146 #define get_word bfd_h_get_64
149 #define put_word bfd_h_put_32
150 #define get_word bfd_h_get_32
153 /* Translate an ELF symbol in external format into an ELF symbol in internal
157 DEFUN (elf_swap_symbol_in
, (abfd
, src
, dst
),
159 Elf_External_Sym
* src AND
160 Elf_Internal_Sym
* dst
)
162 dst
->st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->st_name
);
163 dst
->st_value
= get_word (abfd
, (bfd_byte
*) src
->st_value
);
164 dst
->st_size
= get_word (abfd
, (bfd_byte
*) src
->st_size
);
165 dst
->st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_info
);
166 dst
->st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_other
);
167 dst
->st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->st_shndx
);
170 /* Translate an ELF symbol in internal format into an ELF symbol in external
174 DEFUN (elf_swap_symbol_out
, (abfd
, src
, dst
),
176 Elf_Internal_Sym
* src AND
177 Elf_External_Sym
* dst
)
179 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
180 put_word (abfd
, src
->st_value
, dst
->st_value
);
181 put_word (abfd
, src
->st_size
, dst
->st_size
);
182 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
183 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
184 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
188 /* Translate an ELF file header in external format into an ELF file header in
192 DEFUN (elf_swap_ehdr_in
, (abfd
, src
, dst
),
194 Elf_External_Ehdr
* src AND
195 Elf_Internal_Ehdr
* dst
)
197 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
198 dst
->e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_type
);
199 dst
->e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_machine
);
200 dst
->e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_version
);
201 dst
->e_entry
= get_word (abfd
, (bfd_byte
*) src
->e_entry
);
202 dst
->e_phoff
= get_word (abfd
, (bfd_byte
*) src
->e_phoff
);
203 dst
->e_shoff
= get_word (abfd
, (bfd_byte
*) src
->e_shoff
);
204 dst
->e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_flags
);
205 dst
->e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_ehsize
);
206 dst
->e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phentsize
);
207 dst
->e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phnum
);
208 dst
->e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shentsize
);
209 dst
->e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shnum
);
210 dst
->e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shstrndx
);
213 /* Translate an ELF file header in internal format into an ELF file header in
217 DEFUN (elf_swap_ehdr_out
, (abfd
, src
, dst
),
219 Elf_Internal_Ehdr
* src AND
220 Elf_External_Ehdr
* dst
)
222 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
223 /* note that all elements of dst are *arrays of unsigned char* already... */
224 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
225 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
226 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
227 put_word (abfd
, src
->e_entry
, dst
->e_entry
);
228 put_word (abfd
, src
->e_phoff
, dst
->e_phoff
);
229 put_word (abfd
, src
->e_shoff
, dst
->e_shoff
);
230 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
231 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
232 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
233 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
234 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
235 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
236 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
240 /* Translate an ELF section header table entry in external format into an
241 ELF section header table entry in internal format. */
244 DEFUN (elf_swap_shdr_in
, (abfd
, src
, dst
),
246 Elf_External_Shdr
* src AND
247 Elf_Internal_Shdr
* dst
)
249 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
250 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
251 dst
->sh_flags
= get_word (abfd
, (bfd_byte
*) src
->sh_flags
);
252 dst
->sh_addr
= get_word (abfd
, (bfd_byte
*) src
->sh_addr
);
253 dst
->sh_offset
= get_word (abfd
, (bfd_byte
*) src
->sh_offset
);
254 dst
->sh_size
= get_word (abfd
, (bfd_byte
*) src
->sh_size
);
255 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
256 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
257 dst
->sh_addralign
= get_word (abfd
, (bfd_byte
*) src
->sh_addralign
);
258 dst
->sh_entsize
= get_word (abfd
, (bfd_byte
*) src
->sh_entsize
);
259 /* we haven't done any processing on it yet, so... */
260 dst
->rawdata
= (void *) 0;
263 /* Translate an ELF section header table entry in internal format into an
264 ELF section header table entry in external format. */
267 DEFUN (elf_swap_shdr_out
, (abfd
, src
, dst
),
269 Elf_Internal_Shdr
* src AND
270 Elf_External_Shdr
* dst
)
272 /* note that all elements of dst are *arrays of unsigned char* already... */
273 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
274 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
275 put_word (abfd
, src
->sh_flags
, dst
->sh_flags
);
276 put_word (abfd
, src
->sh_addr
, dst
->sh_addr
);
277 put_word (abfd
, src
->sh_offset
, dst
->sh_offset
);
278 put_word (abfd
, src
->sh_size
, dst
->sh_size
);
279 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
280 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
281 put_word (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
282 put_word (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
286 /* Translate an ELF program header table entry in external format into an
287 ELF program header table entry in internal format. */
290 DEFUN (elf_swap_phdr_in
, (abfd
, src
, dst
),
292 Elf_External_Phdr
* src AND
293 Elf_Internal_Phdr
* dst
)
295 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
296 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
297 dst
->p_offset
= get_word (abfd
, (bfd_byte
*) src
->p_offset
);
298 dst
->p_vaddr
= get_word (abfd
, (bfd_byte
*) src
->p_vaddr
);
299 dst
->p_paddr
= get_word (abfd
, (bfd_byte
*) src
->p_paddr
);
300 dst
->p_filesz
= get_word (abfd
, (bfd_byte
*) src
->p_filesz
);
301 dst
->p_memsz
= get_word (abfd
, (bfd_byte
*) src
->p_memsz
);
302 dst
->p_align
= get_word (abfd
, (bfd_byte
*) src
->p_align
);
306 DEFUN (elf_swap_phdr_out
, (abfd
, src
, dst
),
308 Elf_Internal_Phdr
* src AND
309 Elf_External_Phdr
* dst
)
311 /* note that all elements of dst are *arrays of unsigned char* already... */
312 bfd_h_put_32 (abfd
, src
->p_type
, dst
->p_type
);
313 put_word (abfd
, src
->p_offset
, dst
->p_offset
);
314 put_word (abfd
, src
->p_vaddr
, dst
->p_vaddr
);
315 put_word (abfd
, src
->p_paddr
, dst
->p_paddr
);
316 put_word (abfd
, src
->p_filesz
, dst
->p_filesz
);
317 put_word (abfd
, src
->p_memsz
, dst
->p_memsz
);
318 bfd_h_put_32 (abfd
, src
->p_flags
, dst
->p_flags
);
319 put_word (abfd
, src
->p_align
, dst
->p_align
);
322 /* Translate an ELF reloc from external format to internal format. */
324 DEFUN (elf_swap_reloc_in
, (abfd
, src
, dst
),
326 Elf_External_Rel
* src AND
327 Elf_Internal_Rel
* dst
)
329 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
330 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
334 DEFUN (elf_swap_reloca_in
, (abfd
, src
, dst
),
336 Elf_External_Rela
* src AND
337 Elf_Internal_Rela
* dst
)
339 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
340 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
341 dst
->r_addend
= get_word (abfd
, (bfd_byte
*) src
->r_addend
);
344 /* Translate an ELF reloc from internal format to external format. */
346 DEFUN (elf_swap_reloc_out
, (abfd
, src
, dst
),
348 Elf_Internal_Rel
* src AND
349 Elf_External_Rel
* dst
)
351 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
352 put_word (abfd
, src
->r_info
, dst
->r_info
);
356 DEFUN (elf_swap_reloca_out
, (abfd
, src
, dst
),
358 Elf_Internal_Rela
* src AND
359 Elf_External_Rela
* dst
)
361 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
362 put_word (abfd
, src
->r_info
, dst
->r_info
);
363 put_word (abfd
, src
->r_addend
, dst
->r_addend
);
368 /* String table creation/manipulation routines */
370 static struct strtab
*
371 DEFUN (bfd_new_strtab
, (abfd
),
376 ss
= (struct strtab
*) bfd_xmalloc (sizeof (struct strtab
));
377 ss
->tab
= bfd_xmalloc (1);
378 BFD_ASSERT (ss
->tab
!= 0);
387 DEFUN (bfd_add_to_strtab
, (abfd
, ss
, str
),
389 struct strtab
*ss AND
392 /* should search first, but for now: */
393 /* include the trailing NUL */
394 int ln
= strlen (str
) + 1;
396 /* should this be using obstacks? */
397 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
399 BFD_ASSERT (ss
->tab
!= 0);
400 strcpy (ss
->tab
+ ss
->length
, str
);
404 return ss
->length
- ln
;
408 DEFUN (bfd_add_2_to_strtab
, (abfd
, ss
, str
, str2
),
410 struct strtab
*ss AND
414 /* should search first, but for now: */
415 /* include the trailing NUL */
416 int ln
= strlen (str
) + strlen (str2
) + 1;
418 /* should this be using obstacks? */
420 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
422 ss
->tab
= bfd_xmalloc (ln
);
424 BFD_ASSERT (ss
->tab
!= 0);
425 strcpy (ss
->tab
+ ss
->length
, str
);
426 strcpy (ss
->tab
+ ss
->length
+ strlen (str
), str2
);
430 return ss
->length
- ln
;
434 /* ELF .o/exec file reading */
436 /* Create a new bfd section from an ELF section header. */
439 DEFUN (bfd_section_from_shdr
, (abfd
, shindex
),
441 unsigned int shindex
)
443 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
444 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
448 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
450 switch (hdr
->sh_type
)
454 /* inactive section. Throw it away. */
458 /* Bits that get saved. This one is real. */
461 newsect
= bfd_make_section (abfd
, name
);
464 newsect
->filepos
= hdr
->sh_offset
; /* so we can read back the bits */
465 newsect
->flags
|= SEC_HAS_CONTENTS
;
466 newsect
->vma
= hdr
->sh_addr
;
467 newsect
->_raw_size
= hdr
->sh_size
;
468 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
470 if (hdr
->sh_flags
& SHF_ALLOC
)
472 newsect
->flags
|= SEC_ALLOC
;
473 newsect
->flags
|= SEC_LOAD
;
476 if (!(hdr
->sh_flags
& SHF_WRITE
))
477 newsect
->flags
|= SEC_READONLY
;
479 if (hdr
->sh_flags
& SHF_EXECINSTR
)
480 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
482 newsect
->flags
|= SEC_DATA
;
484 hdr
->rawdata
= (void *) newsect
;
487 hdr
->rawdata
= (void *) bfd_get_section_by_name (abfd
, name
);
492 /* Bits that get saved. This one is real. */
495 newsect
= bfd_make_section (abfd
, name
);
498 newsect
->vma
= hdr
->sh_addr
;
499 newsect
->_raw_size
= hdr
->sh_size
;
500 newsect
->filepos
= hdr
->sh_offset
; /* fake */
501 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
502 if (hdr
->sh_flags
& SHF_ALLOC
)
503 newsect
->flags
|= SEC_ALLOC
;
505 if (!(hdr
->sh_flags
& SHF_WRITE
))
506 newsect
->flags
|= SEC_READONLY
;
508 if (hdr
->sh_flags
& SHF_EXECINSTR
)
509 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
511 newsect
->flags
|= SEC_DATA
;
513 hdr
->rawdata
= (void *) newsect
;
518 case SHT_SYMTAB
: /* A symbol table */
519 if (elf_onesymtab (abfd
) == shindex
)
522 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
523 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
524 elf_onesymtab (abfd
) = shindex
;
525 elf_tdata(abfd
)->symtab_hdr
= *hdr
;
526 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->symtab_hdr
;
527 abfd
->flags
|= HAS_SYMS
;
530 case SHT_STRTAB
: /* A string table */
533 if (ehdr
->e_shstrndx
== shindex
)
535 elf_tdata(abfd
)->shstrtab_hdr
= *hdr
;
536 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->shstrtab_hdr
;
537 hdr
->rawdata
= (PTR
) &elf_tdata(abfd
)->shstrtab_hdr
;
543 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
545 Elf_Internal_Shdr
*hdr2
= elf_elfsections(abfd
)[i
];
546 if (hdr2
->sh_link
== shindex
)
548 bfd_section_from_shdr (abfd
, i
);
549 if (elf_onesymtab (abfd
) == i
)
551 elf_tdata(abfd
)->strtab_hdr
= *hdr
;
552 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->strtab_hdr
;
555 #if 0 /* Not handling other string tables specially right now. */
556 hdr2
= elf_elfsections(abfd
)[i
]; /* in case it moved */
557 /* We have a strtab for some random other section. */
558 newsect
= (asection
*) hdr2
->rawdata
;
561 hdr
->rawdata
= (PTR
) newsect
;
562 hdr2
= &elf_section_data (newsect
)->str_hdr
;
564 elf_elfsections(abfd
)[shindex
] = hdr2
;
570 newsect
= bfd_make_section (abfd
, name
);
573 newsect
->flags
= SEC_HAS_CONTENTS
;
574 hdr
->rawdata
= (PTR
) newsect
;
575 newsect
->_raw_size
= hdr
->sh_size
;
576 newsect
->alignment_power
= 0;
579 if (hdr
->sh_flags
& SHF_ALLOC
)
580 newsect
->flags
|= SEC_ALLOC
|SEC_LOAD
;
581 if (!(hdr
->sh_flags
& SHF_WRITE
))
582 newsect
->flags
|= SEC_READONLY
;
583 if (hdr
->sh_flags
& SHF_EXECINSTR
)
584 newsect
->flags
|= SEC_CODE
;
586 newsect
->flags
|= SEC_DATA
;
593 /* *These* do a lot of work -- but build no sections!
594 The spec says there can be multiple strtabs, but only one symtab,
595 but there can be lots of REL* sections. */
596 /* FIXME: The above statement is wrong! There are typically at least
597 two symbol tables in a dynamically linked executable, ".dynsym"
598 which is the dynamic linkage symbol table and ".symtab", which is
599 the "traditional" symbol table. -fnf */
602 asection
*target_sect
;
603 Elf_Internal_Shdr
*hdr2
;
604 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
606 /* Don't allow REL relocations on a machine that uses RELA and
608 /* @@ Actually, the generic ABI does suggest that both might be
609 used in one file. But the four ABI Processor Supplements I
610 have access to right now all specify that only one is used on
611 each of those architectures. It's conceivable that, e.g., a
612 bunch of absolute 32-bit relocs might be more compact in REL
613 form even on a RELA machine... */
614 BFD_ASSERT (!(use_rela_p
&& (hdr
->sh_type
== SHT_REL
)));
615 BFD_ASSERT (!(!use_rela_p
&& (hdr
->sh_type
== SHT_RELA
)));
616 BFD_ASSERT (hdr
->sh_entsize
==
618 ? sizeof (Elf_External_Rela
)
619 : sizeof (Elf_External_Rel
)));
621 bfd_section_from_shdr (abfd
, hdr
->sh_info
); /* target */
622 bfd_section_from_shdr (abfd
, hdr
->sh_link
); /* symbol table */
623 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
624 if (target_sect
== NULL
)
627 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
629 elf_elfsections(abfd
)[shindex
] = hdr2
;
630 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
631 target_sect
->flags
|= SEC_RELOC
;
632 target_sect
->relocation
= 0;
633 target_sect
->rel_filepos
= hdr
->sh_offset
;
634 abfd
->flags
|= HAS_RELOC
;
641 case SHT_DYNSYM
: /* could treat this like symtab... */
643 fprintf (stderr
, "Dynamic Linking sections not yet supported.\n");
650 fprintf (stderr
, "Note Sections not yet supported.\n");
657 fprintf (stderr
, "SHLIB Sections not supported (and non conforming.)\n");
669 DEFUN (elf_new_section_hook
, (abfd
, sec
),
673 struct bfd_elf_section_data
*sdata
;
674 sec
->used_by_bfd
= sdata
= bfd_alloc (abfd
, sizeof (*sdata
));
675 memset (sdata
, 0, sizeof (*sdata
));
679 /* Create a new bfd section from an ELF program header.
681 Since program segments have no names, we generate a synthetic name
682 of the form segment<NUM>, where NUM is generally the index in the
683 program header table. For segments that are split (see below) we
684 generate the names segment<NUM>a and segment<NUM>b.
686 Note that some program segments may have a file size that is different than
687 (less than) the memory size. All this means is that at execution the
688 system must allocate the amount of memory specified by the memory size,
689 but only initialize it with the first "file size" bytes read from the
690 file. This would occur for example, with program segments consisting
691 of combined data+bss.
693 To handle the above situation, this routine generates TWO bfd sections
694 for the single program segment. The first has the length specified by
695 the file size of the segment, and the second has the length specified
696 by the difference between the two sizes. In effect, the segment is split
697 into it's initialized and uninitialized parts.
702 DEFUN (bfd_section_from_phdr
, (abfd
, hdr
, index
),
704 Elf_Internal_Phdr
* hdr AND
712 split
= ((hdr
->p_memsz
> 0) &&
713 (hdr
->p_filesz
> 0) &&
714 (hdr
->p_memsz
> hdr
->p_filesz
));
715 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
716 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
717 strcpy (name
, namebuf
);
718 newsect
= bfd_make_section (abfd
, name
);
719 newsect
->vma
= hdr
->p_vaddr
;
720 newsect
->_raw_size
= hdr
->p_filesz
;
721 newsect
->filepos
= hdr
->p_offset
;
722 newsect
->flags
|= SEC_HAS_CONTENTS
;
723 if (hdr
->p_type
== PT_LOAD
)
725 newsect
->flags
|= SEC_ALLOC
;
726 newsect
->flags
|= SEC_LOAD
;
727 if (hdr
->p_flags
& PF_X
)
729 /* FIXME: all we known is that it has execute PERMISSION,
731 newsect
->flags
|= SEC_CODE
;
734 if (!(hdr
->p_flags
& PF_W
))
736 newsect
->flags
|= SEC_READONLY
;
741 sprintf (namebuf
, "segment%db", index
);
742 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
743 strcpy (name
, namebuf
);
744 newsect
= bfd_make_section (abfd
, name
);
745 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
746 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
747 if (hdr
->p_type
== PT_LOAD
)
749 newsect
->flags
|= SEC_ALLOC
;
750 if (hdr
->p_flags
& PF_X
)
751 newsect
->flags
|= SEC_CODE
;
753 if (!(hdr
->p_flags
& PF_W
))
754 newsect
->flags
|= SEC_READONLY
;
760 /* Begin processing a given object.
762 First we validate the file by reading in the ELF header and checking
765 static INLINE boolean
766 DEFUN (elf_file_p
, (x_ehdrp
), Elf_External_Ehdr
* x_ehdrp
)
768 return ((x_ehdrp
->e_ident
[EI_MAG0
] == ELFMAG0
)
769 && (x_ehdrp
->e_ident
[EI_MAG1
] == ELFMAG1
)
770 && (x_ehdrp
->e_ident
[EI_MAG2
] == ELFMAG2
)
771 && (x_ehdrp
->e_ident
[EI_MAG3
] == ELFMAG3
));
775 DEFUN (elf_object_p
, (abfd
), bfd
* abfd
)
777 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
778 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
779 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
780 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
782 char *shstrtab
; /* Internal copy of section header stringtab */
783 struct elf_backend_data
*ebd
; /* Use to get ELF_ARCH stored in xvec */
785 /* Read in the ELF header in external format. */
787 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
789 bfd_error
= system_call_error
;
793 /* Now check to see if we have a valid ELF file, and one that BFD can
794 make use of. The magic number must match, the address size ('class')
795 and byte-swapping must match our XVEC entry, and it must have a
796 section header table (FIXME: See comments re sections at top of this
799 if (elf_file_p (&x_ehdr
) == false)
802 bfd_error
= wrong_format
;
806 if (x_ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
)
809 if (x_ehdr
.e_ident
[EI_CLASS
] != ELFCLASS
)
812 /* Switch xvec to match the specified byte order. */
813 switch (x_ehdr
.e_ident
[EI_DATA
])
815 case ELFDATA2MSB
: /* Big-endian */
816 if (!abfd
->xvec
->header_byteorder_big_p
)
819 case ELFDATA2LSB
: /* Little-endian */
820 if (abfd
->xvec
->header_byteorder_big_p
)
823 case ELFDATANONE
: /* No data encoding specified */
824 default: /* Unknown data encoding specified */
828 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
829 the tdata pointer in the bfd. */
831 if (NULL
== (elf_tdata (abfd
) = (struct elf_obj_tdata
*)
832 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
))))
834 bfd_error
= no_memory
;
838 /* FIXME: Any `wrong' exits below here will leak memory (tdata). */
840 /* Now that we know the byte order, swap in the rest of the header */
841 i_ehdrp
= elf_elfheader (abfd
);
842 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
844 elf_debug_file (i_ehdrp
);
847 /* If there is no section header table, we're hosed. */
848 if (i_ehdrp
->e_shoff
== 0)
851 if (i_ehdrp
->e_type
== ET_EXEC
|| i_ehdrp
->e_type
== ET_DYN
)
852 abfd
->flags
|= EXEC_P
;
854 /* Retrieve the architecture information from the xvec and verify
855 that it matches the machine info stored in the ELF header.
856 This allows us to resolve ambiguous formats that might not
857 otherwise be distinguishable. */
859 ebd
= get_elf_backend_data (abfd
);
861 /* Perhaps the elf architecture value should be another field in the
862 elf backend data? If you change this to work that way, make sure
863 that you still get bfd_arch_unknown for unknown architecture types,
864 and that it still gets accepted by the `generic' elf target. */
867 enum bfd_architecture arch
= bfd_arch_unknown
;
869 for (i
= 0; i
< bfd_elf_arch_map_size
; i
++)
871 if (bfd_elf_arch_map
[i
].elf_arch
== i_ehdrp
->e_machine
)
873 arch
= bfd_elf_arch_map
[i
].bfd_arch
;
877 /* start-sanitize-v9 */
878 if (i_ehdrp
->e_machine
== EM_SPARC64
)
879 arch
= bfd_arch_sparc
;
880 /* end-sanitize-v9 */
881 if (ebd
->arch
!= arch
)
883 bfd_default_set_arch_mach (abfd
, arch
, 0);
886 /* Allocate space for a copy of the section header table in
887 internal form, seek to the section header table in the file,
888 read it in, and convert it to internal form. As a simple sanity
889 check, verify that the what BFD thinks is the size of each section
890 header table entry actually matches the size recorded in the file. */
892 if (i_ehdrp
->e_shentsize
!= sizeof (x_shdr
))
894 i_shdrp
= (Elf_Internal_Shdr
*)
895 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
);
896 elf_elfsections (abfd
) = bfd_alloc (abfd
, sizeof (i_shdrp
) * i_ehdrp
->e_shnum
);
897 if (!i_shdrp
|| !elf_elfsections(abfd
))
899 bfd_error
= no_memory
;
902 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) == -1)
904 bfd_error
= system_call_error
;
907 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
909 if (bfd_read ((PTR
) & x_shdr
, sizeof x_shdr
, 1, abfd
)
912 bfd_error
= system_call_error
;
915 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
916 elf_elfsections(abfd
)[shindex
] = i_shdrp
+ shindex
;
918 if (i_ehdrp
->e_shstrndx
)
920 bfd_section_from_shdr (abfd
, i_ehdrp
->e_shstrndx
);
924 for (shindex
= i_ehdrp
->e_shnum
- 1; shindex
>= 0; shindex
--)
926 if (!strcmp (elf_string_from_elf_strtab (abfd
,
927 i_shdrp
[shindex
].sh_name
),
930 elf_tdata(abfd
)->strtab_hdr
= i_shdrp
[shindex
];
931 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->strtab_hdr
;
933 else if (!strcmp (elf_string_from_elf_strtab (abfd
,
934 i_shdrp
[shindex
].sh_name
),
937 elf_tdata(abfd
)->symtab_hdr
= i_shdrp
[shindex
];
938 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->symtab_hdr
;
939 elf_onesymtab (abfd
) = shindex
;
944 /* Read in the string table containing the names of the sections. We
945 will need the base pointer to this table later. */
946 /* We read this inline now, so that we don't have to go through
947 bfd_section_from_shdr with it (since this particular strtab is
948 used to find all of the ELF section names.) */
950 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
954 /* Once all of the section headers have been read and converted, we
955 can start processing them. Note that the first section header is
956 a dummy placeholder entry, so we ignore it.
958 We also watch for the symbol table section and remember the file
959 offset and section size for both the symbol table section and the
960 associated string table section. */
962 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
964 bfd_section_from_shdr (abfd
, shindex
);
967 /* Remember the entry point specified in the ELF file header. */
969 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
975 /* ELF .o/exec file writing */
977 /* Create a new ELF section from a bfd section. */
981 DEFUN (bfd_shdr_from_section
, (abfd
, hdr
, shstrtab
, indx
),
983 Elf_Internal_Shdr
* hdr AND
984 struct strtab
*shstrtab AND
990 sect
= abfd
->sections
;
991 for (ndx
= indx
; --ndx
;)
995 hdr
[indx
].sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
996 bfd_section_name (abfd
, sect
));
997 hdr
[indx
].sh_addr
= sect
->vma
;
998 hdr
[indx
].sh_size
= sect
->_raw_size
;
999 hdr
[indx
].sh_addralign
= 1 << sect
->alignment_power
;
1000 hdr
[indx
].sh_flags
= 0;
1001 /* these need to be preserved on */
1002 hdr
[indx
].sh_link
= 0;
1003 hdr
[indx
].sh_info
= 0;
1004 hdr
[indx
].sh_entsize
= 0;
1006 hdr
[indx
].sh_type
= 0;
1007 if (sect
->flags
& SEC_RELOC
)
1009 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1010 hdr
[indx
].sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1013 if (sect
->flags
& SEC_HAS_CONTENTS
)
1015 hdr
[indx
].sh_offset
= sect
->filepos
;
1016 hdr
[indx
].sh_size
= sect
->_raw_size
;
1018 if (sect
->flags
& SEC_ALLOC
)
1020 hdr
[indx
].sh_flags
|= SHF_ALLOC
;
1021 if (sect
->flags
& SEC_LOAD
)
1023 /* do something with sh_type ? */
1026 if (!(sect
->flags
& SEC_READONLY
))
1027 hdr
[indx
].sh_flags
|= SHF_WRITE
;
1029 if (sect
->flags
& SEC_CODE
)
1030 hdr
[indx
].sh_flags
|= SHF_EXECINSTR
;
1037 Takes a bfd and a symbol, returns a pointer to the elf specific area
1038 of the symbol if there is one.
1040 static INLINE elf_symbol_type
*
1041 DEFUN (elf_symbol_from
, (ignore_abfd
, symbol
),
1042 bfd
* ignore_abfd AND
1045 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
1048 if (symbol
->the_bfd
->tdata
.elf_obj_data
== (struct elf_obj_tdata
*) NULL
)
1051 return (elf_symbol_type
*) symbol
;
1055 Create ELF output from BFD sections.
1057 Essentially, just create the section header and forget about the program
1063 DEFUN (elf_make_sections
, (abfd
, asect
, obj
),
1065 asection
* asect AND
1068 /* most of what is in bfd_shdr_from_section goes in here... */
1069 /* and all of these sections generate at *least* one ELF section. */
1072 Elf_Internal_Shdr
*this_hdr
;
1073 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1075 this_hdr
->sh_addr
= asect
->vma
;
1076 this_hdr
->sh_size
= asect
->_raw_size
;
1077 /* contents already set by elf_set_section_contents */
1079 if ((asect
->flags
& SEC_RELOC
)
1081 /* The flags are sometimes inconsistent. */
1082 && asect
->reloc_count
> 0
1086 /* emit a reloc section, and thus strtab and symtab... */
1087 Elf_Internal_Shdr
*rela_hdr
;
1088 Elf_External_Rela
*outbound_relocas
;
1089 Elf_External_Rel
*outbound_relocs
;
1090 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1092 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1094 /* orelocation has the data, reloc_count has the count... */
1097 rela_hdr
->sh_type
= SHT_RELA
;
1098 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1101 /* REL relocations */
1103 rela_hdr
->sh_type
= SHT_REL
;
1104 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rel
);
1106 rela_hdr
->sh_flags
= 0;
1107 rela_hdr
->sh_addr
= 0;
1108 rela_hdr
->sh_offset
= 0;
1109 rela_hdr
->sh_addralign
= 0;
1112 if (asect
->flags
& SEC_ALLOC
)
1114 this_hdr
->sh_flags
|= SHF_ALLOC
;
1115 if (asect
->flags
& SEC_LOAD
)
1117 /* @@ Do something with sh_type? */
1120 if (!(asect
->flags
& SEC_READONLY
))
1121 this_hdr
->sh_flags
|= SHF_WRITE
;
1123 if (asect
->flags
& SEC_CODE
)
1124 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1128 write_relocs (abfd
, sec
, xxx
)
1133 Elf_Internal_Shdr
*rela_hdr
;
1134 Elf_External_Rela
*outbound_relocas
;
1135 Elf_External_Rel
*outbound_relocs
;
1137 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1140 if ((sec
->flags
& SEC_RELOC
) == 0)
1142 /* Flags are sometimes inconsistent. */
1143 if (sec
->reloc_count
== 0)
1146 rela_hdr
= &elf_section_data (sec
)->rel_hdr
;
1148 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* sec
->reloc_count
;
1149 rela_hdr
->contents
= (void *) bfd_alloc (abfd
, rela_hdr
->sh_size
);
1151 /* orelocation has the data, reloc_count has the count... */
1154 outbound_relocas
= (Elf_External_Rela
*) rela_hdr
->contents
;
1156 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1158 Elf_Internal_Rela dst_rela
;
1159 Elf_External_Rela
*src_rela
;
1163 ptr
= sec
->orelocation
[idx
];
1164 src_rela
= outbound_relocas
+ idx
;
1165 if (!(abfd
->flags
& EXEC_P
))
1166 dst_rela
.r_offset
= ptr
->address
- sec
->vma
;
1168 dst_rela
.r_offset
= ptr
->address
;
1170 sym
= *ptr
->sym_ptr_ptr
;
1172 /* I think this bit is wrong. But doing it right here means
1173 fixing bfd_perform_relocation, and verifying that it doesn't
1174 break other targets. Sigh.
1176 Problem I'm trying to solve here: `ld -r' tends to get
1177 offset of target symbol in output-file section put into
1178 addend, but retains the original symbol, so the net
1179 result is doubling of that offset. */
1180 if (!bfd_is_com_section (sym
->section
)
1181 && sym
->section
!= &bfd_und_section
)
1183 /* Could adjust either the offset or the symbol here.
1184 I'm pretty indifferent. */
1185 sym
= sym
->section
->symbol
;
1189 = ELF_R_INFO (elf_symbol_from_bfd_symbol (abfd
, &sym
),
1192 dst_rela
.r_addend
= ptr
->addend
;
1193 elf_swap_reloca_out (abfd
, &dst_rela
, src_rela
);
1198 /* REL relocations */
1200 outbound_relocs
= (Elf_External_Rel
*) rela_hdr
->contents
;
1202 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1204 Elf_Internal_Rel dst_rel
;
1205 Elf_External_Rel
*src_rel
;
1208 ptr
= sec
->orelocation
[idx
];
1209 src_rel
= outbound_relocs
+ idx
;
1210 if (!(abfd
->flags
& EXEC_P
))
1211 dst_rel
.r_offset
= ptr
->address
- sec
->vma
;
1213 dst_rel
.r_offset
= ptr
->address
;
1216 = ELF_R_INFO (elf_symbol_from_bfd_symbol (abfd
, ptr
->sym_ptr_ptr
),
1219 elf_swap_reloc_out (abfd
, &dst_rel
, src_rel
);
1221 /* Update the addend -- FIXME add 64 bit support. */
1222 bfd_put_32 (abfd
, ptr
->addend
,
1223 (unsigned char *) (elf_section_data (sec
)->this_hdr
.contents
)
1224 + dst_rel
.r_offset
);
1230 fix_up_strtabs (abfd
, asect
, obj
)
1235 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (asect
)->this_hdr
;
1236 int this_idx
= elf_section_data(asect
)->this_idx
;
1238 /* @@ Check flags! */
1239 if (!strncmp (asect
->name
, ".stab", 5)
1240 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
1242 size_t len
= strlen (asect
->name
) + 1;
1243 char *s
= alloca (len
);
1244 strcpy (s
, asect
->name
);
1246 asect
= bfd_get_section_by_name (abfd
, s
);
1249 elf_section_data(asect
)->this_hdr
.sh_link
= this_idx
;
1251 /* @@ Assuming 32 bits! */
1252 this_hdr
->sh_entsize
= 0xc;
1257 DEFUN (elf_fake_sections
, (abfd
, asect
, obj
),
1259 asection
* asect AND
1262 /* most of what is in bfd_shdr_from_section goes in here... */
1263 /* and all of these sections generate at *least* one ELF section. */
1265 Elf_Internal_Shdr
*this_hdr
;
1266 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1268 bfd_add_to_strtab (abfd
, elf_shstrtab (abfd
), asect
->name
);
1269 /* We need to log the type *now* so that elf_section_from_bfd_section
1270 can find us... have to set rawdata too. */
1271 this_hdr
->rawdata
= (void *) asect
;
1272 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1273 if ((asect
->flags
& SEC_ALLOC
) && (asect
->flags
& SEC_LOAD
))
1274 this_hdr
->sh_type
= SHT_PROGBITS
;
1275 /* @@ Select conditions correctly! */
1276 else if (!strcmp (asect
->name
, ".bss"))
1277 this_hdr
->sh_type
= SHT_NOBITS
;
1279 /* what *do* we put here? */
1280 this_hdr
->sh_type
= SHT_PROGBITS
;
1282 this_hdr
->sh_flags
= 0;
1283 this_hdr
->sh_addr
= 0;
1284 this_hdr
->sh_size
= 0;
1285 this_hdr
->sh_entsize
= 0;
1286 this_hdr
->sh_info
= 0;
1287 this_hdr
->sh_link
= 0;
1288 this_hdr
->sh_offset
= 0;
1292 /* Emit a strtab and symtab, and possibly a reloc section. */
1293 Elf_Internal_Shdr
*rela_hdr
;
1294 Elf_Internal_Shdr
*symstrtab_hdr
;
1296 /* Note that only one symtab is used, so just remember it
1299 if ((asect
->flags
& SEC_RELOC
)
1300 /* inconsistent flags... */
1301 && asect
->reloc_count
> 0)
1303 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1305 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1307 bfd_add_2_to_strtab (abfd
, elf_shstrtab (abfd
),
1308 use_rela_p
? ".rela" : ".rel",
1310 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1311 rela_hdr
->sh_entsize
= (use_rela_p
1312 ? sizeof (Elf_External_Rela
)
1313 : sizeof (Elf_External_Rel
));
1315 rela_hdr
->sh_flags
= 0;
1316 rela_hdr
->sh_addr
= 0;
1317 rela_hdr
->sh_size
= 0;
1318 rela_hdr
->sh_offset
= 0;
1319 rela_hdr
->sh_addralign
= 0;
1323 if (asect
->flags
& SEC_ALLOC
)
1325 this_hdr
->sh_flags
|= SHF_ALLOC
;
1326 if (asect
->flags
& SEC_LOAD
)
1328 /* @@ Do something with sh_type? */
1331 if (!(asect
->flags
& SEC_READONLY
))
1332 this_hdr
->sh_flags
|= SHF_WRITE
;
1333 if (asect
->flags
& SEC_CODE
)
1334 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1339 xxxINTERNAL_FUNCTION
1343 struct elf_internal_shdr *bfd_elf_locate_sh (bfd *abfd,
1344 struct strtab *strtab,
1345 struct elf_internal_shdr *shdrp,
1349 Helper function to locate an ELF section header given the
1350 name of a BFD section.
1353 static struct elfNAME (internal_shdr
) *
1354 DEFUN (elf_locate_sh
, (abfd
, strtab
, shdrp
, name
),
1356 struct strtab
*strtab AND
1357 struct elfNAME (internal_shdr
) *shdrp AND
1360 Elf_Internal_Shdr
*gotit
= NULL
;
1363 if (shdrp
!= NULL
&& strtab
!= NULL
)
1365 max
= elf_elfheader (abfd
)->e_shnum
;
1366 for (i
= 1; i
< max
; i
++)
1368 if (!strcmp (strtab
->tab
+ shdrp
[i
].sh_name
, name
))
1377 /* Map symbol from it's internal number to the external number, moving
1378 all local symbols to be at the head of the list. */
1384 if (sym
->flags
& BSF_GLOBAL
)
1386 if (sym
->flags
& BSF_LOCAL
)
1390 if (sym
->section
== &bfd_und_section
)
1392 if (bfd_is_com_section (sym
->section
))
1394 if (sym
->flags
& (BSF_LOCAL
| BSF_SECTION_SYM
| BSF_FILE
))
1400 DEFUN (elf_map_symbols
, (abfd
), bfd
* abfd
)
1402 int symcount
= bfd_get_symcount (abfd
);
1403 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1405 int num_globals
= 0;
1406 int num_locals2
= 0;
1407 int num_globals2
= 0;
1408 int num_sections
= 0;
1414 fprintf (stderr
, "elf_map_symbols\n");
1418 /* Add local symbols for each allocated section
1419 FIXME -- we should only put out symbols for sections that
1420 are actually relocated against. */
1421 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1423 if (/*asect->flags & (SEC_LOAD | SEC_DATA | SEC_CODE)*/1)
1430 syms
= (asymbol
**) bfd_realloc (abfd
, syms
,
1431 ((symcount
+ num_sections
+ 1)
1432 * sizeof (asymbol
*)));
1434 syms
= (asymbol
**) bfd_alloc (abfd
,
1435 (num_sections
+ 1) * sizeof (asymbol
*));
1437 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1439 if (/* asect->flags & (SEC_LOAD | SEC_DATA | SEC_CODE) */ 1)
1441 asymbol
*sym
= syms
[symcount
++] = bfd_make_empty_symbol (abfd
);
1442 sym
->the_bfd
= abfd
;
1443 sym
->name
= asect
->name
;
1444 sym
->value
= asect
->vma
;
1445 sym
->flags
= BSF_SECTION_SYM
;
1446 sym
->section
= asect
;
1450 syms
[symcount
] = (asymbol
*) 0;
1451 bfd_set_symtab (abfd
, syms
, symcount
);
1454 elf_symtab_map (abfd
) = symtab_map
1455 = (int *) bfd_alloc (abfd
, symcount
* sizeof (int *));
1457 /* Identify and classify all of the symbols. */
1458 for (idx
= 0; idx
< symcount
; idx
++)
1460 if (!sym_is_global (syms
[idx
]))
1466 /* Now provide mapping information. Add +1 for skipping over the
1468 for (idx
= 0; idx
< symcount
; idx
++)
1470 if (!sym_is_global (syms
[idx
]))
1471 symtab_map
[idx
] = 1 + num_locals2
++;
1473 symtab_map
[idx
] = 1 + num_locals
+ num_globals2
++;
1476 elf_num_locals (abfd
) = num_locals
;
1477 elf_num_globals (abfd
) = num_globals
;
1480 static void assign_section_numbers ();
1481 static void assign_file_positions_except_relocs ();
1484 DEFUN (elf_compute_section_file_positions
, (abfd
), bfd
* abfd
)
1486 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1487 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1488 struct strtab
*shstrtab
;
1489 int count
, maxsections
;
1491 bfd_map_over_sections (abfd
, elf_fake_sections
, 0);
1493 assign_section_numbers (abfd
);
1495 bfd_map_over_sections (abfd
, elf_make_sections
, 0);
1497 bfd_map_over_sections (abfd
, fix_up_strtabs
, 0); /* .stab/.stabstr &c */
1499 swap_out_syms (abfd
);
1501 assign_file_positions_except_relocs (abfd
);
1507 DEFUN (elf_write_phdrs
, (abfd
, i_ehdrp
, i_phdrp
, phdr_cnt
),
1509 Elf_Internal_Ehdr
* i_ehdrp AND
1510 Elf_Internal_Phdr
* i_phdrp AND
1511 Elf32_Half phdr_cnt
)
1513 /* first program header entry goes after the file header */
1514 int outbase
= i_ehdrp
->e_ehsize
;
1516 Elf_External_Phdr x_phdr
;
1518 for (i
= 0; i
< phdr_cnt
; i
++)
1520 elf_swap_phdr_out (abfd
, i_phdrp
+ i
, &x_phdr
);
1521 bfd_seek (abfd
, outbase
, SEEK_SET
);
1522 bfd_write ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
);
1523 outbase
+= sizeof (x_phdr
);
1529 static Elf_Internal_Phdr
*
1530 DEFUN (elf_build_phdrs
, (abfd
, i_ehdrp
, i_shdrp
, phdr_cnt
),
1532 Elf_Internal_Ehdr
* i_ehdrp AND
1533 Elf_Internal_Shdr
* i_shdrp AND
1534 Elf32_Half
* phdr_cnt
)
1536 Elf_Internal_Phdr
*phdr_buf
;
1539 1. The program header table is *not* loaded as part
1540 of the memory image of the program. If this
1541 changes later, the PT_PHDR entry must come first.
1542 2. there is currently no support for program header
1543 entries of type PT_PHDR, PT_DYNAMIC, PT_INTERP,
1546 /* A. Figure out how many program header table entries are needed
1547 1. PT_LOAD for the text segment
1548 2. PT_LOAD for the data segment
1549 Then, reserve space for one more pointer. This will be NULL
1550 to indicate the end of the program header table. */
1552 #ifdef PHDRS_INCLUDED
1555 /* XXX right now, execve() expects exactly 3 PT entries on HPPA-OSF. */
1559 phdr_buf
= (Elf_Internal_Phdr
*) bfd_xmalloc (((*phdr_cnt
) + 1)
1561 sizeof (Elf_Internal_Phdr
));
1564 #ifdef PHDRS_INCLUDED
1565 /* B. Fill in the PT_PHDR entry. */
1570 /* C. Fill in the PT_LOAD entry for the text segment. */
1572 phdr_buf
[idx
].p_type
= PT_LOAD
;
1574 /* get virtual/physical address from .text section */
1575 phdr_buf
[idx
].p_vaddr
= bfd_get_section_by_name (abfd
, ".text")->vma
;
1576 phdr_buf
[idx
].p_paddr
= 0; /* XXX */
1578 /* Ultimately, we would like the size of the .text load
1579 segment to be the sum of the following sections:
1580 the program header table itself
1591 But, right now, it will be the sum of the following sections:
1596 static char *CONST ld_sect_names
[] =
1597 {".text", ".rodata", NULL
};
1601 for (i
= 0; ld_sect_names
[i
]; i
++)
1603 asection
*asect
= bfd_get_section_by_name (abfd
,
1607 ld_size
+= bfd_section_size (abfd
, asect
);
1609 phdr_buf
[idx
].p_filesz
= ld_size
;
1610 /* XXX: need to fix this */
1611 phdr_buf
[idx
].p_memsz
= ld_size
;
1613 phdr_buf
[idx
].p_flags
= PF_R
+ PF_X
;
1614 phdr_buf
[idx
].p_align
=
1615 bfd_get_section_by_name (abfd
, ".text")->alignment_power
;
1619 /* D. Fill in the PT_LOAD entry for the data segment. */
1621 phdr_buf
[idx
].p_type
= PT_LOAD
;
1623 /* get virtual/physical address from .data section */
1624 phdr_buf
[idx
].p_vaddr
= bfd_get_section_by_name (abfd
, ".data")->vma
;
1625 phdr_buf
[idx
].p_paddr
= 0; /* XXX */
1627 /* Ultimately, we would like the size of the data load segment
1628 to be the sum of the following sections:
1629 the PT_DYNAMIC program header table entry
1635 But, right now, it will be the sum of the following sections:
1639 static char *CONST ld_sect_names
[] =
1644 for (i
= 0; ld_sect_names
[i
]; i
++)
1646 asection
*asect
= bfd_get_section_by_name (abfd
,
1650 ld_size
+= bfd_section_size (abfd
, asect
);
1652 phdr_buf
[idx
].p_filesz
= ld_size
;
1653 /* XXX: need to fix this */
1654 phdr_buf
[idx
].p_memsz
= ld_size
;
1656 phdr_buf
[idx
].p_flags
= PF_R
+ PF_W
+ PF_X
;
1657 phdr_buf
[idx
].p_align
1658 = bfd_get_section_by_name (abfd
, ".data")->alignment_power
;
1662 /* E. Fill in the PT_LOAD entry for the bss segment. */
1664 phdr_buf
[idx
].p_type
= PT_LOAD
;
1666 /* get virtual/physical address from .data section */
1667 phdr_buf
[idx
].p_vaddr
= bfd_get_section_by_name (abfd
, ".bss")->vma
;
1668 phdr_buf
[idx
].p_paddr
= 0; /* XXX */
1671 static char *CONST ld_sect_names
[] =
1676 for (i
= 0; ld_sect_names
[i
]; i
++)
1678 asection
*asect
= bfd_get_section_by_name (abfd
,
1682 ld_size
+= bfd_section_size (abfd
, asect
);
1684 phdr_buf
[idx
].p_filesz
= 0;
1685 /* XXX: need to fix this */
1686 phdr_buf
[idx
].p_memsz
= ld_size
;
1688 phdr_buf
[idx
].p_flags
= PF_R
+ PF_W
+ PF_X
;
1689 phdr_buf
[idx
].p_align
1690 = bfd_get_section_by_name (abfd
, ".bss")->alignment_power
;
1694 /* F. Set up the "end of program header table" sentinel. */
1696 memset ((char *) (phdr_buf
+ idx
), 0, sizeof (Elf_Internal_Phdr
));
1699 BFD_ASSERT (idx
- 1 == *phdr_cnt
);
1704 static const Elf_Internal_Shdr null_shdr
;
1706 /* Assign all ELF section numbers. The dummy first section is handled here
1707 too. The link/info pointers for the standard section types are filled
1708 in here too, while we're at it. (Link pointers for .stab sections are
1709 not filled in here.) */
1711 assign_section_numbers (abfd
)
1714 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1716 int section_number
= 1;
1718 Elf_Internal_Shdr
**i_shdrp
;
1720 t
->shstrtab_hdr
.sh_size
= elf_shstrtab(abfd
)->length
;
1721 t
->shstrtab_hdr
.contents
= (void *) elf_shstrtab(abfd
)->tab
;
1722 shstrtab_length_fixed
= 1;
1724 t
->shstrtab_section
= section_number
++;
1725 elf_elfheader(abfd
)->e_shstrndx
= t
->shstrtab_section
;
1728 t
->symtab_section
= section_number
++;
1729 t
->strtab_section
= section_number
++;
1730 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1732 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1734 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1735 d
->this_idx
= section_number
++;
1736 if (sec
->reloc_count
!= 0)
1738 d
->rel_idx
= section_number
++;
1739 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1740 d
->rel_hdr
.sh_info
= d
->this_idx
;
1744 /* No handling for per-section string tables currently. */
1746 elf_elfheader(abfd
)->e_shnum
= section_number
;
1748 /* Set up the list of section header pointers, in agreement with the
1750 i_shdrp
= bfd_alloc (abfd
,
1751 section_number
* sizeof (Elf_Internal_Shdr
*));
1752 elf_elfsections(abfd
) = i_shdrp
;
1753 for (i
= 0; i
< section_number
; i
++)
1756 i_shdrp
[0] = (Elf_Internal_Shdr
*) &null_shdr
;
1757 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1760 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1761 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1763 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1765 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1766 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1768 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1770 /* Make sure we got everything.... */
1771 for (i
= 0; i
< section_number
; i
++)
1772 if (i_shdrp
[i
] == 0)
1776 static INLINE file_ptr
1777 assign_file_position_for_section (i_shdrp
, offset
)
1778 Elf_Internal_Shdr
*i_shdrp
;
1781 i_shdrp
->sh_offset
= offset
;
1782 offset
+= i_shdrp
->sh_size
;
1787 assign_file_positions_except_relocs (abfd
)
1790 /* For now, we ignore the possibility of having program segments, which
1791 may require some alignment in the file. That'll require padding, and
1792 some interesting calculations to optimize file space usage.
1794 Also, since the application may change the list of relocations for
1795 a given section, we don't figure them in here. We'll put them at the
1796 end of the file, at positions computed during bfd_close.
1798 The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ... */
1802 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1803 Elf_Internal_Shdr
*i_shdrp
;
1804 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1806 off
= i_ehdrp
->e_ehsize
;
1807 i_ehdrp
->e_shoff
= off
;
1808 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1809 off
= assign_file_position_for_section (&elf_tdata(abfd
)->shstrtab_hdr
, off
);
1810 off
= assign_file_position_for_section (&elf_tdata(abfd
)->symtab_hdr
, off
);
1811 off
= assign_file_position_for_section (&elf_tdata(abfd
)->strtab_hdr
, off
);
1812 for (i
= 0; i
< i_ehdrp
->e_shnum
; i
++)
1814 i_shdrp
= i_shdrpp
[i
];
1815 if (i_shdrp
->sh_type
== SHT_REL
|| i_shdrp
->sh_type
== SHT_RELA
)
1817 i_shdrp
->sh_offset
= -1;
1820 off
= assign_file_position_for_section (i_shdrp
, off
);
1822 elf_tdata (abfd
)->next_file_pos
= off
;
1829 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1830 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1831 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
1832 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
1833 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
1837 struct strtab
*shstrtab
;
1839 i_ehdrp
= elf_elfheader (abfd
);
1840 i_shdrp
= elf_elfsections (abfd
);
1842 shstrtab
= bfd_new_strtab (abfd
);
1843 elf_shstrtab (abfd
) = shstrtab
;
1845 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
1846 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
1847 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
1848 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
1850 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS
;
1851 i_ehdrp
->e_ident
[EI_DATA
] =
1852 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
1853 i_ehdrp
->e_ident
[EI_VERSION
] = EV_CURRENT
;
1855 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
1856 i_ehdrp
->e_ident
[count
] = 0;
1858 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
) ? ET_EXEC
: ET_REL
;
1859 switch (bfd_get_arch (abfd
))
1861 case bfd_arch_unknown
:
1862 i_ehdrp
->e_machine
= EM_NONE
;
1864 case bfd_arch_sparc
:
1865 i_ehdrp
->e_machine
= EM_SPARC
;
1866 /* start-sanitize-v9 */
1868 i_ehdrp
->e_machine
= EM_SPARC64
;
1870 /* end-sanitize-v9 */
1873 i_ehdrp
->e_machine
= EM_386
;
1876 i_ehdrp
->e_machine
= EM_68K
;
1879 i_ehdrp
->e_machine
= EM_88K
;
1882 i_ehdrp
->e_machine
= EM_860
;
1884 case bfd_arch_mips
: /* MIPS Rxxxx */
1885 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
1888 i_ehdrp
->e_machine
= EM_HPPA
;
1890 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
1892 i_ehdrp
->e_machine
= EM_NONE
;
1894 i_ehdrp
->e_version
= EV_CURRENT
;
1895 i_ehdrp
->e_ehsize
= sizeof (Elf_External_Ehdr
);
1897 /* no program header, for now. */
1898 i_ehdrp
->e_phoff
= 0;
1899 i_ehdrp
->e_phentsize
= 0;
1900 i_ehdrp
->e_phnum
= 0;
1902 /* each bfd section is section header entry */
1903 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
1904 i_ehdrp
->e_shentsize
= sizeof (Elf_External_Shdr
);
1906 /* if we're building an executable, we'll need a program header table */
1907 if (abfd
->flags
& EXEC_P
)
1912 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
1914 /* elf_build_phdrs() returns a (NULL-terminated) array of
1915 Elf_Internal_Phdrs */
1916 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
1917 i_ehdrp
->e_phoff
= outbase
;
1918 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
1923 i_ehdrp
->e_phentsize
= 0;
1925 i_ehdrp
->e_phoff
= 0;
1928 elf_tdata (abfd
)->symtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
1930 elf_tdata (abfd
)->strtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
1932 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
1938 swap_out_syms (abfd
)
1941 struct strtab
*shstrtab
= elf_shstrtab (abfd
);
1943 elf_map_symbols (abfd
);
1945 /* Dump out the symtabs. */
1947 int symcount
= bfd_get_symcount (abfd
);
1948 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1949 struct strtab
*stt
= bfd_new_strtab (abfd
);
1950 Elf_Internal_Shdr
*symtab_hdr
;
1951 Elf_Internal_Shdr
*symstrtab_hdr
;
1952 Elf_External_Sym
*outbound_syms
;
1955 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1956 symtab_hdr
->sh_type
= SHT_SYMTAB
;
1957 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
1958 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
1959 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
1961 /* see assert in elf_fake_sections that supports this: */
1962 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
1963 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
1965 outbound_syms
= (Elf_External_Sym
*)
1966 bfd_alloc (abfd
, (1 + symcount
) * sizeof (Elf_External_Sym
));
1967 /* now generate the data (for "contents") */
1969 /* Fill in zeroth symbol and swap it out. */
1970 Elf_Internal_Sym sym
;
1976 sym
.st_shndx
= SHN_UNDEF
;
1977 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
1979 for (idx
= 0; idx
< symcount
; idx
++)
1981 Elf_Internal_Sym sym
;
1982 bfd_vma value
= syms
[idx
]->value
;
1984 if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
1985 /* Section symbols have no names. */
1988 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
1990 if (bfd_is_com_section (syms
[idx
]->section
))
1992 /* ELF common symbols put the alignment into the `value' field,
1993 and the size into the `size' field. This is backwards from
1994 how BFD handles it, so reverse it here. */
1995 sym
.st_size
= value
;
1996 /* Should retrieve this from somewhere... */
1998 sym
.st_shndx
= SHN_COMMON
;
2002 asection
*sec
= syms
[idx
]->section
;
2005 if (sec
->output_section
)
2007 value
+= sec
->output_offset
;
2008 sec
= sec
->output_section
;
2011 sym
.st_value
= value
;
2012 sym
.st_size
= (elf_symbol_from (abfd
, syms
[idx
]))->internal_elf_sym
.st_size
;
2013 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec
);
2017 /* Writing this would be a hell of a lot easier if we had
2018 some decent documentation on bfd, and knew what to expect
2019 of the library, and what to demand of applications. For
2020 example, it appears that `objcopy' might not set the
2021 section of a symbol to be a section that is actually in
2023 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2025 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec2
);
2026 assert (shndx
!= -1);
2030 if (bfd_is_com_section (syms
[idx
]->section
))
2031 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2032 else if (syms
[idx
]->section
== &bfd_und_section
)
2033 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2034 else if (syms
[idx
]->flags
& BSF_WEAK
)
2035 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, STT_OBJECT
);
2036 else if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2037 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2038 else if (syms
[idx
]->flags
& BSF_FILE
)
2039 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2040 else if (syms
[idx
]->flags
& (BSF_GLOBAL
| BSF_EXPORT
))
2042 if (syms
[idx
]->flags
& BSF_FUNCTION
)
2043 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_FUNC
);
2045 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2047 else if (syms
[idx
]->flags
& BSF_LOCAL
)
2049 if (syms
[idx
]->flags
& BSF_FUNCTION
)
2050 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FUNC
);
2052 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_OBJECT
);
2055 /* Default to local if flag isn't set at all. */
2056 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_OBJECT
);
2059 elf_swap_symbol_out (abfd
, &sym
,
2060 outbound_syms
+ elf_symtab_map (abfd
)[idx
]);
2063 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2064 symstrtab_hdr
->contents
= (PTR
) stt
->tab
;
2065 symstrtab_hdr
->sh_size
= stt
->length
;
2066 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2068 symstrtab_hdr
->sh_flags
= 0;
2069 symstrtab_hdr
->sh_addr
= 0;
2070 symstrtab_hdr
->sh_entsize
= 0;
2071 symstrtab_hdr
->sh_link
= 0;
2072 symstrtab_hdr
->sh_info
= 0;
2073 symstrtab_hdr
->sh_addralign
= 0;
2074 symstrtab_hdr
->size
= 0;
2077 /* put the strtab out too... */
2079 Elf_Internal_Shdr
*this_hdr
;
2081 this_hdr
= &elf_tdata(abfd
)->shstrtab_hdr
;
2082 this_hdr
->contents
= (PTR
) elf_shstrtab (abfd
)->tab
;
2083 this_hdr
->sh_size
= elf_shstrtab (abfd
)->length
;
2084 this_hdr
->sh_type
= SHT_STRTAB
;
2085 this_hdr
->sh_flags
= 0;
2086 this_hdr
->sh_addr
= 0;
2087 this_hdr
->sh_entsize
= 0;
2088 this_hdr
->sh_addralign
= 0;
2094 write_shdrs_and_ehdr (abfd
)
2097 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
2098 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2099 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2100 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
2101 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2105 struct strtab
*shstrtab
;
2107 i_ehdrp
= elf_elfheader (abfd
);
2108 i_shdrp
= elf_elfsections (abfd
);
2109 shstrtab
= elf_shstrtab (abfd
);
2111 /* swap the header before spitting it out... */
2114 elf_debug_file (i_ehdrp
);
2116 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
2117 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
2118 bfd_write ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
);
2120 /* If we're building an executable, fixup the program header table
2123 @@ For now, assume that the entries are in a fixed order: text,
2126 if (abfd
->flags
& EXEC_P
)
2128 static char *CONST section_name
[] =
2129 {".text", ".data", ".bss"};
2131 for (count
= 0; count
< 3; count
++)
2133 asection
*asect
= bfd_get_section_by_name (abfd
,
2134 section_name
[count
]);
2135 int sh_idx
= elf_section_from_bfd_section (abfd
, asect
);
2137 i_phdrp
[count
].p_offset
= i_shdrp
[sh_idx
]->sh_offset
;
2140 /* write out the program header table entries */
2141 elf_write_phdrs (abfd
, i_ehdrp
, i_phdrp
, i_ehdrp
->e_phnum
);
2144 /* at this point we've concocted all the ELF sections... */
2145 x_shdrp
= (Elf_External_Shdr
*)
2146 bfd_alloc (abfd
, sizeof (*x_shdrp
) * (i_ehdrp
->e_shnum
));
2149 bfd_error
= no_memory
;
2153 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2156 elf_debug_section (shstrtab
->tab
+ i_shdrp
[count
]->sh_name
, count
,
2159 elf_swap_shdr_out (abfd
, i_shdrp
[count
], x_shdrp
+ count
);
2161 bfd_seek (abfd
, (file_ptr
) i_ehdrp
->e_shoff
, SEEK_SET
);
2162 bfd_write ((PTR
) x_shdrp
, sizeof (*x_shdrp
), i_ehdrp
->e_shnum
, abfd
);
2163 /* need to dump the string table too... */
2169 assign_file_positions_for_relocs (abfd
)
2172 file_ptr off
= elf_tdata(abfd
)->next_file_pos
;
2174 Elf_Internal_Shdr
**shdrpp
= elf_elfsections (abfd
);
2175 Elf_Internal_Shdr
*shdrp
;
2176 for (i
= 0; i
< elf_elfheader(abfd
)->e_shnum
; i
++)
2179 if (shdrp
->sh_type
!= SHT_REL
&& shdrp
->sh_type
!= SHT_RELA
)
2181 off
= assign_file_position_for_section (shdrp
, off
);
2183 elf_tdata(abfd
)->next_file_pos
= off
;
2187 DEFUN (NAME(bfd_elf
,write_object_contents
), (abfd
), bfd
* abfd
)
2189 Elf_Internal_Ehdr
*i_ehdrp
;
2190 Elf_Internal_Shdr
**i_shdrp
;
2193 if (abfd
->output_has_begun
== false)
2196 prep_headers (abfd
);
2198 elf_compute_section_file_positions (abfd
);
2200 abfd
->output_has_begun
= true;
2203 i_shdrp
= elf_elfsections (abfd
);
2204 i_ehdrp
= elf_elfheader (abfd
);
2206 bfd_map_over_sections (abfd
, write_relocs
, (PTR
) 0);
2208 assign_file_positions_for_relocs (abfd
);
2210 /* After writing the headers, we need to write the sections too... */
2211 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2212 if (i_shdrp
[count
]->contents
)
2214 bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
);
2215 bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
, 1, abfd
);
2217 return write_shdrs_and_ehdr (abfd
);
2220 /* Given an index of a section, retrieve a pointer to it. Note
2221 that for our purposes, sections are indexed by {1, 2, ...} with
2222 0 being an illegal index. */
2224 /* In the original, each ELF section went into exactly one BFD
2225 section. This doesn't really make sense, so we need a real mapping.
2226 The mapping has to hide in the Elf_Internal_Shdr since asection
2227 doesn't have anything like a tdata field... */
2230 DEFUN (section_from_elf_index
, (abfd
, index
),
2234 /* @@ Is bfd_com_section really correct in all the places it could
2235 be returned from this routine? */
2237 if (index
== SHN_ABS
)
2238 return &bfd_com_section
; /* not abs? */
2239 if (index
== SHN_COMMON
)
2240 return &bfd_com_section
;
2242 if (index
> elf_elfheader (abfd
)->e_shnum
)
2246 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[index
];
2248 switch (hdr
->sh_type
)
2250 /* ELF sections that map to BFD sections */
2254 bfd_section_from_shdr (abfd
, index
);
2255 return (struct sec
*) hdr
->rawdata
;
2258 return (struct sec
*) &bfd_abs_section
;
2263 /* given a section, search the header to find them... */
2265 DEFUN (elf_section_from_bfd_section
, (abfd
, asect
),
2269 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2271 Elf_Internal_Shdr
*hdr
;
2272 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2274 if (asect
== &bfd_abs_section
)
2276 if (asect
== &bfd_com_section
)
2278 if (asect
== &bfd_und_section
)
2281 for (index
= 0; index
< maxindex
; index
++)
2283 hdr
= i_shdrp
[index
];
2284 switch (hdr
->sh_type
)
2286 /* ELF sections that map to BFD sections */
2291 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2302 /* given a symbol, return the bfd index for that symbol. */
2304 DEFUN (elf_symbol_from_bfd_symbol
, (abfd
, asym_ptr_ptr
),
2306 struct symbol_cache_entry
**asym_ptr_ptr
)
2308 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2309 CONST
char *name
= asym_ptr
->name
;
2311 int symcount
= bfd_get_symcount (abfd
);
2312 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2314 /* FIXME -- there has to be a better way than linear search. */
2315 for (idx
= 0; idx
< symcount
; idx
++)
2317 if (syms
[idx
] == asym_ptr
2318 || (name
== syms
[idx
]->name
&& name
)
2319 || ((asym_ptr
->flags
& BSF_SECTION_SYM
)
2320 && (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2321 && asym_ptr
->section
== syms
[idx
]->section
))
2325 if (idx
>= symcount
)
2328 fprintf (stderr
, "bfd app err: can't find sym `%s' in symtab\n",
2332 idx
= elf_symtab_map (abfd
)[idx
];
2336 flagword flags
= asym_ptr
->flags
;
2339 "elfsym<-bfdsym %.8lx `%s' sec=%s symnum=%d {",
2340 (long) asym_ptr
, asym_ptr
->name
, asym_ptr
->section
->name
, idx
);
2342 if (flags
== BSF_NO_FLAGS
)
2343 fprintf (stderr
, " none");
2345 if (flags
& BSF_LOCAL
)
2346 fprintf (stderr
, " local");
2348 if (flags
& BSF_GLOBAL
)
2349 fprintf (stderr
, " global");
2351 if (flags
& BSF_EXPORT
)
2352 fprintf (stderr
, " export");
2354 if (flags
& BSF_DEBUGGING
)
2355 fprintf (stderr
, " debugging");
2357 if (flags
& BSF_KEEP
)
2358 fprintf (stderr
, " keep");
2360 if (flags
& BSF_KEEP_G
)
2361 fprintf (stderr
, " keep_g");
2363 if (flags
& BSF_WEAK
)
2364 fprintf (stderr
, " weak");
2366 if (flags
& BSF_SECTION_SYM
)
2367 fprintf (stderr
, " section_sym");
2369 if (flags
& BSF_OLD_COMMON
)
2370 fprintf (stderr
, " old_common");
2372 if (flags
& BSF_NOT_AT_END
)
2373 fprintf (stderr
, " not_at_end");
2375 if (flags
& BSF_CONSTRUCTOR
)
2376 fprintf (stderr
, " constructor");
2378 if (flags
& BSF_WARNING
)
2379 fprintf (stderr
, " warning");
2381 if (flags
& BSF_INDIRECT
)
2382 fprintf (stderr
, " indirect");
2384 if (flags
& BSF_FILE
)
2385 fprintf (stderr
, " file");
2387 if (flags
& BSF_FUNCTION
)
2388 fprintf (stderr
, " function");
2390 fputs (" }\n", stderr
);
2399 DEFUN (elf_slurp_symbol_table
, (abfd
, symptrs
),
2401 asymbol
** symptrs
) /* Buffer for generated bfd symbols */
2403 Elf_Internal_Shdr
*hdr
= &elf_tdata(abfd
)->symtab_hdr
;
2404 int symcount
; /* Number of external ELF symbols */
2406 elf_symbol_type
*sym
; /* Pointer to current bfd symbol */
2407 elf_symbol_type
*symbase
; /* Buffer for generated bfd symbols */
2408 Elf_Internal_Sym i_sym
;
2409 Elf_External_Sym
*x_symp
;
2411 /* this is only valid because there is only one symtab... */
2412 /* FIXME: This is incorrect, there may also be a dynamic symbol
2413 table which is a subset of the full symbol table. We either need
2414 to be prepared to read both (and merge them) or ensure that we
2415 only read the full symbol table. Currently we only get called to
2416 read the full symbol table. -fnf */
2417 if (bfd_get_outsymbols (abfd
) != NULL
)
2422 /* Read each raw ELF symbol, converting from external ELF form to
2423 internal ELF form, and then using the information to create a
2424 canonical bfd symbol table entry.
2426 Note that we allocate the initial bfd canonical symbol buffer
2427 based on a one-to-one mapping of the ELF symbols to canonical
2428 symbols. We actually use all the ELF symbols, so there will be no
2429 space left over at the end. When we have all the symbols, we
2430 build the caller's pointer vector. */
2432 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2434 bfd_error
= system_call_error
;
2438 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2439 symbase
= (elf_symbol_type
*) bfd_zalloc (abfd
, symcount
* sizeof (elf_symbol_type
));
2442 /* Temporarily allocate room for the raw ELF symbols. */
2443 x_symp
= (Elf_External_Sym
*) bfd_xmalloc (symcount
* sizeof (Elf_External_Sym
));
2445 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
2446 != symcount
* sizeof (Elf_External_Sym
))
2448 free ((PTR
) x_symp
);
2449 bfd_error
= system_call_error
;
2452 /* Skip first symbol, which is a null dummy. */
2453 for (i
= 1; i
< symcount
; i
++)
2455 elf_swap_symbol_in (abfd
, x_symp
+ i
, &i_sym
);
2456 memcpy (&sym
->internal_elf_sym
, &i_sym
, sizeof (Elf_Internal_Sym
));
2457 memcpy (&sym
->native_elf_sym
, x_symp
+ i
, sizeof (Elf_External_Sym
));
2458 sym
->symbol
.the_bfd
= abfd
;
2460 sym
->symbol
.name
= elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2463 sym
->symbol
.value
= i_sym
.st_value
;
2465 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
2467 sym
->symbol
.section
= section_from_elf_index (abfd
, i_sym
.st_shndx
);
2469 else if (i_sym
.st_shndx
== SHN_ABS
)
2471 sym
->symbol
.section
= &bfd_abs_section
;
2473 else if (i_sym
.st_shndx
== SHN_COMMON
)
2475 sym
->symbol
.section
= &bfd_com_section
;
2476 /* Elf puts the alignment into the `value' field, and the size
2477 into the `size' field. BFD wants to see the size in the
2478 value field, and doesn't care (at the moment) about the
2480 sym
->symbol
.value
= i_sym
.st_size
;
2482 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2484 sym
->symbol
.section
= &bfd_und_section
;
2487 sym
->symbol
.section
= &bfd_abs_section
;
2489 sym
->symbol
.value
-= sym
->symbol
.section
->vma
;
2491 switch (ELF_ST_BIND (i_sym
.st_info
))
2494 sym
->symbol
.flags
|= BSF_LOCAL
;
2497 sym
->symbol
.flags
|= (BSF_GLOBAL
| BSF_EXPORT
);
2500 sym
->symbol
.flags
|= BSF_WEAK
;
2504 switch (ELF_ST_TYPE (i_sym
.st_info
))
2507 sym
->symbol
.flags
|= BSF_SECTION_SYM
| BSF_DEBUGGING
;
2510 sym
->symbol
.flags
|= BSF_FILE
| BSF_DEBUGGING
;
2513 sym
->symbol
.flags
|= BSF_FUNCTION
;
2516 /* Is this a definition of $global$? If so, keep it because it will be
2517 needd if any relocations are performed. */
2518 if (!strcmp (sym
->symbol
.name
, "$global$")
2519 && sym
->symbol
.section
!= &bfd_und_section
)
2521 /* @@ Why is this referring to backend data and not a field of
2523 struct elf_backend_data
*be_data
= (struct elf_backend_data
*) abfd
->xvec
->backend_data
;
2525 be_data
->global_sym
= (PTR
) sym
;
2530 /* We rely on the zalloc to clear out the final symbol entry. */
2532 /* obj_raw_syms macro uses a cast... */
2533 elf_tdata (abfd
)->raw_syms
= (PTR
) x_symp
;
2535 bfd_get_symcount (abfd
) = symcount
= sym
- symbase
;
2537 /* Fill in the user's symbol pointer vector if needed. */
2541 while (symcount
-- > 0)
2543 *symptrs
++ = &sym
->symbol
;
2546 *symptrs
= 0; /* Final null pointer */
2552 /* Return the number of bytes required to hold the symtab vector.
2554 Note that we base it on the count plus 1, since we will null terminate
2555 the vector allocated based on this size. However, the ELF symbol table
2556 always has a dummy entry as symbol #0, so it ends up even. */
2559 DEFUN (elf_get_symtab_upper_bound
, (abfd
), bfd
* abfd
)
2561 unsigned int symcount
;
2562 unsigned int symtab_size
= 0;
2564 Elf_Internal_Shdr
*hdr
= &elf_tdata(abfd
)->symtab_hdr
;
2565 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2566 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
));
2572 This function return the number of bytes required to store the
2573 relocation information associated with section <<sect>>
2574 attached to bfd <<abfd>>
2578 elf_get_reloc_upper_bound (abfd
, asect
)
2582 if (asect
->flags
& SEC_RELOC
)
2584 /* either rel or rela */
2585 return elf_section_data(asect
)->rel_hdr
.sh_size
;
2592 DEFUN (elf_slurp_reloca_table
, (abfd
, asect
, symbols
),
2597 Elf_External_Rela
*native_relocs
;
2598 arelent
*reloc_cache
;
2603 if (asect
->relocation
)
2605 if (asect
->reloc_count
== 0)
2607 if (asect
->flags
& SEC_CONSTRUCTOR
)
2610 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2611 native_relocs
= (Elf_External_Rela
*)
2612 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rela
));
2613 bfd_read ((PTR
) native_relocs
,
2614 sizeof (Elf_External_Rela
), asect
->reloc_count
, abfd
);
2616 reloc_cache
= (arelent
*)
2617 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2621 bfd_error
= no_memory
;
2625 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2627 #ifdef RELOC_PROCESSING
2628 Elf_Internal_Rela dst
;
2629 Elf_External_Rela
*src
;
2631 cache_ptr
= reloc_cache
+ idx
;
2632 src
= native_relocs
+ idx
;
2633 elf_swap_reloca_in (abfd
, src
, &dst
);
2635 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2637 Elf_Internal_Rela dst
;
2638 Elf_External_Rela
*src
;
2640 cache_ptr
= reloc_cache
+ idx
;
2641 src
= native_relocs
+ idx
;
2643 elf_swap_reloca_in (abfd
, src
, &dst
);
2645 if (asect
->flags
& SEC_RELOC
)
2647 /* relocatable, so the offset is off of the section */
2648 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2652 /* non-relocatable, so the offset a virtual address */
2653 cache_ptr
->address
= dst
.r_offset
;
2655 /* ELF_R_SYM(dst.r_info) is the symbol table offset; subtract 1
2656 because the first entry is NULL. */
2657 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
2659 /* Is it an ELF section symbol? If so, translate it into a
2660 BFD section symbol. */
2661 asymbol
*s
= *(cache_ptr
->sym_ptr_ptr
);
2662 if (s
->flags
& BSF_SECTION_SYM
)
2663 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
2665 cache_ptr
->addend
= dst
.r_addend
;
2667 /* Fill in the cache_ptr->howto field from dst.r_type */
2669 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2670 (*ebd
->elf_info_to_howto
) (abfd
, cache_ptr
, &dst
);
2675 asect
->relocation
= reloc_cache
;
2681 elf_debug_section (str
, num
, hdr
)
2684 Elf_Internal_Shdr
*hdr
;
2686 fprintf (stderr
, "\nSection#%d '%s' 0x%.8lx\n", num
, str
, (long) hdr
);
2688 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
2689 (long) hdr
->sh_name
,
2690 (long) hdr
->sh_type
,
2691 (long) hdr
->sh_flags
);
2693 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
2694 (long) hdr
->sh_addr
,
2695 (long) hdr
->sh_offset
,
2696 (long) hdr
->sh_size
);
2698 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
2699 (long) hdr
->sh_link
,
2700 (long) hdr
->sh_info
,
2701 (long) hdr
->sh_addralign
);
2702 fprintf (stderr
, "sh_entsize = %ld\n",
2703 (long) hdr
->sh_entsize
);
2704 fprintf (stderr
, "rawdata = 0x%.8lx\n", (long) hdr
->rawdata
);
2705 fprintf (stderr
, "contents = 0x%.8lx\n", (long) hdr
->contents
);
2706 fprintf (stderr
, "size = %ld\n", (long) hdr
->size
);
2711 elf_debug_file (ehdrp
)
2712 Elf_Internal_Ehdr
*ehdrp
;
2714 fprintf (stderr
, "e_entry = 0x%.8lx\n", (long) ehdrp
->e_entry
);
2715 fprintf (stderr
, "e_phoff = %ld\n", (long) ehdrp
->e_phoff
);
2716 fprintf (stderr
, "e_phnum = %ld\n", (long) ehdrp
->e_phnum
);
2717 fprintf (stderr
, "e_phentsize = %ld\n", (long) ehdrp
->e_phentsize
);
2718 fprintf (stderr
, "e_shoff = %ld\n", (long) ehdrp
->e_shoff
);
2719 fprintf (stderr
, "e_shnum = %ld\n", (long) ehdrp
->e_shnum
);
2720 fprintf (stderr
, "e_shentsize = %ld\n", (long) ehdrp
->e_shentsize
);
2725 DEFUN (elf_slurp_reloc_table
, (abfd
, asect
, symbols
),
2730 Elf_External_Rel
*native_relocs
;
2731 arelent
*reloc_cache
;
2733 Elf_Internal_Shdr
*data_hdr
;
2734 ElfNAME (Off
) data_off
;
2735 ElfNAME (Word
) data_max
;
2736 char buf
[4]; /* FIXME -- might be elf64 */
2740 if (asect
->relocation
)
2742 if (asect
->reloc_count
== 0)
2744 if (asect
->flags
& SEC_CONSTRUCTOR
)
2747 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2748 native_relocs
= (Elf_External_Rel
*)
2749 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rel
));
2750 bfd_read ((PTR
) native_relocs
,
2751 sizeof (Elf_External_Rel
), asect
->reloc_count
, abfd
);
2753 reloc_cache
= (arelent
*)
2754 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2758 bfd_error
= no_memory
;
2762 /* Get the offset of the start of the segment we are relocating to read in
2763 the implicit addend. */
2764 data_hdr
= &elf_section_data(asect
)->this_hdr
;
2765 data_off
= data_hdr
->sh_offset
;
2766 data_max
= data_hdr
->sh_size
- sizeof (buf
) + 1;
2769 elf_debug_section ("data section", -1, data_hdr
);
2772 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2774 #ifdef RELOC_PROCESSING
2775 Elf_Internal_Rel dst
;
2776 Elf_External_Rel
*src
;
2778 cache_ptr
= reloc_cache
+ idx
;
2779 src
= native_relocs
+ idx
;
2780 elf_swap_reloc_in (abfd
, src
, &dst
);
2782 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2784 Elf_Internal_Rel dst
;
2785 Elf_External_Rel
*src
;
2787 cache_ptr
= reloc_cache
+ idx
;
2788 src
= native_relocs
+ idx
;
2790 elf_swap_reloc_in (abfd
, src
, &dst
);
2792 if (asect
->flags
& SEC_RELOC
)
2794 /* relocatable, so the offset is off of the section */
2795 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2799 /* non-relocatable, so the offset a virtual address */
2800 cache_ptr
->address
= dst
.r_offset
;
2802 /* ELF_R_SYM(dst.r_info) is the symbol table offset...
2803 -1 is to skip the dummy symbol table entry */
2804 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
2805 BFD_ASSERT (dst
.r_offset
<= data_max
);
2806 if (bfd_seek (abfd
, data_off
+ dst
.r_offset
, SEEK_SET
) != 0
2807 || bfd_read ((PTR
) buf
, sizeof (buf
), 1, abfd
) != sizeof (buf
))
2809 bfd_error
= system_call_error
;
2813 cache_ptr
->addend
= (*abfd
->xvec
->bfd_getx_signed_32
) ((bfd_byte
*) buf
);
2815 /* Fill in the cache_ptr->howto field from dst.r_type */
2817 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2818 (*ebd
->elf_info_to_howto_rel
) (abfd
, cache_ptr
, &dst
);
2823 asect
->relocation
= reloc_cache
;
2828 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
2834 arelent
*tblptr
= section
->relocation
;
2835 unsigned int count
= 0;
2836 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
2838 /* snarfed from coffcode.h */
2840 elf_slurp_reloca_table (abfd
, section
, symbols
);
2842 elf_slurp_reloc_table (abfd
, section
, symbols
);
2844 tblptr
= section
->relocation
;
2848 for (; count
++ < section
->reloc_count
;)
2849 *relptr
++ = tblptr
++;
2852 return section
->reloc_count
;
2856 DEFUN (elf_get_symtab
, (abfd
, alocation
),
2858 asymbol
** alocation
)
2861 if (!elf_slurp_symbol_table (abfd
, alocation
))
2864 return bfd_get_symcount (abfd
);
2868 DEFUN (elf_make_empty_symbol
, (abfd
),
2871 elf_symbol_type
*newsym
;
2873 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
2876 bfd_error
= no_memory
;
2881 newsym
->symbol
.the_bfd
= abfd
;
2882 return &newsym
->symbol
;
2887 DEFUN (elf_get_symbol_info
, (ignore_abfd
, symbol
, ret
),
2888 bfd
* ignore_abfd AND
2889 asymbol
* symbol AND
2892 bfd_symbol_info (symbol
, ret
);
2896 DEFUN (elf_print_symbol
, (ignore_abfd
, filep
, symbol
, how
),
2897 bfd
* ignore_abfd AND
2899 asymbol
* symbol AND
2900 bfd_print_symbol_type how
)
2902 FILE *file
= (FILE *) filep
;
2905 case bfd_print_symbol_name
:
2906 fprintf (file
, "%s", symbol
->name
);
2908 case bfd_print_symbol_more
:
2909 fprintf (file
, "elf ");
2910 fprintf_vma (file
, symbol
->value
);
2911 fprintf (file
, " %lx", (long) symbol
->flags
);
2913 case bfd_print_symbol_all
:
2915 CONST
char *section_name
;
2916 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2917 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
2918 fprintf (file
, " %s\t%s",
2928 DEFUN (elf_get_lineno
, (ignore_abfd
, symbol
),
2929 bfd
* ignore_abfd AND
2932 fprintf (stderr
, "elf_get_lineno unimplemented\n");
2939 DEFUN (elf_set_arch_mach
, (abfd
, arch
, machine
),
2941 enum bfd_architecture arch AND
2942 unsigned long machine
)
2944 /* Allow any architecture to be supported by the elf backend */
2947 case bfd_arch_unknown
: /* EM_NONE */
2948 case bfd_arch_sparc
: /* EM_SPARC */
2949 case bfd_arch_i386
: /* EM_386 */
2950 case bfd_arch_m68k
: /* EM_68K */
2951 case bfd_arch_m88k
: /* EM_88K */
2952 case bfd_arch_i860
: /* EM_860 */
2953 case bfd_arch_mips
: /* EM_MIPS (MIPS R3000) */
2954 case bfd_arch_hppa
: /* EM_HPPA (HP PA_RISC) */
2955 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
2962 DEFUN (elf_find_nearest_line
, (abfd
,
2970 asection
* section AND
2971 asymbol
** symbols AND
2973 CONST
char **filename_ptr AND
2974 CONST
char **functionname_ptr AND
2975 unsigned int *line_ptr
)
2981 DEFUN (elf_sizeof_headers
, (abfd
, reloc
),
2985 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
2992 DEFUN (elf_set_section_contents
, (abfd
, section
, location
, offset
, count
),
2997 bfd_size_type count
)
2999 Elf_Internal_Shdr
*hdr
;
3001 if (abfd
->output_has_begun
== false) /* set by bfd.c handler? */
3003 /* do setup calculations (FIXME) */
3004 prep_headers (abfd
);
3005 elf_compute_section_file_positions (abfd
);
3006 abfd
->output_has_begun
= true;
3009 hdr
= &elf_section_data(section
)->this_hdr
;
3011 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
3013 if (bfd_write (location
, 1, count
, abfd
) != count
)
3020 DEFUN (elf_no_info_to_howto
, (abfd
, cache_ptr
, dst
),
3022 arelent
* cache_ptr AND
3023 Elf_Internal_Rela
* dst
)
3025 fprintf (stderr
, "elf RELA relocation support for target machine unimplemented\n");
3031 DEFUN (elf_no_info_to_howto_rel
, (abfd
, cache_ptr
, dst
),
3033 arelent
* cache_ptr AND
3034 Elf_Internal_Rel
* dst
)
3036 fprintf (stderr
, "elf REL relocation support for target machine unimplemented\n");
3042 /* Core file support */
3044 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3045 #include <sys/procfs.h>
3047 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
3048 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
3049 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
3055 DEFUN (bfd_prstatus
, (abfd
, descdata
, descsz
, filepos
),
3062 prstatus_t
*status
= (prstatus_t
*) 0;
3064 if (descsz
== sizeof (prstatus_t
))
3066 newsect
= bfd_make_section (abfd
, ".reg");
3067 newsect
->_raw_size
= sizeof (status
->pr_reg
);
3068 newsect
->filepos
= filepos
+ (long) &status
->pr_reg
;
3069 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3070 newsect
->alignment_power
= 2;
3071 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3073 memcpy (core_prstatus (abfd
), descdata
, descsz
);
3078 /* Stash a copy of the prpsinfo structure away for future use. */
3081 DEFUN (bfd_prpsinfo
, (abfd
, descdata
, descsz
, filepos
),
3089 if (descsz
== sizeof (prpsinfo_t
))
3091 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3093 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
3099 DEFUN (bfd_fpregset
, (abfd
, descdata
, descsz
, filepos
),
3107 newsect
= bfd_make_section (abfd
, ".reg2");
3108 newsect
->_raw_size
= descsz
;
3109 newsect
->filepos
= filepos
;
3110 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3111 newsect
->alignment_power
= 2;
3114 #endif /* HAVE_PROCFS */
3116 /* Return a pointer to the args (including the command name) that were
3117 seen by the program that generated the core dump. Note that for
3118 some reason, a spurious space is tacked onto the end of the args
3119 in some (at least one anyway) implementations, so strip it off if
3123 DEFUN (elf_core_file_failing_command
, (abfd
),
3127 if (core_prpsinfo (abfd
))
3129 prpsinfo_t
*p
= core_prpsinfo (abfd
);
3130 char *scan
= p
->pr_psargs
;
3135 if ((scan
> p
->pr_psargs
) && (*scan
== ' '))
3139 return p
->pr_psargs
;
3145 /* Return the number of the signal that caused the core dump. Presumably,
3146 since we have a core file, we got a signal of some kind, so don't bother
3147 checking the other process status fields, just return the signal number.
3151 DEFUN (elf_core_file_failing_signal
, (abfd
),
3155 if (core_prstatus (abfd
))
3157 return ((prstatus_t
*) (core_prstatus (abfd
)))->pr_cursig
;
3163 /* Check to see if the core file could reasonably be expected to have
3164 come for the current executable file. Note that by default we return
3165 true unless we find something that indicates that there might be a
3170 DEFUN (elf_core_file_matches_executable_p
, (core_bfd
, exec_bfd
),
3179 /* First, xvecs must match since both are ELF files for the same target. */
3181 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
3183 bfd_error
= system_call_error
;
3189 /* If no prpsinfo, just return true. Otherwise, grab the last component
3190 of the exec'd pathname from the prpsinfo. */
3192 if (core_prpsinfo (core_bfd
))
3194 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
))->pr_fname
);
3201 /* Find the last component of the executable pathname. */
3203 if ((execname
= strrchr (exec_bfd
->filename
, '/')) != NULL
)
3209 execname
= (char *) exec_bfd
->filename
;
3212 /* See if they match */
3214 return strcmp (execname
, corename
) ? false : true;
3220 #endif /* HAVE_PROCFS */
3223 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3224 the information that would normally be available from the /proc interface
3225 for the process, at the time the process dumped core. Currently this
3226 includes copies of the prstatus, prpsinfo, and fpregset structures.
3228 Since these structures are potentially machine dependent in size and
3229 ordering, bfd provides two levels of support for them. The first level,
3230 available on all machines since it does not require that the host
3231 have /proc support or the relevant include files, is to create a bfd
3232 section for each of the prstatus, prpsinfo, and fpregset structures,
3233 without any interpretation of their contents. With just this support,
3234 the bfd client will have to interpret the structures itself. Even with
3235 /proc support, it might want these full structures for it's own reasons.
3237 In the second level of support, where HAVE_PROCFS is defined, bfd will
3238 pick apart the structures to gather some additional information that
3239 clients may want, such as the general register set, the name of the
3240 exec'ed file and its arguments, the signal (if any) that caused the
3246 DEFUN (elf_corefile_note
, (abfd
, hdr
),
3248 Elf_Internal_Phdr
* hdr
)
3250 Elf_External_Note
*x_note_p
; /* Elf note, external form */
3251 Elf_Internal_Note i_note
; /* Elf note, internal form */
3252 char *buf
= NULL
; /* Entire note segment contents */
3253 char *namedata
; /* Name portion of the note */
3254 char *descdata
; /* Descriptor portion of the note */
3255 char *sectname
; /* Name to use for new section */
3256 long filepos
; /* File offset to descriptor data */
3259 if (hdr
->p_filesz
> 0
3260 && (buf
= (char *) bfd_xmalloc (hdr
->p_filesz
)) != NULL
3261 && bfd_seek (abfd
, hdr
->p_offset
, SEEK_SET
) != -1
3262 && bfd_read ((PTR
) buf
, hdr
->p_filesz
, 1, abfd
) == hdr
->p_filesz
)
3264 x_note_p
= (Elf_External_Note
*) buf
;
3265 while ((char *) x_note_p
< (buf
+ hdr
->p_filesz
))
3267 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->namesz
);
3268 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->descsz
);
3269 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->type
);
3270 namedata
= x_note_p
->name
;
3271 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
3272 filepos
= hdr
->p_offset
+ (descdata
- buf
);
3273 switch (i_note
.type
)
3276 /* process descdata as prstatus info */
3277 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
3278 sectname
= ".prstatus";
3281 /* process descdata as fpregset info */
3282 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
3283 sectname
= ".fpregset";
3286 /* process descdata as prpsinfo */
3287 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
3288 sectname
= ".prpsinfo";
3291 /* Unknown descriptor, just ignore it. */
3295 if (sectname
!= NULL
)
3297 newsect
= bfd_make_section (abfd
, sectname
);
3298 newsect
->_raw_size
= i_note
.descsz
;
3299 newsect
->filepos
= filepos
;
3300 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3301 newsect
->alignment_power
= 2;
3303 x_note_p
= (Elf_External_Note
*)
3304 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
3315 /* Core files are simply standard ELF formatted files that partition
3316 the file using the execution view of the file (program header table)
3317 rather than the linking view. In fact, there is no section header
3318 table in a core file.
3320 The process status information (including the contents of the general
3321 register set) and the floating point register set are stored in a
3322 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3323 that allow standard bfd access to the general registers (.reg) and the
3324 floating point registers (.reg2).
3329 DEFUN (elf_core_file_p
, (abfd
), bfd
* abfd
)
3331 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
3332 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3333 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
3334 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
3335 unsigned int phindex
;
3337 /* Read in the ELF header in external format. */
3339 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
3341 bfd_error
= system_call_error
;
3345 /* Now check to see if we have a valid ELF file, and one that BFD can
3346 make use of. The magic number must match, the address size ('class')
3347 and byte-swapping must match our XVEC entry, and it must have a
3348 program header table (FIXME: See comments re segments at top of this
3351 if (elf_file_p (&x_ehdr
) == false)
3354 bfd_error
= wrong_format
;
3358 /* FIXME, Check EI_VERSION here ! */
3362 int desired_address_size
= ELFCLASS32
;
3365 int desired_address_size
= ELFCLASS64
;
3368 if (x_ehdr
.e_ident
[EI_CLASS
] != desired_address_size
)
3372 /* Switch xvec to match the specified byte order. */
3373 switch (x_ehdr
.e_ident
[EI_DATA
])
3375 case ELFDATA2MSB
: /* Big-endian */
3376 if (abfd
->xvec
->byteorder_big_p
== false)
3379 case ELFDATA2LSB
: /* Little-endian */
3380 if (abfd
->xvec
->byteorder_big_p
== true)
3383 case ELFDATANONE
: /* No data encoding specified */
3384 default: /* Unknown data encoding specified */
3388 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3389 the tdata pointer in the bfd. */
3392 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
3393 if (elf_tdata (abfd
) == NULL
)
3395 bfd_error
= no_memory
;
3399 /* FIXME, `wrong' returns from this point onward, leak memory. */
3401 /* Now that we know the byte order, swap in the rest of the header */
3402 i_ehdrp
= elf_elfheader (abfd
);
3403 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
3405 elf_debug_file (i_ehdrp
);
3408 /* If there is no program header, or the type is not a core file, then
3410 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
3413 /* Allocate space for a copy of the program header table in
3414 internal form, seek to the program header table in the file,
3415 read it in, and convert it to internal form. As a simple sanity
3416 check, verify that the what BFD thinks is the size of each program
3417 header table entry actually matches the size recorded in the file. */
3419 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
3421 i_phdrp
= (Elf_Internal_Phdr
*)
3422 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
3425 bfd_error
= no_memory
;
3428 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
3430 bfd_error
= system_call_error
;
3433 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3435 if (bfd_read ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
3438 bfd_error
= system_call_error
;
3441 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
3444 /* Once all of the program headers have been read and converted, we
3445 can start processing them. */
3447 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3449 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
3450 if ((i_phdrp
+ phindex
)->p_type
== PT_NOTE
)
3452 elf_corefile_note (abfd
, i_phdrp
+ phindex
);
3456 /* Remember the entry point specified in the ELF file header. */
3458 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;