1 /* ===-- gcc_personality_v0.c - Implement __gcc_personality_v0 -------------===
3 * The LLVM Compiler Infrastructure
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
8 * ===----------------------------------------------------------------------===
19 * _Unwind_* stuff based on C++ ABI public documentation
20 * http://refspecs.freestandards.org/abi-eh-1.21.html
25 _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
26 _URC_FATAL_PHASE2_ERROR = 2,
27 _URC_FATAL_PHASE1_ERROR = 3,
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;
37 _UA_CLEANUP_PHASE = 2,
38 _UA_HANDLER_FRAME = 4,
43 typedef struct _Unwind_Context* _Unwind_Context_t;
45 struct _Unwind_Exception {
46 uint64_t exception_class;
47 void (*exception_cleanup)(_Unwind_Reason_Code reason,
48 struct _Unwind_Exception* exc);
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);
61 * Pointer encodings documented at:
62 * http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
65 #define DW_EH_PE_omit 0xff /* no data follows */
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
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
82 #define DW_EH_PE_indirect 0x80 /* gcc extension */
86 /* read a uleb128 encoded value and advance pointer */
87 static uintptr_t readULEB128(const uint8_t** data)
92 const uint8_t* p = *data;
95 result |= (byte & 0x7f) << shift;
97 } while (byte & 0x80);
102 /* read a pointer encoded value and advance pointer */
103 static uintptr_t readEncodedPointer(const uint8_t** data, uint8_t encoding)
105 const uint8_t* p = *data;
106 uintptr_t result = 0;
108 if ( encoding == DW_EH_PE_omit )
111 /* first get value */
112 switch (encoding & 0x0F) {
113 case DW_EH_PE_absptr:
114 result = *((uintptr_t*)p);
115 p += sizeof(uintptr_t);
117 case DW_EH_PE_uleb128:
118 result = readULEB128(&p);
120 case DW_EH_PE_udata2:
121 result = *((uint16_t*)p);
122 p += sizeof(uint16_t);
124 case DW_EH_PE_udata4:
125 result = *((uint32_t*)p);
126 p += sizeof(uint32_t);
128 case DW_EH_PE_udata8:
129 result = *((uint64_t*)p);
130 p += sizeof(uint64_t);
132 case DW_EH_PE_sdata2:
133 result = *((int16_t*)p);
134 p += sizeof(int16_t);
136 case DW_EH_PE_sdata4:
137 result = *((int32_t*)p);
138 p += sizeof(int32_t);
140 case DW_EH_PE_sdata8:
141 result = *((int64_t*)p);
142 p += sizeof(int64_t);
144 case DW_EH_PE_sleb128:
151 /* then add relative offset */
152 switch ( encoding & 0x70 ) {
153 case DW_EH_PE_absptr:
157 result += (uintptr_t)(*data);
159 case DW_EH_PE_textrel:
160 case DW_EH_PE_datarel:
161 case DW_EH_PE_funcrel:
162 case DW_EH_PE_aligned:
169 /* then apply indirection */
170 if (encoding & DW_EH_PE_indirect) {
171 result = *((uintptr_t*)result);
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.
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)
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)
198 /* Since C does not have catch clauses, there is nothing to do during */
199 /* phase 1 (the search phase). */
200 if ( actions & _UA_SEARCH_PHASE )
201 return _URC_CONTINUE_UNWIND;
203 /* There is nothing to do if there is no LSDA for this frame. */
204 const uint8_t* lsda = _Unwind_GetLanguageSpecificData(context);
206 return _URC_CONTINUE_UNWIND;
208 uintptr_t pc = _Unwind_GetIP(context)-1;
209 uintptr_t funcStart = _Unwind_GetRegionStart(context);
210 uintptr_t pcOffset = pc - funcStart;
212 /* Parse LSDA header. */
213 uint8_t lpStartEncoding = *lsda++;
214 if (lpStartEncoding != DW_EH_PE_omit) {
215 readEncodedPointer(&lsda, lpStartEncoding);
217 uint8_t ttypeEncoding = *lsda++;
218 if (ttypeEncoding != DW_EH_PE_omit) {
221 /* Walk call-site table looking for range that includes current PC. */
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);
231 readULEB128(&p); /* action value not used for C code */
232 if ( landingPad == 0 )
233 continue; /* no landing pad for this entry */
234 if ( (start <= pcOffset) && (pcOffset < (start+length)) ) {
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.
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;
248 /* No landing pad found, continue unwinding. */
249 return _URC_CONTINUE_UNWIND;