Skip to content

Commit

Permalink
Fea #30, 为LLD添加lib依赖模式
Browse files Browse the repository at this point in the history
  • Loading branch information
mingkuang-Chuyu committed Jun 1, 2024
1 parent 0754978 commit de52011
Show file tree
Hide file tree
Showing 6 changed files with 2,026 additions and 1,980 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/Build&Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
&7z e Bin\Procdump.zip "-o$Env:GITHUB_WORKSPACE\Bin"
Invoke-WebRequest -Uri https://github.com/Chuyu-Team/LibMaker/releases/download/v1.0.2/LibMaker.exe -OutFile Bin\LibMaker.exe
Invoke-WebRequest -Uri https://github.com/Chuyu-Team/LibMaker/releases/download/v1.0.3/LibMaker.exe -OutFile Bin\LibMaker.exe
Invoke-WebRequest -Uri https://github.com/Chuyu-Team/YY-Thunks/releases/download/v1.0.2.8/MinimumRequiredVersionHelper.exe -OutFile Bin\MinimumRequiredVersionHelper.exe
$ProgramFiles = ${env:ProgramFiles(x86)}
Expand Down Expand Up @@ -163,6 +163,7 @@ jobs:
pushd ".\src\YY.Depends.Analyzer"
7z a -tzip %GITHUB_WORKSPACE%\YY-Thunks-${{env.BuildVersion}}-Binary.zip Config
popd
7z a -tzip %GITHUB_WORKSPACE%\YY-Thunks-${{env.BuildVersion}}-Lib-Binary.zip Lib LICENSE ReadMe.md ThunksList.md
::打包Nuget
if "${{env.BuildVersion}}" NEQ "" (
Expand Down
74 changes: 56 additions & 18 deletions src/Build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,76 @@ if defined DEF_FILES goto :AppendWeak

goto:eof

:: BuildObj 6.0.6000.0 NTDDI_WIN6 1.def+2.def
:BuildLib
md "Lib\\%1\\%Platform%"

cl /O1 /Os /Oi /GS- /std:c++17 /arch:IA32 /Z7 /MT /Fo"Lib\\%1\\%Platform%\\YY_Thunks_for_%1.obj" /Zl /c /D "NDEBUG" /D "YY_Thunks_Support_Version=%2" /D "__FALLBACK_PREFIX=YY_Thunks_" "%~dp0Thunks\YY_Thunks.cpp"

::生成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 "%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"
set DEF_FILES=%%b
)

if defined DEF_FILES goto:AppendWeakByLib
:BuildWeak

set "SupportApiSet=/SupportApiSet"
:: XP ~ Win7不支持API Set
if "%1"=="5.1.2600.0" (set "SupportApiSet=")
if "%1"=="5.2.3790.0" (set "SupportApiSet=")
if "%1"=="6.0.6000.0" (set "SupportApiSet=")
if "%1"=="6.1.7600.0" (set "SupportApiSet=")

LibMaker.exe %SupportApiSet% /PREFIX:YY_Thunks_ BuildYY_ThunksLibraries "%~dp0..\\Lib\\%1\\%Platform%\\YY_Thunks_for_%1.obj" "%WindowsSdkDir%\\Lib\\%WindowsSDKLibVersion%\\um\\%Platform%" "%~dp0..\\Lib\\%1\\%Platform%"

goto:eof

:: 6.0.6000.0 YY_Thunks_for_Vista.obj NTDDI_WIN6 1.def+2.def
:BuildX
call:BuildObj %2 %3 %4
call:BuildLib %1 %3 %4
goto:eof

:Buildx86
set PointType=4
call:BuildObj YY_Thunks_for_Win2K.obj NTDDI_WIN2K PSAPI2Kernel32.def+esent.def
call:BuildObj YY_Thunks_for_WinXP.obj NTDDI_WINXP PSAPI2Kernel32.def+esent.def
call:BuildObj YY_Thunks_for_Vista.obj NTDDI_WIN6 PSAPI2Kernel32.def
call:BuildObj YY_Thunks_for_Win7.obj NTDDI_WIN7
call:BuildObj YY_Thunks_for_Win8.obj NTDDI_WIN8
call:BuildObj YY_Thunks_for_Win10.0.10240.obj NTDDI_WIN10
call:BuildObj YY_Thunks_for_Win10.0.19041.obj NTDDI_WIN10_VB
call:BuildX 5.0.2195.0 YY_Thunks_for_Win2K.obj NTDDI_WIN2K PSAPI2Kernel32.def+esent.def
call:BuildX 5.1.2600.0 YY_Thunks_for_WinXP.obj NTDDI_WINXP PSAPI2Kernel32.def+esent.def
call:BuildX 6.0.6000.0 YY_Thunks_for_Vista.obj NTDDI_WIN6 PSAPI2Kernel32.def
call:BuildX 6.1.7600.0 YY_Thunks_for_Win7.obj NTDDI_WIN7
call:BuildX 6.2.9200.0 YY_Thunks_for_Win8.obj NTDDI_WIN8
call:BuildX 10.0.10240.0 YY_Thunks_for_Win10.0.10240.obj NTDDI_WIN10
call:BuildX 10.0.19041.0 YY_Thunks_for_Win10.0.19041.obj NTDDI_WIN10_VB
goto:eof


:Buildx64
set PointType=8
call:BuildObj YY_Thunks_for_WinXP.obj NTDDI_WS03SP1 PSAPI2Kernel32.def+esent.def
call:BuildObj YY_Thunks_for_Vista.obj NTDDI_WIN6 PSAPI2Kernel32.def
call:BuildObj YY_Thunks_for_Win7.obj NTDDI_WIN7
call:BuildObj YY_Thunks_for_Win8.obj NTDDI_WIN8
call:BuildObj YY_Thunks_for_Win10.0.10240.obj NTDDI_WIN10
call:BuildObj YY_Thunks_for_Win10.0.19041.obj NTDDI_WIN10_VB
call:BuildX 5.2.3790.1180 YY_Thunks_for_WinXP.obj NTDDI_WS03SP1 PSAPI2Kernel32.def+esent.def
call:BuildX 6.0.6000.0 YY_Thunks_for_Vista.obj NTDDI_WIN6 PSAPI2Kernel32.def
call:BuildX 6.1.7600.0 YY_Thunks_for_Win7.obj NTDDI_WIN7
call:BuildX 6.2.9200.0 YY_Thunks_for_Win8.obj NTDDI_WIN8
call:BuildX 10.0.10240.0 YY_Thunks_for_Win10.0.10240.obj NTDDI_WIN10
call:BuildX 10.0.19041.0 YY_Thunks_for_Win10.0.19041.obj NTDDI_WIN10_VB
goto:eof

:Buildarm
set PointType=4
call:BuildObj YY_Thunks_for_Win8.obj NTDDI_WIN8
call:BuildObj YY_Thunks_for_Win10.0.10240.obj NTDDI_WIN10
call:BuildObj YY_Thunks_for_Win10.0.19041.obj NTDDI_WIN10_VB
call:BuildX 6.2.9200.0 YY_Thunks_for_Win8.obj NTDDI_WIN8
call:BuildX 10.0.10240.0 YY_Thunks_for_Win10.0.10240.obj NTDDI_WIN10
call:BuildX 10.0.19041.0 YY_Thunks_for_Win10.0.19041.obj NTDDI_WIN10_VB
goto:eof

:Buildarm64
set PointType=8
; NTDDI_WIN10_RS3 = 16299
call:BuildObj YY_Thunks_for_Win10.0.10240.obj NTDDI_WIN10_RS3
call:BuildObj YY_Thunks_for_Win10.0.19041.obj NTDDI_WIN10_VB
call:BuildX 10.0.10240.0 YY_Thunks_for_Win10.0.10240.obj NTDDI_WIN10_RS3
call:BuildX 10.0.19041.0 YY_Thunks_for_Win10.0.19041.obj NTDDI_WIN10_VB
goto:eof
30 changes: 20 additions & 10 deletions src/Thunks/YY_Thunks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,18 @@ RtlCutoverTimeToSystemTime(

#include <HookThunk.h>

#ifndef __FALLBACK_PREFIX
#define __FALLBACK_PREFIX
#endif

//展开函数的所有的 声明 以及 try_get_ 函数
#define __DEFINE_THUNK(_MODULE, _SIZE, _RETURN_, _CONVENTION_, _FUNCTION, ...) \
__APPLY_UNIT_TEST_BOOL(_FUNCTION); \
EXTERN_C _RETURN_ _CONVENTION_ _FUNCTION(__VA_ARGS__); \
static decltype(_FUNCTION)* __cdecl _CRT_CONCATENATE(try_get_, _FUNCTION)() noexcept; \
#define __DEFINE_THUNK_EXTERN_PREFIX(_PREFIX, _MODULE, _SIZE, _RETURN_, _CONVENTION_, _FUNCTION, ...) \
__APPLY_UNIT_TEST_BOOL(_FUNCTION); \
EXTERN_C _RETURN_ _CONVENTION_ _CRT_CONCATENATE_(_PREFIX, _FUNCTION)(__VA_ARGS__); \
static decltype(_CRT_CONCATENATE_(_PREFIX, _FUNCTION))* __cdecl _CRT_CONCATENATE(try_get_, _FUNCTION)() noexcept; \
__if_not_exists(_CRT_CONCATENATE(try_get_, _FUNCTION))

#define __DEFINE_THUNK(_MODULE, _SIZE, _RETURN_, _CONVENTION_, _FUNCTION, ...) __DEFINE_THUNK_EXTERN_PREFIX(__FALLBACK_PREFIX, _MODULE, _SIZE, _RETURN_, _CONVENTION_, _FUNCTION, __VA_ARGS__)

#include "Thunks\YY_Thunks_List.hpp"

Expand Down Expand Up @@ -781,10 +786,13 @@ namespace YY::Thunks::internal

#include "ThreadRunner.h"

#define _DEFINE_IAT_SYMBOL_PREFIX(_PREFIX, _FUNCTION, _SIZE) _LCRT_DEFINE_IAT_SYMBOL(_PREFIX ## _FUNCTION, _SIZE)
#define _YY_THUNKS_DEFINE_RUST_RAW_DYLIB_IAT_SYMBOL_PREFIX(_PREFIX, _FUNCTION, _SIZE) _YY_THUNKS_DEFINE_RUST_RAW_DYLIB_IAT_SYMBOL(_FUNCTION, _SIZE, _PREFIX ## _FUNCTION)

//导入实际的实现
#define YY_Thunks_Implemented
#define __DEFINE_THUNK(_MODULE, _SIZE, _RETURN_, _CONVENTION_, _FUNCTION, ...) \
static decltype(_FUNCTION)* __cdecl _CRT_CONCATENATE(try_get_, _FUNCTION)() noexcept \
#define __DEFINE_THUNK_IMP_PREFIX(_PREFIX, _MODULE, _SIZE, _RETURN_, _CONVENTION_, _FUNCTION, ...) \
static decltype(_CRT_CONCATENATE_(_PREFIX, _FUNCTION))* __cdecl _CRT_CONCATENATE(try_get_, _FUNCTION)() noexcept \
{ \
__CHECK_UNIT_TEST_BOOL(_FUNCTION); \
__declspec(allocate(".YYThr$AAA")) static void* _CRT_CONCATENATE(pInit_ ,_FUNCTION) = \
Expand All @@ -801,13 +809,15 @@ __if_exists(YY::Thunks::Fallback::_CRT_CONCATENATE(try_get_, _FUNCTION))
&YY::Thunks::Fallback::_CRT_CONCATENATE(try_get_, _FUNCTION) \
} \
}; \
return reinterpret_cast<decltype(_FUNCTION)*>(try_get_function( \
return reinterpret_cast<decltype(_CRT_CONCATENATE_(_PREFIX, _FUNCTION))*>(try_get_function( \
&_CRT_CONCATENATE(pFun_ ,_FUNCTION), \
_ProcInfo)); \
} \
_LCRT_DEFINE_IAT_SYMBOL(_FUNCTION, _SIZE); \
_YY_THUNKS_DEFINE_RUST_RAW_DYLIB_IAT_SYMBOL(_FUNCTION, _SIZE); \
EXTERN_C _RETURN_ _CONVENTION_ _FUNCTION(__VA_ARGS__)
_DEFINE_IAT_SYMBOL_PREFIX(_PREFIX, _FUNCTION, _SIZE); \
_YY_THUNKS_DEFINE_RUST_RAW_DYLIB_IAT_SYMBOL_PREFIX(_PREFIX, _FUNCTION, _SIZE); \
EXTERN_C _RETURN_ _CONVENTION_ _CRT_CONCATENATE_(_PREFIX, _FUNCTION)(__VA_ARGS__)

#define __DEFINE_THUNK(_MODULE, _SIZE, _RETURN_, _CONVENTION_, _FUNCTION, ...) __DEFINE_THUNK_IMP_PREFIX(__FALLBACK_PREFIX, _MODULE, _SIZE, _RETURN_, _CONVENTION_, _FUNCTION, __VA_ARGS__)

#include "YY_Thunks_List.hpp"

Expand Down
6 changes: 3 additions & 3 deletions src/Thunks/YY_Thunks.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@

#if defined(_M_IX86)
// 根据 https://github.com/Chuyu-Team/YY-Thunks/issues/78 修正一下rust raw-dylib引用规则
#define _YY_THUNKS_DEFINE_RUST_RAW_DYLIB_IAT_SYMBOL(_FUNCTION, _SIZE) \
#define _YY_THUNKS_DEFINE_RUST_RAW_DYLIB_IAT_SYMBOL(_FUNCTION, _SIZE, _FUNCTION_ADDRESS) \
__pragma(warning(suppress:4483)) \
extern "C" __declspec(selectany) void const* const __identifier(_CRT_STRINGIZE_(_imp_ ## _FUNCTION)) \
= reinterpret_cast<void const*>(_FUNCTION)
= reinterpret_cast<void const*>(_FUNCTION_ADDRESS)
#else
#define _YY_THUNKS_DEFINE_RUST_RAW_DYLIB_IAT_SYMBOL(_FUNCTION, _SIZE)
#define _YY_THUNKS_DEFINE_RUST_RAW_DYLIB_IAT_SYMBOL(_FUNCTION, _SIZE, _FUNCTION_ADDRESS)
#endif

#ifdef __YY_Thunks_Unit_Test
Expand Down
Loading

0 comments on commit de52011

Please sign in to comment.