]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gnu-v3-abi.c
New Romanian translation for ld
[thirdparty/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 int offset;
117
118 struct type *result = vtable_type_gdbarch_data.get (arch);
119 if (result != nullptr)
120 return result;
121
122 struct type *void_ptr_type
123 = builtin_type (arch)->builtin_data_ptr;
124 struct type *ptr_to_void_fn_type
125 = builtin_type (arch)->builtin_func_ptr;
126
127 type_allocator alloc (arch);
128
129 /* ARCH can't give us the true ptrdiff_t type, so we guess. */
130 struct type *ptrdiff_type
131 = init_integer_type (alloc, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
132
133 t = alloc.new_type (TYPE_CODE_STRUCT, 0, nullptr);
134
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. */
141 t->alloc_fields (4);
142
143 offset = 0;
144
145 /* ptrdiff_t vcall_and_vbase_offsets[0]; */
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 }
153
154 /* ptrdiff_t offset_to_top; */
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 }
162
163 /* void *type_info; */
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 }
171
172 /* void (*virtual_functions[0]) (); */
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
183 t->set_name ("gdb_gnu_v3_abi_vtable");
184 INIT_CPLUS_SPECIFIC (t);
185
186 result = make_type_with_address_space (t, TYPE_INSTANCE_FLAG_CODE_SPACE);
187 vtable_type_gdbarch_data.set (arch, result);
188 return result;
189 }
190
191
192 /* Return the ptrdiff_t type used in the vtable type. */
193 static struct type *
194 vtable_ptrdiff_type (struct gdbarch *gdbarch)
195 {
196 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
197
198 /* The "offset_to_top" field has the appropriate (ptrdiff_t) type. */
199 return vtable_type->field (vtable_field_offset_to_top).type ();
200 }
201
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). */
205 static int
206 vtable_address_point_offset (struct gdbarch *gdbarch)
207 {
208 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
209
210 return (vtable_type->field (vtable_field_virtual_functions).loc_bitpos ()
211 / TARGET_CHAR_BIT);
212 }
213
214
215 /* Determine whether structure TYPE is a dynamic class. Cache the
216 result. */
217
218 static int
219 gnuv3_dynamic_class (struct type *type)
220 {
221 int fieldnum, fieldelem;
222
223 type = check_typedef (type);
224 gdb_assert (type->code () == TYPE_CODE_STRUCT
225 || type->code () == TYPE_CODE_UNION);
226
227 if (type->code () == TYPE_CODE_UNION)
228 return 0;
229
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)
237 || gnuv3_dynamic_class (type->field (fieldnum).type ()))
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
264 static struct value *
265 gnuv3_get_vtable (struct gdbarch *gdbarch,
266 struct type *container_type, CORE_ADDR container_addr)
267 {
268 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
269 struct type *vtable_pointer_type;
270 struct value *vtable_pointer;
271 CORE_ADDR vtable_address;
272
273 container_type = check_typedef (container_type);
274 gdb_assert (container_type->code () == TYPE_CODE_STRUCT);
275
276 /* If this type does not have a virtual table, don't read the first
277 field. */
278 if (!gnuv3_dynamic_class (container_type))
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,
298 vtable_address
299 - vtable_address_point_offset (gdbarch));
300 }
301
302
303 static struct type *
304 gnuv3_rtti_type (struct value *value,
305 int *full_p, LONGEST *top_p, int *using_enc_p)
306 {
307 struct gdbarch *gdbarch;
308 struct type *values_type = check_typedef (value->type ());
309 struct value *vtable;
310 struct minimal_symbol *vtable_symbol;
311 const char *vtable_symbol_name;
312 const char *class_name;
313 struct type *run_time_type;
314 LONGEST offset_to_top;
315 const char *atsign;
316
317 /* We only have RTTI for dynamic class objects. */
318 if (values_type->code () != TYPE_CODE_STRUCT
319 || !gnuv3_dynamic_class (values_type))
320 return NULL;
321
322 /* Determine architecture. */
323 gdbarch = values_type->arch ();
324
325 if (using_enc_p)
326 *using_enc_p = 0;
327
328 vtable = gnuv3_get_vtable (gdbarch, values_type,
329 value_as_address (value_addr (value)));
330 if (vtable == NULL)
331 return NULL;
332
333 /* Find the linker symbol for this vtable. */
334 vtable_symbol
335 = lookup_minimal_symbol_by_pc (vtable->address ()
336 + vtable->embedded_offset ()).minsym;
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. */
345 vtable_symbol_name = vtable_symbol->demangled_name ();
346 if (vtable_symbol_name == NULL
347 || !startswith (vtable_symbol_name, "vtable for "))
348 {
349 warning (_("can't find linker symbol for virtual table for `%s' value"),
350 TYPE_SAFE_NAME (values_type));
351 if (vtable_symbol_name)
352 warning (_(" found `%s' instead"), vtable_symbol_name);
353 return NULL;
354 }
355 class_name = vtable_symbol_name + 11;
356
357 /* Strip off @plt and version suffixes. */
358 atsign = strchr (class_name, '@');
359 if (atsign != NULL)
360 {
361 char *copy;
362
363 copy = (char *) alloca (atsign - class_name + 1);
364 memcpy (copy, class_name, atsign - class_name);
365 copy[atsign - class_name] = '\0';
366 class_name = copy;
367 }
368
369 /* Try to look up the class name as a type name. */
370 /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
371 run_time_type = cp_lookup_rtti_type (class_name, NULL);
372 if (run_time_type == NULL)
373 return NULL;
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)
381 *full_p = (- offset_to_top == value->embedded_offset ()
382 && (value->enclosing_type ()->length ()
383 >= run_time_type->length ()));
384 if (top_p)
385 *top_p = - offset_to_top;
386 return run_time_type;
387 }
388
389 /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
390 function, of type FNTYPE. */
391
392 static struct value *
393 gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
394 struct type *fntype, int vtable_index)
395 {
396 struct value *vtable, *vfn;
397
398 /* Every class with virtual functions must have a vtable. */
399 vtable = gnuv3_get_vtable (gdbarch, container->type (),
400 value_as_address (value_addr (container)));
401 gdb_assert (vtable != NULL);
402
403 /* Fetch the appropriate function pointer from the vtable. */
404 vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
405 vtable_index);
406
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
410 by the size of a function descriptor; GCC does that before outputting
411 debug information. */
412 if (gdbarch_vtable_function_descriptors (gdbarch))
413 vfn = value_addr (vfn);
414
415 /* Cast the function pointer to the appropriate type. */
416 vfn = value_cast (lookup_pointer_type (fntype), vfn);
417
418 return vfn;
419 }
420
421 /* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h
422 for a description of the arguments. */
423
424 static struct value *
425 gnuv3_virtual_fn_field (struct value **value_p,
426 struct fn_field *f, int j,
427 struct type *vfn_base, int offset)
428 {
429 struct type *values_type = check_typedef ((*value_p)->type ());
430 struct gdbarch *gdbarch;
431
432 /* Some simple sanity checks. */
433 if (values_type->code () != TYPE_CODE_STRUCT)
434 error (_("Only classes can have virtual functions."));
435
436 /* Determine architecture. */
437 gdbarch = values_type->arch ();
438
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
445 return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
446 TYPE_FN_FIELD_VOFFSET (f, j));
447 }
448
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
455 -1 is returned on error. */
456
457 static int
458 gnuv3_baseclass_offset (struct type *type, int index,
459 const bfd_byte *valaddr, LONGEST embedded_offset,
460 CORE_ADDR address, const struct value *val)
461 {
462 struct gdbarch *gdbarch;
463 struct type *ptr_type;
464 struct value *vtable;
465 struct value *vbase_array;
466 long int cur_base_offset, base_offset;
467
468 /* Determine architecture. */
469 gdbarch = type->arch ();
470 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
471
472 /* If it isn't a virtual base, this is easy. The offset is in the
473 type definition. */
474 if (!BASETYPE_VIA_VIRTUAL (type, index))
475 return TYPE_BASECLASS_BITPOS (type, index) / 8;
476
477 /* If we have a DWARF expression for the offset, evaluate it. */
478 if (type->field (index).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
479 {
480 struct dwarf2_property_baton baton;
481 baton.property_type
482 = lookup_pointer_type (type->field (index).type ());
483 baton.locexpr = *type->field (index).loc_dwarf_block ();
484
485 struct dynamic_prop prop;
486 prop.set_locexpr (&baton);
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,
497 {addr_stack.addr}))
498 return (int) (result - addr_stack.addr);
499 }
500
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;
507 if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
508 error (_("Expected a negative vbase offset (old compiler?)"));
509
510 cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
511 if ((- cur_base_offset) % ptr_type->length () != 0)
512 error (_("Misaligned vbase offset."));
513 cur_base_offset = cur_base_offset / ((int) ptr_type->length ());
514
515 vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset);
516 gdb_assert (vtable != NULL);
517 vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
518 base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
519 return base_offset;
520 }
521
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
526 static const char *
527 gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
528 LONGEST adjustment)
529 {
530 int i;
531
532 /* Search this class first. */
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;
564 basetype = domain->field (i).type ();
565 /* Recurse with a modified adjustment. We don't need to adjust
566 voffset. */
567 if (adjustment >= pos && adjustment < pos + basetype->length ())
568 return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
569 }
570
571 return NULL;
572 }
573
574 /* Decode GNU v3 method pointer. */
575
576 static int
577 gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
578 const gdb_byte *contents,
579 CORE_ADDR *value_p,
580 LONGEST *adjustment_p)
581 {
582 struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
583 struct type *offset_type = vtable_ptrdiff_type (gdbarch);
584 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
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);
596 voffset = extract_signed_integer (contents,
597 funcptr_type->length (), byte_order);
598 contents += funcptr_type->length ();
599 adjustment = extract_signed_integer (contents,
600 offset_type->length (), byte_order);
601
602 if (!gdbarch_vbit_in_delta (gdbarch))
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
618 /* GNU v3 implementation of cplus_print_method_ptr. */
619
620 static void
621 gnuv3_print_method_ptr (const gdb_byte *contents,
622 struct type *type,
623 struct ui_file *stream)
624 {
625 struct type *self_type = TYPE_SELF_TYPE (type);
626 struct gdbarch *gdbarch = self_type->arch ();
627 CORE_ADDR ptr_value;
628 LONGEST adjustment;
629 int vbit;
630
631 /* Extract the pointer to member. */
632 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
633
634 /* Check for NULL. */
635 if (ptr_value == 0 && vbit == 0)
636 {
637 gdb_printf (stream, "NULL");
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. */
650 voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
651
652 physname = gnuv3_find_method_in (self_type, voffset, adjustment);
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 {
658 gdb::unique_xmalloc_ptr<char> demangled_name
659 = gdb_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
660
661 gdb_printf (stream, "&virtual ");
662 if (demangled_name == NULL)
663 gdb_puts (physname, stream);
664 else
665 gdb_puts (demangled_name.get (), stream);
666 return;
667 }
668 }
669 else if (ptr_value != 0)
670 {
671 /* Found a non-virtual function: print out the type. */
672 gdb_puts ("(", stream);
673 c_print_type (type, "", stream, -1, 0, current_language->la_language,
674 &type_print_raw_options);
675 gdb_puts (") ", stream);
676 }
677
678 /* We didn't find it; print the raw data. */
679 if (vbit)
680 {
681 gdb_printf (stream, "&virtual table offset ");
682 print_longest (stream, 'd', 1, ptr_value);
683 }
684 else
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 }
691
692 if (adjustment)
693 {
694 gdb_printf (stream, ", this adjustment ");
695 print_longest (stream, 'd', 1, adjustment);
696 }
697 }
698
699 /* GNU v3 implementation of cplus_method_ptr_size. */
700
701 static int
702 gnuv3_method_ptr_size (struct type *type)
703 {
704 return 2 * builtin_type (type->arch ())->builtin_data_ptr->length ();
705 }
706
707 /* GNU v3 implementation of cplus_make_method_ptr. */
708
709 static void
710 gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
711 CORE_ADDR value, int is_virtual)
712 {
713 struct gdbarch *gdbarch = type->arch ();
714 int size = builtin_type (gdbarch)->builtin_data_ptr->length ();
715 enum bfd_endian byte_order = type_byte_order (type);
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
726 if (!gdbarch_vbit_in_delta (gdbarch))
727 {
728 store_unsigned_integer (contents, size, byte_order, value | is_virtual);
729 store_unsigned_integer (contents + size, size, byte_order, 0);
730 }
731 else
732 {
733 store_unsigned_integer (contents, size, byte_order, value);
734 store_unsigned_integer (contents + size, size, byte_order, is_virtual);
735 }
736 }
737
738 /* GNU v3 implementation of cplus_method_ptr_to_value. */
739
740 static struct value *
741 gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
742 {
743 struct gdbarch *gdbarch;
744 const gdb_byte *contents = method_ptr->contents ().data ();
745 CORE_ADDR ptr_value;
746 struct type *self_type, *final_type, *method_type;
747 LONGEST adjustment;
748 int vbit;
749
750 self_type = TYPE_SELF_TYPE (check_typedef (method_ptr->type ()));
751 final_type = lookup_pointer_type (self_type);
752
753 method_type = check_typedef (method_ptr->type ())->target_type ();
754
755 /* Extract the pointer to member. */
756 gdbarch = self_type->arch ();
757 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
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. */
778 *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
779 *this_p = value_ptradd (*this_p, adjustment);
780 *this_p = value_cast (final_type, *this_p);
781
782 if (vbit)
783 {
784 LONGEST voffset;
785
786 voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
787 return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
788 method_type, voffset);
789 }
790 else
791 return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
792 }
793
794 /* Objects of this type are stored in a hash table and a vector when
795 printing the vtables for a class. */
796
797 struct 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
807 /* Hash function for value_and_voffset. */
808
809 static hashval_t
810 hash_value_and_voffset (const void *p)
811 {
812 const struct value_and_voffset *o = (const struct value_and_voffset *) p;
813
814 return o->value->address () + o->value->embedded_offset ();
815 }
816
817 /* Equality function for value_and_voffset. */
818
819 static int
820 eq_value_and_voffset (const void *a, const void *b)
821 {
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;
824
825 return (ova->value->address () + ova->value->embedded_offset ()
826 == ovb->value->address () + ovb->value->embedded_offset ());
827 }
828
829 /* Comparison function for value_and_voffset. */
830
831 static bool
832 compare_value_and_voffset (const struct value_and_voffset *va,
833 const struct value_and_voffset *vb)
834 {
835 CORE_ADDR addra = (va->value->address ()
836 + va->value->embedded_offset ());
837 CORE_ADDR addrb = (vb->value->address ()
838 + vb->value->embedded_offset ());
839
840 return addra < addrb;
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
849 static void
850 compute_vtable_size (htab_t offset_hash,
851 std::vector<value_and_voffset *> *offset_vec,
852 struct value *value)
853 {
854 int i;
855 struct type *type = check_typedef (value->type ());
856 void **slot;
857 struct value_and_voffset search_vo, *current_vo;
858
859 gdb_assert (type->code () == TYPE_CODE_STRUCT);
860
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)
870 current_vo = (struct value_and_voffset *) *slot;
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;
877 offset_vec->push_back (current_vo);
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
906 static void
907 print_one_vtable (struct gdbarch *gdbarch, struct value *value,
908 int max_voffset,
909 struct value_print_options *opts)
910 {
911 int i;
912 struct type *type = check_typedef (value->type ());
913 struct value *vtable;
914 CORE_ADDR vt_addr;
915
916 vtable = gnuv3_get_vtable (gdbarch, type,
917 value->address ()
918 + value->embedded_offset ());
919 vt_addr = value_field (vtable,
920 vtable_field_virtual_functions)->address ();
921
922 gdb_printf (_("vtable for '%s' @ %s (subobject @ %s):\n"),
923 TYPE_SAFE_NAME (type),
924 paddress (gdbarch, vt_addr),
925 paddress (gdbarch, (value->address ()
926 + value->embedded_offset ())));
927
928 for (i = 0; i <= max_voffset; ++i)
929 {
930 /* Initialize it just to avoid a GCC false warning. */
931 CORE_ADDR addr = 0;
932 int got_error = 0;
933 struct value *vfn;
934
935 gdb_printf ("[%d]: ", i);
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
944 try
945 {
946 addr = value_as_address (vfn);
947 }
948 catch (const gdb_exception_error &ex)
949 {
950 fprintf_styled (gdb_stdout, metadata_style.style (),
951 _("<error: %s>"), ex.what ());
952 got_error = 1;
953 }
954
955 if (!got_error)
956 print_function_pointer_address (opts, gdbarch, addr, gdb_stdout);
957 gdb_printf ("\n");
958 }
959 }
960
961 /* Implementation of the print_vtable method. */
962
963 static void
964 gnuv3_print_vtable (struct value *value)
965 {
966 struct gdbarch *gdbarch;
967 struct type *type;
968 struct value *vtable;
969 struct value_print_options opts;
970 int count;
971
972 value = coerce_ref (value);
973 type = check_typedef (value->type ());
974 if (type->code () == TYPE_CODE_PTR)
975 {
976 value = value_ind (value);
977 type = check_typedef (value->type ());
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);
986 type = check_typedef (value->type ());
987 }
988
989 gdbarch = type->arch ();
990
991 vtable = NULL;
992 if (type->code () == TYPE_CODE_STRUCT)
993 vtable = gnuv3_get_vtable (gdbarch, type,
994 value_as_address (value_addr (value)));
995
996 if (!vtable)
997 {
998 gdb_printf (_("This object does not have a virtual function table\n"));
999 return;
1000 }
1001
1002 htab_up offset_hash (htab_create_alloc (1, hash_value_and_voffset,
1003 eq_value_and_voffset,
1004 xfree, xcalloc, xfree));
1005 std::vector<value_and_voffset *> result_vec;
1006
1007 compute_vtable_size (offset_hash.get (), &result_vec, value);
1008 std::sort (result_vec.begin (), result_vec.end (),
1009 compare_value_and_voffset);
1010
1011 count = 0;
1012 for (value_and_voffset *iter : result_vec)
1013 {
1014 if (iter->max_voffset >= 0)
1015 {
1016 if (count > 0)
1017 gdb_printf ("\n");
1018 print_one_vtable (gdbarch, iter->value, iter->max_voffset, &opts);
1019 ++count;
1020 }
1021 }
1022 }
1023
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
1030 static struct type *
1031 build_std_type_info_type (struct gdbarch *arch)
1032 {
1033 struct type *t;
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
1042 t = type_allocator (arch).new_type (TYPE_CODE_STRUCT, 0, nullptr);
1043
1044 t->alloc_fields (2);
1045
1046 offset = 0;
1047
1048 /* The vtable. */
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 }
1056
1057 /* The name. */
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
1068 t->set_name ("gdb_gnu_v3_type_info");
1069 INIT_CPLUS_SPECIFIC (t);
1070
1071 return t;
1072 }
1073
1074 /* Implement the 'get_typeid_type' method. */
1075
1076 static struct type *
1077 gnuv3_get_typeid_type (struct gdbarch *gdbarch)
1078 {
1079 struct symbol *typeinfo;
1080 struct type *typeinfo_type;
1081
1082 typeinfo = lookup_symbol ("std::type_info", NULL, STRUCT_DOMAIN,
1083 NULL).symbol;
1084 if (typeinfo == NULL)
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 }
1093 else
1094 typeinfo_type = typeinfo->type ();
1095
1096 return typeinfo_type;
1097 }
1098
1099 /* Implement the 'get_typeid' method. */
1100
1101 static struct value *
1102 gnuv3_get_typeid (struct value *value)
1103 {
1104 struct type *typeinfo_type;
1105 struct type *type;
1106 struct gdbarch *gdbarch;
1107 struct value *result;
1108 std::string type_name;
1109 gdb::unique_xmalloc_ptr<char> canonical;
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. */
1114 if (value->lval () == lval_memory)
1115 value = coerce_ref (value);
1116
1117 type = check_typedef (value->type ());
1118
1119 /* In the non_lvalue case, a reference might have slipped through
1120 here. */
1121 if (type->code () == TYPE_CODE_REF)
1122 type = check_typedef (type->target_type ());
1123
1124 /* Ignore top-level cv-qualifiers. */
1125 type = make_cv_type (0, 0, type, NULL);
1126 gdbarch = type->arch ();
1127
1128 type_name = type_to_string (type);
1129 if (type_name.empty ())
1130 error (_("cannot find typeinfo for unnamed type"));
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 *". */
1136 canonical = cp_canonicalize_string (type_name.c_str ());
1137 const char *name = (canonical == nullptr
1138 ? type_name.c_str ()
1139 : canonical.get ());
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. */
1145 if (type->code () == TYPE_CODE_STRUCT
1146 && value->lval () == lval_memory
1147 && gnuv3_dynamic_class (type))
1148 {
1149 struct value *vtable, *typeinfo_value;
1150 CORE_ADDR address = value->address () + value->embedded_offset ();
1151
1152 vtable = gnuv3_get_vtable (gdbarch, type, address);
1153 if (vtable == NULL)
1154 error (_("cannot find typeinfo for object of type '%s'"),
1155 name);
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 {
1162 std::string sym_name = std::string ("typeinfo for ") + name;
1163 bound_minimal_symbol minsym
1164 = lookup_minimal_symbol (sym_name.c_str (), NULL, NULL);
1165
1166 if (minsym.minsym == NULL)
1167 error (_("could not find typeinfo symbol for '%s'"), name);
1168
1169 result = value_at_lazy (typeinfo_type, minsym.value_address ());
1170 }
1171
1172 return result;
1173 }
1174
1175 /* Implement the 'get_typename_from_type_info' method. */
1176
1177 static std::string
1178 gnuv3_get_typename_from_type_info (struct value *type_info_ptr)
1179 {
1180 struct gdbarch *gdbarch = type_info_ptr->type ()->arch ();
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)
1195 symname = typeinfo_sym.minsym->demangled_name ();
1196 if (symname == NULL || strncmp (symname, TYPEINFO_PREFIX,
1197 TYPEINFO_PREFIX_LEN))
1198 error (_("typeinfo symbol '%s' has unexpected name"),
1199 typeinfo_sym.minsym->linkage_name ());
1200 class_name = symname + TYPEINFO_PREFIX_LEN;
1201
1202 /* Strip off @plt and version suffixes. */
1203 atsign = strchr (class_name, '@');
1204 if (atsign != NULL)
1205 return std::string (class_name, atsign - class_name);
1206 return class_name;
1207 }
1208
1209 /* Implement the 'get_type_from_type_info' method. */
1210
1211 static struct type *
1212 gnuv3_get_type_from_type_info (struct value *type_info_ptr)
1213 {
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. */
1218 std::string type_name = gnuv3_get_typename_from_type_info (type_info_ptr);
1219 expression_up expr (parse_expression (type_name.c_str ()));
1220 struct value *type_val = expr->evaluate_type ();
1221 return type_val->type ();
1222 }
1223
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
1227 static CORE_ADDR
1228 gnuv3_skip_trampoline (frame_info_ptr frame, CORE_ADDR stop_pc)
1229 {
1230 CORE_ADDR real_stop_pc, method_stop_pc, func_addr;
1231 struct gdbarch *gdbarch = get_frame_arch (frame);
1232 struct bound_minimal_symbol thunk_sym, fn_sym;
1233 struct obj_section *section;
1234 const char *thunk_name, *fn_name;
1235
1236 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
1237 if (real_stop_pc == 0)
1238 real_stop_pc = stop_pc;
1239
1240 /* Find the linker symbol for this potential thunk. */
1241 thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
1242 section = find_pc_section (real_stop_pc);
1243 if (thunk_sym.minsym == NULL || section == NULL)
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. */
1249 thunk_name = thunk_sym.minsym->demangled_name ();
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);
1255 if (fn_sym.minsym == NULL)
1256 return 0;
1257
1258 method_stop_pc = fn_sym.value_address ();
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. */
1264 func_addr = gdbarch_convert_from_func_ptr_addr
1265 (gdbarch, method_stop_pc, current_inferior ()->top_target ());
1266 if (func_addr != 0)
1267 method_stop_pc = func_addr;
1268
1269 real_stop_pc = gdbarch_skip_trampoline_code
1270 (gdbarch, frame, method_stop_pc);
1271 if (real_stop_pc == 0)
1272 real_stop_pc = method_stop_pc;
1273
1274 return real_stop_pc;
1275 }
1276
1277 /* A member function is in one these states. */
1278
1279 enum 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
1290 static definition_style
1291 get_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
1319 static bool
1320 is_user_provided_def (definition_style def)
1321 {
1322 return def == EXPLICIT || def == DEFAULTED_OUTSIDE;
1323 }
1324
1325 static bool
1326 is_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
1338 static bool
1339 is_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... */
1344 if (method_type->num_fields () < 2)
1345 return false;
1346
1347 /* ...and the second argument should be the same as the class
1348 type, with the expected type code... */
1349 struct type *arg_type = method_type->field (1).type ();
1350
1351 if (arg_type->code () != expected)
1352 return false;
1353
1354 struct type *target = check_typedef (arg_type->target_type ());
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. */
1361 for (int i = 2; i < method_type->num_fields (); i++)
1362 {
1363 arg_type = method_type->field (i).type ();
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
1380 static bool
1381 is_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
1390 static bool
1391 is_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
1398 /* Return pass-by-reference information for the given TYPE.
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
1406 For return values with non-trivial copy/move constructors or
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
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. */
1415
1416 static struct language_pass_by_ref_info
1417 gnuv3_pass_by_reference (struct type *type)
1418 {
1419 int fieldnum, fieldelem;
1420
1421 type = check_typedef (type);
1422
1423 /* Start with the default values. */
1424 struct language_pass_by_ref_info info;
1425
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;
1432
1433 /* We're only interested in things that can have methods. */
1434 if (type->code () != TYPE_CODE_STRUCT
1435 && type->code () != TYPE_CODE_UNION)
1436 return info;
1437
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
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))
1458 is_dynamic = true;
1459
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);
1465 const char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
1466 struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
1467
1468 if (name[0] == '~')
1469 {
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);
1474 }
1475 else if (is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem))
1476 || TYPE_FN_FIELD_CONSTRUCTOR (fn, fieldelem))
1477 {
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))
1503 {
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);
1510 }
1511 }
1512 }
1513
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
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
1543 of complete class type. Also ignore any static members. */
1544 for (fieldnum = 0; fieldnum < type->num_fields (); fieldnum++)
1545 if (!type->field (fieldnum).is_static ())
1546 {
1547 struct type *field_type = type->field (fieldnum).type ();
1548
1549 /* For arrays, make the decision based on the element type. */
1550 if (field_type->code () == TYPE_CODE_ARRAY)
1551 field_type = check_typedef (field_type->target_type ());
1552
1553 struct language_pass_by_ref_info field_info
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;
1560 if (!field_info.trivially_copyable)
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;
1566 }
1567
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
1576 return info;
1577 }
1578
1579 static void
1580 init_gnuv3_ops (void)
1581 {
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";
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;
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;
1593 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
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;
1598 gnu_v3_abi_ops.print_vtable = gnuv3_print_vtable;
1599 gnu_v3_abi_ops.get_typeid = gnuv3_get_typeid;
1600 gnu_v3_abi_ops.get_typeid_type = gnuv3_get_typeid_type;
1601 gnu_v3_abi_ops.get_type_from_type_info = gnuv3_get_type_from_type_info;
1602 gnu_v3_abi_ops.get_typename_from_type_info
1603 = gnuv3_get_typename_from_type_info;
1604 gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
1605 gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
1606 }
1607
1608 void _initialize_gnu_v3_abi ();
1609 void
1610 _initialize_gnu_v3_abi ()
1611 {
1612 init_gnuv3_ops ();
1613
1614 register_cp_abi (&gnu_v3_abi_ops);
1615 set_cp_abi_as_auto_default (gnu_v3_abi_ops.shortname);
1616 }