source: trunk/src/binutils/bfd/cisco-core.c@ 229

Last change on this file since 229 was 10, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 13.0 KB
Line 
1/* BFD back-end for CISCO crash dumps.
2
3Copyright 1994, 1997, 1999, 2000 Free Software Foundation, Inc.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "libbfd.h"
24/* core_file_failing_signal returns a host signal (this probably should
25 be fixed). */
26#include <signal.h>
27
28/* for MSVC builds */
29#ifndef SIGTRAP
30# define SIGTRAP 5
31#endif
32#ifndef SIGEMT
33# define SIGEMT 6
34#endif
35#ifndef SIGBUS
36# define SIGBUS 10
37#endif
38
39
40int crash_info_locs[] = {
41 0x0250, /* mips, ppc, x86, i960 */
42 0x0400, /* m68k, mips, x86, i960 */
43 0x0FFC, /* m68k, mips, ppc, x86, i960 */
44 0x3000, /* ppc */
45 0x4FFC, /* m68k */
46 -1
47};
48
49#define CRASH_MAGIC 0xdead1234
50#define MASK_ADDR(x) ((x) & 0x0fffffff) /* Mask crash info address */
51
52typedef enum {
53 CRASH_REASON_NOTCRASHED = 0,
54 CRASH_REASON_EXCEPTION = 1,
55 CRASH_REASON_CORRUPT = 2,
56} crashreason;
57
58typedef struct {
59 char magic[4]; /* Magic number */
60 char version[4]; /* Version number */
61 char reason[4]; /* Crash reason */
62 char cpu_vector[4]; /* CPU vector for exceptions */
63 char registers[4]; /* Pointer to saved registers */
64 char rambase[4]; /* Base of RAM (not in V1 crash info) */
65 char textbase[4]; /* Base of .text section (not in V3 crash info) */
66 char database[4]; /* Base of .data section (not in V3 crash info) */
67 char bssbase[4]; /* Base of .bss section (not in V3 crash info) */
68} crashinfo_external;
69
70
71struct cisco_core_struct
72{
73 int sig;
74};
75
76
77/* Examine the file for a crash info struct at the offset given by
78 CRASH_INFO_LOC. */
79
80static const bfd_target *
81cisco_core_file_validate (abfd, crash_info_loc)
82 bfd *abfd;
83 int crash_info_loc;
84{
85 char buf[4];
86 unsigned int crashinfo_offset;
87 crashinfo_external crashinfo;
88 int nread;
89 unsigned int magic;
90 unsigned int version;
91 unsigned int rambase;
92 sec_ptr asect;
93 struct stat statbuf;
94
95 if (bfd_seek (abfd, crash_info_loc, SEEK_SET) != 0)
96 return NULL;
97
98 nread = bfd_read (buf, 1, 4, abfd);
99 if (nread != 4)
100 {
101 if (bfd_get_error () != bfd_error_system_call)
102 bfd_set_error (bfd_error_wrong_format);
103 return NULL;
104 }
105 crashinfo_offset = MASK_ADDR (bfd_get_32 (abfd, buf));
106
107 if (bfd_seek (abfd, crashinfo_offset, SEEK_SET) != 0)
108 {
109 /* Most likely we failed because of a bogus (huge) offset */
110 bfd_set_error (bfd_error_wrong_format);
111 return NULL;
112 }
113
114 nread = bfd_read (&crashinfo, 1, sizeof (crashinfo), abfd);
115 if (nread != sizeof (crashinfo))
116 {
117 if (bfd_get_error () != bfd_error_system_call)
118 bfd_set_error (bfd_error_wrong_format);
119 return NULL;
120 }
121
122 if (bfd_stat (abfd, &statbuf) < 0)
123 {
124 bfd_set_error (bfd_error_system_call);
125 return NULL;
126 }
127
128 magic = bfd_get_32 (abfd, crashinfo.magic);
129 if (magic != CRASH_MAGIC)
130 {
131 bfd_set_error (bfd_error_wrong_format);
132 return NULL;
133 }
134
135 version = bfd_get_32 (abfd, crashinfo.version);
136 if (version == 0)
137 {
138 bfd_set_error (bfd_error_wrong_format);
139 return NULL;
140 }
141 else if (version == 1)
142 {
143 /* V1 core dumps don't specify the dump base, assume 0 */
144 rambase = 0;
145 }
146 else
147 {
148 rambase = bfd_get_32 (abfd, crashinfo.rambase);
149 }
150
151 /* OK, we believe you. You're a core file. */
152
153 abfd->tdata.cisco_core_data =
154 ((struct cisco_core_struct *)
155 bfd_zmalloc (sizeof (struct cisco_core_struct)));
156 if (abfd->tdata.cisco_core_data == NULL)
157 return NULL;
158
159 switch ((crashreason) bfd_get_32 (abfd, crashinfo.reason))
160 {
161 case CRASH_REASON_NOTCRASHED:
162 /* Crash file probably came from write core. */
163 abfd->tdata.cisco_core_data->sig = 0;
164 break;
165 case CRASH_REASON_CORRUPT:
166 /* The crash context area was corrupt -- proceed with caution.
167 We have no way of passing this information back to the caller. */
168 abfd->tdata.cisco_core_data->sig = 0;
169 break;
170 case CRASH_REASON_EXCEPTION:
171 /* Crash occured due to CPU exception. */
172
173 /* This is 68k-specific; for MIPS we'll need to interpret
174 cpu_vector differently based on the target configuration
175 (since CISCO core files don't seem to have the processor
176 encoded in them). */
177
178 switch (bfd_get_32 (abfd, crashinfo.cpu_vector))
179 {
180 /* bus error */
181 case 2 : abfd->tdata.cisco_core_data->sig = SIGBUS; break;
182 /* address error */
183 case 3 : abfd->tdata.cisco_core_data->sig = SIGBUS; break;
184 /* illegal instruction */
185 case 4 : abfd->tdata.cisco_core_data->sig = SIGILL; break;
186 /* zero divide */
187 case 5 : abfd->tdata.cisco_core_data->sig = SIGFPE; break;
188 /* chk instruction */
189 case 6 : abfd->tdata.cisco_core_data->sig = SIGFPE; break;
190 /* trapv instruction */
191 case 7 : abfd->tdata.cisco_core_data->sig = SIGFPE; break;
192 /* privilege violation */
193 case 8 : abfd->tdata.cisco_core_data->sig = SIGSEGV; break;
194 /* trace trap */
195 case 9 : abfd->tdata.cisco_core_data->sig = SIGTRAP; break;
196 /* line 1010 emulator */
197 case 10: abfd->tdata.cisco_core_data->sig = SIGILL; break;
198 /* line 1111 emulator */
199 case 11: abfd->tdata.cisco_core_data->sig = SIGILL; break;
200
201 /* Coprocessor protocol violation. Using a standard MMU or FPU
202 this cannot be triggered by software. Call it a SIGBUS. */
203 case 13: abfd->tdata.cisco_core_data->sig = SIGBUS; break;
204
205 /* interrupt */
206 case 31: abfd->tdata.cisco_core_data->sig = SIGINT; break;
207 /* breakpoint */
208 case 33: abfd->tdata.cisco_core_data->sig = SIGTRAP; break;
209
210 /* floating point err */
211 case 48: abfd->tdata.cisco_core_data->sig = SIGFPE; break;
212 /* floating point err */
213 case 49: abfd->tdata.cisco_core_data->sig = SIGFPE; break;
214 /* zero divide */
215 case 50: abfd->tdata.cisco_core_data->sig = SIGFPE; break;
216 /* underflow */
217 case 51: abfd->tdata.cisco_core_data->sig = SIGFPE; break;
218 /* operand error */
219 case 52: abfd->tdata.cisco_core_data->sig = SIGFPE; break;
220 /* overflow */
221 case 53: abfd->tdata.cisco_core_data->sig = SIGFPE; break;
222 /* NAN */
223 case 54: abfd->tdata.cisco_core_data->sig = SIGFPE; break;
224 default:
225#ifndef SIGEMT
226#define SIGEMT SIGTRAP
227#endif
228 /* "software generated"*/
229 abfd->tdata.cisco_core_data->sig = SIGEMT;
230 }
231 break;
232 default:
233 /* Unknown crash reason. */
234 abfd->tdata.cisco_core_data->sig = 0;
235 break;
236 }
237
238 abfd->sections = NULL;
239 abfd->section_count = 0;
240
241 /* Create a ".reg" section to allow access to the saved
242 registers. */
243
244 asect = (asection *) bfd_zmalloc (sizeof (asection));
245 if (asect == NULL)
246 goto error_return;
247 asect->name = ".reg";
248 asect->flags = SEC_HAS_CONTENTS;
249 asect->vma = 0;
250 asect->filepos = bfd_get_32 (abfd, crashinfo.registers) - rambase;
251 /* Since we don't know the exact size of the saved register info,
252 choose a register section size that is either the remaining part
253 of the file, or 1024, whichever is smaller. */
254 nread = statbuf.st_size - asect->filepos;
255 asect->_raw_size = (nread < 1024) ? nread : 1024;
256 asect->next = abfd->sections;
257 abfd->sections = asect;
258 ++abfd->section_count;
259
260 /* Create a ".crash" section to allow access to the saved
261 crash information. */
262
263 asect = (asection *) bfd_zmalloc (sizeof (asection));
264 if (asect == NULL)
265 goto error_return;
266 asect->name = ".crash";
267 asect->flags = SEC_HAS_CONTENTS;
268 asect->vma = 0;
269 asect->filepos = crashinfo_offset;
270 asect->_raw_size = sizeof (crashinfo);
271 asect->next = abfd->sections;
272 abfd->sections = asect;
273 ++abfd->section_count;
274
275 /* Create a ".data" section that maps the entire file, which is
276 essentially a dump of the target system's RAM. */
277
278 asect = (asection *) bfd_zmalloc (sizeof (asection));
279 if (asect == NULL)
280 goto error_return;
281 asect->name = ".data";
282 asect->flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
283 /* The size of memory is the size of the core file itself. */
284 asect->_raw_size = statbuf.st_size;
285 asect->vma = rambase;
286 asect->filepos = 0;
287 asect->next = abfd->sections;
288 abfd->sections = asect;
289 ++abfd->section_count;
290
291 return abfd->xvec;
292
293 /* Get here if we have already started filling out the BFD
294 and there is an error of some kind. */
295
296 error_return:
297 {
298 sec_ptr nextsect;
299 for (asect = abfd->sections; asect != NULL;)
300 {
301 nextsect = asect->next;
302 free (asect);
303 asect = nextsect;
304 }
305 free (abfd->tdata.cisco_core_data);
306 return NULL;
307 }
308}
309
310static const bfd_target *
311cisco_core_file_p (abfd)
312 bfd *abfd;
313{
314 int *crash_info_locp;
315 const bfd_target *target = NULL;
316
317 for (crash_info_locp = crash_info_locs;
318 *crash_info_locp != -1 && target == NULL;
319 crash_info_locp++)
320 {
321 target = cisco_core_file_validate (abfd, *crash_info_locp);
322 }
323 return (target);
324}
325
326char *
327cisco_core_file_failing_command (abfd)
328 bfd *abfd;
329{
330 return NULL;
331}
332
333int
334cisco_core_file_failing_signal (abfd)
335 bfd *abfd;
336{
337 return abfd->tdata.cisco_core_data->sig;
338}
339
340boolean
341cisco_core_file_matches_executable_p (core_bfd, exec_bfd)
342 bfd *core_bfd;
343 bfd *exec_bfd;
344{
345 return true;
346}
347
348
349extern const bfd_target cisco_core_little_vec;
350
351const bfd_target cisco_core_big_vec =
352 {
353 "cisco-ios-core-big",
354 bfd_target_unknown_flavour,
355 BFD_ENDIAN_BIG, /* target byte order */
356 BFD_ENDIAN_BIG, /* target headers byte order */
357 (HAS_RELOC | EXEC_P | /* object flags */
358 HAS_LINENO | HAS_DEBUG |
359 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
360 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
361 0, /* symbol prefix */
362 ' ', /* ar_pad_char */
363 16, /* ar_max_namelen */
364 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
365 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
366 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
367 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
368 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
369 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
370
371 { /* bfd_check_format */
372 _bfd_dummy_target, /* unknown format */
373 _bfd_dummy_target, /* object file */
374 _bfd_dummy_target, /* archive */
375 cisco_core_file_p /* a core file */
376 },
377 { /* bfd_set_format */
378 bfd_false, bfd_false,
379 bfd_false, bfd_false
380 },
381 { /* bfd_write_contents */
382 bfd_false, bfd_false,
383 bfd_false, bfd_false
384 },
385
386 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
387 BFD_JUMP_TABLE_COPY (_bfd_generic),
388 BFD_JUMP_TABLE_CORE (cisco),
389 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
390 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
391 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
392 BFD_JUMP_TABLE_WRITE (_bfd_generic),
393 BFD_JUMP_TABLE_LINK (_bfd_nolink),
394 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
395
396 & cisco_core_little_vec,
397
398 (PTR) 0 /* backend_data */
399};
400
401const bfd_target cisco_core_little_vec =
402 {
403 "cisco-ios-core-little",
404 bfd_target_unknown_flavour,
405 BFD_ENDIAN_LITTLE, /* target byte order */
406 BFD_ENDIAN_LITTLE, /* target headers byte order */
407 (HAS_RELOC | EXEC_P | /* object flags */
408 HAS_LINENO | HAS_DEBUG |
409 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
410 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
411 0, /* symbol prefix */
412 ' ', /* ar_pad_char */
413 16, /* ar_max_namelen */
414 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
415 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
416 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
417 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
418 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
419 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
420
421 { /* bfd_check_format */
422 _bfd_dummy_target, /* unknown format */
423 _bfd_dummy_target, /* object file */
424 _bfd_dummy_target, /* archive */
425 cisco_core_file_p /* a core file */
426 },
427 { /* bfd_set_format */
428 bfd_false, bfd_false,
429 bfd_false, bfd_false
430 },