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