OSDN Git Service

* config/darwin.c (indirect_data): Fix typo in strncmp logic.
[pf3gnuchains/gcc-fork.git] / gcc / unwind.inc
1 /* Exception handling and frame unwind runtime interface routines. -*- C -*-
2    Copyright (C) 2001, 2003 Free Software Foundation, Inc.
3
4    This file is part of GCC.
5
6    GCC is free software; you can redistribute it and/or modify it
7    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    In addition to the permissions in the GNU General Public License, the
12    Free Software Foundation gives you unlimited permission to link the
13    compiled version of this file into combinations with other programs,
14    and to distribute those combinations without any restriction coming
15    from the use of this file.  (The General Public License restrictions
16    do apply in other respects; for example, they cover modification of
17    the file, and distribution when not linked into a combined
18    executable.)
19
20    GCC is distributed in the hope that it will be useful, but WITHOUT
21    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
23    License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with GCC; see the file COPYING.  If not, write to the Free
27    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28    02111-1307, USA.  */
29
30 /* This is derived from the C++ ABI for IA-64.  Where we diverge
31    for cross-architecture compatibility are noted with "@@@".  
32    This file is included from unwind-dw2.c or unwind-ia64.c.  */
33
34 /* Subroutine of _Unwind_RaiseException also invoked from _Unwind_Resume. 
35
36    Unwind the stack calling the personality routine to find both the
37    exception handler and intermediary cleanup code.  We'll only locate
38    the first such frame here.  Cleanup code will call back into
39    _Unwind_Resume and we'll continue Phase 2 there.  */
40
41 static _Unwind_Reason_Code
42 _Unwind_RaiseException_Phase2(struct _Unwind_Exception *exc,
43                               struct _Unwind_Context *context)
44 {
45   _Unwind_Reason_Code code;
46
47   while (1)
48     {
49       _Unwind_FrameState fs;
50       int match_handler;
51
52       code = uw_frame_state_for (context, &fs);
53
54       /* Identify when we've reached the designated handler context.  */
55       match_handler = (uw_identify_context (context) == exc->private_2
56                        ? _UA_HANDLER_FRAME : 0);
57
58       if (code != _URC_NO_REASON)
59         /* Some error encountered.  Usually the unwinder doesn't
60            diagnose these and merely crashes.  */
61         return _URC_FATAL_PHASE2_ERROR;
62
63       /* Unwind successful.  Run the personality routine, if any.  */
64       if (fs.personality)
65         {
66           code = (*fs.personality) (1, _UA_CLEANUP_PHASE | match_handler,
67                                     exc->exception_class, exc, context);
68           if (code == _URC_INSTALL_CONTEXT)
69             break;
70           if (code != _URC_CONTINUE_UNWIND) 
71             return _URC_FATAL_PHASE2_ERROR;
72         }
73
74       /* Don't let us unwind past the handler context.  */
75       if (match_handler)
76         abort ();
77
78       uw_update_context (context, &fs);
79     }
80
81   return code;
82 }
83
84 /* Raise an exception, passing along the given exception object.  */
85
86 _Unwind_Reason_Code
87 _Unwind_RaiseException(struct _Unwind_Exception *exc)
88 {
89   struct _Unwind_Context this_context, cur_context;
90   _Unwind_Reason_Code code;
91
92   /* Set up this_context to describe the current stack frame.  */
93   uw_init_context (&this_context);
94   cur_context = this_context;
95
96   /* Phase 1: Search.  Unwind the stack, calling the personality routine
97      with the _UA_SEARCH_PHASE flag set.  Do not modify the stack yet.  */
98   while (1)
99     {
100       _Unwind_FrameState fs;
101
102       /* Set up fs to describe the FDE for the caller of cur_context.  The
103          first time through the loop, that means __cxa_throw.  */
104       code = uw_frame_state_for (&cur_context, &fs);
105
106       if (code == _URC_END_OF_STACK)
107         /* Hit end of stack with no handler found.  */
108         return _URC_END_OF_STACK;
109
110       if (code != _URC_NO_REASON)
111         /* Some error encountered.  Ususally the unwinder doesn't
112            diagnose these and merely crashes.  */
113         return _URC_FATAL_PHASE1_ERROR;
114
115       /* Unwind successful.  Run the personality routine, if any.  */
116       if (fs.personality)
117         {
118           code = (*fs.personality) (1, _UA_SEARCH_PHASE, exc->exception_class,
119                                     exc, &cur_context);
120           if (code == _URC_HANDLER_FOUND)
121             break;
122           else if (code != _URC_CONTINUE_UNWIND)
123             return _URC_FATAL_PHASE1_ERROR;
124         }
125
126       /* Update cur_context to describe the same frame as fs.  */
127       uw_update_context (&cur_context, &fs);
128     }
129
130   /* Indicate to _Unwind_Resume and associated subroutines that this
131      is not a forced unwind.  Further, note where we found a handler.  */
132   exc->private_1 = 0;
133   exc->private_2 = uw_identify_context (&cur_context);
134
135   cur_context = this_context;
136   code = _Unwind_RaiseException_Phase2 (exc, &cur_context);
137   if (code != _URC_INSTALL_CONTEXT)
138     return code;
139
140   uw_install_context (&this_context, &cur_context);
141 }
142
143
144 /* Subroutine of _Unwind_ForcedUnwind also invoked from _Unwind_Resume.  */
145
146 static _Unwind_Reason_Code
147 _Unwind_ForcedUnwind_Phase2(struct _Unwind_Exception *exc,
148                             struct _Unwind_Context *context)
149 {
150   _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) (_Unwind_Ptr) exc->private_1;
151   void *stop_argument = (void *) (_Unwind_Ptr) exc->private_2;
152   _Unwind_Reason_Code code, stop_code;
153
154   while (1)
155     {
156       _Unwind_FrameState fs;
157       int action;
158
159       /* Set up fs to describe the FDE for the caller of cur_context.  */
160       code = uw_frame_state_for (context, &fs);
161       if (code != _URC_NO_REASON && code != _URC_END_OF_STACK)
162         return _URC_FATAL_PHASE2_ERROR;
163
164       /* Unwind successful.  */
165       action = _UA_FORCE_UNWIND | _UA_CLEANUP_PHASE;
166       if (code == _URC_END_OF_STACK)
167         action |= _UA_END_OF_STACK;
168       stop_code = (*stop) (1, action, exc->exception_class, exc,
169                            context, stop_argument);
170       if (stop_code != _URC_NO_REASON)
171         return _URC_FATAL_PHASE2_ERROR;
172
173       /* Stop didn't want to do anything.  Invoke the personality
174          handler, if applicable, to run cleanups.  */
175       if (code == _URC_END_OF_STACK)
176         break;
177         
178       if (fs.personality)
179         {
180           code = (*fs.personality) (1, _UA_FORCE_UNWIND | _UA_CLEANUP_PHASE,
181                                     exc->exception_class, exc, context);
182           if (code == _URC_INSTALL_CONTEXT)
183             break;
184           if (code != _URC_CONTINUE_UNWIND) 
185             return _URC_FATAL_PHASE2_ERROR;
186         }
187
188       /* Update cur_context to describe the same frame as fs.  */
189       uw_update_context (context, &fs);
190     }
191
192   return code;
193 }
194
195
196 /* Raise an exception for forced unwinding.  */
197
198 _Unwind_Reason_Code
199 _Unwind_ForcedUnwind (struct _Unwind_Exception *exc,
200                       _Unwind_Stop_Fn stop, void * stop_argument)
201 {
202   struct _Unwind_Context this_context, cur_context;
203   _Unwind_Reason_Code code;
204
205   uw_init_context (&this_context);
206   cur_context = this_context;
207
208   exc->private_1 = (_Unwind_Ptr) stop;
209   exc->private_2 = (_Unwind_Ptr) stop_argument;
210
211   code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context);
212   if (code != _URC_INSTALL_CONTEXT)
213     return code;
214
215   uw_install_context (&this_context, &cur_context);
216 }
217
218
219 /* Resume propagation of an existing exception.  This is used after
220    e.g. executing cleanup code, and not to implement rethrowing.  */
221
222 void
223 _Unwind_Resume (struct _Unwind_Exception *exc)
224 {
225   struct _Unwind_Context this_context, cur_context;
226   _Unwind_Reason_Code code;
227
228   uw_init_context (&this_context);
229   cur_context = this_context;
230
231   /* Choose between continuing to process _Unwind_RaiseException
232      or _Unwind_ForcedUnwind.  */
233   if (exc->private_1 == 0)
234     code = _Unwind_RaiseException_Phase2 (exc, &cur_context);
235   else
236     code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context);
237
238   if (code != _URC_INSTALL_CONTEXT)
239     abort ();
240
241   uw_install_context (&this_context, &cur_context);
242 }
243
244
245 /* Resume propagation of an FORCE_UNWIND exception, or to rethrow
246    a normal exception that was handled.  */
247
248 _Unwind_Reason_Code
249 _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *exc)
250 {
251   struct _Unwind_Context this_context, cur_context;
252   _Unwind_Reason_Code code;
253
254   /* Choose between continuing to process _Unwind_RaiseException
255      or _Unwind_ForcedUnwind.  */
256   if (exc->private_1 == 0)
257     return _Unwind_RaiseException (exc);
258
259   uw_init_context (&this_context);
260   cur_context = this_context;
261
262   code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context);
263
264   if (code != _URC_INSTALL_CONTEXT)
265     abort ();
266
267   uw_install_context (&this_context, &cur_context);
268 }
269
270
271 /* A convenience function that calls the exception_cleanup field.  */
272
273 void
274 _Unwind_DeleteException (struct _Unwind_Exception *exc)
275 {
276   if (exc->exception_cleanup)
277     (*exc->exception_cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exc);
278 }
279
280
281 /* Perform stack backtrace through unwind data.  */
282
283 _Unwind_Reason_Code
284 _Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument)
285 {
286   struct _Unwind_Context context;
287   _Unwind_Reason_Code code;
288
289   uw_init_context (&context);
290
291   while (1)
292     {
293       _Unwind_FrameState fs;
294
295       /* Set up fs to describe the FDE for the caller of context.  */
296       code = uw_frame_state_for (&context, &fs);
297       if (code != _URC_NO_REASON && code != _URC_END_OF_STACK)
298         return _URC_FATAL_PHASE1_ERROR;
299
300       /* Call trace function.  */
301       if ((*trace) (&context, trace_argument) != _URC_NO_REASON)
302         return _URC_FATAL_PHASE1_ERROR;
303
304       /* We're done at end of stack.  */        
305       if (code == _URC_END_OF_STACK)
306         break;
307
308       /* Update context to describe the same frame as fs.  */
309       uw_update_context (&context, &fs);
310     }
311
312   return code;
313 }