Daily bump.
[official-gcc.git] / libgcc / unwind-sjlj.c
blob59f8faa3b3a4458fe0db7a919323d3f3af37dbd9
1 /* SJLJ exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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.
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/>. */
25 #include "tconfig.h"
26 #include "tsystem.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "libgcc_tm.h"
30 #include "unwind.h"
31 #include "gthr.h"
33 #ifdef __USING_SJLJ_EXCEPTIONS__
35 #ifdef DONT_USE_BUILTIN_SETJMP
36 #ifndef inhibit_libc
37 #include <setjmp.h>
38 #else
39 typedef void *jmp_buf[JMP_BUF_SIZE];
40 extern void longjmp(jmp_buf, int) __attribute__((noreturn));
41 #endif
42 #else
43 #define longjmp __builtin_longjmp
44 #endif
46 /* The setjmp side is dealt with in the except.c file. */
47 #undef setjmp
48 #define setjmp setjmp_should_not_be_used_in_this_file
51 /* This structure is allocated on the stack of the target function.
52 This must match the definition created in except.c:init_eh. */
53 struct SjLj_Function_Context
55 /* This is the chain through all registered contexts. It is
56 filled in by _Unwind_SjLj_Register. */
57 struct SjLj_Function_Context *prev;
59 /* This is assigned in by the target function before every call
60 to the index of the call site in the lsda. It is assigned by
61 the personality routine to the landing pad index. */
62 int call_site;
64 /* This is how data is returned from the personality routine to
65 the target function's handler. */
66 _Unwind_Word data[4];
68 /* These are filled in once by the target function before any
69 exceptions are expected to be handled. */
70 _Unwind_Personality_Fn personality;
71 void *lsda;
73 #ifdef DONT_USE_BUILTIN_SETJMP
74 /* We don't know what sort of alignment requirements the system
75 jmp_buf has. We over estimated in except.c, and now we have
76 to match that here just in case the system *didn't* have more
77 restrictive requirements. */
78 jmp_buf jbuf __attribute__((aligned));
79 #else
80 void *jbuf[];
81 #endif
84 struct _Unwind_Context
86 struct SjLj_Function_Context *fc;
89 typedef struct
91 _Unwind_Personality_Fn personality;
92 } _Unwind_FrameState;
95 /* Manage the chain of registered function contexts. */
97 /* Single threaded fallback chain. */
98 static struct SjLj_Function_Context *fc_static;
100 #if __GTHREADS
101 static __gthread_key_t fc_key;
102 static int use_fc_key = -1;
104 static void
105 fc_key_init (void)
107 use_fc_key = __gthread_key_create (&fc_key, 0) == 0;
110 static void
111 fc_key_init_once (void)
113 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
114 if (__gthread_once (&once, fc_key_init) != 0 || use_fc_key < 0)
115 use_fc_key = 0;
117 #endif
119 void
120 _Unwind_SjLj_Register (struct SjLj_Function_Context *fc)
122 #if __GTHREADS
123 if (use_fc_key < 0)
124 fc_key_init_once ();
126 if (use_fc_key)
128 fc->prev = __gthread_getspecific (fc_key);
129 __gthread_setspecific (fc_key, fc);
131 else
132 #endif
134 fc->prev = fc_static;
135 fc_static = fc;
139 static inline struct SjLj_Function_Context *
140 _Unwind_SjLj_GetContext (void)
142 #if __GTHREADS
143 if (use_fc_key < 0)
144 fc_key_init_once ();
146 if (use_fc_key)
147 return __gthread_getspecific (fc_key);
148 #endif
149 return fc_static;
152 static inline void
153 _Unwind_SjLj_SetContext (struct SjLj_Function_Context *fc)
155 #if __GTHREADS
156 if (use_fc_key < 0)
157 fc_key_init_once ();
159 if (use_fc_key)
160 __gthread_setspecific (fc_key, fc);
161 else
162 #endif
163 fc_static = fc;
166 void
167 _Unwind_SjLj_Unregister (struct SjLj_Function_Context *fc)
169 _Unwind_SjLj_SetContext (fc->prev);
173 /* Get/set the return data value at INDEX in CONTEXT. */
175 _Unwind_Word
176 _Unwind_GetGR (struct _Unwind_Context *context, int index)
178 return context->fc->data[index];
181 /* Get the value of the CFA as saved in CONTEXT. */
183 _Unwind_Word
184 _Unwind_GetCFA (struct _Unwind_Context *context __attribute__((unused)))
186 /* ??? Ideally __builtin_setjmp places the CFA in the jmpbuf. */
188 #ifndef DONT_USE_BUILTIN_SETJMP
189 /* This is a crude imitation of the CFA: the saved stack pointer.
190 This is roughly the CFA of the frame before CONTEXT. When using the
191 DWARF-2 unwinder _Unwind_GetCFA returns the CFA of the frame described
192 by CONTEXT instead; but for DWARF-2 the cleanups associated with
193 CONTEXT have already been run, and for SJLJ they have not yet been. */
194 if (context->fc != NULL)
195 return (_Unwind_Word) context->fc->jbuf[2];
196 #endif
198 /* Otherwise we're out of luck for now. */
199 return (_Unwind_Word) 0;
202 void
203 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
205 context->fc->data[index] = val;
208 /* Get the call-site index as saved in CONTEXT. */
210 _Unwind_Ptr
211 _Unwind_GetIP (struct _Unwind_Context *context)
213 return context->fc->call_site + 1;
216 _Unwind_Ptr
217 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
219 *ip_before_insn = 0;
220 if (context->fc != NULL)
221 return context->fc->call_site + 1;
222 else
223 return 0;
226 /* Set the return landing pad index in CONTEXT. */
228 void
229 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
231 context->fc->call_site = val - 1;
234 void *
235 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
237 return context->fc->lsda;
240 _Unwind_Ptr
241 _Unwind_GetRegionStart (struct _Unwind_Context *context __attribute__((unused)) )
243 return 0;
246 void *
247 _Unwind_FindEnclosingFunction (void *pc __attribute__((unused)))
249 return NULL;
252 #ifndef __ia64__
253 _Unwind_Ptr
254 _Unwind_GetDataRelBase (struct _Unwind_Context *context __attribute__((unused)) )
256 return 0;
259 _Unwind_Ptr
260 _Unwind_GetTextRelBase (struct _Unwind_Context *context __attribute__((unused)) )
262 return 0;
264 #endif
266 static inline _Unwind_Reason_Code
267 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
269 if (context->fc == NULL)
271 fs->personality = NULL;
272 return _URC_END_OF_STACK;
274 else
276 fs->personality = context->fc->personality;
277 return _URC_NO_REASON;
281 static inline void
282 uw_update_context (struct _Unwind_Context *context,
283 _Unwind_FrameState *fs __attribute__((unused)) )
285 context->fc = context->fc->prev;
288 static void
289 uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
291 _Unwind_SjLj_Unregister (context->fc);
292 uw_update_context (context, fs);
295 static inline void
296 uw_init_context (struct _Unwind_Context *context)
298 context->fc = _Unwind_SjLj_GetContext ();
301 static void __attribute__((noreturn))
302 uw_install_context (struct _Unwind_Context *current __attribute__((unused)),
303 struct _Unwind_Context *target)
305 _Unwind_SjLj_SetContext (target->fc);
306 longjmp (target->fc->jbuf, 1);
309 static inline _Unwind_Ptr
310 uw_identify_context (struct _Unwind_Context *context)
312 return (_Unwind_Ptr) context->fc;
316 /* Play games with unwind symbols so that we can have call frame
317 and sjlj symbols in the same shared library. Not that you can
318 use them simultaneously... */
319 #define _Unwind_RaiseException _Unwind_SjLj_RaiseException
320 #define _Unwind_ForcedUnwind _Unwind_SjLj_ForcedUnwind
321 #define _Unwind_Resume _Unwind_SjLj_Resume
322 #define _Unwind_Resume_or_Rethrow _Unwind_SjLj_Resume_or_Rethrow
324 #include "unwind.inc"
326 #endif /* USING_SJLJ_EXCEPTIONS */