OSDN Git Service

PR c++/40373
[pf3gnuchains/gcc-fork.git] / gcc / ada / seh_init.c
1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                              S E H - I N I T                             *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *           Copyright (C) 2005-2009, Free Software Foundation, Inc.        *
10  *                                                                          *
11  * GNAT is free software;  you can  redistribute it  and/or modify it under *
12  * terms of the  GNU General Public License as published  by the Free Soft- *
13  * ware  Foundation;  either version 3,  or (at your option) any later ver- *
14  * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
15  * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
16  * or FITNESS FOR A PARTICULAR PURPOSE.                                     *
17  *                                                                          *
18  * As a special exception under Section 7 of GPL version 3, you are granted *
19  * additional permissions described in the GCC Runtime Library Exception,   *
20  * version 3.1, as published by the Free Software Foundation.               *
21  *                                                                          *
22  * You should have received a copy of the GNU General Public License and    *
23  * a copy of the GCC Runtime Library Exception along with this program;     *
24  * see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    *
25  * <http://www.gnu.org/licenses/>.                                          *
26  *                                                                          *
27  * GNAT was originally developed  by the GNAT team at  New York University. *
28  * Extensive contributions were provided by Ada Core Technologies Inc.      *
29  *                                                                          *
30  ****************************************************************************/
31
32 /*  This unit contains support for SEH (Structured Exception Handling).
33     Right now the only implementation is for Win32.  */
34
35 #ifdef IN_RTS
36 #include "tconfig.h"
37 #include "tsystem.h"
38
39 /* We don't have libiberty, so use malloc.  */
40 #define xmalloc(S) malloc (S)
41
42 #else
43 #include "config.h"
44 #include "system.h"
45 #endif
46
47 #include "raise.h"
48
49 /* Addresses of exception data blocks for predefined exceptions. */
50 extern struct Exception_Data constraint_error;
51 extern struct Exception_Data numeric_error;
52 extern struct Exception_Data program_error;
53 extern struct Exception_Data storage_error;
54 extern struct Exception_Data tasking_error;
55 extern struct Exception_Data _abort_signal;
56
57 #define Raise_From_Signal_Handler \
58                       ada__exceptions__raise_from_signal_handler
59 extern void Raise_From_Signal_Handler (struct Exception_Data *, const char *);
60
61
62 #if defined (_WIN32) && !defined (_WIN64)
63
64 #include <windows.h>
65 #include <excpt.h>
66
67 extern void _global_unwind2 (void *);
68
69 EXCEPTION_DISPOSITION __gnat_SEH_error_handler
70 (struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*);
71
72 EXCEPTION_DISPOSITION
73 __gnat_SEH_error_handler (struct _EXCEPTION_RECORD* ExceptionRecord,
74                           void *EstablisherFrame,
75                           struct _CONTEXT* ContextRecord ATTRIBUTE_UNUSED,
76                           void *DispatcherContext ATTRIBUTE_UNUSED)
77 {
78   struct Exception_Data *exception;
79   const char *msg;
80
81   switch (ExceptionRecord->ExceptionCode)
82     {
83     case EXCEPTION_ACCESS_VIOLATION:
84       /* If the failing address isn't maximally-aligned or if the page
85          before the faulting page is not accessible, this is a program error.
86       */
87       if ((ExceptionRecord->ExceptionInformation[1] & 3) != 0
88           || IsBadCodePtr
89           ((void *)(ExceptionRecord->ExceptionInformation[1] + 4096)))
90         {
91           exception = &program_error;
92           msg = "EXCEPTION_ACCESS_VIOLATION";
93         }
94       else
95         {
96           /* otherwise it is a stack overflow  */
97           exception = &storage_error;
98           msg = "stack overflow (or erroneous memory access)";
99         }
100       break;
101
102     case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
103       exception = &constraint_error;
104       msg = "EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
105       break;
106
107     case EXCEPTION_DATATYPE_MISALIGNMENT:
108       exception = &constraint_error;
109       msg = "EXCEPTION_DATATYPE_MISALIGNMENT";
110       break;
111
112     case EXCEPTION_FLT_DENORMAL_OPERAND:
113       exception = &constraint_error;
114       msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
115       break;
116
117     case EXCEPTION_FLT_DIVIDE_BY_ZERO:
118       exception = &constraint_error;
119       msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
120       break;
121
122     case EXCEPTION_FLT_INVALID_OPERATION:
123       exception = &constraint_error;
124       msg = "EXCEPTION_FLT_INVALID_OPERATION";
125       break;
126
127     case EXCEPTION_FLT_OVERFLOW:
128       exception = &constraint_error;
129       msg = "EXCEPTION_FLT_OVERFLOW";
130       break;
131
132     case EXCEPTION_FLT_STACK_CHECK:
133       exception = &program_error;
134       msg = "EXCEPTION_FLT_STACK_CHECK";
135       break;
136
137     case EXCEPTION_FLT_UNDERFLOW:
138       exception = &constraint_error;
139       msg = "EXCEPTION_FLT_UNDERFLOW";
140       break;
141
142     case EXCEPTION_INT_DIVIDE_BY_ZERO:
143       exception = &constraint_error;
144       msg = "EXCEPTION_INT_DIVIDE_BY_ZERO";
145       break;
146
147     case EXCEPTION_INT_OVERFLOW:
148       exception = &constraint_error;
149       msg = "EXCEPTION_INT_OVERFLOW";
150       break;
151
152     case EXCEPTION_INVALID_DISPOSITION:
153       exception = &program_error;
154       msg = "EXCEPTION_INVALID_DISPOSITION";
155       break;
156
157     case EXCEPTION_NONCONTINUABLE_EXCEPTION:
158       exception = &program_error;
159       msg = "EXCEPTION_NONCONTINUABLE_EXCEPTION";
160       break;
161
162     case EXCEPTION_PRIV_INSTRUCTION:
163       exception = &program_error;
164       msg = "EXCEPTION_PRIV_INSTRUCTION";
165       break;
166
167     case EXCEPTION_SINGLE_STEP:
168       exception = &program_error;
169       msg = "EXCEPTION_SINGLE_STEP";
170       break;
171
172     case EXCEPTION_STACK_OVERFLOW:
173       exception = &storage_error;
174       msg = "EXCEPTION_STACK_OVERFLOW";
175       break;
176
177    default:
178       exception = &program_error;
179       msg = "unhandled signal";
180     }
181
182   /* This call is important as it avoids locking the second time we catch a
183      signal. Note that this routine is documented as internal to Windows and
184      should not be used.  */
185
186   _global_unwind2 (EstablisherFrame);
187   /* Call equivalent to RtlUnwind (EstablisherFrame, NULL, NULL, 0); */
188
189   Raise_From_Signal_Handler (exception, msg);
190   return 0; /* This is never reached, avoid compiler warning  */
191 }
192
193 /*  Install the Win32 SEH exception handler. Note that the caller must have
194     allocated 8 bytes on the stack and pass the pointer to this stack
195     space. This is needed as the SEH exception handler must be on the stack of
196     the thread.
197
198        int buf[2];
199
200        __gnat_install_SEH_handler ((void*)buf);
201
202        main();
203
204    This call must be done before calling the main procedure or the thread
205    entry. The stack space must exists during all the main run.  */
206
207 void
208 __gnat_install_SEH_handler (void *ER)
209 {
210   int *ptr;
211
212   /* put current handler in ptr */
213
214   asm ("mov %%fs:(0),%%ecx" : : : "%ecx");
215   asm ("mov %%ecx,%0" : "=m" (ptr));
216
217   ((int *)ER)[0] = (int)ptr;                       /* previous handler */
218   ((int *)ER)[1] = (int)__gnat_SEH_error_handler;  /* new handler */
219
220   /* ptr is the new handler, set fs:(0) with this value */
221
222   ptr = (int *)ER;
223   asm ("mov %0,%%ecx" : : "m" (ptr) : "%ecx");
224   asm ("mov %ecx,%fs:(0)");
225 }
226
227 #else /* defined (_WIN32) && !defined (_WIN64) */
228 /* For all non Windows targets we provide a dummy SEH install handler.  */
229 void __gnat_install_SEH_handler (void *eh ATTRIBUTE_UNUSED)
230 {
231 }
232 #endif