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