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
);
132 if (__globallocalestatus
== -1)
141 startinfo
.newmode
= _newmode
;
144 argret
= __wgetmainargs(&argc
,&argv
,&envp
,_dowildcard
,&startinfo
);
146 argret
= __getmainargs(&argc
,&argv
,&envp
,_dowildcard
,&startinfo
);
150 static int __cdecl
__tmainCRTStartup (void);
152 int __cdecl
WinMainCRTStartup (void);
154 int __cdecl
WinMainCRTStartup (void)
158 asm ("\t.l_startw:\n"
159 "\t.seh_handler __C_specific_handler, @except\n"
160 "\t.seh_handlerdata\n"
162 "\t.rva .l_startw, .l_endw, _gnu_exception_handler ,.l_endw\n"
167 __security_init_cookie ();
168 ret
= __tmainCRTStartup ();
176 int __cdecl
mainCRTStartup (void);
179 int __mingw_init_ehandler (void);
182 int __cdecl
mainCRTStartup (void)
187 "\t.seh_handler __C_specific_handler, @except\n"
188 "\t.seh_handlerdata\n"
190 "\t.rva .l_start, .l_end, _gnu_exception_handler ,.l_end\n"
195 __security_init_cookie ();
196 ret
= __tmainCRTStartup ();
207 __tmainCRTStartup (void)
209 _TCHAR
*lpszCommandLine
= NULL
;
210 STARTUPINFO StartupInfo
;
211 WINBOOL inDoubleQuote
= FALSE
;
212 memset (&StartupInfo
, 0, sizeof (STARTUPINFO
));
215 /* We need to make sure that this function is build with frame-pointer
216 and that we align the stack to 16 bytes for the sake of SSE ops in main
217 or in functions inlined into main. */
218 lpszCommandLine
= (_TCHAR
*) alloca (32);
219 memset (lpszCommandLine
, 0xcc, 32);
221 asm __volatile__ ("andl $-16, %%esp" : : : "%esp");
226 GetStartupInfo (&StartupInfo
);
228 void *lock_free
= NULL
;
229 void *fiberid
= ((PNT_TIB
)NtCurrentTeb())->StackBase
;
231 while((lock_free
= InterlockedCompareExchangePointer ((volatile PVOID
*) &__native_startup_lock
,
234 if (lock_free
== fiberid
)
241 if (__native_startup_state
== __initializing
)
245 else if (__native_startup_state
== __uninitialized
)
247 __native_startup_state
= __initializing
;
248 _initterm ((_PVFV
*)(void *)__xi_a
, (_PVFV
*)(void *) __xi_z
);
253 if (__native_startup_state
== __initializing
)
255 _initterm (__xc_a
, __xc_z
);
256 __native_startup_state
= __initialized
;
258 _ASSERTE(__native_startup_state
== __initialized
);
260 (VOID
)InterlockedExchangePointer ((volatile PVOID
*) &__native_startup_lock
, 0);
262 if (__dyn_tls_init_callback
!= NULL
)
263 __dyn_tls_init_callback (NULL
, DLL_THREAD_ATTACH
, NULL
);
265 _pei386_runtime_relocator ();
266 __mingw_oldexcpt_handler
= SetUnhandledExceptionFilter (_gnu_exception_handler
);
268 __mingw_init_ehandler ();
270 __mingw_prepare_except_for_msvcr80_and_higher ();
277 lpszCommandLine
= (_TCHAR
*) _wcmdln
;
279 lpszCommandLine
= (char *) _acmdln
;
281 while (*lpszCommandLine
> SPACECHAR
|| (*lpszCommandLine
&& inDoubleQuote
))
283 if (*lpszCommandLine
== DQUOTECHAR
)
284 inDoubleQuote
= !inDoubleQuote
;
286 if (_ismbblead (*lpszCommandLine
))
288 if (lpszCommandLine
) /* FIXME: Why this check? Should I check for *lpszCommandLine != 0 too? */
294 while (*lpszCommandLine
&& (*lpszCommandLine
<= SPACECHAR
))
297 __mingw_winmain_hInstance
= (HINSTANCE
) &__ImageBase
;
298 __mingw_winmain_lpCmdLine
= lpszCommandLine
;
299 __mingw_winmain_nShowCmd
= StartupInfo
.dwFlags
& STARTF_USESHOWWINDOW
?
300 StartupInfo
.wShowWindow
: SW_SHOWDEFAULT
;
302 duplicate_ppstrings (argc
, &argv
);
306 /* C++ initialization.
307 gcc inserts this call automatically for a function called main, but not for wmain. */
308 mainret
= wmain (argc
, argv
, envp
);
311 mainret
= main (argc
, argv
, envp
);
322 extern int mingw_initltsdrot_force
;
323 extern int mingw_initltsdyn_force
;
324 extern int mingw_initltssuo_force
;
325 extern int mingw_initcharmax
;
328 check_managed_app (void)
330 PIMAGE_DOS_HEADER pDOSHeader
;
331 PIMAGE_NT_HEADERS pPEHeader
;
332 PIMAGE_OPTIONAL_HEADER32 pNTHeader32
;
333 PIMAGE_OPTIONAL_HEADER64 pNTHeader64
;
335 /* Force to be linked. */
336 mingw_initltsdrot_force
=1;
337 mingw_initltsdyn_force
=1;
338 mingw_initltssuo_force
=1;
341 pDOSHeader
= (PIMAGE_DOS_HEADER
) &__ImageBase
;
342 if (pDOSHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
345 pPEHeader
= (PIMAGE_NT_HEADERS
)((char *)pDOSHeader
+ pDOSHeader
->e_lfanew
);
346 if (pPEHeader
->Signature
!= IMAGE_NT_SIGNATURE
)
349 pNTHeader32
= (PIMAGE_OPTIONAL_HEADER32
) &pPEHeader
->OptionalHeader
;
350 switch (pNTHeader32
->Magic
)
352 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
353 if (pNTHeader32
->NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
)
355 return !! pNTHeader32
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
;
356 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
357 pNTHeader64
= (PIMAGE_OPTIONAL_HEADER64
)pNTHeader32
;
358 if (pNTHeader64
->NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
)
360 return !! pNTHeader64
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
;
366 static size_t wbytelen(const wchar_t *p
)
374 static void duplicate_ppstrings (int ac
, wchar_t ***av
)
378 wchar_t **n
= (wchar_t **) malloc (sizeof (wchar_t *) * (ac
+ 1));
381 for (i
=0; i
< ac
; i
++)
383 size_t l
= wbytelen (avl
[i
]);
384 n
[i
] = (wchar_t *) malloc (l
);
385 memcpy (n
[i
], avl
[i
], l
);
391 static void duplicate_ppstrings (int ac
, char ***av
)
395 char **n
= (char **) malloc (sizeof (char *) * (ac
+ 1));
398 for (i
=0; i
< ac
; i
++)
400 size_t l
= strlen (avl
[i
]) + 1;
401 n
[i
] = (char *) malloc (l
);
402 memcpy (n
[i
], avl
[i
], l
);
409 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION
410 #define __UNUSED_PARAM_1(x) x
412 #define __UNUSED_PARAM_1 __UNUSED_PARAM
415 __mingw_invalidParameterHandler (const wchar_t * __UNUSED_PARAM_1(expression
),
416 const wchar_t * __UNUSED_PARAM_1(function
),
417 const wchar_t * __UNUSED_PARAM_1(file
),
418 unsigned int __UNUSED_PARAM_1(line
),
419 uintptr_t __UNUSED_PARAM(pReserved
))
421 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION
422 wprintf(L
"Invalid parameter detected in function %s. File: %s Line: %u\n", function
, file
, line
);
423 wprintf(L
"Expression: %s\n", expression
);
427 HANDLE
__mingw_get_msvcrt_handle(void);
430 __mingw_prepare_except_for_msvcr80_and_higher (void)
432 _invalid_parameter_handler (*fIPH
)(_invalid_parameter_handler
) = NULL
;
434 fIPH
= (void*)GetProcAddress (__mingw_get_msvcrt_handle(), "_set_invalid_parameter_handler");
436 (*fIPH
)(__mingw_invalidParameterHandler
);