How to compile to flat binary?

I’m trying to compile to a flat binary for riscv32, however am having no luck. How can this be done? (preferably without external utilities)

I’m not sure if anything has changed, but I remember this thread with regards to RISC-V binaries.

There is a raw output format, but it is marked as planned, so probably won’t work.

Solved using addObjCopy

Not an answer, but another question: What makes a binary “flat”?

An executable file contains—apart from the actual machine code instructions—some additional data which the OS and linker use to prepare the binary for execution. The code it contains may also be position-independent, making addresses in the code relative so that the linker may decide where in the memory space to put which code before execution (this is used for e.g. shared libraries; position-independent code is not directly executable before it’s processed by the linker).

A flat binary is just raw instructions. You’d generally use that in environments where you have no OS (e.g. embedded microcontrollers), you just flash the raw instructions onto a specific address of the chip’s memory, from whence it gets executed directly by the CPU.

3 Likes

Oh, additionally, an executable file may contain debugging symbols (e.g. DWARF), so it’s generally desirable to emit both an executable file and a flat binary. You’d then flash the flat binary onto a chip and use the executable file on your host PC with a debugger to debug the program you’ve flashed onto the chip.