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