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