Memory leaks and ineffective bounds checking in wasm_scan
[deliverable/binutils-gdb.git] / gdb / gnu-v3-abi.c
CommitLineData
7ed49443
JB
1/* Abstraction of GNU v3 abi.
2 Contributed by Jim Blandy <jimb@redhat.com>
451fbdda 3
b811d2c2 4 Copyright (C) 2001-2020 Free Software Foundation, Inc.
7ed49443
JB
5
6 This file is part of GDB.
7
a9762ec7
JB
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.
7ed49443
JB
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
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
7ed49443
JB
20
21#include "defs.h"
22#include "value.h"
23#include "cp-abi.h"
362ff856 24#include "cp-support.h"
7ed49443 25#include "demangle.h"
62bf63d7 26#include "dwarf2.h"
b18be20d 27#include "objfiles.h"
0d5de010 28#include "valprint.h"
94af9270 29#include "c-lang.h"
79d43c61 30#include "typeprint.h"
59d3651b 31#include <algorithm>
7f6aba03 32#include "cli/cli-style.h"
0d5de010 33
b27b8843 34static struct cp_abi_ops gnu_v3_abi_ops;
7ed49443 35
6e72ca20
TT
36/* A gdbarch key for std::type_info, in the event that it can't be
37 found in the debug info. */
38
39static struct gdbarch_data *std_type_info_gdbarch_data;
40
41
7ed49443
JB
42static int
43gnuv3_is_vtable_name (const char *name)
44{
61012eef 45 return startswith (name, "_ZTV");
7ed49443
JB
46}
47
48static int
49gnuv3_is_operator_name (const char *name)
50{
8090b426 51 return startswith (name, CP_OPERATOR_STR);
7ed49443
JB
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. */
b27b8843 92static struct gdbarch_data *vtable_type_gdbarch_data;
7ed49443
JB
93
94
95/* Human-readable names for the numbers of the fields above. */
96enum {
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
9970f04b 108 initialization function. */
7ed49443
JB
109static void *
110build_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
fde6c819 117 = builtin_type (arch)->builtin_data_ptr;
7ed49443 118 struct type *ptr_to_void_fn_type
fde6c819 119 = builtin_type (arch)->builtin_func_ptr;
7ed49443
JB
120
121 /* ARCH can't give us the true ptrdiff_t type, so we guess. */
122 struct type *ptrdiff_type
e9bb382b 123 = arch_integer_type (arch, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
7ed49443
JB
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. */
8d749320 131 field_list = XCNEWVEC (struct field, 4);
7ed49443
JB
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";
e3506a9f 137 FIELD_TYPE (*field) = lookup_array_range_type (ptrdiff_type, 0, -1);
f41f5e61 138 SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
7ed49443
JB
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;
f41f5e61 145 SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
7ed49443
JB
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;
f41f5e61 152 SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
7ed49443
JB
153 offset += TYPE_LENGTH (FIELD_TYPE (*field));
154 field++;
155
156 /* void (*virtual_functions[0]) (); */
157 FIELD_NAME (*field) = "virtual_functions";
e3506a9f 158 FIELD_TYPE (*field) = lookup_array_range_type (ptr_to_void_fn_type, 0, -1);
f41f5e61 159 SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
7ed49443
JB
160 offset += TYPE_LENGTH (FIELD_TYPE (*field));
161 field++;
162
163 /* We assumed in the allocation above that there were four fields. */
3d499020 164 gdb_assert (field == (field_list + 4));
7ed49443 165
77b7c781 166 t = arch_type (arch, TYPE_CODE_STRUCT, offset * TARGET_CHAR_BIT, NULL);
7ed49443
JB
167 TYPE_NFIELDS (t) = field - field_list;
168 TYPE_FIELDS (t) = field_list;
e86ca25f 169 TYPE_NAME (t) = "gdb_gnu_v3_abi_vtable";
e9bb382b 170 INIT_CPLUS_SPECIFIC (t);
7ed49443 171
706d0883 172 return make_type_with_address_space (t, TYPE_INSTANCE_FLAG_CODE_SPACE);
7ed49443
JB
173}
174
175
ed09d7da
KB
176/* Return the ptrdiff_t type used in the vtable type. */
177static struct type *
178vtable_ptrdiff_type (struct gdbarch *gdbarch)
179{
9a3c8263
SM
180 struct type *vtable_type
181 = (struct type *) gdbarch_data (gdbarch, vtable_type_gdbarch_data);
ed09d7da
KB
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
7ed49443
JB
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). */
190static int
ad4820ab 191vtable_address_point_offset (struct gdbarch *gdbarch)
7ed49443 192{
9a3c8263
SM
193 struct type *vtable_type
194 = (struct type *) gdbarch_data (gdbarch, vtable_type_gdbarch_data);
7ed49443
JB
195
196 return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions)
197 / TARGET_CHAR_BIT);
198}
199
200
d48cc9dd
DJ
201/* Determine whether structure TYPE is a dynamic class. Cache the
202 result. */
203
204static int
205gnuv3_dynamic_class (struct type *type)
206{
207 int fieldnum, fieldelem;
208
f168693b 209 type = check_typedef (type);
5f4ce105
DE
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
d48cc9dd
DJ
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
250static struct value *
251gnuv3_get_vtable (struct gdbarch *gdbarch,
252 struct type *container_type, CORE_ADDR container_addr)
253{
9a3c8263
SM
254 struct type *vtable_type
255 = (struct type *) gdbarch_data (gdbarch, vtable_type_gdbarch_data);
d48cc9dd
DJ
256 struct type *vtable_pointer_type;
257 struct value *vtable_pointer;
258 CORE_ADDR vtable_address;
259
f168693b 260 container_type = check_typedef (container_type);
5f4ce105
DE
261 gdb_assert (TYPE_CODE (container_type) == TYPE_CODE_STRUCT);
262
d48cc9dd
DJ
263 /* If this type does not have a virtual table, don't read the first
264 field. */
5f4ce105 265 if (!gnuv3_dynamic_class (container_type))
d48cc9dd
DJ
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,
0963b4bd
MS
285 vtable_address
286 - vtable_address_point_offset (gdbarch));
d48cc9dd
DJ
287}
288
289
7ed49443
JB
290static struct type *
291gnuv3_rtti_type (struct value *value,
6b850546 292 int *full_p, LONGEST *top_p, int *using_enc_p)
7ed49443 293{
ad4820ab 294 struct gdbarch *gdbarch;
df407dfe 295 struct type *values_type = check_typedef (value_type (value));
7ed49443
JB
296 struct value *vtable;
297 struct minimal_symbol *vtable_symbol;
298 const char *vtable_symbol_name;
299 const char *class_name;
7ed49443
JB
300 struct type *run_time_type;
301 LONGEST offset_to_top;
e6a959d6 302 const char *atsign;
7ed49443 303
e95a97d4
AA
304 /* We only have RTTI for dynamic class objects. */
305 if (TYPE_CODE (values_type) != TYPE_CODE_STRUCT
306 || !gnuv3_dynamic_class (values_type))
7ed49443
JB
307 return NULL;
308
ad4820ab 309 /* Determine architecture. */
50810684 310 gdbarch = get_type_arch (values_type);
7ed49443 311
21cfb3b6
DJ
312 if (using_enc_p)
313 *using_enc_p = 0;
314
5f4ce105 315 vtable = gnuv3_get_vtable (gdbarch, values_type,
d48cc9dd
DJ
316 value_as_address (value_addr (value)));
317 if (vtable == NULL)
318 return NULL;
319
7ed49443
JB
320 /* Find the linker symbol for this vtable. */
321 vtable_symbol
42ae5230 322 = lookup_minimal_symbol_by_pc (value_address (vtable)
7cbd4a93 323 + value_embedded_offset (vtable)).minsym;
7ed49443
JB
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. */
c9d95fa3 332 vtable_symbol_name = vtable_symbol->demangled_name ();
98081e55 333 if (vtable_symbol_name == NULL
61012eef 334 || !startswith (vtable_symbol_name, "vtable for "))
f773fdbb 335 {
8a3fe4f8 336 warning (_("can't find linker symbol for virtual table for `%s' value"),
0a07729b 337 TYPE_SAFE_NAME (values_type));
f773fdbb 338 if (vtable_symbol_name)
8a3fe4f8 339 warning (_(" found `%s' instead"), vtable_symbol_name);
f773fdbb
JM
340 return NULL;
341 }
7ed49443
JB
342 class_name = vtable_symbol_name + 11;
343
8de20a37
TT
344 /* Strip off @plt and version suffixes. */
345 atsign = strchr (class_name, '@');
346 if (atsign != NULL)
347 {
348 char *copy;
349
224c3ddb 350 copy = (char *) alloca (atsign - class_name + 1);
8de20a37
TT
351 memcpy (copy, class_name, atsign - class_name);
352 copy[atsign - class_name] = '\0';
353 class_name = copy;
354 }
355
7ed49443 356 /* Try to look up the class name as a type name. */
0963b4bd 357 /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
362ff856
MC
358 run_time_type = cp_lookup_rtti_type (class_name, NULL);
359 if (run_time_type == NULL)
360 return NULL;
7ed49443
JB
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)
13c3b5f5 368 *full_p = (- offset_to_top == value_embedded_offset (value)
4754a64e 369 && (TYPE_LENGTH (value_enclosing_type (value))
7ed49443
JB
370 >= TYPE_LENGTH (run_time_type)));
371 if (top_p)
372 *top_p = - offset_to_top;
7ed49443
JB
373 return run_time_type;
374}
375
0d5de010
DJ
376/* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
377 function, of type FNTYPE. */
7ed49443 378
0d5de010 379static struct value *
ad4820ab
UW
380gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
381 struct type *fntype, int vtable_index)
0d5de010 382{
d48cc9dd
DJ
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);
7ed49443
JB
389
390 /* Fetch the appropriate function pointer from the vtable. */
391 vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
2497b498 392 vtable_index);
7ed49443 393
0d5de010
DJ
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
85102364 397 by the size of a function descriptor; GCC does that before outputting
0d5de010 398 debug information. */
ad4820ab 399 if (gdbarch_vtable_function_descriptors (gdbarch))
0d5de010 400 vfn = value_addr (vfn);
7ed49443 401
0d5de010
DJ
402 /* Cast the function pointer to the appropriate type. */
403 vfn = value_cast (lookup_pointer_type (fntype), vfn);
76b79d6e 404
7ed49443
JB
405 return vfn;
406}
407
0d5de010
DJ
408/* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h
409 for a description of the arguments. */
410
411static struct value *
412gnuv3_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));
ad4820ab 417 struct gdbarch *gdbarch;
0d5de010
DJ
418
419 /* Some simple sanity checks. */
4753d33b 420 if (TYPE_CODE (values_type) != TYPE_CODE_STRUCT)
0d5de010
DJ
421 error (_("Only classes can have virtual functions."));
422
ad4820ab 423 /* Determine architecture. */
50810684 424 gdbarch = get_type_arch (values_type);
ad4820ab 425
0d5de010
DJ
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
ad4820ab 432 return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
0d5de010
DJ
433 TYPE_FN_FIELD_VOFFSET (f, j));
434}
435
1514d34e
DJ
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
0963b4bd
MS
442 -1 is returned on error. */
443
b9362cc7 444static int
8af8e3bc 445gnuv3_baseclass_offset (struct type *type, int index,
6b850546 446 const bfd_byte *valaddr, LONGEST embedded_offset,
8af8e3bc 447 CORE_ADDR address, const struct value *val)
1514d34e 448{
ad4820ab 449 struct gdbarch *gdbarch;
ad4820ab 450 struct type *ptr_type;
79d5b63a 451 struct value *vtable;
2497b498 452 struct value *vbase_array;
1514d34e 453 long int cur_base_offset, base_offset;
1514d34e 454
ad4820ab 455 /* Determine architecture. */
50810684 456 gdbarch = get_type_arch (type);
ad4820ab
UW
457 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
458
1514d34e 459 /* If it isn't a virtual base, this is easy. The offset is in the
9c37b5ae
TT
460 type definition. */
461 if (!BASETYPE_VIA_VIRTUAL (type, index))
1514d34e
DJ
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;
ad4820ab 470 if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
8a3fe4f8 471 error (_("Expected a negative vbase offset (old compiler?)"));
1514d34e 472
ad4820ab
UW
473 cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
474 if ((- cur_base_offset) % TYPE_LENGTH (ptr_type) != 0)
8a3fe4f8 475 error (_("Misaligned vbase offset."));
ad4820ab 476 cur_base_offset = cur_base_offset / ((int) TYPE_LENGTH (ptr_type));
1514d34e 477
8af8e3bc 478 vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset);
d48cc9dd 479 gdb_assert (vtable != NULL);
1514d34e 480 vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
2497b498 481 base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
1514d34e
DJ
482 return base_offset;
483}
7ed49443 484
0d5de010
DJ
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
2c0b251b 489static const char *
0d5de010
DJ
490gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
491 LONGEST adjustment)
492{
493 int i;
0d5de010
DJ
494
495 /* Search this class first. */
0d5de010
DJ
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
fead6908
UW
537/* Decode GNU v3 method pointer. */
538
539static int
ad4820ab
UW
540gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
541 const gdb_byte *contents,
fead6908
UW
542 CORE_ADDR *value_p,
543 LONGEST *adjustment_p)
544{
ad4820ab 545 struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
ed09d7da 546 struct type *offset_type = vtable_ptrdiff_type (gdbarch);
e17a4113 547 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
fead6908
UW
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);
e17a4113
UW
559 voffset = extract_signed_integer (contents,
560 TYPE_LENGTH (funcptr_type), byte_order);
fead6908 561 contents += TYPE_LENGTH (funcptr_type);
e17a4113
UW
562 adjustment = extract_signed_integer (contents,
563 TYPE_LENGTH (offset_type), byte_order);
fead6908 564
ad4820ab 565 if (!gdbarch_vbit_in_delta (gdbarch))
fead6908
UW
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
0d5de010
DJ
581/* GNU v3 implementation of cplus_print_method_ptr. */
582
583static void
584gnuv3_print_method_ptr (const gdb_byte *contents,
585 struct type *type,
586 struct ui_file *stream)
587{
09e2d7c7
DE
588 struct type *self_type = TYPE_SELF_TYPE (type);
589 struct gdbarch *gdbarch = get_type_arch (self_type);
0d5de010
DJ
590 CORE_ADDR ptr_value;
591 LONGEST adjustment;
0d5de010
DJ
592 int vbit;
593
0d5de010 594 /* Extract the pointer to member. */
ad4820ab 595 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
0d5de010
DJ
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. */
ed09d7da 613 voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
0d5de010 614
09e2d7c7 615 physname = gnuv3_find_method_in (self_type, voffset, adjustment);
0d5de010
DJ
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 {
8de20a37
TT
621 char *demangled_name = gdb_demangle (physname,
622 DMGL_ANSI | DMGL_PARAMS);
d8734c88 623
94af9270
KS
624 fprintf_filtered (stream, "&virtual ");
625 if (demangled_name == NULL)
626 fputs_filtered (physname, stream);
627 else
0d5de010 628 {
0d5de010
DJ
629 fputs_filtered (demangled_name, stream);
630 xfree (demangled_name);
0d5de010 631 }
94af9270 632 return;
0d5de010
DJ
633 }
634 }
94af9270
KS
635 else if (ptr_value != 0)
636 {
637 /* Found a non-virtual function: print out the type. */
638 fputs_filtered ("(", stream);
79d43c61 639 c_print_type (type, "", stream, -1, 0, &type_print_raw_options);
94af9270
KS
640 fputs_filtered (") ", stream);
641 }
0d5de010
DJ
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
edf0c1b7
TT
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 }
0d5de010
DJ
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
666static int
ad4820ab 667gnuv3_method_ptr_size (struct type *type)
0d5de010 668{
561d3825 669 struct gdbarch *gdbarch = get_type_arch (type);
d8734c88 670
ad4820ab 671 return 2 * TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
0d5de010
DJ
672}
673
674/* GNU v3 implementation of cplus_make_method_ptr. */
675
676static void
ad4820ab
UW
677gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
678 CORE_ADDR value, int is_virtual)
0d5de010 679{
561d3825 680 struct gdbarch *gdbarch = get_type_arch (type);
ad4820ab 681 int size = TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
34877895 682 enum bfd_endian byte_order = type_byte_order (type);
0d5de010
DJ
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
ad4820ab 693 if (!gdbarch_vbit_in_delta (gdbarch))
0d5de010 694 {
e17a4113
UW
695 store_unsigned_integer (contents, size, byte_order, value | is_virtual);
696 store_unsigned_integer (contents + size, size, byte_order, 0);
0d5de010
DJ
697 }
698 else
699 {
e17a4113
UW
700 store_unsigned_integer (contents, size, byte_order, value);
701 store_unsigned_integer (contents + size, size, byte_order, is_virtual);
0d5de010
DJ
702 }
703}
704
705/* GNU v3 implementation of cplus_method_ptr_to_value. */
706
707static struct value *
708gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
709{
ad4820ab 710 struct gdbarch *gdbarch;
0d5de010
DJ
711 const gdb_byte *contents = value_contents (method_ptr);
712 CORE_ADDR ptr_value;
09e2d7c7 713 struct type *self_type, *final_type, *method_type;
0d5de010 714 LONGEST adjustment;
0d5de010
DJ
715 int vbit;
716
09e2d7c7
DE
717 self_type = TYPE_SELF_TYPE (check_typedef (value_type (method_ptr)));
718 final_type = lookup_pointer_type (self_type);
0d5de010
DJ
719
720 method_type = TYPE_TARGET_TYPE (check_typedef (value_type (method_ptr)));
721
fead6908 722 /* Extract the pointer to member. */
09e2d7c7 723 gdbarch = get_type_arch (self_type);
ad4820ab 724 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
0d5de010
DJ
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. */
ad4820ab 745 *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
2497b498 746 *this_p = value_ptradd (*this_p, adjustment);
0d5de010
DJ
747 *this_p = value_cast (final_type, *this_p);
748
749 if (vbit)
750 {
ad4820ab 751 LONGEST voffset;
d8734c88 752
ed09d7da 753 voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
ad4820ab
UW
754 return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
755 method_type, voffset);
0d5de010
DJ
756 }
757 else
758 return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
759}
760
c4aeac85
TT
761/* Objects of this type are stored in a hash table and a vector when
762 printing the vtables for a class. */
763
764struct 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
c4aeac85
TT
774/* Hash function for value_and_voffset. */
775
776static hashval_t
777hash_value_and_voffset (const void *p)
778{
9a3c8263 779 const struct value_and_voffset *o = (const struct value_and_voffset *) p;
c4aeac85
TT
780
781 return value_address (o->value) + value_embedded_offset (o->value);
782}
783
784/* Equality function for value_and_voffset. */
785
786static int
787eq_value_and_voffset (const void *a, const void *b)
788{
9a3c8263
SM
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;
c4aeac85
TT
791
792 return (value_address (ova->value) + value_embedded_offset (ova->value)
793 == value_address (ovb->value) + value_embedded_offset (ovb->value));
794}
795
59d3651b 796/* Comparison function for value_and_voffset. */
c4aeac85 797
59d3651b
TT
798static bool
799compare_value_and_voffset (const struct value_and_voffset *va,
800 const struct value_and_voffset *vb)
c4aeac85 801{
59d3651b
TT
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;
c4aeac85
TT
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
816static void
817compute_vtable_size (htab_t offset_hash,
59d3651b 818 std::vector<value_and_voffset *> *offset_vec,
c4aeac85
TT
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;
c4aeac85 825
5f4ce105
DE
826 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT);
827
c4aeac85
TT
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)
9a3c8263 837 current_vo = (struct value_and_voffset *) *slot;
c4aeac85
TT
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;
59d3651b 844 offset_vec->push_back (current_vo);
c4aeac85
TT
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
873static void
874print_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 {
cafe75b0
JK
897 /* Initialize it just to avoid a GCC false warning. */
898 CORE_ADDR addr = 0;
492d29ea 899 int got_error = 0;
c4aeac85 900 struct value *vfn;
c4aeac85
TT
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
a70b8144 911 try
c4aeac85
TT
912 {
913 addr = value_as_address (vfn);
914 }
230d2906 915 catch (const gdb_exception_error &ex)
492d29ea 916 {
7f6aba03
TT
917 fprintf_styled (gdb_stdout, metadata_style.style (),
918 _("<error: %s>"), ex.what ());
492d29ea
PA
919 got_error = 1;
920 }
492d29ea
PA
921
922 if (!got_error)
edf0c1b7 923 print_function_pointer_address (opts, gdbarch, addr, gdb_stdout);
c4aeac85
TT
924 printf_filtered ("\n");
925 }
926}
927
928/* Implementation of the print_vtable method. */
929
930static void
931gnuv3_print_vtable (struct value *value)
932{
933 struct gdbarch *gdbarch;
934 struct type *type;
935 struct value *vtable;
936 struct value_print_options opts;
59d3651b 937 int count;
c4aeac85
TT
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);
5f4ce105
DE
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)));
c4aeac85
TT
962
963 if (!vtable)
964 {
965 printf_filtered (_("This object does not have a virtual function table\n"));
966 return;
967 }
968
fc4007c9
TT
969 htab_up offset_hash (htab_create_alloc (1, hash_value_and_voffset,
970 eq_value_and_voffset,
971 xfree, xcalloc, xfree));
59d3651b 972 std::vector<value_and_voffset *> result_vec;
c4aeac85 973
fc4007c9 974 compute_vtable_size (offset_hash.get (), &result_vec, value);
59d3651b
TT
975 std::sort (result_vec.begin (), result_vec.end (),
976 compare_value_and_voffset);
c4aeac85
TT
977
978 count = 0;
59d3651b 979 for (value_and_voffset *iter : result_vec)
c4aeac85
TT
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 }
c4aeac85
TT
989}
990
6e72ca20
TT
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
997static void *
998build_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
8d749320 1010 field_list = XCNEWVEC (struct field, 2);
6e72ca20
TT
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
77b7c781 1030 t = arch_type (arch, TYPE_CODE_STRUCT, offset * TARGET_CHAR_BIT, NULL);
6e72ca20
TT
1031 TYPE_NFIELDS (t) = field - field_list;
1032 TYPE_FIELDS (t) = field_list;
e86ca25f 1033 TYPE_NAME (t) = "gdb_gnu_v3_type_info";
6e72ca20
TT
1034 INIT_CPLUS_SPECIFIC (t);
1035
1036 return t;
1037}
1038
1039/* Implement the 'get_typeid_type' method. */
1040
1041static struct type *
1042gnuv3_get_typeid_type (struct gdbarch *gdbarch)
1043{
1044 struct symbol *typeinfo;
1045 struct type *typeinfo_type;
1046
d12307c1
PMR
1047 typeinfo = lookup_symbol ("std::type_info", NULL, STRUCT_DOMAIN,
1048 NULL).symbol;
6e72ca20 1049 if (typeinfo == NULL)
9a3c8263
SM
1050 typeinfo_type
1051 = (struct type *) gdbarch_data (gdbarch, std_type_info_gdbarch_data);
6e72ca20
TT
1052 else
1053 typeinfo_type = SYMBOL_TYPE (typeinfo);
1054
1055 return typeinfo_type;
1056}
1057
1058/* Implement the 'get_typeid' method. */
1059
1060static struct value *
1061gnuv3_get_typeid (struct value *value)
1062{
1063 struct type *typeinfo_type;
1064 struct type *type;
1065 struct gdbarch *gdbarch;
6e72ca20 1066 struct value *result;
2f408ecb 1067 std::string type_name, canonical;
6e72ca20
TT
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
fe978cb0 1086 type_name = type_to_string (type);
2f408ecb 1087 if (type_name.empty ())
6e72ca20 1088 error (_("cannot find typeinfo for unnamed type"));
6e72ca20
TT
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 *". */
2f408ecb
PA
1094 canonical = cp_canonicalize_string (type_name.c_str ());
1095 if (!canonical.empty ())
1096 type_name = canonical;
6e72ca20
TT
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. */
4753d33b 1102 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
6e72ca20
TT
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)
2f408ecb
PA
1111 error (_("cannot find typeinfo for object of type '%s'"),
1112 type_name.c_str ());
6e72ca20
TT
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 {
2f408ecb
PA
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);
6e72ca20 1122
3b7344d5 1123 if (minsym.minsym == NULL)
2f408ecb 1124 error (_("could not find typeinfo symbol for '%s'"), type_name.c_str ());
6e72ca20 1125
77e371c0 1126 result = value_at_lazy (typeinfo_type, BMSYMBOL_VALUE_ADDRESS (minsym));
6e72ca20
TT
1127 }
1128
6e72ca20
TT
1129 return result;
1130}
1131
cc16e6c9 1132/* Implement the 'get_typename_from_type_info' method. */
72f1fe8a 1133
2f408ecb 1134static std::string
72f1fe8a
TT
1135gnuv3_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)
c9d95fa3 1152 symname = typeinfo_sym.minsym->demangled_name ();
72f1fe8a
TT
1153 if (symname == NULL || strncmp (symname, TYPEINFO_PREFIX,
1154 TYPEINFO_PREFIX_LEN))
1155 error (_("typeinfo symbol '%s' has unexpected name"),
c9d95fa3 1156 typeinfo_sym.minsym->linkage_name ());
72f1fe8a
TT
1157 class_name = symname + TYPEINFO_PREFIX_LEN;
1158
1159 /* Strip off @plt and version suffixes. */
1160 atsign = strchr (class_name, '@');
1161 if (atsign != NULL)
2f408ecb
PA
1162 return std::string (class_name, atsign - class_name);
1163 return class_name;
72f1fe8a
TT
1164}
1165
1166/* Implement the 'get_type_from_type_info' method. */
1167
1168static struct type *
1169gnuv3_get_type_from_type_info (struct value *type_info_ptr)
1170{
72f1fe8a
TT
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. */
2f408ecb
PA
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);
72f1fe8a
TT
1179}
1180
b18be20d
DJ
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
1184static CORE_ADDR
52f729a7 1185gnuv3_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc)
b18be20d 1186{
a513d1e8 1187 CORE_ADDR real_stop_pc, method_stop_pc, func_addr;
9970f04b 1188 struct gdbarch *gdbarch = get_frame_arch (frame);
3b7344d5 1189 struct bound_minimal_symbol thunk_sym, fn_sym;
b18be20d 1190 struct obj_section *section;
0d5cff50 1191 const char *thunk_name, *fn_name;
b18be20d 1192
9970f04b 1193 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
b18be20d
DJ
1194 if (real_stop_pc == 0)
1195 real_stop_pc = stop_pc;
1196
1197 /* Find the linker symbol for this potential thunk. */
3b7344d5 1198 thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
b18be20d 1199 section = find_pc_section (real_stop_pc);
3b7344d5 1200 if (thunk_sym.minsym == NULL || section == NULL)
b18be20d
DJ
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. */
c9d95fa3 1206 thunk_name = thunk_sym.minsym->demangled_name ();
b18be20d
DJ
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);
3b7344d5 1212 if (fn_sym.minsym == NULL)
b18be20d
DJ
1213 return 0;
1214
77e371c0 1215 method_stop_pc = BMSYMBOL_VALUE_ADDRESS (fn_sym);
a513d1e8
LM
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,
8b88a78e 1222 current_top_target ());
a513d1e8
LM
1223 if (func_addr != 0)
1224 method_stop_pc = func_addr;
1225
e76f05fa 1226 real_stop_pc = gdbarch_skip_trampoline_code
9970f04b 1227 (gdbarch, frame, method_stop_pc);
b18be20d
DJ
1228 if (real_stop_pc == 0)
1229 real_stop_pc = method_stop_pc;
1230
1231 return real_stop_pc;
1232}
1233
62bf63d7
TBA
1234/* A member function is in one these states. */
1235
1236enum 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
1247static definition_style
1248get_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
1276static bool
1277is_user_provided_def (definition_style def)
1278{
1279 return def == EXPLICIT || def == DEFAULTED_OUTSIDE;
1280}
1281
1282static bool
1283is_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
1295static bool
1296is_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
1337static bool
1338is_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
1347static bool
1348is_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
9d084466 1355/* Return pass-by-reference information for the given TYPE.
41f1b697
DJ
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
62bf63d7 1363 For return values with non-trivial copy/move constructors or
41f1b697
DJ
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
62bf63d7
TBA
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. */
9d084466
TBA
1372
1373static struct language_pass_by_ref_info
41f1b697
DJ
1374gnuv3_pass_by_reference (struct type *type)
1375{
1376 int fieldnum, fieldelem;
1377
f168693b 1378 type = check_typedef (type);
41f1b697 1379
9d084466
TBA
1380 /* Start with the default values. */
1381 struct language_pass_by_ref_info info
1382 = default_pass_by_reference (type);
1383
62bf63d7
TBA
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;
9d084466 1390
41f1b697
DJ
1391 /* We're only interested in things that can have methods. */
1392 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
41f1b697 1393 && TYPE_CODE (type) != TYPE_CODE_UNION)
9d084466 1394 return info;
41f1b697 1395
62bf63d7
TBA
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
ebb8ece2
SC
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))
62bf63d7 1416 is_dynamic = true;
ebb8ece2 1417
41f1b697
DJ
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);
0d5cff50 1423 const char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
41f1b697
DJ
1424 struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
1425
41f1b697 1426 if (name[0] == '~')
9d084466 1427 {
62bf63d7
TBA
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);
9d084466 1432 }
62bf63d7
TBA
1433 else if (is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem))
1434 || TYPE_FN_FIELD_CONSTRUCTOR (fn, fieldelem))
82c48ac7 1435 {
62bf63d7
TBA
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))
3433cfa5 1461 {
62bf63d7
TBA
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);
3433cfa5 1468 }
82c48ac7 1469 }
41f1b697
DJ
1470 }
1471
62bf63d7
TBA
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
41f1b697
DJ
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
bceffbf3 1501 of complete class type. Also ignore any static members. */
41f1b697 1502 for (fieldnum = 0; fieldnum < TYPE_NFIELDS (type); fieldnum++)
9d084466
TBA
1503 if (!field_is_static (&TYPE_FIELD (type, fieldnum)))
1504 {
62bf63d7
TBA
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
9d084466 1511 struct language_pass_by_ref_info field_info
62bf63d7
TBA
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;
9d084466 1518 if (!field_info.trivially_copyable)
62bf63d7
TBA
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;
9d084466 1524 }
41f1b697 1525
62bf63d7
TBA
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
9d084466 1534 return info;
41f1b697
DJ
1535}
1536
7ed49443
JB
1537static void
1538init_gnuv3_ops (void)
1539{
0963b4bd
MS
1540 vtable_type_gdbarch_data
1541 = gdbarch_data_register_post_init (build_gdb_vtable_type);
6e72ca20
TT
1542 std_type_info_gdbarch_data
1543 = gdbarch_data_register_post_init (build_std_type_info_type);
7ed49443
JB
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";
358777b0
EZ
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;
7ed49443
JB
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;
1514d34e 1556 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
0d5de010
DJ
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;
c4aeac85 1561 gnu_v3_abi_ops.print_vtable = gnuv3_print_vtable;
6e72ca20
TT
1562 gnu_v3_abi_ops.get_typeid = gnuv3_get_typeid;
1563 gnu_v3_abi_ops.get_typeid_type = gnuv3_get_typeid_type;
72f1fe8a 1564 gnu_v3_abi_ops.get_type_from_type_info = gnuv3_get_type_from_type_info;
cc16e6c9
TT
1565 gnu_v3_abi_ops.get_typename_from_type_info
1566 = gnuv3_get_typename_from_type_info;
b18be20d 1567 gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
41f1b697 1568 gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
7ed49443
JB
1569}
1570
7ed49443
JB
1571void
1572_initialize_gnu_v3_abi (void)
1573{
1574 init_gnuv3_ops ();
1575
fe1f4a5e 1576 register_cp_abi (&gnu_v3_abi_ops);
1605ef26 1577 set_cp_abi_as_auto_default (gnu_v3_abi_ops.shortname);
7ed49443 1578}
This page took 1.504211 seconds and 4 git commands to generate.