]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gnu-v3-abi.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / gnu-v3-abi.c
CommitLineData
7ed49443
JB
1/* Abstraction of GNU v3 abi.
2 Contributed by Jim Blandy <jimb@redhat.com>
451fbdda 3
1d506c26 4 Copyright (C) 2001-2024 Free Software Foundation, Inc.
7ed49443
JB
5
6 This file is part of GDB.
7
a9762ec7
JB
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
7ed49443
JB
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
7ed49443 20
ec452525 21#include "extract-store-integer.h"
83b6e1f1 22#include "language.h"
7ed49443
JB
23#include "value.h"
24#include "cp-abi.h"
362ff856 25#include "cp-support.h"
7ed49443 26#include "demangle.h"
62bf63d7 27#include "dwarf2.h"
b18be20d 28#include "objfiles.h"
0d5de010 29#include "valprint.h"
94af9270 30#include "c-lang.h"
79d43c61 31#include "typeprint.h"
59d3651b 32#include <algorithm>
7f6aba03 33#include "cli/cli-style.h"
7d79de9a 34#include "dwarf2/loc.h"
328d42d8 35#include "inferior.h"
0d5de010 36
b27b8843 37static struct cp_abi_ops gnu_v3_abi_ops;
7ed49443 38
6e72ca20
TT
39/* A gdbarch key for std::type_info, in the event that it can't be
40 found in the debug info. */
41
cb275538 42static const registry<gdbarch>::key<struct type> std_type_info_gdbarch_data;
6e72ca20
TT
43
44
7ed49443
JB
45static int
46gnuv3_is_vtable_name (const char *name)
47{
61012eef 48 return startswith (name, "_ZTV");
7ed49443
JB
49}
50
51static int
52gnuv3_is_operator_name (const char *name)
53{
8090b426 54 return startswith (name, CP_OPERATOR_STR);
7ed49443
JB
55}
56
57
58/* To help us find the components of a vtable, we build ourselves a
59 GDB type object representing the vtable structure. Following the
60 V3 ABI, it goes something like this:
61
62 struct gdb_gnu_v3_abi_vtable {
63
64 / * An array of virtual call and virtual base offsets. The real
dda83cd7
SM
65 length of this array depends on the class hierarchy; we use
66 negative subscripts to access the elements. Yucky, but
67 better than the alternatives. * /
7ed49443
JB
68 ptrdiff_t vcall_and_vbase_offsets[0];
69
70 / * The offset from a virtual pointer referring to this table
dda83cd7 71 to the top of the complete object. * /
7ed49443
JB
72 ptrdiff_t offset_to_top;
73
74 / * The type_info pointer for this class. This is really a
dda83cd7
SM
75 std::type_info *, but GDB doesn't really look at the
76 type_info object itself, so we don't bother to get the type
77 exactly right. * /
7ed49443
JB
78 void *type_info;
79
80 / * Virtual table pointers in objects point here. * /
81
82 / * Virtual function pointers. Like the vcall/vbase array, the
dda83cd7 83 real length of this table depends on the class hierarchy. * /
7ed49443
JB
84 void (*virtual_functions[0]) ();
85
86 };
87
88 The catch, of course, is that the exact layout of this table
89 depends on the ABI --- word size, endianness, alignment, etc. So
90 the GDB type object is actually a per-architecture kind of thing.
91
92 vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
93 which refers to the struct type * for this structure, laid out
94 appropriately for the architecture. */
cb275538 95static const registry<gdbarch>::key<struct type> vtable_type_gdbarch_data;
7ed49443
JB
96
97
98/* Human-readable names for the numbers of the fields above. */
99enum {
100 vtable_field_vcall_and_vbase_offsets,
101 vtable_field_offset_to_top,
102 vtable_field_type_info,
103 vtable_field_virtual_functions
104};
105
106
107/* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
108 described above, laid out appropriately for ARCH.
109
110 We use this function as the gdbarch per-architecture data
9970f04b 111 initialization function. */
cb275538
TT
112static struct type *
113get_gdb_vtable_type (struct gdbarch *arch)
7ed49443
JB
114{
115 struct type *t;
7ed49443
JB
116 int offset;
117
cb275538
TT
118 struct type *result = vtable_type_gdbarch_data.get (arch);
119 if (result != nullptr)
120 return result;
121
7ed49443 122 struct type *void_ptr_type
fde6c819 123 = builtin_type (arch)->builtin_data_ptr;
7ed49443 124 struct type *ptr_to_void_fn_type
fde6c819 125 = builtin_type (arch)->builtin_func_ptr;
7ed49443 126
cc495054
TT
127 type_allocator alloc (arch);
128
7ed49443
JB
129 /* ARCH can't give us the true ptrdiff_t type, so we guess. */
130 struct type *ptrdiff_type
2d39ccd3 131 = init_integer_type (alloc, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
7ed49443 132
2774f2da
TV
133 t = alloc.new_type (TYPE_CODE_STRUCT, 0, nullptr);
134
7ed49443
JB
135 /* We assume no padding is necessary, since GDB doesn't know
136 anything about alignment at the moment. If this assumption bites
137 us, we should add a gdbarch method which, given a type, returns
138 the alignment that type requires, and then use that here. */
139
140 /* Build the field list. */
2774f2da
TV
141 t->alloc_fields (4);
142
7ed49443
JB
143 offset = 0;
144
145 /* ptrdiff_t vcall_and_vbase_offsets[0]; */
2774f2da
TV
146 {
147 struct field &field0 = t->field (0);
148 field0.set_name ("vcall_and_vbase_offsets");
149 field0.set_type (lookup_array_range_type (ptrdiff_type, 0, -1));
150 field0.set_loc_bitpos (offset * TARGET_CHAR_BIT);
151 offset += field0.type ()->length ();
152 }
7ed49443
JB
153
154 /* ptrdiff_t offset_to_top; */
2774f2da
TV
155 {
156 struct field &field1 = t->field (1);
157 field1.set_name ("offset_to_top");
158 field1.set_type (ptrdiff_type);
159 field1.set_loc_bitpos (offset * TARGET_CHAR_BIT);
160 offset += field1.type ()->length ();
161 }
7ed49443
JB
162
163 /* void *type_info; */
2774f2da
TV
164 {
165 struct field &field2 = t->field (2);
166 field2.set_name ("type_info");
167 field2.set_type (void_ptr_type);
168 field2.set_loc_bitpos (offset * TARGET_CHAR_BIT);
169 offset += field2.type ()->length ();
170 }
7ed49443
JB
171
172 /* void (*virtual_functions[0]) (); */
2774f2da
TV
173 {
174 struct field &field3 = t->field (3);
175 field3.set_name ("virtual_functions");
176 field3.set_type (lookup_array_range_type (ptr_to_void_fn_type, 0, -1));
177 field3.set_loc_bitpos (offset * TARGET_CHAR_BIT);
178 offset += field3.type ()->length ();
179 }
180
181 t->set_length (offset);
182
d0e39ea2 183 t->set_name ("gdb_gnu_v3_abi_vtable");
e9bb382b 184 INIT_CPLUS_SPECIFIC (t);
7ed49443 185
cb275538
TT
186 result = make_type_with_address_space (t, TYPE_INSTANCE_FLAG_CODE_SPACE);
187 vtable_type_gdbarch_data.set (arch, result);
188 return result;
7ed49443
JB
189}
190
191
ed09d7da
KB
192/* Return the ptrdiff_t type used in the vtable type. */
193static struct type *
194vtable_ptrdiff_type (struct gdbarch *gdbarch)
195{
cb275538 196 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
ed09d7da
KB
197
198 /* The "offset_to_top" field has the appropriate (ptrdiff_t) type. */
940da03e 199 return vtable_type->field (vtable_field_offset_to_top).type ();
ed09d7da
KB
200}
201
7ed49443
JB
202/* Return the offset from the start of the imaginary `struct
203 gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
204 (i.e., where objects' virtual table pointers point). */
205static int
ad4820ab 206vtable_address_point_offset (struct gdbarch *gdbarch)
7ed49443 207{
cb275538 208 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
7ed49443 209
b610c045 210 return (vtable_type->field (vtable_field_virtual_functions).loc_bitpos ()
dda83cd7 211 / TARGET_CHAR_BIT);
7ed49443
JB
212}
213
214
d48cc9dd
DJ
215/* Determine whether structure TYPE is a dynamic class. Cache the
216 result. */
217
218static int
219gnuv3_dynamic_class (struct type *type)
220{
221 int fieldnum, fieldelem;
222
f168693b 223 type = check_typedef (type);
78134374
SM
224 gdb_assert (type->code () == TYPE_CODE_STRUCT
225 || type->code () == TYPE_CODE_UNION);
5f4ce105 226
78134374 227 if (type->code () == TYPE_CODE_UNION)
5f4ce105
DE
228 return 0;
229
d48cc9dd
DJ
230 if (TYPE_CPLUS_DYNAMIC (type))
231 return TYPE_CPLUS_DYNAMIC (type) == 1;
232
233 ALLOCATE_CPLUS_STRUCT_TYPE (type);
234
235 for (fieldnum = 0; fieldnum < TYPE_N_BASECLASSES (type); fieldnum++)
236 if (BASETYPE_VIA_VIRTUAL (type, fieldnum)
940da03e 237 || gnuv3_dynamic_class (type->field (fieldnum).type ()))
d48cc9dd
DJ
238 {
239 TYPE_CPLUS_DYNAMIC (type) = 1;
240 return 1;
241 }
242
243 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
244 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
245 fieldelem++)
246 {
247 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, fieldnum);
248
249 if (TYPE_FN_FIELD_VIRTUAL_P (f, fieldelem))
250 {
251 TYPE_CPLUS_DYNAMIC (type) = 1;
252 return 1;
253 }
254 }
255
256 TYPE_CPLUS_DYNAMIC (type) = -1;
257 return 0;
258}
259
260/* Find the vtable for a value of CONTAINER_TYPE located at
261 CONTAINER_ADDR. Return a value of the correct vtable type for this
262 architecture, or NULL if CONTAINER does not have a vtable. */
263
264static struct value *
265gnuv3_get_vtable (struct gdbarch *gdbarch,
266 struct type *container_type, CORE_ADDR container_addr)
267{
cb275538 268 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
d48cc9dd
DJ
269 struct type *vtable_pointer_type;
270 struct value *vtable_pointer;
271 CORE_ADDR vtable_address;
272
f168693b 273 container_type = check_typedef (container_type);
78134374 274 gdb_assert (container_type->code () == TYPE_CODE_STRUCT);
5f4ce105 275
d48cc9dd
DJ
276 /* If this type does not have a virtual table, don't read the first
277 field. */
5f4ce105 278 if (!gnuv3_dynamic_class (container_type))
d48cc9dd
DJ
279 return NULL;
280
281 /* We do not consult the debug information to find the virtual table.
282 The ABI specifies that it is always at offset zero in any class,
283 and debug information may not represent it.
284
285 We avoid using value_contents on principle, because the object might
286 be large. */
287
288 /* Find the type "pointer to virtual table". */
289 vtable_pointer_type = lookup_pointer_type (vtable_type);
290
291 /* Load it from the start of the class. */
292 vtable_pointer = value_at (vtable_pointer_type, container_addr);
293 vtable_address = value_as_address (vtable_pointer);
294
295 /* Correct it to point at the start of the virtual table, rather
296 than the address point. */
297 return value_at_lazy (vtable_type,
0963b4bd
MS
298 vtable_address
299 - vtable_address_point_offset (gdbarch));
d48cc9dd
DJ
300}
301
302
7ed49443
JB
303static struct type *
304gnuv3_rtti_type (struct value *value,
dda83cd7 305 int *full_p, LONGEST *top_p, int *using_enc_p)
7ed49443 306{
ad4820ab 307 struct gdbarch *gdbarch;
d0c97917 308 struct type *values_type = check_typedef (value->type ());
7ed49443
JB
309 struct value *vtable;
310 struct minimal_symbol *vtable_symbol;
311 const char *vtable_symbol_name;
312 const char *class_name;
7ed49443
JB
313 struct type *run_time_type;
314 LONGEST offset_to_top;
e6a959d6 315 const char *atsign;
7ed49443 316
e95a97d4 317 /* We only have RTTI for dynamic class objects. */
78134374 318 if (values_type->code () != TYPE_CODE_STRUCT
e95a97d4 319 || !gnuv3_dynamic_class (values_type))
7ed49443
JB
320 return NULL;
321
ad4820ab 322 /* Determine architecture. */
8ee511af 323 gdbarch = values_type->arch ();
7ed49443 324
21cfb3b6
DJ
325 if (using_enc_p)
326 *using_enc_p = 0;
327
5f4ce105 328 vtable = gnuv3_get_vtable (gdbarch, values_type,
d48cc9dd
DJ
329 value_as_address (value_addr (value)));
330 if (vtable == NULL)
331 return NULL;
332
7ed49443
JB
333 /* Find the linker symbol for this vtable. */
334 vtable_symbol
9feb2d07 335 = lookup_minimal_symbol_by_pc (vtable->address ()
391f8628 336 + vtable->embedded_offset ()).minsym;
7ed49443
JB
337 if (! vtable_symbol)
338 return NULL;
339
340 /* The symbol's demangled name should be something like "vtable for
341 CLASS", where CLASS is the name of the run-time type of VALUE.
342 If we didn't like this approach, we could instead look in the
343 type_info object itself to get the class name. But this way
344 should work just as well, and doesn't read target memory. */
c9d95fa3 345 vtable_symbol_name = vtable_symbol->demangled_name ();
98081e55 346 if (vtable_symbol_name == NULL
61012eef 347 || !startswith (vtable_symbol_name, "vtable for "))
f773fdbb 348 {
8a3fe4f8 349 warning (_("can't find linker symbol for virtual table for `%s' value"),
0a07729b 350 TYPE_SAFE_NAME (values_type));
f773fdbb 351 if (vtable_symbol_name)
8a3fe4f8 352 warning (_(" found `%s' instead"), vtable_symbol_name);
f773fdbb
JM
353 return NULL;
354 }
7ed49443
JB
355 class_name = vtable_symbol_name + 11;
356
8de20a37
TT
357 /* Strip off @plt and version suffixes. */
358 atsign = strchr (class_name, '@');
359 if (atsign != NULL)
360 {
361 char *copy;
362
224c3ddb 363 copy = (char *) alloca (atsign - class_name + 1);
8de20a37
TT
364 memcpy (copy, class_name, atsign - class_name);
365 copy[atsign - class_name] = '\0';
366 class_name = copy;
367 }
368
7ed49443 369 /* Try to look up the class name as a type name. */
0963b4bd 370 /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
362ff856
MC
371 run_time_type = cp_lookup_rtti_type (class_name, NULL);
372 if (run_time_type == NULL)
373 return NULL;
7ed49443
JB
374
375 /* Get the offset from VALUE to the top of the complete object.
376 NOTE: this is the reverse of the meaning of *TOP_P. */
377 offset_to_top
378 = value_as_long (value_field (vtable, vtable_field_offset_to_top));
379
380 if (full_p)
391f8628 381 *full_p = (- offset_to_top == value->embedded_offset ()
463b870d 382 && (value->enclosing_type ()->length ()
df86565b 383 >= run_time_type->length ()));
7ed49443
JB
384 if (top_p)
385 *top_p = - offset_to_top;
7ed49443
JB
386 return run_time_type;
387}
388
0d5de010
DJ
389/* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
390 function, of type FNTYPE. */
7ed49443 391
0d5de010 392static struct value *
ad4820ab
UW
393gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
394 struct type *fntype, int vtable_index)
0d5de010 395{
d48cc9dd
DJ
396 struct value *vtable, *vfn;
397
398 /* Every class with virtual functions must have a vtable. */
d0c97917 399 vtable = gnuv3_get_vtable (gdbarch, container->type (),
d48cc9dd
DJ
400 value_as_address (value_addr (container)));
401 gdb_assert (vtable != NULL);
7ed49443
JB
402
403 /* Fetch the appropriate function pointer from the vtable. */
404 vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
dda83cd7 405 vtable_index);
7ed49443 406
0d5de010
DJ
407 /* If this architecture uses function descriptors directly in the vtable,
408 then the address of the vtable entry is actually a "function pointer"
409 (i.e. points to the descriptor). We don't need to scale the index
85102364 410 by the size of a function descriptor; GCC does that before outputting
0d5de010 411 debug information. */
ad4820ab 412 if (gdbarch_vtable_function_descriptors (gdbarch))
0d5de010 413 vfn = value_addr (vfn);
7ed49443 414
0d5de010
DJ
415 /* Cast the function pointer to the appropriate type. */
416 vfn = value_cast (lookup_pointer_type (fntype), vfn);
76b79d6e 417
7ed49443
JB
418 return vfn;
419}
420
0d5de010
DJ
421/* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h
422 for a description of the arguments. */
423
424static struct value *
425gnuv3_virtual_fn_field (struct value **value_p,
dda83cd7 426 struct fn_field *f, int j,
0d5de010
DJ
427 struct type *vfn_base, int offset)
428{
d0c97917 429 struct type *values_type = check_typedef ((*value_p)->type ());
ad4820ab 430 struct gdbarch *gdbarch;
0d5de010
DJ
431
432 /* Some simple sanity checks. */
78134374 433 if (values_type->code () != TYPE_CODE_STRUCT)
0d5de010
DJ
434 error (_("Only classes can have virtual functions."));
435
ad4820ab 436 /* Determine architecture. */
8ee511af 437 gdbarch = values_type->arch ();
ad4820ab 438
0d5de010
DJ
439 /* Cast our value to the base class which defines this virtual
440 function. This takes care of any necessary `this'
441 adjustments. */
442 if (vfn_base != values_type)
443 *value_p = value_cast (vfn_base, *value_p);
444
ad4820ab 445 return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
0d5de010
DJ
446 TYPE_FN_FIELD_VOFFSET (f, j));
447}
448
1514d34e
DJ
449/* Compute the offset of the baseclass which is
450 the INDEXth baseclass of class TYPE,
451 for value at VALADDR (in host) at ADDRESS (in target).
452 The result is the offset of the baseclass value relative
453 to (the address of)(ARG) + OFFSET.
454
0963b4bd
MS
455 -1 is returned on error. */
456
b9362cc7 457static int
8af8e3bc 458gnuv3_baseclass_offset (struct type *type, int index,
6b850546 459 const bfd_byte *valaddr, LONGEST embedded_offset,
8af8e3bc 460 CORE_ADDR address, const struct value *val)
1514d34e 461{
ad4820ab 462 struct gdbarch *gdbarch;
ad4820ab 463 struct type *ptr_type;
79d5b63a 464 struct value *vtable;
2497b498 465 struct value *vbase_array;
1514d34e 466 long int cur_base_offset, base_offset;
1514d34e 467
ad4820ab 468 /* Determine architecture. */
8ee511af 469 gdbarch = type->arch ();
ad4820ab
UW
470 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
471
1514d34e 472 /* If it isn't a virtual base, this is easy. The offset is in the
9c37b5ae
TT
473 type definition. */
474 if (!BASETYPE_VIA_VIRTUAL (type, index))
1514d34e
DJ
475 return TYPE_BASECLASS_BITPOS (type, index) / 8;
476
7d79de9a 477 /* If we have a DWARF expression for the offset, evaluate it. */
2ad53ea1 478 if (type->field (index).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
7d79de9a
TT
479 {
480 struct dwarf2_property_baton baton;
481 baton.property_type
940da03e 482 = lookup_pointer_type (type->field (index).type ());
51e36a3a 483 baton.locexpr = *type->field (index).loc_dwarf_block ();
7d79de9a
TT
484
485 struct dynamic_prop prop;
8c2e4e06 486 prop.set_locexpr (&baton);
7d79de9a
TT
487
488 struct property_addr_info addr_stack;
489 addr_stack.type = type;
490 /* Note that we don't set "valaddr" here. Doing so causes
491 regressions. FIXME. */
492 addr_stack.addr = address + embedded_offset;
493 addr_stack.next = nullptr;
494
495 CORE_ADDR result;
496 if (dwarf2_evaluate_property (&prop, nullptr, &addr_stack, &result,
1fb43cf7 497 {addr_stack.addr}))
7d79de9a
TT
498 return (int) (result - addr_stack.addr);
499 }
500
1514d34e
DJ
501 /* To access a virtual base, we need to use the vbase offset stored in
502 our vtable. Recent GCC versions provide this information. If it isn't
503 available, we could get what we needed from RTTI, or from drawing the
504 complete inheritance graph based on the debug info. Neither is
505 worthwhile. */
506 cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
ad4820ab 507 if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
8a3fe4f8 508 error (_("Expected a negative vbase offset (old compiler?)"));
1514d34e 509
ad4820ab 510 cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
df86565b 511 if ((- cur_base_offset) % ptr_type->length () != 0)
8a3fe4f8 512 error (_("Misaligned vbase offset."));
df86565b 513 cur_base_offset = cur_base_offset / ((int) ptr_type->length ());
1514d34e 514
8af8e3bc 515 vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset);
d48cc9dd 516 gdb_assert (vtable != NULL);
1514d34e 517 vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
2497b498 518 base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
1514d34e
DJ
519 return base_offset;
520}
7ed49443 521
0d5de010
DJ
522/* Locate a virtual method in DOMAIN or its non-virtual base classes
523 which has virtual table index VOFFSET. The method has an associated
524 "this" adjustment of ADJUSTMENT bytes. */
525
2c0b251b 526static const char *
0d5de010
DJ
527gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
528 LONGEST adjustment)
529{
530 int i;
0d5de010
DJ
531
532 /* Search this class first. */
0d5de010
DJ
533 if (adjustment == 0)
534 {
535 int len;
536
537 len = TYPE_NFN_FIELDS (domain);
538 for (i = 0; i < len; i++)
539 {
540 int len2, j;
541 struct fn_field *f;
542
543 f = TYPE_FN_FIELDLIST1 (domain, i);
544 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
545
546 check_stub_method_group (domain, i);
547 for (j = 0; j < len2; j++)
548 if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset)
549 return TYPE_FN_FIELD_PHYSNAME (f, j);
550 }
551 }
552
553 /* Next search non-virtual bases. If it's in a virtual base,
554 we're out of luck. */
555 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
556 {
557 int pos;
558 struct type *basetype;
559
560 if (BASETYPE_VIA_VIRTUAL (domain, i))
561 continue;
562
563 pos = TYPE_BASECLASS_BITPOS (domain, i) / 8;
940da03e 564 basetype = domain->field (i).type ();
0d5de010
DJ
565 /* Recurse with a modified adjustment. We don't need to adjust
566 voffset. */
df86565b 567 if (adjustment >= pos && adjustment < pos + basetype->length ())
0d5de010
DJ
568 return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
569 }
570
571 return NULL;
572}
573
fead6908
UW
574/* Decode GNU v3 method pointer. */
575
576static int
ad4820ab
UW
577gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
578 const gdb_byte *contents,
fead6908
UW
579 CORE_ADDR *value_p,
580 LONGEST *adjustment_p)
581{
ad4820ab 582 struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
ed09d7da 583 struct type *offset_type = vtable_ptrdiff_type (gdbarch);
e17a4113 584 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
fead6908
UW
585 CORE_ADDR ptr_value;
586 LONGEST voffset, adjustment;
587 int vbit;
588
589 /* Extract the pointer to member. The first element is either a pointer
590 or a vtable offset. For pointers, we need to use extract_typed_address
591 to allow the back-end to convert the pointer to a GDB address -- but
592 vtable offsets we must handle as integers. At this point, we do not
593 yet know which case we have, so we extract the value under both
594 interpretations and choose the right one later on. */
595 ptr_value = extract_typed_address (contents, funcptr_type);
e17a4113 596 voffset = extract_signed_integer (contents,
df86565b
SM
597 funcptr_type->length (), byte_order);
598 contents += funcptr_type->length ();
e17a4113 599 adjustment = extract_signed_integer (contents,
df86565b 600 offset_type->length (), byte_order);
fead6908 601
ad4820ab 602 if (!gdbarch_vbit_in_delta (gdbarch))
fead6908
UW
603 {
604 vbit = voffset & 1;
605 voffset = voffset ^ vbit;
606 }
607 else
608 {
609 vbit = adjustment & 1;
610 adjustment = adjustment >> 1;
611 }
612
613 *value_p = vbit? voffset : ptr_value;
614 *adjustment_p = adjustment;
615 return vbit;
616}
617
0d5de010
DJ
618/* GNU v3 implementation of cplus_print_method_ptr. */
619
620static void
621gnuv3_print_method_ptr (const gdb_byte *contents,
622 struct type *type,
623 struct ui_file *stream)
624{
09e2d7c7 625 struct type *self_type = TYPE_SELF_TYPE (type);
8ee511af 626 struct gdbarch *gdbarch = self_type->arch ();
0d5de010
DJ
627 CORE_ADDR ptr_value;
628 LONGEST adjustment;
0d5de010
DJ
629 int vbit;
630
0d5de010 631 /* Extract the pointer to member. */
ad4820ab 632 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
0d5de010
DJ
633
634 /* Check for NULL. */
635 if (ptr_value == 0 && vbit == 0)
636 {
6cb06a8c 637 gdb_printf (stream, "NULL");
0d5de010
DJ
638 return;
639 }
640
641 /* Search for a virtual method. */
642 if (vbit)
643 {
644 CORE_ADDR voffset;
645 const char *physname;
646
647 /* It's a virtual table offset, maybe in this class. Search
648 for a field with the correct vtable offset. First convert it
649 to an index, as used in TYPE_FN_FIELD_VOFFSET. */
df86565b 650 voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
0d5de010 651
09e2d7c7 652 physname = gnuv3_find_method_in (self_type, voffset, adjustment);
0d5de010
DJ
653
654 /* If we found a method, print that. We don't bother to disambiguate
655 possible paths to the method based on the adjustment. */
656 if (physname)
657 {
3456e70c
TT
658 gdb::unique_xmalloc_ptr<char> demangled_name
659 = gdb_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
d8734c88 660
6cb06a8c 661 gdb_printf (stream, "&virtual ");
94af9270 662 if (demangled_name == NULL)
0426ad51 663 gdb_puts (physname, stream);
94af9270 664 else
0426ad51 665 gdb_puts (demangled_name.get (), stream);
94af9270 666 return;
0d5de010
DJ
667 }
668 }
94af9270
KS
669 else if (ptr_value != 0)
670 {
671 /* Found a non-virtual function: print out the type. */
0426ad51 672 gdb_puts ("(", stream);
1c6fbf42
PA
673 c_print_type (type, "", stream, -1, 0, current_language->la_language,
674 &type_print_raw_options);
0426ad51 675 gdb_puts (") ", stream);
94af9270 676 }
0d5de010
DJ
677
678 /* We didn't find it; print the raw data. */
679 if (vbit)
680 {
6cb06a8c 681 gdb_printf (stream, "&virtual table offset ");
0d5de010
DJ
682 print_longest (stream, 'd', 1, ptr_value);
683 }
684 else
edf0c1b7
TT
685 {
686 struct value_print_options opts;
687
688 get_user_print_options (&opts);
689 print_address_demangle (&opts, gdbarch, ptr_value, stream, demangle);
690 }
0d5de010
DJ
691
692 if (adjustment)
693 {
6cb06a8c 694 gdb_printf (stream, ", this adjustment ");
0d5de010
DJ
695 print_longest (stream, 'd', 1, adjustment);
696 }
697}
698
699/* GNU v3 implementation of cplus_method_ptr_size. */
700
701static int
ad4820ab 702gnuv3_method_ptr_size (struct type *type)
0d5de010 703{
df86565b 704 return 2 * builtin_type (type->arch ())->builtin_data_ptr->length ();
0d5de010
DJ
705}
706
707/* GNU v3 implementation of cplus_make_method_ptr. */
708
709static void
ad4820ab
UW
710gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
711 CORE_ADDR value, int is_virtual)
0d5de010 712{
8ee511af 713 struct gdbarch *gdbarch = type->arch ();
df86565b 714 int size = builtin_type (gdbarch)->builtin_data_ptr->length ();
34877895 715 enum bfd_endian byte_order = type_byte_order (type);
0d5de010
DJ
716
717 /* FIXME drow/2006-12-24: The adjustment of "this" is currently
718 always zero, since the method pointer is of the correct type.
719 But if the method pointer came from a base class, this is
720 incorrect - it should be the offset to the base. The best
721 fix might be to create the pointer to member pointing at the
722 base class and cast it to the derived class, but that requires
723 support for adjusting pointers to members when casting them -
724 not currently supported by GDB. */
725
ad4820ab 726 if (!gdbarch_vbit_in_delta (gdbarch))
0d5de010 727 {
e17a4113
UW
728 store_unsigned_integer (contents, size, byte_order, value | is_virtual);
729 store_unsigned_integer (contents + size, size, byte_order, 0);
0d5de010
DJ
730 }
731 else
732 {
e17a4113
UW
733 store_unsigned_integer (contents, size, byte_order, value);
734 store_unsigned_integer (contents + size, size, byte_order, is_virtual);
0d5de010
DJ
735 }
736}
737
738/* GNU v3 implementation of cplus_method_ptr_to_value. */
739
740static struct value *
741gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
742{
ad4820ab 743 struct gdbarch *gdbarch;
efaf1ae0 744 const gdb_byte *contents = method_ptr->contents ().data ();
0d5de010 745 CORE_ADDR ptr_value;
09e2d7c7 746 struct type *self_type, *final_type, *method_type;
0d5de010 747 LONGEST adjustment;
0d5de010
DJ
748 int vbit;
749
d0c97917 750 self_type = TYPE_SELF_TYPE (check_typedef (method_ptr->type ()));
09e2d7c7 751 final_type = lookup_pointer_type (self_type);
0d5de010 752
d0c97917 753 method_type = check_typedef (method_ptr->type ())->target_type ();
0d5de010 754
fead6908 755 /* Extract the pointer to member. */
8ee511af 756 gdbarch = self_type->arch ();
ad4820ab 757 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
0d5de010
DJ
758
759 /* First convert THIS to match the containing type of the pointer to
760 member. This cast may adjust the value of THIS. */
761 *this_p = value_cast (final_type, *this_p);
762
763 /* Then apply whatever adjustment is necessary. This creates a somewhat
764 strange pointer: it claims to have type FINAL_TYPE, but in fact it
765 might not be a valid FINAL_TYPE. For instance, it might be a
766 base class of FINAL_TYPE. And if it's not the primary base class,
767 then printing it out as a FINAL_TYPE object would produce some pretty
768 garbage.
769
770 But we don't really know the type of the first argument in
771 METHOD_TYPE either, which is why this happens. We can't
772 dereference this later as a FINAL_TYPE, but once we arrive in the
773 called method we'll have debugging information for the type of
774 "this" - and that'll match the value we produce here.
775
776 You can provoke this case by casting a Base::* to a Derived::*, for
777 instance. */
ad4820ab 778 *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
2497b498 779 *this_p = value_ptradd (*this_p, adjustment);
0d5de010
DJ
780 *this_p = value_cast (final_type, *this_p);
781
782 if (vbit)
783 {
ad4820ab 784 LONGEST voffset;
d8734c88 785
df86565b 786 voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
ad4820ab
UW
787 return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
788 method_type, voffset);
0d5de010
DJ
789 }
790 else
791 return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
792}
793
c4aeac85
TT
794/* Objects of this type are stored in a hash table and a vector when
795 printing the vtables for a class. */
796
797struct value_and_voffset
798{
799 /* The value representing the object. */
800 struct value *value;
801
802 /* The maximum vtable offset we've found for any object at this
803 offset in the outermost object. */
804 int max_voffset;
805};
806
c4aeac85
TT
807/* Hash function for value_and_voffset. */
808
809static hashval_t
810hash_value_and_voffset (const void *p)
811{
9a3c8263 812 const struct value_and_voffset *o = (const struct value_and_voffset *) p;
c4aeac85 813
9feb2d07 814 return o->value->address () + o->value->embedded_offset ();
c4aeac85
TT
815}
816
817/* Equality function for value_and_voffset. */
818
819static int
820eq_value_and_voffset (const void *a, const void *b)
821{
9a3c8263
SM
822 const struct value_and_voffset *ova = (const struct value_and_voffset *) a;
823 const struct value_and_voffset *ovb = (const struct value_and_voffset *) b;
c4aeac85 824
9feb2d07
TT
825 return (ova->value->address () + ova->value->embedded_offset ()
826 == ovb->value->address () + ovb->value->embedded_offset ());
c4aeac85
TT
827}
828
59d3651b 829/* Comparison function for value_and_voffset. */
c4aeac85 830
59d3651b
TT
831static bool
832compare_value_and_voffset (const struct value_and_voffset *va,
833 const struct value_and_voffset *vb)
c4aeac85 834{
9feb2d07 835 CORE_ADDR addra = (va->value->address ()
391f8628 836 + va->value->embedded_offset ());
9feb2d07 837 CORE_ADDR addrb = (vb->value->address ()
391f8628 838 + vb->value->embedded_offset ());
59d3651b
TT
839
840 return addra < addrb;
c4aeac85
TT
841}
842
843/* A helper function used when printing vtables. This determines the
844 key (most derived) sub-object at each address and also computes the
845 maximum vtable offset seen for the corresponding vtable. Updates
846 OFFSET_HASH and OFFSET_VEC with a new value_and_voffset object, if
847 needed. VALUE is the object to examine. */
848
849static void
850compute_vtable_size (htab_t offset_hash,
59d3651b 851 std::vector<value_and_voffset *> *offset_vec,
c4aeac85
TT
852 struct value *value)
853{
854 int i;
d0c97917 855 struct type *type = check_typedef (value->type ());
c4aeac85
TT
856 void **slot;
857 struct value_and_voffset search_vo, *current_vo;
c4aeac85 858
78134374 859 gdb_assert (type->code () == TYPE_CODE_STRUCT);
5f4ce105 860
c4aeac85
TT
861 /* If the object is not dynamic, then we are done; as it cannot have
862 dynamic base types either. */
863 if (!gnuv3_dynamic_class (type))
864 return;
865
866 /* Update the hash and the vec, if needed. */
867 search_vo.value = value;
868 slot = htab_find_slot (offset_hash, &search_vo, INSERT);
869 if (*slot)
9a3c8263 870 current_vo = (struct value_and_voffset *) *slot;
c4aeac85
TT
871 else
872 {
873 current_vo = XNEW (struct value_and_voffset);
874 current_vo->value = value;
875 current_vo->max_voffset = -1;
876 *slot = current_vo;
59d3651b 877 offset_vec->push_back (current_vo);
c4aeac85
TT
878 }
879
880 /* Update the value_and_voffset object with the highest vtable
881 offset from this class. */
882 for (i = 0; i < TYPE_NFN_FIELDS (type); ++i)
883 {
884 int j;
885 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, i);
886
887 for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (type, i); ++j)
888 {
889 if (TYPE_FN_FIELD_VIRTUAL_P (fn, j))
890 {
891 int voffset = TYPE_FN_FIELD_VOFFSET (fn, j);
892
893 if (voffset > current_vo->max_voffset)
894 current_vo->max_voffset = voffset;
895 }
896 }
897 }
898
899 /* Recurse into base classes. */
900 for (i = 0; i < TYPE_N_BASECLASSES (type); ++i)
901 compute_vtable_size (offset_hash, offset_vec, value_field (value, i));
902}
903
904/* Helper for gnuv3_print_vtable that prints a single vtable. */
905
906static void
907print_one_vtable (struct gdbarch *gdbarch, struct value *value,
908 int max_voffset,
909 struct value_print_options *opts)
910{
911 int i;
d0c97917 912 struct type *type = check_typedef (value->type ());
c4aeac85
TT
913 struct value *vtable;
914 CORE_ADDR vt_addr;
915
916 vtable = gnuv3_get_vtable (gdbarch, type,
9feb2d07 917 value->address ()
391f8628 918 + value->embedded_offset ());
9feb2d07
TT
919 vt_addr = value_field (vtable,
920 vtable_field_virtual_functions)->address ();
c4aeac85 921
6cb06a8c
TT
922 gdb_printf (_("vtable for '%s' @ %s (subobject @ %s):\n"),
923 TYPE_SAFE_NAME (type),
924 paddress (gdbarch, vt_addr),
9feb2d07 925 paddress (gdbarch, (value->address ()
391f8628 926 + value->embedded_offset ())));
c4aeac85
TT
927
928 for (i = 0; i <= max_voffset; ++i)
929 {
cafe75b0
JK
930 /* Initialize it just to avoid a GCC false warning. */
931 CORE_ADDR addr = 0;
492d29ea 932 int got_error = 0;
c4aeac85 933 struct value *vfn;
c4aeac85 934
6cb06a8c 935 gdb_printf ("[%d]: ", i);
c4aeac85
TT
936
937 vfn = value_subscript (value_field (vtable,
938 vtable_field_virtual_functions),
939 i);
940
941 if (gdbarch_vtable_function_descriptors (gdbarch))
942 vfn = value_addr (vfn);
943
a70b8144 944 try
c4aeac85
TT
945 {
946 addr = value_as_address (vfn);
947 }
230d2906 948 catch (const gdb_exception_error &ex)
492d29ea 949 {
7f6aba03
TT
950 fprintf_styled (gdb_stdout, metadata_style.style (),
951 _("<error: %s>"), ex.what ());
492d29ea
PA
952 got_error = 1;
953 }
492d29ea
PA
954
955 if (!got_error)
edf0c1b7 956 print_function_pointer_address (opts, gdbarch, addr, gdb_stdout);
6cb06a8c 957 gdb_printf ("\n");
c4aeac85
TT
958 }
959}
960
961/* Implementation of the print_vtable method. */
962
963static void
964gnuv3_print_vtable (struct value *value)
965{
966 struct gdbarch *gdbarch;
967 struct type *type;
968 struct value *vtable;
969 struct value_print_options opts;
59d3651b 970 int count;
c4aeac85
TT
971
972 value = coerce_ref (value);
d0c97917 973 type = check_typedef (value->type ());
78134374 974 if (type->code () == TYPE_CODE_PTR)
c4aeac85
TT
975 {
976 value = value_ind (value);
d0c97917 977 type = check_typedef (value->type ());
c4aeac85
TT
978 }
979
980 get_user_print_options (&opts);
981
982 /* Respect 'set print object'. */
983 if (opts.objectprint)
984 {
985 value = value_full_object (value, NULL, 0, 0, 0);
d0c97917 986 type = check_typedef (value->type ());
c4aeac85
TT
987 }
988
8ee511af 989 gdbarch = type->arch ();
5f4ce105
DE
990
991 vtable = NULL;
78134374 992 if (type->code () == TYPE_CODE_STRUCT)
5f4ce105
DE
993 vtable = gnuv3_get_vtable (gdbarch, type,
994 value_as_address (value_addr (value)));
c4aeac85
TT
995
996 if (!vtable)
997 {
6cb06a8c 998 gdb_printf (_("This object does not have a virtual function table\n"));
c4aeac85
TT
999 return;
1000 }
1001
fc4007c9
TT
1002 htab_up offset_hash (htab_create_alloc (1, hash_value_and_voffset,
1003 eq_value_and_voffset,
1004 xfree, xcalloc, xfree));
59d3651b 1005 std::vector<value_and_voffset *> result_vec;
c4aeac85 1006
fc4007c9 1007 compute_vtable_size (offset_hash.get (), &result_vec, value);
59d3651b
TT
1008 std::sort (result_vec.begin (), result_vec.end (),
1009 compare_value_and_voffset);
c4aeac85
TT
1010
1011 count = 0;
59d3651b 1012 for (value_and_voffset *iter : result_vec)
c4aeac85
TT
1013 {
1014 if (iter->max_voffset >= 0)
1015 {
1016 if (count > 0)
6cb06a8c 1017 gdb_printf ("\n");
c4aeac85
TT
1018 print_one_vtable (gdbarch, iter->value, iter->max_voffset, &opts);
1019 ++count;
1020 }
1021 }
c4aeac85
TT
1022}
1023
6e72ca20
TT
1024/* Return a GDB type representing `struct std::type_info', laid out
1025 appropriately for ARCH.
1026
1027 We use this function as the gdbarch per-architecture data
1028 initialization function. */
1029
cb275538 1030static struct type *
6e72ca20
TT
1031build_std_type_info_type (struct gdbarch *arch)
1032{
1033 struct type *t;
6e72ca20
TT
1034 int offset;
1035 struct type *void_ptr_type
1036 = builtin_type (arch)->builtin_data_ptr;
1037 struct type *char_type
1038 = builtin_type (arch)->builtin_char;
1039 struct type *char_ptr_type
1040 = make_pointer_type (make_cv_type (1, 0, char_type, NULL), NULL);
1041
2774f2da
TV
1042 t = type_allocator (arch).new_type (TYPE_CODE_STRUCT, 0, nullptr);
1043
1044 t->alloc_fields (2);
1045
6e72ca20
TT
1046 offset = 0;
1047
1048 /* The vtable. */
2774f2da
TV
1049 {
1050 struct field &field0 = t->field (0);
1051 field0.set_name ("_vptr.type_info");
1052 field0.set_type (void_ptr_type);
1053 field0.set_loc_bitpos (offset * TARGET_CHAR_BIT);
1054 offset += field0.type ()->length ();
1055 }
6e72ca20
TT
1056
1057 /* The name. */
2774f2da
TV
1058 {
1059 struct field &field1 = t->field (1);
1060 field1.set_name ("__name");
1061 field1.set_type (char_ptr_type);
1062 field1.set_loc_bitpos (offset * TARGET_CHAR_BIT);
1063 offset += field1.type ()->length ();
1064 }
1065
1066 t->set_length (offset);
1067
d0e39ea2 1068 t->set_name ("gdb_gnu_v3_type_info");
6e72ca20
TT
1069 INIT_CPLUS_SPECIFIC (t);
1070
1071 return t;
1072}
1073
1074/* Implement the 'get_typeid_type' method. */
1075
1076static struct type *
1077gnuv3_get_typeid_type (struct gdbarch *gdbarch)
1078{
1079 struct symbol *typeinfo;
1080 struct type *typeinfo_type;
1081
ccf41c24 1082 typeinfo = lookup_symbol ("std::type_info", NULL, SEARCH_STRUCT_DOMAIN,
d12307c1 1083 NULL).symbol;
6e72ca20 1084 if (typeinfo == NULL)
cb275538
TT
1085 {
1086 typeinfo_type = std_type_info_gdbarch_data.get (gdbarch);
1087 if (typeinfo_type == nullptr)
1088 {
1089 typeinfo_type = build_std_type_info_type (gdbarch);
1090 std_type_info_gdbarch_data.set (gdbarch, typeinfo_type);
1091 }
1092 }
6e72ca20 1093 else
5f9c5a63 1094 typeinfo_type = typeinfo->type ();
6e72ca20
TT
1095
1096 return typeinfo_type;
1097}
1098
1099/* Implement the 'get_typeid' method. */
1100
1101static struct value *
1102gnuv3_get_typeid (struct value *value)
1103{
1104 struct type *typeinfo_type;
1105 struct type *type;
1106 struct gdbarch *gdbarch;
6e72ca20 1107 struct value *result;
596dc4ad
TT
1108 std::string type_name;
1109 gdb::unique_xmalloc_ptr<char> canonical;
6e72ca20
TT
1110
1111 /* We have to handle values a bit trickily here, to allow this code
1112 to work properly with non_lvalue values that are really just
1113 disguised types. */
97044105 1114 if (value->lval () == lval_memory)
6e72ca20
TT
1115 value = coerce_ref (value);
1116
d0c97917 1117 type = check_typedef (value->type ());
6e72ca20
TT
1118
1119 /* In the non_lvalue case, a reference might have slipped through
1120 here. */
78134374 1121 if (type->code () == TYPE_CODE_REF)
27710edb 1122 type = check_typedef (type->target_type ());
6e72ca20
TT
1123
1124 /* Ignore top-level cv-qualifiers. */
1125 type = make_cv_type (0, 0, type, NULL);
8ee511af 1126 gdbarch = type->arch ();
6e72ca20 1127
fe978cb0 1128 type_name = type_to_string (type);
2f408ecb 1129 if (type_name.empty ())
6e72ca20 1130 error (_("cannot find typeinfo for unnamed type"));
6e72ca20
TT
1131
1132 /* We need to canonicalize the type name here, because we do lookups
1133 using the demangled name, and so we must match the format it
1134 uses. E.g., GDB tends to use "const char *" as a type name, but
1135 the demangler uses "char const *". */
2f408ecb 1136 canonical = cp_canonicalize_string (type_name.c_str ());
596dc4ad
TT
1137 const char *name = (canonical == nullptr
1138 ? type_name.c_str ()
1139 : canonical.get ());
6e72ca20
TT
1140
1141 typeinfo_type = gnuv3_get_typeid_type (gdbarch);
1142
1143 /* We check for lval_memory because in the "typeid (type-id)" case,
1144 the type is passed via a not_lval value object. */
78134374 1145 if (type->code () == TYPE_CODE_STRUCT
97044105 1146 && value->lval () == lval_memory
6e72ca20
TT
1147 && gnuv3_dynamic_class (type))
1148 {
1149 struct value *vtable, *typeinfo_value;
9feb2d07 1150 CORE_ADDR address = value->address () + value->embedded_offset ();
6e72ca20
TT
1151
1152 vtable = gnuv3_get_vtable (gdbarch, type, address);
1153 if (vtable == NULL)
2f408ecb 1154 error (_("cannot find typeinfo for object of type '%s'"),
596dc4ad 1155 name);
6e72ca20
TT
1156 typeinfo_value = value_field (vtable, vtable_field_type_info);
1157 result = value_ind (value_cast (make_pointer_type (typeinfo_type, NULL),
1158 typeinfo_value));
1159 }
1160 else
1161 {
596dc4ad 1162 std::string sym_name = std::string ("typeinfo for ") + name;
2f408ecb
PA
1163 bound_minimal_symbol minsym
1164 = lookup_minimal_symbol (sym_name.c_str (), NULL, NULL);
6e72ca20 1165
3b7344d5 1166 if (minsym.minsym == NULL)
596dc4ad 1167 error (_("could not find typeinfo symbol for '%s'"), name);
6e72ca20 1168
4aeddc50 1169 result = value_at_lazy (typeinfo_type, minsym.value_address ());
6e72ca20
TT
1170 }
1171
6e72ca20
TT
1172 return result;
1173}
1174
cc16e6c9 1175/* Implement the 'get_typename_from_type_info' method. */
72f1fe8a 1176
2f408ecb 1177static std::string
72f1fe8a
TT
1178gnuv3_get_typename_from_type_info (struct value *type_info_ptr)
1179{
d0c97917 1180 struct gdbarch *gdbarch = type_info_ptr->type ()->arch ();
72f1fe8a
TT
1181 struct bound_minimal_symbol typeinfo_sym;
1182 CORE_ADDR addr;
1183 const char *symname;
1184 const char *class_name;
1185 const char *atsign;
1186
1187 addr = value_as_address (type_info_ptr);
1188 typeinfo_sym = lookup_minimal_symbol_by_pc (addr);
1189 if (typeinfo_sym.minsym == NULL)
1190 error (_("could not find minimal symbol for typeinfo address %s"),
1191 paddress (gdbarch, addr));
1192
1193#define TYPEINFO_PREFIX "typeinfo for "
1194#define TYPEINFO_PREFIX_LEN (sizeof (TYPEINFO_PREFIX) - 1)
c9d95fa3 1195 symname = typeinfo_sym.minsym->demangled_name ();
72f1fe8a
TT
1196 if (symname == NULL || strncmp (symname, TYPEINFO_PREFIX,
1197 TYPEINFO_PREFIX_LEN))
1198 error (_("typeinfo symbol '%s' has unexpected name"),
c9d95fa3 1199 typeinfo_sym.minsym->linkage_name ());
72f1fe8a
TT
1200 class_name = symname + TYPEINFO_PREFIX_LEN;
1201
1202 /* Strip off @plt and version suffixes. */
1203 atsign = strchr (class_name, '@');
1204 if (atsign != NULL)
2f408ecb
PA
1205 return std::string (class_name, atsign - class_name);
1206 return class_name;
72f1fe8a
TT
1207}
1208
1209/* Implement the 'get_type_from_type_info' method. */
1210
1211static struct type *
1212gnuv3_get_type_from_type_info (struct value *type_info_ptr)
1213{
72f1fe8a
TT
1214 /* We have to parse the type name, since in general there is not a
1215 symbol for a type. This is somewhat bogus since there may be a
1216 mis-parse. Another approach might be to re-use the demangler's
1217 internal form to reconstruct the type somehow. */
2f408ecb
PA
1218 std::string type_name = gnuv3_get_typename_from_type_info (type_info_ptr);
1219 expression_up expr (parse_expression (type_name.c_str ()));
ba71385e 1220 struct value *type_val = expr->evaluate_type ();
d0c97917 1221 return type_val->type ();
72f1fe8a
TT
1222}
1223
b18be20d
DJ
1224/* Determine if we are currently in a C++ thunk. If so, get the address
1225 of the routine we are thunking to and continue to there instead. */
1226
1227static CORE_ADDR
8480a37e 1228gnuv3_skip_trampoline (const frame_info_ptr &frame, CORE_ADDR stop_pc)
b18be20d 1229{
a513d1e8 1230 CORE_ADDR real_stop_pc, method_stop_pc, func_addr;
9970f04b 1231 struct gdbarch *gdbarch = get_frame_arch (frame);
3b7344d5 1232 struct bound_minimal_symbol thunk_sym, fn_sym;
b18be20d 1233 struct obj_section *section;
0d5cff50 1234 const char *thunk_name, *fn_name;
b18be20d 1235
9970f04b 1236 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
b18be20d
DJ
1237 if (real_stop_pc == 0)
1238 real_stop_pc = stop_pc;
1239
1240 /* Find the linker symbol for this potential thunk. */
3b7344d5 1241 thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
b18be20d 1242 section = find_pc_section (real_stop_pc);
3b7344d5 1243 if (thunk_sym.minsym == NULL || section == NULL)
b18be20d
DJ
1244 return 0;
1245
1246 /* The symbol's demangled name should be something like "virtual
1247 thunk to FUNCTION", where FUNCTION is the name of the function
1248 being thunked to. */
c9d95fa3 1249 thunk_name = thunk_sym.minsym->demangled_name ();
b18be20d
DJ
1250 if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
1251 return 0;
1252
1253 fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
1254 fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile);
3b7344d5 1255 if (fn_sym.minsym == NULL)
b18be20d
DJ
1256 return 0;
1257
4aeddc50 1258 method_stop_pc = fn_sym.value_address ();
a513d1e8
LM
1259
1260 /* Some targets have minimal symbols pointing to function descriptors
1261 (powerpc 64 for example). Make sure to retrieve the address
1262 of the real function from the function descriptor before passing on
1263 the address to other layers of GDB. */
328d42d8
SM
1264 func_addr = gdbarch_convert_from_func_ptr_addr
1265 (gdbarch, method_stop_pc, current_inferior ()->top_target ());
a513d1e8
LM
1266 if (func_addr != 0)
1267 method_stop_pc = func_addr;
1268
e76f05fa 1269 real_stop_pc = gdbarch_skip_trampoline_code
9970f04b 1270 (gdbarch, frame, method_stop_pc);
b18be20d
DJ
1271 if (real_stop_pc == 0)
1272 real_stop_pc = method_stop_pc;
1273
1274 return real_stop_pc;
1275}
1276
62bf63d7
TBA
1277/* A member function is in one these states. */
1278
1279enum definition_style
1280{
1281 DOES_NOT_EXIST_IN_SOURCE,
1282 DEFAULTED_INSIDE,
1283 DEFAULTED_OUTSIDE,
1284 DELETED,
1285 EXPLICIT,
1286};
1287
1288/* Return how the given field is defined. */
1289
1290static definition_style
1291get_def_style (struct fn_field *fn, int fieldelem)
1292{
1293 if (TYPE_FN_FIELD_DELETED (fn, fieldelem))
1294 return DELETED;
1295
1296 if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
1297 return DOES_NOT_EXIST_IN_SOURCE;
1298
1299 switch (TYPE_FN_FIELD_DEFAULTED (fn, fieldelem))
1300 {
1301 case DW_DEFAULTED_no:
1302 return EXPLICIT;
1303 case DW_DEFAULTED_in_class:
1304 return DEFAULTED_INSIDE;
1305 case DW_DEFAULTED_out_of_class:
1306 return DEFAULTED_OUTSIDE;
1307 default:
1308 break;
1309 }
1310
1311 return EXPLICIT;
1312}
1313
1314/* Helper functions to determine whether the given definition style
1315 denotes that the definition is user-provided or implicit.
1316 Being defaulted outside the class decl counts as an explicit
1317 user-definition, while being defaulted inside is implicit. */
1318
1319static bool
1320is_user_provided_def (definition_style def)
1321{
1322 return def == EXPLICIT || def == DEFAULTED_OUTSIDE;
1323}
1324
1325static bool
1326is_implicit_def (definition_style def)
1327{
1328 return def == DOES_NOT_EXIST_IN_SOURCE || def == DEFAULTED_INSIDE;
1329}
1330
1331/* Helper function to decide if METHOD_TYPE is a copy/move
1332 constructor type for CLASS_TYPE. EXPECTED is the expected
1333 type code for the "right-hand-side" argument.
1334 This function is supposed to be used by the IS_COPY_CONSTRUCTOR_TYPE
1335 and IS_MOVE_CONSTRUCTOR_TYPE functions below. Normally, you should
1336 not need to call this directly. */
1337
1338static bool
1339is_copy_or_move_constructor_type (struct type *class_type,
1340 struct type *method_type,
1341 type_code expected)
1342{
1343 /* The method should take at least two arguments... */
1f704f76 1344 if (method_type->num_fields () < 2)
62bf63d7
TBA
1345 return false;
1346
1347 /* ...and the second argument should be the same as the class
1348 type, with the expected type code... */
940da03e 1349 struct type *arg_type = method_type->field (1).type ();
62bf63d7 1350
78134374 1351 if (arg_type->code () != expected)
62bf63d7
TBA
1352 return false;
1353
27710edb 1354 struct type *target = check_typedef (arg_type->target_type ());
62bf63d7
TBA
1355 if (!(class_types_same_p (target, class_type)))
1356 return false;
1357
1358 /* ...and if any of the remaining arguments don't have a default value
1359 then this is not a copy or move constructor, but just a
1360 constructor. */
1f704f76 1361 for (int i = 2; i < method_type->num_fields (); i++)
62bf63d7 1362 {
940da03e 1363 arg_type = method_type->field (i).type ();
62bf63d7
TBA
1364 /* FIXME aktemur/2019-10-31: As of this date, neither
1365 clang++-7.0.0 nor g++-8.2.0 produce a DW_AT_default_value
1366 attribute. GDB is also not set to read this attribute, yet.
1367 Hence, we immediately return false if there are more than
1368 2 parameters.
1369 GCC bug link:
1370 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42959
1371 */
1372 return false;
1373 }
1374
1375 return true;
1376}
1377
1378/* Return true if METHOD_TYPE is a copy ctor type for CLASS_TYPE. */
1379
1380static bool
1381is_copy_constructor_type (struct type *class_type,
1382 struct type *method_type)
1383{
1384 return is_copy_or_move_constructor_type (class_type, method_type,
1385 TYPE_CODE_REF);
1386}
1387
1388/* Return true if METHOD_TYPE is a move ctor type for CLASS_TYPE. */
1389
1390static bool
1391is_move_constructor_type (struct type *class_type,
1392 struct type *method_type)
1393{
1394 return is_copy_or_move_constructor_type (class_type, method_type,
1395 TYPE_CODE_RVALUE_REF);
1396}
1397
9d084466 1398/* Return pass-by-reference information for the given TYPE.
41f1b697
DJ
1399
1400 The rule in the v3 ABI document comes from section 3.1.1. If the
1401 type has a non-trivial copy constructor or destructor, then the
1402 caller must make a copy (by calling the copy constructor if there
1403 is one or perform the copy itself otherwise), pass the address of
1404 the copy, and then destroy the temporary (if necessary).
1405
62bf63d7 1406 For return values with non-trivial copy/move constructors or
41f1b697
DJ
1407 destructors, space will be allocated in the caller, and a pointer
1408 will be passed as the first argument (preceding "this").
1409
1410 We don't have a bulletproof mechanism for determining whether a
62bf63d7
TBA
1411 constructor or destructor is trivial. For GCC and DWARF5 debug
1412 information, we can check the calling_convention attribute,
1413 the 'artificial' flag, the 'defaulted' attribute, and the
1414 'deleted' attribute. */
9d084466
TBA
1415
1416static struct language_pass_by_ref_info
41f1b697
DJ
1417gnuv3_pass_by_reference (struct type *type)
1418{
1419 int fieldnum, fieldelem;
1420
f168693b 1421 type = check_typedef (type);
41f1b697 1422
9d084466 1423 /* Start with the default values. */
48448202 1424 struct language_pass_by_ref_info info;
9d084466 1425
62bf63d7
TBA
1426 bool has_cc_attr = false;
1427 bool is_pass_by_value = false;
1428 bool is_dynamic = false;
1429 definition_style cctor_def = DOES_NOT_EXIST_IN_SOURCE;
1430 definition_style dtor_def = DOES_NOT_EXIST_IN_SOURCE;
1431 definition_style mctor_def = DOES_NOT_EXIST_IN_SOURCE;
9d084466 1432
41f1b697 1433 /* We're only interested in things that can have methods. */
78134374
SM
1434 if (type->code () != TYPE_CODE_STRUCT
1435 && type->code () != TYPE_CODE_UNION)
9d084466 1436 return info;
41f1b697 1437
62bf63d7
TBA
1438 /* The compiler may have emitted the calling convention attribute.
1439 Note: GCC does not produce this attribute as of version 9.2.1.
1440 Bug link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92418 */
1441 if (TYPE_CPLUS_CALLING_CONVENTION (type) == DW_CC_pass_by_value)
1442 {
1443 has_cc_attr = true;
1444 is_pass_by_value = true;
1445 /* Do not return immediately. We have to find out if this type
1446 is copy_constructible and destructible. */
1447 }
1448
1449 if (TYPE_CPLUS_CALLING_CONVENTION (type) == DW_CC_pass_by_reference)
1450 {
1451 has_cc_attr = true;
1452 is_pass_by_value = false;
1453 }
1454
ebb8ece2
SC
1455 /* A dynamic class has a non-trivial copy constructor.
1456 See c++98 section 12.8 Copying class objects [class.copy]. */
1457 if (gnuv3_dynamic_class (type))
62bf63d7 1458 is_dynamic = true;
ebb8ece2 1459
41f1b697
DJ
1460 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
1461 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
1462 fieldelem++)
1463 {
1464 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
0d5cff50 1465 const char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
41f1b697
DJ
1466 struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
1467
41f1b697 1468 if (name[0] == '~')
9d084466 1469 {
62bf63d7
TBA
1470 /* We've found a destructor.
1471 There should be at most one dtor definition. */
1472 gdb_assert (dtor_def == DOES_NOT_EXIST_IN_SOURCE);
1473 dtor_def = get_def_style (fn, fieldelem);
9d084466 1474 }
62bf63d7
TBA
1475 else if (is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem))
1476 || TYPE_FN_FIELD_CONSTRUCTOR (fn, fieldelem))
82c48ac7 1477 {
62bf63d7
TBA
1478 /* FIXME drow/2007-09-23: We could do this using the name of
1479 the method and the name of the class instead of dealing
1480 with the mangled name. We don't have a convenient function
1481 to strip off both leading scope qualifiers and trailing
1482 template arguments yet. */
1483 if (is_copy_constructor_type (type, fieldtype))
1484 {
1485 /* There may be more than one cctors. E.g.: one that
1486 take a const parameter and another that takes a
1487 non-const parameter. Such as:
1488
1489 class K {
1490 K (const K &k)...
1491 K (K &k)...
1492 };
1493
1494 It is sufficient for the type to be non-trivial
1495 even only one of the cctors is explicit.
1496 Therefore, update the cctor_def value in the
1497 implicit -> explicit direction, not backwards. */
1498
1499 if (is_implicit_def (cctor_def))
1500 cctor_def = get_def_style (fn, fieldelem);
1501 }
1502 else if (is_move_constructor_type (type, fieldtype))
3433cfa5 1503 {
62bf63d7
TBA
1504 /* Again, there may be multiple move ctors. Update the
1505 mctor_def value if we found an explicit def and the
1506 existing one is not explicit. Otherwise retain the
1507 existing value. */
1508 if (is_implicit_def (mctor_def))
1509 mctor_def = get_def_style (fn, fieldelem);
3433cfa5 1510 }
82c48ac7 1511 }
41f1b697
DJ
1512 }
1513
62bf63d7
TBA
1514 bool cctor_implicitly_deleted
1515 = (mctor_def != DOES_NOT_EXIST_IN_SOURCE
1516 && cctor_def == DOES_NOT_EXIST_IN_SOURCE);
1517
1518 bool cctor_explicitly_deleted = (cctor_def == DELETED);
1519
1520 if (cctor_implicitly_deleted || cctor_explicitly_deleted)
1521 info.copy_constructible = false;
1522
1523 if (dtor_def == DELETED)
1524 info.destructible = false;
1525
1526 info.trivially_destructible = is_implicit_def (dtor_def);
1527
1528 info.trivially_copy_constructible
1529 = (is_implicit_def (cctor_def)
1530 && !is_dynamic);
1531
1532 info.trivially_copyable
1533 = (info.trivially_copy_constructible
1534 && info.trivially_destructible
1535 && !is_user_provided_def (mctor_def));
1536
41f1b697
DJ
1537 /* Even if all the constructors and destructors were artificial, one
1538 of them may have invoked a non-artificial constructor or
1539 destructor in a base class. If any base class needs to be passed
1540 by reference, so does this class. Similarly for members, which
1541 are constructed whenever this class is. We do not need to worry
1542 about recursive loops here, since we are only looking at members
bceffbf3 1543 of complete class type. Also ignore any static members. */
1f704f76 1544 for (fieldnum = 0; fieldnum < type->num_fields (); fieldnum++)
c819a338 1545 if (!type->field (fieldnum).is_static ())
9d084466 1546 {
940da03e 1547 struct type *field_type = type->field (fieldnum).type ();
62bf63d7
TBA
1548
1549 /* For arrays, make the decision based on the element type. */
78134374 1550 if (field_type->code () == TYPE_CODE_ARRAY)
27710edb 1551 field_type = check_typedef (field_type->target_type ());
62bf63d7 1552
9d084466 1553 struct language_pass_by_ref_info field_info
62bf63d7
TBA
1554 = gnuv3_pass_by_reference (field_type);
1555
1556 if (!field_info.copy_constructible)
1557 info.copy_constructible = false;
1558 if (!field_info.destructible)
1559 info.destructible = false;
9d084466 1560 if (!field_info.trivially_copyable)
62bf63d7
TBA
1561 info.trivially_copyable = false;
1562 if (!field_info.trivially_copy_constructible)
1563 info.trivially_copy_constructible = false;
1564 if (!field_info.trivially_destructible)
1565 info.trivially_destructible = false;
9d084466 1566 }
41f1b697 1567
62bf63d7
TBA
1568 /* Consistency check. */
1569 if (has_cc_attr && info.trivially_copyable != is_pass_by_value)
1570 {
1571 /* DWARF CC attribute is not the same as the inferred value;
1572 use the DWARF attribute. */
1573 info.trivially_copyable = is_pass_by_value;
1574 }
1575
9d084466 1576 return info;
41f1b697
DJ
1577}
1578
7ed49443
JB
1579static void
1580init_gnuv3_ops (void)
1581{
7ed49443
JB
1582 gnu_v3_abi_ops.shortname = "gnu-v3";
1583 gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
1584 gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
358777b0
EZ
1585 gnu_v3_abi_ops.is_destructor_name =
1586 (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
1587 gnu_v3_abi_ops.is_constructor_name =
1588 (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
7ed49443
JB
1589 gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
1590 gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
1591 gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
1592 gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
1514d34e 1593 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
0d5de010
DJ
1594 gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
1595 gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
1596 gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
1597 gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
c4aeac85 1598 gnu_v3_abi_ops.print_vtable = gnuv3_print_vtable;
6e72ca20
TT
1599 gnu_v3_abi_ops.get_typeid = gnuv3_get_typeid;
1600 gnu_v3_abi_ops.get_typeid_type = gnuv3_get_typeid_type;
72f1fe8a 1601 gnu_v3_abi_ops.get_type_from_type_info = gnuv3_get_type_from_type_info;
cc16e6c9
TT
1602 gnu_v3_abi_ops.get_typename_from_type_info
1603 = gnuv3_get_typename_from_type_info;
b18be20d 1604 gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
41f1b697 1605 gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
7ed49443
JB
1606}
1607
6c265988 1608void _initialize_gnu_v3_abi ();
7ed49443 1609void
6c265988 1610_initialize_gnu_v3_abi ()
7ed49443
JB
1611{
1612 init_gnuv3_ops ();
1613
fe1f4a5e 1614 register_cp_abi (&gnu_v3_abi_ops);
1605ef26 1615 set_cp_abi_as_auto_default (gnu_v3_abi_ops.shortname);
7ed49443 1616}