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.
16 #include <sect_attribs.h>
22 /* Special handling for ARM & ARM64, __winitenv & __initenv aren't present there. */
23 #if !defined(__arm__) && !defined(__aarch64__)
24 _CRTIMP
extern wchar_t** __winitenv
;
25 _CRTIMP
extern char** __initenv
;
28 /* Hack, for bug in ld. Will be removed soon. */
30 #define __ImageBase __MINGW_LSYMBOL(_image_base__)
33 /* This symbol is defined by ld. */
34 extern IMAGE_DOS_HEADER __ImageBase
;
36 extern void __cdecl
_fpreset (void);
37 #define SPACECHAR _T(' ')
38 #define DQUOTECHAR _T('\"')
40 extern int _dowildcard
;
42 extern _CRTIMP
void __cdecl
_initterm(_PVFV
*, _PVFV
*);
44 static int __cdecl
check_managed_app (void);
46 extern _CRTALLOC(".CRT$XIA") _PIFV __xi_a
[];
47 extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z
[];
48 extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a
[];
49 extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z
[];
51 /* TLS initialization hook. */
52 extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback
;
54 extern _PVFV
*__onexitbegin
;
55 extern _PVFV
*__onexitend
;
57 extern int mingw_app_type
;
59 HINSTANCE __mingw_winmain_hInstance
;
60 _TCHAR
*__mingw_winmain_lpCmdLine
;
61 DWORD __mingw_winmain_nShowCmd
;
64 extern void __main(void);
66 static wchar_t **argv
;
67 static wchar_t **envp
;
75 static int managedapp
;
76 static int has_cctor
= 0;
77 static _startupinfo startinfo
;
78 extern LPTOP_LEVEL_EXCEPTION_FILTER __mingw_oldexcpt_handler
;
80 extern void _pei386_runtime_relocator (void);
81 long CALLBACK
_gnu_exception_handler (EXCEPTION_POINTERS
* exception_data
);
83 static void duplicate_ppstrings (int ac
, wchar_t ***av
);
85 static void duplicate_ppstrings (int ac
, char ***av
);
88 static int __cdecl
pre_c_init (void);
89 static void __cdecl
pre_cpp_init (void);
90 static void __cdecl
__mingw_prepare_except_for_msvcr80_and_higher (void);
91 _CRTALLOC(".CRT$XIAA") _PIFV mingw_pcinit
= pre_c_init
;
92 _CRTALLOC(".CRT$XCAA") _PVFV mingw_pcppinit
= pre_cpp_init
;
94 extern int _MINGW_INSTALL_DEBUG_MATHERR
;
97 extern void __do_global_dtors(void);
103 managedapp
= check_managed_app ();
105 __set_app_type(_GUI_APP
);
107 __set_app_type (_CONSOLE_APP
);
108 __onexitbegin
= __onexitend
= (_PVFV
*)(-1);
115 if (_MINGW_INSTALL_DEBUG_MATHERR
== 1)
117 __setusermatherr (_matherr
);
119 #ifndef __clang__ /* FIXME: CORE-14042 */
120 if (__globallocalestatus
== -1)
130 startinfo
.newmode
= _newmode
;
133 argret
= __wgetmainargs(&argc
,&argv
,&envp
,_dowildcard
,&startinfo
);
135 argret
= __getmainargs(&argc
,&argv
,&envp
,_dowildcard
,&startinfo
);
139 static int __cdecl
__tmainCRTStartup (void);
141 int __cdecl
WinMainCRTStartup (void);
143 int __cdecl
WinMainCRTStartup (void)
147 asm ("\t.l_startw:\n"
148 "\t.seh_handler __C_specific_handler, @except\n"
149 "\t.seh_handlerdata\n"
151 "\t.rva .l_startw, .l_endw, _gnu_exception_handler ,.l_endw\n"
156 __security_init_cookie ();
157 ret
= __tmainCRTStartup ();
165 int __cdecl
mainCRTStartup (void);
168 int __mingw_init_ehandler (void);
171 int __cdecl
mainCRTStartup (void)
176 "\t.seh_handler __C_specific_handler, @except\n"
177 "\t.seh_handlerdata\n"
179 "\t.rva .l_start, .l_end, _gnu_exception_handler ,.l_end\n"
184 __security_init_cookie ();
185 ret
= __tmainCRTStartup ();
196 __tmainCRTStartup (void)
198 _TCHAR
*lpszCommandLine
= NULL
;
199 STARTUPINFO StartupInfo
;
200 WINBOOL inDoubleQuote
= FALSE
;
201 memset (&StartupInfo
, 0, sizeof (STARTUPINFO
));
204 /* We need to make sure that this function is build with frame-pointer
205 and that we align the stack to 16 bytes for the sake of SSE ops in main
206 or in functions inlined into main. */
207 lpszCommandLine
= (_TCHAR
*) alloca (32);
208 memset (lpszCommandLine
, 0xcc, 32);
210 asm __volatile__ ("andl $-16, %%esp" : : : "%esp");
215 GetStartupInfo (&StartupInfo
);
217 void *lock_free
= NULL
;
218 void *fiberid
= ((PNT_TIB
)NtCurrentTeb())->StackBase
;
220 while((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,
223 if (lock_free
== fiberid
)
230 if (__native_startup_state
== __initializing
)
234 else if (__native_startup_state
== __uninitialized
)
236 __native_startup_state
= __initializing
;
237 _initterm ((_PVFV
*)(void *)__xi_a
, (_PVFV
*)(void *) __xi_z
);
242 if (__native_startup_state
== __initializing
)
244 _initterm (__xc_a
, __xc_z
);
245 __native_startup_state
= __initialized
;
247 _ASSERTE(__native_startup_state
== __initialized
);
249 (VOID
)InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
251 if (__dyn_tls_init_callback
!= NULL
)
252 __dyn_tls_init_callback (NULL
, DLL_THREAD_ATTACH
, NULL
);
254 _pei386_runtime_relocator ();
255 __mingw_oldexcpt_handler
= SetUnhandledExceptionFilter (_gnu_exception_handler
);
257 __mingw_init_ehandler ();
259 __mingw_prepare_except_for_msvcr80_and_higher ();
266 lpszCommandLine
= (_TCHAR
*) _wcmdln
;
268 lpszCommandLine
= (char *) _acmdln
;
270 while (*lpszCommandLine
> SPACECHAR
|| (*lpszCommandLine
&& inDoubleQuote
))
272 if (*lpszCommandLine
== DQUOTECHAR
)
273 inDoubleQuote
= !inDoubleQuote
;
275 if (_ismbblead (*lpszCommandLine
))
277 if (lpszCommandLine
) /* FIXME: Why this check? Should I check for *lpszCommandLine != 0 too? */
283 while (*lpszCommandLine
&& (*lpszCommandLine
<= SPACECHAR
))
286 __mingw_winmain_hInstance
= (HINSTANCE
) &__ImageBase
;
287 __mingw_winmain_lpCmdLine
= lpszCommandLine
;
288 __mingw_winmain_nShowCmd
= StartupInfo
.dwFlags
& STARTF_USESHOWWINDOW
?
289 StartupInfo
.wShowWindow
: SW_SHOWDEFAULT
;
291 duplicate_ppstrings (argc
, &argv
);
294 #if !defined(__arm__) && !defined(__aarch64__)
297 /* C++ initialization.
298 gcc inserts this call automatically for a function called main, but not for wmain. */
299 mainret
= wmain (argc
, argv
, envp
);
301 #if !defined(__arm__) && !defined(__aarch64__)
304 mainret
= main (argc
, argv
, envp
);
320 extern int mingw_initltsdrot_force
;
321 extern int mingw_initltsdyn_force
;
322 extern int mingw_initltssuo_force
;
323 extern int mingw_initcharmax
;
326 check_managed_app (void)
328 PIMAGE_DOS_HEADER pDOSHeader
;
329 PIMAGE_NT_HEADERS pPEHeader
;
330 PIMAGE_OPTIONAL_HEADER32 pNTHeader32
;
331 PIMAGE_OPTIONAL_HEADER64 pNTHeader64
;
333 /* Force to be linked. */
334 mingw_initltsdrot_force
=1;
335 mingw_initltsdyn_force
=1;
336 mingw_initltssuo_force
=1;
339 pDOSHeader
= (PIMAGE_DOS_HEADER
) &__ImageBase
;
340 if (pDOSHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
343 pPEHeader
= (PIMAGE_NT_HEADERS
)((char *)pDOSHeader
+ pDOSHeader
->e_lfanew
);
344 if (pPEHeader
->Signature
!= IMAGE_NT_SIGNATURE
)
347 pNTHeader32
= (PIMAGE_OPTIONAL_HEADER32
) &pPEHeader
->OptionalHeader
;
348 switch (pNTHeader32
->Magic
)
350 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
351 if (pNTHeader32
->NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
)
353 return !! pNTHeader32
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
;
354 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
355 pNTHeader64
= (PIMAGE_OPTIONAL_HEADER64
)pNTHeader32
;
356 if (pNTHeader64
->NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
)
358 return !! pNTHeader64
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
;
364 static size_t wbytelen(const wchar_t *p
)
372 static void duplicate_ppstrings (int ac
, wchar_t ***av
)
376 wchar_t **n
= (wchar_t **) malloc (sizeof (wchar_t *) * (ac
+ 1));
379 for (i
=0; i
< ac
; i
++)
381 size_t l
= wbytelen (avl
[i
]);
382 n
[i
] = (wchar_t *) malloc (l
);
383 memcpy (n
[i
], avl
[i
], l
);
389 static void duplicate_ppstrings (int ac
, char ***av
)
393 char **n
= (char **) malloc (sizeof (char *) * (ac
+ 1));
396 for (i
=0; i
< ac
; i
++)
398 size_t l
= strlen (avl
[i
]) + 1;
399 n
[i
] = (char *) malloc (l
);
400 memcpy (n
[i
], avl
[i
], l
);
407 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION
408 #define __UNUSED_PARAM_1(x) x
410 #define __UNUSED_PARAM_1 __UNUSED_PARAM
413 __mingw_invalidParameterHandler (const wchar_t * __UNUSED_PARAM_1(expression
),
414 const wchar_t * __UNUSED_PARAM_1(function
),
415 const wchar_t * __UNUSED_PARAM_1(file
),
416 unsigned int __UNUSED_PARAM_1(line
),
417 uintptr_t __UNUSED_PARAM(pReserved
))
419 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION
420 wprintf(L
"Invalid parameter detected in function %s. File: %s Line: %u\n", function
, file
, line
);
421 wprintf(L
"Expression: %s\n", expression
);
425 HANDLE
__mingw_get_msvcrt_handle(void);
428 __mingw_prepare_except_for_msvcr80_and_higher (void)
430 _invalid_parameter_handler (*fIPH
)(_invalid_parameter_handler
) = NULL
;
432 fIPH
= (void*)GetProcAddress (__mingw_get_msvcrt_handle(), "_set_invalid_parameter_handler");
434 (*fIPH
)(__mingw_invalidParameterHandler
);