1*38fd1498Szrj // -*- C++ -*- Exception handling routines for throwing.
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 "unwind-cxx.h"
27*38fd1498Szrj #include "eh_atomics.h"
28*38fd1498Szrj
29*38fd1498Szrj using namespace __cxxabiv1;
30*38fd1498Szrj
31*38fd1498Szrj
32*38fd1498Szrj static void
__gxx_exception_cleanup(_Unwind_Reason_Code code,_Unwind_Exception * exc)33*38fd1498Szrj __gxx_exception_cleanup (_Unwind_Reason_Code code, _Unwind_Exception *exc)
34*38fd1498Szrj {
35*38fd1498Szrj // This cleanup is set only for primaries.
36*38fd1498Szrj __cxa_refcounted_exception *header
37*38fd1498Szrj = __get_refcounted_exception_header_from_ue (exc);
38*38fd1498Szrj
39*38fd1498Szrj // We only want to be called through _Unwind_DeleteException.
40*38fd1498Szrj // _Unwind_DeleteException in the HP-UX IA64 libunwind library
41*38fd1498Szrj // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
42*38fd1498Szrj // like the GCC _Unwind_DeleteException function does.
43*38fd1498Szrj if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON)
44*38fd1498Szrj __terminate (header->exc.terminateHandler);
45*38fd1498Szrj
46*38fd1498Szrj if (__gnu_cxx::__eh_atomic_dec (&header->referenceCount))
47*38fd1498Szrj {
48*38fd1498Szrj if (header->exc.exceptionDestructor)
49*38fd1498Szrj header->exc.exceptionDestructor (header + 1);
50*38fd1498Szrj
51*38fd1498Szrj __cxa_free_exception (header + 1);
52*38fd1498Szrj }
53*38fd1498Szrj }
54*38fd1498Szrj
55*38fd1498Szrj extern "C" __cxa_refcounted_exception*
56*38fd1498Szrj __cxxabiv1::
__cxa_init_primary_exception(void * obj,std::type_info * tinfo,void (_GLIBCXX_CDTOR_CALLABI * dest)(void *))57*38fd1498Szrj __cxa_init_primary_exception(void *obj, std::type_info *tinfo,
58*38fd1498Szrj void (_GLIBCXX_CDTOR_CALLABI *dest) (void *))
59*38fd1498Szrj _GLIBCXX_NOTHROW
60*38fd1498Szrj {
61*38fd1498Szrj __cxa_refcounted_exception *header
62*38fd1498Szrj = __get_refcounted_exception_header_from_obj (obj);
63*38fd1498Szrj header->referenceCount = 0;
64*38fd1498Szrj header->exc.exceptionType = tinfo;
65*38fd1498Szrj header->exc.exceptionDestructor = dest;
66*38fd1498Szrj header->exc.unexpectedHandler = std::get_unexpected ();
67*38fd1498Szrj header->exc.terminateHandler = std::get_terminate ();
68*38fd1498Szrj __GXX_INIT_PRIMARY_EXCEPTION_CLASS(header->exc.unwindHeader.exception_class);
69*38fd1498Szrj header->exc.unwindHeader.exception_cleanup = __gxx_exception_cleanup;
70*38fd1498Szrj
71*38fd1498Szrj return header;
72*38fd1498Szrj }
73*38fd1498Szrj
74*38fd1498Szrj extern "C" void
__cxa_throw(void * obj,std::type_info * tinfo,void (_GLIBCXX_CDTOR_CALLABI * dest)(void *))75*38fd1498Szrj __cxxabiv1::__cxa_throw (void *obj, std::type_info *tinfo,
76*38fd1498Szrj void (_GLIBCXX_CDTOR_CALLABI *dest) (void *))
77*38fd1498Szrj {
78*38fd1498Szrj PROBE2 (throw, obj, tinfo);
79*38fd1498Szrj
80*38fd1498Szrj __cxa_eh_globals *globals = __cxa_get_globals ();
81*38fd1498Szrj globals->uncaughtExceptions += 1;
82*38fd1498Szrj // Definitely a primary.
83*38fd1498Szrj __cxa_refcounted_exception *header =
84*38fd1498Szrj __cxa_init_primary_exception(obj, tinfo, dest);
85*38fd1498Szrj header->referenceCount = 1;
86*38fd1498Szrj
87*38fd1498Szrj #ifdef __USING_SJLJ_EXCEPTIONS__
88*38fd1498Szrj _Unwind_SjLj_RaiseException (&header->exc.unwindHeader);
89*38fd1498Szrj #else
90*38fd1498Szrj _Unwind_RaiseException (&header->exc.unwindHeader);
91*38fd1498Szrj #endif
92*38fd1498Szrj
93*38fd1498Szrj // Some sort of unwinding error. Note that terminate is a handler.
94*38fd1498Szrj __cxa_begin_catch (&header->exc.unwindHeader);
95*38fd1498Szrj std::terminate ();
96*38fd1498Szrj }
97*38fd1498Szrj
98*38fd1498Szrj extern "C" void
__cxa_rethrow()99*38fd1498Szrj __cxxabiv1::__cxa_rethrow ()
100*38fd1498Szrj {
101*38fd1498Szrj __cxa_eh_globals *globals = __cxa_get_globals ();
102*38fd1498Szrj __cxa_exception *header = globals->caughtExceptions;
103*38fd1498Szrj
104*38fd1498Szrj globals->uncaughtExceptions += 1;
105*38fd1498Szrj
106*38fd1498Szrj // Watch for luser rethrowing with no active exception.
107*38fd1498Szrj if (header)
108*38fd1498Szrj {
109*38fd1498Szrj // Tell __cxa_end_catch this is a rethrow.
110*38fd1498Szrj if (!__is_gxx_exception_class(header->unwindHeader.exception_class))
111*38fd1498Szrj globals->caughtExceptions = 0;
112*38fd1498Szrj else
113*38fd1498Szrj {
114*38fd1498Szrj header->handlerCount = -header->handlerCount;
115*38fd1498Szrj // Only notify probe for C++ exceptions.
116*38fd1498Szrj PROBE2 (rethrow, __get_object_from_ambiguous_exception(header),
117*38fd1498Szrj header->exceptionType);
118*38fd1498Szrj }
119*38fd1498Szrj
120*38fd1498Szrj #ifdef __USING_SJLJ_EXCEPTIONS__
121*38fd1498Szrj _Unwind_SjLj_Resume_or_Rethrow (&header->unwindHeader);
122*38fd1498Szrj #else
123*38fd1498Szrj #if defined(_LIBUNWIND_STD_ABI)
124*38fd1498Szrj _Unwind_RaiseException (&header->unwindHeader);
125*38fd1498Szrj #else
126*38fd1498Szrj _Unwind_Resume_or_Rethrow (&header->unwindHeader);
127*38fd1498Szrj #endif
128*38fd1498Szrj #endif
129*38fd1498Szrj
130*38fd1498Szrj // Some sort of unwinding error. Note that terminate is a handler.
131*38fd1498Szrj __cxa_begin_catch (&header->unwindHeader);
132*38fd1498Szrj }
133*38fd1498Szrj std::terminate ();
134*38fd1498Szrj }
135