xref: /freebsd/contrib/llvm-project/libunwind/src/Unwind-EHABI.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //===--------------------------- Unwind-EHABI.cpp -------------------------===//
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.
320b57cec5SDimitry Andric uint8_t getByte(const uint32_t* data, size_t offset) {
330b57cec5SDimitry Andric   const uint8_t* byteData = reinterpret_cast<const uint8_t*>(data);
34*5ffd83dbSDimitry Andric #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
350b57cec5SDimitry Andric   return byteData[(offset & ~(size_t)0x03) + (3 - (offset & (size_t)0x03))];
36*5ffd83dbSDimitry Andric #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
370b57cec5SDimitry Andric   return byteData[offset];
38*5ffd83dbSDimitry Andric #else
39*5ffd83dbSDimitry Andric #error "Unable to determine endianess"
400b57cec5SDimitry Andric #endif
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
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 
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 
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);
1000b57cec5SDimitry Andric       case Descriptor::LU16:
1010b57cec5SDimitry Andric         descriptor = getNextNibble(descriptor, &length);
1020b57cec5SDimitry Andric         descriptor = getNextNibble(descriptor, &offset);
1030b57cec5SDimitry Andric       default:
1040b57cec5SDimitry Andric         assert(false);
1050b57cec5SDimitry Andric         return _URC_FAILURE;
1060b57cec5SDimitry Andric     }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric     // See # 9.2 table for decoding the kind of descriptor. It's a 2-bit value.
1090b57cec5SDimitry Andric     Descriptor::Kind kind =
1100b57cec5SDimitry Andric         static_cast<Descriptor::Kind>((length & 0x1) | ((offset & 0x1) << 1));
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric     // Clear off flag from last bit.
1130b57cec5SDimitry Andric     length &= ~1u;
1140b57cec5SDimitry Andric     offset &= ~1u;
1150b57cec5SDimitry Andric     uintptr_t scopeStart = ucbp->pr_cache.fnstart + offset;
1160b57cec5SDimitry Andric     uintptr_t scopeEnd = scopeStart + length;
1170b57cec5SDimitry Andric     uintptr_t pc = _Unwind_GetIP(context);
1180b57cec5SDimitry Andric     bool isInScope = (scopeStart <= pc) && (pc < scopeEnd);
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric     switch (kind) {
1210b57cec5SDimitry Andric       case Descriptor::CLEANUP: {
1220b57cec5SDimitry Andric         // TODO(ajwong): Handle cleanup descriptors.
1230b57cec5SDimitry Andric         break;
1240b57cec5SDimitry Andric       }
1250b57cec5SDimitry Andric       case Descriptor::FUNC: {
1260b57cec5SDimitry Andric         // TODO(ajwong): Handle function descriptors.
1270b57cec5SDimitry Andric         break;
1280b57cec5SDimitry Andric       }
1290b57cec5SDimitry Andric       case Descriptor::CATCH: {
1300b57cec5SDimitry Andric         // Catch descriptors require gobbling one more word.
1310b57cec5SDimitry Andric         uint32_t landing_pad;
1320b57cec5SDimitry Andric         descriptor = getNextWord(descriptor, &landing_pad);
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric         if (isInScope) {
1350b57cec5SDimitry Andric           // TODO(ajwong): This is only phase1 compatible logic. Implement
1360b57cec5SDimitry Andric           // phase2.
1370b57cec5SDimitry Andric           landing_pad = signExtendPrel31(landing_pad & ~0x80000000);
1380b57cec5SDimitry Andric           if (landing_pad == 0xffffffff) {
1390b57cec5SDimitry Andric             return _URC_HANDLER_FOUND;
1400b57cec5SDimitry Andric           } else if (landing_pad == 0xfffffffe) {
1410b57cec5SDimitry Andric             return _URC_FAILURE;
1420b57cec5SDimitry Andric           } else {
1430b57cec5SDimitry Andric             /*
1440b57cec5SDimitry Andric             bool is_reference_type = landing_pad & 0x80000000;
1450b57cec5SDimitry Andric             void* matched_object;
1460b57cec5SDimitry Andric             if (__cxxabiv1::__cxa_type_match(
1470b57cec5SDimitry Andric                     ucbp, reinterpret_cast<const std::type_info *>(landing_pad),
1480b57cec5SDimitry Andric                     is_reference_type,
1490b57cec5SDimitry Andric                     &matched_object) != __cxxabiv1::ctm_failed)
1500b57cec5SDimitry Andric                 return _URC_HANDLER_FOUND;
1510b57cec5SDimitry Andric                 */
1520b57cec5SDimitry Andric             _LIBUNWIND_ABORT("Type matching not implemented");
1530b57cec5SDimitry Andric           }
1540b57cec5SDimitry Andric         }
1550b57cec5SDimitry Andric         break;
1560b57cec5SDimitry Andric       }
1570b57cec5SDimitry Andric       default:
1580b57cec5SDimitry Andric         _LIBUNWIND_ABORT("Invalid descriptor kind found.");
1590b57cec5SDimitry Andric     }
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric     getNextWord(descriptor, &descriptorWord);
1620b57cec5SDimitry Andric   }
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   return _URC_CONTINUE_UNWIND;
1650b57cec5SDimitry Andric }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric static _Unwind_Reason_Code unwindOneFrame(_Unwind_State state,
1680b57cec5SDimitry Andric                                           _Unwind_Control_Block* ucbp,
1690b57cec5SDimitry Andric                                           struct _Unwind_Context* context) {
1700b57cec5SDimitry Andric   // Read the compact model EHT entry's header # 6.3
1710b57cec5SDimitry Andric   const uint32_t* unwindingData = ucbp->pr_cache.ehtp;
1720b57cec5SDimitry Andric   assert((*unwindingData & 0xf0000000) == 0x80000000 && "Must be a compact entry");
1730b57cec5SDimitry Andric   Descriptor::Format format =
1740b57cec5SDimitry Andric       static_cast<Descriptor::Format>((*unwindingData & 0x0f000000) >> 24);
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric   const char *lsda =
1770b57cec5SDimitry Andric       reinterpret_cast<const char *>(_Unwind_GetLanguageSpecificData(context));
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   // Handle descriptors before unwinding so they are processed in the context
1800b57cec5SDimitry Andric   // of the correct stack frame.
1810b57cec5SDimitry Andric   _Unwind_Reason_Code result =
1820b57cec5SDimitry Andric       ProcessDescriptors(state, ucbp, context, format, lsda,
1830b57cec5SDimitry Andric                          ucbp->pr_cache.additional);
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   if (result != _URC_CONTINUE_UNWIND)
1860b57cec5SDimitry Andric     return result;
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   if (__unw_step(reinterpret_cast<unw_cursor_t *>(context)) != UNW_STEP_SUCCESS)
1890b57cec5SDimitry Andric     return _URC_FAILURE;
1900b57cec5SDimitry Andric   return _URC_CONTINUE_UNWIND;
1910b57cec5SDimitry Andric }
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_CORE /
1940b57cec5SDimitry Andric // _UVRSD_UINT32.
1950b57cec5SDimitry Andric uint32_t RegisterMask(uint8_t start, uint8_t count_minus_one) {
1960b57cec5SDimitry Andric   return ((1U << (count_minus_one + 1)) - 1) << start;
1970b57cec5SDimitry Andric }
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_VFP /
2000b57cec5SDimitry Andric // _UVRSD_DOUBLE.
2010b57cec5SDimitry Andric uint32_t RegisterRange(uint8_t start, uint8_t count_minus_one) {
2020b57cec5SDimitry Andric   return ((uint32_t)start << 16) | ((uint32_t)count_minus_one + 1);
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric } // end anonymous namespace
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric /**
2080b57cec5SDimitry Andric  * Decodes an EHT entry.
2090b57cec5SDimitry Andric  *
2100b57cec5SDimitry Andric  * @param data Pointer to EHT.
2110b57cec5SDimitry Andric  * @param[out] off Offset from return value (in bytes) to begin interpretation.
2120b57cec5SDimitry Andric  * @param[out] len Number of bytes in unwind code.
2130b57cec5SDimitry Andric  * @return Pointer to beginning of unwind code.
2140b57cec5SDimitry Andric  */
2150b57cec5SDimitry Andric extern "C" const uint32_t*
2160b57cec5SDimitry Andric decode_eht_entry(const uint32_t* data, size_t* off, size_t* len) {
2170b57cec5SDimitry Andric   if ((*data & 0x80000000) == 0) {
2180b57cec5SDimitry Andric     // 6.2: Generic Model
2190b57cec5SDimitry Andric     //
2200b57cec5SDimitry Andric     // EHT entry is a prel31 pointing to the PR, followed by data understood
2210b57cec5SDimitry Andric     // only by the personality routine. Fortunately, all existing assembler
2220b57cec5SDimitry Andric     // implementations, including GNU assembler, LLVM integrated assembler,
2230b57cec5SDimitry Andric     // and ARM assembler, assume that the unwind opcodes come after the
2240b57cec5SDimitry Andric     // personality rountine address.
2250b57cec5SDimitry Andric     *off = 1; // First byte is size data.
2260b57cec5SDimitry Andric     *len = (((data[1] >> 24) & 0xff) + 1) * 4;
2270b57cec5SDimitry Andric     data++; // Skip the first word, which is the prel31 offset.
2280b57cec5SDimitry Andric   } else {
2290b57cec5SDimitry Andric     // 6.3: ARM Compact Model
2300b57cec5SDimitry Andric     //
2310b57cec5SDimitry Andric     // EHT entries here correspond to the __aeabi_unwind_cpp_pr[012] PRs indeded
2320b57cec5SDimitry Andric     // by format:
2330b57cec5SDimitry Andric     Descriptor::Format format =
2340b57cec5SDimitry Andric         static_cast<Descriptor::Format>((*data & 0x0f000000) >> 24);
2350b57cec5SDimitry Andric     switch (format) {
2360b57cec5SDimitry Andric       case Descriptor::SU16:
2370b57cec5SDimitry Andric         *len = 4;
2380b57cec5SDimitry Andric         *off = 1;
2390b57cec5SDimitry Andric         break;
2400b57cec5SDimitry Andric       case Descriptor::LU16:
2410b57cec5SDimitry Andric       case Descriptor::LU32:
2420b57cec5SDimitry Andric         *len = 4 + 4 * ((*data & 0x00ff0000) >> 16);
2430b57cec5SDimitry Andric         *off = 2;
2440b57cec5SDimitry Andric         break;
2450b57cec5SDimitry Andric       default:
2460b57cec5SDimitry Andric         return nullptr;
2470b57cec5SDimitry Andric     }
2480b57cec5SDimitry Andric   }
2490b57cec5SDimitry Andric   return data;
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_Reason_Code
2530b57cec5SDimitry Andric _Unwind_VRS_Interpret(_Unwind_Context *context, const uint32_t *data,
2540b57cec5SDimitry Andric                       size_t offset, size_t len) {
2550b57cec5SDimitry Andric   bool wrotePC = false;
2560b57cec5SDimitry Andric   bool finish = false;
2570b57cec5SDimitry Andric   while (offset < len && !finish) {
2580b57cec5SDimitry Andric     uint8_t byte = getByte(data, offset++);
2590b57cec5SDimitry Andric     if ((byte & 0x80) == 0) {
2600b57cec5SDimitry Andric       uint32_t sp;
2610b57cec5SDimitry Andric       _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
2620b57cec5SDimitry Andric       if (byte & 0x40)
2630b57cec5SDimitry Andric         sp -= (((uint32_t)byte & 0x3f) << 2) + 4;
2640b57cec5SDimitry Andric       else
2650b57cec5SDimitry Andric         sp += ((uint32_t)byte << 2) + 4;
2660b57cec5SDimitry Andric       _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
2670b57cec5SDimitry Andric     } else {
2680b57cec5SDimitry Andric       switch (byte & 0xf0) {
2690b57cec5SDimitry Andric         case 0x80: {
2700b57cec5SDimitry Andric           if (offset >= len)
2710b57cec5SDimitry Andric             return _URC_FAILURE;
2720b57cec5SDimitry Andric           uint32_t registers =
2730b57cec5SDimitry Andric               (((uint32_t)byte & 0x0f) << 12) |
2740b57cec5SDimitry Andric               (((uint32_t)getByte(data, offset++)) << 4);
2750b57cec5SDimitry Andric           if (!registers)
2760b57cec5SDimitry Andric             return _URC_FAILURE;
2770b57cec5SDimitry Andric           if (registers & (1 << 15))
2780b57cec5SDimitry Andric             wrotePC = true;
2790b57cec5SDimitry Andric           _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
2800b57cec5SDimitry Andric           break;
2810b57cec5SDimitry Andric         }
2820b57cec5SDimitry Andric         case 0x90: {
2830b57cec5SDimitry Andric           uint8_t reg = byte & 0x0f;
2840b57cec5SDimitry Andric           if (reg == 13 || reg == 15)
2850b57cec5SDimitry Andric             return _URC_FAILURE;
2860b57cec5SDimitry Andric           uint32_t sp;
2870b57cec5SDimitry Andric           _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_R0 + reg,
2880b57cec5SDimitry Andric                           _UVRSD_UINT32, &sp);
2890b57cec5SDimitry Andric           _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
2900b57cec5SDimitry Andric                           &sp);
2910b57cec5SDimitry Andric           break;
2920b57cec5SDimitry Andric         }
2930b57cec5SDimitry Andric         case 0xa0: {
2940b57cec5SDimitry Andric           uint32_t registers = RegisterMask(4, byte & 0x07);
2950b57cec5SDimitry Andric           if (byte & 0x08)
2960b57cec5SDimitry Andric             registers |= 1 << 14;
2970b57cec5SDimitry Andric           _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
2980b57cec5SDimitry Andric           break;
2990b57cec5SDimitry Andric         }
3000b57cec5SDimitry Andric         case 0xb0: {
3010b57cec5SDimitry Andric           switch (byte) {
3020b57cec5SDimitry Andric             case 0xb0:
3030b57cec5SDimitry Andric               finish = true;
3040b57cec5SDimitry Andric               break;
3050b57cec5SDimitry Andric             case 0xb1: {
3060b57cec5SDimitry Andric               if (offset >= len)
3070b57cec5SDimitry Andric                 return _URC_FAILURE;
3080b57cec5SDimitry Andric               uint8_t registers = getByte(data, offset++);
3090b57cec5SDimitry Andric               if (registers & 0xf0 || !registers)
3100b57cec5SDimitry Andric                 return _URC_FAILURE;
3110b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
3120b57cec5SDimitry Andric               break;
3130b57cec5SDimitry Andric             }
3140b57cec5SDimitry Andric             case 0xb2: {
3150b57cec5SDimitry Andric               uint32_t addend = 0;
3160b57cec5SDimitry Andric               uint32_t shift = 0;
3170b57cec5SDimitry Andric               // This decodes a uleb128 value.
3180b57cec5SDimitry Andric               while (true) {
3190b57cec5SDimitry Andric                 if (offset >= len)
3200b57cec5SDimitry Andric                   return _URC_FAILURE;
3210b57cec5SDimitry Andric                 uint32_t v = getByte(data, offset++);
3220b57cec5SDimitry Andric                 addend |= (v & 0x7f) << shift;
3230b57cec5SDimitry Andric                 if ((v & 0x80) == 0)
3240b57cec5SDimitry Andric                   break;
3250b57cec5SDimitry Andric                 shift += 7;
3260b57cec5SDimitry Andric               }
3270b57cec5SDimitry Andric               uint32_t sp;
3280b57cec5SDimitry Andric               _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
3290b57cec5SDimitry Andric                               &sp);
3300b57cec5SDimitry Andric               sp += 0x204 + (addend << 2);
3310b57cec5SDimitry Andric               _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
3320b57cec5SDimitry Andric                               &sp);
3330b57cec5SDimitry Andric               break;
3340b57cec5SDimitry Andric             }
3350b57cec5SDimitry Andric             case 0xb3: {
3360b57cec5SDimitry Andric               uint8_t v = getByte(data, offset++);
3370b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_VFP,
3380b57cec5SDimitry Andric                               RegisterRange(static_cast<uint8_t>(v >> 4),
3390b57cec5SDimitry Andric                                             v & 0x0f), _UVRSD_VFPX);
3400b57cec5SDimitry Andric               break;
3410b57cec5SDimitry Andric             }
3420b57cec5SDimitry Andric             case 0xb4:
3430b57cec5SDimitry Andric             case 0xb5:
3440b57cec5SDimitry Andric             case 0xb6:
3450b57cec5SDimitry Andric             case 0xb7:
3460b57cec5SDimitry Andric               return _URC_FAILURE;
3470b57cec5SDimitry Andric             default:
3480b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_VFP,
3490b57cec5SDimitry Andric                               RegisterRange(8, byte & 0x07), _UVRSD_VFPX);
3500b57cec5SDimitry Andric               break;
3510b57cec5SDimitry Andric           }
3520b57cec5SDimitry Andric           break;
3530b57cec5SDimitry Andric         }
3540b57cec5SDimitry Andric         case 0xc0: {
3550b57cec5SDimitry Andric           switch (byte) {
3560b57cec5SDimitry Andric #if defined(__ARM_WMMX)
3570b57cec5SDimitry Andric             case 0xc0:
3580b57cec5SDimitry Andric             case 0xc1:
3590b57cec5SDimitry Andric             case 0xc2:
3600b57cec5SDimitry Andric             case 0xc3:
3610b57cec5SDimitry Andric             case 0xc4:
3620b57cec5SDimitry Andric             case 0xc5:
3630b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
3640b57cec5SDimitry Andric                               RegisterRange(10, byte & 0x7), _UVRSD_DOUBLE);
3650b57cec5SDimitry Andric               break;
3660b57cec5SDimitry Andric             case 0xc6: {
3670b57cec5SDimitry Andric               uint8_t v = getByte(data, offset++);
3680b57cec5SDimitry Andric               uint8_t start = static_cast<uint8_t>(v >> 4);
3690b57cec5SDimitry Andric               uint8_t count_minus_one = v & 0xf;
3700b57cec5SDimitry Andric               if (start + count_minus_one >= 16)
3710b57cec5SDimitry Andric                 return _URC_FAILURE;
3720b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
3730b57cec5SDimitry Andric                               RegisterRange(start, count_minus_one),
3740b57cec5SDimitry Andric                               _UVRSD_DOUBLE);
3750b57cec5SDimitry Andric               break;
3760b57cec5SDimitry Andric             }
3770b57cec5SDimitry Andric             case 0xc7: {
3780b57cec5SDimitry Andric               uint8_t v = getByte(data, offset++);
3790b57cec5SDimitry Andric               if (!v || v & 0xf0)
3800b57cec5SDimitry Andric                 return _URC_FAILURE;
3810b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_WMMXC, v, _UVRSD_DOUBLE);
3820b57cec5SDimitry Andric               break;
3830b57cec5SDimitry Andric             }
3840b57cec5SDimitry Andric #endif
3850b57cec5SDimitry Andric             case 0xc8:
3860b57cec5SDimitry Andric             case 0xc9: {
3870b57cec5SDimitry Andric               uint8_t v = getByte(data, offset++);
3880b57cec5SDimitry Andric               uint8_t start =
3890b57cec5SDimitry Andric                   static_cast<uint8_t>(((byte == 0xc8) ? 16 : 0) + (v >> 4));
3900b57cec5SDimitry Andric               uint8_t count_minus_one = v & 0xf;
3910b57cec5SDimitry Andric               if (start + count_minus_one >= 32)
3920b57cec5SDimitry Andric                 return _URC_FAILURE;
3930b57cec5SDimitry Andric               _Unwind_VRS_Pop(context, _UVRSC_VFP,
3940b57cec5SDimitry Andric                               RegisterRange(start, count_minus_one),
3950b57cec5SDimitry Andric                               _UVRSD_DOUBLE);
3960b57cec5SDimitry Andric               break;
3970b57cec5SDimitry Andric             }
3980b57cec5SDimitry Andric             default:
3990b57cec5SDimitry Andric               return _URC_FAILURE;
4000b57cec5SDimitry Andric           }
4010b57cec5SDimitry Andric           break;
4020b57cec5SDimitry Andric         }
4030b57cec5SDimitry Andric         case 0xd0: {
4040b57cec5SDimitry Andric           if (byte & 0x08)
4050b57cec5SDimitry Andric             return _URC_FAILURE;
4060b57cec5SDimitry Andric           _Unwind_VRS_Pop(context, _UVRSC_VFP, RegisterRange(8, byte & 0x7),
4070b57cec5SDimitry Andric                           _UVRSD_DOUBLE);
4080b57cec5SDimitry Andric           break;
4090b57cec5SDimitry Andric         }
4100b57cec5SDimitry Andric         default:
4110b57cec5SDimitry Andric           return _URC_FAILURE;
4120b57cec5SDimitry Andric       }
4130b57cec5SDimitry Andric     }
4140b57cec5SDimitry Andric   }
4150b57cec5SDimitry Andric   if (!wrotePC) {
4160b57cec5SDimitry Andric     uint32_t lr;
4170b57cec5SDimitry Andric     _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_LR, _UVRSD_UINT32, &lr);
4180b57cec5SDimitry Andric     _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_IP, _UVRSD_UINT32, &lr);
4190b57cec5SDimitry Andric   }
4200b57cec5SDimitry Andric   return _URC_CONTINUE_UNWIND;
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
4240b57cec5SDimitry Andric __aeabi_unwind_cpp_pr0(_Unwind_State state, _Unwind_Control_Block *ucbp,
4250b57cec5SDimitry Andric                        _Unwind_Context *context) {
4260b57cec5SDimitry Andric   return unwindOneFrame(state, ucbp, context);
4270b57cec5SDimitry Andric }
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
4300b57cec5SDimitry Andric __aeabi_unwind_cpp_pr1(_Unwind_State state, _Unwind_Control_Block *ucbp,
4310b57cec5SDimitry Andric                        _Unwind_Context *context) {
4320b57cec5SDimitry Andric   return unwindOneFrame(state, ucbp, context);
4330b57cec5SDimitry Andric }
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
4360b57cec5SDimitry Andric __aeabi_unwind_cpp_pr2(_Unwind_State state, _Unwind_Control_Block *ucbp,
4370b57cec5SDimitry Andric                        _Unwind_Context *context) {
4380b57cec5SDimitry Andric   return unwindOneFrame(state, ucbp, context);
4390b57cec5SDimitry Andric }
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric static _Unwind_Reason_Code
4420b57cec5SDimitry Andric unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
4430b57cec5SDimitry Andric   // EHABI #7.3 discusses preserving the VRS in a "temporary VRS" during
4440b57cec5SDimitry Andric   // phase 1 and then restoring it to the "primary VRS" for phase 2. The
4450b57cec5SDimitry Andric   // effect is phase 2 doesn't see any of the VRS manipulations from phase 1.
4460b57cec5SDimitry Andric   // In this implementation, the phases don't share the VRS backing store.
4470b57cec5SDimitry Andric   // Instead, they are passed the original |uc| and they create a new VRS
4480b57cec5SDimitry Andric   // from scratch thus achieving the same effect.
4490b57cec5SDimitry Andric   __unw_init_local(cursor, uc);
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric   // Walk each frame looking for a place to stop.
4520b57cec5SDimitry Andric   for (bool handlerNotFound = true; handlerNotFound;) {
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric     // See if frame has code to run (has personality routine).
4550b57cec5SDimitry Andric     unw_proc_info_t frameInfo;
4560b57cec5SDimitry Andric     if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
4570b57cec5SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
4580b57cec5SDimitry Andric           "unwind_phase1(ex_ojb=%p): __unw_get_proc_info "
4590b57cec5SDimitry Andric           "failed => _URC_FATAL_PHASE1_ERROR",
4600b57cec5SDimitry Andric           static_cast<void *>(exception_object));
4610b57cec5SDimitry Andric       return _URC_FATAL_PHASE1_ERROR;
4620b57cec5SDimitry Andric     }
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric     // When tracing, print state information.
4650b57cec5SDimitry Andric     if (_LIBUNWIND_TRACING_UNWINDING) {
4660b57cec5SDimitry Andric       char functionBuf[512];
4670b57cec5SDimitry Andric       const char *functionName = functionBuf;
4680b57cec5SDimitry Andric       unw_word_t offset;
4690b57cec5SDimitry Andric       if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
4700b57cec5SDimitry Andric                                &offset) != UNW_ESUCCESS) ||
4710b57cec5SDimitry Andric           (frameInfo.start_ip + offset > frameInfo.end_ip))
4720b57cec5SDimitry Andric         functionName = ".anonymous.";
4730b57cec5SDimitry Andric       unw_word_t pc;
4740b57cec5SDimitry Andric       __unw_get_reg(cursor, UNW_REG_IP, &pc);
4750b57cec5SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
4760b57cec5SDimitry Andric           "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR ", func=%s, "
4770b57cec5SDimitry Andric           "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
4780b57cec5SDimitry Andric           static_cast<void *>(exception_object), pc,
4790b57cec5SDimitry Andric           frameInfo.start_ip, functionName,
4800b57cec5SDimitry Andric           frameInfo.lsda, frameInfo.handler);
4810b57cec5SDimitry Andric     }
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric     // If there is a personality routine, ask it if it will want to stop at
4840b57cec5SDimitry Andric     // this frame.
4850b57cec5SDimitry Andric     if (frameInfo.handler != 0) {
486*5ffd83dbSDimitry Andric       _Unwind_Personality_Fn p =
487*5ffd83dbSDimitry Andric           (_Unwind_Personality_Fn)(long)(frameInfo.handler);
4880b57cec5SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
4890b57cec5SDimitry Andric           "unwind_phase1(ex_ojb=%p): calling personality function %p",
4900b57cec5SDimitry Andric           static_cast<void *>(exception_object),
4910b57cec5SDimitry Andric           reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(p)));
4920b57cec5SDimitry Andric       struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
4930b57cec5SDimitry Andric       exception_object->pr_cache.fnstart = frameInfo.start_ip;
4940b57cec5SDimitry Andric       exception_object->pr_cache.ehtp =
4950b57cec5SDimitry Andric           (_Unwind_EHT_Header *)frameInfo.unwind_info;
4960b57cec5SDimitry Andric       exception_object->pr_cache.additional = frameInfo.flags;
4970b57cec5SDimitry Andric       _Unwind_Reason_Code personalityResult =
4980b57cec5SDimitry Andric           (*p)(_US_VIRTUAL_UNWIND_FRAME, exception_object, context);
4990b57cec5SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
5000b57cec5SDimitry Andric           "unwind_phase1(ex_ojb=%p): personality result %d start_ip %x ehtp %p "
5010b57cec5SDimitry Andric           "additional %x",
5020b57cec5SDimitry Andric           static_cast<void *>(exception_object), personalityResult,
5030b57cec5SDimitry Andric           exception_object->pr_cache.fnstart,
5040b57cec5SDimitry Andric           static_cast<void *>(exception_object->pr_cache.ehtp),
5050b57cec5SDimitry Andric           exception_object->pr_cache.additional);
5060b57cec5SDimitry Andric       switch (personalityResult) {
5070b57cec5SDimitry Andric       case _URC_HANDLER_FOUND:
5080b57cec5SDimitry Andric         // found a catch clause or locals that need destructing in this frame
5090b57cec5SDimitry Andric         // stop search and remember stack pointer at the frame
5100b57cec5SDimitry Andric         handlerNotFound = false;
5110b57cec5SDimitry Andric         // p should have initialized barrier_cache. EHABI #7.3.5
5120b57cec5SDimitry Andric         _LIBUNWIND_TRACE_UNWINDING(
5130b57cec5SDimitry Andric             "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
5140b57cec5SDimitry Andric             static_cast<void *>(exception_object));
5150b57cec5SDimitry Andric         return _URC_NO_REASON;
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric       case _URC_CONTINUE_UNWIND:
5180b57cec5SDimitry Andric         _LIBUNWIND_TRACE_UNWINDING(
5190b57cec5SDimitry Andric             "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
5200b57cec5SDimitry Andric             static_cast<void *>(exception_object));
5210b57cec5SDimitry Andric         // continue unwinding
5220b57cec5SDimitry Andric         break;
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric       // EHABI #7.3.3
5250b57cec5SDimitry Andric       case _URC_FAILURE:
5260b57cec5SDimitry Andric         return _URC_FAILURE;
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric       default:
5290b57cec5SDimitry Andric         // something went wrong
5300b57cec5SDimitry Andric         _LIBUNWIND_TRACE_UNWINDING(
5310b57cec5SDimitry Andric             "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
5320b57cec5SDimitry Andric             static_cast<void *>(exception_object));
5330b57cec5SDimitry Andric         return _URC_FATAL_PHASE1_ERROR;
5340b57cec5SDimitry Andric       }
5350b57cec5SDimitry Andric     }
5360b57cec5SDimitry Andric   }
5370b57cec5SDimitry Andric   return _URC_NO_REASON;
5380b57cec5SDimitry Andric }
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric static _Unwind_Reason_Code unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor,
5410b57cec5SDimitry Andric                                          _Unwind_Exception *exception_object,
5420b57cec5SDimitry Andric                                          bool resume) {
5430b57cec5SDimitry Andric   // See comment at the start of unwind_phase1 regarding VRS integrity.
5440b57cec5SDimitry Andric   __unw_init_local(cursor, uc);
5450b57cec5SDimitry Andric 
5460b57cec5SDimitry Andric   _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
5470b57cec5SDimitry Andric                              static_cast<void *>(exception_object));
5480b57cec5SDimitry Andric   int frame_count = 0;
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric   // Walk each frame until we reach where search phase said to stop.
5510b57cec5SDimitry Andric   while (true) {
5520b57cec5SDimitry Andric     // Ask libunwind to get next frame (skip over first which is
5530b57cec5SDimitry Andric     // _Unwind_RaiseException or _Unwind_Resume).
5540b57cec5SDimitry Andric     //
5550b57cec5SDimitry Andric     // Resume only ever makes sense for 1 frame.
5560b57cec5SDimitry Andric     _Unwind_State state =
5570b57cec5SDimitry Andric         resume ? _US_UNWIND_FRAME_RESUME : _US_UNWIND_FRAME_STARTING;
5580b57cec5SDimitry Andric     if (resume && frame_count == 1) {
5590b57cec5SDimitry Andric       // On a resume, first unwind the _Unwind_Resume() frame. The next frame
5600b57cec5SDimitry Andric       // is now the landing pad for the cleanup from a previous execution of
5610b57cec5SDimitry Andric       // phase2. To continue unwindingly correctly, replace VRS[15] with the
5620b57cec5SDimitry Andric       // IP of the frame that the previous run of phase2 installed the context
5630b57cec5SDimitry Andric       // for. After this, continue unwinding as if normal.
5640b57cec5SDimitry Andric       //
5650b57cec5SDimitry Andric       // See #7.4.6 for details.
5660b57cec5SDimitry Andric       __unw_set_reg(cursor, UNW_REG_IP,
5670b57cec5SDimitry Andric                     exception_object->unwinder_cache.reserved2);
5680b57cec5SDimitry Andric       resume = false;
5690b57cec5SDimitry Andric     }
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric     // Get info about this frame.
5720b57cec5SDimitry Andric     unw_word_t sp;
5730b57cec5SDimitry Andric     unw_proc_info_t frameInfo;
5740b57cec5SDimitry Andric     __unw_get_reg(cursor, UNW_REG_SP, &sp);
5750b57cec5SDimitry Andric     if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
5760b57cec5SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
5770b57cec5SDimitry Andric           "unwind_phase2(ex_ojb=%p): __unw_get_proc_info "
5780b57cec5SDimitry Andric           "failed => _URC_FATAL_PHASE2_ERROR",
5790b57cec5SDimitry Andric           static_cast<void *>(exception_object));
5800b57cec5SDimitry Andric       return _URC_FATAL_PHASE2_ERROR;
5810b57cec5SDimitry Andric     }
5820b57cec5SDimitry Andric 
5830b57cec5SDimitry Andric     // When tracing, print state information.
5840b57cec5SDimitry Andric     if (_LIBUNWIND_TRACING_UNWINDING) {
5850b57cec5SDimitry Andric       char functionBuf[512];
5860b57cec5SDimitry Andric       const char *functionName = functionBuf;
5870b57cec5SDimitry Andric       unw_word_t offset;
5880b57cec5SDimitry Andric       if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
5890b57cec5SDimitry Andric                                &offset) != UNW_ESUCCESS) ||
5900b57cec5SDimitry Andric           (frameInfo.start_ip + offset > frameInfo.end_ip))
5910b57cec5SDimitry Andric         functionName = ".anonymous.";
5920b57cec5SDimitry Andric       _LIBUNWIND_TRACE_UNWINDING(
5930b57cec5SDimitry Andric           "unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR ", func=%s, sp=0x%" PRIxPTR ", "
5940b57cec5SDimitry Andric           "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
5950b57cec5SDimitry Andric           static_cast<void *>(exception_object), frameInfo.start_ip,
5960b57cec5SDimitry Andric           functionName, sp, frameInfo.lsda,
5970b57cec5SDimitry Andric           frameInfo.handler);
5980b57cec5SDimitry Andric     }
5990b57cec5SDimitry Andric 
6000b57cec5SDimitry Andric     // If there is a personality routine, tell it we are unwinding.
6010b57cec5SDimitry Andric     if (frameInfo.handler != 0) {
602*5ffd83dbSDimitry Andric       _Unwind_Personality_Fn p =
603*5ffd83dbSDimitry Andric           (_Unwind_Personality_Fn)(long)(frameInfo.handler);
6040b57cec5SDimitry Andric       struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
6050b57cec5SDimitry Andric       // EHABI #7.2
6060b57cec5SDimitry Andric       exception_object->pr_cache.fnstart = frameInfo.start_ip;
6070b57cec5SDimitry Andric       exception_object->pr_cache.ehtp =
6080b57cec5SDimitry Andric           (_Unwind_EHT_Header *)frameInfo.unwind_info;
6090b57cec5SDimitry Andric       exception_object->pr_cache.additional = frameInfo.flags;
6100b57cec5SDimitry Andric       _Unwind_Reason_Code personalityResult =
6110b57cec5SDimitry Andric           (*p)(state, exception_object, context);
6120b57cec5SDimitry Andric       switch (personalityResult) {
6130b57cec5SDimitry Andric       case _URC_CONTINUE_UNWIND:
6140b57cec5SDimitry Andric         // Continue unwinding
6150b57cec5SDimitry Andric         _LIBUNWIND_TRACE_UNWINDING(
6160b57cec5SDimitry Andric             "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
6170b57cec5SDimitry Andric             static_cast<void *>(exception_object));
6180b57cec5SDimitry Andric         // EHABI #7.2
6190b57cec5SDimitry Andric         if (sp == exception_object->barrier_cache.sp) {
6200b57cec5SDimitry Andric           // Phase 1 said we would stop at this frame, but we did not...
6210b57cec5SDimitry Andric           _LIBUNWIND_ABORT("during phase1 personality function said it would "
6220b57cec5SDimitry Andric                            "stop here, but now in phase2 it did not stop here");
6230b57cec5SDimitry Andric         }
6240b57cec5SDimitry Andric         break;
6250b57cec5SDimitry Andric       case _URC_INSTALL_CONTEXT:
6260b57cec5SDimitry Andric         _LIBUNWIND_TRACE_UNWINDING(
6270b57cec5SDimitry Andric             "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
6280b57cec5SDimitry Andric             static_cast<void *>(exception_object));
6290b57cec5SDimitry Andric         // Personality routine says to transfer control to landing pad.
6300b57cec5SDimitry Andric         // We may get control back if landing pad calls _Unwind_Resume().
6310b57cec5SDimitry Andric         if (_LIBUNWIND_TRACING_UNWINDING) {
6320b57cec5SDimitry Andric           unw_word_t pc;
6330b57cec5SDimitry Andric           __unw_get_reg(cursor, UNW_REG_IP, &pc);
6340b57cec5SDimitry Andric           __unw_get_reg(cursor, UNW_REG_SP, &sp);
6350b57cec5SDimitry Andric           _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
6360b57cec5SDimitry Andric                                      "user code with ip=0x%" PRIxPTR ", sp=0x%" PRIxPTR,
6370b57cec5SDimitry Andric                                      static_cast<void *>(exception_object),
6380b57cec5SDimitry Andric                                      pc, sp);
6390b57cec5SDimitry Andric         }
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric         {
6420b57cec5SDimitry Andric           // EHABI #7.4.1 says we need to preserve pc for when _Unwind_Resume
6430b57cec5SDimitry Andric           // is called back, to find this same frame.
6440b57cec5SDimitry Andric           unw_word_t pc;
6450b57cec5SDimitry Andric           __unw_get_reg(cursor, UNW_REG_IP, &pc);
6460b57cec5SDimitry Andric           exception_object->unwinder_cache.reserved2 = (uint32_t)pc;
6470b57cec5SDimitry Andric         }
6480b57cec5SDimitry Andric         __unw_resume(cursor);
6490b57cec5SDimitry Andric         // __unw_resume() only returns if there was an error.
6500b57cec5SDimitry Andric         return _URC_FATAL_PHASE2_ERROR;
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric       // # EHABI #7.4.3
6530b57cec5SDimitry Andric       case _URC_FAILURE:
6540b57cec5SDimitry Andric         abort();
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric       default:
6570b57cec5SDimitry Andric         // Personality routine returned an unknown result code.
6580b57cec5SDimitry Andric         _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
6590b57cec5SDimitry Andric                       personalityResult);
6600b57cec5SDimitry Andric         return _URC_FATAL_PHASE2_ERROR;
6610b57cec5SDimitry Andric       }
6620b57cec5SDimitry Andric     }
6630b57cec5SDimitry Andric     frame_count++;
6640b57cec5SDimitry Andric   }
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric   // Clean up phase did not resume at the frame that the search phase
6670b57cec5SDimitry Andric   // said it would...
6680b57cec5SDimitry Andric   return _URC_FATAL_PHASE2_ERROR;
6690b57cec5SDimitry Andric }
6700b57cec5SDimitry Andric 
6710b57cec5SDimitry Andric /// Called by __cxa_throw.  Only returns if there is a fatal error.
6720b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_Reason_Code
6730b57cec5SDimitry Andric _Unwind_RaiseException(_Unwind_Exception *exception_object) {
6740b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
6750b57cec5SDimitry Andric                        static_cast<void *>(exception_object));
6760b57cec5SDimitry Andric   unw_context_t uc;
6770b57cec5SDimitry Andric   unw_cursor_t cursor;
6780b57cec5SDimitry Andric   __unw_getcontext(&uc);
6790b57cec5SDimitry Andric 
6800b57cec5SDimitry Andric   // This field for is for compatibility with GCC to say this isn't a forced
6810b57cec5SDimitry Andric   // unwind. EHABI #7.2
6820b57cec5SDimitry Andric   exception_object->unwinder_cache.reserved1 = 0;
6830b57cec5SDimitry Andric 
6840b57cec5SDimitry Andric   // phase 1: the search phase
6850b57cec5SDimitry Andric   _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
6860b57cec5SDimitry Andric   if (phase1 != _URC_NO_REASON)
6870b57cec5SDimitry Andric     return phase1;
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric   // phase 2: the clean up phase
6900b57cec5SDimitry Andric   return unwind_phase2(&uc, &cursor, exception_object, false);
6910b57cec5SDimitry Andric }
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric _LIBUNWIND_EXPORT void _Unwind_Complete(_Unwind_Exception* exception_object) {
6940b57cec5SDimitry Andric   // This is to be called when exception handling completes to give us a chance
6950b57cec5SDimitry Andric   // to perform any housekeeping. EHABI #7.2. But we have nothing to do here.
6960b57cec5SDimitry Andric   (void)exception_object;
6970b57cec5SDimitry Andric }
6980b57cec5SDimitry Andric 
6990b57cec5SDimitry Andric /// When _Unwind_RaiseException() is in phase2, it hands control
7000b57cec5SDimitry Andric /// to the personality function at each frame.  The personality
7010b57cec5SDimitry Andric /// may force a jump to a landing pad in that function, the landing
7020b57cec5SDimitry Andric /// pad code may then call _Unwind_Resume() to continue with the
7030b57cec5SDimitry Andric /// unwinding.  Note: the call to _Unwind_Resume() is from compiler
7040b57cec5SDimitry Andric /// geneated user code.  All other _Unwind_* routines are called
7050b57cec5SDimitry Andric /// by the C++ runtime __cxa_* routines.
7060b57cec5SDimitry Andric ///
7070b57cec5SDimitry Andric /// Note: re-throwing an exception (as opposed to continuing the unwind)
7080b57cec5SDimitry Andric /// is implemented by having the code call __cxa_rethrow() which
7090b57cec5SDimitry Andric /// in turn calls _Unwind_Resume_or_Rethrow().
7100b57cec5SDimitry Andric _LIBUNWIND_EXPORT void
7110b57cec5SDimitry Andric _Unwind_Resume(_Unwind_Exception *exception_object) {
7120b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)",
7130b57cec5SDimitry Andric                        static_cast<void *>(exception_object));
7140b57cec5SDimitry Andric   unw_context_t uc;
7150b57cec5SDimitry Andric   unw_cursor_t cursor;
7160b57cec5SDimitry Andric   __unw_getcontext(&uc);
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric   // _Unwind_RaiseException on EHABI will always set the reserved1 field to 0,
7190b57cec5SDimitry Andric   // which is in the same position as private_1 below.
7200b57cec5SDimitry Andric   // TODO(ajwong): Who wronte the above? Why is it true?
7210b57cec5SDimitry Andric   unwind_phase2(&uc, &cursor, exception_object, true);
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric   // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
7240b57cec5SDimitry Andric   _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
7250b57cec5SDimitry Andric }
7260b57cec5SDimitry Andric 
7270b57cec5SDimitry Andric /// Called by personality handler during phase 2 to get LSDA for current frame.
7280b57cec5SDimitry Andric _LIBUNWIND_EXPORT uintptr_t
7290b57cec5SDimitry Andric _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
7300b57cec5SDimitry Andric   unw_cursor_t *cursor = (unw_cursor_t *)context;
7310b57cec5SDimitry Andric   unw_proc_info_t frameInfo;
7320b57cec5SDimitry Andric   uintptr_t result = 0;
7330b57cec5SDimitry Andric   if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
7340b57cec5SDimitry Andric     result = (uintptr_t)frameInfo.lsda;
7350b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API(
7360b57cec5SDimitry Andric       "_Unwind_GetLanguageSpecificData(context=%p) => 0x%llx",
7370b57cec5SDimitry Andric       static_cast<void *>(context), (long long)result);
7380b57cec5SDimitry Andric   return result;
7390b57cec5SDimitry Andric }
7400b57cec5SDimitry Andric 
7410b57cec5SDimitry Andric static uint64_t ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation,
7420b57cec5SDimitry Andric                                   void* valuep) {
7430b57cec5SDimitry Andric   uint64_t value = 0;
7440b57cec5SDimitry Andric   switch (representation) {
7450b57cec5SDimitry Andric     case _UVRSD_UINT32:
7460b57cec5SDimitry Andric     case _UVRSD_FLOAT:
7470b57cec5SDimitry Andric       memcpy(&value, valuep, sizeof(uint32_t));
7480b57cec5SDimitry Andric       break;
7490b57cec5SDimitry Andric 
7500b57cec5SDimitry Andric     case _UVRSD_VFPX:
7510b57cec5SDimitry Andric     case _UVRSD_UINT64:
7520b57cec5SDimitry Andric     case _UVRSD_DOUBLE:
7530b57cec5SDimitry Andric       memcpy(&value, valuep, sizeof(uint64_t));
7540b57cec5SDimitry Andric       break;
7550b57cec5SDimitry Andric   }
7560b57cec5SDimitry Andric   return value;
7570b57cec5SDimitry Andric }
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_VRS_Result
7600b57cec5SDimitry Andric _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
7610b57cec5SDimitry Andric                 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
7620b57cec5SDimitry Andric                 void *valuep) {
7630b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_VRS_Set(context=%p, regclass=%d, reg=%d, "
7640b57cec5SDimitry Andric                        "rep=%d, value=0x%llX)",
7650b57cec5SDimitry Andric                        static_cast<void *>(context), regclass, regno,
7660b57cec5SDimitry Andric                        representation,
7670b57cec5SDimitry Andric                        ValueAsBitPattern(representation, valuep));
7680b57cec5SDimitry Andric   unw_cursor_t *cursor = (unw_cursor_t *)context;
7690b57cec5SDimitry Andric   switch (regclass) {
7700b57cec5SDimitry Andric     case _UVRSC_CORE:
7710b57cec5SDimitry Andric       if (representation != _UVRSD_UINT32 || regno > 15)
7720b57cec5SDimitry Andric         return _UVRSR_FAILED;
7730b57cec5SDimitry Andric       return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
7740b57cec5SDimitry Andric                            *(unw_word_t *)valuep) == UNW_ESUCCESS
7750b57cec5SDimitry Andric                  ? _UVRSR_OK
7760b57cec5SDimitry Andric                  : _UVRSR_FAILED;
7770b57cec5SDimitry Andric     case _UVRSC_VFP:
7780b57cec5SDimitry Andric       if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
7790b57cec5SDimitry Andric         return _UVRSR_FAILED;
7800b57cec5SDimitry Andric       if (representation == _UVRSD_VFPX) {
7810b57cec5SDimitry Andric         // Can only touch d0-15 with FSTMFDX.
7820b57cec5SDimitry Andric         if (regno > 15)
7830b57cec5SDimitry Andric           return _UVRSR_FAILED;
7840b57cec5SDimitry Andric         __unw_save_vfp_as_X(cursor);
7850b57cec5SDimitry Andric       } else {
7860b57cec5SDimitry Andric         if (regno > 31)
7870b57cec5SDimitry Andric           return _UVRSR_FAILED;
7880b57cec5SDimitry Andric       }
7890b57cec5SDimitry Andric       return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
7900b57cec5SDimitry Andric                              *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
7910b57cec5SDimitry Andric                  ? _UVRSR_OK
7920b57cec5SDimitry Andric                  : _UVRSR_FAILED;
7930b57cec5SDimitry Andric #if defined(__ARM_WMMX)
7940b57cec5SDimitry Andric     case _UVRSC_WMMXC:
7950b57cec5SDimitry Andric       if (representation != _UVRSD_UINT32 || regno > 3)
7960b57cec5SDimitry Andric         return _UVRSR_FAILED;
7970b57cec5SDimitry Andric       return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
7980b57cec5SDimitry Andric                            *(unw_word_t *)valuep) == UNW_ESUCCESS
7990b57cec5SDimitry Andric                  ? _UVRSR_OK
8000b57cec5SDimitry Andric                  : _UVRSR_FAILED;
8010b57cec5SDimitry Andric     case _UVRSC_WMMXD:
8020b57cec5SDimitry Andric       if (representation != _UVRSD_DOUBLE || regno > 31)
8030b57cec5SDimitry Andric         return _UVRSR_FAILED;
8040b57cec5SDimitry Andric       return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
8050b57cec5SDimitry Andric                              *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
8060b57cec5SDimitry Andric                  ? _UVRSR_OK
8070b57cec5SDimitry Andric                  : _UVRSR_FAILED;
8080b57cec5SDimitry Andric #else
8090b57cec5SDimitry Andric     case _UVRSC_WMMXC:
8100b57cec5SDimitry Andric     case _UVRSC_WMMXD:
8110b57cec5SDimitry Andric       break;
8120b57cec5SDimitry Andric #endif
8130b57cec5SDimitry Andric   }
8140b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported register class");
8150b57cec5SDimitry Andric }
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric static _Unwind_VRS_Result
8180b57cec5SDimitry Andric _Unwind_VRS_Get_Internal(_Unwind_Context *context,
8190b57cec5SDimitry Andric                          _Unwind_VRS_RegClass regclass, uint32_t regno,
8200b57cec5SDimitry Andric                          _Unwind_VRS_DataRepresentation representation,
8210b57cec5SDimitry Andric                          void *valuep) {
8220b57cec5SDimitry Andric   unw_cursor_t *cursor = (unw_cursor_t *)context;
8230b57cec5SDimitry Andric   switch (regclass) {
8240b57cec5SDimitry Andric     case _UVRSC_CORE:
8250b57cec5SDimitry Andric       if (representation != _UVRSD_UINT32 || regno > 15)
8260b57cec5SDimitry Andric         return _UVRSR_FAILED;
8270b57cec5SDimitry Andric       return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
8280b57cec5SDimitry Andric                            (unw_word_t *)valuep) == UNW_ESUCCESS
8290b57cec5SDimitry Andric                  ? _UVRSR_OK
8300b57cec5SDimitry Andric                  : _UVRSR_FAILED;
8310b57cec5SDimitry Andric     case _UVRSC_VFP:
8320b57cec5SDimitry Andric       if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
8330b57cec5SDimitry Andric         return _UVRSR_FAILED;
8340b57cec5SDimitry Andric       if (representation == _UVRSD_VFPX) {
8350b57cec5SDimitry Andric         // Can only touch d0-15 with FSTMFDX.
8360b57cec5SDimitry Andric         if (regno > 15)
8370b57cec5SDimitry Andric           return _UVRSR_FAILED;
8380b57cec5SDimitry Andric         __unw_save_vfp_as_X(cursor);
8390b57cec5SDimitry Andric       } else {
8400b57cec5SDimitry Andric         if (regno > 31)
8410b57cec5SDimitry Andric           return _UVRSR_FAILED;
8420b57cec5SDimitry Andric       }
8430b57cec5SDimitry Andric       return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
8440b57cec5SDimitry Andric                              (unw_fpreg_t *)valuep) == UNW_ESUCCESS
8450b57cec5SDimitry Andric                  ? _UVRSR_OK
8460b57cec5SDimitry Andric                  : _UVRSR_FAILED;
8470b57cec5SDimitry Andric #if defined(__ARM_WMMX)
8480b57cec5SDimitry Andric     case _UVRSC_WMMXC:
8490b57cec5SDimitry Andric       if (representation != _UVRSD_UINT32 || regno > 3)
8500b57cec5SDimitry Andric         return _UVRSR_FAILED;
8510b57cec5SDimitry Andric       return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
8520b57cec5SDimitry Andric                            (unw_word_t *)valuep) == UNW_ESUCCESS
8530b57cec5SDimitry Andric                  ? _UVRSR_OK
8540b57cec5SDimitry Andric                  : _UVRSR_FAILED;
8550b57cec5SDimitry Andric     case _UVRSC_WMMXD:
8560b57cec5SDimitry Andric       if (representation != _UVRSD_DOUBLE || regno > 31)
8570b57cec5SDimitry Andric         return _UVRSR_FAILED;
8580b57cec5SDimitry Andric       return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
8590b57cec5SDimitry Andric                              (unw_fpreg_t *)valuep) == UNW_ESUCCESS
8600b57cec5SDimitry Andric                  ? _UVRSR_OK
8610b57cec5SDimitry Andric                  : _UVRSR_FAILED;
8620b57cec5SDimitry Andric #else
8630b57cec5SDimitry Andric     case _UVRSC_WMMXC:
8640b57cec5SDimitry Andric     case _UVRSC_WMMXD:
8650b57cec5SDimitry Andric       break;
8660b57cec5SDimitry Andric #endif
8670b57cec5SDimitry Andric   }
8680b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported register class");
8690b57cec5SDimitry Andric }
8700b57cec5SDimitry Andric 
8710b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_VRS_Result
8720b57cec5SDimitry Andric _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
8730b57cec5SDimitry Andric                 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
8740b57cec5SDimitry Andric                 void *valuep) {
8750b57cec5SDimitry Andric   _Unwind_VRS_Result result =
8760b57cec5SDimitry Andric       _Unwind_VRS_Get_Internal(context, regclass, regno, representation,
8770b57cec5SDimitry Andric                                valuep);
8780b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_VRS_Get(context=%p, regclass=%d, reg=%d, "
8790b57cec5SDimitry Andric                        "rep=%d, value=0x%llX, result = %d)",
8800b57cec5SDimitry Andric                        static_cast<void *>(context), regclass, regno,
8810b57cec5SDimitry Andric                        representation,
8820b57cec5SDimitry Andric                        ValueAsBitPattern(representation, valuep), result);
8830b57cec5SDimitry Andric   return result;
8840b57cec5SDimitry Andric }
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric _Unwind_VRS_Result
8870b57cec5SDimitry Andric _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
8880b57cec5SDimitry Andric                 uint32_t discriminator,
8890b57cec5SDimitry Andric                 _Unwind_VRS_DataRepresentation representation) {
8900b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_VRS_Pop(context=%p, regclass=%d, "
8910b57cec5SDimitry Andric                        "discriminator=%d, representation=%d)",
8920b57cec5SDimitry Andric                        static_cast<void *>(context), regclass, discriminator,
8930b57cec5SDimitry Andric                        representation);
8940b57cec5SDimitry Andric   switch (regclass) {
8950b57cec5SDimitry Andric     case _UVRSC_WMMXC:
8960b57cec5SDimitry Andric #if !defined(__ARM_WMMX)
8970b57cec5SDimitry Andric       break;
8980b57cec5SDimitry Andric #endif
8990b57cec5SDimitry Andric     case _UVRSC_CORE: {
9000b57cec5SDimitry Andric       if (representation != _UVRSD_UINT32)
9010b57cec5SDimitry Andric         return _UVRSR_FAILED;
9020b57cec5SDimitry Andric       // When popping SP from the stack, we don't want to override it from the
9030b57cec5SDimitry Andric       // computed new stack location. See EHABI #7.5.4 table 3.
9040b57cec5SDimitry Andric       bool poppedSP = false;
9050b57cec5SDimitry Andric       uint32_t* sp;
9060b57cec5SDimitry Andric       if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
9070b57cec5SDimitry Andric                           _UVRSD_UINT32, &sp) != _UVRSR_OK) {
9080b57cec5SDimitry Andric         return _UVRSR_FAILED;
9090b57cec5SDimitry Andric       }
9100b57cec5SDimitry Andric       for (uint32_t i = 0; i < 16; ++i) {
9110b57cec5SDimitry Andric         if (!(discriminator & static_cast<uint32_t>(1 << i)))
9120b57cec5SDimitry Andric           continue;
9130b57cec5SDimitry Andric         uint32_t value = *sp++;
9140b57cec5SDimitry Andric         if (regclass == _UVRSC_CORE && i == 13)
9150b57cec5SDimitry Andric           poppedSP = true;
9160b57cec5SDimitry Andric         if (_Unwind_VRS_Set(context, regclass, i,
9170b57cec5SDimitry Andric                             _UVRSD_UINT32, &value) != _UVRSR_OK) {
9180b57cec5SDimitry Andric           return _UVRSR_FAILED;
9190b57cec5SDimitry Andric         }
9200b57cec5SDimitry Andric       }
9210b57cec5SDimitry Andric       if (!poppedSP) {
9220b57cec5SDimitry Andric         return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP,
9230b57cec5SDimitry Andric                                _UVRSD_UINT32, &sp);
9240b57cec5SDimitry Andric       }
9250b57cec5SDimitry Andric       return _UVRSR_OK;
9260b57cec5SDimitry Andric     }
9270b57cec5SDimitry Andric     case _UVRSC_WMMXD:
9280b57cec5SDimitry Andric #if !defined(__ARM_WMMX)
9290b57cec5SDimitry Andric       break;
9300b57cec5SDimitry Andric #endif
9310b57cec5SDimitry Andric     case _UVRSC_VFP: {
9320b57cec5SDimitry Andric       if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
9330b57cec5SDimitry Andric         return _UVRSR_FAILED;
9340b57cec5SDimitry Andric       uint32_t first = discriminator >> 16;
9350b57cec5SDimitry Andric       uint32_t count = discriminator & 0xffff;
9360b57cec5SDimitry Andric       uint32_t end = first+count;
9370b57cec5SDimitry Andric       uint32_t* sp;
9380b57cec5SDimitry Andric       if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
9390b57cec5SDimitry Andric                           _UVRSD_UINT32, &sp) != _UVRSR_OK) {
9400b57cec5SDimitry Andric         return _UVRSR_FAILED;
9410b57cec5SDimitry Andric       }
9420b57cec5SDimitry Andric       // For _UVRSD_VFPX, we're assuming the data is stored in FSTMX "standard
9430b57cec5SDimitry Andric       // format 1", which is equivalent to FSTMD + a padding word.
9440b57cec5SDimitry Andric       for (uint32_t i = first; i < end; ++i) {
9450b57cec5SDimitry Andric         // SP is only 32-bit aligned so don't copy 64-bit at a time.
946c2c6a179SDimitry Andric         uint64_t w0 = *sp++;
947c2c6a179SDimitry Andric         uint64_t w1 = *sp++;
948*5ffd83dbSDimitry Andric #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
949c2c6a179SDimitry Andric         uint64_t value = (w1 << 32) | w0;
950*5ffd83dbSDimitry Andric #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
951c2c6a179SDimitry Andric         uint64_t value = (w0 << 32) | w1;
952*5ffd83dbSDimitry Andric #else
953*5ffd83dbSDimitry Andric #error "Unable to determine endianess"
954c2c6a179SDimitry Andric #endif
9550b57cec5SDimitry Andric         if (_Unwind_VRS_Set(context, regclass, i, representation, &value) !=
9560b57cec5SDimitry Andric             _UVRSR_OK)
9570b57cec5SDimitry Andric           return _UVRSR_FAILED;
9580b57cec5SDimitry Andric       }
9590b57cec5SDimitry Andric       if (representation == _UVRSD_VFPX)
9600b57cec5SDimitry Andric         ++sp;
9610b57cec5SDimitry Andric       return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
9620b57cec5SDimitry Andric                              &sp);
9630b57cec5SDimitry Andric     }
9640b57cec5SDimitry Andric   }
9650b57cec5SDimitry Andric   _LIBUNWIND_ABORT("unsupported register class");
9660b57cec5SDimitry Andric }
9670b57cec5SDimitry Andric 
9680b57cec5SDimitry Andric /// Called by personality handler during phase 2 to find the start of the
9690b57cec5SDimitry Andric /// function.
9700b57cec5SDimitry Andric _LIBUNWIND_EXPORT uintptr_t
9710b57cec5SDimitry Andric _Unwind_GetRegionStart(struct _Unwind_Context *context) {
9720b57cec5SDimitry Andric   unw_cursor_t *cursor = (unw_cursor_t *)context;
9730b57cec5SDimitry Andric   unw_proc_info_t frameInfo;
9740b57cec5SDimitry Andric   uintptr_t result = 0;
9750b57cec5SDimitry Andric   if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
9760b57cec5SDimitry Andric     result = (uintptr_t)frameInfo.start_ip;
9770b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%llX",
9780b57cec5SDimitry Andric                        static_cast<void *>(context), (long long)result);
9790b57cec5SDimitry Andric   return result;
9800b57cec5SDimitry Andric }
9810b57cec5SDimitry Andric 
9820b57cec5SDimitry Andric 
9830b57cec5SDimitry Andric /// Called by personality handler during phase 2 if a foreign exception
9840b57cec5SDimitry Andric // is caught.
9850b57cec5SDimitry Andric _LIBUNWIND_EXPORT void
9860b57cec5SDimitry Andric _Unwind_DeleteException(_Unwind_Exception *exception_object) {
9870b57cec5SDimitry Andric   _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
9880b57cec5SDimitry Andric                        static_cast<void *>(exception_object));
9890b57cec5SDimitry Andric   if (exception_object->exception_cleanup != NULL)
9900b57cec5SDimitry Andric     (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
9910b57cec5SDimitry Andric                                            exception_object);
9920b57cec5SDimitry Andric }
9930b57cec5SDimitry Andric 
9940b57cec5SDimitry Andric extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
9950b57cec5SDimitry Andric __gnu_unwind_frame(_Unwind_Exception *exception_object,
9960b57cec5SDimitry Andric                    struct _Unwind_Context *context) {
9970b57cec5SDimitry Andric   unw_cursor_t *cursor = (unw_cursor_t *)context;
9980b57cec5SDimitry Andric   if (__unw_step(cursor) != UNW_STEP_SUCCESS)
9990b57cec5SDimitry Andric     return _URC_FAILURE;
10000b57cec5SDimitry Andric   return _URC_OK;
10010b57cec5SDimitry Andric }
10020b57cec5SDimitry Andric 
10030b57cec5SDimitry Andric #endif  // defined(_LIBUNWIND_ARM_EHABI)
1004