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