exception.cc revision 232950
1/*
2 * Copyright 2010-2011 PathScale, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 *    this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 *    this list of conditions and the following disclaimer in the documentation
12 *    and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
15 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <stdlib.h>
28#include <dlfcn.h>
29#include <stdio.h>
30#include <string.h>
31#include <stdint.h>
32#include <pthread.h>
33#include "typeinfo.h"
34#include "dwarf_eh.h"
35#include "cxxabi.h"
36
37#pragma weak pthread_key_create
38#pragma weak pthread_setspecific
39#pragma weak pthread_getspecific
40#pragma weak pthread_once
41
42using namespace ABI_NAMESPACE;
43
44/**
45 * Saves the result of the landing pad that we have found.  For ARM, this is
46 * stored in the generic unwind structure, while on other platforms it is
47 * stored in the C++ exception.
48 */
49static void saveLandingPad(struct _Unwind_Context *context,
50                           struct _Unwind_Exception *ucb,
51                           struct __cxa_exception *ex,
52                           int selector,
53                           dw_eh_ptr_t landingPad)
54{
55#ifdef __arm__
56	// On ARM, we store the saved exception in the generic part of the structure
57	ucb->barrier_cache.sp = _Unwind_GetGR(context, 13);
58	ucb->barrier_cache.bitpattern[1] = (uint32_t)selector;
59	ucb->barrier_cache.bitpattern[3] = (uint32_t)landingPad;
60#endif
61	// Cache the results for the phase 2 unwind, if we found a handler
62	// and this is not a foreign exception.
63	if (ex)
64	{
65		ex->handlerSwitchValue = selector;
66		ex->catchTemp = landingPad;
67	}
68}
69
70/**
71 * Loads the saved landing pad.  Returns 1 on success, 0 on failure.
72 */
73static int loadLandingPad(struct _Unwind_Context *context,
74                          struct _Unwind_Exception *ucb,
75                          struct __cxa_exception *ex,
76                          unsigned long *selector,
77                          dw_eh_ptr_t *landingPad)
78{
79#ifdef __arm__
80	*selector = ucb->barrier_cache.bitpattern[1];
81	*landingPad = (dw_eh_ptr_t)ucb->barrier_cache.bitpattern[3];
82	return 1;
83#else
84	if (ex)
85	{
86		*selector = ex->handlerSwitchValue;
87		*landingPad = (dw_eh_ptr_t)ex->catchTemp;
88		return 0;
89	}
90	return 0;
91#endif
92}
93
94static inline _Unwind_Reason_Code continueUnwinding(struct _Unwind_Exception *ex,
95                                                    struct _Unwind_Context *context)
96{
97#ifdef __arm__
98	if (__gnu_unwind_frame(ex, context) != _URC_OK) { return _URC_FAILURE; }
99#endif
100	return _URC_CONTINUE_UNWIND;
101}
102
103
104extern "C" void __cxa_free_exception(void *thrown_exception);
105extern "C" void __cxa_free_dependent_exception(void *thrown_exception);
106extern "C" void* __dynamic_cast(const void *sub,
107                                const __class_type_info *src,
108                                const __class_type_info *dst,
109                                ptrdiff_t src2dst_offset);
110
111/**
112 * The type of a handler that has been found.
113 */
114typedef enum
115{
116	/** No handler. */
117	handler_none,
118	/**
119	 * A cleanup - the exception will propagate through this frame, but code
120	 * must be run when this happens.
121	 */
122	handler_cleanup,
123	/**
124	 * A catch statement.  The exception will not propagate past this frame
125	 * (without an explicit rethrow).
126	 */
127	handler_catch
128} handler_type;
129
130/**
131 * Per-thread info required by the runtime.  We store a single structure
132 * pointer in thread-local storage, because this tends to be a scarce resource
133 * and it's impolite to steal all of it and not leave any for the rest of the
134 * program.
135 *
136 * Instances of this structure are allocated lazily - at most one per thread -
137 * and are destroyed on thread termination.
138 */
139struct __cxa_thread_info
140{
141	/** The termination handler for this thread. */
142	terminate_handler terminateHandler;
143	/** The unexpected exception handler for this thread. */
144	unexpected_handler unexpectedHandler;
145	/**
146	 * The number of emergency buffers held by this thread.  This is 0 in
147	 * normal operation - the emergency buffers are only used when malloc()
148	 * fails to return memory for allocating an exception.  Threads are not
149	 * permitted to hold more than 4 emergency buffers (as per recommendation
150	 * in ABI spec [3.3.1]).
151	 */
152	int emergencyBuffersHeld;
153	/**
154	 * The exception currently running in a cleanup.
155	 */
156	_Unwind_Exception *currentCleanup;
157	/**
158	 * The public part of this structure, accessible from outside of this
159	 * module.
160	 */
161	__cxa_eh_globals globals;
162};
163/**
164 * Dependent exception.  This
165 */
166struct __cxa_dependent_exception
167{
168#if __LP64__
169	void *primaryException;
170#endif
171	std::type_info *exceptionType;
172	void (*exceptionDestructor) (void *);
173	unexpected_handler unexpectedHandler;
174	terminate_handler terminateHandler;
175	__cxa_exception *nextException;
176	int handlerCount;
177#ifdef __arm__
178	_Unwind_Exception *nextCleanup;
179	int cleanupCount;
180#endif
181	int handlerSwitchValue;
182	const char *actionRecord;
183	const char *languageSpecificData;
184	void *catchTemp;
185	void *adjustedPtr;
186#if !__LP64__
187	void *primaryException;
188#endif
189	_Unwind_Exception unwindHeader;
190};
191
192
193namespace std
194{
195	void unexpected();
196	class exception
197	{
198		public:
199			virtual ~exception() throw();
200			virtual const char* what() const throw();
201	};
202
203}
204
205extern "C" std::type_info *__cxa_current_exception_type();
206
207/**
208 * Class of exceptions to distinguish between this and other exception types.
209 *
210 * The first four characters are the vendor ID.  Currently, we use GNUC,
211 * because we aim for ABI-compatibility with the GNU implementation, and
212 * various checks may test for equality of the class, which is incorrect.
213 */
214static const uint64_t exception_class =
215	EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\0');
216/**
217 * Class used for dependent exceptions.
218 */
219static const uint64_t dependent_exception_class =
220	EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\x01');
221/**
222 * The low four bytes of the exception class, indicating that we conform to the
223 * Itanium C++ ABI.  This is currently unused, but should be used in the future
224 * if we change our exception class, to allow this library and libsupc++ to be
225 * linked to the same executable and both to interoperate.
226 */
227static const uint32_t abi_exception_class =
228	GENERIC_EXCEPTION_CLASS('C', '+', '+', '\0');
229
230static bool isCXXException(uint64_t cls)
231{
232	return (cls == exception_class) || (cls == dependent_exception_class);
233}
234
235static bool isDependentException(uint64_t cls)
236{
237	return cls == dependent_exception_class;
238}
239
240static __cxa_exception *exceptionFromPointer(void *ex)
241{
242	return (__cxa_exception*)((char*)ex -
243			offsetof(struct __cxa_exception, unwindHeader));
244}
245static __cxa_exception *realExceptionFromException(__cxa_exception *ex)
246{
247	if (!isDependentException(ex->unwindHeader.exception_class)) { return ex; }
248	return ((__cxa_exception*)(((__cxa_dependent_exception*)ex)->primaryException))-1;
249}
250
251
252namespace std
253{
254	// Forward declaration of standard library terminate() function used to
255	// abort execution.
256	void terminate(void);
257}
258
259using namespace ABI_NAMESPACE;
260
261
262
263/** The global termination handler. */
264static terminate_handler terminateHandler = abort;
265/** The global unexpected exception handler. */
266static unexpected_handler unexpectedHandler = std::terminate;
267
268/** Key used for thread-local data. */
269static pthread_key_t eh_key;
270
271
272/**
273 * Cleanup function, allowing foreign exception handlers to correctly destroy
274 * this exception if they catch it.
275 */
276static void exception_cleanup(_Unwind_Reason_Code reason,
277                              struct _Unwind_Exception *ex)
278{
279	__cxa_free_exception((void*)ex);
280}
281static void dependent_exception_cleanup(_Unwind_Reason_Code reason,
282                              struct _Unwind_Exception *ex)
283{
284
285	__cxa_free_dependent_exception((void*)ex);
286}
287
288/**
289 * Recursively walk a list of exceptions and delete them all in post-order.
290 */
291static void free_exception_list(__cxa_exception *ex)
292{
293	if (0 != ex->nextException)
294	{
295		free_exception_list(ex->nextException);
296	}
297	// __cxa_free_exception() expects to be passed the thrown object, which
298	// immediately follows the exception, not the exception itself
299	__cxa_free_exception(ex+1);
300}
301
302/**
303 * Cleanup function called when a thread exists to make certain that all of the
304 * per-thread data is deleted.
305 */
306static void thread_cleanup(void* thread_info)
307{
308	__cxa_thread_info *info = (__cxa_thread_info*)thread_info;
309	if (info->globals.caughtExceptions)
310	{
311		free_exception_list(info->globals.caughtExceptions);
312	}
313	free(thread_info);
314}
315
316
317/**
318 * Once control used to protect the key creation.
319 */
320static pthread_once_t once_control = PTHREAD_ONCE_INIT;
321
322/**
323 * We may not be linked against a full pthread implementation.  If we're not,
324 * then we need to fake the thread-local storage by storing 'thread-local'
325 * things in a global.
326 */
327static bool fakeTLS;
328/**
329 * Thread-local storage for a single-threaded program.
330 */
331static __cxa_thread_info singleThreadInfo;
332/**
333 * Initialise eh_key.
334 */
335static void init_key(void)
336{
337	if ((0 == pthread_key_create) ||
338	    (0 == pthread_setspecific) ||
339	    (0 == pthread_getspecific))
340	{
341		fakeTLS = true;
342		return;
343	}
344	pthread_key_create(&eh_key, thread_cleanup);
345	pthread_setspecific(eh_key, (void*)0x42);
346	fakeTLS = (pthread_getspecific(eh_key) != (void*)0x42);
347	pthread_setspecific(eh_key, 0);
348}
349
350/**
351 * Returns the thread info structure, creating it if it is not already created.
352 */
353static __cxa_thread_info *thread_info()
354{
355	if ((0 == pthread_once) || pthread_once(&once_control, init_key))
356	{
357		fakeTLS = true;
358	}
359	if (fakeTLS) { return &singleThreadInfo; }
360	__cxa_thread_info *info = (__cxa_thread_info*)pthread_getspecific(eh_key);
361	if (0 == info)
362	{
363		info = (__cxa_thread_info*)calloc(1, sizeof(__cxa_thread_info));
364		pthread_setspecific(eh_key, info);
365	}
366	return info;
367}
368/**
369 * Fast version of thread_info().  May fail if thread_info() is not called on
370 * this thread at least once already.
371 */
372static __cxa_thread_info *thread_info_fast()
373{
374	if (fakeTLS) { return &singleThreadInfo; }
375	return (__cxa_thread_info*)pthread_getspecific(eh_key);
376}
377/**
378 * ABI function returning the __cxa_eh_globals structure.
379 */
380extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals(void)
381{
382	return &(thread_info()->globals);
383}
384/**
385 * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already
386 * been called at least once by this thread.
387 */
388extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals_fast(void)
389{
390	return &(thread_info_fast()->globals);
391}
392
393/**
394 * An emergency allocation reserved for when malloc fails.  This is treated as
395 * 16 buffers of 1KB each.
396 */
397static char emergency_buffer[16384];
398/**
399 * Flag indicating whether each buffer is allocated.
400 */
401static bool buffer_allocated[16];
402/**
403 * Lock used to protect emergency allocation.
404 */
405static pthread_mutex_t emergency_malloc_lock = PTHREAD_MUTEX_INITIALIZER;
406/**
407 * Condition variable used to wait when two threads are both trying to use the
408 * emergency malloc() buffer at once.
409 */
410static pthread_cond_t emergency_malloc_wait = PTHREAD_COND_INITIALIZER;
411
412/**
413 * Allocates size bytes from the emergency allocation mechanism, if possible.
414 * This function will fail if size is over 1KB or if this thread already has 4
415 * emergency buffers.  If all emergency buffers are allocated, it will sleep
416 * until one becomes available.
417 */
418static char *emergency_malloc(size_t size)
419{
420	if (size > 1024) { return 0; }
421
422	__cxa_thread_info *info = thread_info();
423	// Only 4 emergency buffers allowed per thread!
424	if (info->emergencyBuffersHeld > 3) { return 0; }
425
426	pthread_mutex_lock(&emergency_malloc_lock);
427	int buffer = -1;
428	while (buffer < 0)
429	{
430		// While we were sleeping on the lock, another thread might have free'd
431		// enough memory for us to use, so try the allocation again - no point
432		// using the emergency buffer if there is some real memory that we can
433		// use...
434		void *m = calloc(1, size);
435		if (0 != m)
436		{
437			pthread_mutex_unlock(&emergency_malloc_lock);
438			return (char*)m;
439		}
440		for (int i=0 ; i<16 ; i++)
441		{
442			if (!buffer_allocated[i])
443			{
444				buffer = i;
445				buffer_allocated[i] = true;
446				break;
447			}
448		}
449		// If there still isn't a buffer available, then sleep on the condition
450		// variable.  This will be signalled when another thread releases one
451		// of the emergency buffers.
452		if (buffer < 0)
453		{
454			pthread_cond_wait(&emergency_malloc_wait, &emergency_malloc_lock);
455		}
456	}
457	pthread_mutex_unlock(&emergency_malloc_lock);
458	info->emergencyBuffersHeld++;
459	return emergency_buffer + (1024 * buffer);
460}
461
462/**
463 * Frees a buffer returned by emergency_malloc().
464 *
465 * Note: Neither this nor emergency_malloc() is particularly efficient.  This
466 * should not matter, because neither will be called in normal operation - they
467 * are only used when the program runs out of memory, which should not happen
468 * often.
469 */
470static void emergency_malloc_free(char *ptr)
471{
472	int buffer = -1;
473	// Find the buffer corresponding to this pointer.
474	for (int i=0 ; i<16 ; i++)
475	{
476		if (ptr == (void*)(emergency_buffer + (1024 * i)))
477		{
478			buffer = i;
479			break;
480		}
481	}
482	assert(buffer > 0 &&
483	       "Trying to free something that is not an emergency buffer!");
484	// emergency_malloc() is expected to return 0-initialized data.  We don't
485	// zero the buffer when allocating it, because the static buffers will
486	// begin life containing 0 values.
487	memset((void*)ptr, 0, 1024);
488	// Signal the condition variable to wake up any threads that are blocking
489	// waiting for some space in the emergency buffer
490	pthread_mutex_lock(&emergency_malloc_lock);
491	// In theory, we don't need to do this with the lock held.  In practice,
492	// our array of bools will probably be updated using 32-bit or 64-bit
493	// memory operations, so this update may clobber adjacent values.
494	buffer_allocated[buffer] = false;
495	pthread_cond_signal(&emergency_malloc_wait);
496	pthread_mutex_unlock(&emergency_malloc_lock);
497}
498
499static char *alloc_or_die(size_t size)
500{
501	char *buffer = (char*)calloc(1, size);
502
503	// If calloc() doesn't want to give us any memory, try using an emergency
504	// buffer.
505	if (0 == buffer)
506	{
507		buffer = emergency_malloc(size);
508		// This is only reached if the allocation is greater than 1KB, and
509		// anyone throwing objects that big really should know better.
510		if (0 == buffer)
511		{
512			fprintf(stderr, "Out of memory attempting to allocate exception\n");
513			std::terminate();
514		}
515	}
516	return buffer;
517}
518static void free_exception(char *e)
519{
520	// If this allocation is within the address range of the emergency buffer,
521	// don't call free() because it was not allocated with malloc()
522	if ((e > emergency_buffer) &&
523	    (e < (emergency_buffer + sizeof(emergency_buffer))))
524	{
525		emergency_malloc_free(e);
526	}
527	else
528	{
529		free(e);
530	}
531}
532
533/**
534 * Allocates an exception structure.  Returns a pointer to the space that can
535 * be used to store an object of thrown_size bytes.  This function will use an
536 * emergency buffer if malloc() fails, and may block if there are no such
537 * buffers available.
538 */
539extern "C" void *__cxa_allocate_exception(size_t thrown_size)
540{
541	size_t size = thrown_size + sizeof(__cxa_exception);
542	char *buffer = alloc_or_die(size);
543	return buffer+sizeof(__cxa_exception);
544}
545
546extern "C" void *__cxa_allocate_dependent_exception(void)
547{
548	size_t size = sizeof(__cxa_dependent_exception);
549	char *buffer = alloc_or_die(size);
550	return buffer+sizeof(__cxa_dependent_exception);
551}
552
553/**
554 * __cxa_free_exception() is called when an exception was thrown in between
555 * calling __cxa_allocate_exception() and actually throwing the exception.
556 * This happens when the object's copy constructor throws an exception.
557 *
558 * In this implementation, it is also called by __cxa_end_catch() and during
559 * thread cleanup.
560 */
561extern "C" void __cxa_free_exception(void *thrown_exception)
562{
563	__cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
564	// Free the object that was thrown, calling its destructor
565	if (0 != ex->exceptionDestructor)
566	{
567		try
568		{
569			ex->exceptionDestructor(thrown_exception);
570		}
571		catch(...)
572		{
573			// FIXME: Check that this is really what the spec says to do.
574			std::terminate();
575		}
576	}
577
578	free_exception((char*)ex);
579}
580
581static void releaseException(__cxa_exception *exception)
582{
583	if (isDependentException(exception->unwindHeader.exception_class))
584	{
585		__cxa_free_dependent_exception(exception+1);
586		return;
587	}
588	if (__sync_sub_and_fetch(&exception->referenceCount, 1) == 0)
589	{
590		// __cxa_free_exception() expects to be passed the thrown object,
591		// which immediately follows the exception, not the exception
592		// itself
593		__cxa_free_exception(exception+1);
594	}
595}
596
597void __cxa_free_dependent_exception(void *thrown_exception)
598{
599	__cxa_dependent_exception *ex = ((__cxa_dependent_exception*)thrown_exception) - 1;
600	assert(isDependentException(ex->unwindHeader.exception_class));
601	if (ex->primaryException)
602	{
603		releaseException(realExceptionFromException((__cxa_exception*)ex));
604	}
605	free_exception((char*)ex);
606}
607
608/**
609 * Callback function used with _Unwind_Backtrace().
610 *
611 * Prints a stack trace.  Used only for debugging help.
612 *
613 * Note: As of FreeBSD 8.1, dladd() still doesn't work properly, so this only
614 * correctly prints function names from public, relocatable, symbols.
615 */
616static _Unwind_Reason_Code trace(struct _Unwind_Context *context, void *c)
617{
618	Dl_info myinfo;
619	int mylookup =
620		dladdr((void*)(uintptr_t)__cxa_current_exception_type, &myinfo);
621	void *ip = (void*)_Unwind_GetIP(context);
622	Dl_info info;
623	if (dladdr(ip, &info) != 0)
624	{
625		if (mylookup == 0 || strcmp(info.dli_fname, myinfo.dli_fname) != 0)
626		{
627			printf("%p:%s() in %s\n", ip, info.dli_sname, info.dli_fname);
628		}
629	}
630	return _URC_CONTINUE_UNWIND;
631}
632
633/**
634 * Report a failure that occurred when attempting to throw an exception.
635 *
636 * If the failure happened by falling off the end of the stack without finding
637 * a handler, prints a back trace before aborting.
638 */
639static void report_failure(_Unwind_Reason_Code err, __cxa_exception *thrown_exception)
640{
641	switch (err)
642	{
643		default: break;
644		case _URC_FATAL_PHASE1_ERROR:
645			fprintf(stderr, "Fatal error during phase 1 unwinding\n");
646			break;
647#ifndef __arm__
648		case _URC_FATAL_PHASE2_ERROR:
649			fprintf(stderr, "Fatal error during phase 2 unwinding\n");
650			break;
651#endif
652		case _URC_END_OF_STACK:
653			fprintf(stderr, "Terminating due to uncaught exception %p",
654					(void*)thrown_exception);
655			thrown_exception = realExceptionFromException(thrown_exception);
656			static const __class_type_info *e_ti =
657				static_cast<const __class_type_info*>(&typeid(std::exception));
658			const __class_type_info *throw_ti =
659				dynamic_cast<const __class_type_info*>(thrown_exception->exceptionType);
660			if (throw_ti)
661			{
662				std::exception *e =
663					(std::exception*)e_ti->cast_to((void*)(thrown_exception+1),
664							throw_ti);
665				if (e)
666				{
667					fprintf(stderr, " '%s'", e->what());
668				}
669			}
670
671			size_t bufferSize = 128;
672			char *demangled = (char*)malloc(bufferSize);
673			const char *mangled = thrown_exception->exceptionType->name();
674			int status;
675			demangled = __cxa_demangle(mangled, demangled, &bufferSize, &status);
676			fprintf(stderr, " of type %s\n",
677				status == 0 ? (const char*)demangled : mangled);
678			if (status == 0) { free(demangled); }
679			// Print a back trace if no handler is found.
680			// TODO: Make this optional
681			_Unwind_Backtrace(trace, 0);
682			break;
683	}
684	std::terminate();
685}
686
687static void throw_exception(__cxa_exception *ex)
688{
689	__cxa_thread_info *info = thread_info();
690	ex->unexpectedHandler = info->unexpectedHandler;
691	if (0 == ex->unexpectedHandler)
692	{
693		ex->unexpectedHandler = unexpectedHandler;
694	}
695	ex->terminateHandler  = info->terminateHandler;
696	if (0 == ex->terminateHandler)
697	{
698		ex->terminateHandler = terminateHandler;
699	}
700	info->globals.uncaughtExceptions++;
701
702	_Unwind_Reason_Code err = _Unwind_RaiseException(&ex->unwindHeader);
703	// The _Unwind_RaiseException() function should not return, it should
704	// unwind the stack past this function.  If it does return, then something
705	// has gone wrong.
706	report_failure(err, ex);
707}
708
709
710/**
711 * ABI function for throwing an exception.  Takes the object to be thrown (the
712 * pointer returned by __cxa_allocate_exception()), the type info for the
713 * pointee, and the destructor (if there is one) as arguments.
714 */
715extern "C" void __cxa_throw(void *thrown_exception,
716                            std::type_info *tinfo,
717                            void(*dest)(void*))
718{
719	__cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
720
721	ex->referenceCount = 1;
722	ex->exceptionType = tinfo;
723
724	ex->exceptionDestructor = dest;
725
726	ex->unwindHeader.exception_class = exception_class;
727	ex->unwindHeader.exception_cleanup = exception_cleanup;
728
729	throw_exception(ex);
730}
731
732extern "C" void __cxa_rethrow_primary_exception(void* thrown_exception)
733{
734	if (NULL == thrown_exception) { return; }
735
736	__cxa_exception *original = exceptionFromPointer(thrown_exception);
737	__cxa_dependent_exception *ex = ((__cxa_dependent_exception*)__cxa_allocate_dependent_exception())-1;
738
739	ex->primaryException = thrown_exception;
740	__cxa_increment_exception_refcount(thrown_exception);
741
742	ex->exceptionType = original->exceptionType;
743	ex->unwindHeader.exception_class = dependent_exception_class;
744	ex->unwindHeader.exception_cleanup = dependent_exception_cleanup;
745
746	throw_exception((__cxa_exception*)ex);
747}
748
749extern "C" void *__cxa_current_primary_exception(void)
750{
751	__cxa_eh_globals* globals = __cxa_get_globals();
752	__cxa_exception *ex = globals->caughtExceptions;
753
754	if (0 == ex) { return NULL; }
755	ex = realExceptionFromException(ex);
756	__sync_fetch_and_add(&ex->referenceCount, 1);
757	return ex + 1;
758}
759
760extern "C" void __cxa_increment_exception_refcount(void* thrown_exception)
761{
762	if (NULL == thrown_exception) { return; }
763	__cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
764	if (isDependentException(ex->unwindHeader.exception_class)) { return; }
765	__sync_fetch_and_add(&ex->referenceCount, 1);
766}
767extern "C" void __cxa_decrement_exception_refcount(void* thrown_exception)
768{
769	if (NULL == thrown_exception) { return; }
770	__cxa_exception *ex = ((__cxa_exception*)thrown_exception) - 1;
771	releaseException(ex);
772}
773
774/**
775 * ABI function.  Rethrows the current exception.  Does not remove the
776 * exception from the stack or decrement its handler count - the compiler is
777 * expected to set the landing pad for this function to the end of the catch
778 * block, and then call _Unwind_Resume() to continue unwinding once
779 * __cxa_end_catch() has been called and any cleanup code has been run.
780 */
781extern "C" void __cxa_rethrow()
782{
783	__cxa_eh_globals *globals = __cxa_get_globals();
784	// Note: We don't remove this from the caught list here, because
785	// __cxa_end_catch will be called when we unwind out of the try block.  We
786	// could probably make this faster by providing an alternative rethrow
787	// function and ensuring that all cleanup code is run before calling it, so
788	// we can skip the top stack frame when unwinding.
789	__cxa_exception *ex = globals->caughtExceptions;
790
791	if (0 == ex)
792	{
793		fprintf(stderr,
794		        "Attempting to rethrow an exception that doesn't exist!\n");
795		std::terminate();
796	}
797
798	assert(ex->handlerCount > 0 && "Rethrowing uncaught exception!");
799
800	// ex->handlerCount will be decremented in __cxa_end_catch in enclosing
801	// catch block
802
803	// Make handler count negative. This will tell __cxa_end_catch that
804	// exception was rethrown and exception object should not be destroyed
805	// when handler count become zero
806	ex->handlerCount = -ex->handlerCount;
807
808	// Continue unwinding the stack with this exception.  This should unwind to
809	// the place in the caller where __cxa_end_catch() is called.  The caller
810	// will then run cleanup code and bounce the exception back with
811	// _Unwind_Resume().
812	_Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(&ex->unwindHeader);
813	report_failure(err, ex);
814}
815
816/**
817 * Returns the type_info object corresponding to the filter.
818 */
819static std::type_info *get_type_info_entry(_Unwind_Context *context,
820                                           dwarf_eh_lsda *lsda,
821                                           int filter)
822{
823	// Get the address of the record in the table.
824	dw_eh_ptr_t record = lsda->type_table -
825		dwarf_size_of_fixed_size_field(lsda->type_table_encoding)*filter;
826	//record -= 4;
827	dw_eh_ptr_t start = record;
828	// Read the value, but it's probably an indirect reference...
829	int64_t offset = read_value(lsda->type_table_encoding, &record);
830
831	// (If the entry is 0, don't try to dereference it.  That would be bad.)
832	if (offset == 0) { return 0; }
833
834	// ...so we need to resolve it
835	return (std::type_info*)resolve_indirect_value(context,
836			lsda->type_table_encoding, offset, start);
837}
838
839
840
841/**
842 * Checks the type signature found in a handler against the type of the thrown
843 * object.  If ex is 0 then it is assumed to be a foreign exception and only
844 * matches cleanups.
845 */
846static bool check_type_signature(__cxa_exception *ex,
847                                 const std::type_info *type,
848                                 void *&adjustedPtr)
849{
850	// TODO: For compatibility with the GNU implementation, we should move this
851	// out into a __do_catch() virtual function in std::type_info
852	void *exception_ptr = (void*)(ex+1);
853    const std::type_info *ex_type = ex->exceptionType;
854
855	const __pointer_type_info *ptr_type =
856		dynamic_cast<const __pointer_type_info*>(ex_type);
857	if (0 != ptr_type)
858	{
859		exception_ptr = *(void**)exception_ptr;
860	}
861	// Always match a catchall, even with a foreign exception
862	//
863	// Note: A 0 here is a catchall, not a cleanup, so we return true to
864	// indicate that we found a catch.
865	//
866	// TODO: Provide a class for matching against foreign exceptions.  This is
867	// already done in libobjc2, allowing C++ exceptions to be boxed as
868	// Objective-C objects.  We should do something similar, allowing foreign
869	// exceptions to be wrapped in a C++ exception and delivered.
870	if (0 == type)
871	{
872		if (ex)
873		{
874			adjustedPtr = exception_ptr;
875		}
876		return true;
877	}
878
879	if (0 == ex) { return false; }
880
881	const __pointer_type_info *target_ptr_type =
882		dynamic_cast<const __pointer_type_info*>(type);
883
884	if (0 != ptr_type && 0 != target_ptr_type)
885	{
886		if (ptr_type->__flags & ~target_ptr_type->__flags)
887		{
888			// Handler pointer is less qualified
889			return false;
890		}
891
892		// Special case for void* handler.
893		if(*target_ptr_type->__pointee == typeid(void))
894		{
895			adjustedPtr = exception_ptr;
896			return true;
897		}
898
899		ex_type = ptr_type->__pointee;
900		type = target_ptr_type->__pointee;
901	}
902
903	// If the types are the same, no casting is needed.
904	if (*type == *ex_type)
905	{
906		adjustedPtr = exception_ptr;
907		return true;
908	}
909
910	const __class_type_info *cls_type =
911		dynamic_cast<const __class_type_info*>(ex_type);
912	const __class_type_info *target_cls_type =
913		dynamic_cast<const __class_type_info*>(type);
914
915	if (0 != cls_type &&
916		0 != target_cls_type &&
917		cls_type->can_cast_to(target_cls_type))
918	{
919		adjustedPtr = cls_type->cast_to(exception_ptr, target_cls_type);
920		return true;
921	}
922	return false;
923}
924/**
925 * Checks whether the exception matches the type specifiers in this action
926 * record.  If the exception only matches cleanups, then this returns false.
927 * If it matches a catch (including a catchall) then it returns true.
928 *
929 * The selector argument is used to return the selector that is passed in the
930 * second exception register when installing the context.
931 */
932static handler_type check_action_record(_Unwind_Context *context,
933                                        dwarf_eh_lsda *lsda,
934                                        dw_eh_ptr_t action_record,
935                                        __cxa_exception *ex,
936                                        unsigned long *selector,
937                                        void *&adjustedPtr)
938{
939	if (!action_record) { return handler_cleanup; }
940	handler_type found = handler_none;
941	while (action_record)
942	{
943		int filter = read_sleb128(&action_record);
944		dw_eh_ptr_t action_record_offset_base = action_record;
945		int displacement = read_sleb128(&action_record);
946		action_record = displacement ?
947			action_record_offset_base + displacement : 0;
948		// We only check handler types for C++ exceptions - foreign exceptions
949		// are only allowed for cleanup.
950		if (filter > 0 && 0 != ex)
951		{
952			std::type_info *handler_type = get_type_info_entry(context, lsda, filter);
953			if (check_type_signature(ex, handler_type, adjustedPtr))
954			{
955				*selector = filter;
956				return handler_catch;
957			}
958		}
959		else if (filter < 0 && 0 != ex)
960		{
961			bool matched = false;
962			*selector = filter;
963#ifdef __arm__
964			filter++;
965			std::type_info *handler_type = get_type_info_entry(context, lsda, filter--);
966			while (handler_type)
967			{
968				if (check_type_signature(ex, handler_type, adjustedPtr))
969				{
970					matched = true;
971					break;
972				}
973				handler_type = get_type_info_entry(context, lsda, filter--);
974			}
975#else
976			unsigned char *type_index = ((unsigned char*)lsda->type_table - filter - 1);
977			while (*type_index)
978			{
979				std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++));
980				// If the exception spec matches a permitted throw type for
981				// this function, don't report a handler - we are allowed to
982				// propagate this exception out.
983				if (check_type_signature(ex, handler_type, adjustedPtr))
984				{
985					matched = true;
986					break;
987				}
988			}
989#endif
990			if (matched) { continue; }
991			// If we don't find an allowed exception spec, we need to install
992			// the context for this action.  The landing pad will then call the
993			// unexpected exception function.  Treat this as a catch
994			return handler_catch;
995		}
996		else if (filter == 0)
997		{
998			*selector = filter;
999			found = handler_cleanup;
1000		}
1001	}
1002	return found;
1003}
1004
1005static void pushCleanupException(_Unwind_Exception *exceptionObject,
1006                                 __cxa_exception *ex)
1007{
1008#ifdef __arm__
1009	__cxa_thread_info *info = thread_info_fast();
1010	if (ex)
1011	{
1012		ex->cleanupCount++;
1013		if (ex->cleanupCount > 1)
1014		{
1015			assert(exceptionObject == info->currentCleanup);
1016			return;
1017		}
1018		ex->nextCleanup = info->currentCleanup;
1019	}
1020	info->currentCleanup = exceptionObject;
1021#endif
1022}
1023
1024/**
1025 * The exception personality function.  This is referenced in the unwinding
1026 * DWARF metadata and is called by the unwind library for each C++ stack frame
1027 * containing catch or cleanup code.
1028 */
1029extern "C"
1030BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0)
1031	// This personality function is for version 1 of the ABI.  If you use it
1032	// with a future version of the ABI, it won't know what to do, so it
1033	// reports a fatal error and give up before it breaks anything.
1034	if (1 != version)
1035	{
1036		return _URC_FATAL_PHASE1_ERROR;
1037	}
1038	__cxa_exception *ex = 0;
1039	__cxa_exception *realEx = 0;
1040
1041	// If this exception is throw by something else then we can't make any
1042	// assumptions about its layout beyond the fields declared in
1043	// _Unwind_Exception.
1044	bool foreignException = !isCXXException(exceptionClass);
1045
1046	// If this isn't a foreign exception, then we have a C++ exception structure
1047	if (!foreignException)
1048	{
1049		ex = exceptionFromPointer(exceptionObject);
1050		realEx = realExceptionFromException(ex);
1051	}
1052
1053	unsigned char *lsda_addr =
1054		(unsigned char*)_Unwind_GetLanguageSpecificData(context);
1055
1056	// No LSDA implies no landing pads - try the next frame
1057	if (0 == lsda_addr) { return continueUnwinding(exceptionObject, context); }
1058
1059	// These two variables define how the exception will be handled.
1060	dwarf_eh_action action = {0};
1061	unsigned long selector = 0;
1062
1063	// During the search phase, we do a complete lookup.  If we return
1064	// _URC_HANDLER_FOUND, then the phase 2 unwind will call this function with
1065	// a _UA_HANDLER_FRAME action, telling us to install the handler frame.  If
1066	// we return _URC_CONTINUE_UNWIND, we may be called again later with a
1067	// _UA_CLEANUP_PHASE action for this frame.
1068	//
1069	// The point of the two-stage unwind allows us to entirely avoid any stack
1070	// unwinding if there is no handler.  If there are just cleanups found,
1071	// then we can just panic call an abort function.
1072	//
1073	// Matching a handler is much more expensive than matching a cleanup,
1074	// because we don't need to bother doing type comparisons (or looking at
1075	// the type table at all) for a cleanup.  This means that there is no need
1076	// to cache the result of finding a cleanup, because it's (quite) quick to
1077	// look it up again from the action table.
1078	if (actions & _UA_SEARCH_PHASE)
1079	{
1080		struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1081
1082		if (!dwarf_eh_find_callsite(context, &lsda, &action))
1083		{
1084			// EH range not found. This happens if exception is thrown and not
1085			// caught inside a cleanup (destructor).  We should call
1086			// terminate() in this case.  The catchTemp (landing pad) field of
1087			// exception object will contain null when personality function is
1088			// called with _UA_HANDLER_FRAME action for phase 2 unwinding.
1089			return _URC_HANDLER_FOUND;
1090		}
1091
1092		handler_type found_handler = check_action_record(context, &lsda,
1093				action.action_record, realEx, &selector, ex->adjustedPtr);
1094		// If there's no action record, we've only found a cleanup, so keep
1095		// searching for something real
1096		if (found_handler == handler_catch)
1097		{
1098			// Cache the results for the phase 2 unwind, if we found a handler
1099			// and this is not a foreign exception.
1100			if (ex)
1101			{
1102				saveLandingPad(context, exceptionObject, ex, selector, action.landing_pad);
1103				ex->languageSpecificData = (const char*)lsda_addr;
1104				ex->actionRecord = (const char*)action.action_record;
1105				// ex->adjustedPtr is set when finding the action record.
1106			}
1107			return _URC_HANDLER_FOUND;
1108		}
1109		return continueUnwinding(exceptionObject, context);
1110	}
1111
1112
1113	// If this is a foreign exception, we didn't have anywhere to cache the
1114	// lookup stuff, so we need to do it again.  If this is either a forced
1115	// unwind, a foreign exception, or a cleanup, then we just install the
1116	// context for a cleanup.
1117	if (!(actions & _UA_HANDLER_FRAME))
1118	{
1119		// cleanup
1120		struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1121		dwarf_eh_find_callsite(context, &lsda, &action);
1122		if (0 == action.landing_pad) { return continueUnwinding(exceptionObject, context); }
1123		handler_type found_handler = check_action_record(context, &lsda,
1124				action.action_record, realEx, &selector, ex->adjustedPtr);
1125		// Ignore handlers this time.
1126		if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); }
1127		pushCleanupException(exceptionObject, ex);
1128	}
1129	else if (foreignException)
1130	{
1131		struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1132		dwarf_eh_find_callsite(context, &lsda, &action);
1133		check_action_record(context, &lsda, action.action_record, realEx,
1134				&selector, ex->adjustedPtr);
1135	}
1136	else if (ex->catchTemp == 0)
1137	{
1138		// Uncaught exception in cleanup, calling terminate
1139		std::terminate();
1140	}
1141	else
1142	{
1143		// Restore the saved info if we saved some last time.
1144		loadLandingPad(context, exceptionObject, ex, &selector, &action.landing_pad);
1145		ex->catchTemp = 0;
1146		ex->handlerSwitchValue = 0;
1147	}
1148
1149
1150	_Unwind_SetIP(context, (unsigned long)action.landing_pad);
1151	_Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
1152	              (unsigned long)exceptionObject);
1153	_Unwind_SetGR(context, __builtin_eh_return_data_regno(1), selector);
1154
1155	return _URC_INSTALL_CONTEXT;
1156}
1157
1158/**
1159 * ABI function called when entering a catch statement.  The argument is the
1160 * pointer passed out of the personality function.  This is always the start of
1161 * the _Unwind_Exception object.  The return value for this function is the
1162 * pointer to the caught exception, which is either the adjusted pointer (for
1163 * C++ exceptions) of the unadjusted pointer (for foreign exceptions).
1164 */
1165#if __GNUC__ > 3 && __GNUC_MINOR__ > 2
1166extern "C" void *__cxa_begin_catch(void *e) throw()
1167#else
1168extern "C" void *__cxa_begin_catch(void *e)
1169#endif
1170{
1171	// Decrement the uncaught exceptions count
1172	__cxa_eh_globals *globals = __cxa_get_globals();
1173	globals->uncaughtExceptions--;
1174	_Unwind_Exception *exceptionObject = (_Unwind_Exception*)e;
1175
1176	if (isCXXException(exceptionObject->exception_class))
1177	{
1178		__cxa_exception *ex =  exceptionFromPointer(exceptionObject);
1179
1180		if (ex->handlerCount == 0)
1181		{
1182			// Add this to the front of the list of exceptions being handled
1183			// and increment its handler count so that it won't be deleted
1184			// prematurely.
1185			ex->nextException = globals->caughtExceptions;
1186			globals->caughtExceptions = ex;
1187		}
1188
1189		if (ex->handlerCount < 0)
1190		{
1191			// Rethrown exception is catched before end of catch block.
1192			// Clear the rethrow flag (make value positive) - we are allowed
1193			// to delete this exception at the end of the catch block, as long
1194			// as it isn't thrown again later.
1195
1196			// Code pattern:
1197			//
1198			// try {
1199			//     throw x;
1200			// }
1201			// catch() {
1202			//     try {
1203			//         throw;
1204			//     }
1205			//     catch() {
1206			//         __cxa_begin_catch() <- we are here
1207			//     }
1208			// }
1209			ex->handlerCount = -ex->handlerCount + 1;
1210		}
1211		else
1212		{
1213			ex->handlerCount++;
1214		}
1215
1216		return ex->adjustedPtr;
1217	}
1218	// exceptionObject is the pointer to the _Unwind_Exception within the
1219	// __cxa_exception.  The throw object is after this
1220	return ((char*)exceptionObject + sizeof(_Unwind_Exception));
1221}
1222
1223
1224
1225/**
1226 * ABI function called when exiting a catch block.  This will free the current
1227 * exception if it is no longer referenced in other catch blocks.
1228 */
1229extern "C" void __cxa_end_catch()
1230{
1231	// We can call the fast version here because the slow version is called in
1232	// __cxa_throw(), which must have been called before we end a catch block
1233	__cxa_eh_globals *globals = __cxa_get_globals_fast();
1234	__cxa_exception *ex = globals->caughtExceptions;
1235
1236	assert(0 != ex && "Ending catch when no exception is on the stack!");
1237
1238	bool deleteException = true;
1239
1240	if (ex->handlerCount < 0)
1241	{
1242		// exception was rethrown. Exception should not be deleted even if
1243		// handlerCount become zero.
1244		// Code pattern:
1245		// try {
1246		//     throw x;
1247		// }
1248		// catch() {
1249		//     {
1250		//         throw;
1251		//     }
1252		//     cleanup {
1253		//         __cxa_end_catch();   <- we are here
1254		//     }
1255		// }
1256		//
1257
1258		ex->handlerCount++;
1259		deleteException = false;
1260	}
1261	else
1262	{
1263		ex->handlerCount--;
1264	}
1265
1266	if (ex->handlerCount == 0)
1267	{
1268		globals->caughtExceptions = ex->nextException;
1269		if (deleteException)
1270		{
1271			releaseException(ex);
1272		}
1273	}
1274}
1275
1276/**
1277 * ABI function.  Returns the type of the current exception.
1278 */
1279extern "C" std::type_info *__cxa_current_exception_type()
1280{
1281	__cxa_eh_globals *globals = __cxa_get_globals();
1282	__cxa_exception *ex = globals->caughtExceptions;
1283	return ex ? ex->exceptionType : 0;
1284}
1285
1286/**
1287 * ABI function, called when an exception specification is violated.
1288 *
1289 * This function does not return.
1290 */
1291extern "C" void __cxa_call_unexpected(void*exception)
1292{
1293	_Unwind_Exception *exceptionObject = (_Unwind_Exception*)exception;
1294	if (exceptionObject->exception_class == exception_class)
1295	{
1296		__cxa_exception *ex =  exceptionFromPointer(exceptionObject);
1297		if (ex->unexpectedHandler)
1298		{
1299			ex->unexpectedHandler();
1300			// Should not be reached.
1301			abort();
1302		}
1303	}
1304	std::unexpected();
1305	// Should not be reached.
1306	abort();
1307}
1308
1309/**
1310 * ABI function, returns the adjusted pointer to the exception object.
1311 */
1312extern "C" void *__cxa_get_exception_ptr(void *exceptionObject)
1313{
1314	return exceptionFromPointer(exceptionObject)->adjustedPtr;
1315}
1316
1317/**
1318 * As an extension, we provide the ability for the unexpected and terminate
1319 * handlers to be thread-local.  We default to the standards-compliant
1320 * behaviour where they are global.
1321 */
1322static bool thread_local_handlers = false;
1323
1324
1325namespace pathscale
1326{
1327	/**
1328	 * Sets whether unexpected and terminate handlers should be thread-local.
1329	 */
1330	void set_use_thread_local_handlers(bool flag) throw()
1331	{
1332		thread_local_handlers = flag;
1333	}
1334	/**
1335	 * Sets a thread-local unexpected handler.
1336	 */
1337	unexpected_handler set_unexpected(unexpected_handler f) throw()
1338	{
1339		static __cxa_thread_info *info = thread_info();
1340		unexpected_handler old = info->unexpectedHandler;
1341		info->unexpectedHandler = f;
1342		return old;
1343	}
1344	/**
1345	 * Sets a thread-local terminate handler.
1346	 */
1347	terminate_handler set_terminate(terminate_handler f) throw()
1348	{
1349		static __cxa_thread_info *info = thread_info();
1350		terminate_handler old = info->terminateHandler;
1351		info->terminateHandler = f;
1352		return old;
1353	}
1354}
1355
1356namespace std
1357{
1358	/**
1359	 * Sets the function that will be called when an exception specification is
1360	 * violated.
1361	 */
1362	unexpected_handler set_unexpected(unexpected_handler f) throw()
1363	{
1364		if (thread_local_handlers) { return pathscale::set_unexpected(f); }
1365
1366		return __sync_lock_test_and_set(&unexpectedHandler, f);
1367	}
1368	/**
1369	 * Sets the function that is called to terminate the program.
1370	 */
1371	terminate_handler set_terminate(terminate_handler f) throw()
1372	{
1373		if (thread_local_handlers) { return pathscale::set_terminate(f); }
1374		return __sync_lock_test_and_set(&terminateHandler, f);
1375	}
1376	/**
1377	 * Terminates the program, calling a custom terminate implementation if
1378	 * required.
1379	 */
1380	void terminate()
1381	{
1382		static __cxa_thread_info *info = thread_info_fast();
1383		if (0 != info && 0 != info->terminateHandler)
1384		{
1385			info->terminateHandler();
1386			// Should not be reached - a terminate handler is not expected to
1387			// return.
1388			abort();
1389		}
1390		terminateHandler();
1391	}
1392	/**
1393	 * Called when an unexpected exception is encountered (i.e. an exception
1394	 * violates an exception specification).  This calls abort() unless a
1395	 * custom handler has been set..
1396	 */
1397	void unexpected()
1398	{
1399		static __cxa_thread_info *info = thread_info_fast();
1400		if (0 != info && 0 != info->unexpectedHandler)
1401		{
1402			info->unexpectedHandler();
1403			// Should not be reached - a terminate handler is not expected to
1404			// return.
1405			abort();
1406		}
1407		unexpectedHandler();
1408	}
1409	/**
1410	 * Returns whether there are any exceptions currently being thrown that
1411	 * have not been caught.  This can occur inside a nested catch statement.
1412	 */
1413	bool uncaught_exception() throw()
1414	{
1415		__cxa_thread_info *info = thread_info();
1416		return info->globals.uncaughtExceptions != 0;
1417	}
1418	/**
1419	 * Returns the current unexpected handler.
1420	 */
1421	unexpected_handler get_unexpected() throw()
1422	{
1423		__cxa_thread_info *info = thread_info();
1424		if (info->unexpectedHandler)
1425		{
1426			return info->unexpectedHandler;
1427		}
1428		return unexpectedHandler;
1429	}
1430	/**
1431	 * Returns the current terminate handler.
1432	 */
1433	terminate_handler get_terminate() throw()
1434	{
1435		__cxa_thread_info *info = thread_info();
1436		if (info->terminateHandler)
1437		{
1438			return info->terminateHandler;
1439		}
1440		return terminateHandler;
1441	}
1442}
1443#ifdef __arm__
1444extern "C" _Unwind_Exception *__cxa_get_cleanup(void)
1445{
1446	__cxa_thread_info *info = thread_info_fast();
1447	_Unwind_Exception *exceptionObject = info->currentCleanup;
1448	if (isCXXException(exceptionObject->exception_class))
1449	{
1450		__cxa_exception *ex =  exceptionFromPointer(exceptionObject);
1451		ex->cleanupCount--;
1452		if (ex->cleanupCount == 0)
1453		{
1454			info->currentCleanup = ex->nextCleanup;
1455			ex->nextCleanup = 0;
1456		}
1457	}
1458	else
1459	{
1460		info->currentCleanup = 0;
1461	}
1462	return exceptionObject;
1463}
1464
1465asm (
1466".pushsection .text.__cxa_end_cleanup    \n"
1467".global __cxa_end_cleanup               \n"
1468".type __cxa_end_cleanup, \"function\"   \n"
1469"__cxa_end_cleanup:                      \n"
1470"	push {r1, r2, r3, r4}                \n"
1471"	bl __cxa_get_cleanup                 \n"
1472"	push {r1, r2, r3, r4}                \n"
1473"	b _Unwind_Resume                     \n"
1474"	bl abort                             \n"
1475".popsection                             \n"
1476);
1477#endif
1478