1 // -*- C++ -*- The GNU C++ exception personality routine.
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // Free Software Foundation, Inc.
6 // This file is part of GCC.
8 // GCC is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3, or (at your option)
13 // GCC is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 #include <bits/c++config.h>
29 #include <bits/exception_defines.h>
31 #include "unwind-cxx.h"
34 using namespace __cxxabiv1;
36 #include "unwind-pe.h"
39 struct lsda_header_info
43 _Unwind_Ptr ttype_base;
44 const unsigned char *TType;
45 const unsigned char *action_table;
46 unsigned char ttype_encoding;
47 unsigned char call_site_encoding;
50 static const unsigned char *
51 parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
52 lsda_header_info *info)
55 unsigned char lpstart_encoding;
57 info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
59 // Find @LPStart, the base to which landing pad offsets are relative.
60 lpstart_encoding = *p++;
61 if (lpstart_encoding != DW_EH_PE_omit)
62 p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
64 info->LPStart = info->Start;
66 // Find @TType, the base of the handler and exception spec type data.
67 info->ttype_encoding = *p++;
68 if (info->ttype_encoding != DW_EH_PE_omit)
70 #if _GLIBCXX_OVERRIDE_TTYPE_ENCODING
71 /* Older ARM EABI toolchains set this value incorrectly, so use a
72 hardcoded OS-specific format. */
73 info->ttype_encoding = _GLIBCXX_OVERRIDE_TTYPE_ENCODING;
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 // Return an element from a type table.
92 static const std::type_info *
93 get_ttype_entry (lsda_header_info *info, _uleb128_t i)
97 i *= size_of_encoded_value (info->ttype_encoding);
98 read_encoded_value_with_base (info->ttype_encoding, info->ttype_base,
99 info->TType - i, &ptr);
101 return reinterpret_cast<const std::type_info *>(ptr);
104 #ifdef __ARM_EABI_UNWINDER__
106 // The ABI provides a routine for matching exception object types.
107 typedef _Unwind_Control_Block _throw_typet;
108 #define get_adjusted_ptr(catch_type, throw_type, thrown_ptr_p) \
109 (__cxa_type_match (throw_type, catch_type, false, thrown_ptr_p) \
112 // Return true if THROW_TYPE matches one if the filter types.
115 check_exception_spec(lsda_header_info* info, _throw_typet* throw_type,
116 void* thrown_ptr, _sleb128_t filter_value)
118 const _uleb128_t* e = ((const _uleb128_t*) info->TType)
123 const std::type_info* catch_type;
128 // Zero signals the end of the list. If we've not found
129 // a match by now, then we've failed the specification.
133 tmp = _Unwind_decode_typeinfo_ptr(info->ttype_base, (_Unwind_Word) e);
135 // Match a ttype entry.
136 catch_type = reinterpret_cast<const std::type_info*>(tmp);
138 // ??? There is currently no way to ask the RTTI code about the
139 // relationship between two types without reference to a specific
140 // object. There should be; then we wouldn't need to mess with
142 if (get_adjusted_ptr(catch_type, throw_type, &thrown_ptr))
145 // Advance to the next entry.
151 // Save stage1 handler information in the exception object
154 save_caught_exception(struct _Unwind_Exception* ue_header,
155 struct _Unwind_Context* context,
157 int handler_switch_value,
158 const unsigned char* language_specific_data,
159 _Unwind_Ptr landing_pad,
160 const unsigned char* action_record
161 __attribute__((__unused__)))
163 ue_header->barrier_cache.sp = _Unwind_GetGR(context, UNWIND_STACK_REG);
164 ue_header->barrier_cache.bitpattern[0] = (_uw) thrown_ptr;
165 ue_header->barrier_cache.bitpattern[1]
166 = (_uw) handler_switch_value;
167 ue_header->barrier_cache.bitpattern[2]
168 = (_uw) language_specific_data;
169 ue_header->barrier_cache.bitpattern[3] = (_uw) landing_pad;
173 // Restore the catch handler data saved during phase1.
176 restore_caught_exception(struct _Unwind_Exception* ue_header,
177 int& handler_switch_value,
178 const unsigned char*& language_specific_data,
179 _Unwind_Ptr& landing_pad)
181 handler_switch_value = (int) ue_header->barrier_cache.bitpattern[1];
182 language_specific_data =
183 (const unsigned char*) ue_header->barrier_cache.bitpattern[2];
184 landing_pad = (_Unwind_Ptr) ue_header->barrier_cache.bitpattern[3];
187 #define CONTINUE_UNWINDING \
190 if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
191 return _URC_FAILURE; \
192 return _URC_CONTINUE_UNWIND; \
196 // Return true if the filter spec is empty, ie throw().
199 empty_exception_spec (lsda_header_info *info, _Unwind_Sword filter_value)
201 const _Unwind_Word* e = ((const _Unwind_Word*) info->TType)
208 typedef const std::type_info _throw_typet;
211 // Given the thrown type THROW_TYPE, pointer to a variable containing a
212 // pointer to the exception object THROWN_PTR_P and a type CATCH_TYPE to
213 // compare against, return whether or not there is a match and if so,
214 // update *THROWN_PTR_P.
217 get_adjusted_ptr (const std::type_info *catch_type,
218 const std::type_info *throw_type,
221 void *thrown_ptr = *thrown_ptr_p;
223 // Pointer types need to adjust the actual pointer, not
224 // the pointer to pointer that is the exception object.
225 // This also has the effect of passing pointer types
226 // "by value" through the __cxa_begin_catch return value.
227 if (throw_type->__is_pointer_p ())
228 thrown_ptr = *(void **) thrown_ptr;
230 if (catch_type->__do_catch (throw_type, &thrown_ptr, 1))
232 *thrown_ptr_p = thrown_ptr;
239 // Return true if THROW_TYPE matches one if the filter types.
242 check_exception_spec(lsda_header_info* info, _throw_typet* throw_type,
243 void* thrown_ptr, _sleb128_t filter_value)
245 const unsigned char *e = info->TType - filter_value - 1;
249 const std::type_info *catch_type;
252 e = read_uleb128 (e, &tmp);
254 // Zero signals the end of the list. If we've not found
255 // a match by now, then we've failed the specification.
259 // Match a ttype entry.
260 catch_type = get_ttype_entry (info, tmp);
262 // ??? There is currently no way to ask the RTTI code about the
263 // relationship between two types without reference to a specific
264 // object. There should be; then we wouldn't need to mess with
266 if (get_adjusted_ptr (catch_type, throw_type, &thrown_ptr))
272 // Save stage1 handler information in the exception object
275 save_caught_exception(struct _Unwind_Exception* ue_header,
276 struct _Unwind_Context* context
277 __attribute__((__unused__)),
279 int handler_switch_value,
280 const unsigned char* language_specific_data,
281 _Unwind_Ptr landing_pad __attribute__((__unused__)),
282 const unsigned char* action_record)
284 __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
286 xh->handlerSwitchValue = handler_switch_value;
287 xh->actionRecord = action_record;
288 xh->languageSpecificData = language_specific_data;
289 xh->adjustedPtr = thrown_ptr;
291 // ??? Completely unknown what this field is supposed to be for.
292 // ??? Need to cache TType encoding base for call_unexpected.
293 xh->catchTemp = landing_pad;
297 // Restore the catch handler information saved during phase1.
300 restore_caught_exception(struct _Unwind_Exception* ue_header,
301 int& handler_switch_value,
302 const unsigned char*& language_specific_data,
303 _Unwind_Ptr& landing_pad)
305 __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
306 handler_switch_value = xh->handlerSwitchValue;
307 language_specific_data = xh->languageSpecificData;
308 landing_pad = (_Unwind_Ptr) xh->catchTemp;
311 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
313 // Return true if the filter spec is empty, ie throw().
316 empty_exception_spec (lsda_header_info *info, _Unwind_Sword filter_value)
318 const unsigned char *e = info->TType - filter_value - 1;
321 e = read_uleb128 (e, &tmp);
325 #endif // !__ARM_EABI_UNWINDER__
330 // Using a different personality function name causes link failures
331 // when trying to mix code using different exception handling models.
332 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
333 #define PERSONALITY_FUNCTION __gxx_personality_sj0
334 #define __builtin_eh_return_data_regno(x) x
336 #define PERSONALITY_FUNCTION __gxx_personality_v0
339 extern "C" _Unwind_Reason_Code
340 #ifdef __ARM_EABI_UNWINDER__
341 PERSONALITY_FUNCTION (_Unwind_State state,
342 struct _Unwind_Exception* ue_header,
343 struct _Unwind_Context* context)
345 PERSONALITY_FUNCTION (int version,
346 _Unwind_Action actions,
347 _Unwind_Exception_Class exception_class,
348 struct _Unwind_Exception *ue_header,
349 struct _Unwind_Context *context)
352 enum found_handler_type
360 lsda_header_info info;
361 const unsigned char *language_specific_data;
362 const unsigned char *action_record;
363 const unsigned char *p;
364 _Unwind_Ptr landing_pad, ip;
365 int handler_switch_value;
366 void* thrown_ptr = 0;
367 bool foreign_exception;
368 int ip_before_insn = 0;
370 #ifdef __ARM_EABI_UNWINDER__
371 _Unwind_Action actions;
373 switch (state & _US_ACTION_MASK)
375 case _US_VIRTUAL_UNWIND_FRAME:
376 actions = _UA_SEARCH_PHASE;
379 case _US_UNWIND_FRAME_STARTING:
380 actions = _UA_CLEANUP_PHASE;
381 if (!(state & _US_FORCE_UNWIND)
382 && ue_header->barrier_cache.sp == _Unwind_GetGR(context,
384 actions |= _UA_HANDLER_FRAME;
387 case _US_UNWIND_FRAME_RESUME:
394 actions |= state & _US_FORCE_UNWIND;
396 // We don't know which runtime we're working with, so can't check this.
397 // However the ABI routines hide this from us, and we don't actually need
399 foreign_exception = false;
401 // The dwarf unwinder assumes the context structure holds things like the
402 // function and LSDA pointers. The ARM implementation caches these in
403 // the exception header (UCB). To avoid rewriting everything we make a
404 // virtual scratch register point at the UCB.
405 ip = (_Unwind_Ptr) ue_header;
406 _Unwind_SetGR(context, UNWIND_POINTER_REG, ip);
408 __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
410 // Interface version check.
412 return _URC_FATAL_PHASE1_ERROR;
413 foreign_exception = !__is_gxx_exception_class(exception_class);
416 // Shortcut for phase 2 found handler for domestic exception.
417 if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
418 && !foreign_exception)
420 restore_caught_exception(ue_header, handler_switch_value,
421 language_specific_data, landing_pad);
422 found_type = (landing_pad == 0 ? found_terminate : found_handler);
423 goto install_context;
426 language_specific_data = (const unsigned char *)
427 _Unwind_GetLanguageSpecificData (context);
429 // If no LSDA, then there are no handlers or cleanups.
430 if (! language_specific_data)
433 // Parse the LSDA header.
434 p = parse_lsda_header (context, language_specific_data, &info);
435 info.ttype_base = base_of_encoded_value (info.ttype_encoding, context);
436 #ifdef _GLIBCXX_HAVE_GETIPINFO
437 ip = _Unwind_GetIPInfo (context, &ip_before_insn);
439 ip = _Unwind_GetIP (context);
441 if (! ip_before_insn)
445 handler_switch_value = 0;
447 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
448 // The given "IP" is an index into the call-site table, with two
449 // exceptions -- -1 means no-action, and 0 means terminate. But
450 // since we're using uleb128 values, we've not got random access
453 return _URC_CONTINUE_UNWIND;
456 // Fall through to set found_terminate.
460 _uleb128_t cs_lp, cs_action;
463 p = read_uleb128 (p, &cs_lp);
464 p = read_uleb128 (p, &cs_action);
468 // Can never have null landing pad for sjlj -- that would have
469 // been indicated by a -1 call site index.
470 landing_pad = cs_lp + 1;
472 action_record = info.action_table + cs_action - 1;
473 goto found_something;
476 // Search the call-site table for the action associated with this IP.
477 while (p < info.action_table)
479 _Unwind_Ptr cs_start, cs_len, cs_lp;
480 _uleb128_t cs_action;
482 // Note that all call-site encodings are "absolute" displacements.
483 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
484 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
485 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
486 p = read_uleb128 (p, &cs_action);
488 // The table is sorted, so if we've passed the ip, stop.
489 if (ip < info.Start + cs_start)
490 p = info.action_table;
491 else if (ip < info.Start + cs_start + cs_len)
494 landing_pad = info.LPStart + cs_lp;
496 action_record = info.action_table + cs_action - 1;
497 goto found_something;
500 #endif // _GLIBCXX_SJLJ_EXCEPTIONS
502 // If ip is not present in the table, call terminate. This is for
503 // a destructor inside a cleanup, or a library routine the compiler
504 // was not expecting to throw.
505 found_type = found_terminate;
509 if (landing_pad == 0)
511 // If ip is present, and has a null landing pad, there are
512 // no cleanups or handlers to be run.
513 found_type = found_nothing;
515 else if (action_record == 0)
517 // If ip is present, has a non-null landing pad, and a null
518 // action table offset, then there are only cleanups present.
519 // Cleanups use a zero switch value, as set above.
520 found_type = found_cleanup;
524 // Otherwise we have a catch handler or exception specification.
526 _sleb128_t ar_filter, ar_disp;
527 const std::type_info* catch_type;
528 _throw_typet* throw_type;
529 bool saw_cleanup = false;
530 bool saw_handler = false;
532 #ifdef __ARM_EABI_UNWINDER__
533 // ??? How does this work - more importantly, how does it interact with
534 // dependent exceptions?
535 throw_type = ue_header;
536 if (actions & _UA_FORCE_UNWIND)
538 __GXX_INIT_FORCED_UNWIND_CLASS(ue_header->exception_class);
540 else if (!foreign_exception)
541 thrown_ptr = __get_object_from_ue (ue_header);
544 // During forced unwinding, match a magic exception type.
545 if (actions & _UA_FORCE_UNWIND)
547 throw_type = &typeid(abi::__forced_unwind);
549 // With a foreign exception class, there's no exception type.
550 // ??? What to do about GNU Java and GNU Ada exceptions?
551 else if (foreign_exception)
553 throw_type = &typeid(abi::__foreign_exception);
558 thrown_ptr = __get_object_from_ue (ue_header);
559 throw_type = __get_exception_header_from_obj
560 (thrown_ptr)->exceptionType;
567 p = read_sleb128 (p, &ar_filter);
568 read_sleb128 (p, &ar_disp);
572 // Zero filter values are cleanups.
575 else if (ar_filter > 0)
577 // Positive filter values are handlers.
578 catch_type = get_ttype_entry (&info, ar_filter);
580 // Null catch type is a catch-all handler; we can catch foreign
581 // exceptions with this. Otherwise we must match types.
584 && get_adjusted_ptr (catch_type, throw_type,
593 // Negative filter values are exception specifications.
594 // ??? How do foreign exceptions fit in? As far as I can
595 // see we can't match because there's no __cxa_exception
596 // object to stuff bits in for __cxa_call_unexpected to use.
597 // Allow them iff the exception spec is non-empty. I.e.
598 // a throw() specification results in __unexpected.
600 && !(actions & _UA_FORCE_UNWIND)
601 && !foreign_exception)
602 ? ! check_exception_spec (&info, throw_type, thrown_ptr,
604 : empty_exception_spec (&info, ar_filter))
613 action_record = p + ar_disp;
618 handler_switch_value = ar_filter;
619 found_type = found_handler;
622 found_type = (saw_cleanup ? found_cleanup : found_nothing);
626 if (found_type == found_nothing)
629 if (actions & _UA_SEARCH_PHASE)
631 if (found_type == found_cleanup)
634 // For domestic exceptions, we cache data from phase 1 for phase 2.
635 if (!foreign_exception)
637 save_caught_exception(ue_header, context, thrown_ptr,
638 handler_switch_value, language_specific_data,
639 landing_pad, action_record);
641 return _URC_HANDLER_FOUND;
646 // We can't use any of the cxa routines with foreign exceptions,
647 // because they all expect ue_header to be a struct __cxa_exception.
648 // So in that case, call terminate or unexpected directly.
649 if ((actions & _UA_FORCE_UNWIND)
650 || foreign_exception)
652 if (found_type == found_terminate)
654 else if (handler_switch_value < 0)
657 { std::unexpected (); }
659 { std::terminate (); }
664 if (found_type == found_terminate)
665 __cxa_call_terminate(ue_header);
667 // Cache the TType base value for __cxa_call_unexpected, as we won't
668 // have an _Unwind_Context then.
669 if (handler_switch_value < 0)
671 parse_lsda_header (context, language_specific_data, &info);
672 info.ttype_base = base_of_encoded_value (info.ttype_encoding,
675 #ifdef __ARM_EABI_UNWINDER__
676 const _Unwind_Word* e;
679 e = ((const _Unwind_Word*) info.TType) - handler_switch_value - 1;
680 // Count the number of rtti objects.
686 ue_header->barrier_cache.bitpattern[1] = n;
688 ue_header->barrier_cache.bitpattern[2] = info.ttype_base;
690 ue_header->barrier_cache.bitpattern[3] = 4;
692 ue_header->barrier_cache.bitpattern[4] = (_Unwind_Word) e;
694 xh->catchTemp = base_of_encoded_value (info.ttype_encoding, context);
699 /* For targets with pointers smaller than the word size, we must extend the
700 pointer, and this extension is target dependent. */
701 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
702 __builtin_extend_pointer (ue_header));
703 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
704 handler_switch_value);
705 _Unwind_SetIP (context, landing_pad);
706 #ifdef __ARM_EABI_UNWINDER__
707 if (found_type == found_cleanup)
708 __cxa_begin_cleanup(ue_header);
710 return _URC_INSTALL_CONTEXT;
713 /* The ARM EABI implementation of __cxa_call_unexpected is in a
714 different file so that the personality routine (PR) can be used
715 standalone. The generic routine shared datastructures with the PR
716 so it is most convenient to implement it here. */
717 #ifndef __ARM_EABI_UNWINDER__
719 __cxa_call_unexpected (void *exc_obj_in)
721 _Unwind_Exception *exc_obj
722 = reinterpret_cast <_Unwind_Exception *>(exc_obj_in);
724 __cxa_begin_catch (exc_obj);
726 // This function is a handler for our exception argument. If we exit
727 // by throwing a different exception, we'll need the original cleaned up.
728 struct end_catch_protect
730 end_catch_protect() { }
731 ~end_catch_protect() { __cxa_end_catch(); }
732 } end_catch_protect_obj;
734 lsda_header_info info;
735 __cxa_exception *xh = __get_exception_header_from_ue (exc_obj);
736 const unsigned char *xh_lsda;
737 _Unwind_Sword xh_switch_value;
738 std::terminate_handler xh_terminate_handler;
740 // If the unexpectedHandler rethrows the exception (e.g. to categorize it),
741 // it will clobber data about the current handler. So copy the data out now.
742 xh_lsda = xh->languageSpecificData;
743 xh_switch_value = xh->handlerSwitchValue;
744 xh_terminate_handler = xh->terminateHandler;
745 info.ttype_base = (_Unwind_Ptr) xh->catchTemp;
748 { __unexpected (xh->unexpectedHandler); }
751 // Get the exception thrown from unexpected.
753 __cxa_eh_globals *globals = __cxa_get_globals_fast ();
754 __cxa_exception *new_xh = globals->caughtExceptions;
755 void *new_ptr = __get_object_from_ambiguous_exception (new_xh);
757 // We don't quite have enough stuff cached; re-parse the LSDA.
758 parse_lsda_header (0, xh_lsda, &info);
760 // If this new exception meets the exception spec, allow it.
761 if (check_exception_spec (&info, __get_exception_header_from_obj
762 (new_ptr)->exceptionType,
763 new_ptr, xh_switch_value))
764 __throw_exception_again;
766 // If the exception spec allows std::bad_exception, throw that.
767 // We don't have a thrown object to compare against, but since
768 // bad_exception doesn't have virtual bases, that's OK; just pass 0.
769 #if defined(__EXCEPTIONS) && defined(__GXX_RTTI)
770 const std::type_info &bad_exc = typeid (std::bad_exception);
771 if (check_exception_spec (&info, &bad_exc, 0, xh_switch_value))
772 throw std::bad_exception();
776 __terminate (xh_terminate_handler);
781 } // namespace __cxxabiv1