blob: 7f857faf78fdab7761a329d18b6ef5eed52b765f [file] [log] [blame]
Howard Hinnanted2ea9b2012-01-04 20:49:43 +00001//===------------------------- 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 Roelofsc82e02d2014-02-12 04:49:09 +000010// http://mentorembedded.github.io/cxx-abi/abi-eh.html
Howard Hinnant701d94c2012-01-06 20:39:47 +000011// http://www.intel.com/design/itanium/downloads/245358.htm
Howard Hinnanted2ea9b2012-01-04 20:49:43 +000012//
13//===----------------------------------------------------------------------===//
14
Dan Albert14690902014-08-29 15:26:06 +000015#include <assert.h>
16#include <stdlib.h>
Eric Fiselier89ea9ad2015-03-10 20:43:34 +000017#include <string.h>
Dan Albert14690902014-08-29 15:26:06 +000018#include <typeinfo>
19
Saleem Abdulrasool6191aec2016-05-04 04:22:29 +000020#include "__cxxabi_config.h"
Howard Hinnant701d94c2012-01-06 20:39:47 +000021#include "cxa_exception.hpp"
Howard Hinnant7da9b962012-01-30 16:07:00 +000022#include "cxa_handlers.hpp"
Howard Hinnantc30bfdc2012-01-22 21:47:40 +000023#include "private_typeinfo.h"
Dan Albert14690902014-08-29 15:26:06 +000024#include "unwind.h"
25
Howard Hinnant66f65802012-01-28 00:28:31 +000026/*
Howard Hinnant7da9b962012-01-30 16:07:00 +000027 Exception Header Layout:
Howard Hinnant66f65802012-01-28 00:28:31 +000028
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 Hinnant6d00fef2013-02-15 15:48:49 +000045| lpStart | (encoded with lpStartEncoding) | defaults to funcStart |
Howard Hinnant66f65802012-01-28 00:28:31 +000046+---------+-----+--------+-----------------+---------------+-------+
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 Hinnant7f9d2132012-02-29 22:14:19 +000054+---------------------+-----------+---------------------------------------------------+
Dan Alberta1fce462015-02-05 01:33:15 +000055#ifndef __USING_SJLJ_EXCEPTIONS__
Howard Hinnant7f9d2132012-02-29 22:14:19 +000056+---------------------+-----------+------------------------------------------------+
57| Beginning of Call Site Table The current ip lies within the |
Howard Hinnant7da9b962012-01-30 16:07:00 +000058| ... (start, length) range of one of these |
Howard Hinnant7f9d2132012-02-29 22:14:19 +000059| call sites. There may be action needed. |
Howard Hinnant7da9b962012-01-30 16:07:00 +000060| +-------------+---------------------------------+------------------------------+ |
61| | start | (encoded with callSiteEncoding) | offset relative to funcStart | |
Howard Hinnant7f9d2132012-02-29 22:14:19 +000062| | length | (encoded with callSiteEncoding) | length of code fragment | |
Howard Hinnant7da9b962012-01-30 16:07:00 +000063| | landingPad | (encoded with callSiteEncoding) | offset relative to lpStart | |
64| | actionEntry | (ULEB128) | Action Table Index 1-based | |
65| | | | actionEntry == 0 -> cleanup | |
66| +-------------+---------------------------------+------------------------------+ |
67| ... |
Howard Hinnant7f9d2132012-02-29 22:14:19 +000068+----------------------------------------------------------------------------------+
Jonathan Roelofs8b547a32014-05-08 19:13:16 +000069#else // __USING_SJLJ_EXCEPTIONS__
Howard Hinnant7f9d2132012-02-29 22:14:19 +000070+---------------------+-----------+------------------------------------------------+
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 Roelofs8b547a32014-05-08 19:13:16 +000082#endif // __USING_SJLJ_EXCEPTIONS__
Howard Hinnant7f9d2132012-02-29 22:14:19 +000083+---------------------------------------------------------------------+
Howard Hinnant66f65802012-01-28 00:28:31 +000084| 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 Hinnant7da9b962012-01-30 16:07:00 +000095| | Nth type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == N | |
Howard Hinnant66f65802012-01-28 00:28:31 +000096| +----------------+------------------------------------------------+-----------------+ |
97| ... |
98| +----------------+------------------------------------------------+-----------------+ |
Howard Hinnant7da9b962012-01-30 16:07:00 +000099| | 1st type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == 1 | |
Howard Hinnant66f65802012-01-28 00:28:31 +0000100| +----------------+------------------------------------------------+-----------------+ |
101| +---------------------------------------+-----------+------------------------------+ |
102| | 1st ttypeIndex for 1st exception spec | (ULEB128) | classInfoOffset points here! | |
Howard Hinnant7da9b962012-01-30 16:07:00 +0000103| | ... | (ULEB128) | | |
104| | Mth ttypeIndex for 1st exception spec | (ULEB128) | | |
Howard Hinnant66f65802012-01-28 00:28:31 +0000105| | 0 | (ULEB128) | | |
106| +---------------------------------------+------------------------------------------+ |
107| ... |
108| +---------------------------------------+------------------------------------------+ |
Howard Hinnant7da9b962012-01-30 16:07:00 +0000109| | 0 | (ULEB128) | throw() | |
110| +---------------------------------------+------------------------------------------+ |
111| ... |
112| +---------------------------------------+------------------------------------------+ |
Howard Hinnant66f65802012-01-28 00:28:31 +0000113| | 1st ttypeIndex for Nth exception spec | (ULEB128) | | |
Howard Hinnant7da9b962012-01-30 16:07:00 +0000114| | ... | (ULEB128) | | |
115| | Mth ttypeIndex for Nth exception spec | (ULEB128) | | |
Howard Hinnant66f65802012-01-28 00:28:31 +0000116| | 0 | (ULEB128) | | |
117| +---------------------------------------+------------------------------------------+ |
118+---------------------------------------------------------------------------------------+
Howard Hinnant7da9b962012-01-30 16:07:00 +0000119
120Notes:
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 Hinnant66f65802012-01-28 00:28:31 +0000136*/
Howard Hinnanta36fb302012-01-22 19:14:27 +0000137
Howard Hinnanted2ea9b2012-01-04 20:49:43 +0000138namespace __cxxabiv1
139{
140
Eric Fiselier89ea9ad2015-03-10 20:43:34 +0000141namespace
142{
143
144template <class AsType>
145uintptr_t readPointerHelper(const uint8_t*& p) {
146 AsType value;
Eric Fiselierfdd39fd2015-03-10 21:32:53 +0000147 memcpy(&value, p, sizeof(AsType));
Eric Fiselier89ea9ad2015-03-10 20:43:34 +0000148 p += sizeof(AsType);
149 return static_cast<uintptr_t>(value);
150}
151
152} // end namespace
153
Howard Hinnanted2ea9b2012-01-04 20:49:43 +0000154extern "C"
155{
156
157// private API
158
Howard Hinnant701d94c2012-01-06 20:39:47 +0000159// Heavily borrowed from llvm/examples/ExceptionDemo/ExceptionDemo.cpp
160
161// DWARF Constants
162enum
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
187static
188uintptr_t
189readULEB128(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 Hinnant6d00fef2013-02-15 15:48:49 +0000206/// See Variable Length Data Appendix C in:
Howard Hinnant701d94c2012-01-06 20:39:47 +0000207/// @link http://dwarfstd.org/Dwarf4.pdf @unlink
208/// @param data reference variable holding memory pointer to decode from
209/// @returns decoded value
210static
Howard Hinnant3e5c7d02012-03-08 18:45:24 +0000211intptr_t
Howard Hinnant701d94c2012-01-06 20:39:47 +0000212readSLEB128(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 Hinnant3e5c7d02012-03-08 18:45:24 +0000227 return static_cast<intptr_t>(result);
Howard Hinnant701d94c2012-01-06 20:39:47 +0000228}
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
236static
237uintptr_t
238readEncodedPointer(const uint8_t** data, uint8_t encoding)
239{
240 uintptr_t result = 0;
Howard Hinnant701d94c2012-01-06 20:39:47 +0000241 if (encoding == DW_EH_PE_omit)
242 return result;
Howard Hinnant7da9b962012-01-30 16:07:00 +0000243 const uint8_t* p = *data;
Howard Hinnant701d94c2012-01-06 20:39:47 +0000244 // first get value
245 switch (encoding & 0x0F)
246 {
247 case DW_EH_PE_absptr:
Saleem Abdulrasool0f111c22015-04-27 02:21:55 +0000248 result = readPointerHelper<uintptr_t>(p);
Howard Hinnant701d94c2012-01-06 20:39:47 +0000249 break;
250 case DW_EH_PE_uleb128:
251 result = readULEB128(&p);
252 break;
253 case DW_EH_PE_sleb128:
Howard Hinnant3e5c7d02012-03-08 18:45:24 +0000254 result = static_cast<uintptr_t>(readSLEB128(&p));
Howard Hinnant701d94c2012-01-06 20:39:47 +0000255 break;
256 case DW_EH_PE_udata2:
Eric Fiselier89ea9ad2015-03-10 20:43:34 +0000257 result = readPointerHelper<uint16_t>(p);
Howard Hinnant701d94c2012-01-06 20:39:47 +0000258 break;
259 case DW_EH_PE_udata4:
Eric Fiselier89ea9ad2015-03-10 20:43:34 +0000260 result = readPointerHelper<uint32_t>(p);
Howard Hinnant701d94c2012-01-06 20:39:47 +0000261 break;
262 case DW_EH_PE_udata8:
Eric Fiselier89ea9ad2015-03-10 20:43:34 +0000263 result = readPointerHelper<uint64_t>(p);
Howard Hinnant701d94c2012-01-06 20:39:47 +0000264 break;
265 case DW_EH_PE_sdata2:
Eric Fiselier89ea9ad2015-03-10 20:43:34 +0000266 result = readPointerHelper<int16_t>(p);
Howard Hinnant701d94c2012-01-06 20:39:47 +0000267 break;
268 case DW_EH_PE_sdata4:
Eric Fiselier89ea9ad2015-03-10 20:43:34 +0000269 result = readPointerHelper<int32_t>(p);
Howard Hinnant701d94c2012-01-06 20:39:47 +0000270 break;
271 case DW_EH_PE_sdata8:
Eric Fiselier89ea9ad2015-03-10 20:43:34 +0000272 result = readPointerHelper<int64_t>(p);
Howard Hinnant701d94c2012-01-06 20:39:47 +0000273 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 Hinnant3fa75e12012-01-08 23:50:46 +0000286 if (result)
287 result += (uintptr_t)(*data);
Howard Hinnant701d94c2012-01-06 20:39:47 +0000288 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 Hinnant3fa75e12012-01-08 23:50:46 +0000299 if (result && (encoding & DW_EH_PE_indirect))
Howard Hinnant701d94c2012-01-06 20:39:47 +0000300 result = *((uintptr_t*)result);
301 *data = p;
302 return result;
303}
304
Howard Hinnant3fa75e12012-01-08 23:50:46 +0000305static
Howard Hinnant7da9b962012-01-30 16:07:00 +0000306void
307call_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 Singh2ecb4ee2017-03-01 11:42:01 +0000319#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chien05d51bc2014-05-10 00:42:10 +0000320static 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 Roelofs52522052014-08-21 18:42:36 +0000325 // "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 Singh2ecb4ee2017-03-01 11:42:01 +0000330#if defined(LIBCXXABI_BAREMETAL)
Jonathan Roelofs52522052014-08-21 18:42:36 +0000331 return reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(ptr) +
332 offset);
333#else
Nico Weberd6e23362014-06-25 23:52:07 +0000334 return *reinterpret_cast<const void **>(reinterpret_cast<uintptr_t>(ptr) +
335 offset);
Jonathan Roelofs52522052014-08-21 18:42:36 +0000336#endif
Logan Chien05d51bc2014-05-10 00:42:10 +0000337}
338
339static const __shim_type_info*
340get_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 Chiencc4f5122016-11-13 14:44:41 +0000350 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 Chien05d51bc2014-05-10 00:42:10 +0000354 (void)ttypeEncoding;
355
356 const uint8_t* ttypePtr = classInfo - ttypeIndex * sizeof(uintptr_t);
Nico Weberd6e23362014-06-25 23:52:07 +0000357 return reinterpret_cast<const __shim_type_info *>(
358 read_target2_value(ttypePtr));
Logan Chien05d51bc2014-05-10 00:42:10 +0000359}
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +0000360#else // !defined(_LIBCXXABI_ARM_EHABI)
Howard Hinnant7da9b962012-01-30 16:07:00 +0000361static
362const __shim_type_info*
Howard Hinnant3e5c7d02012-03-08 18:45:24 +0000363get_shim_type_info(uint64_t ttypeIndex, const uint8_t* classInfo,
Howard Hinnant7da9b962012-01-30 16:07:00 +0000364 uint8_t ttypeEncoding, bool native_exception,
365 _Unwind_Exception* unwind_exception)
366{
Howard Hinnant7da9b962012-01-30 16:07:00 +0000367 if (classInfo == 0)
368 {
Howard Hinnant29ae5fc2012-02-01 16:56:40 +0000369 // this should not happen. Indicates corrupted eh_table.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000370 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 Hinnant29ae5fc2012-02-01 16:56:40 +0000390 // this should not happen. Indicates corrupted eh_table.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000391 call_terminate(native_exception, unwind_exception);
392 }
393 classInfo -= ttypeIndex;
394 return (const __shim_type_info*)readEncodedPointer(&classInfo, ttypeEncoding);
395}
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +0000396#endif // !defined(_LIBCXXABI_ARM_EHABI)
Howard Hinnant7da9b962012-01-30 16:07:00 +0000397
Howard Hinnant135b4bd2012-01-31 18:57:20 +0000398/*
Howard Hinnant6d00fef2013-02-15 15:48:49 +0000399 This is checking a thrown exception type, excpType, against a possibly empty
Howard Hinnant135b4bd2012-01-31 18:57:20 +0000400 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 Singh2ecb4ee2017-03-01 11:42:01 +0000407#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chien05d51bc2014-05-10 00:42:10 +0000408static
409bool
410exception_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 Chiencc4f5122016-11-13 14:44:41 +0000420 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 Chien05d51bc2014-05-10 00:42:10 +0000424 (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 Hinnant7da9b962012-01-30 16:07:00 +0000453static
454bool
455exception_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 Hinnant7da9b962012-01-30 16:07:00 +0000459 if (classInfo == 0)
460 {
Howard Hinnant29ae5fc2012-02-01 16:56:40 +0000461 // this should not happen. Indicates corrupted eh_table.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000462 call_terminate(false, unwind_exception);
463 }
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000464 // specIndex is negative of 1-based byte offset into classInfo;
465 specIndex = -specIndex;
Howard Hinnant7da9b962012-01-30 16:07:00 +0000466 --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 Chien05d51bc2014-05-10 00:42:10 +0000486#endif
Howard Hinnant7da9b962012-01-30 16:07:00 +0000487
488static
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000489void*
490get_thrown_object_ptr(_Unwind_Exception* unwind_exception)
Howard Hinnant3fa75e12012-01-08 23:50:46 +0000491{
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000492 // 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 Hinnant13217312012-03-17 00:10:52 +0000500namespace
501{
502
503struct 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
518static
519void
520set_registers(_Unwind_Exception* unwind_exception, _Unwind_Context* context,
521 const scan_results& results)
522{
Saleem Abdulrasool6bf23542015-09-20 02:08:31 +0000523#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 Hinnant13217312012-03-17 00:10:52 +0000531}
532
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000533/*
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 Weberd6e23362014-06-25 23:52:07 +0000554static void scan_eh_tab(scan_results &results, _Unwind_Action actions,
555 bool native_exception,
556 _Unwind_Exception *unwind_exception,
Logan Chien818a2e92014-06-30 12:35:29 +0000557 _Unwind_Context *context) {
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000558 // 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 Hinnant3fa75e12012-01-08 23:50:46 +0000567 {
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000568 // 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 Hinnant3fa75e12012-01-08 23:50:46 +0000576 }
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000577 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 Hinnant6d00fef2013-02-15 15:48:49 +0000588 else // Neither _UA_SEARCH_PHASE nor _UA_CLEANUP_PHASE is set
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000589 {
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 Chien818a2e92014-06-30 12:35:29 +0000596 const uint8_t *lsda = (const uint8_t *)_Unwind_GetLanguageSpecificData(context);
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000597 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 Alberta1fce462015-02-05 01:33:15 +0000610#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnant7f9d2132012-02-29 22:14:19 +0000611 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 Roelofs8b547a32014-05-08 19:13:16 +0000620#else // !__USING_SJLJ_EXCEPTIONS__
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000621 uintptr_t ipOffset = ip - funcStart;
Jonathan Roelofs8b547a32014-05-08 19:13:16 +0000622#endif // !defined(_USING_SLJL_EXCEPTIONS__)
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000623 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 Alberta1fce462015-02-05 01:33:15 +0000643#ifdef __USING_SJLJ_EXCEPTIONS__
Jonathan Roelofs8b547a32014-05-08 19:13:16 +0000644 (void)callSiteEncoding; // When using SjLj exceptions, callSiteEncoding is never used
Howard Hinnant96f01712012-03-09 18:01:37 +0000645#endif
Howard Hinnant85bc82e2012-03-08 20:16:45 +0000646 uint32_t callSiteTableLength = static_cast<uint32_t>(readULEB128(&lsda));
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000647 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 Hinnantf8f7f7f2012-11-12 18:19:15 +0000651 while (callSitePtr < callSiteTableEnd)
Howard Hinnant701d94c2012-01-06 20:39:47 +0000652 {
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000653 // There is one entry per call site.
Dan Alberta1fce462015-02-05 01:33:15 +0000654#ifndef __USING_SJLJ_EXCEPTIONS__
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000655 // 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 Roelofs8b547a32014-05-08 19:13:16 +0000662#else // __USING_SJLJ_EXCEPTIONS__
Howard Hinnant7f9d2132012-02-29 22:14:19 +0000663 // 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 Roelofs8b547a32014-05-08 19:13:16 +0000667#endif // __USING_SJLJ_EXCEPTIONS__
Howard Hinnant701d94c2012-01-06 20:39:47 +0000668 {
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000669 // Found the call site containing ip.
Dan Alberta1fce462015-02-05 01:33:15 +0000670#ifndef __USING_SJLJ_EXCEPTIONS__
Howard Hinnant701d94c2012-01-06 20:39:47 +0000671 if (landingPad == 0)
Howard Hinnant701d94c2012-01-06 20:39:47 +0000672 {
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000673 // No handler here
674 results.reason = _URC_CONTINUE_UNWIND;
675 return;
Howard Hinnant701d94c2012-01-06 20:39:47 +0000676 }
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000677 landingPad = (uintptr_t)lpStart + landingPad;
Jonathan Roelofs8b547a32014-05-08 19:13:16 +0000678#else // __USING_SJLJ_EXCEPTIONS__
Howard Hinnant13217312012-03-17 00:10:52 +0000679 ++landingPad;
Jonathan Roelofs8b547a32014-05-08 19:13:16 +0000680#endif // __USING_SJLJ_EXCEPTIONS__
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000681 if (actionEntry == 0)
682 {
683 // Found a cleanup
Howard Hinnanta5f9da22012-01-31 17:15:14 +0000684 // If this is a type 1 or type 2 search, there are no handlers
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000685 // 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 Hinnanta5f9da22012-01-31 17:15:14 +0000693 // No handler here
694 results.reason = _URC_CONTINUE_UNWIND;
695 return;
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000696 }
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 Hinnant3e5c7d02012-03-08 18:45:24 +0000709 get_shim_type_info(static_cast<uint64_t>(ttypeIndex),
710 classInfo, ttypeEncoding,
711 native_exception, unwind_exception);
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000712 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 Hinnant4c8d5612012-01-30 20:16:21 +0000865 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 Hinnant701d94c2012-01-06 20:39:47 +0000871 }
Dan Alberta1fce462015-02-05 01:33:15 +0000872#ifndef __USING_SJLJ_EXCEPTIONS__
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000873 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 Roelofs8b547a32014-05-08 19:13:16 +0000880#endif // !__USING_SJLJ_EXCEPTIONS__
Howard Hinnantf8f7f7f2012-11-12 18:19:15 +0000881 } // 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 Hinnanted2ea9b2012-01-04 20:49:43 +0000886}
887
888// public API
889
Howard Hinnant66f65802012-01-28 00:28:31 +0000890/*
Howard Hinnant66f65802012-01-28 00:28:31 +0000891The 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 Hinnant7da9b962012-01-30 16:07:00 +0000896 an error from above, return _URC_FATAL_PHASE1_ERROR.
897
Howard Hinnant66f65802012-01-28 00:28:31 +0000898 Scan for anything that could stop unwinding:
Howard Hinnant7da9b962012-01-30 16:07:00 +0000899
Howard Hinnant66f65802012-01-28 00:28:31 +0000900 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 Hinnant3a2765f2012-01-31 20:22:59 +0000924
925 This branch handles both normal C++ non-catching handlers (cleanups)
926 and forced unwinding.
Howard Hinnant66f65802012-01-28 00:28:31 +0000927 Scan for anything that can not stop unwinding:
928
Howard Hinnant7da9b962012-01-30 16:07:00 +0000929 1. A cleanup.
Howard Hinnant3a2765f2012-01-31 20:22:59 +0000930
Howard Hinnant7da9b962012-01-30 16:07:00 +0000931 If a cleanup is found
Howard Hinnant66f65802012-01-28 00:28:31 +0000932 transfer control to it. return _URC_INSTALL_CONTEXT
Howard Hinnant7da9b962012-01-30 16:07:00 +0000933 Else a cleanup is not found: return _URC_CONTINUE_UNWIND
Howard Hinnant66f65802012-01-28 00:28:31 +0000934*/
Howard Hinnant7da9b962012-01-30 16:07:00 +0000935
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +0000936#if !defined(_LIBCXXABI_ARM_EHABI)
Saleem Abdulrasool6191aec2016-05-04 04:22:29 +0000937_LIBCXXABI_FUNC_VIS _Unwind_Reason_Code
Dan Alberta1fce462015-02-05 01:33:15 +0000938#ifdef __USING_SJLJ_EXCEPTIONS__
Howard Hinnant7f9d2132012-02-29 22:14:19 +0000939__gxx_personality_sj0
940#else
941__gxx_personality_v0
942#endif
943 (int version, _Unwind_Action actions, uint64_t exceptionClass,
Howard Hinnant7da9b962012-01-30 16:07:00 +0000944 _Unwind_Exception* unwind_exception, _Unwind_Context* context)
945{
Howard Hinnant7da9b962012-01-30 16:07:00 +0000946 if (version != 1 || unwind_exception == 0 || context == 0)
947 return _URC_FATAL_PHASE1_ERROR;
Logan Chien05d51bc2014-05-10 00:42:10 +0000948
Howard Hinnant7ab185e2012-02-01 18:15:15 +0000949 bool native_exception = (exceptionClass & get_vendor_and_language) ==
950 (kOurExceptionClass & get_vendor_and_language);
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000951 scan_results results;
Howard Hinnant7da9b962012-01-30 16:07:00 +0000952 if (actions & _UA_SEARCH_PHASE)
953 {
Howard Hinnant29ae5fc2012-02-01 16:56:40 +0000954 // Phase 1 search: All we're looking for in phase 1 is a handler that
955 // halts unwinding
Logan Chien818a2e92014-06-30 12:35:29 +0000956 scan_eh_tab(results, actions, native_exception, unwind_exception, context);
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000957 if (results.reason == _URC_HANDLER_FOUND)
Howard Hinnant7da9b962012-01-30 16:07:00 +0000958 {
Howard Hinnant29ae5fc2012-02-01 16:56:40 +0000959 // Found one. Can we cache the results somewhere to optimize phase 2?
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000960 if (native_exception)
Howard Hinnant7da9b962012-01-30 16:07:00 +0000961 {
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000962 __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 Hinnant7da9b962012-01-30 16:07:00 +0000968 }
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000969 return _URC_HANDLER_FOUND;
Howard Hinnant7da9b962012-01-30 16:07:00 +0000970 }
Howard Hinnant29ae5fc2012-02-01 16:56:40 +0000971 // 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 Hinnant4c8d5612012-01-30 20:16:21 +0000974 return results.reason;
Howard Hinnant7da9b962012-01-30 16:07:00 +0000975 }
976 if (actions & _UA_CLEANUP_PHASE)
977 {
Howard Hinnant29ae5fc2012-02-01 16:56:40 +0000978 // Phase 2 search:
979 // Did we find a catching handler in phase 1?
Howard Hinnant7da9b962012-01-30 16:07:00 +0000980 if (actions & _UA_HANDLER_FRAME)
981 {
Howard Hinnant29ae5fc2012-02-01 16:56:40 +0000982 // Yes, phase 1 said we have a catching handler here.
983 // Did we cache the results of the scan?
Howard Hinnant7da9b962012-01-30 16:07:00 +0000984 if (native_exception)
985 {
Howard Hinnant29ae5fc2012-02-01 16:56:40 +0000986 // Yes, reload the results from the cache.
Howard Hinnant7da9b962012-01-30 16:07:00 +0000987 __cxa_exception* exception_header = (__cxa_exception*)(unwind_exception+1) - 1;
Howard Hinnant4c8d5612012-01-30 20:16:21 +0000988 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 Hinnant7da9b962012-01-30 16:07:00 +0000993 }
994 else
Howard Hinnanta5f9da22012-01-31 17:15:14 +0000995 {
Howard Hinnant29ae5fc2012-02-01 16:56:40 +0000996 // No, do the scan again to reload the results.
Logan Chien818a2e92014-06-30 12:35:29 +0000997 scan_eh_tab(results, actions, native_exception, unwind_exception, context);
Howard Hinnant29ae5fc2012-02-01 16:56:40 +0000998 // 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 Hinnant3a1009c2012-02-01 16:16:11 +00001000 if (results.reason != _URC_HANDLER_FOUND)
1001 call_terminate(native_exception, unwind_exception);
Howard Hinnanta5f9da22012-01-31 17:15:14 +00001002 }
Howard Hinnant29ae5fc2012-02-01 16:56:40 +00001003 // Jump to the handler
Howard Hinnant13217312012-03-17 00:10:52 +00001004 set_registers(unwind_exception, context, results);
Howard Hinnant7da9b962012-01-30 16:07:00 +00001005 return _URC_INSTALL_CONTEXT;
1006 }
Howard Hinnant29ae5fc2012-02-01 16:56:40 +00001007 // 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 Chien818a2e92014-06-30 12:35:29 +00001010 scan_eh_tab(results, actions, native_exception, unwind_exception, context);
Howard Hinnant4c8d5612012-01-30 20:16:21 +00001011 if (results.reason == _URC_HANDLER_FOUND)
Howard Hinnant7da9b962012-01-30 16:07:00 +00001012 {
Howard Hinnant29ae5fc2012-02-01 16:56:40 +00001013 // Found a non-catching handler. Jump to it:
Howard Hinnant13217312012-03-17 00:10:52 +00001014 set_registers(unwind_exception, context, results);
Howard Hinnant4c8d5612012-01-30 20:16:21 +00001015 return _URC_INSTALL_CONTEXT;
Howard Hinnant7da9b962012-01-30 16:07:00 +00001016 }
Howard Hinnant29ae5fc2012-02-01 16:56:40 +00001017 // 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 Hinnant4c8d5612012-01-30 20:16:21 +00001020 return results.reason;
Howard Hinnant7da9b962012-01-30 16:07:00 +00001021 }
Howard Hinnant29ae5fc2012-02-01 16:56:40 +00001022 // We were called improperly: neither a phase 1 or phase 2 search
Howard Hinnant7da9b962012-01-30 16:07:00 +00001023 return _URC_FATAL_PHASE1_ERROR;
1024}
Logan Chien05d51bc2014-05-10 00:42:10 +00001025#else
1026
Logan Chien088a71b2015-05-29 15:34:24 +00001027extern "C" _Unwind_Reason_Code __gnu_unwind_frame(_Unwind_Exception*,
1028 _Unwind_Context*);
Logan Chien083506f2015-01-22 13:27:36 +00001029
Logan Chien05d51bc2014-05-10 00:42:10 +00001030// 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 Chien083506f2015-01-22 13:27:36 +00001034static _Unwind_Reason_Code continue_unwind(_Unwind_Exception* unwind_exception,
1035 _Unwind_Context* context)
Logan Chien05d51bc2014-05-10 00:42:10 +00001036{
Logan Chien083506f2015-01-22 13:27:36 +00001037 if (__gnu_unwind_frame(unwind_exception, context) != _URC_OK)
1038 return _URC_FAILURE;
Logan Chien05d51bc2014-05-10 00:42:10 +00001039 return _URC_CONTINUE_UNWIND;
1040}
1041
1042// ARM register names
Logan Chienb96e8462016-08-31 15:16:40 +00001043#if !defined(LIBCXXABI_USE_LLVM_UNWINDER)
Logan Chien05d51bc2014-05-10 00:42:10 +00001044static const uint32_t REG_UCB = 12; // Register to save _Unwind_Control_Block
Logan Chien083506f2015-01-22 13:27:36 +00001045#endif
Logan Chien05d51bc2014-05-10 00:42:10 +00001046static const uint32_t REG_SP = 13;
1047
1048static 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
1058static 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 Abdulrasool6191aec2016-05-04 04:22:29 +00001068extern "C" _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code
Logan Chien05d51bc2014-05-10 00:42:10 +00001069__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 Chienb96e8462016-08-31 15:16:40 +00001079#if !defined(LIBCXXABI_USE_LLVM_UNWINDER)
Nico Weberd6e23362014-06-25 23:52:07 +00001080 // Copy the address of _Unwind_Control_Block to r12 so that
1081 // _Unwind_GetLanguageSpecificData() and _Unwind_GetRegionStart() can
1082 // return correct address.
Logan Chien05d51bc2014-05-10 00:42:10 +00001083 _Unwind_SetGR(context, REG_UCB, reinterpret_cast<uint32_t>(unwind_exception));
Logan Chien083506f2015-01-22 13:27:36 +00001084#endif
Logan Chien05d51bc2014-05-10 00:42:10 +00001085
Logan Chienf20383c2015-01-22 13:28:39 +00001086 // Check the undocumented force unwinding behavior
1087 bool is_force_unwinding = state & _US_FORCE_UNWIND;
1088 state &= ~_US_FORCE_UNWIND;
1089
Logan Chien05d51bc2014-05-10 00:42:10 +00001090 scan_results results;
1091 switch (state) {
1092 case _US_VIRTUAL_UNWIND_FRAME:
Logan Chienf20383c2015-01-22 13:28:39 +00001093 if (is_force_unwinding)
1094 return continue_unwind(unwind_exception, context);
1095
Logan Chien05d51bc2014-05-10 00:42:10 +00001096 // Phase 1 search: All we're looking for in phase 1 is a handler that halts unwinding
Logan Chien818a2e92014-06-30 12:35:29 +00001097 scan_eh_tab(results, _UA_SEARCH_PHASE, native_exception, unwind_exception, context);
Logan Chien05d51bc2014-05-10 00:42:10 +00001098 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 Chien083506f2015-01-22 13:27:36 +00001107 return continue_unwind(unwind_exception, context);
Logan Chien05d51bc2014-05-10 00:42:10 +00001108 return results.reason;
1109
1110 case _US_UNWIND_FRAME_STARTING:
Logan Chienf20383c2015-01-22 13:28:39 +00001111 // 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 Chien05d51bc2014-05-10 00:42:10 +00001116 // 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 Chien818a2e92014-06-30 12:35:29 +00001130 native_exception, unwind_exception, context);
Logan Chien05d51bc2014-05-10 00:42:10 +00001131 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 Weberd6e23362014-06-25 23:52:07 +00001140 // Either we didn't do a phase 1 search (due to forced unwinding), or
1141 // phase 1 reported no catching-handlers.
Logan Chien05d51bc2014-05-10 00:42:10 +00001142 // Search for a (non-catching) cleanup
Logan Chien818a2e92014-06-30 12:35:29 +00001143 scan_eh_tab(results, _UA_CLEANUP_PHASE, native_exception, unwind_exception, context);
Logan Chien05d51bc2014-05-10 00:42:10 +00001144 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 Chien083506f2015-01-22 13:27:36 +00001160 return continue_unwind(unwind_exception, context);
Logan Chien05d51bc2014-05-10 00:42:10 +00001161 return results.reason;
1162
1163 case _US_UNWIND_FRAME_RESUME:
Logan Chien083506f2015-01-22 13:27:36 +00001164 return continue_unwind(unwind_exception, context);
Logan Chien05d51bc2014-05-10 00:42:10 +00001165 }
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 Hinnant7da9b962012-01-30 16:07:00 +00001172
1173__attribute__((noreturn))
Saleem Abdulrasool5985dd62016-05-11 23:56:37 +00001174_LIBCXXABI_FUNC_VIS void
Howard Hinnant7da9b962012-01-30 16:07:00 +00001175__cxa_call_unexpected(void* arg)
1176{
Howard Hinnant7da9b962012-01-30 16:07:00 +00001177 _Unwind_Exception* unwind_exception = static_cast<_Unwind_Exception*>(arg);
1178 if (unwind_exception == 0)
Howard Hinnant1a584912012-01-31 01:51:15 +00001179 call_terminate(false, unwind_exception);
Howard Hinnant7da9b962012-01-30 16:07:00 +00001180 __cxa_begin_catch(unwind_exception);
Howard Hinnant7ab185e2012-02-01 18:15:15 +00001181 bool native_old_exception =
1182 (unwind_exception->exception_class & get_vendor_and_language) ==
1183 (kOurExceptionClass & get_vendor_and_language);
Howard Hinnant7da9b962012-01-30 16:07:00 +00001184 std::unexpected_handler u_handler;
1185 std::terminate_handler t_handler;
1186 __cxa_exception* old_exception_header = 0;
Howard Hinnant135b4bd2012-01-31 18:57:20 +00001187 int64_t ttypeIndex;
1188 const uint8_t* lsda;
Howard Hinnant7da9b962012-01-30 16:07:00 +00001189 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 Hinnant2e774bf2012-01-31 19:05:08 +00001194 // If std::__unexpected(u_handler) rethrows the same exception,
Howard Hinnant135b4bd2012-01-31 18:57:20 +00001195 // these values get overwritten by the rethrow. So save them now:
Ranjeet Singh2ecb4ee2017-03-01 11:42:01 +00001196#if defined(_LIBCXXABI_ARM_EHABI)
Logan Chien05d51bc2014-05-10 00:42:10 +00001197 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 Hinnant135b4bd2012-01-31 18:57:20 +00001200 ttypeIndex = old_exception_header->handlerSwitchValue;
1201 lsda = old_exception_header->languageSpecificData;
Logan Chien05d51bc2014-05-10 00:42:10 +00001202#endif
Howard Hinnant7da9b962012-01-30 16:07:00 +00001203 }
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 Hinnant7da9b962012-01-30 16:07:00 +00001222 // Have:
1223 // old_exception_header->languageSpecificData
1224 // old_exception_header->actionRecord
1225 // Need
1226 // const uint8_t* classInfo
1227 // uint8_t ttypeEncoding
Howard Hinnant7da9b962012-01-30 16:07:00 +00001228 uint8_t lpStartEncoding = *lsda++;
1229 const uint8_t* lpStart = (const uint8_t*)readEncodedPointer(&lsda, lpStartEncoding);
Howard Hinnantdfa81ca2012-03-08 20:23:24 +00001230 (void)lpStart; // purposefully unused. Just needed to increment lsda.
Howard Hinnant7da9b962012-01-30 16:07:00 +00001231 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 Hinnant7ab185e2012-02-01 18:15:15 +00001246 (new_exception_header->unwindHeader.exception_class & get_vendor_and_language) ==
1247 (kOurExceptionClass & get_vendor_and_language);
Howard Hinnant7da9b962012-01-30 16:07:00 +00001248 void* adjustedPtr;
Howard Hinnant135b4bd2012-01-31 18:57:20 +00001249 if (native_new_exception && (new_exception_header != old_exception_header))
Howard Hinnant7da9b962012-01-30 16:07:00 +00001250 {
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 Hinnant6d00fef2013-02-15 15:48:49 +00001291 // But it is expedient to do it in the opposite order:
Howard Hinnant7da9b962012-01-30 16:07:00 +00001292 // 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 Hinnanted2ea9b2012-01-04 20:49:43 +00001303} // extern "C"
1304
1305} // __cxxabiv1