1 // -*- C++ -*- Exception handling and frame unwind runtime interface routines.
2 // Copyright (C) 2001, 2008 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
42 #include <ext/atomicity.h>
44 #pragma GCC visibility push(default)
49 // A primary C++ exception object consists of a header, which is a wrapper
50 // around an unwind object header with additional C++ specific information,
51 // followed by the exception object itself.
53 struct __cxa_exception
55 // Manage this header.
56 _Atomic_word referenceCount
;
58 // Manage the exception object itself.
59 std::type_info
*exceptionType
;
60 void (*exceptionDestructor
)(void *);
62 // The C++ standard has entertaining rules wrt calling set_terminate
63 // and set_unexpected in the middle of the exception cleanup process.
64 std::unexpected_handler unexpectedHandler
;
65 std::terminate_handler terminateHandler
;
67 // The caught exception stack threads through here.
68 __cxa_exception
*nextException
;
70 // How many nested handlers have caught this exception. A negated
71 // value is a signal that this object has been rethrown.
74 #ifdef __ARM_EABI_UNWINDER__
75 // Stack of exceptions in cleanups.
76 __cxa_exception
* nextPropagatingException
;
78 // The nuber of active cleanup handlers for this exception.
81 // Cache parsed handler data from the personality routine Phase 1
82 // for Phase 2 and __cxa_call_unexpected.
83 int handlerSwitchValue
;
84 const unsigned char *actionRecord
;
85 const unsigned char *languageSpecificData
;
86 _Unwind_Ptr catchTemp
;
90 // The generic exception header. Must be last.
91 _Unwind_Exception unwindHeader
;
94 // A dependent C++ exception object consists of a wrapper around an unwind
95 // object header with additional C++ specific information, containing a pointer
96 // to a primary exception object.
98 struct __cxa_dependent_exception
100 // The primary exception this thing depends on.
101 void *primaryException
;
103 // The C++ standard has entertaining rules wrt calling set_terminate
104 // and set_unexpected in the middle of the exception cleanup process.
105 std::unexpected_handler unexpectedHandler
;
106 std::terminate_handler terminateHandler
;
108 // The caught exception stack threads through here.
109 __cxa_exception
*nextException
;
111 // How many nested handlers have caught this exception. A negated
112 // value is a signal that this object has been rethrown.
115 #ifdef __ARM_EABI_UNWINDER__
116 // Stack of exceptions in cleanups.
117 __cxa_exception
* nextPropagatingException
;
119 // The nuber of active cleanup handlers for this exception.
120 int propagationCount
;
122 // Cache parsed handler data from the personality routine Phase 1
123 // for Phase 2 and __cxa_call_unexpected.
124 int handlerSwitchValue
;
125 const unsigned char *actionRecord
;
126 const unsigned char *languageSpecificData
;
127 _Unwind_Ptr catchTemp
;
131 // The generic exception header. Must be last.
132 _Unwind_Exception unwindHeader
;
135 // Each thread in a C++ program has access to a __cxa_eh_globals object.
136 struct __cxa_eh_globals
138 __cxa_exception
*caughtExceptions
;
139 unsigned int uncaughtExceptions
;
140 #ifdef __ARM_EABI_UNWINDER__
141 __cxa_exception
* propagatingExceptions
;
146 // The __cxa_eh_globals for the current thread can be obtained by using
147 // either of the following functions. The "fast" version assumes at least
148 // one prior call of __cxa_get_globals has been made from the current
149 // thread, so no initialization is necessary.
150 extern "C" __cxa_eh_globals
*__cxa_get_globals () throw();
151 extern "C" __cxa_eh_globals
*__cxa_get_globals_fast () throw();
153 // Allocate memory for the primary exception plus the thrown object.
154 extern "C" void *__cxa_allocate_exception(std::size_t thrown_size
) throw();
156 // Free the space allocated for the primary exception.
157 extern "C" void __cxa_free_exception(void *thrown_exception
) throw();
159 // Allocate memory for a dependent exception.
160 extern "C" __cxa_dependent_exception
*
161 __cxa_allocate_dependent_exception() throw();
163 // Free the space allocated for the dependent exception.
165 __cxa_free_dependent_exception(__cxa_dependent_exception
*ex
) throw();
167 // Throw the exception.
168 extern "C" void __cxa_throw (void *thrown_exception
,
169 std::type_info
*tinfo
,
170 void (*dest
) (void *))
171 __attribute__((noreturn
));
173 // Used to implement exception handlers.
174 extern "C" void *__cxa_get_exception_ptr (void *) throw();
175 extern "C" void *__cxa_begin_catch (void *) throw();
176 extern "C" void __cxa_end_catch ();
177 extern "C" void __cxa_rethrow () __attribute__((noreturn
));
179 // These facilitate code generation for recurring situations.
180 extern "C" void __cxa_bad_cast ();
181 extern "C" void __cxa_bad_typeid ();
183 // @@@ These are not directly specified by the IA-64 C++ ABI.
185 // Handles re-checking the exception specification if unexpectedHandler
186 // throws, and if bad_exception needs to be thrown. Called from the
188 extern "C" void __cxa_call_unexpected (void *) __attribute__((noreturn
));
189 extern "C" void __cxa_call_terminate (void*) __attribute__((noreturn
));
191 #ifdef __ARM_EABI_UNWINDER__
192 // Arm EABI specified routines.
196 ctm_succeeded_with_ptr_to_base
= 2
197 } __cxa_type_match_result
;
198 extern "C" bool __cxa_type_match(_Unwind_Exception
*, const std::type_info
*,
200 extern "C" void __cxa_begin_cleanup (_Unwind_Exception
*);
201 extern "C" void __cxa_end_cleanup (void);
204 // Invokes given handler, dying appropriately if the user handler was
205 // so inconsiderate as to return.
206 extern void __terminate(std::terminate_handler
) __attribute__((noreturn
));
207 extern void __unexpected(std::unexpected_handler
) __attribute__((noreturn
));
209 // The current installed user handlers.
210 extern std::terminate_handler __terminate_handler
;
211 extern std::unexpected_handler __unexpected_handler
;
213 // These are explicitly GNU C++ specific.
215 // Acquire the C++ exception header from the C++ object.
216 static inline __cxa_exception
*
217 __get_exception_header_from_obj (void *ptr
)
219 return reinterpret_cast<__cxa_exception
*>(ptr
) - 1;
222 // Acquire the C++ exception header from the generic exception header.
223 static inline __cxa_exception
*
224 __get_exception_header_from_ue (_Unwind_Exception
*exc
)
226 return reinterpret_cast<__cxa_exception
*>(exc
+ 1) - 1;
229 static inline __cxa_dependent_exception
*
230 __get_dependent_exception_from_ue (_Unwind_Exception
*exc
)
232 return reinterpret_cast<__cxa_dependent_exception
*>(exc
+ 1) - 1;
235 #ifdef __ARM_EABI_UNWINDER__
237 __is_gxx_exception_class(_Unwind_Exception_Class c
)
239 // TODO: Take advantage of the fact that c will always be word aligned.
247 && (c
[7] == '\0' || c
[7] == '\x01');
250 // Only checks for primary or dependent, but not that it is a C++ exception at
253 __is_dependent_exception(_Unwind_Exception_Class c
)
255 return c
[7] == '\x01';
259 __GXX_INIT_PRIMARY_EXCEPTION_CLASS(_Unwind_Exception_Class c
)
272 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(_Unwind_Exception_Class c
)
285 __is_gxx_forced_unwind_class(_Unwind_Exception_Class c
)
298 __GXX_INIT_FORCED_UNWIND_CLASS(_Unwind_Exception_Class c
)
311 __gxx_caught_object(_Unwind_Exception
* eo
)
313 return (void*)eo
->barrier_cache
.bitpattern
[0];
315 #else // !__ARM_EABI_UNWINDER__
316 // This is the primary exception class we report -- "GNUCC++\0".
317 const _Unwind_Exception_Class __gxx_primary_exception_class
318 = ((((((((_Unwind_Exception_Class
) 'G'
319 << 8 | (_Unwind_Exception_Class
) 'N')
320 << 8 | (_Unwind_Exception_Class
) 'U')
321 << 8 | (_Unwind_Exception_Class
) 'C')
322 << 8 | (_Unwind_Exception_Class
) 'C')
323 << 8 | (_Unwind_Exception_Class
) '+')
324 << 8 | (_Unwind_Exception_Class
) '+')
325 << 8 | (_Unwind_Exception_Class
) '\0');
327 // This is the dependent (from std::rethrow_exception) exception class we report
329 const _Unwind_Exception_Class __gxx_dependent_exception_class
330 = ((((((((_Unwind_Exception_Class
) 'G'
331 << 8 | (_Unwind_Exception_Class
) 'N')
332 << 8 | (_Unwind_Exception_Class
) 'U')
333 << 8 | (_Unwind_Exception_Class
) 'C')
334 << 8 | (_Unwind_Exception_Class
) 'C')
335 << 8 | (_Unwind_Exception_Class
) '+')
336 << 8 | (_Unwind_Exception_Class
) '+')
337 << 8 | (_Unwind_Exception_Class
) '\x01');
340 __is_gxx_exception_class(_Unwind_Exception_Class c
)
342 return c
== __gxx_primary_exception_class
343 || c
== __gxx_dependent_exception_class
;
346 // Only checks for primary or dependent, but not that it is a C++ exception at
349 __is_dependent_exception(_Unwind_Exception_Class c
)
354 #define __GXX_INIT_PRIMARY_EXCEPTION_CLASS(c) c = __gxx_primary_exception_class
355 #define __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(c) \
356 c = __gxx_dependent_exception_class
358 // GNU C++ personality routine, Version 0.
359 extern "C" _Unwind_Reason_Code __gxx_personality_v0
360 (int, _Unwind_Action
, _Unwind_Exception_Class
,
361 struct _Unwind_Exception
*, struct _Unwind_Context
*);
363 // GNU C++ sjlj personality routine, Version 0.
364 extern "C" _Unwind_Reason_Code __gxx_personality_sj0
365 (int, _Unwind_Action
, _Unwind_Exception_Class
,
366 struct _Unwind_Exception
*, struct _Unwind_Context
*);
369 __gxx_caught_object(_Unwind_Exception
* eo
)
371 // Bad as it looks, this actually works for dependent exceptions too.
372 __cxa_exception
* header
= __get_exception_header_from_ue (eo
);
373 return header
->adjustedPtr
;
375 #endif // !__ARM_EABI_UNWINDER__
378 __get_object_from_ue(_Unwind_Exception
* eo
) throw()
380 return __is_dependent_exception (eo
->exception_class
) ?
381 __get_dependent_exception_from_ue (eo
)->primaryException
:
386 __get_object_from_ambiguous_exception(__cxa_exception
*p_or_d
) throw()
388 return __get_object_from_ue (&p_or_d
->unwindHeader
);
392 } /* namespace __cxxabiv1 */
394 #pragma GCC visibility pop
396 #endif // _UNWIND_CXX_H