1 // -*- C++ -*- Exception handling and frame unwind runtime interface routines.
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 // Free Software Foundation, Inc.
5 // This file is part of GCC.
7 // GCC is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2, or (at your option)
12 // GCC is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with GCC; see the file COPYING. If not, write to
19 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 // Boston, MA 02110-1301, USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 // This is derived from the C++ ABI for IA-64. Where we diverge
32 // for cross-architecture compatibility are noted with "@@@".
35 #define _UNWIND_CXX_H 1
43 #include <bits/atomic_word.h>
45 #pragma GCC visibility push(default)
50 // A primary C++ exception object consists of a header, which is a wrapper
51 // around an unwind object header with additional C++ specific information,
52 // followed by the exception object itself.
54 struct __cxa_exception
56 // Manage this header.
57 _Atomic_word referenceCount;
59 // Manage the exception object itself.
60 std::type_info *exceptionType;
61 void (*exceptionDestructor)(void *);
63 // The C++ standard has entertaining rules wrt calling set_terminate
64 // and set_unexpected in the middle of the exception cleanup process.
65 std::unexpected_handler unexpectedHandler;
66 std::terminate_handler terminateHandler;
68 // The caught exception stack threads through here.
69 __cxa_exception *nextException;
71 // How many nested handlers have caught this exception. A negated
72 // value is a signal that this object has been rethrown.
75 #ifdef __ARM_EABI_UNWINDER__
76 // Stack of exceptions in cleanups.
77 __cxa_exception* nextPropagatingException;
79 // The nuber of active cleanup handlers for this exception.
82 // Cache parsed handler data from the personality routine Phase 1
83 // for Phase 2 and __cxa_call_unexpected.
84 int handlerSwitchValue;
85 const unsigned char *actionRecord;
86 const unsigned char *languageSpecificData;
87 _Unwind_Ptr catchTemp;
91 // The generic exception header. Must be last.
92 _Unwind_Exception unwindHeader;
95 // A dependent C++ exception object consists of a wrapper around an unwind
96 // object header with additional C++ specific information, containing a pointer
97 // to a primary exception object.
99 struct __cxa_dependent_exception
101 // The primary exception this thing depends on.
102 void *primaryException;
104 // The C++ standard has entertaining rules wrt calling set_terminate
105 // and set_unexpected in the middle of the exception cleanup process.
106 std::unexpected_handler unexpectedHandler;
107 std::terminate_handler terminateHandler;
109 // The caught exception stack threads through here.
110 __cxa_exception *nextException;
112 // How many nested handlers have caught this exception. A negated
113 // value is a signal that this object has been rethrown.
116 #ifdef __ARM_EABI_UNWINDER__
117 // Stack of exceptions in cleanups.
118 __cxa_exception* nextPropagatingException;
120 // The nuber of active cleanup handlers for this exception.
121 int propagationCount;
123 // Cache parsed handler data from the personality routine Phase 1
124 // for Phase 2 and __cxa_call_unexpected.
125 int handlerSwitchValue;
126 const unsigned char *actionRecord;
127 const unsigned char *languageSpecificData;
128 _Unwind_Ptr catchTemp;
132 // The generic exception header. Must be last.
133 _Unwind_Exception unwindHeader;
136 // Each thread in a C++ program has access to a __cxa_eh_globals object.
137 struct __cxa_eh_globals
139 __cxa_exception *caughtExceptions;
140 unsigned int uncaughtExceptions;
141 #ifdef __ARM_EABI_UNWINDER__
142 __cxa_exception* propagatingExceptions;
147 // The __cxa_eh_globals for the current thread can be obtained by using
148 // either of the following functions. The "fast" version assumes at least
149 // one prior call of __cxa_get_globals has been made from the current
150 // thread, so no initialization is necessary.
151 extern "C" __cxa_eh_globals *__cxa_get_globals () throw();
152 extern "C" __cxa_eh_globals *__cxa_get_globals_fast () throw();
154 // Allocate memory for the primary exception plus the thrown object.
155 extern "C" void *__cxa_allocate_exception(std::size_t thrown_size) throw();
157 // Free the space allocated for the primary exception.
158 extern "C" void __cxa_free_exception(void *thrown_exception) throw();
160 // Allocate memory for a dependent exception.
161 extern "C" __cxa_dependent_exception*
162 __cxa_allocate_dependent_exception() throw();
164 // Free the space allocated for the dependent exception.
166 __cxa_free_dependent_exception(__cxa_dependent_exception *ex) throw();
168 // Throw the exception.
169 extern "C" void __cxa_throw (void *thrown_exception,
170 std::type_info *tinfo,
171 void (*dest) (void *))
172 __attribute__((noreturn));
174 // Used to implement exception handlers.
175 extern "C" void *__cxa_get_exception_ptr (void *) throw();
176 extern "C" void *__cxa_begin_catch (void *) throw();
177 extern "C" void __cxa_end_catch ();
178 extern "C" void __cxa_rethrow () __attribute__((noreturn));
180 // These facilitate code generation for recurring situations.
181 extern "C" void __cxa_bad_cast ();
182 extern "C" void __cxa_bad_typeid ();
184 // @@@ These are not directly specified by the IA-64 C++ ABI.
186 // Handles re-checking the exception specification if unexpectedHandler
187 // throws, and if bad_exception needs to be thrown. Called from the
189 extern "C" void __cxa_call_unexpected (void *) __attribute__((noreturn));
190 extern "C" void __cxa_call_terminate (void*) __attribute__((noreturn));
192 #ifdef __ARM_EABI_UNWINDER__
193 // Arm EABI specified routines.
197 ctm_succeeded_with_ptr_to_base = 2
198 } __cxa_type_match_result;
199 extern "C" bool __cxa_type_match(_Unwind_Exception*, const std::type_info*,
201 extern "C" void __cxa_begin_cleanup (_Unwind_Exception*);
202 extern "C" void __cxa_end_cleanup (void);
205 // Invokes given handler, dying appropriately if the user handler was
206 // so inconsiderate as to return.
207 extern void __terminate(std::terminate_handler) __attribute__((noreturn));
208 extern void __unexpected(std::unexpected_handler) __attribute__((noreturn));
210 // The current installed user handlers.
211 extern std::terminate_handler __terminate_handler;
212 extern std::unexpected_handler __unexpected_handler;
214 // These are explicitly GNU C++ specific.
216 // Acquire the C++ exception header from the C++ object.
217 static inline __cxa_exception *
218 __get_exception_header_from_obj (void *ptr)
220 return reinterpret_cast<__cxa_exception *>(ptr) - 1;
223 // Acquire the C++ exception header from the generic exception header.
224 static inline __cxa_exception *
225 __get_exception_header_from_ue (_Unwind_Exception *exc)
227 return reinterpret_cast<__cxa_exception *>(exc + 1) - 1;
230 static inline __cxa_dependent_exception *
231 __get_dependent_exception_from_ue (_Unwind_Exception *exc)
233 return reinterpret_cast<__cxa_dependent_exception *>(exc + 1) - 1;
236 #ifdef __ARM_EABI_UNWINDER__
238 __is_gxx_exception_class(_Unwind_Exception_Class c)
240 // TODO: Take advantage of the fact that c will always be word aligned.
248 && (c[7] == '\0' || c[7] == '\x01');
251 // Only checks for primary or dependent, but not that it is a C++ exception at
254 __is_dependent_exception(_Unwind_Exception_Class c)
256 return c[7] == '\x01';
260 __GXX_INIT_PRIMARY_EXCEPTION_CLASS(_Unwind_Exception_Class c)
273 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(_Unwind_Exception_Class c)
286 __is_gxx_forced_unwind_class(_Unwind_Exception_Class c)
299 __GXX_INIT_FORCED_UNWIND_CLASS(_Unwind_Exception_Class c)
312 __gxx_caught_object(_Unwind_Exception* eo)
314 return (void*)eo->barrier_cache.bitpattern[0];
316 #else // !__ARM_EABI_UNWINDER__
317 // This is the primary exception class we report -- "GNUCC++\0".
318 const _Unwind_Exception_Class __gxx_primary_exception_class
319 = ((((((((_Unwind_Exception_Class) 'G'
320 << 8 | (_Unwind_Exception_Class) 'N')
321 << 8 | (_Unwind_Exception_Class) 'U')
322 << 8 | (_Unwind_Exception_Class) 'C')
323 << 8 | (_Unwind_Exception_Class) 'C')
324 << 8 | (_Unwind_Exception_Class) '+')
325 << 8 | (_Unwind_Exception_Class) '+')
326 << 8 | (_Unwind_Exception_Class) '\0');
328 // This is the dependent (from std::rethrow_exception) exception class we report
330 const _Unwind_Exception_Class __gxx_dependent_exception_class
331 = ((((((((_Unwind_Exception_Class) 'G'
332 << 8 | (_Unwind_Exception_Class) 'N')
333 << 8 | (_Unwind_Exception_Class) 'U')
334 << 8 | (_Unwind_Exception_Class) 'C')
335 << 8 | (_Unwind_Exception_Class) 'C')
336 << 8 | (_Unwind_Exception_Class) '+')
337 << 8 | (_Unwind_Exception_Class) '+')
338 << 8 | (_Unwind_Exception_Class) '\x01');
341 __is_gxx_exception_class(_Unwind_Exception_Class c)
343 return c == __gxx_primary_exception_class
344 || c == __gxx_dependent_exception_class;
347 // Only checks for primary or dependent, but not that it is a C++ exception at
350 __is_dependent_exception(_Unwind_Exception_Class c)
355 #define __GXX_INIT_PRIMARY_EXCEPTION_CLASS(c) c = __gxx_primary_exception_class
356 #define __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(c) \
357 c = __gxx_dependent_exception_class
359 // GNU C++ personality routine, Version 0.
360 extern "C" _Unwind_Reason_Code __gxx_personality_v0
361 (int, _Unwind_Action, _Unwind_Exception_Class,
362 struct _Unwind_Exception *, struct _Unwind_Context *);
364 // GNU C++ sjlj personality routine, Version 0.
365 extern "C" _Unwind_Reason_Code __gxx_personality_sj0
366 (int, _Unwind_Action, _Unwind_Exception_Class,
367 struct _Unwind_Exception *, struct _Unwind_Context *);
370 __gxx_caught_object(_Unwind_Exception* eo)
372 // Bad as it looks, this actually works for dependent exceptions too.
373 __cxa_exception* header = __get_exception_header_from_ue (eo);
374 return header->adjustedPtr;
376 #endif // !__ARM_EABI_UNWINDER__
379 __get_object_from_ue(_Unwind_Exception* eo) throw()
381 return __is_dependent_exception (eo->exception_class) ?
382 __get_dependent_exception_from_ue (eo)->primaryException :
387 __get_object_from_ambiguous_exception(__cxa_exception *p_or_d) throw()
389 return __get_object_from_ue (&p_or_d->unwindHeader);
393 } /* namespace __cxxabiv1 */
395 #pragma GCC visibility pop
397 #endif // _UNWIND_CXX_H