2003-04-28 Andrew Cagney <cagney@redhat.com>
[external/binutils.git] / gdb / alphanbsd-tdep.c
1 /* Target-dependent code for NetBSD/Alpha.
2    Copyright 2002, 2003 Free Software Foundation, Inc.
3    Contributed by Wasabi Systems, 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 2 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, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "gdbcore.h"
24 #include "frame.h"
25 #include "regcache.h"
26 #include "value.h"
27 #include "osabi.h"
28
29 #include "solib-svr4.h"
30
31 #include "alpha-tdep.h"
32 #include "alphabsd-tdep.h"
33 #include "nbsd-tdep.h"
34
35 static void
36 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, int which,
37                       CORE_ADDR ignore)
38 {
39   char *regs, *fpregs;
40   int regno;
41
42   /* Table to map a gdb register number to a trapframe register index.  */
43   static const int regmap[] =
44   {
45      0,   1,   2,   3,
46      4,   5,   6,   7,
47      8,   9,  10,  11,
48     12,  13,  14,  15, 
49     30,  31,  32,  16, 
50     17,  18,  19,  20,
51     21,  22,  23,  24,
52     25,  29,  26
53   };
54 #define SIZEOF_TRAPFRAME (33 * 8)
55
56   /* We get everything from one section.  */
57   if (which != 0)
58     return;
59
60   regs = core_reg_sect;
61   fpregs = core_reg_sect + SIZEOF_TRAPFRAME;
62
63   if (core_reg_size < (SIZEOF_TRAPFRAME + SIZEOF_STRUCT_FPREG))
64     {
65       warning ("Wrong size register set in core file.");
66       return;
67     }
68
69   /* Integer registers.  */
70   for (regno = 0; regno < ALPHA_ZERO_REGNUM; regno++)
71     supply_register (regno, regs + (regmap[regno] * 8));
72   supply_register (ALPHA_ZERO_REGNUM, NULL);
73   supply_register (DEPRECATED_FP_REGNUM, NULL);
74   supply_register (PC_REGNUM, regs + (28 * 8));
75
76   /* Floating point registers.  */
77   alphabsd_supply_fpreg (fpregs, -1);
78 }
79
80 static void
81 fetch_elfcore_registers (char *core_reg_sect, unsigned core_reg_size, int which,
82                          CORE_ADDR ignore)
83 {
84   switch (which)
85     {
86     case 0:  /* Integer registers.  */
87       if (core_reg_size != SIZEOF_STRUCT_REG)
88         warning ("Wrong size register set in core file.");
89       else
90         alphabsd_supply_reg (core_reg_sect, -1);
91       break;
92
93     case 2:  /* Floating point registers.  */
94       if (core_reg_size != SIZEOF_STRUCT_FPREG)
95         warning ("Wrong size FP register set in core file.");
96       else
97         alphabsd_supply_fpreg (core_reg_sect, -1);
98       break;
99
100     default:
101       /* Don't know what kind of register request this is; just ignore it.  */
102       break;
103     }
104 }
105
106 static struct core_fns alphanbsd_core_fns =
107 {
108   bfd_target_unknown_flavour,           /* core_flavour */
109   default_check_format,                 /* check_format */
110   default_core_sniffer,                 /* core_sniffer */
111   fetch_core_registers,                 /* core_read_registers */
112   NULL                                  /* next */
113 };
114
115 static struct core_fns alphanbsd_elfcore_fns =
116 {
117   bfd_target_elf_flavour,               /* core_flavour */
118   default_check_format,                 /* check_format */
119   default_core_sniffer,                 /* core_sniffer */
120   fetch_elfcore_registers,              /* core_read_registers */
121   NULL                                  /* next */
122 };
123
124 /* Under NetBSD/alpha, signal handler invocations can be identified by the
125    designated code sequence that is used to return from a signal handler.
126    In particular, the return address of a signal handler points to the
127    following code sequence:
128
129         ldq     a0, 0(sp)
130         lda     sp, 16(sp)
131         lda     v0, 295(zero)   # __sigreturn14
132         call_pal callsys
133
134    Each instruction has a unique encoding, so we simply attempt to match
135    the instruction the PC is pointing to with any of the above instructions.
136    If there is a hit, we know the offset to the start of the designated
137    sequence and can then check whether we really are executing in the
138    signal trampoline.  If not, -1 is returned, otherwise the offset from the
139    start of the return sequence is returned.  */
140 static const unsigned char sigtramp_retcode[] =
141 {
142   0x00, 0x00, 0x1e, 0xa6,       /* ldq a0, 0(sp) */
143   0x10, 0x00, 0xde, 0x23,       /* lda sp, 16(sp) */
144   0x27, 0x01, 0x1f, 0x20,       /* lda v0, 295(zero) */
145   0x83, 0x00, 0x00, 0x00,       /* call_pal callsys */
146 };
147 #define RETCODE_NWORDS          4
148 #define RETCODE_SIZE            (RETCODE_NWORDS * 4)
149
150 LONGEST
151 alphanbsd_sigtramp_offset (CORE_ADDR pc)
152 {
153   unsigned char ret[RETCODE_SIZE], w[4];
154   LONGEST off;
155   int i;
156
157   if (read_memory_nobpt (pc, (char *) w, 4) != 0)
158     return -1;
159
160   for (i = 0; i < RETCODE_NWORDS; i++)
161     {
162       if (memcmp (w, sigtramp_retcode + (i * 4), 4) == 0)
163         break;
164     }
165   if (i == RETCODE_NWORDS)
166     return (-1);
167
168   off = i * 4;
169   pc -= off;
170
171   if (read_memory_nobpt (pc, (char *) ret, sizeof (ret)) != 0)
172     return -1;
173
174   if (memcmp (ret, sigtramp_retcode, RETCODE_SIZE) == 0)
175     return off;
176
177   return -1;
178 }
179
180 static int
181 alphanbsd_pc_in_sigtramp (CORE_ADDR pc, char *func_name)
182 {
183   return (nbsd_pc_in_sigtramp (pc, func_name)
184           || alphanbsd_sigtramp_offset (pc) >= 0);
185 }
186
187 static CORE_ADDR
188 alphanbsd_sigcontext_addr (struct frame_info *frame)
189 {
190   /* FIXME: This is not correct for all versions of NetBSD/alpha.
191      We will probably need to disassemble the trampoline to figure
192      out which trampoline frame type we have.  */
193   return frame->frame;
194 }
195
196 static CORE_ADDR
197 alphanbsd_skip_sigtramp_frame (struct frame_info *frame, CORE_ADDR pc)
198 {
199   char *name;
200
201   /* FIXME: This is not correct for all versions of NetBSD/alpha.
202      We will probably need to disassemble the trampoline to figure
203      out which trampoline frame type we have.  */
204   find_pc_partial_function (pc, &name, (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
205   if (PC_IN_SIGTRAMP (pc, name))
206     return frame->frame;
207   return 0;
208 }
209
210 static void
211 alphanbsd_init_abi (struct gdbarch_info info,
212                     struct gdbarch *gdbarch)
213 {
214   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
215
216   set_gdbarch_pc_in_sigtramp (gdbarch, alphanbsd_pc_in_sigtramp);
217
218   /* NetBSD/alpha does not provide single step support via ptrace(2); we
219      must use software single-stepping.  */
220   set_gdbarch_software_single_step (gdbarch, alpha_software_single_step);
221
222   set_solib_svr4_fetch_link_map_offsets (gdbarch,
223                                  nbsd_lp64_solib_svr4_fetch_link_map_offsets);
224
225   tdep->skip_sigtramp_frame = alphanbsd_skip_sigtramp_frame;
226   tdep->dynamic_sigtramp_offset = alphanbsd_sigtramp_offset;
227   tdep->sigcontext_addr = alphanbsd_sigcontext_addr;
228
229   tdep->jb_pc = 2;
230   tdep->jb_elt_size = 8;
231 }
232
233 void
234 _initialize_alphanbsd_tdep (void)
235 {
236   gdbarch_register_osabi (bfd_arch_alpha, 0, GDB_OSABI_NETBSD_ELF,
237                           alphanbsd_init_abi);
238
239   add_core_fns (&alphanbsd_core_fns);
240   add_core_fns (&alphanbsd_elfcore_fns);
241 }