GDB (xrefs)
Loading...
Searching...
No Matches
i386-fbsd-tdep.c
Go to the documentation of this file.
1/* Target-dependent code for FreeBSD/i386.
2
3 Copyright (C) 2003-2023 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 "gdbcore.h"
22#include "osabi.h"
23#include "regcache.h"
24#include "regset.h"
25#include "trad-frame.h"
26#include "tramp-frame.h"
27#include "i386-fbsd-tdep.h"
28
29#include "i386-tdep.h"
30#include "i387-tdep.h"
31#include "fbsd-tdep.h"
32#include "solib-svr4.h"
33#include "inferior.h"
34
35/* The general-purpose regset consists of 19 32-bit slots. */
36#define I386_FBSD_SIZEOF_GREGSET (19 * 4)
37
38/* The segment base register set consists of 2 32-bit registers. */
39#define I386_FBSD_SIZEOF_SEGBASES_REGSET (2 * 4)
40
41/* Register maps. */
42
44{
45 { 1, I386_FS_REGNUM, 4 },
46 { 1, I386_ES_REGNUM, 4 },
47 { 1, I386_DS_REGNUM, 4 },
48 { 1, I386_EDI_REGNUM, 0 },
49 { 1, I386_ESI_REGNUM, 0 },
50 { 1, I386_EBP_REGNUM, 0 },
51 { 1, REGCACHE_MAP_SKIP, 4 }, /* isp */
52 { 1, I386_EBX_REGNUM, 0 },
53 { 1, I386_EDX_REGNUM, 0 },
54 { 1, I386_ECX_REGNUM, 0 },
55 { 1, I386_EAX_REGNUM, 0 },
56 { 1, REGCACHE_MAP_SKIP, 4 }, /* trapno */
57 { 1, REGCACHE_MAP_SKIP, 4 }, /* err */
58 { 1, I386_EIP_REGNUM, 0 },
59 { 1, I386_CS_REGNUM, 4 },
60 { 1, I386_EFLAGS_REGNUM, 0 },
61 { 1, I386_ESP_REGNUM, 0 },
62 { 1, I386_SS_REGNUM, 4 },
63 { 1, I386_GS_REGNUM, 4 },
64 { 0 }
65};
66
68{
69 { 1, I386_FSBASE_REGNUM, 0 },
70 { 1, I386_GSBASE_REGNUM, 0 },
71 { 0 }
72};
73
74/* This layout including fsbase and gsbase was adopted in FreeBSD
75 8.0. */
76
78{
79 { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_onstack */
80 { 1, I386_GS_REGNUM, 4 },
81 { 1, I386_FS_REGNUM, 4 },
82 { 1, I386_ES_REGNUM, 4 },
83 { 1, I386_DS_REGNUM, 4 },
84 { 1, I386_EDI_REGNUM, 0 },
85 { 1, I386_ESI_REGNUM, 0 },
86 { 1, I386_EBP_REGNUM, 0 },
87 { 1, REGCACHE_MAP_SKIP, 4 }, /* isp */
88 { 1, I386_EBX_REGNUM, 0 },
89 { 1, I386_EDX_REGNUM, 0 },
90 { 1, I386_ECX_REGNUM, 0 },
91 { 1, I386_EAX_REGNUM, 0 },
92 { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_trapno */
93 { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_err */
94 { 1, I386_EIP_REGNUM, 0 },
95 { 1, I386_CS_REGNUM, 4 },
96 { 1, I386_EFLAGS_REGNUM, 0 },
97 { 1, I386_ESP_REGNUM, 0 },
98 { 1, I386_SS_REGNUM, 4 },
99 { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_len */
100 { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_fpformat */
101 { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_ownedfp */
102 { 1, REGCACHE_MAP_SKIP, 4 }, /* mc_flags */
103 { 128, REGCACHE_MAP_SKIP, 4 },/* mc_fpstate */
104 { 1, I386_FSBASE_REGNUM, 0 },
105 { 1, I386_GSBASE_REGNUM, 0 },
106 { 0 }
107};
108
109/* Register set definitions. */
110
115
120
121/* Support for signal handlers. */
122
123/* In a signal frame, esp points to a 'struct sigframe' which is
124 defined as:
125
126 struct sigframe {
127 register_t sf_signum;
128 register_t sf_siginfo;
129 register_t sf_ucontext;
130 register_t sf_addr;
131 union {
132 __siginfohandler_t *sf_action;
133 __sighandler_t *sf_handler;
134 } sf_ahu;
135 ucontext_t sf_uc;
136 ...
137 }
138
139 ucontext_t is defined as:
140
141 struct __ucontext {
142 sigset_t uc_sigmask;
143 mcontext_t uc_mcontext;
144 ...
145 };
146
147 The mcontext_t contains the general purpose register set as well
148 as the floating point or XSAVE state. */
149
150/* NB: There is a 12 byte padding hole between sf_ahu and sf_uc. */
151#define I386_SIGFRAME_UCONTEXT_OFFSET 32
152#define I386_UCONTEXT_MCONTEXT_OFFSET 16
153#define I386_SIZEOF_MCONTEXT_T 640
154
155/* Implement the "init" method of struct tramp_frame. */
156
157static void
159 frame_info_ptr this_frame,
160 struct trad_frame_cache *this_cache,
161 CORE_ADDR func)
162{
163 CORE_ADDR sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
164 CORE_ADDR mcontext_addr
165 = (sp
168
169 trad_frame_set_reg_regmap (this_cache, i386_fbsd_mcregmap, mcontext_addr,
171
172 /* Don't bother with floating point or XSAVE state for now. The
173 current helper routines for parsing FXSAVE and XSAVE state only
174 work with regcaches. This could perhaps create a temporary
175 regcache, collect the register values from mc_fpstate and
176 mc_xfpustate, and then set register values in the trad_frame. */
177
178 trad_frame_set_id (this_cache, frame_id_build (sp, func));
179}
180
181static const struct tramp_frame i386_fbsd_sigframe =
182{
184 1,
185 {
186 {0x8d, ULONGEST_MAX}, /* lea SIGF_UC(%esp),%eax */
187 {0x44, ULONGEST_MAX},
188 {0x24, ULONGEST_MAX},
189 {0x20, ULONGEST_MAX},
190 {0x50, ULONGEST_MAX}, /* pushl %eax */
191 {0xf7, ULONGEST_MAX}, /* testl $PSL_VM,UC_EFLAGS(%eax) */
192 {0x40, ULONGEST_MAX},
193 {0x54, ULONGEST_MAX},
194 {0x00, ULONGEST_MAX},
195 {0x00, ULONGEST_MAX},
196 {0x02, ULONGEST_MAX},
197 {0x00, ULONGEST_MAX},
198 {0x75, ULONGEST_MAX}, /* jne +3 */
199 {0x03, ULONGEST_MAX},
200 {0x8e, ULONGEST_MAX}, /* mov UC_GS(%eax),%gs */
201 {0x68, ULONGEST_MAX},
202 {0x14, ULONGEST_MAX},
203 {0xb8, ULONGEST_MAX}, /* movl $SYS_sigreturn,%eax */
204 {0xa1, ULONGEST_MAX},
205 {0x01, ULONGEST_MAX},
206 {0x00, ULONGEST_MAX},
207 {0x00, ULONGEST_MAX},
208 {0x50, ULONGEST_MAX}, /* pushl %eax */
209 {0xcd, ULONGEST_MAX}, /* int $0x80 */
210 {0x80, ULONGEST_MAX},
211 {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
212 },
214};
215
216/* FreeBSD/i386 binaries running under an amd64 kernel use a different
217 trampoline. This trampoline differs from the i386 kernel trampoline
218 in that it omits a middle section that conditionally restores
219 %gs. */
220
222{
224 1,
225 {
226 {0x8d, ULONGEST_MAX}, /* lea SIGF_UC(%esp),%eax */
227 {0x44, ULONGEST_MAX},
228 {0x24, ULONGEST_MAX},
229 {0x20, ULONGEST_MAX},
230 {0x50, ULONGEST_MAX}, /* pushl %eax */
231 {0xb8, ULONGEST_MAX}, /* movl $SYS_sigreturn,%eax */
232 {0xa1, ULONGEST_MAX},
233 {0x01, ULONGEST_MAX},
234 {0x00, ULONGEST_MAX},
235 {0x00, ULONGEST_MAX},
236 {0x50, ULONGEST_MAX}, /* pushl %eax */
237 {0xcd, ULONGEST_MAX}, /* int $0x80 */
238 {0x80, ULONGEST_MAX},
239 {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
240 },
242};
243
244/* See i386-fbsd-tdep.h. */
245
246uint64_t
247i386_fbsd_core_read_xsave_info (bfd *abfd, x86_xsave_layout &layout)
248{
249 asection *xstate = bfd_get_section_by_name (abfd, ".reg-xstate");
250 if (xstate == nullptr)
251 return 0;
252
253 /* Check extended state size. */
254 size_t size = bfd_section_size (xstate);
255 if (size < X86_XSTATE_AVX_SIZE)
256 return 0;
257
258 char contents[8];
259 if (! bfd_get_section_contents (abfd, xstate, contents,
261 {
262 warning (_("Couldn't read `xcr0' bytes from "
263 "`.reg-xstate' section in core file."));
264 return 0;
265 }
266
267 uint64_t xcr0 = bfd_get_64 (abfd, contents);
268
269 if (!i387_guess_xsave_layout (xcr0, size, layout))
270 return 0;
271
272 return xcr0;
273}
274
275/* See i386-fbsd-tdep.h. */
276
277bool
279 x86_xsave_layout &layout)
280{
281 return i386_fbsd_core_read_xsave_info (core_bfd, layout) != 0;
282}
283
284/* Implement the core_read_description gdbarch method. */
285
286static const struct target_desc *
288 struct target_ops *target,
289 bfd *abfd)
290{
291 x86_xsave_layout layout;
292 uint64_t xcr0 = i386_fbsd_core_read_xsave_info (abfd, layout);
293 if (xcr0 == 0)
294 xcr0 = X86_XSTATE_X87_MASK;
295 return i386_target_description (xcr0, true);
296}
297
298/* Similar to i386_supply_fpregset, but use XSAVE extended state. */
299
300static void
302 struct regcache *regcache, int regnum,
303 const void *xstateregs, size_t len)
304{
305 i387_supply_xsave (regcache, regnum, xstateregs);
306}
307
308/* Similar to i386_collect_fpregset, but use XSAVE extended state. */
309
310static void
312 const struct regcache *regcache,
313 int regnum, void *xstateregs, size_t len)
314{
315 i387_collect_xsave (regcache, regnum, xstateregs, 1);
316}
317
318/* Register set definitions. */
319
326
327/* Iterate over core file register note sections. */
328
329static void
332 void *cb_data,
333 const struct regcache *regcache)
334{
335 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
336
338 &i386_fbsd_gregset, NULL, cb_data);
339 cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, &i386_fpregset,
340 NULL, cb_data);
341 cb (".reg-x86-segbases", I386_FBSD_SIZEOF_SEGBASES_REGSET,
343 "segment bases", cb_data);
344
345 if (tdep->xsave_layout.sizeof_xsave != 0)
346 cb (".reg-xstate", tdep->xsave_layout.sizeof_xsave,
347 tdep->xsave_layout.sizeof_xsave, &i386fbsd_xstateregset,
348 "XSAVE extended state", cb_data);
349}
350
351/* Implement the get_thread_local_address gdbarch method. */
352
353static CORE_ADDR
355 CORE_ADDR lm_addr, CORE_ADDR offset)
356{
357 struct regcache *regcache;
358
359 regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
360 ptid, gdbarch);
361
363
364 ULONGEST gsbase;
365 if (regcache->cooked_read (I386_GSBASE_REGNUM, &gsbase) != REG_VALID)
366 error (_("Unable to fetch %%gsbase"));
367
368 CORE_ADDR dtv_addr = gsbase + gdbarch_ptr_bit (gdbarch) / 8;
369 return fbsd_get_thread_local_address (gdbarch, dtv_addr, lm_addr, offset);
370}
371
372static void
374{
375 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
376
377 /* Generic FreeBSD support. */
378 fbsd_init_abi (info, gdbarch);
379
380 /* Obviously FreeBSD is BSD-based. */
382
383 /* FreeBSD reserves some space for its FPU emulator in
384 `struct fpreg'. */
385 tdep->sizeof_fpregset = 176;
386
387 /* FreeBSD uses -freg-struct-return by default. */
388 tdep->struct_return = reg_struct_return;
389
392
394
398
399 /* Iterate over core file register note sections. */
402
405
406 /* FreeBSD uses SVR4-style shared libraries. */
409
414}
415
417void
int regnum
@ reg_struct_return
Definition arm-tdep.h:86
enum register_status cooked_read(int regnum, gdb_byte *buf)
Definition regcache.c:698
friend class regcache
Definition regcache.h:269
ptid_t ptid() const
Definition regcache.h:408
CORE_ADDR fbsd_get_thread_local_address(struct gdbarch *gdbarch, CORE_ADDR dtv_addr, CORE_ADDR lm_addr, CORE_ADDR offset)
Definition fbsd-tdep.c:2027
void fbsd_init_abi(struct gdbarch_info info, struct gdbarch *gdbarch)
Definition fbsd-tdep.c:2380
ULONGEST get_frame_register_unsigned(frame_info_ptr frame, int regnum)
Definition frame.c:1399
struct frame_id frame_id_build(CORE_ADDR stack_addr, CORE_ADDR code_addr)
Definition frame.c:736
@ SIGTRAMP_FRAME
Definition frame.h:198
void set_gdbarch_get_thread_local_address(struct gdbarch *gdbarch, gdbarch_get_thread_local_address_ftype *get_thread_local_address)
void set_gdbarch_core_read_description(struct gdbarch *gdbarch, gdbarch_core_read_description_ftype *core_read_description)
void set_gdbarch_core_read_x86_xsave_layout(struct gdbarch *gdbarch, gdbarch_core_read_x86_xsave_layout_ftype *core_read_x86_xsave_layout)
void set_gdbarch_fetch_tls_load_module_address(struct gdbarch *gdbarch, gdbarch_fetch_tls_load_module_address_ftype *fetch_tls_load_module_address)
int gdbarch_ptr_bit(struct gdbarch *gdbarch)
Definition gdbarch.c:1722
void set_gdbarch_iterate_over_regset_sections(struct gdbarch *gdbarch, gdbarch_iterate_over_regset_sections_ftype *iterate_over_regset_sections)
void iterate_over_regset_sections_cb(const char *sect_name, int supply_size, int collect_size, const struct regset *regset, const char *human_name, void *cb_data)
Definition gdbarch.h:104
#define core_bfd
Definition gdbcore.h:130
size_t size
Definition go32-nat.c:239
void i386bsd_init_abi(struct gdbarch_info info, struct gdbarch *gdbarch)
static const struct tramp_frame i386_fbsd_sigframe
#define I386_SIGFRAME_UCONTEXT_OFFSET
static void i386fbsd_collect_xstateregset(const struct regset *regset, const struct regcache *regcache, int regnum, void *xstateregs, size_t len)
static const struct regset i386fbsd_xstateregset
static const struct tramp_frame i386_fbsd64_sigframe
static void i386fbsd_iterate_over_regset_sections(struct gdbarch *gdbarch, iterate_over_regset_sections_cb *cb, void *cb_data, const struct regcache *regcache)
static const struct regcache_map_entry i386_fbsd_gregmap[]
static const struct regcache_map_entry i386_fbsd_mcregmap[]
static CORE_ADDR i386fbsd_get_thread_local_address(struct gdbarch *gdbarch, ptid_t ptid, CORE_ADDR lm_addr, CORE_ADDR offset)
void _initialize_i386fbsd_tdep()
static const struct regcache_map_entry i386_fbsd_segbases_regmap[]
static const struct target_desc * i386fbsd_core_read_description(struct gdbarch *gdbarch, struct target_ops *target, bfd *abfd)
const struct regset i386_fbsd_gregset
#define I386_UCONTEXT_MCONTEXT_OFFSET
static void i386_fbsd_sigframe_init(const struct tramp_frame *self, frame_info_ptr this_frame, struct trad_frame_cache *this_cache, CORE_ADDR func)
#define I386_SIZEOF_MCONTEXT_T
#define I386_FBSD_SIZEOF_GREGSET
#define I386_FBSD_SIZEOF_SEGBASES_REGSET
uint64_t i386_fbsd_core_read_xsave_info(bfd *abfd, x86_xsave_layout &layout)
bool i386_fbsd_core_read_x86_xsave_layout(struct gdbarch *gdbarch, x86_xsave_layout &layout)
const struct regset i386_fbsd_segbases_regset
static void i386fbsd_supply_xstateregset(const struct regset *regset, struct regcache *regcache, int regnum, const void *xstateregs, size_t len)
static void i386fbsd_init_abi(struct gdbarch_info info, struct gdbarch *gdbarch)
#define I386_FBSD_XSAVE_XCR0_OFFSET
const struct regset i386_fpregset
Definition i386-tdep.c:4047
const struct target_desc * i386_target_description(uint64_t xcr0, bool segments)
Definition i386-tdep.c:8957
void i386_elf_init_abi(struct gdbarch_info info, struct gdbarch *gdbarch)
Definition i386-tdep.c:4530
@ I386_GSBASE_REGNUM
Definition i386-tdep.h:311
@ I386_EFLAGS_REGNUM
Definition i386-tdep.h:290
@ I386_ESI_REGNUM
Definition i386-tdep.h:287
@ I386_FS_REGNUM
Definition i386-tdep.h:295
@ I386_ECX_REGNUM
Definition i386-tdep.h:282
@ I386_GS_REGNUM
Definition i386-tdep.h:296
@ I386_EIP_REGNUM
Definition i386-tdep.h:289
@ I386_EBP_REGNUM
Definition i386-tdep.h:286
@ I386_ES_REGNUM
Definition i386-tdep.h:294
@ I386_CS_REGNUM
Definition i386-tdep.h:291
@ I386_EAX_REGNUM
Definition i386-tdep.h:281
@ I386_SS_REGNUM
Definition i386-tdep.h:292
@ I386_EDX_REGNUM
Definition i386-tdep.h:283
@ I386_ESP_REGNUM
Definition i386-tdep.h:285
@ I386_EBX_REGNUM
Definition i386-tdep.h:284
@ I386_DS_REGNUM
Definition i386-tdep.h:293
@ I386_EDI_REGNUM
Definition i386-tdep.h:288
@ I386_FSBASE_REGNUM
Definition i386-tdep.h:310
void i387_supply_xsave(struct regcache *regcache, int regnum, const void *xsave)
Definition i387-tdep.c:1061
bool i387_guess_xsave_layout(uint64_t xcr0, size_t xsave_size, x86_xsave_layout &layout)
Definition i387-tdep.c:938
void i387_collect_xsave(const struct regcache *regcache, int regnum, void *xsave, int gcore)
Definition i387-tdep.c:1508
struct inferior * current_inferior(void)
Definition inferior.c:55
static CORE_ADDR lm_addr(struct so_list *so)
Definition nto-tdep.c:246
void gdbarch_register_osabi(enum bfd_architecture arch, unsigned long machine, enum gdb_osabi osabi, void(*init_osabi)(struct gdbarch_info, struct gdbarch *))
Definition osabi.c:146
@ GDB_OSABI_FREEBSD
Definition osabi.h:33
void regcache_collect_regset(const struct regset *regset, const struct regcache *regcache, int regnum, void *buf, size_t size)
Definition regcache.c:1273
struct regcache * get_thread_arch_regcache(process_stratum_target *target, ptid_t ptid, struct gdbarch *gdbarch)
Definition regcache.c:384
void regcache_supply_regset(const struct regset *regset, struct regcache *regcache, int regnum, const void *buf, size_t size)
Definition regcache.c:1251
@ REGCACHE_MAP_SKIP
Definition regcache.h:121
void(* func)(remote_target *remote, char *)
void set_solib_svr4_fetch_link_map_offsets(struct gdbarch *gdbarch, struct link_map_offsets *(*flmo)(void))
CORE_ADDR svr4_fetch_objfile_link_map(struct objfile *objfile)
struct link_map_offsets * svr4_ilp32_fetch_link_map_offsets(void)
size_t sizeof_fpregset
Definition i386-tdep.h:69
x86_xsave_layout xsave_layout
Definition i386-tdep.h:150
Definition regcache.h:111
void target_fetch_registers(struct regcache *regcache, int regno)
Definition target.c:3928
void trad_frame_set_reg_regmap(struct trad_frame_cache *this_trad_cache, const struct regcache_map_entry *regmap, CORE_ADDR addr, size_t size)
Definition trad-frame.c:117
void trad_frame_set_id(struct trad_frame_cache *this_trad_cache, struct frame_id this_id)
Definition trad-frame.c:220
void tramp_frame_prepend_unwinder(struct gdbarch *gdbarch, const struct tramp_frame *tramp_frame)
#define TRAMP_SENTINEL_INSN
Definition tramp-frame.h:44