Automatic date update in version.in
[platform/upstream/binutils.git] / gdb / ppcfbsd-tdep.c
1 /* Target-dependent code for PowerPC systems running FreeBSD.
2
3    Copyright (C) 2013-2014 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 "gdbcore.h"
24 #include "frame-unwind.h"
25 #include "gdbtypes.h"
26 #include "osabi.h"
27 #include "regcache.h"
28 #include "regset.h"
29 #include "symtab.h"
30 #include "target.h"
31 #include "trad-frame.h"
32
33 #include "ppc-tdep.h"
34 #include "ppc64-tdep.h"
35 #include "ppcfbsd-tdep.h"
36 #include "fbsd-tdep.h"
37 #include "solib-svr4.h"
38
39
40 /* 32-bit regset descriptions.  */
41
42 static const struct ppc_reg_offsets ppc32_fbsd_reg_offsets =
43   {
44         /* General-purpose registers.  */
45         /* .r0_offset = */     0,
46         /* .gpr_size = */      4,
47         /* .xr_size = */       4,
48         /* .pc_offset = */     144,
49         /* .ps_offset = */     -1,
50         /* .cr_offset = */     132,
51         /* .lr_offset = */     128,
52         /* .ctr_offset = */    140,
53         /* .xer_offset = */    136,
54         /* .mq_offset = */     -1,
55
56         /* Floating-point registers.  */
57         /* .f0_offset = */     0,
58         /* .fpscr_offset = */  256,
59         /* .fpscr_size = */    8,
60 #ifdef NOTYET
61         /* AltiVec registers.  */
62         /* .vr0_offset = */    0,
63         /* .vscr_offset = */   512 + 12,
64         /* .vrsave_offset = */ 512
65 #endif
66   };
67
68 /* 64-bit regset descriptions.  */
69
70 static const struct ppc_reg_offsets ppc64_fbsd_reg_offsets =
71   {
72         /* General-purpose registers.  */
73         /* .r0_offset = */     0,
74         /* .gpr_size = */      8,
75         /* .xr_size = */       8,
76         /* .pc_offset = */     288,
77         /* .ps_offset = */     -1,
78         /* .cr_offset = */     264,
79         /* .lr_offset = */     256,
80         /* .ctr_offset = */    280,
81         /* .xer_offset = */    272,
82         /* .mq_offset = */     -1,
83
84         /* Floating-point registers.  */
85         /* .f0_offset = */     0,
86         /* .fpscr_offset = */  256,
87         /* .fpscr_size = */    8,
88 #ifdef NOYET
89         /* AltiVec registers.  */
90         /* .vr0_offset = */    0,
91         /* .vscr_offset = */   512 + 12,
92         /* .vrsave_offset = */ 528
93 #endif
94   };
95
96 /* 32-bit general-purpose register set.  */
97
98 static const struct regset ppc32_fbsd_gregset = {
99   &ppc32_fbsd_reg_offsets,
100   ppc_supply_gregset,
101   ppc_collect_gregset
102 };
103
104 /* 64-bit general-purpose register set.  */
105
106 static const struct regset ppc64_fbsd_gregset = {
107   &ppc64_fbsd_reg_offsets,
108   ppc_supply_gregset,
109   ppc_collect_gregset
110 };
111
112 /* 32-/64-bit floating-point register set.  */
113
114 static const struct regset ppc32_fbsd_fpregset = {
115   &ppc32_fbsd_reg_offsets,
116   ppc_supply_fpregset,
117   ppc_collect_fpregset
118 };
119
120 const struct regset *
121 ppc_fbsd_gregset (int wordsize)
122 {
123   return wordsize == 8 ? &ppc64_fbsd_gregset : &ppc32_fbsd_gregset;
124 }
125
126 const struct regset *
127 ppc_fbsd_fpregset (void)
128 {
129   return &ppc32_fbsd_fpregset;
130 }
131
132 /* Iterate over core file register note sections.  */
133
134 static void
135 ppcfbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
136                                       iterate_over_regset_sections_cb *cb,
137                                       void *cb_data,
138                                       const struct regcache *regcache)
139 {
140   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
141
142   if (tdep->wordsize == 4)
143     cb (".reg", 148, &ppc32_fbsd_gregset, NULL, cb_data);
144   else
145     cb (".reg", 296, &ppc64_fbsd_gregset, NULL, cb_data);
146   cb (".reg2", 264, &ppc32_fbsd_fpregset, NULL, cb_data);
147 }
148
149 /* Default page size.  */
150
151 static const int ppcfbsd_page_size = 4096;
152
153 /* Offset for sigreturn(2).  */
154
155 static const int ppcfbsd_sigreturn_offset[] = {
156   0xc,                          /* FreeBSD 32-bit  */
157   -1
158 };
159
160 /* Signal trampolines.  */
161
162 static int
163 ppcfbsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
164                                 struct frame_info *this_frame,
165                                 void **this_cache)
166 {
167   struct gdbarch *gdbarch = get_frame_arch (this_frame);
168   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
169   CORE_ADDR pc = get_frame_pc (this_frame);
170   CORE_ADDR start_pc = (pc & ~(ppcfbsd_page_size - 1));
171   const int *offset;
172   const char *name;
173
174   /* A stack trampoline is detected if no name is associated
175    to the current pc and if it points inside a trampoline
176    sequence.  */
177
178   find_pc_partial_function (pc, &name, NULL, NULL);
179
180   /* If we have a name, we have no trampoline, return.  */
181   if (name)
182     return 0;
183
184   for (offset = ppcfbsd_sigreturn_offset; *offset != -1; offset++)
185     {
186       gdb_byte buf[2 * PPC_INSN_SIZE];
187       unsigned long insn;
188
189       if (!safe_frame_unwind_memory (this_frame, start_pc + *offset,
190                                      buf, sizeof buf))
191         continue;
192
193       /* Check for "li r0,SYS_sigreturn".  */
194       insn = extract_unsigned_integer (buf, PPC_INSN_SIZE, byte_order);
195       if (insn != 0x380001a1)
196         continue;
197
198       /* Check for "sc".  */
199       insn = extract_unsigned_integer (buf + PPC_INSN_SIZE,
200                                        PPC_INSN_SIZE, byte_order);
201       if (insn != 0x44000002)
202         continue;
203
204       return 1;
205     }
206
207   return 0;
208 }
209
210 static struct trad_frame_cache *
211 ppcfbsd_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
212 {
213   struct gdbarch *gdbarch = get_frame_arch (this_frame);
214   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
215   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
216   struct trad_frame_cache *cache;
217   CORE_ADDR addr, base, func;
218   gdb_byte buf[PPC_INSN_SIZE];
219   int i;
220
221   if (*this_cache)
222     return *this_cache;
223
224   cache = trad_frame_cache_zalloc (this_frame);
225   *this_cache = cache;
226
227   func = get_frame_pc (this_frame);
228   func &= ~(ppcfbsd_page_size - 1);
229   if (!safe_frame_unwind_memory (this_frame, func, buf, sizeof buf))
230     return cache;
231
232   base = get_frame_register_unsigned (this_frame, gdbarch_sp_regnum (gdbarch));
233   addr = base + 0x10 + 2 * tdep->wordsize;
234   for (i = 0; i < ppc_num_gprs; i++, addr += tdep->wordsize)
235     {
236       int regnum = i + tdep->ppc_gp0_regnum;
237       trad_frame_set_reg_addr (cache, regnum, addr);
238     }
239   trad_frame_set_reg_addr (cache, tdep->ppc_lr_regnum, addr);
240   addr += tdep->wordsize;
241   trad_frame_set_reg_addr (cache, tdep->ppc_cr_regnum, addr);
242   addr += tdep->wordsize;
243   trad_frame_set_reg_addr (cache, tdep->ppc_xer_regnum, addr);
244   addr += tdep->wordsize;
245   trad_frame_set_reg_addr (cache, tdep->ppc_ctr_regnum, addr);
246   addr += tdep->wordsize;
247   trad_frame_set_reg_addr (cache, gdbarch_pc_regnum (gdbarch), addr);
248   /* SRR0?  */
249   addr += tdep->wordsize;
250
251   /* Construct the frame ID using the function start.  */
252   trad_frame_set_id (cache, frame_id_build (base, func));
253
254   return cache;
255 }
256
257 static void
258 ppcfbsd_sigtramp_frame_this_id (struct frame_info *this_frame,
259                                 void **this_cache, struct frame_id *this_id)
260 {
261   struct trad_frame_cache *cache =
262     ppcfbsd_sigtramp_frame_cache (this_frame, this_cache);
263
264   trad_frame_get_id (cache, this_id);
265 }
266
267 static struct value *
268 ppcfbsd_sigtramp_frame_prev_register (struct frame_info *this_frame,
269                                       void **this_cache, int regnum)
270 {
271   struct trad_frame_cache *cache =
272     ppcfbsd_sigtramp_frame_cache (this_frame, this_cache);
273
274   return trad_frame_get_register (cache, this_frame, regnum);
275 }
276
277 static const struct frame_unwind ppcfbsd_sigtramp_frame_unwind = {
278   SIGTRAMP_FRAME,
279   default_frame_unwind_stop_reason,
280   ppcfbsd_sigtramp_frame_this_id,
281   ppcfbsd_sigtramp_frame_prev_register,
282   NULL,
283   ppcfbsd_sigtramp_frame_sniffer
284 };
285
286 static enum return_value_convention
287 ppcfbsd_return_value (struct gdbarch *gdbarch, struct value *function,
288                       struct type *valtype, struct regcache *regcache,
289                       gdb_byte *readbuf, const gdb_byte *writebuf)
290 {
291   return ppc_sysv_abi_broken_return_value (gdbarch, function, valtype,
292                                            regcache, readbuf, writebuf);
293 }
294
295
296 static void
297 ppcfbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
298 {
299   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
300
301   /* Generic FreeBSD support. */
302   fbsd_init_abi (info, gdbarch);
303
304   /* FreeBSD doesn't support the 128-bit `long double' from the psABI.  */
305   set_gdbarch_long_double_bit (gdbarch, 64);
306   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
307
308   if (tdep->wordsize == 4)
309     {
310       set_gdbarch_return_value (gdbarch, ppcfbsd_return_value);
311
312       set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
313       set_solib_svr4_fetch_link_map_offsets (gdbarch,
314                                              svr4_ilp32_fetch_link_map_offsets);
315
316       frame_unwind_append_unwinder (gdbarch, &ppcfbsd_sigtramp_frame_unwind);
317       set_gdbarch_gcore_bfd_target (gdbarch, "elf32-powerpc");
318     }
319
320   if (tdep->wordsize == 8)
321     {
322       set_gdbarch_convert_from_func_ptr_addr
323         (gdbarch, ppc64_convert_from_func_ptr_addr);
324       set_gdbarch_elf_make_msymbol_special (gdbarch,
325                                             ppc64_elf_make_msymbol_special);
326
327       set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
328       set_solib_svr4_fetch_link_map_offsets (gdbarch,
329                                              svr4_lp64_fetch_link_map_offsets);
330       set_gdbarch_gcore_bfd_target (gdbarch, "elf64-powerpc");
331     }
332
333   set_gdbarch_iterate_over_regset_sections
334     (gdbarch, ppcfbsd_iterate_over_regset_sections);
335
336   set_gdbarch_fetch_tls_load_module_address (gdbarch,
337                                              svr4_fetch_objfile_link_map);
338 }
339
340 /* Provide a prototype to silence -Wmissing-prototypes.  */
341
342 void _initialize_ppcfbsd_tdep (void);
343
344 void
345 _initialize_ppcfbsd_tdep (void)
346 {
347   gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_FREEBSD_ELF,
348                           ppcfbsd_init_abi);
349   gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64,
350                           GDB_OSABI_FREEBSD_ELF,
351                           ppcfbsd_init_abi);
352   gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_FREEBSD_ELF,
353                           ppcfbsd_init_abi);
354 }