| 1 | /* BFD back end for SCO5 core files (U-area and raw sections) |
| 2 | Copyright 1998, 1999, 2000 Free Software Foundation, Inc. |
| 3 | Written by Jouke Numan <jnuman@hiscom.nl> |
| 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 | #include "libaout.h" /* BFD a.out internal data structures */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/param.h> |
| 29 | #include <sys/dir.h> |
| 30 | #include <signal.h> |
| 31 | |
| 32 | #include <sys/user.h> /* After a.out.h */ |
| 33 | #include <sys/paccess.h> |
| 34 | #include <sys/region.h> |
| 35 | |
| 36 | struct sco5_core_struct |
| 37 | { |
| 38 | struct user u; |
| 39 | }; |
| 40 | |
| 41 | /* forward declarations */ |
| 42 | |
| 43 | static asection * |
| 44 | make_bfd_asection PARAMS ((bfd *, const char *, flagword, bfd_size_type, |
| 45 | bfd_vma, file_ptr)); |
| 46 | static asymbol *sco5_core_make_empty_symbol PARAMS ((bfd *)); |
| 47 | static struct user *read_uarea PARAMS ((bfd *, int)); |
| 48 | const bfd_target *sco5_core_file_p PARAMS ((bfd *abfd)); |
| 49 | char *sco5_core_file_failing_command PARAMS ((bfd *abfd)); |
| 50 | int sco5_core_file_failing_signal PARAMS ((bfd *abfd)); |
| 51 | boolean sco5_core_file_matches_executable_p PARAMS ((bfd *core_bfd, |
| 52 | bfd *exec_bfd)); |
| 53 | static void swap_abort PARAMS ((void)); |
| 54 | |
| 55 | static asection * |
| 56 | make_bfd_asection (abfd, name, flags, _raw_size, vma, filepos) |
| 57 | bfd *abfd; |
| 58 | const char *name; |
| 59 | flagword flags; |
| 60 | bfd_size_type _raw_size; |
| 61 | bfd_vma vma; |
| 62 | file_ptr filepos; |
| 63 | { |
| 64 | asection *asect; |
| 65 | |
| 66 | asect = bfd_make_section_anyway (abfd, name); |
| 67 | if (!asect) |
| 68 | return NULL; |
| 69 | asect->flags = flags; |
| 70 | asect->_raw_size = _raw_size; |
| 71 | asect->vma = vma; |
| 72 | asect->filepos = filepos; |
| 73 | asect->alignment_power = 2; |
| 74 | |
| 75 | return asect; |
| 76 | } |
| 77 | |
| 78 | static asymbol * |
| 79 | sco5_core_make_empty_symbol (abfd) |
| 80 | bfd *abfd; |
| 81 | { |
| 82 | asymbol *new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol)); |
| 83 | if (new) |
| 84 | new->the_bfd = abfd; |
| 85 | return new; |
| 86 | } |
| 87 | |
| 88 | static struct user * |
| 89 | read_uarea(abfd, filepos) |
| 90 | bfd *abfd; |
| 91 | int filepos; |
| 92 | |
| 93 | { |
| 94 | struct sco5_core_struct *rawptr; |
| 95 | |
| 96 | rawptr = ((struct sco5_core_struct *) |
| 97 | bfd_zmalloc (sizeof (struct sco5_core_struct))); |
| 98 | if (rawptr == NULL) |
| 99 | return NULL; |
| 100 | |
| 101 | abfd->tdata.sco5_core_data = rawptr; |
| 102 | |
| 103 | if ((bfd_seek (abfd, filepos, SEEK_SET) != 0) |
| 104 | || (bfd_read ((void *)&rawptr->u, 1, sizeof rawptr->u, abfd) |
| 105 | != sizeof rawptr->u)) |
| 106 | { |
| 107 | bfd_set_error (bfd_error_wrong_format); |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | /* Sanity check perhaps??? */ |
| 112 | if (rawptr->u.u_dsize > 0x1000000) /* Remember, it's in pages... */ |
| 113 | { |
| 114 | bfd_set_error (bfd_error_wrong_format); |
| 115 | return NULL; |
| 116 | } |
| 117 | if (rawptr->u.u_ssize > 0x1000000) |
| 118 | { |
| 119 | bfd_set_error (bfd_error_wrong_format); |
| 120 | return NULL; |
| 121 | } |
| 122 | return &rawptr->u; |
| 123 | } |
| 124 | |
| 125 | /* ARGSUSED */ |
| 126 | const bfd_target * |
| 127 | sco5_core_file_p (abfd) |
| 128 | bfd *abfd; |
| 129 | { |
| 130 | int coffset_siz, val, nsecs, cheadoffs; |
| 131 | int coresize; |
| 132 | struct user *u; |
| 133 | struct coreoffsets coffsets; |
| 134 | struct coresecthead chead; |
| 135 | char *secname; |
| 136 | flagword flags; |
| 137 | |
| 138 | /* Read coreoffsets region at end of core (see core(FP)) */ |
| 139 | |
| 140 | { |
| 141 | FILE *stream = bfd_cache_lookup (abfd); |
| 142 | struct stat statbuf; |
| 143 | if (stream == NULL) |
| 144 | return NULL; |
| 145 | if (fstat (fileno (stream), &statbuf) < 0) |
| 146 | { |
| 147 | bfd_set_error (bfd_error_system_call); |
| 148 | return NULL; |
| 149 | } |
| 150 | coresize = statbuf.st_size; |
| 151 | } |
| 152 | /* Last long in core is sizeof struct coreoffsets, read it */ |
| 153 | if ((bfd_seek (abfd, coresize-sizeof coffset_siz, SEEK_SET) != 0) |
| 154 | || (bfd_read ((void *)&coffset_siz, 1, sizeof coffset_siz, abfd) |
| 155 | != sizeof coffset_siz) ) |
| 156 | { |
| 157 | bfd_set_error (bfd_error_wrong_format); |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | /* Use it to seek start of coreoffsets region, read it and determine |
| 162 | validity */ |
| 163 | if ((bfd_seek (abfd, coresize-coffset_siz, SEEK_SET) != 0) |
| 164 | || (bfd_read ((void *)&coffsets, 1, sizeof coffsets, abfd) |
| 165 | != sizeof coffsets) |
| 166 | || ((coffsets.u_info != 1) && (coffsets.u_info != C_VERSION))) |
| 167 | { |
| 168 | bfd_set_error (bfd_error_wrong_format); |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | if (coffsets.u_info == 1) |
| 173 | { |
| 174 | /* Old version, no section heads, read info from user struct */ |
| 175 | |
| 176 | u = read_uarea(abfd, coffsets.u_user); |
| 177 | if (! u) |
| 178 | return NULL; |
| 179 | |
| 180 | if (!make_bfd_asection (abfd, ".reg", SEC_HAS_CONTENTS, |
| 181 | (bfd_size_type) coffsets.u_usize, |
| 182 | 0 - (bfd_vma) u->u_ar0, |
| 183 | (file_ptr) coffsets.u_user)) |
| 184 | return NULL; |
| 185 | |
| 186 | if (!make_bfd_asection (abfd, ".data", |
| 187 | SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS, |
| 188 | ((bfd_size_type) u->u_exdata.ux_dsize |
| 189 | + u->u_exdata.ux_bsize), |
| 190 | (bfd_vma) u->u_exdata.ux_datorg, |
| 191 | (file_ptr) coffsets.u_data)) |
| 192 | return NULL; |
| 193 | |
| 194 | if (!make_bfd_asection (abfd, ".stack", |
| 195 | SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS, |
| 196 | (bfd_size_type) u->u_ssize * NBPC, |
| 197 | (bfd_vma) u->u_sub, |
| 198 | (file_ptr) coffsets.u_stack)) |
| 199 | return NULL; |
| 200 | |
| 201 | return abfd->xvec; /* Done for version 1 */ |
| 202 | } |
| 203 | |
| 204 | /* Immediately before coreoffsets region is a long with offset in core |
| 205 | to first coresecthead (CORES_OFFSETS), the long before this is the |
| 206 | number of section heads in the list. Read both longs and read the |
| 207 | coresecthead and check its validity */ |
| 208 | |
| 209 | if ((bfd_seek (abfd, |
| 210 | coresize - coffset_siz - 2 * sizeof coffset_siz, |
| 211 | SEEK_SET) != 0) |
| 212 | || (bfd_read ((void *)&nsecs, 1, sizeof nsecs, abfd) != sizeof nsecs) |
| 213 | || (bfd_read ((void *)&cheadoffs, 1, sizeof cheadoffs, abfd) |
| 214 | != sizeof cheadoffs) |
| 215 | || (bfd_seek (abfd, cheadoffs, SEEK_SET) != 0) |
| 216 | || (bfd_read ((void *)&chead, 1, sizeof chead, abfd) != sizeof chead) |
| 217 | || (chead.cs_stype != CORES_OFFSETS) |
| 218 | || (chead.cs_x.csx_magic != COREMAGIC_NUMBER)) |
| 219 | { |
| 220 | bfd_set_error (bfd_error_wrong_format); |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | /* OK, we believe you. You're a core file (sure, sure). */ |
| 225 | |
| 226 | /* Now loop over all regions and map them */ |
| 227 | nsecs--; /* We've seen CORES_OFFSETS already */ |
| 228 | for (; nsecs; nsecs--) |
| 229 | { |
| 230 | if ((bfd_seek (abfd, chead.cs_hseek, SEEK_SET) != 0) |
| 231 | || bfd_read ((void *)&chead, 1, sizeof chead, abfd) != sizeof chead) |
| 232 | { |
| 233 | bfd_set_error (bfd_error_wrong_format); |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | switch (chead.cs_stype) |
| 238 | { |
| 239 | case CORES_MAGIC: /* Core header, check magic */ |
| 240 | if (chead.cs_x.csx_magic != COREMAGIC_NUMBER) |
| 241 | { |
| 242 | bfd_set_error (bfd_error_wrong_format); |
| 243 | return NULL; |
| 244 | } |
| 245 | secname = NULL; |
| 246 | nsecs++; /* MAGIC not in section cnt!*/ |
| 247 | break; |
| 248 | case CORES_UAREA: /* U-area, read in tdata */ |
| 249 | u = read_uarea(abfd, chead.cs_sseek); |
| 250 | if (! u) |
| 251 | return NULL; |
| 252 | |
| 253 | /* This is tricky. As the "register section", we give them |
| 254 | the entire upage and stack. u.u_ar0 points to where |
| 255 | "register 0" is stored. There are two tricks with this, |
| 256 | though. One is that the rest of the registers might be |
| 257 | at positive or negative (or both) displacements from |
| 258 | *u_ar0. The other is that u_ar0 is sometimes an absolute |
| 259 | address in kernel memory, and on other systems it is an |
| 260 | offset from the beginning of the `struct user'. |
| 261 | |
| 262 | As a practical matter, we don't know where the registers |
| 263 | actually are, so we have to pass the whole area to GDB. |
| 264 | We encode the value of u_ar0 by setting the .regs section |
| 265 | up so that its virtual memory address 0 is at the place |
| 266 | pointed to by u_ar0 (by setting the vma of the start of |
| 267 | the section to -u_ar0). GDB uses this info to locate the |
| 268 | regs, using minor trickery to get around the |
| 269 | offset-or-absolute-addr problem. */ |
| 270 | |
| 271 | chead.cs_vaddr = 0 - (bfd_vma) u->u_ar0; |
| 272 | |
| 273 | secname = ".reg"; |
| 274 | flags = SEC_HAS_CONTENTS; |
| 275 | |
| 276 | break; |
| 277 | case CORES_PREGION: /* A program region, map it */ |
| 278 | switch (chead.cs_x.csx_preg.csxp_rtyp) |
| 279 | { |
| 280 | case PT_DATA: |
| 281 | secname = ".data"; /* Data region. */ |
| 282 | break; |
| 283 | case PT_STACK: |
| 284 | secname = ".stack"; /* Stack region. */ |
| 285 | break; |
| 286 | case PT_SHMEM: |
| 287 | secname = ".shmem"; /* Shared memory */ |
| 288 | break; |
| 289 | case PT_LIBDAT: |
| 290 | secname = ".libdat"; /* Shared library data */ |
| 291 | break; |
| 292 | case PT_V86: |
| 293 | secname = ".virt86"; /* Virtual 8086 mode */ |
| 294 | break; |
| 295 | case PT_SHFIL: |
| 296 | secname = ".mmfile"; /* Memory mapped file */ |
| 297 | break; |
| 298 | case PT_XDATA0: |
| 299 | secname = ".Xdat0"; /* XENIX data region, virtual 0 */ |
| 300 | break; |
| 301 | default: |
| 302 | secname = ""; |
| 303 | } |
| 304 | flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS; |
| 305 | break; |
| 306 | case CORES_PROC: /* struct proc */ |
| 307 | case CORES_ITIMER: /* interval timers */ |
| 308 | case CORES_SCOUTSNAME: /* struct scoutsname */ |
| 309 | secname = NULL; /* Ignore these */ |
| 310 | break; |
| 311 | default: |
| 312 | (*_bfd_error_handler) ("Unhandled SCO core file section type %d\n", |
| 313 | chead.cs_stype); |
| 314 | continue; |
| 315 | } |
| 316 | |
| 317 | if (secname |
| 318 | && !make_bfd_asection (abfd, secname, flags, |
| 319 | (bfd_size_type) chead.cs_vsize, |
| 320 | (bfd_vma) chead.cs_vaddr, |
| 321 | (file_ptr) chead.cs_sseek)) |
| 322 | return NULL; |
| 323 | |
| 324 | } |
| 325 | |
| 326 | return abfd->xvec; |
| 327 | |
| 328 | } |
| 329 | |
| 330 | char * |
| 331 | sco5_core_file_failing_command (abfd) |
| 332 | bfd *abfd; |
| 333 | { |
| 334 | char *com = abfd->tdata.sco5_core_data->u.u_comm; |
| 335 | if (*com) |
| 336 | return com; |
| 337 | else |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | /* ARGSUSED */ |
| 342 | int |
| 343 | sco5_core_file_failing_signal (ignore_abfd) |
| 344 | bfd *ignore_abfd; |
| 345 | { |
| 346 | return ((ignore_abfd->tdata.sco5_core_data->u.u_sysabort != 0) |
| 347 | ? ignore_abfd->tdata.sco5_core_data->u.u_sysabort |
| 348 | : -1); |
| 349 | } |
| 350 | |
| 351 | /* ARGSUSED */ |
| 352 | boolean |
| 353 | sco5_core_file_matches_executable_p (core_bfd, exec_bfd) |
| 354 | bfd *core_bfd, *exec_bfd; |
| 355 | { |
| 356 | return true; /* FIXME, We have no way of telling at this point */ |
| 357 | } |
| 358 | |
| 359 | #define sco5_core_get_symtab_upper_bound _bfd_nosymbols_get_symtab_upper_bound |
| 360 | #define sco5_core_get_symtab _bfd_nosymbols_get_symtab |
| 361 | #define sco5_core_print_symbol _bfd_nosymbols_print_symbol |
| 362 | #define sco5_core_get_symbol_info _bfd_nosymbols_get_symbol_info |
| 363 | #define sco5_core_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name |
| 364 | #define sco5_core_get_lineno _bfd_nosymbols_get_lineno |
| 365 | #define sco5_core_find_nearest_line _bfd_nosymbols_find_nearest_line |
| 366 | #define sco5_core_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol |
| 367 | #define sco5_core_read_minisymbols _bfd_nosymbols_read_minisymbols |
| 368 | #define sco5_core_minisymbol_to_symbol _bfd_nosymbols_minisymbol_to_symbol |
| 369 | |
| 370 | /* If somebody calls any byte-swapping routines, shoot them. */ |
| 371 | static void |
| 372 | swap_abort () |
| 373 | { |
| 374 | abort (); /* This way doesn't require any declaration for ANSI to fuck up */ |
| 375 | } |
| 376 | #define NO_GET ((bfd_vma (*) PARAMS (( const bfd_byte *))) swap_abort ) |
| 377 | #define NO_PUT ((void (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort ) |
| 378 | #define NO_SIGNED_GET \ |
| 379 | ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort ) |
| 380 | |
| 381 | const bfd_target sco5_core_vec = |
| 382 | { |
| 383 | "sco5-core", |
| 384 | bfd_target_unknown_flavour, |
| 385 | BFD_ENDIAN_LITTLE, /* target byte order */ |
| 386 | BFD_ENDIAN_LITTLE, /* target headers byte order */ |
| 387 | (HAS_RELOC | EXEC_P | /* object flags */ |
| 388 | HAS_LINENO | HAS_DEBUG | |
| 389 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), |
| 390 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ |
| 391 | 0, /* symbol prefix */ |
| 392 | ' ', /* ar_pad_char */ |
| 393 | 16, /* ar_max_namelen */ |
| 394 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit data */ |
| 395 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit data */ |
| 396 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit data */ |
| 397 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 64 bit hdrs */ |
| 398 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 32 bit hdrs */ |
| 399 | NO_GET, NO_SIGNED_GET, NO_PUT, /* 16 bit hdrs */ |
| 400 | |
| 401 | { /* bfd_check_format */ |
| 402 | _bfd_dummy_target, /* unknown format */ |
| 403 | _bfd_dummy_target, /* object file */ |
| 404 | _bfd_dummy_target, /* archive */ |
| 405 | sco5_core_file_p /* a core file */ |
| 406 | }, |
| 407 | { /* bfd_set_format */ |
| 408 | bfd_false, bfd_false, |
| 409 | bfd_false, bfd_false |
| 410 | }, |
| 411 | { /* bfd_write_contents */ |
| 412 | bfd_false, bfd_false, |
| 413 | bfd_false, bfd_false |
| 414 | }, |
| 415 | |
| 416 | BFD_JUMP_TABLE_GENERIC (_bfd_generic), |
| 417 | BFD_JUMP_TABLE_COPY (_bfd_generic), |
| 418 | BFD_JUMP_TABLE_CORE (sco5), |
| 419 | BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive), |
| 420 | BFD_JUMP_TABLE_SYMBOLS (sco5_core), |
| 421 | BFD_JUMP_TABLE_RELOCS (_bfd_norelocs), |
| 422 | BFD_JUMP_TABLE_WRITE (_bfd_generic), |
| 423 | BFD_JUMP_TABLE_LINK (_bfd_nolink), |
| 424 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
| 425 | |
| 426 | NULL, |
| 427 | |
| 428 | (PTR) 0 /* backend_data */ |
| 429 | }; |