1/* BFD back-end for OSF/1 core files.
2   Copyright 1993, 1994, 1995, 1998, 1999, 2001, 2002, 2003, 2004
3   Free Software Foundation, Inc.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21/* This file can only be compiled on systems which use OSF/1 style
22   core files.  */
23
24#include "bfd.h"
25#include "sysdep.h"
26#include "libbfd.h"
27
28#include <sys/user.h>
29#ifdef OSF_CORE
30#include <sys/core.h>
31#endif
32
33/* forward declarations */
34
35static asection *make_bfd_asection
36  PARAMS ((bfd *, const char *, flagword, bfd_size_type, bfd_vma, file_ptr));
37static const bfd_target *osf_core_core_file_p
38  PARAMS ((bfd *));
39static char *osf_core_core_file_failing_command
40  PARAMS ((bfd *));
41static int osf_core_core_file_failing_signal
42  PARAMS ((bfd *));
43#define osf_core_core_file_matches_executable_p generic_core_file_matches_executable_p
44static void swap_abort
45  PARAMS ((void));
46
47/* These are stored in the bfd's tdata */
48
49struct osf_core_struct
50{
51  int sig;
52  char cmd[MAXCOMLEN + 1];
53};
54
55#define core_hdr(bfd) ((bfd)->tdata.osf_core_data)
56#define core_signal(bfd) (core_hdr(bfd)->sig)
57#define core_command(bfd) (core_hdr(bfd)->cmd)
58
59static asection *
60make_bfd_asection (abfd, name, flags, size, vma, filepos)
61     bfd *abfd;
62     const char *name;
63     flagword flags;
64     bfd_size_type size;
65     bfd_vma vma;
66     file_ptr filepos;
67{
68  asection *asect;
69
70  asect = bfd_make_section_anyway (abfd, name);
71  if (!asect)
72    return NULL;
73
74  asect->flags = flags;
75  asect->size = size;
76  asect->vma = vma;
77  asect->filepos = filepos;
78  asect->alignment_power = 8;
79
80  return asect;
81}
82
83static const bfd_target *
84osf_core_core_file_p (abfd)
85     bfd *abfd;
86{
87  int val;
88  int i;
89  char *secname;
90  struct core_filehdr core_header;
91  bfd_size_type amt;
92
93  amt = sizeof core_header;
94  val = bfd_bread ((PTR) &core_header, amt, abfd);
95  if (val != sizeof core_header)
96    return NULL;
97
98  if (strncmp (core_header.magic, "Core", 4) != 0)
99    return NULL;
100
101  core_hdr (abfd) = (struct osf_core_struct *)
102    bfd_zalloc (abfd, (bfd_size_type) sizeof (struct osf_core_struct));
103  if (!core_hdr (abfd))
104    return NULL;
105
106  strncpy (core_command (abfd), core_header.name, MAXCOMLEN + 1);
107  core_signal (abfd) = core_header.signo;
108
109  for (i = 0; i < core_header.nscns; i++)
110    {
111      struct core_scnhdr core_scnhdr;
112      flagword flags;
113
114      amt = sizeof core_scnhdr;
115      val = bfd_bread ((PTR) &core_scnhdr, amt, abfd);
116      if (val != sizeof core_scnhdr)
117	break;
118
119      /* Skip empty sections.  */
120      if (core_scnhdr.size == 0 || core_scnhdr.scnptr == 0)
121	continue;
122
123      switch (core_scnhdr.scntype)
124	{
125	case SCNRGN:
126	  secname = ".data";
127	  flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
128	  break;
129	case SCNSTACK:
130	  secname = ".stack";
131	  flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
132	  break;
133	case SCNREGS:
134	  secname = ".reg";
135	  flags = SEC_HAS_CONTENTS;
136	  break;
137	default:
138	  (*_bfd_error_handler) (_("Unhandled OSF/1 core file section type %d\n"),
139				 core_scnhdr.scntype);
140	  continue;
141	}
142
143      if (!make_bfd_asection (abfd, secname, flags,
144			      (bfd_size_type) core_scnhdr.size,
145			      (bfd_vma) core_scnhdr.vaddr,
146			      (file_ptr) core_scnhdr.scnptr))
147	goto fail;
148    }
149
150  /* OK, we believe you.  You're a core file (sure, sure).  */
151
152  return abfd->xvec;
153
154 fail:
155  bfd_release (abfd, core_hdr (abfd));
156  core_hdr (abfd) = NULL;
157  bfd_section_list_clear (abfd);
158  return NULL;
159}
160
161static char *
162osf_core_core_file_failing_command (abfd)
163     bfd *abfd;
164{
165  return core_command (abfd);
166}
167
168static int
169osf_core_core_file_failing_signal (abfd)
170     bfd *abfd;
171{
172  return core_signal (abfd);
173}
174
175/* If somebody calls any byte-swapping routines, shoot them.  */
176static void
177swap_abort()
178{
179  abort(); /* This way doesn't require any declaration for ANSI to fuck up */
180}
181
182#define	NO_GET ((bfd_vma (*) (const void *)) swap_abort)
183#define	NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
184#define	NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
185#define	NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
186#define	NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
187#define	NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
188
189const bfd_target osf_core_vec =
190  {
191    "osf-core",
192    bfd_target_unknown_flavour,
193    BFD_ENDIAN_LITTLE,		/* target byte order */
194    BFD_ENDIAN_LITTLE,		/* target headers byte order */
195    (HAS_RELOC | EXEC_P |	/* object flags */
196     HAS_LINENO | HAS_DEBUG |
197     HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
198    (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
199    0,			                                   /* symbol prefix */
200    ' ',						   /* ar_pad_char */
201    16,							   /* ar_max_namelen */
202    NO_GET64, NO_GETS64, NO_PUT64,	/* 64 bit data */
203    NO_GET, NO_GETS, NO_PUT,		/* 32 bit data */
204    NO_GET, NO_GETS, NO_PUT,		/* 16 bit data */
205    NO_GET64, NO_GETS64, NO_PUT64,	/* 64 bit hdrs */
206    NO_GET, NO_GETS, NO_PUT,		/* 32 bit hdrs */
207    NO_GET, NO_GETS, NO_PUT,		/* 16 bit hdrs */
208
209    {				/* bfd_check_format */
210      _bfd_dummy_target,		/* unknown format */
211      _bfd_dummy_target,		/* object file */
212      _bfd_dummy_target,		/* archive */
213      osf_core_core_file_p		/* a core file */
214    },
215    {				/* bfd_set_format */
216      bfd_false, bfd_false,
217      bfd_false, bfd_false
218    },
219    {				/* bfd_write_contents */
220      bfd_false, bfd_false,
221      bfd_false, bfd_false
222    },
223
224    BFD_JUMP_TABLE_GENERIC (_bfd_generic),
225    BFD_JUMP_TABLE_COPY (_bfd_generic),
226    BFD_JUMP_TABLE_CORE (osf_core),
227    BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
228    BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
229    BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
230    BFD_JUMP_TABLE_WRITE (_bfd_generic),
231    BFD_JUMP_TABLE_LINK (_bfd_nolink),
232    BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
233
234    NULL,
235
236    (PTR) 0			/* backend_data */
237  };
238