gdb: Remove support for obsolete OSABIs and a.out
[deliverable/binutils-gdb.git] / gdb / amd64-obsd-tdep.c
1 /* Target-dependent code for OpenBSD/amd64.
2
3 Copyright (C) 2003-2016 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 3 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, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "frame-unwind.h"
23 #include "gdbcore.h"
24 #include "symtab.h"
25 #include "objfiles.h"
26 #include "osabi.h"
27 #include "regcache.h"
28 #include "regset.h"
29 #include "target.h"
30 #include "trad-frame.h"
31
32 #include "obsd-tdep.h"
33 #include "amd64-tdep.h"
34 #include "i387-tdep.h"
35 #include "solib-svr4.h"
36 #include "bsd-uthread.h"
37
38 /* Support for signal handlers. */
39
40 /* Default page size. */
41 static const int amd64obsd_page_size = 4096;
42
43 /* Return whether THIS_FRAME corresponds to an OpenBSD sigtramp
44 routine. */
45
46 static int
47 amd64obsd_sigtramp_p (struct frame_info *this_frame)
48 {
49 CORE_ADDR pc = get_frame_pc (this_frame);
50 CORE_ADDR start_pc = (pc & ~(amd64obsd_page_size - 1));
51 const gdb_byte osigreturn[] =
52 {
53 0x48, 0xc7, 0xc0,
54 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
55 0xcd, 0x80 /* int $0x80 */
56 };
57 const gdb_byte sigreturn[] =
58 {
59 0x48, 0xc7, 0xc0,
60 0x67, 0x00, 0x00, 0x00, /* movq $SYS_sigreturn, %rax */
61 0x0f, 0x05 /* syscall */
62 };
63 size_t buflen = (sizeof sigreturn) + 1;
64 gdb_byte *buf;
65 const char *name;
66
67 /* If the function has a valid symbol name, it isn't a
68 trampoline. */
69 find_pc_partial_function (pc, &name, NULL, NULL);
70 if (name != NULL)
71 return 0;
72
73 /* If the function lives in a valid section (even without a starting
74 point) it isn't a trampoline. */
75 if (find_pc_section (pc) != NULL)
76 return 0;
77
78 /* If we can't read the instructions at START_PC, return zero. */
79 buf = (gdb_byte *) alloca ((sizeof sigreturn) + 1);
80 if (!safe_frame_unwind_memory (this_frame, start_pc + 6, buf, buflen))
81 return 0;
82
83 /* Check for sigreturn(2). Depending on how the assembler encoded
84 the `movq %rsp, %rdi' instruction, the code starts at offset 6 or
85 7. OpenBSD 5.0 and later use the `syscall' instruction. Older
86 versions use `int $0x80'. Check for both. */
87 if (memcmp (buf, sigreturn, sizeof sigreturn)
88 && memcmp (buf + 1, sigreturn, sizeof sigreturn)
89 && memcmp (buf, osigreturn, sizeof osigreturn)
90 && memcmp (buf + 1, osigreturn, sizeof osigreturn))
91 return 0;
92
93 return 1;
94 }
95
96 /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
97 address of the associated sigcontext structure. */
98
99 static CORE_ADDR
100 amd64obsd_sigcontext_addr (struct frame_info *this_frame)
101 {
102 CORE_ADDR pc = get_frame_pc (this_frame);
103 ULONGEST offset = (pc & (amd64obsd_page_size - 1));
104
105 /* The %rsp register points at `struct sigcontext' upon entry of a
106 signal trampoline. The relevant part of the trampoline is
107
108 call *%rax
109 movq %rsp, %rdi
110 pushq %rdi
111 movq $SYS_sigreturn,%rax
112 int $0x80
113
114 (see /usr/src/sys/arch/amd64/amd64/locore.S). The `pushq'
115 instruction clobbers %rsp, but its value is saved in `%rdi'. */
116
117 if (offset > 5)
118 return get_frame_register_unsigned (this_frame, AMD64_RDI_REGNUM);
119 else
120 return get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
121 }
122 \f
123 /* OpenBSD 3.5 or later. */
124
125 /* Mapping between the general-purpose registers in `struct reg'
126 format and GDB's register cache layout. */
127
128 /* From <machine/reg.h>. */
129 int amd64obsd_r_reg_offset[] =
130 {
131 14 * 8, /* %rax */
132 13 * 8, /* %rbx */
133 3 * 8, /* %rcx */
134 2 * 8, /* %rdx */
135 1 * 8, /* %rsi */
136 0 * 8, /* %rdi */
137 12 * 8, /* %rbp */
138 15 * 8, /* %rsp */
139 4 * 8, /* %r8 .. */
140 5 * 8,
141 6 * 8,
142 7 * 8,
143 8 * 8,
144 9 * 8,
145 10 * 8,
146 11 * 8, /* ... %r15 */
147 16 * 8, /* %rip */
148 17 * 8, /* %eflags */
149 18 * 8, /* %cs */
150 19 * 8, /* %ss */
151 20 * 8, /* %ds */
152 21 * 8, /* %es */
153 22 * 8, /* %fs */
154 23 * 8 /* %gs */
155 };
156
157 /* From <machine/signal.h>. */
158 static int amd64obsd_sc_reg_offset[] =
159 {
160 14 * 8, /* %rax */
161 13 * 8, /* %rbx */
162 3 * 8, /* %rcx */
163 2 * 8, /* %rdx */
164 1 * 8, /* %rsi */
165 0 * 8, /* %rdi */
166 12 * 8, /* %rbp */
167 24 * 8, /* %rsp */
168 4 * 8, /* %r8 ... */
169 5 * 8,
170 6 * 8,
171 7 * 8,
172 8 * 8,
173 9 * 8,
174 10 * 8,
175 11 * 8, /* ... %r15 */
176 21 * 8, /* %rip */
177 23 * 8, /* %eflags */
178 22 * 8, /* %cs */
179 25 * 8, /* %ss */
180 18 * 8, /* %ds */
181 17 * 8, /* %es */
182 16 * 8, /* %fs */
183 15 * 8 /* %gs */
184 };
185
186 /* From /usr/src/lib/libpthread/arch/amd64/uthread_machdep.c. */
187 static int amd64obsd_uthread_reg_offset[] =
188 {
189 19 * 8, /* %rax */
190 16 * 8, /* %rbx */
191 18 * 8, /* %rcx */
192 17 * 8, /* %rdx */
193 14 * 8, /* %rsi */
194 13 * 8, /* %rdi */
195 15 * 8, /* %rbp */
196 -1, /* %rsp */
197 12 * 8, /* %r8 ... */
198 11 * 8,
199 10 * 8,
200 9 * 8,
201 8 * 8,
202 7 * 8,
203 6 * 8,
204 5 * 8, /* ... %r15 */
205 20 * 8, /* %rip */
206 4 * 8, /* %eflags */
207 21 * 8, /* %cs */
208 -1, /* %ss */
209 3 * 8, /* %ds */
210 2 * 8, /* %es */
211 1 * 8, /* %fs */
212 0 * 8 /* %gs */
213 };
214
215 /* Offset within the thread structure where we can find the saved
216 stack pointer (%esp). */
217 #define AMD64OBSD_UTHREAD_RSP_OFFSET 400
218
219 static void
220 amd64obsd_supply_uthread (struct regcache *regcache,
221 int regnum, CORE_ADDR addr)
222 {
223 struct gdbarch *gdbarch = get_regcache_arch (regcache);
224 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
225 CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
226 CORE_ADDR sp = 0;
227 gdb_byte buf[8];
228 int i;
229
230 gdb_assert (regnum >= -1);
231
232 if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
233 {
234 int offset;
235
236 /* Fetch stack pointer from thread structure. */
237 sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);
238
239 /* Adjust the stack pointer such that it looks as if we just
240 returned from _thread_machdep_switch. */
241 offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
242 store_unsigned_integer (buf, 8, byte_order, sp + offset);
243 regcache_raw_supply (regcache, AMD64_RSP_REGNUM, buf);
244 }
245
246 for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
247 {
248 if (amd64obsd_uthread_reg_offset[i] != -1
249 && (regnum == -1 || regnum == i))
250 {
251 /* Fetch stack pointer from thread structure (if we didn't
252 do so already). */
253 if (sp == 0)
254 sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);
255
256 /* Read the saved register from the stack frame. */
257 read_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
258 regcache_raw_supply (regcache, i, buf);
259 }
260 }
261 }
262
263 static void
264 amd64obsd_collect_uthread (const struct regcache *regcache,
265 int regnum, CORE_ADDR addr)
266 {
267 struct gdbarch *gdbarch = get_regcache_arch (regcache);
268 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
269 CORE_ADDR sp_addr = addr + AMD64OBSD_UTHREAD_RSP_OFFSET;
270 CORE_ADDR sp = 0;
271 gdb_byte buf[8];
272 int i;
273
274 gdb_assert (regnum >= -1);
275
276 if (regnum == -1 || regnum == AMD64_RSP_REGNUM)
277 {
278 int offset;
279
280 /* Calculate the stack pointer (frame pointer) that will be
281 stored into the thread structure. */
282 offset = amd64obsd_uthread_reg_offset[AMD64_RIP_REGNUM] + 8;
283 regcache_raw_collect (regcache, AMD64_RSP_REGNUM, buf);
284 sp = extract_unsigned_integer (buf, 8, byte_order) - offset;
285
286 /* Store the stack pointer. */
287 write_memory_unsigned_integer (sp_addr, 8, byte_order, sp);
288
289 /* The stack pointer was (potentially) modified. Make sure we
290 build a proper stack frame. */
291 regnum = -1;
292 }
293
294 for (i = 0; i < ARRAY_SIZE (amd64obsd_uthread_reg_offset); i++)
295 {
296 if (amd64obsd_uthread_reg_offset[i] != -1
297 && (regnum == -1 || regnum == i))
298 {
299 /* Fetch stack pointer from thread structure (if we didn't
300 calculate it already). */
301 if (sp == 0)
302 sp = read_memory_unsigned_integer (sp_addr, 8, byte_order);
303
304 /* Write the register into the stack frame. */
305 regcache_raw_collect (regcache, i, buf);
306 write_memory (sp + amd64obsd_uthread_reg_offset[i], buf, 8);
307 }
308 }
309 }
310 /* Kernel debugging support. */
311
312 /* From <machine/frame.h>. Easy since `struct trapframe' matches
313 `struct sigcontext'. */
314 #define amd64obsd_tf_reg_offset amd64obsd_sc_reg_offset
315
316 static struct trad_frame_cache *
317 amd64obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
318 {
319 struct gdbarch *gdbarch = get_frame_arch (this_frame);
320 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
321 struct trad_frame_cache *cache;
322 CORE_ADDR func, sp, addr;
323 ULONGEST cs;
324 const char *name;
325 int i;
326
327 if (*this_cache)
328 return (struct trad_frame_cache *) *this_cache;
329
330 cache = trad_frame_cache_zalloc (this_frame);
331 *this_cache = cache;
332
333 func = get_frame_func (this_frame);
334 sp = get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
335
336 find_pc_partial_function (func, &name, NULL, NULL);
337 if (name && startswith (name, "Xintr"))
338 addr = sp + 8; /* It's an interrupt frame. */
339 else
340 addr = sp;
341
342 for (i = 0; i < ARRAY_SIZE (amd64obsd_tf_reg_offset); i++)
343 if (amd64obsd_tf_reg_offset[i] != -1)
344 trad_frame_set_reg_addr (cache, i, addr + amd64obsd_tf_reg_offset[i]);
345
346 /* Read %cs from trap frame. */
347 addr += amd64obsd_tf_reg_offset[AMD64_CS_REGNUM];
348 cs = read_memory_unsigned_integer (addr, 8, byte_order);
349 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
350 {
351 /* Trap from user space; terminate backtrace. */
352 trad_frame_set_id (cache, outer_frame_id);
353 }
354 else
355 {
356 /* Construct the frame ID using the function start. */
357 trad_frame_set_id (cache, frame_id_build (sp + 16, func));
358 }
359
360 return cache;
361 }
362
363 static void
364 amd64obsd_trapframe_this_id (struct frame_info *this_frame,
365 void **this_cache, struct frame_id *this_id)
366 {
367 struct trad_frame_cache *cache =
368 amd64obsd_trapframe_cache (this_frame, this_cache);
369
370 trad_frame_get_id (cache, this_id);
371 }
372
373 static struct value *
374 amd64obsd_trapframe_prev_register (struct frame_info *this_frame,
375 void **this_cache, int regnum)
376 {
377 struct trad_frame_cache *cache =
378 amd64obsd_trapframe_cache (this_frame, this_cache);
379
380 return trad_frame_get_register (cache, this_frame, regnum);
381 }
382
383 static int
384 amd64obsd_trapframe_sniffer (const struct frame_unwind *self,
385 struct frame_info *this_frame,
386 void **this_prologue_cache)
387 {
388 ULONGEST cs;
389 const char *name;
390
391 /* Check Current Privilege Level and bail out if we're not executing
392 in kernel space. */
393 cs = get_frame_register_unsigned (this_frame, AMD64_CS_REGNUM);
394 if ((cs & I386_SEL_RPL) == I386_SEL_UPL)
395 return 0;
396
397 find_pc_partial_function (get_frame_pc (this_frame), &name, NULL, NULL);
398 return (name && ((strcmp (name, "calltrap") == 0)
399 || (strcmp (name, "osyscall1") == 0)
400 || (strcmp (name, "Xsyscall") == 0)
401 || (startswith (name, "Xintr"))));
402 }
403
404 static const struct frame_unwind amd64obsd_trapframe_unwind = {
405 /* FIXME: kettenis/20051219: This really is more like an interrupt
406 frame, but SIGTRAMP_FRAME would print <signal handler called>,
407 which really is not what we want here. */
408 NORMAL_FRAME,
409 default_frame_unwind_stop_reason,
410 amd64obsd_trapframe_this_id,
411 amd64obsd_trapframe_prev_register,
412 NULL,
413 amd64obsd_trapframe_sniffer
414 };
415 \f
416
417 static void
418 amd64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
419 {
420 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
421
422 amd64_init_abi (info, gdbarch);
423 obsd_init_abi (info, gdbarch);
424
425 /* Initialize general-purpose register set details. */
426 tdep->gregset_reg_offset = amd64obsd_r_reg_offset;
427 tdep->gregset_num_regs = ARRAY_SIZE (amd64obsd_r_reg_offset);
428 tdep->sizeof_gregset = 24 * 8;
429
430 tdep->jb_pc_offset = 7 * 8;
431
432 tdep->sigtramp_p = amd64obsd_sigtramp_p;
433 tdep->sigcontext_addr = amd64obsd_sigcontext_addr;
434 tdep->sc_reg_offset = amd64obsd_sc_reg_offset;
435 tdep->sc_num_regs = ARRAY_SIZE (amd64obsd_sc_reg_offset);
436
437 /* OpenBSD provides a user-level threads implementation. */
438 bsd_uthread_set_supply_uthread (gdbarch, amd64obsd_supply_uthread);
439 bsd_uthread_set_collect_uthread (gdbarch, amd64obsd_collect_uthread);
440
441 /* OpenBSD uses SVR4-style shared libraries. */
442 set_solib_svr4_fetch_link_map_offsets
443 (gdbarch, svr4_lp64_fetch_link_map_offsets);
444
445 /* Unwind kernel trap frames correctly. */
446 frame_unwind_prepend_unwinder (gdbarch, &amd64obsd_trapframe_unwind);
447 }
448 \f
449
450 /* Provide a prototype to silence -Wmissing-prototypes. */
451 void _initialize_amd64obsd_tdep (void);
452
453 void
454 _initialize_amd64obsd_tdep (void)
455 {
456 /* The OpenBSD/amd64 native dependent code makes this assumption. */
457 gdb_assert (ARRAY_SIZE (amd64obsd_r_reg_offset) == AMD64_NUM_GREGS);
458
459 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
460 GDB_OSABI_OPENBSD, amd64obsd_init_abi);
461 }
This page took 0.039535 seconds and 5 git commands to generate.