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