gdb/mips-sde-tdep.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Target-dependent code for SDE on MIPS processors.

  2.    Copyright (C) 2014-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 "osabi.h"
  16. #include "elf-bfd.h"
  17. #include "block.h"
  18. #include "symtab.h"

  19. #include "frame.h"
  20. #include "frame-unwind.h"
  21. #include "frame-base.h"
  22. #include "trad-frame.h"

  23. #include "mips-tdep.h"

  24. /* Fill in the register cache *THIS_CACHE for THIS_FRAME for use
  25.    in the SDE frame unwinder.  */

  26. static struct trad_frame_cache *
  27. mips_sde_frame_cache (struct frame_info *this_frame, void **this_cache)
  28. {
  29.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  30.   const struct mips_regnum *regs = mips_regnum (gdbarch);
  31.   const int sizeof_reg_t = mips_abi_regsize (gdbarch);
  32.   enum mips_abi abi = mips_abi (gdbarch);
  33.   struct trad_frame_cache *cache;
  34.   CORE_ADDR xcpt_frame;
  35.   CORE_ADDR start_addr;
  36.   CORE_ADDR stack_addr;
  37.   CORE_ADDR pc;
  38.   int i;

  39.   if (*this_cache != NULL)
  40.     return *this_cache;
  41.   cache = trad_frame_cache_zalloc (this_frame);
  42.   *this_cache = cache;

  43.   /* The previous registers are held in struct xcptcontext
  44.      which is at $sp+offs

  45.      struct xcptcontext {
  46.        reg_t        sr;                CP0 Status
  47.        reg_t        cr;                CP0 Cause
  48.        reg_t        epc;                CP0 EPC
  49.        reg_t        vaddr;                CP0 BadVAddr
  50.        reg_t        regs[32];        General registers
  51.        reg_t        mdlo;                LO
  52.        reg_t        mdhi;                HI
  53.        reg_t        mdex;                ACX
  54.        ...
  55.      };
  56.   */

  57.   stack_addr = get_frame_register_signed (this_frame,
  58.                                           gdbarch_sp_regnum (gdbarch));
  59.   switch (abi)
  60.     {
  61.     case MIPS_ABI_O32:
  62.       /* 40: XCPTCONTEXT
  63.          24: xcpt_gen() argspace                (16 bytes)
  64.          16: _xcptcall() saved ra, rounded up        ( 8 bytes)
  65.          00: _xcptcall() argspace                 (16 bytes)  */
  66.       xcpt_frame = stack_addr + 40;
  67.       break;
  68.     case MIPS_ABI_N32:
  69.     case MIPS_ABI_N64:
  70.     default:                        /* Wild guess.  */
  71.       /* 16: XCPTCONTEXT
  72.          16: xcpt_gen() argspace                 ( 0 bytes)
  73.          00: _xcptcall() saved ra, rounded up        (16 bytes)  */
  74.       xcpt_frame = stack_addr + 16;
  75.       break;
  76.     }

  77.   trad_frame_set_reg_addr (cache,
  78.                            MIPS_PS_REGNUM + gdbarch_num_regs (gdbarch),
  79.                            xcpt_frame + 0 * sizeof_reg_t);
  80.   trad_frame_set_reg_addr (cache,
  81.                            regs->cause + gdbarch_num_regs (gdbarch),
  82.                            xcpt_frame + 1 * sizeof_reg_t);
  83.   trad_frame_set_reg_addr (cache,
  84.                            regs->pc + gdbarch_num_regs (gdbarch),
  85.                            xcpt_frame + 2 * sizeof_reg_t);
  86.   trad_frame_set_reg_addr (cache,
  87.                            regs->badvaddr + gdbarch_num_regs (gdbarch),
  88.                            xcpt_frame + 3 * sizeof_reg_t);
  89.   for (i = 0; i < MIPS_NUMREGS; i++)
  90.     trad_frame_set_reg_addr (cache,
  91.                              i + MIPS_ZERO_REGNUM + gdbarch_num_regs (gdbarch),
  92.                              xcpt_frame + (4 + i) * sizeof_reg_t);
  93.   trad_frame_set_reg_addr (cache,
  94.                            regs->lo + gdbarch_num_regs (gdbarch),
  95.                            xcpt_frame + 36 * sizeof_reg_t);
  96.   trad_frame_set_reg_addr (cache,
  97.                            regs->hi + gdbarch_num_regs (gdbarch),
  98.                            xcpt_frame + 37 * sizeof_reg_t);

  99.   pc = get_frame_pc (this_frame);
  100.   find_pc_partial_function (pc, NULL, &start_addr, NULL);
  101.   trad_frame_set_id (cache, frame_id_build (start_addr, stack_addr));

  102.   return cache;
  103. }

  104. /* Implement the this_id function for the SDE frame unwinder.  */

  105. static void
  106. mips_sde_frame_this_id (struct frame_info *this_frame, void **this_cache,
  107.                         struct frame_id *this_id)
  108. {
  109.   struct trad_frame_cache *this_trad_cache
  110.     = mips_sde_frame_cache (this_frame, this_cache);

  111.   trad_frame_get_id (this_trad_cache, this_id);
  112. }

  113. /* Implement the prev_register function for the SDE frame unwinder.  */

  114. static struct value *
  115. mips_sde_frame_prev_register (struct frame_info *this_frame,
  116.                               void **this_cache,
  117.                               int prev_regnum)
  118. {
  119.   struct trad_frame_cache *trad_cache
  120.     = mips_sde_frame_cache (this_frame, this_cache);

  121.   return trad_frame_get_register (trad_cache, this_frame, prev_regnum);
  122. }

  123. /* Implement the sniffer function for the SDE frame unwinder.  */

  124. static int
  125. mips_sde_frame_sniffer (const struct frame_unwind *self,
  126.                         struct frame_info *this_frame,
  127.                         void **this_cache)
  128. {
  129.   CORE_ADDR pc = get_frame_pc (this_frame);
  130.   const char *name;

  131.   find_pc_partial_function (pc, &name, NULL, NULL);
  132.   return (name
  133.           && (strcmp (name, "_xcptcall") == 0
  134.               || strcmp (name, "_sigtramp") == 0));
  135. }

  136. /* Data structure for the SDE frame unwinder.  */

  137. static const struct frame_unwind mips_sde_frame_unwind =
  138. {
  139.   SIGTRAMP_FRAME,
  140.   default_frame_unwind_stop_reason,
  141.   mips_sde_frame_this_id,
  142.   mips_sde_frame_prev_register,
  143.   NULL,
  144.   mips_sde_frame_sniffer
  145. };

  146. /* Implement the this_base, this_locals, and this_args hooks
  147.    for the normal unwinder.  */

  148. static CORE_ADDR
  149. mips_sde_frame_base_address (struct frame_info *this_frame, void **this_cache)
  150. {
  151.   struct trad_frame_cache *this_trad_cache
  152.     = mips_sde_frame_cache (this_frame, this_cache);

  153.   return trad_frame_get_this_base (this_trad_cache);
  154. }

  155. static const struct frame_base mips_sde_frame_base =
  156. {
  157.   &mips_sde_frame_unwind,
  158.   mips_sde_frame_base_address,
  159.   mips_sde_frame_base_address,
  160.   mips_sde_frame_base_address
  161. };

  162. static const struct frame_base *
  163. mips_sde_frame_base_sniffer (struct frame_info *this_frame)
  164. {
  165.   if (mips_sde_frame_sniffer (&mips_sde_frame_unwind, this_frame, NULL))
  166.     return &mips_sde_frame_base;
  167.   else
  168.     return NULL;
  169. }

  170. static void
  171. mips_sde_elf_osabi_sniff_abi_tag_sections (bfd *abfd, asection *sect,
  172.                                            void *obj)
  173. {
  174.   enum gdb_osabi *os_ident_ptr = obj;
  175.   const char *name;

  176.   name = bfd_get_section_name (abfd, sect);

  177.   /* The presence of a section with a ".sde" prefix is indicative
  178.      of an SDE binary.  */
  179.   if (strncmp (name, ".sde", 4) == 0)
  180.     *os_ident_ptr = GDB_OSABI_SDE;
  181. }

  182. /* OSABI sniffer for MIPS SDE.  */

  183. static enum gdb_osabi
  184. mips_sde_elf_osabi_sniffer (bfd *abfd)
  185. {
  186.   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
  187.   unsigned int elfosabi;

  188.   /* If the generic sniffer gets a hit, return and let other sniffers
  189.      get a crack at it.  */
  190.   bfd_map_over_sections (abfd,
  191.                          generic_elf_osabi_sniff_abi_tag_sections,
  192.                          &osabi);
  193.   if (osabi != GDB_OSABI_UNKNOWN)
  194.     return GDB_OSABI_UNKNOWN;

  195.   elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];

  196.   if (elfosabi == ELFOSABI_NONE)
  197.     {
  198.       /* When elfosabi is ELFOSABI_NONE (0), then the ELF structures in the
  199.          file are conforming to the base specification for that machine
  200.          (there are no OS-specific extensions).  In order to determine the
  201.          real OS in use we must look for OS notes that have been added.

  202.          For SDE, we simply look for sections named with .sde as prefixes.  */
  203.       bfd_map_over_sections (abfd,
  204.                              mips_sde_elf_osabi_sniff_abi_tag_sections,
  205.                              &osabi);
  206.     }
  207.   return osabi;
  208. }

  209. static void
  210. mips_sde_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  211. {
  212.   frame_unwind_append_unwinder (gdbarch, &mips_sde_frame_unwind);
  213.   frame_base_append_sniffer (gdbarch, mips_sde_frame_base_sniffer);
  214. }

  215. /* Provide a prototype to silence -Wmissing-prototypes.  */
  216. extern initialize_file_ftype _initialize_mips_sde_tdep;

  217. void
  218. _initialize_mips_sde_tdep (void)
  219. {
  220.   gdbarch_register_osabi_sniffer (bfd_arch_mips,
  221.                                   bfd_target_elf_flavour,
  222.                                   mips_sde_elf_osabi_sniffer);

  223.   gdbarch_register_osabi (bfd_arch_mips, 0, GDB_OSABI_SDE, mips_sde_init_abi);
  224. }