* trad-core.c (trad_unix_core_file_p): Call bfd_zmalloc not
[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 \f
25 #define CRASH_INFO (0xffc)
26 #define CRASH_MAGIC 0xdead1234
27
28 typedef enum {
29     CRASH_REASON_NOTCRASHED = 0,
30     CRASH_REASON_EXCEPTION = 1,
31     CRASH_REASON_CORRUPT = 2,
32   } crashreason;
33
34 struct crashinfo_external
35 {
36   char magic[4];                        /* Magic number */
37   char version[4];              /* Version number */
38   char reason[4];               /* Crash reason */
39   char cpu_vector[4];           /* CPU vector for exceptions */
40   char registers[4];            /* Pointer to saved registers */
41   char rambase[4];              /* Base of RAM (not in V1 crash info) */
42 };
43 \f
44 struct cisco_core_struct
45 {
46   int sig;
47 };
48 \f
49 static bfd_target *
50 cisco_core_file_p (abfd)
51      bfd *abfd;
52 {
53   char buf[4];
54   unsigned int crashinfo_offset;
55   struct crashinfo_external crashinfo;
56   int nread;
57   unsigned int rambase;
58   sec_ptr asect;
59   struct stat statbuf;
60
61   if (bfd_seek (abfd, CRASH_INFO, SEEK_SET) != 0)
62     return NULL;
63
64   nread = bfd_read (buf, 1, 4, abfd);
65   if (nread != 4)
66     {
67       /* Maybe the file is too small (FIXME: what about other errors).  */
68       bfd_set_error (bfd_error_wrong_format);
69       return NULL;
70     }
71   crashinfo_offset = bfd_get_32 (abfd, buf);
72
73   if (bfd_seek (abfd, crashinfo_offset, SEEK_SET) != 0)
74     return NULL;
75
76   nread = bfd_read (&crashinfo, 1, sizeof (crashinfo), abfd);
77   if (nread != sizeof (crashinfo))
78     {
79       /* Maybe the file is too small (FIXME: what about other errors).  */
80       bfd_set_error (bfd_error_wrong_format);
81       return NULL;
82     }
83
84   if (bfd_stat (abfd, &statbuf) < 0)
85     {
86       bfd_set_error (bfd_error_system_call);
87       return NULL;
88     }
89
90   if (bfd_get_32 (abfd, crashinfo.magic) != CRASH_MAGIC)
91     {
92       bfd_set_error (bfd_error_wrong_format);
93       return NULL;
94     }
95
96   switch (bfd_get_32 (abfd, crashinfo.version))
97     {
98     case 0:
99       bfd_set_error (bfd_error_wrong_format);
100       return NULL;
101     case 1:
102       rambase = 0;
103       break;
104     default:
105     case 2:
106       rambase = bfd_get_32 (abfd, crashinfo.rambase);
107       break;
108     }
109
110   /* OK, we believe you.  You're a core file.  */
111
112   abfd->tdata.cisco_core_data = (struct cisco_core_struct *)
113     bfd_zmalloc (abfd, sizeof (struct cisco_core_struct));
114   if (abfd->tdata.cisco_core_data == NULL)
115     {
116       bfd_set_error (bfd_error_no_memory);
117       return NULL;
118     }
119
120   switch ((crashreason) bfd_get_32 (abfd, crashinfo.reason))
121     {
122     case CRASH_REASON_NOTCRASHED:
123       /* Crash file probably came from write core.  */
124       abfd->tdata.cisco_core_data->sig = 0;
125       break;
126     case CRASH_REASON_CORRUPT:
127       /* The crash context area was corrupt -- proceed with caution.
128          We have no way of passing this information back to the caller.  */
129       abfd->tdata.cisco_core_data->sig = 0;
130       break;
131     case CRASH_REASON_EXCEPTION:
132       /* Crash occured due to CPU exception.
133          FIXME: convert cpu_vector to a signal number.  */
134       abfd->tdata.cisco_core_data->sig = 0;
135       break;
136     default:
137       /* Unknown crash reason.  */
138       abfd->tdata.cisco_core_data->sig = 0;
139       break;
140     }
141
142   abfd->sections = NULL;
143   abfd->section_count = 0;
144
145   asect = (asection *) bfd_zmalloc (sizeof (asection));
146   if (asect == NULL)
147     {
148       bfd_set_error (bfd_error_no_memory);
149       goto error_return;
150     }
151   asect->name = ".reg";
152   asect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
153   /* This can be bigger than the real size.  Set it to the size of the whole
154      core file.  */
155   asect->_raw_size = statbuf.st_size;
156   asect->vma = 0;
157   asect->filepos = bfd_get_32 (abfd, crashinfo.registers) - rambase;
158   asect->next = abfd->sections;
159   abfd->sections = asect;
160   ++abfd->section_count;
161
162   /* There is only one section containing data from the target system's RAM.
163      We call it .data.  */
164   asect = (asection *) bfd_zmalloc (sizeof (asection));
165   if (asect == NULL)
166     {
167       bfd_set_error (bfd_error_no_memory);
168       goto error_return;
169     }
170   asect->name = ".data";
171   asect->flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
172   /* The size of memory is the size of the core file itself.  */
173   asect->_raw_size = statbuf.st_size;
174   asect->vma = rambase;
175   asect->filepos = 0;
176   asect->next = abfd->sections;
177   abfd->sections = asect;
178   ++abfd->section_count;
179
180   return abfd->xvec;
181
182  error_return:
183   {
184     sec_ptr nextsect;
185     for (asect = abfd->sections; asect != NULL;)
186       {
187         nextsect = asect->next;
188         free (asect);
189         asect = nextsect;
190       }
191     free (abfd->tdata);
192     return NULL;
193   }
194 }
195
196 char *
197 cisco_core_file_failing_command (abfd)
198      bfd *abfd;
199 {
200   return NULL;
201 }
202
203 int
204 cisco_core_file_failing_signal (abfd)
205      bfd *abfd;
206 {
207   return abfd->tdata.cisco_core_data->sig;
208 }
209
210 boolean
211 cisco_core_file_matches_executable_p (core_bfd, exec_bfd)
212      bfd *core_bfd;
213      bfd *exec_bfd;
214 {
215   return true;
216 }
217 \f
218 /* No archive file support via this BFD */
219 #define cisco_openr_next_archived_file  bfd_generic_openr_next_archived_file
220 #define cisco_generic_stat_arch_elt             bfd_generic_stat_arch_elt
221 #define cisco_slurp_armap                       bfd_false
222 #define cisco_slurp_extended_name_table bfd_true
223 #define cisco_write_armap                       (boolean (*) PARAMS     \
224     ((bfd *arch, unsigned int elength, struct orl *map, \
225       unsigned int orl_count, int stridx))) bfd_false
226 #define cisco_truncate_arname           bfd_dont_truncate_arname
227 #define aout_32_openr_next_archived_file        bfd_generic_openr_next_archived_file
228
229 #define cisco_close_and_cleanup         bfd_generic_close_and_cleanup
230 #define cisco_set_section_contents              (boolean (*) PARAMS     \
231         ((bfd *abfd, asection *section, PTR data, file_ptr offset,      \
232         bfd_size_type count))) bfd_generic_set_section_contents
233 #define cisco_get_section_contents              bfd_generic_get_section_contents
234 #define cisco_new_section_hook          (boolean (*) PARAMS     \
235         ((bfd *, sec_ptr))) bfd_true
236 #define cisco_get_symtab_upper_bound    bfd_0u
237 #define cisco_get_symtab                        (unsigned int (*) PARAMS \
238         ((bfd *, struct symbol_cache_entry **))) bfd_0u
239 #define cisco_get_reloc_upper_bound             (unsigned int (*) PARAMS \
240         ((bfd *, sec_ptr))) bfd_0u
241 #define cisco_canonicalize_reloc                (unsigned int (*) PARAMS \
242         ((bfd *, sec_ptr, arelent **, struct symbol_cache_entry**))) bfd_0u
243 #define cisco_make_empty_symbol         (struct symbol_cache_entry * \
244         (*) PARAMS ((bfd *))) bfd_false
245 #define cisco_print_symbol                      (void (*) PARAMS        \
246         ((bfd *, PTR, struct symbol_cache_entry  *,                     \
247         bfd_print_symbol_type))) bfd_false
248 #define cisco_get_symbol_info           (void (*) PARAMS        \
249         ((bfd *, struct symbol_cache_entry  *,                  \
250         symbol_info *))) bfd_false
251 #define cisco_get_lineno                        (alent * (*) PARAMS     \
252         ((bfd *, struct symbol_cache_entry *))) bfd_nullvoidptr
253 #define cisco_set_arch_mach                     (boolean (*) PARAMS     \
254         ((bfd *, enum bfd_architecture, unsigned long))) bfd_false
255 #define cisco_find_nearest_line         (boolean (*) PARAMS     \
256         ((bfd *abfd, struct sec  *section,                              \
257          struct symbol_cache_entry  **symbols,bfd_vma offset,           \
258          CONST char **file, CONST char **func, unsigned int *line))) bfd_false
259 #define cisco_sizeof_headers            (int (*) PARAMS \
260         ((bfd *, boolean))) bfd_0
261
262 #define cisco_bfd_debug_info_start              bfd_void
263 #define cisco_bfd_debug_info_end                bfd_void
264 #define cisco_bfd_debug_info_accumulate (void (*) PARAMS        \
265         ((bfd *, struct sec *))) bfd_void
266 #define cisco_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
267 #define cisco_bfd_relax_section         bfd_generic_relax_section
268 #define cisco_bfd_reloc_type_lookup \
269   ((CONST struct reloc_howto_struct *(*) PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr)
270 #define cisco_bfd_make_debug_symbol \
271   ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
272 #define cisco_bfd_link_hash_table_create \
273   ((struct bfd_link_hash_table *(*) PARAMS ((bfd *))) bfd_nullvoidptr)
274 #define cisco_bfd_link_add_symbols \
275   ((boolean (*) PARAMS ((bfd *, struct bfd_link_info *))) bfd_false)
276 #define cisco_bfd_final_link \
277   ((boolean (*) PARAMS ((bfd *, struct bfd_link_info *))) bfd_false)
278 #define cisco_bfd_copy_private_section_data \
279   ((boolean (*) PARAMS ((bfd *, asection *, bfd *, asection *))) bfd_false)
280 #define cisco_bfd_copy_private_bfd_data \
281   ((boolean (*) PARAMS ((bfd *, bfd *))) bfd_false)
282 #define cisco_bfd_is_local_label \
283   ((boolean (*) PARAMS ((bfd *, asection *))) bfd_false)
284
285 /* If somebody calls any byte-swapping routines, shoot them.  */
286 void
287 swap_abort()
288 {
289   abort(); /* This way doesn't require any declaration for ANSI to fuck up */
290 }
291 #define NO_GET  ((bfd_vma (*) PARAMS ((   const bfd_byte *))) swap_abort )
292 #define NO_PUT  ((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
293 #define NO_SIGNED_GET \
294   ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
295
296 bfd_target trad_core_vec =
297   {
298     "trad-core",
299     bfd_target_unknown_flavour,
300     true,                       /* target byte order */
301     true,                       /* target headers byte order */
302     (HAS_RELOC | EXEC_P |       /* object flags */
303      HAS_LINENO | HAS_DEBUG |
304      HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
305     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
306     0,                                                     /* symbol prefix */
307     ' ',                                                   /* ar_pad_char */
308     16,                                                    /* ar_max_namelen */
309     3,                                                     /* minimum alignment power */
310     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit data */
311     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit data */
312     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit data */
313     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit hdrs */
314     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit hdrs */
315     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit hdrs */
316
317     {                           /* bfd_check_format */
318      _bfd_dummy_target,         /* unknown format */
319      _bfd_dummy_target,         /* object file */
320      _bfd_dummy_target,         /* archive */
321      cisco_core_file_p  /* a core file */
322     },
323     {                           /* bfd_set_format */
324      bfd_false, bfd_false,
325      bfd_false, bfd_false
326     },
327     {                           /* bfd_write_contents */
328      bfd_false, bfd_false,
329      bfd_false, bfd_false
330     },
331     
332     JUMP_TABLE(cisco),
333     (PTR) 0                     /* backend_data */
334 };