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 * ===----------------------------------------------------------------------===
17 * Pointer encodings documented at:
18 * http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
21 #define DW_EH_PE_omit 0xff /* no data follows */
23 #define DW_EH_PE_absptr 0x00
24 #define DW_EH_PE_uleb128 0x01
25 #define DW_EH_PE_udata2 0x02
26 #define DW_EH_PE_udata4 0x03
27 #define DW_EH_PE_udata8 0x04
28 #define DW_EH_PE_sleb128 0x09
29 #define DW_EH_PE_sdata2 0x0A
30 #define DW_EH_PE_sdata4 0x0B
31 #define DW_EH_PE_sdata8 0x0C
33 #define DW_EH_PE_pcrel 0x10
34 #define DW_EH_PE_textrel 0x20
35 #define DW_EH_PE_datarel 0x30
36 #define DW_EH_PE_funcrel 0x40
37 #define DW_EH_PE_aligned 0x50
38 #define DW_EH_PE_indirect 0x80 /* gcc extension */
42 /* read a uleb128 encoded value and advance pointer */
43 static uintptr_t readULEB128(const uint8_t** data)
48 const uint8_t* p = *data;
51 result |= (byte & 0x7f) << shift;
53 } while (byte & 0x80);
58 /* read a pointer encoded value and advance pointer */
59 static uintptr_t readEncodedPointer(const uint8_t** data, uint8_t encoding)
61 const uint8_t* p = *data;
64 if ( encoding == DW_EH_PE_omit )
68 switch (encoding & 0x0F) {
70 result = *((const uintptr_t*)p);
71 p += sizeof(uintptr_t);
73 case DW_EH_PE_uleb128:
74 result = readULEB128(&p);
77 result = *((const uint16_t*)p);
78 p += sizeof(uint16_t);
81 result = *((const uint32_t*)p);
82 p += sizeof(uint32_t);
85 result = *((const uint64_t*)p);
86 p += sizeof(uint64_t);
89 result = *((const int16_t*)p);
93 result = *((const int32_t*)p);
97 result = *((const int64_t*)p);
100 case DW_EH_PE_sleb128:
107 /* then add relative offset */
108 switch ( encoding & 0x70 ) {
109 case DW_EH_PE_absptr:
113 result += (uintptr_t)(*data);
115 case DW_EH_PE_textrel:
116 case DW_EH_PE_datarel:
117 case DW_EH_PE_funcrel:
118 case DW_EH_PE_aligned:
125 /* then apply indirection */
126 if (encoding & DW_EH_PE_indirect) {
127 result = *((const uintptr_t*)result);
134 #if defined(__arm__) && !defined(__USING_SJLJ_EXCEPTIONS__) && \
135 !defined(__ARM_DWARF_EH__)
136 #define USING_ARM_EHABI 1
137 _Unwind_Reason_Code __gnu_unwind_frame(struct _Unwind_Exception *,
138 struct _Unwind_Context *);
141 static inline _Unwind_Reason_Code
142 continueUnwind(struct _Unwind_Exception *exceptionObject,
143 struct _Unwind_Context *context) {
146 * On ARM EHABI the personality routine is responsible for actually
147 * unwinding a single stack frame before returning (ARM EHABI Sec. 6.1).
149 if (__gnu_unwind_frame(exceptionObject, context) != _URC_OK)
152 return _URC_CONTINUE_UNWIND;
156 * The C compiler makes references to __gcc_personality_v0 in
157 * the dwarf unwind information for translation units that use
158 * __attribute__((cleanup(xx))) on local variables.
159 * This personality routine is called by the system unwinder
160 * on each frame as the stack is unwound during a C++ exception
161 * throw through a C function compiled with -fexceptions.
163 #if __USING_SJLJ_EXCEPTIONS__
164 /* the setjump-longjump based exceptions personality routine has a
166 COMPILER_RT_ABI _Unwind_Reason_Code
167 __gcc_personality_sj0(int version, _Unwind_Action actions,
168 uint64_t exceptionClass, struct _Unwind_Exception* exceptionObject,
169 struct _Unwind_Context *context)
170 #elif USING_ARM_EHABI
171 /* The ARM EHABI personality routine has a different signature. */
172 COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
173 _Unwind_State state, struct _Unwind_Exception *exceptionObject,
174 struct _Unwind_Context *context)
176 COMPILER_RT_ABI _Unwind_Reason_Code
177 __gcc_personality_v0(int version, _Unwind_Action actions,
178 uint64_t exceptionClass, struct _Unwind_Exception* exceptionObject,
179 struct _Unwind_Context *context)
182 /* Since C does not have catch clauses, there is nothing to do during */
183 /* phase 1 (the search phase). */
185 /* After resuming from a cleanup we should also continue on to the next
186 * frame straight away. */
187 if ((state & _US_ACTION_MASK) != _US_UNWIND_FRAME_STARTING)
189 if ( actions & _UA_SEARCH_PHASE )
191 return continueUnwind(exceptionObject, context);
193 /* There is nothing to do if there is no LSDA for this frame. */
194 const uint8_t* lsda = (uint8_t*)_Unwind_GetLanguageSpecificData(context);
195 if ( lsda == (uint8_t*) 0 )
196 return continueUnwind(exceptionObject, context);
198 uintptr_t pc = _Unwind_GetIP(context)-1;
199 uintptr_t funcStart = _Unwind_GetRegionStart(context);
200 uintptr_t pcOffset = pc - funcStart;
202 /* Parse LSDA header. */
203 uint8_t lpStartEncoding = *lsda++;
204 if (lpStartEncoding != DW_EH_PE_omit) {
205 readEncodedPointer(&lsda, lpStartEncoding);
207 uint8_t ttypeEncoding = *lsda++;
208 if (ttypeEncoding != DW_EH_PE_omit) {
211 /* Walk call-site table looking for range that includes current PC. */
212 uint8_t callSiteEncoding = *lsda++;
213 uint32_t callSiteTableLength = readULEB128(&lsda);
214 const uint8_t* callSiteTableStart = lsda;
215 const uint8_t* callSiteTableEnd = callSiteTableStart + callSiteTableLength;
216 const uint8_t* p=callSiteTableStart;
217 while (p < callSiteTableEnd) {
218 uintptr_t start = readEncodedPointer(&p, callSiteEncoding);
219 uintptr_t length = readEncodedPointer(&p, callSiteEncoding);
220 uintptr_t landingPad = readEncodedPointer(&p, callSiteEncoding);
221 readULEB128(&p); /* action value not used for C code */
222 if ( landingPad == 0 )
223 continue; /* no landing pad for this entry */
224 if ( (start <= pcOffset) && (pcOffset < (start+length)) ) {
225 /* Found landing pad for the PC.
226 * Set Instruction Pointer to so we re-enter function
227 * at landing pad. The landing pad is created by the compiler
228 * to take two parameters in registers.
230 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
231 (uintptr_t)exceptionObject);
232 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), 0);
233 _Unwind_SetIP(context, (funcStart + landingPad));
234 return _URC_INSTALL_CONTEXT;
238 /* No landing pad found, continue unwinding. */
239 return continueUnwind(exceptionObject, context);