1 // -*- C++ -*- Exception handling routines for catching.
2 // Copyright (C) 2001, 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 <cstdlib>
32 #include "unwind-cxx.h"
33
34 using namespace __cxxabiv1;
35
36
37 extern "C" void *
__cxa_begin_catch(void * exc_obj_in)38 __cxa_begin_catch (void *exc_obj_in)
39 {
40 _Unwind_Exception *exceptionObject
41 = reinterpret_cast <_Unwind_Exception *>(exc_obj_in);
42 __cxa_eh_globals *globals = __cxa_get_globals ();
43 __cxa_exception *prev = globals->caughtExceptions;
44 __cxa_exception *header = __get_exception_header_from_ue (exceptionObject);
45
46 // Foreign exceptions can't be stacked here. If the exception stack is
47 // empty, then fine. Otherwise we really have no choice but to terminate.
48 // Note that this use of "header" is a lie. It's fine so long as we only
49 // examine header->unwindHeader though.
50 if (header->unwindHeader.exception_class != __gxx_exception_class)
51 {
52 if (prev != 0)
53 std::terminate ();
54
55 // Remember for end_catch and rethrow.
56 globals->caughtExceptions = header;
57
58 // ??? No sensible value to return; we don't know what the
59 // object is, much less where it is in relation to the header.
60 return 0;
61 }
62
63 int count = header->handlerCount;
64 if (count < 0)
65 // This exception was rethrown from an immediately enclosing region.
66 count = -count + 1;
67 else
68 count += 1;
69 header->handlerCount = count;
70
71 globals->uncaughtExceptions -= 1;
72 if (header != prev)
73 {
74 header->nextException = prev;
75 globals->caughtExceptions = header;
76 }
77
78 return header->adjustedPtr;
79 }
80
81
82 extern "C" void
__cxa_end_catch()83 __cxa_end_catch ()
84 {
85 __cxa_eh_globals *globals = __cxa_get_globals_fast ();
86 __cxa_exception *header = globals->caughtExceptions;
87
88 // A rethrow of a foreign exception will be removed from the
89 // the exception stack immediately by __cxa_rethrow.
90 if (!header)
91 return;
92
93 // A foreign exception couldn't have been stacked (see above),
94 // so by definition processing must be complete.
95 if (header->unwindHeader.exception_class != __gxx_exception_class)
96 {
97 globals->caughtExceptions = 0;
98 _Unwind_DeleteException (&header->unwindHeader);
99 return;
100 }
101
102 int count = header->handlerCount;
103 if (count < 0)
104 {
105 // This exception was rethrown. Decrement the (inverted) catch
106 // count and remove it from the chain when it reaches zero.
107 if (++count == 0)
108 {
109 globals->uncaughtExceptions += 1;
110 globals->caughtExceptions = header->nextException;
111 }
112 }
113 else if (--count == 0)
114 {
115 // Handling for this exception is complete. Destroy the object.
116 globals->caughtExceptions = header->nextException;
117 _Unwind_DeleteException (&header->unwindHeader);
118 return;
119 }
120 else if (count < 0)
121 // A bug in the exception handling library or compiler.
122 std::terminate ();
123
124 header->handlerCount = count;
125 }
126
127
128 bool
uncaught_exception()129 std::uncaught_exception() throw()
130 {
131 __cxa_eh_globals *globals = __cxa_get_globals ();
132 return globals->uncaughtExceptions != 0;
133 }
134