| 1 | /* BFD back end for traditional Unix core files (U-area and raw sections) |
| 2 | Copyright 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, |
| 3 | 2000, 2001, 2002, 2003, 2004, 2005 |
| 4 | Free Software Foundation, Inc. |
| 5 | Written by John Gilmore of Cygnus Support. |
| 6 | |
| 7 | This file is part of BFD, the Binary File Descriptor library. |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software |
| 21 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
| 22 | |
| 23 | #include "bfd.h" |
| 24 | #include "sysdep.h" |
| 25 | #include "libbfd.h" |
| 26 | #include "libaout.h" /* BFD a.out internal data structures */ |
| 27 | |
| 28 | #include <sys/param.h> |
| 29 | #ifdef HAVE_DIRENT_H |
| 30 | # include <dirent.h> |
| 31 | #else |
| 32 | # ifdef HAVE_SYS_NDIR_H |
| 33 | # include <sys/ndir.h> |
| 34 | # endif |
| 35 | # ifdef HAVE_SYS_DIR_H |
| 36 | # include <sys/dir.h> |
| 37 | # endif |
| 38 | # ifdef HAVE_NDIR_H |
| 39 | # include <ndir.h> |
| 40 | # endif |
| 41 | #endif |
| 42 | #include <signal.h> |
| 43 | |
| 44 | #include <sys/user.h> /* After a.out.h */ |
| 45 | |
| 46 | #ifdef TRAD_HEADER |
| 47 | #include TRAD_HEADER |
| 48 | #endif |
| 49 | |
| 50 | struct trad_core_struct |
| 51 | { |
| 52 | asection *data_section; |
| 53 | asection *stack_section; |
| 54 | asection *reg_section; |
| 55 | struct user u; |
| 56 | }; |
| 57 | |
| 58 | #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u)) |
| 59 | #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section) |
| 60 | #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section) |
| 61 | #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section) |
| 62 | |
| 63 | /* forward declarations */ |
| 64 | |
| 65 | const bfd_target *trad_unix_core_file_p PARAMS ((bfd *abfd)); |
| 66 | char * trad_unix_core_file_failing_command PARAMS ((bfd *abfd)); |
| 67 | int trad_unix_core_file_failing_signal PARAMS ((bfd *abfd)); |
| 68 | #define trad_unix_core_file_matches_executable_p generic_core_file_matches_executable_p |
| 69 | static void swap_abort PARAMS ((void)); |
| 70 | |
| 71 | /* Handle 4.2-style (and perhaps also sysV-style) core dump file. */ |
| 72 | |
| 73 | const bfd_target * |
| 74 | trad_unix_core_file_p (abfd) |
| 75 | bfd *abfd; |
| 76 | |
| 77 | { |
| 78 | int val; |
| 79 | struct user u; |
| 80 | struct trad_core_struct *rawptr; |
| 81 | bfd_size_type amt; |
| 82 | |
| 83 | #ifdef TRAD_CORE_USER_OFFSET |
| 84 | /* If defined, this macro is the file position of the user struct. */ |
| 85 | if (bfd_seek (abfd, (file_ptr) TRAD_CORE_USER_OFFSET, SEEK_SET) != 0) |
| 86 | return 0; |
| 87 | #endif |
| 88 | |
| 89 | val = bfd_bread ((void *) &u, (bfd_size_type) sizeof u, abfd); |
| 90 | if (val != sizeof u) |
| 91 | { |
| 92 | /* Too small to be a core file */ |
| 93 | bfd_set_error (bfd_error_wrong_format); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /* Sanity check perhaps??? */ |
| 98 | if (u.u_dsize > 0x1000000) /* Remember, it's in pages... */ |
| 99 | { |
| 100 | bfd_set_error (bfd_error_wrong_format); |
| 101 | return 0; |
| 102 | } |
| 103 | if (u.u_ssize > 0x1000000) |
| 104 | { |
| 105 | bfd_set_error (bfd_error_wrong_format); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | /* Check that the size claimed is no greater than the file size. */ |
| 110 | { |
| 111 | struct stat statbuf; |
| 112 | |
| 113 | if (bfd_stat (abfd, &statbuf) < 0) |
| 114 | return 0; |
| 115 | |
| 116 | if ((unsigned long) (NBPG * (UPAGES + u.u_dsize |
| 117 | #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE |
| 118 | - u.u_tsize |
| 119 | #endif |
| 120 | + u.u_ssize)) |
| 121 | > (unsigned long) statbuf.st_size) |
| 122 | { |
| 123 | bfd_set_error (bfd_error_wrong_format); |
| 124 | return 0; |
| 125 | } |
| 126 | #ifndef TRAD_CORE_ALLOW_ANY_EXTRA_SIZE |
| 127 | if ((unsigned long) (NBPG * (UPAGES + u.u_dsize + u.u_ssize) |
| 128 | #ifdef TRAD_CORE_EXTRA_SIZE_ALLOWED |
| 129 | /* Some systems write the file too big. */ |
| 130 | + TRAD_CORE_EXTRA_SIZE_ALLOWED |
| 131 | #endif |
| 132 | ) |
| 133 | < (unsigned long) statbuf.st_size) |
| 134 | { |
| 135 | /* The file is too big. Maybe it's not a core file |
| 136 | or we otherwise have bad values for u_dsize and u_ssize). */ |
| 137 | bfd_set_error (bfd_error_wrong_format); |
| 138 | return 0; |
| 139 | } |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | /* OK, we believe you. You're a core file (sure, sure). */ |
| 144 | |
| 145 | /* Allocate both the upage and the struct core_data at once, so |
| 146 | a single free() will free them both. */ |
| 147 | amt = sizeof (struct trad_core_struct); |
| 148 | rawptr = (struct trad_core_struct *) bfd_zmalloc (amt); |
| 149 | if (rawptr == NULL) |
| 150 | return 0; |
| 151 | |
| 152 | abfd->tdata.trad_core_data = rawptr; |
| 153 | |
| 154 | rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */ |
| 155 | |
| 156 | /* Create the sections. */ |
| 157 | |
| 158 | core_stacksec(abfd) = bfd_make_section_anyway (abfd, ".stack"); |
| 159 | if (core_stacksec (abfd) == NULL) |
| 160 | goto fail; |
| 161 | core_datasec (abfd) = bfd_make_section_anyway (abfd, ".data"); |
| 162 | if (core_datasec (abfd) == NULL) |
| 163 | goto fail; |
| 164 | core_regsec (abfd) = bfd_make_section_anyway (abfd, ".reg"); |
| 165 | if (core_regsec (abfd) == NULL) |
| 166 | goto fail; |
| 167 | |
| 168 | core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS; |
| 169 | core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS; |
| 170 | core_regsec (abfd)->flags = SEC_HAS_CONTENTS; |
| 171 | |
| 172 | core_datasec (abfd)->size = NBPG * u.u_dsize |
| 173 | #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE |
| 174 | - NBPG * u.u_tsize |
| 175 | #endif |
| 176 | ; |
| 177 | core_stacksec (abfd)->size = NBPG * u.u_ssize; |
| 178 | core_regsec (abfd)->size = NBPG * UPAGES; /* Larger than sizeof struct u */ |
| 179 | |
| 180 | /* What a hack... we'd like to steal it from the exec file, |
| 181 | since the upage does not seem to provide it. FIXME. */ |
| 182 | #ifdef HOST_DATA_START_ADDR |
| 183 | core_datasec (abfd)->vma = HOST_DATA_START_ADDR; |
| 184 | #else |
| 185 | core_datasec (abfd)->vma = HOST_TEXT_START_ADDR + (NBPG * u.u_tsize); |
| 186 | #endif |
| 187 | |
| 188 | #ifdef HOST_STACK_START_ADDR |
| 189 | core_stacksec (abfd)->vma = HOST_STACK_START_ADDR; |
| 190 | #else |
| 191 | core_stacksec (abfd)->vma = HOST_STACK_END_ADDR - (NBPG * u.u_ssize); |
| 192 | #endif |
| 193 | |
| 194 | /* This is tricky. As the "register section", we give them the entire |
| 195 | upage and stack. u.u_ar0 points to where "register 0" is stored. |
| 196 | There are two tricks with this, though. One is that the rest of the |
| 197 | registers might be at positive or negative (or both) displacements |
| 198 | from *u_ar0. The other is that u_ar0 is sometimes an absolute address |
| 199 | in kernel memory, and on other systems it is an offset from the beginning |
| 200 | of the `struct user'. |
| 201 | |
| 202 | As a practical matter, we don't know where the registers actually are, |
| 203 | so we have to pass the whole area to GDB. We encode the value of u_ar0 |
| 204 | by setting the .regs section up so that its virtual memory address |
| 205 | 0 is at the place pointed to by u_ar0 (by setting the vma of the start |
| 206 | of the section to -u_ar0). GDB uses this info to locate the regs, |
| 207 | using minor trickery to get around the offset-or-absolute-addr problem. */ |
| 208 | core_regsec (abfd)->vma = - (bfd_vma) (unsigned long) u.u_ar0; |
| 209 | |
| 210 | core_datasec (abfd)->filepos = NBPG * UPAGES; |
| 211 | core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize |
| 212 | #ifdef TRAD_CORE_DSIZE_INCLUDES_TSIZE |
| 213 | - NBPG * u.u_tsize |
| 214 | #endif |
| 215 | ; |
| 216 | core_regsec (abfd)->filepos = 0; /* Register segment is the upage */ |
| 217 | |
| 218 | /* Align to word at least */ |
| 219 | core_stacksec (abfd)->alignment_power = 2; |
| 220 | core_datasec (abfd)->alignment_power = 2; |
| 221 | core_regsec (abfd)->alignment_power = 2; |
| 222 | |
| 223 | return abfd->xvec; |
| 224 | |
| 225 | fail: |
| 226 | bfd_release (abfd, abfd->tdata.any); |
| 227 | abfd->tdata.any = NULL; |
| 228 | bfd_section_list_clear (abfd); |
| 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | char * |
| 233 | trad_unix_core_file_failing_command (abfd) |
| 234 | bfd *abfd; |
| 235 | { |
| 236 | #ifndef NO_CORE_COMMAND |
| 237 | char *com = abfd->tdata.trad_core_data->u.u_comm; |
| 238 | if (*com) |
| 239 | return com; |
| 240 | else |
| 241 | #endif |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int |
| 246 | trad_unix_core_file_failing_signal (ignore_abfd) |
| 247 | bfd *ignore_abfd ATTRIBUTE_UNUSED; |
| 248 | { |
| 249 | #ifdef TRAD_UNIX_CORE_FILE_FAILING_SIGNAL |
| 250 | return TRAD_UNIX_CORE_FILE_FAILING_SIGNAL(ignore_abfd); |
| 251 | #else |
| 252 | return -1; /* FIXME, where is it? */ |
| 253 | #endif |
| 254 | } |
| 255 | \f |
| 256 | /* If somebody calls any byte-swapping routines, shoot them. */ |
| 257 | static void |
| 258 | swap_abort () |
| 259 | { |
| 260 | abort (); /* This way doesn't require any declaration for ANSI to fuck up */ |
| 261 | } |
| 262 | |
| 263 | #define NO_GET ((bfd_vma (*) (const void *)) swap_abort) |
| 264 | #define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort) |
| 265 | #define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort) |
| 266 | #define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort) |
| 267 | #define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort) |
| 268 | #define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort) |
| 269 | |
| 270 | const bfd_target trad_core_vec = |
| 271 | { |
| 272 | "trad-core", |
| 273 | bfd_target_unknown_flavour, |
| 274 | BFD_ENDIAN_UNKNOWN, /* target byte order */ |
| 275 | BFD_ENDIAN_UNKNOWN, /* target headers byte order */ |
| 276 | (HAS_RELOC | EXEC_P | /* object flags */ |
| 277 | HAS_LINENO | HAS_DEBUG | |
| 278 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), |
| 279 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ |
| 280 | 0, /* symbol prefix */ |
| 281 | ' ', /* ar_pad_char */ |
| 282 | 16, /* ar_max_namelen */ |
| 283 | NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */ |
| 284 | NO_GET, NO_GETS, NO_PUT, /* 32 bit data */ |
| 285 | NO_GET, NO_GETS, NO_PUT, /* 16 bit data */ |
| 286 | NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */ |
| 287 | NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */ |
| 288 | NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */ |
| 289 | |
| 290 | { /* bfd_check_format */ |
| 291 | _bfd_dummy_target, /* unknown format */ |
| 292 | _bfd_dummy_target, /* object file */ |
| 293 | _bfd_dummy_target, /* archive */ |
| 294 | trad_unix_core_file_p /* a core file */ |
| 295 | }, |
| 296 | { /* bfd_set_format */ |
| 297 | bfd_false, bfd_false, |
| 298 | bfd_false, bfd_false |
| 299 | }, |
| 300 | { /* bfd_write_contents */ |
| 301 | bfd_false, bfd_false, |
| 302 | bfd_false, bfd_false |
| 303 | }, |
| 304 | |
| 305 | BFD_JUMP_TABLE_GENERIC (_bfd_generic), |
| 306 | BFD_JUMP_TABLE_COPY (_bfd_generic), |
| 307 | BFD_JUMP_TABLE_CORE (trad_unix), |
| 308 | BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive), |
| 309 | BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols), |
| 310 | BFD_JUMP_TABLE_RELOCS (_bfd_norelocs), |
| 311 | BFD_JUMP_TABLE_WRITE (_bfd_generic), |
| 312 | BFD_JUMP_TABLE_LINK (_bfd_nolink), |
| 313 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
| 314 | |
| 315 | NULL, |
| 316 | |
| 317 | (PTR) 0 /* backend_data */ |
| 318 | }; |