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