1 // -*- C++ -*- Helpers for calling unextected and terminate
2 // Copyright (C) 2001, 2002, 2003 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with GCC; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 #include <bits/c++config.h>
33 #include <exception_defines.h>
34 #include "unwind-cxx.h"
36 using namespace __cxxabiv1
;
38 #include "unwind-pe.h"
41 // Helper routine for when the exception handling code needs to call
45 __cxa_call_terminate(_Unwind_Exception
* ue_header
)
50 // terminate is classed as a catch handler.
51 __cxa_begin_catch(ue_header
);
53 // Call the terminate handler that was in effect when we threw this
55 if (__is_gxx_exception_class(ue_header
->exception_class
))
59 xh
= __get_exception_header_from_ue(ue_header
);
60 __terminate(xh
->terminateHandler
);
63 /* Call the global routine if we don't have anything better. */
68 #ifdef __ARM_EABI_UNWINDER__
69 // The ARM EABI __cxa_call_unexpected has the same semantics as the generic
70 // routine, but the exception specification has a different format.
72 __cxa_call_unexpected(void* exc_obj_in
)
74 _Unwind_Exception
* exc_obj
75 = reinterpret_cast<_Unwind_Exception
*>(exc_obj_in
);
78 _Unwind_Word rtti_stride
= 0;
79 _Unwind_Word
* rtti_list
= NULL
;
80 bool foreign_exception
;
81 std::unexpected_handler unexpectedHandler
= NULL
;
82 std::terminate_handler terminateHandler
= NULL
;
84 if (__is_gxx_exception_class(exc_obj
->exception_class
))
86 // Save data from the EO, which may be clobbered by _cxa_begin_catch.
87 xh
= __get_exception_header_from_ue(exc_obj
);
88 unexpectedHandler
= xh
->unexpectedHandler
;
89 terminateHandler
= xh
->terminateHandler
;
90 rtti_count
= exc_obj
->barrier_cache
.bitpattern
[1];
92 rtti_stride
= exc_obj
->barrier_cache
.bitpattern
[3];
93 rtti_list
= (_Unwind_Word
*) exc_obj
->barrier_cache
.bitpattern
[4];
94 foreign_exception
= false;
97 foreign_exception
= true;
99 /* This must be called after extracting data from the EO, but before
100 calling unexpected(). */
101 __cxa_begin_catch(exc_obj
);
103 // This function is a handler for our exception argument. If we exit
104 // by throwing a different exception, we'll need the original cleaned up.
105 struct end_catch_protect
107 end_catch_protect() { }
108 ~end_catch_protect() { __cxa_end_catch(); }
109 } end_catch_protect_obj
;
114 if (foreign_exception
)
117 __unexpected(unexpectedHandler
);
121 /* See if the new exception matches the rtti list. */
122 if (foreign_exception
)
125 // Get the exception thrown from unexpected.
127 __cxa_eh_globals
* globals
= __cxa_get_globals_fast();
128 __cxa_exception
* new_xh
= globals
->caughtExceptions
;
129 void* new_ptr
= new_xh
+ 1;
130 const std::type_info
* catch_type
;
132 bool bad_exception_allowed
= false;
133 const std::type_info
& bad_exc
= typeid(std::bad_exception
);
135 // Check the new exception against the rtti list
136 for (n
= 0; n
< rtti_count
; n
++)
140 offset
= (_Unwind_Word
) &rtti_list
[n
* (rtti_stride
>> 2)];
141 offset
= _Unwind_decode_target2(offset
);
142 catch_type
= (const std::type_info
*) (offset
);
144 if (__cxa_type_match(&new_xh
->unwindHeader
, catch_type
, false,
145 &new_ptr
) != ctm_failed
)
146 __throw_exception_again
;
148 if (catch_type
->__do_catch(&bad_exc
, 0, 1))
149 bad_exception_allowed
= true;
152 // If the exception spec allows std::bad_exception, throw that.
154 if (bad_exception_allowed
)
155 throw std::bad_exception();
159 __terminate(terminateHandler
);
162 #endif // __ARM_EABI_UNWINDER__