1 // -*- C++ -*- Helpers for calling unextected and terminate
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
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>
30 #include "unwind-cxx.h"
32 using namespace __cxxabiv1
;
34 #include "unwind-pe.h"
37 // Helper routine for when the exception handling code needs to call
41 __cxa_call_terminate(_Unwind_Exception
* ue_header
) throw ()
46 // terminate is classed as a catch handler.
47 __cxa_begin_catch(ue_header
);
49 // Call the terminate handler that was in effect when we threw this
51 if (__is_gxx_exception_class(ue_header
->exception_class
))
55 xh
= __get_exception_header_from_ue(ue_header
);
56 __terminate(xh
->terminateHandler
);
59 /* Call the global routine if we don't have anything better. */
64 #ifdef __ARM_EABI_UNWINDER__
65 // The ARM EABI __cxa_call_unexpected has the same semantics as the generic
66 // routine, but the exception specification has a different format.
68 __cxa_call_unexpected(void* exc_obj_in
)
70 _Unwind_Exception
* exc_obj
71 = reinterpret_cast<_Unwind_Exception
*>(exc_obj_in
);
74 _Unwind_Word rtti_stride
= 0;
75 _Unwind_Word
* rtti_list
= NULL
;
76 bool foreign_exception
;
77 std::unexpected_handler unexpectedHandler
= NULL
;
78 std::terminate_handler terminateHandler
= NULL
;
80 if (__is_gxx_exception_class(exc_obj
->exception_class
))
82 // Save data from the EO, which may be clobbered by _cxa_begin_catch.
83 xh
= __get_exception_header_from_ue(exc_obj
);
84 unexpectedHandler
= xh
->unexpectedHandler
;
85 terminateHandler
= xh
->terminateHandler
;
86 rtti_count
= exc_obj
->barrier_cache
.bitpattern
[1];
88 rtti_stride
= exc_obj
->barrier_cache
.bitpattern
[3];
89 rtti_list
= (_Unwind_Word
*) exc_obj
->barrier_cache
.bitpattern
[4];
90 foreign_exception
= false;
93 foreign_exception
= true;
95 /* This must be called after extracting data from the EO, but before
96 calling unexpected(). */
97 __cxa_begin_catch(exc_obj
);
99 // This function is a handler for our exception argument. If we exit
100 // by throwing a different exception, we'll need the original cleaned up.
101 struct end_catch_protect
103 end_catch_protect() { }
104 ~end_catch_protect() { __cxa_end_catch(); }
105 } end_catch_protect_obj
;
110 if (foreign_exception
)
113 __unexpected(unexpectedHandler
);
117 /* See if the new exception matches the rtti list. */
118 if (foreign_exception
)
121 // Get the exception thrown from unexpected.
123 __cxa_eh_globals
* globals
= __cxa_get_globals_fast();
124 __cxa_exception
* new_xh
= globals
->caughtExceptions
;
125 void* new_ptr
= __get_object_from_ambiguous_exception (new_xh
);
126 const std::type_info
* catch_type
;
128 bool bad_exception_allowed
= false;
129 const std::type_info
& bad_exc
= typeid(std::bad_exception
);
131 // Check the new exception against the rtti list
132 for (n
= 0; n
< rtti_count
; n
++)
136 offset
= (_Unwind_Word
) &rtti_list
[n
* (rtti_stride
>> 2)];
137 offset
= _Unwind_decode_target2(offset
);
138 catch_type
= (const std::type_info
*) (offset
);
140 if (__cxa_type_match(&new_xh
->unwindHeader
, catch_type
, false,
141 &new_ptr
) != ctm_failed
)
142 __throw_exception_again
;
144 if (catch_type
->__do_catch(&bad_exc
, 0, 1))
145 bad_exception_allowed
= true;
148 // If the exception spec allows std::bad_exception, throw that.
150 if (bad_exception_allowed
)
151 throw std::bad_exception();
155 __terminate(terminateHandler
);
158 #endif // __ARM_EABI_UNWINDER__