xref: /dflybsd-src/contrib/gdb-7/gdb/gnu-v3-abi.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Abstraction of GNU v3 abi.
25796c8dcSSimon Schubert    Contributed by Jim Blandy <jimb@redhat.com>
35796c8dcSSimon Schubert 
4*cf7f2e2dSJohn Marino    Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007, 2008, 2009, 2010
55796c8dcSSimon Schubert    Free Software Foundation, Inc.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "value.h"
245796c8dcSSimon Schubert #include "cp-abi.h"
255796c8dcSSimon Schubert #include "cp-support.h"
265796c8dcSSimon Schubert #include "demangle.h"
275796c8dcSSimon Schubert #include "objfiles.h"
285796c8dcSSimon Schubert #include "valprint.h"
29*cf7f2e2dSJohn Marino #include "c-lang.h"
305796c8dcSSimon Schubert 
315796c8dcSSimon Schubert #include "gdb_assert.h"
325796c8dcSSimon Schubert #include "gdb_string.h"
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert static struct cp_abi_ops gnu_v3_abi_ops;
355796c8dcSSimon Schubert 
365796c8dcSSimon Schubert static int
375796c8dcSSimon Schubert gnuv3_is_vtable_name (const char *name)
385796c8dcSSimon Schubert {
395796c8dcSSimon Schubert   return strncmp (name, "_ZTV", 4) == 0;
405796c8dcSSimon Schubert }
415796c8dcSSimon Schubert 
425796c8dcSSimon Schubert static int
435796c8dcSSimon Schubert gnuv3_is_operator_name (const char *name)
445796c8dcSSimon Schubert {
455796c8dcSSimon Schubert   return strncmp (name, "operator", 8) == 0;
465796c8dcSSimon Schubert }
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert 
495796c8dcSSimon Schubert /* To help us find the components of a vtable, we build ourselves a
505796c8dcSSimon Schubert    GDB type object representing the vtable structure.  Following the
515796c8dcSSimon Schubert    V3 ABI, it goes something like this:
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert    struct gdb_gnu_v3_abi_vtable {
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert      / * An array of virtual call and virtual base offsets.  The real
565796c8dcSSimon Schubert          length of this array depends on the class hierarchy; we use
575796c8dcSSimon Schubert          negative subscripts to access the elements.  Yucky, but
585796c8dcSSimon Schubert          better than the alternatives.  * /
595796c8dcSSimon Schubert      ptrdiff_t vcall_and_vbase_offsets[0];
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert      / * The offset from a virtual pointer referring to this table
625796c8dcSSimon Schubert          to the top of the complete object.  * /
635796c8dcSSimon Schubert      ptrdiff_t offset_to_top;
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert      / * The type_info pointer for this class.  This is really a
665796c8dcSSimon Schubert          std::type_info *, but GDB doesn't really look at the
675796c8dcSSimon Schubert          type_info object itself, so we don't bother to get the type
685796c8dcSSimon Schubert          exactly right.  * /
695796c8dcSSimon Schubert      void *type_info;
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert      / * Virtual table pointers in objects point here.  * /
725796c8dcSSimon Schubert 
735796c8dcSSimon Schubert      / * Virtual function pointers.  Like the vcall/vbase array, the
745796c8dcSSimon Schubert          real length of this table depends on the class hierarchy.  * /
755796c8dcSSimon Schubert      void (*virtual_functions[0]) ();
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert    };
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert    The catch, of course, is that the exact layout of this table
805796c8dcSSimon Schubert    depends on the ABI --- word size, endianness, alignment, etc.  So
815796c8dcSSimon Schubert    the GDB type object is actually a per-architecture kind of thing.
825796c8dcSSimon Schubert 
835796c8dcSSimon Schubert    vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
845796c8dcSSimon Schubert    which refers to the struct type * for this structure, laid out
855796c8dcSSimon Schubert    appropriately for the architecture.  */
865796c8dcSSimon Schubert static struct gdbarch_data *vtable_type_gdbarch_data;
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert /* Human-readable names for the numbers of the fields above.  */
905796c8dcSSimon Schubert enum {
915796c8dcSSimon Schubert   vtable_field_vcall_and_vbase_offsets,
925796c8dcSSimon Schubert   vtable_field_offset_to_top,
935796c8dcSSimon Schubert   vtable_field_type_info,
945796c8dcSSimon Schubert   vtable_field_virtual_functions
955796c8dcSSimon Schubert };
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
995796c8dcSSimon Schubert    described above, laid out appropriately for ARCH.
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert    We use this function as the gdbarch per-architecture data
1025796c8dcSSimon Schubert    initialization function.  */
1035796c8dcSSimon Schubert static void *
1045796c8dcSSimon Schubert build_gdb_vtable_type (struct gdbarch *arch)
1055796c8dcSSimon Schubert {
1065796c8dcSSimon Schubert   struct type *t;
1075796c8dcSSimon Schubert   struct field *field_list, *field;
1085796c8dcSSimon Schubert   int offset;
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert   struct type *void_ptr_type
1115796c8dcSSimon Schubert     = builtin_type (arch)->builtin_data_ptr;
1125796c8dcSSimon Schubert   struct type *ptr_to_void_fn_type
1135796c8dcSSimon Schubert     = builtin_type (arch)->builtin_func_ptr;
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert   /* ARCH can't give us the true ptrdiff_t type, so we guess.  */
1165796c8dcSSimon Schubert   struct type *ptrdiff_type
1175796c8dcSSimon Schubert     = arch_integer_type (arch, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert   /* We assume no padding is necessary, since GDB doesn't know
1205796c8dcSSimon Schubert      anything about alignment at the moment.  If this assumption bites
1215796c8dcSSimon Schubert      us, we should add a gdbarch method which, given a type, returns
1225796c8dcSSimon Schubert      the alignment that type requires, and then use that here.  */
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert   /* Build the field list.  */
1255796c8dcSSimon Schubert   field_list = xmalloc (sizeof (struct field [4]));
1265796c8dcSSimon Schubert   memset (field_list, 0, sizeof (struct field [4]));
1275796c8dcSSimon Schubert   field = &field_list[0];
1285796c8dcSSimon Schubert   offset = 0;
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert   /* ptrdiff_t vcall_and_vbase_offsets[0]; */
1315796c8dcSSimon Schubert   FIELD_NAME (*field) = "vcall_and_vbase_offsets";
1325796c8dcSSimon Schubert   FIELD_TYPE (*field) = lookup_array_range_type (ptrdiff_type, 0, -1);
1335796c8dcSSimon Schubert   FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
1345796c8dcSSimon Schubert   offset += TYPE_LENGTH (FIELD_TYPE (*field));
1355796c8dcSSimon Schubert   field++;
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert   /* ptrdiff_t offset_to_top; */
1385796c8dcSSimon Schubert   FIELD_NAME (*field) = "offset_to_top";
1395796c8dcSSimon Schubert   FIELD_TYPE (*field) = ptrdiff_type;
1405796c8dcSSimon Schubert   FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
1415796c8dcSSimon Schubert   offset += TYPE_LENGTH (FIELD_TYPE (*field));
1425796c8dcSSimon Schubert   field++;
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert   /* void *type_info; */
1455796c8dcSSimon Schubert   FIELD_NAME (*field) = "type_info";
1465796c8dcSSimon Schubert   FIELD_TYPE (*field) = void_ptr_type;
1475796c8dcSSimon Schubert   FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
1485796c8dcSSimon Schubert   offset += TYPE_LENGTH (FIELD_TYPE (*field));
1495796c8dcSSimon Schubert   field++;
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert   /* void (*virtual_functions[0]) (); */
1525796c8dcSSimon Schubert   FIELD_NAME (*field) = "virtual_functions";
1535796c8dcSSimon Schubert   FIELD_TYPE (*field) = lookup_array_range_type (ptr_to_void_fn_type, 0, -1);
1545796c8dcSSimon Schubert   FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
1555796c8dcSSimon Schubert   offset += TYPE_LENGTH (FIELD_TYPE (*field));
1565796c8dcSSimon Schubert   field++;
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert   /* We assumed in the allocation above that there were four fields.  */
1595796c8dcSSimon Schubert   gdb_assert (field == (field_list + 4));
1605796c8dcSSimon Schubert 
1615796c8dcSSimon Schubert   t = arch_type (arch, TYPE_CODE_STRUCT, offset, NULL);
1625796c8dcSSimon Schubert   TYPE_NFIELDS (t) = field - field_list;
1635796c8dcSSimon Schubert   TYPE_FIELDS (t) = field_list;
1645796c8dcSSimon Schubert   TYPE_TAG_NAME (t) = "gdb_gnu_v3_abi_vtable";
1655796c8dcSSimon Schubert   INIT_CPLUS_SPECIFIC (t);
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert   return t;
1685796c8dcSSimon Schubert }
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert /* Return the ptrdiff_t type used in the vtable type.  */
1725796c8dcSSimon Schubert static struct type *
1735796c8dcSSimon Schubert vtable_ptrdiff_type (struct gdbarch *gdbarch)
1745796c8dcSSimon Schubert {
1755796c8dcSSimon Schubert   struct type *vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data);
1765796c8dcSSimon Schubert 
1775796c8dcSSimon Schubert   /* The "offset_to_top" field has the appropriate (ptrdiff_t) type.  */
1785796c8dcSSimon Schubert   return TYPE_FIELD_TYPE (vtable_type, vtable_field_offset_to_top);
1795796c8dcSSimon Schubert }
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert /* Return the offset from the start of the imaginary `struct
1825796c8dcSSimon Schubert    gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
1835796c8dcSSimon Schubert    (i.e., where objects' virtual table pointers point).  */
1845796c8dcSSimon Schubert static int
1855796c8dcSSimon Schubert vtable_address_point_offset (struct gdbarch *gdbarch)
1865796c8dcSSimon Schubert {
1875796c8dcSSimon Schubert   struct type *vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data);
1885796c8dcSSimon Schubert 
1895796c8dcSSimon Schubert   return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions)
1905796c8dcSSimon Schubert           / TARGET_CHAR_BIT);
1915796c8dcSSimon Schubert }
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert 
194*cf7f2e2dSJohn Marino /* Determine whether structure TYPE is a dynamic class.  Cache the
195*cf7f2e2dSJohn Marino    result.  */
196*cf7f2e2dSJohn Marino 
197*cf7f2e2dSJohn Marino static int
198*cf7f2e2dSJohn Marino gnuv3_dynamic_class (struct type *type)
199*cf7f2e2dSJohn Marino {
200*cf7f2e2dSJohn Marino   int fieldnum, fieldelem;
201*cf7f2e2dSJohn Marino 
202*cf7f2e2dSJohn Marino   if (TYPE_CPLUS_DYNAMIC (type))
203*cf7f2e2dSJohn Marino     return TYPE_CPLUS_DYNAMIC (type) == 1;
204*cf7f2e2dSJohn Marino 
205*cf7f2e2dSJohn Marino   ALLOCATE_CPLUS_STRUCT_TYPE (type);
206*cf7f2e2dSJohn Marino 
207*cf7f2e2dSJohn Marino   for (fieldnum = 0; fieldnum < TYPE_N_BASECLASSES (type); fieldnum++)
208*cf7f2e2dSJohn Marino     if (BASETYPE_VIA_VIRTUAL (type, fieldnum)
209*cf7f2e2dSJohn Marino 	|| gnuv3_dynamic_class (TYPE_FIELD_TYPE (type, fieldnum)))
210*cf7f2e2dSJohn Marino       {
211*cf7f2e2dSJohn Marino 	TYPE_CPLUS_DYNAMIC (type) = 1;
212*cf7f2e2dSJohn Marino 	return 1;
213*cf7f2e2dSJohn Marino       }
214*cf7f2e2dSJohn Marino 
215*cf7f2e2dSJohn Marino   for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
216*cf7f2e2dSJohn Marino     for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
217*cf7f2e2dSJohn Marino 	 fieldelem++)
218*cf7f2e2dSJohn Marino       {
219*cf7f2e2dSJohn Marino 	struct fn_field *f = TYPE_FN_FIELDLIST1 (type, fieldnum);
220*cf7f2e2dSJohn Marino 
221*cf7f2e2dSJohn Marino 	if (TYPE_FN_FIELD_VIRTUAL_P (f, fieldelem))
222*cf7f2e2dSJohn Marino 	  {
223*cf7f2e2dSJohn Marino 	    TYPE_CPLUS_DYNAMIC (type) = 1;
224*cf7f2e2dSJohn Marino 	    return 1;
225*cf7f2e2dSJohn Marino 	  }
226*cf7f2e2dSJohn Marino       }
227*cf7f2e2dSJohn Marino 
228*cf7f2e2dSJohn Marino   TYPE_CPLUS_DYNAMIC (type) = -1;
229*cf7f2e2dSJohn Marino   return 0;
230*cf7f2e2dSJohn Marino }
231*cf7f2e2dSJohn Marino 
232*cf7f2e2dSJohn Marino /* Find the vtable for a value of CONTAINER_TYPE located at
233*cf7f2e2dSJohn Marino    CONTAINER_ADDR.  Return a value of the correct vtable type for this
234*cf7f2e2dSJohn Marino    architecture, or NULL if CONTAINER does not have a vtable.  */
235*cf7f2e2dSJohn Marino 
236*cf7f2e2dSJohn Marino static struct value *
237*cf7f2e2dSJohn Marino gnuv3_get_vtable (struct gdbarch *gdbarch,
238*cf7f2e2dSJohn Marino 		  struct type *container_type, CORE_ADDR container_addr)
239*cf7f2e2dSJohn Marino {
240*cf7f2e2dSJohn Marino   struct type *vtable_type = gdbarch_data (gdbarch,
241*cf7f2e2dSJohn Marino 					   vtable_type_gdbarch_data);
242*cf7f2e2dSJohn Marino   struct type *vtable_pointer_type;
243*cf7f2e2dSJohn Marino   struct value *vtable_pointer;
244*cf7f2e2dSJohn Marino   CORE_ADDR vtable_address;
245*cf7f2e2dSJohn Marino 
246*cf7f2e2dSJohn Marino   /* If this type does not have a virtual table, don't read the first
247*cf7f2e2dSJohn Marino      field.  */
248*cf7f2e2dSJohn Marino   if (!gnuv3_dynamic_class (check_typedef (container_type)))
249*cf7f2e2dSJohn Marino     return NULL;
250*cf7f2e2dSJohn Marino 
251*cf7f2e2dSJohn Marino   /* We do not consult the debug information to find the virtual table.
252*cf7f2e2dSJohn Marino      The ABI specifies that it is always at offset zero in any class,
253*cf7f2e2dSJohn Marino      and debug information may not represent it.
254*cf7f2e2dSJohn Marino 
255*cf7f2e2dSJohn Marino      We avoid using value_contents on principle, because the object might
256*cf7f2e2dSJohn Marino      be large.  */
257*cf7f2e2dSJohn Marino 
258*cf7f2e2dSJohn Marino   /* Find the type "pointer to virtual table".  */
259*cf7f2e2dSJohn Marino   vtable_pointer_type = lookup_pointer_type (vtable_type);
260*cf7f2e2dSJohn Marino 
261*cf7f2e2dSJohn Marino   /* Load it from the start of the class.  */
262*cf7f2e2dSJohn Marino   vtable_pointer = value_at (vtable_pointer_type, container_addr);
263*cf7f2e2dSJohn Marino   vtable_address = value_as_address (vtable_pointer);
264*cf7f2e2dSJohn Marino 
265*cf7f2e2dSJohn Marino   /* Correct it to point at the start of the virtual table, rather
266*cf7f2e2dSJohn Marino      than the address point.  */
267*cf7f2e2dSJohn Marino   return value_at_lazy (vtable_type,
268*cf7f2e2dSJohn Marino 			vtable_address - vtable_address_point_offset (gdbarch));
269*cf7f2e2dSJohn Marino }
270*cf7f2e2dSJohn Marino 
271*cf7f2e2dSJohn Marino 
2725796c8dcSSimon Schubert static struct type *
2735796c8dcSSimon Schubert gnuv3_rtti_type (struct value *value,
2745796c8dcSSimon Schubert                  int *full_p, int *top_p, int *using_enc_p)
2755796c8dcSSimon Schubert {
2765796c8dcSSimon Schubert   struct gdbarch *gdbarch;
2775796c8dcSSimon Schubert   struct type *values_type = check_typedef (value_type (value));
2785796c8dcSSimon Schubert   struct value *vtable;
2795796c8dcSSimon Schubert   struct minimal_symbol *vtable_symbol;
2805796c8dcSSimon Schubert   const char *vtable_symbol_name;
2815796c8dcSSimon Schubert   const char *class_name;
2825796c8dcSSimon Schubert   struct type *run_time_type;
2835796c8dcSSimon Schubert   LONGEST offset_to_top;
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert   /* We only have RTTI for class objects.  */
2865796c8dcSSimon Schubert   if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
2875796c8dcSSimon Schubert     return NULL;
2885796c8dcSSimon Schubert 
2895796c8dcSSimon Schubert   /* Determine architecture.  */
2905796c8dcSSimon Schubert   gdbarch = get_type_arch (values_type);
2915796c8dcSSimon Schubert 
2925796c8dcSSimon Schubert   if (using_enc_p)
2935796c8dcSSimon Schubert     *using_enc_p = 0;
2945796c8dcSSimon Schubert 
295*cf7f2e2dSJohn Marino   vtable = gnuv3_get_vtable (gdbarch, value_type (value),
296*cf7f2e2dSJohn Marino 			     value_as_address (value_addr (value)));
297*cf7f2e2dSJohn Marino   if (vtable == NULL)
298*cf7f2e2dSJohn Marino     return NULL;
2995796c8dcSSimon Schubert 
3005796c8dcSSimon Schubert   /* Find the linker symbol for this vtable.  */
3015796c8dcSSimon Schubert   vtable_symbol
3025796c8dcSSimon Schubert     = lookup_minimal_symbol_by_pc (value_address (vtable)
3035796c8dcSSimon Schubert                                    + value_embedded_offset (vtable));
3045796c8dcSSimon Schubert   if (! vtable_symbol)
3055796c8dcSSimon Schubert     return NULL;
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert   /* The symbol's demangled name should be something like "vtable for
3085796c8dcSSimon Schubert      CLASS", where CLASS is the name of the run-time type of VALUE.
3095796c8dcSSimon Schubert      If we didn't like this approach, we could instead look in the
3105796c8dcSSimon Schubert      type_info object itself to get the class name.  But this way
3115796c8dcSSimon Schubert      should work just as well, and doesn't read target memory.  */
3125796c8dcSSimon Schubert   vtable_symbol_name = SYMBOL_DEMANGLED_NAME (vtable_symbol);
3135796c8dcSSimon Schubert   if (vtable_symbol_name == NULL
3145796c8dcSSimon Schubert       || strncmp (vtable_symbol_name, "vtable for ", 11))
3155796c8dcSSimon Schubert     {
3165796c8dcSSimon Schubert       warning (_("can't find linker symbol for virtual table for `%s' value"),
3175796c8dcSSimon Schubert 	       TYPE_NAME (values_type));
3185796c8dcSSimon Schubert       if (vtable_symbol_name)
3195796c8dcSSimon Schubert 	warning (_("  found `%s' instead"), vtable_symbol_name);
3205796c8dcSSimon Schubert       return NULL;
3215796c8dcSSimon Schubert     }
3225796c8dcSSimon Schubert   class_name = vtable_symbol_name + 11;
3235796c8dcSSimon Schubert 
3245796c8dcSSimon Schubert   /* Try to look up the class name as a type name.  */
3255796c8dcSSimon Schubert   /* FIXME: chastain/2003-11-26: block=NULL is bogus.  See pr gdb/1465. */
3265796c8dcSSimon Schubert   run_time_type = cp_lookup_rtti_type (class_name, NULL);
3275796c8dcSSimon Schubert   if (run_time_type == NULL)
3285796c8dcSSimon Schubert     return NULL;
3295796c8dcSSimon Schubert 
3305796c8dcSSimon Schubert   /* Get the offset from VALUE to the top of the complete object.
3315796c8dcSSimon Schubert      NOTE: this is the reverse of the meaning of *TOP_P.  */
3325796c8dcSSimon Schubert   offset_to_top
3335796c8dcSSimon Schubert     = value_as_long (value_field (vtable, vtable_field_offset_to_top));
3345796c8dcSSimon Schubert 
3355796c8dcSSimon Schubert   if (full_p)
3365796c8dcSSimon Schubert     *full_p = (- offset_to_top == value_embedded_offset (value)
3375796c8dcSSimon Schubert                && (TYPE_LENGTH (value_enclosing_type (value))
3385796c8dcSSimon Schubert                    >= TYPE_LENGTH (run_time_type)));
3395796c8dcSSimon Schubert   if (top_p)
3405796c8dcSSimon Schubert     *top_p = - offset_to_top;
3415796c8dcSSimon Schubert   return run_time_type;
3425796c8dcSSimon Schubert }
3435796c8dcSSimon Schubert 
3445796c8dcSSimon Schubert /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
3455796c8dcSSimon Schubert    function, of type FNTYPE.  */
3465796c8dcSSimon Schubert 
3475796c8dcSSimon Schubert static struct value *
3485796c8dcSSimon Schubert gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
3495796c8dcSSimon Schubert 		      struct type *fntype, int vtable_index)
3505796c8dcSSimon Schubert {
351*cf7f2e2dSJohn Marino   struct value *vtable, *vfn;
352*cf7f2e2dSJohn Marino 
353*cf7f2e2dSJohn Marino   /* Every class with virtual functions must have a vtable.  */
354*cf7f2e2dSJohn Marino   vtable = gnuv3_get_vtable (gdbarch, value_type (container),
355*cf7f2e2dSJohn Marino 			     value_as_address (value_addr (container)));
356*cf7f2e2dSJohn Marino   gdb_assert (vtable != NULL);
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert   /* Fetch the appropriate function pointer from the vtable.  */
3595796c8dcSSimon Schubert   vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
3605796c8dcSSimon Schubert                          vtable_index);
3615796c8dcSSimon Schubert 
3625796c8dcSSimon Schubert   /* If this architecture uses function descriptors directly in the vtable,
3635796c8dcSSimon Schubert      then the address of the vtable entry is actually a "function pointer"
3645796c8dcSSimon Schubert      (i.e. points to the descriptor).  We don't need to scale the index
3655796c8dcSSimon Schubert      by the size of a function descriptor; GCC does that before outputing
3665796c8dcSSimon Schubert      debug information.  */
3675796c8dcSSimon Schubert   if (gdbarch_vtable_function_descriptors (gdbarch))
3685796c8dcSSimon Schubert     vfn = value_addr (vfn);
3695796c8dcSSimon Schubert 
3705796c8dcSSimon Schubert   /* Cast the function pointer to the appropriate type.  */
3715796c8dcSSimon Schubert   vfn = value_cast (lookup_pointer_type (fntype), vfn);
3725796c8dcSSimon Schubert 
3735796c8dcSSimon Schubert   return vfn;
3745796c8dcSSimon Schubert }
3755796c8dcSSimon Schubert 
3765796c8dcSSimon Schubert /* GNU v3 implementation of value_virtual_fn_field.  See cp-abi.h
3775796c8dcSSimon Schubert    for a description of the arguments.  */
3785796c8dcSSimon Schubert 
3795796c8dcSSimon Schubert static struct value *
3805796c8dcSSimon Schubert gnuv3_virtual_fn_field (struct value **value_p,
3815796c8dcSSimon Schubert                         struct fn_field *f, int j,
3825796c8dcSSimon Schubert 			struct type *vfn_base, int offset)
3835796c8dcSSimon Schubert {
3845796c8dcSSimon Schubert   struct type *values_type = check_typedef (value_type (*value_p));
3855796c8dcSSimon Schubert   struct gdbarch *gdbarch;
3865796c8dcSSimon Schubert 
3875796c8dcSSimon Schubert   /* Some simple sanity checks.  */
3885796c8dcSSimon Schubert   if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
3895796c8dcSSimon Schubert     error (_("Only classes can have virtual functions."));
3905796c8dcSSimon Schubert 
3915796c8dcSSimon Schubert   /* Determine architecture.  */
3925796c8dcSSimon Schubert   gdbarch = get_type_arch (values_type);
3935796c8dcSSimon Schubert 
3945796c8dcSSimon Schubert   /* Cast our value to the base class which defines this virtual
3955796c8dcSSimon Schubert      function.  This takes care of any necessary `this'
3965796c8dcSSimon Schubert      adjustments.  */
3975796c8dcSSimon Schubert   if (vfn_base != values_type)
3985796c8dcSSimon Schubert     *value_p = value_cast (vfn_base, *value_p);
3995796c8dcSSimon Schubert 
4005796c8dcSSimon Schubert   return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
4015796c8dcSSimon Schubert 			       TYPE_FN_FIELD_VOFFSET (f, j));
4025796c8dcSSimon Schubert }
4035796c8dcSSimon Schubert 
4045796c8dcSSimon Schubert /* Compute the offset of the baseclass which is
4055796c8dcSSimon Schubert    the INDEXth baseclass of class TYPE,
4065796c8dcSSimon Schubert    for value at VALADDR (in host) at ADDRESS (in target).
4075796c8dcSSimon Schubert    The result is the offset of the baseclass value relative
4085796c8dcSSimon Schubert    to (the address of)(ARG) + OFFSET.
4095796c8dcSSimon Schubert 
4105796c8dcSSimon Schubert    -1 is returned on error. */
4115796c8dcSSimon Schubert static int
4125796c8dcSSimon Schubert gnuv3_baseclass_offset (struct type *type, int index, const bfd_byte *valaddr,
4135796c8dcSSimon Schubert 			CORE_ADDR address)
4145796c8dcSSimon Schubert {
4155796c8dcSSimon Schubert   struct gdbarch *gdbarch;
4165796c8dcSSimon Schubert   struct type *ptr_type;
4175796c8dcSSimon Schubert   struct value *vtable;
4185796c8dcSSimon Schubert   struct value *vbase_array;
4195796c8dcSSimon Schubert   long int cur_base_offset, base_offset;
4205796c8dcSSimon Schubert 
4215796c8dcSSimon Schubert   /* Determine architecture.  */
4225796c8dcSSimon Schubert   gdbarch = get_type_arch (type);
4235796c8dcSSimon Schubert   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
4245796c8dcSSimon Schubert 
4255796c8dcSSimon Schubert   /* If it isn't a virtual base, this is easy.  The offset is in the
4265796c8dcSSimon Schubert      type definition.  */
4275796c8dcSSimon Schubert   if (!BASETYPE_VIA_VIRTUAL (type, index))
4285796c8dcSSimon Schubert     return TYPE_BASECLASS_BITPOS (type, index) / 8;
4295796c8dcSSimon Schubert 
4305796c8dcSSimon Schubert   /* To access a virtual base, we need to use the vbase offset stored in
4315796c8dcSSimon Schubert      our vtable.  Recent GCC versions provide this information.  If it isn't
4325796c8dcSSimon Schubert      available, we could get what we needed from RTTI, or from drawing the
4335796c8dcSSimon Schubert      complete inheritance graph based on the debug info.  Neither is
4345796c8dcSSimon Schubert      worthwhile.  */
4355796c8dcSSimon Schubert   cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
4365796c8dcSSimon Schubert   if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
4375796c8dcSSimon Schubert     error (_("Expected a negative vbase offset (old compiler?)"));
4385796c8dcSSimon Schubert 
4395796c8dcSSimon Schubert   cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
4405796c8dcSSimon Schubert   if ((- cur_base_offset) % TYPE_LENGTH (ptr_type) != 0)
4415796c8dcSSimon Schubert     error (_("Misaligned vbase offset."));
4425796c8dcSSimon Schubert   cur_base_offset = cur_base_offset / ((int) TYPE_LENGTH (ptr_type));
4435796c8dcSSimon Schubert 
444*cf7f2e2dSJohn Marino   vtable = gnuv3_get_vtable (gdbarch, type, address);
445*cf7f2e2dSJohn Marino   gdb_assert (vtable != NULL);
4465796c8dcSSimon Schubert   vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
4475796c8dcSSimon Schubert   base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
4485796c8dcSSimon Schubert   return base_offset;
4495796c8dcSSimon Schubert }
4505796c8dcSSimon Schubert 
4515796c8dcSSimon Schubert /* Locate a virtual method in DOMAIN or its non-virtual base classes
4525796c8dcSSimon Schubert    which has virtual table index VOFFSET.  The method has an associated
4535796c8dcSSimon Schubert    "this" adjustment of ADJUSTMENT bytes.  */
4545796c8dcSSimon Schubert 
4555796c8dcSSimon Schubert static const char *
4565796c8dcSSimon Schubert gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
4575796c8dcSSimon Schubert 		      LONGEST adjustment)
4585796c8dcSSimon Schubert {
4595796c8dcSSimon Schubert   int i;
4605796c8dcSSimon Schubert 
4615796c8dcSSimon Schubert   /* Search this class first.  */
4625796c8dcSSimon Schubert   if (adjustment == 0)
4635796c8dcSSimon Schubert     {
4645796c8dcSSimon Schubert       int len;
4655796c8dcSSimon Schubert 
4665796c8dcSSimon Schubert       len = TYPE_NFN_FIELDS (domain);
4675796c8dcSSimon Schubert       for (i = 0; i < len; i++)
4685796c8dcSSimon Schubert 	{
4695796c8dcSSimon Schubert 	  int len2, j;
4705796c8dcSSimon Schubert 	  struct fn_field *f;
4715796c8dcSSimon Schubert 
4725796c8dcSSimon Schubert 	  f = TYPE_FN_FIELDLIST1 (domain, i);
4735796c8dcSSimon Schubert 	  len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
4745796c8dcSSimon Schubert 
4755796c8dcSSimon Schubert 	  check_stub_method_group (domain, i);
4765796c8dcSSimon Schubert 	  for (j = 0; j < len2; j++)
4775796c8dcSSimon Schubert 	    if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset)
4785796c8dcSSimon Schubert 	      return TYPE_FN_FIELD_PHYSNAME (f, j);
4795796c8dcSSimon Schubert 	}
4805796c8dcSSimon Schubert     }
4815796c8dcSSimon Schubert 
4825796c8dcSSimon Schubert   /* Next search non-virtual bases.  If it's in a virtual base,
4835796c8dcSSimon Schubert      we're out of luck.  */
4845796c8dcSSimon Schubert   for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
4855796c8dcSSimon Schubert     {
4865796c8dcSSimon Schubert       int pos;
4875796c8dcSSimon Schubert       struct type *basetype;
4885796c8dcSSimon Schubert 
4895796c8dcSSimon Schubert       if (BASETYPE_VIA_VIRTUAL (domain, i))
4905796c8dcSSimon Schubert 	continue;
4915796c8dcSSimon Schubert 
4925796c8dcSSimon Schubert       pos = TYPE_BASECLASS_BITPOS (domain, i) / 8;
4935796c8dcSSimon Schubert       basetype = TYPE_FIELD_TYPE (domain, i);
4945796c8dcSSimon Schubert       /* Recurse with a modified adjustment.  We don't need to adjust
4955796c8dcSSimon Schubert 	 voffset.  */
4965796c8dcSSimon Schubert       if (adjustment >= pos && adjustment < pos + TYPE_LENGTH (basetype))
4975796c8dcSSimon Schubert 	return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
4985796c8dcSSimon Schubert     }
4995796c8dcSSimon Schubert 
5005796c8dcSSimon Schubert   return NULL;
5015796c8dcSSimon Schubert }
5025796c8dcSSimon Schubert 
5035796c8dcSSimon Schubert /* Decode GNU v3 method pointer.  */
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert static int
5065796c8dcSSimon Schubert gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
5075796c8dcSSimon Schubert 			 const gdb_byte *contents,
5085796c8dcSSimon Schubert 			 CORE_ADDR *value_p,
5095796c8dcSSimon Schubert 			 LONGEST *adjustment_p)
5105796c8dcSSimon Schubert {
5115796c8dcSSimon Schubert   struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
5125796c8dcSSimon Schubert   struct type *offset_type = vtable_ptrdiff_type (gdbarch);
5135796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5145796c8dcSSimon Schubert   CORE_ADDR ptr_value;
5155796c8dcSSimon Schubert   LONGEST voffset, adjustment;
5165796c8dcSSimon Schubert   int vbit;
5175796c8dcSSimon Schubert 
5185796c8dcSSimon Schubert   /* Extract the pointer to member.  The first element is either a pointer
5195796c8dcSSimon Schubert      or a vtable offset.  For pointers, we need to use extract_typed_address
5205796c8dcSSimon Schubert      to allow the back-end to convert the pointer to a GDB address -- but
5215796c8dcSSimon Schubert      vtable offsets we must handle as integers.  At this point, we do not
5225796c8dcSSimon Schubert      yet know which case we have, so we extract the value under both
5235796c8dcSSimon Schubert      interpretations and choose the right one later on.  */
5245796c8dcSSimon Schubert   ptr_value = extract_typed_address (contents, funcptr_type);
5255796c8dcSSimon Schubert   voffset = extract_signed_integer (contents,
5265796c8dcSSimon Schubert 				    TYPE_LENGTH (funcptr_type), byte_order);
5275796c8dcSSimon Schubert   contents += TYPE_LENGTH (funcptr_type);
5285796c8dcSSimon Schubert   adjustment = extract_signed_integer (contents,
5295796c8dcSSimon Schubert 				       TYPE_LENGTH (offset_type), byte_order);
5305796c8dcSSimon Schubert 
5315796c8dcSSimon Schubert   if (!gdbarch_vbit_in_delta (gdbarch))
5325796c8dcSSimon Schubert     {
5335796c8dcSSimon Schubert       vbit = voffset & 1;
5345796c8dcSSimon Schubert       voffset = voffset ^ vbit;
5355796c8dcSSimon Schubert     }
5365796c8dcSSimon Schubert   else
5375796c8dcSSimon Schubert     {
5385796c8dcSSimon Schubert       vbit = adjustment & 1;
5395796c8dcSSimon Schubert       adjustment = adjustment >> 1;
5405796c8dcSSimon Schubert     }
5415796c8dcSSimon Schubert 
5425796c8dcSSimon Schubert   *value_p = vbit? voffset : ptr_value;
5435796c8dcSSimon Schubert   *adjustment_p = adjustment;
5445796c8dcSSimon Schubert   return vbit;
5455796c8dcSSimon Schubert }
5465796c8dcSSimon Schubert 
5475796c8dcSSimon Schubert /* GNU v3 implementation of cplus_print_method_ptr.  */
5485796c8dcSSimon Schubert 
5495796c8dcSSimon Schubert static void
5505796c8dcSSimon Schubert gnuv3_print_method_ptr (const gdb_byte *contents,
5515796c8dcSSimon Schubert 			struct type *type,
5525796c8dcSSimon Schubert 			struct ui_file *stream)
5535796c8dcSSimon Schubert {
5545796c8dcSSimon Schubert   struct type *domain = TYPE_DOMAIN_TYPE (type);
5555796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (domain);
5565796c8dcSSimon Schubert   CORE_ADDR ptr_value;
5575796c8dcSSimon Schubert   LONGEST adjustment;
5585796c8dcSSimon Schubert   int vbit;
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert   /* Extract the pointer to member.  */
5615796c8dcSSimon Schubert   vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
5625796c8dcSSimon Schubert 
5635796c8dcSSimon Schubert   /* Check for NULL.  */
5645796c8dcSSimon Schubert   if (ptr_value == 0 && vbit == 0)
5655796c8dcSSimon Schubert     {
5665796c8dcSSimon Schubert       fprintf_filtered (stream, "NULL");
5675796c8dcSSimon Schubert       return;
5685796c8dcSSimon Schubert     }
5695796c8dcSSimon Schubert 
5705796c8dcSSimon Schubert   /* Search for a virtual method.  */
5715796c8dcSSimon Schubert   if (vbit)
5725796c8dcSSimon Schubert     {
5735796c8dcSSimon Schubert       CORE_ADDR voffset;
5745796c8dcSSimon Schubert       const char *physname;
5755796c8dcSSimon Schubert 
5765796c8dcSSimon Schubert       /* It's a virtual table offset, maybe in this class.  Search
5775796c8dcSSimon Schubert 	 for a field with the correct vtable offset.  First convert it
5785796c8dcSSimon Schubert 	 to an index, as used in TYPE_FN_FIELD_VOFFSET.  */
5795796c8dcSSimon Schubert       voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
5805796c8dcSSimon Schubert 
5815796c8dcSSimon Schubert       physname = gnuv3_find_method_in (domain, voffset, adjustment);
5825796c8dcSSimon Schubert 
5835796c8dcSSimon Schubert       /* If we found a method, print that.  We don't bother to disambiguate
5845796c8dcSSimon Schubert 	 possible paths to the method based on the adjustment.  */
5855796c8dcSSimon Schubert       if (physname)
5865796c8dcSSimon Schubert 	{
5875796c8dcSSimon Schubert 	  char *demangled_name = cplus_demangle (physname,
5885796c8dcSSimon Schubert 						 DMGL_ANSI | DMGL_PARAMS);
589*cf7f2e2dSJohn Marino 
5905796c8dcSSimon Schubert 	  fprintf_filtered (stream, "&virtual ");
591*cf7f2e2dSJohn Marino 	  if (demangled_name == NULL)
592*cf7f2e2dSJohn Marino 	    fputs_filtered (physname, stream);
593*cf7f2e2dSJohn Marino 	  else
594*cf7f2e2dSJohn Marino 	    {
5955796c8dcSSimon Schubert 	      fputs_filtered (demangled_name, stream);
5965796c8dcSSimon Schubert 	      xfree (demangled_name);
597*cf7f2e2dSJohn Marino 	    }
5985796c8dcSSimon Schubert 	  return;
5995796c8dcSSimon Schubert 	}
6005796c8dcSSimon Schubert     }
601*cf7f2e2dSJohn Marino   else if (ptr_value != 0)
602*cf7f2e2dSJohn Marino     {
603*cf7f2e2dSJohn Marino       /* Found a non-virtual function: print out the type.  */
604*cf7f2e2dSJohn Marino       fputs_filtered ("(", stream);
605*cf7f2e2dSJohn Marino       c_print_type (type, "", stream, -1, 0);
606*cf7f2e2dSJohn Marino       fputs_filtered (") ", stream);
6075796c8dcSSimon Schubert     }
6085796c8dcSSimon Schubert 
6095796c8dcSSimon Schubert   /* We didn't find it; print the raw data.  */
6105796c8dcSSimon Schubert   if (vbit)
6115796c8dcSSimon Schubert     {
6125796c8dcSSimon Schubert       fprintf_filtered (stream, "&virtual table offset ");
6135796c8dcSSimon Schubert       print_longest (stream, 'd', 1, ptr_value);
6145796c8dcSSimon Schubert     }
6155796c8dcSSimon Schubert   else
6165796c8dcSSimon Schubert     print_address_demangle (gdbarch, ptr_value, stream, demangle);
6175796c8dcSSimon Schubert 
6185796c8dcSSimon Schubert   if (adjustment)
6195796c8dcSSimon Schubert     {
6205796c8dcSSimon Schubert       fprintf_filtered (stream, ", this adjustment ");
6215796c8dcSSimon Schubert       print_longest (stream, 'd', 1, adjustment);
6225796c8dcSSimon Schubert     }
6235796c8dcSSimon Schubert }
6245796c8dcSSimon Schubert 
6255796c8dcSSimon Schubert /* GNU v3 implementation of cplus_method_ptr_size.  */
6265796c8dcSSimon Schubert 
6275796c8dcSSimon Schubert static int
6285796c8dcSSimon Schubert gnuv3_method_ptr_size (struct type *type)
6295796c8dcSSimon Schubert {
6305796c8dcSSimon Schubert   struct type *domain_type = check_typedef (TYPE_DOMAIN_TYPE (type));
6315796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (domain_type);
632*cf7f2e2dSJohn Marino 
6335796c8dcSSimon Schubert   return 2 * TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
6345796c8dcSSimon Schubert }
6355796c8dcSSimon Schubert 
6365796c8dcSSimon Schubert /* GNU v3 implementation of cplus_make_method_ptr.  */
6375796c8dcSSimon Schubert 
6385796c8dcSSimon Schubert static void
6395796c8dcSSimon Schubert gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
6405796c8dcSSimon Schubert 		       CORE_ADDR value, int is_virtual)
6415796c8dcSSimon Schubert {
6425796c8dcSSimon Schubert   struct type *domain_type = check_typedef (TYPE_DOMAIN_TYPE (type));
6435796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (domain_type);
6445796c8dcSSimon Schubert   int size = TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
6455796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6465796c8dcSSimon Schubert 
6475796c8dcSSimon Schubert   /* FIXME drow/2006-12-24: The adjustment of "this" is currently
6485796c8dcSSimon Schubert      always zero, since the method pointer is of the correct type.
6495796c8dcSSimon Schubert      But if the method pointer came from a base class, this is
6505796c8dcSSimon Schubert      incorrect - it should be the offset to the base.  The best
6515796c8dcSSimon Schubert      fix might be to create the pointer to member pointing at the
6525796c8dcSSimon Schubert      base class and cast it to the derived class, but that requires
6535796c8dcSSimon Schubert      support for adjusting pointers to members when casting them -
6545796c8dcSSimon Schubert      not currently supported by GDB.  */
6555796c8dcSSimon Schubert 
6565796c8dcSSimon Schubert   if (!gdbarch_vbit_in_delta (gdbarch))
6575796c8dcSSimon Schubert     {
6585796c8dcSSimon Schubert       store_unsigned_integer (contents, size, byte_order, value | is_virtual);
6595796c8dcSSimon Schubert       store_unsigned_integer (contents + size, size, byte_order, 0);
6605796c8dcSSimon Schubert     }
6615796c8dcSSimon Schubert   else
6625796c8dcSSimon Schubert     {
6635796c8dcSSimon Schubert       store_unsigned_integer (contents, size, byte_order, value);
6645796c8dcSSimon Schubert       store_unsigned_integer (contents + size, size, byte_order, is_virtual);
6655796c8dcSSimon Schubert     }
6665796c8dcSSimon Schubert }
6675796c8dcSSimon Schubert 
6685796c8dcSSimon Schubert /* GNU v3 implementation of cplus_method_ptr_to_value.  */
6695796c8dcSSimon Schubert 
6705796c8dcSSimon Schubert static struct value *
6715796c8dcSSimon Schubert gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
6725796c8dcSSimon Schubert {
6735796c8dcSSimon Schubert   struct gdbarch *gdbarch;
6745796c8dcSSimon Schubert   const gdb_byte *contents = value_contents (method_ptr);
6755796c8dcSSimon Schubert   CORE_ADDR ptr_value;
6765796c8dcSSimon Schubert   struct type *domain_type, *final_type, *method_type;
6775796c8dcSSimon Schubert   LONGEST adjustment;
6785796c8dcSSimon Schubert   int vbit;
6795796c8dcSSimon Schubert 
6805796c8dcSSimon Schubert   domain_type = TYPE_DOMAIN_TYPE (check_typedef (value_type (method_ptr)));
6815796c8dcSSimon Schubert   final_type = lookup_pointer_type (domain_type);
6825796c8dcSSimon Schubert 
6835796c8dcSSimon Schubert   method_type = TYPE_TARGET_TYPE (check_typedef (value_type (method_ptr)));
6845796c8dcSSimon Schubert 
6855796c8dcSSimon Schubert   /* Extract the pointer to member.  */
6865796c8dcSSimon Schubert   gdbarch = get_type_arch (domain_type);
6875796c8dcSSimon Schubert   vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
6885796c8dcSSimon Schubert 
6895796c8dcSSimon Schubert   /* First convert THIS to match the containing type of the pointer to
6905796c8dcSSimon Schubert      member.  This cast may adjust the value of THIS.  */
6915796c8dcSSimon Schubert   *this_p = value_cast (final_type, *this_p);
6925796c8dcSSimon Schubert 
6935796c8dcSSimon Schubert   /* Then apply whatever adjustment is necessary.  This creates a somewhat
6945796c8dcSSimon Schubert      strange pointer: it claims to have type FINAL_TYPE, but in fact it
6955796c8dcSSimon Schubert      might not be a valid FINAL_TYPE.  For instance, it might be a
6965796c8dcSSimon Schubert      base class of FINAL_TYPE.  And if it's not the primary base class,
6975796c8dcSSimon Schubert      then printing it out as a FINAL_TYPE object would produce some pretty
6985796c8dcSSimon Schubert      garbage.
6995796c8dcSSimon Schubert 
7005796c8dcSSimon Schubert      But we don't really know the type of the first argument in
7015796c8dcSSimon Schubert      METHOD_TYPE either, which is why this happens.  We can't
7025796c8dcSSimon Schubert      dereference this later as a FINAL_TYPE, but once we arrive in the
7035796c8dcSSimon Schubert      called method we'll have debugging information for the type of
7045796c8dcSSimon Schubert      "this" - and that'll match the value we produce here.
7055796c8dcSSimon Schubert 
7065796c8dcSSimon Schubert      You can provoke this case by casting a Base::* to a Derived::*, for
7075796c8dcSSimon Schubert      instance.  */
7085796c8dcSSimon Schubert   *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
7095796c8dcSSimon Schubert   *this_p = value_ptradd (*this_p, adjustment);
7105796c8dcSSimon Schubert   *this_p = value_cast (final_type, *this_p);
7115796c8dcSSimon Schubert 
7125796c8dcSSimon Schubert   if (vbit)
7135796c8dcSSimon Schubert     {
7145796c8dcSSimon Schubert       LONGEST voffset;
715*cf7f2e2dSJohn Marino 
7165796c8dcSSimon Schubert       voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
7175796c8dcSSimon Schubert       return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
7185796c8dcSSimon Schubert 				   method_type, voffset);
7195796c8dcSSimon Schubert     }
7205796c8dcSSimon Schubert   else
7215796c8dcSSimon Schubert     return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
7225796c8dcSSimon Schubert }
7235796c8dcSSimon Schubert 
7245796c8dcSSimon Schubert /* Determine if we are currently in a C++ thunk.  If so, get the address
7255796c8dcSSimon Schubert    of the routine we are thunking to and continue to there instead.  */
7265796c8dcSSimon Schubert 
7275796c8dcSSimon Schubert static CORE_ADDR
7285796c8dcSSimon Schubert gnuv3_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc)
7295796c8dcSSimon Schubert {
7305796c8dcSSimon Schubert   CORE_ADDR real_stop_pc, method_stop_pc;
7315796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (frame);
7325796c8dcSSimon Schubert   struct minimal_symbol *thunk_sym, *fn_sym;
7335796c8dcSSimon Schubert   struct obj_section *section;
7345796c8dcSSimon Schubert   char *thunk_name, *fn_name;
7355796c8dcSSimon Schubert 
7365796c8dcSSimon Schubert   real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
7375796c8dcSSimon Schubert   if (real_stop_pc == 0)
7385796c8dcSSimon Schubert     real_stop_pc = stop_pc;
7395796c8dcSSimon Schubert 
7405796c8dcSSimon Schubert   /* Find the linker symbol for this potential thunk.  */
7415796c8dcSSimon Schubert   thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
7425796c8dcSSimon Schubert   section = find_pc_section (real_stop_pc);
7435796c8dcSSimon Schubert   if (thunk_sym == NULL || section == NULL)
7445796c8dcSSimon Schubert     return 0;
7455796c8dcSSimon Schubert 
7465796c8dcSSimon Schubert   /* The symbol's demangled name should be something like "virtual
7475796c8dcSSimon Schubert      thunk to FUNCTION", where FUNCTION is the name of the function
7485796c8dcSSimon Schubert      being thunked to.  */
7495796c8dcSSimon Schubert   thunk_name = SYMBOL_DEMANGLED_NAME (thunk_sym);
7505796c8dcSSimon Schubert   if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
7515796c8dcSSimon Schubert     return 0;
7525796c8dcSSimon Schubert 
7535796c8dcSSimon Schubert   fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
7545796c8dcSSimon Schubert   fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile);
7555796c8dcSSimon Schubert   if (fn_sym == NULL)
7565796c8dcSSimon Schubert     return 0;
7575796c8dcSSimon Schubert 
7585796c8dcSSimon Schubert   method_stop_pc = SYMBOL_VALUE_ADDRESS (fn_sym);
7595796c8dcSSimon Schubert   real_stop_pc = gdbarch_skip_trampoline_code
7605796c8dcSSimon Schubert 		   (gdbarch, frame, method_stop_pc);
7615796c8dcSSimon Schubert   if (real_stop_pc == 0)
7625796c8dcSSimon Schubert     real_stop_pc = method_stop_pc;
7635796c8dcSSimon Schubert 
7645796c8dcSSimon Schubert   return real_stop_pc;
7655796c8dcSSimon Schubert }
7665796c8dcSSimon Schubert 
7675796c8dcSSimon Schubert /* Return nonzero if a type should be passed by reference.
7685796c8dcSSimon Schubert 
7695796c8dcSSimon Schubert    The rule in the v3 ABI document comes from section 3.1.1.  If the
7705796c8dcSSimon Schubert    type has a non-trivial copy constructor or destructor, then the
7715796c8dcSSimon Schubert    caller must make a copy (by calling the copy constructor if there
7725796c8dcSSimon Schubert    is one or perform the copy itself otherwise), pass the address of
7735796c8dcSSimon Schubert    the copy, and then destroy the temporary (if necessary).
7745796c8dcSSimon Schubert 
7755796c8dcSSimon Schubert    For return values with non-trivial copy constructors or
7765796c8dcSSimon Schubert    destructors, space will be allocated in the caller, and a pointer
7775796c8dcSSimon Schubert    will be passed as the first argument (preceding "this").
7785796c8dcSSimon Schubert 
7795796c8dcSSimon Schubert    We don't have a bulletproof mechanism for determining whether a
7805796c8dcSSimon Schubert    constructor or destructor is trivial.  For GCC and DWARF2 debug
7815796c8dcSSimon Schubert    information, we can check the artificial flag.
7825796c8dcSSimon Schubert 
7835796c8dcSSimon Schubert    We don't do anything with the constructors or destructors,
7845796c8dcSSimon Schubert    but we have to get the argument passing right anyway.  */
7855796c8dcSSimon Schubert static int
7865796c8dcSSimon Schubert gnuv3_pass_by_reference (struct type *type)
7875796c8dcSSimon Schubert {
7885796c8dcSSimon Schubert   int fieldnum, fieldelem;
7895796c8dcSSimon Schubert 
7905796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
7915796c8dcSSimon Schubert 
7925796c8dcSSimon Schubert   /* We're only interested in things that can have methods.  */
7935796c8dcSSimon Schubert   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
7945796c8dcSSimon Schubert       && TYPE_CODE (type) != TYPE_CODE_CLASS
7955796c8dcSSimon Schubert       && TYPE_CODE (type) != TYPE_CODE_UNION)
7965796c8dcSSimon Schubert     return 0;
7975796c8dcSSimon Schubert 
7985796c8dcSSimon Schubert   for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
7995796c8dcSSimon Schubert     for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
8005796c8dcSSimon Schubert 	 fieldelem++)
8015796c8dcSSimon Schubert       {
8025796c8dcSSimon Schubert 	struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
8035796c8dcSSimon Schubert 	char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
8045796c8dcSSimon Schubert 	struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
8055796c8dcSSimon Schubert 
8065796c8dcSSimon Schubert 	/* If this function is marked as artificial, it is compiler-generated,
8075796c8dcSSimon Schubert 	   and we assume it is trivial.  */
8085796c8dcSSimon Schubert 	if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
8095796c8dcSSimon Schubert 	  continue;
8105796c8dcSSimon Schubert 
8115796c8dcSSimon Schubert 	/* If we've found a destructor, we must pass this by reference.  */
8125796c8dcSSimon Schubert 	if (name[0] == '~')
8135796c8dcSSimon Schubert 	  return 1;
8145796c8dcSSimon Schubert 
8155796c8dcSSimon Schubert 	/* If the mangled name of this method doesn't indicate that it
8165796c8dcSSimon Schubert 	   is a constructor, we're not interested.
8175796c8dcSSimon Schubert 
8185796c8dcSSimon Schubert 	   FIXME drow/2007-09-23: We could do this using the name of
8195796c8dcSSimon Schubert 	   the method and the name of the class instead of dealing
8205796c8dcSSimon Schubert 	   with the mangled name.  We don't have a convenient function
8215796c8dcSSimon Schubert 	   to strip off both leading scope qualifiers and trailing
8225796c8dcSSimon Schubert 	   template arguments yet.  */
8235796c8dcSSimon Schubert 	if (!is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem)))
8245796c8dcSSimon Schubert 	  continue;
8255796c8dcSSimon Schubert 
8265796c8dcSSimon Schubert 	/* If this method takes two arguments, and the second argument is
8275796c8dcSSimon Schubert 	   a reference to this class, then it is a copy constructor.  */
8285796c8dcSSimon Schubert 	if (TYPE_NFIELDS (fieldtype) == 2
8295796c8dcSSimon Schubert 	    && TYPE_CODE (TYPE_FIELD_TYPE (fieldtype, 1)) == TYPE_CODE_REF
8305796c8dcSSimon Schubert 	    && check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (fieldtype, 1))) == type)
8315796c8dcSSimon Schubert 	  return 1;
8325796c8dcSSimon Schubert       }
8335796c8dcSSimon Schubert 
8345796c8dcSSimon Schubert   /* Even if all the constructors and destructors were artificial, one
8355796c8dcSSimon Schubert      of them may have invoked a non-artificial constructor or
8365796c8dcSSimon Schubert      destructor in a base class.  If any base class needs to be passed
8375796c8dcSSimon Schubert      by reference, so does this class.  Similarly for members, which
8385796c8dcSSimon Schubert      are constructed whenever this class is.  We do not need to worry
8395796c8dcSSimon Schubert      about recursive loops here, since we are only looking at members
8405796c8dcSSimon Schubert      of complete class type.  */
8415796c8dcSSimon Schubert   for (fieldnum = 0; fieldnum < TYPE_NFIELDS (type); fieldnum++)
8425796c8dcSSimon Schubert     if (gnuv3_pass_by_reference (TYPE_FIELD_TYPE (type, fieldnum)))
8435796c8dcSSimon Schubert       return 1;
8445796c8dcSSimon Schubert 
8455796c8dcSSimon Schubert   return 0;
8465796c8dcSSimon Schubert }
8475796c8dcSSimon Schubert 
8485796c8dcSSimon Schubert static void
8495796c8dcSSimon Schubert init_gnuv3_ops (void)
8505796c8dcSSimon Schubert {
8515796c8dcSSimon Schubert   vtable_type_gdbarch_data = gdbarch_data_register_post_init (build_gdb_vtable_type);
8525796c8dcSSimon Schubert 
8535796c8dcSSimon Schubert   gnu_v3_abi_ops.shortname = "gnu-v3";
8545796c8dcSSimon Schubert   gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
8555796c8dcSSimon Schubert   gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
8565796c8dcSSimon Schubert   gnu_v3_abi_ops.is_destructor_name =
8575796c8dcSSimon Schubert     (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
8585796c8dcSSimon Schubert   gnu_v3_abi_ops.is_constructor_name =
8595796c8dcSSimon Schubert     (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
8605796c8dcSSimon Schubert   gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
8615796c8dcSSimon Schubert   gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
8625796c8dcSSimon Schubert   gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
8635796c8dcSSimon Schubert   gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
8645796c8dcSSimon Schubert   gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
8655796c8dcSSimon Schubert   gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
8665796c8dcSSimon Schubert   gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
8675796c8dcSSimon Schubert   gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
8685796c8dcSSimon Schubert   gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
8695796c8dcSSimon Schubert   gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
8705796c8dcSSimon Schubert   gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
8715796c8dcSSimon Schubert }
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */
8745796c8dcSSimon Schubert 
8755796c8dcSSimon Schubert void
8765796c8dcSSimon Schubert _initialize_gnu_v3_abi (void)
8775796c8dcSSimon Schubert {
8785796c8dcSSimon Schubert   init_gnuv3_ops ();
8795796c8dcSSimon Schubert 
8805796c8dcSSimon Schubert   register_cp_abi (&gnu_v3_abi_ops);
8815796c8dcSSimon Schubert }
882