OSDN Git Service

3f6a76560fd6cd7271732117c5ad8ca12c83681d
[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
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 2, 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 // You should have received a copy of the GNU General Public License
18 // along with GCC; see the file COPYING.  If not, write to
19 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 // This is derived from the C++ ABI for IA-64.  Where we diverge
32 // for cross-architecture compatibility are noted with "@@@".
33
34 #ifndef _UNWIND_CXX_H
35 #define _UNWIND_CXX_H 1
36
37 // Level 2: C++ ABI
38
39 #include <typeinfo>
40 #include <exception>
41 #include <cstddef>
42 #include "unwind.h"
43 #include <bits/atomic_word.h>
44
45 #pragma GCC visibility push(default)
46
47 namespace __cxxabiv1
48 {
49
50 // A primary C++ exception object consists of a header, which is a wrapper
51 // around an unwind object header with additional C++ specific information,
52 // followed by the exception object itself.
53
54 struct __cxa_exception
55 {
56   // Manage this header.
57   _Atomic_word referenceCount;
58
59   // Manage the exception object itself.
60   std::type_info *exceptionType;
61   void (*exceptionDestructor)(void *); 
62
63   // The C++ standard has entertaining rules wrt calling set_terminate
64   // and set_unexpected in the middle of the exception cleanup process.
65   std::unexpected_handler unexpectedHandler;
66   std::terminate_handler terminateHandler;
67
68   // The caught exception stack threads through here.
69   __cxa_exception *nextException;
70
71   // How many nested handlers have caught this exception.  A negated
72   // value is a signal that this object has been rethrown.
73   int handlerCount;
74
75 #ifdef __ARM_EABI_UNWINDER__
76   // Stack of exceptions in cleanups.
77   __cxa_exception* nextPropagatingException;
78
79   // The nuber of active cleanup handlers for this exception.
80   int propagationCount;
81 #else
82   // Cache parsed handler data from the personality routine Phase 1
83   // for Phase 2 and __cxa_call_unexpected.
84   int handlerSwitchValue;
85   const unsigned char *actionRecord;
86   const unsigned char *languageSpecificData;
87   _Unwind_Ptr catchTemp;
88   void *adjustedPtr;
89 #endif
90
91   // The generic exception header.  Must be last.
92   _Unwind_Exception unwindHeader;
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 extern "C" __cxa_eh_globals *__cxa_get_globals_fast () throw();
153
154 // Allocate memory for the primary exception plus the thrown object.
155 extern "C" void *__cxa_allocate_exception(std::size_t thrown_size) throw();
156
157 // Free the space allocated for the primary exception.
158 extern "C" void __cxa_free_exception(void *thrown_exception) throw();
159
160 // Allocate memory for a dependent exception.
161 extern "C" __cxa_dependent_exception*
162 __cxa_allocate_dependent_exception() throw();
163
164 // Free the space allocated for the dependent exception.
165 extern "C" void
166 __cxa_free_dependent_exception(__cxa_dependent_exception *ex) throw();
167
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));
173
174 // Used to implement exception handlers.
175 extern "C" void *__cxa_get_exception_ptr (void *) throw();
176 extern "C" void *__cxa_begin_catch (void *) throw();
177 extern "C" void __cxa_end_catch ();
178 extern "C" void __cxa_rethrow () __attribute__((noreturn));
179
180 // These facilitate code generation for recurring situations.
181 extern "C" void __cxa_bad_cast ();
182 extern "C" void __cxa_bad_typeid ();
183
184 // @@@ These are not directly specified by the IA-64 C++ ABI.
185
186 // Handles re-checking the exception specification if unexpectedHandler
187 // throws, and if bad_exception needs to be thrown.  Called from the
188 // compiler.
189 extern "C" void __cxa_call_unexpected (void *) __attribute__((noreturn));
190 extern "C" void __cxa_call_terminate (void*) __attribute__((noreturn));
191
192 #ifdef __ARM_EABI_UNWINDER__
193 // Arm EABI specified routines.
194 typedef enum {
195   ctm_failed = 0,
196   ctm_succeeded = 1,
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*,
200                                  bool, void**);
201 extern "C" void __cxa_begin_cleanup (_Unwind_Exception*);
202 extern "C" void __cxa_end_cleanup (void);
203 #endif
204
205 // Invokes given handler, dying appropriately if the user handler was
206 // so inconsiderate as to return.
207 extern void __terminate(std::terminate_handler) __attribute__((noreturn));
208 extern void __unexpected(std::unexpected_handler) __attribute__((noreturn));
209
210 // The current installed user handlers.
211 extern std::terminate_handler __terminate_handler;
212 extern std::unexpected_handler __unexpected_handler;
213
214 // These are explicitly GNU C++ specific.
215
216 // Acquire the C++ exception header from the C++ object.
217 static inline __cxa_exception *
218 __get_exception_header_from_obj (void *ptr)
219 {
220   return reinterpret_cast<__cxa_exception *>(ptr) - 1;
221 }
222
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)
226 {
227   return reinterpret_cast<__cxa_exception *>(exc + 1) - 1;
228 }
229
230 static inline __cxa_dependent_exception *
231 __get_dependent_exception_from_ue (_Unwind_Exception *exc)
232 {
233   return reinterpret_cast<__cxa_dependent_exception *>(exc + 1) - 1;
234 }
235
236 #ifdef __ARM_EABI_UNWINDER__
237 static inline bool
238 __is_gxx_exception_class(_Unwind_Exception_Class c)
239 {
240   // TODO: Take advantage of the fact that c will always be word aligned.
241   return c[0] == 'G'
242          && c[1] == 'N'
243          && c[2] == 'U'
244          && c[3] == 'C'
245          && c[4] == 'C'
246          && c[5] == '+'
247          && c[6] == '+'
248          && (c[7] == '\0' || c[7] == '\x01');
249 }
250
251 // Only checks for primary or dependent, but not that it is a C++ exception at
252 // all.
253 static inline bool
254 __is_dependent_exception(_Unwind_Exception_Class c)
255 {
256   return c[7] == '\x01';
257 }
258
259 static inline void
260 __GXX_INIT_PRIMARY_EXCEPTION_CLASS(_Unwind_Exception_Class c)
261 {
262   c[0] = 'G';
263   c[1] = 'N';
264   c[2] = 'U';
265   c[3] = 'C';
266   c[4] = 'C';
267   c[5] = '+';
268   c[6] = '+';
269   c[7] = '\0';
270 }
271
272 static inline void
273 __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(_Unwind_Exception_Class c)
274 {
275   c[0] = 'G';
276   c[1] = 'N';
277   c[2] = 'U';
278   c[3] = 'C';
279   c[4] = 'C';
280   c[5] = '+';
281   c[6] = '+';
282   c[7] = '\x01';
283 }
284
285 static inline bool
286 __is_gxx_forced_unwind_class(_Unwind_Exception_Class c)
287 {
288   return c[0] == 'G'
289          && c[1] == 'N'
290          && c[2] == 'U'
291          && c[3] == 'C'
292          && c[4] == 'F'
293          && c[5] == 'O'
294          && c[6] == 'R'
295          && c[7] == '\0';
296 }
297
298 static inline void
299 __GXX_INIT_FORCED_UNWIND_CLASS(_Unwind_Exception_Class c)
300 {
301   c[0] = 'G';
302   c[1] = 'N';
303   c[2] = 'U';
304   c[3] = 'C';
305   c[4] = 'F';
306   c[5] = 'O';
307   c[6] = 'R';
308   c[7] = '\0';
309 }
310
311 static inline void*
312 __gxx_caught_object(_Unwind_Exception* eo)
313 {
314   return (void*)eo->barrier_cache.bitpattern[0];
315 }
316 #else // !__ARM_EABI_UNWINDER__
317 // This is the primary exception class we report -- "GNUCC++\0".
318 const _Unwind_Exception_Class __gxx_primary_exception_class
319 = ((((((((_Unwind_Exception_Class) 'G' 
320          << 8 | (_Unwind_Exception_Class) 'N')
321         << 8 | (_Unwind_Exception_Class) 'U')
322        << 8 | (_Unwind_Exception_Class) 'C')
323       << 8 | (_Unwind_Exception_Class) 'C')
324      << 8 | (_Unwind_Exception_Class) '+')
325     << 8 | (_Unwind_Exception_Class) '+')
326    << 8 | (_Unwind_Exception_Class) '\0');
327
328 // This is the dependent (from std::rethrow_exception) exception class we report
329 // "GNUCC++\x01"
330 const _Unwind_Exception_Class __gxx_dependent_exception_class
331 = ((((((((_Unwind_Exception_Class) 'G' 
332          << 8 | (_Unwind_Exception_Class) 'N')
333         << 8 | (_Unwind_Exception_Class) 'U')
334        << 8 | (_Unwind_Exception_Class) 'C')
335       << 8 | (_Unwind_Exception_Class) 'C')
336      << 8 | (_Unwind_Exception_Class) '+')
337     << 8 | (_Unwind_Exception_Class) '+')
338    << 8 | (_Unwind_Exception_Class) '\x01');
339
340 static inline bool
341 __is_gxx_exception_class(_Unwind_Exception_Class c)
342 {
343   return c == __gxx_primary_exception_class
344       || c == __gxx_dependent_exception_class;
345 }
346
347 // Only checks for primary or dependent, but not that it is a C++ exception at
348 // all.
349 static inline bool
350 __is_dependent_exception(_Unwind_Exception_Class c)
351 {
352   return (c & 1);
353 }
354
355 #define __GXX_INIT_PRIMARY_EXCEPTION_CLASS(c) c = __gxx_primary_exception_class
356 #define __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(c) \
357   c = __gxx_dependent_exception_class
358
359 // GNU C++ personality routine, Version 0.
360 extern "C" _Unwind_Reason_Code __gxx_personality_v0
361      (int, _Unwind_Action, _Unwind_Exception_Class,
362       struct _Unwind_Exception *, struct _Unwind_Context *);
363
364 // GNU C++ sjlj personality routine, Version 0.
365 extern "C" _Unwind_Reason_Code __gxx_personality_sj0
366      (int, _Unwind_Action, _Unwind_Exception_Class,
367       struct _Unwind_Exception *, struct _Unwind_Context *);
368
369 static inline void*
370 __gxx_caught_object(_Unwind_Exception* eo)
371 {
372   // Bad as it looks, this actually works for dependent exceptions too.
373   __cxa_exception* header = __get_exception_header_from_ue (eo);
374   return header->adjustedPtr;
375 }
376 #endif // !__ARM_EABI_UNWINDER__
377
378 static inline void*
379 __get_object_from_ue(_Unwind_Exception* eo) throw()
380 {
381   return __is_dependent_exception (eo->exception_class) ?
382     __get_dependent_exception_from_ue (eo)->primaryException :
383     eo + 1;
384 }
385
386 static inline void *
387 __get_object_from_ambiguous_exception(__cxa_exception *p_or_d) throw()
388 {
389         return __get_object_from_ue (&p_or_d->unwindHeader);
390 }
391
392
393 } /* namespace __cxxabiv1 */
394
395 #pragma GCC visibility pop
396
397 #endif // _UNWIND_CXX_H