]>
Commit | Line | Data |
---|---|---|
7ed49443 JB |
1 | /* Abstraction of GNU v3 abi. |
2 | Contributed by Jim Blandy <[email protected]> | |
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" | |
26 | ||
27 | struct cp_abi_ops gnu_v3_abi_ops; | |
28 | ||
29 | static int | |
30 | gnuv3_is_vtable_name (const char *name) | |
31 | { | |
32 | return strncmp (name, "_ZTV", 4) == 0; | |
33 | } | |
34 | ||
35 | static int | |
36 | gnuv3_is_operator_name (const char *name) | |
37 | { | |
38 | return strncmp (name, "operator", 8) == 0; | |
39 | } | |
40 | ||
41 | ||
42 | /* To help us find the components of a vtable, we build ourselves a | |
43 | GDB type object representing the vtable structure. Following the | |
44 | V3 ABI, it goes something like this: | |
45 | ||
46 | struct gdb_gnu_v3_abi_vtable { | |
47 | ||
48 | / * An array of virtual call and virtual base offsets. The real | |
49 | length of this array depends on the class hierarchy; we use | |
50 | negative subscripts to access the elements. Yucky, but | |
51 | better than the alternatives. * / | |
52 | ptrdiff_t vcall_and_vbase_offsets[0]; | |
53 | ||
54 | / * The offset from a virtual pointer referring to this table | |
55 | to the top of the complete object. * / | |
56 | ptrdiff_t offset_to_top; | |
57 | ||
58 | / * The type_info pointer for this class. This is really a | |
59 | std::type_info *, but GDB doesn't really look at the | |
60 | type_info object itself, so we don't bother to get the type | |
61 | exactly right. * / | |
62 | void *type_info; | |
63 | ||
64 | / * Virtual table pointers in objects point here. * / | |
65 | ||
66 | / * Virtual function pointers. Like the vcall/vbase array, the | |
67 | real length of this table depends on the class hierarchy. * / | |
68 | void (*virtual_functions[0]) (); | |
69 | ||
70 | }; | |
71 | ||
72 | The catch, of course, is that the exact layout of this table | |
73 | depends on the ABI --- word size, endianness, alignment, etc. So | |
74 | the GDB type object is actually a per-architecture kind of thing. | |
75 | ||
76 | vtable_type_gdbarch_data is a gdbarch per-architecture data pointer | |
77 | which refers to the struct type * for this structure, laid out | |
78 | appropriately for the architecture. */ | |
79 | struct gdbarch_data *vtable_type_gdbarch_data; | |
80 | ||
81 | ||
82 | /* Human-readable names for the numbers of the fields above. */ | |
83 | enum { | |
84 | vtable_field_vcall_and_vbase_offsets, | |
85 | vtable_field_offset_to_top, | |
86 | vtable_field_type_info, | |
87 | vtable_field_virtual_functions | |
88 | }; | |
89 | ||
90 | ||
91 | /* Return a GDB type representing `struct gdb_gnu_v3_abi_vtable', | |
92 | described above, laid out appropriately for ARCH. | |
93 | ||
94 | We use this function as the gdbarch per-architecture data | |
95 | initialization function. We assume that the gdbarch framework | |
96 | calls the per-architecture data initialization functions after it | |
97 | sets current_gdbarch to the new architecture. */ | |
98 | static void * | |
99 | build_gdb_vtable_type (struct gdbarch *arch) | |
100 | { | |
101 | struct type *t; | |
102 | struct field *field_list, *field; | |
103 | int offset; | |
104 | ||
105 | struct type *void_ptr_type | |
106 | = lookup_pointer_type (builtin_type_void); | |
107 | struct type *ptr_to_void_fn_type | |
108 | = lookup_pointer_type (lookup_function_type (builtin_type_void)); | |
109 | ||
110 | /* ARCH can't give us the true ptrdiff_t type, so we guess. */ | |
111 | struct type *ptrdiff_type | |
112 | = init_type (TYPE_CODE_INT, TARGET_PTR_BIT / TARGET_CHAR_BIT, 0, | |
113 | "ptrdiff_t", 0); | |
114 | ||
115 | /* We assume no padding is necessary, since GDB doesn't know | |
116 | anything about alignment at the moment. If this assumption bites | |
117 | us, we should add a gdbarch method which, given a type, returns | |
118 | the alignment that type requires, and then use that here. */ | |
119 | ||
120 | /* Build the field list. */ | |
121 | field_list = xmalloc (sizeof (struct field [4])); | |
122 | memset (field_list, 0, sizeof (struct field [4])); | |
123 | field = &field_list[0]; | |
124 | offset = 0; | |
125 | ||
126 | /* ptrdiff_t vcall_and_vbase_offsets[0]; */ | |
127 | FIELD_NAME (*field) = "vcall_and_vbase_offsets"; | |
128 | FIELD_TYPE (*field) | |
129 | = create_array_type (0, ptrdiff_type, | |
130 | create_range_type (0, builtin_type_int, 0, -1)); | |
131 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
132 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
133 | field++; | |
134 | ||
135 | /* ptrdiff_t offset_to_top; */ | |
136 | FIELD_NAME (*field) = "offset_to_top"; | |
137 | FIELD_TYPE (*field) = ptrdiff_type; | |
138 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
139 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
140 | field++; | |
141 | ||
142 | /* void *type_info; */ | |
143 | FIELD_NAME (*field) = "type_info"; | |
144 | FIELD_TYPE (*field) = void_ptr_type; | |
145 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
146 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
147 | field++; | |
148 | ||
149 | /* void (*virtual_functions[0]) (); */ | |
150 | FIELD_NAME (*field) = "virtual_functions"; | |
151 | FIELD_TYPE (*field) | |
152 | = create_array_type (0, ptr_to_void_fn_type, | |
153 | create_range_type (0, builtin_type_int, 0, -1)); | |
154 | FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT; | |
155 | offset += TYPE_LENGTH (FIELD_TYPE (*field)); | |
156 | field++; | |
157 | ||
158 | /* We assumed in the allocation above that there were four fields. */ | |
159 | if (field != field_list + 4) | |
160 | abort (); | |
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; | |
197 | LONGEST offset_to_top; | |
198 | ||
199 | /* We only have RTTI for class objects. */ | |
200 | if (TYPE_CODE (value_type) != TYPE_CODE_CLASS) | |
201 | return NULL; | |
202 | ||
203 | /* If we can't find the virtual table pointer for value_type, we | |
204 | can't find the RTTI. */ | |
205 | fill_in_vptr_fieldno (value_type); | |
206 | if (TYPE_VPTR_FIELDNO (value_type) == -1) | |
207 | return NULL; | |
208 | ||
209 | /* Fetch VALUE's virtual table pointer, and tweak it to point at | |
210 | an instance of our imaginary gdb_gnu_v3_abi_vtable structure. */ | |
211 | vtable_address | |
212 | = value_as_pointer (value_field (value, TYPE_VPTR_FIELDNO (value_type))); | |
213 | vtable = value_at_lazy (vtable_type, | |
214 | vtable_address - vtable_address_point_offset (), | |
215 | VALUE_BFD_SECTION (value)); | |
216 | ||
217 | /* Find the linker symbol for this vtable. */ | |
218 | vtable_symbol | |
219 | = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtable) | |
220 | + VALUE_OFFSET (vtable) | |
221 | + VALUE_EMBEDDED_OFFSET (vtable)); | |
222 | if (! vtable_symbol) | |
223 | return NULL; | |
224 | ||
225 | /* The symbol's demangled name should be something like "vtable for | |
226 | CLASS", where CLASS is the name of the run-time type of VALUE. | |
227 | If we didn't like this approach, we could instead look in the | |
228 | type_info object itself to get the class name. But this way | |
229 | should work just as well, and doesn't read target memory. */ | |
230 | vtable_symbol_name = SYMBOL_DEMANGLED_NAME (vtable_symbol); | |
231 | if (strncmp (vtable_symbol_name, "vtable for ", 11)) | |
232 | error ("can't find linker symbol for virtual table for `%s' value", | |
233 | TYPE_NAME (value_type)); | |
234 | class_name = vtable_symbol_name + 11; | |
235 | ||
236 | /* Try to look up the class name as a type name. */ | |
237 | class_symbol = lookup_symbol (class_name, 0, STRUCT_NAMESPACE, 0, 0); | |
238 | if (! class_symbol) | |
239 | error ("can't find class named `%s', as given by C++ RTTI", class_name); | |
240 | ||
241 | /* Make sure the type symbol is sane. (An earlier version of this | |
242 | code would find constructor functions, who have the same name as | |
243 | the class.) */ | |
244 | if (SYMBOL_CLASS (class_symbol) != LOC_TYPEDEF | |
245 | || TYPE_CODE (SYMBOL_TYPE (class_symbol)) != TYPE_CODE_CLASS) | |
246 | error ("C++ RTTI gives a class name of `%s', but that isn't a type name", | |
247 | class_name); | |
248 | ||
249 | /* This is the object's run-time type! */ | |
250 | run_time_type = SYMBOL_TYPE (class_symbol); | |
251 | ||
252 | /* Get the offset from VALUE to the top of the complete object. | |
253 | NOTE: this is the reverse of the meaning of *TOP_P. */ | |
254 | offset_to_top | |
255 | = value_as_long (value_field (vtable, vtable_field_offset_to_top)); | |
256 | ||
257 | if (full_p) | |
258 | *full_p = (- offset_to_top == VALUE_EMBEDDED_OFFSET (value) | |
259 | && (TYPE_LENGTH (VALUE_ENCLOSING_TYPE (value)) | |
260 | >= TYPE_LENGTH (run_time_type))); | |
261 | if (top_p) | |
262 | *top_p = - offset_to_top; | |
263 | if (using_enc_p) | |
264 | *using_enc_p = 0; | |
265 | ||
266 | return run_time_type; | |
267 | } | |
268 | ||
269 | ||
270 | static struct value * | |
271 | gnuv3_virtual_fn_field (struct value **value_p, | |
272 | struct fn_field *f, int j, | |
273 | struct type *type, int offset) | |
274 | { | |
275 | struct type *vtable_type = gdbarch_data (vtable_type_gdbarch_data); | |
276 | struct value *value = *value_p; | |
277 | struct type *value_type = check_typedef (VALUE_TYPE (value)); | |
278 | struct type *vfn_base; | |
279 | CORE_ADDR vtable_address; | |
280 | struct value *vtable; | |
281 | struct value *vfn; | |
282 | ||
283 | /* Some simple sanity checks. */ | |
284 | if (TYPE_CODE (value_type) != TYPE_CODE_CLASS) | |
285 | error ("Only classes can have virtual functions."); | |
286 | ||
287 | /* Find the base class that defines this virtual function. */ | |
288 | vfn_base = TYPE_FN_FIELD_FCONTEXT (f, j); | |
289 | if (! vfn_base) | |
290 | /* In programs compiled with G++ version 1, the debug info doesn't | |
291 | say which base class defined the virtual function. We'll guess | |
292 | it's the same base class that has our vtable; this is wrong for | |
293 | multiple inheritance, but it's better than nothing. */ | |
294 | vfn_base = TYPE_VPTR_BASETYPE (type); | |
295 | ||
296 | /* This type may have been defined before its virtual function table | |
297 | was. If so, fill in the virtual function table entry for the | |
298 | type now. */ | |
299 | if (TYPE_VPTR_FIELDNO (vfn_base) < 0) | |
300 | fill_in_vptr_fieldno (vfn_base); | |
301 | ||
302 | /* Now that we know which base class is defining our virtual | |
303 | function, cast our value to that baseclass. This takes care of | |
304 | any necessary `this' adjustments. */ | |
305 | if (vfn_base != value_type) | |
306 | /* It would be nicer to simply cast the value to the appropriate | |
307 | base class (and I think that is supposed to be legal), but | |
308 | value_cast only does the right magic when casting pointers. */ | |
309 | value = value_ind (value_cast (vfn_base, value_addr (value))); | |
310 | ||
311 | /* Now value is an object of the appropriate base type. Fetch its | |
312 | virtual table. */ | |
313 | vtable_address | |
314 | = value_as_pointer (value_field (value, TYPE_VPTR_FIELDNO (vfn_base))); | |
315 | vtable = value_at_lazy (vtable_type, | |
316 | vtable_address - vtable_address_point_offset (), | |
317 | VALUE_BFD_SECTION (value)); | |
318 | ||
319 | /* Fetch the appropriate function pointer from the vtable. */ | |
320 | vfn = value_subscript (value_field (vtable, vtable_field_virtual_functions), | |
321 | value_from_longest (builtin_type_int, | |
322 | TYPE_FN_FIELD_VOFFSET (f, j))); | |
323 | ||
324 | /* Cast the function pointer to the appropriate type. */ | |
325 | vfn = value_cast (lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j)), | |
326 | vfn); | |
327 | ||
328 | return vfn; | |
329 | } | |
330 | ||
331 | ||
332 | static void | |
333 | init_gnuv3_ops (void) | |
334 | { | |
335 | vtable_type_gdbarch_data = register_gdbarch_data (build_gdb_vtable_type, 0); | |
336 | ||
337 | gnu_v3_abi_ops.shortname = "gnu-v3"; | |
338 | gnu_v3_abi_ops.longname = "GNU G++ Version 3 ABI"; | |
339 | gnu_v3_abi_ops.doc = "G++ Version 3 ABI"; | |
340 | gnu_v3_abi_ops.is_destructor_name = is_gnu_v3_mangled_dtor; | |
341 | gnu_v3_abi_ops.is_constructor_name = is_gnu_v3_mangled_ctor; | |
342 | gnu_v3_abi_ops.is_vtable_name = gnuv3_is_vtable_name; | |
343 | gnu_v3_abi_ops.is_operator_name = gnuv3_is_operator_name; | |
344 | gnu_v3_abi_ops.rtti_type = gnuv3_rtti_type; | |
345 | gnu_v3_abi_ops.virtual_fn_field = gnuv3_virtual_fn_field; | |
346 | } | |
347 | ||
348 | ||
349 | void | |
350 | _initialize_gnu_v3_abi (void) | |
351 | { | |
352 | init_gnuv3_ops (); | |
353 | ||
354 | register_cp_abi (gnu_v3_abi_ops); | |
355 | } |