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 static 215 __declspec(noinline) 216 int __cdecl 217 __tmainCRTStartup (void) 218 { 219 _TCHAR *lpszCommandLine = NULL; 220 STARTUPINFO StartupInfo; 221 WINBOOL inDoubleQuote = FALSE; 222 memset (&StartupInfo, 0, sizeof (STARTUPINFO)); 223 224 #ifndef _WIN64 225 /* We need to make sure that this function is build with frame-pointer 226 and that we align the stack to 16 bytes for the sake of SSE ops in main 227 or in functions inlined into main. */ 228 lpszCommandLine = (_TCHAR *) alloca (32); 229 memset (lpszCommandLine, 0xcc, 32); 230 #ifdef __GNUC__ 231 asm __volatile__ ("andl $-16, %%esp" : : : "%esp"); 232 #endif 233 #endif 234 235 if (mingw_app_type) 236 GetStartupInfo (&StartupInfo); 237 { 238 void *lock_free = NULL; 239 void *fiberid = ((PNT_TIB)NtCurrentTeb())->StackBase; 240 int nested = FALSE; 241 while((lock_free = InterlockedCompareExchangePointer ((volatile PVOID *) &__native_startup_lock, 242 fiberid, 0)) != 0) 243 { 244 if (lock_free == fiberid) 245 { 246 nested = TRUE; 247 break; 248 } 249 Sleep(1000); 250 } 251 if (__native_startup_state == __initializing) 252 { 253 _amsg_exit (31); 254 } 255 else if (__native_startup_state == __uninitialized) 256 { 257 __native_startup_state = __initializing; 258 _initterm ((_PVFV *)(void *)__xi_a, (_PVFV *)(void *) __xi_z); 259 } 260 else 261 has_cctor = 1; 262 263 if (__native_startup_state == __initializing) 264 { 265 _initterm (__xc_a, __xc_z); 266 __native_startup_state = __initialized; 267 } 268 _ASSERTE(__native_startup_state == __initialized); 269 if (! nested) 270 (VOID)InterlockedExchangePointer ((volatile PVOID *) &__native_startup_lock, 0); 271 272 if (__dyn_tls_init_callback != NULL) 273 __dyn_tls_init_callback (NULL, DLL_THREAD_ATTACH, NULL); 274 275 _pei386_runtime_relocator (); 276 __mingw_oldexcpt_handler = SetUnhandledExceptionFilter (_gnu_exception_handler); 277 #ifdef _WIN64 278 __mingw_init_ehandler (); 279 #endif 280 __mingw_prepare_except_for_msvcr80_and_higher (); 281 282 _fpreset (); 283 284 if (mingw_app_type) 285 { 286 #ifdef WPRFLAG 287 lpszCommandLine = (_TCHAR *) _wcmdln; 288 #else 289 lpszCommandLine = (char *) _acmdln; 290 #endif 291 while (*lpszCommandLine > SPACECHAR || (*lpszCommandLine && inDoubleQuote)) 292 { 293 if (*lpszCommandLine == DQUOTECHAR) 294 inDoubleQuote = !inDoubleQuote; 295 #ifdef _MBCS 296 if (_ismbblead (*lpszCommandLine)) 297 { 298 if (lpszCommandLine) /* FIXME: Why this check? Should I check for *lpszCommandLine != 0 too? */ 299 lpszCommandLine++; 300 } 301 #endif 302 ++lpszCommandLine; 303 } 304 while (*lpszCommandLine && (*lpszCommandLine <= SPACECHAR)) 305 lpszCommandLine++; 306 307 __mingw_winmain_hInstance = (HINSTANCE) &__ImageBase; 308 __mingw_winmain_lpCmdLine = lpszCommandLine; 309 __mingw_winmain_nShowCmd = StartupInfo.dwFlags & STARTF_USESHOWWINDOW ? 310 StartupInfo.wShowWindow : SW_SHOWDEFAULT; 311 } 312 duplicate_ppstrings (argc, &argv); 313 __main (); 314 #ifdef WPRFLAG 315 #if !defined(__arm__) && !defined(__aarch64__) 316 __winitenv = envp; 317 #endif 318 /* C++ initialization. 319 gcc inserts this call automatically for a function called main, but not for wmain. */ 320 mainret = wmain (argc, argv, envp); 321 #else 322 #if !defined(__arm__) && !defined(__aarch64__) 323 __initenv = envp; 324 #endif 325 mainret = main (argc, argv, envp); 326 #endif 327 if (!managedapp) 328 exit (mainret); 329 330 if (has_cctor == 0) 331 _cexit (); 332 } 333 return mainret; 334 } 335 336 extern int mingw_initltsdrot_force; 337 extern int mingw_initltsdyn_force; 338 extern int mingw_initltssuo_force; 339 extern int mingw_initcharmax; 340 341 static int __cdecl 342 check_managed_app (void) 343 { 344 PIMAGE_DOS_HEADER pDOSHeader; 345 PIMAGE_NT_HEADERS pPEHeader; 346 PIMAGE_OPTIONAL_HEADER32 pNTHeader32; 347 PIMAGE_OPTIONAL_HEADER64 pNTHeader64; 348 349 /* Force to be linked. */ 350 mingw_initltsdrot_force=1; 351 mingw_initltsdyn_force=1; 352 mingw_initltssuo_force=1; 353 mingw_initcharmax=1; 354 355 pDOSHeader = (PIMAGE_DOS_HEADER) &__ImageBase; 356 if (pDOSHeader->e_magic != IMAGE_DOS_SIGNATURE) 357 return 0; 358 359 pPEHeader = (PIMAGE_NT_HEADERS)((char *)pDOSHeader + pDOSHeader->e_lfanew); 360 if (pPEHeader->Signature != IMAGE_NT_SIGNATURE) 361 return 0; 362 363 pNTHeader32 = (PIMAGE_OPTIONAL_HEADER32) &pPEHeader->OptionalHeader; 364 switch (pNTHeader32->Magic) 365 { 366 case IMAGE_NT_OPTIONAL_HDR32_MAGIC: 367 if (pNTHeader32->NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR) 368 return 0; 369 return !! pNTHeader32->DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress; 370 case IMAGE_NT_OPTIONAL_HDR64_MAGIC: 371 pNTHeader64 = (PIMAGE_OPTIONAL_HEADER64)pNTHeader32; 372 if (pNTHeader64->NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR) 373 return 0; 374 return !! pNTHeader64->DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress; 375 } 376 return 0; 377 } 378 379 #ifdef WPRFLAG 380 static size_t wbytelen(const wchar_t *p) 381 { 382 size_t ret = 1; 383 while (*p!=0) { 384 ret++,++p; 385 } 386 return ret*2; 387 } 388 static void duplicate_ppstrings (int ac, wchar_t ***av) 389 { 390 wchar_t **avl; 391 int i; 392 wchar_t **n = (wchar_t **) malloc (sizeof (wchar_t *) * (ac + 1)); 393 394 avl=*av; 395 for (i=0; i < ac; i++) 396 { 397 size_t l = wbytelen (avl[i]); 398 n[i] = (wchar_t *) malloc (l); 399 memcpy (n[i], avl[i], l); 400 } 401 n[i] = NULL; 402 *av = n; 403 } 404 #else 405 static void duplicate_ppstrings (int ac, char ***av) 406 { 407 char **avl; 408 int i; 409 char **n = (char **) malloc (sizeof (char *) * (ac + 1)); 410 411 avl=*av; 412 for (i=0; i < ac; i++) 413 { 414 size_t l = strlen (avl[i]) + 1; 415 n[i] = (char *) malloc (l); 416 memcpy (n[i], avl[i], l); 417 } 418 n[i] = NULL; 419 *av = n; 420 } 421 #endif 422 423 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION 424 #define __UNUSED_PARAM_1(x) x 425 #else 426 #define __UNUSED_PARAM_1 __UNUSED_PARAM 427 #endif 428 static void __cdecl 429 __mingw_invalidParameterHandler (const wchar_t * __UNUSED_PARAM_1(expression), 430 const wchar_t * __UNUSED_PARAM_1(function), 431 const wchar_t * __UNUSED_PARAM_1(file), 432 unsigned int __UNUSED_PARAM_1(line), 433 uintptr_t __UNUSED_PARAM(pReserved)) 434 { 435 #ifdef __MINGW_SHOW_INVALID_PARAMETER_EXCEPTION 436 wprintf(L"Invalid parameter detected in function %s. File: %s Line: %u\n", function, file, line); 437 wprintf(L"Expression: %s\n", expression); 438 #endif 439 } 440 441 HANDLE __mingw_get_msvcrt_handle(void); 442 443 static void __cdecl 444 __mingw_prepare_except_for_msvcr80_and_higher (void) 445 { 446 _invalid_parameter_handler (*fIPH)(_invalid_parameter_handler) = NULL; 447 448 fIPH = (void*)GetProcAddress (__mingw_get_msvcrt_handle(), "_set_invalid_parameter_handler"); 449 if (fIPH) 450 (*fIPH)(__mingw_invalidParameterHandler); 451 } 452