diff --git a/premake5.lua b/premake5.lua index f1e054d5..74a232d0 100644 --- a/premake5.lua +++ b/premake5.lua @@ -310,6 +310,13 @@ project "tlsdll" kind "SharedLib" language "C++" + symbols 'Off' + exceptionhandling "Off" + flags {"NoRuntimeChecks", "NoBufferSecurityCheck", "OmitDefaultLibrary"} + buildoptions {"/Zc:threadSafeInit-"} + removebuildoptions {"/GL"} + linkoptions {"/NODEFAULTLIB", "/IGNORE:4210"} + files {"./src/tlsdll/**.rc", "./src/tlsdll/**.hpp", "./src/tlsdll/**.cpp", "./src/tlsdll/resources/**.*"} includedirs {"./src/tlsdll", "%{prj.location}/src"} diff --git a/src/tlsdll/dllmain.cpp b/src/tlsdll/dllmain.cpp index 56d31a66..969d99b3 100644 --- a/src/tlsdll/dllmain.cpp +++ b/src/tlsdll/dllmain.cpp @@ -1,3 +1,6 @@ +#define WIN32_LEAN_AND_MEAN +#include "Windows.h" + #define TLS_PAYLOAD_SIZE 0x2000 thread_local char tls_data[TLS_PAYLOAD_SIZE]; @@ -5,3 +8,13 @@ __declspec(dllexport) void* get_tls_data() { return &tls_data[0]; } + +int WINAPI _DllMainCRTStartup(const HMODULE module, const unsigned long reason, void*) +{ + if (reason == DLL_PROCESS_ATTACH) + { + DisableThreadLibraryCalls(module); + } + + return 1; +} diff --git a/src/tlsdll/resource.rc b/src/tlsdll/resource.rc index 2a778f5f..e9db05fe 100644 --- a/src/tlsdll/resource.rc +++ b/src/tlsdll/resource.rc @@ -60,7 +60,7 @@ BEGIN BEGIN BLOCK "040904b0" BEGIN - VALUE "CompanyName", "X Labs" + VALUE "CompanyName", "momo5502" VALUE "FileDescription", "TLS index allocation dll" VALUE "FileVersion", "1.0.0.0" VALUE "InternalName", "TLS DLL" diff --git a/src/tlsdll/tlssup.cpp b/src/tlsdll/tlssup.cpp new file mode 100644 index 00000000..91d2e197 --- /dev/null +++ b/src/tlsdll/tlssup.cpp @@ -0,0 +1,93 @@ +/*** + *tlssup.cpp - Thread Local Storage run-time support module + * + * Copyright (c) Microsoft Corporation. All rights reserved. + * + *Purpose: + * + ****/ + +#undef CRTDLL +#undef MRTDLL + +#pragma section(".CRT$XLA", long, read) // First Loader TLS Callback +#pragma section(".CRT$XLC", long, read) // CRT TLS Constructor +#pragma section(".CRT$XLD", long, read) // CRT TLS Terminator +#pragma section(".CRT$XLZ", long, read) // Last Loader TLS Callback + +#pragma section(".rdata$T", long, read) + +#define _CRTALLOC(x) __declspec(allocate(x)) + + //#include +#include + +extern "C" { + + /* Thread Local Storage index for this .EXE or .DLL */ + + ULONG _tls_index = 0; + ULONG _tls_array = 0; + + /* Special symbols to mark start and end of Thread Local Storage area. */ + +#pragma data_seg(".tls") + +#if defined(_M_X64) + _CRTALLOC(".tls") +#endif /* defined (_M_X64) */ + char _tls_start = 0; + +#pragma data_seg(".tls$ZZZ") + +#if defined(_M_X64) + _CRTALLOC(".tls$ZZZ") +#endif /* defined (_M_X64) */ + char _tls_end = 0; + +#pragma data_seg() + + /* Start section for TLS callback array examined by the OS loader code. + * If dynamic TLS initialization is used, then a pointer to __dyn_tls_init + * will be placed in .CRT$XLC by inclusion of tlsdyn.obj. This will cause + * the .CRT$XD? array of individual TLS variable initialization callbacks + * to be walked. + */ + + _CRTALLOC(".CRT$XLA") PIMAGE_TLS_CALLBACK __xl_a = 0; + + /* NULL terminator for TLS callback array. This symbol, __xl_z, is never + * actually referenced anywhere, but it must remain. The OS loader code + * walks the TLS callback array until it finds a NULL pointer, so this makes + * sure the array is properly terminated. + */ + + _CRTALLOC(".CRT$XLZ") PIMAGE_TLS_CALLBACK __xl_z = 0; + +#ifdef _WIN64 + + _CRTALLOC(".rdata$T") + extern const IMAGE_TLS_DIRECTORY64 _tls_used = { + (ULONGLONG)&_tls_start, // start of tls data + (ULONGLONG)&_tls_end, // end of tls data + (ULONGLONG)&_tls_index, // address of tls_index + (ULONGLONG)(&__xl_a + 1), // pointer to call back array + (ULONG)0, // size of tls zero fill + (ULONG)0 // characteristics + }; + +#else /* _WIN64 */ + + _CRTALLOC(".rdata$T") + extern const IMAGE_TLS_DIRECTORY _tls_used = { + (ULONG)(ULONG_PTR)&_tls_start, // start of tls data + (ULONG)(ULONG_PTR)&_tls_end, // end of tls data + (ULONG)(ULONG_PTR)&_tls_index, // address of tls_index + (ULONG)(ULONG_PTR)(&__xl_a + 1), // pointer to call back array + (ULONG)0, // size of tls zero fill + (ULONG)0 // characteristics + }; + +#endif /* _WIN64 */ + +} // extern "C"