blob: fc303a9b7bef11dc0b2ebb3a3b4d2e9076de55df [file] [log] [blame]
Saleem Abdulrasool675df582015-04-24 19:39:17 +00001//===--------------------- UnwindLevel1-gcc-ext.c -------------------------===//
2//
Chandler Carruthb81a9422019-01-19 10:56:40 +00003// 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
Saleem Abdulrasool675df582015-04-24 19:39:17 +00006//
7//
8// Implements gcc extensions to the C++ ABI Exception Handling Level 1.
9//
10//===----------------------------------------------------------------------===//
11
12#include <inttypes.h>
13#include <stdbool.h>
14#include <stdint.h>
15#include <stdio.h>
16#include <stdlib.h>
Logan Chien9f323e02015-05-29 15:33:38 +000017#include <string.h>
Saleem Abdulrasool675df582015-04-24 19:39:17 +000018
19#include "config.h"
20#include "libunwind_ext.h"
21#include "libunwind.h"
22#include "Unwind-EHABI.h"
23#include "unwind.h"
24
Ranjeet Singh7d674132017-03-31 15:28:06 +000025#if defined(_LIBUNWIND_BUILD_ZERO_COST_APIS)
Saleem Abdulrasool675df582015-04-24 19:39:17 +000026
Charles Davis1f89d782018-08-30 21:29:00 +000027#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
28#define private_1 private_[0]
29#endif
30
Saleem Abdulrasool675df582015-04-24 19:39:17 +000031/// Called by __cxa_rethrow().
32_LIBUNWIND_EXPORT _Unwind_Reason_Code
33_Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object) {
Ranjeet Singh7d674132017-03-31 15:28:06 +000034#if defined(_LIBUNWIND_ARM_EHABI)
Ed Masteadc29082016-08-30 15:38:10 +000035 _LIBUNWIND_TRACE_API("_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%ld",
Saleem Abdulrasool675df582015-04-24 19:39:17 +000036 (void *)exception_object,
37 (long)exception_object->unwinder_cache.reserved1);
38#else
Charles Davis96902e12018-08-08 04:21:24 +000039 _LIBUNWIND_TRACE_API("_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%" PRIdPTR,
Saleem Abdulrasool675df582015-04-24 19:39:17 +000040 (void *)exception_object,
Charles Davis96902e12018-08-08 04:21:24 +000041 (intptr_t)exception_object->private_1);
Saleem Abdulrasool675df582015-04-24 19:39:17 +000042#endif
43
Ranjeet Singh7d674132017-03-31 15:28:06 +000044#if defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool675df582015-04-24 19:39:17 +000045 // _Unwind_RaiseException on EHABI will always set the reserved1 field to 0,
46 // which is in the same position as private_1 below.
47 return _Unwind_RaiseException(exception_object);
48#else
49 // If this is non-forced and a stopping place was found, then this is a
50 // re-throw.
51 // Call _Unwind_RaiseException() as if this was a new exception
52 if (exception_object->private_1 == 0) {
53 return _Unwind_RaiseException(exception_object);
54 // Will return if there is no catch clause, so that __cxa_rethrow can call
55 // std::terminate().
56 }
57
58 // Call through to _Unwind_Resume() which distiguishes between forced and
59 // regular exceptions.
60 _Unwind_Resume(exception_object);
61 _LIBUNWIND_ABORT("_Unwind_Resume_or_Rethrow() called _Unwind_RaiseException()"
62 " which unexpectedly returned");
63#endif
64}
65
66
67/// Called by personality handler during phase 2 to get base address for data
68/// relative encodings.
69_LIBUNWIND_EXPORT uintptr_t
70_Unwind_GetDataRelBase(struct _Unwind_Context *context) {
71 (void)context;
Ed Masteadc29082016-08-30 15:38:10 +000072 _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)", (void *)context);
Saleem Abdulrasool675df582015-04-24 19:39:17 +000073 _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented");
74}
75
76
77/// Called by personality handler during phase 2 to get base address for text
78/// relative encodings.
79_LIBUNWIND_EXPORT uintptr_t
80_Unwind_GetTextRelBase(struct _Unwind_Context *context) {
81 (void)context;
Ed Masteadc29082016-08-30 15:38:10 +000082 _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)", (void *)context);
Saleem Abdulrasool675df582015-04-24 19:39:17 +000083 _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented");
84}
85
86
87/// Scans unwind information to find the function that contains the
88/// specified code address "pc".
89_LIBUNWIND_EXPORT void *_Unwind_FindEnclosingFunction(void *pc) {
Ed Masteadc29082016-08-30 15:38:10 +000090 _LIBUNWIND_TRACE_API("_Unwind_FindEnclosingFunction(pc=%p)", pc);
Saleem Abdulrasool675df582015-04-24 19:39:17 +000091 // This is slow, but works.
92 // We create an unwind cursor then alter the IP to be pc
93 unw_cursor_t cursor;
94 unw_context_t uc;
95 unw_proc_info_t info;
96 unw_getcontext(&uc);
97 unw_init_local(&cursor, &uc);
Charles Davis96902e12018-08-08 04:21:24 +000098 unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t) pc);
Saleem Abdulrasool675df582015-04-24 19:39:17 +000099 if (unw_get_proc_info(&cursor, &info) == UNW_ESUCCESS)
Charles Davis96902e12018-08-08 04:21:24 +0000100 return (void *)(intptr_t) info.start_ip;
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000101 else
102 return NULL;
103}
104
105/// Walk every frame and call trace function at each one. If trace function
106/// returns anything other than _URC_NO_REASON, then walk is terminated.
107_LIBUNWIND_EXPORT _Unwind_Reason_Code
108_Unwind_Backtrace(_Unwind_Trace_Fn callback, void *ref) {
109 unw_cursor_t cursor;
110 unw_context_t uc;
111 unw_getcontext(&uc);
112 unw_init_local(&cursor, &uc);
113
Ed Masteadc29082016-08-30 15:38:10 +0000114 _LIBUNWIND_TRACE_API("_Unwind_Backtrace(callback=%p)",
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000115 (void *)(uintptr_t)callback);
116
Ranjeet Singh7d674132017-03-31 15:28:06 +0000117#if defined(_LIBUNWIND_ARM_EHABI)
Logan Chien9f323e02015-05-29 15:33:38 +0000118 // Create a mock exception object for force unwinding.
119 _Unwind_Exception ex;
120 memset(&ex, '\0', sizeof(ex));
121 ex.exception_class = 0x434C4E47554E5700; // CLNGUNW\0
122#endif
123
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000124 // walk each frame
125 while (true) {
126 _Unwind_Reason_Code result;
127
Ranjeet Singh7d674132017-03-31 15:28:06 +0000128#if !defined(_LIBUNWIND_ARM_EHABI)
Ed Maste17473fd2016-08-30 13:08:21 +0000129 // ask libunwind to get next frame (skip over first frame which is
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000130 // _Unwind_Backtrace())
131 if (unw_step(&cursor) <= 0) {
132 _LIBUNWIND_TRACE_UNWINDING(" _backtrace: ended because cursor reached "
Ed Masteadc29082016-08-30 15:38:10 +0000133 "bottom of stack, returning %d",
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000134 _URC_END_OF_STACK);
135 return _URC_END_OF_STACK;
136 }
Logan Chien9f323e02015-05-29 15:33:38 +0000137#else
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000138 // Get the information for this frame.
139 unw_proc_info_t frameInfo;
140 if (unw_get_proc_info(&cursor, &frameInfo) != UNW_ESUCCESS) {
141 return _URC_END_OF_STACK;
142 }
143
Logan Chien9f323e02015-05-29 15:33:38 +0000144 // Update the pr_cache in the mock exception object.
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000145 const uint32_t* unwindInfo = (uint32_t *) frameInfo.unwind_info;
Logan Chien9f323e02015-05-29 15:33:38 +0000146 ex.pr_cache.fnstart = frameInfo.start_ip;
147 ex.pr_cache.ehtp = (_Unwind_EHT_Header *) unwindInfo;
148 ex.pr_cache.additional= frameInfo.flags;
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000149
Logan Chien9f323e02015-05-29 15:33:38 +0000150 struct _Unwind_Context *context = (struct _Unwind_Context *)&cursor;
151 // Get and call the personality function to unwind the frame.
152 __personality_routine handler = (__personality_routine) frameInfo.handler;
153 if (handler == NULL) {
154 return _URC_END_OF_STACK;
155 }
156 if (handler(_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND, &ex, context) !=
157 _URC_CONTINUE_UNWIND) {
158 return _URC_END_OF_STACK;
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000159 }
Ranjeet Singh7d674132017-03-31 15:28:06 +0000160#endif // defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000161
162 // debugging
163 if (_LIBUNWIND_TRACING_UNWINDING) {
164 char functionName[512];
165 unw_proc_info_t frame;
166 unw_word_t offset;
167 unw_get_proc_name(&cursor, functionName, 512, &offset);
168 unw_get_proc_info(&cursor, &frame);
169 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo85f4cfd2017-10-30 19:06:34 +0000170 " _backtrace: start_ip=0x%" PRIxPTR ", func=%s, lsda=0x%" PRIxPTR ", context=%p",
171 frame.start_ip, functionName, frame.lsda,
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000172 (void *)&cursor);
173 }
174
175 // call trace function with this frame
176 result = (*callback)((struct _Unwind_Context *)(&cursor), ref);
177 if (result != _URC_NO_REASON) {
178 _LIBUNWIND_TRACE_UNWINDING(
Ed Masteadc29082016-08-30 15:38:10 +0000179 " _backtrace: ended because callback returned %d", result);
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000180 return result;
181 }
182 }
183}
184
185
Ed Maste63469ff2016-07-19 17:15:50 +0000186/// Find DWARF unwind info for an address 'pc' in some function.
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000187_LIBUNWIND_EXPORT const void *_Unwind_Find_FDE(const void *pc,
188 struct dwarf_eh_bases *bases) {
189 // This is slow, but works.
190 // We create an unwind cursor then alter the IP to be pc
191 unw_cursor_t cursor;
192 unw_context_t uc;
193 unw_proc_info_t info;
194 unw_getcontext(&uc);
195 unw_init_local(&cursor, &uc);
Charles Davis96902e12018-08-08 04:21:24 +0000196 unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t) pc);
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000197 unw_get_proc_info(&cursor, &info);
198 bases->tbase = (uintptr_t)info.extra;
199 bases->dbase = 0; // dbase not used on Mac OS X
200 bases->func = (uintptr_t)info.start_ip;
Ed Masteadc29082016-08-30 15:38:10 +0000201 _LIBUNWIND_TRACE_API("_Unwind_Find_FDE(pc=%p) => %p", pc,
Charles Davis96902e12018-08-08 04:21:24 +0000202 (void *)(intptr_t) info.unwind_info);
203 return (void *)(intptr_t) info.unwind_info;
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000204}
205
206/// Returns the CFA (call frame area, or stack pointer at start of function)
207/// for the current context.
208_LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) {
209 unw_cursor_t *cursor = (unw_cursor_t *)context;
210 unw_word_t result;
211 unw_get_reg(cursor, UNW_REG_SP, &result);
Martin Storsjo85f4cfd2017-10-30 19:06:34 +0000212 _LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p) => 0x%" PRIxPTR,
213 (void *)context, result);
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000214 return (uintptr_t)result;
215}
216
217
218/// Called by personality handler during phase 2 to get instruction pointer.
219/// ipBefore is a boolean that says if IP is already adjusted to be the call
220/// site address. Normally IP is the return address.
221_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
222 int *ipBefore) {
Ed Masteadc29082016-08-30 15:38:10 +0000223 _LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p)", (void *)context);
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000224 *ipBefore = 0;
225 return _Unwind_GetIP(context);
226}
227
Ranjeet Singh7d674132017-03-31 15:28:06 +0000228#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000229
230/// Called by programs with dynamic code generators that want
231/// to register a dynamically generated FDE.
232/// This function has existed on Mac OS X since 10.4, but
233/// was broken until 10.6.
234_LIBUNWIND_EXPORT void __register_frame(const void *fde) {
Ed Masteadc29082016-08-30 15:38:10 +0000235 _LIBUNWIND_TRACE_API("__register_frame(%p)", fde);
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000236 _unw_add_dynamic_fde((unw_word_t)(uintptr_t) fde);
237}
238
239
240/// Called by programs with dynamic code generators that want
241/// to unregister a dynamically generated FDE.
242/// This function has existed on Mac OS X since 10.4, but
243/// was broken until 10.6.
244_LIBUNWIND_EXPORT void __deregister_frame(const void *fde) {
Ed Masteadc29082016-08-30 15:38:10 +0000245 _LIBUNWIND_TRACE_API("__deregister_frame(%p)", fde);
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000246 _unw_remove_dynamic_fde((unw_word_t)(uintptr_t) fde);
247}
248
249
250// The following register/deregister functions are gcc extensions.
251// They have existed on Mac OS X, but have never worked because Mac OS X
252// before 10.6 used keymgr to track known FDEs, but these functions
253// never got updated to use keymgr.
254// For now, we implement these as do-nothing functions to keep any existing
255// applications working. We also add the not in 10.6 symbol so that nwe
256// application won't be able to use them.
257
Ranjeet Singh7d674132017-03-31 15:28:06 +0000258#if defined(_LIBUNWIND_SUPPORT_FRAME_APIS)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000259_LIBUNWIND_EXPORT void __register_frame_info_bases(const void *fde, void *ob,
260 void *tb, void *db) {
261 (void)fde;
262 (void)ob;
263 (void)tb;
264 (void)db;
Ed Masteadc29082016-08-30 15:38:10 +0000265 _LIBUNWIND_TRACE_API("__register_frame_info_bases(%p,%p, %p, %p)",
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000266 fde, ob, tb, db);
267 // do nothing, this function never worked in Mac OS X
268}
269
270_LIBUNWIND_EXPORT void __register_frame_info(const void *fde, void *ob) {
271 (void)fde;
272 (void)ob;
Ed Masteadc29082016-08-30 15:38:10 +0000273 _LIBUNWIND_TRACE_API("__register_frame_info(%p, %p)", fde, ob);
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000274 // do nothing, this function never worked in Mac OS X
275}
276
277_LIBUNWIND_EXPORT void __register_frame_info_table_bases(const void *fde,
278 void *ob, void *tb,
279 void *db) {
280 (void)fde;
281 (void)ob;
282 (void)tb;
283 (void)db;
284 _LIBUNWIND_TRACE_API("__register_frame_info_table_bases"
Ed Masteadc29082016-08-30 15:38:10 +0000285 "(%p,%p, %p, %p)", fde, ob, tb, db);
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000286 // do nothing, this function never worked in Mac OS X
287}
288
289_LIBUNWIND_EXPORT void __register_frame_info_table(const void *fde, void *ob) {
290 (void)fde;
291 (void)ob;
Ed Masteadc29082016-08-30 15:38:10 +0000292 _LIBUNWIND_TRACE_API("__register_frame_info_table(%p, %p)", fde, ob);
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000293 // do nothing, this function never worked in Mac OS X
294}
295
296_LIBUNWIND_EXPORT void __register_frame_table(const void *fde) {
297 (void)fde;
Ed Masteadc29082016-08-30 15:38:10 +0000298 _LIBUNWIND_TRACE_API("__register_frame_table(%p)", fde);
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000299 // do nothing, this function never worked in Mac OS X
300}
301
302_LIBUNWIND_EXPORT void *__deregister_frame_info(const void *fde) {
303 (void)fde;
Ed Masteadc29082016-08-30 15:38:10 +0000304 _LIBUNWIND_TRACE_API("__deregister_frame_info(%p)", fde);
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000305 // do nothing, this function never worked in Mac OS X
306 return NULL;
307}
308
309_LIBUNWIND_EXPORT void *__deregister_frame_info_bases(const void *fde) {
310 (void)fde;
Ed Masteadc29082016-08-30 15:38:10 +0000311 _LIBUNWIND_TRACE_API("__deregister_frame_info_bases(%p)", fde);
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000312 // do nothing, this function never worked in Mac OS X
313 return NULL;
314}
Ranjeet Singh7d674132017-03-31 15:28:06 +0000315#endif // defined(_LIBUNWIND_SUPPORT_FRAME_APIS)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000316
Ranjeet Singh7d674132017-03-31 15:28:06 +0000317#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool675df582015-04-24 19:39:17 +0000318
Ranjeet Singh7d674132017-03-31 15:28:06 +0000319#endif // defined(_LIBUNWIND_BUILD_ZERO_COST_APIS)