* opncls.c (bfd_alloc_by_size_t): Set bfd_error_no_memory if
[binutils-gdb.git] / bfd / elfcore.h
1 /* ELF core file support for BFD.
2 Copyright 1995 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Core file support */
20
21 #ifdef HAVE_SYS_PROCFS_H /* Some core file support requires host /proc files */
22 #include <sys/procfs.h>
23 #else
24 #define bfd_prstatus(abfd, descdata, descsz, filepos) true
25 #define bfd_fpregset(abfd, descdata, descsz, filepos) true
26 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) true
27 #endif
28
29 #ifdef HAVE_SYS_PROCFS_H
30
31 static boolean
32 bfd_prstatus (abfd, descdata, descsz, filepos)
33 bfd *abfd;
34 char *descdata;
35 int descsz;
36 long filepos;
37 {
38 asection *newsect;
39 prstatus_t *status = (prstatus_t *) 0;
40
41 if (descsz == sizeof (prstatus_t))
42 {
43 newsect = bfd_make_section (abfd, ".reg");
44 if (newsect == NULL)
45 return false;
46 newsect->_raw_size = sizeof (status->pr_reg);
47 newsect->filepos = filepos + (long) &status->pr_reg;
48 newsect->flags = SEC_HAS_CONTENTS;
49 newsect->alignment_power = 2;
50 if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
51 {
52 memcpy (core_prstatus (abfd), descdata, descsz);
53 }
54 }
55 return true;
56 }
57
58 /* Stash a copy of the prpsinfo structure away for future use. */
59
60 static boolean
61 bfd_prpsinfo (abfd, descdata, descsz, filepos)
62 bfd *abfd;
63 char *descdata;
64 int descsz;
65 long filepos;
66 {
67 if (descsz == sizeof (prpsinfo_t))
68 {
69 if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) == NULL)
70 return false;
71 memcpy (core_prpsinfo (abfd), descdata, descsz);
72 }
73 return true;
74 }
75
76 static boolean
77 bfd_fpregset (abfd, descdata, descsz, filepos)
78 bfd *abfd;
79 char *descdata;
80 int descsz;
81 long filepos;
82 {
83 asection *newsect;
84
85 newsect = bfd_make_section (abfd, ".reg2");
86 if (newsect == NULL)
87 return false;
88 newsect->_raw_size = descsz;
89 newsect->filepos = filepos;
90 newsect->flags = SEC_HAS_CONTENTS;
91 newsect->alignment_power = 2;
92 return true;
93 }
94
95 #endif /* HAVE_SYS_PROCFS_H */
96
97 /* Return a pointer to the args (including the command name) that were
98 seen by the program that generated the core dump. Note that for
99 some reason, a spurious space is tacked onto the end of the args
100 in some (at least one anyway) implementations, so strip it off if
101 it exists. */
102
103 char *
104 elf_core_file_failing_command (abfd)
105 bfd *abfd;
106 {
107 #ifdef HAVE_SYS_PROCFS_H
108 if (core_prpsinfo (abfd))
109 {
110 prpsinfo_t *p = core_prpsinfo (abfd);
111 char *scan = p->pr_psargs;
112 while (*scan++)
113 {;
114 }
115 scan -= 2;
116 if ((scan > p->pr_psargs) && (*scan == ' '))
117 {
118 *scan = '\000';
119 }
120 return p->pr_psargs;
121 }
122 #endif
123 return NULL;
124 }
125
126 /* Return the number of the signal that caused the core dump. Presumably,
127 since we have a core file, we got a signal of some kind, so don't bother
128 checking the other process status fields, just return the signal number.
129 */
130
131 int
132 elf_core_file_failing_signal (abfd)
133 bfd *abfd;
134 {
135 #ifdef HAVE_SYS_PROCFS_H
136 if (core_prstatus (abfd))
137 {
138 return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig;
139 }
140 #endif
141 return -1;
142 }
143
144 /* Check to see if the core file could reasonably be expected to have
145 come for the current executable file. Note that by default we return
146 true unless we find something that indicates that there might be a
147 problem.
148 */
149
150 boolean
151 elf_core_file_matches_executable_p (core_bfd, exec_bfd)
152 bfd *core_bfd;
153 bfd *exec_bfd;
154 {
155 #ifdef HAVE_SYS_PROCFS_H
156 char *corename;
157 char *execname;
158 #endif
159
160 /* First, xvecs must match since both are ELF files for the same target. */
161
162 if (core_bfd->xvec != exec_bfd->xvec)
163 {
164 bfd_set_error (bfd_error_system_call);
165 return false;
166 }
167
168 #ifdef HAVE_SYS_PROCFS_H
169
170 /* If no prpsinfo, just return true. Otherwise, grab the last component
171 of the exec'd pathname from the prpsinfo. */
172
173 if (core_prpsinfo (core_bfd))
174 {
175 corename = (((prpsinfo_t *) core_prpsinfo (core_bfd))->pr_fname);
176 }
177 else
178 {
179 return true;
180 }
181
182 /* Find the last component of the executable pathname. */
183
184 if ((execname = strrchr (exec_bfd->filename, '/')) != NULL)
185 {
186 execname++;
187 }
188 else
189 {
190 execname = (char *) exec_bfd->filename;
191 }
192
193 /* See if they match */
194
195 return strcmp (execname, corename) ? false : true;
196
197 #else
198
199 return true;
200
201 #endif /* HAVE_SYS_PROCFS_H */
202 }
203
204 /* ELF core files contain a segment of type PT_NOTE, that holds much of
205 the information that would normally be available from the /proc interface
206 for the process, at the time the process dumped core. Currently this
207 includes copies of the prstatus, prpsinfo, and fpregset structures.
208
209 Since these structures are potentially machine dependent in size and
210 ordering, bfd provides two levels of support for them. The first level,
211 available on all machines since it does not require that the host
212 have /proc support or the relevant include files, is to create a bfd
213 section for each of the prstatus, prpsinfo, and fpregset structures,
214 without any interpretation of their contents. With just this support,
215 the bfd client will have to interpret the structures itself. Even with
216 /proc support, it might want these full structures for it's own reasons.
217
218 In the second level of support, where HAVE_SYS_PROCFS_H is defined,
219 bfd will pick apart the structures to gather some additional
220 information that clients may want, such as the general register
221 set, the name of the exec'ed file and its arguments, the signal (if
222 any) that caused the core dump, etc.
223
224 */
225
226 static boolean
227 elf_corefile_note (abfd, hdr)
228 bfd *abfd;
229 Elf_Internal_Phdr *hdr;
230 {
231 Elf_External_Note *x_note_p; /* Elf note, external form */
232 Elf_Internal_Note i_note; /* Elf note, internal form */
233 char *buf = NULL; /* Entire note segment contents */
234 char *namedata; /* Name portion of the note */
235 char *descdata; /* Descriptor portion of the note */
236 char *sectname; /* Name to use for new section */
237 long filepos; /* File offset to descriptor data */
238 asection *newsect;
239
240 if (hdr->p_filesz > 0
241 && (buf = (char *) malloc ((size_t) hdr->p_filesz)) != NULL
242 && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1
243 && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz)
244 {
245 x_note_p = (Elf_External_Note *) buf;
246 while ((char *) x_note_p < (buf + hdr->p_filesz))
247 {
248 i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz);
249 i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz);
250 i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type);
251 namedata = x_note_p->name;
252 descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
253 filepos = hdr->p_offset + (descdata - buf);
254 switch (i_note.type)
255 {
256 case NT_PRSTATUS:
257 /* process descdata as prstatus info */
258 if (! bfd_prstatus (abfd, descdata, i_note.descsz, filepos))
259 return false;
260 sectname = ".prstatus";
261 break;
262 case NT_FPREGSET:
263 /* process descdata as fpregset info */
264 if (! bfd_fpregset (abfd, descdata, i_note.descsz, filepos))
265 return false;
266 sectname = ".fpregset";
267 break;
268 case NT_PRPSINFO:
269 /* process descdata as prpsinfo */
270 if (! bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos))
271 return false;
272 sectname = ".prpsinfo";
273 break;
274 default:
275 /* Unknown descriptor, just ignore it. */
276 sectname = NULL;
277 break;
278 }
279 if (sectname != NULL)
280 {
281 newsect = bfd_make_section (abfd, sectname);
282 if (newsect == NULL)
283 return false;
284 newsect->_raw_size = i_note.descsz;
285 newsect->filepos = filepos;
286 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
287 newsect->alignment_power = 2;
288 }
289 x_note_p = (Elf_External_Note *)
290 (descdata + BFD_ALIGN (i_note.descsz, 4));
291 }
292 }
293 if (buf != NULL)
294 {
295 free (buf);
296 }
297 else if (hdr->p_filesz > 0)
298 {
299 bfd_set_error (bfd_error_no_memory);
300 return false;
301 }
302 return true;
303
304 }
305
306 /* Core files are simply standard ELF formatted files that partition
307 the file using the execution view of the file (program header table)
308 rather than the linking view. In fact, there is no section header
309 table in a core file.
310
311 The process status information (including the contents of the general
312 register set) and the floating point register set are stored in a
313 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
314 that allow standard bfd access to the general registers (.reg) and the
315 floating point registers (.reg2).
316
317 */
318
319 const bfd_target *
320 elf_core_file_p (abfd)
321 bfd *abfd;
322 {
323 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
324 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
325 Elf_External_Phdr x_phdr; /* Program header table entry, external form */
326 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
327 unsigned int phindex;
328 struct elf_backend_data *ebd;
329
330 /* Read in the ELF header in external format. */
331
332 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
333 {
334 if (bfd_get_error () != bfd_error_system_call)
335 bfd_set_error (bfd_error_wrong_format);
336 return NULL;
337 }
338
339 /* Now check to see if we have a valid ELF file, and one that BFD can
340 make use of. The magic number must match, the address size ('class')
341 and byte-swapping must match our XVEC entry, and it must have a
342 program header table (FIXME: See comments re segments at top of this
343 file). */
344
345 if (elf_file_p (&x_ehdr) == false)
346 {
347 wrong:
348 bfd_set_error (bfd_error_wrong_format);
349 return NULL;
350 }
351
352 /* FIXME, Check EI_VERSION here ! */
353
354 {
355 #if ARCH_SIZE == 32
356 int desired_address_size = ELFCLASS32;
357 #endif
358 #if ARCH_SIZE == 64
359 int desired_address_size = ELFCLASS64;
360 #endif
361
362 if (x_ehdr.e_ident[EI_CLASS] != desired_address_size)
363 goto wrong;
364 }
365
366 /* Switch xvec to match the specified byte order. */
367 switch (x_ehdr.e_ident[EI_DATA])
368 {
369 case ELFDATA2MSB: /* Big-endian */
370 if (abfd->xvec->byteorder_big_p == false)
371 goto wrong;
372 break;
373 case ELFDATA2LSB: /* Little-endian */
374 if (abfd->xvec->byteorder_big_p == true)
375 goto wrong;
376 break;
377 case ELFDATANONE: /* No data encoding specified */
378 default: /* Unknown data encoding specified */
379 goto wrong;
380 }
381
382 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
383 the tdata pointer in the bfd. */
384
385 elf_tdata (abfd) =
386 (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
387 if (elf_tdata (abfd) == NULL)
388 return NULL;
389
390 /* FIXME, `wrong' returns from this point onward, leak memory. */
391
392 /* Now that we know the byte order, swap in the rest of the header */
393 i_ehdrp = elf_elfheader (abfd);
394 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
395 #if DEBUG & 1
396 elf_debug_file (i_ehdrp);
397 #endif
398
399 ebd = get_elf_backend_data (abfd);
400
401 /* Check that the ELF e_machine field matches what this particular
402 BFD format expects. */
403 if (ebd->elf_machine_code != i_ehdrp->e_machine
404 && (ebd->elf_machine_alt1 == 0 || i_ehdrp->e_machine != ebd->elf_machine_alt1)
405 && (ebd->elf_machine_alt2 == 0 || i_ehdrp->e_machine != ebd->elf_machine_alt2))
406 {
407 const bfd_target * const *target_ptr;
408
409 if (ebd->elf_machine_code != EM_NONE)
410 goto wrong;
411
412 /* This is the generic ELF target. Let it match any ELF target
413 for which we do not have a specific backend. */
414 for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
415 {
416 struct elf_backend_data *back;
417
418 if ((*target_ptr)->flavour != bfd_target_elf_flavour)
419 continue;
420 back = (struct elf_backend_data *) (*target_ptr)->backend_data;
421 if (back->elf_machine_code == i_ehdrp->e_machine)
422 {
423 /* target_ptr is an ELF backend which matches this
424 object file, so reject the generic ELF target. */
425 goto wrong;
426 }
427 }
428 }
429
430 /* If there is no program header, or the type is not a core file, then
431 we are hosed. */
432 if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
433 goto wrong;
434
435 /* Allocate space for a copy of the program header table in
436 internal form, seek to the program header table in the file,
437 read it in, and convert it to internal form. As a simple sanity
438 check, verify that the what BFD thinks is the size of each program
439 header table entry actually matches the size recorded in the file. */
440
441 if (i_ehdrp->e_phentsize != sizeof (x_phdr))
442 goto wrong;
443 i_phdrp = (Elf_Internal_Phdr *)
444 bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
445 if (!i_phdrp)
446 return NULL;
447 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
448 return NULL;
449 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
450 {
451 if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
452 != sizeof (x_phdr))
453 return NULL;
454 elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
455 }
456
457 /* Once all of the program headers have been read and converted, we
458 can start processing them. */
459
460 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
461 {
462 bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
463 if ((i_phdrp + phindex)->p_type == PT_NOTE)
464 {
465 if (! elf_corefile_note (abfd, i_phdrp + phindex))
466 return NULL;
467 }
468 }
469
470 /* Remember the entry point specified in the ELF file header. */
471
472 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
473
474 return abfd->xvec;
475 }