1 // -*- C++ -*- The GNU C++ exception personality routine.
2 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
4 // This file is part of GCC.
6 // GCC is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3, or (at your option)
11 // GCC is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 #include <bits/c++config.h>
27 #include <bits/exception_defines.h>
29 #include "unwind-cxx.h"
32 using namespace __cxxabiv1
;
34 #include "unwind-pe.h"
37 struct lsda_header_info
41 _Unwind_Ptr ttype_base
;
42 const unsigned char *TType
;
43 const unsigned char *action_table
;
44 unsigned char ttype_encoding
;
45 unsigned char call_site_encoding
;
48 static const unsigned char *
49 parse_lsda_header (_Unwind_Context
*context
, const unsigned char *p
,
50 lsda_header_info
*info
)
53 unsigned char lpstart_encoding
;
55 info
->Start
= (context
? _Unwind_GetRegionStart (context
) : 0);
57 // Find @LPStart, the base to which landing pad offsets are relative.
58 lpstart_encoding
= *p
++;
59 if (lpstart_encoding
!= DW_EH_PE_omit
)
60 p
= read_encoded_value (context
, lpstart_encoding
, p
, &info
->LPStart
);
62 info
->LPStart
= info
->Start
;
64 // Find @TType, the base of the handler and exception spec type data.
65 info
->ttype_encoding
= *p
++;
66 if (info
->ttype_encoding
!= DW_EH_PE_omit
)
68 #if _GLIBCXX_OVERRIDE_TTYPE_ENCODING
69 /* Older ARM EABI toolchains set this value incorrectly, so use a
70 hardcoded OS-specific format. */
71 info
->ttype_encoding
= _GLIBCXX_OVERRIDE_TTYPE_ENCODING
;
73 p
= read_uleb128 (p
, &tmp
);
74 info
->TType
= p
+ tmp
;
79 // The encoding and length of the call-site table; the action table
80 // immediately follows.
81 info
->call_site_encoding
= *p
++;
82 p
= read_uleb128 (p
, &tmp
);
83 info
->action_table
= p
+ tmp
;
88 // Return an element from a type table.
90 static const std::type_info
*
91 get_ttype_entry (lsda_header_info
*info
, _uleb128_t i
)
95 i
*= size_of_encoded_value (info
->ttype_encoding
);
96 read_encoded_value_with_base (info
->ttype_encoding
, info
->ttype_base
,
97 info
->TType
- i
, &ptr
);
99 return reinterpret_cast<const std::type_info
*>(ptr
);
102 #ifdef __ARM_EABI_UNWINDER__
104 // The ABI provides a routine for matching exception object types.
105 typedef _Unwind_Control_Block _throw_typet
;
106 #define get_adjusted_ptr(catch_type, throw_type, thrown_ptr_p) \
107 (__cxa_type_match (throw_type, catch_type, false, thrown_ptr_p) \
110 // Return true if THROW_TYPE matches one if the filter types.
113 check_exception_spec(lsda_header_info
* info
, _throw_typet
* throw_type
,
114 void* thrown_ptr
, _sleb128_t filter_value
)
116 const _uleb128_t
* e
= ((const _uleb128_t
*) info
->TType
)
121 const std::type_info
* catch_type
;
126 // Zero signals the end of the list. If we've not found
127 // a match by now, then we've failed the specification.
131 tmp
= _Unwind_decode_typeinfo_ptr(info
->ttype_base
, (_Unwind_Word
) e
);
133 // Match a ttype entry.
134 catch_type
= reinterpret_cast<const std::type_info
*>(tmp
);
136 // ??? There is currently no way to ask the RTTI code about the
137 // relationship between two types without reference to a specific
138 // object. There should be; then we wouldn't need to mess with
140 if (get_adjusted_ptr(catch_type
, throw_type
, &thrown_ptr
))
143 // Advance to the next entry.
149 // Save stage1 handler information in the exception object
152 save_caught_exception(struct _Unwind_Exception
* ue_header
,
153 struct _Unwind_Context
* context
,
155 int handler_switch_value
,
156 const unsigned char* language_specific_data
,
157 _Unwind_Ptr landing_pad
,
158 const unsigned char* action_record
159 __attribute__((__unused__
)))
161 ue_header
->barrier_cache
.sp
= _Unwind_GetGR(context
, UNWIND_STACK_REG
);
162 ue_header
->barrier_cache
.bitpattern
[0] = (_uw
) thrown_ptr
;
163 ue_header
->barrier_cache
.bitpattern
[1]
164 = (_uw
) handler_switch_value
;
165 ue_header
->barrier_cache
.bitpattern
[2]
166 = (_uw
) language_specific_data
;
167 ue_header
->barrier_cache
.bitpattern
[3] = (_uw
) landing_pad
;
171 // Restore the catch handler data saved during phase1.
174 restore_caught_exception(struct _Unwind_Exception
* ue_header
,
175 int& handler_switch_value
,
176 const unsigned char*& language_specific_data
,
177 _Unwind_Ptr
& landing_pad
)
179 handler_switch_value
= (int) ue_header
->barrier_cache
.bitpattern
[1];
180 language_specific_data
=
181 (const unsigned char*) ue_header
->barrier_cache
.bitpattern
[2];
182 landing_pad
= (_Unwind_Ptr
) ue_header
->barrier_cache
.bitpattern
[3];
185 #define CONTINUE_UNWINDING \
188 if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
189 return _URC_FAILURE; \
190 return _URC_CONTINUE_UNWIND; \
194 // Return true if the filter spec is empty, ie throw().
197 empty_exception_spec (lsda_header_info
*info
, _Unwind_Sword filter_value
)
199 const _Unwind_Word
* e
= ((const _Unwind_Word
*) info
->TType
)
206 typedef const std::type_info _throw_typet
;
209 // Given the thrown type THROW_TYPE, pointer to a variable containing a
210 // pointer to the exception object THROWN_PTR_P and a type CATCH_TYPE to
211 // compare against, return whether or not there is a match and if so,
212 // update *THROWN_PTR_P.
215 get_adjusted_ptr (const std::type_info
*catch_type
,
216 const std::type_info
*throw_type
,
219 void *thrown_ptr
= *thrown_ptr_p
;
221 // Pointer types need to adjust the actual pointer, not
222 // the pointer to pointer that is the exception object.
223 // This also has the effect of passing pointer types
224 // "by value" through the __cxa_begin_catch return value.
225 if (throw_type
->__is_pointer_p ())
226 thrown_ptr
= *(void **) thrown_ptr
;
228 if (catch_type
->__do_catch (throw_type
, &thrown_ptr
, 1))
230 *thrown_ptr_p
= thrown_ptr
;
237 // Return true if THROW_TYPE matches one if the filter types.
240 check_exception_spec(lsda_header_info
* info
, _throw_typet
* throw_type
,
241 void* thrown_ptr
, _sleb128_t filter_value
)
243 const unsigned char *e
= info
->TType
- filter_value
- 1;
247 const std::type_info
*catch_type
;
250 e
= read_uleb128 (e
, &tmp
);
252 // Zero signals the end of the list. If we've not found
253 // a match by now, then we've failed the specification.
257 // Match a ttype entry.
258 catch_type
= get_ttype_entry (info
, tmp
);
260 // ??? There is currently no way to ask the RTTI code about the
261 // relationship between two types without reference to a specific
262 // object. There should be; then we wouldn't need to mess with
264 if (get_adjusted_ptr (catch_type
, throw_type
, &thrown_ptr
))
270 // Save stage1 handler information in the exception object
273 save_caught_exception(struct _Unwind_Exception
* ue_header
,
274 struct _Unwind_Context
* context
275 __attribute__((__unused__
)),
277 int handler_switch_value
,
278 const unsigned char* language_specific_data
,
279 _Unwind_Ptr landing_pad
__attribute__((__unused__
)),
280 const unsigned char* action_record
)
282 __cxa_exception
* xh
= __get_exception_header_from_ue(ue_header
);
284 xh
->handlerSwitchValue
= handler_switch_value
;
285 xh
->actionRecord
= action_record
;
286 xh
->languageSpecificData
= language_specific_data
;
287 xh
->adjustedPtr
= thrown_ptr
;
289 // ??? Completely unknown what this field is supposed to be for.
290 // ??? Need to cache TType encoding base for call_unexpected.
291 xh
->catchTemp
= landing_pad
;
295 // Restore the catch handler information saved during phase1.
298 restore_caught_exception(struct _Unwind_Exception
* ue_header
,
299 int& handler_switch_value
,
300 const unsigned char*& language_specific_data
,
301 _Unwind_Ptr
& landing_pad
)
303 __cxa_exception
* xh
= __get_exception_header_from_ue(ue_header
);
304 handler_switch_value
= xh
->handlerSwitchValue
;
305 language_specific_data
= xh
->languageSpecificData
;
306 landing_pad
= (_Unwind_Ptr
) xh
->catchTemp
;
309 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
311 // Return true if the filter spec is empty, ie throw().
314 empty_exception_spec (lsda_header_info
*info
, _Unwind_Sword filter_value
)
316 const unsigned char *e
= info
->TType
- filter_value
- 1;
319 e
= read_uleb128 (e
, &tmp
);
323 #endif // !__ARM_EABI_UNWINDER__
328 // Using a different personality function name causes link failures
329 // when trying to mix code using different exception handling models.
330 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
331 #define PERSONALITY_FUNCTION __gxx_personality_sj0
332 #define __builtin_eh_return_data_regno(x) x
333 #elif defined(__SEH__) && !defined (_GLIBCXX_SJLJ_EXCEPTIONS)
334 #define PERSONALITY_FUNCTION __gxx_personality_imp
336 #define PERSONALITY_FUNCTION __gxx_personality_v0
339 #if defined (__SEH__) && !defined (_GLIBCXX_SJLJ_EXCEPTIONS)
345 #ifdef __ARM_EABI_UNWINDER__
346 PERSONALITY_FUNCTION (_Unwind_State state
,
347 struct _Unwind_Exception
* ue_header
,
348 struct _Unwind_Context
* context
)
350 PERSONALITY_FUNCTION (int version
,
351 _Unwind_Action actions
,
352 _Unwind_Exception_Class exception_class
,
353 struct _Unwind_Exception
*ue_header
,
354 struct _Unwind_Context
*context
)
357 enum found_handler_type
365 lsda_header_info info
;
366 const unsigned char *language_specific_data
;
367 const unsigned char *action_record
;
368 const unsigned char *p
;
369 _Unwind_Ptr landing_pad
, ip
;
370 int handler_switch_value
;
371 void* thrown_ptr
= 0;
372 bool foreign_exception
;
373 int ip_before_insn
= 0;
375 #ifdef __ARM_EABI_UNWINDER__
376 _Unwind_Action actions
;
378 switch (state
& _US_ACTION_MASK
)
380 case _US_VIRTUAL_UNWIND_FRAME
:
381 actions
= _UA_SEARCH_PHASE
;
384 case _US_UNWIND_FRAME_STARTING
:
385 actions
= _UA_CLEANUP_PHASE
;
386 if (!(state
& _US_FORCE_UNWIND
)
387 && ue_header
->barrier_cache
.sp
== _Unwind_GetGR(context
,
389 actions
|= _UA_HANDLER_FRAME
;
392 case _US_UNWIND_FRAME_RESUME
:
399 actions
|= state
& _US_FORCE_UNWIND
;
401 // We don't know which runtime we're working with, so can't check this.
402 // However the ABI routines hide this from us, and we don't actually need
404 foreign_exception
= false;
406 // The dwarf unwinder assumes the context structure holds things like the
407 // function and LSDA pointers. The ARM implementation caches these in
408 // the exception header (UCB). To avoid rewriting everything we make a
409 // virtual scratch register point at the UCB.
410 ip
= (_Unwind_Ptr
) ue_header
;
411 _Unwind_SetGR(context
, UNWIND_POINTER_REG
, ip
);
413 __cxa_exception
* xh
= __get_exception_header_from_ue(ue_header
);
415 // Interface version check.
417 return _URC_FATAL_PHASE1_ERROR
;
418 foreign_exception
= !__is_gxx_exception_class(exception_class
);
421 // Shortcut for phase 2 found handler for domestic exception.
422 if (actions
== (_UA_CLEANUP_PHASE
| _UA_HANDLER_FRAME
)
423 && !foreign_exception
)
425 restore_caught_exception(ue_header
, handler_switch_value
,
426 language_specific_data
, landing_pad
);
427 found_type
= (landing_pad
== 0 ? found_terminate
: found_handler
);
428 goto install_context
;
431 language_specific_data
= (const unsigned char *)
432 _Unwind_GetLanguageSpecificData (context
);
434 // If no LSDA, then there are no handlers or cleanups.
435 if (! language_specific_data
)
438 // Parse the LSDA header.
439 p
= parse_lsda_header (context
, language_specific_data
, &info
);
440 info
.ttype_base
= base_of_encoded_value (info
.ttype_encoding
, context
);
441 #ifdef _GLIBCXX_HAVE_GETIPINFO
442 ip
= _Unwind_GetIPInfo (context
, &ip_before_insn
);
444 ip
= _Unwind_GetIP (context
);
446 if (! ip_before_insn
)
450 handler_switch_value
= 0;
452 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
453 // The given "IP" is an index into the call-site table, with two
454 // exceptions -- -1 means no-action, and 0 means terminate. But
455 // since we're using uleb128 values, we've not got random access
458 return _URC_CONTINUE_UNWIND
;
461 // Fall through to set found_terminate.
465 _uleb128_t cs_lp
, cs_action
;
468 p
= read_uleb128 (p
, &cs_lp
);
469 p
= read_uleb128 (p
, &cs_action
);
473 // Can never have null landing pad for sjlj -- that would have
474 // been indicated by a -1 call site index.
475 landing_pad
= cs_lp
+ 1;
477 action_record
= info
.action_table
+ cs_action
- 1;
478 goto found_something
;
481 // Search the call-site table for the action associated with this IP.
482 while (p
< info
.action_table
)
484 _Unwind_Ptr cs_start
, cs_len
, cs_lp
;
485 _uleb128_t cs_action
;
487 // Note that all call-site encodings are "absolute" displacements.
488 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_start
);
489 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_len
);
490 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_lp
);
491 p
= read_uleb128 (p
, &cs_action
);
493 // The table is sorted, so if we've passed the ip, stop.
494 if (ip
< info
.Start
+ cs_start
)
495 p
= info
.action_table
;
496 else if (ip
< info
.Start
+ cs_start
+ cs_len
)
499 landing_pad
= info
.LPStart
+ cs_lp
;
501 action_record
= info
.action_table
+ cs_action
- 1;
502 goto found_something
;
505 #endif // _GLIBCXX_SJLJ_EXCEPTIONS
507 // If ip is not present in the table, call terminate. This is for
508 // a destructor inside a cleanup, or a library routine the compiler
509 // was not expecting to throw.
510 found_type
= found_terminate
;
514 if (landing_pad
== 0)
516 // If ip is present, and has a null landing pad, there are
517 // no cleanups or handlers to be run.
518 found_type
= found_nothing
;
520 else if (action_record
== 0)
522 // If ip is present, has a non-null landing pad, and a null
523 // action table offset, then there are only cleanups present.
524 // Cleanups use a zero switch value, as set above.
525 found_type
= found_cleanup
;
529 // Otherwise we have a catch handler or exception specification.
531 _sleb128_t ar_filter
, ar_disp
;
532 const std::type_info
* catch_type
;
533 _throw_typet
* throw_type
;
534 bool saw_cleanup
= false;
535 bool saw_handler
= false;
537 #ifdef __ARM_EABI_UNWINDER__
538 // ??? How does this work - more importantly, how does it interact with
539 // dependent exceptions?
540 throw_type
= ue_header
;
541 if (actions
& _UA_FORCE_UNWIND
)
543 __GXX_INIT_FORCED_UNWIND_CLASS(ue_header
->exception_class
);
545 else if (!foreign_exception
)
546 thrown_ptr
= __get_object_from_ue (ue_header
);
549 // During forced unwinding, match a magic exception type.
550 if (actions
& _UA_FORCE_UNWIND
)
552 throw_type
= &typeid(abi::__forced_unwind
);
554 // With a foreign exception class, there's no exception type.
555 // ??? What to do about GNU Java and GNU Ada exceptions?
556 else if (foreign_exception
)
558 throw_type
= &typeid(abi::__foreign_exception
);
563 thrown_ptr
= __get_object_from_ue (ue_header
);
564 throw_type
= __get_exception_header_from_obj
565 (thrown_ptr
)->exceptionType
;
572 p
= read_sleb128 (p
, &ar_filter
);
573 read_sleb128 (p
, &ar_disp
);
577 // Zero filter values are cleanups.
580 else if (ar_filter
> 0)
582 // Positive filter values are handlers.
583 catch_type
= get_ttype_entry (&info
, ar_filter
);
585 // Null catch type is a catch-all handler; we can catch foreign
586 // exceptions with this. Otherwise we must match types.
589 && get_adjusted_ptr (catch_type
, throw_type
,
598 // Negative filter values are exception specifications.
599 // ??? How do foreign exceptions fit in? As far as I can
600 // see we can't match because there's no __cxa_exception
601 // object to stuff bits in for __cxa_call_unexpected to use.
602 // Allow them iff the exception spec is non-empty. I.e.
603 // a throw() specification results in __unexpected.
605 && !(actions
& _UA_FORCE_UNWIND
)
606 && !foreign_exception
)
607 ? ! check_exception_spec (&info
, throw_type
, thrown_ptr
,
609 : empty_exception_spec (&info
, ar_filter
))
618 action_record
= p
+ ar_disp
;
623 handler_switch_value
= ar_filter
;
624 found_type
= found_handler
;
627 found_type
= (saw_cleanup
? found_cleanup
: found_nothing
);
631 if (found_type
== found_nothing
)
634 if (actions
& _UA_SEARCH_PHASE
)
636 if (found_type
== found_cleanup
)
639 // For domestic exceptions, we cache data from phase 1 for phase 2.
640 if (!foreign_exception
)
642 save_caught_exception(ue_header
, context
, thrown_ptr
,
643 handler_switch_value
, language_specific_data
,
644 landing_pad
, action_record
);
646 return _URC_HANDLER_FOUND
;
651 // We can't use any of the cxa routines with foreign exceptions,
652 // because they all expect ue_header to be a struct __cxa_exception.
653 // So in that case, call terminate or unexpected directly.
654 if ((actions
& _UA_FORCE_UNWIND
)
655 || foreign_exception
)
657 if (found_type
== found_terminate
)
659 else if (handler_switch_value
< 0)
662 { std::unexpected (); }
664 { std::terminate (); }
669 if (found_type
== found_terminate
)
670 __cxa_call_terminate(ue_header
);
672 // Cache the TType base value for __cxa_call_unexpected, as we won't
673 // have an _Unwind_Context then.
674 if (handler_switch_value
< 0)
676 parse_lsda_header (context
, language_specific_data
, &info
);
677 info
.ttype_base
= base_of_encoded_value (info
.ttype_encoding
,
680 #ifdef __ARM_EABI_UNWINDER__
681 const _Unwind_Word
* e
;
684 e
= ((const _Unwind_Word
*) info
.TType
) - handler_switch_value
- 1;
685 // Count the number of rtti objects.
691 ue_header
->barrier_cache
.bitpattern
[1] = n
;
693 ue_header
->barrier_cache
.bitpattern
[2] = info
.ttype_base
;
695 ue_header
->barrier_cache
.bitpattern
[3] = 4;
697 ue_header
->barrier_cache
.bitpattern
[4] = (_Unwind_Word
) e
;
699 xh
->catchTemp
= base_of_encoded_value (info
.ttype_encoding
, context
);
704 /* For targets with pointers smaller than the word size, we must extend the
705 pointer, and this extension is target dependent. */
706 _Unwind_SetGR (context
, __builtin_eh_return_data_regno (0),
707 __builtin_extend_pointer (ue_header
));
708 _Unwind_SetGR (context
, __builtin_eh_return_data_regno (1),
709 handler_switch_value
);
710 _Unwind_SetIP (context
, landing_pad
);
711 #ifdef __ARM_EABI_UNWINDER__
712 if (found_type
== found_cleanup
)
713 __cxa_begin_cleanup(ue_header
);
715 return _URC_INSTALL_CONTEXT
;
718 /* The ARM EABI implementation of __cxa_call_unexpected is in a
719 different file so that the personality routine (PR) can be used
720 standalone. The generic routine shared datastructures with the PR
721 so it is most convenient to implement it here. */
722 #ifndef __ARM_EABI_UNWINDER__
724 __cxa_call_unexpected (void *exc_obj_in
)
726 _Unwind_Exception
*exc_obj
727 = reinterpret_cast <_Unwind_Exception
*>(exc_obj_in
);
729 __cxa_begin_catch (exc_obj
);
731 // This function is a handler for our exception argument. If we exit
732 // by throwing a different exception, we'll need the original cleaned up.
733 struct end_catch_protect
735 end_catch_protect() { }
736 ~end_catch_protect() { __cxa_end_catch(); }
737 } end_catch_protect_obj
;
739 lsda_header_info info
;
740 __cxa_exception
*xh
= __get_exception_header_from_ue (exc_obj
);
741 const unsigned char *xh_lsda
;
742 _Unwind_Sword xh_switch_value
;
743 std::terminate_handler xh_terminate_handler
;
745 // If the unexpectedHandler rethrows the exception (e.g. to categorize it),
746 // it will clobber data about the current handler. So copy the data out now.
747 xh_lsda
= xh
->languageSpecificData
;
748 xh_switch_value
= xh
->handlerSwitchValue
;
749 xh_terminate_handler
= xh
->terminateHandler
;
750 info
.ttype_base
= (_Unwind_Ptr
) xh
->catchTemp
;
753 { __unexpected (xh
->unexpectedHandler
); }
756 // Get the exception thrown from unexpected.
758 __cxa_eh_globals
*globals
= __cxa_get_globals_fast ();
759 __cxa_exception
*new_xh
= globals
->caughtExceptions
;
760 void *new_ptr
= __get_object_from_ambiguous_exception (new_xh
);
762 // We don't quite have enough stuff cached; re-parse the LSDA.
763 parse_lsda_header (0, xh_lsda
, &info
);
765 // If this new exception meets the exception spec, allow it.
766 if (check_exception_spec (&info
, __get_exception_header_from_obj
767 (new_ptr
)->exceptionType
,
768 new_ptr
, xh_switch_value
))
769 { __throw_exception_again
; }
771 // If the exception spec allows std::bad_exception, throw that.
772 // We don't have a thrown object to compare against, but since
773 // bad_exception doesn't have virtual bases, that's OK; just pass 0.
774 #if defined(__EXCEPTIONS) && defined(__GXX_RTTI)
775 const std::type_info
&bad_exc
= typeid (std::bad_exception
);
776 if (check_exception_spec (&info
, &bad_exc
, 0, xh_switch_value
))
777 throw std::bad_exception();
781 __terminate (xh_terminate_handler
);
786 #if defined (__SEH__) && !defined (_GLIBCXX_SJLJ_EXCEPTIONS)
788 EXCEPTION_DISPOSITION
789 __gxx_personality_seh0 (PEXCEPTION_RECORD ms_exc
, void *this_frame
,
790 PCONTEXT ms_orig_context
, PDISPATCHER_CONTEXT ms_disp
)
792 return _GCC_specific_handler (ms_exc
, this_frame
, ms_orig_context
,
793 ms_disp
, __gxx_personality_imp
);
797 } // namespace __cxxabiv1