#include "defs.h"
#include "value.h"
#include "cp-abi.h"
#include "cp-support.h"
#include "demangle.h"
#include "gdb_assert.h"
#include "gdb_string.h"
static struct cp_abi_ops gnu_v3_abi_ops;
static int
gnuv3_is_vtable_name (const char *name)
{
return strncmp (name, "_ZTV", 4) == 0;
}
static int
gnuv3_is_operator_name (const char *name)
{
return strncmp (name, "operator", 8) == 0;
}
static struct gdbarch_data *vtable_type_gdbarch_data;
enum {
vtable_field_vcall_and_vbase_offsets,
vtable_field_offset_to_top,
vtable_field_type_info,
vtable_field_virtual_functions
};
static void *
build_gdb_vtable_type (struct gdbarch *arch)
{
struct type *t;
struct field *field_list, *field;
int offset;
struct type *void_ptr_type
= lookup_pointer_type (builtin_type_void);
struct type *ptr_to_void_fn_type
= lookup_pointer_type (lookup_function_type (builtin_type_void));
struct type *ptrdiff_type
= init_type (TYPE_CODE_INT, TARGET_PTR_BIT / TARGET_CHAR_BIT, 0,
"ptrdiff_t", 0);
field_list = xmalloc (sizeof (struct field [4]));
memset (field_list, 0, sizeof (struct field [4]));
field = &field_list[0];
offset = 0;
FIELD_NAME (*field) = "vcall_and_vbase_offsets";
FIELD_TYPE (*field)
= create_array_type (0, ptrdiff_type,
create_range_type (0, builtin_type_int, 0, -1));
FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
offset += TYPE_LENGTH (FIELD_TYPE (*field));
field++;
FIELD_NAME (*field) = "offset_to_top";
FIELD_TYPE (*field) = ptrdiff_type;
FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
offset += TYPE_LENGTH (FIELD_TYPE (*field));
field++;
FIELD_NAME (*field) = "type_info";
FIELD_TYPE (*field) = void_ptr_type;
FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
offset += TYPE_LENGTH (FIELD_TYPE (*field));
field++;
FIELD_NAME (*field) = "virtual_functions";
FIELD_TYPE (*field)
= create_array_type (0, ptr_to_void_fn_type,
create_range_type (0, builtin_type_int, 0, -1));
FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
offset += TYPE_LENGTH (FIELD_TYPE (*field));
field++;
gdb_assert (field == (field_list + 4));
t = init_type (TYPE_CODE_STRUCT, offset, 0, 0, 0);
TYPE_NFIELDS (t) = field - field_list;
TYPE_FIELDS (t) = field_list;
TYPE_TAG_NAME (t) = "gdb_gnu_v3_abi_vtable";
return t;
}
static int
vtable_address_point_offset (void)
{
struct type *vtable_type = gdbarch_data (current_gdbarch,
vtable_type_gdbarch_data);
return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions)
/ TARGET_CHAR_BIT);
}
static struct type *
gnuv3_rtti_type (struct value *value,
int *full_p, int *top_p, int *using_enc_p)
{
struct type *vtable_type = gdbarch_data (current_gdbarch,
vtable_type_gdbarch_data);
struct type *values_type = check_typedef (value_type (value));
CORE_ADDR vtable_address;
struct value *vtable;
struct minimal_symbol *vtable_symbol;
const char *vtable_symbol_name;
const char *class_name;
struct type *run_time_type;
struct type *base_type;
LONGEST offset_to_top;
if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
return NULL;
fill_in_vptr_fieldno (values_type);
if (TYPE_VPTR_FIELDNO (values_type) == -1)
return NULL;
if (using_enc_p)
*using_enc_p = 0;
base_type = TYPE_VPTR_BASETYPE (values_type);
if (base_type == NULL)
return NULL;
base_type = check_typedef (base_type);
if (values_type != base_type)
{
value = value_cast (base_type, value);
if (using_enc_p)
*using_enc_p = 1;
}
vtable_address
= value_as_address (value_field (value, TYPE_VPTR_FIELDNO (values_type)));
vtable = value_at_lazy (vtable_type,
vtable_address - vtable_address_point_offset ());
vtable_symbol
= lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtable)
+ value_offset (vtable)
+ value_embedded_offset (vtable));
if (! vtable_symbol)
return NULL;
vtable_symbol_name = SYMBOL_DEMANGLED_NAME (vtable_symbol);
if (vtable_symbol_name == NULL
|| strncmp (vtable_symbol_name, "vtable for ", 11))
{
warning (_("can't find linker symbol for virtual table for `%s' value"),
TYPE_NAME (values_type));
if (vtable_symbol_name)
warning (_(" found `%s' instead"), vtable_symbol_name);
return NULL;
}
class_name = vtable_symbol_name + 11;
run_time_type = cp_lookup_rtti_type (class_name, NULL);
if (run_time_type == NULL)
return NULL;
offset_to_top
= value_as_long (value_field (vtable, vtable_field_offset_to_top));
if (full_p)
*full_p = (- offset_to_top == value_embedded_offset (value)
&& (TYPE_LENGTH (value_enclosing_type (value))
>= TYPE_LENGTH (run_time_type)));
if (top_p)
*top_p = - offset_to_top;
return run_time_type;
}
static struct value *
gnuv3_virtual_fn_field (struct value **value_p,
struct fn_field *f, int j,
struct type *type, int offset)
{
struct type *vtable_type = gdbarch_data (current_gdbarch,
vtable_type_gdbarch_data);
struct value *value = *value_p;
struct type *values_type = check_typedef (value_type (value));
struct type *vfn_base;
CORE_ADDR vtable_address;
struct value *vtable;
struct value *vfn;
if (TYPE_CODE (values_type) != TYPE_CODE_CLASS)
error (_("Only classes can have virtual functions."));
vfn_base = TYPE_FN_FIELD_FCONTEXT (f, j);
if (! vfn_base)
vfn_base = TYPE_VPTR_BASETYPE (type);
if (vfn_base == NULL)
error ("Could not find the base type of the virtual table for this class.");
if (TYPE_VPTR_FIELDNO (vfn_base) < 0)
fill_in_vptr_fieldno (vfn_base);
if (TYPE_VPTR_FIELDNO (vfn_base) < 0)
error (_("Could not find virtual table pointer for class \"%s\"."),
TYPE_TAG_NAME (vfn_base) ? TYPE_TAG_NAME (vfn_base) : "<unknown>");
if (vfn_base != values_type)
value = value_cast (vfn_base, value);
if (TYPE_VPTR_BASETYPE (vfn_base) != vfn_base)
value = value_cast (TYPE_VPTR_BASETYPE (vfn_base), value);
vtable_address
= value_as_address (value_field (value, TYPE_VPTR_FIELDNO (vfn_base)));
vtable = value_at_lazy (vtable_type,
vtable_address - vtable_address_point_offset ());
vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
value_from_longest (builtin_type_int,
TYPE_FN_FIELD_VOFFSET (f, j)));
vfn = value_cast (lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j)),
vfn);
*value_p = value_addr (value_cast (type, *value_p));
return vfn;
}
static int
gnuv3_baseclass_offset (struct type *type, int index, const bfd_byte *valaddr,
CORE_ADDR address)
{
struct type *vtable_type = gdbarch_data (current_gdbarch,
vtable_type_gdbarch_data);
struct value *vtable;
struct type *vbasetype;
struct value *offset_val, *vbase_array;
CORE_ADDR vtable_address;
long int cur_base_offset, base_offset;
if (!BASETYPE_VIA_VIRTUAL (type, index))
return TYPE_BASECLASS_BITPOS (type, index) / 8;
cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
if (cur_base_offset >= - vtable_address_point_offset ())
error (_("Expected a negative vbase offset (old compiler?)"));
cur_base_offset = cur_base_offset + vtable_address_point_offset ();
if ((- cur_base_offset) % TYPE_LENGTH (builtin_type_void_data_ptr) != 0)
error (_("Misaligned vbase offset."));
cur_base_offset = cur_base_offset
/ ((int) TYPE_LENGTH (builtin_type_void_data_ptr));
vbasetype = TYPE_VPTR_BASETYPE (type);
if (vbasetype == NULL)
return -1;
if (TYPE_VPTR_FIELDNO (vbasetype) < 0)
fill_in_vptr_fieldno (vbasetype);
if (TYPE_VPTR_FIELDNO (vbasetype) >= 0
&& TYPE_FIELD_BITPOS (vbasetype, TYPE_VPTR_FIELDNO (vbasetype)) != 0)
error (_("Illegal vptr offset in class %s"),
TYPE_NAME (vbasetype) ? TYPE_NAME (vbasetype) : "<unknown>");
vtable_address = value_as_address (value_at_lazy (builtin_type_void_data_ptr,
address));
vtable = value_at_lazy (vtable_type,
vtable_address - vtable_address_point_offset ());
offset_val = value_from_longest(builtin_type_int, 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;
}
static void
init_gnuv3_ops (void)
{
vtable_type_gdbarch_data = gdbarch_data_register_post_init (build_gdb_vtable_type);
gnu_v3_abi_ops.shortname = "gnu-v3";
gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
gnu_v3_abi_ops.is_destructor_name =
(enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
gnu_v3_abi_ops.is_constructor_name =
(enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
}
extern initialize_file_ftype _initialize_gnu_v3_abi;
void
_initialize_gnu_v3_abi (void)
{
init_gnuv3_ops ();
register_cp_abi (&gnu_v3_abi_ops);
}