Accept and ignore the R_BPF_64_NODLYD32 relocation.
[binutils-gdb.git] / gdb / cp-abi.c
blob538d314efda589ffc87c9d40b99f24b1b7c1ff9b
1 /* Generic code for supporting multiple C++ ABI's
3 Copyright (C) 2001-2023 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"
21 #include "language.h"
22 #include "value.h"
23 #include "cp-abi.h"
24 #include "command.h"
25 #include "gdbcmd.h"
26 #include "ui-out.h"
27 static struct cp_abi_ops *find_cp_abi (const char *short_name);
29 static struct cp_abi_ops current_cp_abi = { "", NULL };
30 static struct cp_abi_ops auto_cp_abi = { "auto", NULL };
32 #define CP_ABI_MAX 8
33 static struct cp_abi_ops *cp_abis[CP_ABI_MAX];
34 static int num_cp_abis = 0;
36 enum ctor_kinds
37 is_constructor_name (const char *name)
39 if ((current_cp_abi.is_constructor_name) == NULL)
40 error (_("ABI doesn't define required function is_constructor_name"));
41 return (*current_cp_abi.is_constructor_name) (name);
44 enum dtor_kinds
45 is_destructor_name (const char *name)
47 if ((current_cp_abi.is_destructor_name) == NULL)
48 error (_("ABI doesn't define required function is_destructor_name"));
49 return (*current_cp_abi.is_destructor_name) (name);
52 int
53 is_vtable_name (const char *name)
55 if ((current_cp_abi.is_vtable_name) == NULL)
56 error (_("ABI doesn't define required function is_vtable_name"));
57 return (*current_cp_abi.is_vtable_name) (name);
60 int
61 is_operator_name (const char *name)
63 if ((current_cp_abi.is_operator_name) == NULL)
64 error (_("ABI doesn't define required function is_operator_name"));
65 return (*current_cp_abi.is_operator_name) (name);
68 int
69 baseclass_offset (struct type *type, int index, const gdb_byte *valaddr,
70 LONGEST embedded_offset, CORE_ADDR address,
71 const struct value *val)
73 int res = 0;
75 gdb_assert (current_cp_abi.baseclass_offset != NULL);
77 try
79 res = (*current_cp_abi.baseclass_offset) (type, index, valaddr,
80 embedded_offset,
81 address, val);
83 catch (const gdb_exception_error &ex)
85 if (ex.error != NOT_AVAILABLE_ERROR)
86 throw;
88 throw_error (NOT_AVAILABLE_ERROR,
89 _("Cannot determine virtual baseclass offset "
90 "of incomplete object"));
93 return res;
96 struct value *
97 value_virtual_fn_field (struct value **arg1p,
98 struct fn_field *f, int j,
99 struct type *type, int offset)
101 if ((current_cp_abi.virtual_fn_field) == NULL)
102 return NULL;
103 return (*current_cp_abi.virtual_fn_field) (arg1p, f, j,
104 type, offset);
107 struct type *
108 value_rtti_type (struct value *v, int *full,
109 LONGEST *top, int *using_enc)
111 struct type *ret = NULL;
113 if ((current_cp_abi.rtti_type) == NULL
114 || !HAVE_CPLUS_STRUCT (check_typedef (v->type ())))
115 return NULL;
118 ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc);
120 catch (const gdb_exception_error &e)
122 return NULL;
125 return ret;
128 void
129 cplus_print_method_ptr (const gdb_byte *contents,
130 struct type *type,
131 struct ui_file *stream)
133 if (current_cp_abi.print_method_ptr == NULL)
134 error (_("GDB does not support pointers to methods on this target"));
135 (*current_cp_abi.print_method_ptr) (contents, type, stream);
139 cplus_method_ptr_size (struct type *to_type)
141 if (current_cp_abi.method_ptr_size == NULL)
142 error (_("GDB does not support pointers to methods on this target"));
143 return (*current_cp_abi.method_ptr_size) (to_type);
146 void
147 cplus_make_method_ptr (struct type *type, gdb_byte *contents,
148 CORE_ADDR value, int is_virtual)
150 if (current_cp_abi.make_method_ptr == NULL)
151 error (_("GDB does not support pointers to methods on this target"));
152 (*current_cp_abi.make_method_ptr) (type, contents, value, is_virtual);
155 CORE_ADDR
156 cplus_skip_trampoline (frame_info_ptr frame,
157 CORE_ADDR stop_pc)
159 if (current_cp_abi.skip_trampoline == NULL)
160 return 0;
161 return (*current_cp_abi.skip_trampoline) (frame, stop_pc);
164 struct value *
165 cplus_method_ptr_to_value (struct value **this_p,
166 struct value *method_ptr)
168 if (current_cp_abi.method_ptr_to_value == NULL)
169 error (_("GDB does not support pointers to methods on this target"));
170 return (*current_cp_abi.method_ptr_to_value) (this_p, method_ptr);
173 /* See cp-abi.h. */
175 void
176 cplus_print_vtable (struct value *value)
178 if (current_cp_abi.print_vtable == NULL)
179 error (_("GDB cannot print the vtable on this target"));
180 (*current_cp_abi.print_vtable) (value);
183 /* See cp-abi.h. */
185 struct value *
186 cplus_typeid (struct value *value)
188 if (current_cp_abi.get_typeid == NULL)
189 error (_("GDB cannot find the typeid on this target"));
190 return (*current_cp_abi.get_typeid) (value);
193 /* See cp-abi.h. */
195 struct type *
196 cplus_typeid_type (struct gdbarch *gdbarch)
198 if (current_cp_abi.get_typeid_type == NULL)
199 error (_("GDB cannot find the type for 'typeid' on this target"));
200 return (*current_cp_abi.get_typeid_type) (gdbarch);
203 /* See cp-abi.h. */
205 struct type *
206 cplus_type_from_type_info (struct value *value)
208 if (current_cp_abi.get_type_from_type_info == NULL)
209 error (_("GDB cannot find the type from a std::type_info on this target"));
210 return (*current_cp_abi.get_type_from_type_info) (value);
213 /* See cp-abi.h. */
215 std::string
216 cplus_typename_from_type_info (struct value *value)
218 if (current_cp_abi.get_typename_from_type_info == NULL)
219 error (_("GDB cannot find the type name "
220 "from a std::type_info on this target"));
221 return (*current_cp_abi.get_typename_from_type_info) (value);
224 /* See cp-abi.h. */
226 struct language_pass_by_ref_info
227 cp_pass_by_reference (struct type *type)
229 if ((current_cp_abi.pass_by_reference) == NULL)
230 return {};
231 return (*current_cp_abi.pass_by_reference) (type);
234 /* Set the current C++ ABI to SHORT_NAME. */
236 static int
237 switch_to_cp_abi (const char *short_name)
239 struct cp_abi_ops *abi;
241 abi = find_cp_abi (short_name);
242 if (abi == NULL)
243 return 0;
245 current_cp_abi = *abi;
246 return 1;
249 /* Add ABI to the list of supported C++ ABI's. */
252 register_cp_abi (struct cp_abi_ops *abi)
254 if (num_cp_abis == CP_ABI_MAX)
255 internal_error (_("Too many C++ ABIs, please increase "
256 "CP_ABI_MAX in cp-abi.c"));
258 cp_abis[num_cp_abis++] = abi;
260 return 1;
263 /* Set the ABI to use in "auto" mode to SHORT_NAME. */
265 void
266 set_cp_abi_as_auto_default (const char *short_name)
268 struct cp_abi_ops *abi = find_cp_abi (short_name);
270 if (abi == NULL)
271 internal_error (_("Cannot find C++ ABI \"%s\" to set it as auto default."),
272 short_name);
274 xfree ((char *) auto_cp_abi.longname);
275 xfree ((char *) auto_cp_abi.doc);
277 auto_cp_abi = *abi;
279 auto_cp_abi.shortname = "auto";
280 auto_cp_abi.longname = xstrprintf ("currently \"%s\"",
281 abi->shortname).release ();
282 auto_cp_abi.doc = xstrprintf ("Automatically selected; currently \"%s\"",
283 abi->shortname).release ();
285 /* Since we copy the current ABI into current_cp_abi instead of
286 using a pointer, if auto is currently the default, we need to
287 reset it. */
288 if (strcmp (current_cp_abi.shortname, "auto") == 0)
289 switch_to_cp_abi ("auto");
292 /* Return the ABI operations associated with SHORT_NAME. */
294 static struct cp_abi_ops *
295 find_cp_abi (const char *short_name)
297 int i;
299 for (i = 0; i < num_cp_abis; i++)
300 if (strcmp (cp_abis[i]->shortname, short_name) == 0)
301 return cp_abis[i];
303 return NULL;
306 /* Display the list of registered C++ ABIs. */
308 static void
309 list_cp_abis (int from_tty)
311 struct ui_out *uiout = current_uiout;
312 int i;
314 uiout->text ("The available C++ ABIs are:\n");
315 ui_out_emit_tuple tuple_emitter (uiout, "cp-abi-list");
316 for (i = 0; i < num_cp_abis; i++)
318 char pad[14];
319 int padcount;
321 uiout->text (" ");
322 uiout->field_string ("cp-abi", cp_abis[i]->shortname);
324 padcount = 16 - 2 - strlen (cp_abis[i]->shortname);
325 pad[padcount] = 0;
326 while (padcount > 0)
327 pad[--padcount] = ' ';
328 uiout->text (pad);
330 uiout->field_string ("doc", cp_abis[i]->doc);
331 uiout->text ("\n");
335 /* Set the current C++ ABI, or display the list of options if no
336 argument is given. */
338 static void
339 set_cp_abi_cmd (const char *args, int from_tty)
341 if (args == NULL)
343 list_cp_abis (from_tty);
344 return;
347 if (!switch_to_cp_abi (args))
348 error (_("Could not find \"%s\" in ABI list"), args);
351 /* A completion function for "set cp-abi". */
353 static void
354 cp_abi_completer (struct cmd_list_element *ignore,
355 completion_tracker &tracker,
356 const char *text, const char *word)
358 static const char **cp_abi_names;
360 if (cp_abi_names == NULL)
362 int i;
364 cp_abi_names = XNEWVEC (const char *, num_cp_abis + 1);
365 for (i = 0; i < num_cp_abis; ++i)
366 cp_abi_names[i] = cp_abis[i]->shortname;
367 cp_abi_names[i] = NULL;
370 complete_on_enum (tracker, cp_abi_names, text, word);
373 /* Show the currently selected C++ ABI. */
375 static void
376 show_cp_abi_cmd (const char *args, int from_tty)
378 struct ui_out *uiout = current_uiout;
380 uiout->text ("The currently selected C++ ABI is \"");
382 uiout->field_string ("cp-abi", current_cp_abi.shortname);
383 uiout->text ("\" (");
384 uiout->field_string ("longname", current_cp_abi.longname);
385 uiout->text (").\n");
388 void _initialize_cp_abi ();
389 void
390 _initialize_cp_abi ()
392 struct cmd_list_element *c;
394 register_cp_abi (&auto_cp_abi);
395 switch_to_cp_abi ("auto");
397 c = add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd, _("\
398 Set the ABI used for inspecting C++ objects.\n\
399 \"set cp-abi\" with no arguments will list the available ABIs."),
400 &setlist);
401 set_cmd_completer (c, cp_abi_completer);
403 add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd,
404 _("Show the ABI used for inspecting C++ objects."),
405 &showlist);