1/* BFD back-end for HPPA BSD core files.
2   Copyright 1993, 1994, 1995, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
3   2006, 2007 Free Software Foundation, Inc.
4
5   This file is part of BFD, the Binary File Descriptor library.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 3 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20   MA 02110-1301, USA.
21
22   Written by the Center for Software Science at the University of Utah
23   and by Cygnus Support.
24
25   The core file structure for the Utah 4.3BSD and OSF1 ports on the
26   PA is a mix between traditional cores and hpux cores -- just
27   different enough that supporting this format would tend to add
28   gross hacks to trad-core.c or hpux-core.c.  So instead we keep any
29   gross hacks isolated to this file.  */
30
31/* This file can only be compiled on systems which use HPPA-BSD style
32   core files.
33
34   I would not expect this to be of use to any other host/target, but
35   you never know.  */
36
37#include "sysdep.h"
38#include "bfd.h"
39#include "libbfd.h"
40
41#if defined (HOST_HPPABSD)
42
43#include "machine/vmparam.h"
44
45#include <sys/param.h>
46#include <sys/dir.h>
47#include <signal.h>
48#include <machine/reg.h>
49#include <sys/user.h>		/* After a.out.h  */
50#include <sys/file.h>
51
52static asection *make_bfd_asection
53  PARAMS ((bfd *, const char *, flagword, bfd_size_type, file_ptr,
54	   unsigned int));
55static const bfd_target *hppabsd_core_core_file_p
56  PARAMS ((bfd *));
57static char *hppabsd_core_core_file_failing_command
58  PARAMS ((bfd *));
59static int hppabsd_core_core_file_failing_signal
60  PARAMS ((bfd *));
61#define hppabsd_core_core_file_matches_executable_p generic_core_file_matches_executable_p
62static void swap_abort
63  PARAMS ((void));
64
65/* These are stored in the bfd's tdata.  */
66
67struct hppabsd_core_struct
68  {
69    int sig;
70    char cmd[MAXCOMLEN + 1];
71    asection *data_section;
72    asection *stack_section;
73    asection *reg_section;
74  };
75
76#define core_hdr(bfd) ((bfd)->tdata.hppabsd_core_data)
77#define core_signal(bfd) (core_hdr(bfd)->sig)
78#define core_command(bfd) (core_hdr(bfd)->cmd)
79#define core_datasec(bfd) (core_hdr(bfd)->data_section)
80#define core_stacksec(bfd) (core_hdr(bfd)->stack_section)
81#define core_regsec(bfd) (core_hdr(bfd)->reg_section)
82
83static asection *
84make_bfd_asection (abfd, name, flags, size, offset, alignment_power)
85     bfd *abfd;
86     const char *name;
87     flagword flags;
88     bfd_size_type size;
89     file_ptr offset;
90     unsigned int alignment_power;
91{
92  asection *asect;
93
94  asect = bfd_make_section_with_flags (abfd, name, flags);
95  if (!asect)
96    return NULL;
97
98  asect->size = size;
99  asect->filepos = offset;
100  asect->alignment_power = alignment_power;
101
102  return asect;
103}
104
105static const bfd_target *
106hppabsd_core_core_file_p (abfd)
107     bfd *abfd;
108{
109  int val;
110  struct user u;
111  struct hppabsd_core_struct *coredata;
112  int clicksz;
113
114  /* Try to read in the u-area.  We will need information from this
115     to know how to grok the rest of the core structures.  */
116  val = bfd_bread ((void *) &u, (bfd_size_type) sizeof u, abfd);
117  if (val != sizeof u)
118    {
119      if (bfd_get_error () != bfd_error_system_call)
120	bfd_set_error (bfd_error_wrong_format);
121      return NULL;
122    }
123
124  /* Get the page size out of the u structure.  This will be different
125     for PA 1.0 machines and PA 1.1 machines.   Yuk!  */
126  clicksz = u.u_pcb.pcb_pgsz;
127
128  /* clicksz must be a power of two >= 2k.  */
129  if (clicksz < 0x800
130      || clicksz != (clicksz & -clicksz))
131    {
132      bfd_set_error (bfd_error_wrong_format);
133      return NULL;
134    }
135
136  /* Sanity checks.  Make sure the size of the core file matches the
137     the size computed from information within the core itself.  */
138  {
139    struct stat statbuf;
140
141    if (bfd_stat (abfd, &statbuf) < 0)
142      return NULL;
143
144    if (NBPG * (UPAGES + u.u_dsize + u.u_ssize) > statbuf.st_size)
145      {
146	bfd_set_error (bfd_error_file_truncated);
147	return NULL;
148      }
149    if (clicksz * (UPAGES + u.u_dsize + u.u_ssize) < statbuf.st_size)
150      {
151	/* The file is too big.  Maybe it's not a core file
152	   or we otherwise have bad values for u_dsize and u_ssize).  */
153	bfd_set_error (bfd_error_wrong_format);
154	return NULL;
155      }
156  }
157
158  /* OK, we believe you.  You're a core file (sure, sure).  */
159
160  coredata = (struct hppabsd_core_struct *)
161    bfd_zalloc (abfd, (bfd_size_type) sizeof (struct hppabsd_core_struct));
162  if (!coredata)
163    return NULL;
164
165  /* Make the core data and available via the tdata part of the BFD.  */
166  abfd->tdata.hppabsd_core_data = coredata;
167
168  /* Create the sections.  */
169  core_stacksec (abfd) = make_bfd_asection (abfd, ".stack",
170					   SEC_ALLOC + SEC_HAS_CONTENTS,
171					   clicksz * u.u_ssize,
172					   NBPG * (USIZE + KSTAKSIZE)
173					     + clicksz * u.u_dsize, 2);
174  if (core_stacksec (abfd) == NULL)
175    goto fail;
176  core_stacksec (abfd)->vma = USRSTACK;
177
178  core_datasec (abfd) = make_bfd_asection (abfd, ".data",
179					  SEC_ALLOC + SEC_LOAD
180					    + SEC_HAS_CONTENTS,
181					  clicksz * u.u_dsize,
182					  NBPG * (USIZE + KSTAKSIZE), 2);
183  if (core_datasec (abfd) == NULL)
184    goto fail;
185  core_datasec (abfd)->vma = UDATASEG;
186
187  core_regsec (abfd) = make_bfd_asection (abfd, ".reg",
188					 SEC_HAS_CONTENTS,
189					 KSTAKSIZE * NBPG,
190					 NBPG * USIZE, 2);
191  if (core_regsec (abfd) == NULL)
192    goto fail;
193  core_regsec (abfd)->vma = 0;
194
195  strncpy (core_command (abfd), u.u_comm, MAXCOMLEN + 1);
196  core_signal (abfd) = u.u_code;
197  return abfd->xvec;
198
199 fail:
200  bfd_release (abfd, abfd->tdata.any);
201  abfd->tdata.any = NULL;
202  bfd_section_list_clear (abfd);
203  return NULL;
204}
205
206static char *
207hppabsd_core_core_file_failing_command (abfd)
208     bfd *abfd;
209{
210  return core_command (abfd);
211}
212
213static int
214hppabsd_core_core_file_failing_signal (abfd)
215     bfd *abfd;
216{
217  return core_signal (abfd);
218}
219
220/* If somebody calls any byte-swapping routines, shoot them.  */
221static void
222swap_abort ()
223{
224  /* This way doesn't require any declaration for ANSI to fuck up.  */
225  abort ();
226}
227
228#define	NO_GET ((bfd_vma (*) (const void *)) swap_abort)
229#define	NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
230#define	NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
231#define	NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
232#define	NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
233#define	NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
234
235const bfd_target hppabsd_core_vec =
236  {
237    "hppabsd-core",
238    bfd_target_unknown_flavour,
239    BFD_ENDIAN_BIG,		/* target byte order */
240    BFD_ENDIAN_BIG,		/* target headers byte order */
241    (HAS_RELOC | EXEC_P |	/* object flags */
242     HAS_LINENO | HAS_DEBUG |
243     HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
244    (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
245    0,			                                   /* symbol prefix */
246    ' ',						   /* ar_pad_char */
247    16,							   /* ar_max_namelen */
248    NO_GET64, NO_GETS64, NO_PUT64,	/* 64 bit data */
249    NO_GET, NO_GETS, NO_PUT,		/* 32 bit data */
250    NO_GET, NO_GETS, NO_PUT,		/* 16 bit data */
251    NO_GET64, NO_GETS64, NO_PUT64,	/* 64 bit hdrs */
252    NO_GET, NO_GETS, NO_PUT,		/* 32 bit hdrs */
253    NO_GET, NO_GETS, NO_PUT,		/* 16 bit hdrs */
254
255    {				/* bfd_check_format */
256      _bfd_dummy_target,		/* unknown format */
257      _bfd_dummy_target,		/* object file */
258      _bfd_dummy_target,		/* archive */
259      hppabsd_core_core_file_p		/* a core file */
260    },
261    {				/* bfd_set_format */
262      bfd_false, bfd_false,
263      bfd_false, bfd_false
264    },
265    {				/* bfd_write_contents */
266      bfd_false, bfd_false,
267      bfd_false, bfd_false
268    },
269
270    BFD_JUMP_TABLE_GENERIC (_bfd_generic),
271    BFD_JUMP_TABLE_COPY (_bfd_generic),
272    BFD_JUMP_TABLE_CORE (hppabsd_core),
273    BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
274    BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
275    BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
276    BFD_JUMP_TABLE_WRITE (_bfd_generic),
277    BFD_JUMP_TABLE_LINK (_bfd_nolink),
278    BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
279
280    NULL,
281
282    (PTR) 0			/* backend_data */
283  };
284#endif
285