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