]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/amd64-fbsd-nat.c
x86: Move x86-specific linker options to elf_linker_x86_params
[thirdparty/binutils-gdb.git] / gdb / amd64-fbsd-nat.c
CommitLineData
68cc0bfb 1/* Native-dependent code for FreeBSD/amd64.
2a6d284d 2
42a4f53d 3 Copyright (C) 2003-2019 Free Software Foundation, Inc.
68cc0bfb
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
68cc0bfb
MK
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
68cc0bfb
MK
19
20#include "defs.h"
68cc0bfb 21
d55e5aa6
TT
22/* Standard C includes. */
23#include <machine/reg.h>
68cc0bfb 24#include <signal.h>
68cc0bfb
MK
25#include <sys/ptrace.h>
26#include <sys/sysctl.h>
d55e5aa6 27#include <sys/types.h>
cf424aef 28#include <sys/user.h>
68cc0bfb 29
d55e5aa6 30/* Local non-gdb includes. */
f6ac5f3d 31#include "amd64-bsd-nat.h"
d55e5aa6
TT
32#include "amd64-nat.h"
33#include "amd64-tdep.h"
0747795c 34#include "common/x86-xstate.h"
d55e5aa6
TT
35#include "fbsd-nat.h"
36#include "inferior.h"
37#include "regcache.h"
38#include "target.h"
39#include "x86-nat.h"
40
68cc0bfb
MK
41\f
42
f6ac5f3d
PA
43class amd64_fbsd_nat_target final
44 : public amd64_bsd_nat_target<fbsd_nat_target>
45{
46public:
47 /* Add some extra features to the common *BSD/amd64 target. */
48 const struct target_desc *read_description () override;
49
50#if defined(HAVE_PT_GETDBREGS) && defined(USE_SIGTRAP_SIGINFO)
57810aa7 51 bool supports_stopped_by_hw_breakpoint () override;
f6ac5f3d
PA
52#endif
53};
54
55static amd64_fbsd_nat_target the_amd64_fbsd_nat_target;
56
cb461069
MK
57/* Offset in `struct reg' where MEMBER is stored. */
58#define REG_OFFSET(member) offsetof (struct reg, member)
68cc0bfb 59
cb461069
MK
60/* At amd64fbsd64_r_reg_offset[REGNUM] you'll find the offset in
61 `struct reg' location where the GDB register REGNUM is stored.
62 Unsupported registers are marked with `-1'. */
63static int amd64fbsd64_r_reg_offset[] =
68cc0bfb
MK
64{
65 REG_OFFSET (r_rax),
66 REG_OFFSET (r_rbx),
67 REG_OFFSET (r_rcx),
68 REG_OFFSET (r_rdx),
69 REG_OFFSET (r_rsi),
70 REG_OFFSET (r_rdi),
71 REG_OFFSET (r_rbp),
72 REG_OFFSET (r_rsp),
73 REG_OFFSET (r_r8),
74 REG_OFFSET (r_r9),
75 REG_OFFSET (r_r10),
76 REG_OFFSET (r_r11),
77 REG_OFFSET (r_r12),
78 REG_OFFSET (r_r13),
79 REG_OFFSET (r_r14),
80 REG_OFFSET (r_r15),
81 REG_OFFSET (r_rip),
82 REG_OFFSET (r_rflags),
af233647
MK
83 REG_OFFSET (r_cs),
84 REG_OFFSET (r_ss),
68cc0bfb
MK
85 -1,
86 -1,
87 -1,
88 -1
89};
2a6d284d
MK
90\f
91
92/* Mapping between the general-purpose registers in FreeBSD/amd64
93 `struct reg' format and GDB's register cache layout for
94 FreeBSD/i386.
68cc0bfb 95
2a6d284d
MK
96 Note that most FreeBSD/amd64 registers are 64-bit, while the
97 FreeBSD/i386 registers are all 32-bit, but since we're
98 little-endian we get away with that. */
68cc0bfb 99
2a6d284d
MK
100/* From <machine/reg.h>. */
101static int amd64fbsd32_r_reg_offset[I386_NUM_GREGS] =
102{
103 14 * 8, 13 * 8, /* %eax, %ecx */
104 12 * 8, 11 * 8, /* %edx, %ebx */
105 20 * 8, 10 * 8, /* %esp, %ebp */
106 9 * 8, 8 * 8, /* %esi, %edi */
107 17 * 8, 19 * 8, /* %eip, %eflags */
108 18 * 8, 21 * 8, /* %cs, %ss */
109 -1, -1, -1, -1 /* %ds, %es, %fs, %gs */
110};
68cc0bfb
MK
111\f
112
315c4276
MK
113/* Support for debugging kernel virtual memory images. */
114
315c4276 115#include <machine/pcb.h>
1f596238 116#include <osreldate.h>
315c4276
MK
117
118#include "bsd-kvm.h"
119
120static int
121amd64fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
122{
123 /* The following is true for FreeBSD 5.2:
124
125 The pcb contains %rip, %rbx, %rsp, %rbp, %r12, %r13, %r14, %r15,
126 %ds, %es, %fs and %gs. This accounts for all callee-saved
127 registers specified by the psABI and then some. Here %esp
128 contains the stack pointer at the point just after the call to
129 cpu_switch(). From this information we reconstruct the register
130 state as it would like when we just returned from cpu_switch(). */
131
132 /* The stack pointer shouldn't be zero. */
133 if (pcb->pcb_rsp == 0)
134 return 0;
135
136 pcb->pcb_rsp += 8;
73e1c03f
SM
137 regcache->raw_supply (AMD64_RIP_REGNUM, &pcb->pcb_rip);
138 regcache->raw_supply (AMD64_RBX_REGNUM, &pcb->pcb_rbx);
139 regcache->raw_supply (AMD64_RSP_REGNUM, &pcb->pcb_rsp);
140 regcache->raw_supply (AMD64_RBP_REGNUM, &pcb->pcb_rbp);
141 regcache->raw_supply (12, &pcb->pcb_r12);
142 regcache->raw_supply (13, &pcb->pcb_r13);
143 regcache->raw_supply (14, &pcb->pcb_r14);
144 regcache->raw_supply (15, &pcb->pcb_r15);
c1dec97b 145#if (__FreeBSD_version < 800075) && (__FreeBSD_kernel_version < 800075)
ffeecffa
JB
146 /* struct pcb provides the pcb_ds/pcb_es/pcb_fs/pcb_gs fields only
147 up until __FreeBSD_version 800074: The removal of these fields
148 occurred on 2009-04-01 while the __FreeBSD_version number was
149 bumped to 800075 on 2009-04-06. So 800075 is the closest version
150 number where we should not try to access these fields. */
73e1c03f
SM
151 regcache->raw_supply (AMD64_DS_REGNUM, &pcb->pcb_ds);
152 regcache->raw_supply (AMD64_ES_REGNUM, &pcb->pcb_es);
153 regcache->raw_supply (AMD64_FS_REGNUM, &pcb->pcb_fs);
154 regcache->raw_supply (AMD64_GS_REGNUM, &pcb->pcb_gs);
1f596238 155#endif
315c4276
MK
156
157 return 1;
158}
159\f
160
f6ac5f3d 161/* Implement the read_description method. */
97de3545 162
f6ac5f3d
PA
163const struct target_desc *
164amd64_fbsd_nat_target::read_description ()
97de3545
JB
165{
166#ifdef PT_GETXSTATE_INFO
167 static int xsave_probed;
168 static uint64_t xcr0;
169#endif
170 struct reg regs;
171 int is64;
172
e99b03dc 173 if (ptrace (PT_GETREGS, inferior_ptid.pid (),
97de3545
JB
174 (PTRACE_TYPE_ARG3) &regs, 0) == -1)
175 perror_with_name (_("Couldn't get registers"));
176 is64 = (regs.r_cs == GSEL (GUCODE_SEL, SEL_UPL));
177#ifdef PT_GETXSTATE_INFO
178 if (!xsave_probed)
179 {
180 struct ptrace_xstate_info info;
181
e99b03dc 182 if (ptrace (PT_GETXSTATE_INFO, inferior_ptid.pid (),
97de3545
JB
183 (PTRACE_TYPE_ARG3) &info, sizeof (info)) == 0)
184 {
a3405d12 185 x86bsd_xsave_len = info.xsave_len;
97de3545
JB
186 xcr0 = info.xsave_mask;
187 }
188 xsave_probed = 1;
189 }
190
a3405d12 191 if (x86bsd_xsave_len != 0)
97de3545
JB
192 {
193 if (is64)
41206e32 194 return amd64_target_description (xcr0, true);
97de3545 195 else
dd6876c9 196 return i386_target_description (xcr0, true);
97de3545
JB
197 }
198#endif
199 if (is64)
41206e32 200 return amd64_target_description (X86_XSTATE_SSE_MASK, true);
97de3545 201 else
dd6876c9 202 return i386_target_description (X86_XSTATE_SSE_MASK, true);
97de3545
JB
203}
204
f6ac5f3d
PA
205#if defined(HAVE_PT_GETDBREGS) && defined(USE_SIGTRAP_SIGINFO)
206/* Implement the supports_stopped_by_hw_breakpoints method. */
207
57810aa7 208bool
f6ac5f3d
PA
209amd64_fbsd_nat_target::supports_stopped_by_hw_breakpoint ()
210{
57810aa7 211 return true;
f6ac5f3d
PA
212}
213#endif
214
68cc0bfb 215void
1c671366 216_initialize_amd64fbsd_nat (void)
68cc0bfb
MK
217{
218 int offset;
219
2a6d284d 220 amd64_native_gregset32_reg_offset = amd64fbsd32_r_reg_offset;
cb461069 221 amd64_native_gregset64_reg_offset = amd64fbsd64_r_reg_offset;
2a6d284d 222
d9f719f1 223 add_inf_child_target (&the_amd64_fbsd_nat_target);
6a5c78a3
MK
224
225 /* Support debugging kernel virtual memory images. */
226 bsd_kvm_add_target (amd64fbsd_supply_pcb);
227
03b62bbb 228 /* To support the recognition of signal handlers, i386-bsd-tdep.c
68cc0bfb
MK
229 hardcodes some constants. Inclusion of this file means that we
230 are compiling a native debugger, which means that we can use the
231 system header files and sysctl(3) to get at the relevant
232 information. */
233
68cc0bfb
MK
234#define SC_REG_OFFSET amd64fbsd_sc_reg_offset
235
236 /* We only check the program counter, stack pointer and frame
237 pointer since these members of `struct sigcontext' are essential
238 for providing backtraces. */
239
90f90721
MK
240#define SC_RIP_OFFSET SC_REG_OFFSET[AMD64_RIP_REGNUM]
241#define SC_RSP_OFFSET SC_REG_OFFSET[AMD64_RSP_REGNUM]
242#define SC_RBP_OFFSET SC_REG_OFFSET[AMD64_RBP_REGNUM]
68cc0bfb
MK
243
244 /* Override the default value for the offset of the program counter
245 in the sigcontext structure. */
246 offset = offsetof (struct sigcontext, sc_rip);
247
248 if (SC_RIP_OFFSET != offset)
249 {
edefbb7c 250 warning (_("\
68cc0bfb 251offsetof (struct sigcontext, sc_rip) yields %d instead of %d.\n\
edefbb7c 252Please report this to <bug-gdb@gnu.org>."),
68cc0bfb
MK
253 offset, SC_RIP_OFFSET);
254 }
255
256 SC_RIP_OFFSET = offset;
257
258 /* Likewise for the stack pointer. */
259 offset = offsetof (struct sigcontext, sc_rsp);
260
261 if (SC_RSP_OFFSET != offset)
262 {
edefbb7c 263 warning (_("\
68cc0bfb 264offsetof (struct sigcontext, sc_rsp) yields %d instead of %d.\n\
edefbb7c 265Please report this to <bug-gdb@gnu.org>."),
68cc0bfb
MK
266 offset, SC_RSP_OFFSET);
267 }
268
269 SC_RSP_OFFSET = offset;
270
271 /* And the frame pointer. */
272 offset = offsetof (struct sigcontext, sc_rbp);
273
274 if (SC_RBP_OFFSET != offset)
275 {
edefbb7c 276 warning (_("\
68cc0bfb 277offsetof (struct sigcontext, sc_rbp) yields %d instead of %d.\n\
edefbb7c 278Please report this to <bug-gdb@gnu.org>."),
68cc0bfb
MK
279 offset, SC_RBP_OFFSET);
280 }
281
282 SC_RBP_OFFSET = offset;
283
cf424aef
JB
284#ifdef KERN_PROC_SIGTRAMP
285 /* Normally signal frames are detected via amd64fbsd_sigtramp_p.
286 However, FreeBSD 9.2 through 10.1 do not include the page holding
287 the signal code in core dumps. These releases do provide a
288 kern.proc.sigtramp.<pid> sysctl that returns the location of the
289 signal trampoline for a running process. We fetch the location
290 of the current (gdb) process and use this to identify signal
291 frames in core dumps from these releases. Note that this only
292 works for core dumps of 64-bit (FreeBSD/amd64) processes and does
293 not handle core dumps of 32-bit (FreeBSD/i386) processes. */
68cc0bfb 294 {
cf424aef
JB
295 int mib[4];
296 struct kinfo_sigtramp kst;
68cc0bfb
MK
297 size_t len;
298
68cc0bfb 299 mib[0] = CTL_KERN;
cf424aef
JB
300 mib[1] = KERN_PROC;
301 mib[2] = KERN_PROC_SIGTRAMP;
302 mib[3] = getpid ();
303 len = sizeof (kst);
304 if (sysctl (mib, 4, &kst, &len, NULL, 0) == 0)
68cc0bfb 305 {
cf424aef
JB
306 amd64fbsd_sigtramp_start_addr = (uintptr_t) kst.ksigtramp_start;
307 amd64fbsd_sigtramp_end_addr = (uintptr_t) kst.ksigtramp_end;
68cc0bfb
MK
308 }
309 }
cf424aef 310#endif
68cc0bfb 311}