xref: /freebsd-src/contrib/llvm-project/libunwind/src/Unwind-EHABI.cpp (revision 8a4dda33d67586ca2624f2a38417baa03a533a7f)
1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //
80b57cec5SDimitry Andric //  Implements ARM zero-cost C++ exceptions
90b57cec5SDimitry Andric //
100b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "Unwind-EHABI.h"
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #if defined(_LIBUNWIND_ARM_EHABI)
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include <inttypes.h>
170b57cec5SDimitry Andric #include <stdbool.h>
180b57cec5SDimitry Andric #include <stdint.h>
190b57cec5SDimitry Andric #include <stdio.h>
200b57cec5SDimitry Andric #include <stdlib.h>
210b57cec5SDimitry Andric #include <string.h>
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric #include "config.h"
240b57cec5SDimitry Andric #include "libunwind.h"
250b57cec5SDimitry Andric #include "libunwind_ext.h"
260b57cec5SDimitry Andric #include "unwind.h"
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric namespace {
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric // Strange order: take words in order, but inside word, take from most to least
310b57cec5SDimitry Andric // signinficant byte.
getByte(const uint32_t * data,size_t offset)320b57cec5SDimitry Andric uint8_t getByte(const uint32_t* data, size_t offset) {
330b57cec5SDimitry Andric   const uint8_t* byteData = reinterpret_cast<const uint8_t*>(data);
345ffd83dbSDimitry Andric #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
350b57cec5SDimitry Andric   return byteData[(offset & ~(size_t)0x03) + (3 - (offset & (size_t)0x03))];
365ffd83dbSDimitry Andric #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
370b57cec5SDimitry Andric   return byteData[offset];
385ffd83dbSDimitry Andric #else
395ffd83dbSDimitry Andric #error "Unable to determine endianess"
400b57cec5SDimitry Andric #endif
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
getNextWord(const char * data,uint32_t * out)430b57cec5SDimitry Andric const char* getNextWord(const char* data, uint32_t* out) {
440b57cec5SDimitry Andric   *out = *reinterpret_cast<const uint32_t*>(data);
450b57cec5SDimitry Andric   return data + 4;
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric 
getNextNibble(const char * data,uint32_t * out)480b57cec5SDimitry Andric const char* getNextNibble(const char* data, uint32_t* out) {
490b57cec5SDimitry Andric   *out = *reinterpret_cast<const uint16_t*>(data);
500b57cec5SDimitry Andric   return data + 2;
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric struct Descriptor {
540b57cec5SDimitry Andric   // See # 9.2
550b57cec5SDimitry Andric   typedef enum {
560b57cec5SDimitry Andric     SU16 = 0, // Short descriptor, 16-bit entries
570b57cec5SDimitry Andric     LU16 = 1, // Long descriptor,  16-bit entries
580b57cec5SDimitry Andric     LU32 = 3, // Long descriptor,  32-bit entries
590b57cec5SDimitry Andric     RESERVED0 =  4, RESERVED1 =  5, RESERVED2  = 6,  RESERVED3  =  7,
600b57cec5SDimitry Andric     RESERVED4 =  8, RESERVED5 =  9, RESERVED6  = 10, RESERVED7  = 11,
610b57cec5SDimitry Andric     RESERVED8 = 12, RESERVED9 = 13, RESERVED10 = 14, RESERVED11 = 15
620b57cec5SDimitry Andric   } Format;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   // See # 9.2
650b57cec5SDimitry Andric   typedef enum {
660b57cec5SDimitry Andric     CLEANUP = 0x0,
670b57cec5SDimitry Andric     FUNC    = 0x1,
680b57cec5SDimitry Andric     CATCH   = 0x2,
690b57cec5SDimitry Andric     INVALID = 0x4
700b57cec5SDimitry Andric   } Kind;
710b57cec5SDimitry Andric };
720b57cec5SDimitry Andric 
ProcessDescriptors(_Unwind_State state,_Unwind_Control_Block * ucbp,struct _Unwind_Context * context,Descriptor::Format format,const char * descriptorStart,uint32_t flags)730b57cec5SDimitry Andric _Unwind_Reason_Code ProcessDescriptors(
740b57cec5SDimitry Andric     _Unwind_State state,
750b57cec5SDimitry Andric     _Unwind_Control_Block* ucbp,
760b57cec5SDimitry Andric     struct _Unwind_Context* context,
770b57cec5SDimitry Andric     Descriptor::Format format,
780b57cec5SDimitry Andric     const char* descriptorStart,
790b57cec5SDimitry Andric     uint32_t flags) {
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   // EHT is inlined in the index using compact form. No descriptors. #5
820b57cec5SDimitry Andric   if (flags & 0x1)
830b57cec5SDimitry Andric     return _URC_CONTINUE_UNWIND;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   // TODO: We should check the state here, and determine whether we need to
860b57cec5SDimitry Andric   // perform phase1 or phase2 unwinding.
870b57cec5SDimitry Andric   (void)state;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   const char* descriptor = descriptorStart;
900b57cec5SDimitry Andric   uint32_t descriptorWord;
910b57cec5SDimitry Andric   getNextWord(descriptor, &descriptorWord);
920b57cec5SDimitry Andric   while (descriptorWord) {
930b57cec5SDimitry Andric     // Read descriptor based on # 9.2.
940b57cec5SDimitry Andric     uint32_t length;
950b57cec5SDimitry Andric     uint32_t offset;
960b57cec5SDimitry Andric     switch (format) {
970b57cec5SDimitry Andric       case Descriptor::LU32:
980b57cec5SDimitry Andric         descriptor = getNextWord(descriptor, &length);
990b57cec5SDimitry Andric         descriptor = getNextWord(descriptor, &offset);
10069ade1e0SDimitry Andric         break;
1010b57cec5SDimitry Andric       case Descriptor::LU16:
1020b57cec5SDimitry Andric         descriptor = getNextNibble(descriptor, &length);
1030b57cec5SDimitry Andric         descriptor = getNextNibble(descriptor, &offset);
10469ade1e0SDimitry Andric         break;
1050b57cec5SDimitry Andric       default:
1060b57cec5SDimitry Andric         assert(false);
1070b57cec5SDimitry Andric         return _URC_FAILURE;
1080b57cec5SDimitry Andric     }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric     // See # 9.2 table for decoding the kind of descriptor. It's a 2-bit value.
1110b57cec5SDimitry Andric     Descriptor::Kind kind =
1120b57cec5SDimitry Andric         static_cast<Descriptor::Kind>((length & 0x1) | ((offset & 0x1) << 1));
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric     // Clear off flag from last bit.
1150b57cec5SDimitry Andric     length &= ~1u;
1160b57cec5SDimitry Andric     offset &= ~1u;
1170b57cec5SDimitry Andric     uintptr_t scopeStart = ucbp->pr_cache.fnstart + offset;
1180b57cec5SDimitry Andric     uintptr_t scopeEnd = scopeStart + length;
1190b57cec5SDimitry Andric     uintptr_t pc = _Unwind_GetIP(context);
1200b57cec5SDimitry Andric     bool isInScope = (scopeStart <= pc) && (pc < scopeEnd);
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric     switch (kind) {
1230b57cec5SDimitry Andric       case Descriptor::CLEANUP: {
1240b57cec5SDimitry Andric         // TODO(ajwong): Handle cleanup descriptors.
1250b57cec5SDimitry Andric         break;
1260b57cec5SDimitry Andric       }
1270b57cec5SDimitry Andric       case Descriptor::FUNC: {
1280b57cec5SDimitry Andric         // TODO(ajwong): Handle function descriptors.
1290b57cec5SDimitry Andric         break;
1300b57cec5SDimitry Andric       }
1310b57cec5SDimitry Andric       case Descriptor::CATCH: {
1320b57cec5SDimitry Andric         // Catch descriptors require gobbling one more word.
1330b57cec5SDimitry Andric         uint32_t landing_pad;
1340b57cec5SDimitry Andric         descriptor = getNextWord(descriptor, &landing_pad);
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric         if (isInScope) {
1370b57cec5SDimitry Andric           // TODO(ajwong): This is only phase1 compatible logic. Implement
1380b57cec5SDimitry Andric           // phase2.
1390b57cec5SDimitry Andric           landing_pad = signExtendPrel31(landing_pad & ~0x80000000);
1400b57cec5SDimitry Andric           if (landing_pad == 0xffffffff) {
1410b57cec5SDimitry Andric             return _URC_HANDLER_FOUND;
1420b57cec5SDimitry Andric           } else if (landing_pad == 0xfffffffe) {
1430b57cec5SDimitry Andric             return _URC_FAILURE;
1440b57cec5SDimitry Andric           } else {
1450b57cec5SDimitry Andric             /*
1460b57cec5SDimitry Andric             bool is_reference_type = landing_pad & 0x80000000;
1470b57cec5SDimitry Andric             void* matched_object;
1480b57cec5SDimitry Andric             if (__cxxabiv1::__cxa_type_match(
1490b57cec5SDimitry Andric                     ucbp, reinterpret_cast<const std::type_info *>(landing_pad),
1500b57cec5SDimitry Andric                     is_reference_type,
1510b57cec5SDimitry Andric                     &matched_object) != __cxxabiv1::ctm_failed)
1520b57cec5SDimitry Andric                 return _URC_HANDLER_FOUND;
1530b57cec5SDimitry Andric                 */
1540b57cec5SDimitry Andric             _LIBUNWIND_ABORT("Type matching not implemented");
1550b57cec5SDimitry Andric           }
1560b57cec5SDimitry Andric         }
1570b57cec5SDimitry Andric         break;
1580b57cec5SDimitry Andric       }
1590b57cec5SDimitry Andric       default:
1600b57cec5SDimitry Andric         _LIBUNWIND_ABORT("Invalid descriptor kind found.");
1610b57cec5SDimitry Andric     }
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric     getNextWord(descriptor, &descriptorWord);
1640b57cec5SDimitry Andric   }
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   return _URC_CONTINUE_UNWIND;
1670b57cec5SDimitry Andric }
1680b57cec5SDimitry Andric 
unwindOneFrame(_Unwind_State state,_Unwind_Control_Block * ucbp,struct _Unwind_Context * context)1690b57cec5SDimitry Andric static _Unwind_Reason_Code unwindOneFrame(_Unwind_State state,
1700b57cec5SDimitry Andric                                           _Unwind_Control_Block* ucbp,
1710b57cec5SDimitry Andric                                           struct _Unwind_Context* context) {
1720b57cec5SDimitry Andric   // Read the compact model EHT entry's header # 6.3
1730b57cec5SDimitry Andric   const uint32_t* unwindingData = ucbp->pr_cache.ehtp;
1740b57cec5SDimitry Andric   assert((*unwindingData & 0xf0000000) == 0x80000000 && "Must be a compact entry");
1750b57cec5SDimitry Andric   Descriptor::Format format =
1760b57cec5SDimitry Andric       static_cast<Descriptor::Format>((*unwindingData & 0x0f000000) >> 24);
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric   const char *lsda =
1790b57cec5SDimitry Andric       reinterpret_cast<const char *>(_Unwind_GetLanguageSpecificData(context));
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   // Handle descriptors before unwinding so they are processed in the context
1820b57cec5SDimitry Andric   // of the correct stack frame.
1830b57cec5SDimitry Andric   _Unwind_Reason_Code result =
1840b57cec5SDimitry Andric       ProcessDescriptors(state, ucbp, context, format, lsda,
1850b57cec5SDimitry Andric                          ucbp->pr_cache.additional);
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric   if (result != _URC_CONTINUE_UNWIND)
1880b57cec5SDimitry Andric     return result;
1890b57cec5SDimitry Andric 
1904824e7fdSDimitry Andric   switch (__unw_step(reinterpret_cast<unw_cursor_t *>(context))) {
1914824e7fdSDimitry Andric   case UNW_STEP_SUCCESS:
1920b57cec5SDimitry Andric     return _URC_CONTINUE_UNWIND;
1934824e7fdSDimitry Andric   case UNW_STEP_END:
1944824e7fdSDimitry Andric     return _URC_END_OF_STACK;
1954824e7fdSDimitry Andric   default:
1964824e7fdSDimitry Andric     return _URC_FAILURE;
1974824e7fdSDimitry Andric   }
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_CORE /
2010b57cec5SDimitry Andric // _UVRSD_UINT32.
RegisterMask(uint8_t start,uint8_t count_minus_one)2020b57cec5SDimitry Andric uint32_t RegisterMask(uint8_t start, uint8_t count_minus_one) {
2030b57cec5SDimitry Andric   return ((1U << (count_minus_one + 1)) - 1) << start;
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_VFP /
2070b57cec5SDimitry Andric // _UVRSD_DOUBLE.
RegisterRange(uint8_t start,uint8_t count_minus_one)2080b57cec5SDimitry Andric uint32_t RegisterRange(uint8_t start, uint8_t count_minus_one) {
2090b57cec5SDimitry Andric   return ((uint32_t)start << 16) | ((uint32_t)count_minus_one + 1);
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric } // end anonymous namespace
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric /**
2150b57cec5SDimitry Andric  * Decodes an EHT entry.
2160b57cec5SDimitry Andric  *
2170b57cec5SDimitry Andric  * @param data Pointer to EHT.
2180b57cec5SDimitry Andric  * @param[out] off Offset from return value (in bytes) to begin interpretation.
2190b57cec5SDimitry Andric  * @param[out] len Number of bytes in unwind code.
2200b57cec5SDimitry Andric  * @return Pointer to beginning of unwind code.
2210b57cec5SDimitry Andric  */
2220b57cec5SDimitry Andric extern "C" const uint32_t*
decode_eht_entry(const uint32_t * data,size_t * off,size_t * len)2230b57cec5SDimitry Andric decode_eht_entry(const uint32_t* data, size_t* off, size_t* len) {
2240b57cec5SDimitry Andric   if ((*data & 0x80000000) == 0) {
2250b57cec5SDimitry Andric     // 6.2: Generic Model
2260b57cec5SDimitry Andric     //
2270b57cec5SDimitry Andric     // EHT entry is a prel31 pointing to the PR, followed by data understood
2280b57cec5SDimitry Andric     // only by the personality routine. Fortunately, all existing assembler
2290b57cec5SDimitry Andric     // implementations, including GNU assembler, LLVM integrated assembler,
2300b57cec5SDimitry Andric     // and ARM assembler, assume that the unwind opcodes come after the
2310b57cec5SDimitry Andric     // personality rountine address.
2320b57cec5SDimitry Andric     *off = 1; // First byte is size data.
2330b57cec5SDimitry Andric     *len = (((data[1] >> 24) & 0xff) + 1) * 4;
2340b57cec5SDimitry Andric     data++; // Skip the first word, which is the prel31 offset.
2350b57cec5SDimitry Andric   } else {
2360b57cec5SDimitry Andric     // 6.3: ARM Compact Model
2370b57cec5SDimitry Andric     //
238bdd1243dSDimitry Andric     // EHT entries here correspond to the __aeabi_unwind_cpp_pr[012] PRs indeed
2390b57cec5SDimitry Andric     // by format:
2400b57cec5SDimitry Andric     Descriptor::Format format =
2410b57cec5SDimitry Andric         static_cast<Descriptor::Format>((*data & 0x0f000000) >> 24);
2420b57cec5SDimitry Andric     switch (format) {
2430b57cec5SDimitry Andric       case Descriptor::SU16:
2440b57cec5SDimitry Andric         *len = 4;
2450b57cec5SDimitry Andric         *off = 1;
2460b57cec5SDimitry Andric         break;
2470b57cec5SDimitry Andric       case Descriptor::LU16:
2480b57cec5SDimitry Andric       case Descriptor::LU32:
2490b57cec5SDimitry Andric         *len = 4 + 4 * ((*data & 0x00ff0000) >> 16);
2500b57cec5SDimitry Andric         *off = 2;
2510b57cec5SDimitry Andric         break;
2520b57cec5SDimitry Andric       default:
2530b57cec5SDimitry Andric         return nullptr;
2540b57cec5SDimitry Andric     }
2550b57cec5SDimitry Andric   }
2560b57cec5SDimitry Andric   return data;
2570b57cec5SDimitry Andric }
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_VRS_Interpret(_Unwind_Context * context,const uint32_t * data,size_t offset,size_t len)2600b57cec5SDimitry Andric _Unwind_VRS_Interpret(_Unwind_Context *context, const uint32_t *data,
2610b57cec5SDimitry Andric                       size_t offset, size_t len) {
2620b57cec5SDimitry Andric   bool wrotePC = false;
2630b57cec5SDimitry Andric   bool finish = false;
2640eae32dcSDimitry Andric   bool hasReturnAddrAuthCode = false;
2650b57cec5SDimitry Andric   while (offset < len && !finish) {
2660b57cec5SDimitry Andric     uint8_t byte = getByte(data, offset++);
2670b57cec5SDimitry Andric     if ((byte & 0x80) == 0) {
2680b57cec5SDimitry Andric       uint32_t sp;
2690b57cec5SDimitry Andric       _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
2700b57cec5SDimitry Andric       if (byte & 0x40)
2710b57cec5SDimitry Andric         sp -= (((uint32_t)byte & 0x3f) << 2) + 4;
2720b57cec5SDimitry Andric       else
2730b57cec5SDimitry Andric         sp += ((uint32_t)byte << 2) + 4;
2740b57cec5SDimitry Andric       _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
2750b57cec5SDimitry Andric     } else {
2760b57cec5SDimitry Andric       switch (byte & 0xf0) {
2770b57cec5SDimitry Andric         case 0x80: {
2780b57cec5SDimitry Andric           if (offset >= len)
2790b57cec5SDimitry Andric             return _URC_FAILURE;
2800b57cec5SDimitry Andric           uint32_t registers =
2810b57cec5SDimitry Andric               (((uint32_t)byte & 0x0f) << 12) |
2820b57cec5SDimitry Andric               (((uint32_t)getByte(data, offset++)) << 4);
2830b57cec5SDimitry Andric           if (!registers)
2840b57cec5SDimitry Andric             return _URC_FAILURE;
2850b57cec5SDimitry Andric           if (registers & (1 << 15))
2860b57cec5SDimitry Andric             wrotePC = true;
2870b57cec5SDimitry Andric           _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
2880b57cec5SDimitry Andric           break;
2890b57cec5SDimitry Andric         }
2900b57cec5SDimitry Andric         case 0x90: {
2910b57cec5SDimitry Andric           uint8_t reg = byte & 0x0f;
2920b57cec5SDimitry Andric           if (reg == 13 || reg == 15)
2930b57cec5SDimitry Andric             return _URC_FAILURE;
2940b57cec5SDimitry Andric           uint32_t sp;
2950b57cec5SDimitry Andric           _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_R0 + reg,
2960b57cec5SDimitry Andric                           _UVRSD_UINT32, &sp);
2970b57cec5SDimitry Andric           _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
2980b57cec5SDimitry Andric                           &sp);
2990b57cec5SDimitry Andric           break;
3000b57cec5SDimitry Andric         }
3010b57cec5SDimitry Andric         case 0xa0: {
3020b57cec5SDimitry Andric           uint32_t registers = RegisterMask(4, byte & 0x07);
3030b57cec5SDimitry Andric           if (byte & 0x08)
3040b57cec5SDimitry Andric             registers |= 1 << 14;
3050b57cec5SDimitry Andric           _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
3060b57cec5SDimitry Andric           break;
3070b57cec5SDimitry Andric         }
3080b57cec5SDimitry Andric         case 0xb0: {
3090b57cec5SDimitry Andric           switch (byte) {
3100b57cec5SDimitry Andric             case 0xb0:
3110b57cec5SDimitry Andric               finish = true;
3120b57cec5SDimitry Andric               break;
3130b57cec5SDimitry Andric             case 0xb1: {
3140b57cec5SDimitry Andric               if (offset >= len)
3150b57cec5SDimitry Andric                 return _URC_FAILURE;
3160b57cec5SDimitry Andric               uint8_t registers = getByte(data, offset++);
3170b57cec5SDimitry Andric               if (registers & 0xf0 || !registers)
3180b57cec5SDimitry Andric                 return _URC_FAILURE;
3190b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
3200b57cec5SDimitry Andric               break;
3210b57cec5SDimitry Andric             }
3220b57cec5SDimitry Andric             case 0xb2: {
3230b57cec5SDimitry Andric               uint32_t addend = 0;
3240b57cec5SDimitry Andric               uint32_t shift = 0;
3250b57cec5SDimitry Andric               // This decodes a uleb128 value.
3260b57cec5SDimitry Andric               while (true) {
3270b57cec5SDimitry Andric                 if (offset >= len)
3280b57cec5SDimitry Andric                   return _URC_FAILURE;
3290b57cec5SDimitry Andric                 uint32_t v = getByte(data, offset++);
3300b57cec5SDimitry Andric                 addend |= (v & 0x7f) << shift;
3310b57cec5SDimitry Andric                 if ((v & 0x80) == 0)
3320b57cec5SDimitry Andric                   break;
3330b57cec5SDimitry Andric                 shift += 7;
3340b57cec5SDimitry Andric               }
3350b57cec5SDimitry Andric               uint32_t sp;
3360b57cec5SDimitry Andric               _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
3370b57cec5SDimitry Andric                               &sp);
3380b57cec5SDimitry Andric               sp += 0x204 + (addend << 2);
3390b57cec5SDimitry Andric               _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
3400b57cec5SDimitry Andric                               &sp);
3410b57cec5SDimitry Andric               break;
3420b57cec5SDimitry Andric             }
3430b57cec5SDimitry Andric             case 0xb3: {
3440b57cec5SDimitry Andric               uint8_t v = getByte(data, offset++);
3450b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_VFP,
3460b57cec5SDimitry Andric                               RegisterRange(static_cast<uint8_t>(v >> 4),
3470b57cec5SDimitry Andric                                             v & 0x0f), _UVRSD_VFPX);
3480b57cec5SDimitry Andric               break;
3490b57cec5SDimitry Andric             }
3500b57cec5SDimitry Andric             case 0xb4:
3510eae32dcSDimitry Andric               hasReturnAddrAuthCode = true;
3520eae32dcSDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_PSEUDO,
3530eae32dcSDimitry Andric                               0 /* Return Address Auth Code */, _UVRSD_UINT32);
3540eae32dcSDimitry Andric               break;
3550b57cec5SDimitry Andric             case 0xb5:
3560b57cec5SDimitry Andric             case 0xb6:
3570b57cec5SDimitry Andric             case 0xb7:
3580b57cec5SDimitry Andric               return _URC_FAILURE;
3590b57cec5SDimitry Andric             default:
3600b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_VFP,
3610b57cec5SDimitry Andric                               RegisterRange(8, byte & 0x07), _UVRSD_VFPX);
3620b57cec5SDimitry Andric               break;
3630b57cec5SDimitry Andric           }
3640b57cec5SDimitry Andric           break;
3650b57cec5SDimitry Andric         }
3660b57cec5SDimitry Andric         case 0xc0: {
3670b57cec5SDimitry Andric           switch (byte) {
3680b57cec5SDimitry Andric #if defined(__ARM_WMMX)
3690b57cec5SDimitry Andric             case 0xc0:
3700b57cec5SDimitry Andric             case 0xc1:
3710b57cec5SDimitry Andric             case 0xc2:
3720b57cec5SDimitry Andric             case 0xc3:
3730b57cec5SDimitry Andric             case 0xc4:
3740b57cec5SDimitry Andric             case 0xc5:
3750b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
3760b57cec5SDimitry Andric                               RegisterRange(10, byte & 0x7), _UVRSD_DOUBLE);
3770b57cec5SDimitry Andric               break;
3780b57cec5SDimitry Andric             case 0xc6: {
3790b57cec5SDimitry Andric               uint8_t v = getByte(data, offset++);
3800b57cec5SDimitry Andric               uint8_t start = static_cast<uint8_t>(v >> 4);
3810b57cec5SDimitry Andric               uint8_t count_minus_one = v & 0xf;
3820b57cec5SDimitry Andric               if (start + count_minus_one >= 16)
3830b57cec5SDimitry Andric                 return _URC_FAILURE;
3840b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
3850b57cec5SDimitry Andric                               RegisterRange(start, count_minus_one),
3860b57cec5SDimitry Andric                               _UVRSD_DOUBLE);
3870b57cec5SDimitry Andric               break;
3880b57cec5SDimitry Andric             }
3890b57cec5SDimitry Andric             case 0xc7: {
3900b57cec5SDimitry Andric               uint8_t v = getByte(data, offset++);
3910b57cec5SDimitry Andric               if (!v || v & 0xf0)
3920b57cec5SDimitry Andric                 return _URC_FAILURE;
3930b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_WMMXC, v, _UVRSD_DOUBLE);
3940b57cec5SDimitry Andric               break;
3950b57cec5SDimitry Andric             }
3960b57cec5SDimitry Andric #endif
3970b57cec5SDimitry Andric             case 0xc8:
3980b57cec5SDimitry Andric             case 0xc9: {
3990b57cec5SDimitry Andric               uint8_t v = getByte(data, offset++);
4000b57cec5SDimitry Andric               uint8_t start =
4010b57cec5SDimitry Andric                   static_cast<uint8_t>(((byte == 0xc8) ? 16 : 0) + (v >> 4));
4020b57cec5SDimitry Andric               uint8_t count_minus_one = v & 0xf;
4030b57cec5SDimitry Andric               if (start + count_minus_one >= 32)
4040b57cec5SDimitry Andric                 return _URC_FAILURE;
4050b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_VFP,
4060b57cec5SDimitry Andric                               RegisterRange(start, count_minus_one),
4070b57cec5SDimitry Andric                               _UVRSD_DOUBLE);
4080b57cec5SDimitry Andric               break;
4090b57cec5SDimitry Andric             }
4100b57cec5SDimitry Andric             default:
4110b57cec5SDimitry Andric               return _URC_FAILURE;
4120b57cec5SDimitry Andric           }
4130b57cec5SDimitry Andric           break;
4140b57cec5SDimitry Andric         }
4150b57cec5SDimitry Andric         case 0xd0: {
4160b57cec5SDimitry Andric           if (byte & 0x08)
4170b57cec5SDimitry Andric             return _URC_FAILURE;
4180b57cec5SDimitry Andric           _Unwind_VRS_Pop(context, _UVRSC_VFP, RegisterRange(8, byte & 0x7),
4190b57cec5SDimitry Andric                           _UVRSD_DOUBLE);
4200b57cec5SDimitry Andric           break;
4210b57cec5SDimitry Andric         }
4220b57cec5SDimitry Andric         default:
4230b57cec5SDimitry Andric           return _URC_FAILURE;
4240b57cec5SDimitry Andric       }
4250b57cec5SDimitry Andric     }
4260b57cec5SDimitry Andric   }
4270b57cec5SDimitry Andric   if (!wrotePC) {
4280b57cec5SDimitry Andric     uint32_t lr;
4290b57cec5SDimitry Andric     _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_LR, _UVRSD_UINT32, &lr);
4300eae32dcSDimitry Andric #ifdef __ARM_FEATURE_PAUTH
4310eae32dcSDimitry Andric     if (hasReturnAddrAuthCode) {
4320eae32dcSDimitry Andric       uint32_t sp;
4330eae32dcSDimitry Andric       uint32_t pac;
4340eae32dcSDimitry Andric       _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
43581ad6265SDimitry Andric       _Unwind_VRS_Get(context, _UVRSC_PSEUDO, 0, _UVRSD_UINT32, &pac);
4360eae32dcSDimitry Andric       __asm__ __volatile__("autg %0, %1, %2" : : "r"(pac), "r"(lr), "r"(sp) :);
4370eae32dcSDimitry Andric     }
43881ad6265SDimitry Andric #else
43981ad6265SDimitry Andric     (void)hasReturnAddrAuthCode;
4400eae32dcSDimitry Andric #endif
4410b57cec5SDimitry Andric     _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_IP, _UVRSD_UINT32, &lr);
4420b57cec5SDimitry Andric   }
4430b57cec5SDimitry Andric   return _URC_CONTINUE_UNWIND;
4440b57cec5SDimitry Andric }
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
__aeabi_unwind_cpp_pr0(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context)4470b57cec5SDimitry Andric __aeabi_unwind_cpp_pr0(_Unwind_State state, _Unwind_Control_Block *ucbp,
4480b57cec5SDimitry Andric                        _Unwind_Context *context) {
4490b57cec5SDimitry Andric   return unwindOneFrame(state, ucbp, context);
4500b57cec5SDimitry Andric }
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
__aeabi_unwind_cpp_pr1(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context)4530b57cec5SDimitry Andric __aeabi_unwind_cpp_pr1(_Unwind_State state, _Unwind_Control_Block *ucbp,
4540b57cec5SDimitry Andric                        _Unwind_Context *context) {
4550b57cec5SDimitry Andric   return unwindOneFrame(state, ucbp, context);
4560b57cec5SDimitry Andric }
4570b57cec5SDimitry Andric 
4580b57cec5SDimitry Andric extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
__aeabi_unwind_cpp_pr2(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context)4590b57cec5SDimitry Andric __aeabi_unwind_cpp_pr2(_Unwind_State state, _Unwind_Control_Block *ucbp,
4600b57cec5SDimitry Andric                        _Unwind_Context *context) {
4610b57cec5SDimitry Andric   return unwindOneFrame(state, ucbp, context);
4620b57cec5SDimitry Andric }
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric static _Unwind_Reason_Code
unwind_phase1(unw_context_t * uc,unw_cursor_t * cursor,_Unwind_Exception * exception_object)4650b57cec5SDimitry Andric unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
4660b57cec5SDimitry Andric   // EHABI #7.3 discusses preserving the VRS in a "temporary VRS" during
4670b57cec5SDimitry Andric   // phase 1 and then restoring it to the "primary VRS" for phase 2. The
4680b57cec5SDimitry Andric   // effect is phase 2 doesn't see any of the VRS manipulations from phase 1.
4690b57cec5SDimitry Andric   // In this implementation, the phases don't share the VRS backing store.
4700b57cec5SDimitry Andric   // Instead, they are passed the original |uc| and they create a new VRS
4710b57cec5SDimitry Andric   // from scratch thus achieving the same effect.
4720b57cec5SDimitry Andric   __unw_init_local(cursor, uc);
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric   // Walk each frame looking for a place to stop.
4750b57cec5SDimitry Andric   for (bool handlerNotFound = true; handlerNotFound;) {
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric     // See if frame has code to run (has personality routine).
4780b57cec5SDimitry Andric     unw_proc_info_t frameInfo;
4790b57cec5SDimitry Andric     if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
4800b57cec5SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
4810b57cec5SDimitry Andric           "unwind_phase1(ex_ojb=%p): __unw_get_proc_info "
4820b57cec5SDimitry Andric           "failed => _URC_FATAL_PHASE1_ERROR",
4830b57cec5SDimitry Andric           static_cast<void *>(exception_object));
4840b57cec5SDimitry Andric       return _URC_FATAL_PHASE1_ERROR;
4850b57cec5SDimitry Andric     }
4860b57cec5SDimitry Andric 
487349cc55cSDimitry Andric #ifndef NDEBUG
4880b57cec5SDimitry Andric     // When tracing, print state information.
4890b57cec5SDimitry Andric     if (_LIBUNWIND_TRACING_UNWINDING) {
4900b57cec5SDimitry Andric       char functionBuf[512];
4910b57cec5SDimitry Andric       const char *functionName = functionBuf;
4920b57cec5SDimitry Andric       unw_word_t offset;
4930b57cec5SDimitry Andric       if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
4940b57cec5SDimitry Andric                                &offset) != UNW_ESUCCESS) ||
4950b57cec5SDimitry Andric           (frameInfo.start_ip + offset > frameInfo.end_ip))
4960b57cec5SDimitry Andric         functionName = ".anonymous.";
4970b57cec5SDimitry Andric       unw_word_t pc;
4980b57cec5SDimitry Andric       __unw_get_reg(cursor, UNW_REG_IP, &pc);
4990b57cec5SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
5000b57cec5SDimitry Andric           "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR ", func=%s, "
5010b57cec5SDimitry Andric           "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
5020b57cec5SDimitry Andric           static_cast<void *>(exception_object), pc,
5030b57cec5SDimitry Andric           frameInfo.start_ip, functionName,
5040b57cec5SDimitry Andric           frameInfo.lsda, frameInfo.handler);
5050b57cec5SDimitry Andric     }
506349cc55cSDimitry Andric #endif
5070b57cec5SDimitry Andric 
5080b57cec5SDimitry Andric     // If there is a personality routine, ask it if it will want to stop at
5090b57cec5SDimitry Andric     // this frame.
5100b57cec5SDimitry Andric     if (frameInfo.handler != 0) {
5115ffd83dbSDimitry Andric       _Unwind_Personality_Fn p =
5125ffd83dbSDimitry Andric           (_Unwind_Personality_Fn)(long)(frameInfo.handler);
5130b57cec5SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
5140b57cec5SDimitry Andric           "unwind_phase1(ex_ojb=%p): calling personality function %p",
5150b57cec5SDimitry Andric           static_cast<void *>(exception_object),
5160b57cec5SDimitry Andric           reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(p)));
5170b57cec5SDimitry Andric       struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
5180b57cec5SDimitry Andric       exception_object->pr_cache.fnstart = frameInfo.start_ip;
5190b57cec5SDimitry Andric       exception_object->pr_cache.ehtp =
5200b57cec5SDimitry Andric           (_Unwind_EHT_Header *)frameInfo.unwind_info;
5210b57cec5SDimitry Andric       exception_object->pr_cache.additional = frameInfo.flags;
5220b57cec5SDimitry Andric       _Unwind_Reason_Code personalityResult =
5230b57cec5SDimitry Andric           (*p)(_US_VIRTUAL_UNWIND_FRAME, exception_object, context);
5240b57cec5SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
5250b57cec5SDimitry Andric           "unwind_phase1(ex_ojb=%p): personality result %d start_ip %x ehtp %p "
5260b57cec5SDimitry Andric           "additional %x",
5270b57cec5SDimitry Andric           static_cast<void *>(exception_object), personalityResult,
5280b57cec5SDimitry Andric           exception_object->pr_cache.fnstart,
5290b57cec5SDimitry Andric           static_cast<void *>(exception_object->pr_cache.ehtp),
5300b57cec5SDimitry Andric           exception_object->pr_cache.additional);
5310b57cec5SDimitry Andric       switch (personalityResult) {
5320b57cec5SDimitry Andric       case _URC_HANDLER_FOUND:
5330b57cec5SDimitry Andric         // found a catch clause or locals that need destructing in this frame
5340b57cec5SDimitry Andric         // stop search and remember stack pointer at the frame
5350b57cec5SDimitry Andric         handlerNotFound = false;
5360b57cec5SDimitry Andric         // p should have initialized barrier_cache. EHABI #7.3.5
5370b57cec5SDimitry Andric         _LIBUNWIND_TRACE_UNWINDING(
5380b57cec5SDimitry Andric             "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
5390b57cec5SDimitry Andric             static_cast<void *>(exception_object));
5400b57cec5SDimitry Andric         return _URC_NO_REASON;
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric       case _URC_CONTINUE_UNWIND:
5430b57cec5SDimitry Andric         _LIBUNWIND_TRACE_UNWINDING(
5440b57cec5SDimitry Andric             "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
5450b57cec5SDimitry Andric             static_cast<void *>(exception_object));
5460b57cec5SDimitry Andric         // continue unwinding
5470b57cec5SDimitry Andric         break;
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric       // EHABI #7.3.3
5500b57cec5SDimitry Andric       case _URC_FAILURE:
5510b57cec5SDimitry Andric         return _URC_FAILURE;
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric       default:
5540b57cec5SDimitry Andric         // something went wrong
5550b57cec5SDimitry Andric         _LIBUNWIND_TRACE_UNWINDING(
5560b57cec5SDimitry Andric             "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
5570b57cec5SDimitry Andric             static_cast<void *>(exception_object));
5580b57cec5SDimitry Andric         return _URC_FATAL_PHASE1_ERROR;
5590b57cec5SDimitry Andric       }
5600b57cec5SDimitry Andric     }
5610b57cec5SDimitry Andric   }
5620b57cec5SDimitry Andric   return _URC_NO_REASON;
5630b57cec5SDimitry Andric }
5640b57cec5SDimitry Andric 
unwind_phase2(unw_context_t * uc,unw_cursor_t * cursor,_Unwind_Exception * exception_object,bool resume)5650b57cec5SDimitry Andric static _Unwind_Reason_Code unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor,
5660b57cec5SDimitry Andric                                          _Unwind_Exception *exception_object,
5670b57cec5SDimitry Andric                                          bool resume) {
5680b57cec5SDimitry Andric   // See comment at the start of unwind_phase1 regarding VRS integrity.
5690b57cec5SDimitry Andric   __unw_init_local(cursor, uc);
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric   _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
5720b57cec5SDimitry Andric                              static_cast<void *>(exception_object));
5730b57cec5SDimitry Andric   int frame_count = 0;
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric   // Walk each frame until we reach where search phase said to stop.
5760b57cec5SDimitry Andric   while (true) {
5770b57cec5SDimitry Andric     // Ask libunwind to get next frame (skip over first which is
5780b57cec5SDimitry Andric     // _Unwind_RaiseException or _Unwind_Resume).
5790b57cec5SDimitry Andric     //
5800b57cec5SDimitry Andric     // Resume only ever makes sense for 1 frame.
5810b57cec5SDimitry Andric     _Unwind_State state =
5820b57cec5SDimitry Andric         resume ? _US_UNWIND_FRAME_RESUME : _US_UNWIND_FRAME_STARTING;
5830b57cec5SDimitry Andric     if (resume && frame_count == 1) {
5840b57cec5SDimitry Andric       // On a resume, first unwind the _Unwind_Resume() frame. The next frame
5850b57cec5SDimitry Andric       // is now the landing pad for the cleanup from a previous execution of
5860b57cec5SDimitry Andric       // phase2. To continue unwindingly correctly, replace VRS[15] with the
5870b57cec5SDimitry Andric       // IP of the frame that the previous run of phase2 installed the context
5880b57cec5SDimitry Andric       // for. After this, continue unwinding as if normal.
5890b57cec5SDimitry Andric       //
5900b57cec5SDimitry Andric       // See #7.4.6 for details.
5910b57cec5SDimitry Andric       __unw_set_reg(cursor, UNW_REG_IP,
5920b57cec5SDimitry Andric                     exception_object->unwinder_cache.reserved2);
5930b57cec5SDimitry Andric       resume = false;
5940b57cec5SDimitry Andric     }
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric     // Get info about this frame.
5970b57cec5SDimitry Andric     unw_word_t sp;
5980b57cec5SDimitry Andric     unw_proc_info_t frameInfo;
5990b57cec5SDimitry Andric     __unw_get_reg(cursor, UNW_REG_SP, &sp);
6000b57cec5SDimitry Andric     if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
6010b57cec5SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
6020b57cec5SDimitry Andric           "unwind_phase2(ex_ojb=%p): __unw_get_proc_info "
6030b57cec5SDimitry Andric           "failed => _URC_FATAL_PHASE2_ERROR",
6040b57cec5SDimitry Andric           static_cast<void *>(exception_object));
6050b57cec5SDimitry Andric       return _URC_FATAL_PHASE2_ERROR;
6060b57cec5SDimitry Andric     }
6070b57cec5SDimitry Andric 
608349cc55cSDimitry Andric #ifndef NDEBUG
6090b57cec5SDimitry Andric     // When tracing, print state information.
6100b57cec5SDimitry Andric     if (_LIBUNWIND_TRACING_UNWINDING) {
6110b57cec5SDimitry Andric       char functionBuf[512];
6120b57cec5SDimitry Andric       const char *functionName = functionBuf;
6130b57cec5SDimitry Andric       unw_word_t offset;
6140b57cec5SDimitry Andric       if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
6150b57cec5SDimitry Andric                                &offset) != UNW_ESUCCESS) ||
6160b57cec5SDimitry Andric           (frameInfo.start_ip + offset > frameInfo.end_ip))
6170b57cec5SDimitry Andric         functionName = ".anonymous.";
6180b57cec5SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
6190b57cec5SDimitry Andric           "unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR ", func=%s, sp=0x%" PRIxPTR ", "
6200b57cec5SDimitry Andric           "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
6210b57cec5SDimitry Andric           static_cast<void *>(exception_object), frameInfo.start_ip,
6220b57cec5SDimitry Andric           functionName, sp, frameInfo.lsda,
6230b57cec5SDimitry Andric           frameInfo.handler);
6240b57cec5SDimitry Andric     }
625349cc55cSDimitry Andric #endif
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric     // If there is a personality routine, tell it we are unwinding.
6280b57cec5SDimitry Andric     if (frameInfo.handler != 0) {
6295ffd83dbSDimitry Andric       _Unwind_Personality_Fn p =
630349cc55cSDimitry Andric           (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
6310b57cec5SDimitry Andric       struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
6320b57cec5SDimitry Andric       // EHABI #7.2
6330b57cec5SDimitry Andric       exception_object->pr_cache.fnstart = frameInfo.start_ip;
6340b57cec5SDimitry Andric       exception_object->pr_cache.ehtp =
6350b57cec5SDimitry Andric           (_Unwind_EHT_Header *)frameInfo.unwind_info;
6360b57cec5SDimitry Andric       exception_object->pr_cache.additional = frameInfo.flags;
6370b57cec5SDimitry Andric       _Unwind_Reason_Code personalityResult =
6380b57cec5SDimitry Andric           (*p)(state, exception_object, context);
6390b57cec5SDimitry Andric       switch (personalityResult) {
6400b57cec5SDimitry Andric       case _URC_CONTINUE_UNWIND:
6410b57cec5SDimitry Andric         // Continue unwinding
6420b57cec5SDimitry Andric         _LIBUNWIND_TRACE_UNWINDING(
6430b57cec5SDimitry Andric             "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
6440b57cec5SDimitry Andric             static_cast<void *>(exception_object));
6450b57cec5SDimitry Andric         // EHABI #7.2
6460b57cec5SDimitry Andric         if (sp == exception_object->barrier_cache.sp) {
6470b57cec5SDimitry Andric           // Phase 1 said we would stop at this frame, but we did not...
6480b57cec5SDimitry Andric           _LIBUNWIND_ABORT("during phase1 personality function said it would "
6490b57cec5SDimitry Andric                            "stop here, but now in phase2 it did not stop here");
6500b57cec5SDimitry Andric         }
6510b57cec5SDimitry Andric         break;
6520b57cec5SDimitry Andric       case _URC_INSTALL_CONTEXT:
6530b57cec5SDimitry Andric         _LIBUNWIND_TRACE_UNWINDING(
6540b57cec5SDimitry Andric             "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
6550b57cec5SDimitry Andric             static_cast<void *>(exception_object));
6560b57cec5SDimitry Andric         // Personality routine says to transfer control to landing pad.
6570b57cec5SDimitry Andric         // We may get control back if landing pad calls _Unwind_Resume().
6580b57cec5SDimitry Andric         if (_LIBUNWIND_TRACING_UNWINDING) {
6590b57cec5SDimitry Andric           unw_word_t pc;
6600b57cec5SDimitry Andric           __unw_get_reg(cursor, UNW_REG_IP, &pc);
6610b57cec5SDimitry Andric           __unw_get_reg(cursor, UNW_REG_SP, &sp);
6620b57cec5SDimitry Andric           _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
6630b57cec5SDimitry Andric                                      "user code with ip=0x%" PRIxPTR ", sp=0x%" PRIxPTR,
6640b57cec5SDimitry Andric                                      static_cast<void *>(exception_object),
6650b57cec5SDimitry Andric                                      pc, sp);
6660b57cec5SDimitry Andric         }
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric         {
6690b57cec5SDimitry Andric           // EHABI #7.4.1 says we need to preserve pc for when _Unwind_Resume
6700b57cec5SDimitry Andric           // is called back, to find this same frame.
6710b57cec5SDimitry Andric           unw_word_t pc;
6720b57cec5SDimitry Andric           __unw_get_reg(cursor, UNW_REG_IP, &pc);
6730b57cec5SDimitry Andric           exception_object->unwinder_cache.reserved2 = (uint32_t)pc;
6740b57cec5SDimitry Andric         }
6750b57cec5SDimitry Andric         __unw_resume(cursor);
6760b57cec5SDimitry Andric         // __unw_resume() only returns if there was an error.
6770b57cec5SDimitry Andric         return _URC_FATAL_PHASE2_ERROR;
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric       // # EHABI #7.4.3
6800b57cec5SDimitry Andric       case _URC_FAILURE:
6810b57cec5SDimitry Andric         abort();
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric       default:
6840b57cec5SDimitry Andric         // Personality routine returned an unknown result code.
6850b57cec5SDimitry Andric         _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
6860b57cec5SDimitry Andric                       personalityResult);
6870b57cec5SDimitry Andric         return _URC_FATAL_PHASE2_ERROR;
6880b57cec5SDimitry Andric       }
6890b57cec5SDimitry Andric     }
6900b57cec5SDimitry Andric     frame_count++;
6910b57cec5SDimitry Andric   }
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric   // Clean up phase did not resume at the frame that the search phase
6940b57cec5SDimitry Andric   // said it would...
6950b57cec5SDimitry Andric   return _URC_FATAL_PHASE2_ERROR;
6960b57cec5SDimitry Andric }
6970b57cec5SDimitry Andric 
698349cc55cSDimitry Andric static _Unwind_Reason_Code
unwind_phase2_forced(unw_context_t * uc,unw_cursor_t * cursor,_Unwind_Exception * exception_object,_Unwind_Stop_Fn stop,void * stop_parameter)699349cc55cSDimitry Andric unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
700349cc55cSDimitry Andric                      _Unwind_Exception *exception_object, _Unwind_Stop_Fn stop,
701349cc55cSDimitry Andric                      void *stop_parameter) {
7024824e7fdSDimitry Andric   bool endOfStack = false;
703349cc55cSDimitry Andric   // See comment at the start of unwind_phase1 regarding VRS integrity.
704349cc55cSDimitry Andric   __unw_init_local(cursor, uc);
705349cc55cSDimitry Andric   _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_force(ex_ojb=%p)",
706349cc55cSDimitry Andric                              static_cast<void *>(exception_object));
707349cc55cSDimitry Andric   // Walk each frame until we reach where search phase said to stop
7084824e7fdSDimitry Andric   while (!endOfStack) {
709349cc55cSDimitry Andric     // Update info about this frame.
710349cc55cSDimitry Andric     unw_proc_info_t frameInfo;
711349cc55cSDimitry Andric     if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
71206c3fb27SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_get_proc_info "
713349cc55cSDimitry Andric                                  "failed => _URC_END_OF_STACK",
714349cc55cSDimitry Andric                                  (void *)exception_object);
715349cc55cSDimitry Andric       return _URC_FATAL_PHASE2_ERROR;
716349cc55cSDimitry Andric     }
717349cc55cSDimitry Andric 
718349cc55cSDimitry Andric #ifndef NDEBUG
719349cc55cSDimitry Andric     // When tracing, print state information.
720349cc55cSDimitry Andric     if (_LIBUNWIND_TRACING_UNWINDING) {
721349cc55cSDimitry Andric       char functionBuf[512];
722349cc55cSDimitry Andric       const char *functionName = functionBuf;
723349cc55cSDimitry Andric       unw_word_t offset;
724349cc55cSDimitry Andric       if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
725349cc55cSDimitry Andric                                &offset) != UNW_ESUCCESS) ||
726349cc55cSDimitry Andric           (frameInfo.start_ip + offset > frameInfo.end_ip))
727349cc55cSDimitry Andric         functionName = ".anonymous.";
728349cc55cSDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
729349cc55cSDimitry Andric           "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
730349cc55cSDimitry Andric           ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
731349cc55cSDimitry Andric           (void *)exception_object, frameInfo.start_ip, functionName,
732349cc55cSDimitry Andric           frameInfo.lsda, frameInfo.handler);
733349cc55cSDimitry Andric     }
734349cc55cSDimitry Andric #endif
735349cc55cSDimitry Andric 
736349cc55cSDimitry Andric     // Call stop function at each frame.
737349cc55cSDimitry Andric     _Unwind_Action action =
738349cc55cSDimitry Andric         (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
739349cc55cSDimitry Andric     _Unwind_Reason_Code stopResult =
740349cc55cSDimitry Andric         (*stop)(1, action, exception_object->exception_class, exception_object,
741349cc55cSDimitry Andric                 (_Unwind_Context *)(cursor), stop_parameter);
742349cc55cSDimitry Andric     _LIBUNWIND_TRACE_UNWINDING(
743349cc55cSDimitry Andric         "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
744349cc55cSDimitry Andric         (void *)exception_object, stopResult);
745349cc55cSDimitry Andric     if (stopResult != _URC_NO_REASON) {
746349cc55cSDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
747349cc55cSDimitry Andric           "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
748349cc55cSDimitry Andric           (void *)exception_object);
749349cc55cSDimitry Andric       return _URC_FATAL_PHASE2_ERROR;
750349cc55cSDimitry Andric     }
751349cc55cSDimitry Andric 
752349cc55cSDimitry Andric     // If there is a personality routine, tell it we are unwinding.
753349cc55cSDimitry Andric     if (frameInfo.handler != 0) {
754349cc55cSDimitry Andric       _Unwind_Personality_Fn p =
755349cc55cSDimitry Andric           (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
756349cc55cSDimitry Andric       struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
757349cc55cSDimitry Andric       // EHABI #7.2
758349cc55cSDimitry Andric       exception_object->pr_cache.fnstart = frameInfo.start_ip;
759349cc55cSDimitry Andric       exception_object->pr_cache.ehtp =
760349cc55cSDimitry Andric           (_Unwind_EHT_Header *)frameInfo.unwind_info;
761349cc55cSDimitry Andric       exception_object->pr_cache.additional = frameInfo.flags;
762349cc55cSDimitry Andric       _Unwind_Reason_Code personalityResult =
763349cc55cSDimitry Andric           (*p)(_US_FORCE_UNWIND | _US_UNWIND_FRAME_STARTING, exception_object,
764349cc55cSDimitry Andric                context);
765349cc55cSDimitry Andric       switch (personalityResult) {
766349cc55cSDimitry Andric       case _URC_CONTINUE_UNWIND:
767349cc55cSDimitry Andric         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
768349cc55cSDimitry Andric                                    "personality returned "
769349cc55cSDimitry Andric                                    "_URC_CONTINUE_UNWIND",
770349cc55cSDimitry Andric                                    (void *)exception_object);
771349cc55cSDimitry Andric         // Destructors called, continue unwinding
772349cc55cSDimitry Andric         break;
773349cc55cSDimitry Andric       case _URC_INSTALL_CONTEXT:
774349cc55cSDimitry Andric         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
775349cc55cSDimitry Andric                                    "personality returned "
776349cc55cSDimitry Andric                                    "_URC_INSTALL_CONTEXT",
777349cc55cSDimitry Andric                                    (void *)exception_object);
778349cc55cSDimitry Andric         // We may get control back if landing pad calls _Unwind_Resume().
779349cc55cSDimitry Andric         __unw_resume(cursor);
780349cc55cSDimitry Andric         break;
7814824e7fdSDimitry Andric       case _URC_END_OF_STACK:
7824824e7fdSDimitry Andric         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
7834824e7fdSDimitry Andric                                    "personality returned "
7844824e7fdSDimitry Andric                                    "_URC_END_OF_STACK",
7854824e7fdSDimitry Andric                                    (void *)exception_object);
7864824e7fdSDimitry Andric         // Personalty routine did the step and it can't step forward.
7874824e7fdSDimitry Andric         endOfStack = true;
7884824e7fdSDimitry Andric         break;
789349cc55cSDimitry Andric       default:
790349cc55cSDimitry Andric         // Personality routine returned an unknown result code.
791349cc55cSDimitry Andric         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
792349cc55cSDimitry Andric                                    "personality returned %d, "
793349cc55cSDimitry Andric                                    "_URC_FATAL_PHASE2_ERROR",
794349cc55cSDimitry Andric                                    (void *)exception_object, personalityResult);
795349cc55cSDimitry Andric         return _URC_FATAL_PHASE2_ERROR;
796349cc55cSDimitry Andric       }
797349cc55cSDimitry Andric     }
798349cc55cSDimitry Andric   }
799349cc55cSDimitry Andric 
800349cc55cSDimitry Andric   // Call stop function one last time and tell it we've reached the end
801349cc55cSDimitry Andric   // of the stack.
802349cc55cSDimitry Andric   _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
803349cc55cSDimitry Andric                              "function with _UA_END_OF_STACK",
804349cc55cSDimitry Andric                              (void *)exception_object);
805349cc55cSDimitry Andric   _Unwind_Action lastAction =
806349cc55cSDimitry Andric       (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
807349cc55cSDimitry Andric   (*stop)(1, lastAction, exception_object->exception_class, exception_object,
808349cc55cSDimitry Andric           (struct _Unwind_Context *)(cursor), stop_parameter);
809349cc55cSDimitry Andric 
810349cc55cSDimitry Andric   // Clean up phase did not resume at the frame that the search phase said it
811349cc55cSDimitry Andric   // would.
812349cc55cSDimitry Andric   return _URC_FATAL_PHASE2_ERROR;
813349cc55cSDimitry Andric }
814349cc55cSDimitry Andric 
8150b57cec5SDimitry Andric /// Called by __cxa_throw.  Only returns if there is a fatal error.
8160b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_RaiseException(_Unwind_Exception * exception_object)8170b57cec5SDimitry Andric _Unwind_RaiseException(_Unwind_Exception *exception_object) {
8180b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
8190b57cec5SDimitry Andric                        static_cast<void *>(exception_object));
8200b57cec5SDimitry Andric   unw_context_t uc;
8210b57cec5SDimitry Andric   unw_cursor_t cursor;
8220b57cec5SDimitry Andric   __unw_getcontext(&uc);
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric   // This field for is for compatibility with GCC to say this isn't a forced
8250b57cec5SDimitry Andric   // unwind. EHABI #7.2
8260b57cec5SDimitry Andric   exception_object->unwinder_cache.reserved1 = 0;
8270b57cec5SDimitry Andric 
8280b57cec5SDimitry Andric   // phase 1: the search phase
8290b57cec5SDimitry Andric   _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
8300b57cec5SDimitry Andric   if (phase1 != _URC_NO_REASON)
8310b57cec5SDimitry Andric     return phase1;
8320b57cec5SDimitry Andric 
8330b57cec5SDimitry Andric   // phase 2: the clean up phase
8340b57cec5SDimitry Andric   return unwind_phase2(&uc, &cursor, exception_object, false);
8350b57cec5SDimitry Andric }
8360b57cec5SDimitry Andric 
_Unwind_Complete(_Unwind_Exception * exception_object)8370b57cec5SDimitry Andric _LIBUNWIND_EXPORT void _Unwind_Complete(_Unwind_Exception* exception_object) {
8380b57cec5SDimitry Andric   // This is to be called when exception handling completes to give us a chance
8390b57cec5SDimitry Andric   // to perform any housekeeping. EHABI #7.2. But we have nothing to do here.
8400b57cec5SDimitry Andric   (void)exception_object;
8410b57cec5SDimitry Andric }
8420b57cec5SDimitry Andric 
8430b57cec5SDimitry Andric /// When _Unwind_RaiseException() is in phase2, it hands control
8440b57cec5SDimitry Andric /// to the personality function at each frame.  The personality
8450b57cec5SDimitry Andric /// may force a jump to a landing pad in that function, the landing
8460b57cec5SDimitry Andric /// pad code may then call _Unwind_Resume() to continue with the
8470b57cec5SDimitry Andric /// unwinding.  Note: the call to _Unwind_Resume() is from compiler
848bdd1243dSDimitry Andric /// generated user code.  All other _Unwind_* routines are called
8490b57cec5SDimitry Andric /// by the C++ runtime __cxa_* routines.
8500b57cec5SDimitry Andric ///
8510b57cec5SDimitry Andric /// Note: re-throwing an exception (as opposed to continuing the unwind)
8520b57cec5SDimitry Andric /// is implemented by having the code call __cxa_rethrow() which
8530b57cec5SDimitry Andric /// in turn calls _Unwind_Resume_or_Rethrow().
8540b57cec5SDimitry Andric _LIBUNWIND_EXPORT void
_Unwind_Resume(_Unwind_Exception * exception_object)8550b57cec5SDimitry Andric _Unwind_Resume(_Unwind_Exception *exception_object) {
8560b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)",
8570b57cec5SDimitry Andric                        static_cast<void *>(exception_object));
8580b57cec5SDimitry Andric   unw_context_t uc;
8590b57cec5SDimitry Andric   unw_cursor_t cursor;
8600b57cec5SDimitry Andric   __unw_getcontext(&uc);
8610b57cec5SDimitry Andric 
862349cc55cSDimitry Andric   if (exception_object->unwinder_cache.reserved1)
863349cc55cSDimitry Andric     unwind_phase2_forced(
864349cc55cSDimitry Andric         &uc, &cursor, exception_object,
865349cc55cSDimitry Andric         (_Unwind_Stop_Fn)exception_object->unwinder_cache.reserved1,
866349cc55cSDimitry Andric         (void *)exception_object->unwinder_cache.reserved3);
867349cc55cSDimitry Andric   else
8680b57cec5SDimitry Andric     unwind_phase2(&uc, &cursor, exception_object, true);
8690b57cec5SDimitry Andric 
8700b57cec5SDimitry Andric   // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
8710b57cec5SDimitry Andric   _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
8720b57cec5SDimitry Andric }
8730b57cec5SDimitry Andric 
8740b57cec5SDimitry Andric /// Called by personality handler during phase 2 to get LSDA for current frame.
8750b57cec5SDimitry Andric _LIBUNWIND_EXPORT uintptr_t
_Unwind_GetLanguageSpecificData(struct _Unwind_Context * context)8760b57cec5SDimitry Andric _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
8770b57cec5SDimitry Andric   unw_cursor_t *cursor = (unw_cursor_t *)context;
8780b57cec5SDimitry Andric   unw_proc_info_t frameInfo;
8790b57cec5SDimitry Andric   uintptr_t result = 0;
8800b57cec5SDimitry Andric   if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
8810b57cec5SDimitry Andric     result = (uintptr_t)frameInfo.lsda;
8820b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API(
8830b57cec5SDimitry Andric       "_Unwind_GetLanguageSpecificData(context=%p) => 0x%llx",
8840b57cec5SDimitry Andric       static_cast<void *>(context), (long long)result);
8850b57cec5SDimitry Andric   return result;
8860b57cec5SDimitry Andric }
8870b57cec5SDimitry Andric 
888*8a4dda33SDimitry Andric // Only used in _LIBUNWIND_TRACE_API, which is a no-op when assertions are
889*8a4dda33SDimitry Andric // disabled.
890*8a4dda33SDimitry Andric [[gnu::unused]] static uint64_t
ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation,const void * valuep)891*8a4dda33SDimitry Andric ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation,
892*8a4dda33SDimitry Andric                   const void *valuep) {
8930b57cec5SDimitry Andric   uint64_t value = 0;
8940b57cec5SDimitry Andric   switch (representation) {
8950b57cec5SDimitry Andric     case _UVRSD_UINT32:
8960b57cec5SDimitry Andric     case _UVRSD_FLOAT:
8970b57cec5SDimitry Andric       memcpy(&value, valuep, sizeof(uint32_t));
8980b57cec5SDimitry Andric       break;
8990b57cec5SDimitry Andric 
9000b57cec5SDimitry Andric     case _UVRSD_VFPX:
9010b57cec5SDimitry Andric     case _UVRSD_UINT64:
9020b57cec5SDimitry Andric     case _UVRSD_DOUBLE:
9030b57cec5SDimitry Andric       memcpy(&value, valuep, sizeof(uint64_t));
9040b57cec5SDimitry Andric       break;
9050b57cec5SDimitry Andric   }
9060b57cec5SDimitry Andric   return value;
9070b57cec5SDimitry Andric }
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_VRS_Result
_Unwind_VRS_Set(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,uint32_t regno,_Unwind_VRS_DataRepresentation representation,void * valuep)9100b57cec5SDimitry Andric _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
9110b57cec5SDimitry Andric                 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
9120b57cec5SDimitry Andric                 void *valuep) {
9130b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_VRS_Set(context=%p, regclass=%d, reg=%d, "
9140b57cec5SDimitry Andric                        "rep=%d, value=0x%llX)",
9150b57cec5SDimitry Andric                        static_cast<void *>(context), regclass, regno,
9160b57cec5SDimitry Andric                        representation,
9170b57cec5SDimitry Andric                        ValueAsBitPattern(representation, valuep));
9180b57cec5SDimitry Andric   unw_cursor_t *cursor = (unw_cursor_t *)context;
9190b57cec5SDimitry Andric   switch (regclass) {
9200b57cec5SDimitry Andric     case _UVRSC_CORE:
9210b57cec5SDimitry Andric       if (representation != _UVRSD_UINT32 || regno > 15)
9220b57cec5SDimitry Andric         return _UVRSR_FAILED;
9230b57cec5SDimitry Andric       return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
9240b57cec5SDimitry Andric                            *(unw_word_t *)valuep) == UNW_ESUCCESS
9250b57cec5SDimitry Andric                  ? _UVRSR_OK
9260b57cec5SDimitry Andric                  : _UVRSR_FAILED;
9270b57cec5SDimitry Andric     case _UVRSC_VFP:
9280b57cec5SDimitry Andric       if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
9290b57cec5SDimitry Andric         return _UVRSR_FAILED;
9300b57cec5SDimitry Andric       if (representation == _UVRSD_VFPX) {
9310b57cec5SDimitry Andric         // Can only touch d0-15 with FSTMFDX.
9320b57cec5SDimitry Andric         if (regno > 15)
9330b57cec5SDimitry Andric           return _UVRSR_FAILED;
9340b57cec5SDimitry Andric         __unw_save_vfp_as_X(cursor);
9350b57cec5SDimitry Andric       } else {
9360b57cec5SDimitry Andric         if (regno > 31)
9370b57cec5SDimitry Andric           return _UVRSR_FAILED;
9380b57cec5SDimitry Andric       }
9390b57cec5SDimitry Andric       return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
9400b57cec5SDimitry Andric                              *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
9410b57cec5SDimitry Andric                  ? _UVRSR_OK
9420b57cec5SDimitry Andric                  : _UVRSR_FAILED;
9430b57cec5SDimitry Andric #if defined(__ARM_WMMX)
9440b57cec5SDimitry Andric     case _UVRSC_WMMXC:
9450b57cec5SDimitry Andric       if (representation != _UVRSD_UINT32 || regno > 3)
9460b57cec5SDimitry Andric         return _UVRSR_FAILED;
9470b57cec5SDimitry Andric       return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
9480b57cec5SDimitry Andric                            *(unw_word_t *)valuep) == UNW_ESUCCESS
9490b57cec5SDimitry Andric                  ? _UVRSR_OK
9500b57cec5SDimitry Andric                  : _UVRSR_FAILED;
9510b57cec5SDimitry Andric     case _UVRSC_WMMXD:
9520b57cec5SDimitry Andric       if (representation != _UVRSD_DOUBLE || regno > 31)
9530b57cec5SDimitry Andric         return _UVRSR_FAILED;
9540b57cec5SDimitry Andric       return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
9550b57cec5SDimitry Andric                              *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
9560b57cec5SDimitry Andric                  ? _UVRSR_OK
9570b57cec5SDimitry Andric                  : _UVRSR_FAILED;
9580b57cec5SDimitry Andric #else
9590b57cec5SDimitry Andric     case _UVRSC_WMMXC:
9600b57cec5SDimitry Andric     case _UVRSC_WMMXD:
9610b57cec5SDimitry Andric       break;
9620b57cec5SDimitry Andric #endif
9630eae32dcSDimitry Andric     case _UVRSC_PSEUDO:
9640eae32dcSDimitry Andric       // There's only one pseudo-register, PAC, with regno == 0.
9650eae32dcSDimitry Andric       if (representation != _UVRSD_UINT32 || regno != 0)
9660eae32dcSDimitry Andric         return _UVRSR_FAILED;
9670eae32dcSDimitry Andric       return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_RA_AUTH_CODE),
9680eae32dcSDimitry Andric                            *(unw_word_t *)valuep) == UNW_ESUCCESS
9690eae32dcSDimitry Andric                  ? _UVRSR_OK
9700eae32dcSDimitry Andric                  : _UVRSR_FAILED;
9710eae32dcSDimitry Andric       break;
9720b57cec5SDimitry Andric   }
9730b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported register class");
9740b57cec5SDimitry Andric }
9750b57cec5SDimitry Andric 
9760b57cec5SDimitry Andric static _Unwind_VRS_Result
_Unwind_VRS_Get_Internal(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,uint32_t regno,_Unwind_VRS_DataRepresentation representation,void * valuep)9770b57cec5SDimitry Andric _Unwind_VRS_Get_Internal(_Unwind_Context *context,
9780b57cec5SDimitry Andric                          _Unwind_VRS_RegClass regclass, uint32_t regno,
9790b57cec5SDimitry Andric                          _Unwind_VRS_DataRepresentation representation,
9800b57cec5SDimitry Andric                          void *valuep) {
9810b57cec5SDimitry Andric   unw_cursor_t *cursor = (unw_cursor_t *)context;
9820b57cec5SDimitry Andric   switch (regclass) {
9830b57cec5SDimitry Andric     case _UVRSC_CORE:
9840b57cec5SDimitry Andric       if (representation != _UVRSD_UINT32 || regno > 15)
9850b57cec5SDimitry Andric         return _UVRSR_FAILED;
9860b57cec5SDimitry Andric       return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
9870b57cec5SDimitry Andric                            (unw_word_t *)valuep) == UNW_ESUCCESS
9880b57cec5SDimitry Andric                  ? _UVRSR_OK
9890b57cec5SDimitry Andric                  : _UVRSR_FAILED;
9900b57cec5SDimitry Andric     case _UVRSC_VFP:
9910b57cec5SDimitry Andric       if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
9920b57cec5SDimitry Andric         return _UVRSR_FAILED;
9930b57cec5SDimitry Andric       if (representation == _UVRSD_VFPX) {
9940b57cec5SDimitry Andric         // Can only touch d0-15 with FSTMFDX.
9950b57cec5SDimitry Andric         if (regno > 15)
9960b57cec5SDimitry Andric           return _UVRSR_FAILED;
9970b57cec5SDimitry Andric         __unw_save_vfp_as_X(cursor);
9980b57cec5SDimitry Andric       } else {
9990b57cec5SDimitry Andric         if (regno > 31)
10000b57cec5SDimitry Andric           return _UVRSR_FAILED;
10010b57cec5SDimitry Andric       }
10020b57cec5SDimitry Andric       return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
10030b57cec5SDimitry Andric                              (unw_fpreg_t *)valuep) == UNW_ESUCCESS
10040b57cec5SDimitry Andric                  ? _UVRSR_OK
10050b57cec5SDimitry Andric                  : _UVRSR_FAILED;
10060b57cec5SDimitry Andric #if defined(__ARM_WMMX)
10070b57cec5SDimitry Andric     case _UVRSC_WMMXC:
10080b57cec5SDimitry Andric       if (representation != _UVRSD_UINT32 || regno > 3)
10090b57cec5SDimitry Andric         return _UVRSR_FAILED;
10100b57cec5SDimitry Andric       return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
10110b57cec5SDimitry Andric                            (unw_word_t *)valuep) == UNW_ESUCCESS
10120b57cec5SDimitry Andric                  ? _UVRSR_OK
10130b57cec5SDimitry Andric                  : _UVRSR_FAILED;
10140b57cec5SDimitry Andric     case _UVRSC_WMMXD:
10150b57cec5SDimitry Andric       if (representation != _UVRSD_DOUBLE || regno > 31)
10160b57cec5SDimitry Andric         return _UVRSR_FAILED;
10170b57cec5SDimitry Andric       return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
10180b57cec5SDimitry Andric                              (unw_fpreg_t *)valuep) == UNW_ESUCCESS
10190b57cec5SDimitry Andric                  ? _UVRSR_OK
10200b57cec5SDimitry Andric                  : _UVRSR_FAILED;
10210b57cec5SDimitry Andric #else
10220b57cec5SDimitry Andric     case _UVRSC_WMMXC:
10230b57cec5SDimitry Andric     case _UVRSC_WMMXD:
10240b57cec5SDimitry Andric       break;
10250b57cec5SDimitry Andric #endif
10260eae32dcSDimitry Andric     case _UVRSC_PSEUDO:
10270eae32dcSDimitry Andric       // There's only one pseudo-register, PAC, with regno == 0.
10280eae32dcSDimitry Andric       if (representation != _UVRSD_UINT32 || regno != 0)
10290eae32dcSDimitry Andric         return _UVRSR_FAILED;
10300eae32dcSDimitry Andric       return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_RA_AUTH_CODE),
10310eae32dcSDimitry Andric                            (unw_word_t *)valuep) == UNW_ESUCCESS
10320eae32dcSDimitry Andric                  ? _UVRSR_OK
10330eae32dcSDimitry Andric                  : _UVRSR_FAILED;
10340eae32dcSDimitry Andric       break;
10350b57cec5SDimitry Andric   }
10360b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported register class");
10370b57cec5SDimitry Andric }
10380b57cec5SDimitry Andric 
10390b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_VRS_Result
_Unwind_VRS_Get(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,uint32_t regno,_Unwind_VRS_DataRepresentation representation,void * valuep)10400b57cec5SDimitry Andric _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
10410b57cec5SDimitry Andric                 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
10420b57cec5SDimitry Andric                 void *valuep) {
10430b57cec5SDimitry Andric   _Unwind_VRS_Result result =
10440b57cec5SDimitry Andric       _Unwind_VRS_Get_Internal(context, regclass, regno, representation,
10450b57cec5SDimitry Andric                                valuep);
10460b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_VRS_Get(context=%p, regclass=%d, reg=%d, "
10470b57cec5SDimitry Andric                        "rep=%d, value=0x%llX, result = %d)",
10480b57cec5SDimitry Andric                        static_cast<void *>(context), regclass, regno,
10490b57cec5SDimitry Andric                        representation,
10500b57cec5SDimitry Andric                        ValueAsBitPattern(representation, valuep), result);
10510b57cec5SDimitry Andric   return result;
10520b57cec5SDimitry Andric }
10530b57cec5SDimitry Andric 
10540b57cec5SDimitry Andric _Unwind_VRS_Result
_Unwind_VRS_Pop(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,uint32_t discriminator,_Unwind_VRS_DataRepresentation representation)10550b57cec5SDimitry Andric _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
10560b57cec5SDimitry Andric                 uint32_t discriminator,
10570b57cec5SDimitry Andric                 _Unwind_VRS_DataRepresentation representation) {
10580b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_VRS_Pop(context=%p, regclass=%d, "
10590b57cec5SDimitry Andric                        "discriminator=%d, representation=%d)",
10600b57cec5SDimitry Andric                        static_cast<void *>(context), regclass, discriminator,
10610b57cec5SDimitry Andric                        representation);
10620b57cec5SDimitry Andric   switch (regclass) {
10630b57cec5SDimitry Andric     case _UVRSC_WMMXC:
10640b57cec5SDimitry Andric #if !defined(__ARM_WMMX)
10650b57cec5SDimitry Andric       break;
10660b57cec5SDimitry Andric #endif
10670b57cec5SDimitry Andric     case _UVRSC_CORE: {
10680b57cec5SDimitry Andric       if (representation != _UVRSD_UINT32)
10690b57cec5SDimitry Andric         return _UVRSR_FAILED;
10700b57cec5SDimitry Andric       // When popping SP from the stack, we don't want to override it from the
10710b57cec5SDimitry Andric       // computed new stack location. See EHABI #7.5.4 table 3.
10720b57cec5SDimitry Andric       bool poppedSP = false;
10730b57cec5SDimitry Andric       uint32_t* sp;
10740b57cec5SDimitry Andric       if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
10750b57cec5SDimitry Andric                           _UVRSD_UINT32, &sp) != _UVRSR_OK) {
10760b57cec5SDimitry Andric         return _UVRSR_FAILED;
10770b57cec5SDimitry Andric       }
10780b57cec5SDimitry Andric       for (uint32_t i = 0; i < 16; ++i) {
10790b57cec5SDimitry Andric         if (!(discriminator & static_cast<uint32_t>(1 << i)))
10800b57cec5SDimitry Andric           continue;
10810b57cec5SDimitry Andric         uint32_t value = *sp++;
10820b57cec5SDimitry Andric         if (regclass == _UVRSC_CORE && i == 13)
10830b57cec5SDimitry Andric           poppedSP = true;
10840b57cec5SDimitry Andric         if (_Unwind_VRS_Set(context, regclass, i,
10850b57cec5SDimitry Andric                             _UVRSD_UINT32, &value) != _UVRSR_OK) {
10860b57cec5SDimitry Andric           return _UVRSR_FAILED;
10870b57cec5SDimitry Andric         }
10880b57cec5SDimitry Andric       }
10890b57cec5SDimitry Andric       if (!poppedSP) {
10900b57cec5SDimitry Andric         return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP,
10910b57cec5SDimitry Andric                                _UVRSD_UINT32, &sp);
10920b57cec5SDimitry Andric       }
10930b57cec5SDimitry Andric       return _UVRSR_OK;
10940b57cec5SDimitry Andric     }
10950b57cec5SDimitry Andric     case _UVRSC_WMMXD:
10960b57cec5SDimitry Andric #if !defined(__ARM_WMMX)
10970b57cec5SDimitry Andric       break;
10980b57cec5SDimitry Andric #endif
10990b57cec5SDimitry Andric     case _UVRSC_VFP: {
11000b57cec5SDimitry Andric       if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
11010b57cec5SDimitry Andric         return _UVRSR_FAILED;
11020b57cec5SDimitry Andric       uint32_t first = discriminator >> 16;
11030b57cec5SDimitry Andric       uint32_t count = discriminator & 0xffff;
11040b57cec5SDimitry Andric       uint32_t end = first+count;
11050b57cec5SDimitry Andric       uint32_t* sp;
11060b57cec5SDimitry Andric       if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
11070b57cec5SDimitry Andric                           _UVRSD_UINT32, &sp) != _UVRSR_OK) {
11080b57cec5SDimitry Andric         return _UVRSR_FAILED;
11090b57cec5SDimitry Andric       }
11100b57cec5SDimitry Andric       // For _UVRSD_VFPX, we're assuming the data is stored in FSTMX "standard
11110b57cec5SDimitry Andric       // format 1", which is equivalent to FSTMD + a padding word.
11120b57cec5SDimitry Andric       for (uint32_t i = first; i < end; ++i) {
11130b57cec5SDimitry Andric         // SP is only 32-bit aligned so don't copy 64-bit at a time.
1114c2c6a179SDimitry Andric         uint64_t w0 = *sp++;
1115c2c6a179SDimitry Andric         uint64_t w1 = *sp++;
11165ffd83dbSDimitry Andric #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1117c2c6a179SDimitry Andric         uint64_t value = (w1 << 32) | w0;
11185ffd83dbSDimitry Andric #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1119c2c6a179SDimitry Andric         uint64_t value = (w0 << 32) | w1;
11205ffd83dbSDimitry Andric #else
11215ffd83dbSDimitry Andric #error "Unable to determine endianess"
1122c2c6a179SDimitry Andric #endif
11230b57cec5SDimitry Andric         if (_Unwind_VRS_Set(context, regclass, i, representation, &value) !=
11240b57cec5SDimitry Andric             _UVRSR_OK)
11250b57cec5SDimitry Andric           return _UVRSR_FAILED;
11260b57cec5SDimitry Andric       }
11270b57cec5SDimitry Andric       if (representation == _UVRSD_VFPX)
11280b57cec5SDimitry Andric         ++sp;
11290b57cec5SDimitry Andric       return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
11300b57cec5SDimitry Andric                              &sp);
11310b57cec5SDimitry Andric     }
11320eae32dcSDimitry Andric     case _UVRSC_PSEUDO: {
11330eae32dcSDimitry Andric       if (representation != _UVRSD_UINT32 || discriminator != 0)
11340eae32dcSDimitry Andric         return _UVRSR_FAILED;
11350eae32dcSDimitry Andric       // Return Address Authentication code (PAC) - discriminator 0
11360eae32dcSDimitry Andric       uint32_t *sp;
11370eae32dcSDimitry Andric       if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
11380eae32dcSDimitry Andric                           &sp) != _UVRSR_OK) {
11390eae32dcSDimitry Andric         return _UVRSR_FAILED;
11400eae32dcSDimitry Andric       }
11410eae32dcSDimitry Andric       uint32_t pac = *sp++;
11420eae32dcSDimitry Andric       _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
114381ad6265SDimitry Andric       return _Unwind_VRS_Set(context, _UVRSC_PSEUDO, 0, _UVRSD_UINT32, &pac);
11440eae32dcSDimitry Andric     }
11450b57cec5SDimitry Andric   }
11460b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported register class");
11470b57cec5SDimitry Andric }
11480b57cec5SDimitry Andric 
1149349cc55cSDimitry Andric /// Not used by C++.
1150349cc55cSDimitry Andric /// Unwinds stack, calling "stop" function at each frame.
1151349cc55cSDimitry Andric /// Could be used to implement longjmp().
1152349cc55cSDimitry Andric _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_ForcedUnwind(_Unwind_Exception * exception_object,_Unwind_Stop_Fn stop,void * stop_parameter)1153349cc55cSDimitry Andric _Unwind_ForcedUnwind(_Unwind_Exception *exception_object, _Unwind_Stop_Fn stop,
1154349cc55cSDimitry Andric                      void *stop_parameter) {
1155349cc55cSDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
1156349cc55cSDimitry Andric                        (void *)exception_object, (void *)(uintptr_t)stop);
1157349cc55cSDimitry Andric   unw_context_t uc;
1158349cc55cSDimitry Andric   unw_cursor_t cursor;
1159349cc55cSDimitry Andric   __unw_getcontext(&uc);
1160349cc55cSDimitry Andric 
1161349cc55cSDimitry Andric   // Mark that this is a forced unwind, so _Unwind_Resume() can do
1162349cc55cSDimitry Andric   // the right thing.
1163349cc55cSDimitry Andric   exception_object->unwinder_cache.reserved1 = (uintptr_t)stop;
1164349cc55cSDimitry Andric   exception_object->unwinder_cache.reserved3 = (uintptr_t)stop_parameter;
1165349cc55cSDimitry Andric 
1166349cc55cSDimitry Andric   return unwind_phase2_forced(&uc, &cursor, exception_object, stop,
1167349cc55cSDimitry Andric                               stop_parameter);
1168349cc55cSDimitry Andric }
1169349cc55cSDimitry Andric 
11700b57cec5SDimitry Andric /// Called by personality handler during phase 2 to find the start of the
11710b57cec5SDimitry Andric /// function.
11720b57cec5SDimitry Andric _LIBUNWIND_EXPORT uintptr_t
_Unwind_GetRegionStart(struct _Unwind_Context * context)11730b57cec5SDimitry Andric _Unwind_GetRegionStart(struct _Unwind_Context *context) {
11740b57cec5SDimitry Andric   unw_cursor_t *cursor = (unw_cursor_t *)context;
11750b57cec5SDimitry Andric   unw_proc_info_t frameInfo;
11760b57cec5SDimitry Andric   uintptr_t result = 0;
11770b57cec5SDimitry Andric   if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
11780b57cec5SDimitry Andric     result = (uintptr_t)frameInfo.start_ip;
11790b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%llX",
11800b57cec5SDimitry Andric                        static_cast<void *>(context), (long long)result);
11810b57cec5SDimitry Andric   return result;
11820b57cec5SDimitry Andric }
11830b57cec5SDimitry Andric 
11840b57cec5SDimitry Andric 
11850b57cec5SDimitry Andric /// Called by personality handler during phase 2 if a foreign exception
11860b57cec5SDimitry Andric // is caught.
11870b57cec5SDimitry Andric _LIBUNWIND_EXPORT void
_Unwind_DeleteException(_Unwind_Exception * exception_object)11880b57cec5SDimitry Andric _Unwind_DeleteException(_Unwind_Exception *exception_object) {
11890b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
11900b57cec5SDimitry Andric                        static_cast<void *>(exception_object));
11910b57cec5SDimitry Andric   if (exception_object->exception_cleanup != NULL)
11920b57cec5SDimitry Andric     (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
11930b57cec5SDimitry Andric                                            exception_object);
11940b57cec5SDimitry Andric }
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
__gnu_unwind_frame(_Unwind_Exception * exception_object,struct _Unwind_Context * context)11970b57cec5SDimitry Andric __gnu_unwind_frame(_Unwind_Exception *exception_object,
11980b57cec5SDimitry Andric                    struct _Unwind_Context *context) {
119981ad6265SDimitry Andric   (void)exception_object;
12000b57cec5SDimitry Andric   unw_cursor_t *cursor = (unw_cursor_t *)context;
12014824e7fdSDimitry Andric   switch (__unw_step(cursor)) {
12024824e7fdSDimitry Andric   case UNW_STEP_SUCCESS:
12030b57cec5SDimitry Andric     return _URC_OK;
12044824e7fdSDimitry Andric   case UNW_STEP_END:
12054824e7fdSDimitry Andric     return _URC_END_OF_STACK;
12064824e7fdSDimitry Andric   default:
12074824e7fdSDimitry Andric     return _URC_FAILURE;
12084824e7fdSDimitry Andric   }
12090b57cec5SDimitry Andric }
12100b57cec5SDimitry Andric 
12110b57cec5SDimitry Andric #endif  // defined(_LIBUNWIND_ARM_EHABI)
1212