1 //===--------------------------- Unwind-EHABI.cpp -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
9 // Implements ARM zero-cost C++ exceptions
11 //===----------------------------------------------------------------------===//
13 #include "Unwind-EHABI.h"
15 #if _LIBUNWIND_ARM_EHABI
23 #include <type_traits>
26 #include "libunwind.h"
27 #include "libunwind_ext.h"
32 // Strange order: take words in order, but inside word, take from most to least
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))];
39 const char* getNextWord(const char* data, uint32_t* out) {
40 *out = *reinterpret_cast<const uint32_t*>(data);
44 const char* getNextNibble(const char* data, uint32_t* out) {
45 *out = *reinterpret_cast<const uint16_t*>(data);
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
69 _Unwind_Reason_Code ProcessDescriptors(
71 _Unwind_Control_Block* ucbp,
72 struct _Unwind_Context* context,
73 Descriptor::Format format,
74 const char* descriptorStart,
77 // EHT is inlined in the index using compact form. No descriptors. #5
79 return _URC_CONTINUE_UNWIND;
81 // TODO: We should check the state here, and determine whether we need to
82 // perform phase1 or phase2 unwinding.
85 const char* descriptor = descriptorStart;
86 uint32_t descriptorWord;
87 getNextWord(descriptor, &descriptorWord);
88 while (descriptorWord) {
89 // Read descriptor based on # 9.2.
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);
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));
108 // Clear off flag from last bit.
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);
117 case Descriptor::CLEANUP: {
118 // TODO(ajwong): Handle cleanup descriptors.
121 case Descriptor::FUNC: {
122 // TODO(ajwong): Handle function descriptors.
125 case Descriptor::CATCH: {
126 // Catch descriptors require gobbling one more word.
127 uint32_t landing_pad;
128 descriptor = getNextWord(descriptor, &landing_pad);
131 // TODO(ajwong): This is only phase1 compatible logic. Implement
133 landing_pad = signExtendPrel31(landing_pad & ~0x80000000);
134 if (landing_pad == 0xffffffff) {
135 return _URC_HANDLER_FOUND;
136 } else if (landing_pad == 0xfffffffe) {
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),
145 &matched_object) != __cxxabiv1::ctm_failed)
146 return _URC_HANDLER_FOUND;
148 _LIBUNWIND_ABORT("Type matching not implemented");
154 _LIBUNWIND_ABORT("Invalid descriptor kind found.");
157 getNextWord(descriptor, &descriptorWord);
160 return _URC_CONTINUE_UNWIND;
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);
173 reinterpret_cast<const char *>(_Unwind_GetLanguageSpecificData(context));
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);
181 if (result != _URC_CONTINUE_UNWIND)
184 if (unw_step(reinterpret_cast<unw_cursor_t*>(context)) != UNW_STEP_SUCCESS)
186 return _URC_CONTINUE_UNWIND;
189 // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_CORE /
191 uint32_t RegisterMask(uint8_t start, uint8_t count_minus_one) {
192 return ((1U << (count_minus_one + 1)) - 1) << start;
195 // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_VFP /
197 uint32_t RegisterRange(uint8_t start, uint8_t count_minus_one) {
198 return ((uint32_t)start << 16) | ((uint32_t)count_minus_one + 1);
201 } // end anonymous namespace
204 * Decodes an EHT entry.
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.
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
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.
225 // 6.3: ARM Compact Model
227 // EHT entries here correspond to the __aeabi_unwind_cpp_pr[012] PRs indeded
229 Descriptor::Format format =
230 static_cast<Descriptor::Format>((*data & 0x0f000000) >> 24);
232 case Descriptor::SU16:
236 case Descriptor::LU16:
237 case Descriptor::LU32:
238 *len = 4 + 4 * ((*data & 0x00ff0000) >> 16);
248 _Unwind_Reason_Code _Unwind_VRS_Interpret(
249 _Unwind_Context* context,
250 const uint32_t* data,
253 bool wrotePC = false;
255 while (offset < len && !finish) {
256 uint8_t byte = getByte(data, offset++);
257 if ((byte & 0x80) == 0) {
259 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
261 sp -= (((uint32_t)byte & 0x3f) << 2) + 4;
263 sp += ((uint32_t)byte << 2) + 4;
264 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
266 switch (byte & 0xf0) {
271 (((uint32_t)byte & 0x0f) << 12) |
272 (((uint32_t)getByte(data, offset++)) << 4);
275 if (registers & (1 << 15))
277 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
281 uint8_t reg = byte & 0x0f;
282 if (reg == 13 || reg == 15)
285 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_R0 + reg,
287 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
292 uint32_t registers = RegisterMask(4, byte & 0x07);
294 registers |= 1 << 14;
295 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
306 uint8_t registers = getByte(data, offset++);
307 if (registers & 0xf0 || !registers)
309 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
315 // This decodes a uleb128 value.
319 uint32_t v = getByte(data, offset++);
320 addend |= (v & 0x7f) << shift;
326 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
328 sp += 0x204 + (addend << 2);
329 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
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);
346 _Unwind_VRS_Pop(context, _UVRSC_VFP,
347 RegisterRange(8, byte & 0x07), _UVRSD_VFPX);
360 _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
361 RegisterRange(10, byte & 0x7), _UVRSD_DOUBLE);
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)
369 _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
370 RegisterRange(start, count_minus_one),
375 uint8_t v = getByte(data, offset++);
378 _Unwind_VRS_Pop(context, _UVRSC_WMMXC, v, _UVRSD_DOUBLE);
383 uint8_t v = getByte(data, offset++);
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)
389 _Unwind_VRS_Pop(context, _UVRSC_VFP,
390 RegisterRange(start, count_minus_one),
402 _Unwind_VRS_Pop(context, _UVRSC_VFP, RegisterRange(8, byte & 0x7),
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);
416 return _URC_CONTINUE_UNWIND;
419 extern "C" _Unwind_Reason_Code __aeabi_unwind_cpp_pr0(
421 _Unwind_Control_Block *ucbp,
422 _Unwind_Context *context) {
423 return unwindOneFrame(state, ucbp, context);
426 extern "C" _Unwind_Reason_Code __aeabi_unwind_cpp_pr1(
428 _Unwind_Control_Block *ucbp,
429 _Unwind_Context *context) {
430 return unwindOneFrame(state, ucbp, context);
433 extern "C" _Unwind_Reason_Code __aeabi_unwind_cpp_pr2(
435 _Unwind_Control_Block *ucbp,
436 _Unwind_Context *context) {
437 return unwindOneFrame(state, ucbp, context);
440 static _Unwind_Reason_Code
441 unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _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_init_local(cursor, uc);
450 // Walk each frame looking for a place to stop.
451 for (bool handlerNotFound = true; handlerNotFound;) {
453 // See if frame has code to run (has personality routine).
454 unw_proc_info_t frameInfo;
455 if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
456 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_get_proc_info "
457 "failed => _URC_FATAL_PHASE1_ERROR",
458 static_cast<void *>(exception_object));
459 return _URC_FATAL_PHASE1_ERROR;
462 // When tracing, print state information.
463 if (_LIBUNWIND_TRACING_UNWINDING) {
464 char functionBuf[512];
465 const char *functionName = functionBuf;
467 if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
468 &offset) != UNW_ESUCCESS) ||
469 (frameInfo.start_ip + offset > frameInfo.end_ip))
470 functionName = ".anonymous.";
472 unw_get_reg(cursor, UNW_REG_IP, &pc);
473 _LIBUNWIND_TRACE_UNWINDING(
474 "unwind_phase1(ex_ojb=%p): pc=0x%llX, start_ip=0x%llX, func=%s, "
475 "lsda=0x%llX, personality=0x%llX",
476 static_cast<void *>(exception_object), (long long)pc,
477 (long long)frameInfo.start_ip, functionName,
478 (long long)frameInfo.lsda, (long long)frameInfo.handler);
481 // If there is a personality routine, ask it if it will want to stop at
483 if (frameInfo.handler != 0) {
484 __personality_routine p =
485 (__personality_routine)(long)(frameInfo.handler);
486 _LIBUNWIND_TRACE_UNWINDING(
487 "unwind_phase1(ex_ojb=%p): calling personality function %p",
488 static_cast<void *>(exception_object),
489 reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(p)));
490 struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
491 exception_object->pr_cache.fnstart = frameInfo.start_ip;
492 exception_object->pr_cache.ehtp =
493 (_Unwind_EHT_Header *)frameInfo.unwind_info;
494 exception_object->pr_cache.additional = frameInfo.flags;
495 _Unwind_Reason_Code personalityResult =
496 (*p)(_US_VIRTUAL_UNWIND_FRAME, exception_object, context);
497 _LIBUNWIND_TRACE_UNWINDING(
498 "unwind_phase1(ex_ojb=%p): personality result %d start_ip %x ehtp %p "
500 static_cast<void *>(exception_object), personalityResult,
501 exception_object->pr_cache.fnstart,
502 static_cast<void *>(exception_object->pr_cache.ehtp),
503 exception_object->pr_cache.additional);
504 switch (personalityResult) {
505 case _URC_HANDLER_FOUND:
506 // found a catch clause or locals that need destructing in this frame
507 // stop search and remember stack pointer at the frame
508 handlerNotFound = false;
509 // p should have initialized barrier_cache. EHABI #7.3.5
510 _LIBUNWIND_TRACE_UNWINDING(
511 "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
512 static_cast<void *>(exception_object));
513 return _URC_NO_REASON;
515 case _URC_CONTINUE_UNWIND:
516 _LIBUNWIND_TRACE_UNWINDING(
517 "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
518 static_cast<void *>(exception_object));
519 // continue unwinding
527 // something went wrong
528 _LIBUNWIND_TRACE_UNWINDING(
529 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
530 static_cast<void *>(exception_object));
531 return _URC_FATAL_PHASE1_ERROR;
535 return _URC_NO_REASON;
538 static _Unwind_Reason_Code unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor,
539 _Unwind_Exception *exception_object,
541 // See comment at the start of unwind_phase1 regarding VRS integrity.
542 unw_init_local(cursor, uc);
544 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
545 static_cast<void *>(exception_object));
548 // Walk each frame until we reach where search phase said to stop.
550 // Ask libunwind to get next frame (skip over first which is
551 // _Unwind_RaiseException or _Unwind_Resume).
553 // Resume only ever makes sense for 1 frame.
554 _Unwind_State state =
555 resume ? _US_UNWIND_FRAME_RESUME : _US_UNWIND_FRAME_STARTING;
556 if (resume && frame_count == 1) {
557 // On a resume, first unwind the _Unwind_Resume() frame. The next frame
558 // is now the landing pad for the cleanup from a previous execution of
559 // phase2. To continue unwindingly correctly, replace VRS[15] with the
560 // IP of the frame that the previous run of phase2 installed the context
561 // for. After this, continue unwinding as if normal.
563 // See #7.4.6 for details.
564 unw_set_reg(cursor, UNW_REG_IP,
565 exception_object->unwinder_cache.reserved2);
569 // Get info about this frame.
571 unw_proc_info_t frameInfo;
572 unw_get_reg(cursor, UNW_REG_SP, &sp);
573 if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
574 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_get_proc_info "
575 "failed => _URC_FATAL_PHASE2_ERROR",
576 static_cast<void *>(exception_object));
577 return _URC_FATAL_PHASE2_ERROR;
580 // When tracing, print state information.
581 if (_LIBUNWIND_TRACING_UNWINDING) {
582 char functionBuf[512];
583 const char *functionName = functionBuf;
585 if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
586 &offset) != UNW_ESUCCESS) ||
587 (frameInfo.start_ip + offset > frameInfo.end_ip))
588 functionName = ".anonymous.";
589 _LIBUNWIND_TRACE_UNWINDING(
590 "unwind_phase2(ex_ojb=%p): start_ip=0x%llX, func=%s, sp=0x%llX, "
591 "lsda=0x%llX, personality=0x%llX",
592 static_cast<void *>(exception_object), (long long)frameInfo.start_ip,
593 functionName, (long long)sp, (long long)frameInfo.lsda,
594 (long long)frameInfo.handler);
597 // If there is a personality routine, tell it we are unwinding.
598 if (frameInfo.handler != 0) {
599 __personality_routine p =
600 (__personality_routine)(long)(frameInfo.handler);
601 struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
603 exception_object->pr_cache.fnstart = frameInfo.start_ip;
604 exception_object->pr_cache.ehtp =
605 (_Unwind_EHT_Header *)frameInfo.unwind_info;
606 exception_object->pr_cache.additional = frameInfo.flags;
607 _Unwind_Reason_Code personalityResult =
608 (*p)(state, exception_object, context);
609 switch (personalityResult) {
610 case _URC_CONTINUE_UNWIND:
611 // Continue unwinding
612 _LIBUNWIND_TRACE_UNWINDING(
613 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
614 static_cast<void *>(exception_object));
616 if (sp == exception_object->barrier_cache.sp) {
617 // Phase 1 said we would stop at this frame, but we did not...
618 _LIBUNWIND_ABORT("during phase1 personality function said it would "
619 "stop here, but now in phase2 it did not stop here");
622 case _URC_INSTALL_CONTEXT:
623 _LIBUNWIND_TRACE_UNWINDING(
624 "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
625 static_cast<void *>(exception_object));
626 // Personality routine says to transfer control to landing pad.
627 // We may get control back if landing pad calls _Unwind_Resume().
628 if (_LIBUNWIND_TRACING_UNWINDING) {
630 unw_get_reg(cursor, UNW_REG_IP, &pc);
631 unw_get_reg(cursor, UNW_REG_SP, &sp);
632 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
633 "user code with ip=0x%llX, sp=0x%llX",
634 static_cast<void *>(exception_object),
635 (long long)pc, (long long)sp);
639 // EHABI #7.4.1 says we need to preserve pc for when _Unwind_Resume
640 // is called back, to find this same frame.
642 unw_get_reg(cursor, UNW_REG_IP, &pc);
643 exception_object->unwinder_cache.reserved2 = (uint32_t)pc;
646 // unw_resume() only returns if there was an error.
647 return _URC_FATAL_PHASE2_ERROR;
654 // Personality routine returned an unknown result code.
655 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
657 return _URC_FATAL_PHASE2_ERROR;
663 // Clean up phase did not resume at the frame that the search phase
665 return _URC_FATAL_PHASE2_ERROR;
668 /// Called by __cxa_throw. Only returns if there is a fatal error.
669 _LIBUNWIND_EXPORT _Unwind_Reason_Code
670 _Unwind_RaiseException(_Unwind_Exception *exception_object) {
671 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
672 static_cast<void *>(exception_object));
677 // This field for is for compatibility with GCC to say this isn't a forced
678 // unwind. EHABI #7.2
679 exception_object->unwinder_cache.reserved1 = 0;
681 // phase 1: the search phase
682 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
683 if (phase1 != _URC_NO_REASON)
686 // phase 2: the clean up phase
687 return unwind_phase2(&uc, &cursor, exception_object, false);
690 _LIBUNWIND_EXPORT void _Unwind_Complete(_Unwind_Exception* exception_object) {
691 // This is to be called when exception handling completes to give us a chance
692 // to perform any housekeeping. EHABI #7.2. But we have nothing to do here.
693 (void)exception_object;
696 /// When _Unwind_RaiseException() is in phase2, it hands control
697 /// to the personality function at each frame. The personality
698 /// may force a jump to a landing pad in that function, the landing
699 /// pad code may then call _Unwind_Resume() to continue with the
700 /// unwinding. Note: the call to _Unwind_Resume() is from compiler
701 /// geneated user code. All other _Unwind_* routines are called
702 /// by the C++ runtime __cxa_* routines.
704 /// Note: re-throwing an exception (as opposed to continuing the unwind)
705 /// is implemented by having the code call __cxa_rethrow() which
706 /// in turn calls _Unwind_Resume_or_Rethrow().
707 _LIBUNWIND_EXPORT void
708 _Unwind_Resume(_Unwind_Exception *exception_object) {
709 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)",
710 static_cast<void *>(exception_object));
715 // _Unwind_RaiseException on EHABI will always set the reserved1 field to 0,
716 // which is in the same position as private_1 below.
717 // TODO(ajwong): Who wronte the above? Why is it true?
718 unwind_phase2(&uc, &cursor, exception_object, true);
720 // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
721 _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
724 /// Called by personality handler during phase 2 to get LSDA for current frame.
725 _LIBUNWIND_EXPORT uintptr_t
726 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
727 unw_cursor_t *cursor = (unw_cursor_t *)context;
728 unw_proc_info_t frameInfo;
729 uintptr_t result = 0;
730 if (unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
731 result = (uintptr_t)frameInfo.lsda;
732 _LIBUNWIND_TRACE_API(
733 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%llx",
734 static_cast<void *>(context), (long long)result);
738 static uint64_t ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation,
741 switch (representation) {
744 memcpy(&value, valuep, sizeof(uint32_t));
750 memcpy(&value, valuep, sizeof(uint64_t));
757 _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
758 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
760 _LIBUNWIND_TRACE_API("_Unwind_VRS_Set(context=%p, regclass=%d, reg=%d, "
761 "rep=%d, value=0x%llX)",
762 static_cast<void *>(context), regclass, regno,
764 ValueAsBitPattern(representation, valuep));
765 unw_cursor_t *cursor = (unw_cursor_t *)context;
768 if (representation != _UVRSD_UINT32 || regno > 15)
769 return _UVRSR_FAILED;
770 return unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
771 *(unw_word_t *)valuep) == UNW_ESUCCESS
775 if (representation != _UVRSD_UINT32 || regno > 3)
776 return _UVRSR_FAILED;
777 return unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
778 *(unw_word_t *)valuep) == UNW_ESUCCESS
782 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
783 return _UVRSR_FAILED;
784 if (representation == _UVRSD_VFPX) {
785 // Can only touch d0-15 with FSTMFDX.
787 return _UVRSR_FAILED;
788 unw_save_vfp_as_X(cursor);
791 return _UVRSR_FAILED;
793 return unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
794 *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
798 if (representation != _UVRSD_DOUBLE || regno > 31)
799 return _UVRSR_FAILED;
800 return unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
801 *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
805 _LIBUNWIND_ABORT("unsupported register class");
808 static _Unwind_VRS_Result
809 _Unwind_VRS_Get_Internal(_Unwind_Context *context,
810 _Unwind_VRS_RegClass regclass, uint32_t regno,
811 _Unwind_VRS_DataRepresentation representation,
813 unw_cursor_t *cursor = (unw_cursor_t *)context;
816 if (representation != _UVRSD_UINT32 || regno > 15)
817 return _UVRSR_FAILED;
818 return unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
819 (unw_word_t *)valuep) == UNW_ESUCCESS
823 if (representation != _UVRSD_UINT32 || regno > 3)
824 return _UVRSR_FAILED;
825 return unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
826 (unw_word_t *)valuep) == UNW_ESUCCESS
830 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
831 return _UVRSR_FAILED;
832 if (representation == _UVRSD_VFPX) {
833 // Can only touch d0-15 with FSTMFDX.
835 return _UVRSR_FAILED;
836 unw_save_vfp_as_X(cursor);
839 return _UVRSR_FAILED;
841 return unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
842 (unw_fpreg_t *)valuep) == UNW_ESUCCESS
846 if (representation != _UVRSD_DOUBLE || regno > 31)
847 return _UVRSR_FAILED;
848 return unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
849 (unw_fpreg_t *)valuep) == UNW_ESUCCESS
853 _LIBUNWIND_ABORT("unsupported register class");
856 _Unwind_VRS_Result _Unwind_VRS_Get(
857 _Unwind_Context *context,
858 _Unwind_VRS_RegClass regclass,
860 _Unwind_VRS_DataRepresentation representation,
862 _Unwind_VRS_Result result =
863 _Unwind_VRS_Get_Internal(context, regclass, regno, representation,
865 _LIBUNWIND_TRACE_API("_Unwind_VRS_Get(context=%p, regclass=%d, reg=%d, "
866 "rep=%d, value=0x%llX, result = %d)",
867 static_cast<void *>(context), regclass, regno,
869 ValueAsBitPattern(representation, valuep), result);
874 _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
875 uint32_t discriminator,
876 _Unwind_VRS_DataRepresentation representation) {
877 _LIBUNWIND_TRACE_API("_Unwind_VRS_Pop(context=%p, regclass=%d, "
878 "discriminator=%d, representation=%d)",
879 static_cast<void *>(context), regclass, discriminator,
884 if (representation != _UVRSD_UINT32)
885 return _UVRSR_FAILED;
886 // When popping SP from the stack, we don't want to override it from the
887 // computed new stack location. See EHABI #7.5.4 table 3.
888 bool poppedSP = false;
890 if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
891 _UVRSD_UINT32, &sp) != _UVRSR_OK) {
892 return _UVRSR_FAILED;
894 for (uint32_t i = 0; i < 16; ++i) {
895 if (!(discriminator & static_cast<uint32_t>(1 << i)))
897 uint32_t value = *sp++;
898 if (regclass == _UVRSC_CORE && i == 13)
900 if (_Unwind_VRS_Set(context, regclass, i,
901 _UVRSD_UINT32, &value) != _UVRSR_OK) {
902 return _UVRSR_FAILED;
906 return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP,
913 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
914 return _UVRSR_FAILED;
915 uint32_t first = discriminator >> 16;
916 uint32_t count = discriminator & 0xffff;
917 uint32_t end = first+count;
919 if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
920 _UVRSD_UINT32, &sp) != _UVRSR_OK) {
921 return _UVRSR_FAILED;
923 // For _UVRSD_VFPX, we're assuming the data is stored in FSTMX "standard
924 // format 1", which is equivalent to FSTMD + a padding word.
925 for (uint32_t i = first; i < end; ++i) {
926 // SP is only 32-bit aligned so don't copy 64-bit at a time.
927 uint64_t value = *sp++;
928 value |= ((uint64_t)(*sp++)) << 32;
929 if (_Unwind_VRS_Set(context, regclass, i, representation, &value) !=
931 return _UVRSR_FAILED;
933 if (representation == _UVRSD_VFPX)
935 return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
939 _LIBUNWIND_ABORT("unsupported register class");
942 /// Called by personality handler during phase 2 to find the start of the
944 _LIBUNWIND_EXPORT uintptr_t
945 _Unwind_GetRegionStart(struct _Unwind_Context *context) {
946 unw_cursor_t *cursor = (unw_cursor_t *)context;
947 unw_proc_info_t frameInfo;
948 uintptr_t result = 0;
949 if (unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
950 result = (uintptr_t)frameInfo.start_ip;
951 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%llX",
952 static_cast<void *>(context), (long long)result);
957 /// Called by personality handler during phase 2 if a foreign exception
959 _LIBUNWIND_EXPORT void
960 _Unwind_DeleteException(_Unwind_Exception *exception_object) {
961 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
962 static_cast<void *>(exception_object));
963 if (exception_object->exception_cleanup != NULL)
964 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
968 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
969 __gnu_unwind_frame(_Unwind_Exception *exception_object,
970 struct _Unwind_Context *context) {
971 unw_cursor_t *cursor = (unw_cursor_t *)context;
972 if (unw_step(cursor) != UNW_STEP_SUCCESS)
977 #endif // _LIBUNWIND_ARM_EHABI