GDB (API)
/home/stan/gdb/src/gdb/gnu-v3-abi.c
Go to the documentation of this file.
00001 /* Abstraction of GNU v3 abi.
00002    Contributed by Jim Blandy <jimb@redhat.com>
00003 
00004    Copyright (C) 2001-2013 Free Software Foundation, Inc.
00005 
00006    This file is part of GDB.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 3 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00020 
00021 #include "defs.h"
00022 #include "value.h"
00023 #include "cp-abi.h"
00024 #include "cp-support.h"
00025 #include "demangle.h"
00026 #include "objfiles.h"
00027 #include "valprint.h"
00028 #include "c-lang.h"
00029 #include "exceptions.h"
00030 #include "typeprint.h"
00031 
00032 #include "gdb_assert.h"
00033 #include "gdb_string.h"
00034 
00035 static struct cp_abi_ops gnu_v3_abi_ops;
00036 
00037 /* A gdbarch key for std::type_info, in the event that it can't be
00038    found in the debug info.  */
00039 
00040 static struct gdbarch_data *std_type_info_gdbarch_data;
00041 
00042 
00043 static int
00044 gnuv3_is_vtable_name (const char *name)
00045 {
00046   return strncmp (name, "_ZTV", 4) == 0;
00047 }
00048 
00049 static int
00050 gnuv3_is_operator_name (const char *name)
00051 {
00052   return strncmp (name, "operator", 8) == 0;
00053 }
00054 
00055 
00056 /* To help us find the components of a vtable, we build ourselves a
00057    GDB type object representing the vtable structure.  Following the
00058    V3 ABI, it goes something like this:
00059 
00060    struct gdb_gnu_v3_abi_vtable {
00061 
00062      / * An array of virtual call and virtual base offsets.  The real
00063          length of this array depends on the class hierarchy; we use
00064          negative subscripts to access the elements.  Yucky, but
00065          better than the alternatives.  * /
00066      ptrdiff_t vcall_and_vbase_offsets[0];
00067 
00068      / * The offset from a virtual pointer referring to this table
00069          to the top of the complete object.  * /
00070      ptrdiff_t offset_to_top;
00071 
00072      / * The type_info pointer for this class.  This is really a
00073          std::type_info *, but GDB doesn't really look at the
00074          type_info object itself, so we don't bother to get the type
00075          exactly right.  * /
00076      void *type_info;
00077 
00078      / * Virtual table pointers in objects point here.  * /
00079 
00080      / * Virtual function pointers.  Like the vcall/vbase array, the
00081          real length of this table depends on the class hierarchy.  * /
00082      void (*virtual_functions[0]) ();
00083 
00084    };
00085 
00086    The catch, of course, is that the exact layout of this table
00087    depends on the ABI --- word size, endianness, alignment, etc.  So
00088    the GDB type object is actually a per-architecture kind of thing.
00089 
00090    vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
00091    which refers to the struct type * for this structure, laid out
00092    appropriately for the architecture.  */
00093 static struct gdbarch_data *vtable_type_gdbarch_data;
00094 
00095 
00096 /* Human-readable names for the numbers of the fields above.  */
00097 enum {
00098   vtable_field_vcall_and_vbase_offsets,
00099   vtable_field_offset_to_top,
00100   vtable_field_type_info,
00101   vtable_field_virtual_functions
00102 };
00103 
00104 
00105 /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
00106    described above, laid out appropriately for ARCH.
00107 
00108    We use this function as the gdbarch per-architecture data
00109    initialization function.  */
00110 static void *
00111 build_gdb_vtable_type (struct gdbarch *arch)
00112 {
00113   struct type *t;
00114   struct field *field_list, *field;
00115   int offset;
00116 
00117   struct type *void_ptr_type
00118     = builtin_type (arch)->builtin_data_ptr;
00119   struct type *ptr_to_void_fn_type
00120     = builtin_type (arch)->builtin_func_ptr;
00121 
00122   /* ARCH can't give us the true ptrdiff_t type, so we guess.  */
00123   struct type *ptrdiff_type
00124     = arch_integer_type (arch, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
00125 
00126   /* We assume no padding is necessary, since GDB doesn't know
00127      anything about alignment at the moment.  If this assumption bites
00128      us, we should add a gdbarch method which, given a type, returns
00129      the alignment that type requires, and then use that here.  */
00130 
00131   /* Build the field list.  */
00132   field_list = xmalloc (sizeof (struct field [4]));
00133   memset (field_list, 0, sizeof (struct field [4]));
00134   field = &field_list[0];
00135   offset = 0;
00136 
00137   /* ptrdiff_t vcall_and_vbase_offsets[0]; */
00138   FIELD_NAME (*field) = "vcall_and_vbase_offsets";
00139   FIELD_TYPE (*field) = lookup_array_range_type (ptrdiff_type, 0, -1);
00140   SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
00141   offset += TYPE_LENGTH (FIELD_TYPE (*field));
00142   field++;
00143 
00144   /* ptrdiff_t offset_to_top; */
00145   FIELD_NAME (*field) = "offset_to_top";
00146   FIELD_TYPE (*field) = ptrdiff_type;
00147   SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
00148   offset += TYPE_LENGTH (FIELD_TYPE (*field));
00149   field++;
00150 
00151   /* void *type_info; */
00152   FIELD_NAME (*field) = "type_info";
00153   FIELD_TYPE (*field) = void_ptr_type;
00154   SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
00155   offset += TYPE_LENGTH (FIELD_TYPE (*field));
00156   field++;
00157 
00158   /* void (*virtual_functions[0]) (); */
00159   FIELD_NAME (*field) = "virtual_functions";
00160   FIELD_TYPE (*field) = lookup_array_range_type (ptr_to_void_fn_type, 0, -1);
00161   SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
00162   offset += TYPE_LENGTH (FIELD_TYPE (*field));
00163   field++;
00164 
00165   /* We assumed in the allocation above that there were four fields.  */
00166   gdb_assert (field == (field_list + 4));
00167 
00168   t = arch_type (arch, TYPE_CODE_STRUCT, offset, NULL);
00169   TYPE_NFIELDS (t) = field - field_list;
00170   TYPE_FIELDS (t) = field_list;
00171   TYPE_TAG_NAME (t) = "gdb_gnu_v3_abi_vtable";
00172   INIT_CPLUS_SPECIFIC (t);
00173 
00174   return t;
00175 }
00176 
00177 
00178 /* Return the ptrdiff_t type used in the vtable type.  */
00179 static struct type *
00180 vtable_ptrdiff_type (struct gdbarch *gdbarch)
00181 {
00182   struct type *vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data);
00183 
00184   /* The "offset_to_top" field has the appropriate (ptrdiff_t) type.  */
00185   return TYPE_FIELD_TYPE (vtable_type, vtable_field_offset_to_top);
00186 }
00187 
00188 /* Return the offset from the start of the imaginary `struct
00189    gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
00190    (i.e., where objects' virtual table pointers point).  */
00191 static int
00192 vtable_address_point_offset (struct gdbarch *gdbarch)
00193 {
00194   struct type *vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data);
00195 
00196   return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions)
00197           / TARGET_CHAR_BIT);
00198 }
00199 
00200 
00201 /* Determine whether structure TYPE is a dynamic class.  Cache the
00202    result.  */
00203 
00204 static int
00205 gnuv3_dynamic_class (struct type *type)
00206 {
00207   int fieldnum, fieldelem;
00208 
00209   if (TYPE_CPLUS_DYNAMIC (type))
00210     return TYPE_CPLUS_DYNAMIC (type) == 1;
00211 
00212   ALLOCATE_CPLUS_STRUCT_TYPE (type);
00213 
00214   for (fieldnum = 0; fieldnum < TYPE_N_BASECLASSES (type); fieldnum++)
00215     if (BASETYPE_VIA_VIRTUAL (type, fieldnum)
00216         || gnuv3_dynamic_class (TYPE_FIELD_TYPE (type, fieldnum)))
00217       {
00218         TYPE_CPLUS_DYNAMIC (type) = 1;
00219         return 1;
00220       }
00221 
00222   for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
00223     for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
00224          fieldelem++)
00225       {
00226         struct fn_field *f = TYPE_FN_FIELDLIST1 (type, fieldnum);
00227 
00228         if (TYPE_FN_FIELD_VIRTUAL_P (f, fieldelem))
00229           {
00230             TYPE_CPLUS_DYNAMIC (type) = 1;
00231             return 1;
00232           }
00233       }
00234 
00235   TYPE_CPLUS_DYNAMIC (type) = -1;
00236   return 0;
00237 }
00238 
00239 /* Find the vtable for a value of CONTAINER_TYPE located at
00240    CONTAINER_ADDR.  Return a value of the correct vtable type for this
00241    architecture, or NULL if CONTAINER does not have a vtable.  */
00242 
00243 static struct value *
00244 gnuv3_get_vtable (struct gdbarch *gdbarch,
00245                   struct type *container_type, CORE_ADDR container_addr)
00246 {
00247   struct type *vtable_type = gdbarch_data (gdbarch,
00248                                            vtable_type_gdbarch_data);
00249   struct type *vtable_pointer_type;
00250   struct value *vtable_pointer;
00251   CORE_ADDR vtable_address;
00252 
00253   /* If this type does not have a virtual table, don't read the first
00254      field.  */
00255   if (!gnuv3_dynamic_class (check_typedef (container_type)))
00256     return NULL;
00257 
00258   /* We do not consult the debug information to find the virtual table.
00259      The ABI specifies that it is always at offset zero in any class,
00260      and debug information may not represent it.
00261 
00262      We avoid using value_contents on principle, because the object might
00263      be large.  */
00264 
00265   /* Find the type "pointer to virtual table".  */
00266   vtable_pointer_type = lookup_pointer_type (vtable_type);
00267 
00268   /* Load it from the start of the class.  */
00269   vtable_pointer = value_at (vtable_pointer_type, container_addr);
00270   vtable_address = value_as_address (vtable_pointer);
00271 
00272   /* Correct it to point at the start of the virtual table, rather
00273      than the address point.  */
00274   return value_at_lazy (vtable_type,
00275                         vtable_address
00276                         - vtable_address_point_offset (gdbarch));
00277 }
00278 
00279 
00280 static struct type *
00281 gnuv3_rtti_type (struct value *value,
00282                  int *full_p, int *top_p, int *using_enc_p)
00283 {
00284   struct gdbarch *gdbarch;
00285   struct type *values_type = check_typedef (value_type (value));
00286   struct value *vtable;
00287   struct minimal_symbol *vtable_symbol;
00288   const char *vtable_symbol_name;
00289   const char *class_name;
00290   struct type *run_time_type;
00291   LONGEST offset_to_top;
00292   char *atsign;
00293 
00294   /* We only have RTTI for class objects.  */
00295   if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
00296     return NULL;
00297 
00298   /* Java doesn't have RTTI following the C++ ABI.  */
00299   if (TYPE_CPLUS_REALLY_JAVA (values_type))
00300     return NULL;
00301 
00302   /* Determine architecture.  */
00303   gdbarch = get_type_arch (values_type);
00304 
00305   if (using_enc_p)
00306     *using_enc_p = 0;
00307 
00308   vtable = gnuv3_get_vtable (gdbarch, value_type (value),
00309                              value_as_address (value_addr (value)));
00310   if (vtable == NULL)
00311     return NULL;
00312 
00313   /* Find the linker symbol for this vtable.  */
00314   vtable_symbol
00315     = lookup_minimal_symbol_by_pc (value_address (vtable)
00316                                    + value_embedded_offset (vtable)).minsym;
00317   if (! vtable_symbol)
00318     return NULL;
00319   
00320   /* The symbol's demangled name should be something like "vtable for
00321      CLASS", where CLASS is the name of the run-time type of VALUE.
00322      If we didn't like this approach, we could instead look in the
00323      type_info object itself to get the class name.  But this way
00324      should work just as well, and doesn't read target memory.  */
00325   vtable_symbol_name = SYMBOL_DEMANGLED_NAME (vtable_symbol);
00326   if (vtable_symbol_name == NULL
00327       || strncmp (vtable_symbol_name, "vtable for ", 11))
00328     {
00329       warning (_("can't find linker symbol for virtual table for `%s' value"),
00330                TYPE_SAFE_NAME (values_type));
00331       if (vtable_symbol_name)
00332         warning (_("  found `%s' instead"), vtable_symbol_name);
00333       return NULL;
00334     }
00335   class_name = vtable_symbol_name + 11;
00336 
00337   /* Strip off @plt and version suffixes.  */
00338   atsign = strchr (class_name, '@');
00339   if (atsign != NULL)
00340     {
00341       char *copy;
00342 
00343       copy = alloca (atsign - class_name + 1);
00344       memcpy (copy, class_name, atsign - class_name);
00345       copy[atsign - class_name] = '\0';
00346       class_name = copy;
00347     }
00348 
00349   /* Try to look up the class name as a type name.  */
00350   /* FIXME: chastain/2003-11-26: block=NULL is bogus.  See pr gdb/1465.  */
00351   run_time_type = cp_lookup_rtti_type (class_name, NULL);
00352   if (run_time_type == NULL)
00353     return NULL;
00354 
00355   /* Get the offset from VALUE to the top of the complete object.
00356      NOTE: this is the reverse of the meaning of *TOP_P.  */
00357   offset_to_top
00358     = value_as_long (value_field (vtable, vtable_field_offset_to_top));
00359 
00360   if (full_p)
00361     *full_p = (- offset_to_top == value_embedded_offset (value)
00362                && (TYPE_LENGTH (value_enclosing_type (value))
00363                    >= TYPE_LENGTH (run_time_type)));
00364   if (top_p)
00365     *top_p = - offset_to_top;
00366   return run_time_type;
00367 }
00368 
00369 /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
00370    function, of type FNTYPE.  */
00371 
00372 static struct value *
00373 gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
00374                       struct type *fntype, int vtable_index)
00375 {
00376   struct value *vtable, *vfn;
00377 
00378   /* Every class with virtual functions must have a vtable.  */
00379   vtable = gnuv3_get_vtable (gdbarch, value_type (container),
00380                              value_as_address (value_addr (container)));
00381   gdb_assert (vtable != NULL);
00382 
00383   /* Fetch the appropriate function pointer from the vtable.  */
00384   vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
00385                          vtable_index);
00386 
00387   /* If this architecture uses function descriptors directly in the vtable,
00388      then the address of the vtable entry is actually a "function pointer"
00389      (i.e. points to the descriptor).  We don't need to scale the index
00390      by the size of a function descriptor; GCC does that before outputing
00391      debug information.  */
00392   if (gdbarch_vtable_function_descriptors (gdbarch))
00393     vfn = value_addr (vfn);
00394 
00395   /* Cast the function pointer to the appropriate type.  */
00396   vfn = value_cast (lookup_pointer_type (fntype), vfn);
00397 
00398   return vfn;
00399 }
00400 
00401 /* GNU v3 implementation of value_virtual_fn_field.  See cp-abi.h
00402    for a description of the arguments.  */
00403 
00404 static struct value *
00405 gnuv3_virtual_fn_field (struct value **value_p,
00406                         struct fn_field *f, int j,
00407                         struct type *vfn_base, int offset)
00408 {
00409   struct type *values_type = check_typedef (value_type (*value_p));
00410   struct gdbarch *gdbarch;
00411 
00412   /* Some simple sanity checks.  */
00413   if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
00414     error (_("Only classes can have virtual functions."));
00415 
00416   /* Determine architecture.  */
00417   gdbarch = get_type_arch (values_type);
00418 
00419   /* Cast our value to the base class which defines this virtual
00420      function.  This takes care of any necessary `this'
00421      adjustments.  */
00422   if (vfn_base != values_type)
00423     *value_p = value_cast (vfn_base, *value_p);
00424 
00425   return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
00426                                TYPE_FN_FIELD_VOFFSET (f, j));
00427 }
00428 
00429 /* Compute the offset of the baseclass which is
00430    the INDEXth baseclass of class TYPE,
00431    for value at VALADDR (in host) at ADDRESS (in target).
00432    The result is the offset of the baseclass value relative
00433    to (the address of)(ARG) + OFFSET.
00434 
00435    -1 is returned on error.  */
00436 
00437 static int
00438 gnuv3_baseclass_offset (struct type *type, int index,
00439                         const bfd_byte *valaddr, int embedded_offset,
00440                         CORE_ADDR address, const struct value *val)
00441 {
00442   struct gdbarch *gdbarch;
00443   struct type *ptr_type;
00444   struct value *vtable;
00445   struct value *vbase_array;
00446   long int cur_base_offset, base_offset;
00447 
00448   /* Determine architecture.  */
00449   gdbarch = get_type_arch (type);
00450   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
00451 
00452   /* If it isn't a virtual base, this is easy.  The offset is in the
00453      type definition.  Likewise for Java, which doesn't really have
00454      virtual inheritance in the C++ sense.  */
00455   if (!BASETYPE_VIA_VIRTUAL (type, index) || TYPE_CPLUS_REALLY_JAVA (type))
00456     return TYPE_BASECLASS_BITPOS (type, index) / 8;
00457 
00458   /* To access a virtual base, we need to use the vbase offset stored in
00459      our vtable.  Recent GCC versions provide this information.  If it isn't
00460      available, we could get what we needed from RTTI, or from drawing the
00461      complete inheritance graph based on the debug info.  Neither is
00462      worthwhile.  */
00463   cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
00464   if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
00465     error (_("Expected a negative vbase offset (old compiler?)"));
00466 
00467   cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
00468   if ((- cur_base_offset) % TYPE_LENGTH (ptr_type) != 0)
00469     error (_("Misaligned vbase offset."));
00470   cur_base_offset = cur_base_offset / ((int) TYPE_LENGTH (ptr_type));
00471 
00472   vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset);
00473   gdb_assert (vtable != NULL);
00474   vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
00475   base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
00476   return base_offset;
00477 }
00478 
00479 /* Locate a virtual method in DOMAIN or its non-virtual base classes
00480    which has virtual table index VOFFSET.  The method has an associated
00481    "this" adjustment of ADJUSTMENT bytes.  */
00482 
00483 static const char *
00484 gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
00485                       LONGEST adjustment)
00486 {
00487   int i;
00488 
00489   /* Search this class first.  */
00490   if (adjustment == 0)
00491     {
00492       int len;
00493 
00494       len = TYPE_NFN_FIELDS (domain);
00495       for (i = 0; i < len; i++)
00496         {
00497           int len2, j;
00498           struct fn_field *f;
00499 
00500           f = TYPE_FN_FIELDLIST1 (domain, i);
00501           len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
00502 
00503           check_stub_method_group (domain, i);
00504           for (j = 0; j < len2; j++)
00505             if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset)
00506               return TYPE_FN_FIELD_PHYSNAME (f, j);
00507         }
00508     }
00509 
00510   /* Next search non-virtual bases.  If it's in a virtual base,
00511      we're out of luck.  */
00512   for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
00513     {
00514       int pos;
00515       struct type *basetype;
00516 
00517       if (BASETYPE_VIA_VIRTUAL (domain, i))
00518         continue;
00519 
00520       pos = TYPE_BASECLASS_BITPOS (domain, i) / 8;
00521       basetype = TYPE_FIELD_TYPE (domain, i);
00522       /* Recurse with a modified adjustment.  We don't need to adjust
00523          voffset.  */
00524       if (adjustment >= pos && adjustment < pos + TYPE_LENGTH (basetype))
00525         return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
00526     }
00527 
00528   return NULL;
00529 }
00530 
00531 /* Decode GNU v3 method pointer.  */
00532 
00533 static int
00534 gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
00535                          const gdb_byte *contents,
00536                          CORE_ADDR *value_p,
00537                          LONGEST *adjustment_p)
00538 {
00539   struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
00540   struct type *offset_type = vtable_ptrdiff_type (gdbarch);
00541   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00542   CORE_ADDR ptr_value;
00543   LONGEST voffset, adjustment;
00544   int vbit;
00545 
00546   /* Extract the pointer to member.  The first element is either a pointer
00547      or a vtable offset.  For pointers, we need to use extract_typed_address
00548      to allow the back-end to convert the pointer to a GDB address -- but
00549      vtable offsets we must handle as integers.  At this point, we do not
00550      yet know which case we have, so we extract the value under both
00551      interpretations and choose the right one later on.  */
00552   ptr_value = extract_typed_address (contents, funcptr_type);
00553   voffset = extract_signed_integer (contents,
00554                                     TYPE_LENGTH (funcptr_type), byte_order);
00555   contents += TYPE_LENGTH (funcptr_type);
00556   adjustment = extract_signed_integer (contents,
00557                                        TYPE_LENGTH (offset_type), byte_order);
00558 
00559   if (!gdbarch_vbit_in_delta (gdbarch))
00560     {
00561       vbit = voffset & 1;
00562       voffset = voffset ^ vbit;
00563     }
00564   else
00565     {
00566       vbit = adjustment & 1;
00567       adjustment = adjustment >> 1;
00568     }
00569 
00570   *value_p = vbit? voffset : ptr_value;
00571   *adjustment_p = adjustment;
00572   return vbit;
00573 }
00574 
00575 /* GNU v3 implementation of cplus_print_method_ptr.  */
00576 
00577 static void
00578 gnuv3_print_method_ptr (const gdb_byte *contents,
00579                         struct type *type,
00580                         struct ui_file *stream)
00581 {
00582   struct type *domain = TYPE_DOMAIN_TYPE (type);
00583   struct gdbarch *gdbarch = get_type_arch (domain);
00584   CORE_ADDR ptr_value;
00585   LONGEST adjustment;
00586   int vbit;
00587 
00588   /* Extract the pointer to member.  */
00589   vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
00590 
00591   /* Check for NULL.  */
00592   if (ptr_value == 0 && vbit == 0)
00593     {
00594       fprintf_filtered (stream, "NULL");
00595       return;
00596     }
00597 
00598   /* Search for a virtual method.  */
00599   if (vbit)
00600     {
00601       CORE_ADDR voffset;
00602       const char *physname;
00603 
00604       /* It's a virtual table offset, maybe in this class.  Search
00605          for a field with the correct vtable offset.  First convert it
00606          to an index, as used in TYPE_FN_FIELD_VOFFSET.  */
00607       voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
00608 
00609       physname = gnuv3_find_method_in (domain, voffset, adjustment);
00610 
00611       /* If we found a method, print that.  We don't bother to disambiguate
00612          possible paths to the method based on the adjustment.  */
00613       if (physname)
00614         {
00615           char *demangled_name = gdb_demangle (physname,
00616                                                DMGL_ANSI | DMGL_PARAMS);
00617 
00618           fprintf_filtered (stream, "&virtual ");
00619           if (demangled_name == NULL)
00620             fputs_filtered (physname, stream);
00621           else
00622             {
00623               fputs_filtered (demangled_name, stream);
00624               xfree (demangled_name);
00625             }
00626           return;
00627         }
00628     }
00629   else if (ptr_value != 0)
00630     {
00631       /* Found a non-virtual function: print out the type.  */
00632       fputs_filtered ("(", stream);
00633       c_print_type (type, "", stream, -1, 0, &type_print_raw_options);
00634       fputs_filtered (") ", stream);
00635     }
00636 
00637   /* We didn't find it; print the raw data.  */
00638   if (vbit)
00639     {
00640       fprintf_filtered (stream, "&virtual table offset ");
00641       print_longest (stream, 'd', 1, ptr_value);
00642     }
00643   else
00644     {
00645       struct value_print_options opts;
00646 
00647       get_user_print_options (&opts);
00648       print_address_demangle (&opts, gdbarch, ptr_value, stream, demangle);
00649     }
00650 
00651   if (adjustment)
00652     {
00653       fprintf_filtered (stream, ", this adjustment ");
00654       print_longest (stream, 'd', 1, adjustment);
00655     }
00656 }
00657 
00658 /* GNU v3 implementation of cplus_method_ptr_size.  */
00659 
00660 static int
00661 gnuv3_method_ptr_size (struct type *type)
00662 {
00663   struct gdbarch *gdbarch = get_type_arch (type);
00664 
00665   return 2 * TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
00666 }
00667 
00668 /* GNU v3 implementation of cplus_make_method_ptr.  */
00669 
00670 static void
00671 gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
00672                        CORE_ADDR value, int is_virtual)
00673 {
00674   struct gdbarch *gdbarch = get_type_arch (type);
00675   int size = TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
00676   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00677 
00678   /* FIXME drow/2006-12-24: The adjustment of "this" is currently
00679      always zero, since the method pointer is of the correct type.
00680      But if the method pointer came from a base class, this is
00681      incorrect - it should be the offset to the base.  The best
00682      fix might be to create the pointer to member pointing at the
00683      base class and cast it to the derived class, but that requires
00684      support for adjusting pointers to members when casting them -
00685      not currently supported by GDB.  */
00686 
00687   if (!gdbarch_vbit_in_delta (gdbarch))
00688     {
00689       store_unsigned_integer (contents, size, byte_order, value | is_virtual);
00690       store_unsigned_integer (contents + size, size, byte_order, 0);
00691     }
00692   else
00693     {
00694       store_unsigned_integer (contents, size, byte_order, value);
00695       store_unsigned_integer (contents + size, size, byte_order, is_virtual);
00696     }
00697 }
00698 
00699 /* GNU v3 implementation of cplus_method_ptr_to_value.  */
00700 
00701 static struct value *
00702 gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
00703 {
00704   struct gdbarch *gdbarch;
00705   const gdb_byte *contents = value_contents (method_ptr);
00706   CORE_ADDR ptr_value;
00707   struct type *domain_type, *final_type, *method_type;
00708   LONGEST adjustment;
00709   int vbit;
00710 
00711   domain_type = TYPE_DOMAIN_TYPE (check_typedef (value_type (method_ptr)));
00712   final_type = lookup_pointer_type (domain_type);
00713 
00714   method_type = TYPE_TARGET_TYPE (check_typedef (value_type (method_ptr)));
00715 
00716   /* Extract the pointer to member.  */
00717   gdbarch = get_type_arch (domain_type);
00718   vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
00719 
00720   /* First convert THIS to match the containing type of the pointer to
00721      member.  This cast may adjust the value of THIS.  */
00722   *this_p = value_cast (final_type, *this_p);
00723 
00724   /* Then apply whatever adjustment is necessary.  This creates a somewhat
00725      strange pointer: it claims to have type FINAL_TYPE, but in fact it
00726      might not be a valid FINAL_TYPE.  For instance, it might be a
00727      base class of FINAL_TYPE.  And if it's not the primary base class,
00728      then printing it out as a FINAL_TYPE object would produce some pretty
00729      garbage.
00730 
00731      But we don't really know the type of the first argument in
00732      METHOD_TYPE either, which is why this happens.  We can't
00733      dereference this later as a FINAL_TYPE, but once we arrive in the
00734      called method we'll have debugging information for the type of
00735      "this" - and that'll match the value we produce here.
00736 
00737      You can provoke this case by casting a Base::* to a Derived::*, for
00738      instance.  */
00739   *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
00740   *this_p = value_ptradd (*this_p, adjustment);
00741   *this_p = value_cast (final_type, *this_p);
00742 
00743   if (vbit)
00744     {
00745       LONGEST voffset;
00746 
00747       voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
00748       return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
00749                                    method_type, voffset);
00750     }
00751   else
00752     return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
00753 }
00754 
00755 /* Objects of this type are stored in a hash table and a vector when
00756    printing the vtables for a class.  */
00757 
00758 struct value_and_voffset
00759 {
00760   /* The value representing the object.  */
00761   struct value *value;
00762 
00763   /* The maximum vtable offset we've found for any object at this
00764      offset in the outermost object.  */
00765   int max_voffset;
00766 };
00767 
00768 typedef struct value_and_voffset *value_and_voffset_p;
00769 DEF_VEC_P (value_and_voffset_p);
00770 
00771 /* Hash function for value_and_voffset.  */
00772 
00773 static hashval_t
00774 hash_value_and_voffset (const void *p)
00775 {
00776   const struct value_and_voffset *o = p;
00777 
00778   return value_address (o->value) + value_embedded_offset (o->value);
00779 }
00780 
00781 /* Equality function for value_and_voffset.  */
00782 
00783 static int
00784 eq_value_and_voffset (const void *a, const void *b)
00785 {
00786   const struct value_and_voffset *ova = a;
00787   const struct value_and_voffset *ovb = b;
00788 
00789   return (value_address (ova->value) + value_embedded_offset (ova->value)
00790           == value_address (ovb->value) + value_embedded_offset (ovb->value));
00791 }
00792 
00793 /* qsort comparison function for value_and_voffset.  */
00794 
00795 static int
00796 compare_value_and_voffset (const void *a, const void *b)
00797 {
00798   const struct value_and_voffset * const *ova = a;
00799   CORE_ADDR addra = (value_address ((*ova)->value)
00800                      + value_embedded_offset ((*ova)->value));
00801   const struct value_and_voffset * const *ovb = b;
00802   CORE_ADDR addrb = (value_address ((*ovb)->value)
00803                      + value_embedded_offset ((*ovb)->value));
00804 
00805   if (addra < addrb)
00806     return -1;
00807   if (addra > addrb)
00808     return 1;
00809   return 0;
00810 }
00811 
00812 /* A helper function used when printing vtables.  This determines the
00813    key (most derived) sub-object at each address and also computes the
00814    maximum vtable offset seen for the corresponding vtable.  Updates
00815    OFFSET_HASH and OFFSET_VEC with a new value_and_voffset object, if
00816    needed.  VALUE is the object to examine.  */
00817 
00818 static void
00819 compute_vtable_size (htab_t offset_hash,
00820                      VEC (value_and_voffset_p) **offset_vec,
00821                      struct value *value)
00822 {
00823   int i;
00824   struct type *type = check_typedef (value_type (value));
00825   void **slot;
00826   struct value_and_voffset search_vo, *current_vo;
00827 
00828   /* If the object is not dynamic, then we are done; as it cannot have
00829      dynamic base types either.  */
00830   if (!gnuv3_dynamic_class (type))
00831     return;
00832 
00833   /* Update the hash and the vec, if needed.  */
00834   search_vo.value = value;
00835   slot = htab_find_slot (offset_hash, &search_vo, INSERT);
00836   if (*slot)
00837     current_vo = *slot;
00838   else
00839     {
00840       current_vo = XNEW (struct value_and_voffset);
00841       current_vo->value = value;
00842       current_vo->max_voffset = -1;
00843       *slot = current_vo;
00844       VEC_safe_push (value_and_voffset_p, *offset_vec, current_vo);
00845     }
00846 
00847   /* Update the value_and_voffset object with the highest vtable
00848      offset from this class.  */
00849   for (i = 0; i < TYPE_NFN_FIELDS (type); ++i)
00850     {
00851       int j;
00852       struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, i);
00853 
00854       for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (type, i); ++j)
00855         {
00856           if (TYPE_FN_FIELD_VIRTUAL_P (fn, j))
00857             {
00858               int voffset = TYPE_FN_FIELD_VOFFSET (fn, j);
00859 
00860               if (voffset > current_vo->max_voffset)
00861                 current_vo->max_voffset = voffset;
00862             }
00863         }
00864     }
00865 
00866   /* Recurse into base classes.  */
00867   for (i = 0; i < TYPE_N_BASECLASSES (type); ++i)
00868     compute_vtable_size (offset_hash, offset_vec, value_field (value, i));
00869 }
00870 
00871 /* Helper for gnuv3_print_vtable that prints a single vtable.  */
00872 
00873 static void
00874 print_one_vtable (struct gdbarch *gdbarch, struct value *value,
00875                   int max_voffset,
00876                   struct value_print_options *opts)
00877 {
00878   int i;
00879   struct type *type = check_typedef (value_type (value));
00880   struct value *vtable;
00881   CORE_ADDR vt_addr;
00882 
00883   vtable = gnuv3_get_vtable (gdbarch, type,
00884                              value_address (value)
00885                              + value_embedded_offset (value));
00886   vt_addr = value_address (value_field (vtable,
00887                                         vtable_field_virtual_functions));
00888 
00889   printf_filtered (_("vtable for '%s' @ %s (subobject @ %s):\n"),
00890                    TYPE_SAFE_NAME (type),
00891                    paddress (gdbarch, vt_addr),
00892                    paddress (gdbarch, (value_address (value)
00893                                        + value_embedded_offset (value))));
00894 
00895   for (i = 0; i <= max_voffset; ++i)
00896     {
00897       /* Initialize it just to avoid a GCC false warning.  */
00898       CORE_ADDR addr = 0;
00899       struct value *vfn;
00900       volatile struct gdb_exception ex;
00901 
00902       printf_filtered ("[%d]: ", i);
00903 
00904       vfn = value_subscript (value_field (vtable,
00905                                           vtable_field_virtual_functions),
00906                              i);
00907 
00908       if (gdbarch_vtable_function_descriptors (gdbarch))
00909         vfn = value_addr (vfn);
00910 
00911       TRY_CATCH (ex, RETURN_MASK_ERROR)
00912         {
00913           addr = value_as_address (vfn);
00914         }
00915       if (ex.reason < 0)
00916         printf_filtered (_("<error: %s>"), ex.message);
00917       else
00918         print_function_pointer_address (opts, gdbarch, addr, gdb_stdout);
00919       printf_filtered ("\n");
00920     }
00921 }
00922 
00923 /* Implementation of the print_vtable method.  */
00924 
00925 static void
00926 gnuv3_print_vtable (struct value *value)
00927 {
00928   struct gdbarch *gdbarch;
00929   struct type *type;
00930   struct value *vtable;
00931   struct value_print_options opts;
00932   htab_t offset_hash;
00933   struct cleanup *cleanup;
00934   VEC (value_and_voffset_p) *result_vec = NULL;
00935   struct value_and_voffset *iter;
00936   int i, count;
00937 
00938   value = coerce_ref (value);
00939   type = check_typedef (value_type (value));
00940   if (TYPE_CODE (type) == TYPE_CODE_PTR)
00941     {
00942       value = value_ind (value);
00943       type = check_typedef (value_type (value));
00944     }
00945 
00946   get_user_print_options (&opts);
00947 
00948   /* Respect 'set print object'.  */
00949   if (opts.objectprint)
00950     {
00951       value = value_full_object (value, NULL, 0, 0, 0);
00952       type = check_typedef (value_type (value));
00953     }
00954 
00955   gdbarch = get_type_arch (type);
00956   vtable = gnuv3_get_vtable (gdbarch, type,
00957                              value_as_address (value_addr (value)));
00958 
00959   if (!vtable)
00960     {
00961       printf_filtered (_("This object does not have a virtual function table\n"));
00962       return;
00963     }
00964 
00965   offset_hash = htab_create_alloc (1, hash_value_and_voffset,
00966                                    eq_value_and_voffset,
00967                                    xfree, xcalloc, xfree);
00968   cleanup = make_cleanup_htab_delete (offset_hash);
00969   make_cleanup (VEC_cleanup (value_and_voffset_p), &result_vec);
00970 
00971   compute_vtable_size (offset_hash, &result_vec, value);
00972 
00973   qsort (VEC_address (value_and_voffset_p, result_vec),
00974          VEC_length (value_and_voffset_p, result_vec),
00975          sizeof (value_and_voffset_p),
00976          compare_value_and_voffset);
00977 
00978   count = 0;
00979   for (i = 0; VEC_iterate (value_and_voffset_p, result_vec, i, iter); ++i)
00980     {
00981       if (iter->max_voffset >= 0)
00982         {
00983           if (count > 0)
00984             printf_filtered ("\n");
00985           print_one_vtable (gdbarch, iter->value, iter->max_voffset, &opts);
00986           ++count;
00987         }
00988     }
00989 
00990   do_cleanups (cleanup);
00991 }
00992 
00993 /* Return a GDB type representing `struct std::type_info', laid out
00994    appropriately for ARCH.
00995 
00996    We use this function as the gdbarch per-architecture data
00997    initialization function.  */
00998 
00999 static void *
01000 build_std_type_info_type (struct gdbarch *arch)
01001 {
01002   struct type *t;
01003   struct field *field_list, *field;
01004   int offset;
01005   struct type *void_ptr_type
01006     = builtin_type (arch)->builtin_data_ptr;
01007   struct type *char_type
01008     = builtin_type (arch)->builtin_char;
01009   struct type *char_ptr_type
01010     = make_pointer_type (make_cv_type (1, 0, char_type, NULL), NULL);
01011 
01012   field_list = xmalloc (sizeof (struct field [2]));
01013   memset (field_list, 0, sizeof (struct field [2]));
01014   field = &field_list[0];
01015   offset = 0;
01016 
01017   /* The vtable.  */
01018   FIELD_NAME (*field) = "_vptr.type_info";
01019   FIELD_TYPE (*field) = void_ptr_type;
01020   SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
01021   offset += TYPE_LENGTH (FIELD_TYPE (*field));
01022   field++;
01023 
01024   /* The name.  */
01025   FIELD_NAME (*field) = "__name";
01026   FIELD_TYPE (*field) = char_ptr_type;
01027   SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
01028   offset += TYPE_LENGTH (FIELD_TYPE (*field));
01029   field++;
01030 
01031   gdb_assert (field == (field_list + 2));
01032 
01033   t = arch_type (arch, TYPE_CODE_STRUCT, offset, NULL);
01034   TYPE_NFIELDS (t) = field - field_list;
01035   TYPE_FIELDS (t) = field_list;
01036   TYPE_TAG_NAME (t) = "gdb_gnu_v3_type_info";
01037   INIT_CPLUS_SPECIFIC (t);
01038 
01039   return t;
01040 }
01041 
01042 /* Implement the 'get_typeid_type' method.  */
01043 
01044 static struct type *
01045 gnuv3_get_typeid_type (struct gdbarch *gdbarch)
01046 {
01047   struct symbol *typeinfo;
01048   struct type *typeinfo_type;
01049 
01050   typeinfo = lookup_symbol ("std::type_info", NULL, STRUCT_DOMAIN, NULL);
01051   if (typeinfo == NULL)
01052     typeinfo_type = gdbarch_data (gdbarch, std_type_info_gdbarch_data);
01053   else
01054     typeinfo_type = SYMBOL_TYPE (typeinfo);
01055 
01056   return typeinfo_type;
01057 }
01058 
01059 /* Implement the 'get_typeid' method.  */
01060 
01061 static struct value *
01062 gnuv3_get_typeid (struct value *value)
01063 {
01064   struct type *typeinfo_type;
01065   struct type *type;
01066   struct gdbarch *gdbarch;
01067   struct cleanup *cleanup;
01068   struct value *result;
01069   char *typename, *canonical;
01070 
01071   /* We have to handle values a bit trickily here, to allow this code
01072      to work properly with non_lvalue values that are really just
01073      disguised types.  */
01074   if (value_lval_const (value) == lval_memory)
01075     value = coerce_ref (value);
01076 
01077   type = check_typedef (value_type (value));
01078 
01079   /* In the non_lvalue case, a reference might have slipped through
01080      here.  */
01081   if (TYPE_CODE (type) == TYPE_CODE_REF)
01082     type = check_typedef (TYPE_TARGET_TYPE (type));
01083 
01084   /* Ignore top-level cv-qualifiers.  */
01085   type = make_cv_type (0, 0, type, NULL);
01086   gdbarch = get_type_arch (type);
01087 
01088   typename = type_to_string (type);
01089   if (typename == NULL)
01090     error (_("cannot find typeinfo for unnamed type"));
01091   cleanup = make_cleanup (xfree, typename);
01092 
01093   /* We need to canonicalize the type name here, because we do lookups
01094      using the demangled name, and so we must match the format it
01095      uses.  E.g., GDB tends to use "const char *" as a type name, but
01096      the demangler uses "char const *".  */
01097   canonical = cp_canonicalize_string (typename);
01098   if (canonical != NULL)
01099     {
01100       make_cleanup (xfree, canonical);
01101       typename = canonical;
01102     }
01103 
01104   typeinfo_type = gnuv3_get_typeid_type (gdbarch);
01105 
01106   /* We check for lval_memory because in the "typeid (type-id)" case,
01107      the type is passed via a not_lval value object.  */
01108   if (TYPE_CODE (type) == TYPE_CODE_CLASS
01109       && value_lval_const (value) == lval_memory
01110       && gnuv3_dynamic_class (type))
01111     {
01112       struct value *vtable, *typeinfo_value;
01113       CORE_ADDR address = value_address (value) + value_embedded_offset (value);
01114 
01115       vtable = gnuv3_get_vtable (gdbarch, type, address);
01116       if (vtable == NULL)
01117         error (_("cannot find typeinfo for object of type '%s'"), typename);
01118       typeinfo_value = value_field (vtable, vtable_field_type_info);
01119       result = value_ind (value_cast (make_pointer_type (typeinfo_type, NULL),
01120                                       typeinfo_value));
01121     }
01122   else
01123     {
01124       char *sym_name;
01125       struct minimal_symbol *minsym;
01126 
01127       sym_name = concat ("typeinfo for ", typename, (char *) NULL);
01128       make_cleanup (xfree, sym_name);
01129       minsym = lookup_minimal_symbol (sym_name, NULL, NULL);
01130 
01131       if (minsym == NULL)
01132         error (_("could not find typeinfo symbol for '%s'"), typename);
01133 
01134       result = value_at_lazy (typeinfo_type, SYMBOL_VALUE_ADDRESS (minsym));
01135     }
01136 
01137   do_cleanups (cleanup);
01138   return result;
01139 }
01140 
01141 /* Implement the 'get_typename_from_type_info' method.  */
01142 
01143 static char *
01144 gnuv3_get_typename_from_type_info (struct value *type_info_ptr)
01145 {
01146   struct gdbarch *gdbarch = get_type_arch (value_type (type_info_ptr));
01147   struct bound_minimal_symbol typeinfo_sym;
01148   CORE_ADDR addr;
01149   const char *symname;
01150   const char *class_name;
01151   const char *atsign;
01152 
01153   addr = value_as_address (type_info_ptr);
01154   typeinfo_sym = lookup_minimal_symbol_by_pc (addr);
01155   if (typeinfo_sym.minsym == NULL)
01156     error (_("could not find minimal symbol for typeinfo address %s"),
01157            paddress (gdbarch, addr));
01158 
01159 #define TYPEINFO_PREFIX "typeinfo for "
01160 #define TYPEINFO_PREFIX_LEN (sizeof (TYPEINFO_PREFIX) - 1)
01161   symname = SYMBOL_DEMANGLED_NAME (typeinfo_sym.minsym);
01162   if (symname == NULL || strncmp (symname, TYPEINFO_PREFIX,
01163                                   TYPEINFO_PREFIX_LEN))
01164     error (_("typeinfo symbol '%s' has unexpected name"),
01165            SYMBOL_LINKAGE_NAME (typeinfo_sym.minsym));
01166   class_name = symname + TYPEINFO_PREFIX_LEN;
01167 
01168   /* Strip off @plt and version suffixes.  */
01169   atsign = strchr (class_name, '@');
01170   if (atsign != NULL)
01171     return savestring (class_name, atsign - class_name);
01172   return xstrdup (class_name);
01173 }
01174 
01175 /* Implement the 'get_type_from_type_info' method.  */
01176 
01177 static struct type *
01178 gnuv3_get_type_from_type_info (struct value *type_info_ptr)
01179 {
01180   char *typename;
01181   struct cleanup *cleanup;
01182   struct value *type_val;
01183   struct expression *expr;
01184   struct type *result;
01185 
01186   typename = gnuv3_get_typename_from_type_info (type_info_ptr);
01187   cleanup = make_cleanup (xfree, typename);
01188 
01189   /* We have to parse the type name, since in general there is not a
01190      symbol for a type.  This is somewhat bogus since there may be a
01191      mis-parse.  Another approach might be to re-use the demangler's
01192      internal form to reconstruct the type somehow.  */
01193 
01194   expr = parse_expression (typename);
01195   make_cleanup (xfree, expr);
01196 
01197   type_val = evaluate_type (expr);
01198   result = value_type (type_val);
01199 
01200   do_cleanups (cleanup);
01201   return result;
01202 }
01203 
01204 /* Determine if we are currently in a C++ thunk.  If so, get the address
01205    of the routine we are thunking to and continue to there instead.  */
01206 
01207 static CORE_ADDR 
01208 gnuv3_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc)
01209 {
01210   CORE_ADDR real_stop_pc, method_stop_pc, func_addr;
01211   struct gdbarch *gdbarch = get_frame_arch (frame);
01212   struct minimal_symbol *thunk_sym, *fn_sym;
01213   struct obj_section *section;
01214   const char *thunk_name, *fn_name;
01215   
01216   real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
01217   if (real_stop_pc == 0)
01218     real_stop_pc = stop_pc;
01219 
01220   /* Find the linker symbol for this potential thunk.  */
01221   thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc).minsym;
01222   section = find_pc_section (real_stop_pc);
01223   if (thunk_sym == NULL || section == NULL)
01224     return 0;
01225 
01226   /* The symbol's demangled name should be something like "virtual
01227      thunk to FUNCTION", where FUNCTION is the name of the function
01228      being thunked to.  */
01229   thunk_name = SYMBOL_DEMANGLED_NAME (thunk_sym);
01230   if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
01231     return 0;
01232 
01233   fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
01234   fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile);
01235   if (fn_sym == NULL)
01236     return 0;
01237 
01238   method_stop_pc = SYMBOL_VALUE_ADDRESS (fn_sym);
01239 
01240   /* Some targets have minimal symbols pointing to function descriptors
01241      (powerpc 64 for example).  Make sure to retrieve the address
01242      of the real function from the function descriptor before passing on
01243      the address to other layers of GDB.  */
01244   func_addr = gdbarch_convert_from_func_ptr_addr (gdbarch, method_stop_pc,
01245                                                   &current_target);
01246   if (func_addr != 0)
01247     method_stop_pc = func_addr;
01248 
01249   real_stop_pc = gdbarch_skip_trampoline_code
01250                    (gdbarch, frame, method_stop_pc);
01251   if (real_stop_pc == 0)
01252     real_stop_pc = method_stop_pc;
01253 
01254   return real_stop_pc;
01255 }
01256 
01257 /* Return nonzero if a type should be passed by reference.
01258 
01259    The rule in the v3 ABI document comes from section 3.1.1.  If the
01260    type has a non-trivial copy constructor or destructor, then the
01261    caller must make a copy (by calling the copy constructor if there
01262    is one or perform the copy itself otherwise), pass the address of
01263    the copy, and then destroy the temporary (if necessary).
01264 
01265    For return values with non-trivial copy constructors or
01266    destructors, space will be allocated in the caller, and a pointer
01267    will be passed as the first argument (preceding "this").
01268 
01269    We don't have a bulletproof mechanism for determining whether a
01270    constructor or destructor is trivial.  For GCC and DWARF2 debug
01271    information, we can check the artificial flag.
01272 
01273    We don't do anything with the constructors or destructors,
01274    but we have to get the argument passing right anyway.  */
01275 static int
01276 gnuv3_pass_by_reference (struct type *type)
01277 {
01278   int fieldnum, fieldelem;
01279 
01280   CHECK_TYPEDEF (type);
01281 
01282   /* We're only interested in things that can have methods.  */
01283   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
01284       && TYPE_CODE (type) != TYPE_CODE_CLASS
01285       && TYPE_CODE (type) != TYPE_CODE_UNION)
01286     return 0;
01287 
01288   for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
01289     for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
01290          fieldelem++)
01291       {
01292         struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
01293         const char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
01294         struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
01295 
01296         /* If this function is marked as artificial, it is compiler-generated,
01297            and we assume it is trivial.  */
01298         if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
01299           continue;
01300 
01301         /* If we've found a destructor, we must pass this by reference.  */
01302         if (name[0] == '~')
01303           return 1;
01304 
01305         /* If the mangled name of this method doesn't indicate that it
01306            is a constructor, we're not interested.
01307 
01308            FIXME drow/2007-09-23: We could do this using the name of
01309            the method and the name of the class instead of dealing
01310            with the mangled name.  We don't have a convenient function
01311            to strip off both leading scope qualifiers and trailing
01312            template arguments yet.  */
01313         if (!is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem))
01314             && !TYPE_FN_FIELD_CONSTRUCTOR (fn, fieldelem))
01315           continue;
01316 
01317         /* If this method takes two arguments, and the second argument is
01318            a reference to this class, then it is a copy constructor.  */
01319         if (TYPE_NFIELDS (fieldtype) == 2
01320             && TYPE_CODE (TYPE_FIELD_TYPE (fieldtype, 1)) == TYPE_CODE_REF
01321             && check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (fieldtype,
01322                                                                  1))) == type)
01323           return 1;
01324       }
01325 
01326   /* Even if all the constructors and destructors were artificial, one
01327      of them may have invoked a non-artificial constructor or
01328      destructor in a base class.  If any base class needs to be passed
01329      by reference, so does this class.  Similarly for members, which
01330      are constructed whenever this class is.  We do not need to worry
01331      about recursive loops here, since we are only looking at members
01332      of complete class type.  Also ignore any static members.  */
01333   for (fieldnum = 0; fieldnum < TYPE_NFIELDS (type); fieldnum++)
01334     if (! field_is_static (&TYPE_FIELD (type, fieldnum))
01335         && gnuv3_pass_by_reference (TYPE_FIELD_TYPE (type, fieldnum)))
01336       return 1;
01337 
01338   return 0;
01339 }
01340 
01341 static void
01342 init_gnuv3_ops (void)
01343 {
01344   vtable_type_gdbarch_data
01345     = gdbarch_data_register_post_init (build_gdb_vtable_type);
01346   std_type_info_gdbarch_data
01347     = gdbarch_data_register_post_init (build_std_type_info_type);
01348 
01349   gnu_v3_abi_ops.shortname = "gnu-v3";
01350   gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
01351   gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
01352   gnu_v3_abi_ops.is_destructor_name =
01353     (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
01354   gnu_v3_abi_ops.is_constructor_name =
01355     (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
01356   gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
01357   gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
01358   gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
01359   gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
01360   gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
01361   gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
01362   gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
01363   gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
01364   gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
01365   gnu_v3_abi_ops.print_vtable = gnuv3_print_vtable;
01366   gnu_v3_abi_ops.get_typeid = gnuv3_get_typeid;
01367   gnu_v3_abi_ops.get_typeid_type = gnuv3_get_typeid_type;
01368   gnu_v3_abi_ops.get_type_from_type_info = gnuv3_get_type_from_type_info;
01369   gnu_v3_abi_ops.get_typename_from_type_info
01370     = gnuv3_get_typename_from_type_info;
01371   gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
01372   gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
01373 }
01374 
01375 extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */
01376 
01377 void
01378 _initialize_gnu_v3_abi (void)
01379 {
01380   init_gnuv3_ops ();
01381 
01382   register_cp_abi (&gnu_v3_abi_ops);
01383   set_cp_abi_as_auto_default (gnu_v3_abi_ops.shortname);
01384 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines