| 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., 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 | \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 const 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 | if (bfd_get_error () != bfd_error_system_call) |
| 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 | if (bfd_get_error () != bfd_error_system_call) |
| 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 | return NULL; |
| 120 | |
| 121 | switch ((crashreason) bfd_get_32 (abfd, crashinfo.reason)) |
| 122 | { |
| 123 | case CRASH_REASON_NOTCRASHED: |
| 124 | /* Crash file probably came from write core. */ |
| 125 | abfd->tdata.cisco_core_data->sig = 0; |
| 126 | break; |
| 127 | case CRASH_REASON_CORRUPT: |
| 128 | /* The crash context area was corrupt -- proceed with caution. |
| 129 | We have no way of passing this information back to the caller. */ |
| 130 | abfd->tdata.cisco_core_data->sig = 0; |
| 131 | break; |
| 132 | case CRASH_REASON_EXCEPTION: |
| 133 | /* Crash occured due to CPU exception. */ |
| 134 | |
| 135 | /* This is 68k-specific; for MIPS we'll need to interpret |
| 136 | cpu_vector differently based on the target configuration |
| 137 | (since CISCO core files don't seem to have the processor |
| 138 | encoded in them). */ |
| 139 | |
| 140 | switch (bfd_get_32 (abfd, crashinfo.cpu_vector)) |
| 141 | { |
| 142 | /* bus error */ |
| 143 | case 2 : abfd->tdata.cisco_core_data->sig = SIGBUS; break; |
| 144 | /* address error */ |
| 145 | case 3 : abfd->tdata.cisco_core_data->sig = SIGBUS; break; |
| 146 | /* illegal instruction */ |
| 147 | case 4 : abfd->tdata.cisco_core_data->sig = SIGILL; break; |
| 148 | /* zero divide */ |
| 149 | case 5 : abfd->tdata.cisco_core_data->sig = SIGFPE; break; |
| 150 | /* chk instruction */ |
| 151 | case 6 : abfd->tdata.cisco_core_data->sig = SIGFPE; break; |
| 152 | /* trapv instruction */ |
| 153 | case 7 : abfd->tdata.cisco_core_data->sig = SIGFPE; break; |
| 154 | /* privilege violation */ |
| 155 | case 8 : abfd->tdata.cisco_core_data->sig = SIGSEGV; break; |
| 156 | /* trace trap */ |
| 157 | case 9 : abfd->tdata.cisco_core_data->sig = SIGTRAP; break; |
| 158 | /* line 1010 emulator */ |
| 159 | case 10: abfd->tdata.cisco_core_data->sig = SIGILL; break; |
| 160 | /* line 1111 emulator */ |
| 161 | case 11: abfd->tdata.cisco_core_data->sig = SIGILL; break; |
| 162 | |
| 163 | /* Coprocessor protocol violation. Using a standard MMU or FPU |
| 164 | this cannot be triggered by software. Call it a SIGBUS. */ |
| 165 | case 13: abfd->tdata.cisco_core_data->sig = SIGBUS; break; |
| 166 | |
| 167 | /* interrupt */ |
| 168 | case 31: abfd->tdata.cisco_core_data->sig = SIGINT; break; |
| 169 | /* breakpoint */ |
| 170 | case 33: abfd->tdata.cisco_core_data->sig = SIGTRAP; break; |
| 171 | |
| 172 | /* floating point err */ |
| 173 | case 48: abfd->tdata.cisco_core_data->sig = SIGFPE; break; |
| 174 | /* floating point err */ |
| 175 | case 49: abfd->tdata.cisco_core_data->sig = SIGFPE; break; |
| 176 | /* zero divide */ |
| 177 | case 50: abfd->tdata.cisco_core_data->sig = SIGFPE; break; |
| 178 | /* underflow */ |
| 179 | case 51: abfd->tdata.cisco_core_data->sig = SIGFPE; break; |
| 180 | /* operand error */ |
| 181 | case 52: abfd->tdata.cisco_core_data->sig = SIGFPE; break; |
| 182 | /* overflow */ |
| 183 | case 53: abfd->tdata.cisco_core_data->sig = SIGFPE; break; |
| 184 | /* NAN */ |
| 185 | case 54: abfd->tdata.cisco_core_data->sig = SIGFPE; break; |
| 186 | default: |
| 187 | /* "software generated"*/ |
| 188 | abfd->tdata.cisco_core_data->sig = SIGEMT; |
| 189 | } |
| 190 | break; |
| 191 | default: |
| 192 | /* Unknown crash reason. */ |
| 193 | abfd->tdata.cisco_core_data->sig = 0; |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | abfd->sections = NULL; |
| 198 | abfd->section_count = 0; |
| 199 | |
| 200 | asect = (asection *) bfd_zmalloc (sizeof (asection)); |
| 201 | if (asect == NULL) |
| 202 | goto error_return; |
| 203 | asect->name = ".reg"; |
| 204 | asect->flags = SEC_HAS_CONTENTS; |
| 205 | /* This can be bigger than the real size. Set it to the size of the whole |
| 206 | core file. */ |
| 207 | asect->_raw_size = statbuf.st_size; |
| 208 | asect->vma = 0; |
| 209 | asect->filepos = bfd_get_32 (abfd, crashinfo.registers) - rambase; |
| 210 | asect->next = abfd->sections; |
| 211 | abfd->sections = asect; |
| 212 | ++abfd->section_count; |
| 213 | |
| 214 | /* There is only one section containing data from the target system's RAM. |
| 215 | We call it .data. */ |
| 216 | asect = (asection *) bfd_zmalloc (sizeof (asection)); |
| 217 | if (asect == NULL) |
| 218 | goto error_return; |
| 219 | asect->name = ".data"; |
| 220 | asect->flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS; |
| 221 | /* The size of memory is the size of the core file itself. */ |
| 222 | asect->_raw_size = statbuf.st_size; |
| 223 | asect->vma = rambase; |
| 224 | asect->filepos = 0; |
| 225 | asect->next = abfd->sections; |
| 226 | abfd->sections = asect; |
| 227 | ++abfd->section_count; |
| 228 | |
| 229 | return abfd->xvec; |
| 230 | |
| 231 | error_return: |
| 232 | { |
| 233 | sec_ptr nextsect; |
| 234 | for (asect = abfd->sections; asect != NULL;) |
| 235 | { |
| 236 | nextsect = asect->next; |
| 237 | free (asect); |
| 238 | asect = nextsect; |
| 239 | } |
| 240 | free (abfd->tdata.cisco_core_data); |
| 241 | return NULL; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | char * |
| 246 | cisco_core_file_failing_command (abfd) |
| 247 | bfd *abfd; |
| 248 | { |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | int |
| 253 | cisco_core_file_failing_signal (abfd) |
| 254 | bfd *abfd; |
| 255 | { |
| 256 | return abfd->tdata.cisco_core_data->sig; |
| 257 | } |
| 258 | |
| 259 | boolean |
| 260 | cisco_core_file_matches_executable_p (core_bfd, exec_bfd) |
| 261 | bfd *core_bfd; |
| 262 | bfd *exec_bfd; |
| 263 | { |
| 264 | return true; |
| 265 | } |
| 266 | \f |
| 267 | const bfd_target cisco_core_vec = |
| 268 | { |
| 269 | "trad-core", |
| 270 | bfd_target_unknown_flavour, |
| 271 | true, /* target byte order */ |
| 272 | true, /* target headers byte order */ |
| 273 | (HAS_RELOC | EXEC_P | /* object flags */ |
| 274 | HAS_LINENO | HAS_DEBUG | |
| 275 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), |
| 276 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ |
| 277 | 0, /* symbol prefix */ |
| 278 | ' ', /* ar_pad_char */ |
| 279 | 16, /* ar_max_namelen */ |
| 280 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, |
| 281 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, |
| 282 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */ |
| 283 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, |
| 284 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, |
| 285 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */ |
| 286 | |
| 287 | { /* bfd_check_format */ |
| 288 | _bfd_dummy_target, /* unknown format */ |
| 289 | _bfd_dummy_target, /* object file */ |
| 290 | _bfd_dummy_target, /* archive */ |
| 291 | cisco_core_file_p /* a core file */ |
| 292 | }, |
| 293 | { /* bfd_set_format */ |
| 294 | bfd_false, bfd_false, |
| 295 | bfd_false, bfd_false |
| 296 | }, |
| 297 | { /* bfd_write_contents */ |
| 298 | bfd_false, bfd_false, |
| 299 | bfd_false, bfd_false |
| 300 | }, |
| 301 | |
| 302 | BFD_JUMP_TABLE_GENERIC (_bfd_generic), |
| 303 | BFD_JUMP_TABLE_COPY (_bfd_generic), |
| 304 | BFD_JUMP_TABLE_CORE (cisco), |
| 305 | BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive), |
| 306 | BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols), |
| 307 | BFD_JUMP_TABLE_RELOCS (_bfd_norelocs), |
| 308 | BFD_JUMP_TABLE_WRITE (_bfd_generic), |
| 309 | BFD_JUMP_TABLE_LINK (_bfd_nolink), |
| 310 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
| 311 | |
| 312 | (PTR) 0 /* backend_data */ |
| 313 | }; |