3 ms·
This seems like a related question: what is the high-level process for building a JIT compiler? Specifically, where does the output of the compiler actually g
by omginternets 5y ago
This seems like a related question: what is the high-level process for building a JIT compiler? Specifically, where does the output of the compiler actually get stored, how does it get marked as executable, and how does it get "fed" into program execution?
- duped 5y agoYou give executable permissions to a region of memory with machine code in it. The easy way to do this is mmap with PROT_EXEC then memcpy to it. Here's a good tutorial: https://eli.thegreenplace.net/2013/11/05/how-to-jit-an-introduction https://eli.thegreenplace.net/2013/11/05/how-to-jit-an-intro... There is an issue with this on MacOS Big Sur where pages may not be writeable and executable in a hardened runtime. To get around that you need to write the machine code first, then mark it executable. To execute the code you can cast it to a function pointer and call it.
- woodruffw 5y ago> This seems like a related question: what is the high-level process for building a JIT compiler? Specifically, where does the output of the compiler actually get stored, how does it get marked as executable, and how does it get "fed" into program execution? There are a whole bunch of ways to do this, but a common one is to map a region as writable, build your JITted machine code into it, flip write and execute, and point your program counter at it. Typically the part that does the last part also sets up the return handling for when the JITted segment is finished. If you're JITting entire functions at a time, you could instead use the dynamic symbol functionality that most OSes provide: build up your machine code (or other representation), convert it into an object file, load it in, and grab the function by its name.