#include <stdlib.h>
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <pthread.h>
#include "typeinfo.h"
#include "dwarf_eh.h"
#include "atomic.h"
#include "cxxabi.h"
#pragma weak pthread_key_create
#pragma weak pthread_setspecific
#pragma weak pthread_getspecific
#pragma weak pthread_once
#ifdef LIBCXXRT_WEAK_LOCKS
#pragma weak pthread_mutex_lock
#define pthread_mutex_lock(mtx) do {\
if (pthread_mutex_lock) pthread_mutex_lock(mtx);\
} while(0)
#pragma weak pthread_mutex_unlock
#define pthread_mutex_unlock(mtx) do {\
if (pthread_mutex_unlock) pthread_mutex_unlock(mtx);\
} while(0)
#pragma weak pthread_cond_signal
#define pthread_cond_signal(cv) do {\
if (pthread_cond_signal) pthread_cond_signal(cv);\
} while(0)
#pragma weak pthread_cond_wait
#define pthread_cond_wait(cv, mtx) do {\
if (pthread_cond_wait) pthread_cond_wait(cv, mtx);\
} while(0)
#endif
using namespace ABI_NAMESPACE;
static void saveLandingPad(struct _Unwind_Context *context,
struct _Unwind_Exception *ucb,
struct __cxa_exception *ex,
int selector,
dw_eh_ptr_t landingPad)
{
#if defined(__arm__) && !defined(__ARM_DWARF_EH__)
ucb->barrier_cache.sp = _Unwind_GetGR(context, 13);
ucb->barrier_cache.bitpattern[1] = static_cast<uint32_t>(selector);
ucb->barrier_cache.bitpattern[3] = reinterpret_cast<uint32_t>(landingPad);
#endif
if (ex)
{
ex->handlerSwitchValue = selector;
ex->catchTemp = landingPad;
}
}
static int loadLandingPad(struct _Unwind_Context *context,
struct _Unwind_Exception *ucb,
struct __cxa_exception *ex,
unsigned long *selector,
dw_eh_ptr_t *landingPad)
{
#if defined(__arm__) && !defined(__ARM_DWARF_EH__)
*selector = ucb->barrier_cache.bitpattern[1];
*landingPad = reinterpret_cast<dw_eh_ptr_t>(ucb->barrier_cache.bitpattern[3]);
return 1;
#else
if (ex)
{
*selector = ex->handlerSwitchValue;
*landingPad = reinterpret_cast<dw_eh_ptr_t>(ex->catchTemp);
return 0;
}
return 0;
#endif
}
static inline _Unwind_Reason_Code continueUnwinding(struct _Unwind_Exception *ex,
struct _Unwind_Context *context)
{
#if defined(__arm__) && !defined(__ARM_DWARF_EH__)
if (__gnu_unwind_frame(ex, context) != _URC_OK) { return _URC_FAILURE; }
#endif
return _URC_CONTINUE_UNWIND;
}
extern "C" void __cxa_free_exception(void *thrown_exception) _LIBCXXRT_NOEXCEPT;
extern "C" void __cxa_free_dependent_exception(void *thrown_exception);
extern "C" void* __dynamic_cast(const void *sub,
const __class_type_info *src,
const __class_type_info *dst,
ptrdiff_t src2dst_offset);
typedef enum
{
handler_none,
handler_cleanup,
handler_catch
} handler_type;
struct __cxa_thread_info
{
terminate_handler terminateHandler;
unexpected_handler unexpectedHandler;
#ifndef LIBCXXRT_NO_EMERGENCY_MALLOC
int emergencyBuffersHeld;
#endif
_Unwind_Exception *currentCleanup;
enum
{
none,
caught,
rethrown
} foreign_exception_state;
__cxa_eh_globals globals;
};
struct __cxa_dependent_exception
{
#ifdef __LP64__
void *reserve;
void *primaryException;
#endif
std::type_info *exceptionType;
void (*exceptionDestructor) (void *);
unexpected_handler unexpectedHandler;
terminate_handler terminateHandler;
__cxa_exception *nextException;
int handlerCount;
#if defined(__arm__) && !defined(__ARM_DWARF_EH__)
_Unwind_Exception *nextCleanup;
int cleanupCount;
#endif
int handlerSwitchValue;
const char *actionRecord;
const char *languageSpecificData;
void *catchTemp;
void *adjustedPtr;
#ifndef __LP64__
void *primaryException;
#endif
_Unwind_Exception unwindHeader;
};
static_assert(sizeof(__cxa_exception) == sizeof(__cxa_dependent_exception),
"__cxa_exception and __cxa_dependent_exception should have the same size");
static_assert(offsetof(__cxa_exception, referenceCount) ==
offsetof(__cxa_dependent_exception, primaryException),
"referenceCount and primaryException should have the same offset");
static_assert(offsetof(__cxa_exception, unwindHeader) ==
offsetof(__cxa_dependent_exception, unwindHeader),
"unwindHeader fields should have the same offset");
static_assert(offsetof(__cxa_dependent_exception, unwindHeader) ==
offsetof(__cxa_dependent_exception, adjustedPtr) + 8,
"there should be no padding before unwindHeader");
namespace std
{
[[noreturn]] void unexpected();
class exception
{
public:
virtual ~exception() _LIBCXXRT_NOEXCEPT;
virtual const char* what() const _LIBCXXRT_NOEXCEPT;
};
}
static const uint64_t exception_class =
EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\0');
static const uint64_t dependent_exception_class =
EXCEPTION_CLASS('G', 'N', 'U', 'C', 'C', '+', '+', '\x01');
static const uint32_t abi_exception_class =
GENERIC_EXCEPTION_CLASS('C', '+', '+', '\0');
static bool isCXXException(uint64_t cls)
{
return (cls == exception_class) || (cls == dependent_exception_class);
}
static bool isDependentException(uint64_t cls)
{
return cls == dependent_exception_class;
}
static __cxa_exception *exceptionFromPointer(void *ex)
{
return reinterpret_cast<__cxa_exception*>(static_cast<char*>(ex) -
offsetof(struct __cxa_exception, unwindHeader));
}
static __cxa_exception *realExceptionFromException(__cxa_exception *ex)
{
if (!isDependentException(ex->unwindHeader.exception_class)) { return ex; }
return reinterpret_cast<__cxa_exception*>((reinterpret_cast<__cxa_dependent_exception*>(ex))->primaryException)-1;
}
namespace std
{
[[noreturn]] void terminate(void) _LIBCXXRT_NOEXCEPT;
}
using namespace ABI_NAMESPACE;
#ifdef LIBCXXRT_NO_DEFAULT_TERMINATE_DIAGNOSTICS
static atomic<terminate_handler> terminateHandler = abort;
#else
static _Unwind_Reason_Code trace(struct _Unwind_Context *context, void *c)
{
Dl_info myinfo;
int mylookup =
dladdr(reinterpret_cast<void *>(__cxa_current_exception_type), &myinfo);
void *ip = reinterpret_cast<void*>(_Unwind_GetIP(context));
Dl_info info;
if (dladdr(ip, &info) != 0)
{
if (mylookup == 0 || strcmp(info.dli_fname, myinfo.dli_fname) != 0)
{
printf("%p:%s() in %s\n", ip, info.dli_sname, info.dli_fname);
}
}
return _URC_CONTINUE_UNWIND;
}
static void terminate_with_diagnostics() {
__cxa_eh_globals *globals = __cxa_get_globals();
__cxa_exception *ex = globals->caughtExceptions;
if (ex != nullptr) {
fprintf(stderr, "Terminating due to uncaught exception %p", static_cast<void*>(ex));
ex = realExceptionFromException(ex);
static const __class_type_info *e_ti =
static_cast<const __class_type_info*>(&typeid(std::exception));
const __class_type_info *throw_ti =
dynamic_cast<const __class_type_info*>(ex->exceptionType);
if (throw_ti)
{
std::exception *e =
static_cast<std::exception*>(e_ti->cast_to(static_cast<void*>(ex+1), throw_ti));
if (e)
{
fprintf(stderr, " '%s'", e->what());
}
}
size_t bufferSize = 128;
char *demangled = static_cast<char*>(malloc(bufferSize));
const char *mangled = ex->exceptionType->name();
int status;
demangled = __cxa_demangle(mangled, demangled, &bufferSize, &status);
fprintf(stderr, " of type %s\n",
status == 0 ? demangled : mangled);
if (status == 0) { free(demangled); }
_Unwind_Backtrace(trace, 0);
}
abort();
}
static atomic<terminate_handler> terminateHandler = terminate_with_diagnostics;
#endif
static atomic<unexpected_handler> unexpectedHandler = std::terminate;
static pthread_key_t eh_key;
static void exception_cleanup(_Unwind_Reason_Code reason,
struct _Unwind_Exception *ex)
{
__cxa_free_exception(static_cast<void*>(ex + 1));
}
static void dependent_exception_cleanup(_Unwind_Reason_Code reason,
struct _Unwind_Exception *ex)
{
__cxa_free_dependent_exception(static_cast<void*>(ex + 1));
}
static void free_exception_list(__cxa_exception *ex)
{
if (0 != ex->nextException)
{
free_exception_list(ex->nextException);
}
__cxa_free_exception(ex+1);
}
static void thread_cleanup(void* thread_info)
{
__cxa_thread_info *info = static_cast<__cxa_thread_info*>(thread_info);
if (info->globals.caughtExceptions)
{
if (info->foreign_exception_state != __cxa_thread_info::none)
{
_Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(info->globals.caughtExceptions);
if (e->exception_cleanup)
e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
}
else
{
free_exception_list(info->globals.caughtExceptions);
}
}
free(thread_info);
}
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
static bool fakeTLS;
static __cxa_thread_info singleThreadInfo;
static void init_key(void)
{
if ((0 == pthread_key_create) ||
(0 == pthread_setspecific) ||
(0 == pthread_getspecific))
{
fakeTLS = true;
return;
}
pthread_key_create(&eh_key, thread_cleanup);
pthread_setspecific(eh_key, reinterpret_cast<void *>(0x42));
fakeTLS = (pthread_getspecific(eh_key) != reinterpret_cast<void *>(0x42));
pthread_setspecific(eh_key, 0);
}
static __cxa_thread_info *thread_info()
{
if ((0 == pthread_once) || pthread_once(&once_control, init_key))
{
fakeTLS = true;
}
if (fakeTLS) { return &singleThreadInfo; }
__cxa_thread_info *info = static_cast<__cxa_thread_info*>(pthread_getspecific(eh_key));
if (0 == info)
{
info = static_cast<__cxa_thread_info*>(calloc(1, sizeof(__cxa_thread_info)));
pthread_setspecific(eh_key, info);
}
return info;
}
static __cxa_thread_info *thread_info_fast()
{
if (fakeTLS) { return &singleThreadInfo; }
return static_cast<__cxa_thread_info*>(pthread_getspecific(eh_key));
}
extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals(void)
{
return &(thread_info()->globals);
}
extern "C" __cxa_eh_globals *ABI_NAMESPACE::__cxa_get_globals_fast(void)
{
return &(thread_info_fast()->globals);
}
#ifdef LIBCXXRT_NO_EMERGENCY_MALLOC
static char *alloc_or_die(size_t size)
{
char *buffer = static_cast<char*>(calloc(1, size));
if (buffer == nullptr)
{
fputs("Out of memory attempting to allocate exception\n", stderr);
std::terminate();
}
return buffer;
}
static void free_exception(char *e)
{
free(e);
}
#else
static char emergency_buffer[16384];
static bool buffer_allocated[16];
static pthread_mutex_t emergency_malloc_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t emergency_malloc_wait = PTHREAD_COND_INITIALIZER;
static char *emergency_malloc(size_t size)
{
if (size > 1024) { return 0; }
__cxa_thread_info *info = thread_info();
if (info->emergencyBuffersHeld > 3) { return 0; }
pthread_mutex_lock(&emergency_malloc_lock);
int buffer = -1;
while (buffer < 0)
{
void *m = calloc(1, size);
if (0 != m)
{
pthread_mutex_unlock(&emergency_malloc_lock);
return static_cast<char*>(m);
}
for (int i=0 ; i<16 ; i++)
{
if (!buffer_allocated[i])
{
buffer = i;
buffer_allocated[i] = true;
break;
}
}
if (buffer < 0)
{
pthread_cond_wait(&emergency_malloc_wait, &emergency_malloc_lock);
}
}
pthread_mutex_unlock(&emergency_malloc_lock);
info->emergencyBuffersHeld++;
return emergency_buffer + (1024 * buffer);
}
static void emergency_malloc_free(char *ptr)
{
int buffer = -1;
for (int i=0 ; i<16 ; i++)
{
if (ptr == static_cast<void*>(emergency_buffer + (1024 * i)))
{
buffer = i;
break;
}
}
assert(buffer >= 0 &&
"Trying to free something that is not an emergency buffer!");
memset(ptr, 0, 1024);
pthread_mutex_lock(&emergency_malloc_lock);
buffer_allocated[buffer] = false;
pthread_cond_signal(&emergency_malloc_wait);
pthread_mutex_unlock(&emergency_malloc_lock);
}
static char *alloc_or_die(size_t size)
{
char *buffer = static_cast<char*>(calloc(1, size));
if (0 == buffer)
{
buffer = emergency_malloc(size);
if (0 == buffer)
{
fprintf(stderr, "Out of memory attempting to allocate exception\n");
std::terminate();
}
}
return buffer;
}
static void free_exception(char *e)
{
if ((e >= emergency_buffer) &&
(e < (emergency_buffer + sizeof(emergency_buffer))))
{
emergency_malloc_free(e);
}
else
{
free(e);
}
}
#endif
extern "C" void *__cxa_allocate_exception(size_t thrown_size) _LIBCXXRT_NOEXCEPT
{
size_t size = thrown_size + sizeof(__cxa_exception);
char *buffer = alloc_or_die(size);
return buffer+sizeof(__cxa_exception);
}
extern "C" void *__cxa_allocate_dependent_exception(void)
{
size_t size = sizeof(__cxa_dependent_exception);
char *buffer = alloc_or_die(size);
return buffer+sizeof(__cxa_dependent_exception);
}
extern "C" void __cxa_free_exception(void *thrown_exception) _LIBCXXRT_NOEXCEPT
{
__cxa_exception *ex = reinterpret_cast<__cxa_exception*>(thrown_exception) - 1;
if (0 != ex->exceptionDestructor)
{
try
{
ex->exceptionDestructor(thrown_exception);
}
catch(...)
{
std::terminate();
}
}
free_exception(reinterpret_cast<char*>(ex));
}
static void releaseException(__cxa_exception *exception)
{
if (isDependentException(exception->unwindHeader.exception_class))
{
__cxa_free_dependent_exception(exception+1);
return;
}
if (__sync_sub_and_fetch(&exception->referenceCount, 1) == 0)
{
__cxa_free_exception(exception+1);
}
}
void __cxa_free_dependent_exception(void *thrown_exception)
{
__cxa_dependent_exception *ex = reinterpret_cast<__cxa_dependent_exception*>(thrown_exception) - 1;
assert(isDependentException(ex->unwindHeader.exception_class));
if (ex->primaryException)
{
releaseException(realExceptionFromException(reinterpret_cast<__cxa_exception*>(ex)));
}
free_exception(reinterpret_cast<char*>(ex));
}
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
extern "C" void *__cxa_begin_catch(void *e) _LIBCXXRT_NOEXCEPT;
#else
extern "C" void *__cxa_begin_catch(void *e);
#endif
static void report_failure(_Unwind_Reason_Code err, __cxa_exception *thrown_exception)
{
switch (err)
{
default: break;
case _URC_FATAL_PHASE1_ERROR:
fprintf(stderr, "Fatal error during phase 1 unwinding\n");
break;
#if !defined(__arm__) || defined(__ARM_DWARF_EH__)
case _URC_FATAL_PHASE2_ERROR:
fprintf(stderr, "Fatal error during phase 2 unwinding\n");
break;
#endif
case _URC_END_OF_STACK:
__cxa_begin_catch (&(thrown_exception->unwindHeader));
break;
}
std::terminate();
}
static void throw_exception(__cxa_exception *ex)
{
__cxa_thread_info *info = thread_info();
ex->unexpectedHandler = info->unexpectedHandler;
if (0 == ex->unexpectedHandler)
{
ex->unexpectedHandler = unexpectedHandler.load();
}
ex->terminateHandler = info->terminateHandler;
if (0 == ex->terminateHandler)
{
ex->terminateHandler = terminateHandler.load();
}
info->globals.uncaughtExceptions++;
_Unwind_Reason_Code err = _Unwind_RaiseException(&ex->unwindHeader);
report_failure(err, ex);
}
extern "C" __cxa_exception *__cxa_init_primary_exception(
void *object, std::type_info* tinfo, void (*dest)(void *)) _LIBCXXRT_NOEXCEPT {
__cxa_exception *ex = reinterpret_cast<__cxa_exception*>(object) - 1;
ex->referenceCount = 0;
ex->exceptionType = tinfo;
ex->exceptionDestructor = dest;
ex->unwindHeader.exception_class = exception_class;
ex->unwindHeader.exception_cleanup = exception_cleanup;
return ex;
}
extern "C" void __cxa_throw(void *thrown_exception,
std::type_info *tinfo,
void(*dest)(void*))
{
__cxa_exception *ex = __cxa_init_primary_exception(thrown_exception, tinfo, dest);
ex->referenceCount = 1;
throw_exception(ex);
}
extern "C" void __cxa_rethrow_primary_exception(void* thrown_exception)
{
if (NULL == thrown_exception) { return; }
__cxa_exception *original = exceptionFromPointer(thrown_exception);
__cxa_dependent_exception *ex = reinterpret_cast<__cxa_dependent_exception*>(__cxa_allocate_dependent_exception())-1;
ex->primaryException = thrown_exception;
__cxa_increment_exception_refcount(thrown_exception);
ex->exceptionType = original->exceptionType;
ex->unwindHeader.exception_class = dependent_exception_class;
ex->unwindHeader.exception_cleanup = dependent_exception_cleanup;
throw_exception(reinterpret_cast<__cxa_exception*>(ex));
}
extern "C" void *__cxa_current_primary_exception(void)
{
__cxa_eh_globals* globals = __cxa_get_globals();
__cxa_exception *ex = globals->caughtExceptions;
if (0 == ex) { return NULL; }
ex = realExceptionFromException(ex);
__sync_fetch_and_add(&ex->referenceCount, 1);
return ex + 1;
}
extern "C" void __cxa_increment_exception_refcount(void* thrown_exception)
{
if (NULL == thrown_exception) { return; }
__cxa_exception *ex = static_cast<__cxa_exception*>(thrown_exception) - 1;
if (isDependentException(ex->unwindHeader.exception_class)) { return; }
__sync_fetch_and_add(&ex->referenceCount, 1);
}
extern "C" void __cxa_decrement_exception_refcount(void* thrown_exception)
{
if (NULL == thrown_exception) { return; }
__cxa_exception *ex = static_cast<__cxa_exception*>(thrown_exception) - 1;
releaseException(ex);
}
extern "C" void __cxa_rethrow()
{
__cxa_thread_info *ti = thread_info();
__cxa_eh_globals *globals = &ti->globals;
__cxa_exception *ex = globals->caughtExceptions;
if (0 == ex)
{
fprintf(stderr,
"Attempting to rethrow an exception that doesn't exist!\n");
std::terminate();
}
if (ti->foreign_exception_state != __cxa_thread_info::none)
{
ti->foreign_exception_state = __cxa_thread_info::rethrown;
_Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(ex);
_Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(e);
report_failure(err, ex);
return;
}
assert(ex->handlerCount > 0 && "Rethrowing uncaught exception!");
globals->uncaughtExceptions++;
ex->handlerCount = -ex->handlerCount;
_Unwind_Reason_Code err = _Unwind_Resume_or_Rethrow(&ex->unwindHeader);
report_failure(err, ex);
}
static std::type_info *get_type_info_entry(_Unwind_Context *context,
dwarf_eh_lsda *lsda,
int filter)
{
dw_eh_ptr_t record = lsda->type_table -
dwarf_size_of_fixed_size_field(lsda->type_table_encoding)*filter;
dw_eh_ptr_t start = record;
int64_t offset = read_value(lsda->type_table_encoding, &record);
if (offset == 0) { return 0; }
return reinterpret_cast<std::type_info*>(resolve_indirect_value(context,
lsda->type_table_encoding, offset, start));
}
static bool check_type_signature(__cxa_exception *ex,
const std::type_info *type,
void *&adjustedPtr)
{
void *exception_ptr = static_cast<void*>(ex+1);
const std::type_info *ex_type = ex ? ex->exceptionType : 0;
bool is_ptr = ex ? ex_type->__is_pointer_p() : false;
if (is_ptr)
{
exception_ptr = *static_cast<void**>(exception_ptr);
}
if (0 == type)
{
if (ex)
{
adjustedPtr = exception_ptr;
}
return true;
}
if (0 == ex) { return false; }
if (*type == *ex_type)
{
adjustedPtr = exception_ptr;
return true;
}
if (type->__do_catch(ex_type, &exception_ptr, 1))
{
adjustedPtr = exception_ptr;
return true;
}
return false;
}
static handler_type check_action_record(_Unwind_Context *context,
dwarf_eh_lsda *lsda,
dw_eh_ptr_t action_record,
__cxa_exception *ex,
unsigned long *selector,
void *&adjustedPtr)
{
if (!action_record) { return handler_cleanup; }
handler_type found = handler_none;
while (action_record)
{
int filter = read_sleb128(&action_record);
dw_eh_ptr_t action_record_offset_base = action_record;
int displacement = read_sleb128(&action_record);
action_record = displacement ?
action_record_offset_base + displacement : 0;
if (filter > 0)
{
std::type_info *handler_type = get_type_info_entry(context, lsda, filter);
if (check_type_signature(ex, handler_type, adjustedPtr))
{
*selector = filter;
return handler_catch;
}
}
else if (filter < 0 && 0 != ex)
{
bool matched = false;
*selector = filter;
#if defined(__arm__) && !defined(__ARM_DWARF_EH__)
filter++;
std::type_info *handler_type = get_type_info_entry(context, lsda, filter--);
while (handler_type)
{
if (check_type_signature(ex, handler_type, adjustedPtr))
{
matched = true;
break;
}
handler_type = get_type_info_entry(context, lsda, filter--);
}
#else
unsigned char *type_index = reinterpret_cast<unsigned char*>(lsda->type_table) - filter - 1;
while (*type_index)
{
std::type_info *handler_type = get_type_info_entry(context, lsda, *(type_index++));
if (check_type_signature(ex, handler_type, adjustedPtr))
{
matched = true;
break;
}
}
#endif
if (matched) { continue; }
return handler_catch;
}
else if (filter == 0)
{
*selector = filter;
found = handler_cleanup;
}
}
return found;
}
static void pushCleanupException(_Unwind_Exception *exceptionObject,
__cxa_exception *ex)
{
#if defined(__arm__) && !defined(__ARM_DWARF_EH__)
__cxa_thread_info *info = thread_info_fast();
if (ex)
{
ex->cleanupCount++;
if (ex->cleanupCount > 1)
{
assert(exceptionObject == info->currentCleanup);
return;
}
ex->nextCleanup = info->currentCleanup;
}
info->currentCleanup = exceptionObject;
#endif
}
extern "C"
BEGIN_PERSONALITY_FUNCTION(__gxx_personality_v0)
if (1 != version)
{
return _URC_FATAL_PHASE1_ERROR;
}
__cxa_exception *ex = 0;
__cxa_exception *realEx = 0;
bool foreignException = !isCXXException(exceptionClass);
if (!foreignException)
{
ex = exceptionFromPointer(exceptionObject);
realEx = realExceptionFromException(ex);
}
#if defined(__arm__) && !defined(__ARM_DWARF_EH__)
unsigned char *lsda_addr =
static_cast<unsigned char*>(_Unwind_GetLanguageSpecificData(context));
#else
unsigned char *lsda_addr =
reinterpret_cast<unsigned char*>(static_cast<uintptr_t>(_Unwind_GetLanguageSpecificData(context)));
#endif
if (0 == lsda_addr) { return continueUnwinding(exceptionObject, context); }
dwarf_eh_action action = {0};
unsigned long selector = 0;
if (actions & _UA_SEARCH_PHASE)
{
struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
if (!dwarf_eh_find_callsite(context, &lsda, &action))
{
return _URC_HANDLER_FOUND;
}
handler_type found_handler = check_action_record(context, &lsda,
action.action_record, realEx, &selector, ex->adjustedPtr);
if (found_handler == handler_catch)
{
if (ex)
{
saveLandingPad(context, exceptionObject, ex, selector, action.landing_pad);
ex->languageSpecificData = reinterpret_cast<const char*>(lsda_addr);
ex->actionRecord = reinterpret_cast<const char*>(action.action_record);
}
return _URC_HANDLER_FOUND;
}
return continueUnwinding(exceptionObject, context);
}
if (!(actions & _UA_HANDLER_FRAME))
{
struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
dwarf_eh_find_callsite(context, &lsda, &action);
if (0 == action.landing_pad) { return continueUnwinding(exceptionObject, context); }
handler_type found_handler = check_action_record(context, &lsda,
action.action_record, realEx, &selector, ex->adjustedPtr);
if (found_handler != handler_cleanup) { return continueUnwinding(exceptionObject, context); }
pushCleanupException(exceptionObject, ex);
}
else if (foreignException)
{
struct dwarf_eh_lsda lsda = parse_lsda(context, lsda_addr);
dwarf_eh_find_callsite(context, &lsda, &action);
check_action_record(context, &lsda, action.action_record, realEx,
&selector, ex->adjustedPtr);
}
else if (ex->catchTemp == 0)
{
std::terminate();
}
else
{
loadLandingPad(context, exceptionObject, ex, &selector, &action.landing_pad);
ex->catchTemp = 0;
ex->handlerSwitchValue = 0;
}
_Unwind_SetIP(context, reinterpret_cast<unsigned long>(action.landing_pad));
_Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
reinterpret_cast<unsigned long>(exceptionObject));
_Unwind_SetGR(context, __builtin_eh_return_data_regno(1), selector);
return _URC_INSTALL_CONTEXT;
}
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
extern "C" void *__cxa_begin_catch(void *e) _LIBCXXRT_NOEXCEPT
#else
extern "C" void *__cxa_begin_catch(void *e)
#endif
{
__cxa_thread_info *ti = thread_info();
__cxa_eh_globals *globals = &ti->globals;
_Unwind_Exception *exceptionObject = static_cast<_Unwind_Exception*>(e);
if (isCXXException(exceptionObject->exception_class))
{
globals->uncaughtExceptions--;
__cxa_exception *ex = exceptionFromPointer(exceptionObject);
if (ex->handlerCount == 0)
{
ex->nextException = globals->caughtExceptions;
globals->caughtExceptions = ex;
}
if (ex->handlerCount < 0)
{
ex->handlerCount = -ex->handlerCount + 1;
}
else
{
ex->handlerCount++;
}
ti->foreign_exception_state = __cxa_thread_info::none;
return ex->adjustedPtr;
}
else
{
if (globals->caughtExceptions != 0)
{
std::terminate();
}
globals->caughtExceptions = reinterpret_cast<__cxa_exception*>(exceptionObject);
ti->foreign_exception_state = __cxa_thread_info::caught;
}
return (reinterpret_cast<char*>(exceptionObject) + sizeof(_Unwind_Exception));
}
extern "C" void __cxa_end_catch()
{
__cxa_thread_info *ti = thread_info_fast();
__cxa_eh_globals *globals = &ti->globals;
__cxa_exception *ex = globals->caughtExceptions;
assert(0 != ex && "Ending catch when no exception is on the stack!");
if (ti->foreign_exception_state != __cxa_thread_info::none)
{
if (ti->foreign_exception_state != __cxa_thread_info::rethrown)
{
_Unwind_Exception *e = reinterpret_cast<_Unwind_Exception*>(ti->globals.caughtExceptions);
if (e->exception_cleanup)
e->exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, e);
}
globals->caughtExceptions = 0;
ti->foreign_exception_state = __cxa_thread_info::none;
return;
}
bool deleteException = true;
if (ex->handlerCount < 0)
{
ex->handlerCount++;
deleteException = false;
}
else
{
ex->handlerCount--;
}
if (ex->handlerCount == 0)
{
globals->caughtExceptions = ex->nextException;
if (deleteException)
{
releaseException(ex);
}
}
}
extern "C" std::type_info *__cxa_current_exception_type()
{
__cxa_eh_globals *globals = __cxa_get_globals();
__cxa_exception *ex = globals->caughtExceptions;
return ex ? ex->exceptionType : 0;
}
static void end_catch(char *)
{
__cxa_end_catch();
}
extern "C" void __cxa_call_unexpected(void*exception)
{
_Unwind_Exception *exceptionObject = static_cast<_Unwind_Exception*>(exception);
__cxa_begin_catch(exceptionObject);
__attribute__((cleanup(end_catch)))
char unused;
if (exceptionObject->exception_class == exception_class)
{
__cxa_exception *ex = exceptionFromPointer(exceptionObject);
if (ex->unexpectedHandler)
{
ex->unexpectedHandler();
abort();
}
}
std::unexpected();
abort();
}
extern "C" void __cxa_call_terminate(void*exception) _LIBCXXRT_NOEXCEPT
{
std::terminate();
abort();
}
extern "C" void *__cxa_get_exception_ptr(void *exceptionObject)
{
return exceptionFromPointer(exceptionObject)->adjustedPtr;
}
static bool thread_local_handlers = false;
namespace pathscale
{
void set_use_thread_local_handlers(bool flag) _LIBCXXRT_NOEXCEPT
{
thread_local_handlers = flag;
}
unexpected_handler set_unexpected(unexpected_handler f) _LIBCXXRT_NOEXCEPT
{
static __cxa_thread_info *info = thread_info();
unexpected_handler old = info->unexpectedHandler;
info->unexpectedHandler = f;
return old;
}
terminate_handler set_terminate(terminate_handler f) _LIBCXXRT_NOEXCEPT
{
static __cxa_thread_info *info = thread_info();
terminate_handler old = info->terminateHandler;
info->terminateHandler = f;
return old;
}
}
namespace std
{
unexpected_handler set_unexpected(unexpected_handler f) _LIBCXXRT_NOEXCEPT
{
if (thread_local_handlers) { return pathscale::set_unexpected(f); }
return unexpectedHandler.exchange(f);
}
terminate_handler set_terminate(terminate_handler f) _LIBCXXRT_NOEXCEPT
{
if (thread_local_handlers) { return pathscale::set_terminate(f); }
return terminateHandler.exchange(f);
}
[[noreturn]] void terminate() _LIBCXXRT_NOEXCEPT
{
static __cxa_thread_info *info = thread_info();
if (0 != info && 0 != info->terminateHandler)
{
info->terminateHandler();
}
else
{
terminateHandler.load()();
}
abort();
}
[[noreturn]] void unexpected()
{
static __cxa_thread_info *info = thread_info();
if (0 != info && 0 != info->unexpectedHandler)
{
info->unexpectedHandler();
}
else
{
unexpectedHandler.load()();
}
abort();
}
bool uncaught_exception() _LIBCXXRT_NOEXCEPT
{
__cxa_thread_info *info = thread_info();
return info->globals.uncaughtExceptions != 0;
}
int uncaught_exceptions() _LIBCXXRT_NOEXCEPT
{
__cxa_thread_info *info = thread_info();
return info->globals.uncaughtExceptions;
}
unexpected_handler get_unexpected() _LIBCXXRT_NOEXCEPT
{
__cxa_thread_info *info = thread_info();
if (info->unexpectedHandler)
{
return info->unexpectedHandler;
}
return unexpectedHandler.load();
}
terminate_handler get_terminate() _LIBCXXRT_NOEXCEPT
{
__cxa_thread_info *info = thread_info();
if (info->terminateHandler)
{
return info->terminateHandler;
}
return terminateHandler.load();
}
}
#if defined(__arm__) && !defined(__ARM_DWARF_EH__)
extern "C" _Unwind_Exception *__cxa_get_cleanup(void)
{
__cxa_thread_info *info = thread_info_fast();
_Unwind_Exception *exceptionObject = info->currentCleanup;
if (isCXXException(exceptionObject->exception_class))
{
__cxa_exception *ex = exceptionFromPointer(exceptionObject);
ex->cleanupCount--;
if (ex->cleanupCount == 0)
{
info->currentCleanup = ex->nextCleanup;
ex->nextCleanup = 0;
}
}
else
{
info->currentCleanup = 0;
}
return exceptionObject;
}
asm (
".pushsection .text.__cxa_end_cleanup \n"
".global __cxa_end_cleanup \n"
".type __cxa_end_cleanup, \"function\" \n"
"__cxa_end_cleanup: \n"
" push {r1, r2, r3, r4} \n"
" mov r4, lr \n"
" bl __cxa_get_cleanup \n"
" mov lr, r4 \n"
" pop {r1, r2, r3, r4} \n"
" b _Unwind_Resume \n"
" bl abort \n"
".popsection \n"
);
#endif