1*404b540aSrobert // -*- C++ -*- Helpers for calling unextected and terminate
2*404b540aSrobert // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
3*404b540aSrobert //
4*404b540aSrobert // This file is part of GCC.
5*404b540aSrobert //
6*404b540aSrobert // GCC is free software; you can redistribute it and/or modify
7*404b540aSrobert // it under the terms of the GNU General Public License as published by
8*404b540aSrobert // the Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert // any later version.
10*404b540aSrobert //
11*404b540aSrobert // GCC is distributed in the hope that it will be useful,
12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*404b540aSrobert // GNU General Public License for more details.
15*404b540aSrobert //
16*404b540aSrobert // You should have received a copy of the GNU General Public License
17*404b540aSrobert // along with GCC; see the file COPYING. If not, write to
18*404b540aSrobert // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19*404b540aSrobert // Boston, MA 02110-1301, USA.
20*404b540aSrobert
21*404b540aSrobert // As a special exception, you may use this file as part of a free software
22*404b540aSrobert // library without restriction. Specifically, if other files instantiate
23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert // this file and link it with other files to produce an executable, this
25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert // the GNU General Public License. This exception does not however
27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert // the GNU General Public License.
29*404b540aSrobert
30*404b540aSrobert #include <bits/c++config.h>
31*404b540aSrobert #include <cstdlib>
32*404b540aSrobert #include <exception_defines.h>
33*404b540aSrobert #include "unwind-cxx.h"
34*404b540aSrobert
35*404b540aSrobert using namespace __cxxabiv1;
36*404b540aSrobert
37*404b540aSrobert #include "unwind-pe.h"
38*404b540aSrobert
39*404b540aSrobert
40*404b540aSrobert // Helper routine for when the exception handling code needs to call
41*404b540aSrobert // terminate.
42*404b540aSrobert
43*404b540aSrobert extern "C" void
__cxa_call_terminate(_Unwind_Exception * ue_header)44*404b540aSrobert __cxa_call_terminate(_Unwind_Exception* ue_header)
45*404b540aSrobert {
46*404b540aSrobert
47*404b540aSrobert if (ue_header)
48*404b540aSrobert {
49*404b540aSrobert // terminate is classed as a catch handler.
50*404b540aSrobert __cxa_begin_catch(ue_header);
51*404b540aSrobert
52*404b540aSrobert // Call the terminate handler that was in effect when we threw this
53*404b540aSrobert // exception. */
54*404b540aSrobert if (__is_gxx_exception_class(ue_header->exception_class))
55*404b540aSrobert {
56*404b540aSrobert __cxa_exception* xh;
57*404b540aSrobert
58*404b540aSrobert xh = __get_exception_header_from_ue(ue_header);
59*404b540aSrobert __terminate(xh->terminateHandler);
60*404b540aSrobert }
61*404b540aSrobert }
62*404b540aSrobert /* Call the global routine if we don't have anything better. */
63*404b540aSrobert std::terminate();
64*404b540aSrobert }
65*404b540aSrobert
66*404b540aSrobert
67*404b540aSrobert #ifdef __ARM_EABI_UNWINDER__
68*404b540aSrobert // The ARM EABI __cxa_call_unexpected has the same semantics as the generic
69*404b540aSrobert // routine, but the exception specification has a different format.
70*404b540aSrobert extern "C" void
__cxa_call_unexpected(void * exc_obj_in)71*404b540aSrobert __cxa_call_unexpected(void* exc_obj_in)
72*404b540aSrobert {
73*404b540aSrobert _Unwind_Exception* exc_obj
74*404b540aSrobert = reinterpret_cast<_Unwind_Exception*>(exc_obj_in);
75*404b540aSrobert
76*404b540aSrobert int rtti_count = 0;
77*404b540aSrobert _Unwind_Word rtti_stride = 0;
78*404b540aSrobert _Unwind_Word* rtti_list = NULL;
79*404b540aSrobert bool foreign_exception;
80*404b540aSrobert std::unexpected_handler unexpectedHandler = NULL;
81*404b540aSrobert std::terminate_handler terminateHandler = NULL;
82*404b540aSrobert __cxa_exception* xh;
83*404b540aSrobert if (__is_gxx_exception_class(exc_obj->exception_class))
84*404b540aSrobert {
85*404b540aSrobert // Save data from the EO, which may be clobbered by _cxa_begin_catch.
86*404b540aSrobert xh = __get_exception_header_from_ue(exc_obj);
87*404b540aSrobert unexpectedHandler = xh->unexpectedHandler;
88*404b540aSrobert terminateHandler = xh->terminateHandler;
89*404b540aSrobert rtti_count = exc_obj->barrier_cache.bitpattern[1];
90*404b540aSrobert
91*404b540aSrobert rtti_stride = exc_obj->barrier_cache.bitpattern[3];
92*404b540aSrobert rtti_list = (_Unwind_Word*) exc_obj->barrier_cache.bitpattern[4];
93*404b540aSrobert foreign_exception = false;
94*404b540aSrobert }
95*404b540aSrobert else
96*404b540aSrobert foreign_exception = true;
97*404b540aSrobert
98*404b540aSrobert /* This must be called after extracting data from the EO, but before
99*404b540aSrobert calling unexpected(). */
100*404b540aSrobert __cxa_begin_catch(exc_obj);
101*404b540aSrobert
102*404b540aSrobert // This function is a handler for our exception argument. If we exit
103*404b540aSrobert // by throwing a different exception, we'll need the original cleaned up.
104*404b540aSrobert struct end_catch_protect
105*404b540aSrobert {
106*404b540aSrobert end_catch_protect() { }
107*404b540aSrobert ~end_catch_protect() { __cxa_end_catch(); }
108*404b540aSrobert } end_catch_protect_obj;
109*404b540aSrobert
110*404b540aSrobert
111*404b540aSrobert try
112*404b540aSrobert {
113*404b540aSrobert if (foreign_exception)
114*404b540aSrobert std::unexpected();
115*404b540aSrobert else
116*404b540aSrobert __unexpected(unexpectedHandler);
117*404b540aSrobert }
118*404b540aSrobert catch(...)
119*404b540aSrobert {
120*404b540aSrobert /* See if the new exception matches the rtti list. */
121*404b540aSrobert if (foreign_exception)
122*404b540aSrobert std::terminate();
123*404b540aSrobert
124*404b540aSrobert // Get the exception thrown from unexpected.
125*404b540aSrobert
126*404b540aSrobert __cxa_eh_globals* globals = __cxa_get_globals_fast();
127*404b540aSrobert __cxa_exception* new_xh = globals->caughtExceptions;
128*404b540aSrobert void* new_ptr = new_xh + 1;
129*404b540aSrobert const std::type_info* catch_type;
130*404b540aSrobert int n;
131*404b540aSrobert bool bad_exception_allowed = false;
132*404b540aSrobert const std::type_info& bad_exc = typeid(std::bad_exception);
133*404b540aSrobert
134*404b540aSrobert // Check the new exception against the rtti list
135*404b540aSrobert for (n = 0; n < rtti_count; n++)
136*404b540aSrobert {
137*404b540aSrobert _Unwind_Word offset;
138*404b540aSrobert
139*404b540aSrobert offset = (_Unwind_Word) &rtti_list[n * (rtti_stride >> 2)];
140*404b540aSrobert offset = _Unwind_decode_target2(offset);
141*404b540aSrobert catch_type = (const std::type_info*) (offset);
142*404b540aSrobert
143*404b540aSrobert if (__cxa_type_match(&new_xh->unwindHeader, catch_type, false,
144*404b540aSrobert &new_ptr) != ctm_failed)
145*404b540aSrobert __throw_exception_again;
146*404b540aSrobert
147*404b540aSrobert if (catch_type->__do_catch(&bad_exc, 0, 1))
148*404b540aSrobert bad_exception_allowed = true;
149*404b540aSrobert }
150*404b540aSrobert
151*404b540aSrobert // If the exception spec allows std::bad_exception, throw that.
152*404b540aSrobert #ifdef __EXCEPTIONS
153*404b540aSrobert if (bad_exception_allowed)
154*404b540aSrobert throw std::bad_exception();
155*404b540aSrobert #endif
156*404b540aSrobert
157*404b540aSrobert // Otherwise, die.
158*404b540aSrobert __terminate(terminateHandler);
159*404b540aSrobert }
160*404b540aSrobert }
161*404b540aSrobert #endif // __ARM_EABI_UNWINDER__
162