1 // -*- C++ -*- Exception handling and frame unwind runtime interface routines.
2 // Copyright (C) 2001 Free Software Foundation, Inc.
4 // This file is part of GCC.
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 2, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License
17 // along with GCC; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // This is derived from the C++ ABI for IA-64. Where we diverge
31 // for cross-architecture compatibility are noted with "@@@".
34 #define _UNWIND_CXX_H 1
45 typedef unsigned _Unwind_Word
__attribute__((__mode__(__word__
)));
46 typedef signed _Unwind_Sword
__attribute__((__mode__(__word__
)));
47 typedef unsigned _Unwind_Word
__attribute__((__mode__(__word__
)));
48 typedef unsigned _Unwind_Ptr
__attribute__((__mode__(__pointer__
)));
49 #if !defined __IPHONE_16_4 || __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_16_4
50 typedef unsigned _Unwind_Exception_Class
__attribute__((__mode__(__DI__
)));
52 typedef unsigned _Unwind_Internal_Ptr
__attribute__((__mode__(__pointer__
)));
54 #pragma GCC visibility push(default)
59 // A C++ exception object consists of a header, which is a wrapper around
60 // an unwind object header with additional C++ specific information,
61 // followed by the exception object itself.
63 struct __cxa_exception
67 // This is a new field added with LLVM 10
68 // <https://github.com/llvm/llvm-project/commit/674ec1eb16678b8addc02a4b0534ab383d22fa77>
69 // "[libcxxabi] Insert padding in __cxa_exception struct for compatibility". The HACK in
70 // fillUnoException (bridges/source/cpp_uno/gcc3_macosx_x86-64/except.cxx) tries to find out at
71 // runtime whether a __cxa_exception has this member. Once we can be sure that we only run
72 // against new libcxxabi that has this member, we can drop the "#if 0" here and drop the hack
73 // in fillUnoException.
75 // Now _Unwind_Exception is marked with __attribute__((aligned)),
76 // which implies __cxa_exception is also aligned. Insert padding
77 // in the beginning of the struct, rather than before unwindHeader.
81 // This is a new field to support C++ 0x exception_ptr.
82 // For binary compatibility it is at the start of this
83 // struct which is prepended to the object thrown in
84 // __cxa_allocate_exception.
85 size_t referenceCount
;
87 // Manage the exception object itself.
88 std::type_info
*exceptionType
;
89 void (*exceptionDestructor
)(void *);
91 // The C++ standard has entertaining rules wrt calling set_terminate
92 // and set_unexpected in the middle of the exception cleanup process.
93 void (*unexpectedHandler
)(); // std::unexpected_handler dropped from C++17
94 std::terminate_handler terminateHandler
;
96 // The caught exception stack threads through here.
97 __cxa_exception
*nextException
;
99 // How many nested handlers have caught this exception. A negated
100 // value is a signal that this object has been rethrown.
103 #ifdef __ARM_EABI_UNWINDER__
104 // Stack of exceptions in cleanups.
105 __cxa_exception
* nextPropagatingException
;
107 // The number of active cleanup handlers for this exception.
108 int propagationCount
;
110 // Cache parsed handler data from the personality routine Phase 1
111 // for Phase 2 and __cxa_call_unexpected.
112 int handlerSwitchValue
;
113 const unsigned char *actionRecord
;
114 const unsigned char *languageSpecificData
;
115 _Unwind_Ptr catchTemp
;
119 // This is a new field to support C++ 0x exception_ptr.
120 // For binary compatibility it is placed where the compiler
121 // previously adding padded to 64-bit align unwindHeader.
122 size_t referenceCount
;
125 // The generic exception header. Must be last.
126 _Unwind_Exception unwindHeader
;
129 // Each thread in a C++ program has access to a __cxa_eh_globals object.
130 struct __cxa_eh_globals
132 __cxa_exception
*caughtExceptions
;
133 unsigned int uncaughtExceptions
;
134 #ifdef __ARM_EABI_UNWINDER__
135 __cxa_exception
* propagatingExceptions
;
140 // The __cxa_eh_globals for the current thread can be obtained by using
141 // either of the following functions. The "fast" version assumes at least
142 // one prior call of __cxa_get_globals has been made from the current
143 // thread, so no initialization is necessary.
144 extern "C" __cxa_eh_globals
*__cxa_get_globals () throw();
145 extern "C" __cxa_eh_globals
*__cxa_get_globals_fast () throw();
147 // Allocate memory for the exception plus the thrown object.
148 extern "C" void *__cxa_allocate_exception(size_t thrown_size
) throw();
150 // Free the space allocated for the exception.
151 extern "C" void __cxa_free_exception(void *thrown_exception
) throw();
153 #pragma GCC visibility push(hidden)
154 extern "C" void *__cxa_allocate_dependent_exception() throw();
155 extern "C" void __cxa_free_dependent_exception(void *thrown_exception
) throw();
156 #pragma GCC visibility pop
158 // Throw the exception.
159 extern "C" void __cxa_throw (void *thrown_exception
,
160 std::type_info
*tinfo
,
161 void (*dest
) (void *))
162 __attribute__((noreturn
));
164 // Used to implement exception handlers.
165 extern "C" void *__cxa_get_exception_ptr (void *) throw();
166 extern "C" void *__cxa_begin_catch (void *) throw();
167 extern "C" void __cxa_end_catch ();
168 extern "C" void __cxa_rethrow () __attribute__((noreturn
));
170 // These facilitate code generation for recurring situations.
171 extern "C" void __cxa_bad_cast ();
172 extern "C" void __cxa_bad_typeid ();
174 // @@@ These are not directly specified by the IA-64 C++ ABI.
176 // Handles re-checking the exception specification if unexpectedHandler
177 // throws, and if bad_exception needs to be thrown. Called from the
179 extern "C" void __cxa_call_unexpected (void *) __attribute__((noreturn
));
180 extern "C" void __cxa_call_terminate (void*) __attribute__((noreturn
));
182 #ifdef __ARM_EABI_UNWINDER__
183 // Arm EABI specified routines.
187 ctm_succeeded_with_ptr_to_base
= 2
188 } __cxa_type_match_result
;
189 extern "C" bool __cxa_type_match(_Unwind_Exception
*, const std::type_info
*,
191 extern "C" void __cxa_begin_cleanup (_Unwind_Exception
*);
192 extern "C" void __cxa_end_cleanup (void);
195 // These are explicitly GNU C++ specific.
197 // Acquire the C++ exception header from the C++ object.
198 static inline __cxa_exception
*
199 __get_exception_header_from_obj (void *ptr
)
201 return reinterpret_cast<__cxa_exception
*>(ptr
) - 1;
204 // Acquire the C++ exception header from the generic exception header.
205 static inline __cxa_exception
*
206 __get_exception_header_from_ue (_Unwind_Exception
*exc
)
208 return reinterpret_cast<__cxa_exception
*>(exc
+ 1) - 1;
211 #ifdef __ARM_EABI_UNWINDER__
213 __is_gxx_exception_class(_Unwind_Exception_Class c
)
215 // TODO: Take advantage of the fact that c will always be word aligned.
226 __GXX_INIT_EXCEPTION_CLASS(_Unwind_Exception_Class c
)
239 __gxx_caught_object(_Unwind_Exception
* eo
)
241 return (void*)eo
->barrier_cache
.bitpattern
[0];
243 #else // !__ARM_EABI_UNWINDER__
244 // This is the exception class we report -- "GNUCC++\0".
245 const _Unwind_Exception_Class __gxx_exception_class
246 = ((((((((_Unwind_Exception_Class
) 'G'
247 << 8 | (_Unwind_Exception_Class
) 'N')
248 << 8 | (_Unwind_Exception_Class
) 'U')
249 << 8 | (_Unwind_Exception_Class
) 'C')
250 << 8 | (_Unwind_Exception_Class
) 'C')
251 << 8 | (_Unwind_Exception_Class
) '+')
252 << 8 | (_Unwind_Exception_Class
) '+')
253 << 8 | (_Unwind_Exception_Class
) '\0');
256 __is_gxx_exception_class(_Unwind_Exception_Class c
)
258 return (c
& ((_Unwind_Exception_Class
)~0 << 8)) == __gxx_exception_class
;
261 #define __GXX_INIT_EXCEPTION_CLASS(c) c = __gxx_exception_class
263 // GNU C++ personality routine, Version 0.
264 extern "C" _Unwind_Reason_Code __gxx_personality_v0
265 (int, _Unwind_Action
, _Unwind_Exception_Class
,
266 struct _Unwind_Exception
*, struct _Unwind_Context
*);
268 // GNU C++ sjlj personality routine, Version 0.
269 extern "C" _Unwind_Reason_Code __gxx_personality_sj0
270 (int, _Unwind_Action
, _Unwind_Exception_Class
,
271 struct _Unwind_Exception
*, struct _Unwind_Context
*);
274 __gxx_caught_object(_Unwind_Exception
* eo
)
276 __cxa_exception
* header
= __get_exception_header_from_ue (eo
);
277 return header
->adjustedPtr
;
279 #endif // !__ARM_EABI_UNWINDER__
281 } /* namespace __cxxabiv1 */
283 #pragma GCC visibility pop
285 #endif // _UNWIND_CXX_H