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 defined(_LIBUNWIND_ARM_EHABI)
24 #include <type_traits>
27 #include "libunwind.h"
28 #include "libunwind_ext.h"
33 // Strange order: take words in order, but inside word, take from most to least
35 uint8_t getByte(const uint32_t* data, size_t offset) {
36 const uint8_t* byteData = reinterpret_cast<const uint8_t*>(data);
37 return byteData[(offset & ~(size_t)0x03) + (3 - (offset & (size_t)0x03))];
40 const char* getNextWord(const char* data, uint32_t* out) {
41 *out = *reinterpret_cast<const uint32_t*>(data);
45 const char* getNextNibble(const char* data, uint32_t* out) {
46 *out = *reinterpret_cast<const uint16_t*>(data);
53 SU16 = 0, // Short descriptor, 16-bit entries
54 LU16 = 1, // Long descriptor, 16-bit entries
55 LU32 = 3, // Long descriptor, 32-bit entries
56 RESERVED0 = 4, RESERVED1 = 5, RESERVED2 = 6, RESERVED3 = 7,
57 RESERVED4 = 8, RESERVED5 = 9, RESERVED6 = 10, RESERVED7 = 11,
58 RESERVED8 = 12, RESERVED9 = 13, RESERVED10 = 14, RESERVED11 = 15
70 _Unwind_Reason_Code ProcessDescriptors(
72 _Unwind_Control_Block* ucbp,
73 struct _Unwind_Context* context,
74 Descriptor::Format format,
75 const char* descriptorStart,
78 // EHT is inlined in the index using compact form. No descriptors. #5
80 return _URC_CONTINUE_UNWIND;
82 // TODO: We should check the state here, and determine whether we need to
83 // perform phase1 or phase2 unwinding.
86 const char* descriptor = descriptorStart;
87 uint32_t descriptorWord;
88 getNextWord(descriptor, &descriptorWord);
89 while (descriptorWord) {
90 // Read descriptor based on # 9.2.
94 case Descriptor::LU32:
95 descriptor = getNextWord(descriptor, &length);
96 descriptor = getNextWord(descriptor, &offset);
97 case Descriptor::LU16:
98 descriptor = getNextNibble(descriptor, &length);
99 descriptor = getNextNibble(descriptor, &offset);
105 // See # 9.2 table for decoding the kind of descriptor. It's a 2-bit value.
106 Descriptor::Kind kind =
107 static_cast<Descriptor::Kind>((length & 0x1) | ((offset & 0x1) << 1));
109 // Clear off flag from last bit.
112 uintptr_t scopeStart = ucbp->pr_cache.fnstart + offset;
113 uintptr_t scopeEnd = scopeStart + length;
114 uintptr_t pc = _Unwind_GetIP(context);
115 bool isInScope = (scopeStart <= pc) && (pc < scopeEnd);
118 case Descriptor::CLEANUP: {
119 // TODO(ajwong): Handle cleanup descriptors.
122 case Descriptor::FUNC: {
123 // TODO(ajwong): Handle function descriptors.
126 case Descriptor::CATCH: {
127 // Catch descriptors require gobbling one more word.
128 uint32_t landing_pad;
129 descriptor = getNextWord(descriptor, &landing_pad);
132 // TODO(ajwong): This is only phase1 compatible logic. Implement
134 landing_pad = signExtendPrel31(landing_pad & ~0x80000000);
135 if (landing_pad == 0xffffffff) {
136 return _URC_HANDLER_FOUND;
137 } else if (landing_pad == 0xfffffffe) {
141 bool is_reference_type = landing_pad & 0x80000000;
142 void* matched_object;
143 if (__cxxabiv1::__cxa_type_match(
144 ucbp, reinterpret_cast<const std::type_info *>(landing_pad),
146 &matched_object) != __cxxabiv1::ctm_failed)
147 return _URC_HANDLER_FOUND;
149 _LIBUNWIND_ABORT("Type matching not implemented");
155 _LIBUNWIND_ABORT("Invalid descriptor kind found.");
158 getNextWord(descriptor, &descriptorWord);
161 return _URC_CONTINUE_UNWIND;
164 static _Unwind_Reason_Code unwindOneFrame(_Unwind_State state,
165 _Unwind_Control_Block* ucbp,
166 struct _Unwind_Context* context) {
167 // Read the compact model EHT entry's header # 6.3
168 const uint32_t* unwindingData = ucbp->pr_cache.ehtp;
169 assert((*unwindingData & 0xf0000000) == 0x80000000 && "Must be a compact entry");
170 Descriptor::Format format =
171 static_cast<Descriptor::Format>((*unwindingData & 0x0f000000) >> 24);
174 reinterpret_cast<const char *>(_Unwind_GetLanguageSpecificData(context));
176 // Handle descriptors before unwinding so they are processed in the context
177 // of the correct stack frame.
178 _Unwind_Reason_Code result =
179 ProcessDescriptors(state, ucbp, context, format, lsda,
180 ucbp->pr_cache.additional);
182 if (result != _URC_CONTINUE_UNWIND)
185 if (unw_step(reinterpret_cast<unw_cursor_t*>(context)) != UNW_STEP_SUCCESS)
187 return _URC_CONTINUE_UNWIND;
190 // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_CORE /
192 uint32_t RegisterMask(uint8_t start, uint8_t count_minus_one) {
193 return ((1U << (count_minus_one + 1)) - 1) << start;
196 // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_VFP /
198 uint32_t RegisterRange(uint8_t start, uint8_t count_minus_one) {
199 return ((uint32_t)start << 16) | ((uint32_t)count_minus_one + 1);
202 } // end anonymous namespace
205 * Decodes an EHT entry.
207 * @param data Pointer to EHT.
208 * @param[out] off Offset from return value (in bytes) to begin interpretation.
209 * @param[out] len Number of bytes in unwind code.
210 * @return Pointer to beginning of unwind code.
212 extern "C" const uint32_t*
213 decode_eht_entry(const uint32_t* data, size_t* off, size_t* len) {
214 if ((*data & 0x80000000) == 0) {
215 // 6.2: Generic Model
217 // EHT entry is a prel31 pointing to the PR, followed by data understood
218 // only by the personality routine. Fortunately, all existing assembler
219 // implementations, including GNU assembler, LLVM integrated assembler,
220 // and ARM assembler, assume that the unwind opcodes come after the
221 // personality rountine address.
222 *off = 1; // First byte is size data.
223 *len = (((data[1] >> 24) & 0xff) + 1) * 4;
224 data++; // Skip the first word, which is the prel31 offset.
226 // 6.3: ARM Compact Model
228 // EHT entries here correspond to the __aeabi_unwind_cpp_pr[012] PRs indeded
230 Descriptor::Format format =
231 static_cast<Descriptor::Format>((*data & 0x0f000000) >> 24);
233 case Descriptor::SU16:
237 case Descriptor::LU16:
238 case Descriptor::LU32:
239 *len = 4 + 4 * ((*data & 0x00ff0000) >> 16);
249 _LIBUNWIND_EXPORT _Unwind_Reason_Code
250 _Unwind_VRS_Interpret(_Unwind_Context *context, const uint32_t *data,
251 size_t offset, size_t len) {
252 bool wrotePC = false;
254 while (offset < len && !finish) {
255 uint8_t byte = getByte(data, offset++);
256 if ((byte & 0x80) == 0) {
258 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
260 sp -= (((uint32_t)byte & 0x3f) << 2) + 4;
262 sp += ((uint32_t)byte << 2) + 4;
263 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
265 switch (byte & 0xf0) {
270 (((uint32_t)byte & 0x0f) << 12) |
271 (((uint32_t)getByte(data, offset++)) << 4);
274 if (registers & (1 << 15))
276 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
280 uint8_t reg = byte & 0x0f;
281 if (reg == 13 || reg == 15)
284 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_R0 + reg,
286 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
291 uint32_t registers = RegisterMask(4, byte & 0x07);
293 registers |= 1 << 14;
294 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
305 uint8_t registers = getByte(data, offset++);
306 if (registers & 0xf0 || !registers)
308 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
314 // This decodes a uleb128 value.
318 uint32_t v = getByte(data, offset++);
319 addend |= (v & 0x7f) << shift;
325 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
327 sp += 0x204 + (addend << 2);
328 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
333 uint8_t v = getByte(data, offset++);
334 _Unwind_VRS_Pop(context, _UVRSC_VFP,
335 RegisterRange(static_cast<uint8_t>(v >> 4),
336 v & 0x0f), _UVRSD_VFPX);
345 _Unwind_VRS_Pop(context, _UVRSC_VFP,
346 RegisterRange(8, byte & 0x07), _UVRSD_VFPX);
353 #if defined(__ARM_WMMX)
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);
384 uint8_t v = getByte(data, offset++);
386 static_cast<uint8_t>(((byte == 0xc8) ? 16 : 0) + (v >> 4));
387 uint8_t count_minus_one = v & 0xf;
388 if (start + count_minus_one >= 32)
390 _Unwind_VRS_Pop(context, _UVRSC_VFP,
391 RegisterRange(start, count_minus_one),
403 _Unwind_VRS_Pop(context, _UVRSC_VFP, RegisterRange(8, byte & 0x7),
414 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_LR, _UVRSD_UINT32, &lr);
415 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_IP, _UVRSD_UINT32, &lr);
417 return _URC_CONTINUE_UNWIND;
420 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
421 __aeabi_unwind_cpp_pr0(_Unwind_State state, _Unwind_Control_Block *ucbp,
422 _Unwind_Context *context) {
423 return unwindOneFrame(state, ucbp, context);
426 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
427 __aeabi_unwind_cpp_pr1(_Unwind_State state, _Unwind_Control_Block *ucbp,
428 _Unwind_Context *context) {
429 return unwindOneFrame(state, ucbp, context);
432 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
433 __aeabi_unwind_cpp_pr2(_Unwind_State state, _Unwind_Control_Block *ucbp,
434 _Unwind_Context *context) {
435 return unwindOneFrame(state, ucbp, context);
438 static _Unwind_Reason_Code
439 unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
440 // EHABI #7.3 discusses preserving the VRS in a "temporary VRS" during
441 // phase 1 and then restoring it to the "primary VRS" for phase 2. The
442 // effect is phase 2 doesn't see any of the VRS manipulations from phase 1.
443 // In this implementation, the phases don't share the VRS backing store.
444 // Instead, they are passed the original |uc| and they create a new VRS
445 // from scratch thus achieving the same effect.
446 unw_init_local(cursor, uc);
448 // Walk each frame looking for a place to stop.
449 for (bool handlerNotFound = true; handlerNotFound;) {
451 // See if frame has code to run (has personality routine).
452 unw_proc_info_t frameInfo;
453 if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
454 _LIBUNWIND_TRACE_UNWINDING("unwind_phase1(ex_ojb=%p): unw_get_proc_info "
455 "failed => _URC_FATAL_PHASE1_ERROR",
456 static_cast<void *>(exception_object));
457 return _URC_FATAL_PHASE1_ERROR;
460 // When tracing, print state information.
461 if (_LIBUNWIND_TRACING_UNWINDING) {
462 char functionBuf[512];
463 const char *functionName = functionBuf;
465 if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
466 &offset) != UNW_ESUCCESS) ||
467 (frameInfo.start_ip + offset > frameInfo.end_ip))
468 functionName = ".anonymous.";
470 unw_get_reg(cursor, UNW_REG_IP, &pc);
471 _LIBUNWIND_TRACE_UNWINDING(
472 "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR ", func=%s, "
473 "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
474 static_cast<void *>(exception_object), pc,
475 frameInfo.start_ip, functionName,
476 frameInfo.lsda, frameInfo.handler);
479 // If there is a personality routine, ask it if it will want to stop at
481 if (frameInfo.handler != 0) {
482 __personality_routine p =
483 (__personality_routine)(long)(frameInfo.handler);
484 _LIBUNWIND_TRACE_UNWINDING(
485 "unwind_phase1(ex_ojb=%p): calling personality function %p",
486 static_cast<void *>(exception_object),
487 reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(p)));
488 struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
489 exception_object->pr_cache.fnstart = frameInfo.start_ip;
490 exception_object->pr_cache.ehtp =
491 (_Unwind_EHT_Header *)frameInfo.unwind_info;
492 exception_object->pr_cache.additional = frameInfo.flags;
493 _Unwind_Reason_Code personalityResult =
494 (*p)(_US_VIRTUAL_UNWIND_FRAME, exception_object, context);
495 _LIBUNWIND_TRACE_UNWINDING(
496 "unwind_phase1(ex_ojb=%p): personality result %d start_ip %x ehtp %p "
498 static_cast<void *>(exception_object), personalityResult,
499 exception_object->pr_cache.fnstart,
500 static_cast<void *>(exception_object->pr_cache.ehtp),
501 exception_object->pr_cache.additional);
502 switch (personalityResult) {
503 case _URC_HANDLER_FOUND:
504 // found a catch clause or locals that need destructing in this frame
505 // stop search and remember stack pointer at the frame
506 handlerNotFound = false;
507 // p should have initialized barrier_cache. EHABI #7.3.5
508 _LIBUNWIND_TRACE_UNWINDING(
509 "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
510 static_cast<void *>(exception_object));
511 return _URC_NO_REASON;
513 case _URC_CONTINUE_UNWIND:
514 _LIBUNWIND_TRACE_UNWINDING(
515 "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
516 static_cast<void *>(exception_object));
517 // continue unwinding
525 // something went wrong
526 _LIBUNWIND_TRACE_UNWINDING(
527 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
528 static_cast<void *>(exception_object));
529 return _URC_FATAL_PHASE1_ERROR;
533 return _URC_NO_REASON;
536 static _Unwind_Reason_Code unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor,
537 _Unwind_Exception *exception_object,
539 // See comment at the start of unwind_phase1 regarding VRS integrity.
540 unw_init_local(cursor, uc);
542 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
543 static_cast<void *>(exception_object));
546 // Walk each frame until we reach where search phase said to stop.
548 // Ask libunwind to get next frame (skip over first which is
549 // _Unwind_RaiseException or _Unwind_Resume).
551 // Resume only ever makes sense for 1 frame.
552 _Unwind_State state =
553 resume ? _US_UNWIND_FRAME_RESUME : _US_UNWIND_FRAME_STARTING;
554 if (resume && frame_count == 1) {
555 // On a resume, first unwind the _Unwind_Resume() frame. The next frame
556 // is now the landing pad for the cleanup from a previous execution of
557 // phase2. To continue unwindingly correctly, replace VRS[15] with the
558 // IP of the frame that the previous run of phase2 installed the context
559 // for. After this, continue unwinding as if normal.
561 // See #7.4.6 for details.
562 unw_set_reg(cursor, UNW_REG_IP,
563 exception_object->unwinder_cache.reserved2);
567 // Get info about this frame.
569 unw_proc_info_t frameInfo;
570 unw_get_reg(cursor, UNW_REG_SP, &sp);
571 if (unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
572 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): unw_get_proc_info "
573 "failed => _URC_FATAL_PHASE2_ERROR",
574 static_cast<void *>(exception_object));
575 return _URC_FATAL_PHASE2_ERROR;
578 // When tracing, print state information.
579 if (_LIBUNWIND_TRACING_UNWINDING) {
580 char functionBuf[512];
581 const char *functionName = functionBuf;
583 if ((unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
584 &offset) != UNW_ESUCCESS) ||
585 (frameInfo.start_ip + offset > frameInfo.end_ip))
586 functionName = ".anonymous.";
587 _LIBUNWIND_TRACE_UNWINDING(
588 "unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR ", func=%s, sp=0x%" PRIxPTR ", "
589 "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
590 static_cast<void *>(exception_object), frameInfo.start_ip,
591 functionName, sp, frameInfo.lsda,
595 // If there is a personality routine, tell it we are unwinding.
596 if (frameInfo.handler != 0) {
597 __personality_routine p =
598 (__personality_routine)(long)(frameInfo.handler);
599 struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
601 exception_object->pr_cache.fnstart = frameInfo.start_ip;
602 exception_object->pr_cache.ehtp =
603 (_Unwind_EHT_Header *)frameInfo.unwind_info;
604 exception_object->pr_cache.additional = frameInfo.flags;
605 _Unwind_Reason_Code personalityResult =
606 (*p)(state, exception_object, context);
607 switch (personalityResult) {
608 case _URC_CONTINUE_UNWIND:
609 // Continue unwinding
610 _LIBUNWIND_TRACE_UNWINDING(
611 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
612 static_cast<void *>(exception_object));
614 if (sp == exception_object->barrier_cache.sp) {
615 // Phase 1 said we would stop at this frame, but we did not...
616 _LIBUNWIND_ABORT("during phase1 personality function said it would "
617 "stop here, but now in phase2 it did not stop here");
620 case _URC_INSTALL_CONTEXT:
621 _LIBUNWIND_TRACE_UNWINDING(
622 "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
623 static_cast<void *>(exception_object));
624 // Personality routine says to transfer control to landing pad.
625 // We may get control back if landing pad calls _Unwind_Resume().
626 if (_LIBUNWIND_TRACING_UNWINDING) {
628 unw_get_reg(cursor, UNW_REG_IP, &pc);
629 unw_get_reg(cursor, UNW_REG_SP, &sp);
630 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
631 "user code with ip=0x%" PRIxPTR ", sp=0x%" PRIxPTR,
632 static_cast<void *>(exception_object),
637 // EHABI #7.4.1 says we need to preserve pc for when _Unwind_Resume
638 // is called back, to find this same frame.
640 unw_get_reg(cursor, UNW_REG_IP, &pc);
641 exception_object->unwinder_cache.reserved2 = (uint32_t)pc;
644 // unw_resume() only returns if there was an error.
645 return _URC_FATAL_PHASE2_ERROR;
652 // Personality routine returned an unknown result code.
653 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
655 return _URC_FATAL_PHASE2_ERROR;
661 // Clean up phase did not resume at the frame that the search phase
663 return _URC_FATAL_PHASE2_ERROR;
666 /// Called by __cxa_throw. Only returns if there is a fatal error.
667 _LIBUNWIND_EXPORT _Unwind_Reason_Code
668 _Unwind_RaiseException(_Unwind_Exception *exception_object) {
669 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
670 static_cast<void *>(exception_object));
675 // This field for is for compatibility with GCC to say this isn't a forced
676 // unwind. EHABI #7.2
677 exception_object->unwinder_cache.reserved1 = 0;
679 // phase 1: the search phase
680 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
681 if (phase1 != _URC_NO_REASON)
684 // phase 2: the clean up phase
685 return unwind_phase2(&uc, &cursor, exception_object, false);
688 _LIBUNWIND_EXPORT void _Unwind_Complete(_Unwind_Exception* exception_object) {
689 // This is to be called when exception handling completes to give us a chance
690 // to perform any housekeeping. EHABI #7.2. But we have nothing to do here.
691 (void)exception_object;
694 /// When _Unwind_RaiseException() is in phase2, it hands control
695 /// to the personality function at each frame. The personality
696 /// may force a jump to a landing pad in that function, the landing
697 /// pad code may then call _Unwind_Resume() to continue with the
698 /// unwinding. Note: the call to _Unwind_Resume() is from compiler
699 /// geneated user code. All other _Unwind_* routines are called
700 /// by the C++ runtime __cxa_* routines.
702 /// Note: re-throwing an exception (as opposed to continuing the unwind)
703 /// is implemented by having the code call __cxa_rethrow() which
704 /// in turn calls _Unwind_Resume_or_Rethrow().
705 _LIBUNWIND_EXPORT void
706 _Unwind_Resume(_Unwind_Exception *exception_object) {
707 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)",
708 static_cast<void *>(exception_object));
713 // _Unwind_RaiseException on EHABI will always set the reserved1 field to 0,
714 // which is in the same position as private_1 below.
715 // TODO(ajwong): Who wronte the above? Why is it true?
716 unwind_phase2(&uc, &cursor, exception_object, true);
718 // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
719 _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
722 /// Called by personality handler during phase 2 to get LSDA for current frame.
723 _LIBUNWIND_EXPORT uintptr_t
724 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
725 unw_cursor_t *cursor = (unw_cursor_t *)context;
726 unw_proc_info_t frameInfo;
727 uintptr_t result = 0;
728 if (unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
729 result = (uintptr_t)frameInfo.lsda;
730 _LIBUNWIND_TRACE_API(
731 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%llx",
732 static_cast<void *>(context), (long long)result);
736 static uint64_t ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation,
739 switch (representation) {
742 memcpy(&value, valuep, sizeof(uint32_t));
748 memcpy(&value, valuep, sizeof(uint64_t));
754 _LIBUNWIND_EXPORT _Unwind_VRS_Result
755 _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
756 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
758 _LIBUNWIND_TRACE_API("_Unwind_VRS_Set(context=%p, regclass=%d, reg=%d, "
759 "rep=%d, value=0x%llX)",
760 static_cast<void *>(context), regclass, regno,
762 ValueAsBitPattern(representation, valuep));
763 unw_cursor_t *cursor = (unw_cursor_t *)context;
766 if (representation != _UVRSD_UINT32 || regno > 15)
767 return _UVRSR_FAILED;
768 return unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
769 *(unw_word_t *)valuep) == UNW_ESUCCESS
773 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
774 return _UVRSR_FAILED;
775 if (representation == _UVRSD_VFPX) {
776 // Can only touch d0-15 with FSTMFDX.
778 return _UVRSR_FAILED;
779 unw_save_vfp_as_X(cursor);
782 return _UVRSR_FAILED;
784 return unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
785 *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
788 #if defined(__ARM_WMMX)
790 if (representation != _UVRSD_UINT32 || regno > 3)
791 return _UVRSR_FAILED;
792 return unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
793 *(unw_word_t *)valuep) == UNW_ESUCCESS
797 if (representation != _UVRSD_DOUBLE || regno > 31)
798 return _UVRSR_FAILED;
799 return unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
800 *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
809 _LIBUNWIND_ABORT("unsupported register class");
812 static _Unwind_VRS_Result
813 _Unwind_VRS_Get_Internal(_Unwind_Context *context,
814 _Unwind_VRS_RegClass regclass, uint32_t regno,
815 _Unwind_VRS_DataRepresentation representation,
817 unw_cursor_t *cursor = (unw_cursor_t *)context;
820 if (representation != _UVRSD_UINT32 || regno > 15)
821 return _UVRSR_FAILED;
822 return unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
823 (unw_word_t *)valuep) == UNW_ESUCCESS
827 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
828 return _UVRSR_FAILED;
829 if (representation == _UVRSD_VFPX) {
830 // Can only touch d0-15 with FSTMFDX.
832 return _UVRSR_FAILED;
833 unw_save_vfp_as_X(cursor);
836 return _UVRSR_FAILED;
838 return unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
839 (unw_fpreg_t *)valuep) == UNW_ESUCCESS
842 #if defined(__ARM_WMMX)
844 if (representation != _UVRSD_UINT32 || regno > 3)
845 return _UVRSR_FAILED;
846 return unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
847 (unw_word_t *)valuep) == UNW_ESUCCESS
851 if (representation != _UVRSD_DOUBLE || regno > 31)
852 return _UVRSR_FAILED;
853 return unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
854 (unw_fpreg_t *)valuep) == UNW_ESUCCESS
863 _LIBUNWIND_ABORT("unsupported register class");
866 _LIBUNWIND_EXPORT _Unwind_VRS_Result
867 _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
868 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
870 _Unwind_VRS_Result result =
871 _Unwind_VRS_Get_Internal(context, regclass, regno, representation,
873 _LIBUNWIND_TRACE_API("_Unwind_VRS_Get(context=%p, regclass=%d, reg=%d, "
874 "rep=%d, value=0x%llX, result = %d)",
875 static_cast<void *>(context), regclass, regno,
877 ValueAsBitPattern(representation, valuep), result);
882 _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
883 uint32_t discriminator,
884 _Unwind_VRS_DataRepresentation representation) {
885 _LIBUNWIND_TRACE_API("_Unwind_VRS_Pop(context=%p, regclass=%d, "
886 "discriminator=%d, representation=%d)",
887 static_cast<void *>(context), regclass, discriminator,
891 #if !defined(__ARM_WMMX)
895 if (representation != _UVRSD_UINT32)
896 return _UVRSR_FAILED;
897 // When popping SP from the stack, we don't want to override it from the
898 // computed new stack location. See EHABI #7.5.4 table 3.
899 bool poppedSP = false;
901 if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
902 _UVRSD_UINT32, &sp) != _UVRSR_OK) {
903 return _UVRSR_FAILED;
905 for (uint32_t i = 0; i < 16; ++i) {
906 if (!(discriminator & static_cast<uint32_t>(1 << i)))
908 uint32_t value = *sp++;
909 if (regclass == _UVRSC_CORE && i == 13)
911 if (_Unwind_VRS_Set(context, regclass, i,
912 _UVRSD_UINT32, &value) != _UVRSR_OK) {
913 return _UVRSR_FAILED;
917 return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP,
923 #if !defined(__ARM_WMMX)
927 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
928 return _UVRSR_FAILED;
929 uint32_t first = discriminator >> 16;
930 uint32_t count = discriminator & 0xffff;
931 uint32_t end = first+count;
933 if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
934 _UVRSD_UINT32, &sp) != _UVRSR_OK) {
935 return _UVRSR_FAILED;
937 // For _UVRSD_VFPX, we're assuming the data is stored in FSTMX "standard
938 // format 1", which is equivalent to FSTMD + a padding word.
939 for (uint32_t i = first; i < end; ++i) {
940 // SP is only 32-bit aligned so don't copy 64-bit at a time.
941 uint64_t value = *sp++;
942 value |= ((uint64_t)(*sp++)) << 32;
943 if (_Unwind_VRS_Set(context, regclass, i, representation, &value) !=
945 return _UVRSR_FAILED;
947 if (representation == _UVRSD_VFPX)
949 return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
953 _LIBUNWIND_ABORT("unsupported register class");
956 /// Called by personality handler during phase 2 to find the start of the
958 _LIBUNWIND_EXPORT uintptr_t
959 _Unwind_GetRegionStart(struct _Unwind_Context *context) {
960 unw_cursor_t *cursor = (unw_cursor_t *)context;
961 unw_proc_info_t frameInfo;
962 uintptr_t result = 0;
963 if (unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
964 result = (uintptr_t)frameInfo.start_ip;
965 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%llX",
966 static_cast<void *>(context), (long long)result);
971 /// Called by personality handler during phase 2 if a foreign exception
973 _LIBUNWIND_EXPORT void
974 _Unwind_DeleteException(_Unwind_Exception *exception_object) {
975 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
976 static_cast<void *>(exception_object));
977 if (exception_object->exception_cleanup != NULL)
978 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
982 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
983 __gnu_unwind_frame(_Unwind_Exception *exception_object,
984 struct _Unwind_Context *context) {
985 unw_cursor_t *cursor = (unw_cursor_t *)context;
986 if (unw_step(cursor) != UNW_STEP_SUCCESS)
991 #endif // defined(_LIBUNWIND_ARM_EHABI)