xref: /freebsd/contrib/libcxxrt/cxxabi.h (revision 3f0164abf32b9b761e0a2cb4bdca3a8b84f156d4)
1 /*
2  * Copyright 2012 David Chisnall. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be
12  * included in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __CXXABI_H_
24 #define __CXXABI_H_
25 #include <stdint.h>
26 #include "unwind.h"
27 namespace std
28 {
29 	class type_info;
30 }
31 /*
32  * The cxxabi.h header provides a set of public definitions for types and
33  * functions defined by the Itanium C++ ABI specification.  For reference, see
34  * the ABI specification here:
35  *
36  * http://sourcery.mentor.com/public/cxx-abi/abi.html
37  *
38  * All deviations from this specification, unless otherwise noted, are
39  * accidental.
40  */
41 
42 #ifdef __cplusplus
43 namespace __cxxabiv1 {
44 extern "C" {
45 #endif
46 /**
47  * Function type to call when an unexpected exception is encountered.
48  */
49 typedef void (*unexpected_handler)();
50 /**
51  * Function type to call when an unrecoverable condition is encountered.
52  */
53 typedef void (*terminate_handler)();
54 
55 
56 /**
57  * Structure used as a header on thrown exceptions.  This is the same layout as
58  * defined by the Itanium ABI spec, so should be interoperable with any other
59  * implementation of this spec, such as GNU libsupc++.
60  *
61  * This structure is allocated when an exception is thrown.  Unwinding happens
62  * in two phases, the first looks for a handler and the second installs the
63  * context.  This structure stores a cache of the handler location between
64  * phase 1 and phase 2.  Unfortunately, cleanup information is not cached, so
65  * must be looked up in both phases.  This happens for two reasons.  The first
66  * is that we don't know how many frames containing cleanups there will be, and
67  * we should avoid dynamic allocation during unwinding (the exception may be
68  * reporting that we've run out of memory).  The second is that finding
69  * cleanups is much cheaper than finding handlers, because we don't have to
70  * look at the type table at all.
71  *
72  * Note: Several fields of this structure have not-very-informative names.
73  * These are taken from the ABI spec and have not been changed to make it
74  * easier for people referring to to the spec while reading this code.
75  */
76 struct __cxa_exception
77 {
78 #if __LP64__
79 	/**
80 	 * Reference count.  Used to support the C++11 exception_ptr class.  This
81 	 * is prepended to the structure in 64-bit mode and squeezed in to the
82 	 * padding left before the 64-bit aligned _Unwind_Exception at the end in
83 	 * 32-bit mode.
84 	 *
85 	 * Note that it is safe to extend this structure at the beginning, rather
86 	 * than the end, because the public API for creating it returns the address
87 	 * of the end (where the exception object can be stored).
88 	 */
89 	uintptr_t referenceCount;
90 #endif
91 	/** Type info for the thrown object. */
92 	std::type_info *exceptionType;
93 	/** Destructor for the object, if one exists. */
94 	void (*exceptionDestructor) (void *);
95 	/** Handler called when an exception specification is violated. */
96 	unexpected_handler unexpectedHandler;
97 	/** Hander called to terminate. */
98 	terminate_handler terminateHandler;
99 	/**
100 	 * Next exception in the list.  If an exception is thrown inside a catch
101 	 * block and caught in a nested catch, this points to the exception that
102 	 * will be handled after the inner catch block completes.
103 	 */
104 	__cxa_exception *nextException;
105 	/**
106 	 * The number of handlers that currently have references to this
107 	 * exception.  The top (non-sign) bit of this is used as a flag to indicate
108 	 * that the exception is being rethrown, so should not be deleted when its
109 	 * handler count reaches 0 (which it doesn't with the top bit set).
110 	 */
111 	int handlerCount;
112 #ifdef __arm__
113 	/**
114 	 * The ARM EH ABI requires the unwind library to keep track of exceptions
115 	 * during cleanups.  These support nesting, so we need to keep a list of
116 	 * them.
117 	 */
118 	_Unwind_Exception *nextCleanup;
119 	/**
120 	 * The number of cleanups that are currently being run on this exception.
121 	 */
122 	int cleanupCount;
123 #endif
124 	/**
125 	 * The selector value to be returned when installing the catch handler.
126 	 * Used at the call site to determine which catch() block should execute.
127 	 * This is found in phase 1 of unwinding then installed in phase 2.
128 	 */
129 	int handlerSwitchValue;
130 	/**
131 	 * The action record for the catch.  This is cached during phase 1
132 	 * unwinding.
133 	 */
134 	const char *actionRecord;
135 	/**
136 	 * Pointer to the language-specific data area (LSDA) for the handler
137 	 * frame.  This is unused in this implementation, but set for ABI
138 	 * compatibility in case we want to mix code in very weird ways.
139 	 */
140 	const char *languageSpecificData;
141 	/** The cached landing pad for the catch handler.*/
142 	void *catchTemp;
143 	/**
144 	 * The pointer that will be returned as the pointer to the object.  When
145 	 * throwing a class and catching a virtual superclass (for example), we
146 	 * need to adjust the thrown pointer to make it all work correctly.
147 	 */
148 	void *adjustedPtr;
149 #if !__LP64__
150 	/**
151 	 * Reference count.  Used to support the C++11 exception_ptr class.  This
152 	 * is prepended to the structure in 64-bit mode and squeezed in to the
153 	 * padding left before the 64-bit aligned _Unwind_Exception at the end in
154 	 * 32-bit mode.
155 	 *
156 	 * Note that it is safe to extend this structure at the beginning, rather
157 	 * than the end, because the public API for creating it returns the address
158 	 * of the end (where the exception object can be stored)
159 	 */
160 	uintptr_t referenceCount;
161 #endif
162 	/** The language-agnostic part of the exception header. */
163 	_Unwind_Exception unwindHeader;
164 };
165 
166 /**
167  * ABI-specified globals structure.  Returned by the __cxa_get_globals()
168  * function and its fast variant.  This is a per-thread structure - every
169  * thread will have one lazily allocated.
170  *
171  * This structure is defined by the ABI, so may be used outside of this
172  * library.
173  */
174 struct __cxa_eh_globals
175 {
176 	/**
177 	 * A linked list of exceptions that are currently caught.  There may be
178 	 * several of these in nested catch() blocks.
179 	 */
180 	__cxa_exception *caughtExceptions;
181 	/**
182 	 * The number of uncaught exceptions.
183 	 */
184 	unsigned int uncaughtExceptions;
185 };
186 /**
187  * ABI function returning the __cxa_eh_globals structure.
188  */
189 __cxa_eh_globals *__cxa_get_globals(void);
190 /**
191  * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already
192  * been called at least once by this thread.
193  */
194 __cxa_eh_globals *__cxa_get_globals_fast(void);
195 
196 std::type_info * __cxa_current_exception_type();
197 
198 /**
199  * Throws an exception returned by __cxa_current_primary_exception().  This
200  * exception may have been caught in another thread.
201  */
202 void __cxa_rethrow_primary_exception(void* thrown_exception);
203 /**
204  * Returns the current exception in a form that can be stored in an
205  * exception_ptr object and then rethrown by a call to
206  * __cxa_rethrow_primary_exception().
207  */
208 void *__cxa_current_primary_exception(void);
209 /**
210  * Increments the reference count of an exception.  Called when an
211  * exception_ptr is copied.
212  */
213 void __cxa_increment_exception_refcount(void* thrown_exception);
214 /**
215  * Decrements the reference count of an exception.  Called when an
216  * exception_ptr is deleted.
217  */
218 void __cxa_decrement_exception_refcount(void* thrown_exception);
219 /**
220  * Demangles a C++ symbol or type name.  The buffer, if non-NULL, must be
221  * allocated with malloc() and must be *n bytes or more long.  This function
222  * may call realloc() on the value pointed to by buf, and will return the
223  * length of the string via *n.
224  *
225  * The value pointed to by status is set to one of the following:
226  *
227  * 0: success
228  * -1: memory allocation failure
229  * -2: invalid mangled name
230  * -3: invalid arguments
231  */
232 char* __cxa_demangle(const char* mangled_name,
233                      char* buf,
234                      size_t* n,
235                      int* status);
236 #ifdef __cplusplus
237 } // extern "C"
238 } // namespace
239 
240 namespace abi = __cxxabiv1;
241 
242 #endif /* __cplusplus */
243 #endif /* __CXXABI_H_ */
244