NIOS2: Migrate from 'regset_from_core_section' to 'iterate_over_regset_sections'
[deliverable/binutils-gdb.git] / gdb / ppcobsd-tdep.c
CommitLineData
d195bc9f
MK
1/* Target-dependent code for OpenBSD/powerpc.
2
ecd75fc8 3 Copyright (C) 2004-2014 Free Software Foundation, Inc.
d195bc9f
MK
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
d195bc9f
MK
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d195bc9f
MK
19
20#include "defs.h"
21#include "arch-utils.h"
0dbe1904
MK
22#include "frame.h"
23#include "frame-unwind.h"
e6bb342a 24#include "gdbtypes.h"
d195bc9f
MK
25#include "osabi.h"
26#include "regcache.h"
27#include "regset.h"
0dbe1904 28#include "symtab.h"
18b2ae85 29#include "trad-frame.h"
d195bc9f 30
d195bc9f
MK
31#include "ppc-tdep.h"
32#include "ppcobsd-tdep.h"
33#include "solib-svr4.h"
34
35/* Register offsets from <machine/reg.h>. */
36struct ppc_reg_offsets ppcobsd_reg_offsets;
8599da2d 37struct ppc_reg_offsets ppcobsd_fpreg_offsets;
d195bc9f
MK
38\f
39
40/* Core file support. */
41
42/* Supply register REGNUM in the general-purpose register set REGSET
43 from the buffer specified by GREGS and LEN to register cache
44 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
45
46void
47ppcobsd_supply_gregset (const struct regset *regset,
48 struct regcache *regcache, int regnum,
49 const void *gregs, size_t len)
50{
51 ppc_supply_gregset (regset, regcache, regnum, gregs, len);
52 ppc_supply_fpregset (regset, regcache, regnum, gregs, len);
53}
54
55/* Collect register REGNUM in the general-purpose register set
0df8b418 56 REGSET, from register cache REGCACHE into the buffer specified by
d195bc9f
MK
57 GREGS and LEN. If REGNUM is -1, do this for all registers in
58 REGSET. */
59
60void
61ppcobsd_collect_gregset (const struct regset *regset,
62 const struct regcache *regcache, int regnum,
63 void *gregs, size_t len)
64{
65 ppc_collect_gregset (regset, regcache, regnum, gregs, len);
66 ppc_collect_fpregset (regset, regcache, regnum, gregs, len);
67}
68
5d6210f0 69/* OpenBSD/powerpc register set. */
d195bc9f 70
3ca7dae4 71const struct regset ppcobsd_gregset =
d195bc9f
MK
72{
73 &ppcobsd_reg_offsets,
74 ppcobsd_supply_gregset
75};
76
3ca7dae4 77const struct regset ppcobsd_fpregset =
8599da2d
MK
78{
79 &ppcobsd_fpreg_offsets,
80 ppc_supply_fpregset
81};
82
d195bc9f
MK
83/* Return the appropriate register set for the core section identified
84 by SECT_NAME and SECT_SIZE. */
85
86static const struct regset *
87ppcobsd_regset_from_core_section (struct gdbarch *gdbarch,
88 const char *sect_name, size_t sect_size)
89{
90 if (strcmp (sect_name, ".reg") == 0 && sect_size >= 412)
91 return &ppcobsd_gregset;
92
93 return NULL;
94}
95\f
96
18b2ae85
MK
97/* Signal trampolines. */
98
0dbe1904
MK
99/* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
100 in virtual memory. The randomness makes it somewhat tricky to
101 detect it, but fortunately we can rely on the fact that the start
102 of the sigtramp routine is page-aligned. We recognize the
103 trampoline by looking for the code that invokes the sigreturn
104 system call. The offset where we can find that code varies from
105 release to release.
106
107 By the way, the mapping mentioned above is read-only, so you cannot
108 place a breakpoint in the signal trampoline. */
109
110/* Default page size. */
111static const int ppcobsd_page_size = 4096;
112
113/* Offset for sigreturn(2). */
114static const int ppcobsd_sigreturn_offset[] = {
115 0x98, /* OpenBSD 3.8 */
116 0x0c, /* OpenBSD 3.2 */
117 -1
118};
119
120static int
1af5d7ce
UW
121ppcobsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
122 struct frame_info *this_frame,
123 void **this_cache)
0dbe1904 124{
e17a4113
UW
125 struct gdbarch *gdbarch = get_frame_arch (this_frame);
126 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1af5d7ce 127 CORE_ADDR pc = get_frame_pc (this_frame);
0dbe1904
MK
128 CORE_ADDR start_pc = (pc & ~(ppcobsd_page_size - 1));
129 const int *offset;
2c02bd72 130 const char *name;
0dbe1904
MK
131
132 find_pc_partial_function (pc, &name, NULL, NULL);
133 if (name)
134 return 0;
135
136 for (offset = ppcobsd_sigreturn_offset; *offset != -1; offset++)
137 {
138 gdb_byte buf[2 * PPC_INSN_SIZE];
139 unsigned long insn;
140
1af5d7ce 141 if (!safe_frame_unwind_memory (this_frame, start_pc + *offset,
0dbe1904
MK
142 buf, sizeof buf))
143 continue;
144
145 /* Check for "li r0,SYS_sigreturn". */
e17a4113 146 insn = extract_unsigned_integer (buf, PPC_INSN_SIZE, byte_order);
0dbe1904
MK
147 if (insn != 0x38000067)
148 continue;
149
150 /* Check for "sc". */
e17a4113
UW
151 insn = extract_unsigned_integer (buf + PPC_INSN_SIZE,
152 PPC_INSN_SIZE, byte_order);
0dbe1904
MK
153 if (insn != 0x44000002)
154 continue;
155
156 return 1;
157 }
158
159 return 0;
160}
161
162static struct trad_frame_cache *
1af5d7ce 163ppcobsd_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
18b2ae85 164{
1af5d7ce 165 struct gdbarch *gdbarch = get_frame_arch (this_frame);
18b2ae85 166 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
e17a4113 167 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
0dbe1904
MK
168 struct trad_frame_cache *cache;
169 CORE_ADDR addr, base, func;
170 gdb_byte buf[PPC_INSN_SIZE];
171 unsigned long insn, sigcontext_offset;
18b2ae85
MK
172 int i;
173
0dbe1904
MK
174 if (*this_cache)
175 return *this_cache;
176
1af5d7ce 177 cache = trad_frame_cache_zalloc (this_frame);
0dbe1904
MK
178 *this_cache = cache;
179
1af5d7ce 180 func = get_frame_pc (this_frame);
0dbe1904 181 func &= ~(ppcobsd_page_size - 1);
1af5d7ce 182 if (!safe_frame_unwind_memory (this_frame, func, buf, sizeof buf))
0dbe1904
MK
183 return cache;
184
185 /* Calculate the offset where we can find `struct sigcontext'. We
186 base our calculation on the amount of stack space reserved by the
187 first instruction of the signal trampoline. */
e17a4113 188 insn = extract_unsigned_integer (buf, PPC_INSN_SIZE, byte_order);
0dbe1904
MK
189 sigcontext_offset = (0x10000 - (insn & 0x0000ffff)) + 8;
190
1af5d7ce 191 base = get_frame_register_unsigned (this_frame, gdbarch_sp_regnum (gdbarch));
0dbe1904 192 addr = base + sigcontext_offset + 2 * tdep->wordsize;
18b2ae85
MK
193 for (i = 0; i < ppc_num_gprs; i++, addr += tdep->wordsize)
194 {
195 int regnum = i + tdep->ppc_gp0_regnum;
0dbe1904 196 trad_frame_set_reg_addr (cache, regnum, addr);
18b2ae85 197 }
0dbe1904 198 trad_frame_set_reg_addr (cache, tdep->ppc_lr_regnum, addr);
18b2ae85 199 addr += tdep->wordsize;
0dbe1904 200 trad_frame_set_reg_addr (cache, tdep->ppc_cr_regnum, addr);
18b2ae85 201 addr += tdep->wordsize;
0dbe1904 202 trad_frame_set_reg_addr (cache, tdep->ppc_xer_regnum, addr);
18b2ae85 203 addr += tdep->wordsize;
0dbe1904 204 trad_frame_set_reg_addr (cache, tdep->ppc_ctr_regnum, addr);
18b2ae85 205 addr += tdep->wordsize;
40a6adc1 206 trad_frame_set_reg_addr (cache, gdbarch_pc_regnum (gdbarch), addr);
0df8b418 207 /* SRR0? */
18b2ae85
MK
208 addr += tdep->wordsize;
209
210 /* Construct the frame ID using the function start. */
0dbe1904
MK
211 trad_frame_set_id (cache, frame_id_build (base, func));
212
213 return cache;
18b2ae85
MK
214}
215
0dbe1904 216static void
1af5d7ce 217ppcobsd_sigtramp_frame_this_id (struct frame_info *this_frame,
0dbe1904
MK
218 void **this_cache, struct frame_id *this_id)
219{
220 struct trad_frame_cache *cache =
1af5d7ce 221 ppcobsd_sigtramp_frame_cache (this_frame, this_cache);
0dbe1904
MK
222
223 trad_frame_get_id (cache, this_id);
224}
225
1af5d7ce
UW
226static struct value *
227ppcobsd_sigtramp_frame_prev_register (struct frame_info *this_frame,
228 void **this_cache, int regnum)
18b2ae85 229{
0dbe1904 230 struct trad_frame_cache *cache =
1af5d7ce 231 ppcobsd_sigtramp_frame_cache (this_frame, this_cache);
0dbe1904 232
1af5d7ce 233 return trad_frame_get_register (cache, this_frame, regnum);
0dbe1904
MK
234}
235
236static const struct frame_unwind ppcobsd_sigtramp_frame_unwind = {
18b2ae85 237 SIGTRAMP_FRAME,
8fbca658 238 default_frame_unwind_stop_reason,
0dbe1904 239 ppcobsd_sigtramp_frame_this_id,
1af5d7ce
UW
240 ppcobsd_sigtramp_frame_prev_register,
241 NULL,
242 ppcobsd_sigtramp_frame_sniffer
18b2ae85 243};
18b2ae85 244\f
5d6210f0 245
d195bc9f
MK
246static void
247ppcobsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
248{
5a49dfd0
MK
249 /* OpenBSD doesn't support the 128-bit `long double' from the psABI. */
250 set_gdbarch_long_double_bit (gdbarch, 64);
8da61cc4 251 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
5a49dfd0 252
a86c5ab2
MK
253 /* OpenBSD currently uses a broken GCC. */
254 set_gdbarch_return_value (gdbarch, ppc_sysv_abi_broken_return_value);
255
d195bc9f 256 /* OpenBSD uses SVR4-style shared libraries. */
d195bc9f
MK
257 set_solib_svr4_fetch_link_map_offsets
258 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
259
260 set_gdbarch_regset_from_core_section
261 (gdbarch, ppcobsd_regset_from_core_section);
18b2ae85 262
1af5d7ce 263 frame_unwind_append_unwinder (gdbarch, &ppcobsd_sigtramp_frame_unwind);
d195bc9f
MK
264}
265\f
266
267/* OpenBSD uses uses the traditional NetBSD core file format, even for
268 ports that use ELF. */
269#define GDB_OSABI_NETBSD_CORE GDB_OSABI_OPENBSD_ELF
270
271static enum gdb_osabi
272ppcobsd_core_osabi_sniffer (bfd *abfd)
273{
274 if (strcmp (bfd_get_target (abfd), "netbsd-core") == 0)
275 return GDB_OSABI_NETBSD_CORE;
276
277 return GDB_OSABI_UNKNOWN;
278}
279\f
280
281/* Provide a prototype to silence -Wmissing-prototypes. */
282void _initialize_ppcobsd_tdep (void);
283
284void
285_initialize_ppcobsd_tdep (void)
286{
287 /* BFD doesn't set a flavour for NetBSD style a.out core files. */
288 gdbarch_register_osabi_sniffer (bfd_arch_powerpc, bfd_target_unknown_flavour,
289 ppcobsd_core_osabi_sniffer);
290
5d6210f0
MK
291 gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_OPENBSD_ELF,
292 ppcobsd_init_abi);
d195bc9f
MK
293 gdbarch_register_osabi (bfd_arch_powerpc, 0, GDB_OSABI_OPENBSD_ELF,
294 ppcobsd_init_abi);
295
296 /* Avoid initializing the register offsets again if they were
297 already initailized by ppcobsd-nat.c. */
298 if (ppcobsd_reg_offsets.pc_offset == 0)
299 {
300 /* General-purpose registers. */
301 ppcobsd_reg_offsets.r0_offset = 0;
f2db237a
AM
302 ppcobsd_reg_offsets.gpr_size = 4;
303 ppcobsd_reg_offsets.xr_size = 4;
d195bc9f
MK
304 ppcobsd_reg_offsets.pc_offset = 384;
305 ppcobsd_reg_offsets.ps_offset = 388;
306 ppcobsd_reg_offsets.cr_offset = 392;
307 ppcobsd_reg_offsets.lr_offset = 396;
308 ppcobsd_reg_offsets.ctr_offset = 400;
309 ppcobsd_reg_offsets.xer_offset = 404;
310 ppcobsd_reg_offsets.mq_offset = 408;
311
312 /* Floating-point registers. */
313 ppcobsd_reg_offsets.f0_offset = 128;
314 ppcobsd_reg_offsets.fpscr_offset = -1;
315
316 /* AltiVec registers. */
317 ppcobsd_reg_offsets.vr0_offset = 0;
318 ppcobsd_reg_offsets.vscr_offset = 512;
319 ppcobsd_reg_offsets.vrsave_offset = 520;
320 }
8599da2d
MK
321
322 if (ppcobsd_fpreg_offsets.fpscr_offset == 0)
323 {
324 /* Floating-point registers. */
325 ppcobsd_reg_offsets.f0_offset = 0;
326 ppcobsd_reg_offsets.fpscr_offset = 256;
f2db237a 327 ppcobsd_reg_offsets.fpscr_size = 4;
8599da2d 328 }
d195bc9f 329}
This page took 0.965117 seconds and 4 git commands to generate.