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 "unwind-cxx.h"
35 using namespace __cxxabiv1;
37 #include "unwind-pe.h"
40 struct lsda_header_info
44 _Unwind_Ptr ttype_base;
45 const unsigned char *TType;
46 const unsigned char *action_table;
47 unsigned char ttype_encoding;
48 unsigned char call_site_encoding;
51 static const unsigned char *
52 parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
53 lsda_header_info *info)
56 unsigned char lpstart_encoding;
58 info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
60 // Find @LPStart, the base to which landing pad offsets are relative.
61 lpstart_encoding = *p++;
62 if (lpstart_encoding != DW_EH_PE_omit)
63 p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
65 info->LPStart = info->Start;
67 // Find @TType, the base of the handler and exception spec type data.
68 info->ttype_encoding = *p++;
69 if (info->ttype_encoding != DW_EH_PE_omit)
71 p = read_uleb128 (p, &tmp);
72 info->TType = p + tmp;
77 // The encoding and length of the call-site table; the action table
78 // immediately follows.
79 info->call_site_encoding = *p++;
80 p = read_uleb128 (p, &tmp);
81 info->action_table = p + tmp;
86 static const std::type_info *
87 get_ttype_entry (lsda_header_info *info, _Unwind_Word i)
91 i *= size_of_encoded_value (info->ttype_encoding);
92 read_encoded_value_with_base (info->ttype_encoding, info->ttype_base,
93 info->TType - i, &ptr);
95 return reinterpret_cast<const std::type_info *>(ptr);
99 check_exception_spec (lsda_header_info *info, const std::type_info *throw_type,
100 _Unwind_Sword filter_value)
102 const unsigned char *e = info->TType - filter_value - 1;
106 const std::type_info *catch_type;
110 e = read_uleb128 (e, &tmp);
112 // Zero signals the end of the list. If we've not found
113 // a match by now, then we've failed the specification.
117 // Match a ttype entry.
118 catch_type = get_ttype_entry (info, tmp);
119 if (catch_type->__do_catch (throw_type, &dummy, 1))
124 // Using a different personality function name causes link failures
125 // when trying to mix code using different exception handling models.
126 #ifdef _GLIBCPP_SJLJ_EXCEPTIONS
127 #define PERSONALITY_FUNCTION __gxx_personality_sj0
128 #define __builtin_eh_return_data_regno(x) x
130 #define PERSONALITY_FUNCTION __gxx_personality_v0
133 extern "C" _Unwind_Reason_Code
134 PERSONALITY_FUNCTION (int version,
135 _Unwind_Action actions,
136 _Unwind_Exception_Class exception_class,
137 struct _Unwind_Exception *ue_header,
138 struct _Unwind_Context *context)
140 __cxa_exception *xh = __get_exception_header_from_ue (ue_header);
142 enum found_handler_type
150 lsda_header_info info;
151 const unsigned char *language_specific_data;
152 const unsigned char *action_record;
153 const unsigned char *p;
154 _Unwind_Ptr landing_pad, ip;
155 int handler_switch_value;
156 void *adjusted_ptr = xh + 1;
158 // Interface version check.
160 return _URC_FATAL_PHASE1_ERROR;
162 // Shortcut for phase 2 found handler for domestic exception.
163 if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
164 && exception_class == __gxx_exception_class)
166 handler_switch_value = xh->handlerSwitchValue;
167 landing_pad = (_Unwind_Ptr) xh->catchTemp;
168 found_type = (landing_pad == 0 ? found_terminate : found_handler);
169 goto install_context;
172 language_specific_data = (const unsigned char *)
173 _Unwind_GetLanguageSpecificData (context);
175 // If no LSDA, then there are no handlers or cleanups.
176 if (! language_specific_data)
177 return _URC_CONTINUE_UNWIND;
179 // Parse the LSDA header.
180 p = parse_lsda_header (context, language_specific_data, &info);
181 info.ttype_base = base_of_encoded_value (info.ttype_encoding, context);
182 ip = _Unwind_GetIP (context) - 1;
185 handler_switch_value = 0;
187 #ifdef _GLIBCPP_SJLJ_EXCEPTIONS
188 // The given "IP" is an index into the call-site table, with two
189 // exceptions -- -1 means no-action, and 0 means terminate. But
190 // since we're using uleb128 values, we've not got random access
193 return _URC_CONTINUE_UNWIND;
196 // Fall through to set found_terminate.
200 _Unwind_Ptr cs_lp, cs_action;
203 p = read_uleb128 (p, &cs_lp);
204 p = read_uleb128 (p, &cs_action);
208 // Can never have null landing pad for sjlj -- that would have
209 // been indicated by a -1 call site index.
210 landing_pad = cs_lp + 1;
212 action_record = info.action_table + cs_action - 1;
213 goto found_something;
216 // Search the call-site table for the action associated with this IP.
217 while (p < info.action_table)
219 _Unwind_Ptr cs_start, cs_len, cs_lp, cs_action;
221 // Note that all call-site encodings are "absolute" displacements.
222 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
223 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
224 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
225 p = read_uleb128 (p, &cs_action);
227 // The table is sorted, so if we've passed the ip, stop.
228 if (ip < info.Start + cs_start)
229 p = info.action_table;
230 else if (ip < info.Start + cs_start + cs_len)
233 landing_pad = info.LPStart + cs_lp;
235 action_record = info.action_table + cs_action - 1;
236 goto found_something;
239 #endif // _GLIBCPP_SJLJ_EXCEPTIONS
241 // If ip is not present in the table, call terminate. This is for
242 // a destructor inside a cleanup, or a library routine the compiler
243 // was not expecting to throw.
244 found_type = (actions & _UA_FORCE_UNWIND ? found_nothing : found_terminate);
248 if (landing_pad == 0)
250 // If ip is present, and has a null landing pad, there are
251 // no cleanups or handlers to be run.
252 found_type = found_nothing;
254 else if (action_record == 0)
256 // If ip is present, has a non-null landing pad, and a null
257 // action table offset, then there are only cleanups present.
258 // Cleanups use a zero switch value, as set above.
259 found_type = found_cleanup;
263 // Otherwise we have a catch handler or exception specification.
265 _Unwind_Sword ar_filter, ar_disp;
266 const std::type_info *throw_type, *catch_type;
267 bool saw_cleanup = false;
268 bool saw_handler = false;
270 // During forced unwinding, we only run cleanups. With a foreign
271 // exception class, there's no exception type.
272 // ??? What to do about GNU Java and GNU Ada exceptions.
274 if ((actions & _UA_FORCE_UNWIND)
275 || exception_class != __gxx_exception_class)
278 throw_type = xh->exceptionType;
283 p = read_sleb128 (p, &ar_filter);
284 read_sleb128 (p, &ar_disp);
288 // Zero filter values are cleanups.
291 else if (ar_filter > 0)
293 // Positive filter values are handlers.
294 catch_type = get_ttype_entry (&info, ar_filter);
295 adjusted_ptr = xh + 1;
297 // Null catch type is a catch-all handler. We can catch
298 // foreign exceptions with this.
301 if (!(actions & _UA_FORCE_UNWIND))
309 // Pointer types need to adjust the actual pointer, not
310 // the pointer to pointer that is the exception object.
311 // This also has the effect of passing pointer types
312 // "by value" through the __cxa_begin_catch return value.
313 if (throw_type->__is_pointer_p ())
314 adjusted_ptr = *(void **) adjusted_ptr;
316 if (catch_type->__do_catch (throw_type, &adjusted_ptr, 1))
325 // Negative filter values are exception specifications.
326 // ??? How do foreign exceptions fit in? As far as I can
327 // see we can't match because there's no __cxa_exception
328 // object to stuff bits in for __cxa_call_unexpected to use.
330 && ! check_exception_spec (&info, throw_type, ar_filter))
339 action_record = p + ar_disp;
344 handler_switch_value = ar_filter;
345 found_type = found_handler;
348 found_type = (saw_cleanup ? found_cleanup : found_nothing);
352 if (found_type == found_nothing)
353 return _URC_CONTINUE_UNWIND;
355 if (actions & _UA_SEARCH_PHASE)
357 if (found_type == found_cleanup)
358 return _URC_CONTINUE_UNWIND;
360 // For domestic exceptions, we cache data from phase 1 for phase 2.
361 if (exception_class == __gxx_exception_class)
363 xh->handlerSwitchValue = handler_switch_value;
364 xh->actionRecord = action_record;
365 xh->languageSpecificData = language_specific_data;
366 xh->adjustedPtr = adjusted_ptr;
368 // ??? Completely unknown what this field is supposed to be for.
369 // ??? Need to cache TType encoding base for call_unexpected.
370 xh->catchTemp = (void *) (_Unwind_Ptr) landing_pad;
372 return _URC_HANDLER_FOUND;
376 if (found_type == found_terminate)
378 __cxa_begin_catch (&xh->unwindHeader);
379 __terminate (xh->terminateHandler);
382 // Cache the TType base value for __cxa_call_unexpected, as we won't
383 // have an _Unwind_Context then.
384 if (handler_switch_value < 0)
386 parse_lsda_header (context, xh->languageSpecificData, &info);
387 xh->catchTemp = (void *) base_of_encoded_value (info.ttype_encoding,
391 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
392 (_Unwind_Ptr) &xh->unwindHeader);
393 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
394 handler_switch_value);
395 _Unwind_SetIP (context, landing_pad);
396 return _URC_INSTALL_CONTEXT;
400 __cxa_call_unexpected (_Unwind_Exception *exc_obj)
402 __cxa_begin_catch (exc_obj);
404 // This function is a handler for our exception argument. If we exit
405 // by throwing a different exception, we'll need the original cleaned up.
406 struct end_catch_protect
408 end_catch_protect() { }
409 ~end_catch_protect() { __cxa_end_catch(); }
410 } end_catch_protect_obj;
412 __cxa_exception *xh = __get_exception_header_from_ue (exc_obj);
415 __unexpected (xh->unexpectedHandler);
417 // Get the exception thrown from unexpected.
418 // ??? Foreign exceptions can't be stacked this way.
420 __cxa_eh_globals *globals = __cxa_get_globals_fast ();
421 __cxa_exception *new_xh = globals->caughtExceptions;
423 // We don't quite have enough stuff cached; re-parse the LSDA.
424 lsda_header_info info;
425 parse_lsda_header (0, xh->languageSpecificData, &info);
426 info.ttype_base = (_Unwind_Ptr) xh->catchTemp;
428 // If this new exception meets the exception spec, allow it.
429 if (check_exception_spec (&info, new_xh->exceptionType,
430 xh->handlerSwitchValue))
433 // If the exception spec allows std::bad_exception, throw that.
434 const std::type_info &bad_exc = typeid (std::bad_exception);
435 if (check_exception_spec (&info, &bad_exc, xh->handlerSwitchValue))
436 throw std::bad_exception ();
439 __terminate(xh->terminateHandler);