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