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