1 // -*- C++ -*- The GNU C++ exception personality routine.
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // Free Software Foundation, Inc.
6 // This file is part of GCC.
8 // GCC is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3, or (at your option)
13 // GCC is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 #include <bits/c++config.h>
29 #include <bits/exception_defines.h>
31 #include "unwind-cxx.h"
34 using namespace __cxxabiv1
;
36 #include "unwind-pe.h"
39 struct lsda_header_info
43 _Unwind_Ptr ttype_base
;
44 const unsigned char *TType
;
45 const unsigned char *action_table
;
46 unsigned char ttype_encoding
;
47 unsigned char call_site_encoding
;
50 static const unsigned char *
51 parse_lsda_header (_Unwind_Context
*context
, const unsigned char *p
,
52 lsda_header_info
*info
)
55 unsigned char lpstart_encoding
;
57 info
->Start
= (context
? _Unwind_GetRegionStart (context
) : 0);
59 // Find @LPStart, the base to which landing pad offsets are relative.
60 lpstart_encoding
= *p
++;
61 if (lpstart_encoding
!= DW_EH_PE_omit
)
62 p
= read_encoded_value (context
, lpstart_encoding
, p
, &info
->LPStart
);
64 info
->LPStart
= info
->Start
;
66 // Find @TType, the base of the handler and exception spec type data.
67 info
->ttype_encoding
= *p
++;
68 if (info
->ttype_encoding
!= DW_EH_PE_omit
)
70 #if _GLIBCXX_OVERRIDE_TTYPE_ENCODING
71 /* Older ARM EABI toolchains set this value incorrectly, so use a
72 hardcoded OS-specific format. */
73 info
->ttype_encoding
= _GLIBCXX_OVERRIDE_TTYPE_ENCODING
;
75 p
= read_uleb128 (p
, &tmp
);
76 info
->TType
= p
+ tmp
;
81 // The encoding and length of the call-site table; the action table
82 // immediately follows.
83 info
->call_site_encoding
= *p
++;
84 p
= read_uleb128 (p
, &tmp
);
85 info
->action_table
= p
+ tmp
;
90 // Return an element from a type table.
92 static const std::type_info
*
93 get_ttype_entry (lsda_header_info
*info
, _uleb128_t i
)
97 i
*= size_of_encoded_value (info
->ttype_encoding
);
98 read_encoded_value_with_base (info
->ttype_encoding
, info
->ttype_base
,
99 info
->TType
- i
, &ptr
);
101 return reinterpret_cast<const std::type_info
*>(ptr
);
104 #ifdef __ARM_EABI_UNWINDER__
106 // The ABI provides a routine for matching exception object types.
107 typedef _Unwind_Control_Block _throw_typet
;
108 #define get_adjusted_ptr(catch_type, throw_type, thrown_ptr_p) \
109 (__cxa_type_match (throw_type, catch_type, false, thrown_ptr_p) \
112 // Return true if THROW_TYPE matches one if the filter types.
115 check_exception_spec(lsda_header_info
* info
, _throw_typet
* throw_type
,
116 void* thrown_ptr
, _sleb128_t filter_value
)
118 const _uleb128_t
* e
= ((const _uleb128_t
*) info
->TType
)
123 const std::type_info
* catch_type
;
128 // Zero signals the end of the list. If we've not found
129 // a match by now, then we've failed the specification.
133 tmp
= _Unwind_decode_typeinfo_ptr(info
->ttype_base
, (_Unwind_Word
) e
);
135 // Match a ttype entry.
136 catch_type
= reinterpret_cast<const std::type_info
*>(tmp
);
138 // ??? There is currently no way to ask the RTTI code about the
139 // relationship between two types without reference to a specific
140 // object. There should be; then we wouldn't need to mess with
142 if (get_adjusted_ptr(catch_type
, throw_type
, &thrown_ptr
))
145 // Advance to the next entry.
151 // Save stage1 handler information in the exception object
154 save_caught_exception(struct _Unwind_Exception
* ue_header
,
155 struct _Unwind_Context
* context
,
157 int handler_switch_value
,
158 const unsigned char* language_specific_data
,
159 _Unwind_Ptr landing_pad
,
160 const unsigned char* action_record
161 __attribute__((__unused__
)))
163 ue_header
->barrier_cache
.sp
= _Unwind_GetGR(context
, UNWIND_STACK_REG
);
164 ue_header
->barrier_cache
.bitpattern
[0] = (_uw
) thrown_ptr
;
165 ue_header
->barrier_cache
.bitpattern
[1]
166 = (_uw
) handler_switch_value
;
167 ue_header
->barrier_cache
.bitpattern
[2]
168 = (_uw
) language_specific_data
;
169 ue_header
->barrier_cache
.bitpattern
[3] = (_uw
) landing_pad
;
173 // Restore the catch handler data saved during phase1.
176 restore_caught_exception(struct _Unwind_Exception
* ue_header
,
177 int& handler_switch_value
,
178 const unsigned char*& language_specific_data
,
179 _Unwind_Ptr
& landing_pad
)
181 handler_switch_value
= (int) ue_header
->barrier_cache
.bitpattern
[1];
182 language_specific_data
=
183 (const unsigned char*) ue_header
->barrier_cache
.bitpattern
[2];
184 landing_pad
= (_Unwind_Ptr
) ue_header
->barrier_cache
.bitpattern
[3];
187 #define CONTINUE_UNWINDING \
190 if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
191 return _URC_FAILURE; \
192 return _URC_CONTINUE_UNWIND; \
196 // Return true if the filter spec is empty, ie throw().
199 empty_exception_spec (lsda_header_info
*info
, _Unwind_Sword filter_value
)
201 const _Unwind_Word
* e
= ((const _Unwind_Word
*) info
->TType
)
208 typedef const std::type_info _throw_typet
;
211 // Given the thrown type THROW_TYPE, pointer to a variable containing a
212 // pointer to the exception object THROWN_PTR_P and a type CATCH_TYPE to
213 // compare against, return whether or not there is a match and if so,
214 // update *THROWN_PTR_P.
217 get_adjusted_ptr (const std::type_info
*catch_type
,
218 const std::type_info
*throw_type
,
221 void *thrown_ptr
= *thrown_ptr_p
;
223 // Pointer types need to adjust the actual pointer, not
224 // the pointer to pointer that is the exception object.
225 // This also has the effect of passing pointer types
226 // "by value" through the __cxa_begin_catch return value.
227 if (throw_type
->__is_pointer_p ())
228 thrown_ptr
= *(void **) thrown_ptr
;
230 if (catch_type
->__do_catch (throw_type
, &thrown_ptr
, 1))
232 *thrown_ptr_p
= thrown_ptr
;
239 // Return true if THROW_TYPE matches one if the filter types.
242 check_exception_spec(lsda_header_info
* info
, _throw_typet
* throw_type
,
243 void* thrown_ptr
, _sleb128_t filter_value
)
245 const unsigned char *e
= info
->TType
- filter_value
- 1;
249 const std::type_info
*catch_type
;
252 e
= read_uleb128 (e
, &tmp
);
254 // Zero signals the end of the list. If we've not found
255 // a match by now, then we've failed the specification.
259 // Match a ttype entry.
260 catch_type
= get_ttype_entry (info
, tmp
);
262 // ??? There is currently no way to ask the RTTI code about the
263 // relationship between two types without reference to a specific
264 // object. There should be; then we wouldn't need to mess with
266 if (get_adjusted_ptr (catch_type
, throw_type
, &thrown_ptr
))
272 // Save stage1 handler information in the exception object
275 save_caught_exception(struct _Unwind_Exception
* ue_header
,
276 struct _Unwind_Context
* context
277 __attribute__((__unused__
)),
279 int handler_switch_value
,
280 const unsigned char* language_specific_data
,
281 _Unwind_Ptr landing_pad
__attribute__((__unused__
)),
282 const unsigned char* action_record
)
284 __cxa_exception
* xh
= __get_exception_header_from_ue(ue_header
);
286 xh
->handlerSwitchValue
= handler_switch_value
;
287 xh
->actionRecord
= action_record
;
288 xh
->languageSpecificData
= language_specific_data
;
289 xh
->adjustedPtr
= thrown_ptr
;
291 // ??? Completely unknown what this field is supposed to be for.
292 // ??? Need to cache TType encoding base for call_unexpected.
293 xh
->catchTemp
= landing_pad
;
297 // Restore the catch handler information saved during phase1.
300 restore_caught_exception(struct _Unwind_Exception
* ue_header
,
301 int& handler_switch_value
,
302 const unsigned char*& language_specific_data
,
303 _Unwind_Ptr
& landing_pad
)
305 __cxa_exception
* xh
= __get_exception_header_from_ue(ue_header
);
306 handler_switch_value
= xh
->handlerSwitchValue
;
307 language_specific_data
= xh
->languageSpecificData
;
308 landing_pad
= (_Unwind_Ptr
) xh
->catchTemp
;
311 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
313 // Return true if the filter spec is empty, ie throw().
316 empty_exception_spec (lsda_header_info
*info
, _Unwind_Sword filter_value
)
318 const unsigned char *e
= info
->TType
- filter_value
- 1;
321 e
= read_uleb128 (e
, &tmp
);
325 #endif // !__ARM_EABI_UNWINDER__
330 // Using a different personality function name causes link failures
331 // when trying to mix code using different exception handling models.
332 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
333 #define PERSONALITY_FUNCTION __gxx_personality_sj0
334 #define __builtin_eh_return_data_regno(x) x
335 #elif defined(__SEH__)
336 #define PERSONALITY_FUNCTION __gxx_personality_imp
338 #define PERSONALITY_FUNCTION __gxx_personality_v0
347 #ifdef __ARM_EABI_UNWINDER__
348 PERSONALITY_FUNCTION (_Unwind_State state
,
349 struct _Unwind_Exception
* ue_header
,
350 struct _Unwind_Context
* context
)
352 PERSONALITY_FUNCTION (int version
,
353 _Unwind_Action actions
,
354 _Unwind_Exception_Class exception_class
,
355 struct _Unwind_Exception
*ue_header
,
356 struct _Unwind_Context
*context
)
359 enum found_handler_type
367 lsda_header_info info
;
368 const unsigned char *language_specific_data
;
369 const unsigned char *action_record
;
370 const unsigned char *p
;
371 _Unwind_Ptr landing_pad
, ip
;
372 int handler_switch_value
;
373 void* thrown_ptr
= 0;
374 bool foreign_exception
;
375 int ip_before_insn
= 0;
377 #ifdef __ARM_EABI_UNWINDER__
378 _Unwind_Action actions
;
380 switch (state
& _US_ACTION_MASK
)
382 case _US_VIRTUAL_UNWIND_FRAME
:
383 actions
= _UA_SEARCH_PHASE
;
386 case _US_UNWIND_FRAME_STARTING
:
387 actions
= _UA_CLEANUP_PHASE
;
388 if (!(state
& _US_FORCE_UNWIND
)
389 && ue_header
->barrier_cache
.sp
== _Unwind_GetGR(context
,
391 actions
|= _UA_HANDLER_FRAME
;
394 case _US_UNWIND_FRAME_RESUME
:
401 actions
|= state
& _US_FORCE_UNWIND
;
403 // We don't know which runtime we're working with, so can't check this.
404 // However the ABI routines hide this from us, and we don't actually need
406 foreign_exception
= false;
408 // The dwarf unwinder assumes the context structure holds things like the
409 // function and LSDA pointers. The ARM implementation caches these in
410 // the exception header (UCB). To avoid rewriting everything we make a
411 // virtual scratch register point at the UCB.
412 ip
= (_Unwind_Ptr
) ue_header
;
413 _Unwind_SetGR(context
, UNWIND_POINTER_REG
, ip
);
415 __cxa_exception
* xh
= __get_exception_header_from_ue(ue_header
);
417 // Interface version check.
419 return _URC_FATAL_PHASE1_ERROR
;
420 foreign_exception
= !__is_gxx_exception_class(exception_class
);
423 // Shortcut for phase 2 found handler for domestic exception.
424 if (actions
== (_UA_CLEANUP_PHASE
| _UA_HANDLER_FRAME
)
425 && !foreign_exception
)
427 restore_caught_exception(ue_header
, handler_switch_value
,
428 language_specific_data
, landing_pad
);
429 found_type
= (landing_pad
== 0 ? found_terminate
: found_handler
);
430 goto install_context
;
433 language_specific_data
= (const unsigned char *)
434 _Unwind_GetLanguageSpecificData (context
);
436 // If no LSDA, then there are no handlers or cleanups.
437 if (! language_specific_data
)
440 // Parse the LSDA header.
441 p
= parse_lsda_header (context
, language_specific_data
, &info
);
442 info
.ttype_base
= base_of_encoded_value (info
.ttype_encoding
, context
);
443 #ifdef _GLIBCXX_HAVE_GETIPINFO
444 ip
= _Unwind_GetIPInfo (context
, &ip_before_insn
);
446 ip
= _Unwind_GetIP (context
);
448 if (! ip_before_insn
)
452 handler_switch_value
= 0;
454 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
455 // The given "IP" is an index into the call-site table, with two
456 // exceptions -- -1 means no-action, and 0 means terminate. But
457 // since we're using uleb128 values, we've not got random access
460 return _URC_CONTINUE_UNWIND
;
463 // Fall through to set found_terminate.
467 _uleb128_t cs_lp
, cs_action
;
470 p
= read_uleb128 (p
, &cs_lp
);
471 p
= read_uleb128 (p
, &cs_action
);
475 // Can never have null landing pad for sjlj -- that would have
476 // been indicated by a -1 call site index.
477 landing_pad
= cs_lp
+ 1;
479 action_record
= info
.action_table
+ cs_action
- 1;
480 goto found_something
;
483 // Search the call-site table for the action associated with this IP.
484 while (p
< info
.action_table
)
486 _Unwind_Ptr cs_start
, cs_len
, cs_lp
;
487 _uleb128_t cs_action
;
489 // Note that all call-site encodings are "absolute" displacements.
490 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_start
);
491 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_len
);
492 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_lp
);
493 p
= read_uleb128 (p
, &cs_action
);
495 // The table is sorted, so if we've passed the ip, stop.
496 if (ip
< info
.Start
+ cs_start
)
497 p
= info
.action_table
;
498 else if (ip
< info
.Start
+ cs_start
+ cs_len
)
501 landing_pad
= info
.LPStart
+ cs_lp
;
503 action_record
= info
.action_table
+ cs_action
- 1;
504 goto found_something
;
507 #endif // _GLIBCXX_SJLJ_EXCEPTIONS
509 // If ip is not present in the table, call terminate. This is for
510 // a destructor inside a cleanup, or a library routine the compiler
511 // was not expecting to throw.
512 found_type
= found_terminate
;
516 if (landing_pad
== 0)
518 // If ip is present, and has a null landing pad, there are
519 // no cleanups or handlers to be run.
520 found_type
= found_nothing
;
522 else if (action_record
== 0)
524 // If ip is present, has a non-null landing pad, and a null
525 // action table offset, then there are only cleanups present.
526 // Cleanups use a zero switch value, as set above.
527 found_type
= found_cleanup
;
531 // Otherwise we have a catch handler or exception specification.
533 _sleb128_t ar_filter
, ar_disp
;
534 const std::type_info
* catch_type
;
535 _throw_typet
* throw_type
;
536 bool saw_cleanup
= false;
537 bool saw_handler
= false;
539 #ifdef __ARM_EABI_UNWINDER__
540 // ??? How does this work - more importantly, how does it interact with
541 // dependent exceptions?
542 throw_type
= ue_header
;
543 if (actions
& _UA_FORCE_UNWIND
)
545 __GXX_INIT_FORCED_UNWIND_CLASS(ue_header
->exception_class
);
547 else if (!foreign_exception
)
548 thrown_ptr
= __get_object_from_ue (ue_header
);
551 // During forced unwinding, match a magic exception type.
552 if (actions
& _UA_FORCE_UNWIND
)
554 throw_type
= &typeid(abi::__forced_unwind
);
556 // With a foreign exception class, there's no exception type.
557 // ??? What to do about GNU Java and GNU Ada exceptions?
558 else if (foreign_exception
)
560 throw_type
= &typeid(abi::__foreign_exception
);
565 thrown_ptr
= __get_object_from_ue (ue_header
);
566 throw_type
= __get_exception_header_from_obj
567 (thrown_ptr
)->exceptionType
;
574 p
= read_sleb128 (p
, &ar_filter
);
575 read_sleb128 (p
, &ar_disp
);
579 // Zero filter values are cleanups.
582 else if (ar_filter
> 0)
584 // Positive filter values are handlers.
585 catch_type
= get_ttype_entry (&info
, ar_filter
);
587 // Null catch type is a catch-all handler; we can catch foreign
588 // exceptions with this. Otherwise we must match types.
591 && get_adjusted_ptr (catch_type
, throw_type
,
600 // Negative filter values are exception specifications.
601 // ??? How do foreign exceptions fit in? As far as I can
602 // see we can't match because there's no __cxa_exception
603 // object to stuff bits in for __cxa_call_unexpected to use.
604 // Allow them iff the exception spec is non-empty. I.e.
605 // a throw() specification results in __unexpected.
607 && !(actions
& _UA_FORCE_UNWIND
)
608 && !foreign_exception
)
609 ? ! check_exception_spec (&info
, throw_type
, thrown_ptr
,
611 : empty_exception_spec (&info
, ar_filter
))
620 action_record
= p
+ ar_disp
;
625 handler_switch_value
= ar_filter
;
626 found_type
= found_handler
;
629 found_type
= (saw_cleanup
? found_cleanup
: found_nothing
);
633 if (found_type
== found_nothing
)
636 if (actions
& _UA_SEARCH_PHASE
)
638 if (found_type
== found_cleanup
)
641 // For domestic exceptions, we cache data from phase 1 for phase 2.
642 if (!foreign_exception
)
644 save_caught_exception(ue_header
, context
, thrown_ptr
,
645 handler_switch_value
, language_specific_data
,
646 landing_pad
, action_record
);
648 return _URC_HANDLER_FOUND
;
653 // We can't use any of the cxa routines with foreign exceptions,
654 // because they all expect ue_header to be a struct __cxa_exception.
655 // So in that case, call terminate or unexpected directly.
656 if ((actions
& _UA_FORCE_UNWIND
)
657 || foreign_exception
)
659 if (found_type
== found_terminate
)
661 else if (handler_switch_value
< 0)
664 { std::unexpected (); }
666 { std::terminate (); }
671 if (found_type
== found_terminate
)
672 __cxa_call_terminate(ue_header
);
674 // Cache the TType base value for __cxa_call_unexpected, as we won't
675 // have an _Unwind_Context then.
676 if (handler_switch_value
< 0)
678 parse_lsda_header (context
, language_specific_data
, &info
);
679 info
.ttype_base
= base_of_encoded_value (info
.ttype_encoding
,
682 #ifdef __ARM_EABI_UNWINDER__
683 const _Unwind_Word
* e
;
686 e
= ((const _Unwind_Word
*) info
.TType
) - handler_switch_value
- 1;
687 // Count the number of rtti objects.
693 ue_header
->barrier_cache
.bitpattern
[1] = n
;
695 ue_header
->barrier_cache
.bitpattern
[2] = info
.ttype_base
;
697 ue_header
->barrier_cache
.bitpattern
[3] = 4;
699 ue_header
->barrier_cache
.bitpattern
[4] = (_Unwind_Word
) e
;
701 xh
->catchTemp
= base_of_encoded_value (info
.ttype_encoding
, context
);
706 /* For targets with pointers smaller than the word size, we must extend the
707 pointer, and this extension is target dependent. */
708 _Unwind_SetGR (context
, __builtin_eh_return_data_regno (0),
709 __builtin_extend_pointer (ue_header
));
710 _Unwind_SetGR (context
, __builtin_eh_return_data_regno (1),
711 handler_switch_value
);
712 _Unwind_SetIP (context
, landing_pad
);
713 #ifdef __ARM_EABI_UNWINDER__
714 if (found_type
== found_cleanup
)
715 __cxa_begin_cleanup(ue_header
);
717 return _URC_INSTALL_CONTEXT
;
720 /* The ARM EABI implementation of __cxa_call_unexpected is in a
721 different file so that the personality routine (PR) can be used
722 standalone. The generic routine shared datastructures with the PR
723 so it is most convenient to implement it here. */
724 #ifndef __ARM_EABI_UNWINDER__
726 __cxa_call_unexpected (void *exc_obj_in
)
728 _Unwind_Exception
*exc_obj
729 = reinterpret_cast <_Unwind_Exception
*>(exc_obj_in
);
731 __cxa_begin_catch (exc_obj
);
733 // This function is a handler for our exception argument. If we exit
734 // by throwing a different exception, we'll need the original cleaned up.
735 struct end_catch_protect
737 end_catch_protect() { }
738 ~end_catch_protect() { __cxa_end_catch(); }
739 } end_catch_protect_obj
;
741 lsda_header_info info
;
742 __cxa_exception
*xh
= __get_exception_header_from_ue (exc_obj
);
743 const unsigned char *xh_lsda
;
744 _Unwind_Sword xh_switch_value
;
745 std::terminate_handler xh_terminate_handler
;
747 // If the unexpectedHandler rethrows the exception (e.g. to categorize it),
748 // it will clobber data about the current handler. So copy the data out now.
749 xh_lsda
= xh
->languageSpecificData
;
750 xh_switch_value
= xh
->handlerSwitchValue
;
751 xh_terminate_handler
= xh
->terminateHandler
;
752 info
.ttype_base
= (_Unwind_Ptr
) xh
->catchTemp
;
755 { __unexpected (xh
->unexpectedHandler
); }
758 // Get the exception thrown from unexpected.
760 __cxa_eh_globals
*globals
= __cxa_get_globals_fast ();
761 __cxa_exception
*new_xh
= globals
->caughtExceptions
;
762 void *new_ptr
= __get_object_from_ambiguous_exception (new_xh
);
764 // We don't quite have enough stuff cached; re-parse the LSDA.
765 parse_lsda_header (0, xh_lsda
, &info
);
767 // If this new exception meets the exception spec, allow it.
768 if (check_exception_spec (&info
, __get_exception_header_from_obj
769 (new_ptr
)->exceptionType
,
770 new_ptr
, xh_switch_value
))
771 __throw_exception_again
;
773 // If the exception spec allows std::bad_exception, throw that.
774 // We don't have a thrown object to compare against, but since
775 // bad_exception doesn't have virtual bases, that's OK; just pass 0.
776 #if defined(__EXCEPTIONS) && defined(__GXX_RTTI)
777 const std::type_info
&bad_exc
= typeid (std::bad_exception
);
778 if (check_exception_spec (&info
, &bad_exc
, 0, xh_switch_value
))
779 throw std::bad_exception();
783 __terminate (xh_terminate_handler
);
790 EXCEPTION_DISPOSITION
791 __gxx_personality_seh0 (PEXCEPTION_RECORD ms_exc
, void *this_frame
,
792 PCONTEXT ms_orig_context
, PDISPATCHER_CONTEXT ms_disp
)
794 return _GCC_specific_handler (ms_exc
, this_frame
, ms_orig_context
,
795 ms_disp
, __gxx_personality_imp
);
799 } // namespace __cxxabiv1