I would like to generate some machine code in my program and then run it. One way to do it would be to write out a .so file and then load it in the program but that seems to
The mmap(2) (with munmap(2)
) and mprotect(2) syscalls are the elementary operations to do that. Recall that syscalls are elementary operations from the point of view of an application. You want PROT_EXEC
You could just strace
any dynamically linked executable to get a clue about how you might call them, since the dynamic linker ld.so
is using them.
Generating a shared object might be less expensive than you imagine. Actually, generating C code, running the compiler, then dlopen
-ing the resulting shared object has some sense, even when you work interactively. My MELT domain specific language (to extend GCC) is doing this. Recall that you can do a big lot of dlopen
-s without issues.
If you want to generate machine code in memory, you could use GNU lightning (quick generation of slow machine code), libjit
from dotgnu (generate less bad machine code), LuaJit, asmjit (x86 or amd64 specific), LLVM (slowly generate optimized machine code). BTW, the SBCL Common Lisp implementation is dynamically compiling to memory and produces good machine code at runtime (and there is also all the JIT for JVMs doing that).