1 // -*- C++ -*- Exception handling and frame unwind runtime interface routines.
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 // This is derived from the C++ ABI for IA-64. Where we diverge
27 // for cross-architecture compatibility are noted with "@@@".
30 #define _UNWIND_CXX_H 1
38 #include <bits/atomic_word.h>
40 #pragma GCC visibility push(default)
45 // A primary C++ exception object consists of a header, which is a wrapper
46 // around an unwind object header with additional C++ specific information,
47 // followed by the exception object itself.
49 struct __cxa_exception
51 // Manage the exception object itself.
52 std::type_info
*exceptionType
;
53 void (*exceptionDestructor
)(void *);
55 // The C++ standard has entertaining rules wrt calling set_terminate
56 // and set_unexpected in the middle of the exception cleanup process.
57 std::unexpected_handler unexpectedHandler
;
58 std::terminate_handler terminateHandler
;
60 // The caught exception stack threads through here.
61 __cxa_exception
*nextException
;
63 // How many nested handlers have caught this exception. A negated
64 // value is a signal that this object has been rethrown.
67 #ifdef __ARM_EABI_UNWINDER__
68 // Stack of exceptions in cleanups.
69 __cxa_exception
* nextPropagatingException
;
71 // The nuber of active cleanup handlers for this exception.
74 // Cache parsed handler data from the personality routine Phase 1
75 // for Phase 2 and __cxa_call_unexpected.
76 int handlerSwitchValue
;
77 const unsigned char *actionRecord
;
78 const unsigned char *languageSpecificData
;
79 _Unwind_Ptr catchTemp
;
83 // The generic exception header. Must be last.
84 _Unwind_Exception unwindHeader
;
87 struct __cxa_refcounted_exception
89 // Manage this header.
90 _Atomic_word referenceCount
;
91 // __cxa_exception must be last, and no padding can be after it.
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() __attribute__ ((__const__
));
152 extern "C" __cxa_eh_globals
*__cxa_get_globals_fast () throw() __attribute__ ((__const__
));
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() __attribute__ ((__pure__
));
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 () __attribute__((__noreturn__
));
182 extern "C" void __cxa_bad_typeid () __attribute__((__noreturn__
));
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 (_Unwind_Exception
*) throw () __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
) throw () __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 // Acquire the C++ refcounted exception header from the C++ object.
231 static inline __cxa_refcounted_exception
*
232 __get_refcounted_exception_header_from_obj (void *ptr
)
234 return reinterpret_cast<__cxa_refcounted_exception
*>(ptr
) - 1;
237 // Acquire the C++ refcounted exception header from the generic exception
239 static inline __cxa_refcounted_exception
*
240 __get_refcounted_exception_header_from_ue (_Unwind_Exception
*exc
)
242 return reinterpret_cast<__cxa_refcounted_exception
*>(exc
+ 1) - 1;
245 static inline __cxa_dependent_exception
*
246 __get_dependent_exception_from_ue (_Unwind_Exception
*exc
)
248 return reinterpret_cast<__cxa_dependent_exception
*>(exc
+ 1) - 1;
251 #ifdef __ARM_EABI_UNWINDER__
253 __is_gxx_exception_class(_Unwind_Exception_Class c
)
255 // TODO: Take advantage of the fact that c will always be word aligned.
263 && (c
[7] == '\0' || c
[7] == '\x01');
266 // Only checks for primary or dependent, but not that it is a C++ exception at
269 __is_dependent_exception(_Unwind_Exception_Class c
)
271 return c
[7] == '\x01';
275 __GXX_INIT_PRIMARY_EXCEPTION_CLASS(_Unwind_Exception_Class c
)
288 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(_Unwind_Exception_Class c
)
301 __is_gxx_forced_unwind_class(_Unwind_Exception_Class c
)
314 __GXX_INIT_FORCED_UNWIND_CLASS(_Unwind_Exception_Class c
)
327 __gxx_caught_object(_Unwind_Exception
* eo
)
329 return (void*)eo
->barrier_cache
.bitpattern
[0];
331 #else // !__ARM_EABI_UNWINDER__
332 // This is the primary exception class we report -- "GNUCC++\0".
333 const _Unwind_Exception_Class __gxx_primary_exception_class
334 = ((((((((_Unwind_Exception_Class
) 'G'
335 << 8 | (_Unwind_Exception_Class
) 'N')
336 << 8 | (_Unwind_Exception_Class
) 'U')
337 << 8 | (_Unwind_Exception_Class
) 'C')
338 << 8 | (_Unwind_Exception_Class
) 'C')
339 << 8 | (_Unwind_Exception_Class
) '+')
340 << 8 | (_Unwind_Exception_Class
) '+')
341 << 8 | (_Unwind_Exception_Class
) '\0');
343 // This is the dependent (from std::rethrow_exception) exception class we report
345 const _Unwind_Exception_Class __gxx_dependent_exception_class
346 = ((((((((_Unwind_Exception_Class
) 'G'
347 << 8 | (_Unwind_Exception_Class
) 'N')
348 << 8 | (_Unwind_Exception_Class
) 'U')
349 << 8 | (_Unwind_Exception_Class
) 'C')
350 << 8 | (_Unwind_Exception_Class
) 'C')
351 << 8 | (_Unwind_Exception_Class
) '+')
352 << 8 | (_Unwind_Exception_Class
) '+')
353 << 8 | (_Unwind_Exception_Class
) '\x01');
356 __is_gxx_exception_class(_Unwind_Exception_Class c
)
358 return c
== __gxx_primary_exception_class
359 || c
== __gxx_dependent_exception_class
;
362 // Only checks for primary or dependent, but not that it is a C++ exception at
365 __is_dependent_exception(_Unwind_Exception_Class c
)
370 #define __GXX_INIT_PRIMARY_EXCEPTION_CLASS(c) c = __gxx_primary_exception_class
371 #define __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(c) \
372 c = __gxx_dependent_exception_class
374 // GNU C++ personality routine, Version 0.
375 extern "C" _Unwind_Reason_Code __gxx_personality_v0
376 (int, _Unwind_Action
, _Unwind_Exception_Class
,
377 struct _Unwind_Exception
*, struct _Unwind_Context
*);
379 // GNU C++ sjlj personality routine, Version 0.
380 extern "C" _Unwind_Reason_Code __gxx_personality_sj0
381 (int, _Unwind_Action
, _Unwind_Exception_Class
,
382 struct _Unwind_Exception
*, struct _Unwind_Context
*);
385 __gxx_caught_object(_Unwind_Exception
* eo
)
387 // Bad as it looks, this actually works for dependent exceptions too.
388 __cxa_exception
* header
= __get_exception_header_from_ue (eo
);
389 return header
->adjustedPtr
;
391 #endif // !__ARM_EABI_UNWINDER__
394 __get_object_from_ue(_Unwind_Exception
* eo
) throw()
396 return __is_dependent_exception (eo
->exception_class
) ?
397 __get_dependent_exception_from_ue (eo
)->primaryException
:
402 __get_object_from_ambiguous_exception(__cxa_exception
*p_or_d
) throw()
404 return __get_object_from_ue (&p_or_d
->unwindHeader
);
408 } /* namespace __cxxabiv1 */
410 #pragma GCC visibility pop
412 #endif // _UNWIND_CXX_H