2 * Copyright 2012 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "rtlsupportapi.h"
27 #define RTTI_USE_RVA 1
32 #define VTABLE_ADD_FUNC(name) "\t.quad " THISCALL_NAME(name) "\n"
34 #define __ASM_VTABLE(name,funcs) \
37 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
38 "\t.globl " __ASM_NAME(#name "_vtable") "\n" \
39 __ASM_NAME(#name "_vtable") ":\n" \
44 #define VTABLE_ADD_FUNC(name) "\t.long " THISCALL_NAME(name) "\n"
46 #define __ASM_VTABLE(name,funcs) \
49 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
50 "\t.globl " __ASM_NAME(#name "_vtable") "\n" \
51 __ASM_NAME(#name "_vtable") ":\n" \
58 #define DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
59 static type_info name ## _type_info = { \
65 static const rtti_base_descriptor name ## _rtti_base_descriptor = { \
72 #define DEFINE_RTTI_DATA(name, off, base_classes_no, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, mangled_name) \
73 DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
75 static const rtti_base_array name ## _rtti_base_array = { \
77 &name ## _rtti_base_descriptor, \
90 static const rtti_object_hierarchy name ## _hierarchy = { \
94 &name ## _rtti_base_array \
97 const rtti_object_locator name ## _rtti = { \
101 &name ## _type_info, \
102 &name ## _hierarchy \
105 #define DEFINE_CXX_TYPE_INFO(type) \
106 static const cxx_type_info type ## _cxx_type_info = { \
108 & type ##_type_info, \
111 THISCALL(type ##_copy_ctor) \
114 #define DEFINE_CXX_DATA(type, base_no, cl1, cl2, cl3, cl4, dtor) \
115 DEFINE_CXX_TYPE_INFO(type) \
117 static const cxx_type_info_table type ## _cxx_type_table = { \
120 & type ## _cxx_type_info, \
128 static const cxx_exception_type type ## _exception_type = { \
132 & type ## _cxx_type_table \
137 #define __DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
138 static type_info name ## _type_info = { \
144 static rtti_base_descriptor name ## _rtti_base_descriptor = { \
151 #define DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
152 __DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
154 static void init_ ## name ## _rtti(char *base) \
156 name ## _rtti_base_descriptor.type_descriptor = (char*)&name ## _type_info - base; \
159 #define DEFINE_RTTI_DATA(name, off, base_classes_no, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, mangled_name) \
160 __DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
162 static rtti_base_array name ## _rtti_base_array = { \
177 static rtti_object_hierarchy name ## _hierarchy = { \
184 rtti_object_locator name ## _rtti = { \
193 static void init_ ## name ## _rtti(char *base) \
195 name ## _rtti_base_descriptor.type_descriptor = (char*)&name ## _type_info - base; \
196 name ## _rtti_base_array.bases[0] = (char*)&name ## _rtti_base_descriptor - base; \
197 name ## _rtti_base_array.bases[1] = (char*)cl1 - base; \
198 name ## _rtti_base_array.bases[2] = (char*)cl2 - base; \
199 name ## _rtti_base_array.bases[3] = (char*)cl3 - base; \
200 name ## _rtti_base_array.bases[4] = (char*)cl4 - base; \
201 name ## _rtti_base_array.bases[5] = (char*)cl5 - base; \
202 name ## _rtti_base_array.bases[6] = (char*)cl6 - base; \
203 name ## _rtti_base_array.bases[7] = (char*)cl7 - base; \
204 name ## _rtti_base_array.bases[8] = (char*)cl8 - base; \
205 name ## _rtti_base_array.bases[9] = (char*)cl9 - base; \
206 name ## _hierarchy.base_classes = (char*)&name ## _rtti_base_array - base; \
207 name ## _rtti.type_descriptor = (char*)&name ## _type_info - base; \
208 name ## _rtti.type_hierarchy = (char*)&name ## _hierarchy - base; \
209 name ## _rtti.object_locator = (char*)&name ## _rtti - base; \
212 #define DEFINE_CXX_TYPE_INFO(type) \
213 static cxx_type_info type ## _cxx_type_info = { \
221 static void init_ ## type ## _cxx_type_info(char *base) \
223 type ## _cxx_type_info.type_info = (char *)&type ## _type_info - base; \
224 type ## _cxx_type_info.copy_ctor = (char *)type ## _copy_ctor - base; \
227 #define DEFINE_CXX_DATA(type, base_no, cl1, cl2, cl3, cl4, dtor) \
229 DEFINE_CXX_TYPE_INFO(type) \
231 static cxx_type_info_table type ## _cxx_type_table = { \
242 static cxx_exception_type type ##_exception_type = { \
249 static void init_ ## type ## _cxx(char *base) \
251 init_ ## type ## _cxx_type_info(base); \
252 type ## _cxx_type_table.info[0] = (char *)&type ## _cxx_type_info - base; \
253 type ## _cxx_type_table.info[1] = (char *)cl1 - base; \
254 type ## _cxx_type_table.info[2] = (char *)cl2 - base; \
255 type ## _cxx_type_table.info[3] = (char *)cl3 - base; \
256 type ## _cxx_type_table.info[4] = (char *)cl4 - base; \
257 type ## _exception_type.destructor = (char *)dtor - base; \
258 type ## _exception_type.type_info_table = (char *)&type ## _cxx_type_table - base; \
263 #define DEFINE_RTTI_DATA0(name, off, mangled_name) \
264 DEFINE_RTTI_DATA(name, off, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, mangled_name)
265 #define DEFINE_RTTI_DATA1(name, off, cl1, mangled_name) \
266 DEFINE_RTTI_DATA(name, off, 1, cl1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, mangled_name)
267 #define DEFINE_RTTI_DATA2(name, off, cl1, cl2, mangled_name) \
268 DEFINE_RTTI_DATA(name, off, 2, cl1, cl2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, mangled_name)
269 #define DEFINE_RTTI_DATA3(name, off, cl1, cl2, cl3, mangled_name) \
270 DEFINE_RTTI_DATA(name, off, 3, cl1, cl2, cl3, NULL, NULL, NULL, NULL, NULL, NULL, mangled_name)
271 #define DEFINE_RTTI_DATA4(name, off, cl1, cl2, cl3, cl4, mangled_name) \
272 DEFINE_RTTI_DATA(name, off, 4, cl1, cl2, cl3, cl4, NULL, NULL, NULL, NULL, NULL, mangled_name)
273 #define DEFINE_RTTI_DATA5(name, off, cl1, cl2, cl3, cl4, cl5, mangled_name) \
274 DEFINE_RTTI_DATA(name, off, 5, cl1, cl2, cl3, cl4, cl5, NULL, NULL, NULL, NULL, mangled_name)
275 #define DEFINE_RTTI_DATA8(name, off, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, mangled_name) \
276 DEFINE_RTTI_DATA(name, off, 8, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, NULL, mangled_name)
277 #define DEFINE_RTTI_DATA9(name, off, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, mangled_name) \
278 DEFINE_RTTI_DATA(name, off, 9, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, mangled_name)
280 #define DEFINE_CXX_DATA0(name, dtor) \
281 DEFINE_CXX_DATA(name, 0, NULL, NULL, NULL, NULL, dtor)
282 #define DEFINE_CXX_DATA1(name, cl1, dtor) \
283 DEFINE_CXX_DATA(name, 1, cl1, NULL, NULL, NULL, dtor)
284 #define DEFINE_CXX_DATA2(name, cl1, cl2, dtor) \
285 DEFINE_CXX_DATA(name, 2, cl1, cl2, NULL, NULL, dtor)
286 #define DEFINE_CXX_DATA3(name, cl1, cl2, cl3, dtor) \
287 DEFINE_CXX_DATA(name, 3, cl1, cl2, cl3, NULL, dtor)
288 #define DEFINE_CXX_DATA4(name, cl1, cl2, cl3, cl4, dtor) \
289 DEFINE_CXX_DATA(name, 4, cl1, cl2, cl3, cl4, dtor)
291 #ifdef __ASM_USE_THISCALL_WRAPPER
293 #define CALL_VTBL_FUNC(this, off, ret, type, args) ((ret (WINAPI*)type)&vtbl_wrapper_##off)args
295 extern void *vtbl_wrapper_0
;
296 extern void *vtbl_wrapper_4
;
297 extern void *vtbl_wrapper_8
;
298 extern void *vtbl_wrapper_12
;
299 extern void *vtbl_wrapper_16
;
300 extern void *vtbl_wrapper_20
;
301 extern void *vtbl_wrapper_24
;
302 extern void *vtbl_wrapper_28
;
303 extern void *vtbl_wrapper_32
;
304 extern void *vtbl_wrapper_36
;
305 extern void *vtbl_wrapper_40
;
306 extern void *vtbl_wrapper_44
;
307 extern void *vtbl_wrapper_48
;
308 extern void *vtbl_wrapper_52
;
309 extern void *vtbl_wrapper_56
;
313 #define CALL_VTBL_FUNC(this, off, ret, type, args) ((ret (__thiscall***)type)this)[0][off/4]args
317 /* exception object */
318 typedef void (*vtable_ptr
)(void);
319 typedef struct __exception
321 const vtable_ptr
*vtable
;
322 char *name
; /* Name of this exception, always a new copy for each object */
323 int do_free
; /* Whether to free 'name' in our dtor */
327 typedef struct __type_info
329 const vtable_ptr
*vtable
;
330 char *name
; /* Unmangled name, allocated lazily */
331 char mangled
[128]; /* Variable length, but we declare it large enough for static RTTI */
334 /* offsets for computing the this pointer */
337 int this_offset
; /* offset of base class this pointer from start of object */
338 int vbase_descr
; /* offset of virtual base class descriptor */
339 int vbase_offset
; /* offset of this pointer offset in virtual base class descriptor */
344 typedef struct _rtti_base_descriptor
346 const type_info
*type_descriptor
;
347 int num_base_classes
;
348 this_ptr_offsets offsets
; /* offsets for computing the this pointer */
349 unsigned int attributes
;
350 } rtti_base_descriptor
;
352 typedef struct _rtti_base_array
354 const rtti_base_descriptor
*bases
[10]; /* First element is the class itself */
357 typedef struct _rtti_object_hierarchy
359 unsigned int signature
;
360 unsigned int attributes
;
361 int array_len
; /* Size of the array pointed to by 'base_classes' */
362 const rtti_base_array
*base_classes
;
363 } rtti_object_hierarchy
;
365 typedef struct _rtti_object_locator
367 unsigned int signature
;
368 int base_class_offset
;
370 const type_info
*type_descriptor
;
371 const rtti_object_hierarchy
*type_hierarchy
;
372 } rtti_object_locator
;
377 const type_info
*type_info
;
378 this_ptr_offsets offsets
;
386 const cxx_type_info
*info
[5];
387 } cxx_type_info_table
;
393 void *custom_handler
;
394 const cxx_type_info_table
*type_info_table
;
395 } cxx_exception_type
;
401 unsigned int type_descriptor
;
402 int num_base_classes
;
403 this_ptr_offsets offsets
; /* offsets for computing the this pointer */
404 unsigned int attributes
;
405 } rtti_base_descriptor
;
409 unsigned int bases
[10]; /* First element is the class itself */
414 unsigned int signature
;
415 unsigned int attributes
;
416 int array_len
; /* Size of the array pointed to by 'base_classes' */
417 unsigned int base_classes
;
418 } rtti_object_hierarchy
;
422 unsigned int signature
;
423 int base_class_offset
;
425 unsigned int type_descriptor
;
426 unsigned int type_hierarchy
;
427 unsigned int object_locator
;
428 } rtti_object_locator
;
433 unsigned int type_info
;
434 this_ptr_offsets offsets
;
436 unsigned int copy_ctor
;
442 unsigned int info
[5];
443 } cxx_type_info_table
;
448 unsigned int destructor
;
449 unsigned int custom_handler
;
450 unsigned int type_info_table
;
451 } cxx_exception_type
;
455 extern const vtable_ptr type_info_vtable
;
459 static inline uintptr_t rtti_rva_base( const void *ptr
)
462 return (uintptr_t)RtlPcToFileHeader( (void *)ptr
, &base
);
465 static inline void *rtti_rva( unsigned int rva
, uintptr_t base
)
467 return (void *)(base
+ rva
);
472 static inline uintptr_t rtti_rva_base( const void *ptr
)
477 static inline void *rtti_rva( const void *ptr
, uintptr_t base
)
484 #define CREATE_TYPE_INFO_VTABLE \
485 DEFINE_THISCALL_WRAPPER(type_info_vector_dtor,8) \
486 void * __thiscall type_info_vector_dtor(type_info * _this, unsigned int flags) \
490 /* we have an array, with the number of elements stored before the first object */ \
491 INT_PTR i, *ptr = (INT_PTR *)_this - 1; \
493 for (i = *ptr - 1; i >= 0; i--) free(_this[i].name); \
499 if (flags & 1) free(_this); \
504 DEFINE_RTTI_DATA0( type_info, 0, ".?AVtype_info@@" ) \
506 __ASM_BLOCK_BEGIN(type_info_vtables) \
507 __ASM_VTABLE(type_info, \
508 VTABLE_ADD_FUNC(type_info_vector_dtor)); \