OSDN Git Service

2007-01-24 Andrew Pinski <andrew_pinski@playstation.sony.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / libsupc++ / eh_personality.cc
1 // -*- C++ -*- The GNU C++ exception personality routine.
2 // Copyright (C) 2001, 2002, 2003, 2006 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.
10 //
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.
15 //
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.
20
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.
29
30 #include <bits/c++config.h>
31 #include <cstdlib>
32 #include <exception_defines.h>
33 #include "unwind-cxx.h"
34
35 using namespace __cxxabiv1;
36
37 #ifdef __ARM_EABI_UNWINDER__
38 #define NO_SIZE_OF_ENCODED_VALUE
39 #endif
40
41 #include "unwind-pe.h"
42
43 \f
44 struct lsda_header_info
45 {
46   _Unwind_Ptr Start;
47   _Unwind_Ptr LPStart;
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;
53 };
54
55 static const unsigned char *
56 parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
57                    lsda_header_info *info)
58 {
59   _uleb128_t tmp;
60   unsigned char lpstart_encoding;
61
62   info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
63
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);
68   else
69     info->LPStart = info->Start;
70
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)
74     {
75       p = read_uleb128 (p, &tmp);
76       info->TType = p + tmp;
77     }
78   else
79     info->TType = 0;
80
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;
86
87   return p;
88 }
89
90 #ifdef __ARM_EABI_UNWINDER__
91
92 // Return an element from a type table.
93
94 static const std::type_info*
95 get_ttype_entry(lsda_header_info* info, _uleb128_t i)
96 {
97   _Unwind_Ptr ptr;
98
99   ptr = (_Unwind_Ptr) (info->TType - (i * 4));
100   ptr = _Unwind_decode_target2(ptr);
101   
102   return reinterpret_cast<const std::type_info *>(ptr);
103 }
104
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) \
109    != ctm_failed)
110
111 // Return true if THROW_TYPE matches one if the filter types.
112
113 static bool
114 check_exception_spec(lsda_header_info* info, _throw_typet* throw_type,
115                      void* thrown_ptr, _sleb128_t filter_value)
116 {
117   const _uleb128_t* e = ((const _uleb128_t*) info->TType)
118                           - filter_value - 1;
119
120   while (1)
121     {
122       const std::type_info* catch_type;
123       _uleb128_t tmp;
124
125       tmp = *e;
126       
127       // Zero signals the end of the list.  If we've not found
128       // a match by now, then we've failed the specification.
129       if (tmp == 0)
130         return false;
131
132       tmp = _Unwind_decode_target2((_Unwind_Word) e);
133
134       // Match a ttype entry.
135       catch_type = reinterpret_cast<const std::type_info*>(tmp);
136
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
140       // thrown_ptr here.
141       if (get_adjusted_ptr(catch_type, throw_type, &thrown_ptr))
142         return true;
143
144       // Advance to the next entry.
145       e++;
146     }
147 }
148
149
150 // Save stage1 handler information in the exception object
151
152 static inline void
153 save_caught_exception(struct _Unwind_Exception* ue_header,
154                       struct _Unwind_Context* context,
155                       void* thrown_ptr,
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__)))
161 {
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;
169 }
170
171
172 // Restore the catch handler data saved during phase1.
173
174 static inline void
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)
179 {
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];
184 }
185
186 #define CONTINUE_UNWINDING \
187   do                                                            \
188     {                                                           \
189       if (__gnu_unwind_frame(ue_header, context) != _URC_OK)    \
190         return _URC_FAILURE;                                    \
191       return _URC_CONTINUE_UNWIND;                              \
192     }                                                           \
193   while (0)
194
195 // Return true if the filter spec is empty, ie throw().
196
197 static bool
198 empty_exception_spec (lsda_header_info *info, _Unwind_Sword filter_value)
199 {
200   const _Unwind_Word* e = ((const _Unwind_Word*) info->TType)
201                           - filter_value - 1;
202
203   return *e == 0;
204 }
205
206 #else
207 typedef const std::type_info _throw_typet;
208
209
210 // Return an element from a type table.
211
212 static const std::type_info *
213 get_ttype_entry (lsda_header_info *info, _uleb128_t i)
214 {
215   _Unwind_Ptr ptr;
216
217   i *= size_of_encoded_value (info->ttype_encoding);
218   read_encoded_value_with_base (info->ttype_encoding, info->ttype_base,
219                                 info->TType - i, &ptr);
220
221   return reinterpret_cast<const std::type_info *>(ptr);
222 }
223
224 // Given the thrown type THROW_TYPE, pointer to a variable containing a
225 // pointer to the exception object THROWN_PTR_P and a type CATCH_TYPE to
226 // compare against, return whether or not there is a match and if so,
227 // update *THROWN_PTR_P.
228
229 static bool
230 get_adjusted_ptr (const std::type_info *catch_type,
231                   const std::type_info *throw_type,
232                   void **thrown_ptr_p)
233 {
234   void *thrown_ptr = *thrown_ptr_p;
235
236   // Pointer types need to adjust the actual pointer, not
237   // the pointer to pointer that is the exception object.
238   // This also has the effect of passing pointer types
239   // "by value" through the __cxa_begin_catch return value.
240   if (throw_type->__is_pointer_p ())
241     thrown_ptr = *(void **) thrown_ptr;
242
243   if (catch_type->__do_catch (throw_type, &thrown_ptr, 1))
244     {
245       *thrown_ptr_p = thrown_ptr;
246       return true;
247     }
248
249   return false;
250 }
251
252 // Return true if THROW_TYPE matches one if the filter types.
253
254 static bool
255 check_exception_spec(lsda_header_info* info, _throw_typet* throw_type,
256                       void* thrown_ptr, _sleb128_t filter_value)
257 {
258   const unsigned char *e = info->TType - filter_value - 1;
259
260   while (1)
261     {
262       const std::type_info *catch_type;
263       _uleb128_t tmp;
264
265       e = read_uleb128 (e, &tmp);
266
267       // Zero signals the end of the list.  If we've not found
268       // a match by now, then we've failed the specification.
269       if (tmp == 0)
270         return false;
271
272       // Match a ttype entry.
273       catch_type = get_ttype_entry (info, tmp);
274
275       // ??? There is currently no way to ask the RTTI code about the
276       // relationship between two types without reference to a specific
277       // object.  There should be; then we wouldn't need to mess with
278       // thrown_ptr here.
279       if (get_adjusted_ptr (catch_type, throw_type, &thrown_ptr))
280         return true;
281     }
282 }
283
284
285 // Save stage1 handler information in the exception object
286
287 static inline void
288 save_caught_exception(struct _Unwind_Exception* ue_header,
289                       struct _Unwind_Context* context
290                         __attribute__((__unused__)),
291                       void* thrown_ptr,
292                       int handler_switch_value,
293                       const unsigned char* language_specific_data,
294                       _Unwind_Ptr landing_pad __attribute__((__unused__)),
295                       const unsigned char* action_record)
296 {
297   __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
298
299   xh->handlerSwitchValue = handler_switch_value;
300   xh->actionRecord = action_record;
301   xh->languageSpecificData = language_specific_data;
302   xh->adjustedPtr = thrown_ptr;
303
304   // ??? Completely unknown what this field is supposed to be for.
305   // ??? Need to cache TType encoding base for call_unexpected.
306   xh->catchTemp = landing_pad;
307 }
308
309
310 // Restore the catch handler information saved during phase1.
311
312 static inline void
313 restore_caught_exception(struct _Unwind_Exception* ue_header,
314                          int& handler_switch_value,
315                          const unsigned char*& language_specific_data,
316                          _Unwind_Ptr& landing_pad)
317 {
318   __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
319   handler_switch_value = xh->handlerSwitchValue;
320   language_specific_data = xh->languageSpecificData;
321   landing_pad = (_Unwind_Ptr) xh->catchTemp;
322 }
323
324 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
325
326 // Return true if the filter spec is empty, ie throw().
327
328 static bool
329 empty_exception_spec (lsda_header_info *info, _Unwind_Sword filter_value)
330 {
331   const unsigned char *e = info->TType - filter_value - 1;
332   _uleb128_t tmp;
333
334   e = read_uleb128 (e, &tmp);
335   return tmp == 0;
336 }
337
338 #endif // !__ARM_EABI_UNWINDER__
339
340 namespace __cxxabiv1
341 {
342
343 // Using a different personality function name causes link failures
344 // when trying to mix code using different exception handling models.
345 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
346 #define PERSONALITY_FUNCTION    __gxx_personality_sj0
347 #define __builtin_eh_return_data_regno(x) x
348 #else
349 #define PERSONALITY_FUNCTION    __gxx_personality_v0
350 #endif
351
352 extern "C" _Unwind_Reason_Code
353 #ifdef __ARM_EABI_UNWINDER__
354 PERSONALITY_FUNCTION (_Unwind_State state,
355                       struct _Unwind_Exception* ue_header,
356                       struct _Unwind_Context* context)
357 #else
358 PERSONALITY_FUNCTION (int version,
359                       _Unwind_Action actions,
360                       _Unwind_Exception_Class exception_class,
361                       struct _Unwind_Exception *ue_header,
362                       struct _Unwind_Context *context)
363 #endif
364 {
365   enum found_handler_type
366   {
367     found_nothing,
368     found_terminate,
369     found_cleanup,
370     found_handler
371   } found_type;
372
373   lsda_header_info info;
374   const unsigned char *language_specific_data;
375   const unsigned char *action_record;
376   const unsigned char *p;
377   _Unwind_Ptr landing_pad, ip;
378   int handler_switch_value;
379   void* thrown_ptr = ue_header + 1;
380   bool foreign_exception;
381   int ip_before_insn = 0;
382
383 #ifdef __ARM_EABI_UNWINDER__
384   _Unwind_Action actions;
385
386   switch (state & _US_ACTION_MASK)
387     {
388     case _US_VIRTUAL_UNWIND_FRAME:
389       actions = _UA_SEARCH_PHASE;
390       break;
391
392     case _US_UNWIND_FRAME_STARTING:
393       actions = _UA_CLEANUP_PHASE;
394       if (!(state & _US_FORCE_UNWIND)
395           && ue_header->barrier_cache.sp == _Unwind_GetGR(context, 13))
396         actions |= _UA_HANDLER_FRAME;
397       break;
398
399     case _US_UNWIND_FRAME_RESUME:
400       CONTINUE_UNWINDING;
401       break;
402
403     default:
404       std::abort();
405     }
406   actions |= state & _US_FORCE_UNWIND;
407
408   // We don't know which runtime we're working with, so can't check this.
409   // However the ABI routines hide this from us, and we don't actually need
410   // to know.
411   foreign_exception = false;
412
413   // The dwarf unwinder assumes the context structure holds things like the
414   // function and LSDA pointers.  The ARM implementation caches these in
415   // the exception header (UCB).  To avoid rewriting everything we make the
416   // virtual IP register point at the UCB.
417   ip = (_Unwind_Ptr) ue_header;
418   _Unwind_SetGR(context, 12, ip);
419 #else
420   __cxa_exception* xh = __get_exception_header_from_ue(ue_header);
421
422   // Interface version check.
423   if (version != 1)
424     return _URC_FATAL_PHASE1_ERROR;
425   foreign_exception = !__is_gxx_exception_class(exception_class);
426 #endif
427
428   // Shortcut for phase 2 found handler for domestic exception.
429   if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
430       && !foreign_exception)
431     {
432       restore_caught_exception(ue_header, handler_switch_value,
433                                language_specific_data, landing_pad);
434       found_type = (landing_pad == 0 ? found_terminate : found_handler);
435       goto install_context;
436     }
437
438   language_specific_data = (const unsigned char *)
439     _Unwind_GetLanguageSpecificData (context);
440
441   // If no LSDA, then there are no handlers or cleanups.
442   if (! language_specific_data)
443     CONTINUE_UNWINDING;
444
445   // Parse the LSDA header.
446   p = parse_lsda_header (context, language_specific_data, &info);
447   info.ttype_base = base_of_encoded_value (info.ttype_encoding, context);
448 #ifdef HAVE_GETIPINFO
449   ip = _Unwind_GetIPInfo (context, &ip_before_insn);
450 #else
451   ip = _Unwind_GetIP (context);
452 #endif
453   if (! ip_before_insn)
454     --ip;
455   landing_pad = 0;
456   action_record = 0;
457   handler_switch_value = 0;
458
459 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
460   // The given "IP" is an index into the call-site table, with two
461   // exceptions -- -1 means no-action, and 0 means terminate.  But
462   // since we're using uleb128 values, we've not got random access
463   // to the array.
464   if ((int) ip < 0)
465     return _URC_CONTINUE_UNWIND;
466   else if (ip == 0)
467     {
468       // Fall through to set found_terminate.
469     }
470   else
471     {
472       _uleb128_t cs_lp, cs_action;
473       do
474         {
475           p = read_uleb128 (p, &cs_lp);
476           p = read_uleb128 (p, &cs_action);
477         }
478       while (--ip);
479
480       // Can never have null landing pad for sjlj -- that would have
481       // been indicated by a -1 call site index.
482       landing_pad = cs_lp + 1;
483       if (cs_action)
484         action_record = info.action_table + cs_action - 1;
485       goto found_something;
486     }
487 #else
488   // Search the call-site table for the action associated with this IP.
489   while (p < info.action_table)
490     {
491       _Unwind_Ptr cs_start, cs_len, cs_lp;
492       _uleb128_t cs_action;
493
494       // Note that all call-site encodings are "absolute" displacements.
495       p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
496       p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
497       p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
498       p = read_uleb128 (p, &cs_action);
499
500       // The table is sorted, so if we've passed the ip, stop.
501       if (ip < info.Start + cs_start)
502         p = info.action_table;
503       else if (ip < info.Start + cs_start + cs_len)
504         {
505           if (cs_lp)
506             landing_pad = info.LPStart + cs_lp;
507           if (cs_action)
508             action_record = info.action_table + cs_action - 1;
509           goto found_something;
510         }
511     }
512 #endif // _GLIBCXX_SJLJ_EXCEPTIONS
513
514   // If ip is not present in the table, call terminate.  This is for
515   // a destructor inside a cleanup, or a library routine the compiler
516   // was not expecting to throw.
517   found_type = found_terminate;
518   goto do_something;
519
520  found_something:
521   if (landing_pad == 0)
522     {
523       // If ip is present, and has a null landing pad, there are
524       // no cleanups or handlers to be run.
525       found_type = found_nothing;
526     }
527   else if (action_record == 0)
528     {
529       // If ip is present, has a non-null landing pad, and a null
530       // action table offset, then there are only cleanups present.
531       // Cleanups use a zero switch value, as set above.
532       found_type = found_cleanup;
533     }
534   else
535     {
536       // Otherwise we have a catch handler or exception specification.
537
538       _sleb128_t ar_filter, ar_disp;
539       const std::type_info* catch_type;
540       _throw_typet* throw_type;
541       bool saw_cleanup = false;
542       bool saw_handler = false;
543
544       // During forced unwinding, we only run cleanups.  With a foreign
545       // exception class, there's no exception type.
546       // ??? What to do about GNU Java and GNU Ada exceptions.
547
548       if ((actions & _UA_FORCE_UNWIND)
549           || foreign_exception)
550         throw_type = 0;
551       else
552 #ifdef __ARM_EABI_UNWINDER__
553         throw_type = ue_header;
554 #else
555         throw_type = xh->exceptionType;
556 #endif
557
558       while (1)
559         {
560           p = action_record;
561           p = read_sleb128 (p, &ar_filter);
562           read_sleb128 (p, &ar_disp);
563
564           if (ar_filter == 0)
565             {
566               // Zero filter values are cleanups.
567               saw_cleanup = true;
568             }
569           else if (ar_filter > 0)
570             {
571               // Positive filter values are handlers.
572               catch_type = get_ttype_entry (&info, ar_filter);
573
574               // Null catch type is a catch-all handler; we can catch foreign
575               // exceptions with this.  Otherwise we must match types.
576               if (! catch_type
577                   || (throw_type
578                       && get_adjusted_ptr (catch_type, throw_type,
579                                            &thrown_ptr)))
580                 {
581                   saw_handler = true;
582                   break;
583                 }
584             }
585           else
586             {
587               // Negative filter values are exception specifications.
588               // ??? How do foreign exceptions fit in?  As far as I can
589               // see we can't match because there's no __cxa_exception
590               // object to stuff bits in for __cxa_call_unexpected to use.
591               // Allow them iff the exception spec is non-empty.  I.e.
592               // a throw() specification results in __unexpected.
593               if (throw_type
594                   ? ! check_exception_spec (&info, throw_type, thrown_ptr,
595                                             ar_filter)
596                   : empty_exception_spec (&info, ar_filter))
597                 {
598                   saw_handler = true;
599                   break;
600                 }
601             }
602
603           if (ar_disp == 0)
604             break;
605           action_record = p + ar_disp;
606         }
607
608       if (saw_handler)
609         {
610           handler_switch_value = ar_filter;
611           found_type = found_handler;
612         }
613       else
614         found_type = (saw_cleanup ? found_cleanup : found_nothing);
615     }
616
617  do_something:
618    if (found_type == found_nothing)
619      CONTINUE_UNWINDING;
620
621   if (actions & _UA_SEARCH_PHASE)
622     {
623       if (found_type == found_cleanup)
624         CONTINUE_UNWINDING;
625
626       // For domestic exceptions, we cache data from phase 1 for phase 2.
627       if (!foreign_exception)
628         {
629           save_caught_exception(ue_header, context, thrown_ptr,
630                                 handler_switch_value, language_specific_data,
631                                 landing_pad, action_record);
632         }
633       return _URC_HANDLER_FOUND;
634     }
635
636  install_context:
637   
638   // We can't use any of the cxa routines with foreign exceptions,
639   // because they all expect ue_header to be a struct __cxa_exception.
640   // So in that case, call terminate or unexpected directly.
641   if ((actions & _UA_FORCE_UNWIND)
642       || foreign_exception)
643     {
644       if (found_type == found_terminate)
645         std::terminate ();
646       else if (handler_switch_value < 0)
647         {
648           try 
649             { std::unexpected (); } 
650           catch(...) 
651             { std::terminate (); }
652         }
653     }
654   else
655     {
656       if (found_type == found_terminate)
657         __cxa_call_terminate(ue_header);
658
659       // Cache the TType base value for __cxa_call_unexpected, as we won't
660       // have an _Unwind_Context then.
661       if (handler_switch_value < 0)
662         {
663           parse_lsda_header (context, language_specific_data, &info);
664
665 #ifdef __ARM_EABI_UNWINDER__
666           const _Unwind_Word* e;
667           _Unwind_Word n;
668           
669           e = ((const _Unwind_Word*) info.TType) - handler_switch_value - 1;
670           // Count the number of rtti objects.
671           n = 0;
672           while (e[n] != 0)
673             n++;
674
675           // Count.
676           ue_header->barrier_cache.bitpattern[1] = n;
677           // Base (obsolete)
678           ue_header->barrier_cache.bitpattern[2] = 0;
679           // Stride.
680           ue_header->barrier_cache.bitpattern[3] = 4;
681           // List head.
682           ue_header->barrier_cache.bitpattern[4] = (_Unwind_Word) e;
683 #else
684           xh->catchTemp = base_of_encoded_value (info.ttype_encoding, context);
685 #endif
686         }
687     }
688
689   /* For targets with pointers smaller than the word size, we must extend the
690      pointer, and this extension is target dependent.  */
691   _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
692                  __builtin_extend_pointer (ue_header));
693   _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
694                  handler_switch_value);
695   _Unwind_SetIP (context, landing_pad);
696 #ifdef __ARM_EABI_UNWINDER__
697   if (found_type == found_cleanup)
698     __cxa_begin_cleanup(ue_header);
699 #endif
700   return _URC_INSTALL_CONTEXT;
701 }
702
703 /* The ARM EABI implementation of __cxa_call_unexpected is in a
704    different file so that the personality routine (PR) can be used
705    standalone.  The generic routine shared datastructures with the PR
706    so it is most convenient to implement it here.  */
707 #ifndef __ARM_EABI_UNWINDER__
708 extern "C" void
709 __cxa_call_unexpected (void *exc_obj_in)
710 {
711   _Unwind_Exception *exc_obj
712     = reinterpret_cast <_Unwind_Exception *>(exc_obj_in);
713
714   __cxa_begin_catch (exc_obj);
715
716   // This function is a handler for our exception argument.  If we exit
717   // by throwing a different exception, we'll need the original cleaned up.
718   struct end_catch_protect
719   {
720     end_catch_protect() { }
721     ~end_catch_protect() { __cxa_end_catch(); }
722   } end_catch_protect_obj;
723
724   lsda_header_info info;
725   __cxa_exception *xh = __get_exception_header_from_ue (exc_obj);
726   const unsigned char *xh_lsda;
727   _Unwind_Sword xh_switch_value;
728   std::terminate_handler xh_terminate_handler;
729
730   // If the unexpectedHandler rethrows the exception (e.g. to categorize it),
731   // it will clobber data about the current handler.  So copy the data out now.
732   xh_lsda = xh->languageSpecificData;
733   xh_switch_value = xh->handlerSwitchValue;
734   xh_terminate_handler = xh->terminateHandler;
735   info.ttype_base = (_Unwind_Ptr) xh->catchTemp;
736
737   try 
738     { __unexpected (xh->unexpectedHandler); } 
739   catch(...) 
740     {
741       // Get the exception thrown from unexpected.
742
743       __cxa_eh_globals *globals = __cxa_get_globals_fast ();
744       __cxa_exception *new_xh = globals->caughtExceptions;
745       void *new_ptr = new_xh + 1;
746
747       // We don't quite have enough stuff cached; re-parse the LSDA.
748       parse_lsda_header (0, xh_lsda, &info);
749
750       // If this new exception meets the exception spec, allow it.
751       if (check_exception_spec (&info, new_xh->exceptionType,
752                                 new_ptr, xh_switch_value))
753         __throw_exception_again;
754
755       // If the exception spec allows std::bad_exception, throw that.
756       // We don't have a thrown object to compare against, but since
757       // bad_exception doesn't have virtual bases, that's OK; just pass 0.
758 #ifdef __EXCEPTIONS  
759       const std::type_info &bad_exc = typeid (std::bad_exception);
760       if (check_exception_spec (&info, &bad_exc, 0, xh_switch_value))
761         throw std::bad_exception();
762 #endif   
763
764       // Otherwise, die.
765       __terminate (xh_terminate_handler);
766     }
767 }
768 #endif
769
770 } // namespace __cxxabiv1