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