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
109 #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
110 #define ELF_R_SYM(X) ELF32_R_SYM(X)
111 #define ELFCLASS ELFCLASS32
114 static int shstrtab_length_fixed
;
116 struct elf_sect_data
{
121 /* Forward declarations of static functions */
123 static struct sec
* section_from_elf_index
PARAMS ((bfd
*, int));
125 static int elf_section_from_bfd_section
PARAMS ((bfd
*, struct sec
*));
127 static boolean elf_slurp_symbol_table
PARAMS ((bfd
*, asymbol
**));
129 static int elf_symbol_from_bfd_symbol
PARAMS ((bfd
*,
130 struct symbol_cache_entry
**));
132 static void elf_map_symbols
PARAMS ((bfd
*));
133 static void swap_out_syms
PARAMS ((bfd
*));
136 static void elf_debug_section
PARAMS ((char *, int, Elf_Internal_Shdr
*));
137 static void elf_debug_file
PARAMS ((Elf_Internal_Ehdr
*));
140 #define elf_string_from_elf_strtab(abfd,strindex) \
141 elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
144 /* Structure swapping routines */
146 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
147 can be handled by explicitly specifying 32 bits or "the long type". */
149 #define put_word bfd_h_put_64
150 #define get_word bfd_h_get_64
153 #define put_word bfd_h_put_32
154 #define get_word bfd_h_get_32
157 /* Translate an ELF symbol in external format into an ELF symbol in internal
161 DEFUN (elf_swap_symbol_in
, (abfd
, src
, dst
),
163 Elf_External_Sym
* src AND
164 Elf_Internal_Sym
* dst
)
166 dst
->st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->st_name
);
167 dst
->st_value
= get_word (abfd
, (bfd_byte
*) src
->st_value
);
168 dst
->st_size
= get_word (abfd
, (bfd_byte
*) src
->st_size
);
169 dst
->st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_info
);
170 dst
->st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_other
);
171 dst
->st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->st_shndx
);
174 /* Translate an ELF symbol in internal format into an ELF symbol in external
178 DEFUN (elf_swap_symbol_out
, (abfd
, src
, dst
),
180 Elf_Internal_Sym
* src AND
181 Elf_External_Sym
* dst
)
183 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
184 put_word (abfd
, src
->st_value
, dst
->st_value
);
185 put_word (abfd
, src
->st_size
, dst
->st_size
);
186 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
187 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
188 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
192 /* Translate an ELF file header in external format into an ELF file header in
196 DEFUN (elf_swap_ehdr_in
, (abfd
, src
, dst
),
198 Elf_External_Ehdr
* src AND
199 Elf_Internal_Ehdr
* dst
)
201 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
202 dst
->e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_type
);
203 dst
->e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_machine
);
204 dst
->e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_version
);
205 dst
->e_entry
= get_word (abfd
, (bfd_byte
*) src
->e_entry
);
206 dst
->e_phoff
= get_word (abfd
, (bfd_byte
*) src
->e_phoff
);
207 dst
->e_shoff
= get_word (abfd
, (bfd_byte
*) src
->e_shoff
);
208 dst
->e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_flags
);
209 dst
->e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_ehsize
);
210 dst
->e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phentsize
);
211 dst
->e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phnum
);
212 dst
->e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shentsize
);
213 dst
->e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shnum
);
214 dst
->e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shstrndx
);
217 /* Translate an ELF file header in internal format into an ELF file header in
221 DEFUN (elf_swap_ehdr_out
, (abfd
, src
, dst
),
223 Elf_Internal_Ehdr
* src AND
224 Elf_External_Ehdr
* dst
)
226 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
227 /* note that all elements of dst are *arrays of unsigned char* already... */
228 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
229 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
230 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
231 put_word (abfd
, src
->e_entry
, dst
->e_entry
);
232 put_word (abfd
, src
->e_phoff
, dst
->e_phoff
);
233 put_word (abfd
, src
->e_shoff
, dst
->e_shoff
);
234 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
235 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
236 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
237 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
238 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
239 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
240 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
244 /* Translate an ELF section header table entry in external format into an
245 ELF section header table entry in internal format. */
248 DEFUN (elf_swap_shdr_in
, (abfd
, src
, dst
),
250 Elf_External_Shdr
* src AND
251 Elf_Internal_Shdr
* dst
)
253 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
254 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
255 dst
->sh_flags
= get_word (abfd
, (bfd_byte
*) src
->sh_flags
);
256 dst
->sh_addr
= get_word (abfd
, (bfd_byte
*) src
->sh_addr
);
257 dst
->sh_offset
= get_word (abfd
, (bfd_byte
*) src
->sh_offset
);
258 dst
->sh_size
= get_word (abfd
, (bfd_byte
*) src
->sh_size
);
259 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
260 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
261 dst
->sh_addralign
= get_word (abfd
, (bfd_byte
*) src
->sh_addralign
);
262 dst
->sh_entsize
= get_word (abfd
, (bfd_byte
*) src
->sh_entsize
);
263 /* we haven't done any processing on it yet, so... */
264 dst
->rawdata
= (void *) 0;
267 /* Translate an ELF section header table entry in internal format into an
268 ELF section header table entry in external format. */
271 DEFUN (elf_swap_shdr_out
, (abfd
, src
, dst
),
273 Elf_Internal_Shdr
* src AND
274 Elf_External_Shdr
* dst
)
276 /* note that all elements of dst are *arrays of unsigned char* already... */
277 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
278 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
279 put_word (abfd
, src
->sh_flags
, dst
->sh_flags
);
280 put_word (abfd
, src
->sh_addr
, dst
->sh_addr
);
281 put_word (abfd
, src
->sh_offset
, dst
->sh_offset
);
282 put_word (abfd
, src
->sh_size
, dst
->sh_size
);
283 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
284 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
285 put_word (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
286 put_word (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
290 /* Translate an ELF program header table entry in external format into an
291 ELF program header table entry in internal format. */
294 DEFUN (elf_swap_phdr_in
, (abfd
, src
, dst
),
296 Elf_External_Phdr
* src AND
297 Elf_Internal_Phdr
* dst
)
299 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
300 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
301 dst
->p_offset
= get_word (abfd
, (bfd_byte
*) src
->p_offset
);
302 dst
->p_vaddr
= get_word (abfd
, (bfd_byte
*) src
->p_vaddr
);
303 dst
->p_paddr
= get_word (abfd
, (bfd_byte
*) src
->p_paddr
);
304 dst
->p_filesz
= get_word (abfd
, (bfd_byte
*) src
->p_filesz
);
305 dst
->p_memsz
= get_word (abfd
, (bfd_byte
*) src
->p_memsz
);
306 dst
->p_align
= get_word (abfd
, (bfd_byte
*) src
->p_align
);
310 DEFUN (elf_swap_phdr_out
, (abfd
, src
, dst
),
312 Elf_Internal_Phdr
* src AND
313 Elf_External_Phdr
* dst
)
315 /* note that all elements of dst are *arrays of unsigned char* already... */
316 bfd_h_put_32 (abfd
, src
->p_type
, dst
->p_type
);
317 put_word (abfd
, src
->p_offset
, dst
->p_offset
);
318 put_word (abfd
, src
->p_vaddr
, dst
->p_vaddr
);
319 put_word (abfd
, src
->p_paddr
, dst
->p_paddr
);
320 put_word (abfd
, src
->p_filesz
, dst
->p_filesz
);
321 put_word (abfd
, src
->p_memsz
, dst
->p_memsz
);
322 bfd_h_put_32 (abfd
, src
->p_flags
, dst
->p_flags
);
323 put_word (abfd
, src
->p_align
, dst
->p_align
);
326 /* Translate an ELF reloc from external format to internal format. */
328 DEFUN (elf_swap_reloc_in
, (abfd
, src
, dst
),
330 Elf_External_Rel
* src AND
331 Elf_Internal_Rel
* dst
)
333 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
334 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
338 DEFUN (elf_swap_reloca_in
, (abfd
, src
, dst
),
340 Elf_External_Rela
* src AND
341 Elf_Internal_Rela
* dst
)
343 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
344 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
345 dst
->r_addend
= get_word (abfd
, (bfd_byte
*) src
->r_addend
);
348 /* Translate an ELF reloc from internal format to external format. */
350 DEFUN (elf_swap_reloc_out
, (abfd
, src
, dst
),
352 Elf_Internal_Rel
* src AND
353 Elf_External_Rel
* dst
)
355 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
356 put_word (abfd
, src
->r_info
, dst
->r_info
);
360 DEFUN (elf_swap_reloca_out
, (abfd
, src
, dst
),
362 Elf_Internal_Rela
* src AND
363 Elf_External_Rela
* dst
)
365 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
366 put_word (abfd
, src
->r_info
, dst
->r_info
);
367 put_word (abfd
, src
->r_addend
, dst
->r_addend
);
372 /* String table creation/manipulation routines */
374 static struct strtab
*
375 DEFUN (bfd_new_strtab
, (abfd
),
380 ss
= (struct strtab
*) bfd_xmalloc (sizeof (struct strtab
));
381 ss
->tab
= bfd_xmalloc (1);
382 BFD_ASSERT (ss
->tab
!= 0);
391 DEFUN (bfd_add_to_strtab
, (abfd
, ss
, str
),
393 struct strtab
*ss AND
396 /* should search first, but for now: */
397 /* include the trailing NUL */
398 int ln
= strlen (str
) + 1;
400 /* should this be using obstacks? */
401 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
403 BFD_ASSERT (ss
->tab
!= 0);
404 strcpy (ss
->tab
+ ss
->length
, str
);
408 return ss
->length
- ln
;
412 DEFUN (bfd_add_2_to_strtab
, (abfd
, ss
, str
, str2
),
414 struct strtab
*ss AND
418 /* should search first, but for now: */
419 /* include the trailing NUL */
420 int ln
= strlen (str
) + strlen (str2
) + 1;
422 /* should this be using obstacks? */
424 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
426 ss
->tab
= bfd_xmalloc (ln
);
428 BFD_ASSERT (ss
->tab
!= 0);
429 strcpy (ss
->tab
+ ss
->length
, str
);
430 strcpy (ss
->tab
+ ss
->length
+ strlen (str
), str2
);
434 return ss
->length
- ln
;
438 /* ELF .o/exec file reading */
440 /* Create a new bfd section from an ELF section header. */
443 DEFUN (bfd_section_from_shdr
, (abfd
, shindex
),
445 unsigned int shindex
)
447 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
448 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
452 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
454 switch (hdr
->sh_type
)
458 /* inactive section. Throw it away. */
462 /* Bits that get saved. This one is real. */
465 newsect
= bfd_make_section (abfd
, name
);
468 newsect
->filepos
= hdr
->sh_offset
; /* so we can read back the bits */
469 newsect
->flags
|= SEC_HAS_CONTENTS
;
470 newsect
->vma
= hdr
->sh_addr
;
471 newsect
->_raw_size
= hdr
->sh_size
;
472 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
474 if (hdr
->sh_flags
& SHF_ALLOC
)
476 newsect
->flags
|= SEC_ALLOC
;
477 newsect
->flags
|= SEC_LOAD
;
480 if (!(hdr
->sh_flags
& SHF_WRITE
))
481 newsect
->flags
|= SEC_READONLY
;
483 if (hdr
->sh_flags
& SHF_EXECINSTR
)
484 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
485 else if (newsect
->flags
& SEC_ALLOC
)
486 newsect
->flags
|= SEC_DATA
;
488 hdr
->rawdata
= (void *) newsect
;
491 hdr
->rawdata
= (void *) bfd_get_section_by_name (abfd
, name
);
496 /* Bits that get saved. This one is real. */
499 newsect
= bfd_make_section (abfd
, name
);
502 newsect
->vma
= hdr
->sh_addr
;
503 newsect
->_raw_size
= hdr
->sh_size
;
504 newsect
->filepos
= hdr
->sh_offset
; /* fake */
505 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
506 if (hdr
->sh_flags
& SHF_ALLOC
)
507 newsect
->flags
|= SEC_ALLOC
;
509 if (!(hdr
->sh_flags
& SHF_WRITE
))
510 newsect
->flags
|= SEC_READONLY
;
512 /* FIXME: This section is empty. Does it really make
513 sense to set SEC_CODE for it? */
514 if (hdr
->sh_flags
& SHF_EXECINSTR
)
515 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
517 hdr
->rawdata
= (void *) newsect
;
522 case SHT_SYMTAB
: /* A symbol table */
523 if (elf_onesymtab (abfd
) == shindex
)
526 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
527 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
528 elf_onesymtab (abfd
) = shindex
;
529 elf_tdata(abfd
)->symtab_hdr
= *hdr
;
530 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->symtab_hdr
;
531 abfd
->flags
|= HAS_SYMS
;
534 case SHT_STRTAB
: /* A string table */
537 if (ehdr
->e_shstrndx
== shindex
)
539 elf_tdata(abfd
)->shstrtab_hdr
= *hdr
;
540 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->shstrtab_hdr
;
541 hdr
->rawdata
= (PTR
) &elf_tdata(abfd
)->shstrtab_hdr
;
547 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
549 Elf_Internal_Shdr
*hdr2
= elf_elfsections(abfd
)[i
];
550 if (hdr2
->sh_link
== shindex
)
552 bfd_section_from_shdr (abfd
, i
);
553 if (elf_onesymtab (abfd
) == i
)
555 elf_tdata(abfd
)->strtab_hdr
= *hdr
;
556 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->strtab_hdr
;
559 #if 0 /* Not handling other string tables specially right now. */
560 hdr2
= elf_elfsections(abfd
)[i
]; /* in case it moved */
561 /* We have a strtab for some random other section. */
562 newsect
= (asection
*) hdr2
->rawdata
;
565 hdr
->rawdata
= (PTR
) newsect
;
566 hdr2
= &elf_section_data (newsect
)->str_hdr
;
568 elf_elfsections(abfd
)[shindex
] = hdr2
;
574 newsect
= bfd_make_section (abfd
, name
);
577 newsect
->flags
= SEC_HAS_CONTENTS
;
578 hdr
->rawdata
= (PTR
) newsect
;
579 newsect
->_raw_size
= hdr
->sh_size
;
580 newsect
->alignment_power
= 0;
583 if (hdr
->sh_flags
& SHF_ALLOC
)
584 newsect
->flags
|= SEC_ALLOC
|SEC_LOAD
;
585 if (!(hdr
->sh_flags
& SHF_WRITE
))
586 newsect
->flags
|= SEC_READONLY
;
587 if (hdr
->sh_flags
& SHF_EXECINSTR
)
588 newsect
->flags
|= SEC_CODE
;
589 else if (newsect
->flags
& SEC_ALLOC
)
590 newsect
->flags
|= SEC_DATA
;
597 /* *These* do a lot of work -- but build no sections!
598 The spec says there can be multiple strtabs, but only one symtab,
599 but there can be lots of REL* sections. */
600 /* FIXME: The above statement is wrong! There are typically at least
601 two symbol tables in a dynamically linked executable, ".dynsym"
602 which is the dynamic linkage symbol table and ".symtab", which is
603 the "traditional" symbol table. -fnf */
606 asection
*target_sect
;
607 Elf_Internal_Shdr
*hdr2
;
608 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
610 /* Don't allow REL relocations on a machine that uses RELA and
612 /* @@ Actually, the generic ABI does suggest that both might be
613 used in one file. But the four ABI Processor Supplements I
614 have access to right now all specify that only one is used on
615 each of those architectures. It's conceivable that, e.g., a
616 bunch of absolute 32-bit relocs might be more compact in REL
617 form even on a RELA machine... */
618 BFD_ASSERT (!(use_rela_p
&& (hdr
->sh_type
== SHT_REL
)));
619 BFD_ASSERT (!(!use_rela_p
&& (hdr
->sh_type
== SHT_RELA
)));
620 BFD_ASSERT (hdr
->sh_entsize
==
622 ? sizeof (Elf_External_Rela
)
623 : sizeof (Elf_External_Rel
)));
625 bfd_section_from_shdr (abfd
, hdr
->sh_info
); /* target */
626 bfd_section_from_shdr (abfd
, hdr
->sh_link
); /* symbol table */
627 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
628 if (target_sect
== NULL
)
631 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
633 elf_elfsections(abfd
)[shindex
] = hdr2
;
634 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
635 target_sect
->flags
|= SEC_RELOC
;
636 target_sect
->relocation
= 0;
637 target_sect
->rel_filepos
= hdr
->sh_offset
;
638 abfd
->flags
|= HAS_RELOC
;
645 case SHT_DYNSYM
: /* could treat this like symtab... */
647 fprintf (stderr
, "Dynamic Linking sections not yet supported.\n");
654 fprintf (stderr
, "Note Sections not yet supported.\n");
661 fprintf (stderr
, "SHLIB Sections not supported (and non conforming.)\n");
666 /* Check for any processor-specific section types. */
668 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
670 if (bed
->elf_backend_section_from_shdr
)
671 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
680 DEFUN (elf_new_section_hook
, (abfd
, sec
),
684 struct bfd_elf_section_data
*sdata
;
686 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
687 sec
->used_by_bfd
= (PTR
) sdata
;
688 memset (sdata
, 0, sizeof (*sdata
));
692 /* Create a new bfd section from an ELF program header.
694 Since program segments have no names, we generate a synthetic name
695 of the form segment<NUM>, where NUM is generally the index in the
696 program header table. For segments that are split (see below) we
697 generate the names segment<NUM>a and segment<NUM>b.
699 Note that some program segments may have a file size that is different than
700 (less than) the memory size. All this means is that at execution the
701 system must allocate the amount of memory specified by the memory size,
702 but only initialize it with the first "file size" bytes read from the
703 file. This would occur for example, with program segments consisting
704 of combined data+bss.
706 To handle the above situation, this routine generates TWO bfd sections
707 for the single program segment. The first has the length specified by
708 the file size of the segment, and the second has the length specified
709 by the difference between the two sizes. In effect, the segment is split
710 into it's initialized and uninitialized parts.
715 DEFUN (bfd_section_from_phdr
, (abfd
, hdr
, index
),
717 Elf_Internal_Phdr
* hdr AND
725 split
= ((hdr
->p_memsz
> 0) &&
726 (hdr
->p_filesz
> 0) &&
727 (hdr
->p_memsz
> hdr
->p_filesz
));
728 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
729 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
730 strcpy (name
, namebuf
);
731 newsect
= bfd_make_section (abfd
, name
);
732 newsect
->vma
= hdr
->p_vaddr
;
733 newsect
->_raw_size
= hdr
->p_filesz
;
734 newsect
->filepos
= hdr
->p_offset
;
735 newsect
->flags
|= SEC_HAS_CONTENTS
;
736 if (hdr
->p_type
== PT_LOAD
)
738 newsect
->flags
|= SEC_ALLOC
;
739 newsect
->flags
|= SEC_LOAD
;
740 if (hdr
->p_flags
& PF_X
)
742 /* FIXME: all we known is that it has execute PERMISSION,
744 newsect
->flags
|= SEC_CODE
;
747 if (!(hdr
->p_flags
& PF_W
))
749 newsect
->flags
|= SEC_READONLY
;
754 sprintf (namebuf
, "segment%db", index
);
755 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
756 strcpy (name
, namebuf
);
757 newsect
= bfd_make_section (abfd
, name
);
758 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
759 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
760 if (hdr
->p_type
== PT_LOAD
)
762 newsect
->flags
|= SEC_ALLOC
;
763 if (hdr
->p_flags
& PF_X
)
764 newsect
->flags
|= SEC_CODE
;
766 if (!(hdr
->p_flags
& PF_W
))
767 newsect
->flags
|= SEC_READONLY
;
773 /* Begin processing a given object.
775 First we validate the file by reading in the ELF header and checking
778 static INLINE boolean
779 DEFUN (elf_file_p
, (x_ehdrp
), Elf_External_Ehdr
* x_ehdrp
)
781 return ((x_ehdrp
->e_ident
[EI_MAG0
] == ELFMAG0
)
782 && (x_ehdrp
->e_ident
[EI_MAG1
] == ELFMAG1
)
783 && (x_ehdrp
->e_ident
[EI_MAG2
] == ELFMAG2
)
784 && (x_ehdrp
->e_ident
[EI_MAG3
] == ELFMAG3
));
787 /* Check to see if the file associated with ABFD matches the target vector
790 Note that we may be called several times with the same ABFD, but different
791 target vectors, most of which will not match. We have to avoid leaving
792 any side effects in ABFD, or any data it points to (like tdata), if the
793 file does not match the target vector.
795 FIXME: There is memory leak if we are called more than once with the same
796 ABFD, and that bfd already has tdata allocated, since we allocate more tdata
797 and the old tdata is orphaned. Since it's in the bfd obstack, there isn't
798 much we can do about this except possibly rewrite the code. There are
799 also other bfd_allocs that may be the source of memory leaks as well. */
802 DEFUN (elf_object_p
, (abfd
), bfd
* abfd
)
804 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
805 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
806 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
807 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
809 char *shstrtab
; /* Internal copy of section header stringtab */
810 struct elf_backend_data
*ebd
; /* Use to get ELF_ARCH stored in xvec */
811 struct elf_obj_tdata
*preserved_tdata
= elf_tdata (abfd
);
813 /* Read in the ELF header in external format. */
815 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
816 goto got_system_call_error
;
818 /* Now check to see if we have a valid ELF file, and one that BFD can
819 make use of. The magic number must match, the address size ('class')
820 and byte-swapping must match our XVEC entry, and it must have a
821 section header table (FIXME: See comments re sections at top of this
824 if ((elf_file_p (&x_ehdr
) == false) ||
825 (x_ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) ||
826 (x_ehdr
.e_ident
[EI_CLASS
] != ELFCLASS
))
827 goto got_wrong_format_error
;
829 /* Check that file's byte order matches xvec's */
830 switch (x_ehdr
.e_ident
[EI_DATA
])
832 case ELFDATA2MSB
: /* Big-endian */
833 if (!abfd
->xvec
->header_byteorder_big_p
)
834 goto got_wrong_format_error
;
836 case ELFDATA2LSB
: /* Little-endian */
837 if (abfd
->xvec
->header_byteorder_big_p
)
838 goto got_wrong_format_error
;
840 case ELFDATANONE
: /* No data encoding specified */
841 default: /* Unknown data encoding specified */
842 goto got_wrong_format_error
;
845 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
846 the tdata pointer in the bfd. FIXME: memory leak, see above. */
849 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
850 if (elf_tdata (abfd
) == NULL
)
851 goto got_no_memory_error
;
853 /* Now that we know the byte order, swap in the rest of the header */
854 i_ehdrp
= elf_elfheader (abfd
);
855 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
857 elf_debug_file (i_ehdrp
);
860 /* If there is no section header table, we're hosed. */
861 if (i_ehdrp
->e_shoff
== 0)
862 goto got_wrong_format_error
;
864 if (i_ehdrp
->e_type
== ET_EXEC
|| i_ehdrp
->e_type
== ET_DYN
)
865 abfd
->flags
|= EXEC_P
;
867 /* Retrieve the architecture information from the xvec and verify
868 that it matches the machine info stored in the ELF header.
869 This allows us to resolve ambiguous formats that might not
870 otherwise be distinguishable. */
872 ebd
= get_elf_backend_data (abfd
);
874 /* Perhaps the elf architecture value should be another field in the
875 elf backend data? If you change this to work that way, make sure
876 that you still get bfd_arch_unknown for unknown architecture types,
877 and that it still gets accepted by the `generic' elf target. */
880 enum bfd_architecture arch
= bfd_arch_unknown
;
882 for (i
= 0; i
< bfd_elf_arch_map_size
; i
++)
884 if (bfd_elf_arch_map
[i
].elf_arch
== i_ehdrp
->e_machine
)
886 arch
= bfd_elf_arch_map
[i
].bfd_arch
;
890 /* start-sanitize-v9 */
891 if (i_ehdrp
->e_machine
== EM_SPARC64
)
892 arch
= bfd_arch_sparc
;
893 /* end-sanitize-v9 */
894 if (ebd
->arch
!= arch
)
895 goto got_wrong_format_error
;
896 bfd_default_set_arch_mach (abfd
, arch
, 0);
899 /* Allocate space for a copy of the section header table in
900 internal form, seek to the section header table in the file,
901 read it in, and convert it to internal form. As a simple sanity
902 check, verify that the what BFD thinks is the size of each section
903 header table entry actually matches the size recorded in the file. */
905 if (i_ehdrp
->e_shentsize
!= sizeof (x_shdr
))
906 goto got_wrong_format_error
;
907 i_shdrp
= (Elf_Internal_Shdr
*)
908 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
);
909 elf_elfsections (abfd
) =
910 (Elf_Internal_Shdr
**) bfd_alloc (abfd
, sizeof (i_shdrp
) * i_ehdrp
->e_shnum
);
911 if (!i_shdrp
|| !elf_elfsections(abfd
))
912 goto got_no_memory_error
;
913 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) == -1)
914 goto got_system_call_error
;
915 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
917 if (bfd_read ((PTR
) & x_shdr
, sizeof x_shdr
, 1, abfd
) != sizeof (x_shdr
))
918 goto got_system_call_error
;
919 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
920 elf_elfsections(abfd
)[shindex
] = i_shdrp
+ shindex
;
922 if (i_ehdrp
->e_shstrndx
)
924 bfd_section_from_shdr (abfd
, i_ehdrp
->e_shstrndx
);
928 for (shindex
= i_ehdrp
->e_shnum
- 1; shindex
>= 0; shindex
--)
930 if (!strcmp (elf_string_from_elf_strtab (abfd
,
931 i_shdrp
[shindex
].sh_name
),
934 elf_tdata(abfd
)->strtab_hdr
= i_shdrp
[shindex
];
935 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->strtab_hdr
;
937 else if (!strcmp (elf_string_from_elf_strtab (abfd
,
938 i_shdrp
[shindex
].sh_name
),
941 elf_tdata(abfd
)->symtab_hdr
= i_shdrp
[shindex
];
942 elf_elfsections(abfd
)[shindex
] = &elf_tdata(abfd
)->symtab_hdr
;
943 elf_onesymtab (abfd
) = shindex
;
948 /* Read in the string table containing the names of the sections. We
949 will need the base pointer to this table later. */
950 /* We read this inline now, so that we don't have to go through
951 bfd_section_from_shdr with it (since this particular strtab is
952 used to find all of the ELF section names.) */
954 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
956 goto got_wrong_format_error
;
958 /* Once all of the section headers have been read and converted, we
959 can start processing them. Note that the first section header is
960 a dummy placeholder entry, so we ignore it.
962 We also watch for the symbol table section and remember the file
963 offset and section size for both the symbol table section and the
964 associated string table section. */
966 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
968 bfd_section_from_shdr (abfd
, shindex
);
971 /* Remember the entry point specified in the ELF file header. */
973 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
977 /* If we are going to use goto's to avoid duplicating error setting
978 and return(NULL) code, then this at least makes it more maintainable. */
980 got_system_call_error
:
981 bfd_error
= system_call_error
;
983 got_wrong_format_error
:
984 bfd_error
= wrong_format
;
987 bfd_error
= no_memory
;
990 elf_tdata (abfd
) = preserved_tdata
;
995 /* ELF .o/exec file writing */
997 /* Takes a bfd and a symbol, returns a pointer to the elf specific area
998 of the symbol if there is one. */
999 static INLINE elf_symbol_type
*
1000 DEFUN (elf_symbol_from
, (ignore_abfd
, symbol
),
1001 bfd
* ignore_abfd AND
1004 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
1007 if (symbol
->the_bfd
->tdata
.elf_obj_data
== (struct elf_obj_tdata
*) NULL
)
1010 return (elf_symbol_type
*) symbol
;
1013 /* Create ELF output from BFD sections.
1015 Essentially, just create the section header and forget about the program
1019 DEFUN (elf_make_sections
, (abfd
, asect
, obj
),
1021 asection
* asect AND
1024 /* most of what is in bfd_shdr_from_section goes in here... */
1025 /* and all of these sections generate at *least* one ELF section. */
1028 Elf_Internal_Shdr
*this_hdr
;
1029 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1031 this_hdr
->sh_addr
= asect
->vma
;
1032 this_hdr
->sh_size
= asect
->_raw_size
;
1033 /* contents already set by elf_set_section_contents */
1035 if (asect
->flags
& SEC_RELOC
)
1037 /* emit a reloc section, and thus strtab and symtab... */
1038 Elf_Internal_Shdr
*rela_hdr
;
1039 Elf_External_Rela
*outbound_relocas
;
1040 Elf_External_Rel
*outbound_relocs
;
1041 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1043 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1045 /* orelocation has the data, reloc_count has the count... */
1048 rela_hdr
->sh_type
= SHT_RELA
;
1049 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1052 /* REL relocations */
1054 rela_hdr
->sh_type
= SHT_REL
;
1055 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rel
);
1057 rela_hdr
->sh_flags
= 0;
1058 rela_hdr
->sh_addr
= 0;
1059 rela_hdr
->sh_offset
= 0;
1060 rela_hdr
->sh_addralign
= 0;
1063 if (asect
->flags
& SEC_ALLOC
)
1065 this_hdr
->sh_flags
|= SHF_ALLOC
;
1066 if (asect
->flags
& SEC_LOAD
)
1068 /* @@ Do something with sh_type? */
1071 if (!(asect
->flags
& SEC_READONLY
))
1072 this_hdr
->sh_flags
|= SHF_WRITE
;
1074 if (asect
->flags
& SEC_CODE
)
1075 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1079 write_relocs (abfd
, sec
, xxx
)
1084 Elf_Internal_Shdr
*rela_hdr
;
1085 Elf_External_Rela
*outbound_relocas
;
1086 Elf_External_Rel
*outbound_relocs
;
1088 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1089 asymbol
*last_sym
= 0;
1092 if ((sec
->flags
& SEC_RELOC
) == 0)
1094 /* Flags are sometimes inconsistent. */
1095 if (sec
->reloc_count
== 0)
1098 rela_hdr
= &elf_section_data (sec
)->rel_hdr
;
1100 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* sec
->reloc_count
;
1101 rela_hdr
->contents
= (void *) bfd_alloc (abfd
, rela_hdr
->sh_size
);
1103 /* orelocation has the data, reloc_count has the count... */
1106 outbound_relocas
= (Elf_External_Rela
*) rela_hdr
->contents
;
1108 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1110 Elf_Internal_Rela dst_rela
;
1111 Elf_External_Rela
*src_rela
;
1116 ptr
= sec
->orelocation
[idx
];
1117 src_rela
= outbound_relocas
+ idx
;
1118 if (!(abfd
->flags
& EXEC_P
))
1119 dst_rela
.r_offset
= ptr
->address
- sec
->vma
;
1121 dst_rela
.r_offset
= ptr
->address
;
1123 sym
= *ptr
->sym_ptr_ptr
;
1124 if (sym
== last_sym
)
1129 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1131 dst_rela
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1133 dst_rela
.r_addend
= ptr
->addend
;
1134 elf_swap_reloca_out (abfd
, &dst_rela
, src_rela
);
1138 /* REL relocations */
1140 outbound_relocs
= (Elf_External_Rel
*) rela_hdr
->contents
;
1142 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1144 Elf_Internal_Rel dst_rel
;
1145 Elf_External_Rel
*src_rel
;
1150 ptr
= sec
->orelocation
[idx
];
1151 sym
= *ptr
->sym_ptr_ptr
;
1152 src_rel
= outbound_relocs
+ idx
;
1153 if (!(abfd
->flags
& EXEC_P
))
1154 dst_rel
.r_offset
= ptr
->address
- sec
->vma
;
1156 dst_rel
.r_offset
= ptr
->address
;
1158 if (sym
== last_sym
)
1163 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1165 dst_rel
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1167 elf_swap_reloc_out (abfd
, &dst_rel
, src_rel
);
1173 fix_up_strtabs (abfd
, asect
, obj
)
1178 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (asect
)->this_hdr
;
1179 int this_idx
= elf_section_data(asect
)->this_idx
;
1181 /* @@ Check flags! */
1182 if (!strncmp (asect
->name
, ".stab", 5)
1183 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
1185 size_t len
= strlen (asect
->name
) + 1;
1186 char *s
= (char *) alloca (len
);
1187 strcpy (s
, asect
->name
);
1189 asect
= bfd_get_section_by_name (abfd
, s
);
1192 elf_section_data(asect
)->this_hdr
.sh_link
= this_idx
;
1194 /* @@ Assuming 32 bits! */
1195 this_hdr
->sh_entsize
= 0xc;
1200 DEFUN (elf_fake_sections
, (abfd
, asect
, obj
),
1202 asection
* asect AND
1205 /* most of what is in bfd_shdr_from_section goes in here... */
1206 /* and all of these sections generate at *least* one ELF section. */
1208 Elf_Internal_Shdr
*this_hdr
;
1209 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1211 bfd_add_to_strtab (abfd
, elf_shstrtab (abfd
), asect
->name
);
1212 /* We need to log the type *now* so that elf_section_from_bfd_section
1213 can find us... have to set rawdata too. */
1214 this_hdr
->rawdata
= (void *) asect
;
1215 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1216 if ((asect
->flags
& SEC_ALLOC
) && (asect
->flags
& SEC_LOAD
))
1217 this_hdr
->sh_type
= SHT_PROGBITS
;
1218 else if ((asect
->flags
& SEC_ALLOC
) && ((asect
->flags
& SEC_LOAD
) == 0))
1220 BFD_ASSERT (!strcmp (asect
->name
, ".bss"));
1221 this_hdr
->sh_type
= SHT_NOBITS
;
1223 /* FIXME I am not sure how to detect a .note section from the flags
1224 word of an `asection'. */
1225 else if (!strcmp (asect
->name
, ".note"))
1226 this_hdr
->sh_type
= SHT_NOTE
;
1228 this_hdr
->sh_type
= SHT_PROGBITS
;
1230 /* Now, check for processor-specific section types. */
1232 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1234 if (bed
->elf_backend_fake_sections
)
1235 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1238 this_hdr
->sh_flags
= 0;
1239 this_hdr
->sh_addr
= 0;
1240 this_hdr
->sh_size
= 0;
1241 this_hdr
->sh_entsize
= 0;
1242 this_hdr
->sh_info
= 0;
1243 this_hdr
->sh_link
= 0;
1244 this_hdr
->sh_offset
= 0;
1248 /* Emit a strtab and symtab, and possibly a reloc section. */
1249 Elf_Internal_Shdr
*rela_hdr
;
1250 Elf_Internal_Shdr
*symstrtab_hdr
;
1252 /* Note that only one symtab is used, so just remember it
1255 if (asect
->flags
& SEC_RELOC
)
1257 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1259 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1261 bfd_add_2_to_strtab (abfd
, elf_shstrtab (abfd
),
1262 use_rela_p
? ".rela" : ".rel",
1264 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1265 rela_hdr
->sh_entsize
= (use_rela_p
1266 ? sizeof (Elf_External_Rela
)
1267 : sizeof (Elf_External_Rel
));
1269 rela_hdr
->sh_flags
= 0;
1270 rela_hdr
->sh_addr
= 0;
1271 rela_hdr
->sh_size
= 0;
1272 rela_hdr
->sh_offset
= 0;
1273 rela_hdr
->sh_addralign
= 0;
1277 if (asect
->flags
& SEC_ALLOC
)
1279 this_hdr
->sh_flags
|= SHF_ALLOC
;
1280 if (asect
->flags
& SEC_LOAD
)
1282 /* @@ Do something with sh_type? */
1285 if (!(asect
->flags
& SEC_READONLY
))
1286 this_hdr
->sh_flags
|= SHF_WRITE
;
1287 if (asect
->flags
& SEC_CODE
)
1288 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1294 xxxINTERNAL_FUNCTION
1298 struct elf_internal_shdr *bfd_elf_locate_sh (bfd *abfd,
1299 struct strtab *strtab,
1300 struct elf_internal_shdr *shdrp,
1304 Helper function to locate an ELF section header given the
1305 name of a BFD section.
1308 static struct elf_internal_shdr
*
1309 DEFUN (elf_locate_sh
, (abfd
, strtab
, shdrp
, name
),
1311 struct strtab
*strtab AND
1312 struct elf_internal_shdr
*shdrp AND
1315 Elf_Internal_Shdr
*gotit
= NULL
;
1318 if (shdrp
!= NULL
&& strtab
!= NULL
)
1320 max
= elf_elfheader (abfd
)->e_shnum
;
1321 for (i
= 1; i
< max
; i
++)
1323 if (!strcmp (strtab
->tab
+ shdrp
[i
].sh_name
, name
))
1333 /* Map symbol from it's internal number to the external number, moving
1334 all local symbols to be at the head of the list. */
1340 if (sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
))
1342 if (sym
->flags
& BSF_LOCAL
)
1346 if (sym
->section
== 0)
1348 /* Is this valid? */
1353 if (sym
->section
== &bfd_und_section
)
1355 if (bfd_is_com_section (sym
->section
))
1357 if (sym
->flags
& (BSF_LOCAL
| BSF_SECTION_SYM
| BSF_FILE
))
1363 DEFUN (elf_map_symbols
, (abfd
), bfd
* abfd
)
1365 int symcount
= bfd_get_symcount (abfd
);
1366 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1367 asymbol
**sect_syms
;
1369 int num_globals
= 0;
1370 int num_locals2
= 0;
1371 int num_globals2
= 0;
1373 int num_sections
= 0;
1374 Elf_Sym_Extra
*sym_extra
;
1379 fprintf (stderr
, "elf_map_symbols\n");
1383 /* Add local symbols for each section for which there are relocs.
1384 FIXME: How can we tell which sections have relocs at this point?
1385 Will reloc_count always be accurate? Actually, I think most ELF
1386 targets create section symbols for all sections anyhow. */
1387 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1389 if (max_index
< asect
->index
)
1390 max_index
= asect
->index
;
1394 elf_num_section_syms (abfd
) = max_index
;
1395 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1396 elf_section_syms (abfd
) = sect_syms
;
1398 BFD_ASSERT (sect_syms
!= 0);
1400 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1402 asymbol
*sym
= bfd_make_empty_symbol (abfd
);
1403 sym
->the_bfd
= abfd
;
1404 sym
->name
= asect
->name
;
1405 sym
->value
= asect
->vma
;
1406 sym
->flags
= BSF_SECTION_SYM
;
1407 sym
->section
= asect
;
1408 sect_syms
[asect
->index
] = sym
;
1412 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1413 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1420 syms
= (asymbol
**) bfd_realloc (abfd
, syms
,
1421 ((symcount
+ num_sections
+ 1)
1422 * sizeof (asymbol
*)));
1424 syms
= (asymbol
**) bfd_alloc (abfd
,
1425 (num_sections
+ 1) * sizeof (asymbol
*));
1427 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1429 if (sect_syms
[asect
->index
])
1430 syms
[symcount
++] = sect_syms
[asect
->index
];
1433 syms
[symcount
] = (asymbol
*) 0;
1434 bfd_set_symtab (abfd
, syms
, symcount
);
1437 elf_sym_extra (abfd
) = sym_extra
1438 = (Elf_Sym_Extra
*) bfd_alloc (abfd
, symcount
* sizeof (Elf_Sym_Extra
));
1440 /* Identify and classify all of the symbols. */
1441 for (idx
= 0; idx
< symcount
; idx
++)
1443 if (!sym_is_global (syms
[idx
]))
1449 /* Now provide mapping information. Add +1 for skipping over the
1451 for (idx
= 0; idx
< symcount
; idx
++)
1453 syms
[idx
]->udata
= (PTR
) &sym_extra
[idx
];
1454 if (!sym_is_global (syms
[idx
]))
1455 sym_extra
[idx
].elf_sym_num
= 1 + num_locals2
++;
1457 sym_extra
[idx
].elf_sym_num
= 1 + num_locals
+ num_globals2
++;
1460 elf_num_locals (abfd
) = num_locals
;
1461 elf_num_globals (abfd
) = num_globals
;
1464 static void assign_section_numbers ();
1465 static void assign_file_positions_except_relocs ();
1468 DEFUN (elf_compute_section_file_positions
, (abfd
), bfd
* abfd
)
1470 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1471 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1472 struct strtab
*shstrtab
;
1473 int count
, maxsections
;
1475 bfd_map_over_sections (abfd
, elf_fake_sections
, 0);
1477 assign_section_numbers (abfd
);
1479 bfd_map_over_sections (abfd
, elf_make_sections
, 0);
1481 bfd_map_over_sections (abfd
, fix_up_strtabs
, 0); /* .stab/.stabstr &c */
1483 swap_out_syms (abfd
);
1485 assign_file_positions_except_relocs (abfd
);
1491 DEFUN (elf_write_phdrs
, (abfd
, i_ehdrp
, i_phdrp
, phdr_cnt
),
1493 Elf_Internal_Ehdr
* i_ehdrp AND
1494 Elf_Internal_Phdr
* i_phdrp AND
1495 Elf32_Half phdr_cnt
)
1497 /* first program header entry goes after the file header */
1498 int outbase
= i_ehdrp
->e_phoff
;
1500 Elf_External_Phdr x_phdr
;
1502 for (i
= 0; i
< phdr_cnt
; i
++)
1504 elf_swap_phdr_out (abfd
, i_phdrp
+ i
, &x_phdr
);
1505 bfd_seek (abfd
, outbase
, SEEK_SET
);
1506 bfd_write ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
);
1507 outbase
+= sizeof (x_phdr
);
1513 static const Elf_Internal_Shdr null_shdr
;
1515 /* Assign all ELF section numbers. The dummy first section is handled here
1516 too. The link/info pointers for the standard section types are filled
1517 in here too, while we're at it. (Link pointers for .stab sections are
1518 not filled in here.) */
1520 assign_section_numbers (abfd
)
1523 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1525 int section_number
= 1;
1527 Elf_Internal_Shdr
**i_shdrp
;
1529 t
->shstrtab_hdr
.sh_size
= elf_shstrtab(abfd
)->length
;
1530 t
->shstrtab_hdr
.contents
= (void *) elf_shstrtab(abfd
)->tab
;
1531 shstrtab_length_fixed
= 1;
1533 t
->shstrtab_section
= section_number
++;
1534 elf_elfheader(abfd
)->e_shstrndx
= t
->shstrtab_section
;
1537 t
->symtab_section
= section_number
++;
1538 t
->strtab_section
= section_number
++;
1539 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1541 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1543 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1544 d
->this_idx
= section_number
++;
1545 if (sec
->flags
& SEC_RELOC
)
1547 d
->rel_idx
= section_number
++;
1548 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1549 d
->rel_hdr
.sh_info
= d
->this_idx
;
1553 /* No handling for per-section string tables currently. */
1555 elf_elfheader(abfd
)->e_shnum
= section_number
;
1557 /* Set up the list of section header pointers, in agreement with the
1559 i_shdrp
= (Elf_Internal_Shdr
**)
1560 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*));
1561 elf_elfsections(abfd
) = i_shdrp
;
1562 for (i
= 0; i
< section_number
; i
++)
1565 i_shdrp
[0] = (Elf_Internal_Shdr
*) &null_shdr
;
1566 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1569 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1570 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1572 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1574 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1575 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1577 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1579 /* Make sure we got everything.... */
1580 for (i
= 0; i
< section_number
; i
++)
1581 if (i_shdrp
[i
] == 0)
1585 static INLINE file_ptr
1586 assign_file_position_for_section (i_shdrp
, offset
)
1587 Elf_Internal_Shdr
*i_shdrp
;
1590 i_shdrp
->sh_offset
= offset
;
1591 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1592 offset
+= i_shdrp
->sh_size
;
1596 static INLINE file_ptr
1597 assign_file_positions_for_symtab_and_strtabs (abfd
, off
)
1601 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1603 off
= assign_file_position_for_section (&t
->shstrtab_hdr
, off
);
1604 off
= assign_file_position_for_section (&t
->symtab_hdr
, off
);
1605 off
= assign_file_position_for_section (&t
->strtab_hdr
, off
);
1610 bfd_vma low
, mem_size
;
1614 struct seg_info
*next
;
1618 map_program_segments (abfd
)
1621 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1622 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1623 Elf_Internal_Shdr
*i_shdrp
;
1624 Elf_Internal_Phdr
*phdr
;
1627 file_ptr lowest_offset
= 0;
1628 struct seg_info
*seg
= 0;
1630 done
= (char *) alloca (i_ehdrp
->e_shnum
);
1631 memset (done
, 0, i_ehdrp
->e_shnum
);
1632 for (i
= 0; i
< i_ehdrp
->e_shnum
; i
++)
1634 i_shdrp
= i_shdrpp
[i
];
1635 /* If it's going to be mapped in, it's been assigned a position. */
1636 if (i_shdrp
->sh_offset
+ 1 == 0)
1638 /* Well, not really, but we won't process it here. */
1642 if (i_shdrp
->sh_offset
< lowest_offset
1643 || lowest_offset
== 0)
1644 lowest_offset
= i_shdrp
->sh_offset
;
1645 /* Only interested in PROGBITS or NOBITS for generating segments. */
1646 switch (i_shdrp
->sh_type
)
1659 bfd_vma lowest_vma
= -1, high
;
1664 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1666 i_shdrp
= i_shdrpp
[i
];
1667 if (!done
[i
] && i_shdrp
->sh_addr
< lowest_vma
)
1669 lowest_vma
= i_shdrp
->sh_addr
;
1675 /* So now we know the lowest vma of any unassigned sections; start
1679 s
= (struct seg_info
*) bfd_alloc (abfd
, sizeof (struct seg_info
));
1683 seg
->low
= lowest_vma
;
1684 i_shdrp
= i_shdrpp
[low_sec
];
1685 seg
->start_pos
= i_shdrp
->sh_offset
;
1686 seg
->sh_flags
= i_shdrp
->sh_flags
;
1687 done
[low_sec
] = 1, n_left
--;
1688 mem_size
= i_shdrp
->sh_size
;
1689 high
= lowest_vma
+ i_shdrp
->sh_size
;
1691 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1692 file_size
= i_shdrp
->sh_size
;
1694 for (i
= 0; i
< i_ehdrp
->e_shnum
; i
++)
1698 if (file_size
!= mem_size
)
1702 i_shdrp
= i_shdrpp
[i
];
1703 /* position of next byte on disk */
1704 f1
= seg
->start_pos
+ file_size
;
1705 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1707 if (i_shdrp
->sh_offset
- f1
!= i_shdrp
->sh_addr
- high
)
1710 else /* sh_type == NOBITS */
1712 /* If the section in question has no contents in the disk
1713 file, we really don't care where it supposedly starts.
1714 But we don't want to bother merging it into this segment
1715 if it doesn't start on this memory page. */
1716 bfd_vma page1
, page2
;
1717 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1719 /* page number in address space of current end of seg */
1720 page1
= (high
- 1 + maxpagesize
- 1) / maxpagesize
;
1721 /* page number in address space of start of this section */
1722 page2
= (i_shdrp
->sh_addr
+ maxpagesize
- 1) / maxpagesize
;
1727 done
[i
] = 1, n_left
--;
1728 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1729 file_size
= i_shdrp
->sh_offset
+ i_shdrp
->sh_size
- seg
->start_pos
;
1730 mem_size
= i_shdrp
->sh_addr
+ i_shdrp
->sh_size
- seg
->low
;
1731 high
= i_shdrp
->sh_addr
+ i_shdrp
->sh_size
;
1734 seg
->file_size
= file_size
;
1735 seg
->mem_size
= mem_size
;
1737 /* Now do something with the list of segments we've built up. */
1739 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1744 for (s
= seg
; s
; s
= s
->next
)
1748 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
1749 sz
= sizeof (Elf_External_Phdr
) * n_segs
;
1750 if (i_ehdrp
->e_ehsize
+ sz
<= lowest_offset
)
1751 i_ehdrp
->e_phoff
= i_ehdrp
->e_ehsize
;
1754 i_ehdrp
->e_phoff
= elf_tdata (abfd
)->next_file_pos
;
1755 elf_tdata (abfd
)->next_file_pos
+= sz
;
1757 phdr
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
,
1758 n_segs
* sizeof (Elf_Internal_Phdr
));
1759 elf_tdata (abfd
)->phdr
= phdr
;
1762 phdr
->p_type
= PT_LOAD
; /* only type we really support so far */
1763 phdr
->p_offset
= seg
->start_pos
;
1764 phdr
->p_vaddr
= seg
->low
;
1766 phdr
->p_filesz
= seg
->file_size
;
1767 phdr
->p_memsz
= seg
->mem_size
;
1768 phdr
->p_flags
= PF_R
;
1769 phdr
->p_align
= maxpagesize
; /* ? */
1770 if (seg
->sh_flags
& SHF_WRITE
)
1771 /* SysVr4 ELF docs say "data segments normally have read, write,
1772 and execute permissions." */
1773 phdr
->p_flags
|= (PF_W
| PF_X
);
1774 if (seg
->sh_flags
& SHF_EXECINSTR
)
1775 phdr
->p_flags
|= PF_X
;
1779 i_ehdrp
->e_phnum
= n_segs
;
1781 elf_write_phdrs (abfd
, i_ehdrp
, elf_tdata (abfd
)->phdr
, i_ehdrp
->e_phnum
);
1785 assign_file_positions_except_relocs (abfd
)
1788 /* For now, we ignore the possibility of having program segments, which
1789 may require some alignment in the file. That'll require padding, and
1790 some interesting calculations to optimize file space usage.
1792 Also, since the application may change the list of relocations for
1793 a given section, we don't figure them in here. We'll put them at the
1794 end of the file, at positions computed during bfd_close.
1796 The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ...
1797 or: <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */
1801 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1802 Elf_Internal_Shdr
*i_shdrp
;
1803 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1804 int exec_p
= (abfd
->flags
& EXEC_P
) != 0;
1806 /* Everything starts after the ELF file header. */
1807 off
= i_ehdrp
->e_ehsize
;
1811 /* Section headers. */
1812 i_ehdrp
->e_shoff
= off
;
1813 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1815 off
= assign_file_positions_for_symtab_and_strtabs (abfd
, off
);
1817 for (i
= 0; i
< i_ehdrp
->e_shnum
; i
++)
1819 i_shdrp
= i_shdrpp
[i
];
1820 if (i_shdrp
->sh_type
== SHT_REL
|| i_shdrp
->sh_type
== SHT_RELA
)
1822 i_shdrp
->sh_offset
= -1;
1827 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1828 if (maxpagesize
== 0)
1829 maxpagesize
= 1; /* make the arithmetic work */
1830 /* This isn't necessarily going to give the best packing, if the
1831 segments require padding between them, but since that isn't
1832 usually the case, this'll do. */
1833 if ((i_shdrp
->sh_flags
& SHF_ALLOC
) == 0)
1835 i_shdrp
->sh_offset
= -1;
1838 /* Blindly assume that the segments are ordered optimally. With
1839 the default LD script, they will be. */
1841 /* need big unsigned type */
1843 addtl_off
= i_shdrp
->sh_addr
- off
;
1844 addtl_off
= addtl_off
% maxpagesize
;
1850 if (i_shdrp
->sh_type
== SHT_NOBITS
)
1853 i_shdrp
->sh_offset
= off
;
1854 if (off
% maxpagesize
!= 0)
1855 off2
= maxpagesize
- (off
% maxpagesize
);
1856 if (off2
> i_shdrp
->sh_size
)
1857 off2
= i_shdrp
->sh_size
;
1861 off
= assign_file_position_for_section (i_shdrp
, off
);
1863 && get_elf_backend_data(abfd
)->maxpagesize
> 1
1864 && i_shdrp
->sh_type
== SHT_PROGBITS
1865 && (i_shdrp
->sh_flags
& SHF_ALLOC
)
1866 && (i_shdrp
->sh_offset
- i_shdrp
->sh_addr
) % get_elf_backend_data(abfd
)->maxpagesize
!= 0)
1871 elf_tdata (abfd
)->next_file_pos
= off
;
1872 map_program_segments (abfd
);
1873 off
= elf_tdata (abfd
)->next_file_pos
;
1875 /* Section headers. */
1876 i_ehdrp
->e_shoff
= off
;
1877 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1879 off
= assign_file_positions_for_symtab_and_strtabs (abfd
, off
);
1881 for (i
= 0; i
< i_ehdrp
->e_shnum
; i
++)
1883 i_shdrp
= i_shdrpp
[i
];
1884 if (i_shdrp
->sh_offset
+ 1 == 0
1885 && i_shdrp
->sh_type
!= SHT_REL
1886 && i_shdrp
->sh_type
!= SHT_RELA
)
1887 off
= assign_file_position_for_section (i_shdrp
, off
);
1890 elf_tdata (abfd
)->next_file_pos
= off
;
1897 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1898 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1899 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
1900 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
1901 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
1905 struct strtab
*shstrtab
;
1907 i_ehdrp
= elf_elfheader (abfd
);
1908 i_shdrp
= elf_elfsections (abfd
);
1910 shstrtab
= bfd_new_strtab (abfd
);
1911 elf_shstrtab (abfd
) = shstrtab
;
1913 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
1914 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
1915 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
1916 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
1918 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS
;
1919 i_ehdrp
->e_ident
[EI_DATA
] =
1920 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
1921 i_ehdrp
->e_ident
[EI_VERSION
] = EV_CURRENT
;
1923 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
1924 i_ehdrp
->e_ident
[count
] = 0;
1926 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
) ? ET_EXEC
: ET_REL
;
1927 switch (bfd_get_arch (abfd
))
1929 case bfd_arch_unknown
:
1930 i_ehdrp
->e_machine
= EM_NONE
;
1932 case bfd_arch_sparc
:
1933 i_ehdrp
->e_machine
= EM_SPARC
;
1934 /* start-sanitize-v9 */
1936 i_ehdrp
->e_machine
= EM_SPARC64
;
1938 /* end-sanitize-v9 */
1941 i_ehdrp
->e_machine
= EM_386
;
1944 i_ehdrp
->e_machine
= EM_68K
;
1947 i_ehdrp
->e_machine
= EM_88K
;
1950 i_ehdrp
->e_machine
= EM_860
;
1952 case bfd_arch_mips
: /* MIPS Rxxxx */
1953 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
1956 i_ehdrp
->e_machine
= EM_HPPA
;
1958 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
1960 i_ehdrp
->e_machine
= EM_NONE
;
1962 i_ehdrp
->e_version
= EV_CURRENT
;
1963 i_ehdrp
->e_ehsize
= sizeof (Elf_External_Ehdr
);
1965 /* no program header, for now. */
1966 i_ehdrp
->e_phoff
= 0;
1967 i_ehdrp
->e_phentsize
= 0;
1968 i_ehdrp
->e_phnum
= 0;
1970 /* each bfd section is section header entry */
1971 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
1972 i_ehdrp
->e_shentsize
= sizeof (Elf_External_Shdr
);
1974 /* if we're building an executable, we'll need a program header table */
1975 if (abfd
->flags
& EXEC_P
)
1977 /* it all happens later */
1979 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
1981 /* elf_build_phdrs() returns a (NULL-terminated) array of
1982 Elf_Internal_Phdrs */
1983 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
1984 i_ehdrp
->e_phoff
= outbase
;
1985 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
1990 i_ehdrp
->e_phentsize
= 0;
1992 i_ehdrp
->e_phoff
= 0;
1995 elf_tdata (abfd
)->symtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
1997 elf_tdata (abfd
)->strtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
1999 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2005 swap_out_syms (abfd
)
2008 struct strtab
*shstrtab
= elf_shstrtab (abfd
);
2010 elf_map_symbols (abfd
);
2012 /* Dump out the symtabs. */
2014 int symcount
= bfd_get_symcount (abfd
);
2015 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2016 struct strtab
*stt
= bfd_new_strtab (abfd
);
2017 Elf_Internal_Shdr
*symtab_hdr
;
2018 Elf_Internal_Shdr
*symstrtab_hdr
;
2019 Elf_External_Sym
*outbound_syms
;
2022 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2023 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2024 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
2025 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2026 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2028 /* see assert in elf_fake_sections that supports this: */
2029 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2030 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2032 outbound_syms
= (Elf_External_Sym
*)
2033 bfd_alloc (abfd
, (1 + symcount
) * sizeof (Elf_External_Sym
));
2034 /* now generate the data (for "contents") */
2036 /* Fill in zeroth symbol and swap it out. */
2037 Elf_Internal_Sym sym
;
2043 sym
.st_shndx
= SHN_UNDEF
;
2044 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
2046 for (idx
= 0; idx
< symcount
; idx
++)
2048 Elf_Internal_Sym sym
;
2049 bfd_vma value
= syms
[idx
]->value
;
2051 if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2052 /* Section symbols have no names. */
2055 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
2057 if (bfd_is_com_section (syms
[idx
]->section
))
2059 /* ELF common symbols put the alignment into the `value' field,
2060 and the size into the `size' field. This is backwards from
2061 how BFD handles it, so reverse it here. */
2062 sym
.st_size
= value
;
2063 /* Should retrieve this from somewhere... */
2065 sym
.st_shndx
= SHN_COMMON
;
2069 asection
*sec
= syms
[idx
]->section
;
2070 elf_symbol_type
*type_ptr
;
2073 if (sec
->output_section
)
2075 value
+= sec
->output_offset
;
2076 sec
= sec
->output_section
;
2079 sym
.st_value
= value
;
2080 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2081 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2082 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec
);
2086 /* Writing this would be a hell of a lot easier if we had
2087 some decent documentation on bfd, and knew what to expect
2088 of the library, and what to demand of applications. For
2089 example, it appears that `objcopy' might not set the
2090 section of a symbol to be a section that is actually in
2092 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2094 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec2
);
2095 assert (shndx
!= -1);
2099 if (bfd_is_com_section (syms
[idx
]->section
))
2100 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2101 else if (syms
[idx
]->section
== &bfd_und_section
)
2102 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2103 else if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2104 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2105 else if (syms
[idx
]->flags
& BSF_FILE
)
2106 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2109 int bind
= STB_LOCAL
;
2110 int type
= STT_OBJECT
;
2111 unsigned int flags
= syms
[idx
]->flags
;
2113 if (flags
& BSF_LOCAL
)
2115 else if (flags
& BSF_WEAK
)
2117 else if (flags
& BSF_GLOBAL
)
2120 if (flags
& BSF_FUNCTION
)
2123 sym
.st_info
= ELF_ST_INFO (bind
, type
);
2127 elf_swap_symbol_out (abfd
, &sym
,
2129 + elf_sym_extra (abfd
)[idx
].elf_sym_num
));
2132 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2133 symstrtab_hdr
->contents
= (PTR
) stt
->tab
;
2134 symstrtab_hdr
->sh_size
= stt
->length
;
2135 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2137 symstrtab_hdr
->sh_flags
= 0;
2138 symstrtab_hdr
->sh_addr
= 0;
2139 symstrtab_hdr
->sh_entsize
= 0;
2140 symstrtab_hdr
->sh_link
= 0;
2141 symstrtab_hdr
->sh_info
= 0;
2142 symstrtab_hdr
->sh_addralign
= 0;
2143 symstrtab_hdr
->size
= 0;
2146 /* put the strtab out too... */
2148 Elf_Internal_Shdr
*this_hdr
;
2150 this_hdr
= &elf_tdata(abfd
)->shstrtab_hdr
;
2151 this_hdr
->contents
= (PTR
) elf_shstrtab (abfd
)->tab
;
2152 this_hdr
->sh_size
= elf_shstrtab (abfd
)->length
;
2153 this_hdr
->sh_type
= SHT_STRTAB
;
2154 this_hdr
->sh_flags
= 0;
2155 this_hdr
->sh_addr
= 0;
2156 this_hdr
->sh_entsize
= 0;
2157 this_hdr
->sh_addralign
= 0;
2163 write_shdrs_and_ehdr (abfd
)
2166 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
2167 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2168 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2169 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
2170 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2174 struct strtab
*shstrtab
;
2176 i_ehdrp
= elf_elfheader (abfd
);
2177 i_shdrp
= elf_elfsections (abfd
);
2178 shstrtab
= elf_shstrtab (abfd
);
2180 /* swap the header before spitting it out... */
2183 elf_debug_file (i_ehdrp
);
2185 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
2186 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
2187 bfd_write ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
);
2189 /* at this point we've concocted all the ELF sections... */
2190 x_shdrp
= (Elf_External_Shdr
*)
2191 bfd_alloc (abfd
, sizeof (*x_shdrp
) * (i_ehdrp
->e_shnum
));
2194 bfd_error
= no_memory
;
2198 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2201 elf_debug_section (shstrtab
->tab
+ i_shdrp
[count
]->sh_name
, count
,
2204 elf_swap_shdr_out (abfd
, i_shdrp
[count
], x_shdrp
+ count
);
2206 bfd_seek (abfd
, (file_ptr
) i_ehdrp
->e_shoff
, SEEK_SET
);
2207 bfd_write ((PTR
) x_shdrp
, sizeof (*x_shdrp
), i_ehdrp
->e_shnum
, abfd
);
2208 /* need to dump the string table too... */
2214 assign_file_positions_for_relocs (abfd
)
2217 file_ptr off
= elf_tdata(abfd
)->next_file_pos
;
2219 Elf_Internal_Shdr
**shdrpp
= elf_elfsections (abfd
);
2220 Elf_Internal_Shdr
*shdrp
;
2221 for (i
= 0; i
< elf_elfheader(abfd
)->e_shnum
; i
++)
2224 if (shdrp
->sh_type
!= SHT_REL
&& shdrp
->sh_type
!= SHT_RELA
)
2226 off
= assign_file_position_for_section (shdrp
, off
);
2228 elf_tdata(abfd
)->next_file_pos
= off
;
2232 DEFUN (NAME(bfd_elf
,write_object_contents
), (abfd
), bfd
* abfd
)
2234 Elf_Internal_Ehdr
*i_ehdrp
;
2235 Elf_Internal_Shdr
**i_shdrp
;
2238 if (abfd
->output_has_begun
== false)
2240 prep_headers (abfd
);
2241 elf_compute_section_file_positions (abfd
);
2242 abfd
->output_has_begun
= true;
2245 i_shdrp
= elf_elfsections (abfd
);
2246 i_ehdrp
= elf_elfheader (abfd
);
2248 bfd_map_over_sections (abfd
, write_relocs
, (PTR
) 0);
2249 assign_file_positions_for_relocs (abfd
);
2251 /* After writing the headers, we need to write the sections too... */
2252 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2254 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2256 if (bed
->elf_backend_section_processing
)
2257 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2258 if (i_shdrp
[count
]->contents
)
2260 bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
);
2261 bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
, 1,
2265 return write_shdrs_and_ehdr (abfd
);
2268 /* Given an index of a section, retrieve a pointer to it. Note
2269 that for our purposes, sections are indexed by {1, 2, ...} with
2270 0 being an illegal index. */
2272 /* In the original, each ELF section went into exactly one BFD
2273 section. This doesn't really make sense, so we need a real mapping.
2274 The mapping has to hide in the Elf_Internal_Shdr since asection
2275 doesn't have anything like a tdata field... */
2278 DEFUN (section_from_elf_index
, (abfd
, index
),
2282 /* @@ Is bfd_com_section really correct in all the places it could
2283 be returned from this routine? */
2285 if (index
== SHN_ABS
)
2286 return &bfd_com_section
; /* not abs? */
2287 if (index
== SHN_COMMON
)
2288 return &bfd_com_section
;
2290 if (index
> elf_elfheader (abfd
)->e_shnum
)
2294 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[index
];
2296 switch (hdr
->sh_type
)
2298 /* ELF sections that map to BFD sections */
2302 bfd_section_from_shdr (abfd
, index
);
2303 return (struct sec
*) hdr
->rawdata
;
2306 return (struct sec
*) &bfd_abs_section
;
2311 /* given a section, search the header to find them... */
2313 DEFUN (elf_section_from_bfd_section
, (abfd
, asect
),
2317 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2319 Elf_Internal_Shdr
*hdr
;
2320 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2322 if (asect
== &bfd_abs_section
)
2324 if (asect
== &bfd_com_section
)
2326 if (asect
== &bfd_und_section
)
2329 for (index
= 0; index
< maxindex
; index
++)
2331 hdr
= i_shdrp
[index
];
2332 switch (hdr
->sh_type
)
2334 /* ELF sections that map to BFD sections */
2340 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2346 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2348 if (bed
->elf_backend_section_from_bfd_section
)
2349 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, hdr
, asect
))
2358 /* given a symbol, return the bfd index for that symbol. */
2360 DEFUN (elf_symbol_from_bfd_symbol
, (abfd
, asym_ptr_ptr
),
2362 struct symbol_cache_entry
**asym_ptr_ptr
)
2364 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2365 CONST
char *name
= asym_ptr
->name
;
2367 int symcount
= bfd_get_symcount (abfd
);
2368 flagword flags
= asym_ptr
->flags
;
2369 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2371 /* When gas creates relocations against local labels, it creates its
2372 own symbol for the section, but does put the symbol into the
2373 symbol chain, so udata is 0. When the linker is generating
2374 relocatable output, this section symbol may be for one of the
2375 input sections rather than the output section. */
2376 if (asym_ptr
->udata
== (PTR
) 0
2377 && (flags
& BSF_SECTION_SYM
)
2378 && asym_ptr
->section
)
2382 if (asym_ptr
->section
->output_section
!= NULL
)
2383 indx
= asym_ptr
->section
->output_section
->index
;
2385 indx
= asym_ptr
->section
->index
;
2386 if (elf_section_syms (abfd
)[indx
])
2387 asym_ptr
->udata
= elf_section_syms (abfd
)[indx
]->udata
;
2390 if (asym_ptr
->udata
)
2391 idx
= ((Elf_Sym_Extra
*)asym_ptr
->udata
)->elf_sym_num
;
2401 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
2402 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
, elf_symbol_flags (flags
));
2411 DEFUN (elf_slurp_symbol_table
, (abfd
, symptrs
),
2413 asymbol
** symptrs
) /* Buffer for generated bfd symbols */
2415 Elf_Internal_Shdr
*hdr
= &elf_tdata(abfd
)->symtab_hdr
;
2416 int symcount
; /* Number of external ELF symbols */
2418 elf_symbol_type
*sym
; /* Pointer to current bfd symbol */
2419 elf_symbol_type
*symbase
; /* Buffer for generated bfd symbols */
2420 Elf_Internal_Sym i_sym
;
2421 Elf_External_Sym
*x_symp
;
2423 /* this is only valid because there is only one symtab... */
2424 /* FIXME: This is incorrect, there may also be a dynamic symbol
2425 table which is a subset of the full symbol table. We either need
2426 to be prepared to read both (and merge them) or ensure that we
2427 only read the full symbol table. Currently we only get called to
2428 read the full symbol table. -fnf */
2429 if (bfd_get_outsymbols (abfd
) != NULL
)
2434 /* Read each raw ELF symbol, converting from external ELF form to
2435 internal ELF form, and then using the information to create a
2436 canonical bfd symbol table entry.
2438 Note that we allocate the initial bfd canonical symbol buffer
2439 based on a one-to-one mapping of the ELF symbols to canonical
2440 symbols. We actually use all the ELF symbols, so there will be no
2441 space left over at the end. When we have all the symbols, we
2442 build the caller's pointer vector. */
2444 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2446 bfd_error
= system_call_error
;
2450 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2451 symbase
= (elf_symbol_type
*) bfd_zalloc (abfd
, symcount
* sizeof (elf_symbol_type
));
2454 /* Temporarily allocate room for the raw ELF symbols. */
2455 x_symp
= (Elf_External_Sym
*) bfd_xmalloc (symcount
* sizeof (Elf_External_Sym
));
2457 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
2458 != symcount
* sizeof (Elf_External_Sym
))
2460 free ((PTR
) x_symp
);
2461 bfd_error
= system_call_error
;
2464 /* Skip first symbol, which is a null dummy. */
2465 for (i
= 1; i
< symcount
; i
++)
2467 elf_swap_symbol_in (abfd
, x_symp
+ i
, &i_sym
);
2468 memcpy (&sym
->internal_elf_sym
, &i_sym
, sizeof (Elf_Internal_Sym
));
2469 #ifdef ELF_KEEP_EXTSYM
2470 memcpy (&sym
->native_elf_sym
, x_symp
+ i
, sizeof (Elf_External_Sym
));
2472 sym
->symbol
.the_bfd
= abfd
;
2474 sym
->symbol
.name
= elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2477 sym
->symbol
.value
= i_sym
.st_value
;
2479 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
2481 sym
->symbol
.section
= section_from_elf_index (abfd
, i_sym
.st_shndx
);
2483 else if (i_sym
.st_shndx
== SHN_ABS
)
2485 sym
->symbol
.section
= &bfd_abs_section
;
2487 else if (i_sym
.st_shndx
== SHN_COMMON
)
2489 sym
->symbol
.section
= &bfd_com_section
;
2490 /* Elf puts the alignment into the `value' field, and the size
2491 into the `size' field. BFD wants to see the size in the
2492 value field, and doesn't care (at the moment) about the
2494 sym
->symbol
.value
= i_sym
.st_size
;
2496 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2498 sym
->symbol
.section
= &bfd_und_section
;
2501 sym
->symbol
.section
= &bfd_abs_section
;
2503 sym
->symbol
.value
-= sym
->symbol
.section
->vma
;
2505 switch (ELF_ST_BIND (i_sym
.st_info
))
2508 sym
->symbol
.flags
|= BSF_LOCAL
;
2511 sym
->symbol
.flags
|= BSF_GLOBAL
;
2514 sym
->symbol
.flags
|= BSF_WEAK
;
2518 switch (ELF_ST_TYPE (i_sym
.st_info
))
2521 sym
->symbol
.flags
|= BSF_SECTION_SYM
| BSF_DEBUGGING
;
2524 sym
->symbol
.flags
|= BSF_FILE
| BSF_DEBUGGING
;
2527 sym
->symbol
.flags
|= BSF_FUNCTION
;
2531 /* Do some backend-specific processing on this symbol. */
2533 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2534 if (ebd
->elf_backend_symbol_processing
)
2535 (*ebd
->elf_backend_symbol_processing
) (abfd
, &sym
->symbol
);
2541 /* Do some backend-specific processing on this symbol table. */
2543 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2544 if (ebd
->elf_backend_symbol_table_processing
)
2545 (*ebd
->elf_backend_symbol_table_processing
) (abfd
, symbase
, symcount
);
2548 /* We rely on the zalloc to clear out the final symbol entry. */
2550 bfd_get_symcount (abfd
) = symcount
= sym
- symbase
;
2552 /* Fill in the user's symbol pointer vector if needed. */
2556 while (symcount
-- > 0)
2558 *symptrs
++ = &sym
->symbol
;
2561 *symptrs
= 0; /* Final null pointer */
2567 /* Return the number of bytes required to hold the symtab vector.
2569 Note that we base it on the count plus 1, since we will null terminate
2570 the vector allocated based on this size. However, the ELF symbol table
2571 always has a dummy entry as symbol #0, so it ends up even. */
2574 DEFUN (elf_get_symtab_upper_bound
, (abfd
), bfd
* abfd
)
2576 unsigned int symcount
;
2577 unsigned int symtab_size
= 0;
2579 Elf_Internal_Shdr
*hdr
= &elf_tdata(abfd
)->symtab_hdr
;
2580 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2581 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
));
2587 This function return the number of bytes required to store the
2588 relocation information associated with section <<sect>>
2589 attached to bfd <<abfd>>
2593 elf_get_reloc_upper_bound (abfd
, asect
)
2597 if (asect
->flags
& SEC_RELOC
)
2599 /* either rel or rela */
2600 return elf_section_data(asect
)->rel_hdr
.sh_size
;
2607 DEFUN (elf_slurp_reloca_table
, (abfd
, asect
, symbols
),
2612 Elf_External_Rela
*native_relocs
;
2613 arelent
*reloc_cache
;
2618 if (asect
->relocation
)
2620 if (asect
->reloc_count
== 0)
2622 if (asect
->flags
& SEC_CONSTRUCTOR
)
2625 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2626 native_relocs
= (Elf_External_Rela
*)
2627 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rela
));
2628 bfd_read ((PTR
) native_relocs
,
2629 sizeof (Elf_External_Rela
), asect
->reloc_count
, abfd
);
2631 reloc_cache
= (arelent
*)
2632 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2636 bfd_error
= no_memory
;
2640 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2642 Elf_Internal_Rela dst
;
2643 Elf_External_Rela
*src
;
2645 cache_ptr
= reloc_cache
+ idx
;
2646 src
= native_relocs
+ idx
;
2647 elf_swap_reloca_in (abfd
, src
, &dst
);
2649 #ifdef RELOC_PROCESSING
2650 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2652 if (asect
->flags
& SEC_RELOC
)
2654 /* relocatable, so the offset is off of the section */
2655 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2659 /* non-relocatable, so the offset a virtual address */
2660 cache_ptr
->address
= dst
.r_offset
;
2662 /* ELF_R_SYM(dst.r_info) is the symbol table offset; subtract 1
2663 because the first entry is NULL. */
2664 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
2666 /* Is it an ELF section symbol? If so, translate it into a
2667 BFD section symbol. */
2668 asymbol
*s
= *(cache_ptr
->sym_ptr_ptr
);
2669 if (s
->flags
& BSF_SECTION_SYM
)
2671 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
2672 s
= *cache_ptr
->sym_ptr_ptr
;
2673 if (s
->name
== 0 || s
->name
[0] == 0)
2677 cache_ptr
->addend
= dst
.r_addend
;
2679 /* Fill in the cache_ptr->howto field from dst.r_type */
2681 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2682 (*ebd
->elf_info_to_howto
) (abfd
, cache_ptr
, &dst
);
2687 asect
->relocation
= reloc_cache
;
2693 elf_debug_section (str
, num
, hdr
)
2696 Elf_Internal_Shdr
*hdr
;
2698 fprintf (stderr
, "\nSection#%d '%s' 0x%.8lx\n", num
, str
, (long) hdr
);
2700 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
2701 (long) hdr
->sh_name
,
2702 (long) hdr
->sh_type
,
2703 (long) hdr
->sh_flags
);
2705 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
2706 (long) hdr
->sh_addr
,
2707 (long) hdr
->sh_offset
,
2708 (long) hdr
->sh_size
);
2710 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
2711 (long) hdr
->sh_link
,
2712 (long) hdr
->sh_info
,
2713 (long) hdr
->sh_addralign
);
2714 fprintf (stderr
, "sh_entsize = %ld\n",
2715 (long) hdr
->sh_entsize
);
2716 fprintf (stderr
, "rawdata = 0x%.8lx\n", (long) hdr
->rawdata
);
2717 fprintf (stderr
, "contents = 0x%.8lx\n", (long) hdr
->contents
);
2718 fprintf (stderr
, "size = %ld\n", (long) hdr
->size
);
2723 elf_debug_file (ehdrp
)
2724 Elf_Internal_Ehdr
*ehdrp
;
2726 fprintf (stderr
, "e_entry = 0x%.8lx\n", (long) ehdrp
->e_entry
);
2727 fprintf (stderr
, "e_phoff = %ld\n", (long) ehdrp
->e_phoff
);
2728 fprintf (stderr
, "e_phnum = %ld\n", (long) ehdrp
->e_phnum
);
2729 fprintf (stderr
, "e_phentsize = %ld\n", (long) ehdrp
->e_phentsize
);
2730 fprintf (stderr
, "e_shoff = %ld\n", (long) ehdrp
->e_shoff
);
2731 fprintf (stderr
, "e_shnum = %ld\n", (long) ehdrp
->e_shnum
);
2732 fprintf (stderr
, "e_shentsize = %ld\n", (long) ehdrp
->e_shentsize
);
2737 DEFUN (elf_slurp_reloc_table
, (abfd
, asect
, symbols
),
2742 Elf_External_Rel
*native_relocs
;
2743 arelent
*reloc_cache
;
2745 Elf_Internal_Shdr
*data_hdr
;
2746 ElfNAME (Off
) data_off
;
2747 ElfNAME (Word
) data_max
;
2748 char buf
[4]; /* FIXME -- might be elf64 */
2752 if (asect
->relocation
)
2754 if (asect
->reloc_count
== 0)
2756 if (asect
->flags
& SEC_CONSTRUCTOR
)
2759 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2760 native_relocs
= (Elf_External_Rel
*)
2761 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rel
));
2762 bfd_read ((PTR
) native_relocs
,
2763 sizeof (Elf_External_Rel
), asect
->reloc_count
, abfd
);
2765 reloc_cache
= (arelent
*)
2766 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2770 bfd_error
= no_memory
;
2774 /* Get the offset of the start of the segment we are relocating to read in
2775 the implicit addend. */
2776 data_hdr
= &elf_section_data(asect
)->this_hdr
;
2777 data_off
= data_hdr
->sh_offset
;
2778 data_max
= data_hdr
->sh_size
- sizeof (buf
) + 1;
2781 elf_debug_section ("data section", -1, data_hdr
);
2784 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2786 #ifdef RELOC_PROCESSING
2787 Elf_Internal_Rel dst
;
2788 Elf_External_Rel
*src
;
2790 cache_ptr
= reloc_cache
+ idx
;
2791 src
= native_relocs
+ idx
;
2792 elf_swap_reloc_in (abfd
, src
, &dst
);
2794 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2796 Elf_Internal_Rel dst
;
2797 Elf_External_Rel
*src
;
2799 cache_ptr
= reloc_cache
+ idx
;
2800 src
= native_relocs
+ idx
;
2802 elf_swap_reloc_in (abfd
, src
, &dst
);
2804 if (asect
->flags
& SEC_RELOC
)
2806 /* relocatable, so the offset is off of the section */
2807 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2811 /* non-relocatable, so the offset a virtual address */
2812 cache_ptr
->address
= dst
.r_offset
;
2814 /* ELF_R_SYM(dst.r_info) is the symbol table offset...
2815 -1 is to skip the dummy symbol table entry */
2816 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
2818 /* Is it an ELF section symbol? If so, translate it into a
2819 BFD section symbol. */
2820 asymbol
*s
= *(cache_ptr
->sym_ptr_ptr
);
2821 if (s
->flags
& BSF_SECTION_SYM
)
2823 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
2824 s
= *cache_ptr
->sym_ptr_ptr
;
2825 if (s
->name
== 0 || s
->name
[0] == 0)
2829 BFD_ASSERT (dst
.r_offset
<= data_max
);
2830 cache_ptr
->addend
= 0;
2832 /* Fill in the cache_ptr->howto field from dst.r_type */
2834 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2835 (*ebd
->elf_info_to_howto_rel
) (abfd
, cache_ptr
, &dst
);
2840 asect
->relocation
= reloc_cache
;
2845 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
2851 arelent
*tblptr
= section
->relocation
;
2852 unsigned int count
= 0;
2853 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
2855 /* snarfed from coffcode.h */
2857 elf_slurp_reloca_table (abfd
, section
, symbols
);
2859 elf_slurp_reloc_table (abfd
, section
, symbols
);
2861 tblptr
= section
->relocation
;
2865 for (; count
++ < section
->reloc_count
;)
2866 *relptr
++ = tblptr
++;
2869 return section
->reloc_count
;
2873 DEFUN (elf_get_symtab
, (abfd
, alocation
),
2875 asymbol
** alocation
)
2878 if (!elf_slurp_symbol_table (abfd
, alocation
))
2881 return bfd_get_symcount (abfd
);
2885 DEFUN (elf_make_empty_symbol
, (abfd
),
2888 elf_symbol_type
*newsym
;
2890 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
2893 bfd_error
= no_memory
;
2898 newsym
->symbol
.the_bfd
= abfd
;
2899 return &newsym
->symbol
;
2904 DEFUN (elf_get_symbol_info
, (ignore_abfd
, symbol
, ret
),
2905 bfd
* ignore_abfd AND
2906 asymbol
* symbol AND
2909 bfd_symbol_info (symbol
, ret
);
2913 DEFUN (elf_print_symbol
, (ignore_abfd
, filep
, symbol
, how
),
2914 bfd
* ignore_abfd AND
2916 asymbol
* symbol AND
2917 bfd_print_symbol_type how
)
2919 FILE *file
= (FILE *) filep
;
2922 case bfd_print_symbol_name
:
2923 fprintf (file
, "%s", symbol
->name
);
2925 case bfd_print_symbol_more
:
2926 fprintf (file
, "elf ");
2927 fprintf_vma (file
, symbol
->value
);
2928 fprintf (file
, " %lx", (long) symbol
->flags
);
2930 case bfd_print_symbol_all
:
2932 CONST
char *section_name
;
2933 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2934 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
2935 fprintf (file
, " %s\t%s",
2945 DEFUN (elf_get_lineno
, (ignore_abfd
, symbol
),
2946 bfd
* ignore_abfd AND
2949 fprintf (stderr
, "elf_get_lineno unimplemented\n");
2956 DEFUN (elf_set_arch_mach
, (abfd
, arch
, machine
),
2958 enum bfd_architecture arch AND
2959 unsigned long machine
)
2961 /* Allow any architecture to be supported by the elf backend */
2964 case bfd_arch_unknown
: /* EM_NONE */
2965 case bfd_arch_sparc
: /* EM_SPARC */
2966 case bfd_arch_i386
: /* EM_386 */
2967 case bfd_arch_m68k
: /* EM_68K */
2968 case bfd_arch_m88k
: /* EM_88K */
2969 case bfd_arch_i860
: /* EM_860 */
2970 case bfd_arch_mips
: /* EM_MIPS (MIPS R3000) */
2971 case bfd_arch_hppa
: /* EM_HPPA (HP PA_RISC) */
2972 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
2979 DEFUN (elf_find_nearest_line
, (abfd
,
2987 asection
* section AND
2988 asymbol
** symbols AND
2990 CONST
char **filename_ptr AND
2991 CONST
char **functionname_ptr AND
2992 unsigned int *line_ptr
)
2998 DEFUN (elf_sizeof_headers
, (abfd
, reloc
),
3002 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
3009 DEFUN (elf_set_section_contents
, (abfd
, section
, location
, offset
, count
),
3014 bfd_size_type count
)
3016 Elf_Internal_Shdr
*hdr
;
3018 if (abfd
->output_has_begun
== false) /* set by bfd.c handler? */
3020 /* do setup calculations (FIXME) */
3021 prep_headers (abfd
);
3022 elf_compute_section_file_positions (abfd
);
3023 abfd
->output_has_begun
= true;
3026 hdr
= &elf_section_data(section
)->this_hdr
;
3028 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
3030 if (bfd_write (location
, 1, count
, abfd
) != count
)
3037 DEFUN (elf_no_info_to_howto
, (abfd
, cache_ptr
, dst
),
3039 arelent
* cache_ptr AND
3040 Elf_Internal_Rela
* dst
)
3042 fprintf (stderr
, "elf RELA relocation support for target machine unimplemented\n");
3048 DEFUN (elf_no_info_to_howto_rel
, (abfd
, cache_ptr
, dst
),
3050 arelent
* cache_ptr AND
3051 Elf_Internal_Rel
* dst
)
3053 fprintf (stderr
, "elf REL relocation support for target machine unimplemented\n");
3059 /* Core file support */
3061 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3062 #include <sys/procfs.h>
3064 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
3065 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
3066 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
3072 DEFUN (bfd_prstatus
, (abfd
, descdata
, descsz
, filepos
),
3079 prstatus_t
*status
= (prstatus_t
*) 0;
3081 if (descsz
== sizeof (prstatus_t
))
3083 newsect
= bfd_make_section (abfd
, ".reg");
3084 newsect
->_raw_size
= sizeof (status
->pr_reg
);
3085 newsect
->filepos
= filepos
+ (long) &status
->pr_reg
;
3086 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3087 newsect
->alignment_power
= 2;
3088 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3090 memcpy (core_prstatus (abfd
), descdata
, descsz
);
3095 /* Stash a copy of the prpsinfo structure away for future use. */
3098 DEFUN (bfd_prpsinfo
, (abfd
, descdata
, descsz
, filepos
),
3106 if (descsz
== sizeof (prpsinfo_t
))
3108 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3110 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
3116 DEFUN (bfd_fpregset
, (abfd
, descdata
, descsz
, filepos
),
3124 newsect
= bfd_make_section (abfd
, ".reg2");
3125 newsect
->_raw_size
= descsz
;
3126 newsect
->filepos
= filepos
;
3127 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3128 newsect
->alignment_power
= 2;
3131 #endif /* HAVE_PROCFS */
3133 /* Return a pointer to the args (including the command name) that were
3134 seen by the program that generated the core dump. Note that for
3135 some reason, a spurious space is tacked onto the end of the args
3136 in some (at least one anyway) implementations, so strip it off if
3140 DEFUN (elf_core_file_failing_command
, (abfd
),
3144 if (core_prpsinfo (abfd
))
3146 prpsinfo_t
*p
= core_prpsinfo (abfd
);
3147 char *scan
= p
->pr_psargs
;
3152 if ((scan
> p
->pr_psargs
) && (*scan
== ' '))
3156 return p
->pr_psargs
;
3162 /* Return the number of the signal that caused the core dump. Presumably,
3163 since we have a core file, we got a signal of some kind, so don't bother
3164 checking the other process status fields, just return the signal number.
3168 DEFUN (elf_core_file_failing_signal
, (abfd
),
3172 if (core_prstatus (abfd
))
3174 return ((prstatus_t
*) (core_prstatus (abfd
)))->pr_cursig
;
3180 /* Check to see if the core file could reasonably be expected to have
3181 come for the current executable file. Note that by default we return
3182 true unless we find something that indicates that there might be a
3187 DEFUN (elf_core_file_matches_executable_p
, (core_bfd
, exec_bfd
),
3196 /* First, xvecs must match since both are ELF files for the same target. */
3198 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
3200 bfd_error
= system_call_error
;
3206 /* If no prpsinfo, just return true. Otherwise, grab the last component
3207 of the exec'd pathname from the prpsinfo. */
3209 if (core_prpsinfo (core_bfd
))
3211 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
))->pr_fname
);
3218 /* Find the last component of the executable pathname. */
3220 if ((execname
= strrchr (exec_bfd
->filename
, '/')) != NULL
)
3226 execname
= (char *) exec_bfd
->filename
;
3229 /* See if they match */
3231 return strcmp (execname
, corename
) ? false : true;
3237 #endif /* HAVE_PROCFS */
3240 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3241 the information that would normally be available from the /proc interface
3242 for the process, at the time the process dumped core. Currently this
3243 includes copies of the prstatus, prpsinfo, and fpregset structures.
3245 Since these structures are potentially machine dependent in size and
3246 ordering, bfd provides two levels of support for them. The first level,
3247 available on all machines since it does not require that the host
3248 have /proc support or the relevant include files, is to create a bfd
3249 section for each of the prstatus, prpsinfo, and fpregset structures,
3250 without any interpretation of their contents. With just this support,
3251 the bfd client will have to interpret the structures itself. Even with
3252 /proc support, it might want these full structures for it's own reasons.
3254 In the second level of support, where HAVE_PROCFS is defined, bfd will
3255 pick apart the structures to gather some additional information that
3256 clients may want, such as the general register set, the name of the
3257 exec'ed file and its arguments, the signal (if any) that caused the
3263 DEFUN (elf_corefile_note
, (abfd
, hdr
),
3265 Elf_Internal_Phdr
* hdr
)
3267 Elf_External_Note
*x_note_p
; /* Elf note, external form */
3268 Elf_Internal_Note i_note
; /* Elf note, internal form */
3269 char *buf
= NULL
; /* Entire note segment contents */
3270 char *namedata
; /* Name portion of the note */
3271 char *descdata
; /* Descriptor portion of the note */
3272 char *sectname
; /* Name to use for new section */
3273 long filepos
; /* File offset to descriptor data */
3276 if (hdr
->p_filesz
> 0
3277 && (buf
= (char *) bfd_xmalloc (hdr
->p_filesz
)) != NULL
3278 && bfd_seek (abfd
, hdr
->p_offset
, SEEK_SET
) != -1
3279 && bfd_read ((PTR
) buf
, hdr
->p_filesz
, 1, abfd
) == hdr
->p_filesz
)
3281 x_note_p
= (Elf_External_Note
*) buf
;
3282 while ((char *) x_note_p
< (buf
+ hdr
->p_filesz
))
3284 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->namesz
);
3285 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->descsz
);
3286 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->type
);
3287 namedata
= x_note_p
->name
;
3288 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
3289 filepos
= hdr
->p_offset
+ (descdata
- buf
);
3290 switch (i_note
.type
)
3293 /* process descdata as prstatus info */
3294 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
3295 sectname
= ".prstatus";
3298 /* process descdata as fpregset info */
3299 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
3300 sectname
= ".fpregset";
3303 /* process descdata as prpsinfo */
3304 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
3305 sectname
= ".prpsinfo";
3308 /* Unknown descriptor, just ignore it. */
3312 if (sectname
!= NULL
)
3314 newsect
= bfd_make_section (abfd
, sectname
);
3315 newsect
->_raw_size
= i_note
.descsz
;
3316 newsect
->filepos
= filepos
;
3317 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3318 newsect
->alignment_power
= 2;
3320 x_note_p
= (Elf_External_Note
*)
3321 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
3332 /* Core files are simply standard ELF formatted files that partition
3333 the file using the execution view of the file (program header table)
3334 rather than the linking view. In fact, there is no section header
3335 table in a core file.
3337 The process status information (including the contents of the general
3338 register set) and the floating point register set are stored in a
3339 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3340 that allow standard bfd access to the general registers (.reg) and the
3341 floating point registers (.reg2).
3346 DEFUN (elf_core_file_p
, (abfd
), bfd
* abfd
)
3348 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
3349 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3350 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
3351 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
3352 unsigned int phindex
;
3354 /* Read in the ELF header in external format. */
3356 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
3358 bfd_error
= system_call_error
;
3362 /* Now check to see if we have a valid ELF file, and one that BFD can
3363 make use of. The magic number must match, the address size ('class')
3364 and byte-swapping must match our XVEC entry, and it must have a
3365 program header table (FIXME: See comments re segments at top of this
3368 if (elf_file_p (&x_ehdr
) == false)
3371 bfd_error
= wrong_format
;
3375 /* FIXME, Check EI_VERSION here ! */
3379 int desired_address_size
= ELFCLASS32
;
3382 int desired_address_size
= ELFCLASS64
;
3385 if (x_ehdr
.e_ident
[EI_CLASS
] != desired_address_size
)
3389 /* Switch xvec to match the specified byte order. */
3390 switch (x_ehdr
.e_ident
[EI_DATA
])
3392 case ELFDATA2MSB
: /* Big-endian */
3393 if (abfd
->xvec
->byteorder_big_p
== false)
3396 case ELFDATA2LSB
: /* Little-endian */
3397 if (abfd
->xvec
->byteorder_big_p
== true)
3400 case ELFDATANONE
: /* No data encoding specified */
3401 default: /* Unknown data encoding specified */
3405 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3406 the tdata pointer in the bfd. */
3409 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
3410 if (elf_tdata (abfd
) == NULL
)
3412 bfd_error
= no_memory
;
3416 /* FIXME, `wrong' returns from this point onward, leak memory. */
3418 /* Now that we know the byte order, swap in the rest of the header */
3419 i_ehdrp
= elf_elfheader (abfd
);
3420 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
3422 elf_debug_file (i_ehdrp
);
3425 /* If there is no program header, or the type is not a core file, then
3427 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
3430 /* Allocate space for a copy of the program header table in
3431 internal form, seek to the program header table in the file,
3432 read it in, and convert it to internal form. As a simple sanity
3433 check, verify that the what BFD thinks is the size of each program
3434 header table entry actually matches the size recorded in the file. */
3436 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
3438 i_phdrp
= (Elf_Internal_Phdr
*)
3439 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
3442 bfd_error
= no_memory
;
3445 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
3447 bfd_error
= system_call_error
;
3450 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3452 if (bfd_read ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
3455 bfd_error
= system_call_error
;
3458 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
3461 /* Once all of the program headers have been read and converted, we
3462 can start processing them. */
3464 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3466 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
3467 if ((i_phdrp
+ phindex
)->p_type
== PT_NOTE
)
3469 elf_corefile_note (abfd
, i_phdrp
+ phindex
);
3473 /* Remember the entry point specified in the ELF file header. */
3475 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;