cxxabi.h revision 261727
180708Sjake/*
282899Sjake * Copyright 2012 David Chisnall. All rights reserved.
380708Sjake *
482899Sjake * Permission is hereby granted, free of charge, to any person obtaining a copy
582899Sjake * of this software and associated documentation files (the "Software"), to
680708Sjake * deal in the Software without restriction, including without limitation the
782899Sjake * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
882899Sjake * sell copies of the Software, and to permit persons to whom the Software is
982899Sjake * furnished to do so, subject to the following conditions:
1080708Sjake *
1180708Sjake * The above copyright notice and this permission notice shall be
1280708Sjake * included in all copies or substantial portions of the Software.
1380708Sjake *
1480708Sjake * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1580708Sjake * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1680708Sjake * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1780708Sjake * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
1882899Sjake * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
1982899Sjake * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2082899Sjake * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2180708Sjake */
2282899Sjake
2380708Sjake#ifndef __CXXABI_H_
2480708Sjake#define __CXXABI_H_
2582899Sjake#include <stddef.h>
2680708Sjake#include <stdint.h>
2780708Sjake#include "unwind.h"
2880708Sjakenamespace std
2980708Sjake{
3080708Sjake	class type_info;
3180708Sjake}
3280708Sjake/*
3380708Sjake * The cxxabi.h header provides a set of public definitions for types and
3482899Sjake * functions defined by the Itanium C++ ABI specification.  For reference, see
3582899Sjake * the ABI specification here:
3680708Sjake *
3780708Sjake * http://sourcery.mentor.com/public/cxx-abi/abi.html
3880708Sjake *
3980708Sjake * All deviations from this specification, unless otherwise noted, are
4080708Sjake * accidental.
4180708Sjake */
4280708Sjake
4380708Sjake#ifdef __cplusplus
4480708Sjakenamespace __cxxabiv1 {
4580708Sjakeextern "C" {
4680708Sjake#endif
4780708Sjake/**
4880708Sjake * Function type to call when an unexpected exception is encountered.
4980708Sjake */
5080708Sjaketypedef void (*unexpected_handler)();
5180708Sjake/**
5280708Sjake * Function type to call when an unrecoverable condition is encountered.
5380708Sjake */
5480708Sjaketypedef void (*terminate_handler)();
5580708Sjake
5680708Sjake
5780708Sjake/**
5880708Sjake * Structure used as a header on thrown exceptions.  This is the same layout as
5980708Sjake * defined by the Itanium ABI spec, so should be interoperable with any other
6080708Sjake * implementation of this spec, such as GNU libsupc++.
6180708Sjake *
6280708Sjake * This structure is allocated when an exception is thrown.  Unwinding happens
6380708Sjake * in two phases, the first looks for a handler and the second installs the
6480708Sjake * context.  This structure stores a cache of the handler location between
65169291Salc * phase 1 and phase 2.  Unfortunately, cleanup information is not cached, so
66169291Salc * must be looked up in both phases.  This happens for two reasons.  The first
67169291Salc * is that we don't know how many frames containing cleanups there will be, and
68169291Salc * we should avoid dynamic allocation during unwinding (the exception may be
69169291Salc * reporting that we've run out of memory).  The second is that finding
70170262Salc * cleanups is much cheaper than finding handlers, because we don't have to
71170262Salc * look at the type table at all.
72170262Salc *
73223379Smarius * Note: Several fields of this structure have not-very-informative names.
74170262Salc * These are taken from the ABI spec and have not been changed to make it
75170262Salc * easier for people referring to to the spec while reading this code.
76170262Salc */
77170262Salcstruct __cxa_exception
78284147Salc{
79170262Salc#if __LP64__
80170262Salc	/**
81170262Salc	 * Reference count.  Used to support the C++11 exception_ptr class.  This
82170262Salc	 * is prepended to the structure in 64-bit mode and squeezed in to the
83284147Salc	 * padding left before the 64-bit aligned _Unwind_Exception at the end in
84170262Salc	 * 32-bit mode.
85170262Salc	 *
86170262Salc	 * Note that it is safe to extend this structure at the beginning, rather
87170262Salc	 * than the end, because the public API for creating it returns the address
88170262Salc	 * of the end (where the exception object can be stored).
89170262Salc	 */
90170262Salc	uintptr_t referenceCount;
91170262Salc#endif
92170262Salc	/** Type info for the thrown object. */
93170262Salc	std::type_info *exceptionType;
94170262Salc	/** Destructor for the object, if one exists. */
95170262Salc	void (*exceptionDestructor) (void *);
96170262Salc	/** Handler called when an exception specification is violated. */
97170262Salc	unexpected_handler unexpectedHandler;
98170262Salc	/** Hander called to terminate. */
99170262Salc	terminate_handler terminateHandler;
100170262Salc	/**
101170262Salc	 * Next exception in the list.  If an exception is thrown inside a catch
102170262Salc	 * block and caught in a nested catch, this points to the exception that
103170262Salc	 * will be handled after the inner catch block completes.
104170262Salc	 */
105170262Salc	__cxa_exception *nextException;
106170262Salc	/**
107170262Salc	 * The number of handlers that currently have references to this
108215093Salc	 * exception.  The top (non-sign) bit of this is used as a flag to indicate
109174938Salc	 * that the exception is being rethrown, so should not be deleted when its
110174938Salc	 * handler count reaches 0 (which it doesn't with the top bit set).
111215093Salc	 */
112174938Salc	int handlerCount;
113174938Salc#ifdef __arm__
114174938Salc	/**
115215093Salc	 * The ARM EH ABI requires the unwind library to keep track of exceptions
116215093Salc	 * during cleanups.  These support nesting, so we need to keep a list of
117215093Salc	 * them.
118215093Salc	 */
119215093Salc	_Unwind_Exception *nextCleanup;
120215093Salc	/**
121240190Sgavin	 * The number of cleanups that are currently being run on this exception.
122108245Sjake	 */
123108245Sjake	int cleanupCount;
124108245Sjake#endif
125108245Sjake	/**
126108245Sjake	 * The selector value to be returned when installing the catch handler.
127108245Sjake	 * Used at the call site to determine which catch() block should execute.
128108245Sjake	 * This is found in phase 1 of unwinding then installed in phase 2.
129108245Sjake	 */
130108245Sjake	int handlerSwitchValue;
131223379Smarius	/**
132223379Smarius	 * The action record for the catch.  This is cached during phase 1
133108245Sjake	 * unwinding.
134223379Smarius	 */
135108245Sjake	const char *actionRecord;
136108245Sjake	/**
137223379Smarius	 * Pointer to the language-specific data area (LSDA) for the handler
138108245Sjake	 * frame.  This is unused in this implementation, but set for ABI
139108245Sjake	 * compatibility in case we want to mix code in very weird ways.
140108245Sjake	 */
141108245Sjake	const char *languageSpecificData;
142108245Sjake	/** The cached landing pad for the catch handler.*/
143108245Sjake	void *catchTemp;
144246554Skib	/**
145246554Skib	 * The pointer that will be returned as the pointer to the object.  When
146108245Sjake	 * throwing a class and catching a virtual superclass (for example), we
147108245Sjake	 * need to adjust the thrown pointer to make it all work correctly.
148108245Sjake	 */
149108245Sjake	void *adjustedPtr;
150108245Sjake#if !__LP64__
151108245Sjake	/**
152108245Sjake	 * Reference count.  Used to support the C++11 exception_ptr class.  This
153108245Sjake	 * is prepended to the structure in 64-bit mode and squeezed in to the
154108245Sjake	 * padding left before the 64-bit aligned _Unwind_Exception at the end in
155108245Sjake	 * 32-bit mode.
156108245Sjake	 *
157108245Sjake	 * Note that it is safe to extend this structure at the beginning, rather
158108245Sjake	 * than the end, because the public API for creating it returns the address
159108245Sjake	 * of the end (where the exception object can be stored)
160108245Sjake	 */
161108245Sjake	uintptr_t referenceCount;
162108245Sjake#endif
163108245Sjake	/** The language-agnostic part of the exception header. */
164108245Sjake	_Unwind_Exception unwindHeader;
165108245Sjake};
166108245Sjake
167108245Sjake/**
168108245Sjake * ABI-specified globals structure.  Returned by the __cxa_get_globals()
169108245Sjake * function and its fast variant.  This is a per-thread structure - every
170108245Sjake * thread will have one lazily allocated.
171108245Sjake *
172108245Sjake * This structure is defined by the ABI, so may be used outside of this
173108245Sjake * library.
174108245Sjake */
175108245Sjakestruct __cxa_eh_globals
17688653Sjake{
177108245Sjake	/**
178108245Sjake	 * A linked list of exceptions that are currently caught.  There may be
17980708Sjake	 * several of these in nested catch() blocks.
180108245Sjake	 */
181108245Sjake	__cxa_exception *caughtExceptions;
18288653Sjake	/**
183108245Sjake	 * The number of uncaught exceptions.
184108245Sjake	 */
18580708Sjake	unsigned int uncaughtExceptions;
186108245Sjake};
187181642Smarius/**
188108245Sjake * ABI function returning the __cxa_eh_globals structure.
189108245Sjake */
190108245Sjake__cxa_eh_globals *__cxa_get_globals(void);
191108245Sjake/**
192108245Sjake * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already
193108245Sjake * been called at least once by this thread.
194108245Sjake */
195108245Sjake__cxa_eh_globals *__cxa_get_globals_fast(void);
196188455Smarius
197108245Sjakestd::type_info * __cxa_current_exception_type();
198108245Sjake
19980708Sjake/**
200257854Salc * Throws an exception returned by __cxa_current_primary_exception().  This
20180708Sjake * exception may have been caught in another thread.
202257854Salc */
203257854Salcvoid __cxa_rethrow_primary_exception(void* thrown_exception);
20480708Sjake/**
20580708Sjake * Returns the current exception in a form that can be stored in an
20685241Sjake * exception_ptr object and then rethrown by a call to
207257854Salc * __cxa_rethrow_primary_exception().
20891974Sjake */
209257854Salcvoid *__cxa_current_primary_exception(void);
210257854Salc/**
21191974Sjake * Increments the reference count of an exception.  Called when an
21291974Sjake * exception_ptr is copied.
21391974Sjake */
214257854Salcvoid __cxa_increment_exception_refcount(void* thrown_exception);
215257854Salc/**
216216016Sfjoe * Decrements the reference count of an exception.  Called when an
217216016Sfjoe * exception_ptr is deleted.
218216625Smarius */
219216625Smariusvoid __cxa_decrement_exception_refcount(void* thrown_exception);
220216016Sfjoe/**
221216016Sfjoe * Demangles a C++ symbol or type name.  The buffer, if non-NULL, must be
222216016Sfjoe * allocated with malloc() and must be *n bytes or more long.  This function
22380708Sjake * may call realloc() on the value pointed to by buf, and will return the
22480708Sjake * length of the string via *n.
22580708Sjake *
22680708Sjake * The value pointed to by status is set to one of the following:
22780708Sjake *
22880708Sjake * 0: success
229108332Sjake * -1: memory allocation failure
230108332Sjake * -2: invalid mangled name
231223378Smarius * -3: invalid arguments
232101653Sjake */
233101653Sjakechar* __cxa_demangle(const char* mangled_name,
234221855Smdf                     char* buf,
235221855Smdf                     size_t* n,
236221855Smdf                     int* status);
237221855Smdf#ifdef __cplusplus
238221855Smdf} // extern "C"
239221855Smdf} // namespace
240221855Smdf
241269577Sglebiusnamespace abi = __cxxabiv1;
242269782Skib
243270429Skib#endif /* __cplusplus */
244270429Skib#endif /* __CXXABI_H_ */
245270429Skib