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., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 /* Core file support */
21 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
22 #include <sys/procfs.h>
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
32 bfd_prstatus (abfd
, descdata
, descsz
, filepos
)
39 prstatus_t
*status
= (prstatus_t
*) 0;
41 if (descsz
== sizeof (prstatus_t
))
43 newsect
= bfd_make_section (abfd
, ".reg");
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
)
52 memcpy (core_prstatus (abfd
), descdata
, descsz
);
58 /* Stash a copy of the prpsinfo structure away for future use. */
61 bfd_prpsinfo (abfd
, descdata
, descsz
, filepos
)
67 if (descsz
== sizeof (prpsinfo_t
))
69 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) == NULL
)
71 bfd_set_error (bfd_error_no_memory
);
74 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
80 bfd_fpregset (abfd
, descdata
, descsz
, filepos
)
88 newsect
= bfd_make_section (abfd
, ".reg2");
91 newsect
->_raw_size
= descsz
;
92 newsect
->filepos
= filepos
;
93 newsect
->flags
= SEC_HAS_CONTENTS
;
94 newsect
->alignment_power
= 2;
98 #endif /* HAVE_PROCFS */
100 /* Return a pointer to the args (including the command name) that were
101 seen by the program that generated the core dump. Note that for
102 some reason, a spurious space is tacked onto the end of the args
103 in some (at least one anyway) implementations, so strip it off if
107 elf_core_file_failing_command (abfd
)
111 if (core_prpsinfo (abfd
))
113 prpsinfo_t
*p
= core_prpsinfo (abfd
);
114 char *scan
= p
->pr_psargs
;
119 if ((scan
> p
->pr_psargs
) && (*scan
== ' '))
129 /* Return the number of the signal that caused the core dump. Presumably,
130 since we have a core file, we got a signal of some kind, so don't bother
131 checking the other process status fields, just return the signal number.
135 elf_core_file_failing_signal (abfd
)
139 if (core_prstatus (abfd
))
141 return ((prstatus_t
*) (core_prstatus (abfd
)))->pr_cursig
;
147 /* Check to see if the core file could reasonably be expected to have
148 come for the current executable file. Note that by default we return
149 true unless we find something that indicates that there might be a
154 elf_core_file_matches_executable_p (core_bfd
, exec_bfd
)
163 /* First, xvecs must match since both are ELF files for the same target. */
165 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
167 bfd_set_error (bfd_error_system_call
);
173 /* If no prpsinfo, just return true. Otherwise, grab the last component
174 of the exec'd pathname from the prpsinfo. */
176 if (core_prpsinfo (core_bfd
))
178 corename
= (((prpsinfo_t
*) core_prpsinfo (core_bfd
))->pr_fname
);
185 /* Find the last component of the executable pathname. */
187 if ((execname
= strrchr (exec_bfd
->filename
, '/')) != NULL
)
193 execname
= (char *) exec_bfd
->filename
;
196 /* See if they match */
198 return strcmp (execname
, corename
) ? false : true;
204 #endif /* HAVE_PROCFS */
207 /* ELF core files contain a segment of type PT_NOTE, that holds much of
208 the information that would normally be available from the /proc interface
209 for the process, at the time the process dumped core. Currently this
210 includes copies of the prstatus, prpsinfo, and fpregset structures.
212 Since these structures are potentially machine dependent in size and
213 ordering, bfd provides two levels of support for them. The first level,
214 available on all machines since it does not require that the host
215 have /proc support or the relevant include files, is to create a bfd
216 section for each of the prstatus, prpsinfo, and fpregset structures,
217 without any interpretation of their contents. With just this support,
218 the bfd client will have to interpret the structures itself. Even with
219 /proc support, it might want these full structures for it's own reasons.
221 In the second level of support, where HAVE_PROCFS is defined, bfd will
222 pick apart the structures to gather some additional information that
223 clients may want, such as the general register set, the name of the
224 exec'ed file and its arguments, the signal (if any) that caused the
230 elf_corefile_note (abfd
, hdr
)
232 Elf_Internal_Phdr
*hdr
;
234 Elf_External_Note
*x_note_p
; /* Elf note, external form */
235 Elf_Internal_Note i_note
; /* Elf note, internal form */
236 char *buf
= NULL
; /* Entire note segment contents */
237 char *namedata
; /* Name portion of the note */
238 char *descdata
; /* Descriptor portion of the note */
239 char *sectname
; /* Name to use for new section */
240 long filepos
; /* File offset to descriptor data */
243 if (hdr
->p_filesz
> 0
244 && (buf
= (char *) malloc (hdr
->p_filesz
)) != NULL
245 && bfd_seek (abfd
, hdr
->p_offset
, SEEK_SET
) != -1
246 && bfd_read ((PTR
) buf
, hdr
->p_filesz
, 1, abfd
) == hdr
->p_filesz
)
248 x_note_p
= (Elf_External_Note
*) buf
;
249 while ((char *) x_note_p
< (buf
+ hdr
->p_filesz
))
251 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->namesz
);
252 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->descsz
);
253 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
->type
);
254 namedata
= x_note_p
->name
;
255 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
256 filepos
= hdr
->p_offset
+ (descdata
- buf
);
260 /* process descdata as prstatus info */
261 if (! bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
))
263 sectname
= ".prstatus";
266 /* process descdata as fpregset info */
267 if (! bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
))
269 sectname
= ".fpregset";
272 /* process descdata as prpsinfo */
273 if (! bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
))
275 sectname
= ".prpsinfo";
278 /* Unknown descriptor, just ignore it. */
282 if (sectname
!= NULL
)
284 newsect
= bfd_make_section (abfd
, sectname
);
287 newsect
->_raw_size
= i_note
.descsz
;
288 newsect
->filepos
= filepos
;
289 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
290 newsect
->alignment_power
= 2;
292 x_note_p
= (Elf_External_Note
*)
293 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
300 else if (hdr
->p_filesz
> 0)
302 bfd_set_error (bfd_error_no_memory
);
309 /* Core files are simply standard ELF formatted files that partition
310 the file using the execution view of the file (program header table)
311 rather than the linking view. In fact, there is no section header
312 table in a core file.
314 The process status information (including the contents of the general
315 register set) and the floating point register set are stored in a
316 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
317 that allow standard bfd access to the general registers (.reg) and the
318 floating point registers (.reg2).
323 elf_core_file_p (abfd
)
326 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
327 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
328 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
329 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
330 unsigned int phindex
;
331 struct elf_backend_data
*ebd
;
333 /* Read in the ELF header in external format. */
335 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
337 if (bfd_get_error () != bfd_error_system_call
)
338 bfd_set_error (bfd_error_wrong_format
);
342 /* Now check to see if we have a valid ELF file, and one that BFD can
343 make use of. The magic number must match, the address size ('class')
344 and byte-swapping must match our XVEC entry, and it must have a
345 program header table (FIXME: See comments re segments at top of this
348 if (elf_file_p (&x_ehdr
) == false)
351 bfd_set_error (bfd_error_wrong_format
);
355 /* FIXME, Check EI_VERSION here ! */
359 int desired_address_size
= ELFCLASS32
;
362 int desired_address_size
= ELFCLASS64
;
365 if (x_ehdr
.e_ident
[EI_CLASS
] != desired_address_size
)
369 /* Switch xvec to match the specified byte order. */
370 switch (x_ehdr
.e_ident
[EI_DATA
])
372 case ELFDATA2MSB
: /* Big-endian */
373 if (abfd
->xvec
->byteorder_big_p
== false)
376 case ELFDATA2LSB
: /* Little-endian */
377 if (abfd
->xvec
->byteorder_big_p
== true)
380 case ELFDATANONE
: /* No data encoding specified */
381 default: /* Unknown data encoding specified */
385 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
386 the tdata pointer in the bfd. */
389 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
390 if (elf_tdata (abfd
) == NULL
)
392 bfd_set_error (bfd_error_no_memory
);
396 /* FIXME, `wrong' returns from this point onward, leak memory. */
398 /* Now that we know the byte order, swap in the rest of the header */
399 i_ehdrp
= elf_elfheader (abfd
);
400 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
402 elf_debug_file (i_ehdrp
);
405 ebd
= get_elf_backend_data (abfd
);
407 /* Check that the ELF e_machine field matches what this particular
408 BFD format expects. */
409 if (ebd
->elf_machine_code
!= i_ehdrp
->e_machine
410 && (ebd
->elf_machine_alt1
== 0 || i_ehdrp
->e_machine
!= ebd
->elf_machine_alt1
)
411 && (ebd
->elf_machine_alt2
== 0 || i_ehdrp
->e_machine
!= ebd
->elf_machine_alt2
))
413 const bfd_target
* const *target_ptr
;
415 if (ebd
->elf_machine_code
!= EM_NONE
)
418 /* This is the generic ELF target. Let it match any ELF target
419 for which we do not have a specific backend. */
420 for (target_ptr
= bfd_target_vector
; *target_ptr
!= NULL
; target_ptr
++)
422 struct elf_backend_data
*back
;
424 if ((*target_ptr
)->flavour
!= bfd_target_elf_flavour
)
426 back
= (struct elf_backend_data
*) (*target_ptr
)->backend_data
;
427 if (back
->elf_machine_code
== i_ehdrp
->e_machine
)
429 /* target_ptr is an ELF backend which matches this
430 object file, so reject the generic ELF target. */
436 /* If there is no program header, or the type is not a core file, then
438 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
441 /* Allocate space for a copy of the program header table in
442 internal form, seek to the program header table in the file,
443 read it in, and convert it to internal form. As a simple sanity
444 check, verify that the what BFD thinks is the size of each program
445 header table entry actually matches the size recorded in the file. */
447 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
449 i_phdrp
= (Elf_Internal_Phdr
*)
450 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
453 bfd_set_error (bfd_error_no_memory
);
456 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
458 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
460 if (bfd_read ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
463 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
466 /* Once all of the program headers have been read and converted, we
467 can start processing them. */
469 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
471 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
472 if ((i_phdrp
+ phindex
)->p_type
== PT_NOTE
)
474 if (! elf_corefile_note (abfd
, i_phdrp
+ phindex
))
479 /* Remember the entry point specified in the ELF file header. */
481 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
This page took 0.041885 seconds and 4 git commands to generate.