static int gnuv2_baseclass_offset (struct type *type, int index, const bfd_byte *valaddr, int embedded_offset, CORE_ADDR address, const struct value *val) { struct type *basetype = TYPE_BASECLASS (type, index); if (BASETYPE_VIA_VIRTUAL (type, index)) { /* Must hunt for the pointer to this virtual baseclass. */ int i, len = TYPE_NFIELDS (type); int n_baseclasses = TYPE_N_BASECLASSES (type); /* First look for the virtual baseclass pointer in the fields. */ for (i = n_baseclasses; i < len; i++) { if (vb_match (type, i, basetype)) { struct type *field_type; int field_offset; int field_length; CORE_ADDR addr; field_type = check_typedef (TYPE_FIELD_TYPE (type, i)); field_offset = TYPE_FIELD_BITPOS (type, i) / 8; field_length = TYPE_LENGTH (field_type); if (!value_bytes_available (val, embedded_offset + field_offset, field_length)) throw_error (NOT_AVAILABLE_ERROR, _("Virtual baseclass pointer is not available")); addr = unpack_pointer (field_type, valaddr + embedded_offset + field_offset); return addr - (LONGEST) address + embedded_offset; } } /* Not in the fields, so try looking through the baseclasses. */ for (i = index + 1; i < n_baseclasses; i++) { /* Don't go through baseclass_offset, as that wraps exceptions, thus, inner exceptions would be wrapped more than once. */ int boffset = gnuv2_baseclass_offset (type, i, valaddr, embedded_offset, address, val); if (boffset) return boffset; } error (_("Baseclass offset not found")); } /* Baseclass is easily computed. */ return TYPE_BASECLASS_BITPOS (type, index) / 8; }
static int gnuv3_baseclass_offset (struct type *type, int index, const bfd_byte *valaddr, int embedded_offset, CORE_ADDR address, const struct value *val) { struct gdbarch *gdbarch; struct type *ptr_type; struct value *vtable; struct value *vbase_array; long int cur_base_offset, base_offset; /* Determine architecture. */ gdbarch = get_type_arch (type); ptr_type = builtin_type (gdbarch)->builtin_data_ptr; /* If it isn't a virtual base, this is easy. The offset is in the type definition. Likewise for Java, which doesn't really have virtual inheritance in the C++ sense. */ if (!BASETYPE_VIA_VIRTUAL (type, index) || TYPE_CPLUS_REALLY_JAVA (type)) return TYPE_BASECLASS_BITPOS (type, index) / 8; /* To access a virtual base, we need to use the vbase offset stored in our vtable. Recent GCC versions provide this information. If it isn't available, we could get what we needed from RTTI, or from drawing the complete inheritance graph based on the debug info. Neither is worthwhile. */ cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8; if (cur_base_offset >= - vtable_address_point_offset (gdbarch)) error (_("Expected a negative vbase offset (old compiler?)")); cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch); if ((- cur_base_offset) % TYPE_LENGTH (ptr_type) != 0) error (_("Misaligned vbase offset.")); cur_base_offset = cur_base_offset / ((int) TYPE_LENGTH (ptr_type)); vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset); gdb_assert (vtable != NULL); vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets); base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset)); return base_offset; }
static const char * gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset, LONGEST adjustment) { int i; const char *physname; /* Search this class first. */ physname = NULL; if (adjustment == 0) { int len; len = TYPE_NFN_FIELDS (domain); for (i = 0; i < len; i++) { int len2, j; struct fn_field *f; f = TYPE_FN_FIELDLIST1 (domain, i); len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i); check_stub_method_group (domain, i); for (j = 0; j < len2; j++) if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset) return TYPE_FN_FIELD_PHYSNAME (f, j); } } /* Next search non-virtual bases. If it's in a virtual base, we're out of luck. */ for (i = 0; i < TYPE_N_BASECLASSES (domain); i++) { int pos; struct type *basetype; if (BASETYPE_VIA_VIRTUAL (domain, i)) continue; pos = TYPE_BASECLASS_BITPOS (domain, i) / 8; basetype = TYPE_FIELD_TYPE (domain, i); /* Recurse with a modified adjustment. We don't need to adjust voffset. */ if (adjustment >= pos && adjustment < pos + TYPE_LENGTH (basetype)) return gnuv3_find_method_in (basetype, voffset, adjustment - pos); } return NULL; }
int gnuv2_baseclass_offset (struct type *type, int index, const bfd_byte *valaddr, CORE_ADDR address) { struct type *basetype = TYPE_BASECLASS (type, index); if (BASETYPE_VIA_VIRTUAL (type, index)) { /* Must hunt for the pointer to this virtual baseclass. */ int i, len = TYPE_NFIELDS (type); int n_baseclasses = TYPE_N_BASECLASSES (type); /* First look for the virtual baseclass pointer in the fields. */ for (i = n_baseclasses; i < len; i++) { if (vb_match (type, i, basetype)) { CORE_ADDR addr = unpack_pointer (TYPE_FIELD_TYPE (type, i), valaddr + (TYPE_FIELD_BITPOS (type, i) / 8)); return addr - (LONGEST) address; } } /* Not in the fields, so try looking through the baseclasses. */ for (i = index + 1; i < n_baseclasses; i++) { int boffset = baseclass_offset (type, i, valaddr, address); if (boffset) return boffset; } /* Not found. */ return -1; } /* Baseclass is easily computed. */ return TYPE_BASECLASS_BITPOS (type, index) / 8; }
static struct type * gnuv2_value_rtti_type (struct value *v, int *full, int *top, int *using_enc) { struct type *known_type; struct type *rtti_type; CORE_ADDR vtbl; struct minimal_symbol *minsym; char *demangled_name; struct type *btype; if (full) *full = 0; if (top) *top = -1; if (using_enc) *using_enc = 0; /* Get declared type */ known_type = value_type (v); CHECK_TYPEDEF (known_type); /* RTTI works only or class objects */ if (TYPE_CODE (known_type) != TYPE_CODE_CLASS) return NULL; /* Plan on this changing in the future as i get around to setting the vtables properly for G++ compiled stuff. Also, I'll be using the type info functions, which are always right. Deal with it until then. JCI - This pretty much useless. This gets the "true" type correctly when there is single inheritance - but in all such cases that I could find gdb already knows that. In cases where this points INTO the object (like non-virtual diamond graphs) the demangled name is something like OUTER::INNER and this is not a symbol gdb can resolve, so we fail & return NULL anyway. Seems like this really isn't going to work till we actually call the RTTI function & parse it. */ /* If the type has no vptr fieldno, try to get it filled in */ if (TYPE_VPTR_FIELDNO(known_type) < 0) fill_in_vptr_fieldno(known_type); /* If we still can't find one, give up */ if (TYPE_VPTR_FIELDNO(known_type) < 0) return NULL; /* Make sure our basetype and known type match, otherwise, cast so we can get at the vtable properly. */ btype = TYPE_VPTR_BASETYPE (known_type); CHECK_TYPEDEF (btype); if (btype != known_type ) { v = value_cast (btype, v); if (using_enc) *using_enc=1; } /* We can't use value_ind here, because it would want to use RTTI, and we'd waste a bunch of time figuring out we already know the type. Besides, we don't care about the type, just the actual pointer */ if (VALUE_ADDRESS (value_field (v, TYPE_VPTR_FIELDNO (known_type))) == 0) return NULL; vtbl=value_as_address(value_field(v,TYPE_VPTR_FIELDNO(known_type))); /* Try to find a symbol that is the vtable */ minsym=lookup_minimal_symbol_by_pc(vtbl); if (minsym==NULL || (demangled_name=DEPRECATED_SYMBOL_NAME (minsym))==NULL || !is_vtable_name (demangled_name)) return NULL; /* If we just skip the prefix, we get screwed by namespaces */ demangled_name=cplus_demangle(demangled_name,DMGL_PARAMS|DMGL_ANSI); *(strchr(demangled_name,' '))=0; /* Lookup the type for the name */ /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */ rtti_type = cp_lookup_rtti_type (demangled_name, NULL); if (rtti_type == NULL) return NULL; if (TYPE_N_BASECLASSES(rtti_type) > 1 && full && (*full) != 1) { if (top) *top=TYPE_BASECLASS_BITPOS(rtti_type,TYPE_VPTR_FIELDNO(rtti_type))/8; if (top && ((*top) >0)) { if (TYPE_LENGTH(rtti_type) > TYPE_LENGTH(known_type)) { if (full) *full=0; } else { if (full) *full=1; } } } else { if (full) *full=1; } return rtti_type; }
static struct type * gnuv2_value_rtti_type (struct value *v, int *full, int *top, int *using_enc) { struct type *known_type; struct type *rtti_type; CORE_ADDR vtbl; struct bound_minimal_symbol minsym; char *demangled_name, *p; const char *linkage_name; struct type *btype; struct type *known_type_vptr_basetype; int known_type_vptr_fieldno; if (full) *full = 0; if (top) *top = -1; if (using_enc) *using_enc = 0; /* Get declared type. */ known_type = value_type (v); CHECK_TYPEDEF (known_type); /* RTTI works only or class objects. */ if (TYPE_CODE (known_type) != TYPE_CODE_STRUCT) return NULL; /* Plan on this changing in the future as i get around to setting the vtables properly for G++ compiled stuff. Also, I'll be using the type info functions, which are always right. Deal with it until then. */ /* Try to get the vptr basetype, fieldno. */ known_type_vptr_fieldno = get_vptr_fieldno (known_type, &known_type_vptr_basetype); /* If we can't find it, give up. */ if (known_type_vptr_fieldno < 0) return NULL; /* Make sure our basetype and known type match, otherwise, cast so we can get at the vtable properly. */ btype = known_type_vptr_basetype; CHECK_TYPEDEF (btype); if (btype != known_type ) { v = value_cast (btype, v); if (using_enc) *using_enc=1; } /* We can't use value_ind here, because it would want to use RTTI, and we'd waste a bunch of time figuring out we already know the type. Besides, we don't care about the type, just the actual pointer. */ if (value_address (value_field (v, known_type_vptr_fieldno)) == 0) return NULL; vtbl = value_as_address (value_field (v, known_type_vptr_fieldno)); /* Try to find a symbol that is the vtable. */ minsym=lookup_minimal_symbol_by_pc(vtbl); if (minsym.minsym==NULL || (linkage_name=MSYMBOL_LINKAGE_NAME (minsym.minsym))==NULL || !is_vtable_name (linkage_name)) return NULL; /* If we just skip the prefix, we get screwed by namespaces. */ demangled_name=gdb_demangle(linkage_name,DMGL_PARAMS|DMGL_ANSI); p = strchr (demangled_name, ' '); if (p) *p = '\0'; /* Lookup the type for the name. */ /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */ rtti_type = cp_lookup_rtti_type (demangled_name, NULL); if (rtti_type == NULL) return NULL; if (TYPE_N_BASECLASSES(rtti_type) > 1 && full && (*full) != 1) { if (top) *top = TYPE_BASECLASS_BITPOS (rtti_type, TYPE_VPTR_FIELDNO(rtti_type)) / 8; if (top && ((*top) >0)) { if (TYPE_LENGTH(rtti_type) > TYPE_LENGTH(known_type)) { if (full) *full=0; } else { if (full) *full=1; } } } else { if (full) *full=1; } return rtti_type; }
/* Compute the offset of the baseclass which is the INDEXth baseclass of class TYPE, for value at VALADDR (in host) at ADDRESS (in target). The result is the offset of the baseclass value relative to (the address of)(ARG) + OFFSET. -1 is returned on error. */ static int gnuv3_baseclass_offset (struct type *type, int index, const bfd_byte *valaddr, CORE_ADDR address) { struct gdbarch *gdbarch; struct type *vtable_type; struct type *ptr_type; struct value *vtable; struct type *vbasetype; struct value *offset_val, *vbase_array; CORE_ADDR vtable_address; long int cur_base_offset, base_offset; int vbasetype_vptr_fieldno; /* Determine architecture. */ gdbarch = get_class_arch (type); vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data); ptr_type = builtin_type (gdbarch)->builtin_data_ptr; /* If it isn't a virtual base, this is easy. The offset is in the type definition. */ if (!BASETYPE_VIA_VIRTUAL (type, index)) return TYPE_BASECLASS_BITPOS (type, index) / 8; /* To access a virtual base, we need to use the vbase offset stored in our vtable. Recent GCC versions provide this information. If it isn't available, we could get what we needed from RTTI, or from drawing the complete inheritance graph based on the debug info. Neither is worthwhile. */ cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8; if (cur_base_offset >= - vtable_address_point_offset (gdbarch)) error (_("Expected a negative vbase offset (old compiler?)")); cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch); if ((- cur_base_offset) % TYPE_LENGTH (ptr_type) != 0) error (_("Misaligned vbase offset.")); cur_base_offset = cur_base_offset / ((int) TYPE_LENGTH (ptr_type)); /* We're now looking for the cur_base_offset'th entry (negative index) in the vcall_and_vbase_offsets array. We used to cast the object to its TYPE_VPTR_BASETYPE, and reference the vtable as TYPE_VPTR_FIELDNO; however, that cast can not be done without calling baseclass_offset again if the TYPE_VPTR_BASETYPE is a virtual base class, as described in the v3 C++ ABI Section 2.4.I.2.b. Fortunately the ABI guarantees that the vtable pointer will be located at the beginning of the object, so we can bypass the casting. Verify that the TYPE_VPTR_FIELDNO is in fact at the start of whichever baseclass it resides in, as a sanity measure - iff we have debugging information for that baseclass. */ vbasetype = check_typedef (TYPE_VPTR_BASETYPE (type)); vbasetype_vptr_fieldno = get_vptr_fieldno (vbasetype, NULL); if (vbasetype_vptr_fieldno >= 0 && TYPE_FIELD_BITPOS (vbasetype, vbasetype_vptr_fieldno) != 0) error (_("Illegal vptr offset in class %s"), TYPE_NAME (vbasetype) ? TYPE_NAME (vbasetype) : "<unknown>"); vtable_address = value_as_address (value_at_lazy (ptr_type, address)); vtable = value_at_lazy (vtable_type, vtable_address - vtable_address_point_offset (gdbarch)); offset_val = value_from_longest (builtin_type_int32, cur_base_offset); vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets); base_offset = value_as_long (value_subscript (vbase_array, offset_val)); return base_offset; }