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 | ||
48 | /* To help us find the components of a vtable, we build ourselves a | |
49 | GDB type object representing the vtable structure. Following the | |
50 | V3 ABI, it goes something like this: | |
51 | ||
52 | struct gdb_gnu_v3_abi_vtable { | |
53 | ||
54 | / * An array of virtual call and virtual base offsets. The real | |
55 | length of this array depends on the class hierarchy; we use | |
56 | negative subscripts to access the elements. Yucky, but | |
57 | better than the alternatives. * / | |
58 | ptrdiff_t vcall_and_vbase_offsets[0]; | |
59 | ||
60 | / * The offset from a virtual pointer referring to this table | |
61 | to the top of the complete object. * / | |
62 | ptrdiff_t offset_to_top; | |
63 | ||
64 | / * The type_info pointer for this class. This is really a | |
65 | std::type_info *, but GDB doesn't really look at the | |
66 | type_info object itself, so we don't bother to get the type | |
67 | exactly right. * / | |
68 | void *type_info; | |
69 | ||
70 | / * Virtual table pointers in objects point here. * / | |
71 | ||
72 | / * Virtual function pointers. Like the vcall/vbase array, the | |
73 | real length of this table depends on the class hierarchy. * / | |
74 | void (*virtual_functions[0]) (); | |
75 | ||
76 | }; | |
77 | ||
78 | The catch, of course, is that the exact layout of this table | |
79 | depends on the ABI --- word size, endianness, alignment, etc. So | |
80 | the GDB type object is actually a per-architecture kind of thing. | |
81 | ||
82 | vtable_type_gdbarch_data is a gdbarch per-architecture data pointer | |
83 | which refers to the struct type * for this structure, laid out | |
84 | appropriately for the architecture. */ | |
b27b8843 | 85 | static struct gdbarch_data *vtable_type_gdbarch_data; |
7ed49443 JB |
86 | |
87 | ||
88 | /* Human-readable names for the numbers of the fields above. */ | |
89 | enum { | |
90 | vtable_field_vcall_and_vbase_offsets, | |
91 | vtable_field_offset_to_top, | |
92 | vtable_field_type_info, | |
93 | vtable_field_virtual_functions | |
94 | }; | |
95 | ||
96 | ||
97 | /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable', | |
98 | described above, laid out appropriately for ARCH. | |
99 | ||
100 | We use this function as the gdbarch per-architecture data | |
9970f04b | 101 | initialization function. */ |
7ed49443 JB |
102 | static void * |
103 | build_gdb_vtable_type (struct gdbarch *arch) | |
104 | { | |
105 | struct type *t; | |
106 | struct field *field_list, *field; | |
107 | int offset; | |
108 | ||
109 | struct type *void_ptr_type | |
110 | = lookup_pointer_type (builtin_type_void); | |
111 | struct type *ptr_to_void_fn_type | |
112 | = lookup_pointer_type (lookup_function_type (builtin_type_void)); | |
113 | ||
114 | /* ARCH can't give us the true ptrdiff_t type, so we guess. */ | |
115 | struct type *ptrdiff_type | |
819844ad | 116 | = init_type (TYPE_CODE_INT, |
9970f04b | 117 | gdbarch_ptr_bit (arch) / TARGET_CHAR_BIT, 0, |
7ed49443 JB |
118 | "ptrdiff_t", 0); |
119 | ||
120 | /* We assume no padding is necessary, since GDB doesn't know | |
121 | anything about alignment at the moment. If this assumption bites | |
122 | us, we should add a gdbarch method which, given a type, returns | |
123 | the alignment that type requires, and then use that here. */ | |
124 | ||
125 | /* Build the field list. */ | |
126 | field_list = xmalloc (sizeof (struct field [4])); | |
127 | memset (field_list, 0, sizeof (struct field [4])); | |
128 | field = &field_list[0]; | |
129 | offset = 0; | |
130 | ||
131 | /* ptrdiff_t vcall_and_vbase_offsets[0]; */ | |
132 | FIELD_NAME (*field) = "vcall_and_vbase_offsets"; | |
133 | FIELD_TYPE (*field) | |
134 | = create_array_type (0, ptrdiff_type, | |
135 | create_range_type (0, builtin_type_int, 0, -1)); | |
136 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
137 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
138 | field++; | |
139 | ||
140 | /* ptrdiff_t offset_to_top; */ | |
141 | FIELD_NAME (*field) = "offset_to_top"; | |
142 | FIELD_TYPE (*field) = ptrdiff_type; | |
143 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
144 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
145 | field++; | |
146 | ||
147 | /* void *type_info; */ | |
148 | FIELD_NAME (*field) = "type_info"; | |
149 | FIELD_TYPE (*field) = void_ptr_type; | |
150 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
151 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
152 | field++; | |
153 | ||
154 | /* void (*virtual_functions[0]) (); */ | |
155 | FIELD_NAME (*field) = "virtual_functions"; | |
156 | FIELD_TYPE (*field) | |
157 | = create_array_type (0, ptr_to_void_fn_type, | |
158 | create_range_type (0, builtin_type_int, 0, -1)); | |
159 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
160 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
161 | field++; | |
162 | ||
163 | /* We assumed in the allocation above that there were four fields. */ | |
3d499020 | 164 | gdb_assert (field == (field_list + 4)); |
7ed49443 JB |
165 | |
166 | t = init_type (TYPE_CODE_STRUCT, offset, 0, 0, 0); | |
167 | TYPE_NFIELDS (t) = field - field_list; | |
168 | TYPE_FIELDS (t) = field_list; | |
169 | TYPE_TAG_NAME (t) = "gdb_gnu_v3_abi_vtable"; | |
170 | ||
171 | return t; | |
172 | } | |
173 | ||
174 | ||
175 | /* Return the offset from the start of the imaginary `struct | |
176 | gdb_gnu_v3_abi_vtable' object to the vtable's "address point" | |
177 | (i.e., where objects' virtual table pointers point). */ | |
178 | static int | |
5ae5f592 | 179 | vtable_address_point_offset (void) |
7ed49443 | 180 | { |
451fbdda AC |
181 | struct type *vtable_type = gdbarch_data (current_gdbarch, |
182 | vtable_type_gdbarch_data); | |
7ed49443 JB |
183 | |
184 | return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions) | |
185 | / TARGET_CHAR_BIT); | |
186 | } | |
187 | ||
188 | ||
189 | static struct type * | |
190 | gnuv3_rtti_type (struct value *value, | |
191 | int *full_p, int *top_p, int *using_enc_p) | |
192 | { | |
451fbdda AC |
193 | struct type *vtable_type = gdbarch_data (current_gdbarch, |
194 | vtable_type_gdbarch_data); | |
df407dfe | 195 | struct type *values_type = check_typedef (value_type (value)); |
7ed49443 JB |
196 | CORE_ADDR vtable_address; |
197 | struct value *vtable; | |
198 | struct minimal_symbol *vtable_symbol; | |
199 | const char *vtable_symbol_name; | |
200 | const char *class_name; | |
7ed49443 | 201 | struct type *run_time_type; |
21cfb3b6 | 202 | struct type *base_type; |
7ed49443 | 203 | LONGEST offset_to_top; |
81fe8080 DE |
204 | struct type *values_type_vptr_basetype; |
205 | int values_type_vptr_fieldno; | |
7ed49443 JB |
206 | |
207 | /* We only have RTTI for class objects. */ | |
df407dfe | 208 | if (TYPE_CODE (values_type) != TYPE_CODE_CLASS) |
7ed49443 JB |
209 | return NULL; |
210 | ||
df407dfe | 211 | /* If we can't find the virtual table pointer for values_type, we |
7ed49443 | 212 | can't find the RTTI. */ |
81fe8080 DE |
213 | values_type_vptr_fieldno = get_vptr_fieldno (values_type, |
214 | &values_type_vptr_basetype); | |
215 | if (values_type_vptr_fieldno == -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. */ |
81fe8080 | 223 | base_type = check_typedef (values_type_vptr_basetype); |
df407dfe | 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 |
81fe8080 | 231 | = value_as_address (value_field (value, values_type_vptr_fieldno)); |
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; | |
81fe8080 | 387 | int vbasetype_vptr_fieldno; |
1514d34e DJ |
388 | |
389 | /* If it isn't a virtual base, this is easy. The offset is in the | |
390 | type definition. */ | |
391 | if (!BASETYPE_VIA_VIRTUAL (type, index)) | |
392 | return TYPE_BASECLASS_BITPOS (type, index) / 8; | |
393 | ||
394 | /* To access a virtual base, we need to use the vbase offset stored in | |
395 | our vtable. Recent GCC versions provide this information. If it isn't | |
396 | available, we could get what we needed from RTTI, or from drawing the | |
397 | complete inheritance graph based on the debug info. Neither is | |
398 | worthwhile. */ | |
399 | cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8; | |
400 | if (cur_base_offset >= - vtable_address_point_offset ()) | |
8a3fe4f8 | 401 | error (_("Expected a negative vbase offset (old compiler?)")); |
1514d34e DJ |
402 | |
403 | cur_base_offset = cur_base_offset + vtable_address_point_offset (); | |
404 | if ((- cur_base_offset) % TYPE_LENGTH (builtin_type_void_data_ptr) != 0) | |
8a3fe4f8 | 405 | error (_("Misaligned vbase offset.")); |
1514d34e DJ |
406 | cur_base_offset = cur_base_offset |
407 | / ((int) TYPE_LENGTH (builtin_type_void_data_ptr)); | |
408 | ||
409 | /* We're now looking for the cur_base_offset'th entry (negative index) | |
79d5b63a DJ |
410 | in the vcall_and_vbase_offsets array. We used to cast the object to |
411 | its TYPE_VPTR_BASETYPE, and reference the vtable as TYPE_VPTR_FIELDNO; | |
412 | however, that cast can not be done without calling baseclass_offset again | |
413 | if the TYPE_VPTR_BASETYPE is a virtual base class, as described in the | |
414 | v3 C++ ABI Section 2.4.I.2.b. Fortunately the ABI guarantees that the | |
415 | vtable pointer will be located at the beginning of the object, so we can | |
416 | bypass the casting. Verify that the TYPE_VPTR_FIELDNO is in fact at the | |
7ed85d26 DJ |
417 | start of whichever baseclass it resides in, as a sanity measure - iff |
418 | we have debugging information for that baseclass. */ | |
79d5b63a DJ |
419 | |
420 | vbasetype = TYPE_VPTR_BASETYPE (type); | |
81fe8080 | 421 | vbasetype_vptr_fieldno = get_vptr_fieldno (vbasetype, NULL); |
7ed85d26 | 422 | |
81fe8080 DE |
423 | if (vbasetype_vptr_fieldno >= 0 |
424 | && TYPE_FIELD_BITPOS (vbasetype, vbasetype_vptr_fieldno) != 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 | |
52f729a7 | 681 | gnuv3_skip_trampoline (struct frame_info *frame, CORE_ADDR stop_pc) |
b18be20d DJ |
682 | { |
683 | CORE_ADDR real_stop_pc, method_stop_pc; | |
9970f04b | 684 | struct gdbarch *gdbarch = get_frame_arch (frame); |
b18be20d DJ |
685 | struct minimal_symbol *thunk_sym, *fn_sym; |
686 | struct obj_section *section; | |
687 | char *thunk_name, *fn_name; | |
688 | ||
9970f04b | 689 | real_stop_pc = gdbarch_skip_trampoline_code (gdbarch, frame, stop_pc); |
b18be20d DJ |
690 | if (real_stop_pc == 0) |
691 | real_stop_pc = stop_pc; | |
692 | ||
693 | /* Find the linker symbol for this potential thunk. */ | |
694 | thunk_sym = lookup_minimal_symbol_by_pc (real_stop_pc); | |
695 | section = find_pc_section (real_stop_pc); | |
696 | if (thunk_sym == NULL || section == NULL) | |
697 | return 0; | |
698 | ||
699 | /* The symbol's demangled name should be something like "virtual | |
700 | thunk to FUNCTION", where FUNCTION is the name of the function | |
701 | being thunked to. */ | |
702 | thunk_name = SYMBOL_DEMANGLED_NAME (thunk_sym); | |
703 | if (thunk_name == NULL || strstr (thunk_name, " thunk to ") == NULL) | |
704 | return 0; | |
705 | ||
706 | fn_name = strstr (thunk_name, " thunk to ") + strlen (" thunk to "); | |
707 | fn_sym = lookup_minimal_symbol (fn_name, NULL, section->objfile); | |
708 | if (fn_sym == NULL) | |
709 | return 0; | |
710 | ||
711 | method_stop_pc = SYMBOL_VALUE_ADDRESS (fn_sym); | |
e76f05fa | 712 | real_stop_pc = gdbarch_skip_trampoline_code |
9970f04b | 713 | (gdbarch, frame, method_stop_pc); |
b18be20d DJ |
714 | if (real_stop_pc == 0) |
715 | real_stop_pc = method_stop_pc; | |
716 | ||
717 | return real_stop_pc; | |
718 | } | |
719 | ||
41f1b697 DJ |
720 | /* Return nonzero if a type should be passed by reference. |
721 | ||
722 | The rule in the v3 ABI document comes from section 3.1.1. If the | |
723 | type has a non-trivial copy constructor or destructor, then the | |
724 | caller must make a copy (by calling the copy constructor if there | |
725 | is one or perform the copy itself otherwise), pass the address of | |
726 | the copy, and then destroy the temporary (if necessary). | |
727 | ||
728 | For return values with non-trivial copy constructors or | |
729 | destructors, space will be allocated in the caller, and a pointer | |
730 | will be passed as the first argument (preceding "this"). | |
731 | ||
732 | We don't have a bulletproof mechanism for determining whether a | |
733 | constructor or destructor is trivial. For GCC and DWARF2 debug | |
734 | information, we can check the artificial flag. | |
735 | ||
736 | We don't do anything with the constructors or destructors, | |
737 | but we have to get the argument passing right anyway. */ | |
738 | static int | |
739 | gnuv3_pass_by_reference (struct type *type) | |
740 | { | |
741 | int fieldnum, fieldelem; | |
742 | ||
743 | CHECK_TYPEDEF (type); | |
744 | ||
745 | /* We're only interested in things that can have methods. */ | |
746 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT | |
747 | && TYPE_CODE (type) != TYPE_CODE_CLASS | |
748 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
749 | return 0; | |
750 | ||
751 | for (fieldnum = 0; fieldnum < TYPE_NFN_FIELDS (type); fieldnum++) | |
752 | for (fieldelem = 0; fieldelem < TYPE_FN_FIELDLIST_LENGTH (type, fieldnum); | |
753 | fieldelem++) | |
754 | { | |
755 | struct fn_field *fn = TYPE_FN_FIELDLIST1 (type, fieldnum); | |
756 | char *name = TYPE_FN_FIELDLIST_NAME (type, fieldnum); | |
757 | struct type *fieldtype = TYPE_FN_FIELD_TYPE (fn, fieldelem); | |
758 | ||
759 | /* If this function is marked as artificial, it is compiler-generated, | |
760 | and we assume it is trivial. */ | |
761 | if (TYPE_FN_FIELD_ARTIFICIAL (fn, fieldelem)) | |
762 | continue; | |
763 | ||
764 | /* If we've found a destructor, we must pass this by reference. */ | |
765 | if (name[0] == '~') | |
766 | return 1; | |
767 | ||
768 | /* If the mangled name of this method doesn't indicate that it | |
769 | is a constructor, we're not interested. | |
770 | ||
771 | FIXME drow/2007-09-23: We could do this using the name of | |
772 | the method and the name of the class instead of dealing | |
773 | with the mangled name. We don't have a convenient function | |
774 | to strip off both leading scope qualifiers and trailing | |
775 | template arguments yet. */ | |
776 | if (!is_constructor_name (TYPE_FN_FIELD_PHYSNAME (fn, fieldelem))) | |
777 | continue; | |
778 | ||
779 | /* If this method takes two arguments, and the second argument is | |
780 | a reference to this class, then it is a copy constructor. */ | |
781 | if (TYPE_NFIELDS (fieldtype) == 2 | |
782 | && TYPE_CODE (TYPE_FIELD_TYPE (fieldtype, 1)) == TYPE_CODE_REF | |
783 | && check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (fieldtype, 1))) == type) | |
784 | return 1; | |
785 | } | |
786 | ||
787 | /* Even if all the constructors and destructors were artificial, one | |
788 | of them may have invoked a non-artificial constructor or | |
789 | destructor in a base class. If any base class needs to be passed | |
790 | by reference, so does this class. Similarly for members, which | |
791 | are constructed whenever this class is. We do not need to worry | |
792 | about recursive loops here, since we are only looking at members | |
793 | of complete class type. */ | |
794 | for (fieldnum = 0; fieldnum < TYPE_NFIELDS (type); fieldnum++) | |
795 | if (gnuv3_pass_by_reference (TYPE_FIELD_TYPE (type, fieldnum))) | |
796 | return 1; | |
797 | ||
798 | return 0; | |
799 | } | |
800 | ||
7ed49443 JB |
801 | static void |
802 | init_gnuv3_ops (void) | |
803 | { | |
030f20e1 | 804 | vtable_type_gdbarch_data = gdbarch_data_register_post_init (build_gdb_vtable_type); |
7ed49443 JB |
805 | |
806 | gnu_v3_abi_ops.shortname = "gnu-v3"; | |
807 | gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI"; | |
808 | gnu_v3_abi_ops.doc = "G++ Version 3 ABI"; | |
358777b0 EZ |
809 | gnu_v3_abi_ops.is_destructor_name = |
810 | (enum dtor_kinds (*) (const char *))is_gnu_v3_mangled_dtor; | |
811 | gnu_v3_abi_ops.is_constructor_name = | |
812 | (enum ctor_kinds (*) (const char *))is_gnu_v3_mangled_ctor; | |
7ed49443 JB |
813 | gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name; |
814 | gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name; | |
815 | gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type; | |
816 | gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field; | |
1514d34e | 817 | gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset; |
0d5de010 DJ |
818 | gnu_v3_abi_ops.print_method_ptr = gnuv3_print_method_ptr; |
819 | gnu_v3_abi_ops.method_ptr_size = gnuv3_method_ptr_size; | |
820 | gnu_v3_abi_ops.make_method_ptr = gnuv3_make_method_ptr; | |
821 | gnu_v3_abi_ops.method_ptr_to_value = gnuv3_method_ptr_to_value; | |
b18be20d | 822 | gnu_v3_abi_ops.skip_trampoline = gnuv3_skip_trampoline; |
41f1b697 | 823 | gnu_v3_abi_ops.pass_by_reference = gnuv3_pass_by_reference; |
7ed49443 JB |
824 | } |
825 | ||
b9362cc7 | 826 | extern initialize_file_ftype _initialize_gnu_v3_abi; /* -Wmissing-prototypes */ |
7ed49443 JB |
827 | |
828 | void | |
829 | _initialize_gnu_v3_abi (void) | |
830 | { | |
831 | init_gnuv3_ops (); | |
832 | ||
fe1f4a5e | 833 | register_cp_abi (&gnu_v3_abi_ops); |
7ed49443 | 834 | } |