1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2005-2009, Free Software Foundation, Inc. *
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. *
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. *
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/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 ****************************************************************************/
32 /* This unit contains support for SEH (Structured Exception Handling).
33 Right now the only implementation is for Win32. */
39 /* We don't have libiberty, so use malloc. */
40 #define xmalloc(S) malloc (S)
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;
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 *);
62 #if defined (_WIN32) && !defined (_WIN64)
67 extern void _global_unwind2 (void *);
69 EXCEPTION_DISPOSITION __gnat_SEH_error_handler
70 (struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*);
73 __gnat_SEH_error_handler (struct _EXCEPTION_RECORD* ExceptionRecord,
74 void *EstablisherFrame,
75 struct _CONTEXT* ContextRecord ATTRIBUTE_UNUSED,
76 void *DispatcherContext ATTRIBUTE_UNUSED)
78 struct Exception_Data *exception;
81 switch (ExceptionRecord->ExceptionCode)
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.
87 if ((ExceptionRecord->ExceptionInformation[1] & 3) != 0
89 ((void *)(ExceptionRecord->ExceptionInformation[1] + 4096)))
91 exception = &program_error;
92 msg = "EXCEPTION_ACCESS_VIOLATION";
96 /* otherwise it is a stack overflow */
97 exception = &storage_error;
98 msg = "stack overflow (or erroneous memory access)";
102 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
103 exception = &constraint_error;
104 msg = "EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
107 case EXCEPTION_DATATYPE_MISALIGNMENT:
108 exception = &constraint_error;
109 msg = "EXCEPTION_DATATYPE_MISALIGNMENT";
112 case EXCEPTION_FLT_DENORMAL_OPERAND:
113 exception = &constraint_error;
114 msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
117 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
118 exception = &constraint_error;
119 msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
122 case EXCEPTION_FLT_INVALID_OPERATION:
123 exception = &constraint_error;
124 msg = "EXCEPTION_FLT_INVALID_OPERATION";
127 case EXCEPTION_FLT_OVERFLOW:
128 exception = &constraint_error;
129 msg = "EXCEPTION_FLT_OVERFLOW";
132 case EXCEPTION_FLT_STACK_CHECK:
133 exception = &program_error;
134 msg = "EXCEPTION_FLT_STACK_CHECK";
137 case EXCEPTION_FLT_UNDERFLOW:
138 exception = &constraint_error;
139 msg = "EXCEPTION_FLT_UNDERFLOW";
142 case EXCEPTION_INT_DIVIDE_BY_ZERO:
143 exception = &constraint_error;
144 msg = "EXCEPTION_INT_DIVIDE_BY_ZERO";
147 case EXCEPTION_INT_OVERFLOW:
148 exception = &constraint_error;
149 msg = "EXCEPTION_INT_OVERFLOW";
152 case EXCEPTION_INVALID_DISPOSITION:
153 exception = &program_error;
154 msg = "EXCEPTION_INVALID_DISPOSITION";
157 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
158 exception = &program_error;
159 msg = "EXCEPTION_NONCONTINUABLE_EXCEPTION";
162 case EXCEPTION_PRIV_INSTRUCTION:
163 exception = &program_error;
164 msg = "EXCEPTION_PRIV_INSTRUCTION";
167 case EXCEPTION_SINGLE_STEP:
168 exception = &program_error;
169 msg = "EXCEPTION_SINGLE_STEP";
172 case EXCEPTION_STACK_OVERFLOW:
173 exception = &storage_error;
174 msg = "EXCEPTION_STACK_OVERFLOW";
178 exception = &program_error;
179 msg = "unhandled signal";
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. */
186 _global_unwind2 (EstablisherFrame);
187 /* Call equivalent to RtlUnwind (EstablisherFrame, NULL, NULL, 0); */
189 Raise_From_Signal_Handler (exception, msg);
190 return 0; /* This is never reached, avoid compiler warning */
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
200 __gnat_install_SEH_handler ((void*)buf);
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. */
208 __gnat_install_SEH_handler (void *ER)
212 /* put current handler in ptr */
214 asm ("mov %%fs:(0),%%ecx" : : : "%ecx");
215 asm ("mov %%ecx,%0" : "=m" (ptr));
217 ((int *)ER)[0] = (int)ptr; /* previous handler */
218 ((int *)ER)[1] = (int)__gnat_SEH_error_handler; /* new handler */
220 /* ptr is the new handler, set fs:(0) with this value */
223 asm ("mov %0,%%ecx" : : "m" (ptr) : "%ecx");
224 asm ("mov %ecx,%fs:(0)");
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)