/*---------------------------------------------------------------------*

Name            _dos_keep - exits and remains resident

Usage           void _dos_keep(unsigned status, unsigned size);

Prototype in    dos.h

Description     _dos_keep returns to MS-DOS with the exit status in
                status. The current program remains resident, however. The
                program is set to size paragraphs in length, and the
                remainder of the memory of the program is freed.

                _dos_keep does the same cleanup as _cexit.  It flushes
                files, calls atexit and #pragma exit routines, and
                restores interrupt vectors taken by the library.
                It does not close files; the caller must explicitly
                close any open files, if necessary.

                _dos_keep can be used when installing a TSR program.
                _dos_keep uses DOS function 0x31.

Return value    None

*---------------------------------------------------------------------*/
void _dos_keep(unsigned char status, unsigned size)
{
        _cexit();
        _DX = size;
        _AL = status;
        _AH = 0x31;
        geninterrupt(0x21);
}
Пример #2
0
ptr stack::pop(void)
{    ptr p;
	 if(top==null){
	 cout<<"\n there are no elements in the stack";
	 _cexit();}
  else{ if(no==2){p=top; top=top->next;top->next=null;no--;return(p);}
       else{p=top; top=top->next;no--;return(p);}
  }
 }
Пример #3
0
void cmsSignalError(int ErrorCode, const char *ErrorText, ...)
{
       va_list args;

       if (nDoAbort == LCMS_ERROR_IGNORE) return;

        va_start(args, ErrorText);

        if (UserErrorHandler != NULL) {

            char Buffer[1024];

            vsnprintf(Buffer, 1023, ErrorText, args);
            va_end(args);

            if (UserErrorHandler(ErrorCode, Buffer)) {

                return;
                }
         }

#if defined( __CONSOLE__ ) || defined( NON_WINDOWS )

              fprintf(stderr, "lcms: Error #%d; ", ErrorCode);
              vfprintf(stderr, ErrorText, args);
              fprintf(stderr, "\n");
              va_end(args);

              if (nDoAbort == LCMS_ERROR_ABORT) exit(1);
#else
              {
              char Buffer1[1024];
              char Buffer2[256];

              snprintf(Buffer1,  767, "Error #%x; ", ErrorCode);
              vsnprintf(Buffer2, 255, ErrorText, args);
              strcat(Buffer1, Buffer2);
              MessageBox(NULL, Buffer1, "Little cms",
                                          MB_OK|MB_ICONSTOP|MB_TASKMODAL);
              va_end(args);

              if (nDoAbort == LCMS_ERROR_ABORT) {

#ifdef __BORLANDC__
                    _cexit();
#endif

                  FatalAppExit(0, "lcms is terminating application");
              }
              }
#endif
}
Пример #4
0
int main(void)
{
  int fd;
  char c;

  if ((fd = open("_cexit.c",O_RDONLY)) < 0)
  {
	printf("Unable to open _cexit.c for reading\n");
	return 1;
  }
  atexit(exit_func);
  if (read(fd,&c,1) != 1)

printf("Unable to read from open file handle %d before _cexit\n",fd);
  else
	printf("Successfully read from open file handle %d before _cexit\n",fd);
  _cexit();
  if (read(fd,&c,1) == 1)
	  MessageBeep(0);
  return 0;
}
Пример #5
0
BOOL __cdecl
__CRTDLL_INIT(
        HANDLE  hDllHandle,
        DWORD   dwReason,
        LPVOID  lpreserved
        )
{
        BOOL term_all = FALSE;

        if ( dwReason == DLL_PROCESS_ATTACH ) {
            if ( !_heap_init() )    /* initialize heap */
                /*
                 * The heap cannot be initialized, return failure to the
                 * loader.
                 */
                return FALSE;

            if(!_mtinit())          /* initialize multi-thread */
            {
                /*
                 * If the DLL load is going to fail, we must clean up
                 * all resources that have already been allocated.
                 */
                _heap_term();       /* heap is now invalid! */

                return FALSE;       /* fail DLL load on failure */
            }

            if (_ioinit() < 0)
            {
                _mtterm();          /* free TLS index, call _mtdeletelocks() */
                _heap_term();       /* heap is now invalid! */
                return FALSE;       /* fail DLL load on failure */
            }

            _wcmdln = GetCommandLineW();

#if !defined(_CRT_APP) || defined(_KERNELX)
            _aenvptr = (char *)__crtGetEnvironmentStringsA();
            _acmdln = GetCommandLineA();
#else /* !defined(_CRT_APP) || defined(_KERNELX) */
            // We cannot call GetCommandLineA in the App CRT:
            if (!__copy_to_char(_wcmdln, &_acmdln))
            {
                _acmdln = NULL;
            }
#endif /* !defined(_CRT_APP) || defined(_KERNELX) */

#if !defined(_CRT_APP) || defined(_KERNELX)
#ifdef _MBCS
            /*
             * Initialize multibyte ctype table. Always done since it is
             * needed for processing the environment strings.
             */
            __initmbctable();
#endif  /* _MBCS */

            if (_setenvp() < 0 ||   /* get environ info */
                _cinit(FALSE) != 0)  /* do C data initialize */
            {
                term_all = TRUE;
            }
#else  /* !defined(_CRT_APP) || defined(_KERNELX) */
            if (_cinit(FALSE) != 0)  /* do C data initialize */
            {
                term_all = TRUE;
            }
#endif  /* !defined(_CRT_APP) || defined(_KERNELX) */

            if (term_all)
            {
#if defined(_CRT_APP) && !defined(_KERNELX)
                _free_crt(_acmdln);
                _acmdln = NULL;
#endif
                _ioterm();          /* shut down lowio */
                _mtterm();          /* free TLS index, call _mtdeletelocks() */
                _heap_term();       /* heap is now invalid! */
                return FALSE;       /* fail DLL load on failure */
            }

            /*
             * Increment flag indicating process attach notification
             * has been received.
             */
            proc_attached++;

        }
        else if ( dwReason == DLL_PROCESS_DETACH ) {
            /*
             * if a client process is detaching, make sure minimal
             * runtime termination is performed and clean up our
             * 'locks' (i.e., delete critical sections).
             */
            if ( proc_attached > 0 ) {
                proc_attached--;
                __try {

                    /*
                     * Any basic clean-up done here may also need
                     * to be done below if Process Attach is partly
                     * processed and then a failure is encountered.
                     */

                    if ( _C_Termination_Done == FALSE )
                        _cexit();

                    __crtdll_callstaticterminators();

                    /* Free all allocated CRT memory */
                    __freeCrtMemory();

#ifdef _DEBUG
                    /* Dump all memory leaks */
                    if (_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) & _CRTDBG_LEAK_CHECK_DF)
                    {
                        _CrtSetDumpClient(NULL);
                        _CrtDumpMemoryLeaks();
                    }
#endif  /* _DEBUG */

                    /*
                     * What remains is to clean up the system resources we have
                     * used (handles, critical sections, memory,...,etc.). This
                     * needs to be done if the whole process is NOT terminating.
                     */

                    /* If dwReason is DLL_PROCESS_DETACH, lpreserved is NULL
                     * if FreeLibrary has been called or the DLL load failed
                     * and non-NULL if the process is terminating.
                     */
                    if ( lpreserved == NULL )
                    {
#if defined(_CRT_APP) && !defined(_KERNELX)
                        _free_crt(_acmdln);
                        _acmdln = NULL;
#endif
                        /*
                         * The process is NOT terminating so we must clean up...
                         */
                        _ioterm();

                        /* free TLS index, call _mtdeletelocks() */
                        _mtterm();

                        /* This should be the last thing the C run-time does */
                        _heap_term();   /* heap is now invalid! */
                    }

                }
                __finally {
                    /* we shouldn't really have to care about this, because
                       letting an exception escape from DllMain(DLL_PROCESS_DETACH) should
                       result in process termination. Unfortunately, Windows up to Win7 as of now
                       just silently swallows the exception.

                       I have considered all kinds of tricks, but decided to leave it up to OS
                       folks to fix this.

                       For the time being just remove our FLS callback during phase 2 unwind
                       that would otherwise be left pointing to unmapped address space.
                       */
                     if ( lpreserved == NULL && __flsindex != FLS_OUT_OF_INDEXES )
                         _mtterm();
                }
            }
            else
                /* no prior process attach, just return */
                return FALSE;
Пример #6
0
__declspec(noinline) int
__tmainCRTStartup (void)
{
  _TCHAR *lpszCommandLine = NULL;
  STARTUPINFO StartupInfo;
  WINBOOL inDoubleQuote = FALSE;
  memset (&StartupInfo, 0, sizeof (STARTUPINFO));
  
  if (mingw_app_type)
    GetStartupInfo (&StartupInfo);
  {
    void *lock_free = NULL;
    void *fiberid = ((PNT_TIB)NtCurrentTeb())->StackBase;
    int nested = FALSE;
    while((lock_free = InterlockedCompareExchangePointer ((volatile PVOID *) &__native_startup_lock,
							  fiberid, 0)) != 0)
      {
	if (lock_free == fiberid)
	  {
	    nested = TRUE;
	    break;
	  }
	Sleep(1000);
      }
    if (__native_startup_state == __initializing)
      {
	_amsg_exit (31);
      }
    else if (__native_startup_state == __uninitialized)
      {
	__native_startup_state = __initializing;
	_initterm ((_PVFV *)(void *)__xi_a, (_PVFV *)(void *) __xi_z);
      }
    else
      has_cctor = 1;

    if (__native_startup_state == __initializing)
      {
	_initterm (__xc_a, __xc_z);
	__native_startup_state = __initialized;
      }
    _ASSERTE(__native_startup_state == __initialized);
    if (! nested)
      (VOID)InterlockedExchangePointer ((volatile PVOID *) &__native_startup_lock, 0);
    
    if (__dyn_tls_init_callback != NULL)
      __dyn_tls_init_callback (NULL, DLL_THREAD_ATTACH, NULL);
    
    _pei386_runtime_relocator ();
    __mingw_oldexcpt_handler = SetUnhandledExceptionFilter (_gnu_exception_handler);
#ifdef _WIN64
    __mingw_init_ehandler ();
#endif
    __mingw_prepare_except_for_msvcr80_and_higher ();
    
    _fpreset ();

    if (mingw_app_type)
      {
#ifdef WPRFLAG
    lpszCommandLine = (_TCHAR *) _wcmdln;
#else
    lpszCommandLine = (char *) _acmdln;
#endif
    while (*lpszCommandLine > SPACECHAR || (*lpszCommandLine&&inDoubleQuote))
      {
	if (*lpszCommandLine == DQUOTECHAR)
	  inDoubleQuote = !inDoubleQuote;
#ifdef _MBCS
	if (_ismbblead (*lpszCommandLine))
	  {
	    if (lpszCommandLine) /* FIXME: Why this check? Should I check for *lpszCommandLine != 0 too? */
	      lpszCommandLine++;
	  }
#endif
	++lpszCommandLine;
      }
    while (*lpszCommandLine && (*lpszCommandLine <= SPACECHAR))
      lpszCommandLine++;

    __mingw_winmain_hInstance = (HINSTANCE) &__ImageBase;
    __mingw_winmain_lpCmdLine = lpszCommandLine;
    __mingw_winmain_nShowCmd = StartupInfo.dwFlags & STARTF_USESHOWWINDOW ?
				StartupInfo.wShowWindow : SW_SHOWDEFAULT;
    }
    duplicate_ppstrings (argc, &argv);
#ifdef WPRFLAG
    __winitenv = envp;
    /* C++ initialization.
       gcc inserts this call automatically for a function called main, but not for wmain.  */
    __main ();
    mainret = wmain (argc, argv, envp);
#else
    __initenv = envp;
    mainret = main (argc, argv, envp);
#endif
    if (!managedapp)
      exit (mainret);

    if (has_cctor == 0)
      _cexit ();
  }
  return mainret;
}
Пример #7
0
int QtWinMainCRTStartup()
{
#ifdef __MSVCRT__
    __set_app_type (__GUI_APP);
#endif

    SetUnhandledExceptionFilter (_gnu_exception_handler);

    /*
    * Initialize floating point unit.
    */
    _fpreset ();			/* Supplied by the runtime library. */

    /*
    * Sets the default file mode.
    * If _CRT_fmode is set, also set mode for stdin, stdout
    * and stderr, as well
    * NOTE: DLLs don't do this because that would be rude!
    */
    _mingw32_init_fmode ();

    /* Adust references to dllimported data that have non-zero offsets.  */
    _pei386_runtime_relocator ();

    char *szCmd;
    STARTUPINFO startinfo;
    int nRet;

    /* Get the command line passed to the process. */
    szCmd = GetCommandLineA ();
    GetStartupInfo (&startinfo);

    /* Strip off the name of the application and any leading
    * whitespace. */
    if (szCmd) {

        while (ISSPACE (*szCmd)) {
            szCmd++;
        }

        /* On my system I always get the app name enclosed
        * in quotes... */
        if (*szCmd == '\"') {
            do {
                szCmd++;
            } while (*szCmd != '\"' && *szCmd != '\0');

            if (*szCmd == '\"') {
                szCmd++;
            }
        } else {
        /* If no quotes then assume first token is program
        * name. */
            while (!ISSPACE (*szCmd) && *szCmd != '\0') {
                szCmd++;
            }
        }

        while (ISSPACE (*szCmd)) {
            szCmd++;
        }
    }

    nRet = WinMain (GetModuleHandle (NULL), NULL, szCmd,
                    (startinfo.dwFlags & STARTF_USESHOWWINDOW) ?
                     startinfo.wShowWindow : SW_SHOWDEFAULT);

    /*
    * Perform exit processing for the C library. This means
    * flushing output and calling 'atexit' registered functions.
    */
    _cexit ();

    ExitProcess (nRet);

    return 0;

}
Пример #8
0
int
__tmainCRTStartup(
    void
) {
    unsigned int osplatform = 0;
    unsigned int winver = 0;
    unsigned int winmajor = 0;
    unsigned int winminor = 0;
    unsigned int osver = 0;
    int initret;
    int mainret = 0;
    OSVERSIONINFOA* posvi;
    int managedapp;
#ifdef _WINMAIN_
    _TUCHAR* lpszCommandLine;
    STARTUPINFO StartupInfo;

    __try {
        /*
        Note: MSDN specifically notes that GetStartupInfo returns no error, and throws unspecified SEH if it fails, so
        the very general exception handler below is appropriate
        */
        GetStartupInfo(&StartupInfo);
    } __except (EXCEPTION_EXECUTE_HANDLER) {
        return 255;
    }

#endif  /* _WINMAIN_ */
    /*
     * Dynamically allocate the OSVERSIONINFOA buffer, so we avoid
     * triggering the /GS buffer overrun detection.  That can't be
     * used here, since the guard cookie isn't available until we
     * initialize it from here!
     */
    posvi = (OSVERSIONINFOA*)HeapAlloc(GetProcessHeap(), 0, sizeof(OSVERSIONINFOA));

    if (!posvi) {
        fast_error_exit(_RT_HEAP);
        return 255;
    }

    /*
     * Get the full Win32 version
     */
    posvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);

    if (!GetVersionExA(posvi)) {
        HeapFree(GetProcessHeap(), 0, posvi);
        return 255;
    }

    osplatform = posvi->dwPlatformId;
    winmajor = posvi->dwMajorVersion;
    winminor = posvi->dwMinorVersion;
    /*
     * The somewhat bizarre calculations of _osver and _winver are
     * required for backward compatibility (used to use GetVersion)
     */
    osver = (posvi->dwBuildNumber) & 0x07fff;
    HeapFree(GetProcessHeap(), 0, posvi);

    if (osplatform != VER_PLATFORM_WIN32_NT) {
        osver |= 0x08000;
    }

    winver = (winmajor << 8) + winminor;
    _set_osplatform(osplatform);
    _set_winver(winver);
    _set_winmajor(winmajor);
    _set_winminor(winminor);
    _set_osver(osver);
    /*
     * Determine if this is a managed application
     */
    managedapp = check_managed_app();

    if (!_heap_init(1)) {               /* initialize heap */
        fast_error_exit(_RT_HEAPINIT);    /* write message and die */
    }

    if (!_mtinit()) {                   /* initialize multi-thread */
        fast_error_exit(_RT_THREAD);    /* write message and die */
    }

    /* Enable buffer count checking if linking against static lib */
    _CrtSetCheckCount(TRUE);
    /*
     * Initialize the Runtime Checks stuff
     */
#ifdef _RTC
    _RTC_Initialize();
#endif  /* _RTC */
    /*
     * Guard the remainder of the initialization code and the call
     * to user's main, or WinMain, function in a __try/__except
     * statement.
     */

    __try {
        if (_ioinit() < 0) {            /* initialize lowio */
            _amsg_exit(_RT_LOWIOINIT);
        }

        /* get wide cmd line info */
        _tcmdln = (_TSCHAR*)GetCommandLineT();
        /* get wide environ info */
        _tenvptr = (_TSCHAR*)GetEnvironmentStringsT();

        if (_tsetargv() < 0) {
            _amsg_exit(_RT_SPACEARG);
        }

        if (_tsetenvp() < 0) {
            _amsg_exit(_RT_SPACEENV);
        }

        initret = _cinit(TRUE);                  /* do C data initialize */

        if (initret != 0) {
            _amsg_exit(initret);
        }

#ifdef _WINMAIN_
        lpszCommandLine = _twincmdln();
        mainret = _tWinMain((HINSTANCE)&__ImageBase,
                            NULL,
                            lpszCommandLine,
                            StartupInfo.dwFlags & STARTF_USESHOWWINDOW
                            ? StartupInfo.wShowWindow
                            : SW_SHOWDEFAULT
                           );
#else  /* _WINMAIN_ */
        _tinitenv = _tenviron;
        mainret = _tmain(__argc, _targv, _tenviron);
#endif  /* _WINMAIN_ */

        if (!managedapp) {
            exit(mainret);
        }

        _cexit();
    } __except (_XcptFilter(GetExceptionCode(), GetExceptionInformation())) {
        /*
         * Should never reach here
         */
        mainret = GetExceptionCode();

        if (!managedapp) {
            _exit(mainret);
        }

        _c_exit();
    } /* end of try - except */

    return mainret;
}
Пример #9
0
BOOL WINAPI _CRT_INIT(
    HANDLE  hDllHandle,
    DWORD   dwReason,
    LPVOID  lpreserved
) {
    unsigned int osplatform = 0;
    unsigned int winver = 0;
    unsigned int winmajor = 0;
    unsigned int winminor = 0;
    unsigned int osver = 0;

    /*
     * Start-up code only gets executed when the process is initialized
     */

    if (dwReason == DLL_PROCESS_ATTACH) {
        /*
         * Dynamically allocate the OSVERSIONINFOA buffer, so we avoid
         * triggering the /GS buffer overrun detection.  That can't be
         * used here, since the guard cookie isn't available until we
         * initialize it from here!
         */
        OSVERSIONINFOA* posvi =
            (OSVERSIONINFOA*)HeapAlloc(GetProcessHeap(), 0, sizeof(OSVERSIONINFOA));

        if (!posvi) {
            return FALSE;
        }

        /*
         * Get the full Win32 version
         */
        posvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);

        if (!GetVersionExA(posvi)) {
            HeapFree(GetProcessHeap(), 0, posvi);
            return FALSE;
        }

        osplatform = posvi->dwPlatformId;
        winmajor = posvi->dwMajorVersion;
        winminor = posvi->dwMinorVersion;
        /*
         * The somewhat bizarre calculations of _osver and _winver are
         * required for backward compatibility (used to use GetVersion)
         */
        osver = (posvi->dwBuildNumber) & 0x07fff;
        HeapFree(GetProcessHeap(), 0, posvi);

        if (osplatform != VER_PLATFORM_WIN32_NT) {
            osver |= 0x08000;
        }

        winver = (winmajor << 8) + winminor;
        _set_osplatform(osplatform);
        _set_winver(winver);
        _set_winmajor(winmajor);
        _set_winminor(winminor);
        _set_osver(osver);

        if (!_heap_init(1)) {   /* initialize heap */
            return FALSE;    /* fail to load DLL */
        }

        if (!_mtinit()) {       /* initialize multi-thread */
            _heap_term();       /* heap is now invalid! */
            return FALSE;       /* fail to load DLL */
        }

        /*
         * Initialize the Runtime Checks stuff
         */
#ifdef _RTC
        _RTC_Initialize();
#endif  /* _RTC */
        _acmdln = (char*)GetCommandLineA();
        _aenvptr = (char*)__crtGetEnvironmentStringsA();

        if (_ioinit() < 0) {    /* initialize lowio */
            _mtterm();          /* free TLS index, call _mtdeletelocks() */
            _heap_term();       /* heap is now invalid! */
            return FALSE;       /* fail to load DLL */
        }

        if (_setargv() < 0 ||   /* get cmd line info */
                _setenvp() < 0 ||   /* get environ info */
                _cinit(FALSE) != 0) { /* do C data initialize, but don't init floating point */
            _ioterm();          /* shut down lowio */
            _mtterm();          /* free TLS index, call _mtdeletelocks() */
            _heap_term();       /* heap is now invalid! */
            return FALSE;       /* fail to load DLL */
        }

        /* Enable buffer count checking if linking against static lib */
        _CrtSetCheckCount(TRUE);
        /*
         * increment flag to indicate process attach notification
         * has been received
         */
        __proc_attached++;
    } else if (dwReason == DLL_PROCESS_DETACH) {
        if (__proc_attached > 0) {
            __proc_attached--;

            /*
             * Any basic clean-up code that goes here must be duplicated
             * below in _DllMainCRTStartup for the case where the user's
             * DllMain() routine fails on a Process Attach notification.
             * This does not include calling user C++ destructors, etc.
             */

            if (_C_Termination_Done == FALSE) {
                _cexit();
            }

#ifdef _DEBUG

            /* Dump all memory leaks */
            if (_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) & _CRTDBG_LEAK_CHECK_DF) {
                _CrtDumpMemoryLeaks();
            }

#endif  /* _DEBUG */
            /*
             * What remains is to clean up the system resources we have
             * used (handles, critical sections, memory,...,etc.). This
             * needs to be done if the whole process is NOT terminating.
             */
#ifndef _DEBUG

            if (lpreserved == NULL) {
#endif  /* _DEBUG */
                /*
                 * The process is NOT terminating so we must clean up...
                 */
                /* Shut down lowio */
                _ioterm();
                _mtterm();
                /* This should be the last thing the C run-time does */
                _heap_term();   /* heap is now invalid! */
#ifndef _DEBUG
            }

#endif  /* _DEBUG */
        } else
            /* no prior process attach, just return */
        {
            return FALSE;
        }
    } else if (dwReason == DLL_THREAD_ATTACH) {
        _ptiddata ptd;
        /* Initialize FlsGetValue function pointer */
        __set_flsgetvalue();

        if (((ptd = _calloc_crt(1, sizeof(struct _tiddata))) != NULL)) {
            if (FLS_SETVALUE(__flsindex, (LPVOID)ptd)) {
                /*
                 * Initialize of per-thread data
                 */
                _initptd(ptd, NULL);
                ptd->_tid = GetCurrentThreadId();
                ptd->_thandle = (uintptr_t)(-1);
            } else {
                _free_crt(ptd);
                return FALSE;
            }
        } else {
            return FALSE;
        }
    } else if (dwReason == DLL_THREAD_DETACH) {
        _freeptd(NULL);         /* free up per-thread CRT data */
    }

    return TRUE ;
}
Пример #10
0
int
__tmainCRTStartup(
    void
) {
#ifdef _WINMAIN_
    _TUCHAR* lpszCommandLine;
    STARTUPINFO StartupInfo;
    BOOL inDoubleQuote = FALSE;

    __try {
        /*
        Note: MSDN specifically notes that GetStartupInfo returns no error, and throws unspecified SEH if it fails, so
        the very general exception handler below is appropriate
        */
        GetStartupInfo(&StartupInfo);
    } __except (EXCEPTION_EXECUTE_HANDLER) {
        return 255;
    }

#endif  /* _WINMAIN_ */

    /*
     * Guard the initialization code and the call to user's main, or
     * WinMain, function in a __try/__except statement.
     */

    __try {
        /*
         * There is a possiblity that the module where this object is
         * linked into is a mixed module. In all the cases we gurantee that
         * native initialization will occur before managed initialization.
         * Also in anycase this code should never be called when some other
         * code is initializing native code, that's why we exit in that case.
         *
         * Do runtime startup initializers.
         *
         * Note: the only possible entry we'll be executing here is for
         * __lconv_init, pulled in from charmax.obj only if the EXE was
         * compiled with -J.  All other .CRT$XI* initializers are only
         * run as part of the CRT itself, and so for the CRT DLL model
         * are not found in the EXE.  For that reason, we call _initterm,
         * not _initterm_e, because __lconv_init will never return failure,
         * and _initterm_e is not exported from the CRT DLL.
         *
         * Note further that, when using the CRT DLL, executing the
         * .CRT$XI* initializers is only done for an EXE, not for a DLL
         * using the CRT DLL.  That is to make sure the -J setting for
         * the EXE is not overriden by that of any DLL.
         */
        void* lock_free = 0;
        void* fiberid = ((PNT_TIB)NtCurrentTeb())->StackBase;
        int nested = FALSE;

        while ((lock_free = InterlockedCompareExchangePointer((volatile PVOID*)&__native_startup_lock, fiberid, 0)) != 0) {
            if (lock_free == fiberid) {
                nested = TRUE;
                break;
            }

            /* some other thread is running native startup/shutdown during a cctor/domain unload.
                Should only happen if this DLL was built using the Everett-compat loader lock fix in vcclrit.h
            */
            /* wait for the other thread to complete init before we return */
            Sleep(1000);
        }

        if (__native_startup_state == __initializing) {
            _amsg_exit(_RT_CRT_INIT_CONFLICT);
        } else if (__native_startup_state == __uninitialized) {
            __native_startup_state = __initializing;
#ifndef _SYSCRT

            if (_initterm_e(__xi_a, __xi_z) != 0) {
                return 255;
            }

#else  /* _SYSCRT */
            _initterm((_PVFV*)(void*)__xi_a, (_PVFV*)(void*)__xi_z);
#endif  /* _SYSCRT */
        } else {
            has_cctor = 1;
        }

        /*
        * do C++ constructors (initializers) specific to this EXE
        */
        if (__native_startup_state == __initializing) {
            _initterm(__xc_a, __xc_z);
            __native_startup_state = __initialized;
        }

        _ASSERTE(__native_startup_state == __initialized);

        if (!nested) {
            /* For X86, the definition of InterlockedExchangePointer wrongly causes warning C4312 */
#pragma warning(push)
#pragma warning(disable:4312)
            InterlockedExchangePointer((volatile PVOID*)&__native_startup_lock, 0);
#pragma warning(pop)
        }

        /*
         * If we have any dynamically initialized __declspec(thread)
         * variables, then invoke their initialization for the primary
         * thread used to start the process, by calling __dyn_tls_init
         * through a callback defined in tlsdyn.obj.
         */
        if (__dyn_tls_init_callback != NULL &&
                _IsNonwritableInCurrentImage((PBYTE)&__dyn_tls_init_callback)) {
            __dyn_tls_init_callback(NULL, DLL_THREAD_ATTACH, NULL);
        }

        /* Enable buffer count checking if linking against static lib */
        _CrtSetCheckCount(TRUE);
#ifdef _WINMAIN_
        /*
         * Skip past program name (first token in command line).
         * Check for and handle quoted program name.
         */
#ifdef WPRFLAG

        /* OS may not support "W" flavors */
        if (_wcmdln == NULL) {
            return 255;
        }

        lpszCommandLine = (wchar_t*)_wcmdln;
#else  /* WPRFLAG */
        lpszCommandLine = (unsigned char*)_acmdln;
#endif  /* WPRFLAG */

        while (*lpszCommandLine > SPACECHAR ||
                (*lpszCommandLine && inDoubleQuote)) {
            /*
             * Flip the count from 1 to 0 or 0 to 1 if current character
             * is DOUBLEQUOTE
             */
            if (*lpszCommandLine == DQUOTECHAR) {
                inDoubleQuote = !inDoubleQuote;
            }

#ifdef _MBCS

            if (_ismbblead(*lpszCommandLine)) {
                if (lpszCommandLine) {
                    lpszCommandLine++;
                }
            }

#endif  /* _MBCS */
            ++lpszCommandLine;
        }

        /*
         * Skip past any white space preceeding the second token.
         */
        while (*lpszCommandLine && (*lpszCommandLine <= SPACECHAR)) {
            lpszCommandLine++;
        }

#ifdef WPRFLAG
        mainret = wWinMain(
#else  /* WPRFLAG */
        mainret = WinMain(
#endif  /* WPRFLAG */
                      (HINSTANCE)&__ImageBase,
                      NULL,
                      lpszCommandLine,
                      StartupInfo.dwFlags & STARTF_USESHOWWINDOW
                      ? StartupInfo.wShowWindow
                      : SW_SHOWDEFAULT
                  );
#else  /* _WINMAIN_ */
#ifdef WPRFLAG
        __winitenv = envp;
        mainret = wmain(argc, argv, envp);
#else  /* WPRFLAG */
        __initenv = envp;
        mainret = main(argc, argv, envp);
#endif  /* WPRFLAG */
#endif  /* _WINMAIN_ */

        /*
         * Note that if the exe is managed app, we don't really need to
         * call exit or _c_exit. .cctor should be able to take care of
         * this.
         */
        if (!managedapp)
            exit(mainret);
        if (has_cctor == 0)
            _cexit();
    } __except (_XcptFilter(GetExceptionCode(), GetExceptionInformation())) {
        /*
         * Should never reach here
         */
        mainret = GetExceptionCode();

        /*
         * Note that if the exe is managed app, we don't really need to
         * call exit or _c_exit. .cctor should be able to take care of
         * this.
         */
        if (!managedapp) {
            _exit(mainret);
        }

        if (has_cctor == 0) {
            _cexit();
        }
    } /* end of try - except */

    return mainret;
}
Пример #11
0
// address: 0x4014ec
void _start() {
    __size16 ax; 		// r0
    __size16 cx; 		// r1
    unsigned char dl; 		// r10
    __size32 eax; 		// r24
    __size32 *ebp; 		// r29
    __size32 ebx; 		// r27
    __size32 ecx; 		// r25
    __size32 edi; 		// r31
    int edx; 		// r26
    __size32 esi; 		// r30
    int esp; 		// r28
    void *esp_1; 		// r28{8}
    void *esp_2; 		// r28{44}
    void *esp_3; 		// r28{191}
    int local0; 		// m[esp - 4]
    int local1; 		// m[esp - 24]
    int local2; 		// m[esp - 20]
    unsigned int local3; 		// m[esp - 12]
    int local4; 		// m[esp - 8]
    void *local5; 		// esp_3{191}

    esi = proc1(local1, local2, esi); /* Warning: also results in ebx, edi */
    esp_1 = proc2(16, ebx, esi, edi); /* Warning: also results in ebp */
    local5 = esp_1;
    ebx = 0;
    *(__size32*)(ebp - 4) = 0;
    eax = *24;
    esi = *(eax + 4);
    *(__size32*)(ebp - 28) = 0;
    edi = 0x403374;
    for(;;) {
        esp_3 = local5;
        *(__size32*)(esp_3 - 4) = 0;
        *(__size32*)(esp_3 - 8) = esi;
        *(__size32*)(esp_3 - 12) = 0x403374;
        eax = InterlockedCompareExchange(); /* Warning: also results in edx */
        if (eax == 0) {
            goto L24;
        }
        if (eax == esi) {
            break;
        }
        *(__size32*)(esp_3 - 16) = 1000;
        esp_2 = Sleep(*(esp_3 - 16));
        local5 = esp_2;
    }
    *(__size32*)(ebp - 28) = 1;
L24:
    esi = 1;
    if (global61 != 1) {
        if (global61 != 0) {
            global68 = 1;
L16:
            if (global61 == 1) {
                *(__size32*)(esp_3 - 16) = 0x4020cc;
                *(__size32*)(esp_3 - 20) = 0x4020c4;
                edx = _initterm();
                global61 = 2;
            }
            if (*(ebp - 28) == 0) {
                *(__size32*)(esp_3 - 16) = 0;
                *(__size32*)(esp_3 - 20) = 0x403374;
                edx = InterlockedExchange(*(esp_3 - 20), *(esp_3 - 16));
            }
            esp = esp_3 - 12;
            if (*0x403380 != 0) {
                *(__size32*)(esp_3 - 16) = 0x403380;
                eax = proc5(*(esp_3 - 16), dl, edx, 0); /* Warning: also results in ax, cx, dl, edx */
                ecx = *(esp_3 - 16);
                esp = esp_3 - 12;
                if (eax != 0) {
                    *(__size32*)(esp_3 - 16) = 0;
                    *(__size32*)(esp_3 - 20) = 2;
                    *(__size32*)(esp_3 - 24) = 0;
                    (*global53)(local1, local2, pc, 0x4021d8, 16, ax, cx, dl, eax, ecx, edx, 0, ebp, 1, 0x403374, LOGICALFLAGS32(eax), LOGICALFLAGS32(eax), LOGICALFLAGS32(eax));
                }
            }
            *(__size32*)0x23cc = global75;
            local0 = global75;
            local4 = global76;
            local3 = global77;
            eax = proc6(*(esp - 12), *(esp - 8), 0x23cc, ebx, esi, edi); /* Warning: also results in ebx */
            global79 = eax;
            if (*0x403024 == ebx) {
                local0 = eax;
                exit(*(esp - 4));
            }
            if (*0x403034 == ebx) {
                _cexit();
            }
            *(__size32*)(ebp - 4) = -2;
        } else {
            global61 = 1;
            *(__size32*)(esp_3 - 16) = 0x4020dc;
            *(__size32*)(esp_3 - 20) = 0x4020d0;
            eax = _initterm_e(); /* Warning: also results in edx */
            esp = esp_3 - 12;
            if (eax == 0) {
                goto L16;
            } else {
                *(__size32*)(ebp - 4) = -2;
            }
        }
    } else {
        *(__size32*)(esp_3 - 16) = 31;
        edx = _amsg_exit();
        goto L16;
    }
    proc8(ebp);
    return;
}