2016-10-13 Sandra Loosemore <sandra@codesourcery.com>
[official-gcc.git] / libgo / runtime / go-unwind.c
blobea11e4e3b1d937a556076cffc42073ba480bfd47
1 /* go-unwind.c -- unwind the stack for panic/recover.
3 Copyright 2010 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
7 #include "config.h"
9 #include <stdlib.h>
10 #include <unistd.h>
12 #include "unwind.h"
13 #define NO_SIZE_OF_ENCODED_VALUE
14 #include "unwind-pe.h"
16 #include "runtime.h"
17 #include "go-alloc.h"
18 #include "go-panic.h"
20 /* The code for a Go exception. */
22 #ifdef __ARM_EABI_UNWINDER__
23 static const _Unwind_Exception_Class __go_exception_class =
24 { 'G', 'N', 'U', 'C', 'G', 'O', '\0', '\0' };
25 #else
26 static const _Unwind_Exception_Class __go_exception_class =
27 ((((((((_Unwind_Exception_Class) 'G'
28 << 8 | (_Unwind_Exception_Class) 'N')
29 << 8 | (_Unwind_Exception_Class) 'U')
30 << 8 | (_Unwind_Exception_Class) 'C')
31 << 8 | (_Unwind_Exception_Class) 'G')
32 << 8 | (_Unwind_Exception_Class) 'O')
33 << 8 | (_Unwind_Exception_Class) '\0')
34 << 8 | (_Unwind_Exception_Class) '\0');
35 #endif
38 /* This function is called by exception handlers used when unwinding
39 the stack after a recovered panic. The exception handler looks
40 like this:
41 __go_check_defer (frame);
42 return;
43 If we have not yet reached the frame we are looking for, we
44 continue unwinding. */
46 void
47 __go_check_defer (_Bool *frame)
49 G *g;
50 struct _Unwind_Exception *hdr;
52 g = runtime_g ();
54 if (g == NULL)
56 /* Some other language has thrown an exception. We know there
57 are no defer handlers, so there is nothing to do. */
59 else if (g->isforeign)
61 Panic *n;
62 _Bool recovered;
64 /* Some other language has thrown an exception. We need to run
65 the local defer handlers. If they call recover, we stop
66 unwinding the stack here. */
68 n = (Panic *) __go_alloc (sizeof (Panic));
70 n->arg.__type_descriptor = NULL;
71 n->arg.__object = NULL;
72 n->recovered = 0;
73 n->isforeign = 1;
74 n->next = g->_panic;
75 g->_panic = n;
77 while (1)
79 Defer *d;
80 void (*pfn) (void *);
82 d = g->_defer;
83 if (d == NULL || d->frame != frame || d->pfn == 0)
84 break;
86 pfn = (void (*) (void *)) d->pfn;
87 g->_defer = d->next;
89 (*pfn) (d->arg);
91 if (runtime_m () != NULL)
92 runtime_freedefer (d);
94 if (n->recovered)
96 /* The recover function caught the panic thrown by some
97 other language. */
98 break;
102 recovered = n->recovered;
103 g->_panic = n->next;
104 __go_free (n);
106 if (recovered)
108 /* Just return and continue executing Go code. */
109 *frame = 1;
110 return;
113 /* We are panicing through this function. */
114 *frame = 0;
116 else if (g->_defer != NULL
117 && g->_defer->pfn == 0
118 && g->_defer->frame == frame)
120 Defer *d;
122 /* This is the defer function which called recover. Simply
123 return to stop the stack unwind, and let the Go code continue
124 to execute. */
125 d = g->_defer;
126 g->_defer = d->next;
128 if (runtime_m () != NULL)
129 runtime_freedefer (d);
131 /* We are returning from this function. */
132 *frame = 1;
134 return;
137 /* This is some other defer function. It was already run by the
138 call to panic, or just above. Rethrow the exception. */
140 hdr = (struct _Unwind_Exception *) g->exception;
142 #ifdef __USING_SJLJ_EXCEPTIONS__
143 _Unwind_SjLj_Resume_or_Rethrow (hdr);
144 #else
145 #if defined(_LIBUNWIND_STD_ABI)
146 _Unwind_RaiseException (hdr);
147 #else
148 _Unwind_Resume_or_Rethrow (hdr);
149 #endif
150 #endif
152 /* Rethrowing the exception should not return. */
153 abort();
156 /* Unwind function calls until we reach the one which used a defer
157 function which called recover. Each function which uses a defer
158 statement will have an exception handler, as shown above. */
160 void
161 __go_unwind_stack ()
163 struct _Unwind_Exception *hdr;
165 hdr = ((struct _Unwind_Exception *)
166 __go_alloc (sizeof (struct _Unwind_Exception)));
167 __builtin_memcpy (&hdr->exception_class, &__go_exception_class,
168 sizeof hdr->exception_class);
169 hdr->exception_cleanup = NULL;
171 runtime_g ()->exception = hdr;
173 #ifdef __USING_SJLJ_EXCEPTIONS__
174 _Unwind_SjLj_RaiseException (hdr);
175 #else
176 _Unwind_RaiseException (hdr);
177 #endif
179 /* Raising an exception should not return. */
180 abort ();
183 /* The rest of this code is really similar to gcc/unwind-c.c and
184 libjava/exception.cc. */
186 typedef struct
188 _Unwind_Ptr Start;
189 _Unwind_Ptr LPStart;
190 _Unwind_Ptr ttype_base;
191 const unsigned char *TType;
192 const unsigned char *action_table;
193 unsigned char ttype_encoding;
194 unsigned char call_site_encoding;
195 } lsda_header_info;
197 static const unsigned char *
198 parse_lsda_header (struct _Unwind_Context *context, const unsigned char *p,
199 lsda_header_info *info)
201 _uleb128_t tmp;
202 unsigned char lpstart_encoding;
204 info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
206 /* Find @LPStart, the base to which landing pad offsets are relative. */
207 lpstart_encoding = *p++;
208 if (lpstart_encoding != DW_EH_PE_omit)
209 p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
210 else
211 info->LPStart = info->Start;
213 /* Find @TType, the base of the handler and exception spec type data. */
214 info->ttype_encoding = *p++;
215 if (info->ttype_encoding != DW_EH_PE_omit)
217 p = read_uleb128 (p, &tmp);
218 info->TType = p + tmp;
220 else
221 info->TType = 0;
223 /* The encoding and length of the call-site table; the action table
224 immediately follows. */
225 info->call_site_encoding = *p++;
226 p = read_uleb128 (p, &tmp);
227 info->action_table = p + tmp;
229 return p;
232 /* The personality function is invoked when unwinding the stack due to
233 a panic. Its job is to find the cleanup and exception handlers to
234 run. We can't split the stack here, because we won't be able to
235 unwind from that split. */
237 #ifdef __ARM_EABI_UNWINDER__
238 /* ARM EABI personality routines must also unwind the stack. */
239 #define CONTINUE_UNWINDING \
240 do \
242 if (__gnu_unwind_frame (ue_header, context) != _URC_OK) \
243 return _URC_FAILURE; \
244 return _URC_CONTINUE_UNWIND; \
246 while (0)
247 #else
248 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
249 #endif
251 #ifdef __USING_SJLJ_EXCEPTIONS__
252 #define PERSONALITY_FUNCTION __gccgo_personality_sj0
253 #define __builtin_eh_return_data_regno(x) x
254 #else
255 #define PERSONALITY_FUNCTION __gccgo_personality_v0
256 #endif
258 #ifdef __ARM_EABI_UNWINDER__
259 _Unwind_Reason_Code
260 PERSONALITY_FUNCTION (_Unwind_State, struct _Unwind_Exception *,
261 struct _Unwind_Context *)
262 __attribute__ ((no_split_stack, flatten));
264 _Unwind_Reason_Code
265 PERSONALITY_FUNCTION (_Unwind_State state,
266 struct _Unwind_Exception * ue_header,
267 struct _Unwind_Context * context)
268 #else
269 _Unwind_Reason_Code
270 PERSONALITY_FUNCTION (int, _Unwind_Action, _Unwind_Exception_Class,
271 struct _Unwind_Exception *, struct _Unwind_Context *)
272 __attribute__ ((no_split_stack, flatten));
274 _Unwind_Reason_Code
275 PERSONALITY_FUNCTION (int version,
276 _Unwind_Action actions,
277 _Unwind_Exception_Class exception_class,
278 struct _Unwind_Exception *ue_header,
279 struct _Unwind_Context *context)
280 #endif
282 lsda_header_info info;
283 const unsigned char *language_specific_data, *p, *action_record;
284 _Unwind_Ptr landing_pad, ip;
285 int ip_before_insn = 0;
286 _Bool is_foreign;
287 G *g;
289 #ifdef __ARM_EABI_UNWINDER__
290 _Unwind_Action actions;
292 switch (state & _US_ACTION_MASK)
294 case _US_VIRTUAL_UNWIND_FRAME:
295 actions = _UA_SEARCH_PHASE;
296 break;
298 case _US_UNWIND_FRAME_STARTING:
299 actions = _UA_CLEANUP_PHASE;
300 if (!(state & _US_FORCE_UNWIND)
301 && ue_header->barrier_cache.sp == _Unwind_GetGR(context, 13))
302 actions |= _UA_HANDLER_FRAME;
303 break;
305 case _US_UNWIND_FRAME_RESUME:
306 CONTINUE_UNWINDING;
307 break;
309 default:
310 abort();
312 actions |= state & _US_FORCE_UNWIND;
314 is_foreign = 0;
316 /* The dwarf unwinder assumes the context structure holds things like the
317 function and LSDA pointers. The ARM implementation caches these in
318 the exception header (UCB). To avoid rewriting everything we make the
319 virtual IP register point at the UCB. */
320 ip = (_Unwind_Ptr) ue_header;
321 _Unwind_SetGR (context, 12, ip);
322 #else
323 if (version != 1)
324 return _URC_FATAL_PHASE1_ERROR;
326 is_foreign = exception_class != __go_exception_class;
327 #endif
329 language_specific_data = (const unsigned char *)
330 _Unwind_GetLanguageSpecificData (context);
332 /* If no LSDA, then there are no handlers or cleanups. */
333 if (! language_specific_data)
334 CONTINUE_UNWINDING;
336 /* Parse the LSDA header. */
337 p = parse_lsda_header (context, language_specific_data, &info);
338 #ifdef HAVE_GETIPINFO
339 ip = _Unwind_GetIPInfo (context, &ip_before_insn);
340 #else
341 ip = _Unwind_GetIP (context);
342 #endif
343 if (! ip_before_insn)
344 --ip;
345 landing_pad = 0;
346 action_record = NULL;
348 #ifdef __USING_SJLJ_EXCEPTIONS__
349 /* The given "IP" is an index into the call-site table, with two
350 exceptions -- -1 means no-action, and 0 means terminate. But
351 since we're using uleb128 values, we've not got random access
352 to the array. */
353 if ((int) ip <= 0)
354 return _URC_CONTINUE_UNWIND;
355 else
357 _uleb128_t cs_lp, cs_action;
360 p = read_uleb128 (p, &cs_lp);
361 p = read_uleb128 (p, &cs_action);
363 while (--ip);
365 /* Can never have null landing pad for sjlj -- that would have
366 been indicated by a -1 call site index. */
367 landing_pad = (_Unwind_Ptr)cs_lp + 1;
368 if (cs_action)
369 action_record = info.action_table + cs_action - 1;
370 goto found_something;
372 #else
373 /* Search the call-site table for the action associated with this IP. */
374 while (p < info.action_table)
376 _Unwind_Ptr cs_start, cs_len, cs_lp;
377 _uleb128_t cs_action;
379 /* Note that all call-site encodings are "absolute" displacements. */
380 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
381 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
382 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
383 p = read_uleb128 (p, &cs_action);
385 /* The table is sorted, so if we've passed the ip, stop. */
386 if (ip < info.Start + cs_start)
387 p = info.action_table;
388 else if (ip < info.Start + cs_start + cs_len)
390 if (cs_lp)
391 landing_pad = info.LPStart + cs_lp;
392 if (cs_action)
393 action_record = info.action_table + cs_action - 1;
394 goto found_something;
397 #endif
399 /* IP is not in table. No associated cleanups. */
400 CONTINUE_UNWINDING;
402 found_something:
403 if (landing_pad == 0)
405 /* IP is present, but has a null landing pad.
406 No handler to be run. */
407 CONTINUE_UNWINDING;
410 if (actions & _UA_SEARCH_PHASE)
412 if (action_record == 0)
414 /* This indicates a cleanup rather than an exception
415 handler. */
416 CONTINUE_UNWINDING;
419 return _URC_HANDLER_FOUND;
422 /* It's possible for g to be NULL here for an exception thrown by a
423 language other than Go. */
424 g = runtime_g ();
425 if (g == NULL)
427 if (!is_foreign)
428 abort ();
430 else
432 g->exception = ue_header;
433 g->isforeign = is_foreign;
436 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
437 (_Unwind_Ptr) ue_header);
438 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1), 0);
439 _Unwind_SetIP (context, landing_pad);
440 return _URC_INSTALL_CONTEXT;