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.PD within this package.
12 #define SPECIAL_CRTEXE
21 #include <sect_attribs.h>
25 extern wchar_t *** __MINGW_IMP_SYMBOL(__winitenv
);
26 #define __winitenv (* __MINGW_IMP_SYMBOL(__winitenv))
30 extern char *** __MINGW_IMP_SYMBOL(__initenv
);
31 #define __initenv (* __MINGW_IMP_SYMBOL(__initenv))
34 /* Hack, for bug in ld. Will be removed soon. */
36 #define __ImageBase __MINGW_LSYMBOL(_image_base__)
39 /* This symbol is defined by ld. */
40 extern IMAGE_DOS_HEADER __ImageBase
;
42 extern void __cdecl
_fpreset (void);
43 #define SPACECHAR _T(' ')
44 #define DQUOTECHAR _T('\"')
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
;
55 extern _CRTIMP
void __cdecl
_initterm(_PVFV
*, _PVFV
*);
57 static int __cdecl
check_managed_app (void);
59 extern _CRTALLOC(".CRT$XIA") _PIFV __xi_a
[];
60 extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z
[];
61 extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a
[];
62 extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z
[];
64 /* TLS initialization hook. */
65 extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback
;
67 extern _PVFV
*__onexitbegin
;
68 extern _PVFV
*__onexitend
;
70 extern int mingw_app_type
;
72 HINSTANCE __mingw_winmain_hInstance
;
73 _TCHAR
*__mingw_winmain_lpCmdLine
;
74 DWORD __mingw_winmain_nShowCmd
;
77 extern void __main(void);
79 static wchar_t **argv
;
80 static wchar_t **envp
;
88 static int managedapp
;
89 static int has_cctor
= 0;
90 static _startupinfo startinfo
;
91 extern LPTOP_LEVEL_EXCEPTION_FILTER __mingw_oldexcpt_handler
;
93 extern void _pei386_runtime_relocator (void);
94 long CALLBACK
_gnu_exception_handler (EXCEPTION_POINTERS
* exception_data
);
96 static void duplicate_ppstrings (int ac
, wchar_t ***av
);
98 static void duplicate_ppstrings (int ac
, char ***av
);
101 static int __cdecl
pre_c_init (void);
102 static void __cdecl
pre_cpp_init (void);
103 static void __cdecl
__mingw_prepare_except_for_msvcr80_and_higher (void);
104 _CRTALLOC(".CRT$XIAA") _PIFV mingw_pcinit
= pre_c_init
;
105 _CRTALLOC(".CRT$XCAA") _PVFV mingw_pcppinit
= pre_cpp_init
;
107 extern int _MINGW_INSTALL_DEBUG_MATHERR
;
112 managedapp
= check_managed_app ();
114 __set_app_type(_GUI_APP
);
116 __set_app_type (_CONSOLE_APP
);
117 __onexitbegin
= __onexitend
= (_PVFV
*) _encode_pointer ((_PVFV
*)(-1));
119 * __MINGW_IMP_SYMBOL(_fmode
) = _fmode
;
120 * __MINGW_IMP_SYMBOL(_commode
) = _commode
;
127 if (_MINGW_INSTALL_DEBUG_MATHERR
== 1)
129 __setusermatherr (_matherr
);
131 #ifndef __clang__ /* FIXME: CORE-14042 */
132 if (__globallocalestatus
== -1)
142 startinfo
.newmode
= _newmode
;
145 argret
= __wgetmainargs(&argc
,&argv
,&envp
,_dowildcard
,&startinfo
);
147 argret
= __getmainargs(&argc
,&argv
,&envp
,_dowildcard
,&startinfo
);
151 static int __cdecl
__tmainCRTStartup (void);
153 int __cdecl
WinMainCRTStartup (void);
155 int __cdecl
WinMainCRTStartup (void)
159 asm ("\t.l_startw:\n"
160 "\t.seh_handler __C_specific_handler, @except\n"
161 "\t.seh_handlerdata\n"
163 "\t.rva .l_startw, .l_endw, _gnu_exception_handler ,.l_endw\n"
168 __security_init_cookie ();
169 ret
= __tmainCRTStartup ();
177 int __cdecl
mainCRTStartup (void);
180 int __mingw_init_ehandler (void);
183 int __cdecl
mainCRTStartup (void)
188 "\t.seh_handler __C_specific_handler, @except\n"
189 "\t.seh_handlerdata\n"
191 "\t.rva .l_start, .l_end, _gnu_exception_handler ,.l_end\n"
196 __security_init_cookie ();
197 ret
= __tmainCRTStartup ();
208 __tmainCRTStartup (void)
210 _TCHAR
*lpszCommandLine
= NULL
;
211 STARTUPINFO StartupInfo
;
212 WINBOOL inDoubleQuote
= FALSE
;
213 memset (&StartupInfo
, 0, sizeof (STARTUPINFO
));
216 /* We need to make sure that this function is build with frame-pointer
217 and that we align the stack to 16 bytes for the sake of SSE ops in main
218 or in functions inlined into main. */
219 lpszCommandLine
= (_TCHAR
*) alloca (32);
220 memset (lpszCommandLine
, 0xcc, 32);
222 asm __volatile__ ("andl $-16, %%esp" : : : "%esp");
227 GetStartupInfo (&StartupInfo
);
229 void *lock_free
= NULL
;
230 void *fiberid
= ((PNT_TIB
)NtCurrentTeb())->StackBase
;
232 while((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,
235 if (lock_free
== fiberid
)
242 if (__native_startup_state
== __initializing
)
246 else if (__native_startup_state
== __uninitialized
)
248 __native_startup_state
= __initializing
;
249 _initterm ((_PVFV
*)(void *)__xi_a
, (_PVFV
*)(void *) __xi_z
);
254 if (__native_startup_state
== __initializing
)
256 _initterm (__xc_a
, __xc_z
);
257 __native_startup_state
= __initialized
;
259 _ASSERTE(__native_startup_state
== __initialized
);
261 (VOID
)InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
263 if (__dyn_tls_init_callback
!= NULL
)
264 __dyn_tls_init_callback (NULL
, DLL_THREAD_ATTACH
, NULL
);
266 _pei386_runtime_relocator ();
267 __mingw_oldexcpt_handler
= SetUnhandledExceptionFilter (_gnu_exception_handler
);
269 __mingw_init_ehandler ();
271 __mingw_prepare_except_for_msvcr80_and_higher ();
278 lpszCommandLine
= (_TCHAR
*) _wcmdln
;
280 lpszCommandLine
= (char *) _acmdln
;
282 while (*lpszCommandLine
> SPACECHAR
|| (*lpszCommandLine
&& inDoubleQuote
))
284 if (*lpszCommandLine
== DQUOTECHAR
)
285 inDoubleQuote
= !inDoubleQuote
;
287 if (_ismbblead (*lpszCommandLine
))
289 if (lpszCommandLine
) /* FIXME: Why this check? Should I check for *lpszCommandLine != 0 too? */
295 while (*lpszCommandLine
&& (*lpszCommandLine
<= SPACECHAR
))
298 __mingw_winmain_hInstance
= (HINSTANCE
) &__ImageBase
;
299 __mingw_winmain_lpCmdLine
= lpszCommandLine
;
300 __mingw_winmain_nShowCmd
= StartupInfo
.dwFlags
& STARTF_USESHOWWINDOW
?
301 StartupInfo
.wShowWindow
: SW_SHOWDEFAULT
;
303 duplicate_ppstrings (argc
, &argv
);
307 /* C++ initialization.
308 gcc inserts this call automatically for a function called main, but not for wmain. */
309 mainret
= wmain (argc
, argv
, envp
);
312 mainret
= main (argc
, argv
, envp
);
323 extern int mingw_initltsdrot_force
;
324 extern int mingw_initltsdyn_force
;
325 extern int mingw_initltssuo_force
;
326 extern int mingw_initcharmax
;
329 check_managed_app (void)
331 PIMAGE_DOS_HEADER pDOSHeader
;
332 PIMAGE_NT_HEADERS pPEHeader
;
333 PIMAGE_OPTIONAL_HEADER32 pNTHeader32
;
334 PIMAGE_OPTIONAL_HEADER64 pNTHeader64
;
336 /* Force to be linked. */
337 mingw_initltsdrot_force
=1;
338 mingw_initltsdyn_force
=1;
339 mingw_initltssuo_force
=1;
342 pDOSHeader
= (PIMAGE_DOS_HEADER
) &__ImageBase
;
343 if (pDOSHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
346 pPEHeader
= (PIMAGE_NT_HEADERS
)((char *)pDOSHeader
+ pDOSHeader
->e_lfanew
);
347 if (pPEHeader
->Signature
!= IMAGE_NT_SIGNATURE
)
350 pNTHeader32
= (PIMAGE_OPTIONAL_HEADER32
) &pPEHeader
->OptionalHeader
;
351 switch (pNTHeader32
->Magic
)
353 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
354 if (pNTHeader32
->NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
)
356 return !! pNTHeader32
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
;
357 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
358 pNTHeader64
= (PIMAGE_OPTIONAL_HEADER64
)pNTHeader32
;
359 if (pNTHeader64
->NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
)
361 return !! pNTHeader64
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
;
367 static size_t wbytelen(const wchar_t *p
)
375 static void duplicate_ppstrings (int ac
, wchar_t ***av
)
379 wchar_t **n
= (wchar_t **) malloc (sizeof (wchar_t *) * (ac
+ 1));
382 for (i
=0; i
< ac
; i
++)
384 size_t l
= wbytelen (avl
[i
]);
385 n
[i
] = (wchar_t *) malloc (l
);
386 memcpy (n
[i
], avl
[i
], l
);
392 static void duplicate_ppstrings (int ac
, char ***av
)
396 char **n
= (char **) malloc (sizeof (char *) * (ac
+ 1));
399 for (i
=0; i
< ac
; i
++)
401 size_t l
= strlen (avl
[i
]) + 1;
402 n
[i
] = (char *) malloc (l
);
403 memcpy (n
[i
], avl
[i
], l
);
410 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION
411 #define __UNUSED_PARAM_1(x) x
413 #define __UNUSED_PARAM_1 __UNUSED_PARAM
416 __mingw_invalidParameterHandler (const wchar_t * __UNUSED_PARAM_1(expression
),
417 const wchar_t * __UNUSED_PARAM_1(function
),
418 const wchar_t * __UNUSED_PARAM_1(file
),
419 unsigned int __UNUSED_PARAM_1(line
),
420 uintptr_t __UNUSED_PARAM(pReserved
))
422 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION
423 wprintf(L
"Invalid parameter detected in function %s. File: %s Line: %u\n", function
, file
, line
);
424 wprintf(L
"Expression: %s\n", expression
);
428 HANDLE
__mingw_get_msvcrt_handle(void);
431 __mingw_prepare_except_for_msvcr80_and_higher (void)
433 _invalid_parameter_handler (*fIPH
)(_invalid_parameter_handler
) = NULL
;
435 fIPH
= (void*)GetProcAddress (__mingw_get_msvcrt_handle(), "_set_invalid_parameter_handler");
437 (*fIPH
)(__mingw_invalidParameterHandler
);