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