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