]>
Commit | Line | Data |
---|---|---|
7ed49443 JB |
1 | /* Abstraction of GNU v3 abi. |
2 | Contributed by Jim Blandy <[email protected]> | |
451fbdda AC |
3 | |
4 | Copyright 2001, 2002 Free Software Foundation, Inc. | |
7ed49443 JB |
5 | |
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or | |
9 | modify it under the terms of the GNU General Public License as | |
10 | published by the Free Software Foundation; either version 2 of the | |
11 | License, or (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | #include "defs.h" | |
24 | #include "value.h" | |
25 | #include "cp-abi.h" | |
26 | #include "demangle.h" | |
3d499020 | 27 | #include "gdb_assert.h" |
5f8a3188 | 28 | #include "gdb_string.h" |
7ed49443 | 29 | |
b27b8843 | 30 | static struct cp_abi_ops gnu_v3_abi_ops; |
7ed49443 JB |
31 | |
32 | static int | |
33 | gnuv3_is_vtable_name (const char *name) | |
34 | { | |
35 | return strncmp (name, "_ZTV", 4) == 0; | |
36 | } | |
37 | ||
38 | static int | |
39 | gnuv3_is_operator_name (const char *name) | |
40 | { | |
41 | return strncmp (name, "operator", 8) == 0; | |
42 | } | |
43 | ||
44 | ||
45 | /* To help us find the components of a vtable, we build ourselves a | |
46 | GDB type object representing the vtable structure. Following the | |
47 | V3 ABI, it goes something like this: | |
48 | ||
49 | struct gdb_gnu_v3_abi_vtable { | |
50 | ||
51 | / * An array of virtual call and virtual base offsets. The real | |
52 | length of this array depends on the class hierarchy; we use | |
53 | negative subscripts to access the elements. Yucky, but | |
54 | better than the alternatives. * / | |
55 | ptrdiff_t vcall_and_vbase_offsets[0]; | |
56 | ||
57 | / * The offset from a virtual pointer referring to this table | |
58 | to the top of the complete object. * / | |
59 | ptrdiff_t offset_to_top; | |
60 | ||
61 | / * The type_info pointer for this class. This is really a | |
62 | std::type_info *, but GDB doesn't really look at the | |
63 | type_info object itself, so we don't bother to get the type | |
64 | exactly right. * / | |
65 | void *type_info; | |
66 | ||
67 | / * Virtual table pointers in objects point here. * / | |
68 | ||
69 | / * Virtual function pointers. Like the vcall/vbase array, the | |
70 | real length of this table depends on the class hierarchy. * / | |
71 | void (*virtual_functions[0]) (); | |
72 | ||
73 | }; | |
74 | ||
75 | The catch, of course, is that the exact layout of this table | |
76 | depends on the ABI --- word size, endianness, alignment, etc. So | |
77 | the GDB type object is actually a per-architecture kind of thing. | |
78 | ||
79 | vtable_type_gdbarch_data is a gdbarch per-architecture data pointer | |
80 | which refers to the struct type * for this structure, laid out | |
81 | appropriately for the architecture. */ | |
b27b8843 | 82 | static struct gdbarch_data *vtable_type_gdbarch_data; |
7ed49443 JB |
83 | |
84 | ||
85 | /* Human-readable names for the numbers of the fields above. */ | |
86 | enum { | |
87 | vtable_field_vcall_and_vbase_offsets, | |
88 | vtable_field_offset_to_top, | |
89 | vtable_field_type_info, | |
90 | vtable_field_virtual_functions | |
91 | }; | |
92 | ||
93 | ||
94 | /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable', | |
95 | described above, laid out appropriately for ARCH. | |
96 | ||
97 | We use this function as the gdbarch per-architecture data | |
98 | initialization function. We assume that the gdbarch framework | |
99 | calls the per-architecture data initialization functions after it | |
100 | sets current_gdbarch to the new architecture. */ | |
101 | static void * | |
102 | build_gdb_vtable_type (struct gdbarch *arch) | |
103 | { | |
104 | struct type *t; | |
105 | struct field *field_list, *field; | |
106 | int offset; | |
107 | ||
108 | struct type *void_ptr_type | |
109 | = lookup_pointer_type (builtin_type_void); | |
110 | struct type *ptr_to_void_fn_type | |
111 | = lookup_pointer_type (lookup_function_type (builtin_type_void)); | |
112 | ||
113 | /* ARCH can't give us the true ptrdiff_t type, so we guess. */ | |
114 | struct type *ptrdiff_type | |
115 | = init_type (TYPE_CODE_INT, TARGET_PTR_BIT / TARGET_CHAR_BIT, 0, | |
116 | "ptrdiff_t", 0); | |
117 | ||
118 | /* We assume no padding is necessary, since GDB doesn't know | |
119 | anything about alignment at the moment. If this assumption bites | |
120 | us, we should add a gdbarch method which, given a type, returns | |
121 | the alignment that type requires, and then use that here. */ | |
122 | ||
123 | /* Build the field list. */ | |
124 | field_list = xmalloc (sizeof (struct field [4])); | |
125 | memset (field_list, 0, sizeof (struct field [4])); | |
126 | field = &field_list[0]; | |
127 | offset = 0; | |
128 | ||
129 | /* ptrdiff_t vcall_and_vbase_offsets[0]; */ | |
130 | FIELD_NAME (*field) = "vcall_and_vbase_offsets"; | |
131 | FIELD_TYPE (*field) | |
132 | = create_array_type (0, ptrdiff_type, | |
133 | create_range_type (0, builtin_type_int, 0, -1)); | |
134 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
135 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
136 | field++; | |
137 | ||
138 | /* ptrdiff_t offset_to_top; */ | |
139 | FIELD_NAME (*field) = "offset_to_top"; | |
140 | FIELD_TYPE (*field) = ptrdiff_type; | |
141 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
142 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
143 | field++; | |
144 | ||
145 | /* void *type_info; */ | |
146 | FIELD_NAME (*field) = "type_info"; | |
147 | FIELD_TYPE (*field) = void_ptr_type; | |
148 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
149 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
150 | field++; | |
151 | ||
152 | /* void (*virtual_functions[0]) (); */ | |
153 | FIELD_NAME (*field) = "virtual_functions"; | |
154 | FIELD_TYPE (*field) | |
155 | = create_array_type (0, ptr_to_void_fn_type, | |
156 | create_range_type (0, builtin_type_int, 0, -1)); | |
157 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
158 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
159 | field++; | |
160 | ||
161 | /* We assumed in the allocation above that there were four fields. */ | |
3d499020 | 162 | gdb_assert (field == (field_list + 4)); |
7ed49443 JB |
163 | |
164 | t = init_type (TYPE_CODE_STRUCT, offset, 0, 0, 0); | |
165 | TYPE_NFIELDS (t) = field - field_list; | |
166 | TYPE_FIELDS (t) = field_list; | |
167 | TYPE_TAG_NAME (t) = "gdb_gnu_v3_abi_vtable"; | |
168 | ||
169 | return t; | |
170 | } | |
171 | ||
172 | ||
173 | /* Return the offset from the start of the imaginary `struct | |
174 | gdb_gnu_v3_abi_vtable' object to the vtable's "address point" | |
175 | (i.e., where objects' virtual table pointers point). */ | |
176 | static int | |
5ae5f592 | 177 | vtable_address_point_offset (void) |
7ed49443 | 178 | { |
451fbdda AC |
179 | struct type *vtable_type = gdbarch_data (current_gdbarch, |
180 | vtable_type_gdbarch_data); | |
7ed49443 JB |
181 | |
182 | return (TYPE_FIELD_BITPOS (vtable_type, vtable_field_virtual_functions) | |
183 | / TARGET_CHAR_BIT); | |
184 | } | |
185 | ||
186 | ||
187 | static struct type * | |
188 | gnuv3_rtti_type (struct value *value, | |
189 | int *full_p, int *top_p, int *using_enc_p) | |
190 | { | |
451fbdda AC |
191 | struct type *vtable_type = gdbarch_data (current_gdbarch, |
192 | vtable_type_gdbarch_data); | |
7ed49443 JB |
193 | struct type *value_type = check_typedef (VALUE_TYPE (value)); |
194 | CORE_ADDR vtable_address; | |
195 | struct value *vtable; | |
196 | struct minimal_symbol *vtable_symbol; | |
197 | const char *vtable_symbol_name; | |
198 | const char *class_name; | |
199 | struct symbol *class_symbol; | |
200 | struct type *run_time_type; | |
21cfb3b6 | 201 | struct type *base_type; |
7ed49443 JB |
202 | LONGEST offset_to_top; |
203 | ||
204 | /* We only have RTTI for class objects. */ | |
205 | if (TYPE_CODE (value_type) != TYPE_CODE_CLASS) | |
206 | return NULL; | |
207 | ||
208 | /* If we can't find the virtual table pointer for value_type, we | |
209 | can't find the RTTI. */ | |
210 | fill_in_vptr_fieldno (value_type); | |
211 | if (TYPE_VPTR_FIELDNO (value_type) == -1) | |
212 | return NULL; | |
213 | ||
21cfb3b6 DJ |
214 | if (using_enc_p) |
215 | *using_enc_p = 0; | |
216 | ||
7ed49443 | 217 | /* Fetch VALUE's virtual table pointer, and tweak it to point at |
21cfb3b6 DJ |
218 | an instance of our imaginary gdb_gnu_v3_abi_vtable structure. */ |
219 | base_type = check_typedef (TYPE_VPTR_BASETYPE (value_type)); | |
220 | if (value_type != base_type) | |
221 | { | |
222 | value = value_cast (base_type, value); | |
223 | if (using_enc_p) | |
224 | *using_enc_p = 1; | |
225 | } | |
7ed49443 | 226 | vtable_address |
1aa20aa8 | 227 | = value_as_address (value_field (value, TYPE_VPTR_FIELDNO (value_type))); |
7ed49443 JB |
228 | vtable = value_at_lazy (vtable_type, |
229 | vtable_address - vtable_address_point_offset (), | |
230 | VALUE_BFD_SECTION (value)); | |
231 | ||
232 | /* Find the linker symbol for this vtable. */ | |
233 | vtable_symbol | |
234 | = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtable) | |
235 | + VALUE_OFFSET (vtable) | |
236 | + VALUE_EMBEDDED_OFFSET (vtable)); | |
237 | if (! vtable_symbol) | |
238 | return NULL; | |
239 | ||
240 | /* The symbol's demangled name should be something like "vtable for | |
241 | CLASS", where CLASS is the name of the run-time type of VALUE. | |
242 | If we didn't like this approach, we could instead look in the | |
243 | type_info object itself to get the class name. But this way | |
244 | should work just as well, and doesn't read target memory. */ | |
245 | vtable_symbol_name = SYMBOL_DEMANGLED_NAME (vtable_symbol); | |
98081e55 PB |
246 | if (vtable_symbol_name == NULL |
247 | || strncmp (vtable_symbol_name, "vtable for ", 11)) | |
f773fdbb JM |
248 | { |
249 | warning ("can't find linker symbol for virtual table for `%s' value", | |
250 | TYPE_NAME (value_type)); | |
251 | if (vtable_symbol_name) | |
252 | warning (" found `%s' instead", vtable_symbol_name); | |
253 | return NULL; | |
254 | } | |
7ed49443 JB |
255 | class_name = vtable_symbol_name + 11; |
256 | ||
257 | /* Try to look up the class name as a type name. */ | |
258 | class_symbol = lookup_symbol (class_name, 0, STRUCT_NAMESPACE, 0, 0); | |
259 | if (! class_symbol) | |
f773fdbb JM |
260 | { |
261 | warning ("can't find class named `%s', as given by C++ RTTI", class_name); | |
262 | return NULL; | |
263 | } | |
7ed49443 JB |
264 | |
265 | /* Make sure the type symbol is sane. (An earlier version of this | |
266 | code would find constructor functions, who have the same name as | |
267 | the class.) */ | |
268 | if (SYMBOL_CLASS (class_symbol) != LOC_TYPEDEF | |
269 | || TYPE_CODE (SYMBOL_TYPE (class_symbol)) != TYPE_CODE_CLASS) | |
f773fdbb JM |
270 | { |
271 | warning ("C++ RTTI gives a class name of `%s', but that isn't a type name", | |
272 | class_name); | |
273 | return NULL; | |
274 | } | |
7ed49443 JB |
275 | |
276 | /* This is the object's run-time type! */ | |
277 | run_time_type = SYMBOL_TYPE (class_symbol); | |
278 | ||
279 | /* Get the offset from VALUE to the top of the complete object. | |
280 | NOTE: this is the reverse of the meaning of *TOP_P. */ | |
281 | offset_to_top | |
282 | = value_as_long (value_field (vtable, vtable_field_offset_to_top)); | |
283 | ||
284 | if (full_p) | |
285 | *full_p = (- offset_to_top == VALUE_EMBEDDED_OFFSET (value) | |
286 | && (TYPE_LENGTH (VALUE_ENCLOSING_TYPE (value)) | |
287 | >= TYPE_LENGTH (run_time_type))); | |
288 | if (top_p) | |
289 | *top_p = - offset_to_top; | |
7ed49443 JB |
290 | |
291 | return run_time_type; | |
292 | } | |
293 | ||
294 | ||
295 | static struct value * | |
296 | gnuv3_virtual_fn_field (struct value **value_p, | |
297 | struct fn_field *f, int j, | |
298 | struct type *type, int offset) | |
299 | { | |
451fbdda AC |
300 | struct type *vtable_type = gdbarch_data (current_gdbarch, |
301 | vtable_type_gdbarch_data); | |
7ed49443 JB |
302 | struct value *value = *value_p; |
303 | struct type *value_type = check_typedef (VALUE_TYPE (value)); | |
304 | struct type *vfn_base; | |
305 | CORE_ADDR vtable_address; | |
306 | struct value *vtable; | |
307 | struct value *vfn; | |
308 | ||
309 | /* Some simple sanity checks. */ | |
310 | if (TYPE_CODE (value_type) != TYPE_CODE_CLASS) | |
311 | error ("Only classes can have virtual functions."); | |
312 | ||
313 | /* Find the base class that defines this virtual function. */ | |
314 | vfn_base = TYPE_FN_FIELD_FCONTEXT (f, j); | |
315 | if (! vfn_base) | |
316 | /* In programs compiled with G++ version 1, the debug info doesn't | |
317 | say which base class defined the virtual function. We'll guess | |
318 | it's the same base class that has our vtable; this is wrong for | |
319 | multiple inheritance, but it's better than nothing. */ | |
320 | vfn_base = TYPE_VPTR_BASETYPE (type); | |
321 | ||
322 | /* This type may have been defined before its virtual function table | |
323 | was. If so, fill in the virtual function table entry for the | |
324 | type now. */ | |
325 | if (TYPE_VPTR_FIELDNO (vfn_base) < 0) | |
326 | fill_in_vptr_fieldno (vfn_base); | |
cef4f5dd DJ |
327 | if (TYPE_VPTR_FIELDNO (vfn_base) < 0) |
328 | error ("Could not find virtual table pointer for class \"%s\".", | |
329 | TYPE_TAG_NAME (vfn_base) ? TYPE_TAG_NAME (vfn_base) : "<unknown>"); | |
7ed49443 JB |
330 | |
331 | /* Now that we know which base class is defining our virtual | |
332 | function, cast our value to that baseclass. This takes care of | |
333 | any necessary `this' adjustments. */ | |
334 | if (vfn_base != value_type) | |
21cfb3b6 | 335 | value = value_cast (vfn_base, value); |
7ed49443 JB |
336 | |
337 | /* Now value is an object of the appropriate base type. Fetch its | |
338 | virtual table. */ | |
21cfb3b6 | 339 | /* It might be possible to do this cast at the same time as the above. |
76b79d6e DJ |
340 | Does multiple inheritance affect this? |
341 | Can this even trigger, or is TYPE_VPTR_BASETYPE idempotent? | |
342 | */ | |
21cfb3b6 DJ |
343 | if (TYPE_VPTR_BASETYPE (vfn_base) != vfn_base) |
344 | value = value_cast (TYPE_VPTR_BASETYPE (vfn_base), value); | |
7ed49443 | 345 | vtable_address |
1aa20aa8 | 346 | = value_as_address (value_field (value, TYPE_VPTR_FIELDNO (vfn_base))); |
21cfb3b6 | 347 | |
7ed49443 JB |
348 | vtable = value_at_lazy (vtable_type, |
349 | vtable_address - vtable_address_point_offset (), | |
350 | VALUE_BFD_SECTION (value)); | |
351 | ||
352 | /* Fetch the appropriate function pointer from the vtable. */ | |
353 | vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions), | |
354 | value_from_longest (builtin_type_int, | |
355 | TYPE_FN_FIELD_VOFFSET (f, j))); | |
356 | ||
357 | /* Cast the function pointer to the appropriate type. */ | |
358 | vfn = value_cast (lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j)), | |
359 | vfn); | |
360 | ||
76b79d6e DJ |
361 | /* Is (type)value always numerically the same as (vfn_base)value? |
362 | If so we can spare this cast and use one of the ones above. */ | |
363 | *value_p = value_addr (value_cast (type, *value_p)); | |
364 | ||
7ed49443 JB |
365 | return vfn; |
366 | } | |
367 | ||
1514d34e DJ |
368 | /* Compute the offset of the baseclass which is |
369 | the INDEXth baseclass of class TYPE, | |
370 | for value at VALADDR (in host) at ADDRESS (in target). | |
371 | The result is the offset of the baseclass value relative | |
372 | to (the address of)(ARG) + OFFSET. | |
373 | ||
374 | -1 is returned on error. */ | |
375 | int | |
376 | gnuv3_baseclass_offset (struct type *type, int index, char *valaddr, | |
377 | CORE_ADDR address) | |
378 | { | |
451fbdda AC |
379 | struct type *vtable_type = gdbarch_data (current_gdbarch, |
380 | vtable_type_gdbarch_data); | |
79d5b63a DJ |
381 | struct value *vtable; |
382 | struct type *vbasetype; | |
1514d34e DJ |
383 | struct value *offset_val, *vbase_array; |
384 | CORE_ADDR vtable_address; | |
385 | long int cur_base_offset, base_offset; | |
1514d34e DJ |
386 | |
387 | /* If it isn't a virtual base, this is easy. The offset is in the | |
388 | type definition. */ | |
389 | if (!BASETYPE_VIA_VIRTUAL (type, index)) | |
390 | return TYPE_BASECLASS_BITPOS (type, index) / 8; | |
391 | ||
392 | /* To access a virtual base, we need to use the vbase offset stored in | |
393 | our vtable. Recent GCC versions provide this information. If it isn't | |
394 | available, we could get what we needed from RTTI, or from drawing the | |
395 | complete inheritance graph based on the debug info. Neither is | |
396 | worthwhile. */ | |
397 | cur_base_offset = TYPE_BASECLASS_BITPOS (type, index) / 8; | |
398 | if (cur_base_offset >= - vtable_address_point_offset ()) | |
399 | error ("Expected a negative vbase offset (old compiler?)"); | |
400 | ||
401 | cur_base_offset = cur_base_offset + vtable_address_point_offset (); | |
402 | if ((- cur_base_offset) % TYPE_LENGTH (builtin_type_void_data_ptr) != 0) | |
403 | error ("Misaligned vbase offset."); | |
404 | cur_base_offset = cur_base_offset | |
405 | / ((int) TYPE_LENGTH (builtin_type_void_data_ptr)); | |
406 | ||
407 | /* We're now looking for the cur_base_offset'th entry (negative index) | |
79d5b63a DJ |
408 | in the vcall_and_vbase_offsets array. We used to cast the object to |
409 | its TYPE_VPTR_BASETYPE, and reference the vtable as TYPE_VPTR_FIELDNO; | |
410 | however, that cast can not be done without calling baseclass_offset again | |
411 | if the TYPE_VPTR_BASETYPE is a virtual base class, as described in the | |
412 | v3 C++ ABI Section 2.4.I.2.b. Fortunately the ABI guarantees that the | |
413 | vtable pointer will be located at the beginning of the object, so we can | |
414 | bypass the casting. Verify that the TYPE_VPTR_FIELDNO is in fact at the | |
415 | start of whichever baseclass it resides in, as a sanity measure. */ | |
416 | ||
417 | vbasetype = TYPE_VPTR_BASETYPE (type); | |
418 | if (TYPE_FIELD_BITPOS (vbasetype, TYPE_VPTR_FIELDNO (vbasetype)) != 0) | |
419 | error ("Illegal vptr offset in class %s", | |
420 | TYPE_NAME (vbasetype) ? TYPE_NAME (vbasetype) : "<unknown>"); | |
421 | ||
422 | vtable_address = value_as_address (value_at_lazy (builtin_type_void_data_ptr, | |
423 | address, NULL)); | |
1514d34e DJ |
424 | vtable = value_at_lazy (vtable_type, |
425 | vtable_address - vtable_address_point_offset (), | |
426 | NULL); | |
427 | offset_val = value_from_longest(builtin_type_int, cur_base_offset); | |
428 | vbase_array = value_field (vtable, vtable_field_vcall_and_vbase_offsets); | |
429 | base_offset = value_as_long (value_subscript (vbase_array, offset_val)); | |
430 | return base_offset; | |
431 | } | |
7ed49443 JB |
432 | |
433 | static void | |
434 | init_gnuv3_ops (void) | |
435 | { | |
436 | vtable_type_gdbarch_data = register_gdbarch_data (build_gdb_vtable_type, 0); | |
437 | ||
438 | gnu_v3_abi_ops.shortname = "gnu-v3"; | |
439 | gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI"; | |
440 | gnu_v3_abi_ops.doc = "G++ Version 3 ABI"; | |
441 | gnu_v3_abi_ops.is_destructor_name = is_gnu_v3_mangled_dtor; | |
442 | gnu_v3_abi_ops.is_constructor_name = is_gnu_v3_mangled_ctor; | |
443 | gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name; | |
444 | gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name; | |
445 | gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type; | |
446 | gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field; | |
1514d34e | 447 | gnu_v3_abi_ops.baseclass_offset = gnuv3_baseclass_offset; |
7ed49443 JB |
448 | } |
449 | ||
450 | ||
451 | void | |
452 | _initialize_gnu_v3_abi (void) | |
453 | { | |
454 | init_gnuv3_ops (); | |
455 | ||
fe1f4a5e | 456 | register_cp_abi (&gnu_v3_abi_ops); |
7ed49443 | 457 | } |