diff --git a/src/Build.cmd b/src/Build.cmd index 0e49347..80901de 100644 --- a/src/Build.cmd +++ b/src/Build.cmd @@ -30,19 +30,7 @@ cl /O1 /Os /Oi /GS- /std:c++17 /execution-charset:utf-8 /Zc:sizedDealloc- /Zc:tl if %ErrorLevel% NEQ 0 exit /b %ErrorLevel% ::生成weak符号,一些非必须符号安排为weak可以避免链接失败 -LibMaker.exe FixObj "%~dp0..\\objs\\%Platform%\\%1" /WeakExternFix:__security_cookie=%PointType% /WeakExternFix:__YY_Thunks_Process_Terminating=4 /WeakExternFix:__acrt_atexit_table=%PointType% /WeakExternFix:__pfnDllMainCRTStartupForYY_Thunks=%PointType% -if %ErrorLevel% NEQ 0 exit /b %ErrorLevel% -if "%3"=="" goto:eof -set DEF_FILES=%3 -:AppendWeak -for /f "tokens=1* delims=+" %%a in ("%DEF_FILES%") do ( - echo "AppendWeak %~dp0def\\%Platform%\\%%a" - LibMaker.exe AppendWeak /MACHINE:%Platform% /DEF:"%~dp0def\\%Platform%\\%%a" /OUT:"%~dp0..\\objs\\%Platform%\\%1" - if %ErrorLevel% NEQ 0 exit /b %ErrorLevel% - set DEF_FILES=%%b -) - -if defined DEF_FILES goto :AppendWeak +call:FixObj "%~dp0..\\objs\\%Platform%\\%1" %3 goto:eof @@ -57,21 +45,7 @@ cl /O1 /Os /Oi /GS- /std:c++17 /execution-charset:utf-8 /Zc:sizedDealloc- /Zc:tl if %ErrorLevel% NEQ 0 exit /b %ErrorLevel% ::生成weak符号,一些非必须符号安排为weak可以避免链接失败 -LibMaker.exe FixObj "%~dp0..\\Lib\\%1\\%Platform%\\YY_Thunks_for_%1.obj" /WeakExternFix:__security_cookie=%PointType% /WeakExternFix:__YY_Thunks_Process_Terminating=4 /WeakExternFix:__acrt_atexit_table=%PointType% /WeakExternFix:__pfnDllMainCRTStartupForYY_Thunks=%PointType% -if %ErrorLevel% NEQ 0 exit /b %ErrorLevel% - -if "%3"=="" goto:BuildWeak -set DEF_FILES=%3 -:AppendWeakByLib -for /f "tokens=1* delims=+" %%a in ("%DEF_FILES%") do ( - echo "AppendWeak %~dp0def\\%Platform%\\%%a" - LibMaker.exe /PREFIX:YY_Thunks_ AppendWeak /MACHINE:%Platform% /DEF:"%~dp0def\\%Platform%\\%%a" /OUT:"%~dp0..\\Lib\\%1\\%Platform%\\YY_Thunks_for_%1.obj" - if %ErrorLevel% NEQ 0 exit /b %ErrorLevel% - set DEF_FILES=%%b -) - -if defined DEF_FILES goto:AppendWeakByLib -:BuildWeak +call:FixObj "%~dp0..\\Lib\\%1\\%Platform%\\YY_Thunks_for_%1.obj" %3 set "SupportApiSet=/SupportApiSet" set "WinVersion=%1" @@ -163,3 +137,20 @@ call:BuildX 10.0.19041.0 YY_Thunks_for_Win10.0.19041.obj __WindowsNT10_19041 if %ErrorLevel% NEQ 0 exit /b %ErrorLevel% goto:eof + +:: FixObj "XXX\YY_Thunks_for_Vista.obj" 1.def+2.def +:FixObj +LibMaker.exe FixObj %1 /WeakExternFix:__security_cookie=%PointType% /WeakExternFix:__acrt_atexit_table=%PointType% /WeakExternFix:__pfnDllMainCRTStartupForYY_Thunks=%PointType% +if %ErrorLevel% NEQ 0 exit /b %ErrorLevel% +if "%2"=="" goto:eof +set DEF_FILES=%2 +:AppendWeak +for /f "tokens=1* delims=+" %%a in ("%DEF_FILES%") do ( + echo "AppendWeak %~dp0def\\%Platform%\\%%a" + LibMaker.exe AppendWeak /MACHINE:%Platform% /DEF:"%~dp0def\\%Platform%\\%%a" /OUT:%1 + if %ErrorLevel% NEQ 0 exit /b %ErrorLevel% + set DEF_FILES=%%b +) + +if defined DEF_FILES goto :AppendWeak +goto:eof diff --git a/src/Thunks/DllMainCRTStartup.hpp b/src/Thunks/DllMainCRTStartup.hpp index df4fc75..440714e 100644 --- a/src/Thunks/DllMainCRTStartup.hpp +++ b/src/Thunks/DllMainCRTStartup.hpp @@ -535,9 +535,10 @@ namespace YY::Thunks::internal break; #endif case DLL_PROCESS_DETACH: -#if (YY_Thunks_Target < __WindowsNT5_1) - __YY_Thunks_Process_Terminating = _pReserved != nullptr; -#endif + __if_exists(__YY_Thunks_Process_Terminating) + { + __YY_Thunks_Process_Terminating = _pReserved != nullptr ? -1 : 1; + } #if YY_Thunks_Target < __WindowsNT6 if (internal::GetSystemVersion() < internal::MakeVersion(6, 0) && _tls_index_old == 0 && g_TlsMode == TlsMode::ByDllMainCRTStartupForYY_Thunks) @@ -548,18 +549,15 @@ namespace YY::Thunks::internal if (_pReserved == nullptr) { FreeTlsIndex(); - __YY_uninitialize_winapi_thunks(); } + __YY_uninitialize_winapi_thunks(); return _bRet; } else #endif { auto _bRet = _pfnDllMainCRTStartup(_hInstance, _uReason, _pReserved); - if (_pReserved == nullptr) - { - __YY_uninitialize_winapi_thunks(); - } + __YY_uninitialize_winapi_thunks(); return _bRet; } break; diff --git a/src/Thunks/YY_Thunks.h b/src/Thunks/YY_Thunks.h index 03a6b67..f96fa30 100644 --- a/src/Thunks/YY_Thunks.h +++ b/src/Thunks/YY_Thunks.h @@ -104,27 +104,11 @@ else */ EXTERN_C const UINT64 __YY_Thunks_Installed = YY_Thunks_Target; -/* -瀵煎嚭涓涓閮ㄥ急绗﹀彿锛屾寚绀哄綋鍓嶆槸鍚﹀浜庡己琛屽嵏杞芥ā寮忋 - -EXTERN_C BOOL __YY_Thunks_Process_Terminating; - -BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) -{ - switch(dwReason) - { - case DLL_PROCESS_DETACH: - //鎴戜滑鍙互閫氳繃 lpReserved != NULL 鍒ゆ柇锛屽綋鍓嶆槸鍚﹀浜庡己琛屽嵏杞芥ā寮忋 - __YY_Thunks_Process_Terminating = lpReserved != NULL; - - 鈥︹ - break; - 鈥︹ -*/ -#if (YY_Thunks_Target < __WindowsNT5_1) -//Windows 2000涓嶆敮鎸丷tlDllShutdownInProgress锛屽洜姝や緷鐒跺紩鍏_YY_Thunks_Process_Terminating -EXTERN_C extern BOOL __YY_Thunks_Process_Terminating; -#endif +// 浠嶥llMain缂撳瓨RtlDllShutdownInProgress鐘舵侊紝瑙勯伩閫鍑烘椂璋冪敤RtlDllShutdownInProgress銆 +// 0锛氱紦瀛樻棤鏁 +// 1锛氭ā鍧楁甯稿嵏杞 +// -1锛氬紑濮嬭繘绋嬪噯澶囩粓姝 +static int __YY_Thunks_Process_Terminating; #if (YY_Thunks_Target < __WindowsNT6) static HANDLE _GlobalKeyedEventHandle; @@ -560,6 +544,26 @@ static UINT_PTR GetSecurityNewCookie() } static volatile ThunksInitStatus s_eThunksStatus /*= ThunksInitStatus::None*/; +extern "C" extern const IMAGE_DOS_HEADER __ImageBase; + +static bool __YY_DllShutdownInProgress() +{ + __if_exists(__YY_Thunks_Process_Terminating) + { + if (__YY_Thunks_Process_Terminating != 0) + return __YY_Thunks_Process_Terminating == -1; + } + +#if (YY_Thunks_Target < __WindowsNT5_1) || !defined(__USING_NTDLL_LIB) + if (const auto RtlDllShutdownInProgress = (decltype(::RtlDllShutdownInProgress)*)GetProcAddress(try_get_module_ntdll(), "RtlDllShutdownInProgress")) +#endif + { + return RtlDllShutdownInProgress(); + } + + // 鎸夌悊璇翠笉澶彲鑳借蛋鍒拌繖閲岋紝闅鹃亾鏄佺郴缁熸椂娌℃湁琚獶llMain鎺ョ锛 + return false; +} static void __cdecl __YY_uninitialize_winapi_thunks() { @@ -573,18 +577,8 @@ static void __cdecl __YY_uninitialize_winapi_thunks() return; //褰揇LL琚己琛屽嵏杞芥椂锛屾垜浠粈涔堥兘涓嶅仛锛屽洜涓轰緷璧栫殑鍑芥暟鎸囬拡鍙兘鏄棤鏁堢殑銆 - __if_exists(__YY_Thunks_Process_Terminating) - { - if (__YY_Thunks_Process_Terminating) - return; - } -#if (YY_Thunks_Target < __WindowsNT5_1) || !defined(__USING_NTDLL_LIB) - if (const auto RtlDllShutdownInProgress = (decltype(::RtlDllShutdownInProgress)*)GetProcAddress(try_get_module_ntdll(), "RtlDllShutdownInProgress")) -#endif - { - if (RtlDllShutdownInProgress()) - return; - } + if(__YY_DllShutdownInProgress()) + return; auto pModule = (HMODULE*)__YY_THUNKS_MODULE_START; auto pModuleEnd = (HMODULE*)__YY_THUNKS_FUN_START; @@ -685,6 +679,11 @@ static int __cdecl __YY_initialize_winapi_thunks() return 0; } + // 褰撳墠妯″潡鏄痚xe涓嶆敞鍐屽弽鍒濆鍖栵紝濡傛灉妯″潡灏辨槸exe鑷繁锛岄偅涔堝弽鍒濆鍖栧繀鐒朵唬琛ㄨ繘绋嬮鍑 + // 褰撹繘绋嬮鍑烘椂锛屽叧闂彞鏌勬垨鑰呭嵏杞紻LL杩欎簺閮戒笉鏄繀椤昏繘琛岀殑锛屼互鎻愰珮鎬ц兘銆 + if (PVOID(&__ImageBase) == ((TEB*)NtCurrentTeb())->ProcessEnvironmentBlock->ImageBaseAddress) + return 0; + // 濡傛灉 == null锛岄偅涔堟湁2绉嶆儏鍐碉細 // 1. 闈濽CRT锛屾瘮濡2008锛孷C6锛岃繖鏃讹紝璋冪敤 atexit鏄畨鍏ㄧ殑锛屽洜涓篴texit鍦 XIA鍒濆鍖栧畬鎴愪簡銆 // 2. UCRT 骞朵笖澶勪簬MD鐘舵併傝繖鏃 atexit鍒濆鍖 浼氭彁涓 XIA锛岃繖鏃朵篃鏄病鏈夐棶棰樼殑銆 diff --git a/src/YY-Thunks.UnitTest/weak.c b/src/YY-Thunks.UnitTest/weak.c index 6cd4b85..0b33576 100644 --- a/src/YY-Thunks.UnitTest/weak.c +++ b/src/YY-Thunks.UnitTest/weak.c @@ -1,6 +1,4 @@ 锘 -const int __YY_Thunks_Process_Terminating; - const void* __acrt_atexit_table; const void* __pfnDllMainCRTStartupForYY_Thunks;