1 // -*- C++ -*- The GNU C++ exception personality routine.
2 // Copyright (C) 2001 Free Software Foundation, Inc.
4 // This file is part of GNU CC.
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)
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.
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.
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>
33 #include <exception_defines.h>
34 #include "unwind-cxx.h"
36 using namespace __cxxabiv1;
38 #include "unwind-pe.h"
41 struct lsda_header_info
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;
52 static const unsigned char *
53 parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
54 lsda_header_info *info)
57 unsigned char lpstart_encoding;
59 info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
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);
66 info->LPStart = info->Start;
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)
72 p = read_uleb128 (p, &tmp);
73 info->TType = p + tmp;
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;
87 static const std::type_info *
88 get_ttype_entry (lsda_header_info *info, _Unwind_Word i)
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);
96 return reinterpret_cast<const std::type_info *>(ptr);
100 check_exception_spec (lsda_header_info *info, const std::type_info *throw_type,
101 _Unwind_Sword filter_value)
103 const unsigned char *e = info->TType - filter_value - 1;
107 const std::type_info *catch_type;
111 e = read_uleb128 (e, &tmp);
113 // Zero signals the end of the list. If we've not found
114 // a match by now, then we've failed the specification.
118 // Match a ttype entry.
119 catch_type = get_ttype_entry (info, tmp);
120 if (catch_type->__do_catch (throw_type, &dummy, 1))
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
131 #define PERSONALITY_FUNCTION __gxx_personality_v0
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)
141 __cxa_exception *xh = __get_exception_header_from_ue (ue_header);
143 enum found_handler_type
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;
159 // Interface version check.
161 return _URC_FATAL_PHASE1_ERROR;
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)
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;
173 language_specific_data = (const unsigned char *)
174 _Unwind_GetLanguageSpecificData (context);
176 // If no LSDA, then there are no handlers or cleanups.
177 if (! language_specific_data)
178 return _URC_CONTINUE_UNWIND;
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;
186 handler_switch_value = 0;
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
194 return _URC_CONTINUE_UNWIND;
197 // Fall through to set found_terminate.
201 _Unwind_Word cs_lp, cs_action;
204 p = read_uleb128 (p, &cs_lp);
205 p = read_uleb128 (p, &cs_action);
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;
213 action_record = info.action_table + cs_action - 1;
214 goto found_something;
217 // Search the call-site table for the action associated with this IP.
218 while (p < info.action_table)
220 _Unwind_Ptr cs_start, cs_len, cs_lp;
221 _Unwind_Word cs_action;
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);
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)
235 landing_pad = info.LPStart + cs_lp;
237 action_record = info.action_table + cs_action - 1;
238 goto found_something;
241 #endif // _GLIBCPP_SJLJ_EXCEPTIONS
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);
250 if (landing_pad == 0)
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;
256 else if (action_record == 0)
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;
265 // Otherwise we have a catch handler or exception specification.
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;
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.
276 if ((actions & _UA_FORCE_UNWIND)
277 || exception_class != __gxx_exception_class)
280 throw_type = xh->exceptionType;
285 p = read_sleb128 (p, &ar_filter);
286 read_sleb128 (p, &ar_disp);
290 // Zero filter values are cleanups.
293 else if (ar_filter > 0)
295 // Positive filter values are handlers.
296 catch_type = get_ttype_entry (&info, ar_filter);
297 adjusted_ptr = xh + 1;
299 // Null catch type is a catch-all handler. We can catch
300 // foreign exceptions with this.
303 if (!(actions & _UA_FORCE_UNWIND))
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;
318 if (catch_type->__do_catch (throw_type, &adjusted_ptr, 1))
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.
332 && ! check_exception_spec (&info, throw_type, ar_filter))
341 action_record = p + ar_disp;
346 handler_switch_value = ar_filter;
347 found_type = found_handler;
350 found_type = (saw_cleanup ? found_cleanup : found_nothing);
354 if (found_type == found_nothing)
355 return _URC_CONTINUE_UNWIND;
357 if (actions & _UA_SEARCH_PHASE)
359 if (found_type == found_cleanup)
360 return _URC_CONTINUE_UNWIND;
362 // For domestic exceptions, we cache data from phase 1 for phase 2.
363 if (exception_class == __gxx_exception_class)
365 xh->handlerSwitchValue = handler_switch_value;
366 xh->actionRecord = action_record;
367 xh->languageSpecificData = language_specific_data;
368 xh->adjustedPtr = adjusted_ptr;
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;
374 return _URC_HANDLER_FOUND;
378 if (found_type == found_terminate)
380 __cxa_begin_catch (&xh->unwindHeader);
381 __terminate (xh->terminateHandler);
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)
388 parse_lsda_header (context, xh->languageSpecificData, &info);
389 xh->catchTemp = (void *) base_of_encoded_value (info.ttype_encoding,
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;
402 __cxa_call_unexpected (void *exc_obj_in)
404 _Unwind_Exception *exc_obj
405 = reinterpret_cast <_Unwind_Exception *>(exc_obj_in);
407 __cxa_begin_catch (exc_obj);
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
413 end_catch_protect() { }
414 ~end_catch_protect() { __cxa_end_catch(); }
415 } end_catch_protect_obj;
417 __cxa_exception *xh = __get_exception_header_from_ue (exc_obj);
420 { __unexpected (xh->unexpectedHandler); }
423 // Get the exception thrown from unexpected.
424 // ??? Foreign exceptions can't be stacked this way.
426 __cxa_eh_globals *globals = __cxa_get_globals_fast ();
427 __cxa_exception *new_xh = globals->caughtExceptions;
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;
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;
439 // If the exception spec allows std::bad_exception, throw that.
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();
446 __terminate(xh->terminateHandler);