1 /* Target-dependent code for FreeBSD/amd64.
3 Copyright (C) 2003-2016 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "arch-utils.h"
27 #include "i386-fbsd-tdep.h"
28 #include "x86-xstate.h"
30 #include "amd64-tdep.h"
31 #include "bsd-uthread.h"
32 #include "fbsd-tdep.h"
33 #include "solib-svr4.h"
35 /* Support for signal handlers. */
37 /* Return whether THIS_FRAME corresponds to a FreeBSD sigtramp
40 static const gdb_byte amd64fbsd_sigtramp_code[] =
42 0x48, 0x8d, 0x7c, 0x24, 0x10, /* lea SIGF_UC(%rsp),%rdi */
43 0x6a, 0x00, /* pushq $0 */
44 0x48, 0xc7, 0xc0, 0xa1, 0x01, 0x00, 0x00,
45 /* movq $SYS_sigreturn,%rax */
46 0x0f, 0x05 /* syscall */
50 amd64fbsd_sigtramp_p (struct frame_info *this_frame)
52 CORE_ADDR pc = get_frame_pc (this_frame);
53 gdb_byte buf[sizeof amd64fbsd_sigtramp_code];
55 if (!safe_frame_unwind_memory (this_frame, pc, buf, sizeof buf))
57 if (memcmp (buf, amd64fbsd_sigtramp_code, sizeof amd64fbsd_sigtramp_code)
64 /* Assuming THIS_FRAME is for a BSD sigtramp routine, return the
65 address of the associated sigcontext structure. */
68 amd64fbsd_sigcontext_addr (struct frame_info *this_frame)
70 struct gdbarch *gdbarch = get_frame_arch (this_frame);
71 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
75 /* The `struct sigcontext' (which really is an `ucontext_t' on
76 FreeBSD/amd64) lives at a fixed offset in the signal frame. See
77 <machine/sigframe.h>. */
78 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
79 sp = extract_unsigned_integer (buf, 8, byte_order);
83 /* FreeBSD 5.1-RELEASE or later. */
85 /* Mapping between the general-purpose registers in `struct reg'
86 format and GDB's register cache layout.
88 Note that some registers are 32-bit, but since we're little-endian
89 we get away with that. */
91 /* From <machine/reg.h>. */
92 static int amd64fbsd_r_reg_offset[] =
109 0 * 8, /* ... %r15 */
111 19 * 8, /* %eflags */
120 /* Location of the signal trampoline. */
121 CORE_ADDR amd64fbsd_sigtramp_start_addr;
122 CORE_ADDR amd64fbsd_sigtramp_end_addr;
124 /* From <machine/signal.h>. */
125 int amd64fbsd_sc_reg_offset[] =
127 24 + 6 * 8, /* %rax */
128 24 + 7 * 8, /* %rbx */
129 24 + 3 * 8, /* %rcx */
130 24 + 2 * 8, /* %rdx */
131 24 + 1 * 8, /* %rsi */
132 24 + 0 * 8, /* %rdi */
133 24 + 8 * 8, /* %rbp */
134 24 + 22 * 8, /* %rsp */
135 24 + 4 * 8, /* %r8 ... */
142 24 + 14 * 8, /* ... %r15 */
143 24 + 19 * 8, /* %rip */
144 24 + 21 * 8, /* %eflags */
145 24 + 20 * 8, /* %cs */
146 24 + 23 * 8, /* %ss */
153 /* From /usr/src/lib/libc/amd64/gen/_setjmp.S. */
154 static int amd64fbsd_jmp_buf_reg_offset[] =
168 4 * 8, /* %r12 ... */
171 7 * 8, /* ... %r15 */
175 /* Implement the core_read_description gdbarch method. */
177 static const struct target_desc *
178 amd64fbsd_core_read_description (struct gdbarch *gdbarch,
179 struct target_ops *target,
182 return amd64_target_description (i386fbsd_core_read_xcr0 (abfd));
185 /* Similar to amd64_supply_fpregset, but use XSAVE extended state. */
188 amd64fbsd_supply_xstateregset (const struct regset *regset,
189 struct regcache *regcache, int regnum,
190 const void *xstateregs, size_t len)
192 amd64_supply_xsave (regcache, regnum, xstateregs);
195 /* Similar to amd64_collect_fpregset, but use XSAVE extended state. */
198 amd64fbsd_collect_xstateregset (const struct regset *regset,
199 const struct regcache *regcache,
200 int regnum, void *xstateregs, size_t len)
202 amd64_collect_xsave (regcache, regnum, xstateregs, 1);
205 static const struct regset amd64fbsd_xstateregset =
208 amd64fbsd_supply_xstateregset,
209 amd64fbsd_collect_xstateregset
212 /* Iterate over core file register note sections. */
215 amd64fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
216 iterate_over_regset_sections_cb *cb,
218 const struct regcache *regcache)
220 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
222 cb (".reg", tdep->sizeof_gregset, &i386_gregset, NULL, cb_data);
223 cb (".reg2", tdep->sizeof_fpregset, &amd64_fpregset, NULL, cb_data);
224 cb (".reg-xstate", X86_XSTATE_SIZE(tdep->xcr0),
225 &amd64fbsd_xstateregset, "XSAVE extended state", cb_data);
229 amd64fbsd_supply_uthread (struct regcache *regcache,
230 int regnum, CORE_ADDR addr)
235 gdb_assert (regnum >= -1);
237 for (i = 0; i < ARRAY_SIZE (amd64fbsd_jmp_buf_reg_offset); i++)
239 if (amd64fbsd_jmp_buf_reg_offset[i] != -1
240 && (regnum == -1 || regnum == i))
242 read_memory (addr + amd64fbsd_jmp_buf_reg_offset[i], buf, 8);
243 regcache_raw_supply (regcache, i, buf);
249 amd64fbsd_collect_uthread (const struct regcache *regcache,
250 int regnum, CORE_ADDR addr)
255 gdb_assert (regnum >= -1);
257 for (i = 0; i < ARRAY_SIZE (amd64fbsd_jmp_buf_reg_offset); i++)
259 if (amd64fbsd_jmp_buf_reg_offset[i] != -1
260 && (regnum == -1 || regnum == i))
262 regcache_raw_collect (regcache, i, buf);
263 write_memory (addr + amd64fbsd_jmp_buf_reg_offset[i], buf, 8);
269 amd64fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
271 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
273 /* Generic FreeBSD support. */
274 fbsd_init_abi (info, gdbarch);
276 /* Obviously FreeBSD is BSD-based. */
277 i386bsd_init_abi (info, gdbarch);
279 tdep->gregset_reg_offset = amd64fbsd_r_reg_offset;
280 tdep->gregset_num_regs = ARRAY_SIZE (amd64fbsd_r_reg_offset);
281 tdep->sizeof_gregset = 22 * 8;
283 amd64_init_abi (info, gdbarch);
285 tdep->sigtramp_p = amd64fbsd_sigtramp_p;
286 tdep->sigtramp_start = amd64fbsd_sigtramp_start_addr;
287 tdep->sigtramp_end = amd64fbsd_sigtramp_end_addr;
288 tdep->sigcontext_addr = amd64fbsd_sigcontext_addr;
289 tdep->sc_reg_offset = amd64fbsd_sc_reg_offset;
290 tdep->sc_num_regs = ARRAY_SIZE (amd64fbsd_sc_reg_offset);
292 tdep->xsave_xcr0_offset = I386_FBSD_XSAVE_XCR0_OFFSET;
294 /* Iterate over core file register note sections. */
295 set_gdbarch_iterate_over_regset_sections
296 (gdbarch, amd64fbsd_iterate_over_regset_sections);
298 set_gdbarch_core_read_description (gdbarch,
299 amd64fbsd_core_read_description);
301 /* FreeBSD provides a user-level threads implementation. */
302 bsd_uthread_set_supply_uthread (gdbarch, amd64fbsd_supply_uthread);
303 bsd_uthread_set_collect_uthread (gdbarch, amd64fbsd_collect_uthread);
305 /* FreeBSD uses SVR4-style shared libraries. */
306 set_solib_svr4_fetch_link_map_offsets
307 (gdbarch, svr4_lp64_fetch_link_map_offsets);
311 /* Provide a prototype to silence -Wmissing-prototypes. */
312 void _initialize_amd64fbsd_tdep (void);
315 _initialize_amd64fbsd_tdep (void)
317 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
318 GDB_OSABI_FREEBSD_ELF, amd64fbsd_init_abi);