]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/sparcnbsd-tdep.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / sparcnbsd-tdep.c
CommitLineData
386c036b
MK
1/* Target-dependent code for NetBSD/sparc.
2
6aba47ca 3 Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
9ce5c36a
JT
4 Contributed by Wasabi Systems, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
197e01b6
EZ
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
9ce5c36a
JT
22
23#include "defs.h"
386c036b
MK
24#include "floatformat.h"
25#include "frame.h"
26#include "frame-unwind.h"
9ce5c36a 27#include "gdbcore.h"
9ce5c36a 28#include "osabi.h"
386c036b 29#include "regcache.h"
4e7b0cd3 30#include "regset.h"
386c036b
MK
31#include "solib-svr4.h"
32#include "symtab.h"
33#include "trad-frame.h"
9ce5c36a 34
386c036b 35#include "gdb_assert.h"
f6ad61e3
MK
36#include "gdb_string.h"
37
c139e7d9 38#include "sparc-tdep.h"
9ce5c36a
JT
39#include "nbsd-tdep.h"
40
c893be75
MK
41/* Macros to extract fields from SPARC instructions. */
42#define X_RS1(i) (((i) >> 14) & 0x1f)
43#define X_RS2(i) ((i) & 0x1f)
44#define X_I(i) (((i) >> 13) & 1)
45
386c036b 46const struct sparc_gregset sparc32nbsd_gregset =
9ce5c36a 47{
386c036b
MK
48 0 * 4, /* %psr */
49 1 * 4, /* %pc */
50 2 * 4, /* %npc */
51 3 * 4, /* %y */
52 -1, /* %wim */
53 -1, /* %tbr */
54 5 * 4, /* %g1 */
55 -1 /* %l0 */
56};
9ce5c36a 57
9ce5c36a 58static void
4e7b0cd3
MK
59sparc32nbsd_supply_gregset (const struct regset *regset,
60 struct regcache *regcache,
61 int regnum, const void *gregs, size_t len)
9ce5c36a 62{
9ea75c57 63 sparc32_supply_gregset (&sparc32nbsd_gregset, regcache, regnum, gregs);
c8e737d5
MK
64
65 /* Traditional NetBSD core files don't use multiple register sets.
66 Instead, the general-purpose and floating-point registers are
67 lumped together in a single section. */
68 if (len >= 212)
69 sparc32_supply_fpregset (regcache, regnum, (const char *) gregs + 80);
9ce5c36a
JT
70}
71
4e7b0cd3
MK
72static void
73sparc32nbsd_supply_fpregset (const struct regset *regset,
74 struct regcache *regcache,
75 int regnum, const void *fpregs, size_t len)
9ce5c36a 76{
4e7b0cd3
MK
77 sparc32_supply_fpregset (regcache, regnum, fpregs);
78}
9ce5c36a 79
386c036b
MK
80\f
81/* Signal trampolines. */
82
83/* The following variables describe the location of an on-stack signal
84 trampoline. The current values correspond to the memory layout for
85 NetBSD 1.3 and up. These shouldn't be necessary for NetBSD 2.0 and
86 up, since NetBSD uses signal trampolines provided by libc now. */
9ce5c36a 87
386c036b
MK
88static const CORE_ADDR sparc32nbsd_sigtramp_start = 0xeffffef0;
89static const CORE_ADDR sparc32nbsd_sigtramp_end = 0xeffffff0;
9ce5c36a
JT
90
91static int
386c036b
MK
92sparc32nbsd_pc_in_sigtramp (CORE_ADDR pc, char *name)
93{
94 if (pc >= sparc32nbsd_sigtramp_start && pc < sparc32nbsd_sigtramp_end)
95 return 1;
96
97 return nbsd_pc_in_sigtramp (pc, name);
98}
99
566626fa
MK
100struct trad_frame_saved_reg *
101sparc32nbsd_sigcontext_saved_regs (struct frame_info *next_frame)
9ce5c36a 102{
566626fa 103 struct trad_frame_saved_reg *saved_regs;
386c036b 104 CORE_ADDR addr, sigcontext_addr;
386c036b 105 int regnum, delta;
566626fa 106 ULONGEST psr;
9ce5c36a 107
566626fa 108 saved_regs = trad_frame_alloc_saved_regs (next_frame);
9ce5c36a 109
566626fa
MK
110 /* We find the appropriate instance of `struct sigcontext' at a
111 fixed offset in the signal frame. */
112 addr = frame_unwind_register_unsigned (next_frame, SPARC_FP_REGNUM);
113 sigcontext_addr = addr + 64 + 16;
9ce5c36a 114
386c036b
MK
115 /* The registers are saved in bits and pieces scattered all over the
116 place. The code below records their location on the assumption
117 that the part of the signal trampoline that saves the state has
118 been executed. */
9ce5c36a 119
566626fa
MK
120 saved_regs[SPARC_SP_REGNUM].addr = sigcontext_addr + 8;
121 saved_regs[SPARC32_PC_REGNUM].addr = sigcontext_addr + 12;
122 saved_regs[SPARC32_NPC_REGNUM].addr = sigcontext_addr + 16;
123 saved_regs[SPARC32_PSR_REGNUM].addr = sigcontext_addr + 20;
124 saved_regs[SPARC_G1_REGNUM].addr = sigcontext_addr + 24;
125 saved_regs[SPARC_O0_REGNUM].addr = sigcontext_addr + 28;
386c036b
MK
126
127 /* The remaining `global' registers and %y are saved in the `local'
128 registers. */
129 delta = SPARC_L0_REGNUM - SPARC_G0_REGNUM;
130 for (regnum = SPARC_G2_REGNUM; regnum <= SPARC_G7_REGNUM; regnum++)
566626fa
MK
131 saved_regs[regnum].realreg = regnum + delta;
132 saved_regs[SPARC32_Y_REGNUM].realreg = SPARC_L1_REGNUM;
386c036b
MK
133
134 /* The remaining `out' registers can be found in the current frame's
135 `in' registers. */
136 delta = SPARC_I0_REGNUM - SPARC_O0_REGNUM;
137 for (regnum = SPARC_O1_REGNUM; regnum <= SPARC_O5_REGNUM; regnum++)
566626fa
MK
138 saved_regs[regnum].realreg = regnum + delta;
139 saved_regs[SPARC_O7_REGNUM].realreg = SPARC_I7_REGNUM;
386c036b
MK
140
141 /* The `local' and `in' registers have been saved in the register
142 save area. */
566626fa 143 addr = saved_regs[SPARC_SP_REGNUM].addr;
386c036b
MK
144 addr = get_frame_memory_unsigned (next_frame, addr, 4);
145 for (regnum = SPARC_L0_REGNUM;
146 regnum <= SPARC_I7_REGNUM; regnum++, addr += 4)
566626fa 147 saved_regs[regnum].addr = addr;
386c036b 148
1c800673
MK
149 /* Handle StackGhost. */
150 {
151 ULONGEST wcookie = sparc_fetch_wcookie ();
152
153 if (wcookie != 0)
154 {
155 ULONGEST i7;
156
157 addr = saved_regs[SPARC_I7_REGNUM].addr;
158 i7 = get_frame_memory_unsigned (next_frame, addr, 4);
159 trad_frame_set_value (saved_regs, SPARC_I7_REGNUM, i7 ^ wcookie);
160 }
161 }
162
386c036b
MK
163 /* The floating-point registers are only saved if the EF bit in %prs
164 has been set. */
165
166#define PSR_EF 0x00001000
167
566626fa 168 addr = saved_regs[SPARC32_PSR_REGNUM].addr;
386c036b
MK
169 psr = get_frame_memory_unsigned (next_frame, addr, 4);
170 if (psr & PSR_EF)
171 {
172 CORE_ADDR sp;
173
174 sp = frame_unwind_register_unsigned (next_frame, SPARC_SP_REGNUM);
566626fa 175 saved_regs[SPARC32_FSR_REGNUM].addr = sp + 96;
386c036b
MK
176 for (regnum = SPARC_F0_REGNUM, addr = sp + 96 + 8;
177 regnum <= SPARC_F31_REGNUM; regnum++, addr += 4)
566626fa
MK
178 saved_regs[regnum].addr = addr;
179 }
180
181 return saved_regs;
182}
183
184static struct sparc_frame_cache *
185sparc32nbsd_sigcontext_frame_cache (struct frame_info *next_frame,
186 void **this_cache)
187{
188 struct sparc_frame_cache *cache;
189 CORE_ADDR addr;
190
191 if (*this_cache)
192 return *this_cache;
193
194 cache = sparc_frame_cache (next_frame, this_cache);
195 gdb_assert (cache == *this_cache);
196
197 /* If we couldn't find the frame's function, we're probably dealing
198 with an on-stack signal trampoline. */
199 if (cache->pc == 0)
200 {
201 cache->pc = sparc32nbsd_sigtramp_start;
202
203 /* Since we couldn't find the frame's function, the cache was
204 initialized under the assumption that we're frameless. */
205 cache->frameless_p = 0;
206 addr = frame_unwind_register_unsigned (next_frame, SPARC_FP_REGNUM);
207 cache->base = addr;
386c036b
MK
208 }
209
566626fa
MK
210 cache->saved_regs = sparc32nbsd_sigcontext_saved_regs (next_frame);
211
386c036b 212 return cache;
9ce5c36a
JT
213}
214
386c036b
MK
215static void
216sparc32nbsd_sigcontext_frame_this_id (struct frame_info *next_frame,
217 void **this_cache,
218 struct frame_id *this_id)
219{
220 struct sparc_frame_cache *cache =
221 sparc32nbsd_sigcontext_frame_cache (next_frame, this_cache);
222
223 (*this_id) = frame_id_build (cache->base, cache->pc);
224}
225
226static void
227sparc32nbsd_sigcontext_frame_prev_register (struct frame_info *next_frame,
228 void **this_cache,
229 int regnum, int *optimizedp,
230 enum lval_type *lvalp,
231 CORE_ADDR *addrp,
47ef841b 232 int *realnump, gdb_byte *valuep)
9ce5c36a 233{
386c036b
MK
234 struct sparc_frame_cache *cache =
235 sparc32nbsd_sigcontext_frame_cache (next_frame, this_cache);
9ce5c36a 236
1f67027d
AC
237 trad_frame_get_prev_register (next_frame, cache->saved_regs, regnum,
238 optimizedp, lvalp, addrp, realnump, valuep);
386c036b 239}
9ce5c36a 240
386c036b
MK
241static const struct frame_unwind sparc32nbsd_sigcontext_frame_unwind =
242{
243 SIGTRAMP_FRAME,
244 sparc32nbsd_sigcontext_frame_this_id,
245 sparc32nbsd_sigcontext_frame_prev_register
246};
9ce5c36a 247
386c036b
MK
248static const struct frame_unwind *
249sparc32nbsd_sigtramp_frame_sniffer (struct frame_info *next_frame)
250{
251 CORE_ADDR pc = frame_pc_unwind (next_frame);
252 char *name;
9ce5c36a 253
386c036b
MK
254 find_pc_partial_function (pc, &name, NULL, NULL);
255 if (sparc32nbsd_pc_in_sigtramp (pc, name))
256 {
257 if (name == NULL || strncmp (name, "__sigtramp_sigcontext", 21))
258 return &sparc32nbsd_sigcontext_frame_unwind;
259 }
260
261 return NULL;
9ce5c36a 262}
386c036b 263\f
c893be75
MK
264/* Return the address of a system call's alternative return
265 address. */
266
267CORE_ADDR
268sparcnbsd_step_trap (unsigned long insn)
269{
270 if ((X_I (insn) == 0 && X_RS1 (insn) == 0 && X_RS2 (insn) == 0)
271 || (X_I (insn) == 1 && X_RS1 (insn) == 0 && (insn & 0x7f) == 0))
272 {
273 /* "New" system call. */
274 ULONGEST number;
275
276 regcache_cooked_read_unsigned (current_regcache,
277 SPARC_G1_REGNUM, &number);
278
279 if (number & 0x400)
280 return sparc_address_from_register (SPARC_G2_REGNUM);
281 if (number & 0x800)
282 return sparc_address_from_register (SPARC_G7_REGNUM);
283 }
284
285 return 0;
286}
287\f
386c036b 288
9ce5c36a 289static void
386c036b 290sparc32nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
9ce5c36a 291{
4e7b0cd3
MK
292 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
293
386c036b
MK
294 /* NetBSD doesn't support the 128-bit `long double' from the psABI. */
295 set_gdbarch_long_double_bit (gdbarch, 64);
296 set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_double_big);
297
9ea75c57 298 tdep->gregset = regset_alloc (gdbarch, sparc32nbsd_supply_gregset, NULL);
c8e737d5 299 tdep->sizeof_gregset = 20 * 4;
4e7b0cd3 300
9ea75c57 301 tdep->fpregset = regset_alloc (gdbarch, sparc32nbsd_supply_fpregset, NULL);
c8e737d5 302 tdep->sizeof_fpregset = 33 * 4;
4e7b0cd3 303
c893be75
MK
304 /* Make sure we can single-step "new" syscalls. */
305 tdep->step_trap = sparcnbsd_step_trap;
306
386c036b 307 frame_unwind_append_sniffer (gdbarch, sparc32nbsd_sigtramp_frame_sniffer);
9ce5c36a
JT
308}
309
310static void
386c036b 311sparc32nbsd_aout_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
9ce5c36a 312{
386c036b 313 sparc32nbsd_init_abi (info, gdbarch);
9ce5c36a
JT
314}
315
19671c2b 316void
386c036b 317sparc32nbsd_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
9ce5c36a 318{
386c036b 319 sparc32nbsd_init_abi (info, gdbarch);
9ce5c36a 320
386c036b 321 set_solib_svr4_fetch_link_map_offsets
be24b061 322 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
9ce5c36a
JT
323}
324
325static enum gdb_osabi
326sparcnbsd_aout_osabi_sniffer (bfd *abfd)
327{
328 if (strcmp (bfd_get_target (abfd), "a.out-sparc-netbsd") == 0)
329 return GDB_OSABI_NETBSD_AOUT;
330
331 return GDB_OSABI_UNKNOWN;
332}
333
7e5e9f88
MK
334/* OpenBSD uses the traditional NetBSD core file format, even for
335 ports that use ELF. Therefore, if the default OS ABI is OpenBSD
336 ELF, we return that instead of NetBSD a.out. This is mainly for
337 the benfit of OpenBSD/sparc64, which inherits the sniffer below
338 since we include this file for an OpenBSD/sparc64 target. For
339 OpenBSD/sparc, the NetBSD a.out OS ABI is probably similar enough
340 to both the OpenBSD a.out and the OpenBSD ELF OS ABI. */
341#if defined (GDB_OSABI_DEFAULT) && (GDB_OSABI_DEFAULT == GDB_OSABI_OPENBSD_ELF)
342#define GDB_OSABI_NETBSD_CORE GDB_OSABI_OPENBSD_ELF
343#else
344#define GDB_OSABI_NETBSD_CORE GDB_OSABI_NETBSD_AOUT
345#endif
346
89aac506
MK
347static enum gdb_osabi
348sparcnbsd_core_osabi_sniffer (bfd *abfd)
349{
350 if (strcmp (bfd_get_target (abfd), "netbsd-core") == 0)
7e5e9f88 351 return GDB_OSABI_NETBSD_CORE;
89aac506
MK
352
353 return GDB_OSABI_UNKNOWN;
354}
355
386c036b
MK
356\f
357/* Provide a prototype to silence -Wmissing-prototypes. */
358void _initialize_sparcnbsd_tdep (void);
359
9ce5c36a
JT
360void
361_initialize_sparnbsd_tdep (void)
362{
363 gdbarch_register_osabi_sniffer (bfd_arch_sparc, bfd_target_aout_flavour,
364 sparcnbsd_aout_osabi_sniffer);
365
adf93a2f
MK
366 /* BFD doesn't set a flavour for NetBSD style a.out core files. */
367 gdbarch_register_osabi_sniffer (bfd_arch_sparc, bfd_target_unknown_flavour,
89aac506
MK
368 sparcnbsd_core_osabi_sniffer);
369
05816f70 370 gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_NETBSD_AOUT,
386c036b 371 sparc32nbsd_aout_init_abi);
05816f70 372 gdbarch_register_osabi (bfd_arch_sparc, 0, GDB_OSABI_NETBSD_ELF,
386c036b 373 sparc32nbsd_elf_init_abi);
9ce5c36a 374}