Sync startup code with mingw-w64, reduce difference a bit, remaining changes were passed upstream

svn path=/trunk/; revision=56359
This commit is contained in:
Timo Kreuzer 2012-04-18 15:12:45 +00:00
parent 45aa7bfa9d
commit 8b5d4fe0e3
12 changed files with 153 additions and 87 deletions

View file

@ -16,6 +16,13 @@ extern "C" {
#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)
#ifndef __INTERNAL_FUNC_DEFINED
@ -155,7 +162,7 @@ extern char ** __MINGW_IMP_SYMBOL(_wcmdln);
#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;

View file

@ -34,6 +34,7 @@ list(APPEND MSVCRTEX_SOURCE
startup/crt0_c.c
startup/crt0_w.c
startup/dllentry.c
startup/reactos.c
misc/fltused.c
misc/ofmt_stub.c
)

View file

@ -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 @@ WINBOOL WINAPI _CRT_INIT (HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved)
}
Sleep(1000);
}
if (__native_startup_state != __uninitialized)
if (__native_startup_state == __initializing)
{
_amsg_exit(31);
_amsg_exit (31);
}
else
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 @@ WINBOOL WINAPI _CRT_INIT (HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved)
{
Sleep(1000);
}
if(__native_startup_state!=__initialized)
if (__native_startup_state != __initialized)
{
_amsg_exit (31);
}
@ -183,9 +182,7 @@ __DllMainCRTStartup (HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved)
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 @@ __DllMainCRTStartup (HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved)
goto i__leave;
}
}
#if defined(__GNUC__)
if (dwReason == DLL_PROCESS_ATTACH)
__main ();
#endif
retcode = DllMain(hDllHandle,dwReason,lpreserved);
if (dwReason == DLL_PROCESS_ATTACH && ! retcode)
{

View file

@ -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);
@ -44,8 +43,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 @@ extern int * __MINGW_IMP_SYMBOL(_commode);
#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 @@ _TCHAR *__mingw_winmain_lpCmdLine;
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 int has_cctor = 0;
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 void duplicate_ppstrings (int ac, char ***av);
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 @@ pre_c_init (void)
#else
_setargv();
#endif
if (_MINGW_INSTALL_DEBUG_MATHERR)
if (_MINGW_INSTALL_DEBUG_MATHERR == 1)
{
__setusermatherr (_matherr);
}
@ -224,13 +210,15 @@ __tmainCRTStartup (void)
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 @@ __tmainCRTStartup (void)
_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 @@ __tmainCRTStartup (void)
StartupInfo.wShowWindow : SW_SHOWDEFAULT;
}
duplicate_ppstrings (argc, &argv);
#if defined(__GNUC__)
__main ();
#endif
#ifdef WPRFLAG
__winitenv = envp;
/* C++ initialization.
@ -395,7 +379,7 @@ static void duplicate_ppstrings (int ac, wchar_t ***av)
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 @@ static void duplicate_ppstrings (int ac, char ***av)
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 @@ __mingw_invalidParameterHandler (const wchar_t * __UNUSED_PARAM_1(expression),
#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);
}

View file

@ -97,10 +97,10 @@ __security_init_cookie (void)
#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

View file

@ -8,8 +8,6 @@
#include <math.h>
#include <stdio.h>
int __defaultmatherr = 0;
typedef int (__cdecl *fUserMathErr)(struct _exception *);
static fUserMathErr stUserMathErr;
@ -20,7 +18,7 @@ void __mingw_raise_matherr (int typ, const char *name, double a1, double a2,
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 @@ _matherr (struct _exception *pexcept)
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;
}

View file

@ -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 @@ _CRT_INIT0(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
static int initialized = 0;
void
__main (void)
__main(void)
{
if (!initialized)
{

View file

@ -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 @@ _IsNonwritableInCurrentImage (PBYTE pTarget)
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;
}

View file

@ -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 @@ __report_error (const char *msg, ...)
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 @@ mark_section_writable (LPVOID addr)
}
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 @@ restore_modified_sections (void)
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 @@ __write_memory (void *addr, const void *src, size_t len)
/* 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

View file

@ -0,0 +1,3 @@
int _MINGW_INSTALL_DEBUG_MATHERR = 0;

View file

@ -10,3 +10,4 @@
otherwise
we do tls cleanup in runtime and _CRT_MT has value 2. */
int _CRT_MT = 2;

View file

@ -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"