xref: /freebsd/contrib/libcxxrt/exception.cc (revision 2dccd21949f26b1bdf5e7cf258b760fffd3bf259)
194e3ee44SDavid Chisnall /*
294e3ee44SDavid Chisnall  * Copyright 2010-2011 PathScale, Inc. All rights reserved.
3bfffb66eSDimitry Andric  * Copyright 2021 David Chisnall. All rights reserved.
494e3ee44SDavid Chisnall  *
594e3ee44SDavid Chisnall  * Redistribution and use in source and binary forms, with or without
694e3ee44SDavid Chisnall  * modification, are permitted provided that the following conditions are met:
794e3ee44SDavid Chisnall  *
894e3ee44SDavid Chisnall  * 1. Redistributions of source code must retain the above copyright notice,
994e3ee44SDavid Chisnall  *    this list of conditions and the following disclaimer.
1094e3ee44SDavid Chisnall  *
1194e3ee44SDavid Chisnall  * 2. Redistributions in binary form must reproduce the above copyright notice,
1294e3ee44SDavid Chisnall  *    this list of conditions and the following disclaimer in the documentation
1394e3ee44SDavid Chisnall  *    and/or other materials provided with the distribution.
1494e3ee44SDavid Chisnall  *
1594e3ee44SDavid Chisnall  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
1694e3ee44SDavid Chisnall  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1794e3ee44SDavid Chisnall  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1894e3ee44SDavid Chisnall  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
1994e3ee44SDavid Chisnall  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2094e3ee44SDavid Chisnall  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2194e3ee44SDavid Chisnall  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2294e3ee44SDavid Chisnall  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2394e3ee44SDavid Chisnall  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2494e3ee44SDavid Chisnall  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2594e3ee44SDavid Chisnall  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2694e3ee44SDavid Chisnall  */
2794e3ee44SDavid Chisnall 
287a984708SDavid Chisnall #include <stdlib.h>
297a984708SDavid Chisnall #include <dlfcn.h>
307a984708SDavid Chisnall #include <stdio.h>
317a984708SDavid Chisnall #include <string.h>
327a984708SDavid Chisnall #include <stdint.h>
337a984708SDavid Chisnall #include <pthread.h>
347a984708SDavid Chisnall #include "typeinfo.h"
357a984708SDavid Chisnall #include "dwarf_eh.h"
36f7cb1657SDavid Chisnall #include "atomic.h"
377a984708SDavid Chisnall #include "cxxabi.h"
387a984708SDavid Chisnall 
39db47c4bfSDavid Chisnall #pragma weak pthread_key_create
40db47c4bfSDavid Chisnall #pragma weak pthread_setspecific
41db47c4bfSDavid Chisnall #pragma weak pthread_getspecific
42db47c4bfSDavid Chisnall #pragma weak pthread_once
434bab9fd9SDavid Chisnall #ifdef LIBCXXRT_WEAK_LOCKS
444bab9fd9SDavid Chisnall #pragma weak pthread_mutex_lock
454bab9fd9SDavid Chisnall #define pthread_mutex_lock(mtx) do {\
464bab9fd9SDavid Chisnall 	if (pthread_mutex_lock) pthread_mutex_lock(mtx);\
474bab9fd9SDavid Chisnall 	} while(0)
484bab9fd9SDavid Chisnall #pragma weak pthread_mutex_unlock
494bab9fd9SDavid Chisnall #define pthread_mutex_unlock(mtx) do {\
504bab9fd9SDavid Chisnall 	if (pthread_mutex_unlock) pthread_mutex_unlock(mtx);\
514bab9fd9SDavid Chisnall 	} while(0)
524bab9fd9SDavid Chisnall #pragma weak pthread_cond_signal
534bab9fd9SDavid Chisnall #define pthread_cond_signal(cv) do {\
544bab9fd9SDavid Chisnall 	if (pthread_cond_signal) pthread_cond_signal(cv);\
554bab9fd9SDavid Chisnall 	} while(0)
564bab9fd9SDavid Chisnall #pragma weak pthread_cond_wait
574bab9fd9SDavid Chisnall #define pthread_cond_wait(cv, mtx) do {\
584bab9fd9SDavid Chisnall 	if (pthread_cond_wait) pthread_cond_wait(cv, mtx);\
594bab9fd9SDavid Chisnall 	} while(0)
604bab9fd9SDavid Chisnall #endif
61db47c4bfSDavid Chisnall 
627a984708SDavid Chisnall using namespace ABI_NAMESPACE;
637a984708SDavid Chisnall 
647a984708SDavid Chisnall /**
657a984708SDavid Chisnall  * Saves the result of the landing pad that we have found.  For ARM, this is
667a984708SDavid Chisnall  * stored in the generic unwind structure, while on other platforms it is
677a984708SDavid Chisnall  * stored in the C++ exception.
687a984708SDavid Chisnall  */
saveLandingPad(struct _Unwind_Context * context,struct _Unwind_Exception * ucb,struct __cxa_exception * ex,int selector,dw_eh_ptr_t landingPad)697a984708SDavid Chisnall static void saveLandingPad(struct _Unwind_Context *context,
707a984708SDavid Chisnall                            struct _Unwind_Exception *ucb,
717a984708SDavid Chisnall                            struct __cxa_exception *ex,
727a984708SDavid Chisnall                            int selector,
737a984708SDavid Chisnall                            dw_eh_ptr_t landingPad)
747a984708SDavid Chisnall {
75f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
767a984708SDavid Chisnall 	// On ARM, we store the saved exception in the generic part of the structure
777a984708SDavid Chisnall 	ucb->barrier_cache.sp = _Unwind_GetGR(context, 13);
78f2dc4184SDimitry Andric 	ucb->barrier_cache.bitpattern[1] = static_cast<uint32_t>(selector);
79f2dc4184SDimitry Andric 	ucb->barrier_cache.bitpattern[3] = reinterpret_cast<uint32_t>(landingPad);
807a984708SDavid Chisnall #endif
817a984708SDavid Chisnall 	// Cache the results for the phase 2 unwind, if we found a handler
827a984708SDavid Chisnall 	// and this is not a foreign exception.
837a984708SDavid Chisnall 	if (ex)
847a984708SDavid Chisnall 	{
857a984708SDavid Chisnall 		ex->handlerSwitchValue = selector;
867a984708SDavid Chisnall 		ex->catchTemp = landingPad;
877a984708SDavid Chisnall 	}
887a984708SDavid Chisnall }
897a984708SDavid Chisnall 
907a984708SDavid Chisnall /**
917a984708SDavid Chisnall  * Loads the saved landing pad.  Returns 1 on success, 0 on failure.
927a984708SDavid Chisnall  */
loadLandingPad(struct _Unwind_Context * context,struct _Unwind_Exception * ucb,struct __cxa_exception * ex,unsigned long * selector,dw_eh_ptr_t * landingPad)937a984708SDavid Chisnall static int loadLandingPad(struct _Unwind_Context *context,
947a984708SDavid Chisnall                           struct _Unwind_Exception *ucb,
957a984708SDavid Chisnall                           struct __cxa_exception *ex,
967a984708SDavid Chisnall                           unsigned long *selector,
977a984708SDavid Chisnall                           dw_eh_ptr_t *landingPad)
987a984708SDavid Chisnall {
99f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
1007a984708SDavid Chisnall 	*selector = ucb->barrier_cache.bitpattern[1];
101f2dc4184SDimitry Andric 	*landingPad = reinterpret_cast<dw_eh_ptr_t>(ucb->barrier_cache.bitpattern[3]);
1027a984708SDavid Chisnall 	return 1;
1037a984708SDavid Chisnall #else
1047a984708SDavid Chisnall 	if (ex)
1057a984708SDavid Chisnall 	{
1067a984708SDavid Chisnall 		*selector = ex->handlerSwitchValue;
107f2dc4184SDimitry Andric 		*landingPad = reinterpret_cast<dw_eh_ptr_t>(ex->catchTemp);
1087a984708SDavid Chisnall 		return 0;
1097a984708SDavid Chisnall 	}
1107a984708SDavid Chisnall 	return 0;
1117a984708SDavid Chisnall #endif
1127a984708SDavid Chisnall }
1137a984708SDavid Chisnall 
continueUnwinding(struct _Unwind_Exception * ex,struct _Unwind_Context * context)1147a984708SDavid Chisnall static inline _Unwind_Reason_Code continueUnwinding(struct _Unwind_Exception *ex,
1157a984708SDavid Chisnall                                                     struct _Unwind_Context *context)
1167a984708SDavid Chisnall {
117f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
1187a984708SDavid Chisnall 	if (__gnu_unwind_frame(ex, context) != _URC_OK) { return _URC_FAILURE; }
1197a984708SDavid Chisnall #endif
1207a984708SDavid Chisnall 	return _URC_CONTINUE_UNWIND;
1217a984708SDavid Chisnall }
1227a984708SDavid Chisnall 
1237a984708SDavid Chisnall 
12413da1af1SEd Maste extern "C" void __cxa_free_exception(void *thrown_exception) _LIBCXXRT_NOEXCEPT;
1257a984708SDavid Chisnall extern "C" void __cxa_free_dependent_exception(void *thrown_exception);
1267a984708SDavid Chisnall extern "C" void* __dynamic_cast(const void *sub,
1277a984708SDavid Chisnall                                 const __class_type_info *src,
1287a984708SDavid Chisnall                                 const __class_type_info *dst,
1297a984708SDavid Chisnall                                 ptrdiff_t src2dst_offset);
1307a984708SDavid Chisnall 
1317a984708SDavid Chisnall /**
1327a984708SDavid Chisnall  * The type of a handler that has been found.
1337a984708SDavid Chisnall  */
1347a984708SDavid Chisnall typedef enum
1357a984708SDavid Chisnall {
1367a984708SDavid Chisnall 	/** No handler. */
1377a984708SDavid Chisnall 	handler_none,
1387a984708SDavid Chisnall 	/**
1397a984708SDavid Chisnall 	 * A cleanup - the exception will propagate through this frame, but code
1407a984708SDavid Chisnall 	 * must be run when this happens.
1417a984708SDavid Chisnall 	 */
1427a984708SDavid Chisnall 	handler_cleanup,
1437a984708SDavid Chisnall 	/**
1447a984708SDavid Chisnall 	 * A catch statement.  The exception will not propagate past this frame
1457a984708SDavid Chisnall 	 * (without an explicit rethrow).
1467a984708SDavid Chisnall 	 */
1477a984708SDavid Chisnall 	handler_catch
1487a984708SDavid Chisnall } handler_type;
1497a984708SDavid Chisnall 
1507a984708SDavid Chisnall /**
1517a984708SDavid Chisnall  * Per-thread info required by the runtime.  We store a single structure
1527a984708SDavid Chisnall  * pointer in thread-local storage, because this tends to be a scarce resource
1537a984708SDavid Chisnall  * and it's impolite to steal all of it and not leave any for the rest of the
1547a984708SDavid Chisnall  * program.
1557a984708SDavid Chisnall  *
1567a984708SDavid Chisnall  * Instances of this structure are allocated lazily - at most one per thread -
1577a984708SDavid Chisnall  * and are destroyed on thread termination.
1587a984708SDavid Chisnall  */
1597a984708SDavid Chisnall struct __cxa_thread_info
1607a984708SDavid Chisnall {
1617a984708SDavid Chisnall 	/** The termination handler for this thread. */
1627a984708SDavid Chisnall 	terminate_handler terminateHandler;
1637a984708SDavid Chisnall 	/** The unexpected exception handler for this thread. */
1647a984708SDavid Chisnall 	unexpected_handler unexpectedHandler;
165ecf41062SDimitry Andric #ifndef LIBCXXRT_NO_EMERGENCY_MALLOC
1667a984708SDavid Chisnall 	/**
1677a984708SDavid Chisnall 	 * The number of emergency buffers held by this thread.  This is 0 in
1687a984708SDavid Chisnall 	 * normal operation - the emergency buffers are only used when malloc()
1697a984708SDavid Chisnall 	 * fails to return memory for allocating an exception.  Threads are not
1707a984708SDavid Chisnall 	 * permitted to hold more than 4 emergency buffers (as per recommendation
1717a984708SDavid Chisnall 	 * in ABI spec [3.3.1]).
1727a984708SDavid Chisnall 	 */
1737a984708SDavid Chisnall 	int emergencyBuffersHeld;
174ecf41062SDimitry Andric #endif
1757a984708SDavid Chisnall 	/**
1767a984708SDavid Chisnall 	 * The exception currently running in a cleanup.
1777a984708SDavid Chisnall 	 */
1787a984708SDavid Chisnall 	_Unwind_Exception *currentCleanup;
1797a984708SDavid Chisnall 	/**
180f7cb1657SDavid Chisnall 	 * Our state with respect to foreign exceptions.  Usually none, set to
181f7cb1657SDavid Chisnall 	 * caught if we have just caught an exception and rethrown if we are
182f7cb1657SDavid Chisnall 	 * rethrowing it.
183f7cb1657SDavid Chisnall 	 */
184f7cb1657SDavid Chisnall 	enum
185f7cb1657SDavid Chisnall 	{
186f7cb1657SDavid Chisnall 		none,
187f7cb1657SDavid Chisnall 		caught,
188f7cb1657SDavid Chisnall 		rethrown
189f7cb1657SDavid Chisnall 	} foreign_exception_state;
190f7cb1657SDavid Chisnall 	/**
1917a984708SDavid Chisnall 	 * The public part of this structure, accessible from outside of this
1927a984708SDavid Chisnall 	 * module.
1937a984708SDavid Chisnall 	 */
1947a984708SDavid Chisnall 	__cxa_eh_globals globals;
1957a984708SDavid Chisnall };
1967a984708SDavid Chisnall /**
1977a984708SDavid Chisnall  * Dependent exception.  This
1987a984708SDavid Chisnall  */
1997a984708SDavid Chisnall struct __cxa_dependent_exception
2007a984708SDavid Chisnall {
20113da1af1SEd Maste #ifdef __LP64__
202c40e4349SEd Maste 	void *reserve;
2037a984708SDavid Chisnall 	void *primaryException;
2047a984708SDavid Chisnall #endif
2057a984708SDavid Chisnall 	std::type_info *exceptionType;
2067a984708SDavid Chisnall 	void (*exceptionDestructor) (void *);
2077a984708SDavid Chisnall 	unexpected_handler unexpectedHandler;
2087a984708SDavid Chisnall 	terminate_handler terminateHandler;
2097a984708SDavid Chisnall 	__cxa_exception *nextException;
2107a984708SDavid Chisnall 	int handlerCount;
211f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
2127a984708SDavid Chisnall 	_Unwind_Exception *nextCleanup;
2137a984708SDavid Chisnall 	int cleanupCount;
2147a984708SDavid Chisnall #endif
2157a984708SDavid Chisnall 	int handlerSwitchValue;
2167a984708SDavid Chisnall 	const char *actionRecord;
2177a984708SDavid Chisnall 	const char *languageSpecificData;
2187a984708SDavid Chisnall 	void *catchTemp;
2197a984708SDavid Chisnall 	void *adjustedPtr;
22013da1af1SEd Maste #ifndef __LP64__
2217a984708SDavid Chisnall 	void *primaryException;
2227a984708SDavid Chisnall #endif
2237a984708SDavid Chisnall 	_Unwind_Exception unwindHeader;
2247a984708SDavid Chisnall };
225c40e4349SEd Maste static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
226c40e4349SEd Maste     "__cxa_exception and __cxa_dependent_exception should have the same size");
227c40e4349SEd Maste static_assert(offsetof(__cxa_exception, referenceCount) ==
228c40e4349SEd Maste     offsetof(__cxa_dependent_exception, primaryException),
229c40e4349SEd Maste     "referenceCount and primaryException should have the same offset");
230c40e4349SEd Maste static_assert(offsetof(__cxa_exception, unwindHeader) ==
231c40e4349SEd Maste     offsetof(__cxa_dependent_exception, unwindHeader),
232c40e4349SEd Maste     "unwindHeader fields should have the same offset");
233c40e4349SEd Maste static_assert(offsetof(__cxa_dependent_exception, unwindHeader) ==
234c40e4349SEd Maste     offsetof(__cxa_dependent_exception, adjustedPtr) + 8,
235c40e4349SEd Maste     "there should be no padding before unwindHeader");
2367a984708SDavid Chisnall 
2377a984708SDavid Chisnall 
2387a984708SDavid Chisnall namespace std
2397a984708SDavid Chisnall {
240*2dccd219SEd Maste 	[[noreturn]] void unexpected();
2417a984708SDavid Chisnall 	class exception
2427a984708SDavid Chisnall 	{
2437a984708SDavid Chisnall 		public:
24413da1af1SEd Maste 			virtual ~exception() _LIBCXXRT_NOEXCEPT;
24513da1af1SEd Maste 			virtual const char* what() const _LIBCXXRT_NOEXCEPT;
2467a984708SDavid Chisnall 	};
2477a984708SDavid Chisnall 
2487a984708SDavid Chisnall }
2497a984708SDavid Chisnall 
2507a984708SDavid Chisnall /**
2517a984708SDavid Chisnall  * Class of exceptions to distinguish between this and other exception types.
2527a984708SDavid Chisnall  *
2537a984708SDavid Chisnall  * The first four characters are the vendor ID.  Currently, we use GNUC,
2547a984708SDavid Chisnall  * because we aim for ABI-compatibility with the GNU implementation, and
2557a984708SDavid Chisnall  * various checks may test for equality of the class, which is incorrect.
2567a984708SDavid Chisnall  */
2577a984708SDavid Chisnall static const uint64_t exception_class =
2587a984708SDavid Chisnall 	EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\0');
2597a984708SDavid Chisnall /**
2607a984708SDavid Chisnall  * Class used for dependent exceptions.
2617a984708SDavid Chisnall  */
2627a984708SDavid Chisnall static const uint64_t dependent_exception_class =
2637a984708SDavid Chisnall 	EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\x01');
2647a984708SDavid Chisnall /**
2657a984708SDavid Chisnall  * The low four bytes of the exception class, indicating that we conform to the
2667a984708SDavid Chisnall  * Itanium C++ ABI.  This is currently unused, but should be used in the future
2677a984708SDavid Chisnall  * if we change our exception class, to allow this library and libsupc++ to be
2687a984708SDavid Chisnall  * linked to the same executable and both to interoperate.
2697a984708SDavid Chisnall  */
2707a984708SDavid Chisnall static const uint32_t abi_exception_class =
2717a984708SDavid Chisnall 	GENERIC_EXCEPTION_CLASS('C', '+', '+', '\0');
2727a984708SDavid Chisnall 
isCXXException(uint64_t cls)2737a984708SDavid Chisnall static bool isCXXException(uint64_t cls)
2747a984708SDavid Chisnall {
2757a984708SDavid Chisnall 	return (cls == exception_class) || (cls == dependent_exception_class);
2767a984708SDavid Chisnall }
2777a984708SDavid Chisnall 
isDependentException(uint64_t cls)2787a984708SDavid Chisnall static bool isDependentException(uint64_t cls)
2797a984708SDavid Chisnall {
2807a984708SDavid Chisnall 	return cls == dependent_exception_class;
2817a984708SDavid Chisnall }
2827a984708SDavid Chisnall 
exceptionFromPointer(void * ex)2837a984708SDavid Chisnall static __cxa_exception *exceptionFromPointer(void *ex)
2847a984708SDavid Chisnall {
285f2dc4184SDimitry Andric 	return reinterpret_cast<__cxa_exception*>(static_cast<char*>(ex) -
2867a984708SDavid Chisnall 			offsetof(struct __cxa_exception, unwindHeader));
2877a984708SDavid Chisnall }
realExceptionFromException(__cxa_exception * ex)2887a984708SDavid Chisnall static __cxa_exception *realExceptionFromException(__cxa_exception *ex)
2897a984708SDavid Chisnall {
2907a984708SDavid Chisnall 	if (!isDependentException(ex->unwindHeader.exception_class)) { return ex; }
291f2dc4184SDimitry Andric 	return reinterpret_cast<__cxa_exception*>((reinterpret_cast<__cxa_dependent_exception*>(ex))->primaryException)-1;
2927a984708SDavid Chisnall }
2937a984708SDavid Chisnall 
2947a984708SDavid Chisnall 
2957a984708SDavid Chisnall namespace std
2967a984708SDavid Chisnall {
2977a984708SDavid Chisnall 	// Forward declaration of standard library terminate() function used to
2987a984708SDavid Chisnall 	// abort execution.
29913da1af1SEd Maste 	[[noreturn]] void terminate(void) _LIBCXXRT_NOEXCEPT;
3007a984708SDavid Chisnall }
3017a984708SDavid Chisnall 
3027a984708SDavid Chisnall using namespace ABI_NAMESPACE;
3037a984708SDavid Chisnall 
30413da1af1SEd Maste #ifdef LIBCXXRT_NO_DEFAULT_TERMINATE_DIAGNOSTICS
3057a984708SDavid Chisnall /** The global termination handler. */
306bfffb66eSDimitry Andric static atomic<terminate_handler> terminateHandler = abort;
30713da1af1SEd Maste #else
30813da1af1SEd Maste /**
30913da1af1SEd Maste  * Callback function used with _Unwind_Backtrace().
31013da1af1SEd Maste  *
31113da1af1SEd Maste  * Prints a stack trace.  Used only for debugging help.
31213da1af1SEd Maste  *
31313da1af1SEd Maste  * Note: As of FreeBSD 8.1, dladdr() still doesn't work properly, so this only
31413da1af1SEd Maste  * correctly prints function names from public, relocatable, symbols.
31513da1af1SEd Maste  */
trace(struct _Unwind_Context * context,void * c)31613da1af1SEd Maste static _Unwind_Reason_Code trace(struct _Unwind_Context *context, void *c)
31713da1af1SEd Maste {
31813da1af1SEd Maste 	Dl_info myinfo;
31913da1af1SEd Maste 	int mylookup =
32013da1af1SEd Maste 		dladdr(reinterpret_cast<void *>(__cxa_current_exception_type), &myinfo);
32113da1af1SEd Maste 	void *ip = reinterpret_cast<void*>(_Unwind_GetIP(context));
32213da1af1SEd Maste 	Dl_info info;
32313da1af1SEd Maste 	if (dladdr(ip, &info) != 0)
32413da1af1SEd Maste 	{
32513da1af1SEd Maste 		if (mylookup == 0 || strcmp(info.dli_fname, myinfo.dli_fname) != 0)
32613da1af1SEd Maste 		{
32713da1af1SEd Maste 			printf("%p:%s() in %s\n", ip, info.dli_sname, info.dli_fname);
32813da1af1SEd Maste 		}
32913da1af1SEd Maste 	}
33013da1af1SEd Maste 	return _URC_CONTINUE_UNWIND;
33113da1af1SEd Maste }
33213da1af1SEd Maste 
terminate_with_diagnostics()33313da1af1SEd Maste static void terminate_with_diagnostics() {
33413da1af1SEd Maste 	__cxa_eh_globals *globals = __cxa_get_globals();
33513da1af1SEd Maste 	__cxa_exception *ex = globals->caughtExceptions;
33613da1af1SEd Maste 
33713da1af1SEd Maste 	if (ex != nullptr) {
33813da1af1SEd Maste 		fprintf(stderr, "Terminating due to uncaught exception %p", static_cast<void*>(ex));
33913da1af1SEd Maste 		ex = realExceptionFromException(ex);
34013da1af1SEd Maste 		static const __class_type_info *e_ti =
34113da1af1SEd Maste 			static_cast<const __class_type_info*>(&typeid(std::exception));
34213da1af1SEd Maste 		const __class_type_info *throw_ti =
34313da1af1SEd Maste 			dynamic_cast<const __class_type_info*>(ex->exceptionType);
34413da1af1SEd Maste 		if (throw_ti)
34513da1af1SEd Maste 		{
34613da1af1SEd Maste 			std::exception *e =
34713da1af1SEd Maste 				static_cast<std::exception*>(e_ti->cast_to(static_cast<void*>(ex+1), throw_ti));
34813da1af1SEd Maste 			if (e)
34913da1af1SEd Maste 			{
35013da1af1SEd Maste 				fprintf(stderr, " '%s'", e->what());
35113da1af1SEd Maste 			}
35213da1af1SEd Maste 		}
35313da1af1SEd Maste 
35413da1af1SEd Maste 		size_t bufferSize = 128;
35513da1af1SEd Maste 		char *demangled = static_cast<char*>(malloc(bufferSize));
35613da1af1SEd Maste 		const char *mangled = ex->exceptionType->name();
35713da1af1SEd Maste 		int status;
35813da1af1SEd Maste 		demangled = __cxa_demangle(mangled, demangled, &bufferSize, &status);
35913da1af1SEd Maste 		fprintf(stderr, " of type %s\n",
36013da1af1SEd Maste 			status == 0 ? demangled : mangled);
36113da1af1SEd Maste 		if (status == 0) { free(demangled); }
36213da1af1SEd Maste 
36313da1af1SEd Maste 		_Unwind_Backtrace(trace, 0);
36413da1af1SEd Maste 	}
36513da1af1SEd Maste 
36613da1af1SEd Maste 	abort();
36713da1af1SEd Maste }
36813da1af1SEd Maste 
36913da1af1SEd Maste /** The global termination handler. */
37013da1af1SEd Maste static atomic<terminate_handler> terminateHandler = terminate_with_diagnostics;
37113da1af1SEd Maste #endif
37213da1af1SEd Maste 
3737a984708SDavid Chisnall /** The global unexpected exception handler. */
374bfffb66eSDimitry Andric static atomic<unexpected_handler> unexpectedHandler = std::terminate;
3757a984708SDavid Chisnall 
3767a984708SDavid Chisnall /** Key used for thread-local data. */
3777a984708SDavid Chisnall static pthread_key_t eh_key;
3787a984708SDavid Chisnall 
3797a984708SDavid Chisnall 
3807a984708SDavid Chisnall /**
3817a984708SDavid Chisnall  * Cleanup function, allowing foreign exception handlers to correctly destroy
3827a984708SDavid Chisnall  * this exception if they catch it.
3837a984708SDavid Chisnall  */
exception_cleanup(_Unwind_Reason_Code reason,struct _Unwind_Exception * ex)3847a984708SDavid Chisnall static void exception_cleanup(_Unwind_Reason_Code reason,
3857a984708SDavid Chisnall                               struct _Unwind_Exception *ex)
3867a984708SDavid Chisnall {
387076e75ebSDimitry Andric 	// Exception layout:
388076e75ebSDimitry Andric 	// [__cxa_exception [_Unwind_Exception]] [exception object]
389076e75ebSDimitry Andric 	//
390076e75ebSDimitry Andric 	// __cxa_free_exception expects a pointer to the exception object
391076e75ebSDimitry Andric 	__cxa_free_exception(static_cast<void*>(ex + 1));
3927a984708SDavid Chisnall }
dependent_exception_cleanup(_Unwind_Reason_Code reason,struct _Unwind_Exception * ex)3937a984708SDavid Chisnall static void dependent_exception_cleanup(_Unwind_Reason_Code reason,
3947a984708SDavid Chisnall                               struct _Unwind_Exception *ex)
3957a984708SDavid Chisnall {
3967a984708SDavid Chisnall 
397076e75ebSDimitry Andric 	__cxa_free_dependent_exception(static_cast<void*>(ex + 1));
3987a984708SDavid Chisnall }
3997a984708SDavid Chisnall 
4007a984708SDavid Chisnall /**
4017a984708SDavid Chisnall  * Recursively walk a list of exceptions and delete them all in post-order.
4027a984708SDavid Chisnall  */
free_exception_list(__cxa_exception * ex)4037a984708SDavid Chisnall static void free_exception_list(__cxa_exception *ex)
4047a984708SDavid Chisnall {
4057a984708SDavid Chisnall 	if (0 != ex->nextException)
4067a984708SDavid Chisnall 	{
4077a984708SDavid Chisnall 		free_exception_list(ex->nextException);
4087a984708SDavid Chisnall 	}
4097a984708SDavid Chisnall 	// __cxa_free_exception() expects to be passed the thrown object, which
4107a984708SDavid Chisnall 	// immediately follows the exception, not the exception itself
4117a984708SDavid Chisnall 	__cxa_free_exception(ex+1);
4127a984708SDavid Chisnall }
4137a984708SDavid Chisnall 
4147a984708SDavid Chisnall /**
4157a984708SDavid Chisnall  * Cleanup function called when a thread exists to make certain that all of the
4167a984708SDavid Chisnall  * per-thread data is deleted.
4177a984708SDavid Chisnall  */
thread_cleanup(void * thread_info)4187a984708SDavid Chisnall static void thread_cleanup(void* thread_info)
4197a984708SDavid Chisnall {
420f2dc4184SDimitry Andric 	__cxa_thread_info *info = static_cast<__cxa_thread_info*>(thread_info);
4217a984708SDavid Chisnall 	if (info->globals.caughtExceptions)
4227a984708SDavid Chisnall 	{
423f7cb1657SDavid Chisnall 		// If this is a foreign exception, ask it to clean itself up.
424f7cb1657SDavid Chisnall 		if (info->foreign_exception_state != __cxa_thread_info::none)
425f7cb1657SDavid Chisnall 		{
426f2dc4184SDimitry Andric 			_Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(info->globals.caughtExceptions);
427076e75ebSDimitry Andric 			if (e->exception_cleanup)
428f7cb1657SDavid Chisnall 				e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
429f7cb1657SDavid Chisnall 		}
430f7cb1657SDavid Chisnall 		else
431f7cb1657SDavid Chisnall 		{
4327a984708SDavid Chisnall 			free_exception_list(info->globals.caughtExceptions);
4337a984708SDavid Chisnall 		}
434f7cb1657SDavid Chisnall 	}
4357a984708SDavid Chisnall 	free(thread_info);
4367a984708SDavid Chisnall }
4377a984708SDavid Chisnall 
4387a984708SDavid Chisnall 
4397a984708SDavid Chisnall /**
4407a984708SDavid Chisnall  * Once control used to protect the key creation.
4417a984708SDavid Chisnall  */
4427a984708SDavid Chisnall static pthread_once_t once_control = PTHREAD_ONCE_INIT;
4437a984708SDavid Chisnall 
4447a984708SDavid Chisnall /**
445db47c4bfSDavid Chisnall  * We may not be linked against a full pthread implementation.  If we're not,
446db47c4bfSDavid Chisnall  * then we need to fake the thread-local storage by storing 'thread-local'
447db47c4bfSDavid Chisnall  * things in a global.
448db47c4bfSDavid Chisnall  */
449db47c4bfSDavid Chisnall static bool fakeTLS;
450db47c4bfSDavid Chisnall /**
451db47c4bfSDavid Chisnall  * Thread-local storage for a single-threaded program.
452db47c4bfSDavid Chisnall  */
453db47c4bfSDavid Chisnall static __cxa_thread_info singleThreadInfo;
454db47c4bfSDavid Chisnall /**
4557a984708SDavid Chisnall  * Initialise eh_key.
4567a984708SDavid Chisnall  */
init_key(void)4577a984708SDavid Chisnall static void init_key(void)
4587a984708SDavid Chisnall {
459db47c4bfSDavid Chisnall 	if ((0 == pthread_key_create) ||
460db47c4bfSDavid Chisnall 	    (0 == pthread_setspecific) ||
461db47c4bfSDavid Chisnall 	    (0 == pthread_getspecific))
462db47c4bfSDavid Chisnall 	{
463db47c4bfSDavid Chisnall 		fakeTLS = true;
464db47c4bfSDavid Chisnall 		return;
465db47c4bfSDavid Chisnall 	}
4667a984708SDavid Chisnall 	pthread_key_create(&eh_key, thread_cleanup);
467f2dc4184SDimitry Andric 	pthread_setspecific(eh_key, reinterpret_cast<void *>(0x42));
468f2dc4184SDimitry Andric 	fakeTLS = (pthread_getspecific(eh_key) != reinterpret_cast<void *>(0x42));
469db47c4bfSDavid Chisnall 	pthread_setspecific(eh_key, 0);
4707a984708SDavid Chisnall }
4717a984708SDavid Chisnall 
4727a984708SDavid Chisnall /**
4737a984708SDavid Chisnall  * Returns the thread info structure, creating it if it is not already created.
4747a984708SDavid Chisnall  */
thread_info()4757a984708SDavid Chisnall static __cxa_thread_info *thread_info()
4767a984708SDavid Chisnall {
477db47c4bfSDavid Chisnall 	if ((0 == pthread_once) || pthread_once(&once_control, init_key))
478db47c4bfSDavid Chisnall 	{
479db47c4bfSDavid Chisnall 		fakeTLS = true;
480db47c4bfSDavid Chisnall 	}
481db47c4bfSDavid Chisnall 	if (fakeTLS) { return &singleThreadInfo; }
482f2dc4184SDimitry Andric 	__cxa_thread_info *info = static_cast<__cxa_thread_info*>(pthread_getspecific(eh_key));
4837a984708SDavid Chisnall 	if (0 == info)
4847a984708SDavid Chisnall 	{
485f2dc4184SDimitry Andric 		info = static_cast<__cxa_thread_info*>(calloc(1, sizeof(__cxa_thread_info)));
4867a984708SDavid Chisnall 		pthread_setspecific(eh_key, info);
4877a984708SDavid Chisnall 	}
4887a984708SDavid Chisnall 	return info;
4897a984708SDavid Chisnall }
4907a984708SDavid Chisnall /**
4917a984708SDavid Chisnall  * Fast version of thread_info().  May fail if thread_info() is not called on
4927a984708SDavid Chisnall  * this thread at least once already.
4937a984708SDavid Chisnall  */
thread_info_fast()4947a984708SDavid Chisnall static __cxa_thread_info *thread_info_fast()
4957a984708SDavid Chisnall {
496db47c4bfSDavid Chisnall 	if (fakeTLS) { return &singleThreadInfo; }
497f2dc4184SDimitry Andric 	return static_cast<__cxa_thread_info*>(pthread_getspecific(eh_key));
4987a984708SDavid Chisnall }
4997a984708SDavid Chisnall /**
5007a984708SDavid Chisnall  * ABI function returning the __cxa_eh_globals structure.
5017a984708SDavid Chisnall  */
__cxa_get_globals(void)5027a984708SDavid Chisnall extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals(void)
5037a984708SDavid Chisnall {
5047a984708SDavid Chisnall 	return &(thread_info()->globals);
5057a984708SDavid Chisnall }
5067a984708SDavid Chisnall /**
5077a984708SDavid Chisnall  * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already
5087a984708SDavid Chisnall  * been called at least once by this thread.
5097a984708SDavid Chisnall  */
__cxa_get_globals_fast(void)5107a984708SDavid Chisnall extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals_fast(void)
5117a984708SDavid Chisnall {
5127a984708SDavid Chisnall 	return &(thread_info_fast()->globals);
5137a984708SDavid Chisnall }
5147a984708SDavid Chisnall 
515ecf41062SDimitry Andric #ifdef LIBCXXRT_NO_EMERGENCY_MALLOC
alloc_or_die(size_t size)516ecf41062SDimitry Andric static char *alloc_or_die(size_t size)
517ecf41062SDimitry Andric {
518ecf41062SDimitry Andric 	char *buffer = static_cast<char*>(calloc(1, size));
519ecf41062SDimitry Andric 
520ecf41062SDimitry Andric 	if (buffer == nullptr)
521ecf41062SDimitry Andric 	{
522ecf41062SDimitry Andric 		fputs("Out of memory attempting to allocate exception\n", stderr);
523ecf41062SDimitry Andric 		std::terminate();
524ecf41062SDimitry Andric 	}
525ecf41062SDimitry Andric 	return buffer;
526ecf41062SDimitry Andric }
free_exception(char * e)527ecf41062SDimitry Andric static void free_exception(char *e)
528ecf41062SDimitry Andric {
529ecf41062SDimitry Andric 	free(e);
530ecf41062SDimitry Andric }
531ecf41062SDimitry Andric #else
5327a984708SDavid Chisnall /**
5337a984708SDavid Chisnall  * An emergency allocation reserved for when malloc fails.  This is treated as
5347a984708SDavid Chisnall  * 16 buffers of 1KB each.
5357a984708SDavid Chisnall  */
5367a984708SDavid Chisnall static char emergency_buffer[16384];
5377a984708SDavid Chisnall /**
5387a984708SDavid Chisnall  * Flag indicating whether each buffer is allocated.
5397a984708SDavid Chisnall  */
5407a984708SDavid Chisnall static bool buffer_allocated[16];
5417a984708SDavid Chisnall /**
5427a984708SDavid Chisnall  * Lock used to protect emergency allocation.
5437a984708SDavid Chisnall  */
5447a984708SDavid Chisnall static pthread_mutex_t emergency_malloc_lock = PTHREAD_MUTEX_INITIALIZER;
5457a984708SDavid Chisnall /**
5467a984708SDavid Chisnall  * Condition variable used to wait when two threads are both trying to use the
5477a984708SDavid Chisnall  * emergency malloc() buffer at once.
5487a984708SDavid Chisnall  */
5497a984708SDavid Chisnall static pthread_cond_t emergency_malloc_wait = PTHREAD_COND_INITIALIZER;
5507a984708SDavid Chisnall 
5517a984708SDavid Chisnall /**
5527a984708SDavid Chisnall  * Allocates size bytes from the emergency allocation mechanism, if possible.
5537a984708SDavid Chisnall  * This function will fail if size is over 1KB or if this thread already has 4
5547a984708SDavid Chisnall  * emergency buffers.  If all emergency buffers are allocated, it will sleep
5557a984708SDavid Chisnall  * until one becomes available.
5567a984708SDavid Chisnall  */
emergency_malloc(size_t size)5577a984708SDavid Chisnall static char *emergency_malloc(size_t size)
5587a984708SDavid Chisnall {
5597a984708SDavid Chisnall 	if (size > 1024) { return 0; }
5607a984708SDavid Chisnall 
5617a984708SDavid Chisnall 	__cxa_thread_info *info = thread_info();
5627a984708SDavid Chisnall 	// Only 4 emergency buffers allowed per thread!
5637a984708SDavid Chisnall 	if (info->emergencyBuffersHeld > 3) { return 0; }
5647a984708SDavid Chisnall 
5657a984708SDavid Chisnall 	pthread_mutex_lock(&emergency_malloc_lock);
5667a984708SDavid Chisnall 	int buffer = -1;
5677a984708SDavid Chisnall 	while (buffer < 0)
5687a984708SDavid Chisnall 	{
5697a984708SDavid Chisnall 		// While we were sleeping on the lock, another thread might have free'd
5707a984708SDavid Chisnall 		// enough memory for us to use, so try the allocation again - no point
5717a984708SDavid Chisnall 		// using the emergency buffer if there is some real memory that we can
5727a984708SDavid Chisnall 		// use...
5737a984708SDavid Chisnall 		void *m = calloc(1, size);
5747a984708SDavid Chisnall 		if (0 != m)
5757a984708SDavid Chisnall 		{
5767a984708SDavid Chisnall 			pthread_mutex_unlock(&emergency_malloc_lock);
577f2dc4184SDimitry Andric 			return static_cast<char*>(m);
5787a984708SDavid Chisnall 		}
5797a984708SDavid Chisnall 		for (int i=0 ; i<16 ; i++)
5807a984708SDavid Chisnall 		{
5817a984708SDavid Chisnall 			if (!buffer_allocated[i])
5827a984708SDavid Chisnall 			{
5837a984708SDavid Chisnall 				buffer = i;
5847a984708SDavid Chisnall 				buffer_allocated[i] = true;
5857a984708SDavid Chisnall 				break;
5867a984708SDavid Chisnall 			}
5877a984708SDavid Chisnall 		}
5887a984708SDavid Chisnall 		// If there still isn't a buffer available, then sleep on the condition
5897a984708SDavid Chisnall 		// variable.  This will be signalled when another thread releases one
5907a984708SDavid Chisnall 		// of the emergency buffers.
5917a984708SDavid Chisnall 		if (buffer < 0)
5927a984708SDavid Chisnall 		{
5937a984708SDavid Chisnall 			pthread_cond_wait(&emergency_malloc_wait, &emergency_malloc_lock);
5947a984708SDavid Chisnall 		}
5957a984708SDavid Chisnall 	}
5967a984708SDavid Chisnall 	pthread_mutex_unlock(&emergency_malloc_lock);
5977a984708SDavid Chisnall 	info->emergencyBuffersHeld++;
5987a984708SDavid Chisnall 	return emergency_buffer + (1024 * buffer);
5997a984708SDavid Chisnall }
6007a984708SDavid Chisnall 
6017a984708SDavid Chisnall /**
6027a984708SDavid Chisnall  * Frees a buffer returned by emergency_malloc().
6037a984708SDavid Chisnall  *
6047a984708SDavid Chisnall  * Note: Neither this nor emergency_malloc() is particularly efficient.  This
6057a984708SDavid Chisnall  * should not matter, because neither will be called in normal operation - they
6067a984708SDavid Chisnall  * are only used when the program runs out of memory, which should not happen
6077a984708SDavid Chisnall  * often.
6087a984708SDavid Chisnall  */
emergency_malloc_free(char * ptr)6097a984708SDavid Chisnall static void emergency_malloc_free(char *ptr)
6107a984708SDavid Chisnall {
6117a984708SDavid Chisnall 	int buffer = -1;
6127a984708SDavid Chisnall 	// Find the buffer corresponding to this pointer.
6137a984708SDavid Chisnall 	for (int i=0 ; i<16 ; i++)
6147a984708SDavid Chisnall 	{
615f2dc4184SDimitry Andric 		if (ptr == static_cast<void*>(emergency_buffer + (1024 * i)))
6167a984708SDavid Chisnall 		{
6177a984708SDavid Chisnall 			buffer = i;
6187a984708SDavid Chisnall 			break;
6197a984708SDavid Chisnall 		}
6207a984708SDavid Chisnall 	}
62176ad5667SConrad Meyer 	assert(buffer >= 0 &&
6227a984708SDavid Chisnall 	       "Trying to free something that is not an emergency buffer!");
6237a984708SDavid Chisnall 	// emergency_malloc() is expected to return 0-initialized data.  We don't
6247a984708SDavid Chisnall 	// zero the buffer when allocating it, because the static buffers will
6257a984708SDavid Chisnall 	// begin life containing 0 values.
626f2dc4184SDimitry Andric 	memset(ptr, 0, 1024);
6277a984708SDavid Chisnall 	// Signal the condition variable to wake up any threads that are blocking
6287a984708SDavid Chisnall 	// waiting for some space in the emergency buffer
6297a984708SDavid Chisnall 	pthread_mutex_lock(&emergency_malloc_lock);
6307a984708SDavid Chisnall 	// In theory, we don't need to do this with the lock held.  In practice,
6317a984708SDavid Chisnall 	// our array of bools will probably be updated using 32-bit or 64-bit
6327a984708SDavid Chisnall 	// memory operations, so this update may clobber adjacent values.
6337a984708SDavid Chisnall 	buffer_allocated[buffer] = false;
6347a984708SDavid Chisnall 	pthread_cond_signal(&emergency_malloc_wait);
6357a984708SDavid Chisnall 	pthread_mutex_unlock(&emergency_malloc_lock);
6367a984708SDavid Chisnall }
6377a984708SDavid Chisnall 
alloc_or_die(size_t size)6387a984708SDavid Chisnall static char *alloc_or_die(size_t size)
6397a984708SDavid Chisnall {
640f2dc4184SDimitry Andric 	char *buffer = static_cast<char*>(calloc(1, size));
6417a984708SDavid Chisnall 
6427a984708SDavid Chisnall 	// If calloc() doesn't want to give us any memory, try using an emergency
6437a984708SDavid Chisnall 	// buffer.
6447a984708SDavid Chisnall 	if (0 == buffer)
6457a984708SDavid Chisnall 	{
6467a984708SDavid Chisnall 		buffer = emergency_malloc(size);
6477a984708SDavid Chisnall 		// This is only reached if the allocation is greater than 1KB, and
6487a984708SDavid Chisnall 		// anyone throwing objects that big really should know better.
6497a984708SDavid Chisnall 		if (0 == buffer)
6507a984708SDavid Chisnall 		{
6517a984708SDavid Chisnall 			fprintf(stderr, "Out of memory attempting to allocate exception\n");
6527a984708SDavid Chisnall 			std::terminate();
6537a984708SDavid Chisnall 		}
6547a984708SDavid Chisnall 	}
6557a984708SDavid Chisnall 	return buffer;
6567a984708SDavid Chisnall }
free_exception(char * e)6577a984708SDavid Chisnall static void free_exception(char *e)
6587a984708SDavid Chisnall {
6597a984708SDavid Chisnall 	// If this allocation is within the address range of the emergency buffer,
6607a984708SDavid Chisnall 	// don't call free() because it was not allocated with malloc()
66176ad5667SConrad Meyer 	if ((e >= emergency_buffer) &&
6627a984708SDavid Chisnall 	    (e < (emergency_buffer + sizeof(emergency_buffer))))
6637a984708SDavid Chisnall 	{
6647a984708SDavid Chisnall 		emergency_malloc_free(e);
6657a984708SDavid Chisnall 	}
6667a984708SDavid Chisnall 	else
6677a984708SDavid Chisnall 	{
6687a984708SDavid Chisnall 		free(e);
6697a984708SDavid Chisnall 	}
6707a984708SDavid Chisnall }
671ecf41062SDimitry Andric #endif
6727a984708SDavid Chisnall 
6737a984708SDavid Chisnall /**
6747a984708SDavid Chisnall  * Allocates an exception structure.  Returns a pointer to the space that can
6757a984708SDavid Chisnall  * be used to store an object of thrown_size bytes.  This function will use an
6767a984708SDavid Chisnall  * emergency buffer if malloc() fails, and may block if there are no such
6777a984708SDavid Chisnall  * buffers available.
6787a984708SDavid Chisnall  */
__cxa_allocate_exception(size_t thrown_size)67913da1af1SEd Maste extern "C" void *__cxa_allocate_exception(size_t thrown_size) _LIBCXXRT_NOEXCEPT
6807a984708SDavid Chisnall {
68172df847aSDimitry Andric 	size_t size = thrown_size + sizeof(__cxa_exception);
6827a984708SDavid Chisnall 	char *buffer = alloc_or_die(size);
68372df847aSDimitry Andric 	return buffer+sizeof(__cxa_exception);
6847a984708SDavid Chisnall }
6857a984708SDavid Chisnall 
__cxa_allocate_dependent_exception(void)6867a984708SDavid Chisnall extern "C" void *__cxa_allocate_dependent_exception(void)
6877a984708SDavid Chisnall {
68872df847aSDimitry Andric 	size_t size = sizeof(__cxa_dependent_exception);
6897a984708SDavid Chisnall 	char *buffer = alloc_or_die(size);
69072df847aSDimitry Andric 	return buffer+sizeof(__cxa_dependent_exception);
6917a984708SDavid Chisnall }
6927a984708SDavid Chisnall 
6937a984708SDavid Chisnall /**
6947a984708SDavid Chisnall  * __cxa_free_exception() is called when an exception was thrown in between
6957a984708SDavid Chisnall  * calling __cxa_allocate_exception() and actually throwing the exception.
6967a984708SDavid Chisnall  * This happens when the object's copy constructor throws an exception.
6977a984708SDavid Chisnall  *
6987a984708SDavid Chisnall  * In this implementation, it is also called by __cxa_end_catch() and during
6997a984708SDavid Chisnall  * thread cleanup.
7007a984708SDavid Chisnall  */
__cxa_free_exception(void * thrown_exception)70113da1af1SEd Maste extern "C" void __cxa_free_exception(void *thrown_exception) _LIBCXXRT_NOEXCEPT
7027a984708SDavid Chisnall {
703f2dc4184SDimitry Andric 	__cxa_exception *ex = reinterpret_cast<__cxa_exception*>(thrown_exception) - 1;
7047a984708SDavid Chisnall 	// Free the object that was thrown, calling its destructor
7057a984708SDavid Chisnall 	if (0 != ex->exceptionDestructor)
7067a984708SDavid Chisnall 	{
7077a984708SDavid Chisnall 		try
7087a984708SDavid Chisnall 		{
7097a984708SDavid Chisnall 			ex->exceptionDestructor(thrown_exception);
7107a984708SDavid Chisnall 		}
7117a984708SDavid Chisnall 		catch(...)
7127a984708SDavid Chisnall 		{
7137a984708SDavid Chisnall 			// FIXME: Check that this is really what the spec says to do.
7147a984708SDavid Chisnall 			std::terminate();
7157a984708SDavid Chisnall 		}
7167a984708SDavid Chisnall 	}
7177a984708SDavid Chisnall 
71872df847aSDimitry Andric 	free_exception(reinterpret_cast<char*>(ex));
7197a984708SDavid Chisnall }
7207a984708SDavid Chisnall 
releaseException(__cxa_exception * exception)7217a984708SDavid Chisnall static void releaseException(__cxa_exception *exception)
7227a984708SDavid Chisnall {
7237a984708SDavid Chisnall 	if (isDependentException(exception->unwindHeader.exception_class))
7247a984708SDavid Chisnall 	{
7257a984708SDavid Chisnall 		__cxa_free_dependent_exception(exception+1);
7267a984708SDavid Chisnall 		return;
7277a984708SDavid Chisnall 	}
7287a984708SDavid Chisnall 	if (__sync_sub_and_fetch(&exception->referenceCount, 1) == 0)
7297a984708SDavid Chisnall 	{
7307a984708SDavid Chisnall 		// __cxa_free_exception() expects to be passed the thrown object,
7317a984708SDavid Chisnall 		// which immediately follows the exception, not the exception
7327a984708SDavid Chisnall 		// itself
7337a984708SDavid Chisnall 		__cxa_free_exception(exception+1);
7347a984708SDavid Chisnall 	}
7357a984708SDavid Chisnall }
7367a984708SDavid Chisnall 
__cxa_free_dependent_exception(void * thrown_exception)7377a984708SDavid Chisnall void __cxa_free_dependent_exception(void *thrown_exception)
7387a984708SDavid Chisnall {
739f2dc4184SDimitry Andric 	__cxa_dependent_exception *ex = reinterpret_cast<__cxa_dependent_exception*>(thrown_exception) - 1;
7407a984708SDavid Chisnall 	assert(isDependentException(ex->unwindHeader.exception_class));
7417a984708SDavid Chisnall 	if (ex->primaryException)
7427a984708SDavid Chisnall 	{
743f2dc4184SDimitry Andric 		releaseException(realExceptionFromException(reinterpret_cast<__cxa_exception*>(ex)));
7447a984708SDavid Chisnall 	}
74572df847aSDimitry Andric 	free_exception(reinterpret_cast<char*>(ex));
7467a984708SDavid Chisnall }
7477a984708SDavid Chisnall 
7487a984708SDavid Chisnall /**
7497a984708SDavid Chisnall  * Report a failure that occurred when attempting to throw an exception.
7507a984708SDavid Chisnall  *
7517a984708SDavid Chisnall  * If the failure happened by falling off the end of the stack without finding
75213da1af1SEd Maste  * a handler, catch the exception before calling terminate. The default
75313da1af1SEd Maste  * terminate handler will print a backtrace before aborting.
7547a984708SDavid Chisnall  */
75507122a2aSDimitry Andric #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
75613da1af1SEd Maste extern "C" void *__cxa_begin_catch(void *e) _LIBCXXRT_NOEXCEPT;
757f2dc4184SDimitry Andric #else
758f2dc4184SDimitry Andric extern "C" void *__cxa_begin_catch(void *e);
759f2dc4184SDimitry Andric #endif
report_failure(_Unwind_Reason_Code err,__cxa_exception * thrown_exception)7607a984708SDavid Chisnall static void report_failure(_Unwind_Reason_Code err, __cxa_exception *thrown_exception)
7617a984708SDavid Chisnall {
7627a984708SDavid Chisnall 	switch (err)
7637a984708SDavid Chisnall 	{
7647a984708SDavid Chisnall 		default: break;
7657a984708SDavid Chisnall 		case _URC_FATAL_PHASE1_ERROR:
7667a984708SDavid Chisnall 			fprintf(stderr, "Fatal error during phase 1 unwinding\n");
7677a984708SDavid Chisnall 			break;
768f2dc4184SDimitry Andric #if !defined(__arm__) || defined(__ARM_DWARF_EH__)
7697a984708SDavid Chisnall 		case _URC_FATAL_PHASE2_ERROR:
7707a984708SDavid Chisnall 			fprintf(stderr, "Fatal error during phase 2 unwinding\n");
7717a984708SDavid Chisnall 			break;
7727a984708SDavid Chisnall #endif
7737a984708SDavid Chisnall 		case _URC_END_OF_STACK:
774f2dc4184SDimitry Andric 			__cxa_begin_catch (&(thrown_exception->unwindHeader));
7757a984708SDavid Chisnall 			break;
7767a984708SDavid Chisnall 	}
7777a984708SDavid Chisnall 	std::terminate();
7787a984708SDavid Chisnall }
7797a984708SDavid Chisnall 
throw_exception(__cxa_exception * ex)7807a984708SDavid Chisnall static void throw_exception(__cxa_exception *ex)
7817a984708SDavid Chisnall {
7827a984708SDavid Chisnall 	__cxa_thread_info *info = thread_info();
7837a984708SDavid Chisnall 	ex->unexpectedHandler = info->unexpectedHandler;
7847a984708SDavid Chisnall 	if (0 == ex->unexpectedHandler)
7857a984708SDavid Chisnall 	{
786bfffb66eSDimitry Andric 		ex->unexpectedHandler = unexpectedHandler.load();
7877a984708SDavid Chisnall 	}
7887a984708SDavid Chisnall 	ex->terminateHandler  = info->terminateHandler;
7897a984708SDavid Chisnall 	if (0 == ex->terminateHandler)
7907a984708SDavid Chisnall 	{
791bfffb66eSDimitry Andric 		ex->terminateHandler = terminateHandler.load();
7927a984708SDavid Chisnall 	}
7937a984708SDavid Chisnall 	info->globals.uncaughtExceptions++;
7947a984708SDavid Chisnall 
7957a984708SDavid Chisnall 	_Unwind_Reason_Code err = _Unwind_RaiseException(&ex->unwindHeader);
7967a984708SDavid Chisnall 	// The _Unwind_RaiseException() function should not return, it should
7977a984708SDavid Chisnall 	// unwind the stack past this function.  If it does return, then something
7987a984708SDavid Chisnall 	// has gone wrong.
7997a984708SDavid Chisnall 	report_failure(err, ex);
8007a984708SDavid Chisnall }
8017a984708SDavid Chisnall 
__cxa_init_primary_exception(void * object,std::type_info * tinfo,void (* dest)(void *))802ecf41062SDimitry Andric extern "C" __cxa_exception *__cxa_init_primary_exception(
80313da1af1SEd Maste 		void *object, std::type_info* tinfo, void (*dest)(void *)) _LIBCXXRT_NOEXCEPT {
804ecf41062SDimitry Andric 	__cxa_exception *ex = reinterpret_cast<__cxa_exception*>(object) - 1;
805ecf41062SDimitry Andric 
806ecf41062SDimitry Andric 	ex->referenceCount = 0;
807ecf41062SDimitry Andric 	ex->exceptionType = tinfo;
808ecf41062SDimitry Andric 
809ecf41062SDimitry Andric 	ex->exceptionDestructor = dest;
810ecf41062SDimitry Andric 
811ecf41062SDimitry Andric 	ex->unwindHeader.exception_class = exception_class;
812ecf41062SDimitry Andric 	ex->unwindHeader.exception_cleanup = exception_cleanup;
813ecf41062SDimitry Andric 
814ecf41062SDimitry Andric 	return ex;
815ecf41062SDimitry Andric }
816ecf41062SDimitry Andric 
8177a984708SDavid Chisnall 
8187a984708SDavid Chisnall /**
8197a984708SDavid Chisnall  * ABI function for throwing an exception.  Takes the object to be thrown (the
8207a984708SDavid Chisnall  * pointer returned by __cxa_allocate_exception()), the type info for the
8217a984708SDavid Chisnall  * pointee, and the destructor (if there is one) as arguments.
8227a984708SDavid Chisnall  */
__cxa_throw(void * thrown_exception,std::type_info * tinfo,void (* dest)(void *))8237a984708SDavid Chisnall extern "C" void __cxa_throw(void *thrown_exception,
8247a984708SDavid Chisnall                             std::type_info *tinfo,
8257a984708SDavid Chisnall                             void(*dest)(void*))
8267a984708SDavid Chisnall {
827ecf41062SDimitry Andric 	__cxa_exception *ex = __cxa_init_primary_exception(thrown_exception, tinfo, dest);
8287a984708SDavid Chisnall 	ex->referenceCount = 1;
8297a984708SDavid Chisnall 
8307a984708SDavid Chisnall 	throw_exception(ex);
8317a984708SDavid Chisnall }
8327a984708SDavid Chisnall 
__cxa_rethrow_primary_exception(void * thrown_exception)8337a984708SDavid Chisnall extern "C" void __cxa_rethrow_primary_exception(void* thrown_exception)
8347a984708SDavid Chisnall {
8357a984708SDavid Chisnall 	if (NULL == thrown_exception) { return; }
8367a984708SDavid Chisnall 
8377a984708SDavid Chisnall 	__cxa_exception *original = exceptionFromPointer(thrown_exception);
838f2dc4184SDimitry Andric 	__cxa_dependent_exception *ex = reinterpret_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception())-1;
8397a984708SDavid Chisnall 
8407a984708SDavid Chisnall 	ex->primaryException = thrown_exception;
8417a984708SDavid Chisnall 	__cxa_increment_exception_refcount(thrown_exception);
8427a984708SDavid Chisnall 
8437a984708SDavid Chisnall 	ex->exceptionType = original->exceptionType;
8447a984708SDavid Chisnall 	ex->unwindHeader.exception_class = dependent_exception_class;
8457a984708SDavid Chisnall 	ex->unwindHeader.exception_cleanup = dependent_exception_cleanup;
8467a984708SDavid Chisnall 
847f2dc4184SDimitry Andric 	throw_exception(reinterpret_cast<__cxa_exception*>(ex));
8487a984708SDavid Chisnall }
8497a984708SDavid Chisnall 
__cxa_current_primary_exception(void)8507a984708SDavid Chisnall extern "C" void *__cxa_current_primary_exception(void)
8517a984708SDavid Chisnall {
8527a984708SDavid Chisnall 	__cxa_eh_globals* globals = __cxa_get_globals();
8537a984708SDavid Chisnall 	__cxa_exception *ex = globals->caughtExceptions;
8547a984708SDavid Chisnall 
8557a984708SDavid Chisnall 	if (0 == ex) { return NULL; }
8567a984708SDavid Chisnall 	ex = realExceptionFromException(ex);
8577a984708SDavid Chisnall 	__sync_fetch_and_add(&ex->referenceCount, 1);
8587a984708SDavid Chisnall 	return ex + 1;
8597a984708SDavid Chisnall }
8607a984708SDavid Chisnall 
__cxa_increment_exception_refcount(void * thrown_exception)8617a984708SDavid Chisnall extern "C" void __cxa_increment_exception_refcount(void* thrown_exception)
8627a984708SDavid Chisnall {
8637a984708SDavid Chisnall 	if (NULL == thrown_exception) { return; }
864f2dc4184SDimitry Andric 	__cxa_exception *ex = static_cast<__cxa_exception*>(thrown_exception) - 1;
8657a984708SDavid Chisnall 	if (isDependentException(ex->unwindHeader.exception_class)) { return; }
8667a984708SDavid Chisnall 	__sync_fetch_and_add(&ex->referenceCount, 1);
8677a984708SDavid Chisnall }
__cxa_decrement_exception_refcount(void * thrown_exception)8687a984708SDavid Chisnall extern "C" void __cxa_decrement_exception_refcount(void* thrown_exception)
8697a984708SDavid Chisnall {
8707a984708SDavid Chisnall 	if (NULL == thrown_exception) { return; }
871f2dc4184SDimitry Andric 	__cxa_exception *ex = static_cast<__cxa_exception*>(thrown_exception) - 1;
8727a984708SDavid Chisnall 	releaseException(ex);
8737a984708SDavid Chisnall }
8747a984708SDavid Chisnall 
8757a984708SDavid Chisnall /**
8767a984708SDavid Chisnall  * ABI function.  Rethrows the current exception.  Does not remove the
8777a984708SDavid Chisnall  * exception from the stack or decrement its handler count - the compiler is
8787a984708SDavid Chisnall  * expected to set the landing pad for this function to the end of the catch
8797a984708SDavid Chisnall  * block, and then call _Unwind_Resume() to continue unwinding once
8807a984708SDavid Chisnall  * __cxa_end_catch() has been called and any cleanup code has been run.
8817a984708SDavid Chisnall  */
__cxa_rethrow()8827a984708SDavid Chisnall extern "C" void __cxa_rethrow()
8837a984708SDavid Chisnall {
884d5861eaaSDavid Chisnall 	__cxa_thread_info *ti = thread_info();
885f7cb1657SDavid Chisnall 	__cxa_eh_globals *globals = &ti->globals;
8867a984708SDavid Chisnall 	// Note: We don't remove this from the caught list here, because
8877a984708SDavid Chisnall 	// __cxa_end_catch will be called when we unwind out of the try block.  We
8887a984708SDavid Chisnall 	// could probably make this faster by providing an alternative rethrow
8897a984708SDavid Chisnall 	// function and ensuring that all cleanup code is run before calling it, so
8907a984708SDavid Chisnall 	// we can skip the top stack frame when unwinding.
8917a984708SDavid Chisnall 	__cxa_exception *ex = globals->caughtExceptions;
8927a984708SDavid Chisnall 
8937a984708SDavid Chisnall 	if (0 == ex)
8947a984708SDavid Chisnall 	{
8957a984708SDavid Chisnall 		fprintf(stderr,
8967a984708SDavid Chisnall 		        "Attempting to rethrow an exception that doesn't exist!\n");
8977a984708SDavid Chisnall 		std::terminate();
8987a984708SDavid Chisnall 	}
8997a984708SDavid Chisnall 
900f7cb1657SDavid Chisnall 	if (ti->foreign_exception_state != __cxa_thread_info::none)
901f7cb1657SDavid Chisnall 	{
902f7cb1657SDavid Chisnall 		ti->foreign_exception_state = __cxa_thread_info::rethrown;
903f2dc4184SDimitry Andric 		_Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(ex);
904f7cb1657SDavid Chisnall 		_Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(e);
905f7cb1657SDavid Chisnall 		report_failure(err, ex);
906f7cb1657SDavid Chisnall 		return;
907f7cb1657SDavid Chisnall 	}
908f7cb1657SDavid Chisnall 
9097a984708SDavid Chisnall 	assert(ex->handlerCount > 0 && "Rethrowing uncaught exception!");
9107a984708SDavid Chisnall 
911e91d723aSDimitry Andric 	// `globals->uncaughtExceptions` was decremented by `__cxa_begin_catch`.
912e91d723aSDimitry Andric 	// It's normally incremented by `throw_exception`, but this path invokes
913e91d723aSDimitry Andric 	// `_Unwind_Resume_or_Rethrow` directly to rethrow the exception.
914e91d723aSDimitry Andric 	// This path is only reachable if we're rethrowing a C++ exception -
915e91d723aSDimitry Andric 	// foreign exceptions don't adjust any of this state.
916e91d723aSDimitry Andric 	globals->uncaughtExceptions++;
917e91d723aSDimitry Andric 
9187a984708SDavid Chisnall 	// ex->handlerCount will be decremented in __cxa_end_catch in enclosing
9197a984708SDavid Chisnall 	// catch block
9207a984708SDavid Chisnall 
9217a984708SDavid Chisnall 	// Make handler count negative. This will tell __cxa_end_catch that
9227a984708SDavid Chisnall 	// exception was rethrown and exception object should not be destroyed
9237a984708SDavid Chisnall 	// when handler count become zero
9247a984708SDavid Chisnall 	ex->handlerCount = -ex->handlerCount;
9257a984708SDavid Chisnall 
9267a984708SDavid Chisnall 	// Continue unwinding the stack with this exception.  This should unwind to
9277a984708SDavid Chisnall 	// the place in the caller where __cxa_end_catch() is called.  The caller
9287a984708SDavid Chisnall 	// will then run cleanup code and bounce the exception back with
9297a984708SDavid Chisnall 	// _Unwind_Resume().
9307a984708SDavid Chisnall 	_Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(&ex->unwindHeader);
9317a984708SDavid Chisnall 	report_failure(err, ex);
9327a984708SDavid Chisnall }
9337a984708SDavid Chisnall 
9347a984708SDavid Chisnall /**
9357a984708SDavid Chisnall  * Returns the type_info object corresponding to the filter.
9367a984708SDavid Chisnall  */
get_type_info_entry(_Unwind_Context * context,dwarf_eh_lsda * lsda,int filter)9377a984708SDavid Chisnall static std::type_info *get_type_info_entry(_Unwind_Context *context,
9387a984708SDavid Chisnall                                            dwarf_eh_lsda *lsda,
9397a984708SDavid Chisnall                                            int filter)
9407a984708SDavid Chisnall {
9417a984708SDavid Chisnall 	// Get the address of the record in the table.
9427a984708SDavid Chisnall 	dw_eh_ptr_t record = lsda->type_table -
9437a984708SDavid Chisnall 		dwarf_size_of_fixed_size_field(lsda->type_table_encoding)*filter;
9447a984708SDavid Chisnall 	//record -= 4;
9457a984708SDavid Chisnall 	dw_eh_ptr_t start = record;
9467a984708SDavid Chisnall 	// Read the value, but it's probably an indirect reference...
9477a984708SDavid Chisnall 	int64_t offset = read_value(lsda->type_table_encoding, &record);
9487a984708SDavid Chisnall 
9497a984708SDavid Chisnall 	// (If the entry is 0, don't try to dereference it.  That would be bad.)
9507a984708SDavid Chisnall 	if (offset == 0) { return 0; }
9517a984708SDavid Chisnall 
9527a984708SDavid Chisnall 	// ...so we need to resolve it
953f2dc4184SDimitry Andric 	return reinterpret_cast<std::type_info*>(resolve_indirect_value(context,
954f2dc4184SDimitry Andric 			lsda->type_table_encoding, offset, start));
9557a984708SDavid Chisnall }
9567a984708SDavid Chisnall 
9577a984708SDavid Chisnall 
9587a984708SDavid Chisnall 
9597a984708SDavid Chisnall /**
9607a984708SDavid Chisnall  * Checks the type signature found in a handler against the type of the thrown
9617a984708SDavid Chisnall  * object.  If ex is 0 then it is assumed to be a foreign exception and only
9627a984708SDavid Chisnall  * matches cleanups.
9637a984708SDavid Chisnall  */
check_type_signature(__cxa_exception * ex,const std::type_info * type,void * & adjustedPtr)9647a984708SDavid Chisnall static bool check_type_signature(__cxa_exception *ex,
9657a984708SDavid Chisnall                                  const std::type_info *type,
9667a984708SDavid Chisnall                                  void *&adjustedPtr)
9677a984708SDavid Chisnall {
968f2dc4184SDimitry Andric 	void *exception_ptr = static_cast<void*>(ex+1);
969f7cb1657SDavid Chisnall 	const std::type_info *ex_type = ex ? ex->exceptionType : 0;
9707a984708SDavid Chisnall 
971f7cb1657SDavid Chisnall 	bool is_ptr = ex ? ex_type->__is_pointer_p() : false;
972c725650dSDavid Chisnall 	if (is_ptr)
9737a984708SDavid Chisnall 	{
974f2dc4184SDimitry Andric 		exception_ptr = *static_cast<void**>(exception_ptr);
9757a984708SDavid Chisnall 	}
9767a984708SDavid Chisnall 	// Always match a catchall, even with a foreign exception
9777a984708SDavid Chisnall 	//
9787a984708SDavid Chisnall 	// Note: A 0 here is a catchall, not a cleanup, so we return true to
9797a984708SDavid Chisnall 	// indicate that we found a catch.
9807a984708SDavid Chisnall 	if (0 == type)
9817a984708SDavid Chisnall 	{
9827a984708SDavid Chisnall 		if (ex)
9837a984708SDavid Chisnall 		{
9847a984708SDavid Chisnall 			adjustedPtr = exception_ptr;
9857a984708SDavid Chisnall 		}
9867a984708SDavid Chisnall 		return true;
9877a984708SDavid Chisnall 	}
9887a984708SDavid Chisnall 
9897a984708SDavid Chisnall 	if (0 == ex) { return false; }
9907a984708SDavid Chisnall 
9917a984708SDavid Chisnall 	// If the types are the same, no casting is needed.
9927a984708SDavid Chisnall 	if (*type == *ex_type)
9937a984708SDavid Chisnall 	{
9947a984708SDavid Chisnall 		adjustedPtr = exception_ptr;
9957a984708SDavid Chisnall 		return true;
9967a984708SDavid Chisnall 	}
9977a984708SDavid Chisnall 
9987a984708SDavid Chisnall 
999c725650dSDavid Chisnall 	if (type->__do_catch(ex_type, &exception_ptr, 1))
10007a984708SDavid Chisnall 	{
1001c725650dSDavid Chisnall 		adjustedPtr = exception_ptr;
10027a984708SDavid Chisnall 		return true;
10037a984708SDavid Chisnall 	}
1004c725650dSDavid Chisnall 
10057a984708SDavid Chisnall 	return false;
10067a984708SDavid Chisnall }
10077a984708SDavid Chisnall /**
10087a984708SDavid Chisnall  * Checks whether the exception matches the type specifiers in this action
10097a984708SDavid Chisnall  * record.  If the exception only matches cleanups, then this returns false.
10107a984708SDavid Chisnall  * If it matches a catch (including a catchall) then it returns true.
10117a984708SDavid Chisnall  *
10127a984708SDavid Chisnall  * The selector argument is used to return the selector that is passed in the
10137a984708SDavid Chisnall  * second exception register when installing the context.
10147a984708SDavid Chisnall  */
check_action_record(_Unwind_Context * context,dwarf_eh_lsda * lsda,dw_eh_ptr_t action_record,__cxa_exception * ex,unsigned long * selector,void * & adjustedPtr)10157a984708SDavid Chisnall static handler_type check_action_record(_Unwind_Context *context,
10167a984708SDavid Chisnall                                         dwarf_eh_lsda *lsda,
10177a984708SDavid Chisnall                                         dw_eh_ptr_t action_record,
10187a984708SDavid Chisnall                                         __cxa_exception *ex,
10197a984708SDavid Chisnall                                         unsigned long *selector,
10207a984708SDavid Chisnall                                         void *&adjustedPtr)
10217a984708SDavid Chisnall {
10227a984708SDavid Chisnall 	if (!action_record) { return handler_cleanup; }
10237a984708SDavid Chisnall 	handler_type found = handler_none;
10247a984708SDavid Chisnall 	while (action_record)
10257a984708SDavid Chisnall 	{
10267a984708SDavid Chisnall 		int filter = read_sleb128(&action_record);
10277a984708SDavid Chisnall 		dw_eh_ptr_t action_record_offset_base = action_record;
10287a984708SDavid Chisnall 		int displacement = read_sleb128(&action_record);
10297a984708SDavid Chisnall 		action_record = displacement ?
10307a984708SDavid Chisnall 			action_record_offset_base + displacement : 0;
10317a984708SDavid Chisnall 		// We only check handler types for C++ exceptions - foreign exceptions
1032f7cb1657SDavid Chisnall 		// are only allowed for cleanups and catchalls.
1033f7cb1657SDavid Chisnall 		if (filter > 0)
10347a984708SDavid Chisnall 		{
10357a984708SDavid Chisnall 			std::type_info *handler_type = get_type_info_entry(context, lsda, filter);
10367a984708SDavid Chisnall 			if (check_type_signature(ex, handler_type, adjustedPtr))
10377a984708SDavid Chisnall 			{
10387a984708SDavid Chisnall 				*selector = filter;
10397a984708SDavid Chisnall 				return handler_catch;
10407a984708SDavid Chisnall 			}
10417a984708SDavid Chisnall 		}
10427a984708SDavid Chisnall 		else if (filter < 0 && 0 != ex)
10437a984708SDavid Chisnall 		{
10447a984708SDavid Chisnall 			bool matched = false;
10457a984708SDavid Chisnall 			*selector = filter;
1046f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
10477a984708SDavid Chisnall 			filter++;
10487a984708SDavid Chisnall 			std::type_info *handler_type = get_type_info_entry(context, lsda, filter--);
10497a984708SDavid Chisnall 			while (handler_type)
10507a984708SDavid Chisnall 			{
10517a984708SDavid Chisnall 				if (check_type_signature(ex, handler_type, adjustedPtr))
10527a984708SDavid Chisnall 				{
10537a984708SDavid Chisnall 					matched = true;
10547a984708SDavid Chisnall 					break;
10557a984708SDavid Chisnall 				}
10567a984708SDavid Chisnall 				handler_type = get_type_info_entry(context, lsda, filter--);
10577a984708SDavid Chisnall 			}
10587a984708SDavid Chisnall #else
1059f2dc4184SDimitry Andric 			unsigned char *type_index = reinterpret_cast<unsigned char*>(lsda->type_table) - filter - 1;
10607a984708SDavid Chisnall 			while (*type_index)
10617a984708SDavid Chisnall 			{
10627a984708SDavid Chisnall 				std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++));
10637a984708SDavid Chisnall 				// If the exception spec matches a permitted throw type for
10647a984708SDavid Chisnall 				// this function, don't report a handler - we are allowed to
10657a984708SDavid Chisnall 				// propagate this exception out.
10667a984708SDavid Chisnall 				if (check_type_signature(ex, handler_type, adjustedPtr))
10677a984708SDavid Chisnall 				{
10687a984708SDavid Chisnall 					matched = true;
10697a984708SDavid Chisnall 					break;
10707a984708SDavid Chisnall 				}
10717a984708SDavid Chisnall 			}
10727a984708SDavid Chisnall #endif
10737a984708SDavid Chisnall 			if (matched) { continue; }
10747a984708SDavid Chisnall 			// If we don't find an allowed exception spec, we need to install
10757a984708SDavid Chisnall 			// the context for this action.  The landing pad will then call the
10767a984708SDavid Chisnall 			// unexpected exception function.  Treat this as a catch
10777a984708SDavid Chisnall 			return handler_catch;
10787a984708SDavid Chisnall 		}
10797a984708SDavid Chisnall 		else if (filter == 0)
10807a984708SDavid Chisnall 		{
10817a984708SDavid Chisnall 			*selector = filter;
10827a984708SDavid Chisnall 			found = handler_cleanup;
10837a984708SDavid Chisnall 		}
10847a984708SDavid Chisnall 	}
10857a984708SDavid Chisnall 	return found;
10867a984708SDavid Chisnall }
10877a984708SDavid Chisnall 
pushCleanupException(_Unwind_Exception * exceptionObject,__cxa_exception * ex)10887a984708SDavid Chisnall static void pushCleanupException(_Unwind_Exception *exceptionObject,
10897a984708SDavid Chisnall                                  __cxa_exception *ex)
10907a984708SDavid Chisnall {
1091f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
10927a984708SDavid Chisnall 	__cxa_thread_info *info = thread_info_fast();
10937a984708SDavid Chisnall 	if (ex)
10947a984708SDavid Chisnall 	{
10957a984708SDavid Chisnall 		ex->cleanupCount++;
10967a984708SDavid Chisnall 		if (ex->cleanupCount > 1)
10977a984708SDavid Chisnall 		{
10987a984708SDavid Chisnall 			assert(exceptionObject == info->currentCleanup);
10997a984708SDavid Chisnall 			return;
11007a984708SDavid Chisnall 		}
11017a984708SDavid Chisnall 		ex->nextCleanup = info->currentCleanup;
11027a984708SDavid Chisnall 	}
11037a984708SDavid Chisnall 	info->currentCleanup = exceptionObject;
11047a984708SDavid Chisnall #endif
11057a984708SDavid Chisnall }
11067a984708SDavid Chisnall 
11077a984708SDavid Chisnall /**
11087a984708SDavid Chisnall  * The exception personality function.  This is referenced in the unwinding
11097a984708SDavid Chisnall  * DWARF metadata and is called by the unwind library for each C++ stack frame
11107a984708SDavid Chisnall  * containing catch or cleanup code.
11117a984708SDavid Chisnall  */
11127a984708SDavid Chisnall extern "C"
11137a984708SDavid Chisnall BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0)
11147a984708SDavid Chisnall 	// This personality function is for version 1 of the ABI.  If you use it
11157a984708SDavid Chisnall 	// with a future version of the ABI, it won't know what to do, so it
11167a984708SDavid Chisnall 	// reports a fatal error and give up before it breaks anything.
11177a984708SDavid Chisnall 	if (1 != version)
11187a984708SDavid Chisnall 	{
11197a984708SDavid Chisnall 		return _URC_FATAL_PHASE1_ERROR;
11207a984708SDavid Chisnall 	}
11217a984708SDavid Chisnall 	__cxa_exception *ex = 0;
11227a984708SDavid Chisnall 	__cxa_exception *realEx = 0;
11237a984708SDavid Chisnall 
11247a984708SDavid Chisnall 	// If this exception is throw by something else then we can't make any
11257a984708SDavid Chisnall 	// assumptions about its layout beyond the fields declared in
11267a984708SDavid Chisnall 	// _Unwind_Exception.
11277a984708SDavid Chisnall 	bool foreignException = !isCXXException(exceptionClass);
11287a984708SDavid Chisnall 
11297a984708SDavid Chisnall 	// If this isn't a foreign exception, then we have a C++ exception structure
11307a984708SDavid Chisnall 	if (!foreignException)
11317a984708SDavid Chisnall 	{
11327a984708SDavid Chisnall 		ex = exceptionFromPointer(exceptionObject);
11337a984708SDavid Chisnall 		realEx = realExceptionFromException(ex);
11347a984708SDavid Chisnall 	}
11357a984708SDavid Chisnall 
1136f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
11377a984708SDavid Chisnall 	unsigned char *lsda_addr =
1138f2dc4184SDimitry Andric 		static_cast<unsigned char*>(_Unwind_GetLanguageSpecificData(context));
1139f2dc4184SDimitry Andric #else
1140f2dc4184SDimitry Andric 	unsigned char *lsda_addr =
1141f2dc4184SDimitry Andric 		reinterpret_cast<unsigned char*>(static_cast<uintptr_t>(_Unwind_GetLanguageSpecificData(context)));
1142f2dc4184SDimitry Andric #endif
11437a984708SDavid Chisnall 
11447a984708SDavid Chisnall 	// No LSDA implies no landing pads - try the next frame
11457a984708SDavid Chisnall 	if (0 == lsda_addr) { return continueUnwinding(exceptionObject, context); }
11467a984708SDavid Chisnall 
11477a984708SDavid Chisnall 	// These two variables define how the exception will be handled.
11487a984708SDavid Chisnall 	dwarf_eh_action action = {0};
11497a984708SDavid Chisnall 	unsigned long selector = 0;
11507a984708SDavid Chisnall 
11517a984708SDavid Chisnall 	// During the search phase, we do a complete lookup.  If we return
11527a984708SDavid Chisnall 	// _URC_HANDLER_FOUND, then the phase 2 unwind will call this function with
11537a984708SDavid Chisnall 	// a _UA_HANDLER_FRAME action, telling us to install the handler frame.  If
11547a984708SDavid Chisnall 	// we return _URC_CONTINUE_UNWIND, we may be called again later with a
11557a984708SDavid Chisnall 	// _UA_CLEANUP_PHASE action for this frame.
11567a984708SDavid Chisnall 	//
11577a984708SDavid Chisnall 	// The point of the two-stage unwind allows us to entirely avoid any stack
11587a984708SDavid Chisnall 	// unwinding if there is no handler.  If there are just cleanups found,
11597a984708SDavid Chisnall 	// then we can just panic call an abort function.
11607a984708SDavid Chisnall 	//
11617a984708SDavid Chisnall 	// Matching a handler is much more expensive than matching a cleanup,
11627a984708SDavid Chisnall 	// because we don't need to bother doing type comparisons (or looking at
11637a984708SDavid Chisnall 	// the type table at all) for a cleanup.  This means that there is no need
11647a984708SDavid Chisnall 	// to cache the result of finding a cleanup, because it's (quite) quick to
11657a984708SDavid Chisnall 	// look it up again from the action table.
11667a984708SDavid Chisnall 	if (actions & _UA_SEARCH_PHASE)
11677a984708SDavid Chisnall 	{
11687a984708SDavid Chisnall 		struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
11697a984708SDavid Chisnall 
11707a984708SDavid Chisnall 		if (!dwarf_eh_find_callsite(context, &lsda, &action))
11717a984708SDavid Chisnall 		{
11727a984708SDavid Chisnall 			// EH range not found. This happens if exception is thrown and not
11737a984708SDavid Chisnall 			// caught inside a cleanup (destructor).  We should call
11747a984708SDavid Chisnall 			// terminate() in this case.  The catchTemp (landing pad) field of
11757a984708SDavid Chisnall 			// exception object will contain null when personality function is
11767a984708SDavid Chisnall 			// called with _UA_HANDLER_FRAME action for phase 2 unwinding.
11777a984708SDavid Chisnall 			return _URC_HANDLER_FOUND;
11787a984708SDavid Chisnall 		}
11797a984708SDavid Chisnall 
11807a984708SDavid Chisnall 		handler_type found_handler = check_action_record(context, &lsda,
11817a984708SDavid Chisnall 				action.action_record, realEx, &selector, ex->adjustedPtr);
11827a984708SDavid Chisnall 		// If there's no action record, we've only found a cleanup, so keep
11837a984708SDavid Chisnall 		// searching for something real
11847a984708SDavid Chisnall 		if (found_handler == handler_catch)
11857a984708SDavid Chisnall 		{
11867a984708SDavid Chisnall 			// Cache the results for the phase 2 unwind, if we found a handler
11877a984708SDavid Chisnall 			// and this is not a foreign exception.
11887a984708SDavid Chisnall 			if (ex)
11897a984708SDavid Chisnall 			{
11907a984708SDavid Chisnall 				saveLandingPad(context, exceptionObject, ex, selector, action.landing_pad);
1191f2dc4184SDimitry Andric 				ex->languageSpecificData = reinterpret_cast<const char*>(lsda_addr);
1192f2dc4184SDimitry Andric 				ex->actionRecord = reinterpret_cast<const char*>(action.action_record);
11937a984708SDavid Chisnall 				// ex->adjustedPtr is set when finding the action record.
11947a984708SDavid Chisnall 			}
11957a984708SDavid Chisnall 			return _URC_HANDLER_FOUND;
11967a984708SDavid Chisnall 		}
11977a984708SDavid Chisnall 		return continueUnwinding(exceptionObject, context);
11987a984708SDavid Chisnall 	}
11997a984708SDavid Chisnall 
12007a984708SDavid Chisnall 
12017a984708SDavid Chisnall 	// If this is a foreign exception, we didn't have anywhere to cache the
12027a984708SDavid Chisnall 	// lookup stuff, so we need to do it again.  If this is either a forced
12037a984708SDavid Chisnall 	// unwind, a foreign exception, or a cleanup, then we just install the
12047a984708SDavid Chisnall 	// context for a cleanup.
12057a984708SDavid Chisnall 	if (!(actions & _UA_HANDLER_FRAME))
12067a984708SDavid Chisnall 	{
12077a984708SDavid Chisnall 		// cleanup
12087a984708SDavid Chisnall 		struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
12097a984708SDavid Chisnall 		dwarf_eh_find_callsite(context, &lsda, &action);
12107a984708SDavid Chisnall 		if (0 == action.landing_pad) { return continueUnwinding(exceptionObject, context); }
12117a984708SDavid Chisnall 		handler_type found_handler = check_action_record(context, &lsda,
12127a984708SDavid Chisnall 				action.action_record, realEx, &selector, ex->adjustedPtr);
12137a984708SDavid Chisnall 		// Ignore handlers this time.
12147a984708SDavid Chisnall 		if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); }
12157a984708SDavid Chisnall 		pushCleanupException(exceptionObject, ex);
12167a984708SDavid Chisnall 	}
12177a984708SDavid Chisnall 	else if (foreignException)
12187a984708SDavid Chisnall 	{
12197a984708SDavid Chisnall 		struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
12207a984708SDavid Chisnall 		dwarf_eh_find_callsite(context, &lsda, &action);
12217a984708SDavid Chisnall 		check_action_record(context, &lsda, action.action_record, realEx,
12227a984708SDavid Chisnall 				&selector, ex->adjustedPtr);
12237a984708SDavid Chisnall 	}
12247a984708SDavid Chisnall 	else if (ex->catchTemp == 0)
12257a984708SDavid Chisnall 	{
12267a984708SDavid Chisnall 		// Uncaught exception in cleanup, calling terminate
12277a984708SDavid Chisnall 		std::terminate();
12287a984708SDavid Chisnall 	}
12297a984708SDavid Chisnall 	else
12307a984708SDavid Chisnall 	{
12317a984708SDavid Chisnall 		// Restore the saved info if we saved some last time.
12327a984708SDavid Chisnall 		loadLandingPad(context, exceptionObject, ex, &selector, &action.landing_pad);
12337a984708SDavid Chisnall 		ex->catchTemp = 0;
12347a984708SDavid Chisnall 		ex->handlerSwitchValue = 0;
12357a984708SDavid Chisnall 	}
12367a984708SDavid Chisnall 
12377a984708SDavid Chisnall 
1238f2dc4184SDimitry Andric 	_Unwind_SetIP(context, reinterpret_cast<unsigned long>(action.landing_pad));
12397a984708SDavid Chisnall 	_Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
1240f2dc4184SDimitry Andric 	              reinterpret_cast<unsigned long>(exceptionObject));
12417a984708SDavid Chisnall 	_Unwind_SetGR(context, __builtin_eh_return_data_regno(1), selector);
12427a984708SDavid Chisnall 
12437a984708SDavid Chisnall 	return _URC_INSTALL_CONTEXT;
12447a984708SDavid Chisnall }
12457a984708SDavid Chisnall 
12467a984708SDavid Chisnall /**
12477a984708SDavid Chisnall  * ABI function called when entering a catch statement.  The argument is the
12487a984708SDavid Chisnall  * pointer passed out of the personality function.  This is always the start of
12497a984708SDavid Chisnall  * the _Unwind_Exception object.  The return value for this function is the
12507a984708SDavid Chisnall  * pointer to the caught exception, which is either the adjusted pointer (for
12517a984708SDavid Chisnall  * C++ exceptions) of the unadjusted pointer (for foreign exceptions).
12527a984708SDavid Chisnall  */
125307122a2aSDimitry Andric #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
125413da1af1SEd Maste extern "C" void *__cxa_begin_catch(void *e) _LIBCXXRT_NOEXCEPT
12557a984708SDavid Chisnall #else
12567a984708SDavid Chisnall extern "C" void *__cxa_begin_catch(void *e)
12577a984708SDavid Chisnall #endif
12587a984708SDavid Chisnall {
1259f7cb1657SDavid Chisnall 	// We can't call the fast version here, because if the first exception that
1260f7cb1657SDavid Chisnall 	// we see is a foreign exception then we won't have called it yet.
1261f7cb1657SDavid Chisnall 	__cxa_thread_info *ti = thread_info();
1262f7cb1657SDavid Chisnall 	__cxa_eh_globals *globals = &ti->globals;
1263f2dc4184SDimitry Andric 	_Unwind_Exception *exceptionObject = static_cast<_Unwind_Exception*>(e);
12647a984708SDavid Chisnall 
12657a984708SDavid Chisnall 	if (isCXXException(exceptionObject->exception_class))
12667a984708SDavid Chisnall 	{
1267e91d723aSDimitry Andric 		// Only exceptions thrown with a C++ exception throwing function will
1268e91d723aSDimitry Andric 		// increment this, so don't decrement it here.
1269e91d723aSDimitry Andric 		globals->uncaughtExceptions--;
12707a984708SDavid Chisnall 		__cxa_exception *ex =  exceptionFromPointer(exceptionObject);
12717a984708SDavid Chisnall 
12727a984708SDavid Chisnall 		if (ex->handlerCount == 0)
12737a984708SDavid Chisnall 		{
12747a984708SDavid Chisnall 			// Add this to the front of the list of exceptions being handled
12757a984708SDavid Chisnall 			// and increment its handler count so that it won't be deleted
12767a984708SDavid Chisnall 			// prematurely.
12777a984708SDavid Chisnall 			ex->nextException = globals->caughtExceptions;
12787a984708SDavid Chisnall 			globals->caughtExceptions = ex;
12797a984708SDavid Chisnall 		}
12807a984708SDavid Chisnall 
12817a984708SDavid Chisnall 		if (ex->handlerCount < 0)
12827a984708SDavid Chisnall 		{
12837a984708SDavid Chisnall 			// Rethrown exception is catched before end of catch block.
12847a984708SDavid Chisnall 			// Clear the rethrow flag (make value positive) - we are allowed
12857a984708SDavid Chisnall 			// to delete this exception at the end of the catch block, as long
12867a984708SDavid Chisnall 			// as it isn't thrown again later.
12877a984708SDavid Chisnall 
12887a984708SDavid Chisnall 			// Code pattern:
12897a984708SDavid Chisnall 			//
12907a984708SDavid Chisnall 			// try {
12917a984708SDavid Chisnall 			//     throw x;
12927a984708SDavid Chisnall 			// }
12937a984708SDavid Chisnall 			// catch() {
12947a984708SDavid Chisnall 			//     try {
12957a984708SDavid Chisnall 			//         throw;
12967a984708SDavid Chisnall 			//     }
12977a984708SDavid Chisnall 			//     catch() {
12987a984708SDavid Chisnall 			//         __cxa_begin_catch() <- we are here
12997a984708SDavid Chisnall 			//     }
13007a984708SDavid Chisnall 			// }
13017a984708SDavid Chisnall 			ex->handlerCount = -ex->handlerCount + 1;
13027a984708SDavid Chisnall 		}
13037a984708SDavid Chisnall 		else
13047a984708SDavid Chisnall 		{
13057a984708SDavid Chisnall 			ex->handlerCount++;
13067a984708SDavid Chisnall 		}
1307f7cb1657SDavid Chisnall 		ti->foreign_exception_state = __cxa_thread_info::none;
13087a984708SDavid Chisnall 
13097a984708SDavid Chisnall 		return ex->adjustedPtr;
13107a984708SDavid Chisnall 	}
1311f7cb1657SDavid Chisnall 	else
1312f7cb1657SDavid Chisnall 	{
1313f7cb1657SDavid Chisnall 		// If this is a foreign exception, then we need to be able to
1314f7cb1657SDavid Chisnall 		// store it.  We can't chain foreign exceptions, so we give up
1315f7cb1657SDavid Chisnall 		// if there are already some outstanding ones.
1316f7cb1657SDavid Chisnall 		if (globals->caughtExceptions != 0)
1317f7cb1657SDavid Chisnall 		{
1318f7cb1657SDavid Chisnall 			std::terminate();
1319f7cb1657SDavid Chisnall 		}
1320f2dc4184SDimitry Andric 		globals->caughtExceptions = reinterpret_cast<__cxa_exception*>(exceptionObject);
1321f7cb1657SDavid Chisnall 		ti->foreign_exception_state = __cxa_thread_info::caught;
1322f7cb1657SDavid Chisnall 	}
13237a984708SDavid Chisnall 	// exceptionObject is the pointer to the _Unwind_Exception within the
13247a984708SDavid Chisnall 	// __cxa_exception.  The throw object is after this
1325f2dc4184SDimitry Andric 	return (reinterpret_cast<char*>(exceptionObject) + sizeof(_Unwind_Exception));
13267a984708SDavid Chisnall }
13277a984708SDavid Chisnall 
13287a984708SDavid Chisnall 
13297a984708SDavid Chisnall 
13307a984708SDavid Chisnall /**
13317a984708SDavid Chisnall  * ABI function called when exiting a catch block.  This will free the current
13327a984708SDavid Chisnall  * exception if it is no longer referenced in other catch blocks.
13337a984708SDavid Chisnall  */
13347a984708SDavid Chisnall extern "C" void __cxa_end_catch()
13357a984708SDavid Chisnall {
13367a984708SDavid Chisnall 	// We can call the fast version here because the slow version is called in
13377a984708SDavid Chisnall 	// __cxa_throw(), which must have been called before we end a catch block
1338f7cb1657SDavid Chisnall 	__cxa_thread_info *ti = thread_info_fast();
1339f7cb1657SDavid Chisnall 	__cxa_eh_globals *globals = &ti->globals;
13407a984708SDavid Chisnall 	__cxa_exception *ex = globals->caughtExceptions;
13417a984708SDavid Chisnall 
13427a984708SDavid Chisnall 	assert(0 != ex && "Ending catch when no exception is on the stack!");
13437a984708SDavid Chisnall 
1344f7cb1657SDavid Chisnall 	if (ti->foreign_exception_state != __cxa_thread_info::none)
1345f7cb1657SDavid Chisnall 	{
1346f7cb1657SDavid Chisnall 		if (ti->foreign_exception_state != __cxa_thread_info::rethrown)
1347f7cb1657SDavid Chisnall 		{
1348f2dc4184SDimitry Andric 			_Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(ti->globals.caughtExceptions);
1349076e75ebSDimitry Andric 			if (e->exception_cleanup)
1350f7cb1657SDavid Chisnall 				e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
1351f7cb1657SDavid Chisnall 		}
1352076e75ebSDimitry Andric 		globals->caughtExceptions = 0;
1353f7cb1657SDavid Chisnall 		ti->foreign_exception_state = __cxa_thread_info::none;
1354f7cb1657SDavid Chisnall 		return;
1355f7cb1657SDavid Chisnall 	}
1356f7cb1657SDavid Chisnall 
13577a984708SDavid Chisnall 	bool deleteException = true;
13587a984708SDavid Chisnall 
13597a984708SDavid Chisnall 	if (ex->handlerCount < 0)
13607a984708SDavid Chisnall 	{
13617a984708SDavid Chisnall 		// exception was rethrown. Exception should not be deleted even if
13627a984708SDavid Chisnall 		// handlerCount become zero.
13637a984708SDavid Chisnall 		// Code pattern:
13647a984708SDavid Chisnall 		// try {
13657a984708SDavid Chisnall 		//     throw x;
13667a984708SDavid Chisnall 		// }
13677a984708SDavid Chisnall 		// catch() {
13687a984708SDavid Chisnall 		//     {
13697a984708SDavid Chisnall 		//         throw;
13707a984708SDavid Chisnall 		//     }
13717a984708SDavid Chisnall 		//     cleanup {
13727a984708SDavid Chisnall 		//         __cxa_end_catch();   <- we are here
13737a984708SDavid Chisnall 		//     }
13747a984708SDavid Chisnall 		// }
13757a984708SDavid Chisnall 		//
13767a984708SDavid Chisnall 
13777a984708SDavid Chisnall 		ex->handlerCount++;
13787a984708SDavid Chisnall 		deleteException = false;
13797a984708SDavid Chisnall 	}
13807a984708SDavid Chisnall 	else
13817a984708SDavid Chisnall 	{
13827a984708SDavid Chisnall 		ex->handlerCount--;
13837a984708SDavid Chisnall 	}
13847a984708SDavid Chisnall 
13857a984708SDavid Chisnall 	if (ex->handlerCount == 0)
13867a984708SDavid Chisnall 	{
13877a984708SDavid Chisnall 		globals->caughtExceptions = ex->nextException;
13887a984708SDavid Chisnall 		if (deleteException)
13897a984708SDavid Chisnall 		{
13907a984708SDavid Chisnall 			releaseException(ex);
13917a984708SDavid Chisnall 		}
13927a984708SDavid Chisnall 	}
13937a984708SDavid Chisnall }
13947a984708SDavid Chisnall 
13957a984708SDavid Chisnall /**
13967a984708SDavid Chisnall  * ABI function.  Returns the type of the current exception.
13977a984708SDavid Chisnall  */
13987a984708SDavid Chisnall extern "C" std::type_info *__cxa_current_exception_type()
13997a984708SDavid Chisnall {
14007a984708SDavid Chisnall 	__cxa_eh_globals *globals = __cxa_get_globals();
14017a984708SDavid Chisnall 	__cxa_exception *ex = globals->caughtExceptions;
14027a984708SDavid Chisnall 	return ex ? ex->exceptionType : 0;
14037a984708SDavid Chisnall }
14047a984708SDavid Chisnall 
14057a984708SDavid Chisnall /**
1406e91d723aSDimitry Andric  * Cleanup, ensures that `__cxa_end_catch` is called to balance an explicit
1407e91d723aSDimitry Andric  * `__cxa_begin_catch` call.
1408e91d723aSDimitry Andric  */
1409e91d723aSDimitry Andric static void end_catch(char *)
1410e91d723aSDimitry Andric {
1411e91d723aSDimitry Andric 	__cxa_end_catch();
1412e91d723aSDimitry Andric }
1413e91d723aSDimitry Andric /**
14147a984708SDavid Chisnall  * ABI function, called when an exception specification is violated.
14157a984708SDavid Chisnall  *
14167a984708SDavid Chisnall  * This function does not return.
14177a984708SDavid Chisnall  */
14187a984708SDavid Chisnall extern "C" void __cxa_call_unexpected(void*exception)
14197a984708SDavid Chisnall {
1420f2dc4184SDimitry Andric 	_Unwind_Exception *exceptionObject = static_cast<_Unwind_Exception*>(exception);
1421e91d723aSDimitry Andric 	// Wrap the call to the unexpected handler in calls to `__cxa_begin_catch`
1422e91d723aSDimitry Andric 	// and `__cxa_end_catch` so that we correctly update exception counts if
1423e91d723aSDimitry Andric 	// the unexpected handler throws an exception.
1424e91d723aSDimitry Andric 	__cxa_begin_catch(exceptionObject);
1425e91d723aSDimitry Andric 	__attribute__((cleanup(end_catch)))
1426e91d723aSDimitry Andric 	char unused;
14277a984708SDavid Chisnall 	if (exceptionObject->exception_class == exception_class)
14287a984708SDavid Chisnall 	{
14297a984708SDavid Chisnall 		__cxa_exception *ex =  exceptionFromPointer(exceptionObject);
14307a984708SDavid Chisnall 		if (ex->unexpectedHandler)
14317a984708SDavid Chisnall 		{
14327a984708SDavid Chisnall 			ex->unexpectedHandler();
14337a984708SDavid Chisnall 			// Should not be reached.
14347a984708SDavid Chisnall 			abort();
14357a984708SDavid Chisnall 		}
14367a984708SDavid Chisnall 	}
14377a984708SDavid Chisnall 	std::unexpected();
14387a984708SDavid Chisnall 	// Should not be reached.
14397a984708SDavid Chisnall 	abort();
14407a984708SDavid Chisnall }
14417a984708SDavid Chisnall 
14427a984708SDavid Chisnall /**
1443297a9e55SJohn Baldwin  * ABI function, called when an object destructor exits due to an
1444297a9e55SJohn Baldwin  * exception during stack unwinding.
1445297a9e55SJohn Baldwin  *
1446297a9e55SJohn Baldwin  * This function does not return.
1447297a9e55SJohn Baldwin  */
144813da1af1SEd Maste extern "C" void __cxa_call_terminate(void*exception) _LIBCXXRT_NOEXCEPT
1449297a9e55SJohn Baldwin {
1450297a9e55SJohn Baldwin 	std::terminate();
1451297a9e55SJohn Baldwin 	// Should not be reached.
1452297a9e55SJohn Baldwin 	abort();
1453297a9e55SJohn Baldwin }
1454297a9e55SJohn Baldwin 
1455297a9e55SJohn Baldwin /**
14567a984708SDavid Chisnall  * ABI function, returns the adjusted pointer to the exception object.
14577a984708SDavid Chisnall  */
14587a984708SDavid Chisnall extern "C" void *__cxa_get_exception_ptr(void *exceptionObject)
14597a984708SDavid Chisnall {
14607a984708SDavid Chisnall 	return exceptionFromPointer(exceptionObject)->adjustedPtr;
14617a984708SDavid Chisnall }
14627a984708SDavid Chisnall 
14637a984708SDavid Chisnall /**
14647a984708SDavid Chisnall  * As an extension, we provide the ability for the unexpected and terminate
14657a984708SDavid Chisnall  * handlers to be thread-local.  We default to the standards-compliant
14667a984708SDavid Chisnall  * behaviour where they are global.
14677a984708SDavid Chisnall  */
14687a984708SDavid Chisnall static bool thread_local_handlers = false;
14697a984708SDavid Chisnall 
14707a984708SDavid Chisnall 
14717a984708SDavid Chisnall namespace pathscale
14727a984708SDavid Chisnall {
14737a984708SDavid Chisnall 	/**
14747a984708SDavid Chisnall 	 * Sets whether unexpected and terminate handlers should be thread-local.
14757a984708SDavid Chisnall 	 */
147613da1af1SEd Maste 	void set_use_thread_local_handlers(bool flag) _LIBCXXRT_NOEXCEPT
14777a984708SDavid Chisnall 	{
14787a984708SDavid Chisnall 		thread_local_handlers = flag;
14797a984708SDavid Chisnall 	}
14807a984708SDavid Chisnall 	/**
14817a984708SDavid Chisnall 	 * Sets a thread-local unexpected handler.
14827a984708SDavid Chisnall 	 */
148313da1af1SEd Maste 	unexpected_handler set_unexpected(unexpected_handler f) _LIBCXXRT_NOEXCEPT
14847a984708SDavid Chisnall 	{
14857a984708SDavid Chisnall 		static __cxa_thread_info *info = thread_info();
14867a984708SDavid Chisnall 		unexpected_handler old = info->unexpectedHandler;
14877a984708SDavid Chisnall 		info->unexpectedHandler = f;
14887a984708SDavid Chisnall 		return old;
14897a984708SDavid Chisnall 	}
14907a984708SDavid Chisnall 	/**
14917a984708SDavid Chisnall 	 * Sets a thread-local terminate handler.
14927a984708SDavid Chisnall 	 */
149313da1af1SEd Maste 	terminate_handler set_terminate(terminate_handler f) _LIBCXXRT_NOEXCEPT
14947a984708SDavid Chisnall 	{
14957a984708SDavid Chisnall 		static __cxa_thread_info *info = thread_info();
14967a984708SDavid Chisnall 		terminate_handler old = info->terminateHandler;
14977a984708SDavid Chisnall 		info->terminateHandler = f;
14987a984708SDavid Chisnall 		return old;
14997a984708SDavid Chisnall 	}
15007a984708SDavid Chisnall }
15017a984708SDavid Chisnall 
15027a984708SDavid Chisnall namespace std
15037a984708SDavid Chisnall {
15047a984708SDavid Chisnall 	/**
15057a984708SDavid Chisnall 	 * Sets the function that will be called when an exception specification is
15067a984708SDavid Chisnall 	 * violated.
15077a984708SDavid Chisnall 	 */
150813da1af1SEd Maste 	unexpected_handler set_unexpected(unexpected_handler f) _LIBCXXRT_NOEXCEPT
15097a984708SDavid Chisnall 	{
15107a984708SDavid Chisnall 		if (thread_local_handlers) { return pathscale::set_unexpected(f); }
15117a984708SDavid Chisnall 
1512bfffb66eSDimitry Andric 		return unexpectedHandler.exchange(f);
15137a984708SDavid Chisnall 	}
15147a984708SDavid Chisnall 	/**
15157a984708SDavid Chisnall 	 * Sets the function that is called to terminate the program.
15167a984708SDavid Chisnall 	 */
151713da1af1SEd Maste 	terminate_handler set_terminate(terminate_handler f) _LIBCXXRT_NOEXCEPT
15187a984708SDavid Chisnall 	{
15197a984708SDavid Chisnall 		if (thread_local_handlers) { return pathscale::set_terminate(f); }
1520f7cb1657SDavid Chisnall 
1521bfffb66eSDimitry Andric 		return terminateHandler.exchange(f);
15227a984708SDavid Chisnall 	}
15237a984708SDavid Chisnall 	/**
15247a984708SDavid Chisnall 	 * Terminates the program, calling a custom terminate implementation if
15257a984708SDavid Chisnall 	 * required.
15267a984708SDavid Chisnall 	 */
152713da1af1SEd Maste 	[[noreturn]] void terminate() _LIBCXXRT_NOEXCEPT
15287a984708SDavid Chisnall 	{
1529d9e22925SDimitry Andric 		static __cxa_thread_info *info = thread_info();
15307a984708SDavid Chisnall 		if (0 != info && 0 != info->terminateHandler)
15317a984708SDavid Chisnall 		{
15327a984708SDavid Chisnall 			info->terminateHandler();
15337a984708SDavid Chisnall 		}
1534*2dccd219SEd Maste 		else
1535*2dccd219SEd Maste 		{
1536bfffb66eSDimitry Andric 			terminateHandler.load()();
15377a984708SDavid Chisnall 		}
1538*2dccd219SEd Maste 		// Should not be reached - a terminate handler is not expected
1539*2dccd219SEd Maste 		// to return.
1540*2dccd219SEd Maste 		abort();
1541*2dccd219SEd Maste 	}
15427a984708SDavid Chisnall 	/**
15437a984708SDavid Chisnall 	 * Called when an unexpected exception is encountered (i.e. an exception
15447a984708SDavid Chisnall 	 * violates an exception specification).  This calls abort() unless a
15457a984708SDavid Chisnall 	 * custom handler has been set..
15467a984708SDavid Chisnall 	 */
1547*2dccd219SEd Maste 	[[noreturn]] void unexpected()
15487a984708SDavid Chisnall 	{
1549d9e22925SDimitry Andric 		static __cxa_thread_info *info = thread_info();
15507a984708SDavid Chisnall 		if (0 != info && 0 != info->unexpectedHandler)
15517a984708SDavid Chisnall 		{
15527a984708SDavid Chisnall 			info->unexpectedHandler();
15537a984708SDavid Chisnall 		}
1554*2dccd219SEd Maste 		else
1555*2dccd219SEd Maste 		{
1556bfffb66eSDimitry Andric 			unexpectedHandler.load()();
15577a984708SDavid Chisnall 		}
1558*2dccd219SEd Maste 		// Should not be reached - a unexpected handler is not expected
1559*2dccd219SEd Maste 		// to return.
1560*2dccd219SEd Maste 		abort();
1561*2dccd219SEd Maste 	}
15627a984708SDavid Chisnall 	/**
15637a984708SDavid Chisnall 	 * Returns whether there are any exceptions currently being thrown that
15647a984708SDavid Chisnall 	 * have not been caught.  This can occur inside a nested catch statement.
15657a984708SDavid Chisnall 	 */
156613da1af1SEd Maste 	bool uncaught_exception() _LIBCXXRT_NOEXCEPT
15677a984708SDavid Chisnall 	{
15687a984708SDavid Chisnall 		__cxa_thread_info *info = thread_info();
15697a984708SDavid Chisnall 		return info->globals.uncaughtExceptions != 0;
15707a984708SDavid Chisnall 	}
15717a984708SDavid Chisnall 	/**
1572bb52ed32SDimitry Andric 	 * Returns the number of exceptions currently being thrown that have not
1573bb52ed32SDimitry Andric 	 * been caught.  This can occur inside a nested catch statement.
1574bb52ed32SDimitry Andric 	 */
157513da1af1SEd Maste 	int uncaught_exceptions() _LIBCXXRT_NOEXCEPT
1576bb52ed32SDimitry Andric 	{
1577bb52ed32SDimitry Andric 		__cxa_thread_info *info = thread_info();
1578bb52ed32SDimitry Andric 		return info->globals.uncaughtExceptions;
1579bb52ed32SDimitry Andric 	}
1580bb52ed32SDimitry Andric 	/**
15817a984708SDavid Chisnall 	 * Returns the current unexpected handler.
15827a984708SDavid Chisnall 	 */
158313da1af1SEd Maste 	unexpected_handler get_unexpected() _LIBCXXRT_NOEXCEPT
15847a984708SDavid Chisnall 	{
15857a984708SDavid Chisnall 		__cxa_thread_info *info = thread_info();
15867a984708SDavid Chisnall 		if (info->unexpectedHandler)
15877a984708SDavid Chisnall 		{
15887a984708SDavid Chisnall 			return info->unexpectedHandler;
15897a984708SDavid Chisnall 		}
1590bfffb66eSDimitry Andric 		return unexpectedHandler.load();
15917a984708SDavid Chisnall 	}
15927a984708SDavid Chisnall 	/**
15937a984708SDavid Chisnall 	 * Returns the current terminate handler.
15947a984708SDavid Chisnall 	 */
159513da1af1SEd Maste 	terminate_handler get_terminate() _LIBCXXRT_NOEXCEPT
15967a984708SDavid Chisnall 	{
15977a984708SDavid Chisnall 		__cxa_thread_info *info = thread_info();
15987a984708SDavid Chisnall 		if (info->terminateHandler)
15997a984708SDavid Chisnall 		{
16007a984708SDavid Chisnall 			return info->terminateHandler;
16017a984708SDavid Chisnall 		}
1602bfffb66eSDimitry Andric 		return terminateHandler.load();
16037a984708SDavid Chisnall 	}
16047a984708SDavid Chisnall }
1605f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
16067a984708SDavid Chisnall extern "C" _Unwind_Exception *__cxa_get_cleanup(void)
16077a984708SDavid Chisnall {
16087a984708SDavid Chisnall 	__cxa_thread_info *info = thread_info_fast();
16097a984708SDavid Chisnall 	_Unwind_Exception *exceptionObject = info->currentCleanup;
16107a984708SDavid Chisnall 	if (isCXXException(exceptionObject->exception_class))
16117a984708SDavid Chisnall 	{
16127a984708SDavid Chisnall 		__cxa_exception *ex =  exceptionFromPointer(exceptionObject);
16137a984708SDavid Chisnall 		ex->cleanupCount--;
16147a984708SDavid Chisnall 		if (ex->cleanupCount == 0)
16157a984708SDavid Chisnall 		{
16167a984708SDavid Chisnall 			info->currentCleanup = ex->nextCleanup;
16177a984708SDavid Chisnall 			ex->nextCleanup = 0;
16187a984708SDavid Chisnall 		}
16197a984708SDavid Chisnall 	}
16207a984708SDavid Chisnall 	else
16217a984708SDavid Chisnall 	{
16227a984708SDavid Chisnall 		info->currentCleanup = 0;
16237a984708SDavid Chisnall 	}
16247a984708SDavid Chisnall 	return exceptionObject;
16257a984708SDavid Chisnall }
16267a984708SDavid Chisnall 
16277a984708SDavid Chisnall asm (
16287a984708SDavid Chisnall ".pushsection .text.__cxa_end_cleanup    \n"
16297a984708SDavid Chisnall ".global __cxa_end_cleanup               \n"
16307a984708SDavid Chisnall ".type __cxa_end_cleanup, \"function\"   \n"
16317a984708SDavid Chisnall "__cxa_end_cleanup:                      \n"
16327a984708SDavid Chisnall "	push {r1, r2, r3, r4}                \n"
1633cbd1e831SMark Johnston "	mov r4, lr                           \n"
16347a984708SDavid Chisnall "	bl __cxa_get_cleanup                 \n"
1635cbd1e831SMark Johnston "	mov lr, r4                           \n"
1636cbd1e831SMark Johnston "	pop {r1, r2, r3, r4}                 \n"
16377a984708SDavid Chisnall "	b _Unwind_Resume                     \n"
16387a984708SDavid Chisnall "	bl abort                             \n"
16397a984708SDavid Chisnall ".popsection                             \n"
16407a984708SDavid Chisnall );
16417a984708SDavid Chisnall #endif
1642