2004-03-17 Andrew Cagney <cagney@redhat.com>
[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 3
362ff856 4 Copyright 2001, 2002, 2003 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"
362ff856 26#include "cp-support.h"
7ed49443 27#include "demangle.h"
3d499020 28#include "gdb_assert.h"
5f8a3188 29#include "gdb_string.h"
7ed49443 30
b27b8843 31static struct cp_abi_ops gnu_v3_abi_ops;
7ed49443
JB
32
33static int
34gnuv3_is_vtable_name (const char *name)
35{
36 return strncmp (name, "_ZTV", 4) == 0;
37}
38
39static int
40gnuv3_is_operator_name (const char *name)
41{
42 return strncmp (name, "operator", 8) == 0;
43}
44
45
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:
49
50 struct gdb_gnu_v3_abi_vtable {
51
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];
57
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;
61
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
65 exactly right. * /
66 void *type_info;
67
68 / * Virtual table pointers in objects point here. * /
69
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]) ();
73
74 };
75
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.
79
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. */
b27b8843 83static struct gdbarch_data *vtable_type_gdbarch_data;
7ed49443
JB
84
85
86/* Human-readable names for the numbers of the fields above. */
87enum {
88 vtable_field_vcall_and_vbase_offsets,
89 vtable_field_offset_to_top,
90 vtable_field_type_info,
91 vtable_field_virtual_functions
92};
93
94
95/* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
96 described above, laid out appropriately for ARCH.
97
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. */
102static void *
103build_gdb_vtable_type (struct gdbarch *arch)
104{
105 struct type *t;
106 struct field *field_list, *field;
107 int offset;
108
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));
113
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,
117 "ptrdiff_t", 0);
118
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. */
123
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];
128 offset = 0;
129
130 /* ptrdiff_t vcall_and_vbase_offsets[0]; */
131 FIELD_NAME (*field) = "vcall_and_vbase_offsets";
132 FIELD_TYPE (*field)
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));
137 field++;
138
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));
144 field++;
145
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));
151 field++;
152
153 /* void (*virtual_functions[0]) (); */
154 FIELD_NAME (*field) = "virtual_functions";
155 FIELD_TYPE (*field)
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));
160 field++;
161
162 /* We assumed in the allocation above that there were four fields. */
3d499020 163 gdb_assert (field == (field_list + 4));
7ed49443
JB
164
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";
169
170 return t;
171}
172
173
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). */
177static int
5ae5f592 178vtable_address_point_offset (void)
7ed49443 179{
451fbdda
AC
180 struct type *vtable_type = gdbarch_data (current_gdbarch,
181 vtable_type_gdbarch_data);
7ed49443
JB
182
183 return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions)
184 / TARGET_CHAR_BIT);
185}
186
187
188static struct type *
189gnuv3_rtti_type (struct value *value,
190 int *full_p, int *top_p, int *using_enc_p)
191{
451fbdda
AC
192 struct type *vtable_type = gdbarch_data (current_gdbarch,
193 vtable_type_gdbarch_data);
7ed49443
JB
194 struct type *value_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;
7ed49443 200 struct type *run_time_type;
21cfb3b6 201 struct type *base_type;
7ed49443
JB
202 LONGEST offset_to_top;
203
204 /* We only have RTTI for class objects. */
205 if (TYPE_CODE (value_type) != TYPE_CODE_CLASS)
206 return NULL;
207
208 /* If we can't find the virtual table pointer for value_type, we
209 can't find the RTTI. */
210 fill_in_vptr_fieldno (value_type);
211 if (TYPE_VPTR_FIELDNO (value_type) == -1)
212 return NULL;
213
21cfb3b6
DJ
214 if (using_enc_p)
215 *using_enc_p = 0;
216
7ed49443 217 /* Fetch VALUE's virtual table pointer, and tweak it to point at
21cfb3b6
DJ
218 an instance of our imaginary gdb_gnu_v3_abi_vtable structure. */
219 base_type = check_typedef (TYPE_VPTR_BASETYPE (value_type));
220 if (value_type != base_type)
221 {
222 value = value_cast (base_type, value);
223 if (using_enc_p)
224 *using_enc_p = 1;
225 }
7ed49443 226 vtable_address
1aa20aa8 227 = value_as_address (value_field (value, TYPE_VPTR_FIELDNO (value_type)));
7ed49443
JB
228 vtable = value_at_lazy (vtable_type,
229 vtable_address - vtable_address_point_offset (),
230 VALUE_BFD_SECTION (value));
231
232 /* Find the linker symbol for this vtable. */
233 vtable_symbol
234 = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtable)
235 + VALUE_OFFSET (vtable)
236 + VALUE_EMBEDDED_OFFSET (vtable));
237 if (! vtable_symbol)
238 return NULL;
239
240 /* The symbol's demangled name should be something like "vtable for
241 CLASS", where CLASS is the name of the run-time type of VALUE.
242 If we didn't like this approach, we could instead look in the
243 type_info object itself to get the class name. But this way
244 should work just as well, and doesn't read target memory. */
245 vtable_symbol_name = SYMBOL_DEMANGLED_NAME (vtable_symbol);
98081e55
PB
246 if (vtable_symbol_name == NULL
247 || strncmp (vtable_symbol_name, "vtable for ", 11))
f773fdbb
JM
248 {
249 warning ("can't find linker symbol for virtual table for `%s' value",
250 TYPE_NAME (value_type));
251 if (vtable_symbol_name)
252 warning (" found `%s' instead", vtable_symbol_name);
253 return NULL;
254 }
7ed49443
JB
255 class_name = vtable_symbol_name + 11;
256
257 /* Try to look up the class name as a type name. */
362ff856
MC
258 /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
259 run_time_type = cp_lookup_rtti_type (class_name, NULL);
260 if (run_time_type == NULL)
261 return NULL;
7ed49443
JB
262
263 /* Get the offset from VALUE to the top of the complete object.
264 NOTE: this is the reverse of the meaning of *TOP_P. */
265 offset_to_top
266 = value_as_long (value_field (vtable, vtable_field_offset_to_top));
267
268 if (full_p)
269 *full_p = (- offset_to_top == VALUE_EMBEDDED_OFFSET (value)
270 && (TYPE_LENGTH (VALUE_ENCLOSING_TYPE (value))
271 >= TYPE_LENGTH (run_time_type)));
272 if (top_p)
273 *top_p = - offset_to_top;
7ed49443
JB
274
275 return run_time_type;
276}
277
278
279static struct value *
280gnuv3_virtual_fn_field (struct value **value_p,
281 struct fn_field *f, int j,
282 struct type *type, int offset)
283{
451fbdda
AC
284 struct type *vtable_type = gdbarch_data (current_gdbarch,
285 vtable_type_gdbarch_data);
7ed49443
JB
286 struct value *value = *value_p;
287 struct type *value_type = check_typedef (VALUE_TYPE (value));
288 struct type *vfn_base;
289 CORE_ADDR vtable_address;
290 struct value *vtable;
291 struct value *vfn;
292
293 /* Some simple sanity checks. */
294 if (TYPE_CODE (value_type) != TYPE_CODE_CLASS)
295 error ("Only classes can have virtual functions.");
296
297 /* Find the base class that defines this virtual function. */
298 vfn_base = TYPE_FN_FIELD_FCONTEXT (f, j);
299 if (! vfn_base)
300 /* In programs compiled with G++ version 1, the debug info doesn't
301 say which base class defined the virtual function. We'll guess
302 it's the same base class that has our vtable; this is wrong for
303 multiple inheritance, but it's better than nothing. */
304 vfn_base = TYPE_VPTR_BASETYPE (type);
305
306 /* This type may have been defined before its virtual function table
307 was. If so, fill in the virtual function table entry for the
308 type now. */
309 if (TYPE_VPTR_FIELDNO (vfn_base) < 0)
310 fill_in_vptr_fieldno (vfn_base);
cef4f5dd
DJ
311 if (TYPE_VPTR_FIELDNO (vfn_base) < 0)
312 error ("Could not find virtual table pointer for class \"%s\".",
313 TYPE_TAG_NAME (vfn_base) ? TYPE_TAG_NAME (vfn_base) : "<unknown>");
7ed49443
JB
314
315 /* Now that we know which base class is defining our virtual
316 function, cast our value to that baseclass. This takes care of
317 any necessary `this' adjustments. */
318 if (vfn_base != value_type)
21cfb3b6 319 value = value_cast (vfn_base, value);
7ed49443
JB
320
321 /* Now value is an object of the appropriate base type. Fetch its
322 virtual table. */
21cfb3b6 323 /* It might be possible to do this cast at the same time as the above.
76b79d6e
DJ
324 Does multiple inheritance affect this?
325 Can this even trigger, or is TYPE_VPTR_BASETYPE idempotent?
326 */
21cfb3b6
DJ
327 if (TYPE_VPTR_BASETYPE (vfn_base) != vfn_base)
328 value = value_cast (TYPE_VPTR_BASETYPE (vfn_base), value);
7ed49443 329 vtable_address
1aa20aa8 330 = value_as_address (value_field (value, TYPE_VPTR_FIELDNO (vfn_base)));
21cfb3b6 331
7ed49443
JB
332 vtable = value_at_lazy (vtable_type,
333 vtable_address - vtable_address_point_offset (),
334 VALUE_BFD_SECTION (value));
335
336 /* Fetch the appropriate function pointer from the vtable. */
337 vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
338 value_from_longest (builtin_type_int,
339 TYPE_FN_FIELD_VOFFSET (f, j)));
340
341 /* Cast the function pointer to the appropriate type. */
342 vfn = value_cast (lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j)),
343 vfn);
344
76b79d6e
DJ
345 /* Is (type)value always numerically the same as (vfn_base)value?
346 If so we can spare this cast and use one of the ones above. */
347 *value_p = value_addr (value_cast (type, *value_p));
348
7ed49443
JB
349 return vfn;
350}
351
1514d34e
DJ
352/* Compute the offset of the baseclass which is
353 the INDEXth baseclass of class TYPE,
354 for value at VALADDR (in host) at ADDRESS (in target).
355 The result is the offset of the baseclass value relative
356 to (the address of)(ARG) + OFFSET.
357
358 -1 is returned on error. */
b9362cc7 359static int
1514d34e
DJ
360gnuv3_baseclass_offset (struct type *type, int index, char *valaddr,
361 CORE_ADDR address)
362{
451fbdda
AC
363 struct type *vtable_type = gdbarch_data (current_gdbarch,
364 vtable_type_gdbarch_data);
79d5b63a
DJ
365 struct value *vtable;
366 struct type *vbasetype;
1514d34e
DJ
367 struct value *offset_val, *vbase_array;
368 CORE_ADDR vtable_address;
369 long int cur_base_offset, base_offset;
1514d34e
DJ
370
371 /* If it isn't a virtual base, this is easy. The offset is in the
372 type definition. */
373 if (!BASETYPE_VIA_VIRTUAL (type, index))
374 return TYPE_BASECLASS_BITPOS (type, index) / 8;
375
376 /* To access a virtual base, we need to use the vbase offset stored in
377 our vtable. Recent GCC versions provide this information. If it isn't
378 available, we could get what we needed from RTTI, or from drawing the
379 complete inheritance graph based on the debug info. Neither is
380 worthwhile. */
381 cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
382 if (cur_base_offset >= - vtable_address_point_offset ())
383 error ("Expected a negative vbase offset (old compiler?)");
384
385 cur_base_offset = cur_base_offset + vtable_address_point_offset ();
386 if ((- cur_base_offset) % TYPE_LENGTH (builtin_type_void_data_ptr) != 0)
387 error ("Misaligned vbase offset.");
388 cur_base_offset = cur_base_offset
389 / ((int) TYPE_LENGTH (builtin_type_void_data_ptr));
390
391 /* We're now looking for the cur_base_offset'th entry (negative index)
79d5b63a
DJ
392 in the vcall_and_vbase_offsets array. We used to cast the object to
393 its TYPE_VPTR_BASETYPE, and reference the vtable as TYPE_VPTR_FIELDNO;
394 however, that cast can not be done without calling baseclass_offset again
395 if the TYPE_VPTR_BASETYPE is a virtual base class, as described in the
396 v3 C++ ABI Section 2.4.I.2.b. Fortunately the ABI guarantees that the
397 vtable pointer will be located at the beginning of the object, so we can
398 bypass the casting. Verify that the TYPE_VPTR_FIELDNO is in fact at the
7ed85d26
DJ
399 start of whichever baseclass it resides in, as a sanity measure - iff
400 we have debugging information for that baseclass. */
79d5b63a
DJ
401
402 vbasetype = TYPE_VPTR_BASETYPE (type);
7ed85d26
DJ
403 if (TYPE_VPTR_FIELDNO (vbasetype) < 0)
404 fill_in_vptr_fieldno (vbasetype);
405
406 if (TYPE_VPTR_FIELDNO (vbasetype) >= 0
407 && TYPE_FIELD_BITPOS (vbasetype, TYPE_VPTR_FIELDNO (vbasetype)) != 0)
79d5b63a
DJ
408 error ("Illegal vptr offset in class %s",
409 TYPE_NAME (vbasetype) ? TYPE_NAME (vbasetype) : "<unknown>");
410
411 vtable_address = value_as_address (value_at_lazy (builtin_type_void_data_ptr,
412 address, NULL));
1514d34e
DJ
413 vtable = value_at_lazy (vtable_type,
414 vtable_address - vtable_address_point_offset (),
415 NULL);
416 offset_val = value_from_longest(builtin_type_int, cur_base_offset);
417 vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
418 base_offset = value_as_long (value_subscript (vbase_array, offset_val));
419 return base_offset;
420}
7ed49443
JB
421
422static void
423init_gnuv3_ops (void)
424{
030f20e1 425 vtable_type_gdbarch_data = gdbarch_data_register_post_init (build_gdb_vtable_type);
7ed49443
JB
426
427 gnu_v3_abi_ops.shortname = "gnu-v3";
428 gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
429 gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
430 gnu_v3_abi_ops.is_destructor_name = is_gnu_v3_mangled_dtor;
431 gnu_v3_abi_ops.is_constructor_name = is_gnu_v3_mangled_ctor;
432 gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
433 gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
434 gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
435 gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
1514d34e 436 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
7ed49443
JB
437}
438
b9362cc7 439extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */
7ed49443
JB
440
441void
442_initialize_gnu_v3_abi (void)
443{
444 init_gnuv3_ops ();
445
fe1f4a5e 446 register_cp_abi (&gnu_v3_abi_ops);
7ed49443 447}
This page took 0.282292 seconds and 4 git commands to generate.