gdb/i386obsd-tdep.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for OpenBSD/i386.

  2.    Copyright (C) 1988-2015 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 = 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. static void
  115. i386obsd_aout_supply_regset (const struct regset *regset,
  116.                              struct regcache *regcache, int regnum,
  117.                              const void *regs, size_t len)
  118. {
  119.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  120.   const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  121.   const gdb_byte *gregs = regs;

  122.   gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE);

  123.   i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
  124.   i387_supply_fsave (regcache, regnum, gregs + tdep->sizeof_gregset);
  125. }

  126. static const struct regset i386obsd_aout_gregset =
  127.   {
  128.     NULL, i386obsd_aout_supply_regset, NULL
  129.   };

  130. static void
  131. i386obsd_aout_iterate_over_regset_sections (struct gdbarch *gdbarch,
  132.                                             iterate_over_regset_sections_cb *cb,
  133.                                             void *cb_data,
  134.                                             const struct regcache *regcache)
  135. {
  136.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  137.   /* OpenBSD a.out core dumps don't use seperate register sets for the
  138.      general-purpose and floating-point registers.  */

  139.   cb (".reg", tdep->sizeof_gregset + I387_SIZEOF_FSAVE,
  140.       &i386obsd_aout_gregset, NULL, cb_data);
  141. }


  142. /* Sigtramp routine location for OpenBSD 3.1 and earlier releases.  */
  143. CORE_ADDR i386obsd_sigtramp_start_addr = 0xbfbfdf20;
  144. CORE_ADDR i386obsd_sigtramp_end_addr = 0xbfbfdff0;

  145. /* From <machine/signal.h>.  */
  146. int i386obsd_sc_reg_offset[I386_NUM_GREGS] =
  147. {
  148.   10 * 4,                        /* %eax */
  149.   9 * 4,                        /* %ecx */
  150.   8 * 4,                        /* %edx */
  151.   7 * 4,                        /* %ebx */
  152.   14 * 4,                        /* %esp */
  153.   6 * 4,                        /* %ebp */
  154.   5 * 4,                        /* %esi */
  155.   4 * 4,                        /* %edi */
  156.   11 * 4,                        /* %eip */
  157.   13 * 4,                        /* %eflags */
  158.   12 * 4,                        /* %cs */
  159.   15 * 4,                        /* %ss */
  160.   3 * 4,                        /* %ds */
  161.   2 * 4,                        /* %es */
  162.   1 * 4,                        /* %fs */
  163.   0 * 4                                /* %gs */
  164. };

  165. /* From /usr/src/lib/libpthread/arch/i386/uthread_machdep.c.  */
  166. static int i386obsd_uthread_reg_offset[] =
  167. {
  168.   11 * 4,                        /* %eax */
  169.   10 * 4,                        /* %ecx */
  170.   9 * 4,                        /* %edx */
  171.   8 * 4,                        /* %ebx */
  172.   -1,                                /* %esp */
  173.   6 * 4,                        /* %ebp */
  174.   5 * 4,                        /* %esi */
  175.   4 * 4,                        /* %edi */
  176.   12 * 4,                        /* %eip */
  177.   -1,                                /* %eflags */
  178.   13 * 4,                        /* %cs */
  179.   -1,                                /* %ss */
  180.   3 * 4,                        /* %ds */
  181.   2 * 4,                        /* %es */
  182.   1 * 4,                        /* %fs */
  183.   0 * 4                                /* %gs */
  184. };

  185. /* Offset within the thread structure where we can find the saved
  186.    stack pointer (%esp).  */
  187. #define I386OBSD_UTHREAD_ESP_OFFSET        176

  188. static void
  189. i386obsd_supply_uthread (struct regcache *regcache,
  190.                          int regnum, CORE_ADDR addr)
  191. {
  192.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  193.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  194.   CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
  195.   CORE_ADDR sp = 0;
  196.   gdb_byte buf[4];
  197.   int i;

  198.   gdb_assert (regnum >= -1);

  199.   if (regnum == -1 || regnum == I386_ESP_REGNUM)
  200.     {
  201.       int offset;

  202.       /* Fetch stack pointer from thread structure.  */
  203.       sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);

  204.       /* Adjust the stack pointer such that it looks as if we just
  205.          returned from _thread_machdep_switch.  */
  206.       offset = i386obsd_uthread_reg_offset[I386_EIP_REGNUM] + 4;
  207.       store_unsigned_integer (buf, 4, byte_order, sp + offset);
  208.       regcache_raw_supply (regcache, I386_ESP_REGNUM, buf);
  209.     }

  210.   for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
  211.     {
  212.       if (i386obsd_uthread_reg_offset[i] != -1
  213.           && (regnum == -1 || regnum == i))
  214.         {
  215.           /* Fetch stack pointer from thread structure (if we didn't
  216.              do so already).  */
  217.           if (sp == 0)
  218.             sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);

  219.           /* Read the saved register from the stack frame.  */
  220.           read_memory (sp + i386obsd_uthread_reg_offset[i], buf, 4);
  221.           regcache_raw_supply (regcache, i, buf);
  222.         }
  223.     }
  224. }

  225. static void
  226. i386obsd_collect_uthread (const struct regcache *regcache,
  227.                           int regnum, CORE_ADDR addr)
  228. {
  229.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  230.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  231.   CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
  232.   CORE_ADDR sp = 0;
  233.   gdb_byte buf[4];
  234.   int i;

  235.   gdb_assert (regnum >= -1);

  236.   if (regnum == -1 || regnum == I386_ESP_REGNUM)
  237.     {
  238.       int offset;

  239.       /* Calculate the stack pointer (frame pointer) that will be
  240.          stored into the thread structure.  */
  241.       offset = i386obsd_uthread_reg_offset[I386_EIP_REGNUM] + 4;
  242.       regcache_raw_collect (regcache, I386_ESP_REGNUM, buf);
  243.       sp = extract_unsigned_integer (buf, 4, byte_order) - offset;

  244.       /* Store the stack pointer.  */
  245.       write_memory_unsigned_integer (sp_addr, 4, byte_order, sp);

  246.       /* The stack pointer was (potentially) modified.  Make sure we
  247.          build a proper stack frame.  */
  248.       regnum = -1;
  249.     }

  250.   for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
  251.     {
  252.       if (i386obsd_uthread_reg_offset[i] != -1
  253.           && (regnum == -1 || regnum == i))
  254.         {
  255.           /* Fetch stack pointer from thread structure (if we didn't
  256.              calculate it already).  */
  257.           if (sp == 0)
  258.             sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);

  259.           /* Write the register into the stack frame.  */
  260.           regcache_raw_collect (regcache, i, buf);
  261.           write_memory (sp + i386obsd_uthread_reg_offset[i], buf, 4);
  262.         }
  263.     }
  264. }

  265. /* Kernel debugging support.  */

  266. /* From <machine/frame.h>.  Note that %esp and %ess are only saved in
  267.    a trap frame when entering the kernel from user space.  */
  268. static int i386obsd_tf_reg_offset[] =
  269. {
  270.   10 * 4,                        /* %eax */
  271.   9 * 4,                        /* %ecx */
  272.   8 * 4,                        /* %edx */
  273.   7 * 4,                        /* %ebx */
  274.   -1,                                /* %esp */
  275.   6 * 4,                        /* %ebp */
  276.   5 * 4,                        /* %esi */
  277.   4 * 4,                        /* %edi */
  278.   13 * 4,                        /* %eip */
  279.   15 * 4,                        /* %eflags */
  280.   14 * 4,                        /* %cs */
  281.   -1,                                /* %ss */
  282.   3 * 4,                        /* %ds */
  283.   2 * 4,                        /* %es */
  284.   0 * 4,                        /* %fs */
  285.   1 * 4                                /* %gs */
  286. };

  287. static struct trad_frame_cache *
  288. i386obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
  289. {
  290.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  291.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  292.   struct trad_frame_cache *cache;
  293.   CORE_ADDR func, sp, addr;
  294.   ULONGEST cs;
  295.   const char *name;
  296.   int i;

  297.   if (*this_cache)
  298.     return *this_cache;

  299.   cache = trad_frame_cache_zalloc (this_frame);
  300.   *this_cache = cache;

  301.   func = get_frame_func (this_frame);
  302.   sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);

  303.   find_pc_partial_function (func, &name, NULL, NULL);
  304.   if (name && strncmp (name, "Xintr", 5) == 0)
  305.     addr = sp + 8;                /* It's an interrupt frame.  */
  306.   else
  307.     addr = sp;

  308.   for (i = 0; i < ARRAY_SIZE (i386obsd_tf_reg_offset); i++)
  309.     if (i386obsd_tf_reg_offset[i] != -1)
  310.       trad_frame_set_reg_addr (cache, i, addr + i386obsd_tf_reg_offset[i]);

  311.   /* Read %cs from trap frame.  */
  312.   addr += i386obsd_tf_reg_offset[I386_CS_REGNUM];
  313.   cs = read_memory_unsigned_integer (addr, 4, byte_order);
  314.   if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
  315.     {
  316.       /* Trap from user space; terminate backtrace.  */
  317.       trad_frame_set_id (cache, outer_frame_id);
  318.     }
  319.   else
  320.     {
  321.       /* Construct the frame ID using the function start.  */
  322.       trad_frame_set_id (cache, frame_id_build (sp + 8, func));
  323.     }

  324.   return cache;
  325. }

  326. static void
  327. i386obsd_trapframe_this_id (struct frame_info *this_frame,
  328.                             void **this_cache, struct frame_id *this_id)
  329. {
  330.   struct trad_frame_cache *cache =
  331.     i386obsd_trapframe_cache (this_frame, this_cache);

  332.   trad_frame_get_id (cache, this_id);
  333. }

  334. static struct value *
  335. i386obsd_trapframe_prev_register (struct frame_info *this_frame,
  336.                                   void **this_cache, int regnum)
  337. {
  338.   struct trad_frame_cache *cache =
  339.     i386obsd_trapframe_cache (this_frame, this_cache);

  340.   return trad_frame_get_register (cache, this_frame, regnum);
  341. }

  342. static int
  343. i386obsd_trapframe_sniffer (const struct frame_unwind *self,
  344.                             struct frame_info *this_frame,
  345.                             void **this_prologue_cache)
  346. {
  347.   ULONGEST cs;
  348.   const char *name;

  349.   /* Check Current Privilege Level and bail out if we're not executing
  350.      in kernel space.  */
  351.   cs = get_frame_register_unsigned (this_frame, I386_CS_REGNUM);
  352.   if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
  353.     return 0;

  354.   find_pc_partial_function (get_frame_pc (this_frame), &name, NULL, NULL);
  355.   return (name && (strcmp (name, "calltrap") == 0
  356.                    || strcmp (name, "syscall1") == 0
  357.                    || strncmp (name, "Xintr", 5) == 0
  358.                    || strncmp (name, "Xsoft", 5) == 0));
  359. }

  360. static const struct frame_unwind i386obsd_trapframe_unwind = {
  361.   /* FIXME: kettenis/20051219: This really is more like an interrupt
  362.      frame, but SIGTRAMP_FRAME would print <signal handler called>,
  363.      which really is not what we want here.  */
  364.   NORMAL_FRAME,
  365.   default_frame_unwind_stop_reason,
  366.   i386obsd_trapframe_this_id,
  367.   i386obsd_trapframe_prev_register,
  368.   NULL,
  369.   i386obsd_trapframe_sniffer
  370. };


  371. static void
  372. i386obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  373. {
  374.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  375.   /* Obviously OpenBSD is BSD-based.  */
  376.   i386bsd_init_abi (info, gdbarch);
  377.   obsd_init_abi (info, gdbarch);

  378.   /* OpenBSD has a different `struct reg'.  */
  379.   tdep->gregset_reg_offset = i386obsd_r_reg_offset;
  380.   tdep->gregset_num_regs = ARRAY_SIZE (i386obsd_r_reg_offset);
  381.   tdep->sizeof_gregset = 16 * 4;

  382.   /* OpenBSD uses -freg-struct-return by default.  */
  383.   tdep->struct_return = reg_struct_return;

  384.   /* OpenBSD uses a different memory layout.  */
  385.   tdep->sigtramp_start = i386obsd_sigtramp_start_addr;
  386.   tdep->sigtramp_end = i386obsd_sigtramp_end_addr;
  387.   tdep->sigtramp_p = i386obsd_sigtramp_p;

  388.   /* OpenBSD has a `struct sigcontext' that's different from the
  389.      original 4.3 BSD.  */
  390.   tdep->sc_reg_offset = i386obsd_sc_reg_offset;
  391.   tdep->sc_num_regs = ARRAY_SIZE (i386obsd_sc_reg_offset);

  392.   /* OpenBSD provides a user-level threads implementation.  */
  393.   bsd_uthread_set_supply_uthread (gdbarch, i386obsd_supply_uthread);
  394.   bsd_uthread_set_collect_uthread (gdbarch, i386obsd_collect_uthread);

  395.   /* Unwind kernel trap frames correctly.  */
  396.   frame_unwind_prepend_unwinder (gdbarch, &i386obsd_trapframe_unwind);
  397. }

  398. /* OpenBSD a.out.  */

  399. static void
  400. i386obsd_aout_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  401. {
  402.   i386obsd_init_abi (info, gdbarch);

  403.   /* OpenBSD a.out has a single register set.  */
  404.   set_gdbarch_iterate_over_regset_sections
  405.     (gdbarch, i386obsd_aout_iterate_over_regset_sections);
  406. }

  407. /* OpenBSD ELF.  */

  408. static void
  409. i386obsd_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  410. {
  411.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  412.   /* It's still OpenBSD.  */
  413.   i386obsd_init_abi (info, gdbarch);

  414.   /* But ELF-based.  */
  415.   i386_elf_init_abi (info, gdbarch);

  416.   /* OpenBSD ELF uses SVR4-style shared libraries.  */
  417.   set_solib_svr4_fetch_link_map_offsets
  418.     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
  419. }


  420. /* Provide a prototype to silence -Wmissing-prototypes.  */
  421. void _initialize_i386obsd_tdep (void);

  422. void
  423. _initialize_i386obsd_tdep (void)
  424. {
  425.   /* FIXME: kettenis/20021020: Since OpenBSD/i386 binaries are
  426.      indistingushable from NetBSD/i386 a.out binaries, building a GDB
  427.      that should support both these targets will probably not work as
  428.      expected.  */
  429. #define GDB_OSABI_OPENBSD_AOUT GDB_OSABI_NETBSD_AOUT

  430.   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD_AOUT,
  431.                           i386obsd_aout_init_abi);
  432.   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD_ELF,
  433.                           i386obsd_elf_init_abi);
  434. }