Automatic date update in version.in
[binutils-gdb.git] / gdb / arm-fbsd-tdep.c
blob61c8f0cecad433bb5ccb7936126797e7cac47eec
1 /* Target-dependent code for FreeBSD/arm.
3 Copyright (C) 2017-2022 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/>. */
20 #include "defs.h"
22 #include "elf/common.h"
23 #include "target-descriptions.h"
24 #include "aarch32-tdep.h"
25 #include "arm-tdep.h"
26 #include "arm-fbsd-tdep.h"
27 #include "auxv.h"
28 #include "fbsd-tdep.h"
29 #include "gdbcore.h"
30 #include "inferior.h"
31 #include "osabi.h"
32 #include "solib-svr4.h"
33 #include "trad-frame.h"
34 #include "tramp-frame.h"
36 /* Register maps. */
38 static const struct regcache_map_entry arm_fbsd_gregmap[] =
40 { 13, ARM_A1_REGNUM, 4 }, /* r0 ... r12 */
41 { 1, ARM_SP_REGNUM, 4 },
42 { 1, ARM_LR_REGNUM, 4 },
43 { 1, ARM_PC_REGNUM, 4 },
44 { 1, ARM_PS_REGNUM, 4 },
45 { 0 }
48 static const struct regcache_map_entry arm_fbsd_vfpregmap[] =
50 { 32, ARM_D0_REGNUM, 8 }, /* d0 ... d31 */
51 { 1, ARM_FPSCR_REGNUM, 4 },
52 { 0 }
55 /* In a signal frame, sp points to a 'struct sigframe' which is
56 defined as:
58 struct sigframe {
59 siginfo_t sf_si;
60 ucontext_t sf_uc;
61 mcontext_vfp_t sf_vfp;
64 ucontext_t is defined as:
66 struct __ucontext {
67 sigset_t uc_sigmask;
68 mcontext_t uc_mcontext;
69 ...
72 mcontext_t is defined as:
74 struct {
75 unsigned int __gregs[17];
76 size_t mc_vfp_size;
77 void *mc_vfp_ptr;
78 ...
81 mcontext_vfp_t is defined as:
83 struct {
84 uint64_t mcv_reg[32];
85 uint32_t mcv_fpscr;
88 If the VFP state is valid, then mc_vfp_ptr will point to sf_vfp in
89 the sigframe, otherwise it is NULL. There is no non-VFP floating
90 point register state saved in the signal frame. */
92 #define ARM_SIGFRAME_UCONTEXT_OFFSET 64
93 #define ARM_UCONTEXT_MCONTEXT_OFFSET 16
94 #define ARM_MCONTEXT_VFP_PTR_OFFSET 72
96 /* Implement the "init" method of struct tramp_frame. */
98 static void
99 arm_fbsd_sigframe_init (const struct tramp_frame *self,
100 struct frame_info *this_frame,
101 struct trad_frame_cache *this_cache,
102 CORE_ADDR func)
104 struct gdbarch *gdbarch = get_frame_arch (this_frame);
105 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
106 CORE_ADDR sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
107 CORE_ADDR mcontext_addr = (sp
108 + ARM_SIGFRAME_UCONTEXT_OFFSET
109 + ARM_UCONTEXT_MCONTEXT_OFFSET);
110 ULONGEST mcontext_vfp_addr;
112 trad_frame_set_reg_regmap (this_cache, arm_fbsd_gregmap, mcontext_addr,
113 regcache_map_entry_size (arm_fbsd_gregmap));
115 if (safe_read_memory_unsigned_integer (mcontext_addr
116 + ARM_MCONTEXT_VFP_PTR_OFFSET, 4,
117 byte_order,
118 &mcontext_vfp_addr)
119 && mcontext_vfp_addr != 0)
120 trad_frame_set_reg_regmap (this_cache, arm_fbsd_vfpregmap, mcontext_vfp_addr,
121 regcache_map_entry_size (arm_fbsd_vfpregmap));
123 trad_frame_set_id (this_cache, frame_id_build (sp, func));
126 static const struct tramp_frame arm_fbsd_sigframe =
128 SIGTRAMP_FRAME,
131 {0xe1a0000d, ULONGEST_MAX}, /* mov r0, sp */
132 {0xe2800040, ULONGEST_MAX}, /* add r0, r0, #SIGF_UC */
133 {0xe59f700c, ULONGEST_MAX}, /* ldr r7, [pc, #12] */
134 {0xef0001a1, ULONGEST_MAX}, /* swi SYS_sigreturn */
135 {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
137 arm_fbsd_sigframe_init
140 /* Register set definitions. */
142 const struct regset arm_fbsd_gregset =
144 arm_fbsd_gregmap,
145 regcache_supply_regset, regcache_collect_regset
148 const struct regset arm_fbsd_vfpregset =
150 arm_fbsd_vfpregmap,
151 regcache_supply_regset, regcache_collect_regset
154 /* Implement the "iterate_over_regset_sections" gdbarch method. */
156 static void
157 arm_fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
158 iterate_over_regset_sections_cb *cb,
159 void *cb_data,
160 const struct regcache *regcache)
162 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
164 cb (".reg", ARM_FBSD_SIZEOF_GREGSET, ARM_FBSD_SIZEOF_GREGSET,
165 &arm_fbsd_gregset, NULL, cb_data);
167 if (tdep->tls_regnum > 0)
169 const struct regcache_map_entry arm_fbsd_tlsregmap[] =
171 { 1, tdep->tls_regnum, 4 },
172 { 0 }
175 const struct regset arm_fbsd_tlsregset =
177 arm_fbsd_tlsregmap,
178 regcache_supply_regset, regcache_collect_regset
181 cb (".reg-aarch-tls", ARM_FBSD_SIZEOF_TLSREGSET, ARM_FBSD_SIZEOF_TLSREGSET,
182 &arm_fbsd_tlsregset, NULL, cb_data);
185 /* While FreeBSD/arm cores do contain a NT_FPREGSET / ".reg2"
186 register set, it is not populated with register values by the
187 kernel but just contains all zeroes. */
188 if (tdep->vfp_register_count > 0)
189 cb (".reg-arm-vfp", ARM_FBSD_SIZEOF_VFPREGSET, ARM_FBSD_SIZEOF_VFPREGSET,
190 &arm_fbsd_vfpregset, "VFP floating-point", cb_data);
193 /* Lookup a target description from a target's AT_HWCAP auxiliary
194 vector. */
196 const struct target_desc *
197 arm_fbsd_read_description_auxv (struct target_ops *target, bool tls)
199 CORE_ADDR arm_hwcap = 0;
201 if (target_auxv_search (target, AT_FREEBSD_HWCAP, &arm_hwcap) != 1)
202 return arm_read_description (ARM_FP_TYPE_NONE, tls);
204 if (arm_hwcap & HWCAP_VFP)
206 if (arm_hwcap & HWCAP_NEON)
207 return aarch32_read_description ();
208 else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPD32))
209 == (HWCAP_VFPv3 | HWCAP_VFPD32))
210 return arm_read_description (ARM_FP_TYPE_VFPV3, tls);
211 else
212 return arm_read_description (ARM_FP_TYPE_VFPV2, tls);
215 return arm_read_description (ARM_FP_TYPE_NONE, tls);
218 /* Implement the "core_read_description" gdbarch method. */
220 static const struct target_desc *
221 arm_fbsd_core_read_description (struct gdbarch *gdbarch,
222 struct target_ops *target,
223 bfd *abfd)
225 asection *tls = bfd_get_section_by_name (abfd, ".reg-aarch-tls");
227 return arm_fbsd_read_description_auxv (target, tls != nullptr);
230 /* Implement the get_thread_local_address gdbarch method. */
232 static CORE_ADDR
233 arm_fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
234 CORE_ADDR lm_addr, CORE_ADDR offset)
236 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
237 struct regcache *regcache;
239 regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
240 ptid, gdbarch);
242 target_fetch_registers (regcache, tdep->tls_regnum);
244 ULONGEST tpidruro;
245 if (regcache->cooked_read (tdep->tls_regnum, &tpidruro) != REG_VALID)
246 error (_("Unable to fetch %%tpidruro"));
248 /* %tpidruro points to the TCB whose first member is the dtv
249 pointer. */
250 CORE_ADDR dtv_addr = tpidruro;
251 return fbsd_get_thread_local_address (gdbarch, dtv_addr, lm_addr, offset);
254 /* Implement the 'init_osabi' method of struct gdb_osabi_handler. */
256 static void
257 arm_fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
259 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
261 /* Generic FreeBSD support. */
262 fbsd_init_abi (info, gdbarch);
264 if (tdep->fp_model == ARM_FLOAT_AUTO)
265 tdep->fp_model = ARM_FLOAT_SOFT_VFP;
267 tramp_frame_prepend_unwinder (gdbarch, &arm_fbsd_sigframe);
269 set_solib_svr4_fetch_link_map_offsets
270 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
272 tdep->jb_pc = 24;
273 tdep->jb_elt_size = 4;
275 set_gdbarch_iterate_over_regset_sections
276 (gdbarch, arm_fbsd_iterate_over_regset_sections);
277 set_gdbarch_core_read_description (gdbarch, arm_fbsd_core_read_description);
279 if (tdep->tls_regnum > 0)
281 set_gdbarch_fetch_tls_load_module_address (gdbarch,
282 svr4_fetch_objfile_link_map);
283 set_gdbarch_get_thread_local_address (gdbarch,
284 arm_fbsd_get_thread_local_address);
287 /* Single stepping. */
288 set_gdbarch_software_single_step (gdbarch, arm_software_single_step);
291 void _initialize_arm_fbsd_tdep ();
292 void
293 _initialize_arm_fbsd_tdep ()
295 gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_FREEBSD,
296 arm_fbsd_init_abi);