2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the w64 mingw-runtime package.
4 * No warranty is given; refer to the file DISCLAIMER within this package.
12 #define SPECIAL_CRTEXE
21 #include <sect_attribs.h>
26 extern wchar_t ***_imp____winitenv
;
27 #define __winitenv (*_imp____winitenv)
31 extern char ***_imp____initenv
;
32 #define __initenv (*_imp____initenv)
35 /* Hack, for bug in ld. Will be removed soon. */
36 #define __ImageBase _image_base__
37 /* This symbol is defined by ld. */
38 extern IMAGE_DOS_HEADER __ImageBase
;
40 extern void _fpreset (void);
41 #define SPACECHAR _T(' ')
42 #define DQUOTECHAR _T('\"')
44 __declspec(dllimport
) void __setusermatherr(int (__cdecl
*)(struct _exception
*));
46 extern int *_imp___fmode
;
47 extern int *_imp___commode
;
51 extern int *_imp___commode
;
52 #define _commode (*_imp___commode)
53 extern int _dowildcard
;
56 int _MINGW_INSTALL_DEBUG_MATHERR
__attribute__((weak
)) = 0;
58 int _MINGW_INSTALL_DEBUG_MATHERR
= 0;
60 extern int __defaultmatherr
;
61 extern _CRTIMP
void __cdecl
_initterm(_PVFV
*, _PVFV
*);
63 static int __cdecl
check_managed_app (void);
65 extern _CRTALLOC(".CRT$XIA") _PIFV __xi_a
[];
66 extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z
[];
67 extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a
[];
68 extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z
[];
70 extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback
;
72 extern _PVFV
*__onexitbegin
;
73 extern _PVFV
*__onexitend
;
75 extern int mingw_app_type
;
79 extern void __main(void);
80 static wchar_t **argv
;
81 static wchar_t **envp
;
89 static int managedapp
;
90 static int has_cctor
= 0;
91 static _startupinfo startinfo
;
93 extern void _pei386_runtime_relocator (void);
94 static long CALLBACK
_gnu_exception_handler (EXCEPTION_POINTERS
* exception_data
);
95 //static LONG __mingw_vex(EXCEPTION_POINTERS * exception_data);
97 static void duplicate_ppstrings (int ac
, wchar_t ***av
);
99 static void duplicate_ppstrings (int ac
, char ***av
);
102 static int __cdecl
pre_c_init (void);
103 static void __cdecl
pre_cpp_init (void);
105 _CRTALLOC(".CRT$XIAA") _PIFV mingw_pcinit
= pre_c_init
;
106 _CRTALLOC(".CRT$XCAA") _PVFV mingw_pcppinit
= pre_cpp_init
;
111 managedapp
= check_managed_app ();
113 __set_app_type(_GUI_APP
);
115 __set_app_type (_CONSOLE_APP
);
116 __onexitbegin
= __onexitend
= (_PVFV
*) _encode_pointer ((_PVFV
*)(-1));
118 *_imp___fmode
= _fmode
;
119 *_imp___commode
= _commode
;
126 if (_MINGW_INSTALL_DEBUG_MATHERR
)
128 if (! __defaultmatherr
)
130 __setusermatherr (_matherr
);
131 __defaultmatherr
= 1;
135 if (__globallocalestatus
== -1)
144 startinfo
.newmode
= _newmode
;
147 argret
= __wgetmainargs(&argc
,&argv
,&envp
,_dowildcard
,&startinfo
);
149 argret
= __getmainargs(&argc
,&argv
,&envp
,_dowildcard
,&startinfo
);
153 static int __tmainCRTStartup (void);
155 int WinMainCRTStartup (void)
158 __security_init_cookie ();
159 return __tmainCRTStartup ();
162 int mainCRTStartup (void)
165 __security_init_cookie ();
166 return __tmainCRTStartup ();
170 __declspec(noinline
) int
171 __tmainCRTStartup (void)
173 _TCHAR
*lpszCommandLine
= NULL
;
174 STARTUPINFO StartupInfo
;
175 BOOL inDoubleQuote
= FALSE
;
176 memset (&StartupInfo
, 0, sizeof (STARTUPINFO
));
179 GetStartupInfo (&StartupInfo
);
181 void *lock_free
= NULL
;
182 void *fiberid
= ((PNT_TIB
)NtCurrentTeb())->StackBase
;
184 while((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,
187 if (lock_free
== fiberid
)
194 if (__native_startup_state
== __initializing
)
198 else if (__native_startup_state
== __uninitialized
)
200 __native_startup_state
= __initializing
;
201 _initterm ((_PVFV
*)(void *)__xi_a
, (_PVFV
*)(void *) __xi_z
);
206 if (__native_startup_state
== __initializing
)
208 _initterm (__xc_a
, __xc_z
);
209 __native_startup_state
= __initialized
;
211 _ASSERTE(__native_startup_state
== __initialized
);
213 (VOID
)InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
215 if (__dyn_tls_init_callback
!= NULL
&& _IsNonwritableInCurrentImage ((PBYTE
) &__dyn_tls_init_callback
))
216 __dyn_tls_init_callback (NULL
, DLL_THREAD_ATTACH
, NULL
);
218 _pei386_runtime_relocator ();
220 #if defined(__i386__) || defined(_M_IX86)
221 __writefsdword(0, 0xffffffff);
223 //AddVectoredExceptionHandler (0, (PVECTORED_EXCEPTION_HANDLER)__mingw_vex);
224 SetUnhandledExceptionFilter (_gnu_exception_handler
);
231 lpszCommandLine
= (_TCHAR
*) _wcmdln
;
233 lpszCommandLine
= (char *) _acmdln
;
235 while (*lpszCommandLine
> SPACECHAR
|| (*lpszCommandLine
&&inDoubleQuote
))
237 if (*lpszCommandLine
== DQUOTECHAR
)
238 inDoubleQuote
= !inDoubleQuote
;
240 if (_ismbblead (*lpszCommandLine
))
248 while (*lpszCommandLine
&& (*lpszCommandLine
<= SPACECHAR
))
252 /* C++ initialization.
253 gcc inserts this call automatically for a function called main, but not for wmain. */
256 (int) (StartupInfo
.dwFlags
& STARTF_USESHOWWINDOW
? StartupInfo
.wShowWindow
: SW_SHOWDEFAULT
),
257 (wchar_t **) lpszCommandLine
, (wchar_t **) (HINSTANCE
) &__ImageBase
);
260 (int) (StartupInfo
.dwFlags
& STARTF_USESHOWWINDOW
? StartupInfo
.wShowWindow
: SW_SHOWDEFAULT
),
261 (char **) lpszCommandLine
, (char **) (HINSTANCE
) &__ImageBase
);
266 duplicate_ppstrings (argc
, &argv
);
269 /* C++ initialization.
270 gcc inserts this call automatically for a function called main, but not for wmain. */
272 mainret
= wmain (argc
, argv
, envp
);
275 mainret
= main (argc
, argv
, envp
);
287 extern int mingw_initltsdrot_force
;
288 extern int mingw_initltsdyn_force
;
289 extern int mingw_initltssuo_force
;
290 extern int mingw_initcharmax
;
293 check_managed_app (void)
295 PIMAGE_DOS_HEADER pDOSHeader
;
296 PIMAGE_NT_HEADERS pPEHeader
;
297 PIMAGE_OPTIONAL_HEADER32 pNTHeader32
;
298 PIMAGE_OPTIONAL_HEADER64 pNTHeader64
;
300 /* Force to be linked. */
301 mingw_initltsdrot_force
=1;
302 mingw_initltsdyn_force
=1;
303 mingw_initltssuo_force
=1;
306 pDOSHeader
= (PIMAGE_DOS_HEADER
) &__ImageBase
;
307 if (pDOSHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
310 pPEHeader
= (PIMAGE_NT_HEADERS
)((char *)pDOSHeader
+ pDOSHeader
->e_lfanew
);
311 if (pPEHeader
->Signature
!= IMAGE_NT_SIGNATURE
)
314 pNTHeader32
= (PIMAGE_OPTIONAL_HEADER32
) &pPEHeader
->OptionalHeader
;
315 switch (pNTHeader32
->Magic
)
317 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
318 if (pNTHeader32
->NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
)
320 return !! pNTHeader32
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
;
321 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
322 pNTHeader64
= (PIMAGE_OPTIONAL_HEADER64
)pNTHeader32
;
323 if (pNTHeader64
->NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
)
325 return !! pNTHeader64
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
;
331 _gnu_exception_handler (EXCEPTION_POINTERS
* exception_data
)
333 void (*old_handler
) (int);
334 long action
= EXCEPTION_CONTINUE_SEARCH
;
337 switch (exception_data
->ExceptionRecord
->ExceptionCode
)
339 case EXCEPTION_ACCESS_VIOLATION
:
340 /* test if the user has set SIGSEGV */
341 old_handler
= signal (SIGSEGV
, SIG_DFL
);
342 if (old_handler
== SIG_IGN
)
344 /* this is undefined if the signal was raised by anything other
346 signal (SIGSEGV
, SIG_IGN
);
347 action
= EXCEPTION_CONTINUE_EXECUTION
;
349 else if (old_handler
!= SIG_DFL
)
351 /* This means 'old' is a user defined function. Call it */
352 (*old_handler
) (SIGSEGV
);
353 action
= EXCEPTION_CONTINUE_EXECUTION
;
357 case EXCEPTION_ILLEGAL_INSTRUCTION
:
358 case EXCEPTION_PRIV_INSTRUCTION
:
359 /* test if the user has set SIGILL */
360 old_handler
= signal (SIGILL
, SIG_DFL
);
361 if (old_handler
== SIG_IGN
)
363 /* this is undefined if the signal was raised by anything other
365 signal (SIGILL
, SIG_IGN
);
366 action
= EXCEPTION_CONTINUE_EXECUTION
;
368 else if (old_handler
!= SIG_DFL
)
370 /* This means 'old' is a user defined function. Call it */
371 (*old_handler
) (SIGILL
);
372 action
= EXCEPTION_CONTINUE_EXECUTION
;
376 case EXCEPTION_FLT_INVALID_OPERATION
:
377 case EXCEPTION_FLT_DIVIDE_BY_ZERO
:
378 case EXCEPTION_FLT_DENORMAL_OPERAND
:
379 case EXCEPTION_FLT_OVERFLOW
:
380 case EXCEPTION_FLT_UNDERFLOW
:
381 case EXCEPTION_FLT_INEXACT_RESULT
:
385 case EXCEPTION_INT_DIVIDE_BY_ZERO
:
386 /* test if the user has set SIGFPE */
387 old_handler
= signal (SIGFPE
, SIG_DFL
);
388 if (old_handler
== SIG_IGN
)
390 signal (SIGFPE
, SIG_IGN
);
393 action
= EXCEPTION_CONTINUE_EXECUTION
;
395 else if (old_handler
!= SIG_DFL
)
397 /* This means 'old' is a user defined function. Call it */
398 (*old_handler
) (SIGFPE
);
399 action
= EXCEPTION_CONTINUE_EXECUTION
;
410 static LONG
__mingw_vex(EXCEPTION_POINTERS
* exception_data
)
412 /* TODO this is not chainablem, therefore need rewrite. Disabled the ill code. */
415 __asm__
__volatile__ (
416 "movq %gs:0,%rax" "\n\t"
423 __asm__
__volatile__ (
424 "movl %fs:0,%eax" "\n\t"
432 return _gnu_exception_handler(exception_data
);
438 static size_t wbytelen(const wchar_t *p
)
446 static void duplicate_ppstrings (int ac
, wchar_t ***av
)
450 wchar_t **n
= (wchar_t **) malloc (sizeof (wchar_t *) * (ac
+ 1));
453 for (i
=0; i
< ac
; i
++)
455 int l
= wbytelen (avl
[i
]);
456 n
[i
] = (wchar_t *) malloc (l
);
457 memcpy (n
[i
], avl
[i
], l
);
463 static void duplicate_ppstrings (int ac
, char ***av
)
467 char **n
= (char **) malloc (sizeof (char *) * (ac
+ 1));
470 for (i
=0; i
< ac
; i
++)
472 int l
= strlen (avl
[i
]) + 1;
473 n
[i
] = (char *) malloc (l
);
474 memcpy (n
[i
], avl
[i
], l
);