netbsd-core.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* BFD back end for NetBSD style core files
  2. Copyright (C) 1988-2022 Free Software Foundation, Inc.
  3. Written by Paul Kranenburg, EUR
  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 <sys/param.h>
  22. #include <sys/dir.h>
  23. #include <signal.h>
  24. #include <sys/core.h>
  25. /* The machine ID for OpenBSD/sparc64 and older versions of
  26. NetBSD/sparc64 overlaps with M_MIPS1. */
  27. #define M_SPARC64_OPENBSD M_MIPS1
  28. /* Offset of StackGhost cookie within `struct md_coredump' on
  29. OpenBSD/sparc. */
  30. #define SPARC_WCOOKIE_OFFSET 344
  31. /* Offset of StackGhost cookie within `struct md_coredump' on
  32. OpenBSD/sparc64. */
  33. #define SPARC64_WCOOKIE_OFFSET 832
  34. #define netbsd_core_file_matches_executable_p generic_core_file_matches_executable_p
  35. #define netbsd_core_file_pid _bfd_nocore_core_file_pid
  36. struct netbsd_core_struct
  37. {
  38. struct core core;
  39. } *rawptr;
  40. /* Handle NetBSD-style core dump file. */
  41. static bfd_cleanup
  42. netbsd_core_file_p (bfd *abfd)
  43. {
  44. int val;
  45. unsigned i;
  46. file_ptr offset;
  47. asection *asect;
  48. struct core core;
  49. struct coreseg coreseg;
  50. size_t amt = sizeof core;
  51. val = bfd_bread (&core, amt, abfd);
  52. if (val != sizeof core)
  53. {
  54. /* Too small to be a core file. */
  55. bfd_set_error (bfd_error_wrong_format);
  56. return 0;
  57. }
  58. if (CORE_GETMAGIC (core) != COREMAGIC)
  59. {
  60. bfd_set_error (bfd_error_wrong_format);
  61. return 0;
  62. }
  63. amt = sizeof (struct netbsd_core_struct);
  64. rawptr = (struct netbsd_core_struct *) bfd_zalloc (abfd, amt);
  65. if (rawptr == NULL)
  66. return 0;
  67. rawptr->core = core;
  68. abfd->tdata.netbsd_core_data = rawptr;
  69. offset = core.c_hdrsize;
  70. for (i = 0; i < core.c_nseg; i++)
  71. {
  72. const char *sname;
  73. flagword flags;
  74. if (bfd_seek (abfd, offset, SEEK_SET) != 0)
  75. goto punt;
  76. val = bfd_bread (&coreseg, sizeof coreseg, abfd);
  77. if (val != sizeof coreseg)
  78. {
  79. bfd_set_error (bfd_error_file_truncated);
  80. goto punt;
  81. }
  82. if (CORE_GETMAGIC (coreseg) != CORESEGMAGIC)
  83. {
  84. bfd_set_error (bfd_error_wrong_format);
  85. goto punt;
  86. }
  87. offset += core.c_seghdrsize;
  88. switch (CORE_GETFLAG (coreseg))
  89. {
  90. case CORE_CPU:
  91. sname = ".reg";
  92. flags = SEC_ALLOC + SEC_HAS_CONTENTS;
  93. break;
  94. case CORE_DATA:
  95. sname = ".data";
  96. flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
  97. break;
  98. case CORE_STACK:
  99. sname = ".stack";
  100. flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
  101. break;
  102. default:
  103. sname = ".unknown";
  104. flags = SEC_ALLOC + SEC_HAS_CONTENTS;
  105. break;
  106. }
  107. asect = bfd_make_section_anyway_with_flags (abfd, sname, flags);
  108. if (asect == NULL)
  109. goto punt;
  110. asect->size = coreseg.c_size;
  111. asect->vma = coreseg.c_addr;
  112. asect->filepos = offset;
  113. asect->alignment_power = 2;
  114. if (CORE_GETFLAG (coreseg) == CORE_CPU)
  115. {
  116. bfd_size_type wcookie_offset;
  117. switch (CORE_GETMID (core))
  118. {
  119. case M_SPARC_NETBSD:
  120. wcookie_offset = SPARC_WCOOKIE_OFFSET;
  121. break;
  122. case M_SPARC64_OPENBSD:
  123. wcookie_offset = SPARC64_WCOOKIE_OFFSET;
  124. break;
  125. default:
  126. wcookie_offset = 0;
  127. break;
  128. }
  129. if (wcookie_offset > 0 && coreseg.c_size > wcookie_offset)
  130. {
  131. /* Truncate the .reg section. */
  132. asect->size = wcookie_offset;
  133. /* And create the .wcookie section. */
  134. flags = SEC_ALLOC + SEC_HAS_CONTENTS;
  135. asect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
  136. flags);
  137. if (asect == NULL)
  138. goto punt;
  139. asect->size = coreseg.c_size - wcookie_offset;
  140. asect->vma = 0;
  141. asect->filepos = offset + wcookie_offset;
  142. asect->alignment_power = 2;
  143. }
  144. }
  145. offset += coreseg.c_size;
  146. }
  147. /* Set architecture from machine ID. */
  148. switch (CORE_GETMID (core))
  149. {
  150. case M_ALPHA_NETBSD:
  151. bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
  152. break;
  153. case M_ARM6_NETBSD:
  154. bfd_default_set_arch_mach (abfd, bfd_arch_arm, bfd_mach_arm_3);
  155. break;
  156. case M_X86_64_NETBSD:
  157. bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
  158. break;
  159. case M_386_NETBSD:
  160. bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386);
  161. break;
  162. case M_68K_NETBSD:
  163. case M_68K4K_NETBSD:
  164. bfd_default_set_arch_mach (abfd, bfd_arch_m68k, 0);
  165. break;
  166. case M_HPPA_OPENBSD:
  167. bfd_default_set_arch_mach (abfd, bfd_arch_hppa, bfd_mach_hppa11);
  168. break;
  169. case M_POWERPC_NETBSD:
  170. bfd_default_set_arch_mach (abfd, bfd_arch_powerpc, bfd_mach_ppc);
  171. break;
  172. case M_SPARC_NETBSD:
  173. bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc);
  174. break;
  175. case M_SPARC64_NETBSD:
  176. case M_SPARC64_OPENBSD:
  177. bfd_default_set_arch_mach (abfd, bfd_arch_sparc, bfd_mach_sparc_v9);
  178. break;
  179. case M_VAX_NETBSD:
  180. case M_VAX4K_NETBSD:
  181. bfd_default_set_arch_mach (abfd, bfd_arch_vax, 0);
  182. break;
  183. }
  184. /* OK, we believe you. You're a core file (sure, sure). */
  185. return _bfd_no_cleanup;
  186. punt:
  187. bfd_release (abfd, abfd->tdata.any);
  188. abfd->tdata.any = NULL;
  189. bfd_section_list_clear (abfd);
  190. return 0;
  191. }
  192. static char*
  193. netbsd_core_file_failing_command (bfd *abfd)
  194. {
  195. /*return core_command (abfd);*/
  196. return abfd->tdata.netbsd_core_data->core.c_name;
  197. }
  198. static int
  199. netbsd_core_file_failing_signal (bfd *abfd)
  200. {
  201. /*return core_signal (abfd);*/
  202. return abfd->tdata.netbsd_core_data->core.c_signo;
  203. }
  204. /* If somebody calls any byte-swapping routines, shoot them. */
  205. static void
  206. swap_abort (void)
  207. {
  208. /* This way doesn't require any declaration for ANSI to fuck up. */
  209. abort ();
  210. }
  211. #define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
  212. #define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
  213. #define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
  214. #define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
  215. #define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
  216. #define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
  217. const bfd_target core_netbsd_vec =
  218. {
  219. "netbsd-core",
  220. bfd_target_unknown_flavour,
  221. BFD_ENDIAN_UNKNOWN, /* Target byte order. */
  222. BFD_ENDIAN_UNKNOWN, /* Target headers byte order. */
  223. (HAS_RELOC | EXEC_P | /* Object flags. */
  224. HAS_LINENO | HAS_DEBUG |
  225. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  226. (SEC_HAS_CONTENTS | /* Section flags. */
  227. SEC_ALLOC | SEC_LOAD | SEC_RELOC),
  228. 0, /* Symbol prefix. */
  229. ' ', /* ar_pad_char. */
  230. 16, /* ar_max_namelen. */
  231. 0, /* Match priority. */
  232. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  233. NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data. */
  234. NO_GET, NO_GETS, NO_PUT, /* 32 bit data. */
  235. NO_GET, NO_GETS, NO_PUT, /* 16 bit data. */
  236. NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs. */
  237. NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs. */
  238. NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs. */
  239. { /* bfd_check_format. */
  240. _bfd_dummy_target, /* Unknown format. */
  241. _bfd_dummy_target, /* Object file. */
  242. _bfd_dummy_target, /* Archive. */
  243. netbsd_core_file_p /* A core file. */
  244. },
  245. { /* bfd_set_format. */
  246. _bfd_bool_bfd_false_error,
  247. _bfd_bool_bfd_false_error,
  248. _bfd_bool_bfd_false_error,
  249. _bfd_bool_bfd_false_error
  250. },
  251. { /* bfd_write_contents. */
  252. _bfd_bool_bfd_false_error,
  253. _bfd_bool_bfd_false_error,
  254. _bfd_bool_bfd_false_error,
  255. _bfd_bool_bfd_false_error
  256. },
  257. BFD_JUMP_TABLE_GENERIC (_bfd_generic),
  258. BFD_JUMP_TABLE_COPY (_bfd_generic),
  259. BFD_JUMP_TABLE_CORE (netbsd),
  260. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  261. BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
  262. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  263. BFD_JUMP_TABLE_WRITE (_bfd_generic),
  264. BFD_JUMP_TABLE_LINK (_bfd_nolink),
  265. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  266. NULL,
  267. NULL /* Backend_data. */
  268. };