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