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