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