xref: /freebsd/contrib/libcxxrt/exception.cc (revision 7750ad47a9a7dbc83f87158464170c8640723293)
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 
42 using 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  */
49 static 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  */
73 static 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 
94 static 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 
104 extern "C" void __cxa_free_exception(void *thrown_exception);
105 extern "C" void __cxa_free_dependent_exception(void *thrown_exception);
106 extern "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  */
114 typedef 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  */
139 struct __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  */
166 struct __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 
193 namespace 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 
205 extern "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  */
214 static const uint64_t exception_class =
215 	EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\0');
216 /**
217  * Class used for dependent exceptions.
218  */
219 static 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  */
227 static const uint32_t abi_exception_class =
228 	GENERIC_EXCEPTION_CLASS('C', '+', '+', '\0');
229 
230 static bool isCXXException(uint64_t cls)
231 {
232 	return (cls == exception_class) || (cls == dependent_exception_class);
233 }
234 
235 static bool isDependentException(uint64_t cls)
236 {
237 	return cls == dependent_exception_class;
238 }
239 
240 static __cxa_exception *exceptionFromPointer(void *ex)
241 {
242 	return (__cxa_exception*)((char*)ex -
243 			offsetof(struct __cxa_exception, unwindHeader));
244 }
245 static __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 
252 namespace std
253 {
254 	// Forward declaration of standard library terminate() function used to
255 	// abort execution.
256 	void terminate(void);
257 }
258 
259 using namespace ABI_NAMESPACE;
260 
261 
262 
263 /** The global termination handler. */
264 static terminate_handler terminateHandler = abort;
265 /** The global unexpected exception handler. */
266 static unexpected_handler unexpectedHandler = std::terminate;
267 
268 /** Key used for thread-local data. */
269 static 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  */
276 static void exception_cleanup(_Unwind_Reason_Code reason,
277                               struct _Unwind_Exception *ex)
278 {
279 	__cxa_free_exception((void*)ex);
280 }
281 static 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  */
291 static 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  */
306 static 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  */
320 static 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  */
327 static bool fakeTLS;
328 /**
329  * Thread-local storage for a single-threaded program.
330  */
331 static __cxa_thread_info singleThreadInfo;
332 /**
333  * Initialise eh_key.
334  */
335 static 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  */
353 static __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  */
372 static __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  */
380 extern "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  */
388 extern "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  */
397 static char emergency_buffer[16384];
398 /**
399  * Flag indicating whether each buffer is allocated.
400  */
401 static bool buffer_allocated[16];
402 /**
403  * Lock used to protect emergency allocation.
404  */
405 static 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  */
410 static 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  */
418 static 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  */
470 static 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 
499 static 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 }
518 static 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  */
539 extern "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 
546 extern "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  */
561 extern "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 
581 static 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 
597 void __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  */
616 static _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  */
639 static 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 
687 static 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  */
715 extern "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 
732 extern "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 
749 extern "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 
760 extern "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 }
767 extern "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  */
781 extern "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  */
819 static 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  */
846 static bool check_type_signature(__cxa_exception *ex,
847                                  const std::type_info *type,
848                                  void *&adjustedPtr)
849 {
850 	void *exception_ptr = (void*)(ex+1);
851 	const std::type_info *ex_type = ex->exceptionType;
852 
853 	bool is_ptr = ex_type->__is_pointer_p();
854 	if (is_ptr)
855 	{
856 		exception_ptr = *(void**)exception_ptr;
857 	}
858 	// Always match a catchall, even with a foreign exception
859 	//
860 	// Note: A 0 here is a catchall, not a cleanup, so we return true to
861 	// indicate that we found a catch.
862 	if (0 == type)
863 	{
864 		if (ex)
865 		{
866 			adjustedPtr = exception_ptr;
867 		}
868 		return true;
869 	}
870 
871 	if (0 == ex) { return false; }
872 
873 	// If the types are the same, no casting is needed.
874 	if (*type == *ex_type)
875 	{
876 		adjustedPtr = exception_ptr;
877 		return true;
878 	}
879 
880 
881 	if (type->__do_catch(ex_type, &exception_ptr, 1))
882 	{
883 		adjustedPtr = exception_ptr;
884 		return true;
885 	}
886 
887 	return false;
888 }
889 /**
890  * Checks whether the exception matches the type specifiers in this action
891  * record.  If the exception only matches cleanups, then this returns false.
892  * If it matches a catch (including a catchall) then it returns true.
893  *
894  * The selector argument is used to return the selector that is passed in the
895  * second exception register when installing the context.
896  */
897 static handler_type check_action_record(_Unwind_Context *context,
898                                         dwarf_eh_lsda *lsda,
899                                         dw_eh_ptr_t action_record,
900                                         __cxa_exception *ex,
901                                         unsigned long *selector,
902                                         void *&adjustedPtr)
903 {
904 	if (!action_record) { return handler_cleanup; }
905 	handler_type found = handler_none;
906 	while (action_record)
907 	{
908 		int filter = read_sleb128(&action_record);
909 		dw_eh_ptr_t action_record_offset_base = action_record;
910 		int displacement = read_sleb128(&action_record);
911 		action_record = displacement ?
912 			action_record_offset_base + displacement : 0;
913 		// We only check handler types for C++ exceptions - foreign exceptions
914 		// are only allowed for cleanup.
915 		if (filter > 0 && 0 != ex)
916 		{
917 			std::type_info *handler_type = get_type_info_entry(context, lsda, filter);
918 			if (check_type_signature(ex, handler_type, adjustedPtr))
919 			{
920 				*selector = filter;
921 				return handler_catch;
922 			}
923 		}
924 		else if (filter < 0 && 0 != ex)
925 		{
926 			bool matched = false;
927 			*selector = filter;
928 #ifdef __arm__
929 			filter++;
930 			std::type_info *handler_type = get_type_info_entry(context, lsda, filter--);
931 			while (handler_type)
932 			{
933 				if (check_type_signature(ex, handler_type, adjustedPtr))
934 				{
935 					matched = true;
936 					break;
937 				}
938 				handler_type = get_type_info_entry(context, lsda, filter--);
939 			}
940 #else
941 			unsigned char *type_index = ((unsigned char*)lsda->type_table - filter - 1);
942 			while (*type_index)
943 			{
944 				std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++));
945 				// If the exception spec matches a permitted throw type for
946 				// this function, don't report a handler - we are allowed to
947 				// propagate this exception out.
948 				if (check_type_signature(ex, handler_type, adjustedPtr))
949 				{
950 					matched = true;
951 					break;
952 				}
953 			}
954 #endif
955 			if (matched) { continue; }
956 			// If we don't find an allowed exception spec, we need to install
957 			// the context for this action.  The landing pad will then call the
958 			// unexpected exception function.  Treat this as a catch
959 			return handler_catch;
960 		}
961 		else if (filter == 0)
962 		{
963 			*selector = filter;
964 			found = handler_cleanup;
965 		}
966 	}
967 	return found;
968 }
969 
970 static void pushCleanupException(_Unwind_Exception *exceptionObject,
971                                  __cxa_exception *ex)
972 {
973 #ifdef __arm__
974 	__cxa_thread_info *info = thread_info_fast();
975 	if (ex)
976 	{
977 		ex->cleanupCount++;
978 		if (ex->cleanupCount > 1)
979 		{
980 			assert(exceptionObject == info->currentCleanup);
981 			return;
982 		}
983 		ex->nextCleanup = info->currentCleanup;
984 	}
985 	info->currentCleanup = exceptionObject;
986 #endif
987 }
988 
989 /**
990  * The exception personality function.  This is referenced in the unwinding
991  * DWARF metadata and is called by the unwind library for each C++ stack frame
992  * containing catch or cleanup code.
993  */
994 extern "C"
995 BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0)
996 	// This personality function is for version 1 of the ABI.  If you use it
997 	// with a future version of the ABI, it won't know what to do, so it
998 	// reports a fatal error and give up before it breaks anything.
999 	if (1 != version)
1000 	{
1001 		return _URC_FATAL_PHASE1_ERROR;
1002 	}
1003 	__cxa_exception *ex = 0;
1004 	__cxa_exception *realEx = 0;
1005 
1006 	// If this exception is throw by something else then we can't make any
1007 	// assumptions about its layout beyond the fields declared in
1008 	// _Unwind_Exception.
1009 	bool foreignException = !isCXXException(exceptionClass);
1010 
1011 	// If this isn't a foreign exception, then we have a C++ exception structure
1012 	if (!foreignException)
1013 	{
1014 		ex = exceptionFromPointer(exceptionObject);
1015 		realEx = realExceptionFromException(ex);
1016 	}
1017 
1018 	unsigned char *lsda_addr =
1019 		(unsigned char*)_Unwind_GetLanguageSpecificData(context);
1020 
1021 	// No LSDA implies no landing pads - try the next frame
1022 	if (0 == lsda_addr) { return continueUnwinding(exceptionObject, context); }
1023 
1024 	// These two variables define how the exception will be handled.
1025 	dwarf_eh_action action = {0};
1026 	unsigned long selector = 0;
1027 
1028 	// During the search phase, we do a complete lookup.  If we return
1029 	// _URC_HANDLER_FOUND, then the phase 2 unwind will call this function with
1030 	// a _UA_HANDLER_FRAME action, telling us to install the handler frame.  If
1031 	// we return _URC_CONTINUE_UNWIND, we may be called again later with a
1032 	// _UA_CLEANUP_PHASE action for this frame.
1033 	//
1034 	// The point of the two-stage unwind allows us to entirely avoid any stack
1035 	// unwinding if there is no handler.  If there are just cleanups found,
1036 	// then we can just panic call an abort function.
1037 	//
1038 	// Matching a handler is much more expensive than matching a cleanup,
1039 	// because we don't need to bother doing type comparisons (or looking at
1040 	// the type table at all) for a cleanup.  This means that there is no need
1041 	// to cache the result of finding a cleanup, because it's (quite) quick to
1042 	// look it up again from the action table.
1043 	if (actions & _UA_SEARCH_PHASE)
1044 	{
1045 		struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1046 
1047 		if (!dwarf_eh_find_callsite(context, &lsda, &action))
1048 		{
1049 			// EH range not found. This happens if exception is thrown and not
1050 			// caught inside a cleanup (destructor).  We should call
1051 			// terminate() in this case.  The catchTemp (landing pad) field of
1052 			// exception object will contain null when personality function is
1053 			// called with _UA_HANDLER_FRAME action for phase 2 unwinding.
1054 			return _URC_HANDLER_FOUND;
1055 		}
1056 
1057 		handler_type found_handler = check_action_record(context, &lsda,
1058 				action.action_record, realEx, &selector, ex->adjustedPtr);
1059 		// If there's no action record, we've only found a cleanup, so keep
1060 		// searching for something real
1061 		if (found_handler == handler_catch)
1062 		{
1063 			// Cache the results for the phase 2 unwind, if we found a handler
1064 			// and this is not a foreign exception.
1065 			if (ex)
1066 			{
1067 				saveLandingPad(context, exceptionObject, ex, selector, action.landing_pad);
1068 				ex->languageSpecificData = (const char*)lsda_addr;
1069 				ex->actionRecord = (const char*)action.action_record;
1070 				// ex->adjustedPtr is set when finding the action record.
1071 			}
1072 			return _URC_HANDLER_FOUND;
1073 		}
1074 		return continueUnwinding(exceptionObject, context);
1075 	}
1076 
1077 
1078 	// If this is a foreign exception, we didn't have anywhere to cache the
1079 	// lookup stuff, so we need to do it again.  If this is either a forced
1080 	// unwind, a foreign exception, or a cleanup, then we just install the
1081 	// context for a cleanup.
1082 	if (!(actions & _UA_HANDLER_FRAME))
1083 	{
1084 		// cleanup
1085 		struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1086 		dwarf_eh_find_callsite(context, &lsda, &action);
1087 		if (0 == action.landing_pad) { return continueUnwinding(exceptionObject, context); }
1088 		handler_type found_handler = check_action_record(context, &lsda,
1089 				action.action_record, realEx, &selector, ex->adjustedPtr);
1090 		// Ignore handlers this time.
1091 		if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); }
1092 		pushCleanupException(exceptionObject, ex);
1093 	}
1094 	else if (foreignException)
1095 	{
1096 		struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
1097 		dwarf_eh_find_callsite(context, &lsda, &action);
1098 		check_action_record(context, &lsda, action.action_record, realEx,
1099 				&selector, ex->adjustedPtr);
1100 	}
1101 	else if (ex->catchTemp == 0)
1102 	{
1103 		// Uncaught exception in cleanup, calling terminate
1104 		std::terminate();
1105 	}
1106 	else
1107 	{
1108 		// Restore the saved info if we saved some last time.
1109 		loadLandingPad(context, exceptionObject, ex, &selector, &action.landing_pad);
1110 		ex->catchTemp = 0;
1111 		ex->handlerSwitchValue = 0;
1112 	}
1113 
1114 
1115 	_Unwind_SetIP(context, (unsigned long)action.landing_pad);
1116 	_Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
1117 	              (unsigned long)exceptionObject);
1118 	_Unwind_SetGR(context, __builtin_eh_return_data_regno(1), selector);
1119 
1120 	return _URC_INSTALL_CONTEXT;
1121 }
1122 
1123 /**
1124  * ABI function called when entering a catch statement.  The argument is the
1125  * pointer passed out of the personality function.  This is always the start of
1126  * the _Unwind_Exception object.  The return value for this function is the
1127  * pointer to the caught exception, which is either the adjusted pointer (for
1128  * C++ exceptions) of the unadjusted pointer (for foreign exceptions).
1129  */
1130 #if __GNUC__ > 3 && __GNUC_MINOR__ > 2
1131 extern "C" void *__cxa_begin_catch(void *e) throw()
1132 #else
1133 extern "C" void *__cxa_begin_catch(void *e)
1134 #endif
1135 {
1136 	// Decrement the uncaught exceptions count
1137 	__cxa_eh_globals *globals = __cxa_get_globals();
1138 	globals->uncaughtExceptions--;
1139 	_Unwind_Exception *exceptionObject = (_Unwind_Exception*)e;
1140 
1141 	if (isCXXException(exceptionObject->exception_class))
1142 	{
1143 		__cxa_exception *ex =  exceptionFromPointer(exceptionObject);
1144 
1145 		if (ex->handlerCount == 0)
1146 		{
1147 			// Add this to the front of the list of exceptions being handled
1148 			// and increment its handler count so that it won't be deleted
1149 			// prematurely.
1150 			ex->nextException = globals->caughtExceptions;
1151 			globals->caughtExceptions = ex;
1152 		}
1153 
1154 		if (ex->handlerCount < 0)
1155 		{
1156 			// Rethrown exception is catched before end of catch block.
1157 			// Clear the rethrow flag (make value positive) - we are allowed
1158 			// to delete this exception at the end of the catch block, as long
1159 			// as it isn't thrown again later.
1160 
1161 			// Code pattern:
1162 			//
1163 			// try {
1164 			//     throw x;
1165 			// }
1166 			// catch() {
1167 			//     try {
1168 			//         throw;
1169 			//     }
1170 			//     catch() {
1171 			//         __cxa_begin_catch() <- we are here
1172 			//     }
1173 			// }
1174 			ex->handlerCount = -ex->handlerCount + 1;
1175 		}
1176 		else
1177 		{
1178 			ex->handlerCount++;
1179 		}
1180 
1181 		return ex->adjustedPtr;
1182 	}
1183 	// exceptionObject is the pointer to the _Unwind_Exception within the
1184 	// __cxa_exception.  The throw object is after this
1185 	return ((char*)exceptionObject + sizeof(_Unwind_Exception));
1186 }
1187 
1188 
1189 
1190 /**
1191  * ABI function called when exiting a catch block.  This will free the current
1192  * exception if it is no longer referenced in other catch blocks.
1193  */
1194 extern "C" void __cxa_end_catch()
1195 {
1196 	// We can call the fast version here because the slow version is called in
1197 	// __cxa_throw(), which must have been called before we end a catch block
1198 	__cxa_eh_globals *globals = __cxa_get_globals_fast();
1199 	__cxa_exception *ex = globals->caughtExceptions;
1200 
1201 	assert(0 != ex && "Ending catch when no exception is on the stack!");
1202 
1203 	bool deleteException = true;
1204 
1205 	if (ex->handlerCount < 0)
1206 	{
1207 		// exception was rethrown. Exception should not be deleted even if
1208 		// handlerCount become zero.
1209 		// Code pattern:
1210 		// try {
1211 		//     throw x;
1212 		// }
1213 		// catch() {
1214 		//     {
1215 		//         throw;
1216 		//     }
1217 		//     cleanup {
1218 		//         __cxa_end_catch();   <- we are here
1219 		//     }
1220 		// }
1221 		//
1222 
1223 		ex->handlerCount++;
1224 		deleteException = false;
1225 	}
1226 	else
1227 	{
1228 		ex->handlerCount--;
1229 	}
1230 
1231 	if (ex->handlerCount == 0)
1232 	{
1233 		globals->caughtExceptions = ex->nextException;
1234 		if (deleteException)
1235 		{
1236 			releaseException(ex);
1237 		}
1238 	}
1239 }
1240 
1241 /**
1242  * ABI function.  Returns the type of the current exception.
1243  */
1244 extern "C" std::type_info *__cxa_current_exception_type()
1245 {
1246 	__cxa_eh_globals *globals = __cxa_get_globals();
1247 	__cxa_exception *ex = globals->caughtExceptions;
1248 	return ex ? ex->exceptionType : 0;
1249 }
1250 
1251 /**
1252  * ABI function, called when an exception specification is violated.
1253  *
1254  * This function does not return.
1255  */
1256 extern "C" void __cxa_call_unexpected(void*exception)
1257 {
1258 	_Unwind_Exception *exceptionObject = (_Unwind_Exception*)exception;
1259 	if (exceptionObject->exception_class == exception_class)
1260 	{
1261 		__cxa_exception *ex =  exceptionFromPointer(exceptionObject);
1262 		if (ex->unexpectedHandler)
1263 		{
1264 			ex->unexpectedHandler();
1265 			// Should not be reached.
1266 			abort();
1267 		}
1268 	}
1269 	std::unexpected();
1270 	// Should not be reached.
1271 	abort();
1272 }
1273 
1274 /**
1275  * ABI function, returns the adjusted pointer to the exception object.
1276  */
1277 extern "C" void *__cxa_get_exception_ptr(void *exceptionObject)
1278 {
1279 	return exceptionFromPointer(exceptionObject)->adjustedPtr;
1280 }
1281 
1282 /**
1283  * As an extension, we provide the ability for the unexpected and terminate
1284  * handlers to be thread-local.  We default to the standards-compliant
1285  * behaviour where they are global.
1286  */
1287 static bool thread_local_handlers = false;
1288 
1289 
1290 namespace pathscale
1291 {
1292 	/**
1293 	 * Sets whether unexpected and terminate handlers should be thread-local.
1294 	 */
1295 	void set_use_thread_local_handlers(bool flag) throw()
1296 	{
1297 		thread_local_handlers = flag;
1298 	}
1299 	/**
1300 	 * Sets a thread-local unexpected handler.
1301 	 */
1302 	unexpected_handler set_unexpected(unexpected_handler f) throw()
1303 	{
1304 		static __cxa_thread_info *info = thread_info();
1305 		unexpected_handler old = info->unexpectedHandler;
1306 		info->unexpectedHandler = f;
1307 		return old;
1308 	}
1309 	/**
1310 	 * Sets a thread-local terminate handler.
1311 	 */
1312 	terminate_handler set_terminate(terminate_handler f) throw()
1313 	{
1314 		static __cxa_thread_info *info = thread_info();
1315 		terminate_handler old = info->terminateHandler;
1316 		info->terminateHandler = f;
1317 		return old;
1318 	}
1319 }
1320 
1321 namespace std
1322 {
1323 	/**
1324 	 * Sets the function that will be called when an exception specification is
1325 	 * violated.
1326 	 */
1327 	unexpected_handler set_unexpected(unexpected_handler f) throw()
1328 	{
1329 		if (thread_local_handlers) { return pathscale::set_unexpected(f); }
1330 
1331 		return __sync_lock_test_and_set(&unexpectedHandler, f);
1332 	}
1333 	/**
1334 	 * Sets the function that is called to terminate the program.
1335 	 */
1336 	terminate_handler set_terminate(terminate_handler f) throw()
1337 	{
1338 		if (thread_local_handlers) { return pathscale::set_terminate(f); }
1339 		return __sync_lock_test_and_set(&terminateHandler, f);
1340 	}
1341 	/**
1342 	 * Terminates the program, calling a custom terminate implementation if
1343 	 * required.
1344 	 */
1345 	void terminate()
1346 	{
1347 		static __cxa_thread_info *info = thread_info_fast();
1348 		if (0 != info && 0 != info->terminateHandler)
1349 		{
1350 			info->terminateHandler();
1351 			// Should not be reached - a terminate handler is not expected to
1352 			// return.
1353 			abort();
1354 		}
1355 		terminateHandler();
1356 	}
1357 	/**
1358 	 * Called when an unexpected exception is encountered (i.e. an exception
1359 	 * violates an exception specification).  This calls abort() unless a
1360 	 * custom handler has been set..
1361 	 */
1362 	void unexpected()
1363 	{
1364 		static __cxa_thread_info *info = thread_info_fast();
1365 		if (0 != info && 0 != info->unexpectedHandler)
1366 		{
1367 			info->unexpectedHandler();
1368 			// Should not be reached - a terminate handler is not expected to
1369 			// return.
1370 			abort();
1371 		}
1372 		unexpectedHandler();
1373 	}
1374 	/**
1375 	 * Returns whether there are any exceptions currently being thrown that
1376 	 * have not been caught.  This can occur inside a nested catch statement.
1377 	 */
1378 	bool uncaught_exception() throw()
1379 	{
1380 		__cxa_thread_info *info = thread_info();
1381 		return info->globals.uncaughtExceptions != 0;
1382 	}
1383 	/**
1384 	 * Returns the current unexpected handler.
1385 	 */
1386 	unexpected_handler get_unexpected() throw()
1387 	{
1388 		__cxa_thread_info *info = thread_info();
1389 		if (info->unexpectedHandler)
1390 		{
1391 			return info->unexpectedHandler;
1392 		}
1393 		return unexpectedHandler;
1394 	}
1395 	/**
1396 	 * Returns the current terminate handler.
1397 	 */
1398 	terminate_handler get_terminate() throw()
1399 	{
1400 		__cxa_thread_info *info = thread_info();
1401 		if (info->terminateHandler)
1402 		{
1403 			return info->terminateHandler;
1404 		}
1405 		return terminateHandler;
1406 	}
1407 }
1408 #ifdef __arm__
1409 extern "C" _Unwind_Exception *__cxa_get_cleanup(void)
1410 {
1411 	__cxa_thread_info *info = thread_info_fast();
1412 	_Unwind_Exception *exceptionObject = info->currentCleanup;
1413 	if (isCXXException(exceptionObject->exception_class))
1414 	{
1415 		__cxa_exception *ex =  exceptionFromPointer(exceptionObject);
1416 		ex->cleanupCount--;
1417 		if (ex->cleanupCount == 0)
1418 		{
1419 			info->currentCleanup = ex->nextCleanup;
1420 			ex->nextCleanup = 0;
1421 		}
1422 	}
1423 	else
1424 	{
1425 		info->currentCleanup = 0;
1426 	}
1427 	return exceptionObject;
1428 }
1429 
1430 asm (
1431 ".pushsection .text.__cxa_end_cleanup    \n"
1432 ".global __cxa_end_cleanup               \n"
1433 ".type __cxa_end_cleanup, \"function\"   \n"
1434 "__cxa_end_cleanup:                      \n"
1435 "	push {r1, r2, r3, r4}                \n"
1436 "	bl __cxa_get_cleanup                 \n"
1437 "	push {r1, r2, r3, r4}                \n"
1438 "	b _Unwind_Resume                     \n"
1439 "	bl abort                             \n"
1440 ".popsection                             \n"
1441 );
1442 #endif
1443