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