gdb: convert callback_handler_installed from int to bool
[binutils-gdb.git] / gdb / loongarch-tdep.c
1 /* Target-dependent code for the LoongArch architecture, for GDB.
2
3 Copyright (C) 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 "arch-utils.h"
22 #include "dwarf2/frame.h"
23 #include "elf-bfd.h"
24 #include "frame-unwind.h"
25 #include "loongarch-tdep.h"
26 #include "target-descriptions.h"
27 #include "trad-frame.h"
28 #include "user-regs.h"
29
30 /* Implement the loongarch_skip_prologue gdbarch method. */
31
32 static CORE_ADDR
33 loongarch_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
34 {
35 CORE_ADDR func_addr;
36
37 /* See if we can determine the end of the prologue via the symbol table.
38 If so, then return either PC, or the PC after the prologue, whichever
39 is greater. */
40 if (find_pc_partial_function (pc, nullptr, &func_addr, nullptr))
41 {
42 CORE_ADDR post_prologue_pc
43 = skip_prologue_using_sal (gdbarch, func_addr);
44 if (post_prologue_pc != 0)
45 return std::max (pc, post_prologue_pc);
46 }
47
48 return 0;
49 }
50
51 /* Adjust the address downward (direction of stack growth) so that it
52 is correctly aligned for a new stack frame. */
53
54 static CORE_ADDR
55 loongarch_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
56 {
57 return align_down (addr, 16);
58 }
59
60 /* Generate, or return the cached frame cache for LoongArch frame unwinder. */
61
62 static struct trad_frame_cache *
63 loongarch_frame_cache (struct frame_info *this_frame, void **this_cache)
64 {
65 struct gdbarch *gdbarch = get_frame_arch (this_frame);
66 struct trad_frame_cache *cache;
67 CORE_ADDR pc;
68
69 if (*this_cache != nullptr)
70 return (struct trad_frame_cache *) *this_cache;
71
72 cache = trad_frame_cache_zalloc (this_frame);
73 *this_cache = cache;
74
75 loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
76 trad_frame_set_reg_realreg (cache, gdbarch_pc_regnum (gdbarch), tdep->regs.ra);
77
78 pc = get_frame_address_in_block (this_frame);
79 trad_frame_set_id (cache, frame_id_build_unavailable_stack (pc));
80
81 return cache;
82 }
83
84 /* Implement the this_id callback for LoongArch frame unwinder. */
85
86 static void
87 loongarch_frame_this_id (struct frame_info *this_frame, void **prologue_cache,
88 struct frame_id *this_id)
89 {
90 struct trad_frame_cache *info;
91
92 info = loongarch_frame_cache (this_frame, prologue_cache);
93 trad_frame_get_id (info, this_id);
94 }
95
96 /* Implement the prev_register callback for LoongArch frame unwinder. */
97
98 static struct value *
99 loongarch_frame_prev_register (struct frame_info *this_frame,
100 void **prologue_cache, int regnum)
101 {
102 struct trad_frame_cache *info;
103
104 info = loongarch_frame_cache (this_frame, prologue_cache);
105 return trad_frame_get_register (info, this_frame, regnum);
106 }
107
108 static const struct frame_unwind loongarch_frame_unwind = {
109 "loongarch prologue",
110 /*.type =*/NORMAL_FRAME,
111 /*.stop_reason =*/default_frame_unwind_stop_reason,
112 /*.this_id =*/loongarch_frame_this_id,
113 /*.prev_register =*/loongarch_frame_prev_register,
114 /*.unwind_data =*/nullptr,
115 /*.sniffer =*/default_frame_sniffer,
116 /*.dealloc_cache =*/nullptr,
117 /*.prev_arch =*/nullptr,
118 };
119
120 /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
121
122 static int
123 loongarch_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
124 {
125 loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
126 auto regs = tdep->regs;
127
128 if (0 <= num && num < 32)
129 return regs.r + num;
130 else
131 return -1;
132 }
133
134 static constexpr gdb_byte loongarch_default_breakpoint[] = {0x05, 0x00, 0x2a, 0x00};
135 typedef BP_MANIPULATION (loongarch_default_breakpoint) loongarch_breakpoint;
136
137 /* Extract a set of required target features out of ABFD. If ABFD is nullptr
138 then a LOONGARCH_GDBARCH_FEATURES is returned in its default state. */
139
140 static struct loongarch_gdbarch_features
141 loongarch_features_from_bfd (const bfd *abfd)
142 {
143 struct loongarch_gdbarch_features features;
144
145 /* Now try to improve on the defaults by looking at the binary we are
146 going to execute. We assume the user knows what they are doing and
147 that the target will match the binary. Remember, this code path is
148 only used at all if the target hasn't given us a description, so this
149 is really a last ditched effort to do something sane before giving
150 up. */
151 if (abfd != nullptr && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
152 {
153 unsigned char eclass = elf_elfheader (abfd)->e_ident[EI_CLASS];
154
155 if (eclass == ELFCLASS32)
156 features.xlen = 4;
157 else if (eclass == ELFCLASS64)
158 features.xlen = 8;
159 else
160 internal_error (__FILE__, __LINE__,
161 _("unknown ELF header class %d"), eclass);
162 }
163
164 return features;
165 }
166
167 /* Find a suitable default target description. Use the contents of INFO,
168 specifically the bfd object being executed, to guide the selection of a
169 suitable default target description. */
170
171 static const struct target_desc *
172 loongarch_find_default_target_description (const struct gdbarch_info info)
173 {
174 /* Extract desired feature set from INFO. */
175 struct loongarch_gdbarch_features features
176 = loongarch_features_from_bfd (info.abfd);
177
178 /* If the XLEN field is still 0 then we got nothing useful from INFO.BFD,
179 maybe there was no bfd object. In this case we fall back to a minimal
180 useful target with no floating point, the x-register size is selected
181 based on the architecture from INFO. */
182 if (features.xlen == 0)
183 features.xlen = info.bfd_arch_info->bits_per_address == 32 ? 4 : 8;
184
185 /* Now build a target description based on the feature set. */
186 return loongarch_lookup_target_description (features);
187 }
188
189 /* Initialize the current architecture based on INFO */
190
191 static struct gdbarch *
192 loongarch_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
193 {
194 const struct target_desc *tdesc = info.target_desc;
195
196 /* Ensure we always have a target description. */
197 if (!tdesc_has_registers (tdesc))
198 tdesc = loongarch_find_default_target_description (info);
199
200 const struct tdesc_feature *feature_cpu
201 = tdesc_find_feature (tdesc, "org.gnu.gdb.loongarch.base");
202 if (feature_cpu == nullptr)
203 return nullptr;
204
205 int xlen_bitsize = tdesc_register_bitsize (feature_cpu, "pc");
206 struct loongarch_gdbarch_features features;
207 features.xlen = (xlen_bitsize / 8);
208
209 size_t regnum = 0;
210 tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
211 loongarch_gdbarch_tdep *tdep = new loongarch_gdbarch_tdep;
212 tdep->regs.r = regnum;
213
214 /* Validate the description provides the mandatory base registers
215 and allocate their numbers. */
216 bool valid_p = true;
217 for (int i = 0; i < 32; i++)
218 valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (), regnum++,
219 loongarch_r_normal_name[i] + 1);
220 valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (),
221 tdep->regs.pc = regnum++, "pc");
222 valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (),
223 tdep->regs.badv = regnum++, "badv");
224 if (!valid_p)
225 return nullptr;
226
227 /* LoongArch code is always little-endian. */
228 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
229
230 /* Have a look at what the supplied (if any) bfd object requires of the
231 target, then check that this matches with what the target is
232 providing. */
233 struct loongarch_gdbarch_features abi_features
234 = loongarch_features_from_bfd (info.abfd);
235
236 /* If the ABI_FEATURES xlen is 0 then this indicates we got no useful abi
237 features from the INFO object. In this case we just treat the
238 hardware features as defining the abi. */
239 if (abi_features.xlen == 0)
240 abi_features = features;
241
242 /* Find a candidate among the list of pre-declared architectures. */
243 for (arches = gdbarch_list_lookup_by_info (arches, &info);
244 arches != nullptr;
245 arches = gdbarch_list_lookup_by_info (arches->next, &info))
246 {
247 /* Check that the feature set of the ARCHES matches the feature set
248 we are looking for. If it doesn't then we can't reuse this
249 gdbarch. */
250 loongarch_gdbarch_tdep *candidate_tdep
251 = (loongarch_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
252
253 if (candidate_tdep->abi_features != abi_features)
254 continue;
255
256 break;
257 }
258
259 if (arches != nullptr)
260 return arches->gdbarch;
261
262 /* None found, so create a new architecture from the information provided. */
263 struct gdbarch *gdbarch = gdbarch_alloc (&info, tdep);
264 tdep->abi_features = abi_features;
265
266 /* Target data types. */
267 set_gdbarch_short_bit (gdbarch, 16);
268 set_gdbarch_int_bit (gdbarch, 32);
269 set_gdbarch_long_bit (gdbarch, info.bfd_arch_info->bits_per_address);
270 set_gdbarch_long_long_bit (gdbarch, 64);
271 set_gdbarch_ptr_bit (gdbarch, info.bfd_arch_info->bits_per_address);
272 set_gdbarch_char_signed (gdbarch, 0);
273
274 info.target_desc = tdesc;
275 info.tdesc_data = tdesc_data.get ();
276
277 /* Information about registers. */
278 tdep->regs.ra = tdep->regs.r + 1;
279 tdep->regs.sp = tdep->regs.r + 3;
280 set_gdbarch_num_regs (gdbarch, regnum);
281 set_gdbarch_sp_regnum (gdbarch, tdep->regs.sp);
282 set_gdbarch_pc_regnum (gdbarch, tdep->regs.pc);
283
284 /* Finalise the target description registers. */
285 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
286
287 /* Advance PC across function entry code. */
288 set_gdbarch_skip_prologue (gdbarch, loongarch_skip_prologue);
289
290 /* Stack grows downward. */
291 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
292
293 /* Frame info. */
294 set_gdbarch_frame_align (gdbarch, loongarch_frame_align);
295
296 /* Breakpoint manipulation. */
297 set_gdbarch_breakpoint_kind_from_pc (gdbarch, loongarch_breakpoint::kind_from_pc);
298 set_gdbarch_sw_breakpoint_from_kind (gdbarch, loongarch_breakpoint::bp_from_kind);
299
300 /* Frame unwinders. Use DWARF debug info if available, otherwise use our own unwinder. */
301 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, loongarch_dwarf2_reg_to_regnum);
302 dwarf2_append_unwinders (gdbarch);
303 frame_unwind_append_unwinder (gdbarch, &loongarch_frame_unwind);
304
305 /* Hook in OS ABI-specific overrides, if they have been registered. */
306 gdbarch_init_osabi (info, gdbarch);
307
308 return gdbarch;
309 }
310
311 void _initialize_loongarch_tdep ();
312 void
313 _initialize_loongarch_tdep ()
314 {
315 gdbarch_register (bfd_arch_loongarch, loongarch_gdbarch_init, nullptr);
316 }