Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 1 | /* ===-- gcc_personality_v0.c - Implement __gcc_personality_v0 -------------=== |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
| 5 | * This file is distributed under the University of Illinois Open Source |
| 6 | * License. See LICENSE.TXT for details. |
| 7 | * |
| 8 | * ===----------------------------------------------------------------------=== |
| 9 | * |
| 10 | */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 11 | |
| 12 | #include <stdint.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | |
Nick Kledzik | 5b0a062 | 2010-04-15 20:37:56 +0000 | [diff] [blame] | 16 | #include "int_lib.h" |
| 17 | |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 18 | /* |
| 19 | * _Unwind_* stuff based on C++ ABI public documentation |
| 20 | * http://refspecs.freestandards.org/abi-eh-1.21.html |
| 21 | */ |
| 22 | |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 23 | typedef enum { |
| 24 | _URC_NO_REASON = 0, |
| 25 | _URC_FOREIGN_EXCEPTION_CAUGHT = 1, |
| 26 | _URC_FATAL_PHASE2_ERROR = 2, |
| 27 | _URC_FATAL_PHASE1_ERROR = 3, |
| 28 | _URC_NORMAL_STOP = 4, |
| 29 | _URC_END_OF_STACK = 5, |
| 30 | _URC_HANDLER_FOUND = 6, |
| 31 | _URC_INSTALL_CONTEXT = 7, |
| 32 | _URC_CONTINUE_UNWIND = 8 |
| 33 | } _Unwind_Reason_Code; |
| 34 | |
| 35 | typedef enum { |
| 36 | _UA_SEARCH_PHASE = 1, |
| 37 | _UA_CLEANUP_PHASE = 2, |
| 38 | _UA_HANDLER_FRAME = 4, |
| 39 | _UA_FORCE_UNWIND = 8, |
| 40 | _UA_END_OF_STACK = 16 |
| 41 | } _Unwind_Action; |
| 42 | |
| 43 | typedef struct _Unwind_Context* _Unwind_Context_t; |
| 44 | |
| 45 | struct _Unwind_Exception { |
| 46 | uint64_t exception_class; |
Nick Kledzik | ac02b5a | 2010-01-20 06:13:20 +0000 | [diff] [blame] | 47 | void (*exception_cleanup)(_Unwind_Reason_Code reason, |
| 48 | struct _Unwind_Exception* exc); |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 49 | uintptr_t private_1; |
| 50 | uintptr_t private_2; |
| 51 | }; |
| 52 | |
| 53 | extern const uint8_t* _Unwind_GetLanguageSpecificData(_Unwind_Context_t c); |
| 54 | extern void _Unwind_SetGR(_Unwind_Context_t c, int i, uintptr_t n); |
| 55 | extern void _Unwind_SetIP(_Unwind_Context_t, uintptr_t new_value); |
| 56 | extern uintptr_t _Unwind_GetIP(_Unwind_Context_t context); |
| 57 | extern uintptr_t _Unwind_GetRegionStart(_Unwind_Context_t context); |
| 58 | |
| 59 | |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 60 | /* |
| 61 | * Pointer encodings documented at: |
| 62 | * http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html |
| 63 | */ |
| 64 | |
| 65 | #define DW_EH_PE_omit 0xff /* no data follows */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 66 | |
| 67 | #define DW_EH_PE_absptr 0x00 |
| 68 | #define DW_EH_PE_uleb128 0x01 |
| 69 | #define DW_EH_PE_udata2 0x02 |
| 70 | #define DW_EH_PE_udata4 0x03 |
| 71 | #define DW_EH_PE_udata8 0x04 |
| 72 | #define DW_EH_PE_sleb128 0x09 |
| 73 | #define DW_EH_PE_sdata2 0x0A |
| 74 | #define DW_EH_PE_sdata4 0x0B |
| 75 | #define DW_EH_PE_sdata8 0x0C |
| 76 | |
| 77 | #define DW_EH_PE_pcrel 0x10 |
| 78 | #define DW_EH_PE_textrel 0x20 |
| 79 | #define DW_EH_PE_datarel 0x30 |
| 80 | #define DW_EH_PE_funcrel 0x40 |
| 81 | #define DW_EH_PE_aligned 0x50 |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 82 | #define DW_EH_PE_indirect 0x80 /* gcc extension */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 86 | /* read a uleb128 encoded value and advance pointer */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 87 | static uintptr_t readULEB128(const uint8_t** data) |
| 88 | { |
| 89 | uintptr_t result = 0; |
| 90 | uintptr_t shift = 0; |
| 91 | unsigned char byte; |
| 92 | const uint8_t* p = *data; |
| 93 | do { |
| 94 | byte = *p++; |
| 95 | result |= (byte & 0x7f) << shift; |
| 96 | shift += 7; |
| 97 | } while (byte & 0x80); |
| 98 | *data = p; |
| 99 | return result; |
| 100 | } |
| 101 | |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 102 | /* read a pointer encoded value and advance pointer */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 103 | static uintptr_t readEncodedPointer(const uint8_t** data, uint8_t encoding) |
| 104 | { |
| 105 | const uint8_t* p = *data; |
| 106 | uintptr_t result = 0; |
| 107 | |
| 108 | if ( encoding == DW_EH_PE_omit ) |
| 109 | return 0; |
| 110 | |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 111 | /* first get value */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 112 | switch (encoding & 0x0F) { |
| 113 | case DW_EH_PE_absptr: |
| 114 | result = *((uintptr_t*)p); |
| 115 | p += sizeof(uintptr_t); |
| 116 | break; |
| 117 | case DW_EH_PE_uleb128: |
| 118 | result = readULEB128(&p); |
| 119 | break; |
| 120 | case DW_EH_PE_udata2: |
| 121 | result = *((uint16_t*)p); |
| 122 | p += sizeof(uint16_t); |
| 123 | break; |
| 124 | case DW_EH_PE_udata4: |
| 125 | result = *((uint32_t*)p); |
| 126 | p += sizeof(uint32_t); |
| 127 | break; |
| 128 | case DW_EH_PE_udata8: |
| 129 | result = *((uint64_t*)p); |
| 130 | p += sizeof(uint64_t); |
| 131 | break; |
| 132 | case DW_EH_PE_sdata2: |
| 133 | result = *((int16_t*)p); |
| 134 | p += sizeof(int16_t); |
| 135 | break; |
| 136 | case DW_EH_PE_sdata4: |
| 137 | result = *((int32_t*)p); |
| 138 | p += sizeof(int32_t); |
| 139 | break; |
| 140 | case DW_EH_PE_sdata8: |
| 141 | result = *((int64_t*)p); |
| 142 | p += sizeof(int64_t); |
| 143 | break; |
| 144 | case DW_EH_PE_sleb128: |
| 145 | default: |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 146 | /* not supported */ |
Daniel Dunbar | f287008 | 2010-03-31 17:00:45 +0000 | [diff] [blame] | 147 | compilerrt_abort(); |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 151 | /* then add relative offset */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 152 | switch ( encoding & 0x70 ) { |
| 153 | case DW_EH_PE_absptr: |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 154 | /* do nothing */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 155 | break; |
| 156 | case DW_EH_PE_pcrel: |
| 157 | result += (uintptr_t)(*data); |
| 158 | break; |
| 159 | case DW_EH_PE_textrel: |
| 160 | case DW_EH_PE_datarel: |
| 161 | case DW_EH_PE_funcrel: |
| 162 | case DW_EH_PE_aligned: |
| 163 | default: |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 164 | /* not supported */ |
Daniel Dunbar | f287008 | 2010-03-31 17:00:45 +0000 | [diff] [blame] | 165 | compilerrt_abort(); |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 169 | /* then apply indirection */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 170 | if (encoding & DW_EH_PE_indirect) { |
| 171 | result = *((uintptr_t*)result); |
| 172 | } |
| 173 | |
| 174 | *data = p; |
| 175 | return result; |
| 176 | } |
| 177 | |
| 178 | |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 179 | /* |
| 180 | * The C compiler makes references to __gcc_personality_v0 in |
| 181 | * the dwarf unwind information for translation units that use |
| 182 | * __attribute__((cleanup(xx))) on local variables. |
| 183 | * This personality routine is called by the system unwinder |
| 184 | * on each frame as the stack is unwound during a C++ exception |
| 185 | * throw through a C function compiled with -fexceptions. |
| 186 | */ |
Nick Kledzik | c6cacf9 | 2010-05-07 23:44:20 +0000 | [diff] [blame^] | 187 | #if __arm__ |
| 188 | // the setjump-longjump based exceptions personality routine has a different name |
| 189 | _Unwind_Reason_Code __gcc_personality_sj0(int version, _Unwind_Action actions, |
| 190 | uint64_t exceptionClass, struct _Unwind_Exception* exceptionObject, |
| 191 | _Unwind_Context_t context) |
| 192 | #else |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 193 | _Unwind_Reason_Code __gcc_personality_v0(int version, _Unwind_Action actions, |
| 194 | uint64_t exceptionClass, struct _Unwind_Exception* exceptionObject, |
| 195 | _Unwind_Context_t context) |
Nick Kledzik | c6cacf9 | 2010-05-07 23:44:20 +0000 | [diff] [blame^] | 196 | #endif |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 197 | { |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 198 | /* Since C does not have catch clauses, there is nothing to do during */ |
| 199 | /* phase 1 (the search phase). */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 200 | if ( actions & _UA_SEARCH_PHASE ) |
| 201 | return _URC_CONTINUE_UNWIND; |
| 202 | |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 203 | /* There is nothing to do if there is no LSDA for this frame. */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 204 | const uint8_t* lsda = _Unwind_GetLanguageSpecificData(context); |
| 205 | if ( lsda == NULL ) |
| 206 | return _URC_CONTINUE_UNWIND; |
| 207 | |
| 208 | uintptr_t pc = _Unwind_GetIP(context)-1; |
| 209 | uintptr_t funcStart = _Unwind_GetRegionStart(context); |
| 210 | uintptr_t pcOffset = pc - funcStart; |
| 211 | |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 212 | /* Parse LSDA header. */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 213 | uint8_t lpStartEncoding = *lsda++; |
| 214 | if (lpStartEncoding != DW_EH_PE_omit) { |
| 215 | readEncodedPointer(&lsda, lpStartEncoding); |
| 216 | } |
| 217 | uint8_t ttypeEncoding = *lsda++; |
| 218 | if (ttypeEncoding != DW_EH_PE_omit) { |
| 219 | readULEB128(&lsda); |
| 220 | } |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 221 | /* Walk call-site table looking for range that includes current PC. */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 222 | uint8_t callSiteEncoding = *lsda++; |
| 223 | uint32_t callSiteTableLength = readULEB128(&lsda); |
| 224 | const uint8_t* callSiteTableStart = lsda; |
| 225 | const uint8_t* callSiteTableEnd = callSiteTableStart + callSiteTableLength; |
| 226 | const uint8_t* p=callSiteTableStart; |
| 227 | while (p < callSiteTableEnd) { |
| 228 | uintptr_t start = readEncodedPointer(&p, callSiteEncoding); |
| 229 | uintptr_t length = readEncodedPointer(&p, callSiteEncoding); |
| 230 | uintptr_t landingPad = readEncodedPointer(&p, callSiteEncoding); |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 231 | readULEB128(&p); /* action value not used for C code */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 232 | if ( landingPad == 0 ) |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 233 | continue; /* no landing pad for this entry */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 234 | if ( (start <= pcOffset) && (pcOffset < (start+length)) ) { |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 235 | /* Found landing pad for the PC. |
| 236 | * Set Instruction Pointer to so we re-enter function |
| 237 | * at landing pad. The landing pad is created by the compiler |
| 238 | * to take two parameters in registers. |
| 239 | */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 240 | _Unwind_SetGR(context, __builtin_eh_return_data_regno(0), |
| 241 | (uintptr_t)exceptionObject); |
| 242 | _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), 0); |
| 243 | _Unwind_SetIP(context, funcStart+landingPad); |
| 244 | return _URC_INSTALL_CONTEXT; |
| 245 | } |
| 246 | } |
| 247 | |
Edward O'Callaghan | 4856eef | 2009-08-05 04:02:56 +0000 | [diff] [blame] | 248 | /* No landing pad found, continue unwinding. */ |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 249 | return _URC_CONTINUE_UNWIND; |
| 250 | } |
| 251 | |