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