Slide 16
Slide 16 text
Example of JIT code generation (x86)
#include
#include
#include
typedef uint32_t (*func_t)();
int main() {
void *ptr = mmap(NULL, 4096, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1,
0);
uint8_t *data = (uint8_t*)ptr; // uint32_t func() { return 0x11; }
data[0] = 0xB8; // mov EAX,0x11
data[1] = 0x11; // EAX register is return value in x86 ABI
data[2] = 0x00;
data[3] = 0x00;
data[4] = 0x00;
data[5] = 0xC3; // ret
data[6] = 0x00;
printf("0x%0x\n", ((func_t)ptr)()); // call as C function
return 0;
}