#include "unwind.h"
extern void abort (void);
typedef unsigned char bool;
typedef struct _ZSt9type_info type_info;
void __attribute__((weak)) __cxa_call_unexpected(_Unwind_Control_Block *ucbp);
bool __attribute__((weak)) __cxa_begin_cleanup(_Unwind_Control_Block *ucbp);
bool __attribute__((weak)) __cxa_type_match(_Unwind_Control_Block *ucbp,
const type_info *rttip,
void **matched_object);
_Unwind_Ptr __attribute__((weak))
__gnu_Unwind_Find_exidx (_Unwind_Ptr, int *);
#define R_IP 12
#define R_SP 13
#define R_LR 14
#define R_PC 15
#define EXIDX_CANTUNWIND 1
#define uint32_highbit (((_uw) 1) << 31)
#define UCB_FORCED_STOP_FN(ucbp) ((ucbp)->unwinder_cache.reserved1)
#define UCB_PR_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved2)
#define UCB_SAVED_CALLSITE_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved3)
#define UCB_FORCED_STOP_ARG(ucbp) ((ucbp)->unwinder_cache.reserved4)
struct core_regs
{
_uw r[16];
};
struct vfp_regs
{
_uw64 d[16];
_uw pad;
};
struct fpa_reg
{
_uw w[3];
};
struct fpa_regs
{
struct fpa_reg f[8];
};
typedef struct
{
_uw16 length;
_uw16 offset;
} EHT16;
typedef struct
{
_uw length;
_uw offset;
} EHT32;
typedef struct
{
_uw demand_save_flags;
struct core_regs core;
_uw prev_sp;
struct vfp_regs vfp;
struct fpa_regs fpa;
} phase1_vrs;
#define DEMAND_SAVE_VFP 1
typedef struct
{
_uw demand_save_flags;
struct core_regs core;
} phase2_vrs;
typedef struct __EIT_entry
{
_uw fnoffset;
_uw content;
} __EIT_entry;
void __attribute__((noreturn)) restore_core_regs (struct core_regs *);
void __gnu_Unwind_Save_VFP (struct vfp_regs * p);
void __gnu_Unwind_Restore_VFP (struct vfp_regs * p);
static void
restore_non_core_regs (phase1_vrs * vrs)
{
if ((vrs->demand_save_flags & DEMAND_SAVE_VFP) == 0)
__gnu_Unwind_Restore_VFP (&vrs->vfp);
}
extern int __text_start;
extern int __data_start;
extern __EIT_entry __exidx_start;
extern __EIT_entry __exidx_end;
extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr0 (_Unwind_State,
_Unwind_Control_Block *, _Unwind_Context *);extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr1 (_Unwind_State,
_Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));
extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr2 (_Unwind_State,
_Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));
_Unwind_VRS_Result _Unwind_VRS_Get (_Unwind_Context *context,
_Unwind_VRS_RegClass regclass,
_uw regno,
_Unwind_VRS_DataRepresentation representation,
void *valuep)
{
phase1_vrs *vrs = (phase1_vrs *) context;
switch (regclass)
{
case _UVRSC_CORE:
if (representation != _UVRSD_UINT32
|| regno > 15)
return _UVRSR_FAILED;
*(_uw *) valuep = vrs->core.r[regno];
return _UVRSR_OK;
case _UVRSC_VFP:
case _UVRSC_FPA:
case _UVRSC_WMMXD:
case _UVRSC_WMMXC:
return _UVRSR_NOT_IMPLEMENTED;
default:
return _UVRSR_FAILED;
}
}
_Unwind_VRS_Result _Unwind_VRS_Set (_Unwind_Context *context,
_Unwind_VRS_RegClass regclass,
_uw regno,
_Unwind_VRS_DataRepresentation representation,
void *valuep)
{
phase1_vrs *vrs = (phase1_vrs *) context;
switch (regclass)
{
case _UVRSC_CORE:
if (representation != _UVRSD_UINT32
|| regno > 15)
return _UVRSR_FAILED;
vrs->core.r[regno] = *(_uw *) valuep;
return _UVRSR_OK;
case _UVRSC_VFP:
case _UVRSC_FPA:
case _UVRSC_WMMXD:
case _UVRSC_WMMXC:
return _UVRSR_NOT_IMPLEMENTED;
default:
return _UVRSR_FAILED;
}
}
_Unwind_VRS_Result _Unwind_VRS_Pop (_Unwind_Context *context,
_Unwind_VRS_RegClass regclass,
_uw discriminator,
_Unwind_VRS_DataRepresentation representation)
{
phase1_vrs *vrs = (phase1_vrs *) context;
switch (regclass)
{
case _UVRSC_CORE:
{
_uw *ptr;
_uw mask;
int i;
if (representation != _UVRSD_UINT32)
return _UVRSR_FAILED;
mask = discriminator & 0xffff;
ptr = (_uw *) vrs->core.r[R_SP];
for (i = 0; i < 16; i++)
{
if (mask & (1 << i))
vrs->core.r[i] = *(ptr++);
}
if ((mask & (1 << R_SP)) == 0)
vrs->core.r[R_SP] = (_uw) ptr;
}
return _UVRSR_OK;
case _UVRSC_VFP:
{
_uw start = discriminator >> 16;
_uw count = discriminator & 0xffff;
struct vfp_regs tmp;
_uw *sp;
_uw *dest;
if ((representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
|| start + count > 16)
return _UVRSR_FAILED;
if (vrs->demand_save_flags & DEMAND_SAVE_VFP)
{
vrs->demand_save_flags &= ~DEMAND_SAVE_VFP;
__gnu_Unwind_Save_VFP (&vrs->vfp);
}
__gnu_Unwind_Save_VFP (&tmp);
sp = (_uw *) vrs->core.r[R_SP];
dest = (_uw *) &tmp.d[start];
count *= 2;
while (count--)
*(dest++) = *(sp++);
if (representation == _UVRSD_VFPX)
sp++;
vrs->core.r[R_SP] = (_uw) sp;
__gnu_Unwind_Restore_VFP (&tmp);
}
return _UVRSR_OK;
case _UVRSC_FPA:
case _UVRSC_WMMXD:
case _UVRSC_WMMXC:
return _UVRSR_NOT_IMPLEMENTED;
default:
return _UVRSR_FAILED;
}
}
static inline _uw
selfrel_offset31 (const _uw *p)
{
_uw offset;
offset = *p;
if (offset & (1 << 30))
offset |= 1u << 31;
else
offset &= ~(1u << 31);
return offset + (_uw) p;
}
static const __EIT_entry *
search_EIT_table (const __EIT_entry * table, int nrec, _uw return_address)
{
_uw next_fn;
_uw this_fn;
int n, left, right;
if (nrec == 0)
return (__EIT_entry *) 0;
left = 0;
right = nrec - 1;
while (1)
{
n = (left + right) / 2;
this_fn = selfrel_offset31 (&table[n].fnoffset);
if (n != nrec - 1)
next_fn = selfrel_offset31 (&table[n + 1].fnoffset) - 1;
else
next_fn = (_uw)0 - 1;
if (return_address < this_fn)
{
if (n == left)
return (__EIT_entry *) 0;
right = n - 1;
}
else if (return_address <= next_fn)
return &table[n];
else
left = n + 1;
}
}
static _Unwind_Reason_Code
get_eit_entry (_Unwind_Control_Block *ucbp, _uw return_address)
{
const __EIT_entry * eitp;
int nrec;
return_address -= 2;
if (__gnu_Unwind_Find_exidx)
{
eitp = (const __EIT_entry *) __gnu_Unwind_Find_exidx (return_address,
&nrec);
if (!eitp)
{
UCB_PR_ADDR (ucbp) = 0;
return _URC_FAILURE;
}
}
else
{
eitp = &__exidx_start;
nrec = &__exidx_end - &__exidx_start;
}
eitp = search_EIT_table (eitp, nrec, return_address);
if (!eitp)
{
UCB_PR_ADDR (ucbp) = 0;
return _URC_FAILURE;
}
ucbp->pr_cache.fnstart = selfrel_offset31 (&eitp->fnoffset);
if (eitp->content == EXIDX_CANTUNWIND)
{
UCB_PR_ADDR (ucbp) = 0;
return _URC_END_OF_STACK;
}
if (eitp->content & uint32_highbit)
{
ucbp->pr_cache.ehtp = (_Unwind_EHT_Header *)&eitp->content;
ucbp->pr_cache.additional = 1;
}
else
{
ucbp->pr_cache.ehtp =
(_Unwind_EHT_Header *) selfrel_offset31 (&eitp->content);
ucbp->pr_cache.additional = 0;
}
if (*ucbp->pr_cache.ehtp & (1u << 31))
{
_uw idx = (*(_uw *) ucbp->pr_cache.ehtp >> 24) & 0xf;
if (idx == 0)
UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr0;
else if (idx == 1)
UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr1;
else if (idx == 2)
UCB_PR_ADDR (ucbp) = (_uw) &__aeabi_unwind_cpp_pr2;
else
{
UCB_PR_ADDR (ucbp) = 0;
return _URC_FAILURE;
}
}
else
{
UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp);
}
return _URC_OK;
}
static void __attribute__((noreturn))
unwind_phase2 (_Unwind_Control_Block * ucbp, phase2_vrs * vrs)
{
_Unwind_Reason_Code pr_result;
do
{
if (get_eit_entry (ucbp, vrs->core.r[R_PC]) != _URC_OK)
abort ();
UCB_SAVED_CALLSITE_ADDR (ucbp) = vrs->core.r[R_PC];
pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
(_US_UNWIND_FRAME_STARTING, ucbp, (_Unwind_Context *) vrs);
}
while (pr_result == _URC_CONTINUE_UNWIND);
if (pr_result != _URC_INSTALL_CONTEXT)
abort();
restore_core_regs (&vrs->core);
}
static _Unwind_Reason_Code
unwind_phase2_forced (_Unwind_Control_Block *ucbp, phase2_vrs *entry_vrs,
int resuming)
{
_Unwind_Stop_Fn stop_fn = (_Unwind_Stop_Fn) UCB_FORCED_STOP_FN (ucbp);
void *stop_arg = (void *)UCB_FORCED_STOP_ARG (ucbp);
_Unwind_Reason_Code pr_result = 0;
phase1_vrs saved_vrs, next_vrs;
saved_vrs.core = entry_vrs->core;
saved_vrs.demand_save_flags = 0;
do
{
_Unwind_State action;
_Unwind_Reason_Code entry_code;
_Unwind_Reason_Code stop_code;
entry_code = get_eit_entry (ucbp, saved_vrs.core.r[R_PC]);
if (resuming)
{
action = _US_UNWIND_FRAME_RESUME | _US_FORCE_UNWIND;
resuming = 0;
}
else
action = _US_UNWIND_FRAME_STARTING | _US_FORCE_UNWIND;
if (entry_code == _URC_OK)
{
UCB_SAVED_CALLSITE_ADDR (ucbp) = saved_vrs.core.r[R_PC];
next_vrs = saved_vrs;
pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
(action, ucbp, (void *) &next_vrs);
saved_vrs.prev_sp = next_vrs.core.r[R_SP];
}
else
{
action |= _US_END_OF_STACK;
saved_vrs.prev_sp = saved_vrs.core.r[R_SP];
}
stop_code = stop_fn (1, action, ucbp->exception_class, ucbp,
(void *)&saved_vrs, stop_arg);
if (stop_code != _URC_NO_REASON)
return _URC_FAILURE;
if (entry_code != _URC_OK)
return entry_code;
saved_vrs = next_vrs;
}
while (pr_result == _URC_CONTINUE_UNWIND);
if (pr_result != _URC_INSTALL_CONTEXT)
{
return _URC_FAILURE;
}
restore_core_regs (&saved_vrs.core);
}
_Unwind_Word
_Unwind_GetCFA (_Unwind_Context *context)
{
return ((phase1_vrs *) context)->prev_sp;
}
_Unwind_Reason_Code
__gnu_Unwind_RaiseException (_Unwind_Control_Block *, phase2_vrs *);
_Unwind_Reason_Code
__gnu_Unwind_RaiseException (_Unwind_Control_Block * ucbp,
phase2_vrs * entry_vrs)
{
phase1_vrs saved_vrs;
_Unwind_Reason_Code pr_result;
entry_vrs->core.r[R_PC] = entry_vrs->core.r[R_LR];
saved_vrs.core = entry_vrs->core;
saved_vrs.demand_save_flags = ~(_uw) 0;
do
{
if (get_eit_entry (ucbp, saved_vrs.core.r[R_PC]) != _URC_OK)
return _URC_FAILURE;
pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
(_US_VIRTUAL_UNWIND_FRAME, ucbp, (void *) &saved_vrs);
}
while (pr_result == _URC_CONTINUE_UNWIND);
restore_non_core_regs (&saved_vrs);
if (pr_result != _URC_HANDLER_FOUND)
{
return _URC_FAILURE;
}
unwind_phase2 (ucbp, entry_vrs);
}
_Unwind_Reason_Code
__gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *,
_Unwind_Stop_Fn, void *, phase2_vrs *);
_Unwind_Reason_Code
__gnu_Unwind_ForcedUnwind (_Unwind_Control_Block *ucbp,
_Unwind_Stop_Fn stop_fn, void *stop_arg,
phase2_vrs *entry_vrs)
{
UCB_FORCED_STOP_FN (ucbp) = (_uw) stop_fn;
UCB_FORCED_STOP_ARG (ucbp) = (_uw) stop_arg;
entry_vrs->core.r[R_PC] = entry_vrs->core.r[R_LR];
return unwind_phase2_forced (ucbp, entry_vrs, 0);
}
_Unwind_Reason_Code
__gnu_Unwind_Resume (_Unwind_Control_Block *, phase2_vrs *);
_Unwind_Reason_Code
__gnu_Unwind_Resume (_Unwind_Control_Block * ucbp, phase2_vrs * entry_vrs)
{
_Unwind_Reason_Code pr_result;
entry_vrs->core.r[R_PC] = UCB_SAVED_CALLSITE_ADDR (ucbp);
if (UCB_FORCED_STOP_FN (ucbp))
{
unwind_phase2_forced (ucbp, entry_vrs, 1);
abort ();
}
pr_result = ((personality_routine) UCB_PR_ADDR (ucbp))
(_US_UNWIND_FRAME_RESUME, ucbp, (_Unwind_Context *) entry_vrs);
switch (pr_result)
{
case _URC_INSTALL_CONTEXT:
restore_core_regs (&entry_vrs->core);
case _URC_CONTINUE_UNWIND:
unwind_phase2 (ucbp, entry_vrs);
default:
abort ();
}
}
_Unwind_Reason_Code
__gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block *, phase2_vrs *);
_Unwind_Reason_Code
__gnu_Unwind_Resume_or_Rethrow (_Unwind_Control_Block * ucbp,
phase2_vrs * entry_vrs)
{
if (!UCB_FORCED_STOP_FN (ucbp))
return __gnu_Unwind_RaiseException (ucbp, entry_vrs);
entry_vrs->core.r[R_PC] = entry_vrs->core.r[R_LR];
return unwind_phase2_forced (ucbp, entry_vrs, 0);
}
void
_Unwind_Complete (_Unwind_Control_Block * ucbp __attribute__((unused)))
{
}
static inline _Unwind_Control_Block *
unwind_UCB_from_context (_Unwind_Context * context)
{
return (_Unwind_Control_Block *) _Unwind_GetGR (context, R_IP);
}
void
_Unwind_DeleteException (_Unwind_Exception * exc)
{
if (exc->exception_cleanup)
(*exc->exception_cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exc);
}
static _Unwind_Reason_Code
__gnu_unwind_pr_common (_Unwind_State state,
_Unwind_Control_Block *ucbp,
_Unwind_Context *context,
int id)
{
__gnu_unwind_state uws;
_uw *data;
_uw offset;
_uw len;
_uw rtti_count;
int phase2_call_unexpected_after_unwind = 0;
int in_range = 0;
int forced_unwind = state & _US_FORCE_UNWIND;
state &= _US_ACTION_MASK;
data = (_uw *) ucbp->pr_cache.ehtp;
uws.data = *(data++);
uws.next = data;
if (id == 0)
{
uws.data <<= 8;
uws.words_left = 0;
uws.bytes_left = 3;
}
else
{
uws.words_left = (uws.data >> 16) & 0xff;
uws.data <<= 16;
uws.bytes_left = 2;
data += uws.words_left;
}
if (state == _US_UNWIND_FRAME_RESUME)
data = (_uw *) ucbp->cleanup_cache.bitpattern[0];
if ((ucbp->pr_cache.additional & 1) == 0)
{
while (*data)
{
_uw addr;
_uw fnstart;
if (id == 2)
{
len = ((EHT32 *) data)->length;
offset = ((EHT32 *) data)->offset;
data += 2;
}
else
{
len = ((EHT16 *) data)->length;
offset = ((EHT16 *) data)->offset;
data++;
}
fnstart = ucbp->pr_cache.fnstart + (offset & ~1);
addr = _Unwind_GetGR (context, R_PC);
in_range = (fnstart <= addr && addr < fnstart + (len & ~1));
switch (((offset & 1) << 1) | (len & 1))
{
case 0:
if (state != _US_VIRTUAL_UNWIND_FRAME
&& in_range)
{
_uw lp;
lp = selfrel_offset31 (data);
data++;
ucbp->cleanup_cache.bitpattern[0] = (_uw) data;
if (!__cxa_begin_cleanup (ucbp))
return _URC_FAILURE;
_Unwind_SetGR (context, R_PC, lp);
return _URC_INSTALL_CONTEXT;
}
data++;
break;
case 1:
if (state == _US_VIRTUAL_UNWIND_FRAME)
{
if (in_range)
{
_uw rtti;
void *matched;
if (data[1] == (_uw) -2)
return _URC_FAILURE;
matched = (void *)(ucbp + 1);
if (data[1] != (_uw) -1)
{
rtti = _Unwind_decode_target2 ((_uw) &data[1]);
if (!__cxa_type_match (ucbp, (type_info *) rtti,
&matched))
matched = (void *)0;
}
if (matched)
{
ucbp->barrier_cache.sp =
_Unwind_GetGR (context, R_SP);
ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
ucbp->barrier_cache.bitpattern[1] = (_uw) data;
return _URC_HANDLER_FOUND;
}
}
}
else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
&& ucbp->barrier_cache.bitpattern[1] == (_uw) data)
{
_uw lp;
lp = selfrel_offset31 (data);
_Unwind_SetGR (context, R_PC, lp);
_Unwind_SetGR (context, 0, (_uw) ucbp);
return _URC_INSTALL_CONTEXT;
}
data += 2;
break;
case 2:
rtti_count = data[0] & 0x7fffffff;
if (state == _US_VIRTUAL_UNWIND_FRAME)
{
if (in_range && (!forced_unwind || !rtti_count))
{
_uw i;
_uw rtti;
void *matched;
for (i = 0; i < rtti_count; i++)
{
matched = (void *)(ucbp + 1);
rtti = _Unwind_decode_target2 ((_uw) &data[i + 1]);
if (__cxa_type_match (ucbp, (type_info *) rtti,
&matched))
break;
}
if (i == rtti_count)
{
ucbp->barrier_cache.sp =
_Unwind_GetGR (context, R_SP);
ucbp->barrier_cache.bitpattern[0] = (_uw) matched;
ucbp->barrier_cache.bitpattern[1] = (_uw) data;
return _URC_HANDLER_FOUND;
}
}
}
else if (ucbp->barrier_cache.sp == _Unwind_GetGR (context, R_SP)
&& ucbp->barrier_cache.bitpattern[1] == (_uw) data)
{
_uw lp;
ucbp->barrier_cache.bitpattern[1] = rtti_count;
ucbp->barrier_cache.bitpattern[2] = 0;
ucbp->barrier_cache.bitpattern[3] = 4;
ucbp->barrier_cache.bitpattern[4] = (_uw) &data[1];
if (data[0] & uint32_highbit)
phase2_call_unexpected_after_unwind = 1;
else
{
data += rtti_count + 1;
lp = selfrel_offset31 (data);
data++;
_Unwind_SetGR (context, R_PC, lp);
_Unwind_SetGR (context, 0, (_uw) ucbp);
return _URC_INSTALL_CONTEXT;
}
}
if (data[0] & uint32_highbit)
data++;
data += rtti_count + 1;
break;
default:
return _URC_FAILURE;
}
}
}
if (__gnu_unwind_execute (context, &uws) != _URC_OK)
return _URC_FAILURE;
if (phase2_call_unexpected_after_unwind)
{
_Unwind_SetGR (context, R_LR, _Unwind_GetGR (context, R_PC));
_Unwind_SetGR (context, R_PC, (_uw) &__cxa_call_unexpected);
return _URC_INSTALL_CONTEXT;
}
return _URC_CONTINUE_UNWIND;
}
_Unwind_Reason_Code
__aeabi_unwind_cpp_pr0 (_Unwind_State state,
_Unwind_Control_Block *ucbp,
_Unwind_Context *context)
{
return __gnu_unwind_pr_common (state, ucbp, context, 0);
}
_Unwind_Reason_Code
__aeabi_unwind_cpp_pr1 (_Unwind_State state,
_Unwind_Control_Block *ucbp,
_Unwind_Context *context)
{
return __gnu_unwind_pr_common (state, ucbp, context, 1);
}
_Unwind_Reason_Code
__aeabi_unwind_cpp_pr2 (_Unwind_State state,
_Unwind_Control_Block *ucbp,
_Unwind_Context *context)
{
return __gnu_unwind_pr_common (state, ucbp, context, 2);
}
_Unwind_Ptr
_Unwind_GetDataRelBase (_Unwind_Context *context __attribute__ ((unused)))
{
abort ();
}
_Unwind_Ptr
_Unwind_GetTextRelBase (_Unwind_Context *context __attribute__ ((unused)))
{
abort ();
}