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   while (offset < len && !finish) {
265     uint8_t byte = getByte(data, offset++);
266     if ((byte & 0x80) == 0) {
267       uint32_t sp;
268       _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
269       if (byte & 0x40)
270         sp -= (((uint32_t)byte & 0x3f) << 2) + 4;
271       else
272         sp += ((uint32_t)byte << 2) + 4;
273       _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32, &sp);
274     } else {
275       switch (byte & 0xf0) {
276         case 0x80: {
277           if (offset >= len)
278             return _URC_FAILURE;
279           uint32_t registers =
280               (((uint32_t)byte & 0x0f) << 12) |
281               (((uint32_t)getByte(data, offset++)) << 4);
282           if (!registers)
283             return _URC_FAILURE;
284           if (registers & (1 << 15))
285             wrotePC = true;
286           _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
287           break;
288         }
289         case 0x90: {
290           uint8_t reg = byte & 0x0f;
291           if (reg == 13 || reg == 15)
292             return _URC_FAILURE;
293           uint32_t sp;
294           _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_R0 + reg,
295                           _UVRSD_UINT32, &sp);
296           _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
297                           &sp);
298           break;
299         }
300         case 0xa0: {
301           uint32_t registers = RegisterMask(4, byte & 0x07);
302           if (byte & 0x08)
303             registers |= 1 << 14;
304           _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
305           break;
306         }
307         case 0xb0: {
308           switch (byte) {
309             case 0xb0:
310               finish = true;
311               break;
312             case 0xb1: {
313               if (offset >= len)
314                 return _URC_FAILURE;
315               uint8_t registers = getByte(data, offset++);
316               if (registers & 0xf0 || !registers)
317                 return _URC_FAILURE;
318               _Unwind_VRS_Pop(context, _UVRSC_CORE, registers, _UVRSD_UINT32);
319               break;
320             }
321             case 0xb2: {
322               uint32_t addend = 0;
323               uint32_t shift = 0;
324               // This decodes a uleb128 value.
325               while (true) {
326                 if (offset >= len)
327                   return _URC_FAILURE;
328                 uint32_t v = getByte(data, offset++);
329                 addend |= (v & 0x7f) << shift;
330                 if ((v & 0x80) == 0)
331                   break;
332                 shift += 7;
333               }
334               uint32_t sp;
335               _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
336                               &sp);
337               sp += 0x204 + (addend << 2);
338               _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
339                               &sp);
340               break;
341             }
342             case 0xb3: {
343               uint8_t v = getByte(data, offset++);
344               _Unwind_VRS_Pop(context, _UVRSC_VFP,
345                               RegisterRange(static_cast<uint8_t>(v >> 4),
346                                             v & 0x0f), _UVRSD_VFPX);
347               break;
348             }
349             case 0xb4:
350             case 0xb5:
351             case 0xb6:
352             case 0xb7:
353               return _URC_FAILURE;
354             default:
355               _Unwind_VRS_Pop(context, _UVRSC_VFP,
356                               RegisterRange(8, byte & 0x07), _UVRSD_VFPX);
357               break;
358           }
359           break;
360         }
361         case 0xc0: {
362           switch (byte) {
363 #if defined(__ARM_WMMX)
364             case 0xc0:
365             case 0xc1:
366             case 0xc2:
367             case 0xc3:
368             case 0xc4:
369             case 0xc5:
370               _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
371                               RegisterRange(10, byte & 0x7), _UVRSD_DOUBLE);
372               break;
373             case 0xc6: {
374               uint8_t v = getByte(data, offset++);
375               uint8_t start = static_cast<uint8_t>(v >> 4);
376               uint8_t count_minus_one = v & 0xf;
377               if (start + count_minus_one >= 16)
378                 return _URC_FAILURE;
379               _Unwind_VRS_Pop(context, _UVRSC_WMMXD,
380                               RegisterRange(start, count_minus_one),
381                               _UVRSD_DOUBLE);
382               break;
383             }
384             case 0xc7: {
385               uint8_t v = getByte(data, offset++);
386               if (!v || v & 0xf0)
387                 return _URC_FAILURE;
388               _Unwind_VRS_Pop(context, _UVRSC_WMMXC, v, _UVRSD_DOUBLE);
389               break;
390             }
391 #endif
392             case 0xc8:
393             case 0xc9: {
394               uint8_t v = getByte(data, offset++);
395               uint8_t start =
396                   static_cast<uint8_t>(((byte == 0xc8) ? 16 : 0) + (v >> 4));
397               uint8_t count_minus_one = v & 0xf;
398               if (start + count_minus_one >= 32)
399                 return _URC_FAILURE;
400               _Unwind_VRS_Pop(context, _UVRSC_VFP,
401                               RegisterRange(start, count_minus_one),
402                               _UVRSD_DOUBLE);
403               break;
404             }
405             default:
406               return _URC_FAILURE;
407           }
408           break;
409         }
410         case 0xd0: {
411           if (byte & 0x08)
412             return _URC_FAILURE;
413           _Unwind_VRS_Pop(context, _UVRSC_VFP, RegisterRange(8, byte & 0x7),
414                           _UVRSD_DOUBLE);
415           break;
416         }
417         default:
418           return _URC_FAILURE;
419       }
420     }
421   }
422   if (!wrotePC) {
423     uint32_t lr;
424     _Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_LR, _UVRSD_UINT32, &lr);
425     _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_IP, _UVRSD_UINT32, &lr);
426   }
427   return _URC_CONTINUE_UNWIND;
428 }
429 
430 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
431 __aeabi_unwind_cpp_pr0(_Unwind_State state, _Unwind_Control_Block *ucbp,
432                        _Unwind_Context *context) {
433   return unwindOneFrame(state, ucbp, context);
434 }
435 
436 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
437 __aeabi_unwind_cpp_pr1(_Unwind_State state, _Unwind_Control_Block *ucbp,
438                        _Unwind_Context *context) {
439   return unwindOneFrame(state, ucbp, context);
440 }
441 
442 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
443 __aeabi_unwind_cpp_pr2(_Unwind_State state, _Unwind_Control_Block *ucbp,
444                        _Unwind_Context *context) {
445   return unwindOneFrame(state, ucbp, context);
446 }
447 
448 static _Unwind_Reason_Code
449 unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
450   // EHABI #7.3 discusses preserving the VRS in a "temporary VRS" during
451   // phase 1 and then restoring it to the "primary VRS" for phase 2. The
452   // effect is phase 2 doesn't see any of the VRS manipulations from phase 1.
453   // In this implementation, the phases don't share the VRS backing store.
454   // Instead, they are passed the original |uc| and they create a new VRS
455   // from scratch thus achieving the same effect.
456   __unw_init_local(cursor, uc);
457 
458   // Walk each frame looking for a place to stop.
459   for (bool handlerNotFound = true; handlerNotFound;) {
460 
461     // See if frame has code to run (has personality routine).
462     unw_proc_info_t frameInfo;
463     if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
464       _LIBUNWIND_TRACE_UNWINDING(
465           "unwind_phase1(ex_ojb=%p): __unw_get_proc_info "
466           "failed => _URC_FATAL_PHASE1_ERROR",
467           static_cast<void *>(exception_object));
468       return _URC_FATAL_PHASE1_ERROR;
469     }
470 
471 #ifndef NDEBUG
472     // When tracing, print state information.
473     if (_LIBUNWIND_TRACING_UNWINDING) {
474       char functionBuf[512];
475       const char *functionName = functionBuf;
476       unw_word_t offset;
477       if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
478                                &offset) != UNW_ESUCCESS) ||
479           (frameInfo.start_ip + offset > frameInfo.end_ip))
480         functionName = ".anonymous.";
481       unw_word_t pc;
482       __unw_get_reg(cursor, UNW_REG_IP, &pc);
483       _LIBUNWIND_TRACE_UNWINDING(
484           "unwind_phase1(ex_ojb=%p): pc=0x%" PRIxPTR ", start_ip=0x%" PRIxPTR ", func=%s, "
485           "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
486           static_cast<void *>(exception_object), pc,
487           frameInfo.start_ip, functionName,
488           frameInfo.lsda, frameInfo.handler);
489     }
490 #endif
491 
492     // If there is a personality routine, ask it if it will want to stop at
493     // this frame.
494     if (frameInfo.handler != 0) {
495       _Unwind_Personality_Fn p =
496           (_Unwind_Personality_Fn)(long)(frameInfo.handler);
497       _LIBUNWIND_TRACE_UNWINDING(
498           "unwind_phase1(ex_ojb=%p): calling personality function %p",
499           static_cast<void *>(exception_object),
500           reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(p)));
501       struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
502       exception_object->pr_cache.fnstart = frameInfo.start_ip;
503       exception_object->pr_cache.ehtp =
504           (_Unwind_EHT_Header *)frameInfo.unwind_info;
505       exception_object->pr_cache.additional = frameInfo.flags;
506       _Unwind_Reason_Code personalityResult =
507           (*p)(_US_VIRTUAL_UNWIND_FRAME, exception_object, context);
508       _LIBUNWIND_TRACE_UNWINDING(
509           "unwind_phase1(ex_ojb=%p): personality result %d start_ip %x ehtp %p "
510           "additional %x",
511           static_cast<void *>(exception_object), personalityResult,
512           exception_object->pr_cache.fnstart,
513           static_cast<void *>(exception_object->pr_cache.ehtp),
514           exception_object->pr_cache.additional);
515       switch (personalityResult) {
516       case _URC_HANDLER_FOUND:
517         // found a catch clause or locals that need destructing in this frame
518         // stop search and remember stack pointer at the frame
519         handlerNotFound = false;
520         // p should have initialized barrier_cache. EHABI #7.3.5
521         _LIBUNWIND_TRACE_UNWINDING(
522             "unwind_phase1(ex_ojb=%p): _URC_HANDLER_FOUND",
523             static_cast<void *>(exception_object));
524         return _URC_NO_REASON;
525 
526       case _URC_CONTINUE_UNWIND:
527         _LIBUNWIND_TRACE_UNWINDING(
528             "unwind_phase1(ex_ojb=%p): _URC_CONTINUE_UNWIND",
529             static_cast<void *>(exception_object));
530         // continue unwinding
531         break;
532 
533       // EHABI #7.3.3
534       case _URC_FAILURE:
535         return _URC_FAILURE;
536 
537       default:
538         // something went wrong
539         _LIBUNWIND_TRACE_UNWINDING(
540             "unwind_phase1(ex_ojb=%p): _URC_FATAL_PHASE1_ERROR",
541             static_cast<void *>(exception_object));
542         return _URC_FATAL_PHASE1_ERROR;
543       }
544     }
545   }
546   return _URC_NO_REASON;
547 }
548 
549 static _Unwind_Reason_Code unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor,
550                                          _Unwind_Exception *exception_object,
551                                          bool resume) {
552   // See comment at the start of unwind_phase1 regarding VRS integrity.
553   __unw_init_local(cursor, uc);
554 
555   _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p)",
556                              static_cast<void *>(exception_object));
557   int frame_count = 0;
558 
559   // Walk each frame until we reach where search phase said to stop.
560   while (true) {
561     // Ask libunwind to get next frame (skip over first which is
562     // _Unwind_RaiseException or _Unwind_Resume).
563     //
564     // Resume only ever makes sense for 1 frame.
565     _Unwind_State state =
566         resume ? _US_UNWIND_FRAME_RESUME : _US_UNWIND_FRAME_STARTING;
567     if (resume && frame_count == 1) {
568       // On a resume, first unwind the _Unwind_Resume() frame. The next frame
569       // is now the landing pad for the cleanup from a previous execution of
570       // phase2. To continue unwindingly correctly, replace VRS[15] with the
571       // IP of the frame that the previous run of phase2 installed the context
572       // for. After this, continue unwinding as if normal.
573       //
574       // See #7.4.6 for details.
575       __unw_set_reg(cursor, UNW_REG_IP,
576                     exception_object->unwinder_cache.reserved2);
577       resume = false;
578     }
579 
580     // Get info about this frame.
581     unw_word_t sp;
582     unw_proc_info_t frameInfo;
583     __unw_get_reg(cursor, UNW_REG_SP, &sp);
584     if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
585       _LIBUNWIND_TRACE_UNWINDING(
586           "unwind_phase2(ex_ojb=%p): __unw_get_proc_info "
587           "failed => _URC_FATAL_PHASE2_ERROR",
588           static_cast<void *>(exception_object));
589       return _URC_FATAL_PHASE2_ERROR;
590     }
591 
592 #ifndef NDEBUG
593     // When tracing, print state information.
594     if (_LIBUNWIND_TRACING_UNWINDING) {
595       char functionBuf[512];
596       const char *functionName = functionBuf;
597       unw_word_t offset;
598       if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
599                                &offset) != UNW_ESUCCESS) ||
600           (frameInfo.start_ip + offset > frameInfo.end_ip))
601         functionName = ".anonymous.";
602       _LIBUNWIND_TRACE_UNWINDING(
603           "unwind_phase2(ex_ojb=%p): start_ip=0x%" PRIxPTR ", func=%s, sp=0x%" PRIxPTR ", "
604           "lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR "",
605           static_cast<void *>(exception_object), frameInfo.start_ip,
606           functionName, sp, frameInfo.lsda,
607           frameInfo.handler);
608     }
609 #endif
610 
611     // If there is a personality routine, tell it we are unwinding.
612     if (frameInfo.handler != 0) {
613       _Unwind_Personality_Fn p =
614           (_Unwind_Personality_Fn)(intptr_t)(frameInfo.handler);
615       struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
616       // EHABI #7.2
617       exception_object->pr_cache.fnstart = frameInfo.start_ip;
618       exception_object->pr_cache.ehtp =
619           (_Unwind_EHT_Header *)frameInfo.unwind_info;
620       exception_object->pr_cache.additional = frameInfo.flags;
621       _Unwind_Reason_Code personalityResult =
622           (*p)(state, exception_object, context);
623       switch (personalityResult) {
624       case _URC_CONTINUE_UNWIND:
625         // Continue unwinding
626         _LIBUNWIND_TRACE_UNWINDING(
627             "unwind_phase2(ex_ojb=%p): _URC_CONTINUE_UNWIND",
628             static_cast<void *>(exception_object));
629         // EHABI #7.2
630         if (sp == exception_object->barrier_cache.sp) {
631           // Phase 1 said we would stop at this frame, but we did not...
632           _LIBUNWIND_ABORT("during phase1 personality function said it would "
633                            "stop here, but now in phase2 it did not stop here");
634         }
635         break;
636       case _URC_INSTALL_CONTEXT:
637         _LIBUNWIND_TRACE_UNWINDING(
638             "unwind_phase2(ex_ojb=%p): _URC_INSTALL_CONTEXT",
639             static_cast<void *>(exception_object));
640         // Personality routine says to transfer control to landing pad.
641         // We may get control back if landing pad calls _Unwind_Resume().
642         if (_LIBUNWIND_TRACING_UNWINDING) {
643           unw_word_t pc;
644           __unw_get_reg(cursor, UNW_REG_IP, &pc);
645           __unw_get_reg(cursor, UNW_REG_SP, &sp);
646           _LIBUNWIND_TRACE_UNWINDING("unwind_phase2(ex_ojb=%p): re-entering "
647                                      "user code with ip=0x%" PRIxPTR ", sp=0x%" PRIxPTR,
648                                      static_cast<void *>(exception_object),
649                                      pc, sp);
650         }
651 
652         {
653           // EHABI #7.4.1 says we need to preserve pc for when _Unwind_Resume
654           // is called back, to find this same frame.
655           unw_word_t pc;
656           __unw_get_reg(cursor, UNW_REG_IP, &pc);
657           exception_object->unwinder_cache.reserved2 = (uint32_t)pc;
658         }
659         __unw_resume(cursor);
660         // __unw_resume() only returns if there was an error.
661         return _URC_FATAL_PHASE2_ERROR;
662 
663       // # EHABI #7.4.3
664       case _URC_FAILURE:
665         abort();
666 
667       default:
668         // Personality routine returned an unknown result code.
669         _LIBUNWIND_DEBUG_LOG("personality function returned unknown result %d",
670                       personalityResult);
671         return _URC_FATAL_PHASE2_ERROR;
672       }
673     }
674     frame_count++;
675   }
676 
677   // Clean up phase did not resume at the frame that the search phase
678   // said it would...
679   return _URC_FATAL_PHASE2_ERROR;
680 }
681 
682 static _Unwind_Reason_Code
683 unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
684                      _Unwind_Exception *exception_object, _Unwind_Stop_Fn stop,
685                      void *stop_parameter) {
686   bool endOfStack = false;
687   // See comment at the start of unwind_phase1 regarding VRS integrity.
688   __unw_init_local(cursor, uc);
689   _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_force(ex_ojb=%p)",
690                              static_cast<void *>(exception_object));
691   // Walk each frame until we reach where search phase said to stop
692   while (!endOfStack) {
693     // Update info about this frame.
694     unw_proc_info_t frameInfo;
695     if (__unw_get_proc_info(cursor, &frameInfo) != UNW_ESUCCESS) {
696       _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): __unw_step "
697                                  "failed => _URC_END_OF_STACK",
698                                  (void *)exception_object);
699       return _URC_FATAL_PHASE2_ERROR;
700     }
701 
702 #ifndef NDEBUG
703     // When tracing, print state information.
704     if (_LIBUNWIND_TRACING_UNWINDING) {
705       char functionBuf[512];
706       const char *functionName = functionBuf;
707       unw_word_t offset;
708       if ((__unw_get_proc_name(cursor, functionBuf, sizeof(functionBuf),
709                                &offset) != UNW_ESUCCESS) ||
710           (frameInfo.start_ip + offset > frameInfo.end_ip))
711         functionName = ".anonymous.";
712       _LIBUNWIND_TRACE_UNWINDING(
713           "unwind_phase2_forced(ex_ojb=%p): start_ip=0x%" PRIxPTR
714           ", func=%s, lsda=0x%" PRIxPTR ", personality=0x%" PRIxPTR,
715           (void *)exception_object, frameInfo.start_ip, functionName,
716           frameInfo.lsda, frameInfo.handler);
717     }
718 #endif
719 
720     // Call stop function at each frame.
721     _Unwind_Action action =
722         (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE);
723     _Unwind_Reason_Code stopResult =
724         (*stop)(1, action, exception_object->exception_class, exception_object,
725                 (_Unwind_Context *)(cursor), stop_parameter);
726     _LIBUNWIND_TRACE_UNWINDING(
727         "unwind_phase2_forced(ex_ojb=%p): stop function returned %d",
728         (void *)exception_object, stopResult);
729     if (stopResult != _URC_NO_REASON) {
730       _LIBUNWIND_TRACE_UNWINDING(
731           "unwind_phase2_forced(ex_ojb=%p): stopped by stop function",
732           (void *)exception_object);
733       return _URC_FATAL_PHASE2_ERROR;
734     }
735 
736     // If there is a personality routine, tell it we are unwinding.
737     if (frameInfo.handler != 0) {
738       _Unwind_Personality_Fn p =
739           (_Unwind_Personality_Fn)(uintptr_t)(frameInfo.handler);
740       struct _Unwind_Context *context = (struct _Unwind_Context *)(cursor);
741       // EHABI #7.2
742       exception_object->pr_cache.fnstart = frameInfo.start_ip;
743       exception_object->pr_cache.ehtp =
744           (_Unwind_EHT_Header *)frameInfo.unwind_info;
745       exception_object->pr_cache.additional = frameInfo.flags;
746       _Unwind_Reason_Code personalityResult =
747           (*p)(_US_FORCE_UNWIND | _US_UNWIND_FRAME_STARTING, exception_object,
748                context);
749       switch (personalityResult) {
750       case _URC_CONTINUE_UNWIND:
751         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
752                                    "personality returned "
753                                    "_URC_CONTINUE_UNWIND",
754                                    (void *)exception_object);
755         // Destructors called, continue unwinding
756         break;
757       case _URC_INSTALL_CONTEXT:
758         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
759                                    "personality returned "
760                                    "_URC_INSTALL_CONTEXT",
761                                    (void *)exception_object);
762         // We may get control back if landing pad calls _Unwind_Resume().
763         __unw_resume(cursor);
764         break;
765       case _URC_END_OF_STACK:
766         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
767                                    "personality returned "
768                                    "_URC_END_OF_STACK",
769                                    (void *)exception_object);
770         // Personalty routine did the step and it can't step forward.
771         endOfStack = true;
772         break;
773       default:
774         // Personality routine returned an unknown result code.
775         _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): "
776                                    "personality returned %d, "
777                                    "_URC_FATAL_PHASE2_ERROR",
778                                    (void *)exception_object, personalityResult);
779         return _URC_FATAL_PHASE2_ERROR;
780       }
781     }
782   }
783 
784   // Call stop function one last time and tell it we've reached the end
785   // of the stack.
786   _LIBUNWIND_TRACE_UNWINDING("unwind_phase2_forced(ex_ojb=%p): calling stop "
787                              "function with _UA_END_OF_STACK",
788                              (void *)exception_object);
789   _Unwind_Action lastAction =
790       (_Unwind_Action)(_UA_FORCE_UNWIND | _UA_CLEANUP_PHASE | _UA_END_OF_STACK);
791   (*stop)(1, lastAction, exception_object->exception_class, exception_object,
792           (struct _Unwind_Context *)(cursor), stop_parameter);
793 
794   // Clean up phase did not resume at the frame that the search phase said it
795   // would.
796   return _URC_FATAL_PHASE2_ERROR;
797 }
798 
799 /// Called by __cxa_throw.  Only returns if there is a fatal error.
800 _LIBUNWIND_EXPORT _Unwind_Reason_Code
801 _Unwind_RaiseException(_Unwind_Exception *exception_object) {
802   _LIBUNWIND_TRACE_API("_Unwind_RaiseException(ex_obj=%p)",
803                        static_cast<void *>(exception_object));
804   unw_context_t uc;
805   unw_cursor_t cursor;
806   __unw_getcontext(&uc);
807 
808   // This field for is for compatibility with GCC to say this isn't a forced
809   // unwind. EHABI #7.2
810   exception_object->unwinder_cache.reserved1 = 0;
811 
812   // phase 1: the search phase
813   _Unwind_Reason_Code phase1 = unwind_phase1(&uc, &cursor, exception_object);
814   if (phase1 != _URC_NO_REASON)
815     return phase1;
816 
817   // phase 2: the clean up phase
818   return unwind_phase2(&uc, &cursor, exception_object, false);
819 }
820 
821 _LIBUNWIND_EXPORT void _Unwind_Complete(_Unwind_Exception* exception_object) {
822   // This is to be called when exception handling completes to give us a chance
823   // to perform any housekeeping. EHABI #7.2. But we have nothing to do here.
824   (void)exception_object;
825 }
826 
827 /// When _Unwind_RaiseException() is in phase2, it hands control
828 /// to the personality function at each frame.  The personality
829 /// may force a jump to a landing pad in that function, the landing
830 /// pad code may then call _Unwind_Resume() to continue with the
831 /// unwinding.  Note: the call to _Unwind_Resume() is from compiler
832 /// geneated user code.  All other _Unwind_* routines are called
833 /// by the C++ runtime __cxa_* routines.
834 ///
835 /// Note: re-throwing an exception (as opposed to continuing the unwind)
836 /// is implemented by having the code call __cxa_rethrow() which
837 /// in turn calls _Unwind_Resume_or_Rethrow().
838 _LIBUNWIND_EXPORT void
839 _Unwind_Resume(_Unwind_Exception *exception_object) {
840   _LIBUNWIND_TRACE_API("_Unwind_Resume(ex_obj=%p)",
841                        static_cast<void *>(exception_object));
842   unw_context_t uc;
843   unw_cursor_t cursor;
844   __unw_getcontext(&uc);
845 
846   if (exception_object->unwinder_cache.reserved1)
847     unwind_phase2_forced(
848         &uc, &cursor, exception_object,
849         (_Unwind_Stop_Fn)exception_object->unwinder_cache.reserved1,
850         (void *)exception_object->unwinder_cache.reserved3);
851   else
852     unwind_phase2(&uc, &cursor, exception_object, true);
853 
854   // Clients assume _Unwind_Resume() does not return, so all we can do is abort.
855   _LIBUNWIND_ABORT("_Unwind_Resume() can't return");
856 }
857 
858 /// Called by personality handler during phase 2 to get LSDA for current frame.
859 _LIBUNWIND_EXPORT uintptr_t
860 _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context) {
861   unw_cursor_t *cursor = (unw_cursor_t *)context;
862   unw_proc_info_t frameInfo;
863   uintptr_t result = 0;
864   if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
865     result = (uintptr_t)frameInfo.lsda;
866   _LIBUNWIND_TRACE_API(
867       "_Unwind_GetLanguageSpecificData(context=%p) => 0x%llx",
868       static_cast<void *>(context), (long long)result);
869   return result;
870 }
871 
872 static uint64_t ValueAsBitPattern(_Unwind_VRS_DataRepresentation representation,
873                                   void* valuep) {
874   uint64_t value = 0;
875   switch (representation) {
876     case _UVRSD_UINT32:
877     case _UVRSD_FLOAT:
878       memcpy(&value, valuep, sizeof(uint32_t));
879       break;
880 
881     case _UVRSD_VFPX:
882     case _UVRSD_UINT64:
883     case _UVRSD_DOUBLE:
884       memcpy(&value, valuep, sizeof(uint64_t));
885       break;
886   }
887   return value;
888 }
889 
890 _LIBUNWIND_EXPORT _Unwind_VRS_Result
891 _Unwind_VRS_Set(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
892                 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
893                 void *valuep) {
894   _LIBUNWIND_TRACE_API("_Unwind_VRS_Set(context=%p, regclass=%d, reg=%d, "
895                        "rep=%d, value=0x%llX)",
896                        static_cast<void *>(context), regclass, regno,
897                        representation,
898                        ValueAsBitPattern(representation, valuep));
899   unw_cursor_t *cursor = (unw_cursor_t *)context;
900   switch (regclass) {
901     case _UVRSC_CORE:
902       if (representation != _UVRSD_UINT32 || regno > 15)
903         return _UVRSR_FAILED;
904       return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
905                            *(unw_word_t *)valuep) == UNW_ESUCCESS
906                  ? _UVRSR_OK
907                  : _UVRSR_FAILED;
908     case _UVRSC_VFP:
909       if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
910         return _UVRSR_FAILED;
911       if (representation == _UVRSD_VFPX) {
912         // Can only touch d0-15 with FSTMFDX.
913         if (regno > 15)
914           return _UVRSR_FAILED;
915         __unw_save_vfp_as_X(cursor);
916       } else {
917         if (regno > 31)
918           return _UVRSR_FAILED;
919       }
920       return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
921                              *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
922                  ? _UVRSR_OK
923                  : _UVRSR_FAILED;
924 #if defined(__ARM_WMMX)
925     case _UVRSC_WMMXC:
926       if (representation != _UVRSD_UINT32 || regno > 3)
927         return _UVRSR_FAILED;
928       return __unw_set_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
929                            *(unw_word_t *)valuep) == UNW_ESUCCESS
930                  ? _UVRSR_OK
931                  : _UVRSR_FAILED;
932     case _UVRSC_WMMXD:
933       if (representation != _UVRSD_DOUBLE || regno > 31)
934         return _UVRSR_FAILED;
935       return __unw_set_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
936                              *(unw_fpreg_t *)valuep) == UNW_ESUCCESS
937                  ? _UVRSR_OK
938                  : _UVRSR_FAILED;
939 #else
940     case _UVRSC_WMMXC:
941     case _UVRSC_WMMXD:
942       break;
943 #endif
944   }
945   _LIBUNWIND_ABORT("unsupported register class");
946 }
947 
948 static _Unwind_VRS_Result
949 _Unwind_VRS_Get_Internal(_Unwind_Context *context,
950                          _Unwind_VRS_RegClass regclass, uint32_t regno,
951                          _Unwind_VRS_DataRepresentation representation,
952                          void *valuep) {
953   unw_cursor_t *cursor = (unw_cursor_t *)context;
954   switch (regclass) {
955     case _UVRSC_CORE:
956       if (representation != _UVRSD_UINT32 || regno > 15)
957         return _UVRSR_FAILED;
958       return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_R0 + regno),
959                            (unw_word_t *)valuep) == UNW_ESUCCESS
960                  ? _UVRSR_OK
961                  : _UVRSR_FAILED;
962     case _UVRSC_VFP:
963       if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
964         return _UVRSR_FAILED;
965       if (representation == _UVRSD_VFPX) {
966         // Can only touch d0-15 with FSTMFDX.
967         if (regno > 15)
968           return _UVRSR_FAILED;
969         __unw_save_vfp_as_X(cursor);
970       } else {
971         if (regno > 31)
972           return _UVRSR_FAILED;
973       }
974       return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_D0 + regno),
975                              (unw_fpreg_t *)valuep) == UNW_ESUCCESS
976                  ? _UVRSR_OK
977                  : _UVRSR_FAILED;
978 #if defined(__ARM_WMMX)
979     case _UVRSC_WMMXC:
980       if (representation != _UVRSD_UINT32 || regno > 3)
981         return _UVRSR_FAILED;
982       return __unw_get_reg(cursor, (unw_regnum_t)(UNW_ARM_WC0 + regno),
983                            (unw_word_t *)valuep) == UNW_ESUCCESS
984                  ? _UVRSR_OK
985                  : _UVRSR_FAILED;
986     case _UVRSC_WMMXD:
987       if (representation != _UVRSD_DOUBLE || regno > 31)
988         return _UVRSR_FAILED;
989       return __unw_get_fpreg(cursor, (unw_regnum_t)(UNW_ARM_WR0 + regno),
990                              (unw_fpreg_t *)valuep) == UNW_ESUCCESS
991                  ? _UVRSR_OK
992                  : _UVRSR_FAILED;
993 #else
994     case _UVRSC_WMMXC:
995     case _UVRSC_WMMXD:
996       break;
997 #endif
998   }
999   _LIBUNWIND_ABORT("unsupported register class");
1000 }
1001 
1002 _LIBUNWIND_EXPORT _Unwind_VRS_Result
1003 _Unwind_VRS_Get(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
1004                 uint32_t regno, _Unwind_VRS_DataRepresentation representation,
1005                 void *valuep) {
1006   _Unwind_VRS_Result result =
1007       _Unwind_VRS_Get_Internal(context, regclass, regno, representation,
1008                                valuep);
1009   _LIBUNWIND_TRACE_API("_Unwind_VRS_Get(context=%p, regclass=%d, reg=%d, "
1010                        "rep=%d, value=0x%llX, result = %d)",
1011                        static_cast<void *>(context), regclass, regno,
1012                        representation,
1013                        ValueAsBitPattern(representation, valuep), result);
1014   return result;
1015 }
1016 
1017 _Unwind_VRS_Result
1018 _Unwind_VRS_Pop(_Unwind_Context *context, _Unwind_VRS_RegClass regclass,
1019                 uint32_t discriminator,
1020                 _Unwind_VRS_DataRepresentation representation) {
1021   _LIBUNWIND_TRACE_API("_Unwind_VRS_Pop(context=%p, regclass=%d, "
1022                        "discriminator=%d, representation=%d)",
1023                        static_cast<void *>(context), regclass, discriminator,
1024                        representation);
1025   switch (regclass) {
1026     case _UVRSC_WMMXC:
1027 #if !defined(__ARM_WMMX)
1028       break;
1029 #endif
1030     case _UVRSC_CORE: {
1031       if (representation != _UVRSD_UINT32)
1032         return _UVRSR_FAILED;
1033       // When popping SP from the stack, we don't want to override it from the
1034       // computed new stack location. See EHABI #7.5.4 table 3.
1035       bool poppedSP = false;
1036       uint32_t* sp;
1037       if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
1038                           _UVRSD_UINT32, &sp) != _UVRSR_OK) {
1039         return _UVRSR_FAILED;
1040       }
1041       for (uint32_t i = 0; i < 16; ++i) {
1042         if (!(discriminator & static_cast<uint32_t>(1 << i)))
1043           continue;
1044         uint32_t value = *sp++;
1045         if (regclass == _UVRSC_CORE && i == 13)
1046           poppedSP = true;
1047         if (_Unwind_VRS_Set(context, regclass, i,
1048                             _UVRSD_UINT32, &value) != _UVRSR_OK) {
1049           return _UVRSR_FAILED;
1050         }
1051       }
1052       if (!poppedSP) {
1053         return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP,
1054                                _UVRSD_UINT32, &sp);
1055       }
1056       return _UVRSR_OK;
1057     }
1058     case _UVRSC_WMMXD:
1059 #if !defined(__ARM_WMMX)
1060       break;
1061 #endif
1062     case _UVRSC_VFP: {
1063       if (representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
1064         return _UVRSR_FAILED;
1065       uint32_t first = discriminator >> 16;
1066       uint32_t count = discriminator & 0xffff;
1067       uint32_t end = first+count;
1068       uint32_t* sp;
1069       if (_Unwind_VRS_Get(context, _UVRSC_CORE, UNW_ARM_SP,
1070                           _UVRSD_UINT32, &sp) != _UVRSR_OK) {
1071         return _UVRSR_FAILED;
1072       }
1073       // For _UVRSD_VFPX, we're assuming the data is stored in FSTMX "standard
1074       // format 1", which is equivalent to FSTMD + a padding word.
1075       for (uint32_t i = first; i < end; ++i) {
1076         // SP is only 32-bit aligned so don't copy 64-bit at a time.
1077         uint64_t w0 = *sp++;
1078         uint64_t w1 = *sp++;
1079 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1080         uint64_t value = (w1 << 32) | w0;
1081 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1082         uint64_t value = (w0 << 32) | w1;
1083 #else
1084 #error "Unable to determine endianess"
1085 #endif
1086         if (_Unwind_VRS_Set(context, regclass, i, representation, &value) !=
1087             _UVRSR_OK)
1088           return _UVRSR_FAILED;
1089       }
1090       if (representation == _UVRSD_VFPX)
1091         ++sp;
1092       return _Unwind_VRS_Set(context, _UVRSC_CORE, UNW_ARM_SP, _UVRSD_UINT32,
1093                              &sp);
1094     }
1095   }
1096   _LIBUNWIND_ABORT("unsupported register class");
1097 }
1098 
1099 /// Not used by C++.
1100 /// Unwinds stack, calling "stop" function at each frame.
1101 /// Could be used to implement longjmp().
1102 _LIBUNWIND_EXPORT _Unwind_Reason_Code
1103 _Unwind_ForcedUnwind(_Unwind_Exception *exception_object, _Unwind_Stop_Fn stop,
1104                      void *stop_parameter) {
1105   _LIBUNWIND_TRACE_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)",
1106                        (void *)exception_object, (void *)(uintptr_t)stop);
1107   unw_context_t uc;
1108   unw_cursor_t cursor;
1109   __unw_getcontext(&uc);
1110 
1111   // Mark that this is a forced unwind, so _Unwind_Resume() can do
1112   // the right thing.
1113   exception_object->unwinder_cache.reserved1 = (uintptr_t)stop;
1114   exception_object->unwinder_cache.reserved3 = (uintptr_t)stop_parameter;
1115 
1116   return unwind_phase2_forced(&uc, &cursor, exception_object, stop,
1117                               stop_parameter);
1118 }
1119 
1120 /// Called by personality handler during phase 2 to find the start of the
1121 /// function.
1122 _LIBUNWIND_EXPORT uintptr_t
1123 _Unwind_GetRegionStart(struct _Unwind_Context *context) {
1124   unw_cursor_t *cursor = (unw_cursor_t *)context;
1125   unw_proc_info_t frameInfo;
1126   uintptr_t result = 0;
1127   if (__unw_get_proc_info(cursor, &frameInfo) == UNW_ESUCCESS)
1128     result = (uintptr_t)frameInfo.start_ip;
1129   _LIBUNWIND_TRACE_API("_Unwind_GetRegionStart(context=%p) => 0x%llX",
1130                        static_cast<void *>(context), (long long)result);
1131   return result;
1132 }
1133 
1134 
1135 /// Called by personality handler during phase 2 if a foreign exception
1136 // is caught.
1137 _LIBUNWIND_EXPORT void
1138 _Unwind_DeleteException(_Unwind_Exception *exception_object) {
1139   _LIBUNWIND_TRACE_API("_Unwind_DeleteException(ex_obj=%p)",
1140                        static_cast<void *>(exception_object));
1141   if (exception_object->exception_cleanup != NULL)
1142     (*exception_object->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT,
1143                                            exception_object);
1144 }
1145 
1146 extern "C" _LIBUNWIND_EXPORT _Unwind_Reason_Code
1147 __gnu_unwind_frame(_Unwind_Exception *exception_object,
1148                    struct _Unwind_Context *context) {
1149   unw_cursor_t *cursor = (unw_cursor_t *)context;
1150   switch (__unw_step(cursor)) {
1151   case UNW_STEP_SUCCESS:
1152     return _URC_OK;
1153   case UNW_STEP_END:
1154     return _URC_END_OF_STACK;
1155   default:
1156     return _URC_FAILURE;
1157   }
1158 }
1159 
1160 #endif  // defined(_LIBUNWIND_ARM_EHABI)
1161