gdb/vax-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Target-dependent code for the VAX.

  2.    Copyright (C) 1986-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 "dis-asm.h"
  17. #include "floatformat.h"
  18. #include "frame.h"
  19. #include "frame-base.h"
  20. #include "frame-unwind.h"
  21. #include "gdbcore.h"
  22. #include "gdbtypes.h"
  23. #include "osabi.h"
  24. #include "regcache.h"
  25. #include "regset.h"
  26. #include "trad-frame.h"
  27. #include "value.h"

  28. #include "vax-tdep.h"

  29. /* Return the name of register REGNUM.  */

  30. static const char *
  31. vax_register_name (struct gdbarch *gdbarch, int regnum)
  32. {
  33.   static char *register_names[] =
  34.   {
  35.     "r0", "r1", "r2""r3""r4", "r5", "r6", "r7",
  36.     "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc",
  37.     "ps",
  38.   };

  39.   if (regnum >= 0 && regnum < ARRAY_SIZE (register_names))
  40.     return register_names[regnum];

  41.   return NULL;
  42. }

  43. /* Return the GDB type object for the "standard" data type of data in
  44.    register REGNUM.  */

  45. static struct type *
  46. vax_register_type (struct gdbarch *gdbarch, int regnum)
  47. {
  48.   return builtin_type (gdbarch)->builtin_int;
  49. }

  50. /* Core file support.  */

  51. /* Supply register REGNUM from the buffer specified by GREGS and LEN
  52.    in the general-purpose register set REGSET to register cache
  53.    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */

  54. static void
  55. vax_supply_gregset (const struct regset *regset, struct regcache *regcache,
  56.                     int regnum, const void *gregs, size_t len)
  57. {
  58.   const gdb_byte *regs = gregs;
  59.   int i;

  60.   for (i = 0; i < VAX_NUM_REGS; i++)
  61.     {
  62.       if (regnum == i || regnum == -1)
  63.         regcache_raw_supply (regcache, i, regs + i * 4);
  64.     }
  65. }

  66. /* VAX register set.  */

  67. static const struct regset vax_gregset =
  68. {
  69.   NULL,
  70.   vax_supply_gregset
  71. };

  72. /* Iterate over core file register note sections.  */

  73. static void
  74. vax_iterate_over_regset_sections (struct gdbarch *gdbarch,
  75.                                   iterate_over_regset_sections_cb *cb,
  76.                                   void *cb_data,
  77.                                   const struct regcache *regcache)
  78. {
  79.   cb (".reg", VAX_NUM_REGS * 4, &vax_gregset, NULL, cb_data);
  80. }

  81. /* The VAX UNIX calling convention uses R1 to pass a structure return
  82.    value address instead of passing it as a first (hidden) argument as
  83.    the VMS calling convention suggests.  */

  84. static CORE_ADDR
  85. vax_store_arguments (struct regcache *regcache, int nargs,
  86.                      struct value **args, CORE_ADDR sp)
  87. {
  88.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  89.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  90.   gdb_byte buf[4];
  91.   int count = 0;
  92.   int i;

  93.   /* We create an argument list on the stack, and make the argument
  94.      pointer to it.  */

  95.   /* Push arguments in reverse order.  */
  96.   for (i = nargs - 1; i >= 0; i--)
  97.     {
  98.       int len = TYPE_LENGTH (value_enclosing_type (args[i]));

  99.       sp -= (len + 3) & ~3;
  100.       count += (len + 3) / 4;
  101.       write_memory (sp, value_contents_all (args[i]), len);
  102.     }

  103.   /* Push argument count.  */
  104.   sp -= 4;
  105.   store_unsigned_integer (buf, 4, byte_order, count);
  106.   write_memory (sp, buf, 4);

  107.   /* Update the argument pointer.  */
  108.   store_unsigned_integer (buf, 4, byte_order, sp);
  109.   regcache_cooked_write (regcache, VAX_AP_REGNUM, buf);

  110.   return sp;
  111. }

  112. static CORE_ADDR
  113. vax_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  114.                      struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  115.                      struct value **args, CORE_ADDR sp, int struct_return,
  116.                      CORE_ADDR struct_addr)
  117. {
  118.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  119.   CORE_ADDR fp = sp;
  120.   gdb_byte buf[4];

  121.   /* Set up the function arguments.  */
  122.   sp = vax_store_arguments (regcache, nargs, args, sp);

  123.   /* Store return value address.  */
  124.   if (struct_return)
  125.     regcache_cooked_write_unsigned (regcache, VAX_R1_REGNUM, struct_addr);

  126.   /* Store return address in the PC slot.  */
  127.   sp -= 4;
  128.   store_unsigned_integer (buf, 4, byte_order, bp_addr);
  129.   write_memory (sp, buf, 4);

  130.   /* Store the (fake) frame pointer in the FP slot.  */
  131.   sp -= 4;
  132.   store_unsigned_integer (buf, 4, byte_order, fp);
  133.   write_memory (sp, buf, 4);

  134.   /* Skip the AP slot.  */
  135.   sp -= 4;

  136.   /* Store register save mask and control bits.  */
  137.   sp -= 4;
  138.   store_unsigned_integer (buf, 4, byte_order, 0);
  139.   write_memory (sp, buf, 4);

  140.   /* Store condition handler.  */
  141.   sp -= 4;
  142.   store_unsigned_integer (buf, 4, byte_order, 0);
  143.   write_memory (sp, buf, 4);

  144.   /* Update the stack pointer and frame pointer.  */
  145.   store_unsigned_integer (buf, 4, byte_order, sp);
  146.   regcache_cooked_write (regcache, VAX_SP_REGNUM, buf);
  147.   regcache_cooked_write (regcache, VAX_FP_REGNUM, buf);

  148.   /* Return the saved (fake) frame pointer.  */
  149.   return fp;
  150. }

  151. static struct frame_id
  152. vax_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  153. {
  154.   CORE_ADDR fp;

  155.   fp = get_frame_register_unsigned (this_frame, VAX_FP_REGNUM);
  156.   return frame_id_build (fp, get_frame_pc (this_frame));
  157. }


  158. static enum return_value_convention
  159. vax_return_value (struct gdbarch *gdbarch, struct value *function,
  160.                   struct type *type, struct regcache *regcache,
  161.                   gdb_byte *readbuf, const gdb_byte *writebuf)
  162. {
  163.   int len = TYPE_LENGTH (type);
  164.   gdb_byte buf[8];

  165.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  166.       || TYPE_CODE (type) == TYPE_CODE_UNION
  167.       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
  168.     {
  169.       /* The default on VAX is to return structures in static memory.
  170.          Consequently a function must return the address where we can
  171.          find the return value.  */

  172.       if (readbuf)
  173.         {
  174.           ULONGEST addr;

  175.           regcache_raw_read_unsigned (regcache, VAX_R0_REGNUM, &addr);
  176.           read_memory (addr, readbuf, len);
  177.         }

  178.       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
  179.     }

  180.   if (readbuf)
  181.     {
  182.       /* Read the contents of R0 and (if necessary) R1.  */
  183.       regcache_cooked_read (regcache, VAX_R0_REGNUM, buf);
  184.       if (len > 4)
  185.         regcache_cooked_read (regcache, VAX_R1_REGNUM, buf + 4);
  186.       memcpy (readbuf, buf, len);
  187.     }
  188.   if (writebuf)
  189.     {
  190.       /* Read the contents to R0 and (if necessary) R1.  */
  191.       memcpy (buf, writebuf, len);
  192.       regcache_cooked_write (regcache, VAX_R0_REGNUM, buf);
  193.       if (len > 4)
  194.         regcache_cooked_write (regcache, VAX_R1_REGNUM, buf + 4);
  195.     }

  196.   return RETURN_VALUE_REGISTER_CONVENTION;
  197. }


  198. /* Use the program counter to determine the contents and size of a
  199.    breakpoint instruction.  Return a pointer to a string of bytes that
  200.    encode a breakpoint instruction, store the length of the string in
  201.    *LEN and optionally adjust *PC to point to the correct memory
  202.    location for inserting the breakpoint.  */

  203. static const gdb_byte *
  204. vax_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
  205. {
  206.   static gdb_byte break_insn[] = { 3 };

  207.   *len = sizeof (break_insn);
  208.   return break_insn;
  209. }

  210. /* Advance PC across any function entry prologue instructions
  211.    to reach some "real" code.  */

  212. static CORE_ADDR
  213. vax_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  214. {
  215.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  216.   gdb_byte op = read_memory_unsigned_integer (pc, 1, byte_order);

  217.   if (op == 0x11)
  218.     pc += 2;                        /* skip brb */
  219.   if (op == 0x31)
  220.     pc += 3;                        /* skip brw */
  221.   if (op == 0xC2
  222.       && read_memory_unsigned_integer (pc + 2, 1, byte_order) == 0x5E)
  223.     pc += 3;                        /* skip subl2 */
  224.   if (op == 0x9E
  225.       && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xAE
  226.       && read_memory_unsigned_integer (pc + 3, 1, byte_order) == 0x5E)
  227.     pc += 4;                        /* skip movab */
  228.   if (op == 0x9E
  229.       && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xCE
  230.       && read_memory_unsigned_integer (pc + 4, 1, byte_order) == 0x5E)
  231.     pc += 5;                        /* skip movab */
  232.   if (op == 0x9E
  233.       && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xEE
  234.       && read_memory_unsigned_integer (pc + 6, 1, byte_order) == 0x5E)
  235.     pc += 7;                        /* skip movab */

  236.   return pc;
  237. }


  238. /* Unwinding the stack is relatively easy since the VAX has a
  239.    dedicated frame pointer, and frames are set up automatically as the
  240.    result of a function call.  Most of the relevant information can be
  241.    inferred from the documentation of the Procedure Call Instructions
  242.    in the VAX MACRO and Instruction Set Reference Manual.  */

  243. struct vax_frame_cache
  244. {
  245.   /* Base address.  */
  246.   CORE_ADDR base;

  247.   /* Table of saved registers.  */
  248.   struct trad_frame_saved_reg *saved_regs;
  249. };

  250. static struct vax_frame_cache *
  251. vax_frame_cache (struct frame_info *this_frame, void **this_cache)
  252. {
  253.   struct vax_frame_cache *cache;
  254.   CORE_ADDR addr;
  255.   ULONGEST mask;
  256.   int regnum;

  257.   if (*this_cache)
  258.     return *this_cache;

  259.   /* Allocate a new cache.  */
  260.   cache = FRAME_OBSTACK_ZALLOC (struct vax_frame_cache);
  261.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  262.   /* The frame pointer is used as the base for the frame.  */
  263.   cache->base = get_frame_register_unsigned (this_frame, VAX_FP_REGNUM);
  264.   if (cache->base == 0)
  265.     return cache;

  266.   /* The register save mask and control bits determine the layout of
  267.      the stack frame.  */
  268.   mask = get_frame_memory_unsigned (this_frame, cache->base + 4, 4) >> 16;

  269.   /* These are always saved.  */
  270.   cache->saved_regs[VAX_PC_REGNUM].addr = cache->base + 16;
  271.   cache->saved_regs[VAX_FP_REGNUM].addr = cache->base + 12;
  272.   cache->saved_regs[VAX_AP_REGNUM].addr = cache->base + 8;
  273.   cache->saved_regs[VAX_PS_REGNUM].addr = cache->base + 4;

  274.   /* Scan the register save mask and record the location of the saved
  275.      registers.  */
  276.   addr = cache->base + 20;
  277.   for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++)
  278.     {
  279.       if (mask & (1 << regnum))
  280.         {
  281.           cache->saved_regs[regnum].addr = addr;
  282.           addr += 4;
  283.         }
  284.     }

  285.   /* The CALLS/CALLG flag determines whether this frame has a General
  286.      Argument List or a Stack Argument List.  */
  287.   if (mask & (1 << 13))
  288.     {
  289.       ULONGEST numarg;

  290.       /* This is a procedure with Stack Argument List.  Adjust the
  291.          stack address for the arguments that were pushed onto the
  292.          stack.  The return instruction will automatically pop the
  293.          arguments from the stack.  */
  294.       numarg = get_frame_memory_unsigned (this_frame, addr, 1);
  295.       addr += 4 + numarg * 4;
  296.     }

  297.   /* Bits 1:0 of the stack pointer were saved in the control bits.  */
  298.   trad_frame_set_value (cache->saved_regs, VAX_SP_REGNUM, addr + (mask >> 14));

  299.   return cache;
  300. }

  301. static void
  302. vax_frame_this_id (struct frame_info *this_frame, void **this_cache,
  303.                    struct frame_id *this_id)
  304. {
  305.   struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);

  306.   /* This marks the outermost frame.  */
  307.   if (cache->base == 0)
  308.     return;

  309.   (*this_id) = frame_id_build (cache->base, get_frame_func (this_frame));
  310. }

  311. static struct value *
  312. vax_frame_prev_register (struct frame_info *this_frame,
  313.                          void **this_cache, int regnum)
  314. {
  315.   struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);

  316.   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  317. }

  318. static const struct frame_unwind vax_frame_unwind =
  319. {
  320.   NORMAL_FRAME,
  321.   default_frame_unwind_stop_reason,
  322.   vax_frame_this_id,
  323.   vax_frame_prev_register,
  324.   NULL,
  325.   default_frame_sniffer
  326. };


  327. static CORE_ADDR
  328. vax_frame_base_address (struct frame_info *this_frame, void **this_cache)
  329. {
  330.   struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);

  331.   return cache->base;
  332. }

  333. static CORE_ADDR
  334. vax_frame_args_address (struct frame_info *this_frame, void **this_cache)
  335. {
  336.   return get_frame_register_unsigned (this_frame, VAX_AP_REGNUM);
  337. }

  338. static const struct frame_base vax_frame_base =
  339. {
  340.   &vax_frame_unwind,
  341.   vax_frame_base_address,
  342.   vax_frame_base_address,
  343.   vax_frame_args_address
  344. };

  345. /* Return number of arguments for FRAME.  */

  346. static int
  347. vax_frame_num_args (struct frame_info *frame)
  348. {
  349.   CORE_ADDR args;

  350.   /* Assume that the argument pointer for the outermost frame is
  351.      hosed, as is the case on NetBSD/vax ELF.  */
  352.   if (get_frame_base_address (frame) == 0)
  353.     return 0;

  354.   args = get_frame_register_unsigned (frame, VAX_AP_REGNUM);
  355.   return get_frame_memory_unsigned (frame, args, 1);
  356. }

  357. static CORE_ADDR
  358. vax_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  359. {
  360.   return frame_unwind_register_unsigned (next_frame, VAX_PC_REGNUM);
  361. }


  362. /* Initialize the current architecture based on INFO.  If possible, re-use an
  363.    architecture from ARCHES, which is a list of architectures already created
  364.    during this debugging session.

  365.    Called e.g. at program startup, when reading a core file, and when reading
  366.    a binary file.  */

  367. static struct gdbarch *
  368. vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  369. {
  370.   struct gdbarch *gdbarch;

  371.   /* If there is already a candidate, use it.  */
  372.   arches = gdbarch_list_lookup_by_info (arches, &info);
  373.   if (arches != NULL)
  374.     return arches->gdbarch;

  375.   gdbarch = gdbarch_alloc (&info, NULL);

  376.   set_gdbarch_float_format (gdbarch, floatformats_vax_f);
  377.   set_gdbarch_double_format (gdbarch, floatformats_vax_d);
  378.   set_gdbarch_long_double_format (gdbarch, floatformats_vax_d);
  379.   set_gdbarch_long_double_bit (gdbarch, 64);

  380.   /* Register info */
  381.   set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS);
  382.   set_gdbarch_register_name (gdbarch, vax_register_name);
  383.   set_gdbarch_register_type (gdbarch, vax_register_type);
  384.   set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM);
  385.   set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM);
  386.   set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM);

  387.   set_gdbarch_iterate_over_regset_sections
  388.     (gdbarch, vax_iterate_over_regset_sections);

  389.   /* Frame and stack info */
  390.   set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue);
  391.   set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args);
  392.   set_gdbarch_frame_args_skip (gdbarch, 4);

  393.   /* Stack grows downward.  */
  394.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  395.   /* Return value info */
  396.   set_gdbarch_return_value (gdbarch, vax_return_value);

  397.   /* Call dummy code.  */
  398.   set_gdbarch_push_dummy_call (gdbarch, vax_push_dummy_call);
  399.   set_gdbarch_dummy_id (gdbarch, vax_dummy_id);

  400.   /* Breakpoint info */
  401.   set_gdbarch_breakpoint_from_pc (gdbarch, vax_breakpoint_from_pc);

  402.   /* Misc info */
  403.   set_gdbarch_deprecated_function_start_offset (gdbarch, 2);
  404.   set_gdbarch_believe_pcc_promotion (gdbarch, 1);

  405.   set_gdbarch_print_insn (gdbarch, print_insn_vax);

  406.   set_gdbarch_unwind_pc (gdbarch, vax_unwind_pc);

  407.   frame_base_set_default (gdbarch, &vax_frame_base);

  408.   /* Hook in ABI-specific overrides, if they have been registered.  */
  409.   gdbarch_init_osabi (info, gdbarch);

  410.   frame_unwind_append_unwinder (gdbarch, &vax_frame_unwind);

  411.   return (gdbarch);
  412. }

  413. /* Provide a prototype to silence -Wmissing-prototypes.  */
  414. void _initialize_vax_tdep (void);

  415. void
  416. _initialize_vax_tdep (void)
  417. {
  418.   gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL);
  419. }