Add support for 64bit NETBSD cores
[binutils.git] / bfd / netbsd-core.c
blobadadead8d10f3e697e01a8d5d24294f264f5cf47
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
7 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "libaout.h" /* BFD a.out internal data structures. */
28 #include <sys/param.h>
29 #include <sys/dir.h>
30 #include <signal.h>
31 #include <sys/core.h>
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
37 /* FIXME: On NetBSD/sparc CORE_FPU_OFFSET should be (sizeof (struct trapframe)). */
39 struct netbsd_core_struct
41 struct core core;
42 } *rawptr;
44 /* Forward declarations. */
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));
57 /* Handle NetBSD-style core dump file. */
59 static const bfd_target *
60 netbsd_core_file_p (abfd)
61 bfd *abfd;
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;
71 val = bfd_bread ((void *) &core, amt, abfd);
72 if (val != sizeof core)
74 /* Too small to be a core file. */
75 bfd_set_error (bfd_error_wrong_format);
76 return 0;
79 if (CORE_GETMAGIC (core) != COREMAGIC)
81 bfd_set_error (bfd_error_wrong_format);
82 return 0;
85 amt = sizeof (struct netbsd_core_struct);
86 rawptr = (struct netbsd_core_struct *) bfd_zalloc (abfd, amt);
87 if (rawptr == NULL)
88 return 0;
90 rawptr->core = core;
91 abfd->tdata.netbsd_core_data = rawptr;
93 offset = core.c_hdrsize;
94 for (i = 0; i < core.c_nseg; i++)
96 const char *sname;
97 flagword flags;
99 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
100 goto punt;
102 val = bfd_bread ((void *) &coreseg, (bfd_size_type) sizeof coreseg, abfd);
103 if (val != sizeof coreseg)
105 bfd_set_error (bfd_error_file_truncated);
106 goto punt;
108 if (CORE_GETMAGIC (coreseg) != CORESEGMAGIC)
110 bfd_set_error (bfd_error_wrong_format);
111 goto punt;
114 offset += core.c_seghdrsize;
116 switch (CORE_GETFLAG (coreseg))
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;
135 asect = bfd_make_section_anyway (abfd, sname);
136 if (asect == NULL)
137 goto punt;
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;
145 offset += coreseg.c_size;
147 #ifdef CORE_FPU_OFFSET
148 switch (CORE_GETFLAG (coreseg))
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;
163 #endif
166 /* Set architecture from machine ID. */
167 switch (CORE_GETMID (core))
169 case M_X86_64_NETBSD:
170 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
171 break;
173 case M_386_NETBSD:
174 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386);
175 break;
177 case M_SPARC_NETBSD:
178 bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
179 break;
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;
187 /* OK, we believe you. You're a core file (sure, sure). */
188 return abfd->xvec;
190 punt:
191 bfd_release (abfd, abfd->tdata.any);
192 abfd->tdata.any = NULL;
193 bfd_section_list_clear (abfd);
194 return 0;
197 static char*
198 netbsd_core_file_failing_command (abfd)
199 bfd *abfd;
201 /*return core_command (abfd);*/
202 return abfd->tdata.netbsd_core_data->core.c_name;
205 static int
206 netbsd_core_file_failing_signal (abfd)
207 bfd *abfd;
209 /*return core_signal (abfd);*/
210 return abfd->tdata.netbsd_core_data->core.c_signo;
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;
218 /* FIXME, We have no way of telling at this point. */
219 return TRUE;
222 /* If somebody calls any byte-swapping routines, shoot them. */
224 static void
225 swap_abort ()
227 /* This way doesn't require any declaration for ANSI to fuck up. */
228 abort ();
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 )
236 const bfd_target netbsd_core_vec =
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. */
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. */
262 { /* bfd_set_format. */
263 bfd_false, bfd_false,
264 bfd_false, bfd_false
266 { /* bfd_write_contents. */
267 bfd_false, bfd_false,
268 bfd_false, bfd_false
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),
281 NULL,
283 (PTR) 0 /* Backend_data. */