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_Word 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;
220 _Unwind_Word cs_action;
222 // Note that all call-site encodings are "absolute" displacements.
223 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
224 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
225 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
226 p = read_uleb128 (p, &cs_action);
228 // The table is sorted, so if we've passed the ip, stop.
229 if (ip < info.Start + cs_start)
230 p = info.action_table;
231 else if (ip < info.Start + cs_start + cs_len)
234 landing_pad = info.LPStart + cs_lp;
236 action_record = info.action_table + cs_action - 1;
237 goto found_something;
240 #endif // _GLIBCPP_SJLJ_EXCEPTIONS
242 // If ip is not present in the table, call terminate. This is for
243 // a destructor inside a cleanup, or a library routine the compiler
244 // was not expecting to throw.
245 found_type = (actions & _UA_FORCE_UNWIND ? found_nothing : found_terminate);
249 if (landing_pad == 0)
251 // If ip is present, and has a null landing pad, there are
252 // no cleanups or handlers to be run.
253 found_type = found_nothing;
255 else if (action_record == 0)
257 // If ip is present, has a non-null landing pad, and a null
258 // action table offset, then there are only cleanups present.
259 // Cleanups use a zero switch value, as set above.
260 found_type = found_cleanup;
264 // Otherwise we have a catch handler or exception specification.
266 _Unwind_Sword ar_filter, ar_disp;
267 const std::type_info *throw_type, *catch_type;
268 bool saw_cleanup = false;
269 bool saw_handler = false;
271 // During forced unwinding, we only run cleanups. With a foreign
272 // exception class, there's no exception type.
273 // ??? What to do about GNU Java and GNU Ada exceptions.
275 if ((actions & _UA_FORCE_UNWIND)
276 || exception_class != __gxx_exception_class)
279 throw_type = xh->exceptionType;
284 p = read_sleb128 (p, &ar_filter);
285 read_sleb128 (p, &ar_disp);
289 // Zero filter values are cleanups.
292 else if (ar_filter > 0)
294 // Positive filter values are handlers.
295 catch_type = get_ttype_entry (&info, ar_filter);
296 adjusted_ptr = xh + 1;
298 // Null catch type is a catch-all handler. We can catch
299 // foreign exceptions with this.
302 if (!(actions & _UA_FORCE_UNWIND))
310 // Pointer types need to adjust the actual pointer, not
311 // the pointer to pointer that is the exception object.
312 // This also has the effect of passing pointer types
313 // "by value" through the __cxa_begin_catch return value.
314 if (throw_type->__is_pointer_p ())
315 adjusted_ptr = *(void **) adjusted_ptr;
317 if (catch_type->__do_catch (throw_type, &adjusted_ptr, 1))
326 // Negative filter values are exception specifications.
327 // ??? How do foreign exceptions fit in? As far as I can
328 // see we can't match because there's no __cxa_exception
329 // object to stuff bits in for __cxa_call_unexpected to use.
331 && ! check_exception_spec (&info, throw_type, ar_filter))
340 action_record = p + ar_disp;
345 handler_switch_value = ar_filter;
346 found_type = found_handler;
349 found_type = (saw_cleanup ? found_cleanup : found_nothing);
353 if (found_type == found_nothing)
354 return _URC_CONTINUE_UNWIND;
356 if (actions & _UA_SEARCH_PHASE)
358 if (found_type == found_cleanup)
359 return _URC_CONTINUE_UNWIND;
361 // For domestic exceptions, we cache data from phase 1 for phase 2.
362 if (exception_class == __gxx_exception_class)
364 xh->handlerSwitchValue = handler_switch_value;
365 xh->actionRecord = action_record;
366 xh->languageSpecificData = language_specific_data;
367 xh->adjustedPtr = adjusted_ptr;
369 // ??? Completely unknown what this field is supposed to be for.
370 // ??? Need to cache TType encoding base for call_unexpected.
371 xh->catchTemp = (void *) (_Unwind_Ptr) landing_pad;
373 return _URC_HANDLER_FOUND;
377 if (found_type == found_terminate)
379 __cxa_begin_catch (&xh->unwindHeader);
380 __terminate (xh->terminateHandler);
383 // Cache the TType base value for __cxa_call_unexpected, as we won't
384 // have an _Unwind_Context then.
385 if (handler_switch_value < 0)
387 parse_lsda_header (context, xh->languageSpecificData, &info);
388 xh->catchTemp = (void *) base_of_encoded_value (info.ttype_encoding,
392 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
393 (_Unwind_Ptr) &xh->unwindHeader);
394 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
395 handler_switch_value);
396 _Unwind_SetIP (context, landing_pad);
397 return _URC_INSTALL_CONTEXT;
401 __cxa_call_unexpected (_Unwind_Exception *exc_obj)
403 __cxa_begin_catch (exc_obj);
405 // This function is a handler for our exception argument. If we exit
406 // by throwing a different exception, we'll need the original cleaned up.
407 struct end_catch_protect
409 end_catch_protect() { }
410 ~end_catch_protect() { __cxa_end_catch(); }
411 } end_catch_protect_obj;
413 __cxa_exception *xh = __get_exception_header_from_ue (exc_obj);
416 __unexpected (xh->unexpectedHandler);
418 // Get the exception thrown from unexpected.
419 // ??? Foreign exceptions can't be stacked this way.
421 __cxa_eh_globals *globals = __cxa_get_globals_fast ();
422 __cxa_exception *new_xh = globals->caughtExceptions;
424 // We don't quite have enough stuff cached; re-parse the LSDA.
425 lsda_header_info info;
426 parse_lsda_header (0, xh->languageSpecificData, &info);
427 info.ttype_base = (_Unwind_Ptr) xh->catchTemp;
429 // If this new exception meets the exception spec, allow it.
430 if (check_exception_spec (&info, new_xh->exceptionType,
431 xh->handlerSwitchValue))
434 // If the exception spec allows std::bad_exception, throw that.
435 const std::type_info &bad_exc = typeid (std::bad_exception);
436 if (check_exception_spec (&info, &bad_exc, xh->handlerSwitchValue))
437 throw std::bad_exception ();
440 __terminate(xh->terminateHandler);