ab984ff88ad437af2f0e3e379e703a63b5f4d517
[deliverable/binutils-gdb.git] / gdb / sparcnbsd-tdep.c
1 /* Target-dependent code for SPARC systems running NetBSD.
2 Copyright 2002 Free Software Foundation, Inc.
3 Contributed by Wasabi Systems, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "target.h"
26 #include "value.h"
27 #include "osabi.h"
28
29 #include "sparcnbsd-tdep.h"
30 #include "nbsd-tdep.h"
31
32 #include "solib-svr4.h"
33
34 #define REG32_OFFSET_PSR (0 * 4)
35 #define REG32_OFFSET_PC (1 * 4)
36 #define REG32_OFFSET_NPC (2 * 4)
37 #define REG32_OFFSET_Y (3 * 4)
38 #define REG32_OFFSET_GLOBAL (4 * 4)
39 #define REG32_OFFSET_OUT (12 * 4)
40
41 #define REG64_OFFSET_TSTATE (0 * 8)
42 #define REG64_OFFSET_PC (1 * 8)
43 #define REG64_OFFSET_NPC (2 * 8)
44 #define REG64_OFFSET_Y (3 * 8)
45 #define REG64_OFFSET_GLOBAL (4 * 8)
46 #define REG64_OFFSET_OUT (12 * 8)
47
48 void
49 sparcnbsd_supply_reg32 (char *regs, int regno)
50 {
51 int i;
52
53 if (regno == PS_REGNUM || regno == -1)
54 supply_register (PS_REGNUM, regs + REG32_OFFSET_PSR);
55
56 if (regno == PC_REGNUM || regno == -1)
57 supply_register (PC_REGNUM, regs + REG32_OFFSET_PC);
58
59 if (regno == NPC_REGNUM || regno == -1)
60 supply_register (NPC_REGNUM, regs + REG32_OFFSET_NPC);
61
62 if (regno == Y_REGNUM || regno == -1)
63 supply_register (Y_REGNUM, regs + REG32_OFFSET_Y);
64
65 if ((regno >= G0_REGNUM && regno <= G7_REGNUM) || regno == -1)
66 {
67 if (regno == G0_REGNUM || regno == -1)
68 supply_register (G0_REGNUM, NULL); /* %g0 is always zero */
69 for (i = G1_REGNUM; i <= G7_REGNUM; i++)
70 {
71 if (regno == i || regno == -1)
72 supply_register (i, regs + REG32_OFFSET_GLOBAL +
73 ((i - G0_REGNUM) * 4));
74 }
75 }
76
77 if ((regno >= O0_REGNUM && regno <= O7_REGNUM) || regno == -1)
78 {
79 for (i = O0_REGNUM; i <= O7_REGNUM; i++)
80 {
81 if (regno == i || regno == -1)
82 supply_register (i, regs + REG32_OFFSET_OUT +
83 ((i - O0_REGNUM) * 4));
84 }
85 }
86
87 /* Inputs and Locals are stored onto the stack by by the kernel. */
88 if ((regno >= L0_REGNUM && regno <= I7_REGNUM) || regno == -1)
89 {
90 CORE_ADDR sp = read_register (SP_REGNUM);
91 char buf[4];
92
93 for (i = L0_REGNUM; i <= I7_REGNUM; i++)
94 {
95 if (regno == i || regno == -1)
96 {
97 target_read_memory (sp + ((i - L0_REGNUM) * 4),
98 buf, sizeof (buf));
99 supply_register (i, buf);
100 }
101 }
102 }
103
104 /* FIXME: If we don't set these valid, read_register_bytes() rereads
105 all the regs every time it is called! */
106 if (regno == WIM_REGNUM || regno == -1)
107 supply_register (WIM_REGNUM, NULL);
108 if (regno == TBR_REGNUM || regno == -1)
109 supply_register (TBR_REGNUM, NULL);
110 if (regno == CPS_REGNUM || regno == -1)
111 supply_register (CPS_REGNUM, NULL);
112 }
113
114 void
115 sparcnbsd_supply_reg64 (char *regs, int regno)
116 {
117 int i;
118 char buf[8];
119
120 if (regno == TSTATE_REGNUM || regno == -1)
121 supply_register (PS_REGNUM, regs + REG64_OFFSET_TSTATE);
122
123 if (regno == PC_REGNUM || regno == -1)
124 supply_register (PC_REGNUM, regs + REG64_OFFSET_PC);
125
126 if (regno == NPC_REGNUM || regno == -1)
127 supply_register (NPC_REGNUM, regs + REG64_OFFSET_NPC);
128
129 if (regno == Y_REGNUM || regno == -1)
130 {
131 memset (buf, 0, sizeof (buf));
132 memcpy (&buf[4], regs + REG64_OFFSET_Y, 4);
133 supply_register (Y_REGNUM, buf);
134 }
135
136 if ((regno >= G0_REGNUM && regno <= G7_REGNUM) || regno == -1)
137 {
138 if (regno == G0_REGNUM || regno == -1)
139 supply_register (G0_REGNUM, NULL); /* %g0 is always zero */
140 for (i = G1_REGNUM; i <= G7_REGNUM; i++)
141 {
142 if (regno == i || regno == -1)
143 supply_register (i, regs + REG64_OFFSET_GLOBAL +
144 ((i - G0_REGNUM) * 8));
145 }
146 }
147
148 if ((regno >= O0_REGNUM && regno <= O7_REGNUM) || regno == -1)
149 {
150 for (i = O0_REGNUM; i <= O7_REGNUM; i++)
151 {
152 if (regno == i || regno == -1)
153 supply_register (i, regs + REG64_OFFSET_OUT +
154 ((i - O0_REGNUM) * 8));
155 }
156 }
157
158 /* Inputs and Locals are stored onto the stack by by the kernel. */
159 if ((regno >= L0_REGNUM && regno <= I7_REGNUM) || regno == -1)
160 {
161 CORE_ADDR sp = read_register (SP_REGNUM);
162 char buf[8];
163
164 if (sp & 1)
165 {
166 /* Registers are 64-bit. */
167 sp += 2047;
168
169 for (i = L0_REGNUM; i <= I7_REGNUM; i++)
170 {
171 if (regno == i || regno == -1)
172 {
173 target_read_memory (sp + ((i - L0_REGNUM) * 8),
174 buf, sizeof (buf));
175 supply_register (i, buf);
176 }
177 }
178 }
179 else
180 {
181 /* Registers are 32-bit. Toss any sign-extension of the stack
182 pointer, clear out the top half of the temporary buffer, and
183 put the register value in the bottom half. */
184
185 sp &= 0xffffffffUL;
186 memset (buf, 0, sizeof (buf));
187 for (i = L0_REGNUM; i <= I7_REGNUM; i++)
188 {
189 if (regno == i || regno == -1)
190 {
191 target_read_memory (sp + ((i - L0_REGNUM) * 4),
192 &buf[4], sizeof (buf));
193 supply_register (i, buf);
194 }
195 }
196 }
197 }
198
199 /* FIXME: If we don't set these valid, read_register_bytes() rereads
200 all the regs every time it is called! */
201 if (regno == WIM_REGNUM || regno == -1)
202 supply_register (WIM_REGNUM, NULL);
203 if (regno == TBR_REGNUM || regno == -1)
204 supply_register (TBR_REGNUM, NULL);
205 if (regno == CPS_REGNUM || regno == -1)
206 supply_register (CPS_REGNUM, NULL);
207 }
208
209 void
210 sparcnbsd_fill_reg32 (char *regs, int regno)
211 {
212 int i;
213
214 if (regno == PS_REGNUM || regno == -1)
215 regcache_collect (PS_REGNUM, regs + REG32_OFFSET_PSR);
216
217 if (regno == PC_REGNUM || regno == -1)
218 regcache_collect (PC_REGNUM, regs + REG32_OFFSET_PC);
219
220 if (regno == NPC_REGNUM || regno == -1)
221 regcache_collect (NPC_REGNUM, regs + REG32_OFFSET_NPC);
222
223 if (regno == Y_REGNUM || regno == -1)
224 regcache_collect (Y_REGNUM, regs + REG32_OFFSET_Y);
225
226 if ((regno >= G0_REGNUM && regno <= G7_REGNUM) || regno == -1)
227 {
228 /* %g0 is always zero */
229 for (i = G1_REGNUM; i <= G7_REGNUM; i++)
230 {
231 if (regno == i || regno == -1)
232 regcache_collect (i, regs + REG32_OFFSET_GLOBAL +
233 ((i - G0_REGNUM) * 4));
234 }
235 }
236
237 if ((regno >= O0_REGNUM && regno <= O7_REGNUM) || regno == -1)
238 {
239 for (i = O0_REGNUM; i <= O7_REGNUM; i++)
240 {
241 if (regno == i || regno == -1)
242 regcache_collect (i, regs + REG32_OFFSET_OUT +
243 ((i - O0_REGNUM) * 4));
244 }
245 }
246
247 /* Responsibility for the stack regs is pushed off onto the caller. */
248 }
249
250 void
251 sparcnbsd_fill_reg64 (char *regs, int regno)
252 {
253 int i;
254
255 if (regno == TSTATE_REGNUM || regno == -1)
256 regcache_collect (TSTATE_REGNUM, regs + REG64_OFFSET_TSTATE);
257
258 if (regno == PC_REGNUM || regno == -1)
259 regcache_collect (PC_REGNUM, regs + REG64_OFFSET_PC);
260
261 if (regno == NPC_REGNUM || regno == -1)
262 regcache_collect (NPC_REGNUM, regs + REG64_OFFSET_NPC);
263
264 if (regno == Y_REGNUM || regno == -1)
265 regcache_collect (Y_REGNUM, regs + REG64_OFFSET_Y);
266
267 if ((regno >= G0_REGNUM && regno <= G7_REGNUM) || regno == -1)
268 {
269 /* %g0 is always zero */
270 for (i = G1_REGNUM; i <= G7_REGNUM; i++)
271 {
272 if (regno == i || regno == -1)
273 regcache_collect (i, regs + REG64_OFFSET_GLOBAL +
274 ((i - G0_REGNUM) * 4));
275 }
276 }
277
278 if ((regno >= O0_REGNUM && regno <= O7_REGNUM) || regno == -1)
279 {
280 for (i = O0_REGNUM; i <= O7_REGNUM; i++)
281 {
282 if (regno == i || regno == -1)
283 regcache_collect (i, regs + REG64_OFFSET_OUT +
284 ((i - O0_REGNUM) * 4));
285 }
286 }
287
288 /* Responsibility for the stack regs is pushed off onto the caller. */
289 }
290
291 void
292 sparcnbsd_supply_fpreg32 (char *fpregs, int regno)
293 {
294 int i;
295
296 for (i = 0; i <= 31; i++)
297 {
298 if (regno == (FP0_REGNUM + i) || regno == -1)
299 supply_register (FP0_REGNUM + i, fpregs + (i * 4));
300 }
301
302 if (regno == FPS_REGNUM || regno == -1)
303 supply_register (FPS_REGNUM, fpregs + (32 * 4));
304 }
305
306 void
307 sparcnbsd_supply_fpreg64 (char *fpregs, int regno)
308 {
309 int i;
310
311 for (i = 0; i <= 31; i++)
312 {
313 if (regno == (FP0_REGNUM + i) || regno == -1)
314 supply_register (FP0_REGNUM + i, fpregs + (i * 4));
315 }
316
317 for (; i <= 47; i++)
318 {
319 if (regno == (FP0_REGNUM + i) || regno == -1)
320 supply_register (FP0_REGNUM + i, fpregs + (32 * 4) + (i * 8));
321 }
322
323 if (regno == FPS_REGNUM || regno == -1)
324 supply_register (FPS_REGNUM, fpregs + (32 * 4) + (16 * 8));
325
326 /* XXX %gsr */
327 }
328
329 void
330 sparcnbsd_fill_fpreg32 (char *fpregs, int regno)
331 {
332 int i;
333
334 for (i = 0; i <= 31; i++)
335 {
336 if (regno == (FP0_REGNUM + i) || regno == -1)
337 regcache_collect (FP0_REGNUM + i, fpregs + (i * 4));
338 }
339
340 if (regno == FPS_REGNUM || regno == -1)
341 regcache_collect (FPS_REGNUM, fpregs + (32 * 4));
342 }
343
344 void
345 sparcnbsd_fill_fpreg64 (char *fpregs, int regno)
346 {
347 int i;
348
349 for (i = 0; i <= 31; i++)
350 {
351 if (regno == (FP0_REGNUM + i) || regno == -1)
352 regcache_collect (FP0_REGNUM + i, fpregs + (i * 4));
353 }
354
355 for (; i <= 47; i++)
356 {
357 if (regno == (FP0_REGNUM + i) || regno == -1)
358 regcache_collect (FP0_REGNUM + i, fpregs + (32 * 4) + (i * 8));
359 }
360
361 if (regno == FPS_REGNUM || regno == -1)
362 regcache_collect (FPS_REGNUM, fpregs + (32 * 4) + (16 * 8));
363
364 /* XXX %gsr */
365 }
366
367 /* Unlike other NetBSD implementations, the SPARC port historically used
368 .reg and .reg2 (see bfd/netbsd-core.c), and as such, we can share one
369 routine for a.out and ELF core files. */
370 static void
371 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, int which,
372 CORE_ADDR ignore)
373 {
374 int reg_size, fpreg_size;
375
376 if (gdbarch_ptr_bit (current_gdbarch) == 32)
377 {
378 reg_size = (20 * 4);
379 fpreg_size = (33 * 4);
380 }
381 else
382 {
383 reg_size = (20 * 8);
384 fpreg_size = (64 * 4)
385 + 8 /* fsr */
386 + 4 /* gsr */
387 + 4; /* pad */
388 }
389
390 switch (which)
391 {
392 case 0: /* Integer registers */
393 if (core_reg_size != reg_size)
394 warning ("Wrong size register set in core file.");
395 else if (gdbarch_ptr_bit (current_gdbarch) == 32)
396 sparcnbsd_supply_reg32 (core_reg_sect, -1);
397 else
398 sparcnbsd_supply_reg64 (core_reg_sect, -1);
399 break;
400
401 case 2: /* Floating pointer registers */
402 if (core_reg_size != fpreg_size)
403 warning ("Wrong size FP register set in core file.");
404 else if (gdbarch_ptr_bit (current_gdbarch) == 32)
405 sparcnbsd_supply_fpreg32 (core_reg_sect, -1);
406 else
407 sparcnbsd_supply_fpreg64 (core_reg_sect, -1);
408 break;
409
410 default:
411 /* Don't know what kind of register request this is; just ignore it. */
412 break;
413 }
414 }
415
416 static struct core_fns sparcnbsd_core_fns =
417 {
418 bfd_target_unknown_flavour, /* core_flavour */
419 default_check_format, /* check_format */
420 default_core_sniffer, /* core_sniffer */
421 fetch_core_registers, /* core_read_registers */
422 NULL
423 };
424
425 static struct core_fns sparcnbsd_elfcore_fns =
426 {
427 bfd_target_elf_flavour, /* core_flavour */
428 default_check_format, /* check_format */
429 default_core_sniffer, /* core_sniffer */
430 fetch_core_registers, /* core_read_registers */
431 NULL
432 };
433
434 /* FIXME: Need PC_IN_SIGTRAMP() support, but NetBSD/sparc signal trampolines
435 aren't easily identified. */
436
437 static int
438 sparcnbsd_get_longjmp_target_32 (CORE_ADDR *pc)
439 {
440 CORE_ADDR jb_addr;
441 char buf[4];
442
443 jb_addr = read_register (O0_REGNUM);
444
445 if (target_read_memory (jb_addr + 12, buf, sizeof (buf)))
446 return 0;
447
448 *pc = extract_address (buf, sizeof (buf));
449
450 return 1;
451 }
452
453 static int
454 sparcnbsd_get_longjmp_target_64 (CORE_ADDR *pc)
455 {
456 CORE_ADDR jb_addr;
457 char buf[8];
458
459 jb_addr = read_register (O0_REGNUM);
460
461 if (target_read_memory (jb_addr + 16, buf, sizeof (buf)))
462 return 0;
463
464 *pc = extract_address (buf, sizeof (buf));
465
466 return 1;
467 }
468
469 static int
470 sparcnbsd_aout_in_solib_call_trampoline (CORE_ADDR pc, char *name)
471 {
472 if (strcmp (name, "_DYNAMIC") == 0)
473 return 1;
474
475 return 0;
476 }
477
478 static void
479 sparcnbsd_init_abi_common (struct gdbarch_info info,
480 struct gdbarch *gdbarch)
481 {
482 set_gdbarch_get_longjmp_target (gdbarch, gdbarch_ptr_bit (gdbarch) == 32 ?
483 sparcnbsd_get_longjmp_target_32 :
484 sparcnbsd_get_longjmp_target_64);
485 }
486
487 static void
488 sparcnbsd_init_abi_aout (struct gdbarch_info info,
489 struct gdbarch *gdbarch)
490 {
491 sparcnbsd_init_abi_common (info, gdbarch);
492
493 set_gdbarch_in_solib_call_trampoline (gdbarch,
494 sparcnbsd_aout_in_solib_call_trampoline);
495 }
496
497 static void
498 sparcnbsd_init_abi_elf (struct gdbarch_info info,
499 struct gdbarch *gdbarch)
500 {
501 sparcnbsd_init_abi_common (info, gdbarch);
502
503 set_gdbarch_pc_in_sigtramp (gdbarch, nbsd_pc_in_sigtramp);
504
505 set_solib_svr4_fetch_link_map_offsets (gdbarch,
506 gdbarch_ptr_bit (gdbarch) == 32 ?
507 nbsd_ilp32_solib_svr4_fetch_link_map_offsets :
508 nbsd_lp64_solib_svr4_fetch_link_map_offsets);
509 }
510
511 static enum gdb_osabi
512 sparcnbsd_aout_osabi_sniffer (bfd *abfd)
513 {
514 if (strcmp (bfd_get_target (abfd), "a.out-sparc-netbsd") == 0)
515 return GDB_OSABI_NETBSD_AOUT;
516
517 return GDB_OSABI_UNKNOWN;
518 }
519
520 void
521 _initialize_sparnbsd_tdep (void)
522 {
523 gdbarch_register_osabi_sniffer (bfd_arch_sparc, bfd_target_aout_flavour,
524 sparcnbsd_aout_osabi_sniffer);
525
526 gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_NETBSD_AOUT,
527 sparcnbsd_init_abi_aout);
528 gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_NETBSD_ELF,
529 sparcnbsd_init_abi_elf);
530
531 add_core_fns (&sparcnbsd_core_fns);
532 add_core_fns (&sparcnbsd_elfcore_fns);
533 }
This page took 0.044042 seconds and 4 git commands to generate.