1 /* Target-dependent code for OpenBSD/i386.
3 Copyright (C) 1988-1989, 1991-1992, 1994, 1996, 2000-2012 Free
4 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include "arch-utils.h"
24 #include "frame-unwind.h"
32 #include "trad-frame.h"
34 #include "gdb_assert.h"
35 #include "gdb_string.h"
37 #include "i386-tdep.h"
38 #include "i387-tdep.h"
39 #include "solib-svr4.h"
40 #include "bsd-uthread.h"
42 /* Support for signal handlers. */
44 /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
45 in virtual memory. The randomness makes it somewhat tricky to
46 detect it, but fortunately we can rely on the fact that the start
47 of the sigtramp routine is page-aligned. We recognize the
48 trampoline by looking for the code that invokes the sigreturn
49 system call. The offset where we can find that code varies from
52 By the way, the mapping mentioned above is read-only, so you cannot
53 place a breakpoint in the signal trampoline. */
55 /* Default page size. */
56 static const int i386obsd_page_size
= 4096;
58 /* Offset for sigreturn(2). */
59 static const int i386obsd_sigreturn_offset
[] = {
60 0x0a, /* OpenBSD 3.2 */
61 0x14, /* OpenBSD 3.6 */
62 0x3a, /* OpenBSD 3.8 */
66 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
70 i386obsd_sigtramp_p (struct frame_info
*this_frame
)
72 CORE_ADDR pc
= get_frame_pc (this_frame
);
73 CORE_ADDR start_pc
= (pc
& ~(i386obsd_page_size
- 1));
74 /* The call sequence invoking sigreturn(2). */
75 const gdb_byte sigreturn
[] =
78 0x67, 0x00, 0x00, 0x00, /* movl $SYS_sigreturn, %eax */
79 0xcd, 0x80 /* int $0x80 */
81 size_t buflen
= sizeof sigreturn
;
86 /* If the function has a valid symbol name, it isn't a
88 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
92 /* If the function lives in a valid section (even without a starting
93 point) it isn't a trampoline. */
94 if (find_pc_section (pc
) != NULL
)
97 /* Allocate buffer. */
98 buf
= alloca (buflen
);
100 /* Loop over all offsets. */
101 for (offset
= i386obsd_sigreturn_offset
; *offset
!= -1; offset
++)
103 /* If we can't read the instructions, return zero. */
104 if (!safe_frame_unwind_memory (this_frame
, start_pc
+ *offset
,
108 /* Check for sigreturn(2). */
109 if (memcmp (buf
, sigreturn
, buflen
) == 0)
116 /* Mapping between the general-purpose registers in `struct reg'
117 format and GDB's register cache layout. */
119 /* From <machine/reg.h>. */
120 static int i386obsd_r_reg_offset
[] =
141 i386obsd_aout_supply_regset (const struct regset
*regset
,
142 struct regcache
*regcache
, int regnum
,
143 const void *regs
, size_t len
)
145 const struct gdbarch_tdep
*tdep
= gdbarch_tdep (regset
->arch
);
146 const gdb_byte
*gregs
= regs
;
148 gdb_assert (len
>= tdep
->sizeof_gregset
+ I387_SIZEOF_FSAVE
);
150 i386_supply_gregset (regset
, regcache
, regnum
, regs
, tdep
->sizeof_gregset
);
151 i387_supply_fsave (regcache
, regnum
, gregs
+ tdep
->sizeof_gregset
);
154 static const struct regset
*
155 i386obsd_aout_regset_from_core_section (struct gdbarch
*gdbarch
,
156 const char *sect_name
,
159 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
161 /* OpenBSD a.out core dumps don't use seperate register sets for the
162 general-purpose and floating-point registers. */
164 if (strcmp (sect_name
, ".reg") == 0
165 && sect_size
>= tdep
->sizeof_gregset
+ I387_SIZEOF_FSAVE
)
167 if (tdep
->gregset
== NULL
)
169 regset_alloc (gdbarch
, i386obsd_aout_supply_regset
, NULL
);
170 return tdep
->gregset
;
177 /* Sigtramp routine location for OpenBSD 3.1 and earlier releases. */
178 CORE_ADDR i386obsd_sigtramp_start_addr
= 0xbfbfdf20;
179 CORE_ADDR i386obsd_sigtramp_end_addr
= 0xbfbfdff0;
181 /* From <machine/signal.h>. */
182 int i386obsd_sc_reg_offset
[I386_NUM_GREGS
] =
193 13 * 4, /* %eflags */
202 /* From /usr/src/lib/libpthread/arch/i386/uthread_machdep.c. */
203 static int i386obsd_uthread_reg_offset
[] =
223 /* Offset within the thread structure where we can find the saved
224 stack pointer (%esp). */
225 #define I386OBSD_UTHREAD_ESP_OFFSET 176
228 i386obsd_supply_uthread (struct regcache
*regcache
,
229 int regnum
, CORE_ADDR addr
)
231 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
232 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
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, byte_order
);
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, byte_order
, 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, byte_order
);
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 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
276 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
277 CORE_ADDR sp_addr
= addr
+ I386OBSD_UTHREAD_ESP_OFFSET
;
282 gdb_assert (regnum
>= -1);
284 if (regnum
== -1 || regnum
== I386_ESP_REGNUM
)
288 /* Calculate the stack pointer (frame pointer) that will be
289 stored into the thread structure. */
290 offset
= i386obsd_uthread_reg_offset
[I386_EIP_REGNUM
] + 4;
291 regcache_raw_collect (regcache
, I386_ESP_REGNUM
, buf
);
292 sp
= extract_unsigned_integer (buf
, 4, byte_order
) - offset
;
294 /* Store the stack pointer. */
295 write_memory_unsigned_integer (sp_addr
, 4, byte_order
, sp
);
297 /* The stack pointer was (potentially) modified. Make sure we
298 build a proper stack frame. */
302 for (i
= 0; i
< ARRAY_SIZE (i386obsd_uthread_reg_offset
); i
++)
304 if (i386obsd_uthread_reg_offset
[i
] != -1
305 && (regnum
== -1 || regnum
== i
))
307 /* Fetch stack pointer from thread structure (if we didn't
308 calculate it already). */
310 sp
= read_memory_unsigned_integer (sp_addr
, 4, byte_order
);
312 /* Write the register into the stack frame. */
313 regcache_raw_collect (regcache
, i
, buf
);
314 write_memory (sp
+ i386obsd_uthread_reg_offset
[i
], buf
, 4);
319 /* Kernel debugging support. */
321 /* From <machine/frame.h>. Note that %esp and %ess are only saved in
322 a trap frame when entering the kernel from user space. */
323 static int i386obsd_tf_reg_offset
[] =
334 15 * 4, /* %eflags */
343 static struct trad_frame_cache
*
344 i386obsd_trapframe_cache (struct frame_info
*this_frame
, void **this_cache
)
346 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
347 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
348 struct trad_frame_cache
*cache
;
349 CORE_ADDR func
, sp
, addr
;
357 cache
= trad_frame_cache_zalloc (this_frame
);
360 func
= get_frame_func (this_frame
);
361 sp
= get_frame_register_unsigned (this_frame
, I386_ESP_REGNUM
);
363 find_pc_partial_function (func
, &name
, NULL
, NULL
);
364 if (name
&& strncmp (name
, "Xintr", 5) == 0)
365 addr
= sp
+ 8; /* It's an interrupt frame. */
369 for (i
= 0; i
< ARRAY_SIZE (i386obsd_tf_reg_offset
); i
++)
370 if (i386obsd_tf_reg_offset
[i
] != -1)
371 trad_frame_set_reg_addr (cache
, i
, addr
+ i386obsd_tf_reg_offset
[i
]);
373 /* Read %cs from trap frame. */
374 addr
+= i386obsd_tf_reg_offset
[I386_CS_REGNUM
];
375 cs
= read_memory_unsigned_integer (addr
, 4, byte_order
);
376 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
378 /* Trap from user space; terminate backtrace. */
379 trad_frame_set_id (cache
, outer_frame_id
);
383 /* Construct the frame ID using the function start. */
384 trad_frame_set_id (cache
, frame_id_build (sp
+ 8, func
));
391 i386obsd_trapframe_this_id (struct frame_info
*this_frame
,
392 void **this_cache
, struct frame_id
*this_id
)
394 struct trad_frame_cache
*cache
=
395 i386obsd_trapframe_cache (this_frame
, this_cache
);
397 trad_frame_get_id (cache
, this_id
);
400 static struct value
*
401 i386obsd_trapframe_prev_register (struct frame_info
*this_frame
,
402 void **this_cache
, int regnum
)
404 struct trad_frame_cache
*cache
=
405 i386obsd_trapframe_cache (this_frame
, this_cache
);
407 return trad_frame_get_register (cache
, this_frame
, regnum
);
411 i386obsd_trapframe_sniffer (const struct frame_unwind
*self
,
412 struct frame_info
*this_frame
,
413 void **this_prologue_cache
)
418 /* Check Current Privilege Level and bail out if we're not executing
420 cs
= get_frame_register_unsigned (this_frame
, I386_CS_REGNUM
);
421 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
424 find_pc_partial_function (get_frame_pc (this_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 default_frame_unwind_stop_reason
,
437 i386obsd_trapframe_this_id
,
438 i386obsd_trapframe_prev_register
,
440 i386obsd_trapframe_sniffer
445 i386obsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
447 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
449 /* Obviously OpenBSD is BSD-based. */
450 i386bsd_init_abi (info
, gdbarch
);
452 /* OpenBSD has a different `struct reg'. */
453 tdep
->gregset_reg_offset
= i386obsd_r_reg_offset
;
454 tdep
->gregset_num_regs
= ARRAY_SIZE (i386obsd_r_reg_offset
);
455 tdep
->sizeof_gregset
= 16 * 4;
457 /* OpenBSD uses -freg-struct-return by default. */
458 tdep
->struct_return
= reg_struct_return
;
460 /* OpenBSD uses a different memory layout. */
461 tdep
->sigtramp_start
= i386obsd_sigtramp_start_addr
;
462 tdep
->sigtramp_end
= i386obsd_sigtramp_end_addr
;
463 tdep
->sigtramp_p
= i386obsd_sigtramp_p
;
465 /* OpenBSD has a `struct sigcontext' that's different from the
467 tdep
->sc_reg_offset
= i386obsd_sc_reg_offset
;
468 tdep
->sc_num_regs
= ARRAY_SIZE (i386obsd_sc_reg_offset
);
470 /* OpenBSD provides a user-level threads implementation. */
471 bsd_uthread_set_supply_uthread (gdbarch
, i386obsd_supply_uthread
);
472 bsd_uthread_set_collect_uthread (gdbarch
, i386obsd_collect_uthread
);
474 /* Unwind kernel trap frames correctly. */
475 frame_unwind_prepend_unwinder (gdbarch
, &i386obsd_trapframe_unwind
);
481 i386obsd_aout_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
483 i386obsd_init_abi (info
, gdbarch
);
485 /* OpenBSD a.out has a single register set. */
486 set_gdbarch_regset_from_core_section
487 (gdbarch
, i386obsd_aout_regset_from_core_section
);
493 i386obsd_elf_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
495 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
497 /* It's still OpenBSD. */
498 i386obsd_init_abi (info
, gdbarch
);
501 i386_elf_init_abi (info
, gdbarch
);
503 /* OpenBSD ELF uses SVR4-style shared libraries. */
504 set_solib_svr4_fetch_link_map_offsets
505 (gdbarch
, svr4_ilp32_fetch_link_map_offsets
);
509 /* Provide a prototype to silence -Wmissing-prototypes. */
510 void _initialize_i386obsd_tdep (void);
513 _initialize_i386obsd_tdep (void)
515 /* FIXME: kettenis/20021020: Since OpenBSD/i386 binaries are
516 indistingushable from NetBSD/i386 a.out binaries, building a GDB
517 that should support both these targets will probably not work as
519 #define GDB_OSABI_OPENBSD_AOUT GDB_OSABI_NETBSD_AOUT
521 gdbarch_register_osabi (bfd_arch_i386
, 0, GDB_OSABI_OPENBSD_AOUT
,
522 i386obsd_aout_init_abi
);
523 gdbarch_register_osabi (bfd_arch_i386
, 0, GDB_OSABI_OPENBSD_ELF
,
524 i386obsd_elf_init_abi
);