My embedded projects have a post-process step that replaces a value in the executable with the CRC of (some sections of) the flash. This step can only be done after linking since that is the first opportunity to CRC the image. In the past the file format was COFF, and I have created a custom tool to do the patching.
The development tool has switched to ELF, so I need to re-implement the CRC patcher. Before I do, I thought I'd look for an existing tool to do this. The compiler is based on gcc, but I can't see any combination of ld
and nm
and readelf
that can do the job. A Google search has not been fruitful.
My present tool uses nm
to find the address to patch, and calls the patcher with the address, the expected value (to prevent overwriting the wrong data), and the new CRC value. The CRC is calculated on a "hex" format of the executable (that I also patch) so fortunately I don't have to redo that part.
I can implement this with libelf
and custom code again, but before I do, does it already exist?
Is there a better way to accomplish my goal of putting a CRC of the executable into the executable so it's available to the application?
You can use any hexeditor to do that, if you know precisely which part of ELF you need to modify. If you want to parse ELFs and do more complex logic you should write some code which will open file or better, mmap it.
you can use readelf and objdump to read parts of an elf file. You can also use 'hexdump filename' to get a hexdump of the contents of a binary file (this is likely only useful if you like reading machine code or you are writing an assembler).
An ELF file is an executable file format for the Linux and Unix platforms. Its known file extensions are . axf, . bin, .
If I've understood what you're trying to do correctly, I think the following would work:
nm
gives you the runtime virtual address of the location you want to patch;readelf -S
gives you both the runtime virtual address and the offset within the file for the beginning of each section;If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With