Remove unused variables in fbsd-tdep.c files
[binutils-gdb.git] / gdb / amd64-fbsd-tdep.c
1 /* Target-dependent code for FreeBSD/amd64.
2
3 Copyright (C) 2003-2022 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 "osabi.h"
22 #include "regset.h"
23 #include "target.h"
24 #include "trad-frame.h"
25 #include "tramp-frame.h"
26 #include "i386-fbsd-tdep.h"
27 #include "gdbsupport/x86-xstate.h"
28
29 #include "amd64-tdep.h"
30 #include "amd64-fbsd-tdep.h"
31 #include "fbsd-tdep.h"
32 #include "solib-svr4.h"
33 #include "inferior.h"
34
35 /* The general-purpose regset consists of 22 64-bit slots, most of
36 which contain individual registers, but a few contain multiple
37 16-bit segment registers. */
38 #define AMD64_FBSD_SIZEOF_GREGSET (22 * 8)
39
40 /* Register maps. */
41
42 static const struct regcache_map_entry amd64_fbsd_gregmap[] =
43 {
44 { 1, AMD64_R15_REGNUM, 0 },
45 { 1, AMD64_R14_REGNUM, 0 },
46 { 1, AMD64_R13_REGNUM, 0 },
47 { 1, AMD64_R12_REGNUM, 0 },
48 { 1, AMD64_R11_REGNUM, 0 },
49 { 1, AMD64_R10_REGNUM, 0 },
50 { 1, AMD64_R9_REGNUM, 0 },
51 { 1, AMD64_R8_REGNUM, 0 },
52 { 1, AMD64_RDI_REGNUM, 0 },
53 { 1, AMD64_RSI_REGNUM, 0 },
54 { 1, AMD64_RBP_REGNUM, 0 },
55 { 1, AMD64_RBX_REGNUM, 0 },
56 { 1, AMD64_RDX_REGNUM, 0 },
57 { 1, AMD64_RCX_REGNUM, 0 },
58 { 1, AMD64_RAX_REGNUM, 0 },
59 { 1, REGCACHE_MAP_SKIP, 4 }, /* trapno */
60 { 1, AMD64_FS_REGNUM, 2 },
61 { 1, AMD64_GS_REGNUM, 2 },
62 { 1, REGCACHE_MAP_SKIP, 4 }, /* err */
63 { 1, AMD64_ES_REGNUM, 2 },
64 { 1, AMD64_DS_REGNUM, 2 },
65 { 1, AMD64_RIP_REGNUM, 0 },
66 { 1, AMD64_CS_REGNUM, 8 },
67 { 1, AMD64_EFLAGS_REGNUM, 8 },
68 { 1, AMD64_RSP_REGNUM, 0 },
69 { 1, AMD64_SS_REGNUM, 8 },
70 { 0 }
71 };
72
73 /* This layout including fsbase and gsbase was adopted in FreeBSD
74 8.0. */
75
76 static const struct regcache_map_entry amd64_fbsd_mcregmap[] =
77 {
78 { 1, REGCACHE_MAP_SKIP, 8 }, /* mc_onstack */
79 { 1, AMD64_RDI_REGNUM, 0 },
80 { 1, AMD64_RSI_REGNUM, 0 },
81 { 1, AMD64_RDX_REGNUM, 0 },
82 { 1, AMD64_RCX_REGNUM, 0 },
83 { 1, AMD64_R8_REGNUM, 0 },
84 { 1, AMD64_R9_REGNUM, 0 },
85 { 1, AMD64_RAX_REGNUM, 0 },
86 { 1, AMD64_RBX_REGNUM, 0 },
87 { 1, AMD64_RBP_REGNUM, 0 },
88 { 1, AMD64_R10_REGNUM, 0 },
89 { 1, AMD64_R11_REGNUM, 0 },
90 { 1, AMD64_R12_REGNUM, 0 },
91 { 1, AMD64_R13_REGNUM, 0 },
92 { 1, AMD64_R14_REGNUM, 0 },
93 { 1, AMD64_R15_REGNUM, 0 },
94 { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_trapno */
95 { 1, AMD64_FS_REGNUM, 2 },
96 { 1, AMD64_GS_REGNUM, 2 },
97 { 1, REGCACHE_MAP_SKIP, 8 }, /* mc_addr */
98 { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_flags */
99 { 1, AMD64_ES_REGNUM, 2 },
100 { 1, AMD64_DS_REGNUM, 2 },
101 { 1, REGCACHE_MAP_SKIP, 8 }, /* mc_err */
102 { 1, AMD64_RIP_REGNUM, 0 },
103 { 1, AMD64_CS_REGNUM, 8 },
104 { 1, AMD64_EFLAGS_REGNUM, 8 },
105 { 1, AMD64_RSP_REGNUM, 0 },
106 { 1, AMD64_SS_REGNUM, 8 },
107 { 1, REGCACHE_MAP_SKIP, 8 }, /* mc_len */
108 { 1, REGCACHE_MAP_SKIP, 8 }, /* mc_fpformat */
109 { 1, REGCACHE_MAP_SKIP, 8 }, /* mc_ownedfp */
110 { 64, REGCACHE_MAP_SKIP, 8 }, /* mc_fpstate */
111 { 1, AMD64_FSBASE_REGNUM, 0 },
112 { 1, AMD64_GSBASE_REGNUM, 0 },
113 { 0 }
114 };
115
116 /* Register set definitions. */
117
118 const struct regset amd64_fbsd_gregset =
119 {
120 amd64_fbsd_gregmap, regcache_supply_regset, regcache_collect_regset
121 };
122
123 /* Support for signal handlers. */
124
125 /* In a signal frame, rsp points to a 'struct sigframe' which is
126 defined as:
127
128 struct sigframe {
129 union {
130 __siginfohandler_t *sf_action;
131 __sighandler_t *sf_handler;
132 } sf_ahu;
133 ucontext_t sf_uc;
134 ...
135 }
136
137 ucontext_t is defined as:
138
139 struct __ucontext {
140 sigset_t uc_sigmask;
141 mcontext_t uc_mcontext;
142 ...
143 };
144
145 The mcontext_t contains the general purpose register set as well
146 as the floating point or XSAVE state. */
147
148 /* NB: There is an 8 byte padding hole between sf_ahu and sf_uc. */
149 #define AMD64_SIGFRAME_UCONTEXT_OFFSET 16
150 #define AMD64_UCONTEXT_MCONTEXT_OFFSET 16
151 #define AMD64_SIZEOF_MCONTEXT_T 800
152
153 /* Implement the "init" method of struct tramp_frame. */
154
155 static void
156 amd64_fbsd_sigframe_init (const struct tramp_frame *self,
157 struct frame_info *this_frame,
158 struct trad_frame_cache *this_cache,
159 CORE_ADDR func)
160 {
161 CORE_ADDR sp = get_frame_register_unsigned (this_frame, AMD64_RSP_REGNUM);
162 CORE_ADDR mcontext_addr
163 = (sp
164 + AMD64_SIGFRAME_UCONTEXT_OFFSET
165 + AMD64_UCONTEXT_MCONTEXT_OFFSET);
166
167 trad_frame_set_reg_regmap (this_cache, amd64_fbsd_mcregmap, mcontext_addr,
168 AMD64_SIZEOF_MCONTEXT_T);
169
170 /* Don't bother with floating point or XSAVE state for now. The
171 current helper routines for parsing FXSAVE and XSAVE state only
172 work with regcaches. This could perhaps create a temporary
173 regcache, collect the register values from mc_fpstate and
174 mc_xfpustate, and then set register values in the trad_frame. */
175
176 trad_frame_set_id (this_cache, frame_id_build (sp, func));
177 }
178
179 static const struct tramp_frame amd64_fbsd_sigframe =
180 {
181 SIGTRAMP_FRAME,
182 1,
183 {
184 {0x48, ULONGEST_MAX}, /* lea SIGF_UC(%rsp),%rdi */
185 {0x8d, ULONGEST_MAX},
186 {0x7c, ULONGEST_MAX},
187 {0x24, ULONGEST_MAX},
188 {0x10, ULONGEST_MAX},
189 {0x6a, ULONGEST_MAX}, /* pushq $0 */
190 {0x00, ULONGEST_MAX},
191 {0x48, ULONGEST_MAX}, /* movq $SYS_sigreturn,%rax */
192 {0xc7, ULONGEST_MAX},
193 {0xc0, ULONGEST_MAX},
194 {0xa1, ULONGEST_MAX},
195 {0x01, ULONGEST_MAX},
196 {0x00, ULONGEST_MAX},
197 {0x00, ULONGEST_MAX},
198 {0x0f, ULONGEST_MAX}, /* syscall */
199 {0x05, ULONGEST_MAX},
200 {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
201 },
202 amd64_fbsd_sigframe_init
203 };
204
205 /* Implement the core_read_description gdbarch method. */
206
207 static const struct target_desc *
208 amd64fbsd_core_read_description (struct gdbarch *gdbarch,
209 struct target_ops *target,
210 bfd *abfd)
211 {
212 return amd64_target_description (i386fbsd_core_read_xcr0 (abfd), true);
213 }
214
215 /* Similar to amd64_supply_fpregset, but use XSAVE extended state. */
216
217 static void
218 amd64fbsd_supply_xstateregset (const struct regset *regset,
219 struct regcache *regcache, int regnum,
220 const void *xstateregs, size_t len)
221 {
222 amd64_supply_xsave (regcache, regnum, xstateregs);
223 }
224
225 /* Similar to amd64_collect_fpregset, but use XSAVE extended state. */
226
227 static void
228 amd64fbsd_collect_xstateregset (const struct regset *regset,
229 const struct regcache *regcache,
230 int regnum, void *xstateregs, size_t len)
231 {
232 amd64_collect_xsave (regcache, regnum, xstateregs, 1);
233 }
234
235 static const struct regset amd64fbsd_xstateregset =
236 {
237 NULL,
238 amd64fbsd_supply_xstateregset,
239 amd64fbsd_collect_xstateregset
240 };
241
242 /* Iterate over core file register note sections. */
243
244 static void
245 amd64fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
246 iterate_over_regset_sections_cb *cb,
247 void *cb_data,
248 const struct regcache *regcache)
249 {
250 i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
251
252 cb (".reg", AMD64_FBSD_SIZEOF_GREGSET, AMD64_FBSD_SIZEOF_GREGSET,
253 &amd64_fbsd_gregset, NULL, cb_data);
254 cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, &amd64_fpregset,
255 NULL, cb_data);
256 cb (".reg-xstate", X86_XSTATE_SIZE (tdep->xcr0), X86_XSTATE_SIZE (tdep->xcr0),
257 &amd64fbsd_xstateregset, "XSAVE extended state", cb_data);
258 }
259
260 /* Implement the get_thread_local_address gdbarch method. */
261
262 static CORE_ADDR
263 amd64fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
264 CORE_ADDR lm_addr, CORE_ADDR offset)
265 {
266 struct regcache *regcache;
267
268 regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
269 ptid, gdbarch);
270
271 target_fetch_registers (regcache, AMD64_FSBASE_REGNUM);
272
273 ULONGEST fsbase;
274 if (regcache->cooked_read (AMD64_FSBASE_REGNUM, &fsbase) != REG_VALID)
275 error (_("Unable to fetch %%fsbase"));
276
277 CORE_ADDR dtv_addr = fsbase + gdbarch_ptr_bit (gdbarch) / 8;
278 return fbsd_get_thread_local_address (gdbarch, dtv_addr, lm_addr, offset);
279 }
280
281 static void
282 amd64fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
283 {
284 i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
285
286 /* Generic FreeBSD support. */
287 fbsd_init_abi (info, gdbarch);
288
289 /* Obviously FreeBSD is BSD-based. */
290 i386bsd_init_abi (info, gdbarch);
291
292 amd64_init_abi (info, gdbarch,
293 amd64_target_description (X86_XSTATE_SSE_MASK, true));
294
295 tramp_frame_prepend_unwinder (gdbarch, &amd64_fbsd_sigframe);
296
297 tdep->xsave_xcr0_offset = I386_FBSD_XSAVE_XCR0_OFFSET;
298
299 /* Iterate over core file register note sections. */
300 set_gdbarch_iterate_over_regset_sections
301 (gdbarch, amd64fbsd_iterate_over_regset_sections);
302
303 set_gdbarch_core_read_description (gdbarch,
304 amd64fbsd_core_read_description);
305
306 /* FreeBSD uses SVR4-style shared libraries. */
307 set_solib_svr4_fetch_link_map_offsets
308 (gdbarch, svr4_lp64_fetch_link_map_offsets);
309
310 set_gdbarch_fetch_tls_load_module_address (gdbarch,
311 svr4_fetch_objfile_link_map);
312 set_gdbarch_get_thread_local_address (gdbarch,
313 amd64fbsd_get_thread_local_address);
314 }
315
316 void _initialize_amd64fbsd_tdep ();
317 void
318 _initialize_amd64fbsd_tdep ()
319 {
320 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
321 GDB_OSABI_FREEBSD, amd64fbsd_init_abi);
322 }