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