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