* gdb.cp/classes.exp (test_ptype_class_objects): Remove
[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
0b302171 4 Copyright (C) 2001-2003, 2005-2012 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"
b18be20d 26#include "objfiles.h"
0d5de010 27#include "valprint.h"
94af9270 28#include "c-lang.h"
c4aeac85 29#include "exceptions.h"
0d5de010 30
3d499020 31#include "gdb_assert.h"
5f8a3188 32#include "gdb_string.h"
7ed49443 33
b27b8843 34static struct cp_abi_ops gnu_v3_abi_ops;
7ed49443
JB
35
36static int
37gnuv3_is_vtable_name (const char *name)
38{
39 return strncmp (name, "_ZTV", 4) == 0;
40}
41
42static int
43gnuv3_is_operator_name (const char *name)
44{
45 return strncmp (name, "operator", 8) == 0;
46}
47
48
49/* To help us find the components of a vtable, we build ourselves a
50 GDB type object representing the vtable structure. Following the
51 V3 ABI, it goes something like this:
52
53 struct gdb_gnu_v3_abi_vtable {
54
55 / * An array of virtual call and virtual base offsets. The real
56 length of this array depends on the class hierarchy; we use
57 negative subscripts to access the elements. Yucky, but
58 better than the alternatives. * /
59 ptrdiff_t vcall_and_vbase_offsets[0];
60
61 / * The offset from a virtual pointer referring to this table
62 to the top of the complete object. * /
63 ptrdiff_t offset_to_top;
64
65 / * The type_info pointer for this class. This is really a
66 std::type_info *, but GDB doesn't really look at the
67 type_info object itself, so we don't bother to get the type
68 exactly right. * /
69 void *type_info;
70
71 / * Virtual table pointers in objects point here. * /
72
73 / * Virtual function pointers. Like the vcall/vbase array, the
74 real length of this table depends on the class hierarchy. * /
75 void (*virtual_functions[0]) ();
76
77 };
78
79 The catch, of course, is that the exact layout of this table
80 depends on the ABI --- word size, endianness, alignment, etc. So
81 the GDB type object is actually a per-architecture kind of thing.
82
83 vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
84 which refers to the struct type * for this structure, laid out
85 appropriately for the architecture. */
b27b8843 86static struct gdbarch_data *vtable_type_gdbarch_data;
7ed49443
JB
87
88
89/* Human-readable names for the numbers of the fields above. */
90enum {
91 vtable_field_vcall_and_vbase_offsets,
92 vtable_field_offset_to_top,
93 vtable_field_type_info,
94 vtable_field_virtual_functions
95};
96
97
98/* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
99 described above, laid out appropriately for ARCH.
100
101 We use this function as the gdbarch per-architecture data
9970f04b 102 initialization function. */
7ed49443
JB
103static void *
104build_gdb_vtable_type (struct gdbarch *arch)
105{
106 struct type *t;
107 struct field *field_list, *field;
108 int offset;
109
110 struct type *void_ptr_type
fde6c819 111 = builtin_type (arch)->builtin_data_ptr;
7ed49443 112 struct type *ptr_to_void_fn_type
fde6c819 113 = builtin_type (arch)->builtin_func_ptr;
7ed49443
JB
114
115 /* ARCH can't give us the true ptrdiff_t type, so we guess. */
116 struct type *ptrdiff_type
e9bb382b 117 = arch_integer_type (arch, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
7ed49443
JB
118
119 /* We assume no padding is necessary, since GDB doesn't know
120 anything about alignment at the moment. If this assumption bites
121 us, we should add a gdbarch method which, given a type, returns
122 the alignment that type requires, and then use that here. */
123
124 /* Build the field list. */
125 field_list = xmalloc (sizeof (struct field [4]));
126 memset (field_list, 0, sizeof (struct field [4]));
127 field = &field_list[0];
128 offset = 0;
129
130 /* ptrdiff_t vcall_and_vbase_offsets[0]; */
131 FIELD_NAME (*field) = "vcall_and_vbase_offsets";
e3506a9f 132 FIELD_TYPE (*field) = lookup_array_range_type (ptrdiff_type, 0, -1);
f41f5e61 133 SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
7ed49443
JB
134 offset += TYPE_LENGTH (FIELD_TYPE (*field));
135 field++;
136
137 /* ptrdiff_t offset_to_top; */
138 FIELD_NAME (*field) = "offset_to_top";
139 FIELD_TYPE (*field) = ptrdiff_type;
f41f5e61 140 SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
7ed49443
JB
141 offset += TYPE_LENGTH (FIELD_TYPE (*field));
142 field++;
143
144 /* void *type_info; */
145 FIELD_NAME (*field) = "type_info";
146 FIELD_TYPE (*field) = void_ptr_type;
f41f5e61 147 SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
7ed49443
JB
148 offset += TYPE_LENGTH (FIELD_TYPE (*field));
149 field++;
150
151 /* void (*virtual_functions[0]) (); */
152 FIELD_NAME (*field) = "virtual_functions";
e3506a9f 153 FIELD_TYPE (*field) = lookup_array_range_type (ptr_to_void_fn_type, 0, -1);
f41f5e61 154 SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
7ed49443
JB
155 offset += TYPE_LENGTH (FIELD_TYPE (*field));
156 field++;
157
158 /* We assumed in the allocation above that there were four fields. */
3d499020 159 gdb_assert (field == (field_list + 4));
7ed49443 160
e9bb382b 161 t = arch_type (arch, TYPE_CODE_STRUCT, offset, NULL);
7ed49443
JB
162 TYPE_NFIELDS (t) = field - field_list;
163 TYPE_FIELDS (t) = field_list;
164 TYPE_TAG_NAME (t) = "gdb_gnu_v3_abi_vtable";
e9bb382b 165 INIT_CPLUS_SPECIFIC (t);
7ed49443
JB
166
167 return t;
168}
169
170
ed09d7da
KB
171/* Return the ptrdiff_t type used in the vtable type. */
172static struct type *
173vtable_ptrdiff_type (struct gdbarch *gdbarch)
174{
175 struct type *vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data);
176
177 /* The "offset_to_top" field has the appropriate (ptrdiff_t) type. */
178 return TYPE_FIELD_TYPE (vtable_type, vtable_field_offset_to_top);
179}
180
7ed49443
JB
181/* Return the offset from the start of the imaginary `struct
182 gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
183 (i.e., where objects' virtual table pointers point). */
184static int
ad4820ab 185vtable_address_point_offset (struct gdbarch *gdbarch)
7ed49443 186{
ad4820ab 187 struct type *vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data);
7ed49443
JB
188
189 return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions)
190 / TARGET_CHAR_BIT);
191}
192
193
d48cc9dd
DJ
194/* Determine whether structure TYPE is a dynamic class. Cache the
195 result. */
196
197static int
198gnuv3_dynamic_class (struct type *type)
199{
200 int fieldnum, fieldelem;
201
202 if (TYPE_CPLUS_DYNAMIC (type))
203 return TYPE_CPLUS_DYNAMIC (type) == 1;
204
205 ALLOCATE_CPLUS_STRUCT_TYPE (type);
206
207 for (fieldnum = 0; fieldnum < TYPE_N_BASECLASSES (type); fieldnum++)
208 if (BASETYPE_VIA_VIRTUAL (type, fieldnum)
209 || gnuv3_dynamic_class (TYPE_FIELD_TYPE (type, fieldnum)))
210 {
211 TYPE_CPLUS_DYNAMIC (type) = 1;
212 return 1;
213 }
214
215 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
216 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
217 fieldelem++)
218 {
219 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, fieldnum);
220
221 if (TYPE_FN_FIELD_VIRTUAL_P (f, fieldelem))
222 {
223 TYPE_CPLUS_DYNAMIC (type) = 1;
224 return 1;
225 }
226 }
227
228 TYPE_CPLUS_DYNAMIC (type) = -1;
229 return 0;
230}
231
232/* Find the vtable for a value of CONTAINER_TYPE located at
233 CONTAINER_ADDR. Return a value of the correct vtable type for this
234 architecture, or NULL if CONTAINER does not have a vtable. */
235
236static struct value *
237gnuv3_get_vtable (struct gdbarch *gdbarch,
238 struct type *container_type, CORE_ADDR container_addr)
239{
240 struct type *vtable_type = gdbarch_data (gdbarch,
241 vtable_type_gdbarch_data);
242 struct type *vtable_pointer_type;
243 struct value *vtable_pointer;
244 CORE_ADDR vtable_address;
245
246 /* If this type does not have a virtual table, don't read the first
247 field. */
248 if (!gnuv3_dynamic_class (check_typedef (container_type)))
249 return NULL;
250
251 /* We do not consult the debug information to find the virtual table.
252 The ABI specifies that it is always at offset zero in any class,
253 and debug information may not represent it.
254
255 We avoid using value_contents on principle, because the object might
256 be large. */
257
258 /* Find the type "pointer to virtual table". */
259 vtable_pointer_type = lookup_pointer_type (vtable_type);
260
261 /* Load it from the start of the class. */
262 vtable_pointer = value_at (vtable_pointer_type, container_addr);
263 vtable_address = value_as_address (vtable_pointer);
264
265 /* Correct it to point at the start of the virtual table, rather
266 than the address point. */
267 return value_at_lazy (vtable_type,
0963b4bd
MS
268 vtable_address
269 - vtable_address_point_offset (gdbarch));
d48cc9dd
DJ
270}
271
272
7ed49443
JB
273static struct type *
274gnuv3_rtti_type (struct value *value,
275 int *full_p, int *top_p, int *using_enc_p)
276{
ad4820ab 277 struct gdbarch *gdbarch;
df407dfe 278 struct type *values_type = check_typedef (value_type (value));
7ed49443
JB
279 struct value *vtable;
280 struct minimal_symbol *vtable_symbol;
281 const char *vtable_symbol_name;
282 const char *class_name;
7ed49443
JB
283 struct type *run_time_type;
284 LONGEST offset_to_top;
285
286 /* We only have RTTI for class objects. */
df407dfe 287 if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
7ed49443
JB
288 return NULL;
289
eb2a6f42
TT
290 /* Java doesn't have RTTI following the C++ ABI. */
291 if (TYPE_CPLUS_REALLY_JAVA (values_type))
292 return NULL;
293
ad4820ab 294 /* Determine architecture. */
50810684 295 gdbarch = get_type_arch (values_type);
7ed49443 296
21cfb3b6
DJ
297 if (using_enc_p)
298 *using_enc_p = 0;
299
d48cc9dd
DJ
300 vtable = gnuv3_get_vtable (gdbarch, value_type (value),
301 value_as_address (value_addr (value)));
302 if (vtable == NULL)
303 return NULL;
304
7ed49443
JB
305 /* Find the linker symbol for this vtable. */
306 vtable_symbol
42ae5230 307 = lookup_minimal_symbol_by_pc (value_address (vtable)
13c3b5f5 308 + value_embedded_offset (vtable));
7ed49443
JB
309 if (! vtable_symbol)
310 return NULL;
311
312 /* The symbol's demangled name should be something like "vtable for
313 CLASS", where CLASS is the name of the run-time type of VALUE.
314 If we didn't like this approach, we could instead look in the
315 type_info object itself to get the class name. But this way
316 should work just as well, and doesn't read target memory. */
317 vtable_symbol_name = SYMBOL_DEMANGLED_NAME (vtable_symbol);
98081e55
PB
318 if (vtable_symbol_name == NULL
319 || strncmp (vtable_symbol_name, "vtable for ", 11))
f773fdbb 320 {
8a3fe4f8 321 warning (_("can't find linker symbol for virtual table for `%s' value"),
0a07729b 322 TYPE_SAFE_NAME (values_type));
f773fdbb 323 if (vtable_symbol_name)
8a3fe4f8 324 warning (_(" found `%s' instead"), vtable_symbol_name);
f773fdbb
JM
325 return NULL;
326 }
7ed49443
JB
327 class_name = vtable_symbol_name + 11;
328
329 /* Try to look up the class name as a type name. */
0963b4bd 330 /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
362ff856
MC
331 run_time_type = cp_lookup_rtti_type (class_name, NULL);
332 if (run_time_type == NULL)
333 return NULL;
7ed49443
JB
334
335 /* Get the offset from VALUE to the top of the complete object.
336 NOTE: this is the reverse of the meaning of *TOP_P. */
337 offset_to_top
338 = value_as_long (value_field (vtable, vtable_field_offset_to_top));
339
340 if (full_p)
13c3b5f5 341 *full_p = (- offset_to_top == value_embedded_offset (value)
4754a64e 342 && (TYPE_LENGTH (value_enclosing_type (value))
7ed49443
JB
343 >= TYPE_LENGTH (run_time_type)));
344 if (top_p)
345 *top_p = - offset_to_top;
7ed49443
JB
346 return run_time_type;
347}
348
0d5de010
DJ
349/* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
350 function, of type FNTYPE. */
7ed49443 351
0d5de010 352static struct value *
ad4820ab
UW
353gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
354 struct type *fntype, int vtable_index)
0d5de010 355{
d48cc9dd
DJ
356 struct value *vtable, *vfn;
357
358 /* Every class with virtual functions must have a vtable. */
359 vtable = gnuv3_get_vtable (gdbarch, value_type (container),
360 value_as_address (value_addr (container)));
361 gdb_assert (vtable != NULL);
7ed49443
JB
362
363 /* Fetch the appropriate function pointer from the vtable. */
364 vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
2497b498 365 vtable_index);
7ed49443 366
0d5de010
DJ
367 /* If this architecture uses function descriptors directly in the vtable,
368 then the address of the vtable entry is actually a "function pointer"
369 (i.e. points to the descriptor). We don't need to scale the index
370 by the size of a function descriptor; GCC does that before outputing
371 debug information. */
ad4820ab 372 if (gdbarch_vtable_function_descriptors (gdbarch))
0d5de010 373 vfn = value_addr (vfn);
7ed49443 374
0d5de010
DJ
375 /* Cast the function pointer to the appropriate type. */
376 vfn = value_cast (lookup_pointer_type (fntype), vfn);
76b79d6e 377
7ed49443
JB
378 return vfn;
379}
380
0d5de010
DJ
381/* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h
382 for a description of the arguments. */
383
384static struct value *
385gnuv3_virtual_fn_field (struct value **value_p,
386 struct fn_field *f, int j,
387 struct type *vfn_base, int offset)
388{
389 struct type *values_type = check_typedef (value_type (*value_p));
ad4820ab 390 struct gdbarch *gdbarch;
0d5de010
DJ
391
392 /* Some simple sanity checks. */
393 if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
394 error (_("Only classes can have virtual functions."));
395
ad4820ab 396 /* Determine architecture. */
50810684 397 gdbarch = get_type_arch (values_type);
ad4820ab 398
0d5de010
DJ
399 /* Cast our value to the base class which defines this virtual
400 function. This takes care of any necessary `this'
401 adjustments. */
402 if (vfn_base != values_type)
403 *value_p = value_cast (vfn_base, *value_p);
404
ad4820ab 405 return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
0d5de010
DJ
406 TYPE_FN_FIELD_VOFFSET (f, j));
407}
408
1514d34e
DJ
409/* Compute the offset of the baseclass which is
410 the INDEXth baseclass of class TYPE,
411 for value at VALADDR (in host) at ADDRESS (in target).
412 The result is the offset of the baseclass value relative
413 to (the address of)(ARG) + OFFSET.
414
0963b4bd
MS
415 -1 is returned on error. */
416
b9362cc7 417static int
8af8e3bc
PA
418gnuv3_baseclass_offset (struct type *type, int index,
419 const bfd_byte *valaddr, int embedded_offset,
420 CORE_ADDR address, const struct value *val)
1514d34e 421{
ad4820ab 422 struct gdbarch *gdbarch;
ad4820ab 423 struct type *ptr_type;
79d5b63a 424 struct value *vtable;
2497b498 425 struct value *vbase_array;
1514d34e 426 long int cur_base_offset, base_offset;
1514d34e 427
ad4820ab 428 /* Determine architecture. */
50810684 429 gdbarch = get_type_arch (type);
ad4820ab
UW
430 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
431
1514d34e 432 /* If it isn't a virtual base, this is easy. The offset is in the
b1af9e97
TT
433 type definition. Likewise for Java, which doesn't really have
434 virtual inheritance in the C++ sense. */
435 if (!BASETYPE_VIA_VIRTUAL (type, index) || TYPE_CPLUS_REALLY_JAVA (type))
1514d34e
DJ
436 return TYPE_BASECLASS_BITPOS (type, index) / 8;
437
438 /* To access a virtual base, we need to use the vbase offset stored in
439 our vtable. Recent GCC versions provide this information. If it isn't
440 available, we could get what we needed from RTTI, or from drawing the
441 complete inheritance graph based on the debug info. Neither is
442 worthwhile. */
443 cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
ad4820ab 444 if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
8a3fe4f8 445 error (_("Expected a negative vbase offset (old compiler?)"));
1514d34e 446
ad4820ab
UW
447 cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
448 if ((- cur_base_offset) % TYPE_LENGTH (ptr_type) != 0)
8a3fe4f8 449 error (_("Misaligned vbase offset."));
ad4820ab 450 cur_base_offset = cur_base_offset / ((int) TYPE_LENGTH (ptr_type));
1514d34e 451
8af8e3bc 452 vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset);
d48cc9dd 453 gdb_assert (vtable != NULL);
1514d34e 454 vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
2497b498 455 base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
1514d34e
DJ
456 return base_offset;
457}
7ed49443 458
0d5de010
DJ
459/* Locate a virtual method in DOMAIN or its non-virtual base classes
460 which has virtual table index VOFFSET. The method has an associated
461 "this" adjustment of ADJUSTMENT bytes. */
462
2c0b251b 463static const char *
0d5de010
DJ
464gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
465 LONGEST adjustment)
466{
467 int i;
0d5de010
DJ
468
469 /* Search this class first. */
0d5de010
DJ
470 if (adjustment == 0)
471 {
472 int len;
473
474 len = TYPE_NFN_FIELDS (domain);
475 for (i = 0; i < len; i++)
476 {
477 int len2, j;
478 struct fn_field *f;
479
480 f = TYPE_FN_FIELDLIST1 (domain, i);
481 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
482
483 check_stub_method_group (domain, i);
484 for (j = 0; j < len2; j++)
485 if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset)
486 return TYPE_FN_FIELD_PHYSNAME (f, j);
487 }
488 }
489
490 /* Next search non-virtual bases. If it's in a virtual base,
491 we're out of luck. */
492 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
493 {
494 int pos;
495 struct type *basetype;
496
497 if (BASETYPE_VIA_VIRTUAL (domain, i))
498 continue;
499
500 pos = TYPE_BASECLASS_BITPOS (domain, i) / 8;
501 basetype = TYPE_FIELD_TYPE (domain, i);
502 /* Recurse with a modified adjustment. We don't need to adjust
503 voffset. */
504 if (adjustment >= pos && adjustment < pos + TYPE_LENGTH (basetype))
505 return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
506 }
507
508 return NULL;
509}
510
fead6908
UW
511/* Decode GNU v3 method pointer. */
512
513static int
ad4820ab
UW
514gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
515 const gdb_byte *contents,
fead6908
UW
516 CORE_ADDR *value_p,
517 LONGEST *adjustment_p)
518{
ad4820ab 519 struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
ed09d7da 520 struct type *offset_type = vtable_ptrdiff_type (gdbarch);
e17a4113 521 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
fead6908
UW
522 CORE_ADDR ptr_value;
523 LONGEST voffset, adjustment;
524 int vbit;
525
526 /* Extract the pointer to member. The first element is either a pointer
527 or a vtable offset. For pointers, we need to use extract_typed_address
528 to allow the back-end to convert the pointer to a GDB address -- but
529 vtable offsets we must handle as integers. At this point, we do not
530 yet know which case we have, so we extract the value under both
531 interpretations and choose the right one later on. */
532 ptr_value = extract_typed_address (contents, funcptr_type);
e17a4113
UW
533 voffset = extract_signed_integer (contents,
534 TYPE_LENGTH (funcptr_type), byte_order);
fead6908 535 contents += TYPE_LENGTH (funcptr_type);
e17a4113
UW
536 adjustment = extract_signed_integer (contents,
537 TYPE_LENGTH (offset_type), byte_order);
fead6908 538
ad4820ab 539 if (!gdbarch_vbit_in_delta (gdbarch))
fead6908
UW
540 {
541 vbit = voffset & 1;
542 voffset = voffset ^ vbit;
543 }
544 else
545 {
546 vbit = adjustment & 1;
547 adjustment = adjustment >> 1;
548 }
549
550 *value_p = vbit? voffset : ptr_value;
551 *adjustment_p = adjustment;
552 return vbit;
553}
554
0d5de010
DJ
555/* GNU v3 implementation of cplus_print_method_ptr. */
556
557static void
558gnuv3_print_method_ptr (const gdb_byte *contents,
559 struct type *type,
560 struct ui_file *stream)
561{
ad4820ab 562 struct type *domain = TYPE_DOMAIN_TYPE (type);
50810684 563 struct gdbarch *gdbarch = get_type_arch (domain);
0d5de010
DJ
564 CORE_ADDR ptr_value;
565 LONGEST adjustment;
0d5de010
DJ
566 int vbit;
567
0d5de010 568 /* Extract the pointer to member. */
ad4820ab 569 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
0d5de010
DJ
570
571 /* Check for NULL. */
572 if (ptr_value == 0 && vbit == 0)
573 {
574 fprintf_filtered (stream, "NULL");
575 return;
576 }
577
578 /* Search for a virtual method. */
579 if (vbit)
580 {
581 CORE_ADDR voffset;
582 const char *physname;
583
584 /* It's a virtual table offset, maybe in this class. Search
585 for a field with the correct vtable offset. First convert it
586 to an index, as used in TYPE_FN_FIELD_VOFFSET. */
ed09d7da 587 voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
0d5de010
DJ
588
589 physname = gnuv3_find_method_in (domain, voffset, adjustment);
590
591 /* If we found a method, print that. We don't bother to disambiguate
592 possible paths to the method based on the adjustment. */
593 if (physname)
594 {
595 char *demangled_name = cplus_demangle (physname,
596 DMGL_ANSI | DMGL_PARAMS);
d8734c88 597
94af9270
KS
598 fprintf_filtered (stream, "&virtual ");
599 if (demangled_name == NULL)
600 fputs_filtered (physname, stream);
601 else
0d5de010 602 {
0d5de010
DJ
603 fputs_filtered (demangled_name, stream);
604 xfree (demangled_name);
0d5de010 605 }
94af9270 606 return;
0d5de010
DJ
607 }
608 }
94af9270
KS
609 else if (ptr_value != 0)
610 {
611 /* Found a non-virtual function: print out the type. */
612 fputs_filtered ("(", stream);
613 c_print_type (type, "", stream, -1, 0);
614 fputs_filtered (") ", stream);
615 }
0d5de010
DJ
616
617 /* We didn't find it; print the raw data. */
618 if (vbit)
619 {
620 fprintf_filtered (stream, "&virtual table offset ");
621 print_longest (stream, 'd', 1, ptr_value);
622 }
623 else
edf0c1b7
TT
624 {
625 struct value_print_options opts;
626
627 get_user_print_options (&opts);
628 print_address_demangle (&opts, gdbarch, ptr_value, stream, demangle);
629 }
0d5de010
DJ
630
631 if (adjustment)
632 {
633 fprintf_filtered (stream, ", this adjustment ");
634 print_longest (stream, 'd', 1, adjustment);
635 }
636}
637
638/* GNU v3 implementation of cplus_method_ptr_size. */
639
640static int
ad4820ab 641gnuv3_method_ptr_size (struct type *type)
0d5de010 642{
561d3825 643 struct gdbarch *gdbarch = get_type_arch (type);
d8734c88 644
ad4820ab 645 return 2 * TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
0d5de010
DJ
646}
647
648/* GNU v3 implementation of cplus_make_method_ptr. */
649
650static void
ad4820ab
UW
651gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
652 CORE_ADDR value, int is_virtual)
0d5de010 653{
561d3825 654 struct gdbarch *gdbarch = get_type_arch (type);
ad4820ab 655 int size = TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
e17a4113 656 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
0d5de010
DJ
657
658 /* FIXME drow/2006-12-24: The adjustment of "this" is currently
659 always zero, since the method pointer is of the correct type.
660 But if the method pointer came from a base class, this is
661 incorrect - it should be the offset to the base. The best
662 fix might be to create the pointer to member pointing at the
663 base class and cast it to the derived class, but that requires
664 support for adjusting pointers to members when casting them -
665 not currently supported by GDB. */
666
ad4820ab 667 if (!gdbarch_vbit_in_delta (gdbarch))
0d5de010 668 {
e17a4113
UW
669 store_unsigned_integer (contents, size, byte_order, value | is_virtual);
670 store_unsigned_integer (contents + size, size, byte_order, 0);
0d5de010
DJ
671 }
672 else
673 {
e17a4113
UW
674 store_unsigned_integer (contents, size, byte_order, value);
675 store_unsigned_integer (contents + size, size, byte_order, is_virtual);
0d5de010
DJ
676 }
677}
678
679/* GNU v3 implementation of cplus_method_ptr_to_value. */
680
681static struct value *
682gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
683{
ad4820ab 684 struct gdbarch *gdbarch;
0d5de010
DJ
685 const gdb_byte *contents = value_contents (method_ptr);
686 CORE_ADDR ptr_value;
ad4820ab 687 struct type *domain_type, *final_type, *method_type;
0d5de010 688 LONGEST adjustment;
0d5de010
DJ
689 int vbit;
690
ad4820ab
UW
691 domain_type = TYPE_DOMAIN_TYPE (check_typedef (value_type (method_ptr)));
692 final_type = lookup_pointer_type (domain_type);
0d5de010
DJ
693
694 method_type = TYPE_TARGET_TYPE (check_typedef (value_type (method_ptr)));
695
fead6908 696 /* Extract the pointer to member. */
50810684 697 gdbarch = get_type_arch (domain_type);
ad4820ab 698 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
0d5de010
DJ
699
700 /* First convert THIS to match the containing type of the pointer to
701 member. This cast may adjust the value of THIS. */
702 *this_p = value_cast (final_type, *this_p);
703
704 /* Then apply whatever adjustment is necessary. This creates a somewhat
705 strange pointer: it claims to have type FINAL_TYPE, but in fact it
706 might not be a valid FINAL_TYPE. For instance, it might be a
707 base class of FINAL_TYPE. And if it's not the primary base class,
708 then printing it out as a FINAL_TYPE object would produce some pretty
709 garbage.
710
711 But we don't really know the type of the first argument in
712 METHOD_TYPE either, which is why this happens. We can't
713 dereference this later as a FINAL_TYPE, but once we arrive in the
714 called method we'll have debugging information for the type of
715 "this" - and that'll match the value we produce here.
716
717 You can provoke this case by casting a Base::* to a Derived::*, for
718 instance. */
ad4820ab 719 *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
2497b498 720 *this_p = value_ptradd (*this_p, adjustment);
0d5de010
DJ
721 *this_p = value_cast (final_type, *this_p);
722
723 if (vbit)
724 {
ad4820ab 725 LONGEST voffset;
d8734c88 726
ed09d7da 727 voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
ad4820ab
UW
728 return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
729 method_type, voffset);
0d5de010
DJ
730 }
731 else
732 return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
733}
734
c4aeac85
TT
735/* Objects of this type are stored in a hash table and a vector when
736 printing the vtables for a class. */
737
738struct value_and_voffset
739{
740 /* The value representing the object. */
741 struct value *value;
742
743 /* The maximum vtable offset we've found for any object at this
744 offset in the outermost object. */
745 int max_voffset;
746};
747
748typedef struct value_and_voffset *value_and_voffset_p;
749DEF_VEC_P (value_and_voffset_p);
750
751/* Hash function for value_and_voffset. */
752
753static hashval_t
754hash_value_and_voffset (const void *p)
755{
756 const struct value_and_voffset *o = p;
757
758 return value_address (o->value) + value_embedded_offset (o->value);
759}
760
761/* Equality function for value_and_voffset. */
762
763static int
764eq_value_and_voffset (const void *a, const void *b)
765{
766 const struct value_and_voffset *ova = a;
767 const struct value_and_voffset *ovb = b;
768
769 return (value_address (ova->value) + value_embedded_offset (ova->value)
770 == value_address (ovb->value) + value_embedded_offset (ovb->value));
771}
772
773/* qsort comparison function for value_and_voffset. */
774
775static int
776compare_value_and_voffset (const void *a, const void *b)
777{
778 const struct value_and_voffset * const *ova = a;
779 CORE_ADDR addra = (value_address ((*ova)->value)
780 + value_embedded_offset ((*ova)->value));
781 const struct value_and_voffset * const *ovb = b;
782 CORE_ADDR addrb = (value_address ((*ovb)->value)
783 + value_embedded_offset ((*ovb)->value));
784
785 if (addra < addrb)
786 return -1;
787 if (addra > addrb)
788 return 1;
789 return 0;
790}
791
792/* A helper function used when printing vtables. This determines the
793 key (most derived) sub-object at each address and also computes the
794 maximum vtable offset seen for the corresponding vtable. Updates
795 OFFSET_HASH and OFFSET_VEC with a new value_and_voffset object, if
796 needed. VALUE is the object to examine. */
797
798static void
799compute_vtable_size (htab_t offset_hash,
800 VEC (value_and_voffset_p) **offset_vec,
801 struct value *value)
802{
803 int i;
804 struct type *type = check_typedef (value_type (value));
805 void **slot;
806 struct value_and_voffset search_vo, *current_vo;
807 CORE_ADDR addr = value_address (value) + value_embedded_offset (value);
808
809 /* If the object is not dynamic, then we are done; as it cannot have
810 dynamic base types either. */
811 if (!gnuv3_dynamic_class (type))
812 return;
813
814 /* Update the hash and the vec, if needed. */
815 search_vo.value = value;
816 slot = htab_find_slot (offset_hash, &search_vo, INSERT);
817 if (*slot)
818 current_vo = *slot;
819 else
820 {
821 current_vo = XNEW (struct value_and_voffset);
822 current_vo->value = value;
823 current_vo->max_voffset = -1;
824 *slot = current_vo;
825 VEC_safe_push (value_and_voffset_p, *offset_vec, current_vo);
826 }
827
828 /* Update the value_and_voffset object with the highest vtable
829 offset from this class. */
830 for (i = 0; i < TYPE_NFN_FIELDS (type); ++i)
831 {
832 int j;
833 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, i);
834
835 for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (type, i); ++j)
836 {
837 if (TYPE_FN_FIELD_VIRTUAL_P (fn, j))
838 {
839 int voffset = TYPE_FN_FIELD_VOFFSET (fn, j);
840
841 if (voffset > current_vo->max_voffset)
842 current_vo->max_voffset = voffset;
843 }
844 }
845 }
846
847 /* Recurse into base classes. */
848 for (i = 0; i < TYPE_N_BASECLASSES (type); ++i)
849 compute_vtable_size (offset_hash, offset_vec, value_field (value, i));
850}
851
852/* Helper for gnuv3_print_vtable that prints a single vtable. */
853
854static void
855print_one_vtable (struct gdbarch *gdbarch, struct value *value,
856 int max_voffset,
857 struct value_print_options *opts)
858{
859 int i;
860 struct type *type = check_typedef (value_type (value));
861 struct value *vtable;
862 CORE_ADDR vt_addr;
863
864 vtable = gnuv3_get_vtable (gdbarch, type,
865 value_address (value)
866 + value_embedded_offset (value));
867 vt_addr = value_address (value_field (vtable,
868 vtable_field_virtual_functions));
869
870 printf_filtered (_("vtable for '%s' @ %s (subobject @ %s):\n"),
871 TYPE_SAFE_NAME (type),
872 paddress (gdbarch, vt_addr),
873 paddress (gdbarch, (value_address (value)
874 + value_embedded_offset (value))));
875
876 for (i = 0; i <= max_voffset; ++i)
877 {
cafe75b0
JK
878 /* Initialize it just to avoid a GCC false warning. */
879 CORE_ADDR addr = 0;
c4aeac85 880 struct value *vfn;
c4aeac85
TT
881 volatile struct gdb_exception ex;
882
883 printf_filtered ("[%d]: ", i);
884
885 vfn = value_subscript (value_field (vtable,
886 vtable_field_virtual_functions),
887 i);
888
889 if (gdbarch_vtable_function_descriptors (gdbarch))
890 vfn = value_addr (vfn);
891
892 TRY_CATCH (ex, RETURN_MASK_ERROR)
893 {
894 addr = value_as_address (vfn);
895 }
896 if (ex.reason < 0)
897 printf_filtered (_("<error: %s>"), ex.message);
898 else
edf0c1b7 899 print_function_pointer_address (opts, gdbarch, addr, gdb_stdout);
c4aeac85
TT
900 printf_filtered ("\n");
901 }
902}
903
904/* Implementation of the print_vtable method. */
905
906static void
907gnuv3_print_vtable (struct value *value)
908{
909 struct gdbarch *gdbarch;
910 struct type *type;
911 struct value *vtable;
912 struct value_print_options opts;
913 htab_t offset_hash;
914 struct cleanup *cleanup;
5ff5c7b4 915 VEC (value_and_voffset_p) *result_vec = NULL;
c4aeac85
TT
916 struct value_and_voffset *iter;
917 int i, count;
918
919 value = coerce_ref (value);
920 type = check_typedef (value_type (value));
921 if (TYPE_CODE (type) == TYPE_CODE_PTR)
922 {
923 value = value_ind (value);
924 type = check_typedef (value_type (value));
925 }
926
927 get_user_print_options (&opts);
928
929 /* Respect 'set print object'. */
930 if (opts.objectprint)
931 {
932 value = value_full_object (value, NULL, 0, 0, 0);
933 type = check_typedef (value_type (value));
934 }
935
936 gdbarch = get_type_arch (type);
937 vtable = gnuv3_get_vtable (gdbarch, type,
938 value_as_address (value_addr (value)));
939
940 if (!vtable)
941 {
942 printf_filtered (_("This object does not have a virtual function table\n"));
943 return;
944 }
945
946 offset_hash = htab_create_alloc (1, hash_value_and_voffset,
947 eq_value_and_voffset,
948 xfree, xcalloc, xfree);
949 cleanup = make_cleanup_htab_delete (offset_hash);
950 make_cleanup (VEC_cleanup (value_and_voffset_p), &result_vec);
951
952 compute_vtable_size (offset_hash, &result_vec, value);
953
954 qsort (VEC_address (value_and_voffset_p, result_vec),
955 VEC_length (value_and_voffset_p, result_vec),
956 sizeof (value_and_voffset_p),
957 compare_value_and_voffset);
958
959 count = 0;
960 for (i = 0; VEC_iterate (value_and_voffset_p, result_vec, i, iter); ++i)
961 {
962 if (iter->max_voffset >= 0)
963 {
964 if (count > 0)
965 printf_filtered ("\n");
966 print_one_vtable (gdbarch, iter->value, iter->max_voffset, &opts);
967 ++count;
968 }
969 }
970
971 do_cleanups (cleanup);
972}
973
b18be20d
DJ
974/* Determine if we are currently in a C++ thunk. If so, get the address
975 of the routine we are thunking to and continue to there instead. */
976
977static CORE_ADDR
52f729a7 978gnuv3_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc)
b18be20d
DJ
979{
980 CORE_ADDR real_stop_pc, method_stop_pc;
9970f04b 981 struct gdbarch *gdbarch = get_frame_arch (frame);
b18be20d
DJ
982 struct minimal_symbol *thunk_sym, *fn_sym;
983 struct obj_section *section;
0d5cff50 984 const char *thunk_name, *fn_name;
b18be20d 985
9970f04b 986 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
b18be20d
DJ
987 if (real_stop_pc == 0)
988 real_stop_pc = stop_pc;
989
990 /* Find the linker symbol for this potential thunk. */
991 thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
992 section = find_pc_section (real_stop_pc);
993 if (thunk_sym == NULL || section == NULL)
994 return 0;
995
996 /* The symbol's demangled name should be something like "virtual
997 thunk to FUNCTION", where FUNCTION is the name of the function
998 being thunked to. */
999 thunk_name = SYMBOL_DEMANGLED_NAME (thunk_sym);
1000 if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
1001 return 0;
1002
1003 fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
1004 fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile);
1005 if (fn_sym == NULL)
1006 return 0;
1007
1008 method_stop_pc = SYMBOL_VALUE_ADDRESS (fn_sym);
e76f05fa 1009 real_stop_pc = gdbarch_skip_trampoline_code
9970f04b 1010 (gdbarch, frame, method_stop_pc);
b18be20d
DJ
1011 if (real_stop_pc == 0)
1012 real_stop_pc = method_stop_pc;
1013
1014 return real_stop_pc;
1015}
1016
41f1b697
DJ
1017/* Return nonzero if a type should be passed by reference.
1018
1019 The rule in the v3 ABI document comes from section 3.1.1. If the
1020 type has a non-trivial copy constructor or destructor, then the
1021 caller must make a copy (by calling the copy constructor if there
1022 is one or perform the copy itself otherwise), pass the address of
1023 the copy, and then destroy the temporary (if necessary).
1024
1025 For return values with non-trivial copy constructors or
1026 destructors, space will be allocated in the caller, and a pointer
1027 will be passed as the first argument (preceding "this").
1028
1029 We don't have a bulletproof mechanism for determining whether a
1030 constructor or destructor is trivial. For GCC and DWARF2 debug
1031 information, we can check the artificial flag.
1032
1033 We don't do anything with the constructors or destructors,
1034 but we have to get the argument passing right anyway. */
1035static int
1036gnuv3_pass_by_reference (struct type *type)
1037{
1038 int fieldnum, fieldelem;
1039
1040 CHECK_TYPEDEF (type);
1041
1042 /* We're only interested in things that can have methods. */
1043 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1044 && TYPE_CODE (type) != TYPE_CODE_CLASS
1045 && TYPE_CODE (type) != TYPE_CODE_UNION)
1046 return 0;
1047
1048 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
1049 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
1050 fieldelem++)
1051 {
1052 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
0d5cff50 1053 const char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
41f1b697
DJ
1054 struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
1055
1056 /* If this function is marked as artificial, it is compiler-generated,
1057 and we assume it is trivial. */
1058 if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
1059 continue;
1060
1061 /* If we've found a destructor, we must pass this by reference. */
1062 if (name[0] == '~')
1063 return 1;
1064
1065 /* If the mangled name of this method doesn't indicate that it
1066 is a constructor, we're not interested.
1067
1068 FIXME drow/2007-09-23: We could do this using the name of
1069 the method and the name of the class instead of dealing
1070 with the mangled name. We don't have a convenient function
1071 to strip off both leading scope qualifiers and trailing
1072 template arguments yet. */
1073 if (!is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem)))
1074 continue;
1075
1076 /* If this method takes two arguments, and the second argument is
1077 a reference to this class, then it is a copy constructor. */
1078 if (TYPE_NFIELDS (fieldtype) == 2
1079 && TYPE_CODE (TYPE_FIELD_TYPE (fieldtype, 1)) == TYPE_CODE_REF
0963b4bd
MS
1080 && check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (fieldtype,
1081 1))) == type)
41f1b697
DJ
1082 return 1;
1083 }
1084
1085 /* Even if all the constructors and destructors were artificial, one
1086 of them may have invoked a non-artificial constructor or
1087 destructor in a base class. If any base class needs to be passed
1088 by reference, so does this class. Similarly for members, which
1089 are constructed whenever this class is. We do not need to worry
1090 about recursive loops here, since we are only looking at members
bceffbf3 1091 of complete class type. Also ignore any static members. */
41f1b697 1092 for (fieldnum = 0; fieldnum < TYPE_NFIELDS (type); fieldnum++)
bceffbf3
JK
1093 if (! field_is_static (&TYPE_FIELD (type, fieldnum))
1094 && gnuv3_pass_by_reference (TYPE_FIELD_TYPE (type, fieldnum)))
41f1b697
DJ
1095 return 1;
1096
1097 return 0;
1098}
1099
7ed49443
JB
1100static void
1101init_gnuv3_ops (void)
1102{
0963b4bd
MS
1103 vtable_type_gdbarch_data
1104 = gdbarch_data_register_post_init (build_gdb_vtable_type);
7ed49443
JB
1105
1106 gnu_v3_abi_ops.shortname = "gnu-v3";
1107 gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
1108 gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
358777b0
EZ
1109 gnu_v3_abi_ops.is_destructor_name =
1110 (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
1111 gnu_v3_abi_ops.is_constructor_name =
1112 (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
7ed49443
JB
1113 gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
1114 gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
1115 gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
1116 gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
1514d34e 1117 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
0d5de010
DJ
1118 gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
1119 gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
1120 gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
1121 gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
c4aeac85 1122 gnu_v3_abi_ops.print_vtable = gnuv3_print_vtable;
b18be20d 1123 gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
41f1b697 1124 gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
7ed49443
JB
1125}
1126
b9362cc7 1127extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */
7ed49443
JB
1128
1129void
1130_initialize_gnu_v3_abi (void)
1131{
1132 init_gnuv3_ops ();
1133
fe1f4a5e 1134 register_cp_abi (&gnu_v3_abi_ops);
7ed49443 1135}
This page took 0.938531 seconds and 4 git commands to generate.