1 /*
2 * msvcrt.dll C++ objects
3 *
4 * Copyright 2000 Jon Griffiths
5 * Copyright 2003, 2004 Alexandre Julliard
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26
27 #include "windef.h"
28 #include "winternl.h"
29 #include "wine/exception.h"
30 #include "wine/debug.h"
31 #include "msvcrt.h"
32 #include "cppexcept.h"
33 #include "mtdll.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36
37 typedef exception bad_cast;
38 typedef exception bad_typeid;
39 typedef exception __non_rtti_object;
40
41 typedef struct _rtti_base_descriptor
42 {
43 const type_info *type_descriptor;
44 int num_base_classes;
45 this_ptr_offsets offsets; /* offsets for computing the this pointer */
46 unsigned int attributes;
47 } rtti_base_descriptor;
48
49 typedef struct _rtti_base_array
50 {
51 const rtti_base_descriptor *bases[3]; /* First element is the class itself */
52 } rtti_base_array;
53
54 typedef struct _rtti_object_hierarchy
55 {
56 unsigned int signature;
57 unsigned int attributes;
58 int array_len; /* Size of the array pointed to by 'base_classes' */
59 const rtti_base_array *base_classes;
60 } rtti_object_hierarchy;
61
62 typedef struct _rtti_object_locator
63 {
64 unsigned int signature;
65 int base_class_offset;
66 unsigned int flags;
67 const type_info *type_descriptor;
68 const rtti_object_hierarchy *type_hierarchy;
69 } rtti_object_locator;
70
71
72 #ifdef __i386__ /* thiscall functions are i386-specific */
73
74 #define THISCALL(func) __thiscall_ ## func
75 #define THISCALL_NAME(func) __ASM_NAME("__thiscall_" #func)
76 #define DEFINE_THISCALL_WRAPPER(func) \
77 extern void THISCALL(func)(); \
78 __ASM_GLOBAL_FUNC(__thiscall_ ## func, \
79 "popl %eax\n\t" \
80 "pushl %ecx\n\t" \
81 "pushl %eax\n\t" \
82 "jmp " __ASM_NAME(#func) )
83 #else /* __i386__ */
84
85 #define THISCALL(func) func
86 #define THISCALL_NAME(func) __ASM_NAME(#func)
87 #define DEFINE_THISCALL_WRAPPER(func) /* nothing */
88
89 #endif /* __i386__ */
90
91 extern const vtable_ptr MSVCRT_exception_vtable;
92 extern const vtable_ptr MSVCRT_bad_typeid_vtable;
93 extern const vtable_ptr MSVCRT_bad_cast_vtable;
94 extern const vtable_ptr MSVCRT___non_rtti_object_vtable;
95 extern const vtable_ptr MSVCRT_type_info_vtable;
96
97 /* get the vtable pointer for a C++ object */
98 static inline const vtable_ptr *get_vtable( void *obj )
99 {
100 return *(const vtable_ptr **)obj;
101 }
102
103 static inline const rtti_object_locator *get_obj_locator( void *cppobj )
104 {
105 const vtable_ptr *vtable = get_vtable( cppobj );
106 return (const rtti_object_locator *)vtable[-1];
107 }
108
109 static void dump_obj_locator( const rtti_object_locator *ptr )
110 {
111 int i;
112 const rtti_object_hierarchy *h = ptr->type_hierarchy;
113
114 TRACE( "%p: sig=%08x base_offset=%08x flags=%08x type=%p %s hierarchy=%p\n",
115 ptr, ptr->signature, ptr->base_class_offset, ptr->flags,
116 ptr->type_descriptor, dbgstr_type_info(ptr->type_descriptor), ptr->type_hierarchy );
117 TRACE( " hierarchy: sig=%08x attr=%08x len=%d base classes=%p\n",
118 h->signature, h->attributes, h->array_len, h->base_classes );
119 for (i = 0; i < h->array_len; i++)
120 {
121 TRACE( " base class %p: num %d off %d,%d,%d attr %08x type %p %s\n",
122 h->base_classes->bases[i],
123 h->base_classes->bases[i]->num_base_classes,
124 h->base_classes->bases[i]->offsets.this_offset,
125 h->base_classes->bases[i]->offsets.vbase_descr,
126 h->base_classes->bases[i]->offsets.vbase_offset,
127 h->base_classes->bases[i]->attributes,
128 h->base_classes->bases[i]->type_descriptor,
129 dbgstr_type_info(h->base_classes->bases[i]->type_descriptor) );
130 }
131 }
132
133 /* Internal common ctor for exception */
134 static void EXCEPTION_ctor(exception *_this, const char** name)
135 {
136 _this->vtable = &MSVCRT_exception_vtable;
137 if (*name)
138 {
139 size_t name_len = strlen(*name) + 1;
140 _this->name = MSVCRT_malloc(name_len);
141 memcpy(_this->name, *name, name_len);
142 _this->do_free = TRUE;
143 }
144 else
145 {
146 _this->name = NULL;
147 _this->do_free = FALSE;
148 }
149 }
150
151 /******************************************************************
152 * ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
153 */
154 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_ctor)
155 exception * __stdcall MSVCRT_exception_ctor(exception * _this, const char ** name)
156 {
157 TRACE("(%p,%s)\n", _this, *name);
158 EXCEPTION_ctor(_this, name);
159 return _this;
160 }
161
162 /******************************************************************
163 * ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
164 */
165 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_copy_ctor)
166 exception * __stdcall MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
167 {
168 TRACE("(%p,%p)\n", _this, rhs);
169
170 if (!rhs->do_free)
171 {
172 _this->vtable = &MSVCRT_exception_vtable;
173 _this->name = rhs->name;
174 _this->do_free = FALSE;
175 }
176 else
177 EXCEPTION_ctor(_this, (const char**)&rhs->name);
178 TRACE("name = %s\n", _this->name);
179 return _this;
180 }
181
182 /******************************************************************
183 * ??0exception@@QAE@XZ (MSVCRT.@)
184 */
185 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_default_ctor)
186 exception * __stdcall MSVCRT_exception_default_ctor(exception * _this)
187 {
188 static const char* empty = NULL;
189
190 TRACE("(%p)\n", _this);
191 EXCEPTION_ctor(_this, &empty);
192 return _this;
193 }
194
195 /******************************************************************
196 * ??1exception@@UAE@XZ (MSVCRT.@)
197 */
198 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_dtor)
199 void __stdcall MSVCRT_exception_dtor(exception * _this)
200 {
201 TRACE("(%p)\n", _this);
202 _this->vtable = &MSVCRT_exception_vtable;
203 if (_this->do_free) MSVCRT_free(_this->name);
204 }
205
206 /******************************************************************
207 * ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
208 */
209 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_opequals)
210 exception * __stdcall MSVCRT_exception_opequals(exception * _this, const exception * rhs)
211 {
212 TRACE("(%p %p)\n", _this, rhs);
213 if (_this != rhs)
214 {
215 MSVCRT_exception_dtor(_this);
216 MSVCRT_exception_copy_ctor(_this, rhs);
217 }
218 TRACE("name = %s\n", _this->name);
219 return _this;
220 }
221
222 /******************************************************************
223 * ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
224 */
225 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_vector_dtor)
226 void * __stdcall MSVCRT_exception_vector_dtor(exception * _this, unsigned int flags)
227 {
228 TRACE("(%p %x)\n", _this, flags);
229 if (flags & 2)
230 {
231 /* we have an array, with the number of elements stored before the first object */
232 int i, *ptr = (int *)_this - 1;
233
234 for (i = *ptr - 1; i >= 0; i--) MSVCRT_exception_dtor(_this + i);
235 MSVCRT_operator_delete(ptr);
236 }
237 else
238 {
239 MSVCRT_exception_dtor(_this);
240 if (flags & 1) MSVCRT_operator_delete(_this);
241 }
242 return _this;
243 }
244
245 /******************************************************************
246 * ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
247 */
248 DEFINE_THISCALL_WRAPPER(MSVCRT_exception_scalar_dtor)
249 void * __stdcall MSVCRT_exception_scalar_dtor(exception * _this, unsigned int flags)
250 {
251 TRACE("(%p %x)\n", _this, flags);
252 MSVCRT_exception_dtor(_this);
253 if (flags & 1) MSVCRT_operator_delete(_this);
254 return _this;
255 }
256
257 /******************************************************************
258 * ?what@exception@@UBEPBDXZ (MSVCRT.@)
259 */
260 DEFINE_THISCALL_WRAPPER(MSVCRT_what_exception)
261 const char * __stdcall MSVCRT_what_exception(exception * _this)
262 {
263 TRACE("(%p) returning %s\n", _this, _this->name);
264 return _this->name ? _this->name : "Unknown exception";
265 }
266
267 /******************************************************************
268 * ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
269 */
270 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_copy_ctor)
271 bad_typeid * __stdcall MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
272 {
273 TRACE("(%p %p)\n", _this, rhs);
274 MSVCRT_exception_copy_ctor(_this, rhs);
275 _this->vtable = &MSVCRT_bad_typeid_vtable;
276 return _this;
277 }
278
279 /******************************************************************
280 * ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
281 */
282 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_ctor)
283 bad_typeid * __stdcall MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
284 {
285 TRACE("(%p %s)\n", _this, name);
286 EXCEPTION_ctor(_this, &name);
287 _this->vtable = &MSVCRT_bad_typeid_vtable;
288 return _this;
289 }
290
291 /******************************************************************
292 * ??1bad_typeid@@UAE@XZ (MSVCRT.@)
293 */
294 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_dtor)
295 void __stdcall MSVCRT_bad_typeid_dtor(bad_typeid * _this)
296 {
297 TRACE("(%p)\n", _this);
298 MSVCRT_exception_dtor(_this);
299 }
300
301 /******************************************************************
302 * ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
303 */
304 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_opequals)
305 bad_typeid * __stdcall MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
306 {
307 TRACE("(%p %p)\n", _this, rhs);
308 MSVCRT_exception_opequals(_this, rhs);
309 return _this;
310 }
311
312 /******************************************************************
313 * ??_Ebad_typeid@@UAEPAXI@Z (MSVCRT.@)
314 */
315 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_vector_dtor)
316 void * __stdcall MSVCRT_bad_typeid_vector_dtor(bad_typeid * _this, unsigned int flags)
317 {
318 TRACE("(%p %x)\n", _this, flags);
319 if (flags & 2)
320 {
321 /* we have an array, with the number of elements stored before the first object */
322 int i, *ptr = (int *)_this - 1;
323
324 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_typeid_dtor(_this + i);
325 MSVCRT_operator_delete(ptr);
326 }
327 else
328 {
329 MSVCRT_bad_typeid_dtor(_this);
330 if (flags & 1) MSVCRT_operator_delete(_this);
331 }
332 return _this;
333 }
334
335 /******************************************************************
336 * ??_Gbad_typeid@@UAEPAXI@Z (MSVCRT.@)
337 */
338 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_typeid_scalar_dtor)
339 void * __stdcall MSVCRT_bad_typeid_scalar_dtor(bad_typeid * _this, unsigned int flags)
340 {
341 TRACE("(%p %x)\n", _this, flags);
342 MSVCRT_bad_typeid_dtor(_this);
343 if (flags & 1) MSVCRT_operator_delete(_this);
344 return _this;
345 }
346
347 /******************************************************************
348 * ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
349 */
350 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_copy_ctor)
351 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
352 const __non_rtti_object * rhs)
353 {
354 TRACE("(%p %p)\n", _this, rhs);
355 MSVCRT_bad_typeid_copy_ctor(_this, rhs);
356 _this->vtable = &MSVCRT___non_rtti_object_vtable;
357 return _this;
358 }
359
360 /******************************************************************
361 * ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
362 */
363 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_ctor)
364 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
365 const char * name)
366 {
367 TRACE("(%p %s)\n", _this, name);
368 EXCEPTION_ctor(_this, &name);
369 _this->vtable = &MSVCRT___non_rtti_object_vtable;
370 return _this;
371 }
372
373 /******************************************************************
374 * ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
375 */
376 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_dtor)
377 void __stdcall MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
378 {
379 TRACE("(%p)\n", _this);
380 MSVCRT_bad_typeid_dtor(_this);
381 }
382
383 /******************************************************************
384 * ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
385 */
386 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_opequals)
387 __non_rtti_object * __stdcall MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
388 const __non_rtti_object *rhs)
389 {
390 TRACE("(%p %p)\n", _this, rhs);
391 MSVCRT_bad_typeid_opequals(_this, rhs);
392 return _this;
393 }
394
395 /******************************************************************
396 * ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
397 */
398 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_vector_dtor)
399 void * __stdcall MSVCRT___non_rtti_object_vector_dtor(__non_rtti_object * _this, unsigned int flags)
400 {
401 TRACE("(%p %x)\n", _this, flags);
402 if (flags & 2)
403 {
404 /* we have an array, with the number of elements stored before the first object */
405 int i, *ptr = (int *)_this - 1;
406
407 for (i = *ptr - 1; i >= 0; i--) MSVCRT___non_rtti_object_dtor(_this + i);
408 MSVCRT_operator_delete(ptr);
409 }
410 else
411 {
412 MSVCRT___non_rtti_object_dtor(_this);
413 if (flags & 1) MSVCRT_operator_delete(_this);
414 }
415 return _this;
416 }
417
418 /******************************************************************
419 * ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
420 */
421 DEFINE_THISCALL_WRAPPER(MSVCRT___non_rtti_object_scalar_dtor)
422 void * __stdcall MSVCRT___non_rtti_object_scalar_dtor(__non_rtti_object * _this, unsigned int flags)
423 {
424 TRACE("(%p %x)\n", _this, flags);
425 MSVCRT___non_rtti_object_dtor(_this);
426 if (flags & 1) MSVCRT_operator_delete(_this);
427 return _this;
428 }
429
430 /******************************************************************
431 * ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
432 */
433 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_ctor)
434 bad_cast * __stdcall MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
435 {
436 TRACE("(%p %s)\n", _this, *name);
437 EXCEPTION_ctor(_this, name);
438 _this->vtable = &MSVCRT_bad_cast_vtable;
439 return _this;
440 }
441
442 /******************************************************************
443 * ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
444 */
445 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_copy_ctor)
446 bad_cast * __stdcall MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
447 {
448 TRACE("(%p %p)\n", _this, rhs);
449 MSVCRT_exception_copy_ctor(_this, rhs);
450 _this->vtable = &MSVCRT_bad_cast_vtable;
451 return _this;
452 }
453
454 /******************************************************************
455 * ??1bad_cast@@UAE@XZ (MSVCRT.@)
456 */
457 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_dtor)
458 void __stdcall MSVCRT_bad_cast_dtor(bad_cast * _this)
459 {
460 TRACE("(%p)\n", _this);
461 MSVCRT_exception_dtor(_this);
462 }
463
464 /******************************************************************
465 * ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
466 */
467 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_opequals)
468 bad_cast * __stdcall MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
469 {
470 TRACE("(%p %p)\n", _this, rhs);
471 MSVCRT_exception_opequals(_this, rhs);
472 return _this;
473 }
474
475 /******************************************************************
476 * ??_Ebad_cast@@UAEPAXI@Z (MSVCRT.@)
477 */
478 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_vector_dtor)
479 void * __stdcall MSVCRT_bad_cast_vector_dtor(bad_cast * _this, unsigned int flags)
480 {
481 TRACE("(%p %x)\n", _this, flags);
482 if (flags & 2)
483 {
484 /* we have an array, with the number of elements stored before the first object */
485 int i, *ptr = (int *)_this - 1;
486
487 for (i = *ptr - 1; i >= 0; i--) MSVCRT_bad_cast_dtor(_this + i);
488 MSVCRT_operator_delete(ptr);
489 }
490 else
491 {
492 MSVCRT_bad_cast_dtor(_this);
493 if (flags & 1) MSVCRT_operator_delete(_this);
494 }
495 return _this;
496 }
497
498 /******************************************************************
499 * ??_Gbad_cast@@UAEPAXI@Z (MSVCRT.@)
500 */
501 DEFINE_THISCALL_WRAPPER(MSVCRT_bad_cast_scalar_dtor)
502 void * __stdcall MSVCRT_bad_cast_scalar_dtor(bad_cast * _this, unsigned int flags)
503 {
504 TRACE("(%p %x)\n", _this, flags);
505 MSVCRT_bad_cast_dtor(_this);
506 if (flags & 1) MSVCRT_operator_delete(_this);
507 return _this;
508 }
509
510 /******************************************************************
511 * ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
512 */
513 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opequals_equals)
514 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
515 {
516 int ret = !strcmp(_this->mangled + 1, rhs->mangled + 1);
517 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
518 return ret;
519 }
520
521 /******************************************************************
522 * ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
523 */
524 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_opnot_equals)
525 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
526 {
527 int ret = !!strcmp(_this->mangled + 1, rhs->mangled + 1);
528 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
529 return ret;
530 }
531
532 /******************************************************************
533 * ?before@type_info@@QBEHABV1@@Z (MSVCRT.@)
534 */
535 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_before)
536 int __stdcall MSVCRT_type_info_before(type_info * _this, const type_info * rhs)
537 {
538 int ret = strcmp(_this->mangled + 1, rhs->mangled + 1) < 0;
539 TRACE("(%p %p) returning %d\n", _this, rhs, ret);
540 return ret;
541 }
542
543 /******************************************************************
544 * ??1type_info@@UAE@XZ (MSVCRT.@)
545 */
546 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_dtor)
547 void __stdcall MSVCRT_type_info_dtor(type_info * _this)
548 {
549 TRACE("(%p)\n", _this);
550 MSVCRT_free(_this->name);
551 }
552
553 /******************************************************************
554 * ?name@type_info@@QBEPBDXZ (MSVCRT.@)
555 */
556 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_name)
557 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
558 {
559 if (!_this->name)
560 {
561 /* Create and set the demangled name */
562 /* Nota: mangled name in type_info struct always start with a '.', while
563 * it isn't valid for mangled name.
564 * Is this '.' really part of the mangled name, or has it some other meaning ?
565 */
566 char* name = __unDName(0, _this->mangled + 1, 0,
567 MSVCRT_malloc, MSVCRT_free, 0x2800);
568
569 if (name)
570 {
571 unsigned int len = strlen(name);
572
573 /* It seems _unDName may leave blanks at the end of the demangled name */
574 while (len && name[--len] == ' ')
575 name[len] = '\0';
576
577 _mlock(_EXIT_LOCK2);
578
579 if (_this->name)
580 {
581 /* Another thread set this member since we checked above - use it */
582 MSVCRT_free(name);
583 }
584 else
585 _this->name = name;
586
587 _munlock(_EXIT_LOCK2);
588 }
589 }
590 TRACE("(%p) returning %s\n", _this, _this->name);
591 return _this->name;
592 }
593
594 /******************************************************************
595 * ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
596 */
597 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_raw_name)
598 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
599 {
600 TRACE("(%p) returning %s\n", _this, _this->mangled);
601 return _this->mangled;
602 }
603
604 /* Unexported */
605 DEFINE_THISCALL_WRAPPER(MSVCRT_type_info_vector_dtor)
606 void * __stdcall MSVCRT_type_info_vector_dtor(type_info * _this, unsigned int flags)
607 {
608 TRACE("(%p %x)\n", _this, flags);
609 if (flags & 2)
610 {
611 /* we have an array, with the number of elements stored before the first object */
612 int i, *ptr = (int *)_this - 1;
613
614 for (i = *ptr - 1; i >= 0; i--) MSVCRT_type_info_dtor(_this + i);
615 MSVCRT_operator_delete(ptr);
616 }
617 else
618 {
619 MSVCRT_type_info_dtor(_this);
620 if (flags & 1) MSVCRT_operator_delete(_this);
621 }
622 return _this;
623 }
624
625 /* vtables */
626
627 #ifdef _WIN64
628
629 #define __ASM_VTABLE(name,funcs) \
630 __asm__(".data\n" \
631 "\t.align 8\n" \
632 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
633 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
634 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
635 "\t.quad " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
636 funcs "\n\t.text");
637
638 #define __ASM_EXCEPTION_VTABLE(name) \
639 __ASM_VTABLE(name, "\t.quad " THISCALL_NAME(MSVCRT_what_exception) )
640
641 #else
642
643 #define __ASM_VTABLE(name,funcs) \
644 __asm__(".data\n" \
645 "\t.align 4\n" \
646 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
647 "\t.globl " __ASM_NAME("MSVCRT_" #name "_vtable") "\n" \
648 __ASM_NAME("MSVCRT_" #name "_vtable") ":\n" \
649 "\t.long " THISCALL_NAME(MSVCRT_ ## name ## _vector_dtor) "\n" \
650 funcs "\n\t.text");
651
652 #define __ASM_EXCEPTION_VTABLE(name) \
653 __ASM_VTABLE(name, "\t.long " THISCALL_NAME(MSVCRT_what_exception) )
654
655 #endif /* _WIN64 */
656
657 #ifndef __GNUC__
658 void __asm_dummy_vtables(void) {
659 #endif
660
661 __ASM_VTABLE(type_info,"")
662 __ASM_EXCEPTION_VTABLE(exception)
663 __ASM_EXCEPTION_VTABLE(bad_typeid)
664 __ASM_EXCEPTION_VTABLE(bad_cast)
665 __ASM_EXCEPTION_VTABLE(__non_rtti_object)
666
667 #ifndef __GNUC__
668 }
669 #endif
670
671 /* Static RTTI for exported objects */
672
673 static const type_info exception_type_info =
674 {
675 &MSVCRT_type_info_vtable,
676 NULL,
677 ".?AVexception@@"
678 };
679
680 static const rtti_base_descriptor exception_rtti_base_descriptor =
681 {
682 &exception_type_info,
683 0,
684 { 0, -1, 0 },
685 0
686 };
687
688 static const rtti_base_array exception_rtti_base_array =
689 {
690 {
691 &exception_rtti_base_descriptor,
692 NULL,
693 NULL
694 }
695 };
696
697 static const rtti_object_hierarchy exception_type_hierarchy =
698 {
699 0,
700 0,
701 1,
702 &exception_rtti_base_array
703 };
704
705 const rtti_object_locator exception_rtti =
706 {
707 0,
708 0,
709 0,
710 &exception_type_info,
711 &exception_type_hierarchy
712 };
713
714 static const cxx_type_info exception_cxx_type_info =
715 {
716 0,
717 &exception_type_info,
718 { 0, -1, 0 },
719 sizeof(exception),
720 (cxx_copy_ctor)THISCALL(MSVCRT_exception_copy_ctor)
721 };
722
723 static const type_info bad_typeid_type_info =
724 {
725 &MSVCRT_type_info_vtable,
726 NULL,
727 ".?AVbad_typeid@@"
728 };
729
730 static const rtti_base_descriptor bad_typeid_rtti_base_descriptor =
731 {
732 &bad_typeid_type_info,
733 1,
734 { 0, -1, 0 },
735 0
736 };
737
738 static const rtti_base_array bad_typeid_rtti_base_array =
739 {
740 {
741 &bad_typeid_rtti_base_descriptor,
742 &exception_rtti_base_descriptor,
743 NULL
744 }
745 };
746
747 static const rtti_object_hierarchy bad_typeid_type_hierarchy =
748 {
749 0,
750 0,
751 2,
752 &bad_typeid_rtti_base_array
753 };
754
755 const rtti_object_locator bad_typeid_rtti =
756 {
757 0,
758 0,
759 0,
760 &bad_typeid_type_info,
761 &bad_typeid_type_hierarchy
762 };
763
764 static const cxx_type_info bad_typeid_cxx_type_info =
765 {
766 0,
767 &bad_typeid_type_info,
768 { 0, -1, 0 },
769 sizeof(exception),
770 (cxx_copy_ctor)THISCALL(MSVCRT_bad_typeid_copy_ctor)
771 };
772
773 static const type_info bad_cast_type_info =
774 {
775 &MSVCRT_type_info_vtable,
776 NULL,
777 ".?AVbad_cast@@"
778 };
779
780 static const rtti_base_descriptor bad_cast_rtti_base_descriptor =
781 {
782 &bad_cast_type_info,
783 1,
784 { 0, -1, 0 },
785 0
786 };
787
788 static const rtti_base_array bad_cast_rtti_base_array =
789 {
790 {
791 &bad_cast_rtti_base_descriptor,
792 &exception_rtti_base_descriptor,
793 NULL
794 }
795 };
796
797 static const rtti_object_hierarchy bad_cast_type_hierarchy =
798 {
799 0,
800 0,
801 2,
802 &bad_cast_rtti_base_array
803 };
804
805 const rtti_object_locator bad_cast_rtti =
806 {
807 0,
808 0,
809 0,
810 &bad_cast_type_info,
811 &bad_cast_type_hierarchy
812 };
813
814 static const cxx_type_info bad_cast_cxx_type_info =
815 {
816 0,
817 &bad_cast_type_info,
818 { 0, -1, 0 },
819 sizeof(exception),
820 (cxx_copy_ctor)THISCALL(MSVCRT_bad_cast_copy_ctor)
821 };
822
823 static const type_info __non_rtti_object_type_info =
824 {
825 &MSVCRT_type_info_vtable,
826 NULL,
827 ".?AV__non_rtti_object@@"
828 };
829
830 static const rtti_base_descriptor __non_rtti_object_rtti_base_descriptor =
831 {
832 &__non_rtti_object_type_info,
833 2,
834 { 0, -1, 0 },
835 0
836 };
837
838 static const rtti_base_array __non_rtti_object_rtti_base_array =
839 {
840 {
841 &__non_rtti_object_rtti_base_descriptor,
842 &bad_typeid_rtti_base_descriptor,
843 &exception_rtti_base_descriptor
844 }
845 };
846
847 static const rtti_object_hierarchy __non_rtti_object_type_hierarchy =
848 {
849 0,
850 0,
851 3,
852 &__non_rtti_object_rtti_base_array
853 };
854
855 const rtti_object_locator __non_rtti_object_rtti =
856 {
857 0,
858 0,
859 0,
860 &__non_rtti_object_type_info,
861 &__non_rtti_object_type_hierarchy
862 };
863
864 static const cxx_type_info __non_rtti_object_cxx_type_info =
865 {
866 0,
867 &__non_rtti_object_type_info,
868 { 0, -1, 0 },
869 sizeof(exception),
870 (cxx_copy_ctor)THISCALL(MSVCRT___non_rtti_object_copy_ctor)
871 };
872
873 static const type_info type_info_type_info =
874 {
875 &MSVCRT_type_info_vtable,
876 NULL,
877 ".?AVtype_info@@"
878 };
879
880 static const rtti_base_descriptor type_info_rtti_base_descriptor =
881 {
882 &type_info_type_info,
883 0,
884 { 0, -1, 0 },
885 0
886 };
887
888 static const rtti_base_array type_info_rtti_base_array =
889 {
890 {
891 &type_info_rtti_base_descriptor,
892 NULL,
893 NULL
894 }
895 };
896
897 static const rtti_object_hierarchy type_info_type_hierarchy =
898 {
899 0,
900 0,
901 1,
902 &type_info_rtti_base_array
903 };
904
905 const rtti_object_locator type_info_rtti =
906 {
907 0,
908 0,
909 0,
910 &type_info_type_info,
911 &type_info_type_hierarchy
912 };
913
914 /*
915 * Exception RTTI for cpp objects
916 */
917 static const cxx_type_info_table bad_cast_type_info_table =
918 {
919 3,
920 {
921 &__non_rtti_object_cxx_type_info,
922 &bad_typeid_cxx_type_info,
923 &exception_cxx_type_info
924 }
925 };
926
927 static const cxx_exception_type bad_cast_exception_type =
928 {
929 0,
930 (void*)THISCALL(MSVCRT_bad_cast_dtor),
931 NULL,
932 &bad_cast_type_info_table
933 };
934
935 static const cxx_type_info_table bad_typeid_type_info_table =
936 {
937 2,
938 {
939 &bad_cast_cxx_type_info,
940 &exception_cxx_type_info,
941 NULL
942 }
943 };
944
945 static const cxx_exception_type bad_typeid_exception_type =
946 {
947 0,
948 (void*)THISCALL(MSVCRT_bad_typeid_dtor),
949 NULL,
950 &bad_cast_type_info_table
951 };
952
953 static const cxx_exception_type __non_rtti_object_exception_type =
954 {
955 0,
956 (void*)THISCALL(MSVCRT___non_rtti_object_dtor),
957 NULL,
958 &bad_typeid_type_info_table
959 };
960
961
962 /******************************************************************
963 * ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
964 *
965 * Install a handler to be called when terminate() is called.
966 *
967 * PARAMS
968 * func [I] Handler function to install
969 *
970 * RETURNS
971 * The previously installed handler function, if any.
972 */
973 MSVCRT_terminate_function CDECL MSVCRT_set_terminate(MSVCRT_terminate_function func)
974 {
975 thread_data_t *data = msvcrt_get_thread_data();
976 MSVCRT_terminate_function previous = data->terminate_handler;
977 TRACE("(%p) returning %p\n",func,previous);
978 data->terminate_handler = func;
979 return previous;
980 }
981
982 /******************************************************************
983 * ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
984 *
985 * Install a handler to be called when unexpected() is called.
986 *
987 * PARAMS
988 * func [I] Handler function to install
989 *
990 * RETURNS
991 * The previously installed handler function, if any.
992 */
993 MSVCRT_unexpected_function CDECL MSVCRT_set_unexpected(MSVCRT_unexpected_function func)
994 {
995 thread_data_t *data = msvcrt_get_thread_data();
996 MSVCRT_unexpected_function previous = data->unexpected_handler;
997 TRACE("(%p) returning %p\n",func,previous);
998 data->unexpected_handler = func;
999 return previous;
1000 }
1001
1002 /******************************************************************
1003 * ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z (MSVCRT.@)
1004 */
1005 MSVCRT__se_translator_function CDECL MSVCRT__set_se_translator(MSVCRT__se_translator_function func)
1006 {
1007 thread_data_t *data = msvcrt_get_thread_data();
1008 MSVCRT__se_translator_function previous = data->se_translator;
1009 TRACE("(%p) returning %p\n",func,previous);
1010 data->se_translator = func;
1011 return previous;
1012 }
1013
1014 /******************************************************************
1015 * ?terminate@@YAXXZ (MSVCRT.@)
1016 *
1017 * Default handler for an unhandled exception.
1018 *
1019 * PARAMS
1020 * None.
1021 *
1022 * RETURNS
1023 * This function does not return. Either control resumes from any
1024 * handler installed by calling set_terminate(), or (by default) abort()
1025 * is called.
1026 */
1027 void CDECL MSVCRT_terminate(void)
1028 {
1029 thread_data_t *data = msvcrt_get_thread_data();
1030 if (data->terminate_handler) data->terminate_handler();
1031 MSVCRT_abort();
1032 }
1033
1034 /******************************************************************
1035 * ?unexpected@@YAXXZ (MSVCRT.@)
1036 */
1037 void CDECL MSVCRT_unexpected(void)
1038 {
1039 thread_data_t *data = msvcrt_get_thread_data();
1040 if (data->unexpected_handler) data->unexpected_handler();
1041 MSVCRT_terminate();
1042 }
1043
1044
1045 /******************************************************************
1046 * __RTtypeid (MSVCRT.@)
1047 *
1048 * Retrieve the Run Time Type Information (RTTI) for a C++ object.
1049 *
1050 * PARAMS
1051 * cppobj [I] C++ object to get type information for.
1052 *
1053 * RETURNS
1054 * Success: A type_info object describing cppobj.
1055 * Failure: If the object to be cast has no RTTI, a __non_rtti_object
1056 * exception is thrown. If cppobj is NULL, a bad_typeid exception
1057 * is thrown. In either case, this function does not return.
1058 *
1059 * NOTES
1060 * This function is usually called by compiler generated code as a result
1061 * of using one of the C++ dynamic cast statements.
1062 */
1063 const type_info* CDECL MSVCRT___RTtypeid(void *cppobj)
1064 {
1065 const type_info *ret;
1066
1067 if (!cppobj)
1068 {
1069 bad_typeid e;
1070 MSVCRT_bad_typeid_ctor( &e, "Attempted a typeid of NULL pointer!" );
1071 _CxxThrowException( &e, &bad_typeid_exception_type );
1072 return NULL;
1073 }
1074
1075 __TRY
1076 {
1077 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1078 ret = obj_locator->type_descriptor;
1079 }
1080 __EXCEPT_PAGE_FAULT
1081 {
1082 __non_rtti_object e;
1083 MSVCRT___non_rtti_object_ctor( &e, "Bad read pointer - no RTTI data!" );
1084 _CxxThrowException( &e, &bad_typeid_exception_type );
1085 return NULL;
1086 }
1087 __ENDTRY
1088 return ret;
1089 }
1090
1091 /******************************************************************
1092 * __RTDynamicCast (MSVCRT.@)
1093 *
1094 * Dynamically cast a C++ object to one of its base classes.
1095 *
1096 * PARAMS
1097 * cppobj [I] Any C++ object to cast
1098 * unknown [I] Reserved, set to 0
1099 * src [I] type_info object describing cppobj
1100 * dst [I] type_info object describing the base class to cast to
1101 * do_throw [I] TRUE = throw an exception if the cast fails, FALSE = don't
1102 *
1103 * RETURNS
1104 * Success: The address of cppobj, cast to the object described by dst.
1105 * Failure: NULL, If the object to be cast has no RTTI, or dst is not a
1106 * valid cast for cppobj. If do_throw is TRUE, a bad_cast exception
1107 * is thrown and this function does not return.
1108 *
1109 * NOTES
1110 * This function is usually called by compiler generated code as a result
1111 * of using one of the C++ dynamic cast statements.
1112 */
1113 void* CDECL MSVCRT___RTDynamicCast(void *cppobj, int unknown,
1114 type_info *src, type_info *dst,
1115 int do_throw)
1116 {
1117 void *ret;
1118
1119 if (!cppobj) return NULL;
1120
1121 TRACE("obj: %p unknown: %d src: %p %s dst: %p %s do_throw: %d)\n",
1122 cppobj, unknown, src, dbgstr_type_info(src), dst, dbgstr_type_info(dst), do_throw);
1123
1124 /* To cast an object at runtime:
1125 * 1.Find out the true type of the object from the typeinfo at vtable[-1]
1126 * 2.Search for the destination type in the class hierarchy
1127 * 3.If destination type is found, return base object address + dest offset
1128 * Otherwise, fail the cast
1129 *
1130 * FIXME: the unknown parameter doesn't seem to be used for anything
1131 */
1132 __TRY
1133 {
1134 int i;
1135 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1136 const rtti_object_hierarchy *obj_bases = obj_locator->type_hierarchy;
1137 const rtti_base_descriptor * const* base_desc = obj_bases->base_classes->bases;
1138
1139 if (TRACE_ON(msvcrt)) dump_obj_locator(obj_locator);
1140
1141 ret = NULL;
1142 for (i = 0; i < obj_bases->array_len; i++)
1143 {
1144 const type_info *typ = base_desc[i]->type_descriptor;
1145
1146 if (!strcmp(typ->mangled, dst->mangled))
1147 {
1148 /* compute the correct this pointer for that base class */
1149 void *this_ptr = (char *)cppobj - obj_locator->base_class_offset;
1150 ret = get_this_pointer( &base_desc[i]->offsets, this_ptr );
1151 break;
1152 }
1153 }
1154 /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
1155 * to a reference, since references cannot be NULL.
1156 */
1157 if (!ret && do_throw)
1158 {
1159 const char *msg = "Bad dynamic_cast!";
1160 bad_cast e;
1161 MSVCRT_bad_cast_ctor( &e, &msg );
1162 _CxxThrowException( &e, &bad_cast_exception_type );
1163 }
1164 }
1165 __EXCEPT_PAGE_FAULT
1166 {
1167 __non_rtti_object e;
1168 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1169 _CxxThrowException( &e, &bad_typeid_exception_type );
1170 return NULL;
1171 }
1172 __ENDTRY
1173 return ret;
1174 }
1175
1176
1177 /******************************************************************
1178 * __RTCastToVoid (MSVCRT.@)
1179 *
1180 * Dynamically cast a C++ object to a void*.
1181 *
1182 * PARAMS
1183 * cppobj [I] The C++ object to cast
1184 *
1185 * RETURNS
1186 * Success: The base address of the object as a void*.
1187 * Failure: NULL, if cppobj is NULL or has no RTTI.
1188 *
1189 * NOTES
1190 * This function is usually called by compiler generated code as a result
1191 * of using one of the C++ dynamic cast statements.
1192 */
1193 void* CDECL MSVCRT___RTCastToVoid(void *cppobj)
1194 {
1195 void *ret;
1196
1197 if (!cppobj) return NULL;
1198
1199 __TRY
1200 {
1201 const rtti_object_locator *obj_locator = get_obj_locator( cppobj );
1202 ret = (char *)cppobj - obj_locator->base_class_offset;
1203 }
1204 __EXCEPT_PAGE_FAULT
1205 {
1206 __non_rtti_object e;
1207 MSVCRT___non_rtti_object_ctor( &e, "Access violation - no RTTI data!" );
1208 _CxxThrowException( &e, &bad_typeid_exception_type );
1209 return NULL;
1210 }
1211 __ENDTRY
1212 return ret;
1213 }
1214
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.