unwind-cxx.h revision 269792
1// -*- C++ -*- Exception handling and frame unwind runtime interface routines.
2// Copyright (C) 2001 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
43#pragma GCC visibility push(default)
44
45namespace __cxxabiv1
46{
47
48// A C++ exception object consists of a header, which is a wrapper around
49// an unwind object header with additional C++ specific information,
50// followed by the exception object itself.
51
52struct __cxa_exception
53{
54  // Manage the exception object itself.
55  std::type_info *exceptionType;
56  void (*exceptionDestructor)(void *);
57
58  // The C++ standard has entertaining rules wrt calling set_terminate
59  // and set_unexpected in the middle of the exception cleanup process.
60  std::unexpected_handler unexpectedHandler;
61  std::terminate_handler terminateHandler;
62
63  // The caught exception stack threads through here.
64  __cxa_exception *nextException;
65
66  // How many nested handlers have caught this exception.  A negated
67  // value is a signal that this object has been rethrown.
68  int handlerCount;
69
70#ifdef __ARM_EABI_UNWINDER__
71  // Stack of exceptions in cleanups.
72  __cxa_exception* nextPropagatingException;
73
74  // The nuber of active cleanup handlers for this exception.
75  int propagationCount;
76#else
77  // Cache parsed handler data from the personality routine Phase 1
78  // for Phase 2 and __cxa_call_unexpected.
79  int handlerSwitchValue;
80  const unsigned char *actionRecord;
81  const unsigned char *languageSpecificData;
82  _Unwind_Ptr catchTemp;
83  void *adjustedPtr;
84#endif
85
86  // The generic exception header.  Must be last.
87  _Unwind_Exception unwindHeader;
88};
89
90// Each thread in a C++ program has access to a __cxa_eh_globals object.
91struct __cxa_eh_globals
92{
93  __cxa_exception *caughtExceptions;
94  unsigned int uncaughtExceptions;
95#ifdef __ARM_EABI_UNWINDER__
96  __cxa_exception* propagatingExceptions;
97#endif
98};
99
100
101// The __cxa_eh_globals for the current thread can be obtained by using
102// either of the following functions.  The "fast" version assumes at least
103// one prior call of __cxa_get_globals has been made from the current
104// thread, so no initialization is necessary.
105extern "C" __cxa_eh_globals *__cxa_get_globals () throw();
106extern "C" __cxa_eh_globals *__cxa_get_globals_fast () throw();
107
108// Allocate memory for the exception plus the thown object.
109extern "C" void *__cxa_allocate_exception(std::size_t thrown_size) throw();
110
111// Free the space allocated for the exception.
112extern "C" void __cxa_free_exception(void *thrown_exception) throw();
113
114// Throw the exception.
115extern "C" void __cxa_throw (void *thrown_exception,
116			     std::type_info *tinfo,
117			     void (*dest) (void *))
118     __attribute__((noreturn));
119
120// Used to implement exception handlers.
121extern "C" void *__cxa_get_exception_ptr (void *) throw();
122extern "C" void *__cxa_begin_catch (void *) throw();
123extern "C" void __cxa_end_catch ();
124extern "C" void __cxa_rethrow () __attribute__((noreturn));
125
126// These facilitate code generation for recurring situations.
127extern "C" void __cxa_bad_cast ();
128extern "C" void __cxa_bad_typeid ();
129
130// @@@ These are not directly specified by the IA-64 C++ ABI.
131
132// Handles re-checking the exception specification if unexpectedHandler
133// throws, and if bad_exception needs to be thrown.  Called from the
134// compiler.
135extern "C" void __cxa_call_unexpected (void *) __attribute__((noreturn));
136extern "C" void __cxa_call_terminate (_Unwind_Exception*) __attribute__((noreturn));
137
138#ifdef __ARM_EABI_UNWINDER__
139// Arm EABI specified routines.
140typedef enum {
141  ctm_failed = 0,
142  ctm_succeeded = 1,
143  ctm_succeeded_with_ptr_to_base = 2
144} __cxa_type_match_result;
145extern "C" __cxa_type_match_result __cxa_type_match(_Unwind_Exception*, const std::type_info*,
146				 bool, void**);
147extern "C" bool __cxa_begin_cleanup (_Unwind_Exception*);
148extern "C" void __cxa_end_cleanup (void);
149#endif
150
151// Invokes given handler, dying appropriately if the user handler was
152// so inconsiderate as to return.
153extern void __terminate(std::terminate_handler) __attribute__((noreturn));
154extern void __unexpected(std::unexpected_handler) __attribute__((noreturn));
155
156// The current installed user handlers.
157extern std::terminate_handler __terminate_handler;
158extern std::unexpected_handler __unexpected_handler;
159
160// These are explicitly GNU C++ specific.
161
162// Acquire the C++ exception header from the C++ object.
163static inline __cxa_exception *
164__get_exception_header_from_obj (void *ptr)
165{
166  return reinterpret_cast<__cxa_exception *>(ptr) - 1;
167}
168
169// Acquire the C++ exception header from the generic exception header.
170static inline __cxa_exception *
171__get_exception_header_from_ue (_Unwind_Exception *exc)
172{
173  return reinterpret_cast<__cxa_exception *>(exc + 1) - 1;
174}
175
176#if defined(__ARM_EABI_UNWINDER__) && !defined(__FreeBSD__)
177static inline bool
178__is_gxx_exception_class(_Unwind_Exception_Class c)
179{
180  // TODO: Take advantage of the fact that c will always be word aligned.
181  return c[0] == 'G'
182	 && c[1] == 'N'
183	 && c[2] == 'U'
184	 && c[3] == 'C'
185	 && c[4] == 'C'
186	 && c[5] == '+'
187	 && c[6] == '+'
188	 && c[7] == '\0';
189}
190
191static inline void
192__GXX_INIT_EXCEPTION_CLASS(_Unwind_Exception_Class c)
193{
194  c[0] = 'G';
195  c[1] = 'N';
196  c[2] = 'U';
197  c[3] = 'C';
198  c[4] = 'C';
199  c[5] = '+';
200  c[6] = '+';
201  c[7] = '\0';
202}
203#else // !__ARM_EABI_UNWINDER__ || __FreeBSD__
204// This is the exception class we report -- "GNUCC++\0".
205const _Unwind_Exception_Class __gxx_exception_class
206= ((((((((_Unwind_Exception_Class) 'G'
207	 << 8 | (_Unwind_Exception_Class) 'N')
208	<< 8 | (_Unwind_Exception_Class) 'U')
209       << 8 | (_Unwind_Exception_Class) 'C')
210      << 8 | (_Unwind_Exception_Class) 'C')
211     << 8 | (_Unwind_Exception_Class) '+')
212    << 8 | (_Unwind_Exception_Class) '+')
213   << 8 | (_Unwind_Exception_Class) '\0');
214
215static inline bool
216__is_gxx_exception_class(_Unwind_Exception_Class c)
217{
218  return c == __gxx_exception_class;
219}
220#define __GXX_INIT_EXCEPTION_CLASS(c) c = __gxx_exception_class
221#endif
222
223#ifdef __ARM_EABI_UNWINDER__
224static inline void*
225__gxx_caught_object(_Unwind_Exception* eo)
226{
227  return (void*)eo->barrier_cache.bitpattern[0];
228}
229#else // !__ARM_EABI_UNWINDER__
230
231// GNU C++ personality routine, Version 0.
232extern "C" _Unwind_Reason_Code __gxx_personality_v0
233     (int, _Unwind_Action, _Unwind_Exception_Class,
234      struct _Unwind_Exception *, struct _Unwind_Context *);
235
236// GNU C++ sjlj personality routine, Version 0.
237extern "C" _Unwind_Reason_Code __gxx_personality_sj0
238     (int, _Unwind_Action, _Unwind_Exception_Class,
239      struct _Unwind_Exception *, struct _Unwind_Context *);
240
241static inline void*
242__gxx_caught_object(_Unwind_Exception* eo)
243{
244  __cxa_exception* header = __get_exception_header_from_ue (eo);
245  return header->adjustedPtr;
246}
247#endif // !__ARM_EABI_UNWINDER__
248
249} /* namespace __cxxabiv1 */
250
251#pragma GCC visibility pop
252
253#endif // _UNWIND_CXX_H
254