gnu-v3-abi.c revision 1.7 1 1.1 christos /* Abstraction of GNU v3 abi.
2 1.1 christos Contributed by Jim Blandy <jimb (at) redhat.com>
3 1.1 christos
4 1.7 christos Copyright (C) 2001-2017 Free Software Foundation, Inc.
5 1.1 christos
6 1.1 christos This file is part of GDB.
7 1.1 christos
8 1.1 christos This program is free software; you can redistribute it and/or modify
9 1.1 christos it under the terms of the GNU General Public License as published by
10 1.1 christos the Free Software Foundation; either version 3 of the License, or
11 1.1 christos (at your option) any later version.
12 1.1 christos
13 1.1 christos This program is distributed in the hope that it will be useful,
14 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
15 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 1.1 christos GNU General Public License for more details.
17 1.1 christos
18 1.1 christos You should have received a copy of the GNU General Public License
19 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 1.1 christos
21 1.1 christos #include "defs.h"
22 1.1 christos #include "value.h"
23 1.1 christos #include "cp-abi.h"
24 1.1 christos #include "cp-support.h"
25 1.1 christos #include "demangle.h"
26 1.1 christos #include "objfiles.h"
27 1.1 christos #include "valprint.h"
28 1.1 christos #include "c-lang.h"
29 1.1 christos #include "typeprint.h"
30 1.7 christos #include <algorithm>
31 1.1 christos
32 1.1 christos static struct cp_abi_ops gnu_v3_abi_ops;
33 1.1 christos
34 1.1 christos /* A gdbarch key for std::type_info, in the event that it can't be
35 1.1 christos found in the debug info. */
36 1.1 christos
37 1.1 christos static struct gdbarch_data *std_type_info_gdbarch_data;
38 1.1 christos
39 1.1 christos
40 1.1 christos static int
41 1.1 christos gnuv3_is_vtable_name (const char *name)
42 1.1 christos {
43 1.5 christos return startswith (name, "_ZTV");
44 1.1 christos }
45 1.1 christos
46 1.1 christos static int
47 1.1 christos gnuv3_is_operator_name (const char *name)
48 1.1 christos {
49 1.5 christos return startswith (name, "operator");
50 1.1 christos }
51 1.1 christos
52 1.1 christos
53 1.1 christos /* To help us find the components of a vtable, we build ourselves a
54 1.1 christos GDB type object representing the vtable structure. Following the
55 1.1 christos V3 ABI, it goes something like this:
56 1.1 christos
57 1.1 christos struct gdb_gnu_v3_abi_vtable {
58 1.1 christos
59 1.1 christos / * An array of virtual call and virtual base offsets. The real
60 1.1 christos length of this array depends on the class hierarchy; we use
61 1.1 christos negative subscripts to access the elements. Yucky, but
62 1.1 christos better than the alternatives. * /
63 1.1 christos ptrdiff_t vcall_and_vbase_offsets[0];
64 1.1 christos
65 1.1 christos / * The offset from a virtual pointer referring to this table
66 1.1 christos to the top of the complete object. * /
67 1.1 christos ptrdiff_t offset_to_top;
68 1.1 christos
69 1.1 christos / * The type_info pointer for this class. This is really a
70 1.1 christos std::type_info *, but GDB doesn't really look at the
71 1.1 christos type_info object itself, so we don't bother to get the type
72 1.1 christos exactly right. * /
73 1.1 christos void *type_info;
74 1.1 christos
75 1.1 christos / * Virtual table pointers in objects point here. * /
76 1.1 christos
77 1.1 christos / * Virtual function pointers. Like the vcall/vbase array, the
78 1.1 christos real length of this table depends on the class hierarchy. * /
79 1.1 christos void (*virtual_functions[0]) ();
80 1.1 christos
81 1.1 christos };
82 1.1 christos
83 1.1 christos The catch, of course, is that the exact layout of this table
84 1.1 christos depends on the ABI --- word size, endianness, alignment, etc. So
85 1.1 christos the GDB type object is actually a per-architecture kind of thing.
86 1.1 christos
87 1.1 christos vtable_type_gdbarch_data is a gdbarch per-architecture data pointer
88 1.1 christos which refers to the struct type * for this structure, laid out
89 1.1 christos appropriately for the architecture. */
90 1.1 christos static struct gdbarch_data *vtable_type_gdbarch_data;
91 1.1 christos
92 1.1 christos
93 1.1 christos /* Human-readable names for the numbers of the fields above. */
94 1.1 christos enum {
95 1.1 christos vtable_field_vcall_and_vbase_offsets,
96 1.1 christos vtable_field_offset_to_top,
97 1.1 christos vtable_field_type_info,
98 1.1 christos vtable_field_virtual_functions
99 1.1 christos };
100 1.1 christos
101 1.1 christos
102 1.1 christos /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable',
103 1.1 christos described above, laid out appropriately for ARCH.
104 1.1 christos
105 1.1 christos We use this function as the gdbarch per-architecture data
106 1.1 christos initialization function. */
107 1.1 christos static void *
108 1.1 christos build_gdb_vtable_type (struct gdbarch *arch)
109 1.1 christos {
110 1.1 christos struct type *t;
111 1.1 christos struct field *field_list, *field;
112 1.1 christos int offset;
113 1.1 christos
114 1.1 christos struct type *void_ptr_type
115 1.1 christos = builtin_type (arch)->builtin_data_ptr;
116 1.1 christos struct type *ptr_to_void_fn_type
117 1.1 christos = builtin_type (arch)->builtin_func_ptr;
118 1.1 christos
119 1.1 christos /* ARCH can't give us the true ptrdiff_t type, so we guess. */
120 1.1 christos struct type *ptrdiff_type
121 1.1 christos = arch_integer_type (arch, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
122 1.1 christos
123 1.1 christos /* We assume no padding is necessary, since GDB doesn't know
124 1.1 christos anything about alignment at the moment. If this assumption bites
125 1.1 christos us, we should add a gdbarch method which, given a type, returns
126 1.1 christos the alignment that type requires, and then use that here. */
127 1.1 christos
128 1.1 christos /* Build the field list. */
129 1.6 christos field_list = XCNEWVEC (struct field, 4);
130 1.1 christos field = &field_list[0];
131 1.1 christos offset = 0;
132 1.1 christos
133 1.1 christos /* ptrdiff_t vcall_and_vbase_offsets[0]; */
134 1.1 christos FIELD_NAME (*field) = "vcall_and_vbase_offsets";
135 1.1 christos FIELD_TYPE (*field) = lookup_array_range_type (ptrdiff_type, 0, -1);
136 1.1 christos SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
137 1.1 christos offset += TYPE_LENGTH (FIELD_TYPE (*field));
138 1.1 christos field++;
139 1.1 christos
140 1.1 christos /* ptrdiff_t offset_to_top; */
141 1.1 christos FIELD_NAME (*field) = "offset_to_top";
142 1.1 christos FIELD_TYPE (*field) = ptrdiff_type;
143 1.1 christos SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
144 1.1 christos offset += TYPE_LENGTH (FIELD_TYPE (*field));
145 1.1 christos field++;
146 1.1 christos
147 1.1 christos /* void *type_info; */
148 1.1 christos FIELD_NAME (*field) = "type_info";
149 1.1 christos FIELD_TYPE (*field) = void_ptr_type;
150 1.1 christos SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
151 1.1 christos offset += TYPE_LENGTH (FIELD_TYPE (*field));
152 1.1 christos field++;
153 1.1 christos
154 1.1 christos /* void (*virtual_functions[0]) (); */
155 1.1 christos FIELD_NAME (*field) = "virtual_functions";
156 1.1 christos FIELD_TYPE (*field) = lookup_array_range_type (ptr_to_void_fn_type, 0, -1);
157 1.1 christos SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
158 1.1 christos offset += TYPE_LENGTH (FIELD_TYPE (*field));
159 1.1 christos field++;
160 1.1 christos
161 1.1 christos /* We assumed in the allocation above that there were four fields. */
162 1.1 christos gdb_assert (field == (field_list + 4));
163 1.1 christos
164 1.1 christos t = arch_type (arch, TYPE_CODE_STRUCT, offset, NULL);
165 1.1 christos TYPE_NFIELDS (t) = field - field_list;
166 1.1 christos TYPE_FIELDS (t) = field_list;
167 1.1 christos TYPE_TAG_NAME (t) = "gdb_gnu_v3_abi_vtable";
168 1.1 christos INIT_CPLUS_SPECIFIC (t);
169 1.1 christos
170 1.3 christos return make_type_with_address_space (t, TYPE_INSTANCE_FLAG_CODE_SPACE);
171 1.1 christos }
172 1.1 christos
173 1.1 christos
174 1.1 christos /* Return the ptrdiff_t type used in the vtable type. */
175 1.1 christos static struct type *
176 1.1 christos vtable_ptrdiff_type (struct gdbarch *gdbarch)
177 1.1 christos {
178 1.6 christos struct type *vtable_type
179 1.6 christos = (struct type *) gdbarch_data (gdbarch, vtable_type_gdbarch_data);
180 1.1 christos
181 1.1 christos /* The "offset_to_top" field has the appropriate (ptrdiff_t) type. */
182 1.1 christos return TYPE_FIELD_TYPE (vtable_type, vtable_field_offset_to_top);
183 1.1 christos }
184 1.1 christos
185 1.1 christos /* Return the offset from the start of the imaginary `struct
186 1.1 christos gdb_gnu_v3_abi_vtable' object to the vtable's "address point"
187 1.1 christos (i.e., where objects' virtual table pointers point). */
188 1.1 christos static int
189 1.1 christos vtable_address_point_offset (struct gdbarch *gdbarch)
190 1.1 christos {
191 1.6 christos struct type *vtable_type
192 1.6 christos = (struct type *) gdbarch_data (gdbarch, vtable_type_gdbarch_data);
193 1.1 christos
194 1.1 christos return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions)
195 1.1 christos / TARGET_CHAR_BIT);
196 1.1 christos }
197 1.1 christos
198 1.1 christos
199 1.1 christos /* Determine whether structure TYPE is a dynamic class. Cache the
200 1.1 christos result. */
201 1.1 christos
202 1.1 christos static int
203 1.1 christos gnuv3_dynamic_class (struct type *type)
204 1.1 christos {
205 1.1 christos int fieldnum, fieldelem;
206 1.1 christos
207 1.6 christos type = check_typedef (type);
208 1.5 christos gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
209 1.5 christos || TYPE_CODE (type) == TYPE_CODE_UNION);
210 1.5 christos
211 1.5 christos if (TYPE_CODE (type) == TYPE_CODE_UNION)
212 1.5 christos return 0;
213 1.5 christos
214 1.1 christos if (TYPE_CPLUS_DYNAMIC (type))
215 1.1 christos return TYPE_CPLUS_DYNAMIC (type) == 1;
216 1.1 christos
217 1.1 christos ALLOCATE_CPLUS_STRUCT_TYPE (type);
218 1.1 christos
219 1.1 christos for (fieldnum = 0; fieldnum < TYPE_N_BASECLASSES (type); fieldnum++)
220 1.1 christos if (BASETYPE_VIA_VIRTUAL (type, fieldnum)
221 1.1 christos || gnuv3_dynamic_class (TYPE_FIELD_TYPE (type, fieldnum)))
222 1.1 christos {
223 1.1 christos TYPE_CPLUS_DYNAMIC (type) = 1;
224 1.1 christos return 1;
225 1.1 christos }
226 1.1 christos
227 1.1 christos for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
228 1.1 christos for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
229 1.1 christos fieldelem++)
230 1.1 christos {
231 1.1 christos struct fn_field *f = TYPE_FN_FIELDLIST1 (type, fieldnum);
232 1.1 christos
233 1.1 christos if (TYPE_FN_FIELD_VIRTUAL_P (f, fieldelem))
234 1.1 christos {
235 1.1 christos TYPE_CPLUS_DYNAMIC (type) = 1;
236 1.1 christos return 1;
237 1.1 christos }
238 1.1 christos }
239 1.1 christos
240 1.1 christos TYPE_CPLUS_DYNAMIC (type) = -1;
241 1.1 christos return 0;
242 1.1 christos }
243 1.1 christos
244 1.1 christos /* Find the vtable for a value of CONTAINER_TYPE located at
245 1.1 christos CONTAINER_ADDR. Return a value of the correct vtable type for this
246 1.1 christos architecture, or NULL if CONTAINER does not have a vtable. */
247 1.1 christos
248 1.1 christos static struct value *
249 1.1 christos gnuv3_get_vtable (struct gdbarch *gdbarch,
250 1.1 christos struct type *container_type, CORE_ADDR container_addr)
251 1.1 christos {
252 1.6 christos struct type *vtable_type
253 1.6 christos = (struct type *) gdbarch_data (gdbarch, vtable_type_gdbarch_data);
254 1.1 christos struct type *vtable_pointer_type;
255 1.1 christos struct value *vtable_pointer;
256 1.1 christos CORE_ADDR vtable_address;
257 1.1 christos
258 1.6 christos container_type = check_typedef (container_type);
259 1.5 christos gdb_assert (TYPE_CODE (container_type) == TYPE_CODE_STRUCT);
260 1.5 christos
261 1.1 christos /* If this type does not have a virtual table, don't read the first
262 1.1 christos field. */
263 1.5 christos if (!gnuv3_dynamic_class (container_type))
264 1.1 christos return NULL;
265 1.1 christos
266 1.1 christos /* We do not consult the debug information to find the virtual table.
267 1.1 christos The ABI specifies that it is always at offset zero in any class,
268 1.1 christos and debug information may not represent it.
269 1.1 christos
270 1.1 christos We avoid using value_contents on principle, because the object might
271 1.1 christos be large. */
272 1.1 christos
273 1.1 christos /* Find the type "pointer to virtual table". */
274 1.1 christos vtable_pointer_type = lookup_pointer_type (vtable_type);
275 1.1 christos
276 1.1 christos /* Load it from the start of the class. */
277 1.1 christos vtable_pointer = value_at (vtable_pointer_type, container_addr);
278 1.1 christos vtable_address = value_as_address (vtable_pointer);
279 1.1 christos
280 1.1 christos /* Correct it to point at the start of the virtual table, rather
281 1.1 christos than the address point. */
282 1.1 christos return value_at_lazy (vtable_type,
283 1.1 christos vtable_address
284 1.1 christos - vtable_address_point_offset (gdbarch));
285 1.1 christos }
286 1.1 christos
287 1.1 christos
288 1.1 christos static struct type *
289 1.1 christos gnuv3_rtti_type (struct value *value,
290 1.6 christos int *full_p, LONGEST *top_p, int *using_enc_p)
291 1.1 christos {
292 1.1 christos struct gdbarch *gdbarch;
293 1.1 christos struct type *values_type = check_typedef (value_type (value));
294 1.1 christos struct value *vtable;
295 1.1 christos struct minimal_symbol *vtable_symbol;
296 1.1 christos const char *vtable_symbol_name;
297 1.1 christos const char *class_name;
298 1.1 christos struct type *run_time_type;
299 1.1 christos LONGEST offset_to_top;
300 1.6 christos const char *atsign;
301 1.1 christos
302 1.1 christos /* We only have RTTI for class objects. */
303 1.3 christos if (TYPE_CODE (values_type) != TYPE_CODE_STRUCT)
304 1.1 christos return NULL;
305 1.1 christos
306 1.1 christos /* Determine architecture. */
307 1.1 christos gdbarch = get_type_arch (values_type);
308 1.1 christos
309 1.1 christos if (using_enc_p)
310 1.1 christos *using_enc_p = 0;
311 1.1 christos
312 1.5 christos vtable = gnuv3_get_vtable (gdbarch, values_type,
313 1.1 christos value_as_address (value_addr (value)));
314 1.1 christos if (vtable == NULL)
315 1.1 christos return NULL;
316 1.1 christos
317 1.1 christos /* Find the linker symbol for this vtable. */
318 1.1 christos vtable_symbol
319 1.1 christos = lookup_minimal_symbol_by_pc (value_address (vtable)
320 1.1 christos + value_embedded_offset (vtable)).minsym;
321 1.1 christos if (! vtable_symbol)
322 1.1 christos return NULL;
323 1.1 christos
324 1.1 christos /* The symbol's demangled name should be something like "vtable for
325 1.1 christos CLASS", where CLASS is the name of the run-time type of VALUE.
326 1.1 christos If we didn't like this approach, we could instead look in the
327 1.1 christos type_info object itself to get the class name. But this way
328 1.1 christos should work just as well, and doesn't read target memory. */
329 1.3 christos vtable_symbol_name = MSYMBOL_DEMANGLED_NAME (vtable_symbol);
330 1.1 christos if (vtable_symbol_name == NULL
331 1.5 christos || !startswith (vtable_symbol_name, "vtable for "))
332 1.1 christos {
333 1.1 christos warning (_("can't find linker symbol for virtual table for `%s' value"),
334 1.1 christos TYPE_SAFE_NAME (values_type));
335 1.1 christos if (vtable_symbol_name)
336 1.1 christos warning (_(" found `%s' instead"), vtable_symbol_name);
337 1.1 christos return NULL;
338 1.1 christos }
339 1.1 christos class_name = vtable_symbol_name + 11;
340 1.1 christos
341 1.1 christos /* Strip off @plt and version suffixes. */
342 1.1 christos atsign = strchr (class_name, '@');
343 1.1 christos if (atsign != NULL)
344 1.1 christos {
345 1.1 christos char *copy;
346 1.1 christos
347 1.6 christos copy = (char *) alloca (atsign - class_name + 1);
348 1.1 christos memcpy (copy, class_name, atsign - class_name);
349 1.1 christos copy[atsign - class_name] = '\0';
350 1.1 christos class_name = copy;
351 1.1 christos }
352 1.1 christos
353 1.1 christos /* Try to look up the class name as a type name. */
354 1.1 christos /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */
355 1.1 christos run_time_type = cp_lookup_rtti_type (class_name, NULL);
356 1.1 christos if (run_time_type == NULL)
357 1.1 christos return NULL;
358 1.1 christos
359 1.1 christos /* Get the offset from VALUE to the top of the complete object.
360 1.1 christos NOTE: this is the reverse of the meaning of *TOP_P. */
361 1.1 christos offset_to_top
362 1.1 christos = value_as_long (value_field (vtable, vtable_field_offset_to_top));
363 1.1 christos
364 1.1 christos if (full_p)
365 1.1 christos *full_p = (- offset_to_top == value_embedded_offset (value)
366 1.1 christos && (TYPE_LENGTH (value_enclosing_type (value))
367 1.1 christos >= TYPE_LENGTH (run_time_type)));
368 1.1 christos if (top_p)
369 1.1 christos *top_p = - offset_to_top;
370 1.1 christos return run_time_type;
371 1.1 christos }
372 1.1 christos
373 1.1 christos /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual
374 1.1 christos function, of type FNTYPE. */
375 1.1 christos
376 1.1 christos static struct value *
377 1.1 christos gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container,
378 1.1 christos struct type *fntype, int vtable_index)
379 1.1 christos {
380 1.1 christos struct value *vtable, *vfn;
381 1.1 christos
382 1.1 christos /* Every class with virtual functions must have a vtable. */
383 1.1 christos vtable = gnuv3_get_vtable (gdbarch, value_type (container),
384 1.1 christos value_as_address (value_addr (container)));
385 1.1 christos gdb_assert (vtable != NULL);
386 1.1 christos
387 1.1 christos /* Fetch the appropriate function pointer from the vtable. */
388 1.1 christos vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions),
389 1.1 christos vtable_index);
390 1.1 christos
391 1.1 christos /* If this architecture uses function descriptors directly in the vtable,
392 1.1 christos then the address of the vtable entry is actually a "function pointer"
393 1.1 christos (i.e. points to the descriptor). We don't need to scale the index
394 1.1 christos by the size of a function descriptor; GCC does that before outputing
395 1.1 christos debug information. */
396 1.1 christos if (gdbarch_vtable_function_descriptors (gdbarch))
397 1.1 christos vfn = value_addr (vfn);
398 1.1 christos
399 1.1 christos /* Cast the function pointer to the appropriate type. */
400 1.1 christos vfn = value_cast (lookup_pointer_type (fntype), vfn);
401 1.1 christos
402 1.1 christos return vfn;
403 1.1 christos }
404 1.1 christos
405 1.1 christos /* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h
406 1.1 christos for a description of the arguments. */
407 1.1 christos
408 1.1 christos static struct value *
409 1.1 christos gnuv3_virtual_fn_field (struct value **value_p,
410 1.1 christos struct fn_field *f, int j,
411 1.1 christos struct type *vfn_base, int offset)
412 1.1 christos {
413 1.1 christos struct type *values_type = check_typedef (value_type (*value_p));
414 1.1 christos struct gdbarch *gdbarch;
415 1.1 christos
416 1.1 christos /* Some simple sanity checks. */
417 1.3 christos if (TYPE_CODE (values_type) != TYPE_CODE_STRUCT)
418 1.1 christos error (_("Only classes can have virtual functions."));
419 1.1 christos
420 1.1 christos /* Determine architecture. */
421 1.1 christos gdbarch = get_type_arch (values_type);
422 1.1 christos
423 1.1 christos /* Cast our value to the base class which defines this virtual
424 1.1 christos function. This takes care of any necessary `this'
425 1.1 christos adjustments. */
426 1.1 christos if (vfn_base != values_type)
427 1.1 christos *value_p = value_cast (vfn_base, *value_p);
428 1.1 christos
429 1.1 christos return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j),
430 1.1 christos TYPE_FN_FIELD_VOFFSET (f, j));
431 1.1 christos }
432 1.1 christos
433 1.1 christos /* Compute the offset of the baseclass which is
434 1.1 christos the INDEXth baseclass of class TYPE,
435 1.1 christos for value at VALADDR (in host) at ADDRESS (in target).
436 1.1 christos The result is the offset of the baseclass value relative
437 1.1 christos to (the address of)(ARG) + OFFSET.
438 1.1 christos
439 1.1 christos -1 is returned on error. */
440 1.1 christos
441 1.1 christos static int
442 1.1 christos gnuv3_baseclass_offset (struct type *type, int index,
443 1.6 christos const bfd_byte *valaddr, LONGEST embedded_offset,
444 1.1 christos CORE_ADDR address, const struct value *val)
445 1.1 christos {
446 1.1 christos struct gdbarch *gdbarch;
447 1.1 christos struct type *ptr_type;
448 1.1 christos struct value *vtable;
449 1.1 christos struct value *vbase_array;
450 1.1 christos long int cur_base_offset, base_offset;
451 1.1 christos
452 1.1 christos /* Determine architecture. */
453 1.1 christos gdbarch = get_type_arch (type);
454 1.1 christos ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
455 1.1 christos
456 1.1 christos /* If it isn't a virtual base, this is easy. The offset is in the
457 1.7 christos type definition. */
458 1.7 christos if (!BASETYPE_VIA_VIRTUAL (type, index))
459 1.1 christos return TYPE_BASECLASS_BITPOS (type, index) / 8;
460 1.1 christos
461 1.1 christos /* To access a virtual base, we need to use the vbase offset stored in
462 1.1 christos our vtable. Recent GCC versions provide this information. If it isn't
463 1.1 christos available, we could get what we needed from RTTI, or from drawing the
464 1.1 christos complete inheritance graph based on the debug info. Neither is
465 1.1 christos worthwhile. */
466 1.1 christos cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8;
467 1.1 christos if (cur_base_offset >= - vtable_address_point_offset (gdbarch))
468 1.1 christos error (_("Expected a negative vbase offset (old compiler?)"));
469 1.1 christos
470 1.1 christos cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch);
471 1.1 christos if ((- cur_base_offset) % TYPE_LENGTH (ptr_type) != 0)
472 1.1 christos error (_("Misaligned vbase offset."));
473 1.1 christos cur_base_offset = cur_base_offset / ((int) TYPE_LENGTH (ptr_type));
474 1.1 christos
475 1.1 christos vtable = gnuv3_get_vtable (gdbarch, type, address + embedded_offset);
476 1.1 christos gdb_assert (vtable != NULL);
477 1.1 christos vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets);
478 1.1 christos base_offset = value_as_long (value_subscript (vbase_array, cur_base_offset));
479 1.1 christos return base_offset;
480 1.1 christos }
481 1.1 christos
482 1.1 christos /* Locate a virtual method in DOMAIN or its non-virtual base classes
483 1.1 christos which has virtual table index VOFFSET. The method has an associated
484 1.1 christos "this" adjustment of ADJUSTMENT bytes. */
485 1.1 christos
486 1.1 christos static const char *
487 1.1 christos gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset,
488 1.1 christos LONGEST adjustment)
489 1.1 christos {
490 1.1 christos int i;
491 1.1 christos
492 1.1 christos /* Search this class first. */
493 1.1 christos if (adjustment == 0)
494 1.1 christos {
495 1.1 christos int len;
496 1.1 christos
497 1.1 christos len = TYPE_NFN_FIELDS (domain);
498 1.1 christos for (i = 0; i < len; i++)
499 1.1 christos {
500 1.1 christos int len2, j;
501 1.1 christos struct fn_field *f;
502 1.1 christos
503 1.1 christos f = TYPE_FN_FIELDLIST1 (domain, i);
504 1.1 christos len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
505 1.1 christos
506 1.1 christos check_stub_method_group (domain, i);
507 1.1 christos for (j = 0; j < len2; j++)
508 1.1 christos if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset)
509 1.1 christos return TYPE_FN_FIELD_PHYSNAME (f, j);
510 1.1 christos }
511 1.1 christos }
512 1.1 christos
513 1.1 christos /* Next search non-virtual bases. If it's in a virtual base,
514 1.1 christos we're out of luck. */
515 1.1 christos for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
516 1.1 christos {
517 1.1 christos int pos;
518 1.1 christos struct type *basetype;
519 1.1 christos
520 1.1 christos if (BASETYPE_VIA_VIRTUAL (domain, i))
521 1.1 christos continue;
522 1.1 christos
523 1.1 christos pos = TYPE_BASECLASS_BITPOS (domain, i) / 8;
524 1.1 christos basetype = TYPE_FIELD_TYPE (domain, i);
525 1.1 christos /* Recurse with a modified adjustment. We don't need to adjust
526 1.1 christos voffset. */
527 1.1 christos if (adjustment >= pos && adjustment < pos + TYPE_LENGTH (basetype))
528 1.1 christos return gnuv3_find_method_in (basetype, voffset, adjustment - pos);
529 1.1 christos }
530 1.1 christos
531 1.1 christos return NULL;
532 1.1 christos }
533 1.1 christos
534 1.1 christos /* Decode GNU v3 method pointer. */
535 1.1 christos
536 1.1 christos static int
537 1.1 christos gnuv3_decode_method_ptr (struct gdbarch *gdbarch,
538 1.1 christos const gdb_byte *contents,
539 1.1 christos CORE_ADDR *value_p,
540 1.1 christos LONGEST *adjustment_p)
541 1.1 christos {
542 1.1 christos struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr;
543 1.1 christos struct type *offset_type = vtable_ptrdiff_type (gdbarch);
544 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
545 1.1 christos CORE_ADDR ptr_value;
546 1.1 christos LONGEST voffset, adjustment;
547 1.1 christos int vbit;
548 1.1 christos
549 1.1 christos /* Extract the pointer to member. The first element is either a pointer
550 1.1 christos or a vtable offset. For pointers, we need to use extract_typed_address
551 1.1 christos to allow the back-end to convert the pointer to a GDB address -- but
552 1.1 christos vtable offsets we must handle as integers. At this point, we do not
553 1.1 christos yet know which case we have, so we extract the value under both
554 1.1 christos interpretations and choose the right one later on. */
555 1.1 christos ptr_value = extract_typed_address (contents, funcptr_type);
556 1.1 christos voffset = extract_signed_integer (contents,
557 1.1 christos TYPE_LENGTH (funcptr_type), byte_order);
558 1.1 christos contents += TYPE_LENGTH (funcptr_type);
559 1.1 christos adjustment = extract_signed_integer (contents,
560 1.1 christos TYPE_LENGTH (offset_type), byte_order);
561 1.1 christos
562 1.1 christos if (!gdbarch_vbit_in_delta (gdbarch))
563 1.1 christos {
564 1.1 christos vbit = voffset & 1;
565 1.1 christos voffset = voffset ^ vbit;
566 1.1 christos }
567 1.1 christos else
568 1.1 christos {
569 1.1 christos vbit = adjustment & 1;
570 1.1 christos adjustment = adjustment >> 1;
571 1.1 christos }
572 1.1 christos
573 1.1 christos *value_p = vbit? voffset : ptr_value;
574 1.1 christos *adjustment_p = adjustment;
575 1.1 christos return vbit;
576 1.1 christos }
577 1.1 christos
578 1.1 christos /* GNU v3 implementation of cplus_print_method_ptr. */
579 1.1 christos
580 1.1 christos static void
581 1.1 christos gnuv3_print_method_ptr (const gdb_byte *contents,
582 1.1 christos struct type *type,
583 1.1 christos struct ui_file *stream)
584 1.1 christos {
585 1.5 christos struct type *self_type = TYPE_SELF_TYPE (type);
586 1.5 christos struct gdbarch *gdbarch = get_type_arch (self_type);
587 1.1 christos CORE_ADDR ptr_value;
588 1.1 christos LONGEST adjustment;
589 1.1 christos int vbit;
590 1.1 christos
591 1.1 christos /* Extract the pointer to member. */
592 1.1 christos vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
593 1.1 christos
594 1.1 christos /* Check for NULL. */
595 1.1 christos if (ptr_value == 0 && vbit == 0)
596 1.1 christos {
597 1.1 christos fprintf_filtered (stream, "NULL");
598 1.1 christos return;
599 1.1 christos }
600 1.1 christos
601 1.1 christos /* Search for a virtual method. */
602 1.1 christos if (vbit)
603 1.1 christos {
604 1.1 christos CORE_ADDR voffset;
605 1.1 christos const char *physname;
606 1.1 christos
607 1.1 christos /* It's a virtual table offset, maybe in this class. Search
608 1.1 christos for a field with the correct vtable offset. First convert it
609 1.1 christos to an index, as used in TYPE_FN_FIELD_VOFFSET. */
610 1.1 christos voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
611 1.1 christos
612 1.5 christos physname = gnuv3_find_method_in (self_type, voffset, adjustment);
613 1.1 christos
614 1.1 christos /* If we found a method, print that. We don't bother to disambiguate
615 1.1 christos possible paths to the method based on the adjustment. */
616 1.1 christos if (physname)
617 1.1 christos {
618 1.1 christos char *demangled_name = gdb_demangle (physname,
619 1.1 christos DMGL_ANSI | DMGL_PARAMS);
620 1.1 christos
621 1.1 christos fprintf_filtered (stream, "&virtual ");
622 1.1 christos if (demangled_name == NULL)
623 1.1 christos fputs_filtered (physname, stream);
624 1.1 christos else
625 1.1 christos {
626 1.1 christos fputs_filtered (demangled_name, stream);
627 1.1 christos xfree (demangled_name);
628 1.1 christos }
629 1.1 christos return;
630 1.1 christos }
631 1.1 christos }
632 1.1 christos else if (ptr_value != 0)
633 1.1 christos {
634 1.1 christos /* Found a non-virtual function: print out the type. */
635 1.1 christos fputs_filtered ("(", stream);
636 1.1 christos c_print_type (type, "", stream, -1, 0, &type_print_raw_options);
637 1.1 christos fputs_filtered (") ", stream);
638 1.1 christos }
639 1.1 christos
640 1.1 christos /* We didn't find it; print the raw data. */
641 1.1 christos if (vbit)
642 1.1 christos {
643 1.1 christos fprintf_filtered (stream, "&virtual table offset ");
644 1.1 christos print_longest (stream, 'd', 1, ptr_value);
645 1.1 christos }
646 1.1 christos else
647 1.1 christos {
648 1.1 christos struct value_print_options opts;
649 1.1 christos
650 1.1 christos get_user_print_options (&opts);
651 1.1 christos print_address_demangle (&opts, gdbarch, ptr_value, stream, demangle);
652 1.1 christos }
653 1.1 christos
654 1.1 christos if (adjustment)
655 1.1 christos {
656 1.1 christos fprintf_filtered (stream, ", this adjustment ");
657 1.1 christos print_longest (stream, 'd', 1, adjustment);
658 1.1 christos }
659 1.1 christos }
660 1.1 christos
661 1.1 christos /* GNU v3 implementation of cplus_method_ptr_size. */
662 1.1 christos
663 1.1 christos static int
664 1.1 christos gnuv3_method_ptr_size (struct type *type)
665 1.1 christos {
666 1.1 christos struct gdbarch *gdbarch = get_type_arch (type);
667 1.1 christos
668 1.1 christos return 2 * TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
669 1.1 christos }
670 1.1 christos
671 1.1 christos /* GNU v3 implementation of cplus_make_method_ptr. */
672 1.1 christos
673 1.1 christos static void
674 1.1 christos gnuv3_make_method_ptr (struct type *type, gdb_byte *contents,
675 1.1 christos CORE_ADDR value, int is_virtual)
676 1.1 christos {
677 1.1 christos struct gdbarch *gdbarch = get_type_arch (type);
678 1.1 christos int size = TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
679 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
680 1.1 christos
681 1.1 christos /* FIXME drow/2006-12-24: The adjustment of "this" is currently
682 1.1 christos always zero, since the method pointer is of the correct type.
683 1.1 christos But if the method pointer came from a base class, this is
684 1.1 christos incorrect - it should be the offset to the base. The best
685 1.1 christos fix might be to create the pointer to member pointing at the
686 1.1 christos base class and cast it to the derived class, but that requires
687 1.1 christos support for adjusting pointers to members when casting them -
688 1.1 christos not currently supported by GDB. */
689 1.1 christos
690 1.1 christos if (!gdbarch_vbit_in_delta (gdbarch))
691 1.1 christos {
692 1.1 christos store_unsigned_integer (contents, size, byte_order, value | is_virtual);
693 1.1 christos store_unsigned_integer (contents + size, size, byte_order, 0);
694 1.1 christos }
695 1.1 christos else
696 1.1 christos {
697 1.1 christos store_unsigned_integer (contents, size, byte_order, value);
698 1.1 christos store_unsigned_integer (contents + size, size, byte_order, is_virtual);
699 1.1 christos }
700 1.1 christos }
701 1.1 christos
702 1.1 christos /* GNU v3 implementation of cplus_method_ptr_to_value. */
703 1.1 christos
704 1.1 christos static struct value *
705 1.1 christos gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
706 1.1 christos {
707 1.1 christos struct gdbarch *gdbarch;
708 1.1 christos const gdb_byte *contents = value_contents (method_ptr);
709 1.1 christos CORE_ADDR ptr_value;
710 1.5 christos struct type *self_type, *final_type, *method_type;
711 1.1 christos LONGEST adjustment;
712 1.1 christos int vbit;
713 1.1 christos
714 1.5 christos self_type = TYPE_SELF_TYPE (check_typedef (value_type (method_ptr)));
715 1.5 christos final_type = lookup_pointer_type (self_type);
716 1.1 christos
717 1.1 christos method_type = TYPE_TARGET_TYPE (check_typedef (value_type (method_ptr)));
718 1.1 christos
719 1.1 christos /* Extract the pointer to member. */
720 1.5 christos gdbarch = get_type_arch (self_type);
721 1.1 christos vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment);
722 1.1 christos
723 1.1 christos /* First convert THIS to match the containing type of the pointer to
724 1.1 christos member. This cast may adjust the value of THIS. */
725 1.1 christos *this_p = value_cast (final_type, *this_p);
726 1.1 christos
727 1.1 christos /* Then apply whatever adjustment is necessary. This creates a somewhat
728 1.1 christos strange pointer: it claims to have type FINAL_TYPE, but in fact it
729 1.1 christos might not be a valid FINAL_TYPE. For instance, it might be a
730 1.1 christos base class of FINAL_TYPE. And if it's not the primary base class,
731 1.1 christos then printing it out as a FINAL_TYPE object would produce some pretty
732 1.1 christos garbage.
733 1.1 christos
734 1.1 christos But we don't really know the type of the first argument in
735 1.1 christos METHOD_TYPE either, which is why this happens. We can't
736 1.1 christos dereference this later as a FINAL_TYPE, but once we arrive in the
737 1.1 christos called method we'll have debugging information for the type of
738 1.1 christos "this" - and that'll match the value we produce here.
739 1.1 christos
740 1.1 christos You can provoke this case by casting a Base::* to a Derived::*, for
741 1.1 christos instance. */
742 1.1 christos *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p);
743 1.1 christos *this_p = value_ptradd (*this_p, adjustment);
744 1.1 christos *this_p = value_cast (final_type, *this_p);
745 1.1 christos
746 1.1 christos if (vbit)
747 1.1 christos {
748 1.1 christos LONGEST voffset;
749 1.1 christos
750 1.1 christos voffset = ptr_value / TYPE_LENGTH (vtable_ptrdiff_type (gdbarch));
751 1.1 christos return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p),
752 1.1 christos method_type, voffset);
753 1.1 christos }
754 1.1 christos else
755 1.1 christos return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
756 1.1 christos }
757 1.1 christos
758 1.1 christos /* Objects of this type are stored in a hash table and a vector when
759 1.1 christos printing the vtables for a class. */
760 1.1 christos
761 1.1 christos struct value_and_voffset
762 1.1 christos {
763 1.1 christos /* The value representing the object. */
764 1.1 christos struct value *value;
765 1.1 christos
766 1.1 christos /* The maximum vtable offset we've found for any object at this
767 1.1 christos offset in the outermost object. */
768 1.1 christos int max_voffset;
769 1.1 christos };
770 1.1 christos
771 1.1 christos /* Hash function for value_and_voffset. */
772 1.1 christos
773 1.1 christos static hashval_t
774 1.1 christos hash_value_and_voffset (const void *p)
775 1.1 christos {
776 1.6 christos const struct value_and_voffset *o = (const struct value_and_voffset *) p;
777 1.1 christos
778 1.1 christos return value_address (o->value) + value_embedded_offset (o->value);
779 1.1 christos }
780 1.1 christos
781 1.1 christos /* Equality function for value_and_voffset. */
782 1.1 christos
783 1.1 christos static int
784 1.1 christos eq_value_and_voffset (const void *a, const void *b)
785 1.1 christos {
786 1.6 christos const struct value_and_voffset *ova = (const struct value_and_voffset *) a;
787 1.6 christos const struct value_and_voffset *ovb = (const struct value_and_voffset *) b;
788 1.1 christos
789 1.1 christos return (value_address (ova->value) + value_embedded_offset (ova->value)
790 1.1 christos == value_address (ovb->value) + value_embedded_offset (ovb->value));
791 1.1 christos }
792 1.1 christos
793 1.7 christos /* Comparison function for value_and_voffset. */
794 1.1 christos
795 1.7 christos static bool
796 1.7 christos compare_value_and_voffset (const struct value_and_voffset *va,
797 1.7 christos const struct value_and_voffset *vb)
798 1.7 christos {
799 1.7 christos CORE_ADDR addra = (value_address (va->value)
800 1.7 christos + value_embedded_offset (va->value));
801 1.7 christos CORE_ADDR addrb = (value_address (vb->value)
802 1.7 christos + value_embedded_offset (vb->value));
803 1.7 christos
804 1.7 christos return addra < addrb;
805 1.1 christos }
806 1.1 christos
807 1.1 christos /* A helper function used when printing vtables. This determines the
808 1.1 christos key (most derived) sub-object at each address and also computes the
809 1.1 christos maximum vtable offset seen for the corresponding vtable. Updates
810 1.1 christos OFFSET_HASH and OFFSET_VEC with a new value_and_voffset object, if
811 1.1 christos needed. VALUE is the object to examine. */
812 1.1 christos
813 1.1 christos static void
814 1.1 christos compute_vtable_size (htab_t offset_hash,
815 1.7 christos std::vector<value_and_voffset *> *offset_vec,
816 1.1 christos struct value *value)
817 1.1 christos {
818 1.1 christos int i;
819 1.1 christos struct type *type = check_typedef (value_type (value));
820 1.1 christos void **slot;
821 1.1 christos struct value_and_voffset search_vo, *current_vo;
822 1.1 christos
823 1.5 christos gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT);
824 1.5 christos
825 1.1 christos /* If the object is not dynamic, then we are done; as it cannot have
826 1.1 christos dynamic base types either. */
827 1.1 christos if (!gnuv3_dynamic_class (type))
828 1.1 christos return;
829 1.1 christos
830 1.1 christos /* Update the hash and the vec, if needed. */
831 1.1 christos search_vo.value = value;
832 1.1 christos slot = htab_find_slot (offset_hash, &search_vo, INSERT);
833 1.1 christos if (*slot)
834 1.6 christos current_vo = (struct value_and_voffset *) *slot;
835 1.1 christos else
836 1.1 christos {
837 1.1 christos current_vo = XNEW (struct value_and_voffset);
838 1.1 christos current_vo->value = value;
839 1.1 christos current_vo->max_voffset = -1;
840 1.1 christos *slot = current_vo;
841 1.7 christos offset_vec->push_back (current_vo);
842 1.1 christos }
843 1.1 christos
844 1.1 christos /* Update the value_and_voffset object with the highest vtable
845 1.1 christos offset from this class. */
846 1.1 christos for (i = 0; i < TYPE_NFN_FIELDS (type); ++i)
847 1.1 christos {
848 1.1 christos int j;
849 1.1 christos struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, i);
850 1.1 christos
851 1.1 christos for (j = 0; j < TYPE_FN_FIELDLIST_LENGTH (type, i); ++j)
852 1.1 christos {
853 1.1 christos if (TYPE_FN_FIELD_VIRTUAL_P (fn, j))
854 1.1 christos {
855 1.1 christos int voffset = TYPE_FN_FIELD_VOFFSET (fn, j);
856 1.1 christos
857 1.1 christos if (voffset > current_vo->max_voffset)
858 1.1 christos current_vo->max_voffset = voffset;
859 1.1 christos }
860 1.1 christos }
861 1.1 christos }
862 1.1 christos
863 1.1 christos /* Recurse into base classes. */
864 1.1 christos for (i = 0; i < TYPE_N_BASECLASSES (type); ++i)
865 1.1 christos compute_vtable_size (offset_hash, offset_vec, value_field (value, i));
866 1.1 christos }
867 1.1 christos
868 1.1 christos /* Helper for gnuv3_print_vtable that prints a single vtable. */
869 1.1 christos
870 1.1 christos static void
871 1.1 christos print_one_vtable (struct gdbarch *gdbarch, struct value *value,
872 1.1 christos int max_voffset,
873 1.1 christos struct value_print_options *opts)
874 1.1 christos {
875 1.1 christos int i;
876 1.1 christos struct type *type = check_typedef (value_type (value));
877 1.1 christos struct value *vtable;
878 1.1 christos CORE_ADDR vt_addr;
879 1.1 christos
880 1.1 christos vtable = gnuv3_get_vtable (gdbarch, type,
881 1.1 christos value_address (value)
882 1.1 christos + value_embedded_offset (value));
883 1.1 christos vt_addr = value_address (value_field (vtable,
884 1.1 christos vtable_field_virtual_functions));
885 1.1 christos
886 1.1 christos printf_filtered (_("vtable for '%s' @ %s (subobject @ %s):\n"),
887 1.1 christos TYPE_SAFE_NAME (type),
888 1.1 christos paddress (gdbarch, vt_addr),
889 1.1 christos paddress (gdbarch, (value_address (value)
890 1.1 christos + value_embedded_offset (value))));
891 1.1 christos
892 1.1 christos for (i = 0; i <= max_voffset; ++i)
893 1.1 christos {
894 1.1 christos /* Initialize it just to avoid a GCC false warning. */
895 1.1 christos CORE_ADDR addr = 0;
896 1.5 christos int got_error = 0;
897 1.1 christos struct value *vfn;
898 1.1 christos
899 1.1 christos printf_filtered ("[%d]: ", i);
900 1.1 christos
901 1.1 christos vfn = value_subscript (value_field (vtable,
902 1.1 christos vtable_field_virtual_functions),
903 1.1 christos i);
904 1.1 christos
905 1.1 christos if (gdbarch_vtable_function_descriptors (gdbarch))
906 1.1 christos vfn = value_addr (vfn);
907 1.1 christos
908 1.5 christos TRY
909 1.1 christos {
910 1.1 christos addr = value_as_address (vfn);
911 1.1 christos }
912 1.5 christos CATCH (ex, RETURN_MASK_ERROR)
913 1.5 christos {
914 1.5 christos printf_filtered (_("<error: %s>"), ex.message);
915 1.5 christos got_error = 1;
916 1.5 christos }
917 1.5 christos END_CATCH
918 1.5 christos
919 1.5 christos if (!got_error)
920 1.1 christos print_function_pointer_address (opts, gdbarch, addr, gdb_stdout);
921 1.1 christos printf_filtered ("\n");
922 1.1 christos }
923 1.1 christos }
924 1.1 christos
925 1.1 christos /* Implementation of the print_vtable method. */
926 1.1 christos
927 1.1 christos static void
928 1.1 christos gnuv3_print_vtable (struct value *value)
929 1.1 christos {
930 1.1 christos struct gdbarch *gdbarch;
931 1.1 christos struct type *type;
932 1.1 christos struct value *vtable;
933 1.1 christos struct value_print_options opts;
934 1.7 christos int count;
935 1.1 christos
936 1.1 christos value = coerce_ref (value);
937 1.1 christos type = check_typedef (value_type (value));
938 1.1 christos if (TYPE_CODE (type) == TYPE_CODE_PTR)
939 1.1 christos {
940 1.1 christos value = value_ind (value);
941 1.1 christos type = check_typedef (value_type (value));
942 1.1 christos }
943 1.1 christos
944 1.1 christos get_user_print_options (&opts);
945 1.1 christos
946 1.1 christos /* Respect 'set print object'. */
947 1.1 christos if (opts.objectprint)
948 1.1 christos {
949 1.1 christos value = value_full_object (value, NULL, 0, 0, 0);
950 1.1 christos type = check_typedef (value_type (value));
951 1.1 christos }
952 1.1 christos
953 1.1 christos gdbarch = get_type_arch (type);
954 1.5 christos
955 1.5 christos vtable = NULL;
956 1.5 christos if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
957 1.5 christos vtable = gnuv3_get_vtable (gdbarch, type,
958 1.5 christos value_as_address (value_addr (value)));
959 1.1 christos
960 1.1 christos if (!vtable)
961 1.1 christos {
962 1.1 christos printf_filtered (_("This object does not have a virtual function table\n"));
963 1.1 christos return;
964 1.1 christos }
965 1.1 christos
966 1.7 christos htab_up offset_hash (htab_create_alloc (1, hash_value_and_voffset,
967 1.7 christos eq_value_and_voffset,
968 1.7 christos xfree, xcalloc, xfree));
969 1.7 christos std::vector<value_and_voffset *> result_vec;
970 1.7 christos
971 1.7 christos compute_vtable_size (offset_hash.get (), &result_vec, value);
972 1.7 christos std::sort (result_vec.begin (), result_vec.end (),
973 1.7 christos compare_value_and_voffset);
974 1.1 christos
975 1.1 christos count = 0;
976 1.7 christos for (value_and_voffset *iter : result_vec)
977 1.1 christos {
978 1.1 christos if (iter->max_voffset >= 0)
979 1.1 christos {
980 1.1 christos if (count > 0)
981 1.1 christos printf_filtered ("\n");
982 1.1 christos print_one_vtable (gdbarch, iter->value, iter->max_voffset, &opts);
983 1.1 christos ++count;
984 1.1 christos }
985 1.1 christos }
986 1.1 christos }
987 1.1 christos
988 1.1 christos /* Return a GDB type representing `struct std::type_info', laid out
989 1.1 christos appropriately for ARCH.
990 1.1 christos
991 1.1 christos We use this function as the gdbarch per-architecture data
992 1.1 christos initialization function. */
993 1.1 christos
994 1.1 christos static void *
995 1.1 christos build_std_type_info_type (struct gdbarch *arch)
996 1.1 christos {
997 1.1 christos struct type *t;
998 1.1 christos struct field *field_list, *field;
999 1.1 christos int offset;
1000 1.1 christos struct type *void_ptr_type
1001 1.1 christos = builtin_type (arch)->builtin_data_ptr;
1002 1.1 christos struct type *char_type
1003 1.1 christos = builtin_type (arch)->builtin_char;
1004 1.1 christos struct type *char_ptr_type
1005 1.1 christos = make_pointer_type (make_cv_type (1, 0, char_type, NULL), NULL);
1006 1.1 christos
1007 1.6 christos field_list = XCNEWVEC (struct field, 2);
1008 1.1 christos field = &field_list[0];
1009 1.1 christos offset = 0;
1010 1.1 christos
1011 1.1 christos /* The vtable. */
1012 1.1 christos FIELD_NAME (*field) = "_vptr.type_info";
1013 1.1 christos FIELD_TYPE (*field) = void_ptr_type;
1014 1.1 christos SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
1015 1.1 christos offset += TYPE_LENGTH (FIELD_TYPE (*field));
1016 1.1 christos field++;
1017 1.1 christos
1018 1.1 christos /* The name. */
1019 1.1 christos FIELD_NAME (*field) = "__name";
1020 1.1 christos FIELD_TYPE (*field) = char_ptr_type;
1021 1.1 christos SET_FIELD_BITPOS (*field, offset * TARGET_CHAR_BIT);
1022 1.1 christos offset += TYPE_LENGTH (FIELD_TYPE (*field));
1023 1.1 christos field++;
1024 1.1 christos
1025 1.1 christos gdb_assert (field == (field_list + 2));
1026 1.1 christos
1027 1.1 christos t = arch_type (arch, TYPE_CODE_STRUCT, offset, NULL);
1028 1.1 christos TYPE_NFIELDS (t) = field - field_list;
1029 1.1 christos TYPE_FIELDS (t) = field_list;
1030 1.1 christos TYPE_TAG_NAME (t) = "gdb_gnu_v3_type_info";
1031 1.1 christos INIT_CPLUS_SPECIFIC (t);
1032 1.1 christos
1033 1.1 christos return t;
1034 1.1 christos }
1035 1.1 christos
1036 1.1 christos /* Implement the 'get_typeid_type' method. */
1037 1.1 christos
1038 1.1 christos static struct type *
1039 1.1 christos gnuv3_get_typeid_type (struct gdbarch *gdbarch)
1040 1.1 christos {
1041 1.1 christos struct symbol *typeinfo;
1042 1.1 christos struct type *typeinfo_type;
1043 1.1 christos
1044 1.6 christos typeinfo = lookup_symbol ("std::type_info", NULL, STRUCT_DOMAIN,
1045 1.6 christos NULL).symbol;
1046 1.1 christos if (typeinfo == NULL)
1047 1.6 christos typeinfo_type
1048 1.6 christos = (struct type *) gdbarch_data (gdbarch, std_type_info_gdbarch_data);
1049 1.1 christos else
1050 1.1 christos typeinfo_type = SYMBOL_TYPE (typeinfo);
1051 1.1 christos
1052 1.1 christos return typeinfo_type;
1053 1.1 christos }
1054 1.1 christos
1055 1.1 christos /* Implement the 'get_typeid' method. */
1056 1.1 christos
1057 1.1 christos static struct value *
1058 1.1 christos gnuv3_get_typeid (struct value *value)
1059 1.1 christos {
1060 1.1 christos struct type *typeinfo_type;
1061 1.1 christos struct type *type;
1062 1.1 christos struct gdbarch *gdbarch;
1063 1.1 christos struct value *result;
1064 1.7 christos std::string type_name, canonical;
1065 1.1 christos
1066 1.1 christos /* We have to handle values a bit trickily here, to allow this code
1067 1.1 christos to work properly with non_lvalue values that are really just
1068 1.1 christos disguised types. */
1069 1.1 christos if (value_lval_const (value) == lval_memory)
1070 1.1 christos value = coerce_ref (value);
1071 1.1 christos
1072 1.1 christos type = check_typedef (value_type (value));
1073 1.1 christos
1074 1.1 christos /* In the non_lvalue case, a reference might have slipped through
1075 1.1 christos here. */
1076 1.1 christos if (TYPE_CODE (type) == TYPE_CODE_REF)
1077 1.1 christos type = check_typedef (TYPE_TARGET_TYPE (type));
1078 1.1 christos
1079 1.1 christos /* Ignore top-level cv-qualifiers. */
1080 1.1 christos type = make_cv_type (0, 0, type, NULL);
1081 1.1 christos gdbarch = get_type_arch (type);
1082 1.1 christos
1083 1.5 christos type_name = type_to_string (type);
1084 1.7 christos if (type_name.empty ())
1085 1.1 christos error (_("cannot find typeinfo for unnamed type"));
1086 1.1 christos
1087 1.1 christos /* We need to canonicalize the type name here, because we do lookups
1088 1.1 christos using the demangled name, and so we must match the format it
1089 1.1 christos uses. E.g., GDB tends to use "const char *" as a type name, but
1090 1.1 christos the demangler uses "char const *". */
1091 1.7 christos canonical = cp_canonicalize_string (type_name.c_str ());
1092 1.7 christos if (!canonical.empty ())
1093 1.7 christos type_name = canonical;
1094 1.1 christos
1095 1.1 christos typeinfo_type = gnuv3_get_typeid_type (gdbarch);
1096 1.1 christos
1097 1.1 christos /* We check for lval_memory because in the "typeid (type-id)" case,
1098 1.1 christos the type is passed via a not_lval value object. */
1099 1.3 christos if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1100 1.1 christos && value_lval_const (value) == lval_memory
1101 1.1 christos && gnuv3_dynamic_class (type))
1102 1.1 christos {
1103 1.1 christos struct value *vtable, *typeinfo_value;
1104 1.1 christos CORE_ADDR address = value_address (value) + value_embedded_offset (value);
1105 1.1 christos
1106 1.1 christos vtable = gnuv3_get_vtable (gdbarch, type, address);
1107 1.1 christos if (vtable == NULL)
1108 1.7 christos error (_("cannot find typeinfo for object of type '%s'"),
1109 1.7 christos type_name.c_str ());
1110 1.1 christos typeinfo_value = value_field (vtable, vtable_field_type_info);
1111 1.1 christos result = value_ind (value_cast (make_pointer_type (typeinfo_type, NULL),
1112 1.1 christos typeinfo_value));
1113 1.1 christos }
1114 1.1 christos else
1115 1.1 christos {
1116 1.7 christos std::string sym_name = std::string ("typeinfo for ") + type_name;
1117 1.7 christos bound_minimal_symbol minsym
1118 1.7 christos = lookup_minimal_symbol (sym_name.c_str (), NULL, NULL);
1119 1.1 christos
1120 1.3 christos if (minsym.minsym == NULL)
1121 1.7 christos error (_("could not find typeinfo symbol for '%s'"), type_name.c_str ());
1122 1.1 christos
1123 1.3 christos result = value_at_lazy (typeinfo_type, BMSYMBOL_VALUE_ADDRESS (minsym));
1124 1.1 christos }
1125 1.1 christos
1126 1.1 christos return result;
1127 1.1 christos }
1128 1.1 christos
1129 1.1 christos /* Implement the 'get_typename_from_type_info' method. */
1130 1.1 christos
1131 1.7 christos static std::string
1132 1.1 christos gnuv3_get_typename_from_type_info (struct value *type_info_ptr)
1133 1.1 christos {
1134 1.1 christos struct gdbarch *gdbarch = get_type_arch (value_type (type_info_ptr));
1135 1.1 christos struct bound_minimal_symbol typeinfo_sym;
1136 1.1 christos CORE_ADDR addr;
1137 1.1 christos const char *symname;
1138 1.1 christos const char *class_name;
1139 1.1 christos const char *atsign;
1140 1.1 christos
1141 1.1 christos addr = value_as_address (type_info_ptr);
1142 1.1 christos typeinfo_sym = lookup_minimal_symbol_by_pc (addr);
1143 1.1 christos if (typeinfo_sym.minsym == NULL)
1144 1.1 christos error (_("could not find minimal symbol for typeinfo address %s"),
1145 1.1 christos paddress (gdbarch, addr));
1146 1.1 christos
1147 1.1 christos #define TYPEINFO_PREFIX "typeinfo for "
1148 1.1 christos #define TYPEINFO_PREFIX_LEN (sizeof (TYPEINFO_PREFIX) - 1)
1149 1.3 christos symname = MSYMBOL_DEMANGLED_NAME (typeinfo_sym.minsym);
1150 1.1 christos if (symname == NULL || strncmp (symname, TYPEINFO_PREFIX,
1151 1.1 christos TYPEINFO_PREFIX_LEN))
1152 1.1 christos error (_("typeinfo symbol '%s' has unexpected name"),
1153 1.3 christos MSYMBOL_LINKAGE_NAME (typeinfo_sym.minsym));
1154 1.1 christos class_name = symname + TYPEINFO_PREFIX_LEN;
1155 1.1 christos
1156 1.1 christos /* Strip off @plt and version suffixes. */
1157 1.1 christos atsign = strchr (class_name, '@');
1158 1.1 christos if (atsign != NULL)
1159 1.7 christos return std::string (class_name, atsign - class_name);
1160 1.7 christos return class_name;
1161 1.1 christos }
1162 1.1 christos
1163 1.1 christos /* Implement the 'get_type_from_type_info' method. */
1164 1.1 christos
1165 1.1 christos static struct type *
1166 1.1 christos gnuv3_get_type_from_type_info (struct value *type_info_ptr)
1167 1.1 christos {
1168 1.1 christos /* We have to parse the type name, since in general there is not a
1169 1.1 christos symbol for a type. This is somewhat bogus since there may be a
1170 1.1 christos mis-parse. Another approach might be to re-use the demangler's
1171 1.1 christos internal form to reconstruct the type somehow. */
1172 1.7 christos std::string type_name = gnuv3_get_typename_from_type_info (type_info_ptr);
1173 1.7 christos expression_up expr (parse_expression (type_name.c_str ()));
1174 1.7 christos struct value *type_val = evaluate_type (expr.get ());
1175 1.7 christos return value_type (type_val);
1176 1.1 christos }
1177 1.1 christos
1178 1.1 christos /* Determine if we are currently in a C++ thunk. If so, get the address
1179 1.1 christos of the routine we are thunking to and continue to there instead. */
1180 1.1 christos
1181 1.1 christos static CORE_ADDR
1182 1.1 christos gnuv3_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc)
1183 1.1 christos {
1184 1.1 christos CORE_ADDR real_stop_pc, method_stop_pc, func_addr;
1185 1.1 christos struct gdbarch *gdbarch = get_frame_arch (frame);
1186 1.3 christos struct bound_minimal_symbol thunk_sym, fn_sym;
1187 1.1 christos struct obj_section *section;
1188 1.1 christos const char *thunk_name, *fn_name;
1189 1.1 christos
1190 1.1 christos real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc);
1191 1.1 christos if (real_stop_pc == 0)
1192 1.1 christos real_stop_pc = stop_pc;
1193 1.1 christos
1194 1.1 christos /* Find the linker symbol for this potential thunk. */
1195 1.3 christos thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc);
1196 1.1 christos section = find_pc_section (real_stop_pc);
1197 1.3 christos if (thunk_sym.minsym == NULL || section == NULL)
1198 1.1 christos return 0;
1199 1.1 christos
1200 1.1 christos /* The symbol's demangled name should be something like "virtual
1201 1.1 christos thunk to FUNCTION", where FUNCTION is the name of the function
1202 1.1 christos being thunked to. */
1203 1.3 christos thunk_name = MSYMBOL_DEMANGLED_NAME (thunk_sym.minsym);
1204 1.1 christos if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL)
1205 1.1 christos return 0;
1206 1.1 christos
1207 1.1 christos fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to ");
1208 1.1 christos fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile);
1209 1.3 christos if (fn_sym.minsym == NULL)
1210 1.1 christos return 0;
1211 1.1 christos
1212 1.3 christos method_stop_pc = BMSYMBOL_VALUE_ADDRESS (fn_sym);
1213 1.1 christos
1214 1.1 christos /* Some targets have minimal symbols pointing to function descriptors
1215 1.1 christos (powerpc 64 for example). Make sure to retrieve the address
1216 1.1 christos of the real function from the function descriptor before passing on
1217 1.1 christos the address to other layers of GDB. */
1218 1.1 christos func_addr = gdbarch_convert_from_func_ptr_addr (gdbarch, method_stop_pc,
1219 1.1 christos ¤t_target);
1220 1.1 christos if (func_addr != 0)
1221 1.1 christos method_stop_pc = func_addr;
1222 1.1 christos
1223 1.1 christos real_stop_pc = gdbarch_skip_trampoline_code
1224 1.1 christos (gdbarch, frame, method_stop_pc);
1225 1.1 christos if (real_stop_pc == 0)
1226 1.1 christos real_stop_pc = method_stop_pc;
1227 1.1 christos
1228 1.1 christos return real_stop_pc;
1229 1.1 christos }
1230 1.1 christos
1231 1.1 christos /* Return nonzero if a type should be passed by reference.
1232 1.1 christos
1233 1.1 christos The rule in the v3 ABI document comes from section 3.1.1. If the
1234 1.1 christos type has a non-trivial copy constructor or destructor, then the
1235 1.1 christos caller must make a copy (by calling the copy constructor if there
1236 1.1 christos is one or perform the copy itself otherwise), pass the address of
1237 1.1 christos the copy, and then destroy the temporary (if necessary).
1238 1.1 christos
1239 1.1 christos For return values with non-trivial copy constructors or
1240 1.1 christos destructors, space will be allocated in the caller, and a pointer
1241 1.1 christos will be passed as the first argument (preceding "this").
1242 1.1 christos
1243 1.1 christos We don't have a bulletproof mechanism for determining whether a
1244 1.1 christos constructor or destructor is trivial. For GCC and DWARF2 debug
1245 1.1 christos information, we can check the artificial flag.
1246 1.1 christos
1247 1.1 christos We don't do anything with the constructors or destructors,
1248 1.1 christos but we have to get the argument passing right anyway. */
1249 1.1 christos static int
1250 1.1 christos gnuv3_pass_by_reference (struct type *type)
1251 1.1 christos {
1252 1.1 christos int fieldnum, fieldelem;
1253 1.1 christos
1254 1.6 christos type = check_typedef (type);
1255 1.1 christos
1256 1.1 christos /* We're only interested in things that can have methods. */
1257 1.1 christos if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1258 1.1 christos && TYPE_CODE (type) != TYPE_CODE_UNION)
1259 1.1 christos return 0;
1260 1.1 christos
1261 1.3 christos /* A dynamic class has a non-trivial copy constructor.
1262 1.3 christos See c++98 section 12.8 Copying class objects [class.copy]. */
1263 1.3 christos if (gnuv3_dynamic_class (type))
1264 1.3 christos return 1;
1265 1.3 christos
1266 1.1 christos for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++)
1267 1.1 christos for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum);
1268 1.1 christos fieldelem++)
1269 1.1 christos {
1270 1.1 christos struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum);
1271 1.1 christos const char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum);
1272 1.1 christos struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem);
1273 1.1 christos
1274 1.1 christos /* If this function is marked as artificial, it is compiler-generated,
1275 1.1 christos and we assume it is trivial. */
1276 1.1 christos if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem))
1277 1.1 christos continue;
1278 1.1 christos
1279 1.1 christos /* If we've found a destructor, we must pass this by reference. */
1280 1.1 christos if (name[0] == '~')
1281 1.1 christos return 1;
1282 1.1 christos
1283 1.1 christos /* If the mangled name of this method doesn't indicate that it
1284 1.1 christos is a constructor, we're not interested.
1285 1.1 christos
1286 1.1 christos FIXME drow/2007-09-23: We could do this using the name of
1287 1.1 christos the method and the name of the class instead of dealing
1288 1.1 christos with the mangled name. We don't have a convenient function
1289 1.1 christos to strip off both leading scope qualifiers and trailing
1290 1.1 christos template arguments yet. */
1291 1.1 christos if (!is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem))
1292 1.1 christos && !TYPE_FN_FIELD_CONSTRUCTOR (fn, fieldelem))
1293 1.1 christos continue;
1294 1.1 christos
1295 1.1 christos /* If this method takes two arguments, and the second argument is
1296 1.1 christos a reference to this class, then it is a copy constructor. */
1297 1.3 christos if (TYPE_NFIELDS (fieldtype) == 2)
1298 1.3 christos {
1299 1.3 christos struct type *arg_type = TYPE_FIELD_TYPE (fieldtype, 1);
1300 1.3 christos
1301 1.3 christos if (TYPE_CODE (arg_type) == TYPE_CODE_REF)
1302 1.3 christos {
1303 1.3 christos struct type *arg_target_type;
1304 1.3 christos
1305 1.3 christos arg_target_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
1306 1.3 christos if (class_types_same_p (arg_target_type, type))
1307 1.3 christos return 1;
1308 1.3 christos }
1309 1.3 christos }
1310 1.1 christos }
1311 1.1 christos
1312 1.1 christos /* Even if all the constructors and destructors were artificial, one
1313 1.1 christos of them may have invoked a non-artificial constructor or
1314 1.1 christos destructor in a base class. If any base class needs to be passed
1315 1.1 christos by reference, so does this class. Similarly for members, which
1316 1.1 christos are constructed whenever this class is. We do not need to worry
1317 1.1 christos about recursive loops here, since we are only looking at members
1318 1.1 christos of complete class type. Also ignore any static members. */
1319 1.1 christos for (fieldnum = 0; fieldnum < TYPE_NFIELDS (type); fieldnum++)
1320 1.1 christos if (! field_is_static (&TYPE_FIELD (type, fieldnum))
1321 1.1 christos && gnuv3_pass_by_reference (TYPE_FIELD_TYPE (type, fieldnum)))
1322 1.1 christos return 1;
1323 1.1 christos
1324 1.1 christos return 0;
1325 1.1 christos }
1326 1.1 christos
1327 1.1 christos static void
1328 1.1 christos init_gnuv3_ops (void)
1329 1.1 christos {
1330 1.1 christos vtable_type_gdbarch_data
1331 1.1 christos = gdbarch_data_register_post_init (build_gdb_vtable_type);
1332 1.1 christos std_type_info_gdbarch_data
1333 1.1 christos = gdbarch_data_register_post_init (build_std_type_info_type);
1334 1.1 christos
1335 1.1 christos gnu_v3_abi_ops.shortname = "gnu-v3";
1336 1.1 christos gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI";
1337 1.1 christos gnu_v3_abi_ops.doc = "G++ Version 3 ABI";
1338 1.1 christos gnu_v3_abi_ops.is_destructor_name =
1339 1.1 christos (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor;
1340 1.1 christos gnu_v3_abi_ops.is_constructor_name =
1341 1.1 christos (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor;
1342 1.1 christos gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name;
1343 1.1 christos gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name;
1344 1.1 christos gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type;
1345 1.1 christos gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field;
1346 1.1 christos gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset;
1347 1.1 christos gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr;
1348 1.1 christos gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size;
1349 1.1 christos gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr;
1350 1.1 christos gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value;
1351 1.1 christos gnu_v3_abi_ops.print_vtable = gnuv3_print_vtable;
1352 1.1 christos gnu_v3_abi_ops.get_typeid = gnuv3_get_typeid;
1353 1.1 christos gnu_v3_abi_ops.get_typeid_type = gnuv3_get_typeid_type;
1354 1.1 christos gnu_v3_abi_ops.get_type_from_type_info = gnuv3_get_type_from_type_info;
1355 1.1 christos gnu_v3_abi_ops.get_typename_from_type_info
1356 1.1 christos = gnuv3_get_typename_from_type_info;
1357 1.1 christos gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline;
1358 1.1 christos gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference;
1359 1.1 christos }
1360 1.1 christos
1361 1.1 christos extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */
1362 1.1 christos
1363 1.1 christos void
1364 1.1 christos _initialize_gnu_v3_abi (void)
1365 1.1 christos {
1366 1.1 christos init_gnuv3_ops ();
1367 1.1 christos
1368 1.1 christos register_cp_abi (&gnu_v3_abi_ops);
1369 1.1 christos set_cp_abi_as_auto_default (gnu_v3_abi_ops.shortname);
1370 1.1 christos }
1371