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