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