2004-04-22 Andrew Cagney <cagney@redhat.com>
[platform/upstream/binutils.git] / gdb / ppcnbsd-tdep.c
1 /* Target-dependent code for PowerPC systems running NetBSD.
2
3    Copyright 2002, 2003, 2004 Free Software Foundation, Inc.
4
5    Contributed by Wasabi Systems, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 #include "defs.h"
25 #include "gdbcore.h"
26 #include "regcache.h"
27 #include "target.h"
28 #include "breakpoint.h"
29 #include "value.h"
30 #include "osabi.h"
31
32 #include "ppc-tdep.h"
33 #include "ppcnbsd-tdep.h"
34 #include "nbsd-tdep.h"
35 #include "tramp-frame.h"
36 #include "trad-frame.h"
37
38 #include "solib-svr4.h"
39
40 #define REG_FIXREG_OFFSET(x)    ((x) * 4)
41 #define REG_LR_OFFSET           (32 * 4)
42 #define REG_CR_OFFSET           (33 * 4)
43 #define REG_XER_OFFSET          (34 * 4)
44 #define REG_CTR_OFFSET          (35 * 4)
45 #define REG_PC_OFFSET           (36 * 4)
46 #define SIZEOF_STRUCT_REG       (37 * 4)
47
48 #define FPREG_FPR_OFFSET(x)     ((x) * 8)
49 #define FPREG_FPSCR_OFFSET      (32 * 8)
50 #define SIZEOF_STRUCT_FPREG     (33 * 8)
51
52 void
53 ppcnbsd_supply_reg (char *regs, int regno)
54 {
55   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
56   int i;
57
58   for (i = 0; i <= 31; i++)
59     {
60       if (regno == i || regno == -1)
61         supply_register (i, regs + REG_FIXREG_OFFSET (i));
62     }
63
64   if (regno == tdep->ppc_lr_regnum || regno == -1)
65     supply_register (tdep->ppc_lr_regnum, regs + REG_LR_OFFSET);
66
67   if (regno == tdep->ppc_cr_regnum || regno == -1)
68     supply_register (tdep->ppc_cr_regnum, regs + REG_CR_OFFSET);
69
70   if (regno == tdep->ppc_xer_regnum || regno == -1)
71     supply_register (tdep->ppc_xer_regnum, regs + REG_XER_OFFSET);
72
73   if (regno == tdep->ppc_ctr_regnum || regno == -1)
74     supply_register (tdep->ppc_ctr_regnum, regs + REG_CTR_OFFSET);
75
76   if (regno == PC_REGNUM || regno == -1)
77     supply_register (PC_REGNUM, regs + REG_PC_OFFSET);
78 }
79
80 void
81 ppcnbsd_fill_reg (char *regs, int regno)
82 {
83   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
84   int i;
85
86   for (i = 0; i <= 31; i++)
87     {
88       if (regno == i || regno == -1)
89         regcache_collect (i, regs + REG_FIXREG_OFFSET (i));
90     }
91
92   if (regno == tdep->ppc_lr_regnum || regno == -1)
93     regcache_collect (tdep->ppc_lr_regnum, regs + REG_LR_OFFSET);
94
95   if (regno == tdep->ppc_cr_regnum || regno == -1)
96     regcache_collect (tdep->ppc_cr_regnum, regs + REG_CR_OFFSET);
97
98   if (regno == tdep->ppc_xer_regnum || regno == -1)
99     regcache_collect (tdep->ppc_xer_regnum, regs + REG_XER_OFFSET);
100
101   if (regno == tdep->ppc_ctr_regnum || regno == -1)
102     regcache_collect (tdep->ppc_ctr_regnum, regs + REG_CTR_OFFSET);
103
104   if (regno == PC_REGNUM || regno == -1)
105     regcache_collect (PC_REGNUM, regs + REG_PC_OFFSET);
106 }
107
108 void
109 ppcnbsd_supply_fpreg (char *fpregs, int regno)
110 {
111   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
112   int i;
113
114   for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++)
115     {
116       if (regno == i || regno == -1)
117         supply_register (i, fpregs + FPREG_FPR_OFFSET (i - FP0_REGNUM));
118     }
119
120   if (regno == tdep->ppc_fpscr_regnum || regno == -1)
121     supply_register (tdep->ppc_fpscr_regnum, fpregs + FPREG_FPSCR_OFFSET);
122 }
123
124 void
125 ppcnbsd_fill_fpreg (char *fpregs, int regno)
126 {
127   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
128   int i;
129
130   for (i = FP0_REGNUM; i <= FP0_REGNUM + 31; i++)
131     {
132       if (regno == i || regno == -1)
133         regcache_collect (i, fpregs + FPREG_FPR_OFFSET (i - FP0_REGNUM));
134     }
135
136   if (regno == tdep->ppc_fpscr_regnum || regno == -1)
137     regcache_collect (tdep->ppc_fpscr_regnum, fpregs + FPREG_FPSCR_OFFSET);
138 }
139
140 static void
141 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, int which,
142                       CORE_ADDR ignore)
143 {
144   char *regs, *fpregs;
145
146   /* We get everything from one section.  */
147   if (which != 0)
148     return;
149
150   regs = core_reg_sect;
151   fpregs = core_reg_sect + SIZEOF_STRUCT_REG;
152
153   /* Integer registers.  */
154   ppcnbsd_supply_reg (regs, -1);
155
156   /* Floating point registers.  */
157   ppcnbsd_supply_fpreg (fpregs, -1);
158 }
159
160 static void
161 fetch_elfcore_registers (char *core_reg_sect, unsigned core_reg_size, int which,
162                          CORE_ADDR ignore)
163 {
164   switch (which)
165     {
166     case 0:  /* Integer registers.  */
167       if (core_reg_size != SIZEOF_STRUCT_REG)
168         warning ("Wrong size register set in core file.");
169       else
170         ppcnbsd_supply_reg (core_reg_sect, -1);
171       break;
172
173     case 2:  /* Floating point registers.  */
174       if (core_reg_size != SIZEOF_STRUCT_FPREG)
175         warning ("Wrong size FP register set in core file.");
176       else
177         ppcnbsd_supply_fpreg (core_reg_sect, -1);
178       break;
179
180     default:
181       /* Don't know what kind of register request this is; just ignore it.  */
182       break;
183     }
184 }
185
186 static struct core_fns ppcnbsd_core_fns =
187 {
188   bfd_target_unknown_flavour,           /* core_flavour */
189   default_check_format,                 /* check_format */
190   default_core_sniffer,                 /* core_sniffer */
191   fetch_core_registers,                 /* core_read_registers */
192   NULL                                  /* next */
193 };
194
195 static struct core_fns ppcnbsd_elfcore_fns =
196 {
197   bfd_target_elf_flavour,               /* core_flavour */
198   default_check_format,                 /* check_format */
199   default_core_sniffer,                 /* core_sniffer */
200   fetch_elfcore_registers,              /* core_read_registers */
201   NULL                                  /* next */
202 };
203
204 static int
205 ppcnbsd_pc_in_sigtramp (CORE_ADDR pc, char *func_name)
206 {
207   /* FIXME: Need to add support for kernel-provided signal trampolines.  */
208   return (nbsd_pc_in_sigtramp (pc, func_name));
209 }
210
211 /* NetBSD is confused.  It appears that 1.5 was using the correct SVr4
212    convention but, 1.6 switched to the below broken convention.  For
213    the moment use the broken convention.  Ulgh!.  */
214
215 static enum return_value_convention
216 ppcnbsd_return_value (struct gdbarch *gdbarch, struct type *valtype,
217                       struct regcache *regcache, void *readbuf,
218                       const void *writebuf)
219 {
220   if ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
221        || TYPE_CODE (valtype) == TYPE_CODE_UNION)
222       && !((TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 8)
223             && TYPE_VECTOR (valtype))
224       && !(TYPE_LENGTH (valtype) == 1
225            || TYPE_LENGTH (valtype) == 2
226            || TYPE_LENGTH (valtype) == 4
227            || TYPE_LENGTH (valtype) == 8))
228     return RETURN_VALUE_STRUCT_CONVENTION;
229   else
230     return ppc_sysv_abi_broken_return_value (gdbarch, valtype, regcache,
231                                              readbuf, writebuf);
232 }
233
234 static void
235 ppcnbsd_sigtramp_cache_init (const struct tramp_frame *self,
236                              struct frame_info *next_frame,
237                              struct trad_frame_cache *this_cache,
238                              CORE_ADDR func)
239 {
240   CORE_ADDR base;
241   CORE_ADDR offset;
242   int i;
243   struct gdbarch *gdbarch = get_frame_arch (next_frame);
244   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
245
246   base = frame_unwind_register_unsigned (next_frame, SP_REGNUM);
247   offset = base + 0x18 + 2 * tdep->wordsize;
248   for (i = 0; i < 32; i++)
249     {
250       int regnum = i + tdep->ppc_gp0_regnum;
251       trad_frame_set_reg_addr (this_cache, regnum, offset);
252       offset += tdep->wordsize;
253     }
254   trad_frame_set_reg_addr (this_cache, tdep->ppc_lr_regnum, offset);
255   offset += tdep->wordsize;
256   trad_frame_set_reg_addr (this_cache, tdep->ppc_cr_regnum, offset);
257   offset += tdep->wordsize;
258   trad_frame_set_reg_addr (this_cache, tdep->ppc_xer_regnum, offset);
259   offset += tdep->wordsize;
260   trad_frame_set_reg_addr (this_cache, tdep->ppc_ctr_regnum, offset);
261   offset += tdep->wordsize;
262   trad_frame_set_reg_addr (this_cache, PC_REGNUM, offset); /* SRR0? */
263   offset += tdep->wordsize;
264
265   /* Construct the frame ID using the function start.  */
266   trad_frame_set_id (this_cache, frame_id_build (base, func));
267 }
268
269 /* Given the NEXT frame, examine the instructions at and around this
270    frame's resume address (aka PC) to see of they look like a signal
271    trampoline.  Return the address of the trampolines first
272    instruction, or zero if it isn't a signal trampoline.  */
273
274 static const struct tramp_frame ppcnbsd_sigtramp = {
275   4, /* insn size */
276   { /* insn */
277     0x38610018, /* addi r3,r1,24 */
278     0x38000127, /* li r0,295 */
279     0x44000002, /* sc */
280     0x38000001, /* li r0,1 */
281     0x44000002, /* sc */
282     TRAMP_SENTINEL_INSN
283   },
284   ppcnbsd_sigtramp_cache_init
285 };
286
287 static void
288 ppcnbsd_init_abi (struct gdbarch_info info,
289                   struct gdbarch *gdbarch)
290 {
291   set_gdbarch_deprecated_pc_in_sigtramp (gdbarch, ppcnbsd_pc_in_sigtramp);
292   /* For NetBSD, this is an on again, off again thing.  Some systems
293      do use the broken struct convention, and some don't.  */
294   set_gdbarch_return_value (gdbarch, ppcnbsd_return_value);
295   set_solib_svr4_fetch_link_map_offsets (gdbarch,
296                                 nbsd_ilp32_solib_svr4_fetch_link_map_offsets);
297   tramp_frame_prepend_unwinder (gdbarch, &ppcnbsd_sigtramp);
298 }
299
300 void
301 _initialize_ppcnbsd_tdep (void)
302 {
303   gdbarch_register_osabi (bfd_arch_powerpc, 0, GDB_OSABI_NETBSD_ELF,
304                           ppcnbsd_init_abi);
305
306   deprecated_add_core_fns (&ppcnbsd_core_fns);
307   deprecated_add_core_fns (&ppcnbsd_elfcore_fns);
308 }