1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 // This file implements the "Exception Handling APIs"
9 // https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
10 // http://www.intel.com/design/itanium/downloads/245358.htm
12 //===----------------------------------------------------------------------===//
19 #include "__cxxabi_config.h"
20 #include "cxa_exception.h"
21 #include "cxa_handlers.h"
22 #include "private_typeinfo.h"
25 #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
29 extern "C" EXCEPTION_DISPOSITION
_GCC_specific_handler(PEXCEPTION_RECORD
,
32 _Unwind_Personality_Fn
);
36 Exception Header Layout:
38 +---------------------------+-----------------------------+---------------+
39 | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object |
40 +---------------------------+-----------------------------+---------------+
43 +-------------------------------------------------------+
45 +---------------------------+-----------------------------+
46 | __cxa_dependent_exception | _Unwind_Exception CLNGC++\1 |
47 +---------------------------+-----------------------------+
49 Exception Handling Table Layout:
51 +-----------------+--------+
52 | lpStartEncoding | (char) |
53 +---------+-------+--------+---------------+-----------------------+
54 | lpStart | (encoded with lpStartEncoding) | defaults to funcStart |
55 +---------+-----+--------+-----------------+---------------+-------+
56 | ttypeEncoding | (char) | Encoding of the type_info table |
57 +---------------+-+------+----+----------------------------+----------------+
58 | classInfoOffset | (ULEB128) | Offset to type_info table, defaults to null |
59 +-----------------++--------+-+----------------------------+----------------+
60 | callSiteEncoding | (char) | Encoding for Call Site Table |
61 +------------------+--+-----+-----+------------------------+--------------------------+
62 | callSiteTableLength | (ULEB128) | Call Site Table length, used to find Action table |
63 +---------------------+-----------+---------------------------------------------------+
64 #ifndef __USING_SJLJ_EXCEPTIONS__
65 +---------------------+-----------+------------------------------------------------+
66 | Beginning of Call Site Table The current ip lies within the |
67 | ... (start, length) range of one of these |
68 | call sites. There may be action needed. |
69 | +-------------+---------------------------------+------------------------------+ |
70 | | start | (encoded with callSiteEncoding) | offset relative to funcStart | |
71 | | length | (encoded with callSiteEncoding) | length of code fragment | |
72 | | landingPad | (encoded with callSiteEncoding) | offset relative to lpStart | |
73 | | actionEntry | (ULEB128) | Action Table Index 1-based | |
74 | | | | actionEntry == 0 -> cleanup | |
75 | +-------------+---------------------------------+------------------------------+ |
77 +----------------------------------------------------------------------------------+
78 #else // __USING_SJLJ_EXCEPTIONS__
79 +---------------------+-----------+------------------------------------------------+
80 | Beginning of Call Site Table The current ip is a 1-based index into |
81 | ... this table. Or it is -1 meaning no |
82 | action is needed. Or it is 0 meaning |
84 | +-------------+---------------------------------+------------------------------+ |
85 | | landingPad | (ULEB128) | offset relative to lpStart | |
86 | | actionEntry | (ULEB128) | Action Table Index 1-based | |
87 | | | | actionEntry == 0 -> cleanup | |
88 | +-------------+---------------------------------+------------------------------+ |
90 +----------------------------------------------------------------------------------+
91 #endif // __USING_SJLJ_EXCEPTIONS__
92 +---------------------------------------------------------------------+
93 | Beginning of Action Table ttypeIndex == 0 : cleanup |
94 | ... ttypeIndex > 0 : catch |
95 | ttypeIndex < 0 : exception spec |
96 | +--------------+-----------+--------------------------------------+ |
97 | | ttypeIndex | (SLEB128) | Index into type_info Table (1-based) | |
98 | | actionOffset | (SLEB128) | Offset into next Action Table entry | |
99 | +--------------+-----------+--------------------------------------+ |
101 +---------------------------------------------------------------------+-----------------+
102 | type_info Table, but classInfoOffset does *not* point here! |
103 | +----------------+------------------------------------------------+-----------------+ |
104 | | Nth type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == N | |
105 | +----------------+------------------------------------------------+-----------------+ |
107 | +----------------+------------------------------------------------+-----------------+ |
108 | | 1st type_info* | Encoded with ttypeEncoding, 0 means catch(...) | ttypeIndex == 1 | |
109 | +----------------+------------------------------------------------+-----------------+ |
110 | +---------------------------------------+-----------+------------------------------+ |
111 | | 1st ttypeIndex for 1st exception spec | (ULEB128) | classInfoOffset points here! | |
112 | | ... | (ULEB128) | | |
113 | | Mth ttypeIndex for 1st exception spec | (ULEB128) | | |
114 | | 0 | (ULEB128) | | |
115 | +---------------------------------------+------------------------------------------+ |
117 | +---------------------------------------+------------------------------------------+ |
118 | | 0 | (ULEB128) | throw() | |
119 | +---------------------------------------+------------------------------------------+ |
121 | +---------------------------------------+------------------------------------------+ |
122 | | 1st ttypeIndex for Nth exception spec | (ULEB128) | | |
123 | | ... | (ULEB128) | | |
124 | | Mth ttypeIndex for Nth exception spec | (ULEB128) | | |
125 | | 0 | (ULEB128) | | |
126 | +---------------------------------------+------------------------------------------+ |
127 +---------------------------------------------------------------------------------------+
131 * ttypeIndex in the Action Table, and in the exception spec table, is an index,
132 not a byte count, if positive. It is a negative index offset of
133 classInfoOffset and the sizeof entry depends on ttypeEncoding.
134 But if ttypeIndex is negative, it is a positive 1-based byte offset into the
136 And if ttypeIndex is zero, it refers to a catch (...).
138 * landingPad can be 0, this implies there is nothing to be done.
140 * landingPad != 0 and actionEntry == 0 implies a cleanup needs to be done
143 * A cleanup can also be found under landingPad != 0 and actionEntry != 0 in
144 the Action Table with ttypeIndex == 0.
153 template <class AsType
>
154 uintptr_t readPointerHelper(const uint8_t*& p
) {
156 memcpy(&value
, p
, sizeof(AsType
));
158 return static_cast<uintptr_t>(value
);
168 // Heavily borrowed from llvm/examples/ExceptionDemo/ExceptionDemo.cpp
173 DW_EH_PE_absptr
= 0x00,
174 DW_EH_PE_uleb128
= 0x01,
175 DW_EH_PE_udata2
= 0x02,
176 DW_EH_PE_udata4
= 0x03,
177 DW_EH_PE_udata8
= 0x04,
178 DW_EH_PE_sleb128
= 0x09,
179 DW_EH_PE_sdata2
= 0x0A,
180 DW_EH_PE_sdata4
= 0x0B,
181 DW_EH_PE_sdata8
= 0x0C,
182 DW_EH_PE_pcrel
= 0x10,
183 DW_EH_PE_textrel
= 0x20,
184 DW_EH_PE_datarel
= 0x30,
185 DW_EH_PE_funcrel
= 0x40,
186 DW_EH_PE_aligned
= 0x50,
187 DW_EH_PE_indirect
= 0x80,
191 /// Read a uleb128 encoded value and advance pointer
192 /// See Variable Length Data Appendix C in:
193 /// @link http://dwarfstd.org/Dwarf4.pdf @unlink
194 /// @param data reference variable holding memory pointer to decode from
195 /// @returns decoded value
198 readULEB128(const uint8_t** data
)
200 uintptr_t result
= 0;
203 const uint8_t *p
= *data
;
207 result
|= static_cast<uintptr_t>(byte
& 0x7F) << shift
;
209 } while (byte
& 0x80);
214 /// Read a sleb128 encoded value and advance pointer
215 /// See Variable Length Data Appendix C in:
216 /// @link http://dwarfstd.org/Dwarf4.pdf @unlink
217 /// @param data reference variable holding memory pointer to decode from
218 /// @returns decoded value
221 readSLEB128(const uint8_t** data
)
223 uintptr_t result
= 0;
226 const uint8_t *p
= *data
;
230 result
|= static_cast<uintptr_t>(byte
& 0x7F) << shift
;
232 } while (byte
& 0x80);
234 if ((byte
& 0x40) && (shift
< (sizeof(result
) << 3)))
235 result
|= static_cast<uintptr_t>(~0) << shift
;
236 return static_cast<intptr_t>(result
);
239 /// Read a pointer encoded value and advance pointer
240 /// See Variable Length Data in:
241 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
242 /// @param data reference variable holding memory pointer to decode from
243 /// @param encoding dwarf encoding type
244 /// @param base for adding relative offset, default to 0
245 /// @returns decoded value
248 readEncodedPointer(const uint8_t** data
, uint8_t encoding
, uintptr_t base
= 0)
250 uintptr_t result
= 0;
251 if (encoding
== DW_EH_PE_omit
)
253 const uint8_t* p
= *data
;
255 switch (encoding
& 0x0F)
257 case DW_EH_PE_absptr
:
258 result
= readPointerHelper
<uintptr_t>(p
);
260 case DW_EH_PE_uleb128
:
261 result
= readULEB128(&p
);
263 case DW_EH_PE_sleb128
:
264 result
= static_cast<uintptr_t>(readSLEB128(&p
));
266 case DW_EH_PE_udata2
:
267 result
= readPointerHelper
<uint16_t>(p
);
269 case DW_EH_PE_udata4
:
270 result
= readPointerHelper
<uint32_t>(p
);
272 case DW_EH_PE_udata8
:
273 result
= readPointerHelper
<uint64_t>(p
);
275 case DW_EH_PE_sdata2
:
276 result
= readPointerHelper
<int16_t>(p
);
278 case DW_EH_PE_sdata4
:
279 result
= readPointerHelper
<int32_t>(p
);
281 case DW_EH_PE_sdata8
:
282 result
= readPointerHelper
<int64_t>(p
);
289 // then add relative offset
290 switch (encoding
& 0x70)
292 case DW_EH_PE_absptr
:
297 result
+= (uintptr_t)(*data
);
299 case DW_EH_PE_datarel
:
300 assert((base
!= 0) && "DW_EH_PE_datarel is invalid with a base of 0");
304 case DW_EH_PE_textrel
:
305 case DW_EH_PE_funcrel
:
306 case DW_EH_PE_aligned
:
312 // then apply indirection
313 if (result
&& (encoding
& DW_EH_PE_indirect
))
314 result
= *((uintptr_t*)result
);
321 call_terminate(bool native_exception
, _Unwind_Exception
* unwind_exception
)
323 __cxa_begin_catch(unwind_exception
);
324 if (native_exception
)
326 // Use the stored terminate_handler if possible
327 __cxa_exception
* exception_header
= (__cxa_exception
*)(unwind_exception
+1) - 1;
328 std::__terminate(exception_header
->terminateHandler
);
333 #if defined(_LIBCXXABI_ARM_EHABI)
334 static const void* read_target2_value(const void* ptr
)
336 uintptr_t offset
= *reinterpret_cast<const uintptr_t*>(ptr
);
339 // "ARM EABI provides a TARGET2 relocation to describe these typeinfo
340 // pointers. The reason being it allows their precise semantics to be
341 // deferred to the linker. For bare-metal they turn into absolute
342 // relocations. For linux they turn into GOT-REL relocations."
343 // https://gcc.gnu.org/ml/gcc-patches/2009-08/msg00264.html
344 #if defined(LIBCXXABI_BAREMETAL)
345 return reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(ptr
) +
348 return *reinterpret_cast<const void **>(reinterpret_cast<uintptr_t>(ptr
) +
353 static const __shim_type_info
*
354 get_shim_type_info(uint64_t ttypeIndex
, const uint8_t* classInfo
,
355 uint8_t ttypeEncoding
, bool native_exception
,
356 _Unwind_Exception
* unwind_exception
, uintptr_t /*base*/ = 0)
360 // this should not happen. Indicates corrupted eh_table.
361 call_terminate(native_exception
, unwind_exception
);
364 assert(((ttypeEncoding
== DW_EH_PE_absptr
) || // LLVM or GCC 4.6
365 (ttypeEncoding
== DW_EH_PE_pcrel
) || // GCC 4.7 baremetal
366 (ttypeEncoding
== (DW_EH_PE_pcrel
| DW_EH_PE_indirect
))) && // GCC 4.7 linux
367 "Unexpected TTypeEncoding");
370 const uint8_t* ttypePtr
= classInfo
- ttypeIndex
* sizeof(uintptr_t);
371 return reinterpret_cast<const __shim_type_info
*>(
372 read_target2_value(ttypePtr
));
374 #else // !defined(_LIBCXXABI_ARM_EHABI)
376 const __shim_type_info
*
377 get_shim_type_info(uint64_t ttypeIndex
, const uint8_t* classInfo
,
378 uint8_t ttypeEncoding
, bool native_exception
,
379 _Unwind_Exception
* unwind_exception
, uintptr_t base
= 0)
383 // this should not happen. Indicates corrupted eh_table.
384 call_terminate(native_exception
, unwind_exception
);
386 switch (ttypeEncoding
& 0x0F)
388 case DW_EH_PE_absptr
:
389 ttypeIndex
*= sizeof(void*);
391 case DW_EH_PE_udata2
:
392 case DW_EH_PE_sdata2
:
395 case DW_EH_PE_udata4
:
396 case DW_EH_PE_sdata4
:
399 case DW_EH_PE_udata8
:
400 case DW_EH_PE_sdata8
:
404 // this should not happen. Indicates corrupted eh_table.
405 call_terminate(native_exception
, unwind_exception
);
407 classInfo
-= ttypeIndex
;
408 return (const __shim_type_info
*)readEncodedPointer(&classInfo
,
409 ttypeEncoding
, base
);
411 #endif // !defined(_LIBCXXABI_ARM_EHABI)
414 This is checking a thrown exception type, excpType, against a possibly empty
415 list of catchType's which make up an exception spec.
417 An exception spec acts like a catch handler, but in reverse. This "catch
418 handler" will catch an excpType if and only if none of the catchType's in
419 the list will catch a excpType. If any catchType in the list can catch an
420 excpType, then this exception spec does not catch the excpType.
422 #if defined(_LIBCXXABI_ARM_EHABI)
425 exception_spec_can_catch(int64_t specIndex
, const uint8_t* classInfo
,
426 uint8_t ttypeEncoding
, const __shim_type_info
* excpType
,
427 void* adjustedPtr
, _Unwind_Exception
* unwind_exception
,
428 uintptr_t /*base*/ = 0)
432 // this should not happen. Indicates corrupted eh_table.
433 call_terminate(false, unwind_exception
);
436 assert(((ttypeEncoding
== DW_EH_PE_absptr
) || // LLVM or GCC 4.6
437 (ttypeEncoding
== DW_EH_PE_pcrel
) || // GCC 4.7 baremetal
438 (ttypeEncoding
== (DW_EH_PE_pcrel
| DW_EH_PE_indirect
))) && // GCC 4.7 linux
439 "Unexpected TTypeEncoding");
442 // specIndex is negative of 1-based byte offset into classInfo;
443 specIndex
= -specIndex
;
445 const void** temp
= reinterpret_cast<const void**>(
446 reinterpret_cast<uintptr_t>(classInfo
) +
447 static_cast<uintptr_t>(specIndex
) * sizeof(uintptr_t));
448 // If any type in the spec list can catch excpType, return false, else return true
449 // adjustments to adjustedPtr are ignored.
452 // ARM EHABI exception specification table (filter table) consists of
453 // several pointers which will directly point to the type info object
454 // (instead of ttypeIndex). The table will be terminated with 0.
455 const void** ttypePtr
= temp
++;
458 // We can get the __shim_type_info simply by performing a
459 // R_ARM_TARGET2 relocation, and cast the result to __shim_type_info.
460 const __shim_type_info
* catchType
=
461 static_cast<const __shim_type_info
*>(read_target2_value(ttypePtr
));
462 void* tempPtr
= adjustedPtr
;
463 if (catchType
->can_catch(excpType
, tempPtr
))
471 exception_spec_can_catch(int64_t specIndex
, const uint8_t* classInfo
,
472 uint8_t ttypeEncoding
, const __shim_type_info
* excpType
,
473 void* adjustedPtr
, _Unwind_Exception
* unwind_exception
,
478 // this should not happen. Indicates corrupted eh_table.
479 call_terminate(false, unwind_exception
);
481 // specIndex is negative of 1-based byte offset into classInfo;
482 specIndex
= -specIndex
;
484 const uint8_t* temp
= classInfo
+ specIndex
;
485 // If any type in the spec list can catch excpType, return false, else return true
486 // adjustments to adjustedPtr are ignored.
489 uint64_t ttypeIndex
= readULEB128(&temp
);
492 const __shim_type_info
* catchType
= get_shim_type_info(ttypeIndex
,
498 void* tempPtr
= adjustedPtr
;
499 if (catchType
->can_catch(excpType
, tempPtr
))
508 get_thrown_object_ptr(_Unwind_Exception
* unwind_exception
)
510 // Even for foreign exceptions, the exception object is *probably* at unwind_exception + 1
511 // Regardless, this library is prohibited from touching a foreign exception
512 void* adjustedPtr
= unwind_exception
+ 1;
513 if (__getExceptionClass(unwind_exception
) == kOurDependentExceptionClass
)
514 adjustedPtr
= ((__cxa_dependent_exception
*)adjustedPtr
- 1)->primaryException
;
523 int64_t ttypeIndex
; // > 0 catch handler, < 0 exception spec handler, == 0 a cleanup
524 const uint8_t* actionRecord
; // Currently unused. Retained to ease future maintenance.
525 const uint8_t* languageSpecificData
; // Needed only for __cxa_call_unexpected
526 uintptr_t landingPad
; // null -> nothing found, else something found
527 void* adjustedPtr
; // Used in cxa_exception.cpp
528 _Unwind_Reason_Code reason
; // One of _URC_FATAL_PHASE1_ERROR,
529 // _URC_FATAL_PHASE2_ERROR,
530 // _URC_CONTINUE_UNWIND,
531 // _URC_HANDLER_FOUND
534 } // unnamed namespace
538 set_registers(_Unwind_Exception
* unwind_exception
, _Unwind_Context
* context
,
539 const scan_results
& results
)
541 #if defined(__USING_SJLJ_EXCEPTIONS__)
542 #define __builtin_eh_return_data_regno(regno) regno
543 #elif defined(__ibmxl__)
544 // IBM xlclang++ compiler does not support __builtin_eh_return_data_regno.
545 #define __builtin_eh_return_data_regno(regno) regno + 3
547 _Unwind_SetGR(context
, __builtin_eh_return_data_regno(0),
548 reinterpret_cast<uintptr_t>(unwind_exception
));
549 _Unwind_SetGR(context
, __builtin_eh_return_data_regno(1),
550 static_cast<uintptr_t>(results
.ttypeIndex
));
551 _Unwind_SetIP(context
, results
.landingPad
);
555 There are 3 types of scans needed:
557 1. Scan for handler with native or foreign exception. If handler found,
558 save state and return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND.
559 May also report an error on invalid input.
560 May terminate for invalid exception table.
563 2. Scan for handler with foreign exception. Must return _URC_HANDLER_FOUND,
565 _UA_CLEANUP_PHASE && _UA_HANDLER_FRAME && !native_exception
567 3. Scan for cleanups. If a handler is found and this isn't forced unwind,
568 then terminate, otherwise ignore the handler and keep looking for cleanup.
569 If a cleanup is found, return _URC_HANDLER_FOUND, else return _URC_CONTINUE_UNWIND.
570 May also report an error on invalid input.
571 May terminate for invalid exception table.
572 _UA_CLEANUP_PHASE && !_UA_HANDLER_FRAME
575 static void scan_eh_tab(scan_results
&results
, _Unwind_Action actions
,
576 bool native_exception
,
577 _Unwind_Exception
*unwind_exception
,
578 _Unwind_Context
*context
) {
579 // Initialize results to found nothing but an error
580 results
.ttypeIndex
= 0;
581 results
.actionRecord
= 0;
582 results
.languageSpecificData
= 0;
583 results
.landingPad
= 0;
584 results
.adjustedPtr
= 0;
585 results
.reason
= _URC_FATAL_PHASE1_ERROR
;
586 // Check for consistent actions
587 if (actions
& _UA_SEARCH_PHASE
)
590 if (actions
& (_UA_CLEANUP_PHASE
| _UA_HANDLER_FRAME
| _UA_FORCE_UNWIND
))
592 // None of these flags should be set during Phase 1
594 results
.reason
= _URC_FATAL_PHASE1_ERROR
;
598 else if (actions
& _UA_CLEANUP_PHASE
)
600 if ((actions
& _UA_HANDLER_FRAME
) && (actions
& _UA_FORCE_UNWIND
))
602 // _UA_HANDLER_FRAME should only be set if phase 1 found a handler.
603 // If _UA_FORCE_UNWIND is set, phase 1 shouldn't have happened.
605 results
.reason
= _URC_FATAL_PHASE2_ERROR
;
609 else // Neither _UA_SEARCH_PHASE nor _UA_CLEANUP_PHASE is set
611 // One of these should be set.
613 results
.reason
= _URC_FATAL_PHASE1_ERROR
;
616 // Start scan by getting exception table address
617 const uint8_t *lsda
= (const uint8_t *)_Unwind_GetLanguageSpecificData(context
);
620 // There is no exception table
621 results
.reason
= _URC_CONTINUE_UNWIND
;
624 results
.languageSpecificData
= lsda
;
626 uintptr_t base
= _Unwind_GetDataRelBase(context
);
630 // Get the current instruction pointer and offset it before next
631 // instruction in the current frame which threw the exception.
632 uintptr_t ip
= _Unwind_GetIP(context
) - 1;
633 // Get beginning current frame's code (as defined by the
634 // emitted dwarf code)
635 uintptr_t funcStart
= _Unwind_GetRegionStart(context
);
636 #ifdef __USING_SJLJ_EXCEPTIONS__
637 if (ip
== uintptr_t(-1))
640 results
.reason
= _URC_CONTINUE_UNWIND
;
644 call_terminate(native_exception
, unwind_exception
);
645 // ip is 1-based index into call site table
646 #else // !__USING_SJLJ_EXCEPTIONS__
647 uintptr_t ipOffset
= ip
- funcStart
;
648 #endif // !defined(_USING_SLJL_EXCEPTIONS__)
649 const uint8_t* classInfo
= NULL
;
650 // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding
652 // Parse LSDA header.
653 uint8_t lpStartEncoding
= *lsda
++;
654 const uint8_t* lpStart
=
655 (const uint8_t*)readEncodedPointer(&lsda
, lpStartEncoding
, base
);
657 lpStart
= (const uint8_t*)funcStart
;
658 uint8_t ttypeEncoding
= *lsda
++;
659 if (ttypeEncoding
!= DW_EH_PE_omit
)
661 // Calculate type info locations in emitted dwarf code which
662 // were flagged by type info arguments to llvm.eh.selector
664 uintptr_t classInfoOffset
= readULEB128(&lsda
);
665 classInfo
= lsda
+ classInfoOffset
;
667 // Walk call-site table looking for range that
668 // includes current PC.
669 uint8_t callSiteEncoding
= *lsda
++;
670 #ifdef __USING_SJLJ_EXCEPTIONS__
671 (void)callSiteEncoding
; // When using SjLj exceptions, callSiteEncoding is never used
673 uint32_t callSiteTableLength
= static_cast<uint32_t>(readULEB128(&lsda
));
674 const uint8_t* callSiteTableStart
= lsda
;
675 const uint8_t* callSiteTableEnd
= callSiteTableStart
+ callSiteTableLength
;
676 const uint8_t* actionTableStart
= callSiteTableEnd
;
677 const uint8_t* callSitePtr
= callSiteTableStart
;
678 while (callSitePtr
< callSiteTableEnd
)
680 // There is one entry per call site.
681 #ifndef __USING_SJLJ_EXCEPTIONS__
682 // The call sites are non-overlapping in [start, start+length)
683 // The call sites are ordered in increasing value of start
684 uintptr_t start
= readEncodedPointer(&callSitePtr
, callSiteEncoding
);
685 uintptr_t length
= readEncodedPointer(&callSitePtr
, callSiteEncoding
);
686 uintptr_t landingPad
= readEncodedPointer(&callSitePtr
, callSiteEncoding
);
687 uintptr_t actionEntry
= readULEB128(&callSitePtr
);
688 if ((start
<= ipOffset
) && (ipOffset
< (start
+ length
)))
689 #else // __USING_SJLJ_EXCEPTIONS__
690 // ip is 1-based index into this table
691 uintptr_t landingPad
= readULEB128(&callSitePtr
);
692 uintptr_t actionEntry
= readULEB128(&callSitePtr
);
694 #endif // __USING_SJLJ_EXCEPTIONS__
696 // Found the call site containing ip.
697 #ifndef __USING_SJLJ_EXCEPTIONS__
701 results
.reason
= _URC_CONTINUE_UNWIND
;
704 landingPad
= (uintptr_t)lpStart
+ landingPad
;
705 #else // __USING_SJLJ_EXCEPTIONS__
707 #endif // __USING_SJLJ_EXCEPTIONS__
708 results
.landingPad
= landingPad
;
709 if (actionEntry
== 0)
712 results
.reason
= actions
& _UA_SEARCH_PHASE
713 ? _URC_CONTINUE_UNWIND
714 : _URC_HANDLER_FOUND
;
717 // Convert 1-based byte offset into
718 const uint8_t* action
= actionTableStart
+ (actionEntry
- 1);
719 bool hasCleanup
= false;
720 // Scan action entries until you find a matching handler, cleanup, or the end of action list
723 const uint8_t* actionRecord
= action
;
724 int64_t ttypeIndex
= readSLEB128(&action
);
727 // Found a catch, does it actually catch?
728 // First check for catch (...)
729 const __shim_type_info
* catchType
=
730 get_shim_type_info(static_cast<uint64_t>(ttypeIndex
),
731 classInfo
, ttypeEncoding
,
732 native_exception
, unwind_exception
,
736 // Found catch (...) catches everything, including
737 // foreign exceptions. This is search phase, cleanup
738 // phase with foreign exception, or forced unwinding.
739 assert(actions
& (_UA_SEARCH_PHASE
| _UA_HANDLER_FRAME
|
741 results
.ttypeIndex
= ttypeIndex
;
742 results
.actionRecord
= actionRecord
;
743 results
.adjustedPtr
=
744 get_thrown_object_ptr(unwind_exception
);
745 results
.reason
= _URC_HANDLER_FOUND
;
748 // Else this is a catch (T) clause and will never
749 // catch a foreign exception
750 else if (native_exception
)
752 __cxa_exception
* exception_header
= (__cxa_exception
*)(unwind_exception
+1) - 1;
753 void* adjustedPtr
= get_thrown_object_ptr(unwind_exception
);
754 const __shim_type_info
* excpType
=
755 static_cast<const __shim_type_info
*>(exception_header
->exceptionType
);
756 if (adjustedPtr
== 0 || excpType
== 0)
758 // Something very bad happened
759 call_terminate(native_exception
, unwind_exception
);
761 if (catchType
->can_catch(excpType
, adjustedPtr
))
763 // Found a matching handler. This is either search
764 // phase or forced unwinding.
766 (_UA_SEARCH_PHASE
| _UA_FORCE_UNWIND
));
767 results
.ttypeIndex
= ttypeIndex
;
768 results
.actionRecord
= actionRecord
;
769 results
.adjustedPtr
= adjustedPtr
;
770 results
.reason
= _URC_HANDLER_FOUND
;
774 // Scan next action ...
776 else if (ttypeIndex
< 0)
778 // Found an exception specification.
779 if (actions
& _UA_FORCE_UNWIND
) {
780 // Skip if forced unwinding.
781 } else if (native_exception
) {
782 // Does the exception spec catch this native exception?
783 __cxa_exception
* exception_header
= (__cxa_exception
*)(unwind_exception
+1) - 1;
784 void* adjustedPtr
= get_thrown_object_ptr(unwind_exception
);
785 const __shim_type_info
* excpType
=
786 static_cast<const __shim_type_info
*>(exception_header
->exceptionType
);
787 if (adjustedPtr
== 0 || excpType
== 0)
789 // Something very bad happened
790 call_terminate(native_exception
, unwind_exception
);
792 if (exception_spec_can_catch(ttypeIndex
, classInfo
,
793 ttypeEncoding
, excpType
,
795 unwind_exception
, base
))
797 // Native exception caught by exception
799 assert(actions
& _UA_SEARCH_PHASE
);
800 results
.ttypeIndex
= ttypeIndex
;
801 results
.actionRecord
= actionRecord
;
802 results
.adjustedPtr
= adjustedPtr
;
803 results
.reason
= _URC_HANDLER_FOUND
;
807 // foreign exception caught by exception spec
808 results
.ttypeIndex
= ttypeIndex
;
809 results
.actionRecord
= actionRecord
;
810 results
.adjustedPtr
=
811 get_thrown_object_ptr(unwind_exception
);
812 results
.reason
= _URC_HANDLER_FOUND
;
815 // Scan next action ...
819 const uint8_t* temp
= action
;
820 int64_t actionOffset
= readSLEB128(&temp
);
821 if (actionOffset
== 0)
823 // End of action list. If this is phase 2 and we have found
824 // a cleanup (ttypeIndex=0), return _URC_HANDLER_FOUND;
825 // otherwise return _URC_CONTINUE_UNWIND.
826 results
.reason
= hasCleanup
&& actions
& _UA_CLEANUP_PHASE
828 : _URC_CONTINUE_UNWIND
;
832 action
+= actionOffset
;
833 } // there is no break out of this loop, only return
835 #ifndef __USING_SJLJ_EXCEPTIONS__
836 else if (ipOffset
< start
)
838 // There is no call site for this ip
839 // Something bad has happened. We should never get here.
840 // Possible stack corruption.
841 call_terminate(native_exception
, unwind_exception
);
843 #endif // !__USING_SJLJ_EXCEPTIONS__
844 } // there might be some tricky cases which break out of this loop
846 // It is possible that no eh table entry specify how to handle
847 // this exception. By spec, terminate it immediately.
848 call_terminate(native_exception
, unwind_exception
);
854 The personality function branches on actions like so:
858 If _UA_CLEANUP_PHASE or _UA_HANDLER_FRAME or _UA_FORCE_UNWIND there's
859 an error from above, return _URC_FATAL_PHASE1_ERROR.
861 Scan for anything that could stop unwinding:
863 1. A catch clause that will catch this exception
864 (will never catch foreign).
865 2. A catch (...) (will always catch foreign).
866 3. An exception spec that will catch this exception
867 (will always catch foreign).
868 If a handler is found
871 return _URC_HANDLER_FOUND
872 Else a handler not found
873 return _URC_CONTINUE_UNWIND
879 How did this happen? return _URC_FATAL_PHASE2_ERROR
881 Do _UA_SEARCH_PHASE to recover state
883 Recover state from header
884 Transfer control to landing pad. return _URC_INSTALL_CONTEXT
888 This branch handles both normal C++ non-catching handlers (cleanups)
889 and forced unwinding.
890 Scan for anything that can not stop unwinding:
894 If a cleanup is found
895 transfer control to it. return _URC_INSTALL_CONTEXT
896 Else a cleanup is not found: return _URC_CONTINUE_UNWIND
899 #if !defined(_LIBCXXABI_ARM_EHABI)
900 #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
901 static _Unwind_Reason_Code __gxx_personality_imp
903 _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code
904 #ifdef __USING_SJLJ_EXCEPTIONS__
905 __gxx_personality_sj0
910 (int version
, _Unwind_Action actions
, uint64_t exceptionClass
,
911 _Unwind_Exception
* unwind_exception
, _Unwind_Context
* context
)
913 if (version
!= 1 || unwind_exception
== 0 || context
== 0)
914 return _URC_FATAL_PHASE1_ERROR
;
916 bool native_exception
= (exceptionClass
& get_vendor_and_language
) ==
917 (kOurExceptionClass
& get_vendor_and_language
);
918 scan_results results
;
919 // Process a catch handler for a native exception first.
920 if (actions
== (_UA_CLEANUP_PHASE
| _UA_HANDLER_FRAME
) &&
922 // Reload the results from the phase 1 cache.
923 __cxa_exception
* exception_header
=
924 (__cxa_exception
*)(unwind_exception
+ 1) - 1;
925 results
.ttypeIndex
= exception_header
->handlerSwitchValue
;
926 results
.actionRecord
= exception_header
->actionRecord
;
927 results
.languageSpecificData
= exception_header
->languageSpecificData
;
929 reinterpret_cast<uintptr_t>(exception_header
->catchTemp
);
930 results
.adjustedPtr
= exception_header
->adjustedPtr
;
932 // Jump to the handler.
933 set_registers(unwind_exception
, context
, results
);
934 // Cache base for calculating the address of ttype in
935 // __cxa_call_unexpected.
936 if (results
.ttypeIndex
< 0) {
938 exception_header
->catchTemp
= (void *)_Unwind_GetDataRelBase(context
);
940 exception_header
->catchTemp
= 0;
943 return _URC_INSTALL_CONTEXT
;
946 // In other cases we need to scan LSDA.
947 scan_eh_tab(results
, actions
, native_exception
, unwind_exception
, context
);
948 if (results
.reason
== _URC_CONTINUE_UNWIND
||
949 results
.reason
== _URC_FATAL_PHASE1_ERROR
)
950 return results
.reason
;
952 if (actions
& _UA_SEARCH_PHASE
)
954 // Phase 1 search: All we're looking for in phase 1 is a handler that
956 assert(results
.reason
== _URC_HANDLER_FOUND
);
957 if (native_exception
) {
958 // For a native exception, cache the LSDA result.
959 __cxa_exception
* exc
= (__cxa_exception
*)(unwind_exception
+ 1) - 1;
960 exc
->handlerSwitchValue
= static_cast<int>(results
.ttypeIndex
);
961 exc
->actionRecord
= results
.actionRecord
;
962 exc
->languageSpecificData
= results
.languageSpecificData
;
963 exc
->catchTemp
= reinterpret_cast<void*>(results
.landingPad
);
964 exc
->adjustedPtr
= results
.adjustedPtr
;
966 return _URC_HANDLER_FOUND
;
969 assert(actions
& _UA_CLEANUP_PHASE
);
970 assert(results
.reason
== _URC_HANDLER_FOUND
);
971 set_registers(unwind_exception
, context
, results
);
972 // Cache base for calculating the address of ttype in __cxa_call_unexpected.
973 if (results
.ttypeIndex
< 0) {
974 __cxa_exception
* exception_header
=
975 (__cxa_exception
*)(unwind_exception
+ 1) - 1;
977 exception_header
->catchTemp
= (void *)_Unwind_GetDataRelBase(context
);
979 exception_header
->catchTemp
= 0;
982 return _URC_INSTALL_CONTEXT
;
985 #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
986 extern "C" _LIBCXXABI_FUNC_VIS EXCEPTION_DISPOSITION
987 __gxx_personality_seh0(PEXCEPTION_RECORD ms_exc
, void *this_frame
,
988 PCONTEXT ms_orig_context
, PDISPATCHER_CONTEXT ms_disp
)
990 return _GCC_specific_handler(ms_exc
, this_frame
, ms_orig_context
, ms_disp
,
991 __gxx_personality_imp
);
997 extern "C" _Unwind_Reason_Code
__gnu_unwind_frame(_Unwind_Exception
*,
1000 // Helper function to unwind one frame.
1001 // ARM EHABI 7.3 and 7.4: If the personality function returns _URC_CONTINUE_UNWIND, the
1002 // personality routine should update the virtual register set (VRS) according to the
1003 // corresponding frame unwinding instructions (ARM EHABI 9.3.)
1004 static _Unwind_Reason_Code
continue_unwind(_Unwind_Exception
* unwind_exception
,
1005 _Unwind_Context
* context
)
1007 switch (__gnu_unwind_frame(unwind_exception
, context
)) {
1009 return _URC_CONTINUE_UNWIND
;
1010 case _URC_END_OF_STACK
:
1011 return _URC_END_OF_STACK
;
1013 return _URC_FAILURE
;
1017 // ARM register names
1018 #if !defined(LIBCXXABI_USE_LLVM_UNWINDER)
1019 static const uint32_t REG_UCB
= 12; // Register to save _Unwind_Control_Block
1021 static const uint32_t REG_SP
= 13;
1023 static void save_results_to_barrier_cache(_Unwind_Exception
* unwind_exception
,
1024 const scan_results
& results
)
1026 unwind_exception
->barrier_cache
.bitpattern
[0] = (uint32_t)results
.adjustedPtr
;
1027 unwind_exception
->barrier_cache
.bitpattern
[1] = (uint32_t)results
.actionRecord
;
1028 unwind_exception
->barrier_cache
.bitpattern
[2] = (uint32_t)results
.languageSpecificData
;
1029 unwind_exception
->barrier_cache
.bitpattern
[3] = (uint32_t)results
.landingPad
;
1030 unwind_exception
->barrier_cache
.bitpattern
[4] = (uint32_t)results
.ttypeIndex
;
1033 static void load_results_from_barrier_cache(scan_results
& results
,
1034 const _Unwind_Exception
* unwind_exception
)
1036 results
.adjustedPtr
= (void*)unwind_exception
->barrier_cache
.bitpattern
[0];
1037 results
.actionRecord
= (const uint8_t*)unwind_exception
->barrier_cache
.bitpattern
[1];
1038 results
.languageSpecificData
= (const uint8_t*)unwind_exception
->barrier_cache
.bitpattern
[2];
1039 results
.landingPad
= (uintptr_t)unwind_exception
->barrier_cache
.bitpattern
[3];
1040 results
.ttypeIndex
= (int64_t)(int32_t)unwind_exception
->barrier_cache
.bitpattern
[4];
1043 extern "C" _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code
1044 __gxx_personality_v0(_Unwind_State state
,
1045 _Unwind_Exception
* unwind_exception
,
1046 _Unwind_Context
* context
)
1048 if (unwind_exception
== 0 || context
== 0)
1049 return _URC_FATAL_PHASE1_ERROR
;
1051 bool native_exception
= __isOurExceptionClass(unwind_exception
);
1053 #if !defined(LIBCXXABI_USE_LLVM_UNWINDER)
1054 // Copy the address of _Unwind_Control_Block to r12 so that
1055 // _Unwind_GetLanguageSpecificData() and _Unwind_GetRegionStart() can
1056 // return correct address.
1057 _Unwind_SetGR(context
, REG_UCB
, reinterpret_cast<uint32_t>(unwind_exception
));
1060 // Check the undocumented force unwinding behavior
1061 bool is_force_unwinding
= state
& _US_FORCE_UNWIND
;
1062 state
&= ~_US_FORCE_UNWIND
;
1064 scan_results results
;
1066 case _US_VIRTUAL_UNWIND_FRAME
:
1067 if (is_force_unwinding
)
1068 return continue_unwind(unwind_exception
, context
);
1070 // Phase 1 search: All we're looking for in phase 1 is a handler that halts unwinding
1071 scan_eh_tab(results
, _UA_SEARCH_PHASE
, native_exception
, unwind_exception
, context
);
1072 if (results
.reason
== _URC_HANDLER_FOUND
)
1074 unwind_exception
->barrier_cache
.sp
= _Unwind_GetGR(context
, REG_SP
);
1075 if (native_exception
)
1076 save_results_to_barrier_cache(unwind_exception
, results
);
1077 return _URC_HANDLER_FOUND
;
1079 // Did not find the catch handler
1080 if (results
.reason
== _URC_CONTINUE_UNWIND
)
1081 return continue_unwind(unwind_exception
, context
);
1082 return results
.reason
;
1084 case _US_UNWIND_FRAME_STARTING
:
1085 // TODO: Support force unwinding in the phase 2 search.
1086 // NOTE: In order to call the cleanup functions, _Unwind_ForcedUnwind()
1087 // will call this personality function with (_US_FORCE_UNWIND |
1088 // _US_UNWIND_FRAME_STARTING).
1091 if (unwind_exception
->barrier_cache
.sp
== _Unwind_GetGR(context
, REG_SP
))
1093 // Found a catching handler in phase 1
1094 if (native_exception
)
1096 // Load the result from the native exception barrier cache.
1097 load_results_from_barrier_cache(results
, unwind_exception
);
1098 results
.reason
= _URC_HANDLER_FOUND
;
1102 // Search for the catching handler again for the foreign exception.
1103 scan_eh_tab(results
, static_cast<_Unwind_Action
>(_UA_CLEANUP_PHASE
| _UA_HANDLER_FRAME
),
1104 native_exception
, unwind_exception
, context
);
1105 if (results
.reason
!= _URC_HANDLER_FOUND
) // phase1 search should guarantee to find one
1106 call_terminate(native_exception
, unwind_exception
);
1109 // Install the context for the catching handler
1110 set_registers(unwind_exception
, context
, results
);
1111 return _URC_INSTALL_CONTEXT
;
1114 // Either we didn't do a phase 1 search (due to forced unwinding), or
1115 // phase 1 reported no catching-handlers.
1116 // Search for a (non-catching) cleanup
1117 if (is_force_unwinding
)
1120 static_cast<_Unwind_Action
>(_UA_CLEANUP_PHASE
| _UA_FORCE_UNWIND
),
1121 native_exception
, unwind_exception
, context
);
1123 scan_eh_tab(results
, _UA_CLEANUP_PHASE
, native_exception
,
1124 unwind_exception
, context
);
1125 if (results
.reason
== _URC_HANDLER_FOUND
)
1127 // Found a non-catching handler
1129 // ARM EHABI 8.4.2: Before we can jump to the cleanup handler, we have to setup some
1130 // internal data structures, so that __cxa_end_cleanup() can get unwind_exception from
1131 // __cxa_get_globals().
1132 __cxa_begin_cleanup(unwind_exception
);
1134 // Install the context for the cleanup handler
1135 set_registers(unwind_exception
, context
, results
);
1136 return _URC_INSTALL_CONTEXT
;
1139 // Did not find any handler
1140 if (results
.reason
== _URC_CONTINUE_UNWIND
)
1141 return continue_unwind(unwind_exception
, context
);
1142 return results
.reason
;
1144 case _US_UNWIND_FRAME_RESUME
:
1145 return continue_unwind(unwind_exception
, context
);
1148 // We were called improperly: neither a phase 1 or phase 2 search
1149 return _URC_FATAL_PHASE1_ERROR
;
1154 __attribute__((noreturn
))
1155 _LIBCXXABI_FUNC_VIS
void
1156 __cxa_call_unexpected(void* arg
)
1158 _Unwind_Exception
* unwind_exception
= static_cast<_Unwind_Exception
*>(arg
);
1159 if (unwind_exception
== 0)
1160 call_terminate(false, unwind_exception
);
1161 __cxa_begin_catch(unwind_exception
);
1162 bool native_old_exception
= __isOurExceptionClass(unwind_exception
);
1163 std::unexpected_handler u_handler
;
1164 std::terminate_handler t_handler
;
1165 __cxa_exception
* old_exception_header
= 0;
1167 const uint8_t* lsda
;
1170 if (native_old_exception
)
1172 old_exception_header
= (__cxa_exception
*)(unwind_exception
+1) - 1;
1173 t_handler
= old_exception_header
->terminateHandler
;
1174 u_handler
= old_exception_header
->unexpectedHandler
;
1175 // If std::__unexpected(u_handler) rethrows the same exception,
1176 // these values get overwritten by the rethrow. So save them now:
1177 #if defined(_LIBCXXABI_ARM_EHABI)
1178 ttypeIndex
= (int64_t)(int32_t)unwind_exception
->barrier_cache
.bitpattern
[4];
1179 lsda
= (const uint8_t*)unwind_exception
->barrier_cache
.bitpattern
[2];
1181 ttypeIndex
= old_exception_header
->handlerSwitchValue
;
1182 lsda
= old_exception_header
->languageSpecificData
;
1183 base
= (uintptr_t)old_exception_header
->catchTemp
;
1188 t_handler
= std::get_terminate();
1189 u_handler
= std::get_unexpected();
1193 std::__unexpected(u_handler
);
1197 // If the old exception is foreign, then all we can do is terminate.
1198 // We have no way to recover the needed old exception spec. There's
1199 // no way to pass that information here. And the personality routine
1200 // can't call us directly and do anything but terminate() if we throw
1202 if (native_old_exception
)
1205 // old_exception_header->languageSpecificData
1206 // old_exception_header->actionRecord
1207 // old_exception_header->catchTemp, base for calculating ttype
1209 // const uint8_t* classInfo
1210 // uint8_t ttypeEncoding
1211 uint8_t lpStartEncoding
= *lsda
++;
1212 const uint8_t* lpStart
=
1213 (const uint8_t*)readEncodedPointer(&lsda
, lpStartEncoding
, base
);
1214 (void)lpStart
; // purposefully unused. Just needed to increment lsda.
1215 uint8_t ttypeEncoding
= *lsda
++;
1216 if (ttypeEncoding
== DW_EH_PE_omit
)
1217 std::__terminate(t_handler
);
1218 uintptr_t classInfoOffset
= readULEB128(&lsda
);
1219 const uint8_t* classInfo
= lsda
+ classInfoOffset
;
1220 // Is this new exception catchable by the exception spec at ttypeIndex?
1221 // The answer is obviously yes if the new and old exceptions are the same exception
1224 __cxa_eh_globals
* globals
= __cxa_get_globals_fast();
1225 __cxa_exception
* new_exception_header
= globals
->caughtExceptions
;
1226 if (new_exception_header
== 0)
1227 // This shouldn't be able to happen!
1228 std::__terminate(t_handler
);
1229 bool native_new_exception
= __isOurExceptionClass(&new_exception_header
->unwindHeader
);
1231 if (native_new_exception
&& (new_exception_header
!= old_exception_header
))
1233 const __shim_type_info
* excpType
=
1234 static_cast<const __shim_type_info
*>(new_exception_header
->exceptionType
);
1236 __getExceptionClass(&new_exception_header
->unwindHeader
) == kOurDependentExceptionClass
?
1237 ((__cxa_dependent_exception
*)new_exception_header
)->primaryException
:
1238 new_exception_header
+ 1;
1239 if (!exception_spec_can_catch(ttypeIndex
, classInfo
, ttypeEncoding
,
1240 excpType
, adjustedPtr
,
1241 unwind_exception
, base
))
1243 // We need to __cxa_end_catch, but for the old exception,
1244 // not the new one. This is a little tricky ...
1245 // Disguise new_exception_header as a rethrown exception, but
1246 // don't actually rethrow it. This means you can temporarily
1247 // end the catch clause enclosing new_exception_header without
1248 // __cxa_end_catch destroying new_exception_header.
1249 new_exception_header
->handlerCount
= -new_exception_header
->handlerCount
;
1250 globals
->uncaughtExceptions
+= 1;
1251 // Call __cxa_end_catch for new_exception_header
1253 // Call __cxa_end_catch for old_exception_header
1255 // Renter this catch clause with new_exception_header
1256 __cxa_begin_catch(&new_exception_header
->unwindHeader
);
1257 // Rethrow new_exception_header
1261 // Will a std::bad_exception be catchable by the exception spec at
1264 // throw std::bad_exception();
1265 const __shim_type_info
* excpType
=
1266 static_cast<const __shim_type_info
*>(&typeid(std::bad_exception
));
1267 std::bad_exception be
;
1269 if (!exception_spec_can_catch(ttypeIndex
, classInfo
, ttypeEncoding
,
1270 excpType
, adjustedPtr
,
1271 unwind_exception
, base
))
1273 // We need to __cxa_end_catch for both the old exception and the
1274 // new exception. Technically we should do it in that order.
1275 // But it is expedient to do it in the opposite order:
1276 // Call __cxa_end_catch for new_exception_header
1278 // Throw std::bad_exception will __cxa_end_catch for
1279 // old_exception_header
1284 std::__terminate(t_handler
);
1288 // Personality routine for EH using the range table. Make it an alias of
1289 // __gxx_personality_v0().
1290 _LIBCXXABI_FUNC_VIS _Unwind_Reason_Code
__xlcxx_personality_v1(
1291 int version
, _Unwind_Action actions
, uint64_t exceptionClass
,
1292 _Unwind_Exception
* unwind_exception
, _Unwind_Context
* context
)
1293 __attribute__((__alias__("__gxx_personality_v0")));