gdb/sparcobsd-tdep.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

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

  2.    Copyright (C) 2004-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 "floatformat.h"
  16. #include "frame.h"
  17. #include "frame-unwind.h"
  18. #include "gdbcore.h"
  19. #include "osabi.h"
  20. #include "regcache.h"
  21. #include "symtab.h"
  22. #include "trad-frame.h"

  23. #include "obsd-tdep.h"
  24. #include "sparc-tdep.h"
  25. #include "solib-svr4.h"
  26. #include "bsd-uthread.h"

  27. /* Signal trampolines.  */

  28. /* The OpenBSD kernel maps the signal trampoline at some random
  29.    location in user space, which means that the traditional BSD way of
  30.    detecting it won't work.

  31.    The signal trampoline will be mapped at an address that is page
  32.    aligned.  We recognize the signal trampoline by looking for the
  33.    sigreturn system call.  */

  34. static const int sparc32obsd_page_size = 4096;

  35. static int
  36. sparc32obsd_pc_in_sigtramp (CORE_ADDR pc, const char *name)
  37. {
  38.   CORE_ADDR start_pc = (pc & ~(sparc32obsd_page_size - 1));
  39.   unsigned long insn;

  40.   if (name)
  41.     return 0;

  42.   /* Check for "restore %g0, SYS_sigreturn, %g1".  */
  43.   insn = sparc_fetch_instruction (start_pc + 0xec);
  44.   if (insn != 0x83e82067)
  45.     return 0;

  46.   /* Check for "t ST_SYSCALL".  */
  47.   insn = sparc_fetch_instruction (start_pc + 0xf4);
  48.   if (insn != 0x91d02000)
  49.     return 0;

  50.   return 1;
  51. }

  52. static struct sparc_frame_cache *
  53. sparc32obsd_sigtramp_frame_cache (struct frame_info *this_frame,
  54.                                   void **this_cache)
  55. {
  56.   struct sparc_frame_cache *cache;
  57.   CORE_ADDR addr;

  58.   if (*this_cache)
  59.     return *this_cache;

  60.   cache = sparc_frame_cache (this_frame, this_cache);
  61.   gdb_assert (cache == *this_cache);

  62.   /* If we couldn't find the frame's function, we're probably dealing
  63.      with an on-stack signal trampoline.  */
  64.   if (cache->pc == 0)
  65.     {
  66.       cache->pc = get_frame_pc (this_frame);
  67.       cache->pc &= ~(sparc32obsd_page_size - 1);

  68.       /* Since we couldn't find the frame's function, the cache was
  69.          initialized under the assumption that we're frameless.  */
  70.       sparc_record_save_insn (cache);
  71.       addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
  72.       cache->base = addr;
  73.     }

  74.   cache->saved_regs = sparc32nbsd_sigcontext_saved_regs (this_frame);

  75.   return cache;
  76. }

  77. static void
  78. sparc32obsd_sigtramp_frame_this_id (struct frame_info *this_frame,
  79.                                     void **this_cache,
  80.                                     struct frame_id *this_id)
  81. {
  82.   struct sparc_frame_cache *cache =
  83.     sparc32obsd_sigtramp_frame_cache (this_frame, this_cache);

  84.   (*this_id) = frame_id_build (cache->base, cache->pc);
  85. }

  86. static struct value *
  87. sparc32obsd_sigtramp_frame_prev_register (struct frame_info *this_frame,
  88.                                           void **this_cache, int regnum)
  89. {
  90.   struct sparc_frame_cache *cache =
  91.     sparc32obsd_sigtramp_frame_cache (this_frame, this_cache);

  92.   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  93. }

  94. static int
  95. sparc32obsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
  96.                                     struct frame_info *this_frame,
  97.                                     void **this_cache)
  98. {
  99.   CORE_ADDR pc = get_frame_pc (this_frame);
  100.   const char *name;

  101.   find_pc_partial_function (pc, &name, NULL, NULL);
  102.   if (sparc32obsd_pc_in_sigtramp (pc, name))
  103.     return 1;

  104.   return 0;
  105. }
  106. static const struct frame_unwind sparc32obsd_sigtramp_frame_unwind =
  107. {
  108.   SIGTRAMP_FRAME,
  109.   default_frame_unwind_stop_reason,
  110.   sparc32obsd_sigtramp_frame_this_id,
  111.   sparc32obsd_sigtramp_frame_prev_register,
  112.   NULL,
  113.   sparc32obsd_sigtramp_frame_sniffer
  114. };



  115. /* Offset wthin the thread structure where we can find %fp and %i7.  */
  116. #define SPARC32OBSD_UTHREAD_FP_OFFSET        128
  117. #define SPARC32OBSD_UTHREAD_PC_OFFSET        132

  118. static void
  119. sparc32obsd_supply_uthread (struct regcache *regcache,
  120.                             int regnum, CORE_ADDR addr)
  121. {
  122.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  123.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  124.   CORE_ADDR fp, fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;
  125.   gdb_byte buf[4];

  126.   gdb_assert (regnum >= -1);

  127.   fp = read_memory_unsigned_integer (fp_addr, 4, byte_order);
  128.   if (regnum == SPARC_SP_REGNUM || regnum == -1)
  129.     {
  130.       store_unsigned_integer (buf, 4, byte_order, fp);
  131.       regcache_raw_supply (regcache, SPARC_SP_REGNUM, buf);

  132.       if (regnum == SPARC_SP_REGNUM)
  133.         return;
  134.     }

  135.   if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM
  136.       || regnum == -1)
  137.     {
  138.       CORE_ADDR i7, i7_addr = addr + SPARC32OBSD_UTHREAD_PC_OFFSET;

  139.       i7 = read_memory_unsigned_integer (i7_addr, 4, byte_order);
  140.       if (regnum == SPARC32_PC_REGNUM || regnum == -1)
  141.         {
  142.           store_unsigned_integer (buf, 4, byte_order, i7 + 8);
  143.           regcache_raw_supply (regcache, SPARC32_PC_REGNUM, buf);
  144.         }
  145.       if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
  146.         {
  147.           store_unsigned_integer (buf, 4, byte_order, i7 + 12);
  148.           regcache_raw_supply (regcache, SPARC32_NPC_REGNUM, buf);
  149.         }

  150.       if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
  151.         return;
  152.     }

  153.   sparc_supply_rwindow (regcache, fp, regnum);
  154. }

  155. static void
  156. sparc32obsd_collect_uthread(const struct regcache *regcache,
  157.                             int regnum, CORE_ADDR addr)
  158. {
  159.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  160.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  161.   CORE_ADDR sp;
  162.   gdb_byte buf[4];

  163.   gdb_assert (regnum >= -1);

  164.   if (regnum == SPARC_SP_REGNUM || regnum == -1)
  165.     {
  166.       CORE_ADDR fp_addr = addr + SPARC32OBSD_UTHREAD_FP_OFFSET;

  167.       regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
  168.       write_memory (fp_addr,buf, 4);
  169.     }

  170.   if (regnum == SPARC32_PC_REGNUM || regnum == -1)
  171.     {
  172.       CORE_ADDR i7, i7_addr = addr + SPARC32OBSD_UTHREAD_PC_OFFSET;

  173.       regcache_raw_collect (regcache, SPARC32_PC_REGNUM, buf);
  174.       i7 = extract_unsigned_integer (buf, 4, byte_order) - 8;
  175.       write_memory_unsigned_integer (i7_addr, 4, byte_order, i7);

  176.       if (regnum == SPARC32_PC_REGNUM)
  177.         return;
  178.     }

  179.   regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
  180.   sp = extract_unsigned_integer (buf, 4, byte_order);
  181.   sparc_collect_rwindow (regcache, sp, regnum);
  182. }


  183. static void
  184. sparc32obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  185. {
  186.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  187.   /* OpenBSD/sparc is very similar to NetBSD/sparc ELF.  */
  188.   sparc32nbsd_elf_init_abi (info, gdbarch);

  189.   set_gdbarch_skip_solib_resolver (gdbarch, obsd_skip_solib_resolver);

  190.   frame_unwind_append_unwinder (gdbarch, &sparc32obsd_sigtramp_frame_unwind);

  191.   /* OpenBSD provides a user-level threads implementation.  */
  192.   bsd_uthread_set_supply_uthread (gdbarch, sparc32obsd_supply_uthread);
  193.   bsd_uthread_set_collect_uthread (gdbarch, sparc32obsd_collect_uthread);
  194. }


  195. /* Provide a prototype to silence -Wmissing-prototypes.  */
  196. void _initialize_sparc32obsd_tdep (void);

  197. void
  198. _initialize_sparc32obsd_tdep (void)
  199. {
  200.   gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_OPENBSD_ELF,
  201.                           sparc32obsd_init_abi);
  202. }