xref: /freebsd/contrib/libcxxrt/exception.cc (revision 3fb2e045)
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 
1243fb2e045SDimitry Andric extern "C" void __cxa_free_exception(void *thrown_exception) throw();
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 {
2017a984708SDavid Chisnall #if __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;
2207a984708SDavid Chisnall #if !__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 {
2407a984708SDavid Chisnall 	void unexpected();
2417a984708SDavid Chisnall 	class exception
2427a984708SDavid Chisnall 	{
2437a984708SDavid Chisnall 		public:
2447a984708SDavid Chisnall 			virtual ~exception() throw();
2457a984708SDavid Chisnall 			virtual const char* what() const throw();
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.
2997a984708SDavid Chisnall 	void terminate(void);
3007a984708SDavid Chisnall }
3017a984708SDavid Chisnall 
3027a984708SDavid Chisnall using namespace ABI_NAMESPACE;
3037a984708SDavid Chisnall 
3047a984708SDavid Chisnall 
3057a984708SDavid Chisnall 
3067a984708SDavid Chisnall /** The global termination handler. */
307bfffb66eSDimitry Andric static atomic<terminate_handler> terminateHandler = abort;
3087a984708SDavid Chisnall /** The global unexpected exception handler. */
309bfffb66eSDimitry Andric static atomic<unexpected_handler> unexpectedHandler = std::terminate;
3107a984708SDavid Chisnall 
3117a984708SDavid Chisnall /** Key used for thread-local data. */
3127a984708SDavid Chisnall static pthread_key_t eh_key;
3137a984708SDavid Chisnall 
3147a984708SDavid Chisnall 
3157a984708SDavid Chisnall /**
3167a984708SDavid Chisnall  * Cleanup function, allowing foreign exception handlers to correctly destroy
3177a984708SDavid Chisnall  * this exception if they catch it.
3187a984708SDavid Chisnall  */
exception_cleanup(_Unwind_Reason_Code reason,struct _Unwind_Exception * ex)3197a984708SDavid Chisnall static void exception_cleanup(_Unwind_Reason_Code reason,
3207a984708SDavid Chisnall                               struct _Unwind_Exception *ex)
3217a984708SDavid Chisnall {
322076e75ebSDimitry Andric 	// Exception layout:
323076e75ebSDimitry Andric 	// [__cxa_exception [_Unwind_Exception]] [exception object]
324076e75ebSDimitry Andric 	//
325076e75ebSDimitry Andric 	// __cxa_free_exception expects a pointer to the exception object
326076e75ebSDimitry Andric 	__cxa_free_exception(static_cast<void*>(ex + 1));
3277a984708SDavid Chisnall }
dependent_exception_cleanup(_Unwind_Reason_Code reason,struct _Unwind_Exception * ex)3287a984708SDavid Chisnall static void dependent_exception_cleanup(_Unwind_Reason_Code reason,
3297a984708SDavid Chisnall                               struct _Unwind_Exception *ex)
3307a984708SDavid Chisnall {
3317a984708SDavid Chisnall 
332076e75ebSDimitry Andric 	__cxa_free_dependent_exception(static_cast<void*>(ex + 1));
3337a984708SDavid Chisnall }
3347a984708SDavid Chisnall 
3357a984708SDavid Chisnall /**
3367a984708SDavid Chisnall  * Recursively walk a list of exceptions and delete them all in post-order.
3377a984708SDavid Chisnall  */
free_exception_list(__cxa_exception * ex)3387a984708SDavid Chisnall static void free_exception_list(__cxa_exception *ex)
3397a984708SDavid Chisnall {
3407a984708SDavid Chisnall 	if (0 != ex->nextException)
3417a984708SDavid Chisnall 	{
3427a984708SDavid Chisnall 		free_exception_list(ex->nextException);
3437a984708SDavid Chisnall 	}
3447a984708SDavid Chisnall 	// __cxa_free_exception() expects to be passed the thrown object, which
3457a984708SDavid Chisnall 	// immediately follows the exception, not the exception itself
3467a984708SDavid Chisnall 	__cxa_free_exception(ex+1);
3477a984708SDavid Chisnall }
3487a984708SDavid Chisnall 
3497a984708SDavid Chisnall /**
3507a984708SDavid Chisnall  * Cleanup function called when a thread exists to make certain that all of the
3517a984708SDavid Chisnall  * per-thread data is deleted.
3527a984708SDavid Chisnall  */
thread_cleanup(void * thread_info)3537a984708SDavid Chisnall static void thread_cleanup(void* thread_info)
3547a984708SDavid Chisnall {
355f2dc4184SDimitry Andric 	__cxa_thread_info *info = static_cast<__cxa_thread_info*>(thread_info);
3567a984708SDavid Chisnall 	if (info->globals.caughtExceptions)
3577a984708SDavid Chisnall 	{
358f7cb1657SDavid Chisnall 		// If this is a foreign exception, ask it to clean itself up.
359f7cb1657SDavid Chisnall 		if (info->foreign_exception_state != __cxa_thread_info::none)
360f7cb1657SDavid Chisnall 		{
361f2dc4184SDimitry Andric 			_Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(info->globals.caughtExceptions);
362076e75ebSDimitry Andric 			if (e->exception_cleanup)
363f7cb1657SDavid Chisnall 				e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
364f7cb1657SDavid Chisnall 		}
365f7cb1657SDavid Chisnall 		else
366f7cb1657SDavid Chisnall 		{
3677a984708SDavid Chisnall 			free_exception_list(info->globals.caughtExceptions);
3687a984708SDavid Chisnall 		}
369f7cb1657SDavid Chisnall 	}
3707a984708SDavid Chisnall 	free(thread_info);
3717a984708SDavid Chisnall }
3727a984708SDavid Chisnall 
3737a984708SDavid Chisnall 
3747a984708SDavid Chisnall /**
3757a984708SDavid Chisnall  * Once control used to protect the key creation.
3767a984708SDavid Chisnall  */
3777a984708SDavid Chisnall static pthread_once_t once_control = PTHREAD_ONCE_INIT;
3787a984708SDavid Chisnall 
3797a984708SDavid Chisnall /**
380db47c4bfSDavid Chisnall  * We may not be linked against a full pthread implementation.  If we're not,
381db47c4bfSDavid Chisnall  * then we need to fake the thread-local storage by storing 'thread-local'
382db47c4bfSDavid Chisnall  * things in a global.
383db47c4bfSDavid Chisnall  */
384db47c4bfSDavid Chisnall static bool fakeTLS;
385db47c4bfSDavid Chisnall /**
386db47c4bfSDavid Chisnall  * Thread-local storage for a single-threaded program.
387db47c4bfSDavid Chisnall  */
388db47c4bfSDavid Chisnall static __cxa_thread_info singleThreadInfo;
389db47c4bfSDavid Chisnall /**
3907a984708SDavid Chisnall  * Initialise eh_key.
3917a984708SDavid Chisnall  */
init_key(void)3927a984708SDavid Chisnall static void init_key(void)
3937a984708SDavid Chisnall {
394db47c4bfSDavid Chisnall 	if ((0 == pthread_key_create) ||
395db47c4bfSDavid Chisnall 	    (0 == pthread_setspecific) ||
396db47c4bfSDavid Chisnall 	    (0 == pthread_getspecific))
397db47c4bfSDavid Chisnall 	{
398db47c4bfSDavid Chisnall 		fakeTLS = true;
399db47c4bfSDavid Chisnall 		return;
400db47c4bfSDavid Chisnall 	}
4017a984708SDavid Chisnall 	pthread_key_create(&eh_key, thread_cleanup);
402f2dc4184SDimitry Andric 	pthread_setspecific(eh_key, reinterpret_cast<void *>(0x42));
403f2dc4184SDimitry Andric 	fakeTLS = (pthread_getspecific(eh_key) != reinterpret_cast<void *>(0x42));
404db47c4bfSDavid Chisnall 	pthread_setspecific(eh_key, 0);
4057a984708SDavid Chisnall }
4067a984708SDavid Chisnall 
4077a984708SDavid Chisnall /**
4087a984708SDavid Chisnall  * Returns the thread info structure, creating it if it is not already created.
4097a984708SDavid Chisnall  */
thread_info()4107a984708SDavid Chisnall static __cxa_thread_info *thread_info()
4117a984708SDavid Chisnall {
412db47c4bfSDavid Chisnall 	if ((0 == pthread_once) || pthread_once(&once_control, init_key))
413db47c4bfSDavid Chisnall 	{
414db47c4bfSDavid Chisnall 		fakeTLS = true;
415db47c4bfSDavid Chisnall 	}
416db47c4bfSDavid Chisnall 	if (fakeTLS) { return &singleThreadInfo; }
417f2dc4184SDimitry Andric 	__cxa_thread_info *info = static_cast<__cxa_thread_info*>(pthread_getspecific(eh_key));
4187a984708SDavid Chisnall 	if (0 == info)
4197a984708SDavid Chisnall 	{
420f2dc4184SDimitry Andric 		info = static_cast<__cxa_thread_info*>(calloc(1, sizeof(__cxa_thread_info)));
4217a984708SDavid Chisnall 		pthread_setspecific(eh_key, info);
4227a984708SDavid Chisnall 	}
4237a984708SDavid Chisnall 	return info;
4247a984708SDavid Chisnall }
4257a984708SDavid Chisnall /**
4267a984708SDavid Chisnall  * Fast version of thread_info().  May fail if thread_info() is not called on
4277a984708SDavid Chisnall  * this thread at least once already.
4287a984708SDavid Chisnall  */
thread_info_fast()4297a984708SDavid Chisnall static __cxa_thread_info *thread_info_fast()
4307a984708SDavid Chisnall {
431db47c4bfSDavid Chisnall 	if (fakeTLS) { return &singleThreadInfo; }
432f2dc4184SDimitry Andric 	return static_cast<__cxa_thread_info*>(pthread_getspecific(eh_key));
4337a984708SDavid Chisnall }
4347a984708SDavid Chisnall /**
4357a984708SDavid Chisnall  * ABI function returning the __cxa_eh_globals structure.
4367a984708SDavid Chisnall  */
__cxa_get_globals(void)4377a984708SDavid Chisnall extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals(void)
4387a984708SDavid Chisnall {
4397a984708SDavid Chisnall 	return &(thread_info()->globals);
4407a984708SDavid Chisnall }
4417a984708SDavid Chisnall /**
4427a984708SDavid Chisnall  * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already
4437a984708SDavid Chisnall  * been called at least once by this thread.
4447a984708SDavid Chisnall  */
__cxa_get_globals_fast(void)4457a984708SDavid Chisnall extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals_fast(void)
4467a984708SDavid Chisnall {
4477a984708SDavid Chisnall 	return &(thread_info_fast()->globals);
4487a984708SDavid Chisnall }
4497a984708SDavid Chisnall 
450ecf41062SDimitry Andric #ifdef LIBCXXRT_NO_EMERGENCY_MALLOC
alloc_or_die(size_t size)451ecf41062SDimitry Andric static char *alloc_or_die(size_t size)
452ecf41062SDimitry Andric {
453ecf41062SDimitry Andric 	char *buffer = static_cast<char*>(calloc(1, size));
454ecf41062SDimitry Andric 
455ecf41062SDimitry Andric 	if (buffer == nullptr)
456ecf41062SDimitry Andric 	{
457ecf41062SDimitry Andric 		fputs("Out of memory attempting to allocate exception\n", stderr);
458ecf41062SDimitry Andric 		std::terminate();
459ecf41062SDimitry Andric 	}
460ecf41062SDimitry Andric 	return buffer;
461ecf41062SDimitry Andric }
free_exception(char * e)462ecf41062SDimitry Andric static void free_exception(char *e)
463ecf41062SDimitry Andric {
464ecf41062SDimitry Andric 	free(e);
465ecf41062SDimitry Andric }
466ecf41062SDimitry Andric #else
4677a984708SDavid Chisnall /**
4687a984708SDavid Chisnall  * An emergency allocation reserved for when malloc fails.  This is treated as
4697a984708SDavid Chisnall  * 16 buffers of 1KB each.
4707a984708SDavid Chisnall  */
4717a984708SDavid Chisnall static char emergency_buffer[16384];
4727a984708SDavid Chisnall /**
4737a984708SDavid Chisnall  * Flag indicating whether each buffer is allocated.
4747a984708SDavid Chisnall  */
4757a984708SDavid Chisnall static bool buffer_allocated[16];
4767a984708SDavid Chisnall /**
4777a984708SDavid Chisnall  * Lock used to protect emergency allocation.
4787a984708SDavid Chisnall  */
4797a984708SDavid Chisnall static pthread_mutex_t emergency_malloc_lock = PTHREAD_MUTEX_INITIALIZER;
4807a984708SDavid Chisnall /**
4817a984708SDavid Chisnall  * Condition variable used to wait when two threads are both trying to use the
4827a984708SDavid Chisnall  * emergency malloc() buffer at once.
4837a984708SDavid Chisnall  */
4847a984708SDavid Chisnall static pthread_cond_t emergency_malloc_wait = PTHREAD_COND_INITIALIZER;
4857a984708SDavid Chisnall 
4867a984708SDavid Chisnall /**
4877a984708SDavid Chisnall  * Allocates size bytes from the emergency allocation mechanism, if possible.
4887a984708SDavid Chisnall  * This function will fail if size is over 1KB or if this thread already has 4
4897a984708SDavid Chisnall  * emergency buffers.  If all emergency buffers are allocated, it will sleep
4907a984708SDavid Chisnall  * until one becomes available.
4917a984708SDavid Chisnall  */
emergency_malloc(size_t size)4927a984708SDavid Chisnall static char *emergency_malloc(size_t size)
4937a984708SDavid Chisnall {
4947a984708SDavid Chisnall 	if (size > 1024) { return 0; }
4957a984708SDavid Chisnall 
4967a984708SDavid Chisnall 	__cxa_thread_info *info = thread_info();
4977a984708SDavid Chisnall 	// Only 4 emergency buffers allowed per thread!
4987a984708SDavid Chisnall 	if (info->emergencyBuffersHeld > 3) { return 0; }
4997a984708SDavid Chisnall 
5007a984708SDavid Chisnall 	pthread_mutex_lock(&emergency_malloc_lock);
5017a984708SDavid Chisnall 	int buffer = -1;
5027a984708SDavid Chisnall 	while (buffer < 0)
5037a984708SDavid Chisnall 	{
5047a984708SDavid Chisnall 		// While we were sleeping on the lock, another thread might have free'd
5057a984708SDavid Chisnall 		// enough memory for us to use, so try the allocation again - no point
5067a984708SDavid Chisnall 		// using the emergency buffer if there is some real memory that we can
5077a984708SDavid Chisnall 		// use...
5087a984708SDavid Chisnall 		void *m = calloc(1, size);
5097a984708SDavid Chisnall 		if (0 != m)
5107a984708SDavid Chisnall 		{
5117a984708SDavid Chisnall 			pthread_mutex_unlock(&emergency_malloc_lock);
512f2dc4184SDimitry Andric 			return static_cast<char*>(m);
5137a984708SDavid Chisnall 		}
5147a984708SDavid Chisnall 		for (int i=0 ; i<16 ; i++)
5157a984708SDavid Chisnall 		{
5167a984708SDavid Chisnall 			if (!buffer_allocated[i])
5177a984708SDavid Chisnall 			{
5187a984708SDavid Chisnall 				buffer = i;
5197a984708SDavid Chisnall 				buffer_allocated[i] = true;
5207a984708SDavid Chisnall 				break;
5217a984708SDavid Chisnall 			}
5227a984708SDavid Chisnall 		}
5237a984708SDavid Chisnall 		// If there still isn't a buffer available, then sleep on the condition
5247a984708SDavid Chisnall 		// variable.  This will be signalled when another thread releases one
5257a984708SDavid Chisnall 		// of the emergency buffers.
5267a984708SDavid Chisnall 		if (buffer < 0)
5277a984708SDavid Chisnall 		{
5287a984708SDavid Chisnall 			pthread_cond_wait(&emergency_malloc_wait, &emergency_malloc_lock);
5297a984708SDavid Chisnall 		}
5307a984708SDavid Chisnall 	}
5317a984708SDavid Chisnall 	pthread_mutex_unlock(&emergency_malloc_lock);
5327a984708SDavid Chisnall 	info->emergencyBuffersHeld++;
5337a984708SDavid Chisnall 	return emergency_buffer + (1024 * buffer);
5347a984708SDavid Chisnall }
5357a984708SDavid Chisnall 
5367a984708SDavid Chisnall /**
5377a984708SDavid Chisnall  * Frees a buffer returned by emergency_malloc().
5387a984708SDavid Chisnall  *
5397a984708SDavid Chisnall  * Note: Neither this nor emergency_malloc() is particularly efficient.  This
5407a984708SDavid Chisnall  * should not matter, because neither will be called in normal operation - they
5417a984708SDavid Chisnall  * are only used when the program runs out of memory, which should not happen
5427a984708SDavid Chisnall  * often.
5437a984708SDavid Chisnall  */
emergency_malloc_free(char * ptr)5447a984708SDavid Chisnall static void emergency_malloc_free(char *ptr)
5457a984708SDavid Chisnall {
5467a984708SDavid Chisnall 	int buffer = -1;
5477a984708SDavid Chisnall 	// Find the buffer corresponding to this pointer.
5487a984708SDavid Chisnall 	for (int i=0 ; i<16 ; i++)
5497a984708SDavid Chisnall 	{
550f2dc4184SDimitry Andric 		if (ptr == static_cast<void*>(emergency_buffer + (1024 * i)))
5517a984708SDavid Chisnall 		{
5527a984708SDavid Chisnall 			buffer = i;
5537a984708SDavid Chisnall 			break;
5547a984708SDavid Chisnall 		}
5557a984708SDavid Chisnall 	}
55676ad5667SConrad Meyer 	assert(buffer >= 0 &&
5577a984708SDavid Chisnall 	       "Trying to free something that is not an emergency buffer!");
5587a984708SDavid Chisnall 	// emergency_malloc() is expected to return 0-initialized data.  We don't
5597a984708SDavid Chisnall 	// zero the buffer when allocating it, because the static buffers will
5607a984708SDavid Chisnall 	// begin life containing 0 values.
561f2dc4184SDimitry Andric 	memset(ptr, 0, 1024);
5627a984708SDavid Chisnall 	// Signal the condition variable to wake up any threads that are blocking
5637a984708SDavid Chisnall 	// waiting for some space in the emergency buffer
5647a984708SDavid Chisnall 	pthread_mutex_lock(&emergency_malloc_lock);
5657a984708SDavid Chisnall 	// In theory, we don't need to do this with the lock held.  In practice,
5667a984708SDavid Chisnall 	// our array of bools will probably be updated using 32-bit or 64-bit
5677a984708SDavid Chisnall 	// memory operations, so this update may clobber adjacent values.
5687a984708SDavid Chisnall 	buffer_allocated[buffer] = false;
5697a984708SDavid Chisnall 	pthread_cond_signal(&emergency_malloc_wait);
5707a984708SDavid Chisnall 	pthread_mutex_unlock(&emergency_malloc_lock);
5717a984708SDavid Chisnall }
5727a984708SDavid Chisnall 
alloc_or_die(size_t size)5737a984708SDavid Chisnall static char *alloc_or_die(size_t size)
5747a984708SDavid Chisnall {
575f2dc4184SDimitry Andric 	char *buffer = static_cast<char*>(calloc(1, size));
5767a984708SDavid Chisnall 
5777a984708SDavid Chisnall 	// If calloc() doesn't want to give us any memory, try using an emergency
5787a984708SDavid Chisnall 	// buffer.
5797a984708SDavid Chisnall 	if (0 == buffer)
5807a984708SDavid Chisnall 	{
5817a984708SDavid Chisnall 		buffer = emergency_malloc(size);
5827a984708SDavid Chisnall 		// This is only reached if the allocation is greater than 1KB, and
5837a984708SDavid Chisnall 		// anyone throwing objects that big really should know better.
5847a984708SDavid Chisnall 		if (0 == buffer)
5857a984708SDavid Chisnall 		{
5867a984708SDavid Chisnall 			fprintf(stderr, "Out of memory attempting to allocate exception\n");
5877a984708SDavid Chisnall 			std::terminate();
5887a984708SDavid Chisnall 		}
5897a984708SDavid Chisnall 	}
5907a984708SDavid Chisnall 	return buffer;
5917a984708SDavid Chisnall }
free_exception(char * e)5927a984708SDavid Chisnall static void free_exception(char *e)
5937a984708SDavid Chisnall {
5947a984708SDavid Chisnall 	// If this allocation is within the address range of the emergency buffer,
5957a984708SDavid Chisnall 	// don't call free() because it was not allocated with malloc()
59676ad5667SConrad Meyer 	if ((e >= emergency_buffer) &&
5977a984708SDavid Chisnall 	    (e < (emergency_buffer + sizeof(emergency_buffer))))
5987a984708SDavid Chisnall 	{
5997a984708SDavid Chisnall 		emergency_malloc_free(e);
6007a984708SDavid Chisnall 	}
6017a984708SDavid Chisnall 	else
6027a984708SDavid Chisnall 	{
6037a984708SDavid Chisnall 		free(e);
6047a984708SDavid Chisnall 	}
6057a984708SDavid Chisnall }
606ecf41062SDimitry Andric #endif
6077a984708SDavid Chisnall 
6087a984708SDavid Chisnall /**
6097a984708SDavid Chisnall  * Allocates an exception structure.  Returns a pointer to the space that can
6107a984708SDavid Chisnall  * be used to store an object of thrown_size bytes.  This function will use an
6117a984708SDavid Chisnall  * emergency buffer if malloc() fails, and may block if there are no such
6127a984708SDavid Chisnall  * buffers available.
6137a984708SDavid Chisnall  */
__cxa_allocate_exception(size_t thrown_size)6143fb2e045SDimitry Andric extern "C" void *__cxa_allocate_exception(size_t thrown_size) throw()
6157a984708SDavid Chisnall {
61672df847aSDimitry Andric 	size_t size = thrown_size + sizeof(__cxa_exception);
6177a984708SDavid Chisnall 	char *buffer = alloc_or_die(size);
61872df847aSDimitry Andric 	return buffer+sizeof(__cxa_exception);
6197a984708SDavid Chisnall }
6207a984708SDavid Chisnall 
__cxa_allocate_dependent_exception(void)6217a984708SDavid Chisnall extern "C" void *__cxa_allocate_dependent_exception(void)
6227a984708SDavid Chisnall {
62372df847aSDimitry Andric 	size_t size = sizeof(__cxa_dependent_exception);
6247a984708SDavid Chisnall 	char *buffer = alloc_or_die(size);
62572df847aSDimitry Andric 	return buffer+sizeof(__cxa_dependent_exception);
6267a984708SDavid Chisnall }
6277a984708SDavid Chisnall 
6287a984708SDavid Chisnall /**
6297a984708SDavid Chisnall  * __cxa_free_exception() is called when an exception was thrown in between
6307a984708SDavid Chisnall  * calling __cxa_allocate_exception() and actually throwing the exception.
6317a984708SDavid Chisnall  * This happens when the object's copy constructor throws an exception.
6327a984708SDavid Chisnall  *
6337a984708SDavid Chisnall  * In this implementation, it is also called by __cxa_end_catch() and during
6347a984708SDavid Chisnall  * thread cleanup.
6357a984708SDavid Chisnall  */
__cxa_free_exception(void * thrown_exception)6363fb2e045SDimitry Andric extern "C" void __cxa_free_exception(void *thrown_exception) throw()
6377a984708SDavid Chisnall {
638f2dc4184SDimitry Andric 	__cxa_exception *ex = reinterpret_cast<__cxa_exception*>(thrown_exception) - 1;
6397a984708SDavid Chisnall 	// Free the object that was thrown, calling its destructor
6407a984708SDavid Chisnall 	if (0 != ex->exceptionDestructor)
6417a984708SDavid Chisnall 	{
6427a984708SDavid Chisnall 		try
6437a984708SDavid Chisnall 		{
6447a984708SDavid Chisnall 			ex->exceptionDestructor(thrown_exception);
6457a984708SDavid Chisnall 		}
6467a984708SDavid Chisnall 		catch(...)
6477a984708SDavid Chisnall 		{
6487a984708SDavid Chisnall 			// FIXME: Check that this is really what the spec says to do.
6497a984708SDavid Chisnall 			std::terminate();
6507a984708SDavid Chisnall 		}
6517a984708SDavid Chisnall 	}
6527a984708SDavid Chisnall 
65372df847aSDimitry Andric 	free_exception(reinterpret_cast<char*>(ex));
6547a984708SDavid Chisnall }
6557a984708SDavid Chisnall 
releaseException(__cxa_exception * exception)6567a984708SDavid Chisnall static void releaseException(__cxa_exception *exception)
6577a984708SDavid Chisnall {
6587a984708SDavid Chisnall 	if (isDependentException(exception->unwindHeader.exception_class))
6597a984708SDavid Chisnall 	{
6607a984708SDavid Chisnall 		__cxa_free_dependent_exception(exception+1);
6617a984708SDavid Chisnall 		return;
6627a984708SDavid Chisnall 	}
6637a984708SDavid Chisnall 	if (__sync_sub_and_fetch(&exception->referenceCount, 1) == 0)
6647a984708SDavid Chisnall 	{
6657a984708SDavid Chisnall 		// __cxa_free_exception() expects to be passed the thrown object,
6667a984708SDavid Chisnall 		// which immediately follows the exception, not the exception
6677a984708SDavid Chisnall 		// itself
6687a984708SDavid Chisnall 		__cxa_free_exception(exception+1);
6697a984708SDavid Chisnall 	}
6707a984708SDavid Chisnall }
6717a984708SDavid Chisnall 
__cxa_free_dependent_exception(void * thrown_exception)6727a984708SDavid Chisnall void __cxa_free_dependent_exception(void *thrown_exception)
6737a984708SDavid Chisnall {
674f2dc4184SDimitry Andric 	__cxa_dependent_exception *ex = reinterpret_cast<__cxa_dependent_exception*>(thrown_exception) - 1;
6757a984708SDavid Chisnall 	assert(isDependentException(ex->unwindHeader.exception_class));
6767a984708SDavid Chisnall 	if (ex->primaryException)
6777a984708SDavid Chisnall 	{
678f2dc4184SDimitry Andric 		releaseException(realExceptionFromException(reinterpret_cast<__cxa_exception*>(ex)));
6797a984708SDavid Chisnall 	}
68072df847aSDimitry Andric 	free_exception(reinterpret_cast<char*>(ex));
6817a984708SDavid Chisnall }
6827a984708SDavid Chisnall 
6837a984708SDavid Chisnall /**
6847a984708SDavid Chisnall  * Callback function used with _Unwind_Backtrace().
6857a984708SDavid Chisnall  *
6867a984708SDavid Chisnall  * Prints a stack trace.  Used only for debugging help.
6877a984708SDavid Chisnall  *
6887a984708SDavid Chisnall  * Note: As of FreeBSD 8.1, dladd() still doesn't work properly, so this only
6897a984708SDavid Chisnall  * correctly prints function names from public, relocatable, symbols.
6907a984708SDavid Chisnall  */
trace(struct _Unwind_Context * context,void * c)6917a984708SDavid Chisnall static _Unwind_Reason_Code trace(struct _Unwind_Context *context, void *c)
6927a984708SDavid Chisnall {
6937a984708SDavid Chisnall 	Dl_info myinfo;
6947a984708SDavid Chisnall 	int mylookup =
695f2dc4184SDimitry Andric 		dladdr(reinterpret_cast<void *>(__cxa_current_exception_type), &myinfo);
696f2dc4184SDimitry Andric 	void *ip = reinterpret_cast<void*>(_Unwind_GetIP(context));
6977a984708SDavid Chisnall 	Dl_info info;
6987a984708SDavid Chisnall 	if (dladdr(ip, &info) != 0)
6997a984708SDavid Chisnall 	{
7007a984708SDavid Chisnall 		if (mylookup == 0 || strcmp(info.dli_fname, myinfo.dli_fname) != 0)
7017a984708SDavid Chisnall 		{
7027a984708SDavid Chisnall 			printf("%p:%s() in %s\n", ip, info.dli_sname, info.dli_fname);
7037a984708SDavid Chisnall 		}
7047a984708SDavid Chisnall 	}
7057a984708SDavid Chisnall 	return _URC_CONTINUE_UNWIND;
7067a984708SDavid Chisnall }
7077a984708SDavid Chisnall 
7087a984708SDavid Chisnall /**
7097a984708SDavid Chisnall  * Report a failure that occurred when attempting to throw an exception.
7107a984708SDavid Chisnall  *
7117a984708SDavid Chisnall  * If the failure happened by falling off the end of the stack without finding
7127a984708SDavid Chisnall  * a handler, prints a back trace before aborting.
7137a984708SDavid Chisnall  */
71407122a2aSDimitry Andric #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
715f2dc4184SDimitry Andric extern "C" void *__cxa_begin_catch(void *e) throw();
716f2dc4184SDimitry Andric #else
717f2dc4184SDimitry Andric extern "C" void *__cxa_begin_catch(void *e);
718f2dc4184SDimitry Andric #endif
report_failure(_Unwind_Reason_Code err,__cxa_exception * thrown_exception)7197a984708SDavid Chisnall static void report_failure(_Unwind_Reason_Code err, __cxa_exception *thrown_exception)
7207a984708SDavid Chisnall {
7217a984708SDavid Chisnall 	switch (err)
7227a984708SDavid Chisnall 	{
7237a984708SDavid Chisnall 		default: break;
7247a984708SDavid Chisnall 		case _URC_FATAL_PHASE1_ERROR:
7257a984708SDavid Chisnall 			fprintf(stderr, "Fatal error during phase 1 unwinding\n");
7267a984708SDavid Chisnall 			break;
727f2dc4184SDimitry Andric #if !defined(__arm__) || defined(__ARM_DWARF_EH__)
7287a984708SDavid Chisnall 		case _URC_FATAL_PHASE2_ERROR:
7297a984708SDavid Chisnall 			fprintf(stderr, "Fatal error during phase 2 unwinding\n");
7307a984708SDavid Chisnall 			break;
7317a984708SDavid Chisnall #endif
7327a984708SDavid Chisnall 		case _URC_END_OF_STACK:
733f2dc4184SDimitry Andric 			__cxa_begin_catch (&(thrown_exception->unwindHeader));
734f2dc4184SDimitry Andric  			std::terminate();
7357a984708SDavid Chisnall 			fprintf(stderr, "Terminating due to uncaught exception %p",
736f2dc4184SDimitry Andric 					static_cast<void*>(thrown_exception));
7377a984708SDavid Chisnall 			thrown_exception = realExceptionFromException(thrown_exception);
7387a984708SDavid Chisnall 			static const __class_type_info *e_ti =
7397a984708SDavid Chisnall 				static_cast<const __class_type_info*>(&typeid(std::exception));
7407a984708SDavid Chisnall 			const __class_type_info *throw_ti =
7417a984708SDavid Chisnall 				dynamic_cast<const __class_type_info*>(thrown_exception->exceptionType);
7427a984708SDavid Chisnall 			if (throw_ti)
7437a984708SDavid Chisnall 			{
7447a984708SDavid Chisnall 				std::exception *e =
745f2dc4184SDimitry Andric 					static_cast<std::exception*>(e_ti->cast_to(static_cast<void*>(thrown_exception+1),
746f2dc4184SDimitry Andric 							throw_ti));
7477a984708SDavid Chisnall 				if (e)
7487a984708SDavid Chisnall 				{
7497a984708SDavid Chisnall 					fprintf(stderr, " '%s'", e->what());
7507a984708SDavid Chisnall 				}
7517a984708SDavid Chisnall 			}
7527a984708SDavid Chisnall 
7537a984708SDavid Chisnall 			size_t bufferSize = 128;
754f2dc4184SDimitry Andric 			char *demangled = static_cast<char*>(malloc(bufferSize));
7557a984708SDavid Chisnall 			const char *mangled = thrown_exception->exceptionType->name();
7567a984708SDavid Chisnall 			int status;
7577a984708SDavid Chisnall 			demangled = __cxa_demangle(mangled, demangled, &bufferSize, &status);
7587a984708SDavid Chisnall 			fprintf(stderr, " of type %s\n",
759f2dc4184SDimitry Andric 				status == 0 ? demangled : mangled);
7607a984708SDavid Chisnall 			if (status == 0) { free(demangled); }
7617a984708SDavid Chisnall 			// Print a back trace if no handler is found.
7627a984708SDavid Chisnall 			// TODO: Make this optional
76326b8b75cSDavid Chisnall #ifndef __arm__
7647a984708SDavid Chisnall 			_Unwind_Backtrace(trace, 0);
76526b8b75cSDavid Chisnall #endif
766f2dc4184SDimitry Andric 
767f2dc4184SDimitry Andric 			// Just abort. No need to call std::terminate for the second time
768f2dc4184SDimitry Andric 			abort();
7697a984708SDavid Chisnall 			break;
7707a984708SDavid Chisnall 	}
7717a984708SDavid Chisnall 	std::terminate();
7727a984708SDavid Chisnall }
7737a984708SDavid Chisnall 
throw_exception(__cxa_exception * ex)7747a984708SDavid Chisnall static void throw_exception(__cxa_exception *ex)
7757a984708SDavid Chisnall {
7767a984708SDavid Chisnall 	__cxa_thread_info *info = thread_info();
7777a984708SDavid Chisnall 	ex->unexpectedHandler = info->unexpectedHandler;
7787a984708SDavid Chisnall 	if (0 == ex->unexpectedHandler)
7797a984708SDavid Chisnall 	{
780bfffb66eSDimitry Andric 		ex->unexpectedHandler = unexpectedHandler.load();
7817a984708SDavid Chisnall 	}
7827a984708SDavid Chisnall 	ex->terminateHandler  = info->terminateHandler;
7837a984708SDavid Chisnall 	if (0 == ex->terminateHandler)
7847a984708SDavid Chisnall 	{
785bfffb66eSDimitry Andric 		ex->terminateHandler = terminateHandler.load();
7867a984708SDavid Chisnall 	}
7877a984708SDavid Chisnall 	info->globals.uncaughtExceptions++;
7887a984708SDavid Chisnall 
7897a984708SDavid Chisnall 	_Unwind_Reason_Code err = _Unwind_RaiseException(&ex->unwindHeader);
7907a984708SDavid Chisnall 	// The _Unwind_RaiseException() function should not return, it should
7917a984708SDavid Chisnall 	// unwind the stack past this function.  If it does return, then something
7927a984708SDavid Chisnall 	// has gone wrong.
7937a984708SDavid Chisnall 	report_failure(err, ex);
7947a984708SDavid Chisnall }
7957a984708SDavid Chisnall 
__cxa_init_primary_exception(void * object,std::type_info * tinfo,void (* dest)(void *))796ecf41062SDimitry Andric extern "C" __cxa_exception *__cxa_init_primary_exception(
7973fb2e045SDimitry Andric 		void *object, std::type_info* tinfo, void (*dest)(void *)) throw() {
798ecf41062SDimitry Andric 	__cxa_exception *ex = reinterpret_cast<__cxa_exception*>(object) - 1;
799ecf41062SDimitry Andric 
800ecf41062SDimitry Andric 	ex->referenceCount = 0;
801ecf41062SDimitry Andric 	ex->exceptionType = tinfo;
802ecf41062SDimitry Andric 
803ecf41062SDimitry Andric 	ex->exceptionDestructor = dest;
804ecf41062SDimitry Andric 
805ecf41062SDimitry Andric 	ex->unwindHeader.exception_class = exception_class;
806ecf41062SDimitry Andric 	ex->unwindHeader.exception_cleanup = exception_cleanup;
807ecf41062SDimitry Andric 
808ecf41062SDimitry Andric 	return ex;
809ecf41062SDimitry Andric }
810ecf41062SDimitry Andric 
8117a984708SDavid Chisnall 
8127a984708SDavid Chisnall /**
8137a984708SDavid Chisnall  * ABI function for throwing an exception.  Takes the object to be thrown (the
8147a984708SDavid Chisnall  * pointer returned by __cxa_allocate_exception()), the type info for the
8157a984708SDavid Chisnall  * pointee, and the destructor (if there is one) as arguments.
8167a984708SDavid Chisnall  */
__cxa_throw(void * thrown_exception,std::type_info * tinfo,void (* dest)(void *))8177a984708SDavid Chisnall extern "C" void __cxa_throw(void *thrown_exception,
8187a984708SDavid Chisnall                             std::type_info *tinfo,
8197a984708SDavid Chisnall                             void(*dest)(void*))
8207a984708SDavid Chisnall {
821ecf41062SDimitry Andric 	__cxa_exception *ex = __cxa_init_primary_exception(thrown_exception, tinfo, dest);
8227a984708SDavid Chisnall 	ex->referenceCount = 1;
8237a984708SDavid Chisnall 
8247a984708SDavid Chisnall 	throw_exception(ex);
8257a984708SDavid Chisnall }
8267a984708SDavid Chisnall 
__cxa_rethrow_primary_exception(void * thrown_exception)8277a984708SDavid Chisnall extern "C" void __cxa_rethrow_primary_exception(void* thrown_exception)
8287a984708SDavid Chisnall {
8297a984708SDavid Chisnall 	if (NULL == thrown_exception) { return; }
8307a984708SDavid Chisnall 
8317a984708SDavid Chisnall 	__cxa_exception *original = exceptionFromPointer(thrown_exception);
832f2dc4184SDimitry Andric 	__cxa_dependent_exception *ex = reinterpret_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception())-1;
8337a984708SDavid Chisnall 
8347a984708SDavid Chisnall 	ex->primaryException = thrown_exception;
8357a984708SDavid Chisnall 	__cxa_increment_exception_refcount(thrown_exception);
8367a984708SDavid Chisnall 
8377a984708SDavid Chisnall 	ex->exceptionType = original->exceptionType;
8387a984708SDavid Chisnall 	ex->unwindHeader.exception_class = dependent_exception_class;
8397a984708SDavid Chisnall 	ex->unwindHeader.exception_cleanup = dependent_exception_cleanup;
8407a984708SDavid Chisnall 
841f2dc4184SDimitry Andric 	throw_exception(reinterpret_cast<__cxa_exception*>(ex));
8427a984708SDavid Chisnall }
8437a984708SDavid Chisnall 
__cxa_current_primary_exception(void)8447a984708SDavid Chisnall extern "C" void *__cxa_current_primary_exception(void)
8457a984708SDavid Chisnall {
8467a984708SDavid Chisnall 	__cxa_eh_globals* globals = __cxa_get_globals();
8477a984708SDavid Chisnall 	__cxa_exception *ex = globals->caughtExceptions;
8487a984708SDavid Chisnall 
8497a984708SDavid Chisnall 	if (0 == ex) { return NULL; }
8507a984708SDavid Chisnall 	ex = realExceptionFromException(ex);
8517a984708SDavid Chisnall 	__sync_fetch_and_add(&ex->referenceCount, 1);
8527a984708SDavid Chisnall 	return ex + 1;
8537a984708SDavid Chisnall }
8547a984708SDavid Chisnall 
__cxa_increment_exception_refcount(void * thrown_exception)8557a984708SDavid Chisnall extern "C" void __cxa_increment_exception_refcount(void* thrown_exception)
8567a984708SDavid Chisnall {
8577a984708SDavid Chisnall 	if (NULL == thrown_exception) { return; }
858f2dc4184SDimitry Andric 	__cxa_exception *ex = static_cast<__cxa_exception*>(thrown_exception) - 1;
8597a984708SDavid Chisnall 	if (isDependentException(ex->unwindHeader.exception_class)) { return; }
8607a984708SDavid Chisnall 	__sync_fetch_and_add(&ex->referenceCount, 1);
8617a984708SDavid Chisnall }
__cxa_decrement_exception_refcount(void * thrown_exception)8627a984708SDavid Chisnall extern "C" void __cxa_decrement_exception_refcount(void* thrown_exception)
8637a984708SDavid Chisnall {
8647a984708SDavid Chisnall 	if (NULL == thrown_exception) { return; }
865f2dc4184SDimitry Andric 	__cxa_exception *ex = static_cast<__cxa_exception*>(thrown_exception) - 1;
8667a984708SDavid Chisnall 	releaseException(ex);
8677a984708SDavid Chisnall }
8687a984708SDavid Chisnall 
8697a984708SDavid Chisnall /**
8707a984708SDavid Chisnall  * ABI function.  Rethrows the current exception.  Does not remove the
8717a984708SDavid Chisnall  * exception from the stack or decrement its handler count - the compiler is
8727a984708SDavid Chisnall  * expected to set the landing pad for this function to the end of the catch
8737a984708SDavid Chisnall  * block, and then call _Unwind_Resume() to continue unwinding once
8747a984708SDavid Chisnall  * __cxa_end_catch() has been called and any cleanup code has been run.
8757a984708SDavid Chisnall  */
__cxa_rethrow()8767a984708SDavid Chisnall extern "C" void __cxa_rethrow()
8777a984708SDavid Chisnall {
878d5861eaaSDavid Chisnall 	__cxa_thread_info *ti = thread_info();
879f7cb1657SDavid Chisnall 	__cxa_eh_globals *globals = &ti->globals;
8807a984708SDavid Chisnall 	// Note: We don't remove this from the caught list here, because
8817a984708SDavid Chisnall 	// __cxa_end_catch will be called when we unwind out of the try block.  We
8827a984708SDavid Chisnall 	// could probably make this faster by providing an alternative rethrow
8837a984708SDavid Chisnall 	// function and ensuring that all cleanup code is run before calling it, so
8847a984708SDavid Chisnall 	// we can skip the top stack frame when unwinding.
8857a984708SDavid Chisnall 	__cxa_exception *ex = globals->caughtExceptions;
8867a984708SDavid Chisnall 
8877a984708SDavid Chisnall 	if (0 == ex)
8887a984708SDavid Chisnall 	{
8897a984708SDavid Chisnall 		fprintf(stderr,
8907a984708SDavid Chisnall 		        "Attempting to rethrow an exception that doesn't exist!\n");
8917a984708SDavid Chisnall 		std::terminate();
8927a984708SDavid Chisnall 	}
8937a984708SDavid Chisnall 
894f7cb1657SDavid Chisnall 	if (ti->foreign_exception_state != __cxa_thread_info::none)
895f7cb1657SDavid Chisnall 	{
896f7cb1657SDavid Chisnall 		ti->foreign_exception_state = __cxa_thread_info::rethrown;
897f2dc4184SDimitry Andric 		_Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(ex);
898f7cb1657SDavid Chisnall 		_Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(e);
899f7cb1657SDavid Chisnall 		report_failure(err, ex);
900f7cb1657SDavid Chisnall 		return;
901f7cb1657SDavid Chisnall 	}
902f7cb1657SDavid Chisnall 
9037a984708SDavid Chisnall 	assert(ex->handlerCount > 0 && "Rethrowing uncaught exception!");
9047a984708SDavid Chisnall 
905e91d723aSDimitry Andric 	// `globals->uncaughtExceptions` was decremented by `__cxa_begin_catch`.
906e91d723aSDimitry Andric 	// It's normally incremented by `throw_exception`, but this path invokes
907e91d723aSDimitry Andric 	// `_Unwind_Resume_or_Rethrow` directly to rethrow the exception.
908e91d723aSDimitry Andric 	// This path is only reachable if we're rethrowing a C++ exception -
909e91d723aSDimitry Andric 	// foreign exceptions don't adjust any of this state.
910e91d723aSDimitry Andric 	globals->uncaughtExceptions++;
911e91d723aSDimitry Andric 
9127a984708SDavid Chisnall 	// ex->handlerCount will be decremented in __cxa_end_catch in enclosing
9137a984708SDavid Chisnall 	// catch block
9147a984708SDavid Chisnall 
9157a984708SDavid Chisnall 	// Make handler count negative. This will tell __cxa_end_catch that
9167a984708SDavid Chisnall 	// exception was rethrown and exception object should not be destroyed
9177a984708SDavid Chisnall 	// when handler count become zero
9187a984708SDavid Chisnall 	ex->handlerCount = -ex->handlerCount;
9197a984708SDavid Chisnall 
9207a984708SDavid Chisnall 	// Continue unwinding the stack with this exception.  This should unwind to
9217a984708SDavid Chisnall 	// the place in the caller where __cxa_end_catch() is called.  The caller
9227a984708SDavid Chisnall 	// will then run cleanup code and bounce the exception back with
9237a984708SDavid Chisnall 	// _Unwind_Resume().
9247a984708SDavid Chisnall 	_Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(&ex->unwindHeader);
9257a984708SDavid Chisnall 	report_failure(err, ex);
9267a984708SDavid Chisnall }
9277a984708SDavid Chisnall 
9287a984708SDavid Chisnall /**
9297a984708SDavid Chisnall  * Returns the type_info object corresponding to the filter.
9307a984708SDavid Chisnall  */
get_type_info_entry(_Unwind_Context * context,dwarf_eh_lsda * lsda,int filter)9317a984708SDavid Chisnall static std::type_info *get_type_info_entry(_Unwind_Context *context,
9327a984708SDavid Chisnall                                            dwarf_eh_lsda *lsda,
9337a984708SDavid Chisnall                                            int filter)
9347a984708SDavid Chisnall {
9357a984708SDavid Chisnall 	// Get the address of the record in the table.
9367a984708SDavid Chisnall 	dw_eh_ptr_t record = lsda->type_table -
9377a984708SDavid Chisnall 		dwarf_size_of_fixed_size_field(lsda->type_table_encoding)*filter;
9387a984708SDavid Chisnall 	//record -= 4;
9397a984708SDavid Chisnall 	dw_eh_ptr_t start = record;
9407a984708SDavid Chisnall 	// Read the value, but it's probably an indirect reference...
9417a984708SDavid Chisnall 	int64_t offset = read_value(lsda->type_table_encoding, &record);
9427a984708SDavid Chisnall 
9437a984708SDavid Chisnall 	// (If the entry is 0, don't try to dereference it.  That would be bad.)
9447a984708SDavid Chisnall 	if (offset == 0) { return 0; }
9457a984708SDavid Chisnall 
9467a984708SDavid Chisnall 	// ...so we need to resolve it
947f2dc4184SDimitry Andric 	return reinterpret_cast<std::type_info*>(resolve_indirect_value(context,
948f2dc4184SDimitry Andric 			lsda->type_table_encoding, offset, start));
9497a984708SDavid Chisnall }
9507a984708SDavid Chisnall 
9517a984708SDavid Chisnall 
9527a984708SDavid Chisnall 
9537a984708SDavid Chisnall /**
9547a984708SDavid Chisnall  * Checks the type signature found in a handler against the type of the thrown
9557a984708SDavid Chisnall  * object.  If ex is 0 then it is assumed to be a foreign exception and only
9567a984708SDavid Chisnall  * matches cleanups.
9577a984708SDavid Chisnall  */
check_type_signature(__cxa_exception * ex,const std::type_info * type,void * & adjustedPtr)9587a984708SDavid Chisnall static bool check_type_signature(__cxa_exception *ex,
9597a984708SDavid Chisnall                                  const std::type_info *type,
9607a984708SDavid Chisnall                                  void *&adjustedPtr)
9617a984708SDavid Chisnall {
962f2dc4184SDimitry Andric 	void *exception_ptr = static_cast<void*>(ex+1);
963f7cb1657SDavid Chisnall 	const std::type_info *ex_type = ex ? ex->exceptionType : 0;
9647a984708SDavid Chisnall 
965f7cb1657SDavid Chisnall 	bool is_ptr = ex ? ex_type->__is_pointer_p() : false;
966c725650dSDavid Chisnall 	if (is_ptr)
9677a984708SDavid Chisnall 	{
968f2dc4184SDimitry Andric 		exception_ptr = *static_cast<void**>(exception_ptr);
9697a984708SDavid Chisnall 	}
9707a984708SDavid Chisnall 	// Always match a catchall, even with a foreign exception
9717a984708SDavid Chisnall 	//
9727a984708SDavid Chisnall 	// Note: A 0 here is a catchall, not a cleanup, so we return true to
9737a984708SDavid Chisnall 	// indicate that we found a catch.
9747a984708SDavid Chisnall 	if (0 == type)
9757a984708SDavid Chisnall 	{
9767a984708SDavid Chisnall 		if (ex)
9777a984708SDavid Chisnall 		{
9787a984708SDavid Chisnall 			adjustedPtr = exception_ptr;
9797a984708SDavid Chisnall 		}
9807a984708SDavid Chisnall 		return true;
9817a984708SDavid Chisnall 	}
9827a984708SDavid Chisnall 
9837a984708SDavid Chisnall 	if (0 == ex) { return false; }
9847a984708SDavid Chisnall 
9857a984708SDavid Chisnall 	// If the types are the same, no casting is needed.
9867a984708SDavid Chisnall 	if (*type == *ex_type)
9877a984708SDavid Chisnall 	{
9887a984708SDavid Chisnall 		adjustedPtr = exception_ptr;
9897a984708SDavid Chisnall 		return true;
9907a984708SDavid Chisnall 	}
9917a984708SDavid Chisnall 
9927a984708SDavid Chisnall 
993c725650dSDavid Chisnall 	if (type->__do_catch(ex_type, &exception_ptr, 1))
9947a984708SDavid Chisnall 	{
995c725650dSDavid Chisnall 		adjustedPtr = exception_ptr;
9967a984708SDavid Chisnall 		return true;
9977a984708SDavid Chisnall 	}
998c725650dSDavid Chisnall 
9997a984708SDavid Chisnall 	return false;
10007a984708SDavid Chisnall }
10017a984708SDavid Chisnall /**
10027a984708SDavid Chisnall  * Checks whether the exception matches the type specifiers in this action
10037a984708SDavid Chisnall  * record.  If the exception only matches cleanups, then this returns false.
10047a984708SDavid Chisnall  * If it matches a catch (including a catchall) then it returns true.
10057a984708SDavid Chisnall  *
10067a984708SDavid Chisnall  * The selector argument is used to return the selector that is passed in the
10077a984708SDavid Chisnall  * second exception register when installing the context.
10087a984708SDavid 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)10097a984708SDavid Chisnall static handler_type check_action_record(_Unwind_Context *context,
10107a984708SDavid Chisnall                                         dwarf_eh_lsda *lsda,
10117a984708SDavid Chisnall                                         dw_eh_ptr_t action_record,
10127a984708SDavid Chisnall                                         __cxa_exception *ex,
10137a984708SDavid Chisnall                                         unsigned long *selector,
10147a984708SDavid Chisnall                                         void *&adjustedPtr)
10157a984708SDavid Chisnall {
10167a984708SDavid Chisnall 	if (!action_record) { return handler_cleanup; }
10177a984708SDavid Chisnall 	handler_type found = handler_none;
10187a984708SDavid Chisnall 	while (action_record)
10197a984708SDavid Chisnall 	{
10207a984708SDavid Chisnall 		int filter = read_sleb128(&action_record);
10217a984708SDavid Chisnall 		dw_eh_ptr_t action_record_offset_base = action_record;
10227a984708SDavid Chisnall 		int displacement = read_sleb128(&action_record);
10237a984708SDavid Chisnall 		action_record = displacement ?
10247a984708SDavid Chisnall 			action_record_offset_base + displacement : 0;
10257a984708SDavid Chisnall 		// We only check handler types for C++ exceptions - foreign exceptions
1026f7cb1657SDavid Chisnall 		// are only allowed for cleanups and catchalls.
1027f7cb1657SDavid Chisnall 		if (filter > 0)
10287a984708SDavid Chisnall 		{
10297a984708SDavid Chisnall 			std::type_info *handler_type = get_type_info_entry(context, lsda, filter);
10307a984708SDavid Chisnall 			if (check_type_signature(ex, handler_type, adjustedPtr))
10317a984708SDavid Chisnall 			{
10327a984708SDavid Chisnall 				*selector = filter;
10337a984708SDavid Chisnall 				return handler_catch;
10347a984708SDavid Chisnall 			}
10357a984708SDavid Chisnall 		}
10367a984708SDavid Chisnall 		else if (filter < 0 && 0 != ex)
10377a984708SDavid Chisnall 		{
10387a984708SDavid Chisnall 			bool matched = false;
10397a984708SDavid Chisnall 			*selector = filter;
1040f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
10417a984708SDavid Chisnall 			filter++;
10427a984708SDavid Chisnall 			std::type_info *handler_type = get_type_info_entry(context, lsda, filter--);
10437a984708SDavid Chisnall 			while (handler_type)
10447a984708SDavid Chisnall 			{
10457a984708SDavid Chisnall 				if (check_type_signature(ex, handler_type, adjustedPtr))
10467a984708SDavid Chisnall 				{
10477a984708SDavid Chisnall 					matched = true;
10487a984708SDavid Chisnall 					break;
10497a984708SDavid Chisnall 				}
10507a984708SDavid Chisnall 				handler_type = get_type_info_entry(context, lsda, filter--);
10517a984708SDavid Chisnall 			}
10527a984708SDavid Chisnall #else
1053f2dc4184SDimitry Andric 			unsigned char *type_index = reinterpret_cast<unsigned char*>(lsda->type_table) - filter - 1;
10547a984708SDavid Chisnall 			while (*type_index)
10557a984708SDavid Chisnall 			{
10567a984708SDavid Chisnall 				std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++));
10577a984708SDavid Chisnall 				// If the exception spec matches a permitted throw type for
10587a984708SDavid Chisnall 				// this function, don't report a handler - we are allowed to
10597a984708SDavid Chisnall 				// propagate this exception out.
10607a984708SDavid Chisnall 				if (check_type_signature(ex, handler_type, adjustedPtr))
10617a984708SDavid Chisnall 				{
10627a984708SDavid Chisnall 					matched = true;
10637a984708SDavid Chisnall 					break;
10647a984708SDavid Chisnall 				}
10657a984708SDavid Chisnall 			}
10667a984708SDavid Chisnall #endif
10677a984708SDavid Chisnall 			if (matched) { continue; }
10687a984708SDavid Chisnall 			// If we don't find an allowed exception spec, we need to install
10697a984708SDavid Chisnall 			// the context for this action.  The landing pad will then call the
10707a984708SDavid Chisnall 			// unexpected exception function.  Treat this as a catch
10717a984708SDavid Chisnall 			return handler_catch;
10727a984708SDavid Chisnall 		}
10737a984708SDavid Chisnall 		else if (filter == 0)
10747a984708SDavid Chisnall 		{
10757a984708SDavid Chisnall 			*selector = filter;
10767a984708SDavid Chisnall 			found = handler_cleanup;
10777a984708SDavid Chisnall 		}
10787a984708SDavid Chisnall 	}
10797a984708SDavid Chisnall 	return found;
10807a984708SDavid Chisnall }
10817a984708SDavid Chisnall 
pushCleanupException(_Unwind_Exception * exceptionObject,__cxa_exception * ex)10827a984708SDavid Chisnall static void pushCleanupException(_Unwind_Exception *exceptionObject,
10837a984708SDavid Chisnall                                  __cxa_exception *ex)
10847a984708SDavid Chisnall {
1085f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
10867a984708SDavid Chisnall 	__cxa_thread_info *info = thread_info_fast();
10877a984708SDavid Chisnall 	if (ex)
10887a984708SDavid Chisnall 	{
10897a984708SDavid Chisnall 		ex->cleanupCount++;
10907a984708SDavid Chisnall 		if (ex->cleanupCount > 1)
10917a984708SDavid Chisnall 		{
10927a984708SDavid Chisnall 			assert(exceptionObject == info->currentCleanup);
10937a984708SDavid Chisnall 			return;
10947a984708SDavid Chisnall 		}
10957a984708SDavid Chisnall 		ex->nextCleanup = info->currentCleanup;
10967a984708SDavid Chisnall 	}
10977a984708SDavid Chisnall 	info->currentCleanup = exceptionObject;
10987a984708SDavid Chisnall #endif
10997a984708SDavid Chisnall }
11007a984708SDavid Chisnall 
11017a984708SDavid Chisnall /**
11027a984708SDavid Chisnall  * The exception personality function.  This is referenced in the unwinding
11037a984708SDavid Chisnall  * DWARF metadata and is called by the unwind library for each C++ stack frame
11047a984708SDavid Chisnall  * containing catch or cleanup code.
11057a984708SDavid Chisnall  */
11067a984708SDavid Chisnall extern "C"
11077a984708SDavid Chisnall BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0)
11087a984708SDavid Chisnall 	// This personality function is for version 1 of the ABI.  If you use it
11097a984708SDavid Chisnall 	// with a future version of the ABI, it won't know what to do, so it
11107a984708SDavid Chisnall 	// reports a fatal error and give up before it breaks anything.
11117a984708SDavid Chisnall 	if (1 != version)
11127a984708SDavid Chisnall 	{
11137a984708SDavid Chisnall 		return _URC_FATAL_PHASE1_ERROR;
11147a984708SDavid Chisnall 	}
11157a984708SDavid Chisnall 	__cxa_exception *ex = 0;
11167a984708SDavid Chisnall 	__cxa_exception *realEx = 0;
11177a984708SDavid Chisnall 
11187a984708SDavid Chisnall 	// If this exception is throw by something else then we can't make any
11197a984708SDavid Chisnall 	// assumptions about its layout beyond the fields declared in
11207a984708SDavid Chisnall 	// _Unwind_Exception.
11217a984708SDavid Chisnall 	bool foreignException = !isCXXException(exceptionClass);
11227a984708SDavid Chisnall 
11237a984708SDavid Chisnall 	// If this isn't a foreign exception, then we have a C++ exception structure
11247a984708SDavid Chisnall 	if (!foreignException)
11257a984708SDavid Chisnall 	{
11267a984708SDavid Chisnall 		ex = exceptionFromPointer(exceptionObject);
11277a984708SDavid Chisnall 		realEx = realExceptionFromException(ex);
11287a984708SDavid Chisnall 	}
11297a984708SDavid Chisnall 
1130f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
11317a984708SDavid Chisnall 	unsigned char *lsda_addr =
1132f2dc4184SDimitry Andric 		static_cast<unsigned char*>(_Unwind_GetLanguageSpecificData(context));
1133f2dc4184SDimitry Andric #else
1134f2dc4184SDimitry Andric 	unsigned char *lsda_addr =
1135f2dc4184SDimitry Andric 		reinterpret_cast<unsigned char*>(static_cast<uintptr_t>(_Unwind_GetLanguageSpecificData(context)));
1136f2dc4184SDimitry Andric #endif
11377a984708SDavid Chisnall 
11387a984708SDavid Chisnall 	// No LSDA implies no landing pads - try the next frame
11397a984708SDavid Chisnall 	if (0 == lsda_addr) { return continueUnwinding(exceptionObject, context); }
11407a984708SDavid Chisnall 
11417a984708SDavid Chisnall 	// These two variables define how the exception will be handled.
11427a984708SDavid Chisnall 	dwarf_eh_action action = {0};
11437a984708SDavid Chisnall 	unsigned long selector = 0;
11447a984708SDavid Chisnall 
11457a984708SDavid Chisnall 	// During the search phase, we do a complete lookup.  If we return
11467a984708SDavid Chisnall 	// _URC_HANDLER_FOUND, then the phase 2 unwind will call this function with
11477a984708SDavid Chisnall 	// a _UA_HANDLER_FRAME action, telling us to install the handler frame.  If
11487a984708SDavid Chisnall 	// we return _URC_CONTINUE_UNWIND, we may be called again later with a
11497a984708SDavid Chisnall 	// _UA_CLEANUP_PHASE action for this frame.
11507a984708SDavid Chisnall 	//
11517a984708SDavid Chisnall 	// The point of the two-stage unwind allows us to entirely avoid any stack
11527a984708SDavid Chisnall 	// unwinding if there is no handler.  If there are just cleanups found,
11537a984708SDavid Chisnall 	// then we can just panic call an abort function.
11547a984708SDavid Chisnall 	//
11557a984708SDavid Chisnall 	// Matching a handler is much more expensive than matching a cleanup,
11567a984708SDavid Chisnall 	// because we don't need to bother doing type comparisons (or looking at
11577a984708SDavid Chisnall 	// the type table at all) for a cleanup.  This means that there is no need
11587a984708SDavid Chisnall 	// to cache the result of finding a cleanup, because it's (quite) quick to
11597a984708SDavid Chisnall 	// look it up again from the action table.
11607a984708SDavid Chisnall 	if (actions & _UA_SEARCH_PHASE)
11617a984708SDavid Chisnall 	{
11627a984708SDavid Chisnall 		struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
11637a984708SDavid Chisnall 
11647a984708SDavid Chisnall 		if (!dwarf_eh_find_callsite(context, &lsda, &action))
11657a984708SDavid Chisnall 		{
11667a984708SDavid Chisnall 			// EH range not found. This happens if exception is thrown and not
11677a984708SDavid Chisnall 			// caught inside a cleanup (destructor).  We should call
11687a984708SDavid Chisnall 			// terminate() in this case.  The catchTemp (landing pad) field of
11697a984708SDavid Chisnall 			// exception object will contain null when personality function is
11707a984708SDavid Chisnall 			// called with _UA_HANDLER_FRAME action for phase 2 unwinding.
11717a984708SDavid Chisnall 			return _URC_HANDLER_FOUND;
11727a984708SDavid Chisnall 		}
11737a984708SDavid Chisnall 
11747a984708SDavid Chisnall 		handler_type found_handler = check_action_record(context, &lsda,
11757a984708SDavid Chisnall 				action.action_record, realEx, &selector, ex->adjustedPtr);
11767a984708SDavid Chisnall 		// If there's no action record, we've only found a cleanup, so keep
11777a984708SDavid Chisnall 		// searching for something real
11787a984708SDavid Chisnall 		if (found_handler == handler_catch)
11797a984708SDavid Chisnall 		{
11807a984708SDavid Chisnall 			// Cache the results for the phase 2 unwind, if we found a handler
11817a984708SDavid Chisnall 			// and this is not a foreign exception.
11827a984708SDavid Chisnall 			if (ex)
11837a984708SDavid Chisnall 			{
11847a984708SDavid Chisnall 				saveLandingPad(context, exceptionObject, ex, selector, action.landing_pad);
1185f2dc4184SDimitry Andric 				ex->languageSpecificData = reinterpret_cast<const char*>(lsda_addr);
1186f2dc4184SDimitry Andric 				ex->actionRecord = reinterpret_cast<const char*>(action.action_record);
11877a984708SDavid Chisnall 				// ex->adjustedPtr is set when finding the action record.
11887a984708SDavid Chisnall 			}
11897a984708SDavid Chisnall 			return _URC_HANDLER_FOUND;
11907a984708SDavid Chisnall 		}
11917a984708SDavid Chisnall 		return continueUnwinding(exceptionObject, context);
11927a984708SDavid Chisnall 	}
11937a984708SDavid Chisnall 
11947a984708SDavid Chisnall 
11957a984708SDavid Chisnall 	// If this is a foreign exception, we didn't have anywhere to cache the
11967a984708SDavid Chisnall 	// lookup stuff, so we need to do it again.  If this is either a forced
11977a984708SDavid Chisnall 	// unwind, a foreign exception, or a cleanup, then we just install the
11987a984708SDavid Chisnall 	// context for a cleanup.
11997a984708SDavid Chisnall 	if (!(actions & _UA_HANDLER_FRAME))
12007a984708SDavid Chisnall 	{
12017a984708SDavid Chisnall 		// cleanup
12027a984708SDavid Chisnall 		struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
12037a984708SDavid Chisnall 		dwarf_eh_find_callsite(context, &lsda, &action);
12047a984708SDavid Chisnall 		if (0 == action.landing_pad) { return continueUnwinding(exceptionObject, context); }
12057a984708SDavid Chisnall 		handler_type found_handler = check_action_record(context, &lsda,
12067a984708SDavid Chisnall 				action.action_record, realEx, &selector, ex->adjustedPtr);
12077a984708SDavid Chisnall 		// Ignore handlers this time.
12087a984708SDavid Chisnall 		if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); }
12097a984708SDavid Chisnall 		pushCleanupException(exceptionObject, ex);
12107a984708SDavid Chisnall 	}
12117a984708SDavid Chisnall 	else if (foreignException)
12127a984708SDavid Chisnall 	{
12137a984708SDavid Chisnall 		struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
12147a984708SDavid Chisnall 		dwarf_eh_find_callsite(context, &lsda, &action);
12157a984708SDavid Chisnall 		check_action_record(context, &lsda, action.action_record, realEx,
12167a984708SDavid Chisnall 				&selector, ex->adjustedPtr);
12177a984708SDavid Chisnall 	}
12187a984708SDavid Chisnall 	else if (ex->catchTemp == 0)
12197a984708SDavid Chisnall 	{
12207a984708SDavid Chisnall 		// Uncaught exception in cleanup, calling terminate
12217a984708SDavid Chisnall 		std::terminate();
12227a984708SDavid Chisnall 	}
12237a984708SDavid Chisnall 	else
12247a984708SDavid Chisnall 	{
12257a984708SDavid Chisnall 		// Restore the saved info if we saved some last time.
12267a984708SDavid Chisnall 		loadLandingPad(context, exceptionObject, ex, &selector, &action.landing_pad);
12277a984708SDavid Chisnall 		ex->catchTemp = 0;
12287a984708SDavid Chisnall 		ex->handlerSwitchValue = 0;
12297a984708SDavid Chisnall 	}
12307a984708SDavid Chisnall 
12317a984708SDavid Chisnall 
1232f2dc4184SDimitry Andric 	_Unwind_SetIP(context, reinterpret_cast<unsigned long>(action.landing_pad));
12337a984708SDavid Chisnall 	_Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
1234f2dc4184SDimitry Andric 	              reinterpret_cast<unsigned long>(exceptionObject));
12357a984708SDavid Chisnall 	_Unwind_SetGR(context, __builtin_eh_return_data_regno(1), selector);
12367a984708SDavid Chisnall 
12377a984708SDavid Chisnall 	return _URC_INSTALL_CONTEXT;
12387a984708SDavid Chisnall }
12397a984708SDavid Chisnall 
12407a984708SDavid Chisnall /**
12417a984708SDavid Chisnall  * ABI function called when entering a catch statement.  The argument is the
12427a984708SDavid Chisnall  * pointer passed out of the personality function.  This is always the start of
12437a984708SDavid Chisnall  * the _Unwind_Exception object.  The return value for this function is the
12447a984708SDavid Chisnall  * pointer to the caught exception, which is either the adjusted pointer (for
12457a984708SDavid Chisnall  * C++ exceptions) of the unadjusted pointer (for foreign exceptions).
12467a984708SDavid Chisnall  */
124707122a2aSDimitry Andric #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
12487a984708SDavid Chisnall extern "C" void *__cxa_begin_catch(void *e) throw()
12497a984708SDavid Chisnall #else
12507a984708SDavid Chisnall extern "C" void *__cxa_begin_catch(void *e)
12517a984708SDavid Chisnall #endif
12527a984708SDavid Chisnall {
1253f7cb1657SDavid Chisnall 	// We can't call the fast version here, because if the first exception that
1254f7cb1657SDavid Chisnall 	// we see is a foreign exception then we won't have called it yet.
1255f7cb1657SDavid Chisnall 	__cxa_thread_info *ti = thread_info();
1256f7cb1657SDavid Chisnall 	__cxa_eh_globals *globals = &ti->globals;
1257f2dc4184SDimitry Andric 	_Unwind_Exception *exceptionObject = static_cast<_Unwind_Exception*>(e);
12587a984708SDavid Chisnall 
12597a984708SDavid Chisnall 	if (isCXXException(exceptionObject->exception_class))
12607a984708SDavid Chisnall 	{
1261e91d723aSDimitry Andric 		// Only exceptions thrown with a C++ exception throwing function will
1262e91d723aSDimitry Andric 		// increment this, so don't decrement it here.
1263e91d723aSDimitry Andric 		globals->uncaughtExceptions--;
12647a984708SDavid Chisnall 		__cxa_exception *ex =  exceptionFromPointer(exceptionObject);
12657a984708SDavid Chisnall 
12667a984708SDavid Chisnall 		if (ex->handlerCount == 0)
12677a984708SDavid Chisnall 		{
12687a984708SDavid Chisnall 			// Add this to the front of the list of exceptions being handled
12697a984708SDavid Chisnall 			// and increment its handler count so that it won't be deleted
12707a984708SDavid Chisnall 			// prematurely.
12717a984708SDavid Chisnall 			ex->nextException = globals->caughtExceptions;
12727a984708SDavid Chisnall 			globals->caughtExceptions = ex;
12737a984708SDavid Chisnall 		}
12747a984708SDavid Chisnall 
12757a984708SDavid Chisnall 		if (ex->handlerCount < 0)
12767a984708SDavid Chisnall 		{
12777a984708SDavid Chisnall 			// Rethrown exception is catched before end of catch block.
12787a984708SDavid Chisnall 			// Clear the rethrow flag (make value positive) - we are allowed
12797a984708SDavid Chisnall 			// to delete this exception at the end of the catch block, as long
12807a984708SDavid Chisnall 			// as it isn't thrown again later.
12817a984708SDavid Chisnall 
12827a984708SDavid Chisnall 			// Code pattern:
12837a984708SDavid Chisnall 			//
12847a984708SDavid Chisnall 			// try {
12857a984708SDavid Chisnall 			//     throw x;
12867a984708SDavid Chisnall 			// }
12877a984708SDavid Chisnall 			// catch() {
12887a984708SDavid Chisnall 			//     try {
12897a984708SDavid Chisnall 			//         throw;
12907a984708SDavid Chisnall 			//     }
12917a984708SDavid Chisnall 			//     catch() {
12927a984708SDavid Chisnall 			//         __cxa_begin_catch() <- we are here
12937a984708SDavid Chisnall 			//     }
12947a984708SDavid Chisnall 			// }
12957a984708SDavid Chisnall 			ex->handlerCount = -ex->handlerCount + 1;
12967a984708SDavid Chisnall 		}
12977a984708SDavid Chisnall 		else
12987a984708SDavid Chisnall 		{
12997a984708SDavid Chisnall 			ex->handlerCount++;
13007a984708SDavid Chisnall 		}
1301f7cb1657SDavid Chisnall 		ti->foreign_exception_state = __cxa_thread_info::none;
13027a984708SDavid Chisnall 
13037a984708SDavid Chisnall 		return ex->adjustedPtr;
13047a984708SDavid Chisnall 	}
1305f7cb1657SDavid Chisnall 	else
1306f7cb1657SDavid Chisnall 	{
1307f7cb1657SDavid Chisnall 		// If this is a foreign exception, then we need to be able to
1308f7cb1657SDavid Chisnall 		// store it.  We can't chain foreign exceptions, so we give up
1309f7cb1657SDavid Chisnall 		// if there are already some outstanding ones.
1310f7cb1657SDavid Chisnall 		if (globals->caughtExceptions != 0)
1311f7cb1657SDavid Chisnall 		{
1312f7cb1657SDavid Chisnall 			std::terminate();
1313f7cb1657SDavid Chisnall 		}
1314f2dc4184SDimitry Andric 		globals->caughtExceptions = reinterpret_cast<__cxa_exception*>(exceptionObject);
1315f7cb1657SDavid Chisnall 		ti->foreign_exception_state = __cxa_thread_info::caught;
1316f7cb1657SDavid Chisnall 	}
13177a984708SDavid Chisnall 	// exceptionObject is the pointer to the _Unwind_Exception within the
13187a984708SDavid Chisnall 	// __cxa_exception.  The throw object is after this
1319f2dc4184SDimitry Andric 	return (reinterpret_cast<char*>(exceptionObject) + sizeof(_Unwind_Exception));
13207a984708SDavid Chisnall }
13217a984708SDavid Chisnall 
13227a984708SDavid Chisnall 
13237a984708SDavid Chisnall 
13247a984708SDavid Chisnall /**
13257a984708SDavid Chisnall  * ABI function called when exiting a catch block.  This will free the current
13267a984708SDavid Chisnall  * exception if it is no longer referenced in other catch blocks.
13277a984708SDavid Chisnall  */
13287a984708SDavid Chisnall extern "C" void __cxa_end_catch()
13297a984708SDavid Chisnall {
13307a984708SDavid Chisnall 	// We can call the fast version here because the slow version is called in
13317a984708SDavid Chisnall 	// __cxa_throw(), which must have been called before we end a catch block
1332f7cb1657SDavid Chisnall 	__cxa_thread_info *ti = thread_info_fast();
1333f7cb1657SDavid Chisnall 	__cxa_eh_globals *globals = &ti->globals;
13347a984708SDavid Chisnall 	__cxa_exception *ex = globals->caughtExceptions;
13357a984708SDavid Chisnall 
13367a984708SDavid Chisnall 	assert(0 != ex && "Ending catch when no exception is on the stack!");
13377a984708SDavid Chisnall 
1338f7cb1657SDavid Chisnall 	if (ti->foreign_exception_state != __cxa_thread_info::none)
1339f7cb1657SDavid Chisnall 	{
1340f7cb1657SDavid Chisnall 		if (ti->foreign_exception_state != __cxa_thread_info::rethrown)
1341f7cb1657SDavid Chisnall 		{
1342f2dc4184SDimitry Andric 			_Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(ti->globals.caughtExceptions);
1343076e75ebSDimitry Andric 			if (e->exception_cleanup)
1344f7cb1657SDavid Chisnall 				e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
1345f7cb1657SDavid Chisnall 		}
1346076e75ebSDimitry Andric 		globals->caughtExceptions = 0;
1347f7cb1657SDavid Chisnall 		ti->foreign_exception_state = __cxa_thread_info::none;
1348f7cb1657SDavid Chisnall 		return;
1349f7cb1657SDavid Chisnall 	}
1350f7cb1657SDavid Chisnall 
13517a984708SDavid Chisnall 	bool deleteException = true;
13527a984708SDavid Chisnall 
13537a984708SDavid Chisnall 	if (ex->handlerCount < 0)
13547a984708SDavid Chisnall 	{
13557a984708SDavid Chisnall 		// exception was rethrown. Exception should not be deleted even if
13567a984708SDavid Chisnall 		// handlerCount become zero.
13577a984708SDavid Chisnall 		// Code pattern:
13587a984708SDavid Chisnall 		// try {
13597a984708SDavid Chisnall 		//     throw x;
13607a984708SDavid Chisnall 		// }
13617a984708SDavid Chisnall 		// catch() {
13627a984708SDavid Chisnall 		//     {
13637a984708SDavid Chisnall 		//         throw;
13647a984708SDavid Chisnall 		//     }
13657a984708SDavid Chisnall 		//     cleanup {
13667a984708SDavid Chisnall 		//         __cxa_end_catch();   <- we are here
13677a984708SDavid Chisnall 		//     }
13687a984708SDavid Chisnall 		// }
13697a984708SDavid Chisnall 		//
13707a984708SDavid Chisnall 
13717a984708SDavid Chisnall 		ex->handlerCount++;
13727a984708SDavid Chisnall 		deleteException = false;
13737a984708SDavid Chisnall 	}
13747a984708SDavid Chisnall 	else
13757a984708SDavid Chisnall 	{
13767a984708SDavid Chisnall 		ex->handlerCount--;
13777a984708SDavid Chisnall 	}
13787a984708SDavid Chisnall 
13797a984708SDavid Chisnall 	if (ex->handlerCount == 0)
13807a984708SDavid Chisnall 	{
13817a984708SDavid Chisnall 		globals->caughtExceptions = ex->nextException;
13827a984708SDavid Chisnall 		if (deleteException)
13837a984708SDavid Chisnall 		{
13847a984708SDavid Chisnall 			releaseException(ex);
13857a984708SDavid Chisnall 		}
13867a984708SDavid Chisnall 	}
13877a984708SDavid Chisnall }
13887a984708SDavid Chisnall 
13897a984708SDavid Chisnall /**
13907a984708SDavid Chisnall  * ABI function.  Returns the type of the current exception.
13917a984708SDavid Chisnall  */
13927a984708SDavid Chisnall extern "C" std::type_info *__cxa_current_exception_type()
13937a984708SDavid Chisnall {
13947a984708SDavid Chisnall 	__cxa_eh_globals *globals = __cxa_get_globals();
13957a984708SDavid Chisnall 	__cxa_exception *ex = globals->caughtExceptions;
13967a984708SDavid Chisnall 	return ex ? ex->exceptionType : 0;
13977a984708SDavid Chisnall }
13987a984708SDavid Chisnall 
13997a984708SDavid Chisnall /**
1400e91d723aSDimitry Andric  * Cleanup, ensures that `__cxa_end_catch` is called to balance an explicit
1401e91d723aSDimitry Andric  * `__cxa_begin_catch` call.
1402e91d723aSDimitry Andric  */
1403e91d723aSDimitry Andric static void end_catch(char *)
1404e91d723aSDimitry Andric {
1405e91d723aSDimitry Andric 	__cxa_end_catch();
1406e91d723aSDimitry Andric }
1407e91d723aSDimitry Andric /**
14087a984708SDavid Chisnall  * ABI function, called when an exception specification is violated.
14097a984708SDavid Chisnall  *
14107a984708SDavid Chisnall  * This function does not return.
14117a984708SDavid Chisnall  */
14127a984708SDavid Chisnall extern "C" void __cxa_call_unexpected(void*exception)
14137a984708SDavid Chisnall {
1414f2dc4184SDimitry Andric 	_Unwind_Exception *exceptionObject = static_cast<_Unwind_Exception*>(exception);
1415e91d723aSDimitry Andric 	// Wrap the call to the unexpected handler in calls to `__cxa_begin_catch`
1416e91d723aSDimitry Andric 	// and `__cxa_end_catch` so that we correctly update exception counts if
1417e91d723aSDimitry Andric 	// the unexpected handler throws an exception.
1418e91d723aSDimitry Andric 	__cxa_begin_catch(exceptionObject);
1419e91d723aSDimitry Andric 	__attribute__((cleanup(end_catch)))
1420e91d723aSDimitry Andric 	char unused;
14217a984708SDavid Chisnall 	if (exceptionObject->exception_class == exception_class)
14227a984708SDavid Chisnall 	{
14237a984708SDavid Chisnall 		__cxa_exception *ex =  exceptionFromPointer(exceptionObject);
14247a984708SDavid Chisnall 		if (ex->unexpectedHandler)
14257a984708SDavid Chisnall 		{
14267a984708SDavid Chisnall 			ex->unexpectedHandler();
14277a984708SDavid Chisnall 			// Should not be reached.
14287a984708SDavid Chisnall 			abort();
14297a984708SDavid Chisnall 		}
14307a984708SDavid Chisnall 	}
14317a984708SDavid Chisnall 	std::unexpected();
14327a984708SDavid Chisnall 	// Should not be reached.
14337a984708SDavid Chisnall 	abort();
14347a984708SDavid Chisnall }
14357a984708SDavid Chisnall 
14367a984708SDavid Chisnall /**
14377a984708SDavid Chisnall  * ABI function, returns the adjusted pointer to the exception object.
14387a984708SDavid Chisnall  */
14397a984708SDavid Chisnall extern "C" void *__cxa_get_exception_ptr(void *exceptionObject)
14407a984708SDavid Chisnall {
14417a984708SDavid Chisnall 	return exceptionFromPointer(exceptionObject)->adjustedPtr;
14427a984708SDavid Chisnall }
14437a984708SDavid Chisnall 
14447a984708SDavid Chisnall /**
14457a984708SDavid Chisnall  * As an extension, we provide the ability for the unexpected and terminate
14467a984708SDavid Chisnall  * handlers to be thread-local.  We default to the standards-compliant
14477a984708SDavid Chisnall  * behaviour where they are global.
14487a984708SDavid Chisnall  */
14497a984708SDavid Chisnall static bool thread_local_handlers = false;
14507a984708SDavid Chisnall 
14517a984708SDavid Chisnall 
14527a984708SDavid Chisnall namespace pathscale
14537a984708SDavid Chisnall {
14547a984708SDavid Chisnall 	/**
14557a984708SDavid Chisnall 	 * Sets whether unexpected and terminate handlers should be thread-local.
14567a984708SDavid Chisnall 	 */
14577a984708SDavid Chisnall 	void set_use_thread_local_handlers(bool flag) throw()
14587a984708SDavid Chisnall 	{
14597a984708SDavid Chisnall 		thread_local_handlers = flag;
14607a984708SDavid Chisnall 	}
14617a984708SDavid Chisnall 	/**
14627a984708SDavid Chisnall 	 * Sets a thread-local unexpected handler.
14637a984708SDavid Chisnall 	 */
14647a984708SDavid Chisnall 	unexpected_handler set_unexpected(unexpected_handler f) throw()
14657a984708SDavid Chisnall 	{
14667a984708SDavid Chisnall 		static __cxa_thread_info *info = thread_info();
14677a984708SDavid Chisnall 		unexpected_handler old = info->unexpectedHandler;
14687a984708SDavid Chisnall 		info->unexpectedHandler = f;
14697a984708SDavid Chisnall 		return old;
14707a984708SDavid Chisnall 	}
14717a984708SDavid Chisnall 	/**
14727a984708SDavid Chisnall 	 * Sets a thread-local terminate handler.
14737a984708SDavid Chisnall 	 */
14747a984708SDavid Chisnall 	terminate_handler set_terminate(terminate_handler f) throw()
14757a984708SDavid Chisnall 	{
14767a984708SDavid Chisnall 		static __cxa_thread_info *info = thread_info();
14777a984708SDavid Chisnall 		terminate_handler old = info->terminateHandler;
14787a984708SDavid Chisnall 		info->terminateHandler = f;
14797a984708SDavid Chisnall 		return old;
14807a984708SDavid Chisnall 	}
14817a984708SDavid Chisnall }
14827a984708SDavid Chisnall 
14837a984708SDavid Chisnall namespace std
14847a984708SDavid Chisnall {
14857a984708SDavid Chisnall 	/**
14867a984708SDavid Chisnall 	 * Sets the function that will be called when an exception specification is
14877a984708SDavid Chisnall 	 * violated.
14887a984708SDavid Chisnall 	 */
14897a984708SDavid Chisnall 	unexpected_handler set_unexpected(unexpected_handler f) throw()
14907a984708SDavid Chisnall 	{
14917a984708SDavid Chisnall 		if (thread_local_handlers) { return pathscale::set_unexpected(f); }
14927a984708SDavid Chisnall 
1493bfffb66eSDimitry Andric 		return unexpectedHandler.exchange(f);
14947a984708SDavid Chisnall 	}
14957a984708SDavid Chisnall 	/**
14967a984708SDavid Chisnall 	 * Sets the function that is called to terminate the program.
14977a984708SDavid Chisnall 	 */
14987a984708SDavid Chisnall 	terminate_handler set_terminate(terminate_handler f) throw()
14997a984708SDavid Chisnall 	{
15007a984708SDavid Chisnall 		if (thread_local_handlers) { return pathscale::set_terminate(f); }
1501f7cb1657SDavid Chisnall 
1502bfffb66eSDimitry Andric 		return terminateHandler.exchange(f);
15037a984708SDavid Chisnall 	}
15047a984708SDavid Chisnall 	/**
15057a984708SDavid Chisnall 	 * Terminates the program, calling a custom terminate implementation if
15067a984708SDavid Chisnall 	 * required.
15077a984708SDavid Chisnall 	 */
15087a984708SDavid Chisnall 	void terminate()
15097a984708SDavid Chisnall 	{
1510d9e22925SDimitry Andric 		static __cxa_thread_info *info = thread_info();
15117a984708SDavid Chisnall 		if (0 != info && 0 != info->terminateHandler)
15127a984708SDavid Chisnall 		{
15137a984708SDavid Chisnall 			info->terminateHandler();
15147a984708SDavid Chisnall 			// Should not be reached - a terminate handler is not expected to
15157a984708SDavid Chisnall 			// return.
15167a984708SDavid Chisnall 			abort();
15177a984708SDavid Chisnall 		}
1518bfffb66eSDimitry Andric 		terminateHandler.load()();
15197a984708SDavid Chisnall 	}
15207a984708SDavid Chisnall 	/**
15217a984708SDavid Chisnall 	 * Called when an unexpected exception is encountered (i.e. an exception
15227a984708SDavid Chisnall 	 * violates an exception specification).  This calls abort() unless a
15237a984708SDavid Chisnall 	 * custom handler has been set..
15247a984708SDavid Chisnall 	 */
15257a984708SDavid Chisnall 	void unexpected()
15267a984708SDavid Chisnall 	{
1527d9e22925SDimitry Andric 		static __cxa_thread_info *info = thread_info();
15287a984708SDavid Chisnall 		if (0 != info && 0 != info->unexpectedHandler)
15297a984708SDavid Chisnall 		{
15307a984708SDavid Chisnall 			info->unexpectedHandler();
15317a984708SDavid Chisnall 			// Should not be reached - a terminate handler is not expected to
15327a984708SDavid Chisnall 			// return.
15337a984708SDavid Chisnall 			abort();
15347a984708SDavid Chisnall 		}
1535bfffb66eSDimitry Andric 		unexpectedHandler.load()();
15367a984708SDavid Chisnall 	}
15377a984708SDavid Chisnall 	/**
15387a984708SDavid Chisnall 	 * Returns whether there are any exceptions currently being thrown that
15397a984708SDavid Chisnall 	 * have not been caught.  This can occur inside a nested catch statement.
15407a984708SDavid Chisnall 	 */
15417a984708SDavid Chisnall 	bool uncaught_exception() throw()
15427a984708SDavid Chisnall 	{
15437a984708SDavid Chisnall 		__cxa_thread_info *info = thread_info();
15447a984708SDavid Chisnall 		return info->globals.uncaughtExceptions != 0;
15457a984708SDavid Chisnall 	}
15467a984708SDavid Chisnall 	/**
1547bb52ed32SDimitry Andric 	 * Returns the number of exceptions currently being thrown that have not
1548bb52ed32SDimitry Andric 	 * been caught.  This can occur inside a nested catch statement.
1549bb52ed32SDimitry Andric 	 */
1550bb52ed32SDimitry Andric 	int uncaught_exceptions() throw()
1551bb52ed32SDimitry Andric 	{
1552bb52ed32SDimitry Andric 		__cxa_thread_info *info = thread_info();
1553bb52ed32SDimitry Andric 		return info->globals.uncaughtExceptions;
1554bb52ed32SDimitry Andric 	}
1555bb52ed32SDimitry Andric 	/**
15567a984708SDavid Chisnall 	 * Returns the current unexpected handler.
15577a984708SDavid Chisnall 	 */
15587a984708SDavid Chisnall 	unexpected_handler get_unexpected() throw()
15597a984708SDavid Chisnall 	{
15607a984708SDavid Chisnall 		__cxa_thread_info *info = thread_info();
15617a984708SDavid Chisnall 		if (info->unexpectedHandler)
15627a984708SDavid Chisnall 		{
15637a984708SDavid Chisnall 			return info->unexpectedHandler;
15647a984708SDavid Chisnall 		}
1565bfffb66eSDimitry Andric 		return unexpectedHandler.load();
15667a984708SDavid Chisnall 	}
15677a984708SDavid Chisnall 	/**
15687a984708SDavid Chisnall 	 * Returns the current terminate handler.
15697a984708SDavid Chisnall 	 */
15707a984708SDavid Chisnall 	terminate_handler get_terminate() throw()
15717a984708SDavid Chisnall 	{
15727a984708SDavid Chisnall 		__cxa_thread_info *info = thread_info();
15737a984708SDavid Chisnall 		if (info->terminateHandler)
15747a984708SDavid Chisnall 		{
15757a984708SDavid Chisnall 			return info->terminateHandler;
15767a984708SDavid Chisnall 		}
1577bfffb66eSDimitry Andric 		return terminateHandler.load();
15787a984708SDavid Chisnall 	}
15797a984708SDavid Chisnall }
1580f2dc4184SDimitry Andric #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
15817a984708SDavid Chisnall extern "C" _Unwind_Exception *__cxa_get_cleanup(void)
15827a984708SDavid Chisnall {
15837a984708SDavid Chisnall 	__cxa_thread_info *info = thread_info_fast();
15847a984708SDavid Chisnall 	_Unwind_Exception *exceptionObject = info->currentCleanup;
15857a984708SDavid Chisnall 	if (isCXXException(exceptionObject->exception_class))
15867a984708SDavid Chisnall 	{
15877a984708SDavid Chisnall 		__cxa_exception *ex =  exceptionFromPointer(exceptionObject);
15887a984708SDavid Chisnall 		ex->cleanupCount--;
15897a984708SDavid Chisnall 		if (ex->cleanupCount == 0)
15907a984708SDavid Chisnall 		{
15917a984708SDavid Chisnall 			info->currentCleanup = ex->nextCleanup;
15927a984708SDavid Chisnall 			ex->nextCleanup = 0;
15937a984708SDavid Chisnall 		}
15947a984708SDavid Chisnall 	}
15957a984708SDavid Chisnall 	else
15967a984708SDavid Chisnall 	{
15977a984708SDavid Chisnall 		info->currentCleanup = 0;
15987a984708SDavid Chisnall 	}
15997a984708SDavid Chisnall 	return exceptionObject;
16007a984708SDavid Chisnall }
16017a984708SDavid Chisnall 
16027a984708SDavid Chisnall asm (
16037a984708SDavid Chisnall ".pushsection .text.__cxa_end_cleanup    \n"
16047a984708SDavid Chisnall ".global __cxa_end_cleanup               \n"
16057a984708SDavid Chisnall ".type __cxa_end_cleanup, \"function\"   \n"
16067a984708SDavid Chisnall "__cxa_end_cleanup:                      \n"
16077a984708SDavid Chisnall "	push {r1, r2, r3, r4}                \n"
1608cbd1e831SMark Johnston "	mov r4, lr                           \n"
16097a984708SDavid Chisnall "	bl __cxa_get_cleanup                 \n"
1610cbd1e831SMark Johnston "	mov lr, r4                           \n"
1611cbd1e831SMark Johnston "	pop {r1, r2, r3, r4}                 \n"
16127a984708SDavid Chisnall "	b _Unwind_Resume                     \n"
16137a984708SDavid Chisnall "	bl abort                             \n"
16147a984708SDavid Chisnall ".popsection                             \n"
16157a984708SDavid Chisnall );
16167a984708SDavid Chisnall #endif
1617