1 //===--------------------------- Unwind-EHABI.cpp -------------------------===//
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
8 // Implements ARM zero-cost C++ exceptions
10 //===----------------------------------------------------------------------===//
12 #include "Unwind-EHABI.h"
14 #if defined(_LIBUNWIND_ARM_EHABI)
24 #include "libunwind.h"
25 #include "libunwind_ext.h"
30 // Strange order: take words in order, but inside word, take from most to least
32 uint8_t getByte(const uint32_t* data, size_t offset) {
33 const uint8_t* byteData = reinterpret_cast<const uint8_t*>(data);
34 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
35 return byteData[(offset & ~(size_t)0x03) + (3 - (offset & (size_t)0x03))];
36 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
37 return byteData[offset];
39 #error "Unable to determine endianess"
43 const char* getNextWord(const char* data, uint32_t* out) {
44 *out = *reinterpret_cast<const uint32_t*>(data);
48 const char* getNextNibble(const char* data, uint32_t* out) {
49 *out = *reinterpret_cast<const uint16_t*>(data);
56 SU16 = 0, // Short descriptor, 16-bit entries
57 LU16 = 1, // Long descriptor, 16-bit entries
58 LU32 = 3, // Long descriptor, 32-bit entries
59 RESERVED0 = 4, RESERVED1 = 5, RESERVED2 = 6, RESERVED3 = 7,
60 RESERVED4 = 8, RESERVED5 = 9, RESERVED6 = 10, RESERVED7 = 11,
61 RESERVED8 = 12, RESERVED9 = 13, RESERVED10 = 14, RESERVED11 = 15
73 _Unwind_Reason_Code ProcessDescriptors(
75 _Unwind_Control_Block* ucbp,
76 struct _Unwind_Context* context,
77 Descriptor::Format format,
78 const char* descriptorStart,
81 // EHT is inlined in the index using compact form. No descriptors. #5
83 return _URC_CONTINUE_UNWIND;
85 // TODO: We should check the state here, and determine whether we need to
86 // perform phase1 or phase2 unwinding.
89 const char* descriptor = descriptorStart;
90 uint32_t descriptorWord;
91 getNextWord(descriptor, &descriptorWord);
92 while (descriptorWord) {
93 // Read descriptor based on # 9.2.
97 case Descriptor::LU32:
98 descriptor = getNextWord(descriptor, &length);
99 descriptor = getNextWord(descriptor, &offset);
100 case Descriptor::LU16:
101 descriptor = getNextNibble(descriptor, &length);
102 descriptor = getNextNibble(descriptor, &offset);
108 // See # 9.2 table for decoding the kind of descriptor. It's a 2-bit value.
109 Descriptor::Kind kind =
110 static_cast<Descriptor::Kind>((length & 0x1) | ((offset & 0x1) << 1));
112 // Clear off flag from last bit.
115 uintptr_t scopeStart = ucbp->pr_cache.fnstart + offset;
116 uintptr_t scopeEnd = scopeStart + length;
117 uintptr_t pc = _Unwind_GetIP(context);
118 bool isInScope = (scopeStart <= pc) && (pc < scopeEnd);
121 case Descriptor::CLEANUP: {
122 // TODO(ajwong): Handle cleanup descriptors.
125 case Descriptor::FUNC: {
126 // TODO(ajwong): Handle function descriptors.
129 case Descriptor::CATCH: {
130 // Catch descriptors require gobbling one more word.
131 uint32_t landing_pad;
132 descriptor = getNextWord(descriptor, &landing_pad);
135 // TODO(ajwong): This is only phase1 compatible logic. Implement
137 landing_pad = signExtendPrel31(landing_pad & ~0x80000000);
138 if (landing_pad == 0xffffffff) {
139 return _URC_HANDLER_FOUND;
140 } else if (landing_pad == 0xfffffffe) {
144 bool is_reference_type = landing_pad & 0x80000000;
145 void* matched_object;
146 if (__cxxabiv1::__cxa_type_match(
147 ucbp, reinterpret_cast<const std::type_info *>(landing_pad),
149 &matched_object) != __cxxabiv1::ctm_failed)
150 return _URC_HANDLER_FOUND;
152 _LIBUNWIND_ABORT("Type matching not implemented");
158 _LIBUNWIND_ABORT("Invalid descriptor kind found.");
161 getNextWord(descriptor, &descriptorWord);
164 return _URC_CONTINUE_UNWIND;
167 static _Unwind_Reason_Code unwindOneFrame(_Unwind_State state,
168 _Unwind_Control_Block* ucbp,
169 struct _Unwind_Context* context) {
170 // Read the compact model EHT entry's header # 6.3
171 const uint32_t* unwindingData = ucbp->pr_cache.ehtp;
172 assert((*unwindingData & 0xf0000000) == 0x80000000 && "Must be a compact entry");
173 Descriptor::Format format =
174 static_cast<Descriptor::Format>((*unwindingData & 0x0f000000) >> 24);
177 reinterpret_cast<const char *>(_Unwind_GetLanguageSpecificData(context));
179 // Handle descriptors before unwinding so they are processed in the context
180 // of the correct stack frame.
181 _Unwind_Reason_Code result =
182 ProcessDescriptors(state, ucbp, context, format, lsda,
183 ucbp->pr_cache.additional);
185 if (result != _URC_CONTINUE_UNWIND)
188 if (__unw_step(reinterpret_cast<unw_cursor_t *>(context)) != UNW_STEP_SUCCESS)
190 return _URC_CONTINUE_UNWIND;
193 // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_CORE /
195 uint32_t RegisterMask(uint8_t start, uint8_t count_minus_one) {
196 return ((1U << (count_minus_one + 1)) - 1) << start;
199 // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_VFP /
201 uint32_t RegisterRange(uint8_t start, uint8_t count_minus_one) {
202 return ((uint32_t)start << 16) | ((uint32_t)count_minus_one + 1);
205 } // end anonymous namespace
208 * Decodes an EHT entry.
210 * @param data Pointer to EHT.
211 * @param[out] off Offset from return value (in bytes) to begin interpretation.
212 * @param[out] len Number of bytes in unwind code.
213 * @return Pointer to beginning of unwind code.
215 extern "C" const uint32_t*
216 decode_eht_entry(const uint32_t* data, size_t* off, size_t* len) {
217 if ((*data & 0x80000000) == 0) {
218 // 6.2: Generic Model
220 // EHT entry is a prel31 pointing to the PR, followed by data understood
221 // only by the personality routine. Fortunately, all existing assembler
222 // implementations, including GNU assembler, LLVM integrated assembler,
223 // and ARM assembler, assume that the unwind opcodes come after the
224 // personality rountine address.
225 *off = 1; // First byte is size data.
226 *len = (((data[1] >> 24) & 0xff) + 1) * 4;
227 data++; // Skip the first word, which is the prel31 offset.
229 // 6.3: ARM Compact Model
231 // EHT entries here correspond to the __aeabi_unwind_cpp_pr[012] PRs indeded
233 Descriptor::Format format =
234 static_cast<Descriptor::Format>((*data & 0x0f000000) >> 24);
236 case Descriptor::SU16:
240 case Descriptor::LU16:
241 case Descriptor::LU32:
242 *len = 4 + 4 * ((*data & 0x00ff0000) >> 16);
252 _LIBUNWIND_EXPORT _Unwind_Reason_Code
253 _Unwind_VRS_Interpret(_Unwind_Context *context, const uint32_t *data,
254 size_t offset, size_t len) {
255 bool wrotePC = false;
257 while (offset < len && !finish) {
258 uint8_t byte = getByte(data, offset++);
259 if ((byte & 0x80) == 0) {
261 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
263 sp -= (((uint32_t)byte & 0x3f) << 2) + 4;
265 sp += ((uint32_t)byte << 2) + 4;
266 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
268 switch (byte & 0xf0) {
273 (((uint32_t)byte & 0x0f) << 12) |
274 (((uint32_t)getByte(data, offset++)) << 4);
277 if (registers & (1 << 15))
279 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
283 uint8_t reg = byte & 0x0f;
284 if (reg == 13 || reg == 15)
287 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_R0 + reg,
289 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
294 uint32_t registers = RegisterMask(4, byte & 0x07);
296 registers |= 1 << 14;
297 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
308 uint8_t registers = getByte(data, offset++);
309 if (registers & 0xf0 || !registers)
311 _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
317 // This decodes a uleb128 value.
321 uint32_t v = getByte(data, offset++);
322 addend |= (v & 0x7f) << shift;
328 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
330 sp += 0x204 + (addend << 2);
331 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
336 uint8_t v = getByte(data, offset++);
337 _Unwind_VRS_Pop(context, _UVRSC_VFP,
338 RegisterRange(static_cast<uint8_t>(v >> 4),
339 v & 0x0f), _UVRSD_VFPX);
348 _Unwind_VRS_Pop(context, _UVRSC_VFP,
349 RegisterRange(8, byte & 0x07), _UVRSD_VFPX);
356 #if defined(__ARM_WMMX)
363 _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
364 RegisterRange(10, byte & 0x7), _UVRSD_DOUBLE);
367 uint8_t v = getByte(data, offset++);
368 uint8_t start = static_cast<uint8_t>(v >> 4);
369 uint8_t count_minus_one = v & 0xf;
370 if (start + count_minus_one >= 16)
372 _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
373 RegisterRange(start, count_minus_one),
378 uint8_t v = getByte(data, offset++);
381 _Unwind_VRS_Pop(context, _UVRSC_WMMXC, v, _UVRSD_DOUBLE);
387 uint8_t v = getByte(data, offset++);
389 static_cast<uint8_t>(((byte == 0xc8) ? 16 : 0) + (v >> 4));
390 uint8_t count_minus_one = v & 0xf;
391 if (start + count_minus_one >= 32)
393 _Unwind_VRS_Pop(context, _UVRSC_VFP,
394 RegisterRange(start, count_minus_one),
406 _Unwind_VRS_Pop(context, _UVRSC_VFP, RegisterRange(8, byte & 0x7),
417 _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_LR, _UVRSD_UINT32, &lr);
418 _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_IP, _UVRSD_UINT32, &lr);
420 return _URC_CONTINUE_UNWIND;
423 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
424 __aeabi_unwind_cpp_pr0(_Unwind_State state, _Unwind_Control_Block *ucbp,
425 _Unwind_Context *context) {
426 return unwindOneFrame(state, ucbp, context);
429 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
430 __aeabi_unwind_cpp_pr1(_Unwind_State state, _Unwind_Control_Block *ucbp,
431 _Unwind_Context *context) {
432 return unwindOneFrame(state, ucbp, context);
435 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
436 __aeabi_unwind_cpp_pr2(_Unwind_State state, _Unwind_Control_Block *ucbp,
437 _Unwind_Context *context) {
438 return unwindOneFrame(state, ucbp, context);
441 static _Unwind_Reason_Code
442 unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
443 // EHABI #7.3 discusses preserving the VRS in a "temporary VRS" during
444 // phase 1 and then restoring it to the "primary VRS" for phase 2. The
445 // effect is phase 2 doesn't see any of the VRS manipulations from phase 1.
446 // In this implementation, the phases don't share the VRS backing store.
447 // Instead, they are passed the original |uc| and they create a new VRS
448 // from scratch thus achieving the same effect.
449 __unw_init_local(cursor, uc);
451 // Walk each frame looking for a place to stop.
452 for (bool handlerNotFound = true; handlerNotFound;) {
454 // See if frame has code to run (has personality routine).
455 unw_proc_info_t frameInfo;
456 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
457 _LIBUNWIND_TRACE_UNWINDING(
458 "unwind_phase1(ex_ojb=%p): __unw_get_proc_info "
459 "failed => _URC_FATAL_PHASE1_ERROR",
460 static_cast<void *>(exception_object));
461 return _URC_FATAL_PHASE1_ERROR;
464 // When tracing, print state information.
465 if (_LIBUNWIND_TRACING_UNWINDING) {
466 char functionBuf[512];
467 const char *functionName = functionBuf;
469 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
470 &offset) != UNW_ESUCCESS) ||
471 (frameInfo.start_ip + offset > frameInfo.end_ip))
472 functionName = ".anonymous.";
474 __unw_get_reg(cursor, UNW_REG_IP, &pc);
475 _LIBUNWIND_TRACE_UNWINDING(
476 "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR ", func=%s, "
477 "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
478 static_cast<void *>(exception_object), pc,
479 frameInfo.start_ip, functionName,
480 frameInfo.lsda, frameInfo.handler);
483 // If there is a personality routine, ask it if it will want to stop at
485 if (frameInfo.handler != 0) {
486 _Unwind_Personality_Fn p =
487 (_Unwind_Personality_Fn)(long)(frameInfo.handler);
488 _LIBUNWIND_TRACE_UNWINDING(
489 "unwind_phase1(ex_ojb=%p): calling personality function %p",
490 static_cast<void *>(exception_object),
491 reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(p)));
492 struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
493 exception_object->pr_cache.fnstart = frameInfo.start_ip;
494 exception_object->pr_cache.ehtp =
495 (_Unwind_EHT_Header *)frameInfo.unwind_info;
496 exception_object->pr_cache.additional = frameInfo.flags;
497 _Unwind_Reason_Code personalityResult =
498 (*p)(_US_VIRTUAL_UNWIND_FRAME, exception_object, context);
499 _LIBUNWIND_TRACE_UNWINDING(
500 "unwind_phase1(ex_ojb=%p): personality result %d start_ip %x ehtp %p "
502 static_cast<void *>(exception_object), personalityResult,
503 exception_object->pr_cache.fnstart,
504 static_cast<void *>(exception_object->pr_cache.ehtp),
505 exception_object->pr_cache.additional);
506 switch (personalityResult) {
507 case _URC_HANDLER_FOUND:
508 // found a catch clause or locals that need destructing in this frame
509 // stop search and remember stack pointer at the frame
510 handlerNotFound = false;
511 // p should have initialized barrier_cache. EHABI #7.3.5
512 _LIBUNWIND_TRACE_UNWINDING(
513 "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
514 static_cast<void *>(exception_object));
515 return _URC_NO_REASON;
517 case _URC_CONTINUE_UNWIND:
518 _LIBUNWIND_TRACE_UNWINDING(
519 "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
520 static_cast<void *>(exception_object));
521 // continue unwinding
529 // something went wrong
530 _LIBUNWIND_TRACE_UNWINDING(
531 "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
532 static_cast<void *>(exception_object));
533 return _URC_FATAL_PHASE1_ERROR;
537 return _URC_NO_REASON;
540 static _Unwind_Reason_Code unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor,
541 _Unwind_Exception *exception_object,
543 // See comment at the start of unwind_phase1 regarding VRS integrity.
544 __unw_init_local(cursor, uc);
546 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
547 static_cast<void *>(exception_object));
550 // Walk each frame until we reach where search phase said to stop.
552 // Ask libunwind to get next frame (skip over first which is
553 // _Unwind_RaiseException or _Unwind_Resume).
555 // Resume only ever makes sense for 1 frame.
556 _Unwind_State state =
557 resume ? _US_UNWIND_FRAME_RESUME : _US_UNWIND_FRAME_STARTING;
558 if (resume && frame_count == 1) {
559 // On a resume, first unwind the _Unwind_Resume() frame. The next frame
560 // is now the landing pad for the cleanup from a previous execution of
561 // phase2. To continue unwindingly correctly, replace VRS[15] with the
562 // IP of the frame that the previous run of phase2 installed the context
563 // for. After this, continue unwinding as if normal.
565 // See #7.4.6 for details.
566 __unw_set_reg(cursor, UNW_REG_IP,
567 exception_object->unwinder_cache.reserved2);
571 // Get info about this frame.
573 unw_proc_info_t frameInfo;
574 __unw_get_reg(cursor, UNW_REG_SP, &sp);
575 if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
576 _LIBUNWIND_TRACE_UNWINDING(
577 "unwind_phase2(ex_ojb=%p): __unw_get_proc_info "
578 "failed => _URC_FATAL_PHASE2_ERROR",
579 static_cast<void *>(exception_object));
580 return _URC_FATAL_PHASE2_ERROR;
583 // When tracing, print state information.
584 if (_LIBUNWIND_TRACING_UNWINDING) {
585 char functionBuf[512];
586 const char *functionName = functionBuf;
588 if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
589 &offset) != UNW_ESUCCESS) ||
590 (frameInfo.start_ip + offset > frameInfo.end_ip))
591 functionName = ".anonymous.";
592 _LIBUNWIND_TRACE_UNWINDING(
593 "unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR ", func=%s, sp=0x%" PRIxPTR ", "
594 "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
595 static_cast<void *>(exception_object), frameInfo.start_ip,
596 functionName, sp, frameInfo.lsda,
600 // If there is a personality routine, tell it we are unwinding.
601 if (frameInfo.handler != 0) {
602 _Unwind_Personality_Fn p =
603 (_Unwind_Personality_Fn)(long)(frameInfo.handler);
604 struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
606 exception_object->pr_cache.fnstart = frameInfo.start_ip;
607 exception_object->pr_cache.ehtp =
608 (_Unwind_EHT_Header *)frameInfo.unwind_info;
609 exception_object->pr_cache.additional = frameInfo.flags;
610 _Unwind_Reason_Code personalityResult =
611 (*p)(state, exception_object, context);
612 switch (personalityResult) {
613 case _URC_CONTINUE_UNWIND:
614 // Continue unwinding
615 _LIBUNWIND_TRACE_UNWINDING(
616 "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
617 static_cast<void *>(exception_object));
619 if (sp == exception_object->barrier_cache.sp) {
620 // Phase 1 said we would stop at this frame, but we did not...
621 _LIBUNWIND_ABORT("during phase1 personality function said it would "
622 "stop here, but now in phase2 it did not stop here");
625 case _URC_INSTALL_CONTEXT:
626 _LIBUNWIND_TRACE_UNWINDING(
627 "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
628 static_cast<void *>(exception_object));
629 // Personality routine says to transfer control to landing pad.
630 // We may get control back if landing pad calls _Unwind_Resume().
631 if (_LIBUNWIND_TRACING_UNWINDING) {
633 __unw_get_reg(cursor, UNW_REG_IP, &pc);
634 __unw_get_reg(cursor, UNW_REG_SP, &sp);
635 _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
636 "user code with ip=0x%" PRIxPTR ", sp=0x%" PRIxPTR,
637 static_cast<void *>(exception_object),
642 // EHABI #7.4.1 says we need to preserve pc for when _Unwind_Resume
643 // is called back, to find this same frame.
645 __unw_get_reg(cursor, UNW_REG_IP, &pc);
646 exception_object->unwinder_cache.reserved2 = (uint32_t)pc;
648 __unw_resume(cursor);
649 // __unw_resume() only returns if there was an error.
650 return _URC_FATAL_PHASE2_ERROR;
657 // Personality routine returned an unknown result code.
658 _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
660 return _URC_FATAL_PHASE2_ERROR;
666 // Clean up phase did not resume at the frame that the search phase
668 return _URC_FATAL_PHASE2_ERROR;
671 /// Called by __cxa_throw. Only returns if there is a fatal error.
672 _LIBUNWIND_EXPORT _Unwind_Reason_Code
673 _Unwind_RaiseException(_Unwind_Exception *exception_object) {
674 _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
675 static_cast<void *>(exception_object));
678 __unw_getcontext(&uc);
680 // This field for is for compatibility with GCC to say this isn't a forced
681 // unwind. EHABI #7.2
682 exception_object->unwinder_cache.reserved1 = 0;
684 // phase 1: the search phase
685 _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
686 if (phase1 != _URC_NO_REASON)
689 // phase 2: the clean up phase
690 return unwind_phase2(&uc, &cursor, exception_object, false);
693 _LIBUNWIND_EXPORT void _Unwind_Complete(_Unwind_Exception* exception_object) {
694 // This is to be called when exception handling completes to give us a chance
695 // to perform any housekeeping. EHABI #7.2. But we have nothing to do here.
696 (void)exception_object;
699 /// When _Unwind_RaiseException() is in phase2, it hands control
700 /// to the personality function at each frame. The personality
701 /// may force a jump to a landing pad in that function, the landing
702 /// pad code may then call _Unwind_Resume() to continue with the
703 /// unwinding. Note: the call to _Unwind_Resume() is from compiler
704 /// geneated user code. All other _Unwind_* routines are called
705 /// by the C++ runtime __cxa_* routines.
707 /// Note: re-throwing an exception (as opposed to continuing the unwind)
708 /// is implemented by having the code call __cxa_rethrow() which
709 /// in turn calls _Unwind_Resume_or_Rethrow().
710 _LIBUNWIND_EXPORT void
711 _Unwind_Resume(_Unwind_Exception *exception_object) {
712 _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)",
713 static_cast<void *>(exception_object));
716 __unw_getcontext(&uc);
718 // _Unwind_RaiseException on EHABI will always set the reserved1 field to 0,
719 // which is in the same position as private_1 below.
720 // TODO(ajwong): Who wronte the above? Why is it true?
721 unwind_phase2(&uc, &cursor, exception_object, true);
723 // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
724 _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
727 /// Called by personality handler during phase 2 to get LSDA for current frame.
728 _LIBUNWIND_EXPORT uintptr_t
729 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
730 unw_cursor_t *cursor = (unw_cursor_t *)context;
731 unw_proc_info_t frameInfo;
732 uintptr_t result = 0;
733 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
734 result = (uintptr_t)frameInfo.lsda;
735 _LIBUNWIND_TRACE_API(
736 "_Unwind_GetLanguageSpecificData(context=%p) => 0x%llx",
737 static_cast<void *>(context), (long long)result);
741 static uint64_t ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation,
744 switch (representation) {
747 memcpy(&value, valuep, sizeof(uint32_t));
753 memcpy(&value, valuep, sizeof(uint64_t));
759 _LIBUNWIND_EXPORT _Unwind_VRS_Result
760 _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
761 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
763 _LIBUNWIND_TRACE_API("_Unwind_VRS_Set(context=%p, regclass=%d, reg=%d, "
764 "rep=%d, value=0x%llX)",
765 static_cast<void *>(context), regclass, regno,
767 ValueAsBitPattern(representation, valuep));
768 unw_cursor_t *cursor = (unw_cursor_t *)context;
771 if (representation != _UVRSD_UINT32 || regno > 15)
772 return _UVRSR_FAILED;
773 return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
774 *(unw_word_t *)valuep) == UNW_ESUCCESS
778 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
779 return _UVRSR_FAILED;
780 if (representation == _UVRSD_VFPX) {
781 // Can only touch d0-15 with FSTMFDX.
783 return _UVRSR_FAILED;
784 __unw_save_vfp_as_X(cursor);
787 return _UVRSR_FAILED;
789 return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
790 *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
793 #if defined(__ARM_WMMX)
795 if (representation != _UVRSD_UINT32 || regno > 3)
796 return _UVRSR_FAILED;
797 return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
798 *(unw_word_t *)valuep) == UNW_ESUCCESS
802 if (representation != _UVRSD_DOUBLE || regno > 31)
803 return _UVRSR_FAILED;
804 return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
805 *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
814 _LIBUNWIND_ABORT("unsupported register class");
817 static _Unwind_VRS_Result
818 _Unwind_VRS_Get_Internal(_Unwind_Context *context,
819 _Unwind_VRS_RegClass regclass, uint32_t regno,
820 _Unwind_VRS_DataRepresentation representation,
822 unw_cursor_t *cursor = (unw_cursor_t *)context;
825 if (representation != _UVRSD_UINT32 || regno > 15)
826 return _UVRSR_FAILED;
827 return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
828 (unw_word_t *)valuep) == UNW_ESUCCESS
832 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
833 return _UVRSR_FAILED;
834 if (representation == _UVRSD_VFPX) {
835 // Can only touch d0-15 with FSTMFDX.
837 return _UVRSR_FAILED;
838 __unw_save_vfp_as_X(cursor);
841 return _UVRSR_FAILED;
843 return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
844 (unw_fpreg_t *)valuep) == UNW_ESUCCESS
847 #if defined(__ARM_WMMX)
849 if (representation != _UVRSD_UINT32 || regno > 3)
850 return _UVRSR_FAILED;
851 return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
852 (unw_word_t *)valuep) == UNW_ESUCCESS
856 if (representation != _UVRSD_DOUBLE || regno > 31)
857 return _UVRSR_FAILED;
858 return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
859 (unw_fpreg_t *)valuep) == UNW_ESUCCESS
868 _LIBUNWIND_ABORT("unsupported register class");
871 _LIBUNWIND_EXPORT _Unwind_VRS_Result
872 _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
873 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
875 _Unwind_VRS_Result result =
876 _Unwind_VRS_Get_Internal(context, regclass, regno, representation,
878 _LIBUNWIND_TRACE_API("_Unwind_VRS_Get(context=%p, regclass=%d, reg=%d, "
879 "rep=%d, value=0x%llX, result = %d)",
880 static_cast<void *>(context), regclass, regno,
882 ValueAsBitPattern(representation, valuep), result);
887 _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
888 uint32_t discriminator,
889 _Unwind_VRS_DataRepresentation representation) {
890 _LIBUNWIND_TRACE_API("_Unwind_VRS_Pop(context=%p, regclass=%d, "
891 "discriminator=%d, representation=%d)",
892 static_cast<void *>(context), regclass, discriminator,
896 #if !defined(__ARM_WMMX)
900 if (representation != _UVRSD_UINT32)
901 return _UVRSR_FAILED;
902 // When popping SP from the stack, we don't want to override it from the
903 // computed new stack location. See EHABI #7.5.4 table 3.
904 bool poppedSP = false;
906 if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
907 _UVRSD_UINT32, &sp) != _UVRSR_OK) {
908 return _UVRSR_FAILED;
910 for (uint32_t i = 0; i < 16; ++i) {
911 if (!(discriminator & static_cast<uint32_t>(1 << i)))
913 uint32_t value = *sp++;
914 if (regclass == _UVRSC_CORE && i == 13)
916 if (_Unwind_VRS_Set(context, regclass, i,
917 _UVRSD_UINT32, &value) != _UVRSR_OK) {
918 return _UVRSR_FAILED;
922 return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP,
928 #if !defined(__ARM_WMMX)
932 if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
933 return _UVRSR_FAILED;
934 uint32_t first = discriminator >> 16;
935 uint32_t count = discriminator & 0xffff;
936 uint32_t end = first+count;
938 if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
939 _UVRSD_UINT32, &sp) != _UVRSR_OK) {
940 return _UVRSR_FAILED;
942 // For _UVRSD_VFPX, we're assuming the data is stored in FSTMX "standard
943 // format 1", which is equivalent to FSTMD + a padding word.
944 for (uint32_t i = first; i < end; ++i) {
945 // SP is only 32-bit aligned so don't copy 64-bit at a time.
948 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
949 uint64_t value = (w1 << 32) | w0;
950 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
951 uint64_t value = (w0 << 32) | w1;
953 #error "Unable to determine endianess"
955 if (_Unwind_VRS_Set(context, regclass, i, representation, &value) !=
957 return _UVRSR_FAILED;
959 if (representation == _UVRSD_VFPX)
961 return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
965 _LIBUNWIND_ABORT("unsupported register class");
968 /// Called by personality handler during phase 2 to find the start of the
970 _LIBUNWIND_EXPORT uintptr_t
971 _Unwind_GetRegionStart(struct _Unwind_Context *context) {
972 unw_cursor_t *cursor = (unw_cursor_t *)context;
973 unw_proc_info_t frameInfo;
974 uintptr_t result = 0;
975 if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
976 result = (uintptr_t)frameInfo.start_ip;
977 _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%llX",
978 static_cast<void *>(context), (long long)result);
983 /// Called by personality handler during phase 2 if a foreign exception
985 _LIBUNWIND_EXPORT void
986 _Unwind_DeleteException(_Unwind_Exception *exception_object) {
987 _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
988 static_cast<void *>(exception_object));
989 if (exception_object->exception_cleanup != NULL)
990 (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
994 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
995 __gnu_unwind_frame(_Unwind_Exception *exception_object,
996 struct _Unwind_Context *context) {
997 unw_cursor_t *cursor = (unw_cursor_t *)context;
998 if (__unw_step(cursor) != UNW_STEP_SUCCESS)
1003 #endif // defined(_LIBUNWIND_ARM_EHABI)