1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
9 * C Implementation File *
11 * Copyright (C) 1992-2001 Free Software Foundation, Inc. *
13 * GNAT is free software; you can redistribute it and/or modify it under *
14 * terms of the GNU General Public License as published by the Free Soft- *
15 * ware Foundation; either version 2, or (at your option) any later ver- *
16 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
17 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
19 * for more details. You should have received a copy of the GNU General *
20 * Public License distributed with GNAT; see file COPYING. If not, write *
21 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
22 * MA 02111-1307, USA. *
24 * As a special exception, if you link this file with other files to *
25 * produce an executable, this file does not by itself cause the resulting *
26 * executable to be covered by the GNU General Public License. This except- *
27 * ion does not however invalidate any other reasons why the executable *
28 * file might be covered by the GNU Public License. *
30 * GNAT was originally developed by the GNAT team at New York University. *
31 * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
33 ****************************************************************************/
35 /* This unit contains initialization circuits that are system dependent. A
36 major part of the functionality involved involves stack overflow checking.
37 The GCC backend generates probe instructions to test for stack overflow.
38 For details on the exact approach used to generate these probes, see the
39 "Using and Porting GCC" manual, in particular the "Stack Checking" section
40 and the subsection "Specifying How Stack Checking is Done". The handlers
41 installed by this file are used to handle resulting signals that come
42 from these probes failing (i.e. touching protected pages) */
44 /* The following include is here to meet the published VxWorks requirement
45 that the __vxworks header appear before any other include. */
62 extern void __gnat_raise_program_error (const char *, int);
64 /* Addresses of exception data blocks for predefined exceptions. */
65 extern struct Exception_Data constraint_error
;
66 extern struct Exception_Data numeric_error
;
67 extern struct Exception_Data program_error
;
68 extern struct Exception_Data storage_error
;
69 extern struct Exception_Data tasking_error
;
70 extern struct Exception_Data _abort_signal
;
72 #define Lock_Task system__soft_links__lock_task
73 extern void (*Lock_Task
) PARAMS ((void));
75 #define Unlock_Task system__soft_links__unlock_task
76 extern void (*Unlock_Task
) PARAMS ((void));
78 #define Get_Machine_State_Addr \
79 system__soft_links__get_machine_state_addr
80 extern struct Machine_State
*(*Get_Machine_State_Addr
) PARAMS ((void));
82 #define Check_Abort_Status \
83 system__soft_links__check_abort_status
84 extern int (*Check_Abort_Status
) PARAMS ((void));
86 #define Raise_From_Signal_Handler \
87 ada__exceptions__raise_from_signal_handler
88 extern void Raise_From_Signal_Handler
PARAMS ((struct Exception_Data
*,
91 #define Propagate_Signal_Exception \
92 __gnat_propagate_sig_exc
93 extern void Propagate_Signal_Exception
94 PARAMS ((struct Machine_State
*, struct Exception_Data
*, const char *));
96 /* Copies of global values computed by the binder */
97 int __gl_main_priority
= -1;
98 int __gl_time_slice_val
= -1;
99 char __gl_wc_encoding
= 'n';
100 char __gl_locking_policy
= ' ';
101 char __gl_queuing_policy
= ' ';
102 char __gl_task_dispatching_policy
= ' ';
103 int __gl_unreserve_all_interrupts
= 0;
104 int __gl_exception_tracebacks
= 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;
110 /* HAVE_GNAT_INIT_FLOAT must be set on every targets where a __gnat_init_float
111 is defined. If this is not set them a void implementation will be defined
112 at the end of this unit. */
113 #undef HAVE_GNAT_INIT_FLOAT
115 /**********************/
116 /* __gnat_set_globals */
117 /**********************/
119 /* This routine is called from the binder generated main program. It copies
120 the values for global quantities computed by the binder into the following
121 global locations. The reason that we go through this copy, rather than just
122 define the global locations in the binder generated file, is that they are
123 referenced from the runtime, which may be in a shared library, and the
124 binder file is not in the shared library. Global references across library
125 boundaries like this are not handled correctly in all systems. */
128 __gnat_set_globals (main_priority
, time_slice_val
, wc_encoding
, locking_policy
,
129 queuing_policy
, task_dispatching_policy
, adafinal_ptr
,
130 unreserve_all_interrupts
, exception_tracebacks
)
134 int locking_policy
, queuing_policy
, task_dispatching_policy
;
135 void (*adafinal_ptr
) PARAMS ((void)) ATTRIBUTE_UNUSED
;
136 int unreserve_all_interrupts
, exception_tracebacks
;
138 static int already_called
= 0;
140 /* If this procedure has been already called once, check that the
141 arguments in this call are consistent with the ones in the previous
142 calls. Otherwise, raise a Program_Error exception.
144 We do not check for consistency of the wide character encoding
145 method. This default affects only Wide_Text_IO where no explicit
146 coding method is given, and there is no particular reason to let
147 this default be affected by the source representation of a library
150 The value of main_priority is meaningful only when we are invoked
151 from the main program elaboration routine of an Ada application.
152 Checking the consistency of this parameter should therefore not be
153 done. Since it is assured that the main program elaboration will
154 always invoke this procedure before any library elaboration
155 routine, only the value of main_priority during the first call
156 should be taken into account and all the subsequent ones should be
157 ignored. Note that the case where the main program is not written
158 in Ada is also properly handled, since the default value will then
159 be used for this parameter.
161 For identical reasons, the consistency of time_slice_val should not
166 if (__gl_locking_policy
!= locking_policy
||
167 __gl_queuing_policy
!= queuing_policy
||
168 __gl_task_dispatching_policy
!= task_dispatching_policy
||
169 __gl_unreserve_all_interrupts
!= unreserve_all_interrupts
||
170 __gl_exception_tracebacks
!= exception_tracebacks
)
172 __gnat_raise_program_error (__FILE__
, __LINE__
);
178 __gl_main_priority
= main_priority
;
179 __gl_time_slice_val
= time_slice_val
;
180 __gl_wc_encoding
= wc_encoding
;
181 __gl_locking_policy
= locking_policy
;
182 __gl_queuing_policy
= queuing_policy
;
183 __gl_task_dispatching_policy
= task_dispatching_policy
;
184 __gl_unreserve_all_interrupts
= unreserve_all_interrupts
;
185 __gl_exception_tracebacks
= exception_tracebacks
;
188 /*********************/
189 /* __gnat_initialize */
190 /*********************/
192 /* __gnat_initialize is called at the start of execution of an Ada program
193 (the call is generated by the binder). The standard routine does nothing
194 at all; the intention is that this be replaced by system specific
195 code where initialization is required. */
197 /***********************************/
198 /* __gnat_initialize (AIX version) */
199 /***********************************/
203 /* AiX doesn't have SA_NODEFER */
207 #include <sys/time.h>
209 /* AiX doesn't have nanosleep, but provides nsleep instead */
211 extern int nanosleep
PARAMS ((struct timestruc_t
*, struct timestruc_t
*));
212 static void __gnat_error_handler
PARAMS ((int));
215 nanosleep (Rqtp
, Rmtp
)
216 struct timestruc_t
*Rqtp
, *Rmtp
;
218 return nsleep (Rqtp
, Rmtp
);
224 __gnat_error_handler (sig
)
227 struct Exception_Data
*exception
;
233 /* FIXME: we need to detect the case of a *real* SIGSEGV */
234 exception
= &storage_error
;
235 msg
= "stack overflow or erroneous memory access";
239 exception
= &constraint_error
;
244 exception
= &constraint_error
;
249 exception
= &program_error
;
250 msg
= "unhandled signal";
253 Raise_From_Signal_Handler (exception
, msg
);
257 __gnat_install_handler ()
259 struct sigaction act
;
261 /* Set up signal handler to map synchronous signals to appropriate
262 exceptions. Make sure that the handler isn't interrupted by another
263 signal that might cause a scheduling event! */
265 act
.sa_handler
= __gnat_error_handler
;
266 act
.sa_flags
= SA_NODEFER
| SA_RESTART
;
267 (void) sigemptyset (&act
.sa_mask
);
269 (void) sigaction (SIGABRT
, &act
, NULL
);
270 (void) sigaction (SIGFPE
, &act
, NULL
);
272 if (__gl_unreserve_all_interrupts
== 0)
274 (void) sigaction (SIGILL
, &act
, NULL
);
275 (void) sigaction (SIGSEGV
, &act
, NULL
);
276 (void) sigaction (SIGBUS
, &act
, NULL
);
278 __gnat_handler_installed
= 1;
286 /****************************************/
287 /* __gnat_initialize (Dec Unix version) */
288 /****************************************/
290 #elif defined(__alpha__) && defined(__osf__) && ! defined(__alpha_vxworks)
292 /* Note: it seems that __osf__ is defined for the Alpha VXWorks case. Not
293 clear that this is reasonable, but in any case we have to be sure to
294 exclude this case in the above test. */
297 #include <sys/siginfo.h>
299 static void __gnat_error_handler
PARAMS ((int, siginfo_t
*,
300 struct sigcontext
*));
301 extern char *__gnat_get_code_loc
PARAMS ((struct sigcontext
*));
302 extern void __gnat_enter_handler
PARAMS ((struct sigcontext
*, char *));
303 extern size_t __gnat_machine_state_length
PARAMS ((void));
305 extern long exc_lookup_gp
PARAMS ((char *));
306 extern void exc_resume
PARAMS ((struct sigcontext
*));
309 __gnat_error_handler (sig
, sip
, context
)
312 struct sigcontext
*context
;
314 struct Exception_Data
*exception
;
315 static int recurse
= 0;
316 struct sigcontext
*mstate
;
319 /* If this was an explicit signal from a "kill", just resignal it. */
320 if (SI_FROMUSER (sip
))
322 signal (sig
, SIG_DFL
);
323 kill (getpid(), sig
);
326 /* Otherwise, treat it as something we handle. */
330 /* If the problem was permissions, this is a constraint error.
331 Likewise if the failing address isn't maximally aligned or if
334 ??? Using a static variable here isn't task-safe, but it's
335 much too hard to do anything else and we're just determining
336 which exception to raise. */
337 if (sip
->si_code
== SEGV_ACCERR
338 || (((long) sip
->si_addr
) & 3) != 0
341 exception
= &constraint_error
;
346 /* See if the page before the faulting page is accessible. Do that
347 by trying to access it. We'd like to simply try to access
348 4096 + the faulting address, but it's not guaranteed to be
349 the actual address, just to be on the same page. */
352 ((long) sip
->si_addr
& - getpagesize ()))[getpagesize ()];
353 msg
= "stack overflow (or erroneous memory access)";
354 exception
= &storage_error
;
359 exception
= &program_error
;
364 exception
= &constraint_error
;
369 exception
= &program_error
;
370 msg
= "unhandled signal";
374 mstate
= (struct sigcontext
*) (*Get_Machine_State_Addr
) ();
378 Raise_From_Signal_Handler (exception
, (char *) msg
);
382 __gnat_install_handler ()
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 (*) PARAMS ((int))) __gnat_error_handler
;
391 act
.sa_flags
= SA_ONSTACK
| SA_RESTART
| SA_NODEFER
| SA_SIGINFO
;
392 (void) sigemptyset (&act
.sa_mask
);
394 (void) sigaction (SIGABRT
, &act
, NULL
);
395 (void) sigaction (SIGFPE
, &act
, NULL
);
397 if (__gl_unreserve_all_interrupts
== 0)
399 (void) sigaction (SIGILL
, &act
, NULL
);
400 (void) sigaction (SIGSEGV
, &act
, NULL
);
401 (void) sigaction (SIGBUS
, &act
, NULL
);
404 __gnat_handler_installed
= 1;
412 /* Routines called by 5amastop.adb. */
417 __gnat_get_code_loc (context
)
418 struct sigcontext
*context
;
420 return (char *) context
->sc_pc
;
424 __gnat_enter_handler (context
, pc
)
425 struct sigcontext
*context
;
428 context
->sc_pc
= (long) pc
;
429 context
->sc_regs
[SC_GP
] = exc_lookup_gp (pc
);
430 exc_resume (context
);
434 __gnat_machine_state_length ()
436 return sizeof (struct sigcontext
);
439 /***********************************/
440 /* __gnat_initialize (HPUX version) */
441 /***********************************/
447 static void __gnat_error_handler
PARAMS ((int));
450 __gnat_error_handler (sig
)
453 struct Exception_Data
*exception
;
459 /* FIXME: we need to detect the case of a *real* SIGSEGV */
460 exception
= &storage_error
;
461 msg
= "stack overflow or erroneous memory access";
465 exception
= &constraint_error
;
470 exception
= &constraint_error
;
475 exception
= &program_error
;
476 msg
= "unhandled signal";
479 Raise_From_Signal_Handler (exception
, msg
);
483 __gnat_install_handler ()
485 struct sigaction act
;
487 /* Set up signal handler to map synchronous signals to appropriate
488 exceptions. Make sure that the handler isn't interrupted by another
489 signal that might cause a scheduling event! Also setup an alternate
490 stack region for the handler execution so that stack overflows can be
491 handled properly, avoiding a SEGV generation from stack usage by the
494 static char handler_stack
[SIGSTKSZ
];
498 stack
.ss_sp
= handler_stack
;
499 stack
.ss_size
= SIGSTKSZ
;
502 (void) sigaltstack (&stack
, NULL
);
504 act
.sa_handler
= __gnat_error_handler
;
505 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_ONSTACK
;
506 (void) sigemptyset (&act
.sa_mask
);
508 (void) sigaction (SIGABRT
, &act
, NULL
);
509 (void) sigaction (SIGFPE
, &act
, NULL
);
511 if (__gl_unreserve_all_interrupts
== 0)
513 (void) sigaction (SIGILL
, &act
, NULL
);
514 (void) sigaction (SIGSEGV
, &act
, NULL
);
515 (void) sigaction (SIGBUS
, &act
, NULL
);
517 __gnat_handler_installed
= 1;
525 /*************************************/
526 /* __gnat_initialize (GNU/Linux version) */
527 /*************************************/
529 #elif defined (linux) && defined (i386) && !defined (__RT__)
532 #include <asm/sigcontext.h>
534 /* GNU/Linux, which uses glibc, does not define NULL in included
538 #define NULL ((void *) 0)
551 static void __gnat_error_handler
PARAMS ((int));
554 __gnat_error_handler (sig
)
557 struct Exception_Data
*exception
;
559 static int recurse
= 0;
561 struct sigcontext
*info
562 = (struct sigcontext
*) (((char *) &sig
) + sizeof (int));
564 /* The Linux kernel does not document how to get the machine state in a
565 signal handler, but in fact the necessary data is in a sigcontext_struct
566 value that is on the stack immediately above the signal number
567 parameter, and the above messing accesses this value on the stack. */
569 struct Machine_State
*mstate
;
574 /* If the problem was permissions, this is a constraint error.
575 Likewise if the failing address isn't maximally aligned or if
578 ??? Using a static variable here isn't task-safe, but it's
579 much too hard to do anything else and we're just determining
580 which exception to raise. */
583 exception
= &constraint_error
;
588 /* Here we would like a discrimination test to see whether the
589 page before the faulting address is accessible. Unfortunately
590 Linux seems to have no way of giving us the faulting address.
592 In versions of a-init.c before 1.95, we had a test of the page
593 before the stack pointer using:
597 ((long) info->esp_at_signal & - getpagesize ()))[getpagesize ()];
599 but that's wrong, since it tests the stack pointer location, and
600 the current stack probe code does not move the stack pointer
601 until all probes succeed.
603 For now we simply do not attempt any discrimination at all. Note
604 that this is quite acceptable, since a "real" SIGSEGV can only
605 occur as the result of an erroneous program */
607 msg
= "stack overflow (or erroneous memory access)";
608 exception
= &storage_error
;
613 exception
= &constraint_error
;
618 exception
= &constraint_error
;
623 exception
= &program_error
;
624 msg
= "unhandled signal";
627 mstate
= (*Get_Machine_State_Addr
)();
630 mstate
->eip
= info
->eip
;
631 mstate
->ebx
= info
->ebx
;
632 mstate
->esp
= info
->esp_at_signal
;
633 mstate
->ebp
= info
->ebp
;
634 mstate
->esi
= info
->esi
;
635 mstate
->edi
= info
->edi
;
639 Raise_From_Signal_Handler (exception
, msg
);
643 __gnat_install_handler ()
645 struct sigaction act
;
647 /* Set up signal handler to map synchronous signals to appropriate
648 exceptions. Make sure that the handler isn't interrupted by another
649 signal that might cause a scheduling event! */
651 act
.sa_handler
= __gnat_error_handler
;
652 act
.sa_flags
= SA_NODEFER
| SA_RESTART
;
653 (void) sigemptyset (&act
.sa_mask
);
655 (void) sigaction (SIGABRT
, &act
, NULL
);
656 (void) sigaction (SIGFPE
, &act
, NULL
);
658 if (__gl_unreserve_all_interrupts
== 0)
660 (void) sigaction (SIGILL
, &act
, NULL
);
661 (void) sigaction (SIGSEGV
, &act
, NULL
);
662 (void) sigaction (SIGBUS
, &act
, NULL
);
664 __gnat_handler_installed
= 1;
672 /******************************************/
673 /* __gnat_initialize (NT-mingw32 version) */
674 /******************************************/
676 #elif defined (__MINGW32__)
679 static LONG __gnat_error_handler
PARAMS ((PEXCEPTION_POINTERS
));
681 /* __gnat_initialize (mingw32). */
684 __gnat_error_handler (info
)
685 PEXCEPTION_POINTERS info
;
688 struct Exception_Data
*exception
;
691 switch (info
->ExceptionRecord
->ExceptionCode
)
693 case EXCEPTION_ACCESS_VIOLATION
:
694 /* If the failing address isn't maximally-aligned or if we've
695 recursed, this is a program error. */
696 if ((info
->ExceptionRecord
->ExceptionInformation
[1] & 3) != 0
699 exception
= &program_error
;
700 msg
= "EXCEPTION_ACCESS_VIOLATION";
704 /* See if the page before the faulting page is accessible. Do that
705 by trying to access it. */
707 * ((volatile char *) (info
->ExceptionRecord
->ExceptionInformation
[1]
709 exception
= &storage_error
;
710 msg
= "stack overflow (or erroneous memory access)";
714 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
715 exception
= &constraint_error
;
716 msg
= "EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
719 case EXCEPTION_DATATYPE_MISALIGNMENT
:
720 exception
= &constraint_error
;
721 msg
= "EXCEPTION_DATATYPE_MISALIGNMENT";
724 case EXCEPTION_FLT_DENORMAL_OPERAND
:
725 exception
= &constraint_error
;
726 msg
= "EXCEPTION_FLT_DENORMAL_OPERAND";
729 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
730 exception
= &constraint_error
;
731 msg
= "EXCEPTION_FLT_DENORMAL_OPERAND";
734 case EXCEPTION_FLT_INVALID_OPERATION
:
735 exception
= &constraint_error
;
736 msg
= "EXCEPTION_FLT_INVALID_OPERATION";
739 case EXCEPTION_FLT_OVERFLOW
:
740 exception
= &constraint_error
;
741 msg
= "EXCEPTION_FLT_OVERFLOW";
744 case EXCEPTION_FLT_STACK_CHECK
:
745 exception
= &program_error
;
746 msg
= "EXCEPTION_FLT_STACK_CHECK";
749 case EXCEPTION_FLT_UNDERFLOW
:
750 exception
= &constraint_error
;
751 msg
= "EXCEPTION_FLT_UNDERFLOW";
754 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
755 exception
= &constraint_error
;
756 msg
= "EXCEPTION_INT_DIVIDE_BY_ZERO";
759 case EXCEPTION_INT_OVERFLOW
:
760 exception
= &constraint_error
;
761 msg
= "EXCEPTION_INT_OVERFLOW";
764 case EXCEPTION_INVALID_DISPOSITION
:
765 exception
= &program_error
;
766 msg
= "EXCEPTION_INVALID_DISPOSITION";
769 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
770 exception
= &program_error
;
771 msg
= "EXCEPTION_NONCONTINUABLE_EXCEPTION";
774 case EXCEPTION_PRIV_INSTRUCTION
:
775 exception
= &program_error
;
776 msg
= "EXCEPTION_PRIV_INSTRUCTION";
779 case EXCEPTION_SINGLE_STEP
:
780 exception
= &program_error
;
781 msg
= "EXCEPTION_SINGLE_STEP";
784 case EXCEPTION_STACK_OVERFLOW
:
785 exception
= &storage_error
;
786 msg
= "EXCEPTION_STACK_OVERFLOW";
790 exception
= &program_error
;
791 msg
= "unhandled signal";
795 Raise_From_Signal_Handler (exception
, msg
);
799 __gnat_install_handler ()
801 SetUnhandledExceptionFilter (__gnat_error_handler
);
802 __gnat_handler_installed
= 1;
809 /* Initialize floating-point coprocessor. This call is needed because
810 the MS libraries default to 64-bit precision instead of 80-bit
811 precision, and we require the full precision for proper operation,
812 given that we have set Max_Digits etc with this in mind */
814 __gnat_init_float ();
816 /* initialize a lock for a process handle list - see a-adaint.c for the
817 implementation of __gnat_portable_no_block_spawn, __gnat_portable_wait */
821 /**************************************/
822 /* __gnat_initialize (Interix version) */
823 /**************************************/
825 #elif defined (__INTERIX)
829 static void __gnat_error_handler
PARAMS ((int));
832 __gnat_error_handler (sig
)
835 struct Exception_Data
*exception
;
841 exception
= &storage_error
;
842 msg
= "stack overflow or erroneous memory access";
846 exception
= &constraint_error
;
851 exception
= &constraint_error
;
856 exception
= &program_error
;
857 msg
= "unhandled signal";
860 Raise_From_Signal_Handler (exception
, msg
);
864 __gnat_install_handler ()
866 struct sigaction act
;
868 /* Set up signal handler to map synchronous signals to appropriate
869 exceptions. Make sure that the handler isn't interrupted by another
870 signal that might cause a scheduling event! */
872 act
.sa_handler
= __gnat_error_handler
;
874 (void) sigemptyset (&act
.sa_mask
);
876 /* Handlers for signals besides SIGSEGV cause c974013 to hang */
877 /* (void) sigaction (SIGILL, &act, NULL); */
878 /* (void) sigaction (SIGABRT, &act, NULL); */
879 /* (void) sigaction (SIGFPE, &act, NULL); */
880 /* (void) sigaction (SIGBUS, &act, NULL); */
881 if (__gl_unreserve_all_interrupts
== 0)
883 (void) sigaction (SIGSEGV
, &act
, NULL
);
885 __gnat_handler_installed
= 1;
891 __gnat_init_float ();
894 /**************************************/
895 /* __gnat_initialize (LynxOS version) */
896 /**************************************/
898 #elif defined (__Lynx__)
903 __gnat_init_float ();
906 /*********************************/
907 /* __gnat_install_handler (Lynx) */
908 /*********************************/
911 __gnat_install_handler ()
913 __gnat_handler_installed
= 1;
916 /****************************/
917 /* __gnat_initialize (OS/2) */
918 /****************************/
920 #elif defined (__EMX__) /* OS/2 dependent initialization */
927 /*********************************/
928 /* __gnat_install_handler (OS/2) */
929 /*********************************/
932 __gnat_install_handler ()
934 __gnat_handler_installed
= 1;
937 /***********************************/
938 /* __gnat_initialize (SGI version) */
939 /***********************************/
950 #define SIGADAABORT 48
951 #define SIGNAL_STACK_SIZE 4096
952 #define SIGNAL_STACK_ALIGNMENT 64
956 sigcontext_t context
;
959 static void __gnat_error_handler
PARAMS ((int, int, sigcontext_t
*));
962 __gnat_error_handler (sig
, code
, sc
)
967 struct Machine_State
*mstate
;
968 struct Exception_Data
*exception
;
978 exception
= &program_error
;
979 msg
= "SIGSEGV: (Invalid virtual address)";
981 else if (code
== ENXIO
)
983 exception
= &program_error
;
984 msg
= "SIGSEGV: (Read beyond mapped object)";
986 else if (code
== ENOSPC
)
988 exception
= &program_error
; /* ??? storage_error ??? */
989 msg
= "SIGSEGV: (Autogrow for file failed)";
991 else if (code
== EACCES
)
993 /* ??? Re-add smarts to further verify that we launched
994 the stack into a guard page, not an attempt to
995 write to .text or something */
996 exception
= &storage_error
;
997 msg
= "SIGSEGV: (stack overflow or erroneous memory access)";
1001 /* Just in case the OS guys did it to us again. Sometimes
1002 they fail to document all of the valid codes that are
1003 passed to signal handlers, just in case someone depends
1004 on knowing all the codes */
1005 exception
= &program_error
;
1006 msg
= "SIGSEGV: (Undocumented reason)";
1011 /* Map all bus errors to Program_Error. */
1012 exception
= &program_error
;
1017 /* Map all fpe errors to Constraint_Error. */
1018 exception
= &constraint_error
;
1023 if ((*Check_Abort_Status
) ())
1025 exception
= &_abort_signal
;
1034 /* Everything else is a Program_Error. */
1035 exception
= &program_error
;
1036 msg
= "unhandled signal";
1039 mstate
= (*Get_Machine_State_Addr
)();
1041 memcpy ((void *) mstate
, (const void *) sc
, sizeof (sigcontext_t
));
1043 Raise_From_Signal_Handler (exception
, msg
);
1048 __gnat_install_handler ()
1051 struct sigaction act
;
1053 /* Setup signal handler to map synchronous signals to appropriate
1054 exceptions. Make sure that the handler isn't interrupted by another
1055 signal that might cause a scheduling event! */
1057 act
.sa_handler
= __gnat_error_handler
;
1058 act
.sa_flags
= SA_NODEFER
+ SA_RESTART
;
1059 (void) sigfillset (&act
.sa_mask
);
1060 (void) sigemptyset (&act
.sa_mask
);
1062 (void) sigaction (SIGABRT
, &act
, NULL
);
1063 (void) sigaction (SIGFPE
, &act
, NULL
);
1065 if (__gl_unreserve_all_interrupts
== 0)
1067 (void) sigaction (SIGILL
, &act
, NULL
);
1068 (void) sigaction (SIGSEGV
, &act
, NULL
);
1069 (void) sigaction (SIGBUS
, &act
, NULL
);
1071 (void) sigaction (SIGADAABORT
, &act
, NULL
);
1072 __gnat_handler_installed
= 1;
1076 __gnat_initialize ()
1080 /*************************************************/
1081 /* __gnat_initialize (Solaris and SunOS version) */
1082 /*************************************************/
1084 #elif defined (sun) && defined (__SVR4) && !defined (__vxworks)
1087 #include <siginfo.h>
1089 static void __gnat_error_handler
PARAMS ((int, siginfo_t
*));
1092 __gnat_error_handler (sig
, sip
)
1096 struct Exception_Data
*exception
;
1097 static int recurse
= 0;
1100 /* If this was an explicit signal from a "kill", just resignal it. */
1101 if (SI_FROMUSER (sip
))
1103 signal (sig
, SIG_DFL
);
1104 kill (getpid(), sig
);
1107 /* Otherwise, treat it as something we handle. */
1111 /* If the problem was permissions, this is a constraint error.
1112 Likewise if the failing address isn't maximally aligned or if
1115 ??? Using a static variable here isn't task-safe, but it's
1116 much too hard to do anything else and we're just determining
1117 which exception to raise. */
1118 if (sip
->si_code
== SEGV_ACCERR
1119 || (((long) sip
->si_addr
) & 3) != 0
1122 exception
= &constraint_error
;
1127 /* See if the page before the faulting page is accessible. Do that
1128 by trying to access it. We'd like to simply try to access
1129 4096 + the faulting address, but it's not guaranteed to be
1130 the actual address, just to be on the same page. */
1133 ((long) sip
->si_addr
& - getpagesize ()))[getpagesize ()];
1134 exception
= &storage_error
;
1135 msg
= "stack overflow (or erroneous memory access)";
1140 exception
= &program_error
;
1145 exception
= &constraint_error
;
1150 exception
= &program_error
;
1151 msg
= "unhandled signal";
1156 Raise_From_Signal_Handler (exception
, msg
);
1160 __gnat_install_handler ()
1162 struct sigaction act
;
1164 /* Set up signal handler to map synchronous signals to appropriate
1165 exceptions. Make sure that the handler isn't interrupted by another
1166 signal that might cause a scheduling event! */
1168 act
.sa_handler
= __gnat_error_handler
;
1169 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
1170 (void) sigemptyset (&act
.sa_mask
);
1172 (void) sigaction (SIGABRT
, &act
, NULL
);
1174 if (__gl_unreserve_all_interrupts
== 0)
1176 (void) sigaction (SIGFPE
, &act
, NULL
);
1177 (void) sigaction (SIGSEGV
, &act
, NULL
);
1178 (void) sigaction (SIGBUS
, &act
, NULL
);
1180 __gnat_handler_installed
= 1;
1184 __gnat_initialize ()
1188 /***********************************/
1189 /* __gnat_initialize (SNI version) */
1190 /***********************************/
1192 #elif defined (__sni__)
1194 /* SNI needs special defines and includes */
1196 #define _XOPEN_SOURCE
1197 #define _POSIX_SOURCE
1200 extern size_t __gnat_getpagesize
PARAMS ((void));
1201 static void __gnat_error_handler
PARAMS ((int));
1203 /* The run time needs this function which is a #define in SNI */
1206 __gnat_getpagesize ()
1208 return getpagesize ();
1212 __gnat_error_handler (sig
)
1215 struct Exception_Data
*exception
;
1221 /* FIXME: we need to detect the case of a *real* SIGSEGV */
1222 exception
= &storage_error
;
1223 msg
= "stack overflow or erroneous memory access";
1227 exception
= &constraint_error
;
1232 exception
= &constraint_error
;
1237 exception
= &program_error
;
1238 msg
= "unhandled signal";
1241 Raise_From_Signal_Handler (exception
, msg
);
1245 __gnat_install_handler ()
1247 struct sigaction act
;
1249 /* Set up signal handler to map synchronous signals to appropriate
1250 exceptions. Make sure that the handler isn't interrupted by another
1251 signal that might cause a scheduling event! */
1253 act
.sa_handler
= __gnat_error_handler
;
1254 act
.sa_flags
= SA_NODEFER
| SA_RESTART
;
1255 (void) sigemptyset (&act
.sa_mask
);
1257 (void) sigaction (SIGABRT
, &act
, NULL
);
1258 (void) sigaction (SIGFPE
, &act
, NULL
);
1260 if (__gl_unreserve_all_interrupts
== 0)
1262 (void) sigaction (SIGILL
, &act
, NULL
);
1263 (void) sigaction (SIGSEGV
, &act
, NULL
);
1264 (void) sigaction (SIGBUS
, &act
, NULL
);
1266 __gnat_handler_installed
= 1;
1270 __gnat_initialize ()
1274 /***********************************/
1275 /* __gnat_initialize (VMS version) */
1276 /***********************************/
1280 /* The prehandler actually gets control first on a condition. It swaps the
1281 stack pointer and calls the handler (__gnat_error_handler). */
1282 extern long __gnat_error_prehandler ();
1284 extern char *__gnat_error_prehandler_stack
; /* Alternate signal stack */
1286 /* Conditions that don't have an Ada exception counterpart must raise
1287 Non_Ada_Error. Since this is defined in s-auxdec, it should only be
1288 referenced by user programs, not the compiler or tools. Hence the
1292 #define Non_Ada_Error system__aux_dec__non_ada_error
1293 extern struct Exception_Data Non_Ada_Error
;
1295 #define Coded_Exception system__vms_exception_table__coded_exception
1296 extern struct Exception_Data
*Coded_Exception (int);
1299 /* Define macro symbols for the VMS conditions that become Ada exceptions.
1300 Most of these are also defined in the header file ssdef.h which has not
1301 yet been converted to be recoginized by Gnu C. Some, which couldn't be
1302 located, are assigned names based on the DEC test suite tests which
1305 #define SS$_ACCVIO 12
1306 #define SS$_DEBUG 1132
1307 #define SS$_INTDIV 1156
1308 #define SS$_HPARITH 1284
1309 #define SS$_STKOVF 1364
1310 #define SS$_RESIGNAL 2328
1311 #define MTH$_FLOOVEMAT 1475268 /* Some ACVC_21 CXA tests */
1312 #define SS$_CE24VRU 3253636 /* Write to unopened file */
1313 #define SS$_C980VTE 3246436 /* AST requests time slice */
1314 #define CMA$_EXIT_THREAD 4227492
1315 #define CMA$_EXCCOPLOS 4228108
1316 #define CMA$_ALERTED 4227460
1318 struct descriptor_s
{unsigned short len
, mbz
; char *adr
; };
1320 static long __gnat_error_handler
PARAMS ((int *, void *));
1323 __gnat_error_handler (sigargs
, mechargs
)
1327 struct Exception_Data
*exception
= 0;
1331 struct descriptor_s msgdesc
;
1332 int msg_flag
= 0x000f; /* 1 bit for each of the four message parts */
1333 unsigned short outlen
;
1334 char curr_icb
[544];
1335 long curr_invo_handle
;
1338 /* Resignaled conditions aren't effected by by pragma Import_Exception */
1343 case CMA$_EXIT_THREAD
:
1344 return SS$_RESIGNAL
;
1346 case SS$_DEBUG
: /* Gdb attach, resignal to merge activate gdbstub. */
1347 return SS$_RESIGNAL
;
1349 case 1409786: /* Nickerson bug #33 ??? */
1350 return SS$_RESIGNAL
;
1352 case 1381050: /* Nickerson bug #33 ??? */
1353 return SS$_RESIGNAL
;
1355 case 11829410: /* Resignalled as Use_Error for CE10VRC */
1356 return SS$_RESIGNAL
;
1361 /* See if it's an imported exception. Mask off severity bits. */
1362 exception
= Coded_Exception (sigargs
[1] & 0xfffffff8);
1367 msgdesc
.adr
= message
;
1368 SYS$
GETMSG (sigargs
[1], &outlen
, &msgdesc
, msg_flag
, 0);
1369 message
[outlen
] = 0;
1372 exception
->Name_Length
= 19;
1373 /* The full name really should be get sys$getmsg returns. ??? */
1374 exception
->Full_Name
= "IMPORTED_EXCEPTION";
1375 exception
->Import_Code
= sigargs
[1] & 0xfffffff8;
1383 if (sigargs
[3] == 0)
1385 exception
= &constraint_error
;
1386 msg
= "access zero";
1390 exception
= &storage_error
;
1391 msg
= "stack overflow (or erroneous memory access)";
1396 exception
= &storage_error
;
1397 msg
= "stack overflow";
1401 exception
= &constraint_error
;
1402 msg
= "division by zero";
1407 return SS$_RESIGNAL
; /* toplev.c handles for compiler */
1410 exception
= &constraint_error
;
1411 msg
= "arithmetic error";
1416 case MTH$_FLOOVEMAT
:
1417 exception
= &constraint_error
;
1418 msg
= "floating overflow in math library";
1422 exception
= &constraint_error
;
1427 exception
= &program_error
;
1433 exception
= &program_error
;
1435 /* User programs expect Non_Ada_Error to be raised, reference
1436 DEC Ada test CXCONDHAN. */
1437 exception
= &Non_Ada_Error
;
1441 msgdesc
.adr
= message
;
1442 SYS$
GETMSG (sigargs
[1], &outlen
, &msgdesc
, msg_flag
, 0);
1443 message
[outlen
] = 0;
1448 mstate
= (long *) (*Get_Machine_State_Addr
) ();
1451 LIB$
GET_CURR_INVO_CONTEXT (&curr_icb
);
1452 LIB$
GET_PREV_INVO_CONTEXT (&curr_icb
);
1453 LIB$
GET_PREV_INVO_CONTEXT (&curr_icb
);
1454 curr_invo_handle
= LIB$
GET_INVO_HANDLE (&curr_icb
);
1455 *mstate
= curr_invo_handle
;
1457 Raise_From_Signal_Handler (exception
, msg
);
1461 __gnat_install_handler ()
1466 c
= (char *) malloc (2049);
1468 __gnat_error_prehandler_stack
= &c
[2048];
1470 /* __gnat_error_prehandler is an assembly function. */
1471 SYS$
SETEXV (1, __gnat_error_prehandler
, 3, &prvhnd
);
1472 __gnat_handler_installed
= 1;
1480 /***************************************/
1481 /* __gnat_initialize (VXWorks version) */
1482 /***************************************/
1484 #elif defined(__vxworks)
1487 #include <taskLib.h>
1491 static void __gnat_init_handler
PARAMS ((int));
1492 extern int __gnat_inum_to_ivec
PARAMS ((int));
1493 static void __gnat_error_handler
PARAMS ((int, int, struct sigcontext
*));
1496 __gnat_int_handler (interr
)
1499 /* Note that we should use something like Raise_From_Int_Handler here, but
1500 for now Raise_From_Signal_Handler will do the job. ??? */
1502 Raise_From_Signal_Handler (&storage_error
, "stack overflow");
1505 /* Used for stack-checking on VxWorks. Must be task-local in
1508 void *__gnat_stack_limit
= NULL
;
1510 #ifndef __alpha_vxworks
1512 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1515 extern long getpid
PARAMS ((void));
1520 return taskIdSelf ();
1524 /* This is needed by the GNAT run time to handle Vxworks interrupts */
1526 __gnat_inum_to_ivec (num
)
1529 return INUM_TO_IVEC (num
);
1533 __gnat_error_handler (sig
, code
, sc
)
1536 struct sigcontext
*sc
;
1538 struct Exception_Data
*exception
;
1543 /* VxWorks will always mask out the signal during the signal handler and
1544 will reenable it on a longjmp. GNAT does not generate a longjmp to
1545 return from a signal handler so the signal will still be masked unless
1547 (void) sigprocmask (SIG_SETMASK
, NULL
, &mask
);
1548 sigdelset (&mask
, sig
);
1549 (void) sigprocmask (SIG_SETMASK
, &mask
, NULL
);
1551 /* VxWorks will suspend the task when it gets a hardware exception. We
1552 take the liberty of resuming the task for the application. */
1553 if (taskIsSuspended (taskIdSelf ()) != 0)
1554 (void) taskResume (taskIdSelf ());
1559 exception
= &constraint_error
;
1563 exception
= &constraint_error
;
1567 exception
= &program_error
;
1571 exception
= &program_error
;
1575 exception
= &program_error
;
1576 msg
= "unhandled signal";
1579 Raise_From_Signal_Handler (exception
, msg
);
1583 __gnat_install_handler ()
1585 struct sigaction act
;
1587 /* Setup signal handler to map synchronous signals to appropriate
1588 exceptions. Make sure that the handler isn't interrupted by another
1589 signal that might cause a scheduling event! */
1591 act
.sa_handler
= __gnat_error_handler
;
1592 act
.sa_flags
= SA_SIGINFO
| SA_ONSTACK
;
1593 (void) sigemptyset (&act
.sa_mask
);
1595 (void) sigaction (SIGFPE
, &act
, NULL
);
1597 if (__gl_unreserve_all_interrupts
== 0)
1599 (void) sigaction (SIGILL
, &act
, NULL
);
1600 (void) sigaction (SIGSEGV
, &act
, NULL
);
1601 (void) sigaction (SIGBUS
, &act
, NULL
);
1603 __gnat_handler_installed
= 1;
1606 #define HAVE_GNAT_INIT_FLOAT
1609 __gnat_init_float ()
1611 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT)
1612 /* Disable overflow/underflow exceptions on the PPC processor, this is needed
1613 to get correct Ada semantic */
1620 __gnat_initialize ()
1622 TASK_DESC pTaskDesc
;
1624 if (taskInfoGet (taskIdSelf (), &pTaskDesc
) != OK
)
1625 printErr ("Cannot get task info");
1627 __gnat_stack_limit
= (void *) pTaskDesc
.td_pStackLimit
;
1629 __gnat_init_float ();
1631 #ifdef __mips_vxworks
1633 /* For now remove this handler, since it is causing interferences with gdb */
1635 /* Connect the overflow trap directly to the __gnat_int_handler routine
1636 as it is not converted to a signal by VxWorks. */
1638 intConnect (INUM_TO_IVEC (IV_TRAP_VEC
), &__gnat_int_handler
, IV_TRAP_VEC
);
1645 /* For all other versions of GNAT, the initialize routine and handler
1646 installation do nothing */
1648 /***************************************/
1649 /* __gnat_initialize (default version) */
1650 /***************************************/
1653 __gnat_initialize ()
1657 /********************************************/
1658 /* __gnat_install_handler (default version) */
1659 /********************************************/
1662 __gnat_install_handler ()
1664 __gnat_handler_installed
= 1;
1669 /*********************/
1670 /* __gnat_init_float */
1671 /*********************/
1673 /* This routine is called as each process thread is created, for possible
1674 initialization of the FP processor. This version is used under INTERIX,
1675 WIN32 and could be used under OS/2 */
1677 #if defined (_WIN32) || defined (__INTERIX) || defined (__EMX__) \
1678 || defined (__Lynx__)
1680 #define HAVE_GNAT_INIT_FLOAT
1683 __gnat_init_float ()
1685 #if defined (__i386__) || defined (i386)
1687 /* This is used to properly initialize the FPU on an x86 for each
1692 #endif /* Defined __i386__ */
1696 #ifndef HAVE_GNAT_INIT_FLOAT
1698 /* All targets without a specific __gnat_init_float will use an empty one */
1700 __gnat_init_float ()