I\'m looking at writing a JIT compiler for a hobby virtual machine I\'ve been working on recently. I know a bit of assembly, (I\'m mainly a C programmer. I can read most ass
A little bit simpler example based on the example above. Thanks to dwelch helped a lot.
#include
#include
#include
#include
char buffer [0x2000];
void* bufferp;
char* hola_mundo = "Hola mundo!";
void (*_printf)(const char*,...);
void hola()
{
_printf(hola_mundo);
}
int main ( void )
{
//Compute the start of the page
bufferp = (void*)( ((unsigned long)buffer+0x1000) & 0xfffff000 );
if(mprotect(bufferp, 1024, PROT_READ|PROT_EXEC|PROT_WRITE))
{
printf("mprotect failed\n");
return(1);
}
//The printf function has to be called by an exact address
_printf = printf;
//Copy the function hola into buffer
memcpy(bufferp,(void*)hola,60 //Arbitrary size);
((void (*)())bufferp)();
return(0);
}