unwind-sjlj.c revision 90075
1314564Sdim/* DWARF2 exception handling and frame unwind runtime interface routines.
2303241Sdim   Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
3353358Sdim   Free Software Foundation, Inc.
4353358Sdim
5353358Sdim   This file is part of GCC.
6303241Sdim
7303241Sdim   GCC is free software; you can redistribute it and/or modify it
8303241Sdim   under the terms of the GNU General Public License as published by
9303241Sdim   the Free Software Foundation; either version 2, or (at your option)
10303241Sdim   any later version.
11303241Sdim
12303241Sdim   GCC is distributed in the hope that it will be useful, but WITHOUT
13327952Sdim   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14321369Sdim   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15303241Sdim   License for more details.
16314564Sdim
17303241Sdim   You should have received a copy of the GNU General Public License
18314564Sdim   along with GCC; see the file COPYING.  If not, write to the Free
19314564Sdim   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20314564Sdim   02111-1307, USA.  */
21314564Sdim
22327952Sdim#include "tconfig.h"
23303241Sdim#include "tsystem.h"
24314564Sdim#include "unwind.h"
25303241Sdim#include "gthr.h"
26314564Sdim
27314564Sdim#ifdef __USING_SJLJ_EXCEPTIONS__
28303241Sdim
29314564Sdim#ifdef DONT_USE_BUILTIN_SETJMP
30314564Sdim#ifndef inhibit_libc
31303241Sdim#include <setjmp.h>
32314564Sdim#else
33303241Sdimtypedef void *jmp_buf[JMP_BUF_SIZE];
34314564Sdimextern void longjmp(jmp_buf, int) __attribute__((noreturn));
35303241Sdim#endif
36314564Sdim#else
37303241Sdim#define setjmp __builtin_setjmp
38303241Sdim#define longjmp __builtin_longjmp
39314564Sdim#endif
40303241Sdim
41314564Sdim/* This structure is allocated on the stack of the target function.
42303241Sdim   This must match the definition created in except.c:init_eh.  */
43314564Sdimstruct SjLj_Function_Context
44303241Sdim{
45314564Sdim  /* This is the chain through all registered contexts.  It is
46303241Sdim     filled in by _Unwind_SjLj_Register.  */
47303241Sdim  struct SjLj_Function_Context *prev;
48314564Sdim
49314564Sdim  /* This is assigned in by the target function before every call
50303241Sdim     to the index of the call site in the lsda.  It is assigned by
51314564Sdim     the personality routine to the landing pad index.  */
52314564Sdim  int call_site;
53303241Sdim
54303241Sdim  /* This is how data is returned from the personality routine to
55303241Sdim     the target function's handler.  */
56  _Unwind_Word data[4];
57
58  /* These are filled in once by the target function before any
59     exceptions are expected to be handled.  */
60  _Unwind_Personality_Fn personality;
61  void *lsda;
62
63#ifdef DONT_USE_BUILTIN_SETJMP
64  /* We don't know what sort of alignment requirements the system
65     jmp_buf has.  We over estimated in except.c, and now we have
66     to match that here just in case the system *didn't* have more
67     restrictive requirements.  */
68  jmp_buf jbuf __attribute__((aligned));
69#else
70  void *jbuf[];
71#endif
72};
73
74struct _Unwind_Context
75{
76  struct SjLj_Function_Context *fc;
77};
78
79typedef struct
80{
81  _Unwind_Personality_Fn personality;
82} _Unwind_FrameState;
83
84
85/* Manage the chain of registered function contexts.  */
86
87/* Single threaded fallback chain.  */
88static struct SjLj_Function_Context *fc_static;
89
90#if __GTHREADS
91static __gthread_key_t fc_key;
92static int use_fc_key = -1;
93
94static void
95fc_key_dtor (void *ptr)
96{
97  __gthread_key_dtor (fc_key, ptr);
98}
99
100static void
101fc_key_init (void)
102{
103  use_fc_key = __gthread_key_create (&fc_key, fc_key_dtor) == 0;
104}
105
106static void
107fc_key_init_once (void)
108{
109  static __gthread_once_t once = __GTHREAD_ONCE_INIT;
110  if (__gthread_once (&once, fc_key_init) != 0 || use_fc_key < 0)
111    use_fc_key = 0;
112}
113#endif
114
115void
116_Unwind_SjLj_Register (struct SjLj_Function_Context *fc)
117{
118#if __GTHREADS
119  if (use_fc_key < 0)
120    fc_key_init_once ();
121
122  if (use_fc_key)
123    {
124      fc->prev = __gthread_getspecific (fc_key);
125      __gthread_setspecific (fc_key, fc);
126    }
127  else
128#endif
129    {
130      fc->prev = fc_static;
131      fc_static = fc;
132    }
133}
134
135static inline struct SjLj_Function_Context *
136_Unwind_SjLj_GetContext (void)
137{
138#if __GTHREADS
139  if (use_fc_key < 0)
140    fc_key_init_once ();
141
142  if (use_fc_key)
143    return __gthread_getspecific (fc_key);
144#endif
145  return fc_static;
146}
147
148static inline void
149_Unwind_SjLj_SetContext (struct SjLj_Function_Context *fc)
150{
151#if __GTHREADS
152  if (use_fc_key < 0)
153    fc_key_init_once ();
154
155  if (use_fc_key)
156    __gthread_setspecific (fc_key, fc);
157  else
158#endif
159    fc_static = fc;
160}
161
162void
163_Unwind_SjLj_Unregister (struct SjLj_Function_Context *fc)
164{
165  _Unwind_SjLj_SetContext (fc->prev);
166}
167
168
169/* Get/set the return data value at INDEX in CONTEXT.  */
170
171_Unwind_Word
172_Unwind_GetGR (struct _Unwind_Context *context, int index)
173{
174  return context->fc->data[index];
175}
176
177void
178_Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
179{
180  context->fc->data[index] = val;
181}
182
183/* Get the call-site index as saved in CONTEXT.  */
184
185_Unwind_Ptr
186_Unwind_GetIP (struct _Unwind_Context *context)
187{
188  return context->fc->call_site + 1;
189}
190
191/* Set the return landing pad index in CONTEXT.  */
192
193void
194_Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
195{
196  context->fc->call_site = val - 1;
197}
198
199void *
200_Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
201{
202  return context->fc->lsda;
203}
204
205_Unwind_Ptr
206_Unwind_GetRegionStart (struct _Unwind_Context *context __attribute__((unused)) )
207{
208  return 0;
209}
210
211#ifndef __ia64__
212_Unwind_Ptr
213_Unwind_GetDataRelBase (struct _Unwind_Context *context __attribute__((unused)) )
214{
215  return 0;
216}
217
218_Unwind_Ptr
219_Unwind_GetTextRelBase (struct _Unwind_Context *context __attribute__((unused)) )
220{
221  return 0;
222}
223#endif
224
225static inline _Unwind_Reason_Code
226uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
227{
228  if (context->fc == NULL)
229    {
230      fs->personality = NULL;
231      return _URC_END_OF_STACK;
232    }
233  else
234    {
235      fs->personality = context->fc->personality;
236      return _URC_NO_REASON;
237    }
238}
239
240static inline void
241uw_update_context (struct _Unwind_Context *context,
242		   _Unwind_FrameState *fs __attribute__((unused)) )
243{
244  context->fc = context->fc->prev;
245}
246
247static inline void
248uw_init_context (struct _Unwind_Context *context)
249{
250  context->fc = _Unwind_SjLj_GetContext ();
251}
252
253/* ??? There appear to be bugs in integrate.c wrt __builtin_longjmp and
254   virtual-stack-vars.  An inline version of this segfaults on Sparc.  */
255#define uw_install_context(CURRENT, TARGET)		\
256  do							\
257    {							\
258      _Unwind_SjLj_SetContext ((TARGET)->fc);		\
259      longjmp ((TARGET)->fc->jbuf, 1);			\
260    }							\
261  while (0)
262
263
264static inline _Unwind_Ptr
265uw_identify_context (struct _Unwind_Context *context)
266{
267  return (_Unwind_Ptr) context->fc;
268}
269
270
271/* Play games with unwind symbols so that we can have call frame
272   and sjlj symbols in the same shared library.  Not that you can
273   use them simultaneously...  */
274#define _Unwind_RaiseException		_Unwind_SjLj_RaiseException
275#define _Unwind_ForcedUnwind		_Unwind_SjLj_ForcedUnwind
276#define _Unwind_Resume			_Unwind_SjLj_Resume
277
278#include "unwind.inc"
279
280#endif /* USING_SJLJ_EXCEPTIONS */
281