1*0faf1914Srobert //===----------------------------------------------------------------------===//
2f6c50668Spatrick //
3f6c50668Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4f6c50668Spatrick // See https://llvm.org/LICENSE.txt for license information.
5f6c50668Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f6c50668Spatrick //
7f6c50668Spatrick //
8f6c50668Spatrick // Implements ARM zero-cost C++ exceptions
9f6c50668Spatrick //
10f6c50668Spatrick //===----------------------------------------------------------------------===//
11f6c50668Spatrick
12f6c50668Spatrick #include "Unwind-EHABI.h"
13f6c50668Spatrick
14f6c50668Spatrick #if defined(_LIBUNWIND_ARM_EHABI)
15f6c50668Spatrick
16f6c50668Spatrick #include <inttypes.h>
17f6c50668Spatrick #include <stdbool.h>
18f6c50668Spatrick #include <stdint.h>
19f6c50668Spatrick #include <stdio.h>
20f6c50668Spatrick #include <stdlib.h>
21f6c50668Spatrick #include <string.h>
22f6c50668Spatrick
23f6c50668Spatrick #include "config.h"
24f6c50668Spatrick #include "libunwind.h"
25f6c50668Spatrick #include "libunwind_ext.h"
26f6c50668Spatrick #include "unwind.h"
27f6c50668Spatrick
28f6c50668Spatrick namespace {
29f6c50668Spatrick
30f6c50668Spatrick // Strange order: take words in order, but inside word, take from most to least
31f6c50668Spatrick // signinficant byte.
getByte(const uint32_t * data,size_t offset)32f6c50668Spatrick uint8_t getByte(const uint32_t* data, size_t offset) {
33f6c50668Spatrick const uint8_t* byteData = reinterpret_cast<const uint8_t*>(data);
34f6c50668Spatrick #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
35f6c50668Spatrick return byteData[(offset & ~(size_t)0x03) + (3 - (offset & (size_t)0x03))];
36f6c50668Spatrick #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
37f6c50668Spatrick return byteData[offset];
38f6c50668Spatrick #else
39f6c50668Spatrick #error "Unable to determine endianess"
40f6c50668Spatrick #endif
41f6c50668Spatrick }
42f6c50668Spatrick
getNextWord(const char * data,uint32_t * out)43f6c50668Spatrick const char* getNextWord(const char* data, uint32_t* out) {
44f6c50668Spatrick *out = *reinterpret_cast<const uint32_t*>(data);
45f6c50668Spatrick return data + 4;
46f6c50668Spatrick }
47f6c50668Spatrick
getNextNibble(const char * data,uint32_t * out)48f6c50668Spatrick const char* getNextNibble(const char* data, uint32_t* out) {
49f6c50668Spatrick *out = *reinterpret_cast<const uint16_t*>(data);
50f6c50668Spatrick return data + 2;
51f6c50668Spatrick }
52f6c50668Spatrick
53f6c50668Spatrick struct Descriptor {
54f6c50668Spatrick // See # 9.2
55f6c50668Spatrick typedef enum {
56f6c50668Spatrick SU16 = 0, // Short descriptor, 16-bit entries
57f6c50668Spatrick LU16 = 1, // Long descriptor, 16-bit entries
58f6c50668Spatrick LU32 = 3, // Long descriptor, 32-bit entries
59f6c50668Spatrick RESERVED0 = 4, RESERVED1 = 5, RESERVED2 = 6, RESERVED3 = 7,
60f6c50668Spatrick RESERVED4 = 8, RESERVED5 = 9, RESERVED6 = 10, RESERVED7 = 11,
61f6c50668Spatrick RESERVED8 = 12, RESERVED9 = 13, RESERVED10 = 14, RESERVED11 = 15
62f6c50668Spatrick } Format;
63f6c50668Spatrick
64f6c50668Spatrick // See # 9.2
65f6c50668Spatrick typedef enum {
66f6c50668Spatrick CLEANUP = 0x0,
67f6c50668Spatrick FUNC = 0x1,
68f6c50668Spatrick CATCH = 0x2,
69f6c50668Spatrick INVALID = 0x4
70f6c50668Spatrick } Kind;
71f6c50668Spatrick };
72f6c50668Spatrick
ProcessDescriptors(_Unwind_State state,_Unwind_Control_Block * ucbp,struct _Unwind_Context * context,Descriptor::Format format,const char * descriptorStart,uint32_t flags)73f6c50668Spatrick _Unwind_Reason_Code ProcessDescriptors(
74f6c50668Spatrick _Unwind_State state,
75f6c50668Spatrick _Unwind_Control_Block* ucbp,
76f6c50668Spatrick struct _Unwind_Context* context,
77f6c50668Spatrick Descriptor::Format format,
78f6c50668Spatrick const char* descriptorStart,
79f6c50668Spatrick uint32_t flags) {
80f6c50668Spatrick
81f6c50668Spatrick // EHT is inlined in the index using compact form. No descriptors. #5
82f6c50668Spatrick if (flags & 0x1)
83f6c50668Spatrick return _URC_CONTINUE_UNWIND;
84f6c50668Spatrick
85f6c50668Spatrick // TODO: We should check the state here, and determine whether we need to
86f6c50668Spatrick // perform phase1 or phase2 unwinding.
87f6c50668Spatrick (void)state;
88f6c50668Spatrick
89f6c50668Spatrick const char* descriptor = descriptorStart;
90f6c50668Spatrick uint32_t descriptorWord;
91f6c50668Spatrick getNextWord(descriptor, &descriptorWord);
92f6c50668Spatrick while (descriptorWord) {
93f6c50668Spatrick // Read descriptor based on # 9.2.
94f6c50668Spatrick uint32_t length;
95f6c50668Spatrick uint32_t offset;
96f6c50668Spatrick switch (format) {
97f6c50668Spatrick case Descriptor::LU32:
98f6c50668Spatrick descriptor = getNextWord(descriptor, &length);
99f6c50668Spatrick descriptor = getNextWord(descriptor, &offset);
100b3056a3bSpatrick break;
101f6c50668Spatrick case Descriptor::LU16:
102f6c50668Spatrick descriptor = getNextNibble(descriptor, &length);
103f6c50668Spatrick descriptor = getNextNibble(descriptor, &offset);
104b3056a3bSpatrick break;
105f6c50668Spatrick default:
106f6c50668Spatrick assert(false);
107f6c50668Spatrick return _URC_FAILURE;
108f6c50668Spatrick }
109f6c50668Spatrick
110f6c50668Spatrick // See # 9.2 table for decoding the kind of descriptor. It's a 2-bit value.
111f6c50668Spatrick Descriptor::Kind kind =
112f6c50668Spatrick static_cast<Descriptor::Kind>((length & 0x1) | ((offset & 0x1) << 1));
113f6c50668Spatrick
114f6c50668Spatrick // Clear off flag from last bit.
115f6c50668Spatrick length &= ~1u;
116f6c50668Spatrick offset &= ~1u;
117f6c50668Spatrick uintptr_t scopeStart = ucbp->pr_cache.fnstart + offset;
118f6c50668Spatrick uintptr_t scopeEnd = scopeStart + length;
119f6c50668Spatrick uintptr_t pc = _Unwind_GetIP(context);
120f6c50668Spatrick bool isInScope = (scopeStart <= pc) && (pc < scopeEnd);
121f6c50668Spatrick
122f6c50668Spatrick switch (kind) {
123f6c50668Spatrick case Descriptor::CLEANUP: {
124f6c50668Spatrick // TODO(ajwong): Handle cleanup descriptors.
125f6c50668Spatrick break;
126f6c50668Spatrick }
127f6c50668Spatrick case Descriptor::FUNC: {
128f6c50668Spatrick // TODO(ajwong): Handle function descriptors.
129f6c50668Spatrick break;
130f6c50668Spatrick }
131f6c50668Spatrick case Descriptor::CATCH: {
132f6c50668Spatrick // Catch descriptors require gobbling one more word.
133f6c50668Spatrick uint32_t landing_pad;
134f6c50668Spatrick descriptor = getNextWord(descriptor, &landing_pad);
135f6c50668Spatrick
136f6c50668Spatrick if (isInScope) {
137f6c50668Spatrick // TODO(ajwong): This is only phase1 compatible logic. Implement
138f6c50668Spatrick // phase2.
139f6c50668Spatrick landing_pad = signExtendPrel31(landing_pad & ~0x80000000);
140f6c50668Spatrick if (landing_pad == 0xffffffff) {
141f6c50668Spatrick return _URC_HANDLER_FOUND;
142f6c50668Spatrick } else if (landing_pad == 0xfffffffe) {
143f6c50668Spatrick return _URC_FAILURE;
144f6c50668Spatrick } else {
145f6c50668Spatrick /*
146f6c50668Spatrick bool is_reference_type = landing_pad & 0x80000000;
147f6c50668Spatrick void* matched_object;
148f6c50668Spatrick if (__cxxabiv1::__cxa_type_match(
149f6c50668Spatrick ucbp, reinterpret_cast<const std::type_info *>(landing_pad),
150f6c50668Spatrick is_reference_type,
151f6c50668Spatrick &matched_object) != __cxxabiv1::ctm_failed)
152f6c50668Spatrick return _URC_HANDLER_FOUND;
153f6c50668Spatrick */
154f6c50668Spatrick _LIBUNWIND_ABORT("Type matching not implemented");
155f6c50668Spatrick }
156f6c50668Spatrick }
157f6c50668Spatrick break;
158f6c50668Spatrick }
159f6c50668Spatrick default:
160f6c50668Spatrick _LIBUNWIND_ABORT("Invalid descriptor kind found.");
161f6c50668Spatrick }
162f6c50668Spatrick
163f6c50668Spatrick getNextWord(descriptor, &descriptorWord);
164f6c50668Spatrick }
165f6c50668Spatrick
166f6c50668Spatrick return _URC_CONTINUE_UNWIND;
167f6c50668Spatrick }
168f6c50668Spatrick
unwindOneFrame(_Unwind_State state,_Unwind_Control_Block * ucbp,struct _Unwind_Context * context)169f6c50668Spatrick static _Unwind_Reason_Code unwindOneFrame(_Unwind_State state,
170f6c50668Spatrick _Unwind_Control_Block* ucbp,
171f6c50668Spatrick struct _Unwind_Context* context) {
172f6c50668Spatrick // Read the compact model EHT entry's header # 6.3
173f6c50668Spatrick const uint32_t* unwindingData = ucbp->pr_cache.ehtp;
174f6c50668Spatrick assert((*unwindingData & 0xf0000000) == 0x80000000 && "Must be a compact entry");
175f6c50668Spatrick Descriptor::Format format =
176f6c50668Spatrick static_cast<Descriptor::Format>((*unwindingData & 0x0f000000) >> 24);
177f6c50668Spatrick
178f6c50668Spatrick const char *lsda =
179f6c50668Spatrick reinterpret_cast<const char *>(_Unwind_GetLanguageSpecificData(context));
180f6c50668Spatrick
181f6c50668Spatrick // Handle descriptors before unwinding so they are processed in the context
182f6c50668Spatrick // of the correct stack frame.
183f6c50668Spatrick _Unwind_Reason_Code result =
184f6c50668Spatrick ProcessDescriptors(state, ucbp, context, format, lsda,
185f6c50668Spatrick ucbp->pr_cache.additional);
186f6c50668Spatrick
187f6c50668Spatrick if (result != _URC_CONTINUE_UNWIND)
188f6c50668Spatrick return result;
189f6c50668Spatrick
190*0faf1914Srobert switch (__unw_step(reinterpret_cast<unw_cursor_t *>(context))) {
191*0faf1914Srobert case UNW_STEP_SUCCESS:
192f6c50668Spatrick return _URC_CONTINUE_UNWIND;
193*0faf1914Srobert case UNW_STEP_END:
194*0faf1914Srobert return _URC_END_OF_STACK;
195*0faf1914Srobert default:
196*0faf1914Srobert return _URC_FAILURE;
197*0faf1914Srobert }
198f6c50668Spatrick }
199f6c50668Spatrick
200f6c50668Spatrick // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_CORE /
201f6c50668Spatrick // _UVRSD_UINT32.
RegisterMask(uint8_t start,uint8_t count_minus_one)202f6c50668Spatrick uint32_t RegisterMask(uint8_t start, uint8_t count_minus_one) {
203f6c50668Spatrick return ((1U << (count_minus_one + 1)) - 1) << start;
204f6c50668Spatrick }
205f6c50668Spatrick
206f6c50668Spatrick // Generates mask discriminator for _Unwind_VRS_Pop, e.g. for _UVRSC_VFP /
207f6c50668Spatrick // _UVRSD_DOUBLE.
RegisterRange(uint8_t start,uint8_t count_minus_one)208f6c50668Spatrick uint32_t RegisterRange(uint8_t start, uint8_t count_minus_one) {
209f6c50668Spatrick return ((uint32_t)start << 16) | ((uint32_t)count_minus_one + 1);
210f6c50668Spatrick }
211f6c50668Spatrick
212f6c50668Spatrick } // end anonymous namespace
213f6c50668Spatrick
214f6c50668Spatrick /**
215f6c50668Spatrick * Decodes an EHT entry.
216f6c50668Spatrick *
217f6c50668Spatrick * @param data Pointer to EHT.
218f6c50668Spatrick * @param[out] off Offset from return value (in bytes) to begin interpretation.
219f6c50668Spatrick * @param[out] len Number of bytes in unwind code.
220f6c50668Spatrick * @return Pointer to beginning of unwind code.
221f6c50668Spatrick */
222f6c50668Spatrick extern "C" const uint32_t*
decode_eht_entry(const uint32_t * data,size_t * off,size_t * len)223f6c50668Spatrick decode_eht_entry(const uint32_t* data, size_t* off, size_t* len) {
224f6c50668Spatrick if ((*data & 0x80000000) == 0) {
225f6c50668Spatrick // 6.2: Generic Model
226f6c50668Spatrick //
227f6c50668Spatrick // EHT entry is a prel31 pointing to the PR, followed by data understood
228f6c50668Spatrick // only by the personality routine. Fortunately, all existing assembler
229f6c50668Spatrick // implementations, including GNU assembler, LLVM integrated assembler,
230f6c50668Spatrick // and ARM assembler, assume that the unwind opcodes come after the
231f6c50668Spatrick // personality rountine address.
232f6c50668Spatrick *off = 1; // First byte is size data.
233f6c50668Spatrick *len = (((data[1] >> 24) & 0xff) + 1) * 4;
234f6c50668Spatrick data++; // Skip the first word, which is the prel31 offset.
235f6c50668Spatrick } else {
236f6c50668Spatrick // 6.3: ARM Compact Model
237f6c50668Spatrick //
238*0faf1914Srobert // EHT entries here correspond to the __aeabi_unwind_cpp_pr[012] PRs indeed
239f6c50668Spatrick // by format:
240f6c50668Spatrick Descriptor::Format format =
241f6c50668Spatrick static_cast<Descriptor::Format>((*data & 0x0f000000) >> 24);
242f6c50668Spatrick switch (format) {
243f6c50668Spatrick case Descriptor::SU16:
244f6c50668Spatrick *len = 4;
245f6c50668Spatrick *off = 1;
246f6c50668Spatrick break;
247f6c50668Spatrick case Descriptor::LU16:
248f6c50668Spatrick case Descriptor::LU32:
249f6c50668Spatrick *len = 4 + 4 * ((*data & 0x00ff0000) >> 16);
250f6c50668Spatrick *off = 2;
251f6c50668Spatrick break;
252f6c50668Spatrick default:
253f6c50668Spatrick return nullptr;
254f6c50668Spatrick }
255f6c50668Spatrick }
256f6c50668Spatrick return data;
257f6c50668Spatrick }
258f6c50668Spatrick
259f6c50668Spatrick _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_VRS_Interpret(_Unwind_Context * context,const uint32_t * data,size_t offset,size_t len)260f6c50668Spatrick _Unwind_VRS_Interpret(_Unwind_Context *context, const uint32_t *data,
261f6c50668Spatrick size_t offset, size_t len) {
262f6c50668Spatrick bool wrotePC = false;
263f6c50668Spatrick bool finish = false;
264*0faf1914Srobert bool hasReturnAddrAuthCode = false;
265f6c50668Spatrick while (offset < len && !finish) {
266f6c50668Spatrick uint8_t byte = getByte(data, offset++);
267f6c50668Spatrick if ((byte & 0x80) == 0) {
268f6c50668Spatrick uint32_t sp;
269f6c50668Spatrick _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
270f6c50668Spatrick if (byte & 0x40)
271f6c50668Spatrick sp -= (((uint32_t)byte & 0x3f) << 2) + 4;
272f6c50668Spatrick else
273f6c50668Spatrick sp += ((uint32_t)byte << 2) + 4;
274f6c50668Spatrick _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
275f6c50668Spatrick } else {
276f6c50668Spatrick switch (byte & 0xf0) {
277f6c50668Spatrick case 0x80: {
278f6c50668Spatrick if (offset >= len)
279f6c50668Spatrick return _URC_FAILURE;
280f6c50668Spatrick uint32_t registers =
281f6c50668Spatrick (((uint32_t)byte & 0x0f) << 12) |
282f6c50668Spatrick (((uint32_t)getByte(data, offset++)) << 4);
283f6c50668Spatrick if (!registers)
284f6c50668Spatrick return _URC_FAILURE;
285f6c50668Spatrick if (registers & (1 << 15))
286f6c50668Spatrick wrotePC = true;
287f6c50668Spatrick _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
288f6c50668Spatrick break;
289f6c50668Spatrick }
290f6c50668Spatrick case 0x90: {
291f6c50668Spatrick uint8_t reg = byte & 0x0f;
292f6c50668Spatrick if (reg == 13 || reg == 15)
293f6c50668Spatrick return _URC_FAILURE;
294f6c50668Spatrick uint32_t sp;
295f6c50668Spatrick _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_R0 + reg,
296f6c50668Spatrick _UVRSD_UINT32, &sp);
297f6c50668Spatrick _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
298f6c50668Spatrick &sp);
299f6c50668Spatrick break;
300f6c50668Spatrick }
301f6c50668Spatrick case 0xa0: {
302f6c50668Spatrick uint32_t registers = RegisterMask(4, byte & 0x07);
303f6c50668Spatrick if (byte & 0x08)
304f6c50668Spatrick registers |= 1 << 14;
305f6c50668Spatrick _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
306f6c50668Spatrick break;
307f6c50668Spatrick }
308f6c50668Spatrick case 0xb0: {
309f6c50668Spatrick switch (byte) {
310f6c50668Spatrick case 0xb0:
311f6c50668Spatrick finish = true;
312f6c50668Spatrick break;
313f6c50668Spatrick case 0xb1: {
314f6c50668Spatrick if (offset >= len)
315f6c50668Spatrick return _URC_FAILURE;
316f6c50668Spatrick uint8_t registers = getByte(data, offset++);
317f6c50668Spatrick if (registers & 0xf0 || !registers)
318f6c50668Spatrick return _URC_FAILURE;
319f6c50668Spatrick _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
320f6c50668Spatrick break;
321f6c50668Spatrick }
322f6c50668Spatrick case 0xb2: {
323f6c50668Spatrick uint32_t addend = 0;
324f6c50668Spatrick uint32_t shift = 0;
325f6c50668Spatrick // This decodes a uleb128 value.
326f6c50668Spatrick while (true) {
327f6c50668Spatrick if (offset >= len)
328f6c50668Spatrick return _URC_FAILURE;
329f6c50668Spatrick uint32_t v = getByte(data, offset++);
330f6c50668Spatrick addend |= (v & 0x7f) << shift;
331f6c50668Spatrick if ((v & 0x80) == 0)
332f6c50668Spatrick break;
333f6c50668Spatrick shift += 7;
334f6c50668Spatrick }
335f6c50668Spatrick uint32_t sp;
336f6c50668Spatrick _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
337f6c50668Spatrick &sp);
338f6c50668Spatrick sp += 0x204 + (addend << 2);
339f6c50668Spatrick _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
340f6c50668Spatrick &sp);
341f6c50668Spatrick break;
342f6c50668Spatrick }
343f6c50668Spatrick case 0xb3: {
344f6c50668Spatrick uint8_t v = getByte(data, offset++);
345f6c50668Spatrick _Unwind_VRS_Pop(context, _UVRSC_VFP,
346f6c50668Spatrick RegisterRange(static_cast<uint8_t>(v >> 4),
347f6c50668Spatrick v & 0x0f), _UVRSD_VFPX);
348f6c50668Spatrick break;
349f6c50668Spatrick }
350f6c50668Spatrick case 0xb4:
351*0faf1914Srobert hasReturnAddrAuthCode = true;
352*0faf1914Srobert _Unwind_VRS_Pop(context, _UVRSC_PSEUDO,
353*0faf1914Srobert 0 /* Return Address Auth Code */, _UVRSD_UINT32);
354*0faf1914Srobert break;
355f6c50668Spatrick case 0xb5:
356f6c50668Spatrick case 0xb6:
357f6c50668Spatrick case 0xb7:
358f6c50668Spatrick return _URC_FAILURE;
359f6c50668Spatrick default:
360f6c50668Spatrick _Unwind_VRS_Pop(context, _UVRSC_VFP,
361f6c50668Spatrick RegisterRange(8, byte & 0x07), _UVRSD_VFPX);
362f6c50668Spatrick break;
363f6c50668Spatrick }
364f6c50668Spatrick break;
365f6c50668Spatrick }
366f6c50668Spatrick case 0xc0: {
367f6c50668Spatrick switch (byte) {
368f6c50668Spatrick #if defined(__ARM_WMMX)
369f6c50668Spatrick case 0xc0:
370f6c50668Spatrick case 0xc1:
371f6c50668Spatrick case 0xc2:
372f6c50668Spatrick case 0xc3:
373f6c50668Spatrick case 0xc4:
374f6c50668Spatrick case 0xc5:
375f6c50668Spatrick _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
376f6c50668Spatrick RegisterRange(10, byte & 0x7), _UVRSD_DOUBLE);
377f6c50668Spatrick break;
378f6c50668Spatrick case 0xc6: {
379f6c50668Spatrick uint8_t v = getByte(data, offset++);
380f6c50668Spatrick uint8_t start = static_cast<uint8_t>(v >> 4);
381f6c50668Spatrick uint8_t count_minus_one = v & 0xf;
382f6c50668Spatrick if (start + count_minus_one >= 16)
383f6c50668Spatrick return _URC_FAILURE;
384f6c50668Spatrick _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
385f6c50668Spatrick RegisterRange(start, count_minus_one),
386f6c50668Spatrick _UVRSD_DOUBLE);
387f6c50668Spatrick break;
388f6c50668Spatrick }
389f6c50668Spatrick case 0xc7: {
390f6c50668Spatrick uint8_t v = getByte(data, offset++);
391f6c50668Spatrick if (!v || v & 0xf0)
392f6c50668Spatrick return _URC_FAILURE;
393f6c50668Spatrick _Unwind_VRS_Pop(context, _UVRSC_WMMXC, v, _UVRSD_DOUBLE);
394f6c50668Spatrick break;
395f6c50668Spatrick }
396f6c50668Spatrick #endif
397f6c50668Spatrick case 0xc8:
398f6c50668Spatrick case 0xc9: {
399f6c50668Spatrick uint8_t v = getByte(data, offset++);
400f6c50668Spatrick uint8_t start =
401f6c50668Spatrick static_cast<uint8_t>(((byte == 0xc8) ? 16 : 0) + (v >> 4));
402f6c50668Spatrick uint8_t count_minus_one = v & 0xf;
403f6c50668Spatrick if (start + count_minus_one >= 32)
404f6c50668Spatrick return _URC_FAILURE;
405f6c50668Spatrick _Unwind_VRS_Pop(context, _UVRSC_VFP,
406f6c50668Spatrick RegisterRange(start, count_minus_one),
407f6c50668Spatrick _UVRSD_DOUBLE);
408f6c50668Spatrick break;
409f6c50668Spatrick }
410f6c50668Spatrick default:
411f6c50668Spatrick return _URC_FAILURE;
412f6c50668Spatrick }
413f6c50668Spatrick break;
414f6c50668Spatrick }
415f6c50668Spatrick case 0xd0: {
416f6c50668Spatrick if (byte & 0x08)
417f6c50668Spatrick return _URC_FAILURE;
418f6c50668Spatrick _Unwind_VRS_Pop(context, _UVRSC_VFP, RegisterRange(8, byte & 0x7),
419f6c50668Spatrick _UVRSD_DOUBLE);
420f6c50668Spatrick break;
421f6c50668Spatrick }
422f6c50668Spatrick default:
423f6c50668Spatrick return _URC_FAILURE;
424f6c50668Spatrick }
425f6c50668Spatrick }
426f6c50668Spatrick }
427f6c50668Spatrick if (!wrotePC) {
428f6c50668Spatrick uint32_t lr;
429f6c50668Spatrick _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_LR, _UVRSD_UINT32, &lr);
430*0faf1914Srobert #ifdef __ARM_FEATURE_PAUTH
431*0faf1914Srobert if (hasReturnAddrAuthCode) {
432*0faf1914Srobert uint32_t sp;
433*0faf1914Srobert uint32_t pac;
434*0faf1914Srobert _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
435*0faf1914Srobert _Unwind_VRS_Get(context, _UVRSC_PSEUDO, 0, _UVRSD_UINT32, &pac);
436*0faf1914Srobert __asm__ __volatile__("autg %0, %1, %2" : : "r"(pac), "r"(lr), "r"(sp) :);
437*0faf1914Srobert }
438*0faf1914Srobert #else
439*0faf1914Srobert (void)hasReturnAddrAuthCode;
440*0faf1914Srobert #endif
441f6c50668Spatrick _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_IP, _UVRSD_UINT32, &lr);
442f6c50668Spatrick }
443f6c50668Spatrick return _URC_CONTINUE_UNWIND;
444f6c50668Spatrick }
445f6c50668Spatrick
446f6c50668Spatrick extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
__aeabi_unwind_cpp_pr0(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context)447f6c50668Spatrick __aeabi_unwind_cpp_pr0(_Unwind_State state, _Unwind_Control_Block *ucbp,
448f6c50668Spatrick _Unwind_Context *context) {
449f6c50668Spatrick return unwindOneFrame(state, ucbp, context);
450f6c50668Spatrick }
451f6c50668Spatrick
452f6c50668Spatrick extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
__aeabi_unwind_cpp_pr1(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context)453f6c50668Spatrick __aeabi_unwind_cpp_pr1(_Unwind_State state, _Unwind_Control_Block *ucbp,
454f6c50668Spatrick _Unwind_Context *context) {
455f6c50668Spatrick return unwindOneFrame(state, ucbp, context);
456f6c50668Spatrick }
457f6c50668Spatrick
458f6c50668Spatrick extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
__aeabi_unwind_cpp_pr2(_Unwind_State state,_Unwind_Control_Block * ucbp,_Unwind_Context * context)459f6c50668Spatrick __aeabi_unwind_cpp_pr2(_Unwind_State state, _Unwind_Control_Block *ucbp,
460f6c50668Spatrick _Unwind_Context *context) {
461f6c50668Spatrick return unwindOneFrame(state, ucbp, context);
462f6c50668Spatrick }
463f6c50668Spatrick
464f6c50668Spatrick static _Unwind_Reason_Code
unwind_phase1(unw_context_t * uc,unw_cursor_t * cursor,_Unwind_Exception * exception_object)465f6c50668Spatrick unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
466f6c50668Spatrick // EHABI #7.3 discusses preserving the VRS in a "temporary VRS" during
467f6c50668Spatrick // phase 1 and then restoring it to the "primary VRS" for phase 2. The
468f6c50668Spatrick // effect is phase 2 doesn't see any of the VRS manipulations from phase 1.
469f6c50668Spatrick // In this implementation, the phases don't share the VRS backing store.
470f6c50668Spatrick // Instead, they are passed the original |uc| and they create a new VRS
471f6c50668Spatrick // from scratch thus achieving the same effect.
472f6c50668Spatrick __unw_init_local(cursor, uc);
473f6c50668Spatrick
474f6c50668Spatrick // Walk each frame looking for a place to stop.
475f6c50668Spatrick for (bool handlerNotFound = true; handlerNotFound;) {
476f6c50668Spatrick
477f6c50668Spatrick // See if frame has code to run (has personality routine).
478f6c50668Spatrick unw_proc_info_t frameInfo;
479f6c50668Spatrick if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
480f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
481f6c50668Spatrick "unwind_phase1(ex_ojb=%p): __unw_get_proc_info "
482f6c50668Spatrick "failed => _URC_FATAL_PHASE1_ERROR",
483f6c50668Spatrick static_cast<void *>(exception_object));
484f6c50668Spatrick return _URC_FATAL_PHASE1_ERROR;
485f6c50668Spatrick }
486f6c50668Spatrick
487*0faf1914Srobert #ifndef NDEBUG
488f6c50668Spatrick // When tracing, print state information.
489f6c50668Spatrick if (_LIBUNWIND_TRACING_UNWINDING) {
490f6c50668Spatrick char functionBuf[512];
491f6c50668Spatrick const char *functionName = functionBuf;
492f6c50668Spatrick unw_word_t offset;
493f6c50668Spatrick if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
494f6c50668Spatrick &offset) != UNW_ESUCCESS) ||
495f6c50668Spatrick (frameInfo.start_ip + offset > frameInfo.end_ip))
496f6c50668Spatrick functionName = ".anonymous.";
497f6c50668Spatrick unw_word_t pc;
498f6c50668Spatrick __unw_get_reg(cursor, UNW_REG_IP, &pc);
499f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
500f6c50668Spatrick "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR ", func=%s, "
501f6c50668Spatrick "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
502f6c50668Spatrick static_cast<void *>(exception_object), pc,
503f6c50668Spatrick frameInfo.start_ip, functionName,
504f6c50668Spatrick frameInfo.lsda, frameInfo.handler);
505f6c50668Spatrick }
506*0faf1914Srobert #endif
507f6c50668Spatrick
508f6c50668Spatrick // If there is a personality routine, ask it if it will want to stop at
509f6c50668Spatrick // this frame.
510f6c50668Spatrick if (frameInfo.handler != 0) {
511f6c50668Spatrick _Unwind_Personality_Fn p =
512f6c50668Spatrick (_Unwind_Personality_Fn)(long)(frameInfo.handler);
513f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
514f6c50668Spatrick "unwind_phase1(ex_ojb=%p): calling personality function %p",
515f6c50668Spatrick static_cast<void *>(exception_object),
516f6c50668Spatrick reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(p)));
517f6c50668Spatrick struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
518f6c50668Spatrick exception_object->pr_cache.fnstart = frameInfo.start_ip;
519f6c50668Spatrick exception_object->pr_cache.ehtp =
520f6c50668Spatrick (_Unwind_EHT_Header *)frameInfo.unwind_info;
521f6c50668Spatrick exception_object->pr_cache.additional = frameInfo.flags;
522f6c50668Spatrick _Unwind_Reason_Code personalityResult =
523f6c50668Spatrick (*p)(_US_VIRTUAL_UNWIND_FRAME, exception_object, context);
524f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
525f6c50668Spatrick "unwind_phase1(ex_ojb=%p): personality result %d start_ip %x ehtp %p "
526f6c50668Spatrick "additional %x",
527f6c50668Spatrick static_cast<void *>(exception_object), personalityResult,
528f6c50668Spatrick exception_object->pr_cache.fnstart,
529f6c50668Spatrick static_cast<void *>(exception_object->pr_cache.ehtp),
530f6c50668Spatrick exception_object->pr_cache.additional);
531f6c50668Spatrick switch (personalityResult) {
532f6c50668Spatrick case _URC_HANDLER_FOUND:
533f6c50668Spatrick // found a catch clause or locals that need destructing in this frame
534f6c50668Spatrick // stop search and remember stack pointer at the frame
535f6c50668Spatrick handlerNotFound = false;
536f6c50668Spatrick // p should have initialized barrier_cache. EHABI #7.3.5
537f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
538f6c50668Spatrick "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
539f6c50668Spatrick static_cast<void *>(exception_object));
540f6c50668Spatrick return _URC_NO_REASON;
541f6c50668Spatrick
542f6c50668Spatrick case _URC_CONTINUE_UNWIND:
543f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
544f6c50668Spatrick "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
545f6c50668Spatrick static_cast<void *>(exception_object));
546f6c50668Spatrick // continue unwinding
547f6c50668Spatrick break;
548f6c50668Spatrick
549f6c50668Spatrick // EHABI #7.3.3
550f6c50668Spatrick case _URC_FAILURE:
551f6c50668Spatrick return _URC_FAILURE;
552f6c50668Spatrick
553f6c50668Spatrick default:
554f6c50668Spatrick // something went wrong
555f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
556f6c50668Spatrick "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
557f6c50668Spatrick static_cast<void *>(exception_object));
558f6c50668Spatrick return _URC_FATAL_PHASE1_ERROR;
559f6c50668Spatrick }
560f6c50668Spatrick }
561f6c50668Spatrick }
562f6c50668Spatrick return _URC_NO_REASON;
563f6c50668Spatrick }
564f6c50668Spatrick
unwind_phase2(unw_context_t * uc,unw_cursor_t * cursor,_Unwind_Exception * exception_object,bool resume)565f6c50668Spatrick static _Unwind_Reason_Code unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor,
566f6c50668Spatrick _Unwind_Exception *exception_object,
567f6c50668Spatrick bool resume) {
568f6c50668Spatrick // See comment at the start of unwind_phase1 regarding VRS integrity.
569f6c50668Spatrick __unw_init_local(cursor, uc);
570f6c50668Spatrick
571f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
572f6c50668Spatrick static_cast<void *>(exception_object));
573f6c50668Spatrick int frame_count = 0;
574f6c50668Spatrick
575f6c50668Spatrick // Walk each frame until we reach where search phase said to stop.
576f6c50668Spatrick while (true) {
577f6c50668Spatrick // Ask libunwind to get next frame (skip over first which is
578f6c50668Spatrick // _Unwind_RaiseException or _Unwind_Resume).
579f6c50668Spatrick //
580f6c50668Spatrick // Resume only ever makes sense for 1 frame.
581f6c50668Spatrick _Unwind_State state =
582f6c50668Spatrick resume ? _US_UNWIND_FRAME_RESUME : _US_UNWIND_FRAME_STARTING;
583f6c50668Spatrick if (resume && frame_count == 1) {
584f6c50668Spatrick // On a resume, first unwind the _Unwind_Resume() frame. The next frame
585f6c50668Spatrick // is now the landing pad for the cleanup from a previous execution of
586f6c50668Spatrick // phase2. To continue unwindingly correctly, replace VRS[15] with the
587f6c50668Spatrick // IP of the frame that the previous run of phase2 installed the context
588f6c50668Spatrick // for. After this, continue unwinding as if normal.
589f6c50668Spatrick //
590f6c50668Spatrick // See #7.4.6 for details.
591f6c50668Spatrick __unw_set_reg(cursor, UNW_REG_IP,
592f6c50668Spatrick exception_object->unwinder_cache.reserved2);
593f6c50668Spatrick resume = false;
594f6c50668Spatrick }
595f6c50668Spatrick
596f6c50668Spatrick // Get info about this frame.
597f6c50668Spatrick unw_word_t sp;
598f6c50668Spatrick unw_proc_info_t frameInfo;
599f6c50668Spatrick __unw_get_reg(cursor, UNW_REG_SP, &sp);
600f6c50668Spatrick if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
601f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
602f6c50668Spatrick "unwind_phase2(ex_ojb=%p): __unw_get_proc_info "
603f6c50668Spatrick "failed => _URC_FATAL_PHASE2_ERROR",
604f6c50668Spatrick static_cast<void *>(exception_object));
605f6c50668Spatrick return _URC_FATAL_PHASE2_ERROR;
606f6c50668Spatrick }
607f6c50668Spatrick
608*0faf1914Srobert #ifndef NDEBUG
609f6c50668Spatrick // When tracing, print state information.
610f6c50668Spatrick if (_LIBUNWIND_TRACING_UNWINDING) {
611f6c50668Spatrick char functionBuf[512];
612f6c50668Spatrick const char *functionName = functionBuf;
613f6c50668Spatrick unw_word_t offset;
614f6c50668Spatrick if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
615f6c50668Spatrick &offset) != UNW_ESUCCESS) ||
616f6c50668Spatrick (frameInfo.start_ip + offset > frameInfo.end_ip))
617f6c50668Spatrick functionName = ".anonymous.";
618f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
619f6c50668Spatrick "unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR ", func=%s, sp=0x%" PRIxPTR ", "
620f6c50668Spatrick "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
621f6c50668Spatrick static_cast<void *>(exception_object), frameInfo.start_ip,
622f6c50668Spatrick functionName, sp, frameInfo.lsda,
623f6c50668Spatrick frameInfo.handler);
624f6c50668Spatrick }
625*0faf1914Srobert #endif
626f6c50668Spatrick
627f6c50668Spatrick // If there is a personality routine, tell it we are unwinding.
628f6c50668Spatrick if (frameInfo.handler != 0) {
629f6c50668Spatrick _Unwind_Personality_Fn p =
630*0faf1914Srobert (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
631f6c50668Spatrick struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
632f6c50668Spatrick // EHABI #7.2
633f6c50668Spatrick exception_object->pr_cache.fnstart = frameInfo.start_ip;
634f6c50668Spatrick exception_object->pr_cache.ehtp =
635f6c50668Spatrick (_Unwind_EHT_Header *)frameInfo.unwind_info;
636f6c50668Spatrick exception_object->pr_cache.additional = frameInfo.flags;
637f6c50668Spatrick _Unwind_Reason_Code personalityResult =
638f6c50668Spatrick (*p)(state, exception_object, context);
639f6c50668Spatrick switch (personalityResult) {
640f6c50668Spatrick case _URC_CONTINUE_UNWIND:
641f6c50668Spatrick // Continue unwinding
642f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
643f6c50668Spatrick "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
644f6c50668Spatrick static_cast<void *>(exception_object));
645f6c50668Spatrick // EHABI #7.2
646f6c50668Spatrick if (sp == exception_object->barrier_cache.sp) {
647f6c50668Spatrick // Phase 1 said we would stop at this frame, but we did not...
648f6c50668Spatrick _LIBUNWIND_ABORT("during phase1 personality function said it would "
649f6c50668Spatrick "stop here, but now in phase2 it did not stop here");
650f6c50668Spatrick }
651f6c50668Spatrick break;
652f6c50668Spatrick case _URC_INSTALL_CONTEXT:
653f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING(
654f6c50668Spatrick "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
655f6c50668Spatrick static_cast<void *>(exception_object));
656f6c50668Spatrick // Personality routine says to transfer control to landing pad.
657f6c50668Spatrick // We may get control back if landing pad calls _Unwind_Resume().
658f6c50668Spatrick if (_LIBUNWIND_TRACING_UNWINDING) {
659f6c50668Spatrick unw_word_t pc;
660f6c50668Spatrick __unw_get_reg(cursor, UNW_REG_IP, &pc);
661f6c50668Spatrick __unw_get_reg(cursor, UNW_REG_SP, &sp);
662f6c50668Spatrick _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
663f6c50668Spatrick "user code with ip=0x%" PRIxPTR ", sp=0x%" PRIxPTR,
664f6c50668Spatrick static_cast<void *>(exception_object),
665f6c50668Spatrick pc, sp);
666f6c50668Spatrick }
667f6c50668Spatrick
668f6c50668Spatrick {
669f6c50668Spatrick // EHABI #7.4.1 says we need to preserve pc for when _Unwind_Resume
670f6c50668Spatrick // is called back, to find this same frame.
671f6c50668Spatrick unw_word_t pc;
672f6c50668Spatrick __unw_get_reg(cursor, UNW_REG_IP, &pc);
673f6c50668Spatrick exception_object->unwinder_cache.reserved2 = (uint32_t)pc;
674f6c50668Spatrick }
675f6c50668Spatrick __unw_resume(cursor);
676f6c50668Spatrick // __unw_resume() only returns if there was an error.
677f6c50668Spatrick return _URC_FATAL_PHASE2_ERROR;
678f6c50668Spatrick
679f6c50668Spatrick // # EHABI #7.4.3
680f6c50668Spatrick case _URC_FAILURE:
681f6c50668Spatrick abort();
682f6c50668Spatrick
683f6c50668Spatrick default:
684f6c50668Spatrick // Personality routine returned an unknown result code.
685f6c50668Spatrick _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
686f6c50668Spatrick personalityResult);
687f6c50668Spatrick return _URC_FATAL_PHASE2_ERROR;
688f6c50668Spatrick }
689f6c50668Spatrick }
690f6c50668Spatrick frame_count++;
691f6c50668Spatrick }
692f6c50668Spatrick
693f6c50668Spatrick // Clean up phase did not resume at the frame that the search phase
694f6c50668Spatrick // said it would...
695f6c50668Spatrick return _URC_FATAL_PHASE2_ERROR;
696f6c50668Spatrick }
697f6c50668Spatrick
698*0faf1914Srobert static _Unwind_Reason_Code
unwind_phase2_forced(unw_context_t * uc,unw_cursor_t * cursor,_Unwind_Exception * exception_object,_Unwind_Stop_Fn stop,void * stop_parameter)699*0faf1914Srobert unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
700*0faf1914Srobert _Unwind_Exception *exception_object, _Unwind_Stop_Fn stop,
701*0faf1914Srobert void *stop_parameter) {
702*0faf1914Srobert bool endOfStack = false;
703*0faf1914Srobert // See comment at the start of unwind_phase1 regarding VRS integrity.
704*0faf1914Srobert __unw_init_local(cursor, uc);
705*0faf1914Srobert _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_force(ex_ojb=%p)",
706*0faf1914Srobert static_cast<void *>(exception_object));
707*0faf1914Srobert // Walk each frame until we reach where search phase said to stop
708*0faf1914Srobert while (!endOfStack) {
709*0faf1914Srobert // Update info about this frame.
710*0faf1914Srobert unw_proc_info_t frameInfo;
711*0faf1914Srobert if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
712*0faf1914Srobert _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step "
713*0faf1914Srobert "failed => _URC_END_OF_STACK",
714*0faf1914Srobert (void *)exception_object);
715*0faf1914Srobert return _URC_FATAL_PHASE2_ERROR;
716*0faf1914Srobert }
717*0faf1914Srobert
718*0faf1914Srobert #ifndef NDEBUG
719*0faf1914Srobert // When tracing, print state information.
720*0faf1914Srobert if (_LIBUNWIND_TRACING_UNWINDING) {
721*0faf1914Srobert char functionBuf[512];
722*0faf1914Srobert const char *functionName = functionBuf;
723*0faf1914Srobert unw_word_t offset;
724*0faf1914Srobert if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
725*0faf1914Srobert &offset) != UNW_ESUCCESS) ||
726*0faf1914Srobert (frameInfo.start_ip + offset > frameInfo.end_ip))
727*0faf1914Srobert functionName = ".anonymous.";
728*0faf1914Srobert _LIBUNWIND_TRACE_UNWINDING(
729*0faf1914Srobert "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
730*0faf1914Srobert ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
731*0faf1914Srobert (void *)exception_object, frameInfo.start_ip, functionName,
732*0faf1914Srobert frameInfo.lsda, frameInfo.handler);
733*0faf1914Srobert }
734*0faf1914Srobert #endif
735*0faf1914Srobert
736*0faf1914Srobert // Call stop function at each frame.
737*0faf1914Srobert _Unwind_Action action =
738*0faf1914Srobert (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
739*0faf1914Srobert _Unwind_Reason_Code stopResult =
740*0faf1914Srobert (*stop)(1, action, exception_object->exception_class, exception_object,
741*0faf1914Srobert (_Unwind_Context *)(cursor), stop_parameter);
742*0faf1914Srobert _LIBUNWIND_TRACE_UNWINDING(
743*0faf1914Srobert "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
744*0faf1914Srobert (void *)exception_object, stopResult);
745*0faf1914Srobert if (stopResult != _URC_NO_REASON) {
746*0faf1914Srobert _LIBUNWIND_TRACE_UNWINDING(
747*0faf1914Srobert "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
748*0faf1914Srobert (void *)exception_object);
749*0faf1914Srobert return _URC_FATAL_PHASE2_ERROR;
750*0faf1914Srobert }
751*0faf1914Srobert
752*0faf1914Srobert // If there is a personality routine, tell it we are unwinding.
753*0faf1914Srobert if (frameInfo.handler != 0) {
754*0faf1914Srobert _Unwind_Personality_Fn p =
755*0faf1914Srobert (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
756*0faf1914Srobert struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
757*0faf1914Srobert // EHABI #7.2
758*0faf1914Srobert exception_object->pr_cache.fnstart = frameInfo.start_ip;
759*0faf1914Srobert exception_object->pr_cache.ehtp =
760*0faf1914Srobert (_Unwind_EHT_Header *)frameInfo.unwind_info;
761*0faf1914Srobert exception_object->pr_cache.additional = frameInfo.flags;
762*0faf1914Srobert _Unwind_Reason_Code personalityResult =
763*0faf1914Srobert (*p)(_US_FORCE_UNWIND | _US_UNWIND_FRAME_STARTING, exception_object,
764*0faf1914Srobert context);
765*0faf1914Srobert switch (personalityResult) {
766*0faf1914Srobert case _URC_CONTINUE_UNWIND:
767*0faf1914Srobert _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
768*0faf1914Srobert "personality returned "
769*0faf1914Srobert "_URC_CONTINUE_UNWIND",
770*0faf1914Srobert (void *)exception_object);
771*0faf1914Srobert // Destructors called, continue unwinding
772*0faf1914Srobert break;
773*0faf1914Srobert case _URC_INSTALL_CONTEXT:
774*0faf1914Srobert _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
775*0faf1914Srobert "personality returned "
776*0faf1914Srobert "_URC_INSTALL_CONTEXT",
777*0faf1914Srobert (void *)exception_object);
778*0faf1914Srobert // We may get control back if landing pad calls _Unwind_Resume().
779*0faf1914Srobert __unw_resume(cursor);
780*0faf1914Srobert break;
781*0faf1914Srobert case _URC_END_OF_STACK:
782*0faf1914Srobert _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
783*0faf1914Srobert "personality returned "
784*0faf1914Srobert "_URC_END_OF_STACK",
785*0faf1914Srobert (void *)exception_object);
786*0faf1914Srobert // Personalty routine did the step and it can't step forward.
787*0faf1914Srobert endOfStack = true;
788*0faf1914Srobert break;
789*0faf1914Srobert default:
790*0faf1914Srobert // Personality routine returned an unknown result code.
791*0faf1914Srobert _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
792*0faf1914Srobert "personality returned %d, "
793*0faf1914Srobert "_URC_FATAL_PHASE2_ERROR",
794*0faf1914Srobert (void *)exception_object, personalityResult);
795*0faf1914Srobert return _URC_FATAL_PHASE2_ERROR;
796*0faf1914Srobert }
797*0faf1914Srobert }
798*0faf1914Srobert }
799*0faf1914Srobert
800*0faf1914Srobert // Call stop function one last time and tell it we've reached the end
801*0faf1914Srobert // of the stack.
802*0faf1914Srobert _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
803*0faf1914Srobert "function with _UA_END_OF_STACK",
804*0faf1914Srobert (void *)exception_object);
805*0faf1914Srobert _Unwind_Action lastAction =
806*0faf1914Srobert (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
807*0faf1914Srobert (*stop)(1, lastAction, exception_object->exception_class, exception_object,
808*0faf1914Srobert (struct _Unwind_Context *)(cursor), stop_parameter);
809*0faf1914Srobert
810*0faf1914Srobert // Clean up phase did not resume at the frame that the search phase said it
811*0faf1914Srobert // would.
812*0faf1914Srobert return _URC_FATAL_PHASE2_ERROR;
813*0faf1914Srobert }
814*0faf1914Srobert
815f6c50668Spatrick /// Called by __cxa_throw. Only returns if there is a fatal error.
816f6c50668Spatrick _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_RaiseException(_Unwind_Exception * exception_object)817f6c50668Spatrick _Unwind_RaiseException(_Unwind_Exception *exception_object) {
818f6c50668Spatrick _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
819f6c50668Spatrick static_cast<void *>(exception_object));
820f6c50668Spatrick unw_context_t uc;
821f6c50668Spatrick unw_cursor_t cursor;
822f6c50668Spatrick __unw_getcontext(&uc);
823f6c50668Spatrick
824f6c50668Spatrick // This field for is for compatibility with GCC to say this isn't a forced
825f6c50668Spatrick // unwind. EHABI #7.2
826f6c50668Spatrick exception_object->unwinder_cache.reserved1 = 0;
827f6c50668Spatrick
828f6c50668Spatrick // phase 1: the search phase
829f6c50668Spatrick _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
830f6c50668Spatrick if (phase1 != _URC_NO_REASON)
831f6c50668Spatrick return phase1;
832f6c50668Spatrick
833f6c50668Spatrick // phase 2: the clean up phase
834f6c50668Spatrick return unwind_phase2(&uc, &cursor, exception_object, false);
835f6c50668Spatrick }
836f6c50668Spatrick
_Unwind_Complete(_Unwind_Exception * exception_object)837f6c50668Spatrick _LIBUNWIND_EXPORT void _Unwind_Complete(_Unwind_Exception* exception_object) {
838f6c50668Spatrick // This is to be called when exception handling completes to give us a chance
839f6c50668Spatrick // to perform any housekeeping. EHABI #7.2. But we have nothing to do here.
840f6c50668Spatrick (void)exception_object;
841f6c50668Spatrick }
842f6c50668Spatrick
843f6c50668Spatrick /// When _Unwind_RaiseException() is in phase2, it hands control
844f6c50668Spatrick /// to the personality function at each frame. The personality
845f6c50668Spatrick /// may force a jump to a landing pad in that function, the landing
846f6c50668Spatrick /// pad code may then call _Unwind_Resume() to continue with the
847f6c50668Spatrick /// unwinding. Note: the call to _Unwind_Resume() is from compiler
848*0faf1914Srobert /// generated user code. All other _Unwind_* routines are called
849f6c50668Spatrick /// by the C++ runtime __cxa_* routines.
850f6c50668Spatrick ///
851f6c50668Spatrick /// Note: re-throwing an exception (as opposed to continuing the unwind)
852f6c50668Spatrick /// is implemented by having the code call __cxa_rethrow() which
853f6c50668Spatrick /// in turn calls _Unwind_Resume_or_Rethrow().
854f6c50668Spatrick _LIBUNWIND_EXPORT void
_Unwind_Resume(_Unwind_Exception * exception_object)855f6c50668Spatrick _Unwind_Resume(_Unwind_Exception *exception_object) {
856f6c50668Spatrick _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)",
857f6c50668Spatrick static_cast<void *>(exception_object));
858f6c50668Spatrick unw_context_t uc;
859f6c50668Spatrick unw_cursor_t cursor;
860f6c50668Spatrick __unw_getcontext(&uc);
861f6c50668Spatrick
862*0faf1914Srobert if (exception_object->unwinder_cache.reserved1)
863*0faf1914Srobert unwind_phase2_forced(
864*0faf1914Srobert &uc, &cursor, exception_object,
865*0faf1914Srobert (_Unwind_Stop_Fn)exception_object->unwinder_cache.reserved1,
866*0faf1914Srobert (void *)exception_object->unwinder_cache.reserved3);
867*0faf1914Srobert else
868f6c50668Spatrick unwind_phase2(&uc, &cursor, exception_object, true);
869f6c50668Spatrick
870f6c50668Spatrick // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
871f6c50668Spatrick _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
872f6c50668Spatrick }
873f6c50668Spatrick
874f6c50668Spatrick /// Called by personality handler during phase 2 to get LSDA for current frame.
875f6c50668Spatrick _LIBUNWIND_EXPORT uintptr_t
_Unwind_GetLanguageSpecificData(struct _Unwind_Context * context)876f6c50668Spatrick _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
877f6c50668Spatrick unw_cursor_t *cursor = (unw_cursor_t *)context;
878f6c50668Spatrick unw_proc_info_t frameInfo;
879f6c50668Spatrick uintptr_t result = 0;
880f6c50668Spatrick if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
881f6c50668Spatrick result = (uintptr_t)frameInfo.lsda;
882f6c50668Spatrick _LIBUNWIND_TRACE_API(
883f6c50668Spatrick "_Unwind_GetLanguageSpecificData(context=%p) => 0x%llx",
884f6c50668Spatrick static_cast<void *>(context), (long long)result);
885f6c50668Spatrick return result;
886f6c50668Spatrick }
887f6c50668Spatrick
ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation,void * valuep)888f6c50668Spatrick static uint64_t ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation,
889f6c50668Spatrick void* valuep) {
890f6c50668Spatrick uint64_t value = 0;
891f6c50668Spatrick switch (representation) {
892f6c50668Spatrick case _UVRSD_UINT32:
893f6c50668Spatrick case _UVRSD_FLOAT:
894f6c50668Spatrick memcpy(&value, valuep, sizeof(uint32_t));
895f6c50668Spatrick break;
896f6c50668Spatrick
897f6c50668Spatrick case _UVRSD_VFPX:
898f6c50668Spatrick case _UVRSD_UINT64:
899f6c50668Spatrick case _UVRSD_DOUBLE:
900f6c50668Spatrick memcpy(&value, valuep, sizeof(uint64_t));
901f6c50668Spatrick break;
902f6c50668Spatrick }
903f6c50668Spatrick return value;
904f6c50668Spatrick }
905f6c50668Spatrick
906f6c50668Spatrick _LIBUNWIND_EXPORT _Unwind_VRS_Result
_Unwind_VRS_Set(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,uint32_t regno,_Unwind_VRS_DataRepresentation representation,void * valuep)907f6c50668Spatrick _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
908f6c50668Spatrick uint32_t regno, _Unwind_VRS_DataRepresentation representation,
909f6c50668Spatrick void *valuep) {
910f6c50668Spatrick _LIBUNWIND_TRACE_API("_Unwind_VRS_Set(context=%p, regclass=%d, reg=%d, "
911f6c50668Spatrick "rep=%d, value=0x%llX)",
912f6c50668Spatrick static_cast<void *>(context), regclass, regno,
913f6c50668Spatrick representation,
914f6c50668Spatrick ValueAsBitPattern(representation, valuep));
915f6c50668Spatrick unw_cursor_t *cursor = (unw_cursor_t *)context;
916f6c50668Spatrick switch (regclass) {
917f6c50668Spatrick case _UVRSC_CORE:
918f6c50668Spatrick if (representation != _UVRSD_UINT32 || regno > 15)
919f6c50668Spatrick return _UVRSR_FAILED;
920f6c50668Spatrick return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
921f6c50668Spatrick *(unw_word_t *)valuep) == UNW_ESUCCESS
922f6c50668Spatrick ? _UVRSR_OK
923f6c50668Spatrick : _UVRSR_FAILED;
924f6c50668Spatrick case _UVRSC_VFP:
925f6c50668Spatrick if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
926f6c50668Spatrick return _UVRSR_FAILED;
927f6c50668Spatrick if (representation == _UVRSD_VFPX) {
928f6c50668Spatrick // Can only touch d0-15 with FSTMFDX.
929f6c50668Spatrick if (regno > 15)
930f6c50668Spatrick return _UVRSR_FAILED;
931f6c50668Spatrick __unw_save_vfp_as_X(cursor);
932f6c50668Spatrick } else {
933f6c50668Spatrick if (regno > 31)
934f6c50668Spatrick return _UVRSR_FAILED;
935f6c50668Spatrick }
936f6c50668Spatrick return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
937f6c50668Spatrick *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
938f6c50668Spatrick ? _UVRSR_OK
939f6c50668Spatrick : _UVRSR_FAILED;
940f6c50668Spatrick #if defined(__ARM_WMMX)
941f6c50668Spatrick case _UVRSC_WMMXC:
942f6c50668Spatrick if (representation != _UVRSD_UINT32 || regno > 3)
943f6c50668Spatrick return _UVRSR_FAILED;
944f6c50668Spatrick return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
945f6c50668Spatrick *(unw_word_t *)valuep) == UNW_ESUCCESS
946f6c50668Spatrick ? _UVRSR_OK
947f6c50668Spatrick : _UVRSR_FAILED;
948f6c50668Spatrick case _UVRSC_WMMXD:
949f6c50668Spatrick if (representation != _UVRSD_DOUBLE || regno > 31)
950f6c50668Spatrick return _UVRSR_FAILED;
951f6c50668Spatrick return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
952f6c50668Spatrick *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
953f6c50668Spatrick ? _UVRSR_OK
954f6c50668Spatrick : _UVRSR_FAILED;
955f6c50668Spatrick #else
956f6c50668Spatrick case _UVRSC_WMMXC:
957f6c50668Spatrick case _UVRSC_WMMXD:
958f6c50668Spatrick break;
959f6c50668Spatrick #endif
960*0faf1914Srobert case _UVRSC_PSEUDO:
961*0faf1914Srobert // There's only one pseudo-register, PAC, with regno == 0.
962*0faf1914Srobert if (representation != _UVRSD_UINT32 || regno != 0)
963*0faf1914Srobert return _UVRSR_FAILED;
964*0faf1914Srobert return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_RA_AUTH_CODE),
965*0faf1914Srobert *(unw_word_t *)valuep) == UNW_ESUCCESS
966*0faf1914Srobert ? _UVRSR_OK
967*0faf1914Srobert : _UVRSR_FAILED;
968*0faf1914Srobert break;
969f6c50668Spatrick }
970f6c50668Spatrick _LIBUNWIND_ABORT("unsupported register class");
971f6c50668Spatrick }
972f6c50668Spatrick
973f6c50668Spatrick static _Unwind_VRS_Result
_Unwind_VRS_Get_Internal(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,uint32_t regno,_Unwind_VRS_DataRepresentation representation,void * valuep)974f6c50668Spatrick _Unwind_VRS_Get_Internal(_Unwind_Context *context,
975f6c50668Spatrick _Unwind_VRS_RegClass regclass, uint32_t regno,
976f6c50668Spatrick _Unwind_VRS_DataRepresentation representation,
977f6c50668Spatrick void *valuep) {
978f6c50668Spatrick unw_cursor_t *cursor = (unw_cursor_t *)context;
979f6c50668Spatrick switch (regclass) {
980f6c50668Spatrick case _UVRSC_CORE:
981f6c50668Spatrick if (representation != _UVRSD_UINT32 || regno > 15)
982f6c50668Spatrick return _UVRSR_FAILED;
983f6c50668Spatrick return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
984f6c50668Spatrick (unw_word_t *)valuep) == UNW_ESUCCESS
985f6c50668Spatrick ? _UVRSR_OK
986f6c50668Spatrick : _UVRSR_FAILED;
987f6c50668Spatrick case _UVRSC_VFP:
988f6c50668Spatrick if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
989f6c50668Spatrick return _UVRSR_FAILED;
990f6c50668Spatrick if (representation == _UVRSD_VFPX) {
991f6c50668Spatrick // Can only touch d0-15 with FSTMFDX.
992f6c50668Spatrick if (regno > 15)
993f6c50668Spatrick return _UVRSR_FAILED;
994f6c50668Spatrick __unw_save_vfp_as_X(cursor);
995f6c50668Spatrick } else {
996f6c50668Spatrick if (regno > 31)
997f6c50668Spatrick return _UVRSR_FAILED;
998f6c50668Spatrick }
999f6c50668Spatrick return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
1000f6c50668Spatrick (unw_fpreg_t *)valuep) == UNW_ESUCCESS
1001f6c50668Spatrick ? _UVRSR_OK
1002f6c50668Spatrick : _UVRSR_FAILED;
1003f6c50668Spatrick #if defined(__ARM_WMMX)
1004f6c50668Spatrick case _UVRSC_WMMXC:
1005f6c50668Spatrick if (representation != _UVRSD_UINT32 || regno > 3)
1006f6c50668Spatrick return _UVRSR_FAILED;
1007f6c50668Spatrick return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
1008f6c50668Spatrick (unw_word_t *)valuep) == UNW_ESUCCESS
1009f6c50668Spatrick ? _UVRSR_OK
1010f6c50668Spatrick : _UVRSR_FAILED;
1011f6c50668Spatrick case _UVRSC_WMMXD:
1012f6c50668Spatrick if (representation != _UVRSD_DOUBLE || regno > 31)
1013f6c50668Spatrick return _UVRSR_FAILED;
1014f6c50668Spatrick return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
1015f6c50668Spatrick (unw_fpreg_t *)valuep) == UNW_ESUCCESS
1016f6c50668Spatrick ? _UVRSR_OK
1017f6c50668Spatrick : _UVRSR_FAILED;
1018f6c50668Spatrick #else
1019f6c50668Spatrick case _UVRSC_WMMXC:
1020f6c50668Spatrick case _UVRSC_WMMXD:
1021f6c50668Spatrick break;
1022f6c50668Spatrick #endif
1023*0faf1914Srobert case _UVRSC_PSEUDO:
1024*0faf1914Srobert // There's only one pseudo-register, PAC, with regno == 0.
1025*0faf1914Srobert if (representation != _UVRSD_UINT32 || regno != 0)
1026*0faf1914Srobert return _UVRSR_FAILED;
1027*0faf1914Srobert return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_RA_AUTH_CODE),
1028*0faf1914Srobert (unw_word_t *)valuep) == UNW_ESUCCESS
1029*0faf1914Srobert ? _UVRSR_OK
1030*0faf1914Srobert : _UVRSR_FAILED;
1031*0faf1914Srobert break;
1032f6c50668Spatrick }
1033f6c50668Spatrick _LIBUNWIND_ABORT("unsupported register class");
1034f6c50668Spatrick }
1035f6c50668Spatrick
1036f6c50668Spatrick _LIBUNWIND_EXPORT _Unwind_VRS_Result
_Unwind_VRS_Get(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,uint32_t regno,_Unwind_VRS_DataRepresentation representation,void * valuep)1037f6c50668Spatrick _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
1038f6c50668Spatrick uint32_t regno, _Unwind_VRS_DataRepresentation representation,
1039f6c50668Spatrick void *valuep) {
1040f6c50668Spatrick _Unwind_VRS_Result result =
1041f6c50668Spatrick _Unwind_VRS_Get_Internal(context, regclass, regno, representation,
1042f6c50668Spatrick valuep);
1043f6c50668Spatrick _LIBUNWIND_TRACE_API("_Unwind_VRS_Get(context=%p, regclass=%d, reg=%d, "
1044f6c50668Spatrick "rep=%d, value=0x%llX, result = %d)",
1045f6c50668Spatrick static_cast<void *>(context), regclass, regno,
1046f6c50668Spatrick representation,
1047f6c50668Spatrick ValueAsBitPattern(representation, valuep), result);
1048f6c50668Spatrick return result;
1049f6c50668Spatrick }
1050f6c50668Spatrick
1051f6c50668Spatrick _Unwind_VRS_Result
_Unwind_VRS_Pop(_Unwind_Context * context,_Unwind_VRS_RegClass regclass,uint32_t discriminator,_Unwind_VRS_DataRepresentation representation)1052f6c50668Spatrick _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
1053f6c50668Spatrick uint32_t discriminator,
1054f6c50668Spatrick _Unwind_VRS_DataRepresentation representation) {
1055f6c50668Spatrick _LIBUNWIND_TRACE_API("_Unwind_VRS_Pop(context=%p, regclass=%d, "
1056f6c50668Spatrick "discriminator=%d, representation=%d)",
1057f6c50668Spatrick static_cast<void *>(context), regclass, discriminator,
1058f6c50668Spatrick representation);
1059f6c50668Spatrick switch (regclass) {
1060f6c50668Spatrick case _UVRSC_WMMXC:
1061f6c50668Spatrick #if !defined(__ARM_WMMX)
1062f6c50668Spatrick break;
1063f6c50668Spatrick #endif
1064f6c50668Spatrick case _UVRSC_CORE: {
1065f6c50668Spatrick if (representation != _UVRSD_UINT32)
1066f6c50668Spatrick return _UVRSR_FAILED;
1067f6c50668Spatrick // When popping SP from the stack, we don't want to override it from the
1068f6c50668Spatrick // computed new stack location. See EHABI #7.5.4 table 3.
1069f6c50668Spatrick bool poppedSP = false;
1070f6c50668Spatrick uint32_t* sp;
1071f6c50668Spatrick if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
1072f6c50668Spatrick _UVRSD_UINT32, &sp) != _UVRSR_OK) {
1073f6c50668Spatrick return _UVRSR_FAILED;
1074f6c50668Spatrick }
1075f6c50668Spatrick for (uint32_t i = 0; i < 16; ++i) {
1076f6c50668Spatrick if (!(discriminator & static_cast<uint32_t>(1 << i)))
1077f6c50668Spatrick continue;
1078f6c50668Spatrick uint32_t value = *sp++;
1079f6c50668Spatrick if (regclass == _UVRSC_CORE && i == 13)
1080f6c50668Spatrick poppedSP = true;
1081f6c50668Spatrick if (_Unwind_VRS_Set(context, regclass, i,
1082f6c50668Spatrick _UVRSD_UINT32, &value) != _UVRSR_OK) {
1083f6c50668Spatrick return _UVRSR_FAILED;
1084f6c50668Spatrick }
1085f6c50668Spatrick }
1086f6c50668Spatrick if (!poppedSP) {
1087f6c50668Spatrick return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP,
1088f6c50668Spatrick _UVRSD_UINT32, &sp);
1089f6c50668Spatrick }
1090f6c50668Spatrick return _UVRSR_OK;
1091f6c50668Spatrick }
1092f6c50668Spatrick case _UVRSC_WMMXD:
1093f6c50668Spatrick #if !defined(__ARM_WMMX)
1094f6c50668Spatrick break;
1095f6c50668Spatrick #endif
1096f6c50668Spatrick case _UVRSC_VFP: {
1097f6c50668Spatrick if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
1098f6c50668Spatrick return _UVRSR_FAILED;
1099f6c50668Spatrick uint32_t first = discriminator >> 16;
1100f6c50668Spatrick uint32_t count = discriminator & 0xffff;
1101f6c50668Spatrick uint32_t end = first+count;
1102f6c50668Spatrick uint32_t* sp;
1103f6c50668Spatrick if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
1104f6c50668Spatrick _UVRSD_UINT32, &sp) != _UVRSR_OK) {
1105f6c50668Spatrick return _UVRSR_FAILED;
1106f6c50668Spatrick }
1107f6c50668Spatrick // For _UVRSD_VFPX, we're assuming the data is stored in FSTMX "standard
1108f6c50668Spatrick // format 1", which is equivalent to FSTMD + a padding word.
1109f6c50668Spatrick for (uint32_t i = first; i < end; ++i) {
1110f6c50668Spatrick // SP is only 32-bit aligned so don't copy 64-bit at a time.
1111f6c50668Spatrick uint64_t w0 = *sp++;
1112f6c50668Spatrick uint64_t w1 = *sp++;
1113f6c50668Spatrick #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1114f6c50668Spatrick uint64_t value = (w1 << 32) | w0;
1115f6c50668Spatrick #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1116f6c50668Spatrick uint64_t value = (w0 << 32) | w1;
1117f6c50668Spatrick #else
1118f6c50668Spatrick #error "Unable to determine endianess"
1119f6c50668Spatrick #endif
1120f6c50668Spatrick if (_Unwind_VRS_Set(context, regclass, i, representation, &value) !=
1121f6c50668Spatrick _UVRSR_OK)
1122f6c50668Spatrick return _UVRSR_FAILED;
1123f6c50668Spatrick }
1124f6c50668Spatrick if (representation == _UVRSD_VFPX)
1125f6c50668Spatrick ++sp;
1126f6c50668Spatrick return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
1127f6c50668Spatrick &sp);
1128f6c50668Spatrick }
1129*0faf1914Srobert case _UVRSC_PSEUDO: {
1130*0faf1914Srobert if (representation != _UVRSD_UINT32 || discriminator != 0)
1131*0faf1914Srobert return _UVRSR_FAILED;
1132*0faf1914Srobert // Return Address Authentication code (PAC) - discriminator 0
1133*0faf1914Srobert uint32_t *sp;
1134*0faf1914Srobert if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
1135*0faf1914Srobert &sp) != _UVRSR_OK) {
1136*0faf1914Srobert return _UVRSR_FAILED;
1137*0faf1914Srobert }
1138*0faf1914Srobert uint32_t pac = *sp++;
1139*0faf1914Srobert _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
1140*0faf1914Srobert return _Unwind_VRS_Set(context, _UVRSC_PSEUDO, 0, _UVRSD_UINT32, &pac);
1141*0faf1914Srobert }
1142f6c50668Spatrick }
1143f6c50668Spatrick _LIBUNWIND_ABORT("unsupported register class");
1144f6c50668Spatrick }
1145f6c50668Spatrick
1146*0faf1914Srobert /// Not used by C++.
1147*0faf1914Srobert /// Unwinds stack, calling "stop" function at each frame.
1148*0faf1914Srobert /// Could be used to implement longjmp().
1149*0faf1914Srobert _LIBUNWIND_EXPORT _Unwind_Reason_Code
_Unwind_ForcedUnwind(_Unwind_Exception * exception_object,_Unwind_Stop_Fn stop,void * stop_parameter)1150*0faf1914Srobert _Unwind_ForcedUnwind(_Unwind_Exception *exception_object, _Unwind_Stop_Fn stop,
1151*0faf1914Srobert void *stop_parameter) {
1152*0faf1914Srobert _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
1153*0faf1914Srobert (void *)exception_object, (void *)(uintptr_t)stop);
1154*0faf1914Srobert unw_context_t uc;
1155*0faf1914Srobert unw_cursor_t cursor;
1156*0faf1914Srobert __unw_getcontext(&uc);
1157*0faf1914Srobert
1158*0faf1914Srobert // Mark that this is a forced unwind, so _Unwind_Resume() can do
1159*0faf1914Srobert // the right thing.
1160*0faf1914Srobert exception_object->unwinder_cache.reserved1 = (uintptr_t)stop;
1161*0faf1914Srobert exception_object->unwinder_cache.reserved3 = (uintptr_t)stop_parameter;
1162*0faf1914Srobert
1163*0faf1914Srobert return unwind_phase2_forced(&uc, &cursor, exception_object, stop,
1164*0faf1914Srobert stop_parameter);
1165*0faf1914Srobert }
1166*0faf1914Srobert
1167f6c50668Spatrick /// Called by personality handler during phase 2 to find the start of the
1168f6c50668Spatrick /// function.
1169f6c50668Spatrick _LIBUNWIND_EXPORT uintptr_t
_Unwind_GetRegionStart(struct _Unwind_Context * context)1170f6c50668Spatrick _Unwind_GetRegionStart(struct _Unwind_Context *context) {
1171f6c50668Spatrick unw_cursor_t *cursor = (unw_cursor_t *)context;
1172f6c50668Spatrick unw_proc_info_t frameInfo;
1173f6c50668Spatrick uintptr_t result = 0;
1174f6c50668Spatrick if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
1175f6c50668Spatrick result = (uintptr_t)frameInfo.start_ip;
1176f6c50668Spatrick _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%llX",
1177f6c50668Spatrick static_cast<void *>(context), (long long)result);
1178f6c50668Spatrick return result;
1179f6c50668Spatrick }
1180f6c50668Spatrick
1181f6c50668Spatrick
1182f6c50668Spatrick /// Called by personality handler during phase 2 if a foreign exception
1183f6c50668Spatrick // is caught.
1184f6c50668Spatrick _LIBUNWIND_EXPORT void
_Unwind_DeleteException(_Unwind_Exception * exception_object)1185f6c50668Spatrick _Unwind_DeleteException(_Unwind_Exception *exception_object) {
1186f6c50668Spatrick _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
1187f6c50668Spatrick static_cast<void *>(exception_object));
1188f6c50668Spatrick if (exception_object->exception_cleanup != NULL)
1189f6c50668Spatrick (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
1190f6c50668Spatrick exception_object);
1191f6c50668Spatrick }
1192f6c50668Spatrick
1193f6c50668Spatrick extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
__gnu_unwind_frame(_Unwind_Exception * exception_object,struct _Unwind_Context * context)1194f6c50668Spatrick __gnu_unwind_frame(_Unwind_Exception *exception_object,
1195f6c50668Spatrick struct _Unwind_Context *context) {
1196*0faf1914Srobert (void)exception_object;
1197f6c50668Spatrick unw_cursor_t *cursor = (unw_cursor_t *)context;
1198*0faf1914Srobert switch (__unw_step(cursor)) {
1199*0faf1914Srobert case UNW_STEP_SUCCESS:
1200f6c50668Spatrick return _URC_OK;
1201*0faf1914Srobert case UNW_STEP_END:
1202*0faf1914Srobert return _URC_END_OF_STACK;
1203*0faf1914Srobert default:
1204*0faf1914Srobert return _URC_FAILURE;
1205*0faf1914Srobert }
1206f6c50668Spatrick }
1207f6c50668Spatrick
1208f6c50668Spatrick #endif // defined(_LIBUNWIND_ARM_EHABI)
1209