]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/unwind-sjlj.c
* MAINTAINERS: Update my email address.
[thirdparty/gcc.git] / gcc / unwind-sjlj.c
CommitLineData
b2e863b8 1/* SJLJ exception handling and frame unwind runtime interface routines.
4f6c2131 2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
a01da83b 3 Free Software Foundation, Inc.
52a11cbf 4
1322177d 5 This file is part of GCC.
52a11cbf 6
1322177d
LB
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
52a11cbf
RH
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
1ee93c1b
DJ
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.)
20
1322177d
LB
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.
52a11cbf
RH
25
26 You should have received a copy of the GNU General Public License
1322177d 27 along with GCC; see the file COPYING. If not, write to the Free
366ccddb
KC
28 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 02110-1301, USA. */
52a11cbf
RH
30
31#include "tconfig.h"
32#include "tsystem.h"
4977bab6
ZW
33#include "coretypes.h"
34#include "tm.h"
52a11cbf
RH
35#include "unwind.h"
36#include "gthr.h"
37
0d24f4d1 38#ifdef __USING_SJLJ_EXCEPTIONS__
52a11cbf
RH
39
40#ifdef DONT_USE_BUILTIN_SETJMP
9defc9b7 41#ifndef inhibit_libc
52a11cbf
RH
42#include <setjmp.h>
43#else
9defc9b7
RH
44typedef void *jmp_buf[JMP_BUF_SIZE];
45extern void longjmp(jmp_buf, int) __attribute__((noreturn));
46#endif
47#else
52a11cbf
RH
48#define longjmp __builtin_longjmp
49#endif
50
4f6c2131
EB
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
54
55
52a11cbf
RH
56/* This structure is allocated on the stack of the target function.
57 This must match the definition created in except.c:init_eh. */
58struct SjLj_Function_Context
59{
60 /* This is the chain through all registered contexts. It is
61 filled in by _Unwind_SjLj_Register. */
62 struct SjLj_Function_Context *prev;
63
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;
68
69 /* This is how data is returned from the personality routine to
70 the target function's handler. */
71 _Unwind_Word data[4];
72
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;
77
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
41077ce4 81 to match that here just in case the system *didn't* have more
52a11cbf
RH
82 restrictive requirements. */
83 jmp_buf jbuf __attribute__((aligned));
84#else
85 void *jbuf[];
86#endif
87};
88
89struct _Unwind_Context
90{
91 struct SjLj_Function_Context *fc;
92};
93
41077ce4 94typedef struct
52a11cbf
RH
95{
96 _Unwind_Personality_Fn personality;
97} _Unwind_FrameState;
98
99\f
100/* Manage the chain of registered function contexts. */
101
102/* Single threaded fallback chain. */
103static struct SjLj_Function_Context *fc_static;
104
105#if __GTHREADS
106static __gthread_key_t fc_key;
107static int use_fc_key = -1;
108
52a11cbf
RH
109static void
110fc_key_init (void)
111{
4977bab6 112 use_fc_key = __gthread_key_create (&fc_key, 0) == 0;
52a11cbf
RH
113}
114
115static void
116fc_key_init_once (void)
117{
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;
121}
122#endif
123
124void
125_Unwind_SjLj_Register (struct SjLj_Function_Context *fc)
126{
127#if __GTHREADS
128 if (use_fc_key < 0)
129 fc_key_init_once ();
130
131 if (use_fc_key)
132 {
133 fc->prev = __gthread_getspecific (fc_key);
134 __gthread_setspecific (fc_key, fc);
135 }
136 else
137#endif
138 {
139 fc->prev = fc_static;
140 fc_static = fc;
141 }
142}
143
144static inline struct SjLj_Function_Context *
145_Unwind_SjLj_GetContext (void)
146{
147#if __GTHREADS
148 if (use_fc_key < 0)
149 fc_key_init_once ();
150
151 if (use_fc_key)
152 return __gthread_getspecific (fc_key);
153#endif
154 return fc_static;
155}
156
157static inline void
158_Unwind_SjLj_SetContext (struct SjLj_Function_Context *fc)
159{
160#if __GTHREADS
161 if (use_fc_key < 0)
162 fc_key_init_once ();
163
164 if (use_fc_key)
165 __gthread_setspecific (fc_key, fc);
166 else
167#endif
168 fc_static = fc;
169}
170
171void
172_Unwind_SjLj_Unregister (struct SjLj_Function_Context *fc)
173{
174 _Unwind_SjLj_SetContext (fc->prev);
175}
176
177\f
178/* Get/set the return data value at INDEX in CONTEXT. */
179
180_Unwind_Word
181_Unwind_GetGR (struct _Unwind_Context *context, int index)
182{
183 return context->fc->data[index];
184}
185
378683cf
RH
186/* Get the value of the CFA as saved in CONTEXT. */
187
188_Unwind_Word
525996eb 189_Unwind_GetCFA (struct _Unwind_Context *context __attribute__((unused)))
378683cf
RH
190{
191 /* ??? Ideally __builtin_setjmp places the CFA in the jmpbuf. */
0e32bbcc
DJ
192
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
202
203 /* Otherwise we're out of luck for now. */
d234bf61 204 return (_Unwind_Word) 0;
378683cf
RH
205}
206
52a11cbf
RH
207void
208_Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
209{
210 context->fc->data[index] = val;
211}
212
213/* Get the call-site index as saved in CONTEXT. */
214
215_Unwind_Ptr
216_Unwind_GetIP (struct _Unwind_Context *context)
217{
218 return context->fc->call_site + 1;
219}
220
754e45a8
JJ
221_Unwind_Ptr
222_Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
223{
224 *ip_before_insn = 0;
759580ed
AH
225 if (context->fc != NULL)
226 return context->fc->call_site + 1;
227 else
228 return 0;
754e45a8
JJ
229}
230
52a11cbf
RH
231/* Set the return landing pad index in CONTEXT. */
232
233void
234_Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
235{
236 context->fc->call_site = val - 1;
237}
238
239void *
240_Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
241{
242 return context->fc->lsda;
243}
244
245_Unwind_Ptr
2adaabc6 246_Unwind_GetRegionStart (struct _Unwind_Context *context __attribute__((unused)) )
52a11cbf
RH
247{
248 return 0;
249}
250
5dafd282 251void *
525996eb 252_Unwind_FindEnclosingFunction (void *pc __attribute__((unused)))
5dafd282
AH
253{
254 return NULL;
255}
256
48941cb8
RH
257#ifndef __ia64__
258_Unwind_Ptr
2adaabc6 259_Unwind_GetDataRelBase (struct _Unwind_Context *context __attribute__((unused)) )
48941cb8
RH
260{
261 return 0;
262}
263
264_Unwind_Ptr
2adaabc6 265_Unwind_GetTextRelBase (struct _Unwind_Context *context __attribute__((unused)) )
48941cb8
RH
266{
267 return 0;
268}
269#endif
52a11cbf
RH
270\f
271static inline _Unwind_Reason_Code
272uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
273{
274 if (context->fc == NULL)
275 {
276 fs->personality = NULL;
277 return _URC_END_OF_STACK;
278 }
279 else
280 {
281 fs->personality = context->fc->personality;
282 return _URC_NO_REASON;
283 }
284}
285
286static inline void
287uw_update_context (struct _Unwind_Context *context,
288 _Unwind_FrameState *fs __attribute__((unused)) )
289{
290 context->fc = context->fc->prev;
291}
292
60aef23e
DJ
293static void
294uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
295{
296 _Unwind_SjLj_Unregister (context->fc);
297 uw_update_context (context, fs);
298}
299
41077ce4 300static inline void
52a11cbf
RH
301uw_init_context (struct _Unwind_Context *context)
302{
303 context->fc = _Unwind_SjLj_GetContext ();
304}
305
6de9cd9a
DN
306static void __attribute__((noreturn))
307uw_install_context (struct _Unwind_Context *current __attribute__((unused)),
308 struct _Unwind_Context *target)
309{
310 _Unwind_SjLj_SetContext (target->fc);
311 longjmp (target->fc->jbuf, 1);
312}
52a11cbf
RH
313
314static inline _Unwind_Ptr
315uw_identify_context (struct _Unwind_Context *context)
316{
317 return (_Unwind_Ptr) context->fc;
318}
319
320
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
a944ceb9 327#define _Unwind_Resume_or_Rethrow _Unwind_SjLj_Resume_or_Rethrow
52a11cbf
RH
328
329#include "unwind.inc"
330
331#endif /* USING_SJLJ_EXCEPTIONS */