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. */
13 #define NO_SIZE_OF_ENCODED_VALUE
14 #include "unwind-pe.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' };
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');
38 /* This function is called by exception handlers used when unwinding
39 the stack after a recovered panic. The exception handler looks
41 __go_check_defer (frame);
43 If we have not yet reached the frame we are looking for, we
44 continue unwinding. */
47 __go_check_defer (_Bool
*frame
)
50 struct _Unwind_Exception
*hdr
;
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
)
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
;
83 if (d
== NULL
|| d
->frame
!= frame
|| d
->pfn
== 0)
86 pfn
= (void (*) (void *)) d
->pfn
;
91 if (runtime_m () != NULL
)
92 runtime_freedefer (d
);
96 /* The recover function caught the panic thrown by some
102 recovered
= n
->recovered
;
108 /* Just return and continue executing Go code. */
113 /* We are panicing through this function. */
116 else if (g
->_defer
!= NULL
117 && g
->_defer
->pfn
== 0
118 && g
->_defer
->frame
== frame
)
122 /* This is the defer function which called recover. Simply
123 return to stop the stack unwind, and let the Go code continue
128 if (runtime_m () != NULL
)
129 runtime_freedefer (d
);
131 /* We are returning from this function. */
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
);
145 #if defined(_LIBUNWIND_STD_ABI)
146 _Unwind_RaiseException (hdr
);
148 _Unwind_Resume_or_Rethrow (hdr
);
152 /* Rethrowing the exception should not return. */
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. */
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
);
176 _Unwind_RaiseException (hdr
);
179 /* Raising an exception should not return. */
183 /* The rest of this code is really similar to gcc/unwind-c.c and
184 libjava/exception.cc. */
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
;
197 static const unsigned char *
198 parse_lsda_header (struct _Unwind_Context
*context
, const unsigned char *p
,
199 lsda_header_info
*info
)
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
);
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
;
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
;
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 \
242 if (__gnu_unwind_frame (ue_header, context) != _URC_OK) \
243 return _URC_FAILURE; \
244 return _URC_CONTINUE_UNWIND; \
248 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
251 #ifdef __USING_SJLJ_EXCEPTIONS__
252 #define PERSONALITY_FUNCTION __gccgo_personality_sj0
253 #define __builtin_eh_return_data_regno(x) x
255 #define PERSONALITY_FUNCTION __gccgo_personality_v0
258 #ifdef __ARM_EABI_UNWINDER__
260 PERSONALITY_FUNCTION (_Unwind_State
, struct _Unwind_Exception
*,
261 struct _Unwind_Context
*)
262 __attribute__ ((no_split_stack
, flatten
));
265 PERSONALITY_FUNCTION (_Unwind_State state
,
266 struct _Unwind_Exception
* ue_header
,
267 struct _Unwind_Context
* context
)
270 PERSONALITY_FUNCTION (int, _Unwind_Action
, _Unwind_Exception_Class
,
271 struct _Unwind_Exception
*, struct _Unwind_Context
*)
272 __attribute__ ((no_split_stack
, flatten
));
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
)
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;
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
;
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
;
305 case _US_UNWIND_FRAME_RESUME
:
312 actions
|= state
& _US_FORCE_UNWIND
;
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
);
324 return _URC_FATAL_PHASE1_ERROR
;
326 is_foreign
= exception_class
!= __go_exception_class
;
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
)
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
);
341 ip
= _Unwind_GetIP (context
);
343 if (! ip_before_insn
)
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
354 return _URC_CONTINUE_UNWIND
;
357 _uleb128_t cs_lp
, cs_action
;
360 p
= read_uleb128 (p
, &cs_lp
);
361 p
= read_uleb128 (p
, &cs_action
);
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;
369 action_record
= info
.action_table
+ cs_action
- 1;
370 goto found_something
;
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
)
391 landing_pad
= info
.LPStart
+ cs_lp
;
393 action_record
= info
.action_table
+ cs_action
- 1;
394 goto found_something
;
399 /* IP is not in table. No associated cleanups. */
403 if (landing_pad
== 0)
405 /* IP is present, but has a null landing pad.
406 No handler to be run. */
410 if (actions
& _UA_SEARCH_PHASE
)
412 if (action_record
== 0)
414 /* This indicates a cleanup rather than an exception
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. */
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
;