sco5-core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /* BFD back end for SCO5 core files (U-area and raw sections)
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Written by Jouke Numan <jnuman@hiscom.nl>
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "libaout.h" /* BFD a.out internal data structures */
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/param.h>
  24. #include <sys/dir.h>
  25. #include <signal.h>
  26. #include <sys/user.h> /* After a.out.h */
  27. #ifdef SCO5_CORE
  28. #include <sys/paccess.h>
  29. #include <sys/region.h>
  30. #endif
  31. struct sco5_core_struct
  32. {
  33. struct user u;
  34. };
  35. /* forward declarations */
  36. #define sco5_core_file_matches_executable_p generic_core_file_matches_executable_p
  37. #define sco5_core_file_pid _bfd_nocore_core_file_pid
  38. static asection *
  39. make_bfd_asection (bfd *abfd,
  40. const char *name,
  41. flagword flags,
  42. bfd_size_type size,
  43. bfd_vma vma,
  44. file_ptr filepos)
  45. {
  46. asection *asect;
  47. asect = bfd_make_section_anyway_with_flags (abfd, name, flags);
  48. if (!asect)
  49. return NULL;
  50. asect->size = size;
  51. asect->vma = vma;
  52. asect->filepos = filepos;
  53. asect->alignment_power = 2;
  54. return asect;
  55. }
  56. static struct user *
  57. read_uarea (bfd *abfd, int filepos)
  58. {
  59. struct sco5_core_struct *rawptr;
  60. size_t amt = sizeof (struct sco5_core_struct);
  61. rawptr = (struct sco5_core_struct *) bfd_zmalloc (amt);
  62. if (rawptr == NULL)
  63. return NULL;
  64. abfd->tdata.sco5_core_data = rawptr;
  65. if (bfd_seek (abfd, (file_ptr) filepos, SEEK_SET) != 0
  66. || bfd_bread ((void *) &rawptr->u, (bfd_size_type) sizeof rawptr->u,
  67. abfd) != sizeof rawptr->u)
  68. {
  69. bfd_set_error (bfd_error_wrong_format);
  70. return NULL;
  71. }
  72. /* Sanity check perhaps??? */
  73. if (rawptr->u.u_dsize > 0x1000000) /* Remember, it's in pages... */
  74. {
  75. bfd_set_error (bfd_error_wrong_format);
  76. return NULL;
  77. }
  78. if (rawptr->u.u_ssize > 0x1000000)
  79. {
  80. bfd_set_error (bfd_error_wrong_format);
  81. return NULL;
  82. }
  83. return &rawptr->u;
  84. }
  85. bfd_cleanup
  86. sco5_core_file_p (bfd *abfd)
  87. {
  88. int coffset_siz, val, nsecs, cheadoffs;
  89. int coresize;
  90. struct user *u;
  91. struct coreoffsets coffsets;
  92. struct coresecthead chead;
  93. char *secname;
  94. flagword flags;
  95. /* Read coreoffsets region at end of core (see core(FP)). */
  96. {
  97. struct stat statbuf;
  98. if (bfd_stat (abfd, &statbuf) < 0)
  99. return NULL;
  100. coresize = statbuf.st_size;
  101. }
  102. /* Last long in core is sizeof struct coreoffsets, read it */
  103. if ((bfd_seek (abfd, (file_ptr) (coresize - sizeof coffset_siz),
  104. SEEK_SET) != 0)
  105. || bfd_bread ((void *) &coffset_siz, (bfd_size_type) sizeof coffset_siz,
  106. abfd) != sizeof coffset_siz)
  107. {
  108. bfd_set_error (bfd_error_wrong_format);
  109. return NULL;
  110. }
  111. /* Use it to seek start of coreoffsets region, read it and determine
  112. validity */
  113. if ((bfd_seek (abfd, (file_ptr) (coresize - coffset_siz), SEEK_SET) != 0)
  114. || (bfd_bread ((void *) &coffsets, (bfd_size_type) sizeof coffsets, abfd)
  115. != sizeof coffsets)
  116. || ((coffsets.u_info != 1) && (coffsets.u_info != C_VERSION)))
  117. {
  118. bfd_set_error (bfd_error_wrong_format);
  119. return NULL;
  120. }
  121. if (coffsets.u_info == 1)
  122. {
  123. /* Old version, no section heads, read info from user struct */
  124. u = read_uarea (abfd, coffsets.u_user);
  125. if (! u)
  126. goto fail;
  127. if (!make_bfd_asection (abfd, ".reg", SEC_HAS_CONTENTS,
  128. (bfd_size_type) coffsets.u_usize,
  129. 0 - (bfd_vma) u->u_ar0,
  130. (file_ptr) coffsets.u_user))
  131. goto fail;
  132. if (!make_bfd_asection (abfd, ".data",
  133. SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
  134. ((bfd_size_type) u->u_exdata.ux_dsize
  135. + u->u_exdata.ux_bsize),
  136. (bfd_vma) u->u_exdata.ux_datorg,
  137. (file_ptr) coffsets.u_data))
  138. goto fail;
  139. if (!make_bfd_asection (abfd, ".stack",
  140. SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
  141. (bfd_size_type) u->u_ssize * NBPC,
  142. (bfd_vma) u->u_sub,
  143. (file_ptr) coffsets.u_stack))
  144. goto fail;
  145. return _bfd_no_cleanup; /* Done for version 1 */
  146. }
  147. /* Immediately before coreoffsets region is a long with offset in core
  148. to first coresecthead (CORES_OFFSETS), the long before this is the
  149. number of section heads in the list. Read both longs and read the
  150. coresecthead and check its validity */
  151. if ((bfd_seek (abfd,
  152. (file_ptr) (coresize - coffset_siz - 2 * sizeof coffset_siz),
  153. SEEK_SET) != 0)
  154. || (bfd_bread ((void *) &nsecs, (bfd_size_type) sizeof nsecs, abfd)
  155. != sizeof nsecs)
  156. || (bfd_bread ((void *) &cheadoffs, (bfd_size_type) sizeof cheadoffs,
  157. abfd) != sizeof cheadoffs)
  158. || (bfd_seek (abfd, (file_ptr) cheadoffs, SEEK_SET) != 0)
  159. || (bfd_bread ((void *) &chead, (bfd_size_type) sizeof chead, abfd)
  160. != sizeof chead)
  161. || (chead.cs_stype != CORES_OFFSETS)
  162. || (chead.cs_x.csx_magic != COREMAGIC_NUMBER))
  163. {
  164. bfd_set_error (bfd_error_wrong_format);
  165. goto fail;
  166. }
  167. /* OK, we believe you. You're a core file (sure, sure). */
  168. /* Now loop over all regions and map them */
  169. nsecs--; /* We've seen CORES_OFFSETS already */
  170. for (; nsecs; nsecs--)
  171. {
  172. if ((bfd_seek (abfd, (file_ptr) chead.cs_hseek, SEEK_SET) != 0)
  173. || (bfd_bread ((void *) &chead, (bfd_size_type) sizeof chead, abfd)
  174. != sizeof chead))
  175. {
  176. bfd_set_error (bfd_error_wrong_format);
  177. goto fail;
  178. }
  179. switch (chead.cs_stype)
  180. {
  181. case CORES_MAGIC: /* Core header, check magic */
  182. if (chead.cs_x.csx_magic != COREMAGIC_NUMBER)
  183. {
  184. bfd_set_error (bfd_error_wrong_format);
  185. goto fail;
  186. }
  187. secname = NULL;
  188. nsecs++; /* MAGIC not in section cnt!*/
  189. break;
  190. case CORES_UAREA: /* U-area, read in tdata */
  191. u = read_uarea (abfd, chead.cs_sseek);
  192. if (! u)
  193. goto fail;
  194. /* This is tricky. As the "register section", we give them
  195. the entire upage and stack. u.u_ar0 points to where
  196. "register 0" is stored. There are two tricks with this,
  197. though. One is that the rest of the registers might be
  198. at positive or negative (or both) displacements from
  199. *u_ar0. The other is that u_ar0 is sometimes an absolute
  200. address in kernel memory, and on other systems it is an
  201. offset from the beginning of the `struct user'.
  202. As a practical matter, we don't know where the registers
  203. actually are, so we have to pass the whole area to GDB.
  204. We encode the value of u_ar0 by setting the .regs section
  205. up so that its virtual memory address 0 is at the place
  206. pointed to by u_ar0 (by setting the vma of the start of
  207. the section to -u_ar0). GDB uses this info to locate the
  208. regs, using minor trickery to get around the
  209. offset-or-absolute-addr problem. */
  210. chead.cs_vaddr = 0 - (bfd_vma) u->u_ar0;
  211. secname = ".reg";
  212. flags = SEC_HAS_CONTENTS;
  213. break;
  214. case CORES_PREGION: /* A program region, map it */
  215. switch (chead.cs_x.csx_preg.csxp_rtyp)
  216. {
  217. case PT_DATA:
  218. secname = ".data"; /* Data region. */
  219. break;
  220. case PT_STACK:
  221. secname = ".stack"; /* Stack region. */
  222. break;
  223. case PT_SHMEM:
  224. secname = ".shmem"; /* Shared memory */
  225. break;
  226. case PT_LIBDAT:
  227. secname = ".libdat"; /* Shared library data */
  228. break;
  229. case PT_V86:
  230. secname = ".virt86"; /* Virtual 8086 mode */
  231. break;
  232. case PT_SHFIL:
  233. secname = ".mmfile"; /* Memory mapped file */
  234. break;
  235. case PT_XDATA0:
  236. secname = ".Xdat0"; /* XENIX data region, virtual 0 */
  237. break;
  238. default:
  239. secname = "";
  240. }
  241. flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
  242. break;
  243. case CORES_PROC: /* struct proc */
  244. case CORES_ITIMER: /* interval timers */
  245. case CORES_SCOUTSNAME: /* struct scoutsname */
  246. secname = NULL; /* Ignore these */
  247. break;
  248. default:
  249. _bfd_error_handler ("Unhandled SCO core file section type %d\n",
  250. chead.cs_stype);
  251. continue;
  252. }
  253. if (secname
  254. && !make_bfd_asection (abfd, secname, flags,
  255. (bfd_size_type) chead.cs_vsize,
  256. (bfd_vma) chead.cs_vaddr,
  257. (file_ptr) chead.cs_sseek))
  258. goto fail;
  259. }
  260. return _bfd_no_cleanup;
  261. fail:
  262. if (abfd->tdata.any)
  263. {
  264. bfd_release (abfd, abfd->tdata.any);
  265. abfd->tdata.any = NULL;
  266. }
  267. bfd_section_list_clear (abfd);
  268. return NULL;
  269. }
  270. char *
  271. sco5_core_file_failing_command (bfd *abfd)
  272. {
  273. char *com = abfd->tdata.sco5_core_data->u.u_comm;
  274. if (*com)
  275. return com;
  276. else
  277. return NULL;
  278. }
  279. int
  280. sco5_core_file_failing_signal (bfd *ignore_abfd)
  281. {
  282. return ((ignore_abfd->tdata.sco5_core_data->u.u_sysabort != 0)
  283. ? ignore_abfd->tdata.sco5_core_data->u.u_sysabort
  284. : -1);
  285. }
  286. /* If somebody calls any byte-swapping routines, shoot them. */
  287. static void
  288. swap_abort (void)
  289. {
  290. abort (); /* This way doesn't require any declaration for ANSI to fuck up */
  291. }
  292. #define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
  293. #define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
  294. #define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
  295. #define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
  296. #define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
  297. #define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
  298. const bfd_target core_sco5_vec =
  299. {
  300. "sco5-core",
  301. bfd_target_unknown_flavour,
  302. BFD_ENDIAN_LITTLE, /* target byte order */
  303. BFD_ENDIAN_LITTLE, /* target headers byte order */
  304. (HAS_RELOC | EXEC_P | /* object flags */
  305. HAS_LINENO | HAS_DEBUG |
  306. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  307. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  308. 0, /* symbol prefix */
  309. ' ', /* ar_pad_char */
  310. 16, /* ar_max_namelen */
  311. 0, /* match priority. */
  312. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  313. NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
  314. NO_GET, NO_GETS, NO_PUT, /* 32 bit data */
  315. NO_GET, NO_GETS, NO_PUT, /* 16 bit data */
  316. NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
  317. NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */
  318. NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */
  319. { /* bfd_check_format */
  320. _bfd_dummy_target, /* unknown format */
  321. _bfd_dummy_target, /* object file */
  322. _bfd_dummy_target, /* archive */
  323. sco5_core_file_p /* a core file */
  324. },
  325. { /* bfd_set_format */
  326. _bfd_bool_bfd_false_error,
  327. _bfd_bool_bfd_false_error,
  328. _bfd_bool_bfd_false_error,
  329. _bfd_bool_bfd_false_error
  330. },
  331. { /* bfd_write_contents */
  332. _bfd_bool_bfd_false_error,
  333. _bfd_bool_bfd_false_error,
  334. _bfd_bool_bfd_false_error,
  335. _bfd_bool_bfd_false_error
  336. },
  337. BFD_JUMP_TABLE_GENERIC (_bfd_generic),
  338. BFD_JUMP_TABLE_COPY (_bfd_generic),
  339. BFD_JUMP_TABLE_CORE (sco5),
  340. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  341. BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
  342. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  343. BFD_JUMP_TABLE_WRITE (_bfd_generic),
  344. BFD_JUMP_TABLE_LINK (_bfd_nolink),
  345. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  346. NULL,
  347. NULL /* backend_data */
  348. };