GDB (API)
/home/stan/gdb/src/gdb/m32r-linux-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for GNU/Linux m32r.
00002 
00003    Copyright (C) 2004-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "gdbcore.h"
00022 #include "frame.h"
00023 #include "value.h"
00024 #include "regcache.h"
00025 #include "inferior.h"
00026 #include "osabi.h"
00027 #include "reggroups.h"
00028 #include "regset.h"
00029 
00030 #include "gdb_string.h"
00031 
00032 #include "glibc-tdep.h"
00033 #include "solib-svr4.h"
00034 #include "symtab.h"
00035 
00036 #include "trad-frame.h"
00037 #include "frame-unwind.h"
00038 
00039 #include "m32r-tdep.h"
00040 #include "linux-tdep.h"
00041 
00042 
00043 
00044 /* Recognizing signal handler frames.  */
00045 
00046 /* GNU/Linux has two flavors of signals.  Normal signal handlers, and
00047    "realtime" (RT) signals.  The RT signals can provide additional
00048    information to the signal handler if the SA_SIGINFO flag is set
00049    when establishing a signal handler using `sigaction'.  It is not
00050    unlikely that future versions of GNU/Linux will support SA_SIGINFO
00051    for normal signals too.  */
00052 
00053 /* When the m32r Linux kernel calls a signal handler and the
00054    SA_RESTORER flag isn't set, the return address points to a bit of
00055    code on the stack.  This function returns whether the PC appears to
00056    be within this bit of code.
00057 
00058    The instruction sequence for normal signals is
00059        ldi    r7, #__NR_sigreturn
00060        trap   #2
00061    or 0x67 0x77 0x10 0xf2.
00062 
00063    Checking for the code sequence should be somewhat reliable, because
00064    the effect is to call the system call sigreturn.  This is unlikely
00065    to occur anywhere other than in a signal trampoline.
00066 
00067    It kind of sucks that we have to read memory from the process in
00068    order to identify a signal trampoline, but there doesn't seem to be
00069    any other way.  Therefore we only do the memory reads if no
00070    function name could be identified, which should be the case since
00071    the code is on the stack.
00072 
00073    Detection of signal trampolines for handlers that set the
00074    SA_RESTORER flag is in general not possible.  Unfortunately this is
00075    what the GNU C Library has been doing for quite some time now.
00076    However, as of version 2.1.2, the GNU C Library uses signal
00077    trampolines (named __restore and __restore_rt) that are identical
00078    to the ones used by the kernel.  Therefore, these trampolines are
00079    supported too.  */
00080 
00081 static const gdb_byte linux_sigtramp_code[] = {
00082   0x67, 0x77, 0x10, 0xf2,
00083 };
00084 
00085 /* If PC is in a sigtramp routine, return the address of the start of
00086    the routine.  Otherwise, return 0.  */
00087 
00088 static CORE_ADDR
00089 m32r_linux_sigtramp_start (CORE_ADDR pc, struct frame_info *this_frame)
00090 {
00091   gdb_byte buf[4];
00092 
00093   /* We only recognize a signal trampoline if PC is at the start of
00094      one of the instructions.  We optimize for finding the PC at the
00095      start of the instruction sequence, as will be the case when the
00096      trampoline is not the first frame on the stack.  We assume that
00097      in the case where the PC is not at the start of the instruction
00098      sequence, there will be a few trailing readable bytes on the
00099      stack.  */
00100 
00101   if (pc % 2 != 0)
00102     {
00103       if (!safe_frame_unwind_memory (this_frame, pc, buf, 2))
00104         return 0;
00105 
00106       if (memcmp (buf, linux_sigtramp_code, 2) == 0)
00107         pc -= 2;
00108       else
00109         return 0;
00110     }
00111 
00112   if (!safe_frame_unwind_memory (this_frame, pc, buf, 4))
00113     return 0;
00114 
00115   if (memcmp (buf, linux_sigtramp_code, 4) != 0)
00116     return 0;
00117 
00118   return pc;
00119 }
00120 
00121 /* This function does the same for RT signals.  Here the instruction
00122    sequence is
00123        ldi    r7, #__NR_rt_sigreturn
00124        trap   #2
00125    or 0x97 0xf0 0x00 0xad 0x10 0xf2 0xf0 0x00.
00126 
00127    The effect is to call the system call rt_sigreturn.  */
00128 
00129 static const gdb_byte linux_rt_sigtramp_code[] = {
00130   0x97, 0xf0, 0x00, 0xad, 0x10, 0xf2, 0xf0, 0x00,
00131 };
00132 
00133 /* If PC is in a RT sigtramp routine, return the address of the start
00134    of the routine.  Otherwise, return 0.  */
00135 
00136 static CORE_ADDR
00137 m32r_linux_rt_sigtramp_start (CORE_ADDR pc, struct frame_info *this_frame)
00138 {
00139   gdb_byte buf[4];
00140 
00141   /* We only recognize a signal trampoline if PC is at the start of
00142      one of the instructions.  We optimize for finding the PC at the
00143      start of the instruction sequence, as will be the case when the
00144      trampoline is not the first frame on the stack.  We assume that
00145      in the case where the PC is not at the start of the instruction
00146      sequence, there will be a few trailing readable bytes on the
00147      stack.  */
00148 
00149   if (pc % 2 != 0)
00150     return 0;
00151 
00152   if (!safe_frame_unwind_memory (this_frame, pc, buf, 4))
00153     return 0;
00154 
00155   if (memcmp (buf, linux_rt_sigtramp_code, 4) == 0)
00156     {
00157       if (!safe_frame_unwind_memory (this_frame, pc + 4, buf, 4))
00158         return 0;
00159 
00160       if (memcmp (buf, linux_rt_sigtramp_code + 4, 4) == 0)
00161         return pc;
00162     }
00163   else if (memcmp (buf, linux_rt_sigtramp_code + 4, 4) == 0)
00164     {
00165       if (!safe_frame_unwind_memory (this_frame, pc - 4, buf, 4))
00166         return 0;
00167 
00168       if (memcmp (buf, linux_rt_sigtramp_code, 4) == 0)
00169         return pc - 4;
00170     }
00171 
00172   return 0;
00173 }
00174 
00175 static int
00176 m32r_linux_pc_in_sigtramp (CORE_ADDR pc, const char *name,
00177                            struct frame_info *this_frame)
00178 {
00179   /* If we have NAME, we can optimize the search.  The trampolines are
00180      named __restore and __restore_rt.  However, they aren't dynamically
00181      exported from the shared C library, so the trampoline may appear to
00182      be part of the preceding function.  This should always be sigaction,
00183      __sigaction, or __libc_sigaction (all aliases to the same function).  */
00184   if (name == NULL || strstr (name, "sigaction") != NULL)
00185     return (m32r_linux_sigtramp_start (pc, this_frame) != 0
00186             || m32r_linux_rt_sigtramp_start (pc, this_frame) != 0);
00187 
00188   return (strcmp ("__restore", name) == 0
00189           || strcmp ("__restore_rt", name) == 0);
00190 }
00191 
00192 /* From <asm/sigcontext.h>.  */
00193 static int m32r_linux_sc_reg_offset[] = {
00194   4 * 4,                        /* r0 */
00195   5 * 4,                        /* r1 */
00196   6 * 4,                        /* r2 */
00197   7 * 4,                        /* r3 */
00198   0 * 4,                        /* r4 */
00199   1 * 4,                        /* r5 */
00200   2 * 4,                        /* r6 */
00201   8 * 4,                        /* r7 */
00202   9 * 4,                        /* r8 */
00203   10 * 4,                       /* r9 */
00204   11 * 4,                       /* r10 */
00205   12 * 4,                       /* r11 */
00206   13 * 4,                       /* r12 */
00207   21 * 4,                       /* fp */
00208   22 * 4,                       /* lr */
00209   -1 * 4,                       /* sp */
00210   16 * 4,                       /* psw */
00211   -1 * 4,                       /* cbr */
00212   23 * 4,                       /* spi */
00213   20 * 4,                       /* spu */
00214   19 * 4,                       /* bpc */
00215   17 * 4,                       /* pc */
00216   15 * 4,                       /* accl */
00217   14 * 4                        /* acch */
00218 };
00219 
00220 struct m32r_frame_cache
00221 {
00222   CORE_ADDR base, pc;
00223   struct trad_frame_saved_reg *saved_regs;
00224 };
00225 
00226 static struct m32r_frame_cache *
00227 m32r_linux_sigtramp_frame_cache (struct frame_info *this_frame,
00228                                  void **this_cache)
00229 {
00230   struct m32r_frame_cache *cache;
00231   CORE_ADDR sigcontext_addr, addr;
00232   int regnum;
00233 
00234   if ((*this_cache) != NULL)
00235     return (*this_cache);
00236   cache = FRAME_OBSTACK_ZALLOC (struct m32r_frame_cache);
00237   (*this_cache) = cache;
00238   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
00239 
00240   cache->base = get_frame_register_unsigned (this_frame, M32R_SP_REGNUM);
00241   sigcontext_addr = cache->base + 4;
00242 
00243   cache->pc = get_frame_pc (this_frame);
00244   addr = m32r_linux_sigtramp_start (cache->pc, this_frame);
00245   if (addr == 0)
00246     {
00247       /* If this is a RT signal trampoline, adjust SIGCONTEXT_ADDR
00248          accordingly.  */
00249       addr = m32r_linux_rt_sigtramp_start (cache->pc, this_frame);
00250       if (addr)
00251         sigcontext_addr += 128;
00252       else
00253         addr = get_frame_func (this_frame);
00254     }
00255   cache->pc = addr;
00256 
00257   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
00258 
00259   for (regnum = 0; regnum < sizeof (m32r_linux_sc_reg_offset) / 4; regnum++)
00260     {
00261       if (m32r_linux_sc_reg_offset[regnum] >= 0)
00262         cache->saved_regs[regnum].addr =
00263           sigcontext_addr + m32r_linux_sc_reg_offset[regnum];
00264     }
00265 
00266   return cache;
00267 }
00268 
00269 static void
00270 m32r_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
00271                                    void **this_cache,
00272                                    struct frame_id *this_id)
00273 {
00274   struct m32r_frame_cache *cache =
00275     m32r_linux_sigtramp_frame_cache (this_frame, this_cache);
00276 
00277   (*this_id) = frame_id_build (cache->base, cache->pc);
00278 }
00279 
00280 static struct value *
00281 m32r_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
00282                                          void **this_cache, int regnum)
00283 {
00284   struct m32r_frame_cache *cache =
00285     m32r_linux_sigtramp_frame_cache (this_frame, this_cache);
00286 
00287   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
00288 }
00289 
00290 static int
00291 m32r_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
00292                                    struct frame_info *this_frame,
00293                                    void **this_cache)
00294 {
00295   CORE_ADDR pc = get_frame_pc (this_frame);
00296   const char *name;
00297 
00298   find_pc_partial_function (pc, &name, NULL, NULL);
00299   if (m32r_linux_pc_in_sigtramp (pc, name, this_frame))
00300     return 1;
00301 
00302   return 0;
00303 }
00304 
00305 static const struct frame_unwind m32r_linux_sigtramp_frame_unwind = {
00306   SIGTRAMP_FRAME,
00307   default_frame_unwind_stop_reason,
00308   m32r_linux_sigtramp_frame_this_id,
00309   m32r_linux_sigtramp_frame_prev_register,
00310   NULL,
00311   m32r_linux_sigtramp_frame_sniffer
00312 };
00313 
00314 /* Mapping between the registers in `struct pt_regs'
00315    format and GDB's register array layout.  */
00316 
00317 static int m32r_pt_regs_offset[] = {
00318   4 * 4,                        /* r0 */
00319   4 * 5,                        /* r1 */
00320   4 * 6,                        /* r2 */
00321   4 * 7,                        /* r3 */
00322   4 * 0,                        /* r4 */
00323   4 * 1,                        /* r5 */
00324   4 * 2,                        /* r6 */
00325   4 * 8,                        /* r7 */
00326   4 * 9,                        /* r8 */
00327   4 * 10,                       /* r9 */
00328   4 * 11,                       /* r10 */
00329   4 * 12,                       /* r11 */
00330   4 * 13,                       /* r12 */
00331   4 * 24,                       /* fp */
00332   4 * 25,                       /* lr */
00333   4 * 23,                       /* sp */
00334   4 * 19,                       /* psw */
00335   4 * 19,                       /* cbr */
00336   4 * 26,                       /* spi */
00337   4 * 23,                       /* spu */
00338   4 * 22,                       /* bpc */
00339   4 * 20,                       /* pc */
00340   4 * 16,                       /* accl */
00341   4 * 15                        /* acch */
00342 };
00343 
00344 #define PSW_OFFSET (4 * 19)
00345 #define BBPSW_OFFSET (4 * 21)
00346 #define SPU_OFFSET (4 * 23)
00347 #define SPI_OFFSET (4 * 26)
00348 
00349 static void
00350 m32r_linux_supply_gregset (const struct regset *regset,
00351                            struct regcache *regcache, int regnum,
00352                            const void *gregs, size_t size)
00353 {
00354   const char *regs = gregs;
00355   unsigned long psw, bbpsw;
00356   int i;
00357 
00358   psw = *((unsigned long *) (regs + PSW_OFFSET));
00359   bbpsw = *((unsigned long *) (regs + BBPSW_OFFSET));
00360 
00361   for (i = 0; i < sizeof (m32r_pt_regs_offset) / 4; i++)
00362     {
00363       if (regnum != -1 && regnum != i)
00364         continue;
00365 
00366       switch (i)
00367         {
00368         case PSW_REGNUM:
00369           *((unsigned long *) (regs + m32r_pt_regs_offset[i])) =
00370             ((0x00c1 & bbpsw) << 8) | ((0xc100 & psw) >> 8);
00371           break;
00372         case CBR_REGNUM:
00373           *((unsigned long *) (regs + m32r_pt_regs_offset[i])) =
00374             ((psw >> 8) & 1);
00375           break;
00376         case M32R_SP_REGNUM:
00377           if (psw & 0x8000)
00378             *((unsigned long *) (regs + m32r_pt_regs_offset[i])) =
00379               *((unsigned long *) (regs + SPU_OFFSET));
00380           else
00381             *((unsigned long *) (regs + m32r_pt_regs_offset[i])) =
00382               *((unsigned long *) (regs + SPI_OFFSET));
00383           break;
00384         }
00385 
00386       regcache_raw_supply (regcache, i,
00387                            regs + m32r_pt_regs_offset[i]);
00388     }
00389 }
00390 
00391 static struct regset m32r_linux_gregset = {
00392   NULL, m32r_linux_supply_gregset
00393 };
00394 
00395 static const struct regset *
00396 m32r_linux_regset_from_core_section (struct gdbarch *core_arch,
00397                                      const char *sect_name, size_t sect_size)
00398 {
00399   struct gdbarch_tdep *tdep = gdbarch_tdep (core_arch);
00400   if (strcmp (sect_name, ".reg") == 0)
00401     return &m32r_linux_gregset;
00402   return NULL;
00403 }
00404 
00405 static void
00406 m32r_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
00407 {
00408   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00409 
00410   linux_init_abi (info, gdbarch);
00411 
00412   /* Since EVB register is not available for native debug, we reduce
00413      the number of registers.  */
00414   set_gdbarch_num_regs (gdbarch, M32R_NUM_REGS - 1);
00415 
00416   frame_unwind_append_unwinder (gdbarch, &m32r_linux_sigtramp_frame_unwind);
00417 
00418   /* GNU/Linux uses SVR4-style shared libraries.  */
00419   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
00420   set_solib_svr4_fetch_link_map_offsets
00421     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
00422 
00423   /* Core file support.  */
00424   set_gdbarch_regset_from_core_section
00425     (gdbarch, m32r_linux_regset_from_core_section);
00426 
00427   /* Enable TLS support.  */
00428   set_gdbarch_fetch_tls_load_module_address (gdbarch,
00429                                              svr4_fetch_objfile_link_map);
00430 }
00431 
00432 /* Provide a prototype to silence -Wmissing-prototypes.  */
00433 extern void _initialize_m32r_linux_tdep (void);
00434 
00435 void
00436 _initialize_m32r_linux_tdep (void)
00437 {
00438   gdbarch_register_osabi (bfd_arch_m32r, 0, GDB_OSABI_LINUX,
00439                           m32r_linux_init_abi);
00440 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines