1 /* Core file support */
3 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
4 #include <sys/procfs.h>
6 #define bfd_prstatus(abfd, descdata, descsz, filepos) true
7 #define bfd_fpregset(abfd, descdata, descsz, filepos) true
8 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) true
14 bfd_prstatus (abfd, descdata, descsz, filepos)
21 prstatus_t *status = (prstatus_t *) 0;
23 if (descsz == sizeof (prstatus_t))
25 newsect = bfd_make_section (abfd, ".reg");
28 newsect->_raw_size = sizeof (status->pr_reg);
29 newsect->filepos = filepos + (long) &status->pr_reg;
30 newsect->flags = SEC_HAS_CONTENTS;
31 newsect->alignment_power = 2;
32 if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
34 memcpy (core_prstatus (abfd), descdata, descsz);
40 /* Stash a copy of the prpsinfo structure away for future use. */
43 bfd_prpsinfo (abfd, descdata, descsz, filepos)
49 if (descsz == sizeof (prpsinfo_t))
51 if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) == NULL)
53 bfd_set_error (bfd_error_no_memory);
56 memcpy (core_prpsinfo (abfd), descdata, descsz);
62 bfd_fpregset (abfd, descdata, descsz, filepos)
70 newsect = bfd_make_section (abfd, ".reg2");
73 newsect->_raw_size = descsz;
74 newsect->filepos = filepos;
75 newsect->flags = SEC_HAS_CONTENTS;
76 newsect->alignment_power = 2;
80 #endif /* HAVE_PROCFS */
82 /* Return a pointer to the args (including the command name) that were
83 seen by the program that generated the core dump. Note that for
84 some reason, a spurious space is tacked onto the end of the args
85 in some (at least one anyway) implementations, so strip it off if
89 elf_core_file_failing_command (abfd)
93 if (core_prpsinfo (abfd))
95 prpsinfo_t *p = core_prpsinfo (abfd);
96 char *scan = p->pr_psargs;
101 if ((scan > p->pr_psargs) && (*scan == ' '))
111 /* Return the number of the signal that caused the core dump. Presumably,
112 since we have a core file, we got a signal of some kind, so don't bother
113 checking the other process status fields, just return the signal number.
117 elf_core_file_failing_signal (abfd)
121 if (core_prstatus (abfd))
123 return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig;
129 /* Check to see if the core file could reasonably be expected to have
130 come for the current executable file. Note that by default we return
131 true unless we find something that indicates that there might be a
136 elf_core_file_matches_executable_p (core_bfd, exec_bfd)
145 /* First, xvecs must match since both are ELF files for the same target. */
147 if (core_bfd->xvec != exec_bfd->xvec)
149 bfd_set_error (bfd_error_system_call);
155 /* If no prpsinfo, just return true. Otherwise, grab the last component
156 of the exec'd pathname from the prpsinfo. */
158 if (core_prpsinfo (core_bfd))
160 corename = (((prpsinfo_t *) core_prpsinfo (core_bfd))->pr_fname);
167 /* Find the last component of the executable pathname. */
169 if ((execname = strrchr (exec_bfd->filename, '/')) != NULL)
175 execname = (char *) exec_bfd->filename;
178 /* See if they match */
180 return strcmp (execname, corename) ? false : true;
186 #endif /* HAVE_PROCFS */
189 /* ELF core files contain a segment of type PT_NOTE, that holds much of
190 the information that would normally be available from the /proc interface
191 for the process, at the time the process dumped core. Currently this
192 includes copies of the prstatus, prpsinfo, and fpregset structures.
194 Since these structures are potentially machine dependent in size and
195 ordering, bfd provides two levels of support for them. The first level,
196 available on all machines since it does not require that the host
197 have /proc support or the relevant include files, is to create a bfd
198 section for each of the prstatus, prpsinfo, and fpregset structures,
199 without any interpretation of their contents. With just this support,
200 the bfd client will have to interpret the structures itself. Even with
201 /proc support, it might want these full structures for it's own reasons.
203 In the second level of support, where HAVE_PROCFS is defined, bfd will
204 pick apart the structures to gather some additional information that
205 clients may want, such as the general register set, the name of the
206 exec'ed file and its arguments, the signal (if any) that caused the
212 elf_corefile_note (abfd, hdr)
214 Elf_Internal_Phdr *hdr;
216 Elf_External_Note *x_note_p; /* Elf note, external form */
217 Elf_Internal_Note i_note; /* Elf note, internal form */
218 char *buf = NULL; /* Entire note segment contents */
219 char *namedata; /* Name portion of the note */
220 char *descdata; /* Descriptor portion of the note */
221 char *sectname; /* Name to use for new section */
222 long filepos; /* File offset to descriptor data */
225 if (hdr->p_filesz > 0
226 && (buf = (char *) malloc (hdr->p_filesz)) != NULL
227 && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1
228 && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz)
230 x_note_p = (Elf_External_Note *) buf;
231 while ((char *) x_note_p < (buf + hdr->p_filesz))
233 i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz);
234 i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz);
235 i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type);
236 namedata = x_note_p->name;
237 descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
238 filepos = hdr->p_offset + (descdata - buf);
242 /* process descdata as prstatus info */
243 if (! bfd_prstatus (abfd, descdata, i_note.descsz, filepos))
245 sectname = ".prstatus";
248 /* process descdata as fpregset info */
249 if (! bfd_fpregset (abfd, descdata, i_note.descsz, filepos))
251 sectname = ".fpregset";
254 /* process descdata as prpsinfo */
255 if (! bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos))
257 sectname = ".prpsinfo";
260 /* Unknown descriptor, just ignore it. */
264 if (sectname != NULL)
266 newsect = bfd_make_section (abfd, sectname);
269 newsect->_raw_size = i_note.descsz;
270 newsect->filepos = filepos;
271 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
272 newsect->alignment_power = 2;
274 x_note_p = (Elf_External_Note *)
275 (descdata + BFD_ALIGN (i_note.descsz, 4));
282 else if (hdr->p_filesz > 0)
284 bfd_set_error (bfd_error_no_memory);
291 /* Core files are simply standard ELF formatted files that partition
292 the file using the execution view of the file (program header table)
293 rather than the linking view. In fact, there is no section header
294 table in a core file.
296 The process status information (including the contents of the general
297 register set) and the floating point register set are stored in a
298 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
299 that allow standard bfd access to the general registers (.reg) and the
300 floating point registers (.reg2).
305 elf_core_file_p (abfd)
308 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
309 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
310 Elf_External_Phdr x_phdr; /* Program header table entry, external form */
311 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
312 unsigned int phindex;
313 struct elf_backend_data *ebd;
315 /* Read in the ELF header in external format. */
317 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
319 if (bfd_get_error () != bfd_error_system_call)
320 bfd_set_error (bfd_error_wrong_format);
324 /* Now check to see if we have a valid ELF file, and one that BFD can
325 make use of. The magic number must match, the address size ('class')
326 and byte-swapping must match our XVEC entry, and it must have a
327 program header table (FIXME: See comments re segments at top of this
330 if (elf_file_p (&x_ehdr) == false)
333 bfd_set_error (bfd_error_wrong_format);
337 /* FIXME, Check EI_VERSION here ! */
341 int desired_address_size = ELFCLASS32;
344 int desired_address_size = ELFCLASS64;
347 if (x_ehdr.e_ident[EI_CLASS] != desired_address_size)
351 /* Switch xvec to match the specified byte order. */
352 switch (x_ehdr.e_ident[EI_DATA])
354 case ELFDATA2MSB: /* Big-endian */
355 if (abfd->xvec->byteorder_big_p == false)
358 case ELFDATA2LSB: /* Little-endian */
359 if (abfd->xvec->byteorder_big_p == true)
362 case ELFDATANONE: /* No data encoding specified */
363 default: /* Unknown data encoding specified */
367 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
368 the tdata pointer in the bfd. */
371 (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
372 if (elf_tdata (abfd) == NULL)
374 bfd_set_error (bfd_error_no_memory);
378 /* FIXME, `wrong' returns from this point onward, leak memory. */
380 /* Now that we know the byte order, swap in the rest of the header */
381 i_ehdrp = elf_elfheader (abfd);
382 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
384 elf_debug_file (i_ehdrp);
387 ebd = get_elf_backend_data (abfd);
389 /* Check that the ELF e_machine field matches what this particular
390 BFD format expects. */
391 if (ebd->elf_machine_code != i_ehdrp->e_machine
392 && (ebd->elf_machine_alt1 == 0 || i_ehdrp->e_machine != ebd->elf_machine_alt1)
393 && (ebd->elf_machine_alt2 == 0 || i_ehdrp->e_machine != ebd->elf_machine_alt2))
395 const bfd_target * const *target_ptr;
397 if (ebd->elf_machine_code != EM_NONE)
400 /* This is the generic ELF target. Let it match any ELF target
401 for which we do not have a specific backend. */
402 for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
404 struct elf_backend_data *back;
406 if ((*target_ptr)->flavour != bfd_target_elf_flavour)
408 back = (struct elf_backend_data *) (*target_ptr)->backend_data;
409 if (back->elf_machine_code == i_ehdrp->e_machine)
411 /* target_ptr is an ELF backend which matches this
412 object file, so reject the generic ELF target. */
418 /* If there is no program header, or the type is not a core file, then
420 if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
423 /* Allocate space for a copy of the program header table in
424 internal form, seek to the program header table in the file,
425 read it in, and convert it to internal form. As a simple sanity
426 check, verify that the what BFD thinks is the size of each program
427 header table entry actually matches the size recorded in the file. */
429 if (i_ehdrp->e_phentsize != sizeof (x_phdr))
431 i_phdrp = (Elf_Internal_Phdr *)
432 bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
435 bfd_set_error (bfd_error_no_memory);
438 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
440 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
442 if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
445 elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
448 /* Once all of the program headers have been read and converted, we
449 can start processing them. */
451 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
453 bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
454 if ((i_phdrp + phindex)->p_type == PT_NOTE)
456 if (! elf_corefile_note (abfd, i_phdrp + phindex))
461 /* Remember the entry point specified in the ELF file header. */
463 bfd_get_start_address (abfd) = i_ehdrp->e_entry;