* infptrace.c: Remove #ifdef'ed out inclusion of gdb_stat.h.
[deliverable/binutils-gdb.git] / gdb / amd64obsd-tdep.c
CommitLineData
e2879ccb
MK
1/* Target-dependent code for OpenBSD/amd64.
2
3 Copyright 2003, 2004 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 2 of the License, or
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
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "defs.h"
23#include "frame.h"
24#include "gdbcore.h"
911bc6ee
MK
25#include "symtab.h"
26#include "objfiles.h"
e2879ccb 27#include "osabi.h"
30b344b1 28#include "regset.h"
e2879ccb
MK
29#include "target.h"
30
31#include "gdb_assert.h"
32#include "gdb_string.h"
33
85be1ca6 34#include "amd64-tdep.h"
30b344b1 35#include "i387-tdep.h"
7e654c37 36#include "solib-svr4.h"
30b344b1
MK
37
38/* Support for core dumps. */
39
40static void
41amd64obsd_supply_regset (const struct regset *regset,
42 struct regcache *regcache, int regnum,
43 const void *regs, size_t len)
44{
9ea75c57 45 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
30b344b1
MK
46
47 gdb_assert (len >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE);
48
49 i386_supply_gregset (regset, regcache, regnum, regs, tdep->sizeof_gregset);
90f90721 50 amd64_supply_fxsave (regcache, regnum, (char *)regs + tdep->sizeof_gregset);
30b344b1
MK
51}
52
53static const struct regset *
54amd64obsd_regset_from_core_section (struct gdbarch *gdbarch,
55 const char *sect_name, size_t sect_size)
56{
57 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
58
59 /* OpenBSD core dumps don't use seperate register sets for the
60 general-purpose and floating-point registers. */
61
62 if (strcmp (sect_name, ".reg") == 0
63 && sect_size >= tdep->sizeof_gregset + I387_SIZEOF_FXSAVE)
64 {
65 if (tdep->gregset == NULL)
9ea75c57 66 tdep->gregset = regset_alloc (gdbarch, amd64obsd_supply_regset, NULL);
30b344b1
MK
67 return tdep->gregset;
68 }
69
70 return NULL;
71}
72\f
e2879ccb
MK
73
74/* Support for signal handlers. */
75
911bc6ee 76/* Default page size. */
e2879ccb
MK
77static const int amd64obsd_page_size = 4096;
78
377d9ebd 79/* Return whether the frame preceding NEXT_FRAME corresponds to an
911bc6ee
MK
80 OpenBSD sigtramp routine. */
81
e2879ccb 82static int
911bc6ee 83amd64obsd_sigtramp_p (struct frame_info *next_frame)
e2879ccb 84{
911bc6ee 85 CORE_ADDR pc = frame_pc_unwind (next_frame);
e2879ccb
MK
86 CORE_ADDR start_pc = (pc & ~(amd64obsd_page_size - 1));
87 const char sigreturn[] =
88 {
89 0x48, 0xc7, 0xc0,
90 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
84d04465 91 0xcd, 0x80 /* int $0x80 */
e2879ccb 92 };
911bc6ee
MK
93 char *name, *buf;
94
95 /* If the function has a valid symbol name, it isn't a
96 trampoline. */
97 find_pc_partial_function (pc, &name, NULL, NULL);
98 if (name != NULL)
99 return 0;
e2879ccb 100
911bc6ee
MK
101 /* If the function lives in a valid section (even without a starting
102 point) it isn't a trampoline. */
103 if (find_pc_section (pc) != NULL)
e2879ccb
MK
104 return 0;
105
106 /* If we can't read the instructions at START_PC, return zero. */
4cd28409
MK
107 buf = alloca ((sizeof sigreturn) + 1);
108 if (target_read_memory (start_pc + 6, buf, (sizeof sigreturn) + 1))
e2879ccb
MK
109 return 0;
110
4cd28409
MK
111 /* Check for sigreturn(2). Depending on how the assembler encoded
112 the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
113 7. */
114 if (memcmp (buf, sigreturn, sizeof sigreturn)
115 && memcpy (buf + 1, sigreturn, sizeof sigreturn))
e2879ccb
MK
116 return 0;
117
118 return 1;
119}
120
121/* Assuming NEXT_FRAME is for a frame following a BSD sigtramp
122 routine, return the address of the associated sigcontext structure. */
123
124static CORE_ADDR
125amd64obsd_sigcontext_addr (struct frame_info *next_frame)
126{
0fe85704
MK
127 CORE_ADDR pc = frame_pc_unwind (next_frame);
128 ULONGEST offset = (pc & (amd64obsd_page_size - 1));
129
e2879ccb 130 /* The %rsp register points at `struct sigcontext' upon entry of a
0fe85704
MK
131 signal trampoline. The relevant part of the trampoline is
132
133 call *%rax
134 movq %rsp, %rdi
135 pushq %rdi
136 movq $SYS_sigreturn,%rax
137 int $0x80
138
139 (see /usr/src/sys/arch/amd64/amd64/locore.S). The `pushq'
140 instruction clobbers %rsp, but its value is saved in `%rdi'. */
141
4cd28409 142 if (offset > 5)
0fe85704
MK
143 return frame_unwind_register_unsigned (next_frame, AMD64_RDI_REGNUM);
144 else
145 return frame_unwind_register_unsigned (next_frame, AMD64_RSP_REGNUM);
e2879ccb
MK
146}
147\f
148/* OpenBSD 3.5 or later. */
149
150/* Mapping between the general-purpose registers in `struct reg'
151 format and GDB's register cache layout. */
152
30b344b1 153/* From <machine/reg.h>. */
e2879ccb
MK
154int amd64obsd_r_reg_offset[] =
155{
156 14 * 8, /* %rax */
157 13 * 8, /* %rbx */
158 3 * 8, /* %rcx */
159 2 * 8, /* %rdx */
160 1 * 8, /* %rsi */
161 0 * 8, /* %rdi */
162 12 * 8, /* %rbp */
163 15 * 8, /* %rsp */
164 4 * 8, /* %r8 .. */
165 5 * 8,
166 6 * 8,
167 7 * 8,
168 8 * 8,
169 9 * 8,
170 10 * 8,
171 11 * 8, /* ... %r15 */
172 16 * 8, /* %rip */
173 17 * 8, /* %eflags */
174 18 * 8, /* %cs */
175 19 * 8, /* %ss */
176 20 * 8, /* %ds */
177 21 * 8, /* %es */
178 22 * 8, /* %fs */
179 23 * 8 /* %gs */
180};
181
30b344b1 182/* From <machine/signal.h>. */
e2879ccb
MK
183static int amd64obsd_sc_reg_offset[] =
184{
185 14 * 8, /* %rax */
186 13 * 8, /* %rbx */
187 3 * 8, /* %rcx */
188 2 * 8, /* %rdx */
189 1 * 8, /* %rsi */
190 0 * 8, /* %rdi */
191 12 * 8, /* %rbp */
192 24 * 8, /* %rsp */
193 4 * 8, /* %r8 ... */
194 5 * 8,
195 6 * 8,
196 7 * 8,
197 8 * 8,
198 9 * 8,
199 10 * 8,
200 11 * 8, /* ... %r15 */
201 21 * 8, /* %rip */
202 23 * 8, /* %eflags */
203 22 * 8, /* %cs */
204 25 * 8, /* %ss */
205 18 * 8, /* %ds */
206 17 * 8, /* %es */
207 16 * 8, /* %fs */
208 15 * 8 /* %gs */
209};
210
211static void
212amd64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
213{
214 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
215
90f90721 216 amd64_init_abi (info, gdbarch);
e2879ccb 217
30b344b1
MK
218 /* Initialize general-purpose register set details. */
219 tdep->gregset_reg_offset = amd64obsd_r_reg_offset;
220 tdep->gregset_num_regs = ARRAY_SIZE (amd64obsd_r_reg_offset);
221 tdep->sizeof_gregset = 24 * 8;
222
223 set_gdbarch_regset_from_core_section (gdbarch,
224 amd64obsd_regset_from_core_section);
225
e2879ccb
MK
226 tdep->jb_pc_offset = 7 * 8;
227
911bc6ee 228 tdep->sigtramp_p = amd64obsd_sigtramp_p;
e2879ccb
MK
229 tdep->sigcontext_addr = amd64obsd_sigcontext_addr;
230 tdep->sc_reg_offset = amd64obsd_sc_reg_offset;
231 tdep->sc_num_regs = ARRAY_SIZE (amd64obsd_sc_reg_offset);
7e654c37
MK
232
233 /* OpenBSD uses SVR4-style shared libraries. */
234 set_solib_svr4_fetch_link_map_offsets
235 (gdbarch, svr4_lp64_fetch_link_map_offsets);
e2879ccb
MK
236}
237\f
238
239/* Provide a prototype to silence -Wmissing-prototypes. */
240void _initialize_amd64obsd_tdep (void);
241
242void
30b344b1 243_initialize_amd64obsd_tdep (void)
e2879ccb
MK
244{
245 /* The OpenBSD/amd64 native dependent code makes this assumption. */
90f90721 246 gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset) == AMD64_NUM_GREGS);
e2879ccb
MK
247
248 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
249 GDB_OSABI_OPENBSD_ELF, amd64obsd_init_abi);
30b344b1
MK
250
251 /* OpenBSD uses traditional (a.out) NetBSD-style core dumps. */
252 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
253 GDB_OSABI_NETBSD_AOUT, amd64obsd_init_abi);
e2879ccb 254}
This page took 0.070082 seconds and 4 git commands to generate.