Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run dynamically generated assembly in C (GNU/Linux)

I'm writing a proof-of-concept JIT compiler in C, which at the moment is generating strings of assembly code. The inline assembly functionality in C only deals with string literals that are known at compile time, so I can't use it to run my generated-at-runtime code.

I've read about using mmap() to execute generated machine code at runtime, but I'd like to avoid working with machine code if possible.

Does anyone know of any solutions? I've thought of writing it to a file and invoking the assembler & linker on said file, but that'd be messy and slow.

like image 872
AlexJ136 Avatar asked Aug 13 '13 13:08

AlexJ136


2 Answers

I think ultimately to be "JIT" you need to be time sensitive which means generate machine code. You might try putting in some debug code that generates both machine code to run and assembly code to verify, run the assembler compare the machine code from the assembly language to the machine code you generated directly and use that to debug/validate the machine code (if possible, sometimes assemblers want to do their own thing, not what you wanted them to do).

like image 197
old_timer Avatar answered Sep 29 '22 10:09

old_timer


What I've done is generate C/C++/Fortran code, compile it on the fly, link it into a DLL, and dynamically load the DLL, all of which takes on the order of a few seconds at most. You could do the same, except generate ASM. It's a very effective technique when you need speed of the resulting code, plus the flexibility of the code (and run-time libraries) of the language you're generating.

like image 36
Mike Dunlavey Avatar answered Sep 29 '22 11:09

Mike Dunlavey