blob: 951d5d219a3eb3d9925ede1660d40ab9fb28fa9d [file] [log] [blame]
Louis Dionne7f068e52021-11-17 16:25:01 -05001//===----------------------------------------------------------------------===//
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002//
Chandler Carruth61860a52019-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 Abdulrasool17552662015-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 Chiena54f0962015-05-29 15:33:38 +000017#include <string.h>
Saleem Abdulrasool17552662015-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 Singh421231a2017-03-31 15:28:06 +000025#if defined(_LIBUNWIND_BUILD_ZERO_COST_APIS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000026
Charles Davisfa2e6202018-08-30 21:29:00 +000027#if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
Daniel Kiss6bf52ee2021-08-11 10:11:30 +020028#define PRIVATE_1 private_[0]
29#elif defined(_LIBUNWIND_ARM_EHABI)
30#define PRIVATE_1 unwinder_cache.reserved1
31#else
32#define PRIVATE_1 private_1
Charles Davisfa2e6202018-08-30 21:29:00 +000033#endif
34
Saleem Abdulrasool17552662015-04-24 19:39:17 +000035/// Called by __cxa_rethrow().
36_LIBUNWIND_EXPORT _Unwind_Reason_Code
37_Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object) {
Daniel Kiss6bf52ee2021-08-11 10:11:30 +020038 _LIBUNWIND_TRACE_API(
39 "_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%" PRIdPTR,
40 (void *)exception_object, (intptr_t)exception_object->PRIVATE_1);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000041
Saleem Abdulrasool17552662015-04-24 19:39:17 +000042 // If this is non-forced and a stopping place was found, then this is a
43 // re-throw.
44 // Call _Unwind_RaiseException() as if this was a new exception
Daniel Kiss6bf52ee2021-08-11 10:11:30 +020045 if (exception_object->PRIVATE_1 == 0) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +000046 return _Unwind_RaiseException(exception_object);
47 // Will return if there is no catch clause, so that __cxa_rethrow can call
48 // std::terminate().
49 }
50
51 // Call through to _Unwind_Resume() which distiguishes between forced and
52 // regular exceptions.
53 _Unwind_Resume(exception_object);
54 _LIBUNWIND_ABORT("_Unwind_Resume_or_Rethrow() called _Unwind_RaiseException()"
55 " which unexpectedly returned");
Saleem Abdulrasool17552662015-04-24 19:39:17 +000056}
57
Saleem Abdulrasool17552662015-04-24 19:39:17 +000058/// Called by personality handler during phase 2 to get base address for data
59/// relative encodings.
60_LIBUNWIND_EXPORT uintptr_t
61_Unwind_GetDataRelBase(struct _Unwind_Context *context) {
62 (void)context;
Ed Maste41bc5a72016-08-30 15:38:10 +000063 _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)", (void *)context);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000064 _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented");
65}
66
67
68/// Called by personality handler during phase 2 to get base address for text
69/// relative encodings.
70_LIBUNWIND_EXPORT uintptr_t
71_Unwind_GetTextRelBase(struct _Unwind_Context *context) {
72 (void)context;
Ed Maste41bc5a72016-08-30 15:38:10 +000073 _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)", (void *)context);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000074 _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented");
75}
76
77
78/// Scans unwind information to find the function that contains the
79/// specified code address "pc".
80_LIBUNWIND_EXPORT void *_Unwind_FindEnclosingFunction(void *pc) {
Ed Maste41bc5a72016-08-30 15:38:10 +000081 _LIBUNWIND_TRACE_API("_Unwind_FindEnclosingFunction(pc=%p)", pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000082 // This is slow, but works.
83 // We create an unwind cursor then alter the IP to be pc
84 unw_cursor_t cursor;
85 unw_context_t uc;
86 unw_proc_info_t info;
Petr Hosek9bbfad52019-04-03 21:50:03 +000087 __unw_getcontext(&uc);
88 __unw_init_local(&cursor, &uc);
89 __unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t)pc);
90 if (__unw_get_proc_info(&cursor, &info) == UNW_ESUCCESS)
Charles Davis5aede5d2018-08-08 04:21:24 +000091 return (void *)(intptr_t) info.start_ip;
Saleem Abdulrasool17552662015-04-24 19:39:17 +000092 else
93 return NULL;
94}
95
96/// Walk every frame and call trace function at each one. If trace function
97/// returns anything other than _URC_NO_REASON, then walk is terminated.
98_LIBUNWIND_EXPORT _Unwind_Reason_Code
99_Unwind_Backtrace(_Unwind_Trace_Fn callback, void *ref) {
100 unw_cursor_t cursor;
101 unw_context_t uc;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000102 __unw_getcontext(&uc);
103 __unw_init_local(&cursor, &uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000104
Ed Maste41bc5a72016-08-30 15:38:10 +0000105 _LIBUNWIND_TRACE_API("_Unwind_Backtrace(callback=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000106 (void *)(uintptr_t)callback);
107
Ranjeet Singh421231a2017-03-31 15:28:06 +0000108#if defined(_LIBUNWIND_ARM_EHABI)
Logan Chiena54f0962015-05-29 15:33:38 +0000109 // Create a mock exception object for force unwinding.
110 _Unwind_Exception ex;
111 memset(&ex, '\0', sizeof(ex));
Hans Wennborgedf77b22021-09-02 18:43:26 +0200112 strcpy((char *)&ex.exception_class, "CLNGUNW");
Logan Chiena54f0962015-05-29 15:33:38 +0000113#endif
114
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000115 // walk each frame
116 while (true) {
117 _Unwind_Reason_Code result;
118
Ranjeet Singh421231a2017-03-31 15:28:06 +0000119#if !defined(_LIBUNWIND_ARM_EHABI)
Ed Maste6f723382016-08-30 13:08:21 +0000120 // ask libunwind to get next frame (skip over first frame which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000121 // _Unwind_Backtrace())
Petr Hosek9bbfad52019-04-03 21:50:03 +0000122 if (__unw_step(&cursor) <= 0) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000123 _LIBUNWIND_TRACE_UNWINDING(" _backtrace: ended because cursor reached "
Ed Maste41bc5a72016-08-30 15:38:10 +0000124 "bottom of stack, returning %d",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000125 _URC_END_OF_STACK);
126 return _URC_END_OF_STACK;
127 }
Logan Chiena54f0962015-05-29 15:33:38 +0000128#else
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000129 // Get the information for this frame.
130 unw_proc_info_t frameInfo;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000131 if (__unw_get_proc_info(&cursor, &frameInfo) != UNW_ESUCCESS) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000132 return _URC_END_OF_STACK;
133 }
134
Logan Chiena54f0962015-05-29 15:33:38 +0000135 // Update the pr_cache in the mock exception object.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000136 const uint32_t* unwindInfo = (uint32_t *) frameInfo.unwind_info;
Logan Chiena54f0962015-05-29 15:33:38 +0000137 ex.pr_cache.fnstart = frameInfo.start_ip;
138 ex.pr_cache.ehtp = (_Unwind_EHT_Header *) unwindInfo;
139 ex.pr_cache.additional= frameInfo.flags;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000140
Logan Chiena54f0962015-05-29 15:33:38 +0000141 struct _Unwind_Context *context = (struct _Unwind_Context *)&cursor;
142 // Get and call the personality function to unwind the frame.
Saleem Abdulrasool214b72e2020-02-10 08:52:31 -0800143 _Unwind_Personality_Fn handler = (_Unwind_Personality_Fn)frameInfo.handler;
Logan Chiena54f0962015-05-29 15:33:38 +0000144 if (handler == NULL) {
145 return _URC_END_OF_STACK;
146 }
147 if (handler(_US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND, &ex, context) !=
148 _URC_CONTINUE_UNWIND) {
149 return _URC_END_OF_STACK;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000150 }
Ranjeet Singh421231a2017-03-31 15:28:06 +0000151#endif // defined(_LIBUNWIND_ARM_EHABI)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000152
153 // debugging
154 if (_LIBUNWIND_TRACING_UNWINDING) {
155 char functionName[512];
156 unw_proc_info_t frame;
157 unw_word_t offset;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000158 __unw_get_proc_name(&cursor, functionName, 512, &offset);
159 __unw_get_proc_info(&cursor, &frame);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000160 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +0000161 " _backtrace: start_ip=0x%" PRIxPTR ", func=%s, lsda=0x%" PRIxPTR ", context=%p",
162 frame.start_ip, functionName, frame.lsda,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000163 (void *)&cursor);
164 }
165
166 // call trace function with this frame
167 result = (*callback)((struct _Unwind_Context *)(&cursor), ref);
168 if (result != _URC_NO_REASON) {
169 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000170 " _backtrace: ended because callback returned %d", result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000171 return result;
172 }
173 }
174}
175
176
Ed Maste4c43c3d2016-07-19 17:15:50 +0000177/// Find DWARF unwind info for an address 'pc' in some function.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000178_LIBUNWIND_EXPORT const void *_Unwind_Find_FDE(const void *pc,
179 struct dwarf_eh_bases *bases) {
180 // This is slow, but works.
181 // We create an unwind cursor then alter the IP to be pc
182 unw_cursor_t cursor;
183 unw_context_t uc;
184 unw_proc_info_t info;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000185 __unw_getcontext(&uc);
186 __unw_init_local(&cursor, &uc);
187 __unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(intptr_t)pc);
188 __unw_get_proc_info(&cursor, &info);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000189 bases->tbase = (uintptr_t)info.extra;
190 bases->dbase = 0; // dbase not used on Mac OS X
191 bases->func = (uintptr_t)info.start_ip;
Ed Maste41bc5a72016-08-30 15:38:10 +0000192 _LIBUNWIND_TRACE_API("_Unwind_Find_FDE(pc=%p) => %p", pc,
Charles Davis5aede5d2018-08-08 04:21:24 +0000193 (void *)(intptr_t) info.unwind_info);
194 return (void *)(intptr_t) info.unwind_info;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000195}
196
197/// Returns the CFA (call frame area, or stack pointer at start of function)
198/// for the current context.
199_LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) {
200 unw_cursor_t *cursor = (unw_cursor_t *)context;
201 unw_word_t result;
Petr Hosek9bbfad52019-04-03 21:50:03 +0000202 __unw_get_reg(cursor, UNW_REG_SP, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000203 _LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p) => 0x%" PRIxPTR,
204 (void *)context, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000205 return (uintptr_t)result;
206}
207
208
209/// Called by personality handler during phase 2 to get instruction pointer.
210/// ipBefore is a boolean that says if IP is already adjusted to be the call
211/// site address. Normally IP is the return address.
212_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
213 int *ipBefore) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000214 _LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p)", (void *)context);
Sterling Augustineb6a66392019-10-31 12:45:20 -0700215 int isSignalFrame = __unw_is_signal_frame((unw_cursor_t *)context);
216 // Negative means some kind of error (probably UNW_ENOINFO), but we have no
217 // good way to report that, and this maintains backward compatibility with the
218 // implementation that hard-coded zero in every case, even signal frames.
219 if (isSignalFrame <= 0)
220 *ipBefore = 0;
221 else
222 *ipBefore = 1;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000223 return _Unwind_GetIP(context);
224}
225
Ranjeet Singh421231a2017-03-31 15:28:06 +0000226#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000227
228/// Called by programs with dynamic code generators that want
229/// to register a dynamically generated FDE.
230/// This function has existed on Mac OS X since 10.4, but
231/// was broken until 10.6.
232_LIBUNWIND_EXPORT void __register_frame(const void *fde) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000233 _LIBUNWIND_TRACE_API("__register_frame(%p)", fde);
Petr Hosek9bbfad52019-04-03 21:50:03 +0000234 __unw_add_dynamic_fde((unw_word_t)(uintptr_t)fde);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000235}
236
237
238/// Called by programs with dynamic code generators that want
239/// to unregister a dynamically generated FDE.
240/// This function has existed on Mac OS X since 10.4, but
241/// was broken until 10.6.
242_LIBUNWIND_EXPORT void __deregister_frame(const void *fde) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000243 _LIBUNWIND_TRACE_API("__deregister_frame(%p)", fde);
Petr Hosek9bbfad52019-04-03 21:50:03 +0000244 __unw_remove_dynamic_fde((unw_word_t)(uintptr_t)fde);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000245}
246
247
248// The following register/deregister functions are gcc extensions.
249// They have existed on Mac OS X, but have never worked because Mac OS X
250// before 10.6 used keymgr to track known FDEs, but these functions
251// never got updated to use keymgr.
252// For now, we implement these as do-nothing functions to keep any existing
253// applications working. We also add the not in 10.6 symbol so that nwe
254// application won't be able to use them.
255
Ranjeet Singh421231a2017-03-31 15:28:06 +0000256#if defined(_LIBUNWIND_SUPPORT_FRAME_APIS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000257_LIBUNWIND_EXPORT void __register_frame_info_bases(const void *fde, void *ob,
258 void *tb, void *db) {
259 (void)fde;
260 (void)ob;
261 (void)tb;
262 (void)db;
Ed Maste41bc5a72016-08-30 15:38:10 +0000263 _LIBUNWIND_TRACE_API("__register_frame_info_bases(%p,%p, %p, %p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000264 fde, ob, tb, db);
265 // do nothing, this function never worked in Mac OS X
266}
267
268_LIBUNWIND_EXPORT void __register_frame_info(const void *fde, void *ob) {
269 (void)fde;
270 (void)ob;
Ed Maste41bc5a72016-08-30 15:38:10 +0000271 _LIBUNWIND_TRACE_API("__register_frame_info(%p, %p)", fde, ob);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000272 // do nothing, this function never worked in Mac OS X
273}
274
275_LIBUNWIND_EXPORT void __register_frame_info_table_bases(const void *fde,
276 void *ob, void *tb,
277 void *db) {
278 (void)fde;
279 (void)ob;
280 (void)tb;
281 (void)db;
282 _LIBUNWIND_TRACE_API("__register_frame_info_table_bases"
Ed Maste41bc5a72016-08-30 15:38:10 +0000283 "(%p,%p, %p, %p)", fde, ob, tb, db);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000284 // do nothing, this function never worked in Mac OS X
285}
286
287_LIBUNWIND_EXPORT void __register_frame_info_table(const void *fde, void *ob) {
288 (void)fde;
289 (void)ob;
Ed Maste41bc5a72016-08-30 15:38:10 +0000290 _LIBUNWIND_TRACE_API("__register_frame_info_table(%p, %p)", fde, ob);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000291 // do nothing, this function never worked in Mac OS X
292}
293
294_LIBUNWIND_EXPORT void __register_frame_table(const void *fde) {
295 (void)fde;
Ed Maste41bc5a72016-08-30 15:38:10 +0000296 _LIBUNWIND_TRACE_API("__register_frame_table(%p)", fde);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000297 // do nothing, this function never worked in Mac OS X
298}
299
300_LIBUNWIND_EXPORT void *__deregister_frame_info(const void *fde) {
301 (void)fde;
Ed Maste41bc5a72016-08-30 15:38:10 +0000302 _LIBUNWIND_TRACE_API("__deregister_frame_info(%p)", fde);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000303 // do nothing, this function never worked in Mac OS X
304 return NULL;
305}
306
307_LIBUNWIND_EXPORT void *__deregister_frame_info_bases(const void *fde) {
308 (void)fde;
Ed Maste41bc5a72016-08-30 15:38:10 +0000309 _LIBUNWIND_TRACE_API("__deregister_frame_info_bases(%p)", fde);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000310 // do nothing, this function never worked in Mac OS X
311 return NULL;
312}
Ranjeet Singh421231a2017-03-31 15:28:06 +0000313#endif // defined(_LIBUNWIND_SUPPORT_FRAME_APIS)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000314
Ranjeet Singh421231a2017-03-31 15:28:06 +0000315#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000316
Ranjeet Singh421231a2017-03-31 15:28:06 +0000317#endif // defined(_LIBUNWIND_BUILD_ZERO_COST_APIS)