1 /* Target-dependent code for OpenBSD/powerpc.
3 Copyright (C) 2004-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "arch-utils.h"
23 #include "frame-unwind.h"
29 #include "trad-frame.h"
34 #include "ppcobsd-tdep.h"
35 #include "solib-svr4.h"
37 /* Register offsets from <machine/reg.h>. */
38 struct ppc_reg_offsets ppcobsd_reg_offsets;
39 struct ppc_reg_offsets ppcobsd_fpreg_offsets;
42 /* Core file support. */
44 /* Supply register REGNUM in the general-purpose register set REGSET
45 from the buffer specified by GREGS and LEN to register cache
46 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
49 ppcobsd_supply_gregset (const struct regset *regset,
50 struct regcache *regcache, int regnum,
51 const void *gregs, size_t len)
53 ppc_supply_gregset (regset, regcache, regnum, gregs, len);
54 ppc_supply_fpregset (regset, regcache, regnum, gregs, len);
57 /* Collect register REGNUM in the general-purpose register set
58 REGSET, from register cache REGCACHE into the buffer specified by
59 GREGS and LEN. If REGNUM is -1, do this for all registers in
63 ppcobsd_collect_gregset (const struct regset *regset,
64 const struct regcache *regcache, int regnum,
65 void *gregs, size_t len)
67 ppc_collect_gregset (regset, regcache, regnum, gregs, len);
68 ppc_collect_fpregset (regset, regcache, regnum, gregs, len);
71 /* OpenBSD/powerpc register set. */
73 const struct regset ppcobsd_gregset =
76 ppcobsd_supply_gregset
79 const struct regset ppcobsd_fpregset =
81 &ppcobsd_fpreg_offsets,
85 /* Return the appropriate register set for the core section identified
86 by SECT_NAME and SECT_SIZE. */
88 static const struct regset *
89 ppcobsd_regset_from_core_section (struct gdbarch *gdbarch,
90 const char *sect_name, size_t sect_size)
92 if (strcmp (sect_name, ".reg") == 0 && sect_size >= 412)
93 return &ppcobsd_gregset;
99 /* Signal trampolines. */
101 /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
102 in virtual memory. The randomness makes it somewhat tricky to
103 detect it, but fortunately we can rely on the fact that the start
104 of the sigtramp routine is page-aligned. We recognize the
105 trampoline by looking for the code that invokes the sigreturn
106 system call. The offset where we can find that code varies from
109 By the way, the mapping mentioned above is read-only, so you cannot
110 place a breakpoint in the signal trampoline. */
112 /* Default page size. */
113 static const int ppcobsd_page_size = 4096;
115 /* Offset for sigreturn(2). */
116 static const int ppcobsd_sigreturn_offset[] = {
117 0x98, /* OpenBSD 3.8 */
118 0x0c, /* OpenBSD 3.2 */
123 ppcobsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
124 struct frame_info *this_frame,
127 struct gdbarch *gdbarch = get_frame_arch (this_frame);
128 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
129 CORE_ADDR pc = get_frame_pc (this_frame);
130 CORE_ADDR start_pc = (pc & ~(ppcobsd_page_size - 1));
134 find_pc_partial_function (pc, &name, NULL, NULL);
138 for (offset = ppcobsd_sigreturn_offset; *offset != -1; offset++)
140 gdb_byte buf[2 * PPC_INSN_SIZE];
143 if (!safe_frame_unwind_memory (this_frame, start_pc + *offset,
147 /* Check for "li r0,SYS_sigreturn". */
148 insn = extract_unsigned_integer (buf, PPC_INSN_SIZE, byte_order);
149 if (insn != 0x38000067)
152 /* Check for "sc". */
153 insn = extract_unsigned_integer (buf + PPC_INSN_SIZE,
154 PPC_INSN_SIZE, byte_order);
155 if (insn != 0x44000002)
164 static struct trad_frame_cache *
165 ppcobsd_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
167 struct gdbarch *gdbarch = get_frame_arch (this_frame);
168 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
169 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
170 struct trad_frame_cache *cache;
171 CORE_ADDR addr, base, func;
172 gdb_byte buf[PPC_INSN_SIZE];
173 unsigned long insn, sigcontext_offset;
179 cache = trad_frame_cache_zalloc (this_frame);
182 func = get_frame_pc (this_frame);
183 func &= ~(ppcobsd_page_size - 1);
184 if (!safe_frame_unwind_memory (this_frame, func, buf, sizeof buf))
187 /* Calculate the offset where we can find `struct sigcontext'. We
188 base our calculation on the amount of stack space reserved by the
189 first instruction of the signal trampoline. */
190 insn = extract_unsigned_integer (buf, PPC_INSN_SIZE, byte_order);
191 sigcontext_offset = (0x10000 - (insn & 0x0000ffff)) + 8;
193 base = get_frame_register_unsigned (this_frame, gdbarch_sp_regnum (gdbarch));
194 addr = base + sigcontext_offset + 2 * tdep->wordsize;
195 for (i = 0; i < ppc_num_gprs; i++, addr += tdep->wordsize)
197 int regnum = i + tdep->ppc_gp0_regnum;
198 trad_frame_set_reg_addr (cache, regnum, addr);
200 trad_frame_set_reg_addr (cache, tdep->ppc_lr_regnum, addr);
201 addr += tdep->wordsize;
202 trad_frame_set_reg_addr (cache, tdep->ppc_cr_regnum, addr);
203 addr += tdep->wordsize;
204 trad_frame_set_reg_addr (cache, tdep->ppc_xer_regnum, addr);
205 addr += tdep->wordsize;
206 trad_frame_set_reg_addr (cache, tdep->ppc_ctr_regnum, addr);
207 addr += tdep->wordsize;
208 trad_frame_set_reg_addr (cache, gdbarch_pc_regnum (gdbarch), addr);
210 addr += tdep->wordsize;
212 /* Construct the frame ID using the function start. */
213 trad_frame_set_id (cache, frame_id_build (base, func));
219 ppcobsd_sigtramp_frame_this_id (struct frame_info *this_frame,
220 void **this_cache, struct frame_id *this_id)
222 struct trad_frame_cache *cache =
223 ppcobsd_sigtramp_frame_cache (this_frame, this_cache);
225 trad_frame_get_id (cache, this_id);
228 static struct value *
229 ppcobsd_sigtramp_frame_prev_register (struct frame_info *this_frame,
230 void **this_cache, int regnum)
232 struct trad_frame_cache *cache =
233 ppcobsd_sigtramp_frame_cache (this_frame, this_cache);
235 return trad_frame_get_register (cache, this_frame, regnum);
238 static const struct frame_unwind ppcobsd_sigtramp_frame_unwind = {
240 default_frame_unwind_stop_reason,
241 ppcobsd_sigtramp_frame_this_id,
242 ppcobsd_sigtramp_frame_prev_register,
244 ppcobsd_sigtramp_frame_sniffer
249 ppcobsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
251 /* OpenBSD doesn't support the 128-bit `long double' from the psABI. */
252 set_gdbarch_long_double_bit (gdbarch, 64);
253 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
255 /* OpenBSD currently uses a broken GCC. */
256 set_gdbarch_return_value (gdbarch, ppc_sysv_abi_broken_return_value);
258 /* OpenBSD uses SVR4-style shared libraries. */
259 set_solib_svr4_fetch_link_map_offsets
260 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
262 set_gdbarch_regset_from_core_section
263 (gdbarch, ppcobsd_regset_from_core_section);
265 frame_unwind_append_unwinder (gdbarch, &ppcobsd_sigtramp_frame_unwind);
269 /* OpenBSD uses uses the traditional NetBSD core file format, even for
270 ports that use ELF. */
271 #define GDB_OSABI_NETBSD_CORE GDB_OSABI_OPENBSD_ELF
273 static enum gdb_osabi
274 ppcobsd_core_osabi_sniffer (bfd *abfd)
276 if (strcmp (bfd_get_target (abfd), "netbsd-core") == 0)
277 return GDB_OSABI_NETBSD_CORE;
279 return GDB_OSABI_UNKNOWN;
283 /* Provide a prototype to silence -Wmissing-prototypes. */
284 void _initialize_ppcobsd_tdep (void);
287 _initialize_ppcobsd_tdep (void)
289 /* BFD doesn't set a flavour for NetBSD style a.out core files. */
290 gdbarch_register_osabi_sniffer (bfd_arch_powerpc, bfd_target_unknown_flavour,
291 ppcobsd_core_osabi_sniffer);
293 gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_OPENBSD_ELF,
295 gdbarch_register_osabi (bfd_arch_powerpc, 0, GDB_OSABI_OPENBSD_ELF,
298 /* Avoid initializing the register offsets again if they were
299 already initailized by ppcobsd-nat.c. */
300 if (ppcobsd_reg_offsets.pc_offset == 0)
302 /* General-purpose registers. */
303 ppcobsd_reg_offsets.r0_offset = 0;
304 ppcobsd_reg_offsets.gpr_size = 4;
305 ppcobsd_reg_offsets.xr_size = 4;
306 ppcobsd_reg_offsets.pc_offset = 384;
307 ppcobsd_reg_offsets.ps_offset = 388;
308 ppcobsd_reg_offsets.cr_offset = 392;
309 ppcobsd_reg_offsets.lr_offset = 396;
310 ppcobsd_reg_offsets.ctr_offset = 400;
311 ppcobsd_reg_offsets.xer_offset = 404;
312 ppcobsd_reg_offsets.mq_offset = 408;
314 /* Floating-point registers. */
315 ppcobsd_reg_offsets.f0_offset = 128;
316 ppcobsd_reg_offsets.fpscr_offset = -1;
318 /* AltiVec registers. */
319 ppcobsd_reg_offsets.vr0_offset = 0;
320 ppcobsd_reg_offsets.vscr_offset = 512;
321 ppcobsd_reg_offsets.vrsave_offset = 520;
324 if (ppcobsd_fpreg_offsets.fpscr_offset == 0)
326 /* Floating-point registers. */
327 ppcobsd_reg_offsets.f0_offset = 0;
328 ppcobsd_reg_offsets.fpscr_offset = 256;
329 ppcobsd_reg_offsets.fpscr_size = 4;