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