Commit | Line | Data |
---|---|---|
7ed49443 JB |
1 | /* Abstraction of GNU v3 abi. |
2 | Contributed by Jim Blandy <jimb@redhat.com> | |
451fbdda | 3 | |
9b254dd1 | 4 | Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007, 2008 |
0d5de010 | 5 | Free Software Foundation, Inc. |
7ed49443 JB |
6 | |
7 | This file is part of GDB. | |
8 | ||
a9762ec7 JB |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 3 of the License, or | |
12 | (at your option) any later version. | |
7ed49443 JB |
13 | |
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
7ed49443 JB |
21 | |
22 | #include "defs.h" | |
23 | #include "value.h" | |
24 | #include "cp-abi.h" | |
362ff856 | 25 | #include "cp-support.h" |
7ed49443 | 26 | #include "demangle.h" |
b18be20d | 27 | #include "objfiles.h" |
0d5de010 DJ |
28 | #include "valprint.h" |
29 | ||
3d499020 | 30 | #include "gdb_assert.h" |
5f8a3188 | 31 | #include "gdb_string.h" |
7ed49443 | 32 | |
b27b8843 | 33 | static struct cp_abi_ops gnu_v3_abi_ops; |
7ed49443 JB |
34 | |
35 | static int | |
36 | gnuv3_is_vtable_name (const char *name) | |
37 | { | |
38 | return strncmp (name, "_ZTV", 4) == 0; | |
39 | } | |
40 | ||
41 | static int | |
42 | gnuv3_is_operator_name (const char *name) | |
43 | { | |
44 | return strncmp (name, "operator", 8) == 0; | |
45 | } | |
46 | ||
47 | ||
ad4820ab UW |
48 | /* Determine architecture of class DOMAIN. This architecture is used |
49 | to query C++ ABI details (types, method pointer layout, etc.). | |
50 | ||
51 | Note that we assume DOMAIN must have been allocated with an OBJFILE; | |
52 | GDB does not provide any built-in class types. Thus we use the | |
53 | architecture of that OBJFILE to define the C++ ABI. */ | |
54 | ||
55 | static struct gdbarch * | |
56 | get_class_arch (struct type *domain) | |
57 | { | |
58 | gdb_assert (TYPE_CODE (domain) == TYPE_CODE_CLASS); | |
59 | gdb_assert (TYPE_OBJFILE (domain) != NULL); | |
60 | return get_objfile_arch (TYPE_OBJFILE (domain)); | |
61 | } | |
62 | ||
7ed49443 JB |
63 | /* To help us find the components of a vtable, we build ourselves a |
64 | GDB type object representing the vtable structure. Following the | |
65 | V3 ABI, it goes something like this: | |
66 | ||
67 | struct gdb_gnu_v3_abi_vtable { | |
68 | ||
69 | / * An array of virtual call and virtual base offsets. The real | |
70 | length of this array depends on the class hierarchy; we use | |
71 | negative subscripts to access the elements. Yucky, but | |
72 | better than the alternatives. * / | |
73 | ptrdiff_t vcall_and_vbase_offsets[0]; | |
74 | ||
75 | / * The offset from a virtual pointer referring to this table | |
76 | to the top of the complete object. * / | |
77 | ptrdiff_t offset_to_top; | |
78 | ||
79 | / * The type_info pointer for this class. This is really a | |
80 | std::type_info *, but GDB doesn't really look at the | |
81 | type_info object itself, so we don't bother to get the type | |
82 | exactly right. * / | |
83 | void *type_info; | |
84 | ||
85 | / * Virtual table pointers in objects point here. * / | |
86 | ||
87 | / * Virtual function pointers. Like the vcall/vbase array, the | |
88 | real length of this table depends on the class hierarchy. * / | |
89 | void (*virtual_functions[0]) (); | |
90 | ||
91 | }; | |
92 | ||
93 | The catch, of course, is that the exact layout of this table | |
94 | depends on the ABI --- word size, endianness, alignment, etc. So | |
95 | the GDB type object is actually a per-architecture kind of thing. | |
96 | ||
97 | vtable_type_gdbarch_data is a gdbarch per-architecture data pointer | |
98 | which refers to the struct type * for this structure, laid out | |
99 | appropriately for the architecture. */ | |
b27b8843 | 100 | static struct gdbarch_data *vtable_type_gdbarch_data; |
7ed49443 JB |
101 | |
102 | ||
103 | /* Human-readable names for the numbers of the fields above. */ | |
104 | enum { | |
105 | vtable_field_vcall_and_vbase_offsets, | |
106 | vtable_field_offset_to_top, | |
107 | vtable_field_type_info, | |
108 | vtable_field_virtual_functions | |
109 | }; | |
110 | ||
111 | ||
112 | /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable', | |
113 | described above, laid out appropriately for ARCH. | |
114 | ||
115 | We use this function as the gdbarch per-architecture data | |
9970f04b | 116 | initialization function. */ |
7ed49443 JB |
117 | static void * |
118 | build_gdb_vtable_type (struct gdbarch *arch) | |
119 | { | |
120 | struct type *t; | |
121 | struct field *field_list, *field; | |
122 | int offset; | |
123 | ||
124 | struct type *void_ptr_type | |
fde6c819 | 125 | = builtin_type (arch)->builtin_data_ptr; |
7ed49443 | 126 | struct type *ptr_to_void_fn_type |
fde6c819 | 127 | = builtin_type (arch)->builtin_func_ptr; |
7ed49443 JB |
128 | |
129 | /* ARCH can't give us the true ptrdiff_t type, so we guess. */ | |
130 | struct type *ptrdiff_type | |
819844ad | 131 | = init_type (TYPE_CODE_INT, |
9970f04b | 132 | gdbarch_ptr_bit (arch) / TARGET_CHAR_BIT, 0, |
7ed49443 JB |
133 | "ptrdiff_t", 0); |
134 | ||
135 | /* We assume no padding is necessary, since GDB doesn't know | |
136 | anything about alignment at the moment. If this assumption bites | |
137 | us, we should add a gdbarch method which, given a type, returns | |
138 | the alignment that type requires, and then use that here. */ | |
139 | ||
140 | /* Build the field list. */ | |
141 | field_list = xmalloc (sizeof (struct field [4])); | |
142 | memset (field_list, 0, sizeof (struct field [4])); | |
143 | field = &field_list[0]; | |
144 | offset = 0; | |
145 | ||
146 | /* ptrdiff_t vcall_and_vbase_offsets[0]; */ | |
147 | FIELD_NAME (*field) = "vcall_and_vbase_offsets"; | |
148 | FIELD_TYPE (*field) | |
149 | = create_array_type (0, ptrdiff_type, | |
6d84d3d8 | 150 | create_range_type (0, builtin_type_int32, 0, -1)); |
7ed49443 JB |
151 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; |
152 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
153 | field++; | |
154 | ||
155 | /* ptrdiff_t offset_to_top; */ | |
156 | FIELD_NAME (*field) = "offset_to_top"; | |
157 | FIELD_TYPE (*field) = ptrdiff_type; | |
158 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
159 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
160 | field++; | |
161 | ||
162 | /* void *type_info; */ | |
163 | FIELD_NAME (*field) = "type_info"; | |
164 | FIELD_TYPE (*field) = void_ptr_type; | |
165 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
166 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
167 | field++; | |
168 | ||
169 | /* void (*virtual_functions[0]) (); */ | |
170 | FIELD_NAME (*field) = "virtual_functions"; | |
171 | FIELD_TYPE (*field) | |
172 | = create_array_type (0, ptr_to_void_fn_type, | |
6d84d3d8 | 173 | create_range_type (0, builtin_type_int32, 0, -1)); |
7ed49443 JB |
174 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; |
175 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
176 | field++; | |
177 | ||
178 | /* We assumed in the allocation above that there were four fields. */ | |
3d499020 | 179 | gdb_assert (field == (field_list + 4)); |
7ed49443 JB |
180 | |
181 | t = init_type (TYPE_CODE_STRUCT, offset, 0, 0, 0); | |
182 | TYPE_NFIELDS (t) = field - field_list; | |
183 | TYPE_FIELDS (t) = field_list; | |
184 | TYPE_TAG_NAME (t) = "gdb_gnu_v3_abi_vtable"; | |
185 | ||
186 | return t; | |
187 | } | |
188 | ||
189 | ||
190 | /* Return the offset from the start of the imaginary `struct | |
191 | gdb_gnu_v3_abi_vtable' object to the vtable's "address point" | |
192 | (i.e., where objects' virtual table pointers point). */ | |
193 | static int | |
ad4820ab | 194 | vtable_address_point_offset (struct gdbarch *gdbarch) |
7ed49443 | 195 | { |
ad4820ab | 196 | struct type *vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data); |
7ed49443 JB |
197 | |
198 | return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions) | |
199 | / TARGET_CHAR_BIT); | |
200 | } | |
201 | ||
202 | ||
203 | static struct type * | |
204 | gnuv3_rtti_type (struct value *value, | |
205 | int *full_p, int *top_p, int *using_enc_p) | |
206 | { | |
ad4820ab UW |
207 | struct gdbarch *gdbarch; |
208 | struct type *vtable_type; | |
df407dfe | 209 | struct type *values_type = check_typedef (value_type (value)); |
7ed49443 JB |
210 | CORE_ADDR vtable_address; |
211 | struct value *vtable; | |
212 | struct minimal_symbol *vtable_symbol; | |
213 | const char *vtable_symbol_name; | |
214 | const char *class_name; | |
7ed49443 | 215 | struct type *run_time_type; |
21cfb3b6 | 216 | struct type *base_type; |
7ed49443 | 217 | LONGEST offset_to_top; |
81fe8080 DE |
218 | struct type *values_type_vptr_basetype; |
219 | int values_type_vptr_fieldno; | |
7ed49443 JB |
220 | |
221 | /* We only have RTTI for class objects. */ | |
df407dfe | 222 | if (TYPE_CODE (values_type) != TYPE_CODE_CLASS) |
7ed49443 JB |
223 | return NULL; |
224 | ||
ad4820ab UW |
225 | /* This routine may be called for Java types that do not have |
226 | a proper objfile. Just return NULL for those. */ | |
227 | if (!TYPE_OBJFILE (values_type) | |
228 | || !TYPE_OBJFILE (values_type)->obfd) | |
229 | return NULL; | |
230 | ||
231 | /* Determine architecture. */ | |
232 | gdbarch = get_class_arch (values_type); | |
233 | vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data); | |
234 | ||
df407dfe | 235 | /* If we can't find the virtual table pointer for values_type, we |
7ed49443 | 236 | can't find the RTTI. */ |
81fe8080 DE |
237 | values_type_vptr_fieldno = get_vptr_fieldno (values_type, |
238 | &values_type_vptr_basetype); | |
239 | if (values_type_vptr_fieldno == -1) | |
7ed49443 JB |
240 | return NULL; |
241 | ||
21cfb3b6 DJ |
242 | if (using_enc_p) |
243 | *using_enc_p = 0; | |
244 | ||
7ed49443 | 245 | /* Fetch VALUE's virtual table pointer, and tweak it to point at |
21cfb3b6 | 246 | an instance of our imaginary gdb_gnu_v3_abi_vtable structure. */ |
81fe8080 | 247 | base_type = check_typedef (values_type_vptr_basetype); |
df407dfe | 248 | if (values_type != base_type) |
21cfb3b6 DJ |
249 | { |
250 | value = value_cast (base_type, value); | |
251 | if (using_enc_p) | |
252 | *using_enc_p = 1; | |
253 | } | |
7ed49443 | 254 | vtable_address |
81fe8080 | 255 | = value_as_address (value_field (value, values_type_vptr_fieldno)); |
ad4820ab UW |
256 | vtable |
257 | = value_at_lazy (vtable_type, | |
258 | vtable_address - vtable_address_point_offset (gdbarch)); | |
7ed49443 JB |
259 | |
260 | /* Find the linker symbol for this vtable. */ | |
261 | vtable_symbol | |
262 | = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtable) | |
df407dfe | 263 | + value_offset (vtable) |
13c3b5f5 | 264 | + value_embedded_offset (vtable)); |
7ed49443 JB |
265 | if (! vtable_symbol) |
266 | return NULL; | |
267 | ||
268 | /* The symbol's demangled name should be something like "vtable for | |
269 | CLASS", where CLASS is the name of the run-time type of VALUE. | |
270 | If we didn't like this approach, we could instead look in the | |
271 | type_info object itself to get the class name. But this way | |
272 | should work just as well, and doesn't read target memory. */ | |
273 | vtable_symbol_name = SYMBOL_DEMANGLED_NAME (vtable_symbol); | |
98081e55 PB |
274 | if (vtable_symbol_name == NULL |
275 | || strncmp (vtable_symbol_name, "vtable for ", 11)) | |
f773fdbb | 276 | { |
8a3fe4f8 | 277 | warning (_("can't find linker symbol for virtual table for `%s' value"), |
df407dfe | 278 | TYPE_NAME (values_type)); |
f773fdbb | 279 | if (vtable_symbol_name) |
8a3fe4f8 | 280 | warning (_(" found `%s' instead"), vtable_symbol_name); |
f773fdbb JM |
281 | return NULL; |
282 | } | |
7ed49443 JB |
283 | class_name = vtable_symbol_name + 11; |
284 | ||
285 | /* Try to look up the class name as a type name. */ | |
362ff856 MC |
286 | /* FIXME: chastain/2003-11-26: block=NULL is bogus. See pr gdb/1465. */ |
287 | run_time_type = cp_lookup_rtti_type (class_name, NULL); | |
288 | if (run_time_type == NULL) | |
289 | return NULL; | |
7ed49443 JB |
290 | |
291 | /* Get the offset from VALUE to the top of the complete object. | |
292 | NOTE: this is the reverse of the meaning of *TOP_P. */ | |
293 | offset_to_top | |
294 | = value_as_long (value_field (vtable, vtable_field_offset_to_top)); | |
295 | ||
296 | if (full_p) | |
13c3b5f5 | 297 | *full_p = (- offset_to_top == value_embedded_offset (value) |
4754a64e | 298 | && (TYPE_LENGTH (value_enclosing_type (value)) |
7ed49443 JB |
299 | >= TYPE_LENGTH (run_time_type))); |
300 | if (top_p) | |
301 | *top_p = - offset_to_top; | |
7ed49443 JB |
302 | |
303 | return run_time_type; | |
304 | } | |
305 | ||
0d5de010 DJ |
306 | /* Find the vtable for CONTAINER and return a value of the correct |
307 | vtable type for this architecture. */ | |
7ed49443 JB |
308 | |
309 | static struct value * | |
ad4820ab | 310 | gnuv3_get_vtable (struct gdbarch *gdbarch, struct value *container) |
7ed49443 | 311 | { |
ad4820ab | 312 | struct type *vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data); |
0d5de010 DJ |
313 | struct type *vtable_pointer_type; |
314 | struct value *vtable_pointer; | |
315 | CORE_ADDR vtable_pointer_address, vtable_address; | |
316 | ||
317 | /* We do not consult the debug information to find the virtual table. | |
318 | The ABI specifies that it is always at offset zero in any class, | |
319 | and debug information may not represent it. We won't issue an | |
320 | error if there's a class with virtual functions but no virtual table | |
321 | pointer, but something's already gone seriously wrong if that | |
322 | happens. | |
323 | ||
324 | We avoid using value_contents on principle, because the object might | |
325 | be large. */ | |
326 | ||
327 | /* Find the type "pointer to virtual table". */ | |
328 | vtable_pointer_type = lookup_pointer_type (vtable_type); | |
329 | ||
330 | /* Load it from the start of the class. */ | |
331 | vtable_pointer_address = value_as_address (value_addr (container)); | |
332 | vtable_pointer = value_at (vtable_pointer_type, vtable_pointer_address); | |
333 | vtable_address = value_as_address (vtable_pointer); | |
334 | ||
335 | /* Correct it to point at the start of the virtual table, rather | |
336 | than the address point. */ | |
337 | return value_at_lazy (vtable_type, | |
ad4820ab | 338 | vtable_address - vtable_address_point_offset (gdbarch)); |
0d5de010 | 339 | } |
7ed49443 | 340 | |
0d5de010 DJ |
341 | /* Return a function pointer for CONTAINER's VTABLE_INDEX'th virtual |
342 | function, of type FNTYPE. */ | |
7ed49443 | 343 | |
0d5de010 | 344 | static struct value * |
ad4820ab UW |
345 | gnuv3_get_virtual_fn (struct gdbarch *gdbarch, struct value *container, |
346 | struct type *fntype, int vtable_index) | |
0d5de010 | 347 | { |
ad4820ab | 348 | struct value *vtable = gnuv3_get_vtable (gdbarch, container); |
0d5de010 | 349 | struct value *vfn; |
7ed49443 JB |
350 | |
351 | /* Fetch the appropriate function pointer from the vtable. */ | |
352 | vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions), | |
6d84d3d8 | 353 | value_from_longest (builtin_type_int32, vtable_index)); |
7ed49443 | 354 | |
0d5de010 DJ |
355 | /* If this architecture uses function descriptors directly in the vtable, |
356 | then the address of the vtable entry is actually a "function pointer" | |
357 | (i.e. points to the descriptor). We don't need to scale the index | |
358 | by the size of a function descriptor; GCC does that before outputing | |
359 | debug information. */ | |
ad4820ab | 360 | if (gdbarch_vtable_function_descriptors (gdbarch)) |
0d5de010 | 361 | vfn = value_addr (vfn); |
7ed49443 | 362 | |
0d5de010 DJ |
363 | /* Cast the function pointer to the appropriate type. */ |
364 | vfn = value_cast (lookup_pointer_type (fntype), vfn); | |
76b79d6e | 365 | |
7ed49443 JB |
366 | return vfn; |
367 | } | |
368 | ||
0d5de010 DJ |
369 | /* GNU v3 implementation of value_virtual_fn_field. See cp-abi.h |
370 | for a description of the arguments. */ | |
371 | ||
372 | static struct value * | |
373 | gnuv3_virtual_fn_field (struct value **value_p, | |
374 | struct fn_field *f, int j, | |
375 | struct type *vfn_base, int offset) | |
376 | { | |
377 | struct type *values_type = check_typedef (value_type (*value_p)); | |
ad4820ab | 378 | struct gdbarch *gdbarch; |
0d5de010 DJ |
379 | |
380 | /* Some simple sanity checks. */ | |
381 | if (TYPE_CODE (values_type) != TYPE_CODE_CLASS) | |
382 | error (_("Only classes can have virtual functions.")); | |
383 | ||
ad4820ab UW |
384 | /* Determine architecture. */ |
385 | gdbarch = get_class_arch (values_type); | |
386 | ||
0d5de010 DJ |
387 | /* Cast our value to the base class which defines this virtual |
388 | function. This takes care of any necessary `this' | |
389 | adjustments. */ | |
390 | if (vfn_base != values_type) | |
391 | *value_p = value_cast (vfn_base, *value_p); | |
392 | ||
ad4820ab | 393 | return gnuv3_get_virtual_fn (gdbarch, *value_p, TYPE_FN_FIELD_TYPE (f, j), |
0d5de010 DJ |
394 | TYPE_FN_FIELD_VOFFSET (f, j)); |
395 | } | |
396 | ||
1514d34e DJ |
397 | /* Compute the offset of the baseclass which is |
398 | the INDEXth baseclass of class TYPE, | |
399 | for value at VALADDR (in host) at ADDRESS (in target). | |
400 | The result is the offset of the baseclass value relative | |
401 | to (the address of)(ARG) + OFFSET. | |
402 | ||
403 | -1 is returned on error. */ | |
b9362cc7 | 404 | static int |
96ce45ca | 405 | gnuv3_baseclass_offset (struct type *type, int index, const bfd_byte *valaddr, |
1514d34e DJ |
406 | CORE_ADDR address) |
407 | { | |
ad4820ab UW |
408 | struct gdbarch *gdbarch; |
409 | struct type *vtable_type; | |
410 | struct type *ptr_type; | |
79d5b63a DJ |
411 | struct value *vtable; |
412 | struct type *vbasetype; | |
1514d34e DJ |
413 | struct value *offset_val, *vbase_array; |
414 | CORE_ADDR vtable_address; | |
415 | long int cur_base_offset, base_offset; | |
81fe8080 | 416 | int vbasetype_vptr_fieldno; |
1514d34e | 417 | |
ad4820ab UW |
418 | /* Determine architecture. */ |
419 | gdbarch = get_class_arch (type); | |
420 | vtable_type = gdbarch_data (gdbarch, vtable_type_gdbarch_data); | |
421 | ptr_type = builtin_type (gdbarch)->builtin_data_ptr; | |
422 | ||
1514d34e DJ |
423 | /* If it isn't a virtual base, this is easy. The offset is in the |
424 | type definition. */ | |
425 | if (!BASETYPE_VIA_VIRTUAL (type, index)) | |
426 | return TYPE_BASECLASS_BITPOS (type, index) / 8; | |
427 | ||
428 | /* To access a virtual base, we need to use the vbase offset stored in | |
429 | our vtable. Recent GCC versions provide this information. If it isn't | |
430 | available, we could get what we needed from RTTI, or from drawing the | |
431 | complete inheritance graph based on the debug info. Neither is | |
432 | worthwhile. */ | |
433 | cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8; | |
ad4820ab | 434 | if (cur_base_offset >= - vtable_address_point_offset (gdbarch)) |
8a3fe4f8 | 435 | error (_("Expected a negative vbase offset (old compiler?)")); |
1514d34e | 436 | |
ad4820ab UW |
437 | cur_base_offset = cur_base_offset + vtable_address_point_offset (gdbarch); |
438 | if ((- cur_base_offset) % TYPE_LENGTH (ptr_type) != 0) | |
8a3fe4f8 | 439 | error (_("Misaligned vbase offset.")); |
ad4820ab | 440 | cur_base_offset = cur_base_offset / ((int) TYPE_LENGTH (ptr_type)); |
1514d34e DJ |
441 | |
442 | /* We're now looking for the cur_base_offset'th entry (negative index) | |
79d5b63a DJ |
443 | in the vcall_and_vbase_offsets array. We used to cast the object to |
444 | its TYPE_VPTR_BASETYPE, and reference the vtable as TYPE_VPTR_FIELDNO; | |
445 | however, that cast can not be done without calling baseclass_offset again | |
446 | if the TYPE_VPTR_BASETYPE is a virtual base class, as described in the | |
447 | v3 C++ ABI Section 2.4.I.2.b. Fortunately the ABI guarantees that the | |
448 | vtable pointer will be located at the beginning of the object, so we can | |
449 | bypass the casting. Verify that the TYPE_VPTR_FIELDNO is in fact at the | |
7ed85d26 DJ |
450 | start of whichever baseclass it resides in, as a sanity measure - iff |
451 | we have debugging information for that baseclass. */ | |
79d5b63a DJ |
452 | |
453 | vbasetype = TYPE_VPTR_BASETYPE (type); | |
81fe8080 | 454 | vbasetype_vptr_fieldno = get_vptr_fieldno (vbasetype, NULL); |
7ed85d26 | 455 | |
81fe8080 DE |
456 | if (vbasetype_vptr_fieldno >= 0 |
457 | && TYPE_FIELD_BITPOS (vbasetype, vbasetype_vptr_fieldno) != 0) | |
8a3fe4f8 | 458 | error (_("Illegal vptr offset in class %s"), |
79d5b63a DJ |
459 | TYPE_NAME (vbasetype) ? TYPE_NAME (vbasetype) : "<unknown>"); |
460 | ||
ad4820ab UW |
461 | vtable_address = value_as_address (value_at_lazy (ptr_type, address)); |
462 | vtable | |
463 | = value_at_lazy (vtable_type, | |
464 | vtable_address - vtable_address_point_offset (gdbarch)); | |
465 | offset_val = value_from_longest (builtin_type_int32, cur_base_offset); | |
1514d34e DJ |
466 | vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets); |
467 | base_offset = value_as_long (value_subscript (vbase_array, offset_val)); | |
468 | return base_offset; | |
469 | } | |
7ed49443 | 470 | |
0d5de010 DJ |
471 | /* Locate a virtual method in DOMAIN or its non-virtual base classes |
472 | which has virtual table index VOFFSET. The method has an associated | |
473 | "this" adjustment of ADJUSTMENT bytes. */ | |
474 | ||
475 | const char * | |
476 | gnuv3_find_method_in (struct type *domain, CORE_ADDR voffset, | |
477 | LONGEST adjustment) | |
478 | { | |
479 | int i; | |
480 | const char *physname; | |
481 | ||
482 | /* Search this class first. */ | |
483 | physname = NULL; | |
484 | if (adjustment == 0) | |
485 | { | |
486 | int len; | |
487 | ||
488 | len = TYPE_NFN_FIELDS (domain); | |
489 | for (i = 0; i < len; i++) | |
490 | { | |
491 | int len2, j; | |
492 | struct fn_field *f; | |
493 | ||
494 | f = TYPE_FN_FIELDLIST1 (domain, i); | |
495 | len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i); | |
496 | ||
497 | check_stub_method_group (domain, i); | |
498 | for (j = 0; j < len2; j++) | |
499 | if (TYPE_FN_FIELD_VOFFSET (f, j) == voffset) | |
500 | return TYPE_FN_FIELD_PHYSNAME (f, j); | |
501 | } | |
502 | } | |
503 | ||
504 | /* Next search non-virtual bases. If it's in a virtual base, | |
505 | we're out of luck. */ | |
506 | for (i = 0; i < TYPE_N_BASECLASSES (domain); i++) | |
507 | { | |
508 | int pos; | |
509 | struct type *basetype; | |
510 | ||
511 | if (BASETYPE_VIA_VIRTUAL (domain, i)) | |
512 | continue; | |
513 | ||
514 | pos = TYPE_BASECLASS_BITPOS (domain, i) / 8; | |
515 | basetype = TYPE_FIELD_TYPE (domain, i); | |
516 | /* Recurse with a modified adjustment. We don't need to adjust | |
517 | voffset. */ | |
518 | if (adjustment >= pos && adjustment < pos + TYPE_LENGTH (basetype)) | |
519 | return gnuv3_find_method_in (basetype, voffset, adjustment - pos); | |
520 | } | |
521 | ||
522 | return NULL; | |
523 | } | |
524 | ||
fead6908 UW |
525 | /* Decode GNU v3 method pointer. */ |
526 | ||
527 | static int | |
ad4820ab UW |
528 | gnuv3_decode_method_ptr (struct gdbarch *gdbarch, |
529 | const gdb_byte *contents, | |
fead6908 UW |
530 | CORE_ADDR *value_p, |
531 | LONGEST *adjustment_p) | |
532 | { | |
ad4820ab UW |
533 | struct type *funcptr_type = builtin_type (gdbarch)->builtin_func_ptr; |
534 | struct type *offset_type = builtin_type (gdbarch)->builtin_long; | |
fead6908 UW |
535 | CORE_ADDR ptr_value; |
536 | LONGEST voffset, adjustment; | |
537 | int vbit; | |
538 | ||
539 | /* Extract the pointer to member. The first element is either a pointer | |
540 | or a vtable offset. For pointers, we need to use extract_typed_address | |
541 | to allow the back-end to convert the pointer to a GDB address -- but | |
542 | vtable offsets we must handle as integers. At this point, we do not | |
543 | yet know which case we have, so we extract the value under both | |
544 | interpretations and choose the right one later on. */ | |
545 | ptr_value = extract_typed_address (contents, funcptr_type); | |
546 | voffset = extract_signed_integer (contents, TYPE_LENGTH (funcptr_type)); | |
547 | contents += TYPE_LENGTH (funcptr_type); | |
548 | adjustment = extract_signed_integer (contents, TYPE_LENGTH (offset_type)); | |
549 | ||
ad4820ab | 550 | if (!gdbarch_vbit_in_delta (gdbarch)) |
fead6908 UW |
551 | { |
552 | vbit = voffset & 1; | |
553 | voffset = voffset ^ vbit; | |
554 | } | |
555 | else | |
556 | { | |
557 | vbit = adjustment & 1; | |
558 | adjustment = adjustment >> 1; | |
559 | } | |
560 | ||
561 | *value_p = vbit? voffset : ptr_value; | |
562 | *adjustment_p = adjustment; | |
563 | return vbit; | |
564 | } | |
565 | ||
0d5de010 DJ |
566 | /* GNU v3 implementation of cplus_print_method_ptr. */ |
567 | ||
568 | static void | |
569 | gnuv3_print_method_ptr (const gdb_byte *contents, | |
570 | struct type *type, | |
571 | struct ui_file *stream) | |
572 | { | |
ad4820ab UW |
573 | struct type *domain = TYPE_DOMAIN_TYPE (type); |
574 | struct gdbarch *gdbarch = get_class_arch (domain); | |
0d5de010 DJ |
575 | CORE_ADDR ptr_value; |
576 | LONGEST adjustment; | |
0d5de010 DJ |
577 | int vbit; |
578 | ||
0d5de010 | 579 | /* Extract the pointer to member. */ |
ad4820ab | 580 | vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment); |
0d5de010 DJ |
581 | |
582 | /* Check for NULL. */ | |
583 | if (ptr_value == 0 && vbit == 0) | |
584 | { | |
585 | fprintf_filtered (stream, "NULL"); | |
586 | return; | |
587 | } | |
588 | ||
589 | /* Search for a virtual method. */ | |
590 | if (vbit) | |
591 | { | |
592 | CORE_ADDR voffset; | |
593 | const char *physname; | |
594 | ||
595 | /* It's a virtual table offset, maybe in this class. Search | |
596 | for a field with the correct vtable offset. First convert it | |
597 | to an index, as used in TYPE_FN_FIELD_VOFFSET. */ | |
ad4820ab | 598 | voffset = ptr_value / TYPE_LENGTH (builtin_type (gdbarch)->builtin_long); |
0d5de010 DJ |
599 | |
600 | physname = gnuv3_find_method_in (domain, voffset, adjustment); | |
601 | ||
602 | /* If we found a method, print that. We don't bother to disambiguate | |
603 | possible paths to the method based on the adjustment. */ | |
604 | if (physname) | |
605 | { | |
606 | char *demangled_name = cplus_demangle (physname, | |
607 | DMGL_ANSI | DMGL_PARAMS); | |
608 | if (demangled_name != NULL) | |
609 | { | |
610 | fprintf_filtered (stream, "&virtual "); | |
611 | fputs_filtered (demangled_name, stream); | |
612 | xfree (demangled_name); | |
613 | return; | |
614 | } | |
615 | } | |
616 | } | |
617 | ||
618 | /* We didn't find it; print the raw data. */ | |
619 | if (vbit) | |
620 | { | |
621 | fprintf_filtered (stream, "&virtual table offset "); | |
622 | print_longest (stream, 'd', 1, ptr_value); | |
623 | } | |
624 | else | |
625 | print_address_demangle (ptr_value, stream, demangle); | |
626 | ||
627 | if (adjustment) | |
628 | { | |
629 | fprintf_filtered (stream, ", this adjustment "); | |
630 | print_longest (stream, 'd', 1, adjustment); | |
631 | } | |
632 | } | |
633 | ||
634 | /* GNU v3 implementation of cplus_method_ptr_size. */ | |
635 | ||
636 | static int | |
ad4820ab | 637 | gnuv3_method_ptr_size (struct type *type) |
0d5de010 | 638 | { |
ad4820ab UW |
639 | struct type *domain_type = check_typedef (TYPE_DOMAIN_TYPE (type)); |
640 | struct gdbarch *gdbarch = get_class_arch (domain_type); | |
641 | return 2 * TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr); | |
0d5de010 DJ |
642 | } |
643 | ||
644 | /* GNU v3 implementation of cplus_make_method_ptr. */ | |
645 | ||
646 | static void | |
ad4820ab UW |
647 | gnuv3_make_method_ptr (struct type *type, gdb_byte *contents, |
648 | CORE_ADDR value, int is_virtual) | |
0d5de010 | 649 | { |
ad4820ab UW |
650 | struct type *domain_type = check_typedef (TYPE_DOMAIN_TYPE (type)); |
651 | struct gdbarch *gdbarch = get_class_arch (domain_type); | |
652 | int size = TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr); | |
0d5de010 DJ |
653 | |
654 | /* FIXME drow/2006-12-24: The adjustment of "this" is currently | |
655 | always zero, since the method pointer is of the correct type. | |
656 | But if the method pointer came from a base class, this is | |
657 | incorrect - it should be the offset to the base. The best | |
658 | fix might be to create the pointer to member pointing at the | |
659 | base class and cast it to the derived class, but that requires | |
660 | support for adjusting pointers to members when casting them - | |
661 | not currently supported by GDB. */ | |
662 | ||
ad4820ab | 663 | if (!gdbarch_vbit_in_delta (gdbarch)) |
0d5de010 DJ |
664 | { |
665 | store_unsigned_integer (contents, size, value | is_virtual); | |
666 | store_unsigned_integer (contents + size, size, 0); | |
667 | } | |
668 | else | |
669 | { | |
670 | store_unsigned_integer (contents, size, value); | |
671 | store_unsigned_integer (contents + size, size, is_virtual); | |
672 | } | |
673 | } | |
674 | ||
675 | /* GNU v3 implementation of cplus_method_ptr_to_value. */ | |
676 | ||
677 | static struct value * | |
678 | gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr) | |
679 | { | |
ad4820ab | 680 | struct gdbarch *gdbarch; |
0d5de010 DJ |
681 | const gdb_byte *contents = value_contents (method_ptr); |
682 | CORE_ADDR ptr_value; | |
ad4820ab | 683 | struct type *domain_type, *final_type, *method_type; |
0d5de010 DJ |
684 | LONGEST adjustment; |
685 | struct value *adjval; | |
686 | int vbit; | |
687 | ||
ad4820ab UW |
688 | domain_type = TYPE_DOMAIN_TYPE (check_typedef (value_type (method_ptr))); |
689 | final_type = lookup_pointer_type (domain_type); | |
0d5de010 DJ |
690 | |
691 | method_type = TYPE_TARGET_TYPE (check_typedef (value_type (method_ptr))); | |
692 | ||
fead6908 | 693 | /* Extract the pointer to member. */ |
ad4820ab UW |
694 | gdbarch = get_class_arch (domain_type); |
695 | vbit = gnuv3_decode_method_ptr (gdbarch, contents, &ptr_value, &adjustment); | |
0d5de010 DJ |
696 | |
697 | /* First convert THIS to match the containing type of the pointer to | |
698 | member. This cast may adjust the value of THIS. */ | |
699 | *this_p = value_cast (final_type, *this_p); | |
700 | ||
701 | /* Then apply whatever adjustment is necessary. This creates a somewhat | |
702 | strange pointer: it claims to have type FINAL_TYPE, but in fact it | |
703 | might not be a valid FINAL_TYPE. For instance, it might be a | |
704 | base class of FINAL_TYPE. And if it's not the primary base class, | |
705 | then printing it out as a FINAL_TYPE object would produce some pretty | |
706 | garbage. | |
707 | ||
708 | But we don't really know the type of the first argument in | |
709 | METHOD_TYPE either, which is why this happens. We can't | |
710 | dereference this later as a FINAL_TYPE, but once we arrive in the | |
711 | called method we'll have debugging information for the type of | |
712 | "this" - and that'll match the value we produce here. | |
713 | ||
714 | You can provoke this case by casting a Base::* to a Derived::*, for | |
715 | instance. */ | |
ad4820ab UW |
716 | *this_p = value_cast (builtin_type (gdbarch)->builtin_data_ptr, *this_p); |
717 | adjval = value_from_longest (builtin_type (gdbarch)->builtin_long, | |
718 | adjustment); | |
89eef114 | 719 | *this_p = value_ptradd (*this_p, adjval); |
0d5de010 DJ |
720 | *this_p = value_cast (final_type, *this_p); |
721 | ||
722 | if (vbit) | |
723 | { | |
ad4820ab UW |
724 | LONGEST voffset; |
725 | voffset = ptr_value / TYPE_LENGTH (builtin_type (gdbarch)->builtin_long); | |
726 | return gnuv3_get_virtual_fn (gdbarch, value_ind (*this_p), | |
727 | method_type, voffset); | |
0d5de010 DJ |
728 | } |
729 | else | |
730 | return value_from_pointer (lookup_pointer_type (method_type), ptr_value); | |
731 | } | |
732 | ||
b18be20d DJ |
733 | /* Determine if we are currently in a C++ thunk. If so, get the address |
734 | of the routine we are thunking to and continue to there instead. */ | |
735 | ||
736 | static CORE_ADDR | |
52f729a7 | 737 | gnuv3_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc) |
b18be20d DJ |
738 | { |
739 | CORE_ADDR real_stop_pc, method_stop_pc; | |
9970f04b | 740 | struct gdbarch *gdbarch = get_frame_arch (frame); |
b18be20d DJ |
741 | struct minimal_symbol *thunk_sym, *fn_sym; |
742 | struct obj_section *section; | |
743 | char *thunk_name, *fn_name; | |
744 | ||
9970f04b | 745 | real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc); |
b18be20d DJ |
746 | if (real_stop_pc == 0) |
747 | real_stop_pc = stop_pc; | |
748 | ||
749 | /* Find the linker symbol for this potential thunk. */ | |
750 | thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc); | |
751 | section = find_pc_section (real_stop_pc); | |
752 | if (thunk_sym == NULL || section == NULL) | |
753 | return 0; | |
754 | ||
755 | /* The symbol's demangled name should be something like "virtual | |
756 | thunk to FUNCTION", where FUNCTION is the name of the function | |
757 | being thunked to. */ | |
758 | thunk_name = SYMBOL_DEMANGLED_NAME (thunk_sym); | |
759 | if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL) | |
760 | return 0; | |
761 | ||
762 | fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to "); | |
763 | fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile); | |
764 | if (fn_sym == NULL) | |
765 | return 0; | |
766 | ||
767 | method_stop_pc = SYMBOL_VALUE_ADDRESS (fn_sym); | |
e76f05fa | 768 | real_stop_pc = gdbarch_skip_trampoline_code |
9970f04b | 769 | (gdbarch, frame, method_stop_pc); |
b18be20d DJ |
770 | if (real_stop_pc == 0) |
771 | real_stop_pc = method_stop_pc; | |
772 | ||
773 | return real_stop_pc; | |
774 | } | |
775 | ||
41f1b697 DJ |
776 | /* Return nonzero if a type should be passed by reference. |
777 | ||
778 | The rule in the v3 ABI document comes from section 3.1.1. If the | |
779 | type has a non-trivial copy constructor or destructor, then the | |
780 | caller must make a copy (by calling the copy constructor if there | |
781 | is one or perform the copy itself otherwise), pass the address of | |
782 | the copy, and then destroy the temporary (if necessary). | |
783 | ||
784 | For return values with non-trivial copy constructors or | |
785 | destructors, space will be allocated in the caller, and a pointer | |
786 | will be passed as the first argument (preceding "this"). | |
787 | ||
788 | We don't have a bulletproof mechanism for determining whether a | |
789 | constructor or destructor is trivial. For GCC and DWARF2 debug | |
790 | information, we can check the artificial flag. | |
791 | ||
792 | We don't do anything with the constructors or destructors, | |
793 | but we have to get the argument passing right anyway. */ | |
794 | static int | |
795 | gnuv3_pass_by_reference (struct type *type) | |
796 | { | |
797 | int fieldnum, fieldelem; | |
798 | ||
799 | CHECK_TYPEDEF (type); | |
800 | ||
801 | /* We're only interested in things that can have methods. */ | |
802 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT | |
803 | && TYPE_CODE (type) != TYPE_CODE_CLASS | |
804 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
805 | return 0; | |
806 | ||
807 | for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++) | |
808 | for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum); | |
809 | fieldelem++) | |
810 | { | |
811 | struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum); | |
812 | char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum); | |
813 | struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem); | |
814 | ||
815 | /* If this function is marked as artificial, it is compiler-generated, | |
816 | and we assume it is trivial. */ | |
817 | if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem)) | |
818 | continue; | |
819 | ||
820 | /* If we've found a destructor, we must pass this by reference. */ | |
821 | if (name[0] == '~') | |
822 | return 1; | |
823 | ||
824 | /* If the mangled name of this method doesn't indicate that it | |
825 | is a constructor, we're not interested. | |
826 | ||
827 | FIXME drow/2007-09-23: We could do this using the name of | |
828 | the method and the name of the class instead of dealing | |
829 | with the mangled name. We don't have a convenient function | |
830 | to strip off both leading scope qualifiers and trailing | |
831 | template arguments yet. */ | |
832 | if (!is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem))) | |
833 | continue; | |
834 | ||
835 | /* If this method takes two arguments, and the second argument is | |
836 | a reference to this class, then it is a copy constructor. */ | |
837 | if (TYPE_NFIELDS (fieldtype) == 2 | |
838 | && TYPE_CODE (TYPE_FIELD_TYPE (fieldtype, 1)) == TYPE_CODE_REF | |
839 | && check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (fieldtype, 1))) == type) | |
840 | return 1; | |
841 | } | |
842 | ||
843 | /* Even if all the constructors and destructors were artificial, one | |
844 | of them may have invoked a non-artificial constructor or | |
845 | destructor in a base class. If any base class needs to be passed | |
846 | by reference, so does this class. Similarly for members, which | |
847 | are constructed whenever this class is. We do not need to worry | |
848 | about recursive loops here, since we are only looking at members | |
849 | of complete class type. */ | |
850 | for (fieldnum = 0; fieldnum < TYPE_NFIELDS (type); fieldnum++) | |
851 | if (gnuv3_pass_by_reference (TYPE_FIELD_TYPE (type, fieldnum))) | |
852 | return 1; | |
853 | ||
854 | return 0; | |
855 | } | |
856 | ||
7ed49443 JB |
857 | static void |
858 | init_gnuv3_ops (void) | |
859 | { | |
030f20e1 | 860 | vtable_type_gdbarch_data = gdbarch_data_register_post_init (build_gdb_vtable_type); |
7ed49443 JB |
861 | |
862 | gnu_v3_abi_ops.shortname = "gnu-v3"; | |
863 | gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI"; | |
864 | gnu_v3_abi_ops.doc = "G++ Version 3 ABI"; | |
358777b0 EZ |
865 | gnu_v3_abi_ops.is_destructor_name = |
866 | (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor; | |
867 | gnu_v3_abi_ops.is_constructor_name = | |
868 | (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor; | |
7ed49443 JB |
869 | gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name; |
870 | gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name; | |
871 | gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type; | |
872 | gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field; | |
1514d34e | 873 | gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset; |
0d5de010 DJ |
874 | gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr; |
875 | gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size; | |
876 | gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr; | |
877 | gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value; | |
b18be20d | 878 | gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline; |
41f1b697 | 879 | gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference; |
7ed49443 JB |
880 | } |
881 | ||
b9362cc7 | 882 | extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */ |
7ed49443 JB |
883 | |
884 | void | |
885 | _initialize_gnu_v3_abi (void) | |
886 | { | |
887 | init_gnuv3_ops (); | |
888 | ||
fe1f4a5e | 889 | register_cp_abi (&gnu_v3_abi_ops); |
7ed49443 | 890 | } |