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