home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / a-init.c < prev    next >
C/C++ Source or Header  |  2000-07-19  |  47KB  |  1,775 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*                         GNAT COMPILER COMPONENTS                         */
  4. /*                                                                          */
  5. /*                               A - I N I T                                */
  6. /*                                                                          */
  7. /*                            $Revision: 1.155-3.13a $
  8. /*                                                                          */
  9. /*                          C Implementation File                           */
  10. /*                                                                          */
  11. /*          Copyright (C) 1992-2000 Free Software Foundation, Inc.          */
  12. /*                                                                          */
  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.                                                      */
  23. /*                                                                          */
  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.                        */
  29. /*                                                                          */
  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). */
  32. /*                                                                          */
  33. /****************************************************************************/
  34.  
  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) */
  43.  
  44. /* The following include is here to meet the published VxWorks requirement
  45.    that the __vxworks header appear before any other include. */
  46. #ifdef __vxworks
  47. #include "vxWorks.h"
  48. #endif
  49.  
  50. #include "config.h"
  51. #include "a-ada.h"
  52. #include "a-types.h"
  53. #include "a-raise.h"
  54. #include <errno.h>
  55.  
  56. /* Addresses of exception data blocks for predefined exceptions. */
  57. extern struct Exception_Data constraint_error;
  58. extern struct Exception_Data numeric_error;
  59. extern struct Exception_Data program_error;
  60. extern struct Exception_Data storage_error;
  61. extern struct Exception_Data tasking_error;
  62. extern struct Exception_Data _abort_signal;
  63.  
  64. #define Lock_Task system__soft_links__lock_task
  65. extern void (*Lock_Task) (void);
  66.  
  67. #define Unlock_Task system__soft_links__unlock_task
  68. extern void (*Unlock_Task) (void);
  69.  
  70. #define Get_Machine_State_Addr \
  71.                       system__soft_links__get_machine_state_addr
  72. extern struct Machine_State *(*Get_Machine_State_Addr) (void);
  73.  
  74. #define Check_Abort_Status     \
  75.                       system__soft_links__check_abort_status
  76. extern int    (*Check_Abort_Status) (void);
  77.  
  78. #define Raise_From_Signal_Handler \
  79.                       ada__exceptions__raise_from_signal_handler
  80. extern void   Raise_From_Signal_Handler (struct Exception_Data *, char *);
  81.  
  82. #define Propagate_Signal_Exception \
  83.                       __gnat_propagate_sig_exc
  84. extern void   Propagate_Signal_Exception
  85.         (struct Machine_State *, struct Exception_Data *, char *);
  86.  
  87.  
  88. /* Copies of global values computed by the binder */
  89. int  __gl_main_priority            = -1;
  90. int  __gl_time_slice_val           = -1;
  91. char __gl_wc_encoding              = 'n';
  92. char __gl_locking_policy           = ' ';
  93. char __gl_queuing_policy           = ' ';
  94. char __gl_task_dispatching_policy  = ' ';
  95. void (*__gl_adafinal_ptr) ()       = 0;
  96. int  __gl_unreserve_all_interrupts = 0;
  97. int  __gl_exception_tracebacks     = 0;
  98.  
  99. /* HAVE_GNAT_INIT_FLOAT must be set on every targets where a __gnat_init_float
  100.    is defined. If this is not set them a void implementation will be defined
  101.    at the end of this unit. */
  102. #undef HAVE_GNAT_INIT_FLOAT
  103.  
  104. /**********************/
  105. /* __gnat_set_globals */
  106. /**********************/
  107.  
  108. /* This routine is called from the binder generated main program.  It copies
  109.    the values for global quantities computed by the binder into the following
  110.    global locations. The reason that we go through this copy, rather than just
  111.    define the global locations in the binder generated file, is that they are
  112.    referenced from the runtime, which may be in a shared library, and the
  113.    binder file is not in the shared library. Global references across library
  114.    boundaries like this are not handled correctly in all systems.  */
  115.  
  116. void
  117. __gnat_set_globals (main_priority,
  118.                     time_slice_val,
  119.                     wc_encoding,
  120.                     locking_policy,
  121.             queuing_policy,
  122.             task_dispatching_policy,
  123.             adafinal_ptr,
  124.             unreserve_all_interrupts,
  125.                     exception_tracebacks)
  126.      int main_priority;
  127.      int time_slice_val;
  128.      char wc_encoding;
  129.      char locking_policy, queuing_policy, task_dispatching_policy;
  130.      void (*adafinal_ptr) ();
  131.      int unreserve_all_interrupts, exception_tracebacks;
  132. {
  133.   __gl_main_priority            = main_priority;
  134.   __gl_time_slice_val           = time_slice_val;
  135.   __gl_wc_encoding              = wc_encoding;
  136.   __gl_locking_policy           = locking_policy;
  137.   __gl_queuing_policy           = queuing_policy;
  138.   __gl_task_dispatching_policy  = task_dispatching_policy;
  139.   __gl_adafinal_ptr             = adafinal_ptr;
  140.   __gl_unreserve_all_interrupts = unreserve_all_interrupts;
  141.   __gl_exception_tracebacks     = exception_tracebacks;
  142. }
  143.  
  144. /*********************/
  145. /* __gnat_initialize */
  146. /*********************/
  147.  
  148. /* __gnat_initialize is called at the start of execution of an Ada program
  149.    (the call is generated by the binder). The standard routine does nothing
  150.    at all; the intention is that this be replaced by system specific
  151.    code where initialization is required. */
  152.  
  153. /***********************************/
  154. /* __gnat_initialize (AIX version) */
  155. /***********************************/
  156.  
  157. #if defined (_AIX)
  158.  
  159. /* AiX doesn't have SA_NODEFER */
  160.  
  161. #define SA_NODEFER 0
  162.  
  163. #include <sys/time.h>
  164.  
  165. /* AiX doesn't have nanosleep, but provides nsleep instead */
  166.  
  167. int
  168. nanosleep (Rqtp, Rmtp)
  169.      struct timestruc_t *Rqtp, *Rmtp;
  170. {
  171.   return nsleep (Rqtp, Rmtp);
  172. }
  173.  
  174. #include <signal.h>
  175.  
  176. static void
  177. __gnat_error_handler (sig)
  178.      int sig;
  179. {
  180.   struct Exception_Data *exception;
  181.   char *msg;
  182.  
  183.   switch (sig)
  184.     {
  185.     case SIGSEGV:
  186.       /* FIXME: we need to detect the case of a *real* SIGSEGV */
  187.       exception = &storage_error;
  188.       msg = "stack overflow or erroneous memory access";
  189.       break;
  190.  
  191.     case SIGBUS:
  192.       exception = &constraint_error;
  193.       msg = "SIGBUS";
  194.       break;
  195.  
  196.     case SIGFPE:
  197.       exception = &constraint_error;
  198.       msg = "SIGFPE";
  199.       break;
  200.  
  201.     default:
  202.       exception = &program_error;
  203.       msg = "unhandled signal";
  204.     }
  205.  
  206.   Raise_From_Signal_Handler (exception, msg);
  207. }
  208.  
  209. static void
  210. __gnat_install_handler ()
  211. {
  212.   struct sigaction act;
  213.  
  214.   /* Set up signal handler to map synchronous signals to appropriate
  215.      exceptions.  Make sure that the handler isn't interrupted by another
  216.      signal that might cause a scheduling event! */
  217.  
  218.   act.sa_handler = __gnat_error_handler;
  219.   act.sa_flags = SA_NODEFER | SA_RESTART;
  220.   (void) sigemptyset (&act.sa_mask);
  221.  
  222.   (void) sigaction (SIGILL,  &act, NULL);
  223.   (void) sigaction (SIGABRT, &act, NULL);
  224.   (void) sigaction (SIGFPE,  &act, NULL);
  225.   (void) sigaction (SIGSEGV, &act, NULL);
  226.   (void) sigaction (SIGBUS,  &act, NULL);
  227. }
  228.  
  229. void
  230. __gnat_initialize ()
  231. {
  232.    __gnat_install_handler ();
  233. }
  234.  
  235. /****************************************/
  236. /* __gnat_initialize (Dec Unix version) */
  237. /****************************************/
  238.  
  239. #elif defined(__alpha__) && defined(__osf__) && ! defined(__alpha_vxworks)
  240. /* note: it seems that __alpha__ and __osf__ are both defined for the
  241.    Alpha VXWorks case. Not clear that this is reasonable, but in any case
  242.    we have to be sure to exclude this case in the above test */
  243.  
  244. #include <signal.h>
  245. #include <sys/siginfo.h>
  246. #include <sys/types.h>
  247.  
  248. static void
  249. __gnat_error_handler (sig, sip, context)
  250.      int sig;
  251.      siginfo_t *sip;
  252.      struct sigcontext *context;
  253. {
  254.   struct Exception_Data *exception;
  255.   static int recurse = 0;
  256.   struct sigcontext *mstate;
  257.   char *msg;
  258.  
  259.   /* If this was an explicit signal from a "kill", just resignal it.  */
  260.   if (SI_FROMUSER (sip))
  261.     {
  262.       signal (sig, SIG_DFL);
  263.       kill (getpid(), sig);
  264.     }
  265.  
  266.   /* Otherwise, treat it as something we handle.  */
  267.   switch (sig)
  268.     {
  269.     case SIGSEGV:
  270.       /* If the problem was permissions, this is a constraint error.
  271.      Likewise if the failing address isn't maximally aligned or if
  272.      we've recursed.
  273.  
  274.      ??? Using a static variable here isn't task-safe, but it's
  275.      much too hard to do anything else and we're just determining
  276.      which exception to raise.  */
  277.       if (sip->si_code == SEGV_ACCERR
  278.       || (((long) sip->si_addr) & 3) != 0
  279.       || recurse)
  280.     {
  281.       exception = &constraint_error;
  282.       msg = "SIGSEGV";
  283.     }
  284.       else
  285.     {
  286.       /* See if the page before the faulting page is accessable.  Do that
  287.          by trying to access it.  We'd like to simply try to access
  288.          4096 + the faulting address, but it's not guaranteed to be
  289.          the actual address, just to be on the same page.  */
  290.       recurse++;
  291.       ((volatile char *)
  292.        ((long) sip->si_addr & - getpagesize ()))[getpagesize ()];
  293.       msg = "stack overflow (or erroneous memory access)";
  294.       exception = &storage_error;
  295.     }
  296.       break;
  297.  
  298.     case SIGBUS:
  299.       exception = &program_error;
  300.       msg = "SIGBUS";
  301.       break;
  302.  
  303.     case SIGFPE:
  304.       exception = &constraint_error;
  305.       msg = "SIGFPE";
  306.       break;
  307.  
  308.     default:
  309.       exception = &program_error;
  310.       msg = "unhandled signal";
  311.     }
  312.  
  313.   recurse = 0;
  314.   mstate = (struct sigcontext *) (*Get_Machine_State_Addr) ();
  315.   if (mstate != 0)
  316.     *mstate = *context;
  317.  
  318.   Raise_From_Signal_Handler (exception, msg);
  319. }
  320.  
  321. static
  322. __gnat_install_handler ()
  323. {
  324.   struct sigaction act;
  325.  
  326.   /* Setup signal handler to map synchronous signals to appropriate
  327.      exceptions. Make sure that the handler isn't interrupted by another
  328.      signal that might cause a scheduling event! */
  329.  
  330.   act.sa_handler = __gnat_error_handler;
  331.   act.sa_flags = SA_ONSTACK | SA_RESTART | SA_NODEFER | SA_SIGINFO;
  332.   (void) sigemptyset (&act.sa_mask);
  333.  
  334.   (void) sigaction (SIGILL,  &act, NULL);
  335.   (void) sigaction (SIGABRT, &act, NULL);
  336.   (void) sigaction (SIGFPE,  &act, NULL);
  337.   (void) sigaction (SIGSEGV, &act, NULL);
  338.   (void) sigaction (SIGBUS,  &act, NULL);
  339. }
  340.  
  341. void
  342. __gnat_initialize ()
  343. {
  344.   __gnat_install_handler ();
  345. }
  346.  
  347. /* Routines called by 5amastop.adb.  */
  348.  
  349. #include <machine/reg.h>
  350.  
  351. char *
  352. __gnat_get_code_loc (context)
  353.      struct sigcontext *context;
  354. {
  355.   return (char *) context->sc_pc;
  356. }
  357.  
  358. void
  359. __gnat_enter_handler (context, pc)
  360.      struct sigcontext *context;
  361.      char *pc;
  362. {
  363.   context->sc_pc = (long) pc;
  364.   context->sc_regs[EF_GP] = exc_lookup_gp (pc);
  365.   exc_resume (context);
  366. }
  367.  
  368. size_t
  369. __gnat_machine_state_length ()
  370. {
  371.   return sizeof (struct sigcontext);
  372. }
  373.  
  374. /***********************************/
  375. /* __gnat_initialize (HPUX version) */
  376. /***********************************/
  377.  
  378. #elif defined (hpux)
  379.  
  380. #include <signal.h>
  381.  
  382. static void
  383. __gnat_error_handler (sig)
  384.      int sig;
  385. {
  386.   struct Exception_Data *exception;
  387.   char *msg;
  388.  
  389.   switch (sig)
  390.     {
  391.     case SIGSEGV:
  392.       /* FIXME: we need to detect the case of a *real* SIGSEGV */
  393.       exception = &storage_error;
  394.       msg = "stack overflow or erroneous memory access";
  395.       break;
  396.  
  397.     case SIGBUS:
  398.       exception = &constraint_error;
  399.       msg = "SIGBUS";
  400.       break;
  401.  
  402.     case SIGFPE:
  403.       exception = &constraint_error;
  404.       msg = "SIGFPE";
  405.       break;
  406.  
  407.     default:
  408.       exception = &program_error;
  409.       msg = "unhandled signal";
  410.     }
  411.  
  412.   Raise_From_Signal_Handler (exception, msg);
  413. }
  414.  
  415. static void
  416. __gnat_install_handler ()
  417. {
  418.   struct sigaction act;
  419.  
  420.   /* Set up signal handler to map synchronous signals to appropriate
  421.      exceptions.  Make sure that the handler isn't interrupted by another
  422.      signal that might cause a scheduling event! */
  423.  
  424.   act.sa_handler = __gnat_error_handler;
  425.   act.sa_flags = SA_NODEFER | SA_RESTART;
  426.   (void) sigemptyset (&act.sa_mask);
  427.  
  428.   (void) sigaction (SIGILL,  &act, NULL);
  429.   (void) sigaction (SIGABRT, &act, NULL);
  430.   (void) sigaction (SIGFPE,  &act, NULL);
  431.   (void) sigaction (SIGSEGV, &act, NULL);
  432.   (void) sigaction (SIGBUS,  &act, NULL);
  433. }
  434.  
  435. void
  436. __gnat_initialize ()
  437. {
  438.    __gnat_install_handler ();
  439. }
  440.  
  441.  
  442. /*************************************/
  443. /* __gnat_initialize (Linux version) */
  444. /*************************************/
  445.  
  446. #elif defined (linux) && defined (i386) && !defined (__RT__)
  447.  
  448. #include <signal.h>
  449. #include <asm/sigcontext.h>
  450.  
  451. /* Linux with GNU libc does not define NULL in included header files */
  452.  
  453. #if !defined (NULL)
  454. #define NULL ((void *) 0)
  455. #endif
  456.  
  457. struct Machine_State {
  458.   unsigned long eip;
  459.   unsigned long ebx;
  460.   unsigned long esp;
  461.   unsigned long ebp;
  462.   unsigned long esi;
  463.   unsigned long edi;
  464. };
  465.  
  466. static void
  467. __gnat_error_handler (int sig)
  468. {
  469.   struct Exception_Data *exception;
  470.   char *msg;
  471.   static int recurse = 0;
  472.  
  473.   struct sigcontext_struct *info
  474.     = (struct sigcontext_struct *) (((char *) &sig) + sizeof (int));
  475.   /* Linux does not document how to get the machine state in a signal handler,
  476.      but in fact the necessary data is in a sigcontext_struct value that is
  477.      on the stack immediately above the signal number parameter, and the
  478.      above messing accesses this value on the stack. */
  479.  
  480.   struct Machine_State *mstate;
  481.  
  482.   switch (sig)
  483.     {
  484.     case SIGSEGV:
  485.       /* If the problem was permissions, this is a constraint error.
  486.        Likewise if the failing address isn't maximally aligned or if
  487.        we've recursed.
  488.  
  489.        ??? Using a static variable here isn't task-safe, but it's
  490.        much too hard to do anything else and we're just determining
  491.        which exception to raise.  */
  492.       if (recurse)
  493.       {
  494.         exception = &constraint_error;
  495.         msg = "SIGSEGV";
  496.       }
  497.       else
  498.       {
  499.         /* Here we would like a discrimination test to see whether the
  500.            page before the faulting address is accessible. Unfortunately
  501.            Linux seems to have no way of giving us the faulting address.
  502.  
  503.            In versions of a-init.c before 1.95, we had a test of the page
  504.            before the stack pointer using:
  505.  
  506.             recurse++;
  507.              ((volatile char *)
  508.               ((long) info->esp_at_signal & - getpagesize ()))[getpagesize ()];
  509.  
  510.            but that's wrong, since it tests the stack pointer location, and
  511.            the current stack probe code does not move the stack pointer
  512.            until all probes succeed.
  513.  
  514.            For now we simply do not attempt any discrimination at all. Note
  515.            that this is quite acceptable, since a "real" SIGSEGV can only
  516.            occur as the result of an erroneous program */
  517.  
  518.         msg = "stack overflow (or erroneous memory access)";
  519.         exception = &storage_error;
  520.       }
  521.       break;
  522.  
  523.     case SIGBUS:
  524.       exception = &constraint_error;
  525.       msg = "SIGBUS";
  526.       break;
  527.  
  528.     case SIGFPE:
  529.       exception = &constraint_error;
  530.       msg = "SIGFPE";
  531.       break;
  532.  
  533.     default:
  534.       exception = &program_error;
  535.       msg = "unhandled signal";
  536.     }
  537.  
  538.   mstate = (*Get_Machine_State_Addr)();
  539.   if (mstate)
  540.     {
  541.       mstate->eip = info->eip;
  542.       mstate->ebx = info->ebx;
  543.       mstate->esp = info->esp_at_signal;
  544.       mstate->ebp = info->ebp;
  545.       mstate->esi = info->esi;
  546.       mstate->edi = info->edi;
  547.     }
  548.  
  549.   recurse = 0;
  550.   Raise_From_Signal_Handler (exception, msg);
  551. }
  552.  
  553. static void
  554. __gnat_install_handler ()
  555. {
  556.   struct sigaction act;
  557.  
  558.   /* Set up signal handler to map synchronous signals to appropriate
  559.      exceptions.  Make sure that the handler isn't interrupted by another
  560.      signal that might cause a scheduling event! */
  561.  
  562.   act.sa_handler = __gnat_error_handler;
  563.   act.sa_flags = SA_NODEFER | SA_RESTART;
  564.   (void) sigemptyset (&act.sa_mask);
  565.  
  566.   (void) sigaction (SIGILL,  &act, NULL);
  567.   (void) sigaction (SIGABRT, &act, NULL);
  568.   (void) sigaction (SIGFPE,  &act, NULL);
  569.   (void) sigaction (SIGSEGV, &act, NULL);
  570.   (void) sigaction (SIGBUS,  &act, NULL);
  571. }
  572.  
  573. void
  574. __gnat_initialize ()
  575. {
  576.    __gnat_install_handler ();
  577. }
  578.  
  579. /******************************************/
  580. /* __gnat_initialize (NT-mingw32 version) */
  581. /******************************************/
  582.  
  583. #elif defined (__MINGW32__)
  584. #include <windows.h>
  585.  
  586. /* __gnat_initialize (mingw32).  */
  587.  
  588. static LONG
  589. __gnat_error_handler (info)
  590.      PEXCEPTION_POINTERS info;
  591. {
  592.   static int recurse;
  593.   struct Exception_Data *exception;
  594.   char *msg;
  595.  
  596.   switch (info->ExceptionRecord->ExceptionCode)
  597.     {
  598.     case EXCEPTION_ACCESS_VIOLATION:
  599.       /* If the failing address isn't maximally-aligned or if we've
  600.      recursed, this is a program error.  */
  601.       if ((info->ExceptionRecord->ExceptionInformation[1] & 3) != 0
  602.       || recurse)
  603.     {
  604.       exception = &program_error;
  605.       msg = "EXCEPTION_ACCESS_VIOLATION";
  606.     }
  607.       else
  608.     {
  609.       /* See if the page before the faulting page is accessable.  Do that
  610.          by trying to access it. */
  611.       recurse++;
  612.       * ((volatile char *) (info->ExceptionRecord->ExceptionInformation[1]
  613.                 + 4096));
  614.       exception = &storage_error;
  615.       msg = "stack overflow (or erroneous memory access)";
  616.     }
  617.       break;
  618.  
  619.     case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
  620.       exception = &constraint_error;
  621.       msg = "EXCEPTION_ARRAY_BOUNDS_EXCEEDED";
  622.       break;
  623.  
  624.     case EXCEPTION_DATATYPE_MISALIGNMENT:
  625.       exception = &constraint_error;
  626.       msg = "EXCEPTION_DATATYPE_MISALIGNMENT";
  627.       break;
  628.  
  629.     case EXCEPTION_FLT_DENORMAL_OPERAND:
  630.       exception = &constraint_error;
  631.       msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
  632.       break;
  633.  
  634.     case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  635.       exception = &constraint_error;
  636.       msg = "EXCEPTION_FLT_DENORMAL_OPERAND";
  637.       break;
  638.  
  639.     case EXCEPTION_FLT_INVALID_OPERATION:
  640.       exception = &constraint_error;
  641.       msg = "EXCEPTION_FLT_INVALID_OPERATION";
  642.       break;
  643.  
  644.     case EXCEPTION_FLT_OVERFLOW:
  645.       exception = &constraint_error;
  646.       msg = "EXCEPTION_FLT_OVERFLOW";
  647.       break;
  648.  
  649.     case EXCEPTION_FLT_STACK_CHECK:
  650.       exception = &program_error;
  651.       msg = "EXCEPTION_FLT_STACK_CHECK";
  652.       break;
  653.  
  654.     case EXCEPTION_FLT_UNDERFLOW:
  655.       exception = &constraint_error;
  656.       msg = "EXCEPTION_FLT_UNDERFLOW";
  657.       break;
  658.  
  659.     case EXCEPTION_INT_DIVIDE_BY_ZERO:
  660.       exception = &constraint_error;
  661.       msg = "EXCEPTION_INT_DIVIDE_BY_ZERO";
  662.       break;
  663.  
  664.     case EXCEPTION_INT_OVERFLOW:
  665.       exception = &constraint_error;
  666.       msg = "EXCEPTION_INT_OVERFLOW";
  667.       break;
  668.  
  669.     case EXCEPTION_INVALID_DISPOSITION:
  670.       exception = &program_error;
  671.       msg = "EXCEPTION_INVALID_DISPOSITION";
  672.       break;
  673.  
  674.     case EXCEPTION_NONCONTINUABLE_EXCEPTION:
  675.       exception = &program_error;
  676.       msg = "EXCEPTION_NONCONTINUABLE_EXCEPTION";
  677.       break;
  678.  
  679.     case EXCEPTION_PRIV_INSTRUCTION:
  680.       exception = &program_error;
  681.       msg = "EXCEPTION_PRIV_INSTRUCTION";
  682.       break;
  683.  
  684.     case EXCEPTION_SINGLE_STEP:
  685.       exception = &program_error;
  686.       msg = "EXCEPTION_SINGLE_STEP";
  687.       break;
  688.  
  689.     case EXCEPTION_STACK_OVERFLOW:
  690.       exception = &storage_error;
  691.       msg = "EXCEPTION_STACK_OVERFLOW";
  692.       break;
  693.  
  694.    default:
  695.       exception = &program_error;
  696.       msg = "unhandled signal";
  697.     }
  698.  
  699.   recurse = 0;
  700.   Raise_From_Signal_Handler (exception, msg);
  701. }
  702.  
  703. static void
  704. __gnat_install_handler ()
  705. {
  706.  
  707.   SetUnhandledExceptionFilter (__gnat_error_handler);
  708. }
  709.  
  710. void __gnat_init_float ();
  711.  
  712. void
  713. __gnat_initialize ()
  714. {
  715.    __gnat_install_handler ();
  716.  
  717.    /* Initialize floating-point coprocessor. This call is needed because
  718.       the MS libraries default to 64-bit precision instead of 80-bit
  719.       precision, and we require the full precision for proper operation,
  720.       given that we have set Max_Digits etc with this in mind */
  721.  
  722.    __gnat_init_float ();
  723.  
  724.    /* initialize a lock for a process handle list - see a-adaint.c for the
  725.       implementation of portable_no_block_spawn, portable_wait */
  726.    __plist_init();
  727. }
  728.  
  729. /**************************************/
  730. /* __gnat_initialize (OpenNT version) */
  731. /**************************************/
  732.  
  733. #elif defined (__INTERIX)
  734.  
  735. void __gnat_init_float ();
  736.  
  737. void
  738. __gnat_initialize ()
  739. {
  740.    __gnat_init_float ();
  741. }
  742.  
  743. /**************************************/
  744. /* __gnat_initialize (LynxOS version) */
  745. /**************************************/
  746.  
  747. #elif defined (__Lynx__)
  748.  
  749. void __gnat_init_float ();
  750.  
  751. void
  752. __gnat_initialize ()
  753. {
  754.    __gnat_init_float ();
  755. }
  756.  
  757. /****************************/
  758. /* __gnat_initialize (OS/2) */
  759. /****************************/
  760.  
  761. #elif defined (__EMX__) /* OS/2 dependent initialization */
  762.  
  763. void __gnat_init_float ();
  764.  
  765. void
  766. __gnat_initialize ()
  767. {
  768. }
  769.  
  770. /***********************************/
  771. /* __gnat_initialize (SGI version) */
  772. /***********************************/
  773.  
  774. #elif defined (sgi)
  775.  
  776. #include <signal.h>
  777. #include <siginfo.h>
  778.  
  779. #ifndef NULL
  780. #define NULL 0
  781. #endif
  782.  
  783. #define SIGADAABORT 48
  784. #define SIGNAL_STACK_SIZE 4096
  785. #define SIGNAL_STACK_ALIGNMENT 64
  786.  
  787. struct Machine_State {
  788.    sigcontext_t context;
  789. };
  790.  
  791. static void
  792. __gnat_error_handler (sig, code, sc)
  793.      int sig;
  794.      int code;
  795.      sigcontext_t *sc;
  796. {
  797.   struct Machine_State  *mstate;
  798.   struct Exception_Data *exception;
  799.   char *msg;
  800.  
  801.   int i;
  802.  
  803.   switch (sig)
  804.     {
  805.     case SIGSEGV:
  806.       if (code == EFAULT)
  807.     {
  808.       exception = &program_error;
  809.       msg = "SIGSEGV: (Invalid virtual address)";
  810.     }
  811.       else if (code == ENXIO)
  812.     {
  813.       exception = &program_error;
  814.       msg = "SIGSEGV: (Read beyond mapped object)";
  815.     }
  816.       else if (code == ENOSPC)
  817.     {
  818.       exception = &program_error; /* ??? storage_error ??? */
  819.       msg = "SIGSEGV: (Autogrow for file failed)";
  820.     }
  821.       else if (code == EACCES)
  822.     {
  823.       /* ??? Re-add smarts to further verify that we launched
  824.              the stack into a guard page, not an attempt to
  825.          write to .text or something */
  826.       exception = &storage_error;
  827.       msg = "SIGSEGV: (stack overflow or erroneous memory access)";
  828.     }
  829.       else
  830.     {
  831.       /* Just in case the OS guys did it to us again.  Sometimes
  832.          they fail to document all of the valid codes that are
  833.          passed to signal handlers, just in case someone depends
  834.          on knowing all the codes */
  835.       exception = &program_error;
  836.       msg = "SIGSEGV: (Undocumented reason)";
  837.     }
  838.       break;
  839.  
  840.     case SIGBUS:
  841.       /* Map all bus errors to Program_Error.  */
  842.       exception = &program_error;
  843.       msg = "SIGBUS";
  844.       break;
  845.  
  846.     case SIGFPE:
  847.       /* Map all fpe errors to Constraint_Error.  */
  848.       exception = &constraint_error;
  849.       msg = "SIGFPE";
  850.       break;
  851.  
  852.     case SIGADAABORT:
  853.       if ((*Check_Abort_Status) ())
  854.     {
  855.       exception = &_abort_signal;
  856.       msg = "";
  857.     }
  858.       else
  859.     return;
  860.  
  861.       break;
  862.  
  863.     default:
  864.       /* Everything else is a Program_Error. */
  865.       exception = &program_error;
  866.       msg = "unhandled signal";
  867.     }
  868.  
  869.   mstate = (*Get_Machine_State_Addr)();
  870.   if (mstate != 0)
  871.     bcopy ((const void *) sc, (void *) mstate, sizeof (sigcontext_t));
  872.  
  873.   Raise_From_Signal_Handler (exception, msg);
  874.  
  875. }
  876.  
  877. static void
  878. __gnat_install_handler ()
  879. {
  880.   stack_t ss;
  881.   struct sigaction act;
  882.  
  883.   /* Setup signal handler to map synchronous signals to appropriate
  884.      exceptions.  Make sure that the handler isn't interrupted by another
  885.      signal that might cause a scheduling event! */
  886.  
  887.   act.sa_handler = __gnat_error_handler;
  888.   act.sa_flags = SA_NODEFER + SA_RESTART;
  889.   (void) sigfillset (&act.sa_mask);
  890.   (void) sigemptyset (&act.sa_mask);
  891.  
  892.   (void) sigaction (SIGILL,  &act, NULL);
  893.   (void) sigaction (SIGABRT, &act, NULL);
  894.   (void) sigaction (SIGFPE,  &act, NULL);
  895.   (void) sigaction (SIGSEGV, &act, NULL);
  896.   (void) sigaction (SIGBUS,  &act, NULL);
  897.  
  898.   (void) sigaction (SIGADAABORT,  &act, NULL);
  899. }
  900.  
  901. void
  902. __gnat_initialize ()
  903. {
  904.   __gnat_install_handler ();
  905. }
  906.  
  907. /*************************************************/
  908. /* __gnat_initialize (Solaris and SunOS version) */
  909. /*************************************************/
  910.  
  911. #elif defined (sun) && defined (__SVR4)
  912.  
  913. #include <signal.h>
  914. #include <siginfo.h>
  915.  
  916. static void
  917. __gnat_error_handler (sig, sip)
  918.      int sig;
  919.      siginfo_t *sip;
  920. {
  921.   struct Exception_Data *exception;
  922.   static int recurse = 0;
  923.   char *msg;
  924.  
  925.   /* If this was an explicit signal from a "kill", just resignal it.  */
  926.   if (SI_FROMUSER (sip))
  927.     {
  928.       signal (sig, SIG_DFL);
  929.       kill (getpid(), sig);
  930.     }
  931.  
  932.   /* Otherwise, treat it as something we handle.  */
  933.   switch (sig)
  934.     {
  935.     case SIGSEGV:
  936.       /* If the problem was permissions, this is a constraint error.
  937.      Likewise if the failing address isn't maximally aligned or if
  938.      we've recursed.
  939.  
  940.      ??? Using a static variable here isn't task-safe, but it's
  941.      much too hard to do anything else and we're just determining
  942.      which exception to raise.  */
  943.       if (sip->si_code == SEGV_ACCERR
  944.       || (((long) sip->si_addr) & 3) != 0
  945.       || recurse)
  946.     {
  947.       exception = &constraint_error;
  948.       msg = "SIGSEGV";
  949.     }
  950.       else
  951.     {
  952.       /* See if the page before the faulting page is accessable.  Do that
  953.          by trying to access it.  We'd like to simply try to access
  954.          4096 + the faulting address, but it's not guaranteed to be
  955.          the actual address, just to be on the same page.  */
  956.       recurse++;
  957.       ((volatile char *)
  958.        ((long) sip->si_addr & - getpagesize ()))[getpagesize ()];
  959.       exception = &storage_error;
  960.       msg = "stack overflow (or erroneous memory access)";
  961.     }
  962.       break;
  963.  
  964.     case SIGBUS:
  965.       exception = &program_error;
  966.       msg = "SIGBUS";
  967.       break;
  968.  
  969.     case SIGFPE:
  970.       exception = &constraint_error;
  971.       msg = "SIGFPE";
  972.       break;
  973.  
  974.     default:
  975.       exception = &program_error;
  976.       msg = "unhandled signal";
  977.     }
  978.  
  979.   recurse = 0;
  980.  
  981.   Raise_From_Signal_Handler (exception, msg);
  982. }
  983.  
  984. static void
  985. __gnat_install_handler ()
  986. {
  987.   struct sigaction act;
  988.  
  989.   /* Set up signal handler to map synchronous signals to appropriate
  990.      exceptions.  Make sure that the handler isn't interrupted by another
  991.      signal that might cause a scheduling event! */
  992.  
  993.   act.sa_handler = __gnat_error_handler;
  994.   act.sa_flags = SA_NODEFER | SA_RESTART | SA_SIGINFO;
  995.   (void) sigemptyset (&act.sa_mask);
  996.  
  997.   (void) sigaction (SIGILL,  &act, NULL);
  998.   (void) sigaction (SIGABRT, &act, NULL);
  999.   (void) sigaction (SIGFPE,  &act, NULL);
  1000.   (void) sigaction (SIGSEGV, &act, NULL);
  1001.   (void) sigaction (SIGBUS,  &act, NULL);
  1002. }
  1003.  
  1004. void
  1005. __gnat_initialize ()
  1006. {
  1007.    __gnat_install_handler ();
  1008. }
  1009.  
  1010. /***********************************/
  1011. /* __gnat_initialize (SNI version) */
  1012. /***********************************/
  1013.  
  1014. #elif defined (__sni__)
  1015.  
  1016. /* SNI needs special defines and includes */
  1017.  
  1018. #define _XOPEN_SOURCE
  1019. #define _POSIX_SOURCE
  1020. #include <stdio.h>
  1021. #include <signal.h>
  1022. #include <unistd.h>
  1023.  
  1024. /* The run time needs this function which is a #define in SNI */
  1025.  
  1026. size_t __gnat_getpagesize(void)
  1027. {
  1028.   return getpagesize();
  1029. }
  1030.  
  1031. static void
  1032. __gnat_error_handler (sig)
  1033.      int sig;
  1034. {
  1035.   struct Exception_Data *exception;
  1036.   char *msg;
  1037.  
  1038.   switch (sig)
  1039.     {
  1040.     case SIGSEGV:
  1041.       /* FIXME: we need to detect the case of a *real* SIGSEGV */
  1042.       exception = &storage_error;
  1043.       msg = "stack overflow or erroneous memory access";
  1044.       break;
  1045.  
  1046.     case SIGBUS:
  1047.       exception = &constraint_error;
  1048.       msg = "SIGBUS";
  1049.       break;
  1050.  
  1051.     case SIGFPE:
  1052.       exception = &constraint_error;
  1053.       msg = "SIGFPE";
  1054.       break;
  1055.  
  1056.     default:
  1057.       exception = &program_error;
  1058.       msg = "unhandled signal";
  1059.     }
  1060.  
  1061.   Raise_From_Signal_Handler (exception, msg);
  1062. }
  1063.  
  1064. static void
  1065. __gnat_install_handler ()
  1066. {
  1067.   struct sigaction act;
  1068.  
  1069.   /* Set up signal handler to map synchronous signals to appropriate
  1070.      exceptions.  Make sure that the handler isn't interrupted by another
  1071.      signal that might cause a scheduling event! */
  1072.  
  1073.   act.sa_handler = __gnat_error_handler;
  1074.   act.sa_flags = SA_NODEFER | SA_RESTART;
  1075.   (void) sigemptyset (&act.sa_mask);
  1076.  
  1077.   (void) sigaction (SIGILL,  &act, NULL);
  1078.   (void) sigaction (SIGABRT, &act, NULL);
  1079.   (void) sigaction (SIGFPE,  &act, NULL);
  1080.   (void) sigaction (SIGSEGV, &act, NULL);
  1081.   (void) sigaction (SIGBUS,  &act, NULL);
  1082. }
  1083.  
  1084. void
  1085. __gnat_initialize ()
  1086. {
  1087.    __gnat_install_handler ();
  1088. }
  1089.  
  1090. /***********************************/
  1091. /* __gnat_initialize (VMS version) */
  1092. /***********************************/
  1093.  
  1094. #elif defined (VMS)
  1095.  
  1096. /* The prehandler actually gets control first on a condition. It swaps the
  1097.    stack pointer and calls the handler (__gnat_error_handler). */
  1098. extern long __gnat_error_prehandler ();
  1099.  
  1100. extern char *__gnat_error_prehandler_stack;   /* Alternate signal stack */
  1101.  
  1102. /* Conditions that don't have an Ada exception counterpart must raise
  1103.    Non_Ada_Error.  Since this is defined in s-auxdec, it should only be
  1104.    referenced by user programs, not the compiler or tools. Hence the
  1105.    #ifdef IN_GCC. */
  1106.  
  1107. #ifndef IN_GCC
  1108. #define Non_Ada_Error system__aux_dec__non_ada_error
  1109. extern struct Exception_Data Non_Ada_Error;
  1110.  
  1111. #define Coded_Exception system__vms_exception_table__coded_exception
  1112. extern struct Exception_Data *Coded_Exception (int);
  1113. #endif
  1114.  
  1115. #include <stdio.h>
  1116.  
  1117. /* Define macro symbols for the VMS conditions that become Ada exceptions.
  1118.    Most of these are also defined in the header file ssdef.h which has not
  1119.    yet been converted to be recoginized by Gnu C. Some, which couldn't be
  1120.    located, are assigned names based on the DEC test suite tests which
  1121.    raise them. */
  1122.  
  1123. #define SS$_ACCVIO            12
  1124. #define SS$_DEBUG           1132
  1125. #define SS$_INTDIV          1156
  1126. #define SS$_HPARITH         1284
  1127. #define SS$_STKOVF          1364
  1128. #define SS$_RESIGNAL        2328
  1129. #define MTH$_FLOOVEMAT   1475268       /* Some ACVC_21 CXA tests */
  1130. #define SS$_CE24VRU      3253636       /* Write to unopened file ??? */
  1131. #define SS$_C980VTE      3246436       /* AST requests time slice ??? */
  1132. #define CMA$_EXIT_THREAD 4227492
  1133. #define CMA$_EXCCOPLOS   4228108
  1134. #define CMA$_ALERTED     4227460
  1135.  
  1136. struct descriptor_s {unsigned short len, mbz; char *adr; };
  1137.  
  1138. long
  1139. __gnat_error_handler (sigargs, mechargs)
  1140.      int *sigargs;
  1141.      void *mechargs;
  1142. {
  1143.   struct Exception_Data *exception = 0;
  1144.   char *msg = "";
  1145.   char message [256];
  1146.   long prvhnd;
  1147.   struct descriptor_s msgdesc;
  1148.   int msg_flag = 0x000f; /* 1 bit for each of the four message parts */
  1149.   unsigned short outlen;
  1150.  
  1151.   /* Resignaled condtions aren't effected by by pragma Import_Exception */
  1152.  
  1153.   switch (sigargs[1])
  1154.   {
  1155.  
  1156.     case CMA$_EXIT_THREAD:
  1157.       return SS$_RESIGNAL;
  1158.  
  1159.     case SS$_DEBUG: /* Gdb attach, resignal to merge activate gdbstub. */
  1160.       return SS$_RESIGNAL;
  1161.  
  1162.     case 1409786: /* Nickerson bug #33 ??? */
  1163.       return SS$_RESIGNAL;
  1164.  
  1165.     case 1381050: /* Nickerson bug #33 ??? */
  1166.       return SS$_RESIGNAL;
  1167.  
  1168.     case 11829410: /* Resignalled as Use_Error for CE10VRC */
  1169.       return SS$_RESIGNAL;
  1170.  
  1171.   }
  1172.  
  1173. #ifndef IN_GCC
  1174.   /* See if it's an imported exception. Mask off severity bits. */
  1175.   exception = Coded_Exception (sigargs [1] & 0xfffffff8);
  1176.   if (exception)
  1177.     {
  1178.       msgdesc.len = 256;
  1179.       msgdesc.mbz = 0;
  1180.       msgdesc.adr = message;
  1181.       SYS$GETMSG (sigargs[1], &outlen, &msgdesc, msg_flag, 0);
  1182.       message [outlen] = 0;
  1183.       msg = message;
  1184.  
  1185.       exception->Name_Length = 19;
  1186.       /* The full name really should be get sys$getmsg returns. ??? */
  1187.       exception->Full_Name = "IMPORTED_EXCEPTION";
  1188.       exception->Import_Code = sigargs [1] & 0xfffffff8;
  1189.     }
  1190. #endif
  1191.  
  1192.   if (exception == 0)
  1193.     switch (sigargs[1])
  1194.       {
  1195.       case SS$_ACCVIO:
  1196.         if (sigargs[3] == 0)
  1197.       {
  1198.         exception = &constraint_error;
  1199.         msg = "access zero";
  1200.       }
  1201.     else
  1202.       {
  1203.         exception = &storage_error;
  1204.         msg = "stack overflow (or erroneous memory access)";
  1205.       }
  1206.     break;
  1207.  
  1208.       case SS$_STKOVF:
  1209.     exception = &storage_error;
  1210.     msg = "stack overflow";
  1211.     break;
  1212.  
  1213.       case SS$_INTDIV:
  1214.     exception = &constraint_error;
  1215.     msg = "division by zero";
  1216.     break;
  1217.  
  1218.       case SS$_HPARITH:
  1219. #ifdef IN_GCC
  1220.     return SS$_RESIGNAL; /* toplev.c handles for compiler... */
  1221. #else
  1222.     {
  1223.       exception = &constraint_error;
  1224.       msg = "arithmetic error";
  1225.     }
  1226. #endif
  1227.     break;
  1228.  
  1229.       case MTH$_FLOOVEMAT:
  1230.     exception = &constraint_error;
  1231.     msg = "floating overflow in math library";
  1232.     break;
  1233.  
  1234.       case SS$_CE24VRU:
  1235.     exception = &constraint_error;
  1236.     msg = "";
  1237.     break;
  1238.  
  1239.       case SS$_C980VTE:
  1240.     exception = &program_error;
  1241.     msg = "";
  1242.     break;
  1243.  
  1244.       default:
  1245. #ifdef IN_GCC
  1246.     exception = &program_error;
  1247. #else
  1248.     /* User programs expect Non_Ada_Error to be raised, reference
  1249.        DEC Ada test CXCONDHAN. */
  1250.     exception = &Non_Ada_Error;
  1251.     msgdesc.len = 256;
  1252.     msgdesc.mbz = 0;
  1253.     msgdesc.adr = message;
  1254.     SYS$GETMSG (sigargs[1], &outlen, &msgdesc, msg_flag, 0);
  1255.     message [outlen] = 0;
  1256.     msg = message;
  1257. #endif
  1258.     break;
  1259.       }
  1260.  
  1261.   Raise_From_Signal_Handler (exception, msg);
  1262. }
  1263.  
  1264. static void
  1265. install_handler ()
  1266. {
  1267.   long prvhnd;
  1268.   char *c;
  1269.  
  1270.   c = (char *) malloc (1025);
  1271.  
  1272.   __gnat_error_prehandler_stack = &c[1024];
  1273.  
  1274.   /* __gnat_error_prehandler is an assembly function.  */
  1275.   SYS$SETEXV (1, __gnat_error_prehandler, 3, &prvhnd);
  1276. }
  1277.  
  1278. void
  1279. __gnat_initialize()
  1280. {
  1281.   install_handler();
  1282. }
  1283.  
  1284. /***************************************/
  1285. /* __gnat_initialize (VXWorks version) */
  1286. /***************************************/
  1287.  
  1288. #elif defined(__vxworks)
  1289.  
  1290. #include <stdio.h>
  1291. #include <signal.h>
  1292. #include <taskLib.h>
  1293. #include <intLib.h>
  1294. #include <iv.h>
  1295.  
  1296. static void
  1297. __gnat_int_handler (interr)
  1298.       int interr;
  1299. {
  1300.   /*
  1301.    * Note that we should use something like Raise_From_Int_Handler here,
  1302.    * but for now Raise_From_Signal_Handler will do the job. ???
  1303.    */
  1304.  
  1305.   Raise_From_Signal_Handler (&storage_error, "stack overflow");
  1306. }
  1307.  
  1308. /* Used for stack-checking on VxWorks. Must be task-local in
  1309.    tasking programs */
  1310.  
  1311. void *__gnat_stack_limit = NULL;
  1312.  
  1313. #ifndef __alpha_vxworks
  1314. /* getpid is used by s-parint.adb, but is not defined by VxWorks, except
  1315.    on Alpha VxWorks */
  1316.  
  1317. long getpid (void)
  1318. {
  1319.   return taskIdSelf ();
  1320. }
  1321. #endif
  1322.  
  1323. /* This is needed by the GNAT run time to handle Vxworks interrupts */
  1324. int __gnat_inum_to_ivec (int num)
  1325. {
  1326.   return INUM_TO_IVEC (num);
  1327. }
  1328.  
  1329. static void
  1330. __gnat_error_handler (sig, code, sc)
  1331.      int sig;
  1332.      int code;
  1333.      struct sigcontext *sc;
  1334. {
  1335.   struct Exception_Data *exception;
  1336.   sigset_t mask;
  1337.   int result;
  1338.   char *msg;
  1339.  
  1340.   /*
  1341.    * VxWorks will always mask out the signal during the signal
  1342.    * handler and will reenable it on a longjmp.  GNAT does
  1343.    * not generate a longjmp to return from a signal handler
  1344.    * so the signal will still be masked unless we unmask it.
  1345.    */
  1346.   (void) sigprocmask (SIG_SETMASK, NULL, &mask);
  1347.   sigdelset (&mask, sig);
  1348.   (void) sigprocmask (SIG_SETMASK, &mask, NULL);
  1349.  
  1350.   /*
  1351.    * VxWorks will suspend the task when it gets a hardware
  1352.    * exception.  We take the liberty of resuming the task
  1353.    * for the application.
  1354.    */
  1355.   if (taskIsSuspended (taskIdSelf ()) != 0)
  1356.     (void) taskResume (taskIdSelf ());
  1357.  
  1358.   switch (sig)
  1359.     {
  1360.     case SIGFPE:
  1361.       exception = &constraint_error;
  1362.       msg = "SIGFPE";
  1363.       break;
  1364.     case SIGILL:
  1365.       exception = &constraint_error;
  1366.       msg = "SIGILL";
  1367.       break;
  1368.     case SIGSEGV:
  1369.       exception = &program_error;
  1370.       msg = "SIGSEGV";
  1371.       break;
  1372.     case SIGBUS:
  1373.       exception = &program_error;
  1374.       msg = "SIGBUS";
  1375.       break;
  1376.     default:
  1377.       exception = &program_error;
  1378.       msg = "unhandled signal";
  1379.     }
  1380.   Raise_From_Signal_Handler (exception, msg);
  1381. }
  1382.  
  1383. static void
  1384. __gnat_install_handler ()
  1385. {
  1386.   struct sigaction act;
  1387.  
  1388.   /* Setup signal handler to map synchronous signals to appropriate
  1389.      exceptions.  Make sure that the handler isn't interrupted by another
  1390.      signal that might cause a scheduling event! */
  1391.  
  1392.   act.sa_handler = __gnat_error_handler;
  1393.   act.sa_flags = SA_SIGINFO | SA_ONSTACK;
  1394.   (void) sigemptyset (&act.sa_mask);
  1395.  
  1396.   (void) sigaction (SIGILL,  &act, NULL);
  1397.   (void) sigaction (SIGFPE,  &act, NULL);
  1398.   (void) sigaction (SIGSEGV, &act, NULL);
  1399.   (void) sigaction (SIGBUS,  &act, NULL);
  1400. }
  1401.  
  1402. #define HAVE_GNAT_INIT_FLOAT
  1403.  
  1404. void
  1405. __gnat_init_float ()
  1406. {
  1407. #if defined (_ARCH_PPC) && !defined (_SOFT_FLOAT)
  1408.   /* Disable overflow/underflow exceptions on the PPC processor, this is needed
  1409.       to get correct Ada semantic */
  1410.   asm ("mtfsb0 25");
  1411.   asm ("mtfsb0 26");
  1412. #endif
  1413.  
  1414. }
  1415.  
  1416. void
  1417. __gnat_initialize ()
  1418. {
  1419.   TASK_DESC pTaskDesc;
  1420.   if (taskInfoGet(taskIdSelf(), &pTaskDesc) != OK) {
  1421.     printErr ("Cannot get task info");
  1422.   }
  1423.   __gnat_stack_limit = (void *) pTaskDesc.td_pStackLimit;
  1424.  
  1425.   __gnat_init_float ();
  1426.  
  1427. #ifdef __mips_vxworks
  1428. #if 0
  1429.   /* For now remove this handler, since it is causing interferences with gdb */
  1430.  
  1431.   /*
  1432.    * Connect the overflow trap directly to the __gnat_int_handler routine
  1433.    * as it is not converted to a signal by VxWorks.
  1434.    */
  1435.  
  1436.   intConnect (INUM_TO_IVEC (IV_TRAP_VEC), &__gnat_int_handler, IV_TRAP_VEC);
  1437. #endif
  1438. #endif
  1439.  
  1440.    __gnat_install_handler ();
  1441. }
  1442.  
  1443.  
  1444. /***************************************/
  1445. /* __gnat_initialize (default version) */
  1446. /***************************************/
  1447.  
  1448. #else
  1449.  
  1450. /* For all other versions of GNAT, the initialize routine does nothing.  */
  1451.  
  1452. void
  1453. __gnat_initialize ()
  1454. {
  1455. }
  1456.  
  1457. #endif
  1458.  
  1459.  
  1460. /*********************/
  1461. /* __gnat_init_float */
  1462. /*********************/
  1463.  
  1464. /* This routine is called as each process thread is created, for possible
  1465.    initialization of the FP processor. This version is used under INTERIX,
  1466.    WIN32 and could be used under OS/2 */
  1467.  
  1468. #if defined (_WIN32) || defined (__INTERIX) || defined (__EMX__) \
  1469.   || defined (__Lynx__)
  1470.  
  1471. #define HAVE_GNAT_INIT_FLOAT
  1472.  
  1473. void
  1474. __gnat_init_float ()
  1475. {
  1476. #if defined (__i386__) || defined (i386)
  1477.  
  1478.   /* This is used to properly initialize the FPU on an x86 for each
  1479.      process thread. */
  1480.  
  1481.   asm ("finit");
  1482.  
  1483. #endif  /* Defined __i386__ */
  1484. }
  1485. #endif
  1486.  
  1487. /* Get the stack unwinding mechanism when available and when compiling
  1488.    a-init.c for the run time. Except in the case of a restricted run-time,
  1489.    such as RT-Linux modules (__RT__ is defined). */
  1490.  
  1491. #if !defined (IN_GCC) && !defined (__RT__)
  1492. /* If we have a definition of INCOMING_RETURN_ADDR_RTX, assume that
  1493.    the rest of the DWARF 2 frame unwind support is also provided.  */
  1494. #if !defined (DWARF2_UNWIND_INFO) && defined (INCOMING_RETURN_ADDR_RTX)
  1495. #define DWARF2_UNWIND_INFO 1
  1496. #endif
  1497.  
  1498. #ifdef DWARF2_UNWIND_INFO
  1499. #ifndef _SIZE_T_DEFINED
  1500. #include <sys/types.h>
  1501. #endif
  1502. #include "frame.h"
  1503.  
  1504. struct machine_state {
  1505.   frame_state f1, f2, f3;
  1506.   frame_state *udata, *udata_start, *sub_udata;
  1507.   void *pc, *pc_start, *new_pc;
  1508. };
  1509.  
  1510. typedef int word_type __attribute__ ((mode (__word__)));
  1511.  
  1512. /* This type is used in get_reg and put_reg to deal with ABIs where a void*
  1513.    is smaller than a word, such as the Irix 6 n32 ABI.  We cast twice to
  1514.    avoid a warning about casting between int and pointer of different
  1515.    sizes.  */
  1516.  
  1517. typedef int ptr_type __attribute__ ((mode (pointer)));
  1518.  
  1519. /* Get the value of register REG as saved in UDATA, where SUB_UDATA is a
  1520.    frame called by UDATA or 0.  */
  1521.  
  1522. static void*
  1523. get_reg (unsigned reg, frame_state *udata, frame_state *sub_udata)
  1524. {
  1525.   if (udata->saved[reg] == REG_SAVED_OFFSET)
  1526.     return (void *)(ptr_type)
  1527.       *(word_type *)(udata->cfa + udata->reg_or_offset[reg]);
  1528.   else if (udata->saved[reg] == REG_SAVED_REG && sub_udata)
  1529.     return get_reg (udata->reg_or_offset[reg], sub_udata, 0);
  1530.   else
  1531.     abort ();
  1532. }
  1533.  
  1534. /* Overwrite the saved value for register REG in frame UDATA with VAL.  */
  1535.  
  1536. static void
  1537. put_reg (unsigned reg, void *val, frame_state *udata)
  1538. {
  1539.   if (udata->saved[reg] == REG_SAVED_OFFSET)
  1540.     *(word_type *)(udata->cfa + udata->reg_or_offset[reg])
  1541.       = (word_type)(ptr_type) val;
  1542.   else
  1543.     abort ();
  1544. }
  1545.  
  1546. /* Copy the saved value for register REG from frame UDATA to frame
  1547.    TARGET_UDATA.  Unlike the previous two functions, this can handle
  1548.    registers that are not one word large.  */
  1549.  
  1550. static void
  1551. copy_reg (unsigned reg, frame_state *udata, frame_state *target_udata)
  1552. {
  1553.   if (udata->saved[reg] == REG_SAVED_OFFSET
  1554.       && target_udata->saved[reg] == REG_SAVED_OFFSET)
  1555.     memcpy (target_udata->cfa + target_udata->reg_or_offset[reg],
  1556.             udata->cfa + udata->reg_or_offset[reg],
  1557.             __builtin_dwarf_reg_size (reg));
  1558.   else
  1559.     abort ();
  1560. }
  1561.  
  1562. /* Overwrite the return address for frame UDATA with VAL.  */
  1563.  
  1564. static inline void
  1565. put_return_addr (void *val, frame_state *udata)
  1566. {
  1567.   val = __builtin_frob_return_addr (val);
  1568.   put_reg (udata->retaddr_column, val, udata);
  1569. }
  1570.  
  1571. #ifdef INCOMING_REGNO
  1572. /* Is the saved value for register REG in frame UDATA stored in a register
  1573.    window in the previous frame?  */
  1574.  
  1575. static int
  1576. in_reg_window (int reg, frame_state *udata)
  1577. {
  1578.   if (udata->saved[reg] != REG_SAVED_OFFSET)
  1579.     return 0;
  1580.  
  1581. #ifdef STACK_GROWS_DOWNWARD
  1582.   return udata->reg_or_offset[reg] > 0;
  1583. #else
  1584.   return udata->reg_or_offset[reg] < 0;
  1585. #endif
  1586. }
  1587. #endif /* INCOMING_REGNO */
  1588.  
  1589. /* Retrieve the return address for frame UDATA, where SUB_UDATA is a
  1590.    frame called by UDATA or 0.  */
  1591.  
  1592. static inline void *
  1593. get_return_addr (frame_state *udata, frame_state *sub_udata)
  1594. {
  1595.   return __builtin_extract_return_addr
  1596.     (get_reg (udata->retaddr_column, udata, sub_udata));
  1597. }
  1598.  
  1599. /* Thread-safe version of __frame_state_for */
  1600.  
  1601. static struct frame_state *
  1602. __frame_state_for_r (void *pc_target, struct frame_state *state_in)
  1603. {
  1604.   struct frame_state *f;
  1605.  
  1606.   (*Lock_Task) ();
  1607.   f = __frame_state_for (pc_target, state_in);
  1608.   (*Unlock_Task) ();
  1609.   return f;
  1610. }
  1611.  
  1612. /* Given the current frame UDATA and its return address PC, return the
  1613.    information about the calling frame in CALLER_UDATA.  */
  1614.  
  1615. void
  1616. __gnat_pop_frame (m)
  1617.      struct machine_state *m;
  1618. {
  1619.   frame_state *p;
  1620.  
  1621.   int i;
  1622.  
  1623.   m->pc = m->new_pc;
  1624.   p = m->udata;
  1625.   if (! __frame_state_for_r (m->pc, m->sub_udata))
  1626.     {
  1627.       m->new_pc = 0;
  1628.       return;
  1629.     }
  1630.  
  1631.   /* Now go back to our caller's stack frame.  If our caller's CFA register
  1632.      was saved in our stack frame, restore it; otherwise, assume the CFA
  1633.      register is SP and restore it to our CFA value.  */
  1634.   if (m->udata->saved[m->sub_udata->cfa_reg])
  1635.     m->sub_udata->cfa = get_reg (m->sub_udata->cfa_reg, m->udata, 0);
  1636.   else
  1637.     m->sub_udata->cfa = m->udata->cfa;
  1638.   m->sub_udata->cfa += m->sub_udata->cfa_offset;
  1639.  
  1640.   m->udata = m->sub_udata;
  1641.   m->sub_udata = p;
  1642.   m->new_pc = get_return_addr (m->udata, m->sub_udata) - 1;
  1643.  
  1644.   return;
  1645.  
  1646. /* ??? disable this code for now since it doesn't work properly */
  1647. #if 0
  1648.   if (m->pc == m->pc_start)
  1649.     return;
  1650.  
  1651.   /* Copy the frame's saved register values into our register save slots.  */
  1652.  
  1653.   for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
  1654.     if (i != m->udata->retaddr_column && m->udata->saved[i])
  1655.       {
  1656. #ifdef INCOMING_REGNO
  1657.         /* If you modify the saved value of the return address
  1658.            register on the SPARC, you modify the return address for
  1659.            your caller's frame.  Don't do that here, as it will
  1660.            confuse get_return_addr.  */
  1661.         if (in_reg_window (i, m->udata)
  1662.             && m->udata->saved[m->udata->retaddr_column] == REG_SAVED_REG
  1663.             && m->udata->reg_or_offset[m->udata->retaddr_column] == i)
  1664.           continue;
  1665. #endif
  1666.         copy_reg (i, m->udata, m->udata_start);
  1667.       }
  1668. #endif
  1669. }
  1670.  
  1671. void
  1672. __gnat_set_machine_state (machine_state)
  1673.      struct machine_state *machine_state;
  1674. {
  1675.   frame_state sub_udata;
  1676.  
  1677.   /* Start at our stack frame.  */
  1678. label:
  1679.   machine_state->udata = &machine_state->f1;
  1680.   machine_state->sub_udata = &machine_state->f2;
  1681.   machine_state->udata_start = &machine_state->f3;
  1682.  
  1683.   if (! __frame_state_for_r (&&label, machine_state->udata))
  1684.     return;
  1685.  
  1686.   /* We need to get the value from the CFA register.  At this point in
  1687.      compiling libgnat.a we don't know whether or not we will use the frame
  1688.      pointer register for the CFA, so we check our unwind info.  */
  1689.   if (machine_state->udata->cfa_reg == __builtin_dwarf_fp_regnum ())
  1690.     machine_state->udata->cfa = __builtin_fp ();
  1691.   else
  1692.     machine_state->udata->cfa = __builtin_sp ();
  1693.   machine_state->udata->cfa += machine_state->udata->cfa_offset;
  1694.  
  1695.   memcpy (machine_state->udata_start, machine_state->udata,
  1696.     sizeof (frame_state));
  1697.   machine_state->new_pc =
  1698.   machine_state->pc_start =
  1699.   machine_state->pc = &&label;
  1700.  
  1701.   /* Do any necessary initialization to access arbitrary stack frames.
  1702.      On the SPARC, this means flushing the register windows.  */
  1703.   __builtin_unwind_init ();
  1704.  
  1705.   /* go up one frame */
  1706.   __gnat_pop_frame (machine_state);
  1707. }
  1708.  
  1709. void
  1710. __gnat_enter_handler (m, handler)
  1711.      struct machine_state *m;
  1712.      void *handler;
  1713. {
  1714.   void *retaddr;
  1715.  
  1716. #ifdef INCOMING_REGNO
  1717.       /* we need to update the saved return address register from
  1718.          the last frame we unwind, or the handler frame will have the wrong
  1719.          return address.  */
  1720.       if (m->udata->saved[m->udata->retaddr_column] == REG_SAVED_REG)
  1721.         {
  1722.           int i = m->udata->reg_or_offset[m->udata->retaddr_column];
  1723.           if (in_reg_window (i, m->udata))
  1724.             copy_reg (i, m->udata, m->udata_start);
  1725.         }
  1726. #endif
  1727.  
  1728.   /* Emit the stub to adjust sp and jump to the handler.  */
  1729.   retaddr = __builtin_eh_stub ();
  1730.  
  1731.   /* And then set our return address to point to the stub.  */
  1732.   if (m->udata_start->saved[m->udata_start->retaddr_column] ==
  1733.       REG_SAVED_OFFSET)
  1734.     put_return_addr (retaddr, m->udata_start);
  1735.   else
  1736.     __builtin_set_return_addr_reg (retaddr);
  1737.  
  1738.   /* Set up the registers we use to communicate with the stub.
  1739.      We check STACK_GROWS_DOWNWARD so the stub can use adjust_stack.  */
  1740.   __builtin_set_eh_regs
  1741.     (handler,
  1742. #ifdef STACK_GROWS_DOWNWARD
  1743.      m->udata->cfa - m->udata_start->cfa
  1744. #else
  1745.      m->udata_start->cfa - m->udata->cfa
  1746. #endif
  1747.      + m->udata->args_size);
  1748.  
  1749.   /* Epilogue:  restore the handler frame's register values and return
  1750.      to the stub.  */
  1751. }
  1752.  
  1753. __SIZE_TYPE__
  1754. __gnat_machine_state_length ()
  1755. {
  1756.   return sizeof (struct machine_state);
  1757. }
  1758.  
  1759. void *
  1760. __gnat_get_code_loc (m)
  1761.      struct machine_state *m;
  1762. {
  1763.   return m->pc;
  1764. }
  1765. #endif /* DWARF2_UNWIND_INFO */
  1766. #endif /* !IN_GCC && !__RT__ */
  1767.  
  1768. #ifndef HAVE_GNAT_INIT_FLOAT
  1769. /* All targets without a specific __gnat_init_float will use an empty one */
  1770. void
  1771. __gnat_init_float ()
  1772. {
  1773. }
  1774. #endif
  1775.