144165Sjulian/* ELF core file support for BFD.
244165Sjulian   Copyright 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003
344165Sjulian   Free Software Foundation, Inc.
444165Sjulian
544165Sjulian   This file is part of BFD, the Binary File Descriptor library.
644165Sjulian
744165Sjulian   This program is free software; you can redistribute it and/or modify
844165Sjulian   it under the terms of the GNU General Public License as published by
944165Sjulian   the Free Software Foundation; either version 2 of the License, or
1044165Sjulian   (at your option) any later version.
1144165Sjulian
1244165Sjulian   This program is distributed in the hope that it will be useful,
1344165Sjulian   but WITHOUT ANY WARRANTY; without even the implied warranty of
1444165Sjulian   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1544165Sjulian   GNU General Public License for more details.
1644165Sjulian
1744165Sjulian   You should have received a copy of the GNU General Public License
1844165Sjulian   along with this program; if not, write to the Free Software
1944165Sjulian   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
2044165Sjulian
2144165Sjulianchar*
2244165Sjulianelf_core_file_failing_command (bfd *abfd)
2344165Sjulian{
2444165Sjulian  return elf_tdata (abfd)->core_command;
2544165Sjulian}
2644165Sjulian
2744165Sjulianint
2844165Sjulianelf_core_file_failing_signal (bfd *abfd)
2944165Sjulian{
3044165Sjulian  return elf_tdata (abfd)->core_signal;
3144165Sjulian}
3244165Sjulian
3350477Speterbfd_boolean
3444165Sjulianelf_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
3544165Sjulian{
3644165Sjulian  char* corename;
3744165Sjulian
3844165Sjulian  /* xvecs must match if both are ELF files for the same target.  */
3944165Sjulian
4044165Sjulian  if (core_bfd->xvec != exec_bfd->xvec)
4144165Sjulian    {
4244165Sjulian      bfd_set_error (bfd_error_system_call);
4344165Sjulian      return FALSE;
4444165Sjulian    }
4544165Sjulian
4644165Sjulian  /* See if the name in the corefile matches the executable name.  */
4758313Slile  corename = elf_tdata (core_bfd)->core_program;
4844165Sjulian  if (corename != NULL)
4944165Sjulian    {
5058313Slile      const char* execname = strrchr (exec_bfd->filename, '/');
5158313Slile
5258313Slile      execname = execname ? execname + 1 : exec_bfd->filename;
5358313Slile
5458313Slile      if (strcmp (execname, corename) != 0)
5544165Sjulian	return FALSE;
5658313Slile    }
5758313Slile
5844165Sjulian  return TRUE;
5958313Slile}
6058313Slile
6158313Slile/*  Core files are simply standard ELF formatted files that partition
6244165Sjulian    the file using the execution view of the file (program header table)
6344165Sjulian    rather than the linking view.  In fact, there is no section header
6458313Slile    table in a core file.
6544165Sjulian
6658313Slile    The process status information (including the contents of the general
6758313Slile    register set) and the floating point register set are stored in a
6858313Slile    segment of type PT_NOTE.  We handcraft a couple of extra bfd sections
6958313Slile    that allow standard bfd access to the general registers (.reg) and the
7058313Slile    floating point registers (.reg2).  */
7144165Sjulian
7258313Slileconst bfd_target *
7358313Slileelf_core_file_p (bfd *abfd)
7444165Sjulian{
7558313Slile  Elf_External_Ehdr x_ehdr;	/* Elf file header, external form.  */
7644165Sjulian  Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
7758313Slile  Elf_Internal_Phdr *i_phdrp;	/* Elf program header, internal form.  */
7844165Sjulian  unsigned int phindex;
7944165Sjulian  const struct elf_backend_data *ebd;
8044165Sjulian  struct bfd_preserve preserve;
8144165Sjulian  bfd_size_type amt;
8244165Sjulian
8344165Sjulian  preserve.marker = NULL;
8444165Sjulian
8544165Sjulian  /* Read in the ELF header in external format.  */
8644165Sjulian  if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
8758313Slile    {
8844165Sjulian      if (bfd_get_error () != bfd_error_system_call)
8944165Sjulian	goto wrong;
9058313Slile      else
9158313Slile	goto fail;
9258313Slile    }
9358313Slile
9458313Slile  /* Check the magic number.  */
9558313Slile  if (! elf_file_p (&x_ehdr))
9644165Sjulian    goto wrong;
9744165Sjulian
9844627Sjulian  /* FIXME: Check EI_VERSION here !  */
9944627Sjulian
10044627Sjulian  /* Check the address size ("class").  */
10144627Sjulian  if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
10244627Sjulian    goto wrong;
10344627Sjulian
10444627Sjulian  /* Check the byteorder.  */
10544165Sjulian  switch (x_ehdr.e_ident[EI_DATA])
10644165Sjulian    {
10744165Sjulian    case ELFDATA2MSB:		/* Big-endian.  */
10844165Sjulian      if (! bfd_big_endian (abfd))
10944165Sjulian	goto wrong;
11044165Sjulian      break;
11144165Sjulian    case ELFDATA2LSB:		/* Little-endian.  */
11244165Sjulian      if (! bfd_little_endian (abfd))
11358313Slile	goto wrong;
11458313Slile      break;
11544165Sjulian    default:
11644165Sjulian      goto wrong;
11758313Slile    }
11858313Slile
11958313Slile  if (!bfd_preserve_save (abfd, &preserve))
12058313Slile    goto fail;
12144165Sjulian
12244165Sjulian  /* Give abfd an elf_obj_tdata.  */
123  if (! (*abfd->xvec->_bfd_set_format[bfd_core]) (abfd))
124    goto fail;
125  preserve.marker = elf_tdata (abfd);
126
127  /* Swap in the rest of the header, now that we have the byte order.  */
128  i_ehdrp = elf_elfheader (abfd);
129  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
130
131#if DEBUG & 1
132  elf_debug_file (i_ehdrp);
133#endif
134
135  ebd = get_elf_backend_data (abfd);
136
137  /* Check that the ELF e_machine field matches what this particular
138     BFD format expects.  */
139
140  if (ebd->elf_machine_code != i_ehdrp->e_machine
141      && (ebd->elf_machine_alt1 == 0
142	  || i_ehdrp->e_machine != ebd->elf_machine_alt1)
143      && (ebd->elf_machine_alt2 == 0
144	  || i_ehdrp->e_machine != ebd->elf_machine_alt2))
145    {
146      const bfd_target * const *target_ptr;
147
148      if (ebd->elf_machine_code != EM_NONE)
149	goto wrong;
150
151      /* This is the generic ELF target.  Let it match any ELF target
152	 for which we do not have a specific backend.  */
153
154      for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
155	{
156	  const struct elf_backend_data *back;
157
158	  if ((*target_ptr)->flavour != bfd_target_elf_flavour)
159	    continue;
160	  back = (const struct elf_backend_data *) (*target_ptr)->backend_data;
161	  if (back->elf_machine_code == i_ehdrp->e_machine
162	      || (back->elf_machine_alt1 != 0
163	          && i_ehdrp->e_machine == back->elf_machine_alt1)
164	      || (back->elf_machine_alt2 != 0
165	          && i_ehdrp->e_machine == back->elf_machine_alt2))
166	    {
167	      /* target_ptr is an ELF backend which matches this
168		 object file, so reject the generic ELF target.  */
169	      goto wrong;
170	    }
171	}
172    }
173
174  /* If there is no program header, or the type is not a core file, then
175     we are hosed.  */
176  if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
177    goto wrong;
178
179  /* Does BFD's idea of the phdr size match the size
180     recorded in the file? */
181  if (i_ehdrp->e_phentsize != sizeof (Elf_External_Phdr))
182    goto wrong;
183
184  /* Move to the start of the program headers.  */
185  if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0)
186    goto wrong;
187
188  /* Allocate space for the program headers.  */
189  amt = sizeof (*i_phdrp) * i_ehdrp->e_phnum;
190  i_phdrp = bfd_alloc (abfd, amt);
191  if (!i_phdrp)
192    goto fail;
193
194  elf_tdata (abfd)->phdr = i_phdrp;
195
196  /* Read and convert to internal form.  */
197  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
198    {
199      Elf_External_Phdr x_phdr;
200
201      if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
202	goto fail;
203
204      elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
205    }
206
207  /* Set the machine architecture.  Do this before processing the
208     program headers since we need to know the architecture type
209     when processing the notes of some systems' core files.  */
210  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
211    {
212      /* It's OK if this fails for the generic target.  */
213      if (ebd->elf_machine_code != EM_NONE)
214	goto fail;
215    }
216
217  /* Process each program header.  */
218  for (phindex = 0; phindex < i_ehdrp->e_phnum; ++phindex)
219    if (! bfd_section_from_phdr (abfd, i_phdrp + phindex, (int) phindex))
220      goto fail;
221
222  /* Save the entry point from the ELF header.  */
223  bfd_get_start_address (abfd) = i_ehdrp->e_entry;
224
225  /* Let the backend double check the format and override global
226     information.  */
227  if (ebd->elf_backend_object_p
228      && (! (*ebd->elf_backend_object_p) (abfd)))
229    goto wrong;
230
231  bfd_preserve_finish (abfd, &preserve);
232  return abfd->xvec;
233
234wrong:
235  /* There is way too much undoing of half-known state here.  The caller,
236     bfd_check_format_matches, really shouldn't iterate on live bfd's to
237     check match/no-match like it does.  We have to rely on that a call to
238     bfd_default_set_arch_mach with the previously known mach, undoes what
239     was done by the first bfd_default_set_arch_mach (with mach 0) here.
240     For this to work, only elf-data and the mach may be changed by the
241     target-specific elf_backend_object_p function.  Note that saving the
242     whole bfd here and restoring it would be even worse; the first thing
243     you notice is that the cached bfd file position gets out of sync.  */
244  bfd_set_error (bfd_error_wrong_format);
245
246fail:
247  if (preserve.marker != NULL)
248    bfd_preserve_restore (abfd, &preserve);
249  return NULL;
250}
251