Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / sparc-tdep.c
blobfbc27ffcb5e686bff79ca749940dfe59503b2f0e
1 /* Target-dependent code for SPARC.
3 Copyright (C) 2003-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "arch-utils.h"
21 #include "dis-asm.h"
22 #include "dwarf2.h"
23 #include "dwarf2/frame.h"
24 #include "frame.h"
25 #include "frame-base.h"
26 #include "frame-unwind.h"
27 #include "gdbcore.h"
28 #include "gdbtypes.h"
29 #include "inferior.h"
30 #include "symtab.h"
31 #include "objfiles.h"
32 #include "osabi.h"
33 #include "regcache.h"
34 #include "target.h"
35 #include "target-descriptions.h"
36 #include "value.h"
38 #include "sparc-tdep.h"
39 #include "sparc-ravenscar-thread.h"
40 #include <algorithm>
42 struct regset;
44 /* This file implements the SPARC 32-bit ABI as defined by the section
45 "Low-Level System Information" of the SPARC Compliance Definition
46 (SCD) 2.4.1, which is the 32-bit System V psABI for SPARC. The SCD
47 lists changes with respect to the original 32-bit psABI as defined
48 in the "System V ABI, SPARC Processor Supplement".
50 Note that if we talk about SunOS, we mean SunOS 4.x, which was
51 BSD-based, which is sometimes (retroactively?) referred to as
52 Solaris 1.x. If we talk about Solaris we mean Solaris 2.x and
53 above (Solaris 7, 8 and 9 are nothing but Solaris 2.7, 2.8 and 2.9
54 suffering from severe version number inflation). Solaris 2.x is
55 also known as SunOS 5.x, since that's what uname(1) says. Solaris
56 2.x is SVR4-based. */
58 /* Please use the sparc32_-prefix for 32-bit specific code, the
59 sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
60 code that can handle both. The 64-bit specific code lives in
61 sparc64-tdep.c; don't add any here. */
63 /* The stack pointer is offset from the stack frame by a BIAS of 2047
64 (0x7ff) for 64-bit code. BIAS is likely to be defined on SPARC
65 hosts, so undefine it first. */
66 #undef BIAS
67 #define BIAS 2047
69 /* Macros to extract fields from SPARC instructions. */
70 #define X_OP(i) (((i) >> 30) & 0x3)
71 #define X_RD(i) (((i) >> 25) & 0x1f)
72 #define X_A(i) (((i) >> 29) & 1)
73 #define X_COND(i) (((i) >> 25) & 0xf)
74 #define X_OP2(i) (((i) >> 22) & 0x7)
75 #define X_IMM22(i) ((i) & 0x3fffff)
76 #define X_OP3(i) (((i) >> 19) & 0x3f)
77 #define X_RS1(i) (((i) >> 14) & 0x1f)
78 #define X_RS2(i) ((i) & 0x1f)
79 #define X_I(i) (((i) >> 13) & 1)
80 /* Sign extension macros. */
81 #define X_DISP22(i) ((X_IMM22 (i) ^ 0x200000) - 0x200000)
82 #define X_DISP19(i) ((((i) & 0x7ffff) ^ 0x40000) - 0x40000)
83 #define X_DISP10(i) ((((((i) >> 11) && 0x300) | (((i) >> 5) & 0xff)) ^ 0x200) - 0x200)
84 #define X_SIMM13(i) ((((i) & 0x1fff) ^ 0x1000) - 0x1000)
85 /* Macros to identify some instructions. */
86 /* RETURN (RETT in V8) */
87 #define X_RETTURN(i) ((X_OP (i) == 0x2) && (X_OP3 (i) == 0x39))
89 /* Fetch the instruction at PC. Instructions are always big-endian
90 even if the processor operates in little-endian mode. */
92 unsigned long
93 sparc_fetch_instruction (CORE_ADDR pc)
95 gdb_byte buf[4];
96 unsigned long insn;
97 int i;
99 /* If we can't read the instruction at PC, return zero. */
100 if (target_read_memory (pc, buf, sizeof (buf)))
101 return 0;
103 insn = 0;
104 for (i = 0; i < sizeof (buf); i++)
105 insn = (insn << 8) | buf[i];
106 return insn;
110 /* Return non-zero if the instruction corresponding to PC is an "unimp"
111 instruction. */
113 static int
114 sparc_is_unimp_insn (CORE_ADDR pc)
116 const unsigned long insn = sparc_fetch_instruction (pc);
118 return ((insn & 0xc1c00000) == 0);
121 /* Return non-zero if the instruction corresponding to PC is an
122 "annulled" branch, i.e. the annul bit is set. */
125 sparc_is_annulled_branch_insn (CORE_ADDR pc)
127 /* The branch instructions featuring an annul bit can be identified
128 by the following bit patterns:
130 OP=0
131 OP2=1: Branch on Integer Condition Codes with Prediction (BPcc).
132 OP2=2: Branch on Integer Condition Codes (Bcc).
133 OP2=5: Branch on FP Condition Codes with Prediction (FBfcc).
134 OP2=6: Branch on FP Condition Codes (FBcc).
135 OP2=3 && Bit28=0:
136 Branch on Integer Register with Prediction (BPr).
138 This leaves out ILLTRAP (OP2=0), SETHI/NOP (OP2=4) and the V8
139 coprocessor branch instructions (Op2=7). */
141 const unsigned long insn = sparc_fetch_instruction (pc);
142 const unsigned op2 = X_OP2 (insn);
144 if ((X_OP (insn) == 0)
145 && ((op2 == 1) || (op2 == 2) || (op2 == 5) || (op2 == 6)
146 || ((op2 == 3) && ((insn & 0x10000000) == 0))))
147 return X_A (insn);
148 else
149 return 0;
152 /* OpenBSD/sparc includes StackGhost, which according to the author's
153 website http://stackghost.cerias.purdue.edu "... transparently and
154 automatically protects applications' stack frames; more
155 specifically, it guards the return pointers. The protection
156 mechanisms require no application source or binary modification and
157 imposes only a negligible performance penalty."
159 The same website provides the following description of how
160 StackGhost works:
162 "StackGhost interfaces with the kernel trap handler that would
163 normally write out registers to the stack and the handler that
164 would read them back in. By XORing a cookie into the
165 return-address saved in the user stack when it is actually written
166 to the stack, and then XOR it out when the return-address is pulled
167 from the stack, StackGhost can cause attacker corrupted return
168 pointers to behave in a manner the attacker cannot predict.
169 StackGhost can also use several unused bits in the return pointer
170 to detect a smashed return pointer and abort the process."
172 For GDB this means that whenever we're reading %i7 from a stack
173 frame's window save area, we'll have to XOR the cookie.
175 More information on StackGuard can be found on in:
177 Mike Frantzen and Mike Shuey. "StackGhost: Hardware Facilitated
178 Stack Protection." 2001. Published in USENIX Security Symposium
179 '01. */
181 /* Fetch StackGhost Per-Process XOR cookie. */
183 ULONGEST
184 sparc_fetch_wcookie (struct gdbarch *gdbarch)
186 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
187 struct target_ops *ops = current_inferior ()->top_target ();
188 gdb_byte buf[8];
189 int len;
191 len = target_read (ops, TARGET_OBJECT_WCOOKIE, NULL, buf, 0, 8);
192 if (len == -1)
193 return 0;
195 /* We should have either an 32-bit or an 64-bit cookie. */
196 gdb_assert (len == 4 || len == 8);
198 return extract_unsigned_integer (buf, len, byte_order);
202 /* The functions on this page are intended to be used to classify
203 function arguments. */
205 /* Check whether TYPE is "Integral or Pointer". */
207 static int
208 sparc_integral_or_pointer_p (const struct type *type)
210 int len = type->length ();
212 switch (type->code ())
214 case TYPE_CODE_INT:
215 case TYPE_CODE_BOOL:
216 case TYPE_CODE_CHAR:
217 case TYPE_CODE_ENUM:
218 case TYPE_CODE_RANGE:
219 /* We have byte, half-word, word and extended-word/doubleword
220 integral types. The doubleword is an extension to the
221 original 32-bit ABI by the SCD 2.4.x. */
222 return (len == 1 || len == 2 || len == 4 || len == 8);
223 case TYPE_CODE_PTR:
224 case TYPE_CODE_REF:
225 case TYPE_CODE_RVALUE_REF:
226 /* Allow either 32-bit or 64-bit pointers. */
227 return (len == 4 || len == 8);
228 default:
229 break;
232 return 0;
235 /* Check whether TYPE is "Floating". */
237 static int
238 sparc_floating_p (const struct type *type)
240 switch (type->code ())
242 case TYPE_CODE_FLT:
244 int len = type->length ();
245 return (len == 4 || len == 8 || len == 16);
247 default:
248 break;
251 return 0;
254 /* Check whether TYPE is "Complex Floating". */
256 static int
257 sparc_complex_floating_p (const struct type *type)
259 switch (type->code ())
261 case TYPE_CODE_COMPLEX:
263 int len = type->length ();
264 return (len == 8 || len == 16 || len == 32);
266 default:
267 break;
270 return 0;
273 /* Check whether TYPE is "Structure or Union".
275 In terms of Ada subprogram calls, arrays are treated the same as
276 struct and union types. So this function also returns non-zero
277 for array types. */
279 static int
280 sparc_structure_or_union_p (const struct type *type)
282 switch (type->code ())
284 case TYPE_CODE_STRUCT:
285 case TYPE_CODE_UNION:
286 case TYPE_CODE_ARRAY:
287 return 1;
288 default:
289 break;
292 return 0;
295 /* Return true if TYPE is returned by memory, false if returned by
296 register. */
298 static bool
299 sparc_structure_return_p (const struct type *type)
301 if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
303 /* Float vectors are always returned by memory. */
304 if (sparc_floating_p (check_typedef (type->target_type ())))
305 return true;
306 /* Integer vectors are returned by memory if the vector size
307 is greater than 8 bytes long. */
308 return (type->length () > 8);
311 if (sparc_floating_p (type))
313 /* Floating point types are passed by register for size 4 and
314 8 bytes, and by memory for size 16 bytes. */
315 return (type->length () == 16);
318 /* Other than that, only aggregates of all sizes get returned by
319 memory. */
320 return sparc_structure_or_union_p (type);
323 /* Return true if arguments of the given TYPE are passed by
324 memory; false if returned by register. */
326 static bool
327 sparc_arg_by_memory_p (const struct type *type)
329 if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
331 /* Float vectors are always passed by memory. */
332 if (sparc_floating_p (check_typedef (type->target_type ())))
333 return true;
334 /* Integer vectors are passed by memory if the vector size
335 is greater than 8 bytes long. */
336 return (type->length () > 8);
339 /* Floats are passed by register for size 4 and 8 bytes, and by memory
340 for size 16 bytes. */
341 if (sparc_floating_p (type))
342 return (type->length () == 16);
344 /* Complex floats and aggregates of all sizes are passed by memory. */
345 if (sparc_complex_floating_p (type) || sparc_structure_or_union_p (type))
346 return true;
348 /* Everything else gets passed by register. */
349 return false;
352 /* Register information. */
353 #define SPARC32_FPU_REGISTERS \
354 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", \
355 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", \
356 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23", \
357 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"
358 #define SPARC32_CP0_REGISTERS \
359 "y", "psr", "wim", "tbr", "pc", "npc", "fsr", "csr"
361 static const char * const sparc_core_register_names[] = {
362 SPARC_CORE_REGISTERS
364 static const char * const sparc32_fpu_register_names[] = {
365 SPARC32_FPU_REGISTERS
367 static const char * const sparc32_cp0_register_names[] = {
368 SPARC32_CP0_REGISTERS
371 static const char * const sparc32_register_names[] =
373 SPARC_CORE_REGISTERS,
374 SPARC32_FPU_REGISTERS,
375 SPARC32_CP0_REGISTERS
378 /* Total number of registers. */
379 #define SPARC32_NUM_REGS ARRAY_SIZE (sparc32_register_names)
381 /* We provide the aliases %d0..%d30 for the floating registers as
382 "psuedo" registers. */
384 static const char * const sparc32_pseudo_register_names[] =
386 "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
387 "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30"
390 /* Total number of pseudo registers. */
391 #define SPARC32_NUM_PSEUDO_REGS ARRAY_SIZE (sparc32_pseudo_register_names)
393 /* Return the name of pseudo register REGNUM. */
395 static const char *
396 sparc32_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
398 regnum -= gdbarch_num_regs (gdbarch);
400 gdb_assert (regnum < SPARC32_NUM_PSEUDO_REGS);
401 return sparc32_pseudo_register_names[regnum];
404 /* Return the name of register REGNUM. */
406 static const char *
407 sparc32_register_name (struct gdbarch *gdbarch, int regnum)
409 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
410 return tdesc_register_name (gdbarch, regnum);
412 if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
413 return sparc32_register_names[regnum];
415 return sparc32_pseudo_register_name (gdbarch, regnum);
418 /* Construct types for ISA-specific registers. */
420 static struct type *
421 sparc_psr_type (struct gdbarch *gdbarch)
423 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
425 if (!tdep->sparc_psr_type)
427 struct type *type;
429 type = arch_flags_type (gdbarch, "builtin_type_sparc_psr", 32);
430 append_flags_type_flag (type, 5, "ET");
431 append_flags_type_flag (type, 6, "PS");
432 append_flags_type_flag (type, 7, "S");
433 append_flags_type_flag (type, 12, "EF");
434 append_flags_type_flag (type, 13, "EC");
436 tdep->sparc_psr_type = type;
439 return tdep->sparc_psr_type;
442 static struct type *
443 sparc_fsr_type (struct gdbarch *gdbarch)
445 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
447 if (!tdep->sparc_fsr_type)
449 struct type *type;
451 type = arch_flags_type (gdbarch, "builtin_type_sparc_fsr", 32);
452 append_flags_type_flag (type, 0, "NXA");
453 append_flags_type_flag (type, 1, "DZA");
454 append_flags_type_flag (type, 2, "UFA");
455 append_flags_type_flag (type, 3, "OFA");
456 append_flags_type_flag (type, 4, "NVA");
457 append_flags_type_flag (type, 5, "NXC");
458 append_flags_type_flag (type, 6, "DZC");
459 append_flags_type_flag (type, 7, "UFC");
460 append_flags_type_flag (type, 8, "OFC");
461 append_flags_type_flag (type, 9, "NVC");
462 append_flags_type_flag (type, 22, "NS");
463 append_flags_type_flag (type, 23, "NXM");
464 append_flags_type_flag (type, 24, "DZM");
465 append_flags_type_flag (type, 25, "UFM");
466 append_flags_type_flag (type, 26, "OFM");
467 append_flags_type_flag (type, 27, "NVM");
469 tdep->sparc_fsr_type = type;
472 return tdep->sparc_fsr_type;
475 /* Return the GDB type object for the "standard" data type of data in
476 pseudo register REGNUM. */
478 static struct type *
479 sparc32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
481 regnum -= gdbarch_num_regs (gdbarch);
483 if (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM)
484 return builtin_type (gdbarch)->builtin_double;
486 internal_error (_("sparc32_pseudo_register_type: bad register number %d"),
487 regnum);
490 /* Return the GDB type object for the "standard" data type of data in
491 register REGNUM. */
493 static struct type *
494 sparc32_register_type (struct gdbarch *gdbarch, int regnum)
496 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
497 return tdesc_register_type (gdbarch, regnum);
499 if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
500 return builtin_type (gdbarch)->builtin_float;
502 if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
503 return builtin_type (gdbarch)->builtin_data_ptr;
505 if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
506 return builtin_type (gdbarch)->builtin_func_ptr;
508 if (regnum == SPARC32_PSR_REGNUM)
509 return sparc_psr_type (gdbarch);
511 if (regnum == SPARC32_FSR_REGNUM)
512 return sparc_fsr_type (gdbarch);
514 if (regnum >= gdbarch_num_regs (gdbarch))
515 return sparc32_pseudo_register_type (gdbarch, regnum);
517 return builtin_type (gdbarch)->builtin_int32;
520 static enum register_status
521 sparc32_pseudo_register_read (struct gdbarch *gdbarch,
522 readable_regcache *regcache,
523 int regnum, gdb_byte *buf)
525 enum register_status status;
527 regnum -= gdbarch_num_regs (gdbarch);
528 gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
530 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
531 status = regcache->raw_read (regnum, buf);
532 if (status == REG_VALID)
533 status = regcache->raw_read (regnum + 1, buf + 4);
534 return status;
537 static void
538 sparc32_pseudo_register_write (struct gdbarch *gdbarch,
539 struct regcache *regcache,
540 int regnum, const gdb_byte *buf)
542 regnum -= gdbarch_num_regs (gdbarch);
543 gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
545 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
546 regcache->raw_write (regnum, buf);
547 regcache->raw_write (regnum + 1, buf + 4);
550 /* Implement the stack_frame_destroyed_p gdbarch method. */
553 sparc_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
555 /* This function must return true if we are one instruction after an
556 instruction that destroyed the stack frame of the current
557 function. The SPARC instructions used to restore the callers
558 stack frame are RESTORE and RETURN/RETT.
560 Of these RETURN/RETT is a branch instruction and thus we return
561 true if we are in its delay slot.
563 RESTORE is almost always found in the delay slot of a branch
564 instruction that transfers control to the caller, such as JMPL.
565 Thus the next instruction is in the caller frame and we don't
566 need to do anything about it. */
568 unsigned int insn = sparc_fetch_instruction (pc - 4);
570 return X_RETTURN (insn);
574 static CORE_ADDR
575 sparc32_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
577 /* The ABI requires double-word alignment. */
578 return address & ~0x7;
581 static CORE_ADDR
582 sparc32_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
583 CORE_ADDR funcaddr,
584 struct value **args, int nargs,
585 struct type *value_type,
586 CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
587 struct regcache *regcache)
589 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
591 *bp_addr = sp - 4;
592 *real_pc = funcaddr;
594 if (using_struct_return (gdbarch, NULL, value_type))
596 gdb_byte buf[4];
598 /* This is an UNIMP instruction. */
599 store_unsigned_integer (buf, 4, byte_order,
600 value_type->length () & 0x1fff);
601 write_memory (sp - 8, buf, 4);
602 return sp - 8;
605 return sp - 4;
608 static CORE_ADDR
609 sparc32_store_arguments (struct regcache *regcache, int nargs,
610 struct value **args, CORE_ADDR sp,
611 function_call_return_method return_method,
612 CORE_ADDR struct_addr)
614 struct gdbarch *gdbarch = regcache->arch ();
615 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
616 /* Number of words in the "parameter array". */
617 int num_elements = 0;
618 int element = 0;
619 int i;
621 for (i = 0; i < nargs; i++)
623 struct type *type = args[i]->type ();
624 int len = type->length ();
626 if (sparc_arg_by_memory_p (type))
628 /* Structure, Union and Quad-Precision Arguments. */
629 sp -= len;
631 /* Use doubleword alignment for these values. That's always
632 correct, and wasting a few bytes shouldn't be a problem. */
633 sp &= ~0x7;
635 write_memory (sp, args[i]->contents ().data (), len);
636 args[i] = value_from_pointer (lookup_pointer_type (type), sp);
637 num_elements++;
639 else if (sparc_floating_p (type))
641 /* Floating arguments. */
642 gdb_assert (len == 4 || len == 8);
643 num_elements += (len / 4);
645 else
647 /* Arguments passed via the General Purpose Registers. */
648 num_elements += ((len + 3) / 4);
652 /* Always allocate at least six words. */
653 sp -= std::max (6, num_elements) * 4;
655 /* The psABI says that "Software convention requires space for the
656 struct/union return value pointer, even if the word is unused." */
657 sp -= 4;
659 /* The psABI says that "Although software convention and the
660 operating system require every stack frame to be doubleword
661 aligned." */
662 sp &= ~0x7;
664 for (i = 0; i < nargs; i++)
666 const bfd_byte *valbuf = args[i]->contents ().data ();
667 struct type *type = args[i]->type ();
668 int len = type->length ();
669 gdb_byte buf[4];
671 if (len < 4)
673 memset (buf, 0, 4 - len);
674 memcpy (buf + 4 - len, valbuf, len);
675 valbuf = buf;
676 len = 4;
679 gdb_assert (len == 4 || len == 8);
681 if (element < 6)
683 int regnum = SPARC_O0_REGNUM + element;
685 regcache->cooked_write (regnum, valbuf);
686 if (len > 4 && element < 5)
687 regcache->cooked_write (regnum + 1, valbuf + 4);
690 /* Always store the argument in memory. */
691 write_memory (sp + 4 + element * 4, valbuf, len);
692 element += len / 4;
695 gdb_assert (element == num_elements);
697 if (return_method == return_method_struct)
699 gdb_byte buf[4];
701 store_unsigned_integer (buf, 4, byte_order, struct_addr);
702 write_memory (sp, buf, 4);
705 return sp;
708 static CORE_ADDR
709 sparc32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
710 struct regcache *regcache, CORE_ADDR bp_addr,
711 int nargs, struct value **args, CORE_ADDR sp,
712 function_call_return_method return_method,
713 CORE_ADDR struct_addr)
715 CORE_ADDR call_pc = (return_method == return_method_struct
716 ? (bp_addr - 12) : (bp_addr - 8));
718 /* Set return address. */
719 regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, call_pc);
721 /* Set up function arguments. */
722 sp = sparc32_store_arguments (regcache, nargs, args, sp, return_method,
723 struct_addr);
725 /* Allocate the 16-word window save area. */
726 sp -= 16 * 4;
728 /* Stack should be doubleword aligned at this point. */
729 gdb_assert (sp % 8 == 0);
731 /* Finally, update the stack pointer. */
732 regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
734 return sp;
738 /* Use the program counter to determine the contents and size of a
739 breakpoint instruction. Return a pointer to a string of bytes that
740 encode a breakpoint instruction, store the length of the string in
741 *LEN and optionally adjust *PC to point to the correct memory
742 location for inserting the breakpoint. */
743 constexpr gdb_byte sparc_break_insn[] = { 0x91, 0xd0, 0x20, 0x01 };
745 typedef BP_MANIPULATION (sparc_break_insn) sparc_breakpoint;
748 /* Allocate and initialize a frame cache. */
750 static struct sparc_frame_cache *
751 sparc_alloc_frame_cache (void)
753 struct sparc_frame_cache *cache;
755 cache = FRAME_OBSTACK_ZALLOC (struct sparc_frame_cache);
757 /* Base address. */
758 cache->base = 0;
759 cache->pc = 0;
761 /* Frameless until proven otherwise. */
762 cache->frameless_p = 1;
763 cache->frame_offset = 0;
764 cache->saved_regs_mask = 0;
765 cache->copied_regs_mask = 0;
766 cache->struct_return_p = 0;
768 return cache;
771 /* GCC generates several well-known sequences of instructions at the begining
772 of each function prologue when compiling with -fstack-check. If one of
773 such sequences starts at START_PC, then return the address of the
774 instruction immediately past this sequence. Otherwise, return START_PC. */
776 static CORE_ADDR
777 sparc_skip_stack_check (const CORE_ADDR start_pc)
779 CORE_ADDR pc = start_pc;
780 unsigned long insn;
781 int probing_loop = 0;
783 /* With GCC, all stack checking sequences begin with the same two
784 instructions, plus an optional one in the case of a probing loop:
786 sethi <some immediate>, %g1
787 sub %sp, %g1, %g1
791 sethi <some immediate>, %g1
792 sethi <some immediate>, %g4
793 sub %sp, %g1, %g1
797 sethi <some immediate>, %g1
798 sub %sp, %g1, %g1
799 sethi <some immediate>, %g4
801 If the optional instruction is found (setting g4), assume that a
802 probing loop will follow. */
804 /* sethi <some immediate>, %g1 */
805 insn = sparc_fetch_instruction (pc);
806 pc = pc + 4;
807 if (!(X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 1))
808 return start_pc;
810 /* optional: sethi <some immediate>, %g4 */
811 insn = sparc_fetch_instruction (pc);
812 pc = pc + 4;
813 if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
815 probing_loop = 1;
816 insn = sparc_fetch_instruction (pc);
817 pc = pc + 4;
820 /* sub %sp, %g1, %g1 */
821 if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
822 && X_RD (insn) == 1 && X_RS1 (insn) == 14 && X_RS2 (insn) == 1))
823 return start_pc;
825 insn = sparc_fetch_instruction (pc);
826 pc = pc + 4;
828 /* optional: sethi <some immediate>, %g4 */
829 if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
831 probing_loop = 1;
832 insn = sparc_fetch_instruction (pc);
833 pc = pc + 4;
836 /* First possible sequence:
837 [first two instructions above]
838 clr [%g1 - some immediate] */
840 /* clr [%g1 - some immediate] */
841 if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
842 && X_RS1 (insn) == 1 && X_RD (insn) == 0)
844 /* Valid stack-check sequence, return the new PC. */
845 return pc;
848 /* Second possible sequence: A small number of probes.
849 [first two instructions above]
850 clr [%g1]
851 add %g1, -<some immediate>, %g1
852 clr [%g1]
853 [repeat the two instructions above any (small) number of times]
854 clr [%g1 - some immediate] */
856 /* clr [%g1] */
857 else if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
858 && X_RS1 (insn) == 1 && X_RD (insn) == 0)
860 while (1)
862 /* add %g1, -<some immediate>, %g1 */
863 insn = sparc_fetch_instruction (pc);
864 pc = pc + 4;
865 if (!(X_OP (insn) == 2 && X_OP3(insn) == 0 && X_I(insn)
866 && X_RS1 (insn) == 1 && X_RD (insn) == 1))
867 break;
869 /* clr [%g1] */
870 insn = sparc_fetch_instruction (pc);
871 pc = pc + 4;
872 if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
873 && X_RD (insn) == 0 && X_RS1 (insn) == 1))
874 return start_pc;
877 /* clr [%g1 - some immediate] */
878 if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
879 && X_RS1 (insn) == 1 && X_RD (insn) == 0))
880 return start_pc;
882 /* We found a valid stack-check sequence, return the new PC. */
883 return pc;
886 /* Third sequence: A probing loop.
887 [first three instructions above]
888 sub %g1, %g4, %g4
889 cmp %g1, %g4
890 be <disp>
891 add %g1, -<some immediate>, %g1
892 ba <disp>
893 clr [%g1]
895 And an optional last probe for the remainder:
897 clr [%g4 - some immediate] */
899 if (probing_loop)
901 /* sub %g1, %g4, %g4 */
902 if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
903 && X_RD (insn) == 4 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
904 return start_pc;
906 /* cmp %g1, %g4 */
907 insn = sparc_fetch_instruction (pc);
908 pc = pc + 4;
909 if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x14 && !X_I(insn)
910 && X_RD (insn) == 0 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
911 return start_pc;
913 /* be <disp> */
914 insn = sparc_fetch_instruction (pc);
915 pc = pc + 4;
916 if (!(X_OP (insn) == 0 && X_COND (insn) == 0x1))
917 return start_pc;
919 /* add %g1, -<some immediate>, %g1 */
920 insn = sparc_fetch_instruction (pc);
921 pc = pc + 4;
922 if (!(X_OP (insn) == 2 && X_OP3(insn) == 0 && X_I(insn)
923 && X_RS1 (insn) == 1 && X_RD (insn) == 1))
924 return start_pc;
926 /* ba <disp> */
927 insn = sparc_fetch_instruction (pc);
928 pc = pc + 4;
929 if (!(X_OP (insn) == 0 && X_COND (insn) == 0x8))
930 return start_pc;
932 /* clr [%g1] (st %g0, [%g1] or st %g0, [%g1+0]) */
933 insn = sparc_fetch_instruction (pc);
934 pc = pc + 4;
935 if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4
936 && X_RD (insn) == 0 && X_RS1 (insn) == 1
937 && (!X_I(insn) || X_SIMM13 (insn) == 0)))
938 return start_pc;
940 /* We found a valid stack-check sequence, return the new PC. */
942 /* optional: clr [%g4 - some immediate] */
943 insn = sparc_fetch_instruction (pc);
944 pc = pc + 4;
945 if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
946 && X_RS1 (insn) == 4 && X_RD (insn) == 0))
947 return pc - 4;
948 else
949 return pc;
952 /* No stack check code in our prologue, return the start_pc. */
953 return start_pc;
956 /* Record the effect of a SAVE instruction on CACHE. */
958 void
959 sparc_record_save_insn (struct sparc_frame_cache *cache)
961 /* The frame is set up. */
962 cache->frameless_p = 0;
964 /* The frame pointer contains the CFA. */
965 cache->frame_offset = 0;
967 /* The `local' and `in' registers are all saved. */
968 cache->saved_regs_mask = 0xffff;
970 /* The `out' registers are all renamed. */
971 cache->copied_regs_mask = 0xff;
974 /* Do a full analysis of the prologue at PC and update CACHE accordingly.
975 Bail out early if CURRENT_PC is reached. Return the address where
976 the analysis stopped.
978 We handle both the traditional register window model and the single
979 register window (aka flat) model. */
981 CORE_ADDR
982 sparc_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
983 CORE_ADDR current_pc, struct sparc_frame_cache *cache)
985 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
986 unsigned long insn;
987 int offset = 0;
988 int dest = -1;
990 pc = sparc_skip_stack_check (pc);
992 if (current_pc <= pc)
993 return current_pc;
995 /* We have to handle to "Procedure Linkage Table" (PLT) special. On
996 SPARC the linker usually defines a symbol (typically
997 _PROCEDURE_LINKAGE_TABLE_) at the start of the .plt section.
998 This symbol makes us end up here with PC pointing at the start of
999 the PLT and CURRENT_PC probably pointing at a PLT entry. If we
1000 would do our normal prologue analysis, we would probably conclude
1001 that we've got a frame when in reality we don't, since the
1002 dynamic linker patches up the first PLT with some code that
1003 starts with a SAVE instruction. Patch up PC such that it points
1004 at the start of our PLT entry. */
1005 if (tdep->plt_entry_size > 0 && in_plt_section (current_pc))
1006 pc = current_pc - ((current_pc - pc) % tdep->plt_entry_size);
1008 insn = sparc_fetch_instruction (pc);
1010 /* Recognize store insns and record their sources. */
1011 while (X_OP (insn) == 3
1012 && (X_OP3 (insn) == 0x4 /* stw */
1013 || X_OP3 (insn) == 0x7 /* std */
1014 || X_OP3 (insn) == 0xe) /* stx */
1015 && X_RS1 (insn) == SPARC_SP_REGNUM)
1017 int regnum = X_RD (insn);
1019 /* Recognize stores into the corresponding stack slots. */
1020 if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
1021 && ((X_I (insn)
1022 && X_SIMM13 (insn) == (X_OP3 (insn) == 0xe
1023 ? (regnum - SPARC_L0_REGNUM) * 8 + BIAS
1024 : (regnum - SPARC_L0_REGNUM) * 4))
1025 || (!X_I (insn) && regnum == SPARC_L0_REGNUM)))
1027 cache->saved_regs_mask |= (1 << (regnum - SPARC_L0_REGNUM));
1028 if (X_OP3 (insn) == 0x7)
1029 cache->saved_regs_mask |= (1 << (regnum + 1 - SPARC_L0_REGNUM));
1032 offset += 4;
1034 insn = sparc_fetch_instruction (pc + offset);
1037 /* Recognize a SETHI insn and record its destination. */
1038 if (X_OP (insn) == 0 && X_OP2 (insn) == 0x04)
1040 dest = X_RD (insn);
1041 offset += 4;
1043 insn = sparc_fetch_instruction (pc + offset);
1046 /* Allow for an arithmetic operation on DEST or %g1. */
1047 if (X_OP (insn) == 2 && X_I (insn)
1048 && (X_RD (insn) == 1 || X_RD (insn) == dest))
1050 offset += 4;
1052 insn = sparc_fetch_instruction (pc + offset);
1055 /* Check for the SAVE instruction that sets up the frame. */
1056 if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3c)
1058 sparc_record_save_insn (cache);
1059 offset += 4;
1060 return pc + offset;
1063 /* Check for an arithmetic operation on %sp. */
1064 if (X_OP (insn) == 2
1065 && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
1066 && X_RS1 (insn) == SPARC_SP_REGNUM
1067 && X_RD (insn) == SPARC_SP_REGNUM)
1069 if (X_I (insn))
1071 cache->frame_offset = X_SIMM13 (insn);
1072 if (X_OP3 (insn) == 0)
1073 cache->frame_offset = -cache->frame_offset;
1075 offset += 4;
1077 insn = sparc_fetch_instruction (pc + offset);
1079 /* Check for an arithmetic operation that sets up the frame. */
1080 if (X_OP (insn) == 2
1081 && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
1082 && X_RS1 (insn) == SPARC_SP_REGNUM
1083 && X_RD (insn) == SPARC_FP_REGNUM)
1085 cache->frameless_p = 0;
1086 cache->frame_offset = 0;
1087 /* We could check that the amount subtracted to %sp above is the
1088 same as the one added here, but this seems superfluous. */
1089 cache->copied_regs_mask |= 0x40;
1090 offset += 4;
1092 insn = sparc_fetch_instruction (pc + offset);
1095 /* Check for a move (or) operation that copies the return register. */
1096 if (X_OP (insn) == 2
1097 && X_OP3 (insn) == 0x2
1098 && !X_I (insn)
1099 && X_RS1 (insn) == SPARC_G0_REGNUM
1100 && X_RS2 (insn) == SPARC_O7_REGNUM
1101 && X_RD (insn) == SPARC_I7_REGNUM)
1103 cache->copied_regs_mask |= 0x80;
1104 offset += 4;
1107 return pc + offset;
1110 return pc;
1113 /* Return PC of first real instruction of the function starting at
1114 START_PC. */
1116 static CORE_ADDR
1117 sparc32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
1119 CORE_ADDR func_addr;
1120 struct sparc_frame_cache cache;
1122 /* This is the preferred method, find the end of the prologue by
1123 using the debugging information. */
1125 if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
1127 CORE_ADDR post_prologue_pc
1128 = skip_prologue_using_sal (gdbarch, func_addr);
1130 if (post_prologue_pc != 0)
1131 return std::max (start_pc, post_prologue_pc);
1134 start_pc = sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffUL, &cache);
1136 /* The psABI says that "Although the first 6 words of arguments
1137 reside in registers, the standard stack frame reserves space for
1138 them.". It also suggests that a function may use that space to
1139 "write incoming arguments 0 to 5" into that space, and that's
1140 indeed what GCC seems to be doing. In that case GCC will
1141 generate debug information that points to the stack slots instead
1142 of the registers, so we should consider the instructions that
1143 write out these incoming arguments onto the stack. */
1145 while (1)
1147 unsigned long insn = sparc_fetch_instruction (start_pc);
1149 /* Recognize instructions that store incoming arguments into the
1150 corresponding stack slots. */
1151 if (X_OP (insn) == 3 && (X_OP3 (insn) & 0x3c) == 0x04
1152 && X_I (insn) && X_RS1 (insn) == SPARC_FP_REGNUM)
1154 int regnum = X_RD (insn);
1156 /* Case of arguments still in %o[0..5]. */
1157 if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O5_REGNUM
1158 && !(cache.copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM)))
1159 && X_SIMM13 (insn) == 68 + (regnum - SPARC_O0_REGNUM) * 4)
1161 start_pc += 4;
1162 continue;
1165 /* Case of arguments copied into %i[0..5]. */
1166 if (regnum >= SPARC_I0_REGNUM && regnum <= SPARC_I5_REGNUM
1167 && (cache.copied_regs_mask & (1 << (regnum - SPARC_I0_REGNUM)))
1168 && X_SIMM13 (insn) == 68 + (regnum - SPARC_I0_REGNUM) * 4)
1170 start_pc += 4;
1171 continue;
1175 break;
1178 return start_pc;
1181 /* Normal frames. */
1183 struct sparc_frame_cache *
1184 sparc_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
1186 struct sparc_frame_cache *cache;
1188 if (*this_cache)
1189 return (struct sparc_frame_cache *) *this_cache;
1191 cache = sparc_alloc_frame_cache ();
1192 *this_cache = cache;
1194 cache->pc = get_frame_func (this_frame);
1195 if (cache->pc != 0)
1196 sparc_analyze_prologue (get_frame_arch (this_frame), cache->pc,
1197 get_frame_pc (this_frame), cache);
1199 if (cache->frameless_p)
1201 /* This function is frameless, so %fp (%i6) holds the frame
1202 pointer for our calling frame. Use %sp (%o6) as this frame's
1203 base address. */
1204 cache->base =
1205 get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
1207 else
1209 /* For normal frames, %fp (%i6) holds the frame pointer, the
1210 base address for the current stack frame. */
1211 cache->base =
1212 get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
1215 cache->base += cache->frame_offset;
1217 if (cache->base & 1)
1218 cache->base += BIAS;
1220 return cache;
1223 static int
1224 sparc32_struct_return_from_sym (struct symbol *sym)
1226 struct type *type = check_typedef (sym->type ());
1227 enum type_code code = type->code ();
1229 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
1231 type = check_typedef (type->target_type ());
1232 if (sparc_structure_or_union_p (type)
1233 || (sparc_floating_p (type) && type->length () == 16))
1234 return 1;
1237 return 0;
1240 struct sparc_frame_cache *
1241 sparc32_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
1243 struct sparc_frame_cache *cache;
1244 struct symbol *sym;
1246 if (*this_cache)
1247 return (struct sparc_frame_cache *) *this_cache;
1249 cache = sparc_frame_cache (this_frame, this_cache);
1251 sym = find_pc_function (cache->pc);
1252 if (sym)
1254 cache->struct_return_p = sparc32_struct_return_from_sym (sym);
1256 else
1258 /* There is no debugging information for this function to
1259 help us determine whether this function returns a struct
1260 or not. So we rely on another heuristic which is to check
1261 the instruction at the return address and see if this is
1262 an "unimp" instruction. If it is, then it is a struct-return
1263 function. */
1264 CORE_ADDR pc;
1265 int regnum =
1266 (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
1268 pc = get_frame_register_unsigned (this_frame, regnum) + 8;
1269 if (sparc_is_unimp_insn (pc))
1270 cache->struct_return_p = 1;
1273 return cache;
1276 static void
1277 sparc32_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
1278 struct frame_id *this_id)
1280 struct sparc_frame_cache *cache =
1281 sparc32_frame_cache (this_frame, this_cache);
1283 /* This marks the outermost frame. */
1284 if (cache->base == 0)
1285 return;
1287 (*this_id) = frame_id_build (cache->base, cache->pc);
1290 static struct value *
1291 sparc32_frame_prev_register (const frame_info_ptr &this_frame,
1292 void **this_cache, int regnum)
1294 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1295 struct sparc_frame_cache *cache =
1296 sparc32_frame_cache (this_frame, this_cache);
1298 if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
1300 CORE_ADDR pc = (regnum == SPARC32_NPC_REGNUM) ? 4 : 0;
1302 /* If this functions has a Structure, Union or Quad-Precision
1303 return value, we have to skip the UNIMP instruction that encodes
1304 the size of the structure. */
1305 if (cache->struct_return_p)
1306 pc += 4;
1308 regnum =
1309 (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
1310 pc += get_frame_register_unsigned (this_frame, regnum) + 8;
1311 return frame_unwind_got_constant (this_frame, regnum, pc);
1314 /* Handle StackGhost. */
1316 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1318 if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
1320 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
1321 ULONGEST i7;
1323 /* Read the value in from memory. */
1324 i7 = get_frame_memory_unsigned (this_frame, addr, 4);
1325 return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
1329 /* The previous frame's `local' and `in' registers may have been saved
1330 in the register save area. */
1331 if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
1332 && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
1334 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
1336 return frame_unwind_got_memory (this_frame, regnum, addr);
1339 /* The previous frame's `out' registers may be accessible as the current
1340 frame's `in' registers. */
1341 if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
1342 && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
1343 regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
1345 return frame_unwind_got_register (this_frame, regnum, regnum);
1348 static const struct frame_unwind sparc32_frame_unwind =
1350 "sparc32 prologue",
1351 NORMAL_FRAME,
1352 default_frame_unwind_stop_reason,
1353 sparc32_frame_this_id,
1354 sparc32_frame_prev_register,
1355 NULL,
1356 default_frame_sniffer
1360 static CORE_ADDR
1361 sparc32_frame_base_address (const frame_info_ptr &this_frame, void **this_cache)
1363 struct sparc_frame_cache *cache =
1364 sparc32_frame_cache (this_frame, this_cache);
1366 return cache->base;
1369 static const struct frame_base sparc32_frame_base =
1371 &sparc32_frame_unwind,
1372 sparc32_frame_base_address,
1373 sparc32_frame_base_address,
1374 sparc32_frame_base_address
1377 static struct frame_id
1378 sparc_dummy_id (struct gdbarch *gdbarch, const frame_info_ptr &this_frame)
1380 CORE_ADDR sp;
1382 sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
1383 if (sp & 1)
1384 sp += BIAS;
1385 return frame_id_build (sp, get_frame_pc (this_frame));
1389 /* Extract a function return value of TYPE from REGCACHE, and copy
1390 that into VALBUF. */
1392 static void
1393 sparc32_extract_return_value (struct type *type, struct regcache *regcache,
1394 gdb_byte *valbuf)
1396 int len = type->length ();
1397 gdb_byte buf[32];
1399 gdb_assert (!sparc_structure_return_p (type));
1401 if (sparc_floating_p (type) || sparc_complex_floating_p (type)
1402 || type->code () == TYPE_CODE_ARRAY)
1404 /* Floating return values. */
1405 regcache->cooked_read (SPARC_F0_REGNUM, buf);
1406 if (len > 4)
1407 regcache->cooked_read (SPARC_F1_REGNUM, buf + 4);
1408 if (len > 8)
1410 regcache->cooked_read (SPARC_F2_REGNUM, buf + 8);
1411 regcache->cooked_read (SPARC_F3_REGNUM, buf + 12);
1413 if (len > 16)
1415 regcache->cooked_read (SPARC_F4_REGNUM, buf + 16);
1416 regcache->cooked_read (SPARC_F5_REGNUM, buf + 20);
1417 regcache->cooked_read (SPARC_F6_REGNUM, buf + 24);
1418 regcache->cooked_read (SPARC_F7_REGNUM, buf + 28);
1420 memcpy (valbuf, buf, len);
1422 else
1424 /* Integral and pointer return values. */
1425 gdb_assert (sparc_integral_or_pointer_p (type));
1427 regcache->cooked_read (SPARC_O0_REGNUM, buf);
1428 if (len > 4)
1430 regcache->cooked_read (SPARC_O1_REGNUM, buf + 4);
1431 gdb_assert (len == 8);
1432 memcpy (valbuf, buf, 8);
1434 else
1436 /* Just stripping off any unused bytes should preserve the
1437 signed-ness just fine. */
1438 memcpy (valbuf, buf + 4 - len, len);
1443 /* Store the function return value of type TYPE from VALBUF into
1444 REGCACHE. */
1446 static void
1447 sparc32_store_return_value (struct type *type, struct regcache *regcache,
1448 const gdb_byte *valbuf)
1450 int len = type->length ();
1451 gdb_byte buf[32];
1453 gdb_assert (!sparc_structure_return_p (type));
1455 if (sparc_floating_p (type) || sparc_complex_floating_p (type))
1457 /* Floating return values. */
1458 memcpy (buf, valbuf, len);
1459 regcache->cooked_write (SPARC_F0_REGNUM, buf);
1460 if (len > 4)
1461 regcache->cooked_write (SPARC_F1_REGNUM, buf + 4);
1462 if (len > 8)
1464 regcache->cooked_write (SPARC_F2_REGNUM, buf + 8);
1465 regcache->cooked_write (SPARC_F3_REGNUM, buf + 12);
1467 if (len > 16)
1469 regcache->cooked_write (SPARC_F4_REGNUM, buf + 16);
1470 regcache->cooked_write (SPARC_F5_REGNUM, buf + 20);
1471 regcache->cooked_write (SPARC_F6_REGNUM, buf + 24);
1472 regcache->cooked_write (SPARC_F7_REGNUM, buf + 28);
1475 else
1477 /* Integral and pointer return values. */
1478 gdb_assert (sparc_integral_or_pointer_p (type));
1480 if (len > 4)
1482 gdb_assert (len == 8);
1483 memcpy (buf, valbuf, 8);
1484 regcache->cooked_write (SPARC_O1_REGNUM, buf + 4);
1486 else
1488 /* ??? Do we need to do any sign-extension here? */
1489 memcpy (buf + 4 - len, valbuf, len);
1491 regcache->cooked_write (SPARC_O0_REGNUM, buf);
1495 static enum return_value_convention
1496 sparc32_return_value (struct gdbarch *gdbarch, struct value *function,
1497 struct type *type, struct regcache *regcache,
1498 struct value **read_value, const gdb_byte *writebuf)
1500 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1502 /* The psABI says that "...every stack frame reserves the word at
1503 %fp+64. If a function returns a structure, union, or
1504 quad-precision value, this word should hold the address of the
1505 object into which the return value should be copied." This
1506 guarantees that we can always find the return value, not just
1507 before the function returns. */
1509 if (sparc_structure_return_p (type))
1511 ULONGEST sp;
1512 CORE_ADDR addr;
1514 if (read_value != nullptr)
1516 regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1517 addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
1518 *read_value = value_at_non_lval (type, addr);
1520 if (writebuf)
1522 regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1523 addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
1524 write_memory (addr, writebuf, type->length ());
1527 return RETURN_VALUE_ABI_PRESERVES_ADDRESS;
1530 if (read_value != nullptr)
1532 *read_value = value::allocate (type);
1533 gdb_byte *readbuf = (*read_value)->contents_raw ().data ();
1534 sparc32_extract_return_value (type, regcache, readbuf);
1536 if (writebuf)
1537 sparc32_store_return_value (type, regcache, writebuf);
1539 return RETURN_VALUE_REGISTER_CONVENTION;
1542 static int
1543 sparc32_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
1545 return (sparc_structure_or_union_p (type)
1546 || (sparc_floating_p (type) && type->length () == 16)
1547 || sparc_complex_floating_p (type));
1550 static int
1551 sparc32_dwarf2_struct_return_p (const frame_info_ptr &this_frame)
1553 CORE_ADDR pc = get_frame_address_in_block (this_frame);
1554 struct symbol *sym = find_pc_function (pc);
1556 if (sym)
1557 return sparc32_struct_return_from_sym (sym);
1558 return 0;
1561 static void
1562 sparc32_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1563 struct dwarf2_frame_state_reg *reg,
1564 const frame_info_ptr &this_frame)
1566 int off;
1568 switch (regnum)
1570 case SPARC_G0_REGNUM:
1571 /* Since %g0 is always zero, there is no point in saving it, and
1572 people will be inclined omit it from the CFI. Make sure we
1573 don't warn about that. */
1574 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1575 break;
1576 case SPARC_SP_REGNUM:
1577 reg->how = DWARF2_FRAME_REG_CFA;
1578 break;
1579 case SPARC32_PC_REGNUM:
1580 case SPARC32_NPC_REGNUM:
1581 reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1582 off = 8;
1583 if (sparc32_dwarf2_struct_return_p (this_frame))
1584 off += 4;
1585 if (regnum == SPARC32_NPC_REGNUM)
1586 off += 4;
1587 reg->loc.offset = off;
1588 break;
1592 /* Implement the execute_dwarf_cfa_vendor_op method. */
1594 static bool
1595 sparc_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
1596 struct dwarf2_frame_state *fs)
1598 /* Only DW_CFA_GNU_window_save is expected on SPARC. */
1599 if (op != DW_CFA_GNU_window_save)
1600 return false;
1602 uint64_t reg;
1603 int size = register_size (gdbarch, 0);
1605 fs->regs.alloc_regs (32);
1606 for (reg = 8; reg < 16; reg++)
1608 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
1609 fs->regs.reg[reg].loc.reg = reg + 16;
1611 for (reg = 16; reg < 32; reg++)
1613 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
1614 fs->regs.reg[reg].loc.offset = (reg - 16) * size;
1617 return true;
1621 /* The SPARC Architecture doesn't have hardware single-step support,
1622 and most operating systems don't implement it either, so we provide
1623 software single-step mechanism. */
1625 static CORE_ADDR
1626 sparc_analyze_control_transfer (struct regcache *regcache,
1627 CORE_ADDR pc, CORE_ADDR *npc)
1629 unsigned long insn = sparc_fetch_instruction (pc);
1630 int conditional_p = X_COND (insn) & 0x7;
1631 int branch_p = 0, fused_p = 0;
1632 long offset = 0; /* Must be signed for sign-extend. */
1634 if (X_OP (insn) == 0 && X_OP2 (insn) == 3)
1636 if ((insn & 0x10000000) == 0)
1638 /* Branch on Integer Register with Prediction (BPr). */
1639 branch_p = 1;
1640 conditional_p = 1;
1642 else
1644 /* Compare and Branch */
1645 branch_p = 1;
1646 fused_p = 1;
1647 offset = 4 * X_DISP10 (insn);
1650 else if (X_OP (insn) == 0 && X_OP2 (insn) == 6)
1652 /* Branch on Floating-Point Condition Codes (FBfcc). */
1653 branch_p = 1;
1654 offset = 4 * X_DISP22 (insn);
1656 else if (X_OP (insn) == 0 && X_OP2 (insn) == 5)
1658 /* Branch on Floating-Point Condition Codes with Prediction
1659 (FBPfcc). */
1660 branch_p = 1;
1661 offset = 4 * X_DISP19 (insn);
1663 else if (X_OP (insn) == 0 && X_OP2 (insn) == 2)
1665 /* Branch on Integer Condition Codes (Bicc). */
1666 branch_p = 1;
1667 offset = 4 * X_DISP22 (insn);
1669 else if (X_OP (insn) == 0 && X_OP2 (insn) == 1)
1671 /* Branch on Integer Condition Codes with Prediction (BPcc). */
1672 branch_p = 1;
1673 offset = 4 * X_DISP19 (insn);
1675 else if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3a)
1677 frame_info_ptr frame = get_current_frame ();
1679 /* Trap instruction (TRAP). */
1680 gdbarch *arch = regcache->arch ();
1681 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (arch);
1682 return tdep->step_trap (frame, insn);
1685 /* FIXME: Handle DONE and RETRY instructions. */
1687 if (branch_p)
1689 if (fused_p)
1691 /* Fused compare-and-branch instructions are non-delayed,
1692 and do not have an annulling capability. So we need to
1693 always set a breakpoint on both the NPC and the branch
1694 target address. */
1695 gdb_assert (offset != 0);
1696 return pc + offset;
1698 else if (conditional_p)
1700 /* For conditional branches, return nPC + 4 iff the annul
1701 bit is 1. */
1702 return (X_A (insn) ? *npc + 4 : 0);
1704 else
1706 /* For unconditional branches, return the target if its
1707 specified condition is "always" and return nPC + 4 if the
1708 condition is "never". If the annul bit is 1, set *NPC to
1709 zero. */
1710 if (X_COND (insn) == 0x0)
1711 pc = *npc, offset = 4;
1712 if (X_A (insn))
1713 *npc = 0;
1715 return pc + offset;
1719 return 0;
1722 static CORE_ADDR
1723 sparc_step_trap (const frame_info_ptr &frame, unsigned long insn)
1725 return 0;
1728 static std::vector<CORE_ADDR>
1729 sparc_software_single_step (struct regcache *regcache)
1731 struct gdbarch *arch = regcache->arch ();
1732 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (arch);
1733 CORE_ADDR npc, nnpc;
1735 CORE_ADDR pc, orig_npc;
1736 std::vector<CORE_ADDR> next_pcs;
1738 pc = regcache_raw_get_unsigned (regcache, tdep->pc_regnum);
1739 orig_npc = npc = regcache_raw_get_unsigned (regcache, tdep->npc_regnum);
1741 /* Analyze the instruction at PC. */
1742 nnpc = sparc_analyze_control_transfer (regcache, pc, &npc);
1743 if (npc != 0)
1744 next_pcs.push_back (npc);
1746 if (nnpc != 0)
1747 next_pcs.push_back (nnpc);
1749 /* Assert that we have set at least one breakpoint, and that
1750 they're not set at the same spot - unless we're going
1751 from here straight to NULL, i.e. a call or jump to 0. */
1752 gdb_assert (npc != 0 || nnpc != 0 || orig_npc == 0);
1753 gdb_assert (nnpc != npc || orig_npc == 0);
1755 return next_pcs;
1758 static void
1759 sparc_write_pc (struct regcache *regcache, CORE_ADDR pc)
1761 gdbarch *arch = regcache->arch ();
1762 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (arch);
1764 regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);
1765 regcache_cooked_write_unsigned (regcache, tdep->npc_regnum, pc + 4);
1769 /* Iterate over core file register note sections. */
1771 static void
1772 sparc_iterate_over_regset_sections (struct gdbarch *gdbarch,
1773 iterate_over_regset_sections_cb *cb,
1774 void *cb_data,
1775 const struct regcache *regcache)
1777 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
1779 cb (".reg", tdep->sizeof_gregset, tdep->sizeof_gregset, tdep->gregset, NULL,
1780 cb_data);
1781 cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, tdep->fpregset,
1782 NULL, cb_data);
1786 static int
1787 validate_tdesc_registers (const struct target_desc *tdesc,
1788 struct tdesc_arch_data *tdesc_data,
1789 const char *feature_name,
1790 const char * const register_names[],
1791 unsigned int registers_num,
1792 unsigned int reg_start)
1794 int valid_p = 1;
1795 const struct tdesc_feature *feature;
1797 feature = tdesc_find_feature (tdesc, feature_name);
1798 if (feature == NULL)
1799 return 0;
1801 for (unsigned int i = 0; i < registers_num; i++)
1802 valid_p &= tdesc_numbered_register (feature, tdesc_data,
1803 reg_start + i,
1804 register_names[i]);
1806 return valid_p;
1809 static struct gdbarch *
1810 sparc32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1812 const struct target_desc *tdesc = info.target_desc;
1813 int valid_p = 1;
1815 /* If there is already a candidate, use it. */
1816 arches = gdbarch_list_lookup_by_info (arches, &info);
1817 if (arches != NULL)
1818 return arches->gdbarch;
1820 /* Allocate space for the new architecture. */
1821 gdbarch *gdbarch
1822 = gdbarch_alloc (&info, gdbarch_tdep_up (new sparc_gdbarch_tdep));
1823 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
1825 tdep->pc_regnum = SPARC32_PC_REGNUM;
1826 tdep->npc_regnum = SPARC32_NPC_REGNUM;
1827 tdep->step_trap = sparc_step_trap;
1828 tdep->fpu_register_names = sparc32_fpu_register_names;
1829 tdep->fpu_registers_num = ARRAY_SIZE (sparc32_fpu_register_names);
1830 tdep->cp0_register_names = sparc32_cp0_register_names;
1831 tdep->cp0_registers_num = ARRAY_SIZE (sparc32_cp0_register_names);
1833 set_gdbarch_long_double_bit (gdbarch, 128);
1834 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_quad);
1836 set_gdbarch_wchar_bit (gdbarch, 16);
1837 set_gdbarch_wchar_signed (gdbarch, 1);
1839 set_gdbarch_num_regs (gdbarch, SPARC32_NUM_REGS);
1840 set_gdbarch_register_name (gdbarch, sparc32_register_name);
1841 set_gdbarch_register_type (gdbarch, sparc32_register_type);
1842 set_gdbarch_num_pseudo_regs (gdbarch, SPARC32_NUM_PSEUDO_REGS);
1843 set_tdesc_pseudo_register_name (gdbarch, sparc32_pseudo_register_name);
1844 set_tdesc_pseudo_register_type (gdbarch, sparc32_pseudo_register_type);
1845 set_gdbarch_pseudo_register_read (gdbarch, sparc32_pseudo_register_read);
1846 set_gdbarch_deprecated_pseudo_register_write (gdbarch,
1847 sparc32_pseudo_register_write);
1849 /* Register numbers of various important registers. */
1850 set_gdbarch_sp_regnum (gdbarch, SPARC_SP_REGNUM); /* %sp */
1851 set_gdbarch_pc_regnum (gdbarch, SPARC32_PC_REGNUM); /* %pc */
1852 set_gdbarch_fp0_regnum (gdbarch, SPARC_F0_REGNUM); /* %f0 */
1854 /* Call dummy code. */
1855 set_gdbarch_frame_align (gdbarch, sparc32_frame_align);
1856 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1857 set_gdbarch_push_dummy_code (gdbarch, sparc32_push_dummy_code);
1858 set_gdbarch_push_dummy_call (gdbarch, sparc32_push_dummy_call);
1860 set_gdbarch_return_value_as_value (gdbarch, sparc32_return_value);
1861 set_gdbarch_stabs_argument_has_addr
1862 (gdbarch, sparc32_stabs_argument_has_addr);
1864 set_gdbarch_skip_prologue (gdbarch, sparc32_skip_prologue);
1866 /* Stack grows downward. */
1867 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1869 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
1870 sparc_breakpoint::kind_from_pc);
1871 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
1872 sparc_breakpoint::bp_from_kind);
1874 set_gdbarch_frame_args_skip (gdbarch, 8);
1876 set_gdbarch_software_single_step (gdbarch, sparc_software_single_step);
1877 set_gdbarch_write_pc (gdbarch, sparc_write_pc);
1879 set_gdbarch_dummy_id (gdbarch, sparc_dummy_id);
1881 frame_base_set_default (gdbarch, &sparc32_frame_base);
1883 /* Hook in the DWARF CFI frame unwinder. */
1884 dwarf2_frame_set_init_reg (gdbarch, sparc32_dwarf2_frame_init_reg);
1885 /* Register DWARF vendor CFI handler. */
1886 set_gdbarch_execute_dwarf_cfa_vendor_op (gdbarch,
1887 sparc_execute_dwarf_cfa_vendor_op);
1888 /* FIXME: kettenis/20050423: Don't enable the unwinder until the
1889 StackGhost issues have been resolved. */
1891 /* Hook in ABI-specific overrides, if they have been registered. */
1892 gdbarch_init_osabi (info, gdbarch);
1894 frame_unwind_append_unwinder (gdbarch, &sparc32_frame_unwind);
1896 if (tdesc_has_registers (tdesc))
1898 tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
1900 /* Validate that the descriptor provides the mandatory registers
1901 and allocate their numbers. */
1902 valid_p &= validate_tdesc_registers (tdesc, tdesc_data.get (),
1903 "org.gnu.gdb.sparc.cpu",
1904 sparc_core_register_names,
1905 ARRAY_SIZE (sparc_core_register_names),
1906 SPARC_G0_REGNUM);
1907 valid_p &= validate_tdesc_registers (tdesc, tdesc_data.get (),
1908 "org.gnu.gdb.sparc.fpu",
1909 tdep->fpu_register_names,
1910 tdep->fpu_registers_num,
1911 SPARC_F0_REGNUM);
1912 valid_p &= validate_tdesc_registers (tdesc, tdesc_data.get (),
1913 "org.gnu.gdb.sparc.cp0",
1914 tdep->cp0_register_names,
1915 tdep->cp0_registers_num,
1916 SPARC_F0_REGNUM
1917 + tdep->fpu_registers_num);
1918 if (!valid_p)
1919 return NULL;
1921 /* Target description may have changed. */
1922 info.tdesc_data = tdesc_data.get ();
1923 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
1926 /* If we have register sets, enable the generic core file support. */
1927 if (tdep->gregset)
1928 set_gdbarch_iterate_over_regset_sections
1929 (gdbarch, sparc_iterate_over_regset_sections);
1931 register_sparc_ravenscar_ops (gdbarch);
1933 return gdbarch;
1936 /* Helper functions for dealing with register windows. */
1938 void
1939 sparc_supply_rwindow (struct regcache *regcache, CORE_ADDR sp, int regnum)
1941 struct gdbarch *gdbarch = regcache->arch ();
1942 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1943 int offset = 0;
1944 gdb_byte buf[8];
1945 int i;
1947 /* This function calls functions that depend on the global current thread. */
1948 gdb_assert (regcache->ptid () == inferior_ptid);
1950 if (sp & 1)
1952 /* Registers are 64-bit. */
1953 sp += BIAS;
1955 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1957 if (regnum == i || regnum == -1)
1959 target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
1961 /* Handle StackGhost. */
1962 if (i == SPARC_I7_REGNUM)
1964 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1965 ULONGEST i7;
1967 i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
1968 store_unsigned_integer (buf + offset, 8, byte_order,
1969 i7 ^ wcookie);
1972 regcache->raw_supply (i, buf);
1976 else
1978 /* Registers are 32-bit. Toss any sign-extension of the stack
1979 pointer. */
1980 sp &= 0xffffffffUL;
1982 /* Clear out the top half of the temporary buffer, and put the
1983 register value in the bottom half if we're in 64-bit mode. */
1984 if (gdbarch_ptr_bit (regcache->arch ()) == 64)
1986 memset (buf, 0, 4);
1987 offset = 4;
1990 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1992 if (regnum == i || regnum == -1)
1994 target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
1995 buf + offset, 4);
1997 /* Handle StackGhost. */
1998 if (i == SPARC_I7_REGNUM)
2000 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
2001 ULONGEST i7;
2003 i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
2004 store_unsigned_integer (buf + offset, 4, byte_order,
2005 i7 ^ wcookie);
2008 regcache->raw_supply (i, buf);
2014 void
2015 sparc_collect_rwindow (const struct regcache *regcache,
2016 CORE_ADDR sp, int regnum)
2018 struct gdbarch *gdbarch = regcache->arch ();
2019 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2020 int offset = 0;
2021 gdb_byte buf[8];
2022 int i;
2024 /* This function calls functions that depend on the global current thread. */
2025 gdb_assert (regcache->ptid () == inferior_ptid);
2027 if (sp & 1)
2029 /* Registers are 64-bit. */
2030 sp += BIAS;
2032 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
2034 if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
2036 regcache->raw_collect (i, buf);
2038 /* Handle StackGhost. */
2039 if (i == SPARC_I7_REGNUM)
2041 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
2042 ULONGEST i7;
2044 i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
2045 store_unsigned_integer (buf, 8, byte_order, i7 ^ wcookie);
2048 target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
2052 else
2054 /* Registers are 32-bit. Toss any sign-extension of the stack
2055 pointer. */
2056 sp &= 0xffffffffUL;
2058 /* Only use the bottom half if we're in 64-bit mode. */
2059 if (gdbarch_ptr_bit (regcache->arch ()) == 64)
2060 offset = 4;
2062 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
2064 if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
2066 regcache->raw_collect (i, buf);
2068 /* Handle StackGhost. */
2069 if (i == SPARC_I7_REGNUM)
2071 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
2072 ULONGEST i7;
2074 i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
2075 store_unsigned_integer (buf + offset, 4, byte_order,
2076 i7 ^ wcookie);
2079 target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
2080 buf + offset, 4);
2086 /* Helper functions for dealing with register sets. */
2088 void
2089 sparc32_supply_gregset (const struct sparc_gregmap *gregmap,
2090 struct regcache *regcache,
2091 int regnum, const void *gregs)
2093 const gdb_byte *regs = (const gdb_byte *) gregs;
2094 gdb_byte zero[4] = { 0 };
2095 int i;
2097 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
2098 regcache->raw_supply (SPARC32_PSR_REGNUM, regs + gregmap->r_psr_offset);
2100 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
2101 regcache->raw_supply (SPARC32_PC_REGNUM, regs + gregmap->r_pc_offset);
2103 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
2104 regcache->raw_supply (SPARC32_NPC_REGNUM, regs + gregmap->r_npc_offset);
2106 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
2107 regcache->raw_supply (SPARC32_Y_REGNUM, regs + gregmap->r_y_offset);
2109 if (regnum == SPARC_G0_REGNUM || regnum == -1)
2110 regcache->raw_supply (SPARC_G0_REGNUM, &zero);
2112 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
2114 int offset = gregmap->r_g1_offset;
2116 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
2118 if (regnum == i || regnum == -1)
2119 regcache->raw_supply (i, regs + offset);
2120 offset += 4;
2124 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
2126 /* Not all of the register set variants include Locals and
2127 Inputs. For those that don't, we read them off the stack. */
2128 if (gregmap->r_l0_offset == -1)
2130 ULONGEST sp;
2132 regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
2133 sparc_supply_rwindow (regcache, sp, regnum);
2135 else
2137 int offset = gregmap->r_l0_offset;
2139 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
2141 if (regnum == i || regnum == -1)
2142 regcache->raw_supply (i, regs + offset);
2143 offset += 4;
2149 void
2150 sparc32_collect_gregset (const struct sparc_gregmap *gregmap,
2151 const struct regcache *regcache,
2152 int regnum, void *gregs)
2154 gdb_byte *regs = (gdb_byte *) gregs;
2155 int i;
2157 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
2158 regcache->raw_collect (SPARC32_PSR_REGNUM, regs + gregmap->r_psr_offset);
2160 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
2161 regcache->raw_collect (SPARC32_PC_REGNUM, regs + gregmap->r_pc_offset);
2163 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
2164 regcache->raw_collect (SPARC32_NPC_REGNUM, regs + gregmap->r_npc_offset);
2166 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
2167 regcache->raw_collect (SPARC32_Y_REGNUM, regs + gregmap->r_y_offset);
2169 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
2171 int offset = gregmap->r_g1_offset;
2173 /* %g0 is always zero. */
2174 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
2176 if (regnum == i || regnum == -1)
2177 regcache->raw_collect (i, regs + offset);
2178 offset += 4;
2182 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
2184 /* Not all of the register set variants include Locals and
2185 Inputs. For those that don't, we read them off the stack. */
2186 if (gregmap->r_l0_offset != -1)
2188 int offset = gregmap->r_l0_offset;
2190 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
2192 if (regnum == i || regnum == -1)
2193 regcache->raw_collect (i, regs + offset);
2194 offset += 4;
2200 void
2201 sparc32_supply_fpregset (const struct sparc_fpregmap *fpregmap,
2202 struct regcache *regcache,
2203 int regnum, const void *fpregs)
2205 const gdb_byte *regs = (const gdb_byte *) fpregs;
2206 int i;
2208 for (i = 0; i < 32; i++)
2210 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
2211 regcache->raw_supply (SPARC_F0_REGNUM + i,
2212 regs + fpregmap->r_f0_offset + (i * 4));
2215 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
2216 regcache->raw_supply (SPARC32_FSR_REGNUM, regs + fpregmap->r_fsr_offset);
2219 void
2220 sparc32_collect_fpregset (const struct sparc_fpregmap *fpregmap,
2221 const struct regcache *regcache,
2222 int regnum, void *fpregs)
2224 gdb_byte *regs = (gdb_byte *) fpregs;
2225 int i;
2227 for (i = 0; i < 32; i++)
2229 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
2230 regcache->raw_collect (SPARC_F0_REGNUM + i,
2231 regs + fpregmap->r_f0_offset + (i * 4));
2234 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
2235 regcache->raw_collect (SPARC32_FSR_REGNUM,
2236 regs + fpregmap->r_fsr_offset);
2240 /* SunOS 4. */
2242 /* From <machine/reg.h>. */
2243 const struct sparc_gregmap sparc32_sunos4_gregmap =
2245 0 * 4, /* %psr */
2246 1 * 4, /* %pc */
2247 2 * 4, /* %npc */
2248 3 * 4, /* %y */
2249 -1, /* %wim */
2250 -1, /* %tbr */
2251 4 * 4, /* %g1 */
2252 -1 /* %l0 */
2255 const struct sparc_fpregmap sparc32_sunos4_fpregmap =
2257 0 * 4, /* %f0 */
2258 33 * 4, /* %fsr */
2261 const struct sparc_fpregmap sparc32_bsd_fpregmap =
2263 0 * 4, /* %f0 */
2264 32 * 4, /* %fsr */
2267 void _initialize_sparc_tdep ();
2268 void
2269 _initialize_sparc_tdep ()
2271 gdbarch_register (bfd_arch_sparc, sparc32_gdbarch_init);