blob: 16534b92fd3cfa9d3ebee1a37664c01a23e23b32 [file] [log] [blame]
Edward O'Callaghan4856eef2009-08-05 04:02:56 +00001/* ===-- 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 Dunbarfd089992009-06-26 16:47:03 +000011
12#include <stdint.h>
13#include <stdio.h>
14#include <stdlib.h>
15
Nick Kledzik5b0a0622010-04-15 20:37:56 +000016#include "int_lib.h"
17
Edward O'Callaghan4856eef2009-08-05 04:02:56 +000018/*
19 * _Unwind_* stuff based on C++ ABI public documentation
20 * http://refspecs.freestandards.org/abi-eh-1.21.html
21 */
22
Daniel Dunbarfd089992009-06-26 16:47:03 +000023typedef 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
35typedef 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
43typedef struct _Unwind_Context* _Unwind_Context_t;
44
45struct _Unwind_Exception {
46 uint64_t exception_class;
Nick Kledzikac02b5a2010-01-20 06:13:20 +000047 void (*exception_cleanup)(_Unwind_Reason_Code reason,
48 struct _Unwind_Exception* exc);
Daniel Dunbarfd089992009-06-26 16:47:03 +000049 uintptr_t private_1;
50 uintptr_t private_2;
51};
52
53extern const uint8_t* _Unwind_GetLanguageSpecificData(_Unwind_Context_t c);
54extern void _Unwind_SetGR(_Unwind_Context_t c, int i, uintptr_t n);
55extern void _Unwind_SetIP(_Unwind_Context_t, uintptr_t new_value);
56extern uintptr_t _Unwind_GetIP(_Unwind_Context_t context);
57extern uintptr_t _Unwind_GetRegionStart(_Unwind_Context_t context);
58
59
Edward O'Callaghan4856eef2009-08-05 04:02:56 +000060/*
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 Dunbarfd089992009-06-26 16:47:03 +000066
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'Callaghan4856eef2009-08-05 04:02:56 +000082#define DW_EH_PE_indirect 0x80 /* gcc extension */
Daniel Dunbarfd089992009-06-26 16:47:03 +000083
84
85
Edward O'Callaghan4856eef2009-08-05 04:02:56 +000086/* read a uleb128 encoded value and advance pointer */
Daniel Dunbarfd089992009-06-26 16:47:03 +000087static 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'Callaghan4856eef2009-08-05 04:02:56 +0000102/* read a pointer encoded value and advance pointer */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000103static 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'Callaghan4856eef2009-08-05 04:02:56 +0000111 /* first get value */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000112 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'Callaghan4856eef2009-08-05 04:02:56 +0000146 /* not supported */
Daniel Dunbarf2870082010-03-31 17:00:45 +0000147 compilerrt_abort();
Daniel Dunbarfd089992009-06-26 16:47:03 +0000148 break;
149 }
150
Edward O'Callaghan4856eef2009-08-05 04:02:56 +0000151 /* then add relative offset */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000152 switch ( encoding & 0x70 ) {
153 case DW_EH_PE_absptr:
Edward O'Callaghan4856eef2009-08-05 04:02:56 +0000154 /* do nothing */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000155 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'Callaghan4856eef2009-08-05 04:02:56 +0000164 /* not supported */
Daniel Dunbarf2870082010-03-31 17:00:45 +0000165 compilerrt_abort();
Daniel Dunbarfd089992009-06-26 16:47:03 +0000166 break;
167 }
168
Edward O'Callaghan4856eef2009-08-05 04:02:56 +0000169 /* then apply indirection */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000170 if (encoding & DW_EH_PE_indirect) {
171 result = *((uintptr_t*)result);
172 }
173
174 *data = p;
175 return result;
176}
177
178
Edward O'Callaghan4856eef2009-08-05 04:02:56 +0000179/*
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 */
187
Daniel Dunbarfd089992009-06-26 16:47:03 +0000188_Unwind_Reason_Code __gcc_personality_v0(int version, _Unwind_Action actions,
189 uint64_t exceptionClass, struct _Unwind_Exception* exceptionObject,
190 _Unwind_Context_t context)
191{
Edward O'Callaghan4856eef2009-08-05 04:02:56 +0000192 /* Since C does not have catch clauses, there is nothing to do during */
193 /* phase 1 (the search phase). */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000194 if ( actions & _UA_SEARCH_PHASE )
195 return _URC_CONTINUE_UNWIND;
196
Edward O'Callaghan4856eef2009-08-05 04:02:56 +0000197 /* There is nothing to do if there is no LSDA for this frame. */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000198 const uint8_t* lsda = _Unwind_GetLanguageSpecificData(context);
199 if ( lsda == NULL )
200 return _URC_CONTINUE_UNWIND;
201
202 uintptr_t pc = _Unwind_GetIP(context)-1;
203 uintptr_t funcStart = _Unwind_GetRegionStart(context);
204 uintptr_t pcOffset = pc - funcStart;
205
Edward O'Callaghan4856eef2009-08-05 04:02:56 +0000206 /* Parse LSDA header. */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000207 uint8_t lpStartEncoding = *lsda++;
208 if (lpStartEncoding != DW_EH_PE_omit) {
209 readEncodedPointer(&lsda, lpStartEncoding);
210 }
211 uint8_t ttypeEncoding = *lsda++;
212 if (ttypeEncoding != DW_EH_PE_omit) {
213 readULEB128(&lsda);
214 }
Edward O'Callaghan4856eef2009-08-05 04:02:56 +0000215 /* Walk call-site table looking for range that includes current PC. */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000216 uint8_t callSiteEncoding = *lsda++;
217 uint32_t callSiteTableLength = readULEB128(&lsda);
218 const uint8_t* callSiteTableStart = lsda;
219 const uint8_t* callSiteTableEnd = callSiteTableStart + callSiteTableLength;
220 const uint8_t* p=callSiteTableStart;
221 while (p < callSiteTableEnd) {
222 uintptr_t start = readEncodedPointer(&p, callSiteEncoding);
223 uintptr_t length = readEncodedPointer(&p, callSiteEncoding);
224 uintptr_t landingPad = readEncodedPointer(&p, callSiteEncoding);
Edward O'Callaghan4856eef2009-08-05 04:02:56 +0000225 readULEB128(&p); /* action value not used for C code */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000226 if ( landingPad == 0 )
Edward O'Callaghan4856eef2009-08-05 04:02:56 +0000227 continue; /* no landing pad for this entry */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000228 if ( (start <= pcOffset) && (pcOffset < (start+length)) ) {
Edward O'Callaghan4856eef2009-08-05 04:02:56 +0000229 /* Found landing pad for the PC.
230 * Set Instruction Pointer to so we re-enter function
231 * at landing pad. The landing pad is created by the compiler
232 * to take two parameters in registers.
233 */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000234 _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
235 (uintptr_t)exceptionObject);
236 _Unwind_SetGR(context, __builtin_eh_return_data_regno(1), 0);
237 _Unwind_SetIP(context, funcStart+landingPad);
238 return _URC_INSTALL_CONTEXT;
239 }
240 }
241
Edward O'Callaghan4856eef2009-08-05 04:02:56 +0000242 /* No landing pad found, continue unwinding. */
Daniel Dunbarfd089992009-06-26 16:47:03 +0000243 return _URC_CONTINUE_UNWIND;
244}
245