1220Snever/* BFD back-end for Irix core files.
28346Smhaupt   Copyright 1993, 1994, 1996, 1999, 2001, 2002, 2004
3220Snever   Free Software Foundation, Inc.
4220Snever   Written by Stu Grossman, Cygnus Support.
5220Snever   Converted to back-end form by Ian Lance Taylor, Cygnus Support
6220Snever
7220Snever   This file is part of BFD, the Binary File Descriptor library.
8220Snever
9220Snever   This program is free software; you can redistribute it and/or modify
10220Snever   it under the terms of the GNU General Public License as published by
11220Snever   the Free Software Foundation; either version 2 of the License, or
12220Snever   (at your option) any later version.
13220Snever
14220Snever   This program is distributed in the hope that it will be useful,
15220Snever   but WITHOUT ANY WARRANTY; without even the implied warranty of
16220Snever   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17220Snever   GNU General Public License for more details.
18220Snever
191472Strims   You should have received a copy of the GNU General Public License
201472Strims   along with this program; if not, write to the Free Software
211472Strims   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
22220Snever
23220Snever/* This file can only be compiled on systems which use Irix style core
24220Snever   files (namely, Irix 4 and Irix 5, so far).  */
25220Snever
26220Snever#include "bfd.h"
27220Snever#include "sysdep.h"
28220Snever#include "libbfd.h"
29220Snever
30220Snever#ifdef IRIX_CORE
31220Snever
32220Snever#include <core.out.h>
33220Snever
34220Sneverstruct sgi_core_struct
35220Snever{
36220Snever  int sig;
37220Snever  char cmd[CORE_NAMESIZE];
38220Snever};
39220Snever
40220Snever#define core_hdr(bfd) ((bfd)->tdata.sgi_core_data)
41220Snever#define core_signal(bfd) (core_hdr(bfd)->sig)
42220Snever#define core_command(bfd) (core_hdr(bfd)->cmd)
43220Snever
44220Snever#define irix_core_core_file_matches_executable_p generic_core_file_matches_executable_p
45220Snever
46220Sneverstatic asection *make_bfd_asection
47220Snever  (bfd *, const char *, flagword, bfd_size_type, bfd_vma, file_ptr);
48220Snever
49220Snever/* Helper function for irix_core_core_file_p:
50220Snever   32-bit and 64-bit versions.  */
51220Snever
52220Snever#ifdef CORE_MAGIC64
53220Sneverstatic int
54220Sneverdo_sections64 (bfd *abfd, struct coreout *coreout)
55220Snever{
56220Snever  struct vmap64 vmap;
57220Snever  char *secname;
58220Snever  int i, val;
59220Snever
60220Snever  for (i = 0; i < coreout->c_nvmap; i++)
61220Snever    {
62220Snever      val = bfd_bread ((PTR) &vmap, (bfd_size_type) sizeof vmap, abfd);
63220Snever      if (val != sizeof vmap)
64220Snever	break;
65220Snever
66220Snever      switch (vmap.v_type)
67220Snever	{
68220Snever	case VDATA:
69220Snever	  secname = ".data";
70220Snever	  break;
71220Snever	case VSTACK:
72220Snever	  secname = ".stack";
73220Snever	  break;
74220Snever#ifdef VMAPFILE
75220Snever	case VMAPFILE:
76220Snever	  secname = ".mapfile";
77220Snever	  break;
78220Snever#endif
79220Snever	default:
80220Snever	  continue;
81220Snever	}
82220Snever
83220Snever      /* A file offset of zero means that the
84220Snever	 section is not contained in the corefile.  */
85220Snever      if (vmap.v_offset == 0)
86220Snever	continue;
87220Snever
88220Snever      if (!make_bfd_asection (abfd, secname,
89220Snever			      SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
90220Snever			      vmap.v_len, vmap.v_vaddr, vmap.v_offset))
91220Snever	/* Fail.  */
92220Snever	return 0;
93220Snever    }
94220Snever
95220Snever  return 1;
96220Snever}
97220Snever#endif
98220Snever
99220Snever/* 32-bit version.  */
100220Snever
101220Sneverstatic int
102220Sneverdo_sections (bfd *abfd, struct coreout *coreout)
103220Snever{
104220Snever  struct vmap vmap;
105220Snever  char *secname;
106220Snever  int i, val;
107220Snever
108220Snever  for (i = 0; i < coreout->c_nvmap; i++)
109220Snever    {
110220Snever      val = bfd_bread ((PTR) &vmap, (bfd_size_type) sizeof vmap, abfd);
111220Snever      if (val != sizeof vmap)
112220Snever	break;
113220Snever
114220Snever      switch (vmap.v_type)
115220Snever	{
116220Snever	case VDATA:
117220Snever	  secname = ".data";
118220Snever	  break;
119220Snever	case VSTACK:
120220Snever	  secname = ".stack";
121220Snever	  break;
122220Snever#ifdef VMAPFILE
123220Snever	case VMAPFILE:
124220Snever	  secname = ".mapfile";
125220Snever	  break;
126220Snever#endif
127220Snever	default:
128220Snever	  continue;
129220Snever	}
130220Snever
131220Snever      /* A file offset of zero means that the
132220Snever	 section is not contained in the corefile.  */
133220Snever      if (vmap.v_offset == 0)
134220Snever	continue;
135220Snever
136220Snever      if (!make_bfd_asection (abfd, secname,
137220Snever			      SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
138220Snever			      vmap.v_len, vmap.v_vaddr, vmap.v_offset))
139220Snever	/* Fail.  */
140220Snever	return 0;
141220Snever    }
142220Snever  return 1;
143220Snever}
144220Snever
145220Sneverstatic asection *
146make_bfd_asection (bfd *abfd,
147                   const char *name,
148                   flagword flags,
149                   bfd_size_type size,
150                   bfd_vma vma,
151                   file_ptr filepos)
152{
153  asection *asect;
154
155  asect = bfd_make_section_anyway (abfd, name);
156  if (!asect)
157    return NULL;
158
159  asect->flags = flags;
160  asect->size = size;
161  asect->vma = vma;
162  asect->filepos = filepos;
163  asect->alignment_power = 4;
164
165  return asect;
166}
167
168static const bfd_target *
169irix_core_core_file_p (bfd *abfd)
170{
171  int val;
172  struct coreout coreout;
173  struct idesc *idg, *idf, *ids;
174  bfd_size_type amt;
175
176  val = bfd_bread ((PTR) &coreout, (bfd_size_type) sizeof coreout, abfd);
177  if (val != sizeof coreout)
178    {
179      if (bfd_get_error () != bfd_error_system_call)
180	bfd_set_error (bfd_error_wrong_format);
181      return 0;
182    }
183
184  if (coreout.c_version != CORE_VERSION1)
185    return 0;
186
187  /* Have we got a corefile?  */
188  switch (coreout.c_magic)
189    {
190    case CORE_MAGIC:	break;
191#ifdef CORE_MAGIC64
192    case CORE_MAGIC64:	break;
193#endif
194#ifdef CORE_MAGICN32
195    case CORE_MAGICN32:	break;
196#endif
197    default:		return 0;	/* Un-identifiable or not corefile.  */
198    }
199
200  amt = sizeof (struct sgi_core_struct);
201  core_hdr (abfd) = (struct sgi_core_struct *) bfd_zalloc (abfd, amt);
202  if (!core_hdr (abfd))
203    return NULL;
204
205  strncpy (core_command (abfd), coreout.c_name, CORE_NAMESIZE);
206  core_signal (abfd) = coreout.c_sigcause;
207
208  if (bfd_seek (abfd, coreout.c_vmapoffset, SEEK_SET) != 0)
209    goto fail;
210
211  /* Process corefile sections.  */
212#ifdef CORE_MAGIC64
213  if (coreout.c_magic == (int) CORE_MAGIC64)
214    {
215      if (! do_sections64 (abfd, & coreout))
216	goto fail;
217    }
218  else
219#endif
220    if (! do_sections (abfd, & coreout))
221      goto fail;
222
223  /* Make sure that the regs are contiguous within the core file.  */
224
225  idg = &coreout.c_idesc[I_GPREGS];
226  idf = &coreout.c_idesc[I_FPREGS];
227  ids = &coreout.c_idesc[I_SPECREGS];
228
229  if (idg->i_offset + idg->i_len != idf->i_offset
230      || idf->i_offset + idf->i_len != ids->i_offset)
231    goto fail;			/* Can't deal with non-contig regs */
232
233  if (bfd_seek (abfd, idg->i_offset, SEEK_SET) != 0)
234    goto fail;
235
236  if (!make_bfd_asection (abfd, ".reg",
237			  SEC_HAS_CONTENTS,
238			  idg->i_len + idf->i_len + ids->i_len,
239			  0,
240			  idg->i_offset))
241    goto fail;
242
243  /* OK, we believe you.  You're a core file (sure, sure).  */
244  bfd_default_set_arch_mach (abfd, bfd_arch_mips, 0);
245
246  return abfd->xvec;
247
248 fail:
249  bfd_release (abfd, core_hdr (abfd));
250  core_hdr (abfd) = NULL;
251  bfd_section_list_clear (abfd);
252  return NULL;
253}
254
255static char *
256irix_core_core_file_failing_command (bfd *abfd)
257{
258  return core_command (abfd);
259}
260
261static int
262irix_core_core_file_failing_signal (bfd *abfd)
263{
264  return core_signal (abfd);
265}
266
267/* If somebody calls any byte-swapping routines, shoot them.  */
268static void
269swap_abort(void)
270{
271  abort(); /* This way doesn't require any declaration for ANSI to fuck up */
272}
273
274#define	NO_GET ((bfd_vma (*) (const void *)) swap_abort)
275#define	NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
276#define	NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
277#define	NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
278#define	NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
279#define	NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
280
281const bfd_target irix_core_vec =
282  {
283    "irix-core",
284    bfd_target_unknown_flavour,
285    BFD_ENDIAN_BIG,		/* target byte order */
286    BFD_ENDIAN_BIG,		/* target headers byte order */
287    (HAS_RELOC | EXEC_P |	/* object flags */
288     HAS_LINENO | HAS_DEBUG |
289     HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
290    (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
291    0,			                                   /* symbol prefix */
292    ' ',						   /* ar_pad_char */
293    16,							   /* ar_max_namelen */
294    NO_GET64, NO_GETS64, NO_PUT64,	/* 64 bit data */
295    NO_GET, NO_GETS, NO_PUT,		/* 32 bit data */
296    NO_GET, NO_GETS, NO_PUT,		/* 16 bit data */
297    NO_GET64, NO_GETS64, NO_PUT64,	/* 64 bit hdrs */
298    NO_GET, NO_GETS, NO_PUT,		/* 32 bit hdrs */
299    NO_GET, NO_GETS, NO_PUT,		/* 16 bit hdrs */
300
301    {				/* bfd_check_format */
302      _bfd_dummy_target,		/* unknown format */
303      _bfd_dummy_target,		/* object file */
304      _bfd_dummy_target,		/* archive */
305      irix_core_core_file_p		/* a core file */
306    },
307    {				/* bfd_set_format */
308      bfd_false, bfd_false,
309      bfd_false, bfd_false
310    },
311    {				/* bfd_write_contents */
312      bfd_false, bfd_false,
313      bfd_false, bfd_false
314    },
315
316    BFD_JUMP_TABLE_GENERIC (_bfd_generic),
317    BFD_JUMP_TABLE_COPY (_bfd_generic),
318    BFD_JUMP_TABLE_CORE (irix_core),
319    BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
320    BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
321    BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
322    BFD_JUMP_TABLE_WRITE (_bfd_generic),
323    BFD_JUMP_TABLE_LINK (_bfd_nolink),
324    BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
325
326    NULL,
327
328    (PTR) 0			/* backend_data */
329  };
330
331#endif /* IRIX_CORE */
332