Fix typo in previous delta
[deliverable/binutils-gdb.git] / gdb / i386obsd-tdep.c
CommitLineData
005328e3 1/* Target-dependent code for OpenBSD/i386.
67457012 2
197e01b6 3 Copyright (C) 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002,
a28109e0 4 2003, 2004, 2005
005328e3
MK
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. */
005328e3
MK
23
24#include "defs.h"
25#include "arch-utils.h"
911bc6ee 26#include "frame.h"
508fbfea 27#include "frame-unwind.h"
005328e3
MK
28#include "gdbcore.h"
29#include "regcache.h"
67457012 30#include "regset.h"
911bc6ee
MK
31#include "symtab.h"
32#include "objfiles.h"
4be87837 33#include "osabi.h"
5d93ae8c 34#include "target.h"
508fbfea 35#include "trad-frame.h"
005328e3 36
67457012
MK
37#include "gdb_assert.h"
38#include "gdb_string.h"
39
005328e3
MK
40#include "i386-tdep.h"
41#include "i387-tdep.h"
60a6eeb6 42#include "solib-svr4.h"
a28109e0 43#include "bsd-uthread.h"
005328e3 44
5d93ae8c
MK
45/* Support for signal handlers. */
46
47/* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
48 in virtual memory. The randomness makes it somewhat tricky to
49 detect it, but fortunately we can rely on the fact that the start
3ed85247
MK
50 of the sigtramp routine is page-aligned. We recognize the
51 trampoline by looking for the code that invokes the sigreturn
52 system call. The offset where we can find that code varies from
53 release to release.
54
55 By the way, the mapping mentioned above is read-only, so you cannot
56 place a breakpoint in the signal trampoline. */
5d93ae8c
MK
57
58/* Default page size. */
59static const int i386obsd_page_size = 4096;
60
3ed85247
MK
61/* Offset for sigreturn(2). */
62static const int i386obsd_sigreturn_offset[] = {
63 0x0a, /* OpenBSD 3.2 */
64 0x14, /* OpenBSD 3.6 */
65 0x3a, /* OpenBSD 3.8 */
66 -1
67};
68
377d9ebd 69/* Return whether the frame preceding NEXT_FRAME corresponds to an
911bc6ee 70 OpenBSD sigtramp routine. */
5d93ae8c
MK
71
72static int
911bc6ee 73i386obsd_sigtramp_p (struct frame_info *next_frame)
5d93ae8c 74{
911bc6ee 75 CORE_ADDR pc = frame_pc_unwind (next_frame);
5d93ae8c 76 CORE_ADDR start_pc = (pc & ~(i386obsd_page_size - 1));
3ed85247 77 /* The call sequence invoking sigreturn(2). */
63c0089f 78 const gdb_byte sigreturn[] =
5d93ae8c
MK
79 {
80 0xb8,
81 0x67, 0x00, 0x00, 0x00, /* movl $SYS_sigreturn, %eax */
82 0xcd, 0x80 /* int $0x80 */
83 };
c822af0c 84 size_t buflen = sizeof sigreturn;
3ed85247 85 const int *offset;
63c0089f
MK
86 gdb_byte *buf;
87 char *name;
5d93ae8c 88
911bc6ee
MK
89 /* If the function has a valid symbol name, it isn't a
90 trampoline. */
91 find_pc_partial_function (pc, &name, NULL, NULL);
92 if (name != NULL)
93 return 0;
94
95 /* If the function lives in a valid section (even without a starting
96 point) it isn't a trampoline. */
97 if (find_pc_section (pc) != NULL)
5d93ae8c
MK
98 return 0;
99
9c8e3411 100 /* Allocate buffer. */
c822af0c 101 buf = alloca (buflen);
9c8e3411 102
3ed85247
MK
103 /* Loop over all offsets. */
104 for (offset = i386obsd_sigreturn_offset; *offset != -1; offset++)
105 {
106 /* If we can't read the instructions, return zero. */
107 if (!safe_frame_unwind_memory (next_frame, start_pc + *offset,
108 buf, buflen))
109 return 0;
110
111 /* Check for sigreturn(2). */
112 if (memcmp (buf, sigreturn, buflen) == 0)
113 return 1;
114 }
9c8e3411 115
911bc6ee 116 return 0;
5d93ae8c
MK
117}
118\f
119/* Mapping between the general-purpose registers in `struct reg'
120 format and GDB's register cache layout. */
121
67457012
MK
122/* From <machine/reg.h>. */
123static int i386obsd_r_reg_offset[] =
124{
125 0 * 4, /* %eax */
126 1 * 4, /* %ecx */
127 2 * 4, /* %edx */
128 3 * 4, /* %ebx */
129 4 * 4, /* %esp */
130 5 * 4, /* %ebp */
131 6 * 4, /* %esi */
132 7 * 4, /* %edi */
133 8 * 4, /* %eip */
134 9 * 4, /* %eflags */
135 10 * 4, /* %cs */
136 11 * 4, /* %ss */
137 12 * 4, /* %ds */
138 13 * 4, /* %es */
139 14 * 4, /* %fs */
140 15 * 4 /* %gs */
141};
005328e3
MK
142
143static void
67457012
MK
144i386obsd_aout_supply_regset (const struct regset *regset,
145 struct regcache *regcache, int regnum,
146 const void *regs, size_t len)
005328e3 147{
9ea75c57 148 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
3ed85247 149 const gdb_byte *gregs = regs;
67457012
MK
150
151 gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE);
005328e3 152
67457012 153 i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
3ed85247 154 i387_supply_fsave (regcache, regnum, gregs + tdep->sizeof_gregset);
005328e3
MK
155}
156
49cfa46f 157static const struct regset *
67457012
MK
158i386obsd_aout_regset_from_core_section (struct gdbarch *gdbarch,
159 const char *sect_name,
160 size_t sect_size)
005328e3 161{
67457012 162 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
005328e3 163
67457012
MK
164 /* OpenBSD a.out core dumps don't use seperate register sets for the
165 general-purpose and floating-point registers. */
005328e3 166
67457012
MK
167 if (strcmp (sect_name, ".reg") == 0
168 && sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE)
005328e3 169 {
67457012 170 if (tdep->gregset == NULL)
9ea75c57
MK
171 tdep->gregset =
172 regset_alloc (gdbarch, i386obsd_aout_supply_regset, NULL);
67457012 173 return tdep->gregset;
005328e3
MK
174 }
175
67457012 176 return NULL;
005328e3 177}
005328e3
MK
178\f
179
5d93ae8c
MK
180/* Sigtramp routine location for OpenBSD 3.1 and earlier releases. */
181CORE_ADDR i386obsd_sigtramp_start_addr = 0xbfbfdf20;
182CORE_ADDR i386obsd_sigtramp_end_addr = 0xbfbfdff0;
005328e3
MK
183
184/* From <machine/signal.h>. */
a3386186
MK
185int i386obsd_sc_reg_offset[I386_NUM_GREGS] =
186{
187 10 * 4, /* %eax */
188 9 * 4, /* %ecx */
189 8 * 4, /* %edx */
190 7 * 4, /* %ebx */
191 14 * 4, /* %esp */
192 6 * 4, /* %ebp */
193 5 * 4, /* %esi */
194 4 * 4, /* %edi */
195 11 * 4, /* %eip */
196 13 * 4, /* %eflags */
197 12 * 4, /* %cs */
198 15 * 4, /* %ss */
199 3 * 4, /* %ds */
200 2 * 4, /* %es */
201 1 * 4, /* %fs */
202 0 * 4 /* %gs */
203};
005328e3 204
a28109e0
MK
205/* From /usr/src/lib/libpthread/arch/i386/uthread_machdep.c. */
206static int i386obsd_uthread_reg_offset[] =
207{
208 11 * 4, /* %eax */
209 10 * 4, /* %ecx */
210 9 * 4, /* %edx */
211 8 * 4, /* %ebx */
212 -1, /* %esp */
213 6 * 4, /* %ebp */
214 5 * 4, /* %esi */
215 4 * 4, /* %edi */
216 12 * 4, /* %eip */
217 -1, /* %eflags */
218 13 * 4, /* %cs */
219 -1, /* %ss */
220 3 * 4, /* %ds */
221 2 * 4, /* %es */
222 1 * 4, /* %fs */
223 0 * 4 /* %gs */
224};
225
226/* Offset within the thread structure where we can find the saved
227 stack pointer (%esp). */
228#define I386OBSD_UTHREAD_ESP_OFFSET 176
229
230static void
231i386obsd_supply_uthread (struct regcache *regcache,
232 int regnum, CORE_ADDR addr)
233{
234 CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
235 CORE_ADDR sp = 0;
63c0089f 236 gdb_byte buf[4];
a28109e0
MK
237 int i;
238
239 gdb_assert (regnum >= -1);
240
241 if (regnum == -1 || regnum == I386_ESP_REGNUM)
242 {
243 int offset;
244
245 /* Fetch stack pointer from thread structure. */
246 sp = read_memory_unsigned_integer (sp_addr, 4);
247
248 /* Adjust the stack pointer such that it looks as if we just
249 returned from _thread_machdep_switch. */
250 offset = i386obsd_uthread_reg_offset[I386_EIP_REGNUM] + 4;
251 store_unsigned_integer (buf, 4, sp + offset);
252 regcache_raw_supply (regcache, I386_ESP_REGNUM, buf);
253 }
254
255 for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
256 {
257 if (i386obsd_uthread_reg_offset[i] != -1
258 && (regnum == -1 || regnum == i))
259 {
260 /* Fetch stack pointer from thread structure (if we didn't
261 do so already). */
262 if (sp == 0)
263 sp = read_memory_unsigned_integer (sp_addr, 4);
264
265 /* Read the saved register from the stack frame. */
266 read_memory (sp + i386obsd_uthread_reg_offset[i], buf, 4);
267 regcache_raw_supply (regcache, i, buf);
268 }
269 }
a28109e0
MK
270}
271
272static void
273i386obsd_collect_uthread (const struct regcache *regcache,
274 int regnum, CORE_ADDR addr)
275{
276 CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
277 CORE_ADDR sp = 0;
63c0089f 278 gdb_byte buf[4];
a28109e0
MK
279 int i;
280
281 gdb_assert (regnum >= -1);
282
283 if (regnum == -1 || regnum == I386_ESP_REGNUM)
284 {
285 int offset;
286
287 /* Calculate the stack pointer (frame pointer) that will be
288 stored into the thread structure. */
289 offset = i386obsd_uthread_reg_offset[I386_EIP_REGNUM] + 4;
290 regcache_raw_collect (regcache, I386_ESP_REGNUM, buf);
291 sp = extract_unsigned_integer (buf, 4) - offset;
292
293 /* Store the stack pointer. */
294 write_memory_unsigned_integer (sp_addr, 4, sp);
295
296 /* The stack pointer was (potentially) modified. Make sure we
297 build a proper stack frame. */
298 regnum = -1;
299 }
300
301 for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
302 {
303 if (i386obsd_uthread_reg_offset[i] != -1
304 && (regnum == -1 || regnum == i))
305 {
306 /* Fetch stack pointer from thread structure (if we didn't
307 calculate it already). */
308 if (sp == 0)
309 sp = read_memory_unsigned_integer (sp_addr, 4);
310
311 /* Write the register into the stack frame. */
312 regcache_raw_collect (regcache, i, buf);
313 write_memory (sp + i386obsd_uthread_reg_offset[i], buf, 4);
314 }
315 }
316}
508fbfea
MK
317\f
318/* Kernel debugging support. */
319
320/* From <machine/frame.h>. Note that %esp and %ess are only saved in
321 a trap frame when entering the kernel from user space. */
322static int i386obsd_tf_reg_offset[] =
323{
324 10 * 4, /* %eax */
325 9 * 4, /* %ecx */
326 8 * 4, /* %edx */
327 7 * 4, /* %ebx */
328 -1, /* %esp */
329 6 * 4, /* %ebp */
330 5 * 4, /* %esi */
331 4 * 4, /* %edi */
332 13 * 4, /* %eip */
333 15 * 4, /* %eflags */
334 14 * 4, /* %cs */
335 -1, /* %ss */
336 3 * 4, /* %ds */
337 2 * 4, /* %es */
338 0 * 4, /* %fs */
339 1 * 4 /* %gs */
340};
341
342static struct trad_frame_cache *
343i386obsd_trapframe_cache(struct frame_info *next_frame, void **this_cache)
344{
345 struct trad_frame_cache *cache;
346 CORE_ADDR func, sp, addr;
347 ULONGEST cs;
7238f002 348 char *name;
508fbfea
MK
349 int i;
350
351 if (*this_cache)
352 return *this_cache;
353
354 cache = trad_frame_cache_zalloc (next_frame);
355 *this_cache = cache;
356
357 func = frame_func_unwind (next_frame);
358 sp = frame_unwind_register_unsigned (next_frame, I386_ESP_REGNUM);
7238f002
MK
359
360 find_pc_partial_function (func, &name, NULL, NULL);
361 if (name && strncmp(name, "Xintr", 5) == 0)
362 addr = sp + 8; /* It's an interrupt frame. */
363 else
364 addr = sp;
365
508fbfea
MK
366 for (i = 0; i < ARRAY_SIZE (i386obsd_tf_reg_offset); i++)
367 if (i386obsd_tf_reg_offset[i] != -1)
7238f002 368 trad_frame_set_reg_addr (cache, i, addr + i386obsd_tf_reg_offset[i]);
508fbfea
MK
369
370 /* Read %cs from trap frame. */
7238f002 371 addr += i386obsd_tf_reg_offset[I386_CS_REGNUM];
508fbfea
MK
372 cs = read_memory_unsigned_integer (addr, 4);
373 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
374 {
375 /* Trap from use space; terminate backtrace. */
376 trad_frame_set_id (cache, null_frame_id);
377 }
378 else
379 {
380 /* Construct the frame ID using the function start. */
381 trad_frame_set_id (cache, frame_id_build (sp + 8, func));
382 }
383
384 return cache;
385}
386
387static void
388i386obsd_trapframe_this_id (struct frame_info *next_frame,
389 void **this_cache, struct frame_id *this_id)
390{
391 struct trad_frame_cache *cache =
392 i386obsd_trapframe_cache (next_frame, this_cache);
393
394 trad_frame_get_id (cache, this_id);
395}
396
397static void
398i386obsd_trapframe_prev_register (struct frame_info *next_frame,
399 void **this_cache, int regnum,
400 int *optimizedp, enum lval_type *lvalp,
401 CORE_ADDR *addrp, int *realnump,
402 gdb_byte *valuep)
403{
404 struct trad_frame_cache *cache =
405 i386obsd_trapframe_cache (next_frame, this_cache);
406
407 trad_frame_get_register (cache, next_frame, regnum,
408 optimizedp, lvalp, addrp, realnump, valuep);
409}
410
7238f002
MK
411static int
412i386obsd_trapframe_sniffer (const struct frame_unwind *self,
413 struct frame_info *next_frame,
414 void **this_prologue_cache)
508fbfea
MK
415{
416 ULONGEST cs;
417 char *name;
418
419 cs = frame_unwind_register_unsigned (next_frame, I386_CS_REGNUM);
420 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
421 return NULL;
422
423 find_pc_partial_function (frame_pc_unwind (next_frame), &name, NULL, NULL);
7238f002
MK
424 return (name && ((strcmp (name, "calltrap") == 0)
425 || (strcmp (name, "syscall1") == 0)
426 || (strncmp (name, "Xintr", 5) == 0)));
508fbfea 427}
7238f002
MK
428
429static const struct frame_unwind i386obsd_trapframe_unwind = {
430 /* FIXME: kettenis/20051219: This really is more like an interrupt
431 frame, but SIGTRAMP_FRAME would print <signal handler called>,
432 which really is not what we want here. */
433 NORMAL_FRAME,
434 i386obsd_trapframe_this_id,
435 i386obsd_trapframe_prev_register,
436 NULL,
437 i386obsd_trapframe_sniffer
438};
508fbfea 439\f
a28109e0 440
005328e3
MK
441static void
442i386obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
443{
444 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
445
446 /* Obviously OpenBSD is BSD-based. */
447 i386bsd_init_abi (info, gdbarch);
448
67457012
MK
449 /* OpenBSD has a different `struct reg'. */
450 tdep->gregset_reg_offset = i386obsd_r_reg_offset;
451 tdep->gregset_num_regs = ARRAY_SIZE (i386obsd_r_reg_offset);
452 tdep->sizeof_gregset = 16 * 4;
453
005328e3
MK
454 /* OpenBSD uses -freg-struct-return by default. */
455 tdep->struct_return = reg_struct_return;
456
457 /* OpenBSD uses a different memory layout. */
5d93ae8c
MK
458 tdep->sigtramp_start = i386obsd_sigtramp_start_addr;
459 tdep->sigtramp_end = i386obsd_sigtramp_end_addr;
911bc6ee 460 tdep->sigtramp_p = i386obsd_sigtramp_p;
005328e3
MK
461
462 /* OpenBSD has a `struct sigcontext' that's different from the
f2e7c15d 463 original 4.3 BSD. */
a3386186 464 tdep->sc_reg_offset = i386obsd_sc_reg_offset;
67457012 465 tdep->sc_num_regs = ARRAY_SIZE (i386obsd_sc_reg_offset);
a28109e0
MK
466
467 /* OpenBSD provides a user-level threads implementation. */
468 bsd_uthread_set_supply_uthread (gdbarch, i386obsd_supply_uthread);
469 bsd_uthread_set_collect_uthread (gdbarch, i386obsd_collect_uthread);
508fbfea
MK
470
471 /* Unwind kernel trap frames correctly. */
7238f002 472 frame_unwind_prepend_unwinder (gdbarch, &i386obsd_trapframe_unwind);
005328e3 473}
60a6eeb6
MK
474
475/* OpenBSD a.out. */
476
477static void
478i386obsd_aout_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
479{
480 i386obsd_init_abi (info, gdbarch);
481
482 /* OpenBSD a.out has a single register set. */
483 set_gdbarch_regset_from_core_section
484 (gdbarch, i386obsd_aout_regset_from_core_section);
485}
486
487/* OpenBSD ELF. */
488
489static void
490i386obsd_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
491{
492 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
493
494 /* It's still OpenBSD. */
495 i386obsd_init_abi (info, gdbarch);
496
497 /* But ELF-based. */
498 i386_elf_init_abi (info, gdbarch);
499
500 /* OpenBSD ELF uses SVR4-style shared libraries. */
60a6eeb6
MK
501 set_solib_svr4_fetch_link_map_offsets
502 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
503}
67457012
MK
504\f
505
506/* Provide a prototype to silence -Wmissing-prototypes. */
507void _initialize_i386obsd_tdep (void);
005328e3
MK
508
509void
510_initialize_i386obsd_tdep (void)
511{
005328e3
MK
512 /* FIXME: kettenis/20021020: Since OpenBSD/i386 binaries are
513 indistingushable from NetBSD/i386 a.out binaries, building a GDB
514 that should support both these targets will probably not work as
515 expected. */
516#define GDB_OSABI_OPENBSD_AOUT GDB_OSABI_NETBSD_AOUT
517
05816f70 518 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD_AOUT,
60a6eeb6
MK
519 i386obsd_aout_init_abi);
520 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD_ELF,
521 i386obsd_elf_init_abi);
005328e3 522}
This page took 0.422702 seconds and 4 git commands to generate.