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