1 /** 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. 5 */ 6 7 #undef CRTDLL 8 #ifndef _DLL 9 #define _DLL 10 #endif 11 12 #define SPECIAL_CRTEXE 13 14 #include <oscalls.h> 15 #include <internal.h> 16 #include <process.h> 17 #include <signal.h> 18 #include <math.h> 19 #include <stdlib.h> 20 #include <tchar.h> 21 #include <sect_attribs.h> 22 #include <locale.h> 23 #ifdef _MBCS 24 #include <mbstring.h> 25 #endif 26 27 /* Special handling for ARM & ARM64, __winitenv & __initenv aren't present there. */ 28 29 #if !defined(__arm__) && !defined(__aarch64__) 30 31 #ifndef __winitenv 32 extern wchar_t *** __MINGW_IMP_SYMBOL(__winitenv); 33 #define __winitenv (* __MINGW_IMP_SYMBOL(__winitenv)) 34 #endif 35 36 #ifndef __initenv 37 extern char *** __MINGW_IMP_SYMBOL(__initenv); 38 #define __initenv (* __MINGW_IMP_SYMBOL(__initenv)) 39 #endif 40 41 #endif 42 43 /* Hack, for bug in ld. Will be removed soon. */ 44 #if defined(__GNUC__) 45 #define __ImageBase __MINGW_LSYMBOL(_image_base__) 46 #endif 47 48 /* This symbol is defined by ld. */ 49 extern IMAGE_DOS_HEADER __ImageBase; 50 51 extern void __cdecl _fpreset (void); 52 #define SPACECHAR _T(' ') 53 #define DQUOTECHAR _T('\"') 54 55 extern int * __MINGW_IMP_SYMBOL(_fmode); 56 extern int * __MINGW_IMP_SYMBOL(_commode); 57 58 #undef _fmode 59 extern int _fmode; 60 extern int * __MINGW_IMP_SYMBOL(_commode); 61 #define _commode (* __MINGW_IMP_SYMBOL(_commode)) 62 extern int _dowildcard; 63 64 extern _CRTIMP void __cdecl _initterm(_PVFV *, _PVFV *); 65 66 static int __cdecl check_managed_app (void); 67 68 extern _CRTALLOC(".CRT$XIA") _PIFV __xi_a[]; 69 extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z[]; 70 extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a[]; 71 extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z[]; 72 73 /* TLS initialization hook. */ 74 extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback; 75 76 extern _PVFV *__onexitbegin; 77 extern _PVFV *__onexitend; 78 79 extern int mingw_app_type; 80 81 HINSTANCE __mingw_winmain_hInstance; 82 _TCHAR *__mingw_winmain_lpCmdLine; 83 DWORD __mingw_winmain_nShowCmd; 84 85 static int argc; 86 extern void __main(void); 87 #ifdef WPRFLAG 88 static wchar_t **argv; 89 static wchar_t **envp; 90 #else 91 static char **argv; 92 static char **envp; 93 #endif 94 95 static int argret; 96 static int mainret=0; 97 static int managedapp; 98 static int has_cctor = 0; 99 static _startupinfo startinfo; 100 extern LPTOP_LEVEL_EXCEPTION_FILTER __mingw_oldexcpt_handler; 101 102 extern void _pei386_runtime_relocator (void); 103 long CALLBACK _gnu_exception_handler (EXCEPTION_POINTERS * exception_data); 104 #ifdef WPRFLAG 105 static void duplicate_ppstrings (int ac, wchar_t ***av); 106 #else 107 static void duplicate_ppstrings (int ac, char ***av); 108 #endif 109 110 static int __cdecl pre_c_init (void); 111 static void __cdecl pre_cpp_init (void); 112 static void __cdecl __mingw_prepare_except_for_msvcr80_and_higher (void); 113 _CRTALLOC(".CRT$XIAA") _PIFV mingw_pcinit = pre_c_init; 114 _CRTALLOC(".CRT$XCAA") _PVFV mingw_pcppinit = pre_cpp_init; 115 116 extern int _MINGW_INSTALL_DEBUG_MATHERR; 117 118 static int __cdecl 119 pre_c_init (void) 120 { 121 managedapp = check_managed_app (); 122 if (mingw_app_type) 123 __set_app_type(_GUI_APP); 124 else 125 __set_app_type (_CONSOLE_APP); 126 __onexitbegin = __onexitend = (_PVFV *) _encode_pointer ((_PVFV *)(-1)); 127 128 * __MINGW_IMP_SYMBOL(_fmode) = _fmode; 129 * __MINGW_IMP_SYMBOL(_commode) = _commode; 130 131 #ifdef WPRFLAG 132 _wsetargv(); 133 #else 134 _setargv(); 135 #endif 136 if (_MINGW_INSTALL_DEBUG_MATHERR == 1) 137 { 138 __setusermatherr (_matherr); 139 } 140 #ifndef __clang__ /* FIXME: CORE-14042 */ 141 if (__globallocalestatus == -1) 142 { 143 } 144 #endif 145 return 0; 146 } 147 148 static void __cdecl 149 pre_cpp_init (void) 150 { 151 startinfo.newmode = _newmode; 152 153 #ifdef WPRFLAG 154 argret = __wgetmainargs(&argc,&argv,&envp,_dowildcard,&startinfo); 155 #else 156 argret = __getmainargs(&argc,&argv,&envp,_dowildcard,&startinfo); 157 #endif 158 } 159 160 static int __cdecl __tmainCRTStartup (void); 161 162 int __cdecl WinMainCRTStartup (void); 163 164 int __cdecl WinMainCRTStartup (void) 165 { 166 int ret = 255; 167 #ifdef __SEH__ 168 asm ("\t.l_startw:\n" 169 "\t.seh_handler __C_specific_handler, @except\n" 170 "\t.seh_handlerdata\n" 171 "\t.long 1\n" 172 "\t.rva .l_startw, .l_endw, _gnu_exception_handler ,.l_endw\n" 173 "\t.text" 174 ); 175 #endif 176 mingw_app_type = 1; 177 __security_init_cookie (); 178 ret = __tmainCRTStartup (); 179 #ifdef __SEH__ 180 asm ("\tnop\n" 181 "\t.l_endw: nop\n"); 182 #endif 183 return ret; 184 } 185 186 int __cdecl mainCRTStartup (void); 187 188 #ifdef _WIN64 189 int __mingw_init_ehandler (void); 190 #endif 191 192 int __cdecl mainCRTStartup (void) 193 { 194 int ret = 255; 195 #ifdef __SEH__ 196 asm ("\t.l_start:\n" 197 "\t.seh_handler __C_specific_handler, @except\n" 198 "\t.seh_handlerdata\n" 199 "\t.long 1\n" 200 "\t.rva .l_start, .l_end, _gnu_exception_handler ,.l_end\n" 201 "\t.text" 202 ); 203 #endif 204 mingw_app_type = 0; 205 __security_init_cookie (); 206 ret = __tmainCRTStartup (); 207 #ifdef __SEH__ 208 asm ("\tnop\n" 209 "\t.l_end: nop\n"); 210 #endif 211 return ret; 212 } 213 214 void __call_atexit(); 215 216 static 217 __declspec(noinline) 218 int __cdecl 219 __tmainCRTStartup (void) 220 { 221 _TCHAR *lpszCommandLine = NULL; 222 STARTUPINFO StartupInfo; 223 WINBOOL inDoubleQuote = FALSE; 224 memset (&StartupInfo, 0, sizeof (STARTUPINFO)); 225 226 #ifndef _WIN64 227 /* We need to make sure that this function is build with frame-pointer 228 and that we align the stack to 16 bytes for the sake of SSE ops in main 229 or in functions inlined into main. */ 230 lpszCommandLine = (_TCHAR *) alloca (32); 231 memset (lpszCommandLine, 0xcc, 32); 232 #ifdef __GNUC__ 233 asm __volatile__ ("andl $-16, %%esp" : : : "%esp"); 234 #endif 235 #endif 236 237 if (mingw_app_type) 238 GetStartupInfo (&StartupInfo); 239 { 240 void *lock_free = NULL; 241 void *fiberid = ((PNT_TIB)NtCurrentTeb())->StackBase; 242 int nested = FALSE; 243 while((lock_free = InterlockedCompareExchangePointer ((volatile PVOID *) &__native_startup_lock, 244 fiberid, 0)) != 0) 245 { 246 if (lock_free == fiberid) 247 { 248 nested = TRUE; 249 break; 250 } 251 Sleep(1000); 252 } 253 if (__native_startup_state == __initializing) 254 { 255 _amsg_exit (31); 256 } 257 else if (__native_startup_state == __uninitialized) 258 { 259 __native_startup_state = __initializing; 260 _initterm ((_PVFV *)(void *)__xi_a, (_PVFV *)(void *) __xi_z); 261 } 262 else 263 has_cctor = 1; 264 265 if (__native_startup_state == __initializing) 266 { 267 _initterm (__xc_a, __xc_z); 268 __native_startup_state = __initialized; 269 } 270 _ASSERTE(__native_startup_state == __initialized); 271 if (! nested) 272 (VOID)InterlockedExchangePointer ((volatile PVOID *) &__native_startup_lock, 0); 273 274 if (__dyn_tls_init_callback != NULL) 275 __dyn_tls_init_callback (NULL, DLL_THREAD_ATTACH, NULL); 276 277 _pei386_runtime_relocator (); 278 __mingw_oldexcpt_handler = SetUnhandledExceptionFilter (_gnu_exception_handler); 279 #ifdef _WIN64 280 __mingw_init_ehandler (); 281 #endif 282 __mingw_prepare_except_for_msvcr80_and_higher (); 283 284 _fpreset (); 285 286 if (mingw_app_type) 287 { 288 #ifdef WPRFLAG 289 lpszCommandLine = (_TCHAR *) _wcmdln; 290 #else 291 lpszCommandLine = (char *) _acmdln; 292 #endif 293 while (*lpszCommandLine > SPACECHAR || (*lpszCommandLine && inDoubleQuote)) 294 { 295 if (*lpszCommandLine == DQUOTECHAR) 296 inDoubleQuote = !inDoubleQuote; 297 #ifdef _MBCS 298 if (_ismbblead (*lpszCommandLine)) 299 { 300 if (lpszCommandLine) /* FIXME: Why this check? Should I check for *lpszCommandLine != 0 too? */ 301 lpszCommandLine++; 302 } 303 #endif 304 ++lpszCommandLine; 305 } 306 while (*lpszCommandLine && (*lpszCommandLine <= SPACECHAR)) 307 lpszCommandLine++; 308 309 __mingw_winmain_hInstance = (HINSTANCE) &__ImageBase; 310 __mingw_winmain_lpCmdLine = lpszCommandLine; 311 __mingw_winmain_nShowCmd = StartupInfo.dwFlags & STARTF_USESHOWWINDOW ? 312 StartupInfo.wShowWindow : SW_SHOWDEFAULT; 313 } 314 duplicate_ppstrings (argc, &argv); 315 __main (); 316 #ifdef WPRFLAG 317 #if !defined(__arm__) && !defined(__aarch64__) 318 __winitenv = envp; 319 #endif 320 /* C++ initialization. 321 gcc inserts this call automatically for a function called main, but not for wmain. */ 322 mainret = wmain (argc, argv, envp); 323 #else 324 #if !defined(__arm__) && !defined(__aarch64__) 325 __initenv = envp; 326 #endif 327 mainret = main (argc, argv, envp); 328 #endif 329 __call_atexit(); 330 if (!managedapp) 331 exit (mainret); 332 333 if (has_cctor == 0) 334 _cexit (); 335 } 336 return mainret; 337 } 338 339 extern int mingw_initltsdrot_force; 340 extern int mingw_initltsdyn_force; 341 extern int mingw_initltssuo_force; 342 extern int mingw_initcharmax; 343 344 static int __cdecl 345 check_managed_app (void) 346 { 347 PIMAGE_DOS_HEADER pDOSHeader; 348 PIMAGE_NT_HEADERS pPEHeader; 349 PIMAGE_OPTIONAL_HEADER32 pNTHeader32; 350 PIMAGE_OPTIONAL_HEADER64 pNTHeader64; 351 352 /* Force to be linked. */ 353 mingw_initltsdrot_force=1; 354 mingw_initltsdyn_force=1; 355 mingw_initltssuo_force=1; 356 mingw_initcharmax=1; 357 358 pDOSHeader = (PIMAGE_DOS_HEADER) &__ImageBase; 359 if (pDOSHeader->e_magic != IMAGE_DOS_SIGNATURE) 360 return 0; 361 362 pPEHeader = (PIMAGE_NT_HEADERS)((char *)pDOSHeader + pDOSHeader->e_lfanew); 363 if (pPEHeader->Signature != IMAGE_NT_SIGNATURE) 364 return 0; 365 366 pNTHeader32 = (PIMAGE_OPTIONAL_HEADER32) &pPEHeader->OptionalHeader; 367 switch (pNTHeader32->Magic) 368 { 369 case IMAGE_NT_OPTIONAL_HDR32_MAGIC: 370 if (pNTHeader32->NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR) 371 return 0; 372 return !! pNTHeader32->DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress; 373 case IMAGE_NT_OPTIONAL_HDR64_MAGIC: 374 pNTHeader64 = (PIMAGE_OPTIONAL_HEADER64)pNTHeader32; 375 if (pNTHeader64->NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR) 376 return 0; 377 return !! pNTHeader64->DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress; 378 } 379 return 0; 380 } 381 382 #ifdef WPRFLAG 383 static size_t wbytelen(const wchar_t *p) 384 { 385 size_t ret = 1; 386 while (*p!=0) { 387 ret++,++p; 388 } 389 return ret*2; 390 } 391 static void duplicate_ppstrings (int ac, wchar_t ***av) 392 { 393 wchar_t **avl; 394 int i; 395 wchar_t **n = (wchar_t **) malloc (sizeof (wchar_t *) * (ac + 1)); 396 397 avl=*av; 398 for (i=0; i < ac; i++) 399 { 400 size_t l = wbytelen (avl[i]); 401 n[i] = (wchar_t *) malloc (l); 402 memcpy (n[i], avl[i], l); 403 } 404 n[i] = NULL; 405 *av = n; 406 } 407 #else 408 static void duplicate_ppstrings (int ac, char ***av) 409 { 410 char **avl; 411 int i; 412 char **n = (char **) malloc (sizeof (char *) * (ac + 1)); 413 414 avl=*av; 415 for (i=0; i < ac; i++) 416 { 417 size_t l = strlen (avl[i]) + 1; 418 n[i] = (char *) malloc (l); 419 memcpy (n[i], avl[i], l); 420 } 421 n[i] = NULL; 422 *av = n; 423 } 424 #endif 425 426 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION 427 #define __UNUSED_PARAM_1(x) x 428 #else 429 #define __UNUSED_PARAM_1 __UNUSED_PARAM 430 #endif 431 static void __cdecl 432 __mingw_invalidParameterHandler (const wchar_t * __UNUSED_PARAM_1(expression), 433 const wchar_t * __UNUSED_PARAM_1(function), 434 const wchar_t * __UNUSED_PARAM_1(file), 435 unsigned int __UNUSED_PARAM_1(line), 436 uintptr_t __UNUSED_PARAM(pReserved)) 437 { 438 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION 439 wprintf(L"Invalid parameter detected in function %s. File: %s Line: %u\n", function, file, line); 440 wprintf(L"Expression: %s\n", expression); 441 #endif 442 } 443 444 HANDLE __mingw_get_msvcrt_handle(void); 445 446 static void __cdecl 447 __mingw_prepare_except_for_msvcr80_and_higher (void) 448 { 449 _invalid_parameter_handler (*fIPH)(_invalid_parameter_handler) = NULL; 450 451 fIPH = (void*)GetProcAddress (__mingw_get_msvcrt_handle(), "_set_invalid_parameter_handler"); 452 if (fIPH) 453 (*fIPH)(__mingw_invalidParameterHandler); 454 } 455