Fix unused warnings.
[official-gcc/graphite-test-results.git] / gcc / ada / seh_init.c
blob012692a7ae5dea31582935f075cde0366a24cd2f
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 ****************************************************************************/
32 /* This unit contains support for SEH (Structured Exception Handling).
33 Right now the only implementation is for Win32. */
35 #ifdef IN_RTS
36 #include "tconfig.h"
37 #include "tsystem.h"
39 /* We don't have libiberty, so use malloc. */
40 #define xmalloc(S) malloc (S)
42 #else
43 #include "config.h"
44 #include "system.h"
45 #endif
47 #include "raise.h"
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)
64 #include <windows.h>
65 #include <excpt.h>
67 extern void _global_unwind2 (void *);
69 EXCEPTION_DISPOSITION __gnat_SEH_error_handler
70 (struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*);
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)
78 struct Exception_Data *exception;
79 const char *msg;
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
88 || IsBadCodePtr
89 ((void *)(ExceptionRecord->ExceptionInformation[1] + 4096)))
91 exception = &program_error;
92 msg = "EXCEPTION_ACCESS_VIOLATION";
94 else
96 /* otherwise it is a stack overflow */
97 exception = &storage_error;
98 msg = "stack overflow (or erroneous memory access)";
100 break;
102 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
103 exception = &constraint_error;
104 msg = "EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
105 break;
107 case EXCEPTION_DATATYPE_MISALIGNMENT:
108 exception = &constraint_error;
109 msg = "EXCEPTION_DATATYPE_MISALIGNMENT";
110 break;
112 case EXCEPTION_FLT_DENORMAL_OPERAND:
113 exception = &constraint_error;
114 msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
115 break;
117 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
118 exception = &constraint_error;
119 msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
120 break;
122 case EXCEPTION_FLT_INVALID_OPERATION:
123 exception = &constraint_error;
124 msg = "EXCEPTION_FLT_INVALID_OPERATION";
125 break;
127 case EXCEPTION_FLT_OVERFLOW:
128 exception = &constraint_error;
129 msg = "EXCEPTION_FLT_OVERFLOW";
130 break;
132 case EXCEPTION_FLT_STACK_CHECK:
133 exception = &program_error;
134 msg = "EXCEPTION_FLT_STACK_CHECK";
135 break;
137 case EXCEPTION_FLT_UNDERFLOW:
138 exception = &constraint_error;
139 msg = "EXCEPTION_FLT_UNDERFLOW";
140 break;
142 case EXCEPTION_INT_DIVIDE_BY_ZERO:
143 exception = &constraint_error;
144 msg = "EXCEPTION_INT_DIVIDE_BY_ZERO";
145 break;
147 case EXCEPTION_INT_OVERFLOW:
148 exception = &constraint_error;
149 msg = "EXCEPTION_INT_OVERFLOW";
150 break;
152 case EXCEPTION_INVALID_DISPOSITION:
153 exception = &program_error;
154 msg = "EXCEPTION_INVALID_DISPOSITION";
155 break;
157 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
158 exception = &program_error;
159 msg = "EXCEPTION_NONCONTINUABLE_EXCEPTION";
160 break;
162 case EXCEPTION_PRIV_INSTRUCTION:
163 exception = &program_error;
164 msg = "EXCEPTION_PRIV_INSTRUCTION";
165 break;
167 case EXCEPTION_SINGLE_STEP:
168 exception = &program_error;
169 msg = "EXCEPTION_SINGLE_STEP";
170 break;
172 case EXCEPTION_STACK_OVERFLOW:
173 exception = &storage_error;
174 msg = "EXCEPTION_STACK_OVERFLOW";
175 break;
177 default:
178 exception = &program_error;
179 msg = "unhandled signal";
182 #if ! defined (_WIN64)
183 /* This call is important as it avoids locking the second time we catch a
184 signal. Note that this routine is documented as internal to Windows and
185 should not be used. */
187 _global_unwind2 (EstablisherFrame);
188 /* Call equivalent to RtlUnwind (EstablisherFrame, NULL, NULL, 0); */
189 #endif
191 Raise_From_Signal_Handler (exception, msg);
192 return 0; /* This is never reached, avoid compiler warning */
195 #if defined (_WIN64)
196 /* On x86_64 windows exception mechanism is no more based on a chained list
197 of handlers addresses on the stack. Instead unwinding information is used
198 to retrieve the exception handler (similar to ZCX GCC mechanism). So in
199 order to register an exception handler we need to put in the final
200 executable some unwinding information. This information might be present
201 statically in the image file inside the .pdata section or registered
202 through RtlAddFunctionTable API. Currently the GCC toolchain does not
203 generate the .pdata information for each function. As we don't need to
204 handle SEH exceptions except for signal handling we are registering a
205 "fake" unwinding data that associate a SEH exception handler to the
206 complete .text section. As we never return from the handler, the system
207 does not try to do the final unwinding using the pdata information. The
208 unwinding is handled by the runtime using either the GNAT SJLJ mechanism
209 or the ZCX GCC mechanism.
211 The current implementation is using the RtlAddFunctionTable. Here is for
212 information purposes the equivalent using a static .pdata section:
214 .section .rdata,"dr"
215 .align 4
216 Lunwind_info:
217 .byte 9,0,0,0
218 .rva ___gnat_SEH_error_handler
219 .section .pdata,"dr"
220 .align 4
221 .long 0
222 .rva etext
223 .rva Lunwind_info
225 Solutions based on SetUnhandledExceptionFilter have been discarded as this
226 function is mostly disabled on last Windows versions.
227 Using AddVectoredExceptionHandler should also be discarded as it overrides
228 all SEH exception handlers that might be present in the program itself and
229 the loaded DLL (for example it results in unexpected behaviors in the
230 Win32 subsystem. */
232 typedef struct _UNWIND_INFO {
233 BYTE VersionAndFlags;
234 BYTE PrologSize;
235 BYTE CountOfUnwindCodes;
236 BYTE FrameRegisterAndOffset;
237 ULONG AddressOfExceptionHandler;
238 } UNWIND_INFO,*PUNWIND_INFO;
240 static RUNTIME_FUNCTION Table[1];
241 static UNWIND_INFO unwind_info[1];
243 #define UNW_VERSION 0x01
244 #define UNW_FLAG_EHANDLER 0x08
246 void __gnat_install_SEH_handler (void *eh ATTRIBUTE_UNUSED)
248 /* Get the end of the text section. */
249 extern char etext[] asm("etext");
250 /* Get the base of the module. */
251 extern char __ImageBase[];
253 /* Current version is always 1 and we are registering an
254 exception handler. */
255 unwind_info[0].VersionAndFlags = UNW_FLAG_EHANDLER | UNW_VERSION;
257 /* We don't use the unwinding info so fill the structure with 0 values. */
258 unwind_info[0].PrologSize = 0;
259 unwind_info[0].CountOfUnwindCodes = 0;
260 unwind_info[0].FrameRegisterAndOffset = 0;
262 /* Add the exception handler. */
263 unwind_info[0].AddressOfExceptionHandler =
264 (DWORD)((char *)__gnat_SEH_error_handler - __ImageBase);
266 /* Set its scope to the entire program. */
267 Table[0].BeginAddress = 0;
268 Table[0].EndAddress = (DWORD)(etext - __ImageBase);
269 Table[0].UnwindData = (DWORD)((char *)unwind_info - __ImageBase);
271 /* Register the unwind information. */
272 RtlAddFunctionTable (Table, 1, (DWORD64)__ImageBase);
275 #else /* defined (_WIN64) */
276 /* Install the Win32 SEH exception handler. Note that the caller must have
277 allocated 8 bytes on the stack and pass the pointer to this stack
278 space. This is needed as the SEH exception handler must be on the stack of
279 the thread.
281 int buf[2];
283 __gnat_install_SEH_handler ((void*)buf);
285 main();
287 This call must be done before calling the main procedure or the thread
288 entry. The stack space must exists during all the main run. */
290 void
291 __gnat_install_SEH_handler (void *ER)
293 int *ptr;
295 /* put current handler in ptr */
297 asm ("mov %%fs:(0),%0" : "=r" (ptr));
299 ((int *)ER)[0] = (int)ptr; /* previous handler */
300 ((int *)ER)[1] = (int)__gnat_SEH_error_handler; /* new handler */
302 /* ER is the new handler, set fs:(0) with this value */
304 asm volatile ("mov %0,%%fs:(0)": : "r" (ER));
306 #endif
308 #else /* defined (_WIN32) */
309 /* For all non Windows targets we provide a dummy SEH install handler. */
310 void __gnat_install_SEH_handler (void *eh ATTRIBUTE_UNUSED)
313 #endif