/*1* Copyright 2012 David Chisnall. All rights reserved.2*3* Permission is hereby granted, free of charge, to any person obtaining a copy4* of this software and associated documentation files (the "Software"), to5* deal in the Software without restriction, including without limitation the6* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or7* sell copies of the Software, and to permit persons to whom the Software is8* furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be11* included in all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,14* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND16* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE17* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION18* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION19* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.20*/2122#ifndef __CXXABI_H_23#define __CXXABI_H_24#include <stddef.h>25#include <stdint.h>26#include "unwind.h"27namespace std28{29class type_info;30}31/*32* The cxxabi.h header provides a set of public definitions for types and33* functions defined by the Itanium C++ ABI specification. For reference, see34* the ABI specification here:35*36* http://sourcery.mentor.com/public/cxx-abi/abi.html37*38* All deviations from this specification, unless otherwise noted, are39* accidental.40*/4142#ifdef __cplusplus43#if __cplusplus < 201103L44#define _LIBCXXRT_NOEXCEPT throw()45#else46#define _LIBCXXRT_NOEXCEPT noexcept47#endif48namespace __cxxabiv1 {49extern "C" {50#else51#define _LIBCXXRT_NOEXCEPT52#endif53/**54* Function type to call when an unexpected exception is encountered.55*/56typedef void (*unexpected_handler)();57/**58* Function type to call when an unrecoverable condition is encountered.59*/60typedef void (*terminate_handler)();616263/**64* Structure used as a header on thrown exceptions. This is the same layout as65* defined by the Itanium ABI spec, so should be interoperable with any other66* implementation of this spec, such as GNU libsupc++.67*68* This structure is allocated when an exception is thrown. Unwinding happens69* in two phases, the first looks for a handler and the second installs the70* context. This structure stores a cache of the handler location between71* phase 1 and phase 2. Unfortunately, cleanup information is not cached, so72* must be looked up in both phases. This happens for two reasons. The first73* is that we don't know how many frames containing cleanups there will be, and74* we should avoid dynamic allocation during unwinding (the exception may be75* reporting that we've run out of memory). The second is that finding76* cleanups is much cheaper than finding handlers, because we don't have to77* look at the type table at all.78*79* Note: Several fields of this structure have not-very-informative names.80* These are taken from the ABI spec and have not been changed to make it81* easier for people referring to to the spec while reading this code.82*/83struct __cxa_exception84{85#ifdef __LP64__86/**87* Now _Unwind_Exception is marked with __attribute__((aligned)), which88* implies __cxa_exception is also aligned. Insert padding in the89* beginning of the struct, rather than before unwindHeader.90*/91void *reserve;9293/**94* Reference count. Used to support the C++11 exception_ptr class. This95* is prepended to the structure in 64-bit mode and squeezed in to the96* padding left before the 64-bit aligned _Unwind_Exception at the end in97* 32-bit mode.98*99* Note that it is safe to extend this structure at the beginning, rather100* than the end, because the public API for creating it returns the address101* of the end (where the exception object can be stored).102*/103uintptr_t referenceCount;104#endif105/** Type info for the thrown object. */106std::type_info *exceptionType;107/** Destructor for the object, if one exists. */108void (*exceptionDestructor) (void *);109/** Handler called when an exception specification is violated. */110unexpected_handler unexpectedHandler;111/** Hander called to terminate. */112terminate_handler terminateHandler;113/**114* Next exception in the list. If an exception is thrown inside a catch115* block and caught in a nested catch, this points to the exception that116* will be handled after the inner catch block completes.117*/118__cxa_exception *nextException;119/**120* The number of handlers that currently have references to this121* exception. The top (non-sign) bit of this is used as a flag to indicate122* that the exception is being rethrown, so should not be deleted when its123* handler count reaches 0 (which it doesn't with the top bit set).124*/125int handlerCount;126#if defined(__arm__) && !defined(__ARM_DWARF_EH__)127/**128* The ARM EH ABI requires the unwind library to keep track of exceptions129* during cleanups. These support nesting, so we need to keep a list of130* them.131*/132_Unwind_Exception *nextCleanup;133/**134* The number of cleanups that are currently being run on this exception.135*/136int cleanupCount;137#endif138/**139* The selector value to be returned when installing the catch handler.140* Used at the call site to determine which catch() block should execute.141* This is found in phase 1 of unwinding then installed in phase 2.142*/143int handlerSwitchValue;144/**145* The action record for the catch. This is cached during phase 1146* unwinding.147*/148const char *actionRecord;149/**150* Pointer to the language-specific data area (LSDA) for the handler151* frame. This is unused in this implementation, but set for ABI152* compatibility in case we want to mix code in very weird ways.153*/154const char *languageSpecificData;155/** The cached landing pad for the catch handler.*/156void *catchTemp;157/**158* The pointer that will be returned as the pointer to the object. When159* throwing a class and catching a virtual superclass (for example), we160* need to adjust the thrown pointer to make it all work correctly.161*/162void *adjustedPtr;163#ifndef __LP64__164/**165* Reference count. Used to support the C++11 exception_ptr class. This166* is prepended to the structure in 64-bit mode and squeezed in to the167* padding left before the 64-bit aligned _Unwind_Exception at the end in168* 32-bit mode.169*170* Note that it is safe to extend this structure at the beginning, rather171* than the end, because the public API for creating it returns the address172* of the end (where the exception object can be stored)173*/174uintptr_t referenceCount;175#endif176/** The language-agnostic part of the exception header. */177_Unwind_Exception unwindHeader;178};179180/**181* ABI-specified globals structure. Returned by the __cxa_get_globals()182* function and its fast variant. This is a per-thread structure - every183* thread will have one lazily allocated.184*185* This structure is defined by the ABI, so may be used outside of this186* library.187*/188struct __cxa_eh_globals189{190/**191* A linked list of exceptions that are currently caught. There may be192* several of these in nested catch() blocks.193*/194__cxa_exception *caughtExceptions;195/**196* The number of uncaught exceptions.197*/198unsigned int uncaughtExceptions;199};200/**201* ABI function returning the __cxa_eh_globals structure.202*/203__cxa_eh_globals *__cxa_get_globals(void);204/**205* Version of __cxa_get_globals() assuming that __cxa_get_globals() has already206* been called at least once by this thread.207*/208__cxa_eh_globals *__cxa_get_globals_fast(void);209210std::type_info * __cxa_current_exception_type();211212213void *__cxa_allocate_exception(size_t thrown_size) _LIBCXXRT_NOEXCEPT;214215void __cxa_free_exception(void* thrown_exception) _LIBCXXRT_NOEXCEPT;216217__cxa_exception *__cxa_init_primary_exception(218void *object, std::type_info* tinfo, void (*dest)(void *)) _LIBCXXRT_NOEXCEPT;219220/**221* Throws an exception returned by __cxa_current_primary_exception(). This222* exception may have been caught in another thread.223*/224void __cxa_rethrow_primary_exception(void* thrown_exception);225/**226* Returns the current exception in a form that can be stored in an227* exception_ptr object and then rethrown by a call to228* __cxa_rethrow_primary_exception().229*/230void *__cxa_current_primary_exception(void);231/**232* Increments the reference count of an exception. Called when an233* exception_ptr is copied.234*/235void __cxa_increment_exception_refcount(void* thrown_exception);236/**237* Decrements the reference count of an exception. Called when an238* exception_ptr is deleted.239*/240void __cxa_decrement_exception_refcount(void* thrown_exception);241/**242* Demangles a C++ symbol or type name. The buffer, if non-NULL, must be243* allocated with malloc() and must be *n bytes or more long. This function244* may call realloc() on the value pointed to by buf, and will return the245* length of the string via *n.246*247* The value pointed to by status is set to one of the following:248*249* 0: success250* -1: memory allocation failure251* -2: invalid mangled name252* -3: invalid arguments253*/254char* __cxa_demangle(const char* mangled_name,255char* buf,256size_t* n,257int* status);258#ifdef __cplusplus259} // extern "C"260} // namespace261262namespace abi = __cxxabiv1;263264#endif /* __cplusplus */265#endif /* __CXXABI_H_ */266267268