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