AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [TYT] Calling functions with LTCG optimization via SDKCall on Windows (https://forums.alliedmods.net/showthread.php?t=320920)

gubka 01-15-2020 12:22

[TYT] Calling functions with LTCG optimization via SDKCall on Windows
 
Since when using LTCG, internal functions often have their own call convention, you can't just call them via SDKCall on Windows, so you need to make a "translator" function that will match stdcall and call the desired function.

For example, let's take CBaseAnimating:: Sequence Duration. it returns via xmm0, the "translator" function is as follows

PHP Code:

55                      push   ebp
89 e5                   mov    ebp
,esp
8b 45 0c                mov    eax
,DWORD PTR [ebp+0xc]
50                      push   eax
8b 45 08                mov    eax
,DWORD PTR [ebp+0x8]
50                      push   eax
b8 00 00 00 00          mov    eax
,0x0 in 0x0 write our function address which should call inside transistor func
ff d0                   call   eax
f3 0f 11 44 24 fc       movss  DWORD PTR 
[esp-0x4],xmm0 move value from xmm0 in stack
d9 44 24 fc             fld    DWORD PTR 
[esp-0x4] ; move value from stack to FPU stack (where float value should be)
89 ec                   mov    esp,ebp
5d                      pop    ebp
c2 08 00                ret    0x8 

It remains the most important thing to implement it somehow on sourcepawn and here comes to the aid of a very good article of Kailo Blog: Working with memory in SourcePawn based on it, Phoenix (PTaH author) did this example

Example: xmm0 reg parameter and return function
Spoiler


GameData:
Spoiler


I also used a similar approach to call a function with fastcall convection which not clear the stack is well. The translator function pop the param from the top of the stack to ecx, edx regs and use a trampoline to call needed function.

Example: fastcall for windows from zp core
Spoiler


All times are GMT -4. The time now is 14:22.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.