authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-22 20:45:14-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-24 01:18:48-07:00
log3002856a7ffcd68d91ac6200479f55c15f44b84d
treedc396b655e38199f6d47f96d32e9b1ea5409c0bf
parent8219d92987c7d9641d6b24c4d5be29e3a80fd6b9

libunwind: add __gcc_personality_v0 symbol


2 files changed, 245 insertions(+), 0 deletions(-)

lib/libunwind/src/gcc_personality_v0.c created+244
......@@ -0,0 +1,244 @@
1//===-- gcc_personality_v0.c - Implement __gcc_personality_v0 -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#if __ARM_EABI__
10#ifdef COMPILER_RT_ARMHF_TARGET
11#define COMPILER_RT_ABI
12#else
13#define COMPILER_RT_ABI __attribute__((__pcs__("aapcs")))
14#endif
15#else
16#define COMPILER_RT_ABI
17#endif
18
19#define compilerrt_abort() __builtin_unreachable()
20
21#include <unwind.h>
22#if defined(__arm__) && !defined(__ARM_DWARF_EH__) && \
23 !defined(__USING_SJLJ_EXCEPTIONS__)
24// When building with older compilers (e.g. clang <3.9), it is possible that we
25// have a version of unwind.h which does not provide the EHABI declarations
26// which are quired for the C personality to conform to the specification. In
27// order to provide forward compatibility for such compilers, we re-declare the
28// necessary interfaces in the helper to permit a standalone compilation of the
29// builtins (which contains the C unwinding personality for historical reasons).
30#include "unwind-ehabi-helpers.h"
31#endif
32
33// Pointer encodings documented at:
34// http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
35
36#define DW_EH_PE_omit 0xff // no data follows
37
38#define DW_EH_PE_absptr 0x00
39#define DW_EH_PE_uleb128 0x01
40#define DW_EH_PE_udata2 0x02
41#define DW_EH_PE_udata4 0x03
42#define DW_EH_PE_udata8 0x04
43#define DW_EH_PE_sleb128 0x09
44#define DW_EH_PE_sdata2 0x0A
45#define DW_EH_PE_sdata4 0x0B
46#define DW_EH_PE_sdata8 0x0C
47
48#define DW_EH_PE_pcrel 0x10
49#define DW_EH_PE_textrel 0x20
50#define DW_EH_PE_datarel 0x30
51#define DW_EH_PE_funcrel 0x40
52#define DW_EH_PE_aligned 0x50
53#define DW_EH_PE_indirect 0x80 // gcc extension
54
55// read a uleb128 encoded value and advance pointer
56static uintptr_t readULEB128(const uint8_t **data) {
57 uintptr_t result = 0;
58 uintptr_t shift = 0;
59 unsigned char byte;
60 const uint8_t *p = *data;
61 do {
62 byte = *p++;
63 result |= (byte & 0x7f) << shift;
64 shift += 7;
65 } while (byte & 0x80);
66 *data = p;
67 return result;
68}
69
70// read a pointer encoded value and advance pointer
71static uintptr_t readEncodedPointer(const uint8_t **data, uint8_t encoding) {
72 const uint8_t *p = *data;
73 uintptr_t result = 0;
74
75 if (encoding == DW_EH_PE_omit)
76 return 0;
77
78 // first get value
79 switch (encoding & 0x0F) {
80 case DW_EH_PE_absptr:
81 result = *((const uintptr_t *)p);
82 p += sizeof(uintptr_t);
83 break;
84 case DW_EH_PE_uleb128:
85 result = readULEB128(&p);
86 break;
87 case DW_EH_PE_udata2:
88 result = *((const uint16_t *)p);
89 p += sizeof(uint16_t);
90 break;
91 case DW_EH_PE_udata4:
92 result = *((const uint32_t *)p);
93 p += sizeof(uint32_t);
94 break;
95 case DW_EH_PE_udata8:
96 result = *((const uint64_t *)p);
97 p += sizeof(uint64_t);
98 break;
99 case DW_EH_PE_sdata2:
100 result = *((const int16_t *)p);
101 p += sizeof(int16_t);
102 break;
103 case DW_EH_PE_sdata4:
104 result = *((const int32_t *)p);
105 p += sizeof(int32_t);
106 break;
107 case DW_EH_PE_sdata8:
108 result = *((const int64_t *)p);
109 p += sizeof(int64_t);
110 break;
111 case DW_EH_PE_sleb128:
112 default:
113 // not supported
114 compilerrt_abort();
115 break;
116 }
117
118 // then add relative offset
119 switch (encoding & 0x70) {
120 case DW_EH_PE_absptr:
121 // do nothing
122 break;
123 case DW_EH_PE_pcrel:
124 result += (uintptr_t)(*data);
125 break;
126 case DW_EH_PE_textrel:
127 case DW_EH_PE_datarel:
128 case DW_EH_PE_funcrel:
129 case DW_EH_PE_aligned:
130 default:
131 // not supported
132 compilerrt_abort();
133 break;
134 }
135
136 // then apply indirection
137 if (encoding & DW_EH_PE_indirect) {
138 result = *((const uintptr_t *)result);
139 }
140
141 *data = p;
142 return result;
143}
144
145#if defined(__arm__) && !defined(__USING_SJLJ_EXCEPTIONS__) && \
146 !defined(__ARM_DWARF_EH__)
147#define USING_ARM_EHABI 1
148_Unwind_Reason_Code __gnu_unwind_frame(struct _Unwind_Exception *,
149 struct _Unwind_Context *);
150#endif
151
152static inline _Unwind_Reason_Code
153continueUnwind(struct _Unwind_Exception *exceptionObject,
154 struct _Unwind_Context *context) {
155#if USING_ARM_EHABI
156 // On ARM EHABI the personality routine is responsible for actually
157 // unwinding a single stack frame before returning (ARM EHABI Sec. 6.1).
158 if (__gnu_unwind_frame(exceptionObject, context) != _URC_OK)
159 return _URC_FAILURE;
160#endif
161 return _URC_CONTINUE_UNWIND;
162}
163
164// The C compiler makes references to __gcc_personality_v0 in
165// the dwarf unwind information for translation units that use
166// __attribute__((cleanup(xx))) on local variables.
167// This personality routine is called by the system unwinder
168// on each frame as the stack is unwound during a C++ exception
169// throw through a C function compiled with -fexceptions.
170#if __USING_SJLJ_EXCEPTIONS__
171// the setjump-longjump based exceptions personality routine has a
172// different name
173COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_sj0(
174 int version, _Unwind_Action actions, uint64_t exceptionClass,
175 struct _Unwind_Exception *exceptionObject, struct _Unwind_Context *context)
176#elif USING_ARM_EHABI
177// The ARM EHABI personality routine has a different signature.
178COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
179 _Unwind_State state, struct _Unwind_Exception *exceptionObject,
180 struct _Unwind_Context *context)
181#else
182COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
183 int version, _Unwind_Action actions, uint64_t exceptionClass,
184 struct _Unwind_Exception *exceptionObject, struct _Unwind_Context *context)
185#endif
186{
187 // Since C does not have catch clauses, there is nothing to do during
188 // phase 1 (the search phase).
189#if USING_ARM_EHABI
190 // After resuming from a cleanup we should also continue on to the next
191 // frame straight away.
192 if ((state & _US_ACTION_MASK) != _US_UNWIND_FRAME_STARTING)
193#else
194 if (actions & _UA_SEARCH_PHASE)
195#endif
196 return continueUnwind(exceptionObject, context);
197
198 // There is nothing to do if there is no LSDA for this frame.
199 const uint8_t *lsda = (uint8_t *)_Unwind_GetLanguageSpecificData(context);
200 if (lsda == (uint8_t *)0)
201 return continueUnwind(exceptionObject, context);
202
203 uintptr_t pc = (uintptr_t)_Unwind_GetIP(context) - 1;
204 uintptr_t funcStart = (uintptr_t)_Unwind_GetRegionStart(context);
205 uintptr_t pcOffset = pc - funcStart;
206
207 // Parse LSDA header.
208 uint8_t lpStartEncoding = *lsda++;
209 if (lpStartEncoding != DW_EH_PE_omit) {
210 readEncodedPointer(&lsda, lpStartEncoding);
211 }
212 uint8_t ttypeEncoding = *lsda++;
213 if (ttypeEncoding != DW_EH_PE_omit) {
214 readULEB128(&lsda);
215 }
216 // Walk call-site table looking for range that includes current PC.
217 uint8_t callSiteEncoding = *lsda++;
218 uint32_t callSiteTableLength = readULEB128(&lsda);
219 const uint8_t *callSiteTableStart = lsda;
220 const uint8_t *callSiteTableEnd = callSiteTableStart + callSiteTableLength;
221 const uint8_t *p = callSiteTableStart;
222 while (p < callSiteTableEnd) {
223 uintptr_t start = readEncodedPointer(&p, callSiteEncoding);
224 uintptr_t length = readEncodedPointer(&p, callSiteEncoding);
225 uintptr_t landingPad = readEncodedPointer(&p, callSiteEncoding);
226 readULEB128(&p); // action value not used for C code
227 if (landingPad == 0)
228 continue; // no landing pad for this entry
229 if ((start <= pcOffset) && (pcOffset < (start + length))) {
230 // Found landing pad for the PC.
231 // Set Instruction Pointer to so we re-enter function
232 // at landing pad. The landing pad is created by the compiler
233 // to take two parameters in registers.
234 _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
242 // No landing pad found, continue unwinding.
243 return continueUnwind(exceptionObject, context);
244}
src/libunwind.zig+1
......@@ -42,6 +42,7 @@ pub fn buildStaticLib(comp: *Compilation) !void {
4242 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "Unwind-sjlj.c",
4343 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "UnwindRegistersRestore.S",
4444 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "UnwindRegistersSave.S",
45 "libunwind" ++ path.sep_str ++ "src" ++ path.sep_str ++ "gcc_personality_v0.c",
4546 };
4647 var c_source_files: [unwind_src_list.len]Compilation.CSourceFile = undefined;
4748 for (unwind_src_list) |unwind_src, i| {