bfd/
[deliverable/binutils-gdb.git] / gdb / i386nbsd-tdep.c
CommitLineData
ed504bdf
MK
1/* Target-dependent code for NetBSD/i386.
2
197e01b6 3 Copyright (C) 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002,
7e654c37 4 2003, 2004
0fc93e6b
C
5 Free Software Foundation, 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
197e01b6
EZ
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
0fc93e6b
C
23
24#include "defs.h"
13739f4d 25#include "arch-utils.h"
911bc6ee 26#include "frame.h"
dfe6eb1f
JT
27#include "gdbcore.h"
28#include "regcache.h"
13739f4d 29#include "regset.h"
4be87837 30#include "osabi.h"
911bc6ee 31#include "symtab.h"
0fc93e6b 32
13739f4d
MK
33#include "gdb_assert.h"
34#include "gdb_string.h"
35
3cac699e 36#include "i386-tdep.h"
dfe6eb1f 37#include "i387-tdep.h"
3cac699e 38#include "nbsd-tdep.h"
7d400e77
JT
39#include "solib-svr4.h"
40
13739f4d
MK
41/* From <machine/reg.h>. */
42static int i386nbsd_r_reg_offset[] =
dfe6eb1f 43{
13739f4d
MK
44 0 * 4, /* %eax */
45 1 * 4, /* %ecx */
46 2 * 4, /* %edx */
47 3 * 4, /* %ebx */
48 4 * 4, /* %esp */
49 5 * 4, /* %ebp */
50 6 * 4, /* %esi */
51 7 * 4, /* %edi */
52 8 * 4, /* %eip */
53 9 * 4, /* %eflags */
54 10 * 4, /* %cs */
55 11 * 4, /* %ss */
56 12 * 4, /* %ds */
57 13 * 4, /* %es */
58 14 * 4, /* %fs */
59 15 * 4 /* %gs */
dfe6eb1f
JT
60};
61
dfe6eb1f 62static void
13739f4d
MK
63i386nbsd_aout_supply_regset (const struct regset *regset,
64 struct regcache *regcache, int regnum,
65 const void *regs, size_t len)
dfe6eb1f 66{
9ea75c57 67 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
13739f4d
MK
68
69 gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE);
dfe6eb1f 70
13739f4d
MK
71 i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
72 i387_supply_fsave (regcache, regnum, (char *) regs + tdep->sizeof_gregset);
dfe6eb1f
JT
73}
74
49cfa46f 75static const struct regset *
13739f4d
MK
76i386nbsd_aout_regset_from_core_section (struct gdbarch *gdbarch,
77 const char *sect_name,
78 size_t sect_size)
dfe6eb1f 79{
13739f4d 80 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
dfe6eb1f 81
13739f4d
MK
82 /* NetBSD a.out core dumps don't use seperate register sets for the
83 general-purpose and floating-point registers. */
dfe6eb1f 84
13739f4d
MK
85 if (strcmp (sect_name, ".reg") == 0
86 && sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE)
dfe6eb1f 87 {
13739f4d 88 if (tdep->gregset == NULL)
9ea75c57
MK
89 tdep->gregset =
90 regset_alloc (gdbarch, i386nbsd_aout_supply_regset, NULL);
13739f4d 91 return tdep->gregset;
dfe6eb1f
JT
92 }
93
13739f4d 94 return NULL;
dfe6eb1f
JT
95}
96
d66198e1
JT
97/* Under NetBSD/i386, signal handler invocations can be identified by the
98 designated code sequence that is used to return from a signal handler.
99 In particular, the return address of a signal handler points to the
100 following code sequence:
101
102 leal 0x10(%esp), %eax
103 pushl %eax
104 pushl %eax
105 movl $0x127, %eax # __sigreturn14
106 int $0x80
107
108 Each instruction has a unique encoding, so we simply attempt to match
109 the instruction the PC is pointing to with any of the above instructions.
110 If there is a hit, we know the offset to the start of the designated
111 sequence and can then check whether we really are executing in the
112 signal trampoline. If not, -1 is returned, otherwise the offset from the
113 start of the return sequence is returned. */
114#define RETCODE_INSN1 0x8d
115#define RETCODE_INSN2 0x50
116#define RETCODE_INSN3 0x50
117#define RETCODE_INSN4 0xb8
118#define RETCODE_INSN5 0xcd
119
120#define RETCODE_INSN2_OFF 4
121#define RETCODE_INSN3_OFF 5
122#define RETCODE_INSN4_OFF 6
123#define RETCODE_INSN5_OFF 11
124
125static const unsigned char sigtramp_retcode[] =
3cac699e 126{
d66198e1
JT
127 RETCODE_INSN1, 0x44, 0x24, 0x10,
128 RETCODE_INSN2,
129 RETCODE_INSN3,
130 RETCODE_INSN4, 0x27, 0x01, 0x00, 0x00,
131 RETCODE_INSN5, 0x80,
132};
133
134static LONGEST
24f9f5e3 135i386nbsd_sigtramp_offset (struct frame_info *next_frame)
d66198e1 136{
24f9f5e3 137 CORE_ADDR pc = frame_pc_unwind (next_frame);
d66198e1
JT
138 unsigned char ret[sizeof(sigtramp_retcode)], insn;
139 LONGEST off;
140 int i;
141
24f9f5e3 142 if (!safe_frame_unwind_memory (next_frame, pc, &insn, 1))
d66198e1
JT
143 return -1;
144
145 switch (insn)
146 {
147 case RETCODE_INSN1:
148 off = 0;
149 break;
150
151 case RETCODE_INSN2:
152 /* INSN2 and INSN3 are the same. Read at the location of PC+1
153 to determine if we're actually looking at INSN2 or INSN3. */
24f9f5e3 154 if (!safe_frame_unwind_memory (next_frame, pc + 1, &insn, 1))
d66198e1
JT
155 return -1;
156
157 if (insn == RETCODE_INSN3)
158 off = RETCODE_INSN2_OFF;
159 else
160 off = RETCODE_INSN3_OFF;
161 break;
3cac699e 162
d66198e1
JT
163 case RETCODE_INSN4:
164 off = RETCODE_INSN4_OFF;
165 break;
166
167 case RETCODE_INSN5:
168 off = RETCODE_INSN5_OFF;
169 break;
170
171 default:
172 return -1;
173 }
174
175 pc -= off;
3cac699e 176
24f9f5e3 177 if (!safe_frame_unwind_memory (next_frame, pc, ret, sizeof (ret)))
d66198e1 178 return -1;
3cac699e 179
d66198e1
JT
180 if (memcmp (ret, sigtramp_retcode, sizeof (ret)) == 0)
181 return off;
182
183 return -1;
3cac699e
JT
184}
185
377d9ebd 186/* Return whether the frame preceding NEXT_FRAME corresponds to a
911bc6ee
MK
187 NetBSD sigtramp routine. */
188
d66198e1 189static int
911bc6ee 190i386nbsd_sigtramp_p (struct frame_info *next_frame)
d66198e1 191{
911bc6ee
MK
192 CORE_ADDR pc = frame_pc_unwind (next_frame);
193 char *name;
194
195 find_pc_partial_function (pc, &name, NULL, NULL);
d66198e1 196 return (nbsd_pc_in_sigtramp (pc, name)
24f9f5e3 197 || i386nbsd_sigtramp_offset (next_frame) >= 0);
d66198e1 198}
3cac699e
JT
199
200/* From <machine/signal.h>. */
13739f4d 201int i386nbsd_sc_reg_offset[] =
a3386186
MK
202{
203 10 * 4, /* %eax */
204 9 * 4, /* %ecx */
205 8 * 4, /* %edx */
206 7 * 4, /* %ebx */
207 14 * 4, /* %esp */
208 6 * 4, /* %ebp */
209 5 * 4, /* %esi */
210 4 * 4, /* %edi */
211 11 * 4, /* %eip */
212 13 * 4, /* %eflags */
213 12 * 4, /* %cs */
214 15 * 4, /* %ss */
215 3 * 4, /* %ds */
216 2 * 4, /* %es */
217 1 * 4, /* %fs */
218 0 * 4 /* %gs */
219};
3cac699e
JT
220
221static void
222i386nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
223{
224 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
225
226 /* Obviously NetBSD is BSD-based. */
227 i386bsd_init_abi (info, gdbarch);
228
13739f4d
MK
229 /* NetBSD has a different `struct reg'. */
230 tdep->gregset_reg_offset = i386nbsd_r_reg_offset;
231 tdep->gregset_num_regs = ARRAY_SIZE (i386nbsd_r_reg_offset);
232 tdep->sizeof_gregset = 16 * 4;
233
3cac699e 234 /* NetBSD has different signal trampoline conventions. */
911bc6ee
MK
235 tdep->sigtramp_start = 0;
236 tdep->sigtramp_end = 0;
237 tdep->sigtramp_p = i386nbsd_sigtramp_p;
3cac699e
JT
238
239 /* NetBSD uses -freg-struct-return by default. */
240 tdep->struct_return = reg_struct_return;
241
3cac699e 242 /* NetBSD has a `struct sigcontext' that's different from the
f2e7c15d 243 original 4.3 BSD. */
a3386186 244 tdep->sc_reg_offset = i386nbsd_sc_reg_offset;
13739f4d
MK
245 tdep->sc_num_regs = ARRAY_SIZE (i386nbsd_sc_reg_offset);
246}
247
248/* NetBSD a.out. */
249
250static void
251i386nbsdaout_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
252{
253 i386nbsd_init_abi (info, gdbarch);
254
255 /* NetBSD a.out has a single register set. */
256 set_gdbarch_regset_from_core_section
257 (gdbarch, i386nbsd_aout_regset_from_core_section);
3cac699e
JT
258}
259
260/* NetBSD ELF. */
13739f4d 261
3cac699e
JT
262static void
263i386nbsdelf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
264{
265 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
266
267 /* It's still NetBSD. */
268 i386nbsd_init_abi (info, gdbarch);
269
270 /* But ELF-based. */
271 i386_elf_init_abi (info, gdbarch);
272
273 /* NetBSD ELF uses SVR4-style shared libraries. */
13739f4d 274 set_solib_svr4_fetch_link_map_offsets
7e654c37 275 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
3cac699e
JT
276
277 /* NetBSD ELF uses -fpcc-struct-return by default. */
278 tdep->struct_return = pcc_struct_return;
3cac699e
JT
279}
280
dfe6eb1f
JT
281void
282_initialize_i386nbsd_tdep (void)
283{
05816f70 284 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_NETBSD_AOUT,
13739f4d 285 i386nbsdaout_init_abi);
05816f70 286 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_NETBSD_ELF,
3cac699e 287 i386nbsdelf_init_abi);
dfe6eb1f 288}
This page took 0.475463 seconds and 4 git commands to generate.