]>
Commit | Line | Data |
---|---|---|
9ce0058c SC |
1 | /* ELF support for BFD. |
2 | Copyright (C) 1991 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". | |
7 | ||
8 | This file is part of BFD, the Binary File Descriptor library. | |
9 | ||
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation; either version 2 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | This program is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with this program; if not, write to the Free Software | |
22 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
23 | ||
24 | ||
25 | /**************************************** | |
26 | ||
27 | WARNING | |
28 | ||
29 | This is only a partial ELF implementation, | |
30 | incorporating only those parts that are | |
31 | required to get gdb up and running. It is | |
32 | expected that it will be expanded to a full | |
33 | ELF implementation at some future date. | |
34 | ||
35 | Unimplemented stubs call abort() to ensure | |
36 | that they get proper attention if they are | |
37 | ever called. The stubs are here since | |
38 | this version was hacked from the COFF | |
39 | version, and thus they will probably | |
40 | go away or get expanded appropriately in a | |
41 | future version. | |
42 | ||
43 | [email protected] | |
44 | ||
45 | *****************************************/ | |
46 | ||
47 | ||
48 | /* Problems and other issues to resolve. | |
49 | ||
50 | (1) BFD expects there to be some fixed number of "sections" in | |
51 | the object file. I.E. there is a "section_count" variable in the | |
52 | bfd structure which contains the number of sections. However, ELF | |
53 | supports multiple "views" of a file. In particular, with current | |
54 | implementations, executable files typically have two tables, a | |
55 | program header table and a section header table, both of which | |
56 | partition the executable. | |
57 | ||
58 | In ELF-speak, the "linking view" of the file uses the section header | |
59 | table to access "sections" within the file, and the "execution view" | |
60 | uses the program header table to access "segments" within the file. | |
61 | "Segments" typically may contain all the data from one or more | |
62 | "sections". | |
63 | ||
64 | Note that the section header table is optional in ELF executables, | |
65 | but it is this information that is most useful to gdb. If the | |
66 | section header table is missing, then gdb should probably try | |
67 | to make do with the program header table. (FIXME) | |
68 | ||
69 | */ | |
70 | ||
9ce0058c | 71 | #include "bfd.h" |
e0796d22 | 72 | #include "sysdep.h" |
9ce0058c SC |
73 | #include "libbfd.h" |
74 | #include "obstack.h" | |
c3eb25fc SC |
75 | #include "elf/common.h" |
76 | #include "elf/internal.h" | |
77 | #include "elf/external.h" | |
9ce0058c | 78 | |
8c4a1ace JG |
79 | #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */ |
80 | #include <sys/procfs.h> | |
81 | #else | |
82 | #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */ | |
83 | #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */ | |
84 | #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */ | |
85 | #endif | |
86 | ||
9ce0058c | 87 | /* Forward data declarations */ |
8c4a1ace | 88 | |
9ce0058c SC |
89 | extern bfd_target elf_little_vec, elf_big_vec; |
90 | ||
8c4a1ace JG |
91 | /* Currently the elf_symbol_type struct just contains the generic bfd |
92 | symbol structure. */ | |
93 | ||
94 | typedef struct | |
95 | { | |
96 | asymbol symbol; | |
97 | } elf_symbol_type; | |
98 | ||
99 | /* Some private data is stashed away for future use using the tdata pointer | |
100 | in the bfd structure. This information is different for ELF core files | |
101 | and other ELF files. */ | |
102 | ||
e98e6ec1 | 103 | typedef struct elf_core_tdata_struct |
8c4a1ace JG |
104 | { |
105 | void *prstatus; /* The raw /proc prstatus structure */ | |
106 | void *prpsinfo; /* The raw /proc prpsinfo structure */ | |
107 | } elf_core_tdata; | |
108 | ||
d01cd8fc FF |
109 | #define core_prpsinfo(bfd) (((bfd)->tdata.elf_core_data) -> prpsinfo) |
110 | #define core_prstatus(bfd) (((bfd)->tdata.elf_core_data) -> prstatus) | |
8c4a1ace | 111 | |
e98e6ec1 SC |
112 | |
113 | typedef struct elf_obj_tdata_struct | |
8c4a1ace JG |
114 | { |
115 | file_ptr symtab_filepos; /* Offset to start of ELF symtab section */ | |
116 | long symtab_filesz; /* Size of ELF symtab section */ | |
117 | file_ptr strtab_filepos; /* Offset to start of ELF string tbl section */ | |
118 | long strtab_filesz; /* Size of ELF string tbl section */ | |
119 | } elf_obj_tdata; | |
120 | ||
e98e6ec1 | 121 | #define elf_tdata(bfd) ((bfd) -> tdata.elf_obj_data) |
8c4a1ace JG |
122 | #define elf_symtab_filepos(bfd) (elf_tdata(bfd) -> symtab_filepos) |
123 | #define elf_symtab_filesz(bfd) (elf_tdata(bfd) -> symtab_filesz) | |
124 | #define elf_strtab_filepos(bfd) (elf_tdata(bfd) -> strtab_filepos) | |
125 | #define elf_strtab_filesz(bfd) (elf_tdata(bfd) -> strtab_filesz) | |
126 | ||
127 | /* Translate an ELF symbol in external format into an ELF symbol in internal | |
128 | format. */ | |
129 | ||
130 | static void | |
131 | DEFUN(elf_swap_symbol_in,(abfd, src, dst), | |
132 | bfd *abfd AND | |
133 | Elf_External_Sym *src AND | |
134 | Elf_Internal_Sym *dst) | |
135 | { | |
136 | dst -> st_name = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_name); | |
137 | dst -> st_value = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_value); | |
138 | dst -> st_size = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_size); | |
139 | dst -> st_info = bfd_h_get_8 (abfd, (bfd_byte *) src -> st_info); | |
140 | dst -> st_other = bfd_h_get_8 (abfd, (bfd_byte *) src -> st_other); | |
141 | dst -> st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> st_shndx); | |
142 | } | |
143 | ||
144 | ||
e83f3040 FF |
145 | /* Translate an ELF file header in external format into an ELF file header in |
146 | internal format. */ | |
9ce0058c SC |
147 | |
148 | static void | |
8c4a1ace | 149 | DEFUN(elf_swap_ehdr_in,(abfd, src, dst), |
9ce0058c SC |
150 | bfd *abfd AND |
151 | Elf_External_Ehdr *src AND | |
152 | Elf_Internal_Ehdr *dst) | |
153 | { | |
e98e6ec1 | 154 | memcpy (dst -> e_ident, src -> e_ident, EI_NIDENT); |
9ce0058c SC |
155 | dst -> e_type = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_type); |
156 | dst -> e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_machine); | |
157 | dst -> e_version = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_version); | |
158 | dst -> e_entry = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_entry); | |
159 | dst -> e_phoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_phoff); | |
160 | dst -> e_shoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_shoff); | |
161 | dst -> e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_flags); | |
162 | dst -> e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_ehsize); | |
163 | dst -> e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phentsize); | |
164 | dst -> e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phnum); | |
165 | dst -> e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shentsize); | |
166 | dst -> e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shnum); | |
167 | dst -> e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shstrndx); | |
168 | } | |
169 | ||
170 | ||
171 | /* Translate an ELF section header table entry in external format into an | |
172 | ELF section header table entry in internal format. */ | |
173 | ||
174 | static void | |
8c4a1ace | 175 | DEFUN(elf_swap_shdr_in,(abfd, src, dst), |
9ce0058c SC |
176 | bfd *abfd AND |
177 | Elf_External_Shdr *src AND | |
178 | Elf_Internal_Shdr *dst) | |
179 | { | |
180 | dst -> sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_name); | |
181 | dst -> sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_type); | |
182 | dst -> sh_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_flags); | |
183 | dst -> sh_addr = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_addr); | |
184 | dst -> sh_offset = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_offset); | |
185 | dst -> sh_size = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_size); | |
186 | dst -> sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_link); | |
187 | dst -> sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_info); | |
188 | dst -> sh_addralign = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_addralign); | |
189 | dst -> sh_entsize = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_entsize); | |
190 | } | |
191 | ||
192 | ||
e0796d22 FF |
193 | /* Translate an ELF program header table entry in external format into an |
194 | ELF program header table entry in internal format. */ | |
195 | ||
196 | static void | |
8c4a1ace | 197 | DEFUN(elf_swap_phdr_in,(abfd, src, dst), |
e0796d22 FF |
198 | bfd *abfd AND |
199 | Elf_External_Phdr *src AND | |
200 | Elf_Internal_Phdr *dst) | |
201 | { | |
202 | dst -> p_type = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_type); | |
203 | dst -> p_offset = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_offset); | |
204 | dst -> p_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_vaddr); | |
205 | dst -> p_paddr = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_paddr); | |
206 | dst -> p_filesz = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_filesz); | |
207 | dst -> p_memsz = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_memsz); | |
208 | dst -> p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_flags); | |
209 | dst -> p_align = bfd_h_get_32 (abfd, (bfd_byte *) src -> p_align); | |
210 | } | |
211 | ||
212 | ||
9ce0058c SC |
213 | /* Create a new bfd section from an ELF section header. */ |
214 | ||
215 | static boolean | |
216 | DEFUN(bfd_section_from_shdr, (abfd, hdr, shstrtab), | |
217 | bfd *abfd AND | |
218 | Elf_Internal_Shdr *hdr AND | |
219 | char *shstrtab) | |
220 | { | |
221 | asection *newsect; | |
222 | char *name; | |
223 | ||
224 | name = hdr -> sh_name ? shstrtab + hdr -> sh_name : "unnamed"; | |
225 | newsect = bfd_make_section (abfd, name); | |
226 | newsect -> vma = hdr -> sh_addr; | |
e98e6ec1 | 227 | newsect -> _raw_size = hdr -> sh_size; |
9ce0058c SC |
228 | if (!(hdr -> sh_type == SHT_NOBITS)) |
229 | { | |
230 | newsect -> filepos = hdr -> sh_offset; | |
231 | newsect -> flags |= SEC_HAS_CONTENTS; | |
232 | } | |
233 | if (hdr -> sh_flags & SHF_ALLOC) | |
234 | { | |
235 | newsect -> flags |= SEC_ALLOC; | |
236 | if (hdr -> sh_type != SHT_NOBITS) | |
237 | { | |
238 | newsect -> flags |= SEC_LOAD; | |
239 | } | |
240 | } | |
241 | if (!(hdr -> sh_flags & SHF_WRITE)) | |
242 | { | |
243 | newsect -> flags |= SEC_READONLY; | |
244 | } | |
245 | if (hdr -> sh_flags & SHF_EXECINSTR) | |
246 | { | |
247 | newsect -> flags |= SEC_CODE; /* FIXME: may only contain SOME code */ | |
248 | } | |
e83f3040 FF |
249 | else |
250 | { | |
251 | newsect -> flags |= SEC_DATA; | |
252 | } | |
9ce0058c SC |
253 | if (hdr -> sh_type == SHT_SYMTAB) |
254 | { | |
255 | abfd -> flags |= HAS_SYMS; | |
256 | } | |
257 | ||
258 | return (true); | |
259 | } | |
260 | ||
e0796d22 FF |
261 | /* Create a new bfd section from an ELF program header. |
262 | ||
263 | Since program segments have no names, we generate a synthetic name | |
264 | of the form segment<NUM>, where NUM is generally the index in the | |
265 | program header table. For segments that are split (see below) we | |
266 | generate the names segment<NUM>a and segment<NUM>b. | |
267 | ||
268 | Note that some program segments may have a file size that is different than | |
269 | (less than) the memory size. All this means is that at execution the | |
270 | system must allocate the amount of memory specified by the memory size, | |
271 | but only initialize it with the first "file size" bytes read from the | |
272 | file. This would occur for example, with program segments consisting | |
273 | of combined data+bss. | |
274 | ||
275 | To handle the above situation, this routine generates TWO bfd sections | |
276 | for the single program segment. The first has the length specified by | |
277 | the file size of the segment, and the second has the length specified | |
278 | by the difference between the two sizes. In effect, the segment is split | |
279 | into it's initialized and uninitialized parts. | |
280 | ||
281 | */ | |
282 | ||
283 | static boolean | |
284 | DEFUN(bfd_section_from_phdr, (abfd, hdr, index), | |
285 | bfd *abfd AND | |
286 | Elf_Internal_Phdr *hdr AND | |
287 | int index) | |
288 | { | |
289 | asection *newsect; | |
290 | char *name; | |
291 | char namebuf[64]; | |
292 | int split; | |
293 | ||
294 | split = ((hdr -> p_memsz > 0) && | |
295 | (hdr -> p_filesz > 0) && | |
296 | (hdr -> p_memsz > hdr -> p_filesz)); | |
297 | sprintf (namebuf, split ? "segment%da" : "segment%d", index); | |
298 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
299 | (void) strcpy (name, namebuf); | |
300 | newsect = bfd_make_section (abfd, name); | |
301 | newsect -> vma = hdr -> p_vaddr; | |
e98e6ec1 | 302 | newsect -> _raw_size = hdr -> p_filesz; |
e0796d22 FF |
303 | newsect -> filepos = hdr -> p_offset; |
304 | newsect -> flags |= SEC_HAS_CONTENTS; | |
305 | if (hdr -> p_type == PT_LOAD) | |
306 | { | |
307 | newsect -> flags |= SEC_ALLOC; | |
308 | newsect -> flags |= SEC_LOAD; | |
309 | if (hdr -> p_flags & PF_X) | |
310 | { | |
311 | /* FIXME: all we known is that it has execute PERMISSION, | |
312 | may be data. */ | |
313 | newsect -> flags |= SEC_CODE; | |
314 | } | |
315 | } | |
316 | if (!(hdr -> p_flags & PF_W)) | |
317 | { | |
318 | newsect -> flags |= SEC_READONLY; | |
319 | } | |
320 | ||
321 | if (split) | |
322 | { | |
323 | sprintf (namebuf, "segment%db", index); | |
324 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
325 | (void) strcpy (name, namebuf); | |
326 | newsect = bfd_make_section (abfd, name); | |
327 | newsect -> vma = hdr -> p_vaddr + hdr -> p_filesz; | |
e98e6ec1 | 328 | newsect -> _raw_size = hdr -> p_memsz - hdr -> p_filesz; |
e0796d22 FF |
329 | if (hdr -> p_type == PT_LOAD) |
330 | { | |
331 | newsect -> flags |= SEC_ALLOC; | |
332 | if (hdr -> p_flags & PF_X) | |
333 | { | |
334 | newsect -> flags |= SEC_CODE; | |
335 | } | |
336 | } | |
337 | if (!(hdr -> p_flags & PF_W)) | |
338 | { | |
339 | newsect -> flags |= SEC_READONLY; | |
340 | } | |
341 | } | |
342 | ||
343 | return (true); | |
344 | } | |
345 | ||
8c4a1ace JG |
346 | #ifdef HAVE_PROCFS |
347 | ||
348 | static void | |
349 | DEFUN(bfd_prstatus,(abfd, descdata, descsz, filepos), | |
350 | bfd *abfd AND | |
351 | char *descdata AND | |
352 | int descsz AND | |
353 | long filepos) | |
354 | { | |
355 | asection *newsect; | |
356 | ||
357 | if (descsz == sizeof (prstatus_t)) | |
358 | { | |
359 | newsect = bfd_make_section (abfd, ".reg"); | |
d01cd8fc | 360 | newsect -> _raw_size = sizeof (gregset_t); |
8c4a1ace JG |
361 | newsect -> filepos = filepos + (long) (((prstatus_t *)0) -> pr_reg); |
362 | newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
363 | newsect -> alignment_power = 2; | |
364 | if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL) | |
365 | { | |
e98e6ec1 | 366 | memcpy (core_prstatus (abfd), descdata, descsz); |
8c4a1ace JG |
367 | } |
368 | } | |
369 | } | |
370 | ||
371 | /* Stash a copy of the prpsinfo structure away for future use. */ | |
372 | ||
373 | static void | |
374 | DEFUN(bfd_prpsinfo,(abfd, descdata, descsz, filepos), | |
375 | bfd *abfd AND | |
376 | char *descdata AND | |
377 | int descsz AND | |
378 | long filepos) | |
379 | { | |
380 | asection *newsect; | |
381 | ||
382 | if (descsz == sizeof (prpsinfo_t)) | |
383 | { | |
384 | if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL) | |
385 | { | |
386 | bcopy (descdata, core_prpsinfo (abfd), descsz); | |
387 | } | |
388 | } | |
389 | } | |
390 | ||
391 | static void | |
392 | DEFUN(bfd_fpregset,(abfd, descdata, descsz, filepos), | |
393 | bfd *abfd AND | |
394 | char *descdata AND | |
395 | int descsz AND | |
396 | long filepos) | |
397 | { | |
398 | asection *newsect; | |
399 | ||
400 | if (descsz == sizeof (fpregset_t)) | |
401 | { | |
402 | newsect = bfd_make_section (abfd, ".reg2"); | |
d01cd8fc | 403 | newsect -> _raw_size = sizeof (fpregset_t); |
8c4a1ace JG |
404 | newsect -> filepos = filepos; |
405 | newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
406 | newsect -> alignment_power = 2; | |
407 | } | |
408 | } | |
409 | ||
410 | #endif /* HAVE_PROCFS */ | |
411 | ||
412 | /* Return a pointer to the args (including the command name) that were | |
413 | seen by the program that generated the core dump. Note that for | |
414 | some reason, a spurious space is tacked onto the end of the args | |
415 | in some (at least one anyway) implementations, so strip it off if | |
416 | it exists. */ | |
417 | ||
418 | char * | |
419 | DEFUN(elf_core_file_failing_command, (abfd), | |
420 | bfd *abfd) | |
421 | { | |
e98e6ec1 | 422 | #ifdef HAVE_PROCFS |
8c4a1ace JG |
423 | if (core_prpsinfo (abfd)) |
424 | { | |
425 | prpsinfo_t *p = core_prpsinfo (abfd); | |
426 | char *scan = p -> pr_psargs; | |
427 | while (*scan++) {;} | |
428 | scan -= 2; | |
429 | if ((scan > p -> pr_psargs) && (*scan == ' ')) | |
430 | { | |
431 | *scan = '\000'; | |
432 | } | |
433 | return (p -> pr_psargs); | |
434 | } | |
435 | #endif | |
436 | return (NULL); | |
437 | } | |
438 | ||
439 | /* Return the number of the signal that caused the core dump. Presumably, | |
440 | since we have a core file, we got a signal of some kind, so don't bother | |
441 | checking the other process status fields, just return the signal number. | |
442 | */ | |
443 | ||
444 | static int | |
445 | DEFUN(elf_core_file_failing_signal, (abfd), | |
446 | bfd *abfd) | |
447 | { | |
e98e6ec1 | 448 | #ifdef HAVE_PROCFS |
8c4a1ace JG |
449 | if (core_prstatus (abfd)) |
450 | { | |
451 | return (((prstatus_t *)(core_prstatus (abfd))) -> pr_cursig); | |
452 | } | |
453 | #endif | |
454 | return (-1); | |
455 | } | |
456 | ||
457 | /* Check to see if the core file could reasonably be expected to have | |
458 | come for the current executable file. Note that by default we return | |
459 | true unless we find something that indicates that there might be a | |
460 | problem. | |
461 | */ | |
462 | ||
463 | static boolean | |
464 | DEFUN(elf_core_file_matches_executable_p, (core_bfd, exec_bfd), | |
465 | bfd *core_bfd AND | |
466 | bfd *exec_bfd) | |
467 | { | |
e98e6ec1 | 468 | #ifdef HAVE_PROCFS |
8c4a1ace JG |
469 | char *corename; |
470 | char *execname; | |
e83f3040 | 471 | #endif |
8c4a1ace JG |
472 | |
473 | /* First, xvecs must match since both are ELF files for the same target. */ | |
474 | ||
475 | if (core_bfd->xvec != exec_bfd->xvec) | |
476 | { | |
477 | bfd_error = system_call_error; | |
478 | return (false); | |
479 | } | |
480 | ||
e98e6ec1 | 481 | #ifdef HAVE_PROCFS |
8c4a1ace JG |
482 | |
483 | /* If no prpsinfo, just return true. Otherwise, grab the last component | |
484 | of the exec'd pathname from the prpsinfo. */ | |
485 | ||
486 | if (core_prpsinfo (core_bfd)) | |
487 | { | |
488 | corename = (((struct prpsinfo *) core_prpsinfo (core_bfd)) -> pr_fname); | |
489 | } | |
490 | else | |
491 | { | |
492 | return (true); | |
493 | } | |
494 | ||
495 | /* Find the last component of the executable pathname. */ | |
496 | ||
497 | if ((execname = strrchr (exec_bfd -> filename, '/')) != NULL) | |
498 | { | |
499 | execname++; | |
500 | } | |
501 | else | |
502 | { | |
503 | execname = (char *) exec_bfd -> filename; | |
504 | } | |
505 | ||
506 | /* See if they match */ | |
507 | ||
508 | return (strcmp (execname, corename) ? false : true); | |
509 | ||
510 | #else | |
511 | ||
512 | return (true); | |
513 | ||
514 | #endif /* HAVE_PROCFS */ | |
515 | } | |
516 | ||
517 | /* ELF core files contain a segment of type PT_NOTE, that holds much of | |
518 | the information that would normally be available from the /proc interface | |
519 | for the process, at the time the process dumped core. Currently this | |
520 | includes copies of the prstatus, prpsinfo, and fpregset structures. | |
521 | ||
522 | Since these structures are potentially machine dependent in size and | |
523 | ordering, bfd provides two levels of support for them. The first level, | |
524 | available on all machines since it does not require that the host | |
525 | have /proc support or the relevant include files, is to create a bfd | |
526 | section for each of the prstatus, prpsinfo, and fpregset structures, | |
527 | without any interpretation of their contents. With just this support, | |
528 | the bfd client will have to interpret the structures itself. Even with | |
529 | /proc support, it might want these full structures for it's own reasons. | |
530 | ||
531 | In the second level of support, where HAVE_PROCFS is defined, bfd will | |
532 | pick apart the structures to gather some additional information that | |
533 | clients may want, such as the general register set, the name of the | |
534 | exec'ed file and its arguments, the signal (if any) that caused the | |
535 | core dump, etc. | |
536 | ||
537 | */ | |
538 | ||
539 | static boolean | |
540 | DEFUN(elf_corefile_note, (abfd, hdr), | |
541 | bfd *abfd AND | |
542 | Elf_Internal_Phdr *hdr) | |
543 | { | |
544 | Elf_External_Note *x_note_p; /* Elf note, external form */ | |
545 | Elf_Internal_Note i_note; /* Elf note, internal form */ | |
546 | char *buf = NULL; /* Entire note segment contents */ | |
547 | char *namedata; /* Name portion of the note */ | |
548 | char *descdata; /* Descriptor portion of the note */ | |
549 | char *sectname; /* Name to use for new section */ | |
550 | long filepos; /* File offset to descriptor data */ | |
551 | asection *newsect; | |
552 | ||
553 | if (hdr -> p_filesz > 0 | |
e83f3040 | 554 | && (buf = (char *)malloc(hdr -> p_filesz)) != NULL |
8c4a1ace JG |
555 | && bfd_seek (abfd, hdr -> p_offset, SEEK_SET) != -1L |
556 | && bfd_read ((PTR) buf, hdr -> p_filesz, 1, abfd) == hdr -> p_filesz) | |
557 | { | |
558 | x_note_p = (Elf_External_Note *) buf; | |
559 | while ((char *) x_note_p < (buf + hdr -> p_filesz)) | |
560 | { | |
561 | i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> namesz); | |
562 | i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> descsz); | |
563 | i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> type); | |
564 | namedata = x_note_p -> name; | |
565 | descdata = namedata + BFD_ALIGN (i_note.namesz, 4); | |
566 | filepos = hdr -> p_offset + (descdata - buf); | |
567 | switch (i_note.type) { | |
568 | case NT_PRSTATUS: | |
569 | /* process descdata as prstatus info */ | |
570 | bfd_prstatus (abfd, descdata, i_note.descsz, filepos); | |
571 | sectname = ".prstatus"; | |
572 | break; | |
573 | case NT_FPREGSET: | |
574 | /* process descdata as fpregset info */ | |
575 | bfd_fpregset (abfd, descdata, i_note.descsz, filepos); | |
576 | sectname = ".fpregset"; | |
577 | break; | |
578 | case NT_PRPSINFO: | |
579 | /* process descdata as prpsinfo */ | |
580 | bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos); | |
581 | sectname = ".prpsinfo"; | |
582 | break; | |
583 | default: | |
584 | /* Unknown descriptor, just ignore it. */ | |
585 | sectname = NULL; | |
586 | break; | |
587 | } | |
588 | if (sectname != NULL) | |
589 | { | |
590 | newsect = bfd_make_section (abfd, sectname); | |
e98e6ec1 | 591 | newsect -> _raw_size = i_note.descsz; |
8c4a1ace JG |
592 | newsect -> filepos = filepos; |
593 | newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
594 | newsect -> alignment_power = 2; | |
595 | } | |
596 | x_note_p = (Elf_External_Note *) | |
597 | (descdata + BFD_ALIGN (i_note.descsz, 4)); | |
598 | } | |
599 | } | |
600 | if (buf != NULL) | |
601 | { | |
602 | free (buf); | |
603 | } | |
e83f3040 FF |
604 | return true; |
605 | ||
8c4a1ace JG |
606 | } |
607 | ||
608 | ||
e83f3040 FF |
609 | /* Read a specified number of bytes at a specified offset in an ELF |
610 | file, into a newly allocated buffer, and return a pointer to the | |
611 | buffer. */ | |
612 | ||
613 | static char * | |
614 | DEFUN(elf_read, (abfd, offset, size), | |
615 | bfd *abfd AND | |
616 | long offset AND | |
617 | int size) | |
618 | { | |
619 | char *buf; | |
620 | ||
621 | if ((buf = bfd_alloc (abfd, size)) == NULL) | |
622 | { | |
623 | bfd_error = no_memory; | |
624 | return (NULL); | |
625 | } | |
626 | if (bfd_seek (abfd, offset, SEEK_SET) == -1) | |
627 | { | |
628 | bfd_error = system_call_error; | |
629 | return (NULL); | |
630 | } | |
631 | if (bfd_read ((PTR) buf, size, 1, abfd) != size) | |
632 | { | |
633 | bfd_error = system_call_error; | |
634 | return (NULL); | |
635 | } | |
636 | return (buf); | |
637 | } | |
638 | ||
9ce0058c SC |
639 | /* Begin processing a given object. |
640 | ||
641 | First we validate the file by reading in the ELF header and checking | |
642 | the magic number. | |
643 | ||
644 | */ | |
645 | ||
646 | static bfd_target * | |
647 | DEFUN (elf_object_p, (abfd), bfd *abfd) | |
648 | { | |
649 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ | |
650 | Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */ | |
651 | Elf_External_Shdr *x_shdr; /* Section header table, external form */ | |
652 | Elf_Internal_Shdr *i_shdr; /* Section header table, internal form */ | |
653 | int shindex; | |
654 | char *shstrtab; /* Internal copy of section header stringtab */ | |
655 | int shstrtabsize; /* Size of section header string table */ | |
e83f3040 | 656 | Elf_Off offset; /* Temp place to stash file offsets */ |
9ce0058c SC |
657 | |
658 | /* Read in the ELF header in external format. */ | |
659 | ||
660 | if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr)) | |
661 | { | |
662 | bfd_error = system_call_error; | |
663 | return (NULL); | |
664 | } | |
665 | ||
666 | /* Now check to see if we have a valid ELF file, and one that BFD can | |
667 | make use of. The magic number must match, the address size ('class') | |
668 | and byte-swapping must match our XVEC entry, and it must have a | |
669 | section header table (FIXME: See comments re sections at top of this | |
670 | file). */ | |
671 | ||
672 | if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 || | |
673 | x_ehdr.e_ident[EI_MAG1] != ELFMAG1 || | |
674 | x_ehdr.e_ident[EI_MAG2] != ELFMAG2 || | |
675 | x_ehdr.e_ident[EI_MAG3] != ELFMAG3) | |
676 | { | |
677 | wrong: | |
678 | bfd_error = wrong_format; | |
679 | return (NULL); | |
680 | } | |
681 | ||
682 | /* FIXME, Check EI_VERSION here ! */ | |
683 | ||
684 | switch (x_ehdr.e_ident[EI_CLASS]) { | |
685 | case ELFCLASSNONE: /* address size not specified */ | |
686 | goto wrong; /* No support if can't tell address size */ | |
687 | case ELFCLASS32: /* 32-bit addresses */ | |
688 | break; | |
689 | case ELFCLASS64: /* 64-bit addresses */ | |
690 | goto wrong; /* FIXME: 64 bits not yet supported */ | |
691 | default: | |
692 | goto wrong; /* No support if unknown address class */ | |
693 | } | |
694 | ||
695 | /* Switch xvec to match the specified byte order. */ | |
696 | switch (x_ehdr.e_ident[EI_DATA]) { | |
697 | case ELFDATA2MSB: /* Big-endian */ | |
698 | abfd->xvec = &elf_big_vec; | |
699 | break; | |
700 | case ELFDATA2LSB: /* Little-endian */ | |
701 | abfd->xvec = &elf_little_vec; | |
eb8983c9 | 702 | break; |
9ce0058c SC |
703 | case ELFDATANONE: /* No data encoding specified */ |
704 | default: /* Unknown data encoding specified */ | |
705 | goto wrong; | |
706 | } | |
707 | ||
8c4a1ace JG |
708 | /* Allocate an instance of the elf_obj_tdata structure and hook it up to |
709 | the tdata pointer in the bfd. */ | |
710 | ||
e98e6ec1 SC |
711 | if ((abfd -> tdata.elf_obj_data = |
712 | (elf_obj_tdata*) bfd_zalloc (abfd, sizeof (elf_obj_tdata))) | |
713 | == NULL) | |
8c4a1ace JG |
714 | { |
715 | bfd_error = no_memory; | |
716 | return (NULL); | |
717 | } | |
718 | ||
9ce0058c | 719 | /* Now that we know the byte order, swap in the rest of the header */ |
8c4a1ace | 720 | elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr); |
e0796d22 FF |
721 | |
722 | /* If there is no section header table, we're hosed. */ | |
723 | if (i_ehdr.e_shoff == 0) | |
9ce0058c SC |
724 | goto wrong; |
725 | ||
726 | if (i_ehdr.e_type == ET_EXEC || i_ehdr.e_type == ET_DYN) | |
727 | { | |
728 | abfd -> flags |= EXEC_P; | |
729 | } | |
730 | ||
731 | /* Allocate space for copies of the section header table in external | |
732 | and internal form, seek to the section header table in the file, | |
733 | read it in, and convert it to internal form. As a simple sanity | |
734 | check, verify that the what BFD thinks is the size of each section | |
735 | header table entry actually matches the size recorded in the file. */ | |
736 | ||
737 | if (i_ehdr.e_shentsize != sizeof (*x_shdr)) | |
738 | goto wrong; | |
739 | if ((x_shdr = (Elf_External_Shdr *) | |
740 | bfd_alloc (abfd, sizeof (*x_shdr) * i_ehdr.e_shnum)) == NULL) | |
741 | { | |
742 | bfd_error = no_memory; | |
743 | return (NULL); | |
744 | } | |
745 | if ((i_shdr = (Elf_Internal_Shdr *) | |
746 | bfd_alloc (abfd, sizeof (*i_shdr) * i_ehdr.e_shnum)) == NULL) | |
747 | { | |
748 | bfd_error = no_memory; | |
749 | return (NULL); | |
750 | } | |
751 | if (bfd_seek (abfd, i_ehdr.e_shoff, SEEK_SET) == -1) | |
752 | { | |
753 | bfd_error = system_call_error; | |
754 | return (NULL); | |
755 | } | |
756 | for (shindex = 0; shindex < i_ehdr.e_shnum; shindex++) | |
757 | { | |
758 | if (bfd_read ((PTR) (x_shdr + shindex), sizeof (*x_shdr), 1, abfd) | |
759 | != sizeof (*x_shdr)) | |
760 | { | |
761 | bfd_error = system_call_error; | |
762 | return (NULL); | |
763 | } | |
8c4a1ace | 764 | elf_swap_shdr_in (abfd, x_shdr + shindex, i_shdr + shindex); |
9ce0058c SC |
765 | } |
766 | ||
767 | /* Read in the string table containing the names of the sections. We | |
768 | will need the base pointer to this table later. */ | |
769 | ||
770 | shstrtabsize = i_shdr[i_ehdr.e_shstrndx].sh_size; | |
e83f3040 FF |
771 | offset = i_shdr[i_ehdr.e_shstrndx].sh_offset; |
772 | if ((shstrtab = elf_read (abfd, offset, shstrtabsize)) == NULL) | |
9ce0058c | 773 | { |
9ce0058c SC |
774 | return (NULL); |
775 | } | |
e83f3040 | 776 | |
9ce0058c | 777 | /* Once all of the section headers have been read and converted, we |
a6c1d731 | 778 | can start processing them. Note that the first section header is |
8c4a1ace JG |
779 | a dummy placeholder entry, so we ignore it. |
780 | ||
781 | We also watch for the symbol table section and remember the file | |
782 | offset and section size for both the symbol table section and the | |
783 | associated string table section. */ | |
9ce0058c | 784 | |
a6c1d731 | 785 | for (shindex = 1; shindex < i_ehdr.e_shnum; shindex++) |
9ce0058c | 786 | { |
8c4a1ace JG |
787 | Elf_Internal_Shdr *hdr = i_shdr + shindex; |
788 | bfd_section_from_shdr (abfd, hdr, shstrtab); | |
789 | if (hdr -> sh_type == SHT_SYMTAB) | |
790 | { | |
791 | elf_symtab_filepos(abfd) = hdr -> sh_offset; | |
792 | elf_symtab_filesz(abfd) = hdr -> sh_size; | |
793 | elf_strtab_filepos(abfd) = (i_shdr + hdr -> sh_link) -> sh_offset; | |
794 | elf_strtab_filesz(abfd) = (i_shdr + hdr -> sh_link) -> sh_size; | |
795 | } | |
9ce0058c SC |
796 | } |
797 | ||
e83f3040 FF |
798 | /* Remember the entry point specified in the ELF file header. */ |
799 | ||
800 | bfd_get_start_address (abfd) = i_ehdr.e_entry; | |
801 | ||
9ce0058c SC |
802 | return (abfd->xvec); |
803 | } | |
804 | ||
e0796d22 FF |
805 | /* Core files are simply standard ELF formatted files that partition |
806 | the file using the execution view of the file (program header table) | |
807 | rather than the linking view. In fact, there is no section header | |
808 | table in a core file. | |
8c4a1ace JG |
809 | |
810 | The process status information (including the contents of the general | |
811 | register set) and the floating point register set are stored in a | |
812 | segment of type PT_NOTE. We handcraft a couple of extra bfd sections | |
813 | that allow standard bfd access to the general registers (.reg) and the | |
814 | floating point registers (.reg2). | |
815 | ||
e0796d22 FF |
816 | */ |
817 | ||
818 | static bfd_target * | |
819 | DEFUN (elf_core_file_p, (abfd), bfd *abfd) | |
820 | { | |
821 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ | |
822 | Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */ | |
823 | Elf_External_Phdr *x_phdr; /* Program header table, external form */ | |
824 | Elf_Internal_Phdr *i_phdr; /* Program header table, internal form */ | |
825 | int phindex; | |
826 | ||
827 | /* Read in the ELF header in external format. */ | |
828 | ||
829 | if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr)) | |
830 | { | |
831 | bfd_error = system_call_error; | |
832 | return (NULL); | |
833 | } | |
834 | ||
835 | /* Now check to see if we have a valid ELF file, and one that BFD can | |
836 | make use of. The magic number must match, the address size ('class') | |
837 | and byte-swapping must match our XVEC entry, and it must have a | |
838 | program header table (FIXME: See comments re segments at top of this | |
839 | file). */ | |
840 | ||
841 | if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 || | |
842 | x_ehdr.e_ident[EI_MAG1] != ELFMAG1 || | |
843 | x_ehdr.e_ident[EI_MAG2] != ELFMAG2 || | |
844 | x_ehdr.e_ident[EI_MAG3] != ELFMAG3) | |
845 | { | |
846 | wrong: | |
847 | bfd_error = wrong_format; | |
848 | return (NULL); | |
849 | } | |
850 | ||
851 | /* FIXME, Check EI_VERSION here ! */ | |
852 | ||
853 | switch (x_ehdr.e_ident[EI_CLASS]) { | |
854 | case ELFCLASSNONE: /* address size not specified */ | |
855 | goto wrong; /* No support if can't tell address size */ | |
856 | case ELFCLASS32: /* 32-bit addresses */ | |
857 | break; | |
858 | case ELFCLASS64: /* 64-bit addresses */ | |
859 | goto wrong; /* FIXME: 64 bits not yet supported */ | |
860 | default: | |
861 | goto wrong; /* No support if unknown address class */ | |
862 | } | |
863 | ||
864 | /* Switch xvec to match the specified byte order. */ | |
865 | switch (x_ehdr.e_ident[EI_DATA]) { | |
866 | case ELFDATA2MSB: /* Big-endian */ | |
867 | abfd->xvec = &elf_big_vec; | |
868 | break; | |
869 | case ELFDATA2LSB: /* Little-endian */ | |
870 | abfd->xvec = &elf_little_vec; | |
eb8983c9 | 871 | break; |
e0796d22 FF |
872 | case ELFDATANONE: /* No data encoding specified */ |
873 | default: /* Unknown data encoding specified */ | |
874 | goto wrong; | |
875 | } | |
876 | ||
877 | /* Now that we know the byte order, swap in the rest of the header */ | |
8c4a1ace | 878 | elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr); |
e0796d22 FF |
879 | |
880 | /* If there is no program header, or the type is not a core file, then | |
881 | we are hosed. */ | |
882 | if (i_ehdr.e_phoff == 0 || i_ehdr.e_type != ET_CORE) | |
883 | goto wrong; | |
884 | ||
8c4a1ace JG |
885 | /* Allocate an instance of the elf_core_tdata structure and hook it up to |
886 | the tdata pointer in the bfd. */ | |
887 | ||
e98e6ec1 SC |
888 | if ((abfd -> tdata.elf_core_data = |
889 | (elf_core_tdata *) bfd_zalloc (abfd, sizeof (elf_core_tdata))) | |
890 | == NULL) | |
8c4a1ace JG |
891 | { |
892 | bfd_error = no_memory; | |
893 | return (NULL); | |
894 | } | |
895 | ||
e0796d22 FF |
896 | /* Allocate space for copies of the program header table in external |
897 | and internal form, seek to the program header table in the file, | |
898 | read it in, and convert it to internal form. As a simple sanity | |
899 | check, verify that the what BFD thinks is the size of each program | |
900 | header table entry actually matches the size recorded in the file. */ | |
901 | ||
902 | if (i_ehdr.e_phentsize != sizeof (*x_phdr)) | |
903 | goto wrong; | |
904 | if ((x_phdr = (Elf_External_Phdr *) | |
905 | bfd_alloc (abfd, sizeof (*x_phdr) * i_ehdr.e_phnum)) == NULL) | |
906 | { | |
907 | bfd_error = no_memory; | |
908 | return (NULL); | |
909 | } | |
910 | if ((i_phdr = (Elf_Internal_Phdr *) | |
911 | bfd_alloc (abfd, sizeof (*i_phdr) * i_ehdr.e_phnum)) == NULL) | |
912 | { | |
913 | bfd_error = no_memory; | |
914 | return (NULL); | |
915 | } | |
916 | if (bfd_seek (abfd, i_ehdr.e_phoff, SEEK_SET) == -1) | |
917 | { | |
918 | bfd_error = system_call_error; | |
919 | return (NULL); | |
920 | } | |
921 | for (phindex = 0; phindex < i_ehdr.e_phnum; phindex++) | |
922 | { | |
923 | if (bfd_read ((PTR) (x_phdr + phindex), sizeof (*x_phdr), 1, abfd) | |
924 | != sizeof (*x_phdr)) | |
925 | { | |
926 | bfd_error = system_call_error; | |
927 | return (NULL); | |
928 | } | |
8c4a1ace | 929 | elf_swap_phdr_in (abfd, x_phdr + phindex, i_phdr + phindex); |
e0796d22 FF |
930 | } |
931 | ||
932 | /* Once all of the program headers have been read and converted, we | |
933 | can start processing them. */ | |
934 | ||
935 | for (phindex = 0; phindex < i_ehdr.e_phnum; phindex++) | |
936 | { | |
937 | bfd_section_from_phdr (abfd, i_phdr + phindex, phindex); | |
8c4a1ace JG |
938 | if ((i_phdr + phindex) -> p_type == PT_NOTE) |
939 | { | |
940 | elf_corefile_note (abfd, i_phdr + phindex); | |
941 | } | |
e0796d22 FF |
942 | } |
943 | ||
e83f3040 FF |
944 | /* Remember the entry point specified in the ELF file header. */ |
945 | ||
946 | bfd_get_start_address (abfd) = i_ehdr.e_entry; | |
947 | ||
e0796d22 FF |
948 | return (abfd->xvec); |
949 | } | |
950 | ||
9ce0058c SC |
951 | static boolean |
952 | DEFUN (elf_mkobject, (abfd), bfd *abfd) | |
953 | { | |
954 | fprintf (stderr, "elf_mkobject unimplemented\n"); | |
955 | fflush (stderr); | |
956 | abort (); | |
957 | return (false); | |
958 | } | |
959 | ||
960 | static boolean | |
961 | DEFUN (elf_write_object_contents, (abfd), bfd *abfd) | |
962 | { | |
963 | fprintf (stderr, "elf_write_object_contents unimplemented\n"); | |
964 | fflush (stderr); | |
965 | abort (); | |
966 | return (false); | |
967 | } | |
968 | ||
8c4a1ace JG |
969 | /* Given an index of a section, retrieve a pointer to it. Note |
970 | that for our purposes, sections are indexed by {1, 2, ...} with | |
971 | 0 being an illegal index. */ | |
972 | ||
973 | static struct sec * | |
974 | DEFUN (section_from_bfd_index, (abfd, index), | |
975 | bfd *abfd AND | |
976 | int index) | |
977 | { | |
978 | if (index > 0) | |
979 | { | |
980 | struct sec *answer = abfd -> sections; | |
981 | while (--index > 0) | |
982 | { | |
983 | answer = answer -> next; | |
984 | } | |
985 | return (answer); | |
986 | } | |
987 | return (NULL); | |
988 | } | |
989 | ||
990 | static boolean | |
991 | DEFUN (elf_slurp_symbol_table, (abfd), bfd *abfd) | |
992 | { | |
993 | int symcount; /* Number of external ELF symbols */ | |
994 | char *strtab; /* Buffer for raw ELF string table section */ | |
995 | asymbol *sym; /* Pointer to current bfd symbol */ | |
996 | asymbol *symbase; /* Buffer for generated bfd symbols */ | |
997 | asymbol **vec; /* Pointer to current bfd symbol pointer */ | |
998 | Elf_Internal_Sym i_sym; | |
999 | Elf_External_Sym x_sym; | |
1000 | ||
1001 | if (bfd_get_outsymbols (abfd) != NULL) | |
1002 | { | |
1003 | return (true); | |
1004 | } | |
1005 | ||
1006 | /* Slurp in the string table. We will keep it around permanently, as | |
1007 | long as the bfd is in use, since we will end up setting up pointers | |
1008 | into it for the names of all the symbols. */ | |
1009 | ||
e83f3040 FF |
1010 | strtab = elf_read (abfd, elf_strtab_filepos(abfd), elf_strtab_filesz(abfd)); |
1011 | if (strtab == NULL) | |
8c4a1ace | 1012 | { |
8c4a1ace JG |
1013 | return (false); |
1014 | } | |
1015 | ||
1016 | /* Read each raw ELF symbol, converting from external ELF form to | |
1017 | internal ELF form, and then using the information to create a | |
1018 | canonical bfd symbol table entry. | |
1019 | ||
1020 | Note that be allocate the initial bfd canonical symbol buffer | |
1021 | based on a one-to-one mapping of the ELF symbols to canonical | |
1022 | symbols. However, it is likely that not all the ELF symbols will | |
1023 | be used, so there will be some space leftover at the end. Once | |
1024 | we know how many symbols we actual generate, we realloc the buffer | |
1025 | to the correct size and then build the pointer vector. */ | |
1026 | ||
1027 | if (bfd_seek (abfd, elf_symtab_filepos (abfd), SEEK_SET) == -1) | |
1028 | { | |
1029 | bfd_error = system_call_error; | |
1030 | return (false); | |
1031 | } | |
1032 | ||
1033 | symcount = elf_symtab_filesz(abfd) / sizeof (Elf_External_Sym); | |
1034 | sym = symbase = (asymbol *) bfd_zalloc (abfd, symcount * sizeof (asymbol)); | |
1035 | ||
1036 | while (symcount-- > 0) | |
1037 | { | |
1038 | if (bfd_read ((PTR) &x_sym, sizeof (x_sym), 1, abfd) != sizeof (x_sym)) | |
1039 | { | |
1040 | bfd_error = system_call_error; | |
1041 | return (false); | |
1042 | } | |
1043 | elf_swap_symbol_in (abfd, &x_sym, &i_sym); | |
1044 | if (i_sym.st_name > 0) | |
1045 | { | |
1046 | sym -> the_bfd = abfd; | |
1047 | sym -> name = strtab + i_sym.st_name; | |
1048 | sym -> value = i_sym.st_value; | |
1049 | if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV) | |
1050 | { | |
1051 | /* Note: This code depends upon there being an ordered | |
1052 | one-for-one mapping of ELF sections to bfd sections. */ | |
1053 | sym -> section = section_from_bfd_index (abfd, i_sym.st_shndx); | |
1054 | } | |
1055 | else if (i_sym.st_shndx == SHN_ABS) | |
1056 | { | |
e98e6ec1 | 1057 | /* sym -> flags |= BSF_ABSOLUTE; OBSOLETE */ |
8c4a1ace JG |
1058 | } |
1059 | else if (i_sym.st_shndx == SHN_COMMON) | |
1060 | { | |
e98e6ec1 | 1061 | sym -> section = &bfd_com_section; |
8c4a1ace JG |
1062 | } |
1063 | switch (ELF_ST_BIND (i_sym.st_info)) | |
1064 | { | |
1065 | case STB_LOCAL: | |
1066 | sym -> flags |= BSF_LOCAL; | |
1067 | break; | |
1068 | case STB_GLOBAL: | |
1069 | sym -> flags |= (BSF_GLOBAL | BSF_EXPORT); | |
1070 | break; | |
1071 | case STB_WEAK: | |
1072 | sym -> flags |= BSF_WEAK; | |
1073 | break; | |
1074 | } | |
1075 | sym++; | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | bfd_get_symcount(abfd) = symcount = sym - symbase; | |
1080 | sym = symbase = (asymbol *) | |
1081 | bfd_realloc (abfd, symbase, symcount * sizeof (asymbol)); | |
1082 | bfd_get_outsymbols(abfd) = vec = (asymbol **) | |
1083 | bfd_alloc (abfd, symcount * sizeof (asymbol *)); | |
1084 | ||
1085 | while (symcount-- > 0) | |
1086 | { | |
1087 | *vec++ = sym++; | |
1088 | } | |
1089 | ||
1090 | return (true); | |
1091 | } | |
1092 | ||
1093 | /* Return the number of bytes required to hold the symtab vector. | |
1094 | ||
1095 | Note that we base it on the count plus 1, since we will null terminate | |
1096 | the vector allocated based on this size. */ | |
1097 | ||
9ce0058c | 1098 | static unsigned int |
8c4a1ace | 1099 | DEFUN (elf_get_symtab_upper_bound, (abfd), bfd *abfd) |
9ce0058c | 1100 | { |
8c4a1ace JG |
1101 | unsigned int symtab_size = 0; |
1102 | ||
1103 | if (elf_slurp_symbol_table (abfd)) | |
1104 | { | |
1105 | symtab_size = (bfd_get_symcount (abfd) + 1) * (sizeof (asymbol)); | |
1106 | } | |
1107 | return (symtab_size); | |
9ce0058c SC |
1108 | } |
1109 | ||
1110 | static unsigned int | |
1111 | elf_get_reloc_upper_bound (abfd, asect) | |
1112 | bfd *abfd; | |
1113 | sec_ptr asect; | |
1114 | { | |
1115 | fprintf (stderr, "elf_get_reloc_upper_bound unimplemented\n"); | |
1116 | fflush (stderr); | |
1117 | abort (); | |
1118 | return (0); | |
1119 | } | |
1120 | ||
1121 | static unsigned int | |
1122 | elf_canonicalize_reloc (abfd, section, relptr, symbols) | |
1123 | bfd *abfd; | |
1124 | sec_ptr section; | |
1125 | arelent **relptr; | |
1126 | asymbol **symbols; | |
1127 | { | |
1128 | fprintf (stderr, "elf_canonicalize_reloc unimplemented\n"); | |
1129 | fflush (stderr); | |
1130 | abort (); | |
1131 | return (0); | |
1132 | } | |
1133 | ||
1134 | static unsigned int | |
8c4a1ace JG |
1135 | DEFUN (elf_get_symtab, (abfd, alocation), |
1136 | bfd *abfd AND | |
1137 | asymbol **alocation) | |
9ce0058c | 1138 | { |
8c4a1ace JG |
1139 | unsigned int symcount; |
1140 | asymbol **vec; | |
1141 | ||
1142 | if (!elf_slurp_symbol_table (abfd)) | |
1143 | { | |
1144 | return (0); | |
1145 | } | |
1146 | else | |
1147 | { | |
1148 | symcount = bfd_get_symcount (abfd); | |
1149 | vec = bfd_get_outsymbols (abfd); | |
1150 | while (symcount-- > 0) | |
1151 | { | |
1152 | *alocation++ = *vec++; | |
1153 | } | |
1154 | *alocation++ = NULL; | |
1155 | return (bfd_get_symcount (abfd)); | |
1156 | } | |
9ce0058c SC |
1157 | } |
1158 | ||
1159 | static asymbol * | |
d01cd8fc FF |
1160 | DEFUN (elf_make_empty_symbol, (abfd), |
1161 | bfd *abfd) | |
9ce0058c | 1162 | { |
d01cd8fc FF |
1163 | elf_symbol_type *new; |
1164 | ||
1165 | new = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type)); | |
1166 | if (new == NULL) | |
1167 | { | |
1168 | bfd_error = no_memory; | |
1169 | return (NULL); | |
1170 | } | |
1171 | else | |
1172 | { | |
1173 | new -> symbol.the_bfd = abfd; | |
1174 | return (&new -> symbol); | |
1175 | } | |
9ce0058c SC |
1176 | } |
1177 | ||
1178 | static void | |
1179 | DEFUN (elf_print_symbol,(ignore_abfd, filep, symbol, how), | |
1180 | bfd *ignore_abfd AND | |
1181 | PTR filep AND | |
1182 | asymbol *symbol AND | |
e0796d22 | 1183 | bfd_print_symbol_type how) |
9ce0058c SC |
1184 | { |
1185 | fprintf (stderr, "elf_print_symbol unimplemented\n"); | |
1186 | fflush (stderr); | |
1187 | abort (); | |
1188 | } | |
1189 | ||
1190 | static alent * | |
1191 | DEFUN (elf_get_lineno,(ignore_abfd, symbol), | |
1192 | bfd *ignore_abfd AND | |
1193 | asymbol *symbol) | |
1194 | { | |
1195 | fprintf (stderr, "elf_get_lineno unimplemented\n"); | |
1196 | fflush (stderr); | |
1197 | abort (); | |
1198 | return (NULL); | |
1199 | } | |
1200 | ||
1201 | static boolean | |
1202 | DEFUN (elf_set_arch_mach,(abfd, arch, machine), | |
1203 | bfd *abfd AND | |
1204 | enum bfd_architecture arch AND | |
1205 | unsigned long machine) | |
1206 | { | |
1207 | fprintf (stderr, "elf_set_arch_mach unimplemented\n"); | |
1208 | fflush (stderr); | |
1209 | /* Allow any architecture to be supported by the elf backend */ | |
1210 | return bfd_default_set_arch_mach(abfd, arch, machine); | |
1211 | } | |
1212 | ||
1213 | static boolean | |
1214 | DEFUN (elf_find_nearest_line,(abfd, | |
1215 | section, | |
1216 | symbols, | |
1217 | offset, | |
1218 | filename_ptr, | |
1219 | functionname_ptr, | |
1220 | line_ptr), | |
1221 | bfd *abfd AND | |
1222 | asection *section AND | |
1223 | asymbol **symbols AND | |
1224 | bfd_vma offset AND | |
1225 | CONST char **filename_ptr AND | |
1226 | CONST char **functionname_ptr AND | |
1227 | unsigned int *line_ptr) | |
1228 | { | |
1229 | fprintf (stderr, "elf_find_nearest_line unimplemented\n"); | |
1230 | fflush (stderr); | |
1231 | abort (); | |
1232 | return (false); | |
1233 | } | |
1234 | ||
1235 | static int | |
1236 | DEFUN (elf_sizeof_headers, (abfd, reloc), | |
1237 | bfd *abfd AND | |
1238 | boolean reloc) | |
1239 | { | |
1240 | fprintf (stderr, "elf_sizeof_headers unimplemented\n"); | |
1241 | fflush (stderr); | |
1242 | abort (); | |
1243 | return (0); | |
1244 | } | |
e0796d22 | 1245 | \f |
9ce0058c SC |
1246 | /* This structure contains everything that BFD knows about a target. |
1247 | It includes things like its byte order, name, what routines to call | |
1248 | to do various operations, etc. Every BFD points to a target structure | |
1249 | with its "xvec" member. | |
1250 | ||
1251 | There are two such structures here: one for big-endian machines and | |
1252 | one for little-endian machines. */ | |
1253 | ||
e0796d22 FF |
1254 | /* Archives are generic or unimplemented. */ |
1255 | #define elf_slurp_armap bfd_false | |
1256 | #define elf_slurp_extended_name_table _bfd_slurp_extended_name_table | |
1257 | #define elf_truncate_arname bfd_dont_truncate_arname | |
1258 | #define elf_openr_next_archived_file bfd_generic_openr_next_archived_file | |
1259 | #define elf_generic_stat_arch_elt bfd_generic_stat_arch_elt | |
1260 | #define elf_write_armap (PROTO (boolean, (*), \ | |
a6c1d731 | 1261 | (bfd *arch, unsigned int elength, struct orl *map, unsigned int orl_count, \ |
e0796d22 FF |
1262 | int stridx))) bfd_false |
1263 | ||
1264 | /* Ordinary section reading and writing */ | |
1265 | #define elf_new_section_hook _bfd_dummy_new_section_hook | |
1266 | #define elf_get_section_contents bfd_generic_get_section_contents | |
1267 | #define elf_set_section_contents bfd_generic_set_section_contents | |
1268 | #define elf_close_and_cleanup bfd_generic_close_and_cleanup | |
1269 | ||
1270 | #define elf_bfd_debug_info_start bfd_void | |
1271 | #define elf_bfd_debug_info_end bfd_void | |
1272 | #define elf_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void | |
e98e6ec1 SC |
1273 | #define elf_bfd_get_relocated_section_contents \ |
1274 | bfd_generic_get_relocated_section_contents | |
d01cd8fc | 1275 | #define elf_bfd_relax_section bfd_generic_relax_section |
9ce0058c SC |
1276 | bfd_target elf_big_vec = |
1277 | { | |
1278 | /* name: identify kind of target */ | |
1279 | "elf-big", | |
1280 | ||
1281 | /* flavour: general indication about file */ | |
e0796d22 | 1282 | bfd_target_elf_flavour, |
9ce0058c SC |
1283 | |
1284 | /* byteorder_big_p: data is big endian */ | |
1285 | true, | |
1286 | ||
1287 | /* header_byteorder_big_p: header is also big endian */ | |
1288 | true, | |
1289 | ||
1290 | /* object_flags: mask of all file flags */ | |
1291 | (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS | | |
1292 | DYNAMIC | WP_TEXT), | |
1293 | ||
1294 | /* section_flags: mask of all section flags */ | |
1295 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY | | |
1296 | SEC_DATA), | |
1297 | ||
1298 | /* ar_pad_char: pad character for filenames within an archive header | |
1299 | FIXME: this really has nothing to do with ELF, this is a characteristic | |
1300 | of the archiver and/or os and should be independently tunable */ | |
1301 | '/', | |
1302 | ||
1303 | /* ar_max_namelen: maximum number of characters in an archive header | |
1304 | FIXME: this really has nothing to do with ELF, this is a characteristic | |
1305 | of the archiver and should be independently tunable. This value is | |
1306 | a WAG (wild a** guess) */ | |
1307 | 15, | |
1308 | ||
1309 | /* align_power_min: minimum alignment restriction for any section | |
1310 | FIXME: this value may be target machine dependent */ | |
1311 | 3, | |
1312 | ||
1313 | /* Routines to byte-swap various sized integers from the data sections */ | |
1314 | _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, | |
1315 | ||
1316 | /* Routines to byte-swap various sized integers from the file headers */ | |
1317 | _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, | |
1318 | ||
1319 | /* bfd_check_format: check the format of a file being read */ | |
e0796d22 FF |
1320 | { _bfd_dummy_target, /* unknown format */ |
1321 | elf_object_p, /* assembler/linker output (object file) */ | |
1322 | bfd_generic_archive_p, /* an archive */ | |
1323 | elf_core_file_p /* a core file */ | |
9ce0058c SC |
1324 | }, |
1325 | ||
1326 | /* bfd_set_format: set the format of a file being written */ | |
1327 | { bfd_false, | |
1328 | elf_mkobject, | |
1329 | _bfd_generic_mkarchive, | |
1330 | bfd_false | |
1331 | }, | |
1332 | ||
1333 | /* bfd_write_contents: write cached information into a file being written */ | |
1334 | { bfd_false, | |
1335 | elf_write_object_contents, | |
1336 | _bfd_write_archive_contents, | |
1337 | bfd_false | |
1338 | }, | |
1339 | ||
1340 | /* Initialize a jump table with the standard macro. All names start | |
1341 | with "elf" */ | |
1342 | JUMP_TABLE(elf), | |
1343 | ||
1344 | /* SWAP_TABLE */ | |
1345 | NULL, NULL, NULL | |
1346 | }; | |
1347 | ||
1348 | bfd_target elf_little_vec = | |
1349 | { | |
1350 | /* name: identify kind of target */ | |
1351 | "elf-little", | |
1352 | ||
1353 | /* flavour: general indication about file */ | |
e0796d22 | 1354 | bfd_target_elf_flavour, |
9ce0058c SC |
1355 | |
1356 | /* byteorder_big_p: data is big endian */ | |
1357 | false, /* Nope -- this one's little endian */ | |
1358 | ||
1359 | /* header_byteorder_big_p: header is also big endian */ | |
1360 | false, /* Nope -- this one's little endian */ | |
1361 | ||
1362 | /* object_flags: mask of all file flags */ | |
1363 | (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS | | |
1364 | DYNAMIC | WP_TEXT), | |
1365 | ||
1366 | /* section_flags: mask of all section flags */ | |
1367 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY | | |
1368 | SEC_DATA), | |
1369 | ||
1370 | /* ar_pad_char: pad character for filenames within an archive header | |
1371 | FIXME: this really has nothing to do with ELF, this is a characteristic | |
1372 | of the archiver and/or os and should be independently tunable */ | |
1373 | '/', | |
1374 | ||
1375 | /* ar_max_namelen: maximum number of characters in an archive header | |
1376 | FIXME: this really has nothing to do with ELF, this is a characteristic | |
1377 | of the archiver and should be independently tunable. This value is | |
1378 | a WAG (wild a** guess) */ | |
1379 | 15, | |
1380 | ||
1381 | /* align_power_min: minimum alignment restriction for any section | |
1382 | FIXME: this value may be target machine dependent */ | |
1383 | 3, | |
1384 | ||
1385 | /* Routines to byte-swap various sized integers from the data sections */ | |
1386 | _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, | |
1387 | ||
1388 | /* Routines to byte-swap various sized integers from the file headers */ | |
1389 | _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, | |
1390 | ||
1391 | /* bfd_check_format: check the format of a file being read */ | |
e0796d22 FF |
1392 | { _bfd_dummy_target, /* unknown format */ |
1393 | elf_object_p, /* assembler/linker output (object file) */ | |
1394 | bfd_generic_archive_p, /* an archive */ | |
1395 | elf_core_file_p /* a core file */ | |
9ce0058c SC |
1396 | }, |
1397 | ||
1398 | /* bfd_set_format: set the format of a file being written */ | |
1399 | { bfd_false, | |
1400 | elf_mkobject, | |
1401 | _bfd_generic_mkarchive, | |
1402 | bfd_false | |
1403 | }, | |
1404 | ||
1405 | /* bfd_write_contents: write cached information into a file being written */ | |
1406 | { bfd_false, | |
1407 | elf_write_object_contents, | |
1408 | _bfd_write_archive_contents, | |
1409 | bfd_false | |
1410 | }, | |
1411 | ||
1412 | /* Initialize a jump table with the standard macro. All names start | |
1413 | with "elf" */ | |
1414 | JUMP_TABLE(elf), | |
1415 | ||
1416 | /* SWAP_TABLE */ | |
1417 | NULL, NULL, NULL | |
1418 | }; |