1 /* Target-dependent code for OpenBSD/i386.
3 Copyright (C) 1988-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "arch-utils.h"
23 #include "frame-unwind.h"
31 #include "trad-frame.h"
33 #include "obsd-tdep.h"
34 #include "i386-tdep.h"
35 #include "i387-tdep.h"
36 #include "solib-svr4.h"
37 #include "bsd-uthread.h"
39 /* Support for signal handlers. */
41 /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
42 in virtual memory. The randomness makes it somewhat tricky to
43 detect it, but fortunately we can rely on the fact that the start
44 of the sigtramp routine is page-aligned. We recognize the
45 trampoline by looking for the code that invokes the sigreturn
46 system call. The offset where we can find that code varies from
49 By the way, the mapping mentioned above is read-only, so you cannot
50 place a breakpoint in the signal trampoline. */
52 /* Default page size. */
53 static const int i386obsd_page_size
= 4096;
55 /* Offset for sigreturn(2). */
56 static const int i386obsd_sigreturn_offset
[] = {
57 0x0a, /* OpenBSD 3.2 */
58 0x14, /* OpenBSD 3.6 */
59 0x3a, /* OpenBSD 3.8 */
63 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
67 i386obsd_sigtramp_p (struct frame_info
*this_frame
)
69 CORE_ADDR pc
= get_frame_pc (this_frame
);
70 CORE_ADDR start_pc
= (pc
& ~(i386obsd_page_size
- 1));
71 /* The call sequence invoking sigreturn(2). */
72 const gdb_byte sigreturn
[] =
75 0x67, 0x00, 0x00, 0x00, /* movl $SYS_sigreturn, %eax */
76 0xcd, 0x80 /* int $0x80 */
78 size_t buflen
= sizeof sigreturn
;
83 /* If the function has a valid symbol name, it isn't a
85 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
89 /* If the function lives in a valid section (even without a starting
90 point) it isn't a trampoline. */
91 if (find_pc_section (pc
) != NULL
)
94 /* Allocate buffer. */
95 buf
= alloca (buflen
);
97 /* Loop over all offsets. */
98 for (offset
= i386obsd_sigreturn_offset
; *offset
!= -1; offset
++)
100 /* If we can't read the instructions, return zero. */
101 if (!safe_frame_unwind_memory (this_frame
, start_pc
+ *offset
,
105 /* Check for sigreturn(2). */
106 if (memcmp (buf
, sigreturn
, buflen
) == 0)
113 /* Mapping between the general-purpose registers in `struct reg'
114 format and GDB's register cache layout. */
116 /* From <machine/reg.h>. */
117 static int i386obsd_r_reg_offset
[] =
138 i386obsd_aout_supply_regset (const struct regset
*regset
,
139 struct regcache
*regcache
, int regnum
,
140 const void *regs
, size_t len
)
142 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
143 const struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
144 const gdb_byte
*gregs
= regs
;
146 gdb_assert (len
>= tdep
->sizeof_gregset
+ I387_SIZEOF_FSAVE
);
148 i386_supply_gregset (regset
, regcache
, regnum
, regs
, tdep
->sizeof_gregset
);
149 i387_supply_fsave (regcache
, regnum
, gregs
+ tdep
->sizeof_gregset
);
152 static const struct regset i386obsd_aout_gregset
=
154 NULL
, i386obsd_aout_supply_regset
, NULL
158 i386obsd_aout_iterate_over_regset_sections (struct gdbarch
*gdbarch
,
159 iterate_over_regset_sections_cb
*cb
,
161 const struct regcache
*regcache
)
163 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
165 /* OpenBSD a.out core dumps don't use seperate register sets for the
166 general-purpose and floating-point registers. */
168 cb (".reg", tdep
->sizeof_gregset
+ I387_SIZEOF_FSAVE
,
169 &i386obsd_aout_gregset
, NULL
, cb_data
);
173 /* Sigtramp routine location for OpenBSD 3.1 and earlier releases. */
174 CORE_ADDR i386obsd_sigtramp_start_addr
= 0xbfbfdf20;
175 CORE_ADDR i386obsd_sigtramp_end_addr
= 0xbfbfdff0;
177 /* From <machine/signal.h>. */
178 int i386obsd_sc_reg_offset
[I386_NUM_GREGS
] =
189 13 * 4, /* %eflags */
198 /* From /usr/src/lib/libpthread/arch/i386/uthread_machdep.c. */
199 static int i386obsd_uthread_reg_offset
[] =
219 /* Offset within the thread structure where we can find the saved
220 stack pointer (%esp). */
221 #define I386OBSD_UTHREAD_ESP_OFFSET 176
224 i386obsd_supply_uthread (struct regcache
*regcache
,
225 int regnum
, CORE_ADDR addr
)
227 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
228 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
229 CORE_ADDR sp_addr
= addr
+ I386OBSD_UTHREAD_ESP_OFFSET
;
234 gdb_assert (regnum
>= -1);
236 if (regnum
== -1 || regnum
== I386_ESP_REGNUM
)
240 /* Fetch stack pointer from thread structure. */
241 sp
= read_memory_unsigned_integer (sp_addr
, 4, byte_order
);
243 /* Adjust the stack pointer such that it looks as if we just
244 returned from _thread_machdep_switch. */
245 offset
= i386obsd_uthread_reg_offset
[I386_EIP_REGNUM
] + 4;
246 store_unsigned_integer (buf
, 4, byte_order
, sp
+ offset
);
247 regcache_raw_supply (regcache
, I386_ESP_REGNUM
, buf
);
250 for (i
= 0; i
< ARRAY_SIZE (i386obsd_uthread_reg_offset
); i
++)
252 if (i386obsd_uthread_reg_offset
[i
] != -1
253 && (regnum
== -1 || regnum
== i
))
255 /* Fetch stack pointer from thread structure (if we didn't
258 sp
= read_memory_unsigned_integer (sp_addr
, 4, byte_order
);
260 /* Read the saved register from the stack frame. */
261 read_memory (sp
+ i386obsd_uthread_reg_offset
[i
], buf
, 4);
262 regcache_raw_supply (regcache
, i
, buf
);
268 i386obsd_collect_uthread (const struct regcache
*regcache
,
269 int regnum
, CORE_ADDR addr
)
271 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
272 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
273 CORE_ADDR sp_addr
= addr
+ I386OBSD_UTHREAD_ESP_OFFSET
;
278 gdb_assert (regnum
>= -1);
280 if (regnum
== -1 || regnum
== I386_ESP_REGNUM
)
284 /* Calculate the stack pointer (frame pointer) that will be
285 stored into the thread structure. */
286 offset
= i386obsd_uthread_reg_offset
[I386_EIP_REGNUM
] + 4;
287 regcache_raw_collect (regcache
, I386_ESP_REGNUM
, buf
);
288 sp
= extract_unsigned_integer (buf
, 4, byte_order
) - offset
;
290 /* Store the stack pointer. */
291 write_memory_unsigned_integer (sp_addr
, 4, byte_order
, sp
);
293 /* The stack pointer was (potentially) modified. Make sure we
294 build a proper stack frame. */
298 for (i
= 0; i
< ARRAY_SIZE (i386obsd_uthread_reg_offset
); i
++)
300 if (i386obsd_uthread_reg_offset
[i
] != -1
301 && (regnum
== -1 || regnum
== i
))
303 /* Fetch stack pointer from thread structure (if we didn't
304 calculate it already). */
306 sp
= read_memory_unsigned_integer (sp_addr
, 4, byte_order
);
308 /* Write the register into the stack frame. */
309 regcache_raw_collect (regcache
, i
, buf
);
310 write_memory (sp
+ i386obsd_uthread_reg_offset
[i
], buf
, 4);
315 /* Kernel debugging support. */
317 /* From <machine/frame.h>. Note that %esp and %ess are only saved in
318 a trap frame when entering the kernel from user space. */
319 static int i386obsd_tf_reg_offset
[] =
330 15 * 4, /* %eflags */
339 static struct trad_frame_cache
*
340 i386obsd_trapframe_cache (struct frame_info
*this_frame
, void **this_cache
)
342 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
343 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
344 struct trad_frame_cache
*cache
;
345 CORE_ADDR func
, sp
, addr
;
353 cache
= trad_frame_cache_zalloc (this_frame
);
356 func
= get_frame_func (this_frame
);
357 sp
= get_frame_register_unsigned (this_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, byte_order
);
372 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
374 /* Trap from user space; terminate backtrace. */
375 trad_frame_set_id (cache
, outer_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
*this_frame
,
388 void **this_cache
, struct frame_id
*this_id
)
390 struct trad_frame_cache
*cache
=
391 i386obsd_trapframe_cache (this_frame
, this_cache
);
393 trad_frame_get_id (cache
, this_id
);
396 static struct value
*
397 i386obsd_trapframe_prev_register (struct frame_info
*this_frame
,
398 void **this_cache
, int regnum
)
400 struct trad_frame_cache
*cache
=
401 i386obsd_trapframe_cache (this_frame
, this_cache
);
403 return trad_frame_get_register (cache
, this_frame
, regnum
);
407 i386obsd_trapframe_sniffer (const struct frame_unwind
*self
,
408 struct frame_info
*this_frame
,
409 void **this_prologue_cache
)
414 /* Check Current Privilege Level and bail out if we're not executing
416 cs
= get_frame_register_unsigned (this_frame
, I386_CS_REGNUM
);
417 if ((cs
& I386_SEL_RPL
) == I386_SEL_UPL
)
420 find_pc_partial_function (get_frame_pc (this_frame
), &name
, NULL
, NULL
);
421 return (name
&& (strcmp (name
, "calltrap") == 0
422 || strcmp (name
, "syscall1") == 0
423 || strncmp (name
, "Xintr", 5) == 0
424 || strncmp (name
, "Xsoft", 5) == 0));
427 static const struct frame_unwind i386obsd_trapframe_unwind
= {
428 /* FIXME: kettenis/20051219: This really is more like an interrupt
429 frame, but SIGTRAMP_FRAME would print <signal handler called>,
430 which really is not what we want here. */
432 default_frame_unwind_stop_reason
,
433 i386obsd_trapframe_this_id
,
434 i386obsd_trapframe_prev_register
,
436 i386obsd_trapframe_sniffer
441 i386obsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
443 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
445 /* Obviously OpenBSD is BSD-based. */
446 i386bsd_init_abi (info
, gdbarch
);
447 obsd_init_abi (info
, gdbarch
);
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;
454 /* OpenBSD uses -freg-struct-return by default. */
455 tdep
->struct_return
= reg_struct_return
;
457 /* OpenBSD uses a different memory layout. */
458 tdep
->sigtramp_start
= i386obsd_sigtramp_start_addr
;
459 tdep
->sigtramp_end
= i386obsd_sigtramp_end_addr
;
460 tdep
->sigtramp_p
= i386obsd_sigtramp_p
;
462 /* OpenBSD has a `struct sigcontext' that's different from the
464 tdep
->sc_reg_offset
= i386obsd_sc_reg_offset
;
465 tdep
->sc_num_regs
= ARRAY_SIZE (i386obsd_sc_reg_offset
);
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
);
471 /* Unwind kernel trap frames correctly. */
472 frame_unwind_prepend_unwinder (gdbarch
, &i386obsd_trapframe_unwind
);
478 i386obsd_aout_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
480 i386obsd_init_abi (info
, gdbarch
);
482 /* OpenBSD a.out has a single register set. */
483 set_gdbarch_iterate_over_regset_sections
484 (gdbarch
, i386obsd_aout_iterate_over_regset_sections
);
490 i386obsd_elf_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
492 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
494 /* It's still OpenBSD. */
495 i386obsd_init_abi (info
, gdbarch
);
498 i386_elf_init_abi (info
, gdbarch
);
500 /* OpenBSD ELF uses SVR4-style shared libraries. */
501 set_solib_svr4_fetch_link_map_offsets
502 (gdbarch
, svr4_ilp32_fetch_link_map_offsets
);
506 /* Provide a prototype to silence -Wmissing-prototypes. */
507 void _initialize_i386obsd_tdep (void);
510 _initialize_i386obsd_tdep (void)
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
516 #define GDB_OSABI_OPENBSD_AOUT GDB_OSABI_NETBSD_AOUT
518 gdbarch_register_osabi (bfd_arch_i386
, 0, GDB_OSABI_OPENBSD_AOUT
,
519 i386obsd_aout_init_abi
);
520 gdbarch_register_osabi (bfd_arch_i386
, 0, GDB_OSABI_OPENBSD_ELF
,
521 i386obsd_elf_init_abi
);