1 /* Target-dependent code for OpenBSD/i386.
3 Copyright (C) 1988, 1989, 1991, 1992, 1994, 1996, 2000, 2001, 2002,
5 Free Software Foundation, Inc.
7 This file is part of GDB.
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.
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.
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
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
25 #include "arch-utils.h"
27 #include "frame-unwind.h"
35 #include "trad-frame.h"
37 #include "gdb_assert.h"
38 #include "gdb_string.h"
40 #include "i386-tdep.h"
41 #include "i387-tdep.h"
42 #include "solib-svr4.h"
43 #include "bsd-uthread.h"
45 /* Support for signal handlers. */
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
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
55 By the way, the mapping mentioned above is read-only, so you cannot
56 place a breakpoint in the signal trampoline. */
58 /* Default page size. */
59 static const int i386obsd_page_size = 4096;
61 /* Offset for sigreturn(2). */
62 static const int i386obsd_sigreturn_offset[] = {
63 0x0a, /* OpenBSD 3.2 */
64 0x14, /* OpenBSD 3.6 */
65 0x3a, /* OpenBSD 3.8 */
69 /* Return whether the frame preceding NEXT_FRAME corresponds to an
70 OpenBSD sigtramp routine. */
73 i386obsd_sigtramp_p (struct frame_info *next_frame)
75 CORE_ADDR pc = frame_pc_unwind (next_frame);
76 CORE_ADDR start_pc = (pc & ~(i386obsd_page_size - 1));
77 /* The call sequence invoking sigreturn(2). */
78 const gdb_byte sigreturn[] =
81 0x67, 0x00, 0x00, 0x00, /* movl $SYS_sigreturn, %eax */
82 0xcd, 0x80 /* int $0x80 */
84 size_t buflen = sizeof sigreturn;
89 /* If the function has a valid symbol name, it isn't a
91 find_pc_partial_function (pc, &name, NULL, NULL);
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)
100 /* Allocate buffer. */
101 buf = alloca (buflen);
103 /* Loop over all offsets. */
104 for (offset = i386obsd_sigreturn_offset; *offset != -1; offset++)
106 /* If we can't read the instructions, return zero. */
107 if (!safe_frame_unwind_memory (next_frame, start_pc + *offset,
111 /* Check for sigreturn(2). */
112 if (memcmp (buf, sigreturn, buflen) == 0)
119 /* Mapping between the general-purpose registers in `struct reg'
120 format and GDB's register cache layout. */
122 /* From <machine/reg.h>. */
123 static int i386obsd_r_reg_offset[] =
144 i386obsd_aout_supply_regset (const struct regset *regset,
145 struct regcache *regcache, int regnum,
146 const void *regs, size_t len)
148 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
149 const gdb_byte *gregs = regs;
151 gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE);
153 i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
154 i387_supply_fsave (regcache, regnum, gregs + tdep->sizeof_gregset);
157 static const struct regset *
158 i386obsd_aout_regset_from_core_section (struct gdbarch *gdbarch,
159 const char *sect_name,
162 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
164 /* OpenBSD a.out core dumps don't use seperate register sets for the
165 general-purpose and floating-point registers. */
167 if (strcmp (sect_name, ".reg") == 0
168 && sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE)
170 if (tdep->gregset == NULL)
172 regset_alloc (gdbarch, i386obsd_aout_supply_regset, NULL);
173 return tdep->gregset;
180 /* Sigtramp routine location for OpenBSD 3.1 and earlier releases. */
181 CORE_ADDR i386obsd_sigtramp_start_addr = 0xbfbfdf20;
182 CORE_ADDR i386obsd_sigtramp_end_addr = 0xbfbfdff0;
184 /* From <machine/signal.h>. */
185 int i386obsd_sc_reg_offset[I386_NUM_GREGS] =
196 13 * 4, /* %eflags */
205 /* From /usr/src/lib/libpthread/arch/i386/uthread_machdep.c. */
206 static int i386obsd_uthread_reg_offset[] =
226 /* Offset within the thread structure where we can find the saved
227 stack pointer (%esp). */
228 #define I386OBSD_UTHREAD_ESP_OFFSET 176
231 i386obsd_supply_uthread (struct regcache *regcache,
232 int regnum, CORE_ADDR addr)
234 CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
239 gdb_assert (regnum >= -1);
241 if (regnum == -1 || regnum == I386_ESP_REGNUM)
245 /* Fetch stack pointer from thread structure. */
246 sp = read_memory_unsigned_integer (sp_addr, 4);
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);
255 for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
257 if (i386obsd_uthread_reg_offset[i] != -1
258 && (regnum == -1 || regnum == i))
260 /* Fetch stack pointer from thread structure (if we didn't
263 sp = read_memory_unsigned_integer (sp_addr, 4);
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);
273 i386obsd_collect_uthread (const struct regcache *regcache,
274 int regnum, CORE_ADDR addr)
276 CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
281 gdb_assert (regnum >= -1);
283 if (regnum == -1 || regnum == I386_ESP_REGNUM)
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;
293 /* Store the stack pointer. */
294 write_memory_unsigned_integer (sp_addr, 4, sp);
296 /* The stack pointer was (potentially) modified. Make sure we
297 build a proper stack frame. */
301 for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
303 if (i386obsd_uthread_reg_offset[i] != -1
304 && (regnum == -1 || regnum == i))
306 /* Fetch stack pointer from thread structure (if we didn't
307 calculate it already). */
309 sp = read_memory_unsigned_integer (sp_addr, 4);
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);
318 /* Kernel debugging support. */
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. */
322 static int i386obsd_tf_reg_offset[] =
333 15 * 4, /* %eflags */
342 static struct trad_frame_cache *
343 i386obsd_trapframe_cache(struct frame_info *next_frame, void **this_cache)
345 struct trad_frame_cache *cache;
346 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);
358 for (i = 0; i < ARRAY_SIZE (i386obsd_tf_reg_offset); i++)
359 if (i386obsd_tf_reg_offset[i] != -1)
360 trad_frame_set_reg_addr (cache, i, sp + i386obsd_tf_reg_offset[i]);
362 /* Read %cs from trap frame. */
363 addr = sp + i386obsd_tf_reg_offset[I386_CS_REGNUM];
364 cs = read_memory_unsigned_integer (addr, 4);
365 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
367 /* Trap from use space; terminate backtrace. */
368 trad_frame_set_id (cache, null_frame_id);
372 /* Construct the frame ID using the function start. */
373 trad_frame_set_id (cache, frame_id_build (sp + 8, func));
380 i386obsd_trapframe_this_id (struct frame_info *next_frame,
381 void **this_cache, struct frame_id *this_id)
383 struct trad_frame_cache *cache =
384 i386obsd_trapframe_cache (next_frame, this_cache);
386 trad_frame_get_id (cache, this_id);
390 i386obsd_trapframe_prev_register (struct frame_info *next_frame,
391 void **this_cache, int regnum,
392 int *optimizedp, enum lval_type *lvalp,
393 CORE_ADDR *addrp, int *realnump,
396 struct trad_frame_cache *cache =
397 i386obsd_trapframe_cache (next_frame, this_cache);
399 trad_frame_get_register (cache, next_frame, regnum,
400 optimizedp, lvalp, addrp, realnump, valuep);
403 static const struct frame_unwind i386obsd_trapframe_unwind = {
404 /* FIXME: kettenis/20051219: This really is more like an interrupt
405 frame, but SIGTRAMP_FRAME would print <signal handler called>,
406 which really is not what we want here. */
408 i386obsd_trapframe_this_id,
409 i386obsd_trapframe_prev_register
412 static const struct frame_unwind *
413 i386obsd_trapframe_sniffer (struct frame_info *next_frame)
418 cs = frame_unwind_register_unsigned (next_frame, I386_CS_REGNUM);
419 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
422 find_pc_partial_function (frame_pc_unwind (next_frame), &name, NULL, NULL);
423 if (name && ((strcmp ("calltrap", name) == 0)
424 || (strcmp ("syscall1", name) == 0)))
425 return &i386obsd_trapframe_unwind;
432 i386obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
434 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
436 /* Obviously OpenBSD is BSD-based. */
437 i386bsd_init_abi (info, gdbarch);
439 /* OpenBSD has a different `struct reg'. */
440 tdep->gregset_reg_offset = i386obsd_r_reg_offset;
441 tdep->gregset_num_regs = ARRAY_SIZE (i386obsd_r_reg_offset);
442 tdep->sizeof_gregset = 16 * 4;
444 /* OpenBSD uses -freg-struct-return by default. */
445 tdep->struct_return = reg_struct_return;
447 /* OpenBSD uses a different memory layout. */
448 tdep->sigtramp_start = i386obsd_sigtramp_start_addr;
449 tdep->sigtramp_end = i386obsd_sigtramp_end_addr;
450 tdep->sigtramp_p = i386obsd_sigtramp_p;
452 /* OpenBSD has a `struct sigcontext' that's different from the
454 tdep->sc_reg_offset = i386obsd_sc_reg_offset;
455 tdep->sc_num_regs = ARRAY_SIZE (i386obsd_sc_reg_offset);
457 /* OpenBSD provides a user-level threads implementation. */
458 bsd_uthread_set_supply_uthread (gdbarch, i386obsd_supply_uthread);
459 bsd_uthread_set_collect_uthread (gdbarch, i386obsd_collect_uthread);
461 /* Unwind kernel trap frames correctly. */
462 frame_unwind_append_sniffer (gdbarch, i386obsd_trapframe_sniffer);
468 i386obsd_aout_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
470 i386obsd_init_abi (info, gdbarch);
472 /* OpenBSD a.out has a single register set. */
473 set_gdbarch_regset_from_core_section
474 (gdbarch, i386obsd_aout_regset_from_core_section);
480 i386obsd_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
482 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
484 /* It's still OpenBSD. */
485 i386obsd_init_abi (info, gdbarch);
488 i386_elf_init_abi (info, gdbarch);
490 /* OpenBSD ELF uses SVR4-style shared libraries. */
491 set_solib_svr4_fetch_link_map_offsets
492 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
496 /* Provide a prototype to silence -Wmissing-prototypes. */
497 void _initialize_i386obsd_tdep (void);
500 _initialize_i386obsd_tdep (void)
502 /* FIXME: kettenis/20021020: Since OpenBSD/i386 binaries are
503 indistingushable from NetBSD/i386 a.out binaries, building a GDB
504 that should support both these targets will probably not work as
506 #define GDB_OSABI_OPENBSD_AOUT GDB_OSABI_NETBSD_AOUT
508 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD_AOUT,
509 i386obsd_aout_init_abi);
510 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD_ELF,
511 i386obsd_elf_init_abi);