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