gdb/m32r-linux-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for GNU/Linux m32r.

  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 "gdbcore.h"
  16. #include "frame.h"
  17. #include "value.h"
  18. #include "regcache.h"
  19. #include "inferior.h"
  20. #include "osabi.h"
  21. #include "reggroups.h"
  22. #include "regset.h"

  23. #include "glibc-tdep.h"
  24. #include "solib-svr4.h"
  25. #include "symtab.h"

  26. #include "trad-frame.h"
  27. #include "frame-unwind.h"

  28. #include "m32r-tdep.h"
  29. #include "linux-tdep.h"



  30. /* Recognizing signal handler frames.  */

  31. /* GNU/Linux has two flavors of signalsNormal signal handlers, and
  32.    "realtime" (RT) signals.  The RT signals can provide additional
  33.    information to the signal handler if the SA_SIGINFO flag is set
  34.    when establishing a signal handler using `sigaction'.  It is not
  35.    unlikely that future versions of GNU/Linux will support SA_SIGINFO
  36.    for normal signals too.  */

  37. /* When the m32r Linux kernel calls a signal handler and the
  38.    SA_RESTORER flag isn't set, the return address points to a bit of
  39.    code on the stack.  This function returns whether the PC appears to
  40.    be within this bit of code.

  41.    The instruction sequence for normal signals is
  42.        ldi    r7, #__NR_sigreturn
  43.        trap   #2
  44.    or 0x67 0x77 0x10 0xf2.

  45.    Checking for the code sequence should be somewhat reliable, because
  46.    the effect is to call the system call sigreturn.  This is unlikely
  47.    to occur anywhere other than in a signal trampoline.

  48.    It kind of sucks that we have to read memory from the process in
  49.    order to identify a signal trampoline, but there doesn't seem to be
  50.    any other way.  Therefore we only do the memory reads if no
  51.    function name could be identified, which should be the case since
  52.    the code is on the stack.

  53.    Detection of signal trampolines for handlers that set the
  54.    SA_RESTORER flag is in general not possible.  Unfortunately this is
  55.    what the GNU C Library has been doing for quite some time now.
  56.    However, as of version 2.1.2, the GNU C Library uses signal
  57.    trampolines (named __restore and __restore_rt) that are identical
  58.    to the ones used by the kernel.  Therefore, these trampolines are
  59.    supported too.  */

  60. static const gdb_byte linux_sigtramp_code[] = {
  61.   0x67, 0x77, 0x10, 0xf2,
  62. };

  63. /* If PC is in a sigtramp routine, return the address of the start of
  64.    the routine.  Otherwise, return 0.  */

  65. static CORE_ADDR
  66. m32r_linux_sigtramp_start (CORE_ADDR pc, struct frame_info *this_frame)
  67. {
  68.   gdb_byte buf[4];

  69.   /* We only recognize a signal trampoline if PC is at the start of
  70.      one of the instructions.  We optimize for finding the PC at the
  71.      start of the instruction sequence, as will be the case when the
  72.      trampoline is not the first frame on the stack.  We assume that
  73.      in the case where the PC is not at the start of the instruction
  74.      sequence, there will be a few trailing readable bytes on the
  75.      stack.  */

  76.   if (pc % 2 != 0)
  77.     {
  78.       if (!safe_frame_unwind_memory (this_frame, pc, buf, 2))
  79.         return 0;

  80.       if (memcmp (buf, linux_sigtramp_code, 2) == 0)
  81.         pc -= 2;
  82.       else
  83.         return 0;
  84.     }

  85.   if (!safe_frame_unwind_memory (this_frame, pc, buf, 4))
  86.     return 0;

  87.   if (memcmp (buf, linux_sigtramp_code, 4) != 0)
  88.     return 0;

  89.   return pc;
  90. }

  91. /* This function does the same for RT signals.  Here the instruction
  92.    sequence is
  93.        ldi    r7, #__NR_rt_sigreturn
  94.        trap   #2
  95.    or 0x97 0xf0 0x00 0xad 0x10 0xf2 0xf0 0x00.

  96.    The effect is to call the system call rt_sigreturn.  */

  97. static const gdb_byte linux_rt_sigtramp_code[] = {
  98.   0x97, 0xf0, 0x00, 0xad, 0x10, 0xf2, 0xf0, 0x00,
  99. };

  100. /* If PC is in a RT sigtramp routine, return the address of the start
  101.    of the routine.  Otherwise, return 0.  */

  102. static CORE_ADDR
  103. m32r_linux_rt_sigtramp_start (CORE_ADDR pc, struct frame_info *this_frame)
  104. {
  105.   gdb_byte buf[4];

  106.   /* We only recognize a signal trampoline if PC is at the start of
  107.      one of the instructions.  We optimize for finding the PC at the
  108.      start of the instruction sequence, as will be the case when the
  109.      trampoline is not the first frame on the stack.  We assume that
  110.      in the case where the PC is not at the start of the instruction
  111.      sequence, there will be a few trailing readable bytes on the
  112.      stack.  */

  113.   if (pc % 2 != 0)
  114.     return 0;

  115.   if (!safe_frame_unwind_memory (this_frame, pc, buf, 4))
  116.     return 0;

  117.   if (memcmp (buf, linux_rt_sigtramp_code, 4) == 0)
  118.     {
  119.       if (!safe_frame_unwind_memory (this_frame, pc + 4, buf, 4))
  120.         return 0;

  121.       if (memcmp (buf, linux_rt_sigtramp_code + 4, 4) == 0)
  122.         return pc;
  123.     }
  124.   else if (memcmp (buf, linux_rt_sigtramp_code + 4, 4) == 0)
  125.     {
  126.       if (!safe_frame_unwind_memory (this_frame, pc - 4, buf, 4))
  127.         return 0;

  128.       if (memcmp (buf, linux_rt_sigtramp_code, 4) == 0)
  129.         return pc - 4;
  130.     }

  131.   return 0;
  132. }

  133. static int
  134. m32r_linux_pc_in_sigtramp (CORE_ADDR pc, const char *name,
  135.                            struct frame_info *this_frame)
  136. {
  137.   /* If we have NAME, we can optimize the search.  The trampolines are
  138.      named __restore and __restore_rt.  However, they aren't dynamically
  139.      exported from the shared C library, so the trampoline may appear to
  140.      be part of the preceding function.  This should always be sigaction,
  141.      __sigaction, or __libc_sigaction (all aliases to the same function).  */
  142.   if (name == NULL || strstr (name, "sigaction") != NULL)
  143.     return (m32r_linux_sigtramp_start (pc, this_frame) != 0
  144.             || m32r_linux_rt_sigtramp_start (pc, this_frame) != 0);

  145.   return (strcmp ("__restore", name) == 0
  146.           || strcmp ("__restore_rt", name) == 0);
  147. }

  148. /* From <asm/sigcontext.h>.  */
  149. static int m32r_linux_sc_reg_offset[] = {
  150.   4 * 4,                        /* r0 */
  151.   5 * 4,                        /* r1 */
  152.   6 * 4,                        /* r2 */
  153.   7 * 4,                        /* r3 */
  154.   0 * 4,                        /* r4 */
  155.   1 * 4,                        /* r5 */
  156.   2 * 4,                        /* r6 */
  157.   8 * 4,                        /* r7 */
  158.   9 * 4,                        /* r8 */
  159.   10 * 4,                        /* r9 */
  160.   11 * 4,                        /* r10 */
  161.   12 * 4,                        /* r11 */
  162.   13 * 4,                        /* r12 */
  163.   21 * 4,                        /* fp */
  164.   22 * 4,                        /* lr */
  165.   -1 * 4,                        /* sp */
  166.   16 * 4,                        /* psw */
  167.   -1 * 4,                        /* cbr */
  168.   23 * 4,                        /* spi */
  169.   20 * 4,                        /* spu */
  170.   19 * 4,                        /* bpc */
  171.   17 * 4,                        /* pc */
  172.   15 * 4,                        /* accl */
  173.   14 * 4                        /* acch */
  174. };

  175. struct m32r_frame_cache
  176. {
  177.   CORE_ADDR base, pc;
  178.   struct trad_frame_saved_reg *saved_regs;
  179. };

  180. static struct m32r_frame_cache *
  181. m32r_linux_sigtramp_frame_cache (struct frame_info *this_frame,
  182.                                  void **this_cache)
  183. {
  184.   struct m32r_frame_cache *cache;
  185.   CORE_ADDR sigcontext_addr, addr;
  186.   int regnum;

  187.   if ((*this_cache) != NULL)
  188.     return (*this_cache);
  189.   cache = FRAME_OBSTACK_ZALLOC (struct m32r_frame_cache);
  190.   (*this_cache) = cache;
  191.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  192.   cache->base = get_frame_register_unsigned (this_frame, M32R_SP_REGNUM);
  193.   sigcontext_addr = cache->base + 4;

  194.   cache->pc = get_frame_pc (this_frame);
  195.   addr = m32r_linux_sigtramp_start (cache->pc, this_frame);
  196.   if (addr == 0)
  197.     {
  198.       /* If this is a RT signal trampoline, adjust SIGCONTEXT_ADDR
  199.          accordingly.  */
  200.       addr = m32r_linux_rt_sigtramp_start (cache->pc, this_frame);
  201.       if (addr)
  202.         sigcontext_addr += 128;
  203.       else
  204.         addr = get_frame_func (this_frame);
  205.     }
  206.   cache->pc = addr;

  207.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  208.   for (regnum = 0; regnum < sizeof (m32r_linux_sc_reg_offset) / 4; regnum++)
  209.     {
  210.       if (m32r_linux_sc_reg_offset[regnum] >= 0)
  211.         cache->saved_regs[regnum].addr =
  212.           sigcontext_addr + m32r_linux_sc_reg_offset[regnum];
  213.     }

  214.   return cache;
  215. }

  216. static void
  217. m32r_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
  218.                                    void **this_cache,
  219.                                    struct frame_id *this_id)
  220. {
  221.   struct m32r_frame_cache *cache =
  222.     m32r_linux_sigtramp_frame_cache (this_frame, this_cache);

  223.   (*this_id) = frame_id_build (cache->base, cache->pc);
  224. }

  225. static struct value *
  226. m32r_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
  227.                                          void **this_cache, int regnum)
  228. {
  229.   struct m32r_frame_cache *cache =
  230.     m32r_linux_sigtramp_frame_cache (this_frame, this_cache);

  231.   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  232. }

  233. static int
  234. m32r_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
  235.                                    struct frame_info *this_frame,
  236.                                    void **this_cache)
  237. {
  238.   CORE_ADDR pc = get_frame_pc (this_frame);
  239.   const char *name;

  240.   find_pc_partial_function (pc, &name, NULL, NULL);
  241.   if (m32r_linux_pc_in_sigtramp (pc, name, this_frame))
  242.     return 1;

  243.   return 0;
  244. }

  245. static const struct frame_unwind m32r_linux_sigtramp_frame_unwind = {
  246.   SIGTRAMP_FRAME,
  247.   default_frame_unwind_stop_reason,
  248.   m32r_linux_sigtramp_frame_this_id,
  249.   m32r_linux_sigtramp_frame_prev_register,
  250.   NULL,
  251.   m32r_linux_sigtramp_frame_sniffer
  252. };

  253. /* Mapping between the registers in `struct pt_regs'
  254.    format and GDB's register array layout.  */

  255. static int m32r_pt_regs_offset[] = {
  256.   4 * 4,                        /* r0 */
  257.   4 * 5,                        /* r1 */
  258.   4 * 6,                        /* r2 */
  259.   4 * 7,                        /* r3 */
  260.   4 * 0,                        /* r4 */
  261.   4 * 1,                        /* r5 */
  262.   4 * 2,                        /* r6 */
  263.   4 * 8,                        /* r7 */
  264.   4 * 9,                        /* r8 */
  265.   4 * 10,                        /* r9 */
  266.   4 * 11,                        /* r10 */
  267.   4 * 12,                        /* r11 */
  268.   4 * 13,                        /* r12 */
  269.   4 * 24,                        /* fp */
  270.   4 * 25,                        /* lr */
  271.   4 * 23,                        /* sp */
  272.   4 * 19,                        /* psw */
  273.   4 * 19,                        /* cbr */
  274.   4 * 26,                        /* spi */
  275.   4 * 23,                        /* spu */
  276.   4 * 22,                        /* bpc */
  277.   4 * 20,                        /* pc */
  278.   4 * 16,                        /* accl */
  279.   4 * 15                        /* acch */
  280. };

  281. #define PSW_OFFSET (4 * 19)
  282. #define BBPSW_OFFSET (4 * 21)
  283. #define SPU_OFFSET (4 * 23)
  284. #define SPI_OFFSET (4 * 26)

  285. #define M32R_LINUX_GREGS_SIZE (4 * 28)

  286. static void
  287. m32r_linux_supply_gregset (const struct regset *regset,
  288.                            struct regcache *regcache, int regnum,
  289.                            const void *gregs, size_t size)
  290. {
  291.   const gdb_byte *regs = gregs;
  292.   enum bfd_endian byte_order =
  293.     gdbarch_byte_order (get_regcache_arch (regcache));
  294.   ULONGEST psw, bbpsw;
  295.   gdb_byte buf[4];
  296.   const gdb_byte *p;
  297.   int i;

  298.   psw = extract_unsigned_integer (regs + PSW_OFFSET, 4, byte_order);
  299.   bbpsw = extract_unsigned_integer (regs + BBPSW_OFFSET, 4, byte_order);
  300.   psw = ((0x00c1 & bbpsw) << 8) | ((0xc100 & psw) >> 8);

  301.   for (i = 0; i < ARRAY_SIZE (m32r_pt_regs_offset); i++)
  302.     {
  303.       if (regnum != -1 && regnum != i)
  304.         continue;

  305.       switch (i)
  306.         {
  307.         case PSW_REGNUM:
  308.           store_unsigned_integer (buf, 4, byte_order, psw);
  309.           p = buf;
  310.           break;
  311.         case CBR_REGNUM:
  312.           store_unsigned_integer (buf, 4, byte_order, psw & 1);
  313.           p = buf;
  314.           break;
  315.         case M32R_SP_REGNUM:
  316.           p = regs + ((psw & 0x80) ? SPU_OFFSET : SPI_OFFSET);
  317.           break;
  318.         default:
  319.           p = regs + m32r_pt_regs_offset[i];
  320.         }

  321.       regcache_raw_supply (regcache, i, p);
  322.     }
  323. }

  324. static void
  325. m32r_linux_collect_gregset (const struct regset *regset,
  326.                             const struct regcache *regcache,
  327.                             int regnum, void *gregs, size_t size)
  328. {
  329.   gdb_byte *regs = gregs;
  330.   int i;
  331.   enum bfd_endian byte_order =
  332.     gdbarch_byte_order (get_regcache_arch (regcache));
  333.   ULONGEST psw;
  334.   gdb_byte buf[4];

  335.   regcache_raw_collect (regcache, PSW_REGNUM, buf);
  336.   psw = extract_unsigned_integer (buf, 4, byte_order);

  337.   for (i = 0; i < ARRAY_SIZE (m32r_pt_regs_offset); i++)
  338.     {
  339.       if (regnum != -1 && regnum != i)
  340.         continue;

  341.       switch (i)
  342.         {
  343.         case PSW_REGNUM:
  344.           store_unsigned_integer (regs + PSW_OFFSET, 4, byte_order,
  345.                                   (psw & 0xc1) << 8);
  346.           store_unsigned_integer (regs + BBPSW_OFFSET, 4, byte_order,
  347.                                   (psw >> 8) & 0xc1);
  348.           break;
  349.         case CBR_REGNUM:
  350.           break;
  351.         case M32R_SP_REGNUM:
  352.           regcache_raw_collect (regcache, i, regs
  353.                                 + ((psw & 0x80) ? SPU_OFFSET : SPI_OFFSET));
  354.           break;
  355.         default:
  356.           regcache_raw_collect (regcache, i,
  357.                                 regs + m32r_pt_regs_offset[i]);
  358.         }
  359.     }
  360. }

  361. static const struct regset m32r_linux_gregset = {
  362.   NULL,
  363.   m32r_linux_supply_gregset, m32r_linux_collect_gregset
  364. };

  365. static void
  366. m32r_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
  367.                                          iterate_over_regset_sections_cb *cb,
  368.                                          void *cb_data,
  369.                                          const struct regcache *regcache)
  370. {
  371.   cb (".reg", M32R_LINUX_GREGS_SIZE, &m32r_linux_gregset, NULL, cb_data);
  372. }

  373. static void
  374. m32r_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  375. {
  376.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  377.   linux_init_abi (info, gdbarch);

  378.   /* Since EVB register is not available for native debug, we reduce
  379.      the number of registers.  */
  380.   set_gdbarch_num_regs (gdbarch, M32R_NUM_REGS - 1);

  381.   frame_unwind_append_unwinder (gdbarch, &m32r_linux_sigtramp_frame_unwind);

  382.   /* GNU/Linux uses SVR4-style shared libraries.  */
  383.   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
  384.   set_solib_svr4_fetch_link_map_offsets
  385.     (gdbarch, svr4_ilp32_fetch_link_map_offsets);

  386.   /* Core file support.  */
  387.   set_gdbarch_iterate_over_regset_sections
  388.     (gdbarch, m32r_linux_iterate_over_regset_sections);

  389.   /* Enable TLS support.  */
  390.   set_gdbarch_fetch_tls_load_module_address (gdbarch,
  391.                                              svr4_fetch_objfile_link_map);
  392. }

  393. /* Provide a prototype to silence -Wmissing-prototypes.  */
  394. extern void _initialize_m32r_linux_tdep (void);

  395. void
  396. _initialize_m32r_linux_tdep (void)
  397. {
  398.   gdbarch_register_osabi (bfd_arch_m32r, 0, GDB_OSABI_LINUX,
  399.                           m32r_linux_init_abi);
  400. }