9697498026dddb2d569482a0647b67d55fb47474
[gcc.git] / libstdc++-v3 / libsupc++ / unwind-cxx.h
1 // -*- C++ -*- Exception handling and frame unwind runtime interface routines.
2 // Copyright (C) 2001, 2008 Free Software Foundation, Inc.
3 //
4 // This file is part of GCC.
5 //
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)
9 // any later version.
10 //
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.
15 //
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.
20
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.
29
30 // This is derived from the C++ ABI for IA-64. Where we diverge
31 // for cross-architecture compatibility are noted with "@@@".
32
33 #ifndef _UNWIND_CXX_H
34 #define _UNWIND_CXX_H 1
35
36 // Level 2: C++ ABI
37
38 #include <typeinfo>
39 #include <exception>
40 #include <cstddef>
41 #include "unwind.h"
42 #include <ext/atomicity.h>
43
44 #pragma GCC visibility push(default)
45
46 namespace __cxxabiv1
47 {
48
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.
52
53 struct __cxa_exception
54 {
55 // Manage this header.
56 _Atomic_word referenceCount;
57
58 // Manage the exception object itself.
59 std::type_info *exceptionType;
60 void (*exceptionDestructor)(void *);
61
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;
66
67 // The caught exception stack threads through here.
68 __cxa_exception *nextException;
69
70 // How many nested handlers have caught this exception. A negated
71 // value is a signal that this object has been rethrown.
72 int handlerCount;
73
74 #ifdef __ARM_EABI_UNWINDER__
75 // Stack of exceptions in cleanups.
76 __cxa_exception* nextPropagatingException;
77
78 // The nuber of active cleanup handlers for this exception.
79 int propagationCount;
80 #else
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;
87 void *adjustedPtr;
88 #endif
89
90 // The generic exception header. Must be last.
91 _Unwind_Exception unwindHeader;
92 };
93
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.
97
98 struct __cxa_dependent_exception
99 {
100 // The primary exception this thing depends on.
101 void *primaryException;
102
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;
107
108 // The caught exception stack threads through here.
109 __cxa_exception *nextException;
110
111 // How many nested handlers have caught this exception. A negated
112 // value is a signal that this object has been rethrown.
113 int handlerCount;
114
115 #ifdef __ARM_EABI_UNWINDER__
116 // Stack of exceptions in cleanups.
117 __cxa_exception* nextPropagatingException;
118
119 // The nuber of active cleanup handlers for this exception.
120 int propagationCount;
121 #else
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;
128 void *adjustedPtr;
129 #endif
130
131 // The generic exception header. Must be last.
132 _Unwind_Exception unwindHeader;
133 };
134
135 // Each thread in a C++ program has access to a __cxa_eh_globals object.
136 struct __cxa_eh_globals
137 {
138 __cxa_exception *caughtExceptions;
139 unsigned int uncaughtExceptions;
140 #ifdef __ARM_EABI_UNWINDER__
141 __cxa_exception* propagatingExceptions;
142 #endif
143 };
144
145
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();
152
153 // Allocate memory for the primary exception plus the thrown object.
154 extern "C" void *__cxa_allocate_exception(std::size_t thrown_size) throw();
155
156 // Free the space allocated for the primary exception.
157 extern "C" void __cxa_free_exception(void *thrown_exception) throw();
158
159 // Allocate memory for a dependent exception.
160 extern "C" __cxa_dependent_exception*
161 __cxa_allocate_dependent_exception() throw();
162
163 // Free the space allocated for the dependent exception.
164 extern "C" void
165 __cxa_free_dependent_exception(__cxa_dependent_exception *ex) throw();
166
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));
172
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));
178
179 // These facilitate code generation for recurring situations.
180 extern "C" void __cxa_bad_cast ();
181 extern "C" void __cxa_bad_typeid ();
182
183 // @@@ These are not directly specified by the IA-64 C++ ABI.
184
185 // Handles re-checking the exception specification if unexpectedHandler
186 // throws, and if bad_exception needs to be thrown. Called from the
187 // compiler.
188 extern "C" void __cxa_call_unexpected (void *) __attribute__((noreturn));
189 extern "C" void __cxa_call_terminate (void*) __attribute__((noreturn));
190
191 #ifdef __ARM_EABI_UNWINDER__
192 // Arm EABI specified routines.
193 typedef enum {
194 ctm_failed = 0,
195 ctm_succeeded = 1,
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*,
199 bool, void**);
200 extern "C" void __cxa_begin_cleanup (_Unwind_Exception*);
201 extern "C" void __cxa_end_cleanup (void);
202 #endif
203
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));
208
209 // The current installed user handlers.
210 extern std::terminate_handler __terminate_handler;
211 extern std::unexpected_handler __unexpected_handler;
212
213 // These are explicitly GNU C++ specific.
214
215 // Acquire the C++ exception header from the C++ object.
216 static inline __cxa_exception *
217 __get_exception_header_from_obj (void *ptr)
218 {
219 return reinterpret_cast<__cxa_exception *>(ptr) - 1;
220 }
221
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)
225 {
226 return reinterpret_cast<__cxa_exception *>(exc + 1) - 1;
227 }
228
229 static inline __cxa_dependent_exception *
230 __get_dependent_exception_from_ue (_Unwind_Exception *exc)
231 {
232 return reinterpret_cast<__cxa_dependent_exception *>(exc + 1) - 1;
233 }
234
235 #ifdef __ARM_EABI_UNWINDER__
236 static inline bool
237 __is_gxx_exception_class(_Unwind_Exception_Class c)
238 {
239 // TODO: Take advantage of the fact that c will always be word aligned.
240 return c[0] == 'G'
241 && c[1] == 'N'
242 && c[2] == 'U'
243 && c[3] == 'C'
244 && c[4] == 'C'
245 && c[5] == '+'
246 && c[6] == '+'
247 && (c[7] == '\0' || c[7] == '\x01');
248 }
249
250 // Only checks for primary or dependent, but not that it is a C++ exception at
251 // all.
252 static inline bool
253 __is_dependent_exception(_Unwind_Exception_Class c)
254 {
255 return c[7] == '\x01';
256 }
257
258 static inline void
259 __GXX_INIT_PRIMARY_EXCEPTION_CLASS(_Unwind_Exception_Class c)
260 {
261 c[0] = 'G';
262 c[1] = 'N';
263 c[2] = 'U';
264 c[3] = 'C';
265 c[4] = 'C';
266 c[5] = '+';
267 c[6] = '+';
268 c[7] = '\0';
269 }
270
271 static inline void
272 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(_Unwind_Exception_Class c)
273 {
274 c[0] = 'G';
275 c[1] = 'N';
276 c[2] = 'U';
277 c[3] = 'C';
278 c[4] = 'C';
279 c[5] = '+';
280 c[6] = '+';
281 c[7] = '\x01';
282 }
283
284 static inline bool
285 __is_gxx_forced_unwind_class(_Unwind_Exception_Class c)
286 {
287 return c[0] == 'G'
288 && c[1] == 'N'
289 && c[2] == 'U'
290 && c[3] == 'C'
291 && c[4] == 'F'
292 && c[5] == 'O'
293 && c[6] == 'R'
294 && c[7] == '\0';
295 }
296
297 static inline void
298 __GXX_INIT_FORCED_UNWIND_CLASS(_Unwind_Exception_Class c)
299 {
300 c[0] = 'G';
301 c[1] = 'N';
302 c[2] = 'U';
303 c[3] = 'C';
304 c[4] = 'F';
305 c[5] = 'O';
306 c[6] = 'R';
307 c[7] = '\0';
308 }
309
310 static inline void*
311 __gxx_caught_object(_Unwind_Exception* eo)
312 {
313 return (void*)eo->barrier_cache.bitpattern[0];
314 }
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');
326
327 // This is the dependent (from std::rethrow_exception) exception class we report
328 // "GNUCC++\x01"
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');
338
339 static inline bool
340 __is_gxx_exception_class(_Unwind_Exception_Class c)
341 {
342 return c == __gxx_primary_exception_class
343 || c == __gxx_dependent_exception_class;
344 }
345
346 // Only checks for primary or dependent, but not that it is a C++ exception at
347 // all.
348 static inline bool
349 __is_dependent_exception(_Unwind_Exception_Class c)
350 {
351 return (c & 1);
352 }
353
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
357
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 *);
362
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 *);
367
368 static inline void*
369 __gxx_caught_object(_Unwind_Exception* eo)
370 {
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;
374 }
375 #endif // !__ARM_EABI_UNWINDER__
376
377 static inline void*
378 __get_object_from_ue(_Unwind_Exception* eo) throw()
379 {
380 return __is_dependent_exception (eo->exception_class) ?
381 __get_dependent_exception_from_ue (eo)->primaryException :
382 eo + 1;
383 }
384
385 static inline void *
386 __get_object_from_ambiguous_exception(__cxa_exception *p_or_d) throw()
387 {
388 return __get_object_from_ue (&p_or_d->unwindHeader);
389 }
390
391
392 } /* namespace __cxxabiv1 */
393
394 #pragma GCC visibility pop
395
396 #endif // _UNWIND_CXX_H