2009-08-05 12:02:56 +08:00
|
|
|
/* ===-- gcc_personality_v0.c - Implement __gcc_personality_v0 -------------===
|
|
|
|
*
|
|
|
|
* The LLVM Compiler Infrastructure
|
|
|
|
*
|
2010-11-17 06:13:33 +08:00
|
|
|
* This file is dual licensed under the MIT and the University of Illinois Open
|
|
|
|
* Source Licenses. See LICENSE.TXT for details.
|
2009-08-05 12:02:56 +08:00
|
|
|
*
|
|
|
|
* ===----------------------------------------------------------------------===
|
|
|
|
*
|
|
|
|
*/
|
2009-06-27 00:47:03 +08:00
|
|
|
|
2010-04-16 04:37:56 +08:00
|
|
|
#include "int_lib.h"
|
|
|
|
|
2015-01-14 23:55:17 +08:00
|
|
|
#include <unwind.h>
|
2009-06-27 00:47:03 +08:00
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/*
|
|
|
|
* Pointer encodings documented at:
|
|
|
|
* http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DW_EH_PE_omit 0xff /* no data follows */
|
2009-06-27 00:47:03 +08:00
|
|
|
|
|
|
|
#define DW_EH_PE_absptr 0x00
|
|
|
|
#define DW_EH_PE_uleb128 0x01
|
|
|
|
#define DW_EH_PE_udata2 0x02
|
|
|
|
#define DW_EH_PE_udata4 0x03
|
|
|
|
#define DW_EH_PE_udata8 0x04
|
|
|
|
#define DW_EH_PE_sleb128 0x09
|
|
|
|
#define DW_EH_PE_sdata2 0x0A
|
|
|
|
#define DW_EH_PE_sdata4 0x0B
|
|
|
|
#define DW_EH_PE_sdata8 0x0C
|
|
|
|
|
|
|
|
#define DW_EH_PE_pcrel 0x10
|
|
|
|
#define DW_EH_PE_textrel 0x20
|
|
|
|
#define DW_EH_PE_datarel 0x30
|
|
|
|
#define DW_EH_PE_funcrel 0x40
|
|
|
|
#define DW_EH_PE_aligned 0x50
|
2009-08-05 12:02:56 +08:00
|
|
|
#define DW_EH_PE_indirect 0x80 /* gcc extension */
|
2009-06-27 00:47:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/* read a uleb128 encoded value and advance pointer */
|
2009-06-27 00:47:03 +08:00
|
|
|
static uintptr_t readULEB128(const uint8_t** data)
|
|
|
|
{
|
|
|
|
uintptr_t result = 0;
|
|
|
|
uintptr_t shift = 0;
|
|
|
|
unsigned char byte;
|
|
|
|
const uint8_t* p = *data;
|
|
|
|
do {
|
|
|
|
byte = *p++;
|
|
|
|
result |= (byte & 0x7f) << shift;
|
|
|
|
shift += 7;
|
|
|
|
} while (byte & 0x80);
|
|
|
|
*data = p;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/* read a pointer encoded value and advance pointer */
|
2009-06-27 00:47:03 +08:00
|
|
|
static uintptr_t readEncodedPointer(const uint8_t** data, uint8_t encoding)
|
|
|
|
{
|
|
|
|
const uint8_t* p = *data;
|
|
|
|
uintptr_t result = 0;
|
|
|
|
|
|
|
|
if ( encoding == DW_EH_PE_omit )
|
|
|
|
return 0;
|
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/* first get value */
|
2009-06-27 00:47:03 +08:00
|
|
|
switch (encoding & 0x0F) {
|
|
|
|
case DW_EH_PE_absptr:
|
2014-03-13 08:44:37 +08:00
|
|
|
result = *((const uintptr_t*)p);
|
2009-06-27 00:47:03 +08:00
|
|
|
p += sizeof(uintptr_t);
|
|
|
|
break;
|
|
|
|
case DW_EH_PE_uleb128:
|
|
|
|
result = readULEB128(&p);
|
|
|
|
break;
|
|
|
|
case DW_EH_PE_udata2:
|
2014-03-13 08:44:37 +08:00
|
|
|
result = *((const uint16_t*)p);
|
2009-06-27 00:47:03 +08:00
|
|
|
p += sizeof(uint16_t);
|
|
|
|
break;
|
|
|
|
case DW_EH_PE_udata4:
|
2014-03-13 08:44:37 +08:00
|
|
|
result = *((const uint32_t*)p);
|
2009-06-27 00:47:03 +08:00
|
|
|
p += sizeof(uint32_t);
|
|
|
|
break;
|
|
|
|
case DW_EH_PE_udata8:
|
2014-03-13 08:44:37 +08:00
|
|
|
result = *((const uint64_t*)p);
|
2009-06-27 00:47:03 +08:00
|
|
|
p += sizeof(uint64_t);
|
|
|
|
break;
|
|
|
|
case DW_EH_PE_sdata2:
|
2014-03-13 08:44:37 +08:00
|
|
|
result = *((const int16_t*)p);
|
2009-06-27 00:47:03 +08:00
|
|
|
p += sizeof(int16_t);
|
|
|
|
break;
|
|
|
|
case DW_EH_PE_sdata4:
|
2014-03-13 08:44:37 +08:00
|
|
|
result = *((const int32_t*)p);
|
2009-06-27 00:47:03 +08:00
|
|
|
p += sizeof(int32_t);
|
|
|
|
break;
|
|
|
|
case DW_EH_PE_sdata8:
|
2014-03-13 08:44:37 +08:00
|
|
|
result = *((const int64_t*)p);
|
2009-06-27 00:47:03 +08:00
|
|
|
p += sizeof(int64_t);
|
|
|
|
break;
|
|
|
|
case DW_EH_PE_sleb128:
|
|
|
|
default:
|
2009-08-05 12:02:56 +08:00
|
|
|
/* not supported */
|
2010-04-01 01:00:45 +08:00
|
|
|
compilerrt_abort();
|
2009-06-27 00:47:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/* then add relative offset */
|
2009-06-27 00:47:03 +08:00
|
|
|
switch ( encoding & 0x70 ) {
|
|
|
|
case DW_EH_PE_absptr:
|
2009-08-05 12:02:56 +08:00
|
|
|
/* do nothing */
|
2009-06-27 00:47:03 +08:00
|
|
|
break;
|
|
|
|
case DW_EH_PE_pcrel:
|
|
|
|
result += (uintptr_t)(*data);
|
|
|
|
break;
|
|
|
|
case DW_EH_PE_textrel:
|
|
|
|
case DW_EH_PE_datarel:
|
|
|
|
case DW_EH_PE_funcrel:
|
|
|
|
case DW_EH_PE_aligned:
|
|
|
|
default:
|
2009-08-05 12:02:56 +08:00
|
|
|
/* not supported */
|
2010-04-01 01:00:45 +08:00
|
|
|
compilerrt_abort();
|
2009-06-27 00:47:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/* then apply indirection */
|
2009-06-27 00:47:03 +08:00
|
|
|
if (encoding & DW_EH_PE_indirect) {
|
2014-03-13 08:44:37 +08:00
|
|
|
result = *((const uintptr_t*)result);
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*data = p;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-03-09 21:20:17 +08:00
|
|
|
#if defined(__arm__) && !defined(__USING_SJLJ_EXCEPTIONS__) && \
|
|
|
|
!defined(__ARM_DWARF_EH__)
|
|
|
|
#define USING_ARM_EHABI 1
|
|
|
|
_Unwind_Reason_Code __gnu_unwind_frame(struct _Unwind_Exception *,
|
|
|
|
struct _Unwind_Context *);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline _Unwind_Reason_Code
|
|
|
|
continueUnwind(struct _Unwind_Exception *exceptionObject,
|
|
|
|
struct _Unwind_Context *context) {
|
|
|
|
#if USING_ARM_EHABI
|
|
|
|
/*
|
|
|
|
* On ARM EHABI the personality routine is responsible for actually
|
|
|
|
* unwinding a single stack frame before returning (ARM EHABI Sec. 6.1).
|
|
|
|
*/
|
|
|
|
if (__gnu_unwind_frame(exceptionObject, context) != _URC_OK)
|
|
|
|
return _URC_FAILURE;
|
|
|
|
#endif
|
|
|
|
return _URC_CONTINUE_UNWIND;
|
|
|
|
}
|
2009-06-27 00:47:03 +08:00
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/*
|
|
|
|
* The C compiler makes references to __gcc_personality_v0 in
|
|
|
|
* the dwarf unwind information for translation units that use
|
|
|
|
* __attribute__((cleanup(xx))) on local variables.
|
|
|
|
* This personality routine is called by the system unwinder
|
|
|
|
* on each frame as the stack is unwound during a C++ exception
|
|
|
|
* throw through a C function compiled with -fexceptions.
|
|
|
|
*/
|
2014-10-09 01:28:37 +08:00
|
|
|
#if __USING_SJLJ_EXCEPTIONS__
|
2015-12-23 01:22:25 +08:00
|
|
|
/* the setjump-longjump based exceptions personality routine has a
|
|
|
|
* different name */
|
2014-03-01 23:30:50 +08:00
|
|
|
COMPILER_RT_ABI _Unwind_Reason_Code
|
|
|
|
__gcc_personality_sj0(int version, _Unwind_Action actions,
|
2010-05-08 07:44:20 +08:00
|
|
|
uint64_t exceptionClass, struct _Unwind_Exception* exceptionObject,
|
2015-01-14 23:55:17 +08:00
|
|
|
struct _Unwind_Context *context)
|
2016-03-09 21:20:17 +08:00
|
|
|
#elif USING_ARM_EHABI
|
|
|
|
/* The ARM EHABI personality routine has a different signature. */
|
|
|
|
COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
|
|
|
|
_Unwind_State state, struct _Unwind_Exception *exceptionObject,
|
|
|
|
struct _Unwind_Context *context)
|
2010-05-08 07:44:20 +08:00
|
|
|
#else
|
2014-03-01 23:30:50 +08:00
|
|
|
COMPILER_RT_ABI _Unwind_Reason_Code
|
|
|
|
__gcc_personality_v0(int version, _Unwind_Action actions,
|
2009-06-27 00:47:03 +08:00
|
|
|
uint64_t exceptionClass, struct _Unwind_Exception* exceptionObject,
|
2015-01-14 23:55:17 +08:00
|
|
|
struct _Unwind_Context *context)
|
2010-05-08 07:44:20 +08:00
|
|
|
#endif
|
2009-06-27 00:47:03 +08:00
|
|
|
{
|
2009-08-05 12:02:56 +08:00
|
|
|
/* Since C does not have catch clauses, there is nothing to do during */
|
|
|
|
/* phase 1 (the search phase). */
|
2016-03-09 21:20:17 +08:00
|
|
|
#if USING_ARM_EHABI
|
|
|
|
/* After resuming from a cleanup we should also continue on to the next
|
|
|
|
* frame straight away. */
|
|
|
|
if ((state & _US_ACTION_MASK) != _US_UNWIND_FRAME_STARTING)
|
|
|
|
#else
|
|
|
|
if ( actions & _UA_SEARCH_PHASE )
|
|
|
|
#endif
|
|
|
|
return continueUnwind(exceptionObject, context);
|
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/* There is nothing to do if there is no LSDA for this frame. */
|
2015-01-15 22:27:38 +08:00
|
|
|
const uint8_t* lsda = (uint8_t*)_Unwind_GetLanguageSpecificData(context);
|
2011-11-16 09:19:29 +08:00
|
|
|
if ( lsda == (uint8_t*) 0 )
|
2016-03-09 21:20:17 +08:00
|
|
|
return continueUnwind(exceptionObject, context);
|
2009-06-27 00:47:03 +08:00
|
|
|
|
|
|
|
uintptr_t pc = _Unwind_GetIP(context)-1;
|
|
|
|
uintptr_t funcStart = _Unwind_GetRegionStart(context);
|
|
|
|
uintptr_t pcOffset = pc - funcStart;
|
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/* Parse LSDA header. */
|
2009-06-27 00:47:03 +08:00
|
|
|
uint8_t lpStartEncoding = *lsda++;
|
|
|
|
if (lpStartEncoding != DW_EH_PE_omit) {
|
|
|
|
readEncodedPointer(&lsda, lpStartEncoding);
|
|
|
|
}
|
|
|
|
uint8_t ttypeEncoding = *lsda++;
|
|
|
|
if (ttypeEncoding != DW_EH_PE_omit) {
|
|
|
|
readULEB128(&lsda);
|
|
|
|
}
|
2009-08-05 12:02:56 +08:00
|
|
|
/* Walk call-site table looking for range that includes current PC. */
|
2009-06-27 00:47:03 +08:00
|
|
|
uint8_t callSiteEncoding = *lsda++;
|
|
|
|
uint32_t callSiteTableLength = readULEB128(&lsda);
|
|
|
|
const uint8_t* callSiteTableStart = lsda;
|
|
|
|
const uint8_t* callSiteTableEnd = callSiteTableStart + callSiteTableLength;
|
|
|
|
const uint8_t* p=callSiteTableStart;
|
|
|
|
while (p < callSiteTableEnd) {
|
|
|
|
uintptr_t start = readEncodedPointer(&p, callSiteEncoding);
|
|
|
|
uintptr_t length = readEncodedPointer(&p, callSiteEncoding);
|
|
|
|
uintptr_t landingPad = readEncodedPointer(&p, callSiteEncoding);
|
2009-08-05 12:02:56 +08:00
|
|
|
readULEB128(&p); /* action value not used for C code */
|
2009-06-27 00:47:03 +08:00
|
|
|
if ( landingPad == 0 )
|
2009-08-05 12:02:56 +08:00
|
|
|
continue; /* no landing pad for this entry */
|
2009-06-27 00:47:03 +08:00
|
|
|
if ( (start <= pcOffset) && (pcOffset < (start+length)) ) {
|
2009-08-05 12:02:56 +08:00
|
|
|
/* Found landing pad for the PC.
|
|
|
|
* Set Instruction Pointer to so we re-enter function
|
|
|
|
* at landing pad. The landing pad is created by the compiler
|
|
|
|
* to take two parameters in registers.
|
2015-09-28 01:16:46 +08:00
|
|
|
*/
|
|
|
|
_Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
|
|
|
|
(uintptr_t)exceptionObject);
|
2009-06-27 00:47:03 +08:00
|
|
|
_Unwind_SetGR(context, __builtin_eh_return_data_regno(1), 0);
|
2015-09-28 01:16:46 +08:00
|
|
|
_Unwind_SetIP(context, (funcStart + landingPad));
|
2009-06-27 00:47:03 +08:00
|
|
|
return _URC_INSTALL_CONTEXT;
|
|
|
|
}
|
|
|
|
}
|
2015-09-28 01:16:46 +08:00
|
|
|
|
2009-08-05 12:02:56 +08:00
|
|
|
/* No landing pad found, continue unwinding. */
|
2016-03-09 21:20:17 +08:00
|
|
|
return continueUnwind(exceptionObject, context);
|
2009-06-27 00:47:03 +08:00
|
|
|
}
|
|
|
|
|