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