1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
8 * C Implementation File *
10 * Copyright (C) 1992-2003 Free Software Foundation, Inc. *
12 * GNAT is free software; you can redistribute it and/or modify it under *
13 * terms of the GNU General Public License as published by the Free Soft- *
14 * ware Foundation; either version 2, or (at your option) any later ver- *
15 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
16 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
18 * for more details. You should have received a copy of the GNU General *
19 * Public License distributed with GNAT; see file COPYING. If not, write *
20 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
21 * MA 02111-1307, USA. *
23 * As a special exception, if you link this file with other files to *
24 * produce an executable, this file does not by itself cause the resulting *
25 * executable to be covered by the GNU General Public License. This except- *
26 * ion does not however invalidate any other reasons why the executable *
27 * file might be covered by the GNU Public License. *
29 * GNAT was originally developed by the GNAT team at New York University. *
30 * Extensive contributions were provided by Ada Core Technologies Inc. *
32 ****************************************************************************/
34 /* This unit contains initialization circuits that are system dependent. A
35 major part of the functionality involved involves stack overflow checking.
36 The GCC backend generates probe instructions to test for stack overflow.
37 For details on the exact approach used to generate these probes, see the
38 "Using and Porting GCC" manual, in particular the "Stack Checking" section
39 and the subsection "Specifying How Stack Checking is Done". The handlers
40 installed by this file are used to handle resulting signals that come
41 from these probes failing (i.e. touching protected pages) */
43 /* The following include is here to meet the published VxWorks requirement
44 that the __vxworks header appear before any other include. */
54 /* We don't have libiberty, so us malloc. */
55 #define xmalloc(S) malloc (S)
64 extern void __gnat_raise_program_error (const char *, int);
66 /* Addresses of exception data blocks for predefined exceptions. */
67 extern struct Exception_Data constraint_error
;
68 extern struct Exception_Data numeric_error
;
69 extern struct Exception_Data program_error
;
70 extern struct Exception_Data storage_error
;
71 extern struct Exception_Data tasking_error
;
72 extern struct Exception_Data _abort_signal
;
74 #define Lock_Task system__soft_links__lock_task
75 extern void (*Lock_Task
) PARAMS ((void));
77 #define Unlock_Task system__soft_links__unlock_task
78 extern void (*Unlock_Task
) PARAMS ((void));
80 #define Get_Machine_State_Addr \
81 system__soft_links__get_machine_state_addr
82 extern struct Machine_State
*(*Get_Machine_State_Addr
) PARAMS ((void));
84 #define Check_Abort_Status \
85 system__soft_links__check_abort_status
86 extern int (*Check_Abort_Status
) PARAMS ((void));
88 #define Raise_From_Signal_Handler \
89 ada__exceptions__raise_from_signal_handler
90 extern void Raise_From_Signal_Handler
PARAMS ((struct Exception_Data
*,
93 #define Propagate_Signal_Exception \
94 __gnat_propagate_sig_exc
95 extern void Propagate_Signal_Exception
96 PARAMS ((struct Machine_State
*, struct Exception_Data
*, const char *));
98 /* Copies of global values computed by the binder */
99 int __gl_main_priority
= -1;
100 int __gl_time_slice_val
= -1;
101 char __gl_wc_encoding
= 'n';
102 char __gl_locking_policy
= ' ';
103 char __gl_queuing_policy
= ' ';
104 char *__gl_restrictions
= 0;
105 char __gl_task_dispatching_policy
= ' ';
106 int __gl_unreserve_all_interrupts
= 0;
107 int __gl_exception_tracebacks
= 0;
108 int __gl_zero_cost_exceptions
= 0;
110 /* Indication of whether synchronous signal handler has already been
111 installed by a previous call to adainit */
112 int __gnat_handler_installed
= 0;
114 /* HAVE_GNAT_INIT_FLOAT must be set on every targets where a __gnat_init_float
115 is defined. If this is not set them a void implementation will be defined
116 at the end of this unit. */
117 #undef HAVE_GNAT_INIT_FLOAT
119 /**********************/
120 /* __gnat_set_globals */
121 /**********************/
123 /* This routine is called from the binder generated main program. It copies
124 the values for global quantities computed by the binder into the following
125 global locations. The reason that we go through this copy, rather than just
126 define the global locations in the binder generated file, is that they are
127 referenced from the runtime, which may be in a shared library, and the
128 binder file is not in the shared library. Global references across library
129 boundaries like this are not handled correctly in all systems. */
132 __gnat_set_globals (main_priority
, time_slice_val
, wc_encoding
, locking_policy
,
133 queuing_policy
, task_dispatching_policy
, restrictions
,
134 unreserve_all_interrupts
, exception_tracebacks
,
135 zero_cost_exceptions
)
139 char locking_policy
, queuing_policy
, task_dispatching_policy
;
141 int unreserve_all_interrupts
, exception_tracebacks
, zero_cost_exceptions
;
143 static int already_called
= 0;
145 /* If this procedure has been already called once, check that the
146 arguments in this call are consistent with the ones in the previous
147 calls. Otherwise, raise a Program_Error exception.
149 We do not check for consistency of the wide character encoding
150 method. This default affects only Wide_Text_IO where no explicit
151 coding method is given, and there is no particular reason to let
152 this default be affected by the source representation of a library
155 The value of main_priority is meaningful only when we are invoked
156 from the main program elaboration routine of an Ada application.
157 Checking the consistency of this parameter should therefore not be
158 done. Since it is assured that the main program elaboration will
159 always invoke this procedure before any library elaboration
160 routine, only the value of main_priority during the first call
161 should be taken into account and all the subsequent ones should be
162 ignored. Note that the case where the main program is not written
163 in Ada is also properly handled, since the default value will then
164 be used for this parameter.
166 For identical reasons, the consistency of time_slice_val should not
171 if (__gl_locking_policy
!= locking_policy
172 || __gl_queuing_policy
!= queuing_policy
173 || __gl_task_dispatching_policy
!= task_dispatching_policy
174 || __gl_unreserve_all_interrupts
!= unreserve_all_interrupts
175 || __gl_exception_tracebacks
!= exception_tracebacks
176 || __gl_zero_cost_exceptions
!= zero_cost_exceptions
)
177 __gnat_raise_program_error (__FILE__
, __LINE__
);
183 __gl_main_priority
= main_priority
;
184 __gl_time_slice_val
= time_slice_val
;
185 __gl_wc_encoding
= wc_encoding
;
186 __gl_locking_policy
= locking_policy
;
187 __gl_queuing_policy
= queuing_policy
;
188 __gl_restrictions
= restrictions
;
189 __gl_task_dispatching_policy
= task_dispatching_policy
;
190 __gl_unreserve_all_interrupts
= unreserve_all_interrupts
;
191 __gl_exception_tracebacks
= exception_tracebacks
;
193 /* ??? __gl_zero_cost_exceptions is new in 3.15 and is referenced from
194 a-except.adb, which is also part of the compiler sources. Since the
195 compiler is built with an older release of GNAT, the call generated by
196 the old binder to this function does not provide any value for the
197 corresponding argument, so the global has to be initialized in some
198 reasonable other way. This could be removed as soon as the next major
202 __gl_zero_cost_exceptions
= zero_cost_exceptions
;
204 __gl_zero_cost_exceptions
= 0;
205 /* We never build the compiler to run in ZCX mode currently anyway. */
209 /*********************/
210 /* __gnat_initialize */
211 /*********************/
213 /* __gnat_initialize is called at the start of execution of an Ada program
214 (the call is generated by the binder). The standard routine does nothing
215 at all; the intention is that this be replaced by system specific
216 code where initialization is required. */
218 /***********************************/
219 /* __gnat_initialize (AIX version) */
220 /***********************************/
224 /* AiX doesn't have SA_NODEFER */
228 #include <sys/time.h>
230 /* AiX doesn't have nanosleep, but provides nsleep instead */
232 extern int nanosleep
PARAMS ((struct timestruc_t
*, struct timestruc_t
*));
233 static void __gnat_error_handler
PARAMS ((int));
236 nanosleep (Rqtp
, Rmtp
)
237 struct timestruc_t
*Rqtp
, *Rmtp
;
239 return nsleep (Rqtp
, Rmtp
);
245 __gnat_error_handler (sig
)
248 struct Exception_Data
*exception
;
254 /* FIXME: we need to detect the case of a *real* SIGSEGV */
255 exception
= &storage_error
;
256 msg
= "stack overflow or erroneous memory access";
260 exception
= &constraint_error
;
265 exception
= &constraint_error
;
270 exception
= &program_error
;
271 msg
= "unhandled signal";
274 Raise_From_Signal_Handler (exception
, msg
);
278 __gnat_install_handler ()
280 struct sigaction act
;
282 /* Set up signal handler to map synchronous signals to appropriate
283 exceptions. Make sure that the handler isn't interrupted by another
284 signal that might cause a scheduling event! */
286 act
.sa_handler
= __gnat_error_handler
;
287 act
.sa_flags
= SA_NODEFER
| SA_RESTART
;
288 (void) sigemptyset (&act
.sa_mask
);
290 (void) sigaction (SIGABRT
, &act
, NULL
);
291 (void) sigaction (SIGFPE
, &act
, NULL
);
292 (void) sigaction (SIGILL
, &act
, NULL
);
293 (void) sigaction (SIGSEGV
, &act
, NULL
);
294 (void) sigaction (SIGBUS
, &act
, NULL
);
295 __gnat_handler_installed
= 1;
303 /****************************************/
304 /* __gnat_initialize (Dec Unix version) */
305 /****************************************/
307 #elif defined(__alpha__) && defined(__osf__) && ! defined(__alpha_vxworks)
309 /* Note: it seems that __osf__ is defined for the Alpha VXWorks case. Not
310 clear that this is reasonable, but in any case we have to be sure to
311 exclude this case in the above test. */
314 #include <sys/siginfo.h>
316 static void __gnat_error_handler
PARAMS ((int, siginfo_t
*,
317 struct sigcontext
*));
318 extern char *__gnat_get_code_loc
PARAMS ((struct sigcontext
*));
319 extern void __gnat_enter_handler
PARAMS ((struct sigcontext
*, char *));
320 extern size_t __gnat_machine_state_length
PARAMS ((void));
322 extern long exc_lookup_gp
PARAMS ((char *));
323 extern void exc_resume
PARAMS ((struct sigcontext
*));
326 __gnat_error_handler (sig
, sip
, context
)
329 struct sigcontext
*context
;
331 struct Exception_Data
*exception
;
332 static int recurse
= 0;
333 struct sigcontext
*mstate
;
336 /* If this was an explicit signal from a "kill", just resignal it. */
337 if (SI_FROMUSER (sip
))
339 signal (sig
, SIG_DFL
);
340 kill (getpid(), sig
);
343 /* Otherwise, treat it as something we handle. */
347 /* If the problem was permissions, this is a constraint error.
348 Likewise if the failing address isn't maximally aligned or if
351 ??? Using a static variable here isn't task-safe, but it's
352 much too hard to do anything else and we're just determining
353 which exception to raise. */
354 if (sip
->si_code
== SEGV_ACCERR
355 || (((long) sip
->si_addr
) & 3) != 0
358 exception
= &constraint_error
;
363 /* See if the page before the faulting page is accessible. Do that
364 by trying to access it. We'd like to simply try to access
365 4096 + the faulting address, but it's not guaranteed to be
366 the actual address, just to be on the same page. */
369 ((long) sip
->si_addr
& - getpagesize ()))[getpagesize ()];
370 msg
= "stack overflow (or erroneous memory access)";
371 exception
= &storage_error
;
376 exception
= &program_error
;
381 exception
= &constraint_error
;
386 exception
= &program_error
;
387 msg
= "unhandled signal";
391 mstate
= (struct sigcontext
*) (*Get_Machine_State_Addr
) ();
395 Raise_From_Signal_Handler (exception
, (char *) msg
);
399 __gnat_install_handler ()
401 struct sigaction act
;
403 /* Setup signal handler to map synchronous signals to appropriate
404 exceptions. Make sure that the handler isn't interrupted by another
405 signal that might cause a scheduling event! */
407 act
.sa_handler
= (void (*) PARAMS ((int))) __gnat_error_handler
;
408 act
.sa_flags
= SA_ONSTACK
| SA_RESTART
| SA_NODEFER
| SA_SIGINFO
;
409 (void) sigemptyset (&act
.sa_mask
);
411 (void) sigaction (SIGABRT
, &act
, NULL
);
412 (void) sigaction (SIGFPE
, &act
, NULL
);
413 (void) sigaction (SIGILL
, &act
, NULL
);
414 (void) sigaction (SIGSEGV
, &act
, NULL
);
415 (void) sigaction (SIGBUS
, &act
, NULL
);
417 __gnat_handler_installed
= 1;
425 /* Routines called by 5amastop.adb. */
430 __gnat_get_code_loc (context
)
431 struct sigcontext
*context
;
433 return (char *) context
->sc_pc
;
437 __gnat_enter_handler (context
, pc
)
438 struct sigcontext
*context
;
441 context
->sc_pc
= (long) pc
;
442 context
->sc_regs
[SC_GP
] = exc_lookup_gp (pc
);
443 exc_resume (context
);
447 __gnat_machine_state_length ()
449 return sizeof (struct sigcontext
);
452 /***********************************/
453 /* __gnat_initialize (HPUX version) */
454 /***********************************/
460 static void __gnat_error_handler
PARAMS ((int));
463 __gnat_error_handler (sig
)
466 struct Exception_Data
*exception
;
472 /* FIXME: we need to detect the case of a *real* SIGSEGV */
473 exception
= &storage_error
;
474 msg
= "stack overflow or erroneous memory access";
478 exception
= &constraint_error
;
483 exception
= &constraint_error
;
488 exception
= &program_error
;
489 msg
= "unhandled signal";
492 Raise_From_Signal_Handler (exception
, msg
);
496 __gnat_install_handler ()
498 struct sigaction act
;
500 /* Set up signal handler to map synchronous signals to appropriate
501 exceptions. Make sure that the handler isn't interrupted by another
502 signal that might cause a scheduling event! Also setup an alternate
503 stack region for the handler execution so that stack overflows can be
504 handled properly, avoiding a SEGV generation from stack usage by the
507 static char handler_stack
[SIGSTKSZ
*2];
508 /* SIGSTKSZ appeared to be "short" for the needs in some contexts
509 (e.g. experiments with GCC ZCX exceptions). */
513 stack
.ss_sp
= handler_stack
;
514 stack
.ss_size
= sizeof (handler_stack
);
517 (void) sigaltstack (&stack
, NULL
);
519 act
.sa_handler
= __gnat_error_handler
;
520 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_ONSTACK
;
521 (void) sigemptyset (&act
.sa_mask
);
523 (void) sigaction (SIGABRT
, &act
, NULL
);
524 (void) sigaction (SIGFPE
, &act
, NULL
);
525 (void) sigaction (SIGILL
, &act
, NULL
);
526 (void) sigaction (SIGSEGV
, &act
, NULL
);
527 (void) sigaction (SIGBUS
, &act
, NULL
);
529 __gnat_handler_installed
= 1;
537 /*************************************/
538 /* __gnat_initialize (GNU/Linux version) */
539 /*************************************/
541 #elif defined (linux) && defined (i386) && !defined (__RT__)
544 #include <asm/sigcontext.h>
546 /* GNU/Linux, which uses glibc, does not define NULL in included
550 #define NULL ((void *) 0)
563 static void __gnat_error_handler
PARAMS ((int));
566 __gnat_error_handler (sig
)
569 struct Exception_Data
*exception
;
571 static int recurse
= 0;
573 struct sigcontext
*info
574 = (struct sigcontext
*) (((char *) &sig
) + sizeof (int));
576 /* The Linux kernel does not document how to get the machine state in a
577 signal handler, but in fact the necessary data is in a sigcontext_struct
578 value that is on the stack immediately above the signal number
579 parameter, and the above messing accesses this value on the stack. */
581 struct Machine_State
*mstate
;
586 /* If the problem was permissions, this is a constraint error.
587 Likewise if the failing address isn't maximally aligned or if
590 ??? Using a static variable here isn't task-safe, but it's
591 much too hard to do anything else and we're just determining
592 which exception to raise. */
595 exception
= &constraint_error
;
600 /* Here we would like a discrimination test to see whether the
601 page before the faulting address is accessible. Unfortunately
602 Linux seems to have no way of giving us the faulting address.
604 In versions of a-init.c before 1.95, we had a test of the page
605 before the stack pointer using:
609 ((long) info->esp_at_signal & - getpagesize ()))[getpagesize ()];
611 but that's wrong, since it tests the stack pointer location, and
612 the current stack probe code does not move the stack pointer
613 until all probes succeed.
615 For now we simply do not attempt any discrimination at all. Note
616 that this is quite acceptable, since a "real" SIGSEGV can only
617 occur as the result of an erroneous program */
619 msg
= "stack overflow (or erroneous memory access)";
620 exception
= &storage_error
;
625 exception
= &constraint_error
;
630 exception
= &constraint_error
;
635 exception
= &program_error
;
636 msg
= "unhandled signal";
639 mstate
= (*Get_Machine_State_Addr
)();
642 mstate
->eip
= info
->eip
;
643 mstate
->ebx
= info
->ebx
;
644 mstate
->esp
= info
->esp_at_signal
;
645 mstate
->ebp
= info
->ebp
;
646 mstate
->esi
= info
->esi
;
647 mstate
->edi
= info
->edi
;
651 Raise_From_Signal_Handler (exception
, msg
);
655 __gnat_install_handler ()
657 struct sigaction act
;
659 /* Set up signal handler to map synchronous signals to appropriate
660 exceptions. Make sure that the handler isn't interrupted by another
661 signal that might cause a scheduling event! */
663 act
.sa_handler
= __gnat_error_handler
;
664 act
.sa_flags
= SA_NODEFER
| SA_RESTART
;
665 (void) sigemptyset (&act
.sa_mask
);
667 (void) sigaction (SIGABRT
, &act
, NULL
);
668 (void) sigaction (SIGFPE
, &act
, NULL
);
669 (void) sigaction (SIGILL
, &act
, NULL
);
670 (void) sigaction (SIGSEGV
, &act
, NULL
);
671 (void) sigaction (SIGBUS
, &act
, NULL
);
673 __gnat_handler_installed
= 1;
681 /******************************************/
682 /* __gnat_initialize (NT-mingw32 version) */
683 /******************************************/
685 #elif defined (__MINGW32__)
688 static LONG __gnat_error_handler
PARAMS ((PEXCEPTION_POINTERS
));
690 /* __gnat_initialize (mingw32). */
693 __gnat_error_handler (info
)
694 PEXCEPTION_POINTERS info
;
697 struct Exception_Data
*exception
;
700 switch (info
->ExceptionRecord
->ExceptionCode
)
702 case EXCEPTION_ACCESS_VIOLATION
:
703 /* If the failing address isn't maximally-aligned or if we've
704 recursed, this is a program error. */
705 if ((info
->ExceptionRecord
->ExceptionInformation
[1] & 3) != 0
708 exception
= &program_error
;
709 msg
= "EXCEPTION_ACCESS_VIOLATION";
713 /* See if the page before the faulting page is accessible. Do that
714 by trying to access it. */
716 * ((volatile char *) (info
->ExceptionRecord
->ExceptionInformation
[1]
718 exception
= &storage_error
;
719 msg
= "stack overflow (or erroneous memory access)";
723 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
724 exception
= &constraint_error
;
725 msg
= "EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
728 case EXCEPTION_DATATYPE_MISALIGNMENT
:
729 exception
= &constraint_error
;
730 msg
= "EXCEPTION_DATATYPE_MISALIGNMENT";
733 case EXCEPTION_FLT_DENORMAL_OPERAND
:
734 exception
= &constraint_error
;
735 msg
= "EXCEPTION_FLT_DENORMAL_OPERAND";
738 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
739 exception
= &constraint_error
;
740 msg
= "EXCEPTION_FLT_DENORMAL_OPERAND";
743 case EXCEPTION_FLT_INVALID_OPERATION
:
744 exception
= &constraint_error
;
745 msg
= "EXCEPTION_FLT_INVALID_OPERATION";
748 case EXCEPTION_FLT_OVERFLOW
:
749 exception
= &constraint_error
;
750 msg
= "EXCEPTION_FLT_OVERFLOW";
753 case EXCEPTION_FLT_STACK_CHECK
:
754 exception
= &program_error
;
755 msg
= "EXCEPTION_FLT_STACK_CHECK";
758 case EXCEPTION_FLT_UNDERFLOW
:
759 exception
= &constraint_error
;
760 msg
= "EXCEPTION_FLT_UNDERFLOW";
763 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
764 exception
= &constraint_error
;
765 msg
= "EXCEPTION_INT_DIVIDE_BY_ZERO";
768 case EXCEPTION_INT_OVERFLOW
:
769 exception
= &constraint_error
;
770 msg
= "EXCEPTION_INT_OVERFLOW";
773 case EXCEPTION_INVALID_DISPOSITION
:
774 exception
= &program_error
;
775 msg
= "EXCEPTION_INVALID_DISPOSITION";
778 case EXCEPTION_NONCONTINUABLE_EXCEPTION
:
779 exception
= &program_error
;
780 msg
= "EXCEPTION_NONCONTINUABLE_EXCEPTION";
783 case EXCEPTION_PRIV_INSTRUCTION
:
784 exception
= &program_error
;
785 msg
= "EXCEPTION_PRIV_INSTRUCTION";
788 case EXCEPTION_SINGLE_STEP
:
789 exception
= &program_error
;
790 msg
= "EXCEPTION_SINGLE_STEP";
793 case EXCEPTION_STACK_OVERFLOW
:
794 exception
= &storage_error
;
795 msg
= "EXCEPTION_STACK_OVERFLOW";
799 exception
= &program_error
;
800 msg
= "unhandled signal";
804 Raise_From_Signal_Handler (exception
, msg
);
808 __gnat_install_handler ()
810 SetUnhandledExceptionFilter (__gnat_error_handler
);
811 __gnat_handler_installed
= 1;
818 /* Initialize floating-point coprocessor. This call is needed because
819 the MS libraries default to 64-bit precision instead of 80-bit
820 precision, and we require the full precision for proper operation,
821 given that we have set Max_Digits etc with this in mind */
823 __gnat_init_float ();
825 /* initialize a lock for a process handle list - see a-adaint.c for the
826 implementation of __gnat_portable_no_block_spawn, __gnat_portable_wait */
830 /**************************************/
831 /* __gnat_initialize (Interix version) */
832 /**************************************/
834 #elif defined (__INTERIX)
838 static void __gnat_error_handler
PARAMS ((int));
841 __gnat_error_handler (sig
)
844 struct Exception_Data
*exception
;
850 exception
= &storage_error
;
851 msg
= "stack overflow or erroneous memory access";
855 exception
= &constraint_error
;
860 exception
= &constraint_error
;
865 exception
= &program_error
;
866 msg
= "unhandled signal";
869 Raise_From_Signal_Handler (exception
, msg
);
873 __gnat_install_handler ()
875 struct sigaction act
;
877 /* Set up signal handler to map synchronous signals to appropriate
878 exceptions. Make sure that the handler isn't interrupted by another
879 signal that might cause a scheduling event! */
881 act
.sa_handler
= __gnat_error_handler
;
883 (void) sigemptyset (&act
.sa_mask
);
885 /* Handlers for signals besides SIGSEGV cause c974013 to hang */
886 /* (void) sigaction (SIGILL, &act, NULL); */
887 /* (void) sigaction (SIGABRT, &act, NULL); */
888 /* (void) sigaction (SIGFPE, &act, NULL); */
889 /* (void) sigaction (SIGBUS, &act, NULL); */
891 (void) sigaction (SIGSEGV
, &act
, NULL
);
893 __gnat_handler_installed
= 1;
899 __gnat_init_float ();
902 /**************************************/
903 /* __gnat_initialize (LynxOS version) */
904 /**************************************/
906 #elif defined (__Lynx__)
911 __gnat_init_float ();
914 /*********************************/
915 /* __gnat_install_handler (Lynx) */
916 /*********************************/
919 __gnat_install_handler ()
921 __gnat_handler_installed
= 1;
924 /****************************/
925 /* __gnat_initialize (OS/2) */
926 /****************************/
928 #elif defined (__EMX__) /* OS/2 dependent initialization */
935 /*********************************/
936 /* __gnat_install_handler (OS/2) */
937 /*********************************/
940 __gnat_install_handler ()
942 __gnat_handler_installed
= 1;
945 /***********************************/
946 /* __gnat_initialize (SGI version) */
947 /***********************************/
958 #define SIGADAABORT 48
959 #define SIGNAL_STACK_SIZE 4096
960 #define SIGNAL_STACK_ALIGNMENT 64
964 sigcontext_t context
;
967 static void __gnat_error_handler
PARAMS ((int, int, sigcontext_t
*));
970 __gnat_error_handler (sig
, code
, sc
)
975 struct Machine_State
*mstate
;
976 struct Exception_Data
*exception
;
986 exception
= &program_error
;
987 msg
= "SIGSEGV: (Invalid virtual address)";
989 else if (code
== ENXIO
)
991 exception
= &program_error
;
992 msg
= "SIGSEGV: (Read beyond mapped object)";
994 else if (code
== ENOSPC
)
996 exception
= &program_error
; /* ??? storage_error ??? */
997 msg
= "SIGSEGV: (Autogrow for file failed)";
999 else if (code
== EACCES
)
1001 /* ??? Re-add smarts to further verify that we launched
1002 the stack into a guard page, not an attempt to
1003 write to .text or something */
1004 exception
= &storage_error
;
1005 msg
= "SIGSEGV: (stack overflow or erroneous memory access)";
1009 /* Just in case the OS guys did it to us again. Sometimes
1010 they fail to document all of the valid codes that are
1011 passed to signal handlers, just in case someone depends
1012 on knowing all the codes */
1013 exception
= &program_error
;
1014 msg
= "SIGSEGV: (Undocumented reason)";
1019 /* Map all bus errors to Program_Error. */
1020 exception
= &program_error
;
1025 /* Map all fpe errors to Constraint_Error. */
1026 exception
= &constraint_error
;
1031 if ((*Check_Abort_Status
) ())
1033 exception
= &_abort_signal
;
1042 /* Everything else is a Program_Error. */
1043 exception
= &program_error
;
1044 msg
= "unhandled signal";
1047 mstate
= (*Get_Machine_State_Addr
)();
1049 memcpy ((void *) mstate
, (const void *) sc
, sizeof (sigcontext_t
));
1051 Raise_From_Signal_Handler (exception
, msg
);
1056 __gnat_install_handler ()
1059 struct sigaction act
;
1061 /* Setup signal handler to map synchronous signals to appropriate
1062 exceptions. Make sure that the handler isn't interrupted by another
1063 signal that might cause a scheduling event! */
1065 act
.sa_handler
= __gnat_error_handler
;
1066 act
.sa_flags
= SA_NODEFER
+ SA_RESTART
;
1067 (void) sigfillset (&act
.sa_mask
);
1068 (void) sigemptyset (&act
.sa_mask
);
1070 (void) sigaction (SIGABRT
, &act
, NULL
);
1071 (void) sigaction (SIGFPE
, &act
, NULL
);
1072 (void) sigaction (SIGILL
, &act
, NULL
);
1073 (void) sigaction (SIGSEGV
, &act
, NULL
);
1074 (void) sigaction (SIGBUS
, &act
, NULL
);
1075 (void) sigaction (SIGADAABORT
, &act
, NULL
);
1076 __gnat_handler_installed
= 1;
1080 __gnat_initialize ()
1084 /*************************************************/
1085 /* __gnat_initialize (Solaris and SunOS version) */
1086 /*************************************************/
1088 #elif defined (sun) && defined (__SVR4) && !defined (__vxworks)
1091 #include <siginfo.h>
1093 static void __gnat_error_handler
PARAMS ((int, siginfo_t
*));
1096 __gnat_error_handler (sig
, sip
)
1100 struct Exception_Data
*exception
;
1101 static int recurse
= 0;
1104 /* If this was an explicit signal from a "kill", just resignal it. */
1105 if (SI_FROMUSER (sip
))
1107 signal (sig
, SIG_DFL
);
1108 kill (getpid(), sig
);
1111 /* Otherwise, treat it as something we handle. */
1115 /* If the problem was permissions, this is a constraint error.
1116 Likewise if the failing address isn't maximally aligned or if
1119 ??? Using a static variable here isn't task-safe, but it's
1120 much too hard to do anything else and we're just determining
1121 which exception to raise. */
1122 if (sip
->si_code
== SEGV_ACCERR
1123 || (((long) sip
->si_addr
) & 3) != 0
1126 exception
= &constraint_error
;
1131 /* See if the page before the faulting page is accessible. Do that
1132 by trying to access it. We'd like to simply try to access
1133 4096 + the faulting address, but it's not guaranteed to be
1134 the actual address, just to be on the same page. */
1137 ((long) sip
->si_addr
& - getpagesize ()))[getpagesize ()];
1138 exception
= &storage_error
;
1139 msg
= "stack overflow (or erroneous memory access)";
1144 exception
= &program_error
;
1149 exception
= &constraint_error
;
1154 exception
= &program_error
;
1155 msg
= "unhandled signal";
1160 Raise_From_Signal_Handler (exception
, msg
);
1164 __gnat_install_handler ()
1166 struct sigaction act
;
1168 /* Set up signal handler to map synchronous signals to appropriate
1169 exceptions. Make sure that the handler isn't interrupted by another
1170 signal that might cause a scheduling event! */
1172 act
.sa_handler
= __gnat_error_handler
;
1173 act
.sa_flags
= SA_NODEFER
| SA_RESTART
| SA_SIGINFO
;
1174 (void) sigemptyset (&act
.sa_mask
);
1176 (void) sigaction (SIGABRT
, &act
, NULL
);
1177 (void) sigaction (SIGFPE
, &act
, NULL
);
1178 (void) sigaction (SIGSEGV
, &act
, NULL
);
1179 (void) sigaction (SIGBUS
, &act
, NULL
);
1181 __gnat_handler_installed
= 1;
1185 __gnat_initialize ()
1189 /***********************************/
1190 /* __gnat_initialize (SNI version) */
1191 /***********************************/
1193 #elif defined (__sni__)
1195 /* SNI needs special defines and includes */
1197 #define _XOPEN_SOURCE
1198 #define _POSIX_SOURCE
1201 extern size_t __gnat_getpagesize
PARAMS ((void));
1202 static void __gnat_error_handler
PARAMS ((int));
1204 /* The run time needs this function which is a #define in SNI */
1207 __gnat_getpagesize ()
1209 return getpagesize ();
1213 __gnat_error_handler (sig
)
1216 struct Exception_Data
*exception
;
1222 /* FIXME: we need to detect the case of a *real* SIGSEGV */
1223 exception
= &storage_error
;
1224 msg
= "stack overflow or erroneous memory access";
1228 exception
= &constraint_error
;
1233 exception
= &constraint_error
;
1238 exception
= &program_error
;
1239 msg
= "unhandled signal";
1242 Raise_From_Signal_Handler (exception
, msg
);
1246 __gnat_install_handler ()
1248 struct sigaction act
;
1250 /* Set up signal handler to map synchronous signals to appropriate
1251 exceptions. Make sure that the handler isn't interrupted by another
1252 signal that might cause a scheduling event! */
1254 act
.sa_handler
= __gnat_error_handler
;
1255 act
.sa_flags
= SA_NODEFER
| SA_RESTART
;
1256 (void) sigemptyset (&act
.sa_mask
);
1258 (void) sigaction (SIGABRT
, &act
, NULL
);
1259 (void) sigaction (SIGFPE
, &act
, NULL
);
1260 (void) sigaction (SIGILL
, &act
, NULL
);
1261 (void) sigaction (SIGSEGV
, &act
, NULL
);
1262 (void) sigaction (SIGBUS
, &act
, NULL
);
1264 __gnat_handler_installed
= 1;
1268 __gnat_initialize ()
1272 /***********************************/
1273 /* __gnat_initialize (VMS version) */
1274 /***********************************/
1278 /* The prehandler actually gets control first on a condition. It swaps the
1279 stack pointer and calls the handler (__gnat_error_handler). */
1280 extern long __gnat_error_prehandler ();
1282 extern char *__gnat_error_prehandler_stack
; /* Alternate signal stack */
1284 /* Conditions that don't have an Ada exception counterpart must raise
1285 Non_Ada_Error. Since this is defined in s-auxdec, it should only be
1286 referenced by user programs, not the compiler or tools. Hence the
1290 #define Non_Ada_Error system__aux_dec__non_ada_error
1291 extern struct Exception_Data Non_Ada_Error
;
1293 #define Coded_Exception system__vms_exception_table__coded_exception
1294 extern struct Exception_Data
*Coded_Exception (int);
1297 /* Define macro symbols for the VMS conditions that become Ada exceptions.
1298 Most of these are also defined in the header file ssdef.h which has not
1299 yet been converted to be recoginized by Gnu C. Some, which couldn't be
1300 located, are assigned names based on the DEC test suite tests which
1303 #define SS$_ACCVIO 12
1304 #define SS$_DEBUG 1132
1305 #define SS$_INTDIV 1156
1306 #define SS$_HPARITH 1284
1307 #define SS$_STKOVF 1364
1308 #define SS$_RESIGNAL 2328
1309 #define MTH$_FLOOVEMAT 1475268 /* Some ACVC_21 CXA tests */
1310 #define SS$_CE24VRU 3253636 /* Write to unopened file */
1311 #define SS$_C980VTE 3246436 /* AST requests time slice */
1312 #define CMA$_EXIT_THREAD 4227492
1313 #define CMA$_EXCCOPLOS 4228108
1314 #define CMA$_ALERTED 4227460
1316 struct descriptor_s
{unsigned short len
, mbz
; char *adr
; };
1318 long __gnat_error_handler
PARAMS ((int *, void *));
1321 __gnat_error_handler (sigargs
, mechargs
)
1325 struct Exception_Data
*exception
= 0;
1329 struct descriptor_s msgdesc
;
1330 int msg_flag
= 0x000f; /* 1 bit for each of the four message parts */
1331 unsigned short outlen
;
1333 long curr_invo_handle
;
1336 /* Resignaled conditions aren't effected by by pragma Import_Exception */
1341 case CMA$_EXIT_THREAD
:
1342 return SS$_RESIGNAL
;
1344 case SS$_DEBUG
: /* Gdb attach, resignal to merge activate gdbstub. */
1345 return SS$_RESIGNAL
;
1347 case 1409786: /* Nickerson bug #33 ??? */
1348 return SS$_RESIGNAL
;
1350 case 1381050: /* Nickerson bug #33 ??? */
1351 return SS$_RESIGNAL
;
1353 case 11829410: /* Resignalled as Use_Error for CE10VRC */
1354 return SS$_RESIGNAL
;
1359 /* See if it's an imported exception. Mask off severity bits. */
1360 exception
= Coded_Exception (sigargs
[1] & 0xfffffff8);
1365 msgdesc
.adr
= message
;
1366 SYS$
GETMSG (sigargs
[1], &outlen
, &msgdesc
, msg_flag
, 0);
1367 message
[outlen
] = 0;
1370 exception
->Name_Length
= 19;
1371 /* The full name really should be get sys$getmsg returns. ??? */
1372 exception
->Full_Name
= "IMPORTED_EXCEPTION";
1373 exception
->Import_Code
= sigargs
[1] & 0xfffffff8;
1381 if (sigargs
[3] == 0)
1383 exception
= &constraint_error
;
1384 msg
= "access zero";
1388 exception
= &storage_error
;
1389 msg
= "stack overflow (or erroneous memory access)";
1394 exception
= &storage_error
;
1395 msg
= "stack overflow";
1399 exception
= &constraint_error
;
1400 msg
= "division by zero";
1405 return SS$_RESIGNAL
; /* toplev.c handles for compiler */
1408 exception
= &constraint_error
;
1409 msg
= "arithmetic error";
1414 case MTH$_FLOOVEMAT
:
1415 exception
= &constraint_error
;
1416 msg
= "floating overflow in math library";
1420 exception
= &constraint_error
;
1425 exception
= &program_error
;
1431 exception
= &program_error
;
1433 /* User programs expect Non_Ada_Error to be raised, reference
1434 DEC Ada test CXCONDHAN. */
1435 exception
= &Non_Ada_Error
;
1439 msgdesc
.adr
= message
;
1440 SYS$
GETMSG (sigargs
[1], &outlen
, &msgdesc
, msg_flag
, 0);
1441 message
[outlen
] = 0;
1446 mstate
= (long *) (*Get_Machine_State_Addr
) ();
1449 LIB$
GET_CURR_INVO_CONTEXT (&curr_icb
);
1450 LIB$
GET_PREV_INVO_CONTEXT (&curr_icb
);
1451 LIB$
GET_PREV_INVO_CONTEXT (&curr_icb
);
1452 curr_invo_handle
= LIB$
GET_INVO_HANDLE (&curr_icb
);
1453 *mstate
= curr_invo_handle
;
1455 Raise_From_Signal_Handler (exception
, msg
);
1459 __gnat_install_handler ()
1464 c
= (char *) xmalloc (2049);
1466 __gnat_error_prehandler_stack
= &c
[2048];
1468 /* __gnat_error_prehandler is an assembly function. */
1469 SYS$
SETEXV (1, __gnat_error_prehandler
, 3, &prvhnd
);
1470 __gnat_handler_installed
= 1;
1478 /***************************************/
1479 /* __gnat_initialize (VXWorks version) */
1480 /***************************************/
1482 #elif defined(__vxworks)
1485 #include <taskLib.h>
1489 static void __gnat_init_handler
PARAMS ((int));
1490 extern int __gnat_inum_to_ivec
PARAMS ((int));
1491 static void __gnat_error_handler
PARAMS ((int, int, struct sigcontext
*));
1494 __gnat_int_handler (interr
)
1497 /* Note that we should use something like Raise_From_Int_Handler here, but
1498 for now Raise_From_Signal_Handler will do the job. ??? */
1500 Raise_From_Signal_Handler (&storage_error
, "stack overflow");
1503 /* Used for stack-checking on VxWorks. Must be task-local in
1506 void *__gnat_stack_limit
= NULL
;
1508 #ifndef __alpha_vxworks
1510 /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
1513 extern long getpid
PARAMS ((void));
1518 return taskIdSelf ();
1522 /* This is needed by the GNAT run time to handle Vxworks interrupts */
1524 __gnat_inum_to_ivec (num
)
1527 return INUM_TO_IVEC (num
);
1531 __gnat_error_handler (sig
, code
, sc
)
1534 struct sigcontext
*sc
;
1536 struct Exception_Data
*exception
;
1541 /* VxWorks will always mask out the signal during the signal handler and
1542 will reenable it on a longjmp. GNAT does not generate a longjmp to
1543 return from a signal handler so the signal will still be masked unless
1545 (void) sigprocmask (SIG_SETMASK
, NULL
, &mask
);
1546 sigdelset (&mask
, sig
);
1547 (void) sigprocmask (SIG_SETMASK
, &mask
, NULL
);
1549 /* VxWorks will suspend the task when it gets a hardware exception. We
1550 take the liberty of resuming the task for the application. */
1551 if (taskIsSuspended (taskIdSelf ()) != 0)
1552 (void) taskResume (taskIdSelf ());
1557 exception
= &constraint_error
;
1561 exception
= &constraint_error
;
1565 exception
= &program_error
;
1569 exception
= &program_error
;
1573 exception
= &program_error
;
1574 msg
= "unhandled signal";
1577 Raise_From_Signal_Handler (exception
, msg
);
1581 __gnat_install_handler ()
1583 struct sigaction act
;
1585 /* Setup signal handler to map synchronous signals to appropriate
1586 exceptions. Make sure that the handler isn't interrupted by another
1587 signal that might cause a scheduling event! */
1589 act
.sa_handler
= __gnat_error_handler
;
1590 act
.sa_flags
= SA_SIGINFO
| SA_ONSTACK
;
1591 (void) sigemptyset (&act
.sa_mask
);
1593 (void) sigaction (SIGFPE
, &act
, NULL
);
1594 (void) sigaction (SIGILL
, &act
, NULL
);
1595 (void) sigaction (SIGSEGV
, &act
, NULL
);
1596 (void) sigaction (SIGBUS
, &act
, NULL
);
1598 __gnat_handler_installed
= 1;
1601 #define HAVE_GNAT_INIT_FLOAT
1604 __gnat_init_float ()
1606 #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT)
1607 /* Disable overflow/underflow exceptions on the PPC processor, this is needed
1608 to get correct Ada semantic */
1615 __gnat_initialize ()
1617 TASK_DESC pTaskDesc
;
1619 if (taskInfoGet (taskIdSelf (), &pTaskDesc
) != OK
)
1620 printErr ("Cannot get task info");
1622 __gnat_stack_limit
= (void *) pTaskDesc
.td_pStackLimit
;
1624 __gnat_init_float ();
1628 /***************************************/
1629 /* __gnat_initialize (RTEMS version) */
1630 /***************************************/
1632 #elif defined(__rtems__)
1634 extern void __gnat_install_handler ();
1636 /* For RTEMS, each bsp will provide a custom __gnat_install_handler (). */
1639 __gnat_initialize ()
1641 __gnat_install_handler ();
1646 /* For all other versions of GNAT, the initialize routine and handler
1647 installation do nothing */
1649 /***************************************/
1650 /* __gnat_initialize (default version) */
1651 /***************************************/
1654 __gnat_initialize ()
1658 /********************************************/
1659 /* __gnat_install_handler (default version) */
1660 /********************************************/
1663 __gnat_install_handler ()
1665 __gnat_handler_installed
= 1;
1670 /*********************/
1671 /* __gnat_init_float */
1672 /*********************/
1674 /* This routine is called as each process thread is created, for possible
1675 initialization of the FP processor. This version is used under INTERIX,
1676 WIN32 and could be used under OS/2 */
1678 #if defined (_WIN32) || defined (__INTERIX) || defined (__EMX__) \
1679 || defined (__Lynx__)
1681 #define HAVE_GNAT_INIT_FLOAT
1684 __gnat_init_float ()
1686 #if defined (__i386__) || defined (i386)
1688 /* This is used to properly initialize the FPU on an x86 for each
1693 #endif /* Defined __i386__ */
1697 #ifndef HAVE_GNAT_INIT_FLOAT
1699 /* All targets without a specific __gnat_init_float will use an empty one */
1701 __gnat_init_float ()