Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / aarch64-fbsd-tdep.c
blobe2ff57e83900cd93d4c0367624d022880e7f2937
1 /* Target-dependent code for FreeBSD/aarch64.
3 Copyright (C) 2017-2024 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 "gdbarch.h"
22 #include "fbsd-tdep.h"
23 #include "aarch64-tdep.h"
24 #include "aarch64-fbsd-tdep.h"
25 #include "inferior.h"
26 #include "osabi.h"
27 #include "solib-svr4.h"
28 #include "target.h"
29 #include "tramp-frame.h"
30 #include "trad-frame.h"
32 /* Register maps. */
34 static const struct regcache_map_entry aarch64_fbsd_gregmap[] =
36 { 30, AARCH64_X0_REGNUM, 8 }, /* x0 ... x29 */
37 { 1, AARCH64_LR_REGNUM, 8 },
38 { 1, AARCH64_SP_REGNUM, 8 },
39 { 1, AARCH64_PC_REGNUM, 8 },
40 { 1, AARCH64_CPSR_REGNUM, 4 },
41 { 0 }
44 static const struct regcache_map_entry aarch64_fbsd_fpregmap[] =
46 { 32, AARCH64_V0_REGNUM, 16 }, /* v0 ... v31 */
47 { 1, AARCH64_FPSR_REGNUM, 4 },
48 { 1, AARCH64_FPCR_REGNUM, 4 },
49 { 0 }
52 /* Register numbers are relative to tdep->tls_regnum_base. */
54 static const struct regcache_map_entry aarch64_fbsd_tls_regmap[] =
56 { 1, 0, 8 }, /* tpidr */
57 { 0 }
60 /* In a signal frame, sp points to a 'struct sigframe' which is
61 defined as:
63 struct sigframe {
64 siginfo_t sf_si;
65 ucontext_t sf_uc;
68 ucontext_t is defined as:
70 struct __ucontext {
71 sigset_t uc_sigmask;
72 mcontext_t uc_mcontext;
73 ...
76 The mcontext_t contains the general purpose register set followed
77 by the floating point register set. The floating point register
78 set is only valid if the _MC_FP_VALID flag is set in mc_flags. */
80 #define AARCH64_SIGFRAME_UCONTEXT_OFFSET 80
81 #define AARCH64_UCONTEXT_MCONTEXT_OFFSET 16
82 #define AARCH64_MCONTEXT_FPREGS_OFFSET 272
83 #define AARCH64_MCONTEXT_FLAGS_OFFSET 800
84 #define AARCH64_MCONTEXT_FLAG_FP_VALID 0x1
86 /* Implement the "init" method of struct tramp_frame. */
88 static void
89 aarch64_fbsd_sigframe_init (const struct tramp_frame *self,
90 const frame_info_ptr &this_frame,
91 struct trad_frame_cache *this_cache,
92 CORE_ADDR func)
94 struct gdbarch *gdbarch = get_frame_arch (this_frame);
95 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
96 CORE_ADDR sp = get_frame_register_unsigned (this_frame, AARCH64_SP_REGNUM);
97 CORE_ADDR mcontext_addr
98 = (sp
99 + AARCH64_SIGFRAME_UCONTEXT_OFFSET
100 + AARCH64_UCONTEXT_MCONTEXT_OFFSET);
101 gdb_byte buf[4];
103 trad_frame_set_reg_regmap (this_cache, aarch64_fbsd_gregmap, mcontext_addr,
104 regcache_map_entry_size (aarch64_fbsd_gregmap));
106 if (target_read_memory (mcontext_addr + AARCH64_MCONTEXT_FLAGS_OFFSET, buf,
107 4) == 0
108 && (extract_unsigned_integer (buf, 4, byte_order)
109 & AARCH64_MCONTEXT_FLAG_FP_VALID))
110 trad_frame_set_reg_regmap (this_cache, aarch64_fbsd_fpregmap,
111 mcontext_addr + AARCH64_MCONTEXT_FPREGS_OFFSET,
112 regcache_map_entry_size (aarch64_fbsd_fpregmap));
114 trad_frame_set_id (this_cache, frame_id_build (sp, func));
117 static const struct tramp_frame aarch64_fbsd_sigframe =
119 SIGTRAMP_FRAME,
122 {0x910003e0, ULONGEST_MAX}, /* mov x0, sp */
123 {0x91014000, ULONGEST_MAX}, /* add x0, x0, #SF_UC */
124 {0xd2803428, ULONGEST_MAX}, /* mov x8, #SYS_sigreturn */
125 {0xd4000001, ULONGEST_MAX}, /* svc 0x0 */
126 {TRAMP_SENTINEL_INSN, ULONGEST_MAX}
128 aarch64_fbsd_sigframe_init
131 /* Register set definitions. */
133 const struct regset aarch64_fbsd_gregset =
135 aarch64_fbsd_gregmap,
136 regcache_supply_regset, regcache_collect_regset
139 const struct regset aarch64_fbsd_fpregset =
141 aarch64_fbsd_fpregmap,
142 regcache_supply_regset, regcache_collect_regset
145 static void
146 aarch64_fbsd_supply_tls_regset (const struct regset *regset,
147 struct regcache *regcache,
148 int regnum, const void *buf, size_t size)
150 struct gdbarch *gdbarch = regcache->arch ();
151 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
153 regcache->supply_regset (regset, tdep->tls_regnum_base, regnum, buf, size);
156 static void
157 aarch64_fbsd_collect_tls_regset (const struct regset *regset,
158 const struct regcache *regcache,
159 int regnum, void *buf, size_t size)
161 struct gdbarch *gdbarch = regcache->arch ();
162 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
164 regcache->collect_regset (regset, tdep->tls_regnum_base, regnum, buf, size);
167 const struct regset aarch64_fbsd_tls_regset =
169 aarch64_fbsd_tls_regmap,
170 aarch64_fbsd_supply_tls_regset, aarch64_fbsd_collect_tls_regset
173 /* Implement the "iterate_over_regset_sections" gdbarch method. */
175 static void
176 aarch64_fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
177 iterate_over_regset_sections_cb *cb,
178 void *cb_data,
179 const struct regcache *regcache)
181 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
183 cb (".reg", AARCH64_FBSD_SIZEOF_GREGSET, AARCH64_FBSD_SIZEOF_GREGSET,
184 &aarch64_fbsd_gregset, NULL, cb_data);
185 cb (".reg2", AARCH64_FBSD_SIZEOF_FPREGSET, AARCH64_FBSD_SIZEOF_FPREGSET,
186 &aarch64_fbsd_fpregset, NULL, cb_data);
188 if (tdep->has_tls ())
189 cb (".reg-aarch-tls", AARCH64_FBSD_SIZEOF_TLSREGSET,
190 AARCH64_FBSD_SIZEOF_TLSREGSET, &aarch64_fbsd_tls_regset,
191 "TLS register", cb_data);
194 /* Implement the "core_read_description" gdbarch method. */
196 static const struct target_desc *
197 aarch64_fbsd_core_read_description (struct gdbarch *gdbarch,
198 struct target_ops *target, bfd *abfd)
200 asection *tls = bfd_get_section_by_name (abfd, ".reg-aarch-tls");
202 aarch64_features features;
203 features.tls = tls != nullptr? 1 : 0;
205 return aarch64_read_description (features);
208 /* Implement the get_thread_local_address gdbarch method. */
210 static CORE_ADDR
211 aarch64_fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
212 CORE_ADDR lm_addr, CORE_ADDR offset)
214 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
215 struct regcache *regcache;
217 regcache = get_thread_arch_regcache (current_inferior (), ptid, gdbarch);
219 target_fetch_registers (regcache, tdep->tls_regnum_base);
221 ULONGEST tpidr;
222 if (regcache->cooked_read (tdep->tls_regnum_base, &tpidr) != REG_VALID)
223 error (_("Unable to fetch %%tpidr"));
225 /* %tpidr points to the TCB whose first member is the dtv
226 pointer. */
227 CORE_ADDR dtv_addr = tpidr;
228 return fbsd_get_thread_local_address (gdbarch, dtv_addr, lm_addr, offset);
231 /* Implement the 'init_osabi' method of struct gdb_osabi_handler. */
233 static void
234 aarch64_fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
236 aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
238 /* Generic FreeBSD support. */
239 fbsd_init_abi (info, gdbarch);
241 set_solib_svr4_fetch_link_map_offsets (gdbarch,
242 svr4_lp64_fetch_link_map_offsets);
244 tramp_frame_prepend_unwinder (gdbarch, &aarch64_fbsd_sigframe);
246 /* Enable longjmp. */
247 tdep->jb_pc = 13;
249 set_gdbarch_iterate_over_regset_sections
250 (gdbarch, aarch64_fbsd_iterate_over_regset_sections);
251 set_gdbarch_core_read_description (gdbarch,
252 aarch64_fbsd_core_read_description);
254 if (tdep->has_tls ())
256 set_gdbarch_fetch_tls_load_module_address (gdbarch,
257 svr4_fetch_objfile_link_map);
258 set_gdbarch_get_thread_local_address
259 (gdbarch, aarch64_fbsd_get_thread_local_address);
263 void _initialize_aarch64_fbsd_tdep ();
264 void
265 _initialize_aarch64_fbsd_tdep ()
267 gdbarch_register_osabi (bfd_arch_aarch64, 0, GDB_OSABI_FREEBSD,
268 aarch64_fbsd_init_abi);