Binary Injection into ELF File

206 views Asked by At

I have an ELF file generated through the SDCC compiler using the —out-fmt-elf and —debug options. I need to inject data into the actual binary (this will be loaded to an STM8 microcontroller). Doing this with a hex file is easy, as I can just directly specify the memory address I want the bytes in. However, since SDCC appears to have hacked in DWARF debug support, it is impossible as far as I can tell to split the ELF file into a Hex and Debug file, then rejoin them. Theoretically I could do this with SDCDB, but that doesn’t work with my project infrastructure. It is looking for a main.c, main.asm, project.hex, and project.cdb. When I point it to these files it breaks and I am assuming it is because of how the directories are structured. I would also overall prefer to be able to use GDB.

Is there a way I can inject ~250 bytes at a specific memory address into an ELF file?

Thanks!

1

There are 1 answers

0
Employed Russian On

Is there a way I can inject ~250 bytes at a specific memory address into an ELF file?

Sure: declare the data so it exists in appropriate section and with desired size, e.g.

__attribute__((section, ...))
char my_data[256];

Build/link your binary with a linker script to locate my_data at desired target address.

After the link, find the offset of the data in the file and write desired (new) bytes at that offset.