2005-06-28 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / libstdc++-v3 / libsupc++ / eh_personality.cc
blobecade83399f305644dfbfff07c0016fe586a5f22
1 // -*- C++ -*- The GNU C++ exception personality routine.
2 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
3 //
4 // This file is part of GCC.
5 //
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)
9 // any later version.
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, 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, 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.
31 #include <bits/c++config.h>
32 #include <cstdlib>
33 #include <exception_defines.h>
34 #include "unwind-cxx.h"
36 using namespace __cxxabiv1;
38 #ifdef __ARM_EABI_UNWINDER__
39 #define NO_SIZE_OF_ENCODED_VALUE
40 #endif
42 #include "unwind-pe.h"
45 struct lsda_header_info
47 _Unwind_Ptr Start;
48 _Unwind_Ptr LPStart;
49 _Unwind_Ptr ttype_base;
50 const unsigned char *TType;
51 const unsigned char *action_table;
52 unsigned char ttype_encoding;
53 unsigned char call_site_encoding;
56 static const unsigned char *
57 parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
58 lsda_header_info *info)
60 _Unwind_Word tmp;
61 unsigned char lpstart_encoding;
63 info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
65 // Find @LPStart, the base to which landing pad offsets are relative.
66 lpstart_encoding = *p++;
67 if (lpstart_encoding != DW_EH_PE_omit)
68 p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
69 else
70 info->LPStart = info->Start;
72 // Find @TType, the base of the handler and exception spec type data.
73 info->ttype_encoding = *p++;
74 if (info->ttype_encoding != DW_EH_PE_omit)
76 p = read_uleb128 (p, &tmp);
77 info->TType = p + tmp;
79 else
80 info->TType = 0;
82 // The encoding and length of the call-site table; the action table
83 // immediately follows.
84 info->call_site_encoding = *p++;
85 p = read_uleb128 (p, &tmp);
86 info->action_table = p + tmp;
88 return p;
91 #ifdef __ARM_EABI_UNWINDER__
93 // Return an element from a type table.
95 static const std::type_info*
96 get_ttype_entry(lsda_header_info* info, _Unwind_Word i)
98 _Unwind_Ptr ptr;
100 ptr = (_Unwind_Ptr) (info->TType - (i * 4));
101 ptr = _Unwind_decode_target2(ptr);
103 return reinterpret_cast<const std::type_info *>(ptr);
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) \
110 != ctm_failed)
112 // Return true if THROW_TYPE matches one if the filter types.
114 static bool
115 check_exception_spec(lsda_header_info* info, _throw_typet* throw_type,
116 void* thrown_ptr, _Unwind_Sword filter_value)
118 const _Unwind_Word* e = ((const _Unwind_Word*) info->TType)
119 - filter_value - 1;
121 while (1)
123 const std::type_info* catch_type;
124 _Unwind_Word tmp;
126 tmp = *e;
128 // Zero signals the end of the list. If we've not found
129 // a match by now, then we've failed the specification.
130 if (tmp == 0)
131 return false;
133 tmp = _Unwind_decode_target2((_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
141 // thrown_ptr here.
142 if (get_adjusted_ptr(catch_type, throw_type, &thrown_ptr))
143 return true;
145 // Advance to the next entry.
146 e++;
151 // Save stage1 handler information in the exception object
153 static inline void
154 save_caught_exception(struct _Unwind_Exception* ue_header,
155 struct _Unwind_Context* context,
156 void* thrown_ptr,
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, 13);
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.
175 static inline void
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 \
188 do \
190 if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
191 return _URC_FAILURE; \
192 return _URC_CONTINUE_UNWIND; \
194 while (0)
196 #else
197 typedef const std::type_info _throw_typet;
200 // Return an element from a type table.
202 static const std::type_info *
203 get_ttype_entry (lsda_header_info *info, _Unwind_Word i)
205 _Unwind_Ptr ptr;
207 i *= size_of_encoded_value (info->ttype_encoding);
208 read_encoded_value_with_base (info->ttype_encoding, info->ttype_base,
209 info->TType - i, &ptr);
211 return reinterpret_cast<const std::type_info *>(ptr);
214 // Given the thrown type THROW_TYPE, pointer to a variable containing a
215 // pointer to the exception object THROWN_PTR_P and a type CATCH_TYPE to
216 // compare against, return whether or not there is a match and if so,
217 // update *THROWN_PTR_P.
219 static bool
220 get_adjusted_ptr (const std::type_info *catch_type,
221 const std::type_info *throw_type,
222 void **thrown_ptr_p)
224 void *thrown_ptr = *thrown_ptr_p;
226 // Pointer types need to adjust the actual pointer, not
227 // the pointer to pointer that is the exception object.
228 // This also has the effect of passing pointer types
229 // "by value" through the __cxa_begin_catch return value.
230 if (throw_type->__is_pointer_p ())
231 thrown_ptr = *(void **) thrown_ptr;
233 if (catch_type->__do_catch (throw_type, &thrown_ptr, 1))
235 *thrown_ptr_p = thrown_ptr;
236 return true;
239 return false;
242 // Return true if THROW_TYPE matches one if the filter types.
244 static bool
245 check_exception_spec(lsda_header_info* info, _throw_typet* throw_type,
246 void* thrown_ptr, _Unwind_Sword filter_value)
248 const unsigned char *e = info->TType - filter_value - 1;
250 while (1)
252 const std::type_info *catch_type;
253 _Unwind_Word tmp;
255 e = read_uleb128 (e, &tmp);
257 // Zero signals the end of the list. If we've not found
258 // a match by now, then we've failed the specification.
259 if (tmp == 0)
260 return false;
262 // Match a ttype entry.
263 catch_type = get_ttype_entry (info, tmp);
265 // ??? There is currently no way to ask the RTTI code about the
266 // relationship between two types without reference to a specific
267 // object. There should be; then we wouldn't need to mess with
268 // thrown_ptr here.
269 if (get_adjusted_ptr (catch_type, throw_type, &thrown_ptr))
270 return true;
275 // Save stage1 handler information in the exception object
277 static inline void
278 save_caught_exception(struct _Unwind_Exception* ue_header,
279 struct _Unwind_Context* context
280 __attribute__((__unused__)),
281 void* thrown_ptr,
282 int handler_switch_value,
283 const unsigned char* language_specific_data,
284 _Unwind_Ptr landing_pad __attribute__((__unused__)),
285 const unsigned char* action_record)
287 __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
289 xh->handlerSwitchValue = handler_switch_value;
290 xh->actionRecord = action_record;
291 xh->languageSpecificData = language_specific_data;
292 xh->adjustedPtr = thrown_ptr;
294 // ??? Completely unknown what this field is supposed to be for.
295 // ??? Need to cache TType encoding base for call_unexpected.
296 xh->catchTemp = landing_pad;
300 // Restore the catch handler information saved during phase1.
302 static inline void
303 restore_caught_exception(struct _Unwind_Exception* ue_header,
304 int& handler_switch_value,
305 const unsigned char*& language_specific_data,
306 _Unwind_Ptr& landing_pad)
308 __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
309 handler_switch_value = xh->handlerSwitchValue;
310 language_specific_data = xh->languageSpecificData;
311 landing_pad = (_Unwind_Ptr) xh->catchTemp;
314 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
316 #endif // !__ARM_EABI_UNWINDER__
318 // Return true if the filter spec is empty, ie throw().
320 static bool
321 empty_exception_spec (lsda_header_info *info, _Unwind_Sword filter_value)
323 const unsigned char *e = info->TType - filter_value - 1;
324 _Unwind_Word tmp;
326 e = read_uleb128 (e, &tmp);
327 return tmp == 0;
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
335 #else
336 #define PERSONALITY_FUNCTION __gxx_personality_v0
337 #endif
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)
344 #else
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)
350 #endif
352 enum found_handler_type
354 found_nothing,
355 found_terminate,
356 found_cleanup,
357 found_handler
358 } found_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 = ue_header + 1;
367 bool foreign_exception;
369 #ifdef __ARM_EABI_UNWINDER__
370 _Unwind_Action actions;
372 switch (state)
374 case _US_VIRTUAL_UNWIND_FRAME:
375 actions = _UA_SEARCH_PHASE;
376 break;
378 case _US_UNWIND_FRAME_STARTING:
379 actions = _UA_CLEANUP_PHASE;
380 if (ue_header->barrier_cache.sp == _Unwind_GetGR(context, 13))
381 actions |= _UA_HANDLER_FRAME;
382 break;
384 case _US_UNWIND_FRAME_RESUME:
385 CONTINUE_UNWINDING;
386 break;
388 default:
389 abort();
392 // We don't know which runtime we're working with, so can't check this.
393 // However the ABI routines hide this from us, and we don't actually need
394 // to know.
395 foreign_exception = false;
397 // The dwarf unwinder assumes the context structure holds things like the
398 // function and LSDA pointers. The ARM implementation caches these in
399 // the exception header (UCB). To avoid rewriting everything we make the
400 // virtual IP register point at the UCB.
401 ip = (_Unwind_Ptr) ue_header;
402 _Unwind_SetGR(context, 12, ip);
403 #else
404 __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
406 // Interface version check.
407 if (version != 1)
408 return _URC_FATAL_PHASE1_ERROR;
409 foreign_exception = !__is_gxx_exception_class(exception_class);
410 #endif
412 // Shortcut for phase 2 found handler for domestic exception.
413 if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
414 && !foreign_exception)
416 restore_caught_exception(ue_header, handler_switch_value,
417 language_specific_data, landing_pad);
418 found_type = (landing_pad == 0 ? found_terminate : found_handler);
419 goto install_context;
422 language_specific_data = (const unsigned char *)
423 _Unwind_GetLanguageSpecificData (context);
425 // If no LSDA, then there are no handlers or cleanups.
426 if (! language_specific_data)
427 CONTINUE_UNWINDING;
429 // Parse the LSDA header.
430 p = parse_lsda_header (context, language_specific_data, &info);
431 info.ttype_base = 0;
432 ip = _Unwind_GetIP (context) - 1;
433 landing_pad = 0;
434 action_record = 0;
435 handler_switch_value = 0;
437 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
438 // The given "IP" is an index into the call-site table, with two
439 // exceptions -- -1 means no-action, and 0 means terminate. But
440 // since we're using uleb128 values, we've not got random access
441 // to the array.
442 if ((int) ip < 0)
443 return _URC_CONTINUE_UNWIND;
444 else if (ip == 0)
446 // Fall through to set found_terminate.
448 else
450 _Unwind_Word cs_lp, cs_action;
453 p = read_uleb128 (p, &cs_lp);
454 p = read_uleb128 (p, &cs_action);
456 while (--ip);
458 // Can never have null landing pad for sjlj -- that would have
459 // been indicated by a -1 call site index.
460 landing_pad = cs_lp + 1;
461 if (cs_action)
462 action_record = info.action_table + cs_action - 1;
463 goto found_something;
465 #else
466 // Search the call-site table for the action associated with this IP.
467 while (p < info.action_table)
469 _Unwind_Ptr cs_start, cs_len, cs_lp;
470 _Unwind_Word cs_action;
472 // Note that all call-site encodings are "absolute" displacements.
473 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
474 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
475 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
476 p = read_uleb128 (p, &cs_action);
478 // The table is sorted, so if we've passed the ip, stop.
479 if (ip < info.Start + cs_start)
480 p = info.action_table;
481 else if (ip < info.Start + cs_start + cs_len)
483 if (cs_lp)
484 landing_pad = info.LPStart + cs_lp;
485 if (cs_action)
486 action_record = info.action_table + cs_action - 1;
487 goto found_something;
490 #endif // _GLIBCXX_SJLJ_EXCEPTIONS
492 // If ip is not present in the table, call terminate. This is for
493 // a destructor inside a cleanup, or a library routine the compiler
494 // was not expecting to throw.
495 found_type = found_terminate;
496 goto do_something;
498 found_something:
499 if (landing_pad == 0)
501 // If ip is present, and has a null landing pad, there are
502 // no cleanups or handlers to be run.
503 found_type = found_nothing;
505 else if (action_record == 0)
507 // If ip is present, has a non-null landing pad, and a null
508 // action table offset, then there are only cleanups present.
509 // Cleanups use a zero switch value, as set above.
510 found_type = found_cleanup;
512 else
514 // Otherwise we have a catch handler or exception specification.
516 _Unwind_Sword ar_filter, ar_disp;
517 const std::type_info* catch_type;
518 _throw_typet* throw_type;
519 bool saw_cleanup = false;
520 bool saw_handler = false;
522 // During forced unwinding, we only run cleanups. With a foreign
523 // exception class, there's no exception type.
524 // ??? What to do about GNU Java and GNU Ada exceptions.
526 #ifdef __ARM_EABI_UNWINDER__
527 throw_type = ue_header;
528 #else
529 if ((actions & _UA_FORCE_UNWIND)
530 || foreign_exception)
531 throw_type = 0;
532 else
533 throw_type = xh->exceptionType;
534 #endif
536 while (1)
538 p = action_record;
539 p = read_sleb128 (p, &ar_filter);
540 read_sleb128 (p, &ar_disp);
542 if (ar_filter == 0)
544 // Zero filter values are cleanups.
545 saw_cleanup = true;
547 else if (ar_filter > 0)
549 // Positive filter values are handlers.
550 catch_type = get_ttype_entry (&info, ar_filter);
552 // Null catch type is a catch-all handler; we can catch foreign
553 // exceptions with this. Otherwise we must match types.
554 if (! catch_type
555 || (throw_type
556 && get_adjusted_ptr (catch_type, throw_type,
557 &thrown_ptr)))
559 saw_handler = true;
560 break;
563 else
565 // Negative filter values are exception specifications.
566 // ??? How do foreign exceptions fit in? As far as I can
567 // see we can't match because there's no __cxa_exception
568 // object to stuff bits in for __cxa_call_unexpected to use.
569 // Allow them iff the exception spec is non-empty. I.e.
570 // a throw() specification results in __unexpected.
571 if (throw_type
572 ? ! check_exception_spec (&info, throw_type, thrown_ptr,
573 ar_filter)
574 : empty_exception_spec (&info, ar_filter))
576 saw_handler = true;
577 break;
581 if (ar_disp == 0)
582 break;
583 action_record = p + ar_disp;
586 if (saw_handler)
588 handler_switch_value = ar_filter;
589 found_type = found_handler;
591 else
592 found_type = (saw_cleanup ? found_cleanup : found_nothing);
595 do_something:
596 if (found_type == found_nothing)
597 CONTINUE_UNWINDING;
599 if (actions & _UA_SEARCH_PHASE)
601 if (found_type == found_cleanup)
602 CONTINUE_UNWINDING;
604 // For domestic exceptions, we cache data from phase 1 for phase 2.
605 if (!foreign_exception)
607 save_caught_exception(ue_header, context, thrown_ptr,
608 handler_switch_value, language_specific_data,
609 landing_pad, action_record);
611 return _URC_HANDLER_FOUND;
614 install_context:
616 #ifndef __ARM_EABI_UNWINDER__
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)
624 std::terminate ();
625 else if (handler_switch_value < 0)
627 try
628 { std::unexpected (); }
629 catch(...)
630 { std::terminate (); }
633 else
634 #endif
636 if (found_type == found_terminate)
637 __cxa_call_terminate(ue_header);
639 // Cache the TType base value for __cxa_call_unexpected, as we won't
640 // have an _Unwind_Context then.
641 if (handler_switch_value < 0)
643 parse_lsda_header (context, language_specific_data, &info);
645 #ifdef __ARM_EABI_UNWINDER__
646 const _Unwind_Word* e;
647 _Unwind_Word n;
649 e = ((const _Unwind_Word*) info.TType) - handler_switch_value - 1;
650 // Count the number of rtti objects.
651 n = 0;
652 while (e[n] != 0)
653 n++;
655 // Count.
656 ue_header->barrier_cache.bitpattern[1] = n;
657 // Base (obsolete)
658 ue_header->barrier_cache.bitpattern[2] = 0;
659 // Stride.
660 ue_header->barrier_cache.bitpattern[3] = 4;
661 // List head.
662 ue_header->barrier_cache.bitpattern[4] = (_Unwind_Word) e;
663 #else
664 xh->catchTemp = base_of_encoded_value (info.ttype_encoding, context);
665 #endif
669 /* For targets with pointers smaller than the word size, we must extend the
670 pointer, and this extension is target dependent. */
671 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
672 __builtin_extend_pointer (ue_header));
673 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
674 handler_switch_value);
675 _Unwind_SetIP (context, landing_pad);
676 #ifdef __ARM_EABI_UNWINDER__
677 if (found_type == found_cleanup)
678 __cxa_begin_cleanup(ue_header);
679 #endif
680 return _URC_INSTALL_CONTEXT;
683 /* The ARM EABI implementation of __cxa_call_unexpected is in a different
684 file so that the personality routine san be used standalone. The generic
685 routine sahred datastructures with the PR so it is most convenient to
686 implement it here. */
687 #ifndef __ARM_EABI_UNWINDER__
688 extern "C" void
689 __cxa_call_unexpected (void *exc_obj_in)
691 _Unwind_Exception *exc_obj
692 = reinterpret_cast <_Unwind_Exception *>(exc_obj_in);
694 __cxa_begin_catch (exc_obj);
696 // This function is a handler for our exception argument. If we exit
697 // by throwing a different exception, we'll need the original cleaned up.
698 struct end_catch_protect
700 end_catch_protect() { }
701 ~end_catch_protect() { __cxa_end_catch(); }
702 } end_catch_protect_obj;
704 lsda_header_info info;
705 __cxa_exception *xh = __get_exception_header_from_ue (exc_obj);
706 const unsigned char *xh_lsda;
707 _Unwind_Sword xh_switch_value;
708 std::terminate_handler xh_terminate_handler;
710 // If the unexpectedHandler rethrows the exception (e.g. to categorize it),
711 // it will clobber data about the current handler. So copy the data out now.
712 xh_lsda = xh->languageSpecificData;
713 xh_switch_value = xh->handlerSwitchValue;
714 xh_terminate_handler = xh->terminateHandler;
715 info.ttype_base = (_Unwind_Ptr) xh->catchTemp;
717 try
718 { __unexpected (xh->unexpectedHandler); }
719 catch(...)
721 // Get the exception thrown from unexpected.
723 __cxa_eh_globals *globals = __cxa_get_globals_fast ();
724 __cxa_exception *new_xh = globals->caughtExceptions;
725 void *new_ptr = new_xh + 1;
727 // We don't quite have enough stuff cached; re-parse the LSDA.
728 parse_lsda_header (0, xh_lsda, &info);
730 // If this new exception meets the exception spec, allow it.
731 if (check_exception_spec (&info, new_xh->exceptionType,
732 new_ptr, xh_switch_value))
733 __throw_exception_again;
735 // If the exception spec allows std::bad_exception, throw that.
736 // We don't have a thrown object to compare against, but since
737 // bad_exception doesn't have virtual bases, that's OK; just pass 0.
738 #ifdef __EXCEPTIONS
739 const std::type_info &bad_exc = typeid (std::bad_exception);
740 if (check_exception_spec (&info, &bad_exc, 0, xh_switch_value))
741 throw std::bad_exception();
742 #endif
744 // Otherwise, die.
745 __terminate (xh_terminate_handler);
748 #endif