* config/mcore/tm-mcore.h: Remove file.
[deliverable/binutils-gdb.git] / gdb / gnu-v3-abi.c
CommitLineData
7ed49443
JB
1/* Abstraction of GNU v3 abi.
2 Contributed by Jim Blandy <jimb@redhat.com>
451fbdda
AC
3
4 Copyright 2001, 2002 Free Software Foundation, Inc.
7ed49443
JB
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23#include "defs.h"
24#include "value.h"
25#include "cp-abi.h"
26#include "demangle.h"
3d499020 27#include "gdb_assert.h"
7ed49443 28
b27b8843 29static struct cp_abi_ops gnu_v3_abi_ops;
7ed49443
JB
30
31static int
32gnuv3_is_vtable_name (const char *name)
33{
34 return strncmp (name, "_ZTV", 4) == 0;
35}
36
37static int
38gnuv3_is_operator_name (const char *name)
39{
40 return strncmp (name, "operator", 8) == 0;
41}
42
43
44/* To help us find the components of a vtable, we build ourselves a
45 GDB type object representing the vtable structure. Following the
46 V3 ABI, it goes something like this:
47
48 struct gdb_gnu_v3_abi_vtable {
49
50 / * An array of virtual call and virtual base offsets. The real
51 length of this array depends on the class hierarchy; we use
52 negative subscripts to access the elements. Yucky, but
53 better than the alternatives. * /
54 ptrdiff_t vcall_and_vbase_offsets[0];
55
56 / * The offset from a virtual pointer referring to this table
57 to the top of the complete object. * /
58 ptrdiff_t offset_to_top;
59
60 / * The type_info pointer for this class. This is really a
61 std::type_info *, but GDB doesn't really look at the
62 type_info object itself, so we don't bother to get the type
63 exactly right. * /
64 void *type_info;
65
66 / * Virtual table pointers in objects point here. * /
67
68 / * Virtual function pointers. Like the vcall/vbase array, the
69 real length of this table depends on the class hierarchy. * /
70 void (*virtual_functions[0]) ();
71
72 };
73
74 The catch, of course, is that the exact layout of this table
75 depends on the ABI --- word size, endianness, alignment, etc. So
76 the GDB type object is actually a per-architecture kind of thing.
77
78 vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
79 which refers to the struct type * for this structure, laid out
80 appropriately for the architecture. */
b27b8843 81static struct gdbarch_data *vtable_type_gdbarch_data;
7ed49443
JB
82
83
84/* Human-readable names for the numbers of the fields above. */
85enum {
86 vtable_field_vcall_and_vbase_offsets,
87 vtable_field_offset_to_top,
88 vtable_field_type_info,
89 vtable_field_virtual_functions
90};
91
92
93/* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
94 described above, laid out appropriately for ARCH.
95
96 We use this function as the gdbarch per-architecture data
97 initialization function. We assume that the gdbarch framework
98 calls the per-architecture data initialization functions after it
99 sets current_gdbarch to the new architecture. */
100static void *
101build_gdb_vtable_type (struct gdbarch *arch)
102{
103 struct type *t;
104 struct field *field_list, *field;
105 int offset;
106
107 struct type *void_ptr_type
108 = lookup_pointer_type (builtin_type_void);
109 struct type *ptr_to_void_fn_type
110 = lookup_pointer_type (lookup_function_type (builtin_type_void));
111
112 /* ARCH can't give us the true ptrdiff_t type, so we guess. */
113 struct type *ptrdiff_type
114 = init_type (TYPE_CODE_INT, TARGET_PTR_BIT / TARGET_CHAR_BIT, 0,
115 "ptrdiff_t", 0);
116
117 /* We assume no padding is necessary, since GDB doesn't know
118 anything about alignment at the moment. If this assumption bites
119 us, we should add a gdbarch method which, given a type, returns
120 the alignment that type requires, and then use that here. */
121
122 /* Build the field list. */
123 field_list = xmalloc (sizeof (struct field [4]));
124 memset (field_list, 0, sizeof (struct field [4]));
125 field = &field_list[0];
126 offset = 0;
127
128 /* ptrdiff_t vcall_and_vbase_offsets[0]; */
129 FIELD_NAME (*field) = "vcall_and_vbase_offsets";
130 FIELD_TYPE (*field)
131 = create_array_type (0, ptrdiff_type,
132 create_range_type (0, builtin_type_int, 0, -1));
133 FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
134 offset += TYPE_LENGTH (FIELD_TYPE (*field));
135 field++;
136
137 /* ptrdiff_t offset_to_top; */
138 FIELD_NAME (*field) = "offset_to_top";
139 FIELD_TYPE (*field) = ptrdiff_type;
140 FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
141 offset += TYPE_LENGTH (FIELD_TYPE (*field));
142 field++;
143
144 /* void *type_info; */
145 FIELD_NAME (*field) = "type_info";
146 FIELD_TYPE (*field) = void_ptr_type;
147 FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
148 offset += TYPE_LENGTH (FIELD_TYPE (*field));
149 field++;
150
151 /* void (*virtual_functions[0]) (); */
152 FIELD_NAME (*field) = "virtual_functions";
153 FIELD_TYPE (*field)
154 = create_array_type (0, ptr_to_void_fn_type,
155 create_range_type (0, builtin_type_int, 0, -1));
156 FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
157 offset += TYPE_LENGTH (FIELD_TYPE (*field));
158 field++;
159
160 /* We assumed in the allocation above that there were four fields. */
3d499020 161 gdb_assert (field == (field_list + 4));
7ed49443
JB
162
163 t = init_type (TYPE_CODE_STRUCT, offset, 0, 0, 0);
164 TYPE_NFIELDS (t) = field - field_list;
165 TYPE_FIELDS (t) = field_list;
166 TYPE_TAG_NAME (t) = "gdb_gnu_v3_abi_vtable";
167
168 return t;
169}
170
171
172/* Return the offset from the start of the imaginary `struct
173 gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
174 (i.e., where objects' virtual table pointers point). */
175static int
176vtable_address_point_offset ()
177{
451fbdda
AC
178 struct type *vtable_type = gdbarch_data (current_gdbarch,
179 vtable_type_gdbarch_data);
7ed49443
JB
180
181 return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions)
182 / TARGET_CHAR_BIT);
183}
184
185
186static struct type *
187gnuv3_rtti_type (struct value *value,
188 int *full_p, int *top_p, int *using_enc_p)
189{
451fbdda
AC
190 struct type *vtable_type = gdbarch_data (current_gdbarch,
191 vtable_type_gdbarch_data);
7ed49443
JB
192 struct type *value_type = check_typedef (VALUE_TYPE (value));
193 CORE_ADDR vtable_address;
194 struct value *vtable;
195 struct minimal_symbol *vtable_symbol;
196 const char *vtable_symbol_name;
197 const char *class_name;
198 struct symbol *class_symbol;
199 struct type *run_time_type;
21cfb3b6 200 struct type *base_type;
7ed49443
JB
201 LONGEST offset_to_top;
202
203 /* We only have RTTI for class objects. */
204 if (TYPE_CODE (value_type) != TYPE_CODE_CLASS)
205 return NULL;
206
207 /* If we can't find the virtual table pointer for value_type, we
208 can't find the RTTI. */
209 fill_in_vptr_fieldno (value_type);
210 if (TYPE_VPTR_FIELDNO (value_type) == -1)
211 return NULL;
212
21cfb3b6
DJ
213 if (using_enc_p)
214 *using_enc_p = 0;
215
7ed49443 216 /* Fetch VALUE's virtual table pointer, and tweak it to point at
21cfb3b6
DJ
217 an instance of our imaginary gdb_gnu_v3_abi_vtable structure. */
218 base_type = check_typedef (TYPE_VPTR_BASETYPE (value_type));
219 if (value_type != base_type)
220 {
221 value = value_cast (base_type, value);
222 if (using_enc_p)
223 *using_enc_p = 1;
224 }
7ed49443 225 vtable_address
1aa20aa8 226 = value_as_address (value_field (value, TYPE_VPTR_FIELDNO (value_type)));
7ed49443
JB
227 vtable = value_at_lazy (vtable_type,
228 vtable_address - vtable_address_point_offset (),
229 VALUE_BFD_SECTION (value));
230
231 /* Find the linker symbol for this vtable. */
232 vtable_symbol
233 = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtable)
234 + VALUE_OFFSET (vtable)
235 + VALUE_EMBEDDED_OFFSET (vtable));
236 if (! vtable_symbol)
237 return NULL;
238
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);
98081e55
PB
245 if (vtable_symbol_name == NULL
246 || strncmp (vtable_symbol_name, "vtable for ", 11))
f773fdbb
JM
247 {
248 warning ("can't find linker symbol for virtual table for `%s' value",
249 TYPE_NAME (value_type));
250 if (vtable_symbol_name)
251 warning (" found `%s' instead", vtable_symbol_name);
252 return NULL;
253 }
7ed49443
JB
254 class_name = vtable_symbol_name + 11;
255
256 /* Try to look up the class name as a type name. */
257 class_symbol = lookup_symbol (class_name, 0, STRUCT_NAMESPACE, 0, 0);
258 if (! class_symbol)
f773fdbb
JM
259 {
260 warning ("can't find class named `%s', as given by C++ RTTI", class_name);
261 return NULL;
262 }
7ed49443
JB
263
264 /* Make sure the type symbol is sane. (An earlier version of this
265 code would find constructor functions, who have the same name as
266 the class.) */
267 if (SYMBOL_CLASS (class_symbol) != LOC_TYPEDEF
268 || TYPE_CODE (SYMBOL_TYPE (class_symbol)) != TYPE_CODE_CLASS)
f773fdbb
JM
269 {
270 warning ("C++ RTTI gives a class name of `%s', but that isn't a type name",
271 class_name);
272 return NULL;
273 }
7ed49443
JB
274
275 /* This is the object's run-time type! */
276 run_time_type = SYMBOL_TYPE (class_symbol);
277
278 /* Get the offset from VALUE to the top of the complete object.
279 NOTE: this is the reverse of the meaning of *TOP_P. */
280 offset_to_top
281 = value_as_long (value_field (vtable, vtable_field_offset_to_top));
282
283 if (full_p)
284 *full_p = (- offset_to_top == VALUE_EMBEDDED_OFFSET (value)
285 && (TYPE_LENGTH (VALUE_ENCLOSING_TYPE (value))
286 >= TYPE_LENGTH (run_time_type)));
287 if (top_p)
288 *top_p = - offset_to_top;
7ed49443
JB
289
290 return run_time_type;
291}
292
293
294static struct value *
295gnuv3_virtual_fn_field (struct value **value_p,
296 struct fn_field *f, int j,
297 struct type *type, int offset)
298{
451fbdda
AC
299 struct type *vtable_type = gdbarch_data (current_gdbarch,
300 vtable_type_gdbarch_data);
7ed49443
JB
301 struct value *value = *value_p;
302 struct type *value_type = check_typedef (VALUE_TYPE (value));
303 struct type *vfn_base;
304 CORE_ADDR vtable_address;
305 struct value *vtable;
306 struct value *vfn;
307
308 /* Some simple sanity checks. */
309 if (TYPE_CODE (value_type) != TYPE_CODE_CLASS)
310 error ("Only classes can have virtual functions.");
311
312 /* Find the base class that defines this virtual function. */
313 vfn_base = TYPE_FN_FIELD_FCONTEXT (f, j);
314 if (! vfn_base)
315 /* In programs compiled with G++ version 1, the debug info doesn't
316 say which base class defined the virtual function. We'll guess
317 it's the same base class that has our vtable; this is wrong for
318 multiple inheritance, but it's better than nothing. */
319 vfn_base = TYPE_VPTR_BASETYPE (type);
320
321 /* This type may have been defined before its virtual function table
322 was. If so, fill in the virtual function table entry for the
323 type now. */
324 if (TYPE_VPTR_FIELDNO (vfn_base) < 0)
325 fill_in_vptr_fieldno (vfn_base);
326
327 /* Now that we know which base class is defining our virtual
328 function, cast our value to that baseclass. This takes care of
329 any necessary `this' adjustments. */
330 if (vfn_base != value_type)
21cfb3b6 331 value = value_cast (vfn_base, value);
7ed49443
JB
332
333 /* Now value is an object of the appropriate base type. Fetch its
334 virtual table. */
21cfb3b6 335 /* It might be possible to do this cast at the same time as the above.
76b79d6e
DJ
336 Does multiple inheritance affect this?
337 Can this even trigger, or is TYPE_VPTR_BASETYPE idempotent?
338 */
21cfb3b6
DJ
339 if (TYPE_VPTR_BASETYPE (vfn_base) != vfn_base)
340 value = value_cast (TYPE_VPTR_BASETYPE (vfn_base), value);
7ed49443 341 vtable_address
1aa20aa8 342 = value_as_address (value_field (value, TYPE_VPTR_FIELDNO (vfn_base)));
21cfb3b6 343
7ed49443
JB
344 vtable = value_at_lazy (vtable_type,
345 vtable_address - vtable_address_point_offset (),
346 VALUE_BFD_SECTION (value));
347
348 /* Fetch the appropriate function pointer from the vtable. */
349 vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
350 value_from_longest (builtin_type_int,
351 TYPE_FN_FIELD_VOFFSET (f, j)));
352
353 /* Cast the function pointer to the appropriate type. */
354 vfn = value_cast (lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j)),
355 vfn);
356
76b79d6e
DJ
357 /* Is (type)value always numerically the same as (vfn_base)value?
358 If so we can spare this cast and use one of the ones above. */
359 *value_p = value_addr (value_cast (type, *value_p));
360
7ed49443
JB
361 return vfn;
362}
363
1514d34e
DJ
364/* Compute the offset of the baseclass which is
365 the INDEXth baseclass of class TYPE,
366 for value at VALADDR (in host) at ADDRESS (in target).
367 The result is the offset of the baseclass value relative
368 to (the address of)(ARG) + OFFSET.
369
370 -1 is returned on error. */
371int
372gnuv3_baseclass_offset (struct type *type, int index, char *valaddr,
373 CORE_ADDR address)
374{
451fbdda
AC
375 struct type *vtable_type = gdbarch_data (current_gdbarch,
376 vtable_type_gdbarch_data);
1514d34e
DJ
377 struct type *basetype = TYPE_BASECLASS (type, index);
378 struct value *full_object, *vbase_object, *orig_object;
379 struct value *vtable, *orig_typeinfo, *orig_base_info;
380 struct type *orig_type, *vbasetype;
381 struct value *offset_val, *vbase_array;
382 CORE_ADDR vtable_address;
383 long int cur_base_offset, base_offset;
384 int to_top;
385 int baseclasses, i;
386
387 /* If it isn't a virtual base, this is easy. The offset is in the
388 type definition. */
389 if (!BASETYPE_VIA_VIRTUAL (type, index))
390 return TYPE_BASECLASS_BITPOS (type, index) / 8;
391
392 /* To access a virtual base, we need to use the vbase offset stored in
393 our vtable. Recent GCC versions provide this information. If it isn't
394 available, we could get what we needed from RTTI, or from drawing the
395 complete inheritance graph based on the debug info. Neither is
396 worthwhile. */
397 cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
398 if (cur_base_offset >= - vtable_address_point_offset ())
399 error ("Expected a negative vbase offset (old compiler?)");
400
401 cur_base_offset = cur_base_offset + vtable_address_point_offset ();
402 if ((- cur_base_offset) % TYPE_LENGTH (builtin_type_void_data_ptr) != 0)
403 error ("Misaligned vbase offset.");
404 cur_base_offset = cur_base_offset
405 / ((int) TYPE_LENGTH (builtin_type_void_data_ptr));
406
407 /* We're now looking for the cur_base_offset'th entry (negative index)
408 in the vcall_and_vbase_offsets array. */
409
410 orig_object = value_at_lazy (type, address, NULL);
411 vbasetype = TYPE_VPTR_BASETYPE (VALUE_TYPE (orig_object));
412 vbase_object = value_cast (vbasetype, orig_object);
413
414 vtable_address
415 = value_as_address (value_field (vbase_object,
416 TYPE_VPTR_FIELDNO (vbasetype)));
417 vtable = value_at_lazy (vtable_type,
418 vtable_address - vtable_address_point_offset (),
419 NULL);
420 offset_val = value_from_longest(builtin_type_int, cur_base_offset);
421 vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
422 base_offset = value_as_long (value_subscript (vbase_array, offset_val));
423 return base_offset;
424}
7ed49443
JB
425
426static void
427init_gnuv3_ops (void)
428{
429 vtable_type_gdbarch_data = register_gdbarch_data (build_gdb_vtable_type, 0);
430
431 gnu_v3_abi_ops.shortname = "gnu-v3";
432 gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
433 gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
434 gnu_v3_abi_ops.is_destructor_name = is_gnu_v3_mangled_dtor;
435 gnu_v3_abi_ops.is_constructor_name = is_gnu_v3_mangled_ctor;
436 gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
437 gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
438 gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
439 gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
1514d34e 440 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
7ed49443
JB
441}
442
443
444void
445_initialize_gnu_v3_abi (void)
446{
447 init_gnuv3_ops ();
448
449 register_cp_abi (gnu_v3_abi_ops);
450}
This page took 0.135344 seconds and 4 git commands to generate.