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