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