hpux-core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* BFD back-end for HP/UX 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 HP/UX style
  19. core files. */
  20. #include "sysdep.h"
  21. #include "bfd.h"
  22. #include "libbfd.h"
  23. #if defined (HOST_HPPAHPUX) || defined (HOST_HPPAMPEIX)
  24. /* FIXME: sys/core.h doesn't exist for HPUX version 7. HPUX version
  25. 5, 6, and 7 core files seem to be standard trad-core.c type core
  26. files; can we just use trad-core.c in addition to this file? */
  27. #include <sys/core.h>
  28. #include <sys/utsname.h>
  29. #endif /* HOST_HPPAHPUX */
  30. #ifdef HOST_HPPABSD
  31. /* Not a very swift place to put it, but that's where the BSD port
  32. puts them. */
  33. #include "/hpux/usr/include/sys/core.h"
  34. #endif /* HOST_HPPABSD */
  35. #include <sys/param.h>
  36. #include <dirent.h>
  37. #include <signal.h>
  38. #ifdef HPUX_CORE
  39. #include <machine/reg.h>
  40. #endif
  41. #include <sys/file.h>
  42. /* Kludge: There's no explicit mechanism provided by sys/core.h to
  43. conditionally know whether a proc_info has thread id fields.
  44. However, CORE_ANON_SHMEM shows up first at 10.30, which is
  45. happily also when meaningful thread id's show up in proc_info. */
  46. #if defined(CORE_ANON_SHMEM)
  47. #define PROC_INFO_HAS_THREAD_ID (1)
  48. #endif
  49. /* This type appears at HP-UX 10.30. Defining it if not defined
  50. by sys/core.h allows us to build for older HP-UX's, and (since
  51. it won't be encountered in core-dumps from older HP-UX's) is
  52. harmless. */
  53. #if !defined(CORE_ANON_SHMEM)
  54. #define CORE_ANON_SHMEM 0x00000200 /* anonymous shared memory */
  55. #endif
  56. /* These are stored in the bfd's tdata */
  57. /* .lwpid and .user_tid are only valid if PROC_INFO_HAS_THREAD_ID, else they
  58. are set to 0. Also, until HP-UX implements MxN threads, .user_tid and
  59. .lwpid are synonymous. */
  60. struct hpux_core_struct
  61. {
  62. int sig;
  63. int lwpid; /* Kernel thread ID. */
  64. unsigned long user_tid; /* User thread ID. */
  65. char cmd[MAXCOMLEN + 1];
  66. };
  67. #define core_hdr(bfd) ((bfd)->tdata.hpux_core_data)
  68. #define core_signal(bfd) (core_hdr(bfd)->sig)
  69. #define core_command(bfd) (core_hdr(bfd)->cmd)
  70. #define core_kernel_thread_id(bfd) (core_hdr(bfd)->lwpid)
  71. #define core_user_thread_id(bfd) (core_hdr(bfd)->user_tid)
  72. #define hpux_core_core_file_matches_executable_p generic_core_file_matches_executable_p
  73. #define hpux_core_core_file_pid _bfd_nocore_core_file_pid
  74. static asection *make_bfd_asection (bfd *, const char *, flagword,
  75. bfd_size_type, bfd_vma, unsigned int);
  76. static bfd_cleanup hpux_core_core_file_p (bfd *);
  77. static char *hpux_core_core_file_failing_command (bfd *);
  78. static int hpux_core_core_file_failing_signal (bfd *);
  79. static void swap_abort (void);
  80. static asection *
  81. make_bfd_asection (bfd *abfd, const char *name, flagword flags,
  82. bfd_size_type size, bfd_vma vma,
  83. unsigned int alignment_power)
  84. {
  85. asection *asect;
  86. char *newname;
  87. newname = bfd_alloc (abfd, (bfd_size_type) strlen (name) + 1);
  88. if (!newname)
  89. return NULL;
  90. strcpy (newname, name);
  91. asect = bfd_make_section_anyway_with_flags (abfd, newname, flags);
  92. if (!asect)
  93. return NULL;
  94. asect->size = size;
  95. asect->vma = vma;
  96. asect->filepos = bfd_tell (abfd);
  97. asect->alignment_power = alignment_power;
  98. return asect;
  99. }
  100. /* Return true if the given core file section corresponds to a thread,
  101. based on its name. */
  102. static int
  103. thread_section_p (bfd *abfd ATTRIBUTE_UNUSED,
  104. asection *sect,
  105. void *obj ATTRIBUTE_UNUSED)
  106. {
  107. return startswith (sect->name, ".reg/");
  108. }
  109. /* this function builds a bfd target if the file is a corefile.
  110. It returns null or 0 if it finds out thaat it is not a core file.
  111. The way it checks this is by looking for allowed 'type' field values.
  112. These are declared in sys/core.h
  113. There are some values which are 'reserved for future use'. In particular
  114. CORE_NONE is actually defined as 0. This may be a catch-all for cases
  115. in which the core file is generated by some non-hpux application.
  116. (I am just guessing here!)
  117. */
  118. static bfd_cleanup
  119. hpux_core_core_file_p (bfd *abfd)
  120. {
  121. int good_sections = 0;
  122. int unknown_sections = 0;
  123. core_hdr (abfd) = (struct hpux_core_struct *)
  124. bfd_zalloc (abfd, (bfd_size_type) sizeof (struct hpux_core_struct));
  125. if (!core_hdr (abfd))
  126. return NULL;
  127. while (1)
  128. {
  129. int val;
  130. struct corehead core_header;
  131. val = bfd_bread ((void *) &core_header,
  132. (bfd_size_type) sizeof core_header, abfd);
  133. if (val <= 0)
  134. break;
  135. switch (core_header.type)
  136. {
  137. case CORE_KERNEL:
  138. case CORE_FORMAT:
  139. /* Just skip this. */
  140. bfd_seek (abfd, (file_ptr) core_header.len, SEEK_CUR);
  141. good_sections++;
  142. break;
  143. case CORE_EXEC:
  144. {
  145. struct proc_exec proc_exec;
  146. if (bfd_bread ((void *) &proc_exec, (bfd_size_type) core_header.len,
  147. abfd) != core_header.len)
  148. break;
  149. strncpy (core_command (abfd), proc_exec.cmd, MAXCOMLEN + 1);
  150. good_sections++;
  151. }
  152. break;
  153. case CORE_PROC:
  154. {
  155. struct proc_info proc_info;
  156. char secname[100]; /* Of arbitrary size, but plenty large. */
  157. /* We need to read this section, 'cause we need to determine
  158. whether the core-dumped app was threaded before we create
  159. any .reg sections. */
  160. if (bfd_bread (&proc_info, (bfd_size_type) core_header.len, abfd)
  161. != core_header.len)
  162. break;
  163. /* However, we also want to create those sections with the
  164. file positioned at the start of the record, it seems. */
  165. if (bfd_seek (abfd, -((file_ptr) core_header.len), SEEK_CUR) != 0)
  166. break;
  167. #if defined(PROC_INFO_HAS_THREAD_ID)
  168. core_kernel_thread_id (abfd) = proc_info.lwpid;
  169. core_user_thread_id (abfd) = proc_info.user_tid;
  170. #else
  171. core_kernel_thread_id (abfd) = 0;
  172. core_user_thread_id (abfd) = 0;
  173. #endif
  174. /* If the program was unthreaded, then we'll just create a
  175. .reg section.
  176. If the program was threaded, then we'll create .reg/XXXXX
  177. section for each thread, where XXXXX is a printable
  178. representation of the kernel thread id. We'll also
  179. create a .reg section for the thread that was running
  180. and signalled at the time of the core-dump (i.e., this
  181. is effectively an alias, needed to keep GDB happy.)
  182. Note that we use `.reg/XXXXX' as opposed to '.regXXXXX'
  183. because GDB expects that .reg2 will be the floating-
  184. point registers. */
  185. if (core_kernel_thread_id (abfd) == 0)
  186. {
  187. if (!make_bfd_asection (abfd, ".reg",
  188. SEC_HAS_CONTENTS,
  189. core_header.len,
  190. (bfd_vma) offsetof (struct proc_info,
  191. hw_regs),
  192. 2))
  193. goto fail;
  194. }
  195. else
  196. {
  197. /* There are threads. Is this the one that caused the
  198. core-dump? We'll claim it was the running thread. */
  199. if (proc_info.sig != -1)
  200. {
  201. if (!make_bfd_asection (abfd, ".reg",
  202. SEC_HAS_CONTENTS,
  203. core_header.len,
  204. (bfd_vma)offsetof (struct proc_info,
  205. hw_regs),
  206. 2))
  207. goto fail;
  208. }
  209. /* We always make one of these sections, for every thread. */
  210. sprintf (secname, ".reg/%d", core_kernel_thread_id (abfd));
  211. if (!make_bfd_asection (abfd, secname,
  212. SEC_HAS_CONTENTS,
  213. core_header.len,
  214. (bfd_vma) offsetof (struct proc_info,
  215. hw_regs),
  216. 2))
  217. goto fail;
  218. }
  219. core_signal (abfd) = proc_info.sig;
  220. if (bfd_seek (abfd, (file_ptr) core_header.len, SEEK_CUR) != 0)
  221. break;
  222. good_sections++;
  223. }
  224. break;
  225. case CORE_DATA:
  226. case CORE_STACK:
  227. case CORE_TEXT:
  228. case CORE_MMF:
  229. case CORE_SHM:
  230. case CORE_ANON_SHMEM:
  231. if (!make_bfd_asection (abfd, ".data",
  232. SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
  233. core_header.len,
  234. (bfd_vma) core_header.addr, 2))
  235. goto fail;
  236. bfd_seek (abfd, (file_ptr) core_header.len, SEEK_CUR);
  237. good_sections++;
  238. break;
  239. case CORE_NONE:
  240. /* Let's not punt if we encounter a section of unknown
  241. type. Rather, let's make a note of it. If we later
  242. see that there were also "good" sections, then we'll
  243. declare that this a core file, but we'll also warn that
  244. it may be incompatible with this gdb.
  245. */
  246. unknown_sections++;
  247. break;
  248. default:
  249. goto fail; /*unrecognized core file type */
  250. }
  251. }
  252. /* OK, we believe you. You're a core file (sure, sure). */
  253. /* On HP/UX, we sometimes encounter core files where none of the threads
  254. was found to be the running thread (ie the signal was set to -1 for
  255. all threads). This happens when the program was aborted externally
  256. via a TT_CORE ttrace system call. In that case, we just pick one
  257. thread at random to be the active thread. */
  258. if (core_kernel_thread_id (abfd) != 0
  259. && bfd_get_section_by_name (abfd, ".reg") == NULL)
  260. {
  261. asection *asect = bfd_sections_find_if (abfd, thread_section_p, NULL);
  262. asection *reg_sect;
  263. if (asect != NULL)
  264. {
  265. reg_sect = make_bfd_asection (abfd, ".reg", asect->flags,
  266. asect->size, asect->vma,
  267. asect->alignment_power);
  268. if (reg_sect == NULL)
  269. goto fail;
  270. reg_sect->filepos = asect->filepos;
  271. }
  272. }
  273. /* Were there sections of unknown type? If so, yet there were
  274. at least some complete sections of known type, then, issue
  275. a warning. Possibly the core file was generated on a version
  276. of HP-UX that is incompatible with that for which this gdb was
  277. built.
  278. */
  279. if ((unknown_sections > 0) && (good_sections > 0))
  280. _bfd_error_handler
  281. ("%pB appears to be a core file,\nbut contains unknown sections."
  282. " It may have been created on an incompatible\nversion of HP-UX."
  283. " As a result, some information may be unavailable.\n",
  284. abfd);
  285. return _bfd_no_cleanup;
  286. fail:
  287. bfd_release (abfd, core_hdr (abfd));
  288. core_hdr (abfd) = NULL;
  289. bfd_section_list_clear (abfd);
  290. return NULL;
  291. }
  292. static char *
  293. hpux_core_core_file_failing_command (bfd *abfd)
  294. {
  295. return core_command (abfd);
  296. }
  297. static int
  298. hpux_core_core_file_failing_signal (bfd *abfd)
  299. {
  300. return core_signal (abfd);
  301. }
  302. /* If somebody calls any byte-swapping routines, shoot them. */
  303. static void
  304. swap_abort (void)
  305. {
  306. abort(); /* This way doesn't require any declaration for ANSI to fuck up */
  307. }
  308. #define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
  309. #define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
  310. #define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
  311. #define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
  312. #define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
  313. #define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
  314. const bfd_target core_hpux_vec =
  315. {
  316. "hpux-core",
  317. bfd_target_unknown_flavour,
  318. BFD_ENDIAN_BIG, /* target byte order */
  319. BFD_ENDIAN_BIG, /* target headers byte order */
  320. (HAS_RELOC | EXEC_P | /* object flags */
  321. HAS_LINENO | HAS_DEBUG |
  322. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  323. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  324. 0, /* symbol prefix */
  325. ' ', /* ar_pad_char */
  326. 16, /* ar_max_namelen */
  327. 0, /* match priority. */
  328. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  329. NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
  330. NO_GET, NO_GETS, NO_PUT, /* 32 bit data */
  331. NO_GET, NO_GETS, NO_PUT, /* 16 bit data */
  332. NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
  333. NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */
  334. NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */
  335. { /* bfd_check_format */
  336. _bfd_dummy_target, /* unknown format */
  337. _bfd_dummy_target, /* object file */
  338. _bfd_dummy_target, /* archive */
  339. hpux_core_core_file_p /* a core file */
  340. },
  341. { /* bfd_set_format */
  342. _bfd_bool_bfd_false_error,
  343. _bfd_bool_bfd_false_error,
  344. _bfd_bool_bfd_false_error,
  345. _bfd_bool_bfd_false_error
  346. },
  347. { /* bfd_write_contents */
  348. _bfd_bool_bfd_false_error,
  349. _bfd_bool_bfd_false_error,
  350. _bfd_bool_bfd_false_error,
  351. _bfd_bool_bfd_false_error
  352. },
  353. BFD_JUMP_TABLE_GENERIC (_bfd_generic),
  354. BFD_JUMP_TABLE_COPY (_bfd_generic),
  355. BFD_JUMP_TABLE_CORE (hpux_core),
  356. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  357. BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
  358. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  359. BFD_JUMP_TABLE_WRITE (_bfd_generic),
  360. BFD_JUMP_TABLE_LINK (_bfd_nolink),
  361. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  362. NULL,
  363. NULL /* backend_data */
  364. };