1 // -*- C++ -*- The GNU C++ exception personality routine.
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 // Free Software Foundation, Inc.
5 // This file is part of GCC.
7 // GCC is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2, or (at your option)
12 // GCC is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with GCC; see the file COPYING. If not, write to
19 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 // Boston, MA 02110-1301, USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 #include <bits/c++config.h>
33 #include <exception_defines.h>
35 #include "unwind-cxx.h"
37 using namespace __cxxabiv1
;
39 #ifdef __ARM_EABI_UNWINDER__
40 #define NO_SIZE_OF_ENCODED_VALUE
43 #include "unwind-pe.h"
46 struct lsda_header_info
50 _Unwind_Ptr ttype_base
;
51 const unsigned char *TType
;
52 const unsigned char *action_table
;
53 unsigned char ttype_encoding
;
54 unsigned char call_site_encoding
;
57 static const unsigned char *
58 parse_lsda_header (_Unwind_Context
*context
, const unsigned char *p
,
59 lsda_header_info
*info
)
62 unsigned char lpstart_encoding
;
64 info
->Start
= (context
? _Unwind_GetRegionStart (context
) : 0);
66 // Find @LPStart, the base to which landing pad offsets are relative.
67 lpstart_encoding
= *p
++;
68 if (lpstart_encoding
!= DW_EH_PE_omit
)
69 p
= read_encoded_value (context
, lpstart_encoding
, p
, &info
->LPStart
);
71 info
->LPStart
= info
->Start
;
73 // Find @TType, the base of the handler and exception spec type data.
74 info
->ttype_encoding
= *p
++;
75 if (info
->ttype_encoding
!= DW_EH_PE_omit
)
77 p
= read_uleb128 (p
, &tmp
);
78 info
->TType
= p
+ tmp
;
83 // The encoding and length of the call-site table; the action table
84 // immediately follows.
85 info
->call_site_encoding
= *p
++;
86 p
= read_uleb128 (p
, &tmp
);
87 info
->action_table
= p
+ tmp
;
92 #ifdef __ARM_EABI_UNWINDER__
94 // Return an element from a type table.
96 static const std::type_info
*
97 get_ttype_entry(lsda_header_info
* info
, _uleb128_t i
)
101 ptr
= (_Unwind_Ptr
) (info
->TType
- (i
* 4));
102 ptr
= _Unwind_decode_target2(ptr
);
104 return reinterpret_cast<const std::type_info
*>(ptr
);
107 // The ABI provides a routine for matching exception object types.
108 typedef _Unwind_Control_Block _throw_typet
;
109 #define get_adjusted_ptr(catch_type, throw_type, thrown_ptr_p) \
110 (__cxa_type_match (throw_type, catch_type, false, thrown_ptr_p) \
113 // Return true if THROW_TYPE matches one if the filter types.
116 check_exception_spec(lsda_header_info
* info
, _throw_typet
* throw_type
,
117 void* thrown_ptr
, _sleb128_t filter_value
)
119 const _uleb128_t
* e
= ((const _uleb128_t
*) info
->TType
)
124 const std::type_info
* catch_type
;
129 // Zero signals the end of the list. If we've not found
130 // a match by now, then we've failed the specification.
134 tmp
= _Unwind_decode_target2((_Unwind_Word
) e
);
136 // Match a ttype entry.
137 catch_type
= reinterpret_cast<const std::type_info
*>(tmp
);
139 // ??? There is currently no way to ask the RTTI code about the
140 // relationship between two types without reference to a specific
141 // object. There should be; then we wouldn't need to mess with
143 if (get_adjusted_ptr(catch_type
, throw_type
, &thrown_ptr
))
146 // Advance to the next entry.
152 // Save stage1 handler information in the exception object
155 save_caught_exception(struct _Unwind_Exception
* ue_header
,
156 struct _Unwind_Context
* context
,
158 int handler_switch_value
,
159 const unsigned char* language_specific_data
,
160 _Unwind_Ptr landing_pad
,
161 const unsigned char* action_record
162 __attribute__((__unused__
)))
164 ue_header
->barrier_cache
.sp
= _Unwind_GetGR(context
, 13);
165 ue_header
->barrier_cache
.bitpattern
[0] = (_uw
) thrown_ptr
;
166 ue_header
->barrier_cache
.bitpattern
[1]
167 = (_uw
) handler_switch_value
;
168 ue_header
->barrier_cache
.bitpattern
[2]
169 = (_uw
) language_specific_data
;
170 ue_header
->barrier_cache
.bitpattern
[3] = (_uw
) landing_pad
;
174 // Restore the catch handler data saved during phase1.
177 restore_caught_exception(struct _Unwind_Exception
* ue_header
,
178 int& handler_switch_value
,
179 const unsigned char*& language_specific_data
,
180 _Unwind_Ptr
& landing_pad
)
182 handler_switch_value
= (int) ue_header
->barrier_cache
.bitpattern
[1];
183 language_specific_data
=
184 (const unsigned char*) ue_header
->barrier_cache
.bitpattern
[2];
185 landing_pad
= (_Unwind_Ptr
) ue_header
->barrier_cache
.bitpattern
[3];
188 #define CONTINUE_UNWINDING \
191 if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
192 return _URC_FAILURE; \
193 return _URC_CONTINUE_UNWIND; \
197 // Return true if the filter spec is empty, ie throw().
200 empty_exception_spec (lsda_header_info
*info
, _Unwind_Sword filter_value
)
202 const _Unwind_Word
* e
= ((const _Unwind_Word
*) info
->TType
)
209 typedef const std::type_info _throw_typet
;
212 // Return an element from a type table.
214 static const std::type_info
*
215 get_ttype_entry (lsda_header_info
*info
, _uleb128_t i
)
219 i
*= size_of_encoded_value (info
->ttype_encoding
);
220 read_encoded_value_with_base (info
->ttype_encoding
, info
->ttype_base
,
221 info
->TType
- i
, &ptr
);
223 return reinterpret_cast<const std::type_info
*>(ptr
);
226 // Given the thrown type THROW_TYPE, pointer to a variable containing a
227 // pointer to the exception object THROWN_PTR_P and a type CATCH_TYPE to
228 // compare against, return whether or not there is a match and if so,
229 // update *THROWN_PTR_P.
232 get_adjusted_ptr (const std::type_info
*catch_type
,
233 const std::type_info
*throw_type
,
236 void *thrown_ptr
= *thrown_ptr_p
;
238 // Pointer types need to adjust the actual pointer, not
239 // the pointer to pointer that is the exception object.
240 // This also has the effect of passing pointer types
241 // "by value" through the __cxa_begin_catch return value.
242 if (throw_type
->__is_pointer_p ())
243 thrown_ptr
= *(void **) thrown_ptr
;
245 if (catch_type
->__do_catch (throw_type
, &thrown_ptr
, 1))
247 *thrown_ptr_p
= thrown_ptr
;
254 // Return true if THROW_TYPE matches one if the filter types.
257 check_exception_spec(lsda_header_info
* info
, _throw_typet
* throw_type
,
258 void* thrown_ptr
, _sleb128_t filter_value
)
260 const unsigned char *e
= info
->TType
- filter_value
- 1;
264 const std::type_info
*catch_type
;
267 e
= read_uleb128 (e
, &tmp
);
269 // Zero signals the end of the list. If we've not found
270 // a match by now, then we've failed the specification.
274 // Match a ttype entry.
275 catch_type
= get_ttype_entry (info
, tmp
);
277 // ??? There is currently no way to ask the RTTI code about the
278 // relationship between two types without reference to a specific
279 // object. There should be; then we wouldn't need to mess with
281 if (get_adjusted_ptr (catch_type
, throw_type
, &thrown_ptr
))
287 // Save stage1 handler information in the exception object
290 save_caught_exception(struct _Unwind_Exception
* ue_header
,
291 struct _Unwind_Context
* context
292 __attribute__((__unused__
)),
294 int handler_switch_value
,
295 const unsigned char* language_specific_data
,
296 _Unwind_Ptr landing_pad
__attribute__((__unused__
)),
297 const unsigned char* action_record
)
299 __cxa_exception
* xh
= __get_exception_header_from_ue(ue_header
);
301 xh
->handlerSwitchValue
= handler_switch_value
;
302 xh
->actionRecord
= action_record
;
303 xh
->languageSpecificData
= language_specific_data
;
304 xh
->adjustedPtr
= thrown_ptr
;
306 // ??? Completely unknown what this field is supposed to be for.
307 // ??? Need to cache TType encoding base for call_unexpected.
308 xh
->catchTemp
= landing_pad
;
312 // Restore the catch handler information saved during phase1.
315 restore_caught_exception(struct _Unwind_Exception
* ue_header
,
316 int& handler_switch_value
,
317 const unsigned char*& language_specific_data
,
318 _Unwind_Ptr
& landing_pad
)
320 __cxa_exception
* xh
= __get_exception_header_from_ue(ue_header
);
321 handler_switch_value
= xh
->handlerSwitchValue
;
322 language_specific_data
= xh
->languageSpecificData
;
323 landing_pad
= (_Unwind_Ptr
) xh
->catchTemp
;
326 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
328 // Return true if the filter spec is empty, ie throw().
331 empty_exception_spec (lsda_header_info
*info
, _Unwind_Sword filter_value
)
333 const unsigned char *e
= info
->TType
- filter_value
- 1;
336 e
= read_uleb128 (e
, &tmp
);
340 #endif // !__ARM_EABI_UNWINDER__
345 // Using a different personality function name causes link failures
346 // when trying to mix code using different exception handling models.
347 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
348 #define PERSONALITY_FUNCTION __gxx_personality_sj0
349 #define __builtin_eh_return_data_regno(x) x
351 #define PERSONALITY_FUNCTION __gxx_personality_v0
354 extern "C" _Unwind_Reason_Code
355 #ifdef __ARM_EABI_UNWINDER__
356 PERSONALITY_FUNCTION (_Unwind_State state
,
357 struct _Unwind_Exception
* ue_header
,
358 struct _Unwind_Context
* context
)
360 PERSONALITY_FUNCTION (int version
,
361 _Unwind_Action actions
,
362 _Unwind_Exception_Class exception_class
,
363 struct _Unwind_Exception
*ue_header
,
364 struct _Unwind_Context
*context
)
367 enum found_handler_type
375 lsda_header_info info
;
376 const unsigned char *language_specific_data
;
377 const unsigned char *action_record
;
378 const unsigned char *p
;
379 _Unwind_Ptr landing_pad
, ip
;
380 int handler_switch_value
;
381 void* thrown_ptr
= 0;
382 bool foreign_exception
;
383 int ip_before_insn
= 0;
385 #ifdef __ARM_EABI_UNWINDER__
386 _Unwind_Action actions
;
388 switch (state
& _US_ACTION_MASK
)
390 case _US_VIRTUAL_UNWIND_FRAME
:
391 actions
= _UA_SEARCH_PHASE
;
394 case _US_UNWIND_FRAME_STARTING
:
395 actions
= _UA_CLEANUP_PHASE
;
396 if (!(state
& _US_FORCE_UNWIND
)
397 && ue_header
->barrier_cache
.sp
== _Unwind_GetGR(context
, 13))
398 actions
|= _UA_HANDLER_FRAME
;
401 case _US_UNWIND_FRAME_RESUME
:
408 actions
|= state
& _US_FORCE_UNWIND
;
410 // We don't know which runtime we're working with, so can't check this.
411 // However the ABI routines hide this from us, and we don't actually need
413 foreign_exception
= false;
415 // The dwarf unwinder assumes the context structure holds things like the
416 // function and LSDA pointers. The ARM implementation caches these in
417 // the exception header (UCB). To avoid rewriting everything we make the
418 // virtual IP register point at the UCB.
419 ip
= (_Unwind_Ptr
) ue_header
;
420 _Unwind_SetGR(context
, 12, ip
);
422 __cxa_exception
* xh
= __get_exception_header_from_ue(ue_header
);
424 // Interface version check.
426 return _URC_FATAL_PHASE1_ERROR
;
427 foreign_exception
= !__is_gxx_exception_class(exception_class
);
430 // Shortcut for phase 2 found handler for domestic exception.
431 if (actions
== (_UA_CLEANUP_PHASE
| _UA_HANDLER_FRAME
)
432 && !foreign_exception
)
434 restore_caught_exception(ue_header
, handler_switch_value
,
435 language_specific_data
, landing_pad
);
436 found_type
= (landing_pad
== 0 ? found_terminate
: found_handler
);
437 goto install_context
;
440 language_specific_data
= (const unsigned char *)
441 _Unwind_GetLanguageSpecificData (context
);
443 // If no LSDA, then there are no handlers or cleanups.
444 if (! language_specific_data
)
447 // Parse the LSDA header.
448 p
= parse_lsda_header (context
, language_specific_data
, &info
);
449 info
.ttype_base
= base_of_encoded_value (info
.ttype_encoding
, context
);
450 #ifdef _GLIBCXX_HAVE_GETIPINFO
451 ip
= _Unwind_GetIPInfo (context
, &ip_before_insn
);
453 ip
= _Unwind_GetIP (context
);
455 if (! ip_before_insn
)
459 handler_switch_value
= 0;
461 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
462 // The given "IP" is an index into the call-site table, with two
463 // exceptions -- -1 means no-action, and 0 means terminate. But
464 // since we're using uleb128 values, we've not got random access
467 return _URC_CONTINUE_UNWIND
;
470 // Fall through to set found_terminate.
474 _uleb128_t cs_lp
, cs_action
;
477 p
= read_uleb128 (p
, &cs_lp
);
478 p
= read_uleb128 (p
, &cs_action
);
482 // Can never have null landing pad for sjlj -- that would have
483 // been indicated by a -1 call site index.
484 landing_pad
= cs_lp
+ 1;
486 action_record
= info
.action_table
+ cs_action
- 1;
487 goto found_something
;
490 // Search the call-site table for the action associated with this IP.
491 while (p
< info
.action_table
)
493 _Unwind_Ptr cs_start
, cs_len
, cs_lp
;
494 _uleb128_t cs_action
;
496 // Note that all call-site encodings are "absolute" displacements.
497 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_start
);
498 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_len
);
499 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_lp
);
500 p
= read_uleb128 (p
, &cs_action
);
502 // The table is sorted, so if we've passed the ip, stop.
503 if (ip
< info
.Start
+ cs_start
)
504 p
= info
.action_table
;
505 else if (ip
< info
.Start
+ cs_start
+ cs_len
)
508 landing_pad
= info
.LPStart
+ cs_lp
;
510 action_record
= info
.action_table
+ cs_action
- 1;
511 goto found_something
;
514 #endif // _GLIBCXX_SJLJ_EXCEPTIONS
516 // If ip is not present in the table, call terminate. This is for
517 // a destructor inside a cleanup, or a library routine the compiler
518 // was not expecting to throw.
519 found_type
= found_terminate
;
523 if (landing_pad
== 0)
525 // If ip is present, and has a null landing pad, there are
526 // no cleanups or handlers to be run.
527 found_type
= found_nothing
;
529 else if (action_record
== 0)
531 // If ip is present, has a non-null landing pad, and a null
532 // action table offset, then there are only cleanups present.
533 // Cleanups use a zero switch value, as set above.
534 found_type
= found_cleanup
;
538 // Otherwise we have a catch handler or exception specification.
540 _sleb128_t ar_filter
, ar_disp
;
541 const std::type_info
* catch_type
;
542 _throw_typet
* throw_type
;
543 bool saw_cleanup
= false;
544 bool saw_handler
= false;
546 #ifdef __ARM_EABI_UNWINDER__
547 // ??? How does this work - more importantly, how does it interact with
548 // dependent exceptions?
549 throw_type
= ue_header
;
550 if (actions
& _UA_FORCE_UNWIND
)
552 __GXX_INIT_FORCED_UNWIND_CLASS(ue_header
->exception_class
);
554 else if (!foreign_exception
)
555 thrown_ptr
= __get_object_from_ue (ue_header
);
557 // During forced unwinding, match a magic exception type.
558 if (actions
& _UA_FORCE_UNWIND
)
560 throw_type
= &typeid(abi::__forced_unwind
);
562 // With a foreign exception class, there's no exception type.
563 // ??? What to do about GNU Java and GNU Ada exceptions?
564 else if (foreign_exception
)
566 throw_type
= &typeid(abi::__foreign_exception
);
570 thrown_ptr
= __get_object_from_ue (ue_header
);
571 throw_type
= __get_exception_header_from_obj
572 (thrown_ptr
)->exceptionType
;
579 p
= read_sleb128 (p
, &ar_filter
);
580 read_sleb128 (p
, &ar_disp
);
584 // Zero filter values are cleanups.
587 else if (ar_filter
> 0)
589 // Positive filter values are handlers.
590 catch_type
= get_ttype_entry (&info
, ar_filter
);
592 // Null catch type is a catch-all handler; we can catch foreign
593 // exceptions with this. Otherwise we must match types.
596 && get_adjusted_ptr (catch_type
, throw_type
,
605 // Negative filter values are exception specifications.
606 // ??? How do foreign exceptions fit in? As far as I can
607 // see we can't match because there's no __cxa_exception
608 // object to stuff bits in for __cxa_call_unexpected to use.
609 // Allow them iff the exception spec is non-empty. I.e.
610 // a throw() specification results in __unexpected.
612 && !(actions
& _UA_FORCE_UNWIND
)
613 && !foreign_exception
)
614 ? ! check_exception_spec (&info
, throw_type
, thrown_ptr
,
616 : empty_exception_spec (&info
, ar_filter
))
625 action_record
= p
+ ar_disp
;
630 handler_switch_value
= ar_filter
;
631 found_type
= found_handler
;
634 found_type
= (saw_cleanup
? found_cleanup
: found_nothing
);
638 if (found_type
== found_nothing
)
641 if (actions
& _UA_SEARCH_PHASE
)
643 if (found_type
== found_cleanup
)
646 // For domestic exceptions, we cache data from phase 1 for phase 2.
647 if (!foreign_exception
)
649 save_caught_exception(ue_header
, context
, thrown_ptr
,
650 handler_switch_value
, language_specific_data
,
651 landing_pad
, action_record
);
653 return _URC_HANDLER_FOUND
;
658 // We can't use any of the cxa routines with foreign exceptions,
659 // because they all expect ue_header to be a struct __cxa_exception.
660 // So in that case, call terminate or unexpected directly.
661 if ((actions
& _UA_FORCE_UNWIND
)
662 || foreign_exception
)
664 if (found_type
== found_terminate
)
666 else if (handler_switch_value
< 0)
669 { std::unexpected (); }
671 { std::terminate (); }
676 if (found_type
== found_terminate
)
677 __cxa_call_terminate(ue_header
);
679 // Cache the TType base value for __cxa_call_unexpected, as we won't
680 // have an _Unwind_Context then.
681 if (handler_switch_value
< 0)
683 parse_lsda_header (context
, language_specific_data
, &info
);
685 #ifdef __ARM_EABI_UNWINDER__
686 const _Unwind_Word
* e
;
689 e
= ((const _Unwind_Word
*) info
.TType
) - handler_switch_value
- 1;
690 // Count the number of rtti objects.
696 ue_header
->barrier_cache
.bitpattern
[1] = n
;
698 ue_header
->barrier_cache
.bitpattern
[2] = 0;
700 ue_header
->barrier_cache
.bitpattern
[3] = 4;
702 ue_header
->barrier_cache
.bitpattern
[4] = (_Unwind_Word
) e
;
704 xh
->catchTemp
= base_of_encoded_value (info
.ttype_encoding
, context
);
709 /* For targets with pointers smaller than the word size, we must extend the
710 pointer, and this extension is target dependent. */
711 _Unwind_SetGR (context
, __builtin_eh_return_data_regno (0),
712 __builtin_extend_pointer (ue_header
));
713 _Unwind_SetGR (context
, __builtin_eh_return_data_regno (1),
714 handler_switch_value
);
715 _Unwind_SetIP (context
, landing_pad
);
716 #ifdef __ARM_EABI_UNWINDER__
717 if (found_type
== found_cleanup
)
718 __cxa_begin_cleanup(ue_header
);
720 return _URC_INSTALL_CONTEXT
;
723 /* The ARM EABI implementation of __cxa_call_unexpected is in a
724 different file so that the personality routine (PR) can be used
725 standalone. The generic routine shared datastructures with the PR
726 so it is most convenient to implement it here. */
727 #ifndef __ARM_EABI_UNWINDER__
729 __cxa_call_unexpected (void *exc_obj_in
)
731 _Unwind_Exception
*exc_obj
732 = reinterpret_cast <_Unwind_Exception
*>(exc_obj_in
);
734 __cxa_begin_catch (exc_obj
);
736 // This function is a handler for our exception argument. If we exit
737 // by throwing a different exception, we'll need the original cleaned up.
738 struct end_catch_protect
740 end_catch_protect() { }
741 ~end_catch_protect() { __cxa_end_catch(); }
742 } end_catch_protect_obj
;
744 lsda_header_info info
;
745 __cxa_exception
*xh
= __get_exception_header_from_ue (exc_obj
);
746 const unsigned char *xh_lsda
;
747 _Unwind_Sword xh_switch_value
;
748 std::terminate_handler xh_terminate_handler
;
750 // If the unexpectedHandler rethrows the exception (e.g. to categorize it),
751 // it will clobber data about the current handler. So copy the data out now.
752 xh_lsda
= xh
->languageSpecificData
;
753 xh_switch_value
= xh
->handlerSwitchValue
;
754 xh_terminate_handler
= xh
->terminateHandler
;
755 info
.ttype_base
= (_Unwind_Ptr
) xh
->catchTemp
;
758 { __unexpected (xh
->unexpectedHandler
); }
761 // Get the exception thrown from unexpected.
763 __cxa_eh_globals
*globals
= __cxa_get_globals_fast ();
764 __cxa_exception
*new_xh
= globals
->caughtExceptions
;
765 void *new_ptr
= __get_object_from_ambiguous_exception (new_xh
);
767 // We don't quite have enough stuff cached; re-parse the LSDA.
768 parse_lsda_header (0, xh_lsda
, &info
);
770 // If this new exception meets the exception spec, allow it.
771 if (check_exception_spec (&info
, __get_exception_header_from_obj
772 (new_ptr
)->exceptionType
,
773 new_ptr
, xh_switch_value
))
774 __throw_exception_again
;
776 // If the exception spec allows std::bad_exception, throw that.
777 // We don't have a thrown object to compare against, but since
778 // bad_exception doesn't have virtual bases, that's OK; just pass 0.
780 const std::type_info
&bad_exc
= typeid (std::bad_exception
);
781 if (check_exception_spec (&info
, &bad_exc
, 0, xh_switch_value
))
782 throw std::bad_exception();
786 __terminate (xh_terminate_handler
);
791 } // namespace __cxxabiv1