Home | History | Annotate | Line # | Download | only in gdb
      1   1.1  christos /* Abstraction of GNU v3 abi.
      2   1.1  christos    Contributed by Jim Blandy <jimb (at) redhat.com>
      3   1.1  christos 
      4  1.11  christos    Copyright (C) 2001-2024 Free Software Foundation, Inc.
      5   1.1  christos 
      6   1.1  christos    This file is part of GDB.
      7   1.1  christos 
      8   1.1  christos    This program is free software; you can redistribute it and/or modify
      9   1.1  christos    it under the terms of the GNU General Public License as published by
     10   1.1  christos    the Free Software Foundation; either version 3 of the License, or
     11   1.1  christos    (at your option) any later version.
     12   1.1  christos 
     13   1.1  christos    This program is distributed in the hope that it will be useful,
     14   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16   1.1  christos    GNU General Public License for more details.
     17   1.1  christos 
     18   1.1  christos    You should have received a copy of the GNU General Public License
     19   1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     20   1.1  christos 
     21  1.11  christos #include "extract-store-integer.h"
     22  1.11  christos #include "language.h"
     23   1.1  christos #include "value.h"
     24   1.1  christos #include "cp-abi.h"
     25   1.1  christos #include "cp-support.h"
     26   1.1  christos #include "demangle.h"
     27   1.9  christos #include "dwarf2.h"
     28   1.1  christos #include "objfiles.h"
     29   1.1  christos #include "valprint.h"
     30   1.1  christos #include "c-lang.h"
     31   1.1  christos #include "typeprint.h"
     32   1.7  christos #include <algorithm>
     33   1.9  christos #include "cli/cli-style.h"
     34   1.9  christos #include "dwarf2/loc.h"
     35  1.10  christos #include "inferior.h"
     36   1.1  christos 
     37   1.1  christos static struct cp_abi_ops gnu_v3_abi_ops;
     38   1.1  christos 
     39   1.1  christos /* A gdbarch key for std::type_info, in the event that it can't be
     40   1.1  christos    found in the debug info.  */
     41   1.1  christos 
     42  1.10  christos static const registry<gdbarch>::key<struct type> std_type_info_gdbarch_data;
     43   1.1  christos 
     44   1.1  christos 
     45   1.1  christos static int
     46   1.1  christos gnuv3_is_vtable_name (const char *name)
     47   1.1  christos {
     48   1.5  christos   return startswith (name, "_ZTV");
     49   1.1  christos }
     50   1.1  christos 
     51   1.1  christos static int
     52   1.1  christos gnuv3_is_operator_name (const char *name)
     53   1.1  christos {
     54   1.8  christos   return startswith (name, CP_OPERATOR_STR);
     55   1.1  christos }
     56   1.1  christos 
     57   1.1  christos 
     58   1.1  christos /* To help us find the components of a vtable, we build ourselves a
     59   1.1  christos    GDB type object representing the vtable structure.  Following the
     60   1.1  christos    V3 ABI, it goes something like this:
     61   1.1  christos 
     62   1.1  christos    struct gdb_gnu_v3_abi_vtable {
     63   1.1  christos 
     64   1.1  christos      / * An array of virtual call and virtual base offsets.  The real
     65  1.10  christos 	 length of this array depends on the class hierarchy; we use
     66  1.10  christos 	 negative subscripts to access the elements.  Yucky, but
     67  1.10  christos 	 better than the alternatives.  * /
     68   1.1  christos      ptrdiff_t vcall_and_vbase_offsets[0];
     69   1.1  christos 
     70   1.1  christos      / * The offset from a virtual pointer referring to this table
     71  1.10  christos 	 to the top of the complete object.  * /
     72   1.1  christos      ptrdiff_t offset_to_top;
     73   1.1  christos 
     74   1.1  christos      / * The type_info pointer for this class.  This is really a
     75  1.10  christos 	 std::type_info *, but GDB doesn't really look at the
     76  1.10  christos 	 type_info object itself, so we don't bother to get the type
     77  1.10  christos 	 exactly right.  * /
     78   1.1  christos      void *type_info;
     79   1.1  christos 
     80   1.1  christos      / * Virtual table pointers in objects point here.  * /
     81   1.1  christos 
     82   1.1  christos      / * Virtual function pointers.  Like the vcall/vbase array, the
     83  1.10  christos 	 real length of this table depends on the class hierarchy.  * /
     84   1.1  christos      void (*virtual_functions[0]) ();
     85   1.1  christos 
     86   1.1  christos    };
     87   1.1  christos 
     88   1.1  christos    The catch, of course, is that the exact layout of this table
     89   1.1  christos    depends on the ABI --- word size, endianness, alignment, etc.  So
     90   1.1  christos    the GDB type object is actually a per-architecture kind of thing.
     91   1.1  christos 
     92   1.1  christos    vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
     93   1.1  christos    which refers to the struct type * for this structure, laid out
     94   1.1  christos    appropriately for the architecture.  */
     95  1.10  christos static const registry<gdbarch>::key<struct type> vtable_type_gdbarch_data;
     96   1.1  christos 
     97   1.1  christos 
     98   1.1  christos /* Human-readable names for the numbers of the fields above.  */
     99   1.1  christos enum {
    100   1.1  christos   vtable_field_vcall_and_vbase_offsets,
    101   1.1  christos   vtable_field_offset_to_top,
    102   1.1  christos   vtable_field_type_info,
    103   1.1  christos   vtable_field_virtual_functions
    104   1.1  christos };
    105   1.1  christos 
    106   1.1  christos 
    107   1.1  christos /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
    108   1.1  christos    described above, laid out appropriately for ARCH.
    109   1.1  christos 
    110   1.1  christos    We use this function as the gdbarch per-architecture data
    111   1.1  christos    initialization function.  */
    112  1.10  christos static struct type *
    113  1.10  christos get_gdb_vtable_type (struct gdbarch *arch)
    114   1.1  christos {
    115   1.1  christos   struct type *t;
    116   1.1  christos   int offset;
    117   1.1  christos 
    118  1.10  christos   struct type *result = vtable_type_gdbarch_data.get (arch);
    119  1.10  christos   if (result != nullptr)
    120  1.10  christos     return result;
    121  1.10  christos 
    122   1.1  christos   struct type *void_ptr_type
    123   1.1  christos     = builtin_type (arch)->builtin_data_ptr;
    124   1.1  christos   struct type *ptr_to_void_fn_type
    125   1.1  christos     = builtin_type (arch)->builtin_func_ptr;
    126   1.1  christos 
    127  1.11  christos   type_allocator alloc (arch);
    128  1.11  christos 
    129   1.1  christos   /* ARCH can't give us the true ptrdiff_t type, so we guess.  */
    130   1.1  christos   struct type *ptrdiff_type
    131  1.11  christos     = init_integer_type (alloc, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
    132  1.11  christos 
    133  1.11  christos   t = alloc.new_type (TYPE_CODE_STRUCT, 0, nullptr);
    134   1.1  christos 
    135   1.1  christos   /* We assume no padding is necessary, since GDB doesn't know
    136   1.1  christos      anything about alignment at the moment.  If this assumption bites
    137   1.1  christos      us, we should add a gdbarch method which, given a type, returns
    138   1.1  christos      the alignment that type requires, and then use that here.  */
    139   1.1  christos 
    140   1.1  christos   /* Build the field list.  */
    141  1.11  christos   t->alloc_fields (4);
    142  1.11  christos 
    143   1.1  christos   offset = 0;
    144   1.1  christos 
    145   1.1  christos   /* ptrdiff_t vcall_and_vbase_offsets[0]; */
    146  1.11  christos   {
    147  1.11  christos     struct field &field0 = t->field (0);
    148  1.11  christos     field0.set_name ("vcall_and_vbase_offsets");
    149  1.11  christos     field0.set_type (lookup_array_range_type (ptrdiff_type, 0, -1));
    150  1.11  christos     field0.set_loc_bitpos (offset * TARGET_CHAR_BIT);
    151  1.11  christos     offset += field0.type ()->length ();
    152  1.11  christos   }
    153   1.1  christos 
    154   1.1  christos   /* ptrdiff_t offset_to_top; */
    155  1.11  christos   {
    156  1.11  christos     struct field &field1 = t->field (1);
    157  1.11  christos     field1.set_name ("offset_to_top");
    158  1.11  christos     field1.set_type (ptrdiff_type);
    159  1.11  christos     field1.set_loc_bitpos (offset * TARGET_CHAR_BIT);
    160  1.11  christos     offset += field1.type ()->length ();
    161  1.11  christos   }
    162   1.1  christos 
    163   1.1  christos   /* void *type_info; */
    164  1.11  christos   {
    165  1.11  christos     struct field &field2 = t->field (2);
    166  1.11  christos     field2.set_name ("type_info");
    167  1.11  christos     field2.set_type (void_ptr_type);
    168  1.11  christos     field2.set_loc_bitpos (offset * TARGET_CHAR_BIT);
    169  1.11  christos     offset += field2.type ()->length ();
    170  1.11  christos   }
    171   1.1  christos 
    172   1.1  christos   /* void (*virtual_functions[0]) (); */
    173  1.11  christos   {
    174  1.11  christos     struct field &field3 = t->field (3);
    175  1.11  christos     field3.set_name ("virtual_functions");
    176  1.11  christos     field3.set_type (lookup_array_range_type (ptr_to_void_fn_type, 0, -1));
    177  1.11  christos     field3.set_loc_bitpos (offset * TARGET_CHAR_BIT);
    178  1.11  christos     offset += field3.type ()->length ();
    179  1.11  christos   }
    180  1.11  christos 
    181  1.11  christos   t->set_length (offset);
    182  1.11  christos 
    183   1.9  christos   t->set_name ("gdb_gnu_v3_abi_vtable");
    184   1.1  christos   INIT_CPLUS_SPECIFIC (t);
    185   1.1  christos 
    186  1.10  christos   result = make_type_with_address_space (t, TYPE_INSTANCE_FLAG_CODE_SPACE);
    187  1.10  christos   vtable_type_gdbarch_data.set (arch, result);
    188  1.10  christos   return result;
    189   1.1  christos }
    190   1.1  christos 
    191   1.1  christos 
    192   1.1  christos /* Return the ptrdiff_t type used in the vtable type.  */
    193   1.1  christos static struct type *
    194   1.1  christos vtable_ptrdiff_type (struct gdbarch *gdbarch)
    195   1.1  christos {
    196  1.10  christos   struct type *vtable_type = get_gdb_vtable_type (gdbarch);
    197   1.1  christos 
    198   1.1  christos   /* The "offset_to_top" field has the appropriate (ptrdiff_t) type.  */
    199   1.9  christos   return vtable_type->field (vtable_field_offset_to_top).type ();
    200   1.1  christos }
    201   1.1  christos 
    202   1.1  christos /* Return the offset from the start of the imaginary `struct
    203   1.1  christos    gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
    204   1.1  christos    (i.e., where objects' virtual table pointers point).  */
    205   1.1  christos static int
    206   1.1  christos vtable_address_point_offset (struct gdbarch *gdbarch)
    207   1.1  christos {
    208  1.10  christos   struct type *vtable_type = get_gdb_vtable_type (gdbarch);
    209   1.1  christos 
    210  1.10  christos   return (vtable_type->field (vtable_field_virtual_functions).loc_bitpos ()
    211  1.10  christos 	  / TARGET_CHAR_BIT);
    212   1.1  christos }
    213   1.1  christos 
    214   1.1  christos 
    215   1.1  christos /* Determine whether structure TYPE is a dynamic class.  Cache the
    216   1.1  christos    result.  */
    217   1.1  christos 
    218   1.1  christos static int
    219   1.1  christos gnuv3_dynamic_class (struct type *type)
    220   1.1  christos {
    221   1.1  christos   int fieldnum, fieldelem;
    222   1.1  christos 
    223   1.6  christos   type = check_typedef (type);
    224   1.9  christos   gdb_assert (type->code () == TYPE_CODE_STRUCT
    225   1.9  christos 	      || type->code () == TYPE_CODE_UNION);
    226   1.5  christos 
    227   1.9  christos   if (type->code () == TYPE_CODE_UNION)
    228   1.5  christos     return 0;
    229   1.5  christos 
    230   1.1  christos   if (TYPE_CPLUS_DYNAMIC (type))
    231   1.1  christos     return TYPE_CPLUS_DYNAMIC (type) == 1;
    232   1.1  christos 
    233   1.1  christos   ALLOCATE_CPLUS_STRUCT_TYPE (type);
    234   1.1  christos 
    235   1.1  christos   for (fieldnum = 0; fieldnum < TYPE_N_BASECLASSES (type); fieldnum++)
    236   1.1  christos     if (BASETYPE_VIA_VIRTUAL (type, fieldnum)
    237   1.9  christos 	|| gnuv3_dynamic_class (type->field (fieldnum).type ()))
    238   1.1  christos       {
    239   1.1  christos 	TYPE_CPLUS_DYNAMIC (type) = 1;
    240   1.1  christos 	return 1;
    241   1.1  christos       }
    242   1.1  christos 
    243   1.1  christos   for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
    244   1.1  christos     for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
    245   1.1  christos 	 fieldelem++)
    246   1.1  christos       {
    247   1.1  christos 	struct fn_field *f = TYPE_FN_FIELDLIST1 (type, fieldnum);
    248   1.1  christos 
    249   1.1  christos 	if (TYPE_FN_FIELD_VIRTUAL_P (f, fieldelem))
    250   1.1  christos 	  {
    251   1.1  christos 	    TYPE_CPLUS_DYNAMIC (type) = 1;
    252   1.1  christos 	    return 1;
    253   1.1  christos 	  }
    254   1.1  christos       }
    255   1.1  christos 
    256   1.1  christos   TYPE_CPLUS_DYNAMIC (type) = -1;
    257   1.1  christos   return 0;
    258   1.1  christos }
    259   1.1  christos 
    260   1.1  christos /* Find the vtable for a value of CONTAINER_TYPE located at
    261   1.1  christos    CONTAINER_ADDR.  Return a value of the correct vtable type for this
    262   1.1  christos    architecture, or NULL if CONTAINER does not have a vtable.  */
    263   1.1  christos 
    264   1.1  christos static struct value *
    265   1.1  christos gnuv3_get_vtable (struct gdbarch *gdbarch,
    266   1.1  christos 		  struct type *container_type, CORE_ADDR container_addr)
    267   1.1  christos {
    268  1.10  christos   struct type *vtable_type = get_gdb_vtable_type (gdbarch);
    269   1.1  christos   struct type *vtable_pointer_type;
    270   1.1  christos   struct value *vtable_pointer;
    271   1.1  christos   CORE_ADDR vtable_address;
    272   1.1  christos 
    273   1.6  christos   container_type = check_typedef (container_type);
    274   1.9  christos   gdb_assert (container_type->code () == TYPE_CODE_STRUCT);
    275   1.5  christos 
    276   1.1  christos   /* If this type does not have a virtual table, don't read the first
    277   1.1  christos      field.  */
    278   1.5  christos   if (!gnuv3_dynamic_class (container_type))
    279   1.1  christos     return NULL;
    280   1.1  christos 
    281   1.1  christos   /* We do not consult the debug information to find the virtual table.
    282   1.1  christos      The ABI specifies that it is always at offset zero in any class,
    283   1.1  christos      and debug information may not represent it.
    284   1.1  christos 
    285   1.1  christos      We avoid using value_contents on principle, because the object might
    286   1.1  christos      be large.  */
    287   1.1  christos 
    288   1.1  christos   /* Find the type "pointer to virtual table".  */
    289   1.1  christos   vtable_pointer_type = lookup_pointer_type (vtable_type);
    290   1.1  christos 
    291   1.1  christos   /* Load it from the start of the class.  */
    292   1.1  christos   vtable_pointer = value_at (vtable_pointer_type, container_addr);
    293   1.1  christos   vtable_address = value_as_address (vtable_pointer);
    294   1.1  christos 
    295   1.1  christos   /* Correct it to point at the start of the virtual table, rather
    296   1.1  christos      than the address point.  */
    297   1.1  christos   return value_at_lazy (vtable_type,
    298   1.1  christos 			vtable_address
    299   1.1  christos 			- vtable_address_point_offset (gdbarch));
    300   1.1  christos }
    301   1.1  christos 
    302   1.1  christos 
    303   1.1  christos static struct type *
    304   1.1  christos gnuv3_rtti_type (struct value *value,
    305  1.10  christos 		 int *full_p, LONGEST *top_p, int *using_enc_p)
    306   1.1  christos {
    307   1.1  christos   struct gdbarch *gdbarch;
    308  1.11  christos   struct type *values_type = check_typedef (value->type ());
    309   1.1  christos   struct value *vtable;
    310   1.1  christos   struct minimal_symbol *vtable_symbol;
    311   1.1  christos   const char *vtable_symbol_name;
    312   1.1  christos   const char *class_name;
    313   1.1  christos   struct type *run_time_type;
    314   1.1  christos   LONGEST offset_to_top;
    315   1.6  christos   const char *atsign;
    316   1.1  christos 
    317   1.8  christos   /* We only have RTTI for dynamic class objects.  */
    318   1.9  christos   if (values_type->code () != TYPE_CODE_STRUCT
    319   1.8  christos       || !gnuv3_dynamic_class (values_type))
    320   1.1  christos     return NULL;
    321   1.1  christos 
    322   1.1  christos   /* Determine architecture.  */
    323  1.10  christos   gdbarch = values_type->arch ();
    324   1.1  christos 
    325   1.1  christos   if (using_enc_p)
    326   1.1  christos     *using_enc_p = 0;
    327   1.1  christos 
    328   1.5  christos   vtable = gnuv3_get_vtable (gdbarch, values_type,
    329   1.1  christos 			     value_as_address (value_addr (value)));
    330   1.1  christos   if (vtable == NULL)
    331   1.1  christos     return NULL;
    332   1.1  christos 
    333   1.1  christos   /* Find the linker symbol for this vtable.  */
    334   1.1  christos   vtable_symbol
    335  1.11  christos     = lookup_minimal_symbol_by_pc (vtable->address ()
    336  1.11  christos 				   + vtable->embedded_offset ()).minsym;
    337   1.1  christos   if (! vtable_symbol)
    338   1.1  christos     return NULL;
    339   1.1  christos 
    340   1.1  christos   /* The symbol's demangled name should be something like "vtable for
    341   1.1  christos      CLASS", where CLASS is the name of the run-time type of VALUE.
    342   1.1  christos      If we didn't like this approach, we could instead look in the
    343   1.1  christos      type_info object itself to get the class name.  But this way
    344   1.1  christos      should work just as well, and doesn't read target memory.  */
    345   1.9  christos   vtable_symbol_name = vtable_symbol->demangled_name ();
    346   1.1  christos   if (vtable_symbol_name == NULL
    347   1.5  christos       || !startswith (vtable_symbol_name, "vtable for "))
    348   1.1  christos     {
    349   1.1  christos       warning (_("can't find linker symbol for virtual table for `%s' value"),
    350   1.1  christos 	       TYPE_SAFE_NAME (values_type));
    351   1.1  christos       if (vtable_symbol_name)
    352   1.1  christos 	warning (_("  found `%s' instead"), vtable_symbol_name);
    353   1.1  christos       return NULL;
    354   1.1  christos     }
    355   1.1  christos   class_name = vtable_symbol_name + 11;
    356   1.1  christos 
    357   1.1  christos   /* Strip off @plt and version suffixes.  */
    358   1.1  christos   atsign = strchr (class_name, '@');
    359   1.1  christos   if (atsign != NULL)
    360   1.1  christos     {
    361   1.1  christos       char *copy;
    362   1.1  christos 
    363   1.6  christos       copy = (char *) alloca (atsign - class_name + 1);
    364   1.1  christos       memcpy (copy, class_name, atsign - class_name);
    365   1.1  christos       copy[atsign - class_name] = '\0';
    366   1.1  christos       class_name = copy;
    367   1.1  christos     }
    368   1.1  christos 
    369   1.1  christos   /* Try to look up the class name as a type name.  */
    370   1.1  christos   /* FIXME: chastain/2003-11-26: block=NULL is bogus.  See pr gdb/1465.  */
    371   1.1  christos   run_time_type = cp_lookup_rtti_type (class_name, NULL);
    372   1.1  christos   if (run_time_type == NULL)
    373   1.1  christos     return NULL;
    374   1.1  christos 
    375   1.1  christos   /* Get the offset from VALUE to the top of the complete object.
    376   1.1  christos      NOTE: this is the reverse of the meaning of *TOP_P.  */
    377   1.1  christos   offset_to_top
    378   1.1  christos     = value_as_long (value_field (vtable, vtable_field_offset_to_top));
    379   1.1  christos 
    380   1.1  christos   if (full_p)
    381  1.11  christos     *full_p = (- offset_to_top == value->embedded_offset ()
    382  1.11  christos 	       && (value->enclosing_type ()->length ()
    383  1.10  christos 		   >= run_time_type->length ()));
    384   1.1  christos   if (top_p)
    385   1.1  christos     *top_p = - offset_to_top;
    386   1.1  christos   return run_time_type;
    387   1.1  christos }
    388   1.1  christos 
    389   1.1  christos /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
    390   1.1  christos    function, of type FNTYPE.  */
    391   1.1  christos 
    392   1.1  christos static struct value *
    393   1.1  christos gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
    394   1.1  christos 		      struct type *fntype, int vtable_index)
    395   1.1  christos {
    396   1.1  christos   struct value *vtable, *vfn;
    397   1.1  christos 
    398   1.1  christos   /* Every class with virtual functions must have a vtable.  */
    399  1.11  christos   vtable = gnuv3_get_vtable (gdbarch, container->type (),
    400   1.1  christos 			     value_as_address (value_addr (container)));
    401   1.1  christos   gdb_assert (vtable != NULL);
    402   1.1  christos 
    403   1.1  christos   /* Fetch the appropriate function pointer from the vtable.  */
    404   1.1  christos   vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
    405  1.10  christos 			 vtable_index);
    406   1.1  christos 
    407   1.1  christos   /* If this architecture uses function descriptors directly in the vtable,
    408   1.1  christos      then the address of the vtable entry is actually a "function pointer"
    409   1.1  christos      (i.e. points to the descriptor).  We don't need to scale the index
    410   1.9  christos      by the size of a function descriptor; GCC does that before outputting
    411   1.1  christos      debug information.  */
    412   1.1  christos   if (gdbarch_vtable_function_descriptors (gdbarch))
    413   1.1  christos     vfn = value_addr (vfn);
    414   1.1  christos 
    415   1.1  christos   /* Cast the function pointer to the appropriate type.  */
    416   1.1  christos   vfn = value_cast (lookup_pointer_type (fntype), vfn);
    417   1.1  christos 
    418   1.1  christos   return vfn;
    419   1.1  christos }
    420   1.1  christos 
    421   1.1  christos /* GNU v3 implementation of value_virtual_fn_field.  See cp-abi.h
    422   1.1  christos    for a description of the arguments.  */
    423   1.1  christos 
    424   1.1  christos static struct value *
    425   1.1  christos gnuv3_virtual_fn_field (struct value **value_p,
    426  1.10  christos 			struct fn_field *f, int j,
    427   1.1  christos 			struct type *vfn_base, int offset)
    428   1.1  christos {
    429  1.11  christos   struct type *values_type = check_typedef ((*value_p)->type ());
    430   1.1  christos   struct gdbarch *gdbarch;
    431   1.1  christos 
    432   1.1  christos   /* Some simple sanity checks.  */
    433   1.9  christos   if (values_type->code () != TYPE_CODE_STRUCT)
    434   1.1  christos     error (_("Only classes can have virtual functions."));
    435   1.1  christos 
    436   1.1  christos   /* Determine architecture.  */
    437  1.10  christos   gdbarch = values_type->arch ();
    438   1.1  christos 
    439   1.1  christos   /* Cast our value to the base class which defines this virtual
    440   1.1  christos      function.  This takes care of any necessary `this'
    441   1.1  christos      adjustments.  */
    442   1.1  christos   if (vfn_base != values_type)
    443   1.1  christos     *value_p = value_cast (vfn_base, *value_p);
    444   1.1  christos 
    445   1.1  christos   return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
    446   1.1  christos 			       TYPE_FN_FIELD_VOFFSET (f, j));
    447   1.1  christos }
    448   1.1  christos 
    449   1.1  christos /* Compute the offset of the baseclass which is
    450   1.1  christos    the INDEXth baseclass of class TYPE,
    451   1.1  christos    for value at VALADDR (in host) at ADDRESS (in target).
    452   1.1  christos    The result is the offset of the baseclass value relative
    453   1.1  christos    to (the address of)(ARG) + OFFSET.
    454   1.1  christos 
    455   1.1  christos    -1 is returned on error.  */
    456   1.1  christos 
    457   1.1  christos static int
    458   1.1  christos gnuv3_baseclass_offset (struct type *type, int index,
    459   1.6  christos 			const bfd_byte *valaddr, LONGEST embedded_offset,
    460   1.1  christos 			CORE_ADDR address, const struct value *val)
    461   1.1  christos {
    462   1.1  christos   struct gdbarch *gdbarch;
    463   1.1  christos   struct type *ptr_type;
    464   1.1  christos   struct value *vtable;
    465   1.1  christos   struct value *vbase_array;
    466   1.1  christos   long int cur_base_offset, base_offset;
    467   1.1  christos 
    468   1.1  christos   /* Determine architecture.  */
    469  1.10  christos   gdbarch = type->arch ();
    470   1.1  christos   ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
    471   1.1  christos 
    472   1.1  christos   /* If it isn't a virtual base, this is easy.  The offset is in the
    473   1.7  christos      type definition.  */
    474   1.7  christos   if (!BASETYPE_VIA_VIRTUAL (type, index))
    475   1.1  christos     return TYPE_BASECLASS_BITPOS (type, index) / 8;
    476   1.1  christos 
    477   1.9  christos   /* If we have a DWARF expression for the offset, evaluate it.  */
    478  1.10  christos   if (type->field (index).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK)
    479   1.9  christos     {
    480   1.9  christos       struct dwarf2_property_baton baton;
    481   1.9  christos       baton.property_type
    482   1.9  christos 	= lookup_pointer_type (type->field (index).type ());
    483  1.10  christos       baton.locexpr = *type->field (index).loc_dwarf_block ();
    484   1.9  christos 
    485   1.9  christos       struct dynamic_prop prop;
    486   1.9  christos       prop.set_locexpr (&baton);
    487   1.9  christos 
    488   1.9  christos       struct property_addr_info addr_stack;
    489   1.9  christos       addr_stack.type = type;
    490   1.9  christos       /* Note that we don't set "valaddr" here.  Doing so causes
    491   1.9  christos 	 regressions.  FIXME.  */
    492   1.9  christos       addr_stack.addr = address + embedded_offset;
    493   1.9  christos       addr_stack.next = nullptr;
    494   1.9  christos 
    495   1.9  christos       CORE_ADDR result;
    496   1.9  christos       if (dwarf2_evaluate_property (&prop, nullptr, &addr_stack, &result,
    497  1.10  christos 				    {addr_stack.addr}))
    498   1.9  christos 	return (int) (result - addr_stack.addr);
    499   1.9  christos     }
    500   1.9  christos 
    501   1.1  christos   /* To access a virtual base, we need to use the vbase offset stored in
    502   1.1  christos      our vtable.  Recent GCC versions provide this information.  If it isn't
    503   1.1  christos      available, we could get what we needed from RTTI, or from drawing the
    504   1.1  christos      complete inheritance graph based on the debug info.  Neither is
    505   1.1  christos      worthwhile.  */
    506   1.1  christos   cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
    507   1.1  christos   if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
    508   1.1  christos     error (_("Expected a negative vbase offset (old compiler?)"));
    509   1.1  christos 
    510   1.1  christos   cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
    511  1.10  christos   if ((- cur_base_offset) % ptr_type->length () != 0)
    512   1.1  christos     error (_("Misaligned vbase offset."));
    513  1.10  christos   cur_base_offset = cur_base_offset / ((int) ptr_type->length ());
    514   1.1  christos 
    515   1.1  christos   vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset);
    516   1.1  christos   gdb_assert (vtable != NULL);
    517   1.1  christos   vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
    518   1.1  christos   base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
    519   1.1  christos   return base_offset;
    520   1.1  christos }
    521   1.1  christos 
    522   1.1  christos /* Locate a virtual method in DOMAIN or its non-virtual base classes
    523   1.1  christos    which has virtual table index VOFFSET.  The method has an associated
    524   1.1  christos    "this" adjustment of ADJUSTMENT bytes.  */
    525   1.1  christos 
    526   1.1  christos static const char *
    527   1.1  christos gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
    528   1.1  christos 		      LONGEST adjustment)
    529   1.1  christos {
    530   1.1  christos   int i;
    531   1.1  christos 
    532   1.1  christos   /* Search this class first.  */
    533   1.1  christos   if (adjustment == 0)
    534   1.1  christos     {
    535   1.1  christos       int len;
    536   1.1  christos 
    537   1.1  christos       len = TYPE_NFN_FIELDS (domain);
    538   1.1  christos       for (i = 0; i < len; i++)
    539   1.1  christos 	{
    540   1.1  christos 	  int len2, j;
    541   1.1  christos 	  struct fn_field *f;
    542   1.1  christos 
    543   1.1  christos 	  f = TYPE_FN_FIELDLIST1 (domain, i);
    544   1.1  christos 	  len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
    545   1.1  christos 
    546   1.1  christos 	  check_stub_method_group (domain, i);
    547   1.1  christos 	  for (j = 0; j < len2; j++)
    548   1.1  christos 	    if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset)
    549   1.1  christos 	      return TYPE_FN_FIELD_PHYSNAME (f, j);
    550   1.1  christos 	}
    551   1.1  christos     }
    552   1.1  christos 
    553   1.1  christos   /* Next search non-virtual bases.  If it's in a virtual base,
    554   1.1  christos      we're out of luck.  */
    555   1.1  christos   for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
    556   1.1  christos     {
    557   1.1  christos       int pos;
    558   1.1  christos       struct type *basetype;
    559   1.1  christos 
    560   1.1  christos       if (BASETYPE_VIA_VIRTUAL (domain, i))
    561   1.1  christos 	continue;
    562   1.1  christos 
    563   1.1  christos       pos = TYPE_BASECLASS_BITPOS (domain, i) / 8;
    564   1.9  christos       basetype = domain->field (i).type ();
    565   1.1  christos       /* Recurse with a modified adjustment.  We don't need to adjust
    566   1.1  christos 	 voffset.  */
    567  1.10  christos       if (adjustment >= pos && adjustment < pos + basetype->length ())
    568   1.1  christos 	return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
    569   1.1  christos     }
    570   1.1  christos 
    571   1.1  christos   return NULL;
    572   1.1  christos }
    573   1.1  christos 
    574   1.1  christos /* Decode GNU v3 method pointer.  */
    575   1.1  christos 
    576   1.1  christos static int
    577   1.1  christos gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
    578   1.1  christos 			 const gdb_byte *contents,
    579   1.1  christos 			 CORE_ADDR *value_p,
    580   1.1  christos 			 LONGEST *adjustment_p)
    581   1.1  christos {
    582   1.1  christos   struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
    583   1.1  christos   struct type *offset_type = vtable_ptrdiff_type (gdbarch);
    584   1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    585   1.1  christos   CORE_ADDR ptr_value;
    586   1.1  christos   LONGEST voffset, adjustment;
    587   1.1  christos   int vbit;
    588   1.1  christos 
    589   1.1  christos   /* Extract the pointer to member.  The first element is either a pointer
    590   1.1  christos      or a vtable offset.  For pointers, we need to use extract_typed_address
    591   1.1  christos      to allow the back-end to convert the pointer to a GDB address -- but
    592   1.1  christos      vtable offsets we must handle as integers.  At this point, we do not
    593   1.1  christos      yet know which case we have, so we extract the value under both
    594   1.1  christos      interpretations and choose the right one later on.  */
    595   1.1  christos   ptr_value = extract_typed_address (contents, funcptr_type);
    596   1.1  christos   voffset = extract_signed_integer (contents,
    597  1.10  christos 				    funcptr_type->length (), byte_order);
    598  1.10  christos   contents += funcptr_type->length ();
    599   1.1  christos   adjustment = extract_signed_integer (contents,
    600  1.10  christos 				       offset_type->length (), byte_order);
    601   1.1  christos 
    602   1.1  christos   if (!gdbarch_vbit_in_delta (gdbarch))
    603   1.1  christos     {
    604   1.1  christos       vbit = voffset & 1;
    605   1.1  christos       voffset = voffset ^ vbit;
    606   1.1  christos     }
    607   1.1  christos   else
    608   1.1  christos     {
    609   1.1  christos       vbit = adjustment & 1;
    610   1.1  christos       adjustment = adjustment >> 1;
    611   1.1  christos     }
    612   1.1  christos 
    613   1.1  christos   *value_p = vbit? voffset : ptr_value;
    614   1.1  christos   *adjustment_p = adjustment;
    615   1.1  christos   return vbit;
    616   1.1  christos }
    617   1.1  christos 
    618   1.1  christos /* GNU v3 implementation of cplus_print_method_ptr.  */
    619   1.1  christos 
    620   1.1  christos static void
    621   1.1  christos gnuv3_print_method_ptr (const gdb_byte *contents,
    622   1.1  christos 			struct type *type,
    623   1.1  christos 			struct ui_file *stream)
    624   1.1  christos {
    625   1.5  christos   struct type *self_type = TYPE_SELF_TYPE (type);
    626  1.10  christos   struct gdbarch *gdbarch = self_type->arch ();
    627   1.1  christos   CORE_ADDR ptr_value;
    628   1.1  christos   LONGEST adjustment;
    629   1.1  christos   int vbit;
    630   1.1  christos 
    631   1.1  christos   /* Extract the pointer to member.  */
    632   1.1  christos   vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
    633   1.1  christos 
    634   1.1  christos   /* Check for NULL.  */
    635   1.1  christos   if (ptr_value == 0 && vbit == 0)
    636   1.1  christos     {
    637  1.10  christos       gdb_printf (stream, "NULL");
    638   1.1  christos       return;
    639   1.1  christos     }
    640   1.1  christos 
    641   1.1  christos   /* Search for a virtual method.  */
    642   1.1  christos   if (vbit)
    643   1.1  christos     {
    644   1.1  christos       CORE_ADDR voffset;
    645   1.1  christos       const char *physname;
    646   1.1  christos 
    647   1.1  christos       /* It's a virtual table offset, maybe in this class.  Search
    648   1.1  christos 	 for a field with the correct vtable offset.  First convert it
    649   1.1  christos 	 to an index, as used in TYPE_FN_FIELD_VOFFSET.  */
    650  1.10  christos       voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
    651   1.1  christos 
    652   1.5  christos       physname = gnuv3_find_method_in (self_type, voffset, adjustment);
    653   1.1  christos 
    654   1.1  christos       /* If we found a method, print that.  We don't bother to disambiguate
    655   1.1  christos 	 possible paths to the method based on the adjustment.  */
    656   1.1  christos       if (physname)
    657   1.1  christos 	{
    658  1.10  christos 	  gdb::unique_xmalloc_ptr<char> demangled_name
    659  1.10  christos 	    = gdb_demangle (physname, DMGL_ANSI | DMGL_PARAMS);
    660   1.1  christos 
    661  1.10  christos 	  gdb_printf (stream, "&virtual ");
    662   1.1  christos 	  if (demangled_name == NULL)
    663  1.10  christos 	    gdb_puts (physname, stream);
    664   1.1  christos 	  else
    665  1.10  christos 	    gdb_puts (demangled_name.get (), stream);
    666   1.1  christos 	  return;
    667   1.1  christos 	}
    668   1.1  christos     }
    669   1.1  christos   else if (ptr_value != 0)
    670   1.1  christos     {
    671   1.1  christos       /* Found a non-virtual function: print out the type.  */
    672  1.10  christos       gdb_puts ("(", stream);
    673  1.10  christos       c_print_type (type, "", stream, -1, 0, current_language->la_language,
    674  1.10  christos 		    &type_print_raw_options);
    675  1.10  christos       gdb_puts (") ", stream);
    676   1.1  christos     }
    677   1.1  christos 
    678   1.1  christos   /* We didn't find it; print the raw data.  */
    679   1.1  christos   if (vbit)
    680   1.1  christos     {
    681  1.10  christos       gdb_printf (stream, "&virtual table offset ");
    682   1.1  christos       print_longest (stream, 'd', 1, ptr_value);
    683   1.1  christos     }
    684   1.1  christos   else
    685   1.1  christos     {
    686   1.1  christos       struct value_print_options opts;
    687   1.1  christos 
    688   1.1  christos       get_user_print_options (&opts);
    689   1.1  christos       print_address_demangle (&opts, gdbarch, ptr_value, stream, demangle);
    690   1.1  christos     }
    691   1.1  christos 
    692   1.1  christos   if (adjustment)
    693   1.1  christos     {
    694  1.10  christos       gdb_printf (stream, ", this adjustment ");
    695   1.1  christos       print_longest (stream, 'd', 1, adjustment);
    696   1.1  christos     }
    697   1.1  christos }
    698   1.1  christos 
    699   1.1  christos /* GNU v3 implementation of cplus_method_ptr_size.  */
    700   1.1  christos 
    701   1.1  christos static int
    702   1.1  christos gnuv3_method_ptr_size (struct type *type)
    703   1.1  christos {
    704  1.10  christos   return 2 * builtin_type (type->arch ())->builtin_data_ptr->length ();
    705   1.1  christos }
    706   1.1  christos 
    707   1.1  christos /* GNU v3 implementation of cplus_make_method_ptr.  */
    708   1.1  christos 
    709   1.1  christos static void
    710   1.1  christos gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
    711   1.1  christos 		       CORE_ADDR value, int is_virtual)
    712   1.1  christos {
    713  1.10  christos   struct gdbarch *gdbarch = type->arch ();
    714  1.10  christos   int size = builtin_type (gdbarch)->builtin_data_ptr->length ();
    715   1.9  christos   enum bfd_endian byte_order = type_byte_order (type);
    716   1.1  christos 
    717   1.1  christos   /* FIXME drow/2006-12-24: The adjustment of "this" is currently
    718   1.1  christos      always zero, since the method pointer is of the correct type.
    719   1.1  christos      But if the method pointer came from a base class, this is
    720   1.1  christos      incorrect - it should be the offset to the base.  The best
    721   1.1  christos      fix might be to create the pointer to member pointing at the
    722   1.1  christos      base class and cast it to the derived class, but that requires
    723   1.1  christos      support for adjusting pointers to members when casting them -
    724   1.1  christos      not currently supported by GDB.  */
    725   1.1  christos 
    726   1.1  christos   if (!gdbarch_vbit_in_delta (gdbarch))
    727   1.1  christos     {
    728   1.1  christos       store_unsigned_integer (contents, size, byte_order, value | is_virtual);
    729   1.1  christos       store_unsigned_integer (contents + size, size, byte_order, 0);
    730   1.1  christos     }
    731   1.1  christos   else
    732   1.1  christos     {
    733   1.1  christos       store_unsigned_integer (contents, size, byte_order, value);
    734   1.1  christos       store_unsigned_integer (contents + size, size, byte_order, is_virtual);
    735   1.1  christos     }
    736   1.1  christos }
    737   1.1  christos 
    738   1.1  christos /* GNU v3 implementation of cplus_method_ptr_to_value.  */
    739   1.1  christos 
    740   1.1  christos static struct value *
    741   1.1  christos gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
    742   1.1  christos {
    743   1.1  christos   struct gdbarch *gdbarch;
    744  1.11  christos   const gdb_byte *contents = method_ptr->contents ().data ();
    745   1.1  christos   CORE_ADDR ptr_value;
    746   1.5  christos   struct type *self_type, *final_type, *method_type;
    747   1.1  christos   LONGEST adjustment;
    748   1.1  christos   int vbit;
    749   1.1  christos 
    750  1.11  christos   self_type = TYPE_SELF_TYPE (check_typedef (method_ptr->type ()));
    751   1.5  christos   final_type = lookup_pointer_type (self_type);
    752   1.1  christos 
    753  1.11  christos   method_type = check_typedef (method_ptr->type ())->target_type ();
    754   1.1  christos 
    755   1.1  christos   /* Extract the pointer to member.  */
    756  1.10  christos   gdbarch = self_type->arch ();
    757   1.1  christos   vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
    758   1.1  christos 
    759   1.1  christos   /* First convert THIS to match the containing type of the pointer to
    760   1.1  christos      member.  This cast may adjust the value of THIS.  */
    761   1.1  christos   *this_p = value_cast (final_type, *this_p);
    762   1.1  christos 
    763   1.1  christos   /* Then apply whatever adjustment is necessary.  This creates a somewhat
    764   1.1  christos      strange pointer: it claims to have type FINAL_TYPE, but in fact it
    765   1.1  christos      might not be a valid FINAL_TYPE.  For instance, it might be a
    766   1.1  christos      base class of FINAL_TYPE.  And if it's not the primary base class,
    767   1.1  christos      then printing it out as a FINAL_TYPE object would produce some pretty
    768   1.1  christos      garbage.
    769   1.1  christos 
    770   1.1  christos      But we don't really know the type of the first argument in
    771   1.1  christos      METHOD_TYPE either, which is why this happens.  We can't
    772   1.1  christos      dereference this later as a FINAL_TYPE, but once we arrive in the
    773   1.1  christos      called method we'll have debugging information for the type of
    774   1.1  christos      "this" - and that'll match the value we produce here.
    775   1.1  christos 
    776   1.1  christos      You can provoke this case by casting a Base::* to a Derived::*, for
    777   1.1  christos      instance.  */
    778   1.1  christos   *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
    779   1.1  christos   *this_p = value_ptradd (*this_p, adjustment);
    780   1.1  christos   *this_p = value_cast (final_type, *this_p);
    781   1.1  christos 
    782   1.1  christos   if (vbit)
    783   1.1  christos     {
    784   1.1  christos       LONGEST voffset;
    785   1.1  christos 
    786  1.10  christos       voffset = ptr_value / vtable_ptrdiff_type (gdbarch)->length ();
    787   1.1  christos       return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
    788   1.1  christos 				   method_type, voffset);
    789   1.1  christos     }
    790   1.1  christos   else
    791   1.1  christos     return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
    792   1.1  christos }
    793   1.1  christos 
    794   1.1  christos /* Objects of this type are stored in a hash table and a vector when
    795   1.1  christos    printing the vtables for a class.  */
    796   1.1  christos 
    797   1.1  christos struct value_and_voffset
    798   1.1  christos {
    799   1.1  christos   /* The value representing the object.  */
    800   1.1  christos   struct value *value;
    801   1.1  christos 
    802   1.1  christos   /* The maximum vtable offset we've found for any object at this
    803   1.1  christos      offset in the outermost object.  */
    804   1.1  christos   int max_voffset;
    805   1.1  christos };
    806   1.1  christos 
    807   1.1  christos /* Hash function for value_and_voffset.  */
    808   1.1  christos 
    809   1.1  christos static hashval_t
    810   1.1  christos hash_value_and_voffset (const void *p)
    811   1.1  christos {
    812   1.6  christos   const struct value_and_voffset *o = (const struct value_and_voffset *) p;
    813   1.1  christos 
    814  1.11  christos   return o->value->address () + o->value->embedded_offset ();
    815   1.1  christos }
    816   1.1  christos 
    817   1.1  christos /* Equality function for value_and_voffset.  */
    818   1.1  christos 
    819   1.1  christos static int
    820   1.1  christos eq_value_and_voffset (const void *a, const void *b)
    821   1.1  christos {
    822   1.6  christos   const struct value_and_voffset *ova = (const struct value_and_voffset *) a;
    823   1.6  christos   const struct value_and_voffset *ovb = (const struct value_and_voffset *) b;
    824   1.1  christos 
    825  1.11  christos   return (ova->value->address () + ova->value->embedded_offset ()
    826  1.11  christos 	  == ovb->value->address () + ovb->value->embedded_offset ());
    827   1.1  christos }
    828   1.1  christos 
    829   1.7  christos /* Comparison function for value_and_voffset.  */
    830   1.1  christos 
    831   1.7  christos static bool
    832   1.7  christos compare_value_and_voffset (const struct value_and_voffset *va,
    833   1.7  christos 			   const struct value_and_voffset *vb)
    834   1.7  christos {
    835  1.11  christos   CORE_ADDR addra = (va->value->address ()
    836  1.11  christos 		     + va->value->embedded_offset ());
    837  1.11  christos   CORE_ADDR addrb = (vb->value->address ()
    838  1.11  christos 		     + vb->value->embedded_offset ());
    839   1.7  christos 
    840   1.7  christos   return addra < addrb;
    841   1.1  christos }
    842   1.1  christos 
    843   1.1  christos /* A helper function used when printing vtables.  This determines the
    844   1.1  christos    key (most derived) sub-object at each address and also computes the
    845   1.1  christos    maximum vtable offset seen for the corresponding vtable.  Updates
    846   1.1  christos    OFFSET_HASH and OFFSET_VEC with a new value_and_voffset object, if
    847   1.1  christos    needed.  VALUE is the object to examine.  */
    848   1.1  christos 
    849   1.1  christos static void
    850   1.1  christos compute_vtable_size (htab_t offset_hash,
    851   1.7  christos 		     std::vector<value_and_voffset *> *offset_vec,
    852   1.1  christos 		     struct value *value)
    853   1.1  christos {
    854   1.1  christos   int i;
    855  1.11  christos   struct type *type = check_typedef (value->type ());
    856   1.1  christos   void **slot;
    857   1.1  christos   struct value_and_voffset search_vo, *current_vo;
    858   1.1  christos 
    859   1.9  christos   gdb_assert (type->code () == TYPE_CODE_STRUCT);
    860   1.5  christos 
    861   1.1  christos   /* If the object is not dynamic, then we are done; as it cannot have
    862   1.1  christos      dynamic base types either.  */
    863   1.1  christos   if (!gnuv3_dynamic_class (type))
    864   1.1  christos     return;
    865   1.1  christos 
    866   1.1  christos   /* Update the hash and the vec, if needed.  */
    867   1.1  christos   search_vo.value = value;
    868   1.1  christos   slot = htab_find_slot (offset_hash, &search_vo, INSERT);
    869   1.1  christos   if (*slot)
    870   1.6  christos     current_vo = (struct value_and_voffset *) *slot;
    871   1.1  christos   else
    872   1.1  christos     {
    873   1.1  christos       current_vo = XNEW (struct value_and_voffset);
    874   1.1  christos       current_vo->value = value;
    875   1.1  christos       current_vo->max_voffset = -1;
    876   1.1  christos       *slot = current_vo;
    877   1.7  christos       offset_vec->push_back (current_vo);
    878   1.1  christos     }
    879   1.1  christos 
    880   1.1  christos   /* Update the value_and_voffset object with the highest vtable
    881   1.1  christos      offset from this class.  */
    882   1.1  christos   for (i = 0; i < TYPE_NFN_FIELDS (type); ++i)
    883   1.1  christos     {
    884   1.1  christos       int j;
    885   1.1  christos       struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, i);
    886   1.1  christos 
    887   1.1  christos       for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (type, i); ++j)
    888   1.1  christos 	{
    889   1.1  christos 	  if (TYPE_FN_FIELD_VIRTUAL_P (fn, j))
    890   1.1  christos 	    {
    891   1.1  christos 	      int voffset = TYPE_FN_FIELD_VOFFSET (fn, j);
    892   1.1  christos 
    893   1.1  christos 	      if (voffset > current_vo->max_voffset)
    894   1.1  christos 		current_vo->max_voffset = voffset;
    895   1.1  christos 	    }
    896   1.1  christos 	}
    897   1.1  christos     }
    898   1.1  christos 
    899   1.1  christos   /* Recurse into base classes.  */
    900   1.1  christos   for (i = 0; i < TYPE_N_BASECLASSES (type); ++i)
    901   1.1  christos     compute_vtable_size (offset_hash, offset_vec, value_field (value, i));
    902   1.1  christos }
    903   1.1  christos 
    904   1.1  christos /* Helper for gnuv3_print_vtable that prints a single vtable.  */
    905   1.1  christos 
    906   1.1  christos static void
    907   1.1  christos print_one_vtable (struct gdbarch *gdbarch, struct value *value,
    908   1.1  christos 		  int max_voffset,
    909   1.1  christos 		  struct value_print_options *opts)
    910   1.1  christos {
    911   1.1  christos   int i;
    912  1.11  christos   struct type *type = check_typedef (value->type ());
    913   1.1  christos   struct value *vtable;
    914   1.1  christos   CORE_ADDR vt_addr;
    915   1.1  christos 
    916   1.1  christos   vtable = gnuv3_get_vtable (gdbarch, type,
    917  1.11  christos 			     value->address ()
    918  1.11  christos 			     + value->embedded_offset ());
    919  1.11  christos   vt_addr = value_field (vtable,
    920  1.11  christos 			 vtable_field_virtual_functions)->address ();
    921   1.1  christos 
    922  1.10  christos   gdb_printf (_("vtable for '%s' @ %s (subobject @ %s):\n"),
    923  1.10  christos 	      TYPE_SAFE_NAME (type),
    924  1.10  christos 	      paddress (gdbarch, vt_addr),
    925  1.11  christos 	      paddress (gdbarch, (value->address ()
    926  1.11  christos 				  + value->embedded_offset ())));
    927   1.1  christos 
    928   1.1  christos   for (i = 0; i <= max_voffset; ++i)
    929   1.1  christos     {
    930   1.1  christos       /* Initialize it just to avoid a GCC false warning.  */
    931   1.1  christos       CORE_ADDR addr = 0;
    932   1.5  christos       int got_error = 0;
    933   1.1  christos       struct value *vfn;
    934   1.1  christos 
    935  1.10  christos       gdb_printf ("[%d]: ", i);
    936   1.1  christos 
    937   1.1  christos       vfn = value_subscript (value_field (vtable,
    938   1.1  christos 					  vtable_field_virtual_functions),
    939   1.1  christos 			     i);
    940   1.1  christos 
    941   1.1  christos       if (gdbarch_vtable_function_descriptors (gdbarch))
    942   1.1  christos 	vfn = value_addr (vfn);
    943   1.1  christos 
    944   1.9  christos       try
    945   1.1  christos 	{
    946   1.1  christos 	  addr = value_as_address (vfn);
    947   1.1  christos 	}
    948   1.9  christos       catch (const gdb_exception_error &ex)
    949   1.5  christos 	{
    950   1.9  christos 	  fprintf_styled (gdb_stdout, metadata_style.style (),
    951   1.9  christos 			  _("<error: %s>"), ex.what ());
    952   1.5  christos 	  got_error = 1;
    953   1.5  christos 	}
    954   1.5  christos 
    955   1.5  christos       if (!got_error)
    956   1.1  christos 	print_function_pointer_address (opts, gdbarch, addr, gdb_stdout);
    957  1.10  christos       gdb_printf ("\n");
    958   1.1  christos     }
    959   1.1  christos }
    960   1.1  christos 
    961   1.1  christos /* Implementation of the print_vtable method.  */
    962   1.1  christos 
    963   1.1  christos static void
    964   1.1  christos gnuv3_print_vtable (struct value *value)
    965   1.1  christos {
    966   1.1  christos   struct gdbarch *gdbarch;
    967   1.1  christos   struct type *type;
    968   1.1  christos   struct value *vtable;
    969   1.1  christos   struct value_print_options opts;
    970   1.7  christos   int count;
    971   1.1  christos 
    972   1.1  christos   value = coerce_ref (value);
    973  1.11  christos   type = check_typedef (value->type ());
    974   1.9  christos   if (type->code () == TYPE_CODE_PTR)
    975   1.1  christos     {
    976   1.1  christos       value = value_ind (value);
    977  1.11  christos       type = check_typedef (value->type ());
    978   1.1  christos     }
    979   1.1  christos 
    980   1.1  christos   get_user_print_options (&opts);
    981   1.1  christos 
    982   1.1  christos   /* Respect 'set print object'.  */
    983   1.1  christos   if (opts.objectprint)
    984   1.1  christos     {
    985   1.1  christos       value = value_full_object (value, NULL, 0, 0, 0);
    986  1.11  christos       type = check_typedef (value->type ());
    987   1.1  christos     }
    988   1.1  christos 
    989  1.10  christos   gdbarch = type->arch ();
    990   1.5  christos 
    991   1.5  christos   vtable = NULL;
    992   1.9  christos   if (type->code () == TYPE_CODE_STRUCT)
    993   1.5  christos     vtable = gnuv3_get_vtable (gdbarch, type,
    994   1.5  christos 			       value_as_address (value_addr (value)));
    995   1.1  christos 
    996   1.1  christos   if (!vtable)
    997   1.1  christos     {
    998  1.10  christos       gdb_printf (_("This object does not have a virtual function table\n"));
    999   1.1  christos       return;
   1000   1.1  christos     }
   1001   1.1  christos 
   1002   1.7  christos   htab_up offset_hash (htab_create_alloc (1, hash_value_and_voffset,
   1003   1.7  christos 					  eq_value_and_voffset,
   1004   1.7  christos 					  xfree, xcalloc, xfree));
   1005   1.7  christos   std::vector<value_and_voffset *> result_vec;
   1006   1.7  christos 
   1007   1.7  christos   compute_vtable_size (offset_hash.get (), &result_vec, value);
   1008   1.7  christos   std::sort (result_vec.begin (), result_vec.end (),
   1009   1.7  christos 	     compare_value_and_voffset);
   1010   1.1  christos 
   1011   1.1  christos   count = 0;
   1012   1.7  christos   for (value_and_voffset *iter : result_vec)
   1013   1.1  christos     {
   1014   1.1  christos       if (iter->max_voffset >= 0)
   1015   1.1  christos 	{
   1016   1.1  christos 	  if (count > 0)
   1017  1.10  christos 	    gdb_printf ("\n");
   1018   1.1  christos 	  print_one_vtable (gdbarch, iter->value, iter->max_voffset, &opts);
   1019   1.1  christos 	  ++count;
   1020   1.1  christos 	}
   1021   1.1  christos     }
   1022   1.1  christos }
   1023   1.1  christos 
   1024   1.1  christos /* Return a GDB type representing `struct std::type_info', laid out
   1025   1.1  christos    appropriately for ARCH.
   1026   1.1  christos 
   1027   1.1  christos    We use this function as the gdbarch per-architecture data
   1028   1.1  christos    initialization function.  */
   1029   1.1  christos 
   1030  1.10  christos static struct type *
   1031   1.1  christos build_std_type_info_type (struct gdbarch *arch)
   1032   1.1  christos {
   1033   1.1  christos   struct type *t;
   1034   1.1  christos   int offset;
   1035   1.1  christos   struct type *void_ptr_type
   1036   1.1  christos     = builtin_type (arch)->builtin_data_ptr;
   1037   1.1  christos   struct type *char_type
   1038   1.1  christos     = builtin_type (arch)->builtin_char;
   1039   1.1  christos   struct type *char_ptr_type
   1040   1.1  christos     = make_pointer_type (make_cv_type (1, 0, char_type, NULL), NULL);
   1041   1.1  christos 
   1042  1.11  christos   t = type_allocator (arch).new_type (TYPE_CODE_STRUCT, 0, nullptr);
   1043  1.11  christos 
   1044  1.11  christos   t->alloc_fields (2);
   1045  1.11  christos 
   1046   1.1  christos   offset = 0;
   1047   1.1  christos 
   1048   1.1  christos   /* The vtable.  */
   1049  1.11  christos   {
   1050  1.11  christos     struct field &field0 = t->field (0);
   1051  1.11  christos     field0.set_name ("_vptr.type_info");
   1052  1.11  christos     field0.set_type (void_ptr_type);
   1053  1.11  christos     field0.set_loc_bitpos (offset * TARGET_CHAR_BIT);
   1054  1.11  christos     offset += field0.type ()->length ();
   1055  1.11  christos   }
   1056   1.1  christos 
   1057   1.1  christos   /* The name.  */
   1058  1.11  christos   {
   1059  1.11  christos     struct field &field1 = t->field (1);
   1060  1.11  christos     field1.set_name ("__name");
   1061  1.11  christos     field1.set_type (char_ptr_type);
   1062  1.11  christos     field1.set_loc_bitpos (offset * TARGET_CHAR_BIT);
   1063  1.11  christos     offset += field1.type ()->length ();
   1064  1.11  christos   }
   1065  1.11  christos 
   1066  1.11  christos   t->set_length (offset);
   1067  1.11  christos 
   1068   1.9  christos   t->set_name ("gdb_gnu_v3_type_info");
   1069   1.1  christos   INIT_CPLUS_SPECIFIC (t);
   1070   1.1  christos 
   1071   1.1  christos   return t;
   1072   1.1  christos }
   1073   1.1  christos 
   1074   1.1  christos /* Implement the 'get_typeid_type' method.  */
   1075   1.1  christos 
   1076   1.1  christos static struct type *
   1077   1.1  christos gnuv3_get_typeid_type (struct gdbarch *gdbarch)
   1078   1.1  christos {
   1079   1.1  christos   struct symbol *typeinfo;
   1080   1.1  christos   struct type *typeinfo_type;
   1081   1.1  christos 
   1082  1.11  christos   typeinfo = lookup_symbol ("std::type_info", NULL, SEARCH_STRUCT_DOMAIN,
   1083   1.6  christos 			    NULL).symbol;
   1084   1.1  christos   if (typeinfo == NULL)
   1085  1.10  christos     {
   1086  1.10  christos       typeinfo_type = std_type_info_gdbarch_data.get (gdbarch);
   1087  1.10  christos       if (typeinfo_type == nullptr)
   1088  1.10  christos 	{
   1089  1.10  christos 	  typeinfo_type = build_std_type_info_type (gdbarch);
   1090  1.10  christos 	  std_type_info_gdbarch_data.set (gdbarch, typeinfo_type);
   1091  1.10  christos 	}
   1092  1.10  christos     }
   1093   1.1  christos   else
   1094  1.10  christos     typeinfo_type = typeinfo->type ();
   1095   1.1  christos 
   1096   1.1  christos   return typeinfo_type;
   1097   1.1  christos }
   1098   1.1  christos 
   1099   1.1  christos /* Implement the 'get_typeid' method.  */
   1100   1.1  christos 
   1101   1.1  christos static struct value *
   1102   1.1  christos gnuv3_get_typeid (struct value *value)
   1103   1.1  christos {
   1104   1.1  christos   struct type *typeinfo_type;
   1105   1.1  christos   struct type *type;
   1106   1.1  christos   struct gdbarch *gdbarch;
   1107   1.1  christos   struct value *result;
   1108   1.9  christos   std::string type_name;
   1109   1.9  christos   gdb::unique_xmalloc_ptr<char> canonical;
   1110   1.1  christos 
   1111   1.1  christos   /* We have to handle values a bit trickily here, to allow this code
   1112   1.1  christos      to work properly with non_lvalue values that are really just
   1113   1.1  christos      disguised types.  */
   1114  1.11  christos   if (value->lval () == lval_memory)
   1115   1.1  christos     value = coerce_ref (value);
   1116   1.1  christos 
   1117  1.11  christos   type = check_typedef (value->type ());
   1118   1.1  christos 
   1119   1.1  christos   /* In the non_lvalue case, a reference might have slipped through
   1120   1.1  christos      here.  */
   1121   1.9  christos   if (type->code () == TYPE_CODE_REF)
   1122  1.10  christos     type = check_typedef (type->target_type ());
   1123   1.1  christos 
   1124   1.1  christos   /* Ignore top-level cv-qualifiers.  */
   1125   1.1  christos   type = make_cv_type (0, 0, type, NULL);
   1126  1.10  christos   gdbarch = type->arch ();
   1127   1.1  christos 
   1128   1.5  christos   type_name = type_to_string (type);
   1129   1.7  christos   if (type_name.empty ())
   1130   1.1  christos     error (_("cannot find typeinfo for unnamed type"));
   1131   1.1  christos 
   1132   1.1  christos   /* We need to canonicalize the type name here, because we do lookups
   1133   1.1  christos      using the demangled name, and so we must match the format it
   1134   1.1  christos      uses.  E.g., GDB tends to use "const char *" as a type name, but
   1135   1.1  christos      the demangler uses "char const *".  */
   1136   1.7  christos   canonical = cp_canonicalize_string (type_name.c_str ());
   1137   1.9  christos   const char *name = (canonical == nullptr
   1138   1.9  christos 		      ? type_name.c_str ()
   1139   1.9  christos 		      : canonical.get ());
   1140   1.1  christos 
   1141   1.1  christos   typeinfo_type = gnuv3_get_typeid_type (gdbarch);
   1142   1.1  christos 
   1143   1.1  christos   /* We check for lval_memory because in the "typeid (type-id)" case,
   1144   1.1  christos      the type is passed via a not_lval value object.  */
   1145   1.9  christos   if (type->code () == TYPE_CODE_STRUCT
   1146  1.11  christos       && value->lval () == lval_memory
   1147   1.1  christos       && gnuv3_dynamic_class (type))
   1148   1.1  christos     {
   1149   1.1  christos       struct value *vtable, *typeinfo_value;
   1150  1.11  christos       CORE_ADDR address = value->address () + value->embedded_offset ();
   1151   1.1  christos 
   1152   1.1  christos       vtable = gnuv3_get_vtable (gdbarch, type, address);
   1153   1.1  christos       if (vtable == NULL)
   1154   1.7  christos 	error (_("cannot find typeinfo for object of type '%s'"),
   1155   1.9  christos 	       name);
   1156   1.1  christos       typeinfo_value = value_field (vtable, vtable_field_type_info);
   1157   1.1  christos       result = value_ind (value_cast (make_pointer_type (typeinfo_type, NULL),
   1158   1.1  christos 				      typeinfo_value));
   1159   1.1  christos     }
   1160   1.1  christos   else
   1161   1.1  christos     {
   1162   1.9  christos       std::string sym_name = std::string ("typeinfo for ") + name;
   1163   1.7  christos       bound_minimal_symbol minsym
   1164   1.7  christos 	= lookup_minimal_symbol (sym_name.c_str (), NULL, NULL);
   1165   1.1  christos 
   1166   1.3  christos       if (minsym.minsym == NULL)
   1167   1.9  christos 	error (_("could not find typeinfo symbol for '%s'"), name);
   1168   1.1  christos 
   1169  1.10  christos       result = value_at_lazy (typeinfo_type, minsym.value_address ());
   1170   1.1  christos     }
   1171   1.1  christos 
   1172   1.1  christos   return result;
   1173   1.1  christos }
   1174   1.1  christos 
   1175   1.1  christos /* Implement the 'get_typename_from_type_info' method.  */
   1176   1.1  christos 
   1177   1.7  christos static std::string
   1178   1.1  christos gnuv3_get_typename_from_type_info (struct value *type_info_ptr)
   1179   1.1  christos {
   1180  1.11  christos   struct gdbarch *gdbarch = type_info_ptr->type ()->arch ();
   1181   1.1  christos   struct bound_minimal_symbol typeinfo_sym;
   1182   1.1  christos   CORE_ADDR addr;
   1183   1.1  christos   const char *symname;
   1184   1.1  christos   const char *class_name;
   1185   1.1  christos   const char *atsign;
   1186   1.1  christos 
   1187   1.1  christos   addr = value_as_address (type_info_ptr);
   1188   1.1  christos   typeinfo_sym = lookup_minimal_symbol_by_pc (addr);
   1189   1.1  christos   if (typeinfo_sym.minsym == NULL)
   1190   1.1  christos     error (_("could not find minimal symbol for typeinfo address %s"),
   1191   1.1  christos 	   paddress (gdbarch, addr));
   1192   1.1  christos 
   1193   1.1  christos #define TYPEINFO_PREFIX "typeinfo for "
   1194   1.1  christos #define TYPEINFO_PREFIX_LEN (sizeof (TYPEINFO_PREFIX) - 1)
   1195   1.9  christos   symname = typeinfo_sym.minsym->demangled_name ();
   1196   1.1  christos   if (symname == NULL || strncmp (symname, TYPEINFO_PREFIX,
   1197   1.1  christos 				  TYPEINFO_PREFIX_LEN))
   1198   1.1  christos     error (_("typeinfo symbol '%s' has unexpected name"),
   1199   1.9  christos 	   typeinfo_sym.minsym->linkage_name ());
   1200   1.1  christos   class_name = symname + TYPEINFO_PREFIX_LEN;
   1201   1.1  christos 
   1202   1.1  christos   /* Strip off @plt and version suffixes.  */
   1203   1.1  christos   atsign = strchr (class_name, '@');
   1204   1.1  christos   if (atsign != NULL)
   1205   1.7  christos     return std::string (class_name, atsign - class_name);
   1206   1.7  christos   return class_name;
   1207   1.1  christos }
   1208   1.1  christos 
   1209   1.1  christos /* Implement the 'get_type_from_type_info' method.  */
   1210   1.1  christos 
   1211   1.1  christos static struct type *
   1212   1.1  christos gnuv3_get_type_from_type_info (struct value *type_info_ptr)
   1213   1.1  christos {
   1214   1.1  christos   /* We have to parse the type name, since in general there is not a
   1215   1.1  christos      symbol for a type.  This is somewhat bogus since there may be a
   1216   1.1  christos      mis-parse.  Another approach might be to re-use the demangler's
   1217   1.1  christos      internal form to reconstruct the type somehow.  */
   1218   1.7  christos   std::string type_name = gnuv3_get_typename_from_type_info (type_info_ptr);
   1219   1.7  christos   expression_up expr (parse_expression (type_name.c_str ()));
   1220  1.11  christos   struct value *type_val = expr->evaluate_type ();
   1221  1.11  christos   return type_val->type ();
   1222   1.1  christos }
   1223   1.1  christos 
   1224   1.1  christos /* Determine if we are currently in a C++ thunk.  If so, get the address
   1225   1.1  christos    of the routine we are thunking to and continue to there instead.  */
   1226   1.1  christos 
   1227   1.1  christos static CORE_ADDR
   1228  1.11  christos gnuv3_skip_trampoline (const frame_info_ptr &frame, CORE_ADDR stop_pc)
   1229   1.1  christos {
   1230   1.1  christos   CORE_ADDR real_stop_pc, method_stop_pc, func_addr;
   1231   1.1  christos   struct gdbarch *gdbarch = get_frame_arch (frame);
   1232   1.3  christos   struct bound_minimal_symbol thunk_sym, fn_sym;
   1233   1.1  christos   struct obj_section *section;
   1234   1.1  christos   const char *thunk_name, *fn_name;
   1235   1.1  christos 
   1236   1.1  christos   real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
   1237   1.1  christos   if (real_stop_pc == 0)
   1238   1.1  christos     real_stop_pc = stop_pc;
   1239   1.1  christos 
   1240   1.1  christos   /* Find the linker symbol for this potential thunk.  */
   1241   1.3  christos   thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
   1242   1.1  christos   section = find_pc_section (real_stop_pc);
   1243   1.3  christos   if (thunk_sym.minsym == NULL || section == NULL)
   1244   1.1  christos     return 0;
   1245   1.1  christos 
   1246   1.1  christos   /* The symbol's demangled name should be something like "virtual
   1247   1.1  christos      thunk to FUNCTION", where FUNCTION is the name of the function
   1248   1.1  christos      being thunked to.  */
   1249   1.9  christos   thunk_name = thunk_sym.minsym->demangled_name ();
   1250   1.1  christos   if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
   1251   1.1  christos     return 0;
   1252   1.1  christos 
   1253   1.1  christos   fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
   1254   1.1  christos   fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile);
   1255   1.3  christos   if (fn_sym.minsym == NULL)
   1256   1.1  christos     return 0;
   1257   1.1  christos 
   1258  1.10  christos   method_stop_pc = fn_sym.value_address ();
   1259   1.1  christos 
   1260   1.1  christos   /* Some targets have minimal symbols pointing to function descriptors
   1261   1.1  christos      (powerpc 64 for example).  Make sure to retrieve the address
   1262   1.1  christos      of the real function from the function descriptor before passing on
   1263   1.1  christos      the address to other layers of GDB.  */
   1264  1.10  christos   func_addr = gdbarch_convert_from_func_ptr_addr
   1265  1.10  christos     (gdbarch, method_stop_pc, current_inferior ()->top_target ());
   1266   1.1  christos   if (func_addr != 0)
   1267   1.1  christos     method_stop_pc = func_addr;
   1268   1.1  christos 
   1269   1.1  christos   real_stop_pc = gdbarch_skip_trampoline_code
   1270   1.1  christos 		   (gdbarch, frame, method_stop_pc);
   1271   1.1  christos   if (real_stop_pc == 0)
   1272   1.1  christos     real_stop_pc = method_stop_pc;
   1273   1.1  christos 
   1274   1.1  christos   return real_stop_pc;
   1275   1.1  christos }
   1276   1.1  christos 
   1277   1.9  christos /* A member function is in one these states.  */
   1278   1.9  christos 
   1279   1.9  christos enum definition_style
   1280   1.9  christos {
   1281   1.9  christos   DOES_NOT_EXIST_IN_SOURCE,
   1282   1.9  christos   DEFAULTED_INSIDE,
   1283   1.9  christos   DEFAULTED_OUTSIDE,
   1284   1.9  christos   DELETED,
   1285   1.9  christos   EXPLICIT,
   1286   1.9  christos };
   1287   1.9  christos 
   1288   1.9  christos /* Return how the given field is defined.  */
   1289   1.9  christos 
   1290   1.9  christos static definition_style
   1291   1.9  christos get_def_style (struct fn_field *fn, int fieldelem)
   1292   1.9  christos {
   1293   1.9  christos   if (TYPE_FN_FIELD_DELETED (fn, fieldelem))
   1294   1.9  christos     return DELETED;
   1295   1.9  christos 
   1296   1.9  christos   if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
   1297   1.9  christos     return DOES_NOT_EXIST_IN_SOURCE;
   1298   1.9  christos 
   1299   1.9  christos   switch (TYPE_FN_FIELD_DEFAULTED (fn, fieldelem))
   1300   1.9  christos     {
   1301   1.9  christos     case DW_DEFAULTED_no:
   1302   1.9  christos       return EXPLICIT;
   1303   1.9  christos     case DW_DEFAULTED_in_class:
   1304   1.9  christos       return DEFAULTED_INSIDE;
   1305   1.9  christos     case DW_DEFAULTED_out_of_class:
   1306   1.9  christos       return DEFAULTED_OUTSIDE;
   1307   1.9  christos     default:
   1308   1.9  christos       break;
   1309   1.9  christos     }
   1310   1.9  christos 
   1311   1.9  christos   return EXPLICIT;
   1312   1.9  christos }
   1313   1.9  christos 
   1314   1.9  christos /* Helper functions to determine whether the given definition style
   1315   1.9  christos    denotes that the definition is user-provided or implicit.
   1316   1.9  christos    Being defaulted outside the class decl counts as an explicit
   1317   1.9  christos    user-definition, while being defaulted inside is implicit.  */
   1318   1.9  christos 
   1319   1.9  christos static bool
   1320   1.9  christos is_user_provided_def (definition_style def)
   1321   1.9  christos {
   1322   1.9  christos   return def == EXPLICIT || def == DEFAULTED_OUTSIDE;
   1323   1.9  christos }
   1324   1.9  christos 
   1325   1.9  christos static bool
   1326   1.9  christos is_implicit_def (definition_style def)
   1327   1.9  christos {
   1328   1.9  christos   return def == DOES_NOT_EXIST_IN_SOURCE || def == DEFAULTED_INSIDE;
   1329   1.9  christos }
   1330   1.9  christos 
   1331   1.9  christos /* Helper function to decide if METHOD_TYPE is a copy/move
   1332   1.9  christos    constructor type for CLASS_TYPE.  EXPECTED is the expected
   1333   1.9  christos    type code for the "right-hand-side" argument.
   1334   1.9  christos    This function is supposed to be used by the IS_COPY_CONSTRUCTOR_TYPE
   1335   1.9  christos    and IS_MOVE_CONSTRUCTOR_TYPE functions below.  Normally, you should
   1336   1.9  christos    not need to call this directly.  */
   1337   1.9  christos 
   1338   1.9  christos static bool
   1339   1.9  christos is_copy_or_move_constructor_type (struct type *class_type,
   1340   1.9  christos 				  struct type *method_type,
   1341   1.9  christos 				  type_code expected)
   1342   1.9  christos {
   1343   1.9  christos   /* The method should take at least two arguments...  */
   1344   1.9  christos   if (method_type->num_fields () < 2)
   1345   1.9  christos     return false;
   1346   1.9  christos 
   1347   1.9  christos   /* ...and the second argument should be the same as the class
   1348   1.9  christos      type, with the expected type code...  */
   1349   1.9  christos   struct type *arg_type = method_type->field (1).type ();
   1350   1.9  christos 
   1351   1.9  christos   if (arg_type->code () != expected)
   1352   1.9  christos     return false;
   1353   1.9  christos 
   1354  1.10  christos   struct type *target = check_typedef (arg_type->target_type ());
   1355   1.9  christos   if (!(class_types_same_p (target, class_type)))
   1356   1.9  christos     return false;
   1357   1.9  christos 
   1358   1.9  christos   /* ...and if any of the remaining arguments don't have a default value
   1359   1.9  christos      then this is not a copy or move constructor, but just a
   1360   1.9  christos      constructor.  */
   1361   1.9  christos   for (int i = 2; i < method_type->num_fields (); i++)
   1362   1.9  christos     {
   1363   1.9  christos       arg_type = method_type->field (i).type ();
   1364   1.9  christos       /* FIXME aktemur/2019-10-31: As of this date, neither
   1365   1.9  christos 	 clang++-7.0.0 nor g++-8.2.0 produce a DW_AT_default_value
   1366   1.9  christos 	 attribute.  GDB is also not set to read this attribute, yet.
   1367   1.9  christos 	 Hence, we immediately return false if there are more than
   1368   1.9  christos 	 2 parameters.
   1369   1.9  christos 	 GCC bug link:
   1370   1.9  christos 	 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42959
   1371   1.9  christos       */
   1372   1.9  christos       return false;
   1373   1.9  christos     }
   1374   1.9  christos 
   1375   1.9  christos   return true;
   1376   1.9  christos }
   1377   1.9  christos 
   1378   1.9  christos /* Return true if METHOD_TYPE is a copy ctor type for CLASS_TYPE.  */
   1379   1.9  christos 
   1380   1.9  christos static bool
   1381   1.9  christos is_copy_constructor_type (struct type *class_type,
   1382   1.9  christos 			  struct type *method_type)
   1383   1.9  christos {
   1384   1.9  christos   return is_copy_or_move_constructor_type (class_type, method_type,
   1385   1.9  christos 					   TYPE_CODE_REF);
   1386   1.9  christos }
   1387   1.9  christos 
   1388   1.9  christos /* Return true if METHOD_TYPE is a move ctor type for CLASS_TYPE.  */
   1389   1.9  christos 
   1390   1.9  christos static bool
   1391   1.9  christos is_move_constructor_type (struct type *class_type,
   1392   1.9  christos 			  struct type *method_type)
   1393   1.9  christos {
   1394   1.9  christos   return is_copy_or_move_constructor_type (class_type, method_type,
   1395   1.9  christos 					   TYPE_CODE_RVALUE_REF);
   1396   1.9  christos }
   1397   1.9  christos 
   1398   1.9  christos /* Return pass-by-reference information for the given TYPE.
   1399   1.1  christos 
   1400   1.1  christos    The rule in the v3 ABI document comes from section 3.1.1.  If the
   1401   1.1  christos    type has a non-trivial copy constructor or destructor, then the
   1402   1.1  christos    caller must make a copy (by calling the copy constructor if there
   1403   1.1  christos    is one or perform the copy itself otherwise), pass the address of
   1404   1.1  christos    the copy, and then destroy the temporary (if necessary).
   1405   1.1  christos 
   1406   1.9  christos    For return values with non-trivial copy/move constructors or
   1407   1.1  christos    destructors, space will be allocated in the caller, and a pointer
   1408   1.1  christos    will be passed as the first argument (preceding "this").
   1409   1.1  christos 
   1410   1.1  christos    We don't have a bulletproof mechanism for determining whether a
   1411   1.9  christos    constructor or destructor is trivial.  For GCC and DWARF5 debug
   1412   1.9  christos    information, we can check the calling_convention attribute,
   1413   1.9  christos    the 'artificial' flag, the 'defaulted' attribute, and the
   1414   1.9  christos    'deleted' attribute.  */
   1415   1.1  christos 
   1416   1.9  christos static struct language_pass_by_ref_info
   1417   1.1  christos gnuv3_pass_by_reference (struct type *type)
   1418   1.1  christos {
   1419   1.1  christos   int fieldnum, fieldelem;
   1420   1.1  christos 
   1421   1.6  christos   type = check_typedef (type);
   1422   1.1  christos 
   1423   1.9  christos   /* Start with the default values.  */
   1424   1.9  christos   struct language_pass_by_ref_info info;
   1425   1.9  christos 
   1426   1.9  christos   bool has_cc_attr = false;
   1427   1.9  christos   bool is_pass_by_value = false;
   1428   1.9  christos   bool is_dynamic = false;
   1429   1.9  christos   definition_style cctor_def = DOES_NOT_EXIST_IN_SOURCE;
   1430   1.9  christos   definition_style dtor_def = DOES_NOT_EXIST_IN_SOURCE;
   1431   1.9  christos   definition_style mctor_def = DOES_NOT_EXIST_IN_SOURCE;
   1432   1.9  christos 
   1433   1.1  christos   /* We're only interested in things that can have methods.  */
   1434   1.9  christos   if (type->code () != TYPE_CODE_STRUCT
   1435   1.9  christos       && type->code () != TYPE_CODE_UNION)
   1436   1.9  christos     return info;
   1437   1.9  christos 
   1438   1.9  christos   /* The compiler may have emitted the calling convention attribute.
   1439   1.9  christos      Note: GCC does not produce this attribute as of version 9.2.1.
   1440   1.9  christos      Bug link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92418  */
   1441   1.9  christos   if (TYPE_CPLUS_CALLING_CONVENTION (type) == DW_CC_pass_by_value)
   1442   1.9  christos     {
   1443   1.9  christos       has_cc_attr = true;
   1444   1.9  christos       is_pass_by_value = true;
   1445   1.9  christos       /* Do not return immediately.  We have to find out if this type
   1446   1.9  christos 	 is copy_constructible and destructible.  */
   1447   1.9  christos     }
   1448   1.9  christos 
   1449   1.9  christos   if (TYPE_CPLUS_CALLING_CONVENTION (type) == DW_CC_pass_by_reference)
   1450   1.9  christos     {
   1451   1.9  christos       has_cc_attr = true;
   1452   1.9  christos       is_pass_by_value = false;
   1453   1.9  christos     }
   1454   1.1  christos 
   1455   1.3  christos   /* A dynamic class has a non-trivial copy constructor.
   1456   1.3  christos      See c++98 section 12.8 Copying class objects [class.copy].  */
   1457   1.3  christos   if (gnuv3_dynamic_class (type))
   1458   1.9  christos     is_dynamic = true;
   1459   1.3  christos 
   1460   1.1  christos   for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
   1461   1.1  christos     for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
   1462   1.1  christos 	 fieldelem++)
   1463   1.1  christos       {
   1464   1.1  christos 	struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
   1465   1.1  christos 	const char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
   1466   1.1  christos 	struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
   1467   1.1  christos 
   1468   1.1  christos 	if (name[0] == '~')
   1469   1.3  christos 	  {
   1470   1.9  christos 	    /* We've found a destructor.
   1471   1.9  christos 	       There should be at most one dtor definition.  */
   1472   1.9  christos 	    gdb_assert (dtor_def == DOES_NOT_EXIST_IN_SOURCE);
   1473   1.9  christos 	    dtor_def = get_def_style (fn, fieldelem);
   1474   1.9  christos 	  }
   1475   1.9  christos 	else if (is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem))
   1476   1.9  christos 		 || TYPE_FN_FIELD_CONSTRUCTOR (fn, fieldelem))
   1477   1.9  christos 	  {
   1478   1.9  christos 	    /* FIXME drow/2007-09-23: We could do this using the name of
   1479   1.9  christos 	       the method and the name of the class instead of dealing
   1480   1.9  christos 	       with the mangled name.  We don't have a convenient function
   1481   1.9  christos 	       to strip off both leading scope qualifiers and trailing
   1482   1.9  christos 	       template arguments yet.  */
   1483   1.9  christos 	    if (is_copy_constructor_type (type, fieldtype))
   1484   1.9  christos 	      {
   1485   1.9  christos 		/* There may be more than one cctors.  E.g.: one that
   1486   1.9  christos 		   take a const parameter and another that takes a
   1487   1.9  christos 		   non-const parameter.  Such as:
   1488   1.9  christos 
   1489   1.9  christos 		   class K {
   1490   1.9  christos 		     K (const K &k)...
   1491   1.9  christos 		     K (K &k)...
   1492   1.9  christos 		   };
   1493   1.9  christos 
   1494   1.9  christos 		   It is sufficient for the type to be non-trivial
   1495   1.9  christos 		   even only one of the cctors is explicit.
   1496   1.9  christos 		   Therefore, update the cctor_def value in the
   1497   1.9  christos 		   implicit -> explicit direction, not backwards.  */
   1498   1.3  christos 
   1499   1.9  christos 		if (is_implicit_def (cctor_def))
   1500   1.9  christos 		  cctor_def = get_def_style (fn, fieldelem);
   1501   1.9  christos 	      }
   1502   1.9  christos 	    else if (is_move_constructor_type (type, fieldtype))
   1503   1.3  christos 	      {
   1504   1.9  christos 		/* Again, there may be multiple move ctors.  Update the
   1505   1.9  christos 		   mctor_def value if we found an explicit def and the
   1506   1.9  christos 		   existing one is not explicit.  Otherwise retain the
   1507   1.9  christos 		   existing value.  */
   1508   1.9  christos 		if (is_implicit_def (mctor_def))
   1509   1.9  christos 		  mctor_def = get_def_style (fn, fieldelem);
   1510   1.3  christos 	      }
   1511   1.3  christos 	  }
   1512   1.1  christos       }
   1513   1.1  christos 
   1514   1.9  christos   bool cctor_implicitly_deleted
   1515   1.9  christos     = (mctor_def != DOES_NOT_EXIST_IN_SOURCE
   1516   1.9  christos        && cctor_def == DOES_NOT_EXIST_IN_SOURCE);
   1517   1.9  christos 
   1518   1.9  christos   bool cctor_explicitly_deleted = (cctor_def == DELETED);
   1519   1.9  christos 
   1520   1.9  christos   if (cctor_implicitly_deleted || cctor_explicitly_deleted)
   1521   1.9  christos     info.copy_constructible = false;
   1522   1.9  christos 
   1523   1.9  christos   if (dtor_def == DELETED)
   1524   1.9  christos     info.destructible = false;
   1525   1.9  christos 
   1526   1.9  christos   info.trivially_destructible = is_implicit_def (dtor_def);
   1527   1.9  christos 
   1528   1.9  christos   info.trivially_copy_constructible
   1529   1.9  christos     = (is_implicit_def (cctor_def)
   1530   1.9  christos        && !is_dynamic);
   1531   1.9  christos 
   1532   1.9  christos   info.trivially_copyable
   1533   1.9  christos     = (info.trivially_copy_constructible
   1534   1.9  christos        && info.trivially_destructible
   1535   1.9  christos        && !is_user_provided_def (mctor_def));
   1536   1.9  christos 
   1537   1.1  christos   /* Even if all the constructors and destructors were artificial, one
   1538   1.1  christos      of them may have invoked a non-artificial constructor or
   1539   1.1  christos      destructor in a base class.  If any base class needs to be passed
   1540   1.1  christos      by reference, so does this class.  Similarly for members, which
   1541   1.1  christos      are constructed whenever this class is.  We do not need to worry
   1542   1.1  christos      about recursive loops here, since we are only looking at members
   1543   1.1  christos      of complete class type.  Also ignore any static members.  */
   1544   1.9  christos   for (fieldnum = 0; fieldnum < type->num_fields (); fieldnum++)
   1545  1.11  christos     if (!type->field (fieldnum).is_static ())
   1546   1.9  christos       {
   1547   1.9  christos 	struct type *field_type = type->field (fieldnum).type ();
   1548   1.9  christos 
   1549   1.9  christos 	/* For arrays, make the decision based on the element type.  */
   1550   1.9  christos 	if (field_type->code () == TYPE_CODE_ARRAY)
   1551  1.10  christos 	  field_type = check_typedef (field_type->target_type ());
   1552   1.9  christos 
   1553   1.9  christos 	struct language_pass_by_ref_info field_info
   1554   1.9  christos 	  = gnuv3_pass_by_reference (field_type);
   1555   1.9  christos 
   1556   1.9  christos 	if (!field_info.copy_constructible)
   1557   1.9  christos 	  info.copy_constructible = false;
   1558   1.9  christos 	if (!field_info.destructible)
   1559   1.9  christos 	  info.destructible = false;
   1560   1.9  christos 	if (!field_info.trivially_copyable)
   1561   1.9  christos 	  info.trivially_copyable = false;
   1562   1.9  christos 	if (!field_info.trivially_copy_constructible)
   1563   1.9  christos 	  info.trivially_copy_constructible = false;
   1564   1.9  christos 	if (!field_info.trivially_destructible)
   1565   1.9  christos 	  info.trivially_destructible = false;
   1566   1.9  christos       }
   1567   1.1  christos 
   1568   1.9  christos   /* Consistency check.  */
   1569   1.9  christos   if (has_cc_attr && info.trivially_copyable != is_pass_by_value)
   1570   1.9  christos     {
   1571   1.9  christos       /* DWARF CC attribute is not the same as the inferred value;
   1572   1.9  christos 	 use the DWARF attribute.  */
   1573   1.9  christos       info.trivially_copyable = is_pass_by_value;
   1574   1.9  christos     }
   1575   1.9  christos 
   1576   1.9  christos   return info;
   1577   1.1  christos }
   1578   1.1  christos 
   1579   1.1  christos static void
   1580   1.1  christos init_gnuv3_ops (void)
   1581   1.1  christos {
   1582   1.1  christos   gnu_v3_abi_ops.shortname = "gnu-v3";
   1583   1.1  christos   gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
   1584   1.1  christos   gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
   1585   1.1  christos   gnu_v3_abi_ops.is_destructor_name =
   1586   1.1  christos     (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
   1587   1.1  christos   gnu_v3_abi_ops.is_constructor_name =
   1588   1.1  christos     (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
   1589   1.1  christos   gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
   1590   1.1  christos   gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
   1591   1.1  christos   gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
   1592   1.1  christos   gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
   1593   1.1  christos   gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
   1594   1.1  christos   gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
   1595   1.1  christos   gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
   1596   1.1  christos   gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
   1597   1.1  christos   gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
   1598   1.1  christos   gnu_v3_abi_ops.print_vtable = gnuv3_print_vtable;
   1599   1.1  christos   gnu_v3_abi_ops.get_typeid = gnuv3_get_typeid;
   1600   1.1  christos   gnu_v3_abi_ops.get_typeid_type = gnuv3_get_typeid_type;
   1601   1.1  christos   gnu_v3_abi_ops.get_type_from_type_info = gnuv3_get_type_from_type_info;
   1602   1.1  christos   gnu_v3_abi_ops.get_typename_from_type_info
   1603   1.1  christos     = gnuv3_get_typename_from_type_info;
   1604   1.1  christos   gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
   1605   1.1  christos   gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
   1606   1.1  christos }
   1607   1.1  christos 
   1608   1.9  christos void _initialize_gnu_v3_abi ();
   1609   1.1  christos void
   1610   1.9  christos _initialize_gnu_v3_abi ()
   1611   1.1  christos {
   1612   1.1  christos   init_gnuv3_ops ();
   1613   1.1  christos 
   1614   1.1  christos   register_cp_abi (&gnu_v3_abi_ops);
   1615   1.1  christos   set_cp_abi_as_auto_default (gnu_v3_abi_ops.shortname);
   1616   1.1  christos }
   1617