* cisco-core.c: New file.
[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 = (struct cisco_core_struct *)
116     bfd_zmalloc (abfd, sizeof (struct cisco_core_struct));
117   if (abfd->tdata.cisco_core_data == NULL)
118     {
119       bfd_set_error (bfd_error_no_memory);
120       return NULL;
121     }
122
123   switch ((crashreason) bfd_get_32 (abfd, crashinfo.reason))
124     {
125     case CRASH_REASON_NOTCRASHED:
126       /* Crash file probably came from write core.  */
127       abfd->tdata.cisco_core_data->sig = 0;
128       break;
129     case CRASH_REASON_CORRUPT:
130       /* The crash context area was corrupt -- proceed with caution.
131          We have no way of passing this information back to the caller.  */
132       abfd->tdata.cisco_core_data->sig = 0;
133       break;
134     case CRASH_REASON_EXCEPTION:
135       /* Crash occured due to CPU exception.  */
136
137       /* This is 68k-specific; for MIPS we'll need to interpret
138          cpu_vector differently based on the target configuration
139          (since CISCO core files don't seem to have the processor
140          encoded in them).  */
141
142       switch (bfd_get_32 (abfd, crashinfo.cpu_vector))
143         {
144            /* bus error           */
145         case 2 : abfd->tdata.cisco_core_data->sig = SIGBUS; break;
146            /* address error       */
147         case 3 : abfd->tdata.cisco_core_data->sig = SIGBUS; break;
148            /* illegal instruction */
149         case 4 : abfd->tdata.cisco_core_data->sig = SIGILL;  break;
150            /* zero divide         */
151         case 5 : abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
152            /* chk instruction     */
153         case 6 : abfd->tdata.cisco_core_data->sig = SIGFPE; break;
154            /* trapv instruction   */
155         case 7 : abfd->tdata.cisco_core_data->sig = SIGFPE; break;
156            /* privilege violation */
157         case 8 : abfd->tdata.cisco_core_data->sig = SIGSEGV; break;
158            /* trace trap          */
159         case 9 : abfd->tdata.cisco_core_data->sig = SIGTRAP;  break;
160            /* line 1010 emulator  */
161         case 10: abfd->tdata.cisco_core_data->sig = SIGILL;  break;
162            /* line 1111 emulator  */
163         case 11: abfd->tdata.cisco_core_data->sig = SIGILL;  break;
164
165           /* Coprocessor protocol violation.  Using a standard MMU or FPU
166              this cannot be triggered by software.  Call it a SIGBUS.  */
167         case 13: abfd->tdata.cisco_core_data->sig = SIGBUS;  break;
168
169           /* interrupt           */
170         case 31: abfd->tdata.cisco_core_data->sig = SIGINT;  break;
171           /* breakpoint          */
172         case 33: abfd->tdata.cisco_core_data->sig = SIGTRAP;  break;
173
174           /* floating point err  */
175         case 48: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
176           /* floating point err  */
177         case 49: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
178           /* zero divide         */
179         case 50: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
180           /* underflow           */
181         case 51: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
182           /* operand error       */
183         case 52: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
184            /* overflow            */
185         case 53: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
186           /* NAN                 */
187         case 54: abfd->tdata.cisco_core_data->sig = SIGFPE;  break;
188         default:
189           /* "software generated"*/
190           abfd->tdata.cisco_core_data->sig = SIGEMT;
191         }
192       break;
193     default:
194       /* Unknown crash reason.  */
195       abfd->tdata.cisco_core_data->sig = 0;
196       break;
197     }
198
199   abfd->sections = NULL;
200   abfd->section_count = 0;
201
202   asect = (asection *) bfd_zmalloc (sizeof (asection));
203   if (asect == NULL)
204     {
205       bfd_set_error (bfd_error_no_memory);
206       goto error_return;
207     }
208   asect->name = ".reg";
209   asect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
210   /* This can be bigger than the real size.  Set it to the size of the whole
211      core file.  */
212   asect->_raw_size = statbuf.st_size;
213   asect->vma = 0;
214   asect->filepos = bfd_get_32 (abfd, crashinfo.registers) - rambase;
215   asect->next = abfd->sections;
216   abfd->sections = asect;
217   ++abfd->section_count;
218
219   /* There is only one section containing data from the target system's RAM.
220      We call it .data.  */
221   asect = (asection *) bfd_zmalloc (sizeof (asection));
222   if (asect == NULL)
223     {
224       bfd_set_error (bfd_error_no_memory);
225       goto error_return;
226     }
227   asect->name = ".data";
228   asect->flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
229   /* The size of memory is the size of the core file itself.  */
230   asect->_raw_size = statbuf.st_size;
231   asect->vma = rambase;
232   asect->filepos = 0;
233   asect->next = abfd->sections;
234   abfd->sections = asect;
235   ++abfd->section_count;
236
237   return abfd->xvec;
238
239  error_return:
240   {
241     sec_ptr nextsect;
242     for (asect = abfd->sections; asect != NULL;)
243       {
244         nextsect = asect->next;
245         free (asect);
246         asect = nextsect;
247       }
248     free (abfd->tdata);
249     return NULL;
250   }
251 }
252
253 char *
254 cisco_core_file_failing_command (abfd)
255      bfd *abfd;
256 {
257   return NULL;
258 }
259
260 int
261 cisco_core_file_failing_signal (abfd)
262      bfd *abfd;
263 {
264   return abfd->tdata.cisco_core_data->sig;
265 }
266
267 boolean
268 cisco_core_file_matches_executable_p (core_bfd, exec_bfd)
269      bfd *core_bfd;
270      bfd *exec_bfd;
271 {
272   return true;
273 }
274 \f
275 /* No archive file support via this BFD */
276 #define cisco_openr_next_archived_file  bfd_generic_openr_next_archived_file
277 #define cisco_generic_stat_arch_elt             bfd_generic_stat_arch_elt
278 #define cisco_slurp_armap                       bfd_false
279 #define cisco_slurp_extended_name_table bfd_true
280 #define cisco_write_armap                       (boolean (*) PARAMS     \
281     ((bfd *arch, unsigned int elength, struct orl *map, \
282       unsigned int orl_count, int stridx))) bfd_false
283 #define cisco_truncate_arname           bfd_dont_truncate_arname
284 #define aout_32_openr_next_archived_file        bfd_generic_openr_next_archived_file
285
286 #define cisco_close_and_cleanup         bfd_generic_close_and_cleanup
287 #define cisco_set_section_contents              (boolean (*) PARAMS     \
288         ((bfd *abfd, asection *section, PTR data, file_ptr offset,      \
289         bfd_size_type count))) bfd_generic_set_section_contents
290 #define cisco_get_section_contents              bfd_generic_get_section_contents
291 #define cisco_new_section_hook          (boolean (*) PARAMS     \
292         ((bfd *, sec_ptr))) bfd_true
293 #define cisco_get_symtab_upper_bound    bfd_0u
294 #define cisco_get_symtab                        (unsigned int (*) PARAMS \
295         ((bfd *, struct symbol_cache_entry **))) bfd_0u
296 #define cisco_get_reloc_upper_bound             (unsigned int (*) PARAMS \
297         ((bfd *, sec_ptr))) bfd_0u
298 #define cisco_canonicalize_reloc                (unsigned int (*) PARAMS \
299         ((bfd *, sec_ptr, arelent **, struct symbol_cache_entry**))) bfd_0u
300 #define cisco_make_empty_symbol         (struct symbol_cache_entry * \
301         (*) PARAMS ((bfd *))) bfd_false
302 #define cisco_print_symbol                      (void (*) PARAMS        \
303         ((bfd *, PTR, struct symbol_cache_entry  *,                     \
304         bfd_print_symbol_type))) bfd_false
305 #define cisco_get_symbol_info           (void (*) PARAMS        \
306         ((bfd *, struct symbol_cache_entry  *,                  \
307         symbol_info *))) bfd_false
308 #define cisco_get_lineno                        (alent * (*) PARAMS     \
309         ((bfd *, struct symbol_cache_entry *))) bfd_nullvoidptr
310 #define cisco_set_arch_mach                     (boolean (*) PARAMS     \
311         ((bfd *, enum bfd_architecture, unsigned long))) bfd_false
312 #define cisco_find_nearest_line         (boolean (*) PARAMS     \
313         ((bfd *abfd, struct sec  *section,                              \
314          struct symbol_cache_entry  **symbols,bfd_vma offset,           \
315          CONST char **file, CONST char **func, unsigned int *line))) bfd_false
316 #define cisco_sizeof_headers            (int (*) PARAMS \
317         ((bfd *, boolean))) bfd_0
318
319 #define cisco_bfd_debug_info_start              bfd_void
320 #define cisco_bfd_debug_info_end                bfd_void
321 #define cisco_bfd_debug_info_accumulate (void (*) PARAMS        \
322         ((bfd *, struct sec *))) bfd_void
323 #define cisco_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
324 #define cisco_bfd_relax_section         bfd_generic_relax_section
325 #define cisco_bfd_reloc_type_lookup \
326   ((CONST struct reloc_howto_struct *(*) PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr)
327 #define cisco_bfd_make_debug_symbol \
328   ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
329 #define cisco_bfd_link_hash_table_create \
330   ((struct bfd_link_hash_table *(*) PARAMS ((bfd *))) bfd_nullvoidptr)
331 #define cisco_bfd_link_add_symbols \
332   ((boolean (*) PARAMS ((bfd *, struct bfd_link_info *))) bfd_false)
333 #define cisco_bfd_final_link \
334   ((boolean (*) PARAMS ((bfd *, struct bfd_link_info *))) bfd_false)
335 #define cisco_bfd_copy_private_section_data \
336   ((boolean (*) PARAMS ((bfd *, asection *, bfd *, asection *))) bfd_false)
337 #define cisco_bfd_copy_private_bfd_data \
338   ((boolean (*) PARAMS ((bfd *, bfd *))) bfd_false)
339 #define cisco_bfd_is_local_label \
340   ((boolean (*) PARAMS ((bfd *, asection *))) bfd_false)
341
342 /* If somebody calls any byte-swapping routines, shoot them.  */
343 void
344 swap_abort()
345 {
346   abort(); /* This way doesn't require any declaration for ANSI to fuck up */
347 }
348 #define NO_GET  ((bfd_vma (*) PARAMS ((   const bfd_byte *))) swap_abort )
349 #define NO_PUT  ((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
350 #define NO_SIGNED_GET \
351   ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
352
353 bfd_target trad_core_vec =
354   {
355     "trad-core",
356     bfd_target_unknown_flavour,
357     true,                       /* target byte order */
358     true,                       /* target headers byte order */
359     (HAS_RELOC | EXEC_P |       /* object flags */
360      HAS_LINENO | HAS_DEBUG |
361      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
362     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
363     0,                                                     /* symbol prefix */
364     ' ',                                                   /* ar_pad_char */
365     16,                                                    /* ar_max_namelen */
366     3,                                                     /* minimum alignment power */
367     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit data */
368     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit data */
369     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit data */
370     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit hdrs */
371     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit hdrs */
372     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit hdrs */
373
374     {                           /* bfd_check_format */
375      _bfd_dummy_target,         /* unknown format */
376      _bfd_dummy_target,         /* object file */
377      _bfd_dummy_target,         /* archive */
378      cisco_core_file_p  /* a core file */
379     },
380     {                           /* bfd_set_format */
381      bfd_false, bfd_false,
382      bfd_false, bfd_false
383     },
384     {                           /* bfd_write_contents */
385      bfd_false, bfd_false,
386      bfd_false, bfd_false
387     },
388     
389     JUMP_TABLE(cisco),
390     (PTR) 0                     /* backend_data */
391 };