1 /* ELF core file support for BFD.
2 Copyright 1995 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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 */
21 #ifdef HAVE_SYS_PROCFS_H /* Some core file support requires host /proc files */
23 #include <sys/procfs.h>
25 #define bfd_prstatus(abfd, descdata, descsz, filepos) true
26 #define bfd_fpregset(abfd, descdata, descsz, filepos) true
27 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) true
30 #ifdef HAVE_SYS_PROCFS_H
33 bfd_prstatus (abfd, descdata, descsz, filepos)
40 prstatus_t *status = (prstatus_t *) 0;
42 if (descsz == sizeof (prstatus_t))
44 newsect = bfd_make_section (abfd, ".reg");
47 newsect->_raw_size = sizeof (status->pr_reg);
48 newsect->filepos = filepos + (long) &status->pr_reg;
49 newsect->flags = SEC_HAS_CONTENTS;
50 newsect->alignment_power = 2;
51 if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
53 memcpy (core_prstatus (abfd), descdata, descsz);
59 /* Stash a copy of the prpsinfo structure away for future use. */
62 bfd_prpsinfo (abfd, descdata, descsz, filepos)
68 if (descsz == sizeof (prpsinfo_t))
70 if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) == NULL)
72 memcpy (core_prpsinfo (abfd), descdata, descsz);
78 bfd_fpregset (abfd, descdata, descsz, filepos)
86 newsect = bfd_make_section (abfd, ".reg2");
89 newsect->_raw_size = descsz;
90 newsect->filepos = filepos;
91 newsect->flags = SEC_HAS_CONTENTS;
92 newsect->alignment_power = 2;
96 #endif /* HAVE_SYS_PROCFS_H */
98 /* Return a pointer to the args (including the command name) that were
99 seen by the program that generated the core dump. Note that for
100 some reason, a spurious space is tacked onto the end of the args
101 in some (at least one anyway) implementations, so strip it off if
105 elf_core_file_failing_command (abfd)
108 #ifdef HAVE_SYS_PROCFS_H
109 if (core_prpsinfo (abfd))
111 prpsinfo_t *p = core_prpsinfo (abfd);
112 char *scan = p->pr_psargs;
117 if ((scan > p->pr_psargs) && (*scan == ' '))
127 /* Return the number of the signal that caused the core dump. Presumably,
128 since we have a core file, we got a signal of some kind, so don't bother
129 checking the other process status fields, just return the signal number.
133 elf_core_file_failing_signal (abfd)
136 #ifdef HAVE_SYS_PROCFS_H
137 if (core_prstatus (abfd))
139 return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig;
145 /* Check to see if the core file could reasonably be expected to have
146 come for the current executable file. Note that by default we return
147 true unless we find something that indicates that there might be a
152 elf_core_file_matches_executable_p (core_bfd, exec_bfd)
156 #ifdef HAVE_SYS_PROCFS_H
161 /* First, xvecs must match since both are ELF files for the same target. */
163 if (core_bfd->xvec != exec_bfd->xvec)
165 bfd_set_error (bfd_error_system_call);
169 #ifdef HAVE_SYS_PROCFS_H
171 /* If no prpsinfo, just return true. Otherwise, grab the last component
172 of the exec'd pathname from the prpsinfo. */
174 if (core_prpsinfo (core_bfd))
176 corename = (((prpsinfo_t *) core_prpsinfo (core_bfd))->pr_fname);
183 /* Find the last component of the executable pathname. */
185 if ((execname = strrchr (exec_bfd->filename, '/')) != NULL)
191 execname = (char *) exec_bfd->filename;
194 /* See if they match */
196 return strcmp (execname, corename) ? false : true;
202 #endif /* HAVE_SYS_PROCFS_H */
205 /* ELF core files contain a segment of type PT_NOTE, that holds much of
206 the information that would normally be available from the /proc interface
207 for the process, at the time the process dumped core. Currently this
208 includes copies of the prstatus, prpsinfo, and fpregset structures.
210 Since these structures are potentially machine dependent in size and
211 ordering, bfd provides two levels of support for them. The first level,
212 available on all machines since it does not require that the host
213 have /proc support or the relevant include files, is to create a bfd
214 section for each of the prstatus, prpsinfo, and fpregset structures,
215 without any interpretation of their contents. With just this support,
216 the bfd client will have to interpret the structures itself. Even with
217 /proc support, it might want these full structures for it's own reasons.
219 In the second level of support, where HAVE_SYS_PROCFS_H is defined,
220 bfd will pick apart the structures to gather some additional
221 information that clients may want, such as the general register
222 set, the name of the exec'ed file and its arguments, the signal (if
223 any) that caused the core dump, etc.
228 elf_corefile_note (abfd, hdr)
230 Elf_Internal_Phdr *hdr;
232 Elf_External_Note *x_note_p; /* Elf note, external form */
233 Elf_Internal_Note i_note; /* Elf note, internal form */
234 char *buf = NULL; /* Entire note segment contents */
235 char *namedata; /* Name portion of the note */
236 char *descdata; /* Descriptor portion of the note */
237 char *sectname; /* Name to use for new section */
238 long filepos; /* File offset to descriptor data */
241 if (hdr->p_filesz > 0
242 && (buf = (char *) bfd_malloc ((size_t) hdr->p_filesz)) != NULL
243 && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1
244 && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz)
246 x_note_p = (Elf_External_Note *) buf;
247 while ((char *) x_note_p < (buf + hdr->p_filesz))
249 i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz);
250 i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz);
251 i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type);
252 namedata = x_note_p->name;
253 descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
254 filepos = hdr->p_offset + (descdata - buf);
258 /* process descdata as prstatus info */
259 if (! bfd_prstatus (abfd, descdata, i_note.descsz, filepos))
261 sectname = ".prstatus";
264 /* process descdata as fpregset info */
265 if (! bfd_fpregset (abfd, descdata, i_note.descsz, filepos))
267 sectname = ".fpregset";
270 /* process descdata as prpsinfo */
271 if (! bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos))
273 sectname = ".prpsinfo";
276 /* Unknown descriptor, just ignore it. */
280 if (sectname != NULL)
282 newsect = bfd_make_section (abfd, sectname);
285 newsect->_raw_size = i_note.descsz;
286 newsect->filepos = filepos;
287 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
288 newsect->alignment_power = 2;
290 x_note_p = (Elf_External_Note *)
291 (descdata + BFD_ALIGN (i_note.descsz, 4));
298 else if (hdr->p_filesz > 0)
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.
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).
320 elf_core_file_p (abfd)
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;
330 /* Read in the ELF header in external format. */
332 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
334 if (bfd_get_error () != bfd_error_system_call)
335 bfd_set_error (bfd_error_wrong_format);
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
345 if (elf_file_p (&x_ehdr) == false)
348 bfd_set_error (bfd_error_wrong_format);
352 /* FIXME, Check EI_VERSION here ! */
356 int desired_address_size = ELFCLASS32;
359 int desired_address_size = ELFCLASS64;
362 if (x_ehdr.e_ident[EI_CLASS] != desired_address_size)
366 /* Switch xvec to match the specified byte order. */
367 switch (x_ehdr.e_ident[EI_DATA])
369 case ELFDATA2MSB: /* Big-endian */
370 if (! bfd_big_endian (abfd))
373 case ELFDATA2LSB: /* Little-endian */
374 if (! bfd_little_endian (abfd))
377 case ELFDATANONE: /* No data encoding specified */
378 default: /* Unknown data encoding specified */
382 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
383 the tdata pointer in the bfd. */
386 (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
387 if (elf_tdata (abfd) == NULL)
390 /* FIXME, `wrong' returns from this point onward, leak memory. */
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);
396 elf_debug_file (i_ehdrp);
399 ebd = get_elf_backend_data (abfd);
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))
407 const bfd_target * const *target_ptr;
409 if (ebd->elf_machine_code != EM_NONE)
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++)
416 struct elf_backend_data *back;
418 if ((*target_ptr)->flavour != bfd_target_elf_flavour)
420 back = (struct elf_backend_data *) (*target_ptr)->backend_data;
421 if (back->elf_machine_code == i_ehdrp->e_machine)
423 /* target_ptr is an ELF backend which matches this
424 object file, so reject the generic ELF target. */
430 /* If there is no program header, or the type is not a core file, then
432 if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
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. */
441 if (i_ehdrp->e_phentsize != sizeof (x_phdr))
443 i_phdrp = (Elf_Internal_Phdr *)
444 bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
447 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
449 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
451 if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
454 elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
457 /* Once all of the program headers have been read and converted, we
458 can start processing them. */
460 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
462 bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
463 if ((i_phdrp + phindex)->p_type == PT_NOTE)
465 if (! elf_corefile_note (abfd, i_phdrp + phindex))
470 /* Remember the entry point specified in the ELF file header. */
472 bfd_get_start_address (abfd) = i_ehdrp->e_entry;