PR c++/29000
[official-gcc.git] / gcc / unwind-sjlj.c
blobc541bd5f8f9a1a1fd2f84402137858ffba70cfde
1 /* SJLJ exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
19 executable.)
21 GCC is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
24 License for more details.
26 You should have received a copy of the GNU General Public License
27 along with GCC; see the file COPYING. If not, write to the Free
28 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 02110-1301, USA. */
31 #include "tconfig.h"
32 #include "tsystem.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "unwind.h"
36 #include "gthr.h"
38 #ifdef __USING_SJLJ_EXCEPTIONS__
40 #ifdef DONT_USE_BUILTIN_SETJMP
41 #ifndef inhibit_libc
42 #include <setjmp.h>
43 #else
44 typedef void *jmp_buf[JMP_BUF_SIZE];
45 extern void longjmp(jmp_buf, int) __attribute__((noreturn));
46 #endif
47 #else
48 #define longjmp __builtin_longjmp
49 #endif
51 /* The setjmp side is dealt with in the except.c file. */
52 #undef setjmp
53 #define setjmp setjmp_should_not_be_used_in_this_file
56 /* This structure is allocated on the stack of the target function.
57 This must match the definition created in except.c:init_eh. */
58 struct SjLj_Function_Context
60 /* This is the chain through all registered contexts. It is
61 filled in by _Unwind_SjLj_Register. */
62 struct SjLj_Function_Context *prev;
64 /* This is assigned in by the target function before every call
65 to the index of the call site in the lsda. It is assigned by
66 the personality routine to the landing pad index. */
67 int call_site;
69 /* This is how data is returned from the personality routine to
70 the target function's handler. */
71 _Unwind_Word data[4];
73 /* These are filled in once by the target function before any
74 exceptions are expected to be handled. */
75 _Unwind_Personality_Fn personality;
76 void *lsda;
78 #ifdef DONT_USE_BUILTIN_SETJMP
79 /* We don't know what sort of alignment requirements the system
80 jmp_buf has. We over estimated in except.c, and now we have
81 to match that here just in case the system *didn't* have more
82 restrictive requirements. */
83 jmp_buf jbuf __attribute__((aligned));
84 #else
85 void *jbuf[];
86 #endif
89 struct _Unwind_Context
91 struct SjLj_Function_Context *fc;
94 typedef struct
96 _Unwind_Personality_Fn personality;
97 } _Unwind_FrameState;
100 /* Manage the chain of registered function contexts. */
102 /* Single threaded fallback chain. */
103 static struct SjLj_Function_Context *fc_static;
105 #if __GTHREADS
106 static __gthread_key_t fc_key;
107 static int use_fc_key = -1;
109 static void
110 fc_key_init (void)
112 use_fc_key = __gthread_key_create (&fc_key, 0) == 0;
115 static void
116 fc_key_init_once (void)
118 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
119 if (__gthread_once (&once, fc_key_init) != 0 || use_fc_key < 0)
120 use_fc_key = 0;
122 #endif
124 void
125 _Unwind_SjLj_Register (struct SjLj_Function_Context *fc)
127 #if __GTHREADS
128 if (use_fc_key < 0)
129 fc_key_init_once ();
131 if (use_fc_key)
133 fc->prev = __gthread_getspecific (fc_key);
134 __gthread_setspecific (fc_key, fc);
136 else
137 #endif
139 fc->prev = fc_static;
140 fc_static = fc;
144 static inline struct SjLj_Function_Context *
145 _Unwind_SjLj_GetContext (void)
147 #if __GTHREADS
148 if (use_fc_key < 0)
149 fc_key_init_once ();
151 if (use_fc_key)
152 return __gthread_getspecific (fc_key);
153 #endif
154 return fc_static;
157 static inline void
158 _Unwind_SjLj_SetContext (struct SjLj_Function_Context *fc)
160 #if __GTHREADS
161 if (use_fc_key < 0)
162 fc_key_init_once ();
164 if (use_fc_key)
165 __gthread_setspecific (fc_key, fc);
166 else
167 #endif
168 fc_static = fc;
171 void
172 _Unwind_SjLj_Unregister (struct SjLj_Function_Context *fc)
174 _Unwind_SjLj_SetContext (fc->prev);
178 /* Get/set the return data value at INDEX in CONTEXT. */
180 _Unwind_Word
181 _Unwind_GetGR (struct _Unwind_Context *context, int index)
183 return context->fc->data[index];
186 /* Get the value of the CFA as saved in CONTEXT. */
188 _Unwind_Word
189 _Unwind_GetCFA (struct _Unwind_Context *context __attribute__((unused)))
191 /* ??? Ideally __builtin_setjmp places the CFA in the jmpbuf. */
193 #ifndef DONT_USE_BUILTIN_SETJMP
194 /* This is a crude imitation of the CFA: the saved stack pointer.
195 This is roughly the CFA of the frame before CONTEXT. When using the
196 DWARF-2 unwinder _Unwind_GetCFA returns the CFA of the frame described
197 by CONTEXT instead; but for DWARF-2 the cleanups associated with
198 CONTEXT have already been run, and for SJLJ they have not yet been. */
199 if (context->fc != NULL)
200 return (_Unwind_Word) context->fc->jbuf[2];
201 #endif
203 /* Otherwise we're out of luck for now. */
204 return (_Unwind_Word) 0;
207 void
208 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
210 context->fc->data[index] = val;
213 /* Get the call-site index as saved in CONTEXT. */
215 _Unwind_Ptr
216 _Unwind_GetIP (struct _Unwind_Context *context)
218 return context->fc->call_site + 1;
221 _Unwind_Ptr
222 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
224 *ip_before_insn = 0;
225 if (context->fc != NULL)
226 return context->fc->call_site + 1;
227 else
228 return 0;
231 /* Set the return landing pad index in CONTEXT. */
233 void
234 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
236 context->fc->call_site = val - 1;
239 void *
240 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
242 return context->fc->lsda;
245 _Unwind_Ptr
246 _Unwind_GetRegionStart (struct _Unwind_Context *context __attribute__((unused)) )
248 return 0;
251 void *
252 _Unwind_FindEnclosingFunction (void *pc __attribute__((unused)))
254 return NULL;
257 #ifndef __ia64__
258 _Unwind_Ptr
259 _Unwind_GetDataRelBase (struct _Unwind_Context *context __attribute__((unused)) )
261 return 0;
264 _Unwind_Ptr
265 _Unwind_GetTextRelBase (struct _Unwind_Context *context __attribute__((unused)) )
267 return 0;
269 #endif
271 static inline _Unwind_Reason_Code
272 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
274 if (context->fc == NULL)
276 fs->personality = NULL;
277 return _URC_END_OF_STACK;
279 else
281 fs->personality = context->fc->personality;
282 return _URC_NO_REASON;
286 static inline void
287 uw_update_context (struct _Unwind_Context *context,
288 _Unwind_FrameState *fs __attribute__((unused)) )
290 context->fc = context->fc->prev;
293 static void
294 uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
296 _Unwind_SjLj_Unregister (context->fc);
297 uw_update_context (context, fs);
300 static inline void
301 uw_init_context (struct _Unwind_Context *context)
303 context->fc = _Unwind_SjLj_GetContext ();
306 static void __attribute__((noreturn))
307 uw_install_context (struct _Unwind_Context *current __attribute__((unused)),
308 struct _Unwind_Context *target)
310 _Unwind_SjLj_SetContext (target->fc);
311 longjmp (target->fc->jbuf, 1);
314 static inline _Unwind_Ptr
315 uw_identify_context (struct _Unwind_Context *context)
317 return (_Unwind_Ptr) context->fc;
321 /* Play games with unwind symbols so that we can have call frame
322 and sjlj symbols in the same shared library. Not that you can
323 use them simultaneously... */
324 #define _Unwind_RaiseException _Unwind_SjLj_RaiseException
325 #define _Unwind_ForcedUnwind _Unwind_SjLj_ForcedUnwind
326 #define _Unwind_Resume _Unwind_SjLj_Resume
327 #define _Unwind_Resume_or_Rethrow _Unwind_SjLj_Resume_or_Rethrow
329 #include "unwind.inc"
331 #endif /* USING_SJLJ_EXCEPTIONS */