xref: /openbsd/gnu/usr.bin/binutils/gdb/sparc-tdep.c (revision 5e948f2a)
1 /* Target-dependent code for SPARC.
2 
3    Copyright 2003, 2004, 2005 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 2 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, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21 
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include "dis-asm.h"
25 #include "floatformat.h"
26 #include "frame.h"
27 #include "frame-base.h"
28 #include "frame-unwind.h"
29 #include "gdbcore.h"
30 #include "gdbtypes.h"
31 #include "inferior.h"
32 #include "symtab.h"
33 #include "objfiles.h"
34 #include "osabi.h"
35 #include "regcache.h"
36 #include "target.h"
37 #include "value.h"
38 
39 #include "gdb_assert.h"
40 #include "gdb_string.h"
41 
42 #include "sparc-tdep.h"
43 
44 struct regset;
45 
46 /* This file implements the SPARC 32-bit ABI as defined by the section
47    "Low-Level System Information" of the SPARC Compliance Definition
48    (SCD) 2.4.1, which is the 32-bit System V psABI for SPARC.  The SCD
49    lists changes with respect to the original 32-bit psABI as defined
50    in the "System V ABI, SPARC Processor Supplement".
51 
52    Note that if we talk about SunOS, we mean SunOS 4.x, which was
53    BSD-based, which is sometimes (retroactively?) referred to as
54    Solaris 1.x.  If we talk about Solaris we mean Solaris 2.x and
55    above (Solaris 7, 8 and 9 are nothing but Solaris 2.7, 2.8 and 2.9
56    suffering from severe version number inflation).  Solaris 2.x is
57    also known as SunOS 5.x, since that's what uname(1) says.  Solaris
58    2.x is SVR4-based.  */
59 
60 /* Please use the sparc32_-prefix for 32-bit specific code, the
61    sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
62    code that can handle both.  The 64-bit specific code lives in
63    sparc64-tdep.c; don't add any here.  */
64 
65 /* The SPARC Floating-Point Quad-Precision format is similar to
66    big-endian IA-64 Quad-recision format.  */
67 #define floatformat_sparc_quad floatformat_ia64_quad_big
68 
69 /* The stack pointer is offset from the stack frame by a BIAS of 2047
70    (0x7ff) for 64-bit code.  BIAS is likely to be defined on SPARC
71    hosts, so undefine it first.  */
72 #undef BIAS
73 #define BIAS 2047
74 
75 /* Macros to extract fields from SPARC instructions.  */
76 #define X_OP(i) (((i) >> 30) & 0x3)
77 #define X_RD(i) (((i) >> 25) & 0x1f)
78 #define X_A(i) (((i) >> 29) & 1)
79 #define X_COND(i) (((i) >> 25) & 0xf)
80 #define X_OP2(i) (((i) >> 22) & 0x7)
81 #define X_IMM22(i) ((i) & 0x3fffff)
82 #define X_OP3(i) (((i) >> 19) & 0x3f)
83 #define X_RS1(i) (((i) >> 14) & 0x1f)
84 #define X_RS2(i) ((i) & 0x1f)
85 #define X_I(i) (((i) >> 13) & 1)
86 /* Sign extension macros.  */
87 #define X_DISP22(i) ((X_IMM22 (i) ^ 0x200000) - 0x200000)
88 #define X_DISP19(i) ((((i) & 0x7ffff) ^ 0x40000) - 0x40000)
89 
90 /* Fetch the instruction at PC.  Instructions are always big-endian
91    even if the processor operates in little-endian mode.  */
92 
93 unsigned long
sparc_fetch_instruction(CORE_ADDR pc)94 sparc_fetch_instruction (CORE_ADDR pc)
95 {
96   unsigned char buf[4];
97   unsigned long insn;
98   int i;
99 
100   /* If we can't read the instruction at PC, return zero.  */
101   if (target_read_memory (pc, buf, sizeof (buf)))
102     return 0;
103 
104   insn = 0;
105   for (i = 0; i < sizeof (buf); i++)
106     insn = (insn << 8) | buf[i];
107   return insn;
108 }
109 
110 
111 /* OpenBSD/sparc includes StackGhost, which according to the author's
112    website http://stackghost.cerias.purdue.edu "... transparently and
113    automatically protects applications' stack frames; more
114    specifically, it guards the return pointers.  The protection
115    mechanisms require no application source or binary modification and
116    imposes only a negligible performance penalty."
117 
118    The same website provides the following description of how
119    StackGhost works:
120 
121    "StackGhost interfaces with the kernel trap handler that would
122    normally write out registers to the stack and the handler that
123    would read them back in.  By XORing a cookie into the
124    return-address saved in the user stack when it is actually written
125    to the stack, and then XOR it out when the return-address is pulled
126    from the stack, StackGhost can cause attacker corrupted return
127    pointers to behave in a manner the attacker cannot predict.
128    StackGhost can also use several unused bits in the return pointer
129    to detect a smashed return pointer and abort the process."
130 
131    For GDB this means that whenever we're reading %i7 from a stack
132    frame's window save area, we'll have to XOR the cookie.
133 
134    More information on StackGuard can be found on in:
135 
136    Mike Frantzen and Mike Shuey. "StackGhost: Hardware Facilitated
137    Stack Protection."  2001.  Published in USENIX Security Symposium
138    '01.  */
139 
140 /* Fetch StackGhost Per-Process XOR cookie.  */
141 
142 ULONGEST
sparc_fetch_wcookie(void)143 sparc_fetch_wcookie (void)
144 {
145   struct target_ops *ops = &current_target;
146   char buf[8];
147   int len;
148 
149   len = target_read_partial (ops, TARGET_OBJECT_WCOOKIE, NULL, buf, 0, 8);
150   if (len == -1)
151     return 0;
152 
153   /* We should have either an 32-bit or an 64-bit cookie.  */
154   gdb_assert (len == 4 || len == 8);
155 
156   return extract_unsigned_integer (buf, len);
157 }
158 
159 
160 /* Return the contents if register REGNUM as an address.  */
161 
162 static CORE_ADDR
sparc_address_from_register(int regnum)163 sparc_address_from_register (int regnum)
164 {
165   ULONGEST addr;
166 
167   regcache_cooked_read_unsigned (current_regcache, regnum, &addr);
168   return addr;
169 }
170 
171 
172 /* The functions on this page are intended to be used to classify
173    function arguments.  */
174 
175 /* Check whether TYPE is "Integral or Pointer".  */
176 
177 static int
sparc_integral_or_pointer_p(const struct type * type)178 sparc_integral_or_pointer_p (const struct type *type)
179 {
180   switch (TYPE_CODE (type))
181     {
182     case TYPE_CODE_INT:
183     case TYPE_CODE_BOOL:
184     case TYPE_CODE_CHAR:
185     case TYPE_CODE_ENUM:
186     case TYPE_CODE_RANGE:
187       {
188 	/* We have byte, half-word, word and extended-word/doubleword
189            integral types.  The doubleword is an extension to the
190            original 32-bit ABI by the SCD 2.4.x.  */
191 	int len = TYPE_LENGTH (type);
192 	return (len == 1 || len == 2 || len == 4 || len == 8);
193       }
194       return 1;
195     case TYPE_CODE_PTR:
196     case TYPE_CODE_REF:
197       {
198 	/* Allow either 32-bit or 64-bit pointers.  */
199 	int len = TYPE_LENGTH (type);
200 	return (len == 4 || len == 8);
201       }
202       return 1;
203     default:
204       break;
205     }
206 
207   return 0;
208 }
209 
210 /* Check whether TYPE is "Floating".  */
211 
212 static int
sparc_floating_p(const struct type * type)213 sparc_floating_p (const struct type *type)
214 {
215   switch (TYPE_CODE (type))
216     {
217     case TYPE_CODE_FLT:
218       {
219 	int len = TYPE_LENGTH (type);
220 	return (len == 4 || len == 8 || len == 16);
221       }
222     default:
223       break;
224     }
225 
226   return 0;
227 }
228 
229 /* Check whether TYPE is "Structure or Union".  */
230 
231 static int
sparc_structure_or_union_p(const struct type * type)232 sparc_structure_or_union_p (const struct type *type)
233 {
234   switch (TYPE_CODE (type))
235     {
236     case TYPE_CODE_STRUCT:
237     case TYPE_CODE_UNION:
238       return 1;
239     default:
240       break;
241     }
242 
243   return 0;
244 }
245 
246 /* Register information.  */
247 
248 static const char *sparc32_register_names[] =
249 {
250   "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
251   "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
252   "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
253   "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",
254 
255   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
256   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
257   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
258   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
259 
260   "y", "psr", "wim", "tbr", "pc", "npc", "fsr", "csr"
261 };
262 
263 /* Total number of registers.  */
264 #define SPARC32_NUM_REGS ARRAY_SIZE (sparc32_register_names)
265 
266 /* We provide the aliases %d0..%d30 for the floating registers as
267    "psuedo" registers.  */
268 
269 static const char *sparc32_pseudo_register_names[] =
270 {
271   "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
272   "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30"
273 };
274 
275 /* Total number of pseudo registers.  */
276 #define SPARC32_NUM_PSEUDO_REGS ARRAY_SIZE (sparc32_pseudo_register_names)
277 
278 /* Return the name of register REGNUM.  */
279 
280 static const char *
sparc32_register_name(int regnum)281 sparc32_register_name (int regnum)
282 {
283   if (regnum >= 0 && regnum < SPARC32_NUM_REGS)
284     return sparc32_register_names[regnum];
285 
286   if (regnum < SPARC32_NUM_REGS + SPARC32_NUM_PSEUDO_REGS)
287     return sparc32_pseudo_register_names[regnum - SPARC32_NUM_REGS];
288 
289   return NULL;
290 }
291 
292 /* Return the GDB type object for the "standard" data type of data in
293    register REGNUM. */
294 
295 static struct type *
sparc32_register_type(struct gdbarch * gdbarch,int regnum)296 sparc32_register_type (struct gdbarch *gdbarch, int regnum)
297 {
298   if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
299     return builtin_type_float;
300 
301   if (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM)
302     return builtin_type_double;
303 
304   if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
305     return builtin_type_void_data_ptr;
306 
307   if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
308     return builtin_type_void_func_ptr;
309 
310   return builtin_type_int32;
311 }
312 
313 static void
sparc32_pseudo_register_read(struct gdbarch * gdbarch,struct regcache * regcache,int regnum,void * buf)314 sparc32_pseudo_register_read (struct gdbarch *gdbarch,
315 			      struct regcache *regcache,
316 			      int regnum, void *buf)
317 {
318   gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
319 
320   regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
321   regcache_raw_read (regcache, regnum, buf);
322   regcache_raw_read (regcache, regnum + 1, ((char *)buf) + 4);
323 }
324 
325 static void
sparc32_pseudo_register_write(struct gdbarch * gdbarch,struct regcache * regcache,int regnum,const void * buf)326 sparc32_pseudo_register_write (struct gdbarch *gdbarch,
327 			       struct regcache *regcache,
328 			       int regnum, const void *buf)
329 {
330   gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
331 
332   regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
333   regcache_raw_write (regcache, regnum, buf);
334   regcache_raw_write (regcache, regnum + 1, ((const char *)buf) + 4);
335 }
336 
337 
338 static CORE_ADDR
sparc32_push_dummy_code(struct gdbarch * gdbarch,CORE_ADDR sp,CORE_ADDR funcaddr,int using_gcc,struct value ** args,int nargs,struct type * value_type,CORE_ADDR * real_pc,CORE_ADDR * bp_addr)339 sparc32_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
340 			 CORE_ADDR funcaddr, int using_gcc,
341 			 struct value **args, int nargs,
342 			 struct type *value_type,
343 			 CORE_ADDR *real_pc, CORE_ADDR *bp_addr)
344 {
345   *bp_addr = sp - 4;
346   *real_pc = funcaddr;
347 
348   if (using_struct_return (value_type, using_gcc))
349     {
350       char buf[4];
351 
352       /* This is an UNIMP instruction.  */
353       store_unsigned_integer (buf, 4, TYPE_LENGTH (value_type) & 0x1fff);
354       write_memory (sp - 8, buf, 4);
355       return sp - 8;
356     }
357 
358   return sp - 4;
359 }
360 
361 static CORE_ADDR
sparc32_store_arguments(struct regcache * regcache,int nargs,struct value ** args,CORE_ADDR sp,int struct_return,CORE_ADDR struct_addr)362 sparc32_store_arguments (struct regcache *regcache, int nargs,
363 			 struct value **args, CORE_ADDR sp,
364 			 int struct_return, CORE_ADDR struct_addr)
365 {
366   /* Number of words in the "parameter array".  */
367   int num_elements = 0;
368   int element = 0;
369   int i;
370 
371   for (i = 0; i < nargs; i++)
372     {
373       struct type *type = VALUE_TYPE (args[i]);
374       int len = TYPE_LENGTH (type);
375 
376       if (sparc_structure_or_union_p (type)
377 	  || (sparc_floating_p (type) && len == 16))
378 	{
379 	  /* Structure, Union and Quad-Precision Arguments.  */
380 	  sp -= len;
381 
382 	  /* Use doubleword alignment for these values.  That's always
383              correct, and wasting a few bytes shouldn't be a problem.  */
384 	  sp &= ~0x7;
385 
386 	  write_memory (sp, VALUE_CONTENTS (args[i]), len);
387 	  args[i] = value_from_pointer (lookup_pointer_type (type), sp);
388 	  num_elements++;
389 	}
390       else if (sparc_floating_p (type))
391 	{
392 	  /* Floating arguments.  */
393 	  gdb_assert (len == 4 || len == 8);
394 	  num_elements += (len / 4);
395 	}
396       else
397 	{
398 	  /* Integral and pointer arguments.  */
399 	  gdb_assert (sparc_integral_or_pointer_p (type));
400 
401 	  if (len < 4)
402 	    args[i] = value_cast (builtin_type_int32, args[i]);
403 	  num_elements += ((len + 3) / 4);
404 	}
405     }
406 
407   /* Always allocate at least six words.  */
408   sp -= max (6, num_elements) * 4;
409 
410   /* The psABI says that "Software convention requires space for the
411      struct/union return value pointer, even if the word is unused."  */
412   sp -= 4;
413 
414   /* The psABI says that "Although software convention and the
415      operating system require every stack frame to be doubleword
416      aligned."  */
417   sp &= ~0x7;
418 
419   for (i = 0; i < nargs; i++)
420     {
421       char *valbuf = VALUE_CONTENTS (args[i]);
422       struct type *type = VALUE_TYPE (args[i]);
423       int len = TYPE_LENGTH (type);
424 
425       gdb_assert (len == 4 || len == 8);
426 
427       if (element < 6)
428 	{
429 	  int regnum = SPARC_O0_REGNUM + element;
430 
431 	  regcache_cooked_write (regcache, regnum, valbuf);
432 	  if (len > 4 && element < 5)
433 	    regcache_cooked_write (regcache, regnum + 1, valbuf + 4);
434 	}
435 
436       /* Always store the argument in memory.  */
437       write_memory (sp + 4 + element * 4, valbuf, len);
438       element += len / 4;
439     }
440 
441   gdb_assert (element == num_elements);
442 
443   if (struct_return)
444     {
445       char buf[4];
446 
447       store_unsigned_integer (buf, 4, struct_addr);
448       write_memory (sp, buf, 4);
449     }
450 
451   return sp;
452 }
453 
454 static CORE_ADDR
sparc32_push_dummy_call(struct gdbarch * gdbarch,struct value * function,struct regcache * regcache,CORE_ADDR bp_addr,int nargs,struct value ** args,CORE_ADDR sp,int struct_return,CORE_ADDR struct_addr)455 sparc32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
456 			 struct regcache *regcache, CORE_ADDR bp_addr,
457 			 int nargs, struct value **args, CORE_ADDR sp,
458 			 int struct_return, CORE_ADDR struct_addr)
459 {
460   CORE_ADDR call_pc = (struct_return ? (bp_addr - 12) : (bp_addr - 8));
461 
462   /* Set return address.  */
463   regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, call_pc);
464 
465   /* Set up function arguments.  */
466   sp = sparc32_store_arguments (regcache, nargs, args, sp,
467 				struct_return, struct_addr);
468 
469   /* Allocate the 16-word window save area.  */
470   sp -= 16 * 4;
471 
472   /* Stack should be doubleword aligned at this point.  */
473   gdb_assert (sp % 8 == 0);
474 
475   /* Finally, update the stack pointer.  */
476   regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
477 
478   return sp;
479 }
480 
481 
482 /* Use the program counter to determine the contents and size of a
483    breakpoint instruction.  Return a pointer to a string of bytes that
484    encode a breakpoint instruction, store the length of the string in
485    *LEN and optionally adjust *PC to point to the correct memory
486    location for inserting the breakpoint.  */
487 
488 static const unsigned char *
sparc_breakpoint_from_pc(CORE_ADDR * pc,int * len)489 sparc_breakpoint_from_pc (CORE_ADDR *pc, int *len)
490 {
491   static unsigned char break_insn[] = { 0x91, 0xd0, 0x20, 0x01 };
492 
493   *len = sizeof (break_insn);
494   return break_insn;
495 }
496 
497 
498 /* Allocate and initialize a frame cache.  */
499 
500 static struct sparc_frame_cache *
sparc_alloc_frame_cache(void)501 sparc_alloc_frame_cache (void)
502 {
503   struct sparc_frame_cache *cache;
504   int i;
505 
506   cache = FRAME_OBSTACK_ZALLOC (struct sparc_frame_cache);
507 
508   /* Base address.  */
509   cache->base = 0;
510   cache->pc = 0;
511 
512   /* Frameless until proven otherwise.  */
513   cache->frameless_p = 1;
514 
515   cache->struct_return_p = 0;
516 
517   return cache;
518 }
519 
520 CORE_ADDR
sparc_analyze_prologue(CORE_ADDR pc,CORE_ADDR current_pc,struct sparc_frame_cache * cache)521 sparc_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
522 			struct sparc_frame_cache *cache)
523 {
524   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
525   unsigned long insn;
526   int offset = 0;
527   int dest = -1;
528 
529   if (current_pc <= pc)
530     return current_pc;
531 
532   /* We have to handle to "Procedure Linkage Table" (PLT) special.  On
533      SPARC the linker usually defines a symbol (typically
534      _PROCEDURE_LINKAGE_TABLE_) at the start of the .plt section.
535      This symbol makes us end up here with PC pointing at the start of
536      the PLT and CURRENT_PC probably pointing at a PLT entry.  If we
537      would do our normal prologue analysis, we would probably conclude
538      that we've got a frame when in reality we don't, since the
539      dynamic linker patches up the first PLT with some code that
540      starts with a SAVE instruction.  Patch up PC such that it points
541      at the start of our PLT entry.  */
542   if (tdep->plt_entry_size > 0 && in_plt_section (current_pc, NULL))
543     pc = current_pc - ((current_pc - pc) % tdep->plt_entry_size);
544 
545   insn = sparc_fetch_instruction (pc);
546 
547   /* Recognize a SETHI insn and record its destination.  */
548   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x04)
549     {
550       dest = X_RD (insn);
551       offset += 4;
552 
553       insn = sparc_fetch_instruction (pc + 4);
554     }
555 
556   /* Allow for an arithmetic operation on DEST or %g1.  */
557   if (X_OP (insn) == 2 && X_I (insn)
558       && (X_RD (insn) == 1 || X_RD (insn) == dest))
559     {
560       offset += 4;
561 
562       insn = sparc_fetch_instruction (pc + 8);
563     }
564 
565   /* Check for the SAVE instruction that sets up the frame.  */
566   if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3c)
567     {
568       cache->frameless_p = 0;
569       return pc + offset + 4;
570     }
571 
572   return pc;
573 }
574 
575 static CORE_ADDR
sparc_unwind_pc(struct gdbarch * gdbarch,struct frame_info * next_frame)576 sparc_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
577 {
578   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
579   return frame_unwind_register_unsigned (next_frame, tdep->pc_regnum);
580 }
581 
582 /* Return PC of first real instruction of the function starting at
583    START_PC.  */
584 
585 static CORE_ADDR
sparc32_skip_prologue(CORE_ADDR start_pc)586 sparc32_skip_prologue (CORE_ADDR start_pc)
587 {
588   struct symtab_and_line sal;
589   CORE_ADDR func_start, func_end;
590   struct sparc_frame_cache cache;
591 
592   /* This is the preferred method, find the end of the prologue by
593      using the debugging information.  */
594   if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
595     {
596       sal = find_pc_line (func_start, 0);
597 
598       if (sal.end < func_end
599 	  && start_pc <= sal.end)
600 	return sal.end;
601     }
602 
603   return sparc_analyze_prologue (start_pc, 0xffffffffUL, &cache);
604 }
605 
606 /* Normal frames.  */
607 
608 struct sparc_frame_cache *
sparc_frame_cache(struct frame_info * next_frame,void ** this_cache)609 sparc_frame_cache (struct frame_info *next_frame, void **this_cache)
610 {
611   struct sparc_frame_cache *cache;
612 
613   if (*this_cache)
614     return *this_cache;
615 
616   cache = sparc_alloc_frame_cache ();
617   *this_cache = cache;
618 
619   cache->pc = frame_func_unwind (next_frame);
620   if (cache->pc != 0)
621     {
622       CORE_ADDR addr_in_block = frame_unwind_address_in_block (next_frame);
623       sparc_analyze_prologue (cache->pc, addr_in_block, cache);
624     }
625 
626   if (cache->frameless_p)
627     {
628       /* This function is frameless, so %fp (%i6) holds the frame
629          pointer for our calling frame.  Use %sp (%o6) as this frame's
630          base address.  */
631       cache->base =
632 	frame_unwind_register_unsigned (next_frame, SPARC_SP_REGNUM);
633     }
634   else
635     {
636       /* For normal frames, %fp (%i6) holds the frame pointer, the
637          base address for the current stack frame.  */
638       cache->base =
639 	frame_unwind_register_unsigned (next_frame, SPARC_FP_REGNUM);
640     }
641 
642   return cache;
643 }
644 
645 struct sparc_frame_cache *
sparc32_frame_cache(struct frame_info * next_frame,void ** this_cache)646 sparc32_frame_cache (struct frame_info *next_frame, void **this_cache)
647 {
648   struct sparc_frame_cache *cache;
649   struct symbol *sym;
650 
651   if (*this_cache)
652     return *this_cache;
653 
654   cache = sparc_frame_cache (next_frame, this_cache);
655 
656   sym = find_pc_function (cache->pc);
657   if (sym)
658     {
659       struct type *type = check_typedef (SYMBOL_TYPE (sym));
660       enum type_code code = TYPE_CODE (type);
661 
662       if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
663 	{
664 	  type = check_typedef (TYPE_TARGET_TYPE (type));
665 	  if (sparc_structure_or_union_p (type)
666 	      || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16))
667 	    cache->struct_return_p = 1;
668 	}
669     }
670 
671   return cache;
672 }
673 
674 static void
sparc32_frame_this_id(struct frame_info * next_frame,void ** this_cache,struct frame_id * this_id)675 sparc32_frame_this_id (struct frame_info *next_frame, void **this_cache,
676 		       struct frame_id *this_id)
677 {
678   struct sparc_frame_cache *cache =
679     sparc32_frame_cache (next_frame, this_cache);
680 
681   /* This marks the outermost frame.  */
682   if (cache->base == 0)
683     return;
684 
685   (*this_id) = frame_id_build (cache->base, cache->pc);
686 }
687 
688 static void
sparc32_frame_prev_register(struct frame_info * next_frame,void ** this_cache,int regnum,int * optimizedp,enum lval_type * lvalp,CORE_ADDR * addrp,int * realnump,void * valuep)689 sparc32_frame_prev_register (struct frame_info *next_frame, void **this_cache,
690 			     int regnum, int *optimizedp,
691 			     enum lval_type *lvalp, CORE_ADDR *addrp,
692 			     int *realnump, void *valuep)
693 {
694   struct sparc_frame_cache *cache =
695     sparc32_frame_cache (next_frame, this_cache);
696 
697   if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
698     {
699       *optimizedp = 0;
700       *lvalp = not_lval;
701       *addrp = 0;
702       *realnump = -1;
703       if (valuep)
704 	{
705 	  CORE_ADDR pc = (regnum == SPARC32_NPC_REGNUM) ? 4 : 0;
706 
707 	  /* If this functions has a Structure, Union or
708              Quad-Precision return value, we have to skip the UNIMP
709              instruction that encodes the size of the structure.  */
710 	  if (cache->struct_return_p)
711 	    pc += 4;
712 
713 	  regnum = cache->frameless_p ? SPARC_O7_REGNUM : SPARC_I7_REGNUM;
714 	  pc += frame_unwind_register_unsigned (next_frame, regnum) + 8;
715 	  store_unsigned_integer (valuep, 4, pc);
716 	}
717       return;
718     }
719 
720   /* Handle StackGhost.  */
721   {
722     ULONGEST wcookie = sparc_fetch_wcookie ();
723 
724     if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
725       {
726 	*optimizedp = 0;
727 	*lvalp = not_lval;
728 	*addrp = 0;
729 	*realnump = -1;
730 	if (valuep)
731 	  {
732 	    CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
733 	    ULONGEST i7;
734 
735 	    /* Read the value in from memory.  */
736 	    i7 = get_frame_memory_unsigned (next_frame, addr, 4);
737 	    store_unsigned_integer (valuep, 4, i7 ^ wcookie);
738 	  }
739 	return;
740       }
741   }
742 
743   /* The previous frame's `local' and `in' registers have been saved
744      in the register save area.  */
745   if (!cache->frameless_p
746       && regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM)
747     {
748       *optimizedp = 0;
749       *lvalp = lval_memory;
750       *addrp = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
751       *realnump = -1;
752       if (valuep)
753 	{
754 	  struct gdbarch *gdbarch = get_frame_arch (next_frame);
755 
756 	  /* Read the value in from memory.  */
757 	  read_memory (*addrp, valuep, register_size (gdbarch, regnum));
758 	}
759       return;
760     }
761 
762   /* The previous frame's `out' registers are accessable as the
763      current frame's `in' registers.  */
764   if (!cache->frameless_p
765       && regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM)
766     regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
767 
768   frame_register_unwind (next_frame, regnum,
769 			 optimizedp, lvalp, addrp, realnump, valuep);
770 }
771 
772 static const struct frame_unwind sparc32_frame_unwind =
773 {
774   NORMAL_FRAME,
775   sparc32_frame_this_id,
776   sparc32_frame_prev_register
777 };
778 
779 static const struct frame_unwind *
sparc32_frame_sniffer(struct frame_info * next_frame)780 sparc32_frame_sniffer (struct frame_info *next_frame)
781 {
782   return &sparc32_frame_unwind;
783 }
784 
785 
786 static CORE_ADDR
sparc32_frame_base_address(struct frame_info * next_frame,void ** this_cache)787 sparc32_frame_base_address (struct frame_info *next_frame, void **this_cache)
788 {
789   struct sparc_frame_cache *cache =
790     sparc32_frame_cache (next_frame, this_cache);
791 
792   return cache->base;
793 }
794 
795 static const struct frame_base sparc32_frame_base =
796 {
797   &sparc32_frame_unwind,
798   sparc32_frame_base_address,
799   sparc32_frame_base_address,
800   sparc32_frame_base_address
801 };
802 
803 static struct frame_id
sparc_unwind_dummy_id(struct gdbarch * gdbarch,struct frame_info * next_frame)804 sparc_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
805 {
806   CORE_ADDR sp;
807 
808   sp = frame_unwind_register_unsigned (next_frame, SPARC_SP_REGNUM);
809   return frame_id_build (sp, frame_pc_unwind (next_frame));
810 }
811 
812 
813 /* Extract from an array REGBUF containing the (raw) register state, a
814    function return value of TYPE, and copy that into VALBUF.  */
815 
816 static void
sparc32_extract_return_value(struct type * type,struct regcache * regcache,void * valbuf)817 sparc32_extract_return_value (struct type *type, struct regcache *regcache,
818 			      void *valbuf)
819 {
820   int len = TYPE_LENGTH (type);
821   char buf[8];
822 
823   gdb_assert (!sparc_structure_or_union_p (type));
824   gdb_assert (!(sparc_floating_p (type) && len == 16));
825 
826   if (sparc_floating_p (type))
827     {
828       /* Floating return values.  */
829       regcache_cooked_read (regcache, SPARC_F0_REGNUM, buf);
830       if (len > 4)
831 	regcache_cooked_read (regcache, SPARC_F1_REGNUM, buf + 4);
832       memcpy (valbuf, buf, len);
833     }
834   else
835     {
836       /* Integral and pointer return values.  */
837       gdb_assert (sparc_integral_or_pointer_p (type));
838 
839       regcache_cooked_read (regcache, SPARC_O0_REGNUM, buf);
840       if (len > 4)
841 	{
842 	  regcache_cooked_read (regcache, SPARC_O1_REGNUM, buf + 4);
843 	  gdb_assert (len == 8);
844 	  memcpy (valbuf, buf, 8);
845 	}
846       else
847 	{
848 	  /* Just stripping off any unused bytes should preserve the
849 	     signed-ness just fine.  */
850 	  memcpy (valbuf, buf + 4 - len, len);
851 	}
852     }
853 }
854 
855 /* Write into the appropriate registers a function return value stored
856    in VALBUF of type TYPE.  */
857 
858 static void
sparc32_store_return_value(struct type * type,struct regcache * regcache,const void * valbuf)859 sparc32_store_return_value (struct type *type, struct regcache *regcache,
860 			    const void *valbuf)
861 {
862   int len = TYPE_LENGTH (type);
863   char buf[8];
864 
865   gdb_assert (!sparc_structure_or_union_p (type));
866   gdb_assert (!(sparc_floating_p (type) && len == 16));
867 
868   if (sparc_floating_p (type))
869     {
870       /* Floating return values.  */
871       memcpy (buf, valbuf, len);
872       regcache_cooked_write (regcache, SPARC_F0_REGNUM, buf);
873       if (len > 4)
874 	regcache_cooked_write (regcache, SPARC_F1_REGNUM, buf + 4);
875     }
876   else
877     {
878       /* Integral and pointer return values.  */
879       gdb_assert (sparc_integral_or_pointer_p (type));
880 
881       if (len > 4)
882 	{
883 	  gdb_assert (len == 8);
884 	  memcpy (buf, valbuf, 8);
885 	  regcache_cooked_write (regcache, SPARC_O1_REGNUM, buf + 4);
886 	}
887       else
888 	{
889 	  /* ??? Do we need to do any sign-extension here?  */
890 	  memcpy (buf + 4 - len, valbuf, len);
891 	}
892       regcache_cooked_write (regcache, SPARC_O0_REGNUM, buf);
893     }
894 }
895 
896 static enum return_value_convention
sparc32_return_value(struct gdbarch * gdbarch,struct type * type,struct regcache * regcache,void * readbuf,const void * writebuf)897 sparc32_return_value (struct gdbarch *gdbarch, struct type *type,
898 		      struct regcache *regcache, void *readbuf,
899 		      const void *writebuf)
900 {
901   if (sparc_structure_or_union_p (type)
902       || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16))
903     return RETURN_VALUE_STRUCT_CONVENTION;
904 
905   if (readbuf)
906     sparc32_extract_return_value (type, regcache, readbuf);
907   if (writebuf)
908     sparc32_store_return_value (type, regcache, writebuf);
909 
910   return RETURN_VALUE_REGISTER_CONVENTION;
911 }
912 
913 #if 0
914 /* NOTE: cagney/2004-01-17: For the moment disable this method.  The
915    architecture and CORE-gdb will need new code (and a replacement for
916    DEPRECATED_EXTRACT_STRUCT_VALUE_ADDRESS) before this can be made to
917    work robustly.  Here is a possible function signature: */
918 /* NOTE: cagney/2004-01-17: So far only the 32-bit SPARC ABI has been
919    identifed as having a way to robustly recover the address of a
920    struct-convention return-value (after the function has returned).
921    For all other ABIs so far examined, the calling convention makes no
922    guarenteed that the register containing the return-value will be
923    preserved and hence that the return-value's address can be
924    recovered.  */
925 /* Extract from REGCACHE, which contains the (raw) register state, the
926    address in which a function should return its structure value, as a
927    CORE_ADDR.  */
928 
929 static CORE_ADDR
930 sparc32_extract_struct_value_address (struct regcache *regcache)
931 {
932   ULONGEST sp;
933 
934   regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
935   return read_memory_unsigned_integer (sp + 64, 4);
936 }
937 #endif
938 
939 static int
sparc32_stabs_argument_has_addr(struct gdbarch * gdbarch,struct type * type)940 sparc32_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
941 {
942   return (sparc_structure_or_union_p (type)
943 	  || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16));
944 }
945 
946 
947 /* The SPARC Architecture doesn't have hardware single-step support,
948    and most operating systems don't implement it either, so we provide
949    software single-step mechanism.  */
950 
951 static CORE_ADDR
sparc_analyze_control_transfer(CORE_ADDR pc,CORE_ADDR * npc)952 sparc_analyze_control_transfer (CORE_ADDR pc, CORE_ADDR *npc)
953 {
954   unsigned long insn = sparc_fetch_instruction (pc);
955   int conditional_p = X_COND (insn) & 0x7;
956   int branch_p = 0;
957   long offset = 0;			/* Must be signed for sign-extend.  */
958 
959   if (X_OP (insn) == 0 && X_OP2 (insn) == 3 && (insn & 0x1000000) == 0)
960     {
961       /* Branch on Integer Register with Prediction (BPr).  */
962       branch_p = 1;
963       conditional_p = 1;
964     }
965   else if (X_OP (insn) == 0 && X_OP2 (insn) == 6)
966     {
967       /* Branch on Floating-Point Condition Codes (FBfcc).  */
968       branch_p = 1;
969       offset = 4 * X_DISP22 (insn);
970     }
971   else if (X_OP (insn) == 0 && X_OP2 (insn) == 5)
972     {
973       /* Branch on Floating-Point Condition Codes with Prediction
974          (FBPfcc).  */
975       branch_p = 1;
976       offset = 4 * X_DISP19 (insn);
977     }
978   else if (X_OP (insn) == 0 && X_OP2 (insn) == 2)
979     {
980       /* Branch on Integer Condition Codes (Bicc).  */
981       branch_p = 1;
982       offset = 4 * X_DISP22 (insn);
983     }
984   else if (X_OP (insn) == 0 && X_OP2 (insn) == 1)
985     {
986       /* Branch on Integer Condition Codes with Prediction (BPcc).  */
987       branch_p = 1;
988       offset = 4 * X_DISP19 (insn);
989     }
990   else if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3a)
991     {
992       if ((X_I (insn) == 0 && X_RS1 (insn) == 0 && X_RS2 (insn) == 0)
993 	  || (X_I (insn) == 1 && X_RS1 (insn) == 0 && (insn & 0x7f) == 0))
994 	{
995 	  /* OpenBSD system call.  */
996 	  ULONGEST number;
997 
998 	  regcache_cooked_read_unsigned (current_regcache,
999 					 SPARC_G1_REGNUM, &number);
1000 
1001 	  if (number & 0x400)
1002 	    return sparc_address_from_register (SPARC_G2_REGNUM);
1003 	  if (number & 0x800)
1004 	    return sparc_address_from_register (SPARC_G7_REGNUM);
1005 	}
1006     }
1007 
1008   /* FIXME: Handle DONE and RETRY instructions.  */
1009 
1010   /* FIXME: Handle the Trap instruction.  */
1011 
1012   if (branch_p)
1013     {
1014       if (conditional_p)
1015 	{
1016 	  /* For conditional branches, return nPC + 4 iff the annul
1017 	     bit is 1.  */
1018 	  return (X_A (insn) ? *npc + 4 : 0);
1019 	}
1020       else
1021 	{
1022 	  /* For unconditional branches, return the target if its
1023 	     specified condition is "always" and return nPC + 4 if the
1024 	     condition is "never".  If the annul bit is 1, set *NPC to
1025 	     zero.  */
1026 	  if (X_COND (insn) == 0x0)
1027 	    pc = *npc, offset = 4;
1028 	  if (X_A (insn))
1029 	    *npc = 0;
1030 
1031 	  gdb_assert (offset != 0);
1032 	  return pc + offset;
1033 	}
1034     }
1035 
1036   return 0;
1037 }
1038 
1039 void
sparc_software_single_step(enum target_signal sig,int insert_breakpoints_p)1040 sparc_software_single_step (enum target_signal sig, int insert_breakpoints_p)
1041 {
1042   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1043   static CORE_ADDR npc, nnpc;
1044   static char npc_save[4], nnpc_save[4];
1045 
1046   if (insert_breakpoints_p)
1047     {
1048       CORE_ADDR pc, orig_npc;
1049 
1050       pc = sparc_address_from_register (tdep->pc_regnum);
1051       orig_npc = npc = sparc_address_from_register (tdep->npc_regnum);
1052 
1053       /* Analyze the instruction at PC.  */
1054       nnpc = sparc_analyze_control_transfer (pc, &npc);
1055       if (npc != 0)
1056 	target_insert_breakpoint (npc, npc_save);
1057       if (nnpc != 0)
1058 	target_insert_breakpoint (nnpc, nnpc_save);
1059 
1060       /* Assert that we have set at least one breakpoint, and that
1061 	 they're not set at the same spot - unless we're going
1062 	 from here straight to NULL, i.e. a call or jump to 0.  */
1063       gdb_assert (npc != 0 || nnpc != 0 || orig_npc == 0);
1064       gdb_assert (nnpc != npc || orig_npc == 0);
1065     }
1066   else
1067     {
1068       if (npc != 0)
1069 	target_remove_breakpoint (npc, npc_save);
1070       if (nnpc != 0)
1071 	target_remove_breakpoint (nnpc, nnpc_save);
1072     }
1073 }
1074 
1075 static void
sparc_write_pc(CORE_ADDR pc,ptid_t ptid)1076 sparc_write_pc (CORE_ADDR pc, ptid_t ptid)
1077 {
1078   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1079 
1080   write_register_pid (tdep->pc_regnum, pc, ptid);
1081   write_register_pid (tdep->npc_regnum, pc + 4, ptid);
1082 }
1083 
1084 /* Unglobalize NAME.  */
1085 
1086 char *
sparc_stabs_unglobalize_name(char * name)1087 sparc_stabs_unglobalize_name (char *name)
1088 {
1089   /* The Sun compilers (Sun ONE Studio, Forte Developer, Sun WorkShop,
1090      SunPRO) convert file static variables into global values, a
1091      process known as globalization.  In order to do this, the
1092      compiler will create a unique prefix and prepend it to each file
1093      static variable.  For static variables within a function, this
1094      globalization prefix is followed by the function name (nested
1095      static variables within a function are supposed to generate a
1096      warning message, and are left alone).  The procedure is
1097      documented in the Stabs Interface Manual, which is distrubuted
1098      with the compilers, although version 4.0 of the manual seems to
1099      be incorrect in some places, at least for SPARC.  The
1100      globalization prefix is encoded into an N_OPT stab, with the form
1101      "G=<prefix>".  The globalization prefix always seems to start
1102      with a dollar sign '$'; a dot '.' is used as a seperator.  So we
1103      simply strip everything up until the last dot.  */
1104 
1105   if (name[0] == '$')
1106     {
1107       char *p = strrchr (name, '.');
1108       if (p)
1109 	return p + 1;
1110     }
1111 
1112   return name;
1113 }
1114 
1115 
1116 /* Return the appropriate register set for the core section identified
1117    by SECT_NAME and SECT_SIZE.  */
1118 
1119 const struct regset *
sparc_regset_from_core_section(struct gdbarch * gdbarch,const char * sect_name,size_t sect_size)1120 sparc_regset_from_core_section (struct gdbarch *gdbarch,
1121 				const char *sect_name, size_t sect_size)
1122 {
1123   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1124 
1125   if (strcmp (sect_name, ".reg") == 0 && sect_size >= tdep->sizeof_gregset)
1126     return tdep->gregset;
1127 
1128   if (strcmp (sect_name, ".reg2") == 0 && sect_size >= tdep->sizeof_fpregset)
1129     return tdep->fpregset;
1130 
1131   return NULL;
1132 }
1133 
1134 
1135 static struct gdbarch *
sparc32_gdbarch_init(struct gdbarch_info info,struct gdbarch_list * arches)1136 sparc32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1137 {
1138   struct gdbarch_tdep *tdep;
1139   struct gdbarch *gdbarch;
1140 
1141   /* If there is already a candidate, use it.  */
1142   arches = gdbarch_list_lookup_by_info (arches, &info);
1143   if (arches != NULL)
1144     return arches->gdbarch;
1145 
1146   /* Allocate space for the new architecture.  */
1147   tdep = XMALLOC (struct gdbarch_tdep);
1148   gdbarch = gdbarch_alloc (&info, tdep);
1149 
1150   tdep->pc_regnum = SPARC32_PC_REGNUM;
1151   tdep->npc_regnum = SPARC32_NPC_REGNUM;
1152   tdep->gregset = NULL;
1153   tdep->sizeof_gregset = 0;
1154   tdep->fpregset = NULL;
1155   tdep->sizeof_fpregset = 0;
1156   tdep->plt_entry_size = 0;
1157 
1158   set_gdbarch_long_double_bit (gdbarch, 128);
1159   set_gdbarch_long_double_format (gdbarch, &floatformat_sparc_quad);
1160 
1161   set_gdbarch_num_regs (gdbarch, SPARC32_NUM_REGS);
1162   set_gdbarch_register_name (gdbarch, sparc32_register_name);
1163   set_gdbarch_register_type (gdbarch, sparc32_register_type);
1164   set_gdbarch_num_pseudo_regs (gdbarch, SPARC32_NUM_PSEUDO_REGS);
1165   set_gdbarch_pseudo_register_read (gdbarch, sparc32_pseudo_register_read);
1166   set_gdbarch_pseudo_register_write (gdbarch, sparc32_pseudo_register_write);
1167 
1168   /* Register numbers of various important registers.  */
1169   set_gdbarch_sp_regnum (gdbarch, SPARC_SP_REGNUM); /* %sp */
1170   set_gdbarch_pc_regnum (gdbarch, SPARC32_PC_REGNUM); /* %pc */
1171   set_gdbarch_fp0_regnum (gdbarch, SPARC_F0_REGNUM); /* %f0 */
1172 
1173   /* Call dummy code.  */
1174   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1175   set_gdbarch_push_dummy_code (gdbarch, sparc32_push_dummy_code);
1176   set_gdbarch_push_dummy_call (gdbarch, sparc32_push_dummy_call);
1177 
1178   set_gdbarch_return_value (gdbarch, sparc32_return_value);
1179   set_gdbarch_stabs_argument_has_addr
1180     (gdbarch, sparc32_stabs_argument_has_addr);
1181 
1182   set_gdbarch_skip_prologue (gdbarch, sparc32_skip_prologue);
1183 
1184   /* Stack grows downward.  */
1185   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1186 
1187   set_gdbarch_breakpoint_from_pc (gdbarch, sparc_breakpoint_from_pc);
1188 
1189   set_gdbarch_frame_args_skip (gdbarch, 8);
1190 
1191   set_gdbarch_print_insn (gdbarch, print_insn_sparc);
1192 
1193   set_gdbarch_software_single_step (gdbarch, sparc_software_single_step);
1194   set_gdbarch_write_pc (gdbarch, sparc_write_pc);
1195 
1196   set_gdbarch_unwind_dummy_id (gdbarch, sparc_unwind_dummy_id);
1197 
1198   set_gdbarch_unwind_pc (gdbarch, sparc_unwind_pc);
1199 
1200   frame_base_set_default (gdbarch, &sparc32_frame_base);
1201 
1202   /* Hook in ABI-specific overrides, if they have been registered.  */
1203   gdbarch_init_osabi (info, gdbarch);
1204 
1205   frame_unwind_append_sniffer (gdbarch, sparc32_frame_sniffer);
1206 
1207   /* If we have register sets, enable the generic core file support.  */
1208   if (tdep->gregset)
1209     set_gdbarch_regset_from_core_section (gdbarch,
1210 					  sparc_regset_from_core_section);
1211 
1212   return gdbarch;
1213 }
1214 
1215 /* Helper functions for dealing with register windows.  */
1216 
1217 void
sparc_supply_rwindow(struct regcache * regcache,CORE_ADDR sp,int regnum)1218 sparc_supply_rwindow (struct regcache *regcache, CORE_ADDR sp, int regnum)
1219 {
1220   int offset = 0;
1221   char buf[8];
1222   int i;
1223 
1224   if (sp & 1)
1225     {
1226       /* Registers are 64-bit.  */
1227       sp += BIAS;
1228 
1229       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1230 	{
1231 	  if (regnum == i || regnum == -1)
1232 	    {
1233 	      target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
1234 
1235 	      /* Handle StackGhost.  */
1236 	      if (i == SPARC_I7_REGNUM)
1237 		{
1238 		  ULONGEST wcookie = sparc_fetch_wcookie ();
1239 		  ULONGEST i7 = extract_unsigned_integer (buf + offset, 8);
1240 
1241 		  store_unsigned_integer (buf + offset, 8, i7 ^ wcookie);
1242 		}
1243 
1244 	      regcache_raw_supply (regcache, i, buf);
1245 	    }
1246 	}
1247     }
1248   else
1249     {
1250       /* Registers are 32-bit.  Toss any sign-extension of the stack
1251 	 pointer.  */
1252       sp &= 0xffffffffUL;
1253 
1254       /* Clear out the top half of the temporary buffer, and put the
1255 	 register value in the bottom half if we're in 64-bit mode.  */
1256       if (gdbarch_ptr_bit (current_gdbarch) == 64)
1257 	{
1258 	  memset (buf, 0, 4);
1259 	  offset = 4;
1260 	}
1261 
1262       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1263 	{
1264 	  if (regnum == i || regnum == -1)
1265 	    {
1266 	      target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
1267 				  buf + offset, 4);
1268 
1269 	      /* Handle StackGhost.  */
1270 	      if (i == SPARC_I7_REGNUM)
1271 		{
1272 		  ULONGEST wcookie = sparc_fetch_wcookie ();
1273 		  ULONGEST i7 = extract_unsigned_integer (buf + offset, 4);
1274 
1275 		  store_unsigned_integer (buf + offset, 4, i7 ^ wcookie);
1276 		}
1277 
1278 	      regcache_raw_supply (regcache, i, buf);
1279 	    }
1280 	}
1281     }
1282 }
1283 
1284 void
sparc_collect_rwindow(const struct regcache * regcache,CORE_ADDR sp,int regnum)1285 sparc_collect_rwindow (const struct regcache *regcache,
1286 		       CORE_ADDR sp, int regnum)
1287 {
1288   int offset = 0;
1289   char buf[8];
1290   int i;
1291 
1292   if (sp & 1)
1293     {
1294       /* Registers are 64-bit.  */
1295       sp += BIAS;
1296 
1297       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1298 	{
1299 	  if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
1300 	    {
1301 	      regcache_raw_collect (regcache, i, buf);
1302 
1303 	      /* Handle StackGhost.  */
1304 	      if (i == SPARC_I7_REGNUM)
1305 		{
1306 		  ULONGEST wcookie = sparc_fetch_wcookie ();
1307 		  ULONGEST i7 = extract_unsigned_integer (buf + offset, 8);
1308 
1309 		  store_unsigned_integer (buf, 8, i7 ^ wcookie);
1310 		}
1311 
1312 	      target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
1313 	    }
1314 	}
1315     }
1316   else
1317     {
1318       /* Registers are 32-bit.  Toss any sign-extension of the stack
1319 	 pointer.  */
1320       sp &= 0xffffffffUL;
1321 
1322       /* Only use the bottom half if we're in 64-bit mode.  */
1323       if (gdbarch_ptr_bit (current_gdbarch) == 64)
1324 	offset = 4;
1325 
1326       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1327 	{
1328 	  if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
1329 	    {
1330 	      regcache_raw_collect (regcache, i, buf);
1331 
1332 	      /* Handle StackGhost.  */
1333 	      if (i == SPARC_I7_REGNUM)
1334 		{
1335 		  ULONGEST wcookie = sparc_fetch_wcookie ();
1336 		  ULONGEST i7 = extract_unsigned_integer (buf + offset, 4);
1337 
1338 		  store_unsigned_integer (buf + offset, 4, i7 ^ wcookie);
1339 		}
1340 
1341 	      target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
1342 				   buf + offset, 4);
1343 	    }
1344 	}
1345     }
1346 }
1347 
1348 /* Helper functions for dealing with register sets.  */
1349 
1350 void
sparc32_supply_gregset(const struct sparc_gregset * gregset,struct regcache * regcache,int regnum,const void * gregs)1351 sparc32_supply_gregset (const struct sparc_gregset *gregset,
1352 			struct regcache *regcache,
1353 			int regnum, const void *gregs)
1354 {
1355   const char *regs = gregs;
1356   int i;
1357 
1358   if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1359     regcache_raw_supply (regcache, SPARC32_PSR_REGNUM,
1360 			 regs + gregset->r_psr_offset);
1361 
1362   if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1363     regcache_raw_supply (regcache, SPARC32_PC_REGNUM,
1364 			 regs + gregset->r_pc_offset);
1365 
1366   if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1367     regcache_raw_supply (regcache, SPARC32_NPC_REGNUM,
1368 			 regs + gregset->r_npc_offset);
1369 
1370   if (regnum == SPARC32_Y_REGNUM || regnum == -1)
1371     regcache_raw_supply (regcache, SPARC32_Y_REGNUM,
1372 			 regs + gregset->r_y_offset);
1373 
1374   if (regnum == SPARC_G0_REGNUM || regnum == -1)
1375     regcache_raw_supply (regcache, SPARC_G0_REGNUM, NULL);
1376 
1377   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1378     {
1379       int offset = gregset->r_g1_offset;
1380 
1381       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1382 	{
1383 	  if (regnum == i || regnum == -1)
1384 	    regcache_raw_supply (regcache, i, regs + offset);
1385 	  offset += 4;
1386 	}
1387     }
1388 
1389   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1390     {
1391       /* Not all of the register set variants include Locals and
1392          Inputs.  For those that don't, we read them off the stack.  */
1393       if (gregset->r_l0_offset == -1)
1394 	{
1395 	  ULONGEST sp;
1396 
1397 	  regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1398 	  sparc_supply_rwindow (regcache, sp, regnum);
1399 	}
1400       else
1401 	{
1402 	  int offset = gregset->r_l0_offset;
1403 
1404 	  for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1405 	    {
1406 	      if (regnum == i || regnum == -1)
1407 		regcache_raw_supply (regcache, i, regs + offset);
1408 	      offset += 4;
1409 	    }
1410 	}
1411     }
1412 }
1413 
1414 void
sparc32_collect_gregset(const struct sparc_gregset * gregset,const struct regcache * regcache,int regnum,void * gregs)1415 sparc32_collect_gregset (const struct sparc_gregset *gregset,
1416 			 const struct regcache *regcache,
1417 			 int regnum, void *gregs)
1418 {
1419   char *regs = gregs;
1420   int i;
1421 
1422   if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1423     regcache_raw_collect (regcache, SPARC32_PSR_REGNUM,
1424 			  regs + gregset->r_psr_offset);
1425 
1426   if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1427     regcache_raw_collect (regcache, SPARC32_PC_REGNUM,
1428 			  regs + gregset->r_pc_offset);
1429 
1430   if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1431     regcache_raw_collect (regcache, SPARC32_NPC_REGNUM,
1432 			  regs + gregset->r_npc_offset);
1433 
1434   if (regnum == SPARC32_Y_REGNUM || regnum == -1)
1435     regcache_raw_collect (regcache, SPARC32_Y_REGNUM,
1436 			  regs + gregset->r_y_offset);
1437 
1438   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1439     {
1440       int offset = gregset->r_g1_offset;
1441 
1442       /* %g0 is always zero.  */
1443       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1444 	{
1445 	  if (regnum == i || regnum == -1)
1446 	    regcache_raw_collect (regcache, i, regs + offset);
1447 	  offset += 4;
1448 	}
1449     }
1450 
1451   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1452     {
1453       /* Not all of the register set variants include Locals and
1454          Inputs.  For those that don't, we read them off the stack.  */
1455       if (gregset->r_l0_offset != -1)
1456 	{
1457 	  int offset = gregset->r_l0_offset;
1458 
1459 	  for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1460 	    {
1461 	      if (regnum == i || regnum == -1)
1462 		regcache_raw_collect (regcache, i, regs + offset);
1463 	      offset += 4;
1464 	    }
1465 	}
1466     }
1467 }
1468 
1469 void
sparc32_supply_fpregset(struct regcache * regcache,int regnum,const void * fpregs)1470 sparc32_supply_fpregset (struct regcache *regcache,
1471 			 int regnum, const void *fpregs)
1472 {
1473   const char *regs = fpregs;
1474   int i;
1475 
1476   for (i = 0; i < 32; i++)
1477     {
1478       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
1479 	regcache_raw_supply (regcache, SPARC_F0_REGNUM + i, regs + (i * 4));
1480     }
1481 
1482   if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
1483     regcache_raw_supply (regcache, SPARC32_FSR_REGNUM, regs + (32 * 4) + 4);
1484 }
1485 
1486 void
sparc32_collect_fpregset(const struct regcache * regcache,int regnum,void * fpregs)1487 sparc32_collect_fpregset (const struct regcache *regcache,
1488 			  int regnum, void *fpregs)
1489 {
1490   char *regs = fpregs;
1491   int i;
1492 
1493   for (i = 0; i < 32; i++)
1494     {
1495       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
1496 	regcache_raw_collect (regcache, SPARC_F0_REGNUM + i, regs + (i * 4));
1497     }
1498 
1499   if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
1500     regcache_raw_collect (regcache, SPARC32_FSR_REGNUM, regs + (32 * 4) + 4);
1501 }
1502 
1503 
1504 /* SunOS 4.  */
1505 
1506 /* From <machine/reg.h>.  */
1507 const struct sparc_gregset sparc32_sunos4_gregset =
1508 {
1509   0 * 4,			/* %psr */
1510   1 * 4,			/* %pc */
1511   2 * 4,			/* %npc */
1512   3 * 4,			/* %y */
1513   -1,				/* %wim */
1514   -1,				/* %tbr */
1515   4 * 4,			/* %g1 */
1516   -1				/* %l0 */
1517 };
1518 
1519 
1520 /* Provide a prototype to silence -Wmissing-prototypes.  */
1521 void _initialize_sparc_tdep (void);
1522 
1523 void
_initialize_sparc_tdep(void)1524 _initialize_sparc_tdep (void)
1525 {
1526   register_gdbarch_init (bfd_arch_sparc, sparc32_gdbarch_init);
1527 }
1528