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