eh_personality.cc revision 117397
1// -*- C++ -*- The GNU C++ exception personality routine.
2// Copyright (C) 2001, 2002, 2003 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
36using namespace __cxxabiv1;
37
38#include "unwind-pe.h"
39
40
41struct 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
52static const unsigned char *
53parse_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
87static const std::type_info *
88get_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// Given the thrown type THROW_TYPE, pointer to a variable containing a
100// pointer to the exception object THROWN_PTR_P and a type CATCH_TYPE to
101// compare against, return whether or not there is a match and if so,
102// update *THROWN_PTR_P.
103
104static bool
105get_adjusted_ptr (const std::type_info *catch_type,
106		  const std::type_info *throw_type,
107		  void **thrown_ptr_p)
108{
109  void *thrown_ptr = *thrown_ptr_p;
110
111  // Pointer types need to adjust the actual pointer, not
112  // the pointer to pointer that is the exception object.
113  // This also has the effect of passing pointer types
114  // "by value" through the __cxa_begin_catch return value.
115  if (throw_type->__is_pointer_p ())
116    thrown_ptr = *(void **) thrown_ptr;
117
118  if (catch_type->__do_catch (throw_type, &thrown_ptr, 1))
119    {
120      *thrown_ptr_p = thrown_ptr;
121      return true;
122    }
123
124  return false;
125}
126
127// Return true if THROW_TYPE matches one if the filter types.
128
129static bool
130check_exception_spec (lsda_header_info *info, const std::type_info *throw_type,
131		      void *thrown_ptr, _Unwind_Sword filter_value)
132{
133  const unsigned char *e = info->TType - filter_value - 1;
134
135  while (1)
136    {
137      const std::type_info *catch_type;
138      _Unwind_Word tmp;
139
140      e = read_uleb128 (e, &tmp);
141
142      // Zero signals the end of the list.  If we've not found
143      // a match by now, then we've failed the specification.
144      if (tmp == 0)
145        return false;
146
147      // Match a ttype entry.
148      catch_type = get_ttype_entry (info, tmp);
149
150      // ??? There is currently no way to ask the RTTI code about the
151      // relationship between two types without reference to a specific
152      // object.  There should be; then we wouldn't need to mess with
153      // thrown_ptr here.
154      if (get_adjusted_ptr (catch_type, throw_type, &thrown_ptr))
155	return true;
156    }
157}
158
159// Return true if the filter spec is empty, ie throw().
160
161static bool
162empty_exception_spec (lsda_header_info *info, _Unwind_Sword filter_value)
163{
164  const unsigned char *e = info->TType - filter_value - 1;
165  _Unwind_Word tmp;
166
167  e = read_uleb128 (e, &tmp);
168  return tmp == 0;
169}
170
171// Using a different personality function name causes link failures
172// when trying to mix code using different exception handling models.
173#ifdef _GLIBCPP_SJLJ_EXCEPTIONS
174#define PERSONALITY_FUNCTION	__gxx_personality_sj0
175#define __builtin_eh_return_data_regno(x) x
176#else
177#define PERSONALITY_FUNCTION	__gxx_personality_v0
178#endif
179
180extern "C" _Unwind_Reason_Code
181PERSONALITY_FUNCTION (int version,
182		      _Unwind_Action actions,
183		      _Unwind_Exception_Class exception_class,
184		      struct _Unwind_Exception *ue_header,
185		      struct _Unwind_Context *context)
186{
187  __cxa_exception *xh = __get_exception_header_from_ue (ue_header);
188
189  enum found_handler_type
190  {
191    found_nothing,
192    found_terminate,
193    found_cleanup,
194    found_handler
195  } found_type;
196
197  lsda_header_info info;
198  const unsigned char *language_specific_data;
199  const unsigned char *action_record;
200  const unsigned char *p;
201  _Unwind_Ptr landing_pad, ip;
202  int handler_switch_value;
203  void *thrown_ptr = xh + 1;
204
205  // Interface version check.
206  if (version != 1)
207    return _URC_FATAL_PHASE1_ERROR;
208
209  // Shortcut for phase 2 found handler for domestic exception.
210  if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
211      && exception_class == __gxx_exception_class)
212    {
213      handler_switch_value = xh->handlerSwitchValue;
214      language_specific_data = xh->languageSpecificData;
215      landing_pad = (_Unwind_Ptr) xh->catchTemp;
216      found_type = (landing_pad == 0 ? found_terminate : found_handler);
217      goto install_context;
218    }
219
220  language_specific_data = (const unsigned char *)
221    _Unwind_GetLanguageSpecificData (context);
222
223  // If no LSDA, then there are no handlers or cleanups.
224  if (! language_specific_data)
225    return _URC_CONTINUE_UNWIND;
226
227  // Parse the LSDA header.
228  p = parse_lsda_header (context, language_specific_data, &info);
229  info.ttype_base = base_of_encoded_value (info.ttype_encoding, context);
230  ip = _Unwind_GetIP (context) - 1;
231  landing_pad = 0;
232  action_record = 0;
233  handler_switch_value = 0;
234
235#ifdef _GLIBCPP_SJLJ_EXCEPTIONS
236  // The given "IP" is an index into the call-site table, with two
237  // exceptions -- -1 means no-action, and 0 means terminate.  But
238  // since we're using uleb128 values, we've not got random access
239  // to the array.
240  if ((int) ip < 0)
241    return _URC_CONTINUE_UNWIND;
242  else if (ip == 0)
243    {
244      // Fall through to set found_terminate.
245    }
246  else
247    {
248      _Unwind_Word cs_lp, cs_action;
249      do
250	{
251	  p = read_uleb128 (p, &cs_lp);
252	  p = read_uleb128 (p, &cs_action);
253	}
254      while (--ip);
255
256      // Can never have null landing pad for sjlj -- that would have
257      // been indicated by a -1 call site index.
258      landing_pad = cs_lp + 1;
259      if (cs_action)
260	action_record = info.action_table + cs_action - 1;
261      goto found_something;
262    }
263#else
264  // Search the call-site table for the action associated with this IP.
265  while (p < info.action_table)
266    {
267      _Unwind_Ptr cs_start, cs_len, cs_lp;
268      _Unwind_Word cs_action;
269
270      // Note that all call-site encodings are "absolute" displacements.
271      p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
272      p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
273      p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
274      p = read_uleb128 (p, &cs_action);
275
276      // The table is sorted, so if we've passed the ip, stop.
277      if (ip < info.Start + cs_start)
278	p = info.action_table;
279      else if (ip < info.Start + cs_start + cs_len)
280	{
281	  if (cs_lp)
282	    landing_pad = info.LPStart + cs_lp;
283	  if (cs_action)
284	    action_record = info.action_table + cs_action - 1;
285	  goto found_something;
286	}
287    }
288#endif // _GLIBCPP_SJLJ_EXCEPTIONS
289
290  // If ip is not present in the table, call terminate.  This is for
291  // a destructor inside a cleanup, or a library routine the compiler
292  // was not expecting to throw.
293  found_type = found_terminate;
294  goto do_something;
295
296 found_something:
297  if (landing_pad == 0)
298    {
299      // If ip is present, and has a null landing pad, there are
300      // no cleanups or handlers to be run.
301      found_type = found_nothing;
302    }
303  else if (action_record == 0)
304    {
305      // If ip is present, has a non-null landing pad, and a null
306      // action table offset, then there are only cleanups present.
307      // Cleanups use a zero switch value, as set above.
308      found_type = found_cleanup;
309    }
310  else
311    {
312      // Otherwise we have a catch handler or exception specification.
313
314      _Unwind_Sword ar_filter, ar_disp;
315      const std::type_info *throw_type, *catch_type;
316      bool saw_cleanup = false;
317      bool saw_handler = false;
318
319      // During forced unwinding, we only run cleanups.  With a foreign
320      // exception class, there's no exception type.
321      // ??? What to do about GNU Java and GNU Ada exceptions.
322
323      if ((actions & _UA_FORCE_UNWIND)
324	  || exception_class != __gxx_exception_class)
325	throw_type = 0;
326      else
327	throw_type = xh->exceptionType;
328
329      while (1)
330	{
331	  p = action_record;
332	  p = read_sleb128 (p, &ar_filter);
333	  read_sleb128 (p, &ar_disp);
334
335	  if (ar_filter == 0)
336	    {
337	      // Zero filter values are cleanups.
338	      saw_cleanup = true;
339	    }
340	  else if (ar_filter > 0)
341	    {
342	      // Positive filter values are handlers.
343	      catch_type = get_ttype_entry (&info, ar_filter);
344
345	      // Null catch type is a catch-all handler; we can catch foreign
346	      // exceptions with this.  Otherwise we must match types.
347	      if (! catch_type
348		  || (throw_type
349		      && get_adjusted_ptr (catch_type, throw_type,
350					   &thrown_ptr)))
351		{
352		  saw_handler = true;
353		  break;
354		}
355	    }
356	  else
357	    {
358	      // Negative filter values are exception specifications.
359	      // ??? How do foreign exceptions fit in?  As far as I can
360	      // see we can't match because there's no __cxa_exception
361	      // object to stuff bits in for __cxa_call_unexpected to use.
362	      // Allow them iff the exception spec is non-empty.  I.e.
363	      // a throw() specification results in __unexpected.
364	      if (throw_type
365		  ? ! check_exception_spec (&info, throw_type, thrown_ptr,
366					    ar_filter)
367		  : empty_exception_spec (&info, ar_filter))
368		{
369		  saw_handler = true;
370		  break;
371		}
372	    }
373
374	  if (ar_disp == 0)
375	    break;
376	  action_record = p + ar_disp;
377	}
378
379      if (saw_handler)
380	{
381	  handler_switch_value = ar_filter;
382	  found_type = found_handler;
383	}
384      else
385	found_type = (saw_cleanup ? found_cleanup : found_nothing);
386    }
387
388 do_something:
389   if (found_type == found_nothing)
390     return _URC_CONTINUE_UNWIND;
391
392  if (actions & _UA_SEARCH_PHASE)
393    {
394      if (found_type == found_cleanup)
395	return _URC_CONTINUE_UNWIND;
396
397      // For domestic exceptions, we cache data from phase 1 for phase 2.
398      if (exception_class == __gxx_exception_class)
399        {
400          xh->handlerSwitchValue = handler_switch_value;
401          xh->actionRecord = action_record;
402          xh->languageSpecificData = language_specific_data;
403          xh->adjustedPtr = thrown_ptr;
404
405          // ??? Completely unknown what this field is supposed to be for.
406          // ??? Need to cache TType encoding base for call_unexpected.
407          xh->catchTemp = landing_pad;
408	}
409      return _URC_HANDLER_FOUND;
410    }
411
412 install_context:
413  // We can't use any of the cxa routines with foreign exceptions,
414  // because they all expect ue_header to be a struct __cxa_exception.
415  // So in that case, call terminate or unexpected directly.
416  if ((actions & _UA_FORCE_UNWIND)
417      || exception_class != __gxx_exception_class)
418    {
419      if (found_type == found_terminate)
420	std::terminate ();
421      else if (handler_switch_value < 0)
422	{
423	  try
424	    { std::unexpected (); }
425	  catch(...)
426	    { std::terminate (); }
427	}
428    }
429  else
430    {
431      if (found_type == found_terminate)
432	{
433	  __cxa_begin_catch (&xh->unwindHeader);
434	  __terminate (xh->terminateHandler);
435	}
436
437      // Cache the TType base value for __cxa_call_unexpected, as we won't
438      // have an _Unwind_Context then.
439      if (handler_switch_value < 0)
440	{
441	  parse_lsda_header (context, language_specific_data, &info);
442	  xh->catchTemp = base_of_encoded_value (info.ttype_encoding, context);
443	}
444    }
445
446  _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
447		 (_Unwind_Ptr) &xh->unwindHeader);
448  _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
449		 handler_switch_value);
450  _Unwind_SetIP (context, landing_pad);
451  return _URC_INSTALL_CONTEXT;
452}
453
454extern "C" void
455__cxa_call_unexpected (void *exc_obj_in)
456{
457  _Unwind_Exception *exc_obj
458    = reinterpret_cast <_Unwind_Exception *>(exc_obj_in);
459
460  __cxa_begin_catch (exc_obj);
461
462  // This function is a handler for our exception argument.  If we exit
463  // by throwing a different exception, we'll need the original cleaned up.
464  struct end_catch_protect
465  {
466    end_catch_protect() { }
467    ~end_catch_protect() { __cxa_end_catch(); }
468  } end_catch_protect_obj;
469
470  lsda_header_info info;
471  __cxa_exception *xh = __get_exception_header_from_ue (exc_obj);
472  const unsigned char *xh_lsda;
473  _Unwind_Sword xh_switch_value;
474  std::terminate_handler xh_terminate_handler;
475
476  // If the unexpectedHandler rethrows the exception (e.g. to categorize it),
477  // it will clobber data about the current handler.  So copy the data out now.
478  xh_lsda = xh->languageSpecificData;
479  xh_switch_value = xh->handlerSwitchValue;
480  xh_terminate_handler = xh->terminateHandler;
481  info.ttype_base = (_Unwind_Ptr) xh->catchTemp;
482
483  try
484    { __unexpected (xh->unexpectedHandler); }
485  catch(...)
486    {
487      // Get the exception thrown from unexpected.
488
489      __cxa_eh_globals *globals = __cxa_get_globals_fast ();
490      __cxa_exception *new_xh = globals->caughtExceptions;
491      void *new_ptr = new_xh + 1;
492
493      // We don't quite have enough stuff cached; re-parse the LSDA.
494      parse_lsda_header (0, xh_lsda, &info);
495
496      // If this new exception meets the exception spec, allow it.
497      if (check_exception_spec (&info, new_xh->exceptionType,
498				new_ptr, xh_switch_value))
499	__throw_exception_again;
500
501      // If the exception spec allows std::bad_exception, throw that.
502      // We don't have a thrown object to compare against, but since
503      // bad_exception doesn't have virtual bases, that's OK; just pass 0.
504#ifdef __EXCEPTIONS
505      const std::type_info &bad_exc = typeid (std::bad_exception);
506      if (check_exception_spec (&info, &bad_exc, 0, xh_switch_value))
507	throw std::bad_exception();
508#endif
509
510      // Otherwise, die.
511      __terminate (xh_terminate_handler);
512    }
513}
514