2001-03-11 Philip Blundell <philb@gnu.org>
[binutils.git] / bfd / netbsd-core.c
blobc93d6a4dc226a2f40b780cb3364b7165805d6c2b
1 /* BFD back end for NetBSD style core files
2 Copyright 1988, 1989, 1991, 1992, 1993, 1996, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4 Written by Paul Kranenburg, EUR
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "libaout.h" /* BFD a.out internal data structures */
27 #include <sys/param.h>
28 #include <sys/dir.h>
29 #include <signal.h>
30 #include <sys/core.h>
33 * FIXME: On NetBSD/sparc CORE_FPU_OFFSET should be (sizeof (struct trapframe))
36 struct netbsd_core_struct {
37 struct core core;
38 } *rawptr;
40 /* forward declarations */
42 static const bfd_target * netbsd_core_file_p PARAMS ((bfd *abfd));
43 static char * netbsd_core_file_failing_command PARAMS ((bfd *abfd));
44 static int netbsd_core_file_failing_signal PARAMS ((bfd *abfd));
45 static boolean netbsd_core_file_matches_executable_p
46 PARAMS ((bfd *core_bfd, bfd *exec_bfd));
47 static void swap_abort PARAMS ((void));
49 /* Handle NetBSD-style core dump file. */
51 /* ARGSUSED */
52 static const bfd_target *
53 netbsd_core_file_p (abfd)
54 bfd *abfd;
57 int i, val, offset;
58 asection *asect, *asect2;
59 struct core core;
60 struct coreseg coreseg;
62 val = bfd_read ((void *)&core, 1, sizeof core, abfd);
63 if (val != sizeof core) {
64 /* Too small to be a core file */
65 bfd_set_error(bfd_error_wrong_format);
66 return 0;
69 if (CORE_GETMAGIC(core) != COREMAGIC) {
70 bfd_set_error(bfd_error_wrong_format);
71 return 0;
74 rawptr = (struct netbsd_core_struct *)
75 bfd_zalloc (abfd, sizeof (struct netbsd_core_struct));
76 if (rawptr == NULL) {
77 bfd_set_error(bfd_error_no_memory);
78 return 0;
81 rawptr->core = core;
82 abfd->tdata.netbsd_core_data = rawptr;
84 offset = core.c_hdrsize;
85 for (i = 0; i < core.c_nseg; i++) {
87 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
88 goto punt;
90 val = bfd_read ((void *)&coreseg, 1, sizeof coreseg, abfd);
91 if (val != sizeof coreseg) {
92 bfd_set_error(bfd_error_file_truncated);
93 goto punt;
95 if (CORE_GETMAGIC(coreseg) != CORESEGMAGIC) {
96 bfd_set_error(bfd_error_wrong_format);
97 goto punt;
100 offset += core.c_seghdrsize;
102 asect = (asection *) bfd_zalloc (abfd, sizeof (asection));
103 if (asect == NULL) {
104 bfd_set_error(bfd_error_no_memory);
105 goto punt;
108 asect->_raw_size = coreseg.c_size;
109 asect->vma = coreseg.c_addr;
110 asect->filepos = offset;
111 asect->alignment_power = 2;
112 asect->next = abfd->sections;
113 abfd->sections = asect;
114 abfd->section_count++;
115 offset += coreseg.c_size;
117 switch (CORE_GETFLAG(coreseg)) {
118 case CORE_CPU:
119 asect->name = ".reg";
120 asect->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
121 #ifdef CORE_FPU_OFFSET
122 /* Hackish... */
123 asect->_raw_size = CORE_FPU_OFFSET;
124 asect2 = (asection *)bfd_zalloc (abfd,
125 sizeof (asection));
126 if (asect2 == NULL) {
127 bfd_set_error(bfd_error_no_memory);
128 goto punt;
130 asect2->_raw_size = coreseg.c_size - CORE_FPU_OFFSET;
131 asect2->vma = 0;
132 asect2->filepos = asect->filepos + CORE_FPU_OFFSET;
133 asect2->alignment_power = 2;
134 asect2->next = abfd->sections;
135 asect2->name = ".reg2";
136 asect2->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
137 abfd->sections = asect2;
138 abfd->section_count++;
139 #endif
141 break;
142 case CORE_DATA:
143 asect->name = ".data";
144 asect->flags = SEC_ALLOC+SEC_LOAD+SEC_HAS_CONTENTS;
145 break;
146 case CORE_STACK:
147 asect->name = ".stack";
148 asect->flags = SEC_ALLOC+SEC_LOAD+SEC_HAS_CONTENTS;
149 break;
153 /* OK, we believe you. You're a core file (sure, sure). */
154 return abfd->xvec;
156 punt: {
157 asection *anext;
158 for (asect = abfd->sections; asect; asect = anext) {
159 anext = asect->next;
160 free((void *)asect);
163 free ((void *)rawptr);
164 abfd->tdata.netbsd_core_data = NULL;
165 abfd->sections = NULL;
166 abfd->section_count = 0;
167 return 0;
170 static char*
171 netbsd_core_file_failing_command (abfd)
172 bfd *abfd;
174 /*return core_command (abfd);*/
175 return abfd->tdata.netbsd_core_data->core.c_name;
178 /* ARGSUSED */
179 static int
180 netbsd_core_file_failing_signal (abfd)
181 bfd *abfd;
183 /*return core_signal (abfd);*/
184 return abfd->tdata.netbsd_core_data->core.c_signo;
187 /* ARGSUSED */
188 static boolean
189 netbsd_core_file_matches_executable_p (core_bfd, exec_bfd)
190 bfd *core_bfd, *exec_bfd;
192 return true; /* FIXME, We have no way of telling at this point */
195 /* If somebody calls any byte-swapping routines, shoot them. */
196 static void
197 swap_abort ()
199 abort (); /* This way doesn't require any declaration for ANSI to fuck up */
201 #define NO_GET ((bfd_vma (*) PARAMS (( const bfd_byte *))) swap_abort )
202 #define NO_PUT ((void (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
203 #define NO_SIGNED_GET \
204 ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
206 const bfd_target netbsd_core_vec =
208 "netbsd-core",
209 bfd_target_unknown_flavour,
210 BFD_ENDIAN_UNKNOWN, /* target byte order */
211 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
212 (HAS_RELOC | EXEC_P | /* object flags */
213 HAS_LINENO | HAS_DEBUG |
214 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
215 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
216 0, /* symbol prefix */
217 ' ', /* ar_pad_char */
218 16, /* ar_max_namelen */
219 NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit data */
220 NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit data */
221 NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit data */
222 NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit hdrs */
223 NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit hdrs */
224 NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit hdrs */
226 { /* bfd_check_format */
227 _bfd_dummy_target, /* unknown format */
228 _bfd_dummy_target, /* object file */
229 _bfd_dummy_target, /* archive */
230 netbsd_core_file_p /* a core file */
232 { /* bfd_set_format */
233 bfd_false, bfd_false,
234 bfd_false, bfd_false
236 { /* bfd_write_contents */
237 bfd_false, bfd_false,
238 bfd_false, bfd_false
241 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
242 BFD_JUMP_TABLE_COPY (_bfd_generic),
243 BFD_JUMP_TABLE_CORE (netbsd),
244 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
245 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
246 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
247 BFD_JUMP_TABLE_WRITE (_bfd_generic),
248 BFD_JUMP_TABLE_LINK (_bfd_nolink),
249 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
251 NULL,
253 (PTR) 0 /* backend_data */