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