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