blob: 90a55fd29db1faa1ddfdb4dd3485b699ee2ca684 [file] [log] [blame]
Louis Dionneeb8650a2021-11-17 21:25:011//===----------------------------------------------------------------------===//
Saleem Abdulrasoolb1b19112015-04-24 19:39:172//
Chandler Carruth57b08b02019-01-19 10:56:403// 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 Abdulrasoolb1b19112015-04-24 19:39:176//
7//
8// Implements setjump-longjump based C++ exceptions
9//
10//===----------------------------------------------------------------------===//
11
12#include <unwind.h>
13
Martin Storsjo94adf432019-01-22 20:50:3914#include <inttypes.h>
Saleem Abdulrasoolb1b19112015-04-24 19:39:1715#include <stdint.h>
16#include <stdbool.h>
17#include <stdlib.h>
18
19#include "config.h"
Saleem Abdulrasoolb1b19112015-04-24 19:39:1720
Saleem Abdulrasool9ebbda82017-10-01 20:06:4821/// With SJLJ based exceptions, any function that has a catch clause or needs to
22/// do any clean up when an exception propagates through it, needs to call
23/// \c _Unwind_SjLj_Register at the start of the function and
24/// \c _Unwind_SjLj_Unregister at the end. The register function is called with
25/// the address of a block of memory in the function's stack frame. The runtime
26/// keeps a linked list (stack) of these blocks - one per thread. The calling
27/// function also sets the personality and lsda fields of the block.
Saleem Abdulrasoolb1b19112015-04-24 19:39:1728
Ranjeet Singh58080112017-03-31 15:28:0629#if defined(_LIBUNWIND_BUILD_SJLJ_APIS)
Saleem Abdulrasoolb1b19112015-04-24 19:39:1730
31struct _Unwind_FunctionContext {
32 // next function in stack of handlers
33 struct _Unwind_FunctionContext *prev;
34
Kazushi (Jam) Marukawa3cbd4762020-12-26 13:50:1735#if defined(__ve__)
Gabriel Ravierdad00da2022-08-21 01:09:0336 // VE requires to store 64 bit pointers in the buffer for SjLj exception.
Kazushi (Jam) Marukawa3cbd4762020-12-26 13:50:1737 // We expand the size of values defined here. This size must be matched
38 // to the size returned by TargetMachine::getSjLjDataSize().
39
40 // set by calling function before registering to be the landing pad
41 uint64_t resumeLocation;
42
43 // set by personality handler to be parameters passed to landing pad function
44 uint64_t resumeParameters[4];
45#else
Saleem Abdulrasoolb1b19112015-04-24 19:39:1746 // set by calling function before registering to be the landing pad
Martin Storsjo3b6ea6a2017-09-26 08:07:1747 uint32_t resumeLocation;
Saleem Abdulrasoolb1b19112015-04-24 19:39:1748
49 // set by personality handler to be parameters passed to landing pad function
Martin Storsjo3b6ea6a2017-09-26 08:07:1750 uint32_t resumeParameters[4];
Kazushi (Jam) Marukawa3cbd4762020-12-26 13:50:1751#endif
Saleem Abdulrasoolb1b19112015-04-24 19:39:1752
53 // set by calling function before registering
Saleem Abdulrasool14798b42020-02-10 16:52:3154 _Unwind_Personality_Fn personality; // arm offset=24
Saleem Abdulrasoolb1b19112015-04-24 19:39:1755 uintptr_t lsda; // arm offset=28
56
57 // variable length array, contains registers to restore
58 // 0 = r7, 1 = pc, 2 = sp
59 void *jbuf[];
60};
61
Saleem Abdulrasool9ebbda82017-10-01 20:06:4862#if defined(_LIBUNWIND_HAS_NO_THREADS)
63# define _LIBUNWIND_THREAD_LOCAL
64#else
65# if __STDC_VERSION__ >= 201112L
66# define _LIBUNWIND_THREAD_LOCAL _Thread_local
Martin Storsjoe563e082019-01-18 20:31:1267# elif defined(_MSC_VER)
Saleem Abdulrasool9ebbda82017-10-01 20:06:4868# define _LIBUNWIND_THREAD_LOCAL __declspec(thread)
69# elif defined(__GNUC__) || defined(__clang__)
70# define _LIBUNWIND_THREAD_LOCAL __thread
71# else
72# error Unable to create thread local storage
73# endif
74#endif
75
76
77#if !defined(FOR_DYLD)
78
79#if defined(__APPLE__)
80#include <System/pthread_machdep.h>
81#else
Martin Storsjo59a50042017-10-01 20:22:4082static _LIBUNWIND_THREAD_LOCAL struct _Unwind_FunctionContext *stack = NULL;
Saleem Abdulrasool9ebbda82017-10-01 20:06:4883#endif
84
85static struct _Unwind_FunctionContext *__Unwind_SjLj_GetTopOfFunctionStack() {
86#if defined(__APPLE__)
87 return _pthread_getspecific_direct(__PTK_LIBC_DYLD_Unwind_SjLj_Key);
88#else
89 return stack;
90#endif
91}
92
93static void
94__Unwind_SjLj_SetTopOfFunctionStack(struct _Unwind_FunctionContext *fc) {
95#if defined(__APPLE__)
96 _pthread_setspecific_direct(__PTK_LIBC_DYLD_Unwind_SjLj_Key, fc);
97#else
98 stack = fc;
99#endif
100}
101
102#endif
103
Saleem Abdulrasoolb1b19112015-04-24 19:39:17104
105/// Called at start of each function that catches exceptions
106_LIBUNWIND_EXPORT void
107_Unwind_SjLj_Register(struct _Unwind_FunctionContext *fc) {
108 fc->prev = __Unwind_SjLj_GetTopOfFunctionStack();
109 __Unwind_SjLj_SetTopOfFunctionStack(fc);
110}
111
112
113/// Called at end of each function that catches exceptions
114_LIBUNWIND_EXPORT void
115_Unwind_SjLj_Unregister(struct _Unwind_FunctionContext *fc) {
116 __Unwind_SjLj_SetTopOfFunctionStack(fc->prev);
117}
118
119
120static _Unwind_Reason_Code
121unwind_phase1(struct _Unwind_Exception *exception_object) {
122 _Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack();
Martin Storsjo94adf432019-01-22 20:50:39123 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1: initial function-context=%p",
124 (void *)c);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17125
126 // walk each frame looking for a place to stop
127 for (bool handlerNotFound = true; handlerNotFound; c = c->prev) {
128
129 // check for no more frames
130 if (c == NULL) {
131 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): reached "
Ed Maste92c94892016-08-30 15:38:10132 "bottom => _URC_END_OF_STACK",
Martin Storsjo94adf432019-01-22 20:50:39133 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17134 return _URC_END_OF_STACK;
135 }
136
Martin Storsjo94adf432019-01-22 20:50:39137 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1: function-context=%p", (void *)c);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17138 // if there is a personality routine, ask it if it will want to stop at this
139 // frame
140 if (c->personality != NULL) {
141 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): calling "
Martin Storsjo94adf432019-01-22 20:50:39142 "personality function %p",
143 (void *)exception_object,
144 (void *)c->personality);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17145 _Unwind_Reason_Code personalityResult = (*c->personality)(
146 1, _UA_SEARCH_PHASE, exception_object->exception_class,
147 exception_object, (struct _Unwind_Context *)c);
148 switch (personalityResult) {
149 case _URC_HANDLER_FOUND:
150 // found a catch clause or locals that need destructing in this frame
151 // stop search and remember function context
152 handlerNotFound = false;
153 exception_object->private_2 = (uintptr_t) c;
154 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): "
Martin Storsjo94adf432019-01-22 20:50:39155 "_URC_HANDLER_FOUND",
156 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17157 return _URC_NO_REASON;
158
159 case _URC_CONTINUE_UNWIND:
160 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): "
Martin Storsjo94adf432019-01-22 20:50:39161 "_URC_CONTINUE_UNWIND",
162 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17163 // continue unwinding
164 break;
165
166 default:
167 // something went wrong
168 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste92c94892016-08-30 15:38:10169 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
Martin Storsjo94adf432019-01-22 20:50:39170 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17171 return _URC_FATAL_PHASE1_ERROR;
172 }
173 }
174 }
175 return _URC_NO_REASON;
176}
177
178
179static _Unwind_Reason_Code
180unwind_phase2(struct _Unwind_Exception *exception_object) {
Martin Storsjo94adf432019-01-22 20:50:39181 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
182 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17183
184 // walk each frame until we reach where search phase said to stop
185 _Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack();
186 while (true) {
Ed Maste92c94892016-08-30 15:38:10187 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2s(ex_ojb=%p): context=%p",
Martin Storsjo94adf432019-01-22 20:50:39188 (void *)exception_object, (void *)c);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17189
190 // check for no more frames
191 if (c == NULL) {
Petr Hoseke369a982019-04-03 21:50:03192 _LIBUNWIND_TRACE_UNWINDING(
193 "unwind_phase2(ex_ojb=%p): __unw_step() reached "
194 "bottom => _URC_END_OF_STACK",
195 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17196 return _URC_END_OF_STACK;
197 }
198
199 // if there is a personality routine, tell it we are unwinding
200 if (c->personality != NULL) {
201 _Unwind_Action action = _UA_CLEANUP_PHASE;
202 if ((uintptr_t) c == exception_object->private_2)
203 action = (_Unwind_Action)(
204 _UA_CLEANUP_PHASE |
205 _UA_HANDLER_FRAME); // tell personality this was the frame it marked
206 // in phase 1
207 _Unwind_Reason_Code personalityResult =
208 (*c->personality)(1, action, exception_object->exception_class,
209 exception_object, (struct _Unwind_Context *)c);
210 switch (personalityResult) {
211 case _URC_CONTINUE_UNWIND:
212 // continue unwinding
213 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste92c94892016-08-30 15:38:10214 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Martin Storsjo94adf432019-01-22 20:50:39215 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17216 if ((uintptr_t) c == exception_object->private_2) {
217 // phase 1 said we would stop at this frame, but we did not...
218 _LIBUNWIND_ABORT("during phase1 personality function said it would "
219 "stop here, but now if phase2 it did not stop here");
220 }
221 break;
222 case _URC_INSTALL_CONTEXT:
223 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): "
Martin Storsjo94adf432019-01-22 20:50:39224 "_URC_INSTALL_CONTEXT, will resume at "
225 "landing pad %p",
226 (void *)exception_object, c->jbuf[1]);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17227 // personality routine says to transfer control to landing pad
228 // we may get control back if landing pad calls _Unwind_Resume()
229 __Unwind_SjLj_SetTopOfFunctionStack(c);
230 __builtin_longjmp(c->jbuf, 1);
Petr Hoseke369a982019-04-03 21:50:03231 // __unw_resume() only returns if there was an error
Saleem Abdulrasoolb1b19112015-04-24 19:39:17232 return _URC_FATAL_PHASE2_ERROR;
233 default:
234 // something went wrong
235 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
236 personalityResult);
237 return _URC_FATAL_PHASE2_ERROR;
238 }
239 }
240 c = c->prev;
241 }
242
243 // clean up phase did not resume at the frame that the search phase said it
244 // would
245 return _URC_FATAL_PHASE2_ERROR;
246}
247
248
249static _Unwind_Reason_Code
250unwind_phase2_forced(struct _Unwind_Exception *exception_object,
251 _Unwind_Stop_Fn stop, void *stop_parameter) {
252 // walk each frame until we reach where search phase said to stop
253 _Unwind_FunctionContext_t c = __Unwind_SjLj_GetTopOfFunctionStack();
254 while (true) {
255
256 // get next frame (skip over first which is _Unwind_RaiseException)
257 if (c == NULL) {
Petr Hoseke369a982019-04-03 21:50:03258 _LIBUNWIND_TRACE_UNWINDING(
259 "unwind_phase2(ex_ojb=%p): __unw_step() reached "
260 "bottom => _URC_END_OF_STACK",
261 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17262 return _URC_END_OF_STACK;
263 }
264
265 // call stop function at each frame
266 _Unwind_Action action =
267 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
268 _Unwind_Reason_Code stopResult =
269 (*stop)(1, action, exception_object->exception_class, exception_object,
270 (struct _Unwind_Context *)c, stop_parameter);
271 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
Ed Maste92c94892016-08-30 15:38:10272 "stop function returned %d",
Martin Storsjo94adf432019-01-22 20:50:39273 (void *)exception_object, stopResult);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17274 if (stopResult != _URC_NO_REASON) {
275 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
Ed Maste92c94892016-08-30 15:38:10276 "stopped by stop function",
Martin Storsjo94adf432019-01-22 20:50:39277 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17278 return _URC_FATAL_PHASE2_ERROR;
279 }
280
281 // if there is a personality routine, tell it we are unwinding
282 if (c->personality != NULL) {
Saleem Abdulrasool14798b42020-02-10 16:52:31283 _Unwind_Personality_Fn p = (_Unwind_Personality_Fn)c->personality;
Saleem Abdulrasoolb1b19112015-04-24 19:39:17284 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
Ed Maste92c94892016-08-30 15:38:10285 "calling personality function %p",
Martin Storsjo94adf432019-01-22 20:50:39286 (void *)exception_object, (void *)p);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17287 _Unwind_Reason_Code personalityResult =
288 (*p)(1, action, exception_object->exception_class, exception_object,
289 (struct _Unwind_Context *)c);
290 switch (personalityResult) {
291 case _URC_CONTINUE_UNWIND:
292 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
Ed Maste92c94892016-08-30 15:38:10293 "personality returned _URC_CONTINUE_UNWIND",
Martin Storsjo94adf432019-01-22 20:50:39294 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17295 // destructors called, continue unwinding
296 break;
297 case _URC_INSTALL_CONTEXT:
298 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
Ed Maste92c94892016-08-30 15:38:10299 "personality returned _URC_INSTALL_CONTEXT",
Martin Storsjo94adf432019-01-22 20:50:39300 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17301 // we may get control back if landing pad calls _Unwind_Resume()
302 __Unwind_SjLj_SetTopOfFunctionStack(c);
303 __builtin_longjmp(c->jbuf, 1);
304 break;
305 default:
306 // something went wrong
307 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
308 "personality returned %d, "
Ed Maste92c94892016-08-30 15:38:10309 "_URC_FATAL_PHASE2_ERROR",
Martin Storsjo94adf432019-01-22 20:50:39310 (void *)exception_object, personalityResult);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17311 return _URC_FATAL_PHASE2_ERROR;
312 }
313 }
314 c = c->prev;
315 }
316
317 // call stop function one last time and tell it we've reached the end of the
318 // stack
319 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
Martin Storsjo94adf432019-01-22 20:50:39320 "function with _UA_END_OF_STACK",
321 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17322 _Unwind_Action lastAction =
323 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
324 (*stop)(1, lastAction, exception_object->exception_class, exception_object,
325 (struct _Unwind_Context *)c, stop_parameter);
326
327 // clean up phase did not resume at the frame that the search phase said it
328 // would
329 return _URC_FATAL_PHASE2_ERROR;
330}
331
332
333/// Called by __cxa_throw. Only returns if there is a fatal error
334_LIBUNWIND_EXPORT _Unwind_Reason_Code
335_Unwind_SjLj_RaiseException(struct _Unwind_Exception *exception_object) {
Martin Storsjo94adf432019-01-22 20:50:39336 _LIBUNWIND_TRACE_API("_Unwind_SjLj_RaiseException(ex_obj=%p)",
337 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17338
339 // mark that this is a non-forced unwind, so _Unwind_Resume() can do the right
340 // thing
341 exception_object->private_1 = 0;
342 exception_object->private_2 = 0;
343
344 // phase 1: the search phase
345 _Unwind_Reason_Code phase1 = unwind_phase1(exception_object);
346 if (phase1 != _URC_NO_REASON)
347 return phase1;
348
349 // phase 2: the clean up phase
350 return unwind_phase2(exception_object);
351}
352
353
354
355/// When _Unwind_RaiseException() is in phase2, it hands control
356/// to the personality function at each frame. The personality
357/// may force a jump to a landing pad in that function, the landing
358/// pad code may then call _Unwind_Resume() to continue with the
359/// unwinding. Note: the call to _Unwind_Resume() is from compiler
Gabriel Ravierdad00da2022-08-21 01:09:03360/// generated user code. All other _Unwind_* routines are called
Saleem Abdulrasoolb1b19112015-04-24 19:39:17361/// by the C++ runtime __cxa_* routines.
362///
363/// Re-throwing an exception is implemented by having the code call
364/// __cxa_rethrow() which in turn calls _Unwind_Resume_or_Rethrow()
365_LIBUNWIND_EXPORT void
366_Unwind_SjLj_Resume(struct _Unwind_Exception *exception_object) {
Martin Storsjo94adf432019-01-22 20:50:39367 _LIBUNWIND_TRACE_API("_Unwind_SjLj_Resume(ex_obj=%p)",
368 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17369
370 if (exception_object->private_1 != 0)
371 unwind_phase2_forced(exception_object,
372 (_Unwind_Stop_Fn) exception_object->private_1,
373 (void *)exception_object->private_2);
374 else
375 unwind_phase2(exception_object);
376
377 // clients assume _Unwind_Resume() does not return, so all we can do is abort.
378 _LIBUNWIND_ABORT("_Unwind_SjLj_Resume() can't return");
379}
380
381
382/// Called by __cxa_rethrow().
383_LIBUNWIND_EXPORT _Unwind_Reason_Code
384_Unwind_SjLj_Resume_or_Rethrow(struct _Unwind_Exception *exception_object) {
385 _LIBUNWIND_TRACE_API("__Unwind_SjLj_Resume_or_Rethrow(ex_obj=%p), "
Martin Storsjo94adf432019-01-22 20:50:39386 "private_1=%" PRIuPTR,
387 (void *)exception_object, exception_object->private_1);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17388 // If this is non-forced and a stopping place was found, then this is a
389 // re-throw.
390 // Call _Unwind_RaiseException() as if this was a new exception.
391 if (exception_object->private_1 == 0) {
392 return _Unwind_SjLj_RaiseException(exception_object);
393 // should return if there is no catch clause, so that __cxa_rethrow can call
394 // std::terminate()
395 }
396
Gabriel Ravierdad00da2022-08-21 01:09:03397 // Call through to _Unwind_Resume() which distinguishes between forced and
Saleem Abdulrasoolb1b19112015-04-24 19:39:17398 // regular exceptions.
399 _Unwind_SjLj_Resume(exception_object);
400 _LIBUNWIND_ABORT("__Unwind_SjLj_Resume_or_Rethrow() called "
401 "_Unwind_SjLj_Resume() which unexpectedly returned");
402}
403
404
405/// Called by personality handler during phase 2 to get LSDA for current frame.
406_LIBUNWIND_EXPORT uintptr_t
407_Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
408 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
409 _LIBUNWIND_TRACE_API("_Unwind_GetLanguageSpecificData(context=%p) "
Martin Storsjo94adf432019-01-22 20:50:39410 "=> 0x%" PRIuPTR,
411 (void *)context, ufc->lsda);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17412 return ufc->lsda;
413}
414
415
416/// Called by personality handler during phase 2 to get register values.
417_LIBUNWIND_EXPORT uintptr_t _Unwind_GetGR(struct _Unwind_Context *context,
418 int index) {
Martin Storsjo94adf432019-01-22 20:50:39419 _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d)", (void *)context,
420 index);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17421 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
422 return ufc->resumeParameters[index];
423}
424
425
426/// Called by personality handler during phase 2 to alter register values.
427_LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
428 uintptr_t new_value) {
Martin Storsjo94adf432019-01-22 20:50:39429 _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%" PRIuPTR
430 ")",
431 (void *)context, index, new_value);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17432 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
433 ufc->resumeParameters[index] = new_value;
434}
435
436
437/// Called by personality handler during phase 2 to get instruction pointer.
438_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
439 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
Martin Storsjo94adf432019-01-22 20:50:39440 _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIu32,
441 (void *)context, ufc->resumeLocation + 1);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17442 return ufc->resumeLocation + 1;
443}
444
445
446/// Called by personality handler during phase 2 to get instruction pointer.
447/// ipBefore is a boolean that says if IP is already adjusted to be the call
448/// site address. Normally IP is the return address.
449_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
450 int *ipBefore) {
451 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
452 *ipBefore = 0;
Martin Storsjo94adf432019-01-22 20:50:39453 _LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p, %p) => 0x%" PRIu32,
454 (void *)context, (void *)ipBefore,
455 ufc->resumeLocation + 1);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17456 return ufc->resumeLocation + 1;
457}
458
459
460/// Called by personality handler during phase 2 to alter instruction pointer.
461_LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
462 uintptr_t new_value) {
Martin Storsjo94adf432019-01-22 20:50:39463 _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%" PRIuPTR ")",
464 (void *)context, new_value);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17465 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
466 ufc->resumeLocation = new_value - 1;
467}
468
469
470/// Called by personality handler during phase 2 to find the start of the
471/// function.
472_LIBUNWIND_EXPORT uintptr_t
473_Unwind_GetRegionStart(struct _Unwind_Context *context) {
474 // Not supported or needed for sjlj based unwinding
475 (void)context;
Martin Storsjo94adf432019-01-22 20:50:39476 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p)", (void *)context);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17477 return 0;
478}
479
480
481/// Called by personality handler during phase 2 if a foreign exception
482/// is caught.
483_LIBUNWIND_EXPORT void
484_Unwind_DeleteException(struct _Unwind_Exception *exception_object) {
Ed Maste92c94892016-08-30 15:38:10485 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
Martin Storsjo94adf432019-01-22 20:50:39486 (void *)exception_object);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17487 if (exception_object->exception_cleanup != NULL)
488 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
489 exception_object);
490}
491
492
493
494/// Called by personality handler during phase 2 to get base address for data
495/// relative encodings.
496_LIBUNWIND_EXPORT uintptr_t
497_Unwind_GetDataRelBase(struct _Unwind_Context *context) {
498 // Not supported or needed for sjlj based unwinding
499 (void)context;
Martin Storsjo94adf432019-01-22 20:50:39500 _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)", (void *)context);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17501 _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented");
502}
503
504
505/// Called by personality handler during phase 2 to get base address for text
506/// relative encodings.
507_LIBUNWIND_EXPORT uintptr_t
508_Unwind_GetTextRelBase(struct _Unwind_Context *context) {
509 // Not supported or needed for sjlj based unwinding
510 (void)context;
Martin Storsjo94adf432019-01-22 20:50:39511 _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)", (void *)context);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17512 _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented");
513}
514
515
516/// Called by personality handler to get "Call Frame Area" for current frame.
517_LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) {
Martin Storsjo94adf432019-01-22 20:50:39518 _LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p)", (void *)context);
Saleem Abdulrasoolb1b19112015-04-24 19:39:17519 if (context != NULL) {
520 _Unwind_FunctionContext_t ufc = (_Unwind_FunctionContext_t) context;
521 // Setjmp/longjmp based exceptions don't have a true CFA.
522 // Instead, the SP in the jmpbuf is the closest approximation.
523 return (uintptr_t) ufc->jbuf[2];
524 }
525 return 0;
526}
527
Ranjeet Singh58080112017-03-31 15:28:06528#endif // defined(_LIBUNWIND_BUILD_SJLJ_APIS)