OSDN Git Service

e62ea7c38be7c7593c4cecdb8b77caea5d78571d
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / libsupc++ / unwind-cxx.h
1 // -*- C++ -*- Exception handling and frame unwind runtime interface routines.
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 // Free Software Foundation, Inc.
4 //
5 // This file is part of GCC.
6 //
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)
10 // any later version.
11 //
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.
16 //
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.
20
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/>.
25
26 // This is derived from the C++ ABI for IA-64.  Where we diverge
27 // for cross-architecture compatibility are noted with "@@@".
28
29 #ifndef _UNWIND_CXX_H
30 #define _UNWIND_CXX_H 1
31
32 // Level 2: C++ ABI
33
34 #include <typeinfo>
35 #include <exception>
36 #include <cstddef>
37 #include "unwind.h"
38 #include <bits/atomic_word.h>
39
40 #pragma GCC visibility push(default)
41
42 namespace __cxxabiv1
43 {
44
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.
48
49 struct __cxa_exception
50 {
51   // Manage the exception object itself.
52   std::type_info *exceptionType;
53   void (*exceptionDestructor)(void *); 
54
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;
59
60   // The caught exception stack threads through here.
61   __cxa_exception *nextException;
62
63   // How many nested handlers have caught this exception.  A negated
64   // value is a signal that this object has been rethrown.
65   int handlerCount;
66
67 #ifdef __ARM_EABI_UNWINDER__
68   // Stack of exceptions in cleanups.
69   __cxa_exception* nextPropagatingException;
70
71   // The nuber of active cleanup handlers for this exception.
72   int propagationCount;
73 #else
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;
80   void *adjustedPtr;
81 #endif
82
83   // The generic exception header.  Must be last.
84   _Unwind_Exception unwindHeader;
85 };
86
87 struct __cxa_refcounted_exception
88 {
89   // Manage this header.
90   _Atomic_word referenceCount;
91   // __cxa_exception must be last, and no padding can be after it.
92   __cxa_exception exc;
93 };
94
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.
98
99 struct __cxa_dependent_exception
100 {
101   // The primary exception this thing depends on.
102   void *primaryException;
103
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;
108
109   // The caught exception stack threads through here.
110   __cxa_exception *nextException;
111
112   // How many nested handlers have caught this exception.  A negated
113   // value is a signal that this object has been rethrown.
114   int handlerCount;
115
116 #ifdef __ARM_EABI_UNWINDER__
117   // Stack of exceptions in cleanups.
118   __cxa_exception* nextPropagatingException;
119
120   // The nuber of active cleanup handlers for this exception.
121   int propagationCount;
122 #else
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;
129   void *adjustedPtr;
130 #endif
131
132   // The generic exception header.  Must be last.
133   _Unwind_Exception unwindHeader;
134 };
135
136 // Each thread in a C++ program has access to a __cxa_eh_globals object.
137 struct __cxa_eh_globals
138 {
139   __cxa_exception *caughtExceptions;
140   unsigned int uncaughtExceptions;
141 #ifdef __ARM_EABI_UNWINDER__
142   __cxa_exception* propagatingExceptions;
143 #endif
144 };
145
146
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() 
152   __attribute__ ((__const__));
153 extern "C" __cxa_eh_globals *__cxa_get_globals_fast () throw() 
154   __attribute__ ((__const__));
155
156 // Allocate memory for the primary exception plus the thrown object.
157 extern "C" void *__cxa_allocate_exception(std::size_t thrown_size) throw();
158
159 // Free the space allocated for the primary exception.
160 extern "C" void __cxa_free_exception(void *thrown_exception) throw();
161
162 // Allocate memory for a dependent exception.
163 extern "C" __cxa_dependent_exception*
164 __cxa_allocate_dependent_exception() throw();
165
166 // Free the space allocated for the dependent exception.
167 extern "C" void
168 __cxa_free_dependent_exception(__cxa_dependent_exception *ex) throw();
169
170 // Throw the exception.
171 extern "C" void __cxa_throw (void *thrown_exception,
172                              std::type_info *tinfo,
173                              void (*dest) (void *))
174   __attribute__((__noreturn__));
175
176 // Used to implement exception handlers.
177 extern "C" void *__cxa_get_exception_ptr (void *) throw()
178   __attribute__ ((__pure__));
179 extern "C" void *__cxa_begin_catch (void *) throw();
180 extern "C" void __cxa_end_catch ();
181 extern "C" void __cxa_rethrow () __attribute__((__noreturn__));
182
183 // These facilitate code generation for recurring situations.
184 extern "C" void __cxa_bad_cast () __attribute__((__noreturn__));
185 extern "C" void __cxa_bad_typeid () __attribute__((__noreturn__));
186
187 // @@@ These are not directly specified by the IA-64 C++ ABI.
188
189 // Handles re-checking the exception specification if unexpectedHandler
190 // throws, and if bad_exception needs to be thrown.  Called from the
191 // compiler.
192 extern "C" void __cxa_call_unexpected (void *) __attribute__((__noreturn__));
193 extern "C" void __cxa_call_terminate (_Unwind_Exception*) throw ()
194   __attribute__((__noreturn__));
195
196 #ifdef __ARM_EABI_UNWINDER__
197 // Arm EABI specified routines.
198 typedef enum {
199   ctm_failed = 0,
200   ctm_succeeded = 1,
201   ctm_succeeded_with_ptr_to_base = 2
202 } __cxa_type_match_result;
203 extern "C" __cxa_type_match_result __cxa_type_match(_Unwind_Exception*,
204                                                     const std::type_info*,
205                                                     bool, void**);
206 extern "C" bool __cxa_begin_cleanup (_Unwind_Exception*);
207 extern "C" void __cxa_end_cleanup (void);
208 #endif
209
210 // Invokes given handler, dying appropriately if the user handler was
211 // so inconsiderate as to return.
212 extern void __terminate(std::terminate_handler) throw () 
213   __attribute__((__noreturn__));
214 extern void __unexpected(std::unexpected_handler)
215   __attribute__((__noreturn__));
216
217 // The current installed user handlers.
218 extern std::terminate_handler __terminate_handler;
219 extern std::unexpected_handler __unexpected_handler;
220
221 // These are explicitly GNU C++ specific.
222
223 // Acquire the C++ exception header from the C++ object.
224 static inline __cxa_exception *
225 __get_exception_header_from_obj (void *ptr)
226 {
227   return reinterpret_cast<__cxa_exception *>(ptr) - 1;
228 }
229
230 // Acquire the C++ exception header from the generic exception header.
231 static inline __cxa_exception *
232 __get_exception_header_from_ue (_Unwind_Exception *exc)
233 {
234   return reinterpret_cast<__cxa_exception *>(exc + 1) - 1;
235 }
236
237 // Acquire the C++ refcounted exception header from the C++ object.
238 static inline __cxa_refcounted_exception *
239 __get_refcounted_exception_header_from_obj (void *ptr)
240 {
241   return reinterpret_cast<__cxa_refcounted_exception *>(ptr) - 1;
242 }
243
244 // Acquire the C++ refcounted exception header from the generic exception
245 // header.
246 static inline __cxa_refcounted_exception *
247 __get_refcounted_exception_header_from_ue (_Unwind_Exception *exc)
248 {
249   return reinterpret_cast<__cxa_refcounted_exception *>(exc + 1) - 1;
250 }
251
252 static inline __cxa_dependent_exception *
253 __get_dependent_exception_from_ue (_Unwind_Exception *exc)
254 {
255   return reinterpret_cast<__cxa_dependent_exception *>(exc + 1) - 1;
256 }
257
258 #ifdef __ARM_EABI_UNWINDER__
259 static inline bool
260 __is_gxx_exception_class(_Unwind_Exception_Class c)
261 {
262   // TODO: Take advantage of the fact that c will always be word aligned.
263   return c[0] == 'G'
264          && c[1] == 'N'
265          && c[2] == 'U'
266          && c[3] == 'C'
267          && c[4] == 'C'
268          && c[5] == '+'
269          && c[6] == '+'
270          && (c[7] == '\0' || c[7] == '\x01');
271 }
272
273 // Only checks for primary or dependent, but not that it is a C++ exception at
274 // all.
275 static inline bool
276 __is_dependent_exception(_Unwind_Exception_Class c)
277 {
278   return c[7] == '\x01';
279 }
280
281 static inline void
282 __GXX_INIT_PRIMARY_EXCEPTION_CLASS(_Unwind_Exception_Class c)
283 {
284   c[0] = 'G';
285   c[1] = 'N';
286   c[2] = 'U';
287   c[3] = 'C';
288   c[4] = 'C';
289   c[5] = '+';
290   c[6] = '+';
291   c[7] = '\0';
292 }
293
294 static inline void
295 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(_Unwind_Exception_Class c)
296 {
297   c[0] = 'G';
298   c[1] = 'N';
299   c[2] = 'U';
300   c[3] = 'C';
301   c[4] = 'C';
302   c[5] = '+';
303   c[6] = '+';
304   c[7] = '\x01';
305 }
306
307 static inline bool
308 __is_gxx_forced_unwind_class(_Unwind_Exception_Class c)
309 {
310   return c[0] == 'G'
311          && c[1] == 'N'
312          && c[2] == 'U'
313          && c[3] == 'C'
314          && c[4] == 'F'
315          && c[5] == 'O'
316          && c[6] == 'R'
317          && c[7] == '\0';
318 }
319
320 static inline void
321 __GXX_INIT_FORCED_UNWIND_CLASS(_Unwind_Exception_Class c)
322 {
323   c[0] = 'G';
324   c[1] = 'N';
325   c[2] = 'U';
326   c[3] = 'C';
327   c[4] = 'F';
328   c[5] = 'O';
329   c[6] = 'R';
330   c[7] = '\0';
331 }
332
333 static inline void*
334 __gxx_caught_object(_Unwind_Exception* eo)
335 {
336   return (void*)eo->barrier_cache.bitpattern[0];
337 }
338 #else // !__ARM_EABI_UNWINDER__
339 // This is the primary exception class we report -- "GNUCC++\0".
340 const _Unwind_Exception_Class __gxx_primary_exception_class
341 = ((((((((_Unwind_Exception_Class) 'G' 
342          << 8 | (_Unwind_Exception_Class) 'N')
343         << 8 | (_Unwind_Exception_Class) 'U')
344        << 8 | (_Unwind_Exception_Class) 'C')
345       << 8 | (_Unwind_Exception_Class) 'C')
346      << 8 | (_Unwind_Exception_Class) '+')
347     << 8 | (_Unwind_Exception_Class) '+')
348    << 8 | (_Unwind_Exception_Class) '\0');
349
350 // This is the dependent (from std::rethrow_exception) exception class we report
351 // "GNUCC++\x01"
352 const _Unwind_Exception_Class __gxx_dependent_exception_class
353 = ((((((((_Unwind_Exception_Class) 'G' 
354          << 8 | (_Unwind_Exception_Class) 'N')
355         << 8 | (_Unwind_Exception_Class) 'U')
356        << 8 | (_Unwind_Exception_Class) 'C')
357       << 8 | (_Unwind_Exception_Class) 'C')
358      << 8 | (_Unwind_Exception_Class) '+')
359     << 8 | (_Unwind_Exception_Class) '+')
360    << 8 | (_Unwind_Exception_Class) '\x01');
361
362 static inline bool
363 __is_gxx_exception_class(_Unwind_Exception_Class c)
364 {
365   return c == __gxx_primary_exception_class
366       || c == __gxx_dependent_exception_class;
367 }
368
369 // Only checks for primary or dependent, but not that it is a C++ exception at
370 // all.
371 static inline bool
372 __is_dependent_exception(_Unwind_Exception_Class c)
373 {
374   return (c & 1);
375 }
376
377 #define __GXX_INIT_PRIMARY_EXCEPTION_CLASS(c) c = __gxx_primary_exception_class
378 #define __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(c) \
379   c = __gxx_dependent_exception_class
380
381 // GNU C++ personality routine, Version 0.
382 extern "C" _Unwind_Reason_Code __gxx_personality_v0
383      (int, _Unwind_Action, _Unwind_Exception_Class,
384       struct _Unwind_Exception *, struct _Unwind_Context *);
385
386 // GNU C++ sjlj personality routine, Version 0.
387 extern "C" _Unwind_Reason_Code __gxx_personality_sj0
388      (int, _Unwind_Action, _Unwind_Exception_Class,
389       struct _Unwind_Exception *, struct _Unwind_Context *);
390
391 static inline void*
392 __gxx_caught_object(_Unwind_Exception* eo)
393 {
394   // Bad as it looks, this actually works for dependent exceptions too.
395   __cxa_exception* header = __get_exception_header_from_ue (eo);
396   return header->adjustedPtr;
397 }
398 #endif // !__ARM_EABI_UNWINDER__
399
400 static inline void*
401 __get_object_from_ue(_Unwind_Exception* eo) throw()
402 {
403   return __is_dependent_exception (eo->exception_class) ?
404     __get_dependent_exception_from_ue (eo)->primaryException :
405     eo + 1;
406 }
407
408 static inline void *
409 __get_object_from_ambiguous_exception(__cxa_exception *p_or_d) throw()
410 {
411         return __get_object_from_ue (&p_or_d->unwindHeader);
412 }
413
414
415 } /* namespace __cxxabiv1 */
416
417 #pragma GCC visibility pop
418
419 #endif // _UNWIND_CXX_H