[VCRUNTIME][VCSTARTUP] Add separate vcruntime and vcstartup lib

vcruntime contains the code that is linked into ucrtbase (in VS it is also provided as vcruntime140.dll)
vcstartup contains the code that is statically linked into executables that link to ucrtbase.dll. In Visual Studio this is part of msvcrt.lib (the import library for msvcrt), similar to our current msvcrtex, and it gets linked when you link to ucrtbase as well. The name is based on the folder name in the library.
Both libraries share some code, but each file is only compiled once.
This commit is contained in:
Timo Kreuzer 2025-01-17 11:45:05 +02:00
parent 2b2bdabe72
commit 9186b861a6
21 changed files with 79 additions and 26 deletions

View file

@ -102,7 +102,6 @@ include(stdio/stdio.cmake)
include(stdlib/stdlib.cmake)
include(string/string.cmake)
include(time/time.cmake)
include(vcruntime/vcruntime.cmake)
add_library(ucrt OBJECT
${UCRT_CONIO_SOURCES}
@ -128,5 +127,5 @@ add_library(ucrt OBJECT
${UCRT_VCRUNTIME_SOURCES}
)
#target_link_libraries(ucrt pseh)
target_link_libraries(ucrt vcruntime)
add_dependencies(ucrt psdk asm)

View file

@ -1,11 +0,0 @@
//
// _fltused.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of _fltused.
//
// SPDX-License-Identifier: MIT
//
int _fltused = 0x9875;

View file

@ -1,4 +1,4 @@
list(APPEND UCRT_FLOAT_SOURCES
float/_fltused.c
# TBD
)

View file

@ -1,317 +0,0 @@
//
// internal_shared.h
//
// Copyright (c) 2024 Timo Kreuzer
//
// Header for definitions shared between vcruntime and UCRT.
//
// SPDX-License-Identifier: MIT
//
#pragma once
#include <suppress.h>
#include <intrin.h>
#include <corecrt_startup.h>
#include <crtdbg.h>
#include <windows.h> // for HMODULE
#include <malloc.h>
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#if defined(_MSC_VER)
#define _CRTALLOC(x) __declspec(allocate(x))
#else
#define _CRTALLOC(x) __attribute__((section(x)))
#endif
#ifdef _MSC_VER
#pragma section(".CRT$XIC", long, read)
#pragma section(".CRT$XPX", long, read)
#pragma section(".CRT$XPXA", long, read)
#pragma section(".CRT$XIA", long, read)
#pragma section(".CRT$XIZ", long, read)
#pragma section(".CRT$XCA", long, read)
#pragma section(".CRT$XCZ", long, read)
#pragma section(".CRT$XPA", long, read)
#pragma section(".CRT$XPZ", long, read)
#pragma section(".CRT$XTA", long, read)
#pragma section(".CRT$XTZ", long, read)
#endif
extern _PIFV __xi_a[];
extern _PIFV __xi_z[];
extern _PVFV __xc_a[];
extern _PVFV __xc_z[];
extern _PVFV __xp_a[];
extern _PVFV __xp_z[];
extern _PVFV __xt_a[];
extern _PVFV __xt_z[];
extern char __ImageBase;
#define CRT_WARNING_DISABLE_PUSH(wn, message) \
__pragma(warning(push)) \
__pragma(warning(disable: wn))
#define CRT_WARNING_POP \
__pragma(warning(pop))
#define _BEGIN_SECURE_CRT_DEPRECATION_DISABLE \
__pragma(warning(push)) \
__pragma(warning(disable: 4996))
#define _END_SECURE_CRT_DEPRECATION_DISABLE \
__pragma(warning(pop))
#define _CRT_DEBUGGER_IGNORE -1
#define _CRT_DEBUGGER_GSFAILURE 1
#define _CRT_DEBUGGER_INVALIDPARAMETER 2
#define _CRT_DEBUGGER_ABORT 3
#define _CRT_DEBUGGER_HOOK(x)
#define _CRT_SECURITYCRITICAL_ATTRIBUTE
#define _CRT_SECURITYSAFECRITICAL_ATTRIBUTE
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//
// Parameter validation macros
// Partly duplicated in corecrt_internal_strtox.h
//
//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#ifdef _DEBUG
#define _INVALID_PARAMETER(expr) _invalid_parameter(expr, __FUNCTIONW__, __FILEW__, __LINE__, 0)
#else
#define _INVALID_PARAMETER(expr) _invalid_parameter_noinfo()
#endif
#define _VALIDATE_RETURN(expr, errorcode, retexpr) \
{ \
int _Expr_val = !!(expr); \
_ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
if (!(_Expr_val)) \
{ \
*_errno() = (errorcode); \
_INVALID_PARAMETER(_CRT_WIDE(#expr)); \
return (retexpr); \
} \
}
#define _VALIDATE_RETURN_VOID(expr, errorcode) \
{ \
int _Expr_val = !!(expr); \
_ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
if (!(_Expr_val)) \
{ \
*_errno() = (errorcode); \
_INVALID_PARAMETER(_CRT_WIDE(#expr)); \
return; \
} \
}
#define _VALIDATE_RETURN_NOERRNO(expr, retexpr) \
{ \
int _Expr_val = !!(expr); \
_ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
if (!(_Expr_val)) \
{ \
_INVALID_PARAMETER(_CRT_WIDE(#expr)); \
return (retexpr); \
} \
}
#define _VALIDATE_RETURN_ERRCODE(expr, errorcode) \
_VALIDATE_RETURN(expr, errorcode, errorcode)
#define _VALIDATE_CLEAR_OSSERR_RETURN(expr, errorcode, retexpr) \
{ \
int _Expr_val = !!(expr); \
_ASSERT_EXPR((_Expr_val), _CRT_WIDE(#expr)); \
if (!(_Expr_val)) \
{ \
*__doserrno() = 0L; \
*_errno() = errorcode; \
_INVALID_PARAMETER(_CRT_WIDE(#expr) ); \
return (retexpr); \
} \
}
#define _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(expr, errorcode) \
_VALIDATE_CLEAR_OSSERR_RETURN(expr, errorcode, errorcode)
#define _VALIDATE_RETURN_NOEXC(expr, errorcode, retexpr) \
{ \
if (!(expr)) \
{ \
*_errno() = errorcode; \
return (retexpr); \
} \
}
#define _VALIDATE_RETURN_ERRCODE_NOEXC(expr, errorcode) \
_VALIDATE_RETURN_NOEXC(expr, errorcode, errorcode)
#define _malloc_crt malloc
#define _free_crt free
#define _calloc_crt calloc
#define _recalloc_crt _recalloc
#define _malloca_crt _malloca
#define _freea_crt _freea
#ifdef __cplusplus
} // extern "C"
template<typename T>
__forceinline
T __crt_fast_encode_pointer(T ptr)
{
union { T Ptr; uintptr_t Uint; } u = { ptr };
u.Uint ^= __security_cookie;
return u.Ptr;
}
template<typename T>
__forceinline
T __crt_fast_decode_pointer(T ptr)
{
union { T Ptr; uintptr_t Uint; } u = { ptr };
u.Uint ^= __security_cookie;
return u.Ptr;
}
template<typename T>
T __crt_interlocked_read(const T volatile* ptr)
{
return *ptr;
}
template<typename T>
T __crt_interlocked_read_pointer(const T volatile* ptr)
{
return *ptr;
}
template<typename T>
T __crt_interlocked_exchange_pointer(T *ptr, void* value)
{
return (T)_InterlockedExchangePointer((void* volatile*)ptr, value);
}
template<typename T>
struct __crt_seh_guarded_call
{
template<typename Init, typename Action, typename Cleanup>
T operator()(Init init, Action action, Cleanup cleanup)
{
T result;
init();
__try
{
result = action();
}
__finally
{
cleanup();
}
__endtry
return result;
}
};
template<>
struct __crt_seh_guarded_call<void>
{
template<typename Init, typename Action, typename Cleanup>
void operator()(Init init, Action action, Cleanup cleanup)
{
init();
__try
{
action();
}
__finally
{
cleanup();
}
__endtry
}
};
template<typename FuncPtr>
FuncPtr __crt_get_proc_address(HMODULE module, const char* name)
{
return (FuncPtr)GetProcAddress(module, name);
}
template<typename Action, typename Cleanup>
void __crt_call_and_cleanup(Action action, Cleanup cleanup)
{
action();
cleanup();
}
struct __crt_malloc_free_policy
{
template<typename T>
void operator()(T* const ptr) throw()
{
_free_crt((void*)ptr);
}
};
struct __crt_public_free_policy
{
template<typename T>
void operator()(T* const ptr) throw()
{
_free_crt((void*)ptr);
}
};
struct __crt_malloca_free_policy
{
void operator()(void* const ptr) throw()
{
_freea_crt(ptr);
}
};
template <typename T, typename FreePolicy = __crt_malloc_free_policy>
class __crt_unique_heap_ptr
{
T* _ptr;
public:
__crt_unique_heap_ptr() : _ptr(nullptr) {}
__crt_unique_heap_ptr(T* ptr) : _ptr(ptr) {}
__crt_unique_heap_ptr(__crt_unique_heap_ptr&& from) : _ptr(from._ptr) { from._ptr = nullptr; }
~__crt_unique_heap_ptr() { FreePolicy()(_ptr); }
void attach(T* ptr) { FreePolicy()(_ptr); _ptr = ptr; }
T* detach() { T* ptr = _ptr; _ptr = nullptr; return ptr; }
operator bool() const { return _ptr != nullptr; }
T* get() const { return _ptr; }
T** get_address_of() { return &_ptr; }
// Move assignment
__crt_unique_heap_ptr& operator=(__crt_unique_heap_ptr&& from)
{
FreePolicy()(_ptr);
_ptr = from._ptr;
from._ptr = nullptr;
return *this;
}
};
template <typename T>
using __crt_scoped_stack_ptr = __crt_unique_heap_ptr<T, __crt_malloca_free_policy>;
#define _malloc_crt_t(t, n) (__crt_unique_heap_ptr<t>(static_cast<t*>(_malloc_crt((n) * sizeof(t)))))
#define _calloc_crt_t(t, n) (__crt_unique_heap_ptr<t>(static_cast<t*>(_calloc_crt((n), sizeof(t)))))
#define _recalloc_crt_t(t, p, n) (__crt_unique_heap_ptr<t>(static_cast<t*>(_recalloc_crt(p, (n), sizeof(t)))))
#define _malloca_crt_t(t, n) (__crt_scoped_stack_ptr<t>(static_cast<t*>(_malloca_crt((n) * sizeof(t)))))
#endif // __cplusplus

View file

@ -1,13 +0,0 @@
//
// nt.h
//
// Copyright (c) 2024 Timo Kreuzer
//
// Header for generic NT things.
//
// SPDX-License-Identifier: MIT
//
#pragma once
#include <ndk/pstypes.h>

View file

@ -1,15 +0,0 @@
//
// ntrtl.h
//
// Copyright (c) 2024 Timo Kreuzer
//
// Header for RTL NT things.
//
// SPDX-License-Identifier: MIT
//
#pragma once
#include <ndk/rtltypes.h>
#define RTL_USER_PROC_SECURE_PROCESS 0x80000000

View file

@ -1,11 +0,0 @@
//
// nturtl.h
//
// Copyright (c) 2024 Timo Kreuzer
//
// Header for user mode RTL NT things.
//
// SPDX-License-Identifier: MIT
//
#pragma once

View file

@ -1,17 +0,0 @@
//
// __scrt_uninitialize_crt.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of __scrt_uninitialize_crt.
//
// SPDX-License-Identifier: MIT
//
extern "C"
bool
__cdecl
__scrt_uninitialize_crt(bool is_terminating, bool from_exit)
{
return true;
}

View file

@ -1,6 +1,5 @@
list(APPEND UCRT_STARTUP_SOURCES
startup/__scrt_uninitialize_crt.cpp
startup/abort.cpp
startup/argv_data.cpp
startup/argv_parsing.cpp

View file

@ -1,21 +0,0 @@
//
// __report_gsfailure.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of __report_gsfailure.
//
// SPDX-License-Identifier: MIT
//
#include <intrin.h>
#include <ntrtl.h>
#if defined(_M_IX86)
__declspec(noreturn) void __cdecl __report_gsfailure(void)
#else
__declspec(noreturn) void __cdecl __report_gsfailure(_In_ uintptr_t _StackCookie)
#endif
{
__fastfail(FAST_FAIL_STACK_COOKIE_CHECK_FAILURE);
}

View file

@ -1,17 +0,0 @@
//
// __report_rangecheckfailure.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of __report_rangecheckfailure.
//
// SPDX-License-Identifier: MIT
//
#include <intrin.h>
#include <ntrtl.h>
__declspec(noreturn) void __cdecl __report_rangecheckfailure(void)
{
__fastfail(FAST_FAIL_RANGE_CHECK_FAILURE);
}

View file

@ -1,87 +0,0 @@
//
// __security_init_cookie.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of __security_init_cookie.
//
// SPDX-License-Identifier: MIT
//
#include <stdlib.h>
#include <intrin.h>
#include <corecrt_internal.h>
#ifdef _WIN64
#define DEFAULT_SECURITY_COOKIE 0x00002B992DDFA232ull
#define _rotlptr _rotl64
#else
#define DEFAULT_SECURITY_COOKIE 0xBB40E64E
#define _rotlptr _rotl
#endif
uintptr_t __security_cookie = DEFAULT_SECURITY_COOKIE;
uintptr_t __security_cookie_complement = ~DEFAULT_SECURITY_COOKIE;
void __security_init_cookie(void)
{
LARGE_INTEGER performanceCounter;
FILETIME fileTime;
uintptr_t randomValue = (uintptr_t)0x27E30B2C16B07297ull;
#if defined(_M_IX86) || defined(_M_X64)
if (IsProcessorFeaturePresent(PF_RDRAND_INSTRUCTION_AVAILABLE))
{
#ifdef _M_X64
while (!_rdrand64_step(&randomValue))
_mm_pause();
#else
while (!_rdrand32_step(&randomValue))
_mm_pause();
#endif
}
if (IsProcessorFeaturePresent(PF_RDTSC_INSTRUCTION_AVAILABLE))
{
randomValue += __rdtsc();
}
#endif
randomValue += (uintptr_t)&randomValue;
randomValue ^= GetTickCount();
QueryPerformanceCounter(&performanceCounter);
#ifdef _WIN64
randomValue ^= performanceCounter.QuadPart;
#else
randomValue ^= performanceCounter.LowPart;
randomValue ^= performanceCounter.HighPart;
#endif
randomValue += GetCurrentThreadId();
randomValue = _rotlptr(randomValue, GetCurrentThreadId() >> 2);
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
GetSystemTimePreciseAsFileTime(&fileTime);
#else
GetSystemTimeAsFileTime(&fileTime);
#endif
randomValue += fileTime.dwLowDateTime;
randomValue += fileTime.dwHighDateTime;
randomValue += GetCurrentProcessId();
randomValue = _rotlptr(randomValue, GetCurrentProcessId() >> 2);
if (randomValue == DEFAULT_SECURITY_COOKIE)
{
randomValue++;
}
#ifdef _WIN64
/* Zero out highest 16 bits */
randomValue &= 0x0000FFFFFFFFFFFFull;
#endif
__security_cookie = randomValue;
__security_cookie_complement = ~randomValue;
}

View file

@ -1,36 +0,0 @@
//
// __vcrt_init.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of vcruntime initialization and termination functions.
//
// SPDX-License-Identifier: MIT
//
#include <vcruntime_startup.h>
__vcrt_bool __cdecl __vcrt_initialize(void)
{
return 1;
}
__vcrt_bool __cdecl __vcrt_uninitialize(_In_ __vcrt_bool _Terminating)
{
return 1;
}
__vcrt_bool __cdecl __vcrt_uninitialize_critical(void)
{
return 1;
}
__vcrt_bool __cdecl __vcrt_thread_attach(void)
{
return 1;
}
__vcrt_bool __cdecl __vcrt_thread_detach(void)
{
return 1;
}

View file

@ -1,27 +0,0 @@
//
// __security_check_cookie.asm
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of __security_check_cookie for x64.
//
// SPDX-License-Identifier: MIT
//
#include <asm.inc>
EXTERN __security_cookie:QWORD
EXTERN __report_gsfailure:PROC
.code64
// This function must not clobber any registers!
PUBLIC __security_check_cookie
__security_check_cookie:
cmp rcx, qword ptr __security_cookie[rip]
jne __security_check_cookie_fail
ret
__security_check_cookie_fail:
jmp __report_gsfailure
END

View file

@ -1,27 +0,0 @@
//
// __security_check_cookie.asm
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of __security_check_cookie for x86.
//
// SPDX-License-Identifier: MIT
//
#include <asm.inc>
EXTERN ___security_cookie:QWORD
EXTERN ___report_gsfailure:PROC
.code
// This function must not clobber any registers!
PUBLIC ___security_check_cookie
___security_check_cookie:
cmp ecx, dword ptr [___security_cookie]
jne ___security_check_cookie_fail
ret
___security_check_cookie_fail:
jmp ___report_gsfailure
END

View file

@ -1,22 +0,0 @@
//
// section_markers.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Markers for CRT initializer sections.
//
// SPDX-License-Identifier: MIT
//
#include <internal_shared.h>
_CRTALLOC(".CRT$XIA") _PIFV __xi_a[] = { 0 };
_CRTALLOC(".CRT$XIZ") _PIFV __xi_z[] = { 0 };
_CRTALLOC(".CRT$XCA") _PVFV __xc_a[] = { 0 };
_CRTALLOC(".CRT$XCZ") _PVFV __xc_z[] = { 0 };
_CRTALLOC(".CRT$XPA") _PVFV __xp_a[] = { 0 };
_CRTALLOC(".CRT$XPZ") _PVFV __xp_z[] = { 0 };
_CRTALLOC(".CRT$XTA") _PVFV __xt_a[] = { 0 };
_CRTALLOC(".CRT$XTZ") _PVFV __xt_z[] = { 0 };
#pragma comment(linker, "/merge:.CRT=.rdata")

View file

@ -1,65 +0,0 @@
//
// isa_available.c
//
// Copyright (c) 2024 Timo Kreuzer
//
// Implementation of __isa_available_init.
//
// SPDX-License-Identifier: MIT
//
#include <isa_availability.h>
#include <intrin.h>
#include <windef.h>
#include <winbase.h>
extern "C" { int __isa_available = 0; }
extern "C"
int
__cdecl
__isa_available_init(void)
{
#if defined(_M_IX86) || defined(_M_X64)
if (IsProcessorFeaturePresent(PF_AVX512F_INSTRUCTIONS_AVAILABLE))
{
__isa_available = __ISA_AVAILABLE_AVX512;
}
else if (IsProcessorFeaturePresent(PF_AVX2_INSTRUCTIONS_AVAILABLE))
{
__isa_available = __ISA_AVAILABLE_AVX2;
}
else if (IsProcessorFeaturePresent(PF_AVX_INSTRUCTIONS_AVAILABLE))
{
__isa_available = __ISA_AVAILABLE_AVX;
}
else if (IsProcessorFeaturePresent(PF_SSE4_2_INSTRUCTIONS_AVAILABLE))
{
__isa_available = __ISA_AVAILABLE_SSE42;
}
else if (IsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE))
{
__isa_available = __ISA_AVAILABLE_SSE2;
}
else
{
__isa_available = __ISA_AVAILABLE_X86;
}
#elif defined(_M_ARM) || defined(_M_ARM64)
// CHECKME: Is this correct?
if (IsProcessorFeaturePresent(PF_ARM_V8_INSTRUCTIONS_AVAILABLE))
{
#ifdef _M_ARM64
__isa_available = __ISA_AVAILABLE_NEON_ARM64;
#else
__isa_available = __ISA_AVAILABLE_NEON;
#endif
}
else
{
__isa_available = __ISA_AVAILABLE_ARMNT;
}
#endif
return 0;
}

View file

@ -1,19 +0,0 @@
list(APPEND UCRT_VCRUNTIME_SOURCES
vcruntime/__report_gsfailure.c
vcruntime/__report_rangecheckfailure.c
vcruntime/__security_init_cookie.c
vcruntime/__vcrt_init.c
vcruntime/initializers.cpp
vcruntime/isa_available.cpp
)
if(${ARCH} STREQUAL "i386")
list(APPEND UCRT_VCRUNTIME_SOURCES
vcruntime/i386/__security_check_cookie.s
)
elseif(${ARCH} STREQUAL "amd64")
list(APPEND UCRT_VCRUNTIME_SOURCES
vcruntime/amd64/__security_check_cookie.s
)
endif()