1 // -*- C++ -*- The GNU C++ exception personality routine.
2 // Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
4 // This file is part of GCC.
6 // GCC is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
11 // GCC is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with GCC; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 #include <bits/c++config.h>
32 #include <exception_defines.h>
33 #include "unwind-cxx.h"
35 using namespace __cxxabiv1;
37 #ifdef __ARM_EABI_UNWINDER__
38 #define NO_SIZE_OF_ENCODED_VALUE
41 #include "unwind-pe.h"
44 struct lsda_header_info
48 _Unwind_Ptr ttype_base;
49 const unsigned char *TType;
50 const unsigned char *action_table;
51 unsigned char ttype_encoding;
52 unsigned char call_site_encoding;
55 static const unsigned char *
56 parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
57 lsda_header_info *info)
60 unsigned char lpstart_encoding;
62 info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
64 // Find @LPStart, the base to which landing pad offsets are relative.
65 lpstart_encoding = *p++;
66 if (lpstart_encoding != DW_EH_PE_omit)
67 p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
69 info->LPStart = info->Start;
71 // Find @TType, the base of the handler and exception spec type data.
72 info->ttype_encoding = *p++;
73 if (info->ttype_encoding != DW_EH_PE_omit)
75 p = read_uleb128 (p, &tmp);
76 info->TType = p + tmp;
81 // The encoding and length of the call-site table; the action table
82 // immediately follows.
83 info->call_site_encoding = *p++;
84 p = read_uleb128 (p, &tmp);
85 info->action_table = p + tmp;
90 #ifdef __ARM_EABI_UNWINDER__
92 // Return an element from a type table.
94 static const std::type_info*
95 get_ttype_entry(lsda_header_info* info, _Unwind_Word i)
99 ptr = (_Unwind_Ptr) (info->TType - (i * 4));
100 ptr = _Unwind_decode_target2(ptr);
102 return reinterpret_cast<const std::type_info *>(ptr);
105 // The ABI provides a routine for matching exception object types.
106 typedef _Unwind_Control_Block _throw_typet;
107 #define get_adjusted_ptr(catch_type, throw_type, thrown_ptr_p) \
108 (__cxa_type_match (throw_type, catch_type, false, thrown_ptr_p) \
111 // Return true if THROW_TYPE matches one if the filter types.
114 check_exception_spec(lsda_header_info* info, _throw_typet* throw_type,
115 void* thrown_ptr, _Unwind_Sword filter_value)
117 const _Unwind_Word* e = ((const _Unwind_Word*) info->TType)
122 const std::type_info* catch_type;
127 // Zero signals the end of the list. If we've not found
128 // a match by now, then we've failed the specification.
132 tmp = _Unwind_decode_target2((_Unwind_Word) e);
134 // Match a ttype entry.
135 catch_type = reinterpret_cast<const std::type_info*>(tmp);
137 // ??? There is currently no way to ask the RTTI code about the
138 // relationship between two types without reference to a specific
139 // object. There should be; then we wouldn't need to mess with
141 if (get_adjusted_ptr(catch_type, throw_type, &thrown_ptr))
144 // Advance to the next entry.
150 // Save stage1 handler information in the exception object
153 save_caught_exception(struct _Unwind_Exception* ue_header,
154 struct _Unwind_Context* context,
156 int handler_switch_value,
157 const unsigned char* language_specific_data,
158 _Unwind_Ptr landing_pad,
159 const unsigned char* action_record
160 __attribute__((__unused__)))
162 ue_header->barrier_cache.sp = _Unwind_GetGR(context, 13);
163 ue_header->barrier_cache.bitpattern[0] = (_uw) thrown_ptr;
164 ue_header->barrier_cache.bitpattern[1]
165 = (_uw) handler_switch_value;
166 ue_header->barrier_cache.bitpattern[2]
167 = (_uw) language_specific_data;
168 ue_header->barrier_cache.bitpattern[3] = (_uw) landing_pad;
172 // Restore the catch handler data saved during phase1.
175 restore_caught_exception(struct _Unwind_Exception* ue_header,
176 int& handler_switch_value,
177 const unsigned char*& language_specific_data,
178 _Unwind_Ptr& landing_pad)
180 handler_switch_value = (int) ue_header->barrier_cache.bitpattern[1];
181 language_specific_data =
182 (const unsigned char*) ue_header->barrier_cache.bitpattern[2];
183 landing_pad = (_Unwind_Ptr) ue_header->barrier_cache.bitpattern[3];
186 #define CONTINUE_UNWINDING \
189 if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
190 return _URC_FAILURE; \
191 return _URC_CONTINUE_UNWIND; \
196 typedef const std::type_info _throw_typet;
199 // Return an element from a type table.
201 static const std::type_info *
202 get_ttype_entry (lsda_header_info *info, _Unwind_Word i)
206 i *= size_of_encoded_value (info->ttype_encoding);
207 read_encoded_value_with_base (info->ttype_encoding, info->ttype_base,
208 info->TType - i, &ptr);
210 return reinterpret_cast<const std::type_info *>(ptr);
213 // Given the thrown type THROW_TYPE, pointer to a variable containing a
214 // pointer to the exception object THROWN_PTR_P and a type CATCH_TYPE to
215 // compare against, return whether or not there is a match and if so,
216 // update *THROWN_PTR_P.
219 get_adjusted_ptr (const std::type_info *catch_type,
220 const std::type_info *throw_type,
223 void *thrown_ptr = *thrown_ptr_p;
225 // Pointer types need to adjust the actual pointer, not
226 // the pointer to pointer that is the exception object.
227 // This also has the effect of passing pointer types
228 // "by value" through the __cxa_begin_catch return value.
229 if (throw_type->__is_pointer_p ())
230 thrown_ptr = *(void **) thrown_ptr;
232 if (catch_type->__do_catch (throw_type, &thrown_ptr, 1))
234 *thrown_ptr_p = thrown_ptr;
241 // Return true if THROW_TYPE matches one if the filter types.
244 check_exception_spec(lsda_header_info* info, _throw_typet* throw_type,
245 void* thrown_ptr, _Unwind_Sword filter_value)
247 const unsigned char *e = info->TType - filter_value - 1;
251 const std::type_info *catch_type;
254 e = read_uleb128 (e, &tmp);
256 // Zero signals the end of the list. If we've not found
257 // a match by now, then we've failed the specification.
261 // Match a ttype entry.
262 catch_type = get_ttype_entry (info, tmp);
264 // ??? There is currently no way to ask the RTTI code about the
265 // relationship between two types without reference to a specific
266 // object. There should be; then we wouldn't need to mess with
268 if (get_adjusted_ptr (catch_type, throw_type, &thrown_ptr))
274 // Save stage1 handler information in the exception object
277 save_caught_exception(struct _Unwind_Exception* ue_header,
278 struct _Unwind_Context* context
279 __attribute__((__unused__)),
281 int handler_switch_value,
282 const unsigned char* language_specific_data,
283 _Unwind_Ptr landing_pad __attribute__((__unused__)),
284 const unsigned char* action_record)
286 __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
288 xh->handlerSwitchValue = handler_switch_value;
289 xh->actionRecord = action_record;
290 xh->languageSpecificData = language_specific_data;
291 xh->adjustedPtr = thrown_ptr;
293 // ??? Completely unknown what this field is supposed to be for.
294 // ??? Need to cache TType encoding base for call_unexpected.
295 xh->catchTemp = landing_pad;
299 // Restore the catch handler information saved during phase1.
302 restore_caught_exception(struct _Unwind_Exception* ue_header,
303 int& handler_switch_value,
304 const unsigned char*& language_specific_data,
305 _Unwind_Ptr& landing_pad)
307 __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
308 handler_switch_value = xh->handlerSwitchValue;
309 language_specific_data = xh->languageSpecificData;
310 landing_pad = (_Unwind_Ptr) xh->catchTemp;
313 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
315 #endif // !__ARM_EABI_UNWINDER__
317 // Return true if the filter spec is empty, ie throw().
320 empty_exception_spec (lsda_header_info *info, _Unwind_Sword filter_value)
322 const unsigned char *e = info->TType - filter_value - 1;
325 e = read_uleb128 (e, &tmp);
329 // Using a different personality function name causes link failures
330 // when trying to mix code using different exception handling models.
331 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
332 #define PERSONALITY_FUNCTION __gxx_personality_sj0
333 #define __builtin_eh_return_data_regno(x) x
335 #define PERSONALITY_FUNCTION __gxx_personality_v0
338 extern "C" _Unwind_Reason_Code
339 #ifdef __ARM_EABI_UNWINDER__
340 PERSONALITY_FUNCTION (_Unwind_State state,
341 struct _Unwind_Exception* ue_header,
342 struct _Unwind_Context* context)
344 PERSONALITY_FUNCTION (int version,
345 _Unwind_Action actions,
346 _Unwind_Exception_Class exception_class,
347 struct _Unwind_Exception *ue_header,
348 struct _Unwind_Context *context)
351 enum found_handler_type
359 lsda_header_info info;
360 const unsigned char *language_specific_data;
361 const unsigned char *action_record;
362 const unsigned char *p;
363 _Unwind_Ptr landing_pad, ip;
364 int handler_switch_value;
365 void* thrown_ptr = ue_header + 1;
366 bool foreign_exception;
368 #ifdef __ARM_EABI_UNWINDER__
369 _Unwind_Action actions;
371 switch (state & _US_ACTION_MASK)
373 case _US_VIRTUAL_UNWIND_FRAME:
374 actions = _UA_SEARCH_PHASE;
377 case _US_UNWIND_FRAME_STARTING:
378 actions = _UA_CLEANUP_PHASE;
379 if (!(state & _US_FORCE_UNWIND)
380 && ue_header->barrier_cache.sp == _Unwind_GetGR(context, 13))
381 actions |= _UA_HANDLER_FRAME;
384 case _US_UNWIND_FRAME_RESUME:
391 actions |= state & _US_FORCE_UNWIND;
393 // We don't know which runtime we're working with, so can't check this.
394 // However the ABI routines hide this from us, and we don't actually need
396 foreign_exception = false;
398 // The dwarf unwinder assumes the context structure holds things like the
399 // function and LSDA pointers. The ARM implementation caches these in
400 // the exception header (UCB). To avoid rewriting everything we make the
401 // virtual IP register point at the UCB.
402 ip = (_Unwind_Ptr) ue_header;
403 _Unwind_SetGR(context, 12, ip);
405 __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
407 // Interface version check.
409 return _URC_FATAL_PHASE1_ERROR;
410 foreign_exception = !__is_gxx_exception_class(exception_class);
413 // Shortcut for phase 2 found handler for domestic exception.
414 if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
415 && !foreign_exception)
417 restore_caught_exception(ue_header, handler_switch_value,
418 language_specific_data, landing_pad);
419 found_type = (landing_pad == 0 ? found_terminate : found_handler);
420 goto install_context;
423 language_specific_data = (const unsigned char *)
424 _Unwind_GetLanguageSpecificData (context);
426 // If no LSDA, then there are no handlers or cleanups.
427 if (! language_specific_data)
430 // Parse the LSDA header.
431 p = parse_lsda_header (context, language_specific_data, &info);
432 info.ttype_base = base_of_encoded_value (info.ttype_encoding, context);
433 ip = _Unwind_GetIP (context) - 1;
436 handler_switch_value = 0;
438 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
439 // The given "IP" is an index into the call-site table, with two
440 // exceptions -- -1 means no-action, and 0 means terminate. But
441 // since we're using uleb128 values, we've not got random access
444 return _URC_CONTINUE_UNWIND;
447 // Fall through to set found_terminate.
451 _Unwind_Word cs_lp, cs_action;
454 p = read_uleb128 (p, &cs_lp);
455 p = read_uleb128 (p, &cs_action);
459 // Can never have null landing pad for sjlj -- that would have
460 // been indicated by a -1 call site index.
461 landing_pad = cs_lp + 1;
463 action_record = info.action_table + cs_action - 1;
464 goto found_something;
467 // Search the call-site table for the action associated with this IP.
468 while (p < info.action_table)
470 _Unwind_Ptr cs_start, cs_len, cs_lp;
471 _Unwind_Word cs_action;
473 // Note that all call-site encodings are "absolute" displacements.
474 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
475 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
476 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
477 p = read_uleb128 (p, &cs_action);
479 // The table is sorted, so if we've passed the ip, stop.
480 if (ip < info.Start + cs_start)
481 p = info.action_table;
482 else if (ip < info.Start + cs_start + cs_len)
485 landing_pad = info.LPStart + cs_lp;
487 action_record = info.action_table + cs_action - 1;
488 goto found_something;
491 #endif // _GLIBCXX_SJLJ_EXCEPTIONS
493 // If ip is not present in the table, call terminate. This is for
494 // a destructor inside a cleanup, or a library routine the compiler
495 // was not expecting to throw.
496 found_type = found_terminate;
500 if (landing_pad == 0)
502 // If ip is present, and has a null landing pad, there are
503 // no cleanups or handlers to be run.
504 found_type = found_nothing;
506 else if (action_record == 0)
508 // If ip is present, has a non-null landing pad, and a null
509 // action table offset, then there are only cleanups present.
510 // Cleanups use a zero switch value, as set above.
511 found_type = found_cleanup;
515 // Otherwise we have a catch handler or exception specification.
517 _Unwind_Sword ar_filter, ar_disp;
518 const std::type_info* catch_type;
519 _throw_typet* throw_type;
520 bool saw_cleanup = false;
521 bool saw_handler = false;
523 // During forced unwinding, we only run cleanups. With a foreign
524 // exception class, there's no exception type.
525 // ??? What to do about GNU Java and GNU Ada exceptions.
527 if ((actions & _UA_FORCE_UNWIND)
528 || foreign_exception)
531 #ifdef __ARM_EABI_UNWINDER__
532 throw_type = ue_header;
534 throw_type = xh->exceptionType;
540 p = read_sleb128 (p, &ar_filter);
541 read_sleb128 (p, &ar_disp);
545 // Zero filter values are cleanups.
548 else if (ar_filter > 0)
550 // Positive filter values are handlers.
551 catch_type = get_ttype_entry (&info, ar_filter);
553 // Null catch type is a catch-all handler; we can catch foreign
554 // exceptions with this. Otherwise we must match types.
557 && get_adjusted_ptr (catch_type, throw_type,
566 // Negative filter values are exception specifications.
567 // ??? How do foreign exceptions fit in? As far as I can
568 // see we can't match because there's no __cxa_exception
569 // object to stuff bits in for __cxa_call_unexpected to use.
570 // Allow them iff the exception spec is non-empty. I.e.
571 // a throw() specification results in __unexpected.
573 ? ! check_exception_spec (&info, throw_type, thrown_ptr,
575 : empty_exception_spec (&info, ar_filter))
584 action_record = p + ar_disp;
589 handler_switch_value = ar_filter;
590 found_type = found_handler;
593 found_type = (saw_cleanup ? found_cleanup : found_nothing);
597 if (found_type == found_nothing)
600 if (actions & _UA_SEARCH_PHASE)
602 if (found_type == found_cleanup)
605 // For domestic exceptions, we cache data from phase 1 for phase 2.
606 if (!foreign_exception)
608 save_caught_exception(ue_header, context, thrown_ptr,
609 handler_switch_value, language_specific_data,
610 landing_pad, action_record);
612 return _URC_HANDLER_FOUND;
617 // We can't use any of the cxa routines with foreign exceptions,
618 // because they all expect ue_header to be a struct __cxa_exception.
619 // So in that case, call terminate or unexpected directly.
620 if ((actions & _UA_FORCE_UNWIND)
621 || foreign_exception)
623 if (found_type == found_terminate)
625 else if (handler_switch_value < 0)
628 { std::unexpected (); }
630 { std::terminate (); }
635 if (found_type == found_terminate)
636 __cxa_call_terminate(ue_header);
638 // Cache the TType base value for __cxa_call_unexpected, as we won't
639 // have an _Unwind_Context then.
640 if (handler_switch_value < 0)
642 parse_lsda_header (context, language_specific_data, &info);
644 #ifdef __ARM_EABI_UNWINDER__
645 const _Unwind_Word* e;
648 e = ((const _Unwind_Word*) info.TType) - handler_switch_value - 1;
649 // Count the number of rtti objects.
655 ue_header->barrier_cache.bitpattern[1] = n;
657 ue_header->barrier_cache.bitpattern[2] = 0;
659 ue_header->barrier_cache.bitpattern[3] = 4;
661 ue_header->barrier_cache.bitpattern[4] = (_Unwind_Word) e;
663 xh->catchTemp = base_of_encoded_value (info.ttype_encoding, context);
668 /* For targets with pointers smaller than the word size, we must extend the
669 pointer, and this extension is target dependent. */
670 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
671 __builtin_extend_pointer (ue_header));
672 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
673 handler_switch_value);
674 _Unwind_SetIP (context, landing_pad);
675 #ifdef __ARM_EABI_UNWINDER__
676 if (found_type == found_cleanup)
677 __cxa_begin_cleanup(ue_header);
679 return _URC_INSTALL_CONTEXT;
682 /* The ARM EABI implementation of __cxa_call_unexpected is in a
683 different file so that the personality routine (PR) can be used
684 standalone. The generic routine shared datastructures with the PR
685 so it is most convenient to implement it here. */
686 #ifndef __ARM_EABI_UNWINDER__
688 __cxa_call_unexpected (void *exc_obj_in)
690 _Unwind_Exception *exc_obj
691 = reinterpret_cast <_Unwind_Exception *>(exc_obj_in);
693 __cxa_begin_catch (exc_obj);
695 // This function is a handler for our exception argument. If we exit
696 // by throwing a different exception, we'll need the original cleaned up.
697 struct end_catch_protect
699 end_catch_protect() { }
700 ~end_catch_protect() { __cxa_end_catch(); }
701 } end_catch_protect_obj;
703 lsda_header_info info;
704 __cxa_exception *xh = __get_exception_header_from_ue (exc_obj);
705 const unsigned char *xh_lsda;
706 _Unwind_Sword xh_switch_value;
707 std::terminate_handler xh_terminate_handler;
709 // If the unexpectedHandler rethrows the exception (e.g. to categorize it),
710 // it will clobber data about the current handler. So copy the data out now.
711 xh_lsda = xh->languageSpecificData;
712 xh_switch_value = xh->handlerSwitchValue;
713 xh_terminate_handler = xh->terminateHandler;
714 info.ttype_base = (_Unwind_Ptr) xh->catchTemp;
717 { __unexpected (xh->unexpectedHandler); }
720 // Get the exception thrown from unexpected.
722 __cxa_eh_globals *globals = __cxa_get_globals_fast ();
723 __cxa_exception *new_xh = globals->caughtExceptions;
724 void *new_ptr = new_xh + 1;
726 // We don't quite have enough stuff cached; re-parse the LSDA.
727 parse_lsda_header (0, xh_lsda, &info);
729 // If this new exception meets the exception spec, allow it.
730 if (check_exception_spec (&info, new_xh->exceptionType,
731 new_ptr, xh_switch_value))
732 __throw_exception_again;
734 // If the exception spec allows std::bad_exception, throw that.
735 // We don't have a thrown object to compare against, but since
736 // bad_exception doesn't have virtual bases, that's OK; just pass 0.
738 const std::type_info &bad_exc = typeid (std::bad_exception);
739 if (check_exception_spec (&info, &bad_exc, 0, xh_switch_value))
740 throw std::bad_exception();
744 __terminate (xh_terminate_handler);