hpux-core.c 14 KB

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