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