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