unwind.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*===---- unwind.h - Stack unwinding ----------------------------------------===
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. *
  21. *===-----------------------------------------------------------------------===
  22. */
  23. #ifndef __CLANG_UNWIND_H
  24. #define __CLANG_UNWIND_H
  25. #if defined(__APPLE__) && __has_include_next(<unwind.h>)
  26. /* Darwin (from 11.x on) provide an unwind.h. If that's available,
  27. * use it. libunwind wraps some of its definitions in #ifdef _GNU_SOURCE,
  28. * so define that around the include.*/
  29. # ifndef _GNU_SOURCE
  30. # define _SHOULD_UNDEFINE_GNU_SOURCE
  31. # define _GNU_SOURCE
  32. # endif
  33. // libunwind's unwind.h reflects the current visibility. However, Mozilla
  34. // builds with -fvisibility=hidden and relies on gcc's unwind.h to reset the
  35. // visibility to default and export its contents. gcc also allows users to
  36. // override its override by #defining HIDE_EXPORTS (but note, this only obeys
  37. // the user's -fvisibility setting; it doesn't hide any exports on its own). We
  38. // imitate gcc's header here:
  39. # ifdef HIDE_EXPORTS
  40. # include_next <unwind.h>
  41. # else
  42. # pragma GCC visibility push(default)
  43. # include_next <unwind.h>
  44. # pragma GCC visibility pop
  45. # endif
  46. # ifdef _SHOULD_UNDEFINE_GNU_SOURCE
  47. # undef _GNU_SOURCE
  48. # undef _SHOULD_UNDEFINE_GNU_SOURCE
  49. # endif
  50. #else
  51. #include <stdint.h>
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /* It is a bit strange for a header to play with the visibility of the
  56. symbols it declares, but this matches gcc's behavior and some programs
  57. depend on it */
  58. #ifndef HIDE_EXPORTS
  59. #pragma GCC visibility push(default)
  60. #endif
  61. typedef uintptr_t _Unwind_Word;
  62. typedef intptr_t _Unwind_Sword;
  63. typedef uintptr_t _Unwind_Ptr;
  64. typedef uintptr_t _Unwind_Internal_Ptr;
  65. typedef uint64_t _Unwind_Exception_Class;
  66. typedef intptr_t _sleb128_t;
  67. typedef uintptr_t _uleb128_t;
  68. struct _Unwind_Context;
  69. struct _Unwind_Exception;
  70. typedef enum {
  71. _URC_NO_REASON = 0,
  72. _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
  73. _URC_FATAL_PHASE2_ERROR = 2,
  74. _URC_FATAL_PHASE1_ERROR = 3,
  75. _URC_NORMAL_STOP = 4,
  76. _URC_END_OF_STACK = 5,
  77. _URC_HANDLER_FOUND = 6,
  78. _URC_INSTALL_CONTEXT = 7,
  79. _URC_CONTINUE_UNWIND = 8
  80. } _Unwind_Reason_Code;
  81. typedef enum {
  82. _UA_SEARCH_PHASE = 1,
  83. _UA_CLEANUP_PHASE = 2,
  84. _UA_HANDLER_FRAME = 4,
  85. _UA_FORCE_UNWIND = 8,
  86. _UA_END_OF_STACK = 16 /* gcc extension to C++ ABI */
  87. } _Unwind_Action;
  88. typedef void (*_Unwind_Exception_Cleanup_Fn)(_Unwind_Reason_Code,
  89. struct _Unwind_Exception *);
  90. struct _Unwind_Exception {
  91. _Unwind_Exception_Class exception_class;
  92. _Unwind_Exception_Cleanup_Fn exception_cleanup;
  93. _Unwind_Word private_1;
  94. _Unwind_Word private_2;
  95. /* The Itanium ABI requires that _Unwind_Exception objects are "double-word
  96. * aligned". GCC has interpreted this to mean "use the maximum useful
  97. * alignment for the target"; so do we. */
  98. } __attribute__((__aligned__));
  99. typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)(int, _Unwind_Action,
  100. _Unwind_Exception_Class,
  101. struct _Unwind_Exception *,
  102. struct _Unwind_Context *,
  103. void *);
  104. typedef _Unwind_Reason_Code (*_Unwind_Personality_Fn)(
  105. int, _Unwind_Action, _Unwind_Exception_Class, struct _Unwind_Exception *,
  106. struct _Unwind_Context *);
  107. typedef _Unwind_Personality_Fn __personality_routine;
  108. typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn)(struct _Unwind_Context *,
  109. void *);
  110. #if defined(__arm__) && !defined(__APPLE__)
  111. typedef enum {
  112. _UVRSC_CORE = 0, /* integer register */
  113. _UVRSC_VFP = 1, /* vfp */
  114. _UVRSC_WMMXD = 3, /* Intel WMMX data register */
  115. _UVRSC_WMMXC = 4 /* Intel WMMX control register */
  116. } _Unwind_VRS_RegClass;
  117. typedef enum {
  118. _UVRSD_UINT32 = 0,
  119. _UVRSD_VFPX = 1,
  120. _UVRSD_UINT64 = 3,
  121. _UVRSD_FLOAT = 4,
  122. _UVRSD_DOUBLE = 5
  123. } _Unwind_VRS_DataRepresentation;
  124. typedef enum {
  125. _UVRSR_OK = 0,
  126. _UVRSR_NOT_IMPLEMENTED = 1,
  127. _UVRSR_FAILED = 2
  128. } _Unwind_VRS_Result;
  129. _Unwind_VRS_Result _Unwind_VRS_Get(struct _Unwind_Context *__context,
  130. _Unwind_VRS_RegClass __regclass,
  131. uint32_t __regno,
  132. _Unwind_VRS_DataRepresentation __representation,
  133. void *__valuep);
  134. _Unwind_VRS_Result _Unwind_VRS_Set(struct _Unwind_Context *__context,
  135. _Unwind_VRS_RegClass __regclass,
  136. uint32_t __regno,
  137. _Unwind_VRS_DataRepresentation __representation,
  138. void *__valuep);
  139. static __inline__
  140. _Unwind_Word _Unwind_GetGR(struct _Unwind_Context *__context, int __index) {
  141. _Unwind_Word __value;
  142. _Unwind_VRS_Get(__context, _UVRSC_CORE, __index, _UVRSD_UINT32, &__value);
  143. return __value;
  144. }
  145. static __inline__
  146. void _Unwind_SetGR(struct _Unwind_Context *__context, int __index,
  147. _Unwind_Word __value) {
  148. _Unwind_VRS_Set(__context, _UVRSC_CORE, __index, _UVRSD_UINT32, &__value);
  149. }
  150. static __inline__
  151. _Unwind_Word _Unwind_GetIP(struct _Unwind_Context *__context) {
  152. _Unwind_Word __ip = _Unwind_GetGR(__context, 15);
  153. return __ip & ~(_Unwind_Word)(0x1); /* Remove thumb mode bit. */
  154. }
  155. static __inline__
  156. void _Unwind_SetIP(struct _Unwind_Context *__context, _Unwind_Word __value) {
  157. _Unwind_Word __thumb_mode_bit = _Unwind_GetGR(__context, 15) & 0x1;
  158. _Unwind_SetGR(__context, 15, __value | __thumb_mode_bit);
  159. }
  160. #else
  161. _Unwind_Word _Unwind_GetGR(struct _Unwind_Context *, int);
  162. void _Unwind_SetGR(struct _Unwind_Context *, int, _Unwind_Word);
  163. _Unwind_Word _Unwind_GetIP(struct _Unwind_Context *);
  164. void _Unwind_SetIP(struct _Unwind_Context *, _Unwind_Word);
  165. #endif
  166. _Unwind_Word _Unwind_GetIPInfo(struct _Unwind_Context *, int *);
  167. _Unwind_Word _Unwind_GetCFA(struct _Unwind_Context *);
  168. _Unwind_Word _Unwind_GetBSP(struct _Unwind_Context *);
  169. void *_Unwind_GetLanguageSpecificData(struct _Unwind_Context *);
  170. _Unwind_Ptr _Unwind_GetRegionStart(struct _Unwind_Context *);
  171. /* DWARF EH functions; currently not available on Darwin/ARM */
  172. #if !defined(__APPLE__) || !defined(__arm__)
  173. _Unwind_Reason_Code _Unwind_RaiseException(struct _Unwind_Exception *);
  174. _Unwind_Reason_Code _Unwind_ForcedUnwind(struct _Unwind_Exception *,
  175. _Unwind_Stop_Fn, void *);
  176. void _Unwind_DeleteException(struct _Unwind_Exception *);
  177. void _Unwind_Resume(struct _Unwind_Exception *);
  178. _Unwind_Reason_Code _Unwind_Resume_or_Rethrow(struct _Unwind_Exception *);
  179. #endif
  180. _Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
  181. /* setjmp(3)/longjmp(3) stuff */
  182. typedef struct SjLj_Function_Context *_Unwind_FunctionContext_t;
  183. void _Unwind_SjLj_Register(_Unwind_FunctionContext_t);
  184. void _Unwind_SjLj_Unregister(_Unwind_FunctionContext_t);
  185. _Unwind_Reason_Code _Unwind_SjLj_RaiseException(struct _Unwind_Exception *);
  186. _Unwind_Reason_Code _Unwind_SjLj_ForcedUnwind(struct _Unwind_Exception *,
  187. _Unwind_Stop_Fn, void *);
  188. void _Unwind_SjLj_Resume(struct _Unwind_Exception *);
  189. _Unwind_Reason_Code _Unwind_SjLj_Resume_or_Rethrow(struct _Unwind_Exception *);
  190. void *_Unwind_FindEnclosingFunction(void *);
  191. #ifdef __APPLE__
  192. _Unwind_Ptr _Unwind_GetDataRelBase(struct _Unwind_Context *)
  193. __attribute__((__unavailable__));
  194. _Unwind_Ptr _Unwind_GetTextRelBase(struct _Unwind_Context *)
  195. __attribute__((__unavailable__));
  196. /* Darwin-specific functions */
  197. void __register_frame(const void *);
  198. void __deregister_frame(const void *);
  199. struct dwarf_eh_bases {
  200. uintptr_t tbase;
  201. uintptr_t dbase;
  202. uintptr_t func;
  203. };
  204. void *_Unwind_Find_FDE(const void *, struct dwarf_eh_bases *);
  205. void __register_frame_info_bases(const void *, void *, void *, void *)
  206. __attribute__((__unavailable__));
  207. void __register_frame_info(const void *, void *) __attribute__((__unavailable__));
  208. void __register_frame_info_table_bases(const void *, void*, void *, void *)
  209. __attribute__((__unavailable__));
  210. void __register_frame_info_table(const void *, void *)
  211. __attribute__((__unavailable__));
  212. void __register_frame_table(const void *) __attribute__((__unavailable__));
  213. void __deregister_frame_info(const void *) __attribute__((__unavailable__));
  214. void __deregister_frame_info_bases(const void *)__attribute__((__unavailable__));
  215. #else
  216. _Unwind_Ptr _Unwind_GetDataRelBase(struct _Unwind_Context *);
  217. _Unwind_Ptr _Unwind_GetTextRelBase(struct _Unwind_Context *);
  218. #endif
  219. #ifndef HIDE_EXPORTS
  220. #pragma GCC visibility pop
  221. #endif
  222. #ifdef __cplusplus
  223. }
  224. #endif
  225. #endif
  226. #endif /* __CLANG_UNWIND_H */