AMD64 - Fix format conversions and other warnings.
[dragonfly.git] / contrib / gdb-6.2.1 / gdb / gnu-v3-abi.c
blob195af26400bde9e4ed53eec4d892312a7f0922ac
1 /* Abstraction of GNU v3 abi.
2 Contributed by Jim Blandy <jimb@redhat.com>
4 Copyright 2001, 2002, 2003 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. */
23 #include "defs.h"
24 #include "value.h"
25 #include "cp-abi.h"
26 #include "cp-support.h"
27 #include "demangle.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
31 static struct cp_abi_ops gnu_v3_abi_ops;
33 static int
34 gnuv3_is_vtable_name (const char *name)
36 return strncmp (name, "_ZTV", 4) == 0;
39 static int
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
65 exactly right. * /
66 void *type_info;
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. */
87 enum {
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. */
102 static void *
103 build_gdb_vtable_type (struct gdbarch *arch)
105 struct type *t;
106 struct field *field_list, *field;
107 int offset;
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,
117 "ptrdiff_t", 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];
128 offset = 0;
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++;
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++;
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++;
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++;
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";
170 return t;
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). */
177 static int
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)
184 / TARGET_CHAR_BIT);
188 static struct type *
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 *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;
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 (value_type) != TYPE_CODE_CLASS)
206 return NULL;
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;
214 if (using_enc_p)
215 *using_enc_p = 0;
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 (value_type));
220 if (value_type != base_type)
222 value = value_cast (base_type, value);
223 if (using_enc_p)
224 *using_enc_p = 1;
226 vtable_address
227 = value_as_address (value_field (value, TYPE_VPTR_FIELDNO (value_type)));
228 vtable = value_at_lazy (vtable_type,
229 vtable_address - vtable_address_point_offset (),
230 VALUE_BFD_SECTION (value));
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;
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);
246 if (vtable_symbol_name == NULL
247 || strncmp (vtable_symbol_name, "vtable for ", 11))
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;
255 class_name = vtable_symbol_name + 11;
257 /* Try to look up the class name as a type name. */
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;
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));
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;
275 return run_time_type;
279 static struct value *
280 gnuv3_virtual_fn_field (struct value **value_p,
281 struct fn_field *f, int j,
282 struct type *type, int offset)
284 struct type *vtable_type = gdbarch_data (current_gdbarch,
285 vtable_type_gdbarch_data);
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;
293 /* Some simple sanity checks. */
294 if (TYPE_CODE (value_type) != TYPE_CODE_CLASS)
295 error ("Only classes can have virtual functions.");
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);
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);
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>");
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)
319 value = value_cast (vfn_base, value);
321 /* Now value is an object of the appropriate base type. Fetch its
322 virtual table. */
323 /* It might be possible to do this cast at the same time as the above.
324 Does multiple inheritance affect this?
325 Can this even trigger, or is TYPE_VPTR_BASETYPE idempotent?
327 if (TYPE_VPTR_BASETYPE (vfn_base) != vfn_base)
328 value = value_cast (TYPE_VPTR_BASETYPE (vfn_base), value);
329 vtable_address
330 = value_as_address (value_field (value, TYPE_VPTR_FIELDNO (vfn_base)));
332 vtable = value_at_lazy (vtable_type,
333 vtable_address - vtable_address_point_offset (),
334 VALUE_BFD_SECTION (value));
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)));
341 /* Cast the function pointer to the appropriate type. */
342 vfn = value_cast (lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j)),
343 vfn);
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));
349 return vfn;
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.
358 -1 is returned on error. */
359 static int
360 gnuv3_baseclass_offset (struct type *type, int index, char *valaddr,
361 CORE_ADDR address)
363 struct type *vtable_type = gdbarch_data (current_gdbarch,
364 vtable_type_gdbarch_data);
365 struct value *vtable;
366 struct type *vbasetype;
367 struct value *offset_val, *vbase_array;
368 CORE_ADDR vtable_address;
369 long int cur_base_offset, base_offset;
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;
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?)");
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));
391 /* We're now looking for the cur_base_offset'th entry (negative index)
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
399 start of whichever baseclass it resides in, as a sanity measure - iff
400 we have debugging information for that baseclass. */
402 vbasetype = TYPE_VPTR_BASETYPE (type);
403 if (TYPE_VPTR_FIELDNO (vbasetype) < 0)
404 fill_in_vptr_fieldno (vbasetype);
406 if (TYPE_VPTR_FIELDNO (vbasetype) >= 0
407 && TYPE_FIELD_BITPOS (vbasetype, TYPE_VPTR_FIELDNO (vbasetype)) != 0)
408 error ("Illegal vptr offset in class %s",
409 TYPE_NAME (vbasetype) ? TYPE_NAME (vbasetype) : "<unknown>");
411 vtable_address = value_as_address (value_at_lazy (builtin_type_void_data_ptr,
412 address, NULL));
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;
422 static void
423 init_gnuv3_ops (void)
425 vtable_type_gdbarch_data = gdbarch_data_register_post_init (build_gdb_vtable_type);
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;
436 gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
439 extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */
441 void
442 _initialize_gnu_v3_abi (void)
444 init_gnuv3_ops ();
446 register_cp_abi (&gnu_v3_abi_ops);