1 /* Abstraction of GNU v3 abi.
2 Contributed by Jim Blandy <jimb@redhat.com>
4 Copyright 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
26 #include "cp-support.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
31 static struct cp_abi_ops gnu_v3_abi_ops
;
34 gnuv3_is_vtable_name (const char *name
)
36 return strncmp (name
, "_ZTV", 4) == 0;
40 gnuv3_is_operator_name (const char *name
)
42 return strncmp (name
, "operator", 8) == 0;
46 /* To help us find the components of a vtable, we build ourselves a
47 GDB type object representing the vtable structure. Following the
48 V3 ABI, it goes something like this:
50 struct gdb_gnu_v3_abi_vtable {
52 / * An array of virtual call and virtual base offsets. The real
53 length of this array depends on the class hierarchy; we use
54 negative subscripts to access the elements. Yucky, but
55 better than the alternatives. * /
56 ptrdiff_t vcall_and_vbase_offsets[0];
58 / * The offset from a virtual pointer referring to this table
59 to the top of the complete object. * /
60 ptrdiff_t offset_to_top;
62 / * The type_info pointer for this class. This is really a
63 std::type_info *, but GDB doesn't really look at the
64 type_info object itself, so we don't bother to get the type
68 / * Virtual table pointers in objects point here. * /
70 / * Virtual function pointers. Like the vcall/vbase array, the
71 real length of this table depends on the class hierarchy. * /
72 void (*virtual_functions[0]) ();
76 The catch, of course, is that the exact layout of this table
77 depends on the ABI --- word size, endianness, alignment, etc. So
78 the GDB type object is actually a per-architecture kind of thing.
80 vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
81 which refers to the struct type * for this structure, laid out
82 appropriately for the architecture. */
83 static struct gdbarch_data
*vtable_type_gdbarch_data
;
86 /* Human-readable names for the numbers of the fields above. */
88 vtable_field_vcall_and_vbase_offsets
,
89 vtable_field_offset_to_top
,
90 vtable_field_type_info
,
91 vtable_field_virtual_functions
95 /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
96 described above, laid out appropriately for ARCH.
98 We use this function as the gdbarch per-architecture data
99 initialization function. We assume that the gdbarch framework
100 calls the per-architecture data initialization functions after it
101 sets current_gdbarch to the new architecture. */
103 build_gdb_vtable_type (struct gdbarch
*arch
)
106 struct field
*field_list
, *field
;
109 struct type
*void_ptr_type
110 = lookup_pointer_type (builtin_type_void
);
111 struct type
*ptr_to_void_fn_type
112 = lookup_pointer_type (lookup_function_type (builtin_type_void
));
114 /* ARCH can't give us the true ptrdiff_t type, so we guess. */
115 struct type
*ptrdiff_type
116 = init_type (TYPE_CODE_INT
, TARGET_PTR_BIT
/ TARGET_CHAR_BIT
, 0,
119 /* We assume no padding is necessary, since GDB doesn't know
120 anything about alignment at the moment. If this assumption bites
121 us, we should add a gdbarch method which, given a type, returns
122 the alignment that type requires, and then use that here. */
124 /* Build the field list. */
125 field_list
= xmalloc (sizeof (struct field
[4]));
126 memset (field_list
, 0, sizeof (struct field
[4]));
127 field
= &field_list
[0];
130 /* ptrdiff_t vcall_and_vbase_offsets[0]; */
131 FIELD_NAME (*field
) = "vcall_and_vbase_offsets";
133 = create_array_type (0, ptrdiff_type
,
134 create_range_type (0, builtin_type_int
, 0, -1));
135 FIELD_BITPOS (*field
) = offset
* TARGET_CHAR_BIT
;
136 offset
+= TYPE_LENGTH (FIELD_TYPE (*field
));
139 /* ptrdiff_t offset_to_top; */
140 FIELD_NAME (*field
) = "offset_to_top";
141 FIELD_TYPE (*field
) = ptrdiff_type
;
142 FIELD_BITPOS (*field
) = offset
* TARGET_CHAR_BIT
;
143 offset
+= TYPE_LENGTH (FIELD_TYPE (*field
));
146 /* void *type_info; */
147 FIELD_NAME (*field
) = "type_info";
148 FIELD_TYPE (*field
) = void_ptr_type
;
149 FIELD_BITPOS (*field
) = offset
* TARGET_CHAR_BIT
;
150 offset
+= TYPE_LENGTH (FIELD_TYPE (*field
));
153 /* void (*virtual_functions[0]) (); */
154 FIELD_NAME (*field
) = "virtual_functions";
156 = create_array_type (0, ptr_to_void_fn_type
,
157 create_range_type (0, builtin_type_int
, 0, -1));
158 FIELD_BITPOS (*field
) = offset
* TARGET_CHAR_BIT
;
159 offset
+= TYPE_LENGTH (FIELD_TYPE (*field
));
162 /* We assumed in the allocation above that there were four fields. */
163 gdb_assert (field
== (field_list
+ 4));
165 t
= init_type (TYPE_CODE_STRUCT
, offset
, 0, 0, 0);
166 TYPE_NFIELDS (t
) = field
- field_list
;
167 TYPE_FIELDS (t
) = field_list
;
168 TYPE_TAG_NAME (t
) = "gdb_gnu_v3_abi_vtable";
174 /* Return the offset from the start of the imaginary `struct
175 gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
176 (i.e., where objects' virtual table pointers point). */
178 vtable_address_point_offset (void)
180 struct type
*vtable_type
= gdbarch_data (current_gdbarch
,
181 vtable_type_gdbarch_data
);
183 return (TYPE_FIELD_BITPOS (vtable_type
, vtable_field_virtual_functions
)
189 gnuv3_rtti_type (struct value
*value
,
190 int *full_p
, int *top_p
, int *using_enc_p
)
192 struct type
*vtable_type
= gdbarch_data (current_gdbarch
,
193 vtable_type_gdbarch_data
);
194 struct type
*values_type
= check_typedef (value_type (value
));
195 CORE_ADDR vtable_address
;
196 struct value
*vtable
;
197 struct minimal_symbol
*vtable_symbol
;
198 const char *vtable_symbol_name
;
199 const char *class_name
;
200 struct type
*run_time_type
;
201 struct type
*base_type
;
202 LONGEST offset_to_top
;
204 /* We only have RTTI for class objects. */
205 if (TYPE_CODE (values_type
) != TYPE_CODE_CLASS
)
208 /* If we can't find the virtual table pointer for values_type, we
209 can't find the RTTI. */
210 fill_in_vptr_fieldno (values_type
);
211 if (TYPE_VPTR_FIELDNO (values_type
) == -1)
217 /* Fetch VALUE's virtual table pointer, and tweak it to point at
218 an instance of our imaginary gdb_gnu_v3_abi_vtable structure. */
219 base_type
= check_typedef (TYPE_VPTR_BASETYPE (values_type
));
220 if (values_type
!= base_type
)
222 value
= value_cast (base_type
, value
);
227 = value_as_address (value_field (value
, TYPE_VPTR_FIELDNO (values_type
)));
228 vtable
= value_at_lazy (vtable_type
,
229 vtable_address
- vtable_address_point_offset ());
231 /* Find the linker symbol for this vtable. */
233 = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtable
)
234 + value_offset (vtable
)
235 + value_embedded_offset (vtable
));
239 /* The symbol's demangled name should be something like "vtable for
240 CLASS", where CLASS is the name of the run-time type of VALUE.
241 If we didn't like this approach, we could instead look in the
242 type_info object itself to get the class name. But this way
243 should work just as well, and doesn't read target memory. */
244 vtable_symbol_name
= SYMBOL_DEMANGLED_NAME (vtable_symbol
);
245 if (vtable_symbol_name
== NULL
246 || strncmp (vtable_symbol_name
, "vtable for ", 11))
248 warning (_("can't find linker symbol for virtual table for `%s' value"),
249 TYPE_NAME (values_type
));
250 if (vtable_symbol_name
)
251 warning (_(" found `%s' instead"), vtable_symbol_name
);
254 class_name
= vtable_symbol_name
+ 11;
256 /* Try to look up the class name as a type name. */
257 /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
258 run_time_type
= cp_lookup_rtti_type (class_name
, NULL
);
259 if (run_time_type
== NULL
)
262 /* Get the offset from VALUE to the top of the complete object.
263 NOTE: this is the reverse of the meaning of *TOP_P. */
265 = value_as_long (value_field (vtable
, vtable_field_offset_to_top
));
268 *full_p
= (- offset_to_top
== value_embedded_offset (value
)
269 && (TYPE_LENGTH (value_enclosing_type (value
))
270 >= TYPE_LENGTH (run_time_type
)));
272 *top_p
= - offset_to_top
;
274 return run_time_type
;
278 static struct value
*
279 gnuv3_virtual_fn_field (struct value
**value_p
,
280 struct fn_field
*f
, int j
,
281 struct type
*type
, int offset
)
283 struct type
*vtable_type
= gdbarch_data (current_gdbarch
,
284 vtable_type_gdbarch_data
);
285 struct value
*value
= *value_p
;
286 struct type
*values_type
= check_typedef (value_type (value
));
287 struct type
*vfn_base
;
288 CORE_ADDR vtable_address
;
289 struct value
*vtable
;
292 /* Some simple sanity checks. */
293 if (TYPE_CODE (values_type
) != TYPE_CODE_CLASS
)
294 error (_("Only classes can have virtual functions."));
296 /* Find the base class that defines this virtual function. */
297 vfn_base
= TYPE_FN_FIELD_FCONTEXT (f
, j
);
299 /* In programs compiled with G++ version 1, the debug info doesn't
300 say which base class defined the virtual function. We'll guess
301 it's the same base class that has our vtable; this is wrong for
302 multiple inheritance, but it's better than nothing. */
303 vfn_base
= TYPE_VPTR_BASETYPE (type
);
305 /* This type may have been defined before its virtual function table
306 was. If so, fill in the virtual function table entry for the
308 if (TYPE_VPTR_FIELDNO (vfn_base
) < 0)
309 fill_in_vptr_fieldno (vfn_base
);
310 if (TYPE_VPTR_FIELDNO (vfn_base
) < 0)
311 error (_("Could not find virtual table pointer for class \"%s\"."),
312 TYPE_TAG_NAME (vfn_base
) ? TYPE_TAG_NAME (vfn_base
) : "<unknown>");
314 /* Now that we know which base class is defining our virtual
315 function, cast our value to that baseclass. This takes care of
316 any necessary `this' adjustments. */
317 if (vfn_base
!= values_type
)
318 value
= value_cast (vfn_base
, value
);
320 /* Now value is an object of the appropriate base type. Fetch its
322 /* It might be possible to do this cast at the same time as the above.
323 Does multiple inheritance affect this?
324 Can this even trigger, or is TYPE_VPTR_BASETYPE idempotent?
326 if (TYPE_VPTR_BASETYPE (vfn_base
) != vfn_base
)
327 value
= value_cast (TYPE_VPTR_BASETYPE (vfn_base
), value
);
329 = value_as_address (value_field (value
, TYPE_VPTR_FIELDNO (vfn_base
)));
331 vtable
= value_at_lazy (vtable_type
,
332 vtable_address
- vtable_address_point_offset ());
334 /* Fetch the appropriate function pointer from the vtable. */
335 vfn
= value_subscript (value_field (vtable
, vtable_field_virtual_functions
),
336 value_from_longest (builtin_type_int
,
337 TYPE_FN_FIELD_VOFFSET (f
, j
)));
339 /* Cast the function pointer to the appropriate type. */
340 vfn
= value_cast (lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
)),
343 /* Is (type)value always numerically the same as (vfn_base)value?
344 If so we can spare this cast and use one of the ones above. */
345 *value_p
= value_addr (value_cast (type
, *value_p
));
350 /* Compute the offset of the baseclass which is
351 the INDEXth baseclass of class TYPE,
352 for value at VALADDR (in host) at ADDRESS (in target).
353 The result is the offset of the baseclass value relative
354 to (the address of)(ARG) + OFFSET.
356 -1 is returned on error. */
358 gnuv3_baseclass_offset (struct type
*type
, int index
, const bfd_byte
*valaddr
,
361 struct type
*vtable_type
= gdbarch_data (current_gdbarch
,
362 vtable_type_gdbarch_data
);
363 struct value
*vtable
;
364 struct type
*vbasetype
;
365 struct value
*offset_val
, *vbase_array
;
366 CORE_ADDR vtable_address
;
367 long int cur_base_offset
, base_offset
;
369 /* If it isn't a virtual base, this is easy. The offset is in the
371 if (!BASETYPE_VIA_VIRTUAL (type
, index
))
372 return TYPE_BASECLASS_BITPOS (type
, index
) / 8;
374 /* To access a virtual base, we need to use the vbase offset stored in
375 our vtable. Recent GCC versions provide this information. If it isn't
376 available, we could get what we needed from RTTI, or from drawing the
377 complete inheritance graph based on the debug info. Neither is
379 cur_base_offset
= TYPE_BASECLASS_BITPOS (type
, index
) / 8;
380 if (cur_base_offset
>= - vtable_address_point_offset ())
381 error (_("Expected a negative vbase offset (old compiler?)"));
383 cur_base_offset
= cur_base_offset
+ vtable_address_point_offset ();
384 if ((- cur_base_offset
) % TYPE_LENGTH (builtin_type_void_data_ptr
) != 0)
385 error (_("Misaligned vbase offset."));
386 cur_base_offset
= cur_base_offset
387 / ((int) TYPE_LENGTH (builtin_type_void_data_ptr
));
389 /* We're now looking for the cur_base_offset'th entry (negative index)
390 in the vcall_and_vbase_offsets array. We used to cast the object to
391 its TYPE_VPTR_BASETYPE, and reference the vtable as TYPE_VPTR_FIELDNO;
392 however, that cast can not be done without calling baseclass_offset again
393 if the TYPE_VPTR_BASETYPE is a virtual base class, as described in the
394 v3 C++ ABI Section 2.4.I.2.b. Fortunately the ABI guarantees that the
395 vtable pointer will be located at the beginning of the object, so we can
396 bypass the casting. Verify that the TYPE_VPTR_FIELDNO is in fact at the
397 start of whichever baseclass it resides in, as a sanity measure - iff
398 we have debugging information for that baseclass. */
400 vbasetype
= TYPE_VPTR_BASETYPE (type
);
401 if (TYPE_VPTR_FIELDNO (vbasetype
) < 0)
402 fill_in_vptr_fieldno (vbasetype
);
404 if (TYPE_VPTR_FIELDNO (vbasetype
) >= 0
405 && TYPE_FIELD_BITPOS (vbasetype
, TYPE_VPTR_FIELDNO (vbasetype
)) != 0)
406 error (_("Illegal vptr offset in class %s"),
407 TYPE_NAME (vbasetype
) ? TYPE_NAME (vbasetype
) : "<unknown>");
409 vtable_address
= value_as_address (value_at_lazy (builtin_type_void_data_ptr
,
411 vtable
= value_at_lazy (vtable_type
,
412 vtable_address
- vtable_address_point_offset ());
413 offset_val
= value_from_longest(builtin_type_int
, cur_base_offset
);
414 vbase_array
= value_field (vtable
, vtable_field_vcall_and_vbase_offsets
);
415 base_offset
= value_as_long (value_subscript (vbase_array
, offset_val
));
420 init_gnuv3_ops (void)
422 vtable_type_gdbarch_data
= gdbarch_data_register_post_init (build_gdb_vtable_type
);
424 gnu_v3_abi_ops
.shortname
= "gnu-v3";
425 gnu_v3_abi_ops
.longname
= "GNU G++ Version 3 ABI";
426 gnu_v3_abi_ops
.doc
= "G++ Version 3 ABI";
427 gnu_v3_abi_ops
.is_destructor_name
= is_gnu_v3_mangled_dtor
;
428 gnu_v3_abi_ops
.is_constructor_name
= is_gnu_v3_mangled_ctor
;
429 gnu_v3_abi_ops
.is_vtable_name
= gnuv3_is_vtable_name
;
430 gnu_v3_abi_ops
.is_operator_name
= gnuv3_is_operator_name
;
431 gnu_v3_abi_ops
.rtti_type
= gnuv3_rtti_type
;
432 gnu_v3_abi_ops
.virtual_fn_field
= gnuv3_virtual_fn_field
;
433 gnu_v3_abi_ops
.baseclass_offset
= gnuv3_baseclass_offset
;
436 extern initialize_file_ftype _initialize_gnu_v3_abi
; /* -Wmissing-prototypes */
439 _initialize_gnu_v3_abi (void)
443 register_cp_abi (&gnu_v3_abi_ops
);