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