1 // -*- C++ -*- Exception handling and frame unwind runtime interface routines.
2 // Copyright (C) 2001-2017 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 number 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 // Unused member to get similar layout to __cxa_exception, otherwise the
118 // alignment requirements of _Unwind_Exception would require padding bytes
119 // before the unwindHeader member.
120 void (_GLIBCXX_CDTOR_CALLABI
*__padding
)(void *);
122 // The C++ standard has entertaining rules wrt calling set_terminate
123 // and set_unexpected in the middle of the exception cleanup process.
124 std::unexpected_handler unexpectedHandler
;
125 std::terminate_handler terminateHandler
;
127 // The caught exception stack threads through here.
128 __cxa_exception
*nextException
;
130 // How many nested handlers have caught this exception. A negated
131 // value is a signal that this object has been rethrown.
134 #ifdef __ARM_EABI_UNWINDER__
135 // Stack of exceptions in cleanups.
136 __cxa_exception
* nextPropagatingException
;
138 // The number of active cleanup handlers for this exception.
139 int propagationCount
;
141 // Cache parsed handler data from the personality routine Phase 1
142 // for Phase 2 and __cxa_call_unexpected.
143 int handlerSwitchValue
;
144 const unsigned char *actionRecord
;
145 const unsigned char *languageSpecificData
;
146 _Unwind_Ptr catchTemp
;
150 // The generic exception header. Must be last.
151 _Unwind_Exception unwindHeader
;
154 // Each thread in a C++ program has access to a __cxa_eh_globals object.
155 struct __cxa_eh_globals
157 __cxa_exception
*caughtExceptions
;
158 unsigned int uncaughtExceptions
;
159 #ifdef __ARM_EABI_UNWINDER__
160 __cxa_exception
* propagatingExceptions
;
164 // @@@ These are not directly specified by the IA-64 C++ ABI.
166 // Handles re-checking the exception specification if unexpectedHandler
167 // throws, and if bad_exception needs to be thrown. Called from the
169 extern "C" void __cxa_call_unexpected (void *) __attribute__((__noreturn__
));
170 extern "C" void __cxa_call_terminate (_Unwind_Exception
*) throw ()
171 __attribute__((__noreturn__
));
173 #ifdef __ARM_EABI_UNWINDER__
174 // Arm EABI specified routines.
178 ctm_succeeded_with_ptr_to_base
= 2
179 } __cxa_type_match_result
;
180 extern "C" __cxa_type_match_result
__cxa_type_match(_Unwind_Exception
*,
181 const std::type_info
*,
183 extern "C" bool __cxa_begin_cleanup (_Unwind_Exception
*);
184 extern "C" void __cxa_end_cleanup (void);
187 // Handles cleanup from transactional memory restart.
188 extern "C" void __cxa_tm_cleanup (void *, void *, unsigned int) throw();
190 // Invokes given handler, dying appropriately if the user handler was
191 // so inconsiderate as to return.
192 extern void __terminate(std::terminate_handler
) throw ()
193 __attribute__((__noreturn__
));
194 extern void __unexpected(std::unexpected_handler
)
195 __attribute__((__noreturn__
));
197 // The current installed user handlers.
198 extern std::terminate_handler __terminate_handler
;
199 extern std::unexpected_handler __unexpected_handler
;
201 // These are explicitly GNU C++ specific.
203 // Acquire the C++ exception header from the C++ object.
204 static inline __cxa_exception
*
205 __get_exception_header_from_obj (void *ptr
)
207 return reinterpret_cast<__cxa_exception
*>(ptr
) - 1;
210 // Acquire the C++ exception header from the generic exception header.
211 static inline __cxa_exception
*
212 __get_exception_header_from_ue (_Unwind_Exception
*exc
)
214 return reinterpret_cast<__cxa_exception
*>(exc
+ 1) - 1;
217 // Acquire the C++ refcounted exception header from the C++ object.
218 static inline __cxa_refcounted_exception
*
219 __get_refcounted_exception_header_from_obj (void *ptr
)
221 return reinterpret_cast<__cxa_refcounted_exception
*>(ptr
) - 1;
224 // Acquire the C++ refcounted exception header from the generic exception
226 static inline __cxa_refcounted_exception
*
227 __get_refcounted_exception_header_from_ue (_Unwind_Exception
*exc
)
229 return reinterpret_cast<__cxa_refcounted_exception
*>(exc
+ 1) - 1;
232 static inline __cxa_dependent_exception
*
233 __get_dependent_exception_from_ue (_Unwind_Exception
*exc
)
235 return reinterpret_cast<__cxa_dependent_exception
*>(exc
+ 1) - 1;
238 #ifdef __ARM_EABI_UNWINDER__
240 __is_gxx_exception_class(_Unwind_Exception_Class c
)
242 // TODO: Take advantage of the fact that c will always be word aligned.
250 && (c
[7] == '\0' || c
[7] == '\x01');
253 // Only checks for primary or dependent, but not that it is a C++ exception at
256 __is_dependent_exception(_Unwind_Exception_Class c
)
258 return c
[7] == '\x01';
262 __GXX_INIT_PRIMARY_EXCEPTION_CLASS(_Unwind_Exception_Class c
)
275 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(_Unwind_Exception_Class c
)
288 __is_gxx_forced_unwind_class(_Unwind_Exception_Class c
)
301 __GXX_INIT_FORCED_UNWIND_CLASS(_Unwind_Exception_Class c
)
314 __gxx_caught_object(_Unwind_Exception
* eo
)
316 return (void*)eo
->barrier_cache
.bitpattern
[0];
318 #else // !__ARM_EABI_UNWINDER__
319 // This is the primary exception class we report -- "GNUCC++\0".
320 const _Unwind_Exception_Class __gxx_primary_exception_class
321 = ((((((((_Unwind_Exception_Class
) 'G'
322 << 8 | (_Unwind_Exception_Class
) 'N')
323 << 8 | (_Unwind_Exception_Class
) 'U')
324 << 8 | (_Unwind_Exception_Class
) 'C')
325 << 8 | (_Unwind_Exception_Class
) 'C')
326 << 8 | (_Unwind_Exception_Class
) '+')
327 << 8 | (_Unwind_Exception_Class
) '+')
328 << 8 | (_Unwind_Exception_Class
) '\0');
330 // This is the dependent (from std::rethrow_exception) exception class we report
332 const _Unwind_Exception_Class __gxx_dependent_exception_class
333 = ((((((((_Unwind_Exception_Class
) 'G'
334 << 8 | (_Unwind_Exception_Class
) 'N')
335 << 8 | (_Unwind_Exception_Class
) 'U')
336 << 8 | (_Unwind_Exception_Class
) 'C')
337 << 8 | (_Unwind_Exception_Class
) 'C')
338 << 8 | (_Unwind_Exception_Class
) '+')
339 << 8 | (_Unwind_Exception_Class
) '+')
340 << 8 | (_Unwind_Exception_Class
) '\x01');
343 __is_gxx_exception_class(_Unwind_Exception_Class c
)
345 return c
== __gxx_primary_exception_class
346 || c
== __gxx_dependent_exception_class
;
349 // Only checks for primary or dependent, but not that it is a C++ exception at
352 __is_dependent_exception(_Unwind_Exception_Class c
)
357 #define __GXX_INIT_PRIMARY_EXCEPTION_CLASS(c) c = __gxx_primary_exception_class
358 #define __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(c) \
359 c = __gxx_dependent_exception_class
361 // GNU C++ personality routine, Version 0.
362 extern "C" _Unwind_Reason_Code __gxx_personality_v0
363 (int, _Unwind_Action
, _Unwind_Exception_Class
,
364 struct _Unwind_Exception
*, struct _Unwind_Context
*);
366 // GNU C++ sjlj personality routine, Version 0.
367 extern "C" _Unwind_Reason_Code __gxx_personality_sj0
368 (int, _Unwind_Action
, _Unwind_Exception_Class
,
369 struct _Unwind_Exception
*, struct _Unwind_Context
*);
372 __gxx_caught_object(_Unwind_Exception
* eo
)
374 // Bad as it looks, this actually works for dependent exceptions too.
375 __cxa_exception
* header
= __get_exception_header_from_ue (eo
);
376 return header
->adjustedPtr
;
378 #endif // !__ARM_EABI_UNWINDER__
381 __get_object_from_ue(_Unwind_Exception
* eo
) throw()
383 return __is_dependent_exception (eo
->exception_class
) ?
384 __get_dependent_exception_from_ue (eo
)->primaryException
:
389 __get_object_from_ambiguous_exception(__cxa_exception
*p_or_d
) throw()
391 return __get_object_from_ue (&p_or_d
->unwindHeader
);
395 } /* namespace __cxxabiv1 */
397 #pragma GCC visibility pop
399 #endif // _UNWIND_CXX_H