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