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)
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/>. */
27 #include "coretypes.h"
29 #include "libgcc_tm.h"
33 #ifdef __USING_SJLJ_EXCEPTIONS__
35 #ifdef DONT_USE_BUILTIN_SETJMP
39 typedef void *jmp_buf[JMP_BUF_SIZE
];
40 extern void longjmp(jmp_buf, int) __attribute__((noreturn
));
43 #define longjmp __builtin_longjmp
46 /* The setjmp side is dealt with in the except.c file. */
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. */
64 /* This is how data is returned from the personality routine to
65 the target function's handler. */
68 /* These are filled in once by the target function before any
69 exceptions are expected to be handled. */
70 _Unwind_Personality_Fn personality
;
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
));
84 struct _Unwind_Context
86 struct SjLj_Function_Context
*fc
;
91 _Unwind_Personality_Fn personality
;
95 /* Manage the chain of registered function contexts. */
97 /* Single threaded fallback chain. */
98 static struct SjLj_Function_Context
*fc_static
;
101 static __gthread_key_t fc_key
;
102 static int use_fc_key
= -1;
107 use_fc_key
= __gthread_key_create (&fc_key
, 0) == 0;
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)
120 _Unwind_SjLj_Register (struct SjLj_Function_Context
*fc
)
128 fc
->prev
= __gthread_getspecific (fc_key
);
129 __gthread_setspecific (fc_key
, fc
);
134 fc
->prev
= fc_static
;
139 static inline struct SjLj_Function_Context
*
140 _Unwind_SjLj_GetContext (void)
147 return __gthread_getspecific (fc_key
);
153 _Unwind_SjLj_SetContext (struct SjLj_Function_Context
*fc
)
160 __gthread_setspecific (fc_key
, fc
);
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. */
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. */
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];
198 /* Otherwise we're out of luck for now. */
199 return (_Unwind_Word
) 0;
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. */
211 _Unwind_GetIP (struct _Unwind_Context
*context
)
213 return context
->fc
->call_site
+ 1;
217 _Unwind_GetIPInfo (struct _Unwind_Context
*context
, int *ip_before_insn
)
220 if (context
->fc
!= NULL
)
221 return context
->fc
->call_site
+ 1;
226 /* Set the return landing pad index in CONTEXT. */
229 _Unwind_SetIP (struct _Unwind_Context
*context
, _Unwind_Ptr val
)
231 context
->fc
->call_site
= val
- 1;
235 _Unwind_GetLanguageSpecificData (struct _Unwind_Context
*context
)
237 return context
->fc
->lsda
;
241 _Unwind_GetRegionStart (struct _Unwind_Context
*context
__attribute__((unused
)) )
247 _Unwind_FindEnclosingFunction (void *pc
__attribute__((unused
)))
254 _Unwind_GetDataRelBase (struct _Unwind_Context
*context
__attribute__((unused
)) )
260 _Unwind_GetTextRelBase (struct _Unwind_Context
*context
__attribute__((unused
)) )
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
;
276 fs
->personality
= context
->fc
->personality
;
277 return _URC_NO_REASON
;
282 uw_update_context (struct _Unwind_Context
*context
,
283 _Unwind_FrameState
*fs
__attribute__((unused
)) )
285 context
->fc
= context
->fc
->prev
;
289 uw_advance_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
291 _Unwind_SjLj_Unregister (context
->fc
);
292 uw_update_context (context
, fs
);
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 */