unwind.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* Exception handling and frame unwind runtime interface routines.
  2. Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /* This is derived from the C++ ABI for IA-64. Where we diverge
  20. for cross-architecture compatibility are noted with "@@@". */
  21. #ifndef _UNWIND_H
  22. #define _UNWIND_H
  23. #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
  24. /* Only for _GCC_specific_handler. */
  25. #include <windows.h>
  26. #endif
  27. #ifndef HIDE_EXPORTS
  28. #pragma GCC visibility push(default)
  29. #endif
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* Level 1: Base ABI */
  34. /* @@@ The IA-64 ABI uses uint64 throughout. Most places this is
  35. inefficient for 32-bit and smaller machines. */
  36. typedef unsigned _Unwind_Word __attribute__((__mode__(__unwind_word__)));
  37. typedef signed _Unwind_Sword __attribute__((__mode__(__unwind_word__)));
  38. #if defined(__ia64__) && defined(__hpux__)
  39. typedef unsigned _Unwind_Ptr __attribute__((__mode__(__word__)));
  40. #else
  41. typedef unsigned _Unwind_Ptr __attribute__((__mode__(__pointer__)));
  42. #endif
  43. typedef unsigned _Unwind_Internal_Ptr __attribute__((__mode__(__pointer__)));
  44. /* @@@ The IA-64 ABI uses a 64-bit word to identify the producer and
  45. consumer of an exception. We'll go along with this for now even on
  46. 32-bit machines. We'll need to provide some other option for
  47. 16-bit machines and for machines with > 8 bits per byte. */
  48. typedef unsigned _Unwind_Exception_Class __attribute__((__mode__(__DI__)));
  49. /* The unwind interface uses reason codes in several contexts to
  50. identify the reasons for failures or other actions. */
  51. typedef enum
  52. {
  53. _URC_NO_REASON = 0,
  54. _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
  55. _URC_FATAL_PHASE2_ERROR = 2,
  56. _URC_FATAL_PHASE1_ERROR = 3,
  57. _URC_NORMAL_STOP = 4,
  58. _URC_END_OF_STACK = 5,
  59. _URC_HANDLER_FOUND = 6,
  60. _URC_INSTALL_CONTEXT = 7,
  61. _URC_CONTINUE_UNWIND = 8
  62. } _Unwind_Reason_Code;
  63. /* The unwind interface uses a pointer to an exception header object
  64. as its representation of an exception being thrown. In general, the
  65. full representation of an exception object is language- and
  66. implementation-specific, but it will be prefixed by a header
  67. understood by the unwind interface. */
  68. struct _Unwind_Exception;
  69. typedef void (*_Unwind_Exception_Cleanup_Fn) (_Unwind_Reason_Code,
  70. struct _Unwind_Exception *);
  71. struct _Unwind_Exception
  72. {
  73. _Unwind_Exception_Class exception_class;
  74. _Unwind_Exception_Cleanup_Fn exception_cleanup;
  75. #if !defined (__USING_SJLJ_EXCEPTIONS__) && defined (__SEH__)
  76. _Unwind_Word private_[6];
  77. #else
  78. _Unwind_Word private_1;
  79. _Unwind_Word private_2;
  80. #endif
  81. /* @@@ The IA-64 ABI says that this structure must be double-word aligned.
  82. Taking that literally does not make much sense generically. Instead we
  83. provide the maximum alignment required by any type for the machine. */
  84. } __attribute__((__aligned__));
  85. /* The ACTIONS argument to the personality routine is a bitwise OR of one
  86. or more of the following constants. */
  87. typedef int _Unwind_Action;
  88. #define _UA_SEARCH_PHASE 1
  89. #define _UA_CLEANUP_PHASE 2
  90. #define _UA_HANDLER_FRAME 4
  91. #define _UA_FORCE_UNWIND 8
  92. #define _UA_END_OF_STACK 16
  93. /* The target can override this macro to define any back-end-specific
  94. attributes required for the lowest-level stack frame. */
  95. #ifndef LIBGCC2_UNWIND_ATTRIBUTE
  96. #define LIBGCC2_UNWIND_ATTRIBUTE
  97. #endif
  98. /* This is an opaque type used to refer to a system-specific data
  99. structure used by the system unwinder. This context is created and
  100. destroyed by the system, and passed to the personality routine
  101. during unwinding. */
  102. struct _Unwind_Context;
  103. /* Raise an exception, passing along the given exception object. */
  104. extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
  105. _Unwind_RaiseException (struct _Unwind_Exception *);
  106. /* Raise an exception for forced unwinding. */
  107. typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
  108. (int, _Unwind_Action, _Unwind_Exception_Class,
  109. struct _Unwind_Exception *, struct _Unwind_Context *, void *);
  110. extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
  111. _Unwind_ForcedUnwind (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *);
  112. /* Helper to invoke the exception_cleanup routine. */
  113. extern void _Unwind_DeleteException (struct _Unwind_Exception *);
  114. /* Resume propagation of an existing exception. This is used after
  115. e.g. executing cleanup code, and not to implement rethrowing. */
  116. extern void LIBGCC2_UNWIND_ATTRIBUTE
  117. _Unwind_Resume (struct _Unwind_Exception *);
  118. /* @@@ Resume propagation of a FORCE_UNWIND exception, or to rethrow
  119. a normal exception that was handled. */
  120. extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
  121. _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *);
  122. /* @@@ Use unwind data to perform a stack backtrace. The trace callback
  123. is called for every stack frame in the call chain, but no cleanup
  124. actions are performed. */
  125. typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn)
  126. (struct _Unwind_Context *, void *);
  127. extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
  128. _Unwind_Backtrace (_Unwind_Trace_Fn, void *);
  129. /* These functions are used for communicating information about the unwind
  130. context (i.e. the unwind descriptors and the user register state) between
  131. the unwind library and the personality routine and landing pad. Only
  132. selected registers may be manipulated. */
  133. extern _Unwind_Word _Unwind_GetGR (struct _Unwind_Context *, int);
  134. extern void _Unwind_SetGR (struct _Unwind_Context *, int, _Unwind_Word);
  135. extern _Unwind_Ptr _Unwind_GetIP (struct _Unwind_Context *);
  136. extern _Unwind_Ptr _Unwind_GetIPInfo (struct _Unwind_Context *, int *);
  137. extern void _Unwind_SetIP (struct _Unwind_Context *, _Unwind_Ptr);
  138. /* @@@ Retrieve the CFA of the given context. */
  139. extern _Unwind_Word _Unwind_GetCFA (struct _Unwind_Context *);
  140. extern void *_Unwind_GetLanguageSpecificData (struct _Unwind_Context *);
  141. extern _Unwind_Ptr _Unwind_GetRegionStart (struct _Unwind_Context *);
  142. /* The personality routine is the function in the C++ (or other language)
  143. runtime library which serves as an interface between the system unwind
  144. library and language-specific exception handling semantics. It is
  145. specific to the code fragment described by an unwind info block, and
  146. it is always referenced via the pointer in the unwind info block, and
  147. hence it has no ABI-specified name.
  148. Note that this implies that two different C++ implementations can
  149. use different names, and have different contents in the language
  150. specific data area. Moreover, that the language specific data
  151. area contains no version info because name of the function invoked
  152. provides more effective versioning by detecting at link time the
  153. lack of code to handle the different data format. */
  154. typedef _Unwind_Reason_Code (*_Unwind_Personality_Fn)
  155. (int, _Unwind_Action, _Unwind_Exception_Class,
  156. struct _Unwind_Exception *, struct _Unwind_Context *);
  157. /* @@@ The following alternate entry points are for setjmp/longjmp
  158. based unwinding. */
  159. struct SjLj_Function_Context;
  160. extern void _Unwind_SjLj_Register (struct SjLj_Function_Context *);
  161. extern void _Unwind_SjLj_Unregister (struct SjLj_Function_Context *);
  162. extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
  163. _Unwind_SjLj_RaiseException (struct _Unwind_Exception *);
  164. extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
  165. _Unwind_SjLj_ForcedUnwind (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *);
  166. extern void LIBGCC2_UNWIND_ATTRIBUTE
  167. _Unwind_SjLj_Resume (struct _Unwind_Exception *);
  168. extern _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
  169. _Unwind_SjLj_Resume_or_Rethrow (struct _Unwind_Exception *);
  170. /* @@@ The following provide access to the base addresses for text
  171. and data-relative addressing in the LDSA. In order to stay link
  172. compatible with the standard ABI for IA-64, we inline these. */
  173. #ifdef __ia64__
  174. #include <stdlib.h>
  175. static inline _Unwind_Ptr
  176. _Unwind_GetDataRelBase (struct _Unwind_Context *_C)
  177. {
  178. /* The GP is stored in R1. */
  179. return _Unwind_GetGR (_C, 1);
  180. }
  181. static inline _Unwind_Ptr
  182. _Unwind_GetTextRelBase (struct _Unwind_Context *_C __attribute__ ((__unused__)))
  183. {
  184. abort ();
  185. return 0;
  186. }
  187. /* @@@ Retrieve the Backing Store Pointer of the given context. */
  188. extern _Unwind_Word _Unwind_GetBSP (struct _Unwind_Context *);
  189. #else
  190. extern _Unwind_Ptr _Unwind_GetDataRelBase (struct _Unwind_Context *);
  191. extern _Unwind_Ptr _Unwind_GetTextRelBase (struct _Unwind_Context *);
  192. #endif
  193. /* @@@ Given an address, return the entry point of the function that
  194. contains it. */
  195. extern void * _Unwind_FindEnclosingFunction (void *pc);
  196. #ifndef __SIZEOF_LONG__
  197. #error "__SIZEOF_LONG__ macro not defined"
  198. #endif
  199. #ifndef __SIZEOF_POINTER__
  200. #error "__SIZEOF_POINTER__ macro not defined"
  201. #endif
  202. /* leb128 type numbers have a potentially unlimited size.
  203. The target of the following definitions of _sleb128_t and _uleb128_t
  204. is to have efficient data types large enough to hold the leb128 type
  205. numbers used in the unwind code.
  206. Mostly these types will simply be defined to long and unsigned long
  207. except when a unsigned long data type on the target machine is not
  208. capable of storing a pointer. */
  209. #if __SIZEOF_LONG__ >= __SIZEOF_POINTER__
  210. typedef long _sleb128_t;
  211. typedef unsigned long _uleb128_t;
  212. #elif __SIZEOF_LONG_LONG__ >= __SIZEOF_POINTER__
  213. typedef long long _sleb128_t;
  214. typedef unsigned long long _uleb128_t;
  215. #else
  216. # error "What type shall we use for _sleb128_t?"
  217. #endif
  218. #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
  219. /* Handles the mapping from SEH to GCC interfaces. */
  220. EXCEPTION_DISPOSITION _GCC_specific_handler (PEXCEPTION_RECORD, void *,
  221. PCONTEXT, PDISPATCHER_CONTEXT,
  222. _Unwind_Personality_Fn);
  223. #endif
  224. #ifdef __cplusplus
  225. }
  226. #endif
  227. #ifndef HIDE_EXPORTS
  228. #pragma GCC visibility pop
  229. #endif
  230. #endif /* unwind.h */