irix-core.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* BFD back-end for Irix core files.
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. Written by Stu Grossman, Cygnus Support.
  4. Converted to back-end form by Ian Lance Taylor, Cygnus Support
  5. This file is part of BFD, the Binary File Descriptor library.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. /* This file can only be compiled on systems which use Irix style core
  19. files (namely, Irix 4 and Irix 5, so far). */
  20. #include "sysdep.h"
  21. #include "bfd.h"
  22. #include "libbfd.h"
  23. #ifdef IRIX_CORE
  24. #include <core.out.h>
  25. struct sgi_core_struct
  26. {
  27. int sig;
  28. char cmd[CORE_NAMESIZE];
  29. };
  30. #define core_hdr(bfd) ((bfd)->tdata.sgi_core_data)
  31. #define core_signal(bfd) (core_hdr(bfd)->sig)
  32. #define core_command(bfd) (core_hdr(bfd)->cmd)
  33. #define irix_core_core_file_matches_executable_p generic_core_file_matches_executable_p
  34. #define irix_core_core_file_pid _bfd_nocore_core_file_pid
  35. static asection *make_bfd_asection
  36. (bfd *, const char *, flagword, bfd_size_type, bfd_vma, file_ptr);
  37. /* Helper function for irix_core_core_file_p:
  38. 32-bit and 64-bit versions. */
  39. #ifdef CORE_MAGIC64
  40. static int
  41. do_sections64 (bfd *abfd, struct coreout *coreout)
  42. {
  43. struct vmap64 vmap;
  44. char *secname;
  45. int i, val;
  46. for (i = 0; i < coreout->c_nvmap; i++)
  47. {
  48. val = bfd_bread (&vmap, (bfd_size_type) sizeof vmap, abfd);
  49. if (val != sizeof vmap)
  50. break;
  51. switch (vmap.v_type)
  52. {
  53. case VDATA:
  54. secname = ".data";
  55. break;
  56. case VSTACK:
  57. secname = ".stack";
  58. break;
  59. #ifdef VMAPFILE
  60. case VMAPFILE:
  61. secname = ".mapfile";
  62. break;
  63. #endif
  64. default:
  65. continue;
  66. }
  67. /* A file offset of zero means that the
  68. section is not contained in the corefile. */
  69. if (vmap.v_offset == 0)
  70. continue;
  71. if (!make_bfd_asection (abfd, secname,
  72. SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
  73. vmap.v_len, vmap.v_vaddr, vmap.v_offset))
  74. /* Fail. */
  75. return 0;
  76. }
  77. return 1;
  78. }
  79. #endif
  80. /* 32-bit version. */
  81. static int
  82. do_sections (bfd *abfd, struct coreout *coreout)
  83. {
  84. struct vmap vmap;
  85. char *secname;
  86. int i, val;
  87. for (i = 0; i < coreout->c_nvmap; i++)
  88. {
  89. val = bfd_bread (&vmap, (bfd_size_type) sizeof vmap, abfd);
  90. if (val != sizeof vmap)
  91. break;
  92. switch (vmap.v_type)
  93. {
  94. case VDATA:
  95. secname = ".data";
  96. break;
  97. case VSTACK:
  98. secname = ".stack";
  99. break;
  100. #ifdef VMAPFILE
  101. case VMAPFILE:
  102. secname = ".mapfile";
  103. break;
  104. #endif
  105. default:
  106. continue;
  107. }
  108. /* A file offset of zero means that the
  109. section is not contained in the corefile. */
  110. if (vmap.v_offset == 0)
  111. continue;
  112. if (!make_bfd_asection (abfd, secname,
  113. SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
  114. vmap.v_len, vmap.v_vaddr, vmap.v_offset))
  115. /* Fail. */
  116. return 0;
  117. }
  118. return 1;
  119. }
  120. static asection *
  121. make_bfd_asection (bfd *abfd,
  122. const char *name,
  123. flagword flags,
  124. bfd_size_type size,
  125. bfd_vma vma,
  126. file_ptr filepos)
  127. {
  128. asection *asect;
  129. asect = bfd_make_section_anyway_with_flags (abfd, name, flags);
  130. if (!asect)
  131. return NULL;
  132. asect->size = size;
  133. asect->vma = vma;
  134. asect->filepos = filepos;
  135. asect->alignment_power = 4;
  136. return asect;
  137. }
  138. static bfd_cleanup
  139. irix_core_core_file_p (bfd *abfd)
  140. {
  141. int val;
  142. struct coreout coreout;
  143. struct idesc *idg, *idf, *ids;
  144. size_t amt;
  145. val = bfd_bread (&coreout, (bfd_size_type) sizeof coreout, abfd);
  146. if (val != sizeof coreout)
  147. {
  148. if (bfd_get_error () != bfd_error_system_call)
  149. bfd_set_error (bfd_error_wrong_format);
  150. return 0;
  151. }
  152. if (coreout.c_version != CORE_VERSION1)
  153. return 0;
  154. /* Have we got a corefile? */
  155. switch (coreout.c_magic)
  156. {
  157. case CORE_MAGIC: break;
  158. #ifdef CORE_MAGIC64
  159. case CORE_MAGIC64: break;
  160. #endif
  161. #ifdef CORE_MAGICN32
  162. case CORE_MAGICN32: break;
  163. #endif
  164. default: return 0; /* Un-identifiable or not corefile. */
  165. }
  166. amt = sizeof (struct sgi_core_struct);
  167. core_hdr (abfd) = (struct sgi_core_struct *) bfd_zalloc (abfd, amt);
  168. if (!core_hdr (abfd))
  169. return NULL;
  170. strncpy (core_command (abfd), coreout.c_name, CORE_NAMESIZE);
  171. core_signal (abfd) = coreout.c_sigcause;
  172. if (bfd_seek (abfd, coreout.c_vmapoffset, SEEK_SET) != 0)
  173. goto fail;
  174. /* Process corefile sections. */
  175. #ifdef CORE_MAGIC64
  176. if (coreout.c_magic == (int) CORE_MAGIC64)
  177. {
  178. if (! do_sections64 (abfd, & coreout))
  179. goto fail;
  180. }
  181. else
  182. #endif
  183. if (! do_sections (abfd, & coreout))
  184. goto fail;
  185. /* Make sure that the regs are contiguous within the core file. */
  186. idg = &coreout.c_idesc[I_GPREGS];
  187. idf = &coreout.c_idesc[I_FPREGS];
  188. ids = &coreout.c_idesc[I_SPECREGS];
  189. if (idg->i_offset + idg->i_len != idf->i_offset
  190. || idf->i_offset + idf->i_len != ids->i_offset)
  191. goto fail; /* Can't deal with non-contig regs */
  192. if (bfd_seek (abfd, idg->i_offset, SEEK_SET) != 0)
  193. goto fail;
  194. if (!make_bfd_asection (abfd, ".reg",
  195. SEC_HAS_CONTENTS,
  196. idg->i_len + idf->i_len + ids->i_len,
  197. 0,
  198. idg->i_offset))
  199. goto fail;
  200. /* OK, we believe you. You're a core file (sure, sure). */
  201. bfd_default_set_arch_mach (abfd, bfd_arch_mips, 0);
  202. return _bfd_no_cleanup;
  203. fail:
  204. bfd_release (abfd, core_hdr (abfd));
  205. core_hdr (abfd) = NULL;
  206. bfd_section_list_clear (abfd);
  207. return NULL;
  208. }
  209. static char *
  210. irix_core_core_file_failing_command (bfd *abfd)
  211. {
  212. return core_command (abfd);
  213. }
  214. static int
  215. irix_core_core_file_failing_signal (bfd *abfd)
  216. {
  217. return core_signal (abfd);
  218. }
  219. /* If somebody calls any byte-swapping routines, shoot them. */
  220. static void
  221. swap_abort(void)
  222. {
  223. abort(); /* This way doesn't require any declaration for ANSI to fuck up */
  224. }
  225. #define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
  226. #define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
  227. #define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
  228. #define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
  229. #define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
  230. #define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
  231. const bfd_target core_irix_vec =
  232. {
  233. "irix-core",
  234. bfd_target_unknown_flavour,
  235. BFD_ENDIAN_BIG, /* target byte order */
  236. BFD_ENDIAN_BIG, /* target headers byte order */
  237. (HAS_RELOC | EXEC_P | /* object flags */
  238. HAS_LINENO | HAS_DEBUG |
  239. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  240. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  241. 0, /* symbol prefix */
  242. ' ', /* ar_pad_char */
  243. 16, /* ar_max_namelen */
  244. 0, /* match_priority */
  245. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  246. NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
  247. NO_GET, NO_GETS, NO_PUT, /* 32 bit data */
  248. NO_GET, NO_GETS, NO_PUT, /* 16 bit data */
  249. NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
  250. NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */
  251. NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */
  252. { /* bfd_check_format */
  253. _bfd_dummy_target, /* unknown format */
  254. _bfd_dummy_target, /* object file */
  255. _bfd_dummy_target, /* archive */
  256. irix_core_core_file_p /* a core file */
  257. },
  258. { /* bfd_set_format */
  259. _bfd_bool_bfd_false_error,
  260. _bfd_bool_bfd_false_error,
  261. _bfd_bool_bfd_false_error,
  262. _bfd_bool_bfd_false_error
  263. },
  264. { /* bfd_write_contents */
  265. _bfd_bool_bfd_false_error,
  266. _bfd_bool_bfd_false_error,
  267. _bfd_bool_bfd_false_error,
  268. _bfd_bool_bfd_false_error
  269. },
  270. BFD_JUMP_TABLE_GENERIC (_bfd_generic),
  271. BFD_JUMP_TABLE_COPY (_bfd_generic),
  272. BFD_JUMP_TABLE_CORE (irix_core),
  273. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  274. BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
  275. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  276. BFD_JUMP_TABLE_WRITE (_bfd_generic),
  277. BFD_JUMP_TABLE_LINK (_bfd_nolink),
  278. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  279. NULL,
  280. NULL /* backend_data */
  281. };
  282. #endif /* IRIX_CORE */