Home | History | Annotate | Download | only in src
      1 //===------------------------- cxa_exception.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 //  This file implements the "Exception Handling APIs"
     10 //  http://mentorembedded.github.io/cxx-abi/abi-eh.html
     11 //  http://www.intel.com/design/itanium/downloads/245358.htm
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include <assert.h>
     16 #include <stdlib.h>
     17 #include <string.h>
     18 #include <typeinfo>
     19 
     20 #include "__cxxabi_config.h"
     21 #include "config.h"
     22 #include "cxa_exception.hpp"
     23 #include "cxa_handlers.hpp"
     24 #include "private_typeinfo.h"
     25 #include "unwind.h"
     26 
     27 /*
     28     Exception Header Layout:
     29 
     30 +---------------------------+-----------------------------+---------------+
     31 | __cxa_exception           | _Unwind_Exception CLNGC++\0 | thrown object |
     32 +---------------------------+-----------------------------+---------------+
     33                                                           ^
     34                                                           |
     35   +-------------------------------------------------------+
     36   |
     37 +---------------------------+-----------------------------+
     38 | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |
     39 +---------------------------+-----------------------------+
     40 
     41     Exception Handling Table Layout:
     42 
     43 +-----------------+--------+
     44 | lpStartEncoding | (char) |
     45 +---------+-------+--------+---------------+-----------------------+
     46 | lpStart | (encoded with lpStartEncoding) | defaults to funcStart |
     47 +---------+-----+--------+-----------------+---------------+-------+
     48 | ttypeEncoding | (char) | Encoding of the type_info table |
     49 +---------------+-+------+----+----------------------------+----------------+
     50 | classInfoOffset | (ULEB128) | Offset to type_info table, defaults to null |
     51 +-----------------++--------+-+----------------------------+----------------+
     52 | callSiteEncoding | (char) | Encoding for Call Site Table |
     53 +------------------+--+-----+-----+------------------------+--------------------------+
     54 | callSiteTableLength | (ULEB128) | Call Site Table length, used to find Action table |
     55 +---------------------+-----------+---------------------------------------------------+
     56 #ifndef __USING_SJLJ_EXCEPTIONS__
     57 +---------------------+-----------+------------------------------------------------+
     58 | Beginning of Call Site Table            The current ip lies within the           |
     59 | ...                                     (start, length) range of one of these    |
     60 |                                         call sites. There may be action needed.  |
     61 | +-------------+---------------------------------+------------------------------+ |
     62 | | start       | (encoded with callSiteEncoding) | offset relative to funcStart | |
     63 | | length      | (encoded with callSiteEncoding) | length of code fragment      | |
     64 | | landingPad  | (encoded with callSiteEncoding) | offset relative to lpStart   | |
     65 | | actionEntry | (ULEB128)                       | Action Table Index 1-based   | |
     66 | |             |                                 | actionEntry == 0 -> cleanup  | |
     67 | +-------------+---------------------------------+------------------------------+ |
     68 | ...                                                                              |
     69 +----------------------------------------------------------------------------------+
     70 #else  // __USING_SJLJ_EXCEPTIONS__
     71 +---------------------+-----------+------------------------------------------------+
     72 | Beginning of Call Site Table            The current ip is a 1-based index into   |
     73 | ...                                     this table.  Or it is -1 meaning no      |
     74 |                                         action is needed.  Or it is 0 meaning    |
     75 |                                         terminate.                               |
     76 | +-------------+---------------------------------+------------------------------+ |
     77 | | landingPad  | (ULEB128)                       | offset relative to lpStart   | |
     78 | | actionEntry | (ULEB128)                       | Action Table Index 1-based   | |
     79 | |             |                                 | actionEntry == 0 -> cleanup  | |
     80 | +-------------+---------------------------------+------------------------------+ |
     81 | ...                                                                              |
     82 +----------------------------------------------------------------------------------+
     83 #endif  // __USING_SJLJ_EXCEPTIONS__
     84 +---------------------------------------------------------------------+
     85 | Beginning of Action Table       ttypeIndex == 0 : cleanup           |
     86 | ...                             ttypeIndex  > 0 : catch             |
     87 |                                 ttypeIndex  < 0 : exception spec    |
     88 | +--------------+-----------+--------------------------------------+ |
     89 | | ttypeIndex   | (SLEB128) | Index into type_info Table (1-based) | |
     90 | | actionOffset | (SLEB128) | Offset into next Action Table entry  | |
     91 | +--------------+-----------+--------------------------------------+ |
     92 | ...                                                                 |
     93 +---------------------------------------------------------------------+-----------------+
     94 | type_info Table, but classInfoOffset does *not* point here!                           |
     95 | +----------------+------------------------------------------------+-----------------+ |
     96 | | Nth type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == N | |
     97 | +----------------+------------------------------------------------+-----------------+ |
     98 | ...                                                                                   |
     99 | +----------------+------------------------------------------------+-----------------+ |
    100 | | 1st type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == 1 | |
    101 | +----------------+------------------------------------------------+-----------------+ |
    102 | +---------------------------------------+-----------+------------------------------+  |
    103 | | 1st ttypeIndex for 1st exception spec | (ULEB128) | classInfoOffset points here! |  |
    104 | | ...                                   | (ULEB128) |                              |  |
    105 | | Mth ttypeIndex for 1st exception spec | (ULEB128) |                              |  |
    106 | | 0                                     | (ULEB128) |                              |  |
    107 | +---------------------------------------+------------------------------------------+  |
    108 | ...                                                                                   |
    109 | +---------------------------------------+------------------------------------------+  |
    110 | | 0                                     | (ULEB128) | throw()                      |  |
    111 | +---------------------------------------+------------------------------------------+  |
    112 | ...                                                                                   |
    113 | +---------------------------------------+------------------------------------------+  |
    114 | | 1st ttypeIndex for Nth exception spec | (ULEB128) |                              |  |
    115 | | ...                                   | (ULEB128) |                              |  |
    116 | | Mth ttypeIndex for Nth exception spec | (ULEB128) |                              |  |
    117 | | 0                                     | (ULEB128) |                              |  |
    118 | +---------------------------------------+------------------------------------------+  |
    119 +---------------------------------------------------------------------------------------+
    120 
    121 Notes:
    122 
    123 *  ttypeIndex in the Action Table, and in the exception spec table, is an index,
    124      not a byte count, if positive.  It is a negative index offset of
    125      classInfoOffset and the sizeof entry depends on ttypeEncoding.
    126    But if ttypeIndex is negative, it is a positive 1-based byte offset into the
    127      type_info Table.
    128    And if ttypeIndex is zero, it refers to a catch (...).
    129 
    130 *  landingPad can be 0, this implies there is nothing to be done.
    131 
    132 *  landingPad != 0 and actionEntry == 0 implies a cleanup needs to be done
    133      @landingPad.
    134 
    135 *  A cleanup can also be found under landingPad != 0 and actionEntry != 0 in
    136      the Action Table with ttypeIndex == 0.
    137 */
    138 
    139 namespace __cxxabiv1
    140 {
    141 
    142 namespace
    143 {
    144 
    145 template <class AsType>
    146 uintptr_t readPointerHelper(const uint8_t*& p) {
    147     AsType value;
    148     memcpy(&value, p, sizeof(AsType));
    149     p += sizeof(AsType);
    150     return static_cast<uintptr_t>(value);
    151 }
    152 
    153 } // end namespace
    154 
    155 extern "C"
    156 {
    157 
    158 // private API
    159 
    160 // Heavily borrowed from llvm/examples/ExceptionDemo/ExceptionDemo.cpp
    161 
    162 // DWARF Constants
    163 enum
    164 {
    165     DW_EH_PE_absptr   = 0x00,
    166     DW_EH_PE_uleb128  = 0x01,
    167     DW_EH_PE_udata2   = 0x02,
    168     DW_EH_PE_udata4   = 0x03,
    169     DW_EH_PE_udata8   = 0x04,
    170     DW_EH_PE_sleb128  = 0x09,
    171     DW_EH_PE_sdata2   = 0x0A,
    172     DW_EH_PE_sdata4   = 0x0B,
    173     DW_EH_PE_sdata8   = 0x0C,
    174     DW_EH_PE_pcrel    = 0x10,
    175     DW_EH_PE_textrel  = 0x20,
    176     DW_EH_PE_datarel  = 0x30,
    177     DW_EH_PE_funcrel  = 0x40,
    178     DW_EH_PE_aligned  = 0x50,
    179     DW_EH_PE_indirect = 0x80,
    180     DW_EH_PE_omit     = 0xFF
    181 };
    182 
    183 /// Read a uleb128 encoded value and advance pointer
    184 /// See Variable Length Data Appendix C in:
    185 /// @link http://dwarfstd.org/Dwarf4.pdf @unlink
    186 /// @param data reference variable holding memory pointer to decode from
    187 /// @returns decoded value
    188 static
    189 uintptr_t
    190 readULEB128(const uint8_t** data)
    191 {
    192     uintptr_t result = 0;
    193     uintptr_t shift = 0;
    194     unsigned char byte;
    195     const uint8_t *p = *data;
    196     do
    197     {
    198         byte = *p++;
    199         result |= static_cast<uintptr_t>(byte & 0x7F) << shift;
    200         shift += 7;
    201     } while (byte & 0x80);
    202     *data = p;
    203     return result;
    204 }
    205 
    206 /// Read a sleb128 encoded value and advance pointer
    207 /// See Variable Length Data Appendix C in:
    208 /// @link http://dwarfstd.org/Dwarf4.pdf @unlink
    209 /// @param data reference variable holding memory pointer to decode from
    210 /// @returns decoded value
    211 static
    212 intptr_t
    213 readSLEB128(const uint8_t** data)
    214 {
    215     uintptr_t result = 0;
    216     uintptr_t shift = 0;
    217     unsigned char byte;
    218     const uint8_t *p = *data;
    219     do
    220     {
    221         byte = *p++;
    222         result |= static_cast<uintptr_t>(byte & 0x7F) << shift;
    223         shift += 7;
    224     } while (byte & 0x80);
    225     *data = p;
    226     if ((byte & 0x40) && (shift < (sizeof(result) << 3)))
    227         result |= static_cast<uintptr_t>(~0) << shift;
    228     return static_cast<intptr_t>(result);
    229 }
    230 
    231 /// Read a pointer encoded value and advance pointer
    232 /// See Variable Length Data in:
    233 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
    234 /// @param data reference variable holding memory pointer to decode from
    235 /// @param encoding dwarf encoding type
    236 /// @returns decoded value
    237 static
    238 uintptr_t
    239 readEncodedPointer(const uint8_t** data, uint8_t encoding)
    240 {
    241     uintptr_t result = 0;
    242     if (encoding == DW_EH_PE_omit)
    243         return result;
    244     const uint8_t* p = *data;
    245     // first get value
    246     switch (encoding & 0x0F)
    247     {
    248     case DW_EH_PE_absptr:
    249         result = readPointerHelper<uintptr_t>(p);
    250         break;
    251     case DW_EH_PE_uleb128:
    252         result = readULEB128(&p);
    253         break;
    254     case DW_EH_PE_sleb128:
    255         result = static_cast<uintptr_t>(readSLEB128(&p));
    256         break;
    257     case DW_EH_PE_udata2:
    258         result = readPointerHelper<uint16_t>(p);
    259         break;
    260     case DW_EH_PE_udata4:
    261         result = readPointerHelper<uint32_t>(p);
    262         break;
    263     case DW_EH_PE_udata8:
    264         result = readPointerHelper<uint64_t>(p);
    265         break;
    266     case DW_EH_PE_sdata2:
    267         result = readPointerHelper<int16_t>(p);
    268         break;
    269     case DW_EH_PE_sdata4:
    270         result = readPointerHelper<int32_t>(p);
    271         break;
    272     case DW_EH_PE_sdata8:
    273         result = readPointerHelper<int64_t>(p);
    274         break;
    275     default:
    276         // not supported
    277         abort();
    278         break;
    279     }
    280     // then add relative offset
    281     switch (encoding & 0x70)
    282     {
    283     case DW_EH_PE_absptr:
    284         // do nothing
    285         break;
    286     case DW_EH_PE_pcrel:
    287         if (result)
    288             result += (uintptr_t)(*data);
    289         break;
    290     case DW_EH_PE_textrel:
    291     case DW_EH_PE_datarel:
    292     case DW_EH_PE_funcrel:
    293     case DW_EH_PE_aligned:
    294     default:
    295         // not supported
    296         abort();
    297         break;
    298     }
    299     // then apply indirection
    300     if (result && (encoding & DW_EH_PE_indirect))
    301         result = *((uintptr_t*)result);
    302     *data = p;
    303     return result;
    304 }
    305 
    306 static
    307 void
    308 call_terminate(bool native_exception, _Unwind_Exception* unwind_exception)
    309 {
    310     __cxa_begin_catch(unwind_exception);
    311     if (native_exception)
    312     {
    313         // Use the stored terminate_handler if possible
    314         __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
    315         std::__terminate(exception_header->terminateHandler);
    316     }
    317     std::terminate();
    318 }
    319 
    320 #if LIBCXXABI_ARM_EHABI
    321 static const void* read_target2_value(const void* ptr)
    322 {
    323     uintptr_t offset = *reinterpret_cast<const uintptr_t*>(ptr);
    324     if (!offset)
    325         return 0;
    326     // "ARM EABI provides a TARGET2 relocation to describe these typeinfo
    327     // pointers. The reason being it allows their precise semantics to be
    328     // deferred to the linker. For bare-metal they turn into absolute
    329     // relocations. For linux they turn into GOT-REL relocations."
    330     // https://gcc.gnu.org/ml/gcc-patches/2009-08/msg00264.html
    331 #if LIBCXXABI_BAREMETAL
    332     return reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(ptr) +
    333                                          offset);
    334 #else
    335     return *reinterpret_cast<const void **>(reinterpret_cast<uintptr_t>(ptr) +
    336                                             offset);
    337 #endif
    338 }
    339 
    340 static const __shim_type_info*
    341 get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo,
    342                    uint8_t ttypeEncoding, bool native_exception,
    343                    _Unwind_Exception* unwind_exception)
    344 {
    345     if (classInfo == 0)
    346     {
    347         // this should not happen.  Indicates corrupted eh_table.
    348         call_terminate(native_exception, unwind_exception);
    349     }
    350 
    351     assert(((ttypeEncoding == DW_EH_PE_absptr) ||  // LLVM or GCC 4.6
    352             (ttypeEncoding == DW_EH_PE_pcrel) ||  // GCC 4.7 baremetal
    353             (ttypeEncoding == (DW_EH_PE_pcrel | DW_EH_PE_indirect))) &&  // GCC 4.7 linux
    354            "Unexpected TTypeEncoding");
    355     (void)ttypeEncoding;
    356 
    357     const uint8_t* ttypePtr = classInfo - ttypeIndex * sizeof(uintptr_t);
    358     return reinterpret_cast<const __shim_type_info *>(
    359         read_target2_value(ttypePtr));
    360 }
    361 #else // !LIBCXXABI_ARM_EHABI
    362 static
    363 const __shim_type_info*
    364 get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo,
    365                    uint8_t ttypeEncoding, bool native_exception,
    366                    _Unwind_Exception* unwind_exception)
    367 {
    368     if (classInfo == 0)
    369     {
    370         // this should not happen.  Indicates corrupted eh_table.
    371         call_terminate(native_exception, unwind_exception);
    372     }
    373     switch (ttypeEncoding & 0x0F)
    374     {
    375     case DW_EH_PE_absptr:
    376         ttypeIndex *= sizeof(void*);
    377         break;
    378     case DW_EH_PE_udata2:
    379     case DW_EH_PE_sdata2:
    380         ttypeIndex *= 2;
    381         break;
    382     case DW_EH_PE_udata4:
    383     case DW_EH_PE_sdata4:
    384         ttypeIndex *= 4;
    385         break;
    386     case DW_EH_PE_udata8:
    387     case DW_EH_PE_sdata8:
    388         ttypeIndex *= 8;
    389         break;
    390     default:
    391         // this should not happen.   Indicates corrupted eh_table.
    392         call_terminate(native_exception, unwind_exception);
    393     }
    394     classInfo -= ttypeIndex;
    395     return (const __shim_type_info*)readEncodedPointer(&classInfo, ttypeEncoding);
    396 }
    397 #endif // !LIBCXXABI_ARM_EHABI
    398 
    399 /*
    400     This is checking a thrown exception type, excpType, against a possibly empty
    401     list of catchType's which make up an exception spec.
    402 
    403     An exception spec acts like a catch handler, but in reverse.  This "catch
    404     handler" will catch an excpType if and only if none of the catchType's in
    405     the list will catch a excpType.  If any catchType in the list can catch an
    406     excpType, then this exception spec does not catch the excpType.
    407 */
    408 #if LIBCXXABI_ARM_EHABI
    409 static
    410 bool
    411 exception_spec_can_catch(int64_t specIndex, const uint8_t* classInfo,
    412                          uint8_t ttypeEncoding, const __shim_type_info* excpType,
    413                          void* adjustedPtr, _Unwind_Exception* unwind_exception)
    414 {
    415     if (classInfo == 0)
    416     {
    417         // this should not happen.   Indicates corrupted eh_table.
    418         call_terminate(false, unwind_exception);
    419     }
    420 
    421     assert(((ttypeEncoding == DW_EH_PE_absptr) ||  // LLVM or GCC 4.6
    422             (ttypeEncoding == DW_EH_PE_pcrel) ||  // GCC 4.7 baremetal
    423             (ttypeEncoding == (DW_EH_PE_pcrel | DW_EH_PE_indirect))) &&  // GCC 4.7 linux
    424            "Unexpected TTypeEncoding");
    425     (void)ttypeEncoding;
    426 
    427     // specIndex is negative of 1-based byte offset into classInfo;
    428     specIndex = -specIndex;
    429     --specIndex;
    430     const void** temp = reinterpret_cast<const void**>(
    431         reinterpret_cast<uintptr_t>(classInfo) +
    432         static_cast<uintptr_t>(specIndex) * sizeof(uintptr_t));
    433     // If any type in the spec list can catch excpType, return false, else return true
    434     //    adjustments to adjustedPtr are ignored.
    435     while (true)
    436     {
    437         // ARM EHABI exception specification table (filter table) consists of
    438         // several pointers which will directly point to the type info object
    439         // (instead of ttypeIndex).  The table will be terminated with 0.
    440         const void** ttypePtr = temp++;
    441         if (*ttypePtr == 0)
    442             break;
    443         // We can get the __shim_type_info simply by performing a
    444         // R_ARM_TARGET2 relocation, and cast the result to __shim_type_info.
    445         const __shim_type_info* catchType =
    446             static_cast<const __shim_type_info*>(read_target2_value(ttypePtr));
    447         void* tempPtr = adjustedPtr;
    448         if (catchType->can_catch(excpType, tempPtr))
    449             return false;
    450     }
    451     return true;
    452 }
    453 #else
    454 static
    455 bool
    456 exception_spec_can_catch(int64_t specIndex, const uint8_t* classInfo,
    457                          uint8_t ttypeEncoding, const __shim_type_info* excpType,
    458                          void* adjustedPtr, _Unwind_Exception* unwind_exception)
    459 {
    460     if (classInfo == 0)
    461     {
    462         // this should not happen.   Indicates corrupted eh_table.
    463         call_terminate(false, unwind_exception);
    464     }
    465     // specIndex is negative of 1-based byte offset into classInfo;
    466     specIndex = -specIndex;
    467     --specIndex;
    468     const uint8_t* temp = classInfo + specIndex;
    469     // If any type in the spec list can catch excpType, return false, else return true
    470     //    adjustments to adjustedPtr are ignored.
    471     while (true)
    472     {
    473         uint64_t ttypeIndex = readULEB128(&temp);
    474         if (ttypeIndex == 0)
    475             break;
    476         const __shim_type_info* catchType = get_shim_type_info(ttypeIndex,
    477                                                                classInfo,
    478                                                                ttypeEncoding,
    479                                                                true,
    480                                                                unwind_exception);
    481         void* tempPtr = adjustedPtr;
    482         if (catchType->can_catch(excpType, tempPtr))
    483             return false;
    484     }
    485     return true;
    486 }
    487 #endif
    488 
    489 static
    490 void*
    491 get_thrown_object_ptr(_Unwind_Exception* unwind_exception)
    492 {
    493     // Even for foreign exceptions, the exception object is *probably* at unwind_exception + 1
    494     //    Regardless, this library is prohibited from touching a foreign exception
    495     void* adjustedPtr = unwind_exception + 1;
    496     if (unwind_exception->exception_class == kOurDependentExceptionClass)
    497         adjustedPtr = ((__cxa_dependent_exception*)adjustedPtr - 1)->primaryException;
    498     return adjustedPtr;
    499 }
    500 
    501 namespace
    502 {
    503 
    504 struct scan_results
    505 {
    506     int64_t        ttypeIndex;   // > 0 catch handler, < 0 exception spec handler, == 0 a cleanup
    507     const uint8_t* actionRecord;         // Currently unused.  Retained to ease future maintenance.
    508     const uint8_t* languageSpecificData;  // Needed only for __cxa_call_unexpected
    509     uintptr_t      landingPad;   // null -> nothing found, else something found
    510     void*          adjustedPtr;  // Used in cxa_exception.cpp
    511     _Unwind_Reason_Code reason;  // One of _URC_FATAL_PHASE1_ERROR,
    512                                  //        _URC_FATAL_PHASE2_ERROR,
    513                                  //        _URC_CONTINUE_UNWIND,
    514                                  //        _URC_HANDLER_FOUND
    515 };
    516 
    517 }  // unnamed namespace
    518 
    519 static
    520 void
    521 set_registers(_Unwind_Exception* unwind_exception, _Unwind_Context* context,
    522               const scan_results& results)
    523 {
    524 #if defined(__USING_SJLJ_EXCEPTIONS__)
    525 #define __builtin_eh_return_data_regno(regno) regno
    526 #endif
    527   _Unwind_SetGR(context, __builtin_eh_return_data_regno(0),
    528                 reinterpret_cast<uintptr_t>(unwind_exception));
    529   _Unwind_SetGR(context, __builtin_eh_return_data_regno(1),
    530                 static_cast<uintptr_t>(results.ttypeIndex));
    531   _Unwind_SetIP(context, results.landingPad);
    532 }
    533 
    534 /*
    535     There are 3 types of scans needed:
    536 
    537     1.  Scan for handler with native or foreign exception.  If handler found,
    538         save state and return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND.
    539         May also report an error on invalid input.
    540         May terminate for invalid exception table.
    541         _UA_SEARCH_PHASE
    542 
    543     2.  Scan for handler with foreign exception.  Must return _URC_HANDLER_FOUND,
    544         or call terminate.
    545         _UA_CLEANUP_PHASE && _UA_HANDLER_FRAME && !native_exception
    546 
    547     3.  Scan for cleanups.  If a handler is found and this isn't forced unwind,
    548         then terminate, otherwise ignore the handler and keep looking for cleanup.
    549         If a cleanup is found, return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND.
    550         May also report an error on invalid input.
    551         May terminate for invalid exception table.
    552         _UA_CLEANUP_PHASE && !_UA_HANDLER_FRAME
    553 */
    554 
    555 static void scan_eh_tab(scan_results &results, _Unwind_Action actions,
    556                         bool native_exception,
    557                         _Unwind_Exception *unwind_exception,
    558                         _Unwind_Context *context) {
    559     // Initialize results to found nothing but an error
    560     results.ttypeIndex = 0;
    561     results.actionRecord = 0;
    562     results.languageSpecificData = 0;
    563     results.landingPad = 0;
    564     results.adjustedPtr = 0;
    565     results.reason = _URC_FATAL_PHASE1_ERROR;
    566     // Check for consistent actions
    567     if (actions & _UA_SEARCH_PHASE)
    568     {
    569         // Do Phase 1
    570         if (actions & (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME | _UA_FORCE_UNWIND))
    571         {
    572             // None of these flags should be set during Phase 1
    573             //   Client error
    574             results.reason = _URC_FATAL_PHASE1_ERROR;
    575             return;
    576         }
    577     }
    578     else if (actions & _UA_CLEANUP_PHASE)
    579     {
    580         if ((actions & _UA_HANDLER_FRAME) && (actions & _UA_FORCE_UNWIND))
    581         {
    582             // _UA_HANDLER_FRAME should only be set if phase 1 found a handler.
    583             // If _UA_FORCE_UNWIND is set, phase 1 shouldn't have happened.
    584             //    Client error
    585             results.reason = _URC_FATAL_PHASE2_ERROR;
    586             return;
    587         }
    588     }
    589     else // Neither _UA_SEARCH_PHASE nor _UA_CLEANUP_PHASE is set
    590     {
    591         // One of these should be set.
    592         //   Client error
    593         results.reason = _URC_FATAL_PHASE1_ERROR;
    594         return;
    595     }
    596     // Start scan by getting exception table address
    597     const uint8_t *lsda = (const uint8_t *)_Unwind_GetLanguageSpecificData(context);
    598     if (lsda == 0)
    599     {
    600         // There is no exception table
    601         results.reason = _URC_CONTINUE_UNWIND;
    602         return;
    603     }
    604     results.languageSpecificData = lsda;
    605     // Get the current instruction pointer and offset it before next
    606     // instruction in the current frame which threw the exception.
    607     uintptr_t ip = _Unwind_GetIP(context) - 1;
    608     // Get beginning current frame's code (as defined by the
    609     // emitted dwarf code)
    610     uintptr_t funcStart = _Unwind_GetRegionStart(context);
    611 #ifdef __USING_SJLJ_EXCEPTIONS__
    612     if (ip == uintptr_t(-1))
    613     {
    614         // no action
    615         results.reason = _URC_CONTINUE_UNWIND;
    616         return;
    617     }
    618     else if (ip == 0)
    619         call_terminate(native_exception, unwind_exception);
    620     // ip is 1-based index into call site table
    621 #else  // !__USING_SJLJ_EXCEPTIONS__
    622     uintptr_t ipOffset = ip - funcStart;
    623 #endif  // !defined(_USING_SLJL_EXCEPTIONS__)
    624     const uint8_t* classInfo = NULL;
    625     // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding
    626     //       dwarf emission
    627     // Parse LSDA header.
    628     uint8_t lpStartEncoding = *lsda++;
    629     const uint8_t* lpStart = (const uint8_t*)readEncodedPointer(&lsda, lpStartEncoding);
    630     if (lpStart == 0)
    631         lpStart = (const uint8_t*)funcStart;
    632     uint8_t ttypeEncoding = *lsda++;
    633     if (ttypeEncoding != DW_EH_PE_omit)
    634     {
    635         // Calculate type info locations in emitted dwarf code which
    636         // were flagged by type info arguments to llvm.eh.selector
    637         // intrinsic
    638         uintptr_t classInfoOffset = readULEB128(&lsda);
    639         classInfo = lsda + classInfoOffset;
    640     }
    641     // Walk call-site table looking for range that
    642     // includes current PC.
    643     uint8_t callSiteEncoding = *lsda++;
    644 #ifdef __USING_SJLJ_EXCEPTIONS__
    645     (void)callSiteEncoding;  // When using SjLj exceptions, callSiteEncoding is never used
    646 #endif
    647     uint32_t callSiteTableLength = static_cast<uint32_t>(readULEB128(&lsda));
    648     const uint8_t* callSiteTableStart = lsda;
    649     const uint8_t* callSiteTableEnd = callSiteTableStart + callSiteTableLength;
    650     const uint8_t* actionTableStart = callSiteTableEnd;
    651     const uint8_t* callSitePtr = callSiteTableStart;
    652     while (callSitePtr < callSiteTableEnd)
    653     {
    654         // There is one entry per call site.
    655 #ifndef __USING_SJLJ_EXCEPTIONS__
    656         // The call sites are non-overlapping in [start, start+length)
    657         // The call sites are ordered in increasing value of start
    658         uintptr_t start = readEncodedPointer(&callSitePtr, callSiteEncoding);
    659         uintptr_t length = readEncodedPointer(&callSitePtr, callSiteEncoding);
    660         uintptr_t landingPad = readEncodedPointer(&callSitePtr, callSiteEncoding);
    661         uintptr_t actionEntry = readULEB128(&callSitePtr);
    662         if ((start <= ipOffset) && (ipOffset < (start + length)))
    663 #else  // __USING_SJLJ_EXCEPTIONS__
    664         // ip is 1-based index into this table
    665         uintptr_t landingPad = readULEB128(&callSitePtr);
    666         uintptr_t actionEntry = readULEB128(&callSitePtr);
    667         if (--ip == 0)
    668 #endif  // __USING_SJLJ_EXCEPTIONS__
    669         {
    670             // Found the call site containing ip.
    671 #ifndef __USING_SJLJ_EXCEPTIONS__
    672             if (landingPad == 0)
    673             {
    674                 // No handler here
    675                 results.reason = _URC_CONTINUE_UNWIND;
    676                 return;
    677             }
    678             landingPad = (uintptr_t)lpStart + landingPad;
    679 #else  // __USING_SJLJ_EXCEPTIONS__
    680             ++landingPad;
    681 #endif  // __USING_SJLJ_EXCEPTIONS__
    682             if (actionEntry == 0)
    683             {
    684                 // Found a cleanup
    685                 // If this is a type 1 or type 2 search, there are no handlers
    686                 // If this is a type 3 search, you want to install the cleanup.
    687                 if ((actions & _UA_CLEANUP_PHASE) && !(actions & _UA_HANDLER_FRAME))
    688                 {
    689                     results.ttypeIndex = 0;  // Redundant but clarifying
    690                     results.landingPad = landingPad;
    691                     results.reason = _URC_HANDLER_FOUND;
    692                     return;
    693                 }
    694                 // No handler here
    695                 results.reason = _URC_CONTINUE_UNWIND;
    696                 return;
    697             }
    698             // Convert 1-based byte offset into
    699             const uint8_t* action = actionTableStart + (actionEntry - 1);
    700             // Scan action entries until you find a matching handler, cleanup, or the end of action list
    701             while (true)
    702             {
    703                 const uint8_t* actionRecord = action;
    704                 int64_t ttypeIndex = readSLEB128(&action);
    705                 if (ttypeIndex > 0)
    706                 {
    707                     // Found a catch, does it actually catch?
    708                     // First check for catch (...)
    709                     const __shim_type_info* catchType =
    710                         get_shim_type_info(static_cast<uint64_t>(ttypeIndex),
    711                                            classInfo, ttypeEncoding,
    712                                            native_exception, unwind_exception);
    713                     if (catchType == 0)
    714                     {
    715                         // Found catch (...) catches everything, including foreign exceptions
    716                         // If this is a type 1 search save state and return _URC_HANDLER_FOUND
    717                         // If this is a type 2 search save state and return _URC_HANDLER_FOUND
    718                         // If this is a type 3 search !_UA_FORCE_UNWIND, we should have found this in phase 1!
    719                         // If this is a type 3 search _UA_FORCE_UNWIND, ignore handler and continue scan
    720                         if ((actions & _UA_SEARCH_PHASE) || (actions & _UA_HANDLER_FRAME))
    721                         {
    722                             // Save state and return _URC_HANDLER_FOUND
    723                             results.ttypeIndex = ttypeIndex;
    724                             results.actionRecord = actionRecord;
    725                             results.landingPad = landingPad;
    726                             results.adjustedPtr = get_thrown_object_ptr(unwind_exception);
    727                             results.reason = _URC_HANDLER_FOUND;
    728                             return;
    729                         }
    730                         else if (!(actions & _UA_FORCE_UNWIND))
    731                         {
    732                             // It looks like the exception table has changed
    733                             //    on us.  Likely stack corruption!
    734                             call_terminate(native_exception, unwind_exception);
    735                         }
    736                     }
    737                     // Else this is a catch (T) clause and will never
    738                     //    catch a foreign exception
    739                     else if (native_exception)
    740                     {
    741                         __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
    742                         void* adjustedPtr = get_thrown_object_ptr(unwind_exception);
    743                         const __shim_type_info* excpType =
    744                             static_cast<const __shim_type_info*>(exception_header->exceptionType);
    745                         if (adjustedPtr == 0 || excpType == 0)
    746                         {
    747                             // Something very bad happened
    748                             call_terminate(native_exception, unwind_exception);
    749                         }
    750                         if (catchType->can_catch(excpType, adjustedPtr))
    751                         {
    752                             // Found a matching handler
    753                             // If this is a type 1 search save state and return _URC_HANDLER_FOUND
    754                             // If this is a type 3 search and !_UA_FORCE_UNWIND, we should have found this in phase 1!
    755                             // If this is a type 3 search and _UA_FORCE_UNWIND, ignore handler and continue scan
    756                             if (actions & _UA_SEARCH_PHASE)
    757                             {
    758                                 // Save state and return _URC_HANDLER_FOUND
    759                                 results.ttypeIndex = ttypeIndex;
    760                                 results.actionRecord = actionRecord;
    761                                 results.landingPad = landingPad;
    762                                 results.adjustedPtr = adjustedPtr;
    763                                 results.reason = _URC_HANDLER_FOUND;
    764                                 return;
    765                             }
    766                             else if (!(actions & _UA_FORCE_UNWIND))
    767                             {
    768                                 // It looks like the exception table has changed
    769                                 //    on us.  Likely stack corruption!
    770                                 call_terminate(native_exception, unwind_exception);
    771                             }
    772                         }
    773                     }
    774                     // Scan next action ...
    775                 }
    776                 else if (ttypeIndex < 0)
    777                 {
    778                     // Found an exception spec.  If this is a foreign exception,
    779                     //   it is always caught.
    780                     if (native_exception)
    781                     {
    782                         // Does the exception spec catch this native exception?
    783                         __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
    784                         void* adjustedPtr = get_thrown_object_ptr(unwind_exception);
    785                         const __shim_type_info* excpType =
    786                             static_cast<const __shim_type_info*>(exception_header->exceptionType);
    787                         if (adjustedPtr == 0 || excpType == 0)
    788                         {
    789                             // Something very bad happened
    790                             call_terminate(native_exception, unwind_exception);
    791                         }
    792                         if (exception_spec_can_catch(ttypeIndex, classInfo,
    793                                                      ttypeEncoding, excpType,
    794                                                      adjustedPtr, unwind_exception))
    795                         {
    796                             // native exception caught by exception spec
    797                             // If this is a type 1 search, save state and return _URC_HANDLER_FOUND
    798                             // If this is a type 3 search !_UA_FORCE_UNWIND, we should have found this in phase 1!
    799                             // If this is a type 3 search _UA_FORCE_UNWIND, ignore handler and continue scan
    800                             if (actions & _UA_SEARCH_PHASE)
    801                             {
    802                                 // Save state and return _URC_HANDLER_FOUND
    803                                 results.ttypeIndex = ttypeIndex;
    804                                 results.actionRecord = actionRecord;
    805                                 results.landingPad = landingPad;
    806                                 results.adjustedPtr = adjustedPtr;
    807                                 results.reason = _URC_HANDLER_FOUND;
    808                                 return;
    809                             }
    810                             else if (!(actions & _UA_FORCE_UNWIND))
    811                             {
    812                                 // It looks like the exception table has changed
    813                                 //    on us.  Likely stack corruption!
    814                                 call_terminate(native_exception, unwind_exception);
    815                             }
    816                         }
    817                     }
    818                     else
    819                     {
    820                         // foreign exception caught by exception spec
    821                         // If this is a type 1 search, save state and return _URC_HANDLER_FOUND
    822                         // If this is a type 2 search, save state and return _URC_HANDLER_FOUND
    823                         // If this is a type 3 search !_UA_FORCE_UNWIND, we should have found this in phase 1!
    824                         // If this is a type 3 search _UA_FORCE_UNWIND, ignore handler and continue scan
    825                         if ((actions & _UA_SEARCH_PHASE) || (actions & _UA_HANDLER_FRAME))
    826                         {
    827                             // Save state and return _URC_HANDLER_FOUND
    828                             results.ttypeIndex = ttypeIndex;
    829                             results.actionRecord = actionRecord;
    830                             results.landingPad = landingPad;
    831                             results.adjustedPtr = get_thrown_object_ptr(unwind_exception);
    832                             results.reason = _URC_HANDLER_FOUND;
    833                             return;
    834                         }
    835                         else if (!(actions & _UA_FORCE_UNWIND))
    836                         {
    837                             // It looks like the exception table has changed
    838                             //    on us.  Likely stack corruption!
    839                             call_terminate(native_exception, unwind_exception);
    840                         }
    841                     }
    842                     // Scan next action ...
    843                 }
    844                 else  // ttypeIndex == 0
    845                 {
    846                     // Found a cleanup
    847                     // If this is a type 1 search, ignore it and continue scan
    848                     // If this is a type 2 search, ignore it and continue scan
    849                     // If this is a type 3 search, save state and return _URC_HANDLER_FOUND
    850                     if ((actions & _UA_CLEANUP_PHASE) && !(actions & _UA_HANDLER_FRAME))
    851                     {
    852                         // Save state and return _URC_HANDLER_FOUND
    853                         results.ttypeIndex = ttypeIndex;
    854                         results.actionRecord = actionRecord;
    855                         results.landingPad = landingPad;
    856                         results.adjustedPtr = get_thrown_object_ptr(unwind_exception);
    857                         results.reason = _URC_HANDLER_FOUND;
    858                         return;
    859                     }
    860                 }
    861                 const uint8_t* temp = action;
    862                 int64_t actionOffset = readSLEB128(&temp);
    863                 if (actionOffset == 0)
    864                 {
    865                     // End of action list, no matching handler or cleanup found
    866                     results.reason = _URC_CONTINUE_UNWIND;
    867                     return;
    868                 }
    869                 // Go to next action
    870                 action += actionOffset;
    871             }  // there is no break out of this loop, only return
    872         }
    873 #ifndef __USING_SJLJ_EXCEPTIONS__
    874         else if (ipOffset < start)
    875         {
    876             // There is no call site for this ip
    877             // Something bad has happened.  We should never get here.
    878             // Possible stack corruption.
    879             call_terminate(native_exception, unwind_exception);
    880         }
    881 #endif  // !__USING_SJLJ_EXCEPTIONS__
    882     }  // there might be some tricky cases which break out of this loop
    883 
    884     // It is possible that no eh table entry specify how to handle
    885     // this exception. By spec, terminate it immediately.
    886     call_terminate(native_exception, unwind_exception);
    887 }
    888 
    889 // public API
    890 
    891 /*
    892 The personality function branches on actions like so:
    893 
    894 _UA_SEARCH_PHASE
    895 
    896     If _UA_CLEANUP_PHASE or _UA_HANDLER_FRAME or _UA_FORCE_UNWIND there's
    897       an error from above, return _URC_FATAL_PHASE1_ERROR.
    898 
    899     Scan for anything that could stop unwinding:
    900 
    901        1.  A catch clause that will catch this exception
    902            (will never catch foreign).
    903        2.  A catch (...) (will always catch foreign).
    904        3.  An exception spec that will catch this exception
    905            (will always catch foreign).
    906     If a handler is found
    907         If not foreign
    908             Save state in header
    909         return _URC_HANDLER_FOUND
    910     Else a handler not found
    911         return _URC_CONTINUE_UNWIND
    912 
    913 _UA_CLEANUP_PHASE
    914 
    915     If _UA_HANDLER_FRAME
    916         If _UA_FORCE_UNWIND
    917             How did this happen?  return _URC_FATAL_PHASE2_ERROR
    918         If foreign
    919             Do _UA_SEARCH_PHASE to recover state
    920         else
    921             Recover state from header
    922         Transfer control to landing pad.  return _URC_INSTALL_CONTEXT
    923 
    924     Else
    925 
    926         This branch handles both normal C++ non-catching handlers (cleanups)
    927           and forced unwinding.
    928         Scan for anything that can not stop unwinding:
    929 
    930             1.  A cleanup.
    931 
    932         If a cleanup is found
    933             transfer control to it. return _URC_INSTALL_CONTEXT
    934         Else a cleanup is not found: return _URC_CONTINUE_UNWIND
    935 */
    936 
    937 #if !LIBCXXABI_ARM_EHABI
    938 _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code
    939 #ifdef __USING_SJLJ_EXCEPTIONS__
    940 __gxx_personality_sj0
    941 #else
    942 __gxx_personality_v0
    943 #endif
    944                     (int version, _Unwind_Action actions, uint64_t exceptionClass,
    945                      _Unwind_Exception* unwind_exception, _Unwind_Context* context)
    946 {
    947     if (version != 1 || unwind_exception == 0 || context == 0)
    948         return _URC_FATAL_PHASE1_ERROR;
    949 
    950     bool native_exception = (exceptionClass     & get_vendor_and_language) ==
    951                             (kOurExceptionClass & get_vendor_and_language);
    952     scan_results results;
    953     if (actions & _UA_SEARCH_PHASE)
    954     {
    955         // Phase 1 search:  All we're looking for in phase 1 is a handler that
    956         //   halts unwinding
    957         scan_eh_tab(results, actions, native_exception, unwind_exception, context);
    958         if (results.reason == _URC_HANDLER_FOUND)
    959         {
    960             // Found one.  Can we cache the results somewhere to optimize phase 2?
    961             if (native_exception)
    962             {
    963                 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
    964                 exception_header->handlerSwitchValue = static_cast<int>(results.ttypeIndex);
    965                 exception_header->actionRecord = results.actionRecord;
    966                 exception_header->languageSpecificData = results.languageSpecificData;
    967                 exception_header->catchTemp = reinterpret_cast<void*>(results.landingPad);
    968                 exception_header->adjustedPtr = results.adjustedPtr;
    969             }
    970             return _URC_HANDLER_FOUND;
    971         }
    972         // Did not find a catching-handler.  Return the results of the scan
    973         //    (normally _URC_CONTINUE_UNWIND, but could have been _URC_FATAL_PHASE1_ERROR
    974         //     if we were called improperly).
    975         return results.reason;
    976     }
    977     if (actions & _UA_CLEANUP_PHASE)
    978     {
    979         // Phase 2 search:
    980         //  Did we find a catching handler in phase 1?
    981         if (actions & _UA_HANDLER_FRAME)
    982         {
    983             // Yes, phase 1 said we have a catching handler here.
    984             // Did we cache the results of the scan?
    985             if (native_exception)
    986             {
    987                 // Yes, reload the results from the cache.
    988                 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
    989                 results.ttypeIndex = exception_header->handlerSwitchValue;
    990                 results.actionRecord = exception_header->actionRecord;
    991                 results.languageSpecificData = exception_header->languageSpecificData;
    992                 results.landingPad = reinterpret_cast<uintptr_t>(exception_header->catchTemp);
    993                 results.adjustedPtr = exception_header->adjustedPtr;
    994             }
    995             else
    996             {
    997                 // No, do the scan again to reload the results.
    998                 scan_eh_tab(results, actions, native_exception, unwind_exception, context);
    999                 // Phase 1 told us we would find a handler.  Now in Phase 2 we
   1000                 //   didn't find a handler.  The eh table should not be changing!
   1001                 if (results.reason != _URC_HANDLER_FOUND)
   1002                     call_terminate(native_exception, unwind_exception);
   1003             }
   1004             // Jump to the handler
   1005             set_registers(unwind_exception, context, results);
   1006             return _URC_INSTALL_CONTEXT;
   1007         }
   1008         // Either we didn't do a phase 1 search (due to forced unwinding), or
   1009         //   phase 1 reported no catching-handlers.
   1010         // Search for a (non-catching) cleanup
   1011         scan_eh_tab(results, actions, native_exception, unwind_exception, context);
   1012         if (results.reason == _URC_HANDLER_FOUND)
   1013         {
   1014             // Found a non-catching handler.  Jump to it:
   1015             set_registers(unwind_exception, context, results);
   1016             return _URC_INSTALL_CONTEXT;
   1017         }
   1018         // Did not find a cleanup.  Return the results of the scan
   1019         //    (normally _URC_CONTINUE_UNWIND, but could have been _URC_FATAL_PHASE2_ERROR
   1020         //     if we were called improperly).
   1021         return results.reason;
   1022     }
   1023     // We were called improperly: neither a phase 1 or phase 2 search
   1024     return _URC_FATAL_PHASE1_ERROR;
   1025 }
   1026 #else
   1027 
   1028 extern "C" _Unwind_Reason_Code __gnu_unwind_frame(_Unwind_Exception*,
   1029                                                   _Unwind_Context*);
   1030 
   1031 // Helper function to unwind one frame.
   1032 // ARM EHABI 7.3 and 7.4: If the personality function returns _URC_CONTINUE_UNWIND, the
   1033 // personality routine should update the virtual register set (VRS) according to the
   1034 // corresponding frame unwinding instructions (ARM EHABI 9.3.)
   1035 static _Unwind_Reason_Code continue_unwind(_Unwind_Exception* unwind_exception,
   1036                                            _Unwind_Context* context)
   1037 {
   1038     if (__gnu_unwind_frame(unwind_exception, context) != _URC_OK)
   1039         return _URC_FAILURE;
   1040     return _URC_CONTINUE_UNWIND;
   1041 }
   1042 
   1043 // ARM register names
   1044 #if !defined(LIBCXXABI_USE_LLVM_UNWINDER)
   1045 static const uint32_t REG_UCB = 12;  // Register to save _Unwind_Control_Block
   1046 #endif
   1047 static const uint32_t REG_SP = 13;
   1048 
   1049 static void save_results_to_barrier_cache(_Unwind_Exception* unwind_exception,
   1050                                           const scan_results& results)
   1051 {
   1052     unwind_exception->barrier_cache.bitpattern[0] = (uint32_t)results.adjustedPtr;
   1053     unwind_exception->barrier_cache.bitpattern[1] = (uint32_t)results.actionRecord;
   1054     unwind_exception->barrier_cache.bitpattern[2] = (uint32_t)results.languageSpecificData;
   1055     unwind_exception->barrier_cache.bitpattern[3] = (uint32_t)results.landingPad;
   1056     unwind_exception->barrier_cache.bitpattern[4] = (uint32_t)results.ttypeIndex;
   1057 }
   1058 
   1059 static void load_results_from_barrier_cache(scan_results& results,
   1060                                             const _Unwind_Exception* unwind_exception)
   1061 {
   1062     results.adjustedPtr = (void*)unwind_exception->barrier_cache.bitpattern[0];
   1063     results.actionRecord = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[1];
   1064     results.languageSpecificData = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[2];
   1065     results.landingPad = (uintptr_t)unwind_exception->barrier_cache.bitpattern[3];
   1066     results.ttypeIndex = (int64_t)(int32_t)unwind_exception->barrier_cache.bitpattern[4];
   1067 }
   1068 
   1069 extern "C" _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code
   1070 __gxx_personality_v0(_Unwind_State state,
   1071                      _Unwind_Exception* unwind_exception,
   1072                      _Unwind_Context* context)
   1073 {
   1074     if (unwind_exception == 0 || context == 0)
   1075         return _URC_FATAL_PHASE1_ERROR;
   1076 
   1077     bool native_exception = (unwind_exception->exception_class & get_vendor_and_language) ==
   1078                             (kOurExceptionClass & get_vendor_and_language);
   1079 
   1080 #if !defined(LIBCXXABI_USE_LLVM_UNWINDER)
   1081     // Copy the address of _Unwind_Control_Block to r12 so that
   1082     // _Unwind_GetLanguageSpecificData() and _Unwind_GetRegionStart() can
   1083     // return correct address.
   1084     _Unwind_SetGR(context, REG_UCB, reinterpret_cast<uint32_t>(unwind_exception));
   1085 #endif
   1086 
   1087     // Check the undocumented force unwinding behavior
   1088     bool is_force_unwinding = state & _US_FORCE_UNWIND;
   1089     state &= ~_US_FORCE_UNWIND;
   1090 
   1091     scan_results results;
   1092     switch (state) {
   1093     case _US_VIRTUAL_UNWIND_FRAME:
   1094         if (is_force_unwinding)
   1095             return continue_unwind(unwind_exception, context);
   1096 
   1097         // Phase 1 search:  All we're looking for in phase 1 is a handler that halts unwinding
   1098         scan_eh_tab(results, _UA_SEARCH_PHASE, native_exception, unwind_exception, context);
   1099         if (results.reason == _URC_HANDLER_FOUND)
   1100         {
   1101             unwind_exception->barrier_cache.sp = _Unwind_GetGR(context, REG_SP);
   1102             if (native_exception)
   1103                 save_results_to_barrier_cache(unwind_exception, results);
   1104             return _URC_HANDLER_FOUND;
   1105         }
   1106         // Did not find the catch handler
   1107         if (results.reason == _URC_CONTINUE_UNWIND)
   1108             return continue_unwind(unwind_exception, context);
   1109         return results.reason;
   1110 
   1111     case _US_UNWIND_FRAME_STARTING:
   1112         // TODO: Support force unwinding in the phase 2 search.
   1113         // NOTE: In order to call the cleanup functions, _Unwind_ForcedUnwind()
   1114         // will call this personality function with (_US_FORCE_UNWIND |
   1115         // _US_UNWIND_FRAME_STARTING).
   1116 
   1117         // Phase 2 search
   1118         if (unwind_exception->barrier_cache.sp == _Unwind_GetGR(context, REG_SP))
   1119         {
   1120             // Found a catching handler in phase 1
   1121             if (native_exception)
   1122             {
   1123                 // Load the result from the native exception barrier cache.
   1124                 load_results_from_barrier_cache(results, unwind_exception);
   1125                 results.reason = _URC_HANDLER_FOUND;
   1126             }
   1127             else
   1128             {
   1129                 // Search for the catching handler again for the foreign exception.
   1130                 scan_eh_tab(results, static_cast<_Unwind_Action>(_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME),
   1131                             native_exception, unwind_exception, context);
   1132                 if (results.reason != _URC_HANDLER_FOUND)  // phase1 search should guarantee to find one
   1133                     call_terminate(native_exception, unwind_exception);
   1134             }
   1135 
   1136             // Install the context for the catching handler
   1137             set_registers(unwind_exception, context, results);
   1138             return _URC_INSTALL_CONTEXT;
   1139         }
   1140 
   1141         // Either we didn't do a phase 1 search (due to forced unwinding), or
   1142         //  phase 1 reported no catching-handlers.
   1143         // Search for a (non-catching) cleanup
   1144         scan_eh_tab(results, _UA_CLEANUP_PHASE, native_exception, unwind_exception, context);
   1145         if (results.reason == _URC_HANDLER_FOUND)
   1146         {
   1147             // Found a non-catching handler
   1148 
   1149             // ARM EHABI 8.4.2: Before we can jump to the cleanup handler, we have to setup some
   1150             // internal data structures, so that __cxa_end_cleanup() can get unwind_exception from
   1151             // __cxa_get_globals().
   1152             __cxa_begin_cleanup(unwind_exception);
   1153 
   1154             // Install the context for the cleanup handler
   1155             set_registers(unwind_exception, context, results);
   1156             return _URC_INSTALL_CONTEXT;
   1157         }
   1158 
   1159         // Did not find any handler
   1160         if (results.reason == _URC_CONTINUE_UNWIND)
   1161             return continue_unwind(unwind_exception, context);
   1162         return results.reason;
   1163 
   1164     case _US_UNWIND_FRAME_RESUME:
   1165         return continue_unwind(unwind_exception, context);
   1166     }
   1167 
   1168     // We were called improperly: neither a phase 1 or phase 2 search
   1169     return _URC_FATAL_PHASE1_ERROR;
   1170 }
   1171 #endif
   1172 
   1173 
   1174 __attribute__((noreturn))
   1175 _LIBCXXABI_FUNC_VIS void
   1176 __cxa_call_unexpected(void* arg)
   1177 {
   1178     _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(arg);
   1179     if (unwind_exception == 0)
   1180         call_terminate(false, unwind_exception);
   1181     __cxa_begin_catch(unwind_exception);
   1182     bool native_old_exception =
   1183         (unwind_exception->exception_class & get_vendor_and_language) ==
   1184         (kOurExceptionClass                & get_vendor_and_language);
   1185     std::unexpected_handler u_handler;
   1186     std::terminate_handler t_handler;
   1187     __cxa_exception* old_exception_header = 0;
   1188     int64_t ttypeIndex;
   1189     const uint8_t* lsda;
   1190     if (native_old_exception)
   1191     {
   1192         old_exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
   1193         t_handler = old_exception_header->terminateHandler;
   1194         u_handler = old_exception_header->unexpectedHandler;
   1195         // If std::__unexpected(u_handler) rethrows the same exception,
   1196         //   these values get overwritten by the rethrow.  So save them now:
   1197 #if LIBCXXABI_ARM_EHABI
   1198         ttypeIndex = (int64_t)(int32_t)unwind_exception->barrier_cache.bitpattern[4];
   1199         lsda = (const uint8_t*)unwind_exception->barrier_cache.bitpattern[2];
   1200 #else
   1201         ttypeIndex = old_exception_header->handlerSwitchValue;
   1202         lsda = old_exception_header->languageSpecificData;
   1203 #endif
   1204     }
   1205     else
   1206     {
   1207         t_handler = std::get_terminate();
   1208         u_handler = std::get_unexpected();
   1209     }
   1210     try
   1211     {
   1212         std::__unexpected(u_handler);
   1213     }
   1214     catch (...)
   1215     {
   1216         // If the old exception is foreign, then all we can do is terminate.
   1217         //   We have no way to recover the needed old exception spec.  There's
   1218         //   no way to pass that information here.  And the personality routine
   1219         //   can't call us directly and do anything but terminate() if we throw
   1220         //   from here.
   1221         if (native_old_exception)
   1222         {
   1223             // Have:
   1224             //   old_exception_header->languageSpecificData
   1225             //   old_exception_header->actionRecord
   1226             // Need
   1227             //   const uint8_t* classInfo
   1228             //   uint8_t ttypeEncoding
   1229             uint8_t lpStartEncoding = *lsda++;
   1230             const uint8_t* lpStart = (const uint8_t*)readEncodedPointer(&lsda, lpStartEncoding);
   1231             (void)lpStart;  // purposefully unused.  Just needed to increment lsda.
   1232             uint8_t ttypeEncoding = *lsda++;
   1233             if (ttypeEncoding == DW_EH_PE_omit)
   1234                 std::__terminate(t_handler);
   1235             uintptr_t classInfoOffset = readULEB128(&lsda);
   1236             const uint8_t* classInfo = lsda + classInfoOffset;
   1237             // Is this new exception catchable by the exception spec at ttypeIndex?
   1238             // The answer is obviously yes if the new and old exceptions are the same exception
   1239             // If no
   1240             //    throw;
   1241             __cxa_eh_globals* globals = __cxa_get_globals_fast();
   1242             __cxa_exception* new_exception_header = globals->caughtExceptions;
   1243             if (new_exception_header == 0)
   1244                 // This shouldn't be able to happen!
   1245                 std::__terminate(t_handler);
   1246             bool native_new_exception =
   1247                 (new_exception_header->unwindHeader.exception_class & get_vendor_and_language) ==
   1248                                                 (kOurExceptionClass & get_vendor_and_language);
   1249             void* adjustedPtr;
   1250             if (native_new_exception && (new_exception_header != old_exception_header))
   1251             {
   1252                 const __shim_type_info* excpType =
   1253                     static_cast<const __shim_type_info*>(new_exception_header->exceptionType);
   1254                 adjustedPtr =
   1255                     new_exception_header->unwindHeader.exception_class == kOurDependentExceptionClass ?
   1256                         ((__cxa_dependent_exception*)new_exception_header)->primaryException :
   1257                         new_exception_header + 1;
   1258                 if (!exception_spec_can_catch(ttypeIndex, classInfo, ttypeEncoding,
   1259                                               excpType, adjustedPtr, unwind_exception))
   1260                 {
   1261                     // We need to __cxa_end_catch, but for the old exception,
   1262                     //   not the new one.  This is a little tricky ...
   1263                     // Disguise new_exception_header as a rethrown exception, but
   1264                     //   don't actually rethrow it.  This means you can temporarily
   1265                     //   end the catch clause enclosing new_exception_header without
   1266                     //   __cxa_end_catch destroying new_exception_header.
   1267                     new_exception_header->handlerCount = -new_exception_header->handlerCount;
   1268                     globals->uncaughtExceptions += 1;
   1269                     // Call __cxa_end_catch for new_exception_header
   1270                     __cxa_end_catch();
   1271                     // Call __cxa_end_catch for old_exception_header
   1272                     __cxa_end_catch();
   1273                     // Renter this catch clause with new_exception_header
   1274                     __cxa_begin_catch(&new_exception_header->unwindHeader);
   1275                     // Rethrow new_exception_header
   1276                     throw;
   1277                 }
   1278             }
   1279             // Will a std::bad_exception be catchable by the exception spec at
   1280             //   ttypeIndex?
   1281             // If no
   1282             //    throw std::bad_exception();
   1283             const __shim_type_info* excpType =
   1284                 static_cast<const __shim_type_info*>(&typeid(std::bad_exception));
   1285             std::bad_exception be;
   1286             adjustedPtr = &be;
   1287             if (!exception_spec_can_catch(ttypeIndex, classInfo, ttypeEncoding,
   1288                                           excpType, adjustedPtr, unwind_exception))
   1289             {
   1290                 // We need to __cxa_end_catch for both the old exception and the
   1291                 //   new exception.  Technically we should do it in that order.
   1292                 //   But it is expedient to do it in the opposite order:
   1293                 // Call __cxa_end_catch for new_exception_header
   1294                 __cxa_end_catch();
   1295                 // Throw std::bad_exception will __cxa_end_catch for
   1296                 //   old_exception_header
   1297                 throw be;
   1298             }
   1299         }
   1300     }
   1301     std::__terminate(t_handler);
   1302 }
   1303 
   1304 }  // extern "C"
   1305 
   1306 }  // __cxxabiv1
   1307