unwind.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. /* See "Data Definitions for libgcc_s" in the Linux Standard Base.*/
  24. #if defined(__APPLE__) && __has_include_next(<unwind.h>)
  25. /* Darwin (from 11.x on) provide an unwind.h. If that's available,
  26. * use it. libunwind wraps some of its definitions in #ifdef _GNU_SOURCE,
  27. * so define that around the include.*/
  28. # ifndef _GNU_SOURCE
  29. # define _SHOULD_UNDEFINE_GNU_SOURCE
  30. # define _GNU_SOURCE
  31. # endif
  32. // libunwind's unwind.h reflects the current visibility. However, Mozilla
  33. // builds with -fvisibility=hidden and relies on gcc's unwind.h to reset the
  34. // visibility to default and export its contents. gcc also allows users to
  35. // override its override by #defining HIDE_EXPORTS (but note, this only obeys
  36. // the user's -fvisibility setting; it doesn't hide any exports on its own). We
  37. // imitate gcc's header here:
  38. # ifdef HIDE_EXPORTS
  39. # include_next <unwind.h>
  40. # else
  41. # pragma GCC visibility push(default)
  42. # include_next <unwind.h>
  43. # pragma GCC visibility pop
  44. # endif
  45. # ifdef _SHOULD_UNDEFINE_GNU_SOURCE
  46. # undef _GNU_SOURCE
  47. # undef _SHOULD_UNDEFINE_GNU_SOURCE
  48. # endif
  49. #else
  50. #ifndef __CLANG_UNWIND_H
  51. #define __CLANG_UNWIND_H
  52. #include <stdint.h>
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. /* It is a bit strange for a header to play with the visibility of the
  57. symbols it declares, but this matches gcc's behavior and some programs
  58. depend on it */
  59. #ifndef HIDE_EXPORTS
  60. #pragma GCC visibility push(default)
  61. #endif
  62. typedef uintptr_t _Unwind_Word;
  63. typedef intptr_t _Unwind_Sword;
  64. typedef uintptr_t _Unwind_Ptr;
  65. typedef uintptr_t _Unwind_Internal_Ptr;
  66. typedef uint64_t _Unwind_Exception_Class;
  67. typedef intptr_t _sleb128_t;
  68. typedef uintptr_t _uleb128_t;
  69. struct _Unwind_Context;
  70. #if defined(__arm__) && !(defined(__USING_SJLJ_EXCEPTIONS__) || defined(__ARM_DWARF_EH__))
  71. struct _Unwind_Control_Block;
  72. typedef struct _Unwind_Control_Block _Unwind_Exception; /* Alias */
  73. #else
  74. struct _Unwind_Exception;
  75. typedef struct _Unwind_Exception _Unwind_Exception;
  76. #endif
  77. typedef enum {
  78. _URC_NO_REASON = 0,
  79. #if defined(__arm__) && !defined(__USING_SJLJ_EXCEPTIONS__) && \
  80. !defined(__ARM_DWARF_EH__)
  81. _URC_OK = 0, /* used by ARM EHABI */
  82. #endif
  83. _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
  84. _URC_FATAL_PHASE2_ERROR = 2,
  85. _URC_FATAL_PHASE1_ERROR = 3,
  86. _URC_NORMAL_STOP = 4,
  87. _URC_END_OF_STACK = 5,
  88. _URC_HANDLER_FOUND = 6,
  89. _URC_INSTALL_CONTEXT = 7,
  90. _URC_CONTINUE_UNWIND = 8,
  91. #if defined(__arm__) && !defined(__USING_SJLJ_EXCEPTIONS__) && \
  92. !defined(__ARM_DWARF_EH__)
  93. _URC_FAILURE = 9 /* used by ARM EHABI */
  94. #endif
  95. } _Unwind_Reason_Code;
  96. typedef enum {
  97. _UA_SEARCH_PHASE = 1,
  98. _UA_CLEANUP_PHASE = 2,
  99. _UA_HANDLER_FRAME = 4,
  100. _UA_FORCE_UNWIND = 8,
  101. _UA_END_OF_STACK = 16 /* gcc extension to C++ ABI */
  102. } _Unwind_Action;
  103. typedef void (*_Unwind_Exception_Cleanup_Fn)(_Unwind_Reason_Code,
  104. _Unwind_Exception *);
  105. #if defined(__arm__) && !(defined(__USING_SJLJ_EXCEPTIONS__) || defined(__ARM_DWARF_EH__))
  106. typedef struct _Unwind_Control_Block _Unwind_Control_Block;
  107. typedef uint32_t _Unwind_EHT_Header;
  108. struct _Unwind_Control_Block {
  109. uint64_t exception_class;
  110. void (*exception_cleanup)(_Unwind_Reason_Code, _Unwind_Control_Block *);
  111. /* unwinder cache (private fields for the unwinder's use) */
  112. struct {
  113. uint32_t reserved1; /* forced unwind stop function, 0 if not forced */
  114. uint32_t reserved2; /* personality routine */
  115. uint32_t reserved3; /* callsite */
  116. uint32_t reserved4; /* forced unwind stop argument */
  117. uint32_t reserved5;
  118. } unwinder_cache;
  119. /* propagation barrier cache (valid after phase 1) */
  120. struct {
  121. uint32_t sp;
  122. uint32_t bitpattern[5];
  123. } barrier_cache;
  124. /* cleanup cache (preserved over cleanup) */
  125. struct {
  126. uint32_t bitpattern[4];
  127. } cleanup_cache;
  128. /* personality cache (for personality's benefit) */
  129. struct {
  130. uint32_t fnstart; /* function start address */
  131. _Unwind_EHT_Header *ehtp; /* pointer to EHT entry header word */
  132. uint32_t additional; /* additional data */
  133. uint32_t reserved1;
  134. } pr_cache;
  135. long long int : 0; /* force alignment of next item to 8-byte boundary */
  136. } __attribute__((__aligned__(8)));
  137. #else
  138. struct _Unwind_Exception {
  139. _Unwind_Exception_Class exception_class;
  140. _Unwind_Exception_Cleanup_Fn exception_cleanup;
  141. _Unwind_Word private_1;
  142. _Unwind_Word private_2;
  143. /* The Itanium ABI requires that _Unwind_Exception objects are "double-word
  144. * aligned". GCC has interpreted this to mean "use the maximum useful
  145. * alignment for the target"; so do we. */
  146. } __attribute__((__aligned__));
  147. #endif
  148. typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)(int, _Unwind_Action,
  149. _Unwind_Exception_Class,
  150. _Unwind_Exception *,
  151. struct _Unwind_Context *,
  152. void *);
  153. typedef _Unwind_Reason_Code (*_Unwind_Personality_Fn)(int, _Unwind_Action,
  154. _Unwind_Exception_Class,
  155. _Unwind_Exception *,
  156. struct _Unwind_Context *);
  157. typedef _Unwind_Personality_Fn __personality_routine;
  158. typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn)(struct _Unwind_Context *,
  159. void *);
  160. #if defined(__arm__) && !(defined(__USING_SJLJ_EXCEPTIONS__) || defined(__ARM_DWARF_EH__))
  161. typedef enum {
  162. _UVRSC_CORE = 0, /* integer register */
  163. _UVRSC_VFP = 1, /* vfp */
  164. _UVRSC_WMMXD = 3, /* Intel WMMX data register */
  165. _UVRSC_WMMXC = 4 /* Intel WMMX control register */
  166. } _Unwind_VRS_RegClass;
  167. typedef enum {
  168. _UVRSD_UINT32 = 0,
  169. _UVRSD_VFPX = 1,
  170. _UVRSD_UINT64 = 3,
  171. _UVRSD_FLOAT = 4,
  172. _UVRSD_DOUBLE = 5
  173. } _Unwind_VRS_DataRepresentation;
  174. typedef enum {
  175. _UVRSR_OK = 0,
  176. _UVRSR_NOT_IMPLEMENTED = 1,
  177. _UVRSR_FAILED = 2
  178. } _Unwind_VRS_Result;
  179. typedef uint32_t _Unwind_State;
  180. #define _US_VIRTUAL_UNWIND_FRAME ((_Unwind_State)0)
  181. #define _US_UNWIND_FRAME_STARTING ((_Unwind_State)1)
  182. #define _US_UNWIND_FRAME_RESUME ((_Unwind_State)2)
  183. #define _US_ACTION_MASK ((_Unwind_State)3)
  184. #define _US_FORCE_UNWIND ((_Unwind_State)8)
  185. _Unwind_VRS_Result _Unwind_VRS_Get(struct _Unwind_Context *__context,
  186. _Unwind_VRS_RegClass __regclass,
  187. uint32_t __regno,
  188. _Unwind_VRS_DataRepresentation __representation,
  189. void *__valuep);
  190. _Unwind_VRS_Result _Unwind_VRS_Set(struct _Unwind_Context *__context,
  191. _Unwind_VRS_RegClass __regclass,
  192. uint32_t __regno,
  193. _Unwind_VRS_DataRepresentation __representation,
  194. void *__valuep);
  195. static __inline__
  196. _Unwind_Word _Unwind_GetGR(struct _Unwind_Context *__context, int __index) {
  197. _Unwind_Word __value;
  198. _Unwind_VRS_Get(__context, _UVRSC_CORE, __index, _UVRSD_UINT32, &__value);
  199. return __value;
  200. }
  201. static __inline__
  202. void _Unwind_SetGR(struct _Unwind_Context *__context, int __index,
  203. _Unwind_Word __value) {
  204. _Unwind_VRS_Set(__context, _UVRSC_CORE, __index, _UVRSD_UINT32, &__value);
  205. }
  206. static __inline__
  207. _Unwind_Word _Unwind_GetIP(struct _Unwind_Context *__context) {
  208. _Unwind_Word __ip = _Unwind_GetGR(__context, 15);
  209. return __ip & ~(_Unwind_Word)(0x1); /* Remove thumb mode bit. */
  210. }
  211. static __inline__
  212. void _Unwind_SetIP(struct _Unwind_Context *__context, _Unwind_Word __value) {
  213. _Unwind_Word __thumb_mode_bit = _Unwind_GetGR(__context, 15) & 0x1;
  214. _Unwind_SetGR(__context, 15, __value | __thumb_mode_bit);
  215. }
  216. #else
  217. _Unwind_Word _Unwind_GetGR(struct _Unwind_Context *, int);
  218. void _Unwind_SetGR(struct _Unwind_Context *, int, _Unwind_Word);
  219. _Unwind_Word _Unwind_GetIP(struct _Unwind_Context *);
  220. void _Unwind_SetIP(struct _Unwind_Context *, _Unwind_Word);
  221. #endif
  222. _Unwind_Word _Unwind_GetIPInfo(struct _Unwind_Context *, int *);
  223. _Unwind_Word _Unwind_GetCFA(struct _Unwind_Context *);
  224. _Unwind_Word _Unwind_GetBSP(struct _Unwind_Context *);
  225. void *_Unwind_GetLanguageSpecificData(struct _Unwind_Context *);
  226. _Unwind_Ptr _Unwind_GetRegionStart(struct _Unwind_Context *);
  227. /* DWARF EH functions; currently not available on Darwin/ARM */
  228. #if !defined(__APPLE__) || !defined(__arm__)
  229. _Unwind_Reason_Code _Unwind_RaiseException(_Unwind_Exception *);
  230. _Unwind_Reason_Code _Unwind_ForcedUnwind(_Unwind_Exception *, _Unwind_Stop_Fn,
  231. void *);
  232. void _Unwind_DeleteException(_Unwind_Exception *);
  233. void _Unwind_Resume(_Unwind_Exception *);
  234. _Unwind_Reason_Code _Unwind_Resume_or_Rethrow(_Unwind_Exception *);
  235. #endif
  236. _Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
  237. /* setjmp(3)/longjmp(3) stuff */
  238. typedef struct SjLj_Function_Context *_Unwind_FunctionContext_t;
  239. void _Unwind_SjLj_Register(_Unwind_FunctionContext_t);
  240. void _Unwind_SjLj_Unregister(_Unwind_FunctionContext_t);
  241. _Unwind_Reason_Code _Unwind_SjLj_RaiseException(_Unwind_Exception *);
  242. _Unwind_Reason_Code _Unwind_SjLj_ForcedUnwind(_Unwind_Exception *,
  243. _Unwind_Stop_Fn, void *);
  244. void _Unwind_SjLj_Resume(_Unwind_Exception *);
  245. _Unwind_Reason_Code _Unwind_SjLj_Resume_or_Rethrow(_Unwind_Exception *);
  246. void *_Unwind_FindEnclosingFunction(void *);
  247. #ifdef __APPLE__
  248. _Unwind_Ptr _Unwind_GetDataRelBase(struct _Unwind_Context *)
  249. __attribute__((__unavailable__));
  250. _Unwind_Ptr _Unwind_GetTextRelBase(struct _Unwind_Context *)
  251. __attribute__((__unavailable__));
  252. /* Darwin-specific functions */
  253. void __register_frame(const void *);
  254. void __deregister_frame(const void *);
  255. struct dwarf_eh_bases {
  256. uintptr_t tbase;
  257. uintptr_t dbase;
  258. uintptr_t func;
  259. };
  260. void *_Unwind_Find_FDE(const void *, struct dwarf_eh_bases *);
  261. void __register_frame_info_bases(const void *, void *, void *, void *)
  262. __attribute__((__unavailable__));
  263. void __register_frame_info(const void *, void *) __attribute__((__unavailable__));
  264. void __register_frame_info_table_bases(const void *, void*, void *, void *)
  265. __attribute__((__unavailable__));
  266. void __register_frame_info_table(const void *, void *)
  267. __attribute__((__unavailable__));
  268. void __register_frame_table(const void *) __attribute__((__unavailable__));
  269. void __deregister_frame_info(const void *) __attribute__((__unavailable__));
  270. void __deregister_frame_info_bases(const void *)__attribute__((__unavailable__));
  271. #else
  272. _Unwind_Ptr _Unwind_GetDataRelBase(struct _Unwind_Context *);
  273. _Unwind_Ptr _Unwind_GetTextRelBase(struct _Unwind_Context *);
  274. #endif
  275. #ifndef HIDE_EXPORTS
  276. #pragma GCC visibility pop
  277. #endif
  278. #ifdef __cplusplus
  279. }
  280. #endif
  281. #endif /* __CLANG_UNWIND_H */
  282. #endif