Skip several gcc.dg/builtin-dynamic-object-size tests on hppa*-*-hpux*
[official-gcc.git] / gcc / ada / seh_init.c
blob42db3a111e843ab32bd60213a96ac2a3086c93de
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-2023, 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 and Cygwin. */
35 #if defined (_WIN32) || (defined (__CYGWIN__) && defined (__SEH__))
36 /* Include system headers, before system.h poisons malloc. */
37 #define WIN32_LEAN_AND_MEAN
38 #include <windows.h>
39 #include <excpt.h>
40 #endif
42 #ifdef IN_RTS
44 #include "runtime.h"
46 /* We don't have libiberty, so use malloc. */
47 #define xmalloc(S) malloc (S)
49 #else
50 #include "config.h"
51 #include "system.h"
52 #endif
54 #include "raise.h"
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
60 /* Addresses of exception data blocks for predefined exceptions. */
61 extern struct Exception_Data constraint_error;
62 extern struct Exception_Data numeric_error;
63 extern struct Exception_Data program_error;
64 extern struct Exception_Data storage_error;
65 extern struct Exception_Data tasking_error;
66 extern struct Exception_Data _abort_signal;
68 #define Raise_From_Signal_Handler __gnat_raise_from_signal_handler
69 extern void Raise_From_Signal_Handler (struct Exception_Data *, const void *)
70 ATTRIBUTE_NORETURN;
73 #if defined (_WIN32) || (defined (__CYGWIN__) && defined (__SEH__))
75 /* Prototypes. */
76 extern void _global_unwind2 (void *);
78 EXCEPTION_DISPOSITION __gnat_SEH_error_handler
79 (struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*) ATTRIBUTE_NORETURN;
81 struct Exception_Data *
82 __gnat_map_SEH (EXCEPTION_RECORD* ExceptionRecord, const char **msg);
84 /* Convert an SEH exception to an Ada one. Return the exception ID and set
85 MSG to the corresponding message. */
87 struct Exception_Data *
88 __gnat_map_SEH (EXCEPTION_RECORD* ExceptionRecord, const char **msg)
90 switch (ExceptionRecord->ExceptionCode)
92 case EXCEPTION_ACCESS_VIOLATION:
93 /* If the failing address isn't maximally aligned or if the page before
94 the faulting page is not accessible, this is a program error. */
95 if ((ExceptionRecord->ExceptionInformation[1] & 3) != 0
96 || IsBadCodePtr
97 ((FARPROC)(ExceptionRecord->ExceptionInformation[1] + 4096)))
99 *msg = "EXCEPTION_ACCESS_VIOLATION";
100 return &program_error;
102 else
104 /* Otherwise this is a stack overflow. */
105 *msg = "stack overflow or erroneous memory access";
106 return &storage_error;
109 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
110 *msg = "EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
111 return &constraint_error;
113 case EXCEPTION_DATATYPE_MISALIGNMENT:
114 *msg = "EXCEPTION_DATATYPE_MISALIGNMENT";
115 return &constraint_error;
117 case EXCEPTION_FLT_DENORMAL_OPERAND:
118 *msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
119 return &constraint_error;
121 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
122 *msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
123 return &constraint_error;
125 case EXCEPTION_FLT_INVALID_OPERATION:
126 *msg = "EXCEPTION_FLT_INVALID_OPERATION";
127 return &constraint_error;
129 case EXCEPTION_FLT_OVERFLOW:
130 *msg = "EXCEPTION_FLT_OVERFLOW";
131 return &constraint_error;
133 case EXCEPTION_FLT_STACK_CHECK:
134 *msg = "EXCEPTION_FLT_STACK_CHECK";
135 return &program_error;
137 case EXCEPTION_FLT_UNDERFLOW:
138 *msg = "EXCEPTION_FLT_UNDERFLOW";
139 return &constraint_error;
141 case EXCEPTION_INT_DIVIDE_BY_ZERO:
142 *msg = "EXCEPTION_INT_DIVIDE_BY_ZERO";
143 return &constraint_error;
145 case EXCEPTION_INT_OVERFLOW:
146 *msg = "EXCEPTION_INT_OVERFLOW";
147 return &constraint_error;
149 case EXCEPTION_INVALID_DISPOSITION:
150 *msg = "EXCEPTION_INVALID_DISPOSITION";
151 return &program_error;
153 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
154 *msg = "EXCEPTION_NONCONTINUABLE_EXCEPTION";
155 return &program_error;
157 case EXCEPTION_PRIV_INSTRUCTION:
158 *msg = "EXCEPTION_PRIV_INSTRUCTION";
159 return &program_error;
161 case EXCEPTION_SINGLE_STEP:
162 *msg = "EXCEPTION_SINGLE_STEP";
163 return &program_error;
165 case EXCEPTION_STACK_OVERFLOW:
166 *msg = "EXCEPTION_STACK_OVERFLOW";
167 return &storage_error;
169 default:
170 *msg = NULL;
171 return NULL;
175 #if !(defined (_WIN64) && defined (__SEH__))
177 /* The "fake" exception handler to be associated with the .text section. */
179 EXCEPTION_DISPOSITION
180 __gnat_SEH_error_handler (struct _EXCEPTION_RECORD* ExceptionRecord,
181 void *EstablisherFrame ATTRIBUTE_UNUSED,
182 struct _CONTEXT* ContextRecord ATTRIBUTE_UNUSED,
183 void *DispatcherContext ATTRIBUTE_UNUSED)
185 struct Exception_Data *exception;
186 const char *msg;
188 exception = __gnat_map_SEH (ExceptionRecord, &msg);
190 if (exception == NULL)
192 exception = &program_error;
193 msg = "unhandled signal";
196 #if !defined (_WIN64)
197 /* This call is important as it avoids locking the second time we catch a
198 signal; it's equivalent to RtlUnwind (EstablisherFrame, NULL, NULL, 0);
199 Note that this routine is documented as internal to Windows and should
200 not be used. */
201 _global_unwind2 (EstablisherFrame);
202 #endif
204 Raise_From_Signal_Handler (exception, msg);
207 #endif /* !(defined (_WIN64) && defined (__SEH__)) */
209 #if defined (_WIN64)
211 /* On x86-64/Windows the EH mechanism is no more based on a chained list of
212 handlers addresses on the stack. Instead unwinding information is used
213 to retrieve the exception handler (similar to DWARF2 unwinding). So in
214 order to register an exception handler, we need to put in the binary
215 some unwinding information. This information can be present statically
216 in the image file inside the .pdata section or registered through the
217 RtlAddFunctionTable API. In the case where the GCC toolchain does not
218 generate the .pdata information for each function, we don't really need
219 to handle SEH exceptions except for signal handling, so we register a
220 "fake" unwinding data that associates a SEH exception handler with the
221 complete .text section. As we never return from the handler, the system
222 does not try to do the final unwinding using the .pdata information and
223 the unwinding is handled by the runtime using the GNAT or GCC mechanism.
225 Solutions based on SetUnhandledExceptionFilter have been discarded as this
226 function is mostly disabled on latest Windows versions.
228 Using AddVectoredExceptionHandler should also be discarded as it overrides
229 all SEH exception handlers that might be present in the program itself and
230 the loaded DLL; for example it results in unexpected behavior in the Win32
231 subsystem. */
233 #ifndef __SEH__
235 /* Do not use this trick when GCC generates the .pdata information, since it
236 is not necessary and will conflict with the per-function data. */
239 " .section .rdata, \"dr\"\n"
240 " .align 4\n"
241 "unwind_info:\n"
242 " .byte 9\n" /* UNW_FLAG_EHANDLER | UNW_VERSION */
243 " .byte 0\n" /* Prologue size. */
244 " .byte 0\n" /* Count of unwind code. */
245 " .byte 0\n" /* Frame register and offset. */
246 " .rva __gnat_SEH_error_handler\n"
247 "\n"
248 " .section .pdata, \"dr\"\n"
249 " .align 4\n"
250 " .long 0\n" /* ImageBase */
251 " .rva etext\n"
252 " .rva unwind_info\n"
253 "\n"
254 " .text\n"
257 #endif /* __SEH__ */
259 /* Nothing to do, the handler is either not used or statically installed by
260 the asm statement just above. */
261 void __gnat_install_SEH_handler (void *eh ATTRIBUTE_UNUSED)
265 #else /* defined (_WIN64) */
267 /* Install the Win32 SEH exception handler. Note that the caller must have
268 allocated 8 bytes on the stack and pass the pointer to this stack space.
269 This is needed as the SEH exception handler must be on the stack of the
270 thread.
272 int buf[2];
274 __gnat_install_SEH_handler ((void*)buf);
276 main();
278 This call must be done before calling the main procedure or the thread
279 entry. The stack space must exist during the entire main run. */
281 void
282 __gnat_install_SEH_handler (void *ER)
284 int *ptr;
286 /* Put current handler in PTR. */
287 asm ("mov %%fs:(0),%0" : "=r" (ptr));
289 ((int *)ER)[0] = (int)ptr; /* previous handler */
290 ((int *)ER)[1] = (int)__gnat_SEH_error_handler; /* new handler */
292 /* ER is the new handler, set fs:(0) to this value. */
293 asm volatile ("mov %0,%%fs:(0)": : "r" (ER));
296 #endif
298 #else /* defined (_WIN32) */
300 /* For all non-Windows targets we provide a dummy SEH install handler. */
301 void __gnat_install_SEH_handler (void *eh ATTRIBUTE_UNUSED)
305 #endif
307 #ifdef __cplusplus
309 #endif