1404b540aSrobert // -*- C++ -*- Exception handling and frame unwind runtime interface routines.
2404b540aSrobert // Copyright (C) 2001 Free Software Foundation, Inc.
3404b540aSrobert //
4404b540aSrobert // This file is part of GCC.
5404b540aSrobert //
6404b540aSrobert // GCC is free software; you can redistribute it and/or modify
7404b540aSrobert // it under the terms of the GNU General Public License as published by
8404b540aSrobert // the Free Software Foundation; either version 2, or (at your option)
9404b540aSrobert // any later version.
10404b540aSrobert //
11404b540aSrobert // GCC is distributed in the hope that it will be useful,
12404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
13404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14404b540aSrobert // GNU General Public License for more details.
15404b540aSrobert //
16404b540aSrobert // You should have received a copy of the GNU General Public License
17404b540aSrobert // along with GCC; see the file COPYING. If not, write to
18404b540aSrobert // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19404b540aSrobert // Boston, MA 02110-1301, USA.
20404b540aSrobert
21404b540aSrobert // As a special exception, you may use this file as part of a free software
22404b540aSrobert // library without restriction. Specifically, if other files instantiate
23404b540aSrobert // templates or use macros or inline functions from this file, or you compile
24404b540aSrobert // this file and link it with other files to produce an executable, this
25404b540aSrobert // file does not by itself cause the resulting executable to be covered by
26404b540aSrobert // the GNU General Public License. This exception does not however
27404b540aSrobert // invalidate any other reasons why the executable file might be covered by
28404b540aSrobert // the GNU General Public License.
29404b540aSrobert
30404b540aSrobert // This is derived from the C++ ABI for IA-64. Where we diverge
31404b540aSrobert // for cross-architecture compatibility are noted with "@@@".
32404b540aSrobert
33404b540aSrobert #ifndef _UNWIND_CXX_H
34404b540aSrobert #define _UNWIND_CXX_H 1
35404b540aSrobert
36404b540aSrobert // Level 2: C++ ABI
37404b540aSrobert
38404b540aSrobert #include <typeinfo>
39404b540aSrobert #include <exception>
40404b540aSrobert #include <cstddef>
41404b540aSrobert #include "unwind.h"
42404b540aSrobert
43404b540aSrobert #pragma GCC visibility push(default)
44404b540aSrobert
45404b540aSrobert namespace __cxxabiv1
46404b540aSrobert {
47404b540aSrobert
48404b540aSrobert // A C++ exception object consists of a header, which is a wrapper around
49404b540aSrobert // an unwind object header with additional C++ specific information,
50404b540aSrobert // followed by the exception object itself.
51404b540aSrobert
52404b540aSrobert struct __cxa_exception
53404b540aSrobert {
54404b540aSrobert // Manage the exception object itself.
55404b540aSrobert std::type_info *exceptionType;
56404b540aSrobert void (*exceptionDestructor)(void *);
57404b540aSrobert
58404b540aSrobert // The C++ standard has entertaining rules wrt calling set_terminate
59404b540aSrobert // and set_unexpected in the middle of the exception cleanup process.
60404b540aSrobert std::unexpected_handler unexpectedHandler;
61404b540aSrobert std::terminate_handler terminateHandler;
62404b540aSrobert
63404b540aSrobert // The caught exception stack threads through here.
64404b540aSrobert __cxa_exception *nextException;
65404b540aSrobert
66404b540aSrobert // How many nested handlers have caught this exception. A negated
67404b540aSrobert // value is a signal that this object has been rethrown.
68404b540aSrobert int handlerCount;
69404b540aSrobert
70404b540aSrobert #ifdef __ARM_EABI_UNWINDER__
71404b540aSrobert // Stack of exceptions in cleanups.
72404b540aSrobert __cxa_exception* nextPropagatingException;
73404b540aSrobert
74404b540aSrobert // The nuber of active cleanup handlers for this exception.
75404b540aSrobert int propagationCount;
76404b540aSrobert #else
77404b540aSrobert // Cache parsed handler data from the personality routine Phase 1
78404b540aSrobert // for Phase 2 and __cxa_call_unexpected.
79404b540aSrobert int handlerSwitchValue;
80404b540aSrobert const unsigned char *actionRecord;
81404b540aSrobert const unsigned char *languageSpecificData;
82404b540aSrobert _Unwind_Ptr catchTemp;
83404b540aSrobert void *adjustedPtr;
84404b540aSrobert #endif
85404b540aSrobert
86404b540aSrobert // The generic exception header. Must be last.
87404b540aSrobert _Unwind_Exception unwindHeader;
88404b540aSrobert };
89404b540aSrobert
90404b540aSrobert // Each thread in a C++ program has access to a __cxa_eh_globals object.
91404b540aSrobert struct __cxa_eh_globals
92404b540aSrobert {
93404b540aSrobert __cxa_exception *caughtExceptions;
94404b540aSrobert unsigned int uncaughtExceptions;
95404b540aSrobert #ifdef __ARM_EABI_UNWINDER__
96404b540aSrobert __cxa_exception* propagatingExceptions;
97404b540aSrobert #endif
98404b540aSrobert };
99404b540aSrobert
100404b540aSrobert
101404b540aSrobert // The __cxa_eh_globals for the current thread can be obtained by using
102404b540aSrobert // either of the following functions. The "fast" version assumes at least
103404b540aSrobert // one prior call of __cxa_get_globals has been made from the current
104404b540aSrobert // thread, so no initialization is necessary.
105404b540aSrobert extern "C" __cxa_eh_globals *__cxa_get_globals () throw();
106404b540aSrobert extern "C" __cxa_eh_globals *__cxa_get_globals_fast () throw();
107404b540aSrobert
108404b540aSrobert // Allocate memory for the exception plus the thown object.
109404b540aSrobert extern "C" void *__cxa_allocate_exception(std::size_t thrown_size) throw();
110404b540aSrobert
111404b540aSrobert // Free the space allocated for the exception.
112404b540aSrobert extern "C" void __cxa_free_exception(void *thrown_exception) throw();
113404b540aSrobert
114404b540aSrobert // Throw the exception.
115404b540aSrobert extern "C" void __cxa_throw (void *thrown_exception,
116404b540aSrobert std::type_info *tinfo,
117404b540aSrobert void (*dest) (void *))
118404b540aSrobert __attribute__((noreturn));
119404b540aSrobert
120404b540aSrobert // Used to implement exception handlers.
121404b540aSrobert extern "C" void *__cxa_get_exception_ptr (void *) throw();
122404b540aSrobert extern "C" void *__cxa_begin_catch (void *) throw();
123404b540aSrobert extern "C" void __cxa_end_catch ();
124404b540aSrobert extern "C" void __cxa_rethrow () __attribute__((noreturn));
125404b540aSrobert
126404b540aSrobert // These facilitate code generation for recurring situations.
127404b540aSrobert extern "C" void __cxa_bad_cast ();
128404b540aSrobert extern "C" void __cxa_bad_typeid ();
129404b540aSrobert
130404b540aSrobert // @@@ These are not directly specified by the IA-64 C++ ABI.
131404b540aSrobert
132404b540aSrobert // Handles re-checking the exception specification if unexpectedHandler
133404b540aSrobert // throws, and if bad_exception needs to be thrown. Called from the
134404b540aSrobert // compiler.
135404b540aSrobert extern "C" void __cxa_call_unexpected (void *) __attribute__((noreturn));
136*eb123a78Sjsg extern "C" void __cxa_call_terminate (_Unwind_Exception*) __attribute__((noreturn));
137404b540aSrobert
138404b540aSrobert #ifdef __ARM_EABI_UNWINDER__
139404b540aSrobert // Arm EABI specified routines.
140404b540aSrobert typedef enum {
141404b540aSrobert ctm_failed = 0,
142404b540aSrobert ctm_succeeded = 1,
143404b540aSrobert ctm_succeeded_with_ptr_to_base = 2
144404b540aSrobert } __cxa_type_match_result;
145404b540aSrobert extern "C" bool __cxa_type_match(_Unwind_Exception*, const std::type_info*,
146404b540aSrobert bool, void**);
147404b540aSrobert extern "C" void __cxa_begin_cleanup (_Unwind_Exception*);
148404b540aSrobert extern "C" void __cxa_end_cleanup (void);
149404b540aSrobert #endif
150404b540aSrobert
151404b540aSrobert // Invokes given handler, dying appropriately if the user handler was
152404b540aSrobert // so inconsiderate as to return.
153404b540aSrobert extern void __terminate(std::terminate_handler) __attribute__((noreturn));
154404b540aSrobert extern void __unexpected(std::unexpected_handler) __attribute__((noreturn));
155404b540aSrobert
156404b540aSrobert // The current installed user handlers.
157404b540aSrobert extern std::terminate_handler __terminate_handler;
158404b540aSrobert extern std::unexpected_handler __unexpected_handler;
159404b540aSrobert
160404b540aSrobert // These are explicitly GNU C++ specific.
161404b540aSrobert
162404b540aSrobert // Acquire the C++ exception header from the C++ object.
163404b540aSrobert static inline __cxa_exception *
__get_exception_header_from_obj(void * ptr)164404b540aSrobert __get_exception_header_from_obj (void *ptr)
165404b540aSrobert {
166404b540aSrobert return reinterpret_cast<__cxa_exception *>(ptr) - 1;
167404b540aSrobert }
168404b540aSrobert
169404b540aSrobert // Acquire the C++ exception header from the generic exception header.
170404b540aSrobert static inline __cxa_exception *
__get_exception_header_from_ue(_Unwind_Exception * exc)171404b540aSrobert __get_exception_header_from_ue (_Unwind_Exception *exc)
172404b540aSrobert {
173404b540aSrobert return reinterpret_cast<__cxa_exception *>(exc + 1) - 1;
174404b540aSrobert }
175404b540aSrobert
176404b540aSrobert #ifdef __ARM_EABI_UNWINDER__
177404b540aSrobert static inline bool
__is_gxx_exception_class(_Unwind_Exception_Class c)178404b540aSrobert __is_gxx_exception_class(_Unwind_Exception_Class c)
179404b540aSrobert {
180404b540aSrobert // TODO: Take advantage of the fact that c will always be word aligned.
181404b540aSrobert return c[0] == 'G'
182404b540aSrobert && c[1] == 'N'
183404b540aSrobert && c[2] == 'U'
184404b540aSrobert && c[3] == 'C'
185404b540aSrobert && c[4] == 'C'
186404b540aSrobert && c[5] == '+'
187404b540aSrobert && c[6] == '+'
188404b540aSrobert && c[7] == '\0';
189404b540aSrobert }
190404b540aSrobert
191404b540aSrobert static inline void
__GXX_INIT_EXCEPTION_CLASS(_Unwind_Exception_Class c)192404b540aSrobert __GXX_INIT_EXCEPTION_CLASS(_Unwind_Exception_Class c)
193404b540aSrobert {
194404b540aSrobert c[0] = 'G';
195404b540aSrobert c[1] = 'N';
196404b540aSrobert c[2] = 'U';
197404b540aSrobert c[3] = 'C';
198404b540aSrobert c[4] = 'C';
199404b540aSrobert c[5] = '+';
200404b540aSrobert c[6] = '+';
201404b540aSrobert c[7] = '\0';
202404b540aSrobert }
203404b540aSrobert
204404b540aSrobert static inline void*
__gxx_caught_object(_Unwind_Exception * eo)205404b540aSrobert __gxx_caught_object(_Unwind_Exception* eo)
206404b540aSrobert {
207404b540aSrobert return (void*)eo->barrier_cache.bitpattern[0];
208404b540aSrobert }
209404b540aSrobert #else // !__ARM_EABI_UNWINDER__
210404b540aSrobert // This is the exception class we report -- "GNUCC++\0".
211404b540aSrobert const _Unwind_Exception_Class __gxx_exception_class
212404b540aSrobert = ((((((((_Unwind_Exception_Class) 'G'
213404b540aSrobert << 8 | (_Unwind_Exception_Class) 'N')
214404b540aSrobert << 8 | (_Unwind_Exception_Class) 'U')
215404b540aSrobert << 8 | (_Unwind_Exception_Class) 'C')
216404b540aSrobert << 8 | (_Unwind_Exception_Class) 'C')
217404b540aSrobert << 8 | (_Unwind_Exception_Class) '+')
218404b540aSrobert << 8 | (_Unwind_Exception_Class) '+')
219404b540aSrobert << 8 | (_Unwind_Exception_Class) '\0');
220404b540aSrobert
221404b540aSrobert static inline bool
__is_gxx_exception_class(_Unwind_Exception_Class c)222404b540aSrobert __is_gxx_exception_class(_Unwind_Exception_Class c)
223404b540aSrobert {
224404b540aSrobert return c == __gxx_exception_class;
225404b540aSrobert }
226404b540aSrobert
227404b540aSrobert #define __GXX_INIT_EXCEPTION_CLASS(c) c = __gxx_exception_class
228404b540aSrobert
229404b540aSrobert // GNU C++ personality routine, Version 0.
230404b540aSrobert extern "C" _Unwind_Reason_Code __gxx_personality_v0
231404b540aSrobert (int, _Unwind_Action, _Unwind_Exception_Class,
232404b540aSrobert struct _Unwind_Exception *, struct _Unwind_Context *);
233404b540aSrobert
234404b540aSrobert // GNU C++ sjlj personality routine, Version 0.
235404b540aSrobert extern "C" _Unwind_Reason_Code __gxx_personality_sj0
236404b540aSrobert (int, _Unwind_Action, _Unwind_Exception_Class,
237404b540aSrobert struct _Unwind_Exception *, struct _Unwind_Context *);
238404b540aSrobert
239404b540aSrobert static inline void*
__gxx_caught_object(_Unwind_Exception * eo)240404b540aSrobert __gxx_caught_object(_Unwind_Exception* eo)
241404b540aSrobert {
242404b540aSrobert __cxa_exception* header = __get_exception_header_from_ue (eo);
243404b540aSrobert return header->adjustedPtr;
244404b540aSrobert }
245404b540aSrobert #endif // !__ARM_EABI_UNWINDER__
246404b540aSrobert
247404b540aSrobert } /* namespace __cxxabiv1 */
248404b540aSrobert
249404b540aSrobert #pragma GCC visibility pop
250404b540aSrobert
251404b540aSrobert #endif // _UNWIND_CXX_H
252