Automatic date update in version.in
[binutils-gdb.git] / gdb / sparc64-netbsd-tdep.c
blobb101f4970d90e91b423d869eeb9870be773cb648
1 /* Target-dependent code for NetBSD/sparc64.
3 Copyright (C) 2002-2024 Free Software Foundation, Inc.
4 Based on code contributed by Wasabi Systems, Inc.
6 This file is part of GDB.
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "frame.h"
22 #include "frame-unwind.h"
23 #include "gdbcore.h"
24 #include "osabi.h"
25 #include "regcache.h"
26 #include "regset.h"
27 #include "symtab.h"
28 #include "objfiles.h"
29 #include "solib-svr4.h"
30 #include "trad-frame.h"
32 #include "sparc64-tdep.h"
33 #include "netbsd-tdep.h"
35 /* From <machine/reg.h>. */
36 const struct sparc_gregmap sparc64nbsd_gregmap =
38 0 * 8, /* "tstate" */
39 1 * 8, /* %pc */
40 2 * 8, /* %npc */
41 3 * 8, /* %y */
42 -1, /* %fprs */
43 -1,
44 5 * 8, /* %g1 */
45 -1, /* %l0 */
46 4 /* sizeof (%y) */
50 static void
51 sparc64nbsd_supply_gregset (const struct regset *regset,
52 struct regcache *regcache,
53 int regnum, const void *gregs, size_t len)
55 sparc64_supply_gregset (&sparc64nbsd_gregmap, regcache, regnum, gregs);
58 static void
59 sparc64nbsd_supply_fpregset (const struct regset *regset,
60 struct regcache *regcache,
61 int regnum, const void *fpregs, size_t len)
63 sparc64_supply_fpregset (&sparc64_bsd_fpregmap, regcache, regnum, fpregs);
67 /* Signal trampolines. */
69 /* The following variables describe the location of an on-stack signal
70 trampoline. The current values correspond to the memory layout for
71 NetBSD 1.3 and up. These shouldn't be necessary for NetBSD 2.0 and
72 up, since NetBSD uses signal trampolines provided by libc now. */
74 static const CORE_ADDR sparc64nbsd_sigtramp_start = 0xffffffffffffdee4ULL;
75 static const CORE_ADDR sparc64nbsd_sigtramp_end = 0xffffffffffffe000ULL;
77 static int
78 sparc64nbsd_pc_in_sigtramp (CORE_ADDR pc, const char *name)
80 if (pc >= sparc64nbsd_sigtramp_start && pc < sparc64nbsd_sigtramp_end)
81 return 1;
83 return nbsd_pc_in_sigtramp (pc, name);
86 trad_frame_saved_reg *
87 sparc64nbsd_sigcontext_saved_regs (CORE_ADDR sigcontext_addr,
88 const frame_info_ptr &this_frame)
90 struct gdbarch *gdbarch = get_frame_arch (this_frame);
91 trad_frame_saved_reg *saved_regs;
92 CORE_ADDR addr, sp;
93 int regnum, delta;
95 saved_regs = trad_frame_alloc_saved_regs (this_frame);
97 /* The registers are saved in bits and pieces scattered all over the
98 place. The code below records their location on the assumption
99 that the part of the signal trampoline that saves the state has
100 been executed. */
102 saved_regs[SPARC_SP_REGNUM].set_addr (sigcontext_addr + 8);
103 saved_regs[SPARC64_PC_REGNUM].set_addr (sigcontext_addr + 16);
104 saved_regs[SPARC64_NPC_REGNUM].set_addr (sigcontext_addr + 24);
105 saved_regs[SPARC64_STATE_REGNUM].set_addr (sigcontext_addr + 32);
106 saved_regs[SPARC_G1_REGNUM].set_addr (sigcontext_addr + 40);
107 saved_regs[SPARC_O0_REGNUM].set_addr (sigcontext_addr + 48);
109 /* The remaining `global' registers and %y are saved in the `local'
110 registers. */
111 delta = SPARC_L0_REGNUM - SPARC_G0_REGNUM;
112 for (regnum = SPARC_G2_REGNUM; regnum <= SPARC_G7_REGNUM; regnum++)
113 saved_regs[regnum].set_realreg (regnum + delta);
114 saved_regs[SPARC64_Y_REGNUM].set_realreg (SPARC_L1_REGNUM);
116 /* The remaining `out' registers can be found in the current frame's
117 `in' registers. */
118 delta = SPARC_I0_REGNUM - SPARC_O0_REGNUM;
119 for (regnum = SPARC_O1_REGNUM; regnum <= SPARC_O5_REGNUM; regnum++)
120 saved_regs[regnum].set_realreg (regnum + delta);
121 saved_regs[SPARC_O7_REGNUM].set_realreg (SPARC_I7_REGNUM);
123 /* The `local' and `in' registers have been saved in the register
124 save area. */
125 addr = saved_regs[SPARC_SP_REGNUM].addr ();
126 sp = get_frame_memory_unsigned (this_frame, addr, 8);
127 for (regnum = SPARC_L0_REGNUM, addr = sp + BIAS;
128 regnum <= SPARC_I7_REGNUM; regnum++, addr += 8)
129 saved_regs[regnum].set_addr (addr);
131 /* Handle StackGhost. */
133 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
135 if (wcookie != 0)
137 ULONGEST i7;
139 addr = saved_regs[SPARC_I7_REGNUM].addr ();
140 i7 = get_frame_memory_unsigned (this_frame, addr, 8);
141 saved_regs[SPARC_I7_REGNUM].set_value (i7 ^ wcookie);
145 /* TODO: Handle the floating-point registers. */
147 return saved_regs;
150 static struct sparc_frame_cache *
151 sparc64nbsd_sigcontext_frame_cache (const frame_info_ptr &this_frame,
152 void **this_cache)
154 struct sparc_frame_cache *cache;
155 CORE_ADDR addr;
157 if (*this_cache)
158 return (struct sparc_frame_cache *) *this_cache;
160 cache = sparc_frame_cache (this_frame, this_cache);
161 gdb_assert (cache == *this_cache);
163 /* If we couldn't find the frame's function, we're probably dealing
164 with an on-stack signal trampoline. */
165 if (cache->pc == 0)
167 cache->pc = sparc64nbsd_sigtramp_start;
169 /* Since we couldn't find the frame's function, the cache was
170 initialized under the assumption that we're frameless. */
171 sparc_record_save_insn (cache);
172 addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
173 if (addr & 1)
174 addr += BIAS;
175 cache->base = addr;
178 /* We find the appropriate instance of `struct sigcontext' at a
179 fixed offset in the signal frame. */
180 addr = cache->base + 128 + 8;
181 cache->saved_regs = sparc64nbsd_sigcontext_saved_regs (addr, this_frame);
183 return cache;
186 static void
187 sparc64nbsd_sigcontext_frame_this_id (const frame_info_ptr &this_frame,
188 void **this_cache,
189 struct frame_id *this_id)
191 struct sparc_frame_cache *cache =
192 sparc64nbsd_sigcontext_frame_cache (this_frame, this_cache);
194 (*this_id) = frame_id_build (cache->base, cache->pc);
197 static struct value *
198 sparc64nbsd_sigcontext_frame_prev_register (const frame_info_ptr &this_frame,
199 void **this_cache, int regnum)
201 struct sparc_frame_cache *cache =
202 sparc64nbsd_sigcontext_frame_cache (this_frame, this_cache);
204 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
207 static int
208 sparc64nbsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
209 const frame_info_ptr &this_frame,
210 void **this_cache)
212 CORE_ADDR pc = get_frame_pc (this_frame);
213 const char *name;
215 find_pc_partial_function (pc, &name, NULL, NULL);
216 if (sparc64nbsd_pc_in_sigtramp (pc, name))
218 if (name == NULL || !startswith (name, "__sigtramp_sigcontext"))
219 return 1;
222 return 0;
225 static const struct frame_unwind sparc64nbsd_sigcontext_frame_unwind =
227 "sparc64 netbsd sigcontext",
228 SIGTRAMP_FRAME,
229 default_frame_unwind_stop_reason,
230 sparc64nbsd_sigcontext_frame_this_id,
231 sparc64nbsd_sigcontext_frame_prev_register,
232 NULL,
233 sparc64nbsd_sigtramp_frame_sniffer
237 static const struct regset sparc64nbsd_gregset =
239 NULL, sparc64nbsd_supply_gregset, NULL
242 static const struct regset sparc64nbsd_fpregset =
244 NULL, sparc64nbsd_supply_fpregset, NULL
247 static void
248 sparc64nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
250 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
252 nbsd_init_abi (info, gdbarch);
254 tdep->gregset = &sparc64nbsd_gregset;
255 tdep->sizeof_gregset = 160;
257 tdep->fpregset = &sparc64nbsd_fpregset;
258 tdep->sizeof_fpregset = 272;
260 /* Make sure we can single-step "new" syscalls. */
261 tdep->step_trap = sparcnbsd_step_trap;
263 frame_unwind_append_unwinder (gdbarch, &sparc64nbsd_sigcontext_frame_unwind);
265 sparc64_init_abi (info, gdbarch);
267 /* NetBSD/sparc64 has SVR4-style shared libraries. */
268 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
269 set_solib_svr4_fetch_link_map_offsets
270 (gdbarch, svr4_lp64_fetch_link_map_offsets);
273 void _initialize_sparc64nbsd_tdep ();
274 void
275 _initialize_sparc64nbsd_tdep ()
277 gdbarch_register_osabi (bfd_arch_sparc, bfd_mach_sparc_v9,
278 GDB_OSABI_NETBSD, sparc64nbsd_init_abi);