Update
[gdb.git] / gdb / gnu-v3-abi.c
blob2059f3043add31d13d75f1c3a8b3e283c251b099
1 /* Abstraction of GNU v3 abi.
2 Contributed by Jim Blandy <jimb@redhat.com>
4 Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007, 2008
5 Free Software Foundation, Inc.
7 This file is part of GDB.
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.
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.
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/>. */
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"
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
33 static struct cp_abi_ops gnu_v3_abi_ops;
35 static int
36 gnuv3_is_vtable_name (const char *name)
38 return strncmp (name, "_ZTV", 4) == 0;
41 static int
42 gnuv3_is_operator_name (const char *name)
44 return strncmp (name, "operator", 8) == 0;
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:
52 struct gdb_gnu_v3_abi_vtable {
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];
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;
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;
70 / * Virtual table pointers in objects point here. * /
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]) ();
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.
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. */
85 static struct gdbarch_data *vtable_type_gdbarch_data;
88 /* Human-readable names for the numbers of the fields above. */
89 enum {
90 vtable_field_vcall_and_vbase_offsets,
91 vtable_field_offset_to_top,
92 vtable_field_type_info,
93 vtable_field_virtual_functions
97 /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
98 described above, laid out appropriately for ARCH.
100 We use this function as the gdbarch per-architecture data
101 initialization function. */
102 static void *
103 build_gdb_vtable_type (struct gdbarch *arch)
105 struct type *t;
106 struct field *field_list, *field;
107 int offset;
109 struct type *void_ptr_type
110 = lookup_pointer_type (builtin_type_void);
111 struct type *ptr_to_void_fn_type
112 = lookup_pointer_type (lookup_function_type (builtin_type_void));
114 /* ARCH can't give us the true ptrdiff_t type, so we guess. */
115 struct type *ptrdiff_type
116 = init_type (TYPE_CODE_INT,
117 gdbarch_ptr_bit (arch) / TARGET_CHAR_BIT, 0,
118 "ptrdiff_t", 0);
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. */
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;
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,
135 create_range_type (0, builtin_type_int, 0, -1));
136 FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
137 offset += TYPE_LENGTH (FIELD_TYPE (*field));
138 field++;
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++;
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++;
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,
158 create_range_type (0, builtin_type_int, 0, -1));
159 FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
160 offset += TYPE_LENGTH (FIELD_TYPE (*field));
161 field++;
163 /* We assumed in the allocation above that there were four fields. */
164 gdb_assert (field == (field_list + 4));
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";
171 return t;
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). */
178 static int
179 vtable_address_point_offset (void)
181 struct type *vtable_type = gdbarch_data (current_gdbarch,
182 vtable_type_gdbarch_data);
184 return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions)
185 / TARGET_CHAR_BIT);
189 static struct type *
190 gnuv3_rtti_type (struct value *value,
191 int *full_p, int *top_p, int *using_enc_p)
193 struct type *vtable_type = gdbarch_data (current_gdbarch,
194 vtable_type_gdbarch_data);
195 struct type *values_type = check_typedef (value_type (value));
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;
201 struct type *run_time_type;
202 struct type *base_type;
203 LONGEST offset_to_top;
204 struct type *values_type_vptr_basetype;
205 int values_type_vptr_fieldno;
207 /* We only have RTTI for class objects. */
208 if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
209 return NULL;
211 /* If we can't find the virtual table pointer for values_type, we
212 can't find the RTTI. */
213 values_type_vptr_fieldno = get_vptr_fieldno (values_type,
214 &values_type_vptr_basetype);
215 if (values_type_vptr_fieldno == -1)
216 return NULL;
218 if (using_enc_p)
219 *using_enc_p = 0;
221 /* Fetch VALUE's virtual table pointer, and tweak it to point at
222 an instance of our imaginary gdb_gnu_v3_abi_vtable structure. */
223 base_type = check_typedef (values_type_vptr_basetype);
224 if (values_type != base_type)
226 value = value_cast (base_type, value);
227 if (using_enc_p)
228 *using_enc_p = 1;
230 vtable_address
231 = value_as_address (value_field (value, values_type_vptr_fieldno));
232 vtable = value_at_lazy (vtable_type,
233 vtable_address - vtable_address_point_offset ());
235 /* Find the linker symbol for this vtable. */
236 vtable_symbol
237 = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtable)
238 + value_offset (vtable)
239 + value_embedded_offset (vtable));
240 if (! vtable_symbol)
241 return NULL;
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);
249 if (vtable_symbol_name == NULL
250 || strncmp (vtable_symbol_name, "vtable for ", 11))
252 warning (_("can't find linker symbol for virtual table for `%s' value"),
253 TYPE_NAME (values_type));
254 if (vtable_symbol_name)
255 warning (_(" found `%s' instead"), vtable_symbol_name);
256 return NULL;
258 class_name = vtable_symbol_name + 11;
260 /* Try to look up the class name as a type name. */
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;
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));
271 if (full_p)
272 *full_p = (- offset_to_top == value_embedded_offset (value)
273 && (TYPE_LENGTH (value_enclosing_type (value))
274 >= TYPE_LENGTH (run_time_type)));
275 if (top_p)
276 *top_p = - offset_to_top;
278 return run_time_type;
281 /* Find the vtable for CONTAINER and return a value of the correct
282 vtable type for this architecture. */
284 static struct value *
285 gnuv3_get_vtable (struct value *container)
287 struct type *vtable_type = gdbarch_data (current_gdbarch,
288 vtable_type_gdbarch_data);
289 struct type *vtable_pointer_type;
290 struct value *vtable_pointer;
291 CORE_ADDR vtable_pointer_address, vtable_address;
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.
300 We avoid using value_contents on principle, because the object might
301 be large. */
303 /* Find the type "pointer to virtual table". */
304 vtable_pointer_type = lookup_pointer_type (vtable_type);
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);
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 ());
317 /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
318 function, of type FNTYPE. */
320 static struct value *
321 gnuv3_get_virtual_fn (struct value *container, struct type *fntype,
322 int vtable_index)
324 struct value *vtable = gnuv3_get_vtable (container);
325 struct value *vfn;
327 /* Fetch the appropriate function pointer from the vtable. */
328 vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
329 value_from_longest (builtin_type_int, vtable_index));
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);
339 /* Cast the function pointer to the appropriate type. */
340 vfn = value_cast (lookup_pointer_type (fntype), vfn);
342 return vfn;
345 /* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h
346 for a description of the arguments. */
348 static struct value *
349 gnuv3_virtual_fn_field (struct value **value_p,
350 struct fn_field *f, int j,
351 struct type *vfn_base, int offset)
353 struct type *values_type = check_typedef (value_type (*value_p));
355 /* Some simple sanity checks. */
356 if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
357 error (_("Only classes can have virtual functions."));
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);
365 return gnuv3_get_virtual_fn (*value_p, TYPE_FN_FIELD_TYPE (f, j),
366 TYPE_FN_FIELD_VOFFSET (f, j));
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.
375 -1 is returned on error. */
376 static int
377 gnuv3_baseclass_offset (struct type *type, int index, const bfd_byte *valaddr,
378 CORE_ADDR address)
380 struct type *vtable_type = gdbarch_data (current_gdbarch,
381 vtable_type_gdbarch_data);
382 struct value *vtable;
383 struct type *vbasetype;
384 struct value *offset_val, *vbase_array;
385 CORE_ADDR vtable_address;
386 long int cur_base_offset, base_offset;
387 int vbasetype_vptr_fieldno;
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;
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 ())
401 error (_("Expected a negative vbase offset (old compiler?)"));
403 cur_base_offset = cur_base_offset + vtable_address_point_offset ();
404 if ((- cur_base_offset) % TYPE_LENGTH (builtin_type_void_data_ptr) != 0)
405 error (_("Misaligned vbase offset."));
406 cur_base_offset = cur_base_offset
407 / ((int) TYPE_LENGTH (builtin_type_void_data_ptr));
409 /* We're now looking for the cur_base_offset'th entry (negative index)
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
417 start of whichever baseclass it resides in, as a sanity measure - iff
418 we have debugging information for that baseclass. */
420 vbasetype = TYPE_VPTR_BASETYPE (type);
421 vbasetype_vptr_fieldno = get_vptr_fieldno (vbasetype, NULL);
423 if (vbasetype_vptr_fieldno >= 0
424 && TYPE_FIELD_BITPOS (vbasetype, vbasetype_vptr_fieldno) != 0)
425 error (_("Illegal vptr offset in class %s"),
426 TYPE_NAME (vbasetype) ? TYPE_NAME (vbasetype) : "<unknown>");
428 vtable_address = value_as_address (value_at_lazy (builtin_type_void_data_ptr,
429 address));
430 vtable = value_at_lazy (vtable_type,
431 vtable_address - vtable_address_point_offset ());
432 offset_val = value_from_longest(builtin_type_int, cur_base_offset);
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;
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. */
442 const char *
443 gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
444 LONGEST adjustment)
446 int i;
447 const char *physname;
449 /* Search this class first. */
450 physname = NULL;
451 if (adjustment == 0)
453 int len;
455 len = TYPE_NFN_FIELDS (domain);
456 for (i = 0; i < len; i++)
458 int len2, j;
459 struct fn_field *f;
461 f = TYPE_FN_FIELDLIST1 (domain, i);
462 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
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);
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++)
475 int pos;
476 struct type *basetype;
478 if (BASETYPE_VIA_VIRTUAL (domain, i))
479 continue;
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);
489 return NULL;
492 /* GNU v3 implementation of cplus_print_method_ptr. */
494 static void
495 gnuv3_print_method_ptr (const gdb_byte *contents,
496 struct type *type,
497 struct ui_file *stream)
499 CORE_ADDR ptr_value;
500 LONGEST adjustment;
501 struct type *domain;
502 int vbit;
504 domain = TYPE_DOMAIN_TYPE (type);
506 /* Extract the pointer to member. */
507 ptr_value = extract_typed_address (contents, builtin_type_void_func_ptr);
508 contents += TYPE_LENGTH (builtin_type_void_func_ptr);
509 adjustment = extract_signed_integer (contents,
510 TYPE_LENGTH (builtin_type_long));
512 if (!gdbarch_vbit_in_delta (current_gdbarch))
514 vbit = ptr_value & 1;
515 ptr_value = ptr_value ^ vbit;
517 else
519 vbit = adjustment & 1;
520 adjustment = adjustment >> 1;
523 /* Check for NULL. */
524 if (ptr_value == 0 && vbit == 0)
526 fprintf_filtered (stream, "NULL");
527 return;
530 /* Search for a virtual method. */
531 if (vbit)
533 CORE_ADDR voffset;
534 const char *physname;
536 /* It's a virtual table offset, maybe in this class. Search
537 for a field with the correct vtable offset. First convert it
538 to an index, as used in TYPE_FN_FIELD_VOFFSET. */
539 voffset = ptr_value / TYPE_LENGTH (builtin_type_long);
541 physname = gnuv3_find_method_in (domain, voffset, adjustment);
543 /* If we found a method, print that. We don't bother to disambiguate
544 possible paths to the method based on the adjustment. */
545 if (physname)
547 char *demangled_name = cplus_demangle (physname,
548 DMGL_ANSI | DMGL_PARAMS);
549 if (demangled_name != NULL)
551 fprintf_filtered (stream, "&virtual ");
552 fputs_filtered (demangled_name, stream);
553 xfree (demangled_name);
554 return;
559 /* We didn't find it; print the raw data. */
560 if (vbit)
562 fprintf_filtered (stream, "&virtual table offset ");
563 print_longest (stream, 'd', 1, ptr_value);
565 else
566 print_address_demangle (ptr_value, stream, demangle);
568 if (adjustment)
570 fprintf_filtered (stream, ", this adjustment ");
571 print_longest (stream, 'd', 1, adjustment);
575 /* GNU v3 implementation of cplus_method_ptr_size. */
577 static int
578 gnuv3_method_ptr_size (void)
580 return 2 * TYPE_LENGTH (builtin_type_void_data_ptr);
583 /* GNU v3 implementation of cplus_make_method_ptr. */
585 static void
586 gnuv3_make_method_ptr (gdb_byte *contents, CORE_ADDR value, int is_virtual)
588 int size = TYPE_LENGTH (builtin_type_void_data_ptr);
590 /* FIXME drow/2006-12-24: The adjustment of "this" is currently
591 always zero, since the method pointer is of the correct type.
592 But if the method pointer came from a base class, this is
593 incorrect - it should be the offset to the base. The best
594 fix might be to create the pointer to member pointing at the
595 base class and cast it to the derived class, but that requires
596 support for adjusting pointers to members when casting them -
597 not currently supported by GDB. */
599 if (!gdbarch_vbit_in_delta (current_gdbarch))
601 store_unsigned_integer (contents, size, value | is_virtual);
602 store_unsigned_integer (contents + size, size, 0);
604 else
606 store_unsigned_integer (contents, size, value);
607 store_unsigned_integer (contents + size, size, is_virtual);
611 /* GNU v3 implementation of cplus_method_ptr_to_value. */
613 static struct value *
614 gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
616 const gdb_byte *contents = value_contents (method_ptr);
617 CORE_ADDR ptr_value;
618 struct type *final_type, *method_type;
619 LONGEST adjustment;
620 struct value *adjval;
621 int vbit;
623 final_type = TYPE_DOMAIN_TYPE (check_typedef (value_type (method_ptr)));
624 final_type = lookup_pointer_type (final_type);
626 method_type = TYPE_TARGET_TYPE (check_typedef (value_type (method_ptr)));
628 ptr_value = extract_typed_address (contents, builtin_type_void_func_ptr);
629 contents += TYPE_LENGTH (builtin_type_void_func_ptr);
630 adjustment = extract_signed_integer (contents,
631 TYPE_LENGTH (builtin_type_long));
633 if (!gdbarch_vbit_in_delta (current_gdbarch))
635 vbit = ptr_value & 1;
636 ptr_value = ptr_value ^ vbit;
638 else
640 vbit = adjustment & 1;
641 adjustment = adjustment >> 1;
644 /* First convert THIS to match the containing type of the pointer to
645 member. This cast may adjust the value of THIS. */
646 *this_p = value_cast (final_type, *this_p);
648 /* Then apply whatever adjustment is necessary. This creates a somewhat
649 strange pointer: it claims to have type FINAL_TYPE, but in fact it
650 might not be a valid FINAL_TYPE. For instance, it might be a
651 base class of FINAL_TYPE. And if it's not the primary base class,
652 then printing it out as a FINAL_TYPE object would produce some pretty
653 garbage.
655 But we don't really know the type of the first argument in
656 METHOD_TYPE either, which is why this happens. We can't
657 dereference this later as a FINAL_TYPE, but once we arrive in the
658 called method we'll have debugging information for the type of
659 "this" - and that'll match the value we produce here.
661 You can provoke this case by casting a Base::* to a Derived::*, for
662 instance. */
663 *this_p = value_cast (builtin_type_void_data_ptr, *this_p);
664 adjval = value_from_longest (builtin_type_long, adjustment);
665 *this_p = value_add (*this_p, adjval);
666 *this_p = value_cast (final_type, *this_p);
668 if (vbit)
670 LONGEST voffset = ptr_value / TYPE_LENGTH (builtin_type_long);
671 return gnuv3_get_virtual_fn (value_ind (*this_p), method_type, voffset);
673 else
674 return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
677 /* Determine if we are currently in a C++ thunk. If so, get the address
678 of the routine we are thunking to and continue to there instead. */
680 static CORE_ADDR
681 gnuv3_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc)
683 CORE_ADDR real_stop_pc, method_stop_pc;
684 struct gdbarch *gdbarch = get_frame_arch (frame);
685 struct minimal_symbol *thunk_sym, *fn_sym;
686 struct obj_section *section;
687 char *thunk_name, *fn_name;
689 real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
690 if (real_stop_pc == 0)
691 real_stop_pc = stop_pc;
693 /* Find the linker symbol for this potential thunk. */
694 thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
695 section = find_pc_section (real_stop_pc);
696 if (thunk_sym == NULL || section == NULL)
697 return 0;
699 /* The symbol's demangled name should be something like "virtual
700 thunk to FUNCTION", where FUNCTION is the name of the function
701 being thunked to. */
702 thunk_name = SYMBOL_DEMANGLED_NAME (thunk_sym);
703 if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
704 return 0;
706 fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
707 fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile);
708 if (fn_sym == NULL)
709 return 0;
711 method_stop_pc = SYMBOL_VALUE_ADDRESS (fn_sym);
712 real_stop_pc = gdbarch_skip_trampoline_code
713 (gdbarch, frame, method_stop_pc);
714 if (real_stop_pc == 0)
715 real_stop_pc = method_stop_pc;
717 return real_stop_pc;
720 /* Return nonzero if a type should be passed by reference.
722 The rule in the v3 ABI document comes from section 3.1.1. If the
723 type has a non-trivial copy constructor or destructor, then the
724 caller must make a copy (by calling the copy constructor if there
725 is one or perform the copy itself otherwise), pass the address of
726 the copy, and then destroy the temporary (if necessary).
728 For return values with non-trivial copy constructors or
729 destructors, space will be allocated in the caller, and a pointer
730 will be passed as the first argument (preceding "this").
732 We don't have a bulletproof mechanism for determining whether a
733 constructor or destructor is trivial. For GCC and DWARF2 debug
734 information, we can check the artificial flag.
736 We don't do anything with the constructors or destructors,
737 but we have to get the argument passing right anyway. */
738 static int
739 gnuv3_pass_by_reference (struct type *type)
741 int fieldnum, fieldelem;
743 CHECK_TYPEDEF (type);
745 /* We're only interested in things that can have methods. */
746 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
747 && TYPE_CODE (type) != TYPE_CODE_CLASS
748 && TYPE_CODE (type) != TYPE_CODE_UNION)
749 return 0;
751 for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
752 for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
753 fieldelem++)
755 struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
756 char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
757 struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
759 /* If this function is marked as artificial, it is compiler-generated,
760 and we assume it is trivial. */
761 if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
762 continue;
764 /* If we've found a destructor, we must pass this by reference. */
765 if (name[0] == '~')
766 return 1;
768 /* If the mangled name of this method doesn't indicate that it
769 is a constructor, we're not interested.
771 FIXME drow/2007-09-23: We could do this using the name of
772 the method and the name of the class instead of dealing
773 with the mangled name. We don't have a convenient function
774 to strip off both leading scope qualifiers and trailing
775 template arguments yet. */
776 if (!is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem)))
777 continue;
779 /* If this method takes two arguments, and the second argument is
780 a reference to this class, then it is a copy constructor. */
781 if (TYPE_NFIELDS (fieldtype) == 2
782 && TYPE_CODE (TYPE_FIELD_TYPE (fieldtype, 1)) == TYPE_CODE_REF
783 && check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (fieldtype, 1))) == type)
784 return 1;
787 /* Even if all the constructors and destructors were artificial, one
788 of them may have invoked a non-artificial constructor or
789 destructor in a base class. If any base class needs to be passed
790 by reference, so does this class. Similarly for members, which
791 are constructed whenever this class is. We do not need to worry
792 about recursive loops here, since we are only looking at members
793 of complete class type. */
794 for (fieldnum = 0; fieldnum < TYPE_NFIELDS (type); fieldnum++)
795 if (gnuv3_pass_by_reference (TYPE_FIELD_TYPE (type, fieldnum)))
796 return 1;
798 return 0;
801 static void
802 init_gnuv3_ops (void)
804 vtable_type_gdbarch_data = gdbarch_data_register_post_init (build_gdb_vtable_type);
806 gnu_v3_abi_ops.shortname = "gnu-v3";
807 gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
808 gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
809 gnu_v3_abi_ops.is_destructor_name =
810 (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
811 gnu_v3_abi_ops.is_constructor_name =
812 (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
813 gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
814 gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
815 gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
816 gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
817 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
818 gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
819 gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
820 gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
821 gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
822 gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
823 gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
826 extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */
828 void
829 _initialize_gnu_v3_abi (void)
831 init_gnuv3_ops ();
833 register_cp_abi (&gnu_v3_abi_ops);