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);
136 * The C compiler makes references to __gcc_personality_v0 in
137 * the dwarf unwind information for translation units that use
138 * __attribute__((cleanup(xx))) on local variables.
139 * This personality routine is called by the system unwinder
140 * on each frame as the stack is unwound during a C++ exception
141 * throw through a C function compiled with -fexceptions.
143 #if __USING_SJLJ_EXCEPTIONS__
144 // the setjump-longjump based exceptions personality routine has a different name
145 COMPILER_RT_ABI _Unwind_Reason_Code
146 __gcc_personality_sj0(int version, _Unwind_Action actions,
147 uint64_t exceptionClass, struct _Unwind_Exception* exceptionObject,
148 struct _Unwind_Context *context)
150 COMPILER_RT_ABI _Unwind_Reason_Code
151 __gcc_personality_v0(int version, _Unwind_Action actions,
152 uint64_t exceptionClass, struct _Unwind_Exception* exceptionObject,
153 struct _Unwind_Context *context)
156 /* Since C does not have catch clauses, there is nothing to do during */
157 /* phase 1 (the search phase). */
158 if ( actions & _UA_SEARCH_PHASE )
159 return _URC_CONTINUE_UNWIND;
161 /* There is nothing to do if there is no LSDA for this frame. */
162 const uint8_t* lsda = (uint8_t*)_Unwind_GetLanguageSpecificData(context);
163 if ( lsda == (uint8_t*) 0 )
164 return _URC_CONTINUE_UNWIND;
166 uintptr_t pc = _Unwind_GetIP(context)-1;
167 uintptr_t funcStart = _Unwind_GetRegionStart(context);
168 uintptr_t pcOffset = pc - funcStart;
170 /* Parse LSDA header. */
171 uint8_t lpStartEncoding = *lsda++;
172 if (lpStartEncoding != DW_EH_PE_omit) {
173 readEncodedPointer(&lsda, lpStartEncoding);
175 uint8_t ttypeEncoding = *lsda++;
176 if (ttypeEncoding != DW_EH_PE_omit) {
179 /* Walk call-site table looking for range that includes current PC. */
180 uint8_t callSiteEncoding = *lsda++;
181 uint32_t callSiteTableLength = readULEB128(&lsda);
182 const uint8_t* callSiteTableStart = lsda;
183 const uint8_t* callSiteTableEnd = callSiteTableStart + callSiteTableLength;
184 const uint8_t* p=callSiteTableStart;
185 while (p < callSiteTableEnd) {
186 uintptr_t start = readEncodedPointer(&p, callSiteEncoding);
187 uintptr_t length = readEncodedPointer(&p, callSiteEncoding);
188 uintptr_t landingPad = readEncodedPointer(&p, callSiteEncoding);
189 readULEB128(&p); /* action value not used for C code */
190 if ( landingPad == 0 )
191 continue; /* no landing pad for this entry */
192 if ( (start <= pcOffset) && (pcOffset < (start+length)) ) {
193 /* Found landing pad for the PC.
194 * Set Instruction Pointer to so we re-enter function
195 * at landing pad. The landing pad is created by the compiler
196 * to take two parameters in registers.
198 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
199 (uintptr_t)exceptionObject);
200 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), 0);
201 _Unwind_SetIP(context, funcStart+landingPad);
202 return _URC_INSTALL_CONTEXT;
206 /* No landing pad found, continue unwinding. */
207 return _URC_CONTINUE_UNWIND;