i386-obsd-tdep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Target-dependent code for OpenBSD/i386.
  2. Copyright (C) 1988-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "frame.h"
  17. #include "frame-unwind.h"
  18. #include "gdbcore.h"
  19. #include "regcache.h"
  20. #include "regset.h"
  21. #include "symtab.h"
  22. #include "objfiles.h"
  23. #include "osabi.h"
  24. #include "target.h"
  25. #include "trad-frame.h"
  26. #include "obsd-tdep.h"
  27. #include "i386-tdep.h"
  28. #include "i387-tdep.h"
  29. #include "solib-svr4.h"
  30. #include "bsd-uthread.h"
  31. /* Support for signal handlers. */
  32. /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
  33. in virtual memory. The randomness makes it somewhat tricky to
  34. detect it, but fortunately we can rely on the fact that the start
  35. of the sigtramp routine is page-aligned. We recognize the
  36. trampoline by looking for the code that invokes the sigreturn
  37. system call. The offset where we can find that code varies from
  38. release to release.
  39. By the way, the mapping mentioned above is read-only, so you cannot
  40. place a breakpoint in the signal trampoline. */
  41. /* Default page size. */
  42. static const int i386obsd_page_size = 4096;
  43. /* Offset for sigreturn(2). */
  44. static const int i386obsd_sigreturn_offset[] = {
  45. 0x0a, /* OpenBSD 3.2 */
  46. 0x14, /* OpenBSD 3.6 */
  47. 0x3a, /* OpenBSD 3.8 */
  48. -1
  49. };
  50. /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
  51. routine. */
  52. static int
  53. i386obsd_sigtramp_p (struct frame_info *this_frame)
  54. {
  55. CORE_ADDR pc = get_frame_pc (this_frame);
  56. CORE_ADDR start_pc = (pc & ~(i386obsd_page_size - 1));
  57. /* The call sequence invoking sigreturn(2). */
  58. const gdb_byte sigreturn[] =
  59. {
  60. 0xb8,
  61. 0x67, 0x00, 0x00, 0x00, /* movl $SYS_sigreturn, %eax */
  62. 0xcd, 0x80 /* int $0x80 */
  63. };
  64. size_t buflen = sizeof sigreturn;
  65. const int *offset;
  66. gdb_byte *buf;
  67. const char *name;
  68. /* If the function has a valid symbol name, it isn't a
  69. trampoline. */
  70. find_pc_partial_function (pc, &name, NULL, NULL);
  71. if (name != NULL)
  72. return 0;
  73. /* If the function lives in a valid section (even without a starting
  74. point) it isn't a trampoline. */
  75. if (find_pc_section (pc) != NULL)
  76. return 0;
  77. /* Allocate buffer. */
  78. buf = (gdb_byte *) alloca (buflen);
  79. /* Loop over all offsets. */
  80. for (offset = i386obsd_sigreturn_offset; *offset != -1; offset++)
  81. {
  82. /* If we can't read the instructions, return zero. */
  83. if (!safe_frame_unwind_memory (this_frame, start_pc + *offset,
  84. {buf, buflen}))
  85. return 0;
  86. /* Check for sigreturn(2). */
  87. if (memcmp (buf, sigreturn, buflen) == 0)
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. /* Mapping between the general-purpose registers in `struct reg'
  93. format and GDB's register cache layout. */
  94. /* From <machine/reg.h>. */
  95. static int i386obsd_r_reg_offset[] =
  96. {
  97. 0 * 4, /* %eax */
  98. 1 * 4, /* %ecx */
  99. 2 * 4, /* %edx */
  100. 3 * 4, /* %ebx */
  101. 4 * 4, /* %esp */
  102. 5 * 4, /* %ebp */
  103. 6 * 4, /* %esi */
  104. 7 * 4, /* %edi */
  105. 8 * 4, /* %eip */
  106. 9 * 4, /* %eflags */
  107. 10 * 4, /* %cs */
  108. 11 * 4, /* %ss */
  109. 12 * 4, /* %ds */
  110. 13 * 4, /* %es */
  111. 14 * 4, /* %fs */
  112. 15 * 4 /* %gs */
  113. };
  114. /* Sigtramp routine location for OpenBSD 3.1 and earlier releases. */
  115. CORE_ADDR i386obsd_sigtramp_start_addr = 0xbfbfdf20;
  116. CORE_ADDR i386obsd_sigtramp_end_addr = 0xbfbfdff0;
  117. /* From <machine/signal.h>. */
  118. int i386obsd_sc_reg_offset[I386_NUM_GREGS] =
  119. {
  120. 10 * 4, /* %eax */
  121. 9 * 4, /* %ecx */
  122. 8 * 4, /* %edx */
  123. 7 * 4, /* %ebx */
  124. 14 * 4, /* %esp */
  125. 6 * 4, /* %ebp */
  126. 5 * 4, /* %esi */
  127. 4 * 4, /* %edi */
  128. 11 * 4, /* %eip */
  129. 13 * 4, /* %eflags */
  130. 12 * 4, /* %cs */
  131. 15 * 4, /* %ss */
  132. 3 * 4, /* %ds */
  133. 2 * 4, /* %es */
  134. 1 * 4, /* %fs */
  135. 0 * 4 /* %gs */
  136. };
  137. /* From /usr/src/lib/libpthread/arch/i386/uthread_machdep.c. */
  138. static int i386obsd_uthread_reg_offset[] =
  139. {
  140. 11 * 4, /* %eax */
  141. 10 * 4, /* %ecx */
  142. 9 * 4, /* %edx */
  143. 8 * 4, /* %ebx */
  144. -1, /* %esp */
  145. 6 * 4, /* %ebp */
  146. 5 * 4, /* %esi */
  147. 4 * 4, /* %edi */
  148. 12 * 4, /* %eip */
  149. -1, /* %eflags */
  150. 13 * 4, /* %cs */
  151. -1, /* %ss */
  152. 3 * 4, /* %ds */
  153. 2 * 4, /* %es */
  154. 1 * 4, /* %fs */
  155. 0 * 4 /* %gs */
  156. };
  157. /* Offset within the thread structure where we can find the saved
  158. stack pointer (%esp). */
  159. #define I386OBSD_UTHREAD_ESP_OFFSET 176
  160. static void
  161. i386obsd_supply_uthread (struct regcache *regcache,
  162. int regnum, CORE_ADDR addr)
  163. {
  164. struct gdbarch *gdbarch = regcache->arch ();
  165. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  166. CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
  167. CORE_ADDR sp = 0;
  168. gdb_byte buf[4];
  169. int i;
  170. gdb_assert (regnum >= -1);
  171. if (regnum == -1 || regnum == I386_ESP_REGNUM)
  172. {
  173. int offset;
  174. /* Fetch stack pointer from thread structure. */
  175. sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);
  176. /* Adjust the stack pointer such that it looks as if we just
  177. returned from _thread_machdep_switch. */
  178. offset = i386obsd_uthread_reg_offset[I386_EIP_REGNUM] + 4;
  179. store_unsigned_integer (buf, 4, byte_order, sp + offset);
  180. regcache->raw_supply (I386_ESP_REGNUM, buf);
  181. }
  182. for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
  183. {
  184. if (i386obsd_uthread_reg_offset[i] != -1
  185. && (regnum == -1 || regnum == i))
  186. {
  187. /* Fetch stack pointer from thread structure (if we didn't
  188. do so already). */
  189. if (sp == 0)
  190. sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);
  191. /* Read the saved register from the stack frame. */
  192. read_memory (sp + i386obsd_uthread_reg_offset[i], buf, 4);
  193. regcache->raw_supply (i, buf);
  194. }
  195. }
  196. }
  197. static void
  198. i386obsd_collect_uthread (const struct regcache *regcache,
  199. int regnum, CORE_ADDR addr)
  200. {
  201. struct gdbarch *gdbarch = regcache->arch ();
  202. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  203. CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
  204. CORE_ADDR sp = 0;
  205. gdb_byte buf[4];
  206. int i;
  207. gdb_assert (regnum >= -1);
  208. if (regnum == -1 || regnum == I386_ESP_REGNUM)
  209. {
  210. int offset;
  211. /* Calculate the stack pointer (frame pointer) that will be
  212. stored into the thread structure. */
  213. offset = i386obsd_uthread_reg_offset[I386_EIP_REGNUM] + 4;
  214. regcache->raw_collect (I386_ESP_REGNUM, buf);
  215. sp = extract_unsigned_integer (buf, 4, byte_order) - offset;
  216. /* Store the stack pointer. */
  217. write_memory_unsigned_integer (sp_addr, 4, byte_order, sp);
  218. /* The stack pointer was (potentially) modified. Make sure we
  219. build a proper stack frame. */
  220. regnum = -1;
  221. }
  222. for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
  223. {
  224. if (i386obsd_uthread_reg_offset[i] != -1
  225. && (regnum == -1 || regnum == i))
  226. {
  227. /* Fetch stack pointer from thread structure (if we didn't
  228. calculate it already). */
  229. if (sp == 0)
  230. sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);
  231. /* Write the register into the stack frame. */
  232. regcache->raw_collect (i, buf);
  233. write_memory (sp + i386obsd_uthread_reg_offset[i], buf, 4);
  234. }
  235. }
  236. }
  237. /* Kernel debugging support. */
  238. /* From <machine/frame.h>. Note that %esp and %ess are only saved in
  239. a trap frame when entering the kernel from user space. */
  240. static int i386obsd_tf_reg_offset[] =
  241. {
  242. 10 * 4, /* %eax */
  243. 9 * 4, /* %ecx */
  244. 8 * 4, /* %edx */
  245. 7 * 4, /* %ebx */
  246. -1, /* %esp */
  247. 6 * 4, /* %ebp */
  248. 5 * 4, /* %esi */
  249. 4 * 4, /* %edi */
  250. 13 * 4, /* %eip */
  251. 15 * 4, /* %eflags */
  252. 14 * 4, /* %cs */
  253. -1, /* %ss */
  254. 3 * 4, /* %ds */
  255. 2 * 4, /* %es */
  256. 0 * 4, /* %fs */
  257. 1 * 4 /* %gs */
  258. };
  259. static struct trad_frame_cache *
  260. i386obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
  261. {
  262. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  263. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  264. struct trad_frame_cache *cache;
  265. CORE_ADDR func, sp, addr;
  266. ULONGEST cs;
  267. const char *name;
  268. int i;
  269. if (*this_cache)
  270. return (struct trad_frame_cache *) *this_cache;
  271. cache = trad_frame_cache_zalloc (this_frame);
  272. *this_cache = cache;
  273. func = get_frame_func (this_frame);
  274. sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
  275. find_pc_partial_function (func, &name, NULL, NULL);
  276. if (name && startswith (name, "Xintr"))
  277. addr = sp + 8; /* It's an interrupt frame. */
  278. else
  279. addr = sp;
  280. for (i = 0; i < ARRAY_SIZE (i386obsd_tf_reg_offset); i++)
  281. if (i386obsd_tf_reg_offset[i] != -1)
  282. trad_frame_set_reg_addr (cache, i, addr + i386obsd_tf_reg_offset[i]);
  283. /* Read %cs from trap frame. */
  284. addr += i386obsd_tf_reg_offset[I386_CS_REGNUM];
  285. cs = read_memory_unsigned_integer (addr, 4, byte_order);
  286. if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
  287. {
  288. /* Trap from user space; terminate backtrace. */
  289. trad_frame_set_id (cache, outer_frame_id);
  290. }
  291. else
  292. {
  293. /* Construct the frame ID using the function start. */
  294. trad_frame_set_id (cache, frame_id_build (sp + 8, func));
  295. }
  296. return cache;
  297. }
  298. static void
  299. i386obsd_trapframe_this_id (struct frame_info *this_frame,
  300. void **this_cache, struct frame_id *this_id)
  301. {
  302. struct trad_frame_cache *cache =
  303. i386obsd_trapframe_cache (this_frame, this_cache);
  304. trad_frame_get_id (cache, this_id);
  305. }
  306. static struct value *
  307. i386obsd_trapframe_prev_register (struct frame_info *this_frame,
  308. void **this_cache, int regnum)
  309. {
  310. struct trad_frame_cache *cache =
  311. i386obsd_trapframe_cache (this_frame, this_cache);
  312. return trad_frame_get_register (cache, this_frame, regnum);
  313. }
  314. static int
  315. i386obsd_trapframe_sniffer (const struct frame_unwind *self,
  316. struct frame_info *this_frame,
  317. void **this_prologue_cache)
  318. {
  319. ULONGEST cs;
  320. const char *name;
  321. /* Check Current Privilege Level and bail out if we're not executing
  322. in kernel space. */
  323. cs = get_frame_register_unsigned (this_frame, I386_CS_REGNUM);
  324. if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
  325. return 0;
  326. find_pc_partial_function (get_frame_pc (this_frame), &name, NULL, NULL);
  327. return (name && (strcmp (name, "calltrap") == 0
  328. || strcmp (name, "syscall1") == 0
  329. || startswith (name, "Xintr")
  330. || startswith (name, "Xsoft")));
  331. }
  332. static const struct frame_unwind i386obsd_trapframe_unwind = {
  333. "i386 openbsd trap",
  334. /* FIXME: kettenis/20051219: This really is more like an interrupt
  335. frame, but SIGTRAMP_FRAME would print <signal handler called>,
  336. which really is not what we want here. */
  337. NORMAL_FRAME,
  338. default_frame_unwind_stop_reason,
  339. i386obsd_trapframe_this_id,
  340. i386obsd_trapframe_prev_register,
  341. NULL,
  342. i386obsd_trapframe_sniffer
  343. };
  344. static void
  345. i386obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  346. {
  347. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  348. /* Obviously OpenBSD is BSD-based. */
  349. i386bsd_init_abi (info, gdbarch);
  350. obsd_init_abi (info, gdbarch);
  351. i386_elf_init_abi (info, gdbarch);
  352. /* OpenBSD has a different `struct reg'. */
  353. tdep->gregset_reg_offset = i386obsd_r_reg_offset;
  354. tdep->gregset_num_regs = ARRAY_SIZE (i386obsd_r_reg_offset);
  355. tdep->sizeof_gregset = 16 * 4;
  356. /* OpenBSD uses -freg-struct-return by default. */
  357. tdep->struct_return = reg_struct_return;
  358. /* OpenBSD uses a different memory layout. */
  359. tdep->sigtramp_start = i386obsd_sigtramp_start_addr;
  360. tdep->sigtramp_end = i386obsd_sigtramp_end_addr;
  361. tdep->sigtramp_p = i386obsd_sigtramp_p;
  362. /* OpenBSD has a `struct sigcontext' that's different from the
  363. original 4.3 BSD. */
  364. tdep->sc_reg_offset = i386obsd_sc_reg_offset;
  365. tdep->sc_num_regs = ARRAY_SIZE (i386obsd_sc_reg_offset);
  366. /* OpenBSD provides a user-level threads implementation. */
  367. bsd_uthread_set_supply_uthread (gdbarch, i386obsd_supply_uthread);
  368. bsd_uthread_set_collect_uthread (gdbarch, i386obsd_collect_uthread);
  369. /* Unwind kernel trap frames correctly. */
  370. frame_unwind_prepend_unwinder (gdbarch, &i386obsd_trapframe_unwind);
  371. /* OpenBSD ELF uses SVR4-style shared libraries. */
  372. set_solib_svr4_fetch_link_map_offsets
  373. (gdbarch, svr4_ilp32_fetch_link_map_offsets);
  374. }
  375. void _initialize_i386obsd_tdep ();
  376. void
  377. _initialize_i386obsd_tdep ()
  378. {
  379. gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD,
  380. i386obsd_init_abi);
  381. }