gdb/
[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
4c38e0a4 4 Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010
0d5de010 5 Free Software Foundation, Inc.
7ed49443
JB
6
7 This file is part of GDB.
8
a9762ec7
JB
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
7ed49443
JB
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
7ed49443
JB
21
22#include "defs.h"
23#include "value.h"
24#include "cp-abi.h"
362ff856 25#include "cp-support.h"
7ed49443 26#include "demangle.h"
b18be20d 27#include "objfiles.h"
0d5de010 28#include "valprint.h"
94af9270 29#include "c-lang.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);
7ed49443
JB
133 FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
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;
140 FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
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;
147 FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
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);
7ed49443
JB
154 FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
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,
268 vtable_address - vtable_address_point_offset (gdbarch));
269}
270
271
7ed49443
JB
272static struct type *
273gnuv3_rtti_type (struct value *value,
274 int *full_p, int *top_p, int *using_enc_p)
275{
ad4820ab 276 struct gdbarch *gdbarch;
df407dfe 277 struct type *values_type = check_typedef (value_type (value));
7ed49443
JB
278 struct value *vtable;
279 struct minimal_symbol *vtable_symbol;
280 const char *vtable_symbol_name;
281 const char *class_name;
7ed49443
JB
282 struct type *run_time_type;
283 LONGEST offset_to_top;
284
285 /* We only have RTTI for class objects. */
df407dfe 286 if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
7ed49443
JB
287 return NULL;
288
ad4820ab 289 /* Determine architecture. */
50810684 290 gdbarch = get_type_arch (values_type);
7ed49443 291
21cfb3b6
DJ
292 if (using_enc_p)
293 *using_enc_p = 0;
294
d48cc9dd
DJ
295 vtable = gnuv3_get_vtable (gdbarch, value_type (value),
296 value_as_address (value_addr (value)));
297 if (vtable == NULL)
298 return NULL;
299
7ed49443
JB
300 /* Find the linker symbol for this vtable. */
301 vtable_symbol
42ae5230 302 = lookup_minimal_symbol_by_pc (value_address (vtable)
13c3b5f5 303 + value_embedded_offset (vtable));
7ed49443
JB
304 if (! vtable_symbol)
305 return NULL;
306
307 /* The symbol's demangled name should be something like "vtable for
308 CLASS", where CLASS is the name of the run-time type of VALUE.
309 If we didn't like this approach, we could instead look in the
310 type_info object itself to get the class name. But this way
311 should work just as well, and doesn't read target memory. */
312 vtable_symbol_name = SYMBOL_DEMANGLED_NAME (vtable_symbol);
98081e55
PB
313 if (vtable_symbol_name == NULL
314 || strncmp (vtable_symbol_name, "vtable for ", 11))
f773fdbb 315 {
8a3fe4f8 316 warning (_("can't find linker symbol for virtual table for `%s' value"),
df407dfe 317 TYPE_NAME (values_type));
f773fdbb 318 if (vtable_symbol_name)
8a3fe4f8 319 warning (_(" found `%s' instead"), vtable_symbol_name);
f773fdbb
JM
320 return NULL;
321 }
7ed49443
JB
322 class_name = vtable_symbol_name + 11;
323
324 /* Try to look up the class name as a type name. */
362ff856
MC
325 /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
326 run_time_type = cp_lookup_rtti_type (class_name, NULL);
327 if (run_time_type == NULL)
328 return NULL;
7ed49443
JB
329
330 /* Get the offset from VALUE to the top of the complete object.
331 NOTE: this is the reverse of the meaning of *TOP_P. */
332 offset_to_top
333 = value_as_long (value_field (vtable, vtable_field_offset_to_top));
334
335 if (full_p)
13c3b5f5 336 *full_p = (- offset_to_top == value_embedded_offset (value)
4754a64e 337 && (TYPE_LENGTH (value_enclosing_type (value))
7ed49443
JB
338 >= TYPE_LENGTH (run_time_type)));
339 if (top_p)
340 *top_p = - offset_to_top;
7ed49443
JB
341 return run_time_type;
342}
343
0d5de010
DJ
344/* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
345 function, of type FNTYPE. */
7ed49443 346
0d5de010 347static struct value *
ad4820ab
UW
348gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
349 struct type *fntype, int vtable_index)
0d5de010 350{
d48cc9dd
DJ
351 struct value *vtable, *vfn;
352
353 /* Every class with virtual functions must have a vtable. */
354 vtable = gnuv3_get_vtable (gdbarch, value_type (container),
355 value_as_address (value_addr (container)));
356 gdb_assert (vtable != NULL);
7ed49443
JB
357
358 /* Fetch the appropriate function pointer from the vtable. */
359 vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
2497b498 360 vtable_index);
7ed49443 361
0d5de010
DJ
362 /* If this architecture uses function descriptors directly in the vtable,
363 then the address of the vtable entry is actually a "function pointer"
364 (i.e. points to the descriptor). We don't need to scale the index
365 by the size of a function descriptor; GCC does that before outputing
366 debug information. */
ad4820ab 367 if (gdbarch_vtable_function_descriptors (gdbarch))
0d5de010 368 vfn = value_addr (vfn);
7ed49443 369
0d5de010
DJ
370 /* Cast the function pointer to the appropriate type. */
371 vfn = value_cast (lookup_pointer_type (fntype), vfn);
76b79d6e 372
7ed49443
JB
373 return vfn;
374}
375
0d5de010
DJ
376/* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h
377 for a description of the arguments. */
378
379static struct value *
380gnuv3_virtual_fn_field (struct value **value_p,
381 struct fn_field *f, int j,
382 struct type *vfn_base, int offset)
383{
384 struct type *values_type = check_typedef (value_type (*value_p));
ad4820ab 385 struct gdbarch *gdbarch;
0d5de010
DJ
386
387 /* Some simple sanity checks. */
388 if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
389 error (_("Only classes can have virtual functions."));
390
ad4820ab 391 /* Determine architecture. */
50810684 392 gdbarch = get_type_arch (values_type);
ad4820ab 393
0d5de010
DJ
394 /* Cast our value to the base class which defines this virtual
395 function. This takes care of any necessary `this'
396 adjustments. */
397 if (vfn_base != values_type)
398 *value_p = value_cast (vfn_base, *value_p);
399
ad4820ab 400 return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
0d5de010
DJ
401 TYPE_FN_FIELD_VOFFSET (f, j));
402}
403
1514d34e
DJ
404/* Compute the offset of the baseclass which is
405 the INDEXth baseclass of class TYPE,
406 for value at VALADDR (in host) at ADDRESS (in target).
407 The result is the offset of the baseclass value relative
408 to (the address of)(ARG) + OFFSET.
409
410 -1 is returned on error. */
b9362cc7 411static int
96ce45ca 412gnuv3_baseclass_offset (struct type *type, int index, const bfd_byte *valaddr,
1514d34e
DJ
413 CORE_ADDR address)
414{
ad4820ab 415 struct gdbarch *gdbarch;
ad4820ab 416 struct type *ptr_type;
79d5b63a 417 struct value *vtable;
2497b498 418 struct value *vbase_array;
1514d34e 419 long int cur_base_offset, base_offset;
1514d34e 420
ad4820ab 421 /* Determine architecture. */
50810684 422 gdbarch = get_type_arch (type);
ad4820ab
UW
423 ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
424
1514d34e
DJ
425 /* If it isn't a virtual base, this is easy. The offset is in the
426 type definition. */
427 if (!BASETYPE_VIA_VIRTUAL (type, index))
428 return TYPE_BASECLASS_BITPOS (type, index) / 8;
429
430 /* To access a virtual base, we need to use the vbase offset stored in
431 our vtable. Recent GCC versions provide this information. If it isn't
432 available, we could get what we needed from RTTI, or from drawing the
433 complete inheritance graph based on the debug info. Neither is
434 worthwhile. */
435 cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
ad4820ab 436 if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
8a3fe4f8 437 error (_("Expected a negative vbase offset (old compiler?)"));
1514d34e 438
ad4820ab
UW
439 cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
440 if ((- cur_base_offset) % TYPE_LENGTH (ptr_type) != 0)
8a3fe4f8 441 error (_("Misaligned vbase offset."));
ad4820ab 442 cur_base_offset = cur_base_offset / ((int) TYPE_LENGTH (ptr_type));
1514d34e 443
d48cc9dd
DJ
444 vtable = gnuv3_get_vtable (gdbarch, type, address);
445 gdb_assert (vtable != NULL);
1514d34e 446 vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
2497b498 447 base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
1514d34e
DJ
448 return base_offset;
449}
7ed49443 450
0d5de010
DJ
451/* Locate a virtual method in DOMAIN or its non-virtual base classes
452 which has virtual table index VOFFSET. The method has an associated
453 "this" adjustment of ADJUSTMENT bytes. */
454
2c0b251b 455static const char *
0d5de010
DJ
456gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
457 LONGEST adjustment)
458{
459 int i;
0d5de010
DJ
460
461 /* Search this class first. */
0d5de010
DJ
462 if (adjustment == 0)
463 {
464 int len;
465
466 len = TYPE_NFN_FIELDS (domain);
467 for (i = 0; i < len; i++)
468 {
469 int len2, j;
470 struct fn_field *f;
471
472 f = TYPE_FN_FIELDLIST1 (domain, i);
473 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
474
475 check_stub_method_group (domain, i);
476 for (j = 0; j < len2; j++)
477 if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset)
478 return TYPE_FN_FIELD_PHYSNAME (f, j);
479 }
480 }
481
482 /* Next search non-virtual bases. If it's in a virtual base,
483 we're out of luck. */
484 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
485 {
486 int pos;
487 struct type *basetype;
488
489 if (BASETYPE_VIA_VIRTUAL (domain, i))
490 continue;
491
492 pos = TYPE_BASECLASS_BITPOS (domain, i) / 8;
493 basetype = TYPE_FIELD_TYPE (domain, i);
494 /* Recurse with a modified adjustment. We don't need to adjust
495 voffset. */
496 if (adjustment >= pos && adjustment < pos + TYPE_LENGTH (basetype))
497 return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
498 }
499
500 return NULL;
501}
502
fead6908
UW
503/* Decode GNU v3 method pointer. */
504
505static int
ad4820ab
UW
506gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
507 const gdb_byte *contents,
fead6908
UW
508 CORE_ADDR *value_p,
509 LONGEST *adjustment_p)
510{
ad4820ab 511 struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
ed09d7da 512 struct type *offset_type = vtable_ptrdiff_type (gdbarch);
e17a4113 513 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
fead6908
UW
514 CORE_ADDR ptr_value;
515 LONGEST voffset, adjustment;
516 int vbit;
517
518 /* Extract the pointer to member. The first element is either a pointer
519 or a vtable offset. For pointers, we need to use extract_typed_address
520 to allow the back-end to convert the pointer to a GDB address -- but
521 vtable offsets we must handle as integers. At this point, we do not
522 yet know which case we have, so we extract the value under both
523 interpretations and choose the right one later on. */
524 ptr_value = extract_typed_address (contents, funcptr_type);
e17a4113
UW
525 voffset = extract_signed_integer (contents,
526 TYPE_LENGTH (funcptr_type), byte_order);
fead6908 527 contents += TYPE_LENGTH (funcptr_type);
e17a4113
UW
528 adjustment = extract_signed_integer (contents,
529 TYPE_LENGTH (offset_type), byte_order);
fead6908 530
ad4820ab 531 if (!gdbarch_vbit_in_delta (gdbarch))
fead6908
UW
532 {
533 vbit = voffset & 1;
534 voffset = voffset ^ vbit;
535 }
536 else
537 {
538 vbit = adjustment & 1;
539 adjustment = adjustment >> 1;
540 }
541
542 *value_p = vbit? voffset : ptr_value;
543 *adjustment_p = adjustment;
544 return vbit;
545}
546
0d5de010
DJ
547/* GNU v3 implementation of cplus_print_method_ptr. */
548
549static void
550gnuv3_print_method_ptr (const gdb_byte *contents,
551 struct type *type,
552 struct ui_file *stream)
553{
ad4820ab 554 struct type *domain = TYPE_DOMAIN_TYPE (type);
50810684 555 struct gdbarch *gdbarch = get_type_arch (domain);
0d5de010
DJ
556 CORE_ADDR ptr_value;
557 LONGEST adjustment;
0d5de010
DJ
558 int vbit;
559
0d5de010 560 /* Extract the pointer to member. */
ad4820ab 561 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
0d5de010
DJ
562
563 /* Check for NULL. */
564 if (ptr_value == 0 && vbit == 0)
565 {
566 fprintf_filtered (stream, "NULL");
567 return;
568 }
569
570 /* Search for a virtual method. */
571 if (vbit)
572 {
573 CORE_ADDR voffset;
574 const char *physname;
575
576 /* It's a virtual table offset, maybe in this class. Search
577 for a field with the correct vtable offset. First convert it
578 to an index, as used in TYPE_FN_FIELD_VOFFSET. */
ed09d7da 579 voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
0d5de010
DJ
580
581 physname = gnuv3_find_method_in (domain, voffset, adjustment);
582
583 /* If we found a method, print that. We don't bother to disambiguate
584 possible paths to the method based on the adjustment. */
585 if (physname)
586 {
587 char *demangled_name = cplus_demangle (physname,
588 DMGL_ANSI | DMGL_PARAMS);
d8734c88 589
94af9270
KS
590 fprintf_filtered (stream, "&virtual ");
591 if (demangled_name == NULL)
592 fputs_filtered (physname, stream);
593 else
0d5de010 594 {
0d5de010
DJ
595 fputs_filtered (demangled_name, stream);
596 xfree (demangled_name);
0d5de010 597 }
94af9270 598 return;
0d5de010
DJ
599 }
600 }
94af9270
KS
601 else if (ptr_value != 0)
602 {
603 /* Found a non-virtual function: print out the type. */
604 fputs_filtered ("(", stream);
605 c_print_type (type, "", stream, -1, 0);
606 fputs_filtered (") ", stream);
607 }
0d5de010
DJ
608
609 /* We didn't find it; print the raw data. */
610 if (vbit)
611 {
612 fprintf_filtered (stream, "&virtual table offset ");
613 print_longest (stream, 'd', 1, ptr_value);
614 }
615 else
5af949e3 616 print_address_demangle (gdbarch, ptr_value, stream, demangle);
0d5de010
DJ
617
618 if (adjustment)
619 {
620 fprintf_filtered (stream, ", this adjustment ");
621 print_longest (stream, 'd', 1, adjustment);
622 }
623}
624
625/* GNU v3 implementation of cplus_method_ptr_size. */
626
627static int
ad4820ab 628gnuv3_method_ptr_size (struct type *type)
0d5de010 629{
ad4820ab 630 struct type *domain_type = check_typedef (TYPE_DOMAIN_TYPE (type));
50810684 631 struct gdbarch *gdbarch = get_type_arch (domain_type);
d8734c88 632
ad4820ab 633 return 2 * TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
0d5de010
DJ
634}
635
636/* GNU v3 implementation of cplus_make_method_ptr. */
637
638static void
ad4820ab
UW
639gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
640 CORE_ADDR value, int is_virtual)
0d5de010 641{
ad4820ab 642 struct type *domain_type = check_typedef (TYPE_DOMAIN_TYPE (type));
50810684 643 struct gdbarch *gdbarch = get_type_arch (domain_type);
ad4820ab 644 int size = TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
e17a4113 645 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
0d5de010
DJ
646
647 /* FIXME drow/2006-12-24: The adjustment of "this" is currently
648 always zero, since the method pointer is of the correct type.
649 But if the method pointer came from a base class, this is
650 incorrect - it should be the offset to the base. The best
651 fix might be to create the pointer to member pointing at the
652 base class and cast it to the derived class, but that requires
653 support for adjusting pointers to members when casting them -
654 not currently supported by GDB. */
655
ad4820ab 656 if (!gdbarch_vbit_in_delta (gdbarch))
0d5de010 657 {
e17a4113
UW
658 store_unsigned_integer (contents, size, byte_order, value | is_virtual);
659 store_unsigned_integer (contents + size, size, byte_order, 0);
0d5de010
DJ
660 }
661 else
662 {
e17a4113
UW
663 store_unsigned_integer (contents, size, byte_order, value);
664 store_unsigned_integer (contents + size, size, byte_order, is_virtual);
0d5de010
DJ
665 }
666}
667
668/* GNU v3 implementation of cplus_method_ptr_to_value. */
669
670static struct value *
671gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
672{
ad4820ab 673 struct gdbarch *gdbarch;
0d5de010
DJ
674 const gdb_byte *contents = value_contents (method_ptr);
675 CORE_ADDR ptr_value;
ad4820ab 676 struct type *domain_type, *final_type, *method_type;
0d5de010 677 LONGEST adjustment;
0d5de010
DJ
678 int vbit;
679
ad4820ab
UW
680 domain_type = TYPE_DOMAIN_TYPE (check_typedef (value_type (method_ptr)));
681 final_type = lookup_pointer_type (domain_type);
0d5de010
DJ
682
683 method_type = TYPE_TARGET_TYPE (check_typedef (value_type (method_ptr)));
684
fead6908 685 /* Extract the pointer to member. */
50810684 686 gdbarch = get_type_arch (domain_type);
ad4820ab 687 vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
0d5de010
DJ
688
689 /* First convert THIS to match the containing type of the pointer to
690 member. This cast may adjust the value of THIS. */
691 *this_p = value_cast (final_type, *this_p);
692
693 /* Then apply whatever adjustment is necessary. This creates a somewhat
694 strange pointer: it claims to have type FINAL_TYPE, but in fact it
695 might not be a valid FINAL_TYPE. For instance, it might be a
696 base class of FINAL_TYPE. And if it's not the primary base class,
697 then printing it out as a FINAL_TYPE object would produce some pretty
698 garbage.
699
700 But we don't really know the type of the first argument in
701 METHOD_TYPE either, which is why this happens. We can't
702 dereference this later as a FINAL_TYPE, but once we arrive in the
703 called method we'll have debugging information for the type of
704 "this" - and that'll match the value we produce here.
705
706 You can provoke this case by casting a Base::* to a Derived::*, for
707 instance. */
ad4820ab 708 *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
2497b498 709 *this_p = value_ptradd (*this_p, adjustment);
0d5de010
DJ
710 *this_p = value_cast (final_type, *this_p);
711
712 if (vbit)
713 {
ad4820ab 714 LONGEST voffset;
d8734c88 715
ed09d7da 716 voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
ad4820ab
UW
717 return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
718 method_type, voffset);
0d5de010
DJ
719 }
720 else
721 return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
722}
723
b18be20d
DJ
724/* Determine if we are currently in a C++ thunk. If so, get the address
725 of the routine we are thunking to and continue to there instead. */
726
727static CORE_ADDR
52f729a7 728gnuv3_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc)
b18be20d
DJ
729{
730 CORE_ADDR real_stop_pc, method_stop_pc;
9970f04b 731 struct gdbarch *gdbarch = get_frame_arch (frame);
b18be20d
DJ
732 struct minimal_symbol *thunk_sym, *fn_sym;
733 struct obj_section *section;
734 char *thunk_name, *fn_name;
735
9970f04b 736 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
b18be20d
DJ
737 if (real_stop_pc == 0)
738 real_stop_pc = stop_pc;
739
740 /* Find the linker symbol for this potential thunk. */
741 thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
742 section = find_pc_section (real_stop_pc);
743 if (thunk_sym == NULL || section == NULL)
744 return 0;
745
746 /* The symbol's demangled name should be something like "virtual
747 thunk to FUNCTION", where FUNCTION is the name of the function
748 being thunked to. */
749 thunk_name = SYMBOL_DEMANGLED_NAME (thunk_sym);
750 if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
751 return 0;
752
753 fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
754 fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile);
755 if (fn_sym == NULL)
756 return 0;
757
758 method_stop_pc = SYMBOL_VALUE_ADDRESS (fn_sym);
e76f05fa 759 real_stop_pc = gdbarch_skip_trampoline_code
9970f04b 760 (gdbarch, frame, method_stop_pc);
b18be20d
DJ
761 if (real_stop_pc == 0)
762 real_stop_pc = method_stop_pc;
763
764 return real_stop_pc;
765}
766
41f1b697
DJ
767/* Return nonzero if a type should be passed by reference.
768
769 The rule in the v3 ABI document comes from section 3.1.1. If the
770 type has a non-trivial copy constructor or destructor, then the
771 caller must make a copy (by calling the copy constructor if there
772 is one or perform the copy itself otherwise), pass the address of
773 the copy, and then destroy the temporary (if necessary).
774
775 For return values with non-trivial copy constructors or
776 destructors, space will be allocated in the caller, and a pointer
777 will be passed as the first argument (preceding "this").
778
779 We don't have a bulletproof mechanism for determining whether a
780 constructor or destructor is trivial. For GCC and DWARF2 debug
781 information, we can check the artificial flag.
782
783 We don't do anything with the constructors or destructors,
784 but we have to get the argument passing right anyway. */
785static int
786gnuv3_pass_by_reference (struct type *type)
787{
788 int fieldnum, fieldelem;
789
790 CHECK_TYPEDEF (type);
791
792 /* We're only interested in things that can have methods. */
793 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
794 && TYPE_CODE (type) != TYPE_CODE_CLASS
795 && TYPE_CODE (type) != TYPE_CODE_UNION)
796 return 0;
797
798 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
799 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
800 fieldelem++)
801 {
802 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
803 char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
804 struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
805
806 /* If this function is marked as artificial, it is compiler-generated,
807 and we assume it is trivial. */
808 if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
809 continue;
810
811 /* If we've found a destructor, we must pass this by reference. */
812 if (name[0] == '~')
813 return 1;
814
815 /* If the mangled name of this method doesn't indicate that it
816 is a constructor, we're not interested.
817
818 FIXME drow/2007-09-23: We could do this using the name of
819 the method and the name of the class instead of dealing
820 with the mangled name. We don't have a convenient function
821 to strip off both leading scope qualifiers and trailing
822 template arguments yet. */
823 if (!is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem)))
824 continue;
825
826 /* If this method takes two arguments, and the second argument is
827 a reference to this class, then it is a copy constructor. */
828 if (TYPE_NFIELDS (fieldtype) == 2
829 && TYPE_CODE (TYPE_FIELD_TYPE (fieldtype, 1)) == TYPE_CODE_REF
830 && check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (fieldtype, 1))) == type)
831 return 1;
832 }
833
834 /* Even if all the constructors and destructors were artificial, one
835 of them may have invoked a non-artificial constructor or
836 destructor in a base class. If any base class needs to be passed
837 by reference, so does this class. Similarly for members, which
838 are constructed whenever this class is. We do not need to worry
839 about recursive loops here, since we are only looking at members
840 of complete class type. */
841 for (fieldnum = 0; fieldnum < TYPE_NFIELDS (type); fieldnum++)
842 if (gnuv3_pass_by_reference (TYPE_FIELD_TYPE (type, fieldnum)))
843 return 1;
844
845 return 0;
846}
847
7ed49443
JB
848static void
849init_gnuv3_ops (void)
850{
030f20e1 851 vtable_type_gdbarch_data = gdbarch_data_register_post_init (build_gdb_vtable_type);
7ed49443
JB
852
853 gnu_v3_abi_ops.shortname = "gnu-v3";
854 gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
855 gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
358777b0
EZ
856 gnu_v3_abi_ops.is_destructor_name =
857 (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
858 gnu_v3_abi_ops.is_constructor_name =
859 (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
7ed49443
JB
860 gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
861 gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
862 gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
863 gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
1514d34e 864 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
0d5de010
DJ
865 gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
866 gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
867 gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
868 gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
b18be20d 869 gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
41f1b697 870 gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
7ed49443
JB
871}
872
b9362cc7 873extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */
7ed49443
JB
874
875void
876_initialize_gnu_v3_abi (void)
877{
878 init_gnuv3_ops ();
879
fe1f4a5e 880 register_cp_abi (&gnu_v3_abi_ops);
7ed49443 881}
This page took 0.78685 seconds and 4 git commands to generate.