1e4b17023SJohn Marino // -*- C++ -*- Exception handling routines for throwing.
2e4b17023SJohn Marino // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3e4b17023SJohn Marino // 2011, 2012 Free Software Foundation, Inc.
4e4b17023SJohn Marino //
5e4b17023SJohn Marino // This file is part of GCC.
6e4b17023SJohn Marino //
7e4b17023SJohn Marino // GCC is free software; you can redistribute it and/or modify
8e4b17023SJohn Marino // it under the terms of the GNU General Public License as published by
9e4b17023SJohn Marino // the Free Software Foundation; either version 3, or (at your option)
10e4b17023SJohn Marino // any later version.
11e4b17023SJohn Marino //
12e4b17023SJohn Marino // GCC is distributed in the hope that it will be useful,
13e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
14e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15e4b17023SJohn Marino // GNU General Public License for more details.
16e4b17023SJohn Marino //
17e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
18e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
19e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
20e4b17023SJohn Marino
21e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
22e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
23e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
25e4b17023SJohn Marino
26e4b17023SJohn Marino #include <bits/c++config.h>
27e4b17023SJohn Marino #include "unwind-cxx.h"
28e4b17023SJohn Marino
29e4b17023SJohn Marino using namespace __cxxabiv1;
30e4b17023SJohn Marino
31e4b17023SJohn Marino
32e4b17023SJohn Marino static void
__gxx_exception_cleanup(_Unwind_Reason_Code code,_Unwind_Exception * exc)33e4b17023SJohn Marino __gxx_exception_cleanup (_Unwind_Reason_Code code, _Unwind_Exception *exc)
34e4b17023SJohn Marino {
35e4b17023SJohn Marino // This cleanup is set only for primaries.
36e4b17023SJohn Marino __cxa_refcounted_exception *header
37e4b17023SJohn Marino = __get_refcounted_exception_header_from_ue (exc);
38e4b17023SJohn Marino
39e4b17023SJohn Marino // We only want to be called through _Unwind_DeleteException.
40e4b17023SJohn Marino // _Unwind_DeleteException in the HP-UX IA64 libunwind library
41e4b17023SJohn Marino // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
42e4b17023SJohn Marino // like the GCC _Unwind_DeleteException function does.
43e4b17023SJohn Marino if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON)
44e4b17023SJohn Marino __terminate (header->exc.terminateHandler);
45e4b17023SJohn Marino
46e4b17023SJohn Marino #if ATOMIC_INT_LOCK_FREE > 1
47e4b17023SJohn Marino if (__atomic_sub_fetch (&header->referenceCount, 1, __ATOMIC_ACQ_REL) == 0)
48e4b17023SJohn Marino {
49e4b17023SJohn Marino #endif
50e4b17023SJohn Marino if (header->exc.exceptionDestructor)
51e4b17023SJohn Marino header->exc.exceptionDestructor (header + 1);
52e4b17023SJohn Marino
53e4b17023SJohn Marino __cxa_free_exception (header + 1);
54e4b17023SJohn Marino #if ATOMIC_INT_LOCK_FREE > 1
55e4b17023SJohn Marino }
56e4b17023SJohn Marino #endif
57e4b17023SJohn Marino }
58e4b17023SJohn Marino
59e4b17023SJohn Marino
60e4b17023SJohn Marino extern "C" void
__cxa_throw(void * obj,std::type_info * tinfo,void (_GLIBCXX_CDTOR_CALLABI * dest)(void *))61e4b17023SJohn Marino __cxxabiv1::__cxa_throw (void *obj, std::type_info *tinfo,
62e4b17023SJohn Marino void (_GLIBCXX_CDTOR_CALLABI *dest) (void *))
63e4b17023SJohn Marino {
64*95d28233SJohn Marino __cxa_eh_globals *globals = __cxa_get_globals ();
65*95d28233SJohn Marino globals->uncaughtExceptions += 1;
66*95d28233SJohn Marino
67e4b17023SJohn Marino // Definitely a primary.
68e4b17023SJohn Marino __cxa_refcounted_exception *header
69e4b17023SJohn Marino = __get_refcounted_exception_header_from_obj (obj);
70e4b17023SJohn Marino header->referenceCount = 1;
71e4b17023SJohn Marino header->exc.exceptionType = tinfo;
72e4b17023SJohn Marino header->exc.exceptionDestructor = dest;
73e4b17023SJohn Marino header->exc.unexpectedHandler = __unexpected_handler;
74e4b17023SJohn Marino header->exc.terminateHandler = __terminate_handler;
75e4b17023SJohn Marino __GXX_INIT_PRIMARY_EXCEPTION_CLASS(header->exc.unwindHeader.exception_class);
76e4b17023SJohn Marino header->exc.unwindHeader.exception_cleanup = __gxx_exception_cleanup;
77e4b17023SJohn Marino
78e4b17023SJohn Marino #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
79e4b17023SJohn Marino _Unwind_SjLj_RaiseException (&header->exc.unwindHeader);
80e4b17023SJohn Marino #else
81e4b17023SJohn Marino _Unwind_RaiseException (&header->exc.unwindHeader);
82e4b17023SJohn Marino #endif
83e4b17023SJohn Marino
84e4b17023SJohn Marino // Some sort of unwinding error. Note that terminate is a handler.
85e4b17023SJohn Marino __cxa_begin_catch (&header->exc.unwindHeader);
86e4b17023SJohn Marino std::terminate ();
87e4b17023SJohn Marino }
88e4b17023SJohn Marino
89e4b17023SJohn Marino extern "C" void
__cxa_rethrow()90e4b17023SJohn Marino __cxxabiv1::__cxa_rethrow ()
91e4b17023SJohn Marino {
92e4b17023SJohn Marino __cxa_eh_globals *globals = __cxa_get_globals ();
93e4b17023SJohn Marino __cxa_exception *header = globals->caughtExceptions;
94e4b17023SJohn Marino
95e4b17023SJohn Marino globals->uncaughtExceptions += 1;
96e4b17023SJohn Marino
97e4b17023SJohn Marino // Watch for luser rethrowing with no active exception.
98e4b17023SJohn Marino if (header)
99e4b17023SJohn Marino {
100e4b17023SJohn Marino // Tell __cxa_end_catch this is a rethrow.
101e4b17023SJohn Marino if (!__is_gxx_exception_class(header->unwindHeader.exception_class))
102e4b17023SJohn Marino globals->caughtExceptions = 0;
103e4b17023SJohn Marino else
104e4b17023SJohn Marino header->handlerCount = -header->handlerCount;
105e4b17023SJohn Marino
106e4b17023SJohn Marino #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
107e4b17023SJohn Marino _Unwind_SjLj_Resume_or_Rethrow (&header->unwindHeader);
108e4b17023SJohn Marino #else
109e4b17023SJohn Marino #if defined(_LIBUNWIND_STD_ABI)
110e4b17023SJohn Marino _Unwind_RaiseException (&header->unwindHeader);
111e4b17023SJohn Marino #else
112e4b17023SJohn Marino _Unwind_Resume_or_Rethrow (&header->unwindHeader);
113e4b17023SJohn Marino #endif
114e4b17023SJohn Marino #endif
115e4b17023SJohn Marino
116e4b17023SJohn Marino // Some sort of unwinding error. Note that terminate is a handler.
117e4b17023SJohn Marino __cxa_begin_catch (&header->unwindHeader);
118e4b17023SJohn Marino }
119e4b17023SJohn Marino std::terminate ();
120e4b17023SJohn Marino }
121