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