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 *** __MINGW_IMP_SYMBOL(__winitenv
);
27 #define __winitenv (* __MINGW_IMP_SYMBOL(__winitenv))
31 extern char *** __MINGW_IMP_SYMBOL(__initenv
);
32 #define __initenv (* __MINGW_IMP_SYMBOL(__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 * __MINGW_IMP_SYMBOL(_fmode
);
47 extern int * __MINGW_IMP_SYMBOL(_commode
);
51 extern int * __MINGW_IMP_SYMBOL(_commode
);
52 #define _commode (* __MINGW_IMP_SYMBOL(_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
;
77 HINSTANCE __mingw_winmain_hInstance
;
78 _TCHAR
*__mingw_winmain_lpCmdLine
;
79 DWORD __mingw_winmain_nShowCmd
;
83 extern void __main(void);
84 static wchar_t **argv
;
85 static wchar_t **envp
;
93 static int managedapp
;
94 static int has_cctor
= 0;
95 static _startupinfo startinfo
;
96 static LPTOP_LEVEL_EXCEPTION_FILTER __mingw_oldexcpt_handler
= NULL
;
98 extern void _pei386_runtime_relocator (void);
99 static long CALLBACK
_gnu_exception_handler (EXCEPTION_POINTERS
* exception_data
);
101 static void duplicate_ppstrings (int ac
, wchar_t ***av
);
103 static void duplicate_ppstrings (int ac
, char ***av
);
106 static int __cdecl
pre_c_init (void);
107 static void __cdecl
pre_cpp_init (void);
108 static void __cdecl
__mingw_prepare_except_for_msvcr80_and_higher (void);
109 _CRTALLOC(".CRT$XIAA") _PIFV mingw_pcinit
= pre_c_init
;
110 _CRTALLOC(".CRT$XCAA") _PVFV mingw_pcppinit
= pre_cpp_init
;
115 managedapp
= check_managed_app ();
117 __set_app_type(_GUI_APP
);
119 __set_app_type (_CONSOLE_APP
);
120 __onexitbegin
= __onexitend
= (_PVFV
*) _encode_pointer ((_PVFV
*)(-1));
122 * __MINGW_IMP_SYMBOL(_fmode
) = _fmode
;
123 * __MINGW_IMP_SYMBOL(_commode
) = _commode
;
130 if (_MINGW_INSTALL_DEBUG_MATHERR
)
132 if (! __defaultmatherr
)
134 __setusermatherr (_matherr
);
135 __defaultmatherr
= 1;
139 if (__globallocalestatus
== -1)
148 startinfo
.newmode
= _newmode
;
151 argret
= __wgetmainargs(&argc
,&argv
,&envp
,_dowildcard
,&startinfo
);
153 argret
= __getmainargs(&argc
,&argv
,&envp
,_dowildcard
,&startinfo
);
157 static int __tmainCRTStartup (void);
159 int WinMainCRTStartup (void);
161 int WinMainCRTStartup (void)
164 __security_init_cookie ();
165 return __tmainCRTStartup ();
168 int mainCRTStartup (void);
171 int __mingw_init_ehandler (void);
174 int mainCRTStartup (void)
177 __security_init_cookie ();
178 return __tmainCRTStartup ();
182 __declspec(noinline
) int
183 __tmainCRTStartup (void)
185 _TCHAR
*lpszCommandLine
= NULL
;
186 STARTUPINFO StartupInfo
;
187 BOOL inDoubleQuote
= FALSE
;
188 memset (&StartupInfo
, 0, sizeof (STARTUPINFO
));
191 GetStartupInfo (&StartupInfo
);
193 void *lock_free
= NULL
;
194 void *fiberid
= ((PNT_TIB
)NtCurrentTeb())->StackBase
;
196 while((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,
199 if (lock_free
== fiberid
)
206 if (__native_startup_state
== __initializing
)
210 else if (__native_startup_state
== __uninitialized
)
212 __native_startup_state
= __initializing
;
213 _initterm ((_PVFV
*)(void *)__xi_a
, (_PVFV
*)(void *) __xi_z
);
218 if (__native_startup_state
== __initializing
)
220 _initterm (__xc_a
, __xc_z
);
221 __native_startup_state
= __initialized
;
223 _ASSERTE(__native_startup_state
== __initialized
);
225 (VOID
)InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
227 if (__dyn_tls_init_callback
!= NULL
)
228 __dyn_tls_init_callback (NULL
, DLL_THREAD_ATTACH
, NULL
);
230 _pei386_runtime_relocator ();
231 __mingw_oldexcpt_handler
= SetUnhandledExceptionFilter (_gnu_exception_handler
);
233 __mingw_init_ehandler ();
235 __mingw_prepare_except_for_msvcr80_and_higher ();
242 lpszCommandLine
= (_TCHAR
*) _wcmdln
;
244 lpszCommandLine
= (char *) _acmdln
;
246 while (*lpszCommandLine
> SPACECHAR
|| (*lpszCommandLine
&&inDoubleQuote
))
248 if (*lpszCommandLine
== DQUOTECHAR
)
249 inDoubleQuote
= !inDoubleQuote
;
251 if (_ismbblead (*lpszCommandLine
))
253 if (*lpszCommandLine
)
259 while (*lpszCommandLine
&& (*lpszCommandLine
<= SPACECHAR
))
262 __mingw_winmain_hInstance
= (HINSTANCE
) &__ImageBase
;
263 __mingw_winmain_lpCmdLine
= lpszCommandLine
;
264 __mingw_winmain_nShowCmd
= StartupInfo
.dwFlags
& STARTF_USESHOWWINDOW
?
265 StartupInfo
.wShowWindow
: SW_SHOWDEFAULT
;
267 duplicate_ppstrings (argc
, &argv
);
270 /* C++ initialization.
271 gcc inserts this call automatically for a function called main, but not for wmain. */
273 mainret
= wmain (argc
, argv
, envp
);
276 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
;
403 case EXCEPTION_DATATYPE_MISALIGNMENT
:
404 case EXCEPTION_ARRAY_BOUNDS_EXCEEDED
:
405 case EXCEPTION_FLT_STACK_CHECK
:
406 case EXCEPTION_INT_OVERFLOW
:
407 case EXCEPTION_INVALID_HANDLE
:
408 /*case EXCEPTION_POSSIBLE_DEADLOCK: */
409 action
= EXCEPTION_CONTINUE_EXECUTION
;
416 if (action
== EXCEPTION_CONTINUE_SEARCH
&& __mingw_oldexcpt_handler
)
417 action
= (*__mingw_oldexcpt_handler
)(exception_data
);
422 static size_t wbytelen(const wchar_t *p
)
430 static void duplicate_ppstrings (int ac
, wchar_t ***av
)
434 wchar_t **n
= (wchar_t **) malloc (sizeof (wchar_t *) * (ac
+ 1));
437 for (i
=0; i
< ac
; i
++)
439 int l
= wbytelen (avl
[i
]);
440 n
[i
] = (wchar_t *) malloc (l
);
441 memcpy (n
[i
], avl
[i
], l
);
447 static void duplicate_ppstrings (int ac
, char ***av
)
451 char **n
= (char **) malloc (sizeof (char *) * (ac
+ 1));
454 for (i
=0; i
< ac
; i
++)
456 int l
= strlen (avl
[i
]) + 1;
457 n
[i
] = (char *) malloc (l
);
458 memcpy (n
[i
], avl
[i
], l
);
466 __mingw_invalidParameterHandler (const wchar_t *expression
__attribute__ ((__unused__
)),
467 const wchar_t *function
__attribute__ ((__unused__
)),
468 const wchar_t *file
__attribute__ ((__unused__
)),
469 unsigned int line
__attribute__ ((__unused__
)),
470 uintptr_t pReserved
__attribute__ ((__unused__
)))
472 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION
473 wprintf(L
"Invalid parameter detected in function %s. File: %s Line: %d\n", function
, file
, line
);
474 wprintf(L
"Expression: %s\n", expression
);
479 __mingw_prepare_except_for_msvcr80_and_higher (void)
481 _invalid_parameter_handler (*fIPH
)(_invalid_parameter_handler
) = NULL
;
482 HMODULE hmsv
= GetModuleHandleA ("msvcr80.dll");
484 hmsv
= GetModuleHandleA ("msvcr70.dll");
486 hmsv
= GetModuleHandleA ("msvcrt.dll");
488 hmsv
= LoadLibraryA ("msvcrt.dll");
491 fIPH
= (_invalid_parameter_handler (*)(_invalid_parameter_handler
))
492 GetProcAddress (hmsv
, "_set_invalid_parameter_handler");
494 (*fIPH
)(__mingw_invalidParameterHandler
);