Click here to Skip to main content
14,268,970 members
Rate this:
Please Sign up or sign in to vote.
See more:
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;
}


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
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The printf is part of the standard C library so it does not have a simple offset. You need to load the CRT library by the LoadLibrary function[^], and find the specific function address by GetProcAddress[^].
   
Comments
Alexandre Bencz 24-Feb-13 13:15pm
   
not asking for more, but already asking, is there any error in my code, I'm not getting enchergar perhaps the type of variable vector, do not know, but I can not run this code in any way :(
Richard MacCutchan 25-Feb-13 6:02am
   
I am not sure what all those hex values equate to, but I don't see what the above code is supposed to do. The CallWindowProc function is for sending windows messages direct to a Windows message handler. I do not see how you expect to call printf by that method.
Alexandre Bencz 25-Feb-13 6:37am
   
I solved the problem... :) ... see my git with my solved question :)... https://gist.github.com/bencz/5024780

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100