133965Sjdp/* ELF core file support for BFD. 2218822Sdim Copyright 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2005 378828Sobrien Free Software Foundation, Inc. 433965Sjdp 5130561Sobrien This file is part of BFD, the Binary File Descriptor library. 633965Sjdp 7130561Sobrien This program is free software; you can redistribute it and/or modify 8130561Sobrien it under the terms of the GNU General Public License as published by 9130561Sobrien the Free Software Foundation; either version 2 of the License, or 10130561Sobrien (at your option) any later version. 1133965Sjdp 12130561Sobrien This program is distributed in the hope that it will be useful, 13130561Sobrien but WITHOUT ANY WARRANTY; without even the implied warranty of 14130561Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15130561Sobrien GNU General Public License for more details. 1633965Sjdp 17130561Sobrien You should have received a copy of the GNU General Public License 18130561Sobrien along with this program; if not, write to the Free Software 19218822Sdim Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 2033965Sjdp 2160484Sobrienchar* 22130561Sobrienelf_core_file_failing_command (bfd *abfd) 2333965Sjdp{ 2460484Sobrien return elf_tdata (abfd)->core_command; 2533965Sjdp} 2633965Sjdp 2733965Sjdpint 28130561Sobrienelf_core_file_failing_signal (bfd *abfd) 2933965Sjdp{ 3060484Sobrien return elf_tdata (abfd)->core_signal; 3133965Sjdp} 3233965Sjdp 33130561Sobrienbfd_boolean 34130561Sobrienelf_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd) 3533965Sjdp{ 3660484Sobrien char* corename; 3733965Sjdp 3877298Sobrien /* xvecs must match if both are ELF files for the same target. */ 3933965Sjdp 4033965Sjdp if (core_bfd->xvec != exec_bfd->xvec) 4133965Sjdp { 4233965Sjdp bfd_set_error (bfd_error_system_call); 43130561Sobrien return FALSE; 4433965Sjdp } 4533965Sjdp 4677298Sobrien /* See if the name in the corefile matches the executable name. */ 4760484Sobrien corename = elf_tdata (core_bfd)->core_program; 4860484Sobrien if (corename != NULL) 4933965Sjdp { 5060484Sobrien const char* execname = strrchr (exec_bfd->filename, '/'); 51130561Sobrien 5260484Sobrien execname = execname ? execname + 1 : exec_bfd->filename; 5333965Sjdp 54130561Sobrien if (strcmp (execname, corename) != 0) 55130561Sobrien return FALSE; 5633965Sjdp } 5733965Sjdp 58130561Sobrien return TRUE; 5933965Sjdp} 6033965Sjdp 6133965Sjdp/* Core files are simply standard ELF formatted files that partition 6233965Sjdp the file using the execution view of the file (program header table) 6333965Sjdp rather than the linking view. In fact, there is no section header 6433965Sjdp table in a core file. 6533965Sjdp 6633965Sjdp The process status information (including the contents of the general 6733965Sjdp register set) and the floating point register set are stored in a 6833965Sjdp segment of type PT_NOTE. We handcraft a couple of extra bfd sections 6933965Sjdp that allow standard bfd access to the general registers (.reg) and the 70130561Sobrien floating point registers (.reg2). */ 7133965Sjdp 7233965Sjdpconst bfd_target * 73130561Sobrienelf_core_file_p (bfd *abfd) 7433965Sjdp{ 75130561Sobrien Elf_External_Ehdr x_ehdr; /* Elf file header, external form. */ 76130561Sobrien Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ 77130561Sobrien Elf_Internal_Phdr *i_phdrp; /* Elf program header, internal form. */ 7833965Sjdp unsigned int phindex; 79130561Sobrien const struct elf_backend_data *ebd; 8089857Sobrien struct bfd_preserve preserve; 8189857Sobrien bfd_size_type amt; 8233965Sjdp 83130561Sobrien preserve.marker = NULL; 8489857Sobrien 8533965Sjdp /* Read in the ELF header in external format. */ 86130561Sobrien if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr)) 8733965Sjdp { 8833965Sjdp if (bfd_get_error () != bfd_error_system_call) 89130561Sobrien goto wrong; 90130561Sobrien else 91130561Sobrien goto fail; 9233965Sjdp } 9333965Sjdp 9477298Sobrien /* Check the magic number. */ 95104834Sobrien if (! elf_file_p (&x_ehdr)) 9677298Sobrien goto wrong; 9733965Sjdp 98130561Sobrien /* FIXME: Check EI_VERSION here ! */ 9933965Sjdp 10077298Sobrien /* Check the address size ("class"). */ 10160484Sobrien if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS) 10260484Sobrien goto wrong; 10333965Sjdp 10477298Sobrien /* Check the byteorder. */ 10533965Sjdp switch (x_ehdr.e_ident[EI_DATA]) 10633965Sjdp { 107130561Sobrien case ELFDATA2MSB: /* Big-endian. */ 10833965Sjdp if (! bfd_big_endian (abfd)) 10933965Sjdp goto wrong; 11033965Sjdp break; 111130561Sobrien case ELFDATA2LSB: /* Little-endian. */ 11233965Sjdp if (! bfd_little_endian (abfd)) 11333965Sjdp goto wrong; 11433965Sjdp break; 11560484Sobrien default: 11633965Sjdp goto wrong; 11733965Sjdp } 11833965Sjdp 119130561Sobrien if (!bfd_preserve_save (abfd, &preserve)) 120130561Sobrien goto fail; 121130561Sobrien 12277298Sobrien /* Give abfd an elf_obj_tdata. */ 123130561Sobrien if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd)) 12489857Sobrien goto fail; 125130561Sobrien preserve.marker = elf_tdata (abfd); 12689857Sobrien 12777298Sobrien /* Swap in the rest of the header, now that we have the byte order. */ 12833965Sjdp i_ehdrp = elf_elfheader (abfd); 12933965Sjdp elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp); 13060484Sobrien 13133965Sjdp#if DEBUG & 1 13233965Sjdp elf_debug_file (i_ehdrp); 13333965Sjdp#endif 13433965Sjdp 13533965Sjdp ebd = get_elf_backend_data (abfd); 13633965Sjdp 13733965Sjdp /* Check that the ELF e_machine field matches what this particular 13833965Sjdp BFD format expects. */ 13960484Sobrien 14033965Sjdp if (ebd->elf_machine_code != i_ehdrp->e_machine 14160484Sobrien && (ebd->elf_machine_alt1 == 0 14260484Sobrien || i_ehdrp->e_machine != ebd->elf_machine_alt1) 14360484Sobrien && (ebd->elf_machine_alt2 == 0 14460484Sobrien || i_ehdrp->e_machine != ebd->elf_machine_alt2)) 14533965Sjdp { 14633965Sjdp const bfd_target * const *target_ptr; 14733965Sjdp 14833965Sjdp if (ebd->elf_machine_code != EM_NONE) 14933965Sjdp goto wrong; 15033965Sjdp 15133965Sjdp /* This is the generic ELF target. Let it match any ELF target 15233965Sjdp for which we do not have a specific backend. */ 15360484Sobrien 15433965Sjdp for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++) 15533965Sjdp { 156130561Sobrien const struct elf_backend_data *back; 15733965Sjdp 15833965Sjdp if ((*target_ptr)->flavour != bfd_target_elf_flavour) 15933965Sjdp continue; 160130561Sobrien back = (const struct elf_backend_data *) (*target_ptr)->backend_data; 161130561Sobrien if (back->elf_machine_code == i_ehdrp->e_machine 162130561Sobrien || (back->elf_machine_alt1 != 0 163130561Sobrien && i_ehdrp->e_machine == back->elf_machine_alt1) 164130561Sobrien || (back->elf_machine_alt2 != 0 165130561Sobrien && i_ehdrp->e_machine == back->elf_machine_alt2)) 16633965Sjdp { 16733965Sjdp /* target_ptr is an ELF backend which matches this 16833965Sjdp object file, so reject the generic ELF target. */ 16933965Sjdp goto wrong; 17033965Sjdp } 17133965Sjdp } 17233965Sjdp } 17333965Sjdp 17433965Sjdp /* If there is no program header, or the type is not a core file, then 17577298Sobrien we are hosed. */ 17633965Sjdp if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE) 17733965Sjdp goto wrong; 17833965Sjdp 17960484Sobrien /* Does BFD's idea of the phdr size match the size 18060484Sobrien recorded in the file? */ 18160484Sobrien if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr)) 18260484Sobrien goto wrong; 18333965Sjdp 18477298Sobrien /* Move to the start of the program headers. */ 18589857Sobrien if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0) 18677298Sobrien goto wrong; 18777298Sobrien 18877298Sobrien /* Allocate space for the program headers. */ 18989857Sobrien amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum; 190130561Sobrien i_phdrp = bfd_alloc (abfd, amt); 19133965Sjdp if (!i_phdrp) 19277298Sobrien goto fail; 19360484Sobrien 19460484Sobrien elf_tdata (abfd)->phdr = i_phdrp; 19560484Sobrien 19677298Sobrien /* Read and convert to internal form. */ 19760484Sobrien for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex) 19833965Sjdp { 19960484Sobrien Elf_External_Phdr x_phdr; 200130561Sobrien 201130561Sobrien if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr)) 20277298Sobrien goto fail; 20360484Sobrien 20433965Sjdp elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex); 20533965Sjdp } 20633965Sjdp 20791041Sobrien /* Set the machine architecture. Do this before processing the 20891041Sobrien program headers since we need to know the architecture type 20991041Sobrien when processing the notes of some systems' core files. */ 210218822Sdim if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0) 21191041Sobrien /* It's OK if this fails for the generic target. */ 212218822Sdim && ebd->elf_machine_code != EM_NONE) 213218822Sdim goto fail; 21433965Sjdp 215218822Sdim /* Let the backend double check the format and override global 216218822Sdim information. We do this before processing the program headers 217218822Sdim to allow the correct machine (as opposed to just the default 218218822Sdim machine) to be set, making it possible for grok_prstatus and 219218822Sdim grok_psinfo to rely on the mach setting. */ 220218822Sdim if (ebd->elf_backend_object_p != NULL 221218822Sdim && ! ebd->elf_backend_object_p (abfd)) 222218822Sdim goto wrong; 223218822Sdim 22491041Sobrien /* Process each program header. */ 22591041Sobrien for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex) 226130561Sobrien if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex)) 227130561Sobrien goto fail; 22833965Sjdp 22977298Sobrien /* Save the entry point from the ELF header. */ 23033965Sjdp bfd_get_start_address (abfd) = i_ehdrp->e_entry; 23133965Sjdp 232130561Sobrien bfd_preserve_finish (abfd, &preserve); 23333965Sjdp return abfd->xvec; 23477298Sobrien 23577298Sobrienwrong: 23689857Sobrien /* There is way too much undoing of half-known state here. The caller, 23789857Sobrien bfd_check_format_matches, really shouldn't iterate on live bfd's to 23889857Sobrien check match/no-match like it does. We have to rely on that a call to 23989857Sobrien bfd_default_set_arch_mach with the previously known mach, undoes what 24089857Sobrien was done by the first bfd_default_set_arch_mach (with mach 0) here. 24189857Sobrien For this to work, only elf-data and the mach may be changed by the 24289857Sobrien target-specific elf_backend_object_p function. Note that saving the 24389857Sobrien whole bfd here and restoring it would be even worse; the first thing 24489857Sobrien you notice is that the cached bfd file position gets out of sync. */ 24577298Sobrien bfd_set_error (bfd_error_wrong_format); 24689857Sobrien 24777298Sobrienfail: 248130561Sobrien if (preserve.marker != NULL) 249130561Sobrien bfd_preserve_restore (abfd, &preserve); 25077298Sobrien return NULL; 25133965Sjdp} 252