xref: /openbsd/gnu/usr.bin/gcc/gcc/unwind-sjlj.c (revision c87b03e5)
1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4 
5    This file is part of GCC.
6 
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)
10    any later version.
11 
12    GCC is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21 
22 #include "tconfig.h"
23 #include "tsystem.h"
24 #include "unwind.h"
25 #include "gthr.h"
26 
27 #ifdef __USING_SJLJ_EXCEPTIONS__
28 
29 #ifdef DONT_USE_BUILTIN_SETJMP
30 #ifndef inhibit_libc
31 #include <setjmp.h>
32 #else
33 typedef void *jmp_buf[JMP_BUF_SIZE];
34 extern void longjmp(jmp_buf, int) __attribute__((noreturn));
35 #endif
36 #else
37 #define setjmp __builtin_setjmp
38 #define longjmp __builtin_longjmp
39 #endif
40 
41 /* This structure is allocated on the stack of the target function.
42    This must match the definition created in except.c:init_eh.  */
43 struct SjLj_Function_Context
44 {
45   /* This is the chain through all registered contexts.  It is
46      filled in by _Unwind_SjLj_Register.  */
47   struct SjLj_Function_Context *prev;
48 
49   /* This is assigned in by the target function before every call
50      to the index of the call site in the lsda.  It is assigned by
51      the personality routine to the landing pad index.  */
52   int call_site;
53 
54   /* This is how data is returned from the personality routine to
55      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 
74 struct _Unwind_Context
75 {
76   struct SjLj_Function_Context *fc;
77 };
78 
79 typedef 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.  */
88 static struct SjLj_Function_Context *fc_static;
89 
90 #if __GTHREADS
91 static __gthread_key_t fc_key;
92 static int use_fc_key = -1;
93 
94 static void
fc_key_dtor(void * ptr)95 fc_key_dtor (void *ptr)
96 {
97   __gthread_key_dtor (fc_key, ptr);
98 }
99 
100 static void
fc_key_init(void)101 fc_key_init (void)
102 {
103   use_fc_key = __gthread_key_create (&fc_key, fc_key_dtor) == 0;
104 }
105 
106 static void
fc_key_init_once(void)107 fc_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 
115 void
_Unwind_SjLj_Register(struct SjLj_Function_Context * fc)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 
135 static inline struct SjLj_Function_Context *
_Unwind_SjLj_GetContext(void)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 
148 static inline void
_Unwind_SjLj_SetContext(struct SjLj_Function_Context * fc)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 
162 void
_Unwind_SjLj_Unregister(struct SjLj_Function_Context * fc)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
_Unwind_GetGR(struct _Unwind_Context * context,int index)172 _Unwind_GetGR (struct _Unwind_Context *context, int index)
173 {
174   return context->fc->data[index];
175 }
176 
177 /* Get the value of the CFA as saved in CONTEXT.  */
178 
179 _Unwind_Word
_Unwind_GetCFA(struct _Unwind_Context * context)180 _Unwind_GetCFA (struct _Unwind_Context *context)
181 {
182   /* ??? Ideally __builtin_setjmp places the CFA in the jmpbuf.  */
183   return NULL;
184 }
185 
186 void
_Unwind_SetGR(struct _Unwind_Context * context,int index,_Unwind_Word val)187 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
188 {
189   context->fc->data[index] = val;
190 }
191 
192 /* Get the call-site index as saved in CONTEXT.  */
193 
194 _Unwind_Ptr
_Unwind_GetIP(struct _Unwind_Context * context)195 _Unwind_GetIP (struct _Unwind_Context *context)
196 {
197   return context->fc->call_site + 1;
198 }
199 
200 /* Set the return landing pad index in CONTEXT.  */
201 
202 void
_Unwind_SetIP(struct _Unwind_Context * context,_Unwind_Ptr val)203 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
204 {
205   context->fc->call_site = val - 1;
206 }
207 
208 void *
_Unwind_GetLanguageSpecificData(struct _Unwind_Context * context)209 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
210 {
211   return context->fc->lsda;
212 }
213 
214 _Unwind_Ptr
_Unwind_GetRegionStart(struct _Unwind_Context * context)215 _Unwind_GetRegionStart (struct _Unwind_Context *context __attribute__((unused)) )
216 {
217   return 0;
218 }
219 
220 void *
_Unwind_FindEnclosingFunction(void * pc)221 _Unwind_FindEnclosingFunction (void *pc)
222 {
223   return NULL;
224 }
225 
226 #ifndef __ia64__
227 _Unwind_Ptr
_Unwind_GetDataRelBase(struct _Unwind_Context * context)228 _Unwind_GetDataRelBase (struct _Unwind_Context *context __attribute__((unused)) )
229 {
230   return 0;
231 }
232 
233 _Unwind_Ptr
_Unwind_GetTextRelBase(struct _Unwind_Context * context)234 _Unwind_GetTextRelBase (struct _Unwind_Context *context __attribute__((unused)) )
235 {
236   return 0;
237 }
238 #endif
239 
240 static inline _Unwind_Reason_Code
uw_frame_state_for(struct _Unwind_Context * context,_Unwind_FrameState * fs)241 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
242 {
243   if (context->fc == NULL)
244     {
245       fs->personality = NULL;
246       return _URC_END_OF_STACK;
247     }
248   else
249     {
250       fs->personality = context->fc->personality;
251       return _URC_NO_REASON;
252     }
253 }
254 
255 static inline void
uw_update_context(struct _Unwind_Context * context,_Unwind_FrameState * fs)256 uw_update_context (struct _Unwind_Context *context,
257 		   _Unwind_FrameState *fs __attribute__((unused)) )
258 {
259   context->fc = context->fc->prev;
260 }
261 
262 static inline void
uw_init_context(struct _Unwind_Context * context)263 uw_init_context (struct _Unwind_Context *context)
264 {
265   context->fc = _Unwind_SjLj_GetContext ();
266 }
267 
268 /* ??? There appear to be bugs in integrate.c wrt __builtin_longjmp and
269    virtual-stack-vars.  An inline version of this segfaults on SPARC.  */
270 #define uw_install_context(CURRENT, TARGET)		\
271   do							\
272     {							\
273       _Unwind_SjLj_SetContext ((TARGET)->fc);		\
274       longjmp ((TARGET)->fc->jbuf, 1);			\
275     }							\
276   while (0)
277 
278 
279 static inline _Unwind_Ptr
uw_identify_context(struct _Unwind_Context * context)280 uw_identify_context (struct _Unwind_Context *context)
281 {
282   return (_Unwind_Ptr) context->fc;
283 }
284 
285 
286 /* Play games with unwind symbols so that we can have call frame
287    and sjlj symbols in the same shared library.  Not that you can
288    use them simultaneously...  */
289 #define _Unwind_RaiseException		_Unwind_SjLj_RaiseException
290 #define _Unwind_ForcedUnwind		_Unwind_SjLj_ForcedUnwind
291 #define _Unwind_Resume			_Unwind_SjLj_Resume
292 #define _Unwind_Resume_or_Rethrow	_Unwind_SjLj_Resume_or_Rethrow
293 
294 #include "unwind.inc"
295 
296 #endif /* USING_SJLJ_EXCEPTIONS */
297