ARM: Replace regset_alloc() invocations by static regset structures.
[deliverable/binutils-gdb.git] / gdb / i386obsd-tdep.c
CommitLineData
005328e3 1/* Target-dependent code for OpenBSD/i386.
67457012 2
ecd75fc8 3 Copyright (C) 1988-2014 Free Software Foundation, Inc.
005328e3
MK
4
5 This file is part of GDB.
6
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
005328e3
MK
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
005328e3
MK
19
20#include "defs.h"
21#include "arch-utils.h"
911bc6ee 22#include "frame.h"
508fbfea 23#include "frame-unwind.h"
005328e3
MK
24#include "gdbcore.h"
25#include "regcache.h"
67457012 26#include "regset.h"
911bc6ee
MK
27#include "symtab.h"
28#include "objfiles.h"
4be87837 29#include "osabi.h"
5d93ae8c 30#include "target.h"
508fbfea 31#include "trad-frame.h"
005328e3 32
67457012 33#include "gdb_assert.h"
0e9f083f 34#include <string.h>
67457012 35
93ffa5b9 36#include "obsd-tdep.h"
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 83 gdb_byte *buf;
2c02bd72 84 const 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{
09424cff
AA
145 struct gdbarch *gdbarch = get_regcache_arch (regcache);
146 const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3ed85247 147 const gdb_byte *gregs = regs;
67457012
MK
148
149 gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE);
005328e3 150
67457012 151 i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
3ed85247 152 i387_supply_fsave (regcache, regnum, gregs + tdep->sizeof_gregset);
005328e3
MK
153}
154
49cfa46f 155static const struct regset *
67457012
MK
156i386obsd_aout_regset_from_core_section (struct gdbarch *gdbarch,
157 const char *sect_name,
158 size_t sect_size)
005328e3 159{
67457012 160 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
005328e3 161
67457012
MK
162 /* OpenBSD a.out core dumps don't use seperate register sets for the
163 general-purpose and floating-point registers. */
005328e3 164
67457012
MK
165 if (strcmp (sect_name, ".reg") == 0
166 && sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FSAVE)
005328e3 167 {
67457012 168 if (tdep->gregset == NULL)
9ea75c57
MK
169 tdep->gregset =
170 regset_alloc (gdbarch, i386obsd_aout_supply_regset, NULL);
67457012 171 return tdep->gregset;
005328e3
MK
172 }
173
67457012 174 return NULL;
005328e3 175}
005328e3
MK
176\f
177
5d93ae8c
MK
178/* Sigtramp routine location for OpenBSD 3.1 and earlier releases. */
179CORE_ADDR i386obsd_sigtramp_start_addr = 0xbfbfdf20;
180CORE_ADDR i386obsd_sigtramp_end_addr = 0xbfbfdff0;
005328e3
MK
181
182/* From <machine/signal.h>. */
a3386186
MK
183int i386obsd_sc_reg_offset[I386_NUM_GREGS] =
184{
185 10 * 4, /* %eax */
186 9 * 4, /* %ecx */
187 8 * 4, /* %edx */
188 7 * 4, /* %ebx */
189 14 * 4, /* %esp */
190 6 * 4, /* %ebp */
191 5 * 4, /* %esi */
192 4 * 4, /* %edi */
193 11 * 4, /* %eip */
194 13 * 4, /* %eflags */
195 12 * 4, /* %cs */
196 15 * 4, /* %ss */
197 3 * 4, /* %ds */
198 2 * 4, /* %es */
199 1 * 4, /* %fs */
200 0 * 4 /* %gs */
201};
005328e3 202
a28109e0
MK
203/* From /usr/src/lib/libpthread/arch/i386/uthread_machdep.c. */
204static int i386obsd_uthread_reg_offset[] =
205{
206 11 * 4, /* %eax */
207 10 * 4, /* %ecx */
208 9 * 4, /* %edx */
209 8 * 4, /* %ebx */
210 -1, /* %esp */
211 6 * 4, /* %ebp */
212 5 * 4, /* %esi */
213 4 * 4, /* %edi */
214 12 * 4, /* %eip */
215 -1, /* %eflags */
216 13 * 4, /* %cs */
217 -1, /* %ss */
218 3 * 4, /* %ds */
219 2 * 4, /* %es */
220 1 * 4, /* %fs */
221 0 * 4 /* %gs */
222};
223
224/* Offset within the thread structure where we can find the saved
225 stack pointer (%esp). */
226#define I386OBSD_UTHREAD_ESP_OFFSET 176
227
228static void
229i386obsd_supply_uthread (struct regcache *regcache,
230 int regnum, CORE_ADDR addr)
231{
e17a4113
UW
232 struct gdbarch *gdbarch = get_regcache_arch (regcache);
233 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
a28109e0
MK
234 CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
235 CORE_ADDR sp = 0;
63c0089f 236 gdb_byte buf[4];
a28109e0
MK
237 int i;
238
239 gdb_assert (regnum >= -1);
240
241 if (regnum == -1 || regnum == I386_ESP_REGNUM)
242 {
243 int offset;
244
245 /* Fetch stack pointer from thread structure. */
e17a4113 246 sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);
a28109e0
MK
247
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;
e17a4113 251 store_unsigned_integer (buf, 4, byte_order, sp + offset);
a28109e0
MK
252 regcache_raw_supply (regcache, I386_ESP_REGNUM, buf);
253 }
254
255 for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
256 {
257 if (i386obsd_uthread_reg_offset[i] != -1
258 && (regnum == -1 || regnum == i))
259 {
260 /* Fetch stack pointer from thread structure (if we didn't
261 do so already). */
262 if (sp == 0)
e17a4113 263 sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);
a28109e0
MK
264
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);
268 }
269 }
a28109e0
MK
270}
271
272static void
273i386obsd_collect_uthread (const struct regcache *regcache,
274 int regnum, CORE_ADDR addr)
275{
e17a4113
UW
276 struct gdbarch *gdbarch = get_regcache_arch (regcache);
277 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
a28109e0
MK
278 CORE_ADDR sp_addr = addr + I386OBSD_UTHREAD_ESP_OFFSET;
279 CORE_ADDR sp = 0;
63c0089f 280 gdb_byte buf[4];
a28109e0
MK
281 int i;
282
283 gdb_assert (regnum >= -1);
284
285 if (regnum == -1 || regnum == I386_ESP_REGNUM)
286 {
287 int offset;
288
289 /* Calculate the stack pointer (frame pointer) that will be
290 stored into the thread structure. */
291 offset = i386obsd_uthread_reg_offset[I386_EIP_REGNUM] + 4;
292 regcache_raw_collect (regcache, I386_ESP_REGNUM, buf);
e17a4113 293 sp = extract_unsigned_integer (buf, 4, byte_order) - offset;
a28109e0
MK
294
295 /* Store the stack pointer. */
e17a4113 296 write_memory_unsigned_integer (sp_addr, 4, byte_order, sp);
a28109e0
MK
297
298 /* The stack pointer was (potentially) modified. Make sure we
299 build a proper stack frame. */
300 regnum = -1;
301 }
302
303 for (i = 0; i < ARRAY_SIZE (i386obsd_uthread_reg_offset); i++)
304 {
305 if (i386obsd_uthread_reg_offset[i] != -1
306 && (regnum == -1 || regnum == i))
307 {
308 /* Fetch stack pointer from thread structure (if we didn't
309 calculate it already). */
310 if (sp == 0)
e17a4113 311 sp = read_memory_unsigned_integer (sp_addr, 4, byte_order);
a28109e0
MK
312
313 /* Write the register into the stack frame. */
314 regcache_raw_collect (regcache, i, buf);
315 write_memory (sp + i386obsd_uthread_reg_offset[i], buf, 4);
316 }
317 }
318}
508fbfea
MK
319\f
320/* Kernel debugging support. */
321
322/* From <machine/frame.h>. Note that %esp and %ess are only saved in
323 a trap frame when entering the kernel from user space. */
324static int i386obsd_tf_reg_offset[] =
325{
326 10 * 4, /* %eax */
327 9 * 4, /* %ecx */
328 8 * 4, /* %edx */
329 7 * 4, /* %ebx */
330 -1, /* %esp */
331 6 * 4, /* %ebp */
332 5 * 4, /* %esi */
333 4 * 4, /* %edi */
334 13 * 4, /* %eip */
335 15 * 4, /* %eflags */
336 14 * 4, /* %cs */
337 -1, /* %ss */
338 3 * 4, /* %ds */
339 2 * 4, /* %es */
340 0 * 4, /* %fs */
341 1 * 4 /* %gs */
342};
343
344static struct trad_frame_cache *
10458914 345i386obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
508fbfea 346{
e17a4113
UW
347 struct gdbarch *gdbarch = get_frame_arch (this_frame);
348 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
508fbfea
MK
349 struct trad_frame_cache *cache;
350 CORE_ADDR func, sp, addr;
351 ULONGEST cs;
2c02bd72 352 const char *name;
508fbfea
MK
353 int i;
354
355 if (*this_cache)
356 return *this_cache;
357
10458914 358 cache = trad_frame_cache_zalloc (this_frame);
508fbfea
MK
359 *this_cache = cache;
360
10458914
DJ
361 func = get_frame_func (this_frame);
362 sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
7238f002
MK
363
364 find_pc_partial_function (func, &name, NULL, NULL);
6d566cff 365 if (name && strncmp (name, "Xintr", 5) == 0)
7238f002
MK
366 addr = sp + 8; /* It's an interrupt frame. */
367 else
368 addr = sp;
369
508fbfea
MK
370 for (i = 0; i < ARRAY_SIZE (i386obsd_tf_reg_offset); i++)
371 if (i386obsd_tf_reg_offset[i] != -1)
7238f002 372 trad_frame_set_reg_addr (cache, i, addr + i386obsd_tf_reg_offset[i]);
508fbfea
MK
373
374 /* Read %cs from trap frame. */
7238f002 375 addr += i386obsd_tf_reg_offset[I386_CS_REGNUM];
e17a4113 376 cs = read_memory_unsigned_integer (addr, 4, byte_order);
508fbfea
MK
377 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
378 {
6d566cff 379 /* Trap from user space; terminate backtrace. */
005ca36a 380 trad_frame_set_id (cache, outer_frame_id);
508fbfea
MK
381 }
382 else
383 {
384 /* Construct the frame ID using the function start. */
385 trad_frame_set_id (cache, frame_id_build (sp + 8, func));
386 }
387
388 return cache;
389}
390
391static void
10458914 392i386obsd_trapframe_this_id (struct frame_info *this_frame,
508fbfea
MK
393 void **this_cache, struct frame_id *this_id)
394{
395 struct trad_frame_cache *cache =
10458914 396 i386obsd_trapframe_cache (this_frame, this_cache);
508fbfea
MK
397
398 trad_frame_get_id (cache, this_id);
399}
400
10458914
DJ
401static struct value *
402i386obsd_trapframe_prev_register (struct frame_info *this_frame,
403 void **this_cache, int regnum)
508fbfea
MK
404{
405 struct trad_frame_cache *cache =
10458914 406 i386obsd_trapframe_cache (this_frame, this_cache);
508fbfea 407
10458914 408 return trad_frame_get_register (cache, this_frame, regnum);
508fbfea
MK
409}
410
7238f002
MK
411static int
412i386obsd_trapframe_sniffer (const struct frame_unwind *self,
10458914 413 struct frame_info *this_frame,
7238f002 414 void **this_prologue_cache)
508fbfea
MK
415{
416 ULONGEST cs;
2c02bd72 417 const char *name;
508fbfea 418
e5cc6d11 419 /* Check Current Privilege Level and bail out if we're not executing
6d566cff 420 in kernel space. */
10458914 421 cs = get_frame_register_unsigned (this_frame, I386_CS_REGNUM);
508fbfea 422 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
6d566cff 423 return 0;
508fbfea 424
10458914 425 find_pc_partial_function (get_frame_pc (this_frame), &name, NULL, NULL);
3597fb82
MK
426 return (name && (strcmp (name, "calltrap") == 0
427 || strcmp (name, "syscall1") == 0
428 || strncmp (name, "Xintr", 5) == 0
429 || strncmp (name, "Xsoft", 5) == 0));
508fbfea 430}
7238f002
MK
431
432static const struct frame_unwind i386obsd_trapframe_unwind = {
433 /* FIXME: kettenis/20051219: This really is more like an interrupt
434 frame, but SIGTRAMP_FRAME would print <signal handler called>,
435 which really is not what we want here. */
436 NORMAL_FRAME,
8fbca658 437 default_frame_unwind_stop_reason,
7238f002
MK
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);
93ffa5b9 452 obsd_init_abi (info, gdbarch);
005328e3 453
67457012
MK
454 /* OpenBSD has a different `struct reg'. */
455 tdep->gregset_reg_offset = i386obsd_r_reg_offset;
456 tdep->gregset_num_regs = ARRAY_SIZE (i386obsd_r_reg_offset);
457 tdep->sizeof_gregset = 16 * 4;
458
005328e3
MK
459 /* OpenBSD uses -freg-struct-return by default. */
460 tdep->struct_return = reg_struct_return;
461
462 /* OpenBSD uses a different memory layout. */
5d93ae8c
MK
463 tdep->sigtramp_start = i386obsd_sigtramp_start_addr;
464 tdep->sigtramp_end = i386obsd_sigtramp_end_addr;
911bc6ee 465 tdep->sigtramp_p = i386obsd_sigtramp_p;
005328e3
MK
466
467 /* OpenBSD has a `struct sigcontext' that's different from the
f2e7c15d 468 original 4.3 BSD. */
a3386186 469 tdep->sc_reg_offset = i386obsd_sc_reg_offset;
67457012 470 tdep->sc_num_regs = ARRAY_SIZE (i386obsd_sc_reg_offset);
a28109e0
MK
471
472 /* OpenBSD provides a user-level threads implementation. */
473 bsd_uthread_set_supply_uthread (gdbarch, i386obsd_supply_uthread);
474 bsd_uthread_set_collect_uthread (gdbarch, i386obsd_collect_uthread);
508fbfea
MK
475
476 /* Unwind kernel trap frames correctly. */
7238f002 477 frame_unwind_prepend_unwinder (gdbarch, &i386obsd_trapframe_unwind);
005328e3 478}
60a6eeb6
MK
479
480/* OpenBSD a.out. */
481
482static void
483i386obsd_aout_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
484{
485 i386obsd_init_abi (info, gdbarch);
486
487 /* OpenBSD a.out has a single register set. */
488 set_gdbarch_regset_from_core_section
489 (gdbarch, i386obsd_aout_regset_from_core_section);
490}
491
492/* OpenBSD ELF. */
493
494static void
495i386obsd_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
496{
497 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
498
499 /* It's still OpenBSD. */
500 i386obsd_init_abi (info, gdbarch);
501
502 /* But ELF-based. */
503 i386_elf_init_abi (info, gdbarch);
504
505 /* OpenBSD ELF uses SVR4-style shared libraries. */
60a6eeb6
MK
506 set_solib_svr4_fetch_link_map_offsets
507 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
508}
67457012
MK
509\f
510
511/* Provide a prototype to silence -Wmissing-prototypes. */
512void _initialize_i386obsd_tdep (void);
005328e3
MK
513
514void
515_initialize_i386obsd_tdep (void)
516{
005328e3
MK
517 /* FIXME: kettenis/20021020: Since OpenBSD/i386 binaries are
518 indistingushable from NetBSD/i386 a.out binaries, building a GDB
519 that should support both these targets will probably not work as
520 expected. */
521#define GDB_OSABI_OPENBSD_AOUT GDB_OSABI_NETBSD_AOUT
522
05816f70 523 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD_AOUT,
60a6eeb6
MK
524 i386obsd_aout_init_abi);
525 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_OPENBSD_ELF,
526 i386obsd_elf_init_abi);
005328e3 527}
This page took 1.355041 seconds and 4 git commands to generate.