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