cxxabi.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #ifndef __CXXABI_H_
  23. #define __CXXABI_H_
  24. #include <stdint.h>
  25. #include "unwind.h"
  26. namespace std
  27. {
  28. class type_info;
  29. }
  30. /*
  31. * The cxxabi.h header provides a set of public definitions for types and
  32. * functions defined by the Itanium C++ ABI specification. For reference, see
  33. * the ABI specification here:
  34. *
  35. * http://sourcery.mentor.com/public/cxx-abi/abi.html
  36. *
  37. * All deviations from this specification, unless otherwise noted, are
  38. * accidental.
  39. */
  40. #ifdef __cplusplus
  41. #if __cplusplus < 201103L
  42. #define _LIBCXXRT_NOEXCEPT noexcept
  43. #else
  44. #define _LIBCXXRT_NOEXCEPT noexcept
  45. #endif
  46. namespace __cxxabiv1 {
  47. extern "C" {
  48. #else
  49. #define _LIBCXXRT_NOEXCEPT
  50. #endif
  51. /**
  52. * Function type to call when an unexpected exception is encountered.
  53. */
  54. typedef void (*unexpected_handler)();
  55. /**
  56. * Function type to call when an unrecoverable condition is encountered.
  57. */
  58. typedef void (*terminate_handler)();
  59. /**
  60. * Structure used as a header on thrown exceptions. This is the same layout as
  61. * defined by the Itanium ABI spec, so should be interoperable with any other
  62. * implementation of this spec, such as GNU libsupc++.
  63. *
  64. * This structure is allocated when an exception is thrown. Unwinding happens
  65. * in two phases, the first looks for a handler and the second installs the
  66. * context. This structure stores a cache of the handler location between
  67. * phase 1 and phase 2. Unfortunately, cleanup information is not cached, so
  68. * must be looked up in both phases. This happens for two reasons. The first
  69. * is that we don't know how many frames containing cleanups there will be, and
  70. * we should avoid dynamic allocation during unwinding (the exception may be
  71. * reporting that we've run out of memory). The second is that finding
  72. * cleanups is much cheaper than finding handlers, because we don't have to
  73. * look at the type table at all.
  74. *
  75. * Note: Several fields of this structure have not-very-informative names.
  76. * These are taken from the ABI spec and have not been changed to make it
  77. * easier for people referring to to the spec while reading this code.
  78. */
  79. struct __cxa_exception
  80. {
  81. #ifdef __LP64__
  82. /**
  83. * Now _Unwind_Exception is marked with __attribute__((aligned)), which
  84. * implies __cxa_exception is also aligned. Insert padding in the
  85. * beginning of the struct, rather than before unwindHeader.
  86. */
  87. void *reserve;
  88. /**
  89. * Reference count. Used to support the C++11 exception_ptr class. This
  90. * is prepended to the structure in 64-bit mode and squeezed in to the
  91. * padding left before the 64-bit aligned _Unwind_Exception at the end in
  92. * 32-bit mode.
  93. *
  94. * Note that it is safe to extend this structure at the beginning, rather
  95. * than the end, because the public API for creating it returns the address
  96. * of the end (where the exception object can be stored).
  97. */
  98. uintptr_t referenceCount;
  99. #endif
  100. /** Type info for the thrown object. */
  101. std::type_info *exceptionType;
  102. /** Destructor for the object, if one exists. */
  103. void (*exceptionDestructor) (void *);
  104. /** Handler called when an exception specification is violated. */
  105. unexpected_handler unexpectedHandler;
  106. /** Hander called to terminate. */
  107. terminate_handler terminateHandler;
  108. /**
  109. * Next exception in the list. If an exception is thrown inside a catch
  110. * block and caught in a nested catch, this points to the exception that
  111. * will be handled after the inner catch block completes.
  112. */
  113. __cxa_exception *nextException;
  114. /**
  115. * The number of handlers that currently have references to this
  116. * exception. The top (non-sign) bit of this is used as a flag to indicate
  117. * that the exception is being rethrown, so should not be deleted when its
  118. * handler count reaches 0 (which it doesn't with the top bit set).
  119. */
  120. int handlerCount;
  121. #if defined(__arm__) && !defined(__ARM_DWARF_EH__)
  122. /**
  123. * The ARM EH ABI requires the unwind library to keep track of exceptions
  124. * during cleanups. These support nesting, so we need to keep a list of
  125. * them.
  126. */
  127. _Unwind_Exception *nextCleanup;
  128. /**
  129. * The number of cleanups that are currently being run on this exception.
  130. */
  131. int cleanupCount;
  132. #endif
  133. /**
  134. * The selector value to be returned when installing the catch handler.
  135. * Used at the call site to determine which catch() block should execute.
  136. * This is found in phase 1 of unwinding then installed in phase 2.
  137. */
  138. int handlerSwitchValue;
  139. /**
  140. * The action record for the catch. This is cached during phase 1
  141. * unwinding.
  142. */
  143. const char *actionRecord;
  144. /**
  145. * Pointer to the language-specific data area (LSDA) for the handler
  146. * frame. This is unused in this implementation, but set for ABI
  147. * compatibility in case we want to mix code in very weird ways.
  148. */
  149. const char *languageSpecificData;
  150. /** The cached landing pad for the catch handler.*/
  151. void *catchTemp;
  152. /**
  153. * The pointer that will be returned as the pointer to the object. When
  154. * throwing a class and catching a virtual superclass (for example), we
  155. * need to adjust the thrown pointer to make it all work correctly.
  156. */
  157. void *adjustedPtr;
  158. #ifndef __LP64__
  159. /**
  160. * Reference count. Used to support the C++11 exception_ptr class. This
  161. * is prepended to the structure in 64-bit mode and squeezed in to the
  162. * padding left before the 64-bit aligned _Unwind_Exception at the end in
  163. * 32-bit mode.
  164. *
  165. * Note that it is safe to extend this structure at the beginning, rather
  166. * than the end, because the public API for creating it returns the address
  167. * of the end (where the exception object can be stored)
  168. */
  169. uintptr_t referenceCount;
  170. #endif
  171. /** The language-agnostic part of the exception header. */
  172. _Unwind_Exception unwindHeader;
  173. };
  174. /**
  175. * ABI-specified globals structure. Returned by the __cxa_get_globals()
  176. * function and its fast variant. This is a per-thread structure - every
  177. * thread will have one lazily allocated.
  178. *
  179. * This structure is defined by the ABI, so may be used outside of this
  180. * library.
  181. */
  182. struct __cxa_eh_globals
  183. {
  184. /**
  185. * A linked list of exceptions that are currently caught. There may be
  186. * several of these in nested catch() blocks.
  187. */
  188. __cxa_exception *caughtExceptions;
  189. /**
  190. * The number of uncaught exceptions.
  191. */
  192. unsigned int uncaughtExceptions;
  193. };
  194. #define Y_CXA_EH_GLOBALS_COMPLETE
  195. /**
  196. * ABI function returning the __cxa_eh_globals structure.
  197. */
  198. __cxa_eh_globals *__cxa_get_globals(void);
  199. /**
  200. * Version of __cxa_get_globals() assuming that __cxa_get_globals() has already
  201. * been called at least once by this thread.
  202. */
  203. __cxa_eh_globals *__cxa_get_globals_fast(void);
  204. std::type_info * __cxa_current_exception_type();
  205. void *__cxa_allocate_exception(size_t thrown_size) _LIBCXXRT_NOEXCEPT;
  206. void __cxa_free_exception(void* thrown_exception) _LIBCXXRT_NOEXCEPT;
  207. __cxa_exception *__cxa_init_primary_exception(
  208. void *object, std::type_info* tinfo, void (*dest)(void *)) _LIBCXXRT_NOEXCEPT;
  209. /**
  210. * Throws an exception returned by __cxa_current_primary_exception(). This
  211. * exception may have been caught in another thread.
  212. */
  213. void __cxa_rethrow_primary_exception(void* thrown_exception);
  214. /**
  215. * Returns the current exception in a form that can be stored in an
  216. * exception_ptr object and then rethrown by a call to
  217. * __cxa_rethrow_primary_exception().
  218. */
  219. void *__cxa_current_primary_exception(void);
  220. /**
  221. * Increments the reference count of an exception. Called when an
  222. * exception_ptr is copied.
  223. */
  224. void __cxa_increment_exception_refcount(void* thrown_exception);
  225. /**
  226. * Decrements the reference count of an exception. Called when an
  227. * exception_ptr is deleted.
  228. */
  229. void __cxa_decrement_exception_refcount(void* thrown_exception);
  230. /**
  231. * Demangles a C++ symbol or type name. The buffer, if non-NULL, must be
  232. * allocated with malloc() and must be *n bytes or more long. This function
  233. * may call realloc() on the value pointed to by buf, and will return the
  234. * length of the string via *n.
  235. *
  236. * The value pointed to by status is set to one of the following:
  237. *
  238. * 0: success
  239. * -1: memory allocation failure
  240. * -2: invalid mangled name
  241. * -3: invalid arguments
  242. */
  243. char* __cxa_demangle(const char* mangled_name,
  244. char* buf,
  245. size_t* n,
  246. int* status);
  247. #ifdef _YNDX_LIBUNWIND_ENABLE_EXCEPTION_BACKTRACE
  248. size_t __cxa_collect_current_exception_backtrace(void** dest, size_t size);
  249. #endif
  250. #ifdef __cplusplus
  251. } // extern "C"
  252. } // namespace
  253. namespace abi = __cxxabiv1;
  254. #endif /* __cplusplus */
  255. #endif /* __CXXABI_H_ */