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