1 /* SJLJ exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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)
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
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
33 #include "coretypes.h"
38 #ifdef __USING_SJLJ_EXCEPTIONS__
40 #ifdef DONT_USE_BUILTIN_SETJMP
44 typedef void *jmp_buf[JMP_BUF_SIZE
];
45 extern void longjmp(jmp_buf, int) __attribute__((noreturn
));
48 #define setjmp __builtin_setjmp
49 #define longjmp __builtin_longjmp
52 /* This structure is allocated on the stack of the target function.
53 This must match the definition created in except.c:init_eh. */
54 struct SjLj_Function_Context
56 /* This is the chain through all registered contexts. It is
57 filled in by _Unwind_SjLj_Register. */
58 struct SjLj_Function_Context
*prev
;
60 /* This is assigned in by the target function before every call
61 to the index of the call site in the lsda. It is assigned by
62 the personality routine to the landing pad index. */
65 /* This is how data is returned from the personality routine to
66 the target function's handler. */
69 /* These are filled in once by the target function before any
70 exceptions are expected to be handled. */
71 _Unwind_Personality_Fn personality
;
74 #ifdef DONT_USE_BUILTIN_SETJMP
75 /* We don't know what sort of alignment requirements the system
76 jmp_buf has. We over estimated in except.c, and now we have
77 to match that here just in case the system *didn't* have more
78 restrictive requirements. */
79 jmp_buf jbuf
__attribute__((aligned
));
85 struct _Unwind_Context
87 struct SjLj_Function_Context
*fc
;
92 _Unwind_Personality_Fn personality
;
96 /* Manage the chain of registered function contexts. */
98 /* Single threaded fallback chain. */
99 static struct SjLj_Function_Context
*fc_static
;
102 static __gthread_key_t fc_key
;
103 static int use_fc_key
= -1;
108 use_fc_key
= __gthread_key_create (&fc_key
, 0) == 0;
112 fc_key_init_once (void)
114 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
115 if (__gthread_once (&once
, fc_key_init
) != 0 || use_fc_key
< 0)
121 _Unwind_SjLj_Register (struct SjLj_Function_Context
*fc
)
129 fc
->prev
= __gthread_getspecific (fc_key
);
130 __gthread_setspecific (fc_key
, fc
);
135 fc
->prev
= fc_static
;
140 static inline struct SjLj_Function_Context
*
141 _Unwind_SjLj_GetContext (void)
148 return __gthread_getspecific (fc_key
);
154 _Unwind_SjLj_SetContext (struct SjLj_Function_Context
*fc
)
161 __gthread_setspecific (fc_key
, fc
);
168 _Unwind_SjLj_Unregister (struct SjLj_Function_Context
*fc
)
170 _Unwind_SjLj_SetContext (fc
->prev
);
174 /* Get/set the return data value at INDEX in CONTEXT. */
177 _Unwind_GetGR (struct _Unwind_Context
*context
, int index
)
179 return context
->fc
->data
[index
];
182 /* Get the value of the CFA as saved in CONTEXT. */
185 _Unwind_GetCFA (struct _Unwind_Context
*context
__attribute__((unused
)))
187 /* ??? Ideally __builtin_setjmp places the CFA in the jmpbuf. */
189 #ifndef DONT_USE_BUILTIN_SETJMP
190 /* This is a crude imitation of the CFA: the saved stack pointer.
191 This is roughly the CFA of the frame before CONTEXT. When using the
192 DWARF-2 unwinder _Unwind_GetCFA returns the CFA of the frame described
193 by CONTEXT instead; but for DWARF-2 the cleanups associated with
194 CONTEXT have already been run, and for SJLJ they have not yet been. */
195 if (context
->fc
!= NULL
)
196 return (_Unwind_Word
) context
->fc
->jbuf
[2];
199 /* Otherwise we're out of luck for now. */
200 return (_Unwind_Word
) 0;
204 _Unwind_SetGR (struct _Unwind_Context
*context
, int index
, _Unwind_Word val
)
206 context
->fc
->data
[index
] = val
;
209 /* Get the call-site index as saved in CONTEXT. */
212 _Unwind_GetIP (struct _Unwind_Context
*context
)
214 return context
->fc
->call_site
+ 1;
218 _Unwind_GetIPInfo (struct _Unwind_Context
*context
, int *ip_before_insn
)
221 return context
->fc
->call_site
+ 1;
224 /* Set the return landing pad index in CONTEXT. */
227 _Unwind_SetIP (struct _Unwind_Context
*context
, _Unwind_Ptr val
)
229 context
->fc
->call_site
= val
- 1;
233 _Unwind_GetLanguageSpecificData (struct _Unwind_Context
*context
)
235 return context
->fc
->lsda
;
239 _Unwind_GetRegionStart (struct _Unwind_Context
*context
__attribute__((unused
)) )
245 _Unwind_FindEnclosingFunction (void *pc
__attribute__((unused
)))
252 _Unwind_GetDataRelBase (struct _Unwind_Context
*context
__attribute__((unused
)) )
258 _Unwind_GetTextRelBase (struct _Unwind_Context
*context
__attribute__((unused
)) )
264 static inline _Unwind_Reason_Code
265 uw_frame_state_for (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
267 if (context
->fc
== NULL
)
269 fs
->personality
= NULL
;
270 return _URC_END_OF_STACK
;
274 fs
->personality
= context
->fc
->personality
;
275 return _URC_NO_REASON
;
280 uw_update_context (struct _Unwind_Context
*context
,
281 _Unwind_FrameState
*fs
__attribute__((unused
)) )
283 context
->fc
= context
->fc
->prev
;
287 uw_advance_context (struct _Unwind_Context
*context
, _Unwind_FrameState
*fs
)
289 _Unwind_SjLj_Unregister (context
->fc
);
290 uw_update_context (context
, fs
);
294 uw_init_context (struct _Unwind_Context
*context
)
296 context
->fc
= _Unwind_SjLj_GetContext ();
299 static void __attribute__((noreturn
))
300 uw_install_context (struct _Unwind_Context
*current
__attribute__((unused
)),
301 struct _Unwind_Context
*target
)
303 _Unwind_SjLj_SetContext (target
->fc
);
304 longjmp (target
->fc
->jbuf
, 1);
307 static inline _Unwind_Ptr
308 uw_identify_context (struct _Unwind_Context
*context
)
310 return (_Unwind_Ptr
) context
->fc
;
314 /* Play games with unwind symbols so that we can have call frame
315 and sjlj symbols in the same shared library. Not that you can
316 use them simultaneously... */
317 #define _Unwind_RaiseException _Unwind_SjLj_RaiseException
318 #define _Unwind_ForcedUnwind _Unwind_SjLj_ForcedUnwind
319 #define _Unwind_Resume _Unwind_SjLj_Resume
320 #define _Unwind_Resume_or_Rethrow _Unwind_SjLj_Resume_or_Rethrow
322 #include "unwind.inc"
324 #endif /* USING_SJLJ_EXCEPTIONS */