Commit | Line | Data |
---|---|---|
244ffee7 | 1 | /* ELF executable support for BFD. |
b9d5cdf0 | 2 | Copyright 1991, 1992, 1993, 1994 Free Software Foundation, Inc. |
244ffee7 JK |
3 | |
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. | |
7 | ||
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. | |
15 | ||
16 | This file is part of BFD, the Binary File Descriptor library. | |
17 | ||
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. | |
22 | ||
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. | |
27 | ||
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. */ | |
31 | ||
244ffee7 JK |
32 | /* Problems and other issues to resolve. |
33 | ||
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. | |
41 | ||
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 | |
46 | "sections". | |
47 | ||
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) | |
52 | ||
6a3eb9b6 KR |
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 | |
55 | and moved into elf.c. | |
56 | ||
d24928c0 KR |
57 | (3) ELF section symbols are handled rather sloppily now. This should |
58 | be cleaned up, and ELF section symbols reconciled with BFD section | |
59 | symbols. | |
60 | */ | |
244ffee7 JK |
61 | |
62 | #include <string.h> /* For strrchr and friends */ | |
63 | #include "bfd.h" | |
64 | #include "sysdep.h" | |
65 | #include "libbfd.h" | |
66 | #include "libelf.h" | |
67 | ||
32090b8e | 68 | /* Renaming structures, typedefs, macros and functions to be size-specific. */ |
244ffee7 | 69 | #define Elf_External_Ehdr NAME(Elf,External_Ehdr) |
244ffee7 | 70 | #define Elf_External_Sym NAME(Elf,External_Sym) |
244ffee7 | 71 | #define Elf_External_Shdr NAME(Elf,External_Shdr) |
244ffee7 | 72 | #define Elf_External_Phdr NAME(Elf,External_Phdr) |
244ffee7 JK |
73 | #define Elf_External_Rel NAME(Elf,External_Rel) |
74 | #define Elf_External_Rela NAME(Elf,External_Rela) | |
244ffee7 | 75 | |
244ffee7 JK |
76 | #define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command) |
77 | #define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal) | |
78 | #define elf_core_file_matches_executable_p NAME(bfd_elf,core_file_matches_executable_p) | |
79 | #define elf_object_p NAME(bfd_elf,object_p) | |
80 | #define elf_core_file_p NAME(bfd_elf,core_file_p) | |
244ffee7 JK |
81 | #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound) |
82 | #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound) | |
83 | #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc) | |
84 | #define elf_get_symtab NAME(bfd_elf,get_symtab) | |
85 | #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol) | |
86 | #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info) | |
87 | #define elf_print_symbol NAME(bfd_elf,print_symbol) | |
88 | #define elf_get_lineno NAME(bfd_elf,get_lineno) | |
89 | #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach) | |
90 | #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line) | |
91 | #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers) | |
92 | #define elf_set_section_contents NAME(bfd_elf,set_section_contents) | |
93 | #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto) | |
94 | #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel) | |
fce36137 | 95 | #define elf_new_section_hook NAME(bfd_elf,new_section_hook) |
32090b8e | 96 | #define write_relocs NAME(bfd_elf,_write_relocs) |
f035cc47 | 97 | #define elf_find_section NAME(bfd_elf,find_section) |
244ffee7 | 98 | |
6a3eb9b6 KR |
99 | #if ARCH_SIZE == 64 |
100 | #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y) | |
101 | #define ELF_R_SYM(X) ELF64_R_SYM(X) | |
32090b8e | 102 | #define ELFCLASS ELFCLASS64 |
f035cc47 | 103 | #define FILE_ALIGN 8 |
6a3eb9b6 KR |
104 | #endif |
105 | #if ARCH_SIZE == 32 | |
106 | #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y) | |
107 | #define ELF_R_SYM(X) ELF32_R_SYM(X) | |
32090b8e | 108 | #define ELFCLASS ELFCLASS32 |
f035cc47 | 109 | #define FILE_ALIGN 4 |
244ffee7 JK |
110 | #endif |
111 | ||
32090b8e KR |
112 | static int shstrtab_length_fixed; |
113 | ||
1c6042ee ILT |
114 | struct elf_sect_data |
115 | { | |
116 | int reloc_sec; | |
117 | /* more? */ | |
118 | }; | |
32090b8e | 119 | |
244ffee7 JK |
120 | /* Forward declarations of static functions */ |
121 | ||
1c6042ee | 122 | static struct sec *section_from_elf_index PARAMS ((bfd *, unsigned int)); |
244ffee7 JK |
123 | |
124 | static int elf_section_from_bfd_section PARAMS ((bfd *, struct sec *)); | |
125 | ||
126 | static boolean elf_slurp_symbol_table PARAMS ((bfd *, asymbol **)); | |
127 | ||
244ffee7 | 128 | static int elf_symbol_from_bfd_symbol PARAMS ((bfd *, |
1c6042ee | 129 | struct symbol_cache_entry **)); |
244ffee7 | 130 | |
9783e04a | 131 | static boolean elf_map_symbols PARAMS ((bfd *)); |
b9d5cdf0 | 132 | static boolean swap_out_syms PARAMS ((bfd *)); |
244ffee7 | 133 | |
6a3eb9b6 KR |
134 | #ifdef DEBUG |
135 | static void elf_debug_section PARAMS ((char *, int, Elf_Internal_Shdr *)); | |
136 | static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *)); | |
137 | #endif | |
238ac6ec | 138 | |
32090b8e KR |
139 | #define elf_string_from_elf_strtab(abfd,strindex) \ |
140 | elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex) | |
32090b8e | 141 | \f |
1c6042ee | 142 | |
32090b8e KR |
143 | /* Structure swapping routines */ |
144 | ||
6a3eb9b6 KR |
145 | /* Should perhaps use put_offset, put_word, etc. For now, the two versions |
146 | can be handled by explicitly specifying 32 bits or "the long type". */ | |
238ac6ec KR |
147 | #if ARCH_SIZE == 64 |
148 | #define put_word bfd_h_put_64 | |
149 | #define get_word bfd_h_get_64 | |
150 | #endif | |
151 | #if ARCH_SIZE == 32 | |
152 | #define put_word bfd_h_put_32 | |
153 | #define get_word bfd_h_get_32 | |
154 | #endif | |
155 | ||
244ffee7 JK |
156 | /* Translate an ELF symbol in external format into an ELF symbol in internal |
157 | format. */ | |
158 | ||
159 | static void | |
1c6042ee ILT |
160 | elf_swap_symbol_in (abfd, src, dst) |
161 | bfd *abfd; | |
162 | Elf_External_Sym *src; | |
163 | Elf_Internal_Sym *dst; | |
244ffee7 JK |
164 | { |
165 | dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name); | |
238ac6ec KR |
166 | dst->st_value = get_word (abfd, (bfd_byte *) src->st_value); |
167 | dst->st_size = get_word (abfd, (bfd_byte *) src->st_size); | |
244ffee7 JK |
168 | dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info); |
169 | dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other); | |
170 | dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx); | |
171 | } | |
172 | ||
173 | /* Translate an ELF symbol in internal format into an ELF symbol in external | |
174 | format. */ | |
175 | ||
176 | static void | |
1c6042ee ILT |
177 | elf_swap_symbol_out (abfd, src, dst) |
178 | bfd *abfd; | |
179 | Elf_Internal_Sym *src; | |
180 | Elf_External_Sym *dst; | |
244ffee7 JK |
181 | { |
182 | bfd_h_put_32 (abfd, src->st_name, dst->st_name); | |
238ac6ec KR |
183 | put_word (abfd, src->st_value, dst->st_value); |
184 | put_word (abfd, src->st_size, dst->st_size); | |
244ffee7 JK |
185 | bfd_h_put_8 (abfd, src->st_info, dst->st_info); |
186 | bfd_h_put_8 (abfd, src->st_other, dst->st_other); | |
187 | bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx); | |
188 | } | |
189 | ||
190 | ||
191 | /* Translate an ELF file header in external format into an ELF file header in | |
192 | internal format. */ | |
193 | ||
194 | static void | |
1c6042ee ILT |
195 | elf_swap_ehdr_in (abfd, src, dst) |
196 | bfd *abfd; | |
197 | Elf_External_Ehdr *src; | |
198 | Elf_Internal_Ehdr *dst; | |
244ffee7 JK |
199 | { |
200 | memcpy (dst->e_ident, src->e_ident, EI_NIDENT); | |
201 | dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type); | |
202 | dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine); | |
203 | dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version); | |
238ac6ec KR |
204 | dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry); |
205 | dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff); | |
206 | dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff); | |
244ffee7 JK |
207 | dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags); |
208 | dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize); | |
209 | dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize); | |
210 | dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum); | |
211 | dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize); | |
212 | dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum); | |
213 | dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx); | |
214 | } | |
215 | ||
216 | /* Translate an ELF file header in internal format into an ELF file header in | |
217 | external format. */ | |
218 | ||
219 | static void | |
1c6042ee ILT |
220 | elf_swap_ehdr_out (abfd, src, dst) |
221 | bfd *abfd; | |
222 | Elf_Internal_Ehdr *src; | |
223 | Elf_External_Ehdr *dst; | |
244ffee7 JK |
224 | { |
225 | memcpy (dst->e_ident, src->e_ident, EI_NIDENT); | |
226 | /* note that all elements of dst are *arrays of unsigned char* already... */ | |
227 | bfd_h_put_16 (abfd, src->e_type, dst->e_type); | |
228 | bfd_h_put_16 (abfd, src->e_machine, dst->e_machine); | |
229 | bfd_h_put_32 (abfd, src->e_version, dst->e_version); | |
238ac6ec KR |
230 | put_word (abfd, src->e_entry, dst->e_entry); |
231 | put_word (abfd, src->e_phoff, dst->e_phoff); | |
232 | put_word (abfd, src->e_shoff, dst->e_shoff); | |
244ffee7 JK |
233 | bfd_h_put_32 (abfd, src->e_flags, dst->e_flags); |
234 | bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize); | |
235 | bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize); | |
236 | bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum); | |
237 | bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize); | |
238 | bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum); | |
239 | bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx); | |
240 | } | |
241 | ||
242 | ||
243 | /* Translate an ELF section header table entry in external format into an | |
244 | ELF section header table entry in internal format. */ | |
245 | ||
246 | static void | |
1c6042ee ILT |
247 | elf_swap_shdr_in (abfd, src, dst) |
248 | bfd *abfd; | |
249 | Elf_External_Shdr *src; | |
250 | Elf_Internal_Shdr *dst; | |
244ffee7 JK |
251 | { |
252 | dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name); | |
253 | dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type); | |
238ac6ec KR |
254 | dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags); |
255 | dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr); | |
256 | dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset); | |
257 | dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size); | |
244ffee7 JK |
258 | dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link); |
259 | dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info); | |
238ac6ec KR |
260 | dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign); |
261 | dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize); | |
244ffee7 JK |
262 | /* we haven't done any processing on it yet, so... */ |
263 | dst->rawdata = (void *) 0; | |
264 | } | |
265 | ||
266 | /* Translate an ELF section header table entry in internal format into an | |
267 | ELF section header table entry in external format. */ | |
268 | ||
269 | static void | |
1c6042ee ILT |
270 | elf_swap_shdr_out (abfd, src, dst) |
271 | bfd *abfd; | |
272 | Elf_Internal_Shdr *src; | |
273 | Elf_External_Shdr *dst; | |
244ffee7 JK |
274 | { |
275 | /* note that all elements of dst are *arrays of unsigned char* already... */ | |
276 | bfd_h_put_32 (abfd, src->sh_name, dst->sh_name); | |
277 | bfd_h_put_32 (abfd, src->sh_type, dst->sh_type); | |
238ac6ec KR |
278 | put_word (abfd, src->sh_flags, dst->sh_flags); |
279 | put_word (abfd, src->sh_addr, dst->sh_addr); | |
280 | put_word (abfd, src->sh_offset, dst->sh_offset); | |
281 | put_word (abfd, src->sh_size, dst->sh_size); | |
244ffee7 JK |
282 | bfd_h_put_32 (abfd, src->sh_link, dst->sh_link); |
283 | bfd_h_put_32 (abfd, src->sh_info, dst->sh_info); | |
238ac6ec KR |
284 | put_word (abfd, src->sh_addralign, dst->sh_addralign); |
285 | put_word (abfd, src->sh_entsize, dst->sh_entsize); | |
244ffee7 JK |
286 | } |
287 | ||
288 | ||
289 | /* Translate an ELF program header table entry in external format into an | |
290 | ELF program header table entry in internal format. */ | |
291 | ||
292 | static void | |
1c6042ee ILT |
293 | elf_swap_phdr_in (abfd, src, dst) |
294 | bfd *abfd; | |
295 | Elf_External_Phdr *src; | |
296 | Elf_Internal_Phdr *dst; | |
244ffee7 JK |
297 | { |
298 | dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type); | |
244ffee7 | 299 | dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags); |
238ac6ec KR |
300 | dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset); |
301 | dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr); | |
302 | dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr); | |
303 | dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz); | |
304 | dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz); | |
305 | dst->p_align = get_word (abfd, (bfd_byte *) src->p_align); | |
244ffee7 JK |
306 | } |
307 | ||
244ffee7 | 308 | static void |
1c6042ee ILT |
309 | elf_swap_phdr_out (abfd, src, dst) |
310 | bfd *abfd; | |
311 | Elf_Internal_Phdr *src; | |
312 | Elf_External_Phdr *dst; | |
244ffee7 JK |
313 | { |
314 | /* note that all elements of dst are *arrays of unsigned char* already... */ | |
315 | bfd_h_put_32 (abfd, src->p_type, dst->p_type); | |
94dbb655 KR |
316 | put_word (abfd, src->p_offset, dst->p_offset); |
317 | put_word (abfd, src->p_vaddr, dst->p_vaddr); | |
318 | put_word (abfd, src->p_paddr, dst->p_paddr); | |
319 | put_word (abfd, src->p_filesz, dst->p_filesz); | |
320 | put_word (abfd, src->p_memsz, dst->p_memsz); | |
244ffee7 | 321 | bfd_h_put_32 (abfd, src->p_flags, dst->p_flags); |
94dbb655 | 322 | put_word (abfd, src->p_align, dst->p_align); |
244ffee7 JK |
323 | } |
324 | ||
325 | /* Translate an ELF reloc from external format to internal format. */ | |
32090b8e | 326 | static INLINE void |
1c6042ee ILT |
327 | elf_swap_reloc_in (abfd, src, dst) |
328 | bfd *abfd; | |
329 | Elf_External_Rel *src; | |
330 | Elf_Internal_Rel *dst; | |
244ffee7 | 331 | { |
94dbb655 KR |
332 | dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset); |
333 | dst->r_info = get_word (abfd, (bfd_byte *) src->r_info); | |
244ffee7 JK |
334 | } |
335 | ||
32090b8e | 336 | static INLINE void |
1c6042ee ILT |
337 | elf_swap_reloca_in (abfd, src, dst) |
338 | bfd *abfd; | |
339 | Elf_External_Rela *src; | |
340 | Elf_Internal_Rela *dst; | |
244ffee7 | 341 | { |
94dbb655 KR |
342 | dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset); |
343 | dst->r_info = get_word (abfd, (bfd_byte *) src->r_info); | |
344 | dst->r_addend = get_word (abfd, (bfd_byte *) src->r_addend); | |
244ffee7 JK |
345 | } |
346 | ||
347 | /* Translate an ELF reloc from internal format to external format. */ | |
32090b8e | 348 | static INLINE void |
1c6042ee ILT |
349 | elf_swap_reloc_out (abfd, src, dst) |
350 | bfd *abfd; | |
351 | Elf_Internal_Rel *src; | |
352 | Elf_External_Rel *dst; | |
244ffee7 | 353 | { |
94dbb655 KR |
354 | put_word (abfd, src->r_offset, dst->r_offset); |
355 | put_word (abfd, src->r_info, dst->r_info); | |
244ffee7 JK |
356 | } |
357 | ||
32090b8e | 358 | static INLINE void |
1c6042ee ILT |
359 | elf_swap_reloca_out (abfd, src, dst) |
360 | bfd *abfd; | |
361 | Elf_Internal_Rela *src; | |
362 | Elf_External_Rela *dst; | |
244ffee7 | 363 | { |
94dbb655 KR |
364 | put_word (abfd, src->r_offset, dst->r_offset); |
365 | put_word (abfd, src->r_info, dst->r_info); | |
366 | put_word (abfd, src->r_addend, dst->r_addend); | |
244ffee7 | 367 | } |
32090b8e KR |
368 | \f |
369 | ||
1c6042ee | 370 | |
32090b8e KR |
371 | /* String table creation/manipulation routines */ |
372 | ||
373 | static struct strtab * | |
1c6042ee ILT |
374 | bfd_new_strtab (abfd) |
375 | bfd *abfd; | |
32090b8e KR |
376 | { |
377 | struct strtab *ss; | |
378 | ||
b9d5cdf0 DM |
379 | ss = (struct strtab *) malloc (sizeof (struct strtab)); |
380 | if (!ss) | |
381 | { | |
d1ad85a6 | 382 | bfd_set_error (bfd_error_no_memory); |
b9d5cdf0 DM |
383 | return NULL; |
384 | } | |
385 | ss->tab = malloc (1); | |
386 | if (!ss->tab) | |
387 | { | |
d1ad85a6 | 388 | bfd_set_error (bfd_error_no_memory); |
b9d5cdf0 DM |
389 | return NULL; |
390 | } | |
32090b8e KR |
391 | *ss->tab = 0; |
392 | ss->nentries = 0; | |
393 | ss->length = 1; | |
244ffee7 | 394 | |
32090b8e KR |
395 | return ss; |
396 | } | |
397 | ||
398 | static int | |
1c6042ee ILT |
399 | bfd_add_to_strtab (abfd, ss, str) |
400 | bfd *abfd; | |
401 | struct strtab *ss; | |
402 | CONST char *str; | |
32090b8e KR |
403 | { |
404 | /* should search first, but for now: */ | |
405 | /* include the trailing NUL */ | |
406 | int ln = strlen (str) + 1; | |
407 | ||
408 | /* should this be using obstacks? */ | |
409 | ss->tab = realloc (ss->tab, ss->length + ln); | |
410 | ||
9783e04a | 411 | BFD_ASSERT (ss->tab != 0); /* FIXME */ |
32090b8e KR |
412 | strcpy (ss->tab + ss->length, str); |
413 | ss->nentries++; | |
414 | ss->length += ln; | |
415 | ||
416 | return ss->length - ln; | |
417 | } | |
418 | ||
419 | static int | |
1c6042ee ILT |
420 | bfd_add_2_to_strtab (abfd, ss, str, str2) |
421 | bfd *abfd; | |
422 | struct strtab *ss; | |
423 | char *str; | |
424 | CONST char *str2; | |
244ffee7 | 425 | { |
32090b8e KR |
426 | /* should search first, but for now: */ |
427 | /* include the trailing NUL */ | |
428 | int ln = strlen (str) + strlen (str2) + 1; | |
429 | ||
430 | /* should this be using obstacks? */ | |
431 | if (ss->length) | |
432 | ss->tab = realloc (ss->tab, ss->length + ln); | |
433 | else | |
b9d5cdf0 | 434 | ss->tab = malloc (ln); |
32090b8e | 435 | |
9783e04a | 436 | BFD_ASSERT (ss->tab != 0); /* FIXME */ |
32090b8e KR |
437 | strcpy (ss->tab + ss->length, str); |
438 | strcpy (ss->tab + ss->length + strlen (str), str2); | |
439 | ss->nentries++; | |
440 | ss->length += ln; | |
441 | ||
442 | return ss->length - ln; | |
244ffee7 | 443 | } |
32090b8e | 444 | \f |
1c6042ee | 445 | |
32090b8e KR |
446 | /* ELF .o/exec file reading */ |
447 | ||
448 | /* Create a new bfd section from an ELF section header. */ | |
449 | ||
244ffee7 | 450 | static boolean |
1c6042ee ILT |
451 | bfd_section_from_shdr (abfd, shindex) |
452 | bfd *abfd; | |
453 | unsigned int shindex; | |
244ffee7 | 454 | { |
32090b8e KR |
455 | Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex]; |
456 | Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); | |
244ffee7 JK |
457 | asection *newsect; |
458 | char *name; | |
459 | ||
460 | name = elf_string_from_elf_strtab (abfd, hdr->sh_name); | |
461 | ||
462 | switch (hdr->sh_type) | |
463 | { | |
464 | ||
465 | case SHT_NULL: | |
466 | /* inactive section. Throw it away. */ | |
467 | return true; | |
468 | ||
469 | case SHT_PROGBITS: | |
25677b5b | 470 | case SHT_DYNAMIC: |
244ffee7 JK |
471 | /* Bits that get saved. This one is real. */ |
472 | if (!hdr->rawdata) | |
473 | { | |
474 | newsect = bfd_make_section (abfd, name); | |
475 | if (newsect != NULL) | |
476 | { | |
1c6042ee | 477 | newsect->filepos = hdr->sh_offset; /* so we can read back the bits */ |
32090b8e | 478 | newsect->flags |= SEC_HAS_CONTENTS; |
244ffee7 JK |
479 | newsect->vma = hdr->sh_addr; |
480 | newsect->_raw_size = hdr->sh_size; | |
6a3eb9b6 | 481 | newsect->alignment_power = bfd_log2 (hdr->sh_addralign); |
244ffee7 JK |
482 | |
483 | if (hdr->sh_flags & SHF_ALLOC) | |
484 | { | |
485 | newsect->flags |= SEC_ALLOC; | |
486 | newsect->flags |= SEC_LOAD; | |
487 | } | |
488 | ||
489 | if (!(hdr->sh_flags & SHF_WRITE)) | |
490 | newsect->flags |= SEC_READONLY; | |
491 | ||
492 | if (hdr->sh_flags & SHF_EXECINSTR) | |
1c6042ee | 493 | newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */ |
36d541b1 | 494 | else if (newsect->flags & SEC_ALLOC) |
244ffee7 JK |
495 | newsect->flags |= SEC_DATA; |
496 | ||
d6e5f950 ILT |
497 | /* The debugging sections appear to recognized only by |
498 | name. */ | |
499 | if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0 | |
500 | || strncmp (name, ".line", sizeof ".line" - 1) == 0 | |
501 | || strncmp (name, ".stab", sizeof ".stab" - 1) == 0) | |
502 | newsect->flags |= SEC_DEBUGGING; | |
503 | ||
244ffee7 JK |
504 | hdr->rawdata = (void *) newsect; |
505 | } | |
94dbb655 KR |
506 | else |
507 | hdr->rawdata = (void *) bfd_get_section_by_name (abfd, name); | |
244ffee7 JK |
508 | } |
509 | return true; | |
510 | ||
511 | case SHT_NOBITS: | |
512 | /* Bits that get saved. This one is real. */ | |
513 | if (!hdr->rawdata) | |
514 | { | |
515 | newsect = bfd_make_section (abfd, name); | |
516 | if (newsect != NULL) | |
517 | { | |
518 | newsect->vma = hdr->sh_addr; | |
519 | newsect->_raw_size = hdr->sh_size; | |
520 | newsect->filepos = hdr->sh_offset; /* fake */ | |
6a3eb9b6 | 521 | newsect->alignment_power = bfd_log2 (hdr->sh_addralign); |
244ffee7 JK |
522 | if (hdr->sh_flags & SHF_ALLOC) |
523 | newsect->flags |= SEC_ALLOC; | |
524 | ||
525 | if (!(hdr->sh_flags & SHF_WRITE)) | |
526 | newsect->flags |= SEC_READONLY; | |
527 | ||
36d541b1 ILT |
528 | /* FIXME: This section is empty. Does it really make |
529 | sense to set SEC_CODE for it? */ | |
244ffee7 JK |
530 | if (hdr->sh_flags & SHF_EXECINSTR) |
531 | newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */ | |
244ffee7 JK |
532 | |
533 | hdr->rawdata = (void *) newsect; | |
534 | } | |
535 | } | |
536 | return true; | |
537 | ||
538 | case SHT_SYMTAB: /* A symbol table */ | |
32090b8e KR |
539 | if (elf_onesymtab (abfd) == shindex) |
540 | return true; | |
541 | ||
244ffee7 | 542 | BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym)); |
32090b8e | 543 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
244ffee7 | 544 | elf_onesymtab (abfd) = shindex; |
1c6042ee ILT |
545 | elf_tdata (abfd)->symtab_hdr = *hdr; |
546 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_hdr; | |
244ffee7 JK |
547 | abfd->flags |= HAS_SYMS; |
548 | return true; | |
549 | ||
550 | case SHT_STRTAB: /* A string table */ | |
32090b8e | 551 | if (hdr->rawdata) |
fce36137 | 552 | return true; |
32090b8e KR |
553 | if (ehdr->e_shstrndx == shindex) |
554 | { | |
1c6042ee ILT |
555 | elf_tdata (abfd)->shstrtab_hdr = *hdr; |
556 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
557 | hdr->rawdata = (PTR) & elf_tdata (abfd)->shstrtab_hdr; | |
32090b8e KR |
558 | return true; |
559 | } | |
560 | { | |
68241b2b | 561 | unsigned int i; |
fce36137 | 562 | |
32090b8e KR |
563 | for (i = 1; i < ehdr->e_shnum; i++) |
564 | { | |
1c6042ee | 565 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; |
32090b8e KR |
566 | if (hdr2->sh_link == shindex) |
567 | { | |
568 | bfd_section_from_shdr (abfd, i); | |
569 | if (elf_onesymtab (abfd) == i) | |
570 | { | |
1c6042ee ILT |
571 | elf_tdata (abfd)->strtab_hdr = *hdr; |
572 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
32090b8e KR |
573 | return true; |
574 | } | |
1c6042ee ILT |
575 | #if 0 /* Not handling other string tables specially right now. */ |
576 | hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */ | |
32090b8e KR |
577 | /* We have a strtab for some random other section. */ |
578 | newsect = (asection *) hdr2->rawdata; | |
579 | if (!newsect) | |
580 | break; | |
581 | hdr->rawdata = (PTR) newsect; | |
582 | hdr2 = &elf_section_data (newsect)->str_hdr; | |
583 | *hdr2 = *hdr; | |
1c6042ee | 584 | elf_elfsections (abfd)[shindex] = hdr2; |
32090b8e KR |
585 | #endif |
586 | } | |
587 | } | |
588 | } | |
589 | ||
590 | newsect = bfd_make_section (abfd, name); | |
591 | if (newsect) | |
fce36137 | 592 | { |
32090b8e KR |
593 | newsect->flags = SEC_HAS_CONTENTS; |
594 | hdr->rawdata = (PTR) newsect; | |
595 | newsect->_raw_size = hdr->sh_size; | |
9783e04a DM |
596 | newsect->alignment_power = bfd_log2 (hdr->sh_addralign); |
597 | newsect->vma = hdr->sh_addr; | |
f035cc47 | 598 | newsect->filepos = hdr->sh_offset; |
32090b8e KR |
599 | |
600 | if (hdr->sh_flags & SHF_ALLOC) | |
1c6042ee | 601 | newsect->flags |= SEC_ALLOC | SEC_LOAD; |
32090b8e KR |
602 | if (!(hdr->sh_flags & SHF_WRITE)) |
603 | newsect->flags |= SEC_READONLY; | |
604 | if (hdr->sh_flags & SHF_EXECINSTR) | |
605 | newsect->flags |= SEC_CODE; | |
36d541b1 | 606 | else if (newsect->flags & SEC_ALLOC) |
32090b8e | 607 | newsect->flags |= SEC_DATA; |
01383fb4 KR |
608 | |
609 | /* Check for debugging string tables. */ | |
610 | if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0 | |
611 | || strncmp (name, ".stab", sizeof ".stab" - 1) == 0) | |
612 | newsect->flags |= SEC_DEBUGGING; | |
fce36137 KR |
613 | } |
614 | ||
244ffee7 JK |
615 | return true; |
616 | ||
617 | case SHT_REL: | |
618 | case SHT_RELA: | |
32090b8e KR |
619 | /* *These* do a lot of work -- but build no sections! |
620 | The spec says there can be multiple strtabs, but only one symtab, | |
621 | but there can be lots of REL* sections. */ | |
244ffee7 | 622 | /* FIXME: The above statement is wrong! There are typically at least |
32090b8e KR |
623 | two symbol tables in a dynamically linked executable, ".dynsym" |
624 | which is the dynamic linkage symbol table and ".symtab", which is | |
625 | the "traditional" symbol table. -fnf */ | |
244ffee7 JK |
626 | |
627 | { | |
628 | asection *target_sect; | |
32090b8e | 629 | Elf_Internal_Shdr *hdr2; |
244ffee7 JK |
630 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; |
631 | ||
632 | /* Don't allow REL relocations on a machine that uses RELA and | |
633 | vice versa. */ | |
634 | /* @@ Actually, the generic ABI does suggest that both might be | |
635 | used in one file. But the four ABI Processor Supplements I | |
636 | have access to right now all specify that only one is used on | |
637 | each of those architectures. It's conceivable that, e.g., a | |
638 | bunch of absolute 32-bit relocs might be more compact in REL | |
639 | form even on a RELA machine... */ | |
640 | BFD_ASSERT (!(use_rela_p && (hdr->sh_type == SHT_REL))); | |
641 | BFD_ASSERT (!(!use_rela_p && (hdr->sh_type == SHT_RELA))); | |
642 | BFD_ASSERT (hdr->sh_entsize == | |
643 | (use_rela_p | |
6a3eb9b6 KR |
644 | ? sizeof (Elf_External_Rela) |
645 | : sizeof (Elf_External_Rel))); | |
244ffee7 | 646 | |
244ffee7 | 647 | bfd_section_from_shdr (abfd, hdr->sh_info); /* target */ |
32090b8e | 648 | bfd_section_from_shdr (abfd, hdr->sh_link); /* symbol table */ |
244ffee7 | 649 | target_sect = section_from_elf_index (abfd, hdr->sh_info); |
062189c6 ILT |
650 | if (target_sect == NULL |
651 | || elf_section_data (target_sect) == NULL) | |
244ffee7 JK |
652 | return false; |
653 | ||
32090b8e KR |
654 | hdr2 = &elf_section_data (target_sect)->rel_hdr; |
655 | *hdr2 = *hdr; | |
1c6042ee | 656 | elf_elfsections (abfd)[shindex] = hdr2; |
244ffee7 JK |
657 | target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize; |
658 | target_sect->flags |= SEC_RELOC; | |
659 | target_sect->relocation = 0; | |
660 | target_sect->rel_filepos = hdr->sh_offset; | |
32090b8e | 661 | abfd->flags |= HAS_RELOC; |
244ffee7 JK |
662 | return true; |
663 | } | |
664 | break; | |
665 | ||
666 | case SHT_HASH: | |
244ffee7 JK |
667 | case SHT_DYNSYM: /* could treat this like symtab... */ |
668 | #if 0 | |
669 | fprintf (stderr, "Dynamic Linking sections not yet supported.\n"); | |
670 | BFD_FAIL (); | |
671 | #endif | |
672 | break; | |
673 | ||
674 | case SHT_NOTE: | |
675 | #if 0 | |
676 | fprintf (stderr, "Note Sections not yet supported.\n"); | |
677 | BFD_FAIL (); | |
678 | #endif | |
679 | break; | |
680 | ||
681 | case SHT_SHLIB: | |
682 | #if 0 | |
683 | fprintf (stderr, "SHLIB Sections not supported (and non conforming.)\n"); | |
684 | #endif | |
685 | return true; | |
686 | ||
687 | default: | |
e621c5cc ILT |
688 | /* Check for any processor-specific section types. */ |
689 | { | |
690 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
691 | ||
692 | if (bed->elf_backend_section_from_shdr) | |
693 | (*bed->elf_backend_section_from_shdr) (abfd, hdr, name); | |
694 | } | |
244ffee7 JK |
695 | break; |
696 | } | |
697 | ||
698 | return true; | |
699 | } | |
700 | ||
fce36137 | 701 | boolean |
1c6042ee ILT |
702 | elf_new_section_hook (abfd, sec) |
703 | bfd *abfd | |
704 | ; | |
705 | asection *sec; | |
fce36137 | 706 | { |
32090b8e | 707 | struct bfd_elf_section_data *sdata; |
300adb31 KR |
708 | |
709 | sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata)); | |
9783e04a DM |
710 | if (!sdata) |
711 | { | |
d1ad85a6 | 712 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
713 | return false; |
714 | } | |
300adb31 | 715 | sec->used_by_bfd = (PTR) sdata; |
32090b8e | 716 | memset (sdata, 0, sizeof (*sdata)); |
244ffee7 JK |
717 | return true; |
718 | } | |
719 | ||
720 | /* Create a new bfd section from an ELF program header. | |
721 | ||
722 | Since program segments have no names, we generate a synthetic name | |
723 | of the form segment<NUM>, where NUM is generally the index in the | |
724 | program header table. For segments that are split (see below) we | |
725 | generate the names segment<NUM>a and segment<NUM>b. | |
726 | ||
727 | Note that some program segments may have a file size that is different than | |
728 | (less than) the memory size. All this means is that at execution the | |
729 | system must allocate the amount of memory specified by the memory size, | |
730 | but only initialize it with the first "file size" bytes read from the | |
731 | file. This would occur for example, with program segments consisting | |
732 | of combined data+bss. | |
733 | ||
734 | To handle the above situation, this routine generates TWO bfd sections | |
735 | for the single program segment. The first has the length specified by | |
736 | the file size of the segment, and the second has the length specified | |
737 | by the difference between the two sizes. In effect, the segment is split | |
738 | into it's initialized and uninitialized parts. | |
739 | ||
740 | */ | |
741 | ||
742 | static boolean | |
1c6042ee ILT |
743 | bfd_section_from_phdr (abfd, hdr, index) |
744 | bfd *abfd; | |
745 | Elf_Internal_Phdr *hdr; | |
746 | int index; | |
244ffee7 JK |
747 | { |
748 | asection *newsect; | |
749 | char *name; | |
750 | char namebuf[64]; | |
751 | int split; | |
752 | ||
753 | split = ((hdr->p_memsz > 0) && | |
754 | (hdr->p_filesz > 0) && | |
755 | (hdr->p_memsz > hdr->p_filesz)); | |
756 | sprintf (namebuf, split ? "segment%da" : "segment%d", index); | |
757 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
9783e04a DM |
758 | if (!name) |
759 | { | |
d1ad85a6 | 760 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
761 | return false; |
762 | } | |
244ffee7 JK |
763 | strcpy (name, namebuf); |
764 | newsect = bfd_make_section (abfd, name); | |
765 | newsect->vma = hdr->p_vaddr; | |
766 | newsect->_raw_size = hdr->p_filesz; | |
767 | newsect->filepos = hdr->p_offset; | |
768 | newsect->flags |= SEC_HAS_CONTENTS; | |
769 | if (hdr->p_type == PT_LOAD) | |
770 | { | |
771 | newsect->flags |= SEC_ALLOC; | |
772 | newsect->flags |= SEC_LOAD; | |
773 | if (hdr->p_flags & PF_X) | |
774 | { | |
775 | /* FIXME: all we known is that it has execute PERMISSION, | |
776 | may be data. */ | |
777 | newsect->flags |= SEC_CODE; | |
778 | } | |
779 | } | |
780 | if (!(hdr->p_flags & PF_W)) | |
781 | { | |
782 | newsect->flags |= SEC_READONLY; | |
783 | } | |
784 | ||
785 | if (split) | |
786 | { | |
787 | sprintf (namebuf, "segment%db", index); | |
788 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
9783e04a DM |
789 | if (!name) |
790 | { | |
d1ad85a6 | 791 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
792 | return false; |
793 | } | |
244ffee7 JK |
794 | strcpy (name, namebuf); |
795 | newsect = bfd_make_section (abfd, name); | |
796 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; | |
797 | newsect->_raw_size = hdr->p_memsz - hdr->p_filesz; | |
798 | if (hdr->p_type == PT_LOAD) | |
799 | { | |
800 | newsect->flags |= SEC_ALLOC; | |
801 | if (hdr->p_flags & PF_X) | |
802 | newsect->flags |= SEC_CODE; | |
803 | } | |
804 | if (!(hdr->p_flags & PF_W)) | |
805 | newsect->flags |= SEC_READONLY; | |
806 | } | |
807 | ||
808 | return true; | |
809 | } | |
810 | ||
32090b8e | 811 | /* Begin processing a given object. |
244ffee7 | 812 | |
32090b8e KR |
813 | First we validate the file by reading in the ELF header and checking |
814 | the magic number. */ | |
815 | ||
816 | static INLINE boolean | |
1c6042ee ILT |
817 | elf_file_p (x_ehdrp) |
818 | Elf_External_Ehdr *x_ehdrp; | |
244ffee7 | 819 | { |
32090b8e KR |
820 | return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0) |
821 | && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1) | |
822 | && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2) | |
823 | && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3)); | |
824 | } | |
244ffee7 | 825 | |
d24928c0 KR |
826 | /* Check to see if the file associated with ABFD matches the target vector |
827 | that ABFD points to. | |
828 | ||
829 | Note that we may be called several times with the same ABFD, but different | |
830 | target vectors, most of which will not match. We have to avoid leaving | |
831 | any side effects in ABFD, or any data it points to (like tdata), if the | |
832 | file does not match the target vector. | |
833 | ||
834 | FIXME: There is memory leak if we are called more than once with the same | |
835 | ABFD, and that bfd already has tdata allocated, since we allocate more tdata | |
836 | and the old tdata is orphaned. Since it's in the bfd obstack, there isn't | |
01383fb4 | 837 | much we can do about this except possibly rewrite the code. There are |
d24928c0 KR |
838 | also other bfd_allocs that may be the source of memory leaks as well. */ |
839 | ||
32090b8e | 840 | bfd_target * |
1c6042ee ILT |
841 | elf_object_p (abfd) |
842 | bfd *abfd; | |
244ffee7 | 843 | { |
32090b8e KR |
844 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ |
845 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
846 | Elf_External_Shdr x_shdr; /* Section header table entry, external form */ | |
847 | Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */ | |
68241b2b | 848 | unsigned int shindex; |
32090b8e | 849 | char *shstrtab; /* Internal copy of section header stringtab */ |
062189c6 | 850 | struct elf_backend_data *ebd; |
d24928c0 | 851 | struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd); |
244ffee7 | 852 | |
32090b8e KR |
853 | /* Read in the ELF header in external format. */ |
854 | ||
855 | if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr)) | |
d1ad85a6 | 856 | goto got_system_call; |
244ffee7 | 857 | |
32090b8e KR |
858 | /* Now check to see if we have a valid ELF file, and one that BFD can |
859 | make use of. The magic number must match, the address size ('class') | |
860 | and byte-swapping must match our XVEC entry, and it must have a | |
861 | section header table (FIXME: See comments re sections at top of this | |
862 | file). */ | |
244ffee7 | 863 | |
d24928c0 KR |
864 | if ((elf_file_p (&x_ehdr) == false) || |
865 | (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) || | |
866 | (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)) | |
867 | goto got_wrong_format_error; | |
244ffee7 | 868 | |
d24928c0 | 869 | /* Check that file's byte order matches xvec's */ |
32090b8e | 870 | switch (x_ehdr.e_ident[EI_DATA]) |
244ffee7 | 871 | { |
32090b8e KR |
872 | case ELFDATA2MSB: /* Big-endian */ |
873 | if (!abfd->xvec->header_byteorder_big_p) | |
d24928c0 | 874 | goto got_wrong_format_error; |
32090b8e KR |
875 | break; |
876 | case ELFDATA2LSB: /* Little-endian */ | |
877 | if (abfd->xvec->header_byteorder_big_p) | |
d24928c0 | 878 | goto got_wrong_format_error; |
32090b8e KR |
879 | break; |
880 | case ELFDATANONE: /* No data encoding specified */ | |
881 | default: /* Unknown data encoding specified */ | |
d24928c0 | 882 | goto got_wrong_format_error; |
244ffee7 | 883 | } |
244ffee7 | 884 | |
32090b8e | 885 | /* Allocate an instance of the elf_obj_tdata structure and hook it up to |
d24928c0 | 886 | the tdata pointer in the bfd. FIXME: memory leak, see above. */ |
244ffee7 | 887 | |
d24928c0 KR |
888 | elf_tdata (abfd) = |
889 | (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)); | |
890 | if (elf_tdata (abfd) == NULL) | |
891 | goto got_no_memory_error; | |
244ffee7 | 892 | |
32090b8e KR |
893 | /* Now that we know the byte order, swap in the rest of the header */ |
894 | i_ehdrp = elf_elfheader (abfd); | |
895 | elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp); | |
896 | #if DEBUG & 1 | |
897 | elf_debug_file (i_ehdrp); | |
244ffee7 JK |
898 | #endif |
899 | ||
32090b8e KR |
900 | /* If there is no section header table, we're hosed. */ |
901 | if (i_ehdrp->e_shoff == 0) | |
d24928c0 | 902 | goto got_wrong_format_error; |
244ffee7 | 903 | |
062189c6 ILT |
904 | /* As a simple sanity check, verify that the what BFD thinks is the |
905 | size of each section header table entry actually matches the size | |
906 | recorded in the file. */ | |
907 | if (i_ehdrp->e_shentsize != sizeof (x_shdr)) | |
908 | goto got_wrong_format_error; | |
909 | ||
910 | ebd = get_elf_backend_data (abfd); | |
911 | ||
912 | /* Check that the ELF e_machine field matches what this particular | |
913 | BFD format expects. */ | |
914 | if (ebd->elf_machine_code != i_ehdrp->e_machine) | |
915 | { | |
916 | bfd_target **target_ptr; | |
917 | ||
918 | if (ebd->elf_machine_code != EM_NONE) | |
919 | goto got_wrong_format_error; | |
920 | ||
921 | /* This is the generic ELF target. Let it match any ELF target | |
922 | for which we do not have a specific backend. */ | |
f4bd7a8f | 923 | for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++) |
062189c6 ILT |
924 | { |
925 | struct elf_backend_data *back; | |
926 | ||
927 | if ((*target_ptr)->flavour != bfd_target_elf_flavour) | |
928 | continue; | |
929 | back = (struct elf_backend_data *) (*target_ptr)->backend_data; | |
930 | if (back->elf_machine_code == i_ehdrp->e_machine) | |
931 | { | |
932 | /* target_ptr is an ELF backend which matches this | |
933 | object file, so reject the generic ELF target. */ | |
934 | goto got_wrong_format_error; | |
935 | } | |
936 | } | |
937 | } | |
938 | ||
939 | ||
940 | /* Set the flags and architecture before calling the backend so that | |
941 | it can override them. */ | |
7b8106b4 | 942 | if (i_ehdrp->e_type == ET_EXEC) |
32090b8e | 943 | abfd->flags |= EXEC_P; |
7b8106b4 ILT |
944 | else if (i_ehdrp->e_type == ET_DYN) |
945 | abfd->flags |= DYNAMIC; | |
244ffee7 | 946 | |
062189c6 | 947 | bfd_default_set_arch_mach (abfd, ebd->arch, 0); |
32090b8e | 948 | |
062189c6 ILT |
949 | /* Remember the entry point specified in the ELF file header. */ |
950 | bfd_get_start_address (abfd) = i_ehdrp->e_entry; | |
32090b8e | 951 | |
062189c6 ILT |
952 | /* Let the backend double check the format and override global |
953 | information. */ | |
954 | if (ebd->elf_backend_object_p) | |
955 | { | |
956 | if ((*ebd->elf_backend_object_p) (abfd) == false) | |
957 | goto got_wrong_format_error; | |
958 | } | |
1c6042ee | 959 | |
32090b8e KR |
960 | /* Allocate space for a copy of the section header table in |
961 | internal form, seek to the section header table in the file, | |
062189c6 | 962 | read it in, and convert it to internal form. */ |
32090b8e KR |
963 | i_shdrp = (Elf_Internal_Shdr *) |
964 | bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum); | |
300adb31 KR |
965 | elf_elfsections (abfd) = |
966 | (Elf_Internal_Shdr **) bfd_alloc (abfd, sizeof (i_shdrp) * i_ehdrp->e_shnum); | |
1c6042ee | 967 | if (!i_shdrp || !elf_elfsections (abfd)) |
d24928c0 | 968 | goto got_no_memory_error; |
32090b8e | 969 | if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) == -1) |
d1ad85a6 | 970 | goto got_system_call; |
32090b8e | 971 | for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++) |
244ffee7 | 972 | { |
d24928c0 | 973 | if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr)) |
d1ad85a6 | 974 | goto got_system_call; |
32090b8e | 975 | elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex); |
1c6042ee | 976 | elf_elfsections (abfd)[shindex] = i_shdrp + shindex; |
38a5f510 ILT |
977 | |
978 | /* If this is a .dynamic section, mark the object file as being | |
979 | dynamically linked. */ | |
980 | if (i_shdrp[shindex].sh_type == SHT_DYNAMIC) | |
981 | abfd->flags |= DYNAMIC; | |
244ffee7 | 982 | } |
32090b8e | 983 | if (i_ehdrp->e_shstrndx) |
244ffee7 | 984 | { |
32090b8e | 985 | bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx); |
244ffee7 JK |
986 | } |
987 | ||
32090b8e KR |
988 | /* Read in the string table containing the names of the sections. We |
989 | will need the base pointer to this table later. */ | |
990 | /* We read this inline now, so that we don't have to go through | |
991 | bfd_section_from_shdr with it (since this particular strtab is | |
992 | used to find all of the ELF section names.) */ | |
244ffee7 | 993 | |
32090b8e KR |
994 | shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx); |
995 | if (!shstrtab) | |
d24928c0 | 996 | goto got_wrong_format_error; |
244ffee7 | 997 | |
32090b8e KR |
998 | /* Once all of the section headers have been read and converted, we |
999 | can start processing them. Note that the first section header is | |
1000 | a dummy placeholder entry, so we ignore it. | |
244ffee7 | 1001 | |
32090b8e KR |
1002 | We also watch for the symbol table section and remember the file |
1003 | offset and section size for both the symbol table section and the | |
1004 | associated string table section. */ | |
244ffee7 | 1005 | |
32090b8e KR |
1006 | for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++) |
1007 | { | |
1008 | bfd_section_from_shdr (abfd, shindex); | |
1009 | } | |
244ffee7 | 1010 | |
d24928c0 KR |
1011 | return (abfd->xvec); |
1012 | ||
1013 | /* If we are going to use goto's to avoid duplicating error setting | |
1014 | and return(NULL) code, then this at least makes it more maintainable. */ | |
1015 | ||
1c6042ee | 1016 | got_system_call: |
d1ad85a6 | 1017 | bfd_set_error (bfd_error_system_call); |
d24928c0 | 1018 | goto got_no_match; |
1c6042ee | 1019 | got_wrong_format_error: |
d1ad85a6 | 1020 | bfd_set_error (bfd_error_wrong_format); |
d24928c0 | 1021 | goto got_no_match; |
1c6042ee | 1022 | got_no_memory_error: |
d1ad85a6 | 1023 | bfd_set_error (bfd_error_no_memory); |
d24928c0 | 1024 | goto got_no_match; |
1c6042ee | 1025 | got_no_match: |
d24928c0 KR |
1026 | elf_tdata (abfd) = preserved_tdata; |
1027 | return (NULL); | |
32090b8e | 1028 | } |
32090b8e | 1029 | \f |
1c6042ee | 1030 | |
32090b8e KR |
1031 | /* ELF .o/exec file writing */ |
1032 | ||
d24928c0 KR |
1033 | /* Takes a bfd and a symbol, returns a pointer to the elf specific area |
1034 | of the symbol if there is one. */ | |
32090b8e | 1035 | static INLINE elf_symbol_type * |
1c6042ee ILT |
1036 | elf_symbol_from (ignore_abfd, symbol) |
1037 | bfd *ignore_abfd; | |
1038 | asymbol *symbol; | |
244ffee7 | 1039 | { |
32090b8e KR |
1040 | if (symbol->the_bfd->xvec->flavour != bfd_target_elf_flavour) |
1041 | return 0; | |
1042 | ||
1043 | if (symbol->the_bfd->tdata.elf_obj_data == (struct elf_obj_tdata *) NULL) | |
1044 | return 0; | |
1045 | ||
1046 | return (elf_symbol_type *) symbol; | |
244ffee7 JK |
1047 | } |
1048 | ||
d24928c0 | 1049 | /* Create ELF output from BFD sections. |
244ffee7 | 1050 | |
d24928c0 KR |
1051 | Essentially, just create the section header and forget about the program |
1052 | header for now. */ | |
244ffee7 | 1053 | |
32090b8e | 1054 | static void |
1c6042ee ILT |
1055 | elf_make_sections (abfd, asect, obj) |
1056 | bfd *abfd; | |
1057 | asection *asect; | |
1058 | PTR obj; | |
32090b8e KR |
1059 | { |
1060 | /* most of what is in bfd_shdr_from_section goes in here... */ | |
1061 | /* and all of these sections generate at *least* one ELF section. */ | |
32090b8e KR |
1062 | Elf_Internal_Shdr *this_hdr; |
1063 | this_hdr = &elf_section_data (asect)->this_hdr; | |
244ffee7 | 1064 | |
32090b8e KR |
1065 | this_hdr->sh_addr = asect->vma; |
1066 | this_hdr->sh_size = asect->_raw_size; | |
1067 | /* contents already set by elf_set_section_contents */ | |
244ffee7 | 1068 | |
300adb31 | 1069 | if (asect->flags & SEC_RELOC) |
244ffee7 | 1070 | { |
32090b8e KR |
1071 | /* emit a reloc section, and thus strtab and symtab... */ |
1072 | Elf_Internal_Shdr *rela_hdr; | |
32090b8e | 1073 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; |
244ffee7 | 1074 | |
32090b8e | 1075 | rela_hdr = &elf_section_data (asect)->rel_hdr; |
244ffee7 | 1076 | |
32090b8e KR |
1077 | /* orelocation has the data, reloc_count has the count... */ |
1078 | if (use_rela_p) | |
1079 | { | |
1080 | rela_hdr->sh_type = SHT_RELA; | |
1081 | rela_hdr->sh_entsize = sizeof (Elf_External_Rela); | |
1082 | } | |
1083 | else | |
1084 | /* REL relocations */ | |
1085 | { | |
1086 | rela_hdr->sh_type = SHT_REL; | |
1087 | rela_hdr->sh_entsize = sizeof (Elf_External_Rel); | |
1088 | } | |
1089 | rela_hdr->sh_flags = 0; | |
1090 | rela_hdr->sh_addr = 0; | |
1091 | rela_hdr->sh_offset = 0; | |
062189c6 ILT |
1092 | |
1093 | /* FIXME: Systems I've checked use an alignment of 4, but it is | |
1094 | possible that some systems use a different alignment. */ | |
1095 | rela_hdr->sh_addralign = 4; | |
1096 | ||
32090b8e KR |
1097 | rela_hdr->size = 0; |
1098 | } | |
1099 | if (asect->flags & SEC_ALLOC) | |
244ffee7 | 1100 | { |
32090b8e KR |
1101 | this_hdr->sh_flags |= SHF_ALLOC; |
1102 | if (asect->flags & SEC_LOAD) | |
1103 | { | |
1104 | /* @@ Do something with sh_type? */ | |
1105 | } | |
244ffee7 | 1106 | } |
f035cc47 ILT |
1107 | else |
1108 | { | |
1109 | /* If this section is not part of the program image during | |
1110 | execution, leave the address fields at 0. */ | |
1111 | this_hdr->sh_addr = 0; | |
1112 | asect->vma = 0; | |
1113 | } | |
32090b8e KR |
1114 | if (!(asect->flags & SEC_READONLY)) |
1115 | this_hdr->sh_flags |= SHF_WRITE; | |
244ffee7 | 1116 | |
32090b8e KR |
1117 | if (asect->flags & SEC_CODE) |
1118 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
1119 | } | |
244ffee7 | 1120 | |
32090b8e KR |
1121 | void |
1122 | write_relocs (abfd, sec, xxx) | |
1123 | bfd *abfd; | |
1124 | asection *sec; | |
1125 | PTR xxx; | |
1126 | { | |
1127 | Elf_Internal_Shdr *rela_hdr; | |
1128 | Elf_External_Rela *outbound_relocas; | |
1129 | Elf_External_Rel *outbound_relocs; | |
1130 | int idx; | |
1131 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
300adb31 | 1132 | asymbol *last_sym = 0; |
38a5f510 | 1133 | int last_sym_idx = 9999999; /* should always be written before use */ |
244ffee7 | 1134 | |
32090b8e KR |
1135 | if ((sec->flags & SEC_RELOC) == 0) |
1136 | return; | |
1137 | /* Flags are sometimes inconsistent. */ | |
1138 | if (sec->reloc_count == 0) | |
1139 | return; | |
244ffee7 | 1140 | |
32090b8e | 1141 | rela_hdr = &elf_section_data (sec)->rel_hdr; |
244ffee7 | 1142 | |
32090b8e KR |
1143 | rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count; |
1144 | rela_hdr->contents = (void *) bfd_alloc (abfd, rela_hdr->sh_size); | |
9783e04a DM |
1145 | if (!rela_hdr->contents) |
1146 | { | |
d1ad85a6 | 1147 | bfd_set_error (bfd_error_no_memory); |
1c6042ee | 1148 | abort (); /* FIXME */ |
9783e04a | 1149 | } |
244ffee7 | 1150 | |
32090b8e | 1151 | /* orelocation has the data, reloc_count has the count... */ |
300adb31 KR |
1152 | if (use_rela_p) |
1153 | { | |
1154 | outbound_relocas = (Elf_External_Rela *) rela_hdr->contents; | |
1155 | ||
1156 | for (idx = 0; idx < sec->reloc_count; idx++) | |
32090b8e | 1157 | { |
300adb31 KR |
1158 | Elf_Internal_Rela dst_rela; |
1159 | Elf_External_Rela *src_rela; | |
1160 | arelent *ptr; | |
1161 | asymbol *sym; | |
1162 | int n; | |
1163 | ||
1164 | ptr = sec->orelocation[idx]; | |
1165 | src_rela = outbound_relocas + idx; | |
1166 | if (!(abfd->flags & EXEC_P)) | |
1167 | dst_rela.r_offset = ptr->address - sec->vma; | |
1168 | else | |
1169 | dst_rela.r_offset = ptr->address; | |
6a3eb9b6 | 1170 | |
300adb31 KR |
1171 | sym = *ptr->sym_ptr_ptr; |
1172 | if (sym == last_sym) | |
1173 | n = last_sym_idx; | |
1174 | else | |
32090b8e | 1175 | { |
300adb31 KR |
1176 | last_sym = sym; |
1177 | last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym); | |
32090b8e | 1178 | } |
300adb31 KR |
1179 | dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type); |
1180 | ||
1181 | dst_rela.r_addend = ptr->addend; | |
1182 | elf_swap_reloca_out (abfd, &dst_rela, src_rela); | |
244ffee7 | 1183 | } |
300adb31 KR |
1184 | } |
1185 | else | |
1186 | /* REL relocations */ | |
1187 | { | |
1188 | outbound_relocs = (Elf_External_Rel *) rela_hdr->contents; | |
1189 | ||
1190 | for (idx = 0; idx < sec->reloc_count; idx++) | |
32090b8e | 1191 | { |
300adb31 KR |
1192 | Elf_Internal_Rel dst_rel; |
1193 | Elf_External_Rel *src_rel; | |
1194 | arelent *ptr; | |
1195 | int n; | |
1196 | asymbol *sym; | |
1197 | ||
1198 | ptr = sec->orelocation[idx]; | |
1199 | sym = *ptr->sym_ptr_ptr; | |
1200 | src_rel = outbound_relocs + idx; | |
1201 | if (!(abfd->flags & EXEC_P)) | |
1202 | dst_rel.r_offset = ptr->address - sec->vma; | |
1203 | else | |
1204 | dst_rel.r_offset = ptr->address; | |
244ffee7 | 1205 | |
300adb31 KR |
1206 | if (sym == last_sym) |
1207 | n = last_sym_idx; | |
1208 | else | |
32090b8e | 1209 | { |
300adb31 KR |
1210 | last_sym = sym; |
1211 | last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym); | |
32090b8e | 1212 | } |
300adb31 KR |
1213 | dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type); |
1214 | ||
1215 | elf_swap_reloc_out (abfd, &dst_rel, src_rel); | |
32090b8e | 1216 | } |
300adb31 | 1217 | } |
32090b8e | 1218 | } |
244ffee7 | 1219 | |
32090b8e KR |
1220 | static void |
1221 | fix_up_strtabs (abfd, asect, obj) | |
1222 | bfd *abfd; | |
1223 | asection *asect; | |
1224 | PTR obj; | |
1225 | { | |
1226 | Elf_Internal_Shdr *this_hdr = &elf_section_data (asect)->this_hdr; | |
1c6042ee | 1227 | int this_idx = elf_section_data (asect)->this_idx; |
244ffee7 | 1228 | |
32090b8e KR |
1229 | /* @@ Check flags! */ |
1230 | if (!strncmp (asect->name, ".stab", 5) | |
1231 | && !strcmp ("str", asect->name + strlen (asect->name) - 3)) | |
1232 | { | |
1233 | size_t len = strlen (asect->name) + 1; | |
80425e6c JK |
1234 | char *s = (char *) malloc (len); |
1235 | if (s == NULL) | |
1236 | /* FIXME: Should deal more gracefully with errors. */ | |
1237 | abort (); | |
32090b8e KR |
1238 | strcpy (s, asect->name); |
1239 | s[len - 4] = 0; | |
1240 | asect = bfd_get_section_by_name (abfd, s); | |
80425e6c | 1241 | free (s); |
32090b8e KR |
1242 | if (!asect) |
1243 | abort (); | |
1c6042ee | 1244 | elf_section_data (asect)->this_hdr.sh_link = this_idx; |
32090b8e | 1245 | /* @@ Assuming 32 bits! */ |
1c6042ee | 1246 | elf_section_data (asect)->this_hdr.sh_entsize = 0xc; |
01383fb4 KR |
1247 | |
1248 | this_hdr->sh_type = SHT_STRTAB; | |
244ffee7 | 1249 | } |
32090b8e | 1250 | } |
244ffee7 | 1251 | |
32090b8e | 1252 | static void |
1c6042ee ILT |
1253 | elf_fake_sections (abfd, asect, obj) |
1254 | bfd *abfd; | |
1255 | asection *asect; | |
1256 | PTR obj; | |
32090b8e KR |
1257 | { |
1258 | /* most of what is in bfd_shdr_from_section goes in here... */ | |
1259 | /* and all of these sections generate at *least* one ELF section. */ | |
244ffee7 | 1260 | |
32090b8e KR |
1261 | Elf_Internal_Shdr *this_hdr; |
1262 | this_hdr = &elf_section_data (asect)->this_hdr; | |
1263 | this_hdr->sh_name = | |
1264 | bfd_add_to_strtab (abfd, elf_shstrtab (abfd), asect->name); | |
1265 | /* We need to log the type *now* so that elf_section_from_bfd_section | |
1266 | can find us... have to set rawdata too. */ | |
1267 | this_hdr->rawdata = (void *) asect; | |
1268 | this_hdr->sh_addralign = 1 << asect->alignment_power; | |
1269 | if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) | |
1270 | this_hdr->sh_type = SHT_PROGBITS; | |
e621c5cc ILT |
1271 | else if ((asect->flags & SEC_ALLOC) && ((asect->flags & SEC_LOAD) == 0)) |
1272 | { | |
6c35a16d ILT |
1273 | BFD_ASSERT (strcmp (asect->name, ".bss") == 0 |
1274 | || strcmp (asect->name, ".sbss") == 0); | |
e621c5cc ILT |
1275 | this_hdr->sh_type = SHT_NOBITS; |
1276 | } | |
1277 | /* FIXME I am not sure how to detect a .note section from the flags | |
1278 | word of an `asection'. */ | |
1279 | else if (!strcmp (asect->name, ".note")) | |
1280 | this_hdr->sh_type = SHT_NOTE; | |
32090b8e | 1281 | else |
32090b8e KR |
1282 | this_hdr->sh_type = SHT_PROGBITS; |
1283 | ||
1284 | this_hdr->sh_flags = 0; | |
1285 | this_hdr->sh_addr = 0; | |
1286 | this_hdr->sh_size = 0; | |
1287 | this_hdr->sh_entsize = 0; | |
1288 | this_hdr->sh_info = 0; | |
1289 | this_hdr->sh_link = 0; | |
1290 | this_hdr->sh_offset = 0; | |
1291 | this_hdr->size = 0; | |
244ffee7 | 1292 | |
f035cc47 ILT |
1293 | /* Now, check for processor-specific section types. */ |
1294 | { | |
1295 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1296 | ||
1297 | if (bed->elf_backend_fake_sections) | |
1298 | (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect); | |
1299 | } | |
1300 | ||
32090b8e KR |
1301 | { |
1302 | /* Emit a strtab and symtab, and possibly a reloc section. */ | |
1303 | Elf_Internal_Shdr *rela_hdr; | |
244ffee7 | 1304 | |
32090b8e KR |
1305 | /* Note that only one symtab is used, so just remember it |
1306 | for now. */ | |
244ffee7 | 1307 | |
300adb31 | 1308 | if (asect->flags & SEC_RELOC) |
32090b8e KR |
1309 | { |
1310 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
244ffee7 | 1311 | |
32090b8e KR |
1312 | rela_hdr = &elf_section_data (asect)->rel_hdr; |
1313 | rela_hdr->sh_name = | |
1314 | bfd_add_2_to_strtab (abfd, elf_shstrtab (abfd), | |
1315 | use_rela_p ? ".rela" : ".rel", | |
1316 | asect->name); | |
1317 | rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; | |
1318 | rela_hdr->sh_entsize = (use_rela_p | |
1319 | ? sizeof (Elf_External_Rela) | |
1320 | : sizeof (Elf_External_Rel)); | |
1321 | ||
1322 | rela_hdr->sh_flags = 0; | |
1323 | rela_hdr->sh_addr = 0; | |
1324 | rela_hdr->sh_size = 0; | |
1325 | rela_hdr->sh_offset = 0; | |
062189c6 ILT |
1326 | |
1327 | /* FIXME: Systems I've checked use an alignment of 4, but some | |
1328 | systems may use a different alignment. */ | |
1329 | rela_hdr->sh_addralign = 4; | |
1330 | ||
32090b8e KR |
1331 | rela_hdr->size = 0; |
1332 | } | |
1333 | } | |
1334 | if (asect->flags & SEC_ALLOC) | |
1335 | { | |
1336 | this_hdr->sh_flags |= SHF_ALLOC; | |
1337 | if (asect->flags & SEC_LOAD) | |
1338 | { | |
1339 | /* @@ Do something with sh_type? */ | |
1340 | } | |
1341 | } | |
1342 | if (!(asect->flags & SEC_READONLY)) | |
1343 | this_hdr->sh_flags |= SHF_WRITE; | |
1344 | if (asect->flags & SEC_CODE) | |
1345 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
244ffee7 JK |
1346 | } |
1347 | ||
32090b8e KR |
1348 | /* Map symbol from it's internal number to the external number, moving |
1349 | all local symbols to be at the head of the list. */ | |
244ffee7 | 1350 | |
32090b8e | 1351 | static INLINE int |
062189c6 ILT |
1352 | sym_is_global (abfd, sym) |
1353 | bfd *abfd; | |
32090b8e KR |
1354 | asymbol *sym; |
1355 | { | |
062189c6 ILT |
1356 | /* If the backend has a special mapping, use it. */ |
1357 | if (get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1358 | return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1359 | (abfd, sym)); | |
1360 | ||
d24928c0 | 1361 | if (sym->flags & (BSF_GLOBAL | BSF_WEAK)) |
244ffee7 | 1362 | { |
32090b8e KR |
1363 | if (sym->flags & BSF_LOCAL) |
1364 | abort (); | |
1365 | return 1; | |
244ffee7 | 1366 | } |
d24928c0 KR |
1367 | if (sym->section == 0) |
1368 | { | |
1369 | /* Is this valid? */ | |
1370 | abort (); | |
1371 | ||
1372 | return 1; | |
1373 | } | |
32090b8e KR |
1374 | if (sym->section == &bfd_und_section) |
1375 | return 1; | |
1376 | if (bfd_is_com_section (sym->section)) | |
1377 | return 1; | |
1378 | if (sym->flags & (BSF_LOCAL | BSF_SECTION_SYM | BSF_FILE)) | |
1379 | return 0; | |
1380 | return 0; | |
1381 | } | |
244ffee7 | 1382 | |
9783e04a | 1383 | static boolean |
1c6042ee ILT |
1384 | elf_map_symbols (abfd) |
1385 | bfd *abfd; | |
32090b8e KR |
1386 | { |
1387 | int symcount = bfd_get_symcount (abfd); | |
1388 | asymbol **syms = bfd_get_outsymbols (abfd); | |
d24928c0 | 1389 | asymbol **sect_syms; |
32090b8e KR |
1390 | int num_locals = 0; |
1391 | int num_globals = 0; | |
1392 | int num_locals2 = 0; | |
1393 | int num_globals2 = 0; | |
d24928c0 | 1394 | int max_index = 0; |
32090b8e | 1395 | int num_sections = 0; |
d24928c0 | 1396 | Elf_Sym_Extra *sym_extra; |
32090b8e KR |
1397 | int idx; |
1398 | asection *asect; | |
6a3eb9b6 | 1399 | |
32090b8e KR |
1400 | #ifdef DEBUG |
1401 | fprintf (stderr, "elf_map_symbols\n"); | |
1402 | fflush (stderr); | |
1403 | #endif | |
244ffee7 | 1404 | |
e621c5cc ILT |
1405 | /* Add local symbols for each section for which there are relocs. |
1406 | FIXME: How can we tell which sections have relocs at this point? | |
1407 | Will reloc_count always be accurate? Actually, I think most ELF | |
1408 | targets create section symbols for all sections anyhow. */ | |
32090b8e | 1409 | for (asect = abfd->sections; asect; asect = asect->next) |
244ffee7 | 1410 | { |
d24928c0 KR |
1411 | if (max_index < asect->index) |
1412 | max_index = asect->index; | |
244ffee7 JK |
1413 | } |
1414 | ||
d24928c0 KR |
1415 | max_index++; |
1416 | elf_num_section_syms (abfd) = max_index; | |
1417 | sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *)); | |
1418 | elf_section_syms (abfd) = sect_syms; | |
1419 | ||
5e829a34 | 1420 | if (sect_syms == 0) |
9783e04a | 1421 | { |
d1ad85a6 | 1422 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1423 | return false; |
1424 | } | |
d24928c0 KR |
1425 | |
1426 | for (asect = abfd->sections; asect; asect = asect->next) | |
e621c5cc ILT |
1427 | { |
1428 | asymbol *sym = bfd_make_empty_symbol (abfd); | |
9783e04a DM |
1429 | if (!sym) |
1430 | { | |
d1ad85a6 | 1431 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1432 | return false; |
1433 | } | |
e621c5cc ILT |
1434 | sym->the_bfd = abfd; |
1435 | sym->name = asect->name; | |
1436 | sym->value = asect->vma; | |
1437 | sym->flags = BSF_SECTION_SYM; | |
1438 | sym->section = asect; | |
1439 | sect_syms[asect->index] = sym; | |
1440 | num_sections++; | |
d24928c0 | 1441 | #ifdef DEBUG |
e621c5cc ILT |
1442 | fprintf (stderr, |
1443 | "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n", | |
1444 | asect->name, (long) asect->vma, asect->index, (long) asect); | |
d24928c0 | 1445 | #endif |
e621c5cc | 1446 | } |
d24928c0 | 1447 | |
32090b8e | 1448 | if (num_sections) |
244ffee7 | 1449 | { |
32090b8e KR |
1450 | if (syms) |
1451 | syms = (asymbol **) bfd_realloc (abfd, syms, | |
1452 | ((symcount + num_sections + 1) | |
1453 | * sizeof (asymbol *))); | |
1454 | else | |
1455 | syms = (asymbol **) bfd_alloc (abfd, | |
1c6042ee | 1456 | (num_sections + 1) * sizeof (asymbol *)); |
9783e04a DM |
1457 | if (!syms) |
1458 | { | |
d1ad85a6 | 1459 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1460 | return false; |
1461 | } | |
244ffee7 | 1462 | |
32090b8e KR |
1463 | for (asect = abfd->sections; asect; asect = asect->next) |
1464 | { | |
d24928c0 KR |
1465 | if (sect_syms[asect->index]) |
1466 | syms[symcount++] = sect_syms[asect->index]; | |
32090b8e | 1467 | } |
244ffee7 | 1468 | |
32090b8e KR |
1469 | syms[symcount] = (asymbol *) 0; |
1470 | bfd_set_symtab (abfd, syms, symcount); | |
1471 | } | |
244ffee7 | 1472 | |
d24928c0 KR |
1473 | elf_sym_extra (abfd) = sym_extra |
1474 | = (Elf_Sym_Extra *) bfd_alloc (abfd, symcount * sizeof (Elf_Sym_Extra)); | |
9783e04a DM |
1475 | if (!sym_extra) |
1476 | { | |
d1ad85a6 | 1477 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1478 | return false; |
1479 | } | |
244ffee7 | 1480 | |
32090b8e KR |
1481 | /* Identify and classify all of the symbols. */ |
1482 | for (idx = 0; idx < symcount; idx++) | |
244ffee7 | 1483 | { |
062189c6 | 1484 | if (!sym_is_global (abfd, syms[idx])) |
32090b8e KR |
1485 | num_locals++; |
1486 | else | |
1487 | num_globals++; | |
244ffee7 | 1488 | } |
32090b8e KR |
1489 | |
1490 | /* Now provide mapping information. Add +1 for skipping over the | |
1491 | dummy symbol. */ | |
1492 | for (idx = 0; idx < symcount; idx++) | |
244ffee7 | 1493 | { |
1c6042ee | 1494 | syms[idx]->udata = (PTR) & sym_extra[idx]; |
062189c6 | 1495 | if (!sym_is_global (abfd, syms[idx])) |
d24928c0 | 1496 | sym_extra[idx].elf_sym_num = 1 + num_locals2++; |
32090b8e | 1497 | else |
d24928c0 | 1498 | sym_extra[idx].elf_sym_num = 1 + num_locals + num_globals2++; |
244ffee7 JK |
1499 | } |
1500 | ||
32090b8e KR |
1501 | elf_num_locals (abfd) = num_locals; |
1502 | elf_num_globals (abfd) = num_globals; | |
9783e04a | 1503 | return true; |
32090b8e | 1504 | } |
244ffee7 | 1505 | |
9783e04a DM |
1506 | static boolean assign_section_numbers (); |
1507 | static boolean assign_file_positions_except_relocs (); | |
244ffee7 | 1508 | |
32090b8e | 1509 | static boolean |
1c6042ee ILT |
1510 | elf_compute_section_file_positions (abfd) |
1511 | bfd *abfd; | |
32090b8e | 1512 | { |
32090b8e | 1513 | bfd_map_over_sections (abfd, elf_fake_sections, 0); |
244ffee7 | 1514 | |
9783e04a DM |
1515 | if (!assign_section_numbers (abfd)) |
1516 | return false; | |
244ffee7 | 1517 | |
32090b8e | 1518 | bfd_map_over_sections (abfd, elf_make_sections, 0); |
244ffee7 | 1519 | |
1c6042ee | 1520 | bfd_map_over_sections (abfd, fix_up_strtabs, 0); /* .stab/.stabstr &c */ |
244ffee7 | 1521 | |
b9d5cdf0 DM |
1522 | if (swap_out_syms (abfd) == false) |
1523 | return false; | |
244ffee7 | 1524 | |
9783e04a DM |
1525 | if (!assign_file_positions_except_relocs (abfd)) |
1526 | return false; | |
32090b8e KR |
1527 | |
1528 | return true; | |
1529 | } | |
1530 | ||
1531 | static boolean | |
1c6042ee ILT |
1532 | elf_write_phdrs (abfd, i_ehdrp, i_phdrp, phdr_cnt) |
1533 | bfd *abfd; | |
1534 | Elf_Internal_Ehdr *i_ehdrp; | |
1535 | Elf_Internal_Phdr *i_phdrp; | |
1536 | unsigned short phdr_cnt; | |
244ffee7 | 1537 | { |
32090b8e | 1538 | /* first program header entry goes after the file header */ |
300adb31 | 1539 | int outbase = i_ehdrp->e_phoff; |
68241b2b | 1540 | unsigned int i; |
32090b8e KR |
1541 | Elf_External_Phdr x_phdr; |
1542 | ||
1543 | for (i = 0; i < phdr_cnt; i++) | |
244ffee7 | 1544 | { |
32090b8e KR |
1545 | elf_swap_phdr_out (abfd, i_phdrp + i, &x_phdr); |
1546 | bfd_seek (abfd, outbase, SEEK_SET); | |
1547 | bfd_write ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd); | |
1548 | outbase += sizeof (x_phdr); | |
244ffee7 | 1549 | } |
32090b8e KR |
1550 | |
1551 | return true; | |
244ffee7 JK |
1552 | } |
1553 | ||
32090b8e KR |
1554 | static const Elf_Internal_Shdr null_shdr; |
1555 | ||
1556 | /* Assign all ELF section numbers. The dummy first section is handled here | |
1557 | too. The link/info pointers for the standard section types are filled | |
1558 | in here too, while we're at it. (Link pointers for .stab sections are | |
1559 | not filled in here.) */ | |
9783e04a | 1560 | static boolean |
32090b8e | 1561 | assign_section_numbers (abfd) |
fce36137 | 1562 | bfd *abfd; |
fce36137 | 1563 | { |
32090b8e KR |
1564 | struct elf_obj_tdata *t = elf_tdata (abfd); |
1565 | asection *sec; | |
1566 | int section_number = 1; | |
1567 | int i; | |
1568 | Elf_Internal_Shdr **i_shdrp; | |
244ffee7 | 1569 | |
1c6042ee ILT |
1570 | t->shstrtab_hdr.sh_size = elf_shstrtab (abfd)->length; |
1571 | t->shstrtab_hdr.contents = (void *) elf_shstrtab (abfd)->tab; | |
32090b8e | 1572 | shstrtab_length_fixed = 1; |
244ffee7 | 1573 | |
32090b8e | 1574 | t->shstrtab_section = section_number++; |
1c6042ee | 1575 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; |
32090b8e KR |
1576 | if (abfd->symcount) |
1577 | { | |
1578 | t->symtab_section = section_number++; | |
1579 | t->strtab_section = section_number++; | |
1580 | t->symtab_hdr.sh_link = t->strtab_section; | |
1581 | } | |
1582 | for (sec = abfd->sections; sec; sec = sec->next) | |
1583 | { | |
1584 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1585 | d->this_idx = section_number++; | |
300adb31 | 1586 | if (sec->flags & SEC_RELOC) |
fce36137 | 1587 | { |
32090b8e KR |
1588 | d->rel_idx = section_number++; |
1589 | d->rel_hdr.sh_link = t->symtab_section; | |
1590 | d->rel_hdr.sh_info = d->this_idx; | |
244ffee7 | 1591 | } |
fce36137 | 1592 | else |
32090b8e KR |
1593 | d->rel_idx = 0; |
1594 | /* No handling for per-section string tables currently. */ | |
1595 | } | |
1c6042ee | 1596 | elf_elfheader (abfd)->e_shnum = section_number; |
32090b8e KR |
1597 | |
1598 | /* Set up the list of section header pointers, in agreement with the | |
1599 | indices. */ | |
300adb31 KR |
1600 | i_shdrp = (Elf_Internal_Shdr **) |
1601 | bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)); | |
9783e04a DM |
1602 | if (!i_shdrp) |
1603 | { | |
d1ad85a6 | 1604 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1605 | return false; |
1606 | } | |
1c6042ee | 1607 | elf_elfsections (abfd) = i_shdrp; |
32090b8e KR |
1608 | for (i = 0; i < section_number; i++) |
1609 | i_shdrp[i] = 0; | |
1610 | ||
1c6042ee | 1611 | i_shdrp[0] = (Elf_Internal_Shdr *) & null_shdr; |
32090b8e KR |
1612 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; |
1613 | if (abfd->symcount) | |
1614 | { | |
1615 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
1616 | i_shdrp[t->strtab_section] = &t->strtab_hdr; | |
244ffee7 | 1617 | } |
32090b8e KR |
1618 | for (sec = abfd->sections; sec; sec = sec->next) |
1619 | { | |
1620 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1621 | i_shdrp[d->this_idx] = &d->this_hdr; | |
1622 | if (d->rel_idx) | |
1623 | i_shdrp[d->rel_idx] = &d->rel_hdr; | |
1624 | } | |
1625 | /* Make sure we got everything.... */ | |
1626 | for (i = 0; i < section_number; i++) | |
1627 | if (i_shdrp[i] == 0) | |
1628 | abort (); | |
9783e04a | 1629 | return true; |
32090b8e KR |
1630 | } |
1631 | ||
1632 | static INLINE file_ptr | |
1633 | assign_file_position_for_section (i_shdrp, offset) | |
1634 | Elf_Internal_Shdr *i_shdrp; | |
1635 | file_ptr offset; | |
1636 | { | |
f035cc47 ILT |
1637 | int align; |
1638 | ||
1639 | if (i_shdrp->sh_addralign != 0) | |
1640 | align = i_shdrp->sh_addralign; | |
1641 | else | |
1642 | align = 1; | |
1643 | i_shdrp->sh_offset = offset = BFD_ALIGN (offset, align); | |
7b8106b4 ILT |
1644 | if (i_shdrp->rawdata != NULL) |
1645 | ((asection *) i_shdrp->rawdata)->filepos = offset; | |
300adb31 KR |
1646 | if (i_shdrp->sh_type != SHT_NOBITS) |
1647 | offset += i_shdrp->sh_size; | |
32090b8e | 1648 | return offset; |
244ffee7 JK |
1649 | } |
1650 | ||
01383fb4 KR |
1651 | static INLINE file_ptr |
1652 | align_file_position (off) | |
1653 | file_ptr off; | |
1654 | { | |
f035cc47 | 1655 | return (off + FILE_ALIGN - 1) & ~(FILE_ALIGN - 1); |
01383fb4 KR |
1656 | } |
1657 | ||
300adb31 KR |
1658 | static INLINE file_ptr |
1659 | assign_file_positions_for_symtab_and_strtabs (abfd, off) | |
1660 | bfd *abfd; | |
1661 | file_ptr off; | |
1662 | { | |
1663 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
1664 | ||
01383fb4 | 1665 | off = align_file_position (off); |
300adb31 | 1666 | off = assign_file_position_for_section (&t->symtab_hdr, off); |
01383fb4 | 1667 | off = assign_file_position_for_section (&t->shstrtab_hdr, off); |
300adb31 KR |
1668 | off = assign_file_position_for_section (&t->strtab_hdr, off); |
1669 | return off; | |
1670 | } | |
1671 | ||
1c6042ee ILT |
1672 | struct seg_info |
1673 | { | |
300adb31 KR |
1674 | bfd_vma low, mem_size; |
1675 | file_ptr file_size; | |
1676 | int start_pos; | |
1677 | int sh_flags; | |
1678 | struct seg_info *next; | |
1679 | }; | |
1680 | ||
9783e04a | 1681 | static boolean |
300adb31 KR |
1682 | map_program_segments (abfd) |
1683 | bfd *abfd; | |
1684 | { | |
1685 | Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd); | |
1686 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
1687 | Elf_Internal_Shdr *i_shdrp; | |
1688 | Elf_Internal_Phdr *phdr; | |
80425e6c | 1689 | char *done = NULL; |
68241b2b | 1690 | unsigned int i, n_left = 0; |
300adb31 | 1691 | file_ptr lowest_offset = 0; |
2f3189e7 | 1692 | struct seg_info *seg = NULL; |
300adb31 | 1693 | |
80425e6c JK |
1694 | done = (char *) malloc (i_ehdrp->e_shnum); |
1695 | if (done == NULL) | |
1696 | { | |
1697 | bfd_set_error (bfd_error_no_memory); | |
1698 | goto error_return; | |
1699 | } | |
300adb31 | 1700 | memset (done, 0, i_ehdrp->e_shnum); |
062189c6 | 1701 | for (i = 1; i < i_ehdrp->e_shnum; i++) |
300adb31 KR |
1702 | { |
1703 | i_shdrp = i_shdrpp[i]; | |
1704 | /* If it's going to be mapped in, it's been assigned a position. */ | |
1705 | if (i_shdrp->sh_offset + 1 == 0) | |
1706 | { | |
1707 | /* Well, not really, but we won't process it here. */ | |
1708 | done[i] = 1; | |
1709 | continue; | |
1710 | } | |
1711 | if (i_shdrp->sh_offset < lowest_offset | |
1712 | || lowest_offset == 0) | |
1713 | lowest_offset = i_shdrp->sh_offset; | |
1714 | /* Only interested in PROGBITS or NOBITS for generating segments. */ | |
1715 | switch (i_shdrp->sh_type) | |
1716 | { | |
1717 | case SHT_PROGBITS: | |
1718 | case SHT_NOBITS: | |
1719 | break; | |
1720 | default: | |
1721 | done[i] = 1; | |
1722 | } | |
1723 | if (!done[i]) | |
1724 | n_left++; | |
1725 | } | |
1726 | while (n_left) | |
1727 | { | |
1728 | bfd_vma lowest_vma = -1, high; | |
1729 | int low_sec = 0; | |
1730 | int mem_size; | |
1731 | int file_size = 0; | |
2f3189e7 ILT |
1732 | struct seg_info *snew; |
1733 | struct seg_info **s_ptr; | |
300adb31 KR |
1734 | |
1735 | for (i = 1; i < i_ehdrp->e_shnum; i++) | |
1736 | { | |
1737 | i_shdrp = i_shdrpp[i]; | |
1738 | if (!done[i] && i_shdrp->sh_addr < lowest_vma) | |
1739 | { | |
1740 | lowest_vma = i_shdrp->sh_addr; | |
1741 | low_sec = i; | |
1742 | } | |
1743 | } | |
1744 | if (low_sec == 0) | |
1745 | abort (); | |
1746 | /* So now we know the lowest vma of any unassigned sections; start | |
1747 | a segment there. */ | |
2f3189e7 ILT |
1748 | snew = (struct seg_info *) bfd_alloc (abfd, sizeof (struct seg_info)); |
1749 | if (!snew) | |
1750 | { | |
1751 | bfd_set_error (bfd_error_no_memory); | |
80425e6c | 1752 | goto error_return; |
2f3189e7 ILT |
1753 | } |
1754 | s_ptr = &seg; | |
1755 | while (*s_ptr != (struct seg_info *) NULL) | |
1756 | s_ptr = &(*s_ptr)->next; | |
1757 | *s_ptr = snew; | |
1758 | snew->next = NULL; | |
1759 | snew->low = lowest_vma; | |
300adb31 | 1760 | i_shdrp = i_shdrpp[low_sec]; |
2f3189e7 ILT |
1761 | snew->start_pos = i_shdrp->sh_offset; |
1762 | snew->sh_flags = i_shdrp->sh_flags; | |
300adb31 KR |
1763 | done[low_sec] = 1, n_left--; |
1764 | mem_size = i_shdrp->sh_size; | |
1765 | high = lowest_vma + i_shdrp->sh_size; | |
1766 | ||
1767 | if (i_shdrp->sh_type == SHT_PROGBITS) | |
1768 | file_size = i_shdrp->sh_size; | |
1769 | ||
062189c6 | 1770 | for (i = 1; i < i_ehdrp->e_shnum; i++) |
300adb31 KR |
1771 | { |
1772 | file_ptr f1; | |
1773 | ||
300adb31 KR |
1774 | if (done[i]) |
1775 | continue; | |
1776 | i_shdrp = i_shdrpp[i]; | |
1777 | /* position of next byte on disk */ | |
2f3189e7 | 1778 | f1 = snew->start_pos + file_size; |
300adb31 KR |
1779 | if (i_shdrp->sh_type == SHT_PROGBITS) |
1780 | { | |
1781 | if (i_shdrp->sh_offset - f1 != i_shdrp->sh_addr - high) | |
1782 | continue; | |
6c35a16d ILT |
1783 | if (file_size != mem_size) |
1784 | break; | |
300adb31 | 1785 | } |
1c6042ee ILT |
1786 | else |
1787 | /* sh_type == NOBITS */ | |
300adb31 KR |
1788 | { |
1789 | /* If the section in question has no contents in the disk | |
1790 | file, we really don't care where it supposedly starts. | |
1791 | But we don't want to bother merging it into this segment | |
1792 | if it doesn't start on this memory page. */ | |
1793 | bfd_vma page1, page2; | |
1794 | bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize; | |
1795 | ||
2f3189e7 | 1796 | /* page number in address space of current end of snew */ |
300adb31 KR |
1797 | page1 = (high - 1 + maxpagesize - 1) / maxpagesize; |
1798 | /* page number in address space of start of this section */ | |
1799 | page2 = (i_shdrp->sh_addr + maxpagesize - 1) / maxpagesize; | |
1800 | ||
1801 | if (page1 != page2) | |
1802 | continue; | |
1803 | } | |
1804 | done[i] = 1, n_left--; | |
1805 | if (i_shdrp->sh_type == SHT_PROGBITS) | |
2f3189e7 ILT |
1806 | file_size = i_shdrp->sh_offset + i_shdrp->sh_size - snew->start_pos; |
1807 | mem_size = i_shdrp->sh_addr + i_shdrp->sh_size - snew->low; | |
300adb31 KR |
1808 | high = i_shdrp->sh_addr + i_shdrp->sh_size; |
1809 | i = 0; | |
1810 | } | |
2f3189e7 ILT |
1811 | snew->file_size = file_size; |
1812 | snew->mem_size = mem_size; | |
300adb31 KR |
1813 | } |
1814 | /* Now do something with the list of segments we've built up. */ | |
1815 | { | |
1816 | bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize; | |
1817 | struct seg_info *s; | |
1818 | int n_segs = 0; | |
1819 | int sz; | |
1820 | ||
1821 | for (s = seg; s; s = s->next) | |
1822 | { | |
1823 | n_segs++; | |
1824 | } | |
1825 | i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); | |
1826 | sz = sizeof (Elf_External_Phdr) * n_segs; | |
01383fb4 KR |
1827 | if (align_file_position (i_ehdrp->e_ehsize) + sz <= lowest_offset) |
1828 | i_ehdrp->e_phoff = align_file_position (i_ehdrp->e_ehsize); | |
300adb31 KR |
1829 | else |
1830 | { | |
01383fb4 KR |
1831 | i_ehdrp->e_phoff = align_file_position (elf_tdata (abfd)->next_file_pos); |
1832 | elf_tdata (abfd)->next_file_pos = i_ehdrp->e_phoff + sz; | |
300adb31 | 1833 | } |
1c6042ee ILT |
1834 | phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, |
1835 | n_segs * sizeof (Elf_Internal_Phdr)); | |
9783e04a DM |
1836 | if (!phdr) |
1837 | { | |
d1ad85a6 | 1838 | bfd_set_error (bfd_error_no_memory); |
1c6042ee | 1839 | abort (); /* FIXME */ |
9783e04a | 1840 | } |
300adb31 KR |
1841 | elf_tdata (abfd)->phdr = phdr; |
1842 | while (seg) | |
1843 | { | |
1844 | phdr->p_type = PT_LOAD; /* only type we really support so far */ | |
1845 | phdr->p_offset = seg->start_pos; | |
1846 | phdr->p_vaddr = seg->low; | |
1847 | phdr->p_paddr = 0; | |
1848 | phdr->p_filesz = seg->file_size; | |
1849 | phdr->p_memsz = seg->mem_size; | |
1850 | phdr->p_flags = PF_R; | |
1c6042ee | 1851 | phdr->p_align = maxpagesize; /* ? */ |
300adb31 | 1852 | if (seg->sh_flags & SHF_WRITE) |
e621c5cc ILT |
1853 | /* SysVr4 ELF docs say "data segments normally have read, write, |
1854 | and execute permissions." */ | |
1855 | phdr->p_flags |= (PF_W | PF_X); | |
300adb31 KR |
1856 | if (seg->sh_flags & SHF_EXECINSTR) |
1857 | phdr->p_flags |= PF_X; | |
1858 | phdr++; | |
1859 | seg = seg->next; | |
1860 | } | |
1861 | i_ehdrp->e_phnum = n_segs; | |
1862 | } | |
1863 | elf_write_phdrs (abfd, i_ehdrp, elf_tdata (abfd)->phdr, i_ehdrp->e_phnum); | |
80425e6c JK |
1864 | if (done != NULL) |
1865 | free (done); | |
9783e04a | 1866 | return true; |
1c6042ee | 1867 | error_return: |
80425e6c JK |
1868 | if (done != NULL) |
1869 | free (done); | |
1870 | return false; | |
300adb31 KR |
1871 | } |
1872 | ||
9783e04a | 1873 | static boolean |
32090b8e KR |
1874 | assign_file_positions_except_relocs (abfd) |
1875 | bfd *abfd; | |
244ffee7 | 1876 | { |
32090b8e KR |
1877 | /* For now, we ignore the possibility of having program segments, which |
1878 | may require some alignment in the file. That'll require padding, and | |
1879 | some interesting calculations to optimize file space usage. | |
244ffee7 | 1880 | |
32090b8e KR |
1881 | Also, since the application may change the list of relocations for |
1882 | a given section, we don't figure them in here. We'll put them at the | |
1883 | end of the file, at positions computed during bfd_close. | |
244ffee7 | 1884 | |
300adb31 KR |
1885 | The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ... |
1886 | or: <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */ | |
32090b8e | 1887 | |
062189c6 | 1888 | struct elf_obj_tdata *t = elf_tdata (abfd); |
32090b8e | 1889 | file_ptr off; |
68241b2b | 1890 | unsigned int i; |
32090b8e KR |
1891 | Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd); |
1892 | Elf_Internal_Shdr *i_shdrp; | |
1893 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
300adb31 | 1894 | int exec_p = (abfd->flags & EXEC_P) != 0; |
6c35a16d | 1895 | bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize; |
32090b8e | 1896 | |
300adb31 | 1897 | /* Everything starts after the ELF file header. */ |
32090b8e | 1898 | off = i_ehdrp->e_ehsize; |
300adb31 KR |
1899 | |
1900 | if (!exec_p) | |
1901 | { | |
1902 | /* Section headers. */ | |
01383fb4 | 1903 | off = align_file_position (off); |
300adb31 KR |
1904 | i_ehdrp->e_shoff = off; |
1905 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
300adb31 KR |
1906 | off = assign_file_positions_for_symtab_and_strtabs (abfd, off); |
1907 | } | |
062189c6 | 1908 | for (i = 1; i < i_ehdrp->e_shnum; i++) |
32090b8e | 1909 | { |
062189c6 ILT |
1910 | /* The symtab and strtab sections are placed by |
1911 | assign_file_positions_for_symtab_and_strtabs. */ | |
1912 | if (i == t->symtab_section | |
1913 | || i == t->strtab_section | |
1914 | || i == t->shstrtab_section) | |
1915 | continue; | |
1916 | ||
32090b8e KR |
1917 | i_shdrp = i_shdrpp[i]; |
1918 | if (i_shdrp->sh_type == SHT_REL || i_shdrp->sh_type == SHT_RELA) | |
244ffee7 | 1919 | { |
32090b8e KR |
1920 | i_shdrp->sh_offset = -1; |
1921 | continue; | |
244ffee7 | 1922 | } |
300adb31 KR |
1923 | if (exec_p) |
1924 | { | |
300adb31 KR |
1925 | if (maxpagesize == 0) |
1926 | maxpagesize = 1; /* make the arithmetic work */ | |
1927 | /* This isn't necessarily going to give the best packing, if the | |
1928 | segments require padding between them, but since that isn't | |
1929 | usually the case, this'll do. */ | |
1930 | if ((i_shdrp->sh_flags & SHF_ALLOC) == 0) | |
1931 | { | |
1932 | i_shdrp->sh_offset = -1; | |
1933 | continue; | |
1934 | } | |
1935 | /* Blindly assume that the segments are ordered optimally. With | |
1936 | the default LD script, they will be. */ | |
6c35a16d | 1937 | if (i_shdrp->sh_type != SHT_NOBITS) |
300adb31 | 1938 | { |
6c35a16d ILT |
1939 | /* need big unsigned type */ |
1940 | bfd_vma addtl_off; | |
1941 | addtl_off = i_shdrp->sh_addr - off; | |
1942 | addtl_off = addtl_off % maxpagesize; | |
1943 | if (addtl_off) | |
1944 | { | |
1945 | off += addtl_off; | |
1946 | } | |
300adb31 KR |
1947 | } |
1948 | } | |
32090b8e | 1949 | off = assign_file_position_for_section (i_shdrp, off); |
01383fb4 | 1950 | |
6c35a16d ILT |
1951 | if (exec_p |
1952 | && i_shdrp->sh_type == SHT_NOBITS | |
1953 | && (i == i_ehdrp->e_shnum | |
1954 | || i_shdrpp[i + 1]->sh_type != SHT_NOBITS)) | |
1955 | { | |
1956 | /* Skip to the next page to ensure that when the file is | |
1957 | loaded the bss section is loaded with zeroes. I don't | |
1958 | know if this is required on all platforms, but it | |
1959 | shouldn't really hurt. */ | |
1960 | off = BFD_ALIGN (off, maxpagesize); | |
1961 | } | |
1962 | ||
300adb31 | 1963 | if (exec_p |
1c6042ee | 1964 | && get_elf_backend_data (abfd)->maxpagesize > 1 |
300adb31 KR |
1965 | && i_shdrp->sh_type == SHT_PROGBITS |
1966 | && (i_shdrp->sh_flags & SHF_ALLOC) | |
01383fb4 | 1967 | && (i_shdrp->sh_offset - i_shdrp->sh_addr) % get_elf_backend_data (abfd)->maxpagesize != 0) |
300adb31 KR |
1968 | abort (); |
1969 | } | |
1970 | if (exec_p) | |
1971 | { | |
1972 | elf_tdata (abfd)->next_file_pos = off; | |
9783e04a DM |
1973 | if (!map_program_segments (abfd)) |
1974 | return false; | |
300adb31 KR |
1975 | off = elf_tdata (abfd)->next_file_pos; |
1976 | ||
1977 | /* Section headers. */ | |
01383fb4 | 1978 | off = align_file_position (off); |
300adb31 KR |
1979 | i_ehdrp->e_shoff = off; |
1980 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
1981 | ||
1982 | off = assign_file_positions_for_symtab_and_strtabs (abfd, off); | |
1983 | ||
062189c6 | 1984 | for (i = 1; i < i_ehdrp->e_shnum; i++) |
300adb31 KR |
1985 | { |
1986 | i_shdrp = i_shdrpp[i]; | |
1987 | if (i_shdrp->sh_offset + 1 == 0 | |
1988 | && i_shdrp->sh_type != SHT_REL | |
1989 | && i_shdrp->sh_type != SHT_RELA) | |
1990 | off = assign_file_position_for_section (i_shdrp, off); | |
1991 | } | |
244ffee7 | 1992 | } |
32090b8e | 1993 | elf_tdata (abfd)->next_file_pos = off; |
9783e04a | 1994 | return true; |
244ffee7 JK |
1995 | } |
1996 | ||
32090b8e KR |
1997 | static boolean |
1998 | prep_headers (abfd) | |
1999 | bfd *abfd; | |
2000 | { | |
32090b8e | 2001 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ |
1c6042ee | 2002 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ |
32090b8e | 2003 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ |
32090b8e | 2004 | int count; |
32090b8e | 2005 | struct strtab *shstrtab; |
244ffee7 | 2006 | |
32090b8e KR |
2007 | i_ehdrp = elf_elfheader (abfd); |
2008 | i_shdrp = elf_elfsections (abfd); | |
244ffee7 | 2009 | |
32090b8e | 2010 | shstrtab = bfd_new_strtab (abfd); |
b9d5cdf0 DM |
2011 | if (!shstrtab) |
2012 | return false; | |
1c6042ee | 2013 | |
32090b8e | 2014 | elf_shstrtab (abfd) = shstrtab; |
244ffee7 | 2015 | |
32090b8e KR |
2016 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; |
2017 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
2018 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
2019 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
244ffee7 | 2020 | |
32090b8e KR |
2021 | i_ehdrp->e_ident[EI_CLASS] = ELFCLASS; |
2022 | i_ehdrp->e_ident[EI_DATA] = | |
2023 | abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB; | |
2024 | i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT; | |
244ffee7 | 2025 | |
32090b8e KR |
2026 | for (count = EI_PAD; count < EI_NIDENT; count++) |
2027 | i_ehdrp->e_ident[count] = 0; | |
244ffee7 | 2028 | |
32090b8e KR |
2029 | i_ehdrp->e_type = (abfd->flags & EXEC_P) ? ET_EXEC : ET_REL; |
2030 | switch (bfd_get_arch (abfd)) | |
fce36137 | 2031 | { |
32090b8e KR |
2032 | case bfd_arch_unknown: |
2033 | i_ehdrp->e_machine = EM_NONE; | |
2034 | break; | |
2035 | case bfd_arch_sparc: | |
2036 | i_ehdrp->e_machine = EM_SPARC; | |
2037 | /* start-sanitize-v9 */ | |
2038 | #if ARCH_SIZE == 64 | |
2039 | i_ehdrp->e_machine = EM_SPARC64; | |
2040 | #endif | |
2041 | /* end-sanitize-v9 */ | |
2042 | break; | |
2043 | case bfd_arch_i386: | |
2044 | i_ehdrp->e_machine = EM_386; | |
2045 | break; | |
2046 | case bfd_arch_m68k: | |
2047 | i_ehdrp->e_machine = EM_68K; | |
2048 | break; | |
2049 | case bfd_arch_m88k: | |
2050 | i_ehdrp->e_machine = EM_88K; | |
2051 | break; | |
2052 | case bfd_arch_i860: | |
2053 | i_ehdrp->e_machine = EM_860; | |
2054 | break; | |
2055 | case bfd_arch_mips: /* MIPS Rxxxx */ | |
2056 | i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */ | |
2057 | break; | |
2058 | case bfd_arch_hppa: | |
2059 | i_ehdrp->e_machine = EM_HPPA; | |
2060 | break; | |
99ec1f66 ILT |
2061 | case bfd_arch_powerpc: |
2062 | i_ehdrp->e_machine = EM_CYGNUS_POWERPC; | |
2063 | break; | |
32090b8e KR |
2064 | /* also note that EM_M32, AT&T WE32100 is unknown to bfd */ |
2065 | default: | |
2066 | i_ehdrp->e_machine = EM_NONE; | |
fce36137 | 2067 | } |
32090b8e KR |
2068 | i_ehdrp->e_version = EV_CURRENT; |
2069 | i_ehdrp->e_ehsize = sizeof (Elf_External_Ehdr); | |
244ffee7 | 2070 | |
32090b8e KR |
2071 | /* no program header, for now. */ |
2072 | i_ehdrp->e_phoff = 0; | |
2073 | i_ehdrp->e_phentsize = 0; | |
2074 | i_ehdrp->e_phnum = 0; | |
244ffee7 | 2075 | |
32090b8e KR |
2076 | /* each bfd section is section header entry */ |
2077 | i_ehdrp->e_entry = bfd_get_start_address (abfd); | |
2078 | i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr); | |
244ffee7 | 2079 | |
32090b8e KR |
2080 | /* if we're building an executable, we'll need a program header table */ |
2081 | if (abfd->flags & EXEC_P) | |
244ffee7 | 2082 | { |
300adb31 | 2083 | /* it all happens later */ |
32090b8e KR |
2084 | #if 0 |
2085 | i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); | |
244ffee7 | 2086 | |
32090b8e KR |
2087 | /* elf_build_phdrs() returns a (NULL-terminated) array of |
2088 | Elf_Internal_Phdrs */ | |
2089 | i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum); | |
2090 | i_ehdrp->e_phoff = outbase; | |
2091 | outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum; | |
2092 | #endif | |
244ffee7 | 2093 | } |
32090b8e | 2094 | else |
244ffee7 | 2095 | { |
32090b8e KR |
2096 | i_ehdrp->e_phentsize = 0; |
2097 | i_phdrp = 0; | |
2098 | i_ehdrp->e_phoff = 0; | |
244ffee7 JK |
2099 | } |
2100 | ||
32090b8e KR |
2101 | elf_tdata (abfd)->symtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab, |
2102 | ".symtab"); | |
2103 | elf_tdata (abfd)->strtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab, | |
2104 | ".strtab"); | |
2105 | elf_tdata (abfd)->shstrtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab, | |
2106 | ".shstrtab"); | |
f035cc47 | 2107 | return true; |
244ffee7 JK |
2108 | } |
2109 | ||
b9d5cdf0 | 2110 | static boolean |
32090b8e KR |
2111 | swap_out_syms (abfd) |
2112 | bfd *abfd; | |
244ffee7 | 2113 | { |
9783e04a DM |
2114 | if (!elf_map_symbols (abfd)) |
2115 | return false; | |
244ffee7 | 2116 | |
32090b8e KR |
2117 | /* Dump out the symtabs. */ |
2118 | { | |
2119 | int symcount = bfd_get_symcount (abfd); | |
2120 | asymbol **syms = bfd_get_outsymbols (abfd); | |
2121 | struct strtab *stt = bfd_new_strtab (abfd); | |
2122 | Elf_Internal_Shdr *symtab_hdr; | |
2123 | Elf_Internal_Shdr *symstrtab_hdr; | |
2124 | Elf_External_Sym *outbound_syms; | |
2125 | int idx; | |
244ffee7 | 2126 | |
b9d5cdf0 DM |
2127 | if (!stt) |
2128 | return false; | |
32090b8e KR |
2129 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2130 | symtab_hdr->sh_type = SHT_SYMTAB; | |
2131 | symtab_hdr->sh_entsize = sizeof (Elf_External_Sym); | |
2132 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
2133 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
244ffee7 | 2134 | |
062189c6 ILT |
2135 | /* FIXME: Systems I've checked use 4 byte alignment for .symtab, |
2136 | but it is possible that there are systems which use a different | |
2137 | alignment. */ | |
2138 | symtab_hdr->sh_addralign = 4; | |
2139 | ||
32090b8e KR |
2140 | /* see assert in elf_fake_sections that supports this: */ |
2141 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
2142 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
244ffee7 | 2143 | |
32090b8e KR |
2144 | outbound_syms = (Elf_External_Sym *) |
2145 | bfd_alloc (abfd, (1 + symcount) * sizeof (Elf_External_Sym)); | |
9783e04a DM |
2146 | if (!outbound_syms) |
2147 | { | |
d1ad85a6 | 2148 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
2149 | return false; |
2150 | } | |
32090b8e KR |
2151 | /* now generate the data (for "contents") */ |
2152 | { | |
2153 | /* Fill in zeroth symbol and swap it out. */ | |
2154 | Elf_Internal_Sym sym; | |
2155 | sym.st_name = 0; | |
2156 | sym.st_value = 0; | |
2157 | sym.st_size = 0; | |
2158 | sym.st_info = 0; | |
2159 | sym.st_other = 0; | |
2160 | sym.st_shndx = SHN_UNDEF; | |
2161 | elf_swap_symbol_out (abfd, &sym, outbound_syms); | |
244ffee7 | 2162 | } |
32090b8e KR |
2163 | for (idx = 0; idx < symcount; idx++) |
2164 | { | |
2165 | Elf_Internal_Sym sym; | |
2166 | bfd_vma value = syms[idx]->value; | |
244ffee7 | 2167 | |
32090b8e KR |
2168 | if (syms[idx]->flags & BSF_SECTION_SYM) |
2169 | /* Section symbols have no names. */ | |
2170 | sym.st_name = 0; | |
2171 | else | |
2172 | sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name); | |
244ffee7 | 2173 | |
32090b8e | 2174 | if (bfd_is_com_section (syms[idx]->section)) |
244ffee7 | 2175 | { |
32090b8e KR |
2176 | /* ELF common symbols put the alignment into the `value' field, |
2177 | and the size into the `size' field. This is backwards from | |
2178 | how BFD handles it, so reverse it here. */ | |
2179 | sym.st_size = value; | |
2180 | /* Should retrieve this from somewhere... */ | |
2181 | sym.st_value = 16; | |
d4fb8fce ILT |
2182 | sym.st_shndx = elf_section_from_bfd_section (abfd, |
2183 | syms[idx]->section); | |
244ffee7 JK |
2184 | } |
2185 | else | |
2186 | { | |
32090b8e | 2187 | asection *sec = syms[idx]->section; |
e74034d8 | 2188 | elf_symbol_type *type_ptr; |
32090b8e | 2189 | int shndx; |
244ffee7 | 2190 | |
32090b8e KR |
2191 | if (sec->output_section) |
2192 | { | |
2193 | value += sec->output_offset; | |
2194 | sec = sec->output_section; | |
2195 | } | |
2196 | value += sec->vma; | |
2197 | sym.st_value = value; | |
e74034d8 KR |
2198 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
2199 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
32090b8e KR |
2200 | sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec); |
2201 | if (shndx == -1) | |
2202 | { | |
2203 | asection *sec2; | |
2204 | /* Writing this would be a hell of a lot easier if we had | |
2205 | some decent documentation on bfd, and knew what to expect | |
2206 | of the library, and what to demand of applications. For | |
2207 | example, it appears that `objcopy' might not set the | |
2208 | section of a symbol to be a section that is actually in | |
2209 | the output file. */ | |
2210 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
850584ad | 2211 | BFD_ASSERT (sec2 != 0); |
32090b8e | 2212 | sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec2); |
850584ad | 2213 | BFD_ASSERT (shndx != -1); |
32090b8e KR |
2214 | } |
2215 | } | |
244ffee7 | 2216 | |
32090b8e | 2217 | if (bfd_is_com_section (syms[idx]->section)) |
38a5f510 | 2218 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT); |
32090b8e KR |
2219 | else if (syms[idx]->section == &bfd_und_section) |
2220 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_NOTYPE); | |
32090b8e KR |
2221 | else if (syms[idx]->flags & BSF_SECTION_SYM) |
2222 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
2223 | else if (syms[idx]->flags & BSF_FILE) | |
2224 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
d24928c0 | 2225 | else |
32090b8e | 2226 | { |
d24928c0 KR |
2227 | int bind = STB_LOCAL; |
2228 | int type = STT_OBJECT; | |
2229 | unsigned int flags = syms[idx]->flags; | |
2230 | ||
2231 | if (flags & BSF_LOCAL) | |
2232 | bind = STB_LOCAL; | |
2233 | else if (flags & BSF_WEAK) | |
2234 | bind = STB_WEAK; | |
2235 | else if (flags & BSF_GLOBAL) | |
2236 | bind = STB_GLOBAL; | |
2237 | ||
2238 | if (flags & BSF_FUNCTION) | |
2239 | type = STT_FUNC; | |
2240 | ||
2241 | sym.st_info = ELF_ST_INFO (bind, type); | |
32090b8e | 2242 | } |
244ffee7 | 2243 | |
32090b8e KR |
2244 | sym.st_other = 0; |
2245 | elf_swap_symbol_out (abfd, &sym, | |
d24928c0 KR |
2246 | (outbound_syms |
2247 | + elf_sym_extra (abfd)[idx].elf_sym_num)); | |
32090b8e KR |
2248 | } |
2249 | ||
2250 | symtab_hdr->contents = (PTR) outbound_syms; | |
2251 | symstrtab_hdr->contents = (PTR) stt->tab; | |
2252 | symstrtab_hdr->sh_size = stt->length; | |
2253 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
2254 | ||
2255 | symstrtab_hdr->sh_flags = 0; | |
2256 | symstrtab_hdr->sh_addr = 0; | |
2257 | symstrtab_hdr->sh_entsize = 0; | |
2258 | symstrtab_hdr->sh_link = 0; | |
2259 | symstrtab_hdr->sh_info = 0; | |
062189c6 | 2260 | symstrtab_hdr->sh_addralign = 1; |
32090b8e KR |
2261 | symstrtab_hdr->size = 0; |
2262 | } | |
2263 | ||
2264 | /* put the strtab out too... */ | |
2265 | { | |
2266 | Elf_Internal_Shdr *this_hdr; | |
2267 | ||
1c6042ee | 2268 | this_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
32090b8e KR |
2269 | this_hdr->contents = (PTR) elf_shstrtab (abfd)->tab; |
2270 | this_hdr->sh_size = elf_shstrtab (abfd)->length; | |
2271 | this_hdr->sh_type = SHT_STRTAB; | |
2272 | this_hdr->sh_flags = 0; | |
2273 | this_hdr->sh_addr = 0; | |
2274 | this_hdr->sh_entsize = 0; | |
062189c6 | 2275 | this_hdr->sh_addralign = 1; |
32090b8e KR |
2276 | this_hdr->size = 0; |
2277 | } | |
b9d5cdf0 | 2278 | return true; |
244ffee7 JK |
2279 | } |
2280 | ||
32090b8e KR |
2281 | static boolean |
2282 | write_shdrs_and_ehdr (abfd) | |
2283 | bfd *abfd; | |
244ffee7 | 2284 | { |
32090b8e KR |
2285 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ |
2286 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
32090b8e KR |
2287 | Elf_External_Shdr *x_shdrp; /* Section header table, external form */ |
2288 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ | |
68241b2b | 2289 | unsigned int count; |
32090b8e | 2290 | struct strtab *shstrtab; |
244ffee7 | 2291 | |
32090b8e KR |
2292 | i_ehdrp = elf_elfheader (abfd); |
2293 | i_shdrp = elf_elfsections (abfd); | |
2294 | shstrtab = elf_shstrtab (abfd); | |
2295 | ||
2296 | /* swap the header before spitting it out... */ | |
2297 | ||
2298 | #if DEBUG & 1 | |
2299 | elf_debug_file (i_ehdrp); | |
244ffee7 | 2300 | #endif |
32090b8e KR |
2301 | elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr); |
2302 | bfd_seek (abfd, (file_ptr) 0, SEEK_SET); | |
2303 | bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd); | |
244ffee7 | 2304 | |
32090b8e KR |
2305 | /* at this point we've concocted all the ELF sections... */ |
2306 | x_shdrp = (Elf_External_Shdr *) | |
2307 | bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum)); | |
2308 | if (!x_shdrp) | |
2309 | { | |
d1ad85a6 | 2310 | bfd_set_error (bfd_error_no_memory); |
32090b8e KR |
2311 | return false; |
2312 | } | |
2313 | ||
2314 | for (count = 0; count < i_ehdrp->e_shnum; count++) | |
2315 | { | |
2316 | #if DEBUG & 2 | |
2317 | elf_debug_section (shstrtab->tab + i_shdrp[count]->sh_name, count, | |
2318 | i_shdrp[count]); | |
244ffee7 | 2319 | #endif |
32090b8e KR |
2320 | elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count); |
2321 | } | |
2322 | bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET); | |
2323 | bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd); | |
2324 | /* need to dump the string table too... */ | |
244ffee7 | 2325 | |
32090b8e KR |
2326 | return true; |
2327 | } | |
244ffee7 | 2328 | |
32090b8e KR |
2329 | static void |
2330 | assign_file_positions_for_relocs (abfd) | |
2331 | bfd *abfd; | |
2332 | { | |
1c6042ee | 2333 | file_ptr off = elf_tdata (abfd)->next_file_pos; |
68241b2b | 2334 | unsigned int i; |
32090b8e KR |
2335 | Elf_Internal_Shdr **shdrpp = elf_elfsections (abfd); |
2336 | Elf_Internal_Shdr *shdrp; | |
1c6042ee | 2337 | for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++) |
32090b8e KR |
2338 | { |
2339 | shdrp = shdrpp[i]; | |
2340 | if (shdrp->sh_type != SHT_REL && shdrp->sh_type != SHT_RELA) | |
2341 | continue; | |
01383fb4 | 2342 | off = align_file_position (off); |
32090b8e KR |
2343 | off = assign_file_position_for_section (shdrp, off); |
2344 | } | |
1c6042ee | 2345 | elf_tdata (abfd)->next_file_pos = off; |
32090b8e | 2346 | } |
244ffee7 | 2347 | |
32090b8e | 2348 | boolean |
1c6042ee ILT |
2349 | NAME(bfd_elf,write_object_contents) (abfd) |
2350 | bfd *abfd; | |
32090b8e | 2351 | { |
062189c6 | 2352 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
32090b8e KR |
2353 | Elf_Internal_Ehdr *i_ehdrp; |
2354 | Elf_Internal_Shdr **i_shdrp; | |
68241b2b | 2355 | unsigned int count; |
244ffee7 | 2356 | |
38a5f510 ILT |
2357 | /* We don't know how to write dynamic objects. Specifically, we |
2358 | don't know how to construct the program header. */ | |
2359 | if ((abfd->flags & DYNAMIC) != 0) | |
2360 | { | |
2361 | fprintf (stderr, "Writing ELF dynamic objects is not supported\n"); | |
d1ad85a6 | 2362 | bfd_set_error (bfd_error_wrong_format); |
38a5f510 ILT |
2363 | return false; |
2364 | } | |
2365 | ||
32090b8e KR |
2366 | if (abfd->output_has_begun == false) |
2367 | { | |
5e829a34 JL |
2368 | if (prep_headers (abfd) == false) |
2369 | return false; | |
2370 | if (elf_compute_section_file_positions (abfd) == false) | |
2371 | return false; | |
32090b8e KR |
2372 | abfd->output_has_begun = true; |
2373 | } | |
244ffee7 | 2374 | |
32090b8e KR |
2375 | i_shdrp = elf_elfsections (abfd); |
2376 | i_ehdrp = elf_elfheader (abfd); | |
244ffee7 | 2377 | |
32090b8e | 2378 | bfd_map_over_sections (abfd, write_relocs, (PTR) 0); |
32090b8e | 2379 | assign_file_positions_for_relocs (abfd); |
244ffee7 | 2380 | |
32090b8e | 2381 | /* After writing the headers, we need to write the sections too... */ |
062189c6 | 2382 | for (count = 1; count < i_ehdrp->e_shnum; count++) |
e621c5cc | 2383 | { |
e621c5cc ILT |
2384 | if (bed->elf_backend_section_processing) |
2385 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
2386 | if (i_shdrp[count]->contents) | |
2387 | { | |
2388 | bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET); | |
2389 | bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size, 1, | |
2390 | abfd); | |
2391 | } | |
2392 | } | |
062189c6 ILT |
2393 | |
2394 | if (bed->elf_backend_final_write_processing) | |
2395 | (*bed->elf_backend_final_write_processing) (abfd); | |
2396 | ||
32090b8e KR |
2397 | return write_shdrs_and_ehdr (abfd); |
2398 | } | |
244ffee7 | 2399 | |
32090b8e KR |
2400 | /* Given an index of a section, retrieve a pointer to it. Note |
2401 | that for our purposes, sections are indexed by {1, 2, ...} with | |
2402 | 0 being an illegal index. */ | |
244ffee7 | 2403 | |
32090b8e KR |
2404 | /* In the original, each ELF section went into exactly one BFD |
2405 | section. This doesn't really make sense, so we need a real mapping. | |
2406 | The mapping has to hide in the Elf_Internal_Shdr since asection | |
2407 | doesn't have anything like a tdata field... */ | |
244ffee7 | 2408 | |
32090b8e | 2409 | static struct sec * |
1c6042ee ILT |
2410 | section_from_elf_index (abfd, index) |
2411 | bfd *abfd; | |
2412 | unsigned int index; | |
32090b8e KR |
2413 | { |
2414 | /* @@ Is bfd_com_section really correct in all the places it could | |
2415 | be returned from this routine? */ | |
244ffee7 | 2416 | |
32090b8e KR |
2417 | if (index == SHN_ABS) |
2418 | return &bfd_com_section; /* not abs? */ | |
2419 | if (index == SHN_COMMON) | |
2420 | return &bfd_com_section; | |
244ffee7 | 2421 | |
32090b8e KR |
2422 | if (index > elf_elfheader (abfd)->e_shnum) |
2423 | return 0; | |
244ffee7 JK |
2424 | |
2425 | { | |
32090b8e | 2426 | Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[index]; |
244ffee7 | 2427 | |
32090b8e | 2428 | switch (hdr->sh_type) |
244ffee7 | 2429 | { |
32090b8e KR |
2430 | /* ELF sections that map to BFD sections */ |
2431 | case SHT_PROGBITS: | |
2432 | case SHT_NOBITS: | |
2433 | if (!hdr->rawdata) | |
2434 | bfd_section_from_shdr (abfd, index); | |
2435 | return (struct sec *) hdr->rawdata; | |
244ffee7 | 2436 | |
32090b8e KR |
2437 | default: |
2438 | return (struct sec *) &bfd_abs_section; | |
244ffee7 | 2439 | } |
244ffee7 | 2440 | } |
32090b8e | 2441 | } |
244ffee7 | 2442 | |
32090b8e KR |
2443 | /* given a section, search the header to find them... */ |
2444 | static int | |
1c6042ee ILT |
2445 | elf_section_from_bfd_section (abfd, asect) |
2446 | bfd *abfd; | |
2447 | struct sec *asect; | |
32090b8e KR |
2448 | { |
2449 | Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd); | |
2450 | int index; | |
2451 | Elf_Internal_Shdr *hdr; | |
2452 | int maxindex = elf_elfheader (abfd)->e_shnum; | |
244ffee7 | 2453 | |
32090b8e KR |
2454 | if (asect == &bfd_abs_section) |
2455 | return SHN_ABS; | |
2456 | if (asect == &bfd_com_section) | |
2457 | return SHN_COMMON; | |
2458 | if (asect == &bfd_und_section) | |
2459 | return SHN_UNDEF; | |
244ffee7 | 2460 | |
32090b8e KR |
2461 | for (index = 0; index < maxindex; index++) |
2462 | { | |
2463 | hdr = i_shdrp[index]; | |
2464 | switch (hdr->sh_type) | |
2465 | { | |
2466 | /* ELF sections that map to BFD sections */ | |
2467 | case SHT_PROGBITS: | |
2468 | case SHT_NOBITS: | |
e621c5cc | 2469 | case SHT_NOTE: |
32090b8e KR |
2470 | if (hdr->rawdata) |
2471 | { | |
2472 | if (((struct sec *) (hdr->rawdata)) == asect) | |
2473 | return index; | |
2474 | } | |
2475 | break; | |
01383fb4 KR |
2476 | |
2477 | case SHT_STRTAB: | |
2478 | /* fix_up_strtabs will generate STRTAB sections with names | |
2479 | of .stab*str. */ | |
2480 | if (!strncmp (asect->name, ".stab", 5) | |
2481 | && !strcmp ("str", asect->name + strlen (asect->name) - 3)) | |
2482 | { | |
2483 | if (hdr->rawdata) | |
2484 | { | |
2485 | if (((struct sec *) (hdr->rawdata)) == asect) | |
2486 | return index; | |
2487 | } | |
2488 | break; | |
2489 | } | |
2490 | /* FALL THROUGH */ | |
32090b8e | 2491 | default: |
e621c5cc ILT |
2492 | { |
2493 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2494 | ||
2495 | if (bed->elf_backend_section_from_bfd_section) | |
f035cc47 ILT |
2496 | { |
2497 | int retval; | |
2498 | ||
2499 | retval = index; | |
2500 | if ((*bed->elf_backend_section_from_bfd_section) | |
2501 | (abfd, hdr, asect, &retval)) | |
2502 | return retval; | |
2503 | } | |
e621c5cc | 2504 | } |
32090b8e KR |
2505 | break; |
2506 | } | |
2507 | } | |
2508 | return -1; | |
2509 | } | |
244ffee7 | 2510 | |
32090b8e KR |
2511 | /* given a symbol, return the bfd index for that symbol. */ |
2512 | static int | |
1c6042ee ILT |
2513 | elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr) |
2514 | bfd *abfd; | |
2515 | struct symbol_cache_entry **asym_ptr_ptr; | |
32090b8e KR |
2516 | { |
2517 | struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr; | |
32090b8e | 2518 | int idx; |
d24928c0 | 2519 | flagword flags = asym_ptr->flags; |
32090b8e | 2520 | |
d24928c0 KR |
2521 | /* When gas creates relocations against local labels, it creates its |
2522 | own symbol for the section, but does put the symbol into the | |
e621c5cc ILT |
2523 | symbol chain, so udata is 0. When the linker is generating |
2524 | relocatable output, this section symbol may be for one of the | |
2525 | input sections rather than the output section. */ | |
d24928c0 KR |
2526 | if (asym_ptr->udata == (PTR) 0 |
2527 | && (flags & BSF_SECTION_SYM) | |
e621c5cc ILT |
2528 | && asym_ptr->section) |
2529 | { | |
2530 | int indx; | |
2531 | ||
2532 | if (asym_ptr->section->output_section != NULL) | |
2533 | indx = asym_ptr->section->output_section->index; | |
2534 | else | |
2535 | indx = asym_ptr->section->index; | |
2536 | if (elf_section_syms (abfd)[indx]) | |
2537 | asym_ptr->udata = elf_section_syms (abfd)[indx]->udata; | |
01383fb4 | 2538 | } |
e621c5cc | 2539 | |
d24928c0 | 2540 | if (asym_ptr->udata) |
1c6042ee | 2541 | idx = ((Elf_Sym_Extra *) asym_ptr->udata)->elf_sym_num; |
d24928c0 | 2542 | else |
32090b8e | 2543 | { |
32090b8e KR |
2544 | abort (); |
2545 | } | |
244ffee7 | 2546 | |
32090b8e | 2547 | #if DEBUG & 4 |
244ffee7 | 2548 | { |
244ffee7 | 2549 | |
32090b8e | 2550 | fprintf (stderr, |
d24928c0 | 2551 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n", |
1c6042ee | 2552 | (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags)); |
32090b8e KR |
2553 | fflush (stderr); |
2554 | } | |
2555 | #endif | |
2556 | ||
2557 | return idx; | |
2558 | } | |
2559 | ||
2560 | static boolean | |
1c6042ee ILT |
2561 | elf_slurp_symbol_table (abfd, symptrs) |
2562 | bfd *abfd; | |
2563 | asymbol **symptrs; /* Buffer for generated bfd symbols */ | |
32090b8e | 2564 | { |
1c6042ee | 2565 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; |
7d8aaf36 | 2566 | long symcount; /* Number of external ELF symbols */ |
32090b8e KR |
2567 | elf_symbol_type *sym; /* Pointer to current bfd symbol */ |
2568 | elf_symbol_type *symbase; /* Buffer for generated bfd symbols */ | |
2569 | Elf_Internal_Sym i_sym; | |
80425e6c | 2570 | Elf_External_Sym *x_symp = NULL; |
32090b8e KR |
2571 | |
2572 | /* this is only valid because there is only one symtab... */ | |
2573 | /* FIXME: This is incorrect, there may also be a dynamic symbol | |
2574 | table which is a subset of the full symbol table. We either need | |
2575 | to be prepared to read both (and merge them) or ensure that we | |
2576 | only read the full symbol table. Currently we only get called to | |
2577 | read the full symbol table. -fnf */ | |
244ffee7 | 2578 | |
32090b8e KR |
2579 | /* Read each raw ELF symbol, converting from external ELF form to |
2580 | internal ELF form, and then using the information to create a | |
2581 | canonical bfd symbol table entry. | |
244ffee7 | 2582 | |
32090b8e KR |
2583 | Note that we allocate the initial bfd canonical symbol buffer |
2584 | based on a one-to-one mapping of the ELF symbols to canonical | |
2585 | symbols. We actually use all the ELF symbols, so there will be no | |
2586 | space left over at the end. When we have all the symbols, we | |
2587 | build the caller's pointer vector. */ | |
244ffee7 | 2588 | |
32090b8e KR |
2589 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1) |
2590 | { | |
d1ad85a6 | 2591 | bfd_set_error (bfd_error_system_call); |
32090b8e KR |
2592 | return false; |
2593 | } | |
244ffee7 | 2594 | |
32090b8e | 2595 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); |
244ffee7 | 2596 | |
7d8aaf36 ILT |
2597 | if (symcount == 0) |
2598 | sym = symbase = NULL; | |
2599 | else | |
244ffee7 | 2600 | { |
7d8aaf36 | 2601 | long i; |
244ffee7 | 2602 | |
7d8aaf36 | 2603 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1) |
32090b8e | 2604 | { |
7d8aaf36 ILT |
2605 | bfd_set_error (bfd_error_system_call); |
2606 | return false; | |
32090b8e | 2607 | } |
7d8aaf36 ILT |
2608 | |
2609 | symbase = ((elf_symbol_type *) | |
2610 | bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type))); | |
2611 | if (symbase == (elf_symbol_type *) NULL) | |
32090b8e | 2612 | { |
7d8aaf36 ILT |
2613 | bfd_set_error (bfd_error_no_memory); |
2614 | return false; | |
32090b8e | 2615 | } |
7d8aaf36 ILT |
2616 | sym = symbase; |
2617 | ||
2618 | /* Temporarily allocate room for the raw ELF symbols. */ | |
2619 | x_symp = ((Elf_External_Sym *) | |
80425e6c JK |
2620 | malloc (symcount * sizeof (Elf_External_Sym))); |
2621 | if (x_symp == NULL) | |
2622 | { | |
2623 | bfd_set_error (bfd_error_no_memory); | |
2624 | goto error_return; | |
2625 | } | |
7d8aaf36 ILT |
2626 | |
2627 | if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd) | |
2628 | != symcount * sizeof (Elf_External_Sym)) | |
32090b8e | 2629 | { |
a2439266 | 2630 | bfd_set_error (bfd_error_system_call); |
80425e6c | 2631 | goto error_return; |
32090b8e | 2632 | } |
7d8aaf36 ILT |
2633 | /* Skip first symbol, which is a null dummy. */ |
2634 | for (i = 1; i < symcount; i++) | |
32090b8e | 2635 | { |
7d8aaf36 ILT |
2636 | elf_swap_symbol_in (abfd, x_symp + i, &i_sym); |
2637 | memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym)); | |
2638 | #ifdef ELF_KEEP_EXTSYM | |
2639 | memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym)); | |
2640 | #endif | |
2641 | sym->symbol.the_bfd = abfd; | |
244ffee7 | 2642 | |
7d8aaf36 ILT |
2643 | sym->symbol.name = elf_string_from_elf_section (abfd, hdr->sh_link, |
2644 | i_sym.st_name); | |
244ffee7 | 2645 | |
7d8aaf36 | 2646 | sym->symbol.value = i_sym.st_value; |
244ffee7 | 2647 | |
7d8aaf36 ILT |
2648 | if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV) |
2649 | { | |
2650 | sym->symbol.section = section_from_elf_index (abfd, | |
2651 | i_sym.st_shndx); | |
2652 | } | |
2653 | else if (i_sym.st_shndx == SHN_ABS) | |
2654 | { | |
2655 | sym->symbol.section = &bfd_abs_section; | |
2656 | } | |
2657 | else if (i_sym.st_shndx == SHN_COMMON) | |
2658 | { | |
2659 | sym->symbol.section = &bfd_com_section; | |
2660 | /* Elf puts the alignment into the `value' field, and | |
2661 | the size into the `size' field. BFD wants to see the | |
2662 | size in the value field, and doesn't care (at the | |
2663 | moment) about the alignment. */ | |
2664 | sym->symbol.value = i_sym.st_size; | |
2665 | } | |
2666 | else if (i_sym.st_shndx == SHN_UNDEF) | |
2667 | { | |
2668 | sym->symbol.section = &bfd_und_section; | |
2669 | } | |
2670 | else | |
2671 | sym->symbol.section = &bfd_abs_section; | |
300adb31 | 2672 | |
7d8aaf36 | 2673 | sym->symbol.value -= sym->symbol.section->vma; |
244ffee7 | 2674 | |
7d8aaf36 ILT |
2675 | switch (ELF_ST_BIND (i_sym.st_info)) |
2676 | { | |
2677 | case STB_LOCAL: | |
2678 | sym->symbol.flags |= BSF_LOCAL; | |
2679 | break; | |
2680 | case STB_GLOBAL: | |
2681 | sym->symbol.flags |= BSF_GLOBAL; | |
2682 | break; | |
2683 | case STB_WEAK: | |
2684 | sym->symbol.flags |= BSF_WEAK; | |
2685 | break; | |
2686 | } | |
2687 | ||
2688 | switch (ELF_ST_TYPE (i_sym.st_info)) | |
2689 | { | |
2690 | case STT_SECTION: | |
2691 | sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING; | |
2692 | break; | |
2693 | case STT_FILE: | |
2694 | sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING; | |
2695 | break; | |
2696 | case STT_FUNC: | |
2697 | sym->symbol.flags |= BSF_FUNCTION; | |
2698 | break; | |
2699 | } | |
2700 | ||
2701 | /* Do some backend-specific processing on this symbol. */ | |
2702 | { | |
2703 | struct elf_backend_data *ebd = get_elf_backend_data (abfd); | |
2704 | if (ebd->elf_backend_symbol_processing) | |
2705 | (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol); | |
2706 | } | |
2707 | ||
2708 | sym++; | |
2709 | } | |
244ffee7 JK |
2710 | } |
2711 | ||
e621c5cc ILT |
2712 | /* Do some backend-specific processing on this symbol table. */ |
2713 | { | |
2714 | struct elf_backend_data *ebd = get_elf_backend_data (abfd); | |
2715 | if (ebd->elf_backend_symbol_table_processing) | |
2716 | (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount); | |
2717 | } | |
244ffee7 | 2718 | |
e621c5cc | 2719 | /* We rely on the zalloc to clear out the final symbol entry. */ |
244ffee7 | 2720 | |
32090b8e KR |
2721 | bfd_get_symcount (abfd) = symcount = sym - symbase; |
2722 | ||
2723 | /* Fill in the user's symbol pointer vector if needed. */ | |
2724 | if (symptrs) | |
244ffee7 | 2725 | { |
32090b8e KR |
2726 | sym = symbase; |
2727 | while (symcount-- > 0) | |
244ffee7 | 2728 | { |
32090b8e KR |
2729 | *symptrs++ = &sym->symbol; |
2730 | sym++; | |
244ffee7 | 2731 | } |
32090b8e | 2732 | *symptrs = 0; /* Final null pointer */ |
244ffee7 JK |
2733 | } |
2734 | ||
80425e6c JK |
2735 | if (x_symp != NULL) |
2736 | free (x_symp); | |
244ffee7 | 2737 | return true; |
1c6042ee | 2738 | error_return: |
80425e6c JK |
2739 | if (x_symp != NULL) |
2740 | free (x_symp); | |
2741 | return false; | |
244ffee7 JK |
2742 | } |
2743 | ||
32090b8e | 2744 | /* Return the number of bytes required to hold the symtab vector. |
244ffee7 | 2745 | |
32090b8e KR |
2746 | Note that we base it on the count plus 1, since we will null terminate |
2747 | the vector allocated based on this size. However, the ELF symbol table | |
2748 | always has a dummy entry as symbol #0, so it ends up even. */ | |
244ffee7 | 2749 | |
32090b8e | 2750 | unsigned int |
1c6042ee ILT |
2751 | elf_get_symtab_upper_bound (abfd) |
2752 | bfd *abfd; | |
244ffee7 | 2753 | { |
32090b8e KR |
2754 | unsigned int symcount; |
2755 | unsigned int symtab_size = 0; | |
244ffee7 | 2756 | |
1c6042ee | 2757 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; |
32090b8e | 2758 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); |
d6439785 | 2759 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); |
244ffee7 | 2760 | |
32090b8e KR |
2761 | return symtab_size; |
2762 | } | |
244ffee7 | 2763 | |
32090b8e KR |
2764 | /* |
2765 | This function return the number of bytes required to store the | |
2766 | relocation information associated with section <<sect>> | |
2767 | attached to bfd <<abfd>> | |
244ffee7 | 2768 | |
32090b8e KR |
2769 | */ |
2770 | unsigned int | |
2771 | elf_get_reloc_upper_bound (abfd, asect) | |
2772 | bfd *abfd; | |
2773 | sec_ptr asect; | |
2774 | { | |
2775 | if (asect->flags & SEC_RELOC) | |
2776 | { | |
2777 | /* either rel or rela */ | |
1c6042ee | 2778 | return elf_section_data (asect)->rel_hdr.sh_size; |
32090b8e KR |
2779 | } |
2780 | else | |
2781 | return 0; | |
244ffee7 JK |
2782 | } |
2783 | ||
32090b8e | 2784 | static boolean |
1c6042ee ILT |
2785 | elf_slurp_reloca_table (abfd, asect, symbols) |
2786 | bfd *abfd; | |
2787 | sec_ptr asect; | |
2788 | asymbol **symbols; | |
244ffee7 | 2789 | { |
32090b8e KR |
2790 | Elf_External_Rela *native_relocs; |
2791 | arelent *reloc_cache; | |
2792 | arelent *cache_ptr; | |
244ffee7 | 2793 | |
32090b8e | 2794 | unsigned int idx; |
244ffee7 | 2795 | |
32090b8e KR |
2796 | if (asect->relocation) |
2797 | return true; | |
2798 | if (asect->reloc_count == 0) | |
2799 | return true; | |
2800 | if (asect->flags & SEC_CONSTRUCTOR) | |
2801 | return true; | |
244ffee7 | 2802 | |
32090b8e KR |
2803 | bfd_seek (abfd, asect->rel_filepos, SEEK_SET); |
2804 | native_relocs = (Elf_External_Rela *) | |
2805 | bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rela)); | |
9783e04a | 2806 | if (!native_relocs) |
9783e04a | 2807 | { |
d1ad85a6 | 2808 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
2809 | return false; |
2810 | } | |
32090b8e KR |
2811 | bfd_read ((PTR) native_relocs, |
2812 | sizeof (Elf_External_Rela), asect->reloc_count, abfd); | |
244ffee7 | 2813 | |
32090b8e KR |
2814 | reloc_cache = (arelent *) |
2815 | bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent))); | |
2816 | ||
2817 | if (!reloc_cache) | |
6a3eb9b6 | 2818 | { |
d1ad85a6 | 2819 | bfd_set_error (bfd_error_no_memory); |
32090b8e | 2820 | return false; |
6a3eb9b6 | 2821 | } |
244ffee7 | 2822 | |
32090b8e KR |
2823 | for (idx = 0; idx < asect->reloc_count; idx++) |
2824 | { | |
32090b8e KR |
2825 | Elf_Internal_Rela dst; |
2826 | Elf_External_Rela *src; | |
244ffee7 | 2827 | |
32090b8e KR |
2828 | cache_ptr = reloc_cache + idx; |
2829 | src = native_relocs + idx; | |
2830 | elf_swap_reloca_in (abfd, src, &dst); | |
244ffee7 | 2831 | |
d24928c0 | 2832 | #ifdef RELOC_PROCESSING |
32090b8e KR |
2833 | RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect); |
2834 | #else | |
32090b8e KR |
2835 | if (asect->flags & SEC_RELOC) |
2836 | { | |
2837 | /* relocatable, so the offset is off of the section */ | |
2838 | cache_ptr->address = dst.r_offset + asect->vma; | |
2839 | } | |
2840 | else | |
2841 | { | |
2842 | /* non-relocatable, so the offset a virtual address */ | |
2843 | cache_ptr->address = dst.r_offset; | |
2844 | } | |
7b8106b4 ILT |
2845 | |
2846 | /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset | |
2847 | of zero points to the dummy symbol, which was not read into | |
2848 | the symbol table SYMBOLS. */ | |
2849 | if (ELF_R_SYM (dst.r_info) == 0) | |
2850 | cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; | |
2851 | else | |
2852 | { | |
2853 | asymbol *s; | |
2854 | ||
2855 | cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1; | |
2856 | ||
2857 | /* Translate any ELF section symbol into a BFD section | |
2858 | symbol. */ | |
2859 | s = *(cache_ptr->sym_ptr_ptr); | |
2860 | if (s->flags & BSF_SECTION_SYM) | |
2861 | { | |
2862 | cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr; | |
2863 | s = *cache_ptr->sym_ptr_ptr; | |
2864 | if (s->name == 0 || s->name[0] == 0) | |
2865 | abort (); | |
2866 | } | |
2867 | } | |
32090b8e | 2868 | cache_ptr->addend = dst.r_addend; |
244ffee7 | 2869 | |
32090b8e KR |
2870 | /* Fill in the cache_ptr->howto field from dst.r_type */ |
2871 | { | |
2872 | struct elf_backend_data *ebd = get_elf_backend_data (abfd); | |
2873 | (*ebd->elf_info_to_howto) (abfd, cache_ptr, &dst); | |
2874 | } | |
2875 | #endif | |
2876 | } | |
244ffee7 | 2877 | |
32090b8e KR |
2878 | asect->relocation = reloc_cache; |
2879 | return true; | |
2880 | } | |
238ac6ec | 2881 | |
32090b8e KR |
2882 | #ifdef DEBUG |
2883 | static void | |
2884 | elf_debug_section (str, num, hdr) | |
2885 | char *str; | |
2886 | int num; | |
2887 | Elf_Internal_Shdr *hdr; | |
2888 | { | |
2889 | fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, str, (long) hdr); | |
2890 | fprintf (stderr, | |
2891 | "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n", | |
2892 | (long) hdr->sh_name, | |
2893 | (long) hdr->sh_type, | |
2894 | (long) hdr->sh_flags); | |
2895 | fprintf (stderr, | |
2896 | "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n", | |
2897 | (long) hdr->sh_addr, | |
2898 | (long) hdr->sh_offset, | |
2899 | (long) hdr->sh_size); | |
2900 | fprintf (stderr, | |
2901 | "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n", | |
2902 | (long) hdr->sh_link, | |
2903 | (long) hdr->sh_info, | |
2904 | (long) hdr->sh_addralign); | |
2905 | fprintf (stderr, "sh_entsize = %ld\n", | |
2906 | (long) hdr->sh_entsize); | |
2907 | fprintf (stderr, "rawdata = 0x%.8lx\n", (long) hdr->rawdata); | |
2908 | fprintf (stderr, "contents = 0x%.8lx\n", (long) hdr->contents); | |
2909 | fprintf (stderr, "size = %ld\n", (long) hdr->size); | |
2910 | fflush (stderr); | |
2911 | } | |
244ffee7 | 2912 | |
32090b8e KR |
2913 | static void |
2914 | elf_debug_file (ehdrp) | |
2915 | Elf_Internal_Ehdr *ehdrp; | |
2916 | { | |
2917 | fprintf (stderr, "e_entry = 0x%.8lx\n", (long) ehdrp->e_entry); | |
2918 | fprintf (stderr, "e_phoff = %ld\n", (long) ehdrp->e_phoff); | |
2919 | fprintf (stderr, "e_phnum = %ld\n", (long) ehdrp->e_phnum); | |
2920 | fprintf (stderr, "e_phentsize = %ld\n", (long) ehdrp->e_phentsize); | |
2921 | fprintf (stderr, "e_shoff = %ld\n", (long) ehdrp->e_shoff); | |
2922 | fprintf (stderr, "e_shnum = %ld\n", (long) ehdrp->e_shnum); | |
2923 | fprintf (stderr, "e_shentsize = %ld\n", (long) ehdrp->e_shentsize); | |
244ffee7 | 2924 | } |
32090b8e | 2925 | #endif |
244ffee7 JK |
2926 | |
2927 | static boolean | |
1c6042ee ILT |
2928 | elf_slurp_reloc_table (abfd, asect, symbols) |
2929 | bfd *abfd; | |
2930 | sec_ptr asect; | |
2931 | asymbol **symbols; | |
244ffee7 | 2932 | { |
32090b8e KR |
2933 | Elf_External_Rel *native_relocs; |
2934 | arelent *reloc_cache; | |
2935 | arelent *cache_ptr; | |
2936 | Elf_Internal_Shdr *data_hdr; | |
25677b5b PS |
2937 | bfd_vma data_off; |
2938 | unsigned long data_max; | |
32090b8e | 2939 | char buf[4]; /* FIXME -- might be elf64 */ |
244ffee7 | 2940 | |
32090b8e | 2941 | unsigned int idx; |
244ffee7 | 2942 | |
32090b8e KR |
2943 | if (asect->relocation) |
2944 | return true; | |
2945 | if (asect->reloc_count == 0) | |
2946 | return true; | |
2947 | if (asect->flags & SEC_CONSTRUCTOR) | |
2948 | return true; | |
244ffee7 | 2949 | |
32090b8e KR |
2950 | bfd_seek (abfd, asect->rel_filepos, SEEK_SET); |
2951 | native_relocs = (Elf_External_Rel *) | |
2952 | bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rel)); | |
9783e04a DM |
2953 | if (!native_relocs) |
2954 | { | |
d1ad85a6 | 2955 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
2956 | return false; |
2957 | } | |
32090b8e KR |
2958 | bfd_read ((PTR) native_relocs, |
2959 | sizeof (Elf_External_Rel), asect->reloc_count, abfd); | |
244ffee7 | 2960 | |
32090b8e KR |
2961 | reloc_cache = (arelent *) |
2962 | bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent))); | |
2963 | ||
2964 | if (!reloc_cache) | |
244ffee7 | 2965 | { |
d1ad85a6 | 2966 | bfd_set_error (bfd_error_no_memory); |
244ffee7 JK |
2967 | return false; |
2968 | } | |
2969 | ||
32090b8e KR |
2970 | /* Get the offset of the start of the segment we are relocating to read in |
2971 | the implicit addend. */ | |
1c6042ee | 2972 | data_hdr = &elf_section_data (asect)->this_hdr; |
32090b8e KR |
2973 | data_off = data_hdr->sh_offset; |
2974 | data_max = data_hdr->sh_size - sizeof (buf) + 1; | |
244ffee7 | 2975 | |
32090b8e KR |
2976 | #if DEBUG & 2 |
2977 | elf_debug_section ("data section", -1, data_hdr); | |
2978 | #endif | |
244ffee7 | 2979 | |
32090b8e | 2980 | for (idx = 0; idx < asect->reloc_count; idx++) |
244ffee7 | 2981 | { |
32090b8e KR |
2982 | #ifdef RELOC_PROCESSING |
2983 | Elf_Internal_Rel dst; | |
2984 | Elf_External_Rel *src; | |
244ffee7 | 2985 | |
32090b8e KR |
2986 | cache_ptr = reloc_cache + idx; |
2987 | src = native_relocs + idx; | |
2988 | elf_swap_reloc_in (abfd, src, &dst); | |
244ffee7 | 2989 | |
32090b8e KR |
2990 | RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect); |
2991 | #else | |
2992 | Elf_Internal_Rel dst; | |
2993 | Elf_External_Rel *src; | |
6a3eb9b6 | 2994 | |
32090b8e KR |
2995 | cache_ptr = reloc_cache + idx; |
2996 | src = native_relocs + idx; | |
2997 | ||
2998 | elf_swap_reloc_in (abfd, src, &dst); | |
2999 | ||
3000 | if (asect->flags & SEC_RELOC) | |
244ffee7 | 3001 | { |
32090b8e KR |
3002 | /* relocatable, so the offset is off of the section */ |
3003 | cache_ptr->address = dst.r_offset + asect->vma; | |
244ffee7 | 3004 | } |
32090b8e | 3005 | else |
244ffee7 | 3006 | { |
32090b8e KR |
3007 | /* non-relocatable, so the offset a virtual address */ |
3008 | cache_ptr->address = dst.r_offset; | |
244ffee7 | 3009 | } |
7b8106b4 ILT |
3010 | |
3011 | /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset | |
3012 | of zero points to the dummy symbol, which was not read into | |
3013 | the symbol table SYMBOLS. */ | |
3014 | if (ELF_R_SYM (dst.r_info) == 0) | |
3015 | cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; | |
3016 | else | |
3017 | { | |
3018 | asymbol *s; | |
3019 | ||
3020 | cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1; | |
3021 | ||
3022 | /* Translate any ELF section symbol into a BFD section | |
3023 | symbol. */ | |
3024 | s = *(cache_ptr->sym_ptr_ptr); | |
3025 | if (s->flags & BSF_SECTION_SYM) | |
3026 | { | |
3027 | cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr; | |
3028 | s = *cache_ptr->sym_ptr_ptr; | |
3029 | if (s->name == 0 || s->name[0] == 0) | |
3030 | abort (); | |
3031 | } | |
3032 | } | |
32090b8e | 3033 | BFD_ASSERT (dst.r_offset <= data_max); |
d24928c0 | 3034 | cache_ptr->addend = 0; |
244ffee7 | 3035 | |
32090b8e KR |
3036 | /* Fill in the cache_ptr->howto field from dst.r_type */ |
3037 | { | |
3038 | struct elf_backend_data *ebd = get_elf_backend_data (abfd); | |
3039 | (*ebd->elf_info_to_howto_rel) (abfd, cache_ptr, &dst); | |
3040 | } | |
3041 | #endif | |
3042 | } | |
244ffee7 | 3043 | |
32090b8e KR |
3044 | asect->relocation = reloc_cache; |
3045 | return true; | |
3046 | } | |
244ffee7 | 3047 | |
32090b8e KR |
3048 | unsigned int |
3049 | elf_canonicalize_reloc (abfd, section, relptr, symbols) | |
3050 | bfd *abfd; | |
3051 | sec_ptr section; | |
3052 | arelent **relptr; | |
3053 | asymbol **symbols; | |
3054 | { | |
3055 | arelent *tblptr = section->relocation; | |
3056 | unsigned int count = 0; | |
3057 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
3058 | ||
3059 | /* snarfed from coffcode.h */ | |
3060 | if (use_rela_p) | |
3061 | elf_slurp_reloca_table (abfd, section, symbols); | |
3062 | else | |
3063 | elf_slurp_reloc_table (abfd, section, symbols); | |
3064 | ||
3065 | tblptr = section->relocation; | |
3066 | if (!tblptr) | |
3067 | return 0; | |
3068 | ||
3069 | for (; count++ < section->reloc_count;) | |
3070 | *relptr++ = tblptr++; | |
3071 | ||
3072 | *relptr = 0; | |
3073 | return section->reloc_count; | |
3074 | } | |
3075 | ||
3076 | unsigned int | |
1c6042ee ILT |
3077 | elf_get_symtab (abfd, alocation) |
3078 | bfd *abfd; | |
3079 | asymbol **alocation; | |
32090b8e KR |
3080 | { |
3081 | ||
3082 | if (!elf_slurp_symbol_table (abfd, alocation)) | |
3083 | return 0; | |
3084 | else | |
3085 | return bfd_get_symcount (abfd); | |
3086 | } | |
3087 | ||
3088 | asymbol * | |
1c6042ee ILT |
3089 | elf_make_empty_symbol (abfd) |
3090 | bfd *abfd; | |
32090b8e KR |
3091 | { |
3092 | elf_symbol_type *newsym; | |
3093 | ||
3094 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type)); | |
3095 | if (!newsym) | |
3096 | { | |
d1ad85a6 | 3097 | bfd_set_error (bfd_error_no_memory); |
32090b8e KR |
3098 | return NULL; |
3099 | } | |
3100 | else | |
3101 | { | |
3102 | newsym->symbol.the_bfd = abfd; | |
3103 | return &newsym->symbol; | |
244ffee7 | 3104 | } |
32090b8e | 3105 | } |
244ffee7 | 3106 | |
32090b8e | 3107 | void |
1c6042ee ILT |
3108 | elf_get_symbol_info (ignore_abfd, symbol, ret) |
3109 | bfd *ignore_abfd; | |
3110 | asymbol *symbol; | |
3111 | symbol_info *ret; | |
32090b8e KR |
3112 | { |
3113 | bfd_symbol_info (symbol, ret); | |
3114 | } | |
244ffee7 | 3115 | |
32090b8e | 3116 | void |
1c6042ee ILT |
3117 | elf_print_symbol (ignore_abfd, filep, symbol, how) |
3118 | bfd *ignore_abfd; | |
3119 | PTR filep; | |
3120 | asymbol *symbol; | |
3121 | bfd_print_symbol_type how; | |
32090b8e KR |
3122 | { |
3123 | FILE *file = (FILE *) filep; | |
3124 | switch (how) | |
3125 | { | |
3126 | case bfd_print_symbol_name: | |
3127 | fprintf (file, "%s", symbol->name); | |
3128 | break; | |
3129 | case bfd_print_symbol_more: | |
3130 | fprintf (file, "elf "); | |
3131 | fprintf_vma (file, symbol->value); | |
3132 | fprintf (file, " %lx", (long) symbol->flags); | |
3133 | break; | |
3134 | case bfd_print_symbol_all: | |
3135 | { | |
3136 | CONST char *section_name; | |
3137 | section_name = symbol->section ? symbol->section->name : "(*none*)"; | |
3138 | bfd_print_symbol_vandf ((PTR) file, symbol); | |
3139 | fprintf (file, " %s\t%s", | |
3140 | section_name, | |
3141 | symbol->name); | |
3142 | } | |
3143 | break; | |
3144 | } | |
244ffee7 | 3145 | |
32090b8e | 3146 | } |
244ffee7 | 3147 | |
32090b8e | 3148 | alent * |
1c6042ee ILT |
3149 | elf_get_lineno (ignore_abfd, symbol) |
3150 | bfd *ignore_abfd; | |
3151 | asymbol *symbol; | |
32090b8e KR |
3152 | { |
3153 | fprintf (stderr, "elf_get_lineno unimplemented\n"); | |
3154 | fflush (stderr); | |
3155 | BFD_FAIL (); | |
3156 | return NULL; | |
3157 | } | |
3158 | ||
3159 | boolean | |
1c6042ee ILT |
3160 | elf_set_arch_mach (abfd, arch, machine) |
3161 | bfd *abfd; | |
3162 | enum bfd_architecture arch; | |
3163 | unsigned long machine; | |
32090b8e KR |
3164 | { |
3165 | /* Allow any architecture to be supported by the elf backend */ | |
3166 | switch (arch) | |
244ffee7 | 3167 | { |
32090b8e KR |
3168 | case bfd_arch_unknown: /* EM_NONE */ |
3169 | case bfd_arch_sparc: /* EM_SPARC */ | |
1c6042ee ILT |
3170 | case bfd_arch_i386: /* EM_386 */ |
3171 | case bfd_arch_m68k: /* EM_68K */ | |
3172 | case bfd_arch_m88k: /* EM_88K */ | |
3173 | case bfd_arch_i860: /* EM_860 */ | |
3174 | case bfd_arch_mips: /* EM_MIPS (MIPS R3000) */ | |
3175 | case bfd_arch_hppa: /* EM_HPPA (HP PA_RISC) */ | |
99ec1f66 | 3176 | case bfd_arch_powerpc: /* EM_CYGNUS_POWERPC */ |
32090b8e KR |
3177 | return bfd_default_set_arch_mach (abfd, arch, machine); |
3178 | default: | |
3179 | return false; | |
244ffee7 | 3180 | } |
32090b8e | 3181 | } |
244ffee7 | 3182 | |
32090b8e | 3183 | boolean |
1c6042ee ILT |
3184 | elf_find_nearest_line (abfd, |
3185 | section, | |
3186 | symbols, | |
3187 | offset, | |
3188 | filename_ptr, | |
3189 | functionname_ptr, | |
3190 | line_ptr) | |
3191 | bfd *abfd; | |
3192 | asection *section; | |
3193 | asymbol **symbols; | |
3194 | bfd_vma offset; | |
3195 | CONST char **filename_ptr; | |
3196 | CONST char **functionname_ptr; | |
3197 | unsigned int *line_ptr; | |
32090b8e KR |
3198 | { |
3199 | return false; | |
244ffee7 JK |
3200 | } |
3201 | ||
32090b8e | 3202 | int |
1c6042ee ILT |
3203 | elf_sizeof_headers (abfd, reloc) |
3204 | bfd *abfd; | |
3205 | boolean reloc; | |
32090b8e KR |
3206 | { |
3207 | fprintf (stderr, "elf_sizeof_headers unimplemented\n"); | |
3208 | fflush (stderr); | |
3209 | BFD_FAIL (); | |
3210 | return 0; | |
3211 | } | |
244ffee7 | 3212 | |
32090b8e | 3213 | boolean |
1c6042ee ILT |
3214 | elf_set_section_contents (abfd, section, location, offset, count) |
3215 | bfd *abfd; | |
3216 | sec_ptr section; | |
3217 | PTR location; | |
3218 | file_ptr offset; | |
3219 | bfd_size_type count; | |
244ffee7 | 3220 | { |
244ffee7 JK |
3221 | Elf_Internal_Shdr *hdr; |
3222 | ||
32090b8e | 3223 | if (abfd->output_has_begun == false) /* set by bfd.c handler? */ |
244ffee7 | 3224 | { |
32090b8e | 3225 | /* do setup calculations (FIXME) */ |
5e829a34 JL |
3226 | if (prep_headers (abfd) == false) |
3227 | return false; | |
3228 | if (elf_compute_section_file_positions (abfd) == false) | |
3229 | return false; | |
32090b8e | 3230 | abfd->output_has_begun = true; |
244ffee7 | 3231 | } |
244ffee7 | 3232 | |
1c6042ee | 3233 | hdr = &elf_section_data (section)->this_hdr; |
244ffee7 | 3234 | |
32090b8e KR |
3235 | if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1) |
3236 | return false; | |
3237 | if (bfd_write (location, 1, count, abfd) != count) | |
3238 | return false; | |
3239 | ||
3240 | return true; | |
3241 | } | |
3242 | ||
3243 | void | |
1c6042ee ILT |
3244 | elf_no_info_to_howto (abfd, cache_ptr, dst) |
3245 | bfd *abfd; | |
3246 | arelent *cache_ptr; | |
3247 | Elf_Internal_Rela *dst; | |
244ffee7 | 3248 | { |
32090b8e KR |
3249 | fprintf (stderr, "elf RELA relocation support for target machine unimplemented\n"); |
3250 | fflush (stderr); | |
3251 | BFD_FAIL (); | |
244ffee7 JK |
3252 | } |
3253 | ||
32090b8e | 3254 | void |
1c6042ee ILT |
3255 | elf_no_info_to_howto_rel (abfd, cache_ptr, dst) |
3256 | bfd *abfd; | |
3257 | arelent *cache_ptr; | |
3258 | Elf_Internal_Rel *dst; | |
244ffee7 | 3259 | { |
32090b8e KR |
3260 | fprintf (stderr, "elf REL relocation support for target machine unimplemented\n"); |
3261 | fflush (stderr); | |
3262 | BFD_FAIL (); | |
3263 | } | |
32090b8e | 3264 | \f |
1c6042ee | 3265 | |
32090b8e | 3266 | /* Core file support */ |
244ffee7 | 3267 | |
32090b8e KR |
3268 | #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */ |
3269 | #include <sys/procfs.h> | |
3270 | #else | |
3271 | #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */ | |
3272 | #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */ | |
3273 | #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */ | |
3274 | #endif | |
244ffee7 | 3275 | |
32090b8e | 3276 | #ifdef HAVE_PROCFS |
244ffee7 | 3277 | |
32090b8e | 3278 | static void |
1c6042ee ILT |
3279 | bfd_prstatus (abfd, descdata, descsz, filepos) |
3280 | bfd *abfd; | |
3281 | char *descdata; | |
3282 | int descsz; | |
3283 | long filepos; | |
32090b8e KR |
3284 | { |
3285 | asection *newsect; | |
3286 | prstatus_t *status = (prstatus_t *) 0; | |
244ffee7 | 3287 | |
32090b8e | 3288 | if (descsz == sizeof (prstatus_t)) |
244ffee7 | 3289 | { |
32090b8e KR |
3290 | newsect = bfd_make_section (abfd, ".reg"); |
3291 | newsect->_raw_size = sizeof (status->pr_reg); | |
3292 | newsect->filepos = filepos + (long) &status->pr_reg; | |
3293 | newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
3294 | newsect->alignment_power = 2; | |
3295 | if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL) | |
3296 | { | |
3297 | memcpy (core_prstatus (abfd), descdata, descsz); | |
3298 | } | |
244ffee7 | 3299 | } |
32090b8e | 3300 | } |
244ffee7 | 3301 | |
32090b8e | 3302 | /* Stash a copy of the prpsinfo structure away for future use. */ |
244ffee7 | 3303 | |
32090b8e | 3304 | static void |
1c6042ee ILT |
3305 | bfd_prpsinfo (abfd, descdata, descsz, filepos) |
3306 | bfd *abfd; | |
3307 | char *descdata; | |
3308 | int descsz; | |
3309 | long filepos; | |
32090b8e KR |
3310 | { |
3311 | asection *newsect; | |
244ffee7 | 3312 | |
32090b8e KR |
3313 | if (descsz == sizeof (prpsinfo_t)) |
3314 | { | |
3315 | if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL) | |
244ffee7 | 3316 | { |
32090b8e | 3317 | memcpy (core_prpsinfo (abfd), descdata, descsz); |
244ffee7 | 3318 | } |
244ffee7 | 3319 | } |
244ffee7 JK |
3320 | } |
3321 | ||
244ffee7 | 3322 | static void |
1c6042ee ILT |
3323 | bfd_fpregset (abfd, descdata, descsz, filepos) |
3324 | bfd *abfd; | |
3325 | char *descdata; | |
3326 | int descsz; | |
3327 | long filepos; | |
244ffee7 | 3328 | { |
32090b8e | 3329 | asection *newsect; |
244ffee7 | 3330 | |
32090b8e KR |
3331 | newsect = bfd_make_section (abfd, ".reg2"); |
3332 | newsect->_raw_size = descsz; | |
3333 | newsect->filepos = filepos; | |
3334 | newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
3335 | newsect->alignment_power = 2; | |
6a3eb9b6 | 3336 | } |
244ffee7 | 3337 | |
32090b8e KR |
3338 | #endif /* HAVE_PROCFS */ |
3339 | ||
3340 | /* Return a pointer to the args (including the command name) that were | |
3341 | seen by the program that generated the core dump. Note that for | |
3342 | some reason, a spurious space is tacked onto the end of the args | |
3343 | in some (at least one anyway) implementations, so strip it off if | |
3344 | it exists. */ | |
3345 | ||
3346 | char * | |
1c6042ee ILT |
3347 | elf_core_file_failing_command (abfd) |
3348 | bfd *abfd; | |
244ffee7 | 3349 | { |
32090b8e KR |
3350 | #ifdef HAVE_PROCFS |
3351 | if (core_prpsinfo (abfd)) | |
3352 | { | |
3353 | prpsinfo_t *p = core_prpsinfo (abfd); | |
3354 | char *scan = p->pr_psargs; | |
3355 | while (*scan++) | |
3356 | {; | |
3357 | } | |
3358 | scan -= 2; | |
3359 | if ((scan > p->pr_psargs) && (*scan == ' ')) | |
3360 | { | |
3361 | *scan = '\000'; | |
3362 | } | |
3363 | return p->pr_psargs; | |
3364 | } | |
3365 | #endif | |
3366 | return NULL; | |
3367 | } | |
244ffee7 | 3368 | |
32090b8e KR |
3369 | /* Return the number of the signal that caused the core dump. Presumably, |
3370 | since we have a core file, we got a signal of some kind, so don't bother | |
3371 | checking the other process status fields, just return the signal number. | |
3372 | */ | |
244ffee7 | 3373 | |
32090b8e | 3374 | int |
1c6042ee ILT |
3375 | elf_core_file_failing_signal (abfd) |
3376 | bfd *abfd; | |
32090b8e KR |
3377 | { |
3378 | #ifdef HAVE_PROCFS | |
3379 | if (core_prstatus (abfd)) | |
3380 | { | |
3381 | return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig; | |
3382 | } | |
3383 | #endif | |
3384 | return -1; | |
3385 | } | |
244ffee7 | 3386 | |
32090b8e KR |
3387 | /* Check to see if the core file could reasonably be expected to have |
3388 | come for the current executable file. Note that by default we return | |
3389 | true unless we find something that indicates that there might be a | |
3390 | problem. | |
3391 | */ | |
244ffee7 | 3392 | |
32090b8e | 3393 | boolean |
1c6042ee ILT |
3394 | elf_core_file_matches_executable_p (core_bfd, exec_bfd) |
3395 | bfd *core_bfd; | |
3396 | bfd *exec_bfd; | |
32090b8e KR |
3397 | { |
3398 | #ifdef HAVE_PROCFS | |
3399 | char *corename; | |
3400 | char *execname; | |
3401 | #endif | |
244ffee7 | 3402 | |
32090b8e KR |
3403 | /* First, xvecs must match since both are ELF files for the same target. */ |
3404 | ||
3405 | if (core_bfd->xvec != exec_bfd->xvec) | |
244ffee7 | 3406 | { |
d1ad85a6 | 3407 | bfd_set_error (bfd_error_system_call); |
244ffee7 JK |
3408 | return false; |
3409 | } | |
3410 | ||
32090b8e | 3411 | #ifdef HAVE_PROCFS |
244ffee7 | 3412 | |
32090b8e KR |
3413 | /* If no prpsinfo, just return true. Otherwise, grab the last component |
3414 | of the exec'd pathname from the prpsinfo. */ | |
244ffee7 | 3415 | |
32090b8e | 3416 | if (core_prpsinfo (core_bfd)) |
244ffee7 | 3417 | { |
32090b8e KR |
3418 | corename = (((struct prpsinfo *) core_prpsinfo (core_bfd))->pr_fname); |
3419 | } | |
3420 | else | |
3421 | { | |
3422 | return true; | |
3423 | } | |
244ffee7 | 3424 | |
32090b8e | 3425 | /* Find the last component of the executable pathname. */ |
244ffee7 | 3426 | |
32090b8e KR |
3427 | if ((execname = strrchr (exec_bfd->filename, '/')) != NULL) |
3428 | { | |
3429 | execname++; | |
3430 | } | |
3431 | else | |
3432 | { | |
3433 | execname = (char *) exec_bfd->filename; | |
3434 | } | |
244ffee7 | 3435 | |
32090b8e | 3436 | /* See if they match */ |
244ffee7 | 3437 | |
32090b8e | 3438 | return strcmp (execname, corename) ? false : true; |
244ffee7 | 3439 | |
32090b8e | 3440 | #else |
244ffee7 | 3441 | |
244ffee7 | 3442 | return true; |
244ffee7 | 3443 | |
32090b8e KR |
3444 | #endif /* HAVE_PROCFS */ |
3445 | } | |
244ffee7 | 3446 | |
32090b8e KR |
3447 | /* ELF core files contain a segment of type PT_NOTE, that holds much of |
3448 | the information that would normally be available from the /proc interface | |
3449 | for the process, at the time the process dumped core. Currently this | |
3450 | includes copies of the prstatus, prpsinfo, and fpregset structures. | |
244ffee7 | 3451 | |
32090b8e KR |
3452 | Since these structures are potentially machine dependent in size and |
3453 | ordering, bfd provides two levels of support for them. The first level, | |
3454 | available on all machines since it does not require that the host | |
3455 | have /proc support or the relevant include files, is to create a bfd | |
3456 | section for each of the prstatus, prpsinfo, and fpregset structures, | |
3457 | without any interpretation of their contents. With just this support, | |
3458 | the bfd client will have to interpret the structures itself. Even with | |
3459 | /proc support, it might want these full structures for it's own reasons. | |
244ffee7 | 3460 | |
32090b8e KR |
3461 | In the second level of support, where HAVE_PROCFS is defined, bfd will |
3462 | pick apart the structures to gather some additional information that | |
3463 | clients may want, such as the general register set, the name of the | |
3464 | exec'ed file and its arguments, the signal (if any) that caused the | |
3465 | core dump, etc. | |
244ffee7 | 3466 | |
32090b8e | 3467 | */ |
244ffee7 | 3468 | |
32090b8e | 3469 | static boolean |
1c6042ee ILT |
3470 | elf_corefile_note (abfd, hdr) |
3471 | bfd *abfd; | |
3472 | Elf_Internal_Phdr *hdr; | |
244ffee7 | 3473 | { |
32090b8e KR |
3474 | Elf_External_Note *x_note_p; /* Elf note, external form */ |
3475 | Elf_Internal_Note i_note; /* Elf note, internal form */ | |
3476 | char *buf = NULL; /* Entire note segment contents */ | |
3477 | char *namedata; /* Name portion of the note */ | |
3478 | char *descdata; /* Descriptor portion of the note */ | |
3479 | char *sectname; /* Name to use for new section */ | |
3480 | long filepos; /* File offset to descriptor data */ | |
3481 | asection *newsect; | |
3482 | ||
3483 | if (hdr->p_filesz > 0 | |
b9d5cdf0 | 3484 | && (buf = (char *) malloc (hdr->p_filesz)) != NULL |
32090b8e KR |
3485 | && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1 |
3486 | && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz) | |
3487 | { | |
3488 | x_note_p = (Elf_External_Note *) buf; | |
3489 | while ((char *) x_note_p < (buf + hdr->p_filesz)) | |
3490 | { | |
3491 | i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz); | |
3492 | i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz); | |
3493 | i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type); | |
3494 | namedata = x_note_p->name; | |
3495 | descdata = namedata + BFD_ALIGN (i_note.namesz, 4); | |
3496 | filepos = hdr->p_offset + (descdata - buf); | |
3497 | switch (i_note.type) | |
3498 | { | |
3499 | case NT_PRSTATUS: | |
3500 | /* process descdata as prstatus info */ | |
3501 | bfd_prstatus (abfd, descdata, i_note.descsz, filepos); | |
3502 | sectname = ".prstatus"; | |
3503 | break; | |
3504 | case NT_FPREGSET: | |
3505 | /* process descdata as fpregset info */ | |
3506 | bfd_fpregset (abfd, descdata, i_note.descsz, filepos); | |
3507 | sectname = ".fpregset"; | |
3508 | break; | |
3509 | case NT_PRPSINFO: | |
3510 | /* process descdata as prpsinfo */ | |
3511 | bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos); | |
3512 | sectname = ".prpsinfo"; | |
3513 | break; | |
3514 | default: | |
3515 | /* Unknown descriptor, just ignore it. */ | |
3516 | sectname = NULL; | |
3517 | break; | |
3518 | } | |
3519 | if (sectname != NULL) | |
3520 | { | |
3521 | newsect = bfd_make_section (abfd, sectname); | |
3522 | newsect->_raw_size = i_note.descsz; | |
3523 | newsect->filepos = filepos; | |
3524 | newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
3525 | newsect->alignment_power = 2; | |
3526 | } | |
3527 | x_note_p = (Elf_External_Note *) | |
3528 | (descdata + BFD_ALIGN (i_note.descsz, 4)); | |
3529 | } | |
3530 | } | |
3531 | if (buf != NULL) | |
3532 | { | |
3533 | free (buf); | |
3534 | } | |
b9d5cdf0 DM |
3535 | else if (hdr->p_filesz > 0) |
3536 | { | |
d1ad85a6 | 3537 | bfd_set_error (bfd_error_no_memory); |
b9d5cdf0 DM |
3538 | return false; |
3539 | } | |
32090b8e | 3540 | return true; |
244ffee7 | 3541 | |
244ffee7 JK |
3542 | } |
3543 | ||
32090b8e KR |
3544 | /* Core files are simply standard ELF formatted files that partition |
3545 | the file using the execution view of the file (program header table) | |
3546 | rather than the linking view. In fact, there is no section header | |
3547 | table in a core file. | |
3548 | ||
3549 | The process status information (including the contents of the general | |
3550 | register set) and the floating point register set are stored in a | |
3551 | segment of type PT_NOTE. We handcraft a couple of extra bfd sections | |
3552 | that allow standard bfd access to the general registers (.reg) and the | |
3553 | floating point registers (.reg2). | |
3554 | ||
3555 | */ | |
3556 | ||
3557 | bfd_target * | |
1c6042ee ILT |
3558 | elf_core_file_p (abfd) |
3559 | bfd *abfd; | |
244ffee7 | 3560 | { |
32090b8e KR |
3561 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ |
3562 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
3563 | Elf_External_Phdr x_phdr; /* Program header table entry, external form */ | |
3564 | Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */ | |
3565 | unsigned int phindex; | |
d6439785 | 3566 | struct elf_backend_data *ebd; |
244ffee7 | 3567 | |
32090b8e KR |
3568 | /* Read in the ELF header in external format. */ |
3569 | ||
3570 | if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr)) | |
244ffee7 | 3571 | { |
d1ad85a6 | 3572 | bfd_set_error (bfd_error_system_call); |
244ffee7 JK |
3573 | return NULL; |
3574 | } | |
32090b8e KR |
3575 | |
3576 | /* Now check to see if we have a valid ELF file, and one that BFD can | |
3577 | make use of. The magic number must match, the address size ('class') | |
3578 | and byte-swapping must match our XVEC entry, and it must have a | |
3579 | program header table (FIXME: See comments re segments at top of this | |
3580 | file). */ | |
3581 | ||
3582 | if (elf_file_p (&x_ehdr) == false) | |
244ffee7 | 3583 | { |
32090b8e | 3584 | wrong: |
d1ad85a6 | 3585 | bfd_set_error (bfd_error_wrong_format); |
32090b8e | 3586 | return NULL; |
244ffee7 | 3587 | } |
244ffee7 | 3588 | |
32090b8e | 3589 | /* FIXME, Check EI_VERSION here ! */ |
244ffee7 | 3590 | |
32090b8e KR |
3591 | { |
3592 | #if ARCH_SIZE == 32 | |
3593 | int desired_address_size = ELFCLASS32; | |
3594 | #endif | |
3595 | #if ARCH_SIZE == 64 | |
3596 | int desired_address_size = ELFCLASS64; | |
3597 | #endif | |
3598 | ||
3599 | if (x_ehdr.e_ident[EI_CLASS] != desired_address_size) | |
3600 | goto wrong; | |
3601 | } | |
3602 | ||
3603 | /* Switch xvec to match the specified byte order. */ | |
3604 | switch (x_ehdr.e_ident[EI_DATA]) | |
244ffee7 | 3605 | { |
32090b8e KR |
3606 | case ELFDATA2MSB: /* Big-endian */ |
3607 | if (abfd->xvec->byteorder_big_p == false) | |
3608 | goto wrong; | |
244ffee7 | 3609 | break; |
32090b8e KR |
3610 | case ELFDATA2LSB: /* Little-endian */ |
3611 | if (abfd->xvec->byteorder_big_p == true) | |
3612 | goto wrong; | |
244ffee7 | 3613 | break; |
32090b8e KR |
3614 | case ELFDATANONE: /* No data encoding specified */ |
3615 | default: /* Unknown data encoding specified */ | |
3616 | goto wrong; | |
244ffee7 JK |
3617 | } |
3618 | ||
32090b8e KR |
3619 | /* Allocate an instance of the elf_obj_tdata structure and hook it up to |
3620 | the tdata pointer in the bfd. */ | |
244ffee7 | 3621 | |
32090b8e KR |
3622 | elf_tdata (abfd) = |
3623 | (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)); | |
3624 | if (elf_tdata (abfd) == NULL) | |
244ffee7 | 3625 | { |
d1ad85a6 | 3626 | bfd_set_error (bfd_error_no_memory); |
32090b8e | 3627 | return NULL; |
244ffee7 | 3628 | } |
244ffee7 | 3629 | |
32090b8e | 3630 | /* FIXME, `wrong' returns from this point onward, leak memory. */ |
244ffee7 | 3631 | |
32090b8e KR |
3632 | /* Now that we know the byte order, swap in the rest of the header */ |
3633 | i_ehdrp = elf_elfheader (abfd); | |
3634 | elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp); | |
3635 | #if DEBUG & 1 | |
3636 | elf_debug_file (i_ehdrp); | |
3637 | #endif | |
244ffee7 | 3638 | |
d6439785 JL |
3639 | ebd = get_elf_backend_data (abfd); |
3640 | ||
3641 | /* Check that the ELF e_machine field matches what this particular | |
3642 | BFD format expects. */ | |
3643 | if (ebd->elf_machine_code != i_ehdrp->e_machine) | |
3644 | { | |
3645 | bfd_target **target_ptr; | |
3646 | ||
3647 | if (ebd->elf_machine_code != EM_NONE) | |
3648 | goto wrong; | |
3649 | ||
3650 | /* This is the generic ELF target. Let it match any ELF target | |
3651 | for which we do not have a specific backend. */ | |
3652 | for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++) | |
3653 | { | |
3654 | struct elf_backend_data *back; | |
3655 | ||
3656 | if ((*target_ptr)->flavour != bfd_target_elf_flavour) | |
3657 | continue; | |
3658 | back = (struct elf_backend_data *) (*target_ptr)->backend_data; | |
3659 | if (back->elf_machine_code == i_ehdrp->e_machine) | |
3660 | { | |
3661 | /* target_ptr is an ELF backend which matches this | |
3662 | object file, so reject the generic ELF target. */ | |
3663 | goto wrong; | |
3664 | } | |
3665 | } | |
3666 | } | |
3667 | ||
32090b8e KR |
3668 | /* If there is no program header, or the type is not a core file, then |
3669 | we are hosed. */ | |
3670 | if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE) | |
3671 | goto wrong; | |
244ffee7 | 3672 | |
32090b8e KR |
3673 | /* Allocate space for a copy of the program header table in |
3674 | internal form, seek to the program header table in the file, | |
3675 | read it in, and convert it to internal form. As a simple sanity | |
3676 | check, verify that the what BFD thinks is the size of each program | |
3677 | header table entry actually matches the size recorded in the file. */ | |
3678 | ||
3679 | if (i_ehdrp->e_phentsize != sizeof (x_phdr)) | |
3680 | goto wrong; | |
3681 | i_phdrp = (Elf_Internal_Phdr *) | |
3682 | bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum); | |
3683 | if (!i_phdrp) | |
244ffee7 | 3684 | { |
d1ad85a6 | 3685 | bfd_set_error (bfd_error_no_memory); |
32090b8e KR |
3686 | return NULL; |
3687 | } | |
3688 | if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1) | |
3689 | { | |
d1ad85a6 | 3690 | bfd_set_error (bfd_error_system_call); |
32090b8e KR |
3691 | return NULL; |
3692 | } | |
3693 | for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++) | |
3694 | { | |
3695 | if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd) | |
3696 | != sizeof (x_phdr)) | |
3697 | { | |
d1ad85a6 | 3698 | bfd_set_error (bfd_error_system_call); |
32090b8e KR |
3699 | return NULL; |
3700 | } | |
3701 | elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex); | |
244ffee7 JK |
3702 | } |
3703 | ||
32090b8e KR |
3704 | /* Once all of the program headers have been read and converted, we |
3705 | can start processing them. */ | |
244ffee7 | 3706 | |
32090b8e KR |
3707 | for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++) |
3708 | { | |
3709 | bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex); | |
3710 | if ((i_phdrp + phindex)->p_type == PT_NOTE) | |
3711 | { | |
3712 | elf_corefile_note (abfd, i_phdrp + phindex); | |
3713 | } | |
3714 | } | |
244ffee7 | 3715 | |
32090b8e | 3716 | /* Remember the entry point specified in the ELF file header. */ |
244ffee7 | 3717 | |
32090b8e | 3718 | bfd_get_start_address (abfd) = i_ehdrp->e_entry; |
244ffee7 | 3719 | |
32090b8e | 3720 | return abfd->xvec; |
244ffee7 | 3721 | } |