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