Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / gnu-v3-abi.c
blob90f5b29dd8e9dd20873b74fe242f2c1aaebfe531
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 "language.h"
22 #include "value.h"
23 #include "cp-abi.h"
24 #include "cp-support.h"
25 #include "demangle.h"
26 #include "dwarf2.h"
27 #include "objfiles.h"
28 #include "valprint.h"
29 #include "c-lang.h"
30 #include "typeprint.h"
31 #include <algorithm>
32 #include "cli/cli-style.h"
33 #include "dwarf2/loc.h"
34 #include "inferior.h"
36 static struct cp_abi_ops gnu_v3_abi_ops;
38 /* A gdbarch key for std::type_info, in the event that it can't be
39 found in the debug info. */
41 static const registry<gdbarch>::key<struct type> std_type_info_gdbarch_data;
44 static int
45 gnuv3_is_vtable_name (const char *name)
47 return startswith (name, "_ZTV");
50 static int
51 gnuv3_is_operator_name (const char *name)
53 return startswith (name, CP_OPERATOR_STR);
57 /* To help us find the components of a vtable, we build ourselves a
58 GDB type object representing the vtable structure. Following the
59 V3 ABI, it goes something like this:
61 struct gdb_gnu_v3_abi_vtable {
63 / * An array of virtual call and virtual base offsets. The real
64 length of this array depends on the class hierarchy; we use
65 negative subscripts to access the elements. Yucky, but
66 better than the alternatives. * /
67 ptrdiff_t vcall_and_vbase_offsets[0];
69 / * The offset from a virtual pointer referring to this table
70 to the top of the complete object. * /
71 ptrdiff_t offset_to_top;
73 / * The type_info pointer for this class. This is really a
74 std::type_info *, but GDB doesn't really look at the
75 type_info object itself, so we don't bother to get the type
76 exactly right. * /
77 void *type_info;
79 / * Virtual table pointers in objects point here. * /
81 / * Virtual function pointers. Like the vcall/vbase array, the
82 real length of this table depends on the class hierarchy. * /
83 void (*virtual_functions[0]) ();
87 The catch, of course, is that the exact layout of this table
88 depends on the ABI --- word size, endianness, alignment, etc. So
89 the GDB type object is actually a per-architecture kind of thing.
91 vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
92 which refers to the struct type * for this structure, laid out
93 appropriately for the architecture. */
94 static const registry<gdbarch>::key<struct type> vtable_type_gdbarch_data;
97 /* Human-readable names for the numbers of the fields above. */
98 enum {
99 vtable_field_vcall_and_vbase_offsets,
100 vtable_field_offset_to_top,
101 vtable_field_type_info,
102 vtable_field_virtual_functions
106 /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
107 described above, laid out appropriately for ARCH.
109 We use this function as the gdbarch per-architecture data
110 initialization function. */
111 static struct type *
112 get_gdb_vtable_type (struct gdbarch *arch)
114 struct type *t;
115 int offset;
117 struct type *result = vtable_type_gdbarch_data.get (arch);
118 if (result != nullptr)
119 return result;
121 struct type *void_ptr_type
122 = builtin_type (arch)->builtin_data_ptr;
123 struct type *ptr_to_void_fn_type
124 = builtin_type (arch)->builtin_func_ptr;
126 type_allocator alloc (arch);
128 /* ARCH can't give us the true ptrdiff_t type, so we guess. */
129 struct type *ptrdiff_type
130 = init_integer_type (alloc, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
132 t = alloc.new_type (TYPE_CODE_STRUCT, 0, nullptr);
134 /* We assume no padding is necessary, since GDB doesn't know
135 anything about alignment at the moment. If this assumption bites
136 us, we should add a gdbarch method which, given a type, returns
137 the alignment that type requires, and then use that here. */
139 /* Build the field list. */
140 t->alloc_fields (4);
142 offset = 0;
144 /* ptrdiff_t vcall_and_vbase_offsets[0]; */
146 struct field &field0 = t->field (0);
147 field0.set_name ("vcall_and_vbase_offsets");
148 field0.set_type (lookup_array_range_type (ptrdiff_type, 0, -1));
149 field0.set_loc_bitpos (offset * TARGET_CHAR_BIT);
150 offset += field0.type ()->length ();
153 /* ptrdiff_t offset_to_top; */
155 struct field &field1 = t->field (1);
156 field1.set_name ("offset_to_top");
157 field1.set_type (ptrdiff_type);
158 field1.set_loc_bitpos (offset * TARGET_CHAR_BIT);
159 offset += field1.type ()->length ();
162 /* void *type_info; */
164 struct field &field2 = t->field (2);
165 field2.set_name ("type_info");
166 field2.set_type (void_ptr_type);
167 field2.set_loc_bitpos (offset * TARGET_CHAR_BIT);
168 offset += field2.type ()->length ();
171 /* void (*virtual_functions[0]) (); */
173 struct field &field3 = t->field (3);
174 field3.set_name ("virtual_functions");
175 field3.set_type (lookup_array_range_type (ptr_to_void_fn_type, 0, -1));
176 field3.set_loc_bitpos (offset * TARGET_CHAR_BIT);
177 offset += field3.type ()->length ();
180 t->set_length (offset);
182 t->set_name ("gdb_gnu_v3_abi_vtable");
183 INIT_CPLUS_SPECIFIC (t);
185 result = make_type_with_address_space (t, TYPE_INSTANCE_FLAG_CODE_SPACE);
186 vtable_type_gdbarch_data.set (arch, result);
187 return result;
191 /* Return the ptrdiff_t type used in the vtable type. */
192 static struct type *
193 vtable_ptrdiff_type (struct gdbarch *gdbarch)
195 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
197 /* The "offset_to_top" field has the appropriate (ptrdiff_t) type. */
198 return vtable_type->field (vtable_field_offset_to_top).type ();
201 /* Return the offset from the start of the imaginary `struct
202 gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
203 (i.e., where objects' virtual table pointers point). */
204 static int
205 vtable_address_point_offset (struct gdbarch *gdbarch)
207 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
209 return (vtable_type->field (vtable_field_virtual_functions).loc_bitpos ()
210 / TARGET_CHAR_BIT);
214 /* Determine whether structure TYPE is a dynamic class. Cache the
215 result. */
217 static int
218 gnuv3_dynamic_class (struct type *type)
220 int fieldnum, fieldelem;
222 type = check_typedef (type);
223 gdb_assert (type->code () == TYPE_CODE_STRUCT
224 || type->code () == TYPE_CODE_UNION);
226 if (type->code () == TYPE_CODE_UNION)
227 return 0;
229 if (TYPE_CPLUS_DYNAMIC (type))
230 return TYPE_CPLUS_DYNAMIC (type) == 1;
232 ALLOCATE_CPLUS_STRUCT_TYPE (type);
234 for (fieldnum = 0; fieldnum < TYPE_N_BASECLASSES (type); fieldnum++)
235 if (BASETYPE_VIA_VIRTUAL (type, fieldnum)
236 || gnuv3_dynamic_class (type->field (fieldnum).type ()))
238 TYPE_CPLUS_DYNAMIC (type) = 1;
239 return 1;
242 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
243 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
244 fieldelem++)
246 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, fieldnum);
248 if (TYPE_FN_FIELD_VIRTUAL_P (f, fieldelem))
250 TYPE_CPLUS_DYNAMIC (type) = 1;
251 return 1;
255 TYPE_CPLUS_DYNAMIC (type) = -1;
256 return 0;
259 /* Find the vtable for a value of CONTAINER_TYPE located at
260 CONTAINER_ADDR. Return a value of the correct vtable type for this
261 architecture, or NULL if CONTAINER does not have a vtable. */
263 static struct value *
264 gnuv3_get_vtable (struct gdbarch *gdbarch,
265 struct type *container_type, CORE_ADDR container_addr)
267 struct type *vtable_type = get_gdb_vtable_type (gdbarch);
268 struct type *vtable_pointer_type;
269 struct value *vtable_pointer;
270 CORE_ADDR vtable_address;
272 container_type = check_typedef (container_type);
273 gdb_assert (container_type->code () == TYPE_CODE_STRUCT);
275 /* If this type does not have a virtual table, don't read the first
276 field. */
277 if (!gnuv3_dynamic_class (container_type))
278 return NULL;
280 /* We do not consult the debug information to find the virtual table.
281 The ABI specifies that it is always at offset zero in any class,
282 and debug information may not represent it.
284 We avoid using value_contents on principle, because the object might
285 be large. */
287 /* Find the type "pointer to virtual table". */
288 vtable_pointer_type = lookup_pointer_type (vtable_type);
290 /* Load it from the start of the class. */
291 vtable_pointer = value_at (vtable_pointer_type, container_addr);
292 vtable_address = value_as_address (vtable_pointer);
294 /* Correct it to point at the start of the virtual table, rather
295 than the address point. */
296 return value_at_lazy (vtable_type,
297 vtable_address
298 - vtable_address_point_offset (gdbarch));
302 static struct type *
303 gnuv3_rtti_type (struct value *value,
304 int *full_p, LONGEST *top_p, int *using_enc_p)
306 struct gdbarch *gdbarch;
307 struct type *values_type = check_typedef (value->type ());
308 struct value *vtable;
309 struct minimal_symbol *vtable_symbol;
310 const char *vtable_symbol_name;
311 const char *class_name;
312 struct type *run_time_type;
313 LONGEST offset_to_top;
314 const char *atsign;
316 /* We only have RTTI for dynamic class objects. */
317 if (values_type->code () != TYPE_CODE_STRUCT
318 || !gnuv3_dynamic_class (values_type))
319 return NULL;
321 /* Determine architecture. */
322 gdbarch = values_type->arch ();
324 if (using_enc_p)
325 *using_enc_p = 0;
327 vtable = gnuv3_get_vtable (gdbarch, values_type,
328 value_as_address (value_addr (value)));
329 if (vtable == NULL)
330 return NULL;
332 /* Find the linker symbol for this vtable. */
333 vtable_symbol
334 = lookup_minimal_symbol_by_pc (vtable->address ()
335 + vtable->embedded_offset ()).minsym;
336 if (! vtable_symbol)
337 return NULL;
339 /* The symbol's demangled name should be something like "vtable for
340 CLASS", where CLASS is the name of the run-time type of VALUE.
341 If we didn't like this approach, we could instead look in the
342 type_info object itself to get the class name. But this way
343 should work just as well, and doesn't read target memory. */
344 vtable_symbol_name = vtable_symbol->demangled_name ();
345 if (vtable_symbol_name == NULL
346 || !startswith (vtable_symbol_name, "vtable for "))
348 warning (_("can't find linker symbol for virtual table for `%s' value"),
349 TYPE_SAFE_NAME (values_type));
350 if (vtable_symbol_name)
351 warning (_(" found `%s' instead"), vtable_symbol_name);
352 return NULL;
354 class_name = vtable_symbol_name + 11;
356 /* Strip off @plt and version suffixes. */
357 atsign = strchr (class_name, '@');
358 if (atsign != NULL)
360 char *copy;
362 copy = (char *) alloca (atsign - class_name + 1);
363 memcpy (copy, class_name, atsign - class_name);
364 copy[atsign - class_name] = '\0';
365 class_name = copy;
368 /* Try to look up the class name as a type name. */
369 /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
370 run_time_type = cp_lookup_rtti_type (class_name, NULL);
371 if (run_time_type == NULL)
372 return NULL;
374 /* Get the offset from VALUE to the top of the complete object.
375 NOTE: this is the reverse of the meaning of *TOP_P. */
376 offset_to_top
377 = value_as_long (value_field (vtable, vtable_field_offset_to_top));
379 if (full_p)
380 *full_p = (- offset_to_top == value->embedded_offset ()
381 && (value->enclosing_type ()->length ()
382 >= run_time_type->length ()));
383 if (top_p)
384 *top_p = - offset_to_top;
385 return run_time_type;
388 /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
389 function, of type FNTYPE. */
391 static struct value *
392 gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
393 struct type *fntype, int vtable_index)
395 struct value *vtable, *vfn;
397 /* Every class with virtual functions must have a vtable. */
398 vtable = gnuv3_get_vtable (gdbarch, container->type (),
399 value_as_address (value_addr (container)));
400 gdb_assert (vtable != NULL);
402 /* Fetch the appropriate function pointer from the vtable. */
403 vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
404 vtable_index);
406 /* If this architecture uses function descriptors directly in the vtable,
407 then the address of the vtable entry is actually a "function pointer"
408 (i.e. points to the descriptor). We don't need to scale the index
409 by the size of a function descriptor; GCC does that before outputting
410 debug information. */
411 if (gdbarch_vtable_function_descriptors (gdbarch))
412 vfn = value_addr (vfn);
414 /* Cast the function pointer to the appropriate type. */
415 vfn = value_cast (lookup_pointer_type (fntype), vfn);
417 return vfn;
420 /* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h
421 for a description of the arguments. */
423 static struct value *
424 gnuv3_virtual_fn_field (struct value **value_p,
425 struct fn_field *f, int j,
426 struct type *vfn_base, int offset)
428 struct type *values_type = check_typedef ((*value_p)->type ());
429 struct gdbarch *gdbarch;
431 /* Some simple sanity checks. */
432 if (values_type->code () != TYPE_CODE_STRUCT)
433 error (_("Only classes can have virtual functions."));
435 /* Determine architecture. */
436 gdbarch = values_type->arch ();
438 /* Cast our value to the base class which defines this virtual
439 function. This takes care of any necessary `this'
440 adjustments. */
441 if (vfn_base != values_type)
442 *value_p = value_cast (vfn_base, *value_p);
444 return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
445 TYPE_FN_FIELD_VOFFSET (f, j));
448 /* Compute the offset of the baseclass which is
449 the INDEXth baseclass of class TYPE,
450 for value at VALADDR (in host) at ADDRESS (in target).
451 The result is the offset of the baseclass value relative
452 to (the address of)(ARG) + OFFSET.
454 -1 is returned on error. */
456 static int
457 gnuv3_baseclass_offset (struct type *type, int index,
458 const bfd_byte *valaddr, LONGEST embedded_offset,
459 CORE_ADDR address, const struct value *val)
461 struct gdbarch *gdbarch;
462 struct type *ptr_type;
463 struct value *vtable;
464 struct value *vbase_array;
465 long int cur_base_offset, base_offset;
467 /* Determine architecture. */
468 gdbarch = type->arch ();
469 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
471 /* If it isn't a virtual base, this is easy. The offset is in the
472 type definition. */
473 if (!BASETYPE_VIA_VIRTUAL (type, index))
474 return TYPE_BASECLASS_BITPOS (type, index) / 8;
476 /* If we have a DWARF expression for the offset, evaluate it. */
477 if (type->field (index).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
479 struct dwarf2_property_baton baton;
480 baton.property_type
481 = lookup_pointer_type (type->field (index).type ());
482 baton.locexpr = *type->field (index).loc_dwarf_block ();
484 struct dynamic_prop prop;
485 prop.set_locexpr (&baton);
487 struct property_addr_info addr_stack;
488 addr_stack.type = type;
489 /* Note that we don't set "valaddr" here. Doing so causes
490 regressions. FIXME. */
491 addr_stack.addr = address + embedded_offset;
492 addr_stack.next = nullptr;
494 CORE_ADDR result;
495 if (dwarf2_evaluate_property (&prop, nullptr, &addr_stack, &result,
496 {addr_stack.addr}))
497 return (int) (result - addr_stack.addr);
500 /* To access a virtual base, we need to use the vbase offset stored in
501 our vtable. Recent GCC versions provide this information. If it isn't
502 available, we could get what we needed from RTTI, or from drawing the
503 complete inheritance graph based on the debug info. Neither is
504 worthwhile. */
505 cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
506 if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
507 error (_("Expected a negative vbase offset (old compiler?)"));
509 cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
510 if ((- cur_base_offset) % ptr_type->length () != 0)
511 error (_("Misaligned vbase offset."));
512 cur_base_offset = cur_base_offset / ((int) ptr_type->length ());
514 vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset);
515 gdb_assert (vtable != NULL);
516 vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
517 base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
518 return base_offset;
521 /* Locate a virtual method in DOMAIN or its non-virtual base classes
522 which has virtual table index VOFFSET. The method has an associated
523 "this" adjustment of ADJUSTMENT bytes. */
525 static const char *
526 gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
527 LONGEST adjustment)
529 int i;
531 /* Search this class first. */
532 if (adjustment == 0)
534 int len;
536 len = TYPE_NFN_FIELDS (domain);
537 for (i = 0; i < len; i++)
539 int len2, j;
540 struct fn_field *f;
542 f = TYPE_FN_FIELDLIST1 (domain, i);
543 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
545 check_stub_method_group (domain, i);
546 for (j = 0; j < len2; j++)
547 if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset)
548 return TYPE_FN_FIELD_PHYSNAME (f, j);
552 /* Next search non-virtual bases. If it's in a virtual base,
553 we're out of luck. */
554 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
556 int pos;
557 struct type *basetype;
559 if (BASETYPE_VIA_VIRTUAL (domain, i))
560 continue;
562 pos = TYPE_BASECLASS_BITPOS (domain, i) / 8;
563 basetype = domain->field (i).type ();
564 /* Recurse with a modified adjustment. We don't need to adjust
565 voffset. */
566 if (adjustment >= pos && adjustment < pos + basetype->length ())
567 return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
570 return NULL;
573 /* Decode GNU v3 method pointer. */
575 static int
576 gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
577 const gdb_byte *contents,
578 CORE_ADDR *value_p,
579 LONGEST *adjustment_p)
581 struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
582 struct type *offset_type = vtable_ptrdiff_type (gdbarch);
583 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
584 CORE_ADDR ptr_value;
585 LONGEST voffset, adjustment;
586 int vbit;
588 /* Extract the pointer to member. The first element is either a pointer
589 or a vtable offset. For pointers, we need to use extract_typed_address
590 to allow the back-end to convert the pointer to a GDB address -- but
591 vtable offsets we must handle as integers. At this point, we do not
592 yet know which case we have, so we extract the value under both
593 interpretations and choose the right one later on. */
594 ptr_value = extract_typed_address (contents, funcptr_type);
595 voffset = extract_signed_integer (contents,
596 funcptr_type->length (), byte_order);
597 contents += funcptr_type->length ();
598 adjustment = extract_signed_integer (contents,
599 offset_type->length (), byte_order);
601 if (!gdbarch_vbit_in_delta (gdbarch))
603 vbit = voffset & 1;
604 voffset = voffset ^ vbit;
606 else
608 vbit = adjustment & 1;
609 adjustment = adjustment >> 1;
612 *value_p = vbit? voffset : ptr_value;
613 *adjustment_p = adjustment;
614 return vbit;
617 /* GNU v3 implementation of cplus_print_method_ptr. */
619 static void
620 gnuv3_print_method_ptr (const gdb_byte *contents,
621 struct type *type,
622 struct ui_file *stream)
624 struct type *self_type = TYPE_SELF_TYPE (type);
625 struct gdbarch *gdbarch = self_type->arch ();
626 CORE_ADDR ptr_value;
627 LONGEST adjustment;
628 int vbit;
630 /* Extract the pointer to member. */
631 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
633 /* Check for NULL. */
634 if (ptr_value == 0 && vbit == 0)
636 gdb_printf (stream, "NULL");
637 return;
640 /* Search for a virtual method. */
641 if (vbit)
643 CORE_ADDR voffset;
644 const char *physname;
646 /* It's a virtual table offset, maybe in this class. Search
647 for a field with the correct vtable offset. First convert it
648 to an index, as used in TYPE_FN_FIELD_VOFFSET. */
649 voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
651 physname = gnuv3_find_method_in (self_type, voffset, adjustment);
653 /* If we found a method, print that. We don't bother to disambiguate
654 possible paths to the method based on the adjustment. */
655 if (physname)
657 gdb::unique_xmalloc_ptr<char> demangled_name
658 = gdb_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
660 gdb_printf (stream, "&virtual ");
661 if (demangled_name == NULL)
662 gdb_puts (physname, stream);
663 else
664 gdb_puts (demangled_name.get (), stream);
665 return;
668 else if (ptr_value != 0)
670 /* Found a non-virtual function: print out the type. */
671 gdb_puts ("(", stream);
672 c_print_type (type, "", stream, -1, 0, current_language->la_language,
673 &type_print_raw_options);
674 gdb_puts (") ", stream);
677 /* We didn't find it; print the raw data. */
678 if (vbit)
680 gdb_printf (stream, "&virtual table offset ");
681 print_longest (stream, 'd', 1, ptr_value);
683 else
685 struct value_print_options opts;
687 get_user_print_options (&opts);
688 print_address_demangle (&opts, gdbarch, ptr_value, stream, demangle);
691 if (adjustment)
693 gdb_printf (stream, ", this adjustment ");
694 print_longest (stream, 'd', 1, adjustment);
698 /* GNU v3 implementation of cplus_method_ptr_size. */
700 static int
701 gnuv3_method_ptr_size (struct type *type)
703 return 2 * builtin_type (type->arch ())->builtin_data_ptr->length ();
706 /* GNU v3 implementation of cplus_make_method_ptr. */
708 static void
709 gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
710 CORE_ADDR value, int is_virtual)
712 struct gdbarch *gdbarch = type->arch ();
713 int size = builtin_type (gdbarch)->builtin_data_ptr->length ();
714 enum bfd_endian byte_order = type_byte_order (type);
716 /* FIXME drow/2006-12-24: The adjustment of "this" is currently
717 always zero, since the method pointer is of the correct type.
718 But if the method pointer came from a base class, this is
719 incorrect - it should be the offset to the base. The best
720 fix might be to create the pointer to member pointing at the
721 base class and cast it to the derived class, but that requires
722 support for adjusting pointers to members when casting them -
723 not currently supported by GDB. */
725 if (!gdbarch_vbit_in_delta (gdbarch))
727 store_unsigned_integer (contents, size, byte_order, value | is_virtual);
728 store_unsigned_integer (contents + size, size, byte_order, 0);
730 else
732 store_unsigned_integer (contents, size, byte_order, value);
733 store_unsigned_integer (contents + size, size, byte_order, is_virtual);
737 /* GNU v3 implementation of cplus_method_ptr_to_value. */
739 static struct value *
740 gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
742 struct gdbarch *gdbarch;
743 const gdb_byte *contents = method_ptr->contents ().data ();
744 CORE_ADDR ptr_value;
745 struct type *self_type, *final_type, *method_type;
746 LONGEST adjustment;
747 int vbit;
749 self_type = TYPE_SELF_TYPE (check_typedef (method_ptr->type ()));
750 final_type = lookup_pointer_type (self_type);
752 method_type = check_typedef (method_ptr->type ())->target_type ();
754 /* Extract the pointer to member. */
755 gdbarch = self_type->arch ();
756 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
758 /* First convert THIS to match the containing type of the pointer to
759 member. This cast may adjust the value of THIS. */
760 *this_p = value_cast (final_type, *this_p);
762 /* Then apply whatever adjustment is necessary. This creates a somewhat
763 strange pointer: it claims to have type FINAL_TYPE, but in fact it
764 might not be a valid FINAL_TYPE. For instance, it might be a
765 base class of FINAL_TYPE. And if it's not the primary base class,
766 then printing it out as a FINAL_TYPE object would produce some pretty
767 garbage.
769 But we don't really know the type of the first argument in
770 METHOD_TYPE either, which is why this happens. We can't
771 dereference this later as a FINAL_TYPE, but once we arrive in the
772 called method we'll have debugging information for the type of
773 "this" - and that'll match the value we produce here.
775 You can provoke this case by casting a Base::* to a Derived::*, for
776 instance. */
777 *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
778 *this_p = value_ptradd (*this_p, adjustment);
779 *this_p = value_cast (final_type, *this_p);
781 if (vbit)
783 LONGEST voffset;
785 voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
786 return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
787 method_type, voffset);
789 else
790 return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
793 /* Objects of this type are stored in a hash table and a vector when
794 printing the vtables for a class. */
796 struct value_and_voffset
798 /* The value representing the object. */
799 struct value *value;
801 /* The maximum vtable offset we've found for any object at this
802 offset in the outermost object. */
803 int max_voffset;
806 /* Hash function for value_and_voffset. */
808 static hashval_t
809 hash_value_and_voffset (const void *p)
811 const struct value_and_voffset *o = (const struct value_and_voffset *) p;
813 return o->value->address () + o->value->embedded_offset ();
816 /* Equality function for value_and_voffset. */
818 static int
819 eq_value_and_voffset (const void *a, const void *b)
821 const struct value_and_voffset *ova = (const struct value_and_voffset *) a;
822 const struct value_and_voffset *ovb = (const struct value_and_voffset *) b;
824 return (ova->value->address () + ova->value->embedded_offset ()
825 == ovb->value->address () + ovb->value->embedded_offset ());
828 /* Comparison function for value_and_voffset. */
830 static bool
831 compare_value_and_voffset (const struct value_and_voffset *va,
832 const struct value_and_voffset *vb)
834 CORE_ADDR addra = (va->value->address ()
835 + va->value->embedded_offset ());
836 CORE_ADDR addrb = (vb->value->address ()
837 + vb->value->embedded_offset ());
839 return addra < addrb;
842 /* A helper function used when printing vtables. This determines the
843 key (most derived) sub-object at each address and also computes the
844 maximum vtable offset seen for the corresponding vtable. Updates
845 OFFSET_HASH and OFFSET_VEC with a new value_and_voffset object, if
846 needed. VALUE is the object to examine. */
848 static void
849 compute_vtable_size (htab_t offset_hash,
850 std::vector<value_and_voffset *> *offset_vec,
851 struct value *value)
853 int i;
854 struct type *type = check_typedef (value->type ());
855 void **slot;
856 struct value_and_voffset search_vo, *current_vo;
858 gdb_assert (type->code () == TYPE_CODE_STRUCT);
860 /* If the object is not dynamic, then we are done; as it cannot have
861 dynamic base types either. */
862 if (!gnuv3_dynamic_class (type))
863 return;
865 /* Update the hash and the vec, if needed. */
866 search_vo.value = value;
867 slot = htab_find_slot (offset_hash, &search_vo, INSERT);
868 if (*slot)
869 current_vo = (struct value_and_voffset *) *slot;
870 else
872 current_vo = XNEW (struct value_and_voffset);
873 current_vo->value = value;
874 current_vo->max_voffset = -1;
875 *slot = current_vo;
876 offset_vec->push_back (current_vo);
879 /* Update the value_and_voffset object with the highest vtable
880 offset from this class. */
881 for (i = 0; i < TYPE_NFN_FIELDS (type); ++i)
883 int j;
884 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, i);
886 for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (type, i); ++j)
888 if (TYPE_FN_FIELD_VIRTUAL_P (fn, j))
890 int voffset = TYPE_FN_FIELD_VOFFSET (fn, j);
892 if (voffset > current_vo->max_voffset)
893 current_vo->max_voffset = voffset;
898 /* Recurse into base classes. */
899 for (i = 0; i < TYPE_N_BASECLASSES (type); ++i)
900 compute_vtable_size (offset_hash, offset_vec, value_field (value, i));
903 /* Helper for gnuv3_print_vtable that prints a single vtable. */
905 static void
906 print_one_vtable (struct gdbarch *gdbarch, struct value *value,
907 int max_voffset,
908 struct value_print_options *opts)
910 int i;
911 struct type *type = check_typedef (value->type ());
912 struct value *vtable;
913 CORE_ADDR vt_addr;
915 vtable = gnuv3_get_vtable (gdbarch, type,
916 value->address ()
917 + value->embedded_offset ());
918 vt_addr = value_field (vtable,
919 vtable_field_virtual_functions)->address ();
921 gdb_printf (_("vtable for '%s' @ %s (subobject @ %s):\n"),
922 TYPE_SAFE_NAME (type),
923 paddress (gdbarch, vt_addr),
924 paddress (gdbarch, (value->address ()
925 + value->embedded_offset ())));
927 for (i = 0; i <= max_voffset; ++i)
929 /* Initialize it just to avoid a GCC false warning. */
930 CORE_ADDR addr = 0;
931 int got_error = 0;
932 struct value *vfn;
934 gdb_printf ("[%d]: ", i);
936 vfn = value_subscript (value_field (vtable,
937 vtable_field_virtual_functions),
940 if (gdbarch_vtable_function_descriptors (gdbarch))
941 vfn = value_addr (vfn);
945 addr = value_as_address (vfn);
947 catch (const gdb_exception_error &ex)
949 fprintf_styled (gdb_stdout, metadata_style.style (),
950 _("<error: %s>"), ex.what ());
951 got_error = 1;
954 if (!got_error)
955 print_function_pointer_address (opts, gdbarch, addr, gdb_stdout);
956 gdb_printf ("\n");
960 /* Implementation of the print_vtable method. */
962 static void
963 gnuv3_print_vtable (struct value *value)
965 struct gdbarch *gdbarch;
966 struct type *type;
967 struct value *vtable;
968 struct value_print_options opts;
969 int count;
971 value = coerce_ref (value);
972 type = check_typedef (value->type ());
973 if (type->code () == TYPE_CODE_PTR)
975 value = value_ind (value);
976 type = check_typedef (value->type ());
979 get_user_print_options (&opts);
981 /* Respect 'set print object'. */
982 if (opts.objectprint)
984 value = value_full_object (value, NULL, 0, 0, 0);
985 type = check_typedef (value->type ());
988 gdbarch = type->arch ();
990 vtable = NULL;
991 if (type->code () == TYPE_CODE_STRUCT)
992 vtable = gnuv3_get_vtable (gdbarch, type,
993 value_as_address (value_addr (value)));
995 if (!vtable)
997 gdb_printf (_("This object does not have a virtual function table\n"));
998 return;
1001 htab_up offset_hash (htab_create_alloc (1, hash_value_and_voffset,
1002 eq_value_and_voffset,
1003 xfree, xcalloc, xfree));
1004 std::vector<value_and_voffset *> result_vec;
1006 compute_vtable_size (offset_hash.get (), &result_vec, value);
1007 std::sort (result_vec.begin (), result_vec.end (),
1008 compare_value_and_voffset);
1010 count = 0;
1011 for (value_and_voffset *iter : result_vec)
1013 if (iter->max_voffset >= 0)
1015 if (count > 0)
1016 gdb_printf ("\n");
1017 print_one_vtable (gdbarch, iter->value, iter->max_voffset, &opts);
1018 ++count;
1023 /* Return a GDB type representing `struct std::type_info', laid out
1024 appropriately for ARCH.
1026 We use this function as the gdbarch per-architecture data
1027 initialization function. */
1029 static struct type *
1030 build_std_type_info_type (struct gdbarch *arch)
1032 struct type *t;
1033 int offset;
1034 struct type *void_ptr_type
1035 = builtin_type (arch)->builtin_data_ptr;
1036 struct type *char_type
1037 = builtin_type (arch)->builtin_char;
1038 struct type *char_ptr_type
1039 = make_pointer_type (make_cv_type (1, 0, char_type, NULL), NULL);
1041 t = type_allocator (arch).new_type (TYPE_CODE_STRUCT, 0, nullptr);
1043 t->alloc_fields (2);
1045 offset = 0;
1047 /* The vtable. */
1049 struct field &field0 = t->field (0);
1050 field0.set_name ("_vptr.type_info");
1051 field0.set_type (void_ptr_type);
1052 field0.set_loc_bitpos (offset * TARGET_CHAR_BIT);
1053 offset += field0.type ()->length ();
1056 /* The name. */
1058 struct field &field1 = t->field (1);
1059 field1.set_name ("__name");
1060 field1.set_type (char_ptr_type);
1061 field1.set_loc_bitpos (offset * TARGET_CHAR_BIT);
1062 offset += field1.type ()->length ();
1065 t->set_length (offset);
1067 t->set_name ("gdb_gnu_v3_type_info");
1068 INIT_CPLUS_SPECIFIC (t);
1070 return t;
1073 /* Implement the 'get_typeid_type' method. */
1075 static struct type *
1076 gnuv3_get_typeid_type (struct gdbarch *gdbarch)
1078 struct symbol *typeinfo;
1079 struct type *typeinfo_type;
1081 typeinfo = lookup_symbol ("std::type_info", NULL, SEARCH_STRUCT_DOMAIN,
1082 NULL).symbol;
1083 if (typeinfo == NULL)
1085 typeinfo_type = std_type_info_gdbarch_data.get (gdbarch);
1086 if (typeinfo_type == nullptr)
1088 typeinfo_type = build_std_type_info_type (gdbarch);
1089 std_type_info_gdbarch_data.set (gdbarch, typeinfo_type);
1092 else
1093 typeinfo_type = typeinfo->type ();
1095 return typeinfo_type;
1098 /* Implement the 'get_typeid' method. */
1100 static struct value *
1101 gnuv3_get_typeid (struct value *value)
1103 struct type *typeinfo_type;
1104 struct type *type;
1105 struct gdbarch *gdbarch;
1106 struct value *result;
1107 std::string type_name;
1108 gdb::unique_xmalloc_ptr<char> canonical;
1110 /* We have to handle values a bit trickily here, to allow this code
1111 to work properly with non_lvalue values that are really just
1112 disguised types. */
1113 if (value->lval () == lval_memory)
1114 value = coerce_ref (value);
1116 type = check_typedef (value->type ());
1118 /* In the non_lvalue case, a reference might have slipped through
1119 here. */
1120 if (type->code () == TYPE_CODE_REF)
1121 type = check_typedef (type->target_type ());
1123 /* Ignore top-level cv-qualifiers. */
1124 type = make_cv_type (0, 0, type, NULL);
1125 gdbarch = type->arch ();
1127 type_name = type_to_string (type);
1128 if (type_name.empty ())
1129 error (_("cannot find typeinfo for unnamed type"));
1131 /* We need to canonicalize the type name here, because we do lookups
1132 using the demangled name, and so we must match the format it
1133 uses. E.g., GDB tends to use "const char *" as a type name, but
1134 the demangler uses "char const *". */
1135 canonical = cp_canonicalize_string (type_name.c_str ());
1136 const char *name = (canonical == nullptr
1137 ? type_name.c_str ()
1138 : canonical.get ());
1140 typeinfo_type = gnuv3_get_typeid_type (gdbarch);
1142 /* We check for lval_memory because in the "typeid (type-id)" case,
1143 the type is passed via a not_lval value object. */
1144 if (type->code () == TYPE_CODE_STRUCT
1145 && value->lval () == lval_memory
1146 && gnuv3_dynamic_class (type))
1148 struct value *vtable, *typeinfo_value;
1149 CORE_ADDR address = value->address () + value->embedded_offset ();
1151 vtable = gnuv3_get_vtable (gdbarch, type, address);
1152 if (vtable == NULL)
1153 error (_("cannot find typeinfo for object of type '%s'"),
1154 name);
1155 typeinfo_value = value_field (vtable, vtable_field_type_info);
1156 result = value_ind (value_cast (make_pointer_type (typeinfo_type, NULL),
1157 typeinfo_value));
1159 else
1161 std::string sym_name = std::string ("typeinfo for ") + name;
1162 bound_minimal_symbol minsym
1163 = lookup_minimal_symbol (sym_name.c_str (), NULL, NULL);
1165 if (minsym.minsym == NULL)
1166 error (_("could not find typeinfo symbol for '%s'"), name);
1168 result = value_at_lazy (typeinfo_type, minsym.value_address ());
1171 return result;
1174 /* Implement the 'get_typename_from_type_info' method. */
1176 static std::string
1177 gnuv3_get_typename_from_type_info (struct value *type_info_ptr)
1179 struct gdbarch *gdbarch = type_info_ptr->type ()->arch ();
1180 struct bound_minimal_symbol typeinfo_sym;
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 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 bound_minimal_symbol thunk_sym, fn_sym;
1232 struct obj_section *section;
1233 const char *thunk_name, *fn_name;
1235 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
1236 if (real_stop_pc == 0)
1237 real_stop_pc = stop_pc;
1239 /* Find the linker symbol for this potential thunk. */
1240 thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
1241 section = find_pc_section (real_stop_pc);
1242 if (thunk_sym.minsym == NULL || section == NULL)
1243 return 0;
1245 /* The symbol's demangled name should be something like "virtual
1246 thunk to FUNCTION", where FUNCTION is the name of the function
1247 being thunked to. */
1248 thunk_name = thunk_sym.minsym->demangled_name ();
1249 if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
1250 return 0;
1252 fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
1253 fn_sym = lookup_minimal_symbol (fn_name, NULL, 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);