gdb/gnu-v2-abi.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Abstraction of GNU v2 abi.

  2.    Copyright (C) 2001-2015 Free Software Foundation, Inc.

  3.    Contributed by Daniel Berlin <dberlin@redhat.com>

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "value.h"
  19. #include "demangle.h"
  20. #include "gdb-demangle.h"
  21. #include "cp-abi.h"
  22. #include "cp-support.h"
  23. #include <ctype.h>

  24. struct cp_abi_ops gnu_v2_abi_ops;

  25. static int vb_match (struct type *, int, struct type *);

  26. static enum dtor_kinds
  27. gnuv2_is_destructor_name (const char *name)
  28. {
  29.   if ((name[0] == '_' && is_cplus_marker (name[1]) && name[2] == '_')
  30.       || strncmp (name, "__dt__", 6) == 0)
  31.     return complete_object_dtor;
  32.   else
  33.     return 0;
  34. }

  35. static enum ctor_kinds
  36. gnuv2_is_constructor_name (const char *name)
  37. {
  38.   if ((name[0] == '_' && name[1] == '_'
  39.        && (isdigit (name[2]) || strchr ("Qt", name[2])))
  40.       || strncmp (name, "__ct__", 6) == 0)
  41.     return complete_object_ctor;
  42.   else
  43.     return 0;
  44. }

  45. static int
  46. gnuv2_is_vtable_name (const char *name)
  47. {
  48.   return (((name)[0] == '_'
  49.            && (((name)[1] == 'V' && (name)[2] == 'T')
  50.                || ((name)[1] == 'v' && (name)[2] == 't'))
  51.            && is_cplus_marker ((name)[3])) ||
  52.           ((name)[0] == '_' && (name)[1] == '_'
  53.            && (name)[2] == 'v' && (name)[3] == 't' && (name)[4] == '_'));
  54. }

  55. static int
  56. gnuv2_is_operator_name (const char *name)
  57. {
  58.   return strncmp (name, "operator", 8) == 0;
  59. }


  60. /* Return a virtual function as a value.
  61.    ARG1 is the object which provides the virtual function
  62.    table pointer.  *ARG1P is side-effected in calling this function.
  63.    F is the list of member functions which contains the desired virtual
  64.    function.
  65.    J is an index into F which provides the desired virtual function.

  66.    TYPE is the type in which F is located.  */
  67. static struct value *
  68. gnuv2_virtual_fn_field (struct value **arg1p, struct fn_field * f, int j,
  69.                         struct type * type, int offset)
  70. {
  71.   struct value *arg1 = *arg1p;
  72.   struct type *type1 = check_typedef (value_type (arg1));
  73.   struct type *entry_type;
  74.   /* First, get the virtual function table pointer.  That comes
  75.      with a strange type, so cast it to type `pointer to long' (which
  76.      should serve just fine as a function type).  Then, index into
  77.      the table, and convert final value to appropriate function type.  */
  78.   struct value *entry;
  79.   struct value *vfn;
  80.   struct value *vtbl;
  81.   LONGEST vi = (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j);
  82.   struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j);
  83.   struct type *context;
  84.   struct type *context_vptr_basetype;
  85.   int context_vptr_fieldno;

  86.   if (fcontext == NULL)
  87.     /* We don't have an fcontext (e.g. the program was compiled with
  88.        g++ version 1).  Try to get the vtbl from the TYPE_VPTR_BASETYPE.
  89.        This won't work right for multiple inheritance, but at least we
  90.        should do as well as GDB 3.x did.  */
  91.     fcontext = TYPE_VPTR_BASETYPE (type);
  92.   context = lookup_pointer_type (fcontext);
  93.   /* Now context is a pointer to the basetype containing the vtbl.  */
  94.   if (TYPE_TARGET_TYPE (context) != type1)
  95.     {
  96.       struct value *tmp = value_cast (context, value_addr (arg1));

  97.       arg1 = value_ind (tmp);
  98.       type1 = check_typedef (value_type (arg1));
  99.     }

  100.   context = type1;
  101.   /* Now context is the basetype containing the vtbl.  */

  102.   /* This type may have been defined before its virtual function table
  103.      was.  If so, fill in the virtual function table entry for the
  104.      type now.  */
  105.   context_vptr_fieldno = get_vptr_fieldno (context, &context_vptr_basetype);
  106.   /* FIXME: What to do if vptr_fieldno is still -1?  */

  107.   /* The virtual function table is now an array of structures
  108.      which have the form { int16 offset, delta; void *pfn; }.  */
  109.   vtbl = value_primitive_field (arg1, 0, context_vptr_fieldno,
  110.                                 context_vptr_basetype);

  111.   /* With older versions of g++, the vtbl field pointed to an array
  112.      of structures.  Nowadays it points directly to the structure.  */
  113.   if (TYPE_CODE (value_type (vtbl)) == TYPE_CODE_PTR
  114.       && TYPE_CODE (TYPE_TARGET_TYPE (value_type (vtbl))) == TYPE_CODE_ARRAY)
  115.     {
  116.       /* Handle the case where the vtbl field points to an
  117.          array of structures.  */
  118.       vtbl = value_ind (vtbl);

  119.       /* Index into the virtual function table.  This is hard-coded because
  120.          looking up a field is not cheap, and it may be important to save
  121.          time, e.g. if the user has set a conditional breakpoint calling
  122.          a virtual function.  */
  123.       entry = value_subscript (vtbl, vi);
  124.     }
  125.   else
  126.     {
  127.       /* Handle the case where the vtbl field points directly to a
  128.          structure.  */
  129.       vtbl = value_ptradd (vtbl, vi);
  130.       entry = value_ind (vtbl);
  131.     }

  132.   entry_type = check_typedef (value_type (entry));

  133.   if (TYPE_CODE (entry_type) == TYPE_CODE_STRUCT)
  134.     {
  135.       /* Move the `this' pointer according to the virtual function table.  */
  136.       set_value_offset (arg1, value_offset (arg1)
  137.                         + value_as_long (value_field (entry, 0)));

  138.       if (!value_lazy (arg1))
  139.         {
  140.           set_value_lazy (arg1, 1);
  141.           value_fetch_lazy (arg1);
  142.         }

  143.       vfn = value_field (entry, 2);
  144.     }
  145.   else if (TYPE_CODE (entry_type) == TYPE_CODE_PTR)
  146.     vfn = entry;
  147.   else
  148.     error (_("I'm confused:  virtual function table has bad type"));
  149.   /* Reinstantiate the function pointer with the correct type.  */
  150.   deprecated_set_value_type (vfn,
  151.                              lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j)));

  152.   *arg1p = arg1;
  153.   return vfn;
  154. }


  155. static struct type *
  156. gnuv2_value_rtti_type (struct value *v, int *full, int *top, int *using_enc)
  157. {
  158.   struct type *known_type;
  159.   struct type *rtti_type;
  160.   CORE_ADDR vtbl;
  161.   struct bound_minimal_symbol minsym;
  162.   char *demangled_name, *p;
  163.   const char *linkage_name;
  164.   struct type *btype;
  165.   struct type *known_type_vptr_basetype;
  166.   int known_type_vptr_fieldno;

  167.   if (full)
  168.     *full = 0;
  169.   if (top)
  170.     *top = -1;
  171.   if (using_enc)
  172.     *using_enc = 0;

  173.   /* Get declared type.  */
  174.   known_type = value_type (v);
  175.   CHECK_TYPEDEF (known_type);
  176.   /* RTTI works only or class objects.  */
  177.   if (TYPE_CODE (known_type) != TYPE_CODE_STRUCT)
  178.     return NULL;

  179.   /* Plan on this changing in the future as i get around to setting
  180.      the vtables properly for G++ compiled stuff.  Also, I'll be using
  181.      the type info functions, which are always right.  Deal with it
  182.      until then.  */

  183.   /* Try to get the vptr basetype, fieldno.  */
  184.   known_type_vptr_fieldno = get_vptr_fieldno (known_type,
  185.                                               &known_type_vptr_basetype);

  186.   /* If we can't find it, give up.  */
  187.   if (known_type_vptr_fieldno < 0)
  188.     return NULL;

  189.   /* Make sure our basetype and known type match, otherwise, cast
  190.      so we can get at the vtable properly.  */
  191.   btype = known_type_vptr_basetype;
  192.   CHECK_TYPEDEF (btype);
  193.   if (btype != known_type )
  194.     {
  195.       v = value_cast (btype, v);
  196.       if (using_enc)
  197.         *using_enc=1;
  198.     }
  199.   /* We can't use value_ind here, because it would want to use RTTI, and
  200.      we'd waste a bunch of time figuring out we already know the type.
  201.      Besides, we don't care about the type, just the actual pointer.  */
  202.   if (value_address (value_field (v, known_type_vptr_fieldno)) == 0)
  203.     return NULL;

  204.   vtbl = value_as_address (value_field (v, known_type_vptr_fieldno));

  205.   /* Try to find a symbol that is the vtable.  */
  206.   minsym=lookup_minimal_symbol_by_pc(vtbl);
  207.   if (minsym.minsym==NULL
  208.       || (linkage_name=MSYMBOL_LINKAGE_NAME (minsym.minsym))==NULL
  209.       || !is_vtable_name (linkage_name))
  210.     return NULL;

  211.   /* If we just skip the prefix, we get screwed by namespaces.  */
  212.   demangled_name=gdb_demangle(linkage_name,DMGL_PARAMS|DMGL_ANSI);
  213.   p = strchr (demangled_name, ' ');
  214.   if (p)
  215.     *p = '\0';

  216.   /* Lookup the type for the name.  */
  217.   /* FIXME: chastain/2003-11-26: block=NULL is bogus.  See pr gdb/1465.  */
  218.   rtti_type = cp_lookup_rtti_type (demangled_name, NULL);
  219.   if (rtti_type == NULL)
  220.     return NULL;

  221.   if (TYPE_N_BASECLASSES(rtti_type) > 1 &&  full && (*full) != 1)
  222.     {
  223.       if (top)
  224.         *top = TYPE_BASECLASS_BITPOS (rtti_type,
  225.                                       TYPE_VPTR_FIELDNO(rtti_type)) / 8;
  226.       if (top && ((*top) >0))
  227.         {
  228.           if (TYPE_LENGTH(rtti_type) > TYPE_LENGTH(known_type))
  229.             {
  230.               if (full)
  231.                 *full=0;
  232.             }
  233.           else
  234.             {
  235.               if (full)
  236.                 *full=1;
  237.             }
  238.         }
  239.     }
  240.   else
  241.     {
  242.       if (full)
  243.         *full=1;
  244.     }

  245.   return rtti_type;
  246. }

  247. /* Return true if the INDEXth field of TYPE is a virtual baseclass
  248.    pointer which is for the base class whose type is BASECLASS.  */

  249. static int
  250. vb_match (struct type *type, int index, struct type *basetype)
  251. {
  252.   struct type *fieldtype;
  253.   const char *name = TYPE_FIELD_NAME (type, index);
  254.   const char *field_class_name = NULL;

  255.   if (*name != '_')
  256.     return 0;
  257.   /* gcc 2.4 uses _vb$.  */
  258.   if (name[1] == 'v' && name[2] == 'b' && is_cplus_marker (name[3]))
  259.     field_class_name = name + 4;
  260.   /* gcc 2.5 will use __vb_.  */
  261.   if (name[1] == '_' && name[2] == 'v' && name[3] == 'b' && name[4] == '_')
  262.     field_class_name = name + 5;

  263.   if (field_class_name == NULL)
  264.     /* This field is not a virtual base class pointer.  */
  265.     return 0;

  266.   /* It's a virtual baseclass pointer, now we just need to find out whether
  267.      it is for this baseclass.  */
  268.   fieldtype = TYPE_FIELD_TYPE (type, index);
  269.   if (fieldtype == NULL
  270.       || TYPE_CODE (fieldtype) != TYPE_CODE_PTR)
  271.     /* "Can't happen".  */
  272.     return 0;

  273.   /* What we check for is that either the types are equal (needed for
  274.      nameless types) or have the same name.  This is ugly, and a more
  275.      elegant solution should be devised (which would probably just push
  276.      the ugliness into symbol reading unless we change the stabs format).  */
  277.   if (TYPE_TARGET_TYPE (fieldtype) == basetype)
  278.     return 1;

  279.   if (TYPE_NAME (basetype) != NULL
  280.       && TYPE_NAME (TYPE_TARGET_TYPE (fieldtype)) != NULL
  281.       && strcmp (TYPE_NAME (basetype),
  282.                  TYPE_NAME (TYPE_TARGET_TYPE (fieldtype))) == 0)
  283.     return 1;
  284.   return 0;
  285. }

  286. /* Compute the offset of the baseclass which is the INDEXth baseclass
  287.    of class TYPE, for value at VALADDR (in host) at ADDRESS (in
  288.    target).  The result is the offset of the baseclass value relative
  289.    to (the address of)(ARG) + OFFSET.  */

  290. static int
  291. gnuv2_baseclass_offset (struct type *type, int index,
  292.                         const bfd_byte *valaddr, int embedded_offset,
  293.                         CORE_ADDR address, const struct value *val)
  294. {
  295.   struct type *basetype = TYPE_BASECLASS (type, index);

  296.   if (BASETYPE_VIA_VIRTUAL (type, index))
  297.     {
  298.       /* Must hunt for the pointer to this virtual baseclass.  */
  299.       int i, len = TYPE_NFIELDS (type);
  300.       int n_baseclasses = TYPE_N_BASECLASSES (type);

  301.       /* First look for the virtual baseclass pointer
  302.          in the fields.  */
  303.       for (i = n_baseclasses; i < len; i++)
  304.         {
  305.           if (vb_match (type, i, basetype))
  306.             {
  307.               struct type *field_type;
  308.               int field_offset;
  309.               int field_length;
  310.               CORE_ADDR addr;

  311.               field_type = check_typedef (TYPE_FIELD_TYPE (type, i));
  312.               field_offset = TYPE_FIELD_BITPOS (type, i) / 8;
  313.               field_length = TYPE_LENGTH (field_type);

  314.               if (!value_bytes_available (val, embedded_offset + field_offset,
  315.                                           field_length))
  316.                 throw_error (NOT_AVAILABLE_ERROR,
  317.                              _("Virtual baseclass pointer is not available"));

  318.               addr = unpack_pointer (field_type,
  319.                                      valaddr + embedded_offset + field_offset);

  320.               return addr - (LONGEST) address + embedded_offset;
  321.             }
  322.         }
  323.       /* Not in the fields, so try looking through the baseclasses.  */
  324.       for (i = index + 1; i < n_baseclasses; i++)
  325.         {
  326.           /* Don't go through baseclass_offset, as that wraps
  327.              exceptions, thus, inner exceptions would be wrapped more
  328.              than once.  */
  329.           int boffset =
  330.             gnuv2_baseclass_offset (type, i, valaddr,
  331.                                     embedded_offset, address, val);

  332.           if (boffset)
  333.             return boffset;
  334.         }

  335.       error (_("Baseclass offset not found"));
  336.     }

  337.   /* Baseclass is easily computed.  */
  338.   return TYPE_BASECLASS_BITPOS (type, index) / 8;
  339. }

  340. static void
  341. init_gnuv2_ops (void)
  342. {
  343.   gnu_v2_abi_ops.shortname = "gnu-v2";
  344.   gnu_v2_abi_ops.longname = "GNU G++ Version 2 ABI";
  345.   gnu_v2_abi_ops.doc = "G++ Version 2 ABI";
  346.   gnu_v2_abi_ops.is_destructor_name = gnuv2_is_destructor_name;
  347.   gnu_v2_abi_ops.is_constructor_name = gnuv2_is_constructor_name;
  348.   gnu_v2_abi_ops.is_vtable_name = gnuv2_is_vtable_name;
  349.   gnu_v2_abi_ops.is_operator_name = gnuv2_is_operator_name;
  350.   gnu_v2_abi_ops.virtual_fn_field = gnuv2_virtual_fn_field;
  351.   gnu_v2_abi_ops.rtti_type = gnuv2_value_rtti_type;
  352.   gnu_v2_abi_ops.baseclass_offset = gnuv2_baseclass_offset;
  353. }

  354. extern initialize_file_ftype _initialize_gnu_v2_abi; /* -Wmissing-prototypes */

  355. void
  356. _initialize_gnu_v2_abi (void)
  357. {
  358.   init_gnuv2_ops ();
  359.   register_cp_abi (&gnu_v2_abi_ops);
  360. }