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