blob: 9d4084ff0c48f5ca967f06c5e4dea689883ff64b [file] [log] [blame]
Saleem Abdulrasool17552662015-04-24 19:39:17 +00001//===------------------------- UnwindLevel1.c -----------------------------===//
2//
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 C++ ABI Exception Handling Level 1 as documented at:
9// http://mentorembedded.github.io/cxx-abi/abi-eh.html
10// using libunwind
11//
12//===----------------------------------------------------------------------===//
13
Logan Chien5456e8d2015-07-24 00:16:48 +000014// ARM EHABI does not specify _Unwind_{Get,Set}{GR,IP}(). Thus, we are
15// defining inline functions to delegate the function calls to
16// _Unwind_VRS_{Get,Set}(). However, some applications might declare the
17// function protetype directly (instead of including <unwind.h>), thus we need
18// to export these functions from libunwind.so as well.
19#define _LIBUNWIND_UNWIND_LEVEL1_EXTERNAL_LINKAGE 1
20
Saleem Abdulrasool17552662015-04-24 19:39:17 +000021#include <inttypes.h>
22#include <stdint.h>
23#include <stdbool.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27
28#include "libunwind.h"
29#include "unwind.h"
30#include "config.h"
31
Martin Storsjo9f16c6c2017-09-26 08:07:26 +000032#if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)
Saleem Abdulrasool17552662015-04-24 19:39:17 +000033
Charles Davisfa2e6202018-08-30 21:29:00 +000034#ifndef _LIBUNWIND_SUPPORT_SEH_UNWIND
35
Saleem Abdulrasool17552662015-04-24 19:39:17 +000036static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000037unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
38 unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000039
40 // Walk each frame looking for a place to stop.
Logan Chienf93f2352015-06-25 00:05:24 +000041 bool handlerNotFound = true;
42 while (handlerNotFound) {
Ed Maste6f723382016-08-30 13:08:21 +000043 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +000044 // _Unwind_RaiseException).
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000045 int stepResult = unw_step(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000046 if (stepResult == 0) {
47 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_step() reached "
Ed Maste41bc5a72016-08-30 15:38:10 +000048 "bottom => _URC_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000049 (void *)exception_object);
50 return _URC_END_OF_STACK;
51 } else if (stepResult < 0) {
52 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_step failed => "
Ed Maste41bc5a72016-08-30 15:38:10 +000053 "_URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000054 (void *)exception_object);
55 return _URC_FATAL_PHASE1_ERROR;
56 }
57
58 // See if frame has code to run (has personality routine).
59 unw_proc_info_t frameInfo;
60 unw_word_t sp;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000061 if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +000062 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_get_proc_info "
Ed Maste41bc5a72016-08-30 15:38:10 +000063 "failed => _URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000064 (void *)exception_object);
65 return _URC_FATAL_PHASE1_ERROR;
66 }
67
68 // When tracing, print state information.
69 if (_LIBUNWIND_TRACING_UNWINDING) {
70 char functionBuf[512];
71 const char *functionName = functionBuf;
72 unw_word_t offset;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000073 if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
Saleem Abdulrasool17552662015-04-24 19:39:17 +000074 &offset) != UNW_ESUCCESS) ||
75 (frameInfo.start_ip + offset > frameInfo.end_ip))
76 functionName = ".anonymous.";
77 unw_word_t pc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000078 unw_get_reg(cursor, UNW_REG_IP, &pc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000079 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +000080 "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR
81 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000082 (void *)exception_object, pc, frameInfo.start_ip, functionName,
83 frameInfo.lsda, frameInfo.handler);
84 }
85
86 // If there is a personality routine, ask it if it will want to stop at
87 // this frame.
88 if (frameInfo.handler != 0) {
89 __personality_routine p =
Martin Storsjocac41382017-10-27 08:11:36 +000090 (__personality_routine)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +000091 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +000092 "unwind_phase1(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +000093 (void *)exception_object, (void *)(uintptr_t)p);
94 _Unwind_Reason_Code personalityResult =
95 (*p)(1, _UA_SEARCH_PHASE, exception_object->exception_class,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +000096 exception_object, (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +000097 switch (personalityResult) {
98 case _URC_HANDLER_FOUND:
99 // found a catch clause or locals that need destructing in this frame
100 // stop search and remember stack pointer at the frame
101 handlerNotFound = false;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000102 unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000103 exception_object->private_2 = (uintptr_t)sp;
104 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000105 "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000106 (void *)exception_object);
107 return _URC_NO_REASON;
108
109 case _URC_CONTINUE_UNWIND:
110 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000111 "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000112 (void *)exception_object);
113 // continue unwinding
114 break;
115
116 default:
117 // something went wrong
118 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000119 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000120 (void *)exception_object);
121 return _URC_FATAL_PHASE1_ERROR;
122 }
123 }
124 }
125 return _URC_NO_REASON;
126}
127
128
129static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000130unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
131 unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000132
Ed Maste41bc5a72016-08-30 15:38:10 +0000133 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000134 (void *)exception_object);
135
136 // Walk each frame until we reach where search phase said to stop.
137 while (true) {
138
Ed Maste6f723382016-08-30 13:08:21 +0000139 // Ask libunwind to get next frame (skip over first which is
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000140 // _Unwind_RaiseException).
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000141 int stepResult = unw_step(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000142 if (stepResult == 0) {
143 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step() reached "
Ed Maste41bc5a72016-08-30 15:38:10 +0000144 "bottom => _URC_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000145 (void *)exception_object);
146 return _URC_END_OF_STACK;
147 } else if (stepResult < 0) {
148 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_step failed => "
Ed Maste41bc5a72016-08-30 15:38:10 +0000149 "_URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000150 (void *)exception_object);
151 return _URC_FATAL_PHASE2_ERROR;
152 }
153
154 // Get info about this frame.
155 unw_word_t sp;
156 unw_proc_info_t frameInfo;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000157 unw_get_reg(cursor, UNW_REG_SP, &sp);
158 if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000159 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_get_proc_info "
Ed Maste41bc5a72016-08-30 15:38:10 +0000160 "failed => _URC_FATAL_PHASE1_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000161 (void *)exception_object);
162 return _URC_FATAL_PHASE2_ERROR;
163 }
164
165 // When tracing, print state information.
166 if (_LIBUNWIND_TRACING_UNWINDING) {
167 char functionBuf[512];
168 const char *functionName = functionBuf;
169 unw_word_t offset;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000170 if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000171 &offset) != UNW_ESUCCESS) ||
172 (frameInfo.start_ip + offset > frameInfo.end_ip))
173 functionName = ".anonymous.";
Martin Storsjo97e2d982017-10-30 19:06:34 +0000174 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR
175 ", func=%s, sp=0x%" PRIxPTR ", lsda=0x%" PRIxPTR
176 ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000177 (void *)exception_object, frameInfo.start_ip,
178 functionName, sp, frameInfo.lsda,
179 frameInfo.handler);
180 }
181
182 // If there is a personality routine, tell it we are unwinding.
183 if (frameInfo.handler != 0) {
184 __personality_routine p =
Martin Storsjocac41382017-10-27 08:11:36 +0000185 (__personality_routine)(uintptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000186 _Unwind_Action action = _UA_CLEANUP_PHASE;
187 if (sp == exception_object->private_2) {
188 // Tell personality this was the frame it marked in phase 1.
189 action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME);
190 }
191 _Unwind_Reason_Code personalityResult =
192 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000193 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000194 switch (personalityResult) {
195 case _URC_CONTINUE_UNWIND:
196 // Continue unwinding
197 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000198 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000199 (void *)exception_object);
200 if (sp == exception_object->private_2) {
201 // Phase 1 said we would stop at this frame, but we did not...
202 _LIBUNWIND_ABORT("during phase1 personality function said it would "
203 "stop here, but now in phase2 it did not stop here");
204 }
205 break;
206 case _URC_INSTALL_CONTEXT:
207 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000208 "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000209 (void *)exception_object);
210 // Personality routine says to transfer control to landing pad.
211 // We may get control back if landing pad calls _Unwind_Resume().
212 if (_LIBUNWIND_TRACING_UNWINDING) {
213 unw_word_t pc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000214 unw_get_reg(cursor, UNW_REG_IP, &pc);
215 unw_get_reg(cursor, UNW_REG_SP, &sp);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000216 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
Martin Storsjo97e2d982017-10-30 19:06:34 +0000217 "user code with ip=0x%" PRIxPTR
218 ", sp=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000219 (void *)exception_object, pc, sp);
220 }
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000221 unw_resume(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000222 // unw_resume() only returns if there was an error.
223 return _URC_FATAL_PHASE2_ERROR;
224 default:
225 // Personality routine returned an unknown result code.
226 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
227 personalityResult);
228 return _URC_FATAL_PHASE2_ERROR;
229 }
230 }
231 }
232
233 // Clean up phase did not resume at the frame that the search phase
234 // said it would...
235 return _URC_FATAL_PHASE2_ERROR;
236}
237
238static _Unwind_Reason_Code
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000239unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000240 _Unwind_Exception *exception_object,
241 _Unwind_Stop_Fn stop, void *stop_parameter) {
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000242 unw_init_local(cursor, uc);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000243
244 // Walk each frame until we reach where search phase said to stop
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000245 while (unw_step(cursor) > 0) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000246
247 // Update info about this frame.
248 unw_proc_info_t frameInfo;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000249 if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000250 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): unw_step "
Ed Maste41bc5a72016-08-30 15:38:10 +0000251 "failed => _URC_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000252 (void *)exception_object);
253 return _URC_FATAL_PHASE2_ERROR;
254 }
255
256 // When tracing, print state information.
257 if (_LIBUNWIND_TRACING_UNWINDING) {
258 char functionBuf[512];
259 const char *functionName = functionBuf;
260 unw_word_t offset;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000261 if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000262 &offset) != UNW_ESUCCESS) ||
263 (frameInfo.start_ip + offset > frameInfo.end_ip))
264 functionName = ".anonymous.";
265 _LIBUNWIND_TRACE_UNWINDING(
Martin Storsjo97e2d982017-10-30 19:06:34 +0000266 "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
267 ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000268 (void *)exception_object, frameInfo.start_ip, functionName,
269 frameInfo.lsda, frameInfo.handler);
270 }
271
272 // Call stop function at each frame.
273 _Unwind_Action action =
274 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
275 _Unwind_Reason_Code stopResult =
276 (*stop)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000277 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000278 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000279 "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000280 (void *)exception_object, stopResult);
281 if (stopResult != _URC_NO_REASON) {
282 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000283 "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000284 (void *)exception_object);
285 return _URC_FATAL_PHASE2_ERROR;
286 }
287
288 // If there is a personality routine, tell it we are unwinding.
289 if (frameInfo.handler != 0) {
290 __personality_routine p =
Charles Davis5aede5d2018-08-08 04:21:24 +0000291 (__personality_routine)(intptr_t)(frameInfo.handler);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000292 _LIBUNWIND_TRACE_UNWINDING(
Ed Maste41bc5a72016-08-30 15:38:10 +0000293 "unwind_phase2_forced(ex_ojb=%p): calling personality function %p",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000294 (void *)exception_object, (void *)(uintptr_t)p);
295 _Unwind_Reason_Code personalityResult =
296 (*p)(1, action, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000297 (struct _Unwind_Context *)(cursor));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000298 switch (personalityResult) {
299 case _URC_CONTINUE_UNWIND:
300 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
301 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000302 "_URC_CONTINUE_UNWIND",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000303 (void *)exception_object);
304 // Destructors called, continue unwinding
305 break;
306 case _URC_INSTALL_CONTEXT:
307 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
308 "personality returned "
Ed Maste41bc5a72016-08-30 15:38:10 +0000309 "_URC_INSTALL_CONTEXT",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000310 (void *)exception_object);
311 // We may get control back if landing pad calls _Unwind_Resume().
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000312 unw_resume(cursor);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000313 break;
314 default:
315 // Personality routine returned an unknown result code.
316 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
317 "personality returned %d, "
Ed Maste41bc5a72016-08-30 15:38:10 +0000318 "_URC_FATAL_PHASE2_ERROR",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000319 (void *)exception_object, personalityResult);
320 return _URC_FATAL_PHASE2_ERROR;
321 }
322 }
323 }
324
325 // Call stop function one last time and tell it we've reached the end
326 // of the stack.
327 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
Ed Maste41bc5a72016-08-30 15:38:10 +0000328 "function with _UA_END_OF_STACK",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000329 (void *)exception_object);
330 _Unwind_Action lastAction =
331 (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
332 (*stop)(1, lastAction, exception_object->exception_class, exception_object,
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000333 (struct _Unwind_Context *)(cursor), stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000334
335 // Clean up phase did not resume at the frame that the search phase said it
336 // would.
337 return _URC_FATAL_PHASE2_ERROR;
338}
339
340
341/// Called by __cxa_throw. Only returns if there is a fatal error.
342_LIBUNWIND_EXPORT _Unwind_Reason_Code
343_Unwind_RaiseException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000344 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000345 (void *)exception_object);
346 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000347 unw_cursor_t cursor;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000348 unw_getcontext(&uc);
349
350 // Mark that this is a non-forced unwind, so _Unwind_Resume()
351 // can do the right thing.
352 exception_object->private_1 = 0;
353 exception_object->private_2 = 0;
354
355 // phase 1: the search phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000356 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000357 if (phase1 != _URC_NO_REASON)
358 return phase1;
359
360 // phase 2: the clean up phase
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000361 return unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000362}
363
364
365
366/// When _Unwind_RaiseException() is in phase2, it hands control
367/// to the personality function at each frame. The personality
368/// may force a jump to a landing pad in that function, the landing
369/// pad code may then call _Unwind_Resume() to continue with the
370/// unwinding. Note: the call to _Unwind_Resume() is from compiler
371/// geneated user code. All other _Unwind_* routines are called
372/// by the C++ runtime __cxa_* routines.
373///
374/// Note: re-throwing an exception (as opposed to continuing the unwind)
375/// is implemented by having the code call __cxa_rethrow() which
376/// in turn calls _Unwind_Resume_or_Rethrow().
377_LIBUNWIND_EXPORT void
378_Unwind_Resume(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000379 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000380 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000381 unw_cursor_t cursor;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000382 unw_getcontext(&uc);
383
384 if (exception_object->private_1 != 0)
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000385 unwind_phase2_forced(&uc, &cursor, exception_object,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000386 (_Unwind_Stop_Fn) exception_object->private_1,
387 (void *)exception_object->private_2);
388 else
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000389 unwind_phase2(&uc, &cursor, exception_object);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000390
391 // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
392 _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
393}
394
395
396
397/// Not used by C++.
398/// Unwinds stack, calling "stop" function at each frame.
399/// Could be used to implement longjmp().
400_LIBUNWIND_EXPORT _Unwind_Reason_Code
401_Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
402 _Unwind_Stop_Fn stop, void *stop_parameter) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000403 _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000404 (void *)exception_object, (void *)(uintptr_t)stop);
405 unw_context_t uc;
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000406 unw_cursor_t cursor;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000407 unw_getcontext(&uc);
408
409 // Mark that this is a forced unwind, so _Unwind_Resume() can do
410 // the right thing.
411 exception_object->private_1 = (uintptr_t) stop;
412 exception_object->private_2 = (uintptr_t) stop_parameter;
413
414 // do it
Asiri Rathnayake2ae6e622016-06-14 15:51:01 +0000415 return unwind_phase2_forced(&uc, &cursor, exception_object, stop, stop_parameter);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000416}
417
418
419/// Called by personality handler during phase 2 to get LSDA for current frame.
420_LIBUNWIND_EXPORT uintptr_t
421_Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
422 unw_cursor_t *cursor = (unw_cursor_t *)context;
423 unw_proc_info_t frameInfo;
424 uintptr_t result = 0;
425 if (unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
426 result = (uintptr_t)frameInfo.lsda;
427 _LIBUNWIND_TRACE_API(
Ed Maste41bc5a72016-08-30 15:38:10 +0000428 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000429 (void *)context, result);
430 if (result != 0) {
431 if (*((uint8_t *)result) != 0xFF)
Ed Maste41bc5a72016-08-30 15:38:10 +0000432 _LIBUNWIND_DEBUG_LOG("lsda at 0x%" PRIxPTR " does not start with 0xFF",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000433 result);
434 }
435 return result;
436}
437
438
439/// Called by personality handler during phase 2 to find the start of the
440/// function.
441_LIBUNWIND_EXPORT uintptr_t
442_Unwind_GetRegionStart(struct _Unwind_Context *context) {
443 unw_cursor_t *cursor = (unw_cursor_t *)context;
444 unw_proc_info_t frameInfo;
445 uintptr_t result = 0;
446 if (unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
447 result = (uintptr_t)frameInfo.start_ip;
Ed Maste41bc5a72016-08-30 15:38:10 +0000448 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000449 (void *)context, result);
450 return result;
451}
452
Charles Davisfa2e6202018-08-30 21:29:00 +0000453#endif // !_LIBUNWIND_SUPPORT_SEH_UNWIND
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000454
455/// Called by personality handler during phase 2 if a foreign exception
456// is caught.
457_LIBUNWIND_EXPORT void
458_Unwind_DeleteException(_Unwind_Exception *exception_object) {
Ed Maste41bc5a72016-08-30 15:38:10 +0000459 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000460 (void *)exception_object);
461 if (exception_object->exception_cleanup != NULL)
462 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
463 exception_object);
464}
465
466/// Called by personality handler during phase 2 to get register values.
467_LIBUNWIND_EXPORT uintptr_t
468_Unwind_GetGR(struct _Unwind_Context *context, int index) {
469 unw_cursor_t *cursor = (unw_cursor_t *)context;
470 unw_word_t result;
471 unw_get_reg(cursor, index, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000472 _LIBUNWIND_TRACE_API("_Unwind_GetGR(context=%p, reg=%d) => 0x%" PRIxPTR,
473 (void *)context, index, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000474 return (uintptr_t)result;
475}
476
477/// Called by personality handler during phase 2 to alter register values.
478_LIBUNWIND_EXPORT void _Unwind_SetGR(struct _Unwind_Context *context, int index,
479 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000480 _LIBUNWIND_TRACE_API("_Unwind_SetGR(context=%p, reg=%d, value=0x%0" PRIxPTR
Ed Maste41bc5a72016-08-30 15:38:10 +0000481 ")",
Martin Storsjo97e2d982017-10-30 19:06:34 +0000482 (void *)context, index, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000483 unw_cursor_t *cursor = (unw_cursor_t *)context;
484 unw_set_reg(cursor, index, value);
485}
486
487/// Called by personality handler during phase 2 to get instruction pointer.
488_LIBUNWIND_EXPORT uintptr_t _Unwind_GetIP(struct _Unwind_Context *context) {
489 unw_cursor_t *cursor = (unw_cursor_t *)context;
490 unw_word_t result;
491 unw_get_reg(cursor, UNW_REG_IP, &result);
Martin Storsjo97e2d982017-10-30 19:06:34 +0000492 _LIBUNWIND_TRACE_API("_Unwind_GetIP(context=%p) => 0x%" PRIxPTR,
493 (void *)context, result);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000494 return (uintptr_t)result;
495}
496
497/// Called by personality handler during phase 2 to alter instruction pointer,
498/// such as setting where the landing pad is, so _Unwind_Resume() will
499/// start executing in the landing pad.
500_LIBUNWIND_EXPORT void _Unwind_SetIP(struct _Unwind_Context *context,
501 uintptr_t value) {
Martin Storsjo97e2d982017-10-30 19:06:34 +0000502 _LIBUNWIND_TRACE_API("_Unwind_SetIP(context=%p, value=0x%0" PRIxPTR ")",
503 (void *)context, value);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000504 unw_cursor_t *cursor = (unw_cursor_t *)context;
505 unw_set_reg(cursor, UNW_REG_IP, value);
506}
507
Martin Storsjo9f16c6c2017-09-26 08:07:26 +0000508#endif // !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)