osf-core.c revision 89857
1162271Srwatson/* BFD back-end for OSF/1 core files.
2162271Srwatson   Copyright 1993, 1994, 1995, 1998, 1999, 2001, 2002
3172106Srwatson   Free Software Foundation, Inc.
4162271Srwatson
5162271SrwatsonThis file is part of BFD, the Binary File Descriptor library.
6162271Srwatson
7162271SrwatsonThis program is free software; you can redistribute it and/or modify
8162271Srwatsonit under the terms of the GNU General Public License as published by
9162271Srwatsonthe Free Software Foundation; either version 2 of the License, or
10162271Srwatson(at your option) any later version.
11162271Srwatson
12162271SrwatsonThis program is distributed in the hope that it will be useful,
13162271Srwatsonbut WITHOUT ANY WARRANTY; without even the implied warranty of
14162271SrwatsonMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15162271SrwatsonGNU General Public License for more details.
16162271Srwatson
17162271SrwatsonYou should have received a copy of the GNU General Public License
18162271Srwatsonalong with this program; if not, write to the Free Software
19162271SrwatsonFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20162271Srwatson
21162271Srwatson/* This file can only be compiled on systems which use OSF/1 style
22162271Srwatson   core files.  */
23162271Srwatson
24162271Srwatson#include "bfd.h"
25162271Srwatson#include "sysdep.h"
26162271Srwatson#include "libbfd.h"
27162271Srwatson
28162271Srwatson#include <sys/user.h>
29162271Srwatson#include <sys/core.h>
30162271Srwatson
31162271Srwatson/* forward declarations */
32162271Srwatson
33162271Srwatsonstatic asection *make_bfd_asection
34172106Srwatson  PARAMS ((bfd *, const char *, flagword, bfd_size_type, bfd_vma, file_ptr));
35172106Srwatsonstatic const bfd_target *osf_core_core_file_p PARAMS ((bfd *));
36162271Srwatsonstatic char *osf_core_core_file_failing_command PARAMS ((bfd *));
37162271Srwatsonstatic int osf_core_core_file_failing_signal PARAMS ((bfd *));
38162271Srwatsonstatic boolean osf_core_core_file_matches_executable_p PARAMS ((bfd *, bfd *));
39162271Srwatsonstatic void swap_abort PARAMS ((void));
40162271Srwatson
41162271Srwatson/* These are stored in the bfd's tdata */
42162271Srwatson
43162271Srwatsonstruct osf_core_struct
44162271Srwatson{
45162271Srwatson  int sig;
46162271Srwatson  char cmd[MAXCOMLEN + 1];
47162271Srwatson};
48172106Srwatson
49172106Srwatson#define core_hdr(bfd) ((bfd)->tdata.osf_core_data)
50172106Srwatson#define core_signal(bfd) (core_hdr(bfd)->sig)
51172106Srwatson#define core_command(bfd) (core_hdr(bfd)->cmd)
52172106Srwatson
53172106Srwatsonstatic asection *
54162271Srwatsonmake_bfd_asection (abfd, name, flags, _raw_size, vma, filepos)
55162271Srwatson     bfd *abfd;
56172106Srwatson     const char *name;
57172106Srwatson     flagword flags;
58172106Srwatson     bfd_size_type _raw_size;
59162271Srwatson     bfd_vma vma;
60172106Srwatson     file_ptr filepos;
61172106Srwatson{
62162271Srwatson  asection *asect;
63172106Srwatson
64172106Srwatson  asect = bfd_make_section_anyway (abfd, name);
65162271Srwatson  if (!asect)
66172106Srwatson    return NULL;
67172106Srwatson
68172106Srwatson  asect->flags = flags;
69172106Srwatson  asect->_raw_size = _raw_size;
70162271Srwatson  asect->vma = vma;
71162271Srwatson  asect->filepos = filepos;
72162271Srwatson  asect->alignment_power = 8;
73172106Srwatson
74172106Srwatson  return asect;
75162271Srwatson}
76172106Srwatson
77172106Srwatsonstatic const bfd_target *
78172106Srwatsonosf_core_core_file_p (abfd)
79172106Srwatson     bfd *abfd;
80172106Srwatson{
81172106Srwatson  int val;
82172106Srwatson  int i;
83172106Srwatson  char *secname;
84172106Srwatson  struct core_filehdr core_header;
85172106Srwatson  bfd_size_type amt;
86172106Srwatson
87172106Srwatson  amt = sizeof core_header;
88172106Srwatson  val = bfd_bread ((PTR) &core_header, amt, abfd);
89172106Srwatson  if (val != sizeof core_header)
90172106Srwatson    return NULL;
91172106Srwatson
92172106Srwatson  if (strncmp (core_header.magic, "Core", 4) != 0)
93172106Srwatson    return NULL;
94172106Srwatson
95172106Srwatson  core_hdr (abfd) = (struct osf_core_struct *)
96162271Srwatson    bfd_zalloc (abfd, (bfd_size_type) sizeof (struct osf_core_struct));
97162271Srwatson  if (!core_hdr (abfd))
98    return NULL;
99
100  strncpy (core_command (abfd), core_header.name, MAXCOMLEN + 1);
101  core_signal (abfd) = core_header.signo;
102
103  for (i = 0; i < core_header.nscns; i++)
104    {
105      struct core_scnhdr core_scnhdr;
106      flagword flags;
107
108      amt = sizeof core_scnhdr;
109      val = bfd_bread ((PTR) &core_scnhdr, amt, abfd);
110      if (val != sizeof core_scnhdr)
111	break;
112
113      /* Skip empty sections.  */
114      if (core_scnhdr.size == 0 || core_scnhdr.scnptr == 0)
115	continue;
116
117      switch (core_scnhdr.scntype)
118	{
119	case SCNRGN:
120	  secname = ".data";
121	  flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
122	  break;
123	case SCNSTACK:
124	  secname = ".stack";
125	  flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
126	  break;
127	case SCNREGS:
128	  secname = ".reg";
129	  flags = SEC_HAS_CONTENTS;
130	  break;
131	default:
132	  (*_bfd_error_handler) (_("Unhandled OSF/1 core file section type %d\n"),
133				 core_scnhdr.scntype);
134	  continue;
135	}
136
137      if (!make_bfd_asection (abfd, secname, flags,
138			      (bfd_size_type) core_scnhdr.size,
139			      (bfd_vma) core_scnhdr.vaddr,
140			      (file_ptr) core_scnhdr.scnptr))
141	goto fail;
142    }
143
144  /* OK, we believe you.  You're a core file (sure, sure).  */
145
146  return abfd->xvec;
147
148 fail:
149  bfd_release (abfd, core_hdr (abfd));
150  core_hdr (abfd) = NULL;
151  bfd_section_list_clear (abfd);
152  return NULL;
153}
154
155static char *
156osf_core_core_file_failing_command (abfd)
157     bfd *abfd;
158{
159  return core_command (abfd);
160}
161
162/* ARGSUSED */
163static int
164osf_core_core_file_failing_signal (abfd)
165     bfd *abfd;
166{
167  return core_signal (abfd);
168}
169
170/* ARGSUSED */
171static boolean
172osf_core_core_file_matches_executable_p (core_bfd, exec_bfd)
173     bfd *core_bfd ATTRIBUTE_UNUSED;
174     bfd *exec_bfd ATTRIBUTE_UNUSED;
175{
176  return true;		/* FIXME, We have no way of telling at this point */
177}
178
179/* If somebody calls any byte-swapping routines, shoot them.  */
180static void
181swap_abort()
182{
183  abort(); /* This way doesn't require any declaration for ANSI to fuck up */
184}
185#define	NO_GET	((bfd_vma (*) PARAMS ((   const bfd_byte *))) swap_abort )
186#define	NO_PUT	((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
187#define	NO_SIGNED_GET \
188  ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
189
190const bfd_target osf_core_vec =
191  {
192    "osf-core",
193    bfd_target_unknown_flavour,
194    BFD_ENDIAN_BIG,		/* target byte order */
195    BFD_ENDIAN_BIG,		/* target headers byte order */
196    (HAS_RELOC | EXEC_P |	/* object flags */
197     HAS_LINENO | HAS_DEBUG |
198     HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
199    (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
200    0,			                                   /* symbol prefix */
201    ' ',						   /* ar_pad_char */
202    16,							   /* ar_max_namelen */
203    NO_GET, NO_SIGNED_GET, NO_PUT,	/* 64 bit data */
204    NO_GET, NO_SIGNED_GET, NO_PUT,	/* 32 bit data */
205    NO_GET, NO_SIGNED_GET, NO_PUT,	/* 16 bit data */
206    NO_GET, NO_SIGNED_GET, NO_PUT,	/* 64 bit hdrs */
207    NO_GET, NO_SIGNED_GET, NO_PUT,	/* 32 bit hdrs */
208    NO_GET, NO_SIGNED_GET, NO_PUT,	/* 16 bit hdrs */
209
210    {				/* bfd_check_format */
211     _bfd_dummy_target,		/* unknown format */
212     _bfd_dummy_target,		/* object file */
213     _bfd_dummy_target,		/* archive */
214     osf_core_core_file_p	/* a core file */
215    },
216    {				/* bfd_set_format */
217     bfd_false, bfd_false,
218     bfd_false, bfd_false
219    },
220    {				/* bfd_write_contents */
221     bfd_false, bfd_false,
222     bfd_false, bfd_false
223    },
224
225    BFD_JUMP_TABLE_GENERIC (_bfd_generic),
226    BFD_JUMP_TABLE_COPY (_bfd_generic),
227    BFD_JUMP_TABLE_CORE (osf_core),
228    BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
229    BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
230    BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
231    BFD_JUMP_TABLE_WRITE (_bfd_generic),
232    BFD_JUMP_TABLE_LINK (_bfd_nolink),
233    BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
234
235    NULL,
236
237    (PTR) 0			/* backend_data */
238};
239