Author: tkreuzer Date: Wed Apr 18 15:12:45 2012 New Revision: 56359
URL: http://svn.reactos.org/svn/reactos?rev=56359&view=rev Log: [CRT] Sync startup code with mingw-w64, reduce difference a bit, remaining changes were passed upstream
Added: trunk/reactos/lib/sdk/crt/startup/reactos.c (with props) Modified: trunk/reactos/lib/sdk/crt/include/internal/mingw-w64/internal.h trunk/reactos/lib/sdk/crt/msvcrtex.cmake trunk/reactos/lib/sdk/crt/startup/crtdll.c trunk/reactos/lib/sdk/crt/startup/crtexe.c trunk/reactos/lib/sdk/crt/startup/gs_support.c trunk/reactos/lib/sdk/crt/startup/merr.c trunk/reactos/lib/sdk/crt/startup/mscmain.c trunk/reactos/lib/sdk/crt/startup/pesect.c trunk/reactos/lib/sdk/crt/startup/pseudo-reloc.c trunk/reactos/lib/sdk/crt/startup/tlsmcrt.c trunk/reactos/lib/sdk/crt/startup/wcrtexe.c
Modified: trunk/reactos/lib/sdk/crt/include/internal/mingw-w64/internal.h URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/include/interna... ============================================================================== --- trunk/reactos/lib/sdk/crt/include/internal/mingw-w64/internal.h [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/include/internal/mingw-w64/internal.h [iso-8859-1] Wed Apr 18 15:12:45 2012 @@ -15,6 +15,13 @@
#include <limits.h> #include <windows.h> + +#ifdef __REACTOS__ +#include "malloc.h" +__declspec(dllimport) void __setusermatherr(int (__cdecl *)(struct _exception *)); +#define __mingw_fprintf fprintf +#define __mingw_vfprintf vfprintf +#endif
#pragma pack(push,_CRT_PACKING)
@@ -155,7 +162,7 @@ #define _GUI_APP 2
typedef enum __enative_startup_state { - __uninitialized, __initializing, __initialized + __uninitialized = 0, __initializing, __initialized } __enative_startup_state;
extern volatile __enative_startup_state __native_startup_state;
Modified: trunk/reactos/lib/sdk/crt/msvcrtex.cmake URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/msvcrtex.cmake?... ============================================================================== --- trunk/reactos/lib/sdk/crt/msvcrtex.cmake [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/msvcrtex.cmake [iso-8859-1] Wed Apr 18 15:12:45 2012 @@ -34,6 +34,7 @@ startup/crt0_c.c startup/crt0_w.c startup/dllentry.c + startup/reactos.c misc/fltused.c misc/ofmt_stub.c )
Modified: trunk/reactos/lib/sdk/crt/startup/crtdll.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/startup/crtdll.... ============================================================================== --- trunk/reactos/lib/sdk/crt/startup/crtdll.c [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/startup/crtdll.c [iso-8859-1] Wed Apr 18 15:12:45 2012 @@ -33,12 +33,8 @@ #include <locale.h>
extern void __cdecl _initterm(_PVFV *,_PVFV *); - -#if defined(__GNUC__) -extern void __main (void); +extern void __main (); extern void _pei386_runtime_relocator (void); -#endif - extern _CRTALLOC(".CRT$XIA") _PIFV __xi_a[]; extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z[]; extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a[]; @@ -101,16 +97,19 @@ } Sleep(1000); } - if (__native_startup_state != __uninitialized) - { - _amsg_exit(31); - } - else + 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); - _initterm (__xc_a,__xc_z); + } + if (__native_startup_state == __initializing) + { + _initterm (__xc_a, __xc_z); __native_startup_state = __initialized; } if (! nested) @@ -130,7 +129,7 @@ { Sleep(1000); } - if(__native_startup_state!=__initialized) + if (__native_startup_state != __initialized) { _amsg_exit (31); } @@ -183,9 +182,7 @@ retcode = FALSE; goto i__leave; } -#if defined(__GNUC__) _pei386_runtime_relocator (); -#endif if (dwReason == DLL_PROCESS_ATTACH || dwReason == DLL_THREAD_ATTACH) { retcode = _CRT_INIT (hDllHandle, dwReason, lpreserved); @@ -199,10 +196,8 @@ goto i__leave; } } -#if defined(__GNUC__) if (dwReason == DLL_PROCESS_ATTACH) __main (); -#endif retcode = DllMain(hDllHandle,dwReason,lpreserved); if (dwReason == DLL_PROCESS_ATTACH && ! retcode) {
Modified: trunk/reactos/lib/sdk/crt/startup/crtexe.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/startup/crtexe.... ============================================================================== --- trunk/reactos/lib/sdk/crt/startup/crtexe.c [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/startup/crtexe.c [iso-8859-1] Wed Apr 18 15:12:45 2012 @@ -20,7 +20,6 @@ #include <tchar.h> #include <sect_attribs.h> #include <locale.h> -#include <malloc.h>
#ifndef __winitenv extern wchar_t *** __MINGW_IMP_SYMBOL(__winitenv); @@ -43,8 +42,6 @@ extern void _fpreset (void); #define SPACECHAR _T(' ') #define DQUOTECHAR _T('"') - -__declspec(dllimport) void __setusermatherr(int (__cdecl *)(struct _exception *));
extern int * __MINGW_IMP_SYMBOL(_fmode); extern int * __MINGW_IMP_SYMBOL(_commode); @@ -55,14 +52,6 @@ #define _commode (* __MINGW_IMP_SYMBOL(_commode)) extern int _dowildcard;
-#if defined(__GNUC__) -int _MINGW_INSTALL_DEBUG_MATHERR __attribute__((weak)) = 0; -#else -int __declspec(selectany) _MINGW_INSTALL_DEBUG_MATHERR = 0; -#endif - -extern int __defaultmatherr; - extern _CRTIMP void __cdecl _initterm(_PVFV *, _PVFV *);
static int __cdecl check_managed_app (void); @@ -85,12 +74,7 @@ DWORD __mingw_winmain_nShowCmd;
static int argc; - -#if defined(__GNUC__) extern void __main(void); -extern void _pei386_runtime_relocator (void); -#endif - #ifdef WPRFLAG static wchar_t **argv; static wchar_t **envp; @@ -106,7 +90,7 @@ static _startupinfo startinfo; extern LPTOP_LEVEL_EXCEPTION_FILTER __mingw_oldexcpt_handler;
- +extern void _pei386_runtime_relocator (void); long CALLBACK _gnu_exception_handler (EXCEPTION_POINTERS * exception_data); #ifdef WPRFLAG static void duplicate_ppstrings (int ac, wchar_t ***av); @@ -117,8 +101,10 @@ static int __cdecl pre_c_init (void); static void __cdecl pre_cpp_init (void); static void __cdecl __mingw_prepare_except_for_msvcr80_and_higher (void); -_CRTALLOC(".CRT$XIAA") _PIFV __declspec(selectany) mingw_pcinit = pre_c_init; -_CRTALLOC(".CRT$XCAA") _PVFV __declspec(selectany) mingw_pcppinit = pre_cpp_init; +_CRTALLOC(".CRT$XIAA") _PIFV mingw_pcinit = pre_c_init; +_CRTALLOC(".CRT$XCAA") _PVFV mingw_pcppinit = pre_cpp_init; + +extern int _MINGW_INSTALL_DEBUG_MATHERR;
static int __cdecl pre_c_init (void) @@ -138,7 +124,7 @@ #else _setargv(); #endif - if (_MINGW_INSTALL_DEBUG_MATHERR) + if (_MINGW_INSTALL_DEBUG_MATHERR == 1) { __setusermatherr (_matherr); } @@ -224,13 +210,15 @@ WINBOOL inDoubleQuote = FALSE; memset (&StartupInfo, 0, sizeof (STARTUPINFO));
-#if !defined(_WIN64) && defined(__GNUC__) +#ifndef _WIN64 /* We need to make sure that this function is build with frame-pointer and that we align the stack to 16 bytes for the sake of SSE ops in main or in functions inlined into main. */ lpszCommandLine = (_TCHAR *) alloca (32); memset (lpszCommandLine, 0xcc, 32); +#ifdef __GNUC__ asm __volatile__ ("andl $-16, %%esp" : : : "%esp"); +#endif #endif
if (mingw_app_type) @@ -269,19 +257,17 @@ _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); - -#if defined(__GNUC__) + _pei386_runtime_relocator (); -#endif __mingw_oldexcpt_handler = SetUnhandledExceptionFilter (_gnu_exception_handler); -#if defined(_WIN64) && !defined(_MSC_VER) +#ifdef _WIN64 __mingw_init_ehandler (); #endif __mingw_prepare_except_for_msvcr80_and_higher (); - + _fpreset ();
if (mingw_app_type) @@ -313,9 +299,7 @@ StartupInfo.wShowWindow : SW_SHOWDEFAULT; } duplicate_ppstrings (argc, &argv); -#if defined(__GNUC__) __main (); -#endif #ifdef WPRFLAG __winitenv = envp; /* C++ initialization. @@ -395,7 +379,7 @@ avl=*av; for (i=0; i < ac; i++) { - int l = wbytelen (avl[i]); + size_t l = wbytelen (avl[i]); n[i] = (wchar_t *) malloc (l); memcpy (n[i], avl[i], l); } @@ -408,11 +392,11 @@ char **avl; int i; char **n = (char **) malloc (sizeof (char *) * (ac + 1)); - + avl=*av; for (i=0; i < ac; i++) { - int l = strlen (avl[i]) + 1; + size_t l = strlen (avl[i]) + 1; n[i] = (char *) malloc (l); memcpy (n[i], avl[i], l); } @@ -439,21 +423,14 @@ #endif }
-static void __cdecl +HANDLE __mingw_get_msvcrt_handle(void); + +static void __cdecl __mingw_prepare_except_for_msvcr80_and_higher (void) { _invalid_parameter_handler (*fIPH)(_invalid_parameter_handler) = NULL; - HMODULE hmsv = GetModuleHandleA ("msvcr80.dll"); - if(!hmsv) - hmsv = GetModuleHandleA ("msvcr70.dll"); - if (!hmsv) - hmsv = GetModuleHandleA ("msvcrt.dll"); - if (!hmsv) - hmsv = LoadLibraryA ("msvcrt.dll"); - if (!hmsv) - return; - fIPH = (_invalid_parameter_handler (*)(_invalid_parameter_handler)) - GetProcAddress (hmsv, "_set_invalid_parameter_handler"); + + fIPH = (void*)GetProcAddress (__mingw_get_msvcrt_handle(), "_set_invalid_parameter_handler"); if (fIPH) (*fIPH)(__mingw_invalidParameterHandler); }
Modified: trunk/reactos/lib/sdk/crt/startup/gs_support.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/startup/gs_supp... ============================================================================== --- trunk/reactos/lib/sdk/crt/startup/gs_support.c [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/startup/gs_support.c [iso-8859-1] Wed Apr 18 15:12:45 2012 @@ -97,10 +97,10 @@ #define _AddressOfReturnAddress() __builtin_frame_address (0) #endif /* __GNUC__ */
-__declspec(noreturn) void __cdecl __report_gsfailure (ULONGLONG); +__declspec(noreturn) void __cdecl __report_gsfailure (ULONG_PTR);
__declspec(noreturn) void __cdecl -__report_gsfailure (ULONGLONG StackCookie) +__report_gsfailure (ULONG_PTR StackCookie) { volatile UINT_PTR cookie[2] __MINGW_ATTRIB_UNUSED; #ifdef _WIN64
Modified: trunk/reactos/lib/sdk/crt/startup/merr.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/startup/merr.c?... ============================================================================== --- trunk/reactos/lib/sdk/crt/startup/merr.c [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/startup/merr.c [iso-8859-1] Wed Apr 18 15:12:45 2012 @@ -7,8 +7,6 @@ #include <internal.h> #include <math.h> #include <stdio.h> - -int __defaultmatherr = 0;
typedef int (__cdecl *fUserMathErr)(struct _exception *); static fUserMathErr stUserMathErr; @@ -20,7 +18,7 @@ if (!stUserMathErr) return; ex.type = typ; - ex.name = name; + ex.name = (char*)name; ex.arg1 = a1; ex.arg2 = a2; ex.retval = rslt; @@ -71,7 +69,7 @@ break; }
- fprintf(stderr, "_matherr(): %s in %s(%g, %g) (retval=%g)\n", + __mingw_fprintf (stderr, "_matherr(): %s in %s(%g, %g) (retval=%g)\n", type, pexcept->name, pexcept->arg1, pexcept->arg2, pexcept->retval); return 0; }
Modified: trunk/reactos/lib/sdk/crt/startup/mscmain.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/startup/mscmain... ============================================================================== --- trunk/reactos/lib/sdk/crt/startup/mscmain.c [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/startup/mscmain.c [iso-8859-1] Wed Apr 18 15:12:45 2012 @@ -7,20 +7,24 @@ #include <windows.h> #include <stdlib.h>
-int __mingw_init_ehandler (void) +void _pei386_runtime_relocator(void) +{ +} + +int __mingw_init_ehandler(void) { /* Nothing to do */ return 1; }
void -__do_global_dtors (void) +__do_global_dtors(void) {
}
void -__do_global_ctors (void) +__do_global_ctors(void) {
} @@ -35,7 +39,7 @@ static int initialized = 0;
void -__main (void) +__main(void) { if (!initialized) {
Modified: trunk/reactos/lib/sdk/crt/startup/pesect.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/startup/pesect.... ============================================================================== --- trunk/reactos/lib/sdk/crt/startup/pesect.c [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/startup/pesect.c [iso-8859-1] Wed Apr 18 15:12:45 2012 @@ -10,8 +10,8 @@ #if defined (_WIN64) && defined (__ia64__) #error FIXME: Unsupported __ImageBase implementation. #else +#ifdef __GNUC__ /* Hack, for bug in ld. Will be removed soon. */ -#ifndef _MSC_VER #define __ImageBase __MINGW_LSYMBOL(_image_base__) #endif /* This symbol is defined by the linker. */ @@ -184,3 +184,75 @@ return FALSE; return (pSection->Characteristics & IMAGE_SCN_MEM_WRITE) == 0; } + +const char * +__mingw_enum_import_library_names (int); + +const char * +__mingw_enum_import_library_names (int i) +{ + PBYTE pImageBase; + PIMAGE_NT_HEADERS pNTHeader; + PIMAGE_IMPORT_DESCRIPTOR importDesc; + PIMAGE_SECTION_HEADER pSection; + DWORD importsStartRVA; + + pImageBase = (PBYTE) &__ImageBase; + if (! _ValidateImageBase (pImageBase)) + return NULL; + + pNTHeader = (PIMAGE_NT_HEADERS) (pImageBase + ((PIMAGE_DOS_HEADER) pImageBase)->e_lfanew); + + importsStartRVA = pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress; + if (!importsStartRVA) + return NULL; + + pSection = _FindPESection (pImageBase, importsStartRVA); + if (!pSection) + return NULL; + + importDesc = (PIMAGE_IMPORT_DESCRIPTOR) (pImageBase + importsStartRVA); + if (!importDesc) + return NULL; + + for (;;) + { + if (importDesc->TimeDateStamp == 0 && importDesc->Name == 0) + break; + + if (i <= 0) + return (char *) (pImageBase + importDesc->Name); + --i; + importDesc++; + } + + return NULL; +} + +HMODULE __mingw_get_msvcrt_handle(void); +HMODULE __mingw_get_msvcrt_handle(void) +{ + static HANDLE msvcrt_handle; + + if(!msvcrt_handle) { + const char *lib_name; + int i = 0; + + while ((lib_name = __mingw_enum_import_library_names (i++))) { + if((lib_name[0] == 'm' || lib_name[0] == 'M') + && (lib_name[1] == 's' || lib_name[1] == 'S') + && (lib_name[2] == 'v' || lib_name[2] == 'V') + && (lib_name[3] == 'c' || lib_name[3] == 'C') + && (lib_name[4] == 'r' || lib_name[4] == 'R') + && (lib_name[5] == 't' || lib_name[5] == 'T' || ('0' <= lib_name[5] && lib_name[5] <= '9'))) + break; + } + + if(lib_name) + msvcrt_handle = GetModuleHandleA(lib_name); + if(!msvcrt_handle) + msvcrt_handle = LoadLibraryW(L"msvcrt.dll"); + } + + return msvcrt_handle; +}
Modified: trunk/reactos/lib/sdk/crt/startup/pseudo-reloc.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/startup/pseudo-... ============================================================================== --- trunk/reactos/lib/sdk/crt/startup/pseudo-reloc.c [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/startup/pseudo-reloc.c [iso-8859-1] Wed Apr 18 15:12:45 2012 @@ -3,7 +3,7 @@ Contributed by Egor Duda deo@logos-m.ru Modified by addition of runtime_pseudo_reloc version 2 by Kai Tietz kai.tietz@onevision.com - + THIS SOFTWARE IS NOT COPYRIGHTED
This source code is offered for use in the public domain. You may @@ -20,7 +20,7 @@ #include <stdlib.h> #include <stdarg.h> #include <memory.h> -#include <malloc.h> +#include <internal.h>
#if defined(__CYGWIN__) #include <wchar.h> @@ -146,11 +146,12 @@ va_list argp; va_start (argp, msg); # ifdef __MINGW64_VERSION_MAJOR - fprintf (stderr, "Mingw-w64 runtime failure:\n"); + __mingw_fprintf (stderr, "Mingw-w64 runtime failure:\n"); + __mingw_vfprintf (stderr, msg, argp); # else fprintf (stderr, "Mingw runtime failure:\n"); -# endif vfprintf (stderr, msg, argp); +#endif va_end (argp); abort (); #endif @@ -206,8 +207,13 @@ }
if (b.Protect != PAGE_EXECUTE_READWRITE && b.Protect != PAGE_READWRITE) - VirtualProtect (b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, - &the_secs[i].old_protect); + { + if (!VirtualProtect (b.BaseAddress, b.RegionSize, + PAGE_EXECUTE_READWRITE, + &the_secs[i].old_protect)) + __report_error (" VirtualProtect failed with code 0x%x", + (int) GetLastError ()); + } ++maxSections; return; } @@ -253,15 +259,17 @@ static void __write_memory (void *addr, const void *src, size_t len) { -#ifndef __MINGW64_VERSION_MAJOR MEMORY_BASIC_INFORMATION b; DWORD oldprot; -#endif /* ! __MINGW64_VERSION_MAJOR */ + int call_unprotect = 0;
if (!len) return;
-#ifndef __MINGW64_VERSION_MAJOR +#ifdef __MINGW64_VERSION_MAJOR + mark_section_writable ((LPVOID) addr); +#endif + if (!VirtualQuery (addr, &b, sizeof(b))) { __report_error (" VirtualQuery failed for %d bytes at address %p", @@ -270,19 +278,17 @@
/* Temporarily allow write access to read-only protected memory. */ if (b.Protect != PAGE_EXECUTE_READWRITE && b.Protect != PAGE_READWRITE) - VirtualProtect (b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, - &oldprot); -#else /* ! __MINGW64_VERSION_MAJOR */ - mark_section_writable ((LPVOID) addr); -#endif /* __MINGW64_VERSION_MAJOR */ + { + call_unprotect = 1; + VirtualProtect (b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, + &oldprot); + }
/* write the data. */ memcpy (addr, src, len); /* Restore original protection. */ -#ifndef __MINGW64_VERSION_MAJOR - if (b.Protect != PAGE_EXECUTE_READWRITE && b.Protect != PAGE_READWRITE) + if (call_unprotect && b.Protect != PAGE_EXECUTE_READWRITE && b.Protect != PAGE_READWRITE) VirtualProtect (b.BaseAddress, b.RegionSize, oldprot, &oldprot); -#endif /* !__MINGW64_VERSION_MAJOR */ }
#define RP_VERSION_V1 0
Added: trunk/reactos/lib/sdk/crt/startup/reactos.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/startup/reactos... ============================================================================== --- trunk/reactos/lib/sdk/crt/startup/reactos.c (added) +++ trunk/reactos/lib/sdk/crt/startup/reactos.c [iso-8859-1] Wed Apr 18 15:12:45 2012 @@ -1,0 +1,3 @@ + +int _MINGW_INSTALL_DEBUG_MATHERR = 0; +
Propchange: trunk/reactos/lib/sdk/crt/startup/reactos.c ------------------------------------------------------------------------------ svn:eol-style = native
Modified: trunk/reactos/lib/sdk/crt/startup/tlsmcrt.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/startup/tlsmcrt... ============================================================================== --- trunk/reactos/lib/sdk/crt/startup/tlsmcrt.c [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/startup/tlsmcrt.c [iso-8859-1] Wed Apr 18 15:12:45 2012 @@ -10,3 +10,4 @@ otherwise we do tls cleanup in runtime and _CRT_MT has value 2. */ int _CRT_MT = 2; +
Modified: trunk/reactos/lib/sdk/crt/startup/wcrtexe.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/startup/wcrtexe... ============================================================================== --- trunk/reactos/lib/sdk/crt/startup/wcrtexe.c [iso-8859-1] (original) +++ trunk/reactos/lib/sdk/crt/startup/wcrtexe.c [iso-8859-1] Wed Apr 18 15:12:45 2012 @@ -9,5 +9,7 @@ #define _UNICODE #define mainCRTStartup wmainCRTStartup #define WinMainCRTStartup wWinMainCRTStartup +#define mingw_pcinit mingw_pwcinit +#define mingw_pcppinit mingw_pwcppinit
#include "crtexe.c"