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