ptrace-core.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* BFD backend for core files which use the ptrace_user structure
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. The structure of this file is based on trad-core.c written by John Gilmore
  4. of Cygnus Support.
  5. Modified to work with the ptrace_user structure by Kevin A. Buettner.
  6. (Longterm it may be better to merge this file with trad-core.c)
  7. This file is part of BFD, the Binary File Descriptor library.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 3 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  19. MA 02110-1301, USA. */
  20. #ifdef PTRACE_CORE
  21. #include "sysdep.h"
  22. #include "bfd.h"
  23. #include "libbfd.h"
  24. #include <sys/param.h>
  25. #include <sys/dir.h>
  26. #include <signal.h>
  27. #include <sys/ptrace.h>
  28. struct trad_core_struct
  29. {
  30. asection *data_section;
  31. asection *stack_section;
  32. asection *reg_section;
  33. struct ptrace_user u;
  34. };
  35. #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
  36. #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
  37. #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
  38. #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
  39. /* forward declarations */
  40. const bfd_target *ptrace_unix_core_file_p (bfd *abfd);
  41. char * ptrace_unix_core_file_failing_command (bfd *abfd);
  42. int ptrace_unix_core_file_failing_signal (bfd *abfd);
  43. #define ptrace_unix_core_file_matches_executable_p generic_core_file_matches_executable_p
  44. #define ptrace_unix_core_file_pid _bfd_nocore_core_file_pid
  45. static void swap_abort (void);
  46. bfd_cleanup
  47. ptrace_unix_core_file_p (bfd *abfd)
  48. {
  49. int val;
  50. struct ptrace_user u;
  51. struct trad_core_struct *rawptr;
  52. size_t amt;
  53. flagword flags;
  54. val = bfd_bread ((void *)&u, (bfd_size_type) sizeof u, abfd);
  55. if (val != sizeof u || u.pt_magic != _BCS_PTRACE_MAGIC
  56. || u.pt_rev != _BCS_PTRACE_REV)
  57. {
  58. /* Too small to be a core file */
  59. bfd_set_error (bfd_error_wrong_format);
  60. return 0;
  61. }
  62. /* OK, we believe you. You're a core file (sure, sure). */
  63. /* Allocate both the upage and the struct core_data at once, so
  64. a single free() will free them both. */
  65. amt = sizeof (struct trad_core_struct);
  66. rawptr = (struct trad_core_struct *) bfd_zalloc (abfd, amt);
  67. if (rawptr == NULL)
  68. return 0;
  69. abfd->tdata.trad_core_data = rawptr;
  70. rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
  71. /* Create the sections. */
  72. flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
  73. core_stacksec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".stack",
  74. flags);
  75. if (core_stacksec (abfd) == NULL)
  76. goto fail;
  77. core_datasec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".data",
  78. flags);
  79. if (core_datasec (abfd) == NULL)
  80. goto fail;
  81. core_regsec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".reg",
  82. SEC_HAS_CONTENTS);
  83. if (core_regsec (abfd) == NULL)
  84. goto fail;
  85. /* FIXME: Need to worry about shared memory, library data, and library
  86. text. I don't think that any of these things are supported on the
  87. system on which I am developing this for though. */
  88. core_datasec (abfd)->size = u.pt_dsize;
  89. core_stacksec (abfd)->size = u.pt_ssize;
  90. core_regsec (abfd)->size = sizeof (u);
  91. core_datasec (abfd)->vma = u.pt_o_data_start;
  92. core_stacksec (abfd)->vma = USRSTACK - u.pt_ssize;
  93. core_regsec (abfd)->vma = 0 - sizeof (u); /* see trad-core.c */
  94. core_datasec (abfd)->filepos = (int) u.pt_dataptr;
  95. core_stacksec (abfd)->filepos = (int) (u.pt_dataptr + u.pt_dsize);
  96. core_regsec (abfd)->filepos = 0; /* Register segment is ptrace_user */
  97. /* Align to word at least */
  98. core_stacksec (abfd)->alignment_power = 2;
  99. core_datasec (abfd)->alignment_power = 2;
  100. core_regsec (abfd)->alignment_power = 2;
  101. return _bfd_no_cleanup;
  102. fail:
  103. bfd_release (abfd, abfd->tdata.any);
  104. abfd->tdata.any = NULL;
  105. bfd_section_list_clear (abfd);
  106. return NULL;
  107. }
  108. char *
  109. ptrace_unix_core_file_failing_command (bfd *abfd)
  110. {
  111. char *com = abfd->tdata.trad_core_data->u.pt_comm;
  112. if (*com)
  113. return com;
  114. else
  115. return 0;
  116. }
  117. int
  118. ptrace_unix_core_file_failing_signal (bfd *abfd)
  119. {
  120. return abfd->tdata.trad_core_data->u.pt_sigframe.sig_num;
  121. }
  122. /* If somebody calls any byte-swapping routines, shoot them. */
  123. static void
  124. swap_abort (void)
  125. {
  126. abort (); /* This way doesn't require any declaration for ANSI to fuck up */
  127. }
  128. #define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
  129. #define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
  130. #define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
  131. #define NO_GET64 ((bfd_uint64_t (*) (const void *)) swap_abort)
  132. #define NO_PUT64 ((void (*) (bfd_uint64_t, void *)) swap_abort)
  133. #define NO_GETS64 ((bfd_int64_t (*) (const void *)) swap_abort)
  134. const bfd_target core_ptrace_vec =
  135. {
  136. "trad-core",
  137. bfd_target_unknown_flavour,
  138. BFD_ENDIAN_UNKNOWN, /* target byte order */
  139. BFD_ENDIAN_UNKNOWN, /* target headers byte order */
  140. (HAS_RELOC | EXEC_P | /* object flags */
  141. HAS_LINENO | HAS_DEBUG |
  142. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  143. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  144. 0, /* symbol prefix */
  145. ' ', /* ar_pad_char */
  146. 16, /* ar_max_namelen */
  147. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  148. NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
  149. NO_GET, NO_GETS, NO_PUT, /* 32 bit data */
  150. NO_GET, NO_GETS, NO_PUT, /* 16 bit data */
  151. NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
  152. NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */
  153. NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */
  154. { /* bfd_check_format */
  155. _bfd_dummy_target, /* unknown format */
  156. _bfd_dummy_target, /* object file */
  157. _bfd_dummy_target, /* archive */
  158. ptrace_unix_core_file_p /* a core file */
  159. },
  160. { /* bfd_set_format */
  161. _bfd_bool_bfd_false_error, bfd_false,
  162. _bfd_bool_bfd_false_error, bfd_false
  163. },
  164. { /* bfd_write_contents */
  165. _bfd_bool_bfd_false_error, bfd_false,
  166. _bfd_bool_bfd_false_error, bfd_false
  167. },
  168. BFD_JUMP_TABLE_GENERIC (_bfd_generic),
  169. BFD_JUMP_TABLE_COPY (_bfd_generic),
  170. BFD_JUMP_TABLE_CORE (ptrace_unix),
  171. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  172. BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
  173. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  174. BFD_JUMP_TABLE_WRITE (_bfd_generic),
  175. BFD_JUMP_TABLE_LINK (_bfd_nolink),
  176. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  177. NULL,
  178. NULL /* backend_data */
  179. };
  180. #endif /* PTRACE_CORE */