1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-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 initialization circuits that are system dependent.
33 A major part of the functionality involves stack overflow checking.
34 The GCC backend generates probe instructions to test for stack overflow.
35 For details on the exact approach used to generate these probes, see the
36 "Using and Porting GCC" manual, in particular the "Stack Checking" section
37 and the subsection "Specifying How Stack Checking is Done". The handlers
38 installed by this file are used to catch the resulting signals that come
39 from these probes failing (i.e. touching protected pages). */
41 /* This file should be kept synchronized with 2sinit.ads, 2sinit.adb,
42 s-init-ae653-cert.adb and s-init-xi-sparc.adb. All these files implement
43 the required functionality for different targets. */
45 /* The following include is here to meet the published VxWorks requirement
46 that the __vxworks header appear before any other include. */
56 /* We don't have libiberty, so use malloc. */
57 #define xmalloc(S) malloc (S)
66 extern void __gnat_raise_program_error (const char *, int);
68 /* Addresses of exception data blocks for predefined exceptions. Tasking_Error
69 is not used in this unit, and the abort signal is only used on IRIX. */
70 extern struct Exception_Data constraint_error;
71 extern struct Exception_Data numeric_error;
72 extern struct Exception_Data program_error;
73 extern struct Exception_Data storage_error;
75 /* For the Cert run time we use the regular raise exception routine because
76 Raise_From_Signal_Handler is not available. */
78 #define Raise_From_Signal_Handler \
79 __gnat_raise_exception
80 extern void Raise_From_Signal_Handler (struct Exception_Data *, const char *);
82 #define Raise_From_Signal_Handler \
83 ada__exceptions__raise_from_signal_handler
84 extern void Raise_From_Signal_Handler (struct Exception_Data *, const char *);
87 /* Global values computed by the binder. */
88 int __gl_main_priority = -1;
89 int __gl_time_slice_val = -1;
90 char __gl_wc_encoding = 'n';
91 char __gl_locking_policy = ' ';
92 char __gl_queuing_policy = ' ';
93 char __gl_task_dispatching_policy = ' ';
94 char *__gl_priority_specific_dispatching = 0;
95 int __gl_num_specific_dispatching = 0;
96 char *__gl_interrupt_states = 0;
97 int __gl_num_interrupt_states = 0;
98 int __gl_unreserve_all_interrupts = 0;
99 int __gl_exception_tracebacks = 0;
100 int __gl_zero_cost_exceptions = 0;
101 int __gl_detect_blocking = 0;
102 int __gl_default_stack_size = -1;
103 int __gl_leap_seconds_support = 0;
104 int __gl_canonical_streams = 0;
106 /* Indication of whether synchronous signal handler has already been
107 installed by a previous call to adainit. */
108 int __gnat_handler_installed = 0;
111 int __gnat_inside_elab_final_code = 0;
112 /* ??? This variable is obsolete since 2001-08-29 but is kept to allow
113 bootstrap from old GNAT versions (< 3.15). */
116 /* HAVE_GNAT_INIT_FLOAT must be set on every targets where a __gnat_init_float
117 is defined. If this is not set then a void implementation will be defined
118 at the end of this unit. */
119 #undef HAVE_GNAT_INIT_FLOAT
121 /******************************/
122 /* __gnat_get_interrupt_state */
123 /******************************/
125 char __gnat_get_interrupt_state (int);
127 /* This routine is called from the runtime as needed to determine the state
128 of an interrupt, as set by an Interrupt_State pragma appearing anywhere
129 in the current partition. The input argument is the interrupt number,
130 and the result is one of the following:
132 'n' this interrupt not set by any Interrupt_State pragma
133 'u' Interrupt_State pragma set state to User
134 'r' Interrupt_State pragma set state to Runtime
135 's' Interrupt_State pragma set state to System */
138 __gnat_get_interrupt_state (int intrup)
140 if (intrup >= __gl_num_interrupt_states)
143 return __gl_interrupt_states [intrup];
146 /***********************************/
147 /* __gnat_get_specific_dispatching */
148 /***********************************/
150 char __gnat_get_specific_dispatching (int);
152 /* This routine is called from the runtime as needed to determine the
153 priority specific dispatching policy, as set by a
154 Priority_Specific_Dispatching pragma appearing anywhere in the current
155 partition. The input argument is the priority number, and the result
156 is the upper case first character of the policy name, e.g. 'F' for
157 FIFO_Within_Priorities. A space ' ' is returned if no
158 Priority_Specific_Dispatching pragma is used in the partition. */
161 __gnat_get_specific_dispatching (int priority)
163 if (__gl_num_specific_dispatching == 0)
165 else if (priority >= __gl_num_specific_dispatching)
168 return __gl_priority_specific_dispatching [priority];
173 /**********************/
174 /* __gnat_set_globals */
175 /**********************/
177 /* This routine is kept for bootstrapping purposes, since the binder generated
178 file now sets the __gl_* variables directly. */
181 __gnat_set_globals (void)
194 #include <sys/time.h>
196 /* Some versions of AIX don't define SA_NODEFER. */
200 #endif /* SA_NODEFER */
202 /* Versions of AIX before 4.3 don't have nanosleep but provide
205 #ifndef _AIXVERSION_430
207 extern int nanosleep (struct timestruc_t *, struct timestruc_t *);
210 nanosleep (struct timestruc_t *Rqtp, struct timestruc_t *Rmtp)
212 return nsleep (Rqtp, Rmtp);
215 #endif /* _AIXVERSION_430 */
217 static void __gnat_error_handler (int sig, siginfo_t * si, void * uc);
220 __gnat_error_handler (int sig,
221 siginfo_t * si ATTRIBUTE_UNUSED,
222 void * uc ATTRIBUTE_UNUSED)
224 struct Exception_Data *exception;
230 /* FIXME: we need to detect the case of a *real* SIGSEGV. */
231 exception = &storage_error;
232 msg = "stack overflow or erroneous memory access";
236 exception = &constraint_error;
241 exception = &constraint_error;
246 exception = &program_error;
247 msg = "unhandled signal";
250 Raise_From_Signal_Handler (exception, msg);
254 __gnat_install_handler (void)
256 struct sigaction act;
258 /* Set up signal handler to map synchronous signals to appropriate
259 exceptions. Make sure that the handler isn't interrupted by another
260 signal that might cause a scheduling event! */
262 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
263 act.sa_sigaction = __gnat_error_handler;
264 sigemptyset (&act.sa_mask);
266 /* Do not install handlers if interrupt state is "System". */
267 if (__gnat_get_interrupt_state (SIGABRT) != 's')
268 sigaction (SIGABRT, &act, NULL);
269 if (__gnat_get_interrupt_state (SIGFPE) != 's')
270 sigaction (SIGFPE, &act, NULL);
271 if (__gnat_get_interrupt_state (SIGILL) != 's')
272 sigaction (SIGILL, &act, NULL);
273 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
274 sigaction (SIGSEGV, &act, NULL);
275 if (__gnat_get_interrupt_state (SIGBUS) != 's')
276 sigaction (SIGBUS, &act, NULL);
278 __gnat_handler_installed = 1;
285 #elif defined(__alpha__) && defined(__osf__)
288 #include <sys/siginfo.h>
290 static void __gnat_error_handler (int, siginfo_t *, struct sigcontext *);
291 extern char *__gnat_get_code_loc (struct sigcontext *);
292 extern void __gnat_set_code_loc (struct sigcontext *, char *);
293 extern size_t __gnat_machine_state_length (void);
295 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
298 __gnat_adjust_context_for_raise (int signo, void *ucontext)
300 struct sigcontext *sigcontext = (struct sigcontext *) ucontext;
302 /* The unwinder expects the signal context to contain the address of the
303 faulting instruction. For SIGFPE, this depends on the trap shadow
304 situation (see man ieee). We nonetheless always compensate for it,
305 considering that PC designates the instruction following the one that
306 trapped. This is not necessarily true but corresponds to what we have
313 __gnat_error_handler (int sig, siginfo_t *sip, struct sigcontext *context)
315 struct Exception_Data *exception;
316 static int recurse = 0;
319 /* Adjusting is required for every fault context, so adjust for this one
320 now, before we possibly trigger a recursive fault below. */
321 __gnat_adjust_context_for_raise (sig, context);
323 /* If this was an explicit signal from a "kill", just resignal it. */
324 if (SI_FROMUSER (sip))
326 signal (sig, SIG_DFL);
327 kill (getpid(), sig);
330 /* Otherwise, treat it as something we handle. */
334 /* If the problem was permissions, this is a constraint error.
335 Likewise if the failing address isn't maximally aligned or if
338 ??? Using a static variable here isn't task-safe, but it's
339 much too hard to do anything else and we're just determining
340 which exception to raise. */
341 if (sip->si_code == SEGV_ACCERR
342 || (((long) sip->si_addr) & 3) != 0
345 exception = &constraint_error;
350 /* See if the page before the faulting page is accessible. Do that
351 by trying to access it. We'd like to simply try to access
352 4096 + the faulting address, but it's not guaranteed to be
353 the actual address, just to be on the same page. */
356 ((long) sip->si_addr & - getpagesize ()))[getpagesize ()];
357 msg = "stack overflow (or erroneous memory access)";
358 exception = &storage_error;
363 exception = &program_error;
368 exception = &constraint_error;
373 exception = &program_error;
374 msg = "unhandled signal";
378 Raise_From_Signal_Handler (exception, (const char *) msg);
382 __gnat_install_handler (void)
384 struct sigaction act;
386 /* Setup signal handler to map synchronous signals to appropriate
387 exceptions. Make sure that the handler isn't interrupted by another
388 signal that might cause a scheduling event! */
390 act.sa_handler = (void (*) (int)) __gnat_error_handler;
391 act.sa_flags = SA_RESTART | SA_NODEFER | SA_SIGINFO;
392 sigemptyset (&act.sa_mask);
394 /* Do not install handlers if interrupt state is "System". */
395 if (__gnat_get_interrupt_state (SIGABRT) != 's')
396 sigaction (SIGABRT, &act, NULL);
397 if (__gnat_get_interrupt_state (SIGFPE) != 's')
398 sigaction (SIGFPE, &act, NULL);
399 if (__gnat_get_interrupt_state (SIGILL) != 's')
400 sigaction (SIGILL, &act, NULL);
401 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
402 sigaction (SIGSEGV, &act, NULL);
403 if (__gnat_get_interrupt_state (SIGBUS) != 's')
404 sigaction (SIGBUS, &act, NULL);
406 __gnat_handler_installed = 1;
409 /* Routines called by s-mastop-tru64.adb. */
414 __gnat_get_code_loc (struct sigcontext *context)
416 return (char *) context->sc_pc;
420 __gnat_set_code_loc (struct sigcontext *context, char *pc)
422 context->sc_pc = (long) pc;
426 __gnat_machine_state_length (void)
428 return sizeof (struct sigcontext);
435 #elif defined (__hpux__)
438 #include <sys/ucontext.h>
441 __gnat_error_handler (int sig, siginfo_t *siginfo, void *ucontext);
446 siginfo_t *siginfo ATTRIBUTE_UNUSED,
447 void *ucontext ATTRIBUTE_UNUSED)
449 struct Exception_Data *exception;
455 /* FIXME: we need to detect the case of a *real* SIGSEGV. */
456 exception = &storage_error;
457 msg = "stack overflow or erroneous memory access";
461 exception = &constraint_error;
466 exception = &constraint_error;
471 exception = &program_error;
472 msg = "unhandled signal";
475 Raise_From_Signal_Handler (exception, msg);
478 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
479 #if defined (__hppa__)
480 char __gnat_alternate_stack[16 * 1024]; /* 2 * SIGSTKSZ */
482 char __gnat_alternate_stack[128 * 1024]; /* MINSIGSTKSZ */
486 __gnat_install_handler (void)
488 struct sigaction act;
490 /* Set up signal handler to map synchronous signals to appropriate
491 exceptions. Make sure that the handler isn't interrupted by another
492 signal that might cause a scheduling event! Also setup an alternate
493 stack region for the handler execution so that stack overflows can be
494 handled properly, avoiding a SEGV generation from stack usage by the
498 stack.ss_sp = __gnat_alternate_stack;
499 stack.ss_size = sizeof (__gnat_alternate_stack);
501 sigaltstack (&stack, NULL);
503 act.sa_sigaction = __gnat_error_handler;
504 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
505 sigemptyset (&act.sa_mask);
507 /* Do not install handlers if interrupt state is "System". */
508 if (__gnat_get_interrupt_state (SIGABRT) != 's')
509 sigaction (SIGABRT, &act, NULL);
510 if (__gnat_get_interrupt_state (SIGFPE) != 's')
511 sigaction (SIGFPE, &act, NULL);
512 if (__gnat_get_interrupt_state (SIGILL) != 's')
513 sigaction (SIGILL, &act, NULL);
514 if (__gnat_get_interrupt_state (SIGBUS) != 's')
515 sigaction (SIGBUS, &act, NULL);
516 act.sa_flags |= SA_ONSTACK;
517 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
518 sigaction (SIGSEGV, &act, NULL);
520 __gnat_handler_installed = 1;
523 /*********************/
524 /* GNU/Linux Section */
525 /*********************/
527 #elif defined (linux)
531 #define __USE_GNU 1 /* required to get REG_EIP/RIP from glibc's ucontext.h */
532 #include <sys/ucontext.h>
534 /* GNU/Linux, which uses glibc, does not define NULL in included
538 #define NULL ((void *) 0)
543 /* MaRTE OS provides its own version of sigaction, sigfillset, and
544 sigemptyset (overriding these symbol names). We want to make sure that
545 the versions provided by the underlying C library are used here (these
546 versions are renamed by MaRTE to linux_sigaction, fake_linux_sigfillset,
547 and fake_linux_sigemptyset, respectively). The MaRTE library will not
548 always be present (it will not be linked if no tasking constructs are
549 used), so we use the weak symbol mechanism to point always to the symbols
550 defined within the C library. */
552 #pragma weak linux_sigaction
553 int linux_sigaction (int signum, const struct sigaction *act,
554 struct sigaction *oldact) {
555 return sigaction (signum, act, oldact);
557 #define sigaction(signum, act, oldact) linux_sigaction (signum, act, oldact)
559 #pragma weak fake_linux_sigfillset
560 void fake_linux_sigfillset (sigset_t *set) {
563 #define sigfillset(set) fake_linux_sigfillset (set)
565 #pragma weak fake_linux_sigemptyset
566 void fake_linux_sigemptyset (sigset_t *set) {
569 #define sigemptyset(set) fake_linux_sigemptyset (set)
573 static void __gnat_error_handler (int, siginfo_t *siginfo, void *ucontext);
575 #if defined (i386) || defined (__x86_64__) || defined (__ia64__)
577 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
580 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
582 mcontext_t *mcontext = &((ucontext_t *) ucontext)->uc_mcontext;
584 /* On the i386 and x86-64 architectures, we specifically detect calls to
585 the null address and entirely fold the not-yet-fully-established frame
586 to prevent it from stopping the unwinding.
588 On the i386 and x86-64 architectures, stack checking is performed by
589 means of probes with moving stack pointer, that is to say the probed
590 address is always the value of the stack pointer. Upon hitting the
591 guard page, the stack pointer therefore points to an inaccessible
592 address and an alternate signal stack is needed to run the handler.
593 But there is an additional twist: on these architectures, the EH
594 return code writes the address of the handler at the target CFA's
595 value on the stack before doing the jump. As a consequence, if
596 there is an active handler in the frame whose stack has overflowed,
597 the stack pointer must nevertheless point to an accessible address
598 by the time the EH return is executed.
600 We therefore adjust the saved value of the stack pointer by the size
601 of one page + a small dope of 4 words, in order to make sure that it
602 points to an accessible address in case it's used as the target CFA.
603 The stack checking code guarantees that this address is unused by the
604 time this happens. */
607 unsigned long *pc = (unsigned long *)mcontext->gregs[REG_EIP];
608 /* The call insn pushes the return address onto the stack. Pop it. */
611 mcontext->gregs[REG_EIP] = *(unsigned long *)mcontext->gregs[REG_ESP];
612 mcontext->gregs[REG_ESP] += 4;
614 /* The pattern is "orl $0x0,(%esp)" for a probe in 32-bit mode. */
615 else if (signo == SIGSEGV && *pc == 0x00240c83)
616 mcontext->gregs[REG_ESP] += 4096 + 4 * sizeof (unsigned long);
617 #elif defined (__x86_64__)
618 unsigned long *pc = (unsigned long *)mcontext->gregs[REG_RIP];
619 /* The call insn pushes the return address onto the stack. Pop it. */
622 mcontext->gregs[REG_RIP] = *(unsigned long *)mcontext->gregs[REG_RSP];
623 mcontext->gregs[REG_RSP] += 8;
625 /* The pattern is "orq $0x0,(%rsp)" for a probe in 64-bit mode. */
626 else if (signo == SIGSEGV && (*pc & 0xffffffffff) == 0x00240c8348)
627 mcontext->gregs[REG_RSP] += 4096 + 4 * sizeof (unsigned long);
628 #elif defined (__ia64__)
629 /* ??? The IA-64 unwinder doesn't compensate for signals. */
637 __gnat_error_handler (int sig,
638 siginfo_t *siginfo ATTRIBUTE_UNUSED,
641 struct Exception_Data *exception;
642 static int recurse = 0;
645 /* Adjusting is required for every fault context, so adjust for this one
646 now, before we possibly trigger a recursive fault below. */
647 __gnat_adjust_context_for_raise (sig, ucontext);
652 /* If the problem was permissions, this is a constraint error.
653 Likewise if the failing address isn't maximally aligned or if
656 ??? Using a static variable here isn't task-safe, but it's
657 much too hard to do anything else and we're just determining
658 which exception to raise. */
661 exception = &constraint_error;
666 /* Here we would like a discrimination test to see whether the
667 page before the faulting address is accessible. Unfortunately
668 Linux seems to have no way of giving us the faulting address.
670 In versions of a-init.c before 1.95, we had a test of the page
671 before the stack pointer using:
675 ((long) info->esp_at_signal & - getpagesize ()))[getpagesize ()];
677 but that's wrong, since it tests the stack pointer location, and
678 the current stack probe code does not move the stack pointer
679 until all probes succeed.
681 For now we simply do not attempt any discrimination at all. Note
682 that this is quite acceptable, since a "real" SIGSEGV can only
683 occur as the result of an erroneous program. */
685 msg = "stack overflow (or erroneous memory access)";
686 exception = &storage_error;
691 exception = &constraint_error;
696 exception = &constraint_error;
701 exception = &program_error;
702 msg = "unhandled signal";
706 Raise_From_Signal_Handler (exception, msg);
709 #if defined (i386) || defined (__x86_64__)
710 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
711 char __gnat_alternate_stack[16 * 1024]; /* 2 * SIGSTKSZ */
715 #include <sys/mman.h>
716 #include <native/task.h>
722 __gnat_install_handler (void)
724 struct sigaction act;
729 if (__gl_main_priority == -1)
732 prio = __gl_main_priority;
734 /* Avoid memory swapping for this program */
736 mlockall (MCL_CURRENT|MCL_FUTURE);
738 /* Turn the current Linux task into a native Xenomai task */
740 rt_task_shadow(&main_task, "environment_task", prio, T_FPU);
743 /* Set up signal handler to map synchronous signals to appropriate
744 exceptions. Make sure that the handler isn't interrupted by another
745 signal that might cause a scheduling event! Also setup an alternate
746 stack region for the handler execution so that stack overflows can be
747 handled properly, avoiding a SEGV generation from stack usage by the
750 #if defined (i386) || defined (__x86_64__)
752 stack.ss_sp = __gnat_alternate_stack;
753 stack.ss_size = sizeof (__gnat_alternate_stack);
755 sigaltstack (&stack, NULL);
758 act.sa_sigaction = __gnat_error_handler;
759 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
760 sigemptyset (&act.sa_mask);
762 /* Do not install handlers if interrupt state is "System". */
763 if (__gnat_get_interrupt_state (SIGABRT) != 's')
764 sigaction (SIGABRT, &act, NULL);
765 if (__gnat_get_interrupt_state (SIGFPE) != 's')
766 sigaction (SIGFPE, &act, NULL);
767 if (__gnat_get_interrupt_state (SIGILL) != 's')
768 sigaction (SIGILL, &act, NULL);
769 if (__gnat_get_interrupt_state (SIGBUS) != 's')
770 sigaction (SIGBUS, &act, NULL);
771 #if defined (i386) || defined (__x86_64__)
772 act.sa_flags |= SA_ONSTACK;
774 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
775 sigaction (SIGSEGV, &act, NULL);
777 __gnat_handler_installed = 1;
793 #define SIGADAABORT 48
794 #define SIGNAL_STACK_SIZE 4096
795 #define SIGNAL_STACK_ALIGNMENT 64
797 #define Check_Abort_Status \
798 system__soft_links__check_abort_status
799 extern int (*Check_Abort_Status) (void);
801 extern struct Exception_Data _abort_signal;
803 static void __gnat_error_handler (int, int, sigcontext_t *);
805 /* We are not setting the SA_SIGINFO bit in the sigaction flags when
806 connecting that handler, with the effects described in the sigaction
810 If cleared and the signal is caught, the first argument is
811 also the signal number but the second argument is the signal
812 code identifying the cause of the signal. The third argument
813 points to a sigcontext_t structure containing the receiving
814 process's context when the signal was delivered. */
817 __gnat_error_handler (int sig, int code, sigcontext_t *sc ATTRIBUTE_UNUSED)
819 struct Exception_Data *exception;
827 exception = &program_error;
828 msg = "SIGSEGV: (Invalid virtual address)";
830 else if (code == ENXIO)
832 exception = &program_error;
833 msg = "SIGSEGV: (Read beyond mapped object)";
835 else if (code == ENOSPC)
837 exception = &program_error; /* ??? storage_error ??? */
838 msg = "SIGSEGV: (Autogrow for file failed)";
840 else if (code == EACCES || code == EEXIST)
842 /* ??? We handle stack overflows here, some of which do trigger
843 SIGSEGV + EEXIST on Irix 6.5 although EEXIST is not part of
844 the documented valid codes for SEGV in the signal(5) man
847 /* ??? Re-add smarts to further verify that we launched
848 the stack into a guard page, not an attempt to
849 write to .text or something. */
850 exception = &storage_error;
851 msg = "SIGSEGV: (stack overflow or erroneous memory access)";
855 /* Just in case the OS guys did it to us again. Sometimes
856 they fail to document all of the valid codes that are
857 passed to signal handlers, just in case someone depends
858 on knowing all the codes. */
859 exception = &program_error;
860 msg = "SIGSEGV: (Undocumented reason)";
865 /* Map all bus errors to Program_Error. */
866 exception = &program_error;
871 /* Map all fpe errors to Constraint_Error. */
872 exception = &constraint_error;
877 if ((*Check_Abort_Status) ())
879 exception = &_abort_signal;
888 /* Everything else is a Program_Error. */
889 exception = &program_error;
890 msg = "unhandled signal";
893 Raise_From_Signal_Handler (exception, msg);
897 __gnat_install_handler (void)
899 struct sigaction act;
901 /* Setup signal handler to map synchronous signals to appropriate
902 exceptions. Make sure that the handler isn't interrupted by another
903 signal that might cause a scheduling event! */
905 act.sa_handler = __gnat_error_handler;
906 act.sa_flags = SA_NODEFER + SA_RESTART;
907 sigfillset (&act.sa_mask);
908 sigemptyset (&act.sa_mask);
910 /* Do not install handlers if interrupt state is "System". */
911 if (__gnat_get_interrupt_state (SIGABRT) != 's')
912 sigaction (SIGABRT, &act, NULL);
913 if (__gnat_get_interrupt_state (SIGFPE) != 's')
914 sigaction (SIGFPE, &act, NULL);
915 if (__gnat_get_interrupt_state (SIGILL) != 's')
916 sigaction (SIGILL, &act, NULL);
917 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
918 sigaction (SIGSEGV, &act, NULL);
919 if (__gnat_get_interrupt_state (SIGBUS) != 's')
920 sigaction (SIGBUS, &act, NULL);
921 if (__gnat_get_interrupt_state (SIGADAABORT) != 's')
922 sigaction (SIGADAABORT, &act, NULL);
924 __gnat_handler_installed = 1;
927 /*******************/
929 /*******************/
931 #elif defined (__Lynx__)
937 __gnat_error_handler (int sig)
939 struct Exception_Data *exception;
945 exception = &constraint_error;
949 exception = &constraint_error;
953 exception = &storage_error;
954 msg = "stack overflow or erroneous memory access";
957 exception = &constraint_error;
961 exception = &program_error;
962 msg = "unhandled signal";
965 Raise_From_Signal_Handler(exception, msg);
969 __gnat_install_handler(void)
971 struct sigaction act;
973 act.sa_handler = __gnat_error_handler;
975 sigemptyset (&act.sa_mask);
977 /* Do not install handlers if interrupt state is "System". */
978 if (__gnat_get_interrupt_state (SIGFPE) != 's')
979 sigaction (SIGFPE, &act, NULL);
980 if (__gnat_get_interrupt_state (SIGILL) != 's')
981 sigaction (SIGILL, &act, NULL);
982 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
983 sigaction (SIGSEGV, &act, NULL);
984 if (__gnat_get_interrupt_state (SIGBUS) != 's')
985 sigaction (SIGBUS, &act, NULL);
987 __gnat_handler_installed = 1;
990 /*******************/
991 /* Solaris Section */
992 /*******************/
994 #elif defined (sun) && defined (__SVR4) && !defined (__vxworks)
998 #include <sys/ucontext.h>
999 #include <sys/regset.h>
1001 /* The code below is common to SPARC and x86. Beware of the delay slot
1002 differences for signal context adjustments. */
1004 #if defined (__sparc)
1005 #define RETURN_ADDR_OFFSET 8
1007 #define RETURN_ADDR_OFFSET 0
1010 /* Likewise regarding how the "instruction pointer" register slot can
1011 be identified in signal machine contexts. We have either "REG_PC"
1012 or "PC" at hand, depending on the target CPU and Solaris version. */
1013 #if !defined (REG_PC)
1017 static void __gnat_error_handler (int, siginfo_t *, void *);
1019 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1022 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
1024 mcontext_t *mcontext = &((ucontext_t *) ucontext)->uc_mcontext;
1025 unsigned long *pc = (unsigned long *)mcontext->gregs[REG_PC];
1027 /* We specifically detect calls to the null address and entirely fold
1028 the not-yet-fully-established frame to prevent it from stopping the
1031 #if defined (__sparc)
1032 /* The call insn moves the return address into %o7. Move it back. */
1033 mcontext->gregs[REG_PC] = mcontext->gregs[REG_O7];
1034 #elif defined (i386)
1036 /* The call insn pushes the return address onto the stack. Pop it. */
1037 mcontext->gregs[REG_PC] = *(unsigned long *)mcontext->gregs[UESP];
1038 mcontext->gregs[UESP] += 4;
1040 #elif defined (__x86_64__)
1042 /* The call insn pushes the return address onto the stack. Pop it. */
1043 mcontext->gregs[REG_PC] = *(unsigned long *)mcontext->gregs[REG_RSP];
1044 mcontext->gregs[REG_RSP] += 8;
1047 #error architecture not supported on Solaris
1052 __gnat_error_handler (int sig, siginfo_t *sip, void *ucontext)
1054 struct Exception_Data *exception;
1055 static int recurse = 0;
1058 /* Adjusting is required for every fault context, so adjust for this one
1059 now, before we possibly trigger a recursive fault below. */
1060 __gnat_adjust_context_for_raise (sig, ucontext);
1065 /* If the problem was permissions, this is a constraint error.
1066 Likewise if the failing address isn't maximally aligned or if
1069 ??? Using a static variable here isn't task-safe, but it's
1070 much too hard to do anything else and we're just determining
1071 which exception to raise. */
1072 if (sip->si_code == SEGV_ACCERR
1073 || (long) sip->si_addr == 0
1074 || (((long) sip->si_addr) & 3) != 0
1077 exception = &constraint_error;
1082 /* See if the page before the faulting page is accessible. Do that
1083 by trying to access it. We'd like to simply try to access
1084 4096 + the faulting address, but it's not guaranteed to be
1085 the actual address, just to be on the same page. */
1088 ((long) sip->si_addr & - getpagesize ()))[getpagesize ()];
1089 exception = &storage_error;
1090 msg = "stack overflow (or erroneous memory access)";
1095 exception = &program_error;
1100 exception = &constraint_error;
1105 exception = &program_error;
1106 msg = "unhandled signal";
1110 Raise_From_Signal_Handler (exception, msg);
1114 __gnat_install_handler (void)
1116 struct sigaction act;
1118 /* Set up signal handler to map synchronous signals to appropriate
1119 exceptions. Make sure that the handler isn't interrupted by another
1120 signal that might cause a scheduling event! */
1122 act.sa_handler = __gnat_error_handler;
1123 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
1124 sigemptyset (&act.sa_mask);
1126 /* Do not install handlers if interrupt state is "System". */
1127 if (__gnat_get_interrupt_state (SIGABRT) != 's')
1128 sigaction (SIGABRT, &act, NULL);
1129 if (__gnat_get_interrupt_state (SIGFPE) != 's')
1130 sigaction (SIGFPE, &act, NULL);
1131 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
1132 sigaction (SIGSEGV, &act, NULL);
1133 if (__gnat_get_interrupt_state (SIGBUS) != 's')
1134 sigaction (SIGBUS, &act, NULL);
1136 __gnat_handler_installed = 1;
1145 /* Routine called from binder to override default feature values. */
1146 void __gnat_set_features ();
1147 int __gnat_features_set = 0;
1149 long __gnat_error_handler (int *, void *);
1152 #define lib_get_curr_invo_context LIB$I64_GET_CURR_INVO_CONTEXT
1153 #define lib_get_prev_invo_context LIB$I64_GET_PREV_INVO_CONTEXT
1154 #define lib_get_invo_handle LIB$I64_GET_INVO_HANDLE
1156 #define lib_get_curr_invo_context LIB$GET_CURR_INVO_CONTEXT
1157 #define lib_get_prev_invo_context LIB$GET_PREV_INVO_CONTEXT
1158 #define lib_get_invo_handle LIB$GET_INVO_HANDLE
1161 #if defined (IN_RTS) && !defined (__IA64)
1163 /* The prehandler actually gets control first on a condition. It swaps the
1164 stack pointer and calls the handler (__gnat_error_handler). */
1165 extern long __gnat_error_prehandler (void);
1167 extern char *__gnat_error_prehandler_stack; /* Alternate signal stack */
1170 /* Define macro symbols for the VMS conditions that become Ada exceptions.
1171 Most of these are also defined in the header file ssdef.h which has not
1172 yet been converted to be recognized by GNU C. */
1174 /* Defining these as macros, as opposed to external addresses, allows
1175 them to be used in a case statement below. */
1176 #define SS$_ACCVIO 12
1177 #define SS$_HPARITH 1284
1178 #define SS$_STKOVF 1364
1179 #define SS$_RESIGNAL 2328
1181 /* These codes are in standard message libraries. */
1182 extern int C$_SIGKILL;
1183 extern int CMA$_EXIT_THREAD;
1184 extern int SS$_DEBUG;
1185 extern int SS$_INTDIV;
1186 extern int LIB$_KEYNOTFOU;
1187 extern int LIB$_ACTIMAGE;
1188 extern int MTH$_FLOOVEMAT; /* Some ACVC_21 CXA tests */
1190 /* These codes are non standard, which is to say the author is
1191 not sure if they are defined in the standard message libraries
1192 so keep them as macros for now. */
1193 #define RDB$_STREAM_EOF 20480426
1194 #define FDL$_UNPRIKW 11829410
1196 struct cond_except {
1198 const struct Exception_Data *except;
1201 struct descriptor_s {unsigned short len, mbz; __char_ptr32 adr; };
1203 /* Conditions that don't have an Ada exception counterpart must raise
1204 Non_Ada_Error. Since this is defined in s-auxdec, it should only be
1205 referenced by user programs, not the compiler or tools. Hence the
1210 #define Status_Error ada__io_exceptions__status_error
1211 extern struct Exception_Data Status_Error;
1213 #define Mode_Error ada__io_exceptions__mode_error
1214 extern struct Exception_Data Mode_Error;
1216 #define Name_Error ada__io_exceptions__name_error
1217 extern struct Exception_Data Name_Error;
1219 #define Use_Error ada__io_exceptions__use_error
1220 extern struct Exception_Data Use_Error;
1222 #define Device_Error ada__io_exceptions__device_error
1223 extern struct Exception_Data Device_Error;
1225 #define End_Error ada__io_exceptions__end_error
1226 extern struct Exception_Data End_Error;
1228 #define Data_Error ada__io_exceptions__data_error
1229 extern struct Exception_Data Data_Error;
1231 #define Layout_Error ada__io_exceptions__layout_error
1232 extern struct Exception_Data Layout_Error;
1234 #define Non_Ada_Error system__aux_dec__non_ada_error
1235 extern struct Exception_Data Non_Ada_Error;
1237 #define Coded_Exception system__vms_exception_table__coded_exception
1238 extern struct Exception_Data *Coded_Exception (Exception_Code);
1240 #define Base_Code_In system__vms_exception_table__base_code_in
1241 extern Exception_Code Base_Code_In (Exception_Code);
1243 /* DEC Ada exceptions are not defined in a header file, so they
1244 must be declared as external addresses. */
1246 extern int ADA$_PROGRAM_ERROR;
1247 extern int ADA$_LOCK_ERROR;
1248 extern int ADA$_EXISTENCE_ERROR;
1249 extern int ADA$_KEY_ERROR;
1250 extern int ADA$_KEYSIZERR;
1251 extern int ADA$_STAOVF;
1252 extern int ADA$_CONSTRAINT_ERRO;
1253 extern int ADA$_IOSYSFAILED;
1254 extern int ADA$_LAYOUT_ERROR;
1255 extern int ADA$_STORAGE_ERROR;
1256 extern int ADA$_DATA_ERROR;
1257 extern int ADA$_DEVICE_ERROR;
1258 extern int ADA$_END_ERROR;
1259 extern int ADA$_MODE_ERROR;
1260 extern int ADA$_NAME_ERROR;
1261 extern int ADA$_STATUS_ERROR;
1262 extern int ADA$_NOT_OPEN;
1263 extern int ADA$_ALREADY_OPEN;
1264 extern int ADA$_USE_ERROR;
1265 extern int ADA$_UNSUPPORTED;
1266 extern int ADA$_FAC_MODE_MISMAT;
1267 extern int ADA$_ORG_MISMATCH;
1268 extern int ADA$_RFM_MISMATCH;
1269 extern int ADA$_RAT_MISMATCH;
1270 extern int ADA$_MRS_MISMATCH;
1271 extern int ADA$_MRN_MISMATCH;
1272 extern int ADA$_KEY_MISMATCH;
1273 extern int ADA$_MAXLINEXC;
1274 extern int ADA$_LINEXCMRS;
1276 /* DEC Ada specific conditions. */
1277 static const struct cond_except dec_ada_cond_except_table [] = {
1278 {&ADA$_PROGRAM_ERROR, &program_error},
1279 {&ADA$_USE_ERROR, &Use_Error},
1280 {&ADA$_KEYSIZERR, &program_error},
1281 {&ADA$_STAOVF, &storage_error},
1282 {&ADA$_CONSTRAINT_ERRO, &constraint_error},
1283 {&ADA$_IOSYSFAILED, &Device_Error},
1284 {&ADA$_LAYOUT_ERROR, &Layout_Error},
1285 {&ADA$_STORAGE_ERROR, &storage_error},
1286 {&ADA$_DATA_ERROR, &Data_Error},
1287 {&ADA$_DEVICE_ERROR, &Device_Error},
1288 {&ADA$_END_ERROR, &End_Error},
1289 {&ADA$_MODE_ERROR, &Mode_Error},
1290 {&ADA$_NAME_ERROR, &Name_Error},
1291 {&ADA$_STATUS_ERROR, &Status_Error},
1292 {&ADA$_NOT_OPEN, &Use_Error},
1293 {&ADA$_ALREADY_OPEN, &Use_Error},
1294 {&ADA$_USE_ERROR, &Use_Error},
1295 {&ADA$_UNSUPPORTED, &Use_Error},
1296 {&ADA$_FAC_MODE_MISMAT, &Use_Error},
1297 {&ADA$_ORG_MISMATCH, &Use_Error},
1298 {&ADA$_RFM_MISMATCH, &Use_Error},
1299 {&ADA$_RAT_MISMATCH, &Use_Error},
1300 {&ADA$_MRS_MISMATCH, &Use_Error},
1301 {&ADA$_MRN_MISMATCH, &Use_Error},
1302 {&ADA$_KEY_MISMATCH, &Use_Error},
1303 {&ADA$_MAXLINEXC, &constraint_error},
1304 {&ADA$_LINEXCMRS, &constraint_error},
1309 /* Already handled by a pragma Import_Exception
1310 in Aux_IO_Exceptions */
1311 {&ADA$_LOCK_ERROR, &Lock_Error},
1312 {&ADA$_EXISTENCE_ERROR, &Existence_Error},
1313 {&ADA$_KEY_ERROR, &Key_Error},
1318 /* Non-DEC Ada specific conditions. We could probably also put
1319 SS$_HPARITH here and possibly SS$_ACCVIO, SS$_STKOVF. */
1320 static const struct cond_except cond_except_table [] = {
1321 {&MTH$_FLOOVEMAT, &constraint_error},
1322 {&SS$_INTDIV, &constraint_error},
1326 /* To deal with VMS conditions and their mapping to Ada exceptions,
1327 the __gnat_error_handler routine below is installed as an exception
1328 vector having precedence over DEC frame handlers. Some conditions
1329 still need to be handled by such handlers, however, in which case
1330 __gnat_error_handler needs to return SS$_RESIGNAL. Consider for
1331 instance the use of a third party library compiled with DECAda and
1332 performing its own exception handling internally.
1334 To allow some user-level flexibility, which conditions should be
1335 resignaled is controlled by a predicate function, provided with the
1336 condition value and returning a boolean indication stating whether
1337 this condition should be resignaled or not.
1339 That predicate function is called indirectly, via a function pointer,
1340 by __gnat_error_handler, and changing that pointer is allowed to the
1341 the user code by way of the __gnat_set_resignal_predicate interface.
1343 The user level function may then implement what it likes, including
1344 for instance the maintenance of a dynamic data structure if the set
1345 of to be resignalled conditions has to change over the program's
1348 ??? This is not a perfect solution to deal with the possible
1349 interactions between the GNAT and the DECAda exception handling
1350 models and better (more general) schemes are studied. This is so
1351 just provided as a convenient workaround in the meantime, and
1352 should be use with caution since the implementation has been kept
1356 resignal_predicate (int code);
1358 const int *cond_resignal_table [] = {
1364 (int *) RDB$_STREAM_EOF,
1365 (int *) FDL$_UNPRIKW,
1369 const int facility_resignal_table [] = {
1370 0x1380000, /* RDB */
1371 0x2220000, /* SQL */
1375 /* Default GNAT predicate for resignaling conditions. */
1378 __gnat_default_resignal_p (int code)
1382 for (i = 0; facility_resignal_table [i]; i++)
1383 if ((code & 0xfff0000) == facility_resignal_table [i])
1386 for (i = 0, iexcept = 0;
1387 cond_resignal_table [i] &&
1388 !(iexcept = LIB$MATCH_COND (&code, &cond_resignal_table [i]));
1394 /* Static pointer to predicate that the __gnat_error_handler exception
1395 vector invokes to determine if it should resignal a condition. */
1397 static resignal_predicate * __gnat_resignal_p = __gnat_default_resignal_p;
1399 /* User interface to change the predicate pointer to PREDICATE. Reset to
1400 the default if PREDICATE is null. */
1403 __gnat_set_resignal_predicate (resignal_predicate * predicate)
1406 __gnat_resignal_p = __gnat_default_resignal_p;
1408 __gnat_resignal_p = predicate;
1411 /* Should match System.Parameters.Default_Exception_Msg_Max_Length. */
1412 #define Default_Exception_Msg_Max_Length 512
1414 /* Action routine for SYS$PUTMSG. There may be multiple
1415 conditions, each with text to be appended to MESSAGE
1416 and separated by line termination. */
1419 copy_msg (msgdesc, message)
1420 struct descriptor_s *msgdesc;
1423 int len = strlen (message);
1426 /* Check for buffer overflow and skip. */
1427 if (len > 0 && len <= Default_Exception_Msg_Max_Length - 3)
1429 strcat (message, "\r\n");
1433 /* Check for buffer overflow and truncate if necessary. */
1434 copy_len = (len + msgdesc->len <= Default_Exception_Msg_Max_Length - 1 ?
1436 Default_Exception_Msg_Max_Length - 1 - len);
1437 strncpy (&message [len], msgdesc->adr, copy_len);
1438 message [len + copy_len] = 0;
1444 __gnat_handle_vms_condition (int *sigargs, void *mechargs)
1446 struct Exception_Data *exception = 0;
1447 Exception_Code base_code;
1448 struct descriptor_s gnat_facility = {4,0,"GNAT"};
1449 char message [Default_Exception_Msg_Max_Length];
1451 const char *msg = "";
1453 /* Check for conditions to resignal which aren't effected by pragma
1454 Import_Exception. */
1455 if (__gnat_resignal_p (sigargs [1]))
1456 return SS$_RESIGNAL;
1459 /* See if it's an imported exception. Beware that registered exceptions
1460 are bound to their base code, with the severity bits masked off. */
1461 base_code = Base_Code_In ((Exception_Code) sigargs [1]);
1462 exception = Coded_Exception (base_code);
1468 /* Subtract PC & PSL fields which messes with PUTMSG. */
1470 SYS$PUTMSG (sigargs, copy_msg, &gnat_facility, message);
1474 exception->Name_Length = 19;
1475 /* ??? The full name really should be get sys$getmsg returns. */
1476 exception->Full_Name = "IMPORTED_EXCEPTION";
1477 exception->Import_Code = base_code;
1480 /* Do not adjust the program counter as already points to the next
1481 instruction (just after the call to LIB$STOP). */
1482 Raise_From_Signal_Handler (exception, msg);
1491 if (sigargs[3] == 0)
1493 exception = &constraint_error;
1494 msg = "access zero";
1498 exception = &storage_error;
1499 msg = "stack overflow (or erroneous memory access)";
1501 __gnat_adjust_context_for_raise (0, (void *)mechargs);
1505 exception = &storage_error;
1506 msg = "stack overflow";
1507 __gnat_adjust_context_for_raise (0, (void *)mechargs);
1512 return SS$_RESIGNAL; /* toplev.c handles for compiler */
1514 exception = &constraint_error;
1515 msg = "arithmetic error";
1517 /* No need to adjust pc on Alpha: the pc is already on the instruction
1518 after the trapping one. */
1519 __gnat_adjust_context_for_raise (0, (void *)mechargs);
1529 /* Scan the DEC Ada exception condition table for a match and fetch
1530 the associated GNAT exception pointer. */
1532 dec_ada_cond_except_table [i].cond &&
1533 !LIB$MATCH_COND (&sigargs [1],
1534 &dec_ada_cond_except_table [i].cond);
1536 exception = (struct Exception_Data *)
1537 dec_ada_cond_except_table [i].except;
1541 /* Scan the VMS standard condition table for a match and fetch
1542 the associated GNAT exception pointer. */
1544 cond_except_table [i].cond &&
1545 !LIB$MATCH_COND (&sigargs [1], &cond_except_table [i].cond);
1547 exception = (struct Exception_Data *)
1548 cond_except_table [i].except;
1551 /* User programs expect Non_Ada_Error to be raised, reference
1552 DEC Ada test CXCONDHAN. */
1553 exception = &Non_Ada_Error;
1557 exception = &program_error;
1560 /* Subtract PC & PSL fields which messes with PUTMSG. */
1562 SYS$PUTMSG (sigargs, copy_msg, &gnat_facility, message);
1568 Raise_From_Signal_Handler (exception, msg);
1572 __gnat_error_handler (int *sigargs, void *mechargs)
1574 return __gnat_handle_vms_condition (sigargs, mechargs);
1578 __gnat_install_handler (void)
1580 long prvhnd ATTRIBUTE_UNUSED;
1582 #if !defined (IN_RTS)
1583 SYS$SETEXV (1, __gnat_error_handler, 3, &prvhnd);
1586 /* On alpha-vms, we avoid the global vector annoyance thanks to frame based
1587 handlers to turn conditions into exceptions since GCC 3.4. The global
1588 vector is still required for earlier GCC versions. We're resorting to
1589 the __gnat_error_prehandler assembly function in this case. */
1591 #if defined (IN_RTS) && defined (__alpha__)
1592 if ((__GNUC__ * 10 + __GNUC_MINOR__) < 34)
1594 char * c = (char *) xmalloc (2049);
1596 __gnat_error_prehandler_stack = &c[2048];
1597 SYS$SETEXV (1, __gnat_error_prehandler, 3, &prvhnd);
1601 __gnat_handler_installed = 1;
1604 /* __gnat_adjust_context_for_raise for Alpha - see comments along with the
1605 default version later in this file. */
1607 #if defined (IN_RTS) && defined (__alpha__)
1609 #include <vms/chfctxdef.h>
1610 #include <vms/chfdef.h>
1612 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1615 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
1617 /* Add one to the address of the instruction signaling the condition,
1618 located in the sigargs array. */
1620 CHF$MECH_ARRAY * mechargs = (CHF$MECH_ARRAY *) ucontext;
1621 CHF$SIGNAL_ARRAY * sigargs
1622 = (CHF$SIGNAL_ARRAY *) mechargs->chf$q_mch_sig_addr;
1624 int vcount = sigargs->chf$is_sig_args;
1625 int * pc_slot = & (&sigargs->chf$l_sig_name)[vcount-2];
1632 /* __gnat_adjust_context_for_raise for ia64. */
1634 #if defined (IN_RTS) && defined (__IA64)
1636 #include <vms/chfctxdef.h>
1637 #include <vms/chfdef.h>
1639 #define HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
1641 typedef unsigned long long u64;
1644 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED, void *ucontext)
1646 /* Add one to the address of the instruction signaling the condition,
1647 located in the 64bits sigargs array. */
1649 CHF$MECH_ARRAY * mechargs = (CHF$MECH_ARRAY *) ucontext;
1651 CHF64$SIGNAL_ARRAY *chfsig64
1652 = (CHF64$SIGNAL_ARRAY *) mechargs->chf$ph_mch_sig64_addr;
1655 = (u64 *)chfsig64 + 1 + chfsig64->chf64$l_sig_args;
1657 u64 * ih_pc_loc = post_sigarray - 2;
1664 /* Feature logical name and global variable address pair */
1665 struct feature {char *name; int* gl_addr;};
1667 /* Default values for GNAT features set by environment. */
1668 int __gl_no_malloc_64 = 0;
1670 /* Array feature logical names and global variable addresses */
1671 static struct feature features[] = {
1672 {"GNAT$NO_MALLOC_64", &__gl_no_malloc_64},
1676 void __gnat_set_features ()
1678 struct descriptor_s name_desc, result_desc;
1680 unsigned short rlen;
1683 char buff [MAXEQUIV];
1685 /* Loop through features array and test name for enable/disable */
1686 for (i=0; features [i].name; i++)
1688 name_desc.len = strlen (features [i].name);
1690 name_desc.adr = features [i].name;
1692 result_desc.len = MAXEQUIV - 1;
1693 result_desc.mbz = 0;
1694 result_desc.adr = buff;
1696 status = LIB$GET_LOGICAL (&name_desc, &result_desc, &rlen);
1698 if (((status & 1) == 1) && (rlen < MAXEQUIV))
1703 if (strcmp (buff, "ENABLE") == 0)
1704 *features [i].gl_addr = 1;
1705 else if (strcmp (buff, "DISABLE") == 0)
1706 *features [i].gl_addr = 0;
1709 __gnat_features_set = 1;
1712 /*******************/
1713 /* FreeBSD Section */
1714 /*******************/
1716 #elif defined (__FreeBSD__)
1719 #include <sys/ucontext.h>
1722 static void __gnat_error_handler (int, siginfo_t *, ucontext_t *);
1725 __gnat_error_handler (int sig, siginfo_t *info __attribute__ ((unused)),
1726 ucontext_t *ucontext)
1728 struct Exception_Data *exception;
1734 exception = &constraint_error;
1739 exception = &constraint_error;
1744 exception = &storage_error;
1745 msg = "stack overflow or erroneous memory access";
1749 exception = &constraint_error;
1754 exception = &program_error;
1755 msg = "unhandled signal";
1758 Raise_From_Signal_Handler (exception, msg);
1762 __gnat_install_handler ()
1764 struct sigaction act;
1766 /* Set up signal handler to map synchronous signals to appropriate
1767 exceptions. Make sure that the handler isn't interrupted by another
1768 signal that might cause a scheduling event! */
1771 = (void (*)(int, struct __siginfo *, void*)) __gnat_error_handler;
1772 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
1773 (void) sigemptyset (&act.sa_mask);
1775 (void) sigaction (SIGILL, &act, NULL);
1776 (void) sigaction (SIGFPE, &act, NULL);
1777 (void) sigaction (SIGSEGV, &act, NULL);
1778 (void) sigaction (SIGBUS, &act, NULL);
1780 __gnat_handler_installed = 1;
1783 /*******************/
1784 /* VxWorks Section */
1785 /*******************/
1787 #elif defined(__vxworks)
1790 #include <taskLib.h>
1798 #include "private/vThreadsP.h"
1801 void __gnat_error_handler (int, void *, struct sigcontext *);
1805 /* Directly vectored Interrupt routines are not supported when using RTPs. */
1807 extern int __gnat_inum_to_ivec (int);
1809 /* This is needed by the GNAT run time to handle Vxworks interrupts. */
1811 __gnat_inum_to_ivec (int num)
1813 return INUM_TO_IVEC (num);
1817 #if !defined(__alpha_vxworks) && (_WRS_VXWORKS_MAJOR != 6) && !defined(__RTP__)
1819 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1820 on Alpha VxWorks and VxWorks 6.x (including RTPs). */
1822 extern long getpid (void);
1827 return taskIdSelf ();
1831 /* VxWorks 653 vThreads expects the field excCnt to be zeroed when a signal is.
1832 handled. The VxWorks version of longjmp does this; GCC's builtin_longjmp
1835 __gnat_clear_exception_count (void)
1838 WIND_TCB *currentTask = (WIND_TCB *) taskIdSelf();
1840 currentTask->vThreads.excCnt = 0;
1844 /* Handle different SIGnal to exception mappings in different VxWorks
1847 __gnat_map_signal (int sig)
1849 struct Exception_Data *exception;
1855 exception = &constraint_error;
1859 #ifdef __VXWORKSMILS__
1861 exception = &storage_error;
1862 msg = "SIGILL: possible stack overflow";
1865 exception = &storage_error;
1869 exception = &program_error;
1874 exception = &constraint_error;
1875 msg = "Floating point exception or SIGILL";
1878 exception = &storage_error;
1882 exception = &storage_error;
1883 msg = "SIGBUS: possible stack overflow";
1886 #elif (_WRS_VXWORKS_MAJOR == 6)
1888 exception = &constraint_error;
1892 /* In RTP mode a SIGSEGV is most likely due to a stack overflow,
1893 since stack checking uses the probing mechanism. */
1895 exception = &storage_error;
1896 msg = "SIGSEGV: possible stack overflow";
1899 exception = &program_error;
1903 /* VxWorks 6 kernel mode with probing. SIGBUS for guard page hit */
1905 exception = &storage_error;
1909 exception = &storage_error;
1910 msg = "SIGBUS: possible stack overflow";
1914 /* VxWorks 5: a SIGILL is most likely due to a stack overflow,
1915 since stack checking uses the stack limit mechanism. */
1917 exception = &storage_error;
1918 msg = "SIGILL: possible stack overflow";
1921 exception = &storage_error;
1925 exception = &program_error;
1930 exception = &program_error;
1931 msg = "unhandled signal";
1934 __gnat_clear_exception_count ();
1935 Raise_From_Signal_Handler (exception, msg);
1938 /* Tasking and Non-tasking signal handler. Map SIGnal to Ada exception
1939 propagation after the required low level adjustments. */
1942 __gnat_error_handler (int sig, void * si ATTRIBUTE_UNUSED,
1943 struct sigcontext * sc)
1947 /* VxWorks will always mask out the signal during the signal handler and
1948 will reenable it on a longjmp. GNAT does not generate a longjmp to
1949 return from a signal handler so the signal will still be masked unless
1951 sigprocmask (SIG_SETMASK, NULL, &mask);
1952 sigdelset (&mask, sig);
1953 sigprocmask (SIG_SETMASK, &mask, NULL);
1955 __gnat_map_signal (sig);
1959 __gnat_install_handler (void)
1961 struct sigaction act;
1963 /* Setup signal handler to map synchronous signals to appropriate
1964 exceptions. Make sure that the handler isn't interrupted by another
1965 signal that might cause a scheduling event! */
1967 act.sa_handler = __gnat_error_handler;
1968 act.sa_flags = SA_SIGINFO | SA_ONSTACK;
1969 sigemptyset (&act.sa_mask);
1971 /* For VxWorks, install all signal handlers, since pragma Interrupt_State
1972 applies to vectored hardware interrupts, not signals. */
1973 sigaction (SIGFPE, &act, NULL);
1974 sigaction (SIGILL, &act, NULL);
1975 sigaction (SIGSEGV, &act, NULL);
1976 sigaction (SIGBUS, &act, NULL);
1978 __gnat_handler_installed = 1;
1981 #define HAVE_GNAT_INIT_FLOAT
1984 __gnat_init_float (void)
1986 /* Disable overflow/underflow exceptions on the PPC processor, needed
1987 to get correct Ada semantics. Note that for AE653 vThreads, the HW
1988 overflow settings are an OS configuration issue. The instructions
1989 below have no effect. */
1990 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT) && !defined (VTHREADS)
1991 #if defined (__SPE__)
1993 const unsigned long spefscr_mask = 0xfffffff3;
1994 unsigned long spefscr;
1995 asm ("mfspr %0, 512" : "=r" (spefscr));
1996 spefscr = spefscr & spefscr_mask;
1997 asm ("mtspr 512, %0\n\tisync" : : "r" (spefscr));
2005 #if (defined (__i386__) || defined (i386)) && !defined (VTHREADS)
2006 /* This is used to properly initialize the FPU on an x86 for each
2011 /* Similarly for SPARC64. Achieved by masking bits in the Trap Enable Mask
2012 field of the Floating-point Status Register (see the SPARC Architecture
2013 Manual Version 9, p 48). */
2014 #if defined (sparc64)
2016 #define FSR_TEM_NVM (1 << 27) /* Invalid operand */
2017 #define FSR_TEM_OFM (1 << 26) /* Overflow */
2018 #define FSR_TEM_UFM (1 << 25) /* Underflow */
2019 #define FSR_TEM_DZM (1 << 24) /* Division by Zero */
2020 #define FSR_TEM_NXM (1 << 23) /* Inexact result */
2024 __asm__("st %%fsr, %0" : "=m" (fsr));
2025 fsr &= ~(FSR_TEM_OFM | FSR_TEM_UFM);
2026 __asm__("ld %0, %%fsr" : : "m" (fsr));
2031 /* This subprogram is called by System.Task_Primitives.Operations.Enter_Task
2032 (if not null) when a new task is created. It is initialized by
2033 System.Stack_Checking.Operations.Initialize_Stack_Limit.
2034 The use of a hook avoids to drag stack checking subprograms if stack
2035 checking is not used. */
2036 void (*__gnat_set_stack_limit_hook)(void) = (void (*)(void))0;
2038 /******************/
2039 /* NetBSD Section */
2040 /******************/
2042 #elif defined(__NetBSD__)
2048 __gnat_error_handler (int sig)
2050 struct Exception_Data *exception;
2056 exception = &constraint_error;
2060 exception = &constraint_error;
2064 exception = &storage_error;
2065 msg = "stack overflow or erroneous memory access";
2068 exception = &constraint_error;
2072 exception = &program_error;
2073 msg = "unhandled signal";
2076 Raise_From_Signal_Handler(exception, msg);
2080 __gnat_install_handler(void)
2082 struct sigaction act;
2084 act.sa_handler = __gnat_error_handler;
2085 act.sa_flags = SA_NODEFER | SA_RESTART;
2086 sigemptyset (&act.sa_mask);
2088 /* Do not install handlers if interrupt state is "System". */
2089 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2090 sigaction (SIGFPE, &act, NULL);
2091 if (__gnat_get_interrupt_state (SIGILL) != 's')
2092 sigaction (SIGILL, &act, NULL);
2093 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2094 sigaction (SIGSEGV, &act, NULL);
2095 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2096 sigaction (SIGBUS, &act, NULL);
2098 __gnat_handler_installed = 1;
2101 /*******************/
2102 /* OpenBSD Section */
2103 /*******************/
2105 #elif defined(__OpenBSD__)
2111 __gnat_error_handler (int sig)
2113 struct Exception_Data *exception;
2119 exception = &constraint_error;
2123 exception = &constraint_error;
2127 exception = &storage_error;
2128 msg = "stack overflow or erroneous memory access";
2131 exception = &constraint_error;
2135 exception = &program_error;
2136 msg = "unhandled signal";
2139 Raise_From_Signal_Handler(exception, msg);
2143 __gnat_install_handler(void)
2145 struct sigaction act;
2147 act.sa_handler = __gnat_error_handler;
2148 act.sa_flags = SA_NODEFER | SA_RESTART;
2149 sigemptyset (&act.sa_mask);
2151 /* Do not install handlers if interrupt state is "System" */
2152 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2153 sigaction (SIGFPE, &act, NULL);
2154 if (__gnat_get_interrupt_state (SIGILL) != 's')
2155 sigaction (SIGILL, &act, NULL);
2156 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2157 sigaction (SIGSEGV, &act, NULL);
2158 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2159 sigaction (SIGBUS, &act, NULL);
2161 __gnat_handler_installed = 1;
2164 /******************/
2165 /* Darwin Section */
2166 /******************/
2168 #elif defined(__APPLE__)
2171 #include <sys/syscall.h>
2172 #include <mach/mach_vm.h>
2173 #include <mach/mach_init.h>
2174 #include <mach/vm_statistics.h>
2176 /* This must be in keeping with System.OS_Interface.Alternate_Stack_Size. */
2177 char __gnat_alternate_stack[32 * 1024]; /* 1 * MINSIGSTKSZ */
2179 static void __gnat_error_handler (int sig, siginfo_t * si, void * uc);
2181 /* Defined in xnu unix_signal.c.
2182 Tell the kernel to re-use alt stack when delivering a signal. */
2183 #define UC_RESET_ALT_STACK 0x80000000
2185 /* Return true if ADDR is within a stack guard area. */
2187 __gnat_is_stack_guard (mach_vm_address_t addr)
2190 vm_region_submap_info_data_64_t info;
2191 mach_vm_address_t start;
2192 mach_vm_size_t size;
2194 mach_msg_type_number_t count;
2196 count = VM_REGION_SUBMAP_INFO_COUNT_64;
2200 kret = mach_vm_region_recurse (mach_task_self (), &start, &size, &depth,
2201 (vm_region_recurse_info_t) &info, &count);
2202 if (kret == KERN_SUCCESS
2203 && addr >= start && addr < (start + size)
2204 && info.protection == VM_PROT_NONE
2205 && info.user_tag == VM_MEMORY_STACK)
2211 __gnat_error_handler (int sig, siginfo_t * si, void * uc ATTRIBUTE_UNUSED)
2213 struct Exception_Data *exception;
2220 if (__gnat_is_stack_guard ((unsigned long)si->si_addr))
2222 exception = &storage_error;
2223 msg = "stack overflow";
2227 exception = &constraint_error;
2228 msg = "erroneous memory access";
2230 /* Reset the use of alt stack, so that the alt stack will be used
2231 for the next signal delivery.
2232 The stack can't be used in case of stack checking. */
2233 syscall (SYS_sigreturn, NULL, UC_RESET_ALT_STACK);
2237 exception = &constraint_error;
2242 exception = &program_error;
2243 msg = "unhandled signal";
2246 Raise_From_Signal_Handler (exception, msg);
2250 __gnat_install_handler (void)
2252 struct sigaction act;
2254 /* Set up signal handler to map synchronous signals to appropriate
2255 exceptions. Make sure that the handler isn't interrupted by another
2256 signal that might cause a scheduling event! Also setup an alternate
2257 stack region for the handler execution so that stack overflows can be
2258 handled properly, avoiding a SEGV generation from stack usage by the
2259 handler itself (and it is required by Darwin). */
2262 stack.ss_sp = __gnat_alternate_stack;
2263 stack.ss_size = sizeof (__gnat_alternate_stack);
2265 sigaltstack (&stack, NULL);
2267 act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
2268 act.sa_sigaction = __gnat_error_handler;
2269 sigemptyset (&act.sa_mask);
2271 /* Do not install handlers if interrupt state is "System". */
2272 if (__gnat_get_interrupt_state (SIGABRT) != 's')
2273 sigaction (SIGABRT, &act, NULL);
2274 if (__gnat_get_interrupt_state (SIGFPE) != 's')
2275 sigaction (SIGFPE, &act, NULL);
2276 if (__gnat_get_interrupt_state (SIGILL) != 's')
2277 sigaction (SIGILL, &act, NULL);
2279 act.sa_flags |= SA_ONSTACK;
2280 if (__gnat_get_interrupt_state (SIGSEGV) != 's')
2281 sigaction (SIGSEGV, &act, NULL);
2282 if (__gnat_get_interrupt_state (SIGBUS) != 's')
2283 sigaction (SIGBUS, &act, NULL);
2285 __gnat_handler_installed = 1;
2290 /* For all other versions of GNAT, the handler does nothing. */
2292 /*******************/
2293 /* Default Section */
2294 /*******************/
2297 __gnat_install_handler (void)
2299 __gnat_handler_installed = 1;
2304 /*********************/
2305 /* __gnat_init_float */
2306 /*********************/
2308 /* This routine is called as each process thread is created, for possible
2309 initialization of the FP processor. This version is used under INTERIX,
2310 WIN32 and could be used under OS/2. */
2312 #if defined (_WIN32) || defined (__INTERIX) || defined (__EMX__) \
2313 || defined (__Lynx__) || defined(__NetBSD__) || defined(__FreeBSD__) \
2314 || defined (__OpenBSD__)
2316 #define HAVE_GNAT_INIT_FLOAT
2319 __gnat_init_float (void)
2321 #if defined (__i386__) || defined (i386) || defined (__x86_64)
2323 /* This is used to properly initialize the FPU on an x86 for each
2328 #endif /* Defined __i386__ */
2332 #ifndef HAVE_GNAT_INIT_FLOAT
2334 /* All targets without a specific __gnat_init_float will use an empty one. */
2336 __gnat_init_float (void)
2341 /***********************************/
2342 /* __gnat_adjust_context_for_raise */
2343 /***********************************/
2345 #ifndef HAVE_GNAT_ADJUST_CONTEXT_FOR_RAISE
2347 /* All targets without a specific version will use an empty one. */
2349 /* Given UCONTEXT a pointer to a context structure received by a signal
2350 handler for SIGNO, perform the necessary adjustments to let the handler
2351 raise an exception. Calls to this routine are not conditioned by the
2352 propagation scheme in use. */
2355 __gnat_adjust_context_for_raise (int signo ATTRIBUTE_UNUSED,
2356 void *ucontext ATTRIBUTE_UNUSED)
2358 /* We used to compensate here for the raised from call vs raised from signal
2359 exception discrepancy with the GCC ZCX scheme, but this now can be dealt
2360 with generically in the unwinder (see GCC PR other/26208). This however
2361 requires the use of the _Unwind_GetIPInfo routine in raise-gcc.c, which
2362 is predicated on the definition of HAVE_GETIPINFO at compile time. Only
2363 the VMS ports still do the compensation described in the few lines below.
2365 *** Call vs signal exception discrepancy with GCC ZCX scheme ***
2367 The GCC unwinder expects to be dealing with call return addresses, since
2368 this is the "nominal" case of what we retrieve while unwinding a regular
2371 To evaluate if a handler applies at some point identified by a return
2372 address, the propagation engine needs to determine what region the
2373 corresponding call instruction pertains to. Because the return address
2374 may not be attached to the same region as the call, the unwinder always
2375 subtracts "some" amount from a return address to search the region
2376 tables, amount chosen to ensure that the resulting address is inside the
2379 When we raise an exception from a signal handler, e.g. to transform a
2380 SIGSEGV into Storage_Error, things need to appear as if the signal
2381 handler had been "called" by the instruction which triggered the signal,
2382 so that exception handlers that apply there are considered. What the
2383 unwinder will retrieve as the return address from the signal handler is
2384 what it will find as the faulting instruction address in the signal
2385 context pushed by the kernel. Leaving this address untouched looses, if
2386 the triggering instruction happens to be the very first of a region, as
2387 the later adjustments performed by the unwinder would yield an address
2388 outside that region. We need to compensate for the unwinder adjustments
2389 at some point, and this is what this routine is expected to do.
2391 signo is passed because on some targets for some signals the PC in
2392 context points to the instruction after the faulting one, in which case
2393 the unwinder adjustment is still desired. */