Unwind-EHABI.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. //===----------------------------------------------------------------------===//
  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. // Implements ARM zero-cost C++ exceptions
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #include "Unwind-EHABI.h"
  12. #if defined(_LIBUNWIND_ARM_EHABI)
  13. #include <inttypes.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "config.h"
  20. #include "libunwind.h"
  21. #include "libunwind_ext.h"
  22. #include "unwind.h"
  23. namespace {
  24. // Strange order: take words in order, but inside word, take from most to least
  25. // signinficant byte.
  26. uint8_t getByte(const uint32_t* data, size_t offset) {
  27. const uint8_t* byteData = reinterpret_cast<const uint8_t*>(data);
  28. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  29. return byteData[(offset & ~(size_t)0x03) + (3 - (offset & (size_t)0x03))];
  30. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  31. return byteData[offset];
  32. #else
  33. #error "Unable to determine endianess"
  34. #endif
  35. }
  36. const char* getNextWord(const char* data, uint32_t* out) {
  37. *out = *reinterpret_cast<const uint32_t*>(data);
  38. return data + 4;
  39. }
  40. const char* getNextNibble(const char* data, uint32_t* out) {
  41. *out = *reinterpret_cast<const uint16_t*>(data);
  42. return data + 2;
  43. }
  44. struct Descriptor {
  45. // See # 9.2
  46. typedef enum {
  47. SU16 = 0, // Short descriptor, 16-bit entries
  48. LU16 = 1, // Long descriptor, 16-bit entries
  49. LU32 = 3, // Long descriptor, 32-bit entries
  50. RESERVED0 = 4, RESERVED1 = 5, RESERVED2 = 6, RESERVED3 = 7,
  51. RESERVED4 = 8, RESERVED5 = 9, RESERVED6 = 10, RESERVED7 = 11,
  52. RESERVED8 = 12, RESERVED9 = 13, RESERVED10 = 14, RESERVED11 = 15
  53. } Format;
  54. // See # 9.2
  55. typedef enum {
  56. CLEANUP = 0x0,
  57. FUNC = 0x1,
  58. CATCH = 0x2,
  59. INVALID = 0x4
  60. } Kind;
  61. };
  62. _Unwind_Reason_Code ProcessDescriptors(
  63. _Unwind_State state,
  64. _Unwind_Control_Block* ucbp,
  65. struct _Unwind_Context* context,
  66. Descriptor::Format format,
  67. const char* descriptorStart,
  68. uint32_t flags) {
  69. // EHT is inlined in the index using compact form. No descriptors. #5
  70. if (flags & 0x1)
  71. return _URC_CONTINUE_UNWIND;
  72. // TODO: We should check the state here, and determine whether we need to
  73. // perform phase1 or phase2 unwinding.
  74. (void)state;
  75. const char* descriptor = descriptorStart;
  76. uint32_t descriptorWord;
  77. getNextWord(descriptor, &descriptorWord);
  78. while (descriptorWord) {
  79. // Read descriptor based on # 9.2.
  80. uint32_t length;
  81. uint32_t offset;
  82. switch (format) {
  83. case Descriptor::LU32:
  84. descriptor = getNextWord(descriptor, &length);
  85. descriptor = getNextWord(descriptor, &offset);
  86. break;
  87. case Descriptor::LU16:
  88. descriptor = getNextNibble(descriptor, &length);
  89. descriptor = getNextNibble(descriptor, &offset);
  90. break;
  91. default:
  92. assert(false);
  93. return _URC_FAILURE;
  94. }
  95. // See # 9.2 table for decoding the kind of descriptor. It's a 2-bit value.
  96. Descriptor::Kind kind =
  97. static_cast<Descriptor::Kind>((length & 0x1) | ((offset & 0x1) << 1));
  98. // Clear off flag from last bit.
  99. length &= ~1u;
  100. offset &= ~1u;
  101. uintptr_t scopeStart = ucbp->pr_cache.fnstart + offset;
  102. uintptr_t scopeEnd = scopeStart + length;
  103. uintptr_t pc = _Unwind_GetIP(context);
  104. bool isInScope = (scopeStart <= pc) && (pc < scopeEnd);
  105. switch (kind) {
  106. case Descriptor::CLEANUP: {
  107. // TODO(ajwong): Handle cleanup descriptors.
  108. break;
  109. }
  110. case Descriptor::FUNC: {
  111. // TODO(ajwong): Handle function descriptors.
  112. break;
  113. }
  114. case Descriptor::CATCH: {
  115. // Catch descriptors require gobbling one more word.
  116. uint32_t landing_pad;
  117. descriptor = getNextWord(descriptor, &landing_pad);
  118. if (isInScope) {
  119. // TODO(ajwong): This is only phase1 compatible logic. Implement
  120. // phase2.
  121. landing_pad = signExtendPrel31(landing_pad & ~0x80000000);
  122. if (landing_pad == 0xffffffff) {
  123. return _URC_HANDLER_FOUND;
  124. } else if (landing_pad == 0xfffffffe) {
  125. return _URC_FAILURE;
  126. } else {
  127. /*
  128. bool is_reference_type = landing_pad & 0x80000000;
  129. void* matched_object;
  130. if (__cxxabiv1::__cxa_type_match(
  131. ucbp, reinterpret_cast<const std::type_info *>(landing_pad),
  132. is_reference_type,
  133. &matched_object) != __cxxabiv1::ctm_failed)
  134. return _URC_HANDLER_FOUND;
  135. */
  136. _LIBUNWIND_ABORT("Type matching not implemented");
  137. }
  138. }
  139. break;
  140. }
  141. default:
  142. _LIBUNWIND_ABORT("Invalid descriptor kind found.");
  143. }
  144. getNextWord(descriptor, &descriptorWord);
  145. }
  146. return _URC_CONTINUE_UNWIND;
  147. }
  148. static _Unwind_Reason_Code unwindOneFrame(_Unwind_State state,
  149. _Unwind_Control_Block* ucbp,
  150. struct _Unwind_Context* context) {
  151. // Read the compact model EHT entry's header # 6.3
  152. const uint32_t* unwindingData = ucbp->pr_cache.ehtp;
  153. assert((*unwindingData & 0xf0000000) == 0x80000000 && "Must be a compact entry");
  154. Descriptor::Format format =
  155. static_cast<Descriptor::Format>((*unwindingData & 0x0f000000) >> 24);
  156. const char *lsda =
  157. reinterpret_cast<const char *>(_Unwind_GetLanguageSpecificData(context));
  158. // Handle descriptors before unwinding so they are processed in the context
  159. // of the correct stack frame.
  160. _Unwind_Reason_Code result =
  161. ProcessDescriptors(state, ucbp, context, format, lsda,
  162. ucbp->pr_cache.additional);
  163. if (result != _URC_CONTINUE_UNWIND)
  164. return result;
  165. switch (__unw_step(reinterpret_cast<unw_cursor_t *>(context))) {
  166. case UNW_STEP_SUCCESS:
  167. return _URC_CONTINUE_UNWIND;
  168. case UNW_STEP_END:
  169. return _URC_END_OF_STACK;
  170. default:
  171. return _URC_FAILURE;
  172. }
  173. }
  174. // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_CORE /
  175. // _UVRSD_UINT32.
  176. uint32_t RegisterMask(uint8_t start, uint8_t count_minus_one) {
  177. return ((1U << (count_minus_one + 1)) - 1) << start;
  178. }
  179. // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_VFP /
  180. // _UVRSD_DOUBLE.
  181. uint32_t RegisterRange(uint8_t start, uint8_t count_minus_one) {
  182. return ((uint32_t)start << 16) | ((uint32_t)count_minus_one + 1);
  183. }
  184. } // end anonymous namespace
  185. /**
  186. * Decodes an EHT entry.
  187. *
  188. * @param data Pointer to EHT.
  189. * @param[out] off Offset from return value (in bytes) to begin interpretation.
  190. * @param[out] len Number of bytes in unwind code.
  191. * @return Pointer to beginning of unwind code.
  192. */
  193. extern "C" const uint32_t*
  194. decode_eht_entry(const uint32_t* data, size_t* off, size_t* len) {
  195. if ((*data & 0x80000000) == 0) {
  196. // 6.2: Generic Model
  197. //
  198. // EHT entry is a prel31 pointing to the PR, followed by data understood
  199. // only by the personality routine. Fortunately, all existing assembler
  200. // implementations, including GNU assembler, LLVM integrated assembler,
  201. // and ARM assembler, assume that the unwind opcodes come after the
  202. // personality rountine address.
  203. *off = 1; // First byte is size data.
  204. *len = (((data[1] >> 24) & 0xff) + 1) * 4;
  205. data++; // Skip the first word, which is the prel31 offset.
  206. } else {
  207. // 6.3: ARM Compact Model
  208. //
  209. // EHT entries here correspond to the __aeabi_unwind_cpp_pr[012] PRs indeded
  210. // by format:
  211. Descriptor::Format format =
  212. static_cast<Descriptor::Format>((*data & 0x0f000000) >> 24);
  213. switch (format) {
  214. case Descriptor::SU16:
  215. *len = 4;
  216. *off = 1;
  217. break;
  218. case Descriptor::LU16:
  219. case Descriptor::LU32:
  220. *len = 4 + 4 * ((*data & 0x00ff0000) >> 16);
  221. *off = 2;
  222. break;
  223. default:
  224. return nullptr;
  225. }
  226. }
  227. return data;
  228. }
  229. _LIBUNWIND_EXPORT _Unwind_Reason_Code
  230. _Unwind_VRS_Interpret(_Unwind_Context *context, const uint32_t *data,
  231. size_t offset, size_t len) {
  232. bool wrotePC = false;
  233. bool finish = false;
  234. bool hasReturnAddrAuthCode [[maybe_unused]] = false;
  235. while (offset < len && !finish) {
  236. uint8_t byte = getByte(data, offset++);
  237. if ((byte & 0x80) == 0) {
  238. uint32_t sp;
  239. _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
  240. if (byte & 0x40)
  241. sp -= (((uint32_t)byte & 0x3f) << 2) + 4;
  242. else
  243. sp += ((uint32_t)byte << 2) + 4;
  244. _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
  245. } else {
  246. switch (byte & 0xf0) {
  247. case 0x80: {
  248. if (offset >= len)
  249. return _URC_FAILURE;
  250. uint32_t registers =
  251. (((uint32_t)byte & 0x0f) << 12) |
  252. (((uint32_t)getByte(data, offset++)) << 4);
  253. if (!registers)
  254. return _URC_FAILURE;
  255. if (registers & (1 << 15))
  256. wrotePC = true;
  257. _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
  258. break;
  259. }
  260. case 0x90: {
  261. uint8_t reg = byte & 0x0f;
  262. if (reg == 13 || reg == 15)
  263. return _URC_FAILURE;
  264. uint32_t sp;
  265. _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_R0 + reg,
  266. _UVRSD_UINT32, &sp);
  267. _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
  268. &sp);
  269. break;
  270. }
  271. case 0xa0: {
  272. uint32_t registers = RegisterMask(4, byte & 0x07);
  273. if (byte & 0x08)
  274. registers |= 1 << 14;
  275. _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
  276. break;
  277. }
  278. case 0xb0: {
  279. switch (byte) {
  280. case 0xb0:
  281. finish = true;
  282. break;
  283. case 0xb1: {
  284. if (offset >= len)
  285. return _URC_FAILURE;
  286. uint8_t registers = getByte(data, offset++);
  287. if (registers & 0xf0 || !registers)
  288. return _URC_FAILURE;
  289. _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
  290. break;
  291. }
  292. case 0xb2: {
  293. uint32_t addend = 0;
  294. uint32_t shift = 0;
  295. // This decodes a uleb128 value.
  296. while (true) {
  297. if (offset >= len)
  298. return _URC_FAILURE;
  299. uint32_t v = getByte(data, offset++);
  300. addend |= (v & 0x7f) << shift;
  301. if ((v & 0x80) == 0)
  302. break;
  303. shift += 7;
  304. }
  305. uint32_t sp;
  306. _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
  307. &sp);
  308. sp += 0x204 + (addend << 2);
  309. _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
  310. &sp);
  311. break;
  312. }
  313. case 0xb3: {
  314. uint8_t v = getByte(data, offset++);
  315. _Unwind_VRS_Pop(context, _UVRSC_VFP,
  316. RegisterRange(static_cast<uint8_t>(v >> 4),
  317. v & 0x0f), _UVRSD_VFPX);
  318. break;
  319. }
  320. case 0xb4:
  321. hasReturnAddrAuthCode = true;
  322. _Unwind_VRS_Pop(context, _UVRSC_PSEUDO,
  323. 0 /* Return Address Auth Code */, _UVRSD_UINT32);
  324. break;
  325. case 0xb5:
  326. case 0xb6:
  327. case 0xb7:
  328. return _URC_FAILURE;
  329. default:
  330. _Unwind_VRS_Pop(context, _UVRSC_VFP,
  331. RegisterRange(8, byte & 0x07), _UVRSD_VFPX);
  332. break;
  333. }
  334. break;
  335. }
  336. case 0xc0: {
  337. switch (byte) {
  338. #if defined(__ARM_WMMX)
  339. case 0xc0:
  340. case 0xc1:
  341. case 0xc2:
  342. case 0xc3:
  343. case 0xc4:
  344. case 0xc5:
  345. _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
  346. RegisterRange(10, byte & 0x7), _UVRSD_DOUBLE);
  347. break;
  348. case 0xc6: {
  349. uint8_t v = getByte(data, offset++);
  350. uint8_t start = static_cast<uint8_t>(v >> 4);
  351. uint8_t count_minus_one = v & 0xf;
  352. if (start + count_minus_one >= 16)
  353. return _URC_FAILURE;
  354. _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
  355. RegisterRange(start, count_minus_one),
  356. _UVRSD_DOUBLE);
  357. break;
  358. }
  359. case 0xc7: {
  360. uint8_t v = getByte(data, offset++);
  361. if (!v || v & 0xf0)
  362. return _URC_FAILURE;
  363. _Unwind_VRS_Pop(context, _UVRSC_WMMXC, v, _UVRSD_DOUBLE);
  364. break;
  365. }
  366. #endif
  367. case 0xc8:
  368. case 0xc9: {
  369. uint8_t v = getByte(data, offset++);
  370. uint8_t start =
  371. static_cast<uint8_t>(((byte == 0xc8) ? 16 : 0) + (v >> 4));
  372. uint8_t count_minus_one = v & 0xf;
  373. if (start + count_minus_one >= 32)
  374. return _URC_FAILURE;
  375. _Unwind_VRS_Pop(context, _UVRSC_VFP,
  376. RegisterRange(start, count_minus_one),
  377. _UVRSD_DOUBLE);
  378. break;
  379. }
  380. default:
  381. return _URC_FAILURE;
  382. }
  383. break;
  384. }
  385. case 0xd0: {
  386. if (byte & 0x08)
  387. return _URC_FAILURE;
  388. _Unwind_VRS_Pop(context, _UVRSC_VFP, RegisterRange(8, byte & 0x7),
  389. _UVRSD_DOUBLE);
  390. break;
  391. }
  392. default:
  393. return _URC_FAILURE;
  394. }
  395. }
  396. }
  397. if (!wrotePC) {
  398. uint32_t lr;
  399. _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_LR, _UVRSD_UINT32, &lr);
  400. #ifdef __ARM_FEATURE_PAUTH
  401. if (hasReturnAddrAuthCode) {
  402. uint32_t sp;
  403. uint32_t pac;
  404. _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
  405. _Unwind_VRS_Get(context, _UVRSC_PSEUDO, UNW_ARM_RA_AUTH_CODE,
  406. _UVRSD_UINT32, &pac);
  407. __asm__ __volatile__("autg %0, %1, %2" : : "r"(pac), "r"(lr), "r"(sp) :);
  408. }
  409. #endif
  410. _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_IP, _UVRSD_UINT32, &lr);
  411. }
  412. return _URC_CONTINUE_UNWIND;
  413. }
  414. extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
  415. __aeabi_unwind_cpp_pr0(_Unwind_State state, _Unwind_Control_Block *ucbp,
  416. _Unwind_Context *context) {
  417. return unwindOneFrame(state, ucbp, context);
  418. }
  419. extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
  420. __aeabi_unwind_cpp_pr1(_Unwind_State state, _Unwind_Control_Block *ucbp,
  421. _Unwind_Context *context) {
  422. return unwindOneFrame(state, ucbp, context);
  423. }
  424. extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
  425. __aeabi_unwind_cpp_pr2(_Unwind_State state, _Unwind_Control_Block *ucbp,
  426. _Unwind_Context *context) {
  427. return unwindOneFrame(state, ucbp, context);
  428. }
  429. static _Unwind_Reason_Code
  430. unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
  431. // EHABI #7.3 discusses preserving the VRS in a "temporary VRS" during
  432. // phase 1 and then restoring it to the "primary VRS" for phase 2. The
  433. // effect is phase 2 doesn't see any of the VRS manipulations from phase 1.
  434. // In this implementation, the phases don't share the VRS backing store.
  435. // Instead, they are passed the original |uc| and they create a new VRS
  436. // from scratch thus achieving the same effect.
  437. __unw_init_local(cursor, uc);
  438. // Walk each frame looking for a place to stop.
  439. for (bool handlerNotFound = true; handlerNotFound;) {
  440. // See if frame has code to run (has personality routine).
  441. unw_proc_info_t frameInfo;
  442. if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
  443. _LIBUNWIND_TRACE_UNWINDING(
  444. "unwind_phase1(ex_ojb=%p): __unw_get_proc_info "
  445. "failed => _URC_FATAL_PHASE1_ERROR",
  446. static_cast<void *>(exception_object));
  447. return _URC_FATAL_PHASE1_ERROR;
  448. }
  449. #ifndef NDEBUG
  450. // When tracing, print state information.
  451. if (_LIBUNWIND_TRACING_UNWINDING) {
  452. char functionBuf[512];
  453. const char *functionName = functionBuf;
  454. unw_word_t offset;
  455. if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
  456. &offset) != UNW_ESUCCESS) ||
  457. (frameInfo.start_ip + offset > frameInfo.end_ip))
  458. functionName = ".anonymous.";
  459. unw_word_t pc;
  460. __unw_get_reg(cursor, UNW_REG_IP, &pc);
  461. _LIBUNWIND_TRACE_UNWINDING(
  462. "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR ", func=%s, "
  463. "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
  464. static_cast<void *>(exception_object), pc,
  465. frameInfo.start_ip, functionName,
  466. frameInfo.lsda, frameInfo.handler);
  467. }
  468. #endif
  469. // If there is a personality routine, ask it if it will want to stop at
  470. // this frame.
  471. if (frameInfo.handler != 0) {
  472. _Unwind_Personality_Fn p =
  473. (_Unwind_Personality_Fn)(long)(frameInfo.handler);
  474. _LIBUNWIND_TRACE_UNWINDING(
  475. "unwind_phase1(ex_ojb=%p): calling personality function %p",
  476. static_cast<void *>(exception_object),
  477. reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(p)));
  478. struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
  479. exception_object->pr_cache.fnstart = frameInfo.start_ip;
  480. exception_object->pr_cache.ehtp =
  481. (_Unwind_EHT_Header *)frameInfo.unwind_info;
  482. exception_object->pr_cache.additional = frameInfo.flags;
  483. _Unwind_Reason_Code personalityResult =
  484. (*p)(_US_VIRTUAL_UNWIND_FRAME, exception_object, context);
  485. _LIBUNWIND_TRACE_UNWINDING(
  486. "unwind_phase1(ex_ojb=%p): personality result %d start_ip %x ehtp %p "
  487. "additional %x",
  488. static_cast<void *>(exception_object), personalityResult,
  489. exception_object->pr_cache.fnstart,
  490. static_cast<void *>(exception_object->pr_cache.ehtp),
  491. exception_object->pr_cache.additional);
  492. switch (personalityResult) {
  493. case _URC_HANDLER_FOUND:
  494. // found a catch clause or locals that need destructing in this frame
  495. // stop search and remember stack pointer at the frame
  496. handlerNotFound = false;
  497. // p should have initialized barrier_cache. EHABI #7.3.5
  498. _LIBUNWIND_TRACE_UNWINDING(
  499. "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
  500. static_cast<void *>(exception_object));
  501. return _URC_NO_REASON;
  502. case _URC_CONTINUE_UNWIND:
  503. _LIBUNWIND_TRACE_UNWINDING(
  504. "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
  505. static_cast<void *>(exception_object));
  506. // continue unwinding
  507. break;
  508. // EHABI #7.3.3
  509. case _URC_FAILURE:
  510. return _URC_FAILURE;
  511. default:
  512. // something went wrong
  513. _LIBUNWIND_TRACE_UNWINDING(
  514. "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
  515. static_cast<void *>(exception_object));
  516. return _URC_FATAL_PHASE1_ERROR;
  517. }
  518. }
  519. }
  520. return _URC_NO_REASON;
  521. }
  522. static _Unwind_Reason_Code unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor,
  523. _Unwind_Exception *exception_object,
  524. bool resume) {
  525. // See comment at the start of unwind_phase1 regarding VRS integrity.
  526. __unw_init_local(cursor, uc);
  527. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
  528. static_cast<void *>(exception_object));
  529. int frame_count = 0;
  530. // Walk each frame until we reach where search phase said to stop.
  531. while (true) {
  532. // Ask libunwind to get next frame (skip over first which is
  533. // _Unwind_RaiseException or _Unwind_Resume).
  534. //
  535. // Resume only ever makes sense for 1 frame.
  536. _Unwind_State state =
  537. resume ? _US_UNWIND_FRAME_RESUME : _US_UNWIND_FRAME_STARTING;
  538. if (resume && frame_count == 1) {
  539. // On a resume, first unwind the _Unwind_Resume() frame. The next frame
  540. // is now the landing pad for the cleanup from a previous execution of
  541. // phase2. To continue unwindingly correctly, replace VRS[15] with the
  542. // IP of the frame that the previous run of phase2 installed the context
  543. // for. After this, continue unwinding as if normal.
  544. //
  545. // See #7.4.6 for details.
  546. __unw_set_reg(cursor, UNW_REG_IP,
  547. exception_object->unwinder_cache.reserved2);
  548. resume = false;
  549. }
  550. // Get info about this frame.
  551. unw_word_t sp;
  552. unw_proc_info_t frameInfo;
  553. __unw_get_reg(cursor, UNW_REG_SP, &sp);
  554. if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
  555. _LIBUNWIND_TRACE_UNWINDING(
  556. "unwind_phase2(ex_ojb=%p): __unw_get_proc_info "
  557. "failed => _URC_FATAL_PHASE2_ERROR",
  558. static_cast<void *>(exception_object));
  559. return _URC_FATAL_PHASE2_ERROR;
  560. }
  561. #ifndef NDEBUG
  562. // When tracing, print state information.
  563. if (_LIBUNWIND_TRACING_UNWINDING) {
  564. char functionBuf[512];
  565. const char *functionName = functionBuf;
  566. unw_word_t offset;
  567. if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
  568. &offset) != UNW_ESUCCESS) ||
  569. (frameInfo.start_ip + offset > frameInfo.end_ip))
  570. functionName = ".anonymous.";
  571. _LIBUNWIND_TRACE_UNWINDING(
  572. "unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR ", func=%s, sp=0x%" PRIxPTR ", "
  573. "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
  574. static_cast<void *>(exception_object), frameInfo.start_ip,
  575. functionName, sp, frameInfo.lsda,
  576. frameInfo.handler);
  577. }
  578. #endif
  579. // If there is a personality routine, tell it we are unwinding.
  580. if (frameInfo.handler != 0) {
  581. _Unwind_Personality_Fn p =
  582. (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
  583. struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
  584. // EHABI #7.2
  585. exception_object->pr_cache.fnstart = frameInfo.start_ip;
  586. exception_object->pr_cache.ehtp =
  587. (_Unwind_EHT_Header *)frameInfo.unwind_info;
  588. exception_object->pr_cache.additional = frameInfo.flags;
  589. _Unwind_Reason_Code personalityResult =
  590. (*p)(state, exception_object, context);
  591. switch (personalityResult) {
  592. case _URC_CONTINUE_UNWIND:
  593. // Continue unwinding
  594. _LIBUNWIND_TRACE_UNWINDING(
  595. "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
  596. static_cast<void *>(exception_object));
  597. // EHABI #7.2
  598. if (sp == exception_object->barrier_cache.sp) {
  599. // Phase 1 said we would stop at this frame, but we did not...
  600. _LIBUNWIND_ABORT("during phase1 personality function said it would "
  601. "stop here, but now in phase2 it did not stop here");
  602. }
  603. break;
  604. case _URC_INSTALL_CONTEXT:
  605. _LIBUNWIND_TRACE_UNWINDING(
  606. "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
  607. static_cast<void *>(exception_object));
  608. // Personality routine says to transfer control to landing pad.
  609. // We may get control back if landing pad calls _Unwind_Resume().
  610. if (_LIBUNWIND_TRACING_UNWINDING) {
  611. unw_word_t pc;
  612. __unw_get_reg(cursor, UNW_REG_IP, &pc);
  613. __unw_get_reg(cursor, UNW_REG_SP, &sp);
  614. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
  615. "user code with ip=0x%" PRIxPTR ", sp=0x%" PRIxPTR,
  616. static_cast<void *>(exception_object),
  617. pc, sp);
  618. }
  619. {
  620. // EHABI #7.4.1 says we need to preserve pc for when _Unwind_Resume
  621. // is called back, to find this same frame.
  622. unw_word_t pc;
  623. __unw_get_reg(cursor, UNW_REG_IP, &pc);
  624. exception_object->unwinder_cache.reserved2 = (uint32_t)pc;
  625. }
  626. __unw_resume(cursor);
  627. // __unw_resume() only returns if there was an error.
  628. return _URC_FATAL_PHASE2_ERROR;
  629. // # EHABI #7.4.3
  630. case _URC_FAILURE:
  631. abort();
  632. default:
  633. // Personality routine returned an unknown result code.
  634. _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
  635. personalityResult);
  636. return _URC_FATAL_PHASE2_ERROR;
  637. }
  638. }
  639. frame_count++;
  640. }
  641. // Clean up phase did not resume at the frame that the search phase
  642. // said it would...
  643. return _URC_FATAL_PHASE2_ERROR;
  644. }
  645. static _Unwind_Reason_Code
  646. unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
  647. _Unwind_Exception *exception_object, _Unwind_Stop_Fn stop,
  648. void *stop_parameter) {
  649. bool endOfStack = false;
  650. // See comment at the start of unwind_phase1 regarding VRS integrity.
  651. __unw_init_local(cursor, uc);
  652. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_force(ex_ojb=%p)",
  653. static_cast<void *>(exception_object));
  654. // Walk each frame until we reach where search phase said to stop
  655. while (!endOfStack) {
  656. // Update info about this frame.
  657. unw_proc_info_t frameInfo;
  658. if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
  659. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step "
  660. "failed => _URC_END_OF_STACK",
  661. (void *)exception_object);
  662. return _URC_FATAL_PHASE2_ERROR;
  663. }
  664. #ifndef NDEBUG
  665. // When tracing, print state information.
  666. if (_LIBUNWIND_TRACING_UNWINDING) {
  667. char functionBuf[512];
  668. const char *functionName = functionBuf;
  669. unw_word_t offset;
  670. if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
  671. &offset) != UNW_ESUCCESS) ||
  672. (frameInfo.start_ip + offset > frameInfo.end_ip))
  673. functionName = ".anonymous.";
  674. _LIBUNWIND_TRACE_UNWINDING(
  675. "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
  676. ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
  677. (void *)exception_object, frameInfo.start_ip, functionName,
  678. frameInfo.lsda, frameInfo.handler);
  679. }
  680. #endif
  681. // Call stop function at each frame.
  682. _Unwind_Action action =
  683. (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
  684. _Unwind_Reason_Code stopResult =
  685. (*stop)(1, action, exception_object->exception_class, exception_object,
  686. (_Unwind_Context *)(cursor), stop_parameter);
  687. _LIBUNWIND_TRACE_UNWINDING(
  688. "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
  689. (void *)exception_object, stopResult);
  690. if (stopResult != _URC_NO_REASON) {
  691. _LIBUNWIND_TRACE_UNWINDING(
  692. "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
  693. (void *)exception_object);
  694. return _URC_FATAL_PHASE2_ERROR;
  695. }
  696. // If there is a personality routine, tell it we are unwinding.
  697. if (frameInfo.handler != 0) {
  698. _Unwind_Personality_Fn p =
  699. (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
  700. struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
  701. // EHABI #7.2
  702. exception_object->pr_cache.fnstart = frameInfo.start_ip;
  703. exception_object->pr_cache.ehtp =
  704. (_Unwind_EHT_Header *)frameInfo.unwind_info;
  705. exception_object->pr_cache.additional = frameInfo.flags;
  706. _Unwind_Reason_Code personalityResult =
  707. (*p)(_US_FORCE_UNWIND | _US_UNWIND_FRAME_STARTING, exception_object,
  708. context);
  709. switch (personalityResult) {
  710. case _URC_CONTINUE_UNWIND:
  711. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
  712. "personality returned "
  713. "_URC_CONTINUE_UNWIND",
  714. (void *)exception_object);
  715. // Destructors called, continue unwinding
  716. break;
  717. case _URC_INSTALL_CONTEXT:
  718. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
  719. "personality returned "
  720. "_URC_INSTALL_CONTEXT",
  721. (void *)exception_object);
  722. // We may get control back if landing pad calls _Unwind_Resume().
  723. __unw_resume(cursor);
  724. break;
  725. case _URC_END_OF_STACK:
  726. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
  727. "personality returned "
  728. "_URC_END_OF_STACK",
  729. (void *)exception_object);
  730. // Personalty routine did the step and it can't step forward.
  731. endOfStack = true;
  732. break;
  733. default:
  734. // Personality routine returned an unknown result code.
  735. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
  736. "personality returned %d, "
  737. "_URC_FATAL_PHASE2_ERROR",
  738. (void *)exception_object, personalityResult);
  739. return _URC_FATAL_PHASE2_ERROR;
  740. }
  741. }
  742. }
  743. // Call stop function one last time and tell it we've reached the end
  744. // of the stack.
  745. _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
  746. "function with _UA_END_OF_STACK",
  747. (void *)exception_object);
  748. _Unwind_Action lastAction =
  749. (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
  750. (*stop)(1, lastAction, exception_object->exception_class, exception_object,
  751. (struct _Unwind_Context *)(cursor), stop_parameter);
  752. // Clean up phase did not resume at the frame that the search phase said it
  753. // would.
  754. return _URC_FATAL_PHASE2_ERROR;
  755. }
  756. /// Called by __cxa_throw. Only returns if there is a fatal error.
  757. _LIBUNWIND_EXPORT _Unwind_Reason_Code
  758. _Unwind_RaiseException(_Unwind_Exception *exception_object) {
  759. _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
  760. static_cast<void *>(exception_object));
  761. unw_context_t uc;
  762. unw_cursor_t cursor;
  763. __unw_getcontext(&uc);
  764. // This field for is for compatibility with GCC to say this isn't a forced
  765. // unwind. EHABI #7.2
  766. exception_object->unwinder_cache.reserved1 = 0;
  767. // phase 1: the search phase
  768. _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
  769. if (phase1 != _URC_NO_REASON)
  770. return phase1;
  771. // phase 2: the clean up phase
  772. return unwind_phase2(&uc, &cursor, exception_object, false);
  773. }
  774. _LIBUNWIND_EXPORT void _Unwind_Complete(_Unwind_Exception* exception_object) {
  775. // This is to be called when exception handling completes to give us a chance
  776. // to perform any housekeeping. EHABI #7.2. But we have nothing to do here.
  777. (void)exception_object;
  778. }
  779. /// When _Unwind_RaiseException() is in phase2, it hands control
  780. /// to the personality function at each frame. The personality
  781. /// may force a jump to a landing pad in that function, the landing
  782. /// pad code may then call _Unwind_Resume() to continue with the
  783. /// unwinding. Note: the call to _Unwind_Resume() is from compiler
  784. /// geneated user code. All other _Unwind_* routines are called
  785. /// by the C++ runtime __cxa_* routines.
  786. ///
  787. /// Note: re-throwing an exception (as opposed to continuing the unwind)
  788. /// is implemented by having the code call __cxa_rethrow() which
  789. /// in turn calls _Unwind_Resume_or_Rethrow().
  790. _LIBUNWIND_EXPORT void
  791. _Unwind_Resume(_Unwind_Exception *exception_object) {
  792. _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)",
  793. static_cast<void *>(exception_object));
  794. unw_context_t uc;
  795. unw_cursor_t cursor;
  796. __unw_getcontext(&uc);
  797. if (exception_object->unwinder_cache.reserved1)
  798. unwind_phase2_forced(
  799. &uc, &cursor, exception_object,
  800. (_Unwind_Stop_Fn)exception_object->unwinder_cache.reserved1,
  801. (void *)exception_object->unwinder_cache.reserved3);
  802. else
  803. unwind_phase2(&uc, &cursor, exception_object, true);
  804. // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
  805. _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
  806. }
  807. /// Called by personality handler during phase 2 to get LSDA for current frame.
  808. _LIBUNWIND_EXPORT uintptr_t
  809. _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
  810. unw_cursor_t *cursor = (unw_cursor_t *)context;
  811. unw_proc_info_t frameInfo;
  812. uintptr_t result = 0;
  813. if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
  814. result = (uintptr_t)frameInfo.lsda;
  815. _LIBUNWIND_TRACE_API(
  816. "_Unwind_GetLanguageSpecificData(context=%p) => 0x%llx",
  817. static_cast<void *>(context), (long long)result);
  818. return result;
  819. }
  820. [[maybe_unused]] static uint64_t ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation,
  821. void* valuep) {
  822. uint64_t value = 0;
  823. switch (representation) {
  824. case _UVRSD_UINT32:
  825. case _UVRSD_FLOAT:
  826. memcpy(&value, valuep, sizeof(uint32_t));
  827. break;
  828. case _UVRSD_VFPX:
  829. case _UVRSD_UINT64:
  830. case _UVRSD_DOUBLE:
  831. memcpy(&value, valuep, sizeof(uint64_t));
  832. break;
  833. }
  834. return value;
  835. }
  836. _LIBUNWIND_EXPORT _Unwind_VRS_Result
  837. _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
  838. uint32_t regno, _Unwind_VRS_DataRepresentation representation,
  839. void *valuep) {
  840. _LIBUNWIND_TRACE_API("_Unwind_VRS_Set(context=%p, regclass=%d, reg=%d, "
  841. "rep=%d, value=0x%llX)",
  842. static_cast<void *>(context), regclass, regno,
  843. representation,
  844. ValueAsBitPattern(representation, valuep));
  845. unw_cursor_t *cursor = (unw_cursor_t *)context;
  846. switch (regclass) {
  847. case _UVRSC_CORE:
  848. if (representation != _UVRSD_UINT32 || regno > 15)
  849. return _UVRSR_FAILED;
  850. return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
  851. *(unw_word_t *)valuep) == UNW_ESUCCESS
  852. ? _UVRSR_OK
  853. : _UVRSR_FAILED;
  854. case _UVRSC_VFP:
  855. if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
  856. return _UVRSR_FAILED;
  857. if (representation == _UVRSD_VFPX) {
  858. // Can only touch d0-15 with FSTMFDX.
  859. if (regno > 15)
  860. return _UVRSR_FAILED;
  861. __unw_save_vfp_as_X(cursor);
  862. } else {
  863. if (regno > 31)
  864. return _UVRSR_FAILED;
  865. }
  866. return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
  867. *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
  868. ? _UVRSR_OK
  869. : _UVRSR_FAILED;
  870. #if defined(__ARM_WMMX)
  871. case _UVRSC_WMMXC:
  872. if (representation != _UVRSD_UINT32 || regno > 3)
  873. return _UVRSR_FAILED;
  874. return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
  875. *(unw_word_t *)valuep) == UNW_ESUCCESS
  876. ? _UVRSR_OK
  877. : _UVRSR_FAILED;
  878. case _UVRSC_WMMXD:
  879. if (representation != _UVRSD_DOUBLE || regno > 31)
  880. return _UVRSR_FAILED;
  881. return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
  882. *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
  883. ? _UVRSR_OK
  884. : _UVRSR_FAILED;
  885. #else
  886. case _UVRSC_WMMXC:
  887. case _UVRSC_WMMXD:
  888. break;
  889. #endif
  890. case _UVRSC_PSEUDO:
  891. // There's only one pseudo-register, PAC, with regno == 0.
  892. if (representation != _UVRSD_UINT32 || regno != 0)
  893. return _UVRSR_FAILED;
  894. return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_RA_AUTH_CODE),
  895. *(unw_word_t *)valuep) == UNW_ESUCCESS
  896. ? _UVRSR_OK
  897. : _UVRSR_FAILED;
  898. break;
  899. }
  900. _LIBUNWIND_ABORT("unsupported register class");
  901. }
  902. static _Unwind_VRS_Result
  903. _Unwind_VRS_Get_Internal(_Unwind_Context *context,
  904. _Unwind_VRS_RegClass regclass, uint32_t regno,
  905. _Unwind_VRS_DataRepresentation representation,
  906. void *valuep) {
  907. unw_cursor_t *cursor = (unw_cursor_t *)context;
  908. switch (regclass) {
  909. case _UVRSC_CORE:
  910. if (representation != _UVRSD_UINT32 || regno > 15)
  911. return _UVRSR_FAILED;
  912. return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
  913. (unw_word_t *)valuep) == UNW_ESUCCESS
  914. ? _UVRSR_OK
  915. : _UVRSR_FAILED;
  916. case _UVRSC_VFP:
  917. if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
  918. return _UVRSR_FAILED;
  919. if (representation == _UVRSD_VFPX) {
  920. // Can only touch d0-15 with FSTMFDX.
  921. if (regno > 15)
  922. return _UVRSR_FAILED;
  923. __unw_save_vfp_as_X(cursor);
  924. } else {
  925. if (regno > 31)
  926. return _UVRSR_FAILED;
  927. }
  928. return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
  929. (unw_fpreg_t *)valuep) == UNW_ESUCCESS
  930. ? _UVRSR_OK
  931. : _UVRSR_FAILED;
  932. #if defined(__ARM_WMMX)
  933. case _UVRSC_WMMXC:
  934. if (representation != _UVRSD_UINT32 || regno > 3)
  935. return _UVRSR_FAILED;
  936. return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
  937. (unw_word_t *)valuep) == UNW_ESUCCESS
  938. ? _UVRSR_OK
  939. : _UVRSR_FAILED;
  940. case _UVRSC_WMMXD:
  941. if (representation != _UVRSD_DOUBLE || regno > 31)
  942. return _UVRSR_FAILED;
  943. return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
  944. (unw_fpreg_t *)valuep) == UNW_ESUCCESS
  945. ? _UVRSR_OK
  946. : _UVRSR_FAILED;
  947. #else
  948. case _UVRSC_WMMXC:
  949. case _UVRSC_WMMXD:
  950. break;
  951. #endif
  952. case _UVRSC_PSEUDO:
  953. // There's only one pseudo-register, PAC, with regno == 0.
  954. if (representation != _UVRSD_UINT32 || regno != 0)
  955. return _UVRSR_FAILED;
  956. return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_RA_AUTH_CODE),
  957. (unw_word_t *)valuep) == UNW_ESUCCESS
  958. ? _UVRSR_OK
  959. : _UVRSR_FAILED;
  960. break;
  961. }
  962. _LIBUNWIND_ABORT("unsupported register class");
  963. }
  964. _LIBUNWIND_EXPORT _Unwind_VRS_Result
  965. _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
  966. uint32_t regno, _Unwind_VRS_DataRepresentation representation,
  967. void *valuep) {
  968. _Unwind_VRS_Result result =
  969. _Unwind_VRS_Get_Internal(context, regclass, regno, representation,
  970. valuep);
  971. _LIBUNWIND_TRACE_API("_Unwind_VRS_Get(context=%p, regclass=%d, reg=%d, "
  972. "rep=%d, value=0x%llX, result = %d)",
  973. static_cast<void *>(context), regclass, regno,
  974. representation,
  975. ValueAsBitPattern(representation, valuep), result);
  976. return result;
  977. }
  978. _Unwind_VRS_Result
  979. _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
  980. uint32_t discriminator,
  981. _Unwind_VRS_DataRepresentation representation) {
  982. _LIBUNWIND_TRACE_API("_Unwind_VRS_Pop(context=%p, regclass=%d, "
  983. "discriminator=%d, representation=%d)",
  984. static_cast<void *>(context), regclass, discriminator,
  985. representation);
  986. switch (regclass) {
  987. case _UVRSC_WMMXC:
  988. #if !defined(__ARM_WMMX)
  989. break;
  990. #endif
  991. case _UVRSC_CORE: {
  992. if (representation != _UVRSD_UINT32)
  993. return _UVRSR_FAILED;
  994. // When popping SP from the stack, we don't want to override it from the
  995. // computed new stack location. See EHABI #7.5.4 table 3.
  996. bool poppedSP = false;
  997. uint32_t* sp;
  998. if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
  999. _UVRSD_UINT32, &sp) != _UVRSR_OK) {
  1000. return _UVRSR_FAILED;
  1001. }
  1002. for (uint32_t i = 0; i < 16; ++i) {
  1003. if (!(discriminator & static_cast<uint32_t>(1 << i)))
  1004. continue;
  1005. uint32_t value = *sp++;
  1006. if (regclass == _UVRSC_CORE && i == 13)
  1007. poppedSP = true;
  1008. if (_Unwind_VRS_Set(context, regclass, i,
  1009. _UVRSD_UINT32, &value) != _UVRSR_OK) {
  1010. return _UVRSR_FAILED;
  1011. }
  1012. }
  1013. if (!poppedSP) {
  1014. return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP,
  1015. _UVRSD_UINT32, &sp);
  1016. }
  1017. return _UVRSR_OK;
  1018. }
  1019. case _UVRSC_WMMXD:
  1020. #if !defined(__ARM_WMMX)
  1021. break;
  1022. #endif
  1023. case _UVRSC_VFP: {
  1024. if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
  1025. return _UVRSR_FAILED;
  1026. uint32_t first = discriminator >> 16;
  1027. uint32_t count = discriminator & 0xffff;
  1028. uint32_t end = first+count;
  1029. uint32_t* sp;
  1030. if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
  1031. _UVRSD_UINT32, &sp) != _UVRSR_OK) {
  1032. return _UVRSR_FAILED;
  1033. }
  1034. // For _UVRSD_VFPX, we're assuming the data is stored in FSTMX "standard
  1035. // format 1", which is equivalent to FSTMD + a padding word.
  1036. for (uint32_t i = first; i < end; ++i) {
  1037. // SP is only 32-bit aligned so don't copy 64-bit at a time.
  1038. uint64_t w0 = *sp++;
  1039. uint64_t w1 = *sp++;
  1040. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  1041. uint64_t value = (w1 << 32) | w0;
  1042. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  1043. uint64_t value = (w0 << 32) | w1;
  1044. #else
  1045. #error "Unable to determine endianess"
  1046. #endif
  1047. if (_Unwind_VRS_Set(context, regclass, i, representation, &value) !=
  1048. _UVRSR_OK)
  1049. return _UVRSR_FAILED;
  1050. }
  1051. if (representation == _UVRSD_VFPX)
  1052. ++sp;
  1053. return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
  1054. &sp);
  1055. }
  1056. case _UVRSC_PSEUDO: {
  1057. if (representation != _UVRSD_UINT32 || discriminator != 0)
  1058. return _UVRSR_FAILED;
  1059. // Return Address Authentication code (PAC) - discriminator 0
  1060. uint32_t *sp;
  1061. if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
  1062. &sp) != _UVRSR_OK) {
  1063. return _UVRSR_FAILED;
  1064. }
  1065. uint32_t pac = *sp++;
  1066. _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
  1067. return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_RA_AUTH_CODE,
  1068. _UVRSD_UINT32, &pac);
  1069. }
  1070. }
  1071. _LIBUNWIND_ABORT("unsupported register class");
  1072. }
  1073. /// Not used by C++.
  1074. /// Unwinds stack, calling "stop" function at each frame.
  1075. /// Could be used to implement longjmp().
  1076. _LIBUNWIND_EXPORT _Unwind_Reason_Code
  1077. _Unwind_ForcedUnwind(_Unwind_Exception *exception_object, _Unwind_Stop_Fn stop,
  1078. void *stop_parameter) {
  1079. _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
  1080. (void *)exception_object, (void *)(uintptr_t)stop);
  1081. unw_context_t uc;
  1082. unw_cursor_t cursor;
  1083. __unw_getcontext(&uc);
  1084. // Mark that this is a forced unwind, so _Unwind_Resume() can do
  1085. // the right thing.
  1086. exception_object->unwinder_cache.reserved1 = (uintptr_t)stop;
  1087. exception_object->unwinder_cache.reserved3 = (uintptr_t)stop_parameter;
  1088. return unwind_phase2_forced(&uc, &cursor, exception_object, stop,
  1089. stop_parameter);
  1090. }
  1091. /// Called by personality handler during phase 2 to find the start of the
  1092. /// function.
  1093. _LIBUNWIND_EXPORT uintptr_t
  1094. _Unwind_GetRegionStart(struct _Unwind_Context *context) {
  1095. unw_cursor_t *cursor = (unw_cursor_t *)context;
  1096. unw_proc_info_t frameInfo;
  1097. uintptr_t result = 0;
  1098. if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
  1099. result = (uintptr_t)frameInfo.start_ip;
  1100. _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%llX",
  1101. static_cast<void *>(context), (long long)result);
  1102. return result;
  1103. }
  1104. /// Called by personality handler during phase 2 if a foreign exception
  1105. // is caught.
  1106. _LIBUNWIND_EXPORT void
  1107. _Unwind_DeleteException(_Unwind_Exception *exception_object) {
  1108. _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
  1109. static_cast<void *>(exception_object));
  1110. if (exception_object->exception_cleanup != NULL)
  1111. (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
  1112. exception_object);
  1113. }
  1114. extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
  1115. __gnu_unwind_frame(_Unwind_Exception * /* exception_object */,
  1116. struct _Unwind_Context *context) {
  1117. unw_cursor_t *cursor = (unw_cursor_t *)context;
  1118. switch (__unw_step(cursor)) {
  1119. case UNW_STEP_SUCCESS:
  1120. return _URC_OK;
  1121. case UNW_STEP_END:
  1122. return _URC_END_OF_STACK;
  1123. default:
  1124. return _URC_FAILURE;
  1125. }
  1126. }
  1127. #endif // defined(_LIBUNWIND_ARM_EHABI)