testsuite, threads: fix LD_LIBRARY_PATH in 'tls-sepdebug.exp'
[binutils-gdb.git] / gdb / gnu-v3-abi.c
blobaefbee5427030a7d7576fc0058dfad51d2952296
1 /* Abstraction of GNU v3 abi.
2 Contributed by Jim Blandy <jimb@redhat.com>
4 Copyright (C) 2001-2024 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
21 #include "extract-store-integer.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"
37 static struct cp_abi_ops gnu_v3_abi_ops;
39 /* A gdbarch key for std::type_info, in the event that it can't be
40 found in the debug info. */
42 static const registry<gdbarch>::key<struct type> std_type_info_gdbarch_data;
45 static int
46 gnuv3_is_vtable_name (const char *name)
48 return startswith (name, "_ZTV");
51 static int
52 gnuv3_is_operator_name (const char *name)
54 return startswith (name, CP_OPERATOR_STR);
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:
62 struct gdb_gnu_v3_abi_vtable {
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];
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;
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;
80 / * Virtual table pointers in objects point here. * /
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]) ();
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.
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;
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
107 /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
108 described above, laid out appropriately for ARCH.
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)
115 struct type *t;
116 int offset;
118 struct type *result = vtable_type_gdbarch_data.get (arch);
119 if (result != nullptr)
120 return result;
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;
127 type_allocator alloc (arch);
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");
133 t = alloc.new_type (TYPE_CODE_STRUCT, 0, nullptr);
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. */
140 /* Build the field list. */
141 t->alloc_fields (4);
143 offset = 0;
145 /* ptrdiff_t vcall_and_vbase_offsets[0]; */
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 ();
154 /* ptrdiff_t offset_to_top; */
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 ();
163 /* void *type_info; */
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 ();
172 /* void (*virtual_functions[0]) (); */
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 ();
181 t->set_length (offset);
183 t->set_name ("gdb_gnu_v3_abi_vtable");
184 INIT_CPLUS_SPECIFIC (t);
186 result = make_type_with_address_space (t, TYPE_INSTANCE_FLAG_CODE_SPACE);
187 vtable_type_gdbarch_data.set (arch, result);
188 return result;
192 /* Return the ptrdiff_t type used in the vtable type. */
193 static struct type *
194 vtable_ptrdiff_type (struct gdbarch *gdbarch)
196 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
198 /* The "offset_to_top" field has the appropriate (ptrdiff_t) type. */
199 return vtable_type->field (vtable_field_offset_to_top).type ();
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)
208 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
210 return (vtable_type->field (vtable_field_virtual_functions).loc_bitpos ()
211 / TARGET_CHAR_BIT);
215 /* Determine whether structure TYPE is a dynamic class. Cache the
216 result. */
218 static int
219 gnuv3_dynamic_class (struct type *type)
221 int fieldnum, fieldelem;
223 type = check_typedef (type);
224 gdb_assert (type->code () == TYPE_CODE_STRUCT
225 || type->code () == TYPE_CODE_UNION);
227 if (type->code () == TYPE_CODE_UNION)
228 return 0;
230 if (TYPE_CPLUS_DYNAMIC (type))
231 return TYPE_CPLUS_DYNAMIC (type) == 1;
233 ALLOCATE_CPLUS_STRUCT_TYPE (type);
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 ()))
239 TYPE_CPLUS_DYNAMIC (type) = 1;
240 return 1;
243 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
244 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
245 fieldelem++)
247 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, fieldnum);
249 if (TYPE_FN_FIELD_VIRTUAL_P (f, fieldelem))
251 TYPE_CPLUS_DYNAMIC (type) = 1;
252 return 1;
256 TYPE_CPLUS_DYNAMIC (type) = -1;
257 return 0;
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. */
264 static struct value *
265 gnuv3_get_vtable (struct gdbarch *gdbarch,
266 struct type *container_type, CORE_ADDR container_addr)
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;
273 container_type = check_typedef (container_type);
274 gdb_assert (container_type->code () == TYPE_CODE_STRUCT);
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;
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.
285 We avoid using value_contents on principle, because the object might
286 be large. */
288 /* Find the type "pointer to virtual table". */
289 vtable_pointer_type = lookup_pointer_type (vtable_type);
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);
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));
303 static struct type *
304 gnuv3_rtti_type (struct value *value,
305 int *full_p, LONGEST *top_p, int *using_enc_p)
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;
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;
322 /* Determine architecture. */
323 gdbarch = values_type->arch ();
325 if (using_enc_p)
326 *using_enc_p = 0;
328 vtable = gnuv3_get_vtable (gdbarch, values_type,
329 value_as_address (value_addr (value)));
330 if (vtable == NULL)
331 return NULL;
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;
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 "))
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;
355 class_name = vtable_symbol_name + 11;
357 /* Strip off @plt and version suffixes. */
358 atsign = strchr (class_name, '@');
359 if (atsign != NULL)
361 char *copy;
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;
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;
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));
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;
389 /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
390 function, of type FNTYPE. */
392 static struct value *
393 gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
394 struct type *fntype, int vtable_index)
396 struct value *vtable, *vfn;
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);
403 /* Fetch the appropriate function pointer from the vtable. */
404 vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
405 vtable_index);
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);
415 /* Cast the function pointer to the appropriate type. */
416 vfn = value_cast (lookup_pointer_type (fntype), vfn);
418 return vfn;
421 /* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h
422 for a description of the arguments. */
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)
429 struct type *values_type = check_typedef ((*value_p)->type ());
430 struct gdbarch *gdbarch;
432 /* Some simple sanity checks. */
433 if (values_type->code () != TYPE_CODE_STRUCT)
434 error (_("Only classes can have virtual functions."));
436 /* Determine architecture. */
437 gdbarch = values_type->arch ();
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);
445 return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
446 TYPE_FN_FIELD_VOFFSET (f, j));
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.
455 -1 is returned on error. */
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)
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;
468 /* Determine architecture. */
469 gdbarch = type->arch ();
470 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
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;
477 /* If we have a DWARF expression for the offset, evaluate it. */
478 if (type->field (index).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
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 ();
485 struct dynamic_prop prop;
486 prop.set_locexpr (&baton);
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;
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);
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?)"));
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 ());
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;
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. */
526 static const char *
527 gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
528 LONGEST adjustment)
530 int i;
532 /* Search this class first. */
533 if (adjustment == 0)
535 int len;
537 len = TYPE_NFN_FIELDS (domain);
538 for (i = 0; i < len; i++)
540 int len2, j;
541 struct fn_field *f;
543 f = TYPE_FN_FIELDLIST1 (domain, i);
544 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
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);
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++)
557 int pos;
558 struct type *basetype;
560 if (BASETYPE_VIA_VIRTUAL (domain, i))
561 continue;
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);
571 return NULL;
574 /* Decode GNU v3 method pointer. */
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)
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;
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);
602 if (!gdbarch_vbit_in_delta (gdbarch))
604 vbit = voffset & 1;
605 voffset = voffset ^ vbit;
607 else
609 vbit = adjustment & 1;
610 adjustment = adjustment >> 1;
613 *value_p = vbit? voffset : ptr_value;
614 *adjustment_p = adjustment;
615 return vbit;
618 /* GNU v3 implementation of cplus_print_method_ptr. */
620 static void
621 gnuv3_print_method_ptr (const gdb_byte *contents,
622 struct type *type,
623 struct ui_file *stream)
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;
631 /* Extract the pointer to member. */
632 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
634 /* Check for NULL. */
635 if (ptr_value == 0 && vbit == 0)
637 gdb_printf (stream, "NULL");
638 return;
641 /* Search for a virtual method. */
642 if (vbit)
644 CORE_ADDR voffset;
645 const char *physname;
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 ();
652 physname = gnuv3_find_method_in (self_type, voffset, adjustment);
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)
658 gdb::unique_xmalloc_ptr<char> demangled_name
659 = gdb_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
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;
669 else if (ptr_value != 0)
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);
678 /* We didn't find it; print the raw data. */
679 if (vbit)
681 gdb_printf (stream, "&virtual table offset ");
682 print_longest (stream, 'd', 1, ptr_value);
684 else
686 struct value_print_options opts;
688 get_user_print_options (&opts);
689 print_address_demangle (&opts, gdbarch, ptr_value, stream, demangle);
692 if (adjustment)
694 gdb_printf (stream, ", this adjustment ");
695 print_longest (stream, 'd', 1, adjustment);
699 /* GNU v3 implementation of cplus_method_ptr_size. */
701 static int
702 gnuv3_method_ptr_size (struct type *type)
704 return 2 * builtin_type (type->arch ())->builtin_data_ptr->length ();
707 /* GNU v3 implementation of cplus_make_method_ptr. */
709 static void
710 gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
711 CORE_ADDR value, int is_virtual)
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);
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. */
726 if (!gdbarch_vbit_in_delta (gdbarch))
728 store_unsigned_integer (contents, size, byte_order, value | is_virtual);
729 store_unsigned_integer (contents + size, size, byte_order, 0);
731 else
733 store_unsigned_integer (contents, size, byte_order, value);
734 store_unsigned_integer (contents + size, size, byte_order, is_virtual);
738 /* GNU v3 implementation of cplus_method_ptr_to_value. */
740 static struct value *
741 gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
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;
750 self_type = TYPE_SELF_TYPE (check_typedef (method_ptr->type ()));
751 final_type = lookup_pointer_type (self_type);
753 method_type = check_typedef (method_ptr->type ())->target_type ();
755 /* Extract the pointer to member. */
756 gdbarch = self_type->arch ();
757 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
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);
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.
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.
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);
782 if (vbit)
784 LONGEST voffset;
786 voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
787 return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
788 method_type, voffset);
790 else
791 return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
794 /* Objects of this type are stored in a hash table and a vector when
795 printing the vtables for a class. */
797 struct value_and_voffset
799 /* The value representing the object. */
800 struct value *value;
802 /* The maximum vtable offset we've found for any object at this
803 offset in the outermost object. */
804 int max_voffset;
807 /* Hash function for value_and_voffset. */
809 static hashval_t
810 hash_value_and_voffset (const void *p)
812 const struct value_and_voffset *o = (const struct value_and_voffset *) p;
814 return o->value->address () + o->value->embedded_offset ();
817 /* Equality function for value_and_voffset. */
819 static int
820 eq_value_and_voffset (const void *a, const void *b)
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;
825 return (ova->value->address () + ova->value->embedded_offset ()
826 == ovb->value->address () + ovb->value->embedded_offset ());
829 /* Comparison function for value_and_voffset. */
831 static bool
832 compare_value_and_voffset (const struct value_and_voffset *va,
833 const struct value_and_voffset *vb)
835 CORE_ADDR addra = (va->value->address ()
836 + va->value->embedded_offset ());
837 CORE_ADDR addrb = (vb->value->address ()
838 + vb->value->embedded_offset ());
840 return addra < addrb;
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. */
849 static void
850 compute_vtable_size (htab_t offset_hash,
851 std::vector<value_and_voffset *> *offset_vec,
852 struct value *value)
854 int i;
855 struct type *type = check_typedef (value->type ());
856 void **slot;
857 struct value_and_voffset search_vo, *current_vo;
859 gdb_assert (type->code () == TYPE_CODE_STRUCT);
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;
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
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);
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)
884 int j;
885 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, i);
887 for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (type, i); ++j)
889 if (TYPE_FN_FIELD_VIRTUAL_P (fn, j))
891 int voffset = TYPE_FN_FIELD_VOFFSET (fn, j);
893 if (voffset > current_vo->max_voffset)
894 current_vo->max_voffset = voffset;
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));
904 /* Helper for gnuv3_print_vtable that prints a single vtable. */
906 static void
907 print_one_vtable (struct gdbarch *gdbarch, struct value *value,
908 int max_voffset,
909 struct value_print_options *opts)
911 int i;
912 struct type *type = check_typedef (value->type ());
913 struct value *vtable;
914 CORE_ADDR vt_addr;
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 ();
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 ())));
928 for (i = 0; i <= max_voffset; ++i)
930 /* Initialize it just to avoid a GCC false warning. */
931 CORE_ADDR addr = 0;
932 int got_error = 0;
933 struct value *vfn;
935 gdb_printf ("[%d]: ", i);
937 vfn = value_subscript (value_field (vtable,
938 vtable_field_virtual_functions),
941 if (gdbarch_vtable_function_descriptors (gdbarch))
942 vfn = value_addr (vfn);
946 addr = value_as_address (vfn);
948 catch (const gdb_exception_error &ex)
950 fprintf_styled (gdb_stdout, metadata_style.style (),
951 _("<error: %s>"), ex.what ());
952 got_error = 1;
955 if (!got_error)
956 print_function_pointer_address (opts, gdbarch, addr, gdb_stdout);
957 gdb_printf ("\n");
961 /* Implementation of the print_vtable method. */
963 static void
964 gnuv3_print_vtable (struct value *value)
966 struct gdbarch *gdbarch;
967 struct type *type;
968 struct value *vtable;
969 struct value_print_options opts;
970 int count;
972 value = coerce_ref (value);
973 type = check_typedef (value->type ());
974 if (type->code () == TYPE_CODE_PTR)
976 value = value_ind (value);
977 type = check_typedef (value->type ());
980 get_user_print_options (&opts);
982 /* Respect 'set print object'. */
983 if (opts.objectprint)
985 value = value_full_object (value, NULL, 0, 0, 0);
986 type = check_typedef (value->type ());
989 gdbarch = type->arch ();
991 vtable = NULL;
992 if (type->code () == TYPE_CODE_STRUCT)
993 vtable = gnuv3_get_vtable (gdbarch, type,
994 value_as_address (value_addr (value)));
996 if (!vtable)
998 gdb_printf (_("This object does not have a virtual function table\n"));
999 return;
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;
1007 compute_vtable_size (offset_hash.get (), &result_vec, value);
1008 std::sort (result_vec.begin (), result_vec.end (),
1009 compare_value_and_voffset);
1011 count = 0;
1012 for (value_and_voffset *iter : result_vec)
1014 if (iter->max_voffset >= 0)
1016 if (count > 0)
1017 gdb_printf ("\n");
1018 print_one_vtable (gdbarch, iter->value, iter->max_voffset, &opts);
1019 ++count;
1024 /* Return a GDB type representing `struct std::type_info', laid out
1025 appropriately for ARCH.
1027 We use this function as the gdbarch per-architecture data
1028 initialization function. */
1030 static struct type *
1031 build_std_type_info_type (struct gdbarch *arch)
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);
1042 t = type_allocator (arch).new_type (TYPE_CODE_STRUCT, 0, nullptr);
1044 t->alloc_fields (2);
1046 offset = 0;
1048 /* The vtable. */
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 ();
1057 /* The name. */
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 ();
1066 t->set_length (offset);
1068 t->set_name ("gdb_gnu_v3_type_info");
1069 INIT_CPLUS_SPECIFIC (t);
1071 return t;
1074 /* Implement the 'get_typeid_type' method. */
1076 static struct type *
1077 gnuv3_get_typeid_type (struct gdbarch *gdbarch)
1079 struct symbol *typeinfo;
1080 struct type *typeinfo_type;
1082 typeinfo = lookup_symbol ("std::type_info", NULL, SEARCH_STRUCT_DOMAIN,
1083 NULL).symbol;
1084 if (typeinfo == NULL)
1086 typeinfo_type = std_type_info_gdbarch_data.get (gdbarch);
1087 if (typeinfo_type == nullptr)
1089 typeinfo_type = build_std_type_info_type (gdbarch);
1090 std_type_info_gdbarch_data.set (gdbarch, typeinfo_type);
1093 else
1094 typeinfo_type = typeinfo->type ();
1096 return typeinfo_type;
1099 /* Implement the 'get_typeid' method. */
1101 static struct value *
1102 gnuv3_get_typeid (struct value *value)
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;
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);
1117 type = check_typedef (value->type ());
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 ());
1124 /* Ignore top-level cv-qualifiers. */
1125 type = make_cv_type (0, 0, type, NULL);
1126 gdbarch = type->arch ();
1128 type_name = type_to_string (type);
1129 if (type_name.empty ())
1130 error (_("cannot find typeinfo for unnamed type"));
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 ());
1141 typeinfo_type = gnuv3_get_typeid_type (gdbarch);
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))
1149 struct value *vtable, *typeinfo_value;
1150 CORE_ADDR address = value->address () + value->embedded_offset ();
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));
1160 else
1162 std::string sym_name = std::string ("typeinfo for ") + name;
1163 bound_minimal_symbol minsym
1164 = lookup_minimal_symbol (current_program_space, sym_name.c_str ());
1166 if (minsym.minsym == NULL)
1167 error (_("could not find typeinfo symbol for '%s'"), name);
1169 result = value_at_lazy (typeinfo_type, minsym.value_address ());
1172 return result;
1175 /* Implement the 'get_typename_from_type_info' method. */
1177 static std::string
1178 gnuv3_get_typename_from_type_info (struct value *type_info_ptr)
1180 struct gdbarch *gdbarch = type_info_ptr->type ()->arch ();
1181 CORE_ADDR addr;
1182 const char *symname;
1183 const char *class_name;
1184 const char *atsign;
1186 addr = value_as_address (type_info_ptr);
1187 bound_minimal_symbol typeinfo_sym = lookup_minimal_symbol_by_pc (addr);
1188 if (typeinfo_sym.minsym == NULL)
1189 error (_("could not find minimal symbol for typeinfo address %s"),
1190 paddress (gdbarch, addr));
1192 #define TYPEINFO_PREFIX "typeinfo for "
1193 #define TYPEINFO_PREFIX_LEN (sizeof (TYPEINFO_PREFIX) - 1)
1194 symname = typeinfo_sym.minsym->demangled_name ();
1195 if (symname == NULL || strncmp (symname, TYPEINFO_PREFIX,
1196 TYPEINFO_PREFIX_LEN))
1197 error (_("typeinfo symbol '%s' has unexpected name"),
1198 typeinfo_sym.minsym->linkage_name ());
1199 class_name = symname + TYPEINFO_PREFIX_LEN;
1201 /* Strip off @plt and version suffixes. */
1202 atsign = strchr (class_name, '@');
1203 if (atsign != NULL)
1204 return std::string (class_name, atsign - class_name);
1205 return class_name;
1208 /* Implement the 'get_type_from_type_info' method. */
1210 static struct type *
1211 gnuv3_get_type_from_type_info (struct value *type_info_ptr)
1213 /* We have to parse the type name, since in general there is not a
1214 symbol for a type. This is somewhat bogus since there may be a
1215 mis-parse. Another approach might be to re-use the demangler's
1216 internal form to reconstruct the type somehow. */
1217 std::string type_name = gnuv3_get_typename_from_type_info (type_info_ptr);
1218 expression_up expr (parse_expression (type_name.c_str ()));
1219 struct value *type_val = expr->evaluate_type ();
1220 return type_val->type ();
1223 /* Determine if we are currently in a C++ thunk. If so, get the address
1224 of the routine we are thunking to and continue to there instead. */
1226 static CORE_ADDR
1227 gnuv3_skip_trampoline (const frame_info_ptr &frame, CORE_ADDR stop_pc)
1229 CORE_ADDR real_stop_pc, method_stop_pc, func_addr;
1230 struct gdbarch *gdbarch = get_frame_arch (frame);
1231 struct obj_section *section;
1232 const char *thunk_name, *fn_name;
1234 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
1235 if (real_stop_pc == 0)
1236 real_stop_pc = stop_pc;
1238 /* Find the linker symbol for this potential thunk. */
1239 bound_minimal_symbol thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
1240 section = find_pc_section (real_stop_pc);
1241 if (thunk_sym.minsym == NULL || section == NULL)
1242 return 0;
1244 /* The symbol's demangled name should be something like "virtual
1245 thunk to FUNCTION", where FUNCTION is the name of the function
1246 being thunked to. */
1247 thunk_name = thunk_sym.minsym->demangled_name ();
1248 if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
1249 return 0;
1251 fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
1252 bound_minimal_symbol fn_sym
1253 = lookup_minimal_symbol (current_program_space, fn_name, section->objfile);
1254 if (fn_sym.minsym == NULL)
1255 return 0;
1257 method_stop_pc = fn_sym.value_address ();
1259 /* Some targets have minimal symbols pointing to function descriptors
1260 (powerpc 64 for example). Make sure to retrieve the address
1261 of the real function from the function descriptor before passing on
1262 the address to other layers of GDB. */
1263 func_addr = gdbarch_convert_from_func_ptr_addr
1264 (gdbarch, method_stop_pc, current_inferior ()->top_target ());
1265 if (func_addr != 0)
1266 method_stop_pc = func_addr;
1268 real_stop_pc = gdbarch_skip_trampoline_code
1269 (gdbarch, frame, method_stop_pc);
1270 if (real_stop_pc == 0)
1271 real_stop_pc = method_stop_pc;
1273 return real_stop_pc;
1276 /* A member function is in one these states. */
1278 enum definition_style
1280 DOES_NOT_EXIST_IN_SOURCE,
1281 DEFAULTED_INSIDE,
1282 DEFAULTED_OUTSIDE,
1283 DELETED,
1284 EXPLICIT,
1287 /* Return how the given field is defined. */
1289 static definition_style
1290 get_def_style (struct fn_field *fn, int fieldelem)
1292 if (TYPE_FN_FIELD_DELETED (fn, fieldelem))
1293 return DELETED;
1295 if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
1296 return DOES_NOT_EXIST_IN_SOURCE;
1298 switch (TYPE_FN_FIELD_DEFAULTED (fn, fieldelem))
1300 case DW_DEFAULTED_no:
1301 return EXPLICIT;
1302 case DW_DEFAULTED_in_class:
1303 return DEFAULTED_INSIDE;
1304 case DW_DEFAULTED_out_of_class:
1305 return DEFAULTED_OUTSIDE;
1306 default:
1307 break;
1310 return EXPLICIT;
1313 /* Helper functions to determine whether the given definition style
1314 denotes that the definition is user-provided or implicit.
1315 Being defaulted outside the class decl counts as an explicit
1316 user-definition, while being defaulted inside is implicit. */
1318 static bool
1319 is_user_provided_def (definition_style def)
1321 return def == EXPLICIT || def == DEFAULTED_OUTSIDE;
1324 static bool
1325 is_implicit_def (definition_style def)
1327 return def == DOES_NOT_EXIST_IN_SOURCE || def == DEFAULTED_INSIDE;
1330 /* Helper function to decide if METHOD_TYPE is a copy/move
1331 constructor type for CLASS_TYPE. EXPECTED is the expected
1332 type code for the "right-hand-side" argument.
1333 This function is supposed to be used by the IS_COPY_CONSTRUCTOR_TYPE
1334 and IS_MOVE_CONSTRUCTOR_TYPE functions below. Normally, you should
1335 not need to call this directly. */
1337 static bool
1338 is_copy_or_move_constructor_type (struct type *class_type,
1339 struct type *method_type,
1340 type_code expected)
1342 /* The method should take at least two arguments... */
1343 if (method_type->num_fields () < 2)
1344 return false;
1346 /* ...and the second argument should be the same as the class
1347 type, with the expected type code... */
1348 struct type *arg_type = method_type->field (1).type ();
1350 if (arg_type->code () != expected)
1351 return false;
1353 struct type *target = check_typedef (arg_type->target_type ());
1354 if (!(class_types_same_p (target, class_type)))
1355 return false;
1357 /* ...and if any of the remaining arguments don't have a default value
1358 then this is not a copy or move constructor, but just a
1359 constructor. */
1360 for (int i = 2; i < method_type->num_fields (); i++)
1362 arg_type = method_type->field (i).type ();
1363 /* FIXME aktemur/2019-10-31: As of this date, neither
1364 clang++-7.0.0 nor g++-8.2.0 produce a DW_AT_default_value
1365 attribute. GDB is also not set to read this attribute, yet.
1366 Hence, we immediately return false if there are more than
1367 2 parameters.
1368 GCC bug link:
1369 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42959
1371 return false;
1374 return true;
1377 /* Return true if METHOD_TYPE is a copy ctor type for CLASS_TYPE. */
1379 static bool
1380 is_copy_constructor_type (struct type *class_type,
1381 struct type *method_type)
1383 return is_copy_or_move_constructor_type (class_type, method_type,
1384 TYPE_CODE_REF);
1387 /* Return true if METHOD_TYPE is a move ctor type for CLASS_TYPE. */
1389 static bool
1390 is_move_constructor_type (struct type *class_type,
1391 struct type *method_type)
1393 return is_copy_or_move_constructor_type (class_type, method_type,
1394 TYPE_CODE_RVALUE_REF);
1397 /* Return pass-by-reference information for the given TYPE.
1399 The rule in the v3 ABI document comes from section 3.1.1. If the
1400 type has a non-trivial copy constructor or destructor, then the
1401 caller must make a copy (by calling the copy constructor if there
1402 is one or perform the copy itself otherwise), pass the address of
1403 the copy, and then destroy the temporary (if necessary).
1405 For return values with non-trivial copy/move constructors or
1406 destructors, space will be allocated in the caller, and a pointer
1407 will be passed as the first argument (preceding "this").
1409 We don't have a bulletproof mechanism for determining whether a
1410 constructor or destructor is trivial. For GCC and DWARF5 debug
1411 information, we can check the calling_convention attribute,
1412 the 'artificial' flag, the 'defaulted' attribute, and the
1413 'deleted' attribute. */
1415 static struct language_pass_by_ref_info
1416 gnuv3_pass_by_reference (struct type *type)
1418 int fieldnum, fieldelem;
1420 type = check_typedef (type);
1422 /* Start with the default values. */
1423 struct language_pass_by_ref_info info;
1425 bool has_cc_attr = false;
1426 bool is_pass_by_value = false;
1427 bool is_dynamic = false;
1428 definition_style cctor_def = DOES_NOT_EXIST_IN_SOURCE;
1429 definition_style dtor_def = DOES_NOT_EXIST_IN_SOURCE;
1430 definition_style mctor_def = DOES_NOT_EXIST_IN_SOURCE;
1432 /* We're only interested in things that can have methods. */
1433 if (type->code () != TYPE_CODE_STRUCT
1434 && type->code () != TYPE_CODE_UNION)
1435 return info;
1437 /* The compiler may have emitted the calling convention attribute.
1438 Note: GCC does not produce this attribute as of version 9.2.1.
1439 Bug link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92418 */
1440 if (TYPE_CPLUS_CALLING_CONVENTION (type) == DW_CC_pass_by_value)
1442 has_cc_attr = true;
1443 is_pass_by_value = true;
1444 /* Do not return immediately. We have to find out if this type
1445 is copy_constructible and destructible. */
1448 if (TYPE_CPLUS_CALLING_CONVENTION (type) == DW_CC_pass_by_reference)
1450 has_cc_attr = true;
1451 is_pass_by_value = false;
1454 /* A dynamic class has a non-trivial copy constructor.
1455 See c++98 section 12.8 Copying class objects [class.copy]. */
1456 if (gnuv3_dynamic_class (type))
1457 is_dynamic = true;
1459 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
1460 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
1461 fieldelem++)
1463 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
1464 const char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
1465 struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
1467 if (name[0] == '~')
1469 /* We've found a destructor.
1470 There should be at most one dtor definition. */
1471 gdb_assert (dtor_def == DOES_NOT_EXIST_IN_SOURCE);
1472 dtor_def = get_def_style (fn, fieldelem);
1474 else if (is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem))
1475 || TYPE_FN_FIELD_CONSTRUCTOR (fn, fieldelem))
1477 /* FIXME drow/2007-09-23: We could do this using the name of
1478 the method and the name of the class instead of dealing
1479 with the mangled name. We don't have a convenient function
1480 to strip off both leading scope qualifiers and trailing
1481 template arguments yet. */
1482 if (is_copy_constructor_type (type, fieldtype))
1484 /* There may be more than one cctors. E.g.: one that
1485 take a const parameter and another that takes a
1486 non-const parameter. Such as:
1488 class K {
1489 K (const K &k)...
1490 K (K &k)...
1493 It is sufficient for the type to be non-trivial
1494 even only one of the cctors is explicit.
1495 Therefore, update the cctor_def value in the
1496 implicit -> explicit direction, not backwards. */
1498 if (is_implicit_def (cctor_def))
1499 cctor_def = get_def_style (fn, fieldelem);
1501 else if (is_move_constructor_type (type, fieldtype))
1503 /* Again, there may be multiple move ctors. Update the
1504 mctor_def value if we found an explicit def and the
1505 existing one is not explicit. Otherwise retain the
1506 existing value. */
1507 if (is_implicit_def (mctor_def))
1508 mctor_def = get_def_style (fn, fieldelem);
1513 bool cctor_implicitly_deleted
1514 = (mctor_def != DOES_NOT_EXIST_IN_SOURCE
1515 && cctor_def == DOES_NOT_EXIST_IN_SOURCE);
1517 bool cctor_explicitly_deleted = (cctor_def == DELETED);
1519 if (cctor_implicitly_deleted || cctor_explicitly_deleted)
1520 info.copy_constructible = false;
1522 if (dtor_def == DELETED)
1523 info.destructible = false;
1525 info.trivially_destructible = is_implicit_def (dtor_def);
1527 info.trivially_copy_constructible
1528 = (is_implicit_def (cctor_def)
1529 && !is_dynamic);
1531 info.trivially_copyable
1532 = (info.trivially_copy_constructible
1533 && info.trivially_destructible
1534 && !is_user_provided_def (mctor_def));
1536 /* Even if all the constructors and destructors were artificial, one
1537 of them may have invoked a non-artificial constructor or
1538 destructor in a base class. If any base class needs to be passed
1539 by reference, so does this class. Similarly for members, which
1540 are constructed whenever this class is. We do not need to worry
1541 about recursive loops here, since we are only looking at members
1542 of complete class type. Also ignore any static members. */
1543 for (fieldnum = 0; fieldnum < type->num_fields (); fieldnum++)
1544 if (!type->field (fieldnum).is_static ())
1546 struct type *field_type = type->field (fieldnum).type ();
1548 /* For arrays, make the decision based on the element type. */
1549 if (field_type->code () == TYPE_CODE_ARRAY)
1550 field_type = check_typedef (field_type->target_type ());
1552 struct language_pass_by_ref_info field_info
1553 = gnuv3_pass_by_reference (field_type);
1555 if (!field_info.copy_constructible)
1556 info.copy_constructible = false;
1557 if (!field_info.destructible)
1558 info.destructible = false;
1559 if (!field_info.trivially_copyable)
1560 info.trivially_copyable = false;
1561 if (!field_info.trivially_copy_constructible)
1562 info.trivially_copy_constructible = false;
1563 if (!field_info.trivially_destructible)
1564 info.trivially_destructible = false;
1567 /* Consistency check. */
1568 if (has_cc_attr && info.trivially_copyable != is_pass_by_value)
1570 /* DWARF CC attribute is not the same as the inferred value;
1571 use the DWARF attribute. */
1572 info.trivially_copyable = is_pass_by_value;
1575 return info;
1578 static void
1579 init_gnuv3_ops (void)
1581 gnu_v3_abi_ops.shortname = "gnu-v3";
1582 gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
1583 gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
1584 gnu_v3_abi_ops.is_destructor_name =
1585 (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
1586 gnu_v3_abi_ops.is_constructor_name =
1587 (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
1588 gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
1589 gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
1590 gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
1591 gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
1592 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
1593 gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
1594 gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
1595 gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
1596 gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
1597 gnu_v3_abi_ops.print_vtable = gnuv3_print_vtable;
1598 gnu_v3_abi_ops.get_typeid = gnuv3_get_typeid;
1599 gnu_v3_abi_ops.get_typeid_type = gnuv3_get_typeid_type;
1600 gnu_v3_abi_ops.get_type_from_type_info = gnuv3_get_type_from_type_info;
1601 gnu_v3_abi_ops.get_typename_from_type_info
1602 = gnuv3_get_typename_from_type_info;
1603 gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
1604 gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
1607 void _initialize_gnu_v3_abi ();
1608 void
1609 _initialize_gnu_v3_abi ()
1611 init_gnuv3_ops ();
1613 register_cp_abi (&gnu_v3_abi_ops);
1614 set_cp_abi_as_auto_default (gnu_v3_abi_ops.shortname);