OSDN Git Service

* libsupc++/eh_personality.cc (__cxa_call_unexpected): Take void*.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / libsupc++ / eh_personality.cc
1 // -*- C++ -*- The GNU C++ exception personality routine.
2 // Copyright (C) 2001 Free Software Foundation, Inc.
3 //
4 // This file is part of GNU CC.
5 //
6 // GNU CC 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 // GNU CC 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 GNU CC; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, 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
31 #include <bits/c++config.h>
32 #include <cstdlib>
33 #include <exception_defines.h>
34 #include "unwind-cxx.h"
35
36 using namespace __cxxabiv1;
37
38 #include "unwind-pe.h"
39
40 \f
41 struct lsda_header_info
42 {
43   _Unwind_Ptr Start;
44   _Unwind_Ptr LPStart;
45   _Unwind_Ptr ttype_base;
46   const unsigned char *TType;
47   const unsigned char *action_table;
48   unsigned char ttype_encoding;
49   unsigned char call_site_encoding;
50 };
51
52 static const unsigned char *
53 parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
54                    lsda_header_info *info)
55 {
56   _Unwind_Word tmp;
57   unsigned char lpstart_encoding;
58
59   info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
60
61   // Find @LPStart, the base to which landing pad offsets are relative.
62   lpstart_encoding = *p++;
63   if (lpstart_encoding != DW_EH_PE_omit)
64     p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
65   else
66     info->LPStart = info->Start;
67
68   // Find @TType, the base of the handler and exception spec type data.
69   info->ttype_encoding = *p++;
70   if (info->ttype_encoding != DW_EH_PE_omit)
71     {
72       p = read_uleb128 (p, &tmp);
73       info->TType = p + tmp;
74     }
75   else
76     info->TType = 0;
77
78   // The encoding and length of the call-site table; the action table
79   // immediately follows.
80   info->call_site_encoding = *p++;
81   p = read_uleb128 (p, &tmp);
82   info->action_table = p + tmp;
83
84   return p;
85 }
86
87 static const std::type_info *
88 get_ttype_entry (lsda_header_info *info, _Unwind_Word i)
89 {
90   _Unwind_Ptr ptr;
91
92   i *= size_of_encoded_value (info->ttype_encoding);
93   read_encoded_value_with_base (info->ttype_encoding, info->ttype_base,
94                                 info->TType - i, &ptr);
95
96   return reinterpret_cast<const std::type_info *>(ptr);
97 }
98
99 static bool
100 check_exception_spec (lsda_header_info *info, const std::type_info *throw_type,
101                       _Unwind_Sword filter_value)
102 {
103   const unsigned char *e = info->TType - filter_value - 1;
104
105   while (1)
106     {
107       const std::type_info *catch_type;
108       _Unwind_Word tmp;
109       void *dummy;
110
111       e = read_uleb128 (e, &tmp);
112
113       // Zero signals the end of the list.  If we've not found
114       // a match by now, then we've failed the specification.
115       if (tmp == 0)
116         return false;
117
118       // Match a ttype entry.
119       catch_type = get_ttype_entry (info, tmp);
120       if (catch_type->__do_catch (throw_type, &dummy, 1))
121         return true;
122     }
123 }
124
125 // Using a different personality function name causes link failures
126 // when trying to mix code using different exception handling models.
127 #ifdef _GLIBCPP_SJLJ_EXCEPTIONS
128 #define PERSONALITY_FUNCTION    __gxx_personality_sj0
129 #define __builtin_eh_return_data_regno(x) x
130 #else
131 #define PERSONALITY_FUNCTION    __gxx_personality_v0
132 #endif
133
134 extern "C" _Unwind_Reason_Code
135 PERSONALITY_FUNCTION (int version,
136                       _Unwind_Action actions,
137                       _Unwind_Exception_Class exception_class,
138                       struct _Unwind_Exception *ue_header,
139                       struct _Unwind_Context *context)
140 {
141   __cxa_exception *xh = __get_exception_header_from_ue (ue_header);
142
143   enum found_handler_type
144   {
145     found_nothing,
146     found_terminate,
147     found_cleanup,
148     found_handler
149   } found_type;
150
151   lsda_header_info info;
152   const unsigned char *language_specific_data;
153   const unsigned char *action_record;
154   const unsigned char *p;
155   _Unwind_Ptr landing_pad, ip;
156   int handler_switch_value;
157   void *adjusted_ptr = xh + 1;
158
159   // Interface version check.
160   if (version != 1)
161     return _URC_FATAL_PHASE1_ERROR;
162
163   // Shortcut for phase 2 found handler for domestic exception.
164   if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
165       && exception_class == __gxx_exception_class)
166     {
167       handler_switch_value = xh->handlerSwitchValue;
168       landing_pad = (_Unwind_Ptr) xh->catchTemp;
169       found_type = (landing_pad == 0 ? found_terminate : found_handler);
170       goto install_context;
171     }
172
173   language_specific_data = (const unsigned char *)
174     _Unwind_GetLanguageSpecificData (context);
175
176   // If no LSDA, then there are no handlers or cleanups.
177   if (! language_specific_data)
178     return _URC_CONTINUE_UNWIND;
179
180   // Parse the LSDA header.
181   p = parse_lsda_header (context, language_specific_data, &info);
182   info.ttype_base = base_of_encoded_value (info.ttype_encoding, context);
183   ip = _Unwind_GetIP (context) - 1;
184   landing_pad = 0;
185   action_record = 0;
186   handler_switch_value = 0;
187
188 #ifdef _GLIBCPP_SJLJ_EXCEPTIONS
189   // The given "IP" is an index into the call-site table, with two
190   // exceptions -- -1 means no-action, and 0 means terminate.  But
191   // since we're using uleb128 values, we've not got random access
192   // to the array.
193   if ((int) ip < 0)
194     return _URC_CONTINUE_UNWIND;
195   else if (ip == 0)
196     {
197       // Fall through to set found_terminate.
198     }
199   else
200     {
201       _Unwind_Word cs_lp, cs_action;
202       do
203         {
204           p = read_uleb128 (p, &cs_lp);
205           p = read_uleb128 (p, &cs_action);
206         }
207       while (--ip);
208
209       // Can never have null landing pad for sjlj -- that would have
210       // been indicated by a -1 call site index.
211       landing_pad = cs_lp + 1;
212       if (cs_action)
213         action_record = info.action_table + cs_action - 1;
214       goto found_something;
215     }
216 #else
217   // Search the call-site table for the action associated with this IP.
218   while (p < info.action_table)
219     {
220       _Unwind_Ptr cs_start, cs_len, cs_lp;
221       _Unwind_Word cs_action;
222
223       // Note that all call-site encodings are "absolute" displacements.
224       p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
225       p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
226       p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
227       p = read_uleb128 (p, &cs_action);
228
229       // The table is sorted, so if we've passed the ip, stop.
230       if (ip < info.Start + cs_start)
231         p = info.action_table;
232       else if (ip < info.Start + cs_start + cs_len)
233         {
234           if (cs_lp)
235             landing_pad = info.LPStart + cs_lp;
236           if (cs_action)
237             action_record = info.action_table + cs_action - 1;
238           goto found_something;
239         }
240     }
241 #endif // _GLIBCPP_SJLJ_EXCEPTIONS
242
243   // If ip is not present in the table, call terminate.  This is for
244   // a destructor inside a cleanup, or a library routine the compiler
245   // was not expecting to throw.
246   found_type = (actions & _UA_FORCE_UNWIND ? found_nothing : found_terminate);
247   goto do_something;
248
249  found_something:
250   if (landing_pad == 0)
251     {
252       // If ip is present, and has a null landing pad, there are
253       // no cleanups or handlers to be run.
254       found_type = found_nothing;
255     }
256   else if (action_record == 0)
257     {
258       // If ip is present, has a non-null landing pad, and a null
259       // action table offset, then there are only cleanups present.
260       // Cleanups use a zero switch value, as set above.
261       found_type = found_cleanup;
262     }
263   else
264     {
265       // Otherwise we have a catch handler or exception specification.
266
267       _Unwind_Sword ar_filter, ar_disp;
268       const std::type_info *throw_type, *catch_type;
269       bool saw_cleanup = false;
270       bool saw_handler = false;
271
272       // During forced unwinding, we only run cleanups.  With a foreign
273       // exception class, there's no exception type.
274       // ??? What to do about GNU Java and GNU Ada exceptions.
275
276       if ((actions & _UA_FORCE_UNWIND)
277           || exception_class != __gxx_exception_class)
278         throw_type = 0;
279       else
280         throw_type = xh->exceptionType;
281
282       while (1)
283         {
284           p = action_record;
285           p = read_sleb128 (p, &ar_filter);
286           read_sleb128 (p, &ar_disp);
287
288           if (ar_filter == 0)
289             {
290               // Zero filter values are cleanups.
291               saw_cleanup = true;
292             }
293           else if (ar_filter > 0)
294             {
295               // Positive filter values are handlers.
296               catch_type = get_ttype_entry (&info, ar_filter);
297               adjusted_ptr = xh + 1;
298
299               // Null catch type is a catch-all handler.  We can catch
300               // foreign exceptions with this.
301               if (! catch_type)
302                 {
303                   if (!(actions & _UA_FORCE_UNWIND))
304                     {
305                       saw_handler = true;
306                       break;
307                     }
308                 }
309               else if (throw_type)
310                 {
311                   // Pointer types need to adjust the actual pointer, not
312                   // the pointer to pointer that is the exception object.
313                   // This also has the effect of passing pointer types
314                   // "by value" through the __cxa_begin_catch return value.
315                   if (throw_type->__is_pointer_p ())
316                     adjusted_ptr = *(void **) adjusted_ptr;
317
318                   if (catch_type->__do_catch (throw_type, &adjusted_ptr, 1))
319                     {
320                       saw_handler = true;
321                       break;
322                     }
323                 }
324             }
325           else
326             {
327               // Negative filter values are exception specifications.
328               // ??? How do foreign exceptions fit in?  As far as I can
329               // see we can't match because there's no __cxa_exception
330               // object to stuff bits in for __cxa_call_unexpected to use.
331               if (throw_type
332                   && ! check_exception_spec (&info, throw_type, ar_filter))
333                 {
334                   saw_handler = true;
335                   break;
336                 }
337             }
338
339           if (ar_disp == 0)
340             break;
341           action_record = p + ar_disp;
342         }
343
344       if (saw_handler)
345         {
346           handler_switch_value = ar_filter;
347           found_type = found_handler;
348         }
349       else
350         found_type = (saw_cleanup ? found_cleanup : found_nothing);
351     }
352
353  do_something:
354    if (found_type == found_nothing)
355      return _URC_CONTINUE_UNWIND;
356
357   if (actions & _UA_SEARCH_PHASE)
358     {
359       if (found_type == found_cleanup)
360         return _URC_CONTINUE_UNWIND;
361
362       // For domestic exceptions, we cache data from phase 1 for phase 2.
363       if (exception_class == __gxx_exception_class)
364         {
365           xh->handlerSwitchValue = handler_switch_value;
366           xh->actionRecord = action_record;
367           xh->languageSpecificData = language_specific_data;
368           xh->adjustedPtr = adjusted_ptr;
369
370           // ??? Completely unknown what this field is supposed to be for.
371           // ??? Need to cache TType encoding base for call_unexpected.
372           xh->catchTemp = (void *) (_Unwind_Ptr) landing_pad;
373         }
374       return _URC_HANDLER_FOUND;
375     }
376
377  install_context:
378   if (found_type == found_terminate)
379     {
380       __cxa_begin_catch (&xh->unwindHeader);
381       __terminate (xh->terminateHandler);
382     }
383
384   // Cache the TType base value for __cxa_call_unexpected, as we won't
385   // have an _Unwind_Context then.
386   if (handler_switch_value < 0)
387     {
388       parse_lsda_header (context, xh->languageSpecificData, &info);
389       xh->catchTemp = (void *) base_of_encoded_value (info.ttype_encoding,
390                                                       context);
391     }
392
393   _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
394                  (_Unwind_Ptr) &xh->unwindHeader);
395   _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
396                  handler_switch_value);
397   _Unwind_SetIP (context, landing_pad);
398   return _URC_INSTALL_CONTEXT;
399 }
400
401 extern "C" void
402 __cxa_call_unexpected (void *exc_obj_in)
403 {
404   _Unwind_Exception *exc_obj
405     = reinterpret_cast <_Unwind_Exception *>(exc_obj_in);
406
407   __cxa_begin_catch (exc_obj);
408
409   // This function is a handler for our exception argument.  If we exit
410   // by throwing a different exception, we'll need the original cleaned up.
411   struct end_catch_protect
412   {
413     end_catch_protect() { }
414     ~end_catch_protect() { __cxa_end_catch(); }
415   } end_catch_protect_obj;
416
417   __cxa_exception *xh = __get_exception_header_from_ue (exc_obj);
418
419   try 
420     { __unexpected (xh->unexpectedHandler); } 
421   catch(...) 
422     {
423       // Get the exception thrown from unexpected.
424       // ??? Foreign exceptions can't be stacked this way.
425       
426       __cxa_eh_globals *globals = __cxa_get_globals_fast ();
427       __cxa_exception *new_xh = globals->caughtExceptions;
428       
429       // We don't quite have enough stuff cached; re-parse the LSDA.
430       lsda_header_info info;
431       parse_lsda_header (0, xh->languageSpecificData, &info);
432       info.ttype_base = (_Unwind_Ptr) xh->catchTemp;
433       
434       // If this new exception meets the exception spec, allow it.
435       if (check_exception_spec (&info, new_xh->exceptionType,
436                                 xh->handlerSwitchValue))
437         __throw_exception_again;
438       
439       // If the exception spec allows std::bad_exception, throw that.
440 #ifdef __EXCEPTIONS  
441       const std::type_info &bad_exc = typeid (std::bad_exception);
442       if (check_exception_spec (&info, &bad_exc, xh->handlerSwitchValue))
443         throw std::bad_exception();
444 #endif   
445       // Otherwise, die.
446       __terminate(xh->terminateHandler);
447     }
448 }