1 /* Target-dependent code for OpenBSD/i386.
3 Copyright (C) 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include "arch-utils.h"
26 #include "frame-unwind.h"
34 #include "trad-frame.h"
36 #include "gdb_assert.h"
37 #include "gdb_string.h"
39 #include "i386-tdep.h"
40 #include "i387-tdep.h"
41 #include "solib-svr4.h"
42 #include "bsd-uthread.h"
44 /* Support for signal handlers. */
46 /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
47 in virtual memory. The randomness makes it somewhat tricky to
48 detect it, but fortunately we can rely on the fact that the start
49 of the sigtramp routine is page-aligned. We recognize the
50 trampoline by looking for the code that invokes the sigreturn
51 system call. The offset where we can find that code varies from
54 By the way, the mapping mentioned above is read-only, so you cannot
55 place a breakpoint in the signal trampoline. */
57 /* Default page size. */
58 static const int i386obsd_page_size
= 4096;
60 /* Offset for sigreturn(2). */
61 static const int i386obsd_sigreturn_offset
[] = {
62 0x0a, /* OpenBSD 3.2 */
63 0x14, /* OpenBSD 3.6 */
64 0x3a, /* OpenBSD 3.8 */
68 /* Return whether the frame preceding NEXT_FRAME corresponds to an
69 OpenBSD sigtramp routine. */
72 i386obsd_sigtramp_p (struct frame_info
*next_frame
)
74 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
75 CORE_ADDR start_pc
= (pc
& ~(i386obsd_page_size
- 1));
76 /* The call sequence invoking sigreturn(2). */
77 const gdb_byte sigreturn
[] =
80 0x67, 0x00, 0x00, 0x00, /* movl $SYS_sigreturn, %eax */
81 0xcd, 0x80 /* int $0x80 */
83 size_t buflen
= sizeof sigreturn
;
88 /* If the function has a valid symbol name, it isn't a
90 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
94 /* If the function lives in a valid section (even without a starting
95 point) it isn't a trampoline. */
96 if (find_pc_section (pc
) != NULL
)
99 /* Allocate buffer. */
100 buf
= alloca (buflen
);
102 /* Loop over all offsets. */
103 for (offset
= i386obsd_sigreturn_offset
; *offset
!= -1; offset
++)
105 /* If we can't read the instructions, return zero. */
106 if (!safe_frame_unwind_memory (next_frame
, start_pc
+ *offset
,
110 /* Check for sigreturn(2). */
111 if (memcmp (buf
, sigreturn
, buflen
) == 0)
118 /* Mapping between the general-purpose registers in `struct reg'
119 format and GDB's register cache layout. */
121 /* From <machine/reg.h>. */
122 static int i386obsd_r_reg_offset
[] =
143 i386obsd_aout_supply_regset (const struct regset
*regset
,
144 struct regcache
*regcache
, int regnum
,
145 const void *regs
, size_t len
)
147 const struct gdbarch_tdep
*tdep
= gdbarch_tdep (regset
->arch
);
148 const gdb_byte
*gregs
= regs
;
150 gdb_assert (len
>= tdep
->sizeof_gregset
+ I387_SIZEOF_FSAVE
);
152 i386_supply_gregset (regset
, regcache
, regnum
, regs
, tdep
->sizeof_gregset
);
153 i387_supply_fsave (regcache
, regnum
, gregs
+ tdep
->sizeof_gregset
);
156 static const struct regset
*
157 i386obsd_aout_regset_from_core_section (struct gdbarch
*gdbarch
,
158 const char *sect_name
,
161 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
163 /* OpenBSD a.out core dumps don't use seperate register sets for the
164 general-purpose and floating-point registers. */
166 if (strcmp (sect_name
, ".reg") == 0
167 && sect_size
>= tdep
->sizeof_gregset
+ I387_SIZEOF_FSAVE
)
169 if (tdep
->gregset
== NULL
)
171 regset_alloc (gdbarch
, i386obsd_aout_supply_regset
, NULL
);
172 return tdep
->gregset
;
179 /* Sigtramp routine location for OpenBSD 3.1 and earlier releases. */
180 CORE_ADDR i386obsd_sigtramp_start_addr
= 0xbfbfdf20;
181 CORE_ADDR i386obsd_sigtramp_end_addr
= 0xbfbfdff0;
183 /* From <machine/signal.h>. */
184 int i386obsd_sc_reg_offset
[I386_NUM_GREGS
] =
195 13 * 4, /* %eflags */
204 /* From /usr/src/lib/libpthread/arch/i386/uthread_machdep.c. */
205 static int i386obsd_uthread_reg_offset
[] =
225 /* Offset within the thread structure where we can find the saved
226 stack pointer (%esp). */
227 #define I386OBSD_UTHREAD_ESP_OFFSET 176
230 i386obsd_supply_uthread (struct regcache
*regcache
,
231 int regnum
, CORE_ADDR addr
)
233 CORE_ADDR sp_addr
= addr
+ I386OBSD_UTHREAD_ESP_OFFSET
;
238 gdb_assert (regnum
>= -1);
240 if (regnum
== -1 || regnum
== I386_ESP_REGNUM
)
244 /* Fetch stack pointer from thread structure. */
245 sp
= read_memory_unsigned_integer (sp_addr
, 4);
247 /* Adjust the stack pointer such that it looks as if we just
248 returned from _thread_machdep_switch. */
249 offset
= i386obsd_uthread_reg_offset
[I386_EIP_REGNUM
] + 4;
250 store_unsigned_integer (buf
, 4, sp
+ offset
);
251 regcache_raw_supply (regcache
, I386_ESP_REGNUM
, buf
);
254 for (i
= 0; i
< ARRAY_SIZE (i386obsd_uthread_reg_offset
); i
++)
256 if (i386obsd_uthread_reg_offset
[i
] != -1
257 && (regnum
== -1 || regnum
== i
))
259 /* Fetch stack pointer from thread structure (if we didn't
262 sp
= read_memory_unsigned_integer (sp_addr
, 4);
264 /* Read the saved register from the stack frame. */
265 read_memory (sp
+ i386obsd_uthread_reg_offset
[i
], buf
, 4);
266 regcache_raw_supply (regcache
, i
, buf
);
272 i386obsd_collect_uthread (const struct regcache
*regcache
,
273 int regnum
, CORE_ADDR addr
)
275 CORE_ADDR sp_addr
= addr
+ I386OBSD_UTHREAD_ESP_OFFSET
;
280 gdb_assert (regnum
>= -1);
282 if (regnum
== -1 || regnum
== I386_ESP_REGNUM
)
286 /* Calculate the stack pointer (frame pointer) that will be
287 stored into the thread structure. */
288 offset
= i386obsd_uthread_reg_offset
[I386_EIP_REGNUM
] + 4;
289 regcache_raw_collect (regcache
, I386_ESP_REGNUM
, buf
);
290 sp
= extract_unsigned_integer (buf
, 4) - offset
;
292 /* Store the stack pointer. */
293 write_memory_unsigned_integer (sp_addr
, 4, sp
);
295 /* The stack pointer was (potentially) modified. Make sure we
296 build a proper stack frame. */
300 for (i
= 0; i
< ARRAY_SIZE (i386obsd_uthread_reg_offset
); i
++)
302 if (i386obsd_uthread_reg_offset
[i
] != -1
303 && (regnum
== -1 || regnum
== i
))
305 /* Fetch stack pointer from thread structure (if we didn't
306 calculate it already). */
308 sp
= read_memory_unsigned_integer (sp_addr
, 4);
310 /* Write the register into the stack frame. */
311 regcache_raw_collect (regcache
, i
, buf
);
312 write_memory (sp
+ i386obsd_uthread_reg_offset
[i
], buf
, 4);
317 /* Kernel debugging support. */
319 /* From <machine/frame.h>. Note that %esp and %ess are only saved in
320 a trap frame when entering the kernel from user space. */
321 static int i386obsd_tf_reg_offset
[] =
332 15 * 4, /* %eflags */
341 static struct trad_frame_cache
*
342 i386obsd_trapframe_cache(struct frame_info
*next_frame
, void **this_cache
)
344 struct trad_frame_cache
*cache
;
345 CORE_ADDR func
, sp
, addr
;
353 cache
= trad_frame_cache_zalloc (next_frame
);
356 func
= frame_func_unwind (next_frame
);
357 sp
= frame_unwind_register_unsigned (next_frame
, I386_ESP_REGNUM
);
359 find_pc_partial_function (func
, &name
, NULL
, NULL
);
360 if (name
&& strncmp (name
, "Xintr", 5) == 0)
361 addr
= sp
+ 8; /* It's an interrupt frame. */
365 for (i
= 0; i
< ARRAY_SIZE (i386obsd_tf_reg_offset
); i
++)
366 if (i386obsd_tf_reg_offset
[i
] != -1)
367 trad_frame_set_reg_addr (cache
, i
, addr
+ i386obsd_tf_reg_offset
[i
]);
369 /* Read %cs from trap frame. */
370 addr
+= i386obsd_tf_reg_offset
[I386_CS_REGNUM
];
371 cs
= read_memory_unsigned_integer (addr
, 4);
372 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
374 /* Trap from user space; terminate backtrace. */
375 trad_frame_set_id (cache
, null_frame_id
);
379 /* Construct the frame ID using the function start. */
380 trad_frame_set_id (cache
, frame_id_build (sp
+ 8, func
));
387 i386obsd_trapframe_this_id (struct frame_info
*next_frame
,
388 void **this_cache
, struct frame_id
*this_id
)
390 struct trad_frame_cache
*cache
=
391 i386obsd_trapframe_cache (next_frame
, this_cache
);
393 trad_frame_get_id (cache
, this_id
);
397 i386obsd_trapframe_prev_register (struct frame_info
*next_frame
,
398 void **this_cache
, int regnum
,
399 int *optimizedp
, enum lval_type
*lvalp
,
400 CORE_ADDR
*addrp
, int *realnump
,
403 struct trad_frame_cache
*cache
=
404 i386obsd_trapframe_cache (next_frame
, this_cache
);
406 trad_frame_get_register (cache
, next_frame
, regnum
,
407 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
411 i386obsd_trapframe_sniffer (const struct frame_unwind
*self
,
412 struct frame_info
*next_frame
,
413 void **this_prologue_cache
)
418 /* Check Current Privilege Level and bail out if we're not executing
420 cs
= frame_unwind_register_unsigned (next_frame
, I386_CS_REGNUM
);
421 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
424 find_pc_partial_function (frame_pc_unwind (next_frame
), &name
, NULL
, NULL
);
425 return (name
&& (strcmp (name
, "calltrap") == 0
426 || strcmp (name
, "syscall1") == 0
427 || strncmp (name
, "Xintr", 5) == 0
428 || strncmp (name
, "Xsoft", 5) == 0));
431 static const struct frame_unwind i386obsd_trapframe_unwind
= {
432 /* FIXME: kettenis/20051219: This really is more like an interrupt
433 frame, but SIGTRAMP_FRAME would print <signal handler called>,
434 which really is not what we want here. */
436 i386obsd_trapframe_this_id
,
437 i386obsd_trapframe_prev_register
,
439 i386obsd_trapframe_sniffer
444 i386obsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
446 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
448 /* Obviously OpenBSD is BSD-based. */
449 i386bsd_init_abi (info
, gdbarch
);
451 /* OpenBSD has a different `struct reg'. */
452 tdep
->gregset_reg_offset
= i386obsd_r_reg_offset
;
453 tdep
->gregset_num_regs
= ARRAY_SIZE (i386obsd_r_reg_offset
);
454 tdep
->sizeof_gregset
= 16 * 4;
456 /* OpenBSD uses -freg-struct-return by default. */
457 tdep
->struct_return
= reg_struct_return
;
459 /* OpenBSD uses a different memory layout. */
460 tdep
->sigtramp_start
= i386obsd_sigtramp_start_addr
;
461 tdep
->sigtramp_end
= i386obsd_sigtramp_end_addr
;
462 tdep
->sigtramp_p
= i386obsd_sigtramp_p
;
464 /* OpenBSD has a `struct sigcontext' that's different from the
466 tdep
->sc_reg_offset
= i386obsd_sc_reg_offset
;
467 tdep
->sc_num_regs
= ARRAY_SIZE (i386obsd_sc_reg_offset
);
469 /* OpenBSD provides a user-level threads implementation. */
470 bsd_uthread_set_supply_uthread (gdbarch
, i386obsd_supply_uthread
);
471 bsd_uthread_set_collect_uthread (gdbarch
, i386obsd_collect_uthread
);
473 /* Unwind kernel trap frames correctly. */
474 frame_unwind_prepend_unwinder (gdbarch
, &i386obsd_trapframe_unwind
);
480 i386obsd_aout_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
482 i386obsd_init_abi (info
, gdbarch
);
484 /* OpenBSD a.out has a single register set. */
485 set_gdbarch_regset_from_core_section
486 (gdbarch
, i386obsd_aout_regset_from_core_section
);
492 i386obsd_elf_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
494 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
496 /* It's still OpenBSD. */
497 i386obsd_init_abi (info
, gdbarch
);
500 i386_elf_init_abi (info
, gdbarch
);
502 /* OpenBSD ELF uses SVR4-style shared libraries. */
503 set_solib_svr4_fetch_link_map_offsets
504 (gdbarch
, svr4_ilp32_fetch_link_map_offsets
);
508 /* Provide a prototype to silence -Wmissing-prototypes. */
509 void _initialize_i386obsd_tdep (void);
512 _initialize_i386obsd_tdep (void)
514 /* FIXME: kettenis/20021020: Since OpenBSD/i386 binaries are
515 indistingushable from NetBSD/i386 a.out binaries, building a GDB
516 that should support both these targets will probably not work as
518 #define GDB_OSABI_OPENBSD_AOUT GDB_OSABI_NETBSD_AOUT
520 gdbarch_register_osabi (bfd_arch_i386
, 0, GDB_OSABI_OPENBSD_AOUT
,
521 i386obsd_aout_init_abi
);
522 gdbarch_register_osabi (bfd_arch_i386
, 0, GDB_OSABI_OPENBSD_ELF
,
523 i386obsd_elf_init_abi
);