0

I would like to build a program that can expand with new code at runtime (JIT compilation). Wondering, where do I put this new code in an ELF file? Do I put it in the .text section somehow, or the .data section? How does this generally work?

Lance
  • 75,200
  • 93
  • 289
  • 503
  • 1
    If it's just-in-time, it happens while the program is running. So it has nothing to do with any file. – rici Apr 22 '21 at 00:57
  • 3
    I'm not sure I understand what you're trying to do, but for a regular JIT compiler, you don't write anything into a file. You write machine code into memory (just the code - no ELF headers or anything like that) and then you jump into it. – sepp2k Apr 22 '21 at 00:57
  • 3
    You put new code in new memory pages you allocate with malloc. (Or really with `mmap(MAP_ANONYMOUS, PROT_WRITE|PROT_READ|PROT_EXEC)`. Or flip them from R+W to R+X once you're done generating code into them, for systems that enforce W^X). Also related: [Handling calls to (potentially) far away ahead-of-time compiled functions from JITed code](https://stackoverflow.com/q/54947302) – Peter Cordes Apr 22 '21 at 03:06

0 Answers0