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
57 (3) ELF section symbols are handled rather sloppily now. This should
58 be cleaned up, and ELF section symbols reconciled with BFD section
63 #include <string.h> /* For strrchr and friends */
73 /* Renaming structures, typedefs, macros and functions to be size-specific. */
74 #define Elf_External_Ehdr NAME(Elf,External_Ehdr)
75 #define Elf_External_Sym NAME(Elf,External_Sym)
76 #define Elf_External_Shdr NAME(Elf,External_Shdr)
77 #define Elf_External_Phdr NAME(Elf,External_Phdr)
78 #define Elf_External_Rel NAME(Elf,External_Rel)
79 #define Elf_External_Rela NAME(Elf,External_Rela)
81 #define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command)
82 #define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal)
83 #define elf_core_file_matches_executable_p NAME(bfd_elf,core_file_matches_executable_p)
84 #define elf_object_p NAME(bfd_elf,object_p)
85 #define elf_core_file_p NAME(bfd_elf,core_file_p)
86 #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
87 #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
88 #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
89 #define elf_get_symtab NAME(bfd_elf,get_symtab)
90 #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol)
91 #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info)
92 #define elf_print_symbol NAME(bfd_elf,print_symbol)
93 #define elf_get_lineno NAME(bfd_elf,get_lineno)
94 #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach)
95 #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line)
96 #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers)
97 #define elf_set_section_contents NAME(bfd_elf,set_section_contents)
98 #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto)
99 #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel)
100 #define elf_new_section_hook NAME(bfd_elf,new_section_hook)
101 #define write_relocs NAME(bfd_elf,_write_relocs)
104 #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
105 #define ELF_R_SYM(X) ELF64_R_SYM(X)
106 #define ELFCLASS ELFCLASS64
110 #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
111 #define ELF_R_SYM(X) ELF32_R_SYM(X)
112 #define ELFCLASS ELFCLASS32
116 static int shstrtab_length_fixed
;
118 struct elf_sect_data
{
123 /* Forward declarations of static functions */
125 static struct sec
* section_from_elf_index
PARAMS ((bfd
*, int));
127 static int elf_section_from_bfd_section
PARAMS ((bfd
*, struct sec
*));
129 static boolean elf_slurp_symbol_table
PARAMS ((bfd
*, asymbol
**));
131 static int elf_symbol_from_bfd_symbol
PARAMS ((bfd
*,
132 struct symbol_cache_entry
**));
134 static void elf_map_symbols
PARAMS ((bfd
*));
135 static void swap_out_syms
PARAMS ((bfd
*));
138 static void elf_debug_section
PARAMS ((char *, int, Elf_Internal_Shdr
*));
139 static void elf_debug_file
PARAMS ((Elf_Internal_Ehdr
*));
142 #define elf_string_from_elf_strtab(abfd,strindex) \
143 elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
146 /* Structure swapping routines */
148 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
149 can be handled by explicitly specifying 32 bits or "the long type". */
151 #define put_word bfd_h_put_64
152 #define get_word bfd_h_get_64
155 #define put_word bfd_h_put_32
156 #define get_word bfd_h_get_32
159 /* Translate an ELF symbol in external format into an ELF symbol in internal
163 DEFUN (elf_swap_symbol_in
, (abfd
, src
, dst
),
165 Elf_External_Sym
* src AND
166 Elf_Internal_Sym
* dst
)
168 dst
->st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->st_name
);
169 dst
->st_value
= get_word (abfd
, (bfd_byte
*) src
->st_value
);
170 dst
->st_size
= get_word (abfd
, (bfd_byte
*) src
->st_size
);
171 dst
->st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_info
);
172 dst
->st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_other
);
173 dst
->st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->st_shndx
);
176 /* Translate an ELF symbol in internal format into an ELF symbol in external
180 DEFUN (elf_swap_symbol_out
, (abfd
, src
, dst
),
182 Elf_Internal_Sym
* src AND
183 Elf_External_Sym
* dst
)
185 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
186 put_word (abfd
, src
->st_value
, dst
->st_value
);
187 put_word (abfd
, src
->st_size
, dst
->st_size
);
188 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
189 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
190 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
194 /* Translate an ELF file header in external format into an ELF file header in
198 DEFUN (elf_swap_ehdr_in
, (abfd
, src
, dst
),
200 Elf_External_Ehdr
* src AND
201 Elf_Internal_Ehdr
* dst
)
203 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
204 dst
->e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_type
);
205 dst
->e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_machine
);
206 dst
->e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_version
);
207 dst
->e_entry
= get_word (abfd
, (bfd_byte
*) src
->e_entry
);
208 dst
->e_phoff
= get_word (abfd
, (bfd_byte
*) src
->e_phoff
);
209 dst
->e_shoff
= get_word (abfd
, (bfd_byte
*) src
->e_shoff
);
210 dst
->e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_flags
);
211 dst
->e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_ehsize
);
212 dst
->e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phentsize
);
213 dst
->e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phnum
);
214 dst
->e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shentsize
);
215 dst
->e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shnum
);
216 dst
->e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shstrndx
);
219 /* Translate an ELF file header in internal format into an ELF file header in
223 DEFUN (elf_swap_ehdr_out
, (abfd
, src
, dst
),
225 Elf_Internal_Ehdr
* src AND
226 Elf_External_Ehdr
* dst
)
228 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
229 /* note that all elements of dst are *arrays of unsigned char* already... */
230 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
231 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
232 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
233 put_word (abfd
, src
->e_entry
, dst
->e_entry
);
234 put_word (abfd
, src
->e_phoff
, dst
->e_phoff
);
235 put_word (abfd
, src
->e_shoff
, dst
->e_shoff
);
236 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
237 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
238 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
239 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
240 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
241 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
242 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
246 /* Translate an ELF section header table entry in external format into an
247 ELF section header table entry in internal format. */
250 DEFUN (elf_swap_shdr_in
, (abfd
, src
, dst
),
252 Elf_External_Shdr
* src AND
253 Elf_Internal_Shdr
* dst
)
255 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
256 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
257 dst
->sh_flags
= get_word (abfd
, (bfd_byte
*) src
->sh_flags
);
258 dst
->sh_addr
= get_word (abfd
, (bfd_byte
*) src
->sh_addr
);
259 dst
->sh_offset
= get_word (abfd
, (bfd_byte
*) src
->sh_offset
);
260 dst
->sh_size
= get_word (abfd
, (bfd_byte
*) src
->sh_size
);
261 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
262 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
263 dst
->sh_addralign
= get_word (abfd
, (bfd_byte
*) src
->sh_addralign
);
264 dst
->sh_entsize
= get_word (abfd
, (bfd_byte
*) src
->sh_entsize
);
265 /* we haven't done any processing on it yet, so... */
266 dst
->rawdata
= (void *) 0;
269 /* Translate an ELF section header table entry in internal format into an
270 ELF section header table entry in external format. */
273 DEFUN (elf_swap_shdr_out
, (abfd
, src
, dst
),
275 Elf_Internal_Shdr
* src AND
276 Elf_External_Shdr
* dst
)
278 /* note that all elements of dst are *arrays of unsigned char* already... */
279 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
280 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
281 put_word (abfd
, src
->sh_flags
, dst
->sh_flags
);
282 put_word (abfd
, src
->sh_addr
, dst
->sh_addr
);
283 put_word (abfd
, src
->sh_offset
, dst
->sh_offset
);
284 put_word (abfd
, src
->sh_size
, dst
->sh_size
);
285 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
286 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
287 put_word (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
288 put_word (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
292 /* Translate an ELF program header table entry in external format into an
293 ELF program header table entry in internal format. */
296 DEFUN (elf_swap_phdr_in
, (abfd
, src
, dst
),
298 Elf_External_Phdr
* src AND
299 Elf_Internal_Phdr
* dst
)
301 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
302 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
303 dst
->p_offset
= get_word (abfd
, (bfd_byte
*) src
->p_offset
);
304 dst
->p_vaddr
= get_word (abfd
, (bfd_byte
*) src
->p_vaddr
);
305 dst
->p_paddr
= get_word (abfd
, (bfd_byte
*) src
->p_paddr
);
306 dst
->p_filesz
= get_word (abfd
, (bfd_byte
*) src
->p_filesz
);
307 dst
->p_memsz
= get_word (abfd
, (bfd_byte
*) src
->p_memsz
);
308 dst
->p_align
= get_word (abfd
, (bfd_byte
*) src
->p_align
);
312 DEFUN (elf_swap_phdr_out
, (abfd
, src
, dst
),
314 Elf_Internal_Phdr
* src AND
315 Elf_External_Phdr
* dst
)
317 /* note that all elements of dst are *arrays of unsigned char* already... */
318 bfd_h_put_32 (abfd
, src
->p_type
, dst
->p_type
);
319 put_word (abfd
, src
->p_offset
, dst
->p_offset
);
320 put_word (abfd
, src
->p_vaddr
, dst
->p_vaddr
);
321 put_word (abfd
, src
->p_paddr
, dst
->p_paddr
);
322 put_word (abfd
, src
->p_filesz
, dst
->p_filesz
);
323 put_word (abfd
, src
->p_memsz
, dst
->p_memsz
);
324 bfd_h_put_32 (abfd
, src
->p_flags
, dst
->p_flags
);
325 put_word (abfd
, src
->p_align
, dst
->p_align
);
328 /* Translate an ELF reloc from external format to internal format. */
330 DEFUN (elf_swap_reloc_in
, (abfd
, src
, dst
),
332 Elf_External_Rel
* src AND
333 Elf_Internal_Rel
* dst
)
335 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
336 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
340 DEFUN (elf_swap_reloca_in
, (abfd
, src
, dst
),
342 Elf_External_Rela
* src AND
343 Elf_Internal_Rela
* dst
)
345 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
346 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
347 dst
->r_addend
= get_word (abfd
, (bfd_byte
*) src
->r_addend
);
350 /* Translate an ELF reloc from internal format to external format. */
352 DEFUN (elf_swap_reloc_out
, (abfd
, src
, dst
),
354 Elf_Internal_Rel
* src AND
355 Elf_External_Rel
* dst
)
357 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
358 put_word (abfd
, src
->r_info
, dst
->r_info
);
362 DEFUN (elf_swap_reloca_out
, (abfd
, src
, dst
),
364 Elf_Internal_Rela
* src AND
365 Elf_External_Rela
* dst
)
367 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
368 put_word (abfd
, src
->r_info
, dst
->r_info
);
369 put_word (abfd
, src
->r_addend
, dst
->r_addend
);
374 /* String table creation/manipulation routines */
376 static struct strtab
*
377 DEFUN (bfd_new_strtab
, (abfd
),
382 ss
= (struct strtab
*) bfd_xmalloc (sizeof (struct strtab
));
383 ss
->tab
= bfd_xmalloc (1);
384 BFD_ASSERT (ss
->tab
!= 0);
393 DEFUN (bfd_add_to_strtab
, (abfd
, ss
, str
),
395 struct strtab
*ss AND
398 /* should search first, but for now: */
399 /* include the trailing NUL */
400 int ln
= strlen (str
) + 1;
402 /* should this be using obstacks? */
403 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
405 BFD_ASSERT (ss
->tab
!= 0);
406 strcpy (ss
->tab
+ ss
->length
, str
);
410 return ss
->length
- ln
;
414 DEFUN (bfd_add_2_to_strtab
, (abfd
, ss
, str
, str2
),
416 struct strtab
*ss AND
420 /* should search first, but for now: */
421 /* include the trailing NUL */
422 int ln
= strlen (str
) + strlen (str2
) + 1;
424 /* should this be using obstacks? */
426 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
428 ss
->tab
= bfd_xmalloc (ln
);
430 BFD_ASSERT (ss
->tab
!= 0);
431 strcpy (ss
->tab
+ ss
->length
, str
);
432 strcpy (ss
->tab
+ ss
->length
+ strlen (str
), str2
);
436 return ss
->length
- ln
;
440 /* ELF .o/exec file reading */
442 /* Create a new bfd section from an ELF section header. */
445 DEFUN (bfd_section_from_shdr
, (abfd
, shindex
),
447 unsigned int shindex
)
449 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
450 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
454 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
456 switch (hdr
->sh_type
)
460 /* inactive section. Throw it away. */
464 /* Bits that get saved. This one is real. */
467 newsect
= bfd_make_section (abfd
, name
);
470 newsect
->filepos
= hdr
->sh_offset
; /* so we can read back the bits */
471 newsect
->flags
|= SEC_HAS_CONTENTS
;
472 newsect
->vma
= hdr
->sh_addr
;
473 newsect
->_raw_size
= hdr
->sh_size
;
474 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
476 if (hdr
->sh_flags
& SHF_ALLOC
)
478 newsect
->flags
|= SEC_ALLOC
;
479 newsect
->flags
|= SEC_LOAD
;
482 if (!(hdr
->sh_flags
& SHF_WRITE
))
483 newsect
->flags
|= SEC_READONLY
;
485 if (hdr
->sh_flags
& SHF_EXECINSTR
)
486 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
487 else if (newsect
->flags
& SEC_ALLOC
)
488 newsect
->flags
|= SEC_DATA
;
490 /* The debugging sections appear to recognized only by
492 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
493 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
494 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
495 newsect
->flags
|= SEC_DEBUGGING
;
497 hdr
->rawdata
= (void *) newsect
;
500 hdr
->rawdata
= (void *) bfd_get_section_by_name (abfd
, name
);
505 /* Bits that get saved. This one is real. */
508 newsect
= bfd_make_section (abfd
, name
);
511 newsect
->vma
= hdr
->sh_addr
;
512 newsect
->_raw_size
= hdr
->sh_size
;
513 newsect
->filepos
= hdr
->sh_offset
; /* fake */
514 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
515 if (hdr
->sh_flags
& SHF_ALLOC
)
516 newsect
->flags
|= SEC_ALLOC
;
518 if (!(hdr
->sh_flags
& SHF_WRITE
))
519 newsect
->flags
|= SEC_READONLY
;
521 /* FIXME: This section is empty. Does it really make
522 sense to set SEC_CODE for it? */
523 if (hdr
->sh_flags
& SHF_EXECINSTR
)
524 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
526 hdr
->rawdata
= (void *) newsect
;
531 case SHT_SYMTAB
: /* A symbol table */
532 if (elf_onesymtab (abfd
) == shindex
)
535 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
536 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
537 elf_onesymtab (abfd
) = shindex
;
538 elf_tdata(abfd
)->symtab_hdr
= *hdr
;
539 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->symtab_hdr
;
540 abfd
->flags
|= HAS_SYMS
;
543 case SHT_STRTAB
: /* A string table */
546 if (ehdr
->e_shstrndx
== shindex
)
548 elf_tdata(abfd
)->shstrtab_hdr
= *hdr
;
549 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->shstrtab_hdr
;
550 hdr
->rawdata
= (PTR
) &elf_tdata(abfd
)->shstrtab_hdr
;
556 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
558 Elf_Internal_Shdr
*hdr2
= elf_elfsections(abfd
)[i
];
559 if (hdr2
->sh_link
== shindex
)
561 bfd_section_from_shdr (abfd
, i
);
562 if (elf_onesymtab (abfd
) == i
)
564 elf_tdata(abfd
)->strtab_hdr
= *hdr
;
565 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->strtab_hdr
;
568 #if 0 /* Not handling other string tables specially right now. */
569 hdr2
= elf_elfsections(abfd
)[i
]; /* in case it moved */
570 /* We have a strtab for some random other section. */
571 newsect
= (asection
*) hdr2
->rawdata
;
574 hdr
->rawdata
= (PTR
) newsect
;
575 hdr2
= &elf_section_data (newsect
)->str_hdr
;
577 elf_elfsections(abfd
)[shindex
] = hdr2
;
583 newsect
= bfd_make_section (abfd
, name
);
586 newsect
->filepos
= hdr
->sh_offset
; /* so we can read back the bits */
587 newsect
->flags
= SEC_HAS_CONTENTS
;
588 hdr
->rawdata
= (PTR
) newsect
;
589 newsect
->_raw_size
= hdr
->sh_size
;
590 newsect
->alignment_power
= 0;
593 if (hdr
->sh_flags
& SHF_ALLOC
)
594 newsect
->flags
|= SEC_ALLOC
|SEC_LOAD
;
595 if (!(hdr
->sh_flags
& SHF_WRITE
))
596 newsect
->flags
|= SEC_READONLY
;
597 if (hdr
->sh_flags
& SHF_EXECINSTR
)
598 newsect
->flags
|= SEC_CODE
;
599 else if (newsect
->flags
& SEC_ALLOC
)
600 newsect
->flags
|= SEC_DATA
;
602 /* Check for debugging string tables. */
603 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
604 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
605 newsect
->flags
|= SEC_DEBUGGING
;
612 /* *These* do a lot of work -- but build no sections!
613 The spec says there can be multiple strtabs, but only one symtab,
614 but there can be lots of REL* sections. */
615 /* FIXME: The above statement is wrong! There are typically at least
616 two symbol tables in a dynamically linked executable, ".dynsym"
617 which is the dynamic linkage symbol table and ".symtab", which is
618 the "traditional" symbol table. -fnf */
621 asection
*target_sect
;
622 Elf_Internal_Shdr
*hdr2
;
623 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
625 /* Don't allow REL relocations on a machine that uses RELA and
627 /* @@ Actually, the generic ABI does suggest that both might be
628 used in one file. But the four ABI Processor Supplements I
629 have access to right now all specify that only one is used on
630 each of those architectures. It's conceivable that, e.g., a
631 bunch of absolute 32-bit relocs might be more compact in REL
632 form even on a RELA machine... */
633 BFD_ASSERT (!(use_rela_p
&& (hdr
->sh_type
== SHT_REL
)));
634 BFD_ASSERT (!(!use_rela_p
&& (hdr
->sh_type
== SHT_RELA
)));
635 BFD_ASSERT (hdr
->sh_entsize
==
637 ? sizeof (Elf_External_Rela
)
638 : sizeof (Elf_External_Rel
)));
640 bfd_section_from_shdr (abfd
, hdr
->sh_info
); /* target */
641 bfd_section_from_shdr (abfd
, hdr
->sh_link
); /* symbol table */
642 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
643 if (target_sect
== NULL
)
646 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
648 elf_elfsections(abfd
)[shindex
] = hdr2
;
649 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
650 target_sect
->flags
|= SEC_RELOC
;
651 target_sect
->relocation
= 0;
652 target_sect
->rel_filepos
= hdr
->sh_offset
;
653 abfd
->flags
|= HAS_RELOC
;
660 case SHT_DYNSYM
: /* could treat this like symtab... */
662 fprintf (stderr
, "Dynamic Linking sections not yet supported.\n");
669 fprintf (stderr
, "Note Sections not yet supported.\n");
676 fprintf (stderr
, "SHLIB Sections not supported (and non conforming.)\n");
681 /* Check for any processor-specific section types. */
683 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
685 if (bed
->elf_backend_section_from_shdr
)
686 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
695 DEFUN (elf_new_section_hook
, (abfd
, sec
),
699 struct bfd_elf_section_data
*sdata
;
701 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
702 sec
->used_by_bfd
= (PTR
) sdata
;
703 memset (sdata
, 0, sizeof (*sdata
));
707 /* Create a new bfd section from an ELF program header.
709 Since program segments have no names, we generate a synthetic name
710 of the form segment<NUM>, where NUM is generally the index in the
711 program header table. For segments that are split (see below) we
712 generate the names segment<NUM>a and segment<NUM>b.
714 Note that some program segments may have a file size that is different than
715 (less than) the memory size. All this means is that at execution the
716 system must allocate the amount of memory specified by the memory size,
717 but only initialize it with the first "file size" bytes read from the
718 file. This would occur for example, with program segments consisting
719 of combined data+bss.
721 To handle the above situation, this routine generates TWO bfd sections
722 for the single program segment. The first has the length specified by
723 the file size of the segment, and the second has the length specified
724 by the difference between the two sizes. In effect, the segment is split
725 into it's initialized and uninitialized parts.
730 DEFUN (bfd_section_from_phdr
, (abfd
, hdr
, index
),
732 Elf_Internal_Phdr
* hdr AND
740 split
= ((hdr
->p_memsz
> 0) &&
741 (hdr
->p_filesz
> 0) &&
742 (hdr
->p_memsz
> hdr
->p_filesz
));
743 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
744 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
745 strcpy (name
, namebuf
);
746 newsect
= bfd_make_section (abfd
, name
);
747 newsect
->vma
= hdr
->p_vaddr
;
748 newsect
->_raw_size
= hdr
->p_filesz
;
749 newsect
->filepos
= hdr
->p_offset
;
750 newsect
->flags
|= SEC_HAS_CONTENTS
;
751 if (hdr
->p_type
== PT_LOAD
)
753 newsect
->flags
|= SEC_ALLOC
;
754 newsect
->flags
|= SEC_LOAD
;
755 if (hdr
->p_flags
& PF_X
)
757 /* FIXME: all we known is that it has execute PERMISSION,
759 newsect
->flags
|= SEC_CODE
;
762 if (!(hdr
->p_flags
& PF_W
))
764 newsect
->flags
|= SEC_READONLY
;
769 sprintf (namebuf
, "segment%db", index
);
770 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
771 strcpy (name
, namebuf
);
772 newsect
= bfd_make_section (abfd
, name
);
773 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
774 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
775 if (hdr
->p_type
== PT_LOAD
)
777 newsect
->flags
|= SEC_ALLOC
;
778 if (hdr
->p_flags
& PF_X
)
779 newsect
->flags
|= SEC_CODE
;
781 if (!(hdr
->p_flags
& PF_W
))
782 newsect
->flags
|= SEC_READONLY
;
788 /* Begin processing a given object.
790 First we validate the file by reading in the ELF header and checking
793 static INLINE boolean
794 DEFUN (elf_file_p
, (x_ehdrp
), Elf_External_Ehdr
* x_ehdrp
)
796 return ((x_ehdrp
->e_ident
[EI_MAG0
] == ELFMAG0
)
797 && (x_ehdrp
->e_ident
[EI_MAG1
] == ELFMAG1
)
798 && (x_ehdrp
->e_ident
[EI_MAG2
] == ELFMAG2
)
799 && (x_ehdrp
->e_ident
[EI_MAG3
] == ELFMAG3
));
802 /* Check to see if the file associated with ABFD matches the target vector
805 Note that we may be called several times with the same ABFD, but different
806 target vectors, most of which will not match. We have to avoid leaving
807 any side effects in ABFD, or any data it points to (like tdata), if the
808 file does not match the target vector.
810 FIXME: There is memory leak if we are called more than once with the same
811 ABFD, and that bfd already has tdata allocated, since we allocate more tdata
812 and the old tdata is orphaned. Since it's in the bfd obstack, there isn't
813 much we can do about this except possibly rewrite the code. There are
814 also other bfd_allocs that may be the source of memory leaks as well. */
817 DEFUN (elf_object_p
, (abfd
), bfd
* abfd
)
819 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
820 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
821 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
822 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
824 char *shstrtab
; /* Internal copy of section header stringtab */
825 struct elf_backend_data
*ebd
; /* Use to get ELF_ARCH stored in xvec */
826 struct elf_obj_tdata
*preserved_tdata
= elf_tdata (abfd
);
828 /* Read in the ELF header in external format. */
830 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
831 goto got_system_call_error
;
833 /* Now check to see if we have a valid ELF file, and one that BFD can
834 make use of. The magic number must match, the address size ('class')
835 and byte-swapping must match our XVEC entry, and it must have a
836 section header table (FIXME: See comments re sections at top of this
839 if ((elf_file_p (&x_ehdr
) == false) ||
840 (x_ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) ||
841 (x_ehdr
.e_ident
[EI_CLASS
] != ELFCLASS
))
842 goto got_wrong_format_error
;
844 /* Check that file's byte order matches xvec's */
845 switch (x_ehdr
.e_ident
[EI_DATA
])
847 case ELFDATA2MSB
: /* Big-endian */
848 if (!abfd
->xvec
->header_byteorder_big_p
)
849 goto got_wrong_format_error
;
851 case ELFDATA2LSB
: /* Little-endian */
852 if (abfd
->xvec
->header_byteorder_big_p
)
853 goto got_wrong_format_error
;
855 case ELFDATANONE
: /* No data encoding specified */
856 default: /* Unknown data encoding specified */
857 goto got_wrong_format_error
;
860 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
861 the tdata pointer in the bfd. FIXME: memory leak, see above. */
864 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
865 if (elf_tdata (abfd
) == NULL
)
866 goto got_no_memory_error
;
868 /* Now that we know the byte order, swap in the rest of the header */
869 i_ehdrp
= elf_elfheader (abfd
);
870 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
872 elf_debug_file (i_ehdrp
);
875 /* If there is no section header table, we're hosed. */
876 if (i_ehdrp
->e_shoff
== 0)
877 goto got_wrong_format_error
;
879 if (i_ehdrp
->e_type
== ET_EXEC
|| i_ehdrp
->e_type
== ET_DYN
)
880 abfd
->flags
|= EXEC_P
;
882 /* Retrieve the architecture information from the xvec and verify
883 that it matches the machine info stored in the ELF header.
884 This allows us to resolve ambiguous formats that might not
885 otherwise be distinguishable. */
887 ebd
= get_elf_backend_data (abfd
);
889 /* Perhaps the elf architecture value should be another field in the
890 elf backend data? If you change this to work that way, make sure
891 that you still get bfd_arch_unknown for unknown architecture types,
892 and that it still gets accepted by the `generic' elf target. */
895 enum bfd_architecture arch
= bfd_arch_unknown
;
897 for (i
= 0; i
< bfd_elf_arch_map_size
; i
++)
899 if (bfd_elf_arch_map
[i
].elf_arch
== i_ehdrp
->e_machine
)
901 arch
= bfd_elf_arch_map
[i
].bfd_arch
;
905 /* start-sanitize-v9 */
906 if (i_ehdrp
->e_machine
== EM_SPARC64
)
907 arch
= bfd_arch_sparc
;
908 /* end-sanitize-v9 */
909 if (ebd
->arch
!= arch
)
910 goto got_wrong_format_error
;
911 bfd_default_set_arch_mach (abfd
, arch
, 0);
914 /* Allocate space for a copy of the section header table in
915 internal form, seek to the section header table in the file,
916 read it in, and convert it to internal form. As a simple sanity
917 check, verify that the what BFD thinks is the size of each section
918 header table entry actually matches the size recorded in the file. */
920 if (i_ehdrp
->e_shentsize
!= sizeof (x_shdr
))
921 goto got_wrong_format_error
;
922 i_shdrp
= (Elf_Internal_Shdr
*)
923 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
);
924 elf_elfsections (abfd
) =
925 (Elf_Internal_Shdr
**) bfd_alloc (abfd
, sizeof (i_shdrp
) * i_ehdrp
->e_shnum
);
926 if (!i_shdrp
|| !elf_elfsections(abfd
))
927 goto got_no_memory_error
;
928 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) == -1)
929 goto got_system_call_error
;
930 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
932 if (bfd_read ((PTR
) & x_shdr
, sizeof x_shdr
, 1, abfd
) != sizeof (x_shdr
))
933 goto got_system_call_error
;
934 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
935 elf_elfsections(abfd
)[shindex
] = i_shdrp
+ shindex
;
937 if (i_ehdrp
->e_shstrndx
)
939 bfd_section_from_shdr (abfd
, i_ehdrp
->e_shstrndx
);
943 for (shindex
= i_ehdrp
->e_shnum
- 1; shindex
>= 0; shindex
--)
945 if (!strcmp (elf_string_from_elf_strtab (abfd
,
946 i_shdrp
[shindex
].sh_name
),
949 elf_tdata(abfd
)->strtab_hdr
= i_shdrp
[shindex
];
950 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->strtab_hdr
;
952 else if (!strcmp (elf_string_from_elf_strtab (abfd
,
953 i_shdrp
[shindex
].sh_name
),
956 elf_tdata(abfd
)->symtab_hdr
= i_shdrp
[shindex
];
957 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->symtab_hdr
;
958 elf_onesymtab (abfd
) = shindex
;
963 /* Read in the string table containing the names of the sections. We
964 will need the base pointer to this table later. */
965 /* We read this inline now, so that we don't have to go through
966 bfd_section_from_shdr with it (since this particular strtab is
967 used to find all of the ELF section names.) */
969 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
971 goto got_wrong_format_error
;
973 /* Once all of the section headers have been read and converted, we
974 can start processing them. Note that the first section header is
975 a dummy placeholder entry, so we ignore it.
977 We also watch for the symbol table section and remember the file
978 offset and section size for both the symbol table section and the
979 associated string table section. */
981 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
983 bfd_section_from_shdr (abfd
, shindex
);
986 /* Remember the entry point specified in the ELF file header. */
988 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
992 /* If we are going to use goto's to avoid duplicating error setting
993 and return(NULL) code, then this at least makes it more maintainable. */
995 got_system_call_error
:
996 bfd_error
= system_call_error
;
998 got_wrong_format_error
:
999 bfd_error
= wrong_format
;
1001 got_no_memory_error
:
1002 bfd_error
= no_memory
;
1005 elf_tdata (abfd
) = preserved_tdata
;
1010 /* ELF .o/exec file writing */
1012 /* Takes a bfd and a symbol, returns a pointer to the elf specific area
1013 of the symbol if there is one. */
1014 static INLINE elf_symbol_type
*
1015 DEFUN (elf_symbol_from
, (ignore_abfd
, symbol
),
1016 bfd
* ignore_abfd AND
1019 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
1022 if (symbol
->the_bfd
->tdata
.elf_obj_data
== (struct elf_obj_tdata
*) NULL
)
1025 return (elf_symbol_type
*) symbol
;
1028 /* Create ELF output from BFD sections.
1030 Essentially, just create the section header and forget about the program
1034 DEFUN (elf_make_sections
, (abfd
, asect
, obj
),
1036 asection
* asect AND
1039 /* most of what is in bfd_shdr_from_section goes in here... */
1040 /* and all of these sections generate at *least* one ELF section. */
1043 Elf_Internal_Shdr
*this_hdr
;
1044 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1046 this_hdr
->sh_addr
= asect
->vma
;
1047 this_hdr
->sh_size
= asect
->_raw_size
;
1048 /* contents already set by elf_set_section_contents */
1050 if (asect
->flags
& SEC_RELOC
)
1052 /* emit a reloc section, and thus strtab and symtab... */
1053 Elf_Internal_Shdr
*rela_hdr
;
1054 Elf_External_Rela
*outbound_relocas
;
1055 Elf_External_Rel
*outbound_relocs
;
1056 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1058 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1060 /* orelocation has the data, reloc_count has the count... */
1063 rela_hdr
->sh_type
= SHT_RELA
;
1064 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1067 /* REL relocations */
1069 rela_hdr
->sh_type
= SHT_REL
;
1070 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rel
);
1072 rela_hdr
->sh_flags
= 0;
1073 rela_hdr
->sh_addr
= 0;
1074 rela_hdr
->sh_offset
= 0;
1075 rela_hdr
->sh_addralign
= 0;
1078 if (asect
->flags
& SEC_ALLOC
)
1080 this_hdr
->sh_flags
|= SHF_ALLOC
;
1081 if (asect
->flags
& SEC_LOAD
)
1083 /* @@ Do something with sh_type? */
1086 if (!(asect
->flags
& SEC_READONLY
))
1087 this_hdr
->sh_flags
|= SHF_WRITE
;
1089 if (asect
->flags
& SEC_CODE
)
1090 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1094 write_relocs (abfd
, sec
, xxx
)
1099 Elf_Internal_Shdr
*rela_hdr
;
1100 Elf_External_Rela
*outbound_relocas
;
1101 Elf_External_Rel
*outbound_relocs
;
1103 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1104 asymbol
*last_sym
= 0;
1107 if ((sec
->flags
& SEC_RELOC
) == 0)
1109 /* Flags are sometimes inconsistent. */
1110 if (sec
->reloc_count
== 0)
1113 rela_hdr
= &elf_section_data (sec
)->rel_hdr
;
1115 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* sec
->reloc_count
;
1116 rela_hdr
->contents
= (void *) bfd_alloc (abfd
, rela_hdr
->sh_size
);
1118 /* orelocation has the data, reloc_count has the count... */
1121 outbound_relocas
= (Elf_External_Rela
*) rela_hdr
->contents
;
1123 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1125 Elf_Internal_Rela dst_rela
;
1126 Elf_External_Rela
*src_rela
;
1131 ptr
= sec
->orelocation
[idx
];
1132 src_rela
= outbound_relocas
+ idx
;
1133 if (!(abfd
->flags
& EXEC_P
))
1134 dst_rela
.r_offset
= ptr
->address
- sec
->vma
;
1136 dst_rela
.r_offset
= ptr
->address
;
1138 sym
= *ptr
->sym_ptr_ptr
;
1139 if (sym
== last_sym
)
1144 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1146 dst_rela
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1148 dst_rela
.r_addend
= ptr
->addend
;
1149 elf_swap_reloca_out (abfd
, &dst_rela
, src_rela
);
1153 /* REL relocations */
1155 outbound_relocs
= (Elf_External_Rel
*) rela_hdr
->contents
;
1157 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1159 Elf_Internal_Rel dst_rel
;
1160 Elf_External_Rel
*src_rel
;
1165 ptr
= sec
->orelocation
[idx
];
1166 sym
= *ptr
->sym_ptr_ptr
;
1167 src_rel
= outbound_relocs
+ idx
;
1168 if (!(abfd
->flags
& EXEC_P
))
1169 dst_rel
.r_offset
= ptr
->address
- sec
->vma
;
1171 dst_rel
.r_offset
= ptr
->address
;
1173 if (sym
== last_sym
)
1178 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1180 dst_rel
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1182 elf_swap_reloc_out (abfd
, &dst_rel
, src_rel
);
1188 fix_up_strtabs (abfd
, asect
, obj
)
1193 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (asect
)->this_hdr
;
1194 int this_idx
= elf_section_data(asect
)->this_idx
;
1196 /* @@ Check flags! */
1197 if (!strncmp (asect
->name
, ".stab", 5)
1198 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
1200 size_t len
= strlen (asect
->name
) + 1;
1201 char *s
= (char *) alloca (len
);
1202 strcpy (s
, asect
->name
);
1204 asect
= bfd_get_section_by_name (abfd
, s
);
1207 elf_section_data(asect
)->this_hdr
.sh_link
= this_idx
;
1208 /* @@ Assuming 32 bits! */
1209 elf_section_data(asect
)->this_hdr
.sh_entsize
= 0xc;
1211 this_hdr
->sh_type
= SHT_STRTAB
;
1216 DEFUN (elf_fake_sections
, (abfd
, asect
, obj
),
1218 asection
* asect AND
1221 /* most of what is in bfd_shdr_from_section goes in here... */
1222 /* and all of these sections generate at *least* one ELF section. */
1224 Elf_Internal_Shdr
*this_hdr
;
1225 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1227 bfd_add_to_strtab (abfd
, elf_shstrtab (abfd
), asect
->name
);
1228 /* We need to log the type *now* so that elf_section_from_bfd_section
1229 can find us... have to set rawdata too. */
1230 this_hdr
->rawdata
= (void *) asect
;
1231 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1232 if ((asect
->flags
& SEC_ALLOC
) && (asect
->flags
& SEC_LOAD
))
1233 this_hdr
->sh_type
= SHT_PROGBITS
;
1234 else if ((asect
->flags
& SEC_ALLOC
) && ((asect
->flags
& SEC_LOAD
) == 0))
1236 BFD_ASSERT (!strcmp (asect
->name
, ".bss"));
1237 this_hdr
->sh_type
= SHT_NOBITS
;
1239 /* FIXME I am not sure how to detect a .note section from the flags
1240 word of an `asection'. */
1241 else if (!strcmp (asect
->name
, ".note"))
1242 this_hdr
->sh_type
= SHT_NOTE
;
1244 this_hdr
->sh_type
= SHT_PROGBITS
;
1246 /* Now, check for processor-specific section types. */
1248 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1250 if (bed
->elf_backend_fake_sections
)
1251 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1254 this_hdr
->sh_flags
= 0;
1255 this_hdr
->sh_addr
= 0;
1256 this_hdr
->sh_size
= 0;
1257 this_hdr
->sh_entsize
= 0;
1258 this_hdr
->sh_info
= 0;
1259 this_hdr
->sh_link
= 0;
1260 this_hdr
->sh_offset
= 0;
1264 /* Emit a strtab and symtab, and possibly a reloc section. */
1265 Elf_Internal_Shdr
*rela_hdr
;
1266 Elf_Internal_Shdr
*symstrtab_hdr
;
1268 /* Note that only one symtab is used, so just remember it
1271 if (asect
->flags
& SEC_RELOC
)
1273 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1275 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1277 bfd_add_2_to_strtab (abfd
, elf_shstrtab (abfd
),
1278 use_rela_p
? ".rela" : ".rel",
1280 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1281 rela_hdr
->sh_entsize
= (use_rela_p
1282 ? sizeof (Elf_External_Rela
)
1283 : sizeof (Elf_External_Rel
));
1285 rela_hdr
->sh_flags
= 0;
1286 rela_hdr
->sh_addr
= 0;
1287 rela_hdr
->sh_size
= 0;
1288 rela_hdr
->sh_offset
= 0;
1289 rela_hdr
->sh_addralign
= 0;
1293 if (asect
->flags
& SEC_ALLOC
)
1295 this_hdr
->sh_flags
|= SHF_ALLOC
;
1296 if (asect
->flags
& SEC_LOAD
)
1298 /* @@ Do something with sh_type? */
1301 if (!(asect
->flags
& SEC_READONLY
))
1302 this_hdr
->sh_flags
|= SHF_WRITE
;
1303 if (asect
->flags
& SEC_CODE
)
1304 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1307 /* Map symbol from it's internal number to the external number, moving
1308 all local symbols to be at the head of the list. */
1314 if (sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
))
1316 if (sym
->flags
& BSF_LOCAL
)
1320 if (sym
->section
== 0)
1322 /* Is this valid? */
1327 if (sym
->section
== &bfd_und_section
)
1329 if (bfd_is_com_section (sym
->section
))
1331 if (sym
->flags
& (BSF_LOCAL
| BSF_SECTION_SYM
| BSF_FILE
))
1337 DEFUN (elf_map_symbols
, (abfd
), bfd
* abfd
)
1339 int symcount
= bfd_get_symcount (abfd
);
1340 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1341 asymbol
**sect_syms
;
1343 int num_globals
= 0;
1344 int num_locals2
= 0;
1345 int num_globals2
= 0;
1347 int num_sections
= 0;
1348 Elf_Sym_Extra
*sym_extra
;
1353 fprintf (stderr
, "elf_map_symbols\n");
1357 /* Add local symbols for each section for which there are relocs.
1358 FIXME: How can we tell which sections have relocs at this point?
1359 Will reloc_count always be accurate? Actually, I think most ELF
1360 targets create section symbols for all sections anyhow. */
1361 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1363 if (max_index
< asect
->index
)
1364 max_index
= asect
->index
;
1368 elf_num_section_syms (abfd
) = max_index
;
1369 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1370 elf_section_syms (abfd
) = sect_syms
;
1372 BFD_ASSERT (sect_syms
!= 0);
1374 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1376 asymbol
*sym
= bfd_make_empty_symbol (abfd
);
1377 sym
->the_bfd
= abfd
;
1378 sym
->name
= asect
->name
;
1379 sym
->value
= asect
->vma
;
1380 sym
->flags
= BSF_SECTION_SYM
;
1381 sym
->section
= asect
;
1382 sect_syms
[asect
->index
] = sym
;
1386 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1387 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1394 syms
= (asymbol
**) bfd_realloc (abfd
, syms
,
1395 ((symcount
+ num_sections
+ 1)
1396 * sizeof (asymbol
*)));
1398 syms
= (asymbol
**) bfd_alloc (abfd
,
1399 (num_sections
+ 1) * sizeof (asymbol
*));
1401 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1403 if (sect_syms
[asect
->index
])
1404 syms
[symcount
++] = sect_syms
[asect
->index
];
1407 syms
[symcount
] = (asymbol
*) 0;
1408 bfd_set_symtab (abfd
, syms
, symcount
);
1411 elf_sym_extra (abfd
) = sym_extra
1412 = (Elf_Sym_Extra
*) bfd_alloc (abfd
, symcount
* sizeof (Elf_Sym_Extra
));
1414 /* Identify and classify all of the symbols. */
1415 for (idx
= 0; idx
< symcount
; idx
++)
1417 if (!sym_is_global (syms
[idx
]))
1423 /* Now provide mapping information. Add +1 for skipping over the
1425 for (idx
= 0; idx
< symcount
; idx
++)
1427 syms
[idx
]->udata
= (PTR
) &sym_extra
[idx
];
1428 if (!sym_is_global (syms
[idx
]))
1429 sym_extra
[idx
].elf_sym_num
= 1 + num_locals2
++;
1431 sym_extra
[idx
].elf_sym_num
= 1 + num_locals
+ num_globals2
++;
1434 elf_num_locals (abfd
) = num_locals
;
1435 elf_num_globals (abfd
) = num_globals
;
1438 static void assign_section_numbers ();
1439 static void assign_file_positions_except_relocs ();
1442 DEFUN (elf_compute_section_file_positions
, (abfd
), bfd
* abfd
)
1444 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1445 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1446 struct strtab
*shstrtab
;
1447 int count
, maxsections
;
1449 bfd_map_over_sections (abfd
, elf_fake_sections
, 0);
1451 assign_section_numbers (abfd
);
1453 bfd_map_over_sections (abfd
, elf_make_sections
, 0);
1455 bfd_map_over_sections (abfd
, fix_up_strtabs
, 0); /* .stab/.stabstr &c */
1457 swap_out_syms (abfd
);
1459 assign_file_positions_except_relocs (abfd
);
1465 DEFUN (elf_write_phdrs
, (abfd
, i_ehdrp
, i_phdrp
, phdr_cnt
),
1467 Elf_Internal_Ehdr
* i_ehdrp AND
1468 Elf_Internal_Phdr
* i_phdrp AND
1469 Elf32_Half phdr_cnt
)
1471 /* first program header entry goes after the file header */
1472 int outbase
= i_ehdrp
->e_phoff
;
1474 Elf_External_Phdr x_phdr
;
1476 for (i
= 0; i
< phdr_cnt
; i
++)
1478 elf_swap_phdr_out (abfd
, i_phdrp
+ i
, &x_phdr
);
1479 bfd_seek (abfd
, outbase
, SEEK_SET
);
1480 bfd_write ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
);
1481 outbase
+= sizeof (x_phdr
);
1487 static const Elf_Internal_Shdr null_shdr
;
1489 /* Assign all ELF section numbers. The dummy first section is handled here
1490 too. The link/info pointers for the standard section types are filled
1491 in here too, while we're at it. (Link pointers for .stab sections are
1492 not filled in here.) */
1494 assign_section_numbers (abfd
)
1497 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1499 int section_number
= 1;
1501 Elf_Internal_Shdr
**i_shdrp
;
1503 t
->shstrtab_hdr
.sh_size
= elf_shstrtab(abfd
)->length
;
1504 t
->shstrtab_hdr
.contents
= (void *) elf_shstrtab(abfd
)->tab
;
1505 shstrtab_length_fixed
= 1;
1507 t
->shstrtab_section
= section_number
++;
1508 elf_elfheader(abfd
)->e_shstrndx
= t
->shstrtab_section
;
1511 t
->symtab_section
= section_number
++;
1512 t
->strtab_section
= section_number
++;
1513 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1515 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1517 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1518 d
->this_idx
= section_number
++;
1519 if (sec
->flags
& SEC_RELOC
)
1521 d
->rel_idx
= section_number
++;
1522 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1523 d
->rel_hdr
.sh_info
= d
->this_idx
;
1527 /* No handling for per-section string tables currently. */
1529 elf_elfheader(abfd
)->e_shnum
= section_number
;
1531 /* Set up the list of section header pointers, in agreement with the
1533 i_shdrp
= (Elf_Internal_Shdr
**)
1534 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*));
1535 elf_elfsections(abfd
) = i_shdrp
;
1536 for (i
= 0; i
< section_number
; i
++)
1539 i_shdrp
[0] = (Elf_Internal_Shdr
*) &null_shdr
;
1540 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1543 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1544 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1546 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1548 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1549 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1551 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1553 /* Make sure we got everything.... */
1554 for (i
= 0; i
< section_number
; i
++)
1555 if (i_shdrp
[i
] == 0)
1559 static INLINE file_ptr
1560 assign_file_position_for_section (i_shdrp
, offset
)
1561 Elf_Internal_Shdr
*i_shdrp
;
1564 i_shdrp
->sh_offset
= offset
;
1565 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1566 offset
+= i_shdrp
->sh_size
;
1570 static INLINE file_ptr
1571 align_file_position (off
)
1574 return (off
+ EALIGN
- 1) & ~(EALIGN
- 1);
1577 static INLINE file_ptr
1578 assign_file_positions_for_symtab_and_strtabs (abfd
, off
)
1582 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1584 off
= align_file_position (off
);
1585 off
= assign_file_position_for_section (&t
->symtab_hdr
, off
);
1586 off
= assign_file_position_for_section (&t
->shstrtab_hdr
, off
);
1587 off
= assign_file_position_for_section (&t
->strtab_hdr
, off
);
1592 bfd_vma low
, mem_size
;
1596 struct seg_info
*next
;
1600 map_program_segments (abfd
)
1603 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1604 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1605 Elf_Internal_Shdr
*i_shdrp
;
1606 Elf_Internal_Phdr
*phdr
;
1609 file_ptr lowest_offset
= 0;
1610 struct seg_info
*seg
= 0;
1612 done
= (char *) alloca (i_ehdrp
->e_shnum
);
1613 memset (done
, 0, i_ehdrp
->e_shnum
);
1614 for (i
= 0; i
< i_ehdrp
->e_shnum
; i
++)
1616 i_shdrp
= i_shdrpp
[i
];
1617 /* If it's going to be mapped in, it's been assigned a position. */
1618 if (i_shdrp
->sh_offset
+ 1 == 0)
1620 /* Well, not really, but we won't process it here. */
1624 if (i_shdrp
->sh_offset
< lowest_offset
1625 || lowest_offset
== 0)
1626 lowest_offset
= i_shdrp
->sh_offset
;
1627 /* Only interested in PROGBITS or NOBITS for generating segments. */
1628 switch (i_shdrp
->sh_type
)
1641 bfd_vma lowest_vma
= -1, high
;
1646 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1648 i_shdrp
= i_shdrpp
[i
];
1649 if (!done
[i
] && i_shdrp
->sh_addr
< lowest_vma
)
1651 lowest_vma
= i_shdrp
->sh_addr
;
1657 /* So now we know the lowest vma of any unassigned sections; start
1661 s
= (struct seg_info
*) bfd_alloc (abfd
, sizeof (struct seg_info
));
1665 seg
->low
= lowest_vma
;
1666 i_shdrp
= i_shdrpp
[low_sec
];
1667 seg
->start_pos
= i_shdrp
->sh_offset
;
1668 seg
->sh_flags
= i_shdrp
->sh_flags
;
1669 done
[low_sec
] = 1, n_left
--;
1670 mem_size
= i_shdrp
->sh_size
;
1671 high
= lowest_vma
+ i_shdrp
->sh_size
;
1673 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1674 file_size
= i_shdrp
->sh_size
;
1676 for (i
= 0; i
< i_ehdrp
->e_shnum
; i
++)
1680 if (file_size
!= mem_size
)
1684 i_shdrp
= i_shdrpp
[i
];
1685 /* position of next byte on disk */
1686 f1
= seg
->start_pos
+ file_size
;
1687 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1689 if (i_shdrp
->sh_offset
- f1
!= i_shdrp
->sh_addr
- high
)
1692 else /* sh_type == NOBITS */
1694 /* If the section in question has no contents in the disk
1695 file, we really don't care where it supposedly starts.
1696 But we don't want to bother merging it into this segment
1697 if it doesn't start on this memory page. */
1698 bfd_vma page1
, page2
;
1699 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1701 /* page number in address space of current end of seg */
1702 page1
= (high
- 1 + maxpagesize
- 1) / maxpagesize
;
1703 /* page number in address space of start of this section */
1704 page2
= (i_shdrp
->sh_addr
+ maxpagesize
- 1) / maxpagesize
;
1709 done
[i
] = 1, n_left
--;
1710 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1711 file_size
= i_shdrp
->sh_offset
+ i_shdrp
->sh_size
- seg
->start_pos
;
1712 mem_size
= i_shdrp
->sh_addr
+ i_shdrp
->sh_size
- seg
->low
;
1713 high
= i_shdrp
->sh_addr
+ i_shdrp
->sh_size
;
1716 seg
->file_size
= file_size
;
1717 seg
->mem_size
= mem_size
;
1719 /* Now do something with the list of segments we've built up. */
1721 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1726 for (s
= seg
; s
; s
= s
->next
)
1730 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
1731 sz
= sizeof (Elf_External_Phdr
) * n_segs
;
1732 if (align_file_position (i_ehdrp
->e_ehsize
) + sz
<= lowest_offset
)
1733 i_ehdrp
->e_phoff
= align_file_position (i_ehdrp
->e_ehsize
);
1736 i_ehdrp
->e_phoff
= align_file_position (elf_tdata (abfd
)->next_file_pos
);
1737 elf_tdata (abfd
)->next_file_pos
= i_ehdrp
->e_phoff
+ sz
;
1739 phdr
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
,
1740 n_segs
* sizeof (Elf_Internal_Phdr
));
1741 elf_tdata (abfd
)->phdr
= phdr
;
1744 phdr
->p_type
= PT_LOAD
; /* only type we really support so far */
1745 phdr
->p_offset
= seg
->start_pos
;
1746 phdr
->p_vaddr
= seg
->low
;
1748 phdr
->p_filesz
= seg
->file_size
;
1749 phdr
->p_memsz
= seg
->mem_size
;
1750 phdr
->p_flags
= PF_R
;
1751 phdr
->p_align
= maxpagesize
; /* ? */
1752 if (seg
->sh_flags
& SHF_WRITE
)
1753 /* SysVr4 ELF docs say "data segments normally have read, write,
1754 and execute permissions." */
1755 phdr
->p_flags
|= (PF_W
| PF_X
);
1756 if (seg
->sh_flags
& SHF_EXECINSTR
)
1757 phdr
->p_flags
|= PF_X
;
1761 i_ehdrp
->e_phnum
= n_segs
;
1763 elf_write_phdrs (abfd
, i_ehdrp
, elf_tdata (abfd
)->phdr
, i_ehdrp
->e_phnum
);
1767 assign_file_positions_except_relocs (abfd
)
1770 /* For now, we ignore the possibility of having program segments, which
1771 may require some alignment in the file. That'll require padding, and
1772 some interesting calculations to optimize file space usage.
1774 Also, since the application may change the list of relocations for
1775 a given section, we don't figure them in here. We'll put them at the
1776 end of the file, at positions computed during bfd_close.
1778 The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ...
1779 or: <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */
1783 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1784 Elf_Internal_Shdr
*i_shdrp
;
1785 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1786 int exec_p
= (abfd
->flags
& EXEC_P
) != 0;
1788 /* Everything starts after the ELF file header. */
1789 off
= i_ehdrp
->e_ehsize
;
1793 /* Section headers. */
1794 off
= align_file_position (off
);
1795 i_ehdrp
->e_shoff
= off
;
1796 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1798 off
= assign_file_positions_for_symtab_and_strtabs (abfd
, off
);
1800 for (i
= 0; i
< i_ehdrp
->e_shnum
; i
++)
1802 i_shdrp
= i_shdrpp
[i
];
1803 if (i_shdrp
->sh_type
== SHT_REL
|| i_shdrp
->sh_type
== SHT_RELA
)
1805 i_shdrp
->sh_offset
= -1;
1810 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1811 if (maxpagesize
== 0)
1812 maxpagesize
= 1; /* make the arithmetic work */
1813 /* This isn't necessarily going to give the best packing, if the
1814 segments require padding between them, but since that isn't
1815 usually the case, this'll do. */
1816 if ((i_shdrp
->sh_flags
& SHF_ALLOC
) == 0)
1818 i_shdrp
->sh_offset
= -1;
1821 /* Blindly assume that the segments are ordered optimally. With
1822 the default LD script, they will be. */
1824 /* need big unsigned type */
1826 addtl_off
= i_shdrp
->sh_addr
- off
;
1827 addtl_off
= addtl_off
% maxpagesize
;
1833 if (i_shdrp
->sh_type
== SHT_NOBITS
)
1836 i_shdrp
->sh_offset
= off
;
1837 if (off
% maxpagesize
!= 0)
1838 off2
= maxpagesize
- (off
% maxpagesize
);
1839 if (off2
> i_shdrp
->sh_size
)
1840 off2
= i_shdrp
->sh_size
;
1844 off
= assign_file_position_for_section (i_shdrp
, off
);
1847 && get_elf_backend_data(abfd
)->maxpagesize
> 1
1848 && i_shdrp
->sh_type
== SHT_PROGBITS
1849 && (i_shdrp
->sh_flags
& SHF_ALLOC
)
1850 && (i_shdrp
->sh_offset
- i_shdrp
->sh_addr
) % get_elf_backend_data (abfd
)->maxpagesize
!= 0)
1855 elf_tdata (abfd
)->next_file_pos
= off
;
1856 map_program_segments (abfd
);
1857 off
= elf_tdata (abfd
)->next_file_pos
;
1859 /* Section headers. */
1860 off
= align_file_position (off
);
1861 i_ehdrp
->e_shoff
= off
;
1862 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1864 off
= assign_file_positions_for_symtab_and_strtabs (abfd
, off
);
1866 for (i
= 0; i
< i_ehdrp
->e_shnum
; i
++)
1868 i_shdrp
= i_shdrpp
[i
];
1869 if (i_shdrp
->sh_offset
+ 1 == 0
1870 && i_shdrp
->sh_type
!= SHT_REL
1871 && i_shdrp
->sh_type
!= SHT_RELA
)
1872 off
= assign_file_position_for_section (i_shdrp
, off
);
1875 elf_tdata (abfd
)->next_file_pos
= off
;
1882 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1883 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1884 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
1885 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
1886 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
1890 struct strtab
*shstrtab
;
1892 i_ehdrp
= elf_elfheader (abfd
);
1893 i_shdrp
= elf_elfsections (abfd
);
1895 shstrtab
= bfd_new_strtab (abfd
);
1896 elf_shstrtab (abfd
) = shstrtab
;
1898 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
1899 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
1900 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
1901 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
1903 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS
;
1904 i_ehdrp
->e_ident
[EI_DATA
] =
1905 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
1906 i_ehdrp
->e_ident
[EI_VERSION
] = EV_CURRENT
;
1908 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
1909 i_ehdrp
->e_ident
[count
] = 0;
1911 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
) ? ET_EXEC
: ET_REL
;
1912 switch (bfd_get_arch (abfd
))
1914 case bfd_arch_unknown
:
1915 i_ehdrp
->e_machine
= EM_NONE
;
1917 case bfd_arch_sparc
:
1918 i_ehdrp
->e_machine
= EM_SPARC
;
1919 /* start-sanitize-v9 */
1921 i_ehdrp
->e_machine
= EM_SPARC64
;
1923 /* end-sanitize-v9 */
1926 i_ehdrp
->e_machine
= EM_386
;
1929 i_ehdrp
->e_machine
= EM_68K
;
1932 i_ehdrp
->e_machine
= EM_88K
;
1935 i_ehdrp
->e_machine
= EM_860
;
1937 case bfd_arch_mips
: /* MIPS Rxxxx */
1938 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
1941 i_ehdrp
->e_machine
= EM_HPPA
;
1943 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
1945 i_ehdrp
->e_machine
= EM_NONE
;
1947 i_ehdrp
->e_version
= EV_CURRENT
;
1948 i_ehdrp
->e_ehsize
= sizeof (Elf_External_Ehdr
);
1950 /* no program header, for now. */
1951 i_ehdrp
->e_phoff
= 0;
1952 i_ehdrp
->e_phentsize
= 0;
1953 i_ehdrp
->e_phnum
= 0;
1955 /* each bfd section is section header entry */
1956 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
1957 i_ehdrp
->e_shentsize
= sizeof (Elf_External_Shdr
);
1959 /* if we're building an executable, we'll need a program header table */
1960 if (abfd
->flags
& EXEC_P
)
1962 /* it all happens later */
1964 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
1966 /* elf_build_phdrs() returns a (NULL-terminated) array of
1967 Elf_Internal_Phdrs */
1968 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
1969 i_ehdrp
->e_phoff
= outbase
;
1970 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
1975 i_ehdrp
->e_phentsize
= 0;
1977 i_ehdrp
->e_phoff
= 0;
1980 elf_tdata (abfd
)->symtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
1982 elf_tdata (abfd
)->strtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
1984 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
1990 swap_out_syms (abfd
)
1993 struct strtab
*shstrtab
= elf_shstrtab (abfd
);
1995 elf_map_symbols (abfd
);
1997 /* Dump out the symtabs. */
1999 int symcount
= bfd_get_symcount (abfd
);
2000 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2001 struct strtab
*stt
= bfd_new_strtab (abfd
);
2002 Elf_Internal_Shdr
*symtab_hdr
;
2003 Elf_Internal_Shdr
*symstrtab_hdr
;
2004 Elf_External_Sym
*outbound_syms
;
2007 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2008 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2009 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
2010 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2011 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2013 /* see assert in elf_fake_sections that supports this: */
2014 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2015 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2017 outbound_syms
= (Elf_External_Sym
*)
2018 bfd_alloc (abfd
, (1 + symcount
) * sizeof (Elf_External_Sym
));
2019 /* now generate the data (for "contents") */
2021 /* Fill in zeroth symbol and swap it out. */
2022 Elf_Internal_Sym sym
;
2028 sym
.st_shndx
= SHN_UNDEF
;
2029 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
2031 for (idx
= 0; idx
< symcount
; idx
++)
2033 Elf_Internal_Sym sym
;
2034 bfd_vma value
= syms
[idx
]->value
;
2036 if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2037 /* Section symbols have no names. */
2040 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
2042 if (bfd_is_com_section (syms
[idx
]->section
))
2044 /* ELF common symbols put the alignment into the `value' field,
2045 and the size into the `size' field. This is backwards from
2046 how BFD handles it, so reverse it here. */
2047 sym
.st_size
= value
;
2048 /* Should retrieve this from somewhere... */
2050 sym
.st_shndx
= SHN_COMMON
;
2054 asection
*sec
= syms
[idx
]->section
;
2055 elf_symbol_type
*type_ptr
;
2058 if (sec
->output_section
)
2060 value
+= sec
->output_offset
;
2061 sec
= sec
->output_section
;
2064 sym
.st_value
= value
;
2065 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2066 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2067 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec
);
2071 /* Writing this would be a hell of a lot easier if we had
2072 some decent documentation on bfd, and knew what to expect
2073 of the library, and what to demand of applications. For
2074 example, it appears that `objcopy' might not set the
2075 section of a symbol to be a section that is actually in
2077 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2079 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec2
);
2080 assert (shndx
!= -1);
2084 if (bfd_is_com_section (syms
[idx
]->section
))
2085 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2086 else if (syms
[idx
]->section
== &bfd_und_section
)
2087 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2088 else if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2089 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2090 else if (syms
[idx
]->flags
& BSF_FILE
)
2091 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2094 int bind
= STB_LOCAL
;
2095 int type
= STT_OBJECT
;
2096 unsigned int flags
= syms
[idx
]->flags
;
2098 if (flags
& BSF_LOCAL
)
2100 else if (flags
& BSF_WEAK
)
2102 else if (flags
& BSF_GLOBAL
)
2105 if (flags
& BSF_FUNCTION
)
2108 sym
.st_info
= ELF_ST_INFO (bind
, type
);
2112 elf_swap_symbol_out (abfd
, &sym
,
2114 + elf_sym_extra (abfd
)[idx
].elf_sym_num
));
2117 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2118 symstrtab_hdr
->contents
= (PTR
) stt
->tab
;
2119 symstrtab_hdr
->sh_size
= stt
->length
;
2120 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2122 symstrtab_hdr
->sh_flags
= 0;
2123 symstrtab_hdr
->sh_addr
= 0;
2124 symstrtab_hdr
->sh_entsize
= 0;
2125 symstrtab_hdr
->sh_link
= 0;
2126 symstrtab_hdr
->sh_info
= 0;
2127 symstrtab_hdr
->sh_addralign
= 0;
2128 symstrtab_hdr
->size
= 0;
2131 /* put the strtab out too... */
2133 Elf_Internal_Shdr
*this_hdr
;
2135 this_hdr
= &elf_tdata(abfd
)->shstrtab_hdr
;
2136 this_hdr
->contents
= (PTR
) elf_shstrtab (abfd
)->tab
;
2137 this_hdr
->sh_size
= elf_shstrtab (abfd
)->length
;
2138 this_hdr
->sh_type
= SHT_STRTAB
;
2139 this_hdr
->sh_flags
= 0;
2140 this_hdr
->sh_addr
= 0;
2141 this_hdr
->sh_entsize
= 0;
2142 this_hdr
->sh_addralign
= 0;
2148 write_shdrs_and_ehdr (abfd
)
2151 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
2152 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2153 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2154 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
2155 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2159 struct strtab
*shstrtab
;
2161 i_ehdrp
= elf_elfheader (abfd
);
2162 i_shdrp
= elf_elfsections (abfd
);
2163 shstrtab
= elf_shstrtab (abfd
);
2165 /* swap the header before spitting it out... */
2168 elf_debug_file (i_ehdrp
);
2170 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
2171 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
2172 bfd_write ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
);
2174 /* at this point we've concocted all the ELF sections... */
2175 x_shdrp
= (Elf_External_Shdr
*)
2176 bfd_alloc (abfd
, sizeof (*x_shdrp
) * (i_ehdrp
->e_shnum
));
2179 bfd_error
= no_memory
;
2183 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2186 elf_debug_section (shstrtab
->tab
+ i_shdrp
[count
]->sh_name
, count
,
2189 elf_swap_shdr_out (abfd
, i_shdrp
[count
], x_shdrp
+ count
);
2191 bfd_seek (abfd
, (file_ptr
) i_ehdrp
->e_shoff
, SEEK_SET
);
2192 bfd_write ((PTR
) x_shdrp
, sizeof (*x_shdrp
), i_ehdrp
->e_shnum
, abfd
);
2193 /* need to dump the string table too... */
2199 assign_file_positions_for_relocs (abfd
)
2202 file_ptr off
= elf_tdata(abfd
)->next_file_pos
;
2204 Elf_Internal_Shdr
**shdrpp
= elf_elfsections (abfd
);
2205 Elf_Internal_Shdr
*shdrp
;
2206 for (i
= 0; i
< elf_elfheader(abfd
)->e_shnum
; i
++)
2209 if (shdrp
->sh_type
!= SHT_REL
&& shdrp
->sh_type
!= SHT_RELA
)
2211 off
= align_file_position (off
);
2212 off
= assign_file_position_for_section (shdrp
, off
);
2214 elf_tdata(abfd
)->next_file_pos
= off
;
2218 DEFUN (NAME(bfd_elf
,write_object_contents
), (abfd
), bfd
* abfd
)
2220 Elf_Internal_Ehdr
*i_ehdrp
;
2221 Elf_Internal_Shdr
**i_shdrp
;
2224 if (abfd
->output_has_begun
== false)
2226 prep_headers (abfd
);
2227 elf_compute_section_file_positions (abfd
);
2228 abfd
->output_has_begun
= true;
2231 i_shdrp
= elf_elfsections (abfd
);
2232 i_ehdrp
= elf_elfheader (abfd
);
2234 bfd_map_over_sections (abfd
, write_relocs
, (PTR
) 0);
2235 assign_file_positions_for_relocs (abfd
);
2237 /* After writing the headers, we need to write the sections too... */
2238 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2240 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2242 if (bed
->elf_backend_section_processing
)
2243 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2244 if (i_shdrp
[count
]->contents
)
2246 bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
);
2247 bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
, 1,
2251 return write_shdrs_and_ehdr (abfd
);
2254 /* Given an index of a section, retrieve a pointer to it. Note
2255 that for our purposes, sections are indexed by {1, 2, ...} with
2256 0 being an illegal index. */
2258 /* In the original, each ELF section went into exactly one BFD
2259 section. This doesn't really make sense, so we need a real mapping.
2260 The mapping has to hide in the Elf_Internal_Shdr since asection
2261 doesn't have anything like a tdata field... */
2264 DEFUN (section_from_elf_index
, (abfd
, index
),
2268 /* @@ Is bfd_com_section really correct in all the places it could
2269 be returned from this routine? */
2271 if (index
== SHN_ABS
)
2272 return &bfd_com_section
; /* not abs? */
2273 if (index
== SHN_COMMON
)
2274 return &bfd_com_section
;
2276 if (index
> elf_elfheader (abfd
)->e_shnum
)
2280 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[index
];
2282 switch (hdr
->sh_type
)
2284 /* ELF sections that map to BFD sections */
2288 bfd_section_from_shdr (abfd
, index
);
2289 return (struct sec
*) hdr
->rawdata
;
2292 return (struct sec
*) &bfd_abs_section
;
2297 /* given a section, search the header to find them... */
2299 DEFUN (elf_section_from_bfd_section
, (abfd
, asect
),
2303 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2305 Elf_Internal_Shdr
*hdr
;
2306 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2308 if (asect
== &bfd_abs_section
)
2310 if (asect
== &bfd_com_section
)
2312 if (asect
== &bfd_und_section
)
2315 for (index
= 0; index
< maxindex
; index
++)
2317 hdr
= i_shdrp
[index
];
2318 switch (hdr
->sh_type
)
2320 /* ELF sections that map to BFD sections */
2326 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2332 /* fix_up_strtabs will generate STRTAB sections with names
2334 if (!strncmp (asect
->name
, ".stab", 5)
2335 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
2339 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2347 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2349 if (bed
->elf_backend_section_from_bfd_section
)
2350 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, hdr
, asect
))
2359 /* given a symbol, return the bfd index for that symbol. */
2361 DEFUN (elf_symbol_from_bfd_symbol
, (abfd
, asym_ptr_ptr
),
2363 struct symbol_cache_entry
**asym_ptr_ptr
)
2365 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2366 CONST
char *name
= asym_ptr
->name
;
2368 int symcount
= bfd_get_symcount (abfd
);
2369 flagword flags
= asym_ptr
->flags
;
2370 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2372 /* When gas creates relocations against local labels, it creates its
2373 own symbol for the section, but does put the symbol into the
2374 symbol chain, so udata is 0. When the linker is generating
2375 relocatable output, this section symbol may be for one of the
2376 input sections rather than the output section. */
2377 if (asym_ptr
->udata
== (PTR
) 0
2378 && (flags
& BSF_SECTION_SYM
)
2379 && asym_ptr
->section
)
2383 if (asym_ptr
->section
->output_section
!= NULL
)
2384 indx
= asym_ptr
->section
->output_section
->index
;
2386 indx
= asym_ptr
->section
->index
;
2387 if (elf_section_syms (abfd
)[indx
])
2388 asym_ptr
->udata
= elf_section_syms (abfd
)[indx
]->udata
;
2391 if (asym_ptr
->udata
)
2392 idx
= ((Elf_Sym_Extra
*)asym_ptr
->udata
)->elf_sym_num
;
2402 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
2403 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
, elf_symbol_flags (flags
));
2412 DEFUN (elf_slurp_symbol_table
, (abfd
, symptrs
),
2414 asymbol
** symptrs
) /* Buffer for generated bfd symbols */
2416 Elf_Internal_Shdr
*hdr
= &elf_tdata(abfd
)->symtab_hdr
;
2417 int symcount
; /* Number of external ELF symbols */
2419 elf_symbol_type
*sym
; /* Pointer to current bfd symbol */
2420 elf_symbol_type
*symbase
; /* Buffer for generated bfd symbols */
2421 Elf_Internal_Sym i_sym
;
2422 Elf_External_Sym
*x_symp
;
2424 /* this is only valid because there is only one symtab... */
2425 /* FIXME: This is incorrect, there may also be a dynamic symbol
2426 table which is a subset of the full symbol table. We either need
2427 to be prepared to read both (and merge them) or ensure that we
2428 only read the full symbol table. Currently we only get called to
2429 read the full symbol table. -fnf */
2430 if (bfd_get_outsymbols (abfd
) != NULL
)
2435 /* Read each raw ELF symbol, converting from external ELF form to
2436 internal ELF form, and then using the information to create a
2437 canonical bfd symbol table entry.
2439 Note that we allocate the initial bfd canonical symbol buffer
2440 based on a one-to-one mapping of the ELF symbols to canonical
2441 symbols. We actually use all the ELF symbols, so there will be no
2442 space left over at the end. When we have all the symbols, we
2443 build the caller's pointer vector. */
2445 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2447 bfd_error
= system_call_error
;
2451 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2452 symbase
= (elf_symbol_type
*) bfd_zalloc (abfd
, symcount
* sizeof (elf_symbol_type
));
2455 /* Temporarily allocate room for the raw ELF symbols. */
2456 x_symp
= (Elf_External_Sym
*) bfd_xmalloc (symcount
* sizeof (Elf_External_Sym
));
2458 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
2459 != symcount
* sizeof (Elf_External_Sym
))
2461 free ((PTR
) x_symp
);
2462 bfd_error
= system_call_error
;
2465 /* Skip first symbol, which is a null dummy. */
2466 for (i
= 1; i
< symcount
; i
++)
2468 elf_swap_symbol_in (abfd
, x_symp
+ i
, &i_sym
);
2469 memcpy (&sym
->internal_elf_sym
, &i_sym
, sizeof (Elf_Internal_Sym
));
2470 #ifdef ELF_KEEP_EXTSYM
2471 memcpy (&sym
->native_elf_sym
, x_symp
+ i
, sizeof (Elf_External_Sym
));
2473 sym
->symbol
.the_bfd
= abfd
;
2475 sym
->symbol
.name
= elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2478 sym
->symbol
.value
= i_sym
.st_value
;
2480 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
2482 sym
->symbol
.section
= section_from_elf_index (abfd
, i_sym
.st_shndx
);
2484 else if (i_sym
.st_shndx
== SHN_ABS
)
2486 sym
->symbol
.section
= &bfd_abs_section
;
2488 else if (i_sym
.st_shndx
== SHN_COMMON
)
2490 sym
->symbol
.section
= &bfd_com_section
;
2491 /* Elf puts the alignment into the `value' field, and the size
2492 into the `size' field. BFD wants to see the size in the
2493 value field, and doesn't care (at the moment) about the
2495 sym
->symbol
.value
= i_sym
.st_size
;
2497 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2499 sym
->symbol
.section
= &bfd_und_section
;
2502 sym
->symbol
.section
= &bfd_abs_section
;
2504 sym
->symbol
.value
-= sym
->symbol
.section
->vma
;
2506 switch (ELF_ST_BIND (i_sym
.st_info
))
2509 sym
->symbol
.flags
|= BSF_LOCAL
;
2512 sym
->symbol
.flags
|= BSF_GLOBAL
;
2515 sym
->symbol
.flags
|= BSF_WEAK
;
2519 switch (ELF_ST_TYPE (i_sym
.st_info
))
2522 sym
->symbol
.flags
|= BSF_SECTION_SYM
| BSF_DEBUGGING
;
2525 sym
->symbol
.flags
|= BSF_FILE
| BSF_DEBUGGING
;
2528 sym
->symbol
.flags
|= BSF_FUNCTION
;
2532 /* Do some backend-specific processing on this symbol. */
2534 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2535 if (ebd
->elf_backend_symbol_processing
)
2536 (*ebd
->elf_backend_symbol_processing
) (abfd
, &sym
->symbol
);
2542 /* Do some backend-specific processing on this symbol table. */
2544 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2545 if (ebd
->elf_backend_symbol_table_processing
)
2546 (*ebd
->elf_backend_symbol_table_processing
) (abfd
, symbase
, symcount
);
2549 /* We rely on the zalloc to clear out the final symbol entry. */
2551 bfd_get_symcount (abfd
) = symcount
= sym
- symbase
;
2553 /* Fill in the user's symbol pointer vector if needed. */
2557 while (symcount
-- > 0)
2559 *symptrs
++ = &sym
->symbol
;
2562 *symptrs
= 0; /* Final null pointer */
2568 /* Return the number of bytes required to hold the symtab vector.
2570 Note that we base it on the count plus 1, since we will null terminate
2571 the vector allocated based on this size. However, the ELF symbol table
2572 always has a dummy entry as symbol #0, so it ends up even. */
2575 DEFUN (elf_get_symtab_upper_bound
, (abfd
), bfd
* abfd
)
2577 unsigned int symcount
;
2578 unsigned int symtab_size
= 0;
2580 Elf_Internal_Shdr
*hdr
= &elf_tdata(abfd
)->symtab_hdr
;
2581 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2582 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
));
2588 This function return the number of bytes required to store the
2589 relocation information associated with section <<sect>>
2590 attached to bfd <<abfd>>
2594 elf_get_reloc_upper_bound (abfd
, asect
)
2598 if (asect
->flags
& SEC_RELOC
)
2600 /* either rel or rela */
2601 return elf_section_data(asect
)->rel_hdr
.sh_size
;
2608 DEFUN (elf_slurp_reloca_table
, (abfd
, asect
, symbols
),
2613 Elf_External_Rela
*native_relocs
;
2614 arelent
*reloc_cache
;
2619 if (asect
->relocation
)
2621 if (asect
->reloc_count
== 0)
2623 if (asect
->flags
& SEC_CONSTRUCTOR
)
2626 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2627 native_relocs
= (Elf_External_Rela
*)
2628 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rela
));
2629 bfd_read ((PTR
) native_relocs
,
2630 sizeof (Elf_External_Rela
), asect
->reloc_count
, abfd
);
2632 reloc_cache
= (arelent
*)
2633 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2637 bfd_error
= no_memory
;
2641 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2643 Elf_Internal_Rela dst
;
2644 Elf_External_Rela
*src
;
2646 cache_ptr
= reloc_cache
+ idx
;
2647 src
= native_relocs
+ idx
;
2648 elf_swap_reloca_in (abfd
, src
, &dst
);
2650 #ifdef RELOC_PROCESSING
2651 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
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
)
2672 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
2673 s
= *cache_ptr
->sym_ptr_ptr
;
2674 if (s
->name
== 0 || s
->name
[0] == 0)
2678 cache_ptr
->addend
= dst
.r_addend
;
2680 /* Fill in the cache_ptr->howto field from dst.r_type */
2682 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2683 (*ebd
->elf_info_to_howto
) (abfd
, cache_ptr
, &dst
);
2688 asect
->relocation
= reloc_cache
;
2694 elf_debug_section (str
, num
, hdr
)
2697 Elf_Internal_Shdr
*hdr
;
2699 fprintf (stderr
, "\nSection#%d '%s' 0x%.8lx\n", num
, str
, (long) hdr
);
2701 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
2702 (long) hdr
->sh_name
,
2703 (long) hdr
->sh_type
,
2704 (long) hdr
->sh_flags
);
2706 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
2707 (long) hdr
->sh_addr
,
2708 (long) hdr
->sh_offset
,
2709 (long) hdr
->sh_size
);
2711 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
2712 (long) hdr
->sh_link
,
2713 (long) hdr
->sh_info
,
2714 (long) hdr
->sh_addralign
);
2715 fprintf (stderr
, "sh_entsize = %ld\n",
2716 (long) hdr
->sh_entsize
);
2717 fprintf (stderr
, "rawdata = 0x%.8lx\n", (long) hdr
->rawdata
);
2718 fprintf (stderr
, "contents = 0x%.8lx\n", (long) hdr
->contents
);
2719 fprintf (stderr
, "size = %ld\n", (long) hdr
->size
);
2724 elf_debug_file (ehdrp
)
2725 Elf_Internal_Ehdr
*ehdrp
;
2727 fprintf (stderr
, "e_entry = 0x%.8lx\n", (long) ehdrp
->e_entry
);
2728 fprintf (stderr
, "e_phoff = %ld\n", (long) ehdrp
->e_phoff
);
2729 fprintf (stderr
, "e_phnum = %ld\n", (long) ehdrp
->e_phnum
);
2730 fprintf (stderr
, "e_phentsize = %ld\n", (long) ehdrp
->e_phentsize
);
2731 fprintf (stderr
, "e_shoff = %ld\n", (long) ehdrp
->e_shoff
);
2732 fprintf (stderr
, "e_shnum = %ld\n", (long) ehdrp
->e_shnum
);
2733 fprintf (stderr
, "e_shentsize = %ld\n", (long) ehdrp
->e_shentsize
);
2738 DEFUN (elf_slurp_reloc_table
, (abfd
, asect
, symbols
),
2743 Elf_External_Rel
*native_relocs
;
2744 arelent
*reloc_cache
;
2746 Elf_Internal_Shdr
*data_hdr
;
2747 ElfNAME (Off
) data_off
;
2748 ElfNAME (Word
) data_max
;
2749 char buf
[4]; /* FIXME -- might be elf64 */
2753 if (asect
->relocation
)
2755 if (asect
->reloc_count
== 0)
2757 if (asect
->flags
& SEC_CONSTRUCTOR
)
2760 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2761 native_relocs
= (Elf_External_Rel
*)
2762 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rel
));
2763 bfd_read ((PTR
) native_relocs
,
2764 sizeof (Elf_External_Rel
), asect
->reloc_count
, abfd
);
2766 reloc_cache
= (arelent
*)
2767 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2771 bfd_error
= no_memory
;
2775 /* Get the offset of the start of the segment we are relocating to read in
2776 the implicit addend. */
2777 data_hdr
= &elf_section_data(asect
)->this_hdr
;
2778 data_off
= data_hdr
->sh_offset
;
2779 data_max
= data_hdr
->sh_size
- sizeof (buf
) + 1;
2782 elf_debug_section ("data section", -1, data_hdr
);
2785 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2787 #ifdef RELOC_PROCESSING
2788 Elf_Internal_Rel dst
;
2789 Elf_External_Rel
*src
;
2791 cache_ptr
= reloc_cache
+ idx
;
2792 src
= native_relocs
+ idx
;
2793 elf_swap_reloc_in (abfd
, src
, &dst
);
2795 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2797 Elf_Internal_Rel dst
;
2798 Elf_External_Rel
*src
;
2800 cache_ptr
= reloc_cache
+ idx
;
2801 src
= native_relocs
+ idx
;
2803 elf_swap_reloc_in (abfd
, src
, &dst
);
2805 if (asect
->flags
& SEC_RELOC
)
2807 /* relocatable, so the offset is off of the section */
2808 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2812 /* non-relocatable, so the offset a virtual address */
2813 cache_ptr
->address
= dst
.r_offset
;
2815 /* ELF_R_SYM(dst.r_info) is the symbol table offset...
2816 -1 is to skip the dummy symbol table entry */
2817 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
2819 /* Is it an ELF section symbol? If so, translate it into a
2820 BFD section symbol. */
2821 asymbol
*s
= *(cache_ptr
->sym_ptr_ptr
);
2822 if (s
->flags
& BSF_SECTION_SYM
)
2824 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
2825 s
= *cache_ptr
->sym_ptr_ptr
;
2826 if (s
->name
== 0 || s
->name
[0] == 0)
2830 BFD_ASSERT (dst
.r_offset
<= data_max
);
2831 cache_ptr
->addend
= 0;
2833 /* Fill in the cache_ptr->howto field from dst.r_type */
2835 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2836 (*ebd
->elf_info_to_howto_rel
) (abfd
, cache_ptr
, &dst
);
2841 asect
->relocation
= reloc_cache
;
2846 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
2852 arelent
*tblptr
= section
->relocation
;
2853 unsigned int count
= 0;
2854 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
2856 /* snarfed from coffcode.h */
2858 elf_slurp_reloca_table (abfd
, section
, symbols
);
2860 elf_slurp_reloc_table (abfd
, section
, symbols
);
2862 tblptr
= section
->relocation
;
2866 for (; count
++ < section
->reloc_count
;)
2867 *relptr
++ = tblptr
++;
2870 return section
->reloc_count
;
2874 DEFUN (elf_get_symtab
, (abfd
, alocation
),
2876 asymbol
** alocation
)
2879 if (!elf_slurp_symbol_table (abfd
, alocation
))
2882 return bfd_get_symcount (abfd
);
2886 DEFUN (elf_make_empty_symbol
, (abfd
),
2889 elf_symbol_type
*newsym
;
2891 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
2894 bfd_error
= no_memory
;
2899 newsym
->symbol
.the_bfd
= abfd
;
2900 return &newsym
->symbol
;
2905 DEFUN (elf_get_symbol_info
, (ignore_abfd
, symbol
, ret
),
2906 bfd
* ignore_abfd AND
2907 asymbol
* symbol AND
2910 bfd_symbol_info (symbol
, ret
);
2914 DEFUN (elf_print_symbol
, (ignore_abfd
, filep
, symbol
, how
),
2915 bfd
* ignore_abfd AND
2917 asymbol
* symbol AND
2918 bfd_print_symbol_type how
)
2920 FILE *file
= (FILE *) filep
;
2923 case bfd_print_symbol_name
:
2924 fprintf (file
, "%s", symbol
->name
);
2926 case bfd_print_symbol_more
:
2927 fprintf (file
, "elf ");
2928 fprintf_vma (file
, symbol
->value
);
2929 fprintf (file
, " %lx", (long) symbol
->flags
);
2931 case bfd_print_symbol_all
:
2933 CONST
char *section_name
;
2934 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2935 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
2936 fprintf (file
, " %s\t%s",
2946 DEFUN (elf_get_lineno
, (ignore_abfd
, symbol
),
2947 bfd
* ignore_abfd AND
2950 fprintf (stderr
, "elf_get_lineno unimplemented\n");
2957 DEFUN (elf_set_arch_mach
, (abfd
, arch
, machine
),
2959 enum bfd_architecture arch AND
2960 unsigned long machine
)
2962 /* Allow any architecture to be supported by the elf backend */
2965 case bfd_arch_unknown
: /* EM_NONE */
2966 case bfd_arch_sparc
: /* EM_SPARC */
2967 case bfd_arch_i386
: /* EM_386 */
2968 case bfd_arch_m68k
: /* EM_68K */
2969 case bfd_arch_m88k
: /* EM_88K */
2970 case bfd_arch_i860
: /* EM_860 */
2971 case bfd_arch_mips
: /* EM_MIPS (MIPS R3000) */
2972 case bfd_arch_hppa
: /* EM_HPPA (HP PA_RISC) */
2973 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
2980 DEFUN (elf_find_nearest_line
, (abfd
,
2988 asection
* section AND
2989 asymbol
** symbols AND
2991 CONST
char **filename_ptr AND
2992 CONST
char **functionname_ptr AND
2993 unsigned int *line_ptr
)
2999 DEFUN (elf_sizeof_headers
, (abfd
, reloc
),
3003 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
3010 DEFUN (elf_set_section_contents
, (abfd
, section
, location
, offset
, count
),
3015 bfd_size_type count
)
3017 Elf_Internal_Shdr
*hdr
;
3019 if (abfd
->output_has_begun
== false) /* set by bfd.c handler? */
3021 /* do setup calculations (FIXME) */
3022 prep_headers (abfd
);
3023 elf_compute_section_file_positions (abfd
);
3024 abfd
->output_has_begun
= true;
3027 hdr
= &elf_section_data(section
)->this_hdr
;
3029 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
3031 if (bfd_write (location
, 1, count
, abfd
) != count
)
3038 DEFUN (elf_no_info_to_howto
, (abfd
, cache_ptr
, dst
),
3040 arelent
* cache_ptr AND
3041 Elf_Internal_Rela
* dst
)
3043 fprintf (stderr
, "elf RELA relocation support for target machine unimplemented\n");
3049 DEFUN (elf_no_info_to_howto_rel
, (abfd
, cache_ptr
, dst
),
3051 arelent
* cache_ptr AND
3052 Elf_Internal_Rel
* dst
)
3054 fprintf (stderr
, "elf REL relocation support for target machine unimplemented\n");
3060 /* Core file support */
3062 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3063 #include <sys/procfs.h>
3065 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
3066 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
3067 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
3073 DEFUN (bfd_prstatus
, (abfd
, descdata
, descsz
, filepos
),
3080 prstatus_t
*status
= (prstatus_t
*) 0;
3082 if (descsz
== sizeof (prstatus_t
))
3084 newsect
= bfd_make_section (abfd
, ".reg");
3085 newsect
->_raw_size
= sizeof (status
->pr_reg
);
3086 newsect
->filepos
= filepos
+ (long) &status
->pr_reg
;
3087 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3088 newsect
->alignment_power
= 2;
3089 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3091 memcpy (core_prstatus (abfd
), descdata
, descsz
);
3096 /* Stash a copy of the prpsinfo structure away for future use. */
3099 DEFUN (bfd_prpsinfo
, (abfd
, descdata
, descsz
, filepos
),
3107 if (descsz
== sizeof (prpsinfo_t
))
3109 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3111 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
3117 DEFUN (bfd_fpregset
, (abfd
, descdata
, descsz
, filepos
),
3125 newsect
= bfd_make_section (abfd
, ".reg2");
3126 newsect
->_raw_size
= descsz
;
3127 newsect
->filepos
= filepos
;
3128 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3129 newsect
->alignment_power
= 2;
3132 #endif /* HAVE_PROCFS */
3134 /* Return a pointer to the args (including the command name) that were
3135 seen by the program that generated the core dump. Note that for
3136 some reason, a spurious space is tacked onto the end of the args
3137 in some (at least one anyway) implementations, so strip it off if
3141 DEFUN (elf_core_file_failing_command
, (abfd
),
3145 if (core_prpsinfo (abfd
))
3147 prpsinfo_t
*p
= core_prpsinfo (abfd
);
3148 char *scan
= p
->pr_psargs
;
3153 if ((scan
> p
->pr_psargs
) && (*scan
== ' '))
3157 return p
->pr_psargs
;
3163 /* Return the number of the signal that caused the core dump. Presumably,
3164 since we have a core file, we got a signal of some kind, so don't bother
3165 checking the other process status fields, just return the signal number.
3169 DEFUN (elf_core_file_failing_signal
, (abfd
),
3173 if (core_prstatus (abfd
))
3175 return ((prstatus_t
*) (core_prstatus (abfd
)))->pr_cursig
;
3181 /* Check to see if the core file could reasonably be expected to have
3182 come for the current executable file. Note that by default we return
3183 true unless we find something that indicates that there might be a
3188 DEFUN (elf_core_file_matches_executable_p
, (core_bfd
, exec_bfd
),
3197 /* First, xvecs must match since both are ELF files for the same target. */
3199 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
3201 bfd_error
= system_call_error
;
3207 /* If no prpsinfo, just return true. Otherwise, grab the last component
3208 of the exec'd pathname from the prpsinfo. */
3210 if (core_prpsinfo (core_bfd
))
3212 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
))->pr_fname
);
3219 /* Find the last component of the executable pathname. */
3221 if ((execname
= strrchr (exec_bfd
->filename
, '/')) != NULL
)
3227 execname
= (char *) exec_bfd
->filename
;
3230 /* See if they match */
3232 return strcmp (execname
, corename
) ? false : true;
3238 #endif /* HAVE_PROCFS */
3241 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3242 the information that would normally be available from the /proc interface
3243 for the process, at the time the process dumped core. Currently this
3244 includes copies of the prstatus, prpsinfo, and fpregset structures.
3246 Since these structures are potentially machine dependent in size and
3247 ordering, bfd provides two levels of support for them. The first level,
3248 available on all machines since it does not require that the host
3249 have /proc support or the relevant include files, is to create a bfd
3250 section for each of the prstatus, prpsinfo, and fpregset structures,
3251 without any interpretation of their contents. With just this support,
3252 the bfd client will have to interpret the structures itself. Even with
3253 /proc support, it might want these full structures for it's own reasons.
3255 In the second level of support, where HAVE_PROCFS is defined, bfd will
3256 pick apart the structures to gather some additional information that
3257 clients may want, such as the general register set, the name of the
3258 exec'ed file and its arguments, the signal (if any) that caused the
3264 DEFUN (elf_corefile_note
, (abfd
, hdr
),
3266 Elf_Internal_Phdr
* hdr
)
3268 Elf_External_Note
*x_note_p
; /* Elf note, external form */
3269 Elf_Internal_Note i_note
; /* Elf note, internal form */
3270 char *buf
= NULL
; /* Entire note segment contents */
3271 char *namedata
; /* Name portion of the note */
3272 char *descdata
; /* Descriptor portion of the note */
3273 char *sectname
; /* Name to use for new section */
3274 long filepos
; /* File offset to descriptor data */
3277 if (hdr
->p_filesz
> 0
3278 && (buf
= (char *) bfd_xmalloc (hdr
->p_filesz
)) != NULL
3279 && bfd_seek (abfd
, hdr
->p_offset
, SEEK_SET
) != -1
3280 && bfd_read ((PTR
) buf
, hdr
->p_filesz
, 1, abfd
) == hdr
->p_filesz
)
3282 x_note_p
= (Elf_External_Note
*) buf
;
3283 while ((char *) x_note_p
< (buf
+ hdr
->p_filesz
))
3285 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->namesz
);
3286 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->descsz
);
3287 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->type
);
3288 namedata
= x_note_p
->name
;
3289 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
3290 filepos
= hdr
->p_offset
+ (descdata
- buf
);
3291 switch (i_note
.type
)
3294 /* process descdata as prstatus info */
3295 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
3296 sectname
= ".prstatus";
3299 /* process descdata as fpregset info */
3300 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
3301 sectname
= ".fpregset";
3304 /* process descdata as prpsinfo */
3305 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
3306 sectname
= ".prpsinfo";
3309 /* Unknown descriptor, just ignore it. */
3313 if (sectname
!= NULL
)
3315 newsect
= bfd_make_section (abfd
, sectname
);
3316 newsect
->_raw_size
= i_note
.descsz
;
3317 newsect
->filepos
= filepos
;
3318 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3319 newsect
->alignment_power
= 2;
3321 x_note_p
= (Elf_External_Note
*)
3322 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
3333 /* Core files are simply standard ELF formatted files that partition
3334 the file using the execution view of the file (program header table)
3335 rather than the linking view. In fact, there is no section header
3336 table in a core file.
3338 The process status information (including the contents of the general
3339 register set) and the floating point register set are stored in a
3340 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3341 that allow standard bfd access to the general registers (.reg) and the
3342 floating point registers (.reg2).
3347 DEFUN (elf_core_file_p
, (abfd
), bfd
* abfd
)
3349 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
3350 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3351 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
3352 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
3353 unsigned int phindex
;
3355 /* Read in the ELF header in external format. */
3357 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
3359 bfd_error
= system_call_error
;
3363 /* Now check to see if we have a valid ELF file, and one that BFD can
3364 make use of. The magic number must match, the address size ('class')
3365 and byte-swapping must match our XVEC entry, and it must have a
3366 program header table (FIXME: See comments re segments at top of this
3369 if (elf_file_p (&x_ehdr
) == false)
3372 bfd_error
= wrong_format
;
3376 /* FIXME, Check EI_VERSION here ! */
3380 int desired_address_size
= ELFCLASS32
;
3383 int desired_address_size
= ELFCLASS64
;
3386 if (x_ehdr
.e_ident
[EI_CLASS
] != desired_address_size
)
3390 /* Switch xvec to match the specified byte order. */
3391 switch (x_ehdr
.e_ident
[EI_DATA
])
3393 case ELFDATA2MSB
: /* Big-endian */
3394 if (abfd
->xvec
->byteorder_big_p
== false)
3397 case ELFDATA2LSB
: /* Little-endian */
3398 if (abfd
->xvec
->byteorder_big_p
== true)
3401 case ELFDATANONE
: /* No data encoding specified */
3402 default: /* Unknown data encoding specified */
3406 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3407 the tdata pointer in the bfd. */
3410 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
3411 if (elf_tdata (abfd
) == NULL
)
3413 bfd_error
= no_memory
;
3417 /* FIXME, `wrong' returns from this point onward, leak memory. */
3419 /* Now that we know the byte order, swap in the rest of the header */
3420 i_ehdrp
= elf_elfheader (abfd
);
3421 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
3423 elf_debug_file (i_ehdrp
);
3426 /* If there is no program header, or the type is not a core file, then
3428 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
3431 /* Allocate space for a copy of the program header table in
3432 internal form, seek to the program header table in the file,
3433 read it in, and convert it to internal form. As a simple sanity
3434 check, verify that the what BFD thinks is the size of each program
3435 header table entry actually matches the size recorded in the file. */
3437 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
3439 i_phdrp
= (Elf_Internal_Phdr
*)
3440 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
3443 bfd_error
= no_memory
;
3446 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
3448 bfd_error
= system_call_error
;
3451 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3453 if (bfd_read ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
3456 bfd_error
= system_call_error
;
3459 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
3462 /* Once all of the program headers have been read and converted, we
3463 can start processing them. */
3465 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3467 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
3468 if ((i_phdrp
+ phindex
)->p_type
== PT_NOTE
)
3470 elf_corefile_note (abfd
, i_phdrp
+ phindex
);
3474 /* Remember the entry point specified in the ELF file header. */
3476 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;