How to make gcc generate only machine code that can be loaded directly into memory and executed?

前端 未结 2 1292
长情又很酷
长情又很酷 2020-12-09 04:14

I would like to produce a file that I can load into memory (for example with mmap) and then jump to the start of that memory to run the code.

Ideally, I

2条回答
  •  [愿得一人]
    2020-12-09 05:08

    You can do this but you will need to go through the object file format. In particular, the objcopy command can transform an executable file to a "flat" binary file (depending on your target platform). Perhaps something like this:

    gcc -o test test.c
    objcopy -O binary test test.bin
    

    See man objcopy on your platform for more details.

提交回复
热议问题