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