[VCSTARTUP] Implement (w)mainCRTStartup

__acrt_initialize is here as a stub to later support linking ucrt statically. When it is statically linked the real function should be called, but when the executable links to ucrtdll, the initialization happens when the DLL is loaded, so we call a stub here.
This commit is contained in:
Timo Kreuzer 2025-01-28 21:56:12 +02:00
parent d243b6cdf3
commit a978879ddb
5 changed files with 136 additions and 0 deletions

View file

@ -31,7 +31,10 @@ list(APPEND VCRT_RUNTIME_SOURCES
)
list(APPEND VCRT_STARTUP_SOURCES
__acrt_initialize_stub.cpp
__scrt_uninitialize_crt.cpp
mainCRTStartup.cpp
wmainCRTStartup.cpp
)
if(${ARCH} STREQUAL "i386")

View file

@ -0,0 +1,8 @@
#include <internal_shared.h>
extern "C"
__crt_bool __cdecl __acrt_initialize()
{
return true;
}

View file

@ -0,0 +1,89 @@
//
// commonCRTStartup.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of common executable startup code.
//
// SPDX-License-Identifier: MIT
//
#include <corecrt_startup.h>
#include <internal_shared.h>
#include <excpt.h>
#include <stdlib.h>
#include <intrin.h>
#include <pseh/pseh2.h>
// Defined in winnt.h
#define FAST_FAIL_FATAL_APP_EXIT 7
extern "C" int __cdecl main(int, char**, char**);
extern "C" int __cdecl wmain(int, wchar_t**, wchar_t**);
template<typename Tmain>
static int call_main();
template<>
int call_main<decltype(main)>()
{
_configure_narrow_argv(_crt_argv_unexpanded_arguments);
return main(*__p___argc(), *__p___argv(), _get_initial_narrow_environment());
}
template<>
int call_main<decltype(wmain)>()
{
_configure_wide_argv(_crt_argv_unexpanded_arguments);
return wmain(*__p___argc(), *__p___wargv(), _get_initial_wide_environment());
}
static bool __scrt_initialize()
{
__isa_available_init();
if (!__vcrt_initialize())
{
return false;
}
if (!__acrt_initialize())
{
__vcrt_uninitialize(false);
return false;
}
if (_initterm_e(__xi_a, __xi_z) != 0)
{
return false;
}
_initterm(__xc_a, __xc_z);
return true;
}
template<typename Tmain>
static __declspec(noinline) int __cdecl __commonCRTStartup()
{
int exitCode;
if (!__scrt_initialize())
{
__fastfail(FAST_FAIL_FATAL_APP_EXIT);
}
__try
{
exitCode = call_main<Tmain>();
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
exitCode = GetExceptionCode();
}
__endtry
exit(exitCode);
}

View file

@ -0,0 +1,18 @@
//
// mainCRTStartup.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of ANSI executable entry point.
//
// SPDX-License-Identifier: MIT
//
#include "commonCRTStartup.hpp"
extern "C" unsigned long mainCRTStartup(void*)
{
__security_init_cookie();
return __commonCRTStartup<decltype(main)>();
}

View file

@ -0,0 +1,18 @@
//
// wmainCRTStartup.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of unicode executable entry point.
//
// SPDX-License-Identifier: MIT
//
#include "commonCRTStartup.hpp"
extern "C" unsigned long wmainCRTStartup(void*)
{
__security_init_cookie();
return __commonCRTStartup<decltype(wmain)>();
}