1*349cc55cSDimitry 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. 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 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); 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 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 1900b57cec5SDimitry Andric if (__unw_step(reinterpret_cast<unw_cursor_t *>(context)) != UNW_STEP_SUCCESS) 1910b57cec5SDimitry Andric return _URC_FAILURE; 1920b57cec5SDimitry Andric return _URC_CONTINUE_UNWIND; 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_CORE / 1960b57cec5SDimitry Andric // _UVRSD_UINT32. 1970b57cec5SDimitry Andric uint32_t RegisterMask(uint8_t start, uint8_t count_minus_one) { 1980b57cec5SDimitry Andric return ((1U << (count_minus_one + 1)) - 1) << start; 1990b57cec5SDimitry Andric } 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_VFP / 2020b57cec5SDimitry Andric // _UVRSD_DOUBLE. 2030b57cec5SDimitry Andric uint32_t RegisterRange(uint8_t start, uint8_t count_minus_one) { 2040b57cec5SDimitry Andric return ((uint32_t)start << 16) | ((uint32_t)count_minus_one + 1); 2050b57cec5SDimitry Andric } 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric } // end anonymous namespace 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric /** 2100b57cec5SDimitry Andric * Decodes an EHT entry. 2110b57cec5SDimitry Andric * 2120b57cec5SDimitry Andric * @param data Pointer to EHT. 2130b57cec5SDimitry Andric * @param[out] off Offset from return value (in bytes) to begin interpretation. 2140b57cec5SDimitry Andric * @param[out] len Number of bytes in unwind code. 2150b57cec5SDimitry Andric * @return Pointer to beginning of unwind code. 2160b57cec5SDimitry Andric */ 2170b57cec5SDimitry Andric extern "C" const uint32_t* 2180b57cec5SDimitry Andric decode_eht_entry(const uint32_t* data, size_t* off, size_t* len) { 2190b57cec5SDimitry Andric if ((*data & 0x80000000) == 0) { 2200b57cec5SDimitry Andric // 6.2: Generic Model 2210b57cec5SDimitry Andric // 2220b57cec5SDimitry Andric // EHT entry is a prel31 pointing to the PR, followed by data understood 2230b57cec5SDimitry Andric // only by the personality routine. Fortunately, all existing assembler 2240b57cec5SDimitry Andric // implementations, including GNU assembler, LLVM integrated assembler, 2250b57cec5SDimitry Andric // and ARM assembler, assume that the unwind opcodes come after the 2260b57cec5SDimitry Andric // personality rountine address. 2270b57cec5SDimitry Andric *off = 1; // First byte is size data. 2280b57cec5SDimitry Andric *len = (((data[1] >> 24) & 0xff) + 1) * 4; 2290b57cec5SDimitry Andric data++; // Skip the first word, which is the prel31 offset. 2300b57cec5SDimitry Andric } else { 2310b57cec5SDimitry Andric // 6.3: ARM Compact Model 2320b57cec5SDimitry Andric // 2330b57cec5SDimitry Andric // EHT entries here correspond to the __aeabi_unwind_cpp_pr[012] PRs indeded 2340b57cec5SDimitry Andric // by format: 2350b57cec5SDimitry Andric Descriptor::Format format = 2360b57cec5SDimitry Andric static_cast<Descriptor::Format>((*data & 0x0f000000) >> 24); 2370b57cec5SDimitry Andric switch (format) { 2380b57cec5SDimitry Andric case Descriptor::SU16: 2390b57cec5SDimitry Andric *len = 4; 2400b57cec5SDimitry Andric *off = 1; 2410b57cec5SDimitry Andric break; 2420b57cec5SDimitry Andric case Descriptor::LU16: 2430b57cec5SDimitry Andric case Descriptor::LU32: 2440b57cec5SDimitry Andric *len = 4 + 4 * ((*data & 0x00ff0000) >> 16); 2450b57cec5SDimitry Andric *off = 2; 2460b57cec5SDimitry Andric break; 2470b57cec5SDimitry Andric default: 2480b57cec5SDimitry Andric return nullptr; 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric } 2510b57cec5SDimitry Andric return data; 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric 2540b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_Reason_Code 2550b57cec5SDimitry Andric _Unwind_VRS_Interpret(_Unwind_Context *context, const uint32_t *data, 2560b57cec5SDimitry Andric size_t offset, size_t len) { 2570b57cec5SDimitry Andric bool wrotePC = false; 2580b57cec5SDimitry Andric bool finish = false; 2590b57cec5SDimitry Andric while (offset < len && !finish) { 2600b57cec5SDimitry Andric uint8_t byte = getByte(data, offset++); 2610b57cec5SDimitry Andric if ((byte & 0x80) == 0) { 2620b57cec5SDimitry Andric uint32_t sp; 2630b57cec5SDimitry Andric _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp); 2640b57cec5SDimitry Andric if (byte & 0x40) 2650b57cec5SDimitry Andric sp -= (((uint32_t)byte & 0x3f) << 2) + 4; 2660b57cec5SDimitry Andric else 2670b57cec5SDimitry Andric sp += ((uint32_t)byte << 2) + 4; 2680b57cec5SDimitry Andric _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp); 2690b57cec5SDimitry Andric } else { 2700b57cec5SDimitry Andric switch (byte & 0xf0) { 2710b57cec5SDimitry Andric case 0x80: { 2720b57cec5SDimitry Andric if (offset >= len) 2730b57cec5SDimitry Andric return _URC_FAILURE; 2740b57cec5SDimitry Andric uint32_t registers = 2750b57cec5SDimitry Andric (((uint32_t)byte & 0x0f) << 12) | 2760b57cec5SDimitry Andric (((uint32_t)getByte(data, offset++)) << 4); 2770b57cec5SDimitry Andric if (!registers) 2780b57cec5SDimitry Andric return _URC_FAILURE; 2790b57cec5SDimitry Andric if (registers & (1 << 15)) 2800b57cec5SDimitry Andric wrotePC = true; 2810b57cec5SDimitry Andric _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32); 2820b57cec5SDimitry Andric break; 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric case 0x90: { 2850b57cec5SDimitry Andric uint8_t reg = byte & 0x0f; 2860b57cec5SDimitry Andric if (reg == 13 || reg == 15) 2870b57cec5SDimitry Andric return _URC_FAILURE; 2880b57cec5SDimitry Andric uint32_t sp; 2890b57cec5SDimitry Andric _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_R0 + reg, 2900b57cec5SDimitry Andric _UVRSD_UINT32, &sp); 2910b57cec5SDimitry Andric _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, 2920b57cec5SDimitry Andric &sp); 2930b57cec5SDimitry Andric break; 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric case 0xa0: { 2960b57cec5SDimitry Andric uint32_t registers = RegisterMask(4, byte & 0x07); 2970b57cec5SDimitry Andric if (byte & 0x08) 2980b57cec5SDimitry Andric registers |= 1 << 14; 2990b57cec5SDimitry Andric _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32); 3000b57cec5SDimitry Andric break; 3010b57cec5SDimitry Andric } 3020b57cec5SDimitry Andric case 0xb0: { 3030b57cec5SDimitry Andric switch (byte) { 3040b57cec5SDimitry Andric case 0xb0: 3050b57cec5SDimitry Andric finish = true; 3060b57cec5SDimitry Andric break; 3070b57cec5SDimitry Andric case 0xb1: { 3080b57cec5SDimitry Andric if (offset >= len) 3090b57cec5SDimitry Andric return _URC_FAILURE; 3100b57cec5SDimitry Andric uint8_t registers = getByte(data, offset++); 3110b57cec5SDimitry Andric if (registers & 0xf0 || !registers) 3120b57cec5SDimitry Andric return _URC_FAILURE; 3130b57cec5SDimitry Andric _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32); 3140b57cec5SDimitry Andric break; 3150b57cec5SDimitry Andric } 3160b57cec5SDimitry Andric case 0xb2: { 3170b57cec5SDimitry Andric uint32_t addend = 0; 3180b57cec5SDimitry Andric uint32_t shift = 0; 3190b57cec5SDimitry Andric // This decodes a uleb128 value. 3200b57cec5SDimitry Andric while (true) { 3210b57cec5SDimitry Andric if (offset >= len) 3220b57cec5SDimitry Andric return _URC_FAILURE; 3230b57cec5SDimitry Andric uint32_t v = getByte(data, offset++); 3240b57cec5SDimitry Andric addend |= (v & 0x7f) << shift; 3250b57cec5SDimitry Andric if ((v & 0x80) == 0) 3260b57cec5SDimitry Andric break; 3270b57cec5SDimitry Andric shift += 7; 3280b57cec5SDimitry Andric } 3290b57cec5SDimitry Andric uint32_t sp; 3300b57cec5SDimitry Andric _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, 3310b57cec5SDimitry Andric &sp); 3320b57cec5SDimitry Andric sp += 0x204 + (addend << 2); 3330b57cec5SDimitry Andric _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, 3340b57cec5SDimitry Andric &sp); 3350b57cec5SDimitry Andric break; 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric case 0xb3: { 3380b57cec5SDimitry Andric uint8_t v = getByte(data, offset++); 3390b57cec5SDimitry Andric _Unwind_VRS_Pop(context, _UVRSC_VFP, 3400b57cec5SDimitry Andric RegisterRange(static_cast<uint8_t>(v >> 4), 3410b57cec5SDimitry Andric v & 0x0f), _UVRSD_VFPX); 3420b57cec5SDimitry Andric break; 3430b57cec5SDimitry Andric } 3440b57cec5SDimitry Andric case 0xb4: 3450b57cec5SDimitry Andric case 0xb5: 3460b57cec5SDimitry Andric case 0xb6: 3470b57cec5SDimitry Andric case 0xb7: 3480b57cec5SDimitry Andric return _URC_FAILURE; 3490b57cec5SDimitry Andric default: 3500b57cec5SDimitry Andric _Unwind_VRS_Pop(context, _UVRSC_VFP, 3510b57cec5SDimitry Andric RegisterRange(8, byte & 0x07), _UVRSD_VFPX); 3520b57cec5SDimitry Andric break; 3530b57cec5SDimitry Andric } 3540b57cec5SDimitry Andric break; 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric case 0xc0: { 3570b57cec5SDimitry Andric switch (byte) { 3580b57cec5SDimitry Andric #if defined(__ARM_WMMX) 3590b57cec5SDimitry Andric case 0xc0: 3600b57cec5SDimitry Andric case 0xc1: 3610b57cec5SDimitry Andric case 0xc2: 3620b57cec5SDimitry Andric case 0xc3: 3630b57cec5SDimitry Andric case 0xc4: 3640b57cec5SDimitry Andric case 0xc5: 3650b57cec5SDimitry Andric _Unwind_VRS_Pop(context, _UVRSC_WMMXD, 3660b57cec5SDimitry Andric RegisterRange(10, byte & 0x7), _UVRSD_DOUBLE); 3670b57cec5SDimitry Andric break; 3680b57cec5SDimitry Andric case 0xc6: { 3690b57cec5SDimitry Andric uint8_t v = getByte(data, offset++); 3700b57cec5SDimitry Andric uint8_t start = static_cast<uint8_t>(v >> 4); 3710b57cec5SDimitry Andric uint8_t count_minus_one = v & 0xf; 3720b57cec5SDimitry Andric if (start + count_minus_one >= 16) 3730b57cec5SDimitry Andric return _URC_FAILURE; 3740b57cec5SDimitry Andric _Unwind_VRS_Pop(context, _UVRSC_WMMXD, 3750b57cec5SDimitry Andric RegisterRange(start, count_minus_one), 3760b57cec5SDimitry Andric _UVRSD_DOUBLE); 3770b57cec5SDimitry Andric break; 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric case 0xc7: { 3800b57cec5SDimitry Andric uint8_t v = getByte(data, offset++); 3810b57cec5SDimitry Andric if (!v || v & 0xf0) 3820b57cec5SDimitry Andric return _URC_FAILURE; 3830b57cec5SDimitry Andric _Unwind_VRS_Pop(context, _UVRSC_WMMXC, v, _UVRSD_DOUBLE); 3840b57cec5SDimitry Andric break; 3850b57cec5SDimitry Andric } 3860b57cec5SDimitry Andric #endif 3870b57cec5SDimitry Andric case 0xc8: 3880b57cec5SDimitry Andric case 0xc9: { 3890b57cec5SDimitry Andric uint8_t v = getByte(data, offset++); 3900b57cec5SDimitry Andric uint8_t start = 3910b57cec5SDimitry Andric static_cast<uint8_t>(((byte == 0xc8) ? 16 : 0) + (v >> 4)); 3920b57cec5SDimitry Andric uint8_t count_minus_one = v & 0xf; 3930b57cec5SDimitry Andric if (start + count_minus_one >= 32) 3940b57cec5SDimitry Andric return _URC_FAILURE; 3950b57cec5SDimitry Andric _Unwind_VRS_Pop(context, _UVRSC_VFP, 3960b57cec5SDimitry Andric RegisterRange(start, count_minus_one), 3970b57cec5SDimitry Andric _UVRSD_DOUBLE); 3980b57cec5SDimitry Andric break; 3990b57cec5SDimitry Andric } 4000b57cec5SDimitry Andric default: 4010b57cec5SDimitry Andric return _URC_FAILURE; 4020b57cec5SDimitry Andric } 4030b57cec5SDimitry Andric break; 4040b57cec5SDimitry Andric } 4050b57cec5SDimitry Andric case 0xd0: { 4060b57cec5SDimitry Andric if (byte & 0x08) 4070b57cec5SDimitry Andric return _URC_FAILURE; 4080b57cec5SDimitry Andric _Unwind_VRS_Pop(context, _UVRSC_VFP, RegisterRange(8, byte & 0x7), 4090b57cec5SDimitry Andric _UVRSD_DOUBLE); 4100b57cec5SDimitry Andric break; 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric default: 4130b57cec5SDimitry Andric return _URC_FAILURE; 4140b57cec5SDimitry Andric } 4150b57cec5SDimitry Andric } 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric if (!wrotePC) { 4180b57cec5SDimitry Andric uint32_t lr; 4190b57cec5SDimitry Andric _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_LR, _UVRSD_UINT32, &lr); 4200b57cec5SDimitry Andric _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_IP, _UVRSD_UINT32, &lr); 4210b57cec5SDimitry Andric } 4220b57cec5SDimitry Andric return _URC_CONTINUE_UNWIND; 4230b57cec5SDimitry Andric } 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code 4260b57cec5SDimitry Andric __aeabi_unwind_cpp_pr0(_Unwind_State state, _Unwind_Control_Block *ucbp, 4270b57cec5SDimitry Andric _Unwind_Context *context) { 4280b57cec5SDimitry Andric return unwindOneFrame(state, ucbp, context); 4290b57cec5SDimitry Andric } 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code 4320b57cec5SDimitry Andric __aeabi_unwind_cpp_pr1(_Unwind_State state, _Unwind_Control_Block *ucbp, 4330b57cec5SDimitry Andric _Unwind_Context *context) { 4340b57cec5SDimitry Andric return unwindOneFrame(state, ucbp, context); 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code 4380b57cec5SDimitry Andric __aeabi_unwind_cpp_pr2(_Unwind_State state, _Unwind_Control_Block *ucbp, 4390b57cec5SDimitry Andric _Unwind_Context *context) { 4400b57cec5SDimitry Andric return unwindOneFrame(state, ucbp, context); 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric static _Unwind_Reason_Code 4440b57cec5SDimitry Andric unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) { 4450b57cec5SDimitry Andric // EHABI #7.3 discusses preserving the VRS in a "temporary VRS" during 4460b57cec5SDimitry Andric // phase 1 and then restoring it to the "primary VRS" for phase 2. The 4470b57cec5SDimitry Andric // effect is phase 2 doesn't see any of the VRS manipulations from phase 1. 4480b57cec5SDimitry Andric // In this implementation, the phases don't share the VRS backing store. 4490b57cec5SDimitry Andric // Instead, they are passed the original |uc| and they create a new VRS 4500b57cec5SDimitry Andric // from scratch thus achieving the same effect. 4510b57cec5SDimitry Andric __unw_init_local(cursor, uc); 4520b57cec5SDimitry Andric 4530b57cec5SDimitry Andric // Walk each frame looking for a place to stop. 4540b57cec5SDimitry Andric for (bool handlerNotFound = true; handlerNotFound;) { 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric // See if frame has code to run (has personality routine). 4570b57cec5SDimitry Andric unw_proc_info_t frameInfo; 4580b57cec5SDimitry Andric if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) { 4590b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 4600b57cec5SDimitry Andric "unwind_phase1(ex_ojb=%p): __unw_get_proc_info " 4610b57cec5SDimitry Andric "failed => _URC_FATAL_PHASE1_ERROR", 4620b57cec5SDimitry Andric static_cast<void *>(exception_object)); 4630b57cec5SDimitry Andric return _URC_FATAL_PHASE1_ERROR; 4640b57cec5SDimitry Andric } 4650b57cec5SDimitry Andric 466*349cc55cSDimitry Andric #ifndef NDEBUG 4670b57cec5SDimitry Andric // When tracing, print state information. 4680b57cec5SDimitry Andric if (_LIBUNWIND_TRACING_UNWINDING) { 4690b57cec5SDimitry Andric char functionBuf[512]; 4700b57cec5SDimitry Andric const char *functionName = functionBuf; 4710b57cec5SDimitry Andric unw_word_t offset; 4720b57cec5SDimitry Andric if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf), 4730b57cec5SDimitry Andric &offset) != UNW_ESUCCESS) || 4740b57cec5SDimitry Andric (frameInfo.start_ip + offset > frameInfo.end_ip)) 4750b57cec5SDimitry Andric functionName = ".anonymous."; 4760b57cec5SDimitry Andric unw_word_t pc; 4770b57cec5SDimitry Andric __unw_get_reg(cursor, UNW_REG_IP, &pc); 4780b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 4790b57cec5SDimitry Andric "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR ", func=%s, " 4800b57cec5SDimitry Andric "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR, 4810b57cec5SDimitry Andric static_cast<void *>(exception_object), pc, 4820b57cec5SDimitry Andric frameInfo.start_ip, functionName, 4830b57cec5SDimitry Andric frameInfo.lsda, frameInfo.handler); 4840b57cec5SDimitry Andric } 485*349cc55cSDimitry Andric #endif 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric // If there is a personality routine, ask it if it will want to stop at 4880b57cec5SDimitry Andric // this frame. 4890b57cec5SDimitry Andric if (frameInfo.handler != 0) { 4905ffd83dbSDimitry Andric _Unwind_Personality_Fn p = 4915ffd83dbSDimitry Andric (_Unwind_Personality_Fn)(long)(frameInfo.handler); 4920b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 4930b57cec5SDimitry Andric "unwind_phase1(ex_ojb=%p): calling personality function %p", 4940b57cec5SDimitry Andric static_cast<void *>(exception_object), 4950b57cec5SDimitry Andric reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(p))); 4960b57cec5SDimitry Andric struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor); 4970b57cec5SDimitry Andric exception_object->pr_cache.fnstart = frameInfo.start_ip; 4980b57cec5SDimitry Andric exception_object->pr_cache.ehtp = 4990b57cec5SDimitry Andric (_Unwind_EHT_Header *)frameInfo.unwind_info; 5000b57cec5SDimitry Andric exception_object->pr_cache.additional = frameInfo.flags; 5010b57cec5SDimitry Andric _Unwind_Reason_Code personalityResult = 5020b57cec5SDimitry Andric (*p)(_US_VIRTUAL_UNWIND_FRAME, exception_object, context); 5030b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 5040b57cec5SDimitry Andric "unwind_phase1(ex_ojb=%p): personality result %d start_ip %x ehtp %p " 5050b57cec5SDimitry Andric "additional %x", 5060b57cec5SDimitry Andric static_cast<void *>(exception_object), personalityResult, 5070b57cec5SDimitry Andric exception_object->pr_cache.fnstart, 5080b57cec5SDimitry Andric static_cast<void *>(exception_object->pr_cache.ehtp), 5090b57cec5SDimitry Andric exception_object->pr_cache.additional); 5100b57cec5SDimitry Andric switch (personalityResult) { 5110b57cec5SDimitry Andric case _URC_HANDLER_FOUND: 5120b57cec5SDimitry Andric // found a catch clause or locals that need destructing in this frame 5130b57cec5SDimitry Andric // stop search and remember stack pointer at the frame 5140b57cec5SDimitry Andric handlerNotFound = false; 5150b57cec5SDimitry Andric // p should have initialized barrier_cache. EHABI #7.3.5 5160b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 5170b57cec5SDimitry Andric "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND", 5180b57cec5SDimitry Andric static_cast<void *>(exception_object)); 5190b57cec5SDimitry Andric return _URC_NO_REASON; 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric case _URC_CONTINUE_UNWIND: 5220b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 5230b57cec5SDimitry Andric "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND", 5240b57cec5SDimitry Andric static_cast<void *>(exception_object)); 5250b57cec5SDimitry Andric // continue unwinding 5260b57cec5SDimitry Andric break; 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric // EHABI #7.3.3 5290b57cec5SDimitry Andric case _URC_FAILURE: 5300b57cec5SDimitry Andric return _URC_FAILURE; 5310b57cec5SDimitry Andric 5320b57cec5SDimitry Andric default: 5330b57cec5SDimitry Andric // something went wrong 5340b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 5350b57cec5SDimitry Andric "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR", 5360b57cec5SDimitry Andric static_cast<void *>(exception_object)); 5370b57cec5SDimitry Andric return _URC_FATAL_PHASE1_ERROR; 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric } 5400b57cec5SDimitry Andric } 5410b57cec5SDimitry Andric return _URC_NO_REASON; 5420b57cec5SDimitry Andric } 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric static _Unwind_Reason_Code unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, 5450b57cec5SDimitry Andric _Unwind_Exception *exception_object, 5460b57cec5SDimitry Andric bool resume) { 5470b57cec5SDimitry Andric // See comment at the start of unwind_phase1 regarding VRS integrity. 5480b57cec5SDimitry Andric __unw_init_local(cursor, uc); 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)", 5510b57cec5SDimitry Andric static_cast<void *>(exception_object)); 5520b57cec5SDimitry Andric int frame_count = 0; 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric // Walk each frame until we reach where search phase said to stop. 5550b57cec5SDimitry Andric while (true) { 5560b57cec5SDimitry Andric // Ask libunwind to get next frame (skip over first which is 5570b57cec5SDimitry Andric // _Unwind_RaiseException or _Unwind_Resume). 5580b57cec5SDimitry Andric // 5590b57cec5SDimitry Andric // Resume only ever makes sense for 1 frame. 5600b57cec5SDimitry Andric _Unwind_State state = 5610b57cec5SDimitry Andric resume ? _US_UNWIND_FRAME_RESUME : _US_UNWIND_FRAME_STARTING; 5620b57cec5SDimitry Andric if (resume && frame_count == 1) { 5630b57cec5SDimitry Andric // On a resume, first unwind the _Unwind_Resume() frame. The next frame 5640b57cec5SDimitry Andric // is now the landing pad for the cleanup from a previous execution of 5650b57cec5SDimitry Andric // phase2. To continue unwindingly correctly, replace VRS[15] with the 5660b57cec5SDimitry Andric // IP of the frame that the previous run of phase2 installed the context 5670b57cec5SDimitry Andric // for. After this, continue unwinding as if normal. 5680b57cec5SDimitry Andric // 5690b57cec5SDimitry Andric // See #7.4.6 for details. 5700b57cec5SDimitry Andric __unw_set_reg(cursor, UNW_REG_IP, 5710b57cec5SDimitry Andric exception_object->unwinder_cache.reserved2); 5720b57cec5SDimitry Andric resume = false; 5730b57cec5SDimitry Andric } 5740b57cec5SDimitry Andric 5750b57cec5SDimitry Andric // Get info about this frame. 5760b57cec5SDimitry Andric unw_word_t sp; 5770b57cec5SDimitry Andric unw_proc_info_t frameInfo; 5780b57cec5SDimitry Andric __unw_get_reg(cursor, UNW_REG_SP, &sp); 5790b57cec5SDimitry Andric if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) { 5800b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 5810b57cec5SDimitry Andric "unwind_phase2(ex_ojb=%p): __unw_get_proc_info " 5820b57cec5SDimitry Andric "failed => _URC_FATAL_PHASE2_ERROR", 5830b57cec5SDimitry Andric static_cast<void *>(exception_object)); 5840b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 5850b57cec5SDimitry Andric } 5860b57cec5SDimitry Andric 587*349cc55cSDimitry Andric #ifndef NDEBUG 5880b57cec5SDimitry Andric // When tracing, print state information. 5890b57cec5SDimitry Andric if (_LIBUNWIND_TRACING_UNWINDING) { 5900b57cec5SDimitry Andric char functionBuf[512]; 5910b57cec5SDimitry Andric const char *functionName = functionBuf; 5920b57cec5SDimitry Andric unw_word_t offset; 5930b57cec5SDimitry Andric if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf), 5940b57cec5SDimitry Andric &offset) != UNW_ESUCCESS) || 5950b57cec5SDimitry Andric (frameInfo.start_ip + offset > frameInfo.end_ip)) 5960b57cec5SDimitry Andric functionName = ".anonymous."; 5970b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 5980b57cec5SDimitry Andric "unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR ", func=%s, sp=0x%" PRIxPTR ", " 5990b57cec5SDimitry Andric "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "", 6000b57cec5SDimitry Andric static_cast<void *>(exception_object), frameInfo.start_ip, 6010b57cec5SDimitry Andric functionName, sp, frameInfo.lsda, 6020b57cec5SDimitry Andric frameInfo.handler); 6030b57cec5SDimitry Andric } 604*349cc55cSDimitry Andric #endif 6050b57cec5SDimitry Andric 6060b57cec5SDimitry Andric // If there is a personality routine, tell it we are unwinding. 6070b57cec5SDimitry Andric if (frameInfo.handler != 0) { 6085ffd83dbSDimitry Andric _Unwind_Personality_Fn p = 609*349cc55cSDimitry Andric (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler); 6100b57cec5SDimitry Andric struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor); 6110b57cec5SDimitry Andric // EHABI #7.2 6120b57cec5SDimitry Andric exception_object->pr_cache.fnstart = frameInfo.start_ip; 6130b57cec5SDimitry Andric exception_object->pr_cache.ehtp = 6140b57cec5SDimitry Andric (_Unwind_EHT_Header *)frameInfo.unwind_info; 6150b57cec5SDimitry Andric exception_object->pr_cache.additional = frameInfo.flags; 6160b57cec5SDimitry Andric _Unwind_Reason_Code personalityResult = 6170b57cec5SDimitry Andric (*p)(state, exception_object, context); 6180b57cec5SDimitry Andric switch (personalityResult) { 6190b57cec5SDimitry Andric case _URC_CONTINUE_UNWIND: 6200b57cec5SDimitry Andric // Continue unwinding 6210b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 6220b57cec5SDimitry Andric "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND", 6230b57cec5SDimitry Andric static_cast<void *>(exception_object)); 6240b57cec5SDimitry Andric // EHABI #7.2 6250b57cec5SDimitry Andric if (sp == exception_object->barrier_cache.sp) { 6260b57cec5SDimitry Andric // Phase 1 said we would stop at this frame, but we did not... 6270b57cec5SDimitry Andric _LIBUNWIND_ABORT("during phase1 personality function said it would " 6280b57cec5SDimitry Andric "stop here, but now in phase2 it did not stop here"); 6290b57cec5SDimitry Andric } 6300b57cec5SDimitry Andric break; 6310b57cec5SDimitry Andric case _URC_INSTALL_CONTEXT: 6320b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 6330b57cec5SDimitry Andric "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT", 6340b57cec5SDimitry Andric static_cast<void *>(exception_object)); 6350b57cec5SDimitry Andric // Personality routine says to transfer control to landing pad. 6360b57cec5SDimitry Andric // We may get control back if landing pad calls _Unwind_Resume(). 6370b57cec5SDimitry Andric if (_LIBUNWIND_TRACING_UNWINDING) { 6380b57cec5SDimitry Andric unw_word_t pc; 6390b57cec5SDimitry Andric __unw_get_reg(cursor, UNW_REG_IP, &pc); 6400b57cec5SDimitry Andric __unw_get_reg(cursor, UNW_REG_SP, &sp); 6410b57cec5SDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering " 6420b57cec5SDimitry Andric "user code with ip=0x%" PRIxPTR ", sp=0x%" PRIxPTR, 6430b57cec5SDimitry Andric static_cast<void *>(exception_object), 6440b57cec5SDimitry Andric pc, sp); 6450b57cec5SDimitry Andric } 6460b57cec5SDimitry Andric 6470b57cec5SDimitry Andric { 6480b57cec5SDimitry Andric // EHABI #7.4.1 says we need to preserve pc for when _Unwind_Resume 6490b57cec5SDimitry Andric // is called back, to find this same frame. 6500b57cec5SDimitry Andric unw_word_t pc; 6510b57cec5SDimitry Andric __unw_get_reg(cursor, UNW_REG_IP, &pc); 6520b57cec5SDimitry Andric exception_object->unwinder_cache.reserved2 = (uint32_t)pc; 6530b57cec5SDimitry Andric } 6540b57cec5SDimitry Andric __unw_resume(cursor); 6550b57cec5SDimitry Andric // __unw_resume() only returns if there was an error. 6560b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric // # EHABI #7.4.3 6590b57cec5SDimitry Andric case _URC_FAILURE: 6600b57cec5SDimitry Andric abort(); 6610b57cec5SDimitry Andric 6620b57cec5SDimitry Andric default: 6630b57cec5SDimitry Andric // Personality routine returned an unknown result code. 6640b57cec5SDimitry Andric _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d", 6650b57cec5SDimitry Andric personalityResult); 6660b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 6670b57cec5SDimitry Andric } 6680b57cec5SDimitry Andric } 6690b57cec5SDimitry Andric frame_count++; 6700b57cec5SDimitry Andric } 6710b57cec5SDimitry Andric 6720b57cec5SDimitry Andric // Clean up phase did not resume at the frame that the search phase 6730b57cec5SDimitry Andric // said it would... 6740b57cec5SDimitry Andric return _URC_FATAL_PHASE2_ERROR; 6750b57cec5SDimitry Andric } 6760b57cec5SDimitry Andric 677*349cc55cSDimitry Andric static _Unwind_Reason_Code 678*349cc55cSDimitry Andric unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor, 679*349cc55cSDimitry Andric _Unwind_Exception *exception_object, _Unwind_Stop_Fn stop, 680*349cc55cSDimitry Andric void *stop_parameter) { 681*349cc55cSDimitry Andric // See comment at the start of unwind_phase1 regarding VRS integrity. 682*349cc55cSDimitry Andric __unw_init_local(cursor, uc); 683*349cc55cSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_force(ex_ojb=%p)", 684*349cc55cSDimitry Andric static_cast<void *>(exception_object)); 685*349cc55cSDimitry Andric // Walk each frame until we reach where search phase said to stop 686*349cc55cSDimitry Andric while (true) { 687*349cc55cSDimitry Andric // Update info about this frame. 688*349cc55cSDimitry Andric unw_proc_info_t frameInfo; 689*349cc55cSDimitry Andric if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) { 690*349cc55cSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step " 691*349cc55cSDimitry Andric "failed => _URC_END_OF_STACK", 692*349cc55cSDimitry Andric (void *)exception_object); 693*349cc55cSDimitry Andric return _URC_FATAL_PHASE2_ERROR; 694*349cc55cSDimitry Andric } 695*349cc55cSDimitry Andric 696*349cc55cSDimitry Andric #ifndef NDEBUG 697*349cc55cSDimitry Andric // When tracing, print state information. 698*349cc55cSDimitry Andric if (_LIBUNWIND_TRACING_UNWINDING) { 699*349cc55cSDimitry Andric char functionBuf[512]; 700*349cc55cSDimitry Andric const char *functionName = functionBuf; 701*349cc55cSDimitry Andric unw_word_t offset; 702*349cc55cSDimitry Andric if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf), 703*349cc55cSDimitry Andric &offset) != UNW_ESUCCESS) || 704*349cc55cSDimitry Andric (frameInfo.start_ip + offset > frameInfo.end_ip)) 705*349cc55cSDimitry Andric functionName = ".anonymous."; 706*349cc55cSDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 707*349cc55cSDimitry Andric "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR 708*349cc55cSDimitry Andric ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR, 709*349cc55cSDimitry Andric (void *)exception_object, frameInfo.start_ip, functionName, 710*349cc55cSDimitry Andric frameInfo.lsda, frameInfo.handler); 711*349cc55cSDimitry Andric } 712*349cc55cSDimitry Andric #endif 713*349cc55cSDimitry Andric 714*349cc55cSDimitry Andric // Call stop function at each frame. 715*349cc55cSDimitry Andric _Unwind_Action action = 716*349cc55cSDimitry Andric (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE); 717*349cc55cSDimitry Andric _Unwind_Reason_Code stopResult = 718*349cc55cSDimitry Andric (*stop)(1, action, exception_object->exception_class, exception_object, 719*349cc55cSDimitry Andric (_Unwind_Context *)(cursor), stop_parameter); 720*349cc55cSDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 721*349cc55cSDimitry Andric "unwind_phase2_forced(ex_ojb=%p): stop function returned %d", 722*349cc55cSDimitry Andric (void *)exception_object, stopResult); 723*349cc55cSDimitry Andric if (stopResult != _URC_NO_REASON) { 724*349cc55cSDimitry Andric _LIBUNWIND_TRACE_UNWINDING( 725*349cc55cSDimitry Andric "unwind_phase2_forced(ex_ojb=%p): stopped by stop function", 726*349cc55cSDimitry Andric (void *)exception_object); 727*349cc55cSDimitry Andric return _URC_FATAL_PHASE2_ERROR; 728*349cc55cSDimitry Andric } 729*349cc55cSDimitry Andric 730*349cc55cSDimitry Andric // If there is a personality routine, tell it we are unwinding. 731*349cc55cSDimitry Andric if (frameInfo.handler != 0) { 732*349cc55cSDimitry Andric _Unwind_Personality_Fn p = 733*349cc55cSDimitry Andric (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler); 734*349cc55cSDimitry Andric struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor); 735*349cc55cSDimitry Andric // EHABI #7.2 736*349cc55cSDimitry Andric exception_object->pr_cache.fnstart = frameInfo.start_ip; 737*349cc55cSDimitry Andric exception_object->pr_cache.ehtp = 738*349cc55cSDimitry Andric (_Unwind_EHT_Header *)frameInfo.unwind_info; 739*349cc55cSDimitry Andric exception_object->pr_cache.additional = frameInfo.flags; 740*349cc55cSDimitry Andric _Unwind_Reason_Code personalityResult = 741*349cc55cSDimitry Andric (*p)(_US_FORCE_UNWIND | _US_UNWIND_FRAME_STARTING, exception_object, 742*349cc55cSDimitry Andric context); 743*349cc55cSDimitry Andric switch (personalityResult) { 744*349cc55cSDimitry Andric case _URC_CONTINUE_UNWIND: 745*349cc55cSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " 746*349cc55cSDimitry Andric "personality returned " 747*349cc55cSDimitry Andric "_URC_CONTINUE_UNWIND", 748*349cc55cSDimitry Andric (void *)exception_object); 749*349cc55cSDimitry Andric // Destructors called, continue unwinding 750*349cc55cSDimitry Andric break; 751*349cc55cSDimitry Andric case _URC_INSTALL_CONTEXT: 752*349cc55cSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " 753*349cc55cSDimitry Andric "personality returned " 754*349cc55cSDimitry Andric "_URC_INSTALL_CONTEXT", 755*349cc55cSDimitry Andric (void *)exception_object); 756*349cc55cSDimitry Andric // We may get control back if landing pad calls _Unwind_Resume(). 757*349cc55cSDimitry Andric __unw_resume(cursor); 758*349cc55cSDimitry Andric break; 759*349cc55cSDimitry Andric default: 760*349cc55cSDimitry Andric // Personality routine returned an unknown result code. 761*349cc55cSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): " 762*349cc55cSDimitry Andric "personality returned %d, " 763*349cc55cSDimitry Andric "_URC_FATAL_PHASE2_ERROR", 764*349cc55cSDimitry Andric (void *)exception_object, personalityResult); 765*349cc55cSDimitry Andric return _URC_FATAL_PHASE2_ERROR; 766*349cc55cSDimitry Andric } 767*349cc55cSDimitry Andric } 768*349cc55cSDimitry Andric } 769*349cc55cSDimitry Andric 770*349cc55cSDimitry Andric // Call stop function one last time and tell it we've reached the end 771*349cc55cSDimitry Andric // of the stack. 772*349cc55cSDimitry Andric _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop " 773*349cc55cSDimitry Andric "function with _UA_END_OF_STACK", 774*349cc55cSDimitry Andric (void *)exception_object); 775*349cc55cSDimitry Andric _Unwind_Action lastAction = 776*349cc55cSDimitry Andric (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK); 777*349cc55cSDimitry Andric (*stop)(1, lastAction, exception_object->exception_class, exception_object, 778*349cc55cSDimitry Andric (struct _Unwind_Context *)(cursor), stop_parameter); 779*349cc55cSDimitry Andric 780*349cc55cSDimitry Andric // Clean up phase did not resume at the frame that the search phase said it 781*349cc55cSDimitry Andric // would. 782*349cc55cSDimitry Andric return _URC_FATAL_PHASE2_ERROR; 783*349cc55cSDimitry Andric } 784*349cc55cSDimitry Andric 7850b57cec5SDimitry Andric /// Called by __cxa_throw. Only returns if there is a fatal error. 7860b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_Reason_Code 7870b57cec5SDimitry Andric _Unwind_RaiseException(_Unwind_Exception *exception_object) { 7880b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)", 7890b57cec5SDimitry Andric static_cast<void *>(exception_object)); 7900b57cec5SDimitry Andric unw_context_t uc; 7910b57cec5SDimitry Andric unw_cursor_t cursor; 7920b57cec5SDimitry Andric __unw_getcontext(&uc); 7930b57cec5SDimitry Andric 7940b57cec5SDimitry Andric // This field for is for compatibility with GCC to say this isn't a forced 7950b57cec5SDimitry Andric // unwind. EHABI #7.2 7960b57cec5SDimitry Andric exception_object->unwinder_cache.reserved1 = 0; 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric // phase 1: the search phase 7990b57cec5SDimitry Andric _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object); 8000b57cec5SDimitry Andric if (phase1 != _URC_NO_REASON) 8010b57cec5SDimitry Andric return phase1; 8020b57cec5SDimitry Andric 8030b57cec5SDimitry Andric // phase 2: the clean up phase 8040b57cec5SDimitry Andric return unwind_phase2(&uc, &cursor, exception_object, false); 8050b57cec5SDimitry Andric } 8060b57cec5SDimitry Andric 8070b57cec5SDimitry Andric _LIBUNWIND_EXPORT void _Unwind_Complete(_Unwind_Exception* exception_object) { 8080b57cec5SDimitry Andric // This is to be called when exception handling completes to give us a chance 8090b57cec5SDimitry Andric // to perform any housekeeping. EHABI #7.2. But we have nothing to do here. 8100b57cec5SDimitry Andric (void)exception_object; 8110b57cec5SDimitry Andric } 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric /// When _Unwind_RaiseException() is in phase2, it hands control 8140b57cec5SDimitry Andric /// to the personality function at each frame. The personality 8150b57cec5SDimitry Andric /// may force a jump to a landing pad in that function, the landing 8160b57cec5SDimitry Andric /// pad code may then call _Unwind_Resume() to continue with the 8170b57cec5SDimitry Andric /// unwinding. Note: the call to _Unwind_Resume() is from compiler 8180b57cec5SDimitry Andric /// geneated user code. All other _Unwind_* routines are called 8190b57cec5SDimitry Andric /// by the C++ runtime __cxa_* routines. 8200b57cec5SDimitry Andric /// 8210b57cec5SDimitry Andric /// Note: re-throwing an exception (as opposed to continuing the unwind) 8220b57cec5SDimitry Andric /// is implemented by having the code call __cxa_rethrow() which 8230b57cec5SDimitry Andric /// in turn calls _Unwind_Resume_or_Rethrow(). 8240b57cec5SDimitry Andric _LIBUNWIND_EXPORT void 8250b57cec5SDimitry Andric _Unwind_Resume(_Unwind_Exception *exception_object) { 8260b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)", 8270b57cec5SDimitry Andric static_cast<void *>(exception_object)); 8280b57cec5SDimitry Andric unw_context_t uc; 8290b57cec5SDimitry Andric unw_cursor_t cursor; 8300b57cec5SDimitry Andric __unw_getcontext(&uc); 8310b57cec5SDimitry Andric 832*349cc55cSDimitry Andric if (exception_object->unwinder_cache.reserved1) 833*349cc55cSDimitry Andric unwind_phase2_forced( 834*349cc55cSDimitry Andric &uc, &cursor, exception_object, 835*349cc55cSDimitry Andric (_Unwind_Stop_Fn)exception_object->unwinder_cache.reserved1, 836*349cc55cSDimitry Andric (void *)exception_object->unwinder_cache.reserved3); 837*349cc55cSDimitry Andric else 8380b57cec5SDimitry Andric unwind_phase2(&uc, &cursor, exception_object, true); 8390b57cec5SDimitry Andric 8400b57cec5SDimitry Andric // Clients assume _Unwind_Resume() does not return, so all we can do is abort. 8410b57cec5SDimitry Andric _LIBUNWIND_ABORT("_Unwind_Resume() can't return"); 8420b57cec5SDimitry Andric } 8430b57cec5SDimitry Andric 8440b57cec5SDimitry Andric /// Called by personality handler during phase 2 to get LSDA for current frame. 8450b57cec5SDimitry Andric _LIBUNWIND_EXPORT uintptr_t 8460b57cec5SDimitry Andric _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) { 8470b57cec5SDimitry Andric unw_cursor_t *cursor = (unw_cursor_t *)context; 8480b57cec5SDimitry Andric unw_proc_info_t frameInfo; 8490b57cec5SDimitry Andric uintptr_t result = 0; 8500b57cec5SDimitry Andric if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS) 8510b57cec5SDimitry Andric result = (uintptr_t)frameInfo.lsda; 8520b57cec5SDimitry Andric _LIBUNWIND_TRACE_API( 8530b57cec5SDimitry Andric "_Unwind_GetLanguageSpecificData(context=%p) => 0x%llx", 8540b57cec5SDimitry Andric static_cast<void *>(context), (long long)result); 8550b57cec5SDimitry Andric return result; 8560b57cec5SDimitry Andric } 8570b57cec5SDimitry Andric 8580b57cec5SDimitry Andric static uint64_t ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation, 8590b57cec5SDimitry Andric void* valuep) { 8600b57cec5SDimitry Andric uint64_t value = 0; 8610b57cec5SDimitry Andric switch (representation) { 8620b57cec5SDimitry Andric case _UVRSD_UINT32: 8630b57cec5SDimitry Andric case _UVRSD_FLOAT: 8640b57cec5SDimitry Andric memcpy(&value, valuep, sizeof(uint32_t)); 8650b57cec5SDimitry Andric break; 8660b57cec5SDimitry Andric 8670b57cec5SDimitry Andric case _UVRSD_VFPX: 8680b57cec5SDimitry Andric case _UVRSD_UINT64: 8690b57cec5SDimitry Andric case _UVRSD_DOUBLE: 8700b57cec5SDimitry Andric memcpy(&value, valuep, sizeof(uint64_t)); 8710b57cec5SDimitry Andric break; 8720b57cec5SDimitry Andric } 8730b57cec5SDimitry Andric return value; 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric 8760b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_VRS_Result 8770b57cec5SDimitry Andric _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass, 8780b57cec5SDimitry Andric uint32_t regno, _Unwind_VRS_DataRepresentation representation, 8790b57cec5SDimitry Andric void *valuep) { 8800b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_VRS_Set(context=%p, regclass=%d, reg=%d, " 8810b57cec5SDimitry Andric "rep=%d, value=0x%llX)", 8820b57cec5SDimitry Andric static_cast<void *>(context), regclass, regno, 8830b57cec5SDimitry Andric representation, 8840b57cec5SDimitry Andric ValueAsBitPattern(representation, valuep)); 8850b57cec5SDimitry Andric unw_cursor_t *cursor = (unw_cursor_t *)context; 8860b57cec5SDimitry Andric switch (regclass) { 8870b57cec5SDimitry Andric case _UVRSC_CORE: 8880b57cec5SDimitry Andric if (representation != _UVRSD_UINT32 || regno > 15) 8890b57cec5SDimitry Andric return _UVRSR_FAILED; 8900b57cec5SDimitry Andric return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno), 8910b57cec5SDimitry Andric *(unw_word_t *)valuep) == UNW_ESUCCESS 8920b57cec5SDimitry Andric ? _UVRSR_OK 8930b57cec5SDimitry Andric : _UVRSR_FAILED; 8940b57cec5SDimitry Andric case _UVRSC_VFP: 8950b57cec5SDimitry Andric if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE) 8960b57cec5SDimitry Andric return _UVRSR_FAILED; 8970b57cec5SDimitry Andric if (representation == _UVRSD_VFPX) { 8980b57cec5SDimitry Andric // Can only touch d0-15 with FSTMFDX. 8990b57cec5SDimitry Andric if (regno > 15) 9000b57cec5SDimitry Andric return _UVRSR_FAILED; 9010b57cec5SDimitry Andric __unw_save_vfp_as_X(cursor); 9020b57cec5SDimitry Andric } else { 9030b57cec5SDimitry Andric if (regno > 31) 9040b57cec5SDimitry Andric return _UVRSR_FAILED; 9050b57cec5SDimitry Andric } 9060b57cec5SDimitry Andric return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno), 9070b57cec5SDimitry Andric *(unw_fpreg_t *)valuep) == UNW_ESUCCESS 9080b57cec5SDimitry Andric ? _UVRSR_OK 9090b57cec5SDimitry Andric : _UVRSR_FAILED; 9100b57cec5SDimitry Andric #if defined(__ARM_WMMX) 9110b57cec5SDimitry Andric case _UVRSC_WMMXC: 9120b57cec5SDimitry Andric if (representation != _UVRSD_UINT32 || regno > 3) 9130b57cec5SDimitry Andric return _UVRSR_FAILED; 9140b57cec5SDimitry Andric return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno), 9150b57cec5SDimitry Andric *(unw_word_t *)valuep) == UNW_ESUCCESS 9160b57cec5SDimitry Andric ? _UVRSR_OK 9170b57cec5SDimitry Andric : _UVRSR_FAILED; 9180b57cec5SDimitry Andric case _UVRSC_WMMXD: 9190b57cec5SDimitry Andric if (representation != _UVRSD_DOUBLE || regno > 31) 9200b57cec5SDimitry Andric return _UVRSR_FAILED; 9210b57cec5SDimitry Andric return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno), 9220b57cec5SDimitry Andric *(unw_fpreg_t *)valuep) == UNW_ESUCCESS 9230b57cec5SDimitry Andric ? _UVRSR_OK 9240b57cec5SDimitry Andric : _UVRSR_FAILED; 9250b57cec5SDimitry Andric #else 9260b57cec5SDimitry Andric case _UVRSC_WMMXC: 9270b57cec5SDimitry Andric case _UVRSC_WMMXD: 9280b57cec5SDimitry Andric break; 9290b57cec5SDimitry Andric #endif 9300b57cec5SDimitry Andric } 9310b57cec5SDimitry Andric _LIBUNWIND_ABORT("unsupported register class"); 9320b57cec5SDimitry Andric } 9330b57cec5SDimitry Andric 9340b57cec5SDimitry Andric static _Unwind_VRS_Result 9350b57cec5SDimitry Andric _Unwind_VRS_Get_Internal(_Unwind_Context *context, 9360b57cec5SDimitry Andric _Unwind_VRS_RegClass regclass, uint32_t regno, 9370b57cec5SDimitry Andric _Unwind_VRS_DataRepresentation representation, 9380b57cec5SDimitry Andric void *valuep) { 9390b57cec5SDimitry Andric unw_cursor_t *cursor = (unw_cursor_t *)context; 9400b57cec5SDimitry Andric switch (regclass) { 9410b57cec5SDimitry Andric case _UVRSC_CORE: 9420b57cec5SDimitry Andric if (representation != _UVRSD_UINT32 || regno > 15) 9430b57cec5SDimitry Andric return _UVRSR_FAILED; 9440b57cec5SDimitry Andric return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno), 9450b57cec5SDimitry Andric (unw_word_t *)valuep) == UNW_ESUCCESS 9460b57cec5SDimitry Andric ? _UVRSR_OK 9470b57cec5SDimitry Andric : _UVRSR_FAILED; 9480b57cec5SDimitry Andric case _UVRSC_VFP: 9490b57cec5SDimitry Andric if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE) 9500b57cec5SDimitry Andric return _UVRSR_FAILED; 9510b57cec5SDimitry Andric if (representation == _UVRSD_VFPX) { 9520b57cec5SDimitry Andric // Can only touch d0-15 with FSTMFDX. 9530b57cec5SDimitry Andric if (regno > 15) 9540b57cec5SDimitry Andric return _UVRSR_FAILED; 9550b57cec5SDimitry Andric __unw_save_vfp_as_X(cursor); 9560b57cec5SDimitry Andric } else { 9570b57cec5SDimitry Andric if (regno > 31) 9580b57cec5SDimitry Andric return _UVRSR_FAILED; 9590b57cec5SDimitry Andric } 9600b57cec5SDimitry Andric return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno), 9610b57cec5SDimitry Andric (unw_fpreg_t *)valuep) == UNW_ESUCCESS 9620b57cec5SDimitry Andric ? _UVRSR_OK 9630b57cec5SDimitry Andric : _UVRSR_FAILED; 9640b57cec5SDimitry Andric #if defined(__ARM_WMMX) 9650b57cec5SDimitry Andric case _UVRSC_WMMXC: 9660b57cec5SDimitry Andric if (representation != _UVRSD_UINT32 || regno > 3) 9670b57cec5SDimitry Andric return _UVRSR_FAILED; 9680b57cec5SDimitry Andric return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno), 9690b57cec5SDimitry Andric (unw_word_t *)valuep) == UNW_ESUCCESS 9700b57cec5SDimitry Andric ? _UVRSR_OK 9710b57cec5SDimitry Andric : _UVRSR_FAILED; 9720b57cec5SDimitry Andric case _UVRSC_WMMXD: 9730b57cec5SDimitry Andric if (representation != _UVRSD_DOUBLE || regno > 31) 9740b57cec5SDimitry Andric return _UVRSR_FAILED; 9750b57cec5SDimitry Andric return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno), 9760b57cec5SDimitry Andric (unw_fpreg_t *)valuep) == UNW_ESUCCESS 9770b57cec5SDimitry Andric ? _UVRSR_OK 9780b57cec5SDimitry Andric : _UVRSR_FAILED; 9790b57cec5SDimitry Andric #else 9800b57cec5SDimitry Andric case _UVRSC_WMMXC: 9810b57cec5SDimitry Andric case _UVRSC_WMMXD: 9820b57cec5SDimitry Andric break; 9830b57cec5SDimitry Andric #endif 9840b57cec5SDimitry Andric } 9850b57cec5SDimitry Andric _LIBUNWIND_ABORT("unsupported register class"); 9860b57cec5SDimitry Andric } 9870b57cec5SDimitry Andric 9880b57cec5SDimitry Andric _LIBUNWIND_EXPORT _Unwind_VRS_Result 9890b57cec5SDimitry Andric _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass, 9900b57cec5SDimitry Andric uint32_t regno, _Unwind_VRS_DataRepresentation representation, 9910b57cec5SDimitry Andric void *valuep) { 9920b57cec5SDimitry Andric _Unwind_VRS_Result result = 9930b57cec5SDimitry Andric _Unwind_VRS_Get_Internal(context, regclass, regno, representation, 9940b57cec5SDimitry Andric valuep); 9950b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_VRS_Get(context=%p, regclass=%d, reg=%d, " 9960b57cec5SDimitry Andric "rep=%d, value=0x%llX, result = %d)", 9970b57cec5SDimitry Andric static_cast<void *>(context), regclass, regno, 9980b57cec5SDimitry Andric representation, 9990b57cec5SDimitry Andric ValueAsBitPattern(representation, valuep), result); 10000b57cec5SDimitry Andric return result; 10010b57cec5SDimitry Andric } 10020b57cec5SDimitry Andric 10030b57cec5SDimitry Andric _Unwind_VRS_Result 10040b57cec5SDimitry Andric _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass, 10050b57cec5SDimitry Andric uint32_t discriminator, 10060b57cec5SDimitry Andric _Unwind_VRS_DataRepresentation representation) { 10070b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_VRS_Pop(context=%p, regclass=%d, " 10080b57cec5SDimitry Andric "discriminator=%d, representation=%d)", 10090b57cec5SDimitry Andric static_cast<void *>(context), regclass, discriminator, 10100b57cec5SDimitry Andric representation); 10110b57cec5SDimitry Andric switch (regclass) { 10120b57cec5SDimitry Andric case _UVRSC_WMMXC: 10130b57cec5SDimitry Andric #if !defined(__ARM_WMMX) 10140b57cec5SDimitry Andric break; 10150b57cec5SDimitry Andric #endif 10160b57cec5SDimitry Andric case _UVRSC_CORE: { 10170b57cec5SDimitry Andric if (representation != _UVRSD_UINT32) 10180b57cec5SDimitry Andric return _UVRSR_FAILED; 10190b57cec5SDimitry Andric // When popping SP from the stack, we don't want to override it from the 10200b57cec5SDimitry Andric // computed new stack location. See EHABI #7.5.4 table 3. 10210b57cec5SDimitry Andric bool poppedSP = false; 10220b57cec5SDimitry Andric uint32_t* sp; 10230b57cec5SDimitry Andric if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, 10240b57cec5SDimitry Andric _UVRSD_UINT32, &sp) != _UVRSR_OK) { 10250b57cec5SDimitry Andric return _UVRSR_FAILED; 10260b57cec5SDimitry Andric } 10270b57cec5SDimitry Andric for (uint32_t i = 0; i < 16; ++i) { 10280b57cec5SDimitry Andric if (!(discriminator & static_cast<uint32_t>(1 << i))) 10290b57cec5SDimitry Andric continue; 10300b57cec5SDimitry Andric uint32_t value = *sp++; 10310b57cec5SDimitry Andric if (regclass == _UVRSC_CORE && i == 13) 10320b57cec5SDimitry Andric poppedSP = true; 10330b57cec5SDimitry Andric if (_Unwind_VRS_Set(context, regclass, i, 10340b57cec5SDimitry Andric _UVRSD_UINT32, &value) != _UVRSR_OK) { 10350b57cec5SDimitry Andric return _UVRSR_FAILED; 10360b57cec5SDimitry Andric } 10370b57cec5SDimitry Andric } 10380b57cec5SDimitry Andric if (!poppedSP) { 10390b57cec5SDimitry Andric return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, 10400b57cec5SDimitry Andric _UVRSD_UINT32, &sp); 10410b57cec5SDimitry Andric } 10420b57cec5SDimitry Andric return _UVRSR_OK; 10430b57cec5SDimitry Andric } 10440b57cec5SDimitry Andric case _UVRSC_WMMXD: 10450b57cec5SDimitry Andric #if !defined(__ARM_WMMX) 10460b57cec5SDimitry Andric break; 10470b57cec5SDimitry Andric #endif 10480b57cec5SDimitry Andric case _UVRSC_VFP: { 10490b57cec5SDimitry Andric if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE) 10500b57cec5SDimitry Andric return _UVRSR_FAILED; 10510b57cec5SDimitry Andric uint32_t first = discriminator >> 16; 10520b57cec5SDimitry Andric uint32_t count = discriminator & 0xffff; 10530b57cec5SDimitry Andric uint32_t end = first+count; 10540b57cec5SDimitry Andric uint32_t* sp; 10550b57cec5SDimitry Andric if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, 10560b57cec5SDimitry Andric _UVRSD_UINT32, &sp) != _UVRSR_OK) { 10570b57cec5SDimitry Andric return _UVRSR_FAILED; 10580b57cec5SDimitry Andric } 10590b57cec5SDimitry Andric // For _UVRSD_VFPX, we're assuming the data is stored in FSTMX "standard 10600b57cec5SDimitry Andric // format 1", which is equivalent to FSTMD + a padding word. 10610b57cec5SDimitry Andric for (uint32_t i = first; i < end; ++i) { 10620b57cec5SDimitry Andric // SP is only 32-bit aligned so don't copy 64-bit at a time. 1063c2c6a179SDimitry Andric uint64_t w0 = *sp++; 1064c2c6a179SDimitry Andric uint64_t w1 = *sp++; 10655ffd83dbSDimitry Andric #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 1066c2c6a179SDimitry Andric uint64_t value = (w1 << 32) | w0; 10675ffd83dbSDimitry Andric #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 1068c2c6a179SDimitry Andric uint64_t value = (w0 << 32) | w1; 10695ffd83dbSDimitry Andric #else 10705ffd83dbSDimitry Andric #error "Unable to determine endianess" 1071c2c6a179SDimitry Andric #endif 10720b57cec5SDimitry Andric if (_Unwind_VRS_Set(context, regclass, i, representation, &value) != 10730b57cec5SDimitry Andric _UVRSR_OK) 10740b57cec5SDimitry Andric return _UVRSR_FAILED; 10750b57cec5SDimitry Andric } 10760b57cec5SDimitry Andric if (representation == _UVRSD_VFPX) 10770b57cec5SDimitry Andric ++sp; 10780b57cec5SDimitry Andric return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, 10790b57cec5SDimitry Andric &sp); 10800b57cec5SDimitry Andric } 10810b57cec5SDimitry Andric } 10820b57cec5SDimitry Andric _LIBUNWIND_ABORT("unsupported register class"); 10830b57cec5SDimitry Andric } 10840b57cec5SDimitry Andric 1085*349cc55cSDimitry Andric /// Not used by C++. 1086*349cc55cSDimitry Andric /// Unwinds stack, calling "stop" function at each frame. 1087*349cc55cSDimitry Andric /// Could be used to implement longjmp(). 1088*349cc55cSDimitry Andric _LIBUNWIND_EXPORT _Unwind_Reason_Code 1089*349cc55cSDimitry Andric _Unwind_ForcedUnwind(_Unwind_Exception *exception_object, _Unwind_Stop_Fn stop, 1090*349cc55cSDimitry Andric void *stop_parameter) { 1091*349cc55cSDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)", 1092*349cc55cSDimitry Andric (void *)exception_object, (void *)(uintptr_t)stop); 1093*349cc55cSDimitry Andric unw_context_t uc; 1094*349cc55cSDimitry Andric unw_cursor_t cursor; 1095*349cc55cSDimitry Andric __unw_getcontext(&uc); 1096*349cc55cSDimitry Andric 1097*349cc55cSDimitry Andric // Mark that this is a forced unwind, so _Unwind_Resume() can do 1098*349cc55cSDimitry Andric // the right thing. 1099*349cc55cSDimitry Andric exception_object->unwinder_cache.reserved1 = (uintptr_t)stop; 1100*349cc55cSDimitry Andric exception_object->unwinder_cache.reserved3 = (uintptr_t)stop_parameter; 1101*349cc55cSDimitry Andric 1102*349cc55cSDimitry Andric return unwind_phase2_forced(&uc, &cursor, exception_object, stop, 1103*349cc55cSDimitry Andric stop_parameter); 1104*349cc55cSDimitry Andric } 1105*349cc55cSDimitry Andric 11060b57cec5SDimitry Andric /// Called by personality handler during phase 2 to find the start of the 11070b57cec5SDimitry Andric /// function. 11080b57cec5SDimitry Andric _LIBUNWIND_EXPORT uintptr_t 11090b57cec5SDimitry Andric _Unwind_GetRegionStart(struct _Unwind_Context *context) { 11100b57cec5SDimitry Andric unw_cursor_t *cursor = (unw_cursor_t *)context; 11110b57cec5SDimitry Andric unw_proc_info_t frameInfo; 11120b57cec5SDimitry Andric uintptr_t result = 0; 11130b57cec5SDimitry Andric if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS) 11140b57cec5SDimitry Andric result = (uintptr_t)frameInfo.start_ip; 11150b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%llX", 11160b57cec5SDimitry Andric static_cast<void *>(context), (long long)result); 11170b57cec5SDimitry Andric return result; 11180b57cec5SDimitry Andric } 11190b57cec5SDimitry Andric 11200b57cec5SDimitry Andric 11210b57cec5SDimitry Andric /// Called by personality handler during phase 2 if a foreign exception 11220b57cec5SDimitry Andric // is caught. 11230b57cec5SDimitry Andric _LIBUNWIND_EXPORT void 11240b57cec5SDimitry Andric _Unwind_DeleteException(_Unwind_Exception *exception_object) { 11250b57cec5SDimitry Andric _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)", 11260b57cec5SDimitry Andric static_cast<void *>(exception_object)); 11270b57cec5SDimitry Andric if (exception_object->exception_cleanup != NULL) 11280b57cec5SDimitry Andric (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT, 11290b57cec5SDimitry Andric exception_object); 11300b57cec5SDimitry Andric } 11310b57cec5SDimitry Andric 11320b57cec5SDimitry Andric extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code 11330b57cec5SDimitry Andric __gnu_unwind_frame(_Unwind_Exception *exception_object, 11340b57cec5SDimitry Andric struct _Unwind_Context *context) { 11350b57cec5SDimitry Andric unw_cursor_t *cursor = (unw_cursor_t *)context; 11360b57cec5SDimitry Andric if (__unw_step(cursor) != UNW_STEP_SUCCESS) 11370b57cec5SDimitry Andric return _URC_FAILURE; 11380b57cec5SDimitry Andric return _URC_OK; 11390b57cec5SDimitry Andric } 11400b57cec5SDimitry Andric 11410b57cec5SDimitry Andric #endif // defined(_LIBUNWIND_ARM_EHABI) 1142