Home | History | Annotate | Line # | Download | only in gdb
sparc-tdep.c revision 1.6
      1 /* Target-dependent code for SPARC.
      2 
      3    Copyright (C) 2003-2016 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 #include "defs.h"
     21 #include "arch-utils.h"
     22 #include "dis-asm.h"
     23 #include "dwarf2-frame.h"
     24 #include "floatformat.h"
     25 #include "frame.h"
     26 #include "frame-base.h"
     27 #include "frame-unwind.h"
     28 #include "gdbcore.h"
     29 #include "gdbtypes.h"
     30 #include "inferior.h"
     31 #include "symtab.h"
     32 #include "objfiles.h"
     33 #include "osabi.h"
     34 #include "regcache.h"
     35 #include "target.h"
     36 #include "value.h"
     37 
     38 #include "sparc-tdep.h"
     39 #include "sparc-ravenscar-thread.h"
     40 
     41 struct regset;
     42 
     43 /* This file implements the SPARC 32-bit ABI as defined by the section
     44    "Low-Level System Information" of the SPARC Compliance Definition
     45    (SCD) 2.4.1, which is the 32-bit System V psABI for SPARC.  The SCD
     46    lists changes with respect to the original 32-bit psABI as defined
     47    in the "System V ABI, SPARC Processor Supplement".
     48 
     49    Note that if we talk about SunOS, we mean SunOS 4.x, which was
     50    BSD-based, which is sometimes (retroactively?) referred to as
     51    Solaris 1.x.  If we talk about Solaris we mean Solaris 2.x and
     52    above (Solaris 7, 8 and 9 are nothing but Solaris 2.7, 2.8 and 2.9
     53    suffering from severe version number inflation).  Solaris 2.x is
     54    also known as SunOS 5.x, since that's what uname(1) says.  Solaris
     55    2.x is SVR4-based.  */
     56 
     57 /* Please use the sparc32_-prefix for 32-bit specific code, the
     58    sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
     59    code that can handle both.  The 64-bit specific code lives in
     60    sparc64-tdep.c; don't add any here.  */
     61 
     62 /* The SPARC Floating-Point Quad-Precision format is similar to
     63    big-endian IA-64 Quad-Precision format.  */
     64 #define floatformats_sparc_quad floatformats_ia64_quad
     65 
     66 /* The stack pointer is offset from the stack frame by a BIAS of 2047
     67    (0x7ff) for 64-bit code.  BIAS is likely to be defined on SPARC
     68    hosts, so undefine it first.  */
     69 #undef BIAS
     70 #define BIAS 2047
     71 
     72 /* Macros to extract fields from SPARC instructions.  */
     73 #define X_OP(i) (((i) >> 30) & 0x3)
     74 #define X_RD(i) (((i) >> 25) & 0x1f)
     75 #define X_A(i) (((i) >> 29) & 1)
     76 #define X_COND(i) (((i) >> 25) & 0xf)
     77 #define X_OP2(i) (((i) >> 22) & 0x7)
     78 #define X_IMM22(i) ((i) & 0x3fffff)
     79 #define X_OP3(i) (((i) >> 19) & 0x3f)
     80 #define X_RS1(i) (((i) >> 14) & 0x1f)
     81 #define X_RS2(i) ((i) & 0x1f)
     82 #define X_I(i) (((i) >> 13) & 1)
     83 /* Sign extension macros.  */
     84 #define X_DISP22(i) ((X_IMM22 (i) ^ 0x200000) - 0x200000)
     85 #define X_DISP19(i) ((((i) & 0x7ffff) ^ 0x40000) - 0x40000)
     86 #define X_DISP10(i) ((((((i) >> 11) && 0x300) | (((i) >> 5) & 0xff)) ^ 0x200) - 0x200)
     87 #define X_SIMM13(i) ((((i) & 0x1fff) ^ 0x1000) - 0x1000)
     88 /* Macros to identify some instructions.  */
     89 /* RETURN (RETT in V8) */
     90 #define X_RETTURN(i) ((X_OP (i) == 0x2) && (X_OP3 (i) == 0x39))
     91 
     92 /* Fetch the instruction at PC.  Instructions are always big-endian
     93    even if the processor operates in little-endian mode.  */
     94 
     95 unsigned long
     96 sparc_fetch_instruction (CORE_ADDR pc)
     97 {
     98   gdb_byte buf[4];
     99   unsigned long insn;
    100   int i;
    101 
    102   /* If we can't read the instruction at PC, return zero.  */
    103   if (target_read_memory (pc, buf, sizeof (buf)))
    104     return 0;
    105 
    106   insn = 0;
    107   for (i = 0; i < sizeof (buf); i++)
    108     insn = (insn << 8) | buf[i];
    109   return insn;
    110 }
    111 
    112 
    114 /* Return non-zero if the instruction corresponding to PC is an "unimp"
    115    instruction.  */
    116 
    117 static int
    118 sparc_is_unimp_insn (CORE_ADDR pc)
    119 {
    120   const unsigned long insn = sparc_fetch_instruction (pc);
    121 
    122   return ((insn & 0xc1c00000) == 0);
    123 }
    124 
    125 /* Return non-zero if the instruction corresponding to PC is an
    126    "annulled" branch, i.e. the annul bit is set.  */
    127 
    128 int
    129 sparc_is_annulled_branch_insn (CORE_ADDR pc)
    130 {
    131   /* The branch instructions featuring an annul bit can be identified
    132      by the following bit patterns:
    133 
    134      OP=0
    135       OP2=1: Branch on Integer Condition Codes with Prediction (BPcc).
    136       OP2=2: Branch on Integer Condition Codes (Bcc).
    137       OP2=5: Branch on FP Condition Codes with Prediction (FBfcc).
    138       OP2=6: Branch on FP Condition Codes (FBcc).
    139       OP2=3 && Bit28=0:
    140              Branch on Integer Register with Prediction (BPr).
    141 
    142      This leaves out ILLTRAP (OP2=0), SETHI/NOP (OP2=4) and the V8
    143      coprocessor branch instructions (Op2=7).  */
    144 
    145   const unsigned long insn = sparc_fetch_instruction (pc);
    146   const unsigned op2 = X_OP2 (insn);
    147 
    148   if ((X_OP (insn) == 0)
    149       && ((op2 == 1) || (op2 == 2) || (op2 == 5) || (op2 == 6)
    150 	  || ((op2 == 3) && ((insn & 0x10000000) == 0))))
    151     return X_A (insn);
    152   else
    153     return 0;
    154 }
    155 
    156 /* OpenBSD/sparc includes StackGhost, which according to the author's
    157    website http://stackghost.cerias.purdue.edu "... transparently and
    158    automatically protects applications' stack frames; more
    159    specifically, it guards the return pointers.  The protection
    160    mechanisms require no application source or binary modification and
    161    imposes only a negligible performance penalty."
    162 
    163    The same website provides the following description of how
    164    StackGhost works:
    165 
    166    "StackGhost interfaces with the kernel trap handler that would
    167    normally write out registers to the stack and the handler that
    168    would read them back in.  By XORing a cookie into the
    169    return-address saved in the user stack when it is actually written
    170    to the stack, and then XOR it out when the return-address is pulled
    171    from the stack, StackGhost can cause attacker corrupted return
    172    pointers to behave in a manner the attacker cannot predict.
    173    StackGhost can also use several unused bits in the return pointer
    174    to detect a smashed return pointer and abort the process."
    175 
    176    For GDB this means that whenever we're reading %i7 from a stack
    177    frame's window save area, we'll have to XOR the cookie.
    178 
    179    More information on StackGuard can be found on in:
    180 
    181    Mike Frantzen and Mike Shuey.  "StackGhost: Hardware Facilitated
    182    Stack Protection."  2001.  Published in USENIX Security Symposium
    183    '01.  */
    184 
    185 /* Fetch StackGhost Per-Process XOR cookie.  */
    186 
    187 ULONGEST
    188 sparc_fetch_wcookie (struct gdbarch *gdbarch)
    189 {
    190   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    191   struct target_ops *ops = &current_target;
    192   gdb_byte buf[8];
    193   int len;
    194 
    195   len = target_read (ops, TARGET_OBJECT_WCOOKIE, NULL, buf, 0, 8);
    196   if (len == -1)
    197     return 0;
    198 
    199   /* We should have either an 32-bit or an 64-bit cookie.  */
    200   gdb_assert (len == 4 || len == 8);
    201 
    202   return extract_unsigned_integer (buf, len, byte_order);
    203 }
    204 
    205 
    207 /* The functions on this page are intended to be used to classify
    208    function arguments.  */
    209 
    210 /* Check whether TYPE is "Integral or Pointer".  */
    211 
    212 static int
    213 sparc_integral_or_pointer_p (const struct type *type)
    214 {
    215   int len = TYPE_LENGTH (type);
    216 
    217   switch (TYPE_CODE (type))
    218     {
    219     case TYPE_CODE_INT:
    220     case TYPE_CODE_BOOL:
    221     case TYPE_CODE_CHAR:
    222     case TYPE_CODE_ENUM:
    223     case TYPE_CODE_RANGE:
    224       /* We have byte, half-word, word and extended-word/doubleword
    225 	 integral types.  The doubleword is an extension to the
    226 	 original 32-bit ABI by the SCD 2.4.x.  */
    227       return (len == 1 || len == 2 || len == 4 || len == 8);
    228     case TYPE_CODE_PTR:
    229     case TYPE_CODE_REF:
    230       /* Allow either 32-bit or 64-bit pointers.  */
    231       return (len == 4 || len == 8);
    232     default:
    233       break;
    234     }
    235 
    236   return 0;
    237 }
    238 
    239 /* Check whether TYPE is "Floating".  */
    240 
    241 static int
    242 sparc_floating_p (const struct type *type)
    243 {
    244   switch (TYPE_CODE (type))
    245     {
    246     case TYPE_CODE_FLT:
    247       {
    248 	int len = TYPE_LENGTH (type);
    249 	return (len == 4 || len == 8 || len == 16);
    250       }
    251     default:
    252       break;
    253     }
    254 
    255   return 0;
    256 }
    257 
    258 /* Check whether TYPE is "Complex Floating".  */
    259 
    260 static int
    261 sparc_complex_floating_p (const struct type *type)
    262 {
    263   switch (TYPE_CODE (type))
    264     {
    265     case TYPE_CODE_COMPLEX:
    266       {
    267 	int len = TYPE_LENGTH (type);
    268 	return (len == 8 || len == 16 || len == 32);
    269       }
    270     default:
    271       break;
    272     }
    273 
    274   return 0;
    275 }
    276 
    277 /* Check whether TYPE is "Structure or Union".
    278 
    279    In terms of Ada subprogram calls, arrays are treated the same as
    280    struct and union types.  So this function also returns non-zero
    281    for array types.  */
    282 
    283 static int
    284 sparc_structure_or_union_p (const struct type *type)
    285 {
    286   switch (TYPE_CODE (type))
    287     {
    288     case TYPE_CODE_STRUCT:
    289     case TYPE_CODE_UNION:
    290     case TYPE_CODE_ARRAY:
    291       return 1;
    292     default:
    293       break;
    294     }
    295 
    296   return 0;
    297 }
    298 
    299 /* Register information.  */
    300 
    301 static const char *sparc32_register_names[] =
    302 {
    303   "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
    304   "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
    305   "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
    306   "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",
    307 
    308   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
    309   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
    310   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
    311   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
    312 
    313   "y", "psr", "wim", "tbr", "pc", "npc", "fsr", "csr"
    314 };
    315 
    316 /* Total number of registers.  */
    317 #define SPARC32_NUM_REGS ARRAY_SIZE (sparc32_register_names)
    318 
    319 /* We provide the aliases %d0..%d30 for the floating registers as
    320    "psuedo" registers.  */
    321 
    322 static const char *sparc32_pseudo_register_names[] =
    323 {
    324   "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
    325   "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30"
    326 };
    327 
    328 /* Total number of pseudo registers.  */
    329 #define SPARC32_NUM_PSEUDO_REGS ARRAY_SIZE (sparc32_pseudo_register_names)
    330 
    331 /* Return the name of register REGNUM.  */
    332 
    333 static const char *
    334 sparc32_register_name (struct gdbarch *gdbarch, int regnum)
    335 {
    336   if (regnum >= 0 && regnum < SPARC32_NUM_REGS)
    337     return sparc32_register_names[regnum];
    338 
    339   if (regnum < SPARC32_NUM_REGS + SPARC32_NUM_PSEUDO_REGS)
    340     return sparc32_pseudo_register_names[regnum - SPARC32_NUM_REGS];
    341 
    342   return NULL;
    343 }
    344 
    345 /* Construct types for ISA-specific registers.  */
    347 
    348 static struct type *
    349 sparc_psr_type (struct gdbarch *gdbarch)
    350 {
    351   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    352 
    353   if (!tdep->sparc_psr_type)
    354     {
    355       struct type *type;
    356 
    357       type = arch_flags_type (gdbarch, "builtin_type_sparc_psr", 4);
    358       append_flags_type_flag (type, 5, "ET");
    359       append_flags_type_flag (type, 6, "PS");
    360       append_flags_type_flag (type, 7, "S");
    361       append_flags_type_flag (type, 12, "EF");
    362       append_flags_type_flag (type, 13, "EC");
    363 
    364       tdep->sparc_psr_type = type;
    365     }
    366 
    367   return tdep->sparc_psr_type;
    368 }
    369 
    370 static struct type *
    371 sparc_fsr_type (struct gdbarch *gdbarch)
    372 {
    373   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    374 
    375   if (!tdep->sparc_fsr_type)
    376     {
    377       struct type *type;
    378 
    379       type = arch_flags_type (gdbarch, "builtin_type_sparc_fsr", 4);
    380       append_flags_type_flag (type, 0, "NXA");
    381       append_flags_type_flag (type, 1, "DZA");
    382       append_flags_type_flag (type, 2, "UFA");
    383       append_flags_type_flag (type, 3, "OFA");
    384       append_flags_type_flag (type, 4, "NVA");
    385       append_flags_type_flag (type, 5, "NXC");
    386       append_flags_type_flag (type, 6, "DZC");
    387       append_flags_type_flag (type, 7, "UFC");
    388       append_flags_type_flag (type, 8, "OFC");
    389       append_flags_type_flag (type, 9, "NVC");
    390       append_flags_type_flag (type, 22, "NS");
    391       append_flags_type_flag (type, 23, "NXM");
    392       append_flags_type_flag (type, 24, "DZM");
    393       append_flags_type_flag (type, 25, "UFM");
    394       append_flags_type_flag (type, 26, "OFM");
    395       append_flags_type_flag (type, 27, "NVM");
    396 
    397       tdep->sparc_fsr_type = type;
    398     }
    399 
    400   return tdep->sparc_fsr_type;
    401 }
    402 
    403 /* Return the GDB type object for the "standard" data type of data in
    404    register REGNUM.  */
    405 
    406 static struct type *
    407 sparc32_register_type (struct gdbarch *gdbarch, int regnum)
    408 {
    409   if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
    410     return builtin_type (gdbarch)->builtin_float;
    411 
    412   if (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM)
    413     return builtin_type (gdbarch)->builtin_double;
    414 
    415   if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
    416     return builtin_type (gdbarch)->builtin_data_ptr;
    417 
    418   if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
    419     return builtin_type (gdbarch)->builtin_func_ptr;
    420 
    421   if (regnum == SPARC32_PSR_REGNUM)
    422     return sparc_psr_type (gdbarch);
    423 
    424   if (regnum == SPARC32_FSR_REGNUM)
    425     return sparc_fsr_type (gdbarch);
    426 
    427   return builtin_type (gdbarch)->builtin_int32;
    428 }
    429 
    430 static enum register_status
    431 sparc32_pseudo_register_read (struct gdbarch *gdbarch,
    432 			      struct regcache *regcache,
    433 			      int regnum, gdb_byte *buf)
    434 {
    435   enum register_status status;
    436 
    437   gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
    438 
    439   regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
    440   status = regcache_raw_read (regcache, regnum, buf);
    441   if (status == REG_VALID)
    442     status = regcache_raw_read (regcache, regnum + 1, buf + 4);
    443   return status;
    444 }
    445 
    446 static void
    447 sparc32_pseudo_register_write (struct gdbarch *gdbarch,
    448 			       struct regcache *regcache,
    449 			       int regnum, const gdb_byte *buf)
    450 {
    451   gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
    452 
    453   regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
    454   regcache_raw_write (regcache, regnum, buf);
    455   regcache_raw_write (regcache, regnum + 1, buf + 4);
    456 }
    457 
    458 /* Implement the stack_frame_destroyed_p gdbarch method.  */
    460 
    461 int
    462 sparc_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
    463 {
    464   /* This function must return true if we are one instruction after an
    465      instruction that destroyed the stack frame of the current
    466      function.  The SPARC instructions used to restore the callers
    467      stack frame are RESTORE and RETURN/RETT.
    468 
    469      Of these RETURN/RETT is a branch instruction and thus we return
    470      true if we are in its delay slot.
    471 
    472      RESTORE is almost always found in the delay slot of a branch
    473      instruction that transfers control to the caller, such as JMPL.
    474      Thus the next instruction is in the caller frame and we don't
    475      need to do anything about it.  */
    476 
    477   unsigned int insn = sparc_fetch_instruction (pc - 4);
    478 
    479   return X_RETTURN (insn);
    480 }
    481 
    482 
    484 static CORE_ADDR
    485 sparc32_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
    486 {
    487   /* The ABI requires double-word alignment.  */
    488   return address & ~0x7;
    489 }
    490 
    491 static CORE_ADDR
    492 sparc32_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
    493 			 CORE_ADDR funcaddr,
    494 			 struct value **args, int nargs,
    495 			 struct type *value_type,
    496 			 CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
    497 			 struct regcache *regcache)
    498 {
    499   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    500 
    501   *bp_addr = sp - 4;
    502   *real_pc = funcaddr;
    503 
    504   if (using_struct_return (gdbarch, NULL, value_type))
    505     {
    506       gdb_byte buf[4];
    507 
    508       /* This is an UNIMP instruction.  */
    509       store_unsigned_integer (buf, 4, byte_order,
    510 			      TYPE_LENGTH (value_type) & 0x1fff);
    511       write_memory (sp - 8, buf, 4);
    512       return sp - 8;
    513     }
    514 
    515   return sp - 4;
    516 }
    517 
    518 static CORE_ADDR
    519 sparc32_store_arguments (struct regcache *regcache, int nargs,
    520 			 struct value **args, CORE_ADDR sp,
    521 			 int struct_return, CORE_ADDR struct_addr)
    522 {
    523   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    524   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    525   /* Number of words in the "parameter array".  */
    526   int num_elements = 0;
    527   int element = 0;
    528   int i;
    529 
    530   for (i = 0; i < nargs; i++)
    531     {
    532       struct type *type = value_type (args[i]);
    533       int len = TYPE_LENGTH (type);
    534 
    535       if (sparc_structure_or_union_p (type)
    536 	  || (sparc_floating_p (type) && len == 16)
    537 	  || sparc_complex_floating_p (type))
    538 	{
    539 	  /* Structure, Union and Quad-Precision Arguments.  */
    540 	  sp -= len;
    541 
    542 	  /* Use doubleword alignment for these values.  That's always
    543              correct, and wasting a few bytes shouldn't be a problem.  */
    544 	  sp &= ~0x7;
    545 
    546 	  write_memory (sp, value_contents (args[i]), len);
    547 	  args[i] = value_from_pointer (lookup_pointer_type (type), sp);
    548 	  num_elements++;
    549 	}
    550       else if (sparc_floating_p (type))
    551 	{
    552 	  /* Floating arguments.  */
    553 	  gdb_assert (len == 4 || len == 8);
    554 	  num_elements += (len / 4);
    555 	}
    556       else
    557 	{
    558 	  /* Integral and pointer arguments.  */
    559 	  gdb_assert (sparc_integral_or_pointer_p (type));
    560 
    561 	  if (len < 4)
    562 	    args[i] = value_cast (builtin_type (gdbarch)->builtin_int32,
    563 				  args[i]);
    564 	  num_elements += ((len + 3) / 4);
    565 	}
    566     }
    567 
    568   /* Always allocate at least six words.  */
    569   sp -= max (6, num_elements) * 4;
    570 
    571   /* The psABI says that "Software convention requires space for the
    572      struct/union return value pointer, even if the word is unused."  */
    573   sp -= 4;
    574 
    575   /* The psABI says that "Although software convention and the
    576      operating system require every stack frame to be doubleword
    577      aligned."  */
    578   sp &= ~0x7;
    579 
    580   for (i = 0; i < nargs; i++)
    581     {
    582       const bfd_byte *valbuf = value_contents (args[i]);
    583       struct type *type = value_type (args[i]);
    584       int len = TYPE_LENGTH (type);
    585 
    586       gdb_assert (len == 4 || len == 8);
    587 
    588       if (element < 6)
    589 	{
    590 	  int regnum = SPARC_O0_REGNUM + element;
    591 
    592 	  regcache_cooked_write (regcache, regnum, valbuf);
    593 	  if (len > 4 && element < 5)
    594 	    regcache_cooked_write (regcache, regnum + 1, valbuf + 4);
    595 	}
    596 
    597       /* Always store the argument in memory.  */
    598       write_memory (sp + 4 + element * 4, valbuf, len);
    599       element += len / 4;
    600     }
    601 
    602   gdb_assert (element == num_elements);
    603 
    604   if (struct_return)
    605     {
    606       gdb_byte buf[4];
    607 
    608       store_unsigned_integer (buf, 4, byte_order, struct_addr);
    609       write_memory (sp, buf, 4);
    610     }
    611 
    612   return sp;
    613 }
    614 
    615 static CORE_ADDR
    616 sparc32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
    617 			 struct regcache *regcache, CORE_ADDR bp_addr,
    618 			 int nargs, struct value **args, CORE_ADDR sp,
    619 			 int struct_return, CORE_ADDR struct_addr)
    620 {
    621   CORE_ADDR call_pc = (struct_return ? (bp_addr - 12) : (bp_addr - 8));
    622 
    623   /* Set return address.  */
    624   regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, call_pc);
    625 
    626   /* Set up function arguments.  */
    627   sp = sparc32_store_arguments (regcache, nargs, args, sp,
    628 				struct_return, struct_addr);
    629 
    630   /* Allocate the 16-word window save area.  */
    631   sp -= 16 * 4;
    632 
    633   /* Stack should be doubleword aligned at this point.  */
    634   gdb_assert (sp % 8 == 0);
    635 
    636   /* Finally, update the stack pointer.  */
    637   regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
    638 
    639   return sp;
    640 }
    641 
    642 
    644 /* Use the program counter to determine the contents and size of a
    645    breakpoint instruction.  Return a pointer to a string of bytes that
    646    encode a breakpoint instruction, store the length of the string in
    647    *LEN and optionally adjust *PC to point to the correct memory
    648    location for inserting the breakpoint.  */
    649 
    650 static const gdb_byte *
    651 sparc_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
    652 {
    653   static const gdb_byte break_insn[] = { 0x91, 0xd0, 0x20, 0x01 };
    654 
    655   *len = sizeof (break_insn);
    656   return break_insn;
    657 }
    658 
    659 
    661 /* Allocate and initialize a frame cache.  */
    662 
    663 static struct sparc_frame_cache *
    664 sparc_alloc_frame_cache (void)
    665 {
    666   struct sparc_frame_cache *cache;
    667 
    668   cache = FRAME_OBSTACK_ZALLOC (struct sparc_frame_cache);
    669 
    670   /* Base address.  */
    671   cache->base = 0;
    672   cache->pc = 0;
    673 
    674   /* Frameless until proven otherwise.  */
    675   cache->frameless_p = 1;
    676   cache->frame_offset = 0;
    677   cache->saved_regs_mask = 0;
    678   cache->copied_regs_mask = 0;
    679   cache->struct_return_p = 0;
    680 
    681   return cache;
    682 }
    683 
    684 /* GCC generates several well-known sequences of instructions at the begining
    685    of each function prologue when compiling with -fstack-check.  If one of
    686    such sequences starts at START_PC, then return the address of the
    687    instruction immediately past this sequence.  Otherwise, return START_PC.  */
    688 
    689 static CORE_ADDR
    690 sparc_skip_stack_check (const CORE_ADDR start_pc)
    691 {
    692   CORE_ADDR pc = start_pc;
    693   unsigned long insn;
    694   int probing_loop = 0;
    695 
    696   /* With GCC, all stack checking sequences begin with the same two
    697      instructions, plus an optional one in the case of a probing loop:
    698 
    699          sethi <some immediate>, %g1
    700          sub %sp, %g1, %g1
    701 
    702      or:
    703 
    704          sethi <some immediate>, %g1
    705          sethi <some immediate>, %g4
    706          sub %sp, %g1, %g1
    707 
    708      or:
    709 
    710          sethi <some immediate>, %g1
    711          sub %sp, %g1, %g1
    712          sethi <some immediate>, %g4
    713 
    714      If the optional instruction is found (setting g4), assume that a
    715      probing loop will follow.  */
    716 
    717   /* sethi <some immediate>, %g1 */
    718   insn = sparc_fetch_instruction (pc);
    719   pc = pc + 4;
    720   if (!(X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 1))
    721     return start_pc;
    722 
    723   /* optional: sethi <some immediate>, %g4 */
    724   insn = sparc_fetch_instruction (pc);
    725   pc = pc + 4;
    726   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
    727     {
    728       probing_loop = 1;
    729       insn = sparc_fetch_instruction (pc);
    730       pc = pc + 4;
    731     }
    732 
    733   /* sub %sp, %g1, %g1 */
    734   if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
    735         && X_RD (insn) == 1 && X_RS1 (insn) == 14 && X_RS2 (insn) == 1))
    736     return start_pc;
    737 
    738   insn = sparc_fetch_instruction (pc);
    739   pc = pc + 4;
    740 
    741   /* optional: sethi <some immediate>, %g4 */
    742   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
    743     {
    744       probing_loop = 1;
    745       insn = sparc_fetch_instruction (pc);
    746       pc = pc + 4;
    747     }
    748 
    749   /* First possible sequence:
    750          [first two instructions above]
    751          clr [%g1 - some immediate]  */
    752 
    753   /* clr [%g1 - some immediate]  */
    754   if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
    755       && X_RS1 (insn) == 1 && X_RD (insn) == 0)
    756     {
    757       /* Valid stack-check sequence, return the new PC.  */
    758       return pc;
    759     }
    760 
    761   /* Second possible sequence: A small number of probes.
    762          [first two instructions above]
    763          clr [%g1]
    764          add   %g1, -<some immediate>, %g1
    765          clr [%g1]
    766          [repeat the two instructions above any (small) number of times]
    767          clr [%g1 - some immediate]  */
    768 
    769   /* clr [%g1] */
    770   else if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
    771       && X_RS1 (insn) == 1 && X_RD (insn) == 0)
    772     {
    773       while (1)
    774         {
    775           /* add %g1, -<some immediate>, %g1 */
    776           insn = sparc_fetch_instruction (pc);
    777           pc = pc + 4;
    778           if (!(X_OP (insn) == 2  && X_OP3(insn) == 0 && X_I(insn)
    779                 && X_RS1 (insn) == 1 && X_RD (insn) == 1))
    780             break;
    781 
    782           /* clr [%g1] */
    783           insn = sparc_fetch_instruction (pc);
    784           pc = pc + 4;
    785           if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
    786                 && X_RD (insn) == 0 && X_RS1 (insn) == 1))
    787             return start_pc;
    788         }
    789 
    790       /* clr [%g1 - some immediate] */
    791       if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
    792             && X_RS1 (insn) == 1 && X_RD (insn) == 0))
    793         return start_pc;
    794 
    795       /* We found a valid stack-check sequence, return the new PC.  */
    796       return pc;
    797     }
    798 
    799   /* Third sequence: A probing loop.
    800          [first three instructions above]
    801          sub  %g1, %g4, %g4
    802          cmp  %g1, %g4
    803          be  <disp>
    804          add  %g1, -<some immediate>, %g1
    805          ba  <disp>
    806          clr  [%g1]
    807 
    808      And an optional last probe for the remainder:
    809 
    810          clr [%g4 - some immediate]  */
    811 
    812   if (probing_loop)
    813     {
    814       /* sub  %g1, %g4, %g4 */
    815       if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
    816             && X_RD (insn) == 4 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
    817         return start_pc;
    818 
    819       /* cmp  %g1, %g4 */
    820       insn = sparc_fetch_instruction (pc);
    821       pc = pc + 4;
    822       if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x14 && !X_I(insn)
    823             && X_RD (insn) == 0 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
    824         return start_pc;
    825 
    826       /* be  <disp> */
    827       insn = sparc_fetch_instruction (pc);
    828       pc = pc + 4;
    829       if (!(X_OP (insn) == 0 && X_COND (insn) == 0x1))
    830         return start_pc;
    831 
    832       /* add  %g1, -<some immediate>, %g1 */
    833       insn = sparc_fetch_instruction (pc);
    834       pc = pc + 4;
    835       if (!(X_OP (insn) == 2  && X_OP3(insn) == 0 && X_I(insn)
    836             && X_RS1 (insn) == 1 && X_RD (insn) == 1))
    837         return start_pc;
    838 
    839       /* ba  <disp> */
    840       insn = sparc_fetch_instruction (pc);
    841       pc = pc + 4;
    842       if (!(X_OP (insn) == 0 && X_COND (insn) == 0x8))
    843         return start_pc;
    844 
    845       /* clr  [%g1] (st %g0, [%g1] or st %g0, [%g1+0]) */
    846       insn = sparc_fetch_instruction (pc);
    847       pc = pc + 4;
    848       if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4
    849             && X_RD (insn) == 0 && X_RS1 (insn) == 1
    850 	    && (!X_I(insn) || X_SIMM13 (insn) == 0)))
    851         return start_pc;
    852 
    853       /* We found a valid stack-check sequence, return the new PC.  */
    854 
    855       /* optional: clr [%g4 - some immediate]  */
    856       insn = sparc_fetch_instruction (pc);
    857       pc = pc + 4;
    858       if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
    859             && X_RS1 (insn) == 4 && X_RD (insn) == 0))
    860         return pc - 4;
    861       else
    862 	return pc;
    863     }
    864 
    865   /* No stack check code in our prologue, return the start_pc.  */
    866   return start_pc;
    867 }
    868 
    869 /* Record the effect of a SAVE instruction on CACHE.  */
    870 
    871 void
    872 sparc_record_save_insn (struct sparc_frame_cache *cache)
    873 {
    874   /* The frame is set up.  */
    875   cache->frameless_p = 0;
    876 
    877   /* The frame pointer contains the CFA.  */
    878   cache->frame_offset = 0;
    879 
    880   /* The `local' and `in' registers are all saved.  */
    881   cache->saved_regs_mask = 0xffff;
    882 
    883   /* The `out' registers are all renamed.  */
    884   cache->copied_regs_mask = 0xff;
    885 }
    886 
    887 /* Do a full analysis of the prologue at PC and update CACHE accordingly.
    888    Bail out early if CURRENT_PC is reached.  Return the address where
    889    the analysis stopped.
    890 
    891    We handle both the traditional register window model and the single
    892    register window (aka flat) model.  */
    893 
    894 CORE_ADDR
    895 sparc_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
    896 			CORE_ADDR current_pc, struct sparc_frame_cache *cache)
    897 {
    898   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    899   unsigned long insn;
    900   int offset = 0;
    901   int dest = -1;
    902 
    903   pc = sparc_skip_stack_check (pc);
    904 
    905   if (current_pc <= pc)
    906     return current_pc;
    907 
    908   /* We have to handle to "Procedure Linkage Table" (PLT) special.  On
    909      SPARC the linker usually defines a symbol (typically
    910      _PROCEDURE_LINKAGE_TABLE_) at the start of the .plt section.
    911      This symbol makes us end up here with PC pointing at the start of
    912      the PLT and CURRENT_PC probably pointing at a PLT entry.  If we
    913      would do our normal prologue analysis, we would probably conclude
    914      that we've got a frame when in reality we don't, since the
    915      dynamic linker patches up the first PLT with some code that
    916      starts with a SAVE instruction.  Patch up PC such that it points
    917      at the start of our PLT entry.  */
    918   if (tdep->plt_entry_size > 0 && in_plt_section (current_pc))
    919     pc = current_pc - ((current_pc - pc) % tdep->plt_entry_size);
    920 
    921   insn = sparc_fetch_instruction (pc);
    922 
    923   /* Recognize store insns and record their sources.  */
    924   while (X_OP (insn) == 3
    925 	 && (X_OP3 (insn) == 0x4     /* stw */
    926 	     || X_OP3 (insn) == 0x7  /* std */
    927 	     || X_OP3 (insn) == 0xe) /* stx */
    928 	 && X_RS1 (insn) == SPARC_SP_REGNUM)
    929     {
    930       int regnum = X_RD (insn);
    931 
    932       /* Recognize stores into the corresponding stack slots.  */
    933       if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
    934 	  && ((X_I (insn)
    935 	       && X_SIMM13 (insn) == (X_OP3 (insn) == 0xe
    936 				      ? (regnum - SPARC_L0_REGNUM) * 8 + BIAS
    937 				      : (regnum - SPARC_L0_REGNUM) * 4))
    938 	      || (!X_I (insn) && regnum == SPARC_L0_REGNUM)))
    939 	{
    940 	  cache->saved_regs_mask |= (1 << (regnum - SPARC_L0_REGNUM));
    941 	  if (X_OP3 (insn) == 0x7)
    942 	    cache->saved_regs_mask |= (1 << (regnum + 1 - SPARC_L0_REGNUM));
    943 	}
    944 
    945       offset += 4;
    946 
    947       insn = sparc_fetch_instruction (pc + offset);
    948     }
    949 
    950   /* Recognize a SETHI insn and record its destination.  */
    951   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x04)
    952     {
    953       dest = X_RD (insn);
    954       offset += 4;
    955 
    956       insn = sparc_fetch_instruction (pc + offset);
    957     }
    958 
    959   /* Allow for an arithmetic operation on DEST or %g1.  */
    960   if (X_OP (insn) == 2 && X_I (insn)
    961       && (X_RD (insn) == 1 || X_RD (insn) == dest))
    962     {
    963       offset += 4;
    964 
    965       insn = sparc_fetch_instruction (pc + offset);
    966     }
    967 
    968   /* Check for the SAVE instruction that sets up the frame.  */
    969   if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3c)
    970     {
    971       sparc_record_save_insn (cache);
    972       offset += 4;
    973       return pc + offset;
    974     }
    975 
    976   /* Check for an arithmetic operation on %sp.  */
    977   if (X_OP (insn) == 2
    978       && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
    979       && X_RS1 (insn) == SPARC_SP_REGNUM
    980       && X_RD (insn) == SPARC_SP_REGNUM)
    981     {
    982       if (X_I (insn))
    983 	{
    984 	  cache->frame_offset = X_SIMM13 (insn);
    985 	  if (X_OP3 (insn) == 0)
    986 	    cache->frame_offset = -cache->frame_offset;
    987 	}
    988       offset += 4;
    989 
    990       insn = sparc_fetch_instruction (pc + offset);
    991 
    992       /* Check for an arithmetic operation that sets up the frame.  */
    993       if (X_OP (insn) == 2
    994 	  && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
    995 	  && X_RS1 (insn) == SPARC_SP_REGNUM
    996 	  && X_RD (insn) == SPARC_FP_REGNUM)
    997 	{
    998 	  cache->frameless_p = 0;
    999 	  cache->frame_offset = 0;
   1000 	  /* We could check that the amount subtracted to %sp above is the
   1001 	     same as the one added here, but this seems superfluous.  */
   1002 	  cache->copied_regs_mask |= 0x40;
   1003 	  offset += 4;
   1004 
   1005 	  insn = sparc_fetch_instruction (pc + offset);
   1006 	}
   1007 
   1008       /* Check for a move (or) operation that copies the return register.  */
   1009       if (X_OP (insn) == 2
   1010 	  && X_OP3 (insn) == 0x2
   1011 	  && !X_I (insn)
   1012 	  && X_RS1 (insn) == SPARC_G0_REGNUM
   1013 	  && X_RS2 (insn) == SPARC_O7_REGNUM
   1014 	  && X_RD (insn) == SPARC_I7_REGNUM)
   1015 	{
   1016 	   cache->copied_regs_mask |= 0x80;
   1017 	   offset += 4;
   1018 	}
   1019 
   1020       return pc + offset;
   1021     }
   1022 
   1023   return pc;
   1024 }
   1025 
   1026 static CORE_ADDR
   1027 sparc_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
   1028 {
   1029   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
   1030   return frame_unwind_register_unsigned (this_frame, tdep->pc_regnum);
   1031 }
   1032 
   1033 /* Return PC of first real instruction of the function starting at
   1034    START_PC.  */
   1035 
   1036 static CORE_ADDR
   1037 sparc32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
   1038 {
   1039   struct symtab_and_line sal;
   1040   CORE_ADDR func_start, func_end;
   1041   struct sparc_frame_cache cache;
   1042 
   1043   /* This is the preferred method, find the end of the prologue by
   1044      using the debugging information.  */
   1045   if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
   1046     {
   1047       sal = find_pc_line (func_start, 0);
   1048 
   1049       if (sal.end < func_end
   1050 	  && start_pc <= sal.end)
   1051 	return sal.end;
   1052     }
   1053 
   1054   start_pc = sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffUL, &cache);
   1055 
   1056   /* The psABI says that "Although the first 6 words of arguments
   1057      reside in registers, the standard stack frame reserves space for
   1058      them.".  It also suggests that a function may use that space to
   1059      "write incoming arguments 0 to 5" into that space, and that's
   1060      indeed what GCC seems to be doing.  In that case GCC will
   1061      generate debug information that points to the stack slots instead
   1062      of the registers, so we should consider the instructions that
   1063      write out these incoming arguments onto the stack.  */
   1064 
   1065   while (1)
   1066     {
   1067       unsigned long insn = sparc_fetch_instruction (start_pc);
   1068 
   1069       /* Recognize instructions that store incoming arguments into the
   1070 	 corresponding stack slots.  */
   1071       if (X_OP (insn) == 3 && (X_OP3 (insn) & 0x3c) == 0x04
   1072 	  && X_I (insn) && X_RS1 (insn) == SPARC_FP_REGNUM)
   1073 	{
   1074 	  int regnum = X_RD (insn);
   1075 
   1076 	  /* Case of arguments still in %o[0..5].  */
   1077 	  if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O5_REGNUM
   1078 	      && !(cache.copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM)))
   1079 	      && X_SIMM13 (insn) == 68 + (regnum - SPARC_O0_REGNUM) * 4)
   1080 	    {
   1081 	      start_pc += 4;
   1082 	      continue;
   1083 	    }
   1084 
   1085 	  /* Case of arguments copied into %i[0..5].  */
   1086 	  if (regnum >= SPARC_I0_REGNUM && regnum <= SPARC_I5_REGNUM
   1087 	      && (cache.copied_regs_mask & (1 << (regnum - SPARC_I0_REGNUM)))
   1088 	      && X_SIMM13 (insn) == 68 + (regnum - SPARC_I0_REGNUM) * 4)
   1089 	    {
   1090 	      start_pc += 4;
   1091 	      continue;
   1092 	    }
   1093 	}
   1094 
   1095       break;
   1096     }
   1097 
   1098   return start_pc;
   1099 }
   1100 
   1101 /* Normal frames.  */
   1102 
   1103 struct sparc_frame_cache *
   1104 sparc_frame_cache (struct frame_info *this_frame, void **this_cache)
   1105 {
   1106   struct sparc_frame_cache *cache;
   1107 
   1108   if (*this_cache)
   1109     return (struct sparc_frame_cache *) *this_cache;
   1110 
   1111   cache = sparc_alloc_frame_cache ();
   1112   *this_cache = cache;
   1113 
   1114   cache->pc = get_frame_func (this_frame);
   1115   if (cache->pc != 0)
   1116     sparc_analyze_prologue (get_frame_arch (this_frame), cache->pc,
   1117 			    get_frame_pc (this_frame), cache);
   1118 
   1119   if (cache->frameless_p)
   1120     {
   1121       /* This function is frameless, so %fp (%i6) holds the frame
   1122          pointer for our calling frame.  Use %sp (%o6) as this frame's
   1123          base address.  */
   1124       cache->base =
   1125         get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
   1126     }
   1127   else
   1128     {
   1129       /* For normal frames, %fp (%i6) holds the frame pointer, the
   1130          base address for the current stack frame.  */
   1131       cache->base =
   1132 	get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
   1133     }
   1134 
   1135   cache->base += cache->frame_offset;
   1136 
   1137   if (cache->base & 1)
   1138     cache->base += BIAS;
   1139 
   1140   return cache;
   1141 }
   1142 
   1143 static int
   1144 sparc32_struct_return_from_sym (struct symbol *sym)
   1145 {
   1146   struct type *type = check_typedef (SYMBOL_TYPE (sym));
   1147   enum type_code code = TYPE_CODE (type);
   1148 
   1149   if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
   1150     {
   1151       type = check_typedef (TYPE_TARGET_TYPE (type));
   1152       if (sparc_structure_or_union_p (type)
   1153 	  || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16))
   1154 	return 1;
   1155     }
   1156 
   1157   return 0;
   1158 }
   1159 
   1160 struct sparc_frame_cache *
   1161 sparc32_frame_cache (struct frame_info *this_frame, void **this_cache)
   1162 {
   1163   struct sparc_frame_cache *cache;
   1164   struct symbol *sym;
   1165 
   1166   if (*this_cache)
   1167     return (struct sparc_frame_cache *) *this_cache;
   1168 
   1169   cache = sparc_frame_cache (this_frame, this_cache);
   1170 
   1171   sym = find_pc_function (cache->pc);
   1172   if (sym)
   1173     {
   1174       cache->struct_return_p = sparc32_struct_return_from_sym (sym);
   1175     }
   1176   else
   1177     {
   1178       /* There is no debugging information for this function to
   1179          help us determine whether this function returns a struct
   1180          or not.  So we rely on another heuristic which is to check
   1181          the instruction at the return address and see if this is
   1182          an "unimp" instruction.  If it is, then it is a struct-return
   1183          function.  */
   1184       CORE_ADDR pc;
   1185       int regnum =
   1186 	(cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
   1187 
   1188       pc = get_frame_register_unsigned (this_frame, regnum) + 8;
   1189       if (sparc_is_unimp_insn (pc))
   1190         cache->struct_return_p = 1;
   1191     }
   1192 
   1193   return cache;
   1194 }
   1195 
   1196 static void
   1197 sparc32_frame_this_id (struct frame_info *this_frame, void **this_cache,
   1198 		       struct frame_id *this_id)
   1199 {
   1200   struct sparc_frame_cache *cache =
   1201     sparc32_frame_cache (this_frame, this_cache);
   1202 
   1203   /* This marks the outermost frame.  */
   1204   if (cache->base == 0)
   1205     return;
   1206 
   1207   (*this_id) = frame_id_build (cache->base, cache->pc);
   1208 }
   1209 
   1210 static struct value *
   1211 sparc32_frame_prev_register (struct frame_info *this_frame,
   1212 			     void **this_cache, int regnum)
   1213 {
   1214   struct gdbarch *gdbarch = get_frame_arch (this_frame);
   1215   struct sparc_frame_cache *cache =
   1216     sparc32_frame_cache (this_frame, this_cache);
   1217 
   1218   if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
   1219     {
   1220       CORE_ADDR pc = (regnum == SPARC32_NPC_REGNUM) ? 4 : 0;
   1221 
   1222       /* If this functions has a Structure, Union or Quad-Precision
   1223 	 return value, we have to skip the UNIMP instruction that encodes
   1224 	 the size of the structure.  */
   1225       if (cache->struct_return_p)
   1226 	pc += 4;
   1227 
   1228       regnum =
   1229 	(cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
   1230       pc += get_frame_register_unsigned (this_frame, regnum) + 8;
   1231       return frame_unwind_got_constant (this_frame, regnum, pc);
   1232     }
   1233 
   1234   /* Handle StackGhost.  */
   1235   {
   1236     ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
   1237 
   1238     if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
   1239       {
   1240         CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
   1241         ULONGEST i7;
   1242 
   1243         /* Read the value in from memory.  */
   1244         i7 = get_frame_memory_unsigned (this_frame, addr, 4);
   1245         return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
   1246       }
   1247   }
   1248 
   1249   /* The previous frame's `local' and `in' registers may have been saved
   1250      in the register save area.  */
   1251   if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
   1252       && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
   1253     {
   1254       CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
   1255 
   1256       return frame_unwind_got_memory (this_frame, regnum, addr);
   1257     }
   1258 
   1259   /* The previous frame's `out' registers may be accessible as the current
   1260      frame's `in' registers.  */
   1261   if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
   1262       && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
   1263     regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
   1264 
   1265   return frame_unwind_got_register (this_frame, regnum, regnum);
   1266 }
   1267 
   1268 static const struct frame_unwind sparc32_frame_unwind =
   1269 {
   1270   NORMAL_FRAME,
   1271   default_frame_unwind_stop_reason,
   1272   sparc32_frame_this_id,
   1273   sparc32_frame_prev_register,
   1274   NULL,
   1275   default_frame_sniffer
   1276 };
   1277 
   1278 
   1280 static CORE_ADDR
   1281 sparc32_frame_base_address (struct frame_info *this_frame, void **this_cache)
   1282 {
   1283   struct sparc_frame_cache *cache =
   1284     sparc32_frame_cache (this_frame, this_cache);
   1285 
   1286   return cache->base;
   1287 }
   1288 
   1289 static const struct frame_base sparc32_frame_base =
   1290 {
   1291   &sparc32_frame_unwind,
   1292   sparc32_frame_base_address,
   1293   sparc32_frame_base_address,
   1294   sparc32_frame_base_address
   1295 };
   1296 
   1297 static struct frame_id
   1298 sparc_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
   1299 {
   1300   CORE_ADDR sp;
   1301 
   1302   sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
   1303   if (sp & 1)
   1304     sp += BIAS;
   1305   return frame_id_build (sp, get_frame_pc (this_frame));
   1306 }
   1307 
   1308 
   1310 /* Extract a function return value of TYPE from REGCACHE, and copy
   1311    that into VALBUF.  */
   1312 
   1313 static void
   1314 sparc32_extract_return_value (struct type *type, struct regcache *regcache,
   1315 			      gdb_byte *valbuf)
   1316 {
   1317   int len = TYPE_LENGTH (type);
   1318   gdb_byte buf[32];
   1319 
   1320   gdb_assert (!sparc_structure_or_union_p (type));
   1321   gdb_assert (!(sparc_floating_p (type) && len == 16));
   1322 
   1323   if (sparc_floating_p (type) || sparc_complex_floating_p (type))
   1324     {
   1325       /* Floating return values.  */
   1326       regcache_cooked_read (regcache, SPARC_F0_REGNUM, buf);
   1327       if (len > 4)
   1328 	regcache_cooked_read (regcache, SPARC_F1_REGNUM, buf + 4);
   1329       if (len > 8)
   1330 	{
   1331 	  regcache_cooked_read (regcache, SPARC_F2_REGNUM, buf + 8);
   1332 	  regcache_cooked_read (regcache, SPARC_F3_REGNUM, buf + 12);
   1333 	}
   1334       if (len > 16)
   1335 	{
   1336 	  regcache_cooked_read (regcache, SPARC_F4_REGNUM, buf + 16);
   1337 	  regcache_cooked_read (regcache, SPARC_F5_REGNUM, buf + 20);
   1338 	  regcache_cooked_read (regcache, SPARC_F6_REGNUM, buf + 24);
   1339 	  regcache_cooked_read (regcache, SPARC_F7_REGNUM, buf + 28);
   1340 	}
   1341       memcpy (valbuf, buf, len);
   1342     }
   1343   else
   1344     {
   1345       /* Integral and pointer return values.  */
   1346       gdb_assert (sparc_integral_or_pointer_p (type));
   1347 
   1348       regcache_cooked_read (regcache, SPARC_O0_REGNUM, buf);
   1349       if (len > 4)
   1350 	{
   1351 	  regcache_cooked_read (regcache, SPARC_O1_REGNUM, buf + 4);
   1352 	  gdb_assert (len == 8);
   1353 	  memcpy (valbuf, buf, 8);
   1354 	}
   1355       else
   1356 	{
   1357 	  /* Just stripping off any unused bytes should preserve the
   1358 	     signed-ness just fine.  */
   1359 	  memcpy (valbuf, buf + 4 - len, len);
   1360 	}
   1361     }
   1362 }
   1363 
   1364 /* Store the function return value of type TYPE from VALBUF into
   1365    REGCACHE.  */
   1366 
   1367 static void
   1368 sparc32_store_return_value (struct type *type, struct regcache *regcache,
   1369 			    const gdb_byte *valbuf)
   1370 {
   1371   int len = TYPE_LENGTH (type);
   1372   gdb_byte buf[8];
   1373 
   1374   gdb_assert (!sparc_structure_or_union_p (type));
   1375   gdb_assert (!(sparc_floating_p (type) && len == 16));
   1376   gdb_assert (len <= 8);
   1377 
   1378   if (sparc_floating_p (type) || sparc_complex_floating_p (type))
   1379     {
   1380       /* Floating return values.  */
   1381       memcpy (buf, valbuf, len);
   1382       regcache_cooked_write (regcache, SPARC_F0_REGNUM, buf);
   1383       if (len > 4)
   1384 	regcache_cooked_write (regcache, SPARC_F1_REGNUM, buf + 4);
   1385       if (len > 8)
   1386 	{
   1387 	  regcache_cooked_write (regcache, SPARC_F2_REGNUM, buf + 8);
   1388 	  regcache_cooked_write (regcache, SPARC_F3_REGNUM, buf + 12);
   1389 	}
   1390       if (len > 16)
   1391 	{
   1392 	  regcache_cooked_write (regcache, SPARC_F4_REGNUM, buf + 16);
   1393 	  regcache_cooked_write (regcache, SPARC_F5_REGNUM, buf + 20);
   1394 	  regcache_cooked_write (regcache, SPARC_F6_REGNUM, buf + 24);
   1395 	  regcache_cooked_write (regcache, SPARC_F7_REGNUM, buf + 28);
   1396 	}
   1397     }
   1398   else
   1399     {
   1400       /* Integral and pointer return values.  */
   1401       gdb_assert (sparc_integral_or_pointer_p (type));
   1402 
   1403       if (len > 4)
   1404 	{
   1405 	  gdb_assert (len == 8);
   1406 	  memcpy (buf, valbuf, 8);
   1407 	  regcache_cooked_write (regcache, SPARC_O1_REGNUM, buf + 4);
   1408 	}
   1409       else
   1410 	{
   1411 	  /* ??? Do we need to do any sign-extension here?  */
   1412 	  memcpy (buf + 4 - len, valbuf, len);
   1413 	}
   1414       regcache_cooked_write (regcache, SPARC_O0_REGNUM, buf);
   1415     }
   1416 }
   1417 
   1418 static enum return_value_convention
   1419 sparc32_return_value (struct gdbarch *gdbarch, struct value *function,
   1420 		      struct type *type, struct regcache *regcache,
   1421 		      gdb_byte *readbuf, const gdb_byte *writebuf)
   1422 {
   1423   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1424 
   1425   /* The psABI says that "...every stack frame reserves the word at
   1426      %fp+64.  If a function returns a structure, union, or
   1427      quad-precision value, this word should hold the address of the
   1428      object into which the return value should be copied."  This
   1429      guarantees that we can always find the return value, not just
   1430      before the function returns.  */
   1431 
   1432   if (sparc_structure_or_union_p (type)
   1433       || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16))
   1434     {
   1435       ULONGEST sp;
   1436       CORE_ADDR addr;
   1437 
   1438       if (readbuf)
   1439 	{
   1440 	  regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
   1441 	  addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
   1442 	  read_memory (addr, readbuf, TYPE_LENGTH (type));
   1443 	}
   1444       if (writebuf)
   1445 	{
   1446 	  regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
   1447 	  addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
   1448 	  write_memory (addr, writebuf, TYPE_LENGTH (type));
   1449 	}
   1450 
   1451       return RETURN_VALUE_ABI_PRESERVES_ADDRESS;
   1452     }
   1453 
   1454   if (readbuf)
   1455     sparc32_extract_return_value (type, regcache, readbuf);
   1456   if (writebuf)
   1457     sparc32_store_return_value (type, regcache, writebuf);
   1458 
   1459   return RETURN_VALUE_REGISTER_CONVENTION;
   1460 }
   1461 
   1462 static int
   1463 sparc32_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
   1464 {
   1465   return (sparc_structure_or_union_p (type)
   1466 	  || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16)
   1467 	  || sparc_complex_floating_p (type));
   1468 }
   1469 
   1470 static int
   1471 sparc32_dwarf2_struct_return_p (struct frame_info *this_frame)
   1472 {
   1473   CORE_ADDR pc = get_frame_address_in_block (this_frame);
   1474   struct symbol *sym = find_pc_function (pc);
   1475 
   1476   if (sym)
   1477     return sparc32_struct_return_from_sym (sym);
   1478   return 0;
   1479 }
   1480 
   1481 static void
   1482 sparc32_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
   1483 			       struct dwarf2_frame_state_reg *reg,
   1484 			       struct frame_info *this_frame)
   1485 {
   1486   int off;
   1487 
   1488   switch (regnum)
   1489     {
   1490     case SPARC_G0_REGNUM:
   1491       /* Since %g0 is always zero, there is no point in saving it, and
   1492 	 people will be inclined omit it from the CFI.  Make sure we
   1493 	 don't warn about that.  */
   1494       reg->how = DWARF2_FRAME_REG_SAME_VALUE;
   1495       break;
   1496     case SPARC_SP_REGNUM:
   1497       reg->how = DWARF2_FRAME_REG_CFA;
   1498       break;
   1499     case SPARC32_PC_REGNUM:
   1500     case SPARC32_NPC_REGNUM:
   1501       reg->how = DWARF2_FRAME_REG_RA_OFFSET;
   1502       off = 8;
   1503       if (sparc32_dwarf2_struct_return_p (this_frame))
   1504 	off += 4;
   1505       if (regnum == SPARC32_NPC_REGNUM)
   1506 	off += 4;
   1507       reg->loc.offset = off;
   1508       break;
   1509     }
   1510 }
   1511 
   1512 
   1513 /* The SPARC Architecture doesn't have hardware single-step support,
   1515    and most operating systems don't implement it either, so we provide
   1516    software single-step mechanism.  */
   1517 
   1518 static CORE_ADDR
   1519 sparc_analyze_control_transfer (struct frame_info *frame,
   1520 				CORE_ADDR pc, CORE_ADDR *npc)
   1521 {
   1522   unsigned long insn = sparc_fetch_instruction (pc);
   1523   int conditional_p = X_COND (insn) & 0x7;
   1524   int branch_p = 0, fused_p = 0;
   1525   long offset = 0;			/* Must be signed for sign-extend.  */
   1526 
   1527   if (X_OP (insn) == 0 && X_OP2 (insn) == 3)
   1528     {
   1529       if ((insn & 0x10000000) == 0)
   1530 	{
   1531 	  /* Branch on Integer Register with Prediction (BPr).  */
   1532 	  branch_p = 1;
   1533 	  conditional_p = 1;
   1534 	}
   1535       else
   1536 	{
   1537 	  /* Compare and Branch  */
   1538 	  branch_p = 1;
   1539 	  fused_p = 1;
   1540 	  offset = 4 * X_DISP10 (insn);
   1541 	}
   1542     }
   1543   else if (X_OP (insn) == 0 && X_OP2 (insn) == 6)
   1544     {
   1545       /* Branch on Floating-Point Condition Codes (FBfcc).  */
   1546       branch_p = 1;
   1547       offset = 4 * X_DISP22 (insn);
   1548     }
   1549   else if (X_OP (insn) == 0 && X_OP2 (insn) == 5)
   1550     {
   1551       /* Branch on Floating-Point Condition Codes with Prediction
   1552          (FBPfcc).  */
   1553       branch_p = 1;
   1554       offset = 4 * X_DISP19 (insn);
   1555     }
   1556   else if (X_OP (insn) == 0 && X_OP2 (insn) == 2)
   1557     {
   1558       /* Branch on Integer Condition Codes (Bicc).  */
   1559       branch_p = 1;
   1560       offset = 4 * X_DISP22 (insn);
   1561     }
   1562   else if (X_OP (insn) == 0 && X_OP2 (insn) == 1)
   1563     {
   1564       /* Branch on Integer Condition Codes with Prediction (BPcc).  */
   1565       branch_p = 1;
   1566       offset = 4 * X_DISP19 (insn);
   1567     }
   1568   else if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3a)
   1569     {
   1570       /* Trap instruction (TRAP).  */
   1571       return gdbarch_tdep (get_frame_arch (frame))->step_trap (frame, insn);
   1572     }
   1573 
   1574   /* FIXME: Handle DONE and RETRY instructions.  */
   1575 
   1576   if (branch_p)
   1577     {
   1578       if (fused_p)
   1579 	{
   1580 	  /* Fused compare-and-branch instructions are non-delayed,
   1581 	     and do not have an annuling capability.  So we need to
   1582 	     always set a breakpoint on both the NPC and the branch
   1583 	     target address.  */
   1584 	  gdb_assert (offset != 0);
   1585 	  return pc + offset;
   1586 	}
   1587       else if (conditional_p)
   1588 	{
   1589 	  /* For conditional branches, return nPC + 4 iff the annul
   1590 	     bit is 1.  */
   1591 	  return (X_A (insn) ? *npc + 4 : 0);
   1592 	}
   1593       else
   1594 	{
   1595 	  /* For unconditional branches, return the target if its
   1596 	     specified condition is "always" and return nPC + 4 if the
   1597 	     condition is "never".  If the annul bit is 1, set *NPC to
   1598 	     zero.  */
   1599 	  if (X_COND (insn) == 0x0)
   1600 	    pc = *npc, offset = 4;
   1601 	  if (X_A (insn))
   1602 	    *npc = 0;
   1603 
   1604 	  return pc + offset;
   1605 	}
   1606     }
   1607 
   1608   return 0;
   1609 }
   1610 
   1611 static CORE_ADDR
   1612 sparc_step_trap (struct frame_info *frame, unsigned long insn)
   1613 {
   1614   return 0;
   1615 }
   1616 
   1617 static int
   1618 sparc_software_single_step (struct frame_info *frame)
   1619 {
   1620   struct gdbarch *arch = get_frame_arch (frame);
   1621   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
   1622   struct address_space *aspace = get_frame_address_space (frame);
   1623   CORE_ADDR npc, nnpc;
   1624 
   1625   CORE_ADDR pc, orig_npc;
   1626 
   1627   pc = get_frame_register_unsigned (frame, tdep->pc_regnum);
   1628   orig_npc = npc = get_frame_register_unsigned (frame, tdep->npc_regnum);
   1629 
   1630   /* Analyze the instruction at PC.  */
   1631   nnpc = sparc_analyze_control_transfer (frame, pc, &npc);
   1632   if (npc != 0)
   1633     insert_single_step_breakpoint (arch, aspace, npc);
   1634 
   1635   if (nnpc != 0)
   1636     insert_single_step_breakpoint (arch, aspace, nnpc);
   1637 
   1638   /* Assert that we have set at least one breakpoint, and that
   1639      they're not set at the same spot - unless we're going
   1640      from here straight to NULL, i.e. a call or jump to 0.  */
   1641   gdb_assert (npc != 0 || nnpc != 0 || orig_npc == 0);
   1642   gdb_assert (nnpc != npc || orig_npc == 0);
   1643 
   1644   return 1;
   1645 }
   1646 
   1647 static void
   1648 sparc_write_pc (struct regcache *regcache, CORE_ADDR pc)
   1649 {
   1650   struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
   1651 
   1652   regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);
   1653   regcache_cooked_write_unsigned (regcache, tdep->npc_regnum, pc + 4);
   1654 }
   1655 
   1656 
   1658 /* Iterate over core file register note sections.  */
   1659 
   1660 static void
   1661 sparc_iterate_over_regset_sections (struct gdbarch *gdbarch,
   1662 				    iterate_over_regset_sections_cb *cb,
   1663 				    void *cb_data,
   1664 				    const struct regcache *regcache)
   1665 {
   1666   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
   1667 
   1668   cb (".reg", tdep->sizeof_gregset, tdep->gregset, NULL, cb_data);
   1669   cb (".reg2", tdep->sizeof_fpregset, tdep->fpregset, NULL, cb_data);
   1670 }
   1671 
   1672 
   1674 static struct gdbarch *
   1675 sparc32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   1676 {
   1677   struct gdbarch_tdep *tdep;
   1678   struct gdbarch *gdbarch;
   1679 
   1680   /* If there is already a candidate, use it.  */
   1681   arches = gdbarch_list_lookup_by_info (arches, &info);
   1682   if (arches != NULL)
   1683     return arches->gdbarch;
   1684 
   1685   /* Allocate space for the new architecture.  */
   1686   tdep = XCNEW (struct gdbarch_tdep);
   1687   gdbarch = gdbarch_alloc (&info, tdep);
   1688 
   1689   tdep->pc_regnum = SPARC32_PC_REGNUM;
   1690   tdep->npc_regnum = SPARC32_NPC_REGNUM;
   1691   tdep->step_trap = sparc_step_trap;
   1692 
   1693   set_gdbarch_long_double_bit (gdbarch, 128);
   1694   set_gdbarch_long_double_format (gdbarch, floatformats_sparc_quad);
   1695 
   1696   set_gdbarch_num_regs (gdbarch, SPARC32_NUM_REGS);
   1697   set_gdbarch_register_name (gdbarch, sparc32_register_name);
   1698   set_gdbarch_register_type (gdbarch, sparc32_register_type);
   1699   set_gdbarch_num_pseudo_regs (gdbarch, SPARC32_NUM_PSEUDO_REGS);
   1700   set_gdbarch_pseudo_register_read (gdbarch, sparc32_pseudo_register_read);
   1701   set_gdbarch_pseudo_register_write (gdbarch, sparc32_pseudo_register_write);
   1702 
   1703   /* Register numbers of various important registers.  */
   1704   set_gdbarch_sp_regnum (gdbarch, SPARC_SP_REGNUM); /* %sp */
   1705   set_gdbarch_pc_regnum (gdbarch, SPARC32_PC_REGNUM); /* %pc */
   1706   set_gdbarch_fp0_regnum (gdbarch, SPARC_F0_REGNUM); /* %f0 */
   1707 
   1708   /* Call dummy code.  */
   1709   set_gdbarch_frame_align (gdbarch, sparc32_frame_align);
   1710   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
   1711   set_gdbarch_push_dummy_code (gdbarch, sparc32_push_dummy_code);
   1712   set_gdbarch_push_dummy_call (gdbarch, sparc32_push_dummy_call);
   1713 
   1714   set_gdbarch_return_value (gdbarch, sparc32_return_value);
   1715   set_gdbarch_stabs_argument_has_addr
   1716     (gdbarch, sparc32_stabs_argument_has_addr);
   1717 
   1718   set_gdbarch_skip_prologue (gdbarch, sparc32_skip_prologue);
   1719 
   1720   /* Stack grows downward.  */
   1721   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
   1722 
   1723   set_gdbarch_breakpoint_from_pc (gdbarch, sparc_breakpoint_from_pc);
   1724 
   1725   set_gdbarch_frame_args_skip (gdbarch, 8);
   1726 
   1727   set_gdbarch_print_insn (gdbarch, print_insn_sparc);
   1728 
   1729   set_gdbarch_software_single_step (gdbarch, sparc_software_single_step);
   1730   set_gdbarch_write_pc (gdbarch, sparc_write_pc);
   1731 
   1732   set_gdbarch_dummy_id (gdbarch, sparc_dummy_id);
   1733 
   1734   set_gdbarch_unwind_pc (gdbarch, sparc_unwind_pc);
   1735 
   1736   frame_base_set_default (gdbarch, &sparc32_frame_base);
   1737 
   1738   /* Hook in the DWARF CFI frame unwinder.  */
   1739   dwarf2_frame_set_init_reg (gdbarch, sparc32_dwarf2_frame_init_reg);
   1740   /* FIXME: kettenis/20050423: Don't enable the unwinder until the
   1741      StackGhost issues have been resolved.  */
   1742 
   1743   /* Hook in ABI-specific overrides, if they have been registered.  */
   1744   gdbarch_init_osabi (info, gdbarch);
   1745 
   1746   frame_unwind_append_unwinder (gdbarch, &sparc32_frame_unwind);
   1747 
   1748   /* If we have register sets, enable the generic core file support.  */
   1749   if (tdep->gregset)
   1750     set_gdbarch_iterate_over_regset_sections
   1751       (gdbarch, sparc_iterate_over_regset_sections);
   1752 
   1753   register_sparc_ravenscar_ops (gdbarch);
   1754 
   1755   return gdbarch;
   1756 }
   1757 
   1758 /* Helper functions for dealing with register windows.  */
   1760 
   1761 void
   1762 sparc_supply_rwindow (struct regcache *regcache, CORE_ADDR sp, int regnum)
   1763 {
   1764   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   1765   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1766   int offset = 0;
   1767   gdb_byte buf[8];
   1768   int i;
   1769 
   1770   if (sp & 1)
   1771     {
   1772       /* Registers are 64-bit.  */
   1773       sp += BIAS;
   1774 
   1775       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   1776 	{
   1777 	  if (regnum == i || regnum == -1)
   1778 	    {
   1779 	      target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
   1780 
   1781 	      /* Handle StackGhost.  */
   1782 	      if (i == SPARC_I7_REGNUM)
   1783 		{
   1784 		  ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
   1785 		  ULONGEST i7;
   1786 
   1787 		  i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
   1788 		  store_unsigned_integer (buf + offset, 8, byte_order,
   1789 					  i7 ^ wcookie);
   1790 		}
   1791 
   1792 	      regcache_raw_supply (regcache, i, buf);
   1793 	    }
   1794 	}
   1795     }
   1796   else
   1797     {
   1798       /* Registers are 32-bit.  Toss any sign-extension of the stack
   1799 	 pointer.  */
   1800       sp &= 0xffffffffUL;
   1801 
   1802       /* Clear out the top half of the temporary buffer, and put the
   1803 	 register value in the bottom half if we're in 64-bit mode.  */
   1804       if (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 64)
   1805 	{
   1806 	  memset (buf, 0, 4);
   1807 	  offset = 4;
   1808 	}
   1809 
   1810       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   1811 	{
   1812 	  if (regnum == i || regnum == -1)
   1813 	    {
   1814 	      target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
   1815 				  buf + offset, 4);
   1816 
   1817 	      /* Handle StackGhost.  */
   1818 	      if (i == SPARC_I7_REGNUM)
   1819 		{
   1820 		  ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
   1821 		  ULONGEST i7;
   1822 
   1823 		  i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
   1824 		  store_unsigned_integer (buf + offset, 4, byte_order,
   1825 					  i7 ^ wcookie);
   1826 		}
   1827 
   1828 	      regcache_raw_supply (regcache, i, buf);
   1829 	    }
   1830 	}
   1831     }
   1832 }
   1833 
   1834 void
   1835 sparc_collect_rwindow (const struct regcache *regcache,
   1836 		       CORE_ADDR sp, int regnum)
   1837 {
   1838   struct gdbarch *gdbarch = get_regcache_arch (regcache);
   1839   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1840   int offset = 0;
   1841   gdb_byte buf[8];
   1842   int i;
   1843 
   1844   if (sp & 1)
   1845     {
   1846       /* Registers are 64-bit.  */
   1847       sp += BIAS;
   1848 
   1849       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   1850 	{
   1851 	  if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
   1852 	    {
   1853 	      regcache_raw_collect (regcache, i, buf);
   1854 
   1855 	      /* Handle StackGhost.  */
   1856 	      if (i == SPARC_I7_REGNUM)
   1857 		{
   1858 		  ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
   1859 		  ULONGEST i7;
   1860 
   1861 		  i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
   1862 		  store_unsigned_integer (buf, 8, byte_order, i7 ^ wcookie);
   1863 		}
   1864 
   1865 	      target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
   1866 	    }
   1867 	}
   1868     }
   1869   else
   1870     {
   1871       /* Registers are 32-bit.  Toss any sign-extension of the stack
   1872 	 pointer.  */
   1873       sp &= 0xffffffffUL;
   1874 
   1875       /* Only use the bottom half if we're in 64-bit mode.  */
   1876       if (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 64)
   1877 	offset = 4;
   1878 
   1879       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   1880 	{
   1881 	  if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
   1882 	    {
   1883 	      regcache_raw_collect (regcache, i, buf);
   1884 
   1885 	      /* Handle StackGhost.  */
   1886 	      if (i == SPARC_I7_REGNUM)
   1887 		{
   1888 		  ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
   1889 		  ULONGEST i7;
   1890 
   1891 		  i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
   1892 		  store_unsigned_integer (buf + offset, 4, byte_order,
   1893 					  i7 ^ wcookie);
   1894 		}
   1895 
   1896 	      target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
   1897 				   buf + offset, 4);
   1898 	    }
   1899 	}
   1900     }
   1901 }
   1902 
   1903 /* Helper functions for dealing with register sets.  */
   1904 
   1905 void
   1906 sparc32_supply_gregset (const struct sparc_gregmap *gregmap,
   1907 			struct regcache *regcache,
   1908 			int regnum, const void *gregs)
   1909 {
   1910   const gdb_byte *regs = (const gdb_byte *) gregs;
   1911   gdb_byte zero[4] = { 0 };
   1912   int i;
   1913 
   1914   if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
   1915     regcache_raw_supply (regcache, SPARC32_PSR_REGNUM,
   1916 			 regs + gregmap->r_psr_offset);
   1917 
   1918   if (regnum == SPARC32_PC_REGNUM || regnum == -1)
   1919     regcache_raw_supply (regcache, SPARC32_PC_REGNUM,
   1920 			 regs + gregmap->r_pc_offset);
   1921 
   1922   if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
   1923     regcache_raw_supply (regcache, SPARC32_NPC_REGNUM,
   1924 			 regs + gregmap->r_npc_offset);
   1925 
   1926   if (regnum == SPARC32_Y_REGNUM || regnum == -1)
   1927     regcache_raw_supply (regcache, SPARC32_Y_REGNUM,
   1928 			 regs + gregmap->r_y_offset);
   1929 
   1930   if (regnum == SPARC_G0_REGNUM || regnum == -1)
   1931     regcache_raw_supply (regcache, SPARC_G0_REGNUM, &zero);
   1932 
   1933   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
   1934     {
   1935       int offset = gregmap->r_g1_offset;
   1936 
   1937       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
   1938 	{
   1939 	  if (regnum == i || regnum == -1)
   1940 	    regcache_raw_supply (regcache, i, regs + offset);
   1941 	  offset += 4;
   1942 	}
   1943     }
   1944 
   1945   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
   1946     {
   1947       /* Not all of the register set variants include Locals and
   1948          Inputs.  For those that don't, we read them off the stack.  */
   1949       if (gregmap->r_l0_offset == -1)
   1950 	{
   1951 	  ULONGEST sp;
   1952 
   1953 	  regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
   1954 	  sparc_supply_rwindow (regcache, sp, regnum);
   1955 	}
   1956       else
   1957 	{
   1958 	  int offset = gregmap->r_l0_offset;
   1959 
   1960 	  for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   1961 	    {
   1962 	      if (regnum == i || regnum == -1)
   1963 		regcache_raw_supply (regcache, i, regs + offset);
   1964 	      offset += 4;
   1965 	    }
   1966 	}
   1967     }
   1968 }
   1969 
   1970 void
   1971 sparc32_collect_gregset (const struct sparc_gregmap *gregmap,
   1972 			 const struct regcache *regcache,
   1973 			 int regnum, void *gregs)
   1974 {
   1975   gdb_byte *regs = (gdb_byte *) gregs;
   1976   int i;
   1977 
   1978   if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
   1979     regcache_raw_collect (regcache, SPARC32_PSR_REGNUM,
   1980 			  regs + gregmap->r_psr_offset);
   1981 
   1982   if (regnum == SPARC32_PC_REGNUM || regnum == -1)
   1983     regcache_raw_collect (regcache, SPARC32_PC_REGNUM,
   1984 			  regs + gregmap->r_pc_offset);
   1985 
   1986   if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
   1987     regcache_raw_collect (regcache, SPARC32_NPC_REGNUM,
   1988 			  regs + gregmap->r_npc_offset);
   1989 
   1990   if (regnum == SPARC32_Y_REGNUM || regnum == -1)
   1991     regcache_raw_collect (regcache, SPARC32_Y_REGNUM,
   1992 			  regs + gregmap->r_y_offset);
   1993 
   1994   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
   1995     {
   1996       int offset = gregmap->r_g1_offset;
   1997 
   1998       /* %g0 is always zero.  */
   1999       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
   2000 	{
   2001 	  if (regnum == i || regnum == -1)
   2002 	    regcache_raw_collect (regcache, i, regs + offset);
   2003 	  offset += 4;
   2004 	}
   2005     }
   2006 
   2007   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
   2008     {
   2009       /* Not all of the register set variants include Locals and
   2010          Inputs.  For those that don't, we read them off the stack.  */
   2011       if (gregmap->r_l0_offset != -1)
   2012 	{
   2013 	  int offset = gregmap->r_l0_offset;
   2014 
   2015 	  for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   2016 	    {
   2017 	      if (regnum == i || regnum == -1)
   2018 		regcache_raw_collect (regcache, i, regs + offset);
   2019 	      offset += 4;
   2020 	    }
   2021 	}
   2022     }
   2023 }
   2024 
   2025 void
   2026 sparc32_supply_fpregset (const struct sparc_fpregmap *fpregmap,
   2027 			 struct regcache *regcache,
   2028 			 int regnum, const void *fpregs)
   2029 {
   2030   const gdb_byte *regs = (const gdb_byte *) fpregs;
   2031   int i;
   2032 
   2033   for (i = 0; i < 32; i++)
   2034     {
   2035       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
   2036 	regcache_raw_supply (regcache, SPARC_F0_REGNUM + i,
   2037 			     regs + fpregmap->r_f0_offset + (i * 4));
   2038     }
   2039 
   2040   if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
   2041     regcache_raw_supply (regcache, SPARC32_FSR_REGNUM,
   2042 			 regs + fpregmap->r_fsr_offset);
   2043 }
   2044 
   2045 void
   2046 sparc32_collect_fpregset (const struct sparc_fpregmap *fpregmap,
   2047 			  const struct regcache *regcache,
   2048 			  int regnum, void *fpregs)
   2049 {
   2050   gdb_byte *regs = (gdb_byte *) fpregs;
   2051   int i;
   2052 
   2053   for (i = 0; i < 32; i++)
   2054     {
   2055       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
   2056 	regcache_raw_collect (regcache, SPARC_F0_REGNUM + i,
   2057 			      regs + fpregmap->r_f0_offset + (i * 4));
   2058     }
   2059 
   2060   if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
   2061     regcache_raw_collect (regcache, SPARC32_FSR_REGNUM,
   2062 			  regs + fpregmap->r_fsr_offset);
   2063 }
   2064 
   2065 
   2067 /* SunOS 4.  */
   2068 
   2069 /* From <machine/reg.h>.  */
   2070 const struct sparc_gregmap sparc32_sunos4_gregmap =
   2071 {
   2072   0 * 4,			/* %psr */
   2073   1 * 4,			/* %pc */
   2074   2 * 4,			/* %npc */
   2075   3 * 4,			/* %y */
   2076   -1,				/* %wim */
   2077   -1,				/* %tbr */
   2078   4 * 4,			/* %g1 */
   2079   -1				/* %l0 */
   2080 };
   2081 
   2082 const struct sparc_fpregmap sparc32_sunos4_fpregmap =
   2083 {
   2084   0 * 4,			/* %f0 */
   2085   33 * 4,			/* %fsr */
   2086 };
   2087 
   2088 const struct sparc_fpregmap sparc32_bsd_fpregmap =
   2089 {
   2090   0 * 4,			/* %f0 */
   2091   32 * 4,			/* %fsr */
   2092 };
   2093 
   2094 
   2096 /* Provide a prototype to silence -Wmissing-prototypes.  */
   2097 void _initialize_sparc_tdep (void);
   2098 
   2099 void
   2100 _initialize_sparc_tdep (void)
   2101 {
   2102   register_gdbarch_init (bfd_arch_sparc, sparc32_gdbarch_init);
   2103 }
   2104