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