Wed Mar 30 16:25:41 1994 Ian Lance Taylor (ian@tweedledumb.cygnus.com)
[external/binutils.git] / bfd / cisco-core.c
1 /* BFD back-end for CISCO crash dumps.
2
3 Copyright 1994 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 \f
28 #define CRASH_INFO (0xffc)
29 #define CRASH_MAGIC 0xdead1234
30
31 typedef enum {
32     CRASH_REASON_NOTCRASHED = 0,
33     CRASH_REASON_EXCEPTION = 1,
34     CRASH_REASON_CORRUPT = 2,
35   } crashreason;
36
37 struct crashinfo_external
38 {
39   char magic[4];                        /* Magic number */
40   char version[4];              /* Version number */
41   char reason[4];               /* Crash reason */
42   char cpu_vector[4];           /* CPU vector for exceptions */
43   char registers[4];            /* Pointer to saved registers */
44   char rambase[4];              /* Base of RAM (not in V1 crash info) */
45 };
46 \f
47 struct cisco_core_struct
48 {
49   int sig;
50 };
51 \f
52 static bfd_target *
53 cisco_core_file_p (abfd)
54      bfd *abfd;
55 {
56   char buf[4];
57   unsigned int crashinfo_offset;
58   struct crashinfo_external crashinfo;
59   int nread;
60   unsigned int rambase;
61   sec_ptr asect;
62   struct stat statbuf;
63
64   if (bfd_seek (abfd, CRASH_INFO, SEEK_SET) != 0)
65     return NULL;
66
67   nread = bfd_read (buf, 1, 4, abfd);
68   if (nread != 4)
69     {
70       /* Maybe the file is too small (FIXME: what about other errors).  */
71       bfd_set_error (bfd_error_wrong_format);
72       return NULL;
73     }
74   crashinfo_offset = bfd_get_32 (abfd, buf);
75
76   if (bfd_seek (abfd, crashinfo_offset, SEEK_SET) != 0)
77     return NULL;
78
79   nread = bfd_read (&crashinfo, 1, sizeof (crashinfo), abfd);
80   if (nread != sizeof (crashinfo))
81     {
82       /* Maybe the file is too small (FIXME: what about other errors).  */
83       bfd_set_error (bfd_error_wrong_format);
84       return NULL;
85     }
86
87   if (bfd_stat (abfd, &statbuf) < 0)
88     {
89       bfd_set_error (bfd_error_system_call);
90       return NULL;
91     }
92
93   if (bfd_get_32 (abfd, crashinfo.magic) != CRASH_MAGIC)
94     {
95       bfd_set_error (bfd_error_wrong_format);
96       return NULL;
97     }
98
99   switch (bfd_get_32 (abfd, crashinfo.version))
100     {
101     case 0:
102       bfd_set_error (bfd_error_wrong_format);
103       return NULL;
104     case 1:
105       rambase = 0;
106       break;
107     default:
108     case 2:
109       rambase = bfd_get_32 (abfd, crashinfo.rambase);
110       break;
111     }
112
113   /* OK, we believe you.  You're a core file.  */
114
115   abfd->tdata.cisco_core_data =
116     ((struct cisco_core_struct *)
117      bfd_zmalloc (sizeof (struct cisco_core_struct)));
118   if (abfd->tdata.cisco_core_data == NULL)
119     {
120       bfd_set_error (bfd_error_no_memory);
121       return NULL;
122     }
123
124   switch ((crashreason) bfd_get_32 (abfd, crashinfo.reason))
125     {
126     case CRASH_REASON_NOTCRASHED:
127       /* Crash file probably came from write core.  */
128       abfd->tdata.cisco_core_data->sig = 0;
129       break;
130     case CRASH_REASON_CORRUPT:
131       /* The crash context area was corrupt -- proceed with caution.
132          We have no way of passing this information back to the caller.  */
133       abfd->tdata.cisco_core_data->sig = 0;
134       break;
135     case CRASH_REASON_EXCEPTION:
136       /* Crash occured due to CPU exception.  */
137
138       /* This is 68k-specific; for MIPS we'll need to interpret
139          cpu_vector differently based on the target configuration
140          (since CISCO core files don't seem to have the processor
141          encoded in them).  */
142
143       switch (bfd_get_32 (abfd, crashinfo.cpu_vector))
144         {
145            /* bus error           */
146         case 2 : abfd->tdata.cisco_core_data->sig = SIGBUS; break;
147            /* address error       */
148         case 3 : abfd->tdata.cisco_core_data->sig = SIGBUS; break;
149            /* illegal instruction */
150         case 4 : abfd->tdata.cisco_core_data->sig = SIGILL;  break;
151            /* zero divide         */
152         case 5 : abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
153            /* chk instruction     */
154         case 6 : abfd->tdata.cisco_core_data->sig = SIGFPE; break;
155            /* trapv instruction   */
156         case 7 : abfd->tdata.cisco_core_data->sig = SIGFPE; break;
157            /* privilege violation */
158         case 8 : abfd->tdata.cisco_core_data->sig = SIGSEGV; break;
159            /* trace trap          */
160         case 9 : abfd->tdata.cisco_core_data->sig = SIGTRAP;  break;
161            /* line 1010 emulator  */
162         case 10: abfd->tdata.cisco_core_data->sig = SIGILL;  break;
163            /* line 1111 emulator  */
164         case 11: abfd->tdata.cisco_core_data->sig = SIGILL;  break;
165
166           /* Coprocessor protocol violation.  Using a standard MMU or FPU
167              this cannot be triggered by software.  Call it a SIGBUS.  */
168         case 13: abfd->tdata.cisco_core_data->sig = SIGBUS;  break;
169
170           /* interrupt           */
171         case 31: abfd->tdata.cisco_core_data->sig = SIGINT;  break;
172           /* breakpoint          */
173         case 33: abfd->tdata.cisco_core_data->sig = SIGTRAP;  break;
174
175           /* floating point err  */
176         case 48: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
177           /* floating point err  */
178         case 49: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
179           /* zero divide         */
180         case 50: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
181           /* underflow           */
182         case 51: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
183           /* operand error       */
184         case 52: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
185            /* overflow            */
186         case 53: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
187           /* NAN                 */
188         case 54: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
189         default:
190           /* "software generated"*/
191           abfd->tdata.cisco_core_data->sig = SIGEMT;
192         }
193       break;
194     default:
195       /* Unknown crash reason.  */
196       abfd->tdata.cisco_core_data->sig = 0;
197       break;
198     }
199
200   abfd->sections = NULL;
201   abfd->section_count = 0;
202
203   asect = (asection *) bfd_zmalloc (sizeof (asection));
204   if (asect == NULL)
205     {
206       bfd_set_error (bfd_error_no_memory);
207       goto error_return;
208     }
209   asect->name = ".reg";
210   asect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
211   /* This can be bigger than the real size.  Set it to the size of the whole
212      core file.  */
213   asect->_raw_size = statbuf.st_size;
214   asect->vma = 0;
215   asect->filepos = bfd_get_32 (abfd, crashinfo.registers) - rambase;
216   asect->next = abfd->sections;
217   abfd->sections = asect;
218   ++abfd->section_count;
219
220   /* There is only one section containing data from the target system's RAM.
221      We call it .data.  */
222   asect = (asection *) bfd_zmalloc (sizeof (asection));
223   if (asect == NULL)
224     {
225       bfd_set_error (bfd_error_no_memory);
226       goto error_return;
227     }
228   asect->name = ".data";
229   asect->flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
230   /* The size of memory is the size of the core file itself.  */
231   asect->_raw_size = statbuf.st_size;
232   asect->vma = rambase;
233   asect->filepos = 0;
234   asect->next = abfd->sections;
235   abfd->sections = asect;
236   ++abfd->section_count;
237
238   return abfd->xvec;
239
240  error_return:
241   {
242     sec_ptr nextsect;
243     for (asect = abfd->sections; asect != NULL;)
244       {
245         nextsect = asect->next;
246         free (asect);
247         asect = nextsect;
248       }
249     free (abfd->tdata.cisco_core_data);
250     return NULL;
251   }
252 }
253
254 char *
255 cisco_core_file_failing_command (abfd)
256      bfd *abfd;
257 {
258   return NULL;
259 }
260
261 int
262 cisco_core_file_failing_signal (abfd)
263      bfd *abfd;
264 {
265   return abfd->tdata.cisco_core_data->sig;
266 }
267
268 boolean
269 cisco_core_file_matches_executable_p (core_bfd, exec_bfd)
270      bfd *core_bfd;
271      bfd *exec_bfd;
272 {
273   return true;
274 }
275 \f
276 /* No archive file support via this BFD */
277 #define cisco_openr_next_archived_file  bfd_generic_openr_next_archived_file
278 #define cisco_generic_stat_arch_elt             bfd_generic_stat_arch_elt
279 #define cisco_slurp_armap                       bfd_false
280 #define cisco_slurp_extended_name_table bfd_true
281 #define cisco_write_armap                       (boolean (*) PARAMS     \
282     ((bfd *arch, unsigned int elength, struct orl *map, \
283       unsigned int orl_count, int stridx))) bfd_false
284 #define cisco_truncate_arname           bfd_dont_truncate_arname
285 #define aout_32_openr_next_archived_file        bfd_generic_openr_next_archived_file
286
287 #define cisco_close_and_cleanup         bfd_generic_close_and_cleanup
288 #define cisco_set_section_contents              (boolean (*) PARAMS     \
289         ((bfd *abfd, asection *section, PTR data, file_ptr offset,      \
290         bfd_size_type count))) bfd_generic_set_section_contents
291 #define cisco_get_section_contents              bfd_generic_get_section_contents
292 #define cisco_new_section_hook          (boolean (*) PARAMS     \
293         ((bfd *, sec_ptr))) bfd_true
294 #define cisco_get_symtab_upper_bound    bfd_0l
295 #define cisco_get_symtab                        (long (*) PARAMS \
296         ((bfd *, struct symbol_cache_entry **))) bfd_0l
297 #define cisco_get_reloc_upper_bound             (long (*) PARAMS \
298         ((bfd *, sec_ptr))) bfd_0l
299 #define cisco_canonicalize_reloc                (long (*) PARAMS \
300         ((bfd *, sec_ptr, arelent **, struct symbol_cache_entry**))) bfd_0l
301 #define cisco_make_empty_symbol         (struct symbol_cache_entry * \
302         (*) PARAMS ((bfd *))) bfd_false
303 #define cisco_print_symbol                      (void (*) PARAMS        \
304         ((bfd *, PTR, struct symbol_cache_entry  *,                     \
305         bfd_print_symbol_type))) bfd_false
306 #define cisco_get_symbol_info           (void (*) PARAMS        \
307         ((bfd *, struct symbol_cache_entry  *,                  \
308         symbol_info *))) bfd_false
309 #define cisco_get_lineno                        (alent * (*) PARAMS     \
310         ((bfd *, struct symbol_cache_entry *))) bfd_nullvoidptr
311 #define cisco_set_arch_mach                     (boolean (*) PARAMS     \
312         ((bfd *, enum bfd_architecture, unsigned long))) bfd_false
313 #define cisco_find_nearest_line         (boolean (*) PARAMS     \
314         ((bfd *abfd, struct sec  *section,                              \
315          struct symbol_cache_entry  **symbols,bfd_vma offset,           \
316          CONST char **file, CONST char **func, unsigned int *line))) bfd_false
317 #define cisco_sizeof_headers            (int (*) PARAMS \
318         ((bfd *, boolean))) bfd_0
319
320 #define cisco_bfd_debug_info_start              bfd_void
321 #define cisco_bfd_debug_info_end                bfd_void
322 #define cisco_bfd_debug_info_accumulate (void (*) PARAMS        \
323         ((bfd *, struct sec *))) bfd_void
324 #define cisco_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
325 #define cisco_bfd_relax_section         bfd_generic_relax_section
326 #define cisco_bfd_reloc_type_lookup \
327   ((CONST struct reloc_howto_struct *(*) PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr)
328 #define cisco_bfd_make_debug_symbol \
329   ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
330 #define cisco_bfd_link_hash_table_create \
331   ((struct bfd_link_hash_table *(*) PARAMS ((bfd *))) bfd_nullvoidptr)
332 #define cisco_bfd_link_add_symbols \
333   ((boolean (*) PARAMS ((bfd *, struct bfd_link_info *))) bfd_false)
334 #define cisco_bfd_final_link \
335   ((boolean (*) PARAMS ((bfd *, struct bfd_link_info *))) bfd_false)
336 #define cisco_bfd_copy_private_section_data \
337   ((boolean (*) PARAMS ((bfd *, asection *, bfd *, asection *))) bfd_false)
338 #define cisco_bfd_copy_private_bfd_data \
339   ((boolean (*) PARAMS ((bfd *, bfd *))) bfd_false)
340 #define cisco_bfd_is_local_label \
341   ((boolean (*) PARAMS ((bfd *, asymbol *))) bfd_false)
342
343 bfd_target cisco_core_vec =
344   {
345     "trad-core",
346     bfd_target_unknown_flavour,
347     true,                       /* target byte order */
348     true,                       /* target headers byte order */
349     (HAS_RELOC | EXEC_P |       /* object flags */
350      HAS_LINENO | HAS_DEBUG |
351      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
352     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
353     0,                                                     /* symbol prefix */
354     ' ',                                                   /* ar_pad_char */
355     16,                                                    /* ar_max_namelen */
356     3,                                                     /* minimum alignment power */
357     bfd_getb64, bfd_getb_signed_64, bfd_putb64,
358     bfd_getb32, bfd_getb_signed_32, bfd_putb32,
359     bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
360     bfd_getb64, bfd_getb_signed_64, bfd_putb64,
361     bfd_getb32, bfd_getb_signed_32, bfd_putb32,
362     bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
363
364     {                           /* bfd_check_format */
365      _bfd_dummy_target,         /* unknown format */
366      _bfd_dummy_target,         /* object file */
367      _bfd_dummy_target,         /* archive */
368      cisco_core_file_p  /* a core file */
369     },
370     {                           /* bfd_set_format */
371      bfd_false, bfd_false,
372      bfd_false, bfd_false
373     },
374     {                           /* bfd_write_contents */
375      bfd_false, bfd_false,
376      bfd_false, bfd_false
377     },
378     
379     JUMP_TABLE(cisco),
380     (PTR) 0                     /* backend_data */
381 };