Unwind-seh.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. //===--------------------------- Unwind-seh.cpp ---------------------------===//
  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. // Implements SEH-based Itanium C++ exceptions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "config.h"
  13. #if defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)
  14. #include <unwind.h>
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <stdlib.h>
  18. #include <windef.h>
  19. #include <excpt.h>
  20. #include <winnt.h>
  21. #include <ntstatus.h>
  22. #include "libunwind_ext.h"
  23. #include "UnwindCursor.hpp"
  24. using namespace libunwind;
  25. #define STATUS_USER_DEFINED (1u << 29)
  26. #define STATUS_GCC_MAGIC (('G' << 16) | ('C' << 8) | 'C')
  27. #define MAKE_CUSTOM_STATUS(s, c) \
  28. ((NTSTATUS)(((s) << 30) | STATUS_USER_DEFINED | (c)))
  29. #define MAKE_GCC_EXCEPTION(c) \
  30. MAKE_CUSTOM_STATUS(STATUS_SEVERITY_SUCCESS, STATUS_GCC_MAGIC | ((c) << 24))
  31. /// SEH exception raised by libunwind when the program calls
  32. /// \c _Unwind_RaiseException.
  33. #define STATUS_GCC_THROW MAKE_GCC_EXCEPTION(0) // 0x20474343
  34. /// SEH exception raised by libunwind to initiate phase 2 of exception
  35. /// handling.
  36. #define STATUS_GCC_UNWIND MAKE_GCC_EXCEPTION(1) // 0x21474343
  37. static int __unw_init_seh(unw_cursor_t *cursor, CONTEXT *ctx);
  38. static DISPATCHER_CONTEXT *__unw_seh_get_disp_ctx(unw_cursor_t *cursor);
  39. static void __unw_seh_set_disp_ctx(unw_cursor_t *cursor,
  40. DISPATCHER_CONTEXT *disp);
  41. /// Common implementation of SEH-style handler functions used by Itanium-
  42. /// style frames. Depending on how and why it was called, it may do one of:
  43. /// a) Delegate to the given Itanium-style personality function; or
  44. /// b) Initiate a collided unwind to halt unwinding.
  45. _LIBUNWIND_EXPORT EXCEPTION_DISPOSITION
  46. _GCC_specific_handler(PEXCEPTION_RECORD ms_exc, PVOID frame, PCONTEXT ms_ctx,
  47. DISPATCHER_CONTEXT *disp, _Unwind_Personality_Fn pers) {
  48. unw_cursor_t cursor;
  49. _Unwind_Exception *exc;
  50. _Unwind_Action action;
  51. struct _Unwind_Context *ctx = nullptr;
  52. _Unwind_Reason_Code urc;
  53. uintptr_t retval, target;
  54. bool ours = false;
  55. _LIBUNWIND_TRACE_UNWINDING("_GCC_specific_handler(%#010lx(%lx), %p)",
  56. ms_exc->ExceptionCode, ms_exc->ExceptionFlags,
  57. (void *)frame);
  58. if (ms_exc->ExceptionCode == STATUS_GCC_UNWIND) {
  59. if (IS_TARGET_UNWIND(ms_exc->ExceptionFlags)) {
  60. // Set up the upper return value (the lower one and the target PC
  61. // were set in the call to RtlUnwindEx()) for the landing pad.
  62. #ifdef __x86_64__
  63. disp->ContextRecord->Rdx = ms_exc->ExceptionInformation[3];
  64. #elif defined(__arm__)
  65. disp->ContextRecord->R1 = ms_exc->ExceptionInformation[3];
  66. #elif defined(__aarch64__)
  67. disp->ContextRecord->X1 = ms_exc->ExceptionInformation[3];
  68. #endif
  69. }
  70. // This is the collided unwind to the landing pad. Nothing to do.
  71. return ExceptionContinueSearch;
  72. }
  73. if (ms_exc->ExceptionCode == STATUS_GCC_THROW) {
  74. // This is (probably) a libunwind-controlled exception/unwind. Recover the
  75. // parameters which we set below, and pass them to the personality function.
  76. ours = true;
  77. exc = (_Unwind_Exception *)ms_exc->ExceptionInformation[0];
  78. if (!IS_UNWINDING(ms_exc->ExceptionFlags) && ms_exc->NumberParameters > 1) {
  79. ctx = (struct _Unwind_Context *)ms_exc->ExceptionInformation[1];
  80. action = (_Unwind_Action)ms_exc->ExceptionInformation[2];
  81. }
  82. } else {
  83. // Foreign exception.
  84. // We can't interact with them (we don't know the original target frame
  85. // that we should pass on to RtlUnwindEx in _Unwind_Resume), so just
  86. // pass without calling our destructors here.
  87. return ExceptionContinueSearch;
  88. }
  89. if (!ctx) {
  90. __unw_init_seh(&cursor, disp->ContextRecord);
  91. __unw_seh_set_disp_ctx(&cursor, disp);
  92. __unw_set_reg(&cursor, UNW_REG_IP, disp->ControlPc - 1);
  93. ctx = (struct _Unwind_Context *)&cursor;
  94. if (!IS_UNWINDING(ms_exc->ExceptionFlags)) {
  95. if (ours && ms_exc->NumberParameters > 1)
  96. action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_FORCE_UNWIND);
  97. else
  98. action = _UA_SEARCH_PHASE;
  99. } else {
  100. if (ours && ms_exc->ExceptionInformation[1] == (ULONG_PTR)frame)
  101. action = (_Unwind_Action)(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME);
  102. else
  103. action = _UA_CLEANUP_PHASE;
  104. }
  105. }
  106. _LIBUNWIND_TRACE_UNWINDING("_GCC_specific_handler() calling personality "
  107. "function %p(1, %d, %llx, %p, %p)",
  108. (void *)pers, action, exc->exception_class,
  109. (void *)exc, (void *)ctx);
  110. urc = pers(1, action, exc->exception_class, exc, ctx);
  111. _LIBUNWIND_TRACE_UNWINDING("_GCC_specific_handler() personality returned %d", urc);
  112. switch (urc) {
  113. case _URC_CONTINUE_UNWIND:
  114. // If we're in phase 2, and the personality routine said to continue
  115. // at the target frame, we're in real trouble.
  116. if (action & _UA_HANDLER_FRAME)
  117. _LIBUNWIND_ABORT("Personality continued unwind at the target frame!");
  118. return ExceptionContinueSearch;
  119. case _URC_HANDLER_FOUND:
  120. // If we were called by __libunwind_seh_personality(), indicate that
  121. // a handler was found; otherwise, initiate phase 2 by unwinding.
  122. if (ours && ms_exc->NumberParameters > 1)
  123. return 4 /* ExecptionExecuteHandler in mingw */;
  124. // This should never happen in phase 2.
  125. if (IS_UNWINDING(ms_exc->ExceptionFlags))
  126. _LIBUNWIND_ABORT("Personality indicated exception handler in phase 2!");
  127. exc->private_[1] = (ULONG_PTR)frame;
  128. if (ours) {
  129. ms_exc->NumberParameters = 4;
  130. ms_exc->ExceptionInformation[1] = (ULONG_PTR)frame;
  131. }
  132. // FIXME: Indicate target frame in foreign case!
  133. // phase 2: the clean up phase
  134. RtlUnwindEx(frame, (PVOID)disp->ControlPc, ms_exc, exc, ms_ctx, disp->HistoryTable);
  135. _LIBUNWIND_ABORT("RtlUnwindEx() failed");
  136. case _URC_INSTALL_CONTEXT: {
  137. // If we were called by __libunwind_seh_personality(), indicate that
  138. // a handler was found; otherwise, it's time to initiate a collided
  139. // unwind to the target.
  140. if (ours && !IS_UNWINDING(ms_exc->ExceptionFlags) && ms_exc->NumberParameters > 1)
  141. return 4 /* ExecptionExecuteHandler in mingw */;
  142. // This should never happen in phase 1.
  143. if (!IS_UNWINDING(ms_exc->ExceptionFlags))
  144. _LIBUNWIND_ABORT("Personality installed context during phase 1!");
  145. #ifdef __x86_64__
  146. exc->private_[2] = disp->TargetIp;
  147. __unw_get_reg(&cursor, UNW_X86_64_RAX, &retval);
  148. __unw_get_reg(&cursor, UNW_X86_64_RDX, &exc->private_[3]);
  149. #elif defined(__arm__)
  150. exc->private_[2] = disp->TargetPc;
  151. __unw_get_reg(&cursor, UNW_ARM_R0, &retval);
  152. __unw_get_reg(&cursor, UNW_ARM_R1, &exc->private_[3]);
  153. #elif defined(__aarch64__)
  154. exc->private_[2] = disp->TargetPc;
  155. __unw_get_reg(&cursor, UNW_ARM64_X0, &retval);
  156. __unw_get_reg(&cursor, UNW_ARM64_X1, &exc->private_[3]);
  157. #endif
  158. __unw_get_reg(&cursor, UNW_REG_IP, &target);
  159. ms_exc->ExceptionCode = STATUS_GCC_UNWIND;
  160. #ifdef __x86_64__
  161. ms_exc->ExceptionInformation[2] = disp->TargetIp;
  162. #elif defined(__arm__) || defined(__aarch64__)
  163. ms_exc->ExceptionInformation[2] = disp->TargetPc;
  164. #endif
  165. ms_exc->ExceptionInformation[3] = exc->private_[3];
  166. // Give NTRTL some scratch space to keep track of the collided unwind.
  167. // Don't use the one that was passed in; we don't want to overwrite the
  168. // context in the DISPATCHER_CONTEXT.
  169. CONTEXT new_ctx;
  170. RtlUnwindEx(frame, (PVOID)target, ms_exc, (PVOID)retval, &new_ctx, disp->HistoryTable);
  171. _LIBUNWIND_ABORT("RtlUnwindEx() failed");
  172. }
  173. // Anything else indicates a serious problem.
  174. default: return ExceptionContinueExecution;
  175. }
  176. }
  177. /// Personality function returned by \c __unw_get_proc_info() in SEH contexts.
  178. /// This is a wrapper that calls the real SEH handler function, which in
  179. /// turn (at least, for Itanium-style frames) calls the real Itanium
  180. /// personality function (see \c _GCC_specific_handler()).
  181. extern "C" _Unwind_Reason_Code
  182. __libunwind_seh_personality(int version, _Unwind_Action state,
  183. uint64_t klass, _Unwind_Exception *exc,
  184. struct _Unwind_Context *context) {
  185. (void)version;
  186. (void)klass;
  187. EXCEPTION_RECORD ms_exc;
  188. bool phase2 = (state & (_UA_SEARCH_PHASE|_UA_CLEANUP_PHASE)) == _UA_CLEANUP_PHASE;
  189. ms_exc.ExceptionCode = STATUS_GCC_THROW;
  190. ms_exc.ExceptionFlags = 0;
  191. ms_exc.NumberParameters = 3;
  192. ms_exc.ExceptionInformation[0] = (ULONG_PTR)exc;
  193. ms_exc.ExceptionInformation[1] = (ULONG_PTR)context;
  194. ms_exc.ExceptionInformation[2] = state;
  195. DISPATCHER_CONTEXT *disp_ctx =
  196. __unw_seh_get_disp_ctx((unw_cursor_t *)context);
  197. EXCEPTION_DISPOSITION ms_act = disp_ctx->LanguageHandler(&ms_exc,
  198. (PVOID)disp_ctx->EstablisherFrame,
  199. disp_ctx->ContextRecord,
  200. disp_ctx);
  201. switch (ms_act) {
  202. case ExceptionContinueSearch: return _URC_CONTINUE_UNWIND;
  203. case 4 /*ExceptionExecuteHandler*/:
  204. return phase2 ? _URC_INSTALL_CONTEXT : _URC_HANDLER_FOUND;
  205. default:
  206. return phase2 ? _URC_FATAL_PHASE2_ERROR : _URC_FATAL_PHASE1_ERROR;
  207. }
  208. }
  209. static _Unwind_Reason_Code
  210. unwind_phase2_forced(unw_context_t *uc,
  211. _Unwind_Exception *exception_object,
  212. _Unwind_Stop_Fn stop, void *stop_parameter) {
  213. unw_cursor_t cursor2;
  214. __unw_init_local(&cursor2, uc);
  215. // Walk each frame until we reach where search phase said to stop
  216. while (__unw_step(&cursor2) > 0) {
  217. // Update info about this frame.
  218. unw_proc_info_t frameInfo;
  219. if (__unw_get_proc_info(&cursor2, &frameInfo) != UNW_ESUCCESS) {
  220. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step "
  221. "failed => _URC_END_OF_STACK",
  222. (void *)exception_object);
  223. return _URC_FATAL_PHASE2_ERROR;
  224. }
  225. // When tracing, print state information.
  226. if (_LIBUNWIND_TRACING_UNWINDING) {
  227. char functionBuf[512];
  228. const char *functionName = functionBuf;
  229. unw_word_t offset;
  230. if ((__unw_get_proc_name(&cursor2, functionBuf, sizeof(functionBuf),
  231. &offset) != UNW_ESUCCESS) ||
  232. (frameInfo.start_ip + offset > frameInfo.end_ip))
  233. functionName = ".anonymous.";
  234. _LIBUNWIND_TRACE_UNWINDING(
  235. "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIx64
  236. ", func=%s, lsda=0x%" PRIx64 ", personality=0x%" PRIx64,
  237. (void *)exception_object, frameInfo.start_ip, functionName,
  238. frameInfo.lsda, frameInfo.handler);
  239. }
  240. // Call stop function at each frame.
  241. _Unwind_Action action =
  242. (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
  243. _Unwind_Reason_Code stopResult =
  244. (*stop)(1, action, exception_object->exception_class, exception_object,
  245. (struct _Unwind_Context *)(&cursor2), stop_parameter);
  246. _LIBUNWIND_TRACE_UNWINDING(
  247. "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
  248. (void *)exception_object, stopResult);
  249. if (stopResult != _URC_NO_REASON) {
  250. _LIBUNWIND_TRACE_UNWINDING(
  251. "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
  252. (void *)exception_object);
  253. return _URC_FATAL_PHASE2_ERROR;
  254. }
  255. // If there is a personality routine, tell it we are unwinding.
  256. if (frameInfo.handler != 0) {
  257. _Unwind_Personality_Fn p =
  258. (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
  259. _LIBUNWIND_TRACE_UNWINDING(
  260. "unwind_phase2_forced(ex_ojb=%p): calling personality function %p",
  261. (void *)exception_object, (void *)(uintptr_t)p);
  262. _Unwind_Reason_Code personalityResult =
  263. (*p)(1, action, exception_object->exception_class, exception_object,
  264. (struct _Unwind_Context *)(&cursor2));
  265. switch (personalityResult) {
  266. case _URC_CONTINUE_UNWIND:
  267. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
  268. "personality returned "
  269. "_URC_CONTINUE_UNWIND",
  270. (void *)exception_object);
  271. // Destructors called, continue unwinding
  272. break;
  273. case _URC_INSTALL_CONTEXT:
  274. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
  275. "personality returned "
  276. "_URC_INSTALL_CONTEXT",
  277. (void *)exception_object);
  278. // We may get control back if landing pad calls _Unwind_Resume().
  279. __unw_resume(&cursor2);
  280. break;
  281. default:
  282. // Personality routine returned an unknown result code.
  283. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
  284. "personality returned %d, "
  285. "_URC_FATAL_PHASE2_ERROR",
  286. (void *)exception_object, personalityResult);
  287. return _URC_FATAL_PHASE2_ERROR;
  288. }
  289. }
  290. }
  291. // Call stop function one last time and tell it we've reached the end
  292. // of the stack.
  293. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
  294. "function with _UA_END_OF_STACK",
  295. (void *)exception_object);
  296. _Unwind_Action lastAction =
  297. (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
  298. (*stop)(1, lastAction, exception_object->exception_class, exception_object,
  299. (struct _Unwind_Context *)(&cursor2), stop_parameter);
  300. // Clean up phase did not resume at the frame that the search phase said it
  301. // would.
  302. return _URC_FATAL_PHASE2_ERROR;
  303. }
  304. /// Called by \c __cxa_throw(). Only returns if there is a fatal error.
  305. _LIBUNWIND_EXPORT _Unwind_Reason_Code
  306. _Unwind_RaiseException(_Unwind_Exception *exception_object) {
  307. _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
  308. (void *)exception_object);
  309. // Mark that this is a non-forced unwind, so _Unwind_Resume()
  310. // can do the right thing.
  311. memset(exception_object->private_, 0, sizeof(exception_object->private_));
  312. // phase 1: the search phase
  313. // We'll let the system do that for us.
  314. RaiseException(STATUS_GCC_THROW, 0, 1, (ULONG_PTR *)&exception_object);
  315. // If we get here, either something went horribly wrong or we reached the
  316. // top of the stack. Either way, let libc++abi call std::terminate().
  317. return _URC_END_OF_STACK;
  318. }
  319. /// When \c _Unwind_RaiseException() is in phase2, it hands control
  320. /// to the personality function at each frame. The personality
  321. /// may force a jump to a landing pad in that function; the landing
  322. /// pad code may then call \c _Unwind_Resume() to continue with the
  323. /// unwinding. Note: the call to \c _Unwind_Resume() is from compiler
  324. /// geneated user code. All other \c _Unwind_* routines are called
  325. /// by the C++ runtime \c __cxa_* routines.
  326. ///
  327. /// Note: re-throwing an exception (as opposed to continuing the unwind)
  328. /// is implemented by having the code call \c __cxa_rethrow() which
  329. /// in turn calls \c _Unwind_Resume_or_Rethrow().
  330. _LIBUNWIND_EXPORT void
  331. _Unwind_Resume(_Unwind_Exception *exception_object) {
  332. _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", (void *)exception_object);
  333. if (exception_object->private_[0] != 0) {
  334. unw_context_t uc;
  335. __unw_getcontext(&uc);
  336. unwind_phase2_forced(&uc, exception_object,
  337. (_Unwind_Stop_Fn) exception_object->private_[0],
  338. (void *)exception_object->private_[4]);
  339. } else {
  340. // Recover the parameters for the unwind from the exception object
  341. // so we can start unwinding again.
  342. EXCEPTION_RECORD ms_exc;
  343. CONTEXT ms_ctx;
  344. UNWIND_HISTORY_TABLE hist;
  345. memset(&ms_exc, 0, sizeof(ms_exc));
  346. memset(&hist, 0, sizeof(hist));
  347. ms_exc.ExceptionCode = STATUS_GCC_THROW;
  348. ms_exc.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
  349. ms_exc.NumberParameters = 4;
  350. ms_exc.ExceptionInformation[0] = (ULONG_PTR)exception_object;
  351. ms_exc.ExceptionInformation[1] = exception_object->private_[1];
  352. ms_exc.ExceptionInformation[2] = exception_object->private_[2];
  353. ms_exc.ExceptionInformation[3] = exception_object->private_[3];
  354. RtlUnwindEx((PVOID)exception_object->private_[1],
  355. (PVOID)exception_object->private_[2], &ms_exc,
  356. exception_object, &ms_ctx, &hist);
  357. }
  358. // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
  359. _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
  360. }
  361. /// Not used by C++.
  362. /// Unwinds stack, calling "stop" function at each frame.
  363. /// Could be used to implement \c longjmp().
  364. _LIBUNWIND_EXPORT _Unwind_Reason_Code
  365. _Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
  366. _Unwind_Stop_Fn stop, void *stop_parameter) {
  367. _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
  368. (void *)exception_object, (void *)(uintptr_t)stop);
  369. unw_context_t uc;
  370. __unw_getcontext(&uc);
  371. // Mark that this is a forced unwind, so _Unwind_Resume() can do
  372. // the right thing.
  373. exception_object->private_[0] = (uintptr_t) stop;
  374. exception_object->private_[4] = (uintptr_t) stop_parameter;
  375. // do it
  376. return unwind_phase2_forced(&uc, exception_object, stop, stop_parameter);
  377. }
  378. /// Called by personality handler during phase 2 to get LSDA for current frame.
  379. _LIBUNWIND_EXPORT uintptr_t
  380. _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
  381. uintptr_t result =
  382. (uintptr_t)__unw_seh_get_disp_ctx((unw_cursor_t *)context)->HandlerData;
  383. _LIBUNWIND_TRACE_API(
  384. "_Unwind_GetLanguageSpecificData(context=%p) => 0x%" PRIxPTR,
  385. (void *)context, result);
  386. return result;
  387. }
  388. /// Called by personality handler during phase 2 to find the start of the
  389. /// function.
  390. _LIBUNWIND_EXPORT uintptr_t
  391. _Unwind_GetRegionStart(struct _Unwind_Context *context) {
  392. DISPATCHER_CONTEXT *disp = __unw_seh_get_disp_ctx((unw_cursor_t *)context);
  393. uintptr_t result = (uintptr_t)disp->FunctionEntry->BeginAddress + disp->ImageBase;
  394. _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%" PRIxPTR,
  395. (void *)context, result);
  396. return result;
  397. }
  398. static int __unw_init_seh(unw_cursor_t *cursor, CONTEXT *context) {
  399. #ifdef _LIBUNWIND_TARGET_X86_64
  400. new (reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_x86_64> *>(cursor))
  401. UnwindCursor<LocalAddressSpace, Registers_x86_64>(
  402. context, LocalAddressSpace::sThisAddressSpace);
  403. auto *co = reinterpret_cast<AbstractUnwindCursor *>(cursor);
  404. co->setInfoBasedOnIPRegister();
  405. return UNW_ESUCCESS;
  406. #elif defined(_LIBUNWIND_TARGET_ARM)
  407. new (reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm> *>(cursor))
  408. UnwindCursor<LocalAddressSpace, Registers_arm>(
  409. context, LocalAddressSpace::sThisAddressSpace);
  410. auto *co = reinterpret_cast<AbstractUnwindCursor *>(cursor);
  411. co->setInfoBasedOnIPRegister();
  412. return UNW_ESUCCESS;
  413. #elif defined(_LIBUNWIND_TARGET_AARCH64)
  414. new (reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm64> *>(cursor))
  415. UnwindCursor<LocalAddressSpace, Registers_arm64>(
  416. context, LocalAddressSpace::sThisAddressSpace);
  417. auto *co = reinterpret_cast<AbstractUnwindCursor *>(cursor);
  418. co->setInfoBasedOnIPRegister();
  419. return UNW_ESUCCESS;
  420. #else
  421. return UNW_EINVAL;
  422. #endif
  423. }
  424. static DISPATCHER_CONTEXT *__unw_seh_get_disp_ctx(unw_cursor_t *cursor) {
  425. #ifdef _LIBUNWIND_TARGET_X86_64
  426. return reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_x86_64> *>(cursor)->getDispatcherContext();
  427. #elif defined(_LIBUNWIND_TARGET_ARM)
  428. return reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm> *>(cursor)->getDispatcherContext();
  429. #elif defined(_LIBUNWIND_TARGET_AARCH64)
  430. return reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm64> *>(cursor)->getDispatcherContext();
  431. #else
  432. return nullptr;
  433. #endif
  434. }
  435. static void __unw_seh_set_disp_ctx(unw_cursor_t *cursor,
  436. DISPATCHER_CONTEXT *disp) {
  437. #ifdef _LIBUNWIND_TARGET_X86_64
  438. reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_x86_64> *>(cursor)->setDispatcherContext(disp);
  439. #elif defined(_LIBUNWIND_TARGET_ARM)
  440. reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm> *>(cursor)->setDispatcherContext(disp);
  441. #elif defined(_LIBUNWIND_TARGET_AARCH64)
  442. reinterpret_cast<UnwindCursor<LocalAddressSpace, Registers_arm64> *>(cursor)->setDispatcherContext(disp);
  443. #endif
  444. }
  445. #endif // defined(_LIBUNWIND_SUPPORT_SEH_UNWIND)