1 /* ===-- gcc_personality_v0.c - Implement __gcc_personality_v0 -------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
7 *
8 * ===----------------------------------------------------------------------===
9 *
10 */
11
12 #include "int_lib.h"
13
14 #include <unwind.h>
15
16 /*
17 * Pointer encodings documented at:
18 * http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
19 */
20
21 #define DW_EH_PE_omit 0xff /* no data follows */
22
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
32
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 */
39
40
41
42 /* read a uleb128 encoded value and advance pointer */
readULEB128(const uint8_t ** data)43 static uintptr_t readULEB128(const uint8_t** data)
44 {
45 uintptr_t result = 0;
46 uintptr_t shift = 0;
47 unsigned char byte;
48 const uint8_t* p = *data;
49 do {
50 byte = *p++;
51 result |= (byte & 0x7f) << shift;
52 shift += 7;
53 } while (byte & 0x80);
54 *data = p;
55 return result;
56 }
57
58 /* read a pointer encoded value and advance pointer */
readEncodedPointer(const uint8_t ** data,uint8_t encoding)59 static uintptr_t readEncodedPointer(const uint8_t** data, uint8_t encoding)
60 {
61 const uint8_t* p = *data;
62 uintptr_t result = 0;
63
64 if ( encoding == DW_EH_PE_omit )
65 return 0;
66
67 /* first get value */
68 switch (encoding & 0x0F) {
69 case DW_EH_PE_absptr:
70 result = *((const uintptr_t*)p);
71 p += sizeof(uintptr_t);
72 break;
73 case DW_EH_PE_uleb128:
74 result = readULEB128(&p);
75 break;
76 case DW_EH_PE_udata2:
77 result = *((const uint16_t*)p);
78 p += sizeof(uint16_t);
79 break;
80 case DW_EH_PE_udata4:
81 result = *((const uint32_t*)p);
82 p += sizeof(uint32_t);
83 break;
84 case DW_EH_PE_udata8:
85 result = *((const uint64_t*)p);
86 p += sizeof(uint64_t);
87 break;
88 case DW_EH_PE_sdata2:
89 result = *((const int16_t*)p);
90 p += sizeof(int16_t);
91 break;
92 case DW_EH_PE_sdata4:
93 result = *((const int32_t*)p);
94 p += sizeof(int32_t);
95 break;
96 case DW_EH_PE_sdata8:
97 result = *((const int64_t*)p);
98 p += sizeof(int64_t);
99 break;
100 case DW_EH_PE_sleb128:
101 default:
102 /* not supported */
103 compilerrt_abort();
104 break;
105 }
106
107 /* then add relative offset */
108 switch ( encoding & 0x70 ) {
109 case DW_EH_PE_absptr:
110 /* do nothing */
111 break;
112 case DW_EH_PE_pcrel:
113 result += (uintptr_t)(*data);
114 break;
115 case DW_EH_PE_textrel:
116 case DW_EH_PE_datarel:
117 case DW_EH_PE_funcrel:
118 case DW_EH_PE_aligned:
119 default:
120 /* not supported */
121 compilerrt_abort();
122 break;
123 }
124
125 /* then apply indirection */
126 if (encoding & DW_EH_PE_indirect) {
127 result = *((const uintptr_t*)result);
128 }
129
130 *data = p;
131 return result;
132 }
133
134
135 /*
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.
142 */
143 #if __USING_SJLJ_EXCEPTIONS__
144 /* the setjump-longjump based exceptions personality routine has a
145 * different name */
146 COMPILER_RT_ABI _Unwind_Reason_Code
__gcc_personality_sj0(int version,_Unwind_Action actions,uint64_t exceptionClass,struct _Unwind_Exception * exceptionObject,struct _Unwind_Context * context)147 __gcc_personality_sj0(int version, _Unwind_Action actions,
148 uint64_t exceptionClass, struct _Unwind_Exception* exceptionObject,
149 struct _Unwind_Context *context)
150 #else
151 COMPILER_RT_ABI _Unwind_Reason_Code
152 __gcc_personality_v0(int version, _Unwind_Action actions,
153 uint64_t exceptionClass, struct _Unwind_Exception* exceptionObject,
154 struct _Unwind_Context *context)
155 #endif
156 {
157 /* Since C does not have catch clauses, there is nothing to do during */
158 /* phase 1 (the search phase). */
159 if ( actions & _UA_SEARCH_PHASE )
160 return _URC_CONTINUE_UNWIND;
161
162 /* There is nothing to do if there is no LSDA for this frame. */
163 const uint8_t* lsda = (uint8_t*)_Unwind_GetLanguageSpecificData(context);
164 if ( lsda == (uint8_t*) 0 )
165 return _URC_CONTINUE_UNWIND;
166
167 uintptr_t pc = _Unwind_GetIP(context)-1;
168 uintptr_t funcStart = _Unwind_GetRegionStart(context);
169 uintptr_t pcOffset = pc - funcStart;
170
171 /* Parse LSDA header. */
172 uint8_t lpStartEncoding = *lsda++;
173 if (lpStartEncoding != DW_EH_PE_omit) {
174 readEncodedPointer(&lsda, lpStartEncoding);
175 }
176 uint8_t ttypeEncoding = *lsda++;
177 if (ttypeEncoding != DW_EH_PE_omit) {
178 readULEB128(&lsda);
179 }
180 /* Walk call-site table looking for range that includes current PC. */
181 uint8_t callSiteEncoding = *lsda++;
182 uint32_t callSiteTableLength = readULEB128(&lsda);
183 const uint8_t* callSiteTableStart = lsda;
184 const uint8_t* callSiteTableEnd = callSiteTableStart + callSiteTableLength;
185 const uint8_t* p=callSiteTableStart;
186 while (p < callSiteTableEnd) {
187 uintptr_t start = readEncodedPointer(&p, callSiteEncoding);
188 uintptr_t length = readEncodedPointer(&p, callSiteEncoding);
189 uintptr_t landingPad = readEncodedPointer(&p, callSiteEncoding);
190 readULEB128(&p); /* action value not used for C code */
191 if ( landingPad == 0 )
192 continue; /* no landing pad for this entry */
193 if ( (start <= pcOffset) && (pcOffset < (start+length)) ) {
194 /* Found landing pad for the PC.
195 * Set Instruction Pointer to so we re-enter function
196 * at landing pad. The landing pad is created by the compiler
197 * to take two parameters in registers.
198 */
199 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
200 (uintptr_t)exceptionObject);
201 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), 0);
202 _Unwind_SetIP(context, (funcStart + landingPad));
203 return _URC_INSTALL_CONTEXT;
204 }
205 }
206
207 /* No landing pad found, continue unwinding. */
208 return _URC_CONTINUE_UNWIND;
209 }
210
211