*** empty log message ***
[platform/upstream/binutils.git] / gdb / i386nbsd-tdep.c
1 /* Target-dependent code for NetBSD/i386, for GDB.
2    Copyright 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002, 2003
3    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 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 "gdbtypes.h"
24 #include "gdbcore.h"
25 #include "regcache.h"
26 #include "arch-utils.h"
27 #include "osabi.h"
28
29 #include "i386-tdep.h"
30 #include "i387-tdep.h"
31 #include "nbsd-tdep.h"
32
33 #include "solib-svr4.h"
34
35 /* Map a GDB register number to an offset in the reg structure.  */
36 static int regmap[] =
37 {
38   ( 0 * 4),             /* %eax */
39   ( 1 * 4),             /* %ecx */
40   ( 2 * 4),             /* %edx */
41   ( 3 * 4),             /* %ebx */
42   ( 4 * 4),             /* %esp */
43   ( 5 * 4),             /* %epb */
44   ( 6 * 4),             /* %esi */
45   ( 7 * 4),             /* %edi */
46   ( 8 * 4),             /* %eip */
47   ( 9 * 4),             /* %eflags */
48   (10 * 4),             /* %cs */
49   (11 * 4),             /* %ss */
50   (12 * 4),             /* %ds */
51   (13 * 4),             /* %es */
52   (14 * 4),             /* %fs */
53   (15 * 4),             /* %gs */
54 };
55
56 #define SIZEOF_STRUCT_REG       (16 * 4)
57
58 static void
59 i386nbsd_supply_reg (char *regs, int regno)
60 {
61   int i;
62
63   for (i = 0; i <= 15; i++)
64     if (regno == i || regno == -1)
65       supply_register (i, regs + regmap[i]);
66 }
67
68 static void
69 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, int which,
70                       CORE_ADDR ignore)
71 {
72   char *regs, *fsave;
73
74   /* We get everything from one section.  */
75   if (which != 0)
76     return;
77
78   if (core_reg_size < (SIZEOF_STRUCT_REG + 108))
79     {
80       warning ("Wrong size register set in core file.");
81       return;
82     }
83
84   regs = core_reg_sect;
85   fsave = core_reg_sect + SIZEOF_STRUCT_REG;
86
87   /* Integer registers.  */
88   i386nbsd_supply_reg (regs, -1);
89
90   /* Floating point registers.  */
91   i387_supply_fsave (fsave);
92 }
93
94 static void
95 fetch_elfcore_registers (char *core_reg_sect, unsigned core_reg_size,
96                          int which, CORE_ADDR ignore)
97 {
98   switch (which)
99     {
100     case 0:  /* Integer registers.  */
101       if (core_reg_size != SIZEOF_STRUCT_REG)
102         warning ("Wrong size register set in core file.");
103       else
104         i386nbsd_supply_reg (core_reg_sect, -1);
105       break;
106
107     case 2:  /* Floating point registers.  */
108       if (core_reg_size != 108)
109         warning ("Wrong size FP register set in core file."); 
110       else
111         i387_supply_fsave (core_reg_sect);  
112       break;
113
114     case 3:  /* "Extended" floating point registers.  This is gdb-speak
115                 for SSE/SSE2. */
116       if (core_reg_size != 512)
117         warning ("Wrong size XMM register set in core file.");
118       else
119         i387_supply_fxsave (core_reg_sect);
120       break;
121
122     default:
123       /* Don't know what kind of register request this is; just ignore it.  */
124       break;
125     }
126 }
127
128 static struct core_fns i386nbsd_core_fns =
129 {
130   bfd_target_unknown_flavour,           /* core_flavour */
131   default_check_format,                 /* check_format */
132   default_core_sniffer,                 /* core_sniffer */
133   fetch_core_registers,                 /* core_read_registers */
134   NULL                                  /* next */
135 };
136
137 static struct core_fns i386nbsd_elfcore_fns =
138 {
139   bfd_target_elf_flavour,               /* core_flavour */
140   default_check_format,                 /* check_format */
141   default_core_sniffer,                 /* core_sniffer */
142   fetch_elfcore_registers,              /* core_read_registers */
143   NULL                                  /* next */
144 };
145
146 /* Under NetBSD/i386, signal handler invocations can be identified by the
147    designated code sequence that is used to return from a signal handler.
148    In particular, the return address of a signal handler points to the
149    following code sequence:
150
151         leal    0x10(%esp), %eax
152         pushl   %eax
153         pushl   %eax
154         movl    $0x127, %eax            # __sigreturn14
155         int     $0x80
156
157    Each instruction has a unique encoding, so we simply attempt to match
158    the instruction the PC is pointing to with any of the above instructions.
159    If there is a hit, we know the offset to the start of the designated
160    sequence and can then check whether we really are executing in the
161    signal trampoline.  If not, -1 is returned, otherwise the offset from the
162    start of the return sequence is returned.  */
163 #define RETCODE_INSN1           0x8d
164 #define RETCODE_INSN2           0x50
165 #define RETCODE_INSN3           0x50
166 #define RETCODE_INSN4           0xb8
167 #define RETCODE_INSN5           0xcd
168
169 #define RETCODE_INSN2_OFF       4
170 #define RETCODE_INSN3_OFF       5
171 #define RETCODE_INSN4_OFF       6
172 #define RETCODE_INSN5_OFF       11
173
174 static const unsigned char sigtramp_retcode[] =
175 {
176   RETCODE_INSN1, 0x44, 0x24, 0x10,
177   RETCODE_INSN2,
178   RETCODE_INSN3,
179   RETCODE_INSN4, 0x27, 0x01, 0x00, 0x00,
180   RETCODE_INSN5, 0x80,
181 };
182
183 static LONGEST
184 i386nbsd_sigtramp_offset (CORE_ADDR pc)
185 {
186   unsigned char ret[sizeof(sigtramp_retcode)], insn;
187   LONGEST off;
188   int i;
189
190   if (read_memory_nobpt (pc, &insn, 1) != 0)
191     return -1;
192
193   switch (insn)
194     {
195     case RETCODE_INSN1:
196       off = 0;
197       break;
198
199     case RETCODE_INSN2:
200       /* INSN2 and INSN3 are the same.  Read at the location of PC+1
201          to determine if we're actually looking at INSN2 or INSN3.  */
202       if (read_memory_nobpt (pc + 1, &insn, 1) != 0)
203         return -1;
204
205       if (insn == RETCODE_INSN3)
206         off = RETCODE_INSN2_OFF;
207       else
208         off = RETCODE_INSN3_OFF;
209       break;
210
211     case RETCODE_INSN4:
212       off = RETCODE_INSN4_OFF;
213       break;
214
215     case RETCODE_INSN5:
216       off = RETCODE_INSN5_OFF;
217       break;
218
219     default:
220       return -1;
221     }
222
223   pc -= off;
224
225   if (read_memory_nobpt (pc, (char *) ret, sizeof (ret)) != 0)
226     return -1;
227
228   if (memcmp (ret, sigtramp_retcode, sizeof (ret)) == 0)
229     return off;
230
231   return -1;
232 }
233
234 static int
235 i386nbsd_pc_in_sigtramp (CORE_ADDR pc, char *name)
236 {
237   return (nbsd_pc_in_sigtramp (pc, name)
238           || i386nbsd_sigtramp_offset (pc) >= 0);
239 }
240
241 /* From <machine/signal.h>.  */
242 int i386nbsd_sc_reg_offset[I386_NUM_GREGS] =
243 {
244   10 * 4,                       /* %eax */
245   9 * 4,                        /* %ecx */
246   8 * 4,                        /* %edx */
247   7 * 4,                        /* %ebx */
248   14 * 4,                       /* %esp */
249   6 * 4,                        /* %ebp */
250   5 * 4,                        /* %esi */
251   4 * 4,                        /* %edi */
252   11 * 4,                       /* %eip */
253   13 * 4,                       /* %eflags */
254   12 * 4,                       /* %cs */
255   15 * 4,                       /* %ss */
256   3 * 4,                        /* %ds */
257   2 * 4,                        /* %es */
258   1 * 4,                        /* %fs */
259   0 * 4                         /* %gs */
260 };
261
262 static void 
263 i386nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
264 {
265   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
266
267   /* Obviously NetBSD is BSD-based.  */
268   i386bsd_init_abi (info, gdbarch);
269
270   /* NetBSD has different signal trampoline conventions.  */
271   set_gdbarch_pc_in_sigtramp (gdbarch, i386nbsd_pc_in_sigtramp);
272   /* FIXME: kettenis/20020906: We should probably provide
273      NetBSD-specific versions of these functions if we want to
274      recognize signal trampolines that live on the stack.  */
275   set_gdbarch_sigtramp_start (gdbarch, NULL);
276   set_gdbarch_sigtramp_end (gdbarch, NULL);
277
278   /* NetBSD uses -freg-struct-return by default.  */
279   tdep->struct_return = reg_struct_return;
280
281   /* NetBSD has a `struct sigcontext' that's different from the
282      origional 4.3 BSD.  */
283   tdep->sc_reg_offset = i386nbsd_sc_reg_offset;
284   tdep->sc_num_regs = I386_NUM_GREGS;
285 }
286
287 /* NetBSD ELF.  */
288 static void
289 i386nbsdelf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
290 {
291   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
292
293   /* It's still NetBSD.  */
294   i386nbsd_init_abi (info, gdbarch);
295
296   /* But ELF-based.  */
297   i386_elf_init_abi (info, gdbarch);
298
299   /* NetBSD ELF uses SVR4-style shared libraries.  */
300   set_gdbarch_in_solib_call_trampoline (gdbarch,
301                                         generic_in_solib_call_trampoline);
302   set_solib_svr4_fetch_link_map_offsets (gdbarch,
303                                  nbsd_ilp32_solib_svr4_fetch_link_map_offsets);
304
305   /* NetBSD ELF uses -fpcc-struct-return by default.  */
306   tdep->struct_return = pcc_struct_return;
307
308   /* We support the SSE registers on NetBSD ELF.  */
309   tdep->num_xmm_regs = I386_NUM_XREGS - 1;
310   set_gdbarch_num_regs (gdbarch, I386_NUM_GREGS + I386_NUM_FREGS
311                         + I386_NUM_XREGS);
312 }
313
314 void
315 _initialize_i386nbsd_tdep (void)
316 {
317   add_core_fns (&i386nbsd_core_fns);
318   add_core_fns (&i386nbsd_elfcore_fns);
319
320   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_NETBSD_AOUT,
321                           i386nbsd_init_abi);
322   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_NETBSD_ELF,
323                           i386nbsdelf_init_abi);
324 }