Add support for 64bit NETBSD cores
[external/binutils.git] / bfd / netbsd-core.c
1 /* BFD back end for NetBSD style core files
2    Copyright 1988, 1989, 1991, 1992, 1993, 1996, 1998, 1999, 2000, 2001,
3    2002, 2004
4    Free Software Foundation, Inc.
5    Written by Paul Kranenburg, EUR
6
7    This file is part of BFD, the Binary File Descriptor library.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "libaout.h"           /* BFD a.out internal data structures.  */
27
28 #include <sys/param.h>
29 #include <sys/dir.h>
30 #include <signal.h>
31 #include <sys/core.h>
32
33 /* The machine ID for OpenBSD/sparc64 and older versions of
34    NetBSD/sparc64 overlaps with M_MIPS1.  */
35 #define M_SPARC64_OPENBSD       M_MIPS1
36
37 /* FIXME: On NetBSD/sparc CORE_FPU_OFFSET should be (sizeof (struct trapframe)).  */
38
39 struct netbsd_core_struct
40 {
41   struct core core;
42 } *rawptr;
43
44 /* Forward declarations.  */
45
46 static const bfd_target *netbsd_core_file_p
47   PARAMS ((bfd *abfd));
48 static char *netbsd_core_file_failing_command
49   PARAMS ((bfd *abfd));
50 static int netbsd_core_file_failing_signal
51   PARAMS ((bfd *abfd));
52 static bfd_boolean netbsd_core_file_matches_executable_p
53   PARAMS ((bfd *core_bfd, bfd *exec_bfd));
54 static void swap_abort
55   PARAMS ((void));
56
57 /* Handle NetBSD-style core dump file.  */
58
59 static const bfd_target *
60 netbsd_core_file_p (abfd)
61      bfd *abfd;
62
63 {
64   int i, val;
65   file_ptr offset;
66   asection *asect, *asect2;
67   struct core core;
68   struct coreseg coreseg;
69   bfd_size_type amt = sizeof core;
70
71   val = bfd_bread ((void *) &core, amt, abfd);
72   if (val != sizeof core)
73     {
74       /* Too small to be a core file.  */
75       bfd_set_error (bfd_error_wrong_format);
76       return 0;
77     }
78
79   if (CORE_GETMAGIC (core) != COREMAGIC)
80     {
81       bfd_set_error (bfd_error_wrong_format);
82       return 0;
83     }
84
85   amt = sizeof (struct netbsd_core_struct);
86   rawptr = (struct netbsd_core_struct *) bfd_zalloc (abfd, amt);
87   if (rawptr == NULL)
88     return 0;
89
90   rawptr->core = core;
91   abfd->tdata.netbsd_core_data = rawptr;
92
93   offset = core.c_hdrsize;
94   for (i = 0; i < core.c_nseg; i++)
95     {
96       const char *sname;
97       flagword flags;
98
99       if (bfd_seek (abfd, offset, SEEK_SET) != 0)
100         goto punt;
101
102       val = bfd_bread ((void *) &coreseg, (bfd_size_type) sizeof coreseg, abfd);
103       if (val != sizeof coreseg)
104         {
105           bfd_set_error (bfd_error_file_truncated);
106           goto punt;
107         }
108       if (CORE_GETMAGIC (coreseg) != CORESEGMAGIC)
109         {
110           bfd_set_error (bfd_error_wrong_format);
111           goto punt;
112         }
113
114       offset += core.c_seghdrsize;
115
116       switch (CORE_GETFLAG (coreseg))
117         {
118         case CORE_CPU:
119           sname = ".reg";
120           flags = SEC_ALLOC + SEC_HAS_CONTENTS;
121           break;
122         case CORE_DATA:
123           sname = ".data";
124           flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
125           break;
126         case CORE_STACK:
127           sname = ".stack";
128           flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
129           break;
130         default:
131           sname = ".unknown";
132           flags = SEC_ALLOC + SEC_HAS_CONTENTS;
133           break;
134         }
135       asect = bfd_make_section_anyway (abfd, sname);
136       if (asect == NULL)
137         goto punt;
138
139       asect->flags = flags;
140       asect->_raw_size = coreseg.c_size;
141       asect->vma = coreseg.c_addr;
142       asect->filepos = offset;
143       asect->alignment_power = 2;
144
145       offset += coreseg.c_size;
146
147 #ifdef CORE_FPU_OFFSET
148       switch (CORE_GETFLAG (coreseg))
149         {
150         case CORE_CPU:
151           /* Hackish...  */
152           asect->_raw_size = CORE_FPU_OFFSET;
153           asect2 = bfd_make_section_anyway (abfd, ".reg2");
154           if (asect2 == NULL)
155             goto punt;
156           asect2->_raw_size = coreseg.c_size - CORE_FPU_OFFSET;
157           asect2->vma = 0;
158           asect2->filepos = asect->filepos + CORE_FPU_OFFSET;
159           asect2->alignment_power = 2;
160           asect2->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
161           break;
162         }
163 #endif
164     }
165
166  /* Set architecture from machine ID.  */
167  switch (CORE_GETMID (core))
168    {
169    case M_X86_64_NETBSD:
170      bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
171      break;
172
173    case M_386_NETBSD:
174      bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386);
175      break;
176
177    case M_SPARC_NETBSD:
178      bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
179      break;
180
181    case M_SPARC64_NETBSD:
182    case M_SPARC64_OPENBSD:
183      bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc_v9);
184      break;
185    }
186  
187   /* OK, we believe you.  You're a core file (sure, sure).  */
188   return abfd->xvec;
189
190  punt:
191   bfd_release (abfd, abfd->tdata.any);
192   abfd->tdata.any = NULL;
193   bfd_section_list_clear (abfd);
194   return 0;
195 }
196
197 static char*
198 netbsd_core_file_failing_command (abfd)
199         bfd *abfd;
200 {
201  /*return core_command (abfd);*/
202   return abfd->tdata.netbsd_core_data->core.c_name;
203 }
204
205 static int
206 netbsd_core_file_failing_signal (abfd)
207         bfd *abfd;
208 {
209   /*return core_signal (abfd);*/
210   return abfd->tdata.netbsd_core_data->core.c_signo;
211 }
212
213 static bfd_boolean
214 netbsd_core_file_matches_executable_p  (core_bfd, exec_bfd)
215      bfd *core_bfd ATTRIBUTE_UNUSED;
216      bfd *exec_bfd ATTRIBUTE_UNUSED;
217 {
218   /* FIXME, We have no way of telling at this point.  */
219   return TRUE;
220 }
221 \f
222 /* If somebody calls any byte-swapping routines, shoot them.  */
223
224 static void
225 swap_abort ()
226 {
227  /* This way doesn't require any declaration for ANSI to fuck up.  */
228   abort ();
229 }
230
231 #define NO_GET  ((bfd_vma (*) PARAMS ((   const bfd_byte *))) swap_abort )
232 #define NO_PUT  ((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
233 #define NO_SIGNED_GET \
234   ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
235
236 const bfd_target netbsd_core_vec =
237   {
238     "netbsd-core",
239     bfd_target_unknown_flavour,
240     BFD_ENDIAN_UNKNOWN,         /* Target byte order.  */
241     BFD_ENDIAN_UNKNOWN,         /* Target headers byte order.  */
242     (HAS_RELOC | EXEC_P |       /* Object flags.  */
243      HAS_LINENO | HAS_DEBUG |
244      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
245     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags.  */
246     0,                                                     /* Symbol prefix.  */
247     ' ',                                                   /* ar_pad_char.  */
248     16,                                                    /* ar_max_namelen.  */
249     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit data.  */
250     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit data.  */
251     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit data.  */
252     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit hdrs.  */
253     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit hdrs.  */
254     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit hdrs.  */
255
256     {                                   /* bfd_check_format.  */
257      _bfd_dummy_target,                 /* Unknown format.  */
258      _bfd_dummy_target,                 /* Object file.  */
259      _bfd_dummy_target,                 /* Archive.  */
260      netbsd_core_file_p                 /* A core file.  */
261     },
262     {                                   /* bfd_set_format.  */
263      bfd_false, bfd_false,
264      bfd_false, bfd_false
265     },
266     {                                   /* bfd_write_contents.  */
267      bfd_false, bfd_false,
268      bfd_false, bfd_false
269     },
270
271        BFD_JUMP_TABLE_GENERIC (_bfd_generic),
272        BFD_JUMP_TABLE_COPY (_bfd_generic),
273        BFD_JUMP_TABLE_CORE (netbsd),
274        BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
275        BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
276        BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
277        BFD_JUMP_TABLE_WRITE (_bfd_generic),
278        BFD_JUMP_TABLE_LINK (_bfd_nolink),
279        BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
280
281     NULL,
282
283     (PTR) 0                             /* Backend_data.  */
284 };