1 /* ELF executable support for BFD.
2 Copyright 1991, 1992, 1993, 1994 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
62 #include <string.h> /* For strrchr and friends */
69 /* Renaming structures, typedefs, macros and functions to be size-specific. */
70 #define Elf_External_Ehdr NAME(Elf,External_Ehdr)
71 #define Elf_External_Sym NAME(Elf,External_Sym)
72 #define Elf_External_Shdr NAME(Elf,External_Shdr)
73 #define Elf_External_Phdr NAME(Elf,External_Phdr)
74 #define Elf_External_Rel NAME(Elf,External_Rel)
75 #define Elf_External_Rela NAME(Elf,External_Rela)
77 #define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command)
78 #define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal)
79 #define elf_core_file_matches_executable_p \
80 NAME(bfd_elf,core_file_matches_executable_p)
81 #define elf_object_p NAME(bfd_elf,object_p)
82 #define elf_core_file_p NAME(bfd_elf,core_file_p)
83 #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
84 #define elf_get_dynamic_symtab_upper_bound \
85 NAME(bfd_elf,get_dynamic_symtab_upper_bound)
86 #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
87 #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
88 #define elf_get_symtab NAME(bfd_elf,get_symtab)
89 #define elf_canonicalize_dynamic_symtab \
90 NAME(bfd_elf,canonicalize_dynamic_symtab)
91 #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol)
92 #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info)
93 #define elf_print_symbol NAME(bfd_elf,print_symbol)
94 #define elf_get_lineno NAME(bfd_elf,get_lineno)
95 #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach)
96 #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line)
97 #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers)
98 #define elf_set_section_contents NAME(bfd_elf,set_section_contents)
99 #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto)
100 #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel)
101 #define elf_new_section_hook NAME(bfd_elf,new_section_hook)
102 #define write_relocs NAME(bfd_elf,_write_relocs)
103 #define elf_find_section NAME(bfd_elf,find_section)
104 #define elf_bfd_link_add_symbols NAME(bfd_elf,bfd_link_add_symbols)
105 #define elf_bfd_final_link NAME(bfd_elf,bfd_final_link)
108 #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
109 #define ELF_R_SYM(X) ELF64_R_SYM(X)
110 #define ELF_R_TYPE(X) ELF64_R_TYPE(X)
111 #define ELFCLASS ELFCLASS64
115 #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
116 #define ELF_R_SYM(X) ELF32_R_SYM(X)
117 #define ELF_R_TYPE(X) ELF32_R_TYPE(X)
118 #define ELFCLASS ELFCLASS32
122 /* Forward declarations of static functions */
124 static void elf_swap_reloc_in
125 PARAMS ((bfd
*, Elf_External_Rel
*, Elf_Internal_Rel
*));
126 static void elf_swap_reloc_out
127 PARAMS ((bfd
*, Elf_Internal_Rel
*, Elf_External_Rel
*));
128 static void elf_swap_reloca_in
129 PARAMS ((bfd
*, Elf_External_Rela
*, Elf_Internal_Rela
*));
130 static void elf_swap_reloca_out
131 PARAMS ((bfd
*, Elf_Internal_Rela
*, Elf_External_Rela
*));
133 static unsigned long bfd_add_to_strtab
134 PARAMS ((bfd
*, struct strtab
*, const char *));
135 static asection
*section_from_elf_index
PARAMS ((bfd
*, unsigned int));
137 static int elf_section_from_bfd_section
PARAMS ((bfd
*, struct sec
*));
139 static long elf_slurp_symbol_table
PARAMS ((bfd
*, asymbol
**, boolean
));
141 static int elf_symbol_from_bfd_symbol
PARAMS ((bfd
*,
142 struct symbol_cache_entry
**));
144 static boolean elf_compute_section_file_positions
145 PARAMS ((bfd
*, struct bfd_link_info
*));
146 static boolean prep_headers
PARAMS ((bfd
*));
147 static boolean assign_section_numbers
PARAMS ((bfd
*));
148 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*, boolean
));
149 static INLINE file_ptr assign_file_positions_for_symtab_and_strtabs
150 PARAMS ((bfd
*, file_ptr
, boolean
));
152 static boolean elf_map_symbols
PARAMS ((bfd
*));
153 static boolean swap_out_syms
PARAMS ((bfd
*));
155 static boolean bfd_section_from_shdr
PARAMS ((bfd
*, unsigned int shindex
));
158 static void elf_debug_section
PARAMS ((char *, int, Elf_Internal_Shdr
*));
159 static void elf_debug_file
PARAMS ((Elf_Internal_Ehdr
*));
162 #define elf_string_from_elf_strtab(abfd,strindex) \
163 elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
166 /* Structure swapping routines */
168 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
169 can be handled by explicitly specifying 32 bits or "the long type". */
171 #define put_word bfd_h_put_64
172 #define get_word bfd_h_get_64
175 #define put_word bfd_h_put_32
176 #define get_word bfd_h_get_32
179 /* Translate an ELF symbol in external format into an ELF symbol in internal
183 elf_swap_symbol_in (abfd
, src
, dst
)
185 Elf_External_Sym
*src
;
186 Elf_Internal_Sym
*dst
;
188 dst
->st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->st_name
);
189 dst
->st_value
= get_word (abfd
, (bfd_byte
*) src
->st_value
);
190 dst
->st_size
= get_word (abfd
, (bfd_byte
*) src
->st_size
);
191 dst
->st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_info
);
192 dst
->st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_other
);
193 dst
->st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->st_shndx
);
196 /* Translate an ELF symbol in internal format into an ELF symbol in external
200 elf_swap_symbol_out (abfd
, src
, dst
)
202 Elf_Internal_Sym
*src
;
203 Elf_External_Sym
*dst
;
205 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
206 put_word (abfd
, src
->st_value
, dst
->st_value
);
207 put_word (abfd
, src
->st_size
, dst
->st_size
);
208 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
209 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
210 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
214 /* Translate an ELF file header in external format into an ELF file header in
218 elf_swap_ehdr_in (abfd
, src
, dst
)
220 Elf_External_Ehdr
*src
;
221 Elf_Internal_Ehdr
*dst
;
223 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
224 dst
->e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_type
);
225 dst
->e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_machine
);
226 dst
->e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_version
);
227 dst
->e_entry
= get_word (abfd
, (bfd_byte
*) src
->e_entry
);
228 dst
->e_phoff
= get_word (abfd
, (bfd_byte
*) src
->e_phoff
);
229 dst
->e_shoff
= get_word (abfd
, (bfd_byte
*) src
->e_shoff
);
230 dst
->e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_flags
);
231 dst
->e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_ehsize
);
232 dst
->e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phentsize
);
233 dst
->e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phnum
);
234 dst
->e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shentsize
);
235 dst
->e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shnum
);
236 dst
->e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shstrndx
);
239 /* Translate an ELF file header in internal format into an ELF file header in
243 elf_swap_ehdr_out (abfd
, src
, dst
)
245 Elf_Internal_Ehdr
*src
;
246 Elf_External_Ehdr
*dst
;
248 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
249 /* note that all elements of dst are *arrays of unsigned char* already... */
250 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
251 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
252 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
253 put_word (abfd
, src
->e_entry
, dst
->e_entry
);
254 put_word (abfd
, src
->e_phoff
, dst
->e_phoff
);
255 put_word (abfd
, src
->e_shoff
, dst
->e_shoff
);
256 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
257 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
258 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
259 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
260 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
261 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
262 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
266 /* Translate an ELF section header table entry in external format into an
267 ELF section header table entry in internal format. */
270 elf_swap_shdr_in (abfd
, src
, dst
)
272 Elf_External_Shdr
*src
;
273 Elf_Internal_Shdr
*dst
;
275 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
276 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
277 dst
->sh_flags
= get_word (abfd
, (bfd_byte
*) src
->sh_flags
);
278 dst
->sh_addr
= get_word (abfd
, (bfd_byte
*) src
->sh_addr
);
279 dst
->sh_offset
= get_word (abfd
, (bfd_byte
*) src
->sh_offset
);
280 dst
->sh_size
= get_word (abfd
, (bfd_byte
*) src
->sh_size
);
281 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
282 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
283 dst
->sh_addralign
= get_word (abfd
, (bfd_byte
*) src
->sh_addralign
);
284 dst
->sh_entsize
= get_word (abfd
, (bfd_byte
*) src
->sh_entsize
);
285 /* we haven't done any processing on it yet, so... */
286 dst
->rawdata
= (void *) 0;
289 /* Translate an ELF section header table entry in internal format into an
290 ELF section header table entry in external format. */
293 elf_swap_shdr_out (abfd
, src
, dst
)
295 Elf_Internal_Shdr
*src
;
296 Elf_External_Shdr
*dst
;
298 /* note that all elements of dst are *arrays of unsigned char* already... */
299 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
300 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
301 put_word (abfd
, src
->sh_flags
, dst
->sh_flags
);
302 put_word (abfd
, src
->sh_addr
, dst
->sh_addr
);
303 put_word (abfd
, src
->sh_offset
, dst
->sh_offset
);
304 put_word (abfd
, src
->sh_size
, dst
->sh_size
);
305 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
306 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
307 put_word (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
308 put_word (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
312 /* Translate an ELF program header table entry in external format into an
313 ELF program header table entry in internal format. */
316 elf_swap_phdr_in (abfd
, src
, dst
)
318 Elf_External_Phdr
*src
;
319 Elf_Internal_Phdr
*dst
;
321 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
322 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
323 dst
->p_offset
= get_word (abfd
, (bfd_byte
*) src
->p_offset
);
324 dst
->p_vaddr
= get_word (abfd
, (bfd_byte
*) src
->p_vaddr
);
325 dst
->p_paddr
= get_word (abfd
, (bfd_byte
*) src
->p_paddr
);
326 dst
->p_filesz
= get_word (abfd
, (bfd_byte
*) src
->p_filesz
);
327 dst
->p_memsz
= get_word (abfd
, (bfd_byte
*) src
->p_memsz
);
328 dst
->p_align
= get_word (abfd
, (bfd_byte
*) src
->p_align
);
332 elf_swap_phdr_out (abfd
, src
, dst
)
334 Elf_Internal_Phdr
*src
;
335 Elf_External_Phdr
*dst
;
337 /* note that all elements of dst are *arrays of unsigned char* already... */
338 bfd_h_put_32 (abfd
, src
->p_type
, dst
->p_type
);
339 put_word (abfd
, src
->p_offset
, dst
->p_offset
);
340 put_word (abfd
, src
->p_vaddr
, dst
->p_vaddr
);
341 put_word (abfd
, src
->p_paddr
, dst
->p_paddr
);
342 put_word (abfd
, src
->p_filesz
, dst
->p_filesz
);
343 put_word (abfd
, src
->p_memsz
, dst
->p_memsz
);
344 bfd_h_put_32 (abfd
, src
->p_flags
, dst
->p_flags
);
345 put_word (abfd
, src
->p_align
, dst
->p_align
);
348 /* Translate an ELF reloc from external format to internal format. */
350 elf_swap_reloc_in (abfd
, src
, dst
)
352 Elf_External_Rel
*src
;
353 Elf_Internal_Rel
*dst
;
355 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
356 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
360 elf_swap_reloca_in (abfd
, src
, dst
)
362 Elf_External_Rela
*src
;
363 Elf_Internal_Rela
*dst
;
365 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
366 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
367 dst
->r_addend
= get_word (abfd
, (bfd_byte
*) src
->r_addend
);
370 /* Translate an ELF reloc from internal format to external format. */
372 elf_swap_reloc_out (abfd
, src
, dst
)
374 Elf_Internal_Rel
*src
;
375 Elf_External_Rel
*dst
;
377 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
378 put_word (abfd
, src
->r_info
, dst
->r_info
);
382 elf_swap_reloca_out (abfd
, src
, dst
)
384 Elf_Internal_Rela
*src
;
385 Elf_External_Rela
*dst
;
387 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
388 put_word (abfd
, src
->r_info
, dst
->r_info
);
389 put_word (abfd
, src
->r_addend
, dst
->r_addend
);
394 /* String table creation/manipulation routines */
396 static struct strtab
*
397 bfd_new_strtab (abfd
)
402 ss
= (struct strtab
*) malloc (sizeof (struct strtab
));
405 bfd_set_error (bfd_error_no_memory
);
408 ss
->tab
= malloc (1);
411 bfd_set_error (bfd_error_no_memory
);
422 bfd_add_to_strtab (abfd
, ss
, str
)
427 /* should search first, but for now: */
428 /* include the trailing NUL */
429 int ln
= strlen (str
) + 1;
431 /* FIXME: This is slow. Also, we could combine this with the a.out
432 string table building and use a hash table, although it might not
433 be worth it since ELF symbols don't include debugging information
434 and thus have much less overlap. */
435 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
438 bfd_set_error (bfd_error_no_memory
);
439 return (unsigned long) -1;
442 strcpy (ss
->tab
+ ss
->length
, str
);
446 return ss
->length
- ln
;
450 bfd_add_2_to_strtab (abfd
, ss
, str
, str2
)
456 /* should search first, but for now: */
457 /* include the trailing NUL */
458 int ln
= strlen (str
) + strlen (str2
) + 1;
460 /* should this be using obstacks? */
462 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
464 ss
->tab
= malloc (ln
);
466 BFD_ASSERT (ss
->tab
!= 0); /* FIXME */
467 strcpy (ss
->tab
+ ss
->length
, str
);
468 strcpy (ss
->tab
+ ss
->length
+ strlen (str
), str2
);
472 return ss
->length
- ln
;
476 /* ELF .o/exec file reading */
478 /* Create a new bfd section from an ELF section header. */
481 bfd_section_from_shdr (abfd
, shindex
)
483 unsigned int shindex
;
485 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
486 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
490 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
492 switch (hdr
->sh_type
)
496 /* inactive section. Throw it away. */
501 /* Bits that get saved. This one is real. */
502 if (hdr
->rawdata
== NULL
)
504 newsect
= bfd_make_section_anyway (abfd
, name
);
508 newsect
->filepos
= hdr
->sh_offset
;
509 newsect
->flags
|= SEC_HAS_CONTENTS
;
510 newsect
->vma
= hdr
->sh_addr
;
511 newsect
->_raw_size
= hdr
->sh_size
;
512 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
514 if (hdr
->sh_flags
& SHF_ALLOC
)
516 newsect
->flags
|= SEC_ALLOC
;
517 newsect
->flags
|= SEC_LOAD
;
520 if (!(hdr
->sh_flags
& SHF_WRITE
))
521 newsect
->flags
|= SEC_READONLY
;
523 if (hdr
->sh_flags
& SHF_EXECINSTR
)
524 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
525 else if (newsect
->flags
& SEC_ALLOC
)
526 newsect
->flags
|= SEC_DATA
;
528 /* The debugging sections appear to recognized only by name,
529 not any sort of flag. */
530 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
531 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
532 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
533 newsect
->flags
|= SEC_DEBUGGING
;
535 hdr
->rawdata
= (PTR
) newsect
;
540 /* Bits that get saved. This one is real. */
541 if (hdr
->rawdata
== NULL
)
543 newsect
= bfd_make_section_anyway (abfd
, name
);
547 newsect
->vma
= hdr
->sh_addr
;
548 newsect
->_raw_size
= hdr
->sh_size
;
549 newsect
->filepos
= hdr
->sh_offset
; /* fake */
550 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
551 if (hdr
->sh_flags
& SHF_ALLOC
)
552 newsect
->flags
|= SEC_ALLOC
;
554 if (!(hdr
->sh_flags
& SHF_WRITE
))
555 newsect
->flags
|= SEC_READONLY
;
557 /* FIXME: This section is empty. Does it really make sense
558 to set SEC_CODE for it? */
559 if (hdr
->sh_flags
& SHF_EXECINSTR
)
560 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
562 hdr
->rawdata
= (PTR
) newsect
;
566 case SHT_SYMTAB
: /* A symbol table */
567 if (elf_onesymtab (abfd
) == shindex
)
570 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
571 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
572 elf_onesymtab (abfd
) = shindex
;
573 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
574 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_hdr
;
575 abfd
->flags
|= HAS_SYMS
;
578 case SHT_DYNSYM
: /* A dynamic symbol table */
579 if (elf_dynsymtab (abfd
) == shindex
)
582 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
583 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
584 elf_dynsymtab (abfd
) = shindex
;
585 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
586 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->dynsymtab_hdr
;
587 abfd
->flags
|= HAS_SYMS
;
590 case SHT_STRTAB
: /* A string table */
593 if (ehdr
->e_shstrndx
== shindex
)
595 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
596 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
597 hdr
->rawdata
= (PTR
) & elf_tdata (abfd
)->shstrtab_hdr
;
603 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
605 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
606 if (hdr2
->sh_link
== shindex
)
608 if (! bfd_section_from_shdr (abfd
, i
))
610 if (elf_onesymtab (abfd
) == i
)
612 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
613 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
616 if (elf_dynsymtab (abfd
) == i
)
618 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
619 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->dynstrtab_hdr
;
622 #if 0 /* Not handling other string tables specially right now. */
623 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
624 /* We have a strtab for some random other section. */
625 newsect
= (asection
*) hdr2
->rawdata
;
628 hdr
->rawdata
= (PTR
) newsect
;
629 hdr2
= &elf_section_data (newsect
)->str_hdr
;
631 elf_elfsections (abfd
)[shindex
] = hdr2
;
637 newsect
= bfd_make_section_anyway (abfd
, name
);
641 newsect
->flags
= SEC_HAS_CONTENTS
;
642 hdr
->rawdata
= (PTR
) newsect
;
643 newsect
->_raw_size
= hdr
->sh_size
;
644 newsect
->alignment_power
= bfd_log2 (hdr
->sh_addralign
);
645 newsect
->vma
= hdr
->sh_addr
;
646 newsect
->filepos
= hdr
->sh_offset
;
648 if (hdr
->sh_flags
& SHF_ALLOC
)
649 newsect
->flags
|= SEC_ALLOC
| SEC_LOAD
;
650 if (!(hdr
->sh_flags
& SHF_WRITE
))
651 newsect
->flags
|= SEC_READONLY
;
652 if (hdr
->sh_flags
& SHF_EXECINSTR
)
653 newsect
->flags
|= SEC_CODE
;
654 else if (newsect
->flags
& SEC_ALLOC
)
655 newsect
->flags
|= SEC_DATA
;
657 /* Check for debugging string tables. */
658 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
659 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
660 newsect
->flags
|= SEC_DEBUGGING
;
666 /* *These* do a lot of work -- but build no sections!
667 The spec says there can be multiple strtabs, but only one symtab,
668 but there can be lots of REL* sections. */
669 /* FIXME: The above statement is wrong! There are typically at least
670 two symbol tables in a dynamically linked executable, ".dynsym"
671 which is the dynamic linkage symbol table and ".symtab", which is
672 the "traditional" symbol table. -fnf */
675 asection
*target_sect
;
676 Elf_Internal_Shdr
*hdr2
;
677 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
679 /* Don't allow REL relocations on a machine that uses RELA and
681 /* @@ Actually, the generic ABI does suggest that both might be
682 used in one file. But the four ABI Processor Supplements I
683 have access to right now all specify that only one is used on
684 each of those architectures. It's conceivable that, e.g., a
685 bunch of absolute 32-bit relocs might be more compact in REL
686 form even on a RELA machine... */
687 BFD_ASSERT (!(use_rela_p
&& (hdr
->sh_type
== SHT_REL
)));
688 BFD_ASSERT (!(!use_rela_p
&& (hdr
->sh_type
== SHT_RELA
)));
689 BFD_ASSERT (hdr
->sh_entsize
==
691 ? sizeof (Elf_External_Rela
)
692 : sizeof (Elf_External_Rel
)));
694 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
) /* target */
695 || ! bfd_section_from_shdr (abfd
, hdr
->sh_link
)) /* symbol table */
697 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
698 if (target_sect
== NULL
699 || elf_section_data (target_sect
) == NULL
)
702 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
704 elf_elfsections (abfd
)[shindex
] = hdr2
;
705 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
706 target_sect
->flags
|= SEC_RELOC
;
707 target_sect
->relocation
= 0;
708 target_sect
->rel_filepos
= hdr
->sh_offset
;
709 abfd
->flags
|= HAS_RELOC
;
716 fprintf (stderr
, "Dynamic Linking sections not yet supported.\n");
723 fprintf (stderr
, "Note Sections not yet supported.\n");
730 fprintf (stderr
, "SHLIB Sections not supported (and non conforming.)\n");
735 /* Check for any processor-specific section types. */
737 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
739 if (bed
->elf_backend_section_from_shdr
)
740 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
749 elf_new_section_hook (abfd
, sec
)
754 struct bfd_elf_section_data
*sdata
;
756 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
759 bfd_set_error (bfd_error_no_memory
);
762 sec
->used_by_bfd
= (PTR
) sdata
;
763 memset (sdata
, 0, sizeof (*sdata
));
767 /* Create a new bfd section from an ELF program header.
769 Since program segments have no names, we generate a synthetic name
770 of the form segment<NUM>, where NUM is generally the index in the
771 program header table. For segments that are split (see below) we
772 generate the names segment<NUM>a and segment<NUM>b.
774 Note that some program segments may have a file size that is different than
775 (less than) the memory size. All this means is that at execution the
776 system must allocate the amount of memory specified by the memory size,
777 but only initialize it with the first "file size" bytes read from the
778 file. This would occur for example, with program segments consisting
779 of combined data+bss.
781 To handle the above situation, this routine generates TWO bfd sections
782 for the single program segment. The first has the length specified by
783 the file size of the segment, and the second has the length specified
784 by the difference between the two sizes. In effect, the segment is split
785 into it's initialized and uninitialized parts.
790 bfd_section_from_phdr (abfd
, hdr
, index
)
792 Elf_Internal_Phdr
*hdr
;
800 split
= ((hdr
->p_memsz
> 0) &&
801 (hdr
->p_filesz
> 0) &&
802 (hdr
->p_memsz
> hdr
->p_filesz
));
803 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
804 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
807 bfd_set_error (bfd_error_no_memory
);
810 strcpy (name
, namebuf
);
811 newsect
= bfd_make_section (abfd
, name
);
814 newsect
->vma
= hdr
->p_vaddr
;
815 newsect
->_raw_size
= hdr
->p_filesz
;
816 newsect
->filepos
= hdr
->p_offset
;
817 newsect
->flags
|= SEC_HAS_CONTENTS
;
818 if (hdr
->p_type
== PT_LOAD
)
820 newsect
->flags
|= SEC_ALLOC
;
821 newsect
->flags
|= SEC_LOAD
;
822 if (hdr
->p_flags
& PF_X
)
824 /* FIXME: all we known is that it has execute PERMISSION,
826 newsect
->flags
|= SEC_CODE
;
829 if (!(hdr
->p_flags
& PF_W
))
831 newsect
->flags
|= SEC_READONLY
;
836 sprintf (namebuf
, "segment%db", index
);
837 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
840 bfd_set_error (bfd_error_no_memory
);
843 strcpy (name
, namebuf
);
844 newsect
= bfd_make_section (abfd
, name
);
847 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
848 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
849 if (hdr
->p_type
== PT_LOAD
)
851 newsect
->flags
|= SEC_ALLOC
;
852 if (hdr
->p_flags
& PF_X
)
853 newsect
->flags
|= SEC_CODE
;
855 if (!(hdr
->p_flags
& PF_W
))
856 newsect
->flags
|= SEC_READONLY
;
862 /* Begin processing a given object.
864 First we validate the file by reading in the ELF header and checking
867 static INLINE boolean
869 Elf_External_Ehdr
*x_ehdrp
;
871 return ((x_ehdrp
->e_ident
[EI_MAG0
] == ELFMAG0
)
872 && (x_ehdrp
->e_ident
[EI_MAG1
] == ELFMAG1
)
873 && (x_ehdrp
->e_ident
[EI_MAG2
] == ELFMAG2
)
874 && (x_ehdrp
->e_ident
[EI_MAG3
] == ELFMAG3
));
877 /* Check to see if the file associated with ABFD matches the target vector
880 Note that we may be called several times with the same ABFD, but different
881 target vectors, most of which will not match. We have to avoid leaving
882 any side effects in ABFD, or any data it points to (like tdata), if the
883 file does not match the target vector. */
889 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
890 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
891 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
892 Elf_Internal_Shdr
*i_shdrp
= NULL
; /* Section header table, internal form */
893 unsigned int shindex
;
894 char *shstrtab
; /* Internal copy of section header stringtab */
895 struct elf_backend_data
*ebd
;
896 struct elf_obj_tdata
*preserved_tdata
= elf_tdata (abfd
);
897 struct elf_obj_tdata
*new_tdata
= NULL
;
899 /* Read in the ELF header in external format. */
901 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
903 if (bfd_get_error () != bfd_error_system_call
)
904 goto got_wrong_format_error
;
909 /* Now check to see if we have a valid ELF file, and one that BFD can
910 make use of. The magic number must match, the address size ('class')
911 and byte-swapping must match our XVEC entry, and it must have a
912 section header table (FIXME: See comments re sections at top of this
915 if ((elf_file_p (&x_ehdr
) == false) ||
916 (x_ehdr
.e_ident
[EI_VERSION
] != EV_CURRENT
) ||
917 (x_ehdr
.e_ident
[EI_CLASS
] != ELFCLASS
))
918 goto got_wrong_format_error
;
920 /* Check that file's byte order matches xvec's */
921 switch (x_ehdr
.e_ident
[EI_DATA
])
923 case ELFDATA2MSB
: /* Big-endian */
924 if (!abfd
->xvec
->header_byteorder_big_p
)
925 goto got_wrong_format_error
;
927 case ELFDATA2LSB
: /* Little-endian */
928 if (abfd
->xvec
->header_byteorder_big_p
)
929 goto got_wrong_format_error
;
931 case ELFDATANONE
: /* No data encoding specified */
932 default: /* Unknown data encoding specified */
933 goto got_wrong_format_error
;
936 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
937 the tdata pointer in the bfd. */
939 new_tdata
= ((struct elf_obj_tdata
*)
940 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
)));
941 if (new_tdata
== NULL
)
942 goto got_no_memory_error
;
943 elf_tdata (abfd
) = new_tdata
;
945 /* Now that we know the byte order, swap in the rest of the header */
946 i_ehdrp
= elf_elfheader (abfd
);
947 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
949 elf_debug_file (i_ehdrp
);
952 /* If there is no section header table, we're hosed. */
953 if (i_ehdrp
->e_shoff
== 0)
954 goto got_wrong_format_error
;
956 /* As a simple sanity check, verify that the what BFD thinks is the
957 size of each section header table entry actually matches the size
958 recorded in the file. */
959 if (i_ehdrp
->e_shentsize
!= sizeof (x_shdr
))
960 goto got_wrong_format_error
;
962 ebd
= get_elf_backend_data (abfd
);
964 /* Check that the ELF e_machine field matches what this particular
965 BFD format expects. */
966 if (ebd
->elf_machine_code
!= i_ehdrp
->e_machine
)
968 bfd_target
**target_ptr
;
970 if (ebd
->elf_machine_code
!= EM_NONE
)
971 goto got_wrong_format_error
;
973 /* This is the generic ELF target. Let it match any ELF target
974 for which we do not have a specific backend. */
975 for (target_ptr
= bfd_target_vector
; *target_ptr
!= NULL
; target_ptr
++)
977 struct elf_backend_data
*back
;
979 if ((*target_ptr
)->flavour
!= bfd_target_elf_flavour
)
981 back
= (struct elf_backend_data
*) (*target_ptr
)->backend_data
;
982 if (back
->elf_machine_code
== i_ehdrp
->e_machine
)
984 /* target_ptr is an ELF backend which matches this
985 object file, so reject the generic ELF target. */
986 goto got_wrong_format_error
;
991 /* Set the flags and architecture before calling the backend so that
992 it can override them. */
993 if (i_ehdrp
->e_type
== ET_EXEC
)
994 abfd
->flags
|= EXEC_P
;
995 else if (i_ehdrp
->e_type
== ET_DYN
)
996 abfd
->flags
|= DYNAMIC
;
998 if (! bfd_default_set_arch_mach (abfd
, ebd
->arch
, 0))
1001 /* Remember the entry point specified in the ELF file header. */
1002 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
1004 /* Let the backend double check the format and override global
1006 if (ebd
->elf_backend_object_p
)
1008 if ((*ebd
->elf_backend_object_p
) (abfd
) == false)
1009 goto got_wrong_format_error
;
1012 /* Allocate space for a copy of the section header table in
1013 internal form, seek to the section header table in the file,
1014 read it in, and convert it to internal form. */
1015 i_shdrp
= ((Elf_Internal_Shdr
*)
1016 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
));
1017 elf_elfsections (abfd
) = ((Elf_Internal_Shdr
**)
1019 sizeof (i_shdrp
) * i_ehdrp
->e_shnum
));
1020 if (!i_shdrp
|| !elf_elfsections (abfd
))
1021 goto got_no_memory_error
;
1022 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) != 0)
1024 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1026 if (bfd_read ((PTR
) & x_shdr
, sizeof x_shdr
, 1, abfd
) != sizeof (x_shdr
))
1028 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
1029 elf_elfsections (abfd
)[shindex
] = i_shdrp
+ shindex
;
1031 /* If this is a .dynamic section, mark the object file as being
1032 dynamically linked. */
1033 if (i_shdrp
[shindex
].sh_type
== SHT_DYNAMIC
)
1034 abfd
->flags
|= DYNAMIC
;
1036 if (i_ehdrp
->e_shstrndx
)
1038 if (! bfd_section_from_shdr (abfd
, i_ehdrp
->e_shstrndx
))
1042 /* Read in the string table containing the names of the sections. We
1043 will need the base pointer to this table later. */
1044 /* We read this inline now, so that we don't have to go through
1045 bfd_section_from_shdr with it (since this particular strtab is
1046 used to find all of the ELF section names.) */
1048 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
1052 /* Once all of the section headers have been read and converted, we
1053 can start processing them. Note that the first section header is
1054 a dummy placeholder entry, so we ignore it. */
1056 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1058 if (! bfd_section_from_shdr (abfd
, shindex
))
1062 return (abfd
->xvec
);
1064 got_wrong_format_error
:
1065 bfd_set_error (bfd_error_wrong_format
);
1067 got_no_memory_error
:
1068 bfd_set_error (bfd_error_no_memory
);
1071 if (new_tdata
!= NULL
1072 && new_tdata
->elf_sect_ptr
!= NULL
)
1073 bfd_release (abfd
, new_tdata
->elf_sect_ptr
);
1074 if (i_shdrp
!= NULL
)
1075 bfd_release (abfd
, i_shdrp
);
1076 if (new_tdata
!= NULL
)
1077 bfd_release (abfd
, new_tdata
);
1078 elf_tdata (abfd
) = preserved_tdata
;
1083 /* ELF .o/exec file writing */
1085 /* Takes a bfd and a symbol, returns a pointer to the elf specific area
1086 of the symbol if there is one. */
1087 static INLINE elf_symbol_type
*
1088 elf_symbol_from (ignore_abfd
, symbol
)
1092 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
1095 if (symbol
->the_bfd
->tdata
.elf_obj_data
== (struct elf_obj_tdata
*) NULL
)
1098 return (elf_symbol_type
*) symbol
;
1101 /* Create ELF output from BFD sections.
1103 Essentially, just create the section header and forget about the program
1107 elf_make_sections (abfd
, asect
, obj
)
1112 /* most of what is in bfd_shdr_from_section goes in here... */
1113 /* and all of these sections generate at *least* one ELF section. */
1114 Elf_Internal_Shdr
*this_hdr
;
1115 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1117 this_hdr
->sh_addr
= asect
->vma
;
1118 this_hdr
->sh_size
= asect
->_raw_size
;
1119 /* contents already set by elf_set_section_contents */
1121 if (asect
->flags
& SEC_RELOC
)
1123 /* emit a reloc section, and thus strtab and symtab... */
1124 Elf_Internal_Shdr
*rela_hdr
;
1125 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1127 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1129 /* orelocation has the data, reloc_count has the count... */
1132 rela_hdr
->sh_type
= SHT_RELA
;
1133 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1136 /* REL relocations */
1138 rela_hdr
->sh_type
= SHT_REL
;
1139 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rel
);
1141 rela_hdr
->sh_flags
= 0;
1142 rela_hdr
->sh_addr
= 0;
1143 rela_hdr
->sh_offset
= 0;
1145 /* FIXME: Systems I've checked use an alignment of 4, but it is
1146 possible that some systems use a different alignment. */
1147 rela_hdr
->sh_addralign
= 4;
1151 if (asect
->flags
& SEC_ALLOC
)
1153 this_hdr
->sh_flags
|= SHF_ALLOC
;
1154 if (asect
->flags
& SEC_LOAD
)
1156 /* @@ Do something with sh_type? */
1161 /* If this section is not part of the program image during
1162 execution, leave the address fields at 0. */
1163 this_hdr
->sh_addr
= 0;
1166 if (!(asect
->flags
& SEC_READONLY
))
1167 this_hdr
->sh_flags
|= SHF_WRITE
;
1169 if (asect
->flags
& SEC_CODE
)
1170 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1174 write_relocs (abfd
, sec
, xxx
)
1179 Elf_Internal_Shdr
*rela_hdr
;
1180 Elf_External_Rela
*outbound_relocas
;
1181 Elf_External_Rel
*outbound_relocs
;
1183 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1184 asymbol
*last_sym
= 0;
1185 int last_sym_idx
= 9999999; /* should always be written before use */
1187 if ((sec
->flags
& SEC_RELOC
) == 0)
1190 /* The linker backend writes the relocs out itself, and sets the
1191 reloc_count field to zero to inhibit writing them here. Also,
1192 sometimes the SEC_RELOC flag gets set even when there aren't any
1194 if (sec
->reloc_count
== 0)
1197 rela_hdr
= &elf_section_data (sec
)->rel_hdr
;
1199 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* sec
->reloc_count
;
1200 rela_hdr
->contents
= (void *) bfd_alloc (abfd
, rela_hdr
->sh_size
);
1201 if (!rela_hdr
->contents
)
1203 bfd_set_error (bfd_error_no_memory
);
1204 abort (); /* FIXME */
1207 /* orelocation has the data, reloc_count has the count... */
1210 outbound_relocas
= (Elf_External_Rela
*) rela_hdr
->contents
;
1212 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1214 Elf_Internal_Rela dst_rela
;
1215 Elf_External_Rela
*src_rela
;
1220 ptr
= sec
->orelocation
[idx
];
1221 src_rela
= outbound_relocas
+ idx
;
1222 if (!(abfd
->flags
& EXEC_P
))
1223 dst_rela
.r_offset
= ptr
->address
- sec
->vma
;
1225 dst_rela
.r_offset
= ptr
->address
;
1227 sym
= *ptr
->sym_ptr_ptr
;
1228 if (sym
== last_sym
)
1233 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1235 dst_rela
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1237 dst_rela
.r_addend
= ptr
->addend
;
1238 elf_swap_reloca_out (abfd
, &dst_rela
, src_rela
);
1242 /* REL relocations */
1244 outbound_relocs
= (Elf_External_Rel
*) rela_hdr
->contents
;
1246 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1248 Elf_Internal_Rel dst_rel
;
1249 Elf_External_Rel
*src_rel
;
1254 ptr
= sec
->orelocation
[idx
];
1255 sym
= *ptr
->sym_ptr_ptr
;
1256 src_rel
= outbound_relocs
+ idx
;
1257 if (!(abfd
->flags
& EXEC_P
))
1258 dst_rel
.r_offset
= ptr
->address
- sec
->vma
;
1260 dst_rel
.r_offset
= ptr
->address
;
1262 if (sym
== last_sym
)
1267 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1269 dst_rel
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1271 elf_swap_reloc_out (abfd
, &dst_rel
, src_rel
);
1277 fix_up_strtabs (abfd
, asect
, obj
)
1282 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (asect
)->this_hdr
;
1283 int this_idx
= elf_section_data (asect
)->this_idx
;
1285 /* @@ Check flags! */
1286 if (!strncmp (asect
->name
, ".stab", 5)
1287 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
1289 size_t len
= strlen (asect
->name
) + 1;
1290 char *s
= (char *) malloc (len
);
1292 /* FIXME: Should deal more gracefully with errors. */
1294 strcpy (s
, asect
->name
);
1296 asect
= bfd_get_section_by_name (abfd
, s
);
1300 elf_section_data (asect
)->this_hdr
.sh_link
= this_idx
;
1301 /* @@ Assuming 32 bits! */
1302 elf_section_data (asect
)->this_hdr
.sh_entsize
= 0xc;
1304 this_hdr
->sh_type
= SHT_STRTAB
;
1309 elf_fake_sections (abfd
, asect
, obj
)
1314 /* most of what is in bfd_shdr_from_section goes in here... */
1315 /* and all of these sections generate at *least* one ELF section. */
1317 Elf_Internal_Shdr
*this_hdr
;
1318 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1320 bfd_add_to_strtab (abfd
, elf_shstrtab (abfd
), asect
->name
);
1321 if (this_hdr
->sh_name
== (unsigned long) -1)
1322 abort (); /* FIXME */
1323 /* We need to log the type *now* so that elf_section_from_bfd_section
1324 can find us... have to set rawdata too. */
1325 this_hdr
->rawdata
= (void *) asect
;
1326 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1327 if ((asect
->flags
& SEC_ALLOC
) && (asect
->flags
& SEC_LOAD
))
1328 this_hdr
->sh_type
= SHT_PROGBITS
;
1329 else if ((asect
->flags
& SEC_ALLOC
) && ((asect
->flags
& SEC_LOAD
) == 0))
1331 BFD_ASSERT (strcmp (asect
->name
, ".bss") == 0
1332 || strcmp (asect
->name
, ".sbss") == 0);
1333 this_hdr
->sh_type
= SHT_NOBITS
;
1335 /* FIXME I am not sure how to detect a .note section from the flags
1336 word of an `asection'. */
1337 else if (!strcmp (asect
->name
, ".note"))
1338 this_hdr
->sh_type
= SHT_NOTE
;
1340 this_hdr
->sh_type
= SHT_PROGBITS
;
1342 this_hdr
->sh_flags
= 0;
1343 this_hdr
->sh_addr
= 0;
1344 this_hdr
->sh_size
= 0;
1345 this_hdr
->sh_entsize
= 0;
1346 this_hdr
->sh_info
= 0;
1347 this_hdr
->sh_link
= 0;
1348 this_hdr
->sh_offset
= 0;
1351 /* Now, check for processor-specific section types. */
1353 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1355 if (bed
->elf_backend_fake_sections
)
1356 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1360 /* Emit a strtab and symtab, and possibly a reloc section. */
1361 Elf_Internal_Shdr
*rela_hdr
;
1363 /* Note that only one symtab is used, so just remember it
1366 if (asect
->flags
& SEC_RELOC
)
1368 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1370 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1372 bfd_add_2_to_strtab (abfd
, elf_shstrtab (abfd
),
1373 use_rela_p
? ".rela" : ".rel",
1375 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1376 rela_hdr
->sh_entsize
= (use_rela_p
1377 ? sizeof (Elf_External_Rela
)
1378 : sizeof (Elf_External_Rel
));
1380 rela_hdr
->sh_flags
= 0;
1381 rela_hdr
->sh_addr
= 0;
1382 rela_hdr
->sh_size
= 0;
1383 rela_hdr
->sh_offset
= 0;
1385 /* FIXME: Systems I've checked use an alignment of 4, but some
1386 systems may use a different alignment. */
1387 rela_hdr
->sh_addralign
= 4;
1392 if (asect
->flags
& SEC_ALLOC
)
1394 this_hdr
->sh_flags
|= SHF_ALLOC
;
1395 if (asect
->flags
& SEC_LOAD
)
1397 /* @@ Do something with sh_type? */
1400 if (!(asect
->flags
& SEC_READONLY
))
1401 this_hdr
->sh_flags
|= SHF_WRITE
;
1402 if (asect
->flags
& SEC_CODE
)
1403 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1406 /* Map symbol from it's internal number to the external number, moving
1407 all local symbols to be at the head of the list. */
1410 sym_is_global (abfd
, sym
)
1414 /* If the backend has a special mapping, use it. */
1415 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1416 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1419 if (sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
))
1421 if (sym
->flags
& BSF_LOCAL
)
1425 if (sym
->section
== 0)
1427 /* Is this valid? */
1432 if (sym
->section
== &bfd_und_section
)
1434 if (bfd_is_com_section (sym
->section
))
1436 if (sym
->flags
& (BSF_LOCAL
| BSF_SECTION_SYM
| BSF_FILE
))
1442 elf_map_symbols (abfd
)
1445 int symcount
= bfd_get_symcount (abfd
);
1446 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1447 asymbol
**sect_syms
;
1449 int num_globals
= 0;
1450 int num_locals2
= 0;
1451 int num_globals2
= 0;
1453 int num_sections
= 0;
1454 Elf_Sym_Extra
*sym_extra
;
1459 fprintf (stderr
, "elf_map_symbols\n");
1463 /* Add local symbols for each section for which there are relocs.
1464 FIXME: How can we tell which sections have relocs at this point?
1465 Will reloc_count always be accurate? Actually, I think most ELF
1466 targets create section symbols for all sections anyhow. */
1467 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1469 if (max_index
< asect
->index
)
1470 max_index
= asect
->index
;
1474 elf_num_section_syms (abfd
) = max_index
;
1475 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1476 elf_section_syms (abfd
) = sect_syms
;
1480 bfd_set_error (bfd_error_no_memory
);
1484 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1486 asymbol
*sym
= bfd_make_empty_symbol (abfd
);
1489 bfd_set_error (bfd_error_no_memory
);
1492 sym
->the_bfd
= abfd
;
1493 sym
->name
= asect
->name
;
1494 sym
->value
= asect
->vma
;
1495 sym
->flags
= BSF_SECTION_SYM
;
1496 sym
->section
= asect
;
1497 sect_syms
[asect
->index
] = sym
;
1501 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1502 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1509 syms
= (asymbol
**) bfd_realloc (abfd
, syms
,
1510 ((symcount
+ num_sections
+ 1)
1511 * sizeof (asymbol
*)));
1513 syms
= (asymbol
**) bfd_alloc (abfd
,
1514 (num_sections
+ 1) * sizeof (asymbol
*));
1517 bfd_set_error (bfd_error_no_memory
);
1521 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1523 if (sect_syms
[asect
->index
])
1524 syms
[symcount
++] = sect_syms
[asect
->index
];
1527 syms
[symcount
] = (asymbol
*) 0;
1528 bfd_set_symtab (abfd
, syms
, symcount
);
1531 elf_sym_extra (abfd
) = sym_extra
1532 = (Elf_Sym_Extra
*) bfd_alloc (abfd
, symcount
* sizeof (Elf_Sym_Extra
));
1535 bfd_set_error (bfd_error_no_memory
);
1539 /* Identify and classify all of the symbols. */
1540 for (idx
= 0; idx
< symcount
; idx
++)
1542 if (!sym_is_global (abfd
, syms
[idx
]))
1548 /* Now provide mapping information. Add +1 for skipping over the
1550 for (idx
= 0; idx
< symcount
; idx
++)
1552 syms
[idx
]->udata
= (PTR
) & sym_extra
[idx
];
1553 if (!sym_is_global (abfd
, syms
[idx
]))
1554 sym_extra
[idx
].elf_sym_num
= 1 + num_locals2
++;
1556 sym_extra
[idx
].elf_sym_num
= 1 + num_locals
+ num_globals2
++;
1559 elf_num_locals (abfd
) = num_locals
;
1560 elf_num_globals (abfd
) = num_globals
;
1564 /* Compute the file positions we are going to put the sections at, and
1565 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1566 is not NULL, this is being called by the ELF backend linker. */
1569 elf_compute_section_file_positions (abfd
, link_info
)
1571 struct bfd_link_info
*link_info
;
1573 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1574 Elf_Internal_Shdr
*shstrtab_hdr
;
1576 if (abfd
->output_has_begun
)
1579 /* Do any elf backend specific processing first. */
1580 if (bed
->elf_backend_begin_write_processing
)
1581 (*bed
->elf_backend_begin_write_processing
) (abfd
);
1583 if (! prep_headers (abfd
))
1586 bfd_map_over_sections (abfd
, elf_fake_sections
, 0);
1588 if (!assign_section_numbers (abfd
))
1591 bfd_map_over_sections (abfd
, elf_make_sections
, 0);
1593 bfd_map_over_sections (abfd
, fix_up_strtabs
, 0); /* .stab/.stabstr &c */
1595 /* The backend linker builds symbol table information itself. */
1596 if (link_info
== NULL
)
1598 if (! swap_out_syms (abfd
))
1602 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
1603 /* sh_name was set in prep_headers. */
1604 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
1605 shstrtab_hdr
->sh_flags
= 0;
1606 shstrtab_hdr
->sh_addr
= 0;
1607 shstrtab_hdr
->sh_size
= elf_shstrtab (abfd
)->length
;
1608 shstrtab_hdr
->sh_entsize
= 0;
1609 shstrtab_hdr
->sh_link
= 0;
1610 shstrtab_hdr
->sh_info
= 0;
1611 /* sh_offset is set in assign_file_positions_for_symtabs_and_strtabs. */
1612 shstrtab_hdr
->sh_addralign
= 1;
1613 shstrtab_hdr
->contents
= (PTR
) elf_shstrtab (abfd
)->tab
;
1615 if (!assign_file_positions_except_relocs (abfd
,
1616 link_info
== NULL
? true : false))
1619 abfd
->output_has_begun
= true;
1625 elf_write_phdrs (abfd
, i_ehdrp
, i_phdrp
, phdr_cnt
)
1627 Elf_Internal_Ehdr
*i_ehdrp
;
1628 Elf_Internal_Phdr
*i_phdrp
;
1629 unsigned short phdr_cnt
;
1631 /* first program header entry goes after the file header */
1632 int outbase
= i_ehdrp
->e_phoff
;
1634 Elf_External_Phdr x_phdr
;
1636 for (i
= 0; i
< phdr_cnt
; i
++)
1638 elf_swap_phdr_out (abfd
, i_phdrp
+ i
, &x_phdr
);
1639 if (bfd_seek (abfd
, outbase
, SEEK_SET
) != 0
1640 || (bfd_write ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
1641 != sizeof (x_phdr
)))
1643 outbase
+= sizeof (x_phdr
);
1649 /* Assign all ELF section numbers. The dummy first section is handled here
1650 too. The link/info pointers for the standard section types are filled
1651 in here too, while we're at it. (Link pointers for .stab sections are
1652 not filled in here.) */
1654 assign_section_numbers (abfd
)
1657 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1659 int section_number
= 1;
1661 Elf_Internal_Shdr
**i_shdrp
;
1663 t
->shstrtab_hdr
.sh_size
= elf_shstrtab (abfd
)->length
;
1664 t
->shstrtab_hdr
.contents
= (void *) elf_shstrtab (abfd
)->tab
;
1666 t
->shstrtab_section
= section_number
++;
1667 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1670 t
->symtab_section
= section_number
++;
1671 t
->strtab_section
= section_number
++;
1672 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1674 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1676 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1677 d
->this_idx
= section_number
++;
1678 if (sec
->flags
& SEC_RELOC
)
1680 d
->rel_idx
= section_number
++;
1681 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1682 d
->rel_hdr
.sh_info
= d
->this_idx
;
1686 /* No handling for per-section string tables currently. */
1688 elf_elfheader (abfd
)->e_shnum
= section_number
;
1690 /* Set up the list of section header pointers, in agreement with the
1692 i_shdrp
= ((Elf_Internal_Shdr
**)
1693 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1694 if (i_shdrp
== NULL
)
1696 bfd_set_error (bfd_error_no_memory
);
1699 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1700 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1701 if (i_shdrp
[0] == NULL
)
1703 bfd_release (abfd
, i_shdrp
);
1704 bfd_set_error (bfd_error_no_memory
);
1707 elf_elfsections (abfd
) = i_shdrp
;
1708 for (i
= 1; i
< section_number
; i
++)
1710 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1712 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1715 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1716 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1718 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1720 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1721 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1723 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1725 /* Make sure we got everything.... */
1726 for (i
= 0; i
< section_number
; i
++)
1727 if (i_shdrp
[i
] == NULL
)
1732 static INLINE file_ptr
1733 assign_file_position_for_section (i_shdrp
, offset
)
1734 Elf_Internal_Shdr
*i_shdrp
;
1739 if (i_shdrp
->sh_addralign
!= 0)
1740 align
= i_shdrp
->sh_addralign
;
1743 i_shdrp
->sh_offset
= offset
= BFD_ALIGN (offset
, align
);
1744 if (i_shdrp
->rawdata
!= NULL
)
1745 ((asection
*) i_shdrp
->rawdata
)->filepos
= offset
;
1746 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1747 offset
+= i_shdrp
->sh_size
;
1751 static INLINE file_ptr
1752 align_file_position (off
)
1755 return (off
+ FILE_ALIGN
- 1) & ~(FILE_ALIGN
- 1);
1758 static INLINE file_ptr
1759 assign_file_positions_for_symtab_and_strtabs (abfd
, off
, dosyms
)
1764 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1766 off
= align_file_position (off
);
1768 off
= assign_file_position_for_section (&t
->symtab_hdr
, off
);
1769 off
= assign_file_position_for_section (&t
->shstrtab_hdr
, off
);
1771 off
= assign_file_position_for_section (&t
->strtab_hdr
, off
);
1777 bfd_vma low
, mem_size
;
1781 struct seg_info
*next
;
1785 map_program_segments (abfd
)
1788 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1789 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1790 Elf_Internal_Shdr
*i_shdrp
;
1791 Elf_Internal_Phdr
*phdr
;
1793 unsigned int i
, n_left
= 0;
1794 file_ptr lowest_offset
= 0;
1795 struct seg_info
*seg
= NULL
;
1797 done
= (char *) malloc (i_ehdrp
->e_shnum
);
1798 if (done
== NULL
&& i_ehdrp
->e_shnum
!= 0)
1800 bfd_set_error (bfd_error_no_memory
);
1803 memset (done
, 0, i_ehdrp
->e_shnum
);
1804 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1806 i_shdrp
= i_shdrpp
[i
];
1807 /* If it's going to be mapped in, it's been assigned a position. */
1808 if (i_shdrp
->sh_offset
+ 1 == 0)
1810 /* Well, not really, but we won't process it here. */
1814 if (i_shdrp
->sh_offset
< lowest_offset
1815 || lowest_offset
== 0)
1816 lowest_offset
= i_shdrp
->sh_offset
;
1817 /* Only interested in PROGBITS or NOBITS for generating segments. */
1818 switch (i_shdrp
->sh_type
)
1831 bfd_vma lowest_vma
= -1, high
;
1835 struct seg_info
*snew
;
1836 struct seg_info
**s_ptr
;
1838 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1840 i_shdrp
= i_shdrpp
[i
];
1841 if (!done
[i
] && i_shdrp
->sh_addr
< lowest_vma
)
1843 lowest_vma
= i_shdrp
->sh_addr
;
1849 /* So now we know the lowest vma of any unassigned sections; start
1851 snew
= (struct seg_info
*) bfd_alloc (abfd
, sizeof (struct seg_info
));
1854 bfd_set_error (bfd_error_no_memory
);
1858 while (*s_ptr
!= (struct seg_info
*) NULL
)
1859 s_ptr
= &(*s_ptr
)->next
;
1862 snew
->low
= lowest_vma
;
1863 i_shdrp
= i_shdrpp
[low_sec
];
1864 snew
->start_pos
= i_shdrp
->sh_offset
;
1865 snew
->sh_flags
= i_shdrp
->sh_flags
;
1866 done
[low_sec
] = 1, n_left
--;
1867 mem_size
= i_shdrp
->sh_size
;
1868 high
= lowest_vma
+ i_shdrp
->sh_size
;
1870 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1871 file_size
= i_shdrp
->sh_size
;
1873 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
1879 i_shdrp
= i_shdrpp
[i
];
1880 /* position of next byte on disk */
1881 f1
= snew
->start_pos
+ file_size
;
1882 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1884 if (i_shdrp
->sh_offset
- f1
!= i_shdrp
->sh_addr
- high
)
1886 if (file_size
!= mem_size
)
1890 /* sh_type == NOBITS */
1892 /* If the section in question has no contents in the disk
1893 file, we really don't care where it supposedly starts.
1894 But we don't want to bother merging it into this segment
1895 if it doesn't start on this memory page. */
1896 bfd_vma page1
, page2
;
1897 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1899 /* page number in address space of current end of snew */
1900 page1
= (high
- 1 + maxpagesize
- 1) / maxpagesize
;
1901 /* page number in address space of start of this section */
1902 page2
= (i_shdrp
->sh_addr
+ maxpagesize
- 1) / maxpagesize
;
1907 done
[i
] = 1, n_left
--;
1908 if (i_shdrp
->sh_type
== SHT_PROGBITS
)
1909 file_size
= i_shdrp
->sh_offset
+ i_shdrp
->sh_size
- snew
->start_pos
;
1910 mem_size
= i_shdrp
->sh_addr
+ i_shdrp
->sh_size
- snew
->low
;
1911 high
= i_shdrp
->sh_addr
+ i_shdrp
->sh_size
;
1914 snew
->file_size
= file_size
;
1915 snew
->mem_size
= mem_size
;
1917 /* Now do something with the list of segments we've built up. */
1919 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1924 for (s
= seg
; s
; s
= s
->next
)
1928 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
1929 sz
= sizeof (Elf_External_Phdr
) * n_segs
;
1930 if (align_file_position (i_ehdrp
->e_ehsize
) + sz
<= lowest_offset
)
1931 i_ehdrp
->e_phoff
= align_file_position (i_ehdrp
->e_ehsize
);
1934 i_ehdrp
->e_phoff
= align_file_position (elf_tdata (abfd
)->next_file_pos
);
1935 elf_tdata (abfd
)->next_file_pos
= i_ehdrp
->e_phoff
+ sz
;
1937 phdr
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
,
1938 n_segs
* sizeof (Elf_Internal_Phdr
));
1941 bfd_set_error (bfd_error_no_memory
);
1942 abort (); /* FIXME */
1944 elf_tdata (abfd
)->phdr
= phdr
;
1947 phdr
->p_type
= PT_LOAD
; /* only type we really support so far */
1948 phdr
->p_offset
= seg
->start_pos
;
1949 phdr
->p_vaddr
= seg
->low
;
1951 phdr
->p_filesz
= seg
->file_size
;
1952 phdr
->p_memsz
= seg
->mem_size
;
1953 phdr
->p_flags
= PF_R
;
1954 phdr
->p_align
= maxpagesize
; /* ? */
1955 if (seg
->sh_flags
& SHF_WRITE
)
1956 /* SysVr4 ELF docs say "data segments normally have read, write,
1957 and execute permissions." */
1958 phdr
->p_flags
|= (PF_W
| PF_X
);
1959 if (seg
->sh_flags
& SHF_EXECINSTR
)
1960 phdr
->p_flags
|= PF_X
;
1964 i_ehdrp
->e_phnum
= n_segs
;
1966 elf_write_phdrs (abfd
, i_ehdrp
, elf_tdata (abfd
)->phdr
, i_ehdrp
->e_phnum
);
1977 assign_file_positions_except_relocs (abfd
, dosyms
)
1981 /* For now, we ignore the possibility of having program segments, which
1982 may require some alignment in the file. That'll require padding, and
1983 some interesting calculations to optimize file space usage.
1985 Also, since the application may change the list of relocations for
1986 a given section, we don't figure them in here. We'll put them at the
1987 end of the file, at positions computed during bfd_close.
1989 The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ...
1990 or: <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */
1992 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1995 Elf_Internal_Shdr
**i_shdrpp
= elf_elfsections (abfd
);
1996 Elf_Internal_Shdr
*i_shdrp
;
1997 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1998 int exec_p
= (abfd
->flags
& EXEC_P
) != 0;
1999 bfd_vma maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2001 /* Everything starts after the ELF file header. */
2002 off
= i_ehdrp
->e_ehsize
;
2006 /* Section headers. */
2007 off
= align_file_position (off
);
2008 i_ehdrp
->e_shoff
= off
;
2009 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
2010 off
= assign_file_positions_for_symtab_and_strtabs (abfd
, off
, dosyms
);
2012 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
2014 /* The symtab and strtab sections are placed by
2015 assign_file_positions_for_symtab_and_strtabs. */
2016 if (i
== t
->symtab_section
2017 || i
== t
->strtab_section
2018 || i
== t
->shstrtab_section
)
2021 i_shdrp
= i_shdrpp
[i
];
2022 if (i_shdrp
->sh_type
== SHT_REL
|| i_shdrp
->sh_type
== SHT_RELA
)
2024 i_shdrp
->sh_offset
= -1;
2029 if (maxpagesize
== 0)
2030 maxpagesize
= 1; /* make the arithmetic work */
2031 /* This isn't necessarily going to give the best packing, if the
2032 segments require padding between them, but since that isn't
2033 usually the case, this'll do. */
2034 if ((i_shdrp
->sh_flags
& SHF_ALLOC
) == 0)
2036 i_shdrp
->sh_offset
= -1;
2039 /* Blindly assume that the segments are ordered optimally. With
2040 the default LD script, they will be. */
2041 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2043 /* need big unsigned type */
2045 addtl_off
= i_shdrp
->sh_addr
- off
;
2046 addtl_off
= addtl_off
% maxpagesize
;
2053 off
= assign_file_position_for_section (i_shdrp
, off
);
2056 && i_shdrp
->sh_type
== SHT_NOBITS
2057 && (i
== i_ehdrp
->e_shnum
2058 || i_shdrpp
[i
+ 1]->sh_type
!= SHT_NOBITS
))
2060 /* Skip to the next page to ensure that when the file is
2061 loaded the bss section is loaded with zeroes. I don't
2062 know if this is required on all platforms, but it
2063 shouldn't really hurt. */
2064 off
= BFD_ALIGN (off
, maxpagesize
);
2068 && (abfd
->flags
& D_PAGED
) != 0
2069 && get_elf_backend_data (abfd
)->maxpagesize
> 1
2070 && i_shdrp
->sh_type
== SHT_PROGBITS
2071 && (i_shdrp
->sh_flags
& SHF_ALLOC
)
2072 && (i_shdrp
->sh_offset
- i_shdrp
->sh_addr
) % get_elf_backend_data (abfd
)->maxpagesize
!= 0)
2077 elf_tdata (abfd
)->next_file_pos
= off
;
2078 if (!map_program_segments (abfd
))
2080 off
= elf_tdata (abfd
)->next_file_pos
;
2082 /* Section headers. */
2083 off
= align_file_position (off
);
2084 i_ehdrp
->e_shoff
= off
;
2085 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
2087 off
= assign_file_positions_for_symtab_and_strtabs (abfd
, off
, dosyms
);
2089 for (i
= 1; i
< i_ehdrp
->e_shnum
; i
++)
2091 i_shdrp
= i_shdrpp
[i
];
2092 if (i_shdrp
->sh_offset
+ 1 == 0
2093 && i_shdrp
->sh_type
!= SHT_REL
2094 && i_shdrp
->sh_type
!= SHT_RELA
)
2095 off
= assign_file_position_for_section (i_shdrp
, off
);
2098 elf_tdata (abfd
)->next_file_pos
= off
;
2106 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2107 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2108 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2110 struct strtab
*shstrtab
;
2112 i_ehdrp
= elf_elfheader (abfd
);
2113 i_shdrp
= elf_elfsections (abfd
);
2115 shstrtab
= bfd_new_strtab (abfd
);
2119 elf_shstrtab (abfd
) = shstrtab
;
2121 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
2122 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
2123 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
2124 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
2126 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS
;
2127 i_ehdrp
->e_ident
[EI_DATA
] =
2128 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
2129 i_ehdrp
->e_ident
[EI_VERSION
] = EV_CURRENT
;
2131 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2132 i_ehdrp
->e_ident
[count
] = 0;
2134 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
) ? ET_EXEC
: ET_REL
;
2135 switch (bfd_get_arch (abfd
))
2137 case bfd_arch_unknown
:
2138 i_ehdrp
->e_machine
= EM_NONE
;
2140 case bfd_arch_sparc
:
2141 i_ehdrp
->e_machine
= EM_SPARC
;
2142 /* start-sanitize-v9 */
2144 i_ehdrp
->e_machine
= EM_SPARC64
;
2146 /* end-sanitize-v9 */
2149 i_ehdrp
->e_machine
= EM_386
;
2152 i_ehdrp
->e_machine
= EM_68K
;
2155 i_ehdrp
->e_machine
= EM_88K
;
2158 i_ehdrp
->e_machine
= EM_860
;
2160 case bfd_arch_mips
: /* MIPS Rxxxx */
2161 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
2164 i_ehdrp
->e_machine
= EM_HPPA
;
2166 case bfd_arch_powerpc
:
2167 i_ehdrp
->e_machine
= EM_CYGNUS_POWERPC
;
2169 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2171 i_ehdrp
->e_machine
= EM_NONE
;
2173 i_ehdrp
->e_version
= EV_CURRENT
;
2174 i_ehdrp
->e_ehsize
= sizeof (Elf_External_Ehdr
);
2176 /* no program header, for now. */
2177 i_ehdrp
->e_phoff
= 0;
2178 i_ehdrp
->e_phentsize
= 0;
2179 i_ehdrp
->e_phnum
= 0;
2181 /* each bfd section is section header entry */
2182 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
2183 i_ehdrp
->e_shentsize
= sizeof (Elf_External_Shdr
);
2185 /* if we're building an executable, we'll need a program header table */
2186 if (abfd
->flags
& EXEC_P
)
2188 /* it all happens later */
2190 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
2192 /* elf_build_phdrs() returns a (NULL-terminated) array of
2193 Elf_Internal_Phdrs */
2194 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
2195 i_ehdrp
->e_phoff
= outbase
;
2196 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
2201 i_ehdrp
->e_phentsize
= 0;
2203 i_ehdrp
->e_phoff
= 0;
2206 elf_tdata (abfd
)->symtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2208 elf_tdata (abfd
)->strtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2210 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2212 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2213 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2214 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
2221 swap_out_syms (abfd
)
2224 if (!elf_map_symbols (abfd
))
2227 /* Dump out the symtabs. */
2229 int symcount
= bfd_get_symcount (abfd
);
2230 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2231 struct strtab
*stt
= bfd_new_strtab (abfd
);
2232 Elf_Internal_Shdr
*symtab_hdr
;
2233 Elf_Internal_Shdr
*symstrtab_hdr
;
2234 Elf_External_Sym
*outbound_syms
;
2239 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2240 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2241 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
2242 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2243 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2245 /* FIXME: Systems I've checked use 4 byte alignment for .symtab,
2246 but it is possible that there are systems which use a different
2248 symtab_hdr
->sh_addralign
= 4;
2250 /* see assert in elf_fake_sections that supports this: */
2251 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2252 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2254 outbound_syms
= (Elf_External_Sym
*)
2255 bfd_alloc (abfd
, (1 + symcount
) * sizeof (Elf_External_Sym
));
2258 bfd_set_error (bfd_error_no_memory
);
2261 /* now generate the data (for "contents") */
2263 /* Fill in zeroth symbol and swap it out. */
2264 Elf_Internal_Sym sym
;
2270 sym
.st_shndx
= SHN_UNDEF
;
2271 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
2273 for (idx
= 0; idx
< symcount
; idx
++)
2275 Elf_Internal_Sym sym
;
2276 bfd_vma value
= syms
[idx
]->value
;
2278 if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2279 /* Section symbols have no names. */
2283 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
2284 if (sym
.st_name
== (unsigned long) -1)
2288 if (bfd_is_com_section (syms
[idx
]->section
))
2290 /* ELF common symbols put the alignment into the `value' field,
2291 and the size into the `size' field. This is backwards from
2292 how BFD handles it, so reverse it here. */
2293 sym
.st_size
= value
;
2294 /* Should retrieve this from somewhere... */
2296 sym
.st_shndx
= elf_section_from_bfd_section (abfd
,
2297 syms
[idx
]->section
);
2301 asection
*sec
= syms
[idx
]->section
;
2302 elf_symbol_type
*type_ptr
;
2305 if (sec
->output_section
)
2307 value
+= sec
->output_offset
;
2308 sec
= sec
->output_section
;
2311 sym
.st_value
= value
;
2312 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2313 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2314 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec
);
2318 /* Writing this would be a hell of a lot easier if we had
2319 some decent documentation on bfd, and knew what to expect
2320 of the library, and what to demand of applications. For
2321 example, it appears that `objcopy' might not set the
2322 section of a symbol to be a section that is actually in
2324 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2325 BFD_ASSERT (sec2
!= 0);
2326 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec2
);
2327 BFD_ASSERT (shndx
!= -1);
2331 if (bfd_is_com_section (syms
[idx
]->section
))
2332 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2333 else if (syms
[idx
]->section
== &bfd_und_section
)
2334 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
2335 else if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2336 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2337 else if (syms
[idx
]->flags
& BSF_FILE
)
2338 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2341 int bind
= STB_LOCAL
;
2342 int type
= STT_OBJECT
;
2343 unsigned int flags
= syms
[idx
]->flags
;
2345 if (flags
& BSF_LOCAL
)
2347 else if (flags
& BSF_WEAK
)
2349 else if (flags
& BSF_GLOBAL
)
2352 if (flags
& BSF_FUNCTION
)
2355 sym
.st_info
= ELF_ST_INFO (bind
, type
);
2359 elf_swap_symbol_out (abfd
, &sym
,
2361 + elf_sym_extra (abfd
)[idx
].elf_sym_num
));
2364 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2365 symstrtab_hdr
->contents
= (PTR
) stt
->tab
;
2366 symstrtab_hdr
->sh_size
= stt
->length
;
2367 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2369 symstrtab_hdr
->sh_flags
= 0;
2370 symstrtab_hdr
->sh_addr
= 0;
2371 symstrtab_hdr
->sh_entsize
= 0;
2372 symstrtab_hdr
->sh_link
= 0;
2373 symstrtab_hdr
->sh_info
= 0;
2374 symstrtab_hdr
->sh_addralign
= 1;
2375 symstrtab_hdr
->size
= 0;
2382 write_shdrs_and_ehdr (abfd
)
2385 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
2386 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2387 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
2388 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2390 struct strtab
*shstrtab
;
2392 i_ehdrp
= elf_elfheader (abfd
);
2393 i_shdrp
= elf_elfsections (abfd
);
2394 shstrtab
= elf_shstrtab (abfd
);
2396 /* swap the header before spitting it out... */
2399 elf_debug_file (i_ehdrp
);
2401 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
2402 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0
2403 || (bfd_write ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
)
2404 != sizeof (x_ehdr
)))
2407 /* at this point we've concocted all the ELF sections... */
2408 x_shdrp
= (Elf_External_Shdr
*)
2409 bfd_alloc (abfd
, sizeof (*x_shdrp
) * (i_ehdrp
->e_shnum
));
2412 bfd_set_error (bfd_error_no_memory
);
2416 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2419 elf_debug_section (shstrtab
->tab
+ i_shdrp
[count
]->sh_name
, count
,
2422 elf_swap_shdr_out (abfd
, i_shdrp
[count
], x_shdrp
+ count
);
2424 if (bfd_seek (abfd
, (file_ptr
) i_ehdrp
->e_shoff
, SEEK_SET
) != 0
2425 || (bfd_write ((PTR
) x_shdrp
, sizeof (*x_shdrp
), i_ehdrp
->e_shnum
, abfd
)
2426 != sizeof (*x_shdrp
) * i_ehdrp
->e_shnum
))
2429 /* need to dump the string table too... */
2435 assign_file_positions_for_relocs (abfd
)
2438 file_ptr off
= elf_tdata (abfd
)->next_file_pos
;
2440 Elf_Internal_Shdr
**shdrpp
= elf_elfsections (abfd
);
2441 Elf_Internal_Shdr
*shdrp
;
2442 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
2445 if (shdrp
->sh_type
!= SHT_REL
&& shdrp
->sh_type
!= SHT_RELA
)
2447 off
= align_file_position (off
);
2448 off
= assign_file_position_for_section (shdrp
, off
);
2450 elf_tdata (abfd
)->next_file_pos
= off
;
2454 NAME(bfd_elf
,write_object_contents
) (abfd
)
2457 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2458 Elf_Internal_Ehdr
*i_ehdrp
;
2459 Elf_Internal_Shdr
**i_shdrp
;
2462 /* We don't know how to write dynamic objects. Specifically, we
2463 don't know how to construct the program header. */
2464 if ((abfd
->flags
& DYNAMIC
) != 0)
2466 fprintf (stderr
, "Writing ELF dynamic objects is not supported\n");
2467 bfd_set_error (bfd_error_wrong_format
);
2471 if (! abfd
->output_has_begun
2472 && ! elf_compute_section_file_positions (abfd
,
2473 (struct bfd_link_info
*) NULL
))
2476 i_shdrp
= elf_elfsections (abfd
);
2477 i_ehdrp
= elf_elfheader (abfd
);
2479 bfd_map_over_sections (abfd
, write_relocs
, (PTR
) 0);
2480 assign_file_positions_for_relocs (abfd
);
2482 /* After writing the headers, we need to write the sections too... */
2483 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2485 if (bed
->elf_backend_section_processing
)
2486 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2487 if (i_shdrp
[count
]->contents
)
2489 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
2490 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
2492 != i_shdrp
[count
]->sh_size
))
2497 if (bed
->elf_backend_final_write_processing
)
2498 (*bed
->elf_backend_final_write_processing
) (abfd
);
2500 return write_shdrs_and_ehdr (abfd
);
2503 /* Given an index of a section, retrieve a pointer to it. Note
2504 that for our purposes, sections are indexed by {1, 2, ...} with
2505 0 being an illegal index. */
2507 /* In the original, each ELF section went into exactly one BFD
2508 section. This doesn't really make sense, so we need a real mapping.
2509 The mapping has to hide in the Elf_Internal_Shdr since asection
2510 doesn't have anything like a tdata field... */
2513 section_from_elf_index (abfd
, index
)
2517 /* @@ Is bfd_com_section really correct in all the places it could
2518 be returned from this routine? */
2520 if (index
== SHN_ABS
)
2521 return &bfd_com_section
; /* not abs? */
2522 if (index
== SHN_COMMON
)
2523 return &bfd_com_section
;
2525 if (index
> elf_elfheader (abfd
)->e_shnum
)
2529 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[index
];
2531 switch (hdr
->sh_type
)
2533 /* ELF sections that map to BFD sections */
2536 if (hdr
->rawdata
== NULL
)
2538 if (! bfd_section_from_shdr (abfd
, index
))
2541 return (struct sec
*) hdr
->rawdata
;
2544 return (struct sec
*) &bfd_abs_section
;
2549 /* given a section, search the header to find them... */
2551 elf_section_from_bfd_section (abfd
, asect
)
2555 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2557 Elf_Internal_Shdr
*hdr
;
2558 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2560 if (asect
== &bfd_abs_section
)
2562 if (asect
== &bfd_com_section
)
2564 if (asect
== &bfd_und_section
)
2567 for (index
= 0; index
< maxindex
; index
++)
2569 hdr
= i_shdrp
[index
];
2570 switch (hdr
->sh_type
)
2572 /* ELF sections that map to BFD sections */
2578 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2584 /* fix_up_strtabs will generate STRTAB sections with names
2586 if (!strncmp (asect
->name
, ".stab", 5)
2587 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
2591 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2599 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2601 if (bed
->elf_backend_section_from_bfd_section
)
2606 if ((*bed
->elf_backend_section_from_bfd_section
)
2607 (abfd
, hdr
, asect
, &retval
))
2617 /* given a symbol, return the bfd index for that symbol. */
2619 elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
2621 struct symbol_cache_entry
**asym_ptr_ptr
;
2623 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2625 flagword flags
= asym_ptr
->flags
;
2627 /* When gas creates relocations against local labels, it creates its
2628 own symbol for the section, but does put the symbol into the
2629 symbol chain, so udata is 0. When the linker is generating
2630 relocatable output, this section symbol may be for one of the
2631 input sections rather than the output section. */
2632 if (asym_ptr
->udata
== (PTR
) 0
2633 && (flags
& BSF_SECTION_SYM
)
2634 && asym_ptr
->section
)
2638 if (asym_ptr
->section
->output_section
!= NULL
)
2639 indx
= asym_ptr
->section
->output_section
->index
;
2641 indx
= asym_ptr
->section
->index
;
2642 if (elf_section_syms (abfd
)[indx
])
2643 asym_ptr
->udata
= elf_section_syms (abfd
)[indx
]->udata
;
2646 if (asym_ptr
->udata
)
2647 idx
= ((Elf_Sym_Extra
*) asym_ptr
->udata
)->elf_sym_num
;
2657 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
2658 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
, elf_symbol_flags (flags
));
2667 elf_slurp_symbol_table (abfd
, symptrs
, dynamic
)
2669 asymbol
**symptrs
; /* Buffer for generated bfd symbols */
2672 Elf_Internal_Shdr
*hdr
;
2673 long symcount
; /* Number of external ELF symbols */
2674 elf_symbol_type
*sym
; /* Pointer to current bfd symbol */
2675 elf_symbol_type
*symbase
; /* Buffer for generated bfd symbols */
2676 Elf_Internal_Sym i_sym
;
2677 Elf_External_Sym
*x_symp
= NULL
;
2679 /* Read each raw ELF symbol, converting from external ELF form to
2680 internal ELF form, and then using the information to create a
2681 canonical bfd symbol table entry.
2683 Note that we allocate the initial bfd canonical symbol buffer
2684 based on a one-to-one mapping of the ELF symbols to canonical
2685 symbols. We actually use all the ELF symbols, so there will be no
2686 space left over at the end. When we have all the symbols, we
2687 build the caller's pointer vector. */
2690 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2692 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2693 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2696 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2699 sym
= symbase
= NULL
;
2704 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2707 symbase
= ((elf_symbol_type
*)
2708 bfd_zalloc (abfd
, symcount
* sizeof (elf_symbol_type
)));
2709 if (symbase
== (elf_symbol_type
*) NULL
)
2711 bfd_set_error (bfd_error_no_memory
);
2716 /* Temporarily allocate room for the raw ELF symbols. */
2717 x_symp
= ((Elf_External_Sym
*)
2718 malloc (symcount
* sizeof (Elf_External_Sym
)));
2719 if (x_symp
== NULL
&& symcount
!= 0)
2721 bfd_set_error (bfd_error_no_memory
);
2725 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
2726 != symcount
* sizeof (Elf_External_Sym
))
2728 /* Skip first symbol, which is a null dummy. */
2729 for (i
= 1; i
< symcount
; i
++)
2731 elf_swap_symbol_in (abfd
, x_symp
+ i
, &i_sym
);
2732 memcpy (&sym
->internal_elf_sym
, &i_sym
, sizeof (Elf_Internal_Sym
));
2733 #ifdef ELF_KEEP_EXTSYM
2734 memcpy (&sym
->native_elf_sym
, x_symp
+ i
, sizeof (Elf_External_Sym
));
2736 sym
->symbol
.the_bfd
= abfd
;
2738 sym
->symbol
.name
= elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2741 sym
->symbol
.value
= i_sym
.st_value
;
2743 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERVE
)
2745 sym
->symbol
.section
= section_from_elf_index (abfd
,
2747 if (sym
->symbol
.section
== NULL
)
2750 else if (i_sym
.st_shndx
== SHN_ABS
)
2752 sym
->symbol
.section
= &bfd_abs_section
;
2754 else if (i_sym
.st_shndx
== SHN_COMMON
)
2756 sym
->symbol
.section
= &bfd_com_section
;
2757 /* Elf puts the alignment into the `value' field, and
2758 the size into the `size' field. BFD wants to see the
2759 size in the value field, and doesn't care (at the
2760 moment) about the alignment. */
2761 sym
->symbol
.value
= i_sym
.st_size
;
2763 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2765 sym
->symbol
.section
= &bfd_und_section
;
2768 sym
->symbol
.section
= &bfd_abs_section
;
2770 sym
->symbol
.value
-= sym
->symbol
.section
->vma
;
2772 switch (ELF_ST_BIND (i_sym
.st_info
))
2775 sym
->symbol
.flags
|= BSF_LOCAL
;
2778 sym
->symbol
.flags
|= BSF_GLOBAL
;
2781 sym
->symbol
.flags
|= BSF_WEAK
;
2785 switch (ELF_ST_TYPE (i_sym
.st_info
))
2788 sym
->symbol
.flags
|= BSF_SECTION_SYM
| BSF_DEBUGGING
;
2791 sym
->symbol
.flags
|= BSF_FILE
| BSF_DEBUGGING
;
2794 sym
->symbol
.flags
|= BSF_FUNCTION
;
2799 sym
->symbol
.flags
|= BSF_DYNAMIC
;
2801 /* Do some backend-specific processing on this symbol. */
2803 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2804 if (ebd
->elf_backend_symbol_processing
)
2805 (*ebd
->elf_backend_symbol_processing
) (abfd
, &sym
->symbol
);
2812 /* Do some backend-specific processing on this symbol table. */
2814 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2815 if (ebd
->elf_backend_symbol_table_processing
)
2816 (*ebd
->elf_backend_symbol_table_processing
) (abfd
, symbase
, symcount
);
2819 /* We rely on the zalloc to clear out the final symbol entry. */
2821 symcount
= sym
- symbase
;
2823 /* Fill in the user's symbol pointer vector if needed. */
2831 *symptrs
++ = &sym
->symbol
;
2834 *symptrs
= 0; /* Final null pointer */
2846 /* Return the number of bytes required to hold the symtab vector.
2848 Note that we base it on the count plus 1, since we will null terminate
2849 the vector allocated based on this size. However, the ELF symbol table
2850 always has a dummy entry as symbol #0, so it ends up even. */
2853 elf_get_symtab_upper_bound (abfd
)
2858 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2860 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2861 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2867 elf_get_dynamic_symtab_upper_bound (abfd
)
2872 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2874 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2875 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2881 This function return the number of bytes required to store the
2882 relocation information associated with section <<sect>>
2883 attached to bfd <<abfd>>
2887 elf_get_reloc_upper_bound (abfd
, asect
)
2891 if (asect
->flags
& SEC_RELOC
)
2893 /* either rel or rela */
2894 return elf_section_data (asect
)->rel_hdr
.sh_size
;
2901 elf_slurp_reloca_table (abfd
, asect
, symbols
)
2906 Elf_External_Rela
*native_relocs
;
2907 arelent
*reloc_cache
;
2912 if (asect
->relocation
)
2914 if (asect
->reloc_count
== 0)
2916 if (asect
->flags
& SEC_CONSTRUCTOR
)
2919 if (bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
) != 0)
2921 native_relocs
= (Elf_External_Rela
*)
2922 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rela
));
2925 bfd_set_error (bfd_error_no_memory
);
2928 if (bfd_read ((PTR
) native_relocs
,
2929 sizeof (Elf_External_Rela
), asect
->reloc_count
, abfd
)
2930 != sizeof (Elf_External_Rela
) * asect
->reloc_count
)
2933 reloc_cache
= (arelent
*)
2934 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
2938 bfd_set_error (bfd_error_no_memory
);
2942 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2944 Elf_Internal_Rela dst
;
2945 Elf_External_Rela
*src
;
2947 cache_ptr
= reloc_cache
+ idx
;
2948 src
= native_relocs
+ idx
;
2949 elf_swap_reloca_in (abfd
, src
, &dst
);
2951 #ifdef RELOC_PROCESSING
2952 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
2954 if (asect
->flags
& SEC_RELOC
)
2956 /* relocatable, so the offset is off of the section */
2957 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2961 /* non-relocatable, so the offset a virtual address */
2962 cache_ptr
->address
= dst
.r_offset
;
2965 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
2966 of zero points to the dummy symbol, which was not read into
2967 the symbol table SYMBOLS. */
2968 if (ELF_R_SYM (dst
.r_info
) == 0)
2969 cache_ptr
->sym_ptr_ptr
= bfd_abs_section
.symbol_ptr_ptr
;
2974 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
2976 /* Translate any ELF section symbol into a BFD section
2978 s
= *(cache_ptr
->sym_ptr_ptr
);
2979 if (s
->flags
& BSF_SECTION_SYM
)
2981 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
2982 s
= *cache_ptr
->sym_ptr_ptr
;
2983 if (s
->name
== 0 || s
->name
[0] == 0)
2987 cache_ptr
->addend
= dst
.r_addend
;
2989 /* Fill in the cache_ptr->howto field from dst.r_type */
2991 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
2992 (*ebd
->elf_info_to_howto
) (abfd
, cache_ptr
, &dst
);
2997 asect
->relocation
= reloc_cache
;
3003 elf_debug_section (str
, num
, hdr
)
3006 Elf_Internal_Shdr
*hdr
;
3008 fprintf (stderr
, "\nSection#%d '%s' 0x%.8lx\n", num
, str
, (long) hdr
);
3010 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
3011 (long) hdr
->sh_name
,
3012 (long) hdr
->sh_type
,
3013 (long) hdr
->sh_flags
);
3015 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
3016 (long) hdr
->sh_addr
,
3017 (long) hdr
->sh_offset
,
3018 (long) hdr
->sh_size
);
3020 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
3021 (long) hdr
->sh_link
,
3022 (long) hdr
->sh_info
,
3023 (long) hdr
->sh_addralign
);
3024 fprintf (stderr
, "sh_entsize = %ld\n",
3025 (long) hdr
->sh_entsize
);
3026 fprintf (stderr
, "rawdata = 0x%.8lx\n", (long) hdr
->rawdata
);
3027 fprintf (stderr
, "contents = 0x%.8lx\n", (long) hdr
->contents
);
3028 fprintf (stderr
, "size = %ld\n", (long) hdr
->size
);
3033 elf_debug_file (ehdrp
)
3034 Elf_Internal_Ehdr
*ehdrp
;
3036 fprintf (stderr
, "e_entry = 0x%.8lx\n", (long) ehdrp
->e_entry
);
3037 fprintf (stderr
, "e_phoff = %ld\n", (long) ehdrp
->e_phoff
);
3038 fprintf (stderr
, "e_phnum = %ld\n", (long) ehdrp
->e_phnum
);
3039 fprintf (stderr
, "e_phentsize = %ld\n", (long) ehdrp
->e_phentsize
);
3040 fprintf (stderr
, "e_shoff = %ld\n", (long) ehdrp
->e_shoff
);
3041 fprintf (stderr
, "e_shnum = %ld\n", (long) ehdrp
->e_shnum
);
3042 fprintf (stderr
, "e_shentsize = %ld\n", (long) ehdrp
->e_shentsize
);
3047 elf_slurp_reloc_table (abfd
, asect
, symbols
)
3052 Elf_External_Rel
*native_relocs
;
3053 arelent
*reloc_cache
;
3055 Elf_Internal_Shdr
*data_hdr
;
3057 unsigned long data_max
;
3058 char buf
[4]; /* FIXME -- might be elf64 */
3062 if (asect
->relocation
)
3064 if (asect
->reloc_count
== 0)
3066 if (asect
->flags
& SEC_CONSTRUCTOR
)
3069 if (bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
) != 0)
3071 native_relocs
= (Elf_External_Rel
*)
3072 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rel
));
3075 bfd_set_error (bfd_error_no_memory
);
3078 if (bfd_read ((PTR
) native_relocs
,
3079 sizeof (Elf_External_Rel
), asect
->reloc_count
, abfd
)
3080 != sizeof (Elf_External_Rel
) * asect
->reloc_count
)
3083 reloc_cache
= (arelent
*)
3084 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
3088 bfd_set_error (bfd_error_no_memory
);
3092 /* Get the offset of the start of the segment we are relocating to read in
3093 the implicit addend. */
3094 data_hdr
= &elf_section_data (asect
)->this_hdr
;
3095 data_off
= data_hdr
->sh_offset
;
3096 data_max
= data_hdr
->sh_size
- sizeof (buf
) + 1;
3099 elf_debug_section ("data section", -1, data_hdr
);
3102 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
3104 #ifdef RELOC_PROCESSING
3105 Elf_Internal_Rel dst
;
3106 Elf_External_Rel
*src
;
3108 cache_ptr
= reloc_cache
+ idx
;
3109 src
= native_relocs
+ idx
;
3110 elf_swap_reloc_in (abfd
, src
, &dst
);
3112 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
3114 Elf_Internal_Rel dst
;
3115 Elf_External_Rel
*src
;
3117 cache_ptr
= reloc_cache
+ idx
;
3118 src
= native_relocs
+ idx
;
3120 elf_swap_reloc_in (abfd
, src
, &dst
);
3122 if (asect
->flags
& SEC_RELOC
)
3124 /* relocatable, so the offset is off of the section */
3125 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
3129 /* non-relocatable, so the offset a virtual address */
3130 cache_ptr
->address
= dst
.r_offset
;
3133 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
3134 of zero points to the dummy symbol, which was not read into
3135 the symbol table SYMBOLS. */
3136 if (ELF_R_SYM (dst
.r_info
) == 0)
3137 cache_ptr
->sym_ptr_ptr
= bfd_abs_section
.symbol_ptr_ptr
;
3142 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
3144 /* Translate any ELF section symbol into a BFD section
3146 s
= *(cache_ptr
->sym_ptr_ptr
);
3147 if (s
->flags
& BSF_SECTION_SYM
)
3149 cache_ptr
->sym_ptr_ptr
= s
->section
->symbol_ptr_ptr
;
3150 s
= *cache_ptr
->sym_ptr_ptr
;
3151 if (s
->name
== 0 || s
->name
[0] == 0)
3155 BFD_ASSERT (dst
.r_offset
<= data_max
);
3156 cache_ptr
->addend
= 0;
3158 /* Fill in the cache_ptr->howto field from dst.r_type */
3160 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
3161 (*ebd
->elf_info_to_howto_rel
) (abfd
, cache_ptr
, &dst
);
3166 asect
->relocation
= reloc_cache
;
3171 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
3177 arelent
*tblptr
= section
->relocation
;
3178 unsigned int count
= 0;
3179 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
3181 /* snarfed from coffcode.h */
3184 if (! elf_slurp_reloca_table (abfd
, section
, symbols
))
3189 if (! elf_slurp_reloc_table (abfd
, section
, symbols
))
3193 tblptr
= section
->relocation
;
3195 for (; count
++ < section
->reloc_count
;)
3196 *relptr
++ = tblptr
++;
3199 return section
->reloc_count
;
3203 elf_get_symtab (abfd
, alocation
)
3205 asymbol
**alocation
;
3207 long symcount
= elf_slurp_symbol_table (abfd
, alocation
, false);
3210 bfd_get_symcount (abfd
) = symcount
;
3215 elf_canonicalize_dynamic_symtab (abfd
, alocation
)
3217 asymbol
**alocation
;
3219 return elf_slurp_symbol_table (abfd
, alocation
, true);
3223 elf_make_empty_symbol (abfd
)
3226 elf_symbol_type
*newsym
;
3228 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
3231 bfd_set_error (bfd_error_no_memory
);
3236 newsym
->symbol
.the_bfd
= abfd
;
3237 return &newsym
->symbol
;
3242 elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
3247 bfd_symbol_info (symbol
, ret
);
3251 elf_print_symbol (ignore_abfd
, filep
, symbol
, how
)
3255 bfd_print_symbol_type how
;
3257 FILE *file
= (FILE *) filep
;
3260 case bfd_print_symbol_name
:
3261 fprintf (file
, "%s", symbol
->name
);
3263 case bfd_print_symbol_more
:
3264 fprintf (file
, "elf ");
3265 fprintf_vma (file
, symbol
->value
);
3266 fprintf (file
, " %lx", (long) symbol
->flags
);
3268 case bfd_print_symbol_all
:
3270 CONST
char *section_name
;
3271 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
3272 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
3273 fprintf (file
, " %s\t%s",
3283 elf_get_lineno (ignore_abfd
, symbol
)
3287 fprintf (stderr
, "elf_get_lineno unimplemented\n");
3294 elf_set_arch_mach (abfd
, arch
, machine
)
3296 enum bfd_architecture arch
;
3297 unsigned long machine
;
3299 /* If this isn't the right architecture for this backend, and this
3300 isn't the generic backend, fail. */
3301 if (arch
!= get_elf_backend_data (abfd
)->arch
3302 && arch
!= bfd_arch_unknown
3303 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
3306 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
3310 elf_find_nearest_line (abfd
,
3321 CONST
char **filename_ptr
;
3322 CONST
char **functionname_ptr
;
3323 unsigned int *line_ptr
;
3329 elf_sizeof_headers (abfd
, reloc
)
3333 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
3340 elf_set_section_contents (abfd
, section
, location
, offset
, count
)
3345 bfd_size_type count
;
3347 Elf_Internal_Shdr
*hdr
;
3349 if (! abfd
->output_has_begun
3350 && ! elf_compute_section_file_positions (abfd
,
3351 (struct bfd_link_info
*) NULL
))
3354 hdr
= &elf_section_data (section
)->this_hdr
;
3356 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
3358 if (bfd_write (location
, 1, count
, abfd
) != count
)
3365 elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
3368 Elf_Internal_Rela
*dst
;
3370 fprintf (stderr
, "elf RELA relocation support for target machine unimplemented\n");
3376 elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
3379 Elf_Internal_Rel
*dst
;
3381 fprintf (stderr
, "elf REL relocation support for target machine unimplemented\n");
3387 /* Core file support */
3389 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3390 #include <sys/procfs.h>
3392 #define bfd_prstatus(abfd, descdata, descsz, filepos) true
3393 #define bfd_fpregset(abfd, descdata, descsz, filepos) true
3394 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) true
3400 bfd_prstatus (abfd
, descdata
, descsz
, filepos
)
3407 prstatus_t
*status
= (prstatus_t
*) 0;
3409 if (descsz
== sizeof (prstatus_t
))
3411 newsect
= bfd_make_section (abfd
, ".reg");
3412 if (newsect
== NULL
)
3414 newsect
->_raw_size
= sizeof (status
->pr_reg
);
3415 newsect
->filepos
= filepos
+ (long) &status
->pr_reg
;
3416 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3417 newsect
->alignment_power
= 2;
3418 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
3420 memcpy (core_prstatus (abfd
), descdata
, descsz
);
3426 /* Stash a copy of the prpsinfo structure away for future use. */
3429 bfd_prpsinfo (abfd
, descdata
, descsz
, filepos
)
3435 if (descsz
== sizeof (prpsinfo_t
))
3437 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) == NULL
)
3439 bfd_set_error (bfd_error_no_memory
);
3442 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
3448 bfd_fpregset (abfd
, descdata
, descsz
, filepos
)
3456 newsect
= bfd_make_section (abfd
, ".reg2");
3457 if (newsect
== NULL
)
3459 newsect
->_raw_size
= descsz
;
3460 newsect
->filepos
= filepos
;
3461 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3462 newsect
->alignment_power
= 2;
3466 #endif /* HAVE_PROCFS */
3468 /* Return a pointer to the args (including the command name) that were
3469 seen by the program that generated the core dump. Note that for
3470 some reason, a spurious space is tacked onto the end of the args
3471 in some (at least one anyway) implementations, so strip it off if
3475 elf_core_file_failing_command (abfd
)
3479 if (core_prpsinfo (abfd
))
3481 prpsinfo_t
*p
= core_prpsinfo (abfd
);
3482 char *scan
= p
->pr_psargs
;
3487 if ((scan
> p
->pr_psargs
) && (*scan
== ' '))
3491 return p
->pr_psargs
;
3497 /* Return the number of the signal that caused the core dump. Presumably,
3498 since we have a core file, we got a signal of some kind, so don't bother
3499 checking the other process status fields, just return the signal number.
3503 elf_core_file_failing_signal (abfd
)
3507 if (core_prstatus (abfd
))
3509 return ((prstatus_t
*) (core_prstatus (abfd
)))->pr_cursig
;
3515 /* Check to see if the core file could reasonably be expected to have
3516 come for the current executable file. Note that by default we return
3517 true unless we find something that indicates that there might be a
3522 elf_core_file_matches_executable_p (core_bfd
, exec_bfd
)
3531 /* First, xvecs must match since both are ELF files for the same target. */
3533 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
3535 bfd_set_error (bfd_error_system_call
);
3541 /* If no prpsinfo, just return true. Otherwise, grab the last component
3542 of the exec'd pathname from the prpsinfo. */
3544 if (core_prpsinfo (core_bfd
))
3546 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
))->pr_fname
);
3553 /* Find the last component of the executable pathname. */
3555 if ((execname
= strrchr (exec_bfd
->filename
, '/')) != NULL
)
3561 execname
= (char *) exec_bfd
->filename
;
3564 /* See if they match */
3566 return strcmp (execname
, corename
) ? false : true;
3572 #endif /* HAVE_PROCFS */
3575 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3576 the information that would normally be available from the /proc interface
3577 for the process, at the time the process dumped core. Currently this
3578 includes copies of the prstatus, prpsinfo, and fpregset structures.
3580 Since these structures are potentially machine dependent in size and
3581 ordering, bfd provides two levels of support for them. The first level,
3582 available on all machines since it does not require that the host
3583 have /proc support or the relevant include files, is to create a bfd
3584 section for each of the prstatus, prpsinfo, and fpregset structures,
3585 without any interpretation of their contents. With just this support,
3586 the bfd client will have to interpret the structures itself. Even with
3587 /proc support, it might want these full structures for it's own reasons.
3589 In the second level of support, where HAVE_PROCFS is defined, bfd will
3590 pick apart the structures to gather some additional information that
3591 clients may want, such as the general register set, the name of the
3592 exec'ed file and its arguments, the signal (if any) that caused the
3598 elf_corefile_note (abfd
, hdr
)
3600 Elf_Internal_Phdr
*hdr
;
3602 Elf_External_Note
*x_note_p
; /* Elf note, external form */
3603 Elf_Internal_Note i_note
; /* Elf note, internal form */
3604 char *buf
= NULL
; /* Entire note segment contents */
3605 char *namedata
; /* Name portion of the note */
3606 char *descdata
; /* Descriptor portion of the note */
3607 char *sectname
; /* Name to use for new section */
3608 long filepos
; /* File offset to descriptor data */
3611 if (hdr
->p_filesz
> 0
3612 && (buf
= (char *) malloc (hdr
->p_filesz
)) != NULL
3613 && bfd_seek (abfd
, hdr
->p_offset
, SEEK_SET
) != -1
3614 && bfd_read ((PTR
) buf
, hdr
->p_filesz
, 1, abfd
) == hdr
->p_filesz
)
3616 x_note_p
= (Elf_External_Note
*) buf
;
3617 while ((char *) x_note_p
< (buf
+ hdr
->p_filesz
))
3619 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->namesz
);
3620 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->descsz
);
3621 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->type
);
3622 namedata
= x_note_p
->name
;
3623 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
3624 filepos
= hdr
->p_offset
+ (descdata
- buf
);
3625 switch (i_note
.type
)
3628 /* process descdata as prstatus info */
3629 if (! bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
))
3631 sectname
= ".prstatus";
3634 /* process descdata as fpregset info */
3635 if (! bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
))
3637 sectname
= ".fpregset";
3640 /* process descdata as prpsinfo */
3641 if (! bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
))
3643 sectname
= ".prpsinfo";
3646 /* Unknown descriptor, just ignore it. */
3650 if (sectname
!= NULL
)
3652 newsect
= bfd_make_section (abfd
, sectname
);
3653 if (newsect
== NULL
)
3655 newsect
->_raw_size
= i_note
.descsz
;
3656 newsect
->filepos
= filepos
;
3657 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3658 newsect
->alignment_power
= 2;
3660 x_note_p
= (Elf_External_Note
*)
3661 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
3668 else if (hdr
->p_filesz
> 0)
3670 bfd_set_error (bfd_error_no_memory
);
3677 /* Core files are simply standard ELF formatted files that partition
3678 the file using the execution view of the file (program header table)
3679 rather than the linking view. In fact, there is no section header
3680 table in a core file.
3682 The process status information (including the contents of the general
3683 register set) and the floating point register set are stored in a
3684 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3685 that allow standard bfd access to the general registers (.reg) and the
3686 floating point registers (.reg2).
3691 elf_core_file_p (abfd
)
3694 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
3695 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3696 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
3697 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
3698 unsigned int phindex
;
3699 struct elf_backend_data
*ebd
;
3701 /* Read in the ELF header in external format. */
3703 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
3705 if (bfd_get_error () != bfd_error_system_call
)
3706 bfd_set_error (bfd_error_wrong_format
);
3710 /* Now check to see if we have a valid ELF file, and one that BFD can
3711 make use of. The magic number must match, the address size ('class')
3712 and byte-swapping must match our XVEC entry, and it must have a
3713 program header table (FIXME: See comments re segments at top of this
3716 if (elf_file_p (&x_ehdr
) == false)
3719 bfd_set_error (bfd_error_wrong_format
);
3723 /* FIXME, Check EI_VERSION here ! */
3727 int desired_address_size
= ELFCLASS32
;
3730 int desired_address_size
= ELFCLASS64
;
3733 if (x_ehdr
.e_ident
[EI_CLASS
] != desired_address_size
)
3737 /* Switch xvec to match the specified byte order. */
3738 switch (x_ehdr
.e_ident
[EI_DATA
])
3740 case ELFDATA2MSB
: /* Big-endian */
3741 if (abfd
->xvec
->byteorder_big_p
== false)
3744 case ELFDATA2LSB
: /* Little-endian */
3745 if (abfd
->xvec
->byteorder_big_p
== true)
3748 case ELFDATANONE
: /* No data encoding specified */
3749 default: /* Unknown data encoding specified */
3753 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3754 the tdata pointer in the bfd. */
3757 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
3758 if (elf_tdata (abfd
) == NULL
)
3760 bfd_set_error (bfd_error_no_memory
);
3764 /* FIXME, `wrong' returns from this point onward, leak memory. */
3766 /* Now that we know the byte order, swap in the rest of the header */
3767 i_ehdrp
= elf_elfheader (abfd
);
3768 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
3770 elf_debug_file (i_ehdrp
);
3773 ebd
= get_elf_backend_data (abfd
);
3775 /* Check that the ELF e_machine field matches what this particular
3776 BFD format expects. */
3777 if (ebd
->elf_machine_code
!= i_ehdrp
->e_machine
)
3779 bfd_target
**target_ptr
;
3781 if (ebd
->elf_machine_code
!= EM_NONE
)
3784 /* This is the generic ELF target. Let it match any ELF target
3785 for which we do not have a specific backend. */
3786 for (target_ptr
= bfd_target_vector
; *target_ptr
!= NULL
; target_ptr
++)
3788 struct elf_backend_data
*back
;
3790 if ((*target_ptr
)->flavour
!= bfd_target_elf_flavour
)
3792 back
= (struct elf_backend_data
*) (*target_ptr
)->backend_data
;
3793 if (back
->elf_machine_code
== i_ehdrp
->e_machine
)
3795 /* target_ptr is an ELF backend which matches this
3796 object file, so reject the generic ELF target. */
3802 /* If there is no program header, or the type is not a core file, then
3804 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
3807 /* Allocate space for a copy of the program header table in
3808 internal form, seek to the program header table in the file,
3809 read it in, and convert it to internal form. As a simple sanity
3810 check, verify that the what BFD thinks is the size of each program
3811 header table entry actually matches the size recorded in the file. */
3813 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
3815 i_phdrp
= (Elf_Internal_Phdr
*)
3816 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
3819 bfd_set_error (bfd_error_no_memory
);
3822 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
3824 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3826 if (bfd_read ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
3829 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
3832 /* Once all of the program headers have been read and converted, we
3833 can start processing them. */
3835 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3837 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
3838 if ((i_phdrp
+ phindex
)->p_type
== PT_NOTE
)
3840 if (! elf_corefile_note (abfd
, i_phdrp
+ phindex
))
3845 /* Remember the entry point specified in the ELF file header. */
3847 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
3852 /* ELF linker code. */
3854 static boolean elf_link_add_object_symbols
3855 PARAMS ((bfd
*, struct bfd_link_info
*));
3856 static boolean elf_link_add_archive_symbols
3857 PARAMS ((bfd
*, struct bfd_link_info
*));
3859 /* Given an ELF BFD, add symbols to the global hash table as
3863 elf_bfd_link_add_symbols (abfd
, info
)
3865 struct bfd_link_info
*info
;
3867 switch (bfd_get_format (abfd
))
3870 return elf_link_add_object_symbols (abfd
, info
);
3872 return elf_link_add_archive_symbols (abfd
, info
);
3874 bfd_set_error (bfd_error_wrong_format
);
3879 /* Add symbols from an ELF archive file to the linker hash table. We
3880 don't use _bfd_generic_link_add_archive_symbols because of a
3881 problem which arises on UnixWare. The UnixWare libc.so is an
3882 archive which includes an entry libc.so.1 which defines a bunch of
3883 symbols. The libc.so archive also includes a number of other
3884 object files, which also define symbols, some of which are the same
3885 as those defined in libc.so.1. Correct linking requires that we
3886 consider each object file in turn, and include it if it defines any
3887 symbols we need. _bfd_generic_link_add_archive_symbols does not do
3888 this; it looks through the list of undefined symbols, and includes
3889 any object file which defines them. When this algorithm is used on
3890 UnixWare, it winds up pulling in libc.so.1 early and defining a
3891 bunch of symbols. This means that some of the other objects in the
3892 archive are not included in the link, which is incorrect since they
3893 precede libc.so.1 in the archive.
3895 Fortunately, ELF archive handling is simpler than that done by
3896 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
3897 oddities. In ELF, if we find a symbol in the archive map, and the
3898 symbol is currently undefined, we know that we must pull in that
3901 Unfortunately, we do have to make multiple passes over the symbol
3902 table until nothing further is resolved. */
3905 elf_link_add_archive_symbols (abfd
, info
)
3907 struct bfd_link_info
*info
;
3910 boolean
*defined
= NULL
;
3911 boolean
*included
= NULL
;
3915 if (! bfd_has_map (abfd
))
3917 bfd_set_error (bfd_error_no_symbols
);
3921 /* Keep track of all symbols we know to be already defined, and all
3922 files we know to be already included. This is to speed up the
3923 second and subsequent passes. */
3924 c
= bfd_ardata (abfd
)->symdef_count
;
3927 defined
= (boolean
*) malloc (c
* sizeof (boolean
));
3928 included
= (boolean
*) malloc (c
* sizeof (boolean
));
3929 if (defined
== (boolean
*) NULL
|| included
== (boolean
*) NULL
)
3931 bfd_set_error (bfd_error_no_memory
);
3934 memset (defined
, 0, c
* sizeof (boolean
));
3935 memset (included
, 0, c
* sizeof (boolean
));
3937 symdefs
= bfd_ardata (abfd
)->symdefs
;
3950 symdefend
= symdef
+ c
;
3951 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
3953 struct elf_link_hash_entry
*h
;
3955 struct bfd_link_hash_entry
*undefs_tail
;
3958 if (defined
[i
] || included
[i
])
3960 if (symdef
->file_offset
== last
)
3966 h
= elf_link_hash_lookup (elf_hash_table (info
), symdef
->name
,
3967 false, false, false);
3968 if (h
== (struct elf_link_hash_entry
*) NULL
)
3970 if (h
->root
.type
!= bfd_link_hash_undefined
)
3976 /* We need to include this archive member. */
3978 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3979 if (element
== (bfd
*) NULL
)
3982 if (! bfd_check_format (element
, bfd_object
))
3985 /* Doublecheck that we have not included this object
3986 already--it should be impossible, but there may be
3987 something wrong with the archive. */
3988 if (element
->archive_pass
!= 0)
3990 bfd_set_error (bfd_error_bad_value
);
3993 element
->archive_pass
= 1;
3995 undefs_tail
= info
->hash
->undefs_tail
;
3997 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4000 if (! elf_link_add_object_symbols (element
, info
))
4003 /* If there are any new undefined symbols, we need to make
4004 another pass through the archive in order to see whether
4005 they can be defined. FIXME: This isn't perfect, because
4006 common symbols wind up on undefs_tail and because an
4007 undefined symbol which is defined later on in this pass
4008 does not require another pass. This isn't a bug, but it
4009 does make the code less efficient than it could be. */
4010 if (undefs_tail
!= info
->hash
->undefs_tail
)
4013 /* Look backward to mark all symbols from this object file
4014 which we have already seen in this pass. */
4018 included
[mark
] = true;
4023 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4025 /* We mark subsequent symbols from this object file as we go
4026 on through the loop. */
4027 last
= symdef
->file_offset
;
4038 if (defined
!= (boolean
*) NULL
)
4040 if (included
!= (boolean
*) NULL
)
4045 /* Add symbols from an ELF object file to the linker hash table. */
4048 elf_link_add_object_symbols (abfd
, info
)
4050 struct bfd_link_info
*info
;
4052 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
4053 const Elf_Internal_Sym
*,
4054 const char **, flagword
*,
4055 asection
**, bfd_vma
*));
4057 Elf_Internal_Shdr
*hdr
;
4060 Elf_External_Sym
*buf
= NULL
;
4061 struct elf_link_hash_entry
**sym_hash
;
4062 Elf_External_Sym
*esym
;
4063 Elf_External_Sym
*esymend
;
4065 add_symbol_hook
= get_elf_backend_data (abfd
)->elf_add_symbol_hook
;
4066 collect
= get_elf_backend_data (abfd
)->collect
;
4068 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4069 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
4071 /* The sh_info field of the symtab header tells us where the
4072 external symbols start. We don't care about the local symbols at
4074 extsymcount
= symcount
- hdr
->sh_info
;
4076 buf
= (Elf_External_Sym
*) malloc (extsymcount
* sizeof (Elf_External_Sym
));
4077 if (buf
== NULL
&& extsymcount
!= 0)
4079 bfd_set_error (bfd_error_no_memory
);
4083 /* We keep hash table entry for each external symbol. */
4084 sym_hash
= ((struct elf_link_hash_entry
**)
4086 extsymcount
* sizeof (struct elf_link_hash_entry
*)));
4087 if (sym_hash
== NULL
)
4089 bfd_set_error (bfd_error_no_memory
);
4092 elf_sym_hashes (abfd
) = sym_hash
;
4095 hdr
->sh_offset
+ hdr
->sh_info
* sizeof (Elf_External_Sym
),
4097 || (bfd_read ((PTR
) buf
, sizeof (Elf_External_Sym
), extsymcount
, abfd
)
4098 != extsymcount
* sizeof (Elf_External_Sym
)))
4101 esymend
= buf
+ extsymcount
;
4102 for (esym
= buf
; esym
< esymend
; esym
++, sym_hash
++)
4104 Elf_Internal_Sym sym
;
4111 elf_swap_symbol_in (abfd
, esym
, &sym
);
4113 flags
= BSF_NO_FLAGS
;
4115 value
= sym
.st_value
;
4118 bind
= ELF_ST_BIND (sym
.st_info
);
4119 if (bind
== STB_LOCAL
)
4121 /* This should be impossible, since ELF requires that all
4122 global symbols follow all local symbols, and that sh_info
4123 point to the first global symbol. */
4124 bfd_set_error (bfd_error_bad_value
);
4127 else if (bind
== STB_GLOBAL
)
4129 else if (bind
== STB_WEAK
)
4133 /* Leave it up to the processor backend. */
4136 if (sym
.st_shndx
== SHN_UNDEF
)
4137 sec
= &bfd_und_section
;
4138 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
4140 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
4145 else if (sym
.st_shndx
== SHN_ABS
)
4146 sec
= &bfd_abs_section
;
4147 else if (sym
.st_shndx
== SHN_COMMON
)
4149 sec
= &bfd_com_section
;
4150 /* What ELF calls the size we call the value. What ELF
4151 calls the value we call the alignment. */
4152 value
= sym
.st_size
;
4156 /* Leave it up to the processor backend. */
4159 name
= elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
4160 if (name
== (const char *) NULL
)
4163 if (add_symbol_hook
)
4165 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
4169 /* The hook function sets the name to NULL if this symbol
4170 should be skipped for some reason. */
4171 if (name
== (const char *) NULL
)
4175 /* Sanity check that all possibilities were handled. */
4176 if (flags
== BSF_NO_FLAGS
|| sec
== (asection
*) NULL
)
4178 bfd_set_error (bfd_error_bad_value
);
4182 if (! (_bfd_generic_link_add_one_symbol
4183 (info
, abfd
, name
, flags
, sec
, value
, (const char *) NULL
,
4184 false, collect
, (struct bfd_link_hash_entry
**) sym_hash
)))
4187 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
4189 /* Remember the symbol size, type and alignment. */
4190 if (sym
.st_size
!= 0)
4192 /* FIXME: We should probably somehow give a warning if
4193 the symbol size changes. */
4194 (*sym_hash
)->size
= sym
.st_size
;
4196 if (sym
.st_shndx
== SHN_COMMON
4197 && sym
.st_value
> (*sym_hash
)->align
)
4198 (*sym_hash
)->align
= sym
.st_value
;
4199 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
)
4201 /* FIXME: We should probably somehow give a warning if
4202 the symbol type changes. */
4203 (*sym_hash
)->type
= ELF_ST_TYPE (sym
.st_info
);
4219 /* Final phase of ELF linker. */
4221 /* A structure we use to avoid passing large numbers of arguments. */
4223 struct elf_final_link_info
4225 /* General link information. */
4226 struct bfd_link_info
*info
;
4229 /* Symbol string table. */
4230 struct strtab
*symstrtab
;
4231 /* Buffer large enough to hold contents of any section. */
4233 /* Buffer large enough to hold external relocs of any section. */
4234 PTR external_relocs
;
4235 /* Buffer large enough to hold internal relocs of any section. */
4236 Elf_Internal_Rela
*internal_relocs
;
4237 /* Buffer large enough to hold external local symbols of any input
4239 Elf_External_Sym
*external_syms
;
4240 /* Buffer large enough to hold internal local symbols of any input
4242 Elf_Internal_Sym
*internal_syms
;
4243 /* Array large enough to hold a symbol index for each local symbol
4244 of any input BFD. */
4246 /* Array large enough to hold a section pointer for each local
4247 symbol of any input BFD. */
4248 asection
**sections
;
4249 /* Buffer to hold swapped out symbols. */
4250 Elf_External_Sym
*symbuf
;
4251 /* Number of swapped out symbols in buffer. */
4252 size_t symbuf_count
;
4253 /* Number of symbols which fit in symbuf. */
4257 static boolean elf_link_output_sym
4258 PARAMS ((struct elf_final_link_info
*, const char *, Elf_Internal_Sym
*));
4259 static boolean elf_link_flush_output_syms
4260 PARAMS ((struct elf_final_link_info
*));
4261 static boolean elf_link_output_extsym
4262 PARAMS ((struct elf_link_hash_entry
*, PTR
));
4263 static boolean elf_link_input_bfd
4264 PARAMS ((struct elf_final_link_info
*, bfd
*));
4265 static boolean elf_reloc_link_order
4266 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
4267 struct bfd_link_order
*));
4269 /* Do the final step of an ELF link. */
4272 elf_bfd_final_link (abfd
, info
)
4274 struct bfd_link_info
*info
;
4276 struct elf_final_link_info finfo
;
4277 register asection
*o
;
4278 register struct bfd_link_order
*p
;
4280 size_t max_contents_size
;
4281 size_t max_external_reloc_size
;
4282 size_t max_internal_reloc_count
;
4283 size_t max_sym_count
;
4285 Elf_Internal_Sym elfsym
;
4286 Elf_Internal_Shdr
*symtab_hdr
;
4287 Elf_Internal_Shdr
*symstrtab_hdr
;
4290 finfo
.output_bfd
= abfd
;
4291 finfo
.symstrtab
= bfd_new_strtab (abfd
);
4292 if (finfo
.symstrtab
== NULL
)
4294 finfo
.contents
= NULL
;
4295 finfo
.external_relocs
= NULL
;
4296 finfo
.internal_relocs
= NULL
;
4297 finfo
.external_syms
= NULL
;
4298 finfo
.internal_syms
= NULL
;
4299 finfo
.indices
= NULL
;
4300 finfo
.sections
= NULL
;
4301 finfo
.symbuf
= NULL
;
4302 finfo
.symbuf_count
= 0;
4304 /* Count up the number of relocations we will output for each output
4305 section, so that we know the sizes of the reloc sections. We
4306 also figure out some maximum sizes. */
4307 max_contents_size
= 0;
4308 max_external_reloc_size
= 0;
4309 max_internal_reloc_count
= 0;
4311 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
4315 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4317 if (p
->type
== bfd_section_reloc_link_order
4318 || p
->type
== bfd_symbol_reloc_link_order
)
4320 else if (p
->type
== bfd_indirect_link_order
)
4324 sec
= p
->u
.indirect
.section
;
4326 if (info
->relocateable
)
4327 o
->reloc_count
+= sec
->reloc_count
;
4329 if (sec
->_raw_size
> max_contents_size
)
4330 max_contents_size
= sec
->_raw_size
;
4331 if (sec
->_cooked_size
> max_contents_size
)
4332 max_contents_size
= sec
->_cooked_size
;
4334 /* We are interested in just local symbols, not all
4336 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
4337 && (elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
4339 max_sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
4341 if ((sec
->flags
& SEC_RELOC
) != 0)
4345 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
4346 if (ext_size
> max_external_reloc_size
)
4347 max_external_reloc_size
= ext_size
;
4348 if (sec
->reloc_count
> max_internal_reloc_count
)
4349 max_internal_reloc_count
= sec
->reloc_count
;
4354 if (o
->reloc_count
> 0)
4355 o
->flags
|= SEC_RELOC
;
4358 /* Explicitly clear the SEC_RELOC flag. The linker tends to
4359 set it (this is probably a bug) and if it is set
4360 assign_section_numbers will create a reloc section. */
4361 o
->flags
&=~ SEC_RELOC
;
4365 /* Figure out the file positions for everything but the symbol table
4366 and the relocs. We set symcount to force assign_section_numbers
4367 to create a symbol table. */
4368 abfd
->symcount
= info
->strip
== strip_all
? 0 : 1;
4369 BFD_ASSERT (! abfd
->output_has_begun
);
4370 if (! elf_compute_section_file_positions (abfd
, info
))
4373 /* That created the reloc sections. Set their sizes, and assign
4374 them file positions, and allocate some buffers. */
4375 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4377 if ((o
->flags
& SEC_RELOC
) != 0)
4379 Elf_Internal_Shdr
*rel_hdr
;
4380 register struct elf_link_hash_entry
**p
, **pend
;
4382 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
4384 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* o
->reloc_count
;
4386 /* The contents field must last into write_object_contents,
4387 so we allocate it with bfd_alloc rather than malloc. */
4388 rel_hdr
->contents
= (PTR
) bfd_alloc (abfd
, rel_hdr
->sh_size
);
4389 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
4391 bfd_set_error (bfd_error_no_memory
);
4395 p
= ((struct elf_link_hash_entry
**)
4396 malloc (o
->reloc_count
4397 * sizeof (struct elf_link_hash_entry
*)));
4398 if (p
== NULL
&& o
->reloc_count
!= 0)
4400 bfd_set_error (bfd_error_no_memory
);
4403 elf_section_data (o
)->rel_hashes
= p
;
4404 pend
= p
+ o
->reloc_count
;
4405 for (; p
< pend
; p
++)
4408 /* Use the reloc_count field as an index when outputting the
4414 assign_file_positions_for_relocs (abfd
);
4416 /* We have now assigned file positions for all the sections except
4417 .symtab and .strtab. We start the .symtab section at the current
4418 file position, and write directly to it. We build the .strtab
4419 section in memory. When we add .dynsym support, we will build
4420 that in memory as well (.dynsym is smaller than .symtab). */
4422 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4423 /* sh_name is set in prep_headers. */
4424 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4425 symtab_hdr
->sh_flags
= 0;
4426 symtab_hdr
->sh_addr
= 0;
4427 symtab_hdr
->sh_size
= 0;
4428 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
4429 /* sh_link is set in assign_section_numbers. */
4430 /* sh_info is set below. */
4431 /* sh_offset is set just below. */
4432 symtab_hdr
->sh_addralign
= 4; /* FIXME: system dependent? */
4434 off
= elf_tdata (abfd
)->next_file_pos
;
4435 off
= align_file_position (off
);
4436 off
= assign_file_position_for_section (symtab_hdr
, off
);
4438 /* Note that at this point elf_tdata (abfd)->next_file_pos is
4439 incorrect. We do not yet know the size of the .symtab section.
4440 We correct next_file_pos below, after we do know the size. */
4442 /* Allocate a buffer to hold swapped out symbols. This is to avoid
4443 continuously seeking to the right position in the file. */
4444 if (! info
->keep_memory
|| max_sym_count
< 20)
4445 finfo
.symbuf_size
= 20;
4447 finfo
.symbuf_size
= max_sym_count
;
4448 finfo
.symbuf
= ((Elf_External_Sym
*)
4449 malloc (finfo
.symbuf_size
* sizeof (Elf_External_Sym
)));
4450 if (finfo
.symbuf
== NULL
)
4452 bfd_set_error (bfd_error_no_memory
);
4456 /* Start writing out the symbol table. The first symbol is always a
4458 elfsym
.st_value
= 0;
4461 elfsym
.st_other
= 0;
4462 elfsym
.st_shndx
= SHN_UNDEF
;
4463 if (! elf_link_output_sym (&finfo
, (const char *) NULL
, &elfsym
))
4467 /* Some standard ELF linkers do this, but we don't because it causes
4468 bootstrap comparison failures. */
4469 /* Output a file symbol for the output file as the second symbol.
4470 We output this even if we are discarding local symbols, although
4471 I'm not sure if this is correct. */
4472 elfsym
.st_value
= 0;
4474 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4475 elfsym
.st_other
= 0;
4476 elfsym
.st_shndx
= SHN_ABS
;
4477 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
), &elfsym
))
4481 /* Output a symbol for each section. We output these even if we are
4482 discarding local symbols, since they are used for relocs. These
4483 symbols have no names. We store the index of each one in the
4484 index field of the section, so that we can find it again when
4485 outputting relocs. */
4486 elfsym
.st_value
= 0;
4488 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4489 elfsym
.st_other
= 0;
4490 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4492 o
->index
= abfd
->symcount
;
4493 elfsym
.st_shndx
= elf_section_from_bfd_section (abfd
, o
);
4494 if (elfsym
.st_shndx
== (unsigned long) -1)
4496 if (! elf_link_output_sym (&finfo
, (const char *) NULL
, &elfsym
))
4500 /* Allocate some memory to hold information read in from the input
4502 finfo
.contents
= (bfd_byte
*) malloc (max_contents_size
);
4503 finfo
.external_relocs
= (PTR
) malloc (max_external_reloc_size
);
4504 finfo
.internal_relocs
= ((Elf_Internal_Rela
*)
4505 malloc (max_internal_reloc_count
4506 * sizeof (Elf_Internal_Rela
)));
4507 finfo
.external_syms
= ((Elf_External_Sym
*)
4508 malloc (max_sym_count
* sizeof (Elf_External_Sym
)));
4509 finfo
.internal_syms
= ((Elf_Internal_Sym
*)
4510 malloc (max_sym_count
* sizeof (Elf_Internal_Sym
)));
4511 finfo
.indices
= (long *) malloc (max_sym_count
* sizeof (long));
4512 finfo
.sections
= (asection
**) malloc (max_sym_count
* sizeof (asection
*));
4513 if ((finfo
.contents
== NULL
&& max_contents_size
!= 0)
4514 || (finfo
.external_relocs
== NULL
&& max_external_reloc_size
!= 0)
4515 || (finfo
.internal_relocs
== NULL
&& max_internal_reloc_count
!= 0)
4516 || (finfo
.external_syms
== NULL
&& max_sym_count
!= 0)
4517 || (finfo
.internal_syms
== NULL
&& max_sym_count
!= 0)
4518 || (finfo
.indices
== NULL
&& max_sym_count
!= 0)
4519 || (finfo
.sections
== NULL
&& max_sym_count
!= 0))
4521 bfd_set_error (bfd_error_no_memory
);
4525 /* Since ELF permits relocations to be against local symbols, we
4526 must have the local symbols available when we do the relocations.
4527 Since we would rather only read the local symbols once, and we
4528 would rather not keep them in memory, we handle all the
4529 relocations for a single input file at the same time.
4531 Unfortunately, there is no way to know the total number of local
4532 symbols until we have seen all of them, and the local symbol
4533 indices precede the global symbol indices. This means that when
4534 we are generating relocateable output, and we see a reloc against
4535 a global symbol, we can not know the symbol index until we have
4536 finished examining all the local symbols to see which ones we are
4537 going to output. To deal with this, we keep the relocations in
4538 memory, and don't output them until the end of the link. This is
4539 an unfortunate waste of memory, but I don't see a good way around
4540 it. Fortunately, it only happens when performing a relocateable
4541 link, which is not the common case. FIXME: If keep_memory is set
4542 we could write the relocs out and then read them again; I don't
4543 know how bad the memory loss will be. */
4545 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->next
)
4546 sub
->output_has_begun
= false;
4547 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4549 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4551 if (p
->type
== bfd_indirect_link_order
4552 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
4553 == bfd_target_elf_flavour
))
4555 sub
= p
->u
.indirect
.section
->owner
;
4556 if (! sub
->output_has_begun
)
4558 if (! elf_link_input_bfd (&finfo
, sub
))
4560 sub
->output_has_begun
= true;
4563 else if (p
->type
== bfd_section_reloc_link_order
4564 || p
->type
== bfd_symbol_reloc_link_order
)
4566 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
4571 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
4577 /* That wrote out all the local symbols. Finish up the symbol table
4578 with the global symbols. */
4580 /* The sh_info field records the index of the first non local
4582 symtab_hdr
->sh_info
= abfd
->symcount
;
4584 /* We get the global symbols from the hash table. */
4585 if (info
->strip
!= strip_all
)
4586 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
4589 /* Flush all symbols to the file. */
4590 if (! elf_link_flush_output_syms (&finfo
))
4593 /* Now we know the size of the symtab section. */
4594 off
+= symtab_hdr
->sh_size
;
4596 /* Finish up the symbol string table (.strtab) section. */
4597 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4598 /* sh_name was set in prep_headers. */
4599 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4600 symstrtab_hdr
->sh_flags
= 0;
4601 symstrtab_hdr
->sh_addr
= 0;
4602 symstrtab_hdr
->sh_size
= finfo
.symstrtab
->length
;
4603 symstrtab_hdr
->sh_entsize
= 0;
4604 symstrtab_hdr
->sh_link
= 0;
4605 symstrtab_hdr
->sh_info
= 0;
4606 /* sh_offset is set just below. */
4607 symstrtab_hdr
->sh_addralign
= 1;
4608 symstrtab_hdr
->contents
= (PTR
) finfo
.symstrtab
->tab
;
4610 off
= assign_file_position_for_section (symstrtab_hdr
, off
);
4611 elf_tdata (abfd
)->next_file_pos
= off
;
4613 /* Adjust the relocs to have the correct symbol indices. */
4614 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4616 struct elf_link_hash_entry
**rel_hash
;
4617 Elf_Internal_Shdr
*rel_hdr
;
4620 if ((o
->flags
& SEC_RELOC
) == 0)
4623 rel_hash
= elf_section_data (o
)->rel_hashes
;
4624 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
4625 for (i
= 0; i
< o
->reloc_count
; i
++, rel_hash
++)
4627 if (*rel_hash
== NULL
)
4630 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
4632 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
4634 Elf_External_Rel
*erel
;
4635 Elf_Internal_Rel irel
;
4637 erel
= (Elf_External_Rel
*) rel_hdr
->contents
+ i
;
4638 elf_swap_reloc_in (abfd
, erel
, &irel
);
4639 irel
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4640 ELF_R_TYPE (irel
.r_info
));
4641 elf_swap_reloc_out (abfd
, &irel
, erel
);
4645 Elf_External_Rela
*erela
;
4646 Elf_Internal_Rela irela
;
4648 BFD_ASSERT (rel_hdr
->sh_entsize
4649 == sizeof (Elf_External_Rela
));
4651 erela
= (Elf_External_Rela
*) rel_hdr
->contents
+ i
;
4652 elf_swap_reloca_in (abfd
, erela
, &irela
);
4653 irela
.r_info
= ELF_R_INFO ((*rel_hash
)->indx
,
4654 ELF_R_TYPE (irela
.r_info
));
4655 elf_swap_reloca_out (abfd
, &irela
, erela
);
4659 /* Set the reloc_count field to 0 to prevent write_relocs from
4660 trying to swap the relocs out itself. */
4664 if (finfo
.contents
!= NULL
)
4665 free (finfo
.contents
);
4666 if (finfo
.external_relocs
!= NULL
)
4667 free (finfo
.external_relocs
);
4668 if (finfo
.internal_relocs
!= NULL
)
4669 free (finfo
.internal_relocs
);
4670 if (finfo
.external_syms
!= NULL
)
4671 free (finfo
.external_syms
);
4672 if (finfo
.internal_syms
!= NULL
)
4673 free (finfo
.internal_syms
);
4674 if (finfo
.indices
!= NULL
)
4675 free (finfo
.indices
);
4676 if (finfo
.sections
!= NULL
)
4677 free (finfo
.sections
);
4678 if (finfo
.symbuf
!= NULL
)
4679 free (finfo
.symbuf
);
4680 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4682 if ((o
->flags
& SEC_RELOC
) != 0
4683 && elf_section_data (o
)->rel_hashes
!= NULL
)
4684 free (elf_section_data (o
)->rel_hashes
);
4690 if (finfo
.contents
!= NULL
)
4691 free (finfo
.contents
);
4692 if (finfo
.external_relocs
!= NULL
)
4693 free (finfo
.external_relocs
);
4694 if (finfo
.internal_relocs
!= NULL
)
4695 free (finfo
.internal_relocs
);
4696 if (finfo
.external_syms
!= NULL
)
4697 free (finfo
.external_syms
);
4698 if (finfo
.internal_syms
!= NULL
)
4699 free (finfo
.internal_syms
);
4700 if (finfo
.indices
!= NULL
)
4701 free (finfo
.indices
);
4702 if (finfo
.sections
!= NULL
)
4703 free (finfo
.sections
);
4704 if (finfo
.symbuf
!= NULL
)
4705 free (finfo
.symbuf
);
4706 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4708 if ((o
->flags
& SEC_RELOC
) != 0
4709 && elf_section_data (o
)->rel_hashes
!= NULL
)
4710 free (elf_section_data (o
)->rel_hashes
);
4716 /* Add a symbol to the output symbol table. */
4719 elf_link_output_sym (finfo
, name
, elfsym
)
4720 struct elf_final_link_info
*finfo
;
4722 Elf_Internal_Sym
*elfsym
;
4724 if (name
== (const char *) NULL
|| *name
== '\0')
4725 elfsym
->st_name
= 0;
4728 elfsym
->st_name
= bfd_add_to_strtab (finfo
->output_bfd
,
4729 finfo
->symstrtab
, name
);
4730 if (elfsym
->st_name
== (unsigned long) -1)
4734 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
4736 if (! elf_link_flush_output_syms (finfo
))
4740 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
4741 finfo
->symbuf
+ finfo
->symbuf_count
);
4742 ++finfo
->symbuf_count
;
4744 ++finfo
->output_bfd
->symcount
;
4749 /* Flush the output symbols to the file. */
4752 elf_link_flush_output_syms (finfo
)
4753 struct elf_final_link_info
*finfo
;
4755 Elf_Internal_Shdr
*symtab
;
4757 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
4759 if (bfd_seek (finfo
->output_bfd
, symtab
->sh_offset
+ symtab
->sh_size
,
4761 || (bfd_write ((PTR
) finfo
->symbuf
, finfo
->symbuf_count
,
4762 sizeof (Elf_External_Sym
), finfo
->output_bfd
)
4763 != finfo
->symbuf_count
* sizeof (Elf_External_Sym
)))
4766 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
4768 finfo
->symbuf_count
= 0;
4773 /* Add an external symbol to the symbol table. This is called from
4774 the hash table traversal routine. */
4777 elf_link_output_extsym (h
, data
)
4778 struct elf_link_hash_entry
*h
;
4781 struct elf_final_link_info
*finfo
= (struct elf_final_link_info
*) data
;
4782 Elf_Internal_Sym sym
;
4784 /* h->indx is set to -2 if this symbol is used by a reloc. */
4786 && finfo
->info
->strip
== strip_some
4787 && bfd_hash_lookup (finfo
->info
->keep_hash
, h
->root
.root
.string
,
4788 false, false) == NULL
)
4792 sym
.st_size
= h
->size
;
4793 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
4796 switch (h
->root
.type
)
4799 case bfd_link_hash_new
:
4803 case bfd_link_hash_undefined
:
4804 sym
.st_shndx
= SHN_UNDEF
;
4807 case bfd_link_hash_weak
:
4808 sym
.st_shndx
= SHN_UNDEF
;
4809 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
4812 case bfd_link_hash_defined
:
4816 sec
= h
->root
.u
.def
.section
;
4817 sym
.st_shndx
= elf_section_from_bfd_section (finfo
->output_bfd
,
4818 sec
->output_section
);
4819 if (sym
.st_shndx
== (unsigned long) -1)
4821 /* FIXME: No way to handle errors. */
4825 /* ELF symbols in relocateable files are section relative, but
4826 in nonrelocateable files they are virtual addresses. */
4827 sym
.st_value
= h
->root
.u
.def
.value
+ sec
->output_offset
;
4828 if (! finfo
->info
->relocateable
)
4829 sym
.st_value
+= sec
->output_section
->vma
;
4833 case bfd_link_hash_common
:
4834 sym
.st_shndx
= SHN_COMMON
;
4838 sym
.st_value
= h
->align
;
4841 case bfd_link_hash_indirect
:
4842 case bfd_link_hash_warning
:
4843 /* I have no idea how these should be handled. */
4847 h
->indx
= finfo
->output_bfd
->symcount
;
4849 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
))
4851 /* FIXME: No way to return error. */
4858 /* Link an input file into the linker output file. This function
4859 handles all the sections and relocations of the input file at once.
4860 This is so that we only have to read the local symbols once, and
4861 don't have to keep them in memory. */
4864 elf_link_input_bfd (finfo
, input_bfd
)
4865 struct elf_final_link_info
*finfo
;
4868 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
4869 bfd
*, asection
*, bfd_byte
*,
4870 Elf_Internal_Rela
*,
4874 Elf_Internal_Shdr
*symtab_hdr
;
4875 Elf_External_Sym
*esym
;
4876 Elf_External_Sym
*esymend
;
4877 Elf_Internal_Sym
*isym
;
4879 asection
**ppsection
;
4882 output_bfd
= finfo
->output_bfd
;
4884 get_elf_backend_data (output_bfd
)->elf_backend_relocate_section
;
4886 /* Read the local symbols. */
4887 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4888 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
4889 || (bfd_read (finfo
->external_syms
, sizeof (Elf_External_Sym
),
4890 symtab_hdr
->sh_info
, input_bfd
)
4891 != symtab_hdr
->sh_info
* sizeof (Elf_External_Sym
)))
4894 /* Swap in the local symbols and write out the ones which we know
4895 are going into the output file. */
4896 esym
= finfo
->external_syms
;
4897 esymend
= esym
+ symtab_hdr
->sh_info
;
4898 isym
= finfo
->internal_syms
;
4899 pindex
= finfo
->indices
;
4900 ppsection
= finfo
->sections
;
4901 for (; esym
< esymend
; esym
++, isym
++, pindex
++, ppsection
++)
4907 elf_swap_symbol_in (input_bfd
, esym
, isym
);
4910 if (isym
->st_shndx
== SHN_UNDEF
)
4911 isec
= &bfd_und_section
;
4912 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
4914 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
4918 else if (isym
->st_shndx
== SHN_ABS
)
4919 isec
= &bfd_abs_section
;
4920 else if (isym
->st_shndx
== SHN_COMMON
)
4921 isec
= &bfd_com_section
;
4930 /* Don't output the first, undefined, symbol. */
4931 if (esym
== finfo
->external_syms
)
4934 /* If we are stripping all symbols, we don't want to output this
4936 if (finfo
->info
->strip
== strip_all
)
4939 /* We never output section symbols. Instead, we use the section
4940 symbol of the corresponding section in the output file. */
4941 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
4944 /* If we are discarding all local symbols, we don't want to
4945 output this one. If we are generating a relocateable output
4946 file, then some of the local symbols may be required by
4947 relocs; we output them below as we discover that they are
4949 if (finfo
->info
->discard
== discard_all
)
4952 /* Get the name of the symbol. */
4953 name
= elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
4958 /* See if we are discarding symbols with this name. */
4959 if ((finfo
->info
->strip
== strip_some
4960 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
4962 || (finfo
->info
->discard
== discard_l
4963 && strncmp (name
, finfo
->info
->lprefix
,
4964 finfo
->info
->lprefix_len
) == 0))
4967 /* If we get here, we are going to output this symbol. */
4969 /* Adjust the section index for the output file. */
4970 isym
->st_shndx
= elf_section_from_bfd_section (output_bfd
,
4971 isec
->output_section
);
4972 if (isym
->st_shndx
== (unsigned long) -1)
4975 *pindex
= output_bfd
->symcount
;
4977 /* ELF symbols in relocateable files are section relative, but
4978 in executable files they are virtual addresses. Note that
4979 this code assumes that all ELF sections have an associated
4980 BFD section with a reasonable value for output_offset; below
4981 we assume that they also have a reasonable value for
4982 output_section. Any special sections must be set up to meet
4983 these requirements. */
4984 oldval
= isym
->st_value
;
4985 isym
->st_value
+= isec
->output_offset
;
4986 if (! finfo
->info
->relocateable
)
4987 isym
->st_value
+= isec
->output_section
->vma
;
4989 if (! elf_link_output_sym (finfo
, name
, isym
))
4992 /* Restore the old value for reloc handling. */
4993 isym
->st_value
= oldval
;
4996 /* Relocate the contents of each section. */
4997 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4999 Elf_Internal_Shdr
*input_rel_hdr
;
5001 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
5004 /* Read the contents of the section. */
5005 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
5006 (file_ptr
) 0, o
->_raw_size
))
5009 if ((o
->flags
& SEC_RELOC
) != 0)
5011 /* Read in the relocs. */
5012 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
5013 if (bfd_seek (input_bfd
, input_rel_hdr
->sh_offset
, SEEK_SET
) != 0
5014 || bfd_read (finfo
->external_relocs
, 1, input_rel_hdr
->sh_size
,
5015 input_bfd
) != input_rel_hdr
->sh_size
)
5018 /* Swap in the relocs. For convenience, we always produce
5019 an Elf_Internal_Rela array; if the relocs are Rel, we set
5021 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
5023 Elf_External_Rel
*erel
;
5024 Elf_External_Rel
*erelend
;
5025 Elf_Internal_Rela
*irela
;
5027 erel
= (Elf_External_Rel
*) finfo
->external_relocs
;
5028 erelend
= erel
+ o
->reloc_count
;
5029 irela
= finfo
->internal_relocs
;
5030 for (; erel
< erelend
; erel
++, irela
++)
5032 Elf_Internal_Rel irel
;
5034 elf_swap_reloc_in (input_bfd
, erel
, &irel
);
5035 irela
->r_offset
= irel
.r_offset
;
5036 irela
->r_info
= irel
.r_info
;
5037 irela
->r_addend
= 0;
5042 Elf_External_Rela
*erela
;
5043 Elf_External_Rela
*erelaend
;
5044 Elf_Internal_Rela
*irela
;
5046 BFD_ASSERT (input_rel_hdr
->sh_entsize
5047 == sizeof (Elf_External_Rela
));
5049 erela
= (Elf_External_Rela
*) finfo
->external_relocs
;
5050 erelaend
= erela
+ o
->reloc_count
;
5051 irela
= finfo
->internal_relocs
;
5052 for (; erela
< erelaend
; erela
++, irela
++)
5053 elf_swap_reloca_in (input_bfd
, erela
, irela
);
5056 /* Relocate the section by invoking a back end routine.
5058 The back end routine is responsible for adjusting the
5059 section contents as necessary, and (if using Rela relocs
5060 and generating a relocateable output file) adjusting the
5061 reloc addend as necessary.
5063 The back end routine does not have to worry about setting
5064 the reloc address or the reloc symbol index.
5066 The back end routine is given a pointer to the swapped in
5067 internal symbols, and can access the hash table entries
5068 for the external symbols via elf_sym_hashes (input_bfd).
5070 When generating relocateable output, the back end routine
5071 must handle STB_LOCAL/STT_SECTION symbols specially. The
5072 output symbol is going to be a section symbol
5073 corresponding to the output section, which will require
5074 the addend to be adjusted. */
5076 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
5079 finfo
->internal_relocs
,
5080 finfo
->internal_syms
,
5084 if (finfo
->info
->relocateable
)
5086 Elf_Internal_Rela
*irela
;
5087 Elf_Internal_Rela
*irelaend
;
5088 struct elf_link_hash_entry
**rel_hash
;
5089 Elf_Internal_Shdr
*output_rel_hdr
;
5091 /* Adjust the reloc addresses and symbol indices. */
5093 irela
= finfo
->internal_relocs
;
5094 irelaend
= irela
+ o
->reloc_count
;
5095 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
5096 + o
->output_section
->reloc_count
);
5097 for (; irela
< irelaend
; irela
++, rel_hash
++)
5100 Elf_Internal_Sym
*isym
;
5103 irela
->r_offset
+= o
->output_offset
;
5105 r_symndx
= ELF_R_SYM (irela
->r_info
);
5110 if (r_symndx
>= symtab_hdr
->sh_info
)
5114 /* This is a reloc against a global symbol. We
5115 have not yet output all the local symbols, so
5116 we do not know the symbol index of any global
5117 symbol. We set the rel_hash entry for this
5118 reloc to point to the global hash table entry
5119 for this symbol. The symbol index is then
5120 set at the end of elf_bfd_final_link. */
5121 indx
= r_symndx
- symtab_hdr
->sh_info
;
5122 *rel_hash
= elf_sym_hashes (input_bfd
)[indx
];
5124 /* Setting the index to -2 tells
5125 elf_link_output_extsym that this symbol is
5127 BFD_ASSERT ((*rel_hash
)->indx
< 0);
5128 (*rel_hash
)->indx
= -2;
5133 /* This is a reloc against a local symbol. */
5136 isym
= finfo
->internal_syms
+ r_symndx
;
5137 sec
= finfo
->sections
[r_symndx
];
5138 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
5140 /* I suppose the backend ought to fill in the
5141 section of any STT_SECTION symbol against a
5142 processor specific section. */
5144 || sec
== &bfd_und_section
5145 || sec
== &bfd_com_section
)
5147 bfd_set_error (bfd_error_bad_value
);
5150 else if (sec
== &bfd_abs_section
)
5153 r_symndx
= sec
->output_section
->index
;
5157 if (finfo
->indices
[r_symndx
] == -1)
5163 if (finfo
->info
->strip
== strip_all
)
5165 /* You can't do ld -r -s. */
5166 bfd_set_error (bfd_error_invalid_operation
);
5170 /* This symbol was skipped earlier, but
5171 since it is needed by a reloc, we
5172 must output it now. */
5173 link
= symtab_hdr
->sh_link
;
5174 name
= elf_string_from_elf_section (input_bfd
,
5180 osec
= sec
->output_section
;
5182 elf_section_from_bfd_section (output_bfd
,
5184 if (isym
->st_shndx
== (unsigned long) -1)
5187 isym
->st_value
+= sec
->output_offset
;
5188 if (! finfo
->info
->relocateable
)
5189 isym
->st_value
+= osec
->vma
;
5191 finfo
->indices
[r_symndx
] = output_bfd
->symcount
;
5193 if (! elf_link_output_sym (finfo
, name
, isym
))
5197 r_symndx
= finfo
->indices
[r_symndx
];
5200 irela
->r_info
= ELF_R_INFO (r_symndx
,
5201 ELF_R_TYPE (irela
->r_info
));
5204 /* Swap out the relocs. */
5205 output_rel_hdr
= &elf_section_data (o
->output_section
)->rel_hdr
;
5206 BFD_ASSERT (output_rel_hdr
->sh_entsize
5207 == input_rel_hdr
->sh_entsize
);
5208 irela
= finfo
->internal_relocs
;
5209 irelaend
= irela
+ o
->reloc_count
;
5210 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
5212 Elf_External_Rel
*erel
;
5214 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
5215 + o
->output_section
->reloc_count
);
5216 for (; irela
< irelaend
; irela
++, erel
++)
5218 Elf_Internal_Rel irel
;
5220 irel
.r_offset
= irela
->r_offset
;
5221 irel
.r_info
= irela
->r_info
;
5222 BFD_ASSERT (irela
->r_addend
== 0);
5223 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
5228 Elf_External_Rela
*erela
;
5230 BFD_ASSERT (input_rel_hdr
->sh_entsize
5231 == sizeof (Elf_External_Rela
));
5232 erela
= ((Elf_External_Rela
*) output_rel_hdr
->contents
5233 + o
->output_section
->reloc_count
);
5234 for (; irela
< irelaend
; irela
++, erela
++)
5235 elf_swap_reloca_out (output_bfd
, irela
, erela
);
5238 o
->output_section
->reloc_count
+= o
->reloc_count
;
5242 /* Write out the modified section contents. */
5243 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
5244 finfo
->contents
, o
->output_offset
,
5245 (o
->_cooked_size
!= 0
5254 /* Generate a reloc when linking an ELF file. This is a reloc
5255 requested by the linker, and does come from any input file. This
5256 is used to build constructor and destructor tables when linking
5260 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
5262 struct bfd_link_info
*info
;
5263 asection
*output_section
;
5264 struct bfd_link_order
*link_order
;
5266 const reloc_howto_type
*howto
;
5269 struct elf_link_hash_entry
**rel_hash_ptr
;
5270 Elf_Internal_Shdr
*rel_hdr
;
5272 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5275 bfd_set_error (bfd_error_bad_value
);
5279 /* If this is an inplace reloc, we must write the addend into the
5281 if (howto
->partial_inplace
5282 && link_order
->u
.reloc
.p
->addend
!= 0)
5285 bfd_reloc_status_type rstat
;
5289 size
= bfd_get_reloc_size (howto
);
5290 buf
= (bfd_byte
*) bfd_zmalloc (size
);
5291 if (buf
== (bfd_byte
*) NULL
)
5293 bfd_set_error (bfd_error_no_memory
);
5296 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
5297 link_order
->u
.reloc
.p
->addend
, buf
);
5303 case bfd_reloc_outofrange
:
5305 case bfd_reloc_overflow
:
5306 if (! ((*info
->callbacks
->reloc_overflow
)
5308 (link_order
->type
== bfd_section_reloc_link_order
5309 ? bfd_section_name (output_bfd
,
5310 link_order
->u
.reloc
.p
->u
.section
)
5311 : link_order
->u
.reloc
.p
->u
.name
),
5312 howto
->name
, link_order
->u
.reloc
.p
->addend
,
5313 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
5320 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
5321 (file_ptr
) link_order
->offset
, size
);
5327 /* Figure out the symbol index. */
5328 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
5329 + output_section
->reloc_count
);
5330 if (link_order
->type
== bfd_section_reloc_link_order
)
5332 indx
= link_order
->u
.reloc
.p
->u
.section
->index
;
5333 *rel_hash_ptr
= NULL
;
5337 struct elf_link_hash_entry
*h
;
5339 h
= elf_link_hash_lookup (elf_hash_table (info
),
5340 link_order
->u
.reloc
.p
->u
.name
,
5341 false, false, true);
5344 /* Setting the index to -2 tells elf_link_output_extsym that
5345 this symbol is used by a reloc. */
5352 if (! ((*info
->callbacks
->unattached_reloc
)
5353 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
5354 (asection
*) NULL
, (bfd_vma
) 0)))
5360 /* The address of a reloc is relative to the section in a
5361 relocateable file, and is a virtual address in an executable
5363 offset
= link_order
->offset
;
5364 if (! info
->relocateable
)
5365 offset
+= output_section
->vma
;
5367 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
5369 if (rel_hdr
->sh_type
== SHT_REL
)
5371 Elf_Internal_Rel irel
;
5372 Elf_External_Rel
*erel
;
5374 irel
.r_offset
= offset
;
5375 irel
.r_info
= ELF_R_INFO (indx
, howto
->type
);
5376 erel
= ((Elf_External_Rel
*) rel_hdr
->contents
5377 + output_section
->reloc_count
);
5378 elf_swap_reloc_out (output_bfd
, &irel
, erel
);
5382 Elf_Internal_Rela irela
;
5383 Elf_External_Rela
*erela
;
5385 irela
.r_offset
= offset
;
5386 irela
.r_info
= ELF_R_INFO (indx
, howto
->type
);
5387 irela
.r_addend
= link_order
->u
.reloc
.p
->addend
;
5388 erela
= ((Elf_External_Rela
*) rel_hdr
->contents
5389 + output_section
->reloc_count
);
5390 elf_swap_reloca_out (output_bfd
, &irela
, erela
);
5393 ++output_section
->reloc_count
;