OSDN Git Service

2009-04-17 Pascal Obry <obry@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / initialize.c
1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                           I N I T I A L I Z E                            *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *          Copyright (C) 1992-2009, Free Software Foundation, Inc.         *
10  *                                                                          *
11  * GNAT is free software;  you can  redistribute it  and/or modify it under *
12  * terms of the  GNU General Public License as published  by the Free Soft- *
13  * ware  Foundation;  either version 3,  or (at your option) any later ver- *
14  * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
15  * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
16  * or FITNESS FOR A PARTICULAR PURPOSE.                                     *
17  *                                                                          *
18  * As a special exception under Section 7 of GPL version 3, you are granted *
19  * additional permissions described in the GCC Runtime Library Exception,   *
20  * version 3.1, as published by the Free Software Foundation.               *
21  *                                                                          *
22  * You should have received a copy of the GNU General Public License and    *
23  * a copy of the GCC Runtime Library Exception along with this program;     *
24  * see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    *
25  * <http://www.gnu.org/licenses/>.                                          *
26  *                                                                          *
27  * GNAT was originally developed  by the GNAT team at  New York University. *
28  * Extensive contributions were provided by Ada Core Technologies Inc.      *
29  *                                                                          *
30  ****************************************************************************/
31
32 /*  This unit provides default implementation for __gnat_initialize ()
33     which is called before the elaboration of the partition. It is provided
34     in a separate file/object so that users can replace it easily.
35     The default implementation should be null on most targets. */
36
37 /* The following include is here to meet the published VxWorks requirement
38    that the __vxworks header appear before any other include. */
39 #ifdef __vxworks
40 #include "vxWorks.h"
41 #endif
42
43 #ifdef IN_RTS
44 #include "tconfig.h"
45 #include "tsystem.h"
46 /* We don't have libiberty, so use malloc.  */
47 #define xmalloc(S) malloc (S)
48 #else
49 #include "config.h"
50 #include "system.h"
51 #endif
52
53 #include "raise.h"
54
55 /******************************************/
56 /* __gnat_initialize (NT-mingw32 Version) */
57 /******************************************/
58
59 #if defined (__MINGW32__)
60 #include "mingw32.h"
61 #include <windows.h>
62
63 extern void __gnat_init_float (void);
64 extern void __gnat_install_SEH_handler (void *);
65
66 extern int gnat_argc;
67 extern char **gnat_argv;
68
69 #ifndef RTX
70 /* Do not define for RTX since it is only used for creating child processes
71    which is not supported in RTX. */
72 extern void __gnat_plist_init (void);
73 #endif
74
75 void
76 __gnat_initialize (void *eh)
77 {
78    /* Initialize floating-point coprocessor. This call is needed because
79       the MS libraries default to 64-bit precision instead of 80-bit
80       precision, and we require the full precision for proper operation,
81       given that we have set Max_Digits etc with this in mind */
82    __gnat_init_float ();
83
84    /* Adjust gnat_argv to support Unicode characters. */
85    {
86      char arg_utf8[MAX_PATH];
87      LPWSTR *wargv;
88      int wargc;
89      int k;
90
91      wargv = CommandLineToArgvW (GetCommandLineW(), &wargc);
92
93      if (wargv != NULL)
94        {
95          /* Set gnat_argv with arguments encoded in UTF-8. */
96          gnat_argv = (char **) xmalloc ((wargc + 1) * sizeof (char *));
97
98          for (k=0; k<wargc; k++)
99            {
100              WS2SU (arg_utf8, wargv[k], MAX_PATH);
101              gnat_argv[k] = (char *) xmalloc (strlen (arg_utf8) + 1);
102              strcpy (gnat_argv[k], arg_utf8);
103            }
104
105          LocalFree (wargv);
106          gnat_argc = wargc;
107        }
108     }
109
110    /* Note that we do not activate this for the compiler itself to avoid a
111       bootstrap path problem.  Older version of gnatbind will generate a call
112       to __gnat_initialize() without argument. Therefore we cannot use eh in
113       this case.  It will be possible to remove the following #ifdef at some
114       point.  */
115 #ifdef IN_RTS
116    /* Install the Structured Exception handler.  */
117    if (eh)
118      __gnat_install_SEH_handler (eh);
119 #endif
120 }
121
122 /******************************************/
123 /* __gnat_initialize (init_float version) */
124 /******************************************/
125
126 #elif defined (__Lynx__) || defined (__FreeBSD__) || defined(__NetBSD__) \
127   || defined (__OpenBSD__)
128
129 extern void __gnat_init_float (void);
130
131 void
132 __gnat_initialize (void *eh ATTRIBUTE_UNUSED)
133 {
134    __gnat_init_float ();
135 }
136
137 /***************************************/
138 /* __gnat_initialize (VxWorks Version) */
139 /***************************************/
140
141 #elif defined(__vxworks)
142
143 extern void __gnat_init_float (void);
144
145 void
146 __gnat_initialize (void *eh)
147 {
148   __gnat_init_float ();
149
150   /* On targets where we use the ZCX scheme, we need to register the frame
151      tables at load/startup time.
152
153      For applications loaded as a set of "modules", the crtstuff objects
154      linked in (crtbegin.o/end.o) are tailored to provide this service
155      automatically, a-la C++ constructor fashion, triggered by the VxWorks
156      loader thanks to a special variable declaration in crtbegin.o (_ctors).
157
158      Automatic de-registration is handled symmetrically, a-la C++ destructor
159      fashion (with a _dtors variable also in crtbegin.o) triggered by the
160      dynamic unloader.
161
162      Note that since the tables shall be registered against a common
163      data structure, libgcc should be one of the modules (vs being partially
164      linked against all the others at build time) and shall be loaded first.
165
166      For applications linked with the kernel, the scheme above would lead to
167      duplicated symbols because the VxWorks kernel build "munches" by default,
168      so we link against crtbeginT.o instead of crtbegin.o, which doesn't
169      include the special variables. We know which set of crt objects is used
170      thanks to a boolean indicator present in both sets (__module_has_ctors),
171      and directly call the appropriate function here in the not-automatic
172      case. We'll never unload that, so there is no de-registration to worry
173      about.
174
175      For whole applications loaded as a single module, we may use one scheme
176      or the other, except for the mixed Ada/C++ case in which the first scheme
177      would fail for the same reason as in the linked-with-kernel situation.
178
179      Selecting the crt set with the ctors/dtors capabilities (first scheme
180      above) is triggered by adding "-dynamic" to the gcc *link* command line
181      options. Selecting the other set is achieved by using "-static" instead.
182
183      This is a first approach, tightly synchronized with a number of GCC
184      configuration and crtstuff changes. We need to ensure that those changes
185      are there to activate this circuitry.  */
186
187 #if (__GNUC__ >= 3) && (defined (_ARCH_PPC) || defined (__ppc))
188  {
189    /* The scheme described above is only useful for the actual ZCX case, and
190       we don't want any reference to the crt provided symbols otherwise.  We
191       may not link with any of the crt objects in the non-ZCX case, e.g. from
192       documented procedures instructing the use of -nostdlib, and references
193       to the ctors symbols here would just remain unsatisfied.
194
195       We have no way to avoid those references in the right conditions in this
196       C module, because we have nothing like a IN_ZCX_RTS macro.  This aspect
197       is then deferred to an Ada routine, which can do that based on a test
198       against a constant System flag value.  */
199
200    extern void __gnat_vxw_setup_for_eh (void);
201    __gnat_vxw_setup_for_eh ();
202  }
203 #endif
204 }
205
206 #elif defined(_T_HPUX10) || (!defined(IN_RTS) && defined(_X_HPUX10))
207
208 /************************************************/
209 /* __gnat_initialize (PA-RISC HP-UX 10 Version) */
210 /************************************************/
211
212 extern void __main (void);
213
214 void
215 __gnat_initialize (void *eh ATTRIBUTE_UNUSED)
216 {
217   __main ();
218 }
219
220 #else
221
222 /* For all other versions of GNAT, the initialize routine and handler
223    installation do nothing */
224
225 /***************************************/
226 /* __gnat_initialize (Default Version) */
227 /***************************************/
228
229 void
230 __gnat_initialize (void *eh ATTRIBUTE_UNUSED)
231 {
232 }
233
234 #endif