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