gdb/sparc-tdep.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for SPARC.

  2.    Copyright (C) 2003-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 "dwarf2-frame.h"
  18. #include "floatformat.h"
  19. #include "frame.h"
  20. #include "frame-base.h"
  21. #include "frame-unwind.h"
  22. #include "gdbcore.h"
  23. #include "gdbtypes.h"
  24. #include "inferior.h"
  25. #include "symtab.h"
  26. #include "objfiles.h"
  27. #include "osabi.h"
  28. #include "regcache.h"
  29. #include "target.h"
  30. #include "value.h"

  31. #include "sparc-tdep.h"
  32. #include "sparc-ravenscar-thread.h"

  33. struct regset;

  34. /* This file implements the SPARC 32-bit ABI as defined by the section
  35.    "Low-Level System Information" of the SPARC Compliance Definition
  36.    (SCD) 2.4.1, which is the 32-bit System V psABI for SPARC.  The SCD
  37.    lists changes with respect to the original 32-bit psABI as defined
  38.    in the "System V ABI, SPARC Processor Supplement".

  39.    Note that if we talk about SunOS, we mean SunOS 4.x, which was
  40.    BSD-based, which is sometimes (retroactively?) referred to as
  41.    Solaris 1.x.  If we talk about Solaris we mean Solaris 2.x and
  42.    above (Solaris 7, 8 and 9 are nothing but Solaris 2.7, 2.8 and 2.9
  43.    suffering from severe version number inflation).  Solaris 2.x is
  44.    also known as SunOS 5.x, since that's what uname(1) says.  Solaris
  45.    2.x is SVR4-based.  */

  46. /* Please use the sparc32_-prefix for 32-bit specific code, the
  47.    sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
  48.    code that can handle both.  The 64-bit specific code lives in
  49.    sparc64-tdep.c; don't add any here.  */

  50. /* The SPARC Floating-Point Quad-Precision format is similar to
  51.    big-endian IA-64 Quad-Precision format.  */
  52. #define floatformats_sparc_quad floatformats_ia64_quad

  53. /* The stack pointer is offset from the stack frame by a BIAS of 2047
  54.    (0x7ff) for 64-bit codeBIAS is likely to be defined on SPARC
  55.    hosts, so undefine it first.  */
  56. #undef BIAS
  57. #define BIAS 2047

  58. /* Macros to extract fields from SPARC instructions.  */
  59. #define X_OP(i) (((i) >> 30) & 0x3)
  60. #define X_RD(i) (((i) >> 25) & 0x1f)
  61. #define X_A(i) (((i) >> 29) & 1)
  62. #define X_COND(i) (((i) >> 25) & 0xf)
  63. #define X_OP2(i) (((i) >> 22) & 0x7)
  64. #define X_IMM22(i) ((i) & 0x3fffff)
  65. #define X_OP3(i) (((i) >> 19) & 0x3f)
  66. #define X_RS1(i) (((i) >> 14) & 0x1f)
  67. #define X_RS2(i) ((i) & 0x1f)
  68. #define X_I(i) (((i) >> 13) & 1)
  69. /* Sign extension macros.  */
  70. #define X_DISP22(i) ((X_IMM22 (i) ^ 0x200000) - 0x200000)
  71. #define X_DISP19(i) ((((i) & 0x7ffff) ^ 0x40000) - 0x40000)
  72. #define X_DISP10(i) ((((((i) >> 11) && 0x300) | (((i) >> 5) & 0xff)) ^ 0x200) - 0x200)
  73. #define X_SIMM13(i) ((((i) & 0x1fff) ^ 0x1000) - 0x1000)
  74. /* Macros to identify some instructions.  */
  75. /* RETURN (RETT in V8) */
  76. #define X_RETTURN(i) ((X_OP (i) == 0x2) && (X_OP3 (i) == 0x39))

  77. /* Fetch the instruction at PC.  Instructions are always big-endian
  78.    even if the processor operates in little-endian mode.  */

  79. unsigned long
  80. sparc_fetch_instruction (CORE_ADDR pc)
  81. {
  82.   gdb_byte buf[4];
  83.   unsigned long insn;
  84.   int i;

  85.   /* If we can't read the instruction at PC, return zero.  */
  86.   if (target_read_memory (pc, buf, sizeof (buf)))
  87.     return 0;

  88.   insn = 0;
  89.   for (i = 0; i < sizeof (buf); i++)
  90.     insn = (insn << 8) | buf[i];
  91.   return insn;
  92. }


  93. /* Return non-zero if the instruction corresponding to PC is an "unimp"
  94.    instruction.  */

  95. static int
  96. sparc_is_unimp_insn (CORE_ADDR pc)
  97. {
  98.   const unsigned long insn = sparc_fetch_instruction (pc);

  99.   return ((insn & 0xc1c00000) == 0);
  100. }

  101. /* Return non-zero if the instruction corresponding to PC is an
  102.    "annulled" branch, i.e. the annul bit is set.  */

  103. int
  104. sparc_is_annulled_branch_insn (CORE_ADDR pc)
  105. {
  106.   /* The branch instructions featuring an annul bit can be identified
  107.      by the following bit patterns:

  108.      OP=0
  109.       OP2=1: Branch on Integer Condition Codes with Prediction (BPcc).
  110.       OP2=2: Branch on Integer Condition Codes (Bcc).
  111.       OP2=5: Branch on FP Condition Codes with Prediction (FBfcc).
  112.       OP2=6: Branch on FP Condition Codes (FBcc).
  113.       OP2=3 && Bit28=0:
  114.              Branch on Integer Register with Prediction (BPr).

  115.      This leaves out ILLTRAP (OP2=0), SETHI/NOP (OP2=4) and the V8
  116.      coprocessor branch instructions (Op2=7).  */

  117.   const unsigned long insn = sparc_fetch_instruction (pc);
  118.   const unsigned op2 = X_OP2 (insn);

  119.   if ((X_OP (insn) == 0)
  120.       && ((op2 == 1) || (op2 == 2) || (op2 == 5) || (op2 == 6)
  121.           || ((op2 == 3) && ((insn & 0x10000000) == 0))))
  122.     return X_A (insn);
  123.   else
  124.     return 0;
  125. }

  126. /* OpenBSD/sparc includes StackGhost, which according to the author's
  127.    website http://stackghost.cerias.purdue.edu "... transparently and
  128.    automatically protects applications' stack frames; more
  129.    specifically, it guards the return pointers.  The protection
  130.    mechanisms require no application source or binary modification and
  131.    imposes only a negligible performance penalty."

  132.    The same website provides the following description of how
  133.    StackGhost works:

  134.    "StackGhost interfaces with the kernel trap handler that would
  135.    normally write out registers to the stack and the handler that
  136.    would read them back in.  By XORing a cookie into the
  137.    return-address saved in the user stack when it is actually written
  138.    to the stack, and then XOR it out when the return-address is pulled
  139.    from the stack, StackGhost can cause attacker corrupted return
  140.    pointers to behave in a manner the attacker cannot predict.
  141.    StackGhost can also use several unused bits in the return pointer
  142.    to detect a smashed return pointer and abort the process."

  143.    For GDB this means that whenever we're reading %i7 from a stack
  144.    frame's window save area, we'll have to XOR the cookie.

  145.    More information on StackGuard can be found on in:

  146.    Mike Frantzen and Mike Shuey.  "StackGhost: Hardware Facilitated
  147.    Stack Protection."  2001.  Published in USENIX Security Symposium
  148.    '01.  */

  149. /* Fetch StackGhost Per-Process XOR cookie.  */

  150. ULONGEST
  151. sparc_fetch_wcookie (struct gdbarch *gdbarch)
  152. {
  153.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  154.   struct target_ops *ops = &current_target;
  155.   gdb_byte buf[8];
  156.   int len;

  157.   len = target_read (ops, TARGET_OBJECT_WCOOKIE, NULL, buf, 0, 8);
  158.   if (len == -1)
  159.     return 0;

  160.   /* We should have either an 32-bit or an 64-bit cookie.  */
  161.   gdb_assert (len == 4 || len == 8);

  162.   return extract_unsigned_integer (buf, len, byte_order);
  163. }


  164. /* The functions on this page are intended to be used to classify
  165.    function arguments.  */

  166. /* Check whether TYPE is "Integral or Pointer".  */

  167. static int
  168. sparc_integral_or_pointer_p (const struct type *type)
  169. {
  170.   int len = TYPE_LENGTH (type);

  171.   switch (TYPE_CODE (type))
  172.     {
  173.     case TYPE_CODE_INT:
  174.     case TYPE_CODE_BOOL:
  175.     case TYPE_CODE_CHAR:
  176.     case TYPE_CODE_ENUM:
  177.     case TYPE_CODE_RANGE:
  178.       /* We have byte, half-word, word and extended-word/doubleword
  179.          integral types.  The doubleword is an extension to the
  180.          original 32-bit ABI by the SCD 2.4.x.  */
  181.       return (len == 1 || len == 2 || len == 4 || len == 8);
  182.     case TYPE_CODE_PTR:
  183.     case TYPE_CODE_REF:
  184.       /* Allow either 32-bit or 64-bit pointers.  */
  185.       return (len == 4 || len == 8);
  186.     default:
  187.       break;
  188.     }

  189.   return 0;
  190. }

  191. /* Check whether TYPE is "Floating".  */

  192. static int
  193. sparc_floating_p (const struct type *type)
  194. {
  195.   switch (TYPE_CODE (type))
  196.     {
  197.     case TYPE_CODE_FLT:
  198.       {
  199.         int len = TYPE_LENGTH (type);
  200.         return (len == 4 || len == 8 || len == 16);
  201.       }
  202.     default:
  203.       break;
  204.     }

  205.   return 0;
  206. }

  207. /* Check whether TYPE is "Complex Floating".  */

  208. static int
  209. sparc_complex_floating_p (const struct type *type)
  210. {
  211.   switch (TYPE_CODE (type))
  212.     {
  213.     case TYPE_CODE_COMPLEX:
  214.       {
  215.         int len = TYPE_LENGTH (type);
  216.         return (len == 8 || len == 16 || len == 32);
  217.       }
  218.     default:
  219.       break;
  220.     }

  221.   return 0;
  222. }

  223. /* Check whether TYPE is "Structure or Union".

  224.    In terms of Ada subprogram calls, arrays are treated the same as
  225.    struct and union types.  So this function also returns non-zero
  226.    for array types.  */

  227. static int
  228. sparc_structure_or_union_p (const struct type *type)
  229. {
  230.   switch (TYPE_CODE (type))
  231.     {
  232.     case TYPE_CODE_STRUCT:
  233.     case TYPE_CODE_UNION:
  234.     case TYPE_CODE_ARRAY:
  235.       return 1;
  236.     default:
  237.       break;
  238.     }

  239.   return 0;
  240. }

  241. /* Register information.  */

  242. static const char *sparc32_register_names[] =
  243. {
  244.   "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
  245.   "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
  246.   "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
  247.   "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",

  248.   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
  249.   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
  250.   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  251.   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",

  252.   "y", "psr", "wim", "tbr", "pc", "npc", "fsr", "csr"
  253. };

  254. /* Total number of registers.  */
  255. #define SPARC32_NUM_REGS ARRAY_SIZE (sparc32_register_names)

  256. /* We provide the aliases %d0..%d30 for the floating registers as
  257.    "psuedo" registers.  */

  258. static const char *sparc32_pseudo_register_names[] =
  259. {
  260.   "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
  261.   "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30"
  262. };

  263. /* Total number of pseudo registers.  */
  264. #define SPARC32_NUM_PSEUDO_REGS ARRAY_SIZE (sparc32_pseudo_register_names)

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

  266. static const char *
  267. sparc32_register_name (struct gdbarch *gdbarch, int regnum)
  268. {
  269.   if (regnum >= 0 && regnum < SPARC32_NUM_REGS)
  270.     return sparc32_register_names[regnum];

  271.   if (regnum < SPARC32_NUM_REGS + SPARC32_NUM_PSEUDO_REGS)
  272.     return sparc32_pseudo_register_names[regnum - SPARC32_NUM_REGS];

  273.   return NULL;
  274. }

  275. /* Construct types for ISA-specific registers.  */

  276. static struct type *
  277. sparc_psr_type (struct gdbarch *gdbarch)
  278. {
  279.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  280.   if (!tdep->sparc_psr_type)
  281.     {
  282.       struct type *type;

  283.       type = arch_flags_type (gdbarch, "builtin_type_sparc_psr", 4);
  284.       append_flags_type_flag (type, 5, "ET");
  285.       append_flags_type_flag (type, 6, "PS");
  286.       append_flags_type_flag (type, 7, "S");
  287.       append_flags_type_flag (type, 12, "EF");
  288.       append_flags_type_flag (type, 13, "EC");

  289.       tdep->sparc_psr_type = type;
  290.     }

  291.   return tdep->sparc_psr_type;
  292. }

  293. static struct type *
  294. sparc_fsr_type (struct gdbarch *gdbarch)
  295. {
  296.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  297.   if (!tdep->sparc_fsr_type)
  298.     {
  299.       struct type *type;

  300.       type = arch_flags_type (gdbarch, "builtin_type_sparc_fsr", 4);
  301.       append_flags_type_flag (type, 0, "NXA");
  302.       append_flags_type_flag (type, 1, "DZA");
  303.       append_flags_type_flag (type, 2, "UFA");
  304.       append_flags_type_flag (type, 3, "OFA");
  305.       append_flags_type_flag (type, 4, "NVA");
  306.       append_flags_type_flag (type, 5, "NXC");
  307.       append_flags_type_flag (type, 6, "DZC");
  308.       append_flags_type_flag (type, 7, "UFC");
  309.       append_flags_type_flag (type, 8, "OFC");
  310.       append_flags_type_flag (type, 9, "NVC");
  311.       append_flags_type_flag (type, 22, "NS");
  312.       append_flags_type_flag (type, 23, "NXM");
  313.       append_flags_type_flag (type, 24, "DZM");
  314.       append_flags_type_flag (type, 25, "UFM");
  315.       append_flags_type_flag (type, 26, "OFM");
  316.       append_flags_type_flag (type, 27, "NVM");

  317.       tdep->sparc_fsr_type = type;
  318.     }

  319.   return tdep->sparc_fsr_type;
  320. }

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

  323. static struct type *
  324. sparc32_register_type (struct gdbarch *gdbarch, int regnum)
  325. {
  326.   if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
  327.     return builtin_type (gdbarch)->builtin_float;

  328.   if (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM)
  329.     return builtin_type (gdbarch)->builtin_double;

  330.   if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
  331.     return builtin_type (gdbarch)->builtin_data_ptr;

  332.   if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
  333.     return builtin_type (gdbarch)->builtin_func_ptr;

  334.   if (regnum == SPARC32_PSR_REGNUM)
  335.     return sparc_psr_type (gdbarch);

  336.   if (regnum == SPARC32_FSR_REGNUM)
  337.     return sparc_fsr_type (gdbarch);

  338.   return builtin_type (gdbarch)->builtin_int32;
  339. }

  340. static enum register_status
  341. sparc32_pseudo_register_read (struct gdbarch *gdbarch,
  342.                               struct regcache *regcache,
  343.                               int regnum, gdb_byte *buf)
  344. {
  345.   enum register_status status;

  346.   gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);

  347.   regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
  348.   status = regcache_raw_read (regcache, regnum, buf);
  349.   if (status == REG_VALID)
  350.     status = regcache_raw_read (regcache, regnum + 1, buf + 4);
  351.   return status;
  352. }

  353. static void
  354. sparc32_pseudo_register_write (struct gdbarch *gdbarch,
  355.                                struct regcache *regcache,
  356.                                int regnum, const gdb_byte *buf)
  357. {
  358.   gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);

  359.   regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
  360.   regcache_raw_write (regcache, regnum, buf);
  361.   regcache_raw_write (regcache, regnum + 1, buf + 4);
  362. }

  363. /* Implement "in_function_epilogue_p".  */

  364. int
  365. sparc_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  366. {
  367.   /* This function must return true if we are one instruction after an
  368.      instruction that destroyed the stack frame of the current
  369.      function.  The SPARC instructions used to restore the callers
  370.      stack frame are RESTORE and RETURN/RETT.

  371.      Of these RETURN/RETT is a branch instruction and thus we return
  372.      true if we are in its delay slot.

  373.      RESTORE is almost always found in the delay slot of a branch
  374.      instruction that transfers control to the caller, such as JMPL.
  375.      Thus the next instruction is in the caller frame and we don't
  376.      need to do anything about it.  */

  377.   unsigned int insn = sparc_fetch_instruction (pc - 4);

  378.   return X_RETTURN (insn);
  379. }


  380. static CORE_ADDR
  381. sparc32_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
  382. {
  383.   /* The ABI requires double-word alignment.  */
  384.   return address & ~0x7;
  385. }

  386. static CORE_ADDR
  387. sparc32_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
  388.                          CORE_ADDR funcaddr,
  389.                          struct value **args, int nargs,
  390.                          struct type *value_type,
  391.                          CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
  392.                          struct regcache *regcache)
  393. {
  394.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  395.   *bp_addr = sp - 4;
  396.   *real_pc = funcaddr;

  397.   if (using_struct_return (gdbarch, NULL, value_type))
  398.     {
  399.       gdb_byte buf[4];

  400.       /* This is an UNIMP instruction.  */
  401.       store_unsigned_integer (buf, 4, byte_order,
  402.                               TYPE_LENGTH (value_type) & 0x1fff);
  403.       write_memory (sp - 8, buf, 4);
  404.       return sp - 8;
  405.     }

  406.   return sp - 4;
  407. }

  408. static CORE_ADDR
  409. sparc32_store_arguments (struct regcache *regcache, int nargs,
  410.                          struct value **args, CORE_ADDR sp,
  411.                          int struct_return, CORE_ADDR struct_addr)
  412. {
  413.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  414.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  415.   /* Number of words in the "parameter array".  */
  416.   int num_elements = 0;
  417.   int element = 0;
  418.   int i;

  419.   for (i = 0; i < nargs; i++)
  420.     {
  421.       struct type *type = value_type (args[i]);
  422.       int len = TYPE_LENGTH (type);

  423.       if (sparc_structure_or_union_p (type)
  424.           || (sparc_floating_p (type) && len == 16)
  425.           || sparc_complex_floating_p (type))
  426.         {
  427.           /* Structure, Union and Quad-Precision Arguments.  */
  428.           sp -= len;

  429.           /* Use doubleword alignment for these values.  That's always
  430.              correct, and wasting a few bytes shouldn't be a problem.  */
  431.           sp &= ~0x7;

  432.           write_memory (sp, value_contents (args[i]), len);
  433.           args[i] = value_from_pointer (lookup_pointer_type (type), sp);
  434.           num_elements++;
  435.         }
  436.       else if (sparc_floating_p (type))
  437.         {
  438.           /* Floating arguments.  */
  439.           gdb_assert (len == 4 || len == 8);
  440.           num_elements += (len / 4);
  441.         }
  442.       else
  443.         {
  444.           /* Integral and pointer arguments.  */
  445.           gdb_assert (sparc_integral_or_pointer_p (type));

  446.           if (len < 4)
  447.             args[i] = value_cast (builtin_type (gdbarch)->builtin_int32,
  448.                                   args[i]);
  449.           num_elements += ((len + 3) / 4);
  450.         }
  451.     }

  452.   /* Always allocate at least six words.  */
  453.   sp -= max (6, num_elements) * 4;

  454.   /* The psABI says that "Software convention requires space for the
  455.      struct/union return value pointer, even if the word is unused."  */
  456.   sp -= 4;

  457.   /* The psABI says that "Although software convention and the
  458.      operating system require every stack frame to be doubleword
  459.      aligned."  */
  460.   sp &= ~0x7;

  461.   for (i = 0; i < nargs; i++)
  462.     {
  463.       const bfd_byte *valbuf = value_contents (args[i]);
  464.       struct type *type = value_type (args[i]);
  465.       int len = TYPE_LENGTH (type);

  466.       gdb_assert (len == 4 || len == 8);

  467.       if (element < 6)
  468.         {
  469.           int regnum = SPARC_O0_REGNUM + element;

  470.           regcache_cooked_write (regcache, regnum, valbuf);
  471.           if (len > 4 && element < 5)
  472.             regcache_cooked_write (regcache, regnum + 1, valbuf + 4);
  473.         }

  474.       /* Always store the argument in memory.  */
  475.       write_memory (sp + 4 + element * 4, valbuf, len);
  476.       element += len / 4;
  477.     }

  478.   gdb_assert (element == num_elements);

  479.   if (struct_return)
  480.     {
  481.       gdb_byte buf[4];

  482.       store_unsigned_integer (buf, 4, byte_order, struct_addr);
  483.       write_memory (sp, buf, 4);
  484.     }

  485.   return sp;
  486. }

  487. static CORE_ADDR
  488. sparc32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  489.                          struct regcache *regcache, CORE_ADDR bp_addr,
  490.                          int nargs, struct value **args, CORE_ADDR sp,
  491.                          int struct_return, CORE_ADDR struct_addr)
  492. {
  493.   CORE_ADDR call_pc = (struct_return ? (bp_addr - 12) : (bp_addr - 8));

  494.   /* Set return address.  */
  495.   regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, call_pc);

  496.   /* Set up function arguments.  */
  497.   sp = sparc32_store_arguments (regcache, nargs, args, sp,
  498.                                 struct_return, struct_addr);

  499.   /* Allocate the 16-word window save area.  */
  500.   sp -= 16 * 4;

  501.   /* Stack should be doubleword aligned at this point.  */
  502.   gdb_assert (sp % 8 == 0);

  503.   /* Finally, update the stack pointer.  */
  504.   regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);

  505.   return sp;
  506. }


  507. /* Use the program counter to determine the contents and size of a
  508.    breakpoint instruction.  Return a pointer to a string of bytes that
  509.    encode a breakpoint instruction, store the length of the string in
  510.    *LEN and optionally adjust *PC to point to the correct memory
  511.    location for inserting the breakpoint.  */

  512. static const gdb_byte *
  513. sparc_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
  514. {
  515.   static const gdb_byte break_insn[] = { 0x91, 0xd0, 0x20, 0x01 };

  516.   *len = sizeof (break_insn);
  517.   return break_insn;
  518. }


  519. /* Allocate and initialize a frame cache.  */

  520. static struct sparc_frame_cache *
  521. sparc_alloc_frame_cache (void)
  522. {
  523.   struct sparc_frame_cache *cache;

  524.   cache = FRAME_OBSTACK_ZALLOC (struct sparc_frame_cache);

  525.   /* Base address.  */
  526.   cache->base = 0;
  527.   cache->pc = 0;

  528.   /* Frameless until proven otherwise.  */
  529.   cache->frameless_p = 1;
  530.   cache->frame_offset = 0;
  531.   cache->saved_regs_mask = 0;
  532.   cache->copied_regs_mask = 0;
  533.   cache->struct_return_p = 0;

  534.   return cache;
  535. }

  536. /* GCC generates several well-known sequences of instructions at the begining
  537.    of each function prologue when compiling with -fstack-check.  If one of
  538.    such sequences starts at START_PC, then return the address of the
  539.    instruction immediately past this sequence.  Otherwise, return START_PC.  */

  540. static CORE_ADDR
  541. sparc_skip_stack_check (const CORE_ADDR start_pc)
  542. {
  543.   CORE_ADDR pc = start_pc;
  544.   unsigned long insn;
  545.   int offset_stack_checking_sequence = 0;
  546.   int probing_loop = 0;

  547.   /* With GCC, all stack checking sequences begin with the same two
  548.      instructions, plus an optional one in the case of a probing loop:

  549.          sethi <some immediate>, %g1
  550.          sub %sp, %g1, %g1

  551.      or:

  552.          sethi <some immediate>, %g1
  553.          sethi <some immediate>, %g4
  554.          sub %sp, %g1, %g1

  555.      or:

  556.          sethi <some immediate>, %g1
  557.          sub %sp, %g1, %g1
  558.          sethi <some immediate>, %g4

  559.      If the optional instruction is found (setting g4), assume that a
  560.      probing loop will follow.  */

  561.   /* sethi <some immediate>, %g1 */
  562.   insn = sparc_fetch_instruction (pc);
  563.   pc = pc + 4;
  564.   if (!(X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 1))
  565.     return start_pc;

  566.   /* optional: sethi <some immediate>, %g4 */
  567.   insn = sparc_fetch_instruction (pc);
  568.   pc = pc + 4;
  569.   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
  570.     {
  571.       probing_loop = 1;
  572.       insn = sparc_fetch_instruction (pc);
  573.       pc = pc + 4;
  574.     }

  575.   /* sub %sp, %g1, %g1 */
  576.   if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
  577.         && X_RD (insn) == 1 && X_RS1 (insn) == 14 && X_RS2 (insn) == 1))
  578.     return start_pc;

  579.   insn = sparc_fetch_instruction (pc);
  580.   pc = pc + 4;

  581.   /* optional: sethi <some immediate>, %g4 */
  582.   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
  583.     {
  584.       probing_loop = 1;
  585.       insn = sparc_fetch_instruction (pc);
  586.       pc = pc + 4;
  587.     }

  588.   /* First possible sequence:
  589.          [first two instructions above]
  590.          clr [%g1 - some immediate]  */

  591.   /* clr [%g1 - some immediate]  */
  592.   if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
  593.       && X_RS1 (insn) == 1 && X_RD (insn) == 0)
  594.     {
  595.       /* Valid stack-check sequence, return the new PC.  */
  596.       return pc;
  597.     }

  598.   /* Second possible sequence: A small number of probes.
  599.          [first two instructions above]
  600.          clr [%g1]
  601.          add   %g1, -<some immediate>, %g1
  602.          clr [%g1]
  603.          [repeat the two instructions above any (small) number of times]
  604.          clr [%g1 - some immediate]  */

  605.   /* clr [%g1] */
  606.   else if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
  607.       && X_RS1 (insn) == 1 && X_RD (insn) == 0)
  608.     {
  609.       while (1)
  610.         {
  611.           /* add %g1, -<some immediate>, %g1 */
  612.           insn = sparc_fetch_instruction (pc);
  613.           pc = pc + 4;
  614.           if (!(X_OP (insn) == 2  && X_OP3(insn) == 0 && X_I(insn)
  615.                 && X_RS1 (insn) == 1 && X_RD (insn) == 1))
  616.             break;

  617.           /* clr [%g1] */
  618.           insn = sparc_fetch_instruction (pc);
  619.           pc = pc + 4;
  620.           if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
  621.                 && X_RD (insn) == 0 && X_RS1 (insn) == 1))
  622.             return start_pc;
  623.         }

  624.       /* clr [%g1 - some immediate] */
  625.       if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
  626.             && X_RS1 (insn) == 1 && X_RD (insn) == 0))
  627.         return start_pc;

  628.       /* We found a valid stack-check sequence, return the new PC.  */
  629.       return pc;
  630.     }

  631.   /* Third sequence: A probing loop.
  632.          [first three instructions above]
  633.          sub  %g1, %g4, %g4
  634.          cmp  %g1, %g4
  635.          be  <disp>
  636.          add  %g1, -<some immediate>, %g1
  637.          ba  <disp>
  638.          clr  [%g1]

  639.      And an optional last probe for the remainder:

  640.          clr [%g4 - some immediate]  */

  641.   if (probing_loop)
  642.     {
  643.       /* sub  %g1, %g4, %g4 */
  644.       if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
  645.             && X_RD (insn) == 4 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
  646.         return start_pc;

  647.       /* cmp  %g1, %g4 */
  648.       insn = sparc_fetch_instruction (pc);
  649.       pc = pc + 4;
  650.       if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x14 && !X_I(insn)
  651.             && X_RD (insn) == 0 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
  652.         return start_pc;

  653.       /* be  <disp> */
  654.       insn = sparc_fetch_instruction (pc);
  655.       pc = pc + 4;
  656.       if (!(X_OP (insn) == 0 && X_COND (insn) == 0x1))
  657.         return start_pc;

  658.       /* add  %g1, -<some immediate>, %g1 */
  659.       insn = sparc_fetch_instruction (pc);
  660.       pc = pc + 4;
  661.       if (!(X_OP (insn) == 2  && X_OP3(insn) == 0 && X_I(insn)
  662.             && X_RS1 (insn) == 1 && X_RD (insn) == 1))
  663.         return start_pc;

  664.       /* ba  <disp> */
  665.       insn = sparc_fetch_instruction (pc);
  666.       pc = pc + 4;
  667.       if (!(X_OP (insn) == 0 && X_COND (insn) == 0x8))
  668.         return start_pc;

  669.       /* clr  [%g1] (st %g0, [%g1] or st %g0, [%g1+0]) */
  670.       insn = sparc_fetch_instruction (pc);
  671.       pc = pc + 4;
  672.       if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4
  673.             && X_RD (insn) == 0 && X_RS1 (insn) == 1
  674.             && (!X_I(insn) || X_SIMM13 (insn) == 0)))
  675.         return start_pc;

  676.       /* We found a valid stack-check sequence, return the new PC.  */

  677.       /* optional: clr [%g4 - some immediate]  */
  678.       insn = sparc_fetch_instruction (pc);
  679.       pc = pc + 4;
  680.       if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
  681.             && X_RS1 (insn) == 4 && X_RD (insn) == 0))
  682.         return pc - 4;
  683.       else
  684.         return pc;
  685.     }

  686.   /* No stack check code in our prologue, return the start_pc.  */
  687.   return start_pc;
  688. }

  689. /* Record the effect of a SAVE instruction on CACHE.  */

  690. void
  691. sparc_record_save_insn (struct sparc_frame_cache *cache)
  692. {
  693.   /* The frame is set up.  */
  694.   cache->frameless_p = 0;

  695.   /* The frame pointer contains the CFA.  */
  696.   cache->frame_offset = 0;

  697.   /* The `local' and `in' registers are all saved.  */
  698.   cache->saved_regs_mask = 0xffff;

  699.   /* The `out' registers are all renamed.  */
  700.   cache->copied_regs_mask = 0xff;
  701. }

  702. /* Do a full analysis of the prologue at PC and update CACHE accordingly.
  703.    Bail out early if CURRENT_PC is reached.  Return the address where
  704.    the analysis stopped.

  705.    We handle both the traditional register window model and the single
  706.    register window (aka flat) model.  */

  707. CORE_ADDR
  708. sparc_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
  709.                         CORE_ADDR current_pc, struct sparc_frame_cache *cache)
  710. {
  711.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  712.   unsigned long insn;
  713.   int offset = 0;
  714.   int dest = -1;

  715.   pc = sparc_skip_stack_check (pc);

  716.   if (current_pc <= pc)
  717.     return current_pc;

  718.   /* We have to handle to "Procedure Linkage Table" (PLT) special.  On
  719.      SPARC the linker usually defines a symbol (typically
  720.      _PROCEDURE_LINKAGE_TABLE_) at the start of the .plt section.
  721.      This symbol makes us end up here with PC pointing at the start of
  722.      the PLT and CURRENT_PC probably pointing at a PLT entry.  If we
  723.      would do our normal prologue analysis, we would probably conclude
  724.      that we've got a frame when in reality we don't, since the
  725.      dynamic linker patches up the first PLT with some code that
  726.      starts with a SAVE instruction.  Patch up PC such that it points
  727.      at the start of our PLT entry.  */
  728.   if (tdep->plt_entry_size > 0 && in_plt_section (current_pc))
  729.     pc = current_pc - ((current_pc - pc) % tdep->plt_entry_size);

  730.   insn = sparc_fetch_instruction (pc);

  731.   /* Recognize store insns and record their sources.  */
  732.   while (X_OP (insn) == 3
  733.          && (X_OP3 (insn) == 0x4     /* stw */
  734.              || X_OP3 (insn) == 0x7  /* std */
  735.              || X_OP3 (insn) == 0xe) /* stx */
  736.          && X_RS1 (insn) == SPARC_SP_REGNUM)
  737.     {
  738.       int regnum = X_RD (insn);

  739.       /* Recognize stores into the corresponding stack slots.  */
  740.       if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
  741.           && ((X_I (insn)
  742.                && X_SIMM13 (insn) == (X_OP3 (insn) == 0xe
  743.                                       ? (regnum - SPARC_L0_REGNUM) * 8 + BIAS
  744.                                       : (regnum - SPARC_L0_REGNUM) * 4))
  745.               || (!X_I (insn) && regnum == SPARC_L0_REGNUM)))
  746.         {
  747.           cache->saved_regs_mask |= (1 << (regnum - SPARC_L0_REGNUM));
  748.           if (X_OP3 (insn) == 0x7)
  749.             cache->saved_regs_mask |= (1 << (regnum + 1 - SPARC_L0_REGNUM));
  750.         }

  751.       offset += 4;

  752.       insn = sparc_fetch_instruction (pc + offset);
  753.     }

  754.   /* Recognize a SETHI insn and record its destination.  */
  755.   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x04)
  756.     {
  757.       dest = X_RD (insn);
  758.       offset += 4;

  759.       insn = sparc_fetch_instruction (pc + offset);
  760.     }

  761.   /* Allow for an arithmetic operation on DEST or %g1.  */
  762.   if (X_OP (insn) == 2 && X_I (insn)
  763.       && (X_RD (insn) == 1 || X_RD (insn) == dest))
  764.     {
  765.       offset += 4;

  766.       insn = sparc_fetch_instruction (pc + offset);
  767.     }

  768.   /* Check for the SAVE instruction that sets up the frame.  */
  769.   if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3c)
  770.     {
  771.       sparc_record_save_insn (cache);
  772.       offset += 4;
  773.       return pc + offset;
  774.     }

  775.   /* Check for an arithmetic operation on %sp.  */
  776.   if (X_OP (insn) == 2
  777.       && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
  778.       && X_RS1 (insn) == SPARC_SP_REGNUM
  779.       && X_RD (insn) == SPARC_SP_REGNUM)
  780.     {
  781.       if (X_I (insn))
  782.         {
  783.           cache->frame_offset = X_SIMM13 (insn);
  784.           if (X_OP3 (insn) == 0)
  785.             cache->frame_offset = -cache->frame_offset;
  786.         }
  787.       offset += 4;

  788.       insn = sparc_fetch_instruction (pc + offset);

  789.       /* Check for an arithmetic operation that sets up the frame.  */
  790.       if (X_OP (insn) == 2
  791.           && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
  792.           && X_RS1 (insn) == SPARC_SP_REGNUM
  793.           && X_RD (insn) == SPARC_FP_REGNUM)
  794.         {
  795.           cache->frameless_p = 0;
  796.           cache->frame_offset = 0;
  797.           /* We could check that the amount subtracted to %sp above is the
  798.              same as the one added here, but this seems superfluous.  */
  799.           cache->copied_regs_mask |= 0x40;
  800.           offset += 4;

  801.           insn = sparc_fetch_instruction (pc + offset);
  802.         }

  803.       /* Check for a move (or) operation that copies the return register.  */
  804.       if (X_OP (insn) == 2
  805.           && X_OP3 (insn) == 0x2
  806.           && !X_I (insn)
  807.           && X_RS1 (insn) == SPARC_G0_REGNUM
  808.           && X_RS2 (insn) == SPARC_O7_REGNUM
  809.           && X_RD (insn) == SPARC_I7_REGNUM)
  810.         {
  811.            cache->copied_regs_mask |= 0x80;
  812.            offset += 4;
  813.         }

  814.       return pc + offset;
  815.     }

  816.   return pc;
  817. }

  818. static CORE_ADDR
  819. sparc_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
  820. {
  821.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  822.   return frame_unwind_register_unsigned (this_frame, tdep->pc_regnum);
  823. }

  824. /* Return PC of first real instruction of the function starting at
  825.    START_PC.  */

  826. static CORE_ADDR
  827. sparc32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  828. {
  829.   struct symtab_and_line sal;
  830.   CORE_ADDR func_start, func_end;
  831.   struct sparc_frame_cache cache;

  832.   /* This is the preferred method, find the end of the prologue by
  833.      using the debugging information.  */
  834.   if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
  835.     {
  836.       sal = find_pc_line (func_start, 0);

  837.       if (sal.end < func_end
  838.           && start_pc <= sal.end)
  839.         return sal.end;
  840.     }

  841.   start_pc = sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffUL, &cache);

  842.   /* The psABI says that "Although the first 6 words of arguments
  843.      reside in registers, the standard stack frame reserves space for
  844.      them.".  It also suggests that a function may use that space to
  845.      "write incoming arguments 0 to 5" into that space, and that's
  846.      indeed what GCC seems to be doing.  In that case GCC will
  847.      generate debug information that points to the stack slots instead
  848.      of the registers, so we should consider the instructions that
  849.      write out these incoming arguments onto the stack.  */

  850.   while (1)
  851.     {
  852.       unsigned long insn = sparc_fetch_instruction (start_pc);

  853.       /* Recognize instructions that store incoming arguments into the
  854.          corresponding stack slots.  */
  855.       if (X_OP (insn) == 3 && (X_OP3 (insn) & 0x3c) == 0x04
  856.           && X_I (insn) && X_RS1 (insn) == SPARC_FP_REGNUM)
  857.         {
  858.           int regnum = X_RD (insn);

  859.           /* Case of arguments still in %o[0..5].  */
  860.           if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O5_REGNUM
  861.               && !(cache.copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM)))
  862.               && X_SIMM13 (insn) == 68 + (regnum - SPARC_O0_REGNUM) * 4)
  863.             {
  864.               start_pc += 4;
  865.               continue;
  866.             }

  867.           /* Case of arguments copied into %i[0..5].  */
  868.           if (regnum >= SPARC_I0_REGNUM && regnum <= SPARC_I5_REGNUM
  869.               && (cache.copied_regs_mask & (1 << (regnum - SPARC_I0_REGNUM)))
  870.               && X_SIMM13 (insn) == 68 + (regnum - SPARC_I0_REGNUM) * 4)
  871.             {
  872.               start_pc += 4;
  873.               continue;
  874.             }
  875.         }

  876.       break;
  877.     }

  878.   return start_pc;
  879. }

  880. /* Normal frames.  */

  881. struct sparc_frame_cache *
  882. sparc_frame_cache (struct frame_info *this_frame, void **this_cache)
  883. {
  884.   struct sparc_frame_cache *cache;

  885.   if (*this_cache)
  886.     return *this_cache;

  887.   cache = sparc_alloc_frame_cache ();
  888.   *this_cache = cache;

  889.   cache->pc = get_frame_func (this_frame);
  890.   if (cache->pc != 0)
  891.     sparc_analyze_prologue (get_frame_arch (this_frame), cache->pc,
  892.                             get_frame_pc (this_frame), cache);

  893.   if (cache->frameless_p)
  894.     {
  895.       /* This function is frameless, so %fp (%i6) holds the frame
  896.          pointer for our calling frame.  Use %sp (%o6) as this frame's
  897.          base address.  */
  898.       cache->base =
  899.         get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
  900.     }
  901.   else
  902.     {
  903.       /* For normal frames, %fp (%i6) holds the frame pointer, the
  904.          base address for the current stack frame.  */
  905.       cache->base =
  906.         get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
  907.     }

  908.   cache->base += cache->frame_offset;

  909.   if (cache->base & 1)
  910.     cache->base += BIAS;

  911.   return cache;
  912. }

  913. static int
  914. sparc32_struct_return_from_sym (struct symbol *sym)
  915. {
  916.   struct type *type = check_typedef (SYMBOL_TYPE (sym));
  917.   enum type_code code = TYPE_CODE (type);

  918.   if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
  919.     {
  920.       type = check_typedef (TYPE_TARGET_TYPE (type));
  921.       if (sparc_structure_or_union_p (type)
  922.           || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16))
  923.         return 1;
  924.     }

  925.   return 0;
  926. }

  927. struct sparc_frame_cache *
  928. sparc32_frame_cache (struct frame_info *this_frame, void **this_cache)
  929. {
  930.   struct sparc_frame_cache *cache;
  931.   struct symbol *sym;

  932.   if (*this_cache)
  933.     return *this_cache;

  934.   cache = sparc_frame_cache (this_frame, this_cache);

  935.   sym = find_pc_function (cache->pc);
  936.   if (sym)
  937.     {
  938.       cache->struct_return_p = sparc32_struct_return_from_sym (sym);
  939.     }
  940.   else
  941.     {
  942.       /* There is no debugging information for this function to
  943.          help us determine whether this function returns a struct
  944.          or not.  So we rely on another heuristic which is to check
  945.          the instruction at the return address and see if this is
  946.          an "unimp" instruction.  If it is, then it is a struct-return
  947.          function.  */
  948.       CORE_ADDR pc;
  949.       int regnum =
  950.         (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;

  951.       pc = get_frame_register_unsigned (this_frame, regnum) + 8;
  952.       if (sparc_is_unimp_insn (pc))
  953.         cache->struct_return_p = 1;
  954.     }

  955.   return cache;
  956. }

  957. static void
  958. sparc32_frame_this_id (struct frame_info *this_frame, void **this_cache,
  959.                        struct frame_id *this_id)
  960. {
  961.   struct sparc_frame_cache *cache =
  962.     sparc32_frame_cache (this_frame, this_cache);

  963.   /* This marks the outermost frame.  */
  964.   if (cache->base == 0)
  965.     return;

  966.   (*this_id) = frame_id_build (cache->base, cache->pc);
  967. }

  968. static struct value *
  969. sparc32_frame_prev_register (struct frame_info *this_frame,
  970.                              void **this_cache, int regnum)
  971. {
  972.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  973.   struct sparc_frame_cache *cache =
  974.     sparc32_frame_cache (this_frame, this_cache);

  975.   if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
  976.     {
  977.       CORE_ADDR pc = (regnum == SPARC32_NPC_REGNUM) ? 4 : 0;

  978.       /* If this functions has a Structure, Union or Quad-Precision
  979.          return value, we have to skip the UNIMP instruction that encodes
  980.          the size of the structure.  */
  981.       if (cache->struct_return_p)
  982.         pc += 4;

  983.       regnum =
  984.         (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
  985.       pc += get_frame_register_unsigned (this_frame, regnum) + 8;
  986.       return frame_unwind_got_constant (this_frame, regnum, pc);
  987.     }

  988.   /* Handle StackGhost.  */
  989.   {
  990.     ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);

  991.     if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
  992.       {
  993.         CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
  994.         ULONGEST i7;

  995.         /* Read the value in from memory.  */
  996.         i7 = get_frame_memory_unsigned (this_frame, addr, 4);
  997.         return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
  998.       }
  999.   }

  1000.   /* The previous frame's `local' and `in' registers may have been saved
  1001.      in the register save area.  */
  1002.   if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
  1003.       && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
  1004.     {
  1005.       CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;

  1006.       return frame_unwind_got_memory (this_frame, regnum, addr);
  1007.     }

  1008.   /* The previous frame's `out' registers may be accessible as the current
  1009.      frame's `in' registers.  */
  1010.   if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
  1011.       && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
  1012.     regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);

  1013.   return frame_unwind_got_register (this_frame, regnum, regnum);
  1014. }

  1015. static const struct frame_unwind sparc32_frame_unwind =
  1016. {
  1017.   NORMAL_FRAME,
  1018.   default_frame_unwind_stop_reason,
  1019.   sparc32_frame_this_id,
  1020.   sparc32_frame_prev_register,
  1021.   NULL,
  1022.   default_frame_sniffer
  1023. };


  1024. static CORE_ADDR
  1025. sparc32_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1026. {
  1027.   struct sparc_frame_cache *cache =
  1028.     sparc32_frame_cache (this_frame, this_cache);

  1029.   return cache->base;
  1030. }

  1031. static const struct frame_base sparc32_frame_base =
  1032. {
  1033.   &sparc32_frame_unwind,
  1034.   sparc32_frame_base_address,
  1035.   sparc32_frame_base_address,
  1036.   sparc32_frame_base_address
  1037. };

  1038. static struct frame_id
  1039. sparc_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1040. {
  1041.   CORE_ADDR sp;

  1042.   sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
  1043.   if (sp & 1)
  1044.     sp += BIAS;
  1045.   return frame_id_build (sp, get_frame_pc (this_frame));
  1046. }


  1047. /* Extract a function return value of TYPE from REGCACHE, and copy
  1048.    that into VALBUF.  */

  1049. static void
  1050. sparc32_extract_return_value (struct type *type, struct regcache *regcache,
  1051.                               gdb_byte *valbuf)
  1052. {
  1053.   int len = TYPE_LENGTH (type);
  1054.   gdb_byte buf[32];

  1055.   gdb_assert (!sparc_structure_or_union_p (type));
  1056.   gdb_assert (!(sparc_floating_p (type) && len == 16));

  1057.   if (sparc_floating_p (type) || sparc_complex_floating_p (type))
  1058.     {
  1059.       /* Floating return values.  */
  1060.       regcache_cooked_read (regcache, SPARC_F0_REGNUM, buf);
  1061.       if (len > 4)
  1062.         regcache_cooked_read (regcache, SPARC_F1_REGNUM, buf + 4);
  1063.       if (len > 8)
  1064.         {
  1065.           regcache_cooked_read (regcache, SPARC_F2_REGNUM, buf + 8);
  1066.           regcache_cooked_read (regcache, SPARC_F3_REGNUM, buf + 12);
  1067.         }
  1068.       if (len > 16)
  1069.         {
  1070.           regcache_cooked_read (regcache, SPARC_F4_REGNUM, buf + 16);
  1071.           regcache_cooked_read (regcache, SPARC_F5_REGNUM, buf + 20);
  1072.           regcache_cooked_read (regcache, SPARC_F6_REGNUM, buf + 24);
  1073.           regcache_cooked_read (regcache, SPARC_F7_REGNUM, buf + 28);
  1074.         }
  1075.       memcpy (valbuf, buf, len);
  1076.     }
  1077.   else
  1078.     {
  1079.       /* Integral and pointer return values.  */
  1080.       gdb_assert (sparc_integral_or_pointer_p (type));

  1081.       regcache_cooked_read (regcache, SPARC_O0_REGNUM, buf);
  1082.       if (len > 4)
  1083.         {
  1084.           regcache_cooked_read (regcache, SPARC_O1_REGNUM, buf + 4);
  1085.           gdb_assert (len == 8);
  1086.           memcpy (valbuf, buf, 8);
  1087.         }
  1088.       else
  1089.         {
  1090.           /* Just stripping off any unused bytes should preserve the
  1091.              signed-ness just fine.  */
  1092.           memcpy (valbuf, buf + 4 - len, len);
  1093.         }
  1094.     }
  1095. }

  1096. /* Store the function return value of type TYPE from VALBUF into
  1097.    REGCACHE.  */

  1098. static void
  1099. sparc32_store_return_value (struct type *type, struct regcache *regcache,
  1100.                             const gdb_byte *valbuf)
  1101. {
  1102.   int len = TYPE_LENGTH (type);
  1103.   gdb_byte buf[8];

  1104.   gdb_assert (!sparc_structure_or_union_p (type));
  1105.   gdb_assert (!(sparc_floating_p (type) && len == 16));
  1106.   gdb_assert (len <= 8);

  1107.   if (sparc_floating_p (type) || sparc_complex_floating_p (type))
  1108.     {
  1109.       /* Floating return values.  */
  1110.       memcpy (buf, valbuf, len);
  1111.       regcache_cooked_write (regcache, SPARC_F0_REGNUM, buf);
  1112.       if (len > 4)
  1113.         regcache_cooked_write (regcache, SPARC_F1_REGNUM, buf + 4);
  1114.       if (len > 8)
  1115.         {
  1116.           regcache_cooked_write (regcache, SPARC_F2_REGNUM, buf + 8);
  1117.           regcache_cooked_write (regcache, SPARC_F3_REGNUM, buf + 12);
  1118.         }
  1119.       if (len > 16)
  1120.         {
  1121.           regcache_cooked_write (regcache, SPARC_F4_REGNUM, buf + 16);
  1122.           regcache_cooked_write (regcache, SPARC_F5_REGNUM, buf + 20);
  1123.           regcache_cooked_write (regcache, SPARC_F6_REGNUM, buf + 24);
  1124.           regcache_cooked_write (regcache, SPARC_F7_REGNUM, buf + 28);
  1125.         }
  1126.     }
  1127.   else
  1128.     {
  1129.       /* Integral and pointer return values.  */
  1130.       gdb_assert (sparc_integral_or_pointer_p (type));

  1131.       if (len > 4)
  1132.         {
  1133.           gdb_assert (len == 8);
  1134.           memcpy (buf, valbuf, 8);
  1135.           regcache_cooked_write (regcache, SPARC_O1_REGNUM, buf + 4);
  1136.         }
  1137.       else
  1138.         {
  1139.           /* ??? Do we need to do any sign-extension here?  */
  1140.           memcpy (buf + 4 - len, valbuf, len);
  1141.         }
  1142.       regcache_cooked_write (regcache, SPARC_O0_REGNUM, buf);
  1143.     }
  1144. }

  1145. static enum return_value_convention
  1146. sparc32_return_value (struct gdbarch *gdbarch, struct value *function,
  1147.                       struct type *type, struct regcache *regcache,
  1148.                       gdb_byte *readbuf, const gdb_byte *writebuf)
  1149. {
  1150.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  1151.   /* The psABI says that "...every stack frame reserves the word at
  1152.      %fp+64.  If a function returns a structure, union, or
  1153.      quad-precision value, this word should hold the address of the
  1154.      object into which the return value should be copied."  This
  1155.      guarantees that we can always find the return value, not just
  1156.      before the function returns.  */

  1157.   if (sparc_structure_or_union_p (type)
  1158.       || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16))
  1159.     {
  1160.       ULONGEST sp;
  1161.       CORE_ADDR addr;

  1162.       if (readbuf)
  1163.         {
  1164.           regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
  1165.           addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
  1166.           read_memory (addr, readbuf, TYPE_LENGTH (type));
  1167.         }
  1168.       if (writebuf)
  1169.         {
  1170.           regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
  1171.           addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
  1172.           write_memory (addr, writebuf, TYPE_LENGTH (type));
  1173.         }

  1174.       return RETURN_VALUE_ABI_PRESERVES_ADDRESS;
  1175.     }

  1176.   if (readbuf)
  1177.     sparc32_extract_return_value (type, regcache, readbuf);
  1178.   if (writebuf)
  1179.     sparc32_store_return_value (type, regcache, writebuf);

  1180.   return RETURN_VALUE_REGISTER_CONVENTION;
  1181. }

  1182. static int
  1183. sparc32_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
  1184. {
  1185.   return (sparc_structure_or_union_p (type)
  1186.           || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16)
  1187.           || sparc_complex_floating_p (type));
  1188. }

  1189. static int
  1190. sparc32_dwarf2_struct_return_p (struct frame_info *this_frame)
  1191. {
  1192.   CORE_ADDR pc = get_frame_address_in_block (this_frame);
  1193.   struct symbol *sym = find_pc_function (pc);

  1194.   if (sym)
  1195.     return sparc32_struct_return_from_sym (sym);
  1196.   return 0;
  1197. }

  1198. static void
  1199. sparc32_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
  1200.                                struct dwarf2_frame_state_reg *reg,
  1201.                                struct frame_info *this_frame)
  1202. {
  1203.   int off;

  1204.   switch (regnum)
  1205.     {
  1206.     case SPARC_G0_REGNUM:
  1207.       /* Since %g0 is always zero, there is no point in saving it, and
  1208.          people will be inclined omit it from the CFI.  Make sure we
  1209.          don't warn about that.  */
  1210.       reg->how = DWARF2_FRAME_REG_SAME_VALUE;
  1211.       break;
  1212.     case SPARC_SP_REGNUM:
  1213.       reg->how = DWARF2_FRAME_REG_CFA;
  1214.       break;
  1215.     case SPARC32_PC_REGNUM:
  1216.     case SPARC32_NPC_REGNUM:
  1217.       reg->how = DWARF2_FRAME_REG_RA_OFFSET;
  1218.       off = 8;
  1219.       if (sparc32_dwarf2_struct_return_p (this_frame))
  1220.         off += 4;
  1221.       if (regnum == SPARC32_NPC_REGNUM)
  1222.         off += 4;
  1223.       reg->loc.offset = off;
  1224.       break;
  1225.     }
  1226. }


  1227. /* The SPARC Architecture doesn't have hardware single-step support,
  1228.    and most operating systems don't implement it either, so we provide
  1229.    software single-step mechanism.  */

  1230. static CORE_ADDR
  1231. sparc_analyze_control_transfer (struct frame_info *frame,
  1232.                                 CORE_ADDR pc, CORE_ADDR *npc)
  1233. {
  1234.   unsigned long insn = sparc_fetch_instruction (pc);
  1235.   int conditional_p = X_COND (insn) & 0x7;
  1236.   int branch_p = 0, fused_p = 0;
  1237.   long offset = 0;                        /* Must be signed for sign-extend.  */

  1238.   if (X_OP (insn) == 0 && X_OP2 (insn) == 3)
  1239.     {
  1240.       if ((insn & 0x10000000) == 0)
  1241.         {
  1242.           /* Branch on Integer Register with Prediction (BPr).  */
  1243.           branch_p = 1;
  1244.           conditional_p = 1;
  1245.         }
  1246.       else
  1247.         {
  1248.           /* Compare and Branch  */
  1249.           branch_p = 1;
  1250.           fused_p = 1;
  1251.           offset = 4 * X_DISP10 (insn);
  1252.         }
  1253.     }
  1254.   else if (X_OP (insn) == 0 && X_OP2 (insn) == 6)
  1255.     {
  1256.       /* Branch on Floating-Point Condition Codes (FBfcc).  */
  1257.       branch_p = 1;
  1258.       offset = 4 * X_DISP22 (insn);
  1259.     }
  1260.   else if (X_OP (insn) == 0 && X_OP2 (insn) == 5)
  1261.     {
  1262.       /* Branch on Floating-Point Condition Codes with Prediction
  1263.          (FBPfcc).  */
  1264.       branch_p = 1;
  1265.       offset = 4 * X_DISP19 (insn);
  1266.     }
  1267.   else if (X_OP (insn) == 0 && X_OP2 (insn) == 2)
  1268.     {
  1269.       /* Branch on Integer Condition Codes (Bicc).  */
  1270.       branch_p = 1;
  1271.       offset = 4 * X_DISP22 (insn);
  1272.     }
  1273.   else if (X_OP (insn) == 0 && X_OP2 (insn) == 1)
  1274.     {
  1275.       /* Branch on Integer Condition Codes with Prediction (BPcc).  */
  1276.       branch_p = 1;
  1277.       offset = 4 * X_DISP19 (insn);
  1278.     }
  1279.   else if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3a)
  1280.     {
  1281.       /* Trap instruction (TRAP).  */
  1282.       return gdbarch_tdep (get_frame_arch (frame))->step_trap (frame, insn);
  1283.     }

  1284.   /* FIXME: Handle DONE and RETRY instructions.  */

  1285.   if (branch_p)
  1286.     {
  1287.       if (fused_p)
  1288.         {
  1289.           /* Fused compare-and-branch instructions are non-delayed,
  1290.              and do not have an annuling capability.  So we need to
  1291.              always set a breakpoint on both the NPC and the branch
  1292.              target address.  */
  1293.           gdb_assert (offset != 0);
  1294.           return pc + offset;
  1295.         }
  1296.       else if (conditional_p)
  1297.         {
  1298.           /* For conditional branches, return nPC + 4 iff the annul
  1299.              bit is 1.  */
  1300.           return (X_A (insn) ? *npc + 4 : 0);
  1301.         }
  1302.       else
  1303.         {
  1304.           /* For unconditional branches, return the target if its
  1305.              specified condition is "always" and return nPC + 4 if the
  1306.              condition is "never".  If the annul bit is 1, set *NPC to
  1307.              zero.  */
  1308.           if (X_COND (insn) == 0x0)
  1309.             pc = *npc, offset = 4;
  1310.           if (X_A (insn))
  1311.             *npc = 0;

  1312.           return pc + offset;
  1313.         }
  1314.     }

  1315.   return 0;
  1316. }

  1317. static CORE_ADDR
  1318. sparc_step_trap (struct frame_info *frame, unsigned long insn)
  1319. {
  1320.   return 0;
  1321. }

  1322. int
  1323. sparc_software_single_step (struct frame_info *frame)
  1324. {
  1325.   struct gdbarch *arch = get_frame_arch (frame);
  1326.   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
  1327.   struct address_space *aspace = get_frame_address_space (frame);
  1328.   CORE_ADDR npc, nnpc;

  1329.   CORE_ADDR pc, orig_npc;

  1330.   pc = get_frame_register_unsigned (frame, tdep->pc_regnum);
  1331.   orig_npc = npc = get_frame_register_unsigned (frame, tdep->npc_regnum);

  1332.   /* Analyze the instruction at PC.  */
  1333.   nnpc = sparc_analyze_control_transfer (frame, pc, &npc);
  1334.   if (npc != 0)
  1335.     insert_single_step_breakpoint (arch, aspace, npc);

  1336.   if (nnpc != 0)
  1337.     insert_single_step_breakpoint (arch, aspace, nnpc);

  1338.   /* Assert that we have set at least one breakpoint, and that
  1339.      they're not set at the same spot - unless we're going
  1340.      from here straight to NULL, i.e. a call or jump to 0.  */
  1341.   gdb_assert (npc != 0 || nnpc != 0 || orig_npc == 0);
  1342.   gdb_assert (nnpc != npc || orig_npc == 0);

  1343.   return 1;
  1344. }

  1345. static void
  1346. sparc_write_pc (struct regcache *regcache, CORE_ADDR pc)
  1347. {
  1348.   struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));

  1349.   regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);
  1350.   regcache_cooked_write_unsigned (regcache, tdep->npc_regnum, pc + 4);
  1351. }


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

  1353. static void
  1354. sparc_iterate_over_regset_sections (struct gdbarch *gdbarch,
  1355.                                     iterate_over_regset_sections_cb *cb,
  1356.                                     void *cb_data,
  1357.                                     const struct regcache *regcache)
  1358. {
  1359.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1360.   cb (".reg", tdep->sizeof_gregset, tdep->gregset, NULL, cb_data);
  1361.   cb (".reg2", tdep->sizeof_fpregset, tdep->fpregset, NULL, cb_data);
  1362. }


  1363. static struct gdbarch *
  1364. sparc32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1365. {
  1366.   struct gdbarch_tdep *tdep;
  1367.   struct gdbarch *gdbarch;

  1368.   /* If there is already a candidate, use it.  */
  1369.   arches = gdbarch_list_lookup_by_info (arches, &info);
  1370.   if (arches != NULL)
  1371.     return arches->gdbarch;

  1372.   /* Allocate space for the new architecture.  */
  1373.   tdep = XCNEW (struct gdbarch_tdep);
  1374.   gdbarch = gdbarch_alloc (&info, tdep);

  1375.   tdep->pc_regnum = SPARC32_PC_REGNUM;
  1376.   tdep->npc_regnum = SPARC32_NPC_REGNUM;
  1377.   tdep->step_trap = sparc_step_trap;

  1378.   set_gdbarch_long_double_bit (gdbarch, 128);
  1379.   set_gdbarch_long_double_format (gdbarch, floatformats_sparc_quad);

  1380.   set_gdbarch_num_regs (gdbarch, SPARC32_NUM_REGS);
  1381.   set_gdbarch_register_name (gdbarch, sparc32_register_name);
  1382.   set_gdbarch_register_type (gdbarch, sparc32_register_type);
  1383.   set_gdbarch_num_pseudo_regs (gdbarch, SPARC32_NUM_PSEUDO_REGS);
  1384.   set_gdbarch_pseudo_register_read (gdbarch, sparc32_pseudo_register_read);
  1385.   set_gdbarch_pseudo_register_write (gdbarch, sparc32_pseudo_register_write);

  1386.   /* Register numbers of various important registers.  */
  1387.   set_gdbarch_sp_regnum (gdbarch, SPARC_SP_REGNUM); /* %sp */
  1388.   set_gdbarch_pc_regnum (gdbarch, SPARC32_PC_REGNUM); /* %pc */
  1389.   set_gdbarch_fp0_regnum (gdbarch, SPARC_F0_REGNUM); /* %f0 */

  1390.   /* Call dummy code.  */
  1391.   set_gdbarch_frame_align (gdbarch, sparc32_frame_align);
  1392.   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
  1393.   set_gdbarch_push_dummy_code (gdbarch, sparc32_push_dummy_code);
  1394.   set_gdbarch_push_dummy_call (gdbarch, sparc32_push_dummy_call);

  1395.   set_gdbarch_return_value (gdbarch, sparc32_return_value);
  1396.   set_gdbarch_stabs_argument_has_addr
  1397.     (gdbarch, sparc32_stabs_argument_has_addr);

  1398.   set_gdbarch_skip_prologue (gdbarch, sparc32_skip_prologue);

  1399.   /* Stack grows downward.  */
  1400.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  1401.   set_gdbarch_breakpoint_from_pc (gdbarch, sparc_breakpoint_from_pc);

  1402.   set_gdbarch_frame_args_skip (gdbarch, 8);

  1403.   set_gdbarch_print_insn (gdbarch, print_insn_sparc);

  1404.   set_gdbarch_software_single_step (gdbarch, sparc_software_single_step);
  1405.   set_gdbarch_write_pc (gdbarch, sparc_write_pc);

  1406.   set_gdbarch_dummy_id (gdbarch, sparc_dummy_id);

  1407.   set_gdbarch_unwind_pc (gdbarch, sparc_unwind_pc);

  1408.   frame_base_set_default (gdbarch, &sparc32_frame_base);

  1409.   /* Hook in the DWARF CFI frame unwinder.  */
  1410.   dwarf2_frame_set_init_reg (gdbarch, sparc32_dwarf2_frame_init_reg);
  1411.   /* FIXME: kettenis/20050423: Don't enable the unwinder until the
  1412.      StackGhost issues have been resolved.  */

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

  1415.   frame_unwind_append_unwinder (gdbarch, &sparc32_frame_unwind);

  1416.   /* If we have register sets, enable the generic core file support.  */
  1417.   if (tdep->gregset)
  1418.     set_gdbarch_iterate_over_regset_sections
  1419.       (gdbarch, sparc_iterate_over_regset_sections);

  1420.   register_sparc_ravenscar_ops (gdbarch);

  1421.   return gdbarch;
  1422. }

  1423. /* Helper functions for dealing with register windows.  */

  1424. void
  1425. sparc_supply_rwindow (struct regcache *regcache, CORE_ADDR sp, int regnum)
  1426. {
  1427.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1428.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1429.   int offset = 0;
  1430.   gdb_byte buf[8];
  1431.   int i;

  1432.   if (sp & 1)
  1433.     {
  1434.       /* Registers are 64-bit.  */
  1435.       sp += BIAS;

  1436.       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1437.         {
  1438.           if (regnum == i || regnum == -1)
  1439.             {
  1440.               target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);

  1441.               /* Handle StackGhost.  */
  1442.               if (i == SPARC_I7_REGNUM)
  1443.                 {
  1444.                   ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
  1445.                   ULONGEST i7;

  1446.                   i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
  1447.                   store_unsigned_integer (buf + offset, 8, byte_order,
  1448.                                           i7 ^ wcookie);
  1449.                 }

  1450.               regcache_raw_supply (regcache, i, buf);
  1451.             }
  1452.         }
  1453.     }
  1454.   else
  1455.     {
  1456.       /* Registers are 32-bit.  Toss any sign-extension of the stack
  1457.          pointer.  */
  1458.       sp &= 0xffffffffUL;

  1459.       /* Clear out the top half of the temporary buffer, and put the
  1460.          register value in the bottom half if we're in 64-bit mode.  */
  1461.       if (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 64)
  1462.         {
  1463.           memset (buf, 0, 4);
  1464.           offset = 4;
  1465.         }

  1466.       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1467.         {
  1468.           if (regnum == i || regnum == -1)
  1469.             {
  1470.               target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
  1471.                                   buf + offset, 4);

  1472.               /* Handle StackGhost.  */
  1473.               if (i == SPARC_I7_REGNUM)
  1474.                 {
  1475.                   ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
  1476.                   ULONGEST i7;

  1477.                   i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
  1478.                   store_unsigned_integer (buf + offset, 4, byte_order,
  1479.                                           i7 ^ wcookie);
  1480.                 }

  1481.               regcache_raw_supply (regcache, i, buf);
  1482.             }
  1483.         }
  1484.     }
  1485. }

  1486. void
  1487. sparc_collect_rwindow (const struct regcache *regcache,
  1488.                        CORE_ADDR sp, int regnum)
  1489. {
  1490.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1491.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1492.   int offset = 0;
  1493.   gdb_byte buf[8];
  1494.   int i;

  1495.   if (sp & 1)
  1496.     {
  1497.       /* Registers are 64-bit.  */
  1498.       sp += BIAS;

  1499.       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1500.         {
  1501.           if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
  1502.             {
  1503.               regcache_raw_collect (regcache, i, buf);

  1504.               /* Handle StackGhost.  */
  1505.               if (i == SPARC_I7_REGNUM)
  1506.                 {
  1507.                   ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
  1508.                   ULONGEST i7;

  1509.                   i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
  1510.                   store_unsigned_integer (buf, 8, byte_order, i7 ^ wcookie);
  1511.                 }

  1512.               target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
  1513.             }
  1514.         }
  1515.     }
  1516.   else
  1517.     {
  1518.       /* Registers are 32-bit.  Toss any sign-extension of the stack
  1519.          pointer.  */
  1520.       sp &= 0xffffffffUL;

  1521.       /* Only use the bottom half if we're in 64-bit mode.  */
  1522.       if (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 64)
  1523.         offset = 4;

  1524.       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1525.         {
  1526.           if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
  1527.             {
  1528.               regcache_raw_collect (regcache, i, buf);

  1529.               /* Handle StackGhost.  */
  1530.               if (i == SPARC_I7_REGNUM)
  1531.                 {
  1532.                   ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
  1533.                   ULONGEST i7;

  1534.                   i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
  1535.                   store_unsigned_integer (buf + offset, 4, byte_order,
  1536.                                           i7 ^ wcookie);
  1537.                 }

  1538.               target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
  1539.                                    buf + offset, 4);
  1540.             }
  1541.         }
  1542.     }
  1543. }

  1544. /* Helper functions for dealing with register sets.  */

  1545. void
  1546. sparc32_supply_gregset (const struct sparc_gregmap *gregmap,
  1547.                         struct regcache *regcache,
  1548.                         int regnum, const void *gregs)
  1549. {
  1550.   const gdb_byte *regs = gregs;
  1551.   gdb_byte zero[4] = { 0 };
  1552.   int i;

  1553.   if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
  1554.     regcache_raw_supply (regcache, SPARC32_PSR_REGNUM,
  1555.                          regs + gregmap->r_psr_offset);

  1556.   if (regnum == SPARC32_PC_REGNUM || regnum == -1)
  1557.     regcache_raw_supply (regcache, SPARC32_PC_REGNUM,
  1558.                          regs + gregmap->r_pc_offset);

  1559.   if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
  1560.     regcache_raw_supply (regcache, SPARC32_NPC_REGNUM,
  1561.                          regs + gregmap->r_npc_offset);

  1562.   if (regnum == SPARC32_Y_REGNUM || regnum == -1)
  1563.     regcache_raw_supply (regcache, SPARC32_Y_REGNUM,
  1564.                          regs + gregmap->r_y_offset);

  1565.   if (regnum == SPARC_G0_REGNUM || regnum == -1)
  1566.     regcache_raw_supply (regcache, SPARC_G0_REGNUM, &zero);

  1567.   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
  1568.     {
  1569.       int offset = gregmap->r_g1_offset;

  1570.       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
  1571.         {
  1572.           if (regnum == i || regnum == -1)
  1573.             regcache_raw_supply (regcache, i, regs + offset);
  1574.           offset += 4;
  1575.         }
  1576.     }

  1577.   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
  1578.     {
  1579.       /* Not all of the register set variants include Locals and
  1580.          Inputs.  For those that don't, we read them off the stack.  */
  1581.       if (gregmap->r_l0_offset == -1)
  1582.         {
  1583.           ULONGEST sp;

  1584.           regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
  1585.           sparc_supply_rwindow (regcache, sp, regnum);
  1586.         }
  1587.       else
  1588.         {
  1589.           int offset = gregmap->r_l0_offset;

  1590.           for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1591.             {
  1592.               if (regnum == i || regnum == -1)
  1593.                 regcache_raw_supply (regcache, i, regs + offset);
  1594.               offset += 4;
  1595.             }
  1596.         }
  1597.     }
  1598. }

  1599. void
  1600. sparc32_collect_gregset (const struct sparc_gregmap *gregmap,
  1601.                          const struct regcache *regcache,
  1602.                          int regnum, void *gregs)
  1603. {
  1604.   gdb_byte *regs = gregs;
  1605.   int i;

  1606.   if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
  1607.     regcache_raw_collect (regcache, SPARC32_PSR_REGNUM,
  1608.                           regs + gregmap->r_psr_offset);

  1609.   if (regnum == SPARC32_PC_REGNUM || regnum == -1)
  1610.     regcache_raw_collect (regcache, SPARC32_PC_REGNUM,
  1611.                           regs + gregmap->r_pc_offset);

  1612.   if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
  1613.     regcache_raw_collect (regcache, SPARC32_NPC_REGNUM,
  1614.                           regs + gregmap->r_npc_offset);

  1615.   if (regnum == SPARC32_Y_REGNUM || regnum == -1)
  1616.     regcache_raw_collect (regcache, SPARC32_Y_REGNUM,
  1617.                           regs + gregmap->r_y_offset);

  1618.   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
  1619.     {
  1620.       int offset = gregmap->r_g1_offset;

  1621.       /* %g0 is always zero.  */
  1622.       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
  1623.         {
  1624.           if (regnum == i || regnum == -1)
  1625.             regcache_raw_collect (regcache, i, regs + offset);
  1626.           offset += 4;
  1627.         }
  1628.     }

  1629.   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
  1630.     {
  1631.       /* Not all of the register set variants include Locals and
  1632.          Inputs.  For those that don't, we read them off the stack.  */
  1633.       if (gregmap->r_l0_offset != -1)
  1634.         {
  1635.           int offset = gregmap->r_l0_offset;

  1636.           for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1637.             {
  1638.               if (regnum == i || regnum == -1)
  1639.                 regcache_raw_collect (regcache, i, regs + offset);
  1640.               offset += 4;
  1641.             }
  1642.         }
  1643.     }
  1644. }

  1645. void
  1646. sparc32_supply_fpregset (const struct sparc_fpregmap *fpregmap,
  1647.                          struct regcache *regcache,
  1648.                          int regnum, const void *fpregs)
  1649. {
  1650.   const gdb_byte *regs = fpregs;
  1651.   int i;

  1652.   for (i = 0; i < 32; i++)
  1653.     {
  1654.       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
  1655.         regcache_raw_supply (regcache, SPARC_F0_REGNUM + i,
  1656.                              regs + fpregmap->r_f0_offset + (i * 4));
  1657.     }

  1658.   if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
  1659.     regcache_raw_supply (regcache, SPARC32_FSR_REGNUM,
  1660.                          regs + fpregmap->r_fsr_offset);
  1661. }

  1662. void
  1663. sparc32_collect_fpregset (const struct sparc_fpregmap *fpregmap,
  1664.                           const struct regcache *regcache,
  1665.                           int regnum, void *fpregs)
  1666. {
  1667.   gdb_byte *regs = fpregs;
  1668.   int i;

  1669.   for (i = 0; i < 32; i++)
  1670.     {
  1671.       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
  1672.         regcache_raw_collect (regcache, SPARC_F0_REGNUM + i,
  1673.                               regs + fpregmap->r_f0_offset + (i * 4));
  1674.     }

  1675.   if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
  1676.     regcache_raw_collect (regcache, SPARC32_FSR_REGNUM,
  1677.                           regs + fpregmap->r_fsr_offset);
  1678. }


  1679. /* SunOS 4.  */

  1680. /* From <machine/reg.h>.  */
  1681. const struct sparc_gregmap sparc32_sunos4_gregmap =
  1682. {
  1683.   0 * 4,                        /* %psr */
  1684.   1 * 4,                        /* %pc */
  1685.   2 * 4,                        /* %npc */
  1686.   3 * 4,                        /* %y */
  1687.   -1,                                /* %wim */
  1688.   -1,                                /* %tbr */
  1689.   4 * 4,                        /* %g1 */
  1690.   -1                                /* %l0 */
  1691. };

  1692. const struct sparc_fpregmap sparc32_sunos4_fpregmap =
  1693. {
  1694.   0 * 4,                        /* %f0 */
  1695.   33 * 4,                        /* %fsr */
  1696. };

  1697. const struct sparc_fpregmap sparc32_bsd_fpregmap =
  1698. {
  1699.   0 * 4,                        /* %f0 */
  1700.   32 * 4,                        /* %fsr */
  1701. };


  1702. /* Provide a prototype to silence -Wmissing-prototypes.  */
  1703. void _initialize_sparc_tdep (void);

  1704. void
  1705. _initialize_sparc_tdep (void)
  1706. {
  1707.   register_gdbarch_init (bfd_arch_sparc, sparc32_gdbarch_init);
  1708. }