GDB (API)
/home/stan/gdb/src/gdb/vax-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for the VAX.
00002 
00003    Copyright (C) 1986-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 "arch-utils.h"
00022 #include "dis-asm.h"
00023 #include "floatformat.h"
00024 #include "frame.h"
00025 #include "frame-base.h"
00026 #include "frame-unwind.h"
00027 #include "gdbcore.h"
00028 #include "gdbtypes.h"
00029 #include "osabi.h"
00030 #include "regcache.h"
00031 #include "regset.h"
00032 #include "trad-frame.h"
00033 #include "value.h"
00034 
00035 #include "gdb_string.h"
00036 
00037 #include "vax-tdep.h"
00038 
00039 /* Return the name of register REGNUM.  */
00040 
00041 static const char *
00042 vax_register_name (struct gdbarch *gdbarch, int regnum)
00043 {
00044   static char *register_names[] =
00045   {
00046     "r0", "r1", "r2",  "r3",  "r4", "r5", "r6", "r7",
00047     "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc",
00048     "ps",
00049   };
00050 
00051   if (regnum >= 0 && regnum < ARRAY_SIZE (register_names))
00052     return register_names[regnum];
00053 
00054   return NULL;
00055 }
00056 
00057 /* Return the GDB type object for the "standard" data type of data in
00058    register REGNUM.  */
00059 
00060 static struct type *
00061 vax_register_type (struct gdbarch *gdbarch, int regnum)
00062 {
00063   return builtin_type (gdbarch)->builtin_int;
00064 }
00065 
00066 /* Core file support.  */
00067 
00068 /* Supply register REGNUM from the buffer specified by GREGS and LEN
00069    in the general-purpose register set REGSET to register cache
00070    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
00071 
00072 static void
00073 vax_supply_gregset (const struct regset *regset, struct regcache *regcache,
00074                     int regnum, const void *gregs, size_t len)
00075 {
00076   const gdb_byte *regs = gregs;
00077   int i;
00078 
00079   for (i = 0; i < VAX_NUM_REGS; i++)
00080     {
00081       if (regnum == i || regnum == -1)
00082         regcache_raw_supply (regcache, i, regs + i * 4);
00083     }
00084 }
00085 
00086 /* VAX register set.  */
00087 
00088 static struct regset vax_gregset =
00089 {
00090   NULL,
00091   vax_supply_gregset
00092 };
00093 
00094 /* Return the appropriate register set for the core section identified
00095    by SECT_NAME and SECT_SIZE.  */
00096 
00097 static const struct regset *
00098 vax_regset_from_core_section (struct gdbarch *gdbarch,
00099                               const char *sect_name, size_t sect_size)
00100 {
00101   if (strcmp (sect_name, ".reg") == 0 && sect_size >= VAX_NUM_REGS * 4)
00102     return &vax_gregset;
00103 
00104   return NULL;
00105 }
00106 
00107 /* The VAX UNIX calling convention uses R1 to pass a structure return
00108    value address instead of passing it as a first (hidden) argument as
00109    the VMS calling convention suggests.  */
00110 
00111 static CORE_ADDR
00112 vax_store_arguments (struct regcache *regcache, int nargs,
00113                      struct value **args, CORE_ADDR sp)
00114 {
00115   struct gdbarch *gdbarch = get_regcache_arch (regcache);
00116   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00117   gdb_byte buf[4];
00118   int count = 0;
00119   int i;
00120 
00121   /* We create an argument list on the stack, and make the argument
00122      pointer to it.  */
00123 
00124   /* Push arguments in reverse order.  */
00125   for (i = nargs - 1; i >= 0; i--)
00126     {
00127       int len = TYPE_LENGTH (value_enclosing_type (args[i]));
00128 
00129       sp -= (len + 3) & ~3;
00130       count += (len + 3) / 4;
00131       write_memory (sp, value_contents_all (args[i]), len);
00132     }
00133 
00134   /* Push argument count.  */
00135   sp -= 4;
00136   store_unsigned_integer (buf, 4, byte_order, count);
00137   write_memory (sp, buf, 4);
00138 
00139   /* Update the argument pointer.  */
00140   store_unsigned_integer (buf, 4, byte_order, sp);
00141   regcache_cooked_write (regcache, VAX_AP_REGNUM, buf);
00142 
00143   return sp;
00144 }
00145 
00146 static CORE_ADDR
00147 vax_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
00148                      struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
00149                      struct value **args, CORE_ADDR sp, int struct_return,
00150                      CORE_ADDR struct_addr)
00151 {
00152   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00153   CORE_ADDR fp = sp;
00154   gdb_byte buf[4];
00155 
00156   /* Set up the function arguments.  */
00157   sp = vax_store_arguments (regcache, nargs, args, sp);
00158 
00159   /* Store return value address.  */
00160   if (struct_return)
00161     regcache_cooked_write_unsigned (regcache, VAX_R1_REGNUM, struct_addr);
00162 
00163   /* Store return address in the PC slot.  */
00164   sp -= 4;
00165   store_unsigned_integer (buf, 4, byte_order, bp_addr);
00166   write_memory (sp, buf, 4);
00167 
00168   /* Store the (fake) frame pointer in the FP slot.  */
00169   sp -= 4;
00170   store_unsigned_integer (buf, 4, byte_order, fp);
00171   write_memory (sp, buf, 4);
00172 
00173   /* Skip the AP slot.  */
00174   sp -= 4;
00175 
00176   /* Store register save mask and control bits.  */
00177   sp -= 4;
00178   store_unsigned_integer (buf, 4, byte_order, 0);
00179   write_memory (sp, buf, 4);
00180 
00181   /* Store condition handler.  */
00182   sp -= 4;
00183   store_unsigned_integer (buf, 4, byte_order, 0);
00184   write_memory (sp, buf, 4);
00185 
00186   /* Update the stack pointer and frame pointer.  */
00187   store_unsigned_integer (buf, 4, byte_order, sp);
00188   regcache_cooked_write (regcache, VAX_SP_REGNUM, buf);
00189   regcache_cooked_write (regcache, VAX_FP_REGNUM, buf);
00190 
00191   /* Return the saved (fake) frame pointer.  */
00192   return fp;
00193 }
00194 
00195 static struct frame_id
00196 vax_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
00197 {
00198   CORE_ADDR fp;
00199 
00200   fp = get_frame_register_unsigned (this_frame, VAX_FP_REGNUM);
00201   return frame_id_build (fp, get_frame_pc (this_frame));
00202 }
00203 
00204 
00205 static enum return_value_convention
00206 vax_return_value (struct gdbarch *gdbarch, struct value *function,
00207                   struct type *type, struct regcache *regcache,
00208                   gdb_byte *readbuf, const gdb_byte *writebuf)
00209 {
00210   int len = TYPE_LENGTH (type);
00211   gdb_byte buf[8];
00212 
00213   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
00214       || TYPE_CODE (type) == TYPE_CODE_UNION
00215       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
00216     {
00217       /* The default on VAX is to return structures in static memory.
00218          Consequently a function must return the address where we can
00219          find the return value.  */
00220 
00221       if (readbuf)
00222         {
00223           ULONGEST addr;
00224 
00225           regcache_raw_read_unsigned (regcache, VAX_R0_REGNUM, &addr);
00226           read_memory (addr, readbuf, len);
00227         }
00228 
00229       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
00230     }
00231 
00232   if (readbuf)
00233     {
00234       /* Read the contents of R0 and (if necessary) R1.  */
00235       regcache_cooked_read (regcache, VAX_R0_REGNUM, buf);
00236       if (len > 4)
00237         regcache_cooked_read (regcache, VAX_R1_REGNUM, buf + 4);
00238       memcpy (readbuf, buf, len);
00239     }
00240   if (writebuf)
00241     {
00242       /* Read the contents to R0 and (if necessary) R1.  */
00243       memcpy (buf, writebuf, len);
00244       regcache_cooked_write (regcache, VAX_R0_REGNUM, buf);
00245       if (len > 4)
00246         regcache_cooked_write (regcache, VAX_R1_REGNUM, buf + 4);
00247     }
00248 
00249   return RETURN_VALUE_REGISTER_CONVENTION;
00250 }
00251 
00252 
00253 /* Use the program counter to determine the contents and size of a
00254    breakpoint instruction.  Return a pointer to a string of bytes that
00255    encode a breakpoint instruction, store the length of the string in
00256    *LEN and optionally adjust *PC to point to the correct memory
00257    location for inserting the breakpoint.  */
00258    
00259 static const gdb_byte *
00260 vax_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
00261 {
00262   static gdb_byte break_insn[] = { 3 };
00263 
00264   *len = sizeof (break_insn);
00265   return break_insn;
00266 }
00267 
00268 /* Advance PC across any function entry prologue instructions
00269    to reach some "real" code.  */
00270 
00271 static CORE_ADDR
00272 vax_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
00273 {
00274   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00275   gdb_byte op = read_memory_unsigned_integer (pc, 1, byte_order);
00276 
00277   if (op == 0x11)
00278     pc += 2;                    /* skip brb */
00279   if (op == 0x31)
00280     pc += 3;                    /* skip brw */
00281   if (op == 0xC2
00282       && read_memory_unsigned_integer (pc + 2, 1, byte_order) == 0x5E)
00283     pc += 3;                    /* skip subl2 */
00284   if (op == 0x9E
00285       && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xAE
00286       && read_memory_unsigned_integer (pc + 3, 1, byte_order) == 0x5E)
00287     pc += 4;                    /* skip movab */
00288   if (op == 0x9E
00289       && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xCE
00290       && read_memory_unsigned_integer (pc + 4, 1, byte_order) == 0x5E)
00291     pc += 5;                    /* skip movab */
00292   if (op == 0x9E
00293       && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xEE
00294       && read_memory_unsigned_integer (pc + 6, 1, byte_order) == 0x5E)
00295     pc += 7;                    /* skip movab */
00296 
00297   return pc;
00298 }
00299 
00300 
00301 /* Unwinding the stack is relatively easy since the VAX has a
00302    dedicated frame pointer, and frames are set up automatically as the
00303    result of a function call.  Most of the relevant information can be
00304    inferred from the documentation of the Procedure Call Instructions
00305    in the VAX MACRO and Instruction Set Reference Manual.  */
00306 
00307 struct vax_frame_cache
00308 {
00309   /* Base address.  */
00310   CORE_ADDR base;
00311 
00312   /* Table of saved registers.  */
00313   struct trad_frame_saved_reg *saved_regs;
00314 };
00315 
00316 static struct vax_frame_cache *
00317 vax_frame_cache (struct frame_info *this_frame, void **this_cache)
00318 {
00319   struct vax_frame_cache *cache;
00320   CORE_ADDR addr;
00321   ULONGEST mask;
00322   int regnum;
00323 
00324   if (*this_cache)
00325     return *this_cache;
00326 
00327   /* Allocate a new cache.  */
00328   cache = FRAME_OBSTACK_ZALLOC (struct vax_frame_cache);
00329   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
00330 
00331   /* The frame pointer is used as the base for the frame.  */
00332   cache->base = get_frame_register_unsigned (this_frame, VAX_FP_REGNUM);
00333   if (cache->base == 0)
00334     return cache;
00335 
00336   /* The register save mask and control bits determine the layout of
00337      the stack frame.  */
00338   mask = get_frame_memory_unsigned (this_frame, cache->base + 4, 4) >> 16;
00339 
00340   /* These are always saved.  */
00341   cache->saved_regs[VAX_PC_REGNUM].addr = cache->base + 16;
00342   cache->saved_regs[VAX_FP_REGNUM].addr = cache->base + 12;
00343   cache->saved_regs[VAX_AP_REGNUM].addr = cache->base + 8;
00344   cache->saved_regs[VAX_PS_REGNUM].addr = cache->base + 4;
00345 
00346   /* Scan the register save mask and record the location of the saved
00347      registers.  */
00348   addr = cache->base + 20;
00349   for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++)
00350     {
00351       if (mask & (1 << regnum))
00352         {
00353           cache->saved_regs[regnum].addr = addr;
00354           addr += 4;
00355         }
00356     }
00357 
00358   /* The CALLS/CALLG flag determines whether this frame has a General
00359      Argument List or a Stack Argument List.  */
00360   if (mask & (1 << 13))
00361     {
00362       ULONGEST numarg;
00363 
00364       /* This is a procedure with Stack Argument List.  Adjust the
00365          stack address for the arguments that were pushed onto the
00366          stack.  The return instruction will automatically pop the
00367          arguments from the stack.  */
00368       numarg = get_frame_memory_unsigned (this_frame, addr, 1);
00369       addr += 4 + numarg * 4;
00370     }
00371 
00372   /* Bits 1:0 of the stack pointer were saved in the control bits.  */
00373   trad_frame_set_value (cache->saved_regs, VAX_SP_REGNUM, addr + (mask >> 14));
00374 
00375   return cache;
00376 }
00377 
00378 static void
00379 vax_frame_this_id (struct frame_info *this_frame, void **this_cache,
00380                    struct frame_id *this_id)
00381 {
00382   struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
00383 
00384   /* This marks the outermost frame.  */
00385   if (cache->base == 0)
00386     return;
00387 
00388   (*this_id) = frame_id_build (cache->base, get_frame_func (this_frame));
00389 }
00390 
00391 static struct value *
00392 vax_frame_prev_register (struct frame_info *this_frame,
00393                          void **this_cache, int regnum)
00394 {
00395   struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
00396 
00397   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
00398 }
00399 
00400 static const struct frame_unwind vax_frame_unwind =
00401 {
00402   NORMAL_FRAME,
00403   default_frame_unwind_stop_reason,
00404   vax_frame_this_id,
00405   vax_frame_prev_register,
00406   NULL,
00407   default_frame_sniffer
00408 };
00409 
00410 
00411 static CORE_ADDR
00412 vax_frame_base_address (struct frame_info *this_frame, void **this_cache)
00413 {
00414   struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
00415 
00416   return cache->base;
00417 }
00418 
00419 static CORE_ADDR
00420 vax_frame_args_address (struct frame_info *this_frame, void **this_cache)
00421 {
00422   return get_frame_register_unsigned (this_frame, VAX_AP_REGNUM);
00423 }
00424 
00425 static const struct frame_base vax_frame_base =
00426 {
00427   &vax_frame_unwind,
00428   vax_frame_base_address,
00429   vax_frame_base_address,
00430   vax_frame_args_address
00431 };
00432 
00433 /* Return number of arguments for FRAME.  */
00434 
00435 static int
00436 vax_frame_num_args (struct frame_info *frame)
00437 {
00438   CORE_ADDR args;
00439 
00440   /* Assume that the argument pointer for the outermost frame is
00441      hosed, as is the case on NetBSD/vax ELF.  */
00442   if (get_frame_base_address (frame) == 0)
00443     return 0;
00444 
00445   args = get_frame_register_unsigned (frame, VAX_AP_REGNUM);
00446   return get_frame_memory_unsigned (frame, args, 1);
00447 }
00448 
00449 static CORE_ADDR
00450 vax_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
00451 {
00452   return frame_unwind_register_unsigned (next_frame, VAX_PC_REGNUM);
00453 }
00454 
00455 
00456 /* Initialize the current architecture based on INFO.  If possible, re-use an
00457    architecture from ARCHES, which is a list of architectures already created
00458    during this debugging session.
00459 
00460    Called e.g. at program startup, when reading a core file, and when reading
00461    a binary file.  */
00462 
00463 static struct gdbarch *
00464 vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
00465 {
00466   struct gdbarch *gdbarch;
00467 
00468   /* If there is already a candidate, use it.  */
00469   arches = gdbarch_list_lookup_by_info (arches, &info);
00470   if (arches != NULL)
00471     return arches->gdbarch;
00472 
00473   gdbarch = gdbarch_alloc (&info, NULL);
00474 
00475   set_gdbarch_float_format (gdbarch, floatformats_vax_f);
00476   set_gdbarch_double_format (gdbarch, floatformats_vax_d);
00477   set_gdbarch_long_double_format (gdbarch, floatformats_vax_d);
00478   set_gdbarch_long_double_bit (gdbarch, 64);
00479 
00480   /* Register info */
00481   set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS);
00482   set_gdbarch_register_name (gdbarch, vax_register_name);
00483   set_gdbarch_register_type (gdbarch, vax_register_type);
00484   set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM);
00485   set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM);
00486   set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM);
00487 
00488   set_gdbarch_regset_from_core_section
00489     (gdbarch, vax_regset_from_core_section);
00490 
00491   /* Frame and stack info */
00492   set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue);
00493   set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args);
00494   set_gdbarch_frame_args_skip (gdbarch, 4);
00495 
00496   /* Stack grows downward.  */
00497   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
00498 
00499   /* Return value info */
00500   set_gdbarch_return_value (gdbarch, vax_return_value);
00501 
00502   /* Call dummy code.  */
00503   set_gdbarch_push_dummy_call (gdbarch, vax_push_dummy_call);
00504   set_gdbarch_dummy_id (gdbarch, vax_dummy_id);
00505 
00506   /* Breakpoint info */
00507   set_gdbarch_breakpoint_from_pc (gdbarch, vax_breakpoint_from_pc);
00508 
00509   /* Misc info */
00510   set_gdbarch_deprecated_function_start_offset (gdbarch, 2);
00511   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
00512 
00513   set_gdbarch_print_insn (gdbarch, print_insn_vax);
00514 
00515   set_gdbarch_unwind_pc (gdbarch, vax_unwind_pc);
00516 
00517   frame_base_set_default (gdbarch, &vax_frame_base);
00518 
00519   /* Hook in ABI-specific overrides, if they have been registered.  */
00520   gdbarch_init_osabi (info, gdbarch);
00521 
00522   frame_unwind_append_unwinder (gdbarch, &vax_frame_unwind);
00523 
00524   return (gdbarch);
00525 }
00526 
00527 /* Provide a prototype to silence -Wmissing-prototypes.  */
00528 void _initialize_vax_tdep (void);
00529 
00530 void
00531 _initialize_vax_tdep (void)
00532 {
00533   gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL);
00534 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines