x86 JIT汇编程序,调用C函数 [英] x86 JIT Assembler, call C functions
本文介绍了x86 JIT汇编程序,调用C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨:)
我正在用C ++开发一个简单的JIT Assembly系统,但是,我想在这个jit系统中调用C函数,所以,我想到了......我需要命令的指针......但是,我不知道怎么能得到这个...
这是我的代码
Hi :)
I''m developing a simple JIT Assembly system in C++, but, I whant to call C functions in this jit system, so, what I have thinked... I need the pointer of the command... but, I don''t know how I can get this...
That is my code
#include <cstdio>
#include <vector>
#include <windows.h>
int Execute(std::vector<unsigned char> code)
{
int eaxRegister;
unsigned char* func = (unsigned char*)VirtualAlloc( 0, code.size() + 1, 0x1000, 0x40 );
memcpy( func, code.data(), code.size() );
func[code.size()] = 0xC3; // add the ret to the final of code final
CallWindowProc( (WNDPROC)func, 0, 0, 0, 0 );
_asm mov eaxRegister, eax;
VirtualFree( func, code.size() + 1, 0x4000 );
return eaxRegister;
}
int main()
{
std::vector<unsigned char> code;
//mov eax, 10
code.push_back( 0xc7 );
code.push_back( 0xc0 );
code.push_back( 0xa );
code.push_back( 0x0 );
code.push_back( 0x0 );
code.push_back( 0x0 );
//mov ecx, 10
code.push_back( 0xc7 );
code.push_back( 0xc1 );
code.push_back( 0xa );
code.push_back( 0x0 );
code.push_back( 0x0 );
code.push_back( 0x0 );
//add eax, ecx
code.push_back( 0x3 );
code.push_back( 0xc1 );
// push MESSAGE
const char* ohi = "HI";
code.push_back( 0x69 );
code.push_back( *ohi );
// call prinf ?????
code.push_back( 0xe8 );
code.push_back( 0xfff/* offset of printf */ ) ;
// add esp, 4
code.push_back( 0x83 );
code.push_back( 0xc4 );
code.push_back( 0x04 );
code.push_back( 0x0 );
code.push_back( 0x0 );
code.push_back( 0x0 );
int exec = Execute( code );
printf("SUM = %d", exec);
return 0;
}
所以,我的问题是,我怎样才能获得在JIT中使用的printf命令的偏移量,或者,我如何使用JIT来使用C函数???
谢谢
Alexandre
So, my problem is, how I can get the offset of printf command to use in JIT, or, how I can use the C function using the JIT ???
Thanks
Alexandre
推荐答案
printf
是标准C库的一部分,因此它没有简单的偏移量。您需要通过 <$ c $加载CRT库。 c> LoadLibrary 功能 [ ^ ],并通过GetProcAddress
[ ^ ]。
Theprintf
is part of the standard C library so it does not have a simple offset. You need to load the CRT library by theLoadLibrary
function[^], and find the specific function address byGetProcAddress
[^].
这篇关于x86 JIT汇编程序,调用C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文