xref: /openbsd-src/gnu/gcc/libstdc++-v3/libsupc++/eh_throw.cc (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // -*- C++ -*- Exception handling routines for throwing.
2*404b540aSrobert // Copyright (C) 2001, 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 "unwind-cxx.h"
32*404b540aSrobert 
33*404b540aSrobert using namespace __cxxabiv1;
34*404b540aSrobert 
35*404b540aSrobert 
36*404b540aSrobert static void
__gxx_exception_cleanup(_Unwind_Reason_Code code,_Unwind_Exception * exc)37*404b540aSrobert __gxx_exception_cleanup (_Unwind_Reason_Code code, _Unwind_Exception *exc)
38*404b540aSrobert {
39*404b540aSrobert   __cxa_exception *header = __get_exception_header_from_ue (exc);
40*404b540aSrobert 
41*404b540aSrobert   // If we haven't been caught by a foreign handler, then this is
42*404b540aSrobert   // some sort of unwind error.  In that case just die immediately.
43*404b540aSrobert   // _Unwind_DeleteException in the HP-UX IA64 libunwind library
44*404b540aSrobert   //  returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
45*404b540aSrobert   // like the GCC _Unwind_DeleteException function does.
46*404b540aSrobert   if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON)
47*404b540aSrobert     __terminate (header->terminateHandler);
48*404b540aSrobert 
49*404b540aSrobert   if (header->exceptionDestructor)
50*404b540aSrobert     header->exceptionDestructor (header + 1);
51*404b540aSrobert 
52*404b540aSrobert   __cxa_free_exception (header + 1);
53*404b540aSrobert }
54*404b540aSrobert 
55*404b540aSrobert 
56*404b540aSrobert extern "C" void
__cxa_throw(void * obj,std::type_info * tinfo,void (* dest)(void *))57*404b540aSrobert __cxxabiv1::__cxa_throw (void *obj, std::type_info *tinfo,
58*404b540aSrobert 			 void (*dest) (void *))
59*404b540aSrobert {
60*404b540aSrobert   __cxa_exception *header = __get_exception_header_from_obj (obj);
61*404b540aSrobert   header->exceptionType = tinfo;
62*404b540aSrobert   header->exceptionDestructor = dest;
63*404b540aSrobert   header->unexpectedHandler = __unexpected_handler;
64*404b540aSrobert   header->terminateHandler = __terminate_handler;
65*404b540aSrobert   __GXX_INIT_EXCEPTION_CLASS(header->unwindHeader.exception_class);
66*404b540aSrobert   header->unwindHeader.exception_cleanup = __gxx_exception_cleanup;
67*404b540aSrobert 
68*404b540aSrobert #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
69*404b540aSrobert   _Unwind_SjLj_RaiseException (&header->unwindHeader);
70*404b540aSrobert #else
71*404b540aSrobert   _Unwind_RaiseException (&header->unwindHeader);
72*404b540aSrobert #endif
73*404b540aSrobert 
74*404b540aSrobert   // Some sort of unwinding error.  Note that terminate is a handler.
75*404b540aSrobert   __cxa_begin_catch (&header->unwindHeader);
76*404b540aSrobert   std::terminate ();
77*404b540aSrobert }
78*404b540aSrobert 
79*404b540aSrobert extern "C" void
__cxa_rethrow()80*404b540aSrobert __cxxabiv1::__cxa_rethrow ()
81*404b540aSrobert {
82*404b540aSrobert   __cxa_eh_globals *globals = __cxa_get_globals ();
83*404b540aSrobert   __cxa_exception *header = globals->caughtExceptions;
84*404b540aSrobert 
85*404b540aSrobert   globals->uncaughtExceptions += 1;
86*404b540aSrobert 
87*404b540aSrobert   // Watch for luser rethrowing with no active exception.
88*404b540aSrobert   if (header)
89*404b540aSrobert     {
90*404b540aSrobert       // Tell __cxa_end_catch this is a rethrow.
91*404b540aSrobert       if (!__is_gxx_exception_class(header->unwindHeader.exception_class))
92*404b540aSrobert 	globals->caughtExceptions = 0;
93*404b540aSrobert       else
94*404b540aSrobert 	header->handlerCount = -header->handlerCount;
95*404b540aSrobert 
96*404b540aSrobert #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
97*404b540aSrobert       _Unwind_SjLj_Resume_or_Rethrow (&header->unwindHeader);
98*404b540aSrobert #else
99*404b540aSrobert #if defined(_LIBUNWIND_STD_ABI)
100*404b540aSrobert       _Unwind_RaiseException (&header->unwindHeader);
101*404b540aSrobert #else
102*404b540aSrobert       _Unwind_Resume_or_Rethrow (&header->unwindHeader);
103*404b540aSrobert #endif
104*404b540aSrobert #endif
105*404b540aSrobert 
106*404b540aSrobert       // Some sort of unwinding error.  Note that terminate is a handler.
107*404b540aSrobert       __cxa_begin_catch (&header->unwindHeader);
108*404b540aSrobert     }
109*404b540aSrobert   std::terminate ();
110*404b540aSrobert }
111