1 // -*- C++ -*- Exception handling and frame unwind runtime interface routines.
2 // Copyright (C) 2001-2014 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 // This is derived from the C++ ABI for IA-64. Where we diverge
26 // for cross-architecture compatibility are noted with "@@@".
29 #define _UNWIND_CXX_H 1
37 #include <bits/atomic_word.h>
40 #ifdef _GLIBCXX_HAVE_SYS_SDT_H
42 /* We only want to use stap probes starting with v3. Earlier versions
43 added too much startup cost. */
44 #if defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
45 #define PROBE2(name, arg1, arg2) STAP_PROBE2 (libstdcxx, name, arg1, arg2)
50 #define PROBE2(name, arg1, arg2)
53 #pragma GCC visibility push(default)
58 // A primary C++ exception object consists of a header, which is a wrapper
59 // around an unwind object header with additional C++ specific information,
60 // followed by the exception object itself.
62 struct __cxa_exception
64 // Manage the exception object itself.
65 std::type_info
*exceptionType
;
66 void (_GLIBCXX_CDTOR_CALLABI
*exceptionDestructor
)(void *);
68 // The C++ standard has entertaining rules wrt calling set_terminate
69 // and set_unexpected in the middle of the exception cleanup process.
70 std::unexpected_handler unexpectedHandler
;
71 std::terminate_handler terminateHandler
;
73 // The caught exception stack threads through here.
74 __cxa_exception
*nextException
;
76 // How many nested handlers have caught this exception. A negated
77 // value is a signal that this object has been rethrown.
80 #ifdef __ARM_EABI_UNWINDER__
81 // Stack of exceptions in cleanups.
82 __cxa_exception
* nextPropagatingException
;
84 // The nuber of active cleanup handlers for this exception.
87 // Cache parsed handler data from the personality routine Phase 1
88 // for Phase 2 and __cxa_call_unexpected.
89 int handlerSwitchValue
;
90 const unsigned char *actionRecord
;
91 const unsigned char *languageSpecificData
;
92 _Unwind_Ptr catchTemp
;
96 // The generic exception header. Must be last.
97 _Unwind_Exception unwindHeader
;
100 struct __cxa_refcounted_exception
102 // Manage this header.
103 _Atomic_word referenceCount
;
104 // __cxa_exception must be last, and no padding can be after it.
108 // A dependent C++ exception object consists of a wrapper around an unwind
109 // object header with additional C++ specific information, containing a pointer
110 // to a primary exception object.
112 struct __cxa_dependent_exception
114 // The primary exception this thing depends on.
115 void *primaryException
;
117 // The C++ standard has entertaining rules wrt calling set_terminate
118 // and set_unexpected in the middle of the exception cleanup process.
119 std::unexpected_handler unexpectedHandler
;
120 std::terminate_handler terminateHandler
;
122 // The caught exception stack threads through here.
123 __cxa_exception
*nextException
;
125 // How many nested handlers have caught this exception. A negated
126 // value is a signal that this object has been rethrown.
129 #ifdef __ARM_EABI_UNWINDER__
130 // Stack of exceptions in cleanups.
131 __cxa_exception
* nextPropagatingException
;
133 // The nuber of active cleanup handlers for this exception.
134 int propagationCount
;
136 // Cache parsed handler data from the personality routine Phase 1
137 // for Phase 2 and __cxa_call_unexpected.
138 int handlerSwitchValue
;
139 const unsigned char *actionRecord
;
140 const unsigned char *languageSpecificData
;
141 _Unwind_Ptr catchTemp
;
145 // The generic exception header. Must be last.
146 _Unwind_Exception unwindHeader
;
149 // Each thread in a C++ program has access to a __cxa_eh_globals object.
150 struct __cxa_eh_globals
152 __cxa_exception
*caughtExceptions
;
153 unsigned int uncaughtExceptions
;
154 #ifdef __ARM_EABI_UNWINDER__
155 __cxa_exception
* propagatingExceptions
;
159 // @@@ These are not directly specified by the IA-64 C++ ABI.
161 // Handles re-checking the exception specification if unexpectedHandler
162 // throws, and if bad_exception needs to be thrown. Called from the
164 extern "C" void __cxa_call_unexpected (void *) __attribute__((__noreturn__
));
165 extern "C" void __cxa_call_terminate (_Unwind_Exception
*) throw ()
166 __attribute__((__noreturn__
));
168 #ifdef __ARM_EABI_UNWINDER__
169 // Arm EABI specified routines.
173 ctm_succeeded_with_ptr_to_base
= 2
174 } __cxa_type_match_result
;
175 extern "C" __cxa_type_match_result
__cxa_type_match(_Unwind_Exception
*,
176 const std::type_info
*,
178 extern "C" bool __cxa_begin_cleanup (_Unwind_Exception
*);
179 extern "C" void __cxa_end_cleanup (void);
182 // Handles cleanup from transactional memory restart.
183 extern "C" void __cxa_tm_cleanup (void *, void *, unsigned int) throw();
185 // Invokes given handler, dying appropriately if the user handler was
186 // so inconsiderate as to return.
187 extern void __terminate(std::terminate_handler
) throw ()
188 __attribute__((__noreturn__
));
189 extern void __unexpected(std::unexpected_handler
)
190 __attribute__((__noreturn__
));
192 // The current installed user handlers.
193 extern std::terminate_handler __terminate_handler
;
194 extern std::unexpected_handler __unexpected_handler
;
196 // These are explicitly GNU C++ specific.
198 // Acquire the C++ exception header from the C++ object.
199 static inline __cxa_exception
*
200 __get_exception_header_from_obj (void *ptr
)
202 return reinterpret_cast<__cxa_exception
*>(ptr
) - 1;
205 // Acquire the C++ exception header from the generic exception header.
206 static inline __cxa_exception
*
207 __get_exception_header_from_ue (_Unwind_Exception
*exc
)
209 return reinterpret_cast<__cxa_exception
*>(exc
+ 1) - 1;
212 // Acquire the C++ refcounted exception header from the C++ object.
213 static inline __cxa_refcounted_exception
*
214 __get_refcounted_exception_header_from_obj (void *ptr
)
216 return reinterpret_cast<__cxa_refcounted_exception
*>(ptr
) - 1;
219 // Acquire the C++ refcounted exception header from the generic exception
221 static inline __cxa_refcounted_exception
*
222 __get_refcounted_exception_header_from_ue (_Unwind_Exception
*exc
)
224 return reinterpret_cast<__cxa_refcounted_exception
*>(exc
+ 1) - 1;
227 static inline __cxa_dependent_exception
*
228 __get_dependent_exception_from_ue (_Unwind_Exception
*exc
)
230 return reinterpret_cast<__cxa_dependent_exception
*>(exc
+ 1) - 1;
233 #ifdef __ARM_EABI_UNWINDER__
235 __is_gxx_exception_class(_Unwind_Exception_Class c
)
237 // TODO: Take advantage of the fact that c will always be word aligned.
245 && (c
[7] == '\0' || c
[7] == '\x01');
248 // Only checks for primary or dependent, but not that it is a C++ exception at
251 __is_dependent_exception(_Unwind_Exception_Class c
)
253 return c
[7] == '\x01';
257 __GXX_INIT_PRIMARY_EXCEPTION_CLASS(_Unwind_Exception_Class c
)
270 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(_Unwind_Exception_Class c
)
283 __is_gxx_forced_unwind_class(_Unwind_Exception_Class c
)
296 __GXX_INIT_FORCED_UNWIND_CLASS(_Unwind_Exception_Class c
)
309 __gxx_caught_object(_Unwind_Exception
* eo
)
311 return (void*)eo
->barrier_cache
.bitpattern
[0];
313 #else // !__ARM_EABI_UNWINDER__
314 // This is the primary exception class we report -- "GNUCC++\0".
315 const _Unwind_Exception_Class __gxx_primary_exception_class
316 = ((((((((_Unwind_Exception_Class
) 'G'
317 << 8 | (_Unwind_Exception_Class
) 'N')
318 << 8 | (_Unwind_Exception_Class
) 'U')
319 << 8 | (_Unwind_Exception_Class
) 'C')
320 << 8 | (_Unwind_Exception_Class
) 'C')
321 << 8 | (_Unwind_Exception_Class
) '+')
322 << 8 | (_Unwind_Exception_Class
) '+')
323 << 8 | (_Unwind_Exception_Class
) '\0');
325 // This is the dependent (from std::rethrow_exception) exception class we report
327 const _Unwind_Exception_Class __gxx_dependent_exception_class
328 = ((((((((_Unwind_Exception_Class
) 'G'
329 << 8 | (_Unwind_Exception_Class
) 'N')
330 << 8 | (_Unwind_Exception_Class
) 'U')
331 << 8 | (_Unwind_Exception_Class
) 'C')
332 << 8 | (_Unwind_Exception_Class
) 'C')
333 << 8 | (_Unwind_Exception_Class
) '+')
334 << 8 | (_Unwind_Exception_Class
) '+')
335 << 8 | (_Unwind_Exception_Class
) '\x01');
338 __is_gxx_exception_class(_Unwind_Exception_Class c
)
340 return c
== __gxx_primary_exception_class
341 || c
== __gxx_dependent_exception_class
;
344 // Only checks for primary or dependent, but not that it is a C++ exception at
347 __is_dependent_exception(_Unwind_Exception_Class c
)
352 #define __GXX_INIT_PRIMARY_EXCEPTION_CLASS(c) c = __gxx_primary_exception_class
353 #define __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(c) \
354 c = __gxx_dependent_exception_class
356 // GNU C++ personality routine, Version 0.
357 extern "C" _Unwind_Reason_Code __gxx_personality_v0
358 (int, _Unwind_Action
, _Unwind_Exception_Class
,
359 struct _Unwind_Exception
*, struct _Unwind_Context
*);
361 // GNU C++ sjlj personality routine, Version 0.
362 extern "C" _Unwind_Reason_Code __gxx_personality_sj0
363 (int, _Unwind_Action
, _Unwind_Exception_Class
,
364 struct _Unwind_Exception
*, struct _Unwind_Context
*);
367 __gxx_caught_object(_Unwind_Exception
* eo
)
369 // Bad as it looks, this actually works for dependent exceptions too.
370 __cxa_exception
* header
= __get_exception_header_from_ue (eo
);
371 return header
->adjustedPtr
;
373 #endif // !__ARM_EABI_UNWINDER__
376 __get_object_from_ue(_Unwind_Exception
* eo
) throw()
378 return __is_dependent_exception (eo
->exception_class
) ?
379 __get_dependent_exception_from_ue (eo
)->primaryException
:
384 __get_object_from_ambiguous_exception(__cxa_exception
*p_or_d
) throw()
386 return __get_object_from_ue (&p_or_d
->unwindHeader
);
390 } /* namespace __cxxabiv1 */
392 #pragma GCC visibility pop
394 #endif // _UNWIND_CXX_H