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