#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <setjmp.h>
#if !FOR_DYLD
#include <System/pthread_machdep.h>
#endif
#include "unwind.h"
#include "InternalMacros.h"
#if __arm__
struct _Unwind_FunctionContext
{
struct _Unwind_FunctionContext* prev;
uintptr_t resumeLocation;
uintptr_t resumeParameters[4];
__personality_routine personality; uintptr_t lsda;
void* jbuf[];
};
#if FOR_DYLD
extern struct _Unwind_FunctionContext* __Unwind_SjLj_GetTopOfFunctionStack();
extern void __Unwind_SjLj_SetTopOfFunctionStack(struct _Unwind_FunctionContext* fc);
#else
static struct _Unwind_FunctionContext* __Unwind_SjLj_GetTopOfFunctionStack()
{
return (struct _Unwind_FunctionContext*)_pthread_getspecific_direct(__PTK_LIBC_DYLD_Unwind_SjLj_Key);
}
static void __Unwind_SjLj_SetTopOfFunctionStack(struct _Unwind_FunctionContext* fc)
{
_pthread_setspecific_direct(__PTK_LIBC_DYLD_Unwind_SjLj_Key, fc);
}
#endif
EXPORT void _Unwind_SjLj_Register(struct _Unwind_FunctionContext* fc)
{
fc->prev = __Unwind_SjLj_GetTopOfFunctionStack();
__Unwind_SjLj_SetTopOfFunctionStack(fc);
}
EXPORT void _Unwind_SjLj_Unregister(struct _Unwind_FunctionContext* fc)
{
__Unwind_SjLj_SetTopOfFunctionStack(fc->prev);
}
static _Unwind_Reason_Code unwind_phase1(struct _Unwind_Exception* exception_object)
{
_Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack();
DEBUG_PRINT_UNWINDING("unwind_phase1: initial function-context=%p\n", c);
for (bool handlerNotFound = true; handlerNotFound; c = c->prev) {
if ( c == NULL ) {
DEBUG_PRINT_UNWINDING("unwind_phase1(ex_ojb=%p): reached bottom => _URC_END_OF_STACK\n", exception_object);
return _URC_END_OF_STACK;
}
DEBUG_PRINT_UNWINDING("unwind_phase1: function-context=%p\n", c);
if ( c->personality != NULL ) {
DEBUG_PRINT_UNWINDING("unwind_phase1(ex_ojb=%p): calling personality function %p\n", exception_object, c->personality);
_Unwind_Reason_Code personalityResult = (*c->personality)(1, _UA_SEARCH_PHASE,
exception_object->exception_class, exception_object,
(struct _Unwind_Context*)c);
switch ( personalityResult ) {
case _URC_HANDLER_FOUND:
handlerNotFound = false;
exception_object->private_2 = (uintptr_t)c;
DEBUG_PRINT_UNWINDING("unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND\n", exception_object);
return _URC_NO_REASON;
case _URC_CONTINUE_UNWIND:
DEBUG_PRINT_UNWINDING("unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND\n", exception_object);
break;
default:
DEBUG_PRINT_UNWINDING("unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR\n", exception_object);
return _URC_FATAL_PHASE1_ERROR;
}
}
}
return _URC_NO_REASON;
}
static _Unwind_Reason_Code unwind_phase2(struct _Unwind_Exception* exception_object)
{
DEBUG_PRINT_UNWINDING("unwind_phase2(ex_ojb=%p)\n", exception_object);
_Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack();
while ( true ) {
DEBUG_PRINT_UNWINDING("unwind_phase2s(ex_ojb=%p): function-context=%p\n", exception_object, c);
if ( c == NULL ) {
DEBUG_PRINT_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step() reached bottom => _URC_END_OF_STACK\n", exception_object);
return _URC_END_OF_STACK;
}
if ( c->personality != NULL ) {
_Unwind_Action action = _UA_CLEANUP_PHASE;
if ( (uintptr_t)c == exception_object->private_2 )
action = (_Unwind_Action)(_UA_CLEANUP_PHASE|_UA_HANDLER_FRAME); _Unwind_Reason_Code personalityResult = (*c->personality)(1, action,
exception_object->exception_class, exception_object,
(struct _Unwind_Context*)c);
switch ( personalityResult ) {
case _URC_CONTINUE_UNWIND:
DEBUG_PRINT_UNWINDING("unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND\n", exception_object);
if ( (uintptr_t)c == exception_object->private_2 ) {
ABORT("during phase1 personality function said it would stop here, but now if phase2 it did not stop here");
}
break;
case _URC_INSTALL_CONTEXT:
DEBUG_PRINT_UNWINDING("unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT, will resume at landing pad %p\n", exception_object, c->jbuf[1]);
__Unwind_SjLj_SetTopOfFunctionStack(c);
__builtin_longjmp(c->jbuf, 1);
return _URC_FATAL_PHASE2_ERROR;
default:
DEBUG_MESSAGE("personality function returned unknown result %d", personalityResult);
return _URC_FATAL_PHASE2_ERROR;
}
}
c = c->prev;
}
return _URC_FATAL_PHASE2_ERROR;
}
static _Unwind_Reason_Code unwind_phase2_forced(struct _Unwind_Exception* exception_object,
_Unwind_Stop_Fn stop, void* stop_parameter)
{
_Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack();
while ( true ) {
if ( c == NULL ) {
DEBUG_PRINT_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step() reached bottom => _URC_END_OF_STACK\n", exception_object);
return _URC_END_OF_STACK;
}
_Unwind_Action action = (_Unwind_Action)(_UA_FORCE_UNWIND|_UA_CLEANUP_PHASE);
_Unwind_Reason_Code stopResult = (*stop)(1, action,
exception_object->exception_class, exception_object,
(struct _Unwind_Context*)c, stop_parameter);
DEBUG_PRINT_UNWINDING("unwind_phase2_forced(ex_ojb=%p): stop function returned %d\n", exception_object, stopResult);
if ( stopResult != _URC_NO_REASON ) {
DEBUG_PRINT_UNWINDING("unwind_phase2_forced(ex_ojb=%p): stopped by stop function\n", exception_object);
return _URC_FATAL_PHASE2_ERROR;
}
if ( c->personality != NULL ) {
__personality_routine p = (__personality_routine)c->personality;
DEBUG_PRINT_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling personality function %p\n", exception_object, p);
_Unwind_Reason_Code personalityResult = (*p)(1, action,
exception_object->exception_class, exception_object,
(struct _Unwind_Context*)c);
switch ( personalityResult ) {
case _URC_CONTINUE_UNWIND:
DEBUG_PRINT_UNWINDING("unwind_phase2_forced(ex_ojb=%p): personality returned _URC_CONTINUE_UNWIND\n", exception_object);
break;
case _URC_INSTALL_CONTEXT:
DEBUG_PRINT_UNWINDING("unwind_phase2_forced(ex_ojb=%p): personality returned _URC_INSTALL_CONTEXT\n", exception_object);
__Unwind_SjLj_SetTopOfFunctionStack(c);
__builtin_longjmp(c->jbuf, 1);
break;
default:
DEBUG_PRINT_UNWINDING("unwind_phase2_forced(ex_ojb=%p): personality returned %d, _URC_FATAL_PHASE2_ERROR\n",
exception_object, personalityResult);
return _URC_FATAL_PHASE2_ERROR;
}
}
c = c->prev;
}
DEBUG_PRINT_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop function with _UA_END_OF_STACK\n", exception_object);
_Unwind_Action lastAction = (_Unwind_Action)(_UA_FORCE_UNWIND|_UA_CLEANUP_PHASE|_UA_END_OF_STACK);
(*stop)(1, lastAction, exception_object->exception_class, exception_object, (struct _Unwind_Context*)c, stop_parameter);
return _URC_FATAL_PHASE2_ERROR;
}
EXPORT _Unwind_Reason_Code _Unwind_SjLj_RaiseException(struct _Unwind_Exception* exception_object)
{
DEBUG_PRINT_API("_Unwind_SjLj_RaiseException(ex_obj=%p)\n", exception_object);
exception_object->private_1 = 0;
exception_object->private_2 = 0;
_Unwind_Reason_Code phase1 = unwind_phase1(exception_object);
if ( phase1 != _URC_NO_REASON )
return phase1;
return unwind_phase2(exception_object);
}
EXPORT void _Unwind_SjLj_Resume(struct _Unwind_Exception* exception_object)
{
DEBUG_PRINT_API("_Unwind_SjLj_Resume(ex_obj=%p)\n", exception_object);
if ( exception_object->private_1 != 0 )
unwind_phase2_forced(exception_object, (_Unwind_Stop_Fn)exception_object->private_1, (void*)exception_object->private_2);
else
unwind_phase2(exception_object);
ABORT("_Unwind_SjLj_Resume() can't return");
}
EXPORT _Unwind_Reason_Code _Unwind_SjLj_Resume_or_Rethrow(struct _Unwind_Exception* exception_object)
{
DEBUG_PRINT_API("__Unwind_SjLj_Resume_or_Rethrow(ex_obj=%p), private_1=%ld\n", exception_object, exception_object->private_1);
if ( exception_object->private_1 == 0 ) {
return _Unwind_SjLj_RaiseException(exception_object);
}
_Unwind_SjLj_Resume(exception_object);
ABORT("__Unwind_SjLj_Resume_or_Rethrow() called _Unwind_SjLj_Resume() which unexpectedly returned");
}
EXPORT uintptr_t _Unwind_GetLanguageSpecificData(struct _Unwind_Context* context)
{
_Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t)context;
DEBUG_PRINT_API("_Unwind_GetLanguageSpecificData(context=%p) => 0x%0lX\n", context, ufc->lsda);
return ufc->lsda;
}
EXPORT uintptr_t _Unwind_GetGR(struct _Unwind_Context* context, int index)
{
DEBUG_PRINT_API("_Unwind_GetGR(context=%p, reg=%d)\n", context, index);
_Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t)context;
return ufc->resumeParameters[index];
}
EXPORT void _Unwind_SetGR(struct _Unwind_Context* context, int index, uintptr_t new_value)
{
DEBUG_PRINT_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0lX)\n", context, index, new_value);
_Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t)context;
ufc->resumeParameters[index] = new_value;
}
EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context* context)
{
_Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t)context;
DEBUG_PRINT_API("_Unwind_GetIP(context=%p) => 0x%lX\n", context, ufc->resumeLocation+1);
return ufc->resumeLocation+1;
}
EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context* context, int* ipBefore)
{
_Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t)context;
*ipBefore = 0;
DEBUG_PRINT_API("_Unwind_GetIPInfo(context=%p, %p) => 0x%lX\n", context, ipBefore, ufc->resumeLocation+1);
return ufc->resumeLocation+1;
}
EXPORT void _Unwind_SetIP(struct _Unwind_Context* context, uintptr_t new_value)
{
DEBUG_PRINT_API("_Unwind_SetIP(context=%p, value=0x%0lX)\n", context, new_value);
_Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t)context;
ufc->resumeLocation = new_value-1;
}
EXPORT uintptr_t _Unwind_GetRegionStart(struct _Unwind_Context* context)
{
DEBUG_PRINT_API("_Unwind_GetRegionStart(context=%p)\n", context);
return 0;
}
EXPORT void _Unwind_DeleteException(struct _Unwind_Exception* exception_object)
{
DEBUG_PRINT_API("_Unwind_DeleteException(ex_obj=%p)\n", exception_object);
if ( exception_object->exception_cleanup != NULL )
(*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT, exception_object);
}
EXPORT uintptr_t _Unwind_GetDataRelBase(struct _Unwind_Context* context)
{
DEBUG_PRINT_API("_Unwind_GetDataRelBase(context=%p)\n", context);
ABORT("_Unwind_GetDataRelBase() not implemented");
}
EXPORT uintptr_t _Unwind_GetTextRelBase(struct _Unwind_Context* context)
{
DEBUG_PRINT_API("_Unwind_GetTextRelBase(context=%p)\n", context);
ABORT("_Unwind_GetTextRelBase() not implemented");
}
EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context* context)
{
DEBUG_PRINT_API("_Unwind_GetCFA(context=%p)\n", context);
if ( context != NULL ) {
_Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t)context;
return (uintptr_t)ufc->jbuf[2];
}
return 0;
}
#if !FOR_DYLD && __IPHONE_OS_VERSION_MIN_REQUIRED
NOT_HERE_BEFORE_5_0(_Unwind_GetLanguageSpecificData)
NOT_HERE_BEFORE_5_0(_Unwind_GetRegionStart)
NOT_HERE_BEFORE_5_0(_Unwind_GetIP)
NOT_HERE_BEFORE_5_0(_Unwind_SetGR)
NOT_HERE_BEFORE_5_0(_Unwind_SetIP)
NOT_HERE_BEFORE_5_0(_Unwind_DeleteException)
NOT_HERE_BEFORE_5_0(_Unwind_SjLj_Register)
NOT_HERE_BEFORE_5_0(_Unwind_GetGR)
NOT_HERE_BEFORE_5_0(_Unwind_GetIPInfo)
NOT_HERE_BEFORE_5_0(_Unwind_GetCFA)
NOT_HERE_BEFORE_5_0(_Unwind_SjLj_Resume)
NOT_HERE_BEFORE_5_0(_Unwind_SjLj_RaiseException)
NOT_HERE_BEFORE_5_0(_Unwind_SjLj_Resume_or_Rethrow)
NOT_HERE_BEFORE_5_0(_Unwind_SjLj_Unregister)
#endif // !FOR_DYLD
#endif // __arm__